Procedure Abstraction VI Run-time structures for OOLs ( II ) and Wrap-up Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.

Slides:



Advertisements
Similar presentations
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Advertisements

Lecture 10: Part 1: OO Issues CS 540 George Mason University.
Dynamic Compilation in an OOL: The Deutsch-Schiffman Smalltalk-80 Implementation “Efficient Implementation of the Smalltalk-80 System” L. Peter Deutsch.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
Chapter 12: Support for Object-Oriented Programming
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Code Shape IV Procedure Calls & Dispatch Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
Intermediate Representations Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
CS 536 Spring Run-time organization Lecture 19.
The Procedure Abstraction Part III: Allocating Storage & Establishing Addressability Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
The Procedure Abstraction Part IV: Run-time Structures for OOLs Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
The Procedure Abstraction Part I: Basics Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
Run time vs. Compile time
Run-time Environment and Program Organization
Introduction to Code Generation Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
The Procedure Abstraction Part II: Symbol Tables, Storage Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled.
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
CS 355 – PROGRAMMING LANGUAGES Dr. X. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 12 Topics Introduction Object-Oriented Programming.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
OOP Languages: Java vs C++
Programming Languages and Paradigms Object-Oriented Programming.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
The Procedure Abstraction Part I: Basics Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
Adapted from Prof. Necula UCB CS 1641 Overview of COOL ICOM 4029 Lecture 2 ICOM 4029 Fall 2008.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Compiler Construction
The Procedure Abstraction, Part VI: Inheritance in OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
CSSE501 Object-Oriented Development. Chapter 12: Implications of Substitution  In this chapter we will investigate some of the implications of the principle.
The Procedure Abstraction, Part V: Support for OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 9.
1 CIS 461 Compiler Design and Construction Winter 2012 Lecture-Module #16 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon.
COMP3190: Principle of Programming Languages
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Introduction to Code Generation Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Chapter 12 Support for Object-Oriented Programming.
Object-Oriented Programming Chapter Chapter
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
ISBN Object-Oriented Programming Chapter Chapter
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
The Procedure Abstraction Part III: Symbol Tables, Storage Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
ISBN Chapter 12 Support for Object-Oriented Programming.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Design issues for Object-Oriented Languages
Name Spaces: ALL versus OOL
Names and Attributes Names are a key programming language feature
The Procedure Abstraction Part III: Symbol Tables, Storage
The Procedure Abstraction Part IV: Allocating Storage & Establishing Addressability Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights.
Inheritance, Polymorphism and the Object Memory Model
Names, Binding, and Scope
Introduction to Code Generation
The Procedure Abstraction Part V: Run-time Structures for OOLs
The Procedure Abstraction Part I: Basics
Code Shape IV Procedure Calls & Dispatch
Binding Times Binding is an association between two things Examples:
Support for Object-Oriented Programming
Lecture 10 Concepts of Programming Languages
ICOM 4029 Fall 2003 Lecture 2 (Adapted from Prof. Necula UCB CS 164)
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Presentation transcript:

Procedure Abstraction VI Run-time structures for OOLs ( II ) and Wrap-up Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit permission to make copies of these materials for their personal use.

Implementing Object-Oriented Languages Mapping message names to methods Static mapping, known at compile-time ( Java, C++ )  Fixed offsets & indirect calls Dynamic mapping, unknown until run-time ( Smalltalk )  Look up name in class’ table of methods Want uniform placement of standard services ( NEW, PRINT, …) This is really a data-structures problem Build a table of function pointers Use a standard invocation sequence

Implementing Object-Oriented Languages With static, compile-time mapped classes fee() fie() foe() fum() Class A ijk z ijk y ijk x band() bend() bind() bond() Class B abc p abc n abc m Message dispatch becomes an indirect call through a function table

Single Inheritance and Dynamic Dispatch Use prefixing of tables Class Point { int x, y; public void draw(); public void d2o(); } Class ColorPoint extends Point { Color c; public void draw(); public void rev(); } x y table draw d2o draw d2o table rev Point: draw ColorPoint:draw ColorPoint: rev Point: d20 self x y c

The Inheritance Hierarchy To simplify object creation, we allow a class to inherit methods from an ancestor, or superclass. The descendant class is called the subclass of its ancestor. The Concept: Principle B subclass A  d  B can be used wherever e  A is expected  B may override a method definition from A Subclass provides all the interfaces of superclass, fee()fie() x a x b fee()foe() fum()fie() x c yx d yx e z Three Two One Method table is an extension of table from One

The Inheritance Hierarchy Two distinct philosophies Impact on name space Method can see instance variables of self, class, & superclasses Many different levels where a value can reside In essence, OOL differs from ALL in the shape of its name space AND in the mechanism used to bind names to implementations Static class structure Can map name to code at compile time Leads to 1-level jump vector Copy superclass methods Fixed offsets & indirect calls Less flexible & expressive Dynamic class structure Cannot map name to code at compile time Multiple jump vector ( 1 /class) Must search for method Run-time lookups caching Much more expensive to run

