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

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

CA Compiler Construction

Acknowledgement. CS Compiler Design. Semantic Processing. Alternatives for semantic processing. Intro to Semantic Analysis. V.

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

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

Programming Languages

CSE 504: Compiler Design. Runtime Environments

Acknowledgement. CS Compiler Design. Intermediate representations. Intermediate representations. Semantic Analysis - IR Generation

ECE232: Hardware Organization and Design

Implementing Procedure Calls

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

Chap. 8 :: Subroutines and Control Abstraction

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

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

Register allocation. CS Compiler Design. Liveness analysis. Register allocation. Liveness analysis and Register allocation. V.

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

CS 314 Principles of Programming Languages. Lecture 13

The compilation process is driven by the syntactic structure of the program as discovered by the parser

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

The Activation Record (AR)

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

Chapter 9 :: Subroutines and Control Abstraction

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

Runtime Support for Algol-Like Languages Comp 412

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

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

Lecture 7: Procedures and Program Execution Preview

Course Administration

CS 61c: Great Ideas in Computer Architecture

ECE260: Fundamentals of Computer Engineering

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

CS 480 Fall Runtime Environments. Mike Lam, Professor. (a.k.a. procedure calls and heap management)

Functions in MIPS. Functions in MIPS 1

CPS311 Lecture: Procedures Last revised 9/9/13. Objectives:

12/4/18. Outline. Implementing Subprograms. Semantics of a subroutine call. Storage of Information. Semantics of a subroutine return

Naming in OOLs and Storage Layout Comp 412

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

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

Anne Bracy CS 3410 Computer Science Cornell University

Alternatives for semantic processing

Control Abstraction. Hwansoo Han

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

Introduction to optimizations. CS Compiler Design. Phases inside the compiler. Optimization. Introduction to Optimizations. V.

LECTURE 19. Subroutines and Parameter Passing

Functions and Procedures

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

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

ECE331: Hardware Organization and Design

Implementing Subroutines. Outline [1]

Today. Putting it all together

Concepts Introduced in Chapter 7

G Programming Languages - Fall 2012

CS 61C: Great Ideas in Computer Architecture More MIPS, MIPS Functions

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

CS61C : Machine Structures

Lecture 7: Procedures

CS61C : Machine Structures

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

Run Time Environments

CS A331 Programming Language Concepts

CS153: Compilers Lecture 8: Compiling Calls

Compilers and computer architecture: A realistic compiler to MIPS

The Procedure Abstraction

CS 110 Computer Architecture Lecture 6: More MIPS, MIPS Functions

Topic 3-a. Calling Convention 2/29/2008 1

Lecture 5: Procedure Calls

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

MIPS Datapath. MIPS Registers (and the conventions associated with them) MIPS Instruction Types

Anne Bracy CS 3410 Computer Science Cornell University

2/16/2018. Procedures, the basic idea. MIPS Procedure convention. Example: compute multiplication. Re-write it as a MIPS procedure

Instruction Set Architectures (4)

MIPS Procedure Calls. Lecture 6 CS301

UCB CS61C : Machine Structures

CS 2210 Programming Project (Part IV)

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

Run Time Environment. Activation Records Procedure Linkage Name Translation and Variable Access

CSE Lecture In Class Example Handout

ECE 15B Computer Organization Spring 2010

Compiling Code, Procedures and Stacks

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

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

Review of Activation Frames. FP of caller Y X Return value A B C

Chapter 10. Implementing Subprograms

Topic 7: Activation Records

CS 316: Procedure Calls/Pipelining

Q1: /20 Q2: /30 Q3: /24 Q4: /26. Total: /100

Storage in Programs. largest. address. address

SPIM Procedure Calls

Procedure Call and Return Procedure call

The Procedure Abstraction Part I: Basics

ECE260: Fundamentals of Computer Engineering. Supporting Procedures in Computer Hardware

Procedure and Object- Oriented Abstraction

THEORY OF COMPILATION

Lecture 5: Procedure Calls

Arrays and Functions

Run-time Environment

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

Lecture 9: Procedures & Functions. CS 540 George Mason University

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.

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

