Top Banner
Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University
55

Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

Dec 18, 2015

Download

Documents

Oswin Hardy
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

Fall 2014-2015 Compiler PrinciplesLecture 7: Intermediate Representation

Roman ManevichBen-Gurion University

Page 2: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

2

Tentative syllabusFrontEnd

ScanningTop-down

Parsing (LL)

Bottom-upParsing (LR)

AttributeGrammars

mid-term exam

Page 3: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

3

Previously• Becoming parsing ninjas– Going from text to an Abstract Syntax Tree

By Admiral Ham [GFDL (http://www.gnu.org/copyleft/fdl.html) or CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0)], via Wikimedia Commons

Page 4: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

From scanning to parsing59 + (1257 * xPosition)

) id * num ( + num

Lexical Analyzer

program text

token stream

Parser

Grammar:E id E numE E + EE E * EE ( E ) +

num

num x

*

Abstract Syntax Tree

validsyntaxerror

4

Page 5: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

5

Agenda• Why compilers need Intermediate

Representations (IR)• Translating from abstract syntax (AST) to IR– Three-Address Code

Page 6: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

Role of intermediate representation

• Bridge between front-end and back-end

• Allow implementing optimizations independent of source language and executable (target) language

High-levelLanguage

(scheme)

Executable

Code

LexicalAnalysis

Syntax Analysis

Parsing

AST SymbolTableetc.

Inter.Rep.(IR)

CodeGeneration

6

Page 7: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

7

Motivation for intermediate representation

Page 8: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

Intermediate representation• A language that is between the source language and

the target language– Not specific to any source language of machine language

• Goal 1: retargeting compiler components for different source languages/target machines

8

C++ IR

Pentium

Java bytecode

SparcPyhton

Java

Page 9: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

Intermediate representation• A language that is between the source language and

the target language– Not specific to any source language of machine language

• Goal 1: retargeting compiler components for different source languages/target machines

• Goal 2: machine-independent optimizer– Narrow interface: small number of node types

(instructions)

9

C++ IR

Pentium

Java bytecode

SparcPyhton

Javaoptimize

Lowering Code Gen.

Page 10: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

Multiple IRs

• Some optimizations require high-level structure

• Others more appropriate on low-level code• Solution: use multiple IR stages

AST LIR

Pentium

Java bytecode

Sparc

optimize

HIR

optimize

10

Page 11: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

11

Multiple IRs example

ElixirProgram

AutomatedReasoning

(Boogie+Z3)

DeltaInferencer

Query Answer

ElixirProgram+ delta

AutomatedPlanner

ILSynthesizer

PlanningProblem Plan

LIR

C++backend

C++ code

GaloisLibrary

HIRLowering HIR

Elixir – a language for parallel graph algorithms

Mini-project on parallel graph algorithms

Page 12: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

12

AST vs. LIR for imperative languages

AST

• Rich set of language constructs• Rich type system• Declarations: types (classes,

interfaces), functions, variables• Control flow statements: if-

then-else, while-do, break-continue, switch, exceptions

• Data statements: assignments, array access, field access

• Expressions: variables, constants, arithmetic operators, logical operators, function calls

LIR

• An abstract machine language• Very limited type system• Only computation-related code• Labels and conditional/

unconditional jumps, no looping

• Data movements, generic memory access statements

• No sub-expressions, logical as numeric, temporaries, constants, function calls – explicit argument passing

Page 13: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

13

Lowering to three address code

Page 14: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

14

Three-Address Code IR

• A popular form of IR• High-level assembly where instructions have

at most three operands

• There exist other types of IR– For example, IR based on acyclic graphs – more

amenable for analysis and optimizations

Chapter 8

Page 15: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

TAC sub-expressions example

int a;int b;int c;int d;a := b + c + d;b := a * a + b * b;

15

Source LIR (unoptimized)

_t0 := b + c;a := _t0 + d;_t1 := a * a;_t2 := b * b;b := _t1 + _t2;

Where have the declarations gone?

Page 16: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

TAC sub-expressions example

int a;int b;int c;int d;a := b + c + d;b := a * a + b * b;

_t0 := b + c;a := _t0 + d;_t1 := a * a;_t2 := b * b;b := _t1 + _t2;

16

Source LIR (unoptimized)

Temporaries explicitly store intermediate values resulting from sub-expressions

Page 17: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

17

Elements of TAC-based IR

Page 18: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

18

Variable assignments

• var := constant;• var1 := var2;

• var1 := var2 op var3;

• var1 := constant op var2;

• var1 := var2 op constant;

• var := constant1 op constant2;

• Permitted operators are +, -, *, /, %

Page 19: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

19

Booleans

• Boolean variables are represented as integers that have zero or nonzero values

• In addition to the arithmetic operator, TAC supports <, ==, ||, and &&

• How might you compile the following?

b := (x <= y); _t0 := x < y;_t1 := x == y;b := _t0 || _t1;

Page 20: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

20

Booleans

• Boolean variables are represented as integers that have zero or nonzero values

• In addition to the arithmetic operator, TAC supports <, ==, ||, and &&

• How might you compile the following?

b := (x <= y); _t0 := x < y;_t1 := x == y;b := _t0 + _t1;

Page 21: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

21

Unary operators

• How would you compile the following assignments from unary statements?

y := -x;

z := !w;

y := 0 - x;

z := w == 0;

y := -1 * x;

Page 22: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

Control flow instructions• Label introduction

_label_name:Indicates a point in the code that can be jumped to

• Unconditional jump: go to instruction following label LGoto L;

• Conditional jump: test condition variable t;if 0, jump to label L

IfZ t Goto L;• Similarly : test condition variable t;

if 1, jump to label LIfNZ t Goto L;

22

Page 23: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

Control-flow example – conditionsint x;int y;int z;

if (x < y) z := x;else z := y;z := z * z;

?

23

Page 24: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

Control-flow example – conditionsint x;int y;int z;

if (x < y) z := x;else z := y;z := z * z;

_t0 := x < y;IfZ _t0 Goto _L0;z := x;Goto _L1;

_L0:z := y;

_L1:z := z * z;

24

Page 25: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

Control-flow example – loops

int x;int y;

while (x < y) {x := x * 2;

{

y := x;

?

25

Page 26: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

Control-flow example – loops

int x;int y;

while (x < y) {x := x * 2;

{

y := x;

_L0:_t0 := x < y;IfZ _t0 Goto _L1;x := x * 2;Goto _L0;

_L1:y := x;

26

Page 27: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

27

Data as control flow

x := y || z; x := yIfZ z Goto _L1;x := 1;_L1:

Page 28: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

Functions• Store local variables/temporaries in a stack• A function call instruction pushes arguments to stack and

jumps to the function labelA statement x:=f(a1,…,an); looks like

Push a1; … Push an;Call f;Pop x; // copy returned value

• Returning a value is done by pushing it to the stack (return x;)

Push x;• Return control to caller (and roll up stack)

Return;

28

Page 29: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

29

A logical stack frame

Param N

Param N-1

Param 1

_t0

_tk

x

y

Parameters(actual arguments)

Locals and temporaries

Stack frame for function f(a1,…,aN)

Page 30: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

Functions example

int SimpleFn(int z) {int x, y;x := x * y * z;return x;

{

void main() {int w;w := SimpleFn(137);

{

_SimpleFn:Pop z;_t0 := x * y;_t1 := _t0 * z;x := _t1;Push x;Return;

main:_t0 := 137;Push _t0;Call _SimpleFn;Pop w;

30

Page 31: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

Memory access instructions• Copy instruction: a = b• Load/store instructions:

a := *b *a := b• Address of instruction a := &b• Array accesses:

a := b[i] a[i] := b• Field accesses:

a := b[f] a[f] := b• Memory allocation:

a := alloc(size)– Sometimes left out (e.g., malloc is a procedure in C)

31

constant constant

Page 32: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

32

Lowering AST to TAC via syntax directed translation

Page 33: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

33

TAC generation

• At this stage in compilation, we have– an AST– annotated with scope information– and annotated with type information

• To generate TAC for the program, we do recursive tree traversal– Generate TAC for any subexpressions and

substatements– Using the result, generate TAC for the overall

expression (bottom-up manner)

Page 34: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

34

TAC generation for expressions

• Define a function cgen(expr) that generates TAC that computes an expression, stores it in a temporary variable, then hands back the name of that temporary

• Define cgen directly for atomic expressions (constants, this, identifiers, etc.)

• Define cgen recursively for compound expressions (binary operators, function calls, etc.)

Page 35: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

35

cgen for basic expressions

cgen(k) = { // k is a constant Choose a new temporary t Emit( t := k ) Return t{

cgen(id) = { // id is an identifier Choose a new temporary t Emit( t := id ) Return t{

Page 36: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

36

Naive cgen for binary expressions

• Maintain a counter for temporaries in c• Initially: c = 0• cgen(e1 op e2) = {

Let A = cgen(e1) c = c + 1 Let B = cgen(e2) c = c + 1 Emit( _tc := A op B; ) Return _tc}

The translation emits code to evaluate e1 before e2. Why is that?

Page 37: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

Example: cgen for binary expressions

37

cgen( (a*b)-d)

Page 38: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

Example: cgen for binary expressions

38

c = 0cgen( (a*b)-d)

Page 39: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

Example: cgen for binary expressions

39

c = 0cgen( (a*b)-d) = { Let A = cgen(a*b) c = c + 1 Let B = cgen(d) c = c + 1 Emit( _tc := A - B; ) Return _tc }

Page 40: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

Example: cgen for binary expressions

40

c = 0cgen( (a*b)-d) = { Let A = { Let A = cgen(a) c = c + 1 Let B = cgen(b) c = c + 1 Emit( _tc := A * B; ) Return tc } c = c + 1 Let B = cgen(d) c = c + 1 Emit( _tc := A - B; ) Return _tc }

Page 41: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

Example: cgen for binary expressions

41

c = 0cgen( (a*b)-d) = { Let A = { Let A = { Emit(_tc := a;), return _tc } c = c + 1 Let B = { Emit(_tc := b;), return _tc } c = c + 1 Emit( _tc := A * B; ) Return _tc } c = c + 1 Let B = { Emit(_tc := d;), return _tc } c = c + 1 Emit( _tc := A - B; ) Return _tc }

Code

here A=_t0

Page 42: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

Example: cgen for binary expressions

42

c = 0cgen( (a*b)-d) = { Let A = { Let A = { Emit(_tc := a;), return _tc } c = c + 1 Let B = { Emit(_tc := b;), return _tc } c = c + 1 Emit( _tc := A * B; ) Return _tc } c = c + 1 Let B = { Emit(_tc := d;), return _tc } c = c + 1 Emit( _tc := A - B; ) Return _tc }

Code_t0:=a;

here A=_t0

Page 43: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

Example: cgen for binary expressions

43

c = 0cgen( (a*b)-d) = { Let A = { Let A = { Emit(_tc := a;), return _tc } c = c + 1 Let B = { Emit(_tc := b;), return _tc } c = c + 1 Emit( _tc := A * B; ) Return _tc } c = c + 1 Let B = { Emit(_tc := d;), return _tc } c = c + 1 Emit( _tc := A - B; ) Return _tc }

Code_t0:=a;_t1:=b;here A=_t0

Page 44: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

Example: cgen for binary expressions

44

c = 0cgen( (a*b)-d) = { Let A = { Let A = { Emit(_tc := a;), return _tc } c = c + 1 Let B = { Emit(_tc := b;), return _tc } c = c + 1 Emit( _tc := A * B; ) Return _tc } c = c + 1 Let B = { Emit(_tc := d;), return _tc } c = c + 1 Emit( _tc := A - B; ) Return _tc }

Code_t0:=a;_t1:=b;_t2:=_t0*_t1

here A=_t0

Page 45: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

Example: cgen for binary expressions

45

c = 0cgen( (a*b)-d) = { Let A = { Let A = { Emit(_tc := a;), return _tc } c = c + 1 Let B = { Emit(_tc := b;), return _tc } c = c + 1 Emit( _tc := A * B; ) Return _tc } c = c + 1 Let B = { Emit(_tc := d;), return _tc } c = c + 1 Emit( _tc := A - B; ) Return _tc }

Code_t0:=a;_t1:=b;_t2:=_t0*_t1

here A=_t0

here A=_t2

Page 46: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

Example: cgen for binary expressions

46

c = 0cgen( (a*b)-d) = { Let A = { Let A = { Emit(_tc := a;), return _tc } c = c + 1 Let B = { Emit(_tc := b;), return _tc } c = c + 1 Emit( _tc := A * B; ) Return _tc } c = c + 1 Let B = { Emit(_tc := d;), return _tc } c = c + 1 Emit( _tc := A - B; ) Return _tc }

Code_t0:=a;_t1:=b;_t2:=_t0*_t1_t3:=d;

here A=_t0

here A=_t2

Page 47: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

Example: cgen for binary expressions

47

c = 0cgen( (a*b)-d) = { Let A = { Let A = { Emit(_tc := a;), return _tc } c = c + 1 Let B = { Emit(_tc := b;), return _tc } c = c + 1 Emit( _tc := A * B; ) Return _tc } c = c + 1 Let B = { Emit(_tc := d;), return _tc } c = c + 1 Emit( _tc := A - B; ) Return _tc }

Code_t0:=a;_t1:=b;_t2:=_t0*_t1_t3:=d;_t4:=_t2-_t3

here A=_t0

here A=_t2

Page 48: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

Num

val = 5

AddExprleft right

Ident

name = x

visit

visit(left)

visit(right)

cgen(5 + x)

t1 := 5;

t2 := x;

t := t1 + t2;

t1:=5 t2:=x

t := t1 + t2

cgen as recursive AST traversal

48

Page 49: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

cgen for short-circuit disjunction

cgen(e1 || e2)Emit(_t1 := 0;)Emit(_t2 := 0;)Let Lafter be a new label

Let _t1 = cgen(e1)Emit( IfNZ _t1 Goto Lafter)

Let _t2 = cgen(e2)Emit( Lafter: )

Emit( _t := _t1 || _t2; )Return _t

49

Page 50: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

50

cgen for statements

• We can extend the cgen function to operate over statements as well

• Unlike cgen for expressions, cgen for statements does not return the name of a temporary holding a value– (Why?)

Page 51: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

51

cgen for simple statements

cgen(expr;) = { cgen(expr) {

Page 52: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

cgen for if-then-else

52

cgen(if (e) s1 else s2)Let _t = cgen(e)Let Ltrue be a new label

Let Lfalse be a new label

Let Lafter be a new label

Emit( IfZ _t Goto Lfalse; )

cgen(s1)

Emit( Goto Lafter; )

Emit( Lfalse: )

cgen(s2)

Emit( Lafter: )

Page 53: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

cgen for while loops

53

cgen(while (expr) stmt) Let Lbefore be a new labelLet Lafter be a new labelEmit( Lbefore: )Let t = cgen(expr)Emit( IfZ t Goto Lafter; )cgen(stmt)Emit( Goto Lbefore; )Emit( Lafter: )

Page 54: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

Exercise: cgen for try-catch

54

cgen(try { try-stmt } catch (v) { catch-stmt })

?

Page 55: Compiler Principles Fall 2014-2015 Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.

Next lecture:IR part 2