Routines and Control Flows Code Generation Professor Yihjia Tsai Tamkang University.

Slides:



Advertisements
Similar presentations
Code Generation for Control Flow Mooly Sagiv Ohad Shacham Chapter 6.4.
Advertisements

Intermediate Code Generation
The University of Adelaide, School of Computer Science
Programming Languages and Paradigms
Programming Languages and Paradigms The C Programming Language.
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
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)
Compiler construction in4020 – lecture 12 Koen Langendoen Delft University of Technology The Netherlands.
Intermediate code generation. Code Generation Create linear representation of program Result can be machine code, assembly code, code for an abstract.
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.
Runtime Environments Source language issues Storage organization
Lecture 18 Procedures Topics Procedural Abstraction Activation records Readings: 7.4 March 20, 2006 CSCE 531 Compiler Construction.
Run time vs. Compile time
Code Generation for Control Flow Mooly Sagiv html:// Chapter 6.4.
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:
Chapter 9: Subprogram Control
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.
Chapter 6 Activation Records. Local Variables, Instantiations int f(int x) { int y = x+x; if (y
C Stack Frames / Pointer variables Stack: Local Variables Pass & Return values Frame Ptr linkage (R5) and PC linkage (R7) Pointer Variables: Defining &
Code Generation for Control Flow Mooly Sagiv html:// Chapter 6.4.
Compiler construction in4020 – lecture 11 Koen Langendoen Delft University of Technology The Netherlands.
Activation Records Chapter 6. 2 Local Variables, Instantiations Ex: function f(x:int) : int = let var y := x + x in if y < 10 then f(y) else y – 1 end.
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.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
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.
Program Compilation and Execution. Today’s Objectives Explain why runtime stack needed for C Explain why runtime stack needed for C Draw logical division.
Lesson 13 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
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.
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.
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
Code Generation for Control Flow Mooly Sagiv html:// Chapter
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
Computer Architecture & Operations I
Computer Science 210 Computer Organization
COMPILERS Activation Records
Activation Records Mooly Sagiv
Run-Time Storage Organization
Run-Time Storage Organization
Introduction to Compilers Tim Teitelbaum
Procedures (Functions)
Implementing Subprograms
Activation Records and Function Calls
Programming Languages (CS 550) Mini Language Compiler
The University of Adelaide, School of Computer Science
Understanding Program Address Space
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Run Time Environments 薛智文
Runtime Environments What is in the memory?.
Computer Architecture
Programming Languages (CS 360) Mini Language Compiler
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Implementing Subprograms
Runtime Stack Activation record for hanoi;
Presentation transcript:

Routines and Control Flows Code Generation Professor Yihjia Tsai Tamkang University

Overview routine types activation records invocation values code generation for control flow statements boolean expressions selection statements repetition statements

Routines and their activations functionality of a routine call supplying a new computing environment passing information to the new environment transfer of the flow-of-control to the new environment and (guaranteed) return returning info from the new environment

Activation records activation record holds local variables parameters working stack administration part routine activation order last-in-first-out  stack otherwise  heap parameter k... parameter 1 lexical pointer registers dynamic link return info local variables working stack frame ptr FP stack ptr SP

working stack Routine invocation registers dynamic link return address local variables FP SP parameter k parameter 1... caller builds new stack frame for callee push parameters push admin transfer control to callee stack return address, adjust FP allocate space for local vars execute code

working stack parameter n parameter 1... Routine invocation registers dynamic link return address local variables FP SP parameter k parameter 1... caller builds new stack frame for callee push parameters push admin transfer control to callee stack return address, adjust FP allocate space for local vars execute code

Routine invocation registers dynamic link return address working stack parameter n parameter 1... registers dynamic link return address local variables FP SP parameter k parameter 1... caller builds new stack frame for callee push parameters push admin transfer control to callee stack return address, adjust FP allocate space for local vars execute code

Routine invocation registers dynamic link return address working stack parameter n parameter 1... registers dynamic link return address local variables FP parameter k parameter 1... SP caller builds new stack frame for callee push parameters push admin transfer control to callee stack return address, adjust FP allocate space for local vars execute code

Routine invocation registers dynamic link return address working stack parameter n parameter 1... registers dynamic link return address local variables parameter k parameter 1... local variables SP FP caller builds new stack frame for callee push parameters push admin transfer control to callee stack return address, adjust FP allocate space for local vars execute code

Routine invocation caller builds new stack frame for callee push parameters push admin transfer control to callee stack return address, adjust FP allocate space for local vars execute code registers dynamic link return address working stack parameter n parameter 1... registers dynamic link return address local variables parameter k parameter 1... local variables SP FP working stack

Nested routines activation record contains a lexical pointer or static link that points to outer scope int aap( int i) { int noot() {return i+7;} int mies(int i) {return i+noot();} return mies(2)-3; } static link dynamic link return address parameter i aap static link dynamic link return address parameter i mies parameter i noot static link dynamic link return address

Exercise (5 min.) Given the GNU C routine: a) draw the stack that results from the call A(2) b) how does C access the parameter (a) from A? void A(int a) { void B(int b) { void C(void) { printf(“C called, a = %d\n”, a); } if (b == 0) C() else B(b-1); } B(a); }

