Scope, Functions, and Storage Management

Similar documents
Virtual Machine (Part I)

From Dependencies to Evaluation Strategies

Pointers and Arrays. More Pointer Examples. Pointers CS 217

cisc1110 fall 2010 lecture VI.2 call by value function parameters another call by value example:

Agenda & Reading. Class Exercise. COMPSCI 105 SS 2012 Principles of Computer Science. Arrays

Introduction To Files In Pascal

Lecture Overview. Knowledge-based systems in Bioinformatics, 1MB602. Procedural abstraction. The sum procedure. Integration as a procedure

CPSC 213. Polymorphism. Introduction to Computer Systems. Readings for Next Two Lectures. Back to Procedure Calls

Procedures vs. Functions. Procedure Call. Syntax. Activation Record for Nested Blocks. Environment. CSE 3302 Programming Languages

Sample Midterm Solutions COMS W4115 Programming Languages and Translators Monday, October 12, 2009

Memory Management Functions

- 2 U NIX FILES 1. Explin different file types vilble in UNIX or P OSIX s ystem. ( 08 mrks) ( My-08/Dec-08/My-10/My- 12) 2. Wht is n API? How is it di

Symbol Table management

Reference types and their characteristics Class Definition Constructors and Object Creation Special objects: Strings and Arrays

Lecture 10 Evolutionary Computation: Evolution strategies and genetic programming

Mid-term exam. Scores. Fall term 2012 KAIST EE209 Programming Structures for EE. Thursday Oct 25, Student's name: Student ID:

Functor (1A) Young Won Lim 8/2/17

PARALLEL AND DISTRIBUTED COMPUTING

Functor (1A) Young Won Lim 10/5/17

! Smaller, simpler, subcomponent of program! Provides abstraction. n hide low-level details, give high-level structure

UNIT 11. Query Optimization

CS201 Discussion 10 DRAWTREE + TRIES

Discussion 1 Recap. COP4600 Discussion 2 OS concepts, System call, and Assignment 1. Questions. Questions. Outline. Outline 10/24/2010

box Boxes and Arrows 3 true 7.59 'X' An object is drawn as a box that contains its data members, for example:

2 Computing all Intersections of a Set of Segments Line Segment Intersection

Reducing Costs with Duck Typing. Structural

Today s Lecture. Basics of Logic Design: Boolean Algebra, Logic Gates. Recursive Example. Review: The C / C++ code. Recursive Example (Continued)

Fig.25: the Role of LEX

Outline CS 412/413. Function calls. Stack layout. Tiling a call. Two translations

Midterm 2 Sample solution

CSCI 3130: Formal Languages and Automata Theory Lecture 12 The Chinese University of Hong Kong, Fall 2011

CS321 Languages and Compiler Design I. Winter 2012 Lecture 5

Outline. Tiling, formally. Expression tile as rule. Statement tiles as rules. Function calls. CS 412 Introduction to Compilers

COMMON HALF YEARLY EXAMINATION DECEMBER 2018

Compiler-Assisted Cache Replacement

Lists in Lisp and Scheme

520 Principles of Programming Languages. Memory Management. Memory Management... 35: Garbage Collection

COMPUTER SCIENCE 123. Foundations of Computer Science. 6. Tuples

Coprocessor memory definition. Loic Pallardy / Arnaud Pouliquen

Page 1. Memory Allocation and Usage CSE 361S. Different free lists for different size classes

Lexical analysis, scanners. Construction of a scanner

Data Flow on a Queue Machine. Bruno R. Preiss. Copyright (c) 1987 by Bruno R. Preiss, P.Eng. All rights reserved.

Virtual Machine I: Stack Arithmetic

Introduction to Computer Science, Shimon Schocken, IDC Herzliya. Lecture Writing Classes

CSCI 104. Rafael Ferreira da Silva. Slides adapted from: Mark Redekopp and David Kempe

CS 340, Fall 2014 Dec 11 th /13 th Final Exam Note: in all questions, the special symbol ɛ (epsilon) is used to indicate the empty string.

Stack Manipulation. Other Issues. How about larger constants? Frame Pointer. PowerPC. Alternative Architectures

Registering as a HPE Reseller. Quick Reference Guide for new Partners in Asia Pacific

Lab 1 - Counter. Create a project. Add files to the project. Compile design files. Run simulation. Debug results

Compilers Spring 2013 PRACTICE Midterm Exam

10.5 Graphing Quadratic Functions

ITEC2620 Introduction to Data Structures

Complete Coverage Path Planning of Mobile Robot Based on Dynamic Programming Algorithm Peng Zhou, Zhong-min Wang, Zhen-nan Li, Yang Li

Data sharing in OpenMP

CS 241. Fall 2017 Midterm Review Solutions. October 24, Bits and Bytes 1. 3 MIPS Assembler 6. 4 Regular Languages 7.

