1 Lecture 9 Runtime Environment. 2 Outline Basic computer execution model Procedure abstraction run-time storage management Procedure linkage We need.

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Advertisements

The University of Adelaide, School of Computer Science
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
Procedure Calls Prof. Sirer CS 316 Cornell University.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
Informationsteknologi Saturday, September 29, 2007 Computer Architecture I - Class 41 Today’s class More assembly language programming.
CPSC Compiler Tutorial 8 Code Generator (unoptimized)
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
CS 536 Spring Run-time organization Lecture 19.
The Procedure Abstraction Part III: Allocating Storage & Establishing Addressability Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
CS 536 Spring Code generation I Lecture 20.
The Procedure Abstraction Part I: Basics Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
Intro to Computer Architecture
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Run time vs. Compile time
Run-time Environment and Program Organization
7/13/20151 Topic 3: Run-Time Environment Memory Model Activation Record Call Convention Storage Allocation Runtime Stack and Heap Garbage Collection.
Chapter 8 :: Subroutines and Control Abstraction
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
The Procedure Abstraction Part I: Basics Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Runtime Environments Compiler Construction Chapter 7.
Compiler Run-time Organization Lecture 7. 2 What we have covered so far… We have covered the front-end phases –Lexical analysis –Parsing –Semantic analysis.
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
Compiler Construction
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
CPSC 388 – Compiler Design and Construction Runtime Environments.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
1 CIS 461 Compiler Design and Construction Winter 2012 Lecture-Module #16 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon.
Lecture 4: MIPS Instruction Set
CSC 8505 Compiler Construction Runtime Environments.
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Computer Science 210 Computer Organization
Computer structure: Procedure Calls
Lecture 5: Procedure Calls
Run-time organization
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
The Procedure Abstraction Part IV: Allocating Storage & Establishing Addressability Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights.
Chapter 9 :: Subroutines and Control Abstraction
Lecture 4: MIPS Instruction Set
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
10/4: Lecture Topics Overflow and underflow Logical operations
UNIT V Run Time Environments.
Program and memory layout
Operating Systems Run-time Organization
Lecture 6: Assembly Programs
Procedures and Calling Conventions
Program and memory layout
Computer Architecture
Program and memory layout
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
Presentation transcript:

1 Lecture 9 Runtime Environment

2 Outline Basic computer execution model Procedure abstraction run-time storage management Procedure linkage We need to understand how a program executes at run time before we can generate code for it

3 Basic Execution Model l MIPS as an example l CPU »ALU Unit »Registers »Control Unit l Memory »program »data Memory Registers Control ALU

4 Arithmetic and Logic Unit l Performs most of the data operations Has the form: OP R dest, R src1, R src2 l Operations are: »Arithmetic operations (add, sub, mulo [mult with overflow]) »Logical operations (and, sll, srl) »Comparison operations (seq, sge, slt [set to 1 if less than]) Memory Registers Control ALU

5 Arithmetic and Logic Unit l Many arithmetic operations can cause an exception »overflow and underflow l Can operate on different data types »8, 16, 32 bits »signed and unsigned arithmetic »Floating-point operations (separate ALU) »Instructions to convert between formats (cvt.s.d) Memory Registers Control ALU

6 Control l Handles the instruction sequencing l Executing instructions »All instructions are in memory »Fetch the instruction pointed by the PC and execute it »For general instructions, increment the PC to point to the next location in memory Memory RegistersALU Control

7 l Unconditional Branches »Fetch the next instruction from a different location »Unconditional jump to a given address j label »Unconditional jump to an address in a register jr r src »To handle procedure calls, do an unconditional jump, but save the next address in the current stream in a register jal label [jump and link] jalr r src Memory RegistersALU Control

8 l Conditional Branches »Perform a test, if successful fetch instructions from a new address, otherwise fetch the next instruction »Instructions are of the form: brelop R src1, R src2, label »‘ relop ’ is of the form: ‘’, ‘eq’, ‘ne’, ‘gt’, ‘ge’, ‘lt’, ‘le’ Memory RegistersALU Control