Transcription:

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 for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and full citation on the first page To copy otherwise, to republish, to post on servers, or to redistribute to lists, requires prior specific permission and/or fee Request permission to publish from hosking@cspurdueedu VKrishna Nandivada (IIT Madras) CS3300 - Aug 2014 2 / 28 The procedure abstraction Separate compilation: allows us to build large programs keeps compile times reasonable requires independent procedures The linkage convention: a social contract machine dependent division of responsibility The linkage convention ensures that procedures inherit a valid run-time environment and that they restore one for their parents Linkages execute at run time Code to make the linkage is generated at compile time The procedure abstraction The essentials: on entry, establish p s environment at a call, preserve p s environment on exit, tear down p s environment in between, addressability and proper lifetimes procedure P prologue pre call call post call epilogue procedure Q prologue epilogue Each system has a standard linkage VKrishna Nandivada (IIT Madras) CS3300 - Aug 2014 3 / 28 VKrishna Nandivada (IIT Madras) CS3300 - Aug 2014 4 / 28

Procedure linkages Assume that each procedure activation has an associated activation record or frame (at run time) Assumptions: RISC architecture can always expand an allocated block locals stored in frame frame pointer stack pointer incoming arguments outgoing arguments argument n argument 2 argument 1 local variables return address temporaries saved registers argument m argument 2 argument 1 higher addresses previous frame current frame next frame lower addresses VKrishna Nandivada (IIT Madras) CS3300 - Aug 2014 5 / 28 Procedure linkages The linkage divides responsibility between caller and callee Caller Callee Call pre-call prologue 1 allocate basic frame 1 save registers, state 2 evaluate & store params 2 store FP (dynamic link) 3 store return address 3 set new FP 4 jump to child 4 store static link 5 extend basic frame 6 initialize locals Return post-call 7 fall through to code epilogue 1 copy return value 1 store return value 2 deallocate basic frame 2 restore state 3 restore parameters 3 cut back to basic frame (if copy out) 4 restore parent s FP 5 jump to return address At compile time, generate the code to do this At run time, that code manipulates the frame & data areas VKrishna Nandivada (IIT Madras) CS3300 - Aug 2014 6 / 28 Run-time storage organization Run-time storage organization To maintain the illusion of procedures, the compiler can adopt some conventions to govern memory use Code space fixed size statically allocated (link time) Typical memory layout high address stack free memory Data space fixed-sized data may be statically allocated variable-sized data must be dynamically allocated Control stack dynamic slice of activation tree return addresses may be implemented in hardware heap static data code low address The classical scheme allows both stack and heap maximal freedom code and static data may be separate or intermingled VKrishna Nandivada (IIT Madras) CS3300 - Aug 2014 7 / 28 VKrishna Nandivada (IIT Madras) CS3300 - Aug 2014 8 / 28

Run-time storage organization Storage classes Where do local variables go? When can we allocate them on a stack? Key issue is lifetime of local names Downward exposure: called procedures may reference my variables dynamic scoping lexical scoping Upward exposure: can I return a reference to my variables? functions that return functions continuation-passing style With only downward exposure, the compiler can allocate the frames on the run-time call stack Each variable must be assigned a storage class Static variables: addresses compiled into code (usually) allocated at compile-time limited to fixed size objects control access with naming scheme Global variables: almost identical to static variables layout may be important naming scheme ensures universal access Link editor must handle duplicate definitions (base address) (relocatable) (exposed) VKrishna Nandivada (IIT Madras) CS3300 - Aug 2014 9 / 28 VKrishna Nandivada (IIT Madras) CS3300 - Aug 2014 10 / 28 Storage classes (cont) Access to non-local data Procedure local variables Put them on the stack if sizes are fixed if lifetimes are limited if values are not preserved Dynamically allocated variables Must be treated differently call-by-reference, pointers, lead to non-local lifetimes (usually) an explicit allocation explicit or implicit deallocation How does the code find non-local data at run-time? Real globals visible everywhere naming convention gives an address initialization requires cooperation Lexical nesting view variables as (level,offset) pairs chain of non-local access links more expensive to find (compile-time) (at run-time) VKrishna Nandivada (IIT Madras) CS3300 - Aug 2014 11 / 28 VKrishna Nandivada (IIT Madras) CS3300 - Aug 2014 12 / 28

