Mooly Sagiv and Roman Manevich School of Computer Science

Slides:



Advertisements
Similar presentations
Symbol Table.
Advertisements

Intermediate Code Generation
Programming Languages and Paradigms
Intermediate Representation III. 2 PAs PA2 deadline is
1 Compiler Construction Intermediate Code Generation.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
Program Representations. Representing programs Goals.
Compiler Construction Intermediate Representation III Activation Records Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
Intermediate Representation I High-Level to Low-Level IR Translation EECS 483 – Lecture 17 University of Michigan Monday, November 6, 2006.
Chapter 14: Building a Runnable Program Chapter 14: Building a runnable program 14.1 Back-End Compiler Structure 14.2 Intermediate Forms 14.3 Code.
Lecture 13 – Compiling Object-Oriented Programs Eran Yahav 1 Reference: MCD
Intermediate code generation. Code Generation Create linear representation of program Result can be machine code, assembly code, code for an abstract.
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
CS 536 Spring Run-time organization Lecture 19.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Compiler Summary Mooly Sagiv html://
Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Compiler Construction Intermediate Representation I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
Building An Interpreter After having done all of the analysis, it’s possible to run the program directly rather than compile it … and it may be worth it.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Semantic Analysis III + Intermediate Representation I.
Compiler Construction
Winter Compiler Construction T8 – semantic analysis recap + IR part 1 Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv University.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
MIT Intermediate Formats Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology.
Compiler Construction Dr. Noam Rinetzky and Orr Tamir School of Computer Science Tel Aviv University
Winter Compiler Construction T9 – IR part 2 + Runtime organization Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv University.
Introduction to Code Generation and Intermediate Representations
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
Introduction CPSC 388 Ellen Walker Hiram College.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
Semantic Analysis III + Intermediate Representation I.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
1 Structure of a Compiler Source Language Target Language Semantic Analyzer Syntax Analyzer Lexical Analyzer Front End Code Optimizer Target Code Generator.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
1 Chapter10: Code generator. 2 Code Generator Source Program Target Program Semantic Analyzer Intermediate Code Generator Code Optimizer Code Generator.
Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Lecture 9 Symbol Table and Attributed Grammars
CS 404 Introduction to Compiler Design
Chapter 1 Introduction.
Names and Attributes Names are a key programming language feature
Context-Sensitive Analysis
Winter Compiler Construction
Intermediate code Jakub Yaghob
A Simple Syntax-Directed Translator
Constructing Precedence Table
Compiler Construction (CS-636)
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Semantic Analysis with Emphasis on Name Analysis
Chapter 1 Introduction.
Run-time organization
Introduction to Compilers Tim Teitelbaum
CS 536 / Fall 2017 Introduction to programming languages and compilers
The University of Adelaide, School of Computer Science
Lecture 4: MIPS Instruction Set
Code Generation.
Unit IV Code Generation
Chapter 6 Intermediate-Code Generation
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
CSE401 Introduction to Compiler Construction
Winter Compiler Construction T10 – IR part 3
Mooly Sagiv and Roman Manevich School of Computer Science
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
UNIT V Run Time Environments.
Intermediate Code Generation
Course Overview PART I: overview material PART II: inside a compiler
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Presentation transcript:

Mooly Sagiv and Roman Manevich School of Computer Science Winter 2006-2007 Compiler Construction T9 – IR part 2 + Runtime organization Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv University TODO: ensure consistency in displaying the values of instrumentation predicates for concrete heaps

Announcements Know thy group’s code What is expected in PA3 documentation (5 pts) Testing strategy: lists of tests and what they check (scope rules, type rules etc.) How did you conduct the semantic analysis – in which order do you do things Don’t document the most obvious things (“The input to CUP is in IC.cup”), don’t repeat stuff already in PA3.pdf Document non-obvious choices (“We build the type table after parsing”, “we do not handle situation X”, “we handle special situation Y like so”) Output format (e.g., “first we print type table then we print…”) Know thy group’s code

Today Today: Next time: IR Runtime organization IC Language Lexical Analysis Syntax Analysis Parsing AST Symbol Table etc. Inter. Rep. (IR) Code Generation Executable code exe Today: IR Specific LIR language Translating HIR nodes Runtime organization Objects Polymorphism Next time: Simple lowering techniques Runtime organization Activation records (frames) PA4

Recap tomatoes + potatoes + carrots Symtab hierarchy Global type table Lexical analyzer tomatoes + potatoes + carrots tomatoes,PLUS,potatoes,PLUS,carrots,EOF Parser AddExpr left right Symtab hierarchy Global type table symbol kind type tomatoes var int potatoes carrots Id Type obj int O1 boolean O2 Foo O3 AddExpr left right LocationExpr id=tomatoes LocationExpr id=potatoes id=carrots LocationExpr A  E1 : T[] Type checking Additional semantic checks A  E1.length : int Move tomatoes,R1 Move potatoes,R2 Add R2,R1 ... LIR

