Procedure and Object- Oriented Abstraction

Similar documents
Scope, Functions, and Storage Management

The Procedure Abstraction

CSE 504: Compiler Design. Runtime Environments

Run-time Environments

Run-time Environments

CS 480 Fall Runtime Environments. Mike Lam, Professor. (a.k.a. procedure calls and heap management)

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

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

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

System Software Assignment 1 Runtime Support for Procedures

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

Weeks 6&7: Procedures and Parameter Passing

Naming in OOLs and Storage Layout Comp 412

CA Compiler Construction

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

Run-time Environments - 2

What about Object-Oriented Languages?

G Programming Languages - Fall 2012

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

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

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

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

LECTURE 19. Subroutines and Parameter Passing

INF3110 Programming Languages Runtime Organization part II

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

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

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

CSE 307: Principles of Programming Languages

Run-time Environment

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

Runtime Environments I. Basilio B. Fraguela

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

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

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

THEORY OF COMPILATION

2. Reachability in garbage collection is just an approximation of garbage.

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

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

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

Concepts Introduced in Chapter 7

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

Chapter 9 :: Subroutines and Control Abstraction

G53CMP: Lecture 14. Run-Time Organisation I. Henrik Nilsson. University of Nottingham, UK. G53CMP: Lecture 14 p.1/37

C++ Yanyan SHEN. slide 1

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

Object typing and subtypes

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

This Lecture. G53CMP: Lecture 14 Run-Time Organisation I. Example: Lifetime (1) Storage Areas

Final CSE 131B Spring 2004

CS 314 Principles of Programming Languages. Lecture 13

Implementing Subprograms

Object Oriented Languages. Hwansoo Han

CS 314 Principles of Programming Languages

Names, Scopes, and Bindings II. Hwansoo Han

Run-time Environments - 3

Subtyping (Dynamic Polymorphism)

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

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

Optimizer. Defining and preserving the meaning of the program

Names, Scope, and Bindings

The Procedure Abstraction Part I: Basics

Runtime Support for Algol-Like Languages Comp 412

The role of semantic analysis in a compiler

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

CIT Week13 Lecture

CSE 504: Compilers. Expressions. R. Sekar

CS558 Programming Languages

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1

Runtime management. CS Compiler Design. The procedure abstraction. The procedure abstraction. Runtime management. V.

Names, Scope, and Bindings

Intermediate Representations & Symbol Tables

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

The Procedure Abstraction Part I Basics

Control Abstraction. Hwansoo Han

Inheritance, Polymorphism and the Object Memory Model

The compilation process is driven by the syntactic structure of the program as discovered by the parser

Implementing Subprograms

CS 314 Principles of Programming Languages

CS 406/534 Compiler Construction Intermediate Representation and Procedure Abstraction

Implementing Subprograms

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

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

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

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

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

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

Topic 7: Activation Records

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Semantic Analysis and Type Checking

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

CSCI312 Principles of Programming Languages!

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

CSE 307: Principles of Programming Languages

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

Special Topics: Programming Languages

Programming Languages

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

Transcription:

Procedure and Object- Oriented Abstraction Scope and storage management cs5363 1

Procedure abstractions Procedures are fundamental programming abstractions They are used to support dynamically nested blocks Paired function call and return jumps They have standalone semantics defined by an abstraction interface input parameters, return values, global side effects Procedures are units of separate compilation They represent parameterized blocks of computation int main(int argc, char* argv[]) { float a; a = foo( ) } float foo( ) { int a, b, c;. r = bar( ); return r; } float bar( ) { float r;. return r; } cs5363 2

Scoping rules Global and local variables program main(input,output); outer block x 0 var x : integer; function g(z: integer) :integer; begin g := x+z end; h(3) z 3 function h(z: integer) :integer; x 1 var x : integer; begin x := 1; h:=g(z) end; g(12) z 3 begin x := 0; print(h(3)) end Static scoping Find global variables in enclosing blocks in program text Dynamic scoping Find global variables in the most recently evaluated blocks Easier to implement in interpreted languages What is the scoping rule for C/C++, Java? cs5363 3

Simplified memory model Runtime stack: activation records of blocks/functions Block entry: add new data to stack Block exit: remove outdated data Heap: data of varying lifetime Variables that last throughout the program Address may be contained by variables on the runtime stack Code Data Stack Program Counter Activation record pointer(rarp) Heap/ static data cs5363 4

Managing Data Storage Local variables --- activation records on stack Declared inside a block (e.g. function body) Enter block: allocate space Exit block: de-allocate space Local variables in an enclosing block Already allocated before entering current Block Remain allocated after exiting current block Function parameters and return value Allocated and initialized before entering function body Formal parameters dallocated after exiting function body Global/static variables --- static data areas Allocated when program is loaded to memory Storage remain until program exits Dynamically allocated variables --- heap Storage dynamically allocated at runtime (e.g., malloc in C) Storage remain until explicitly de-allocated or garbage collected cs5363 5

