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

Similar documents
Scope, Functions, and Storage Management

Concepts Introduced in Chapter 7

Memory Management and Run-Time Systems

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

G Programming Languages - Fall 2012

Run-time Environments - 2

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

CS 314 Principles of Programming Languages. Lecture 13

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

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

LECTURE 18. Control Flow

Run-time Environments -Part 1

Implementing Subprograms

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

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

Programming Languages: Lecture 12

NOTE: Answer ANY FOUR of the following 6 sections:

Implementing Subprograms

Run-time Environments

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

Run-time Environments

Run Time Environment. Activation Records Procedure Linkage Name Translation and Variable Access

CIT Week13 Lecture

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

Weeks 6&7: Procedures and Parameter Passing

Special Topics: Programming Languages

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

Runtime Environments I. Basilio B. Fraguela

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

THEORY OF COMPILATION

Programming Languages

Informatica 3 Marcello Restelli

Lecture 11: Subprograms & their implementation. Subprograms. Parameters

Chapter 10. Implementing Subprograms ISBN

System Software Assignment 1 Runtime Support for Procedures

Run-time Environment

Chapter 5 Names, Binding, Type Checking and Scopes

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

Compiler Construction

Programming Language Pragmatics

G Programming Languages - Fall 2012

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

COP4020 Fall 2006 Final Exam

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

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

References and pointers

Lecture 9: Control Flow

Compiler Construction

Topic 7: Activation Records

Run Time Environments

UNIT V Sub u P b ro r g o r g a r m a s

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

Run-Time Environments

CS 314 Principles of Programming Languages

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

CS 4100 Block Structured Languages. Fixed vs Variable. Activation Record. Activation Records. State of an Activation

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

CA Compiler Construction

Compilers and computer architecture: A realistic compiler to MIPS

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

Procedure and Object- Oriented Abstraction

Functions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

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

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

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

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

CS 314 Principles of Programming Languages

Lecture08: Scope and Lexical Address

Compiler Design. Spring Run-Time Environments. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Lecture 9: Procedures & Functions. CS 540 George Mason University

An Elegant Weapon for a More Civilized Age

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

Subroutines and Control Abstraction. CSE 307 Principles of Programming Languages Stony Brook University

CSC 2400: Computer Systems. Using the Stack for Function Calls

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

CSE 307: Principles of Programming Languages

The Procedure Abstraction

Implementing Subprograms

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

CS558 Programming Languages

Language of the Machine Recursive functions

The Procedure Abstraction Part I: Basics

Today's Topics. Last Time Modelling Scopes and Visibility The Run Stack, the Dynamic Pointer Stack, the Display (LL,ON) addressing

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

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Recursion(int day){return Recursion(day += 1);} Comp Sci 1575 Data Structures. Recursive design. Convert loops to recursion

Optimizer. Defining and preserving the meaning of the program

Chapter 10 Implementing Subprograms

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

Names, Bindings, Scopes

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Run-Time Environments

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

Assembly III: Procedures. Jo, Heeseung

Chapter 5. Names, Bindings, and Scopes

CS415 Compilers. Procedure Abstractions. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Transcription:

Attributes of Variable Lecture 13 Run-Time Storage Management CSC 131 Kim Bruce Scope Lifetime Location Value Done! Lifetime Value & Location FORTRAN - all allocated statically - Stack-based (C/C++/Java/Pascal/...) - local vbles/ parameters method/procedure/block entry to exit - allocate space in activation record on run-time stack Heap allocated variable - lifetime independent of scope Static - global vbles or static vbles Sometimes referred to as l-value & r-value - x = x + 1 What does each occurrence of x stand for? - location normally bound when declaration processed Normally values change dynamically - if frozen at compilation then called constants - Java final variables frozen when declaration processed. - Java static final bound at compile time.

Aliases x and y are aliases if both refer to same location. If x, y are aliases then changes to x affect value of y. Java has uniform model where assignment is by sharing, so create aliases. Languages that mix are more confusing. - Common mistakes occur when not realize aliases. E.g, add elt to priority queue and then change it... Pointers Pointers have been lumped with the goto statement as a marvelous way to create impossible to understand programs - K & R, C Programming Language Problems - Dangling pointers -- leave pointer to recycled space stack frame popped or recycled heap item - Dereference nil pointers or other illegal address - Unreachable garbage - in C p+1 different from (int)p + 1 Program Units Activation Record Structure Separate segments of code allowing separate declarations of variables - Ex. procedures, functions, methods, blocks - During execution represented by unit instance fixed code segment activation record with fixed ways of ing items Return address Access info on parameters (how?) Space for local vbles How get to non-local variables?

