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

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

Scope, Functions, and Storage Management

System Software Assignment 1 Runtime Support for Procedures

Run-time Environment

CIT Week13 Lecture

Run-time Environments

Run-time Environments

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

Procedure and Object- Oriented Abstraction

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

Topic 7: Activation Records

CA Compiler Construction

Implementing Subprograms

Course Administration

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

Run Time Environments

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

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

G Programming Languages - Fall 2012

Concepts Introduced in Chapter 7

Announcements. Scope, Function Calls and Storage Management. Block Structured Languages. Topics. Examples. Simplified Machine Model.

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

Run-time Environments - 2

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

ECE232: Hardware Organization and Design

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

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

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

CSC 2400: Computing Systems. X86 Assembly: Function Calls

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

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

Compilers and computer architecture: A realistic compiler to MIPS

Stacks and Frames Demystified. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

Assembly Language: Function Calls

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

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?

Compiler Design. Spring Run-Time Environments. Sample Exercises and Solutions. Prof. Pedro C. Diniz

THEORY OF COMPILATION

Stacks and Function Calls

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

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

Programming Languages

Assembly Language: Function Calls" Goals of this Lecture"

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

ECE260: Fundamentals of Computer Engineering

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

Assembly III: Procedures. Jo, Heeseung

G Programming Languages - Fall 2012

Code Generation & Parameter Passing

Assembly Language: Function Calls" Goals of this Lecture"

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

CS 4110 Programming Languages & Logics. Lecture 35 Typed Assembly Language

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

Instruction Set Architectures (4)

Systems I. Machine-Level Programming V: 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; :

CS 314 Principles of Programming Languages. Lecture 13

Run-Time Environements

CSE 3302 Notes 5: Memory Management

ECE331: Hardware Organization and Design

CS 314 Principles of Programming Languages

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

CSE Lecture In Class Example Handout

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

Language of the Machine Recursive functions

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

Memory Management and Run-Time Systems

Implementing Subroutines. Outline [1]

The Procedure Abstraction Part I Basics

Assembly Language: Function Calls

CS213. Machine-Level Programming III: Procedures

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

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 3 MODULE, AUTUMN SEMESTER COMPILERS ANSWERS. Time allowed TWO hours

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CSC 1600 Memory Layout for Unix Processes"

CMa simple C Abstract Machine

Code Generation. Lecture 12

Run-Time Environments

Lexical Considerations

Name, Scope, and Binding. Outline [1]

Lecture 7: Binding Time and Storage

Functions in MIPS. Functions in MIPS 1

Memory management COSC346

Runtime Environments I. Basilio B. Fraguela

Contents. 8-1 Copyright (c) N. Afshartous

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

Procedure Call. Procedure Call CS 217. Involves following actions

Project. there are a couple of 3 person teams. a new drop with new type checking is coming. regroup or see me or forever hold your peace

LECTURE 19. Subroutines and Parameter Passing

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

MIPS Procedure Calls. Lecture 6 CS301

Machine-level Programming (3)

An Overview to Compiler Design. 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 1

Compilation 2014 Activation Records (Part 1)

The Activation Record (AR)

Special Topics: Programming Languages

Announcements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM.

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

Transcription:

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 allocation: - stack frames - s Allocation for nested procedures: - non-local variable access - static links G53CMP: Lecture 14 p.1/37 G53CMP: Lecture 14 p.2/37 Storage Areas Example: Lifetime (1) Static storage: storage for entities that live throughout an execution. Stack storage: storage allocated dynamically, but deallocation must be carried out in the opposite order to allocation. Heap storage: region of the memory where entities can be allocated and deallocated dynamically as needed, in any order. var x, y:... proc P() var p1, p2:... begin... end proc Q() var q1, q2:... begin... if... Q();... end proc R() var r1, r2:... begin... Q()... end begin... P()... R()... end G53CMP: Lecture 14 p.3/37 G53CMP: Lecture 14 p.4/37

Example: Lifetime (2) Example: Lifetime (3) time private static Integer foo(int i) { Integer n = new Integer(i); x, y, z } return n; p1, p2 r1, r2 q1, q2 (1) The lifetimes ofiandncoincides with the invocation offoo. start P() ret R() Q() Q() q1, q2 (2) ret ret ret end The lifetime of the integer object created by new starts whennew is executed and ends when there are no more references to it. The integer object thus survives the invocation offoo. G53CMP: Lecture 14 p.5/37 G53CMP: Lecture 14 p.6/37 Storage Allocation (1) Global variables exist throughout the program s run-time. Where to store such variables can thus be decided statically, at compile (or link) time, once and for all. Example: private static String [] tokentable =... Storage Allocation (2) Arguments and local variables exist only during a function (or procedure or method) invocation: - Function calls are properly nested. - In case of recursion, a function may be re-entered any number of times. - Each function activation needs a private set of arguments and local variables. These observations suggest that storage for arguments and local variables should be allocated on a stack. G53CMP: Lecture 14 p.7/37 G53CMP: Lecture 14 p.8/37

