670 likes | 746 Views
Lecture 9. Runtime Environment. 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. Memory. Registers. ALU. Control.
E N D
Lecture 9 Runtime Environment
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
Memory Registers ALU Control Basic Execution Model • MIPS as an example • CPU • ALU Unit • Registers • Control Unit • Memory • program • data
Arithmetic and Logic Unit Memory • Performs most of the data operations • Has the form:OP Rdest, Rsrc1, Rsrc2 • 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]) Registers ALU Control
Arithmetic and Logic Unit Memory • Many arithmetic operations can cause an exception • overflow and underflow • 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) Registers ALU Control
Control Memory • Handles the instruction sequencing • 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 Registers ALU Control
Control Memory • Unconditional Branches • Fetch the next instruction from a different location • Unconditional jump to a given addressj label • Unconditional jump to an address in a registerjr rsrc • To handle procedure calls, do an unconditional jump, but save the next address in the current stream in a registerjal label [jump and link]jalr rsrc Registers ALU Control
Control Memory • Conditional Branches • Perform a test, if successful fetch instructions from a new address, otherwise fetch the next instruction • Instructions are of the form: brelop Rsrc1, Rsrc2, label • ‘relop’ is of the form:‘’, ‘eq’, ‘ne’, ‘gt’, ‘ge’, ‘lt’, ‘le’ Registers ALU Control
Control Memory • 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 Registers ALU Control
Memory Memory • Flat Address Space • composed of words • byte addressable • Need to store • Program • Local variables • Global variables and data • Stack • Heap Registers ALU Control
Memory Memory Stack locals (parameters) Registers ALU Objects Control Heap Arrays Generated Code
Registers Memory • Load/store architecture • All operations are on register values • Need to bring data in-to/out-of registers • la Rdest, address [load address] • lw Rdest, address [load word] • li, Rdest, imm [load imm] • sw Rsrc, address [store word ] • mv Rdest, Rsrc [ move ] • address has the from value(R) • Important for performance • limited in number Registers ALU Control
Other interactions Memory • Other operations • Input/Output • Privilege / secure operations • Handling special hardware • TLBs, Caches etc. • Mostly via system calls • hand-coded in assembly • compiler can treat them as a normal function call Registers ALU Control
The MIPS ISA and MIPS Processor • 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 …..
Procedure Abstraction • Decomposition of programs into callable procedures. • 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.
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
Procedure Abstraction • procedure abstraction leads ... • multiple procedures • library calls • compiled by many compilers, written in different languages, hand-written assembly • For a compiler, we need to worry about • Memory layout • Registers • Stack
Parameter passing disciplines • Many different methods • call by reference • call by value • call by value-result • How do you pass the parameters? • via. the stack • via. the registers • or a combination
Homes for Variables? • A Simplistic model • Allocate a data area for each distinct scope • One data area per “sheaf” in scoped table • 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 ! • 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
Memory Layout 0x7fffffff Stack • Start of the stack • Heap management • free lists • starting location in the text segment locals (parameters) Objects Heap Arrays Data segment Text segment 0x400000 Reserved
Run-time Resources • Execution of a program is initially under the control of the operating system • 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”)
Code Memory Layout High Address Other Space Memory Low Address
Notes • Our pictures of machine organization have: • Low address at the bottom • High address at the top • Lines delimiting areas for different kinds of data • These pictures are simplifications • E.g., not all memory need be contiguous • In some textbooks Higher addresses are at bottom
What is Other Space? • Holds all data for the program • Other Space = Data Space • Compiler is responsible for: • Generating code • Orchestrating use of the data area
Code Generation Goals • Two goals: • Correctness • Speed • Most complications in code generation come from trying to be fast as well as correct
Assumptions about Execution • Execution is sequential; control moves from one point in a program to another in a well-defined order • When a procedure is called, control eventually returns to the point immediately after the call Do these assumptions always hold?
Activations • An invocation of procedure Pis an activationof P • The lifetime of an activation of P is • All the steps to execute P • Including all the steps in procedures that P calls
Lifetimes of Variables • The lifetimeof a variable x is the portion of execution in which x is defined • Note that • Lifetime is a dynamic (run-time) concept • Scope is a static concept
Activation Trees • Assumption (2) requires that when P calls Q, then Q returns before P does • Lifetimes of procedure activations are properly nested • Activation lifetimes can be depicted as a tree
m g f g Example Class Main { void g() { return ; } void f() { g() ; } void m() { g(); f(); }; } m enter g enter g return f enter g enter g return f return m return Lifetime of invocations activation tree for m()
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?
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()
Notes • The activation tree depends on run-time behavior • The activation tree may be different for every program input • Since activations are properly nested, a stack can track currently active procedures
Example Class Main { g() { return; }; f() { g() }; m() { g(); f(); }; } m Stack m
g Example Class Main { g() { return; } f(): Int { g() } m() { g(); f(); } } m Stack m g
g f Example Class Main { g() { return } f() { g() } m() { g(); f(); } } m Stack m f
g f g Example Class Main { g() { return; } f() { g(); } m(){ g(); f(); } } m Stack m f g
Code Revised Memory Layout High Address Stack Memory Low Address
Activation Records • On many machine the stack starts at high-addresses and grows towards lower addresses • The information needed to manage one procedure activation is called an activation record(AR) orframe • If procedure F calls G, then G’s activation record contains a mix of info about F and G.
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
The Contents of a Typical AR for G • Space for G’s return value • Actual parameters • Pointer to the previous activation record • The control link; points to AR of caller of G • Machine status prior to calling G • Contents of registers & program counter • Local variables • Other temporary values
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:
main Stack After Two Calls to f result Stack 3 f(3) (**) fp for f(2) result 2 f(2) (*) fp for f(1)
Notes • main has no argument or local variables and its result is never used; its AR is uninteresting • (*) and (**) are return addresses of the invocations of f • The return address is where execution resumes after a procedure call finishes • This is only one of many possible AR designs • Would also work for C, Pascal, FORTRAN, etc.
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 generatormust be designed together!
Discussion • 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 • 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
Discussion (Cont.) • Real compilers hold as much of the frame as possible in registers • Especially the method result and arguments
Globals • All references to a global variable point to the same object • Can’t store a global in an activation record • Globals are assigned a fixed address once • Variables with fixed address are “statically allocated” • Depending on the language, there may be other statically allocated values