intermediate representation construction in a nutshell
play

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


  1. Intermediate Representation Construction in a Nutshell Christoph Mallon and Johannes Doerfert 18. Dezember 2015 1 / 17

  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

  3. Code Generation for a Constant 1 Constant : : makeRValue () { return createConstantNode ( value ) ; } 3 / 17

  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

  5. Code Generation for + + α β ◮ Generate code for operands ◮ Then generate code for + Addition : : makeRValue () { l = l e f t − > makeRValue ( ) ; r = right − > makeRValue ( ) ; createAddNode ( l , r ) ; return } 5 / 17

  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

  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 of ∗ α is the R-value of α I n d i r e c t i o n : : makeRValue () { address = operand − > makeRValue ( ) ; createLoadNode ( address ) ; return } I n d i r e c t i o n : : makeLValue () { operand − > makeRValue ( ) ; return } 7 / 17

  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 () { operand − > makeLValue ( ) ; return } Address : : makeLValue () { PANIC( ” i n v a l i d L − value ” ) ; } 8 / 17

  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 Value ∗ Expression : : makeRValue () { v i r t u a l address = makeLValue ( ) ; createLoadNode ( address ) ; return } v i r t u a l Value ∗ Expression : : makeLValue () { PANIC( ” i n v a l i d L − value ” ) ; } 9 / 17

  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

  11. Control-Flow Code generation for < α < β T F < α trueBB falseBB β LessThan : : makeCF( trueBB , falseBB ) { l = l e f t − > makeRValue ( ) ; r = right − > makeRValue ( ) ; cond = createCmpLessThanNode ( l , r ) ; createBranch ( trueBB , falseBB , cond ) ; } 11 / 17

  12. Control-Flow Code generation for && α T F β T F && α trueBB falseBB β ◮ 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

  13. Control-Flow Code Generation for ! α ! F T trueBB falseBB α ◮ To negate the condition, just swap the targets LogicalNegation : : makeCF( trueBB , falseBB ) { operand − > makeCF( falseBB , trueBB ) ; } 13 / 17

  14. Control-Flow Code Generation for Arbitrary Expression α � = 0 T F trueBB falseBB α ◮ Test R-value � = 0 v i r t u a l Expression : : makeCF( trueBB , falseBB ) { PANIC( ” implement t h i s ” ) ; } 14 / 17

  15. R-value Code Generation for Control Flow Expression α T F φ (1 , 0) α ◮ 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

  16. R-value Code Generation for Conditional Expression α T F v 2 : γ v 1 : β ? : γ α φ ( v 1 , v 2 ) β ◮ 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

  17. Control-Flow Code Generation for Conditional Expression α T F γ β T F T F ? : γ α trueBB falseBB β ◮ 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

Recommend


More recommend