Runtime Environments. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.

Slides:



Advertisements
Similar presentations
Procedures. 2 Procedure Definition A procedure is a mechanism for abstracting a group of related operations into a single operation that can be used repeatedly.
Advertisements

Garbage Collection  records not reachable  reclaim to allow reuse  performed by runtime system (support programs linked with the compiled code) (support.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
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.
Memory Management. History Run-time management of dynamic memory is a necessary activity for modern programming languages Lisp of the 1960’s was one of.
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)
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
ISBN Chapter 10 Implementing Subprograms.
Runtime Environments Source language issues 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
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
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.
Presented by Dr Ioanna Dionysiou
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.
Chapter 7 Run-Time Environments
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
Runtime Environments Compiler Construction Chapter 7.
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.
Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage.
Chapter 7 Runtime Environments. Relationships between names and data objects As execution proceeds, the same name can denote different data objects Procedures,
1 Run-Time Environments. 2 Procedure Activation and Lifetime A procedure is activated when called The lifetime of an activation of a procedure is the.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 9.
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.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Run-Time Environments How do we allocate the space for the generated target code and the data object.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
CSE 425: Control Abstraction I Functions vs. Procedures It is useful to differentiate functions vs. procedures –Procedures have side effects but usually.
國立台灣大學 資訊工程學系 薛智文 98 Spring Run Time Environments (textbook ch# 7.1–7.3 )
CSC 8505 Compiler Construction Runtime Environments.
7. Runtime Environments Zhang Zhizheng
1 Run-Time Environments Chapter 7 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate.
ISBN Chapter 10 Implementing Subprograms.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
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?
Run-Time Environments Chapter 7
Subject Name Compiler Design Subject Code: 10CS63
Run-Time Storage Organization
Run-Time Storage Organization
Run-Time Environments
Implementing Subprograms
Chapter 10: Implementing Subprograms Sangho Ha
Implementing Subprograms
Run-Time Environments
Topic 3-b Run-Time Environment
Topic 3-a Calling Convention 1/10/2019.
Binding Times Binding is an association between two things Examples:
UNIT V Run Time Environments.
Run Time Environments 薛智文
Implementing Subprograms
Presentation transcript:

Runtime Environments

Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment (functions that map to stg) –State (funct that maps a stg location to the value held there)

Storage Allocation Strategies  Static –Names are bound to storage at compile time  Dynamic – names are bound to storage at run time

Storage Organization CODE CODE STATIC DATA STATIC DATA STACK STACK \/ \/ /\ /\ HEAP HEAP

Stack  Allocate activation record  Locals get new storage  Enters information into its fields

Activation Record (Stack Frames)  Returned value  Actual parameters  Optional control link  Optional access link  Saved machine status  Local data  Temporary data

Calling Sequence  Caller evaluates arguments  Caller stores return address in callee’s activation record  Caller stores stack top  Callee saves register values and status information for caller

Return Sequence.  Callee restores state of machine  Callee places return value next to the activation record of the caller  Restores top of stack pointer  Caller copies return value

Variable Length Data  Arrays –Stored after the activation record –The activation record does not have to allocate space for the

Dangling Reference  #include  #include  int *dangle();  main(){  int *p;  p = dangle(); printf("p = %d\n",*p);  sub(); printf("p = %d\n",*p);  }  int *dangle(){  int i = 23;  return &i;  }  sub(){  int i,j,k;  }

Heap vs Stack Allocation  Stack allocation cannot be used if –Local names must be retained after activation ends –Activation outlives caller

Heap Management Strategies  free list  first fit  best fit  worst fit

Garbage Collection  records not reachable  reclaim to allow reuse  performed by runtime system (support programs linked with the compiled code) (support programs linked with the compiled code)

Record Types  live – will be used in the future  not live – will not be used in the future  reachable – able to be accessed via programs

Types of Algorithms  Mark-And-Sweep Collection  Reference Counts  Copying Collection  Generational Collection  Incremental Collection

Mark-And-Sweep Collection  Program variables and heap records form a directed graph  Roots are the variables  node n is reachable if r -> … -> n r -> … -> n  Depth first search marks reachable nodes  Any node not marked is garbage

Cost of Garbage Collection  Depth first search takes time proportional to the number of reachable nodes  Sweep phase takes time proportional to the size of the heap

Maintaining Free Space  Create a list of free space  Search for a space of size N might be long  Maintain several free lists of differing sizes  External fragmentation a problem  Internal fragmentation can also be a problem

Reference Counts  Count the number of pointers pointing to each record  Store the reference count with each record  If p addresses an alternate record, decrement the old and increment the new  If count reaches 0, free record

When to Decrement Instead of decrementing the counts a record references when the record is placed on the free list, it is better to do this when the record is removed from the free list. Instead of decrementing the counts a record references when the record is placed on the free list, it is better to do this when the record is removed from the free list.

Why  Breaks the recursive decrementing work into shorter pieces  Compiler emits code to check whether the count has reached 0, but the recursive decrementing will be done only in one place, in the allocator

Problems with Reference Count  Cycles of garbage cannot be reclaimed  Incrementing the reference counts is very expensive

Solutions-Cycles, Expensive  Require the programmer to break the cycle  Combine reference counting with mark-sweep  No solution for it being expensive  Problems outweigh advantages, thus rarely used

Copying Collection  Reachable part is a directed graph with records as nodes, pointers as edges, and variables as roots  Copy the graph from “from-space” to “to-space”  Delete all “from-space”qq

Access to Nonlocal Names  Lexical scope without nested procedures –Allows nonlocals to be found via static addresses –Uses physical layout –All storage locations known at compile time –Functions can be passed as parameters

Lexical Scope Example main { /* main */ A.R. main p(); … p(); … } /* main */ A.R. p p{ control link main p{ control link main int n; no access link int n; no access link n = 1; … n = 1; … r(2); A.R. r r(2); A.R. r fun q{ /* inside of p */ contol link p fun q{ /* inside of p */ contol link p n = 5; /*n non-local non-global*/ access link p n = 5; /*n non-local non-global*/ access link p } … } … fun r(int n){ /* inside of p */ A.R. q fun r(int n){ /* inside of p */ A.R. q q(); control link r q(); control link r }/* r */ access link p }/* r */ access link p } … } …

Access Chaining Example main{ A.R. main p(); … p(); … fun p{ A.R. p fun p{ A.R. p int x; ctl link main, acc main int x; ctl link main, acc main q(); A.R. q q(); A.R. q fun q{ ctl link p, acc link p fun q{ ctl link p, acc link p r(); A.R. r r(); A.R. r fun r{ /* fun r */ ctl link q, acc link q fun r{ /* fun r */ ctl link q, acc link q x = 2; A.R. p x = 2; A.R. p if... then p(); ctl link r, acc link main if... then p(); ctl link r, acc link main } /* fun r */ A.R. q } /* fun r */ A.R. q } /* fun q */ ctl link p, acc link p } /* fun q */ ctl link p, acc link p } /* fun p */ A.R. r } /* fun p */ A.R. r } /* fun main */ ctl link q, acc link q

Passing Function Example main{ A.R. main q(); … q(); … fun p (fun a) { A.R. q fun p (fun a) { A.R. q a(); ctl link main, acc main a(); ctl link main, acc main } /* end p */ A.R. p } /* end p */ A.R. p fun q { ctl link q, acc main fun q { ctl link q, acc main int x; A.R. a int x; A.R. a x = 2; ctl link p, acc q x = 2; ctl link p, acc q p(r); p(r); fun r{ fun r{ printf( x ); printf( x ); } /* end r */ } /* end r */ } /* end q */ } /* end q */ } /* end main */

Dynamic Scope lisp, apl, snobol, spitbol, scheme main(){ float r; float r; r :=.25; r :=.25; show; small; show; small; fun show{ fun show{ printf(r); printf(r); } fun small{ fun small{ fun r; fun r; r := 0.125; r := 0.125; show; show; }}  What is printed?

Parameter Passing  Call by value  Call by reference (address)  Call by copy restore  Call by name

Call by Value  Only the value is passed  Storage is in the A.R. of called function  Caller evaluates and places the value in callee A.R.  Operations do not effect original value

Call by Reference  Caller passes pointer to callee  if a+b is passed, the address of a temporary is used  Consider swap(i,a[i]) –Does indeed swap –Addresses are bound at time of call

Call by Copy-Restore  Value of argument is given to callee  Upon completion value is copied back to caller  swap(i, a[i]) works correctly

Copy-Restore/Reference Example int a = 1; main() { unsafe(a); unsafe(a); print(a); print(a);} fun unsafe( int x) { x = 2; x = 2; a = 0; a = 0;}

Copy-Restore/Reference Example Nested Functions main() { int a = 1; int a = 1; unsafe(a); unsafe(a); print(a); print(a); fun unsafe( int x) { fun unsafe( int x) { x = 2; x = 2; a = 0; a = 0; }}

Call by Name  A macro  Body of the function replaces the call  Local values are protected  swap(i, a[i]) does not work since i will have changed value

Call by Name Example  #include  #include  int temp;  #define swap(x,y) { temp = x, x = y, y = temp; }   main(){  int i;  int a[5] ={1,2,3,4,5};  for(i = 0; i < 5; i++)  printf("a[%d]=%d ",i,a[i]);  i = 3;  swap(i,a[i]);  for(i = 0; i < 5; i++ )  printf("a[%d]=%d ",i,a[i]);  }  Prints a[0]=1 a[1]=2 a[2]=3 a[3]=4 a[4]=3