Answers

dynamic links static links A 2 B 2 B 1 B 0 C FP->static_link->static_link->param[#i]

Operations on routines calling passing as a parameter returning as a value currying [functional programming]

Passing a routine as a parameter int aap(int i) {return i+7;} int noot(int i, int (*f)(int)) {return (*f)(i);} int mies(int i) {return noot(i,aap);} flat int noot(int (*f)()) {return (*f)();} int mies(int i) { int aap() {return i+7;} return noot(aap); } nested static link address

Returning a routine as a value typedef int (*fptr)(int); int aap(int i) {return i+7;} fptr mies() {return aap;} flat return routine address typedef int (*fptr)(); fptr mies(int i) { int aap() {return i+7;} return aap; } nested return routine descriptor and allocate activation records on the heap

Lambda lifting remove static links, only global routines out-of-scope variables referencing: pass additional pointers creating: heap allocation typedef int (*fptr)(); fptr mies(int i) { int aap() {return i+7;} return aap; } nested int aap(int *i) {return *i+7;} fptr mies(int i) { int *_i = malloc(sizeof(i)); *_i = i; return closure(aap,_i); } lifted

Code generation for control flow statements glue between basic blocks transfer typetarget instruction jumpGOTO label indirect jumpGOTO register conditional jump IF a  b THEN GOTO label cmpl %eax,%edx jle L3

Boolean expressions in flow of control why special code generation? performance: avoid conversions lazy semantics: || and && in C if (list != NULL && list->key < key) {... } Code expr( ” list != NULL ”, r1) Code expr( ” list->key < key ”, r2) IF NOT(r1 and r2) THEN GOTO label-end … label-end:

PROCEDURE Boolean control code(Node, True label, False label): SELECT Node.type CASE Lazy and type: Boolean control code( Node.left, No label, False label); Boolean control code( Node.right, True label, False label); CASE Lazy or type: Boolean control code( Node.left, True label, No label); Boolean control code( Node.right, True label, False label); CASE Comparison type: Code expr( Node.left, R1) Code expr( Node.right, R2); IF True label /= No label): Emit(”IF” R1 Node.op R2 ”THEN GOTO” True label); IF False label /= No label): Emit( ”GOTO” False label); ELSE Emit(”IF” R1 Inv(Node.op) R2 ”THEN GOTO” False label); PROCEDURE Boolean control code(Node, True label, False label): SELECT Node.type CASE Lazy and type: Boolean control code( Node.left, No label, False label); Boolean control code( Node.right, True label, False label); CASE Lazy or type: Boolean control code( Node.left, True label, No label); Boolean control code( Node.right, True label, False label); CASE Comparison type: Code expr( Node.left, R1) Code expr( Node.right, R2); IF True label /= No label): Emit(”IF” R1 Node.op R2 ”THEN GOTO” True label); IF False label /= No label): Emit( ”GOTO” False label); ELSE Emit(”IF” R1 Inv(Node.op) R2 ”THEN GOTO” False label); PROCEDURE Boolean control code(Node, True label, False label): SELECT Node.type CASE Lazy and type: Boolean control code( Node.left, No label, False label); Boolean control code( Node.right, True label, False label); CASE Lazy or type: Boolean control code( Node.left, True label, No label); Boolean control code( Node.right, True label, False label); CASE Comparison type: Code expr( Node.left, R1) Code expr( Node.right, R2); IF True label /= No label): Emit(”IF” R1 Node.op R2 ”THEN GOTO” True label); IF False label /= No label): Emit( ”GOTO” False label); ELSE Emit(”IF” R1 Inv(Node.op) R2 ”THEN GOTO” False label); PROCEDURE Boolean control code(Node, True label, False label): SELECT Node.type CASE Lazy and type: Boolean control code( Node.left, No label, False label); Boolean control code( Node.right, True label, False label); CASE Lazy or type: Boolean control code( Node.left, True label, No label); Boolean control code( Node.right, True label, False label); CASE Comparison type: Code expr( Node.left, R1) Code expr( Node.right, R2); IF True label /= No label): Emit(”IF” R1 Node.op R2 ”THEN GOTO” True label); IF False label /= No label): Emit( ”GOTO” False label); ELSE Emit(”IF” R1 Inv(Node.op) R2 ”THEN GOTO” False label); PROCEDURE Boolean control code(Node, True label, False label): SELECT Node.type CASE Lazy and type: Boolean control code( Node.left, No label, False label); Boolean control code( Node.right, True label, False label); CASE Lazy or type: Boolean control code( Node.left, True label, No label); Boolean control code( Node.right, True label, False label); CASE Comparison type: Code expr( Node.left, R1) Code expr( Node.right, R2); IF True label /= No label): Emit(”IF” R1 Node.op R2 ”THEN GOTO” True label); IF False label /= No label): Emit( ”GOTO” False label); ELSE Emit(”IF” R1 Inv(Node.op) R2 ”THEN GOTO” False label);

