Procedures and Functions Procedures and Functions – subprograms – are named fragments of program they can be called from numerous places  within a main.

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.
Procedure Calls Prof. Sirer CS 316 Cornell University.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
The University of Adelaide, School of Computer Science
The Assembly Language Level
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
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)
1 Handling nested procedures Method 1 : static (access) links –Reference to the frame of the lexically enclosing procedure –Static chains of such links.
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
The Procedure Abstraction Part III: Allocating Storage & Establishing Addressability Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all.
ISBN Chapter 10 Implementing Subprograms.
Runtime Environments Source language issues Storage organization
CS 536 Spring Code generation I Lecture 20.
Run-Time Storage Organization
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Run time vs. Compile time
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
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.
CSC 310 – Imperative Programming Languages, Spring, 2009 Virtual Machines and Threaded Intermediate Code (instead of PR Chapter 5 on Target Machine Architecture)
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
Compiler Construction
Chapter 7 Runtime Environments. Relationships between names and data objects As execution proceeds, the same name can denote different data objects Procedures,
Chapter 6: User-Defined Functions
CPSC 388 – Compiler Design and Construction Runtime Environments.
1 Run-Time Environments. 2 Procedure Activation and Lifetime A procedure is activated when called The lifetime of an activation of a procedure is the.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
Programming Languages and Paradigms Imperative Programming.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Angela Guercio.
1 CIS 461 Compiler Design and Construction Winter 2012 Lecture-Module #16 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Run-Time Environments How do we allocate the space for the generated target code and the data object.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
國立台灣大學 資訊工程學系 薛智文 98 Spring Run Time Environments (textbook ch# 7.1–7.3 )
CSC 8505 Compiler Construction Runtime Environments.
7. Runtime Environments Zhang Zhizheng
1 Run-Time Environments Chapter 7 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate.
Hello world !!! ASCII representation of hello.c.
ISBN Chapter 10 Implementing Subprograms.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Computer structure: Procedure Calls
Implementing Subprograms
Introduction to Compilers Tim Teitelbaum
Functions and Procedures
Chapter 9 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
MIPS Instructions.
Topic 3-a Calling Convention 1/10/2019.
UNIT V Run Time Environments.
Procedures and Calling Conventions
Run Time Environments 薛智文
Where is all the knowledge we lost with information? T. S. Eliot
Presentation transcript:

Procedures and Functions Procedures and Functions – subprograms – are named fragments of program they can be called from numerous places  within a main program  within the body of other subprograms  within themselves (if language allows recursion) they have formal parameters (identifiers) which are associated with argument expressions at a place where the subprogram is called A function returns a result, which can be used in – or as – some expression whose value is stored somewhere or passed along somewhere. A procedure returns no result, it must cause some side-effect if it is to be useful:  perform some I/O  modify some external data structure or the value of some argument expression Compiler Construction1

Parameter passing mechanisms Different languages support a variety of parameter-passing mechanisms. Call-by-value: space is created for a FP, the actual parameter (sometimes called argument) – Rvalue of the ARGEXPR – is calculated and used to initialise the FP. That is, copying of the value occurs. On return, the ARGEXPR is unaffected. Call-by-value-result: space is created for a FP, the actual parameter is calculated and used to initialise the FP. On return, the Rvalue of the FP is stored back into the Lvalue of the ARGEXPR. Call-by-result: space is created for a FP, but it is not initialised and the ARGEXPR is not evaluated. On return, its Rvalue is stored into the Lvalue of the ARGEXPR. Call-by-reference: the Lvalue of the ARGEXPR is used as the Lvalue of the FP, no extra space for it is allocated. Changes to the FP directly affect the ARGEXPR. Call-by-name: the FP is treated as fully synonymous with the ARGEXPR. Compiler Construction2 procedure P(int x, int y, int z) x, y, z are formal parameters ( FP s) call P(var, i+j, 4)) var, i+j, 4 are argument expressions ( ARGEXPR s)

Some of the issues in parameter passing 1.Some kinds of argument expression do not have Lvalues  constants, function calls, expressions with operators –call-by-reference needs a temporary location to be created for them –call-by-(value-)result can perhaps ignore the copy-back in these cases 2.Some kinds of actual parameter are large  arrays, strings, class instances  call-by-value(-result) involves making copies of such objects –expensive  passing pointers to such objects is cheap, the pointers may be dereferenced within the subprogram body –effectively what call-by-reference does –programmers must distinguish between making changes to a copy of an object, and making changes to the original dereferenced object Compiler Construction3

