Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Angela Guercio.

Similar presentations


Presentation on theme: "1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Angela Guercio."— Presentation transcript:

1 1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Angela Guercio who’s slides this lecture is based on.

2 2 Subprograms 1.A subprogram has a single entry point 2.The caller is suspended during execution of the called subprogram 3.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

3 3 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)

4 4 Activation Record Functional value Parameter Dynamic Link Static Link Return address

5 5 FORTRAN FORTRAN subprograms can have no more than one activation record instance at any given time –No recursion! The code of all of the program units of a FORTRAN program may reside together in memory, with the data for all units stored together elsewhere The alternative is to store all local subprogram data with the subprogram code

6 6 Implementing Subprograms in ALGOL-like Languages This is more complicated than implementing FORTRAN 77 subprograms because: 1. Local variables are often dynamically allocated 2. Recursion must be supported 3. Static scoping must be supported 4. Parameters are often passed by two methods

7 7 Recursion: it needs a stack Recursive procedures can call themselves an infinite number of times For each of the calls a new activation record is put on top of a stack –a new set of memory location is needed to handle the local variables and the computation The calling pattern is determined at runtime, therefore infinite memory might be needed at run time –this is way memory get exhausted quickly

8 8 Activation record A typical activation record for an ALGOL-like language: Local variables Parameters Dynamic link Static link stack top Return address The activation record format is static, but its size may be dynamic An activation record instance is dynamically created when a subprogram is called

9 9 Pointers in the Stack Dynamic Link (also called the Previous Frame Pointer) –points to the previous calling procedure Static Link –used in languages with nested procedures such as Pascal –used to access non-local variables Return Instruction Pointer –points to the next instruction of the calling procedure Frame Pointer (or “current”) –points to the first location in the current frame Stack Pointer (or “top”) –points to the top of the stack

10 10 Stack Based Language Behavior Calling a subprogram: 1.copy the outgoing parameters from the previous frame to the incoming parameters 2.set up the return link to point to the next instruction 3.set up the dynamic link to point to the previous frame 4.set up the static link to point to the frame of the procedure under which the current procedure is nested 5.save the register which have been altered 6.set up the top of the current frame 7.jump to the instruction area of the called subprogram

11 11 Returning from subprograms Return Semantics: 1.restore the registers 2.If pass-by-value-result parameters are used, move the current values of those parameters to their corresponding actual parameters 3.If it is a function, move the functional value to a place the caller can get it 4.Restore the execution status of the caller by restoring the rest of the pointers 5.Set IP using the return link to transfer control back to the caller

12 12 Nonlocal References - Static Scoping Observation: All variables that can be non-locally accessed reside in some activation record instance in the stack The process of locating a non-local reference: 1. Find the correct activation record instance 2. Determine the correct offset within that activation record instance Finding the offset is easy and can be done at translation time Finding the correct activation record instance: - Static semantic rules guarantee that all non-local variables that can be referenced have been allocated in some activation record instance that is on the stack when the reference is made

13 13 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)

14 14 Call by Name 1.Replace the formal parameter with the name of the actual parameter 2.Name conflicts in the called procedure must be removed rename the conflicting local variables replace the modified code in the calling proceedure int area_of_square(int x) {int y = x * x; return(y);} Invocation of square( a[i] ) is {int y = a[i] * a[i]; return(y);}

15 15 Side effects with Call by Name swap(name: int a, b) { int temp; temp = a; a = b; b = temp; }; after replacing the codevoid main(){ int i=3; int a[5]; a[3]=4; swap(i, a[i]);}int temp; temp = i; i = a[i]; a[i] = temp; Result: i=4 and a[4]=3……a[3] is unaffected!!!

16 16 Call by Value 1.Create a memory location for each formal parameter formal parameters are treated as local variables 2.Evaluate the expression of the actual parameter and store it in the area called “Outgoing Parameters” 3.Copy the value of the actual parameters into formal parameters copying strictly one way no relationship between actual and formal parameter

17 Value Parameter Rules 1.Formal parameter is created on function invocation and it is initialized with the value of the actual parameter 2.Changes to formal parameter do not affect actual parameter 3.Reference to a formal parameter produces the value for it in the current activation record 4.New activation record for every function invocation 5.Formal parameter name is only known within its function 6.Formal parameter ceases to exist when the function completes 7.Activation record memory is automatically released at function completion

18 18 Call by Value: Evaluation Disadvantages –creates a memory location for each data requires excessive memory location –copying cost is high –no values are passed back to callee Advantages –memory access cost is low –no side effect –efficient when a lot of computation on formal parameters is performed

