Scope and Activation Records

Similar documents
COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 20: Stack Frames 7 March 08

Stack Allocation. Run-Time Data Structures. Static Structures

Stack machines The MIPS assembly language A simple source language Stack-machine implementation of the simple language Readings:

Master of Sciences in Informatics Engineering Programming Paradigms 2005/2006. Final Examination. January 24 th, 2006

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Regression Verification: Status Report

Memory Allocation. Static Allocation. Dynamic Allocation. Memory Management. Dynamic Allocation. Dynamic Storage Allocation

Chapter 5 Names, Bindings, Type Checking, and Scopes

1 The Java Virtual Machine

Computer Systems Architecture

Machine-Code Generation for Functions

Semester Review. CSC 301, Fall 2015

Parameter passing in LISP

Symbol Tables. Introduction

Sources: On the Web: Slides will be available on:

Tail call elimination. Michel Schinz

Interpreters and virtual machines. Interpreters. Interpreters. Why interpreters? Tree-based interpreters. Text-based interpreters

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

C Compiler Targeting the Java Virtual Machine

2) Write in detail the issues in the design of code generator.

Glossary of Object Oriented Terms

Module 2 Stacks and Queues: Abstract Data Types

Instruction Set Architecture

Chapter 7: Functional Programming Languages

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

JavaScript. JavaScript: fundamentals, concepts, object model. Document Object Model. The Web Page. The window object (1/2) The document object

Programming Language Features (cont.) CMSC 330: Organization of Programming Languages. Parameter Passing in OCaml. Call-by-Value

X86-64 Architecture Guide

Introduction. Earlier programs structured as methods that call one another in a disciplined, hierarchical manner Recursive methods

Lecture 11: Tail Recursion; Continuations

Chapter 5. Recursion. Data Structures and Algorithms in Java

Moving from CS 61A Scheme to CS 61B Java

Tail Recursion Without Space Leaks

Recursion. Slides. Programming in C++ Computer Science Dept Va Tech Aug., Barnette ND, McQuain WD

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Recursion. Definition: o A procedure or function that calls itself, directly or indirectly, is said to be recursive.

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

Advanced compiler construction. General course information. Teacher & assistant. Course goals. Evaluation. Grading scheme. Michel Schinz

JVM Tool Interface. Michal Pokorný

Introduction to Embedded Systems. Software Update Problem

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

The Java Virtual Machine and Mobile Devices. John Buford, Ph.D. Oct 2003 Presented to Gordon College CS 311

Optimizations. Optimization Safety. Optimization Safety. Control Flow Graphs. Code transformations to improve program

Lecture Outline. Stack machines The MIPS assembly language. Code Generation (I)

Chapter 12. Paging an Virtual Memory Systems

The Advantages of Dan Grossman CSE303 Spring 2005, Lecture 25

Java Program Coding Standards Programming for Information Technology

Lecture Data Types and Types of a Language

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

High-Level Language. Building a Modern Computer From First Principles.

More MIPS: Recursion. Computer Science 104 Lecture 9

umps software development

Laboratorio di Sistemi Operativi Anno Accademico

Bypassing Browser Memory Protections in Windows Vista