Dynamic versus Static Dispatch When must the compiler generate indirect calls for M? If object O’s type is known at compile time and all of its methods can be resolved at compile time, compiler can generate a direct call. If object O’s type is not known — for example, due to subtyping — then run-time resolution may be necessary  Use function-table pointer from O’s object record  Generate an indirect call at the “right offset” Compiler can generate efficient tables by instantiating function pointer(s) at each class

Dynamic versus Static Dispatch Does the run-time system ever need to resolve method names at run-time? If the class hierarchy can change at run-time  Either rebuild compact function tables at change, or  Keep the whole hierarchy intact & use caching Smalltalk had this problem feature Run-time name resolution Extensive caching of results ( inline method caches ) See Deutsch & Schiffman, “Efficient Implementation of the Smalltalk-80 System”, Conference Record of the 10th ACM SIGACT-SIGPLAN Symposium on Principles of Programming Languages (POPL), January (Available through the ACM Digital Library, from Fondren’s web page)

Multiple Inheritance The idea Allow more flexible sharing of methods & attributes Relax the inclusion requirement If B is a subclass of A, it need not implement all of A’s methods Need a linguistic mechanism for specifying partial inheritance Problems when C inherits from both A & B C’s method table can extend A or B, but not both  Layout of an object record for C becomes tricky Other classes, say D, can inherit from C & B  Adjustments to offsets become complex Both A & B might provide fum() — which is seen in C ?  C++ produces a “syntax error” when fum() is used Need a better way to say “inherit”

Multiple Inheritance Example Use prefixing of storage Class Point { int x, y; } Class ColoredThing { Color c; } Class ColorPoint extends Point, ColoredThing { } x y x y c self Does casting work properly? self c

Multiple Inheritance Example Use prefixing of storage Class Point { int x, y; void draw(); void d2o(); } Class CThing { Color c; void rev(); } Class Cpoint extends Point, CThing { void draw() } x y x y c self c table draw d2o draw d2o rev Point: draw CPoint:draw Point: d20 rev CThing: rev rev self +=12

Casting with Multiple Inheritance Usage as Point:  No extra action (prefixing does everything) Usage as CThing:  Increment self by 12 Usage as CPoint:  Lay out data for CThing at self + 16  When calling rev  Call in table points to a trampoline function that adds 12 to self, then calls rev  Ensures that rev, which assumes that self points to a CThing data area, gets the right data

Multiple Inheritance ( Example ) Assume that C inherits fee() from A, fie() from B, & defines both foe() and fum() Object record for an instance of C A varsB varsC vars fee()fie()foe()fum() + offset code This implementation Uses trampoline functions Optimizes well with inlining Adds overhead where needed (Zero offsets go away) Folds inheritance into data structure, rather than linkage Assumes static class structure For dynamic, why not rebuild on a change in structure? fie() Trampoline function

Implementing Object-Oriented Languages So, what can an executing method see? (Reprise) The object’s own attributes & private variables The attributes & private variables of classes that define it  May be many such classes, in many combinations  Class variables are visible to methods that inherit the class Object defined in the global name space (or scope)  Objects may contain other objects Objects that contain their definition  A class as an instance variable of another class,... An executing method might reference any of these Making this work requires compile-time elaboration for static case and run-time elaboration for dynamic case Making it run quickly takes care, planning, and trickery … (later)

Memory Layout Placing run time data structures Alignment & padding Both languages & machines have alignment restrictions Place values with identical restrictions next to each other Assign offsets from most restrictive to least Insert padding to match restrictions (if needed) Stack & heap share free space Fixed-size areas together For compiler, this is the entire picture CodeCode S G t l a & o t b i a c l HeapHeap 0 high StackStack

Impact of Memory Model on Code Shape Memory-to-memory model Compiler works within constraints of register set At each statement, compiler ensures demand < k Each variable has a location in memory Register allocation becomes an optimization Register-to-register model Compiler works with an unlimited set of virtual registers Compiler largely ignores relationship between demand & k Only allocate memory for spills, parameters, & ambiguous values  Complex data structures (arrays) will be assigned to memory Register allocation is needed for correctness (names)

Cache Performance & Relative Offsets Principle: Two items referenced together  Would like them in same cache block, or  Would like them to map into different cache sets This notion is sometimes called “cache packing” Manageable problem for two variables Complexity rises immensely with more variables Cache organization Virtual address tags  cache behavior based on compiler’s model (cache blocks associate by virtual address) Physical tags  no relationship if distance  page size

Heap management Allocate( ) & Free( ) Implementing these requires attention to detail Watch allocation & free cost, as well as fragmentation Many classic algorithms ( first fit, first fit with rover, best fit ) Implicit deallocation Humans are bad at writing calls to free() Major source of run-time problems Solution is to automate deallocation  Reference counting + automatic free()  Mark-sweep style garbage collection