編譯原理與實踐(中英雙語版)課件 Chapter 8 Intermediate Code_第1頁
編譯原理與實踐(中英雙語版)課件 Chapter 8 Intermediate Code_第2頁
編譯原理與實踐(中英雙語版)課件 Chapter 8 Intermediate Code_第3頁
編譯原理與實踐(中英雙語版)課件 Chapter 8 Intermediate Code_第4頁
編譯原理與實踐(中英雙語版)課件 Chapter 8 Intermediate Code_第5頁
已閱讀5頁,還剩35頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論