Visualizing principles of abstract machines by generating interactive animations

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Topic VII. Data abstraction and modularity SML Modules a. References: Chapter 7 of ML for the working programmer (2ND

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

Chapter 5 Programming Statements. Chapter Table of Contents

GENERIC and GIMPLE: A New Tree Representation for Entire Functions

Podpora počas behu. Runtime Environments. Ján Šturc. Zima 2010

Assembly Language: Function Calls" Jennifer Rexford!

University of Twente. A simulation of the Java Virtual Machine using graph grammars

The previous chapter provided a definition of the semantics of a programming

MXwendler Javascript Interface Description Version 2.3

Inside the Java Virtual Machine

From Bytecode to Javascript: the Js of ocaml Compiler

Stack Overflows. Mitchell Adair

Software Analysis (POPA Sec , 2.5.4, 2.5.6)

CS2210: Compiler Construction. Runtime Environment

Code Generation & Parameter Passing

Overview. Elements of Programming Languages. Advanced constructs. Motivating inner class example

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

Lecture 1 Introduction to Android

Trace-Based and Sample-Based Profiling in Rational Application Developer

AP Computer Science Java Subset

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

The Little Man Computer

TOWARDS A GREEN PROGRAMMING PARADIGM FOR MOBILE SOFTWARE DEVELOPMENT

Curriculum Map. Discipline: Computer Science Course: C++

Typy danych. Data types: Literals:

13 Classes & Objects with Constructors/Destructors

Using the Intel Inspector XE

Virtual vs Physical Addresses

Relational Databases

Chapter 13 Storage classes

picojava TM : A Hardware Implementation of the Java Virtual Machine

MICROPROCESSOR AND MICROCOMPUTER BASICS

Parameter Passing. Parameter Passing. Parameter Passing Modes in Fortran. Parameter Passing Modes in C

Database Programming with PL/SQL: Learning Objectives

Static Analysis. Find the Bug! : Analysis of Software Artifacts. Jonathan Aldrich. disable interrupts. ERROR: returning with interrupts disabled

Pseudo code Tutorial and Exercises Teacher s Version

Homework/Program #5 Solutions

Tutorial on C Language Programming

Transcription:

CS 345 Scope and Activation Records Vitaly Shmatikov slide 1

Activation Records for Functions Block of information ( frame ) associated with each function call, including: Parameters Local variables Return address Location to put return value when function exits Control link to the caller s activation record Saved registers Temporary variables and intermediate results (not always) Access link to the function s static parent slide 2

Activation Record Layout Control link Return address Return-result addr Parameters Local variables Intermediate results Environment pointer Return address Location of code to execute on function return Return-result address Address in activation record of calling block to receive returned value Parameters Locations to contain data from calling block slide 3

Example Control link Return address Return result addr Parameters Local variables Intermediate results Environment pointer Function fact(n) = if n<=1 then 1 else n * fact(n-1) Return result address: location to put fact(n) Parameter Set to value of n by calling sequence Intermediate result Locations to contain value of fact(n-1) slide 4

Typical x86 Activation Record frame pointer stack pointer slide 5

Run-Time Stack Activation records are kept on the stack Each new call pushes an activation record Each completing call pops the topmost one Stack has all records of all active calls at any moment during execution (topmost record = most recent call) Example: fact(3) Pushes one activation record on the stack, calls fact(2) This call pushes another record, calls fact(1) This call pushes another record, resulting in three activation records on the stack slide 6

Function Call fact(k) Control link Return-result addr n k fact(n-1) Environment pointer fact(3) fact(2) Control link Return-result addr n 3 fact(n-1) Control link Return-result addr n 2 fact(n-1) fact(n) = if n<= 1 then 1 else n * fact(n-1) Return address omitted; would be a pointer into code segment fact(1) Control link Return-result addr n 1 fact(n-1) slide 7

Function Return fact(3) High addresses fact(2) fact(1) Low addresses Control link Return-result addr n 3 fact(n-1) Control link Return-result addr n 2 fact(n-1) 1 Control link Return-result addr n 1 fact(n-1) fact(3) Control link Return-result addr n 3 fact(n-1) 2 fact(2) Control link Return-result addr n 2 fact(n-1) 1 fact(n) = if n<=1 then 1 else n * fact(n-1) slide 8

Scoping Rules Global and local variables x, y are local to outer block z is local to inner bock x, y are global to inner block { int x=0; int y=x+1; { int z=(x+y)*(x-y); Static scope }; }; Global refers to declaration in closest enclosing block Dynamic scope Global refers to most recent activation record Do you see the difference? (think function calls) slide 9

Static vs. Dynamic Scope Example var x=1; function g(z) { return x+z; } function f(y) { var x = y+1; return g(y*x); } f(3); outer block x 1 f(3) y 3 x 4 g(12) z 12 Which x is used for expression x+z? static scope dynamic scope slide 10

Activation Record For Static Scope Control link Access link Return address Return result addr Parameters Local variables Intermediate results Environment pointer Control link Link to activation record of previous (calling) block Depends on dynamic behavior of the program Access link Link to activation record of closest lexically enclosing block in program text Is this needed in C? (why?) Depends on the static program text slide 11

Static Scope with Access Links var x=1; function g(z) = { return x+z; } function f(y) = f(3); { var x = y+1; return g(y*x); } Use access link to find global variable: - Access link is always set to frame of closest enclosing lexical block - For function body, this is the block that contains function definition outer block f(3) g(12) x 1 control link access link g control link access link f control link access link y 3 x 4 control link access link z 12 slide 13

Variable Arguments (Redux) Special functions va_start, va_arg, va_end compute arguments at run-time (how?) slide 14

Activation Record for Variable Args va_arg(ap,type) retrieves next arg from offset ap va_start computes location on the stack past last statically known argument slide 15

Tail Recursion (first-order case) Function g makes a tail call to function f if return value of function f is return value of g Example tail call fun g(x) = if x>0 then f(x) else f(x)*2 not a tail call Optimization: can pop current activation record on a tail call Especially useful for recursive tail call because next activation record has exactly same form slide 16

Example of Tail Recursion Calculate least power of 2 greater than y f(1,3) control return val x 1 y 3 fun f(x,y) = if x>y then x else f(2*x, y); f(1,3) + 7; control return val x 1 y 3 control return val x 2 y 3 control return val x 4 y 3 Optimization Set return value address to that of caller Can we do same with control link? Optimization Avoid return to caller Does this work with dynamic scope? slide 17

Tail Recursion Elimination f(1,3) control return val x 1 y 3 f(2,3) control return val x 2 y 3 f(4,3) control return val x 4 y 3 fun f(x,y) = if x>y then x else f(2*x, y); f(1,3) + 7; Optimization pop followed by push - reuse activation record in place Tail recursive function is equivalent to iterative loop slide 18

Tail Recursion and Iteration f(1,3) f(2,3) f(4,3) control control control return val return val return val x 1 x 2 x 4 y 3 y 3 y 3 fun f(x,y) = if x>y test function g(y) { then x var x = 1; else f(2*x, y); loop body while (!x>y) f(1,y); x = 2*x; initial value } return x; slide 19

Higher-Order Functions Function passed as argument Need pointer to activation record higher up in stack Function returned as the result of function call Need to keep activation record of the returning function (why?) Functions that take function(s) as input and return functions as output are known as functionals slide 20

Pass Function as Argument val x = 4; fun f(y) = x*y; fun g(h) = let val x=7 in h(3) + x; g(f); { var x = 4; { function f(y) {return x*y;} { function g(h) { var x = 7; return h(3) + x; } g(f); } } } There are two declarations of x Which one is used for each occurrence of x? slide 21

Static Scope for Function Argument val x = 4; fun f(y) = x*y; fun g(h) = x 4 f Code for f let g val x=7 in g(f) h x 7 Code for g h(3) + x; g(f); h(3) y 3 follow access link x * y local var How is access link for h(3) set? slide 22

Closures Function value is pair closure = env, code Idea: statically scoped function must carry a link to its static environment with it Only needed if function is defined in a nested block (why?) When a function represented by a closure is called Allocate activation record for call (as always) Set the access link in the activation record using the environment pointer from the closure slide 23

Function Argument and Closures Run-time stack with access links val x = 4; fun f(y) = x*y; fun g(h) = let x 4 access f Code for f in g(f); val x=7 h(3) + x; g(f) access g access h x 7 Code for g h(3) access y 3 access link set from closure slide 24

Summary: Function Arguments Use closure to maintain a pointer to the static environment of a function body When called, set access link from closure All access links point up in stack May jump past activation records to find global vars Still deallocate activation records using stack (last-infirst-out) order slide 25

Return Function as Result Language feature (e.g., ML) Functions that return new functions Example: fun compose(f,g) = (fn x => g(f x)); Function is created dynamically Expression with free variables; values determined at run-time Function value is closure = env, code Code not compiled dynamically (in most languages) Need to maintain environment of the creating function (why?) slide 26

Return Function with Private State fun mk_counter (init : int) = let val count = ref init fun counter(inc:int) = (count :=!count + inc;!count) in counter end; val c = mk_counter(1); c(2) + c(2); Function to make counter returns a closure How is correct value of count determined in c(2)? slide 27

Function Results and Closures fun mk_counter (init : int) = let val count = ref init in end; fun counter(inc:int) = (count :=!count + inc;!count) counter val c = mk_counter(1); c(2) + c(2); mk_counter(1) Call changes cell value from 1 to 3 c(2) mk_c access c access init 1 count counter access inc 2 Code for counter Code for mk_counter 1 3 slide 28

Closures in Web Programming Useful for event handlers function AppendButton(container, name, message) { var btn = document.createelement('button'); btn.innerhtml = name; btn.onclick = function(evt) { alert(message); } container.appendchild(btn); } Environment pointer lets the button s click handler find the message to display slide 29

Managing Closures Closures as used to maintain static environment of functions as they are passed around May need to keep activation records after function returns (why?) Stack (last-in-first-out) order fails! (why?) Possible stack implementation: Put activation records on heap Instead of explicit deallocation, invoke garbage collector as needed Not as totally crazy as is sounds (may only need to search reachable data) slide 30