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

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
Advertisements

CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha
Lecture 16 Subroutine Calls and Parameter Passing Semantics Dragon: Sec. 7.5 Fischer: Sec Procedure declaration procedure p( a, b : integer, f :
1 Chapter 8 – Subroutines and Control Abstractions.
Implementing Subprograms
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison- Wesley. All rights reserved. 1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
1 Languages and Compilers (SProg og Oversættere) Sequence control and Subprogram Control.
Chapter 9 Subprograms Sections 1-5 and 9. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Two fundamental abstraction facilities.
ISBN Chapter 10 Implementing Subprograms.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Introduction Two fundamental abstraction facilities.
Run time vs. Compile time
ISBN Chapter 9 Subprograms. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-2 Chapter 9 Topics Introduction Fundamentals.
Semantics of Calls and Returns
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
ISBN Chapter 10 Implementing Subprograms –Semantics of Calls and Returns –Implementing “Simple” Subprograms –Implementing Subprograms with.
1 CSCI 360 Survey Of Programming Languages 9 – Implementing Subprograms Spring, 2008 Doug L Hoffman, PhD.
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
ISBN Chapter 9 Subprograms and Functions –Design Issues –Local Referencing Environments –Parameter-Passing Methods –Parameters that are Subprogram.
Chapter 8 :: Subroutines and Control Abstraction
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
ISBN Chapter 10 Implementing Subprograms.
1 Subprograms Fundamentals of subprograms Design issues for subprograms –Parameter-passing methods –Type checking of parameters –Static or dynamic storage.
CS 2104 Prog. Lang. Concepts Subprograms
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
Chapter 9 Subprograms Fundamentals of Subprograms
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 9 Def: The subprogram call and return operations of a language are together called its subprogram.
1 Chapter 9 © 2002 by Addison Wesley Longman, Inc. 9.2 Fundamentals of Subprograms - General characteristics of subprograms: 1. A subprogram has a single.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 8 Fundamental Characteristics of Subprograms 1. A subprogram has a single entry point 2. The.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
Programming Languages and Paradigms Imperative Programming.
1 Subprograms In Text: Chapter 8. 2 Chapter 8: Subprograms Outline Definitions Referencing environments Parameter passing modes and mechanisms Independent.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University NESTED SUBPROGRAMS.
1 Chapter 10 © 2002 by Addison Wesley Longman, Inc The General Semantics of Calls and Returns - Def: The subprogram call and return operations of.
Chapter 9: Subprograms Introduction Fundamentals of Subprograms
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
ISBN Chapter 10 Implementing Subprograms.
1 CSC 533: Programming Languages Spring 2014 Subprogram implementation  subprograms (procedures/functions/subroutines)  subprogram linkage  parameter.
ISBN Chapter 10 Implementing Subprograms.
Chapter 9 Subprograms. Chapter 9 Topics Introduction Fundamentals of Subprograms Design Issues for Subprograms Local Referencing Environments Parameter-Passing.
Implementing Subprograms
Chapter 10 : Implementing Subprograms
Implementing Subprograms Chapter 10
Implementing Subprograms
Implementing Subprograms
Implementing Subprograms
Chapter 10: Implementing Subprograms Sangho Ha
Implementing Subprograms
Implementing Subprograms
CSC 533: Programming Languages Spring 2015
Implementing Subprograms
Subprograms In Text: Chapter 9.
Languages and Compilers (SProg og Oversættere)
Implementing Subprograms
CSC 533: Programming Languages Spring 2019
Implementing Subprograms
Chapter 10 Def: The subprogram call and return operations of
Presentation transcript:

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 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 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 Activation Record Functional value Parameter Dynamic Link Static Link Return address

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

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