CS421 COMPILERS AND INTERPRETERS. the point it was passed (line 11). explicit vs implicit memory de-allocation? (malloc-free vs. garbage collection)

Similar documents
More on Runtime Environments. Procedure Parameters (in Pascal) Traditional Stack Scheme. Restrictions in C & Pascal. code.

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

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

Concepts Introduced in Chapter 7

Scope, Functions, and Storage Management

The Volcano Optimizer Generator: Extensibility and Efficient Search

CS 153 Design of Operating Systems

CS 153 Design of Operating Systems

Weeks 6&7: Procedures and Parameter Passing

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

Control in Sequential Languages

EXAMINATIONS 2010 END OF YEAR NWEN 242 COMPUTER ORGANIZATION

Topic 7: Activation Records

Names, Bindings, Scopes

More eamples with half half(0) handle Zero => 1000; > val it = 1000 : int half(1) handle Zero => 1000; > ncaght eception Odd half(0) handle Zero => 10

G Programming Languages - Fall 2012

Run-time Environments

Run-time Environments

Hardware Design Tips. Outline

CA Compiler Construction

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

CMSC 330: Organization of Programming Languages. Memory Management and Garbage Collection

CS 153 Design of Operating Systems Spring 18

CS 153 Design of Operating Systems Spring 18

CS 153 Design of Operating Systems

CAPL Scripting Quickstart

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

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

CS 153 Design of Operating Systems Spring 18

Compiler Construction

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

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

Minimum Spanning Trees Outline: MST

Compiler Construction

CMSC 330: Organization of Programming Languages

CS558 Programming Languages

LECTURE 16. Functional Programming

C Puzzles The course that gives CMU its Zip! Integer Arithmetic Operations Jan. 25, Unsigned Addition. Visualizing Integer Addition

Programming Languages

Automatic Memory Management

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

Storage. Outline. Variables and Updating. Composite Variables. Storables Lifetime : Programming Languages. Course slides - Storage

System Software Assignment 1 Runtime Support for Procedures

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

CS558 Programming Languages

CS 153 Design of Operating Systems

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

CS 415 Midterm Exam Spring 2002

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

Lecture 13: Complex Types and Garbage Collection

CS558 Programming Languages

CSC 533: Organization of Programming Languages. Spring 2005

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

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

CS 153 Design of Operating Systems Spring 18

CMSC 330: Organization of Programming Languages

Page # CISC360. Integers Sep 11, Encoding Integers Unsigned. Encoding Example (Cont.) Topics. Twoʼs Complement. Sign Bit

One-Slide Summary. Lecture Outine. Automatic Memory Management #1. Why Automatic Memory Management? Garbage Collection.

Lecture 15 Garbage Collection

Putting the dynamic into software security testing

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

Lecture 13: Garbage Collection

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

Run-Time Environments

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

Scope. Chapter Ten Modern Programming Languages 1

Dr Paolo Guagliardo. Fall 2018

An Introduction to GPU Computing. Aaron Coutino MFCF

6. Names, Scopes, and Bindings

Run-time Environment

Chapter 6: Pipelining

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

Names, Scopes, and Bindings II. Hwansoo Han

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

The single-cycle design from last time

Bits, Bytes, and Integers. Bits, Bytes, and Integers. The Decimal System and Bases. Everything is bits. Converting from Decimal to Binary

Requirements Engineering. Objectives. System requirements. Types of requirements. FAQS about requirements. Requirements problems

The Procedure Abstraction Part I Basics

C Puzzles! Taken from old exams. Integers Sep 3, Encoding Integers Unsigned. Encoding Example (Cont.) The course that gives CMU its Zip!

Chapter 5. Names, Bindings, and Scopes

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

Answer: Early binding generally leads to greater efficiency (compilation approach) Late binding general leads to greater flexibility

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

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

Static Program Analysis Part 9 pointer analysis. Anders Møller & Michael I. Schwartzbach Computer Science, Aarhus University

PART I: Adding Instructions to the Datapath. (2 nd Edition):

The Procedure Abstraction

TDT4255 Friday the 21st of October. Real world examples of pipelining? How does pipelining influence instruction

Names and Abstractions: What s in a Name?

Memory Management. Memory Management... Memory Management... Interface to Dynamic allocation

An Introduction to Scheme

The role of semantic analysis in a compiler

Compilers and Code Optimization EDOARDO FUSELLA

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

EEC 483 Computer Organization. Branch (Control) Hazards

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

Compiler Construction Lent Term 2015 Lectures 10, 11 (of 16)

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

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

Control in Sequential Languages

Transcription:

rocedre arameters (in ascal) rocedre parameters permit procedres to be invoked ot-of-scope ; 1 program main(inpt, otpt); 2 3 procedre b(fnction h(n : integer): integer); 4 var m : integer; 5 begin m := 6; writeln(h(2)) ; 6 7 procedre c; 8 var m : integer; 9 fnction f(n: integer): integer; 10 begin f := m + n ; 11 begin m := 0; b(f) ; 12 begin c. Qestion: how to get the correct environment when calling h inside b? Soltion: mst pass static link along with f as if it had been called at the point it was passed (line 11). Traditional Stack Scheme parameters and retrned vales STACK (activation records) program conter links and saved stats code temporaries and local data Activation Record HEA (dynamic data) garbage collector STATIC (code and globals) REGISTERS Memory Layot More on Rntime Environments How to efficiently implement procedre call and retrn in the presence of higher-order fnctions? what are higher-order fnctions? how to ext frames to spport higher-order fnctions? efficiency isses (exection time, space sage)? How to efficiently spport memory allocation and de-allocation? what are the data representations? what are the memory layot? explicit vs implicit memory de-allocation? (malloc-free vs. garbage collection) Restrictions in C & ascal C does not allow nested procedres --- names in C are either local to some procedre or are global and visible in all procedres. rocedres in C can be passed as argments or retrned as reslts. ascal (or Modla-2, Modla-3, Algol ) allows procedre declarations to be nested, bt procedre parameters are of restricted se, and procedres cannot be retrned as reslt. Fnctional langages (e.g. ML, Haskell, Scheme, Lisp) spport higherorder fnctions --- spporting both nested procedres and procedres passed as parameters or retrned as reslts. spporting it is a big challenge to the compiler writers!

rocedre Activations (cont d) ested Fnctions in ML val BIG = big() v=big w=x=y=0 access links Q, fn (v,w,x,y) = val = hd(v) R control links... (v,,,y)... v=v,w= x=,y=y Q, in... Q()... val reslt = (BIG,0,0,0) $ Higher-Order Fnctions (cont d) Q lost track of its environment fn (v,w,x,y) = val = hd(v)... (,w+x+y+3)... Q val S = (BIG,0,0,0) & access links # % rocedre Activations ested Fnctions in ML val BIG = big() v=big w=x=y=0 fn (v,w,x,y) = val = hd(v) Q, control links R... (v,,,y)... in... Q()... val reslt = (BIG,0,0,0) Higher-Order Fnctions How to create a closre for Q? v=big w=x=y=0 fn (v,w,x,y) = val = hd(v)... (,w+x+y+3)... Q val S = (BIG,0,0,0)

Higher-Order Fnctions (cont d) Q s environment is in the! fn (v,w,x,y) = val = hd(v) val S = (BIG,0,0,0) ested Higher-Order Fnctions fn (v,w,x,y) = val = hd(v) Qor S R val S = (BIG,0,0,0) val reslt = T() Higher-Order Fnctions (cont d) Q mst copy the frame! v=big w=x=y=0 fn (v,w,x,y) = val = hd(v)... (,w+x+y+3)... val S = (BIG,0,0,0) Applying Higher-Order Fnctions Accessing the Closre Q! Qor S fn (v,w,x,y) = val = hd(v) R val S = (BIG,0,0,0)

R or T R w x y copying!!! Linked Closres : O( 2 ) R -1 Flat Closres : O() R w x y $ Flat Closres fn (v,w,x,y) = val = hd(v) val S = (BIG,0,0,0) val reslt = T() Slow creation, Fast access! Space Usage Space Leaks for Linked Closres fn (v,w,x,y) = val = hd(v) (,w+x+y+3) fn loop (n,res) = if n<1 then res else ( val S = (big(),0,0,0) in loop(n-1,t::res) ) val reslt = loop(,[]) Linked Closres fn (v,w,x,y) = val = hd(v) R or T R val S = (BIG,0,0,0) val reslt = T() Fast creation, Slow access! Better Representations? Closres cannot point to frame (different life time, so yo mst copy.) Linked closres --- fast creation, slow access Flat closres --- slow creation, fast access Stack frames with access links are similar to linked closres (accessing non-local variables is slow.) GOAL : We need good closre representations that have both fast access and fast creation! #

Better Space Usage? The safe for space complexity rle : Local variable mst be assmed dead after its last se within its scope! Stacks and linked closres are OT safe for space Flat closres are safe for space SML/J : nsafe version = (2 to 80) x safe version & Efficient Heap-based Compilation An efficient -based scheme has the following advantages: very good space sage (safe for space complexity!) very fast closre creation and closre access closres can be shared with activation records fast call/cc and fast generational GC simple implementation % Space Usage (cont d) -1 Q,v,w n= Space Leaks for Stack Allocations Q,v,w n=-1 fn (x) =... -1-2 -2-3 Q,v,w n=-2 Use O( 2 ) Space! fn Q(n) = val = big(n) val v = () val w = hd() in if n > 0 then Q(n-1)+v(w) else... val reslt = Q() is dead after this call! Drawbacks of Stack Allocation inefficient space sage slow access to non-local variables expensive copying between and (activation records cannot be shared by closres) scanning roots is expensive in generational GC very slow first-class continations (call/cc) correct implementation is complicated and messy

