版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Chapter9
CodeGeneration
Thephaseafterintermediaterepresentationincompileriscodegeneration.Theinputforcodegenerationistheintermediaterepresentationandtheoutputofitisequivalenttargetprogram,thepositionofcodegenerationincompilerisshownbyFigure9.1..2Asweknowthatthereareseveralchoicesfortheintermediatelanguage,
including:
postfixnotation,four-address
code,three-addresscodeandsyntaxtrees.
.
Inthischapterweassumethatthesourceprogramhasbeentranslatedintoareasonablyintermediaterepresentation,andthevalueofidentifierappearinginintermediaterepresentationcanbedirectlymanipulated..3Wealsoassumethatthenecessarytypecheckinghasbeentakenplaceandsemanticerrorshavealreadybeendetected.Targetprogramofcodegenerationconsistsofabsolutemachinelanguage,relocatablemachinelanguage,orassemblylanguage.Here,weuseassemblylanguageasthetargetlanguageforreadability..4
SourceprogramSymboltableLexical,syntax,semanticanalyzerandIntermediaterepresentationCodeGenerationFig.9.1Positionofcodegenerationincompiler图9.1代码生成阶段在编译器中的位置CodeoptimizationTargetcode59.1Structureoftargetprogram
Thereare
three
typesoftargetlanguagethatareabsolutemachine-language,relocatedmachine-languageandassemblylanguage.
.Usingabsolutemachine-languageastargetprogramhastheadvantagethatitcanbeplacedinafixedlocationinmemoryandimmediatelyexecuted,portablecodeisakindofabsolutecode..6Arelocatedmachine-languageprogramastargetprogramcangainagreatdealofflexibilityforsubprogramtobecompiledseparately,becauseasetofrelocatableobjectmodulescanbelinkedtogetherandbeloadedforexecutionbyalinkingloader.Assemblylanguageasoutputiseasiertomakecodegenerationprocessduetoitdoesn’tduplicatetheentiretaskofassembler.Inthischapter,weuseportablecodewrittenbyPASCALsubprogramtoexplainhowtoproduceabsolutecodewhichcomesfromtheoffsetofintermediatecode..
79.1.1Structureof“If”sentenceinprogram
Theformof“IF”sentenceinprogramisasfollows:IFi>10THEN…24ELSE BEGIN…25END;
820 LOD 0 321 LIT 0 1022 OPR 0 1223JPC025……2425Thetargetcodeofitis:9meansthatiftheresultofexpressioni>10isfalse,itwillgototheelsepartofinstruction25,ifnotitwillrunthethenpartofinstruction24.
Thealgorithmofgeneratingtargetcodeisshownbelow.10IFSYM=IFSYM/*checkupifthereisword“IF”*/THENBEGIN GETSYM; CONDITION(…) /*Targetcodeofconditionexpression*/
IFSYM=THENSYM /*checkupifthereisword“THEN”*/THENGETSYM ELSEERROR(18);
BEGIN IFSYM=ELSESYM /*checkupifthereisword“ELSE”*/THEN BEGIN
119.1.2Structureof“While”sentenceinprogram
Theformof“while”sentenceinprogramisasfollows.WHILEc>1DOBEGIN….END12Itscorrespondtargetcodeis16 LOD 0 317 LIT 0 118 OPR 0 1219JPC02820….. 27JMP016281316istheinstructionlinenumberofwhile,whenitreachesline27,itwillreturntoline16asaloop..
28meansthatifthevalueoflogicexpressioni>1isfalse,thengotoline28.Weknowwhenprogramisrunningatline19thatwecannotlookaheadtoline28,sowehavetowaituntilreachingline28,thensaveandadd28toit.Thealgorithmofgeneratingtargetcodeisshownbelow..14IFSYM=WHILESYM /*Judgeifitis“while”sentence*/THEN BEGIN L1:=CX;/*L1storethelinenumberof“while”sentenceCX*/GETSYM; CONDITION(…) /*Targetcodeofconditionexpression*/L2:=CX; /*L2recordstheescapelinenumberof“while”,nowwaitsforbeingadded*/GEN(JPC,0,0);/*generatetheloopexitinstructionthatL2isemptynow*/IFSYM=DOSYM /*checkupifthereisword“DO”*/THENGETSYM ELSEERROR(18);GEN(JMP,0,L1);/*generatejumpinstructionwithoutlimitationandreturntolineL1*/CODE[L2].A:=CX/*recordtheloopescapelinenumberandaddittoL2*/END; 159.1.3Structureof“PROCEDURE”sentenceinprogram
Weknowtheformofprocedureinprogramincludestwoparts,thefirstpartistheprocedure,thesecondoneisthecallinginstruction.Theformofprocedureprogramisshownbelow.PROGRAMmain;
PROCEDURE1;
PROCEDURE2;
CALL1CALL2END16TheformoftargetcodeprocedurecanbewrittenasLine7andline9aretheinstructionlineofprocedure1andprocedure2.Line24and25arethecallinginstructions..
7 …… 9 ……
24 CAL07 25CAL0917Thealgorithmofgeneratingthetargetcodeisshownbelow.BEGIN DX:=3; /*therelativeinputaddressofmainprograminstack*/TX:=0; /*theinputaddressofmainprograminsymboltable*/TABLE[0].ADR:=0;/*thefirstinstructionaddressinsymboltableofmain
program*/BLOCK(0,2,…); /*enteringPROCEDURE1*/DX:=3;/*therelativeinputaddressofprocedure1instack*/TX:=2;/*theinputaddressofprocedure1insymboltable*/TABLE[2].ADR:=0;/*thelevelofprocedure1isrecordedinsymboltable,andwillbereplacedbythestartaddressofprocedure1later*/GEN(JMP,0,0);/*generatetheinstructionshifttoprocedure1,theaddresswillbeaddedlater*/CODE[TABLE[2].ADR:=7/*recordthestartaddressofprocedure1,andaddittoGEN(JMP,0,0);*/
18TABLE[2].SIZE:=3/*Thespaceneedbyprocedure1*/GEN(INT,0,3)/*Thestackspaceneedbyprocedure1*/BLOCK(0,4,…); /*enteringPROCEDURE2*/DX:=3; /*therelativeinputaddressofprocedure2instack*/TX:=4; /*theinputaddressofprocedure2insymboltable*/TABLE[4].ADR:=0;/*thelevelofprocedure2isrecordedinsymboltable,andwillbereplacedbythestartaddressofprocedure2later*/GEN(JMP,0,0);/*generatetheinstructionshifttoprocedure2,theaddresswillbeaddedlater*/CODE[TABLE[4].ADR:=9/*recordthestartaddressofprocedure1,andaddittoGEN(JMP,0,0);*/TABLE[4].SIZE:=3/*Thespaceneedbyprocedure1*/GEN(INT,0,3)/*Thestackspaceneedbyprocedure1*/19IFSYM=CALLSYMTHEN/*generatethetargetcodeofprocedure1*/BEGINWITHTABLE[2]DOIFKIND=PROCEDURETHENGEN(CAL,0,7)/*generatethecallcodeforprocedure1*/GEN(OPR,0,0)/*generatethereturncode*/IFSYM=CALLSYMTHEN/*generatethetargetcodeofprocedure1*/BEGINWITHTABLE[4]DOIFKIND=PROCEDURETHENGEN(CAL,0,9)/*generatethecallcodeforprocedure2*/GEN(OPR,0,0)/*generatethereturncode*/209.2Anexample9.2.1Thealgorithmofgeneratingthetargetcode
BEGIN DX:=3; /*therelativeinputaddressofmainprograminstack*/TX:=0; /*theinputaddressofmainprograminsymboltable*/TABLE[0].ADR:=0; /*thefirstinstructionaddressinsymboltableofmainprogram*/GEN(JMP,0,0);/*generatethejumpinstruction*/BLOCK(0,2,…); /*enteringPROCEDURE1*/DX:=3;/*therelativeinputaddressofprocedure1instack*/TX:=2;/*theinputaddressofprocedure1insymboltable*/TABLE[2].ADR:=0;/*thelevelofprocedure1isrecordedinsymboltable,andwillbereplacedbythestartaddressofprocedure1later*/GEN(JMP,0,0);/*generatetheinstructionshifttoprocedure1,theaddressaddedlater*/IFSYM=VARSYMTHEN/*recordidentifyinsymboltable*/…21CODE[TABLE[2].ADR:=7/*recordthestartaddressofprocedure1,andaddittoGEN(JMP,0,0);*/TABLE[2].SIZE:=3/*Thespaceneedbyprocedure1*/GEN(INT,0,3)/*Thestackspaceneedbyprocedure1*/BLOCK(0,4,…); /*enteringPROCEDURE2*/DX:=3; /*therelativeinputaddressofprocedure2instack*/TX:=4; /*theinputaddressofprocedure2insymboltable*/TABLE[4].ADR:=0;/*thelevelofprocedure2isrecordedinsymboltable,andwillbereplacedbythestartaddressofprocedure1later*/
GEN(JMP,0,0);/*generatetheinstructionshifttoprocedure1,theaddressaddedlater*/IFSYM=CONSTSYMTHEN/*recordconstantinsymboltable*/…22CODE[TABLE[4].ADR:=9/*recordthestartaddressofprocedure1,andaddittoGEN(JMP,0,0);*/TABLE[4].SIZE:=3/*Thespaceneedbyprocedure1*/GEN(INT,0,3)/*Thestackspaceneedbyprocedure1*/IFSYM=WHILESYM /*Judgeifitis“while”sentence*/THEN BEGIN L1:=CX; /*L1storethelinenumberof“while”sentenceCX*/ GETSYM;
CONDITION(…); /*Targetcodeofexpression*/
L2:=CX; /*L2recordstheescapelinenumberof“while”,nowwaitsforbeingadded*/GEN(JPC,0,0); /*generatetheloopexitinstructionthatL2isemptynow*/23IFSYM=DOSYM/*checkupifthereisword“DO”*/THENGETSYM ELSEERROR(18);IFSYM=IFSYM/*checkupifthereisword“IF”*/THENBEGIN GETSYM; CONDITION(…)/*Targetcodeofconditionexpression*/IFSYM=THENSYM /*checkupifthereisword“THEN”*/THENGETSYM24 ELSEERROR(18);BEGINIFSYM=CALLSYMTHEN/*generatethetargetcodeofprocedure1*/BEGINWITHTABLE[2]DOIFKIND=PROCEDURETHENGEN(CAL,0,7)/*generatethecallcodeforprocedure1*/GEN(OPR,0,0)/*generatethereturncode*/IFSYM=ELSESYM /*checkupifthereisword“ELSE”*/THEN BEGIN 25GETSYM;
IFSYM=CALLSYMTHEN/*generatethetargetcodeofprocedure1*/BEGINWITHTABLE[4]DOIFKIND=PROCEDURETHENGEN(CAL,0,9)/*generatethecallcodeforprocedu
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 小学主题班会课件:保护环境,从我做起
- 小学主题班会课件:教育的希望永远在儿童的心田
- 养成良好学习习惯提高学习效率小学主题班会课件
- 智能制造企业自动化设备操作指南
- 零售业销售专员店铺管理绩效考评表
- 2026福建龙岩市汉剧传习中心招聘紧缺急需专业技术人员2人的笔试题库(模拟题)附答案详解
- 都兰县卫生健康局面向社会公开招聘三名临聘人员的模拟试卷(基础题)附答案详解
- 提升自我安全意识生命至上小学主题班会课件
- AI与传统茶文化的数字化创新与发展
- 纳米晶磁芯用改性环氧树脂复合涂层的制备及性能研究
- 医院放射科院感知识培训
- 高中语文全册文言文原文及翻译
- 肝衰竭诊治指南(2024年版)解读
- GB/T 32399-2024信息技术云计算参考架构
- 会计师事务所保密制度
- 幼儿园园本课程建设培训
- 《肌电图的临床应用》课件
- 标准预防与额外预防
- 山东省汽车维修工时定额(T-SDAMTIA 0001-2023)
- 2024年上海市黄浦区初三语文一模试卷及答案
- 幼儿生活活动保育(学前教育专业)PPT完整全套教学课件
评论
0/150
提交评论