Semantic Analysis III + Intermediate Representation I.

37
Semantic Analysis III + Intermediate Representation I

Transcript of Semantic Analysis III + Intermediate Representation I.

Page 1: Semantic Analysis III + Intermediate Representation I.

Semantic Analysis III+

Intermediate Representation I

Page 2: Semantic Analysis III + Intermediate Representation I.

22

Compiler

ICProgram

ic

x86 executable

exeLexicalAnalysi

s

Syntax Analysi

s

Parsing

AST Symbol

Tableetc.

Inter.Rep.(IR)

CodeGeneration

IC compiler

Page 3: Semantic Analysis III + Intermediate Representation I.

33

SubtypingType hierarchy is a treeSubtyping relation ≤

For all types:

For reference types:

A ≤ A

A extends B {…}

A ≤ B

A ≤ B B ≤ C

A ≤ C null ≤ A

Page 4: Semantic Analysis III + Intermediate Representation I.

44

Subtyping

S ≤ T values(S) values(T)

“A value of type S may be used wherever a value of type T is expected”

Page 5: Semantic Analysis III + Intermediate Representation I.

Examples

int ≤ int ?null ≤ A ?null ≤ string ?string ≤ null ?null ≤ boolean ?null ≤ boolean[] ?A[] ≤ B[] ?

55

Page 6: Semantic Analysis III + Intermediate Representation I.

IC rules with subtyping

66

E e1 : T1 E e2 : T2 T1 ≤ T2 or T2 ≤ T1

op {==,!=}

E e1 op e2 : bool

Method invocation rules:

Page 7: Semantic Analysis III + Intermediate Representation I.

Semantics Analysis Flow

77

Page 8: Semantic Analysis III + Intermediate Representation I.

class A { int x; int f(int x) { boolean y; ... }}

class B extends A { boolean y; int t;}

class C { A o; int z;}

IC Program

88

ICClassname = A

Fieldname = x …

Methodname = f

Formalname = x …

LocalVariablevarName = yinitExpr = null …

fields[0] methods[0]

bodyformals[0]

ASTProgramfile = …

classes[0]

ICClassname = Bsuper = A

classes[1]classes[2]

…ICClassname = C

Page 9: Semantic Analysis III + Intermediate Representation I.

class A { int x; int f(int x) { boolean y; ... }}

class B extends A { boolean y; int t;}

class C { A o; int z;}

99

class TypeTable { createUnique … get …}

abstract class Type { String name; boolean subtypeof(Type t) {...}}class IntType extends Type {...}class BoolType extends Type {...}class ArrayType extends Type { Type elemType;}class MethodType extends Type { Type[] paramTypes; Type returnType;}class ClassType extends Type { ICClass classAST;}

IntTypeBoolTypeABCint->int…

TypeTable

Types

Page 10: Semantic Analysis III + Intermediate Representation I.

Types

1010

IntType BoolType

...

TypeTable

ClassTypename = A

ClassTypename = B

ClassTypename = C

MethodType retTypeparamTypes

super

ICClassname = A

Fieldname = xtype = IntType

Methodname = f

Formalname = xtype = IntType

LocalVariablename = yinitExpr = nulltype = BoolType

fields[0] methods[0]

bodyformals[0]

ASTProgramfile = …

classes[0]

ICClassname = Bsuper = A

classes[1]classes[2]

…ICClassname = C

Page 11: Semantic Analysis III + Intermediate Representation I.

Types

DataTypes TableSubtyping relation…

Partial CorrectnessAcyclic HierarchyNo Redefinitions…

1111

Page 12: Semantic Analysis III + Intermediate Representation I.

Symbol tables

1212

ICClassname = A

Fieldname = xtype = IntType

Methodname = f

Formalname = xtype = IntType

LocalVariablename = yinitExpr = nulltype = BoolType

fields[0] methods[0]

bodyformals[0]

ASTProgramfile = …

classes[0]

ICClassname = Bsuper = A

classes[1]classes[2]

…ICClassname = C

A CLASS

B CLASS

C CLASS

Global symtab

x FIELD IntType

f METHOD

int->int

A symtabo CLAS

SA

z FIELD IntType

C symtab

t FIELD IntType

y FIELD BoolType

B symtabx PARAM IntType

y VAR BoolType

this VAR A

$ret RET_VAR

IntType

f symtab

Locationname = xtype = ?

… Resolve each id to a symbolcheck scope rules:illegal symbol re-definitions,illegal shadowing,illegal use of undefined symbols

Page 13: Semantic Analysis III + Intermediate Representation I.

Symbol tables

1313

ICClassname = A

Fieldname = xtype = IntType

Methodname = f

Formalname = xtype = IntType

LocalVariablename = yinitExpr = nulltype = BoolType

fields[0] methods[0]

bodyformals[0]

ASTProgramfile = …

classes[0]

ICClassname = Bsuper = A

classes[1]classes[2]

…ICClassname = C

A CLASS

B CLASS

C CLASS

Global symtab

x FIELD IntType

f METHOD

int->int

A symtabo CLAS

SA

z FIELD IntType

C symtab

t FIELD IntType

y FIELD BoolType

B symtabx PARAM IntType

y VAR BoolType

this VAR A

$ret RET_VAR

IntType

f symtab

Locationname = xtype = ?

this belongs to

method scope

$ret can be used later for type-checking return statements

Page 14: Semantic Analysis III + Intermediate Representation I.

Symbol tables

DataSymbol tables…

Partial CorrectnessScope rules…

1414

Page 15: Semantic Analysis III + Intermediate Representation I.

Type Checking

Infer types for expression nodesCheck rules

1515

Page 16: Semantic Analysis III + Intermediate Representation I.

Miscellaneous semantic checks

