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

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

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

The Procedure Abstraction

Runtime Support for Algol-Like Languages Comp 412

Naming in OOLs and Storage Layout Comp 412

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

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

Implementing Subroutines. Outline [1]

Optimizer. Defining and preserving the meaning of the program

CS 314 Principles of Programming Languages. Lecture 13

Run-time Environments

Run-time Environments

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

Procedure and Object- Oriented Abstraction

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

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

Control Abstraction. Hwansoo Han

Concepts Introduced in Chapter 7

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

CSE 504: Compiler Design. Runtime Environments

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

Arrays and Functions

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

Run-time Environment

Runtime Support for OOLs Object Records, Code Vectors, Inheritance Comp 412

CA Compiler Construction

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

What about Object-Oriented Languages?

Topic 7: Activation Records

Functions and Procedures

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

The Procedure Abstraction Part I Basics

Implementing Subprograms

Compilers and Code Optimization EDOARDO FUSELLA

The Procedure Abstraction Part I: Basics

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

System Software Assignment 1 Runtime Support for Procedures

Lecture 7: Binding Time and Storage

Run Time Environments

Functions in MIPS. Functions in MIPS 1

CS 406/534 Compiler Construction Intermediate Representation and Procedure Abstraction

Today. Putting it all together

Runtime Environments I. Basilio B. Fraguela

Programming Languages

Compilation 2014 Activation Records (Part 1)

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

Procedure Call. Procedure Call CS 217. Involves following actions

CSE 3302 Notes 5: Memory Management

Compilers and computer architecture: A realistic compiler to MIPS

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

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

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

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

Scope, Functions, and Storage Management

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

Chapter 3:: Names, Scopes, and Bindings

CIT Week13 Lecture

Implementing Subprograms

Programming Languages

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

Code Generation & Parameter Passing

Chap. 8 :: Subroutines and Control Abstraction

G Programming Languages - Fall 2012

Implementing Procedure Calls

Implementing Subprograms

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

Programming Languages: Lecture 12

Chapter 10. Implementing Subprograms ISBN

Chapter 10. Implementing Subprograms

Subroutines and Control Abstraction. CSE 307 Principles of Programming Languages Stony Brook University

Special Topics: Programming Languages

Run-Time Environments

Course Administration

Chapter 9 :: Subroutines and Control Abstraction

Plan. Regression testing: Demo of how to use the regress.sh script.

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

Compilation 2014 Activation Records

CSE Lecture In Class Example Handout

Binding and Storage. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

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

Systems I. Machine-Level Programming V: Procedures

THEORY OF COMPILATION

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

Intermediate Code Generation (ICG)

Outline. Unresolved references

Run-time Environments - 2

TILE PROCESSOR APPLICATION BINARY INTERFACE

Classifying Information Stored in Memory! Memory Management in a Uniprogrammed System! Segments of a Process! Processing a User Program!

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary

Control Instructions

Stack Frames. September 2, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 September 2, / 15

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

Lecture 7: Procedures and Program Execution Preview

Storage in Programs. largest. address. address

CS453 Register Allocation

CSC 8400: Computer Systems. Using the Stack for Function Calls

Chapter 5. Names, Bindings, and Scopes

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

Transcription:

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 issues are critical to high-performance code generation Topics not covered : Garbage collection support Heap management Exception handling support Optimization for cache and TLB: covered next semester Nevertheless, these issues are important for performance. Requires system-wide compact must involve architecture, OS, and compiler broad agreement on memory layout, protection requirements, calling sequence, error handling and reporting Separate compilation Compilation strategy that allows subsets (files, modules, or directories) of a single program to be compiled separately and then linked together essential for building large systems keeps compile times reasonable requires independent procedures Establishes the need for private context create a run-time record for each procedure invocation encapsulate run-time control and data information Topic 6: Runtime Environments p.1/21 Topic 6: Runtime Environments p.2/21 Storage Management Conventions The linkage convention 1. Storage layout convention defines memory layout of code and data (static, stack, heap) mostly specified by operating system 2. Linkage convention protocol for passing values and program control at procedure call and return all code in a single program must follow common convention partly specified by processor and OS; rest is left to compiler Related Tools linker: resolve name references across procedures static linking: absolute virtual addresses chosen at link-time dynamic linking: some addresses chosen at load time loader: loads code and static data into memory The linkage convention ensures that procedures inherit a valid run-time environment and that they restore one for their parents. specifies steps in calling sequence and return sequence division of responsibility between caller and callee procedure p (caller) prolog pre call post return epilog procedure q (callee) prolog epilog At compile-time: generate code to implement linkage convention At run-time: that code manipulates the stack frame and data areas Topic 6: Runtime Environments p.3/21 Topic 6: Runtime Environments p.4/21