Safely Linked Closres THE TRICK: Safe for Space : se O() space Variables w,x,y have same life time! R fn (v,w,x,y) = val = hd(v) (,w+x+y+3) w x y Q v fn loop (n,res) = if n<1 then res else ( val S = (big(),0,0,0) in loop(n-1,t::res) ) -1 val reslt = loop(,[]) Good Use of Registers To avoid memory traffic, modern compilers often pass argments, retrn reslts, and allocate local variables in machine registers. Typical parameter-passing convention on modern machines: the first k argments ( k = 4 or 6) of a fnction are passed in registers R p,..., R p+k-1, the rest are passed on the. roblem : extra memory traffic cased by passing args. in registers fnction g(x : int, y : int, z :int) : int = x*y*z fnction f(x : int, y : int, z : int) = val a := g(z+3, y+3, x+4) in a*x+y+z Sppose fnction f and g pass their argments 1, R 2, R 3 ; then f mst save R 1, R 2, and R 3 to the memory before calling g, re Heap-based Scheme Ideas: no rntime! program conter safely linked closres good se of registers code HEA (dynamic data) (activation records) garbage collector STATIC (code and globals) REGISTERS Memory Layot Safely Linked Closres (cont d) THE TRICK: Shorter Access ath! Variables w,x,y have same life time! S R w x y fn (v,w,x,y) = val = hd(v) fn S() = w+x+y+3 in (S,) Q v val T = (big(),0,0,0) -1 The nmber of links traversed is at most 1.

Closres egisters? o! pred is an escaping fnction! Modle FOO: (in file foo.sml ) Its closre mst be bilt on the! fn pred(x) =...v(w,x)... val reslt = BAR.filter(pred,...) Modle BAR : (in file bar.sml ) Escaping fnctions: fnctions whose call sites are not all known at compile time! fn filter(p,l) = fn h(s,z) = if (s=[]) then rev z else ( val a = car s val r = cdr s in if p a then h(r,a::z) else h(r,z) ) in h(l,[]) $ Lambda Lifting fn filter(p,l) = r 0 r 1 r 2 r 3 s z rev p h s=[] fn h(s,z,rev,p) = if (s=[]) then rev z else ( val a = car s val r = cdr s in if p a then h(r,a::z,rev,p) else h(r,z,rev,p) ) p a in h(l,[],rev,p) rev z known fnctions can be rewritten into fnctions that are flly closed! (i.e. with no free variables!) & Good Use of Registers (cont d) how to avoid extra memory traffic? Leaf procedres (or fnctions) are procedres that do not call other procedres; e.g, the fnction exchange. The parameters of leaf procedres can be allocated in registers withot casing any extra memory traffic. Use global register allocation, different fnctions se different set of registers to pass their argments. Use register windows (as on SARC) --- each fnction invocation can allocate a fresh set of registers. Allocate closres in registers or se callee-save registers When all fails --- save to the frame or to the. # Closres egisters? es! Known fnctions: fn filter(p,l) = fnctions whose call sites are all known at compile time! h fn h(s,z) = if (s=[]) then rev z else ( val a = car s val r = cdr s in if p a then h(r,a::z) else h(r,z) ) s=[] in h(l,[]) p a h is a known fnction! Its closre can be pt in registers! (e.g., {rev,p}) rev z %

Callee-save Registers callee-save registers general registers Convention : r 4 r 5 r 6 r 0 r 1 r 2 r 3 Reserve k special registers! A B C g v w f A B C Every fnction promises to always preservethese registers! w g g v g Example : k=3 (r 4,r 5,r 6 ) x w x g x w g y A B C f retrn fn f(g,,v,w) = val x =g(,v) val y = g(x,w) in x+y+w Smmary : A Uniform Soltion Take advantage of variable life time and compiime control flow information! Spilled activation records are also thoght as closres! no rntime ---------- everything is sharable all se safely-linked closres ---------- to maximize sharing pass argments and retrn reslts in registers allocating most closres in registers good se of callee-save registers Spilled Activation Records We do not know how p treats the registers! r 0 r 1 r 2 r 3 r 4 r 5 s z rev p h s=[] r 0 r 1 r 2 r 3 r 4 r 5 a::z r z rev p a p a r 0 r 1 r 2 r 3 r 4 r 5 r rev p a r 0 r 1 r 2 r 3 r 4 r 5 r z rev p a Mst save and load everything here! rev z Callee-save Registers (cont d) A B C D E F r 0 r 1 r 2 r 3 s z rev p 6 callee-save registers : r 4,r 5,r 6,r 7,r 8,r 9 A B C D E F h s z rev p recover everything! s=[] a::z r z rev p a p a r rev p a r z rev p a no need to save and load anymore! rev z