Compilation 2014 Activation Records

Size: px
Start display at page:

Download "Compilation 2014 Activation Records"

Transcription

1 Compilation 2014 Activation Records Aslan Askarov Revised from slides by E. Ernst

2 (Abstract) computer organization Program memory code segment contains program text data segment contains static program data (globals) stack for locals and function arguments heap for dynamically allocated memory Processors registers Operations for moving data between registers/memory }size fixed prior to runtime }size changes at runtime

3 Call stack of a program Higher addresses in memory contiguous region in memory that the program can use the stack grows from higher memory addresses to low ones (could be otherwise depending on architecture) Purpose of the stack: store local variables pass arguments store return values save registers Low addresses in memory stack limit set by OS

4 Call stack of a program Higher addresses in memory contiguous region in memory that the program can use the stack grows from higher memory addresses to low ones (could be otherwise depending on architecture) Purpose of the stack: store local variables pass arguments store return addresses save registers Low addresses in memory Q: what happens if we push past beyond stack limit? stack limit set by OS

5 Stack frames Function 1 calls Function 2 that calls Function 3 active functions, because they haven t returned yet Function 1 Function 1 Function 1 Function 2 Function 2 Function 2 Function 2 Function 2 Function 3 Function 3 Function 3 Idea: the maximum amount of memory that each function needs for its locals, temps, etc can be (usually) precomputed by the compiler Let s increase the stack by that much at once instead of many small increases Call the region of the stack corresponding to each active function that function s stack frame (also called activation record) Function 3 stack manipulations by each of the functions

6 Stack frames Function 1 calls Function 2 that calls Function 3 active functions, because they haven t returned yet Function 1 Function 1 activation record (or stack frame) for Function 1 Function 1 Function 2 Function 2 Function 2 Function 2 stack frame for Function 2 Function 2 Function 3 Function 3 Function 3 stack frame for Function 3 Function 3 stack manipulations by each of the functions

7 Frame pointer and Stack pointer Stack pointer (SP): points to the top of the stack FP activation record for Function 1 Frame pointer (FP): the value of SP at the time the frame got activated activation record for Function 2 SP Not allocated: garbage

8 Frame layout: calling conventions Cross-language calls important: using libraries Reasonable to follow a standard: calling convention Specifies stack frame layout, register usage, routine entry, exit code Likely C bias FP SP argument 2 argument 1 returnaddr localvar1 localvar2 storedr1 temp1 Not allocated: garbage

9 Typical frame layout Fits RISC architectures (such as MIPS) well Note staticlink Consider offsets from FP and SP: are all known at compile time? FP could be virtual, if frame size is fixed FP SP (prev. frame) arg_k arg_1 staticlink localvar_1 localvar_m returnaddr temp_1 temp_p saved_r1 saved_rt (args for next)

10 Our Tiger frame layout Fits x86, with simple register usage strategy Worth noting return address pushed automatically by call instruction FP non-virtual, always saved SP adjusted at runtime: arguments pushed old FP FP SP SP SP (prev. frame) arg_k arg_1 staticlink returnaddr saved_fp localvar_1 localvar_m returnaddr temp_1 temp_p nextarg_k nextarg_2 nextarg_1

11 Accessing Tiger frame slots old FP (prev.frame)... arg_k... arg_1 staticlink returnaddr Memory[saved_FP], Memory[FP+4*(2+k)] Memory[FP+4*(2+1)] Memory[FP+8] Memory[FP+4] Memory[staticLink]? FP SP SP SP SP saved_fp localvar_1... localvar_m temp_1... temp_p... (args for next) Memory[FP] Memory[FP-4*1] Memory[FP-4*m] Memory[FP-4*(m+1)] Memory[FP-4*(m+p)] Memory[FP-who_cares] Top of frame: known early Bottom: known later Activations

12 The Frame Pointer Relative concepts: caller/callee frames, routines On routine entry SP points to arg_1 or staticlink or returnaddr.. (will return to this later) Allocate new frame (same figure): push FP FP := SP SP := SP - framesize If SP fixed, may replace FP by SP - framesize ( virtual FP) FP SP (prev.frame)...???

13 Saving Registers Re: A frame/routine may be a caller or a callee Roles for registers: Caller-save vs. callee-save E.g. on MIPS: r16-r23 callee-save, others callersave Don t mess with r16-r23 / Don t rely on others Scenario: Nice if value in r3 is dead before call Scenario: Put long-lived value in r21 (think: loop)

14 Passing Parameters Pre-1960: Use globals, no recursion 1970 ies: Pass parameters on stack Later: 4-6 parameters in registers, rest on stack Experience: Few routines >6 parameters Our approach: Pass parameters on stack (fits x86, C calling conventions)

15 Why is register passing useful? Scenario: Calling f(a1..an) which calls g(z) As much as possible kept in registers (prev.frame)... saved_ri... staticlink Tempting claim: No difference Concept: Leaf routines How rare? Most invocations are leaves May not even need stack frame f frame g frame localvar_1... localvar_m returnaddr... saved_a1... staticlink localvar_1...

