Implementing Object-Oriented Languages - Part 1

Similar documents
Implementation Aspects of OO-Languages

Lecture 9. Semantic Analysis Scoping and Symbol Table

Java CPD (I) Frans Coenen Department of Computer Science

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Object Oriented Design

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

Java (12 Weeks) Introduction to Java Programming Language

Object Oriented Programming With C++(10CS36) Question Bank. UNIT 1: Introduction to C++

Lecture 7 Notes: Object-Oriented Programming (OOP) and Inheritance

Software Engineering. Software Testing. Based on Software Engineering, 7 th Edition by Ian Sommerville

NPTEL STRUCTURAL RELIABILITY

Stack Allocation. Run-Time Data Structures. Static Structures

Fundamentals of Java Programming

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

How To Develop Software

11 November

Java Virtual Machine Locks

Problems and Measures Regarding Waste 1 Management and 3R Era of public health improvement Situation subsequent to the Meiji Restoration

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Case studies: Outline. Requirement Engineering. Case Study: Automated Banking System. UML and Case Studies ITNP090 - Object Oriented Software Design

UML Class Diagrams (1.8.7) 9/2/2009

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

The Class Blueprint A Visualization of the Internal Structure of Classes

Java Interview Questions and Answers

Description of Class Mutation Mutation Operators for Java

Habanero Extreme Scale Software Research Project

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

Start Oracle Insurance Policy Administration. Activity Processing. Version

Databases and DBMS. What is a Database?

Programming Languages Featherweight Java David Walker

Comp 411 Principles of Programming Languages Lecture 34 Semantics of OO Languages. Corky Cartwright Swarat Chaudhuri November 30, 20111

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 20: Stack Frames 7 March 08

Java Interfaces. Recall: A List Interface. Another Java Interface Example. Interface Notes. Why an interface construct? Interfaces & Java Types

The AVR Microcontroller and C Compiler Co-Design Dr. Gaute Myklebust ATMEL Corporation ATMEL Development Center, Trondheim, Norway

Chapter 5 Names, Bindings, Type Checking, and Scopes

Instruction Scheduling. Software Pipelining - 2

System 800xA Control AC 800M Planning

Classes and Pointers: Some Peculiarities (cont d.)

Java Programming Language

2! Multimedia Programming with! Python and SDL

Part I. The Picture class

COMPUTER SCIENCE. Paper 1 (THEORY)

TAPE. Test Code Adaptation Plugin for Eclipse. Lehmia Kiran Department of Computer Sciences NUCES-FAST Lahore, Pakistan

TAPE. Test Code Adaptation Plugin for Eclipse. Lehmia Kiran Department of Computer Sciences NUCES-FAST Lahore, Pakistan

AN ENABLING OPTIMIZATION FOR C++ VIRTUAL FUNCTIONS

COWLEY COLLEGE & Area Vocational Technical School

CS-XXX: Graduate Programming Languages. Lecture 25 Multiple Inheritance and Interfaces. Dan Grossman 2012

Java Application Developer Certificate Program Competencies

Lab Experience 17. Programming Language Translation

Design Document for OO Design for Case Study 1 (Course Scheduling)

Embedded Software development Process and Tools: Lesson-1

Advanced Data Structures

Compile-time type versus run-time type. Consider the parameter to this function:

Universität Karlsruhe (TH) Forschungsuniversität gegründet Inheritance Depth as a Cost Factor in Maintenance

Interpreters and virtual machines. Interpreters. Interpreters. Why interpreters? Tree-based interpreters. Text-based interpreters

In order to understand Perl objects, you first need to understand references in Perl. See perlref for details.

Software Visualization and Model Generation

5. Advanced Object-Oriented Programming Language-Oriented Programming

AP Computer Science Java Subset

Understanding Software Static and Dynamic Aspects

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

University of Twente. A simulation of the Java Virtual Machine using graph grammars

Automaton Programming and Inheritance of Automata

VB.NET Programming Fundamentals

Object-Oriented Programming

Syllabus for CS 134 Java Programming

1 Description of The Simpletron

CS 111 Classes I 1. Software Organization View to this point:

THE OPEN UNIVERSITY OF TANZANIA

Glossary of Object Oriented Terms