Invoking Function Returning from Function Make parameters available to callee - E.g., put on stack or in registers Save state of caller (registers, prog. counter) Ensure callee knows where to return Enter callee at first instruction If function, leave result in ible location Get return address and transfer execution Caller restores state Parameter Passing Call-by-reference (FORTRAN, Pascal, C++) - pass address (l-value) of parameter Call-by-copying (Algol 60, Pascal, C, C++) - pass (r-)value of parameter - options in, out, in-out Call-by-name (Algol 60) - pass actual expression (as thunk ) - not macro! - re-evaluate at each - lazy gives efficient implementation if no side effects Won t always work! Call-by-name procedure swap(a, b integer); var temp integer; begin temp = a; a = b; b = temp end; swap(i, a[i]) with i = 1, a[1] = 3, a[3] = 17 Can t write swap that always works!

What about Java? Static Memory allocation FORTRAN Conceptually call-by-sharing Implemented as call-by-value of a reference - All storage known at translation time - Activation records directly associated with code segments - At compile time, instructions and vbles ed by (unit name, offset) - At link time, resolve to absolute addresses. - Procedure call and return straightforward Stack-based Allocation Pascal, C, C++, Java,... - Activation records on stack - Problem static (scope) vs dynamic (return address) - Activation records pushed on call and popped on return - Activation record contains return address return-result address -- if necessary control or dynamic link -- to next stack!ame or static link -- to nearest stack!ame of enclosing scope parameters, local vbles, & intermediate results. Accessing non-local vbles program main; type array_type = array [1..10] of real; var a integer; b array_type; procedure x (var c integer; d array_type); var e array_type; procedure y (f array_type); var g integer; begin z(a+c); end; {y} begin {x}... = b[6]... y(e); end; {x} procedure z (h integer); var a array_type; begin x (h,a); end; begin {main} x (a,b); end. {main}

Assign Variables Offsets Name Level Name Level execution of main a b x z code for x code for z main 0 y 2 a 1 f 3 b 1 g 3 x 1 z 1 c 2 h 2 d 2 a 2 e 2 Look at run time stack when main ca"s x, which ca"s y, which ca"s z, which ca"s x, which ca"s y,... execution of x execution of y execution of z c d e y f g h a code for y c d e y Accessing non-local Variables Allocating Activation Record Length of chain from any fixed procedure to main is always same length Any non-local variable found after some fixed number of links (independent of activation record) # of links = constant determinable at compile time Access via <chain position, offset> where 1st is # of links to traverse, 2nd is offset in activation record. Static - sizes of all local variables and parameters known at compile time - Fixed size activation records Size known at unit activation - Array bounds depend on parameters - Space for parameter descriptors at fixed offset Dynamic - Flexible arrays and pointers - Allocate on heap w/reference on stack

Tail-Recursive Functions Recursion less efficient (space & time) than iteration because of activation records. A call to function f in body of g is tail if g returns immediately after call of f terminates. - Ex fun g x = if x > 0 then f x else f (~x) Tail calls use stack space more efficiently Tail recursive functions even better! Compare Tail-recursive Functions rev [] = [] rev (fstrest) = (rev rest)++[fst] and reverse l = tlrev l [] where tlrev [] r = r tlrev (fstrest) r = tlrev rest (fstr) Can accumulate answer... fact n = tlfact(n,1) tlfact (n,ans) = if n <= 1 then ans else tlfact(n-1,n*ans); Fibonacci int fib(int n) {! int current = 1;! int next = 1;! while (n > 0) {!! int temp = current; current = next;!! next = next + temp;!! n = n - 1;! }! return current; } or recursively fib 0 = 1 fib 1 = 1 fib n = fib (n-1) + fib(n-2); Replace while loops Can replace while by tail recursive function where a" variables used become parameters fastfib n = fibloop n 1 1 where fibloop 0 current next = current fibloop n current next = fibloop (n-1) next (current + next);

Correctness Function Parameters Let a 0, a 1,... be list of Fibonacci numbers Lemma For all n, k 0, fibloop n a k a k+1 = a k+n fastfib n = fibloop n 1 1 = fibloop n a 0 a 1 = a n Harder to cope with because need environment defined in. Two problems - Downward funarg x = 47 f y = x + y; g(h) = let val x = 17 in h(1) > g(f) - When evaluate f(1), is in environment where x = 17!