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

Similar documents
Compiler Construction Lent Term 2013 Lectures 9 & 10 (of 16)

Scope, Functions, and Storage Management

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

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

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

CS153: Compilers Lecture 11: Compiling Objects

G Programming Languages - Fall 2012

CS153: Compilers Lecture 15: Local Optimization

Procedure and Object- Oriented Abstraction

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

Lecture 23: Object Lifetime and Garbage Collection

CGS 2405 Advanced Programming with C++ Course Justification

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

Object-Oriented Programming

INF 102 CONCEPTS OF PROG. LANGS FUNCTIONAL COMPOSITION. Instructors: James Jones Copyright Instructors.

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

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

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

The role of semantic analysis in a compiler

Lecture Notes on Programming Languages

CS558 Programming Languages

Data Abstraction. Hwansoo Han

CS558 Programming Languages

Introduction to Programming Using Java (98-388)

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

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

Subtyping (Dynamic Polymorphism)

Implementing Subprograms

CS558 Programming Languages

Project Compiler. CS031 TA Help Session November 28, 2011

CSE 307: Principles of Programming Languages

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


STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Weeks 6&7: Procedures and Parameter Passing

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

CSE 303: Concepts and Tools for Software Development

Code Generation & Parameter Passing

Chapter 5 Object-Oriented Programming

Binghamton University. CS-140 Fall Dynamic Types

CS558 Programming Languages

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

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

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

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

CompuScholar, Inc. Alignment to Nevada "Computer Science" Course Standards

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

Today s lecture. CS 314 fall 01 C++ 1, page 1

C++ (Non for C Programmer) (BT307) 40 Hours

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

Programming, numerics and optimization

Polymorphism. Contents. Assignment to Derived Class Object. Assignment to Base Class Object

Implementing Abstractions

C++ Important Questions with Answers

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

Type Hierarchy. Lecture 6: OOP, autumn 2003

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

An introduction to C++ template programming

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

StackVsHeap SPL/2010 SPL/20

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

2. The object-oriented paradigm!

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

CS558 Programming Languages. Winter 2013 Lecture 3

ECS 142 Project: Code generation hints

Programming Languages and Techniques (CIS120)

C++ Programming: Polymorphism

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Object-Oriented Programming

Advanced Systems Programming

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

CS558 Programming Languages

IR Generation. May 13, Monday, May 13, 13

System Software Assignment 1 Runtime Support for Procedures

COS 320. Compiling Techniques

Programming Languages and Techniques (CIS120)

Lecture 18 Tao Wang 1

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

Compiler construction 2009

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

CMSC330 Fall 2013 Practice Problems 6 Solutions

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

CMSC 330: Organization of Programming Languages

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

Java and C CSE 351 Spring

CPS 506 Comparative Programming Languages. Programming Language

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

CS558 Programming Languages

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

Topic 7: Activation Records


Topics. Modularity and Object-Oriented Programming. Dijkstra s Example (1969) Stepwise Refinement. Modular program development

CMSC330. Objects, Functional Programming, and lambda calculus

CS250 Final Review Questions

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

Absolute C++ Walter Savitch

CLASSES AND OBJECTS IN JAVA

Transcription:

Compiler Construction Lent Term 15 Lectures 10, 11 (of 16) 1. Slang.2 (Lecture 10) 1. In lecture code walk of slang2_derive 2. Assorted topics (Lecture 11) 1. Exceptions 2. Objects 3. Stacks vs. Register 4. Simple optimisations 5. Boxed and unboxed objects Timothy G. Griffin tgg22@cam.ac.uk Computer Laboratory University of Cambridge 1 Topic 1 : Exceptions (informal description) e handle f raise e If expression e evaluates normally to value v, then v is the result of the entire expression. Otherwise, an exceptional value v is raised in the evaluation of e, then result is (f v ) Evaluate expression e to value v, and then raise v as an exceptional value, which can only be handled. Implementation of exceptions may require a lot of language-specific consideration and care. Exceptions can interact in powerful and unexpected ways with other language features. Think of C++ and class destructors, for example. 1

Viewed from the call stack current frame... handle frame...... handle frame frame for f v... Call stack just before evaluating code for e handle f Push a special frame for the handle raise v is encountered while evaluating a function body associated with top-most frame Unwind call stack. Depending on language, this may involve some clean up to free resources. e handle f Possible pseudo-code implementation let fun _h27 () = build special handle frame save address of f in frame; code for e return value of e in _h27 () end raise e code for e save v, the value of e; unwind stack until first fp found pointing at a handle frame; Replace handle frame with frame for call to (extracted) f using v as argument. 2