The if-statement if -statement IFTHENELSE conditiontrue stmtfalse stmt Boolean control code( condition, No_label, false_label) Code statement( true stmt) GOTO end_label; false_label: Code statement( false stmt) end_label:

The if-statement if -statement IFTHENELSE conditiontrue stmtfalse stmt Boolean control code( condition, No_label, false_label) Code statement( true stmt) GOTO end_label: false_label: Code statement( false stmt) end_label: Boolean control code( condition, No_label, end_label) Code statement( true stmt) end_label:

The case-statement CASE expression IN I 1 : statement 1... I n : statement n ELSE statement default END CASE Code expr( expression, R expr ) IF R expr = I 1 THEN GOTO label_1... IF R expr = I n THEN GOTO label_n GOTO label_else; label_1: Code statement (statement 1 ) GOTO end_label;... label_n: Code statement (statement n ) GOTO end_label; label_else: Code statement (statement default ) end_label:

The case-statement CASE expression IN I 1 : statement 1... I n : statement n ELSE statement default END CASE Code expr( expression, R expr ) IF R expr < I low THEN GOTO label_else IF R expr > I high THEN GOTO label_else GOTO case_table[R expr - I low ]; label_1:....data case_table: label_low... label_high jump table

test_label: Boolean control code( condition, No_label, end_label) Code statement( statement) GOTO test_label; end_label: GOTO test_label; loop_label: Code statement( statement) test_label: Boolean control code( condition, loop_label, No_label) The while-statement while -statement WHILEDOEND WHILE conditionstatement

The for-statement FOR i IN lower bound.. upper bound DO statement END FOR i := lower bound ; tmp_ub := upper bound ; WHILE i <= tmp_ub DO statement i := i+1; END FOR Code expr( lower bound, R i ) Code expr( upper bound, R ub ) IF R i > R ub THEN GOTO end_label loop_label: Code statement (statement) IF R i = R ub THEN GOTO end_label R i := R i +1; GOTO loop_label; end_label: test before increment

Exercise (3 min.) give a translation schema for repeat statements repeat -statement REPEATUNTILEND REPEAT statementcondition

Answers

repeat_label: Code statement( statement) Boolean control code( condition, No_label, repeat_label) Answers repeat -statement REPEATUNTILEND REPEAT statementcondition

Summary (nested) routines activation records routine descriptors code generation for control flow statements conditional expressions: true & false labels case-statement: jump table for-statement: overflow static link dynamic link return address parameter i mies parameter i noot static link dynamic link return address