Real-Time Programming in Java

Arrays as functions. Types. Multidimensional Arrays (row major, column major form) Java arrays

Graphs with at most two trees in a forest building process

A Formalism for Functionality Preserving System Level Transformations

Some Thoughts on Grad School. Undergraduate Compilers Review and Intro to MJC. Structure of a Typical Compiler. Lexing and Parsing

Simrad ES80. Software Release Note Introduction

Lecture 7: Integration Techniques

MTH 146 Conics Supplement

Simplifying Algebra. Simplifying Algebra. Curriculum Ready.

MIPS I/O and Interrupt

TSGS#15(02)0025. Technical Specification Group Services and System Aspects Meeting #15, Cheju Island, Korea, March 2002

How. Without. project. your. wanting. personal. Corey. by Galen E

Engineer To Engineer Note

The Greedy Method. The Greedy Method

L2-Python-Data-Structures

Integration of a Scenario Service in a Multimedia Messaging System

UT1553B BCRT True Dual-port Memory Interface

Agilent Mass Hunter Software

Reducing a DFA to a Minimal DFA

Network Interconnection: Bridging CS 571 Fall Kenneth L. Calvert All rights reserved

Introduction to Julia for Bioinformatics

Keeping Track of Free Blocks The course that gives CMU its Zip! Dynamic Memory Allocation II Nov 7, Allocating From Explicit Free Lists

OUTPUT DELIVERY SYSTEM

Scanner Termination. Multi Character Lookahead. to its physical end. Most parsers require an end of file token. Lex and Jlex automatically create an

Network Layer: Routing Classifications; Shortest Path Routing

12 <= rm <digit> 2 <= rm <no> 2 <= rm <no> <digit> <= rm <no> <= rm <number>

Solution of Linear Algebraic Equations using the Gauss-Jordan Method

Stack. A list whose end points are pointed by top and bottom

Languages. L((a (b)(c))*) = { ε,a,bc,aa,abc,bca,... } εw = wε = w. εabba = abbaε = abba. (a (b)(c)) *

Linear Lists. Linked List Mechanics. Sequential Lists. Linked Lists. 6. LL Mechanics. 6. LL Mechanics. Slides

6/23/2011. Review: IEEE-754. CSE 2021: Computer Organization. Exercises. Examples. Shakil M. Khan (adapted from Profs. Roumani & Asif)

L. Yaroslavsky. Fundamentals of Digital Image Processing. Course

CIS 1068 Program Design and Abstraction Spring2015 Midterm Exam 1. Name SOLUTION

Deterministic. Finite Automata. And Regular Languages. Fall 2018 Costas Busch - RPI 1

CSCE 531, Spring 2017, Midterm Exam Answer Key

Allocator Basics. Dynamic Memory Allocation in the Heap (malloc and free) Allocator Goals: malloc/free. Internal Fragmentation

Systems I. Logic Design I. Topics Digital logic Logic gates Simple combinational logic circuits

Example: 2:1 Multiplexer

What are suffix trees?

McAfee Network Security Platform

LING/C SC/PSYC 438/538. Lecture 21 Sandiway Fong

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

Definition of Regular Expression

CS 268: IP Multicast Routing

Fall 2018 Midterm 1 October 11, ˆ You may not ask questions about the exam except for language clarifications.

Transcription:

Scope, Functions, nd Storge Mngement Block-structured lnguges nd stck storge In-le Blocks (previous set of overheds) ctivtion records storge for locl, glol vriles First-order functions (previous set of overheds) prmeter pssg til recursion nd itertion Higher-order functions devitions from stck disciple lnguge expressiveness => implementtion complexity Higher-order functions Lnguge fetures Functions pssed s rguments Functions tht return functions from nested locks Need to mt environment of function Simpler cse Function pssed s rgument Need poter to ctivtion record higher up stck More complicted second cse Function returned s result of function cll Need to keep ctivtion record of returng function L. Dillon, CSE 452, Fll 2008 1 L. Dillon, CSE 452, Fll 2008 2 Pss function s rgument Sttic scope for function rguments vl x = ; vl x= Hello, world There re two declrtions for x. Which one is to e used for ech occurrence of x? vl x = ; : vl x= Hello, world H.. ()?? (To reduce clutter, I m not showg CLs ARs; when omitted, you my ssume CL references the AR directly ove.) L. Dillon, CSE 452, Fll 2008 3 How is the ccess lk for h set up? It needs to reference the AR declrg the ctul rgument But the ctul rgument cn e different for different clls to g Cnnot determe stticlly how to fd the declrg AR L. Dillon, CSE 452, Fll 2008 4