Universal hashing. In other words, the probability of a collision for two different keys x and y given a hash function randomly chosen from H is 1/m.

Databases What the Specification Says

The first program: Little Crab

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

Advanced Computer Architecture-CS501. Computer Systems Design and Architecture 2.1, 2.2, 3.2

Inside the Java Virtual Machine

Working with SmartArt

Database Design and the E-R Model

Assuming the Role of Systems Analyst & Analysis Alternatives

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Compiling Object Oriented Languages. What is an Object-Oriented Programming Language? Implementation: Dynamic Binding

Lecture 11: Tail Recursion; Continuations

Exercise 8: SRS - Student Registration System

2 The first program: Little Crab

USING UML FOR OBJECT-RELATIONAL DATABASE SYSTEMS DEVELOPMENT: A FRAMEWORK

Abstract Class & Java Interface

Programming Embedded Systems

PART IV Performance oriented design, Performance testing, Performance tuning & Performance solutions. Outline. Performance oriented design

MCI-Java: A Modified Java Virtual Machine Approach to Multiple Code Inheritance

A terminology model approach for defining and managing statistical metadata

JAVA - INHERITANCE. extends is the keyword used to inherit the properties of a class. Below given is the syntax of extends keyword.

CS193j, Stanford Handout #10 OOP 3

Configuration & Build Management

Java Classes. GEEN163 Introduction to Computer Programming

Chapter 4 Generic Log Adapter: A Step towards building Parser based Self-healable Autonomic System 4.1 Introduction

Chapter 1 Fundamentals of Java Programming

Chapter 4 OOPS WITH C++ Sahaj Computer Solutions

PharmaSUG Paper AD03

Transcription:

Implementing Object-Oriented Languages - Part 1 Y.N. Srikant Computer Science and Automation Indian Institute of Science Bangalore 560 012 NPTEL Course on Compiler Design

Outline of the Lecture Language requirements Mapping names to methods Variable name visibility Code generation for methods Simple optimizations Parts of this lecture are based on the book, Engineering a Compiler, by Keith Cooper and Linda Torczon, Morgan Kaufmann, 2004, sections 6.3.3 and 7.10. Y.N. Srikant 2

Language Requirements Objects and Classes Inheritance, subes and superes Inheritance requires that a sub have all the instance variables specified by its super Necessary for super methods to work with sub objects If A is B s super, then some or all of A s methods/instance variables may be redefined in B Y.N. Srikant 3

Example of Class Hierarchy with Complete Method Tables n: 0 fum fum three two n: 1 fum foe foe one n: 2 fum foe fie fie c x: 5 y: 3 x: 2 y: 0 z: a object method x: 5 y: 3 z: b Y.N. Srikant 4

Mapping Names to Methods Method invocations are not static calls a.() invokes one.(), b.foe() invokes two.foe(), and c.fum() invokes three.fum() Conceptually, method lookup behaves as if it performs a search for each procedure call These are called virtual calls Search for the method in the receiver s ; if it fails, move up to the receiver s super, and further To make this search efficient, an implementation places a complete method table in each Or, a to the method table is included (virtual tbl ptr) Y.N. Srikant 5

Mapping Names to Methods If the structure can be determined wholly at compile time, then the method tables can be statically built for each If es can be created at run-time or loaded dynamically ( definition can change too) Full lookup in the hierarchy can be performed at runtime or Use complete method tables as before, and include a mechanism to update them when needed Y.N. Srikant 6

Rules for Variable Name Visibility Invoking b.() allows () access to all of b s instance variables (x,y,z), (since and b are both declared by one), and also all variables of es one, two, and three However, invoking b.foe() allows foe() access only to instance variables x and y of b (not z), since foe() is declared by two, and b by one foe() can also access variables of es two and three, but not variables of one Y.N. Srikant 7

Example of Class Hierarchy with Complete Method Tables n: 0 fum fum three two n: 1 fum foe foe one n: 2 fum foe fie fie c x: 5 y: 3 x: 2 y: 0 z: a object method x: 5 y: 3 z: b Y.N. Srikant 8

Code Generation for Methods Methods can access any data member of any object that becomes its receiver receiver - every object that can find the method subject to hierarchy restrictions Compiler must establish an offset for each data member that applies uniformly to every receiver The compiler constructs these offsets as it processes the declarations for a Objects contain no code, only data Y.N. Srikant 9