9 l Control transfer in special (rare) cases »traps and exceptions »Mechanism –Save the next(or current) instruction location –find the address to jump to (from an exception vector) –jump to that location Memory RegistersALU Control

10 Memory l Flat Address Space »composed of words »byte addressable l Need to store »Program »Local variables »Global variables and data »Stack »Heap Memory RegistersALU Control

11 Memory RegistersALU Control Stack Generated Code Heap Objects Arrays locals (parameters)

12 Registers l Load/store architecture »All operations are on register values »Need to bring data in-to/out-of registers »la R dest, address [load address] »lw R dest, address [load word] »li, R dest, imm [load imm] »sw R src, address [store word ] »mv R dest, R src [ move ] »address has the from value(R) l Important for performance »limited in number ALU Control Memory Registers

13 Registers (of MIPS processors)

14 Other interactions l Other operations »Input/Output »Privilege / secure operations »Handling special hardware –TLBs, Caches etc. l Mostly via system calls »hand-coded in assembly »compiler can treat them as a normal function call ALU Control Memory Registers

15 The MIPS ISA and MIPS Processor l One of the earliest RISC processors »Has evolved from 1980 ’ s »ISA has also evolved –Always backward compatible, I.e. add more to the ISA –MIPS-I, MIPS-II ….MIPS-V »Many processor incarnation –From a simple 5-stage pipeline to an out-of-order superscalar –R2000, R4000, R8000, R10000 …..

16 Procedure Abstraction l Decomposition of programs into callable procedures. l Issues: »calling /returning mechanism »parameter passing »local variables (scope) »private execution context » private storage for each procedure invocation » Encapsulate information about control flow & data abstractions The procedure abstraction is a social contract.

17 Benefits of procedure abstraction 1. control abstraction »well defined entry, exits »mechanism to pass parameters, return values 2. name space » new name space within procedure » local names are protected from outside 3. external interface »accessed by procedure name, parameters »protection for both caller and callee » enables software libraries, systems 4. separate compilation »compile procedures independently »keeps compile times reasonable »allows us to build large programs

18 Procedure Abstraction l procedure abstraction leads... »multiple procedures »library calls »compiled by many compilers, written in different languages, hand-written assembly l For a compiler, we need to worry about »Memory layout »Registers »Stack

19 Registers (of MIPS processors)

20 Parameter passing disciplines l Many different methods »call by reference »call by value »call by value-result l How do you pass the parameters? »via. the stack »via. the registers »or a combination

21 Homes for Variables? l A Simplistic model »Allocate a data area for each distinct scope »One data area per “sheaf” in scoped table l What about recursion? »Need a data area per invocation (or activation) of a scope »We call this the scope’s activation record »The compiler can also store control information there ! l More complex scheme »One activation record ( AR ) per procedure instance »All the procedure’s scopes share a single AR »Use a stack to keep the activation records

22 Memory Layout l Start of the stack l Heap management »free lists l starting location in the text segment Stack Text segment Heap Objects Arrays locals (parameters) 0x7fffffff 0x Reserved Data segment

23 Run-time Resources l Execution of a program is initially under the control of the operating system l When a program is invoked: »The OS allocates space for the program »The code is loaded into part of the space »The OS jumps to the entry point (i.e., “ main ” )

24 Memory Layout Low Address High Address Memory Code Other Space

25 Notes l Our pictures of machine organization have: »Low address at the bottom »High address at the top »Lines delimiting areas for different kinds of data l These pictures are simplifications »E.g., not all memory need be contiguous l In some textbooks Higher addresses are at bottom

26 What is Other Space? l Holds all data for the program l Other Space = Data Space l Compiler is responsible for: »Generating code »Orchestrating use of the data area

27 Code Generation Goals l Two goals: »Correctness »Speed l Most complications in code generation come from trying to be fast as well as correct

28 Assumptions about Execution 1. Execution is sequential; control moves from one point in a program to another in a well-defined order 2. When a procedure is called, control eventually returns to the point immediately after the call Do these assumptions always hold?

29 Activations l An invocation of procedure P is an activation of P l The lifetime of an activation of P is »All the steps to execute P »Including all the steps in procedures that P calls