Low-level IR (LIR) An abstract machine language Generic instruction set Not specific to a particular machine Low-level language constructs No looping structures, only labels + jumps/conditional jumps We will use – two-operand instructions Advantage – close to Intel assembly language LIR spec. available on web-site Will be used in PA4 Subject to changes LIR from T8 only for demonstration purposes

LIR instructions Instruction Meaning Move c,Rn Rn = c Move x,Rn Rn = x Move Rn,x x = Rn Add Rm,Rn Rn = Rn + Rm Sub Rm,Rn Rn = Rn – Rm Mul Rm,Rn Rn = Rn * Rm ... Immediate (constant) Memory (variable) Note 1: rightmost operand = operation destination Note 2: two register instr - second operand doubles as source and destination

Example Move 42,R1 Move R1,x x = 42; _test_label: while (x > 0) { Move x,R1 Compare 0,R1 JumpLE _end_label Move 1,R2 Sub R1,R2 Move R2,x Jump _test_label _end_label: x = 42; while (x > 0) { x = x - 1; } Compare results stored in global compare register (implicit register) Condition depends on compare register (warning: code shown is a naïve translation)

Translating expressions – example TR[x + 42] visit Add R2, R1 Move x, R1 AddExpr left right Move 42, R2 visit (left) visit (right) Add R2, R1 LocationEx id = x ValueExpr val = 42 Move x, R1 Move 42, R2 Very inefficient translation – can we do better?

Translation (IR lowering) How to translate HIR to LIR? Assuming HIR has AST form (ignore non-computation nodes) Define how each HIR node is translated Recursively translate HIR (HIR tree traversal) TR[e] = LIR translation of HIR construct e A sequence of LIR instructions Temporary variables = new locations Use temporary variables (LIR registers) to store intermediate values during translation

Translating expressions Fresh virtual (LIR) register generated by translation Binary operations (arithmetic and comparisons) R1 := TR[e1] R2 := TR[e2] R3 := R1 OP R2 TR[e1 OP e2] Shortcut notation to indicate target register NOT LIR instruction Unary operations TR[OP e] R1 := TR[e] R2 := OP R1

Translating (short-circuit) OR TR[e1 OR e2] R1 := T[e1] Compare 1,R1 JumpTrue _end_label R2 := T[e2] Or R2,R1 _end_label: (OR can be replaced by Move operation since R1 is 0)

Translating (short-circuit) AND TR[e1 AND e2] R1 := T[e1] Compare 0,R1 JumpTrue _end_label R2 := T[e2] And R2,R1 _end_label: (AND can be replaced by Move operation since R1 is 1)

Translating array and field access R1 := TR[e1] R2 := TR[e2] MoveArray R1[R2], R3 TR[e1[e2]] Give class type of e1 need to compute offset of field f TR[e1.f] R1 := TR[e1] MoveField R1.cf,R3 Need to identify class type of e1 from semantic analysis phase Constant representing offset of field f in objects of class type of e1 (we shall discuss object representation soon)

Translating statement block TR[s1] TR[s2] TR[s3] … TR[sN] TR[s1; s2; … ; sN]

Translating if-then-else R1 := TR[e] Compare 0,R1 JumpTrue _false_label T[s1] Jump _end_label _false_label: T[s2] _end_label: TR[if (e) then s1 else s2] Fresh labels generated during translation Fresh labels generated during translation

Translating if-then R1 := TR[e] TR[if (e) then s] Compare 0,R1 JumpTrue _end_label TR[s1] _end_label: TR[if (e) then s] Can avoid generating labels unnecessarily

Translating while TR[while (e) s] _test_label: R1 := TR[e] Compare 0,R1 JumpTrue _end_label TR[s] Jump _test_label _end_label

Translating call/return TR[C.foo(e1,…,en)] R1 := TR[e1] … Rn := TR[en] StaticCall C_foo(x1=R1,…,xn=Rn),R formal parameter name actual argument register R1 := TR[e1] R2 := TR[e2] VirtualCall R1.cfoo(x=R2),R TR[e1.foo(e2)] TR[return e] R1 := TR[e] Return R1 Constant representing offset of method f in dispatch table of class type of e1

Translation implementation Define classes for LIR instructions Define LoweringVisitor Apply visitor to translate each method Mechanism to generate new LIR registers (keep track of registers for next phase) Mechanism to generate new labels For each node type translation returns List of LIR instructions TR[e] Target register Splice lists: instructions.addAll(TR[e]) (input is a tree, output is a flat list)

spliced list for TR[x=x-1] Example revisited TR[ x = 42; while (x > 0) { x=x-1; } ] TR[x = 42] TR[ while (x > 0) { x=x-1; } ] Move 42,R1 Move R1,x TR[ while (x > 0) { x=x-1; } ] Move 42,R1 Move R1,x _test_label: Move x,R1 Compare 0,R1 JumpLE _end_label TR[x=x-1] Jump _test_label _end_label: Move 42,R1 Move R1,x _test_label: Move x,R1 Compare 0,R1 JumpLE _end_label Move 1,R2 Sub R2,R1 Jump _test_label _end_label: spliced list for TR[x=x-1] (actually a DFS walk)

Naïve translation Pretend all LIR registers are local variables Increases memory needed for each procedure during runtime Expensive access vs. accessing real registers (spilling) Generate fresh LIR register per HIR node Lifetime of registers very limited Allow consecutive labels Code bloat

Better translation Pretend all LIR registers are local variables Ideally: leave it to register allocation phase to store LIR registers in machine registers In this project we ignore this inefficiency Limit register allocation to single LIR instructions Generate fresh LIR register per HIR node Reuse registers Avoid generating registers for HIR leaves Allow consecutive labels Avoid generating consecutive labels Optimizations techniques discussed next time

Runtime organization Representation of basic types Representation of allocated objects Class instances Dispatch vectors Strings Arrays Procedures Activation records (frames) Discussed next time (relevant mostly for PA5)

Representing data at runtime Source language types int, boolean, string, object types, arrays etc. Target language types Single bytes, integers, address representation Compiler maps source types to some combination of target types Implement source types using target types Compiler maps basic operations on source types to combinations of operations on target types We will limit our discussion to IC

Representing basic types in IC int, boolean, string Simplified representation: 32 bit for all types boolean type could be more efficiently implemented with single byte Arithmetic operations Addition, subtraction, multiplication, division, remainder Could be mapped directly to target language types and operations Exception: string concatenation implemented using library function __stringCat

Pointer types Represent addresses of source language data structures Usually implemented as an unsigned integer (4 bytes) Pointer dereferencing – retrieves pointed value May produce an error Null pointer dereference Insert code to check fragile operations (in PA5) Only implicit in our case

Object types Basic operations How does it look at runtime? Field selection computing address of field, dereferencing address Method invocation Identifying method to be called, calling it How does it look at runtime?

Object types class Foo { int x; int y; void rise() {…} void shine() {…} } DispacthVectorPtr x y Runtime memory layout for object of class Foo Field offsets DispacthVectorPtr Method offsets x rise 1 y shine 2 1 Compile time information for Foo class type

Runtime memory layout for object of class Foo Field selection Foo f; int q; q = f.x; DispacthVectorPtr x y Runtime memory layout for object of class Foo Field offsets DispacthVectorPtr Method offsets Move f,R1 MoveField R1.1,R2 Move R2,q 1 x rise y shine 2 1 Compile time information for Foo class type generated during LIR translation

Implementation Store map for each ClassType From field to offset (starting from 1, since 0 reserved for DispatchVectorPtr) From method to offset

Object types and inheritance DispacthVectorPtr class Foo { int x; int y; void rise() {…} void shine() {…} } x prefix from Foo y z Runtime memory layout for object of class Bar Field offsets DispacthVectorPtr x rise 1 prefix from Foo class Bar extends Foo { int z; void twinkle() {…} void rise() {…} } y shine 2 z 3 twinkle Compile time information for Bar class type

Object types and polymorphism class Foo { … void rise() {…} void shine() {…} } Pointer to Bar f DVPtr x y z class Bar extends Foo { void rise() {…} } Runtime memory layout for object of class Bar Initializes value of DVPtr for Bar class Main { void main() { Foo f = new Bar(); f.rise(); } Foo dispatch vector Bar dispatch vector _Foo_rise _Bar_rise _Foo_shine _Foo_shine _Bar_twinkle Runtime static information

Dynamic binding Finding the right method implementation class Foo { … void rise() {…} void shine() {…} } class Main { void main() { Foo f = new Bar(); f.rise(); } Foo dispatch vector Bar dispatch vector class Bar extends Foo { void rise() {…} } _Foo_rise _Bar_rise _Foo_shine _Foo_shine _Bar_twinkle Finding the right method implementation Done at runtime according to object type Using the Dispatch Vector (a.k.a. Dispatch Table)

Dispatch vectors in depth class Foo { … void rise() {…} void shine() {…} } class Bar extends Foo { void rise() {…} } class Main { void main() { Foo f = new Bar(); f.rise(); } 1 Pointer to Bar f DVPtr rise _Bar_rise Pointer to Foo inside Bar x shine _Bar_shine y Bar Dispatch vector Method code z Object layout Vector contains addresses of methods Indexed by method-id number A method signature has the same id number for all subclasses

Dispatch vectors in depth class Foo { … void rise() {…} void shine() {…} } class Bar extends Foo { void rise() {…} } class Main { void main() { Foo f = new Foo(); f.rise(); } 1 Pointer to Foo f DVPtr rise _Foo_rise x shine _Foo_shine y Foo Dispatch vector Object layout

Object creation Foo f = new Bar(); x y rise x shine y DispacthVectorPtr LIR translation computes object for each class type size of object |Foo| = |x|+|y|+|z|+|DVPtr| = 1+1+1 +1= 4 (16 bytes) x y Runtime memory layout for object of class Foo LIR translation computes object for each class type field offsets and method offsets for DispatchVector Field offsets DispacthVectorPtr Method offsets Library __allocateObject(16),R1 MoveField R1.0,_Bar_DV Move R1,f x rise 1 y shine 2 1 Compile time information for Foo class type generated during LIR translation Label generated for class type Bar during LIR translation

See you next week