16 C extremely well-established, collaboration needed Address-of operator & applicable to arguments Varargs (printf) requires address arithmetics Allocate space for all arguments at end of frame, save only if address taken Some C Language Issues Address taken also known as escaping, may use separate analysis

17 Managing Return Addresses Return address not statically known (.. why?) Solution: Store return address Old approach: Push at call instruction New: Store in register at call instruction Non-leaf routines will have to write to the stack

18 Forcing Memory Storage Using registers a good default, may fail... Address of variable taken ( &, pass-by-reference) Variable used from nested function Variable too large for register (use several?) Pointer arithmetics used (C arrays) Spilling

19 Static links: concept The previous frame is not necessarily the one that is lexically scoped Record information about enclosing frames Pass nearest enclosing frame as hidden argument (static link) Keep display : global array of currently nearest frame at all nesting levels Lambda lifting: lots of arguments We use static links

20 Static Links: Example Code type tree = {key: string, left: tree, right: tree} function prettyprint(tree: tree): string = let var output := " " function write(s: string) = output := concat(output, s) function show(n: int, t: tree) = let function indent(s: string) = ( for i:=1 to n do write(" ") ; output := concat(output,s) ; write("\n")) in if t=nil then indent(".") else ( indent(t.key) ; show(n+1, t.left) ; show(n+1, t.right)) end in show(0,tree); output end Main uses of static link: Emphasized name introductions

21 Static Links: Example Stack type tree = {key: string, left: tree, right: tree} function prettyprint(tree: tree): string = let var output := " " function write(s: string) = output := concat(output, s) function show(n: int, t: tree) = let function indent(s: string) = ( for i:=1 to n do write(" ") ; output := concat(output,s) ; write("\n")) in if t=nil then indent(".") else ( indent(t.key) ; show(n+1, t.left) ; show(n+1, t.right)) end in show(0,tree); output end saved FP [prettyprint] [show] wanted [show] [indent] [write]

22 Static Links: Example Stack type tree = {key: string, left: tree, right: tree} function prettyprint(tree: tree): string = let var output := " " function write(s: string) = output := concat(output, s) function show(n: int, t: tree) = let function indent(s: string) = ( for i:=1 to n do write(" ") ; output := concat(output,s) ; write("\n")) in if t=nil then indent(".") else ( indent(t.key) ; show(n+1, t.left) ; show(n+1, t.right)) end in show(0,tree); output end static link saved FP [prettyprint] [show] wanted [show] [indent] [write]

23 Static Links: Call Nested type tree = {key: string, left: tree, right: tree} function prettyprint(tree: tree): string = let var output := " " function write(s: string) = output := concat(output, s) function show(n: int, t: tree) = let function indent(s: string) = ( for i:=1 to n do write(" ") ; output := concat(output,s) ; write("\n")) in if t=nil then indent(".") else ( indent(t.key) ; show(n+1, t.left) ; show(n+1, t.right)) end in show(0,tree); output end Case call nested : Static link for show from prettyprint is the frame of prettyprint itself, etc. SL = FP SL = FP

24 Static Links: Call Same type tree = {key: string, left: tree, right: tree} function prettyprint(tree: tree): string = let var output := " " function write(s: string) = output := concat(output, s) function show(n: int, t: tree) = let function indent(s: string) = ( for i:=1 to n do write(" ") ; output := concat(output,s) ; write("\n")) in if t=nil then indent(".") else ( indent(t.key) ; show(n+1, t.left) ; show(n+1, t.right)) end in show(0,tree); output end Case call same : Static link for show from show is the given static link SL = SL

25 Static Links: Call Less Nested type tree = {key: string, left: tree, right: tree} function prettyprint(tree: tree): string = let var output := " " function write(s: string) = output := concat(output, s) function show(n: int, t: tree) = let function indent(s: string) = ( for i:=1 to n do write(" ") ; output := concat(output,s) ; write("\n")) in if t=nil then indent(".") else ( indent(t.key) ; show(n+1, t.left) ; show(n+1, t.right)) end in show(0,tree); output end Case call outer : Static link for write from indent is found by following the static link twice SL = SL.SL

26 Modules for activations Goals: Frame should not depend on static links Semant should not depend on machine details in Frame Machine addresses and registers should be abstract where possible (especially in IRgen) Structure of our compiler Note the difference from the book, where top-level translation is implemented in Semant IRgen Top-level translation Translate Translation details Frame Temp Machine dependent elements

27 Structure for frames Module type FRAME encapsulates machine dependent details Uses abstract machine code addresses: Temp signature FRAME = sig type frame type access val newframe: { name: Temp.label, formals: bool list} -> frame val name: frame -> Temp.label val formals: frame -> access list val alloclocal: frame -> bool -> access end IRGen Translate Frame Temp Implementation: structure MipsFrame: FRAME = struct... end Semi-encapsulated usage: structure Frame: FRAME = MipsFrame