Storage Allocation (3) When the lifetime does not coincide with procedure/function invocations, heap allocation is needed. E.g. for: - objects in object-oriented languages - function closures in languages supporting functions as first class entities - storage allocated by procedures like malloc in C. Such storage either explicitly deallocated when no longer needed, or automatically reclaimed by a garbage collector. Stack Frames One stack frame or activation record for each currently active function/procedure/method. Contents: Arguments Bookkeeping information; e.g. - Return address - Dynamic link - Static link Local variables Temporary workspace G53CMP: Lecture 14 p.9/37 G53CMP: Lecture 14 p.10/37 Defining the Stack The stack is usually defined by a handful of registers, dictated by the CPU architecture and/or convention. For example: SB: Stack Base ST : Stack Top LB: Local Base The names vary. Stack Pointer (SP) and Frame Pointer (FP) are often used instead of ST and LB, respectively. G53CMP: Lecture 14 p.11/37 Typical Stack Frame Layout address contents LB - argoffset arguments LB static link LB + 1 LB + 2 LB + 3 local variables LB + tempoffset temporary storage where argoffset = size(arguments) tempoffset = 3 + size(local variables) TAM uses this convention. (Word (e.g. 4 bytes) addressing assumed, offsets in words.) G53CMP: Lecture 14 p.12/37

Example: A functionf (Not quite current MiniTriangle, but language could easily be extended in this way.) var n: Integer;... fun f(x,y: Integer): Integer = let z: Integer in begin z := x * x + y * y; return n * z end Example: Callingf Call sequence forf(3,7) * 8: 2015 LOADL 3 ; 1st arg. (x) 2016 LOADL 7 ; 2nd arg. (y) 2017 CALL f 2018 LOADL 8 2019 MUL Address of each instruction explicitly indicated to the left. Address offhere given symbolically by a label. Corresponds to the address where the code forfstarts, say 2082. G53CMP: Lecture 14 p.13/37 G53CMP: Lecture 14 p.14/37 Example: Stack layout on entry tof On entry tof; caller sst =f slb: address contents SB + 42 n: n LB - 2 x: 3 LB - 1 y: 7 LB static link LB + 1 LB + 2 = 2018 Ret. addr. = old program counter (PC) = addr. of instruction immediately after the call instruction. New PC = address of first instruction off=2082. G53CMP: Lecture 14 p.15/37 Example: TAM Code forf TAM-code for the functionf(at address 2082): LOADL 0 LOAD [LB - 2]; x LOAD [LB - 2]; x MUL LOAD [LB - 1]; y LOAD [LB - 1]; y MUL ADD STORE LOAD LOAD MUL POP 1 1 RETURN 1 2 [LB + 3] ; z [SB + 42]; n [LB + 3] ; z RETURN replaces activation record (frame) offby result, restores LB, and jumps to ret. addr. (2018). Note: all variable offsets are static. G53CMP: Lecture 14 p.16/37

Dynamic and Static Links Dynamic Link: Value to whichlb (Local Base) is restored byreturn when exiting procedure; i.e. addr. of caller s frame = oldlb: - Dynamic because related to dynamic call graph. Static Link: Base of underlying frame of function that immediately lexically encloses this one. - Static because related to program s static structure. - Used to determine addresses of variables of lexically enclosing functions. Example: Stack Allocation (1) let var a: Integer[3]; var b: Boolean; var c: Character; proc Y () let var d: Integer; var e: record c: Character, n: Integer end in...; proc Z () let var f: Integer in begin...; Y();... end in begin...; Y();...; Z();... end G53CMP: Lecture 14 p.17/37 G53CMP: Lecture 14 p.18/37 Example: Stack Allocation (2) Initially LB = SB; i.e., the global variables constitute the frame of the main program. Call sequence: main Y (i.e. after main callingy): Global variables Frame ofy SB a[0] a[1] a[2] b c LB static link d e.c e.n Example: Stack Allocation (3) Call sequence: main Z Y: Global variables Frame ofz Frame ofy SB a[0] a[1] a[2] b c static link f LB static link d e.c e.n G53CMP: Lecture 14 p.19/37 G53CMP: Lecture 14 p.20/37

Exercise: Stack Allocation Global variables Frame ofz Frame ofy SB a[0] a[1] a[2] b c static link f LB static link d e.c e.n InY, what is the address of: b? e.c? f? Non-Local Variable Access (1) Consider nested procedures: proc P() var x, y, z: Integer proc Q()... begin... if... Q()... end proc R()... begin... Q()... end begin... Q()... R()... end P s variables are in scope also inqandr. But how to access them fromqorr? Neither global, nor local! Belong to the lexically enclosing procedure. G53CMP: Lecture 14 p.21/37 G53CMP: Lecture 14 p.22/37 Non-Local Variable Access (2) In particular: We cannot accessx,y,zrelative to the stack base (SB) since we cannot (in general) statically know ifpwas called directly from the main program or indirectly via one or more other procedures. I.e., there could be arbitrarily many stack frames belowp s frame. Non-Local Variable Access (3) We cannot accessx,y,zrelative to the local base (LB) since we cannot (in general) statically know if e.g.qwas called directly fromp, or indirectly viarand/or recursively via itself. I.e., there could be arbitrarily many stack frames betweenq s andp s frames. G53CMP: Lecture 14 p.23/37 G53CMP: Lecture 14 p.24/37