let start := 10 Topic 2 : Objects (with single inheritance) class Vehicle extends Object { var position := start method move(int x) = {position := position + x class Car extends Vehicle { var passengers := 0 method await(v : Vehicle) = if (v.position < position) then v.move(position v.position) else self.move(10) class Truck extends Vehicle { method move(int x) = if x <= 55 then position := position +x var t := new Truck var c := new Car var v : Vehicle := c in c.passengers := 2; c.move(60); v.move(70); c.await(t) end method override subtyping allows a Truck or Car to be viewed and used as a Vehicle 5 Object Implementation? how do we access object fields? both inherited fields and fields for the current object? how do we access method code? if the current class does not define a particular method, where do we go to get the inherited method code? how do we handle method override? How do we implement subtyping ( object polymorphism )? If B is derived from A, then need to be able to treat a pointer to a B-object as if it were an A- object. 6 3

Another OO Feature Protection mechanisms to encapsulate local state within an object, Java has private protected and public qualifiers private methods/fields can t be called/used outside of the class in which they are defined This is really a scope/visibility issue Frontend during semantic analysis (type checking and so on), the compiler maintains this information in the symbol table for each class and enforces visibility rules. 7 Object representation class A { public: int a1, a2; C++ void m1(int i) { a1 = i; void m2(int i) { a2 = a1 + i; a1 a2 m1_a m2_a An A object object data method table NB: a compiler typically generates methods with an extra argument representing the object (self) and used to access object data. 8 4

Inheritance ( pointer polymorphism ) class B : public A { public: int b1; a B object void m3(void) { b1 = a1 + a2; a1 a2 b1 object data m1_a m2_a m3_b method table (code entry points = memory locations) Note that a pointer to a B object can be treated as if it were a pointer to an A object 9 Method overriding class C : public A { public: int c1; void m3(void) { b1 = a1 + a2; void m2(int i) { a2 = c1 + i; a1 a2 c1 m1_a_a m2_a_c m3_c_c a C object object data method table declared defined 10 5

Static vs. Dynamic which method to invoke on overloaded polymorphic types? class C *c =...; class A *a = c; a->m2(3);??? m2_a_a(a, 3); m2_a_c(a, 3); static dynamic 11 Dynamic dispatch implementation: dispatch tables ptr to C Is also a ptr to A class C *c =...; class A *a = c; a1 a2 b1 m1_a_a m2_a_c m3_c_c a->m2(3); *(a->dispatch_table[1])(a, 3); 12 6

This implicitly uses some form of pointer subtyping void m2(int i) { a2 = c1 + i; void m2_a_c(class_a *this_a, int i) { class_c *this = convert_ptra_to_ptrc(this_a); this->a2 = this->c1 + i; 13 Topic 3 : stack vs regsisters V2 V1 add V1 + V2 r3 : V2 r7 : r8 : V1 add r7 r8 r3 r3 : V2 r7 : V1 + V2 r8 : V1 Stack-oriented: (+) argument locations is implicit, so instructions are smaller. (-) Execution is slower Register-oriented: (+) Execution faster (-) argument location is explicit, so instructions are larger 14 7

Topic 4: Simple optimisations. (a) Inline expansion fun f(x) = x + 1 fun g(x) = x 1 fun h(x) = f(x) + g(x) (+) Avoid building activation records at runtime (+) May allow further optimisations inline f and g fun f(x) = x + 1 fun g(x) = x 1 fun h(x) = (x+1) + (x-1) (-) May lead to code bloat (apply only to functions with small bodies?) Question: if we inline all occurrences of a function, can we delete its definition from the code? What if it is needed at link time? 15 Be careful with variable scope Inline g in h NO let val x = 1 fun g(y) = x + y fun h(x) = g(x) + 1 in h(17) end YES let val x = 1 fun g(y) = x + y fun h(x) = x + y + 1 in h(17) end let val x = 1 fun g(y) = x + y fun h(z) = x + z + 1 in h(17) end 8

(b) Constant propagation, constant folding let x = 2 let y = x 1 let z = y * 17 let x = 2 let y = 2 1 let z = y * 17 let x = 2 let y = 1 let z = y * 17 let x = 2 let y = 1 let z = 1 * 17 let x = 2 let y = 1 let z = 17 Propagate constants and evaluate simple expressions at compile-time Note : opportunities are often exposed by inline expansion David Gries : Never put off till run-time what you can do at compile-time. How about this? Replace with x * 0 0 But be careful OOPS, not if x has type float NAN*0 = NAN, 17 (c) peephole optimisation Communications of the ACM, July 65 Eliminate Results for syntax-directed code generation. 9

peephole optimisation code sequence Sweep a window over the code sequence looking for instances of simple code patterns that can be rewritten to better code (might be combined with constant folding, etc, and employ multiple passes) Examples -- eliminate useless combinations (push 0; pop) -- introduce machine-specific instructions -- improve control flow (rewrite GOTO L1 L1: GOTO L2 to GOTO L2 L1 : GOTO L2 ) (d) Eliminate Tail recursion A recursive function exhibits tail recursion if on all recursive branches the last thing it does is call itself. fun foldl f e [] = e foldl f e (x::xr) = foldl f (f(x, e)) xr We should be able to compile this to a LOOP in order to avoid constructing many activation records at runtime. Exercise : How? 10

Topic 5 : Boxed and unboxed objects map : ( a -> b) -> a list -> b list fun map f [] = [] map f (a::rest) = (f a) :: (map f rest) The code generated for map must work for any times a and b. So it seems that all values of any type must be represented by objects of the same size. Boxing and Unboxing An unboxed integer : 1066 On the heap A boxed integer : Many ML compilers use a single bit in each machine word to distinguish boxed from unboxed values. This is why mosml has 31 (or 63) bit integers. It is better to work with unboxed values than with boxed values. Compilers for ML-like languages must expend a good deal of effort trying to find good optimizations for boxed/unboxed choices. See Appel. HEADER 1066 Similar terminology is used in Java for putting a value in a container class (boxing) and taking it out (unboxing) For example, put an int into the Integer container class. 11

Tuples (in ML-like, L3-like languages) g: int -> int * int * int fun g x = (x+1, x+2, x+3)... (g 17)... Heap allocated 17 HEADER stack before call to g stack after On a stack-oriented machine fun g x = (x+1, x+1, x+3) fun g x = Some IR let val y1 = x+1 val y2 = x+2 val y3 = x+3 in return (ALLOCATE_TUPLE 3) end ALLOCATE_TUPLE 3 Heap allocated HEADER 12

Tuples (in ML-like, L3-like languages) fun g x = (x+1, x+1, x+3) fun f (u, v, w) = u + v + w... f (g 17)... Does function f take 3 arguments or 1? How would you inline f? How might we avoid this? stack frame for g stack frame for f 17 17 57 Stack snapshots Evaluation of f(g 17) 13