Run-Time Environements

Similar documents
Run-time Environments - 2

Concepts Introduced in Chapter 7

! Those values must be stored somewhere! Therefore, variables must somehow be bound. ! How?

Today's Topics. CISC 458 Winter J.R. Cordy

Scope, Functions, and Storage Management

Run-time Environments

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Run-time Environments

Run Time Environments

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

The basic operations defined on a symbol table include: free to remove all entries and free the storage of a symbol table

Runtime Environments I. Basilio B. Fraguela

Contents. 8-1 Copyright (c) N. Afshartous

Run-Time Environments

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

Procedure and Function Calls, Part II. Comp 412 COMP 412 FALL Chapter 6 in EaC2e. target code. source code Front End Optimizer Back End

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Compilers. 8. Run-time Support. Laszlo Böszörmenyi Compilers Run-time - 1

G53CMP: Lecture 14. Run-Time Organisation I. Henrik Nilsson. University of Nottingham, UK. G53CMP: Lecture 14 p.1/37

Module 27 Switch-case statements and Run-time storage management

This Lecture. G53CMP: Lecture 14 Run-Time Organisation I. Example: Lifetime (1) Storage Areas

Typical Runtime Layout. Tiger Runtime Environments. Example: Nested Functions. Activation Trees. code. Memory Layout

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

G Programming Languages - Fall 2012

Announcements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM.

Where We Are. Lexical Analysis. Syntax Analysis. IR Generation. IR Optimization. Code Generation. Machine Code. Optimization.

SISTEMI EMBEDDED. Stack, Subroutine, Parameter Passing C Storage Classes and Scope. Federico Baronti Last version:

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

The Procedure Abstraction Part I: Basics

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Procedure and Object- Oriented Abstraction

Runtime management. CS Compiler Design. The procedure abstraction. The procedure abstraction. Runtime management. V.

What about Object-Oriented Languages?

Attributes of Variable. Lecture 13: Run-Time Storage Management. Lifetime. Value & Location

Chapter 9. Def: The subprogram call and return operations of a language are together called its subprogram linkage

CA Compiler Construction

Implementing Subroutines. Outline [1]

The Procedure Abstraction

Name, Scope, and Binding. Outline [1]

System Software Assignment 1 Runtime Support for Procedures

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

Chapter 10. Implementing Subprograms

Announcements. Scope, Function Calls and Storage Management. Block Structured Languages. Topics. Examples. Simplified Machine Model.

Runtime Support for Algol-Like Languages Comp 412

12/4/18. Outline. Implementing Subprograms. Semantics of a subroutine call. Storage of Information. Semantics of a subroutine return

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

CMa simple C Abstract Machine

Programming Languages

Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule

Implementing Subprograms

Chapter 10. Implementing Subprograms ISBN

CSCI312 Principles of Programming Languages!

Lecture08: Scope and Lexical Address

Run-time Environments - 3

Compiler Construction

CSE 3302 Notes 5: Memory Management

An Activation Record for Simple Subprograms. Activation Record for a Language with Stack-Dynamic Local Variables

Compilers and computer architecture: A realistic compiler to MIPS

Compiler Construction

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Lecture Notes on Memory Management

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

ECE 2035 Programming HW/SW Systems Fall problems, 5 pages Exam Three 28 November 2012

Language of the Machine Recursive functions

Compilers and Code Optimization EDOARDO FUSELLA

Run-Time Environments

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

Programming Languages: Lecture 12

CSE 504: Compiler Design. Runtime Environments

Implementing Subprograms

Qualifying Exam in Programming Languages and Compilers

1 Lexical Considerations

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

The Procedure Abstraction Part I Basics

Properties of an identifier (and the object it represents) may be set at

Chapter 5. Names, Bindings, and Scopes

Inheritance. Transitivity

Technical Questions. Q 1) What are the key features in C programming language?

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

Run Time Environment. Procedure Abstraction. The Procedure as a Control Abstraction. The Procedure as a Control Abstraction

What goes inside when you declare a variable?

Memory management COSC346

Intermediate Formats. for object oriented languages

Procedures and Stacks

Dynamic Data Structures (II)

Iterative Languages. Scoping

CS558 Programming Languages

Informatica 3 Marcello Restelli

LECTURE 14. Names, Scopes, and Bindings: Scopes

Translation to Intermediate Code. Chapter 7

Concurrent and Real-Time Programming in Java

Lecture Notes on Memory Management

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Intermediate Formats. Martin Rinard Laboratory for Computer Science

Object-Oriented Programming

Run-Time Data Structures

Transcription:

Run-Time Environements Martin Sulzmann Martin Sulzmann Run-Time Environements 1 / 21

Overview Things to consider Static program code versus dynamic procedure activation. First-order functions (C). Nested procedure declarations (C++/Java). Higher-order functions (OCaml/Haskell/Go). Allocation and deallocation of data objects. Stack versus heap. Martin Sulzmann Run-Time Environements 2 / 21