19 19 Call by Reference 1.Create a memory location for each formal parameter in the activation record of the called procedure 2.Formal parameters are treated as pointers to actual parameters NO copy of the value of the actual parameter! Only copy of the address. 3.If the parameter is a complex data structure or an array, one memory location is allocated to save the pointer to the base location of the data structure use indexing to access other locations in the structure.

20 20 Call by Reference: Effect on store Disadvantages –It alters continuously the store of the calling procedure. –The effect on the store is dependent upon the computation order main(); void foo(int *i, *j) {int x, y { x=1; y=2; foo(&x,&x);} *j=4; *i=8} result x=8 –Value of x in the caller depends upon the last assignments of the alias pointer

21 21 Call by Reference: Loss of Commutativity main() { int a,b,x,y x=3;y=4; // x is 3 // x and y are passed by reference a = square-sum(&x,&y) + x; b = x+y;} int square_sum(int *x, *y) { *x=*x * *x; *y=*y * *y; return(*x + *y);} After execution of square_sum x=9, y=16;a=34 –not what we expected! (a would be 28) –if a=x+square_sum(&x, &y) then a=28! loss of commutativity!

22 22 Call by Reference: Evaluation Compare with Call by value evaluation –The advantages of one are the disadvantages of the other Advantages –creates only one memory location for each data requires minimal memory location –copying cost is low –no values are passed but actual parameter is altered in its original location Disadvantages –memory access cost is high (indirect addressing) –efficient when minimal computation on formal parameters is performed –it has possible side effect can allow aliasing

23 23 Call by Reference: Aliasing Disadvantages: Side effect - Aliasing i. Actual parameter collisions: e.g. procedure sub1(a: int, b: int);... sub1(x, x); ii. Array element collisions: e.g. sub1(a[i], a[j]); /* if i = j */ Also, sub2(a, a[i]); iii. Collision between formals and globals e.g. int *global {… sub1(global) …} void sub1(*local){ int * global;..}

24 24 Call by Reference: Aliasing disadvantages Root cause of all of these is: The called subprogram is provided wider access to non-locals than is necessary Aliasing disadvantages: readability, reliability

25 25 Call by Value Result 1.Create a memory location for each formal parameter (same as a call-by value) formal parameters are treated as local variables 2.No alteration of the locations in calling procedure during the execution of the called procedure 3.Result (if available) is passed back to calling procedure Result alters the actual parameter OBSERVE: Call by value is strictly one way and does not allow results to be returned through parameters.

26 26 Call by Value Result: Evaluation It alters the store after the termination of the called procedure. The result is dependent on order of the parameter passing –order dependence may be a problem main(); void foo(value-result int i, j) {int x, y { x=1; y=2; foo(x,x);} j=4; i=8} result x=4 –Value of x in the caller depends on order of assignments at the return

27 27 Call by Value Result: Evaluation Compare it with Call by reference evaluation Disadvantages –creates memory location for each data requires excessive memory location –copying cost is high –values are passed back altering the store after computation Advantages –memory access cost is low (no need for indirect addressing) –efficient when minimal computation on formal parameters is performed –suitable for distributed computing –it has possible side effect

28 28 Removing problems of side effect Restricted use of the pointers –disallow arithmetic on pointers –associate pointers with specific types to disallow type violation Disciplined programming –disciplined use of global and non local variables The non-local variables of a subprogram are those that are visible but not declared in the subprogram Global variables are those that may be visible in all of the subprograms of a program –disciplined used of call by reference Disallow destructive update of variables –variables can be associates with only one value functional programming paradigm logic programming paradigm

29 29 Languages Examples 1. FORTRAN Before 77, pass-by-reference 77 - scalar variables are often passed by value-result 2. ALGOL 60 Pass-by-name is default; pass-by-value is optional 3. ALGOL W Pass-by-value-result 4. C Pass-by-value (but these can be pointers) 5. Pascal and Modula-2 Default is pass-by-value; pass-by-reference is optional

30 30 Languages Examples 6. C++ – Like C, but also allows reference type actual parameters; the corresponding formal parameters can be pointers to constants, which provide the efficiency of pass-by- reference with in-mode semantics 7. Ada All three semantic modes are available 8. Java Primitive datatypes are passed by value, composit data types (Objects, arrays, …) are passed references

31 31 Design Considerations for Parameter Passing 1.Efficiency 2.One-way or two-way - These two are in conflict with one another! –Good programming  limited access to variables, which means one-way whenever possible –Efficiency  pass by reference is fastest way to pass structures of significant size –Also, functions should not allow reference parameters


Download ppt "1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Angela Guercio."

Similar presentations


Ads by Google