Intermediate Representation Construction in a Nutshell Christoph - - PowerPoint PPT Presentation

intermediate representation construction in a nutshell
SMART_READER_LITE
LIVE PREVIEW

Intermediate Representation Construction in a Nutshell Christoph - - PowerPoint PPT Presentation

Intermediate Representation Construction in a Nutshell Christoph Mallon and Johannes Doerfert 18. Dezember 2015 1 / 17 Code Generation for Expresssions = y + x 1 Do not evaluate expression Create code , which, when run , evaluates


slide-1
SLIDE 1

Intermediate Representation Construction in a Nutshell

Christoph Mallon and Johannes Doerfert

  • 18. Dezember 2015

1 / 17

slide-2
SLIDE 2

Code Generation for Expresssions

= y + x 1

◮ Do not evaluate expression ◮ Create code, which, when run, evaluates the expression ◮ IR construction is code generation, just for a virtual machine ◮ Recursively create code for expressions ◮ Create code for operands, then create code for current node ◮ Same order as evaluating, but generating code instead

2 / 17

slide-3
SLIDE 3

Code Generation for a Constant

1 Constant : : makeRValue () { return createConstantNode ( value ) ; }

3 / 17

slide-4
SLIDE 4

Different Code Generation in Different Contexts

expr = . . . /∗ L−value ∗/ . . . = expr /∗ R−value ∗/ i f ( expr ) /∗ Control flow ∗/

◮ Code generated depends on context, where the expression

appears

◮ L-value: address of the object denoted by an expression ◮ R-value: value of an expression ◮ Control Flow: Branch depending on result of an expression ◮ Different contexts call each other recursively for operands

4 / 17

slide-5
SLIDE 5

Code Generation for +

+ α β

◮ Generate code for operands ◮ Then generate code for +

Addition : : makeRValue () { l = l e f t − >makeRValue ( ) ; r = right − >makeRValue ( ) ; return createAddNode ( l , r ) ; }

5 / 17

slide-6
SLIDE 6

Code Generation for =

= α β

◮ L-value: address of the object denoted by an expression ◮ R-value: value of an expression ◮ L and R stand for left and right hand side (of assignment) ◮ Assignment happens as side effect of the expression

Assignment : : makeRValue () { address = l e f t − >makeLValue ( ) ; value = right − >makeRValue ( ) ; createStoreNode ( address , value ) ; return value ; }

6 / 17

slide-7
SLIDE 7

Code Generation for ∗ (Indirection)

∗ α

◮ R-value of ∗α is the value loaded from the address denoted by

the R-value of α

◮ Address of the object denoted by ∗α is the value of α: L-value

  • f ∗α is the R-value of α

I n d i r e c t i o n : : makeRValue () { address = operand− >makeRValue ( ) ; return createLoadNode ( address ) ; } I n d i r e c t i o n : : makeLValue () { return

  • perand−

>makeRValue ( ) ; }

7 / 17

slide-8
SLIDE 8

Code Generation for & (Address)

& α

◮ Value of &α is the address of the object denoted by α:

R-value of &α is the L-value of α

◮ &α does not denote an object: &α is not an L-value

Address : : makeRValue () { return

  • perand−

>makeLValue ( ) ; } Address : : makeLValue () { PANIC( ” i n v a l i d L−value ” ) ; }

8 / 17

slide-9
SLIDE 9

Connection between L-value and R-value

◮ R-value is just loading from L-value ◮ Unfortunately most expressions are not an L-value, i.e. do not

denote an object v i r t u a l Value ∗ Expression : : makeRValue () { address = makeLValue ( ) ; return createLoadNode ( address ) ; } v i r t u a l Value ∗ Expression : : makeLValue () { PANIC( ” i n v a l i d L−value ” ) ; }

9 / 17

slide-10
SLIDE 10

Control-Flow Code Generation for Condition

i f (C) S1 else S2

◮ If C evaluates to = 0 continue at S1 ◮ Otherwise continue at S2 ◮ Label/Basic block of S1 and S2 are input for code generation

v i r t u a l void Expression : : makeCF( trueBB , falseBB ) ;

10 / 17

slide-11
SLIDE 11

Control-Flow Code generation for <

< α β α < β trueBB falseBB T F LessThan : : makeCF( trueBB , falseBB ) { l = l e f t − >makeRValue ( ) ; r = right − >makeRValue ( ) ; cond = createCmpLessThanNode ( l , r ) ; createBranch ( trueBB , falseBB , cond ) ; }

11 / 17

slide-12
SLIDE 12

Control-Flow Code generation for &&

&& α β α β trueBB falseBB T F T F

◮ Lazy evaluation is part of semantics: β might have side effects ◮ Stop evaluation if value of left hand side determines result

LogicalAnd : : makeCF( trueBB , falseBB ) { extraBB = c r e a t e B a s i c B l o c k ( ) ; l e f t − >makeCF( extraBB , falseBB ) ; setCurrentBB ( extraBB ) ; r i g h t − >makeCF( trueBB , falseBB ) ; }

12 / 17

slide-13
SLIDE 13

Control-Flow Code Generation for !

! α α trueBB falseBB F T

◮ To negate the condition, just swap the targets

LogicalNegation : : makeCF( trueBB , falseBB ) {

  • perand−

>makeCF( falseBB , trueBB ) ; }

13 / 17

slide-14
SLIDE 14

Control-Flow Code Generation for Arbitrary Expression

α α = 0 trueBB falseBB T F

◮ Test R-value = 0

v i r t u a l Expression : : makeCF( trueBB , falseBB ) { PANIC( ” implement t h i s ” ) ; }

14 / 17

slide-15
SLIDE 15

R-value Code Generation for Control Flow Expression

α α φ(1, 0) T F

◮ Control flow operators produce 1 and 0 ◮ Select the value depending on whether the true or false basic

block was reached ControlFlowExpression : : makeRValue () { PANIC( ” implement t h i s ” ) ; }

15 / 17

slide-16
SLIDE 16

R-value Code Generation for Conditional Expression

? : α β γ α v1: β v2: γ φ(v1, v2) T F

◮ First evaluate condition α to control flow ◮ Then either evaluate consequence β or alternative γ ◮ Pick result using a φ

C o n d i t i o n a l E x p r e s s i o n : : makeRValue () { PANIC( ” implement t h i s ” ) ; }

16 / 17

slide-17
SLIDE 17

Control-Flow Code Generation for Conditional Expression

? : α β γ α β γ trueBB falseBB T F T F T F

◮ First evaluate condition α to control flow ◮ Then either evaluate consequence β or alternative γ to

control flow C o n d i t i o n a l E x p r e s s i o n : : makeCF( trueBB , falseBB ) { PANIC( ” implement t h i s ” ) ; }

17 / 17