资源描述:
《东南大学 编译原理 教学教案 Cp8.ppt》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Chapter8IntermediateCodeGenerationZhaiYuqingZhouXiaoyuOverviewPositionofIntermediateCodeGeneratorparsertokenstreamStaticcheckersyntaxtreeintermediatecodegeneratorsyntaxtreeintermediatecodecodegeneratorImplementationofIntermediateCodeGeneratorSyntax-directedtranslation,foldedintoparsingIntermediateR
2、epresentationsSyntaxtreePostfixnotationThree-addresscodeQuadrupleBenefitsforUsingaMachine-independentIntermediateFormAmachine-independentcodeoptimizercanbeappliedtotheintermediaterepresentation.Three-addressCode(TAC)Three-addressCode(TAC)Asequenceofstatementsofthegeneralformx=yopz.Thereisonlyoneoper
3、atorontherightsideofastatement.Here,x,y,zarenames,constants,orcompiler-generatedtemporaries;opstandsforanyoperator.Three-addressCode(TAC)Eachthree-addresscodestatementcontainsthreeaddresses,twofortheoperandsandonefortheresult.Threeaddresscodeisalinearizedrepresentationofasyntaxtree.TypesofTACX=yopzX
4、=opyX=ygotoLifxrelopygotoLparamxcallp,nreturnyTypesofTACx=y[i]x[i]=yx=&y/*valueofxisthelocationofy*/x=*y*x=ySyntax-directedTranslationintoTACWecanuseS-attributeddefinitiontogeneratethree-addresscodeforassignmentstatement.Wecreateanewnameeverytimeatemporaryisneeded.ProductionSemanticRulesSid=ES.code
5、=E.code
6、
7、gen(id.place‘=’E.place)EE1+E2E.place=newtemp();E.code=E1.code
8、
9、E2.code
10、
11、gen(E.place,’=’,E1.place‘+’E2.place)EidE.place=id.placeE.code=‘’QuadruplesImplementationsofThree-addressStatementsAthree-addressstatementisanabstractformofintermediatecodesQuadruples(op,arg1,arg2,result)Triples(n)(op,
12、arg1,arg2)(m)(op,(n),arg)AdvantagesofQuadruplesGoodforoptimizingAssignmentStatementsfunctionsNEWTEMP()GEN(OP,ARG1,ARG2,RESULT)SemanticRulesforQuadrupleCodeGenerationAi=E{GEN(=,E•PLACE,_,i.entry}E-E1{T=NEWTEMP();GEN(@,E1•PLACE,_,T);E•PLACE=T}EE1*E2{T=NEWTEMP();GEN(*,E1•PLACE,E2•PLACE,T);E•PLACE=T}
13、SemanticRulesforQuadrupleCodeGenerationEE1+E2{T=NEWTEMP();GEN(+,E1•PLACE,E1•PLACE,T);E•PLACE=T}E(E1){E•PLACE=E1•PLACE}EiE•PLACE=i.entryAddressingArrayElementsAddressingArrayElementsOne-dimensionala