Lectures Basics in Procedural Programming: Machinery

Similar documents
G Programming Languages - Fall 2012

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Programmiersprachen (Programming Languages)

Functional Programming. Pure Functional Programming

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

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Weeks 6&7: Procedures and Parameter Passing

Programming Languages

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

CSE 307: Principles of Programming Languages

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

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

NOTE: Answer ANY FOUR of the following 6 sections:

CS 314 Principles of Programming Languages. Lecture 13

CS558 Programming Languages

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

Programming Languages Third Edition. Chapter 7 Basic Semantics

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

CS 314 Principles of Programming Languages

Chapter 13: Reference. Why reference Typing Evaluation Store Typings Safety Notes

CS558 Programming Languages

Functional Programming and Haskell

CS558 Programming Languages. Winter 2013 Lecture 3

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Concepts Introduced in Chapter 7

Chapter 5. Names, Bindings, and Scopes

Scope, Functions, and Storage Management

Implementing Subprograms

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

names names identifiers variables subroutines constants

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

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

R13 SET Discuss how producer-consumer problem and Dining philosopher s problem are solved using concurrency in ADA.

Short Notes of CS201

CS558 Programming Languages

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

Reasoning About Imperative Programs. COS 441 Slides 10

Programming Languages and Techniques (CIS120)

CS201 - Introduction to Programming Glossary By

Test 1 Summer 2014 Multiple Choice. Write your answer to the LEFT of each problem. 5 points each 1. Preprocessor macros are associated with: A. C B.

Chapter 5 Names, Binding, Type Checking and Scopes

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

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

CS 415 Midterm Exam Spring 2002

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

G Programming Languages - Fall 2012

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

Attributes, Bindings, and Semantic Functions Declarations, Blocks, Scope, and the Symbol Table Name Resolution and Overloading Allocation, Lifetimes,

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

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

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

Names, Bindings, Scopes

INF 212 ANALYSIS OF PROG. LANGS ELEMENTS OF IMPERATIVE PROGRAMMING STYLE. Instructors: Crista Lopes Copyright Instructors.

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

CSC 533: Organization of Programming Languages. Spring 2005

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

Special Topics: Programming Languages

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

The role of semantic analysis in a compiler

Programming Languages: Lecture 12

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

Run-time Environments - 2

CMSC 330: Organization of Programming Languages

CS 242. Fundamentals. Reading: See last slide

COMP 524 Spring 2018 Midterm Thursday, March 1

Program Abstractions, Language Paradigms. CS152. Chris Pollett. Aug. 27, 2008.

CS 314 Principles of Programming Languages

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

Programming Languages, Summary CSC419; Odelia Schwartz

G Programming Languages - Fall 2012

Intermediate Representations & Symbol Tables

Compiler Construction

LECTURE 18. Control Flow

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

Lecture 15 CIS 341: COMPILERS

Control in Sequential Languages

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

Administration CS 412/413. Advanced Language Support. First-class vs. Second-class. First-class functions. Function Types

CS422 - Programming Language Design

Run-Time Environments

Functions - Lecture 7. Nested functions. Another example. A Nested Function. Josef Svenningsson

Programmin Languages/Variables and Storage

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

CS 415 Midterm Exam Spring SOLUTION

Lecture08: Scope and Lexical Address

Compiler Construction

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

6. Names, Scopes, and Bindings

5. Semantic Analysis!

CS4215 Programming Language Implementation. Martin Henz

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

The Environment Model. Nate Foster Spring 2018

Chapter 13: Reference. Why reference Typing Evaluation Store Typings Safety Notes

n Closed book n You are allowed 5 cheat pages n Practice problems available in Course Materials n Check grades in Rainbow grades

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

Compiler Construction

Outline. Programming Languages 1/16/18 PROGRAMMING LANGUAGE FOUNDATIONS AND HISTORY. Current

Chapter 10. Implementing Subprograms ISBN

CSE 3302 Notes 5: Memory Management

Transcription:

Lectures 3-4-5-6 Basics in Procedural Programming: Machinery February 21-28, 2014 2014-03-01 11:04:07 1/48 Lecture3-6E-2014.pdf (#21)

Basics in Procedural Programming: Machinery Naming and Binding Mutable Values: Denotable, Storable and Expressible Value Env, Store, AR and Blocks: Motivations Blocks: Inline blocks and Procedure/function (body) block Blocks: Static and Dynamic Scope Activation Records: Structure and Implementation Programming Unit Aliasing, Closures, Lambda Lifting Env: Formalization and Implementation Store: Formalization and Implementation 2014-03-01 11:04:08 2/48 Lecture3-6E-2014.pdf (2/48)

Naming and Binding Naming = Use of identifiers to refer to definitions of programming entities Definition of the entity = Definition results a Denotable Value of the language semantic domain Den final double pigreco = 3.15; /*an example of constant*/ int y = 5; /*an example of variable*/ See next slide, for other examples of definitions. Binding = Association between the name and its definition Bindings of a program are all collected in the semantic structure Env 2014-03-01 11:04:08 3/48 Lecture3-6E-2014.pdf (3/48)

Mutable and Immutable values Names for Constants and Variables are in common use in the introduction (i.e. declaration) of values Variable = It is synonym of Mutable Value Mutable values are basics in Imperative Languages but they are non incompatible with Descriptive Languages, in principle However, problems arise from the different ways in which such values can be used in the program: Haskell preserves Transparency Property, instead Ocaml does not. Constant = It is an example of Immutable Value Var x: int /*Pascal Declaration of a mutable value*/ Const y:int /*Pascal Declaration of a immutable value*/ int z[] /*C Declaration of an immutable, structured, value with mutable components of type int */ *int y /*C Declaration of an mutable value that it is, in turn, a mutable value yet, namely a pointer*/ label u /*Pascal Declaration of a immutable value, namely a position in program */ void p(...){...} /*Declaration of an immutable value, namely a procedure*/ public class A{...} /*Declaration of an immutable value, namely a class (of Java)*/ struct S{...} /* Declaration of an immutable value, namely a type record (of C) */ type B =... /*Again, declaration, in OCaml, of an immutable value, namely a (concrete) type*/ 2014-03-01 11:04:08 4/48 Lecture3-6E-2014.pdf (4/48)

Mutable and Immutable values: The Equality Property The two classes of values definitely, differ in some form of state that is underlying of mutable values. This is clearly, reflected from the behavior of (almost all) operations of the two classes, and then, from their use in programming. However, what about comparing two values? The Equality Property = Two values are equals only if they can be exchanged with one another, in the program. Each mutable value is equal only to itself Comment the equality predications in the following C++ text: 2014-03-01 11:04:08 5/48 Lecture3-6E-2014.pdf (5/48)

Mutable and Immutable values: Implementation Skills The two classes of values definitely, differ in some form of state that is underlying of mutable values. This is clearly, reflected from the behavior of (almost all) operations of the two classes, and then, from their use in programming. However, what can we say about the internal representation of such values? Implementation Skills One immuable and one immutable value in two different memory organizations: Memory on the right has a constant pool memory. 2014-03-01 11:04:08 6/48 Lecture3-6E-2014.pdf (6/48)

Den, Mem, Val: The Value Domains of a Language The Value (semantic) Domains are a fundamental characteristic of a language They highly constrain the way in which algorithms may be written, in the language Val = Domain of the Values that can be involved in the language programs Den = D. of the Values that can be expressed in definitions Mem = D. of the Mutable Values of the language The following hold: (1) Den Val; (2) Mem Val But: Den Mem = Val; Mem Val ; and so on... hold or not depending on the language int A[3] = {3,5,17}; /*define,inc,amutablevalueinabindingfora*/ A={3,5,12}; /*isnotpermitted*/ Then: Is {3,5,17} defining a mutable or immutable value? In providing for an answer, compare it with: int B = 3; /* define, in C, a mutable value in a binding for B */ B=15;/*acommonstatementinC*/ Then: 3 and 15 are immutable integers. What can you say about the other languages that you know? 2014-03-01 11:04:08 7/48 Lecture3-6E-2014.pdf (7/48)

Expressible Values Expressible Values = Have an explicit, syntactic, presentation in the language; These values are useful for introducing constant values in expressions; Hence, values of common use in the expressions of the language are also expressible value; Integers are expressible values of C Are arrays expressible values of C? Are lists expressible values of Ocaml (Haskell)? Are vectors expressible values of Java? Are functions expressible values of Ocaml (Haskell)? Are methods expressible values of Java? 2014-03-01 11:04:08 8/48 Lecture3-6E-2014.pdf (8/48)

Env, Store, AR Env = Semantic Structure for collecting the bindings of the program It is quite related to the symbol tables of the front-end of language executors and Compilers Machine Languages do not use naming and do not require Env Store = Semantic Structure for Mutable Values, i.e. Mem Additional Memory components are always present in the implementation machinery, to handle immutable values and the program representation of all languages (including pure Functional ones) AR = Implementation Machinery Component for the computation control It is used to support the program sectioning into parts that: can be executed separately, and, include inline blocks, procedures/functions, modules, monitors, threads,.. 2014-03-01 11:04:08 9/48 Lecture3-6E-2014.pdf (9/48)

Program Sectioning: Blocks Block = It is used for creating sections of program that are: (partially) autonomous in the definitions that may be used, and may exhibit specific functionalities, and may be valid supports in program verification and modification Two main kinds in Procedural Programming: inline blocks procedures and functions 2014-03-01 11:04:08 10/48 Lecture3-6E-2014.pdf (10/48)

Blocks: Inline vs. Procedures Inline Blocks anonymous contain two parts: Local Definitions and End/Exit Code; may be nested: Execution exits nested blocks in reverse order to the entering Procedures and Functions named contain three parts: Parameter Transmission, Local Definitions and Return/Exit Code; (a) According to the above features, describe the features of the blocks of the compound statements of the language C. (b) Moreover, answer to: in what features the inline blocks of Java differ from the ones described in the slide Suggested Reading: Gabrielli M., S. Martini, Programming Languages: Principles and Paradigms, Springer, 2006 - Chapter 4-4.2 2014-03-01 11:04:08 11/48 Lecture3-6E-2014.pdf (11/48)

Blocks: Inline vs. Procedures - Exercises (a) According to the above features, describe the features of the blocks of the compound statements of C. (b) Moreover, answer to: in what features the inline blocks of Java differ from the ones described in the slide (a) Answer. anonymous; contains two parts: 1. Local Definitions: But without procedure/functions 2. Code: Any sequence of statements including jump stms. (break, return, continue, goto) may be nested: Execution exits depend on the Code stms. (b) Answer. anonymous; contains two parts: 1. Local Definitions (including classes, hence methods) 2. Code: Any sequence of statements including jump stms. (break, return, continue, goto) may be nested: Execution exits depend on the Code stms. 2014-03-01 11:04:08 12/48 Lecture3-6E-2014.pdf (12/48)

Blocks: Scope of Identifier definitions Scope. Let I be an identifier defined with the value d in a block A, of a program P, i.e.binding(a,i)=d in P. Then, Scope(I,A) is the set Z of sections of P that must use the value d when they refer to the identifier I: Scope(I,A)={B binding(b,i)=binding(a,i)} Definition of Scope depends from the language; Two kinds of Scope (and correspondingly, two classes of languages): Scope is static (Hence, Languages with static Scope) Scope is dynamic (Hence, Languages with dynamic Scope) 2014-03-01 11:04:08 13/48 Lecture3-6E-2014.pdf (13/48)

Blocks: Static and Dynamic Scope Scope(I,A)={B binding(b,i)=binding(a,i)} Static Scope: S-Scope Z includes A; Z includes also, any block B which is: (defined) within A and it is such that its section Local Definitions does not contain a new definition for I in this case, I is also, called a non-local of B. Dynamic Scope: D-Scope Z includes A; Z includes also, any block B which is: executed during the execution of the Code of A and it is such that its section Local Definitions does not contain a new definition for I in this case, I is also called a non-local of B. 2014-03-01 11:04:09 14/48 Lecture3-6E-2014.pdf (14/48)

Blocks: Static and Dynamic Scope/2 They differ only on the non-locals of procedures and functions Give names to inline blocks by using capital letters, in alphabetic order, from A that is assigned to the outermost, topmost, block; 1 List the block in the program; 2 Compute the function Scope of each defined identifiers; 3 Compute the static, S-Scope, and dynamic, D-Scope, scope of each defined identifiers; 4 Show printed values when static, respectively dynamic, scope is used A:{int x = 0; void pippo(int n){x=n+x;} pippo(3); print(x); printer: 3 3 B:{int x = 0; pippo(3); print(x); printer: 0 3 } print(x); printer: 6 3 } (1) The program blocks are: {A,pippo, B}; (2) Scope(A,x)={A,pippo}; Scope(B,x)={B,pippo}; Scope(pippo,n)={pippo} (3) S-Scope(A,x)={A,pippo}; S-Scope(B,x)={B}; S-Scope(pippo,n)={pippo} D-Scope(A,x)={A,pippo}; D-Scope(B,x)={B,pippo}; D-Scope(pippo,n)={pippo} 2014-03-01 11:04:09 15/48 Lecture3-6E-2014.pdf (15/48)

Static vs. Dynamic Scope: Motivations Two kinds of Scope (and correspondingly, two classes of languages): Scope is static (Almost all languages) Also called, lexical scope (Symbol-Tables of front-ends) The binding of a non-local is localized near to its use The binding of a non-local in a block is the same in all block executions (during each program execution) Allow a better sectioning of the program; Allow a better programming approach (programming methodologies) Implementation is efficient but a bit heavy. Scope is dynamic (Lisp-like languages) Avoid the use of non-locals is recommended in the use of languages with dynamic scope (lambda-lifting). Implementation is not efficient but very easy to do. 2014-03-01 11:04:09 16/48 Lecture3-6E-2014.pdf (16/48)

Blocks: Different Notions In some languages (including Java) inline blocks cannot re-define a non-local variable (i.e.the shadowing of local variables is forbidden) In some languages blocks are not always, enclosed by delimiters (non ANSI C), or declarations may occur everywhere in a block (JavaScripts) {int x = 5;... {int y = 0; x+1;... int x = 10; This declaration may be considered: y = x+y; (a) either, the beginning of a new block, ending at the end of its outer block (non ANSI C) } (b) or, to be moved to the beginning of the block in which it is declared (JavaScript).... } How many blocks here? {int x = 4; {int x = 4; while(x > 0){ while(x > 0){ --x; int x; int x; - -x; print(x); print(x); } }...}...} What is while supposed to compute accor- Provide a re-phrasing in ANSI C of the code and ding to the two readings, (a) and (b) above? show the first 10 printed rows and comment them. 2014-03-01 11:04:09 17/48 Lecture3-6E-2014.pdf (17/48)

Activation Record: Implementation for inline blocks Activation Records: Support the execution of the code of a block (i.e. program section) Support the control transfer among different blocks Have different structure depending on: inline block: Env (called frame) Program Counter (pc) Memory Section for Expression Intermediate Results (ri) Dynamic Chain pointer (cd) 2014-03-01 11:04:09 18/48 Lecture3-6E-2014.pdf (18/48)

Activation Record: Implementation for procedure blocks Activation Records: inline block:... procedure block: Env (called frame) Program Counter (pc) Memory Section for Expression Intermediate Results (ri) Dynamic Chain pointer (cd) Static Chain pointer (cs) only for static scope Return Address (ret) Result Value Address (val) 2014-03-01 11:04:09 19/48 Lecture3-6E-2014.pdf (19/48)

Finding the Right Binding: The Simple Approach Q: How can we finding the right binding of an identifier (during program execution)? A: By using the active AR in a backward visit of the AR frames along: (Static Scope) the Static Chain (cs if procedures / cd if inline) (Dynamic Scope) the Dynamic Chain (cd) and stopping when a binding for the identifier is found. the found binding, if any, is the right binding of the identifier. 2014-03-01 11:04:09 20/48 Lecture3-6E-2014.pdf (20/48)

Finding the Right Binding: Le Blank - Cook Approach The simple approach requires O(n*p) accesses and comparisons (for n-sized frames / p-sized chain lenghts) Le Blank - Cook (1983) is only for Static Scope It reduces the finding cost to O(p) (and by using, display vector to O(1)) It consits in: To each identifier I that is used in a block B it associates a pair [l,p]: l=iscalledstatic Chain Link and is equal to the number of nestings of B w.r. to the block A containing the binding of I. l=0 means the 0-nesting( level)s Noting that, procedure blocks that are declared in a block are considered as nested in such a block. p=iscalledposition and is equal to the position, from the top, in the frame of A (above), of the binding of I. It replaces, identifiers, everywhere are used, with their pair [l,p], above. 2014-03-01 11:04:09 21/48 Lecture3-6E-2014.pdf (21/48)

Le Blank - Cook (1983): s Le Blank - Cook is only for Static Scope It reduces the finding cost to O(p) (and by using, display vector to O(1)) It replaces, identifiers, everywhere are used, with their pair [l,p], above. 2014-03-01 11:04:09 22/48 Lecture3-6E-2014.pdf (22/48)

Le Blank - Cook (1983): s/2 Noting the use of display vectors, in red lines/boxes, in the image on the right side. 2014-03-01 11:04:09 23/48 Lecture3-6E-2014.pdf (23/48)

Programming Units Programming Units = Each Section that is able to completely describe one (or more) functionalities of the algorithm that the program expresses. procedure, function, inline block, module, package, abstraction, class... are candidates for Programming Units but each of them is or not, a P.U. depending on various factors, including the way in which bindings are used We must write a program for an algorithm A which: -First,itreadsasequenceofstudentapplications:Todoitforinstance,A suggests the use of an algorithm N that reads the anagraphic data and a different one, let us say M, thatreadstheacademicdata; -Then,itsortsthecollecteddata:Again,todoit(thealgorithmor)theprogrammer suggests to use of a sorting algorithm B that requires some suitable representation conversion algorithms, let us say N and M ; -Then,...butwestopherethehistory. 2014-03-01 11:04:09 24/48 Lecture3-6E-2014.pdf (24/48)

Programming Units: Apply them We must write a program for an algorithm A which: -First,itreadsasequenceofstudentapplications:Todoitforinstance,A suggests the use of an algorithm N that reads the anagraphic data and a different one, let us say M, thatreadstheacademicdata; -Then,itsortsthecollecteddata:Again,todoit(thealgorithmor)theprogrammer suggests to use of a sorting algorithm B that requires some suitable representation conversion algorithms, let us say N and M ; -Then,...butwestopherethehistory. 2014-03-01 11:04:09 25/48 Lecture3-6E-2014.pdf (25/48)

Sectioning and Scope in C: A Case Study Procedures of C may contain only non-locals that must be globals of the module Q: How can such a constraint be imposed in C? What about its implications in: Q: Programming? A: Procedures are not Programming Units, in C Q: Implementation? A: Static Chain is ever pointing to the module global frame 2014-03-01 11:04:09 26/48 Lecture3-6E-2014.pdf (26/48)

Sectioning and Scope in C: A Case Study/2 Procedures of C may contain non-locals that must be globals of the module Q: How can such a constraint be imposed in C? A: Blocks (included procedures) cannot introduce naming for local procedures What about its implications in: Q: Programming? A: Procedures are not Programming Units, in C Q: Implementation? A: Static Chain is ever pointing to the module global frame Hence: pointer cs can be dropped from AR s of C Hence: pairs [l,p], in the code of C procedures, have l equals to either 0 (for local) or -1 (for global). 2014-03-01 11:04:10 27/48 Lecture3-6E-2014.pdf (27/48)

Env: Aliasing Env: It is at the basis of the mechanism of Naming Naming: It allows Use of name instead of Den(otable) Values Sharing of Den(otable) Values, i.e. Aliasing Aliasing = Different Names for the same Den Value Constructs introducing Aliasing are: Parameter Transmission: By Reference Alias: x = alias y (unix Bash) 2014-03-01 11:04:10 28/48 Lecture3-6E-2014.pdf (28/48)

Lambda Lifting and Dynamic Scope Dynamic Scope = It may be overcome by using LL LL = Technique for the Elimination of the free variables [i.e. non-local parameters] in (functional) L.P. with Dynamic Scope The non-locals become additional parameters of the procedures Transmission of non-locals is By Reference, or by using Pointers (as in the C example, below) 2014-03-01 11:04:10 29/48 Lecture3-6E-2014.pdf (29/48)

Lambda Lifting and Dynamic Scope/2 The C program, on right side, has been obtained by lambda lifting: Hence its procedures do not contain non-locals computes as the program on the left side but according to dynamic scope. Can you give the same but according to static scope? The answer is NO (in C, it cannot be done), but... 2014-03-01 11:04:10 30/48 Lecture3-6E-2014.pdf (30/48)

Lambda Lifting and Dynamic Scope/3 Can you give the same but according to static scope? The answer is NO (in C, it cannot be done), but it can be done in Ocaml, below 2014-03-01 11:04:10 31/48 Lecture3-6E-2014.pdf (31/48)

Closures Closure = Code enclosing its Non-local Bindings Semantic View: pair <code,non-locals bindings> Syntactic View: functions returned as values from functions 2014-03-01 11:04:10 32/48 Lecture3-6E-2014.pdf (32/48)

Env: Formalization and Implementation Formalization. Env = Structure defined by the following operations bind: Ide x Den x Env Env abstract view: bind(i,d,e) = λu. if (u=i) then d else e(u) a function concrete view: bind(i,d,e) = (i,d)::e a pair list find: Ide x Env (Den + Ide) abstract view: find(i,e) =e(i) a function application concrete view: find(i,e) = match e with [] i (u,d j )::er if (u=i) then d else find(i,er) empty: () Env abstract view: empty() = λ u. u identity concrete view: empty() = [] empty list Implementation. A frame similar to the one used in AR 2014-03-01 11:04:10 33/48 Lecture3-6E-2014.pdf (33/48)

Env: Formalization and Implementation in Summary Formalization. Env = Structure defined by the following operations bind: Ide x Den x Env Env find: Ide x Env (Den + Ide) empty: () Env Implementation is a frame similar to the one used in AR Apictorialrepresentationisbelow 2014-03-01 11:04:10 34/48 Lecture3-6E-2014.pdf (34/48)

Store: Formalization and Implementation Formalization. Store = Structure defined by the following operations new: Mem x Store Loc x Store allocation operations are possibly, more than one according to language store features upd: Loc x Mem x Store Store abstract view:... look: Loc x Store Mem abstract view:... Implementation. 3 different main kinds of store: Static: The Standard Store of Machine Languages Stack: The supporting store of block based Programming Languages (with recursive procedures) Dynamic: In almost all today Programming Languages. 2014-03-01 11:04:10 35/48 Lecture3-6E-2014.pdf (35/48)

Stack: Implementation/1 It is built on a section of (contiguous words of) static memory Pointer Start is pointing to the first word of the section Pointer Top is pointing to the Stack Top, (in case of AR Stack) Pointer Access is pointing to the Access Point of the AR in the top. 2014-03-01 11:04:10 36/48 Lecture3-6E-2014.pdf (36/48)

Stack: Implementation/2 The role of the Stack for Intermediate Results in a program for JVM 2014-03-01 11:04:10 37/48 Lecture3-6E-2014.pdf (37/48)

Heap: Implementation It is built on a section of (contiguous words of) static store Two kinds of Heap: Homogeneous Heap: Blocks of one only size Variable Heap: Blocks of different sizes Allocation may reserve only contiguous blocks 2014-03-01 11:04:10 38/48 Lecture3-6E-2014.pdf (38/48)

Heap: Implementation/2 It is built on a section of (contiguous words of) static store Two kinds of Heap: Homogeneous Heap: All blocks have fixed size k (words) Memory is sectioned in blocks of k+1 The additional word is for pointing the next free block Pointer LL is always pointing to the first block that is free for allocation 2014-03-01 11:04:11 39/48 Lecture3-6E-2014.pdf (39/48)

Heap: Fragmentation/1 Two kinds of Heap: Homogeneous Heap: All blocks have fixed size k (words) Variable Heap: (see later on) Fragmentation: A problem in Unused Memory Internal Fragmentation: Cannot be avoided External Fragmentation: Of dramatic relevance 2014-03-01 11:04:11 40/48 Lecture3-6E-2014.pdf (40/48)

Heap: Fragmentation/2 Two kinds of Heap: Homogeneous Heap: All blocks have fixed size k (words) Variable Heap: (see later on) Fragmentation: A problem in Unused Memory Internal Fragmentation: Cannot be avoided External Fragmentation: Of dramatic relevance in both kind of Heaps 2014-03-01 11:04:11 41/48 Lecture3-6E-2014.pdf (41/48)

Heap: Implementation/3 It is built on a section of (contiguous words of) static store Two kinds of Heap: Homogeneous Heap: (see previous slides) Variable Heap: Blocks of different sizes. Two main structures: Single list: e.g. Best Fit allocation Multiple List: Buddy or Fibonacci allocation 2014-03-01 11:04:11 42/48 Lecture3-6E-2014.pdf (42/48)

Heap: Implementation/4 (Best Fit) Best Fit allocation: The Free (Single) List is scanned for finding the minimum size free block larger than the section to be allocated Compaction may merge contiguous blocks into one block 2014-03-01 11:04:11 43/48 Lecture3-6E-2014.pdf (43/48)

Heap: Implementation/5 (Multiple Lists) Multiple (Homo-)lists: [k 1 ],[k 2 ],...,[k n ] = n homo-lists of different sizes k 1 <k 2 <...<k n Section of size k is allocated in list [k i ]suchthat: k i 1 <k<k i Directly: If [k i ]hasafreeblock Through list [k i+j ]: If [k i+j ]hasafreeblockandk i+j is such that: Let p=i+j-1, then (m1,...,mp) exists: m1*k i +...+mp*k p =k i+j One block B of [k i+j ]isthenbrokenin(m1,m2,...,mp)blocks of the right sizes Lists [k i ],...,[k p ]areextendedwiththenewblocks Allocation of Section of size k then applies Block B is re-built once: m1 blocks of list [k i ],..., mp blocks of list [k p ] are become free. Critical is: The choice of the block size in the lists Buddy: k 1 <k 2 <...<k n are (contiguous) powers of 2 Fibonacci: k 1 <k 2 <...<k n are (contiguous) Fibonacci s numbers 2014-03-01 11:04:11 44/48 Lecture3-6E-2014.pdf (44/48)

Heap: Implementation/6 Buddy: k 1 <k 2 <...<k n are (contiguous) powers of 2 Fibonacci: k 1 <k 2 <...<k n are (contiguous) Fibonacci s numbers 2014-03-01 11:04:11 45/48 Lecture3-6E-2014.pdf (45/48)

Heap: Compaction - An Exercise/7 Compaction is an Heap operation for allowing that contiguous, free blocks may be allocated at once. In programming languages with pointers as values, the operation is acting on a rearrangement of the free list and in no case, it moves values that are in use, in a block, into a different block. a. Discuss the reasons of it and show an example that illustrates such points. b. Using the example in (a), discuss the problem in Languages which have not pointers as values b. Discuss an algorithm (or write a program) for the re-arrangement of the free list in a homo-heap Answer (a): Moving memory that contains absolute values does not present any problem. In contrast, when the memory contains relocatable values, to move memory it is needed to know (and bring along, the relocation base). Consider the code below and suppose that x is allocated in a block X and Y is allocated in a block Y.... x = malloc(k*sizeof(int));...... y = malloc(k*sizeof(int));...... z = x > y?x:y;... What is the value of z if X and Y are relocated immediately after the assignment of z? Can You say that the relocation of X and Y is not affecting the behavior of the program? 2014-03-01 11:04:11 46/48 Lecture3-6E-2014.pdf (46/48)

Comparison Table Environment and Memory in: Fortran, C, Pascal, Caml, Java, Prolog 2014-03-01 11:04:11 47/48 Lecture3-6E-2014.pdf (47/48)

Things To Do STUDY in deeper way, the content of the slides. You can read in addition, the chapters 4 and 5 of the book by Gabrielli and Martini before mentioned, and use the bibliographic references in the chapter for additional readings. PROGRAMMING check the installation of the tools for the use of following languages: C, Java, Caml, Prolog (Use the first 3 in giving a program for the computation of factorial) EXERCISES: verify, complete and extend all the examples and/or exercises included in the slides by using the right tools (programs and languages) consider the exercises included in the chapters 4 and 5 of the book by Gabrielli and Martini. 2014-03-01 11:04:11 48/48 Lecture3-6E-2014.pdf (48/48)