30 Lifetimes of Variables l The lifetime of a variable x is the portion of execution in which x is defined l Note that »Lifetime is a dynamic (run-time) concept »Scope is a static concept

31 Activation Trees l Assumption (2) requires that when P calls Q, then Q returns before P does l Lifetimes of procedure activations are properly nested l Activation lifetimes can be depicted as a tree

32 Example Class Main { void g() { return ; } void f() { g() ; } void m() { g(); f(); }; } m f g g m enter g enter g return f enter g enter g return f return m return activation tree for m() Lifetime of invocations

33 Example 2 Class Main { int g() { return 1; }; int f(int x){ if( x == 0) return g(); else return f(x - 1); } int m(){ return f(3) } } What is the activation tree for this example?

34 Example 2 m() enter f(3) enter f(2) enter f(1) enter f(0) enter g() enter g() return f(0) return f(1) return f(2) return f(3) return m() return m() f(3) f(2) f(1) g()

35 Notes l The activation tree depends on run-time behavior l The activation tree may be different for every program input l Since activations are properly nested, a stack can track currently active procedures

36 Example Class Main { g() { return; }; f() { g() }; m() { g(); f(); }; } mStack m

37 Example Class Main { g() { return; } f(): Int { g() } m() { g(); f(); } } m g Stack m g

38 Example Class Main { g() { return } f() { g() } m() { g(); f(); } } m g f Stack m f

39 Example Class Main { g() { return; } f() { g(); } m(){ g(); f(); } } m f g g Stack m f g

40 Revised Memory Layout Low Address High Address Memory Code Stack

41 Activation Records l On many machine the stack starts at high- addresses and grows towards lower addresses l The information needed to manage one procedure activation is called an activation record (AR) or frame If procedure F calls G, then G ’ s activation record contains a mix of info about F and G.

42 What is in G ’ s AR when F calls G? F is “ suspended ” until G completes, at which point F resumes. G ’ s AR contains information needed to resume execution of F. G ’ s AR may also contain: »Actual parameters to G (supplied by F) »G ’ s return value (needed by F) »Space for G ’ s local variables

43 The Contents of a Typical AR for G Space for G ’ s return value l Actual parameters l Pointer to the previous activation record »The control link; points to AR of caller of G l Machine status prior to calling G »Contents of registers & program counter »Local variables l Other temporary values