28 Structure for frames signature FRAME = sig type frame type access val newframe: { name: Temp.label, formals: bool list} -> frame val name: frame -> Temp.label val formals: frame -> access list val alloclocal: frame -> bool -> access end frame: info about formal parameters and local vars formals: bool list: escaping or not? val formals: create access descriptions of formals datatype access = InFrame of int InReg of Temp.temp val alloclocal: update frame for an extra var -> access

29 Understanding frame access NB: assume arg1 escapes arg1 arg2 arg3 InFrame(8) InFrame(0) InFrame(8) InFrame(12) InReg(t InReg(t InFrame(16) InReg(t InReg(t M[SP] := FP SP := SP - K save SP,-K,SP Viewshift FP := SP M[SP+K] := r2 M[FP+68] := i0 SP := SP - K t157 t157 t158 t158 x86 MIPS SPARC Viewshift may move data and handle changes due to call

30 Abstract registers and addresses Structure Temp provides and manages abstractions: Type temp represents registers (an unbounded set) Type label represents addresses namedlabel allows for labels controlled externally structure Temp: sig eqtype temp val newtemp: unit -> temp type label = Symbol.symbol val newlabel: unit -> label val namedlabel: string -> label... end = struct... end IRgen Translate Frame Temp

31 Adding static links Structure Translate: TRANSLATE hides translation details, adds support for static links to the bottom level Note: Translate.access Frame.access signature TRANSLATE = sig type level type access val outermost: level val newlevel: { parent: level, name: Temp.label, formals: bool list} -> level val formals: level -> access list val alloclocal: level -> bool -> access... end IRgen Translate Frame Temp

32 Environments updated Structure Env: ENV now extended to hold access information for variables and level information for functions signature ENV = sig... datatype enventry = VarEntry of { ty: ty, access: Translate.access} FunEntry of { formals: ty list,, result: ty, level: Translate.level, label: Temp.label}... end

33 Summary Procedural abstraction requires LIFO discipline Use stack push/pop complete frame Frame layout: Convention plus many considerations Our Tiger frame layout: Heavily InFrame-ish Concepts: caller/callee, -save registers, longevity Benefits of using registers, forces against it Static links: Purpose, computation Tiger software Frame, Translate, Temp. Abstract registers and addresses, environment update

Compilation 2014 Activation Records (Part 1)

Compilation 2014 Activation Records (Part 1) Compilation 2014 Activation Records (Part 1) Aslan Askarov aslan@cs.au.dk Revised from slides by E. Ernst (Abstract) computer organization Program memory code segment contains program text data segment

More information

Typical Runtime Layout. Tiger Runtime Environments. Example: Nested Functions. Activation Trees. code. Memory Layout

Typical Runtime Layout. Tiger Runtime Environments. Example: Nested Functions. Activation Trees. code. Memory Layout Tiger Runtime Environments Compile-time environments are just symbol tables; they are used to assist static semantic analysis, code generation and code optimization. Run-time environments are about how

More information

CA Compiler Construction

CA Compiler Construction CA4003 - Compiler Construction David Sinclair When procedure A calls procedure B, we name procedure A the caller and procedure B the callee. A Runtime Environment, also called an Activation Record, is

More information

Topic 7: Activation Records

Topic 7: Activation Records Topic 7: Activation Records Compiler Design Prof. Hanjun Kim CoreLab (Compiler Research Lab) POSTECH 1 Storage Organization Stack Free Memory Heap Static Code 2 ELF file format example Executable Object

More information

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

Compilation /15a Lecture 7. Activation Records Noam Rinetzky Compilation 0368-3133 2014/15a Lecture 7 Activation Records Noam Rinetzky 1 Code generation for procedure calls (+ a few words on the runtime system) 2 Code generation for procedure calls Compile time

More information

System Software Assignment 1 Runtime Support for Procedures

System Software Assignment 1 Runtime Support for Procedures System Software Assignment 1 Runtime Support for Procedures Exercise 1: Nested procedures Some programming languages like Oberon and Pascal support nested procedures. 1. Find a run-time structure for such

More information

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention Runtime Environment The Procedure Abstraction and Separate Compilation Topics we will cover The procedure abstraction and linkage conventions Runtime storage convention Non-local data access (brief) These

More information

Run-time Environment

Run-time Environment Run-time Environment Prof. James L. Frankel Harvard University Version of 3:08 PM 20-Apr-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights reserved. Storage Organization Automatic objects are

More information

Run-time Environments

Run-time Environments Run-time Environments Status We have so far covered the front-end phases Lexical analysis Parsing Semantic analysis Next come the back-end phases Code generation Optimization Register allocation Instruction

More information

Run-time Environments

Run-time Environments Run-time Environments Status We have so far covered the front-end phases Lexical analysis Parsing Semantic analysis Next come the back-end phases Code generation Optimization Register allocation Instruction

More information

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12 Calling Conventions Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University See P&H 2.8 and 2.12 Goals for Today Calling Convention for Procedure Calls Enable code to be reused by allowing

More information

Compilation 2014 Code Generation Issues

Compilation 2014 Code Generation Issues Compilation 2014 Code Generation Issues Aslan Askarov aslan@cs.au.dk Based on slides by E. Ernst Administrativia November 28 guest lecture by Filip Sieczkowski (AU) Memory Models December 12 guest lecture

More information

Module 27 Switch-case statements and Run-time storage management

Module 27 Switch-case statements and Run-time storage management Module 27 Switch-case statements and Run-time storage management In this module we will discuss the pending constructs in generating three-address code namely switch-case statements. We will also discuss

More information

Code Generation. Lecture 12

Code Generation. Lecture 12 Code Generation Lecture 12 1 Lecture Outline Topic 1: Basic Code Generation The MIPS assembly language A simple source language Stack-machine implementation of the simple language Topic 2: Code Generation

More information

Scope, Functions, and Storage Management

Scope, Functions, and Storage Management Scope, Functions, and Storage Management Implementing Functions and Blocks cs3723 1 Simplified Machine Model (Compare To List Abstract Machine) Registers Code Data Program Counter (current instruction)

More information

Compilers and computer architecture: A realistic compiler to MIPS

Compilers and computer architecture: A realistic compiler to MIPS 1 / 1 Compilers and computer architecture: A realistic compiler to MIPS Martin Berger November 2017 Recall the function of compilers 2 / 1 3 / 1 Recall the structure of compilers Source program Lexical

More information

Compilers and Code Optimization EDOARDO FUSELLA

Compilers and Code Optimization EDOARDO FUSELLA Compilers and Code Optimization EDOARDO FUSELLA Contents Data memory layout Instruction selection Register allocation Data memory layout Memory Hierarchy Capacity vs access speed Main memory Classes of

More information

CSE Lecture In Class Example Handout

CSE Lecture In Class Example Handout CSE 30321 Lecture 07-09 In Class Example Handout Part A: A Simple, MIPS-based Procedure: Swap Procedure Example: Let s write the MIPS code for the following statement (and function call): if (A[i] > A

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 4 Thomas Wies New York University Review Last week Control Structures Selection Loops Adding Invariants Outline Subprograms Calling Sequences Parameter

More information

ECE260: Fundamentals of Computer Engineering

ECE260: Fundamentals of Computer Engineering Supporting Nested Procedures James Moscola Dept. of Engineering & Computer Science York College of Pennsylvania Based on Computer Organization and Design, 5th Edition by Patterson & Hennessy Memory Layout

More information

LECTURE 19. Subroutines and Parameter Passing

LECTURE 19. Subroutines and Parameter Passing LECTURE 19 Subroutines and Parameter Passing ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments behind a simple name. Data abstraction: hide data

More information

Code Generation & Parameter Passing

Code Generation & Parameter Passing Code Generation & Parameter Passing Lecture Outline 1. Allocating temporaries in the activation record Let s optimize our code generator a bit 2. A deeper look into calling sequences Caller/Callee responsibilities

More information

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline. The Main Idea of Today s Lecture Code Generation We can emit stack-machine-style code for expressions via recursion (We will use MIPS assembly as our target language) 2 Lecture Outline What are stack machines?

More information

We can emit stack-machine-style code for expressions via recursion

We can emit stack-machine-style code for expressions via recursion Code Generation The Main Idea of Today s Lecture We can emit stack-machine-style code for expressions via recursion (We will use MIPS assembly as our target language) 2 Lecture Outline What are stack machines?

More information

Chap. 8 :: Subroutines and Control Abstraction

Chap. 8 :: Subroutines and Control Abstraction Chap. 8 :: Subroutines and Control Abstraction Michael L. Scott Programming Language Theory 2015, kkman@sangji.ac.kr 1 Review Of Stack Layout Allocation strategies Static Code Globals Own variables Explicit

More information

Run Time Environments

Run Time Environments Run Time Environments ALSU Textbook Chapter 7.1 7.3 Tsan-sheng Hsu tshsu@iis.sinica.edu.tw http://www.iis.sinica.edu.tw/~tshsu 1 Preliminaries During the execution of a program, the same name in the source

More information

Programming Languages

Programming Languages Programming Languages Tevfik Koşar Lecture - XX April 4 th, 2006 1 Roadmap Subroutines Allocation Strategies Calling Sequences Parameter Passing Generic Subroutines Exception Handling Co-routines 2 1 Review

More information

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero Do-While Example In C++ do { z--; while (a == b); z = b; In assembly language loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero 25 Comparisons Set on less than (slt) compares its source registers

More information

Functions and Procedures

Functions and Procedures Functions and Procedures Function or Procedure A separate piece of code Possibly separately compiled Located at some address in the memory used for code, away from main and other functions (main is itself

More information

Run-time Environments - 2

Run-time Environments - 2 Run-time Environments - 2 Y.N. Srikant Computer Science and Automation Indian Institute of Science Bangalore 560 012 NPTEL Course on Principles of Compiler Design Outline of the Lecture n What is run-time

More information

Course Administration

Course Administration Fall 2018 EE 3613: Computer Organization Chapter 2: Instruction Set Architecture Introduction 4/4 Avinash Karanth Department of Electrical Engineering & Computer Science Ohio University, Athens, Ohio 45701

More information

Concepts Introduced in Chapter 7

Concepts Introduced in Chapter 7 Concepts Introduced in Chapter 7 Storage Allocation Strategies Static Stack Heap Activation Records Access to Nonlocal Names Access links followed by Fig. 7.1 EECS 665 Compiler Construction 1 Activation

More information

Compiler Construction Lecture 05 A Simple Stack Machine. Lent Term, Lecturer: Timothy G. Griffin. Computer Laboratory University of Cambridge

Compiler Construction Lecture 05 A Simple Stack Machine. Lent Term, Lecturer: Timothy G. Griffin. Computer Laboratory University of Cambridge Compiler Construction Lecture 05 A Simple Stack Machine Lent Term, 2015 Lecturer: Timothy G. Griffin Computer Laboratory University of Cambridge 1 Where are we going? When we derived the stack machine

More information

Code Generation. Lecture 19

Code Generation. Lecture 19 Code Generation Lecture 19 Lecture Outline Topic 1: Basic Code Generation The MIPS assembly language A simple source language Stack-machine implementation of the simple language Topic 2: Code Generation

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 2 Thomas Wies New York University Review Last week Programming Languages Overview Syntax and Semantics Grammars and Regular Expressions High-level

More information

Lecture Outline. Topic 1: Basic Code Generation. Code Generation. Lecture 12. Topic 2: Code Generation for Objects. Simulating a Stack Machine

Lecture Outline. Topic 1: Basic Code Generation. Code Generation. Lecture 12. Topic 2: Code Generation for Objects. Simulating a Stack Machine Lecture Outline Code Generation Lecture 12 Topic 1: Basic Code Generation The MIPS assembly language A simple source language Stack-machine implementation of the simple language Topic 2: Code Generation

More information

Control Abstraction. Hwansoo Han

Control Abstraction. Hwansoo Han Control Abstraction Hwansoo Han Review of Static Allocation Static allocation strategies Code Global variables Own variables (live within an encapsulation - static in C) Explicit constants (including strings,

More information

CSE 504: Compiler Design. Runtime Environments

CSE 504: Compiler Design. Runtime Environments Runtime Environments Pradipta De pradipta.de@sunykorea.ac.kr Current Topic Procedure Abstractions Mechanisms to manage procedures and procedure calls from compiler s perspective Runtime Environment Choices

More information

Procedure and Function Calls, Part II. Comp 412 COMP 412 FALL Chapter 6 in EaC2e. target code. source code Front End Optimizer Back End

Procedure and Function Calls, Part II. Comp 412 COMP 412 FALL Chapter 6 in EaC2e. target code. source code Front End Optimizer Back End COMP 412 FALL 2017 Procedure and Function Calls, Part II Comp 412 source code IR Front End Optimizer Back End IR target code Copyright 2017, Keith D. Cooper & Linda Torczon, all rights reserved. Students

More information

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University G22.2110-001 Programming Languages Spring 2010 Lecture 4 Robert Grimm, New York University 1 Review Last week Control Structures Selection Loops 2 Outline Subprograms Calling Sequences Parameter Passing

More information

Implementing Subprograms

Implementing Subprograms Implementing Subprograms In Text: Chapter 10 Slide 1 Implementing Subprograms Call - Save registers - provide storage for parameters - provide storage for locals - save execution status of caller - provide

More information

Procedure Call. Procedure Call CS 217. Involves following actions

Procedure Call. Procedure Call CS 217. Involves following actions Procedure Call CS 217 Procedure Call Involves following actions pass arguments save a return address transfer control to callee transfer control back to caller return results int add(int a, int b) { return

More information

Runtime management. CS Compiler Design. The procedure abstraction. The procedure abstraction. Runtime management. V.

Runtime management. CS Compiler Design. The procedure abstraction. The procedure abstraction. Runtime management. V. Runtime management CS3300 - Compiler Design Runtime management V Krishna Nandivada IIT Madras Copyright c 2001 by Antony L Hosking Permission to make digital or hard copies of part or all of this work

More information

CS 314 Principles of Programming Languages. Lecture 13

CS 314 Principles of Programming Languages. Lecture 13 CS 314 Principles of Programming Languages Lecture 13 Zheng Zhang Department of Computer Science Rutgers University Wednesday 19 th October, 2016 Zheng Zhang 1 CS@Rutgers University Class Information Reminder:

More information

CS415 Compilers. Procedure Abstractions. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

CS415 Compilers. Procedure Abstractions. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University CS415 Compilers Procedure Abstractions These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University Where are we? Well understood Engineering Source Code

More information

Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University. See P&H 2.8 and 2.12, and A.

Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University. See P&H 2.8 and 2.12, and A. Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University See P&H 2.8 and 2.12, and A.5 6 compute jump/branch targets memory PC +4 new pc Instruction Fetch

More information

CS153: Compilers Lecture 8: Compiling Calls

CS153: Compilers Lecture 8: Compiling Calls CS153: Compilers Lecture 8: Compiling Calls Stephen Chong https://www.seas.harvard.edu/courses/cs153 Announcements Project 2 out Due Thu Oct 4 (7 days) Project 3 out Due Tuesday Oct 9 (12 days) Reminder:

More information

Procedure Calls Main Procedure. MIPS Calling Convention. MIPS-specific info. Procedure Calls. MIPS-specific info who cares? Chapter 2.7 Appendix A.

Procedure Calls Main Procedure. MIPS Calling Convention. MIPS-specific info. Procedure Calls. MIPS-specific info who cares? Chapter 2.7 Appendix A. MIPS Calling Convention Chapter 2.7 Appendix A.6 Procedure Calls Main Procedure Call Procedure Call Procedure Procedure Calls Procedure must from any call Procedure uses that main was using We need a convention

More information

Lecture08: Scope and Lexical Address

Lecture08: Scope and Lexical Address Lecture08: Scope and Lexical Address Free and Bound Variables (EOPL 1.3.1) Given an expression E, does a particular variable reference x appear free or bound in that expression? Definition: A variable

More information

! Those values must be stored somewhere! Therefore, variables must somehow be bound. ! How?

! Those values must be stored somewhere! Therefore, variables must somehow be bound. ! How? A Binding Question! Variables are bound (dynamically) to values Subprogram Activation! Those values must be stored somewhere! Therefore, variables must somehow be bound to memory locations! How? Function

More information

MIPS Procedure Calls. Lecture 6 CS301

MIPS Procedure Calls. Lecture 6 CS301 MIPS Procedure Calls Lecture 6 CS301 Function Call Steps Place parameters in accessible location Transfer control to function Acquire storage for procedure variables Perform calculations in function Place

More information

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture COMP 303 Computer Architecture Lecture 3 Comp 303 Computer Architecture 1 Supporting procedures in computer hardware The execution of a procedure Place parameters in a place where the procedure can access

More information

Lecture 7: Binding Time and Storage

Lecture 7: Binding Time and Storage Lecture 7: Binding Time and Storage COMP 524 Programming Language Concepts Stephen Olivier February 5, 2009 Based on notes by A. Block, N. Fisher, F. Hernandez-Campos, and D. Stotts Goal of Lecture The

More information

The Procedure Abstraction

The Procedure Abstraction The Procedure Abstraction Procedure Abstraction Begins Chapter 6 in EAC The compiler must deal with interface between compile time and run time Most of the tricky issues arise in implementing procedures

More information

Implementing Subprograms

Implementing Subprograms 1 Implementing Subprograms CS 315 Programming Languages Pinar Duygulu Bilkent University CS315 Programming Languages Pinar Duygulu The General Semantics of Calls and Returns 2 The subprogram call and return

More information

Chapter 8 :: Subroutines and Control Abstraction. Final Test. Final Test Review Tomorrow

Chapter 8 :: Subroutines and Control Abstraction. Final Test. Final Test Review Tomorrow Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott Administrative Notes Final Test Thursday, August 3 2006 at 11:30am No lecture before or after the mid-term

More information

Procedure and Object- Oriented Abstraction

Procedure and Object- Oriented Abstraction Procedure and Object- Oriented Abstraction Scope and storage management cs5363 1 Procedure abstractions Procedures are fundamental programming abstractions They are used to support dynamically nested blocks

More information

Compilation 2013 Translation to IR

Compilation 2013 Translation to IR Compilation 2013 Erik Ernst Aarhus University Intermediate Representation Translation source/target uses IR as a bridge Simplification: n+m combinations, not n m Java SML Pascal C C++ SPARC MIPS Pentium

More information

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem: Expression evaluation CSE 504 Order of evaluation For the abstract syntax tree + + 5 Expression Evaluation, Runtime Environments + + x 3 2 4 the equivalent expression is (x + 3) + (2 + 4) + 5 1 2 (. Contd

More information

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention Subroutines Why we use subroutines more modular program (small routines, outside data passed in) more readable, easier to debug code reuse i.e. smaller code space Memory Usage A software convention stack

More information

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; : Runtime Environment Relationship between names and data objects (of target machine) Allocation & de-allocation is managed by run time support package Each execution of a procedure is an activation of the

More information

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13 Run-time Environments Lecture 13 by Prof. Vijay Ganesh) Lecture 13 1 What have we covered so far? We have covered the front-end phases Lexical analysis (Lexer, regular expressions,...) Parsing (CFG, Top-down,

More information

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1 Lecture #16: Introduction to Runtime Organization Last modified: Fri Mar 19 00:17:19 2010 CS164: Lecture #16 1 Status Lexical analysis Produces tokens Detects & eliminates illegal tokens Parsing Produces

More information

Functions in MIPS. Functions in MIPS 1

Functions in MIPS. Functions in MIPS 1 Functions in MIPS We ll talk about the 3 steps in handling function calls: 1. The program s flow of control must be changed. 2. Arguments and return values are passed back and forth. 3. Local variables

More information

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS Lectures 5 Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS 1 OOPS - What does this C code do? int foo(char *s) { int L = 0; while (*s++) { ++L; } return L; } 2

More information

CS61C : Machine Structures

CS61C : Machine Structures inst.eecs.berkeley.edu/~cs61c/su06 CS61C : Machine Structures Lecture #6: Memory Management CS 61C L06 Memory Management (1) 2006-07-05 Andy Carle Memory Management (1/2) Variable declaration allocates

More information

Anne Bracy CS 3410 Computer Science Cornell University

Anne Bracy CS 3410 Computer Science Cornell University Anne Bracy CS 3410 Computer Science Cornell University The slides are the product of many rounds of teaching CS 3410 by Professors Weatherspoon, Bala, Bracy, McKee, and Sirer. See P&H 2.8 and 2.12, and

More information

CS64 Week 5 Lecture 1. Kyle Dewey

CS64 Week 5 Lecture 1. Kyle Dewey CS64 Week 5 Lecture 1 Kyle Dewey Overview More branches in MIPS Memory in MIPS MIPS Calling Convention More Branches in MIPS else_if.asm nested_if.asm nested_else_if.asm Memory in MIPS Accessing Memory

More information

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program? Lexical Binding There are two ways a variable can be used in a program: As a declaration As a "reference" or use of the variable Scheme has two kinds of variable "declarations" -- the bindings of a let-expression

More information

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University See P&H 2.8 and 2.12, and A.5-6 Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University Upcoming agenda PA1 due yesterday PA2 available and discussed during lab section this week

More information

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;} Subroutines Also called procedures or functions Example C code: int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;} // subroutine converts Celsius to kelvin int celtokel(int i) { return (i

More information

THEORY OF COMPILATION

THEORY OF COMPILATION Lecture 10 Activation Records THEORY OF COMPILATION EranYahav www.cs.technion.ac.il/~yahave/tocs2011/compilers-lec10.pptx Reference: Dragon 7.1,7.2. MCD 6.3,6.4.2 1 You are here Compiler txt Source Lexical

More information

Run Time Environment. Procedure Abstraction. The Procedure as a Control Abstraction. The Procedure as a Control Abstraction

Run Time Environment. Procedure Abstraction. The Procedure as a Control Abstraction. The Procedure as a Control Abstraction Procedure Abstraction Run Time Environment Records Procedure Linkage Name Translation and Variable Access Copyright 2010, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at

More information

G53CMP: Lecture 14. Run-Time Organisation I. Henrik Nilsson. University of Nottingham, UK. G53CMP: Lecture 14 p.1/37

G53CMP: Lecture 14. Run-Time Organisation I. Henrik Nilsson. University of Nottingham, UK. G53CMP: Lecture 14 p.1/37 G53CMP: Lecture 14 Run-Time Organisation I Henrik Nilsson University of Nottingham, UK G53CMP: Lecture 14 p.1/37 This Lecture One aspect of run-time organisation: stack-based storage allocation Lifetime

More information

This Lecture. G53CMP: Lecture 14 Run-Time Organisation I. Example: Lifetime (1) Storage Areas

This Lecture. G53CMP: Lecture 14 Run-Time Organisation I. Example: Lifetime (1) Storage Areas This Lecture G53CMP: Lecture 14 Run-Time Organisation I Henrik Nilsson University of Nottingham, UK One aspect of run-time organisation: stack-based storage allocation Lifetime and storage Basic stack

More information

Chapter 9 :: Subroutines and Control Abstraction

Chapter 9 :: Subroutines and Control Abstraction Chapter 9 :: Subroutines and Control Abstraction Programming Language Pragmatics, Fourth Edition Michael L. Scott Copyright 2016 Elsevier 1 Chapter09_Subroutines_and_Control_Abstraction_4e - Tue November

More information

Recap: Functions as first-class values

Recap: Functions as first-class values Recap: Functions as first-class values Arguments, return values, bindings What are the benefits? Parameterized, similar functions (e.g. Testers) Creating, (Returning) Functions Iterator, Accumul, Reuse

More information

Implementing Procedure Calls

Implementing Procedure Calls 1 / 39 Implementing Procedure Calls February 18 22, 2013 2 / 39 Outline Intro to procedure calls Caller vs. callee Procedure call basics Calling conventions The stack Interacting with the stack Structure

More information

Lecture 5. Announcements: Today: Finish up functions in MIPS

Lecture 5. Announcements: Today: Finish up functions in MIPS Lecture 5 Announcements: Today: Finish up functions in MIPS 1 Control flow in C Invoking a function changes the control flow of a program twice. 1. Calling the function 2. Returning from the function In

More information

CSE 3302 Notes 5: Memory Management

CSE 3302 Notes 5: Memory Management CSE 0 Notes 5: Memory Management (Last updated 10/7/15 1:6 PM) References: Gabbrielli-Martini: 5 Wirth: 4 5.1. IMPLEMENTING SIMPLE SUBPROGRAMS Historical FORTRAN and COBOL Simple = no recursion static

More information

Code Generation. Dragon: Ch (Just part of it) Holub: Ch 6.

Code Generation. Dragon: Ch (Just part of it) Holub: Ch 6. Code Generation Dragon: Ch 7. 8. (Just part of it) Holub: Ch 6. Compilation Processes Again Choice of Intermediate Code Representation (IR) IR examples Parse tree Three address code (e.g., x := y op z)

More information

Implementing Subroutines. Outline [1]

Implementing Subroutines. Outline [1] Implementing Subroutines In Text: Chapter 9 Outline [1] General semantics of calls and returns Implementing simple subroutines Call Stack Implementing subroutines with stackdynamic local variables Nested

More information

Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule

Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule Scope CSC 4181 Compiler Construction Scope and Symbol Table A scope is a textual region of the program in which a (name-to-object) binding is active. There are two types of scope: Static scope Dynamic

More information

7 Translation to Intermediate Code

7 Translation to Intermediate Code 7 Translation to Intermediate Code ( 7. Translation to Intermediate Code, p. 150) This chpater marks the transition from the source program analysis phase to the target program synthesis phase. All static

More information

Today. Putting it all together

Today. Putting it all together Today! One complete example To put together the snippets of assembly code we have seen! Functions in MIPS Slides adapted from Josep Torrellas, Craig Zilles, and Howard Huang Putting it all together! Count

More information

Chapter 2A Instructions: Language of the Computer

Chapter 2A Instructions: Language of the Computer Chapter 2A Instructions: Language of the Computer Copyright 2009 Elsevier, Inc. All rights reserved. Instruction Set The repertoire of instructions of a computer Different computers have different instruction

More information

Computer Architecture. Chapter 2-2. Instructions: Language of the Computer

Computer Architecture. Chapter 2-2. Instructions: Language of the Computer Computer Architecture Chapter 2-2 Instructions: Language of the Computer 1 Procedures A major program structuring mechanism Calling & returning from a procedure requires a protocol. The protocol is a sequence

More information

MIPS Functions and Instruction Formats

MIPS Functions and Instruction Formats MIPS Functions and Instruction Formats 1 The Contract: The MIPS Calling Convention You write functions, your compiler writes functions, other compilers write functions And all your functions call other

More information

CS 314 Principles of Programming Languages. Lecture 11

CS 314 Principles of Programming Languages. Lecture 11 CS 314 Principles of Programming Languages Lecture 11 Zheng Zhang Department of Computer Science Rutgers University Wednesday 12 th October, 2016 Zheng Zhang 1 eddy.zhengzhang@cs.rutgers.edu Class Information

More information

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University CS558 Programming Languages Winter 2018 Lecture 4a Andrew Tolmach Portland State University 1994-2018 Pragmatics of Large Values Real machines are very efficient at handling word-size chunks of data (e.g.

More information

Calling Conventions. See P&H 2.8 and Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University

Calling Conventions. See P&H 2.8 and Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University Calling Conventions See P&H 2.8 and 2.12 Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University Goals for Today Review: Calling Conventions call a routine (i.e. transfer control to

More information

CSCI Compiler Design

CSCI Compiler Design CSCI 565 - Compiler Design Spring 2010 Final Exam - Solution May 07, 2010 at 1.30 PM in Room RTH 115 Duration: 2h 30 min. Please label all pages you turn in with your name and student number. Name: Number:

More information

Lexical Considerations

Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Fall 2005 Handout 6 Decaf Language Wednesday, September 7 The project for the course is to write a

More information

CSE Lecture In Class Example Handout

CSE Lecture In Class Example Handout CSE 30321 Lecture 07-08 In Class Example Handout Part A: J-Type Example: If you look in your book at the syntax for j (an unconditional jump instruction), you see something like: e.g. j addr would seemingly

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Fall 2016 Lecture 4a Andrew Tolmach Portland State University 1994-2016 Pragmatics of Large Values Real machines are very efficient at handling word-size chunks of data (e.g.

More information

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont ) Chapter 2 Computer Abstractions and Technology Lesson 4: MIPS (cont ) Logical Operations Instructions for bitwise manipulation Operation C Java MIPS Shift left >>> srl Bitwise

More information

CSc 520 Principles of Programming Languages. Questions. rocedures as Control Abstractions... 30: Procedures Introduction

CSc 520 Principles of Programming Languages. Questions. rocedures as Control Abstractions... 30: Procedures Introduction Procedures as Control Abstractions CSc 520 Principles of Programming Languages 30: Procedures Introduction Christian Collberg collberg@cs.arizona.edu Department of Computer Science University of Arizona

More information

Arguments and Return Values. EE 109 Unit 16 Stack Frames. Assembly & HLL s. Arguments and Return Values

Arguments and Return Values. EE 109 Unit 16 Stack Frames. Assembly & HLL s. Arguments and Return Values 1 Arguments and Return Values 2 EE 109 Unit 16 Stack Frames MIPS convention is to use certain registers for this task used to pass up to 4 arguments. If more arguments, use the stack used for return value

More information

1 Lexical Considerations

1 Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2013 Handout Decaf Language Thursday, Feb 7 The project for the course is to write a compiler

More information

CS A331 Programming Language Concepts

CS A331 Programming Language Concepts CS A331 Programming Language Concepts Lecture 9 Subroutines March 17, 2014 Sam Siewert Purpose of a Subroutine Control Abstraction perform a well defined operation E.g. transform a color image into a graymap

More information