Solution: Closures Exmple: function rgument Represent function vlue y closure, consistg of pir <l, i> where l pots to the AR declrg the function i.e. the glol referencg environment needed when the function is clled i pots to the first struction of the function code i.e., the structions to execute when the function is clled When function represented y closure is clled, Allocte new ctivtion record for the cll ( the usul mnner) Set the ccess lk the new ctivtion record to the poter l from the closure: new AR := l L. Dillon, CSE 452, Fll 2008 5 : vl x = ; vl x= Hello, world () To itilize function, set it to the closure formed us L. Dillon, CSE 452, Fll 2008 6 the current vlue of the when the function is clled this AR will e used to estlish the clled function s glol referencg environment the loction of the function code (the function code is not -le with the code for the lock) Exmple: function rgument Exmple: function rgument : vl x = ; vl x= Hello, world () : vl x = ; vl x= Hello, world () To itilize function, set it to the closure formed us the current vlue of the when the function is clled this AR will e used to estlish the clled function s glol referencg environment the loction of the function code (the function code is not -le with the code for the lock) L. Dillon, CSE 452, Fll 2008 7 To itilize function, set it to the closure formed us the current vlue of the when the function is clled this AR will e used to estlish the clled function s glol referencg environment the loction of the function code (the function code is not -le with the code for the lock) L. Dillon, CSE 452, Fll 2008 8

Exmple: function rgument Exmple: function rgument : vl x = ; vl x= Hello, world () L. Dillon, CSE 452, Fll 2008 9 Initilize the AL the AR for the cll to g To itilize function, set it to the closure formed us use the glol environment lk (first component) of the vlue ound to g. Pss the the current functionl vlue of the rgument when the function is clled this AR will e forml used prmeter to estlish := the the ctul clled rgument function s glol referencg environment In the thexmple: loction of the function code (the function code is not -le with how the re code the ARs for the with lock) the closures for f & g found? : vl x = ; vl x= Hello, world () L. Dillon, CSE 452, Fll 2008 10 To itilize Trnsfer control function, to gset it to the closure formed us the current Set EP vlue to reference of the the when se of the the function new ARis clled this AR will e used Set to estlish PC to first the struction clled function s code for glol g referencg environment the loction use the of IP the (second function component) code (the function of the closure code is ound not -le to g with the code for the lock) Exmple: function rgument Exmple: function rgument : vl x = ; vl x= Hello, world () : vl x = ; vl x= Hello, world () To itilize function, set it to the closure formed us Initilize the current the AL vlue of the the AR for when the the cll function to h is clled this AR will e use used the to glol estlish environment the clled lk function s (first component) glol referencg of the closure environment ound to h. the loction of the function code (the function code is not -le with the code for the lock) L. Dillon, CSE 452, Fll 2008 11 To itilize Trnsfer control function, to hset it to the closure formed us the current Set EP vlue to reference of the the when se of the the function new ARis clled this AR will e used Set to estlish PC to first the struction clled function s code for glol h referencg environment the loction use the of IP the of function the closure code ound (the function to h code is not -le with the code for the lock) L. Dillon, CSE 452, Fll 2008 12

Exmple: function rgument Summr Function rguments : vl x = ; vl x= Hello, world () L. Dillon, CSE 452, Fll 2008 13 To itilize function, set it to the closure formed us thehow current re vlue the of the ARs contg when the function the is dgs clled this AR will e used to estlish the clled function s glol referencg environment for y nd x found? the loction of the function code (the function code is not -le with the code for the lock) To represent function, use closure contg A lk to the glol referencg environment which to evlute the function ody A lk to the function code When function is clled AL new AR := the lk to the glol referencg environment When redy to trnsfer control: PC := the lk to the function code All ALs pot up stck (if stck grows down) My skip over ARs to fd glol vriles My still dellocte ARs usg stck (lifo) order L. Dillon, CSE 452, Fll 2008 14 Hrder cse: return function s result Lnguge feture Functions tht return new functions Need to mt environment of function Exmple fun compose(f,g) = (fn x => g(f x)); Function creted dynmiclly Vlues of free vriles the function ody re determed t run time function vlue is closure Exmple - dttype Trns = DEP of rel WTD of rel; - fun mk_cct_lncer(it: rel) = = = vl mt = ref it; = fun l (DEP x) = (mt :=!mt + x;!mt) = l (WTD x) = (mt :=!mt - x;!mt) = = l = - vl lnce = mk_cct_lncer(100.00); vl lnce = fn : Trns -> rel - lnce (WTD 25.00); vl it = 75.0 : rel - - mp lnce [WTD 30.0, DEP 200.00, WTD 65.00]; vl it = [45.0,245.0,180.0] : rel list L. Dillon, CSE 452, Fll 2008 15 L. Dillon, CSE 452, Fll 2008 16