Single Class, No Inheritance Example: method offset Class giant { 0 %giant.new_ int () {} int fie() {} int foe() {} int fum() {} static n; 4 8 12 16 20 %giant._ %giant.fie_ %giant.foe_ %giant.fum_ 2 x y 13 14 15 16 x y fred object layout int x,y; } giant record joe object layout Y.N. Srikant 10

Implementing Single Inheritance sc ptr super ptr methods variables %_ %fum_ mc giant ptr 1 x: 15 x: 5 jack %_ %foe_ 1 x: 5 y: 6 %_ %fie_ 2 x: 13 { y: 14 z: 20 y: 16 z: 30 fred object goose variables joe Y.N. Srikant 11

Single Inheritance Object Layout Object layout for joe/fred (giant) record for giant sc data sc mc data data mc giant data datagiant data (x) (y) (z) super %_ %fie_ 2 Object layout for goose (mc) sc data (x) mc data (y) record for mc super %_ %foe_ 1 Object layout for jack (sc) sc data (x) record for sc super %_ %fum_ 1 Y.N. Srikant 12

Single Inheritance Object Layout sc data mc data giant data Now, an instance variable has the same offset in every where it exists up in its super Method tables also follow a similar sequence as above When a redefines a method defined in one of its superes the method for that method implementation must be stored at the same offset as the previous implementation of that method in the superes Y.N. Srikant 13

Implementing Multiple Inheritance Assume that c inherits from es a and b, but that a and b are unrelated in the inheritance hierarchy Assume that c implements, inherits fie from a, and inherits foe and fum from b The diagram and the object layouts are shown next Y.N. Srikant 14

Implementing Multiple Inheritance a b fie foe fum c Y.N. Srikant 15

Implementing Multiple Inheritance object layout for objects of a a data object layout for objects of b b data object layout for objects of c a data b data c data Y.N. Srikant 16

Implementing Multiple Inheritance object layout for objects of c a data b data c data When c.fie() (inherited from a) is invoked with an object layout as above, it finds all of a s instance variables at the correct offsets Since fie was compiled with a, it will not (and cannot) access the other instance variables present in the object and hence works correctly Similarly, c.() also works correctly (implemented in c) finds all the instance variables at the correct offsets since it was compiled with c with a knowledge of the entire hierarchy Y.N. Srikant 17

Implementing Multiple Inheritance a b fie foe fum c Y.N. Srikant 18

Implementing Multiple Inheritance object layout for objects of c a data b data c data However, invoking c.foe() or c.fum() creates a problem foe and fum are inherited from b, but invoked from an object of c Instance variables of b are in the wrong place in this object record sandwiched between the instance variables of es a and c In objects of b, the instance variables are at the start of the object record Hence the offset to the instance variables of b inside an object record of c is unknown Y.N. Srikant 19

Implementing Multiple Inheritance To compensate for this, the compiler must insert code to adjust the receiver so that it points into the middle of the object record to the beginning of b s instance variables There are two ways of doing this Y.N. Srikant 20

Implementing Multiple Inheritance - Fixed Offset Method object layout for objects of c a data b data c data Record the constant offset in the method table along with the methods Offsets for this example are as follows: (c) : 0, (a) fie: 0, (b) foe : 8, (b) fum : 8, assuming that instance variables of a take 8 bytes Generated code adds this offset to the receiver s address before invoking the method Y.N. Srikant 21

Implementing Multiple Inheritance a b fie foe fum c object layout for objects of c a data b data c data data for c.foe() Y.N. Srikant 22

Implementing Multiple Inheritance - Trampoline Functions Create trampoline functions for each method of b A function that increments this ( to receiver) by the required offset and then invokes the actual method from b. On return, it decrements the receiver, if it was passed by reference Y.N. Srikant 23

Implementing Multiple Inheritance Trampolines appear to be more expensive than the fixed offset method, but not really so They are used only for calls to methods inherited from b In the other method, offset (possibly 0) was added for all calls Method inlining will make it better than option 1, since the offset is a constant Finally, a duplicate (pointing to c) may need to be inserted just before instance variables of b (for convenience) Y.N. Srikant 24