Unformatted text preview:

CSc 453Compilers and Systems Software14 : Intermediate Code IIDepartment of Computer ScienceUniversity of [email protected]° 2009 Christian CollbergIntermediate Code GenerationIntermediate Code GenerationDecorated ASTIntermediateCode GeneratorCode (expressiontrees, tuples,DAGs...)IntermediateMachine CodeGeneratorset L, %o0ld [%o0], %l1Lexing,Parsing,SemanticAnalysisMachine CodeAfter semantic analysis we traverse the AST and emit thecorrect intermediate code.Generating Expression TreesAssignType=Des ExprId="R"NameType=RealRopLopOp="+"Binary Type=RealDecoratedASTNameId="C"Type=RealNameId="J"Type=IntIntConstType=IntValue=13RopLopOp="*"Binary Type=IntTree−WalkTransformerStoreVarLevel=0Addr=0x10LoadIntermediateCode ExpressionTreeIntegervalue = 13MultLoadVarLevel=1Addr=0x14FloatVarLevel=0Addr=0xAPlusAn expression tree is generated from an AST. The float caneasily be inserted since all types are known in the AST.Generating QuadruplesTo generate quadruples from an AST we make an extratraversal of the tree after semantic analysis.Each AST node in an expression sub-tree is given an attribute⇑Place:SymbolT which represents the name of the identifieror temporary in which the value of the subtree will becomputed.Generating Quadruples. . .PROCEDURE Program (n: Node);Decl(n.DeclSeq);Stat(n.StatSeq);END;PROCEDURE Decl (n: Node);IF n.Kind = ProcDecl THENDecl(n.Locals);Decl(n.Next);Stat(n.StatSeq);ENDIFEND;Generating Quadruples. . .NewTemp generates a new temporary var.PROCEDURE Stat (n: Node);IF n.Kind = Assign THENExpr(n.Des);Expr(n.Expr);Emit(n.Des.Place ’:=’ n.Expr.Place);Stat(n.Next);ENDIFEND;Generating Quadruples. . .PROCEDURE Expr (n: Node);IF n.Kind = Add THENExpr(n.LOP);Expr(n.ROP);n.Place := NewTemp();Emit(n.Place ’:=’ n.LOP.Place ’+’ n.ROP.Place);ELSIF n.Kind = VarRef THENn.Place := n.Symbol;ENDIFEND;Generating Quadruples. . .Symbol Table EntryDes ExprAssignId="R"NamePlace=NameId="C"Place=NameId="J"Place=IntConstValue=13Place=RopLopOp="+"BinaryPlace=RopLopOp="*"BinaryPlace=R := T2T2 := C + T1T1 := J * 13(R,VAR,INT)(T1,TEMP,INT)(T2,TEMP,INT)(C,VAR,INT)(J,VAR,INT) (13,CONST,INT)QuadruplesAttribute Grammar NotationAttribute GrammarThe tree-walker is better described using an attributegrammar notation.Assign ::= Des:Expr Expr:Expr{ Emit(n.Des.Place ’:=’ n.Expr.Place); }Add ::= LOP:Expr ROP:Expr{ n.Place := NewTemp();Emit(n.Place ’:=’ n.LOP.Place ’+’ n.ROP.Place);}Name ::= Ident{ n.Place := n.Symbol; }SummaryReadings and ReferencesRead Louden:Generating Intermediate Code


View Full Document

UA CSC 453 - Intermediate Code Generation

Download Intermediate Code Generation
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Intermediate Code Generation and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Intermediate Code Generation 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?