Non-Local Variable Access (4) Answer: The Static Links inq s andr s frames are set to point top s frame on each activation. The static link inp s frame is set to point to the frame of its closest lexically enclosing procedure, and so on. Thus, by following the chain of static links, one can access variables at any level of a nested scope. Non-Local Variable Access (5) Call sequence: main P Q: Global variables SB other frames Frame ofp static link x y z Frame ofq LB static link G53CMP: Lecture 14 p.25/37 G53CMP: Lecture 14 p.26/37 Non-Local Variable Access (6) Call sequence: main P R Q Q: Global variables SB other frames Frame ofp static link x y z Frame ofr static link Frame ofq(1) static link Frame of Q (2) LB static link G53CMP: Lecture 14 p.27/37 Non-Local Variable Access (7) Consider further levels of nesting: proc P() var x, y, z: Integer proc Q() proc R()... begin...if... R()... end... begin... R()... end begin... Q()... end Note:Q s variables now in scope inr. To access, compute the difference between scope levels of the accessing procedure/function and the accessed variable (note: static information), and follow that many static links. G53CMP: Lecture 14 p.28/37

Non-Local Variable Access (8) Call sequence: main P Q R R: Global variables SB other frames Frame ofp static link x y z Frame ofq static link Frame ofr(1) static link Frame of R (2) LB static link G53CMP: Lecture 14 p.29/37 Example: Call with Static Link TAM code,pcallingq:q s static link =P s local base, pushed onto stack prior to call: LOADA [LB + 0] ; Q s static link LOADCA #1_Q ; Address of Q CALLI TAM code, R calling iteself recursively: copy of R s static link (as callee s and caller s scope levels are the same) pushed onto stack prior to call: LOAD [LB + 0] ; R s static link LOADCA #2_R ; Address of R CALLI G53CMP: Lecture 14 p.30/37 Example: Non-local Access AccessingyinPfrom withinr; scope level difference is 2: LOAD [LB + 0] ; R s static link LOADI 0 ; Q s static link LOADI 4 ; y at offset 4 in P s frame G53CMP: Lecture 14 p.31/37 Code Generation (1) evaluate majl env (ExpVar {evvar = itms}) = case lookupisv itms env of ISVDisp d -> address majl vl d ISVLbl l -> do staticlink majl vl emit (LOADCA l) where vl = majscopelvl (itmslvl itms) Note: A label represents a procedure or function; what is pushed onto stack is effectively the corresponding closure (see later slide). G53CMP: Lecture 14 p.32/37

Code Generation (2) address :: Int -> Int -> MTInt -> TAMCG () address cl vl d vl == topmajscopelvl = emit (LOADA (SB d)) cl == vl = emit (LOADA (LB d)) cl > vl = do emit (LOAD (LB sld)) emitn (cl - vl - 1) (LOADI sld) emit (LOADL d) emit ADD Variable Scope Level Current Scope Level otherwise = error "Bug: Not in scope!" Code Generation (3) staticlink :: Int -> Int -> TAMCG () staticlink crl cel cel == topmajscopelvl = emit (LOADL 0) crl == cel = emit (LOADA (LB 0)) crl > cel = do emit (LOAD (LB sld)) emitn (crl - cel - 1) (LOADI sld) otherwise = error "Bug: Not in scope!" Callee Scope Level Caller Scope Level G53CMP: Lecture 14 p.33/37 G53CMP: Lecture 14 p.34/37 Closures (1) A closure: Code for function or procedure; and Bindings for all its free variables. Under the present scheme: Code: Address of function or procedure; Bindings: Chain of stack-allocated activation records linked by the static links. Works only when closure does not survive the activation of the function/procedure where it was created. Cannot support first-class functions/procedures! G53CMP: Lecture 14 p.35/37 Closures (2) Functions/procedures are first class if they can be handled just like any other values; e.g. - bound to variables - passed as arguments - returned as results. Supporting first-class functions/procedures requires closures to be heap-allocated: - Code still just address of function or procedure. - Static link replaced by (pointer(s) to) heap-allocated activation record(s). G53CMP: Lecture 14 p.36/37

Closures (3) As an optimisation, one could imagine combined schemes: stack allocation and static links might be used when known that a closure will never survive activation of enclosing function/procedure. G53CMP: Lecture 14 p.37/37