Exmple Exmple : fun mk_cct_lncer(it: rel) = vl mt = ref it; fun l (DEP x) = (mt :=!mt + x;!mt) l (WTD x) = (mt :=!mt - x;!mt) l vl lnce = mk_cct_lncer(100.00); lnce (WTD 25.00); lnce (DEP 10.00); lnce (DEP 15.00); fun mk_(it: rel) = : vl mt = ref it; fun l (DEP x) = (mt :=!mt + x;!mt) l (WTD x) = (mt :=!mt - x;!mt) l vl lce = mk_(100.00); lce (WTD 25.00); lce (DEP 10.00); lce (DEP 15.00); (mk_...) (lce...) mk_: lce: it: 100. mt: l: 25. L. Dillon, CSE 452, Fll 2008 17 L. Dillon, CSE 452, Fll 2008 18 Exmple Exmple fun mk_(it: rel) = : vl mt = ref it; fun l (DEP x) = (mt :=!mt + x;!mt) l (WTD x) = (mt :=!mt - x;!mt) l vl lce = mk_(100.00); lce (WTD 25.00); lce (DEP 10.00); (mk_...) (lce...) mk_: lce: it: 100. mt: l: 25. : fun mk_(it: rel) = vl mt = ref it; fun l (DEP x) = (mt :=!mt + x;!mt) l (WTD x) = (mt :=!mt - x;!mt) l vl lce = mk_(100.00); lce (WTD 25.00); lce (DEP 10.00); (mk_...) (lce...) mk_: lce: it: 100. mt: l: 25. lce (DEP 15.00); lce (DEP 15.00); L. Dillon, CSE 452, Fll 2008 19 L. Dillon, CSE 452, Fll 2008 20

Exmple Exmple fun mk_(it: rel) = : vl mt = ref it; fun l (DEP x) = (mt :=!mt + x;!mt) l (WTD x) = (mt :=!mt - x;!mt) l vl lce = mk_(100.00); lce (WTD 25.00); lce (DEP 10.00); lce (DEP 15.00); (mk_...) (lce...) mk_: lce: it: 100. mt: l: 25. 100.0 fun mk_(it: rel) = : vl mt = ref it; fun l (DEP x) = (mt :=!mt + x;!mt) l (WTD x) = (mt :=!mt - x;!mt) l vl lce = mk_(100.00); lce (WTD 25.00); lce (DEP 10.00); lce (DEP 15.00); (mk_...) (lce...) mk_: lce: it: 100. mt: l: 25. 100.0 Wht would normlly hppen next nd wht is the next sttement to execute? Why cn t we use the norml procedure this sitution? L. Dillon, CSE 452, Fll 2008 21 L. Dillon, CSE 452, Fll 2008 22 Exmple Exmple : fun mk_(it: rel) = vl mt = ref it; fun l (DEP x) = (mt :=!mt + x;!mt) l (WTD x) = (mt :=!mt - x;!mt) l vl lce = mk_(100.00); lce (WTD 25.00); lce (DEP 10.00); lce (DEP 15.00); (mk_...) (lce...) mk_: lce: it: 100. mt: l: 25. 100.0 How re the ARs declrg x nd mt found? Wht ARs re on the sttic ch? the dynmic ch? Wht is the effect of executg the next two expressions? fun mk_(it: rel) = : vl mt = ref it; fun l (DEP x) = (mt :=!mt + x;!mt) l (WTD x) = (mt :=!mt - x;!mt) l vl lce = mk_(100.00); lce (WTD 25.00); lce (DEP 10.00); lce (DEP 15.00); (mk_...) (lce...) mk_: lce: it: 100. mt: l: 25. You should fish up this exmple... 100.0 75.0 L. Dillon, CSE 452, Fll 2008 23 L. Dillon, CSE 452, Fll 2008 24

Closures We progrmmg Useful for event hndlers We progrmm function AppendButton(conter, nme, messge) { vr tn = document.creteelement(ʻuttonʼ); tn.nerhtml = nme; tn.onclick = function (evt) { lert(messge); } conter.ppendchild(tn); } Environment poter s the utton s click hndler fd the messge to disply Summr returng functions s results Use closure to mt sttic environment My need to keep ctivtion records fter return Stck (lifo) order fils! Possile stck implementtion Forget out explicit delloction Put ctivtion records on hep Invoke grge collector s needed Not s totlly crzy s is sounds L. Dillon, CSE 452, Fll 2008 25 L. Dillon, CSE 452, Fll 2008 26 Summry of scope issues Block-structured lnguge uses stck of ctivtion records Activtion records cont prmeters, locl vrs, Also poters to enclosg scope Severl different prmeter pssg mechnisms Til clls my e optimized Function prmeters/results require closures Closure environment poter is used on function cll Stck delloction my fil if function is returned from cll Closures re not needed if functions re not nested locks L. Dillon, CSE 452, Fll 2008 27