Activation Record Allocate storage for each block dynamically Allocate an activation record before evaluating each block Storage for each local variable determined as compile time Values of local variables evaluated at runtime Delete the activation record after block exits { int x=0; int y=x+1; { int z=(x+y)*(x-y); }; }; Allocate AR with space for x, y Set values of x, y Allocate AR for inner block Set value of z Delete AR for inner block Delete AR for outer block May need space for intermediate results such as (x+y), (x-y) cs5363 6

Activation Records For Inline Blocks { int x=0; }; int y=x+1; { int z=(x+y)*(x-y); }; Push activation record on stack Set caller ARP to rarp Set rarp to new AR Pop activation record off stack Reset rarp to caller s ARP When making function calls Caller must also set return address, return value addr, saved registers, and parameters Caller s ARP x y x+y x-y Access link cs5363 7 0 1 Caller s ARP Access link z -1 1-1 Activation record pointer(rarp)

Activation Records For Procedures Caller s ARP Access link Return address Return-result addr Register save area Parameters Local variables Activation record pointer(rarp) Access link Pointer to activation record of the enclosing block Return address Pointer to the instruction immediately following function call Return-result address Address of the storage to put the result to be returned Register save area Save register values before function call Restore register values before return Parameters Storage for function parameters Values initialized by caller cs5363 8

Linkage Convention: Implementing Function Calls Procedure p prologue precall postreturn epilogue call return Procedure q prologue epilogue Linkage convention: programs in different files must follow a single contract of function call implementation Precall Push callee s AR (increment rarp) Set caller s ARP Set return address Set return result addr Save live register values initialize formal parameters Postreturn Restore live register values Pop callee s AR(decrement rarp) Prologue Initialize local variables Load local environment (access link) Epilogue Deallocate local variables Goto return address cs5363 9

Parameter Passing Formal and actual parameters Parameter declarations and initializations Pass-by-value Formal parameters contain values of actual parameters Callee cannot change values of actual parameters Pass-by-reference Formal parameters contain locations of actual parameters Callee can change values of actual parameters Formal parameters in activation record may be aliased Aliasing: two names refer to same location What about pass-by-pointer (in C)? Formal parameter int f (int x) { x := x+1; return x; }; main() { int y = 0; print f(y)+y; } Actual parameter cs5363 10

Example: What is the final result? pseudo-code int f (int x) { x := x+1; return x; }; main() { } int y = 0; print f(y)+y; pass-by-ref =>2 pass-by-value =>1 Draw the activation records for the evaluation What parameter passing is supported by the languages you know? cs5363 11

Exercise: Managing Function Calls 1: program main(input,output) 2: var x : integar; 3: function f(y : integer) 4: begin 5: f = (x + y) - 2 6: end 7: function g(function h(b:integer):integer) 8: begin 9: var x : integer; 10: x := 7; 11: g = h(x); 12: end 13: begin 14: x := 5; 15: g(f); 16: end cs5363 12

Accessing Variables In Memory Each memory store has an address Base address: the starting address of a data area Local variables of current block activation record pointer (rarp) Offset: the number of bytes after the base address Local variables of current block predetermined at compile time Address of variable base address + offset Accessing local variable a: LoadAI rarp, @a => r1 loadi @a => r1 loada0 rarp, r1 => r2 loadi @a => r1 Add rarp, r1 => r2 load r2 => r3 cs5363 13

Accessing Global/Static Variables Allocated separately in static data area Base address unknown until program is loaded into memory Use symbolic labels to substitute at compile time Symbolic labels replaced with runtime value by assembler and loader Offset calculated at compile time Individual variables: offset=0 Group of data layout pre-determined Accessing global variable fee: LoadI &fee => r1 Load r1 => r2 Accessing foo.a: LoadI &foo => r1 LoadAI r1, @foo_a => r3 LoadI &foo => r1 Add r1, @foo_a => r2 Load r2 => r3 cs5363 14

Variables of Enclosing Blocks int x=1; int g(int z) { return x+z; } int h(int z) { print h(3); int x = 1; return g(z); } Outer block Use access link to find AR of an enclosing block (static scoping) Access link is always set to frame of closest enclosing lexical block h(3) g(3) control link access link x 1 h(3) control link access link z 3 x 1 control link access link z 3 cs5363 15

Coordinates of Variables Accessing local variables Offset calculated at compile time Need to find the base address The AR that contains the variable Lexical level of a block Number of enclosing scopes g: 1; h: 1; outer-block: 0 For each variable x Coordinate of x is <n, o>, where n: lexical level of block that defines x O: offset of x in it s AR If a block at lexical level m references x Follow access link m-n times to find the base address for x int x=1; int g(int z) { return x+z; } int h(int z) { print h(3); int x = 1; return g(z); } Coordinate for x: <0,8> Lexical level of g: 1 Load instructions: loadai rarp, 4 => r1 loadai r1, 8 => r2 cs5363 16