Activation Record or Stack Frame Example Stack Frame Layout At run-time, each procedure invocation has some associated storage. We call this its activation record. For most languages, activation records can live on the stack, and then they are also called stack frames. Why is this useful? Caller s Frame Common components of a stack frame? Frame Pointer return value return addr. caller s FP Assumptions Stack grows downwards in memory Dedicated frame pointer (FP) register Stack pointer (SP), if any, is separate spilled regs fixed Also see Sparc V9 Stack Frame handout. variable size saved regs Topic 6: Runtime Environments p.5/21 Topic 6: Runtime Environments p.6/21 Procedure linkages Caller: allocate basic frame store store return address save caller-saved regs store self s FP set FP for child jump to child Caller: copy return value deallocate basic frame restore caller-saved regs. At a call At a return Callee: save callee-saved regs, state extend frame (for ) initialize fall through to code Callee: store return value restore callee-saved regs, state unextend frame restore parent s FP jump to return address Logical address-space low C o d e S t a t i c H e a p Code functions: fixed size, statically allocated Control stack holds frames at run-time: dynamic slice of the activation tree free memory S t a c k high Data initialized global data uninitialized global data statically allocated local data variable-size or dynamic local data dynamic non-local data Topic 6: Runtime Environments p.7/21 Topic 6: Runtime Environments p.8/21

Mapping variables into memory Compile time Binding mapping of a name to an attribute (e.g., storage location) assign each name a class (base address) & offset (location) Code generation: generate instruction sequence for each access Link time decide static memory layout resolve references across procedures (global variables and functions) Load time load code and static data into memory resolve relocatable labels Run time execute linkage code at each procedure invocation execute generated instruction sequences Each variable must be assigned a storage class Global and static variables symbolic addresses compiled into code (relocatable) limited to fixed-size objects layout may be important for performance compiler enforces access rights (private vs. global) Dynamically Allocated (non-local) Variables returned pointers lead to non-local lifetimes explicit allocation explicit or implicit deallocation Procedure-local variables Put in the stack frame if lifetime is limited (see next slide) Otherwise allocate on the heap On stack: May be allocated statically or dynamically On stack: May be fixed-size or variable size On stack: Implicit deallocation at procedure return Topic 6: Runtime Environments p.9/21 Topic 6: Runtime Environments p.10/21 Downward exposure Do local variables go on stack? = Lifetime of memory is key called procedure may reference my variables lexical or dynamic scoping Upward exposure return a reference to my variables return a function that may reference my variables special case: later instance of current function or callee may reference my varibles e.g., static (C) or save (FORTRAN) problem: value must be preserved across calls How should these variables be allocated? Assume all are local/formal. C 1 QueueItem* q; /* q? */ 2 static int n; /* n? */ Java 1 QueueItem q = new QueueItem; // q? new object? COOL 1 let m: Int <- a+b in self.n <- m -- m? self? OCaml 1 # let findfunc = fun x -> (* x? *) 2 let p = makepair (x+1) (x+2) in (* p? *) 3 (fun y -> (car p > y));; (* y? fun? *) Topic 6: Runtime Environments p.11/21 Topic 6: Runtime Environments p.12/21