Access to non-local data Access to non-local data Two important problems arise How do we map a name into a (level,offset) pair? Use a block-structured symbol table (remember last lecture?) look up a name, want its most recent declaration declaration may be at current level or any lower level Given a (level,offset) pair, what s the address? Two classic approaches access links displays (or static links) To find the value specified by (l,o) need current procedure level, k k = l local value k > l find l s activation record k < l cannot occur Maintaining access links: (static links ) calling level k + 1 procedure 1 pass my FP as access link 2 my backward chain will work for lower levels calling procedure at level l < k 1 find link to level l 1 and pass it 2 its access link will work for lower levels VKrishna Nandivada (IIT Madras) CS3300 - Aug 2014 13 / 28 VKrishna Nandivada (IIT Madras) CS3300 - Aug 2014 14 / 28 The display Display management To improve run-time access costs, use a display: table of access links for lower levels lookup is index from known offset takes slight amount of time at call a single display or one per frame for level k procedure, need k 1 slots Access with the display assume a value described by (l,o) find slot as display[l] add offset to pointer from slot (display[l][o]) Setting up the basic frame now includes display manipulation Single global display: complex, obsolete method bogus idea, do not use Call from level k to level l if l = k + 1 add a new display entry for level k if l = k no change to display is required if l < k preserve entries for levels l through k 1 in the local frame On return (back in calling procedure) if l < k restore preserved display entries A single display ties up another register VKrishna Nandivada (IIT Madras) CS3300 - Aug 2014 15 / 28 VKrishna Nandivada (IIT Madras) CS3300 - Aug 2014 16 / 28

Display management Display management Single global display: Key insight: overallocate the display by 1 slot On entry to a procedure at level l save the level l display value push FP into level l display slot On return restore the level l display value Quick, simple, and foolproof! simple method Individual frame-based displays: Call from level k to level l if l k copy l 1 display entries into child s frame if l > k (l = k + 1) copy k 1 entries into child s frame copy own FP into k th slot in child s frame No work required on return display is deallocated with frame Display accessed by offset from FP one less register required VKrishna Nandivada (IIT Madras) CS3300 - Aug 2014 17 / 28 VKrishna Nandivada (IIT Madras) CS3300 - Aug 2014 18 / 28 Display versus access links Parameter passing How to make the trade-off? The cost differences are somewhat subtle frequency of non-local access average lexical nesting depth ratio of calls to non-local access What about parameters? Call-by-value store values, not addresses never restore on return arrays, structures, strings are a problem Call-by-reference pass address access to formal is indirect reference to actual (Sort of) Conventional wisdom tight on registers use access links lots of registers use global display shallow average nesting frame-based display Your mileage will vary Making the decision requires understanding reality Call-by-value-result store values, not addresses always restore on return arrays, structures, strings are a problem Call-by-name build and pass thunk access to parameter invokes thunk all parameters are same size in frame! VKrishna Nandivada (IIT Madras) CS3300 - Aug 2014 19 / 28 VKrishna Nandivada (IIT Madras) CS3300 - Aug 2014 20 / 28

