Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.

Slides:



Advertisements
Similar presentations
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Advertisements

1 Languages and Compilers (SProg og Oversættere) Code Generation.
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
CSE 5317/4305 L7: Run-Time Storage Organization1 Run-Time Storage Organization Leonidas Fegaras.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
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:
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
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.
1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.
CS 536 Spring Run-time organization Lecture 19.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
ISBN Chapter 10 Implementing Subprograms.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
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
Catriel Beeri Pls/Winter 2004/5 environment 68  Some details of implementation As part of / extension of type-checking: Each declaration d(x) associated.
Semantics of Calls and Returns
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Run-time Environment and Program Organization
1 CSCI 360 Survey Of Programming Languages 9 – Implementing Subprograms Spring, 2008 Doug L Hoffman, PhD.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
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.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm Hutchinson.
Compilation (Chapter 3) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
Runtime Environments Compiler Construction Chapter 7.
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
Compiler Construction
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Activation Records CS 671 February 7, CS 671 – Spring The Compiler So Far Lexical analysis Detects inputs with illegal tokens Syntactic analysis.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm Hutchinson.
Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard form for.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Activation Records (in Tiger) CS 471 October 24, 2007.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
CSC 8505 Compiler Construction Runtime Environments.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
Functions. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 9-2 JSR Instruction Jumps to a location (like.
7. Runtime Environments Zhang Zhizheng
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
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
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
Code Generation (Chapter 7) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
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.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Chapter 14 Functions.
Run-time organization
Introduction to Compilers Tim Teitelbaum
Chapter 9 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
UNIT V Run Time Environments.
Course Overview PART I: overview material PART II: inside a compiler
Program and memory layout
Course Overview PART I: overview material PART II: inside a compiler
Implementing Functions: Overview
Presentation transcript:

Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture of a compiler PART II: inside a compiler 4Syntax analysis 5Contextual analysis 6Runtime organization 7Code generation PART III: conclusion 8Interpretation 9Review

Runtime Organization (Chapter 6) 2 Routines Data Representation: how to represent values of the source language on the target machine Expression Evaluation: How to organize computing the values of expressions (taking care of intermediate results) Stack Storage Allocation: How to organize storage for variables (considering lifetimes of global and local variables) Routines: How to implement procedures, functions (and how to pass their parameters and return values) Heap Storage Allocation: How to organize storage for variables (considering lifetimes of heap variables ) Object Orientation: Runtime organization for OO languages (how to handle classes, objects, methods)

Runtime Organization (Chapter 6) 3 Routines We call the assembly language equivalent of procedures “routines”. In the preceding material we already learned some things about the implementation of routines in terms of the stack allocation model: Addressing locals and globals through LB, L1, L2, …, and SB Link data: static link, dynamic link, and return address We have yet to learn how the static link and the L1, L2, etc. registers are set up. We have yet to learn how a routine can receive argument values from its caller, and return results to its caller.

Runtime Organization (Chapter 6) 4 Routines We call the assembly language equivalent of procedures “routines”. What are routines? Unlike procedures/functions in higher level languages, they are not directly supported by language constructs. Instead they are modeled in terms of how to use the low-level machine to “emulate” procedures. What behavior needs to be “emulated”? Calling a routine and returning to the caller after completion Passing arguments to a called routine Returning a result from a routine Accessing local and non-local variables

Runtime Organization (Chapter 6) 5 Routines Transferring control to and from a routine: Most low-level processors have CALL and RET instructions for transferring control from caller to callee and back. Transmitting arguments and return values: Caller and callee must agree on a method to transfer argument and return values. => This is called the “routine protocol” There are many possible ways to pass argument and return values. => A routine protocol is like a “contract” between the caller and the callee. There are many possible ways to pass argument and return values. => A routine protocol is like a “contract” between the caller and the callee. ! The routine protocol is often dictated by the operating system.

Runtime Organization (Chapter 6) 6 Routine Protocol Examples The routine protocol depends on the machine architecture (e.g. stack machine versus register machine). Example 1: A possible routine protocol for a RM - Passing of arguments: first argument in R1, second argument in R2, etc. - Passing of return value: return the result (if any) in R0 Note: this example is simplistic: - What if more arguments than registers? - What if the representation of an argument is larger than can be stored in a register? For RM protocols, the protocol usually also specifies who (caller or callee) is responsible for saving the previous contents of registers.

Runtime Organization (Chapter 6) 7 Routine Protocol Examples Example 2: A possible routine protocol for a stack machine - Passing of arguments: pass arguments on the top of the stack. - Passing of return value: leave the return value on the stack top, in place of the arguments. Note: this protocol puts no boundary on the number of arguments and the size of the arguments. Most micro-processors have registers as well as a stack. Such “mixed” machines also often use a protocol like this one. The “Triangle Abstract Machine” also adopts this routine protocol. We now look at it in detail (in TAM).

Runtime Organization (Chapter 6) 8 TAM: Routine Protocol SB LB ST globals just before the calljust after return args SB LB ST globals result What happens in between call and return?

Runtime Organization (Chapter 6) 9 TAM: Routine Protocol LB ST (1) just before the call args (2) just after entry LB ST args link data Note: Going from (1) to (2) in TAM is the execution of a single CALL instruction.

Runtime Organization (Chapter 6) 10 TAM: Routine Protocol (2) just after entry LB ST args link data (3) during execution of routine LB ST args link data local data shrinks and grows during execution (enter/exit blocks, push/pop temporary values)

Runtime Organization (Chapter 6) 11 TAM: Routine Protocol (4) just before return LB ST args link data local data result (5) just after return LB ST result Note: Going from (4) to (5) in TAM is the execution of a single RET instruction.

Runtime Organization (Chapter 6) 12 TAM: Routine Protocol, Example let var g: Integer; func F(m: Integer, n: Integer) : Integer ~ m*n ; proc W(i:Integer) ~ let const s ~ i*i in begin putint(F(i,s)); putint(F(s,s)) end in begin getint(var g); W(g+1) end Example Triangle Program

Runtime Organization (Chapter 6) 13 TAM: Routine Protocol, Example PUSH 1 -- expand globals to make place for g LOADA 0[SB] -- push address of g (pass-by-ref) CALL getint -- read integer into g LOAD 0[SB] -- push value of g CALL succ -- add 1 CALL(SB) W -- call W (using SB as static link) POP 1 -- contract globals PUSH 1 -- expand globals to make place for g LOADA 0[SB] -- push address of g (pass-by-ref) CALL getint -- read integer into g LOAD 0[SB] -- push value of g CALL succ -- add 1 CALL(SB) W -- call W (using SB as static link) POP 1 -- contract globals TAM assembly code: let var g: Integer;... in begin getint(var g); W(g+1) end

Runtime Organization (Chapter 6) 14 TAM: Routine Protocol, Example F: LOAD -2[LB] -- push value of argument m LOAD -1[LB] -- push value of argument n CALL mult -- multiply m and n RETURN(1) 2 -- return, replacing 2-word argument pair by 1-word result F: LOAD -2[LB] -- push value of argument m LOAD -1[LB] -- push value of argument n CALL mult -- multiply m and n RETURN(1) 2 -- return, replacing 2-word argument pair by 1-word result func F(m: Integer, n: Integer) : Integer ~ m*n ; Arguments are addressed relative to LB (note the negative offsets!) Size of return value and argument space needed for updating the stack upon return from call.

Runtime Organization (Chapter 6) 15 TAM: Routine Protocol, Example W: LOAD -1[LB] -- push value of argument i LOAD -1[LB] -- push value of argument i CALL mult -- multiply: result is value of s ( 3[LB] ) LOAD -1[LB] -- push value of argument i LOAD 3[LB] -- push value of local variable s CALL(SB) F -- call F (using SB as static link) … RETURN(0) 1 -- return, replacing 1-word argument by 0-word result W: LOAD -1[LB] -- push value of argument i LOAD -1[LB] -- push value of argument i CALL mult -- multiply: result is value of s ( 3[LB] ) LOAD -1[LB] -- push value of argument i LOAD 3[LB] -- push value of local variable s CALL(SB) F -- call F (using SB as static link) … RETURN(0) 1 -- return, replacing 1-word argument by 0-word result proc W(i: Integer) ~ let const s~i*i in... F(i,s)...

Runtime Organization (Chapter 6) 16 TAM: Routine Protocol, Example let var g: Integer;... in begin getint(var g); W(g+1) end SB g 3 after reading g 3 just before call to W 4 ST SB g ST arg #1

Runtime Organization (Chapter 6) 17 TAM: Routine Protocol, Example proc W(i: Integer) ~ let const s~i*i in... F(i,s)... just after entering W 3 4 SB g LB arg #1 ST link data just after computing s 3 4 SB g LB arg i ST link data 16 just before calling F 3 4 SB g LB arg i ST link data 16 4 s arg #1 arg #2 static link dynamic link

Runtime Organization (Chapter 6) 18 TAM: Routine Protocol, Example func F(m: Integer, n: Integer) : Integer ~ m*n ; just before calling F 3 4 SB g LB arg i ST link data 16 4 s arg #1 arg #2 just after entering F 3 4 SB g LB arg i ST link data 16 4 s arg m arg n link data just before return from F 3 4 SB g LB arg i ST link data 16 4 s arg m arg n link data 64

Runtime Organization (Chapter 6) 19 TAM: Routine Protocol, Example func F(m: Integer, n: Integer) : Integer ~ m*n ; just before return from F 3 4 SB g LB arg i ST link data 16 4 s arg m arg n link data 64 after return from F 3 4 SB g LB arg i ST link data s …

Runtime Organization (Chapter 6) 20 TAM Routine Protocol: Frame Layout Summary LB ST local variables and intermediate results dynamic link static link return address Local data, grows and shrinks during execution. Link data arguments Arguments for current procedure. They were put here by the caller.

Runtime Organization (Chapter 6) 21 Accessing variables, addressing schemas (revised) We now have a complete picture of the different kinds of addresses that are used for accessing variables and formal parameters stored on the stack. Type of variable Global Local Parameter Non-local, 1 level up Non-local, 2 levels up... Load instruction LOAD +offset[SB] LOAD +offset[LB] LOAD -offset[LB] LOAD +offset[L1] LOAD +offset[L2]

Runtime Organization (Chapter 6) 22 Static Links We have already seen somewhat how the static link in the call frame is initialized by a parameter of the CALL instruction. But how can the compiler determine this parameter at compile time? To understand this we need to know a few things about the Triangle programming language. Statically scoped, block-structured language. The static link points to the scope where the function was defined. If there are no higher-order funcs or procs, the compiler always knows which func/proc is being called. That func/proc must be defined somewhere within the active scope. So the static link can always be found in one of the display registers!

Runtime Organization (Chapter 6) 23 Static Links The static link can always be found in one of the display registers: let proc P( ) ~ let proc Q( ) ~ let proc Q1 Q1body proc Q2 Q2body in Qbody proc S( ) ~ let proc S1 S1body proc S2 S2body in Sbody in Pbody in...Main Program... Call fromto MainP Static link? PQSQS SP S1 S2 Q S SB LB SB LB L1 S2L1 L2 SB S1 S P

Runtime Organization (Chapter 6) 24 Arguments We have already discussed how space on the stack is allocated for arguments to routines. We now discuss some specific issues about passing by value versus passing by reference passing of functional and procedural parameters

Runtime Organization (Chapter 6) 25 Arguments: by value or by reference Some programming languages allow two kinds of parameter passing to functions/procedures. Example: in Triangle (similar in Pascal or C ++ ) let proc S(var n:Integer, i:Integer) ~ n:=n+i; var today: record y:integer, m:Integer, d:Integer end; in begin b := {y ~ 2002, m ~ 2, d ~ 22}; ! b is non-local S(var b.m, 6); end let proc S(var n:Integer, i:Integer) ~ n:=n+i; var today: record y:integer, m:Integer, d:Integer end; in begin b := {y ~ 2002, m ~ 2, d ~ 22}; ! b is non-local S(var b.m, 6); end Constant/value parameter Var/reference parameter

Runtime Organization (Chapter 6) 26 Arguments: by value or by reference Value parameters: At the call site the argument is an expression. The evaluation of that expression leaves some value on the stack. This value is passed to the procedure/function. Typical instructions for putting a value parameter on the stack: LOADL 6LOAD 3[L1] Var/reference parameters : Instead of passing a value on the stack, the address of a memory location is pushed. This implies a restriction that only “variable-like” things can be passed to a var parameter. In Triangle there is an explicit keyword var at the call-site, to signal passing a var parameter. In Pascal and C ++ the reference is created implicitly (but the same restrictions apply). Typical instructions for putting a var parameter on the stack: LOADA 5[LB] LOADA 10[SB]