Access to non-local data Conditions for Stack Allocation Allocate procedure-local data values in the stack frame if the locations are downwards-exposed only = values are not preserved across calls Optimization Escape analysis: Decides whether a local variable or object can be used after procedure returns. Important for languages like Java, C# where all objects are heap-allocated by default. Compiler assign variable an offset (k) from some symbolic address (label) generate unique label for each name (name mangling) code to emit: loadi label r1 loadai r1, k, r2 Global & static variables Static linking Resolve all labels at link-time Relocation Assigns each label an absolute virtual address Dynamic linking Relocate some labels at run-time Indirection table for external references Compiler: generates extra load to get base address from table Runtime linker: fills addresses of labels into indirection table Topic 6: Runtime Environments p.13/21 Topic 6: Runtime Environments p.14/21 Access to non-local data Access to non-local data Languages without nested procedures : Local Variables with Lexical Scoping C, C++, Java, etc. Non-local names must be global or static Note: Heap-allocated data has no name: need some non-heap variable to access it (e.g., C pointer or Java reference) Languages with nested procedures (e.g., Pascal) : view variables as level,offset pairs find pointer to appropriate activation record for level add offset to level s pointer more expensive to access than The level,offset pair is called a Static Distance Coordinate (compile-time) Two important problems arise How do we map a name into a level,offset pair? Given a level,offset pair, how to compute the address? How do we map a name into a level & offset? Use a scoped symbol table look up a name, want its most recent declaration declaration may be at current level or any lower level offsets directly determined by stack frame layout Given a level & offset, what s the address? Two classic approaches s or static links displays See slides and Cooper-Torczon text if interested. (compile-time) (run-time) (run-time) Topic 6: Runtime Environments p.15/21 Topic 6: Runtime Environments p.16/21

Access links: run-time addressability Access links: run-time addressability The idea each frame contains a pointer to its lexical parent chain of indirection follows lexical nesting The implementation creates a chain of frames FP by lexical ancestry can follow chain to find level k frame must maintain chain at call & return level 2 level 1 level 0 ret. value caller FP Note: caller FP is not same as To find the value specified by l, o (assume k = current procedure level = 2) l, o Generated code 2, 8 loadai FP, 8 r2 1, 12 0, 16 loadai FP, -4 r1 loadai r1, 12 r2 loadai FP, -4 r1 loadai r1, -4 r1 loadai r1, 16 r2 Access cost for non- varies with k l Note: k < l cannot occur Maintaining s: (static links ) calling level k + 1 procedure 1. pass my FP as 2. my backward chain will work for lower levels calling procedure at level l < k 1. find my link to level l 1 and pass it 2. its will work for lower levels Topic 6: Runtime Environments p.17/21 Topic 6: Runtime Environments p.18/21 The display: run-time addressability The display: run-time addressability The idea replace linked list with global array indexed by level global array: holds currently active frame pointers The implementation single, global table each call updates the table appropriately reference is one indirection through table Display level 0 level 1 level 2 level 3 level 2 FP saved ptr. level 1 saved ptr. level 0 ret. value saved ptr. caller FP To find the value specified by l, o (assume k = 2) l, o Generated code 2, 8 loadai FP, 8 r2 1, 12 0, 16 loadi DISPLAY BASE r1 loadai r1, 4 r1 loadai r1, 12 r2 loadi DISPLAY BASE r1 loadai r1, 8 r1 loadai r1, 16 r2 Desired FP is at DB + 4 l Access cost is constant & independent of k l Maintaining a global display (overallocate by 1 slot) 1. on entry to procedure at level l save level l display ptr in frame push FP into level l display slot 2. on return restore the level l display slot from frame Quick, simple, & foolproof Topic 6: Runtime Environments p.19/21 Topic 6: Runtime Environments p.20/21

Minor issues (Displays & s) Improvements Leaf procedures with display, don t update it statically allocate frame Other procedures 1 call in loop move frame manipulation out of loop keep accessed FP elements in temporaries (contain no calls) Cost comparison Display load + store on CALL & RETURN loadi + 2 loadai s per access Access links store on CALL, nothing on RETURN (k l + 1) loadai s per access Major problem: threads Topic 6: Runtime Environments p.21/21