Parameter passing What about variable length argument lists? 1 if caller knows that callee expects a variable number 1 caller can pass number as 0 th parameter 2 callee can find the number directly 2 if caller doesn t know anything about it 1 callee must be able to determine number 2 first parameter must be closest to FP Consider printf : number of parameters determined by the format string it assumes the numbers match VKrishna Nandivada (IIT Madras) CS3300 - Aug 2014 21 / 28 Calls: Saving and restoring registers caller s registers callee s registers all registers callee saves 1 3 5 caller saves 2 4 6 1 Call includes bitmap of caller s registers to save/restore (best with save/restore instructions to interpret bitmap) 2 Caller saves and restores its own registers Unstructured returns (eg, non-local gotos, exceptions) create some problems, since code to restore must be located and executed 3 Backpatch code to save regs used in callee on entry, restore on exit eg, VAX places bitmap in callee s stack frame for use on call/return/non-local goto/exception Non-local gotos and exceptions must unwind dynamic chain restoring callee-saved registers 4 Bitmap in callee s stack frame is used by caller to save/restore (best with save/restore instructions to interpret bitmap directly) Unwind dynamic chain as for 3 5 Easy: Non-local gotos and exceptions must restore all registers from outermost callee 6 Easy (use utility routine to keep calls compact): Non-local gotos and exceptions need only restore original registers from caller VKrishna Nandivada (IIT Madras) CS3300 - Aug 2014 22 / 28 Top-left is best: saves fewer registers, compact calling sequences Call/return Assuming callee saves: 1 caller pushes space for return value 2 caller pushes SP 3 caller pushes space for: return address, static chain, saved registers 4 caller evaluates and pushes actuals onto stack 5 caller sets return address, callee s static chain, performs call 6 callee saves registers in register-save area 7 callee copies by-value arrays/records using addresses passed as actuals 8 callee allocates dynamic arrays as needed 9 on return, callee restores saved registers 10 jumps to return address Caller must allocate much of stack frame, because it computes the actual parameters Alternative is to put actuals below callee s stack frame in caller s: common when hardware supports stack management (eg, VAX) VKrishna Nandivada (IIT Madras) CS3300 - Aug 2014 23 / 28 MIPS procedure call convention Registers: Number Name Usage 0 zero Constant 0 1 at Reserved for assembler 2, 3 v0, v1 Expression evaluation, scalar function results 4 7 a0 a3 first 4 scalar arguments 8 15 t0 t7 Temporaries, caller-saved; caller must save to preserve across calls 16 23 s0 s7 Callee-saved; must be preserved across calls 24, 25 t8, t9 Temporaries, caller-saved; caller must save to preserve across calls 26, 27 k0, k1 Reserved for OS kernel 28 gp Pointer to global area 29 sp Stack pointer 30 s8 (fp) Callee-saved; must be preserved across calls 31 ra Expression evaluation, pass return address in calls VKrishna Nandivada (IIT Madras) CS3300 - Aug 2014 24 / 28

MIPS procedure call convention MIPS procedure call convention The stack frame Philosophy: Use full, general calling sequence only when necessary; omit portions of it where possible (eg, avoid using fp register whenever possible) Classify routines as: non-leaf routines: routines that call other routines leaf routines: routines that do not themselves call other routines leaf routines that require stack storage for locals leaf routines that do not require stack storage for locals virtual frame pointer ($fp) frame offset high memory argument n argument 1 static link locals saved $ra temporaries other saved registers framesize argument build stack pointer ($sp) low memory VKrishna Nandivada (IIT Madras) CS3300 - Aug 2014 25 / 28 VKrishna Nandivada (IIT Madras) CS3300 - Aug 2014 26 / 28 MIPS procedure call convention MIPS procedure call convention Pre-call: 1 Pass arguments: use registers $a0 $a3; remaining arguments are pushed on the stack along with save space for $a0 $a3 2 Save caller-saved registers if necessary 3 Execute a jal instruction: jumps to target address (callee s first instruction), saves return address in register $ra Prologue: 1 Leaf procedures that use the stack and non-leaf procedures: 1 Allocate all stack space needed by routine: local variables saved registers sufficient space for arguments to routines called by this routine subu $sp,framesize 2 Save registers ($ra, etc): sw $31,framesize+frameoffset($sp) sw $17,framesize+frameoffset-4($sp) sw $16,framesize+frameoffset-8($sp) where framesize and frameoffset (usually negative) are compile-time constants 2 Emit code for routine VKrishna Nandivada (IIT Madras) CS3300 - Aug 2014 27 / 28 VKrishna Nandivada (IIT Madras) CS3300 - Aug 2014 28 / 28

MIPS procedure call convention Epilogue: 1 Copy return values into result registers (if not already there) 2 Restore saved registers lw reg,framesize+frameoffset-n($sp) 3 Get return address lw $31,framesize+frameoffset($sp) 4 Clean up stack addu $sp,framesize 5 Return j $31 VKrishna Nandivada (IIT Madras) CS3300 - Aug 2014 29 / 28