Calling sequence requirements A compiler must arrange for certain things to happen upon call and return On call: Allocate space – in registers, or on control stack  for the FPs, in call-by-value, call-by-result, call-by-value-result  for ARGEXPR Lvalues, in call-by-reference, call-by-result, call-by-value-result Calculate Rvalues and/or Lvalues, as appropriate, and store them in this space Push a return address onto the control stack, jump to callee’s entry point Execute spill code for saving registers, if their contents are needed after return Reserve stack space for local variables and compiler-generated temporaries Adjust the top-of-stack On return: Restore the top-of-stack, return control to the saved point of call (If a function: Place its return value in a register) Copy FP s to ARGEXPR Lvalues, in call-by-result, call-by-value-result Execute spill code to restore registers Compiler Construction4

Dividing responsibilities between caller and callee In a single-language environment, a compiler-writer has some discretion over how to arrange a calling sequence. –In a multi-language environment, conventions must be adhered to. The caller ‘knows’ which registers hold values that are required after return of control, so can provide spill code for exactly those registers  At many points of call, few, if any, registers need be saved then restored –faster code, avoiding recalculation and pointless dumps/restores The callee does not know which registers are important to the caller after return:  At any point of call, it is possible that every register is important to the caller, so provide spill code for saving and restoring all registers –smaller code overall –each call is slower and may be doing work pointlessly Optional arguments, dynamic local arrays etc. may have implications for where, and how many times, the top-of-stack should be adjusted. Compiler Construction5

Inlining An optimisation technique, applicable to small non-recursive subprograms: At a call point, take (abstract) parse tree for function/procedure,  systematically rename all its FP s to new names  replace (abstract) parse tree node for a call to the subprogram with a constructed parse tree corresponding to a block of code that –initialises (renamed) FP s as if they were local variables, –performs the (modified) body of the subprogram –places function value in a register, and/or modifies ARGEXPR values  compile the code for this modified part of the (abstract) parse tree Eliminates much call-sequence overhead:  no transfers of control, push/pop of a return address, unnecessary spill code  no restriction on register use – each call is treated individually Compiler Construction6

Execution strategies An interpreter contains routines that mimic the actions called for by a source program. A parser and lexer produce a (abstract) parse tree, the interpreter then walks this tree, carrying out actions on its own memory structures in sympathy with the desired actions of the program being interpreted. A compile-and-go compiler compiles a main program, possibly accessing subprograms from a subprogram library, initialises memory with the program code it has generated, and transfers control to its start address. Instructions for a Virtual Machine (such as the JVM, with its bytecode) are produced, typically based on stack manipulations. An interpreter for this virtual machine executes these instructions. (Java bytecode is target for some non-Java.) –Machine-specific compilation of these VM instructions may be performed Code for subprograms may be put into, and later got out of, a subprogram library. Compiler Construction7

Subprogram Libraries Compilers often offer the possibility of separate compilation  Header files provide the compiler with information about the signatures of other subprograms mentioned in the subprogram(s) about to be compiled.  Compiler then produces so-called object code, which is written to a file.  Compiler can do this for a main routine as well.  A collection of object code files constitutes a subprogram library. A main routine, and the subprograms it depends on, and the subprograms they in turn depend on and so on, then need to be linked (by a system linker program)  to form a stored executable file; or to be immediately loaded and run. The object files for subprograms may be linked with a variety of main routines. Subprograms need not all be written in the same high-level language. Compiler Construction8

Code Relocation At the time of separate compilation, it is not known where in memory the code for a routine will be when it is executed jumps to instructions need to know an address to jump to references to statically-allocated memory need to know an address to refer to Object code therefore consists of at least two parts the subprogram code, written as if it were going to be loaded into machine memory starting at address zero a relocation map, indicating those instructions containing an address field  when loading, or producing an executable, these instructions are modified by adding the subprogram’s actual starting address to their address field If an executable file is being produced, it is still not known where in memory it will reside: a second round of relocation is needed when loading then running it. Compiler Construction9