Code Generation II. Code generation for OO languages. Object layout Dynamic dispatch. Parameter-passing mechanisms Allocating temporaries in the AR

Similar documents
Lecture Outline. Topic 1: Basic Code Generation. Code Generation. Lecture 12. Topic 2: Code Generation for Objects. Simulating a Stack Machine

Code Generation. Lecture 12

The remote testing experiment. It works! Code Generation. Lecture 12. Remote testing. From the cs164 newsgroup

Code Generation. Lecture 19

Code Generation Super Lectures

Code Generation & Parameter Passing

The code generator must statically assign a location in the AR for each temporary add $a0 $t1 $a0 ; $a0 = e 1 + e 2 addiu $sp $sp 4 ; adjust $sp (!

COL728 Minor2 Exam Compiler Design Sem II, Answer all 5 questions Max. Marks: 20

Code Generation Super Lectures

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

We can emit stack-machine-style code for expressions via recursion

Written Assignment 5 Due??

Compilers and computer architecture: A realistic compiler to MIPS

Run-time Environments

Run-time Environments

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

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

Midterm II CS164, Spring 2006

G Programming Languages - Fall 2012

CS143 - Written Assignment 4 Reference Solutions

CS2210: Compiler Construction. Runtime Environment

Lecture Outline. Code Generation. Lecture 30. Example of a Stack Machine Program. Stack Machines

Code Generation. Lecture 30

CS2210: Compiler Construction. Runtime Environment

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

The Activation Record (AR)

System Software Assignment 1 Runtime Support for Procedures

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

Procedure and Object- Oriented Abstraction

CSE Lecture In Class Example Handout

Intermediate Programming, Spring 2017*

Wednesday, October 15, 14. Functions

Review of Activation Frames. FP of caller Y X Return value A B C

Data Abstraction. Hwansoo Han

Code Generation. Lecture 31 (courtesy R. Bodik) CS164 Lecture14 Fall2004 1

ECE 15B COMPUTER ORGANIZATION

SPIM Procedure Calls

Implementing Procedure Calls

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12

Lecture08: Scope and Lexical Address

Compiler Construction Lecture 05 A Simple Stack Machine. Lent Term, Lecturer: Timothy G. Griffin. Computer Laboratory University of Cambridge

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

Dynamic Memory Allocation

An Overview to Compiler Design. 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 1

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

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Overview of Semantic Analysis. Lecture 9

Chapter 10. Implementing Subprograms

Homework I - Solution

Scope, Functions, and Storage Management

Principles of Programming Languages

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

Implementing Higher-Level Languages. Quick tour of programming language implementation techniques. From the Java level to the C level.

Inheritance, Polymorphism and the Object Memory Model

Programming Languages

ECE 2035 A Programming Hw/Sw Systems Spring problems, 8 pages Final Exam Solutions 29 April 2015

CA Compiler Construction

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Notation. The rules. Evaluation Rules So Far.

CS143 Compilers - Written Assignment 4

More Static Semantics. Static. One-Slide Summary. Lecture Outline. Typing Rules. Dispatch Rules SELF_TYPE

Chapter 2. Instructions:

Run-time Environments - 2

LECTURE 19. Subroutines and Parameter Passing

2. dead code elimination (declaration and initialization of z) 3. common subexpression elimination (temp1 = j + g + h)

ECS 142 Project: Code generation hints

Data Structure Layout. In HERA/Assembly

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far

CS558 Programming Languages. Winter 2013 Lecture 3

5. Semantic Analysis!

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

Programming Languages

Operational Semantics of Cool

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

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

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

Lecture 7: Binding Time and Storage

CIT Week13 Lecture

200 points total. Start early! Update March 27: Problem 2 updated, Problem 8 is now a study problem.

Assembly Language Programming. CPSC 252 Computer Organization Ellen Walker, Hiram College

Topic 7: Activation Records

Formal Languages and Compilers Lecture IX Semantic Analysis: Type Chec. Type Checking & Symbol Table

Structure of a Compiler. We looked at each stage in turn. Exceptions. Language Design and Implementation Issues

Chapter 5: Procedural abstraction. Function procedures. Function procedures. Proper procedures and function procedures

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Contents. Slide Set 2. Outline of Slide Set 2. More about Pseudoinstructions. Avoid using pseudoinstructions in ENCM 369 labs

Subprograms. Copyright 2015 Pearson. All rights reserved. 1-1

Java and C CSE 351 Spring

Compilers. Cool Semantics II. Alex Aiken

CSE 401/M501 Compilers

ECE 2400 Computer Systems Programming Fall 2018 Topic 7: Concrete Data Types

The role of semantic analysis in a compiler

CS 2630 Computer Organization. Meeting 10/11: data structures in MIPS Brandon Myers University of Iowa

Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University. See P&H 2.8 and 2.12, and A.

Three-Address Code IR

Late Binding; OOP as a Racket Pattern

MIPS Functions and the Runtime Stack

Functions in C. Memory Allocation in C. C to LC3 Code generation. Next.. Complete and submit C to LC3 code generation. How to handle function calls?

Chap. 8 :: Subroutines and Control Abstraction

Transcription:

Code Generation II Code generation for OO languages Object layout Dynamic dispatch Parameter-passing mechanisms Allocating temporaries in the AR Object Layout OO implementation = Stuff from last lecture + More stuff OO Slogan: If B is a subclass of A, than an object of class B can be used wherever an object of class A is expected This means that code in class A works unmodified for an object of class B

Two Issues How are objects represented in memory? How is dynamic dispatch implemented? Object Layout Example class A { int a = 0; int d = 1; int f() { a = a + d; return a; } }; class B extends A { int b = 2; int f() { return a; } int g() { a = a b; return a; } }; class C extends A { int c = 3; int h() { a = a * c; return a; } };

Object Layout (Cont.) Fields a and d are inherited by classes B and C All methods in all classes refer to a For A methods to work correctly in A, B, and C objects, field a must be in the same place in each object Object Layout (Cont.) An object is like a struct in C. The reference foo.field is an index into a foo struct at an offset corresponding to field Objects in Java/C++ are implemented similarly Objects are laid out in contiguous memory Each field is stored at a fixed offset in object

A Sample Object Layout The first 3 words of an object contain header information: Offset Class Tag Object Size Dispatch Ptr Field 1 Field 2... 0 4 8 12 16 Sample Object Layout (Cont.) Class tag is an integer Identifies class of the object Object size is an integer Size of the object in words Dispatch ptr is a pointer to a table of methods More later Fields in subsequent slots Layout in contiguous memory

Subclasses Observation: Given a layout for class A, a layout for subclass B can be defined by extending the layout of A with additional slots for the additional field of B Leaves the layout of A unchanged (B is an extension of A) Layout Picture Offset Class 0 4 8 12 16 20 A Atag 5 * a d B Btag 6 * a d b C Ctag 6 * a d c

Subclasses (Cont.) The offset for a field is the same in a class and all of its subclasses Any method for an A 1 can be used on a subclass A 2 Consider layout for A n < < A 3 < A 2 < A 1 Header A 1 attrs. A 2 attrs A 3 attrs... A 1 object A 2 object A 3 object What about multiple inheritance? Object Layout Example (Repeat) class A { int a = 0; int d = 1; int f() { a = a + d; return a; } }; class C extends A { int c = 3; int h() { a = a * c; return a; } }; class B extends A { int b = 2; int f() { return a; } int g() { a = a b; return a; } };

Dynamic Dispatch Example e.g() g refers to method in B if e is a B e.f() f refers to method in A if f is an A or C (inherited in the case of C) f refers to method in B for a B object The implementation of methods and dynamic dispatch strongly resembles the implementation of fields Dispatch Tables Every class has a fixed set of methods (including inherited methods) A dispatch table indexes these methods dispatch table = an array of method entry points A method f lives at a fixed offset in the dispatch table for a class and all of its subclasses

Dispatch Table Example Offset Class 0 4 The dispatch table for class A has only 1 method A B C fa fb fa g h The tables for B and C extend the table for A to the right Because methods can be overridden, the method for f is not the same in every class, but is always at the same offset Using Dispatch Tables The dispatch pointer in an object of class X points to the dispatch table for class X Every method f of class X is assigned an offset O f in the dispatch table at compile time

Using Dispatch Tables (Cont.) To implement a dynamic dispatch e.f() we Evaluate e. The result is a pointer to an object x Call D[O f ] D is the dispatch table for x In the call, this is bound to x Parameter Passing Mechanisms There are many semantic issues in programming languages centering on when values are computed and the scope of names we ve already seen static vs. dynamic scoping Now we ll focus on parameter passing When are arguments of function calls evaluated? To what objects are the formal parameters bound?

Call-By-Value To evaluate f(e) Evaluate e to a value v Bind v to the formal parameter in the function body Example void g(x) { x = x + 1; } void f(y) { g(y); print(y); } Under call-by-value, f(0) prints 0. The Stack Under Call-By-Value y 0 f(y) g(y) x = x + 1 print(y) y 0 x 0 y 0 x 1 y 0

Call-By-Value Discussion Under call-by-value, g(y) does not affect the value of y y s value, not y itself, is passed to g The formal parameter is stored in a different location from the actual parameter Call-by-value is widely used C, C++, Java are prominent examples Call-By-Reference To evaluate f(e) e is evaluated A pointer to e is passed as the argument f s code accesses the argument through the pointer If e is already a stored value (i.e., a variable) a pointer to that location is used Otherwise, e is evaluated and stored in a fresh, temporary location first

The Stack Under Call-By- Reference y 0 f(y) g(y) x = x + 1 print(y) y x y x 0 1 y 1 Call-By-Reference Discussion Under Call-By-Reference, only the address is passed References to the value dereference the pointer In the example, because x and y refer to the same value, changes to x also change y Many languages pass large data structures (e.g., arrays) by reference

Review The stack machine has activation records and intermediate results interleaved on the stack AR Temporaries AR Temporaries Review (Cont.) Advantage: Very simple code generation Disadvantage: Very slow code Storing/loading temporaries requires a store/load and $sp adjustment

A Better Way Idea: Keep temporaries in the AR The code generator must assign a location in the AR for each temporary Example def fib(x) = if x = 1 then 0 else if x = 2 then 1 else fib(x - 1) + fib(x 2) What intermediate values are placed on the stack? How many slots are needed in the AR to hold these values?

How Many Temporaries? Let NT(e) = # of temps needed to evaluate e NT(e 1 + e 2 ) Needs at least as many temporaries as NT(e 1 ) Needs at least as many temporaries as NT(e 2 ) + 1 Space used for temporaries in e 1 can be reused for temporaries in e 2 The Equations NT(e 1 + e 2 ) = max(nt(e 1 ), 1 + NT(e 2 )) NT(e 1 -e 2 ) = max(nt(e 1 ), 1 + NT(e 2 )) NT(if e 1 = e 2 then e 3 else e 4 ) = max(nt(e 1 ),1 + NT(e 2 ), NT(e 3 ), NT(e 4 )) NT(id(e 1,,e n ) = max(nt(e 1 ),,NT(e n )) NT(int) = 0 NT(id) = 0 Is this bottom-up or top-down? What is NT( code for fib )?

The Revised AR For a function definition f(x 1,,x n ) = e the AR has 2 + n + NT(e) elements Return address Frame pointer n arguments NT(e) locations for intermediate results Picture Old FP x n... x 1 Return Addr. Temp NT(e)... Temp 1

Revised Code Generation Code generation must know how many temporaries are in use at each point Add a new argument to code generation: the position of the next available temporary Code Generation for + (original) cgen(e 1 + e 2 ) = cgen(e 1 ) sw $a0 0($sp) addiu $sp $sp -4 cgen(e 2 ) lw $t1 4($sp) add $a0 $t1 $a0 addiu $sp $sp 4

Code Generation for + (revised) cgen(e 1 + e 2, nt) = cgen(e 1, nt) sw $a0 nt($fp) cgen(e 2, nt + 4) lw $t1 nt($fp) add $a0 $t1 $a0 Notes The temporary area is used like a small, fixed-size stack Exercise: Write out cgen for other constructs