Single main methodbreak/continue inside loopsreturn on every control path…

1616

Page 17: Semantic Analysis III + Intermediate Representation I.

Semantic Analysis

Program is “correct”Data

1717

Page 18: Semantic Analysis III + Intermediate Representation I.

Intermediate Representation I

1818

Page 19: Semantic Analysis III + Intermediate Representation I.

1919

Compiler

ICProgram

ic

x86 executable

exeLexicalAnalysi

s

Syntax Analysi

s

Parsing

AST Symbol

Tableetc.

Inter.Rep.(IR)

CodeGeneration

IC compiler

Page 20: Semantic Analysis III + Intermediate Representation I.

2020

Lexical analyzer

tomatoes + potatoes + carrots

tomatoes,PLUS,potatoes,PLUS,carrots,EOF

Parser

symbol kind type

tomatoes var int

potatoes var int

carrots var intLocationExprid=tomatoes

AddExprleft right

AddExprleft right

LocationExprid=potatoes id=carrots

LocationExpr

Id Type obj

int O1

boolean O2

Foo O3

Symtab hierarchy Global type table

A E1 : T[]

A E1.length : intType checking Additional semantic checks

Move tomatoes,R1

Move potatoes,R2

Add R2,R1

...

LIR

Page 21: Semantic Analysis III + Intermediate Representation I.

2121

Intermediate representation

Allows language-independent, machine independent optimizations and transformations Easy to translate from AST Easy to translate to assembly Narrow interface

AST IR

Pentium

Java bytecode

Sparc

optimize

Page 22: Semantic Analysis III + Intermediate Representation I.

2222

Multiple IRs

Some optimizations require high-level structure e.g. optimizations of nested “for” loops

Others more appropriate on low-level code e.g. register allocation

Solution: use multiple IR stages

AST LIR

Pentium

Java bytecode

Sparc

optimize

HIR

optimize

Page 23: Semantic Analysis III + Intermediate Representation I.

Machine Optimizations

Some optimizations take advantage of the features of the target machine

2323

AST LIR

Pentium

Java bytecode

Sparc

optimize

HIR

optimize

optimize

optimize

optimize

Page 24: Semantic Analysis III + Intermediate Representation I.

2424

High-level IR (HIR)

High-level intermediate representation is essentially the AST Must be expressive for all input languages

Preserves high level constructs If, while, for, switch, …

Allows high-level optimizations

Page 25: Semantic Analysis III + Intermediate Representation I.

2525

Low-level IR (LIR)

Low-level representation is essentially an abstract machine language

Low-level language constructs jumps, conditional jumps, …

Allows optimizations specific to these constructs

Page 26: Semantic Analysis III + Intermediate Representation I.

2626

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

...

Note 1: rightmost operand = operation destinationNote 2: two register instr - second operand doubles as source and destination

LIR instructions

Immediate(constant)

Memory(variable)

Page 27: Semantic Analysis III + Intermediate Representation I.

2727

Example

x = 42;

while (x > 0) {

x = x - 1;

}

Move 42,R1

Move R1,x

_test_label:

Move x,R1

Compare 0,R1

JumpLE _end_label

Move x,R1

Move 1,R2

Sub R2,R1

Move R1,x

Jump _test_label

_end_label:

Page 28: Semantic Analysis III + Intermediate Representation I.

2828

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 Use temporary variables (LIR registers) to store

intermediate values during translation

Page 29: Semantic Analysis III + Intermediate Representation I.

2929

TR[e1 OP e2]

R1 := TR[e1]

R2 := TR[e2]

R3 := R1 OP R2

TR[OP e] R1 := TR[e]

R2 := OP R1

Binary operations(arithmetic and comparisons)

Fresh virtual (LIR) register

generated by translation

Shortcut notationto indicate target

registerNOT LIR instruction

Unary operations

Translating expressions

Page 30: Semantic Analysis III + Intermediate Representation I.

3030

LocationEx

id = x

AddExprleft right

ValueExpr

val = 42

visit

visit(left)

visit(right)

TR[x + 42]

Move x, R1

Move 42, R2

Add R2, R1

Move x, R1 Move 42, R2

Add R2, R1

Translating expressions – example

Page 31: Semantic Analysis III + Intermediate Representation I.

3131

Translating (short-circuit) OR

TR[e1 OR e2]R1 := TR[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)

Fresh labels generated

during translation

Page 32: Semantic Analysis III + Intermediate Representation I.

3232

Translating (short-circuit) AND

TR[e1 AND e2]R1 := TR[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)

Page 33: Semantic Analysis III + Intermediate Representation I.

3333

Translating array and field access

TR[e1[e2]]R1 := TR[e1]

R2 := TR[e2]

MoveArray R1[R2], R3

TR[e1.f] R1 := TR[e1]

MoveField R1.cf,R3

Page 34: Semantic Analysis III + Intermediate Representation I.

3434

Translating statement block

TR[s1; s2; … ; sN] TR[s1]

TR[s2]

TR[s3]

TR[sN]

Page 35: Semantic Analysis III + Intermediate Representation I.

3535

Translating if-then-else

TR[if (e) then s1 else s2]

R1 := TR[e]

Compare 0,R1

JumpTrue _false_label

TR[s1]

Jump _end_label

_false_label:

TR[s2]

_end_label:

Page 36: Semantic Analysis III + Intermediate Representation I.

3636

Translating if-then

TR[if (e) then s]R1 := TR[e]

Compare 0,R1

JumpTrue _end_label

TR[s]

_end_label:

Page 37: Semantic Analysis III + Intermediate Representation I.

3737

Translating while

TR[while (e) s]_test_label:

R1 := TR[e]

Compare 0,R1

JumpTrue _end_label

TR[s]

Jump _test_label

_end_label