版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
Chapter8
IntermediateCode
Intermediatecodegenerationisinthemediatepartofcompiler,itisabridgewhichtranslatesourceprogramintointermediaterepresentationandthentranslateintotargetcode.ThepositionofintermediatecodegenerationincompilerisshowninFigure8.1..
23Therearetwoadvantagesofusingintermediatecode,Thefirstoneisthatwecanattachdifferenttargetcodemachinestosamefrontpartafterthepartofintermediatecodegeneration;;Thesecondoneisthatamachine-independentcodeoptimizercanbeappliedtotheintermediatedrepresentation..
4Intermediatecodesaremachineindependentcodes,buttheyareclosetomachineinstructions.Thegivenprograminasourcelanguageisconvertedtoanequivalentprograminanintermediatelanguagebytheintermediatecodegenerator..
5Intermediatelanguagecanbemanydifferentlanguages,andthedesignerofthecompilerdecidesthisintermediatelanguage.Postfixnotation,four-addresscode(Quadraples),three-addresscode,portablecodeandassemblycodecanbeusedasanintermediatelanguage.Inthischapter,wewillintroducethemindetail.68.1PostfixNotation
Ifwecanrepresentthesourceprogrambypostfixnotation,itwillbeeasytobetranslatedintotargetcode,becausethetargetinstructionorderissamewiththeoperatororderinpostfixnotation..
78.1.1Thedefinitionofpostfixnotation
thepostfixnotationfortheexpressiona+b*cisabc*+.theexpressionareasfollows:
1Theorderofoperandsforexpressioninpostfixnotationissamewithitsoriginalorder.2Operatorfollowsitsoperand,andtherearenoparenthesesinpostfixnotation.3Theoperatorappearsintheorderbythecalculationorder.8Forexample,thepostfixnotationforexpressiona*(b+c/d)isabcd/+*,thetranslationprocedureisjustfollowingthestepsabove..firstly,accordingtostep1wegettheorderofoperandsoftheexpression:abcd,secondly,bythestep2,thefirstoperatorinoperatororderis/,becauseitjustfollowsitsoperandscd,inaddition,asthestep3,operator/iscalculatedfirst,sotheoperatorfollowoperandsis/.Thesecondoperatorinoperatororderis+,itduestothatthereisparenthesesintheoriginalexpression,operator+shouldbecalculatedearlierthanoperator*.Thelastoneis*,because*iscalculatedlastly..
9Theotherexample,thepostfixnotationforexpressiona*b+(c-d)/eisab*cd-e/+.Fromexamples,weknowitisabitdifficulttotranslateanexpressionintoitspostfixnotation.SoscientistE.W.DIJKSTRAfromHolandcreatedamethodtosolvetheproblem..
108.1.2E.W.DIJKSTRAMethod
TherearetwostacksinE.W.DIJKSTRAmethod,onestackstoragesoperands,theotheroneisforoperators,theprocedureofitisshownbyFigure8.2,andthestepofE.W.DIJKSTRAmethodisasfollows:.
11
12Actually,scanningtheexpressionisfromlefttoright.Atthebeginningofscanning,wepushidentifier#tothebottomofoperatorstack,similarly,weaddidentifier#totheendofexpressiontolabelthatitisterminalofexpression.Whenthetwoidentifier#meet,itmeanstheendofscanning.Thestepsofscanningare:
1Ifitisoperand,gototheoperandstack
:
132Ifitisoperator,itshouldbecomparedwiththeoperatoronthetopofoperatorstack.Whenthepriorityofoperatoronthetopstackisbiggerthanthescanningoperator,orequaltoit,theoperatoronthetopofoperatorstackwouldbepoppedandgototheleftside.Ontheotherhandwhenthepriorityofoperatoronthetopstackislessthanthescanningoperator,scanningoperatorshouldbepushedintooperatorstack.143Ifitisleftparenthesis,justpushitintooperatorstack,andthencomparetheoperatorswithinparentheses..Ifitisrightparenthesis,popalltheoperatorswithinparentheses,whatismore,parentheseswouldbedisappearedandwouldnotberepresentedaspostfixnotation..4
Returntostep1tilltwoidentifier#meet.
15Example8.1
Thereisanexpressionofa+b*c,itspostfixnotationisabc*+.FromthetranslatingprocedureshownbyFigure8.3,wecanseethatoperatororderis*+,itisalsothepoporderoftheoperatorstackandcalculatingorder.
1617188.1.3Extendedpostfixnotation
IftheexpressionEisintheformofE1:=E2,thenthepostfixnotationforEis:=,whereandarethepostfixnotationforE1andE2,respectively.Example8.2
Thereisanexpressiona:=b,accordingtothedefinitionabove,itspostfixnotationisab:=.19Example8.3Forexpressiona:=5*(b+8),thepostfixnotationofitis:a5b8+*:=IfThereisaprogramwhichformis:
IFuTHENS1
ELSE……BEGINS2……END20uinthisprogramisaconditionwhichhastworesults,oneistrue,theotheroneisfalse,S1,S2aretwopartsofprogram,l1isthestartnumberofS2,l2istheendnumberofS2..Thepostfixnotationoftheprogramisasfollows:BZmeansifthevalueuisnottrue,thenturntol1,BRmeansjustgotol2.Nowwewillgiveanexampletoexplainthetranslation..
21Example8.4
S:=0;IFi<10THENS:=S+i
ELSE BEGINi:=i+1END;
Thepostfixnotationofexample8.4isasfollows,thevalueofinthisprogramis7,thevalueofis9.S0:=i10<7BZSSi+:=9BR(7)ii1+:=(9)228.2Four-AddressCode
Theotherrepresentationofintermediatecodeisfour-addresscode,anditsdefinitionis:op,y,z,xApplyoperatoroptoyandz,andstoretheresultinx.wherex,yandzarenames,constantsorcompiler-generatedtemporaries;opisanyoperator..
23Example8.5
Theexpression:a+(-b*c+d)*emightbetranslatedintothefollowingfour-addresscodesequence
(1)(-,b,,T1)(2)(*,T1,c,T2)(3)(+,T2,d,T3)(4)(*,T3,e,T4)(5)(+,a,T4,T5)Wecandividefour-addresscodeintodifferenttypesaccordingtotheiroperators.
24BinaryOperator:
op,y,z,resultWhereopisabinaryarithmeticorlogicaloperator.Thisbinaryoperatorisappliedtoyandz,andtheresultoftheoperationisstoredinresult.Forexample:a+b*c,itsfouraddresscodeis:
(*,b,c,T1)(+,a,T1,T2)25UnaryOperator:
op,y,,resultWhereopisaunaryarithmeticorlogicaloperator.Thisunaryoperatorisappliedtoy,andtheresultoftheoperationisstoredinresult.Forexample,expressionS:=0,itsfouraddresscodeis::
(:=,0,,S)26UnconditionalJumps:
op,L,Wewilljumptothethree-addresscodewiththelabelL,andtheexecutioncontinuesfromthatstatement.HereopisBR.Forexample::
(BR,9,,)27ConditionalJumps:
op,L,,xHereopisBZ,wewilljumptothethree-addresscodewiththelabelLiftheresultofxisnottrue,andtheexecutioncontinuesfromthatstatement.Iftheresultistrue,theexecutioncontinuesfromthestatementfollowingthisconditionaljumpstatement..
(BZ,7,,T1)28Thefouraddresscodeforexample8.4:(1)(:=,0,,S)(2)(—,i,10,T1)(3)(BZ,7,,T1)(4)(+,S,i,T2)(5)(:=,T2,,S)(6)(BR,9,,)(7)(+,i,l,T3)(8)(:=,T3,,i)Thestorageforfour-addresscodeissimilarwithpostfixnotation,namely,theyarealluseE.W.DIJKSTRAmethodtorealizetheoperation..
298.3Three-AddressCode
Thedifferencebetweenthree-addresscodeandfour-addresscodeisthedifferentmemorytheyoccupy.Whenweproducetargetcode,allthedatawillbeassignedrun-timememory.Thememorylocationwillbeplacedinthesymbol-tableforthedata.Comparedwiththree-addresscode,symboltableforfour-addresscodeinterposeanextrafieldtostoretheresultpartinfour-addresscode.Whenweusethecalculationresult,weshouldonlylookforthefourthpartinfour-addresscode,however,inthree-addresscode,weshoulddefineatemporaryvaluewhichreferencestotheresultpart.Thisproblemmakesthree-addresscodemoredifficulttobedesignedinanoptimizingcompiler..308.5PortableCode
Portablecodeisakindofintermediatecode,itcanbewrittenbymanyprogramlanguages.Thissection,wewillexplainportablecodewrittenbyPASCALsubprogram..
Portablecodeincludestwosections,oneisPROCEDUREBLOCKwhichformsintermediatecode,theotheroneisPROCEDUREGENwhichgeneratesintermediatecode,andthenstoresittoCODEbyPROCEDUREINTERPRET
31WewillintroducethePROCEDUREGENindetail.ThereisaPASCALsourceprogramwhichisshownbelow..
PROGRAMmain;PROCEDURE1;PROCEDURE2;BEGINREAD(i);WHILEi>1DOBEGINIFi>10THENCALL1ELSE BEGINCALL2END;END;END32Theportablecodeofthesourceprogramisasfollows.33Fromaboveportablecode,thestructureofportablecodeincludesthreeparts,thefirstoneisoperand,suchasINT,
STO,OPR,LOD,JPCandCAL,thesecond
partisthelevelvalue,actuallyitis0,thethird
partisvalue,suchasrelativeaddress,thenumberofunits,procedureenteraddress,valueofconstantorsomespecialoperators..34INTmeansdataspaceinstack.Arepresentsunitnumberinstackforprocedures,forexample,5inline11.CALmeansthatitcallsprocedure.Ainitistheaddressofprocedure.LITispushingconstantintothetopofstack.Ainitisthevalueofconstant.LODispushingvariableintothetopofstack.Ainitistherelativeaddressofvariable.35STOmeanstopopthetopofstacktounit.Ainitistherelativeaddressofit.JMPmeanstogotoanaddressdirectly.JPCistomovetheaddresswhilethevalueonthetopofstackisfalse,otherwiseitmovesforward.OPRisoperator.WhenA=2,itrepresentst
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 尊師重道:培養良好品質的小學主題班會課件
- 人教版九年級下冊數學 27.2.1.2 相似三角形的判定(第二課時) 導學案
- 2026年食品營養與檢測(食品安全評估)試題及答案
- 2026-2030中國沙灘鞋行業市場深度調研及發展趨勢與投資價值評估研究報告
- 安全教育:掌握基本應急知識小學主題班會課件
- 供應商產品質量投訴處理回函(5篇)
- 線上醫藥行業班組長培訓協議
- 2020-2021學年上海市金匯高級中學高三上語文第五單元測試
- 信息技術外包2026年網站建設協議
- 高中美術(湘美版)《美術鑒賞》第二單元第2課知識清單:禮制的視覺編碼-從青銅紋樣到墓室壁
- DB34-T 5477-2026 光敏性藥物靜脈輸注避光管理規范
- 成都市大邑中學高一入學語文分班考試真題含答案
- 2026年張家界市永定區住房和城鄉建設局人員招聘筆試參考試題及答案詳解
- 旅游直播培訓課件
- 既有建筑幕墻檢查及安全性鑒定技術標準
- 十歲那年的試題及答案
- 廠區生活垃圾管理制度
- 臨建拆除施工方案新
- 勵耕計劃 申請書
- DB11-T 1771-2020 地源熱泵系統運行技術規范
- 游戲開發外包合同
評論
0/150
提交評論