Static versus Dynamic Classification Static = at compile time. Dynamic = at run-time. Stack maintains data local to (procedure call). Layout statically known. Managed automatically by generated code. Heap maintains (dynamic) data that survives between calls. Layout statically not known. Managed dynamically by garbage collector. Martin Sulzmann Run-Time Environements 3 / 21

Storage Organization Classification Code area: instructions. Static area: (global) constants. Heap: objects (records, arrays,...). Run-time stack: (procedure) activation records/stack frames. Memory Layout Code Static Data Stack. Heap Martin Sulzmann Run-Time Environements 4 / 21

Procedures Example int fib(int n){ int f1, f2; if (n<=1) return 1; f1=fib(n-1); f2=fib(n-2); return f1+f2; } We assume a procedural language with sequential control flow, and return to point after the call. Martin Sulzmann Run-Time Environements 5 / 21

Activation Tree Example fib(3) fib(2) fib(1) fib(1) fib(0) Node: procedure activation Child: activation from inside parent Order: Left child finishes before right child begins Life-time: From call to exit Martin Sulzmann Run-Time Environements 6 / 21

Control Stack Stack for procedure activation Depth-first left-to-right visit of activation tree. fib(3) fib(2) fib(1) fib(3) fib(2) fib(1) fib(1) fib(0) Where do we store (local) data, scope of declarations, binding of names? Martin Sulzmann Run-Time Environements 7 / 21

Activation Records Layout Item on (control) stack. Contains all information for an activation of a procedure. local variables function parameters return address static link dynamic link dynamic link = caller s activation record (where to return after procedure exit) static link = non-local data on stack (how to access variables from some outer scope) Martin Sulzmann Run-Time Environements 8 / 21

Variable Access Things to consider Method Variables found in some activation record (ignore heap allocated data). Local variable easy (within current activation record). Non-local variable? Must find proper activation record. Location of a variable represented by (level, relpos). level = level of the lexical scope. relpos = relative position in the activation record. Need protocol to properly access variables based on the information provided. Martin Sulzmann Run-Time Environements 9 / 21

Example Mini-Go { x := 1; y := true } Activation record ----------------------- y 1 <-- relative position ----------------------- x 0 ----------------------- NULL <-- return address ----------------------- NULL <-- static link (SL) ----------------------- NULL <-- dynamic link (DL) ----------------------- Martin Sulzmann Run-Time Environements 10 / 21

Nested Procedure Declarations Example main () { int i; int c() { return i }; int a(int i) { i = 1; int b( ) { int d( ) { return c( ); }; d(); }; b(); }; i = 2; print a(i); } Martin Sulzmann Run-Time Environements 11 / 21

Nested Procedure Declarations (2) Level information Procedure level main 1 c 2 a 2 b 3 d 4 Things to consider Call sequence: a(2) b() d() c() Procedure c has access to variable i. Which i? Where do we find i? Martin Sulzmann Run-Time Environements 12 / 21

Initial Stack of activation records main i=2 SL=Null DL=Null Martin Sulzmann Run-Time Environements 13 / 21

Call a(2) Stack of activation records a(2) // i=1! i=1 SL=main DL=main main i=2 SL=Null DL=Null Martin Sulzmann Run-Time Environements 14 / 21

Call b() Stack of activation records b() SL=a DL=a a(2) // i=1! i=1 SL=main DL=main main i=2 SL=Null DL=Null Martin Sulzmann Run-Time Environements 15 / 21

Call d() Stack of activation records d() SL=b DL=b b() SL=a DL=a a(2) // i=1! i=1 SL=main DL=main main i=2 SL=Null DL=Null Martin Sulzmann Run-Time Environements 16 / 21

Call c() Stack of activation records c() SL=main DL=d d() SL=b DL=b b() SL=a DL=a a(2) // i=1! i=1 SL=main DL=main main i=2 SL=Null DL=Null c = level 2 and d = level 4. Hence k = 2. Follow SL twice. Hence, SL of c equals main. Access i: main (level = 1) and c (level = 2). Follow SL once. Look-up i. Martin Sulzmann Run-Time Environements 17 / 21

Call sequence: a(2) b() d() c() Dynamic link main() a(2) b() d() c() Static link main() a(2) b() d() c() Martin Sulzmann Run-Time Environements 18 / 21

Managing Activation Records Return sequence Pop current activation record. Follow DL to previous one. Jump to return address and continue. Calling sequence Push new activation record. Set DL to previous one. If level of current block > level of previous block then set SL = SL of the kth previous block (=activation) where k= level of current block - level of previous block (see call c()). Otherwise, set SL = SL of previous block. Martin Sulzmann Run-Time Environements 19 / 21

Variable Access Variable access protocol Assume we are within a block at level i and access variable x whose location is represented by (k,relpos). If i=k (x local) then relpos refers to relative position of x in current activation record. If i>k (x global) then we need to follow the SL chain (i-k) times to get to x s activation record. Otherwise (i<k), can never happen. Martin Sulzmann Run-Time Environements 20 / 21

Mini-Go Observation No nested procedure declarations. But nested scope. Variables do not need to be declared at the beginning of a block. Introduce distinct variables (renaming!). Variables in main can then be managed by a single activation record. Martin Sulzmann Run-Time Environements 21 / 21