Runtime Environments Compiler Construction Chapter 7.

Slides:



Advertisements
Similar presentations
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Advertisements

The University of Adelaide, School of Computer Science
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
The University of Adelaide, School of Computer Science
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
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)
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
CS 536 Spring Run-time organization Lecture 19.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
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.
Run time vs. Compile time
Semantics of Calls and Returns
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
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
Run-time Environment and Program Organization
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.
Stacks and HeapsCS-502 Fall A Short Digression Stacks and Heaps CS-502, Operating Systems Fall 2007 (Slides include materials from Operating System.
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
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.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
COMPILER CONSTRUCTION Principles and Practice Kenneth C. Louden.
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.
Chapter 8 - Control II: Procedures and Environments
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
CPSC 388 – Compiler Design and Construction Runtime Environments.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Lesson 13 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Basic Semantics Associating meaning with language entities.
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,
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
CSC 8505 Compiler Construction Runtime Environments.
Intermediate Representation II Storage Allocation and Management EECS 483 – Lecture 18 University of Michigan Wednesday, November 8, 2006.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
ISBN Chapter 10 Implementing Subprograms.
LECTURE 13 Names, Scopes, and Bindings: Memory Management Schemes.
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
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
Lecture 7 Macro Review Stack Frames
Run-Time Environments Chapter 7
Implementing Subprograms Chapter 10
ENERGY 211 / CME 211 Lecture 25 November 17, 2008.
Run-time organization
Introduction to Compilers Tim Teitelbaum
Procedures (Functions)
Chapter 9 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Implementing Subprograms
The University of Adelaide, School of Computer Science
Run Time Environments 薛智文
Runtime Environments What is in the memory?.
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Run-time environments
Presentation transcript:

Runtime Environments Compiler Construction Chapter 7

In This Lecture Runtime Environments Runtime Environments Language Features for different Environments Language Features for different Environments Scoping and Allocation Scoping and Allocation Procedure Calls Procedure Calls Parameter Passing Parameter Passing

Runtime Environments Three types Three types Fully Static Fully Static Fortran77 Fortran77 Stack-based Stack-based C, C++, Pascal, Ada C, C++, Pascal, Ada Fully Dynamic Fully Dynamic LISP LISP

Memory Organization Code for Proc 1 Code for Proc 2 … Code for Proc n Entry Point for Proc 1 Code memory Entry Point for Proc 2 Entry Point for Proc n

Static Code and Data The code area of a program is fixed prior to execution The code area of a program is fixed prior to execution Thus the address for code can be computed at compile time Thus the address for code can be computed at compile time Actually, the location of the code is usually relative to some base register, or some other memory addressing scheme Actually, the location of the code is usually relative to some base register, or some other memory addressing scheme The address for data (variables etc.) is in general not assigned fixed locations at compile time The address for data (variables etc.) is in general not assigned fixed locations at compile time Static data however, does have a fixed memory address and is known at compile time Static data however, does have a fixed memory address and is known at compile time Constants can also be assigned a fixed address Constants can also be assigned a fixed address This is usually reserved for strings and other large constants This is usually reserved for strings and other large constants Other constants like ‘0’ or ‘1’ are inserted directly into the code Other constants like ‘0’ or ‘1’ are inserted directly into the code

Dynamic Data Allocation The memory for dynamic data is typically organized into two major areas The memory for dynamic data is typically organized into two major areas The stack The stack Used for local variables, parameter variables, return addresses and return values Used for local variables, parameter variables, return addresses and return values The heap The heap Used for dynamically allocated variables Used for dynamically allocated variables

Runtime Storage Organization Code Area Global Static Area Stack Free space Heap The heap generally grows towards lower memory The stack generally grows towards higher memory

Procedure Activation Records arguments (parameters) return address etc. local variables In C – like languages these records are stored on the stack In Fortran they are in the static storage area In Lisp they are in the heap area called a stack-frame if stored on the stack Depending on the Number of Registers much of this data might be stored in registers

Calling Sequence Operations that must be performed when a procedure is called the calling sequence Operations that must be performed when a procedure is called the calling sequence Includes Includes allocation of memory for activation record allocation of memory for activation record computation and storing of arguments computation and storing of arguments storing and setting necessary registers storing and setting necessary registers Additional operations are needed when a procedure returns called the return sequence Additional operations are needed when a procedure returns called the return sequence placing the return value placing the return value readjustment of registers readjustment of registers de-allocation of memory for activation record de-allocation of memory for activation record

Important Registers Program Counter (pc) Program Counter (pc) set this for procedure calls set this for procedure calls Stack Pointer (sp) Stack Pointer (sp) for activation frame info in stack-based environments for activation frame info in stack-based environments Base Pointer (bp) Base Pointer (bp) similar to the stack pointer, but points to the beginning of the activation record similar to the stack pointer, but points to the beginning of the activation record

Fully Static Runtime Environments All variables are allocated statically All variables are allocated statically including local variables including local variables No pointers or dynamic allocation No pointers or dynamic allocation No recursion No recursion why? why? All procedures have a single activation record All procedures have a single activation record allocated statically before program execution allocated statically before program execution

Static Runtime Environment code for main code for proc 1 … code for proc n activation record for main activation record for proc 1 global data area … activation record for proc 2 Code Area Data Area

Stack-based Runtime Environments Supports Recursion Supports Recursion No way of knowing at compile time, how many times a procedure will need to be called No way of knowing at compile time, how many times a procedure will need to be called Use stack-based activation records in order to support recursion Use stack-based activation records in order to support recursion

GCD int x,y; int gcd ( int u, int v) { if (v == 0) return u; else return gcd (v, u%v); } void main() { scanf(“%d%d”,&x,&y); printf(“%d\n”,gcd(x,y)); }

Activation Records x: 15 y: 10 global static area

Activation Records x: 15 y: 10 global static area activation record of main

Activation Records x: 15 y: 10 global static area activation record of main u: 15 v: 10 main bp return value return address first call to gcd

Activation Records x: 15 y: 10 global static area activation record of main u: 10 v: 5 old bp return value return address first call to gcd second call to gcd u: 15 v: 10 main bp return value return address

Activation Records x: 15 y: 10 global static area activation record of main u: 10 v: 5 old bp return value return address first call to gcd second call to gcd u: 15 v: 10 main bp return value return address u: 5 v: 0 old bp return address third call to gcd

Activation Records x: 15 y: 10 global static area activation record of main u: 10 v: 5 old bp rv: 5 return address first call to gcd second call to gcd u: 15 v: 10 main bp return value return address

Activation Records x: 15 y: 10 global static area activation record of main u: 15 v: 10 main bp rv : 5 return address first call to gcd

Activation Records x: 15 y: 10 global static area activation record of main u: 15 v: 10 main bp rv : 5 return address first call to gcd this value sent to printf

Sample code int x = 2; void f(int n) { static int x= 1; g(n); x--; } void g(int m) { int y=m-1; if (y>0) { f(y); x--; g(y); } } int main () { g(x) return 0; }

Activation Records x: 2 f::x 1 global static area return value activation record of main sp bp

Activation Records x: 2 f::x 1 global static area return value activation record of main m: 2 main bp return address y: 1 call to g( ) sp bp

Activation Records x: 2 f::x 1 global static area return value activation record of main m: 2 main bp return address y: 1 call to g( ) n: 10 g: bp return address call to f( ) sp bp

Activation Records x: 2 f::x 1 global static area return value activation record of main m: 2 main bp return address y: 1 call to g( ) n: 10 g: bp return address call to f( ) m: 1 main bp return address y: 0 call to g( ) sp bp

Arrays on the stack void f ( int x, char c) { int a[10]; double y; … } Consider function f:

Activation Record x: 2 c: 1 old bp: return address a[9] … a[2] a[1] a[0] y sp bp low memory high memory offset of a = -13*2 = -26 a[i] = bp + a + 2*i calculation of a[i]

Calling Sequence Create space for return value Create space for return value push 0 push 0 Store a return address Store a return address push [pc + 4] push [pc + 4] Store the current base pointer Store the current base pointer push bp push bp Set bp to new address Set bp to new address mov bp, sp mov bp, sp Create space for local variables Create space for local variables Compute the arguments and store them in the new activation record Compute the arguments and store them in the new activation record (push arguments in order) (push arguments in order) Jump to the new address Jump to the new address

Return Sequence change the sp to pop the arguments change the sp to pop the arguments load the control link to the bp load the control link to the bp pop bp pop bp jump to the return address jump to the return address pop pc pop pc copy retvalue into right location copy retvalue into right location