Global Display Allocate a global array (global display) hold the address of most recent ARs at each lexical level When pushing a new AR, save the previous AR at the same lexical level, modify global display When popping an AR, restore the global display with saved AR at the current lexical level To access variable <n,o> use the ARP at element n of the global display Display level0 level1 level2 level3 Runtime stack Level0 AR Level1 AR Level2 AR Level3 AR cs5363 17

Global Display vs. Access Links Maintenance Constant cost for global display When entering every block at lexical level n, save the level-n ARP from global display, replace it with new ARP When exiting the block, restore the old level-n ARP into display Varying cost for access links If a level-m block invokes a level-n block m==n 1 callee s access link points to caller s AR m==n callee s access link = caller s access link m > n callee s access link = caller s level (n-1) access link Referencing variables in enclosing scope Constant cost through global display Varying cost through access links The tradeoff depends on the ratio of non-local references If ARs can outlive their invocation, access link must be used The chosen approach by functional programming languages cs5363 18

Managing memory Registers Data need to be loaded to registers before being operated on If a variable can be kept in register throughout its lifetime, it does not need a storage Register-to-register model Try to keep as many variables in registers as possible Allocate memory storage later if not enough register Alignment and padding Target machines may restrain where data can be stored Needs to be at 32/64 bit boundaries, etc. Cache and variable layout Data in memory can be loaded into cache and reused Managing the heap: dynamically allocate/free storage cs5363 19

Object-Orientation Abstraction: information hiding Separate interface and implementation details Function and data abstractions Object-oriented programming Organize concepts into objects and classes Build extensible systems Language concepts Encapsulation (access control): members can be private only a few functions can access private data Dynamic lookup definitions of functions (function pointers) Object behavior can change dynamically Subtyping polymorphism (relations between types) Operations can be applied to multiple types of values Inheritance (reuse of implementation) Subclasses can modify and inherit behavior of base classes cs5363 20

Static vs. dynamic lookup What about operator overloading (ad hoc polymorphism)? int add(int x, int y) { return x + y; } float add(float x, float y) { return x + y; } Static lookup: overloading is resolved at compile time Examples: C++ non-virtual functions, Java static functions Dynamic lookup: resolved at run time C++ virtual functions, Java non-static functions Difference: flexibility vs. efficiency class vehicle { protected: double speed, fuel; public: virtual void run() = 0; }; class car : public vehicle { public: virtual void run() { if (fuel > 0) fuel = fuel 1;} }; vehicle* a = new car; a->run(); cs5363 21

Static Binding of Methods C++ class: non-virtual member functions Essentially global functions with an extra object pointer parameter class vehicle { protected: double speed, fuel; public: vehicle() : speed(0),fuel(0) {} void start(double x) {speed = x;} }; vehicle* a = new vehicle; a->start(5); Java/C++: Static Methods/Variables Essentially global functions/variables in a name space A single instance of member for all class objects class vehicle { static protected double speed, fuel; public static void start(double x) {speed = x;} }; Vehicle::start(3.0); cs5363 22

Implementing Dynamic Objects An object consists of Hidden data instance variables, also called member data hidden functions also possible Public operations methods or member functions can also have public variables in some languages Dynamic binding Put all the name-value bindings into a table Table can be changed, just like the activation record of a function Example: the vehecle/car objects Object-oriented program: Send messages to objects hidden data msg1 method1...... msgn methodn cs5363 23

C++: Object Layout and Single Inheritance class A { int x; public: virtual int f() { return x;} }; Object a of type A vptr x 3 class A vtable: f Code for A::f class B : public A { int y; public: virtual int f() { return y; } virtual void f2() { } }; Object b of type B vptr x 3 y 5 class B vtable: f f2 b used as an object of A Code for B::f Code for B::f2 cs5363 24

Looking up methods Point object Point vtable Code for move vptr x 3 ColorPoint object ColorPoint vtable Code for move vptr x c 5 blue Code for darken Data at same offset Function pointers at same offset Point p = new Pt(3); p->move(2); // (*(p->vptr[0]))(p,2) cs5363 25

C++ method lookup C++ compiler knows all the base classes Offset of data and function pointer are same in subclass and base class Offset of data and function pointer known at compile time Code p->move(x) compiles to equivalent of (*(p->vptr[move_offset]))(p,x) cs5363 26

Exercise: OO Memory Layout Draw the memory layout for the following C++ code immediately before the main function returns. class A { int x; public: virtual void f(); }; class B: public A { int y; public: virtual void f(); }; class C: public B { int z; public: virtual void g(); }; int main() { C *pc = new C; B *pb = pc; A *pa = pc; } cs5363 27