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.

Slides:



Advertisements
Similar presentations
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Advertisements

CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
1 Languages and Compilers (SProg og Oversættere) Sequence control and Subprogram Control.
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:
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
Runtime Environments Source language issues Storage organization
Honors Compilers Addressing of Local Variables Mar 19 th, 2002.
Run-Time Storage Organization
The Procedure Abstraction Part I: Basics Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Run time vs. Compile time
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Chapter 9: Subprogram Control
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 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
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.
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.
Compiler Construction
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
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.
Activation Records CS 671 February 7, CS 671 – Spring The Compiler So Far Lexical analysis Detects inputs with illegal tokens Syntactic analysis.
1 Scope Rules (Section 3.3) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott.
Runtime Organization.
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
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.
ISBN Chapter 10 Implementing Subprograms.
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.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Storage Allocation Mechanisms
Implementing Subprograms Chapter 10
Implementing Subprograms
Names and Attributes Names are a key programming language feature
Implementing Subprograms
Chapter 9 :: Subroutines and Control Abstraction
The Procedure Abstraction Part I: Basics
Implementing Subprograms
Binding Times Binding is an association between two things Examples:
UNIT V Run Time Environments.
Languages and Compilers (SProg og Oversættere)
Programming Languages
Run Time Environments 薛智文
Runtime Environments What is in the memory?.
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Chapter 10 Def: The subprogram call and return operations of
Presentation transcript:

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 Storage organization

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

3 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.

4 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

5 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

6 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.

7 Storage allocation Static 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.

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

9 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 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

10 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

11 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)

12 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?

13 Scope rules Dynamic scoping example procedure main x, a: int; procedure one() begin x := 1; end procedure two() begin x:int; one(); end begin x := 2; read(a); if a>0 then one() else two(); print(x); end. 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. 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.

14 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.