44 Example 2, Revisited Class Main { int g() { return 1; }; int f(int x) {if (x==0) return g(); else return f(x - 1); (**) }; void main() { f(3); (*)} AR for f: result argument control link return address

45 Stack After Two Calls to f main result 3 (**) f(3) result 2 (*)(*) f(2) Stack fp for f(1) fp for f(2)

46 Notes l main has no argument or local variables and its result is never used; its AR is uninteresting l (*) and (**) are return addresses of the invocations of f »The return address is where execution resumes after a procedure call finishes l This is only one of many possible AR designs »Would also work for C, Pascal, FORTRAN, etc.

47 The Main Point The compiler must determine, at compile-time, the layout of activation records and generate code that correctly accesses locations in the activation record Thus, the AR layout and the code generator must be designed together!

48 Discussion l The advantage of placing the return value 1st in a frame is that the caller can find it at a fixed offset from its own frame l There is nothing magic about this organization »Can rearrange order of frame elements »Can divide caller/callee responsibilities differently »An organization is better if it improves execution speed or simplifies code generation

49 Discussion (Cont.) l Real compilers hold as much of the frame as possible in registers »Especially the method result and arguments

50 Globals l All references to a global variable point to the same object »Can ’ t store a global in an activation record l Globals are assigned a fixed address once »Variables with fixed address are “ statically allocated ” l Depending on the language, there may be other statically allocated values

51 Memory Layout with Static Data Low Address High Address Memory Code Stack Static Data

52 Heap Storage l A value that outlives the procedure that creates it cannot be kept in the AR void foo(Foo f) { f.bar = new Bar(); } The Bar object must survive deallocation of foo ’ s AR l Languages with dynamically allocated data use a heap to store dynamic data

53 Notes l The code area contains object code »For most languages, fixed size and read only l The static area contains data (not code) with fixed addresses (e.g., global data) »Fixed size, may be readable or writable l The stack contains an AR for each currently active procedure »Each AR usually fixed size, contains locals l Heap contains all other data »In C, heap is managed by malloc and free

54 Notes (Cont.) l Both the heap and the stack grow Must take care that they don ’ t grow into each other l Solution: start heap and stack at opposite ends of memory and let the grow towards each other

55 Memory Layout with Heap Low Address High Address Memory Code Heap Static Data Stack

56 Data Layout l Low-level details of machine architecture are important in laying out data for correct code and maximum performance l Chief among these concerns is alignment

57 Alignment l Most modern machines are (still) 32 bit »8 bits in a byte »4 bytes in a word »Machines are either byte or word addressable l Data is word aligned if it begins at a word boundary l Most machines have some alignment restrictions »Or performance penalties for poor alignment

58 Alignment (Cont.) l Example: A string “ Hello ” Takes 5 characters (without a terminating \0) To word align next datum, add 3 “ padding ” characters to the string The padding is not part of the string, it ’ s just unused memory

59 Procedure Linkage and Activation Records

60 Procedure linkages l The linkage convention is the interface used for performing procedure calls »on entry, establish p's environment »at a call, preserve p's environment »after a call, restore p’s environment »on exit, tear down p's environment »in between, handle addressability and lifetimes l Ensures each procedure inherits from caller a valid run-time environment and also restores one for its caller

61 Procedure Linkages Standard procedure linkage procedure p prolog epilog pre-call post-return procedure q prolog epilog Procedure has standard prolog standard epilog Each call involves a pre-call sequence post-return sequence

62 return address old frame pointer Stack Local variables Calliee saved registers Stack temporaries... argument 5 argument 4 Dynamic area Caller saved registers arguments fp sp l When calling a new procedure, caller: »push any t0-t9 that has a live value on the stack »put arguments 1-4 on a0- a3 »push rest of the arguments on the stack »do a jal or jalr Pre-Call

63 return address old frame pointer return address old frame pointer Stack Local variables Callee saved registers Stack temporaries... argument 5 argument 4 Dynamic area Local variables Callee saved registers Caller saved registers arguments Dynamic area fp sp l In a procedure call, the callee at the beginning: »push $fp on the stack »copy $sp+4 to $fp »push $ra on the stack »if any s0-s7 is used in the procedure save it on the stack »create space for local variables on the stack »execute the callee... Prolog stack frame

64 return address old frame pointer Stack Local variables Callee saved registers Stack temporaries... argument 5 argument 4 Dynamic area Caller saved registers arguments l In a procedure call, the callee at the end: »put return values on v0,v1 »update $sp using $fp ($fp-8) -... »Pop the callee saved registers from stack »restore $ra from stack »restore $fp from stack »execute jr ra and return to caller fp sp Epilog

65 Stack l On return from a procedure call, the caller: »Update $sp to ignore arguments »pop the caller saved registers »Continue... return address old frame pointer Local variables Calliee saved registers Stack temporaries... argument 5 argument 4 Dynamic area fp sp Post-call

66 Argument 5: bx (0) Example Program class auxmath { int sum3d(int ax, int ay, int az, int bx, int by, int bz) { int dx, dy, dz; if(ax > ay) dx = ax - bx; else dx = bx - ax; … retrun dx + dy + dz; } … int px, py, pz; px = 10; py = 20; pz = 30; auxmath am; am.sum3d(px, py, pz, 0, 1, -1); return address old frame pointer Dynamic area Caller saved registers Argument 7: bz (-1) fp sp Argument 6: by (1) Local variable dx (??) Local variable dy (??) Local variable dz (??)

67 Caller/Callee Responsibility Caller Callee pre-call sequence prolog code allocate basic frame save registers, state evaluate & store params. extend basic frame Call store return address (for local data) store FP find static data area set FP for child initialize locals jump to child fall through to code post-return sequence epilog code copy return value store return value Return deallocate basic frame restore state restore params.(?) unextend basic frame (call-by-value-save) restore parent's FP jump to return address