Run time vs. Compile time

Slides:



Advertisements
Similar presentations
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Advertisements

Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
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:
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
1 Handling nested procedures Method 1 : static (access) links –Reference to the frame of the lexically enclosing procedure –Static chains of such links.
The Procedure Abstraction Part III: Allocating Storage & Establishing Addressability Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all.
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
Runtime Environments Source language issues Storage organization
Run-Time Storage Organization
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Run-time Environment and Program Organization
ISBN Chapter 10 Implementing Subprograms –Semantics of Calls and Returns –Implementing “Simple” Subprograms –Implementing Subprograms with.
1 CSCI 360 Survey Of Programming Languages 9 – Implementing Subprograms Spring, 2008 Doug L Hoffman, PhD.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
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.
ISBN Chapter 10 Implementing Subprograms.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
Runtime Environments Compiler Construction Chapter 7.
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
Compiler Construction
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 9 Def: The subprogram call and return operations of a language are together called its subprogram.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Activation Records CS 671 February 7, CS 671 – Spring The Compiler So Far Lexical analysis Detects inputs with illegal tokens Syntactic analysis.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Runtime Environments. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Activation Records (in Tiger) CS 471 October 24, 2007.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
1 CIS 461 Compiler Design and Construction Winter 2012 Lecture-Module #16 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
COMP3190: Principle of Programming Languages
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
國立台灣大學 資訊工程學系 薛智文 98 Spring Run Time Environments (textbook ch# 7.1–7.3 )
CSC 8505 Compiler Construction Runtime Environments.
1 Chapter 10 © 2002 by Addison Wesley Longman, Inc The General Semantics of Calls and Returns - Def: The subprogram call and return operations of.
7. Runtime Environments Zhang Zhizheng
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Runtime Environments Chapter 7. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Storage Allocation Mechanisms
Chapter 10 : Implementing Subprograms
Implementing Subprograms Chapter 10
Implementing Subprograms
COMPILERS Activation Records
Implementing Subprograms
Introduction to Compilers Tim Teitelbaum
Chapter 10: Implementing Subprograms Sangho Ha
Chapter 9 :: Subroutines and Control Abstraction
Implementing Subprograms
Languages and Compilers (SProg og Oversættere)
Run Time Environments 薛智文
Runtime Environments What is in the memory?.
Chapter 10 Def: The subprogram call and return operations of
Presentation transcript:

Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage Storage organization

Data representation Fundamental types are supported directly by machine operations Enumerated types? Booleans? Arrays? Strings? Records? packed unpacked

Control abstraction A procedure is a control abstraction it associates a name with a chunk of code that piece of code is regarded in terms of its purpose and not of its implementation. Big issue #1: Allow separate compilation Without it we can't build large systems Saves compile time Saves development time We must establish conventions on memory layout, calling sequences, procedure entries and exits, interfaces, etc.

Control abstraction Procedures must have a well defined call mechanism In Algol-like languages: a call creates an instance (activation) of the procedure on exit, control returns to the call site, to the point right after the call. Use a call graph to see set of potential calls

Control abstraction Generated code must be able to preserve current state save variables that cannot be saved in registers save specific register values establish procedure environment on entry map actual to formal parameters create storage for locals restore previous state on exit This can be modeled with a stack Allocate a memory block for each activation Maintain a stack of such blocks This mechanism can handle recursion

Name space A procedure creates its own name space It can declare local variables Local declarations may hide non-local ones Local names cannot be seen from outside. The scope of a variable is the area in which it is active Scope rules determine how declarations are mapped to names.

Storage allocation Static allocation Stack allocation Heap allocation object is allocated address during compile time location is retained during execution Stack allocation objects are allocated in LIFO order Heap allocation objects may be allocated and deallocated at any time.

Static allocation Objects that are allocated statically include: globals explicitly declared static variables instructions string literals compiler-generated tables used during run time.

Stack allocation Follows stack model for procedure activation A procedure's memory block associated with an activation of the procedure is called an activation record or stack frame Local variables are stored in the stack frame The stack frame is pushed on the stack when the procedure is called and popped (and destroyed) when the procedure terminates What can we determine at compile time? We cannot determine the address of the stack frame But we can determine the size of the stack frame and the offsets of various objects within a frame

Heap allocation Used for dynamically allocated/resized objects Managed by special algorithms General model maintain list of free blocks allocate block of appropriate size handle fragmentation handle garbage collection

Scope rules Static scoping determined at compile time Algol languages: resolve conflicts by using "closest nested scope" rule We must come up with a mechanism to access enclosing scopes (later)

Scope rules Dynamic scoping depends on flow of control at run time resolve conflicts using "most recently executed" rule type checking deferred until run time complicates program any advantages?

Scope rules Dynamic scoping example run 1 : a is positive one() is called. The x in one() is bound to the most recent declaration which is the global one. The global x is given the value of 1 and the program prints 1 in the end. procedure main x, a: int; procedure one() begin x := 1; end procedure two() x:int; one(); x := 2; read(a); if a>0 then one() else two(); print(x); end. run 2 : a is negative two() is called. It declares a local x. Then it calls one(). The x in one() is bound to the most recent declaration which is the local declaration in two(). That local x is given the value of 1. one() exits. two() exits and the local x dies. The global x is still 2. The program prints 2 in the end.

Scope rules Dynamic scoping -- why bother? It allows us to customize procedures. Consider a procedure print_integer that can print its argument in base 8, 10 or 16. Furthermore, we usually want to print it in base 10. We can use a global variable (such as x in the example) which is set to 10. Then, if we want a different base, we can declare a local x, set it to 8 or 16, and call print_integer. Once we exit the current scope, the local x dies and we are back to the default value. Is there another way to do this? Yes, we could have passed x as a parameter Yes, we could have made x a statically allocated variable (e.g. global), initialized it to 10 and, if necessary, reset its value right before and after a call to print_integer bad idea: we might forget to restore it after the call.

Storage organization Stack layout each procedure is given a stack frame at the top of the stack stack grows towards lower addresses at any time the stack pointer points to the current top of the stack (first available location) at any time, the frame pointer points to the beginning of the current frame the stack frame size and the offsets are determined at compile time! stack frame contents are accessed as offsets relative to the stack pointer or frame pointer Do we need both?

Storage organization Stack frames. What's in them? arguments return value local variables temporary values stack pointer frame pointer return address dynamic link? static link? etc.

The stack frame Dynamic link = Some languages allow nested procedures a reference to the frame of the caller needed to be able to restore the stack can we use it to access non-locals? Some languages allow nested procedures In order to implement the "closest nested scope" rule we need access to the frame of the lexically enclosing procedure Example: A() { B(); C(); } C can call B, but B is lexically enclosed in A

Handling nested procedures Method 1 : static (access) links Reference to the frame of the lexically enclosing procedure Static chains of such links are created. How do we use them to access non-locals? The compiler knows the scope s of a variable The compiler knows the current scope t Follow s-t links

Handling nested procedures Method 1 : static (access) links Setting the links: if the callee is nested directly within the caller, set its static link to point to the caller's frame pointer (or stack pointer) if the callee has the same nesting level as the caller, set its static link to point to wherever the caller's static link points to

Handling nested procedures Method 2 : Displays A Display encodes the static link info in an array. The ith element of the array points to the frame of the most recent procedure at scope level i How? When a new stack frame is created for a procedure at nesting level i, save the current value of D[i] in the new stack frame (to be restored on exit) set D[i] to the new stack frame

Stack maintenance Calling sequence : code executed by the caller before and after a call code executed by the callee at the beginning code executed by the callee at the end

Stack maintenance A typical calling sequence : Caller assembles arguments and transfers control evaluate arguments place arguments in stack frame and/or registers save caller-saved registers save return address jump to callee's first instruction

Stack maintenance A typical calling sequence : Callee saves info on entry allocate memory for stack frame, update stack pointer save callee-saved registers save old frame pointer update frame pointer Callee executes

Stack maintenance A typical calling sequence : Callee restores info on exit and returns control place return value in appropriate location restore callee-saved registers restore frame pointer pop the stack frame jump to return address Caller restores info restore caller-saved registers