Lesson 13 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.

Slides:



Advertisements
Similar presentations
The University of Adelaide, School of Computer Science
Advertisements

Procedure Calls Prof. Sirer CS 316 Cornell University.
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Ch. 8 Functions.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
Procedure call frame: Hold values passed to a procedure as arguments
CSE 5317/4305 L7: Run-Time Storage Organization1 Run-Time Storage Organization Leonidas Fegaras.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
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)
CS 536 Spring Run-time organization Lecture 19.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
Runtime Environments Source language issues Storage organization
CS 536 Spring Code generation I Lecture 20.
Run-Time Storage Organization
Run time vs. Compile time
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
Run-time Environment and Program Organization
Stacks and Frames Demystified CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
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.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Runtime Environments Compiler Construction Chapter 7.
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
Program Compilation and Execution. Today’s Objectives Explain why runtime stack needed for C Explain why runtime stack needed for C Draw logical division.
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
CPSC 388 – Compiler Design and Construction Runtime Environments.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Lesson 7 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
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,
CSC 8505 Compiler Construction Runtime Environments.
Intermediate Representation II Storage Allocation and Management EECS 483 – Lecture 18 University of Michigan Wednesday, November 8, 2006.
Compiler Construction Code Generation Activation Records
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
1 Topic 6: Activation Records COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer.
Chapter 14 Functions.
Computer Architecture & Operations I
Lecture 7 Macro Review Stack Frames
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Computer Science 210 Computer Organization
Computer structure: Procedure Calls
CSCI206 - Computer Organization & Programming
COMPILERS Activation Records
Introduction to Compilers Tim Teitelbaum
Procedures (Functions)
Chapter 9 :: Subroutines and Control Abstraction
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2012
Activation Records and Function Calls
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Stack Frame Linkage.
The University of Adelaide, School of Computer Science
Understanding Program Address Space
10/4: Lecture Topics Overflow and underflow Logical operations
UNIT V Run Time Environments.
Procedures and Calling Conventions
Runtime Environments What is in the memory?.
Computer Architecture
Program and memory layout
Topic 2b ISA Support for High-Level Languages
Runtime Stack Activation record for hanoi;
Implementing Functions: Overview
Presentation transcript:

Lesson 13 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Outline Overview of Trac42VM Activation records 2

OVERVIEW OF TRAC42VM 3

Overview of Trac42VM A stack A code memory area Registers: –PC: Points to the next instruction to execute –SP: Points to the top of the stack –FP: Points to the current activation record 4

Memory arrangement Memory is partitioned into different sections –Executable code of a program* –Stack – e.g. local variables* –Static – global and static memory –Heap – dynamically allocated memory The stack and the heap grow and shrink during runtime * Used in Trac42VM 5

Lab 3 vs. lab 1 No named program points, i.e., labels or functions –We use absolute code addresses No names on variables or (an –We use relative stack addresses New data types: int, bool, and string –Need different operations –Need different amount of stack space 6

Data types in Trac42VM Data typeSize Int1 Bool1 String100 7

Instructions in Trac42VM PUSHINT, PUSHBOOL, PUSHSTRING RVALINT, RVALBOOL, RVALSTRING ASSINT, ASSBOOL, ASSSTRING EQINT, EQBOOL, EQSTRING, LTSTRING, LESTRING WRITEINT, WRITEBOOL, WRITESTRING LINK, UNLINK 8

ACTIVATION RECORDS 9

Function calls and the stack Conveys information: –To called function: arguments –From called function: return value –Program state necessary to resume execution after the call (e.g., the return-to address) The information held for one function call: activation record (or stack frame) –The frame pointer register points to the current activation record –One activation record for each active function 10

(Possible) layout of an activation record 11 Actual arguments Return address Frame pointer Local variables Temporary variables Return value Top Bottom

FP-relative addresses int funcA() { int x = funcB(23); return x + funcC(11, 17); } int funcB(int x) { if (x == 0) return funcC(12, 13); return x * funcB(x – 1); } int funcC(int x, int y) { return x + y; } 12

Example 13 Caller’s record 42 Start here 42 FP SP int twice(int x) { 3 int y; 4 y = 2 * x; 5 return y; } 7 a = twice(13); 8 x =...

Example 14 Return value Caller’s record ?? int twice(int x) { 3 int y; 4 y = 2 * x; 5 return y; } 7 a = twice(13); 8 x = FP SP

Example 15 Actual arguments Return value Caller’s record int twice(int x) { 3 int y; 4 y = 2 * x; 5 return y; } 7 a = twice(13); 8 x = FP SP

Example 16 Actual arguments Return address Return value Caller’s record 13 8 * 42 int twice(int x) { 3 int y; 4 y = 2 * x; 5 return y; } 7 a = twice(13); 8 x = FP SP * does not map to high-level view

Example 17 Actual arguments Return address Frame pointer Return value Caller’s record int twice(int x) { 3 int y; 4 y = 2 * x; 5 return y; } 7 a = twice(13); 8 x = FP SP

Example 18 Actual arguments Return address Frame pointer Local variables Return value Callers record ?? 42 int twice(int x) { 3 int y; 4 y = 2 * x; 5 return y; } 7 a = twice(13); 8 x = FP SP

Example 19 Actual arguments Return address Frame pointer Local variables Return value Callers record int twice(int x) { 3 int y; 4 y = 2 * x; 5 return y; } 7 a = twice(13); 8 x = FP SP

Example 20 Actual arguments Return address Frame pointer Local variables Return value Callers record int twice(int x) { 3 int y; 4 y = 2 * x; 5 return y; } 7 a = twice(13); 8 x = FP SP

Example 21 Actual arguments Return address Return value Callers record int twice(int x) { 3 int y; 4 y = 2 * x; 5 return y; } 7 a = twice(13); 8 x = FP SP

Example 22 Actual arguments Return value Callers record int twice(int x) { 3 int y; 4 y = 2 * x; 5 return y; } 7 a = twice(13); 8 x = FP SP

Example 23 Return value Callers record int twice(int x) { 3 int y; 4 y = 2 * x; 5 return y; } 7 a = twice(13); /*-> 26 */ 8 x = FP SP

Calling conventions – responsibility Calling function Reserve return value space Push actual arguments (in reverse order) Push the return-to address Call the function Pop actual arguments Called function Save and re-set FP Reserve space for local variables Assign the return value Restore FP Return to caller 24

In the caller Reserve space for the return value Evaluate and push each argument onto the stack Call the function and push the return-to address at the same time Pop the arguments DECL size (or PUSH) BSR address POP size 25

In the callee Push FP and set FP to a stack address that is fixed throughout the function’s execution Reserve space for all the local variables Do computations… Restore FP to the value it had when entering this function Return to the caller LINK DECL size UNLINK RTS 26

The tasks of the compiler Calculate the addresses relative to FP: –Variables –Parameters –Return value –Generate instructions using proper addresses Generate code using the offsets 27

Offset calculation 28 Actual arguments Return address Frame pointer Local variables Return value Callers record int twice(int x) { 3 int y; 4 y = 2 * x; 5 return y; } 7 a = twice(13); 8 x = FP SP What are the offsets for y, x and the return value?

A complete example Run the code below and assume that initially, FP = 101 and SP = 100. int twice(int x) { int y; y = 2 * x; return y; } void trac42() { int a; a = twice(13); } 29 save FP y &y 2 x 2 * x y = 2 * x = y restore FP return restore FP return save FP a arg. 13 call twice pop arg. a restore FP return 2 [twice] 3 LINK 4 DECL 1 5 LVAL -1(FP) 6 PUSHINT 2 7 RVALINT 2(FP) 8 MULT 9 ASSINT 10 LVAL 3(FP) 11 RVALINT -1(FP) 12 ASSINT 13 UNLINK 14 RTS 15 UNLINK 16 RTS 17 [trac42] 18 LINK 19 DECL 1 20 LVAL -1(FP) 21 DECL 1 22 PUSHINT BSR 2 24 POP 1 25 ASSINT 26 UNLINK 27 RTS

Exercise (1) Show the stack contents as they are just before instruction 26 if initially, FP = 201 and SP = 200. int abs(int x) { if (x < 0) x = -x; return x; } void trac42() { int a; a = abs(-7); } 30 2 [abs] 3 LINK 4 RVALINT 2(FP) 5 PUSHINT 0 6 LTINT 7 BRF 13 8 LVAL 2(FP) 9 RVALINT 2(FP) 10 NEG 11 ASSINT 12 BRA LVAL 3(FP) 14 RVALINT 2(FP) 15 ASSINT 16 UNLINK 17 RTS 18 [trac42] 19 LINK 20 DECL 1 21 LVAL -1(FP) 22 DECL 1 23 PUSHINT 7 24 NEG 25 BSR 2 26 POP 1 27 ASSINT 28 UNLINK 29 RTS save FP x 0 x < 0 jump to else &x x -x x = -x jump past else = x restore FP return save FP a 7 arg. -7 call abs pop the arg. a restore FP return

Exercise (2) Revert this to Trac42 code (invent names of identifiers as needed). Hint 1: The WRITEINT instruction does not pop the written value Hint 2: Do a trace first! 31 2 [id] 3 LINK 4 RVALINT 2(FP) 5 RVALINT 3(FP) 6 ADD 7 WRITEINT 8 POP 1 9 UNLINK 10 RTS 11 [trac42] 12 LINK 13 DECL 1 14 LVAL -1(FP) 15 PUSHINT ASSINT 17 PUSHINT RVALINT -1(FP) 19 PUSHINT ADD 21 BSR 2 22 POP 1 23 POP 1 24 UNLINK 25 RTS

Exercise (3) Translate this to Trac42VM code. void print(string s1, int x, string s2, int y) { write s1; write x; write s2; write y; } void trac42 () { print("One: ", 1, "Two:", 2); } 32

Parameter passing Call by value – push a copy of the value –Used in e.g. C and Trac42 Call by reference – push a pointer to it – E.g. Java objects and references in C++ – Usually combined with call by value Other: –Call by name – similar to macros in C 33

Local functions void trac42(void) { int x; foo(int y) { if (y == 0) return; x *= 2; foo(y – 1); } x = 1; foo(2); } How is x found on the stack? How is y found on the stack, not confusing it with the y of the (recursive) caller? 34

Local functions 35 Actual arguments Return address Frame pointer Local variables Return value Access link

Conclusion An activation record holds information on the stack necessary in function calls The frame pointer points to the activation record of the current active function Local variables, actual arguments, and the return value are accessed through the frame pointer 36