Languages and Compilers (SProg og Oversættere) Sequence control and Subprogram Control
Sequence control and Subprogram Control Evaluation of expressions Explicit sequence control vs. structured sequence control Subprogram implementation Parameter mechanisms
Sequence control Implicit and explicit sequence control Expressions Precedence rules Associativity Statements Sequence Conditionals Iterations Subprograms Declarative programming Functional Logic programming
Expression Evaluation Determined by operator evaluation order operand evaluation order Operators: Most operators are either infix or prefix (some languages have postfix) Order of evaluation determined by operator precedence and associativity
… but it depends on the precedence of operators Example What is the result for: 3 + 4 * 5 + 6 Possible answers: 41 = ((3 + 4) * 5) + 6 47 = 3 + (4 * (5 + 6)) 29 = (3 + (4 * 5)) + 6 = 3 + ((4 * 5) + 6) 77 = (3 + 4) * (5 + 6) In most language, 3 + 4 * 5 + 6 = 29 … but it depends on the precedence of operators
Precedence table for ADA Operator Precedence Operators of highest precedence evaluated first (bind more tightly). Precedence for operators usually given in a table, e.g.: In APL, all infix operators have same precedence Level Operator Operation Highest ** abs not Exp, abs, negation * / mod rem + - Unary + - & Binary = <= < > => Relations Lowest And or xor Boolean Precedence table for ADA
C precedence levels Precedence Operators Operator names 17 tokens, a[k], f() Literals, subscripting, function call .,-> Selection 16 ++, -- Postfix increment/decrement 15* ++, -- Prefix inc/dec , -, sizeof Unary operators, storage !,&,* Logical negation, indirection 14 typename Casts 13 *, /, % Multiplicative operators 12 +,- Additive operators 11 <<, >> Shift 10 <,>,<=, >= Relational 9 ==, != Equality 8 & Bitwise and 7 Bitwise xor 6 | Bitwise or 5 && Logical and 4 || Logical or 3 ?: Conditional 2 =, +=, -=, *=, Assignment /=, %=, <<=, >>=, &=, =, |= 1 , Sequential evaluation Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000
Control of Statement Execution Sequential Conditional Selection Looping Construct Must have all three to provide full power of a Computing Machine
Loops Main types: Counter-controlled iteraters (For-loops) Logical-test iterators Recursion
Gotos Requires notion of program point Transfers execution to given program point Basic construct in machine language Implements loops
Advance in Computer Science Standard constructs that structure jumps if … then … else … end while … do … end for … { … } case … Modern style Group code in logical blocks Avoid explicit jumps except for function return Cannot jump into middle of block or function body But there may be situations when “jumping” is the right thing to do!
Exceptions: Structured Exit Terminate part of computation Jump out of construct Pass data as part of jump Return to most recent site set up to handle exception Unnecessary activation records may be deallocated May need to free heap space, other resources Two main language constructs Declaration to establish exception handler Statement or expression to raise or throw exception Often used for unusual or exceptional condition, but not necessarily.
Subprograms A subprogram has a single entry point The caller is suspended during execution of the called subprogram Control always returns to the caller when the called subprogram’s execution terminates Functions or Procedures? Procedures provide user-defined statements Functions provide user-defined operators
Executing Subprograms The subprogram call and return operations of a language are together called its subprogram linkage Call Semantics: 1. Save the execution status of the caller 2. Carry out the parameter-passing process 3. Pass the return address 4. Transfer control to the callee Return Semantics: 1. If pass-by-value-result parameters are used, move the current values of those parameters to their corresponding actual parameters 2. If it is a function, move the functional value to a place the caller can get it 3. Restore the execution status of the caller 4. Transfer control back to the caller Required Storage: - Status information of the caller, parameters, returnaddress, and functional value (if it is a function)
Code segment generated once at compiled time Activation Records Code segment generated once at compiled time Activation record instances created at run time Fresh activation record instance created for each call of subprogram Usually put in a stack (top inserted first)
Subprogram Implementation Code segment Activation Record Code to create activation record inst Program code Code to delete activation record inst const 1 const n Return point Static link Dynamic link Result data Input parameters Local parameters
Activation Records Static link – pointer to bottom of activation record instance of static parent Used to find non-local variables Dynamic link – pointer to top of activation record instance of the caller Used to delete subprogram activation record instance at completion
Subprogram Parameters Formal parameters: names (and types) of arguments to the subprogram used in defining the subprogram body Actual parameters: arguments supplied for formal parameters when subprogram is called Actual/Formal Parameter Correspondence: attributes of variables are used to exchange information Name – Call-by-name Memory Location – Call-by reference Value Call-by-value (one way from actual to formal parameter) Call-by-value-result (two ways between actual and formal parameter) Call-by-result (one way from formal to actual parameter)