FoxARMBackend.Mod 147 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582
  1. MODULE FoxARMBackend; (** AUTHOR ""; PURPOSE "backend for ARM (advanced RISC machines)"; *)
  2. IMPORT
  3. Basic := FoxBasic, SyntaxTree := FoxSyntaxTree, Global := FoxGlobal, Backend := FoxBackend, Sections := FoxSections,
  4. IntermediateCode := FoxIntermediateCode, IntermediateBackend := FoxIntermediateBackend, CodeGenerators := FoxCodeGenerators, BinaryCode := FoxBinaryCode,
  5. SemanticChecker := FoxSemanticChecker, Formats := FoxFormats, Assembler := FoxARMAssembler, InstructionSet := FoxARMInstructionSet,
  6. SYSTEM, Diagnostics, Streams, Options, WMUtilities, Strings, ObjectFile, Scanner := FoxScanner, ObjectFileFormat := FoxGenericObjectFile,
  7. ActiveCells := FoxActiveCells, D := Debugging;
  8. CONST
  9. Trace = FALSE; (* general trace *)
  10. TraceFixups = FALSE;
  11. DefaultRuntimeModuleName = "ARMRuntime";
  12. None = -1;
  13. (* parts of an ARM operand *)
  14. Low = 0; High = 1;
  15. (* mnemonics of the ARM instruction set *)
  16. opADC = InstructionSet.opADC; opADD = InstructionSet.opADD;
  17. opAND = InstructionSet.opAND; opB = InstructionSet.opB;
  18. opBIC = InstructionSet.opBIC; opBKPT = InstructionSet.opBKPT;
  19. opBL = InstructionSet.opBL; opBLX = InstructionSet.opBLX;
  20. opBX = InstructionSet.opBX; opCDP = InstructionSet.opCDP;
  21. opCDP2 = InstructionSet.opCDP2; opCLZ = InstructionSet.opCLZ;
  22. opCMN = InstructionSet.opCMN; opCMP = InstructionSet.opCMP;
  23. opEOR = InstructionSet.opEOR; opFABSD = InstructionSet.opFABSD;
  24. opFABSS = InstructionSet.opFABSS; opFADDD = InstructionSet.opFADDD;
  25. opFADDS = InstructionSet.opFADDS; opFCMPD = InstructionSet.opFCMPD;
  26. opFCMPED = InstructionSet.opFCMPED; opFCMPES = InstructionSet.opFCMPES;
  27. opFCMPEZD = InstructionSet.opFCMPEZD; opFCMPEZS = InstructionSet.opFCMPEZS;
  28. opFCMPS = InstructionSet.opFCMPS; opFCMPZD = InstructionSet.opFCMPZD;
  29. opFCMPZS = InstructionSet.opFCMPZS; opFCPYD = InstructionSet.opFCPYD;
  30. opFCPYS = InstructionSet.opFCPYS; opFCVTDS = InstructionSet.opFCVTDS;
  31. opFCVTSD = InstructionSet.opFCVTSD; opFDIVD = InstructionSet.opFDIVD;
  32. opFDIVS = InstructionSet.opFDIVS; opFLDD = InstructionSet.opFLDD;
  33. opFLDMIAD = InstructionSet.opFLDMIAD; opFLDMIAS = InstructionSet.opFLDMIAS;
  34. opFLDMIAX = InstructionSet.opFLDMIAX; opFLDMDBD = InstructionSet.opFLDMDBD;
  35. opFLDMDBS = InstructionSet.opFLDMDBS; opFLDMDBX = InstructionSet.opFLDMDBX;
  36. opFLDS = InstructionSet.opFLDS; opFMACD = InstructionSet.opFMACD;
  37. opFMACS = InstructionSet.opFMACS; opFMDHR = InstructionSet.opFMDHR;
  38. opFMDLR = InstructionSet.opFMDLR; opFMRDH = InstructionSet.opFMRDH;
  39. opFMRDL = InstructionSet.opFMRDL; opFMRS = InstructionSet.opFMRS;
  40. opFMRX = InstructionSet.opFMRX; opFMSCD = InstructionSet.opFMSCD;
  41. opFMSCS = InstructionSet.opFMSCS; opFMSR = InstructionSet.opFMSR;
  42. opFMSTAT = InstructionSet.opFMSTAT; opFMULD = InstructionSet.opFMULD;
  43. opFMULS = InstructionSet.opFMULS; opFMXR = InstructionSet.opFMXR;
  44. opFNEGD = InstructionSet.opFNEGD; opFNEGS = InstructionSet.opFNEGS;
  45. opFNMACD = InstructionSet.opFNMACD; opFNMACS = InstructionSet.opFNMACS;
  46. opFNMSCD = InstructionSet.opFNMSCD; opFNMSCS = InstructionSet.opFNMSCS;
  47. opFNMULD = InstructionSet.opFNMULD ; opFNMULS = InstructionSet.opFNMULS;
  48. opFSITOD = InstructionSet.opFSITOD; opFSITOS = InstructionSet.opFSITOS;
  49. opFSQRTD = InstructionSet.opFSQRTD; opFSQRTS = InstructionSet.opFSQRTS;
  50. opFSTD = InstructionSet.opFSTD; opFSTMIAD = InstructionSet.opFSTMIAD;
  51. opFSTMIAS = InstructionSet.opFSTMIAS; opFSTMIAX = InstructionSet.opFSTMIAX;
  52. opFSTMDBD = InstructionSet.opFSTMDBD; opFSTMDBS = InstructionSet.opFSTMDBS;
  53. opFSTMDBX = InstructionSet.opFSTMDBX; opFSTS = InstructionSet.opFSTS;
  54. opFSUBD = InstructionSet.opFSUBD; opFSUBS = InstructionSet.opFSUBS;
  55. opFTOSID = InstructionSet.opFTOSID; opFTOSIZD = InstructionSet.opFTOSIZD;
  56. opFTOSIS = InstructionSet.opFTOSIS; opFTOSIZS = InstructionSet.opFTOSIZS;
  57. opFTOUID = InstructionSet.opFTOUID; opFTOUIZD = InstructionSet.opFTOUIZD;
  58. opFTOUIS = InstructionSet.opFTOUIS; opFTOUIZS = InstructionSet.opFTOUIZS;
  59. opFUITOD = InstructionSet.opFUITOD; opFUITOS = InstructionSet.opFUITOS;
  60. opLDC = InstructionSet.opLDC; opLDC2 = InstructionSet.opLDC2;
  61. opLDM = InstructionSet.opLDM; opLDR = InstructionSet.opLDR;
  62. opLDREX = InstructionSet.opLDREX; opSTREX = InstructionSet.opSTREX;
  63. opMCR = InstructionSet.opMCR; opMCR2 = InstructionSet.opMCR2;
  64. opMCRR = InstructionSet.opMCRR; opMLA = InstructionSet.opMLA;
  65. opMOV = InstructionSet.opMOV; opMRC = InstructionSet.opMRC;
  66. opMRC2 = InstructionSet.opMRC2; opMRRC = InstructionSet.opMRRC;
  67. opMRS = InstructionSet.opMRS; opMSR = InstructionSet.opMSR;
  68. opMUL = InstructionSet.opMUL; opMVN = InstructionSet.opMVN;
  69. opORR = InstructionSet.opORR; opPLD = InstructionSet.opPLD;
  70. opQADD = InstructionSet.opQADD; opQDADD = InstructionSet.opQDADD;
  71. opQDSUB = InstructionSet.opQDSUB; opQSUB = InstructionSet.opQSUB;
  72. opRSB = InstructionSet.opRSB; opRSC = InstructionSet.opRSC;
  73. opSBC = InstructionSet.opSBC; opSMLABB = InstructionSet.opSMLABB;
  74. opSMLABT = InstructionSet.opSMLABT; opSMLAL = InstructionSet.opSMLAL;
  75. opSMLATB = InstructionSet.opSMLATB; opSMLATT = InstructionSet.opSMLATT;
  76. opSMLALBB = InstructionSet.opSMLALBB; opSMLALBT = InstructionSet.opSMLALBT;
  77. opSMLALTB = InstructionSet.opSMLALTB; opSMLALTT = InstructionSet.opSMLALTT;
  78. opSMLAWB = InstructionSet.opSMLAWB; opSMLAWT = InstructionSet.opSMLAWT;
  79. opSMULBB = InstructionSet.opSMULBB; opSMULBT = InstructionSet.opSMULBT;
  80. opSMULTB = InstructionSet.opSMULTB; opSMULTT = InstructionSet.opSMULTT;
  81. opSMULWB = InstructionSet.opSMULWB; opSMULWT = InstructionSet.opSMULWT;
  82. opSMULL = InstructionSet.opSMULL; opSTC = InstructionSet.opSTC;
  83. opSTC2 = InstructionSet.opSTC2; opSTM = InstructionSet.opSTM;
  84. opSTR = InstructionSet.opSTR; opSUB = InstructionSet.opSUB;
  85. opSWI = InstructionSet.opSWI; opSWP = InstructionSet.opSWP;
  86. opTEQ = InstructionSet.opTEQ; opTST = InstructionSet.opTST;
  87. opUMLAL = InstructionSet.opUMLAL; opUMULL = InstructionSet.opUMULL;
  88. MaximumFixupDistance = (*4103*) 1024; (* = 2^12-1+8 (maximum distance [in bytes] between a symbol fixup location and an instruction that uses the symbol) *)
  89. (* builtin backend specific system instructions *)
  90. GetSP = 0; SetSP = 1;
  91. GetFP = 2; SetFP = 3;
  92. GetLNK = 4; SetLNK = 5;
  93. GetPC = 6; SetPC = 7;
  94. LDPSR = 8; STPSR = 9;
  95. LDCPR = 10; STCPR = 11;
  96. FLUSH = 12;
  97. NULL = 13; XOR = 14; MULD = 15; ADDC = 16;
  98. PACK = 17; UNPK = 18;
  99. UseFPUFlag = "useFPU";
  100. TYPE
  101. Operand = InstructionSet.Operand;
  102. Ticket = CodeGenerators.Ticket;
  103. (* a citation of a symbol, i.e., an ARM instruction that requires a symbol's address *)
  104. Citation = OBJECT
  105. VAR
  106. pc: LONGINT; (* program counter of the ARM instruction *)
  107. next: Citation;
  108. END Citation;
  109. (* a reference to a symbol and offset in IR units that is used by at least one instruction *)
  110. Reference = OBJECT
  111. VAR
  112. firstCitation, lastCitation: Citation; (* linked list of citations *)
  113. next: Reference;
  114. PROCEDURE & Init;
  115. BEGIN
  116. firstCitation := NIL; lastCitation := NIL; next := NIL;
  117. END Init;
  118. PROCEDURE AddCitation(pc: LONGINT);
  119. VAR
  120. citation: Citation;
  121. BEGIN
  122. NEW(citation); citation.pc := pc; citation.next := NIL;
  123. IF firstCitation = NIL THEN firstCitation := citation ELSE lastCitation.next := citation END;
  124. lastCitation := citation
  125. END AddCitation;
  126. END Reference;
  127. ImmediateReference = OBJECT (Reference)
  128. VAR value: LONGINT;
  129. PROCEDURE & InitImm(v: LONGINT);
  130. BEGIN
  131. Init;
  132. SELF.value := v;
  133. END InitImm;
  134. END ImmediateReference;
  135. (* a reference to a symbol and offset in IR units that is used by at least one instruction *)
  136. SymbolReference = OBJECT (Reference)
  137. VAR
  138. symbol: Sections.SectionName;
  139. fingerprint: LONGINT;
  140. symbolOffset: LONGINT; (* offset to the symbol in IR units *)
  141. PROCEDURE & InitSym(s: Sections.SectionName; fp: LONGINT; offs: LONGINT);
  142. BEGIN
  143. Init;
  144. SELF.symbol := s; SELF.symbolOffset := offs; fingerprint := fp;
  145. END InitSym;
  146. END SymbolReference;
  147. ListOfReferences = OBJECT
  148. VAR
  149. firstReference, lastReference: Reference; (* linked list of all symbol references *)
  150. referenceCount: LONGINT; (* the number of reference = length of the required fixup block *)
  151. pcOfFirstCitation: LONGINT; (* the PC of the first instruction that cites a symbol or immediate *)
  152. PROCEDURE & Init;
  153. BEGIN
  154. firstReference := NIL; lastReference := NIL;
  155. referenceCount := 0;
  156. pcOfFirstCitation := None;
  157. END Init;
  158. PROCEDURE AddSymbol(symbol: Sections.SectionName; fingerprint: LONGINT; symbolOffset: LONGINT; pc: LONGINT);
  159. VAR
  160. reference, foundReference: Reference; symbolReference: SymbolReference;
  161. BEGIN
  162. (* go through the list of symbol/offset-combinations and check if there already is an entry for the symbol and offset in question *)
  163. reference := firstReference;
  164. WHILE reference # NIL DO
  165. IF reference IS SymbolReference THEN
  166. WITH reference: SymbolReference DO
  167. IF (reference.symbol = symbol) & (reference.symbolOffset = symbolOffset) THEN
  168. foundReference := reference (* an entry already exists *)
  169. END;
  170. END;
  171. END;
  172. reference := reference.next
  173. END;
  174. IF foundReference # NIL THEN
  175. reference := foundReference
  176. ELSE
  177. (* no entry was found for the symbol/offset combination: create a new one *)
  178. NEW(symbolReference, symbol, fingerprint, symbolOffset);
  179. reference := symbolReference;
  180. IF firstReference = NIL THEN firstReference := reference ELSE lastReference.next := reference END;
  181. lastReference := reference;
  182. INC(referenceCount)
  183. END;
  184. (* add a citation to the reference *)
  185. reference.AddCitation(pc);
  186. IF pcOfFirstCitation = None THEN pcOfFirstCitation := pc END
  187. END AddSymbol;
  188. PROCEDURE AddImmediate(value: LONGINT; pc: LONGINT);
  189. VAR
  190. reference, foundReference: Reference; immediateReference: ImmediateReference;
  191. BEGIN
  192. (* go through the list of symbol/offset-combinations and check if there already is an entry for the symbol and offset in question *)
  193. reference := firstReference;
  194. WHILE reference # NIL DO
  195. IF reference IS ImmediateReference THEN
  196. WITH reference: ImmediateReference DO
  197. IF (reference.value = value) THEN
  198. foundReference := reference (* an entry already exists *)
  199. END;
  200. END;
  201. END;
  202. reference := reference.next
  203. END;
  204. IF foundReference # NIL THEN
  205. reference := foundReference
  206. ELSE
  207. (* no entry was found for the symbol/offset combination: create a new one *)
  208. NEW(immediateReference, value);
  209. reference := immediateReference;
  210. IF firstReference = NIL THEN firstReference := reference ELSE lastReference.next := reference END;
  211. lastReference := reference;
  212. INC(referenceCount)
  213. END;
  214. (* add a citation to the reference *)
  215. reference.AddCitation(pc);
  216. IF pcOfFirstCitation = None THEN pcOfFirstCitation := pc END
  217. END AddImmediate;
  218. END ListOfReferences;
  219. PhysicalRegisters* = OBJECT(CodeGenerators.PhysicalRegisters)
  220. VAR
  221. toVirtual: ARRAY InstructionSet.NumberRegisters OF Ticket; (* registers real register -> none / reserved / split / blocked / virtual register (>0) *)
  222. reserved: ARRAY InstructionSet.NumberRegisters OF BOOLEAN;
  223. unusable: Ticket;
  224. hint: LONGINT;
  225. useFPU: BOOLEAN;
  226. PROCEDURE & InitPhysicalRegisters(supportFramePointer, useFPU, cooperative: BOOLEAN);
  227. VAR
  228. i: LONGINT;
  229. unusable: Ticket;
  230. BEGIN
  231. SELF.useFPU := useFPU;
  232. FOR i := 0 TO LEN(toVirtual) - 1 DO
  233. toVirtual[i] := NIL;
  234. reserved[i] := FALSE
  235. END;
  236. NEW(unusable);
  237. (* reserve special purpose registers *)
  238. toVirtual[InstructionSet.RES] := unusable; (* low part result register *)
  239. toVirtual[InstructionSet.RESHI] := unusable; (* high part result register *)
  240. toVirtual[InstructionSet.RESFS] := unusable; (* single precision floatin point result register *)
  241. toVirtual[InstructionSet.SP] := unusable; (* stack pointer *)
  242. toVirtual[InstructionSet.FP] := unusable; (* frame pointer *)
  243. toVirtual[InstructionSet.PC] := unusable; (* program counter *)
  244. toVirtual[InstructionSet.LR] := unusable; (* link register *)
  245. toVirtual[InstructionSet.CPSR] := unusable; (* current program state register *)
  246. toVirtual[InstructionSet.SPSR] := unusable; (* saved program state register *)
  247. IF cooperative THEN
  248. toVirtual[InstructionSet.R11] := unusable; (* current activity register *)
  249. END;
  250. (* disable coprocessor registers *)
  251. FOR i := InstructionSet.CR0 TO InstructionSet.CR15 DO toVirtual[i] := unusable END;
  252. IF ~useFPU THEN
  253. (* disable single precision VFP registers *)
  254. FOR i := InstructionSet.SR0 TO InstructionSet.SR15 DO toVirtual[i] := unusable END
  255. END;
  256. (* disable double precision VFP registers *)
  257. FOR i := InstructionSet.DR0 TO InstructionSet.DR15 DO toVirtual[i] := unusable END;
  258. END InitPhysicalRegisters;
  259. (** the number of physical registers **)
  260. PROCEDURE NumberRegisters(): LONGINT;
  261. BEGIN RETURN InstructionSet.NumberRegisters
  262. END NumberRegisters;
  263. (** allocate, i.e., map, a physical register to a ticket **)
  264. PROCEDURE Allocate(physicalRegisterNumber: LONGINT; ticket: Ticket);
  265. BEGIN
  266. ASSERT(~ticket.spilled);
  267. Assert(toVirtual[physicalRegisterNumber] = NIL,"register already allocated");
  268. toVirtual[physicalRegisterNumber] := ticket
  269. END Allocate;
  270. (** set whether a certain physical register is reserved or not **)
  271. PROCEDURE SetReserved(physicalRegisterNumber: LONGINT; isReserved: BOOLEAN);
  272. BEGIN reserved[physicalRegisterNumber] := isReserved
  273. END SetReserved;
  274. (** whether a certain physical register is reserved **)
  275. PROCEDURE Reserved(physicalRegisterNumber: LONGINT): BOOLEAN;
  276. BEGIN RETURN (physicalRegisterNumber > 0) & reserved[physicalRegisterNumber]
  277. END Reserved;
  278. (** free a certain physical register **)
  279. PROCEDURE Free(physicalRegisterNumber: LONGINT);
  280. BEGIN
  281. Assert((toVirtual[physicalRegisterNumber] # NIL), "register not reserved");
  282. toVirtual[physicalRegisterNumber] := NIL
  283. END Free;
  284. (** get the number of the next free physical register for a certain data type
  285. - if a register hint has been set, it is respected if possible
  286. **)
  287. PROCEDURE NextFree(CONST type: IntermediateCode.Type): LONGINT;
  288. VAR
  289. result, i: LONGINT;
  290. BEGIN
  291. result := None;
  292. IF (type.form IN IntermediateCode.Integer) OR ~useFPU THEN
  293. ASSERT(type.sizeInBits <= 32); (* integers of larger size have already been split *)
  294. (* allocate a regular general purpose ARM register *)
  295. FOR i := InstructionSet.R0 TO InstructionSet.R15 DO
  296. IF (toVirtual[i] = NIL) & ((result = None) OR (i = hint)) THEN result := i END
  297. END
  298. ELSIF type.form = IntermediateCode.Float THEN
  299. IF type.sizeInBits = 32 THEN
  300. (* allocate a single precision VFP register *)
  301. FOR i := InstructionSet.SR0 TO InstructionSet.SR31 DO
  302. IF (toVirtual[i] = NIL) & ((result = None) OR (i = hint)) THEN result := i END
  303. END
  304. ELSIF type.sizeInBits = 64 THEN
  305. (* allocate a double precision VFP register *)
  306. HALT(200); (* not supported yet *)
  307. ELSE
  308. HALT(100)
  309. END
  310. ELSE
  311. HALT(100)
  312. END;
  313. IF result # None THEN ASSERT(toVirtual[result] = NIL) END;
  314. RETURN result
  315. END NextFree;
  316. (** give the register allocator a hint on what physical register to use next **)
  317. PROCEDURE AllocationHint(physicalRegisterNumber: LONGINT);
  318. BEGIN hint := physicalRegisterNumber
  319. END AllocationHint;
  320. (** get the ticket that is currently mapped to a certain physical register **)
  321. PROCEDURE Mapped(physicalRegisterNumber: LONGINT): Ticket;
  322. BEGIN RETURN toVirtual[physicalRegisterNumber]
  323. END Mapped;
  324. (** dump the current register mapping to a stream **)
  325. PROCEDURE Dump(w: Streams.Writer);
  326. VAR i: LONGINT; virtual: Ticket;
  327. BEGIN
  328. w.String("---- registers ----"); w.Ln;
  329. FOR i := 0 TO LEN(toVirtual)-1 DO
  330. virtual := toVirtual[i];
  331. IF virtual # unusable THEN
  332. w.String("reg "); w.Int(i,1); w.String(": ");
  333. IF virtual = NIL THEN w.String("free")
  334. ELSE w.String(" r"); w.Int(virtual.register,1);
  335. END;
  336. IF reserved[i] THEN w.String("reserved") END;
  337. w.Ln
  338. END
  339. END
  340. END Dump;
  341. END PhysicalRegisters;
  342. CodeGeneratorARM = OBJECT(CodeGenerators.GeneratorWithTickets)
  343. VAR
  344. runtimeModuleName: SyntaxTree.IdentifierString;
  345. backend: BackendARM;
  346. opSP, opFP, opPC, opLR, opRES, opRESHI, opRESFS: InstructionSet.Operand;
  347. listOfReferences: ListOfReferences;
  348. spillStackStart, pushChainLength: LONGINT;
  349. stackSize: LONGINT; (* the size of the current stack frame *)
  350. stackSizeKnown: BOOLEAN; (* whether the size of the current stack frame is known at compile time *)
  351. inStackAllocation: BOOLEAN;
  352. fixupPattern: ObjectFile.FixupPatterns; (* pattern for an absolute 32-bit fixup *)
  353. PROCEDURE & InitGeneratorARM(CONST runtimeModuleName: SyntaxTree.IdentifierString; diagnostics: Diagnostics.Diagnostics; backend: BackendARM);
  354. VAR
  355. physicalRegisters: PhysicalRegisters;
  356. BEGIN
  357. SELF.runtimeModuleName := runtimeModuleName;
  358. SELF.backend := backend;
  359. IF Trace THEN IF backend.useFPU THEN D.String("use FPU"); D.Ln ELSE D.String("don't use FPU"); D.Ln END END;
  360. NEW(physicalRegisters, TRUE, backend.useFPU, backend.cooperative);
  361. InitTicketGenerator(diagnostics, backend.optimize, 2, physicalRegisters);
  362. error := FALSE;
  363. inStackAllocation := FALSE;
  364. pushChainLength := 0;
  365. opSP := InstructionSet.NewRegister(InstructionSet.SP, None, None, 0);
  366. opFP := InstructionSet.NewRegister(InstructionSet.FP, None, None, 0);
  367. opPC := InstructionSet.NewRegister(InstructionSet.PC, None, None, 0);
  368. opLR := InstructionSet.NewRegister(InstructionSet.LR, None, None, 0);
  369. opRES := InstructionSet.NewRegister(InstructionSet.RES, None, None, 0);
  370. opRESHI := InstructionSet.NewRegister(InstructionSet.RESHI, None, None, 0);
  371. opRESFS := InstructionSet.NewRegister(InstructionSet.RESFS, None, None, 0);
  372. dump := NIL;
  373. NEW(fixupPattern, 1);
  374. fixupPattern[0].offset := 0;
  375. fixupPattern[0].bits := 32;
  376. NEW(listOfReferences);
  377. END InitGeneratorARM;
  378. (*------------------- overwritten methods ----------------------*)
  379. (* TODO: revise this *)
  380. PROCEDURE Section(in: IntermediateCode.Section; out: BinaryCode.Section);
  381. VAR
  382. oldSpillStackSize: LONGINT;
  383. PROCEDURE CheckEmptySpillStack(): BOOLEAN;
  384. BEGIN
  385. IF spillStack.Size() # 0 THEN
  386. Error(inPC,"implementation error, spill stack not cleared");
  387. IF dump # NIL THEN
  388. spillStack.Dump(dump);
  389. tickets.Dump(dump)
  390. END;
  391. RETURN FALSE
  392. ELSE
  393. RETURN TRUE
  394. END
  395. END CheckEmptySpillStack;
  396. BEGIN
  397. stackSizeKnown := TRUE;
  398. stackSize := 0; (* TODO: ok? *)
  399. tickets.Init; spillStack.Init; listOfReferences.Init;
  400. Section^(in, out); (* pass 1 *)
  401. EmitFinalFixupBlock; (* force the emission of fixups for all references *)
  402. IF stackSizeKnown = FALSE THEN
  403. tickets.Init; spillStack.Init; listOfReferences.Init;
  404. out.Reset;
  405. Section^(in, out); (* pass 2 *)
  406. EmitFinalFixupBlock (* force the emission of fixups for all references *)
  407. END;
  408. IF CheckEmptySpillStack() & (spillStack.MaxSize() > 0) THEN
  409. listOfReferences.Init;
  410. oldSpillStackSize := spillStack.MaxSize();
  411. out.Reset;
  412. Section^(in, out); (* pass 3 *)
  413. EmitFinalFixupBlock; (* force the emission of fixups for all references *)
  414. ASSERT(spillStack.MaxSize() = oldSpillStackSize);
  415. END;
  416. IF CheckEmptySpillStack() THEN END
  417. END Section;
  418. (* TODO: complete this *)
  419. (** whether the code generator can generate code for a certain intermediate code intstruction
  420. if not, the location of a runtime is returned **)
  421. PROCEDURE Supported(CONST irInstruction: IntermediateCode.Instruction; VAR moduleName, procedureName: ARRAY OF CHAR): BOOLEAN;
  422. VAR
  423. result: BOOLEAN; value: HUGEINT; exp: LONGINT;
  424. BEGIN
  425. CASE irInstruction.opcode OF
  426. | IntermediateCode.add, IntermediateCode.sub, IntermediateCode.mul, IntermediateCode.abs, IntermediateCode.neg:
  427. IF (irInstruction.opcode = IntermediateCode.mul) & IsInteger(irInstruction.op1) & IsInteger(irInstruction.op2) & (IsComplex(irInstruction.op1) OR IsComplex(irInstruction.op2)) THEN
  428. result := FALSE;
  429. ELSE
  430. result := backend.useFPU & IsSinglePrecisionFloat(irInstruction.op1) OR ~IsFloat(irInstruction.op1)
  431. END;
  432. | IntermediateCode.div:
  433. result := backend.useFPU & IsSinglePrecisionFloat(irInstruction.op1);
  434. (*
  435. result := result OR IntermediateCode.IsConstantInteger(irInstruction.op3,value) & PowerOf2(value,exp)
  436. *)
  437. | IntermediateCode.conv:
  438. result := backend.useFPU & (IsSinglePrecisionFloat(irInstruction.op1) OR IsSinglePrecisionFloat(irInstruction.op2)) OR ~IsFloat(irInstruction.op1) & ~IsFloat(irInstruction.op2) (* if no FPU and either operand is a float *)
  439. | IntermediateCode.mod:
  440. result := FALSE;
  441. (*
  442. result := IntermediateCode.IsConstantInteger(irInstruction.op3,value) & PowerOf2(value,exp)
  443. *)
  444. | IntermediateCode.rol, IntermediateCode.ror:
  445. result := ~IsComplex(irInstruction.op1)
  446. ELSE
  447. result := TRUE
  448. END;
  449. IF ~result THEN
  450. COPY(runtimeModuleName, moduleName);
  451. GetRuntimeProcedureName(irInstruction, procedureName);
  452. END;
  453. RETURN result
  454. END Supported;
  455. (* determines the name of a runtime procedure to handle a certain IR instruction *)
  456. PROCEDURE GetRuntimeProcedureName(CONST irInstruction: IntermediateCode.Instruction; VAR resultingName: ARRAY OF CHAR);
  457. PROCEDURE AppendType(VAR string: ARRAY OF CHAR; type: IntermediateCode.Type);
  458. VAR
  459. sizeString: ARRAY 3 OF CHAR;
  460. BEGIN
  461. CASE type.form OF
  462. | IntermediateCode.SignedInteger: Strings.AppendChar(string, 'S')
  463. | IntermediateCode.UnsignedInteger: Strings.AppendChar(string, 'U')
  464. | IntermediateCode.Float:Strings.AppendChar(string, 'F')
  465. ELSE HALT(200)
  466. END;
  467. Strings.IntToStr(type.sizeInBits, sizeString); Strings.Append(string, sizeString)
  468. END AppendType;
  469. BEGIN
  470. COPY(IntermediateCode.instructionFormat[irInstruction.opcode].name, resultingName);
  471. Strings.UpperCaseChar(resultingName[0]);
  472. AppendType(resultingName, irInstruction.op1.type);
  473. IF irInstruction.op1.mode # IntermediateCode.Undefined THEN
  474. IF (irInstruction.op1.type.form # irInstruction.op2.type.form) OR (irInstruction.op1.type.sizeInBits # irInstruction.op2.type.sizeInBits) THEN
  475. AppendType(resultingName, irInstruction.op2.type);
  476. END
  477. END;
  478. IF Trace THEN D.Ln; D.String(" runtime procedure name: "); D.String(resultingName); D.Ln; D.Update END
  479. END GetRuntimeProcedureName;
  480. (* check whether the instruction modifies the stack pointer (outside of a stack allocation )*)
  481. PROCEDURE CheckStackPointer(CONST destination: Operand);
  482. BEGIN
  483. IF stackSizeKnown & ~inStackAllocation THEN
  484. IF (destination.mode = InstructionSet.modeRegister) & (destination.register = InstructionSet.SP) THEN
  485. IF dump # NIL THEN dump.String("stackSize unkown"); dump.Ln END;
  486. stackSizeKnown := FALSE
  487. END
  488. END
  489. END CheckStackPointer;
  490. (** emit an ARM instruction with an arbitrary amount of operands **)
  491. PROCEDURE Emit(opCode, condition: LONGINT; flags: SET; CONST operands: ARRAY InstructionSet.MaxOperands OF Operand);
  492. VAR
  493. i: LONGINT;
  494. BEGIN
  495. (* check whether the instruction modifies the stack pointer *)
  496. CheckStackPointer(operands[0]);
  497. (*
  498. (* dump the instruction *)
  499. IF Trace THEN
  500. D.String("opCode="); D.Int(opCode, 0); D.Ln;
  501. D.String("condition="); D.Int(condition, 0); D.Ln;
  502. D.String("flags="); D.Set(flags); D.Ln;
  503. FOR i := 0 TO InstructionSet.MaxOperands - 1 DO
  504. D.String("operand #"); D.Int(i, 0); D.String(": ");
  505. InstructionSet.DumpOperand(D.Log, operands[i]);
  506. D.Ln
  507. END;
  508. D.Ln;
  509. D.Ln
  510. END;
  511. *)
  512. (* emit the instruction *)
  513. InstructionSet.Emit(opCode, condition, flags, operands, out)
  514. END Emit;
  515. (** emit an ARM instruction with no operand **)
  516. PROCEDURE Emit0(opCode: LONGINT);
  517. VAR
  518. operands: ARRAY InstructionSet.MaxOperands OF Operand;
  519. BEGIN
  520. ASSERT(InstructionSet.MaxOperands = 6);
  521. operands[0] := emptyOperand;
  522. operands[1] := emptyOperand;
  523. operands[2] := emptyOperand;
  524. operands[3] := emptyOperand;
  525. operands[4] := emptyOperand;
  526. operands[5] := emptyOperand;
  527. Emit(opCode, InstructionSet.unconditional, {}, operands)
  528. END Emit0;
  529. (** emit an ARM instruction with 1 operand **)
  530. PROCEDURE Emit1(opCode: LONGINT; op: Operand);
  531. VAR
  532. operands: ARRAY InstructionSet.MaxOperands OF Operand;
  533. BEGIN
  534. ASSERT(InstructionSet.MaxOperands = 6);
  535. operands[0] := op;
  536. operands[1] := emptyOperand;
  537. operands[2] := emptyOperand;
  538. operands[3] := emptyOperand;
  539. operands[4] := emptyOperand;
  540. operands[5] := emptyOperand;
  541. Emit(opCode, InstructionSet.unconditional, {}, operands)
  542. END Emit1;
  543. (** emit an ARM instruction with 2 operands **)
  544. PROCEDURE Emit2(opCode: LONGINT; op1, op2: Operand);
  545. VAR
  546. operands: ARRAY InstructionSet.MaxOperands OF Operand;
  547. BEGIN
  548. ASSERT(InstructionSet.MaxOperands = 6);
  549. operands[0] := op1;
  550. operands[1] := op2;
  551. operands[2] := emptyOperand;
  552. operands[3] := emptyOperand;
  553. operands[4] := emptyOperand;
  554. operands[5] := emptyOperand;
  555. Emit(opCode, InstructionSet.unconditional, {}, operands)
  556. END Emit2;
  557. (** emit an ARM instruction with 3 operands **)
  558. PROCEDURE Emit3(opCode: LONGINT; op1, op2, op3: Operand);
  559. VAR
  560. operands: ARRAY InstructionSet.MaxOperands OF Operand;
  561. BEGIN
  562. ASSERT(InstructionSet.MaxOperands = 6);
  563. operands[0] := op1;
  564. operands[1] := op2;
  565. operands[2] := op3;
  566. operands[3] := emptyOperand;
  567. operands[4] := emptyOperand;
  568. operands[5] := emptyOperand;
  569. Emit(opCode, InstructionSet.unconditional, {}, operands)
  570. END Emit3;
  571. (** emit an ARM instruction with 4 operands **)
  572. PROCEDURE Emit4(opCode: LONGINT; op1, op2, op3, op4: Operand);
  573. VAR
  574. operands: ARRAY InstructionSet.MaxOperands OF Operand;
  575. BEGIN
  576. ASSERT(InstructionSet.MaxOperands = 6);
  577. operands[0] := op1;
  578. operands[1] := op2;
  579. operands[2] := op3;
  580. operands[3] := op4;
  581. operands[4] := emptyOperand;
  582. operands[5] := emptyOperand;
  583. Emit(opCode, InstructionSet.unconditional, {}, operands)
  584. END Emit4;
  585. (** emit an ARM instruction with 6 operands **)
  586. PROCEDURE Emit6(opCode: LONGINT; op1, op2, op3, op4, op5, op6: Operand);
  587. VAR
  588. operands: ARRAY InstructionSet.MaxOperands OF Operand;
  589. BEGIN
  590. ASSERT(InstructionSet.MaxOperands = 6);
  591. operands[0] := op1;
  592. operands[1] := op2;
  593. operands[2] := op3;
  594. operands[3] := op4;
  595. operands[4] := op5;
  596. operands[5] := op6;
  597. Emit(opCode, InstructionSet.unconditional, {}, operands)
  598. END Emit6;
  599. (** emit an ARM instruction with 2 operands and certain flags **)
  600. PROCEDURE Emit2WithFlags(opCode: LONGINT; op1, op2: Operand; flags: SET);
  601. VAR
  602. operands: ARRAY InstructionSet.MaxOperands OF Operand;
  603. BEGIN
  604. ASSERT(InstructionSet.MaxOperands = 6);
  605. operands[0] := op1;
  606. operands[1] := op2;
  607. operands[2] := emptyOperand;
  608. operands[3] := emptyOperand;
  609. operands[4] := emptyOperand;
  610. operands[5] := emptyOperand;
  611. Emit(opCode, InstructionSet.unconditional, flags, operands)
  612. END Emit2WithFlags;
  613. (** emit an ARM instruction with 3 operands and certain flags **)
  614. PROCEDURE Emit3WithFlags(opCode: LONGINT; op1, op2, op3: Operand; flags: SET);
  615. VAR
  616. operands: ARRAY InstructionSet.MaxOperands OF Operand;
  617. BEGIN
  618. ASSERT(InstructionSet.MaxOperands = 6);
  619. operands[0] := op1;
  620. operands[1] := op2;
  621. operands[2] := op3;
  622. operands[3] := emptyOperand;
  623. operands[4] := emptyOperand;
  624. operands[5] := emptyOperand;
  625. Emit(opCode, InstructionSet.unconditional, flags, operands)
  626. END Emit3WithFlags;
  627. (** emit an ARM instruction with 1 operand and a condition **)
  628. PROCEDURE Emit1WithCondition(opCode: LONGINT; op1: Operand; condition: LONGINT);
  629. VAR
  630. operands: ARRAY InstructionSet.MaxOperands OF Operand;
  631. BEGIN
  632. ASSERT(InstructionSet.MaxOperands = 6);
  633. operands[0] := op1;
  634. operands[1] := emptyOperand;
  635. operands[2] := emptyOperand;
  636. operands[3] := emptyOperand;
  637. operands[4] := emptyOperand;
  638. operands[5] := emptyOperand;
  639. Emit(opCode, condition, {}, operands)
  640. END Emit1WithCondition;
  641. (** emit an ARM instruction with 2 operands and a condition **)
  642. PROCEDURE Emit2WithCondition(opCode: LONGINT; op1, op2: Operand; condition: LONGINT);
  643. VAR
  644. operands: ARRAY InstructionSet.MaxOperands OF Operand;
  645. BEGIN
  646. ASSERT(InstructionSet.MaxOperands = 6);
  647. operands[0] := op1;
  648. operands[1] := op2;
  649. operands[2] := emptyOperand;
  650. operands[3] := emptyOperand;
  651. operands[4] := emptyOperand;
  652. operands[5] := emptyOperand;
  653. Emit(opCode, condition, {}, operands)
  654. END Emit2WithCondition;
  655. (** emit an ARM instruction with 3 operands and a condition **)
  656. PROCEDURE Emit3WithCondition(opCode: LONGINT; op1, op2, op3: Operand; condition: LONGINT);
  657. VAR
  658. operands: ARRAY InstructionSet.MaxOperands OF Operand;
  659. BEGIN
  660. ASSERT(InstructionSet.MaxOperands = 6);
  661. operands[0] := op1;
  662. operands[1] := op2;
  663. operands[2] := op3;
  664. operands[3] := emptyOperand;
  665. operands[4] := emptyOperand;
  666. operands[5] := emptyOperand;
  667. Emit(opCode, condition, {}, operands)
  668. END Emit3WithCondition;
  669. (**
  670. - generate an arbitrary 32 bit value with as few as possible instructions and move the result into a specified target register
  671. - return the number of instructions required
  672. - if 'doEmit' is TRUE, emit the instructions
  673. **)
  674. PROCEDURE ValueComposition(value: LONGINT; doEmit: BOOLEAN; CONST targetRegister: Operand): LONGINT;
  675. VAR
  676. result: LONGINT;
  677. BEGIN
  678. IF doEmit THEN ASSERT(targetRegister.mode = InstructionSet.modeRegister) END;
  679. IF Trace & doEmit THEN D.Ln; D.String("original value: "); DBin(value, -32); D.String(" ("); D.Int(value, 0); D.String(") "); D.Ln; END;
  680. IF ValueComposition2(value, FALSE, emptyOperand) <= ValueComposition2(-value, FALSE, emptyOperand) + 1 THEN
  681. (* more efficient to calculate the value directly *)
  682. result := ValueComposition2(value, doEmit, targetRegister)
  683. ELSE
  684. (* more efficient to calculate the negation of the value and then negate it *)
  685. result := ValueComposition2(-value, doEmit, targetRegister) + 1;
  686. IF doEmit THEN
  687. Emit3(opRSB, targetRegister, targetRegister, InstructionSet.NewImmediate(0))
  688. END
  689. END;
  690. ASSERT((result >= 1) & (result <= 4));
  691. RETURN result
  692. END ValueComposition;
  693. (* note: used by 'ValueComposition'. do not call directly *)
  694. PROCEDURE ValueComposition2(value: LONGINT; doEmit: BOOLEAN; CONST targetRegister: Operand): LONGINT;
  695. VAR
  696. immediateOperand: Operand;
  697. result, position, partialValue, i: LONGINT;
  698. valueAsSet: SET;
  699. isFirst: BOOLEAN;
  700. BEGIN
  701. IF doEmit THEN ASSERT(targetRegister.mode = InstructionSet.modeRegister) END;
  702. IF Trace & doEmit THEN D.String("value to use: "); DBin(value, -32); D.String(" ("); D.Int(value, 0); D.String(") "); D.Ln; END;
  703. IF (value >= 0) & (value <= 255) THEN
  704. (* directly encodable as ARM immediate *)
  705. result := 1;
  706. IF doEmit THEN
  707. Emit2(opMOV, targetRegister, InstructionSet.NewImmediate(value))
  708. END
  709. ELSE
  710. valueAsSet := SYSTEM.VAL(SET, value);
  711. result := 0;
  712. position := 0;
  713. isFirst := TRUE;
  714. WHILE position < 32 DO
  715. IF (position IN valueAsSet) OR (position + 1 IN valueAsSet) THEN
  716. (* determine partial value for the 8 bit block *)
  717. partialValue := 0;
  718. FOR i := 7 TO 0 BY -1 DO
  719. partialValue := partialValue * 2;
  720. IF ((position + i) < 32) & ((position + i) IN valueAsSet) THEN INC(partialValue) END
  721. END;
  722. IF Trace & doEmit THEN
  723. D.String(" block found @ "); D.Int(position, 0); D.Ln;
  724. D.String(" unshifted partialValue: "); DBin(partialValue, -32); D.String(" ("); D.Int(partialValue, 0); D.String(") "); D.Ln;
  725. D.String(" shifted partialValue: "); DBin(ASH(partialValue, position), -32); D.String(" ("); D.Int(ASH(partialValue, position), 0); D.String(") "); D.Ln;
  726. END;
  727. ASSERT(~ODD(position));
  728. INC(result);
  729. IF doEmit THEN
  730. immediateOperand := InstructionSet.NewImmediate(ASH(partialValue, position)); (* TODO: check shift direction *)
  731. IF isFirst THEN
  732. Emit2(opMOV, targetRegister, immediateOperand);
  733. isFirst := FALSE
  734. ELSE
  735. Emit3(opADD, targetRegister, targetRegister, immediateOperand)
  736. END
  737. END;
  738. INC(position, 8)
  739. ELSE
  740. INC(position, 2)
  741. END
  742. END
  743. END;
  744. ASSERT((result >= 1) & (result <= 4));
  745. RETURN result
  746. END ValueComposition2;
  747. (** get the physical register number that corresponds to a virtual register number and part **)
  748. PROCEDURE PhysicalRegisterNumber(virtualRegisterNumber: LONGINT; part: LONGINT): LONGINT;
  749. VAR
  750. ticket: Ticket;
  751. result: LONGINT;
  752. BEGIN
  753. IF virtualRegisterNumber = IntermediateCode.FP THEN
  754. result := InstructionSet.FP
  755. ELSIF virtualRegisterNumber = IntermediateCode.SP THEN
  756. result := InstructionSet.SP
  757. ELSIF virtualRegisterNumber = IntermediateCode.LR THEN
  758. result := InstructionSet.LR
  759. ELSIF virtualRegisterNumber = IntermediateCode.AP THEN
  760. result := InstructionSet.R11
  761. ELSE
  762. ticket := virtualRegisters.Mapped(virtualRegisterNumber, part);
  763. IF ticket = NIL THEN
  764. result := None
  765. ELSE
  766. result := ticket.register
  767. END
  768. END;
  769. RETURN result
  770. END PhysicalRegisterNumber;
  771. (** get an ARM memory operand that represents a spill location (from a ticket) **)
  772. PROCEDURE GetSpillOperand(ticket: Ticket): Operand;
  773. VAR
  774. offset: LONGINT;
  775. result: Operand;
  776. BEGIN
  777. ASSERT(ticket.spilled);
  778. offset := spillStackStart + ticket.offset + 1; (* TODO: check this *)
  779. ASSERT((0 <= offset) & (offset < InstructionSet.Bits12));
  780. result := InstructionSet.NewImmediateOffsetMemory(PhysicalRegisterNumber(IntermediateCode.FP, Low), offset, {InstructionSet.Decrement});
  781. ASSERT(result.mode = InstructionSet.modeMemory);
  782. RETURN result
  783. END GetSpillOperand;
  784. (** get an ARM operand that represents a certain ticket (might be spilled or not) **)
  785. PROCEDURE OperandFromTicket(ticket: Ticket): Operand;
  786. VAR
  787. result: Operand;
  788. BEGIN
  789. ASSERT(ticket # NIL);
  790. IF ticket.spilled THEN
  791. (* the ticket is spilled *)
  792. result := GetSpillOperand(ticket)
  793. ELSE
  794. result := InstructionSet.NewRegister(ticket.register, None, None, 0)
  795. END;
  796. RETURN result
  797. END OperandFromTicket;
  798. (** get a free temporary register that holds data of a certain type **)
  799. PROCEDURE GetFreeRegister(CONST type: IntermediateCode.Type): Operand;
  800. VAR
  801. result: Operand;
  802. BEGIN
  803. result := OperandFromTicket(TemporaryTicket(IntermediateCode.GeneralPurposeRegister, type));
  804. ASSERT(result.mode = InstructionSet.modeRegister);
  805. RETURN result
  806. END GetFreeRegister;
  807. (** get a new free ARM register
  808. - if a register hint is provided that can hold data of the required type, it is returned instead
  809. **)
  810. PROCEDURE GetFreeRegisterOrHint(CONST type: IntermediateCode.Type; CONST registerHint: Operand): Operand;
  811. VAR
  812. result: Operand;
  813. BEGIN
  814. IF (registerHint.mode = InstructionSet.modeRegister) & IsRegisterForType(registerHint.register, type) THEN
  815. result := registerHint
  816. ELSE
  817. result := GetFreeRegister(type)
  818. END;
  819. ASSERT(result.mode = InstructionSet.modeRegister);
  820. RETURN result
  821. END GetFreeRegisterOrHint;
  822. (** whether a register can hold data of a certain IR type **)
  823. PROCEDURE IsRegisterForType(registerNumber: LONGINT; CONST type: IntermediateCode.Type): BOOLEAN;
  824. VAR
  825. result: BOOLEAN;
  826. BEGIN
  827. result := FALSE;
  828. IF type.form IN IntermediateCode.Integer THEN
  829. IF type.sizeInBits <= 32 THEN
  830. result := (registerNumber >= InstructionSet.R0) & (registerNumber <= InstructionSet.R15)
  831. END
  832. ELSIF type.form = IntermediateCode.Float THEN
  833. IF type.sizeInBits = 32 THEN
  834. result := (registerNumber >= InstructionSet.SR0) & (registerNumber <= InstructionSet.SR31)
  835. ELSE
  836. HALT(200)
  837. END
  838. ELSE
  839. HALT(100)
  840. END;
  841. RETURN result
  842. END IsRegisterForType;
  843. (** get an ARM register that that is set off by a certain amount **)
  844. PROCEDURE RegisterAfterAppliedOffset(register: Operand; offset: LONGINT; registerHint: Operand): Operand;
  845. VAR
  846. result, offsetOperand: Operand;
  847. BEGIN
  848. IF offset = 0 THEN
  849. result := register
  850. ELSE
  851. result := GetFreeRegisterOrHint(IntermediateCode.UnsignedIntegerType(32), registerHint);
  852. offsetOperand := OperandFromValue(ABS(offset), result); (* might be immediate operand or register (tempRegister is given as a register hint) *)
  853. IF offset > 0 THEN
  854. Emit3(opADD, result, register, offsetOperand)
  855. ELSE
  856. Emit3(opSUB, result, register, offsetOperand)
  857. END
  858. END;
  859. RETURN result
  860. END RegisterAfterAppliedOffset;
  861. (** get an ARM register from an IR register
  862. - use register hint if provided
  863. **)
  864. PROCEDURE RegisterFromIrRegister(CONST irRegisterOperand: IntermediateCode.Operand; part: LONGINT; registerHint: Operand): Operand;
  865. VAR
  866. result, offsetOperand, tempReg: Operand;
  867. BEGIN
  868. ASSERT(irRegisterOperand.mode = IntermediateCode.ModeRegister);
  869. result := InstructionSet.NewRegister(PhysicalRegisterNumber(irRegisterOperand.register, part), None, None, 0);
  870. result := RegisterAfterAppliedOffset(result, irRegisterOperand.offset, registerHint);
  871. ASSERT(result.mode = InstructionSet.modeRegister);
  872. RETURN result
  873. END RegisterFromIrRegister;
  874. PROCEDURE Load(targetRegister, memoryOperand: Operand; irType: IntermediateCode.Type);
  875. BEGIN
  876. IF (irType.form IN IntermediateCode.Integer) OR ~(backend.useFPU) THEN
  877. CASE irType.sizeInBits OF
  878. | 8: Emit2WithFlags(opLDR, targetRegister, memoryOperand, {InstructionSet.flagB}) (* LDRB *)
  879. | 16: Emit2WithFlags(opLDR, targetRegister, memoryOperand, {InstructionSet.flagH}) (* LDRH *)
  880. | 32: (* TM*)
  881. Emit2(opLDR, targetRegister, memoryOperand)
  882. ELSE HALT(100)
  883. END
  884. ELSIF irType.form = IntermediateCode.Float THEN
  885. ASSERT(irType.sizeInBits = 32, 200);
  886. Emit2(opFLDS, targetRegister, memoryOperand)
  887. ELSE
  888. HALT(100)
  889. END
  890. END Load;
  891. PROCEDURE Store(sourceRegister, memoryOperand: Operand; type: IntermediateCode.Type);
  892. BEGIN
  893. IF (type.form IN IntermediateCode.Integer) OR ~backend.useFPU THEN
  894. CASE type.sizeInBits OF
  895. | 8: Emit2WithFlags(opSTR, sourceRegister, memoryOperand, {InstructionSet.flagB}) (* STRB *)
  896. | 16: Emit2WithFlags(opSTR, sourceRegister, memoryOperand, {InstructionSet.flagH}) (* STRH *)
  897. | 32: Emit2(opSTR, sourceRegister, memoryOperand)
  898. ELSE HALT(100)
  899. END
  900. ELSIF type.form = IntermediateCode.Float THEN
  901. ASSERT(type.sizeInBits = 32, 200);
  902. Emit2(opFSTS, sourceRegister, memoryOperand)
  903. ELSE
  904. HALT(100)
  905. END
  906. END Store;
  907. (** get an ARM register that contains the address of a symbol/section
  908. - use register hint if provided **)
  909. PROCEDURE RegisterFromSymbol(symbol: Sections.SectionName; fingerprint: LONGINT; resolved: Sections.Section; symbolOffset: LONGINT; CONST registerHint: Operand): Operand;
  910. VAR
  911. address: LONGINT;
  912. result: Operand;
  913. irSection: IntermediateCode.Section;
  914. BEGIN
  915. IF resolved # NIL THEN
  916. irSection := resolved(IntermediateCode.Section);
  917. END;
  918. IF (irSection # NIL) & (irSection.resolved # NIL) & (irSection.resolved.os.fixed) THEN
  919. (* optimization: if the IR section is already resolved and positioned at a fixed location, no fixup is required *)
  920. address := irSection.resolved.os.alignment + irSection.instructions[symbolOffset].pc;
  921. result := RegisterFromValue(address, registerHint)
  922. ELSE
  923. result := GetFreeRegisterOrHint(IntermediateCode.UnsignedIntegerType(32), registerHint);
  924. listOfReferences.AddSymbol(symbol, fingerprint, symbolOffset, out.pc);
  925. Emit2(opLDR, result, InstructionSet.NewImmediateOffsetMemory(opPC.register, 0, {InstructionSet.Increment})); (* LDR ..., [PC, #+???] *)
  926. END;
  927. ASSERT(result.mode = InstructionSet.modeRegister);
  928. RETURN result
  929. END RegisterFromSymbol;
  930. (** get an ARM memory operand from an IR memory operand
  931. - note that the constraints on memory operands depend on the type of data (e.g., the allowed offset range is more restricted for memory operands on floating point values)
  932. **)
  933. PROCEDURE MemoryOperandFromIrMemoryOperand(VAR irMemoryOperand: IntermediateCode.Operand; part: LONGINT; CONST registerHint: Operand): Operand;
  934. VAR
  935. baseAddressRegisterNumber, offset: LONGINT;
  936. indexingMode: SET;
  937. result, baseAddressRegister, offsetRegister, tempRegister: Operand;
  938. BEGIN
  939. ASSERT(irMemoryOperand.mode = IntermediateCode.ModeMemory);
  940. (* determine base address register *)
  941. IF irMemoryOperand.register # IntermediateCode.None THEN
  942. (* case 1: [r1] or [r1 + 7] *)
  943. ASSERT(irMemoryOperand.symbol.name = "");
  944. baseAddressRegisterNumber := PhysicalRegisterNumber(irMemoryOperand.register, Low); (* addresses always are in the lower part *)
  945. ELSIF irMemoryOperand.symbol.name # "" THEN
  946. (* case 2: [symbol], [symbol:3], [symbol + 7] or [symbol:3 + 7] *)
  947. Resolve(irMemoryOperand);
  948. baseAddressRegister := RegisterFromSymbol(irMemoryOperand.symbol.name, irMemoryOperand.symbol.fingerprint, irMemoryOperand.resolved, irMemoryOperand.symbolOffset, registerHint);
  949. baseAddressRegisterNumber := baseAddressRegister.register
  950. ELSE
  951. (* case 3: [123456] *)
  952. ASSERT(irMemoryOperand.offset = 0);
  953. baseAddressRegister := RegisterFromValue(LONGINT(irMemoryOperand.intValue), registerHint);
  954. baseAddressRegisterNumber := baseAddressRegister.register
  955. END;
  956. ASSERT(baseAddressRegisterNumber # None);
  957. (* get offset of part in question *)
  958. offset := irMemoryOperand.offset + part * 4;
  959. (* determine indexing mode *)
  960. IF offset >= 0 THEN indexingMode := {InstructionSet.Increment} ELSE indexingMode := {InstructionSet.Decrement} END;
  961. IF irMemoryOperand.type.form IN IntermediateCode.Integer THEN
  962. (* regular ARM memory operand *)
  963. (*! LDRH supports only 8 bits immediates, while LDR and LDRB support 12 bits immediates *)
  964. IF ((irMemoryOperand.type.sizeInBits = 16) & (ABS(offset) < 256)) OR ((irMemoryOperand.type.sizeInBits # 16) & (ABS(offset) < InstructionSet.Bits12)) THEN
  965. (* offset can be encoded directly *)
  966. result := InstructionSet.NewImmediateOffsetMemory(baseAddressRegisterNumber, ABS(offset), indexingMode)
  967. ELSE
  968. (* offset has to be provided in a register *)
  969. offsetRegister := RegisterFromValue(ABS(offset), emptyOperand);
  970. result := InstructionSet.NewRegisterOffsetMemory(baseAddressRegisterNumber, offsetRegister.register, None, 0, indexingMode)
  971. END
  972. ELSIF irMemoryOperand.type.form = IntermediateCode.Float THEN
  973. (* VFP memory operand *)
  974. ASSERT((ABS(offset) MOD 4) = 0);
  975. IF ABS(offset) >= 1024 THEN
  976. (* offset cannot be encoded directly _> it has to be provided by means of an adapted base register *)
  977. tempRegister := RegisterFromValue(ABS(offset), emptyOperand);
  978. IF offset < 0 THEN
  979. Emit3(opSUB, tempRegister, tempRegister, baseAddressRegister)
  980. ELSE
  981. Emit3(opADD, tempRegister, tempRegister, baseAddressRegister)
  982. END;
  983. ReleaseHint(baseAddressRegister.register);
  984. baseAddressRegister := tempRegister;
  985. baseAddressRegisterNumber := baseAddressRegister.register;
  986. offset := 0;
  987. END;
  988. result := InstructionSet.NewImmediateOffsetMemory(baseAddressRegisterNumber, ABS(offset), indexingMode)
  989. ELSE
  990. HALT(100)
  991. END;
  992. ASSERT(result.mode = InstructionSet.modeMemory);
  993. RETURN result
  994. END MemoryOperandFromIrMemoryOperand;
  995. (** get an ARM immediate operand or register from any IR operand
  996. - if possible, the an immediate is returned
  997. - if needed, use register hint if provided
  998. **)
  999. PROCEDURE RegisterOrImmediateFromIrOperand(VAR irOperand: IntermediateCode.Operand; part: LONGINT; registerHint: Operand): Operand;
  1000. VAR
  1001. result: Operand;
  1002. BEGIN
  1003. IF IrOperandIsDirectlyEncodable(irOperand, part) THEN
  1004. result := InstructionSet.NewImmediate(ValueOfPart(irOperand.intValue, part))
  1005. ELSE
  1006. result := RegisterFromIrOperand(irOperand, part, registerHint)
  1007. END;
  1008. RETURN result
  1009. END RegisterOrImmediateFromIrOperand;
  1010. (** get an ARM register operand from any IR operand
  1011. - use register hint if provided
  1012. **)
  1013. PROCEDURE RegisterFromIrOperand(VAR irOperand: IntermediateCode.Operand; part: LONGINT; registerHint: Operand): Operand;
  1014. VAR
  1015. value: LONGINT;
  1016. result: Operand;
  1017. BEGIN
  1018. CASE irOperand.mode OF
  1019. | IntermediateCode.ModeRegister:
  1020. ASSERT((irOperand.intValue = 0) & (irOperand.symbol.name = ""));
  1021. result := RegisterFromIrRegister(irOperand, part, registerHint)
  1022. | IntermediateCode.ModeMemory:
  1023. result := GetFreeRegisterOrHint(PartType(irOperand.type, part), registerHint);
  1024. Load(result, MemoryOperandFromIrMemoryOperand(irOperand, part, result), PartType(irOperand.type, part))
  1025. | IntermediateCode.ModeImmediate:
  1026. ASSERT(irOperand.register = IntermediateCode.None);
  1027. IF irOperand.symbol.name # "" THEN
  1028. Resolve(irOperand);
  1029. result := RegisterFromSymbol(irOperand.symbol.name, irOperand.symbol.fingerprint, irOperand.resolved, irOperand.symbolOffset, emptyOperand);
  1030. result := RegisterAfterAppliedOffset(result, irOperand.offset, registerHint);
  1031. ELSE
  1032. ASSERT(irOperand.offset = 0);
  1033. IF IsInteger(irOperand) THEN result := RegisterFromValue(ValueOfPart(irOperand.intValue, part), registerHint)
  1034. ELSIF ~backend.useFPU THEN
  1035. IF IsSinglePrecisionFloat(irOperand) THEN
  1036. result := RegisterFromValue(BinaryCode.ConvertReal(SHORT(irOperand.floatValue)), registerHint)
  1037. ELSE
  1038. result := RegisterFromValue(ValueOfPart(BinaryCode.ConvertLongreal(irOperand.floatValue),part), registerHint);
  1039. END;
  1040. ELSIF IsSinglePrecisionFloat(irOperand) THEN result := SinglePrecisionFloatRegisterFromValue(REAL(irOperand.floatValue), registerHint)
  1041. ELSE HALT(200)
  1042. END
  1043. END
  1044. ELSE
  1045. HALT(100)
  1046. END;
  1047. ASSERT(result.mode = InstructionSet.modeRegister);
  1048. RETURN result
  1049. END RegisterFromIrOperand;
  1050. (** whether an IR operand is complex, i.e., requires more than one ARM operands to be represented **)
  1051. PROCEDURE IsComplex(CONST irOperand: IntermediateCode.Operand): BOOLEAN;
  1052. VAR
  1053. result: BOOLEAN;
  1054. BEGIN
  1055. IF (irOperand.type.form IN IntermediateCode.Integer) OR ~backend.useFPU THEN
  1056. result := irOperand.type.sizeInBits > 32 (* integers above 32 bits have to be represented in multiple registers *)
  1057. ELSIF irOperand.type.form = IntermediateCode.Float THEN
  1058. result := FALSE (* for all types of floating point numbers there are dedicated VFP registers *)
  1059. ELSE
  1060. HALT(100)
  1061. END;
  1062. RETURN result
  1063. END IsComplex;
  1064. (** whether an IR operand hold a single precision floating point value **)
  1065. PROCEDURE IsSinglePrecisionFloat(CONST irOperand: IntermediateCode.Operand): BOOLEAN;
  1066. BEGIN RETURN (irOperand.type.sizeInBits = 32) & (irOperand.type.form = IntermediateCode.Float)
  1067. END IsSinglePrecisionFloat;
  1068. (** whether an IR operand hold a single precision floating point value **)
  1069. PROCEDURE IsDoublePrecisionFloat(CONST irOperand: IntermediateCode.Operand): BOOLEAN;
  1070. BEGIN RETURN (irOperand.type.sizeInBits = 64) & (irOperand.type.form = IntermediateCode.Float)
  1071. END IsDoublePrecisionFloat;
  1072. PROCEDURE IsFloat(CONST irOperand: IntermediateCode.Operand): BOOLEAN;
  1073. BEGIN
  1074. RETURN irOperand.type.form = IntermediateCode.Float
  1075. END IsFloat;
  1076. (** whether an IR operand hold am integer value **)
  1077. PROCEDURE IsInteger(CONST irOperand: IntermediateCode.Operand): BOOLEAN;
  1078. BEGIN RETURN irOperand.type.form IN IntermediateCode.Integer
  1079. END IsInteger;
  1080. PROCEDURE PartType(CONST type: IntermediateCode.Type; part: LONGINT): IntermediateCode.Type;
  1081. VAR
  1082. result: IntermediateCode.Type;
  1083. BEGIN
  1084. GetPartType(type, part, result);
  1085. RETURN result
  1086. END PartType;
  1087. (* the intermediate code type of a part
  1088. - a part type is by definition directly representable in a register *)
  1089. PROCEDURE GetPartType(CONST type: IntermediateCode.Type; part: LONGINT; VAR partType: IntermediateCode.Type);
  1090. BEGIN
  1091. ASSERT((part = Low) OR (part = High));
  1092. IF (type.form = IntermediateCode.Float) & backend.useFPU THEN
  1093. IF part = Low THEN
  1094. partType := type
  1095. ELSE
  1096. partType := IntermediateCode.undef
  1097. END
  1098. ELSIF (type.form IN IntermediateCode.Integer) OR ~backend.useFPU THEN
  1099. IF type.sizeInBits <= 32 THEN
  1100. IF part = Low THEN
  1101. partType := type
  1102. ELSE
  1103. partType := IntermediateCode.undef
  1104. END
  1105. ELSIF type.sizeInBits = 64 THEN
  1106. IF part = Low THEN
  1107. partType := IntermediateCode.NewType(IntermediateCode.UnsignedInteger, 32) (* conceptually the low part is always unsigned *)
  1108. ELSE
  1109. partType := IntermediateCode.NewType(type.form, 32)
  1110. END
  1111. ELSE
  1112. HALT(100)
  1113. END
  1114. ELSE
  1115. HALT(100)
  1116. END
  1117. END GetPartType;
  1118. (** the value of a 32 bit part **)
  1119. PROCEDURE ValueOfPart(value: HUGEINT; part: LONGINT): LONGINT;
  1120. VAR
  1121. result: LONGINT;
  1122. BEGIN
  1123. IF part = Low THEN
  1124. result := LONGINT(value) (* get the 32 least significant bits *)
  1125. ELSIF part = High THEN
  1126. result := LONGINT(ASH(value, -32)) (* get the 32 most significant bits *)
  1127. ELSE
  1128. HALT(100)
  1129. END;
  1130. RETURN result
  1131. END ValueOfPart;
  1132. (** whether a 32 bit value can be directly encoded as an ARM immediate (using a 8-bit base value and 4-bit half rotation) **)
  1133. PROCEDURE ValueIsDirectlyEncodable(value: LONGINT): BOOLEAN;
  1134. VAR
  1135. baseValue, halfRotation: LONGINT;
  1136. result: BOOLEAN;
  1137. BEGIN
  1138. result := InstructionSet.EncodeImmediate(value, baseValue, halfRotation);
  1139. RETURN result
  1140. END ValueIsDirectlyEncodable;
  1141. (* whether an IR operand (or part thereof) can be directly encoded as an ARM immediate *)
  1142. PROCEDURE IrOperandIsDirectlyEncodable(irOperand: IntermediateCode.Operand; part: LONGINT): BOOLEAN;
  1143. BEGIN RETURN
  1144. (irOperand.mode = IntermediateCode.ModeImmediate) &
  1145. (irOperand.symbol.name = "") &
  1146. (irOperand.type.form IN IntermediateCode.Integer) &
  1147. ValueIsDirectlyEncodable(ValueOfPart(irOperand.intValue, part))
  1148. END IrOperandIsDirectlyEncodable;
  1149. (* whether the negation of an IR operand (or part thereof) can be directly encoded as an ARM immediate *)
  1150. PROCEDURE NegatedIrOperandIsDirectlyEncodable(irOperand: IntermediateCode.Operand; part: LONGINT): BOOLEAN;
  1151. BEGIN RETURN
  1152. (irOperand.mode = IntermediateCode.ModeImmediate) &
  1153. (irOperand.symbol.name = "") &
  1154. (irOperand.type.form IN IntermediateCode.Integer) &
  1155. ValueIsDirectlyEncodable(ValueOfPart(-irOperand.intValue, part)) (* note the minus sign *)
  1156. END NegatedIrOperandIsDirectlyEncodable;
  1157. (** generate code for a certain IR instruction **)
  1158. PROCEDURE Generate(VAR irInstruction: IntermediateCode.Instruction);
  1159. VAR
  1160. ticket: Ticket;
  1161. (* hwreg, lastUse: LONGINT; *)
  1162. BEGIN
  1163. (* CheckFixups; *)
  1164. EmitFixupBlockIfNeeded;
  1165. (*
  1166. IF ((irInstruction.opcode = IntermediateCode.mov) OR (irInstruction.opcode = IntermediateCode.pop)) & (instruction.op1.register <= IntermediateCode.ParameterRegister) THEN
  1167. hwreg := ParameterRegister(IntermediateCode.ParameterRegister-instruction.op1.register, instruction.op1.type);
  1168. Spill(physicalRegisters.Mapped(hwreg));
  1169. lastUse := inPC+1;
  1170. WHILE (lastUse < in.pc) &
  1171. ((in.instructions[lastUse].opcode # IntermediateCode.push) OR (in.instructions[lastUse].op1.register # instruction.op1.register)) & (in.instructions[lastUse].opcode # IntermediateCode.call) DO
  1172. INC(lastUse)
  1173. END;
  1174. ticket := ReservePhysicalRegister(instruction.op1.type,hwreg,lastUse);
  1175. END;
  1176. *)
  1177. ReserveOperandRegisters(irInstruction.op1, TRUE);
  1178. ReserveOperandRegisters(irInstruction.op2, TRUE);
  1179. ReserveOperandRegisters(irInstruction.op3, TRUE);
  1180. CASE irInstruction.opcode OF
  1181. | IntermediateCode.nop: (* do nothing *)
  1182. | IntermediateCode.mov: EmitMov(irInstruction, Low); IF IsComplex(irInstruction.op1) THEN EmitMov(irInstruction, High) END
  1183. | IntermediateCode.conv: EmitConv(irInstruction)
  1184. | IntermediateCode.call: EmitCall(irInstruction)
  1185. | IntermediateCode.enter: EmitEnter(irInstruction)
  1186. | IntermediateCode.leave: EmitLeave(irInstruction)
  1187. | IntermediateCode.exit: EmitExit(irInstruction)
  1188. | IntermediateCode.return: EmitReturn(irInstruction, Low); IF IsComplex(irInstruction.op1) THEN EmitReturn(irInstruction, High) END;
  1189. | IntermediateCode.result: EmitResult(irInstruction, Low); IF IsComplex(irInstruction.op1) THEN EmitResult(irInstruction, High) END;
  1190. | IntermediateCode.trap: EmitTrap(irInstruction);
  1191. | IntermediateCode.br .. IntermediateCode.brlt: EmitBr(irInstruction)
  1192. | IntermediateCode.pop: EmitPop(irInstruction.op1, Low); IF IsComplex(irInstruction.op1) THEN EmitPop(irInstruction.op1, High) END
  1193. | IntermediateCode.push: IF IsComplex(irInstruction.op1) THEN EmitPush(irInstruction.op1, High) END; EmitPush(irInstruction.op1, Low)
  1194. | IntermediateCode.neg: EmitNeg(irInstruction)
  1195. | IntermediateCode.not: EmitNot(irInstruction, Low); IF IsComplex(irInstruction.op1) THEN EmitNot(irInstruction, High) END
  1196. | IntermediateCode.abs: EmitAbs(irInstruction)
  1197. | IntermediateCode.mul: EmitMul(irInstruction)
  1198. | IntermediateCode.div: EmitDiv(irInstruction)
  1199. | IntermediateCode.mod: EmitMod(irInstruction)
  1200. | IntermediateCode.sub, IntermediateCode.add: EmitAddOrSub(irInstruction)
  1201. | IntermediateCode.and: EmitAnd(irInstruction, Low); IF IsComplex(irInstruction.op1) THEN EmitAnd(irInstruction, High) END
  1202. | IntermediateCode.or: EmitOr(irInstruction, Low); IF IsComplex(irInstruction.op1) THEN EmitOr(irInstruction, High) END
  1203. | IntermediateCode.xor: EmitXor(irInstruction, Low); IF IsComplex(irInstruction.op1) THEN EmitXor(irInstruction, High) END
  1204. | IntermediateCode.shl: EmitShiftOrRotation(irInstruction)
  1205. | IntermediateCode.shr: EmitShiftOrRotation(irInstruction)
  1206. | IntermediateCode.rol: EmitShiftOrRotation(irInstruction)
  1207. | IntermediateCode.ror: EmitShiftOrRotation(irInstruction)
  1208. | IntermediateCode.cas: EmitCas(irInstruction);
  1209. | IntermediateCode.copy: EmitCopy(irInstruction)
  1210. | IntermediateCode.fill: EmitFill(irInstruction, FALSE)
  1211. | IntermediateCode.asm: EmitAsm(irInstruction)
  1212. | IntermediateCode.special: EmitSpecial(irInstruction)
  1213. END;
  1214. ReserveOperandRegisters(irInstruction.op3, FALSE);
  1215. ReserveOperandRegisters(irInstruction.op2 ,FALSE);
  1216. ReserveOperandRegisters(irInstruction.op1, FALSE);
  1217. END Generate;
  1218. PROCEDURE PostGenerate(CONST instruction: IntermediateCode.Instruction);
  1219. VAR ticket: Ticket;
  1220. BEGIN
  1221. TryUnmap(instruction.op3); TryUnmap(instruction.op2); TryUnmap(instruction.op1);
  1222. ticket := tickets.live;
  1223. WHILE (ticket # NIL) & (ticket.lastuse = inPC) DO
  1224. UnmapTicket(ticket);
  1225. ticket := tickets.live
  1226. END;
  1227. END PostGenerate;
  1228. PROCEDURE EmitFinalFixupBlock;
  1229. BEGIN
  1230. IF listOfReferences.referenceCount > 0 THEN
  1231. ASSERT(in.pc > 0);
  1232. IF in.instructions[in.pc - 1].opcode # IntermediateCode.exit THEN
  1233. (* there is no exit instruction at the end of the IR section -> emit a branch that skips the fixup block (in particular used by @BodyStub procedures)*)
  1234. Emit1(opB, InstructionSet.NewImmediate((listOfReferences.referenceCount + 1) * 4 - 8))
  1235. END
  1236. END;
  1237. EmitFixupBlock; (* emit the fixup block *)
  1238. END EmitFinalFixupBlock;
  1239. (* if needed, emit fixup block for all used symbol references
  1240. - the fixup block is skipped by a branch instruction
  1241. - afterwards, the list of references is cleared
  1242. *)
  1243. PROCEDURE EmitFixupBlockIfNeeded;
  1244. BEGIN
  1245. IF out.pc - listOfReferences.pcOfFirstCitation + listOfReferences.referenceCount + 1 > MaximumFixupDistance THEN
  1246. Emit1(opB, InstructionSet.NewImmediate((listOfReferences.referenceCount + 1) * 4 - 8)); (* emit branch instruction that skips the fixup block *)
  1247. EmitFixupBlock; (* emit the fixup block *)
  1248. listOfReferences.Init (* clear the list *)
  1249. END
  1250. END EmitFixupBlockIfNeeded;
  1251. (* emit fixup block for all used symbol references, and clear the list *)
  1252. PROCEDURE EmitFixupBlock;
  1253. VAR
  1254. reference: Reference;
  1255. citation: Citation;
  1256. fixup: BinaryCode.Fixup;
  1257. patchValue: LONGINT;
  1258. identifier: ObjectFile.Identifier;
  1259. BEGIN
  1260. IF listOfReferences.referenceCount > 0 THEN
  1261. IF out.comments # NIL THEN
  1262. out.comments.String("REFERENCES BLOCK"); out.comments.String(" (");
  1263. out.comments.Int(listOfReferences.referenceCount, 0);
  1264. out.comments.String(" references):"); out.comments.Ln; out.comments.Update
  1265. END;
  1266. reference := listOfReferences.firstReference;
  1267. WHILE reference # NIL DO
  1268. (* 1. patch all of the citations, i.e., the LDR instructions that use the symbol reference *)
  1269. citation := reference.firstCitation;
  1270. WHILE citation # NIL DO
  1271. patchValue := out.pc - 8 - citation.pc;
  1272. ASSERT((0 <= patchValue) & (patchValue < InstructionSet.Bits12));
  1273. out.PutBitsAt(citation.pc, patchValue, 12);
  1274. citation := citation.next
  1275. END;
  1276. IF reference IS SymbolReference THEN
  1277. WITH reference: SymbolReference DO
  1278. (* alternative version that relies on the fixup mechanism:
  1279. NEW(fixupPattern12, 1);
  1280. fixupPattern12[0].offset := 0;
  1281. fixupPattern12[0].bits := 12;
  1282. fixup := BinaryCode.NewFixup(BinaryCode.Relative, entry.pc, in, 0, out.pc - 8, 0, fixupPattern12); (* TODO: determine the correct displacement *)
  1283. out.fixupList.AddFixup(fixup);
  1284. *)
  1285. (* 2. add an absolute fixup for the symbol reference and emit space *)
  1286. IF out.comments # NIL THEN
  1287. out.comments.String("fixup location for ");
  1288. Basic.WriteSegmentedName(out.comments, reference.symbol);
  1289. out.comments.String(":"); out.comments.Int(reference.symbolOffset, 0);
  1290. out.comments.String(" :"); out.comments.Ln; out.comments.Update
  1291. END;
  1292. identifier.name := reference.symbol;
  1293. identifier.fingerprint := reference.fingerprint;
  1294. fixup := BinaryCode.NewFixup(BinaryCode.Absolute, out.pc, identifier, reference.symbolOffset, 0, 0, fixupPattern);
  1295. out.fixupList.AddFixup(fixup);
  1296. out.PutBits(0, 32);
  1297. END;
  1298. ELSIF reference IS ImmediateReference THEN
  1299. WITH reference: ImmediateReference DO
  1300. IF out.comments # NIL THEN
  1301. out.comments.String("immediate value"); out.comments.Ln; out.comments.Update;
  1302. END;
  1303. out.PutBits(reference.value,32);
  1304. END
  1305. END;
  1306. reference := reference.next
  1307. END
  1308. END
  1309. END EmitFixupBlock;
  1310. (** get an ARM operand that hold a certain value
  1311. - if possible the value is returned as an ARM immediate operand
  1312. - otherwise a register is returned instead (if a register hint is present, it is used) **)
  1313. PROCEDURE OperandFromValue(value: LONGINT; registerHint: Operand): Operand;
  1314. VAR
  1315. result: Operand;
  1316. BEGIN
  1317. IF ValueIsDirectlyEncodable(value) THEN
  1318. result := InstructionSet.NewImmediate(value)
  1319. ELSE
  1320. result := RegisterFromValue(value, registerHint)
  1321. END;
  1322. RETURN result
  1323. END OperandFromValue;
  1324. (** get a single precision VFP register that holds a certain floating point value **)
  1325. PROCEDURE SinglePrecisionFloatRegisterFromValue(value: REAL; registerHint: Operand): Operand;
  1326. VAR
  1327. intValue, dummy: LONGINT;
  1328. result, temp: Operand;
  1329. BEGIN
  1330. intValue := SYSTEM.VAL(LONGINT, value);
  1331. (* alternative: integerValue := BinaryCode.ConvertReal(value) *)
  1332. temp := GetFreeRegisterOrHint(IntermediateCode.UnsignedIntegerType(32), registerHint);
  1333. dummy := ValueComposition(intValue, TRUE, temp);
  1334. result := GetFreeRegisterOrHint(IntermediateCode.FloatType(32), registerHint);
  1335. Emit2(opFMSR, result, temp);
  1336. ASSERT(result.mode = InstructionSet.modeRegister);
  1337. ASSERT((result.register >= InstructionSet.SR0) & (result.register <= InstructionSet.SR31));
  1338. RETURN result;
  1339. END SinglePrecisionFloatRegisterFromValue;
  1340. (** get an ARM register that holds a certain integer value
  1341. - if a register hint is present, it is used **)
  1342. PROCEDURE RegisterFromValue(value: LONGINT; registerHint: Operand): Operand;
  1343. VAR
  1344. dummy: LONGINT;
  1345. result: Operand;
  1346. BEGIN
  1347. result := GetFreeRegisterOrHint(IntermediateCode.SignedIntegerType(32), registerHint);
  1348. IF ValueComposition(value, FALSE, result) < 3 THEN
  1349. dummy := ValueComposition(value, TRUE, result);
  1350. ELSE
  1351. result := GetFreeRegisterOrHint(IntermediateCode.UnsignedIntegerType(32), registerHint);
  1352. listOfReferences.AddImmediate(value, out.pc);
  1353. Emit2(opLDR, result, InstructionSet.NewImmediateOffsetMemory(opPC.register, 0, {InstructionSet.Increment})); (* LDR ..., [PC, #+???] *)
  1354. END;
  1355. ASSERT(result.mode = InstructionSet.modeRegister);
  1356. ASSERT((result.register >= InstructionSet.R0) & (result.register <= InstructionSet.R15));
  1357. RETURN result
  1358. END RegisterFromValue;
  1359. (** allocate or deallocate on the stack
  1360. - note: updateStackSize is important as intermediate RETURNs should not change stack size
  1361. **)
  1362. PROCEDURE AllocateStack(allocationSize: LONGINT; doUpdateStackSize: BOOLEAN; clear: BOOLEAN);
  1363. VAR
  1364. operand, zero, count: InstructionSet.Operand; i: LONGINT;
  1365. BEGIN
  1366. inStackAllocation := TRUE;
  1367. operand := OperandFromValue(ABS(allocationSize), emptyOperand);
  1368. IF allocationSize > 0 THEN
  1369. IF clear THEN
  1370. zero := InstructionSet.NewRegister(0, None, None, 0);
  1371. Emit2(opMOV, zero , InstructionSet.NewImmediate(0));
  1372. IF allocationSize < 16 THEN
  1373. FOR i := 0 TO allocationSize-1 BY 4 DO
  1374. Emit2(opSTR, InstructionSet.NewRegister(0, None, None, 0), InstructionSet.NewImmediateOffsetMemory(InstructionSet.SP, 4, {InstructionSet.Decrement, InstructionSet.PreIndexed}));
  1375. END;
  1376. ELSE
  1377. count := InstructionSet.NewRegister(1, None, None, 0);
  1378. Emit1(opB, InstructionSet.NewImmediate(0)); (* PC offset = 8 ! Jump over immediate *)
  1379. out.PutBits(allocationSize DIV 4, 32);
  1380. Emit2(opLDR, count, InstructionSet.NewImmediateOffsetMemory(InstructionSet.PC, 8+4, {InstructionSet.Decrement}));
  1381. (* label *)
  1382. Emit2(opSTR, zero, InstructionSet.NewImmediateOffsetMemory(InstructionSet.SP, 4, {InstructionSet.Decrement, InstructionSet.PreIndexed}));
  1383. Emit3WithFlags(opSUB, count, count, InstructionSet.NewImmediate(1),{InstructionSet.flagS});
  1384. Emit1WithCondition(opB, InstructionSet.NewImmediate(-8 -8), InstructionSet.conditionGT); (* label *)
  1385. END;
  1386. ELSE
  1387. Emit3(opSUB, opSP, opSP, operand) (* decreasing SP: allocation *)
  1388. END;
  1389. ELSIF allocationSize < 0 THEN
  1390. Emit3(opADD, opSP, opSP, operand) (* increasing SP: deallocation *)
  1391. END;
  1392. IF doUpdateStackSize THEN stackSize := stackSize + allocationSize END;
  1393. inStackAllocation := FALSE
  1394. END AllocateStack;
  1395. (** whether two ARM operands represent the same physical register **)
  1396. PROCEDURE IsSameRegister(CONST a, b: Operand): BOOLEAN;
  1397. BEGIN RETURN (a.mode = InstructionSet.modeRegister) & (b.mode = InstructionSet.modeRegister) & (a.register = b.register)
  1398. END IsSameRegister;
  1399. (** emit a MOV instruction if the two operands do not represent the same register
  1400. - for moves involving floating point registers special VFP instructions opFCPYS, opFMSR and opFMRS are used
  1401. **)
  1402. PROCEDURE MovIfDifferent(CONST a, b: Operand);
  1403. BEGIN
  1404. IF ~IsSameRegister(a, b) THEN
  1405. ASSERT(a.mode = InstructionSet.modeRegister);
  1406. IF IsRegisterForType(a.register, IntermediateCode.FloatType(32)) THEN
  1407. IF IsRegisterForType(b.register, IntermediateCode.FloatType(32)) THEN
  1408. (* mov float, float: *)
  1409. Emit2(opFCPYS, a, b)
  1410. ELSE
  1411. (* mov float, int: *)
  1412. Emit2(opFMSR, a, b)
  1413. END
  1414. ELSE
  1415. IF IsRegisterForType(b.register, IntermediateCode.FloatType(32)) THEN
  1416. (* mov int, float: *)
  1417. Emit2(opFMRS, a, b)
  1418. ELSE
  1419. (* mov int, int: *)
  1420. Emit2(opMOV, a, b)
  1421. END
  1422. END
  1423. END
  1424. END MovIfDifferent;
  1425. (** acquire an ARM register fr oa IR destination operand part
  1426. - if IR operand is a memory location, get a temporary register (if provided the hinted register is used)
  1427. - if IR operand is an IR register, get the ARM register that is mapped to the corresponding part
  1428. **)
  1429. PROCEDURE AcquireDestinationRegister(CONST irDestinationOperand: IntermediateCode.Operand; part: LONGINT; registerHint: Operand): Operand;
  1430. VAR
  1431. result: Operand;
  1432. BEGIN
  1433. IF irDestinationOperand.mode = IntermediateCode.ModeMemory THEN
  1434. result := GetFreeRegisterOrHint(PartType(irDestinationOperand.type, part), registerHint)
  1435. ELSIF irDestinationOperand.mode = IntermediateCode.ModeRegister THEN
  1436. ASSERT(irDestinationOperand.offset = 0);
  1437. IF virtualRegisters.Mapped(irDestinationOperand.register, part) = NIL THEN TryAllocate(irDestinationOperand, part) END; (* create the mapping if not yet done *)
  1438. result := InstructionSet.NewRegister(PhysicalRegisterNumber(irDestinationOperand.register, part), None, None, 0)
  1439. ELSE
  1440. HALT(100)
  1441. END;
  1442. ASSERT(result.mode = InstructionSet.modeRegister);
  1443. RETURN result
  1444. END AcquireDestinationRegister;
  1445. (** write the content of an ARM register to an IR destination operand (memory location or IR register)
  1446. - afterwards, try to release the register
  1447. **)
  1448. PROCEDURE WriteBack(VAR irDestinationOperand: IntermediateCode.Operand; part: LONGINT; register: Operand);
  1449. VAR
  1450. mappedArmRegister: Operand;
  1451. BEGIN
  1452. ASSERT(register.mode = InstructionSet.modeRegister);
  1453. IF irDestinationOperand.mode = IntermediateCode.ModeMemory THEN
  1454. Store(register, MemoryOperandFromIrMemoryOperand(irDestinationOperand, part, emptyOperand), PartType(irDestinationOperand.type, part))
  1455. ELSIF irDestinationOperand.mode = IntermediateCode.ModeRegister THEN
  1456. ASSERT((virtualRegisters.Mapped(irDestinationOperand.register, part) # NIL)
  1457. OR (irDestinationOperand.register = IntermediateCode.SP)
  1458. OR (irDestinationOperand.register = IntermediateCode.FP));
  1459. mappedArmRegister := InstructionSet.NewRegister(PhysicalRegisterNumber(irDestinationOperand.register, part), None, None, 0);
  1460. MovIfDifferent(mappedArmRegister, register)
  1461. ELSE
  1462. HALT(100)
  1463. END;
  1464. ReleaseHint(register.register)
  1465. END WriteBack;
  1466. PROCEDURE ZeroExtendOperand(operand: Operand; sizeInBits: LONGINT);
  1467. BEGIN
  1468. ASSERT(sizeInBits <= 32);
  1469. IF operand.mode = InstructionSet.modeRegister THEN
  1470. IF sizeInBits = 8 THEN
  1471. Emit3(opAND, operand, operand, InstructionSet.NewImmediate(255)); (* AND reg, reg, 11111111b *)
  1472. ELSIF sizeInBits = 16 THEN
  1473. Emit2(opMOV, operand, InstructionSet.NewRegister(operand.register, InstructionSet.shiftLSL, None, 16));
  1474. Emit2(opMOV, operand, InstructionSet.NewRegister(operand.register, InstructionSet.shiftLSR, None, 16))
  1475. ELSIF sizeInBits = 32 THEN
  1476. (* nothing to do *)
  1477. ELSE
  1478. HALT(100)
  1479. END
  1480. END
  1481. END ZeroExtendOperand;
  1482. PROCEDURE SignExtendOperand(operand: Operand; sizeInBits: LONGINT);
  1483. BEGIN
  1484. ASSERT(sizeInBits <= 32);
  1485. IF operand.mode = InstructionSet.modeRegister THEN
  1486. IF sizeInBits < 32 THEN
  1487. Emit2(opMOV, operand, InstructionSet.NewRegister(operand.register, InstructionSet.shiftLSL, None, 32 - sizeInBits));
  1488. Emit2(opMOV, operand, InstructionSet.NewRegister(operand.register, InstructionSet.shiftASR, None, 32 - sizeInBits))
  1489. END
  1490. END
  1491. END SignExtendOperand;
  1492. (** sign or zero-extends the content of an operand to 32 bits, depending on the IR type **)
  1493. PROCEDURE SignOrZeroExtendOperand(operand: Operand; irType: IntermediateCode.Type);
  1494. BEGIN
  1495. ASSERT(irType.sizeInBits <= 32);
  1496. IF irType.form = IntermediateCode.UnsignedInteger THEN
  1497. ZeroExtendOperand(operand, irType.sizeInBits)
  1498. ELSE
  1499. SignExtendOperand(operand, irType.sizeInBits)
  1500. END
  1501. END SignOrZeroExtendOperand;
  1502. (* ACTUAL CODE GENERATION *)
  1503. PROCEDURE EmitPush(VAR irOperand: IntermediateCode.Operand; part: LONGINT);
  1504. VAR
  1505. register: Operand;
  1506. partType: IntermediateCode.Type;
  1507. (*pc: LONGINT;*)
  1508. BEGIN
  1509. register := RegisterFromIrOperand(irOperand, part, emptyOperand);
  1510. IF ~IsRegisterForType(register.register, IntermediateCode.FloatType(32)) THEN
  1511. Emit2(opSTR, register, InstructionSet.NewImmediateOffsetMemory(InstructionSet.SP, 4, {InstructionSet.Decrement, InstructionSet.PreIndexed}));
  1512. ELSE
  1513. partType := PartType(irOperand.type, part);
  1514. AllocateStack(MAX(4, partType.sizeInBits DIV 8), TRUE,FALSE);
  1515. Store(register, InstructionSet.NewImmediateOffsetMemory(InstructionSet.SP, 0, {InstructionSet.Increment}), PartType(irOperand.type, part));
  1516. END;
  1517. (*
  1518. (* optimization for push chains (THIS DOES NOT WORK IF inEmulation) *)
  1519. IF pushChainLength = 0 THEN
  1520. pc := inPC;
  1521. (* search for consecutive push instructions *)
  1522. WHILE (pc < in.pc) & (in.instructions[pc].opcode = IntermediateCode.push) DO
  1523. ASSERT(in.instructions[pc].op1.mode # IntermediateCode.Undefined);
  1524. INC(pushChainLength, MAX(4, in.instructions[pc].op1.type.sizeInBits DIV 8));
  1525. INC(pc)
  1526. END;
  1527. AllocateStack(pushChainLength, TRUE)
  1528. END;
  1529. DEC(pushChainLength, 4); (* for 64 bit operands, this procedure is executed twice -> the push chain will be decremented by 8 bytes *)
  1530. register := RegisterFromIrOperand(irOperand, part, emptyOperand);
  1531. ASSERT(pushChainLength < InstructionSet.Bits12, 100);
  1532. ASSERT((pushChainLength MOD 4) = 0);
  1533. Store(register, InstructionSet.NewImmediateOffsetMemory(InstructionSet.SP, pushChainLength, {InstructionSet.Increment}), PartType(irOperand.type, part))
  1534. *)
  1535. END EmitPush;
  1536. PROCEDURE EmitPop(VAR irOperand: IntermediateCode.Operand; part: LONGINT);
  1537. VAR
  1538. register: Operand; partType: IntermediateCode.Type;
  1539. BEGIN
  1540. register := AcquireDestinationRegister(irOperand, part, emptyOperand);
  1541. IF ~IsRegisterForType(register.register, IntermediateCode.FloatType(32)) THEN
  1542. (*Emit2(opLDR, register, InstructionSet.NewImmediateOffsetMemory(InstructionSet.SP, 4, {InstructionSet.Increment, InstructionSet.PostIndexed}));*)
  1543. Load(register, InstructionSet.NewImmediateOffsetMemory(InstructionSet.SP, 4, {InstructionSet.Increment, InstructionSet.PostIndexed}), PartType(irOperand.type, part));
  1544. ELSE
  1545. Load(register, InstructionSet.NewImmediateOffsetMemory(InstructionSet.SP, 0, {InstructionSet.Increment}), PartType(irOperand.type, part));
  1546. partType := PartType(irOperand.type, part);
  1547. AllocateStack(-MAX(4, partType.sizeInBits DIV 8), TRUE,FALSE);
  1548. END;
  1549. WriteBack(irOperand, part, register)
  1550. END EmitPop;
  1551. PROCEDURE Resolve(VAR op: IntermediateCode.Operand);
  1552. BEGIN
  1553. IF (op.symbol.name # "") & (op.resolved = NIL) THEN op.resolved := module.allSections.FindByName(op.symbol.name) END
  1554. END Resolve;
  1555. (* call <address>, <parSize> *)
  1556. PROCEDURE EmitCall(VAR irInstruction: IntermediateCode.Instruction);
  1557. VAR
  1558. code: BinaryCode.Section;
  1559. fixup, newFixup: BinaryCode.Fixup;
  1560. BEGIN
  1561. Resolve(irInstruction.op1);
  1562. IF (irInstruction.op1.resolved # NIL) & (irInstruction.op1.resolved.type = Sections.InlineCodeSection) THEN
  1563. (* call of an inline procedure: *)
  1564. code := irInstruction.op1.resolved(IntermediateCode.Section).resolved;
  1565. ASSERT(code # NIL); (* TODO: what if section is not yet resolved, i.e., code has not yet been generated? *)
  1566. IF (out.comments # NIL) THEN
  1567. out.comments.String("inlined code sequence:");
  1568. out.comments.Ln;
  1569. out.comments.Update;
  1570. END;
  1571. (* emit the generated code of the other section *)
  1572. out.CopyBits(code.os.bits, 0, code.os.bits.GetSize());
  1573. (* transfer the fixups *)
  1574. fixup := code.fixupList.firstFixup;
  1575. WHILE fixup # NIL DO
  1576. newFixup := BinaryCode.NewFixup(fixup.mode, fixup.offset + code.pc, fixup.symbol, fixup.symbolOffset, fixup.displacement, fixup.scale, fixup.pattern);
  1577. out.fixupList.AddFixup(newFixup);
  1578. fixup := fixup.nextFixup
  1579. END
  1580. ELSE
  1581. (* store the address of the procedure in a register and branch and link there *)
  1582. Emit1(opBLX, RegisterFromIrOperand(irInstruction.op1, Low, emptyOperand));
  1583. (* remove parameters on stack *)
  1584. AllocateStack(-LONGINT(irInstruction.op2.intValue), TRUE, FALSE)
  1585. END
  1586. END EmitCall;
  1587. (* enter <callingConvention>, <pafSize>, <numRegParams> *)
  1588. PROCEDURE EmitEnter(CONST irInstruction: IntermediateCode.Instruction);
  1589. VAR allocationSize: LONGINT;
  1590. BEGIN
  1591. (* store registers for interrupts, if required *)
  1592. IF (irInstruction.op1.intValue = SyntaxTree.InterruptCallingConvention) THEN (* TODO: needed? *)
  1593. (* push R0-R11, FP and LR *)
  1594. Emit2WithFlags(opSTM, opSP, InstructionSet.NewRegisterList(0, {InstructionSet.FP, InstructionSet.LR, 0..11}), {InstructionSet.flagDB, InstructionSet.flagBaseRegisterUpdate});
  1595. Emit2(opMOV, opFP, opSP);
  1596. END;
  1597. stackSize := 0;
  1598. (* allocate space on stack for local variables *)
  1599. allocationSize := LONGINT(irInstruction.op2.intValue);
  1600. Basic.Align(allocationSize, 4); (* 4 byte alignment *)
  1601. AllocateStack(allocationSize, TRUE, backend.initLocals);
  1602. (* allocate space on stack for register spills *)
  1603. spillStackStart := -stackSize;
  1604. IF spillStack.MaxSize() > 0 THEN AllocateStack(spillStack.MaxSize(), TRUE, FALSE) END
  1605. END EmitEnter;
  1606. (* leave <callingConvention> *)
  1607. PROCEDURE EmitLeave(CONST irInstruction: IntermediateCode.Instruction);
  1608. BEGIN
  1609. (* LDMFD (Full Descending) aka LDMIA (Increment After) *)
  1610. IF (irInstruction.op1.intValue = SyntaxTree.InterruptCallingConvention) THEN
  1611. (* pop R0-R11, FP and LR *)
  1612. Emit2(opMOV, opSP, opFP);
  1613. Emit2WithFlags(opLDM, opSP, InstructionSet.NewRegisterList(0, {InstructionSet.FP, InstructionSet.LR, 0..11}), {InstructionSet.flagIA, InstructionSet.flagBaseRegisterUpdate})
  1614. END
  1615. END EmitLeave;
  1616. (* exit <parSize>, <pcOffset> *)
  1617. PROCEDURE EmitExit(CONST irInstruction: IntermediateCode.Instruction);
  1618. BEGIN
  1619. Emit2(opLDR, opLR, InstructionSet.NewImmediateOffsetMemory(InstructionSet.SP, 4, {InstructionSet.Increment, InstructionSet.PostIndexed}));
  1620. IF (irInstruction.op1.intValue = 0) & (irInstruction.op2.intValue # SyntaxTree.InterruptCallingConvention) THEN
  1621. (* Emit2(opMOV, opPC, opLR) *)
  1622. Emit1(opBX, opLR) (* recommended for better interoperability between ARM and Thumb *)
  1623. ELSE
  1624. IF (irInstruction.op2.intValue = SyntaxTree.InterruptCallingConvention) THEN
  1625. Emit3WithFlags(opSUB, opPC, opLR, InstructionSet.NewImmediate(LONGINT(irInstruction.op1.intValue)),{InstructionSet.flagS})
  1626. ELSE
  1627. (* exit from an ARM interrupt procedure that has a PC offset *)
  1628. Emit3(opSUB, opPC, opLR, InstructionSet.NewImmediate(LONGINT(irInstruction.op1.intValue)))
  1629. END;
  1630. END
  1631. END EmitExit;
  1632. PROCEDURE EmitMov(VAR irInstruction: IntermediateCode.Instruction; part: LONGINT);
  1633. VAR
  1634. destinationRegister, sourceOperand: Operand;
  1635. BEGIN
  1636. IF irInstruction.op1.mode # IntermediateCode.ModeRegister THEN
  1637. (* optimization: mov [?], r? it is more optimal to determine the source operand first *)
  1638. sourceOperand := RegisterOrImmediateFromIrOperand(irInstruction.op2, part, emptyOperand);
  1639. destinationRegister := GetFreeRegisterOrHint(PartType(irInstruction.op2.type, part), sourceOperand) (* note that the source operand (possibly a register) is used as hint *)
  1640. ELSE
  1641. PrepareSingleSourceOpWithImmediate(irInstruction, part, destinationRegister, sourceOperand);
  1642. END;
  1643. MovIfDifferent(destinationRegister, sourceOperand);
  1644. WriteBack(irInstruction.op1, part, destinationRegister)
  1645. END EmitMov;
  1646. (* BITWISE LOGICAL OPERATIONS *)
  1647. PROCEDURE EmitNot(VAR irInstruction: IntermediateCode.Instruction; part: LONGINT);
  1648. VAR
  1649. destination, source: Operand;
  1650. BEGIN
  1651. PrepareSingleSourceOpWithImmediate(irInstruction, part, destination, source);
  1652. Emit2(opMVN, destination, source); (* invert bits *)
  1653. WriteBack(irInstruction.op1, part, destination)
  1654. END EmitNot;
  1655. PROCEDURE EmitAnd(VAR irInstruction: IntermediateCode.Instruction; part: LONGINT);
  1656. VAR
  1657. dummy: BOOLEAN;
  1658. destination, left, right: Operand;
  1659. BEGIN
  1660. PrepareDoubleSourceOpWithImmediate(irInstruction, part, destination, left, right, dummy);
  1661. Emit3(opAND, destination, left, right);
  1662. WriteBack(irInstruction.op1, part, destination)
  1663. END EmitAnd;
  1664. PROCEDURE EmitOr(VAR irInstruction: IntermediateCode.Instruction; part: LONGINT);
  1665. VAR
  1666. dummy: BOOLEAN;
  1667. destination, left, right: Operand;
  1668. BEGIN
  1669. PrepareDoubleSourceOpWithImmediate(irInstruction, part, destination, left, right, dummy);
  1670. Emit3(opORR, destination, left, right);
  1671. WriteBack(irInstruction.op1, part, destination)
  1672. END EmitOr;
  1673. PROCEDURE EmitXor(VAR irInstruction: IntermediateCode.Instruction; part: LONGINT);
  1674. VAR
  1675. dummy: BOOLEAN;
  1676. destination, left, right: Operand;
  1677. BEGIN
  1678. PrepareDoubleSourceOpWithImmediate(irInstruction, part, destination, left, right, dummy);
  1679. Emit3(opEOR, destination, left, right);
  1680. WriteBack(irInstruction.op1, part, destination)
  1681. END EmitXor;
  1682. (* ARITHMETIC OPERATIONS *)
  1683. (*
  1684. - TODO: double precision floats
  1685. - note that for operand sizes 8 and 16, the unused bits of the result might be in a unpredictable state (sign/zero-extension is not done on purpose)
  1686. *)
  1687. PROCEDURE EmitAddOrSub(VAR irInstruction: IntermediateCode.Instruction);
  1688. VAR
  1689. destination, left, right: Operand;
  1690. (* registerSR0, registerSR1, registerSR2: Operand; *)
  1691. BEGIN
  1692. IF IsSinglePrecisionFloat(irInstruction.op1) THEN
  1693. ASSERT(backend.useFPU);
  1694. PrepareDoubleSourceOp(irInstruction, Low, destination, left, right);
  1695. IF irInstruction.opcode = IntermediateCode.add THEN
  1696. Emit3(opFADDS, destination, left, right)
  1697. ELSE
  1698. Emit3(opFSUBS, destination, left, right)
  1699. END;
  1700. WriteBack(irInstruction.op1, Low, destination)
  1701. ELSIF IsInteger(irInstruction.op1) THEN
  1702. IF IsComplex(irInstruction.op1) THEN
  1703. EmitPartialAddOrSub(irInstruction, Low, TRUE);
  1704. EmitPartialAddOrSub(irInstruction, High, FALSE)
  1705. ELSE
  1706. EmitPartialAddOrSub(irInstruction, Low, FALSE)
  1707. END
  1708. ELSE
  1709. HALT(200)
  1710. END
  1711. END EmitAddOrSub;
  1712. PROCEDURE EmitPartialAddOrSub(CONST irInstruction: IntermediateCode.Instruction; part: LONGINT; doUpdateFlags: BOOLEAN);
  1713. VAR
  1714. destination, left, right, hint: Operand;
  1715. irDestination, irLeft, irRight: IntermediateCode.Operand;
  1716. operation: LONGINT;
  1717. doSwap, doNegateRight: BOOLEAN;
  1718. BEGIN
  1719. irDestination := irInstruction.op1; irLeft := irInstruction.op2; irRight := irInstruction.op3;
  1720. doSwap := FALSE; doNegateRight := FALSE; (* defaults *)
  1721. IF irInstruction.opcode = IntermediateCode.add THEN
  1722. IF IrOperandIsDirectlyEncodable(irRight, part) THEN
  1723. (* add r0, r1, 16 ~> ADD R0, R1, #16 *)
  1724. operation := opADD
  1725. ELSIF IrOperandIsDirectlyEncodable(irLeft, part) THEN
  1726. (* add r0, 16, r1 ~> ADD R0, R1, #16 *)
  1727. operation := opADD; doSwap := TRUE
  1728. ELSIF NegatedIrOperandIsDirectlyEncodable(irRight, part) THEN
  1729. (* add r0, r1, -16 ~> SUB R0, R1, #16 *)
  1730. operation := opSUB; doNegateRight := TRUE
  1731. ELSIF NegatedIrOperandIsDirectlyEncodable(irLeft, part) THEN
  1732. (* add r0, -16, r1 ~> SUB R0, R1, #16 *)
  1733. operation := opSUB; doSwap := TRUE; doNegateRight := TRUE
  1734. ELSE
  1735. operation := opADD
  1736. END
  1737. ELSIF irInstruction.opcode = IntermediateCode.sub THEN
  1738. IF IrOperandIsDirectlyEncodable(irRight, part) THEN
  1739. (* sub r0, r1, 16 ~> SUB R0, R1, #16 *)
  1740. operation := opSUB
  1741. ELSIF IrOperandIsDirectlyEncodable(irLeft, part) THEN
  1742. (* sub r0, 16, r1 ~> RSB R0, R1, #16 *)
  1743. operation := opRSB; doSwap := TRUE
  1744. ELSIF NegatedIrOperandIsDirectlyEncodable(irRight, part) THEN
  1745. (* sub r0, r1, -16 ~> ADD R0, R1, #16 *)
  1746. operation := opADD; doNegateRight := TRUE
  1747. ELSE
  1748. operation := opSUB
  1749. END
  1750. ELSE
  1751. HALT(100)
  1752. END;
  1753. (* get destination operand *)
  1754. destination := AcquireDestinationRegister(irDestination, part, emptyOperand);
  1755. (* get source operands *)
  1756. IF doSwap THEN SwapIrOperands(irLeft, irRight) END; (* if needed, swap operands *)
  1757. (* TODO: revise this! *)
  1758. IF IsSameRegister(right, destination) THEN hint := destination ELSE hint := emptyOperand END;
  1759. left := RegisterFromIrOperand(irLeft, part, hint);
  1760. IF doNegateRight THEN
  1761. ASSERT(NegatedIrOperandIsDirectlyEncodable(irRight, part));
  1762. right := InstructionSet.NewImmediate(-ValueOfPart(irRight.intValue, part))
  1763. ELSE
  1764. right := RegisterOrImmediateFromIrOperand(irRight, part, emptyOperand)
  1765. END;
  1766. (* if needed, use operation that incorporates carry *)
  1767. IF part # Low THEN
  1768. CASE operation OF
  1769. | opADD: operation := opADC
  1770. | opSUB: operation := opSBC
  1771. | opRSB: operation := opRSC
  1772. ELSE HALT(100)
  1773. END
  1774. END;
  1775. IF doUpdateFlags THEN
  1776. Emit3WithFlags(operation, destination, left, right, {InstructionSet.flagS})
  1777. ELSE
  1778. Emit3(operation, destination, left, right)
  1779. END;
  1780. WriteBack(irDestination, part, destination)
  1781. END EmitPartialAddOrSub;
  1782. PROCEDURE EmitMul(VAR irInstruction: IntermediateCode.Instruction);
  1783. VAR
  1784. destination, left, right: ARRAY 2 OF Operand;
  1785. BEGIN
  1786. IF IsSinglePrecisionFloat(irInstruction.op1) THEN
  1787. ASSERT(backend.useFPU);
  1788. PrepareDoubleSourceOp(irInstruction, Low, destination[Low], left[Low], right[Low]);
  1789. Emit3(opFMULS, destination[Low], left[Low], right[Low]);
  1790. WriteBack(irInstruction.op1, Low, destination[Low])
  1791. ELSIF IsInteger(irInstruction.op1) THEN
  1792. IF IsComplex(irInstruction.op1) THEN
  1793. ASSERT(irInstruction.op1.type.form = IntermediateCode.SignedInteger);
  1794. HALT(200);
  1795. (* TODO: fix signed 64 bit integer multiplication:
  1796. PrepareDoubleSourceOp(irInstruction, Low, destination[Low], left[Low], right[Low]);
  1797. PrepareDoubleSourceOp(irInstruction, High, destination[High], left[High], right[High]);
  1798. Emit4(opSMULL, destination[Low], destination[High], left[Low], right[Low]); (* signed long multiplication *)
  1799. Emit3(opMLA, destination[High], left[Low], right[High]); (* multiply and accumulate *)
  1800. Emit3(opMLA, destination[High], left[High], right[Low]);
  1801. WriteBack(irInstruction.op1, Low, destination[Low]);
  1802. WriteBack(irInstruction.op1, High, destination[High]);
  1803. *)
  1804. ELSE
  1805. (* signed or unsigned integer multiplication: *)
  1806. PrepareDoubleSourceOp(irInstruction, Low, destination[Low], left[Low], right[Low]);
  1807. SignOrZeroExtendOperand(left[Low], irInstruction.op2.type);
  1808. SignOrZeroExtendOperand(right[Low], irInstruction.op3.type);
  1809. Emit3(opMUL, destination[Low], left[Low], right[Low]); (* note that the sign does not matter for the least 32 significant bits *)
  1810. WriteBack(irInstruction.op1, Low, destination[Low])
  1811. END
  1812. ELSE
  1813. HALT(200)
  1814. END
  1815. END EmitMul;
  1816. PROCEDURE EmitDiv(VAR irInstruction: IntermediateCode.Instruction);
  1817. VAR
  1818. destination, left, right: Operand;
  1819. BEGIN
  1820. IF IsSinglePrecisionFloat(irInstruction.op1) THEN
  1821. ASSERT(backend.useFPU);
  1822. PrepareDoubleSourceOp(irInstruction, Low, destination, left, right);
  1823. Emit3(opFDIVS, destination, left, right);
  1824. WriteBack(irInstruction.op1, Low, destination)
  1825. ELSE
  1826. HALT(200)
  1827. END
  1828. END EmitDiv;
  1829. PROCEDURE EmitMod(CONST irInstruction: IntermediateCode.Instruction);
  1830. BEGIN HALT(100) (* handled by a runtime call *)
  1831. END EmitMod;
  1832. PROCEDURE EmitAbs(VAR irInstruction: IntermediateCode.Instruction);
  1833. VAR
  1834. destination, source: ARRAY 2 OF Operand;
  1835. zero: Operand;
  1836. BEGIN
  1837. IF IsInteger(irInstruction.op1) THEN
  1838. zero := InstructionSet.NewImmediate(0);
  1839. IF IsComplex(irInstruction.op1) THEN
  1840. PrepareSingleSourceOpWithImmediate(irInstruction, Low, destination[Low], source[Low]);
  1841. PrepareSingleSourceOpWithImmediate(irInstruction, High, destination[High], source[High]);
  1842. MovIfDifferent(destination[Low], source[Low]);
  1843. MovIfDifferent(destination[High], source[High]);
  1844. (* negate the value if it is negative *)
  1845. IF irInstruction.op2.type.form = IntermediateCode.SignedInteger THEN
  1846. Emit2(opCMP, destination[High], zero); (* note that only the high part has to be looked at to determine the sign *)
  1847. Emit1WithCondition(opB, InstructionSet.NewImmediate(4), InstructionSet.conditionGE); (* BGE #4 = skip the following two instructions if greater or equal *)
  1848. Emit3WithFlags(opRSB, destination[Low], destination[Low], zero, {InstructionSet.flagS}); (* RSBS *)
  1849. Emit3(opRSC, destination[High], destination[High], zero); (* RSC - reverse subtraction with carry *)
  1850. END;
  1851. WriteBack(irInstruction.op1, Low, destination[Low]);
  1852. WriteBack(irInstruction.op1, High, destination[High])
  1853. ELSE
  1854. PrepareSingleSourceOpWithImmediate(irInstruction, Low, destination[Low], source[Low]);
  1855. SignOrZeroExtendOperand(source[Low], irInstruction.op2.type);
  1856. MovIfDifferent(destination[Low], source[Low]);
  1857. (* negate the value if it is negative *)
  1858. IF irInstruction.op2.type.form = IntermediateCode.SignedInteger THEN
  1859. SignExtendOperand(destination[Low], irInstruction.op2.type.sizeInBits);
  1860. Emit2(opCMP, destination[Low], zero);
  1861. Emit3WithCondition(opRSB, destination[Low], destination[Low], zero, InstructionSet.conditionLT)
  1862. END;
  1863. WriteBack(irInstruction.op1, Low, destination[Low])
  1864. END
  1865. ELSIF IsSinglePrecisionFloat(irInstruction.op1) THEN
  1866. ASSERT(backend.useFPU);
  1867. PrepareSingleSourceOp(irInstruction, Low, destination[Low], source[Low]);
  1868. Emit2(opFABSS, destination[Low], source[Low]);
  1869. WriteBack(irInstruction.op1, Low, destination[Low])
  1870. ELSE
  1871. HALT(200)
  1872. END
  1873. END EmitAbs;
  1874. (* TODO: floats *)
  1875. PROCEDURE EmitNeg(VAR irInstruction: IntermediateCode.Instruction);
  1876. VAR
  1877. destination, source: ARRAY 2 OF Operand;
  1878. zero: Operand;
  1879. BEGIN
  1880. IF IsInteger(irInstruction.op1) THEN
  1881. zero := InstructionSet.NewImmediate(0);
  1882. IF IsComplex(irInstruction.op1) THEN
  1883. PrepareSingleSourceOpWithImmediate(irInstruction, Low, destination[Low], source[Low]);
  1884. PrepareSingleSourceOpWithImmediate(irInstruction, High, destination[High], source[High]);
  1885. Emit3WithFlags(opRSB, destination[Low], source[Low], zero, {InstructionSet.flagS}); (* RSBS *)
  1886. Emit3(opRSC, destination[High], source[High], zero); (* RSC - reverse subtraction with carry *)
  1887. WriteBack(irInstruction.op1, Low, destination[Low]);
  1888. WriteBack(irInstruction.op1, High, destination[High])
  1889. ELSE
  1890. PrepareSingleSourceOpWithImmediate(irInstruction, Low, destination[Low], source[Low]);
  1891. SignOrZeroExtendOperand(source[Low], irInstruction.op2.type);
  1892. Emit3(opRSB, destination[Low], source[Low], zero); (* reverse subtraction with zero *)
  1893. WriteBack(irInstruction.op1, Low, destination[Low])
  1894. END
  1895. ELSIF IsSinglePrecisionFloat(irInstruction.op1) THEN
  1896. ASSERT(backend.useFPU);
  1897. PrepareSingleSourceOp(irInstruction, Low, destination[Low], source[Low]);
  1898. Emit2(opFNEGS, destination[Low], source[Low]);
  1899. WriteBack(irInstruction.op1, Low, destination[Low])
  1900. ELSE
  1901. HALT(200)
  1902. END
  1903. END EmitNeg;
  1904. (*
  1905. - note that the ARM instructions ASR, LSL, LSR, ROR, etc. are actually aliases for a MOV with a shifted register operand
  1906. - note that ARM does not support LSL by 32 bits
  1907. - note that for operand sizes 8 and 16, the unused bits of the result might be in a unpredictable state (sign/zero-extension is not done on purpose)
  1908. *)
  1909. PROCEDURE EmitShiftOrRotation(VAR irInstruction: IntermediateCode.Instruction);
  1910. VAR
  1911. shiftAmountImmediate, shiftMode: LONGINT;
  1912. destination, source: ARRAY 2 OF Operand;
  1913. irShiftOperand: IntermediateCode.Operand;
  1914. temp, shiftAmountRegister: Operand;
  1915. BEGIN
  1916. ASSERT(IsInteger(irInstruction.op1), 100); (* shifts are only allowed on integers *)
  1917. destination[Low] := AcquireDestinationRegister(irInstruction.op1, Low, emptyOperand);
  1918. source[Low] := RegisterFromIrOperand(irInstruction.op2, Low, emptyOperand); (* note that the destination register cannot be used as hint for the source *)
  1919. IF IsComplex(irInstruction.op1) THEN
  1920. destination[High] := AcquireDestinationRegister(irInstruction.op1, High, emptyOperand);
  1921. source[High] := RegisterFromIrOperand(irInstruction.op2, High, emptyOperand); (* note that the destination register cannot be used as hint for the source *)
  1922. END;
  1923. irShiftOperand := irInstruction.op3;
  1924. ASSERT((irShiftOperand.type.form = IntermediateCode.UnsignedInteger) & ~IsComplex(irShiftOperand)); (* the shift operand is assumed to be a single part unsigned integer *)
  1925. (* use ARM register or shift immediate to represent IR shift operand *)
  1926. IF (irShiftOperand.mode = IntermediateCode.ModeImmediate) & (irShiftOperand.symbol.name = "") THEN
  1927. shiftAmountImmediate := LONGINT(irShiftOperand.intValue); (* note that at this point the shift amount could also be >= 32 *)
  1928. shiftAmountRegister := emptyOperand;
  1929. ASSERT(shiftAmountImmediate >= 0);
  1930. ELSE
  1931. shiftAmountImmediate := 0;
  1932. shiftAmountRegister := RegisterFromIrOperand(irShiftOperand, Low, emptyOperand);
  1933. ZeroExtendOperand(shiftAmountRegister, irShiftOperand.type.sizeInBits)
  1934. END;
  1935. CASE irInstruction.opcode OF
  1936. | IntermediateCode.ror, IntermediateCode.rol:
  1937. (* rotation: *)
  1938. IF IsComplex(irInstruction.op1) THEN HALT(100) END; (* complex rotations are handled as runtime calls *)
  1939. IF irInstruction.opcode = IntermediateCode.rol THEN
  1940. (* simple left rotation: rotate right with complementary rotation amount, since ARM does not support left rotations *)
  1941. IF shiftAmountRegister.register = None THEN
  1942. shiftAmountImmediate := 32 - shiftAmountImmediate
  1943. ELSE
  1944. IF IsSameRegister(destination[Low], source[Low]) THEN temp := GetFreeRegister(IntermediateCode.UnsignedIntegerType(32)) ELSE temp := destination[Low] END;
  1945. Emit3(opRSB, temp, shiftAmountRegister, InstructionSet.NewImmediate(32));
  1946. shiftAmountRegister := temp
  1947. END
  1948. END;
  1949. shiftAmountImmediate := shiftAmountImmediate MOD 32; (* make sure rotation amount is in range 0..31 *)
  1950. IF (shiftAmountRegister.register = None) & (shiftAmountImmediate = 0) THEN
  1951. (* simple rotation by 0: *)
  1952. Emit2(opMOV, destination[Low], source[Low])
  1953. ELSE
  1954. IF irInstruction.op1.type.sizeInBits = 8 THEN
  1955. (* simple 8 bit rotation: *)
  1956. ZeroExtendOperand(source[Low], 8);
  1957. IF IsSameRegister(destination[Low], source[Low]) THEN temp := GetFreeRegister(IntermediateCode.UnsignedIntegerType(32)) ELSE temp := destination[Low] END;
  1958. Emit2(opMOV, temp, InstructionSet.NewRegister(source[Low].register, InstructionSet.shiftROR, shiftAmountRegister.register, shiftAmountImmediate));
  1959. Emit3(opORR, temp, temp, InstructionSet.NewRegister(temp.register, InstructionSet.shiftLSR, None, 8));
  1960. Emit3(opORR, temp, temp, InstructionSet.NewRegister(temp.register, InstructionSet.shiftLSR, None, 16));
  1961. Emit3(opORR, destination[Low], temp, InstructionSet.NewRegister(temp.register, InstructionSet.shiftLSR, None, 24))
  1962. ELSIF irInstruction.op1.type.sizeInBits = 16 THEN
  1963. (* simple 16 bit rotation: *)
  1964. ZeroExtendOperand(source[Low], 16);
  1965. IF IsSameRegister(destination[Low], source[Low]) THEN temp := GetFreeRegister(IntermediateCode.UnsignedIntegerType(32)) ELSE temp := destination[Low] END;
  1966. Emit2(opMOV, temp, InstructionSet.NewRegister(source[Low].register, InstructionSet.shiftROR, shiftAmountRegister.register, shiftAmountImmediate));
  1967. Emit3(opORR, destination[Low], temp, InstructionSet.NewRegister(temp.register, InstructionSet.shiftLSR, None, 16))
  1968. ELSIF irInstruction.op1.type.sizeInBits = 32 THEN
  1969. (* simple 32 bit rotation: *)
  1970. Emit2(opMOV, destination[Low], InstructionSet.NewRegister(source[Low].register, InstructionSet.shiftROR, shiftAmountRegister.register, shiftAmountImmediate))
  1971. ELSE
  1972. HALT(100)
  1973. END
  1974. END
  1975. | IntermediateCode.shl:
  1976. (* left shift: *)
  1977. IF IsComplex(irInstruction.op1) THEN
  1978. (* complex left shift: *)
  1979. IF shiftAmountRegister.register = None THEN
  1980. (* complex left immediate shift: *)
  1981. IF shiftAmountImmediate = 0 THEN
  1982. Emit2(opMOV, destination[High], source[High]);
  1983. Emit2(opMOV, destination[Low], source[Low])
  1984. ELSIF (shiftAmountImmediate > 0) & (shiftAmountImmediate < 32) THEN
  1985. IF ~IsSameRegister(destination[High], source[High]) THEN temp := destination[High] ELSE temp := GetFreeRegister(IntermediateCode.UnsignedIntegerType(32)) END;
  1986. Emit2(opMOV, temp, InstructionSet.NewRegister(source[Low].register, InstructionSet.shiftLSR, None, 32 - shiftAmountImmediate));
  1987. Emit3(opORR, destination[High], temp, InstructionSet.NewRegister(source[High].register, InstructionSet.shiftLSL, None, shiftAmountImmediate));
  1988. Emit2(opMOV, destination[Low], InstructionSet.NewRegister(source[Low].register, InstructionSet.shiftLSL, None, shiftAmountImmediate))
  1989. ELSIF (shiftAmountImmediate >= 32) & (shiftAmountImmediate < 64) THEN
  1990. Emit2(opMOV, destination[High], InstructionSet.NewRegister(source[Low].register, InstructionSet.shiftLSL, None, shiftAmountImmediate - 32));
  1991. Emit2(opMOV, destination[Low], InstructionSet.NewImmediate(0))
  1992. ELSIF shiftAmountImmediate >= 64 THEN
  1993. Emit2(opMOV, destination[High], InstructionSet.NewImmediate(0));
  1994. Emit2(opMOV, destination[Low], InstructionSet.NewImmediate(0))
  1995. ELSE
  1996. HALT(100)
  1997. END
  1998. ELSE
  1999. (* complex left register shift: *)
  2000. IF ~IsSameRegister(destination[Low], source[Low]) THEN temp := destination[Low] ELSE temp := GetFreeRegister(IntermediateCode.UnsignedIntegerType(32)) END;
  2001. Emit2(opCMP, shiftAmountRegister, InstructionSet.NewImmediate(32));
  2002. (* shiftAmount < 32: *)
  2003. Emit3WithCondition(opRSB, temp, shiftAmountRegister, InstructionSet.NewImmediate(32), InstructionSet.conditionLT);
  2004. Emit2WithCondition(opMOV, temp, InstructionSet.NewRegister(source[Low].register, InstructionSet.shiftLSR, temp.register, 0), InstructionSet.conditionLT);
  2005. Emit3WithCondition(opORR, destination[High], temp, InstructionSet.NewRegister(source[High].register, InstructionSet.shiftLSL, shiftAmountRegister.register, 0), InstructionSet.conditionLT);
  2006. Emit2WithCondition(opMOV, destination[Low], InstructionSet.NewRegister(source[Low].register, InstructionSet.shiftLSL, shiftAmountRegister.register, 0), InstructionSet.conditionLT);
  2007. (* shift amount >= 32: *)
  2008. Emit3WithCondition(opSUB, temp, shiftAmountRegister, InstructionSet.NewImmediate(32), InstructionSet.conditionGE);
  2009. Emit2WithCondition(opMOV, destination[High], InstructionSet.NewRegister(source[Low].register, InstructionSet.shiftLSL, temp.register, 0), InstructionSet.conditionGE);
  2010. Emit2WithCondition(opMOV, destination[Low], InstructionSet.NewImmediate(0), InstructionSet.conditionGE)
  2011. END
  2012. ELSE
  2013. (* simple left shift: *)
  2014. IF shiftAmountRegister.register = None THEN
  2015. (* simple left immediate shift *)
  2016. IF (shiftAmountImmediate >= 0) & (shiftAmountImmediate < 32) THEN
  2017. Emit2(opMOV, destination[Low], InstructionSet.NewRegister(source[Low].register, InstructionSet.shiftLSL, None, shiftAmountImmediate)) (* note: LSL has to be in the range 0..31 *)
  2018. ELSIF shiftAmountImmediate >= 32 THEN
  2019. Emit2(opMOV, destination[Low], InstructionSet.NewImmediate(0))
  2020. ELSE
  2021. HALT(100)
  2022. END
  2023. ELSE
  2024. (* simple left register shift: *)
  2025. Emit2(opMOV, destination[Low], InstructionSet.NewRegister(source[Low].register, InstructionSet.shiftLSL, shiftAmountRegister.register, 0))
  2026. END
  2027. END
  2028. | IntermediateCode.shr:
  2029. (* right shift: *)
  2030. (* determine shift mode (depends on if source operand is signed) *)
  2031. IF irInstruction.op1.type.form = IntermediateCode.UnsignedInteger THEN
  2032. (* logical right shift: *)
  2033. shiftMode := InstructionSet.shiftLSR
  2034. ELSE
  2035. (* arithmetic right shift: *)
  2036. shiftMode := InstructionSet.shiftASR
  2037. END;
  2038. IF IsComplex(irInstruction.op1) THEN
  2039. (* complex right shift: *)
  2040. IF shiftAmountRegister.register = None THEN
  2041. (* complex right immediate shift: *)
  2042. IF shiftAmountImmediate = 0 THEN
  2043. Emit2(opMOV, destination[High], source[High]);
  2044. Emit2(opMOV, destination[Low], source[Low])
  2045. ELSIF (shiftAmountImmediate > 0) & (shiftAmountImmediate < 32) THEN
  2046. IF ~IsSameRegister(destination[High], source[High]) THEN temp := destination[High] ELSE temp := GetFreeRegister(IntermediateCode.UnsignedIntegerType(32)) END;
  2047. Emit2(opMOV, temp, InstructionSet.NewRegister(source[High].register, InstructionSet.shiftLSL, None, 32 - shiftAmountImmediate));
  2048. Emit3(opORR, destination[Low], temp, InstructionSet.NewRegister(source[Low].register, InstructionSet.shiftLSR, None, shiftAmountImmediate));
  2049. Emit2(opMOV, destination[High], InstructionSet.NewRegister(source[High].register, shiftMode, None, shiftAmountImmediate))
  2050. ELSIF shiftAmountImmediate >= 32 THEN
  2051. IF shiftAmountImmediate > 64 THEN shiftAmountImmediate := 64 END;
  2052. Emit2(opMOV, destination[Low], InstructionSet.NewRegister(source[High].register, shiftMode, None, shiftAmountImmediate - 32));
  2053. Emit2(opMOV, destination[High], InstructionSet.NewRegister(source[High].register, shiftMode, None, 32))
  2054. ELSE
  2055. HALT(100)
  2056. END
  2057. ELSE
  2058. (* complex right register shift: *)
  2059. IF ~IsSameRegister(destination[High], source[High]) THEN temp := destination[High] ELSE temp := GetFreeRegister(IntermediateCode.UnsignedIntegerType(32)) END;
  2060. Emit2(opCMP, shiftAmountRegister, InstructionSet.NewImmediate(32));
  2061. (* shiftAmount < 32: *)
  2062. Emit3WithCondition(opRSB, temp, shiftAmountRegister, InstructionSet.NewImmediate(32), InstructionSet.conditionLT);
  2063. Emit2WithCondition(opMOV, temp, InstructionSet.NewRegister(source[High].register, InstructionSet.shiftLSL, temp.register, 0), InstructionSet.conditionLT);
  2064. Emit3WithCondition(opORR, destination[Low], temp, InstructionSet.NewRegister(source[Low].register, InstructionSet.shiftLSR, shiftAmountRegister.register, 0), InstructionSet.conditionLT);
  2065. Emit2WithCondition(opMOV, destination[High], InstructionSet.NewRegister(source[High].register, shiftMode, shiftAmountRegister.register, 0), InstructionSet.conditionLT);
  2066. (* shift amount >= 32: *)
  2067. Emit3WithCondition(opSUB, temp, shiftAmountRegister, InstructionSet.NewImmediate(32), InstructionSet.conditionGE);
  2068. Emit2WithCondition(opMOV, destination[Low], InstructionSet.NewRegister(source[High].register, shiftMode, temp.register, 0), InstructionSet.conditionGE);
  2069. Emit2WithCondition(opMOV, destination[High], InstructionSet.NewRegister(source[High].register, shiftMode, shiftAmountRegister.register, 0), InstructionSet.conditionGE)
  2070. END
  2071. ELSE
  2072. (* simple right shift: *)
  2073. SignOrZeroExtendOperand(source[Low], irInstruction.op1.type);
  2074. IF shiftAmountRegister.register = None THEN
  2075. (* simple right immediate shift: *)
  2076. IF shiftAmountImmediate > 32 THEN shiftAmountImmediate := 32 END;
  2077. Emit2(opMOV, destination[Low], InstructionSet.NewRegister(source[Low].register, shiftMode, None, shiftAmountImmediate))
  2078. ELSE
  2079. (* simple right register shift: *)
  2080. Emit2(opMOV, destination[Low], InstructionSet.NewRegister(source[Low].register, shiftMode, shiftAmountRegister.register, 0))
  2081. END
  2082. END
  2083. ELSE
  2084. HALT(100)
  2085. END;
  2086. WriteBack(irInstruction.op1, Low, destination[Low]);
  2087. IF IsComplex(irInstruction.op1) THEN WriteBack(irInstruction.op1, High, destination[High]) END
  2088. END EmitShiftOrRotation;
  2089. PROCEDURE EmitAsm(CONST irInstruction: IntermediateCode.Instruction);
  2090. VAR
  2091. reader: Streams.StringReader;
  2092. procedure: SyntaxTree.Procedure;
  2093. scope: SyntaxTree.Scope;
  2094. symbol: SyntaxTree.Symbol;
  2095. assembler: Assembler.Assembler;
  2096. scanner: Scanner.AssemblerScanner;
  2097. len: LONGINT;
  2098. BEGIN
  2099. len := Strings.Length(irInstruction.op1.string^);
  2100. NEW(reader, len);
  2101. reader.Set(irInstruction.op1.string^);
  2102. (* determine scope of the section *)
  2103. symbol := in.symbol;
  2104. IF symbol = NIL THEN
  2105. scope := NIL
  2106. ELSE
  2107. procedure := symbol(SyntaxTree.Procedure);
  2108. scope := procedure.procedureScope
  2109. END;
  2110. NEW(assembler, diagnostics);
  2111. scanner := Scanner.NewAssemblerScanner(module.moduleName(*module.module.sourceName*), reader, LONGINT(irInstruction.op1.intValue) (* ? *), diagnostics);
  2112. assembler.InlineAssemble(scanner, in, scope, module);
  2113. error := error OR assembler.error
  2114. END EmitAsm;
  2115. PROCEDURE EmitSpecial(VAR instruction: IntermediateCode.Instruction);
  2116. VAR
  2117. psrNumber, code, a, b, c, d: LONGINT;
  2118. register, register2, register3, register4, temp, cpOperand, cpRegister1, cpRegister2, opCode1Operand, opCode2Operand: Operand;
  2119. BEGIN
  2120. CASE instruction.subtype OF
  2121. | GetSP: Emit2(opMOV, opRES, opSP)
  2122. | SetSP: Emit2(opMOV, opSP, RegisterOrImmediateFromIrOperand(instruction.op1, Low, emptyOperand))
  2123. | GetFP: Emit2(opMOV, opRES, opFP)
  2124. | SetFP: Emit2(opMOV, opFP, RegisterOrImmediateFromIrOperand(instruction.op1, Low, emptyOperand))
  2125. | GetLNK: Emit2(opMOV, opRES, opLR)
  2126. | SetLNK: Emit2(opMOV, opLR, RegisterOrImmediateFromIrOperand(instruction.op1, Low, emptyOperand))
  2127. | GetPC: Emit2(opMOV, opRES, opPC)
  2128. | SetPC: Emit2(opMOV, opPC, RegisterOrImmediateFromIrOperand(instruction.op1, Low, emptyOperand))
  2129. | LDPSR, STPSR:
  2130. ASSERT(instruction.op1.type.form IN IntermediateCode.Integer);
  2131. IF instruction.op1.mode # IntermediateCode.ModeImmediate THEN
  2132. Error(instruction.textPosition,"first operand must be immediate")
  2133. ELSIF (instruction.op1.intValue < 0) OR (instruction.op1.intValue > 1) THEN
  2134. Error(instruction.textPosition,"first operand must be 0 or 1")
  2135. ELSE
  2136. IF instruction.op1.intValue = 0 THEN
  2137. psrNumber := InstructionSet.CPSR
  2138. ELSE
  2139. psrNumber := InstructionSet.SPSR
  2140. END;
  2141. register := RegisterFromIrOperand(instruction.op2, Low, emptyOperand);
  2142. IF instruction.subtype = LDPSR THEN
  2143. Emit2(opMSR, InstructionSet.NewRegisterWithFields(psrNumber, {InstructionSet.fieldF, InstructionSet.fieldC}), register)
  2144. ELSE
  2145. temp := GetFreeRegister(IntermediateCode.UnsignedIntegerType(32));
  2146. Emit2(opMRS, temp, InstructionSet.NewRegister(psrNumber, None, None, 0));
  2147. Emit2(opSTR, temp, InstructionSet.NewImmediateOffsetMemory(register.register, 0, {InstructionSet.Increment}))
  2148. END
  2149. END
  2150. | LDCPR, STCPR:
  2151. IF instruction.op1.mode # IntermediateCode.ModeImmediate THEN
  2152. Error(instruction.textPosition,"first operand must be immediate")
  2153. ELSIF (instruction.op2.mode # IntermediateCode.ModeImmediate) THEN
  2154. Error(instruction.textPosition,"second operand must be immediate")
  2155. ELSIF (instruction.op2.intValue < 0) OR (instruction.op2.intValue > 15) THEN
  2156. Error(instruction.textPosition,"second operand must be between 0 or 15")
  2157. ELSE
  2158. code := LONGINT(instruction.op1.intValue); (* code = a00bcdH *)
  2159. a := (code DIV 100000H) MOD 10H; (* opcode1 * 2 *)
  2160. b := (code DIV 100H) MOD 10H; (* coprocessor number *)
  2161. c := (code DIV 10H) MOD 10H; (* opcode2 * 2 *)
  2162. d := code MOD 10H; (* coprocessor register2 number *)
  2163. InstructionSet.InitCoprocessor(cpOperand, InstructionSet.CP0 + b);
  2164. InstructionSet.InitOpcode(opCode1Operand, a DIV 2);
  2165. register := RegisterFromIrOperand(instruction.op3, Low, emptyOperand);
  2166. InstructionSet.InitRegister(cpRegister1, InstructionSet.CR0 + LONGINT(instruction.op2.intValue), None, None, 0);
  2167. InstructionSet.InitRegister(cpRegister2, InstructionSet.CR0 + d, None, None, 0);
  2168. InstructionSet.InitOpcode(opCode2Operand, c DIV 2);
  2169. IF instruction.subtype = LDCPR THEN
  2170. Emit6(opMCR, cpOperand, opCode1Operand, register, cpRegister1, cpRegister2, opCode2Operand)
  2171. ELSE
  2172. temp := GetFreeRegister(IntermediateCode.UnsignedIntegerType(32));
  2173. Emit6(opMRC, cpOperand, opCode1Operand, temp, cpRegister1, cpRegister2, opCode2Operand);
  2174. Emit2(opSTR, temp, InstructionSet.NewImmediateOffsetMemory(register.register, 0, {InstructionSet.Increment}))
  2175. END
  2176. END
  2177. | FLUSH:
  2178. IF instruction.op1.mode # IntermediateCode.ModeImmediate THEN
  2179. Error(instruction.textPosition,"first operand must be immediate")
  2180. ELSIF (instruction.op1.intValue < 0) OR (instruction.op2.intValue > 0FFH) THEN
  2181. Error(instruction.textPosition,"first operand must be between 0 and 255")
  2182. ELSE
  2183. code := LONGINT(instruction.op1.intValue); (* code = aaa1bbbbB *)
  2184. a := (code DIV 20H) MOD 8; (* coprocessor opcode 2 *)
  2185. b := (code MOD 10H); (* coprocessor register2 number *)
  2186. (* examples:
  2187. 9AH = 10011000B -> MCR p15, 0, R0, c7, c10, 4
  2188. 17H = 00010111B -> MCR p15, 0, R0, c7, c7, 0
  2189. *)
  2190. InstructionSet.InitCoprocessor(cpOperand, InstructionSet.CP15);
  2191. InstructionSet.InitOpcode(opCode1Operand, 0);
  2192. InstructionSet.InitRegister(register, InstructionSet.R0, None, None, 0);
  2193. InstructionSet.InitRegister(cpRegister1, InstructionSet.CR7, None, None, 0);
  2194. InstructionSet.InitRegister(cpRegister2, InstructionSet.CR0 + b, None, None, 0);
  2195. InstructionSet.InitOpcode(opCode2Operand, a);
  2196. Emit6(opMCR, cpOperand, opCode1Operand, register, cpRegister1, cpRegister2, opCode2Operand);
  2197. Emit2(opMOV, register, register); (* NOP (register = R0) *)
  2198. Emit2(opMOV, register, register); (* NOP *)
  2199. Emit2(opMOV, register, register); (* NOP *)
  2200. Emit2(opMOV, register, register) (* NOP *)
  2201. END
  2202. | NULL:
  2203. register := RegisterFromIrOperand(instruction.op1, Low, emptyOperand);
  2204. Emit3(opBIC, register, register, InstructionSet.NewImmediate(LONGINT(80000000H)));
  2205. Emit2(opCMP, register, InstructionSet.NewImmediate(0));
  2206. Emit2WithCondition(opMOV, opRES, InstructionSet.NewImmediate(1), InstructionSet.conditionEQ);
  2207. Emit2WithCondition(opMOV, opRES, InstructionSet.NewImmediate(0), InstructionSet.conditionNE);
  2208. | XOR:
  2209. register := RegisterFromIrOperand(instruction.op1, Low, emptyOperand);
  2210. register2 := RegisterFromIrOperand(instruction.op2, Low, emptyOperand);
  2211. (*
  2212. register3 := RegisterFromIrOperand(instruction.op3, Low, emptyOperand);
  2213. *)
  2214. Emit3(opEOR, opRES, register, register2);
  2215. | MULD:
  2216. register := RegisterFromIrOperand(instruction.op1, Low, emptyOperand); (* note that 'register' contains an address *)
  2217. register2 := RegisterFromIrOperand(instruction.op2, Low, emptyOperand);
  2218. register3 := RegisterFromIrOperand(instruction.op3, Low, emptyOperand);
  2219. Emit4(opUMULL, opRES, opRESHI, register2, register3);
  2220. Emit2(opSTR, opRES, InstructionSet.NewImmediateOffsetMemory(register.register, 0, {InstructionSet.Increment})); (* JCH: 15.05.2012 *)
  2221. Emit2(opSTR, opRESHI, InstructionSet.NewImmediateOffsetMemory(register.register, 4, {InstructionSet.Increment}))
  2222. | ADDC:
  2223. register := RegisterFromIrOperand(instruction.op1, Low, emptyOperand);
  2224. register2 := RegisterFromIrOperand(instruction.op2, Low, emptyOperand);
  2225. Emit3(opADC, opRES, register, register2)
  2226. | PACK:
  2227. (* PACK(x, y):
  2228. add y to the binary exponent of y. PACK(x, y) is equivalent to x := x * 2^y. *)
  2229. register := RegisterFromIrOperand(instruction.op1, Low, emptyOperand); (* register = address of x *)
  2230. register2 := RegisterFromIrOperand(instruction.op2, Low, emptyOperand); (* register2 = value of y *)
  2231. register3 := GetFreeRegister(IntermediateCode.UnsignedIntegerType(32)); (* a temporary INTEGER (!) register that is used to store a float *)
  2232. Emit2(opLDR, register3, InstructionSet.NewImmediateOffsetMemory(register.register, 0, {InstructionSet.Increment})); (* register3 = value of x *)
  2233. Emit3(opADD, register3, register3, InstructionSet.NewRegister(register2.register, InstructionSet.shiftLSL, None, 23)); (* increase the (biased) exponent of x by y*)
  2234. Emit2(opSTR, register3, InstructionSet.NewImmediateOffsetMemory(register.register, 0, {InstructionSet.Increment})) (* store new value of x *)
  2235. | UNPK:
  2236. (* UNPK(x, y):
  2237. remove the binary exponent on x and put it into y. UNPK is the reverse operation of PACK. The resulting x is normalized, i.e. 1.0 <= x < 2.0.
  2238. *)
  2239. register := RegisterFromIrOperand(instruction.op1, Low, emptyOperand); (* register = address of x *)
  2240. register2 := RegisterFromIrOperand(instruction.op2, Low, emptyOperand); (* register2 = address of y *)
  2241. register3 := GetFreeRegister(IntermediateCode.UnsignedIntegerType(32)); (* a temporary INTEGER (!) register that is used to store a float *)
  2242. Emit2(opLDR, register3, InstructionSet.NewImmediateOffsetMemory(register.register, 0, {InstructionSet.Increment})); (* register3 = value of x *)
  2243. register4 := GetFreeRegister(IntermediateCode.UnsignedIntegerType(32));
  2244. Emit2(opMOV, register4, InstructionSet.NewRegister(register3.register, InstructionSet.shiftLSR, None, 23)); (* register4 = biased exponent (and sign) of x *)
  2245. Emit3(opSUB, register4, register4, InstructionSet.NewImmediate(127)); (* register4 = exponent of x (biased exponent - 127) *)
  2246. Emit2(opSTR, register4, InstructionSet.NewImmediateOffsetMemory(register2.register, 0, {InstructionSet.Increment})); (* store exponent of x as value for y *)
  2247. Emit3(opSUB, register3, register3, InstructionSet.NewRegister(register4.register, InstructionSet.shiftLSL, None, 23)); (* reduce the biased exponent of x by the value of y *)
  2248. Emit2(opSTR, register3, InstructionSet.NewImmediateOffsetMemory(register.register, 0, {InstructionSet.Increment})) (* store new value of x *)
  2249. ELSE
  2250. HALT(100)
  2251. END
  2252. END EmitSpecial;
  2253. PROCEDURE EmitBr(VAR irInstruction: IntermediateCode.Instruction);
  2254. VAR
  2255. branchDistance: LONGINT;
  2256. isSwapped: BOOLEAN;
  2257. left, right: ARRAY 2 OF Operand;
  2258. temp: Operand;
  2259. irLeft, irRight: IntermediateCode.Operand;
  2260. fixup,failFixup: BinaryCode.Fixup;
  2261. fixupPatternList: ObjectFile.FixupPatterns;
  2262. identifier: ObjectFile.Identifier;
  2263. hiHit, hiFail, lowHit, lowFail: LONGINT;
  2264. PROCEDURE JmpDest(branchConditionCode: LONGINT);
  2265. BEGIN
  2266. IF (irInstruction.op1.mode = IntermediateCode.ModeImmediate) & (irInstruction.op1.symbol.name = in.name) & (irInstruction.op1.offset = 0) THEN
  2267. (* branch within same section at a certain IR offset *)
  2268. (* optimization: abort if branch is to the next instruction *)
  2269. IF irInstruction.op1.symbolOffset = inPC + 1 THEN
  2270. IF dump # NIL THEN dump.String("branch to next instruction ignored"); dump.Ln END;
  2271. RETURN
  2272. END;
  2273. IF irInstruction.op1.symbolOffset <= inPC THEN
  2274. (* backward branch: calculate the branch distance *)
  2275. branchDistance := in.instructions[irInstruction.op1.symbolOffset].pc - out.pc - 8;
  2276. ASSERT((-33554432 <= branchDistance) & (branchDistance <= 0) & ((ABS(branchDistance) MOD 4) = 0), 200);
  2277. ELSE
  2278. (* forward branch: the distance is not yet known, use some placeholder and add a relative fixup *)
  2279. branchDistance := -4;
  2280. (* TODO: what about a branch to the next instruction? this would require the fixup meachnism to patch a negative value! (-> -4) *)
  2281. NEW(fixupPatternList, 1);
  2282. fixupPatternList[0].offset := 0;
  2283. fixupPatternList[0].bits := 24;
  2284. identifier.name := in.name;
  2285. identifier.fingerprint := in.fingerprint;
  2286. fixup := BinaryCode.NewFixup(BinaryCode.Relative, out.pc, identifier, irInstruction.op1.symbolOffset, -8, -2, fixupPatternList);
  2287. out.fixupList.AddFixup(fixup)
  2288. END;
  2289. Emit1WithCondition(opB, InstructionSet.NewImmediate(branchDistance), branchConditionCode)
  2290. ELSE
  2291. (* any other type of branch -> do register branch *)
  2292. Emit1WithCondition(opBX, RegisterFromIrOperand(irInstruction.op1, Low, emptyOperand), branchConditionCode)
  2293. END;
  2294. END JmpDest;
  2295. PROCEDURE Cmp(CONST left, right: InstructionSet.Operand; float: BOOLEAN);
  2296. BEGIN
  2297. IF float THEN
  2298. IF ~backend.useFPU OR IsComplex(irLeft) (* 64 bit *) THEN
  2299. (* floating point comparisons without VFP unit *)
  2300. temp := GetFreeRegister(IntermediateCode.UnsignedIntegerType(32));
  2301. Emit3WithFlags(opAND, temp, left, right, {InstructionSet.flagS});
  2302. Emit2(opCMP, temp, InstructionSet.NewImmediate(0));
  2303. Emit1WithCondition(opB, InstructionSet.NewImmediate(4), InstructionSet.conditionLT); (* skip two instructions *)
  2304. Emit2(opCMP, left, right);
  2305. Emit1(opB, InstructionSet.NewImmediate(0)); (* skip one instructions *)
  2306. Emit2(opCMP, right, left);
  2307. ELSE
  2308. Emit2(opFCMPS, left, right);
  2309. Emit0(opFMSTAT); (* transfer the VFP flags to the standard ARM flags *)
  2310. END
  2311. ELSE
  2312. Emit2(opCMP, left, right);
  2313. END;
  2314. END Cmp;
  2315. BEGIN
  2316. hiFail := None;
  2317. hiHit := None;
  2318. IF irInstruction.opcode = IntermediateCode.br THEN
  2319. (* unconditional branch: *)
  2320. lowHit := InstructionSet.conditionAL
  2321. ELSE
  2322. (* conditional branch: *)
  2323. irLeft := irInstruction.op2; irRight := irInstruction.op3;
  2324. ASSERT((irLeft.type.form = irRight.type.form) & (irLeft.type.sizeInBits = irRight.type.sizeInBits));
  2325. IF IsInteger(irLeft) THEN
  2326. IF IsComplex(irLeft) THEN
  2327. CASE irInstruction.opcode OF
  2328. | IntermediateCode.breq, IntermediateCode.brne: (* left = right, left # right *)
  2329. lowHit := InstructionSet.conditionEQ;
  2330. left[High] := RegisterFromIrOperand(irLeft, High, emptyOperand);
  2331. right[High] := RegisterOrImmediateFromIrOperand(irRight, High, emptyOperand);
  2332. Emit2(opCMP, left[High], right[High]);
  2333. left[Low] := RegisterFromIrOperand(irLeft, Low, left[High]);
  2334. right[Low] := RegisterOrImmediateFromIrOperand(irRight, Low, right[High]);
  2335. Emit2WithCondition(opCMP, left[Low], right[Low], lowHit);
  2336. IF irInstruction.opcode = IntermediateCode.brne THEN lowHit := InstructionSet.conditionNE END;
  2337. | IntermediateCode.brlt, IntermediateCode.brge: (* left < right, left >= right *)
  2338. IF irInstruction.opcode = IntermediateCode.brlt THEN lowHit := InstructionSet.conditionLT ELSE lowHit := InstructionSet.conditionGE END;
  2339. ASSERT(irLeft.type.form = IntermediateCode.SignedInteger);
  2340. left[Low] := RegisterFromIrOperand(irLeft, Low, emptyOperand);
  2341. right[Low] := RegisterOrImmediateFromIrOperand(irRight, Low, emptyOperand);
  2342. temp := GetFreeRegister(IntermediateCode.UnsignedIntegerType(32));
  2343. Emit3WithFlags(opSUB, temp, left[Low], right[Low], {InstructionSet.flagS});
  2344. left[High] := RegisterFromIrOperand(irLeft, High, left[Low]);
  2345. right[High] := RegisterOrImmediateFromIrOperand(irRight, High, right[Low]);
  2346. Emit3WithFlags(opSBC, temp, left[High], right[High], {InstructionSet.flagS}) (* the high part of the subtraction determines the sign *)
  2347. ELSE
  2348. HALT(100)
  2349. END
  2350. ELSE
  2351. ASSERT((irLeft.type.form IN IntermediateCode.Integer) & (irLeft.type.sizeInBits <= 32));
  2352. (* swap operands if beneficial *)
  2353. IF ~IrOperandIsDirectlyEncodable(irRight, Low) & IrOperandIsDirectlyEncodable(irLeft, Low) THEN
  2354. isSwapped := TRUE;
  2355. SwapIrOperands(irLeft, irRight)
  2356. END;
  2357. left[Low] := RegisterFromIrOperand(irLeft, Low, emptyOperand);
  2358. right[Low] := RegisterOrImmediateFromIrOperand(irRight, Low, emptyOperand);
  2359. SignOrZeroExtendOperand(left[Low], irLeft.type);
  2360. SignOrZeroExtendOperand(right[Low], irRight.type);
  2361. Cmp(left[Low], right[Low], FALSE);
  2362. (* determine condition code for the branch (take into consideration that operands could have been swapped) *)
  2363. CASE irInstruction.opcode OF
  2364. | IntermediateCode.breq: (* left = right *) lowHit := InstructionSet.conditionEQ
  2365. | IntermediateCode.brne: (* left # right *) lowHit := InstructionSet.conditionNE
  2366. | IntermediateCode.brlt: (* left < right *)
  2367. IF irInstruction.op2.type.form = IntermediateCode.UnsignedInteger THEN
  2368. IF isSwapped THEN lowHit := InstructionSet.conditionHI ELSE lowHit := InstructionSet.conditionLO END
  2369. ELSE
  2370. IF isSwapped THEN lowHit := InstructionSet.conditionGT ELSE lowHit := InstructionSet.conditionLT END
  2371. END
  2372. | IntermediateCode.brge: (* left >= right *)
  2373. IF irInstruction.op2.type.form = IntermediateCode.UnsignedInteger THEN
  2374. IF isSwapped THEN lowHit := InstructionSet.conditionLS ELSE lowHit := InstructionSet.conditionHS END
  2375. ELSE
  2376. IF isSwapped THEN lowHit := InstructionSet.conditionLE ELSE lowHit := InstructionSet.conditionGE END
  2377. END
  2378. ELSE HALT(100)
  2379. END
  2380. END
  2381. ELSIF IsSinglePrecisionFloat(irLeft) THEN
  2382. left[Low] := RegisterFromIrOperand(irLeft, Low, emptyOperand);
  2383. right[Low] := RegisterFromIrOperand(irRight, Low, emptyOperand);
  2384. Cmp(left[Low], right[Low], TRUE);
  2385. CASE irInstruction.opcode OF
  2386. | IntermediateCode.breq: (* left = right *) lowHit := InstructionSet.conditionEQ
  2387. | IntermediateCode.brne: (* left # right *) lowHit := InstructionSet.conditionNE
  2388. | IntermediateCode.brlt: (* left < right *) lowHit := InstructionSet.conditionLT
  2389. | IntermediateCode.brge: (* left >= right *) lowHit := InstructionSet.conditionGE
  2390. ELSE HALT(100)
  2391. END
  2392. ELSIF IsDoublePrecisionFloat(irLeft) THEN
  2393. CASE irInstruction.opcode OF
  2394. IntermediateCode.breq:
  2395. hiHit := None; hiFail := InstructionSet.conditionNE; lowHit := InstructionSet.conditionEQ
  2396. |IntermediateCode.brne:
  2397. hiHit := InstructionSet.conditionNE; hiFail := None; lowHit := InstructionSet.conditionNE
  2398. |IntermediateCode.brge:
  2399. IF isSwapped THEN
  2400. hiHit := InstructionSet.conditionLT; hiFail := InstructionSet.conditionGT; lowHit := InstructionSet.conditionLS
  2401. ELSE
  2402. hiHit := InstructionSet.conditionGT; hiFail := InstructionSet.conditionLT; lowHit := InstructionSet.conditionHS
  2403. END;
  2404. |IntermediateCode.brlt:
  2405. IF isSwapped THEN
  2406. hiHit := InstructionSet.conditionGT; hiFail := InstructionSet.conditionLT; lowHit := InstructionSet.conditionHI
  2407. ELSE
  2408. hiHit := InstructionSet.conditionLT; hiFail := InstructionSet.conditionGT; lowHit := InstructionSet.conditionLO
  2409. END;
  2410. END;
  2411. (*
  2412. compare hi part (as float)
  2413. if hiHit then br dest
  2414. elsif hiFail then br fail
  2415. else compare low part (as unsigned int)
  2416. if lowHit then br dest
  2417. end
  2418. end,
  2419. fail:
  2420. *)
  2421. (* hi part *)
  2422. left[High] := RegisterFromIrOperand(irLeft, High, emptyOperand);
  2423. right[High] := RegisterOrImmediateFromIrOperand(irRight, High, emptyOperand);
  2424. Cmp(left[High], right[High], TRUE);
  2425. IF hiHit # None THEN
  2426. JmpDest(hiHit)
  2427. END;
  2428. IF hiFail # None THEN
  2429. NEW(fixupPatternList, 1);
  2430. fixupPatternList[0].offset := 0;
  2431. fixupPatternList[0].bits := 24;
  2432. identifier.name := in.name;
  2433. identifier.fingerprint := in.fingerprint;
  2434. failFixup := BinaryCode.NewFixup(BinaryCode.Relative, out.pc, identifier, irInstruction.op1.symbolOffset, -8, -2, fixupPatternList);
  2435. out.fixupList.AddFixup(failFixup);
  2436. Emit1WithCondition(opB, InstructionSet.NewImmediate(branchDistance), hiFail)
  2437. END;
  2438. (* low part *)
  2439. left[Low] := RegisterFromIrOperand(irLeft, Low, emptyOperand);
  2440. right[Low] := RegisterFromIrOperand(irRight, Low, emptyOperand);
  2441. Cmp(left[Low], right[Low], FALSE);
  2442. ELSE
  2443. HALT(200)
  2444. END
  2445. END;
  2446. JmpDest(lowHit);
  2447. IF failFixup # NIL THEN
  2448. failFixup.SetSymbol(in.name, in.fingerprint, 0, out.pc+failFixup.displacement (* displacement offset computed during operand emission, typically -1 *) );
  2449. failFixup.resolved := in;
  2450. END;
  2451. END EmitBr;
  2452. (* TODO: floats *)
  2453. PROCEDURE EmitConv(VAR irInstruction: IntermediateCode.Instruction);
  2454. VAR
  2455. irDestination, irSource: IntermediateCode.Operand;
  2456. destination, source: ARRAY 2 OF Operand;
  2457. temp: Operand;
  2458. partType: IntermediateCode.Type;
  2459. BEGIN
  2460. irDestination := irInstruction.op1; irSource := irInstruction.op2;
  2461. (* prepare operands *)
  2462. destination[Low] := AcquireDestinationRegister(irDestination, Low, emptyOperand); (* TODO: find more optimal register allocation *)
  2463. source[Low] := RegisterOrImmediateFromIrOperand(irSource, Low, destination[Low]);
  2464. IF IsComplex(irDestination) THEN destination[High]:= AcquireDestinationRegister(irDestination, High, emptyOperand) END;
  2465. IF IsComplex(irSource) THEN source[High] := RegisterOrImmediateFromIrOperand(irSource, High, destination[High]) END; (* note that the corresponding destination register is used as hint *)
  2466. IF IsInteger(irDestination) THEN
  2467. (* to integer: *)
  2468. IF IsComplex(irDestination) THEN
  2469. (* to complex integer: *)
  2470. IF IsInteger(irSource) THEN
  2471. (* integer to complex integer: *)
  2472. IF IsComplex(irSource) THEN
  2473. (* complex integer to complex integer: *)
  2474. MovIfDifferent(destination[Low], source[Low]);
  2475. MovIfDifferent(destination[High], source[High]);
  2476. ELSE
  2477. (* non-complex integer to complex integer: *)
  2478. SignOrZeroExtendOperand(source[Low], irSource.type);
  2479. MovIfDifferent(destination[Low], source[Low]);
  2480. IF irDestination.type.form = IntermediateCode.UnsignedInteger THEN
  2481. Emit2(opMOV, destination[High], InstructionSet.NewImmediate(0));
  2482. ELSE
  2483. (* for signed values the high part is set to 0...0 or 1...1, depending on the sign of the low part *)
  2484. Emit2(opMOV, destination[High], InstructionSet.NewRegister(source[Low].register, InstructionSet.shiftASR, None, 32))
  2485. END
  2486. END
  2487. ELSIF IsSinglePrecisionFloat(irSource) THEN
  2488. ASSERT(backend.useFPU);
  2489. (* single precision float to complex integer: *)
  2490. temp := GetFreeRegister(IntermediateCode.FloatType(32));
  2491. IF irDestination.type.form = IntermediateCode.UnsignedInteger THEN
  2492. (* single precision float to non-complex unsigned integer: *)
  2493. Emit2(opFTOUIS, temp, source[Low]);
  2494. ELSE
  2495. (* single precision float to non-complex signed integer: *)
  2496. Emit2(opFTOSIS, temp, source[Low]);
  2497. END;
  2498. Emit2(opFMRS, destination[Low], temp);
  2499. IF irDestination.type.form = IntermediateCode.UnsignedInteger THEN
  2500. Emit2(opMOV, destination[High], InstructionSet.NewImmediate(0));
  2501. ELSE
  2502. (* for signed values the high part is set to 0...0 or 1...1, depending on the sign of the low part *)
  2503. Emit2(opMOV, destination[High], InstructionSet.NewRegister(destination[Low].register, InstructionSet.shiftASR, None, 32))
  2504. END
  2505. ELSE
  2506. (* anything else to complex-integer: *)
  2507. HALT(200)
  2508. END
  2509. ELSE
  2510. (* to non-complex integer: *)
  2511. IF IsInteger(irSource) THEN
  2512. (* integer to non-complex integer: ignore high part of source *)
  2513. GetPartType(irSource.type, Low, partType);
  2514. SignOrZeroExtendOperand(source[Low], partType);
  2515. MovIfDifferent(destination[Low], source[Low])
  2516. ELSIF IsSinglePrecisionFloat(irSource) THEN
  2517. ASSERT(backend.useFPU);
  2518. (* single precision float to non-complex integer: *)
  2519. temp := GetFreeRegister(IntermediateCode.FloatType(32));
  2520. IF irDestination.type.form = IntermediateCode.UnsignedInteger THEN
  2521. (* single precision float to non-complex unsigned integer: *)
  2522. Emit2(opFTOUIS, temp, source[Low]);
  2523. ELSE
  2524. (* single precision float to non-complex signed integer: *)
  2525. Emit2(opFTOSIS, temp, source[Low]);
  2526. END;
  2527. Emit2(opFMRS, destination[Low], temp)
  2528. ELSE
  2529. (* anything to non-complex integer: *)
  2530. HALT(200)
  2531. END
  2532. END
  2533. ELSIF IsSinglePrecisionFloat(irDestination) THEN
  2534. (* to single precision float: *)
  2535. IF IsInteger(irSource) THEN
  2536. (* integer to single precision float: ignore high part of source *)
  2537. temp := GetFreeRegister(IntermediateCode.FloatType(32));
  2538. Emit2(opFMSR, temp, source[Low]);
  2539. IF irSource.type.form = IntermediateCode.UnsignedInteger THEN
  2540. (* non-complex unsigned integer to single precision float: *)
  2541. Emit2(opFUITOS, destination[Low], temp)
  2542. ELSE
  2543. (* non-complex signed integer to single precision float: *)
  2544. Emit2(opFSITOS, destination[Low], temp)
  2545. END
  2546. ELSIF IsSinglePrecisionFloat(irSource) THEN
  2547. (* single precision float to single precision float: *)
  2548. MovIfDifferent(destination[Low], source[Low])
  2549. ELSE
  2550. (* anything else to single precision float: *)
  2551. HALT(200)
  2552. END
  2553. ELSE
  2554. (* to anything else: *)
  2555. HALT(200)
  2556. END;
  2557. WriteBack(irDestination, Low, destination[Low]);
  2558. IF IsComplex(irDestination) THEN WriteBack(irInstruction.op1, High, destination[High]) END
  2559. END EmitConv;
  2560. (** get the register that is dedicated to store a return value of a function **)
  2561. PROCEDURE ResultRegister(part: LONGINT; type: IntermediateCode.Type): InstructionSet.Operand;
  2562. VAR
  2563. result: Operand;
  2564. BEGIN
  2565. IF (type.form IN IntermediateCode.Integer) OR ~(backend.useFPU) THEN
  2566. IF part = Low THEN result := opRES
  2567. ELSIF part = High THEN result := opRESHI
  2568. ELSE HALT(200)
  2569. END
  2570. ELSIF type.form = IntermediateCode.Float THEN
  2571. ASSERT(type.sizeInBits = 32, 200);
  2572. result := opRESFS
  2573. END;
  2574. RETURN result
  2575. END ResultRegister;
  2576. PROCEDURE EmitReturn(VAR irInstruction: IntermediateCode.Instruction; part: LONGINT);
  2577. VAR
  2578. source: Operand;
  2579. BEGIN
  2580. source := RegisterOrImmediateFromIrOperand(irInstruction.op1, part, ResultRegister(part, irInstruction.op1.type)); (* note: the result register is given as a hint *)
  2581. MovIfDifferent(ResultRegister(part, irInstruction.op1.type), source)
  2582. END EmitReturn;
  2583. PROCEDURE EmitResult(VAR irInstruction: IntermediateCode.Instruction; part: LONGINT);
  2584. VAR
  2585. destinationRegister: Operand;
  2586. BEGIN
  2587. destinationRegister := AcquireDestinationRegister(irInstruction.op1, part, emptyOperand);
  2588. MovIfDifferent(destinationRegister, ResultRegister(part, irInstruction.op1.type));
  2589. WriteBack(irInstruction.op1, part, destinationRegister)
  2590. END EmitResult;
  2591. PROCEDURE EmitTrap(CONST irInstruction: IntermediateCode.Instruction);
  2592. BEGIN
  2593. ASSERT(irInstruction.op1.mode = IntermediateCode.ModeNumber);
  2594. Emit1(opSWI, InstructionSet.NewImmediate(LONGINT(irInstruction.op1.intValue))) (* software interrupt *)
  2595. END EmitTrap;
  2596. PROCEDURE EmitCas(VAR irInstruction: IntermediateCode.Instruction);
  2597. VAR
  2598. addressReg, addressBaseReg, comparandReg, comparandBaseReg, comparatorReg, comparatorBaseReg, tempReg: Operand
  2599. BEGIN
  2600. addressReg := GetFreeRegister(IntermediateCode.UnsignedIntegerType(32));
  2601. addressBaseReg := RegisterFromIrOperand(irInstruction.op1, Low, addressReg);
  2602. MovIfDifferent(addressReg, addressBaseReg);
  2603. IF IntermediateCode.OperandEquals (irInstruction.op2, irInstruction.op3) THEN
  2604. Emit2(opLDR, opRES, InstructionSet.NewImmediateOffsetMemory(addressReg.register, 0, {InstructionSet.Increment}));
  2605. ELSE
  2606. comparandReg := GetFreeRegister(IntermediateCode.UnsignedIntegerType(32));
  2607. comparandBaseReg := RegisterFromIrOperand(irInstruction.op2, Low, comparandReg);
  2608. MovIfDifferent(comparandReg, comparandBaseReg);
  2609. comparatorReg := GetFreeRegister(IntermediateCode.UnsignedIntegerType(32));
  2610. comparatorBaseReg := RegisterFromIrOperand(irInstruction.op3, Low, comparatorReg);
  2611. MovIfDifferent(comparatorReg, comparatorBaseReg);
  2612. Emit2(opLDREX, opRES, addressReg);
  2613. Emit2(opCMP, opRES, comparandReg);
  2614. tempReg := GetFreeRegister(IntermediateCode.UnsignedIntegerType(32));
  2615. Emit3WithCondition(opSTREX, tempReg, comparatorReg, addressReg, InstructionSet.conditionEQ);
  2616. Emit2WithCondition(opCMP, tempReg, InstructionSet.NewImmediate(1), InstructionSet.conditionEQ);
  2617. Emit1WithCondition(opB, InstructionSet.NewImmediate (-24), InstructionSet.conditionEQ);
  2618. END;
  2619. END EmitCas;
  2620. (* possible optimization: use a combination of LDR and LDRB (would be 4x faster on average) *)
  2621. PROCEDURE EmitCopy(VAR irInstruction: IntermediateCode.Instruction);
  2622. VAR
  2623. targetBaseReg, sourceBaseReg, length, lastSourceAddress, currentTargetReg, currentSourceReg, tempReg: Operand;
  2624. BEGIN
  2625. ASSERT((irInstruction.op1.type.form = IntermediateCode.UnsignedInteger) & (irInstruction.op1.type.sizeInBits = 32));
  2626. ASSERT((irInstruction.op2.type.form = IntermediateCode.UnsignedInteger) & (irInstruction.op2.type.sizeInBits = 32));
  2627. ASSERT((irInstruction.op3.type.form = IntermediateCode.UnsignedInteger) & (irInstruction.op3.type.sizeInBits = 32));
  2628. currentTargetReg := GetFreeRegister(IntermediateCode.UnsignedIntegerType(32));
  2629. currentSourceReg := GetFreeRegister(IntermediateCode.UnsignedIntegerType(32));
  2630. (* note that the registers that store the current addresses are used as hints: *)
  2631. targetBaseReg := RegisterFromIrOperand(irInstruction.op1, Low, currentTargetReg);
  2632. sourceBaseReg := RegisterFromIrOperand(irInstruction.op2, Low, currentSourceReg);
  2633. MovIfDifferent(currentTargetReg, targetBaseReg);
  2634. MovIfDifferent(currentSourceReg, sourceBaseReg);
  2635. lastSourceAddress := GetFreeRegister(IntermediateCode.UnsignedIntegerType(32));
  2636. length := RegisterOrImmediateFromIrOperand(irInstruction.op3, Low, lastSourceAddress); (* note that the last source address register is used as hint*)
  2637. Emit3(opADD, lastSourceAddress, sourceBaseReg, length);
  2638. tempReg := GetFreeRegister(IntermediateCode.UnsignedIntegerType(32));
  2639. Emit2WithFlags(opLDR, tempReg, InstructionSet.NewImmediateOffsetMemory(currentSourceReg.register, 1, {InstructionSet.Increment, InstructionSet.PostIndexed}), {InstructionSet.flagB});
  2640. Emit2WithFlags(opSTR, tempReg, InstructionSet.NewImmediateOffsetMemory(currentTargetReg.register, 1, {InstructionSet.Increment, InstructionSet.PostIndexed}), {InstructionSet.flagB});
  2641. Emit2(opCMP, currentSourceReg, lastSourceAddress);
  2642. Emit1WithCondition(opB, InstructionSet.NewImmediate(-20), InstructionSet.conditionLT)
  2643. END EmitCopy;
  2644. PROCEDURE EmitFill(CONST irInstruction: IntermediateCode.Instruction; down: BOOLEAN);
  2645. BEGIN
  2646. HALT(200) (* note that this instruction is not used at the moment *)
  2647. END EmitFill;
  2648. (* PREPARATION OF OPERATIONS *)
  2649. (** swap a pair of IR operands **)
  2650. PROCEDURE SwapIrOperands(VAR left, right: IntermediateCode.Operand);
  2651. VAR
  2652. temp: IntermediateCode.Operand;
  2653. BEGIN
  2654. temp := left;
  2655. left := right;
  2656. right := temp
  2657. END SwapIrOperands;
  2658. PROCEDURE PrepareSingleSourceOp(VAR irInstruction: IntermediateCode.Instruction; part: LONGINT; VAR destinationRegister, sourceOperand: Operand);
  2659. BEGIN
  2660. destinationRegister := AcquireDestinationRegister(irInstruction.op1, part, emptyOperand);
  2661. sourceOperand := RegisterFromIrOperand(irInstruction.op2, part, destinationRegister); (* note that the destination register is used as hint *)
  2662. END PrepareSingleSourceOp;
  2663. PROCEDURE PrepareSingleSourceOpWithImmediate(VAR irInstruction: IntermediateCode.Instruction; part: LONGINT; VAR destinationRegister, sourceOperand: Operand);
  2664. BEGIN
  2665. destinationRegister := AcquireDestinationRegister(irInstruction.op1, part, emptyOperand);
  2666. sourceOperand := RegisterOrImmediateFromIrOperand(irInstruction.op2, part, destinationRegister); (* note that the destination register is used as hint *)
  2667. END PrepareSingleSourceOpWithImmediate;
  2668. PROCEDURE PrepareDoubleSourceOpWithImmediate(CONST irInstruction: IntermediateCode.Instruction; part: LONGINT; VAR destinationRegister, leftSourceOperand, rightSourceOperand: Operand; VAR isSwapped: BOOLEAN);
  2669. VAR
  2670. irDestination, irLeft, irRight: IntermediateCode.Operand;
  2671. BEGIN
  2672. irDestination := irInstruction.op1;
  2673. irLeft := irInstruction.op2;
  2674. irRight := irInstruction.op3;
  2675. destinationRegister:= AcquireDestinationRegister(irDestination, part, emptyOperand);
  2676. (* swap operands such that the right one is an immediate *)
  2677. IF IrOperandIsDirectlyEncodable(irLeft, part) & ~IrOperandIsDirectlyEncodable(irRight, part) THEN
  2678. SwapIrOperands(irLeft, irRight);
  2679. isSwapped := TRUE
  2680. ELSIF IntermediateCode.OperandEquals(irRight, irDestination) THEN
  2681. SwapIrOperands(irLeft, irRight);
  2682. isSwapped := TRUE
  2683. ELSE
  2684. isSwapped := FALSE
  2685. END;
  2686. leftSourceOperand := RegisterFromIrOperand(irLeft, part, destinationRegister); (* the destination register is used as hint *)
  2687. IF IsSameRegister(leftSourceOperand, destinationRegister) THEN
  2688. rightSourceOperand := RegisterOrImmediateFromIrOperand(irRight, part, emptyOperand) (* no hint is provided *)
  2689. ELSE
  2690. rightSourceOperand := RegisterOrImmediateFromIrOperand(irRight, part, destinationRegister) (* the destination register is again used as hint *)
  2691. END
  2692. END PrepareDoubleSourceOpWithImmediate;
  2693. PROCEDURE PrepareDoubleSourceOp(CONST irInstruction: IntermediateCode.Instruction; part: LONGINT; VAR destinationRegister, leftSourceOperand, rightSourceOperand: Operand);
  2694. VAR
  2695. irDestination, irLeft, irRight: IntermediateCode.Operand;
  2696. BEGIN
  2697. irDestination := irInstruction.op1;
  2698. irLeft := irInstruction.op2;
  2699. irRight := irInstruction.op3;
  2700. destinationRegister:= AcquireDestinationRegister(irDestination, part, emptyOperand);
  2701. IF IntermediateCode.OperandEquals(irRight, irDestination) THEN
  2702. leftSourceOperand := RegisterFromIrOperand(irLeft, part, emptyOperand); (* do not use destination register as hint *)
  2703. ELSE
  2704. leftSourceOperand := RegisterFromIrOperand(irLeft, part, destinationRegister); (* the destination register is used as hint *)
  2705. END;
  2706. IF IsSameRegister(leftSourceOperand, destinationRegister) OR IntermediateCode.OperandEquals(irRight, irDestination) THEN
  2707. rightSourceOperand := RegisterFromIrOperand(irRight, part, emptyOperand) (* no hint is provided *)
  2708. ELSE
  2709. rightSourceOperand := RegisterFromIrOperand(irRight, part, destinationRegister) (* the destination register is again used as hint *)
  2710. END
  2711. END PrepareDoubleSourceOp;
  2712. END CodeGeneratorARM;
  2713. BackendARM = OBJECT(IntermediateBackend.IntermediateBackend)
  2714. VAR
  2715. cg: CodeGeneratorARM;
  2716. system: Global.System;
  2717. useFPU: BOOLEAN;
  2718. initLocals: BOOLEAN;
  2719. PROCEDURE & InitBackendARM;
  2720. BEGIN
  2721. useFPU := FALSE;
  2722. InitIntermediateBackend;
  2723. SetRuntimeModuleName(DefaultRuntimeModuleName);
  2724. SetNewObjectFile(TRUE,FALSE);
  2725. system := NIL;
  2726. initLocals := TRUE;
  2727. SetHasLinkRegister;
  2728. END InitBackendARM;
  2729. PROCEDURE Initialize(diagnostics: Diagnostics.Diagnostics; log: Streams.Writer; flags: SET; checker: SemanticChecker.Checker; system: Global.System; activeCellsSpecification: ActiveCells.Specification);
  2730. BEGIN
  2731. Initialize^(diagnostics, log, flags, checker, system, activeCellsSpecification);
  2732. NEW(cg, runtimeModuleName, diagnostics, SELF)
  2733. END Initialize;
  2734. PROCEDURE EnterCustomBuiltins;
  2735. VAR
  2736. procedureType: SyntaxTree.ProcedureType;
  2737. parameter: SyntaxTree.Parameter;
  2738. PROCEDURE New;
  2739. BEGIN procedureType := SyntaxTree.NewProcedureType(-1, NIL)
  2740. END New;
  2741. PROCEDURE BoolRet;
  2742. BEGIN procedureType.SetReturnType(system.booleanType)
  2743. END BoolRet;
  2744. PROCEDURE IntRet;
  2745. BEGIN procedureType.SetReturnType(Global.Integer32)
  2746. END IntRet;
  2747. PROCEDURE IntPar;
  2748. BEGIN
  2749. parameter := SyntaxTree.NewParameter(-1, procedureType, SyntaxTree.NewIdentifier(""), SyntaxTree.ValueParameter);
  2750. parameter.SetType(Global.Integer32); procedureType.AddParameter(parameter)
  2751. END IntPar;
  2752. PROCEDURE IntVarPar;
  2753. BEGIN
  2754. parameter := SyntaxTree.NewParameter(-1, procedureType, SyntaxTree.NewIdentifier(""), SyntaxTree.VarParameter);
  2755. parameter.SetType(Global.Integer32); procedureType.AddParameter(parameter)
  2756. END IntVarPar;
  2757. PROCEDURE RealVarPar;
  2758. BEGIN
  2759. parameter := SyntaxTree.NewParameter(-1, procedureType, SyntaxTree.NewIdentifier(""), SyntaxTree.VarParameter);
  2760. parameter.SetType(Global.Float32); procedureType.AddParameter(parameter)
  2761. END RealVarPar;
  2762. PROCEDURE Finish(CONST name: ARRAY OF CHAR; number: SHORTINT);
  2763. BEGIN Global.NewCustomBuiltin(name, system.systemScope, number, procedureType);
  2764. END Finish;
  2765. BEGIN
  2766. New; IntRet; Finish("SP", GetSP);
  2767. New; IntPar; Finish("SetSP", SetSP);
  2768. New; IntRet; Finish("FP", GetFP);
  2769. New; IntPar; Finish("SetFP", SetFP);
  2770. New; IntRet; Finish("PC", GetPC);
  2771. New; IntPar; Finish("SetPC", SetPC);
  2772. New; IntRet; Finish("LNK", GetLNK);
  2773. New; IntPar; Finish("SetLNK", SetLNK);
  2774. New; IntPar; IntPar; Finish("LDPSR", LDPSR);
  2775. New; IntPar; IntVarPar; Finish("STPSR", STPSR);
  2776. New; IntPar; IntPar; IntPar; Finish("LDCPR", LDCPR);
  2777. New; IntPar; IntPar; IntVarPar; Finish("STCPR", STCPR);
  2778. New; IntPar; Finish("FLUSH", FLUSH);
  2779. New; BoolRet; IntPar; Finish("NULL", NULL);
  2780. New; IntRet; IntPar; IntPar; Finish("XOR", XOR);
  2781. New; IntVarPar; IntPar; IntPar; Finish("MULD", MULD);
  2782. New; IntVarPar; IntPar; IntPar; Finish("ADDC", ADDC);
  2783. New; RealVarPar; IntPar; Finish("PACK", PACK);
  2784. New; RealVarPar; IntVarPar; Finish("UNPK", UNPK);
  2785. END EnterCustomBuiltins;
  2786. PROCEDURE GetSystem(): Global.System;
  2787. BEGIN
  2788. (* create system object if not yet existing *)
  2789. IF system = NIL THEN
  2790. (* used stack frame layout:
  2791. param 1
  2792. param 2
  2793. ...
  2794. param n-1
  2795. FP+8 -> param n
  2796. FP+4 -> old LR
  2797. FP -> old FP
  2798. FP-4 -> local 1
  2799. local 2
  2800. ...
  2801. spill 1
  2802. spill 2
  2803. ....
  2804. *)
  2805. (*
  2806. codeUnit, dataUnit = 8, 8
  2807. addressSize = 32
  2808. minVarAlign, maxVarAlign = 32, 32
  2809. minParAlign, maxParAlign = 8, 32
  2810. offsetFirstPar = 32 * 2
  2811. registerParameters = 0
  2812. *)
  2813. NEW(system, 8, 8, 32, (*32*) 8, 32, 8, 32, 32 * 2, 0, cooperative);
  2814. IF oberon07 THEN
  2815. IF Trace THEN D.String("Oberon07"); D.Ln END;
  2816. Global.SetDefaultDeclarations(system, 32) (* each basic type uses at least 32 bits -> INTEGER will be 32 bits long *)
  2817. ELSE
  2818. IF Trace THEN D.String("not Oberon07"); D.Ln END;
  2819. Global.SetDefaultDeclarations(system, 8) (* INTEGER will be 16 bits long *)
  2820. END;
  2821. Global.SetDefaultOperators(system);
  2822. EnterCustomBuiltins
  2823. END;
  2824. RETURN system
  2825. END GetSystem;
  2826. (** whether the code generator can generate code for a certain IR instruction
  2827. if not, where to find the runtime procedure that is to be called instead **)
  2828. PROCEDURE SupportedInstruction(CONST irInstruction: IntermediateCode.Instruction; VAR moduleName, procedureName: ARRAY OF CHAR): BOOLEAN;
  2829. BEGIN
  2830. (* only necessary for binary object file format for symbol / module entry in IntermediateBackend *)
  2831. RETURN cg.Supported(irInstruction, moduleName, procedureName);
  2832. END SupportedInstruction;
  2833. (** whether a certain intermediate code immediate value can be directly appear in code
  2834. if not, the value is stored in a const section and loaded from there **)
  2835. PROCEDURE SupportedImmediate(CONST irImmediateOperand: IntermediateCode.Operand): BOOLEAN;
  2836. VAR
  2837. result: BOOLEAN;
  2838. BEGIN
  2839. (* TODO: remove this *)
  2840. RETURN TRUE; (* tentatively generate all immediates, as symbol fixups are not yet implemented *)
  2841. result := FALSE;
  2842. IF (irImmediateOperand.type.form IN IntermediateCode.Integer) & (irImmediateOperand.type.sizeInBits <= 32) THEN
  2843. (* 32 bit integers *)
  2844. IF cg.ValueIsDirectlyEncodable(LONGINT(irImmediateOperand.intValue)) THEN
  2845. (* the value can be directly encoded as an ARM immediate operand *)
  2846. result := TRUE
  2847. ELSIF cg.ValueComposition(LONGINT(irImmediateOperand.intValue), FALSE, emptyOperand) <= 2 THEN (* TODO: find reasonable limit *)
  2848. (* the value can be generated using a limited amount of intructions *)
  2849. result := TRUE
  2850. END
  2851. END;
  2852. RETURN result
  2853. END SupportedImmediate;
  2854. PROCEDURE GenerateBinary(module: Sections.Module; dump: Streams.Writer);
  2855. VAR
  2856. in: Sections.Section;
  2857. out: BinaryCode.Section;
  2858. name: Basic.SectionName;
  2859. procedure: SyntaxTree.Procedure;
  2860. i, j, initialSectionCount: LONGINT;
  2861. (* recompute fixup positions and assign binary sections *)
  2862. PROCEDURE PatchFixups(section: BinaryCode.Section);
  2863. VAR resolved: BinaryCode.Section; fixup: BinaryCode.Fixup; displacement,symbolOffset: LONGINT; in: IntermediateCode.Section;
  2864. symbol: Sections.Section;
  2865. BEGIN
  2866. fixup := section.fixupList.firstFixup;
  2867. WHILE fixup # NIL DO
  2868. symbol := module.allSections.FindByName(fixup.symbol.name);
  2869. IF (symbol # NIL) & (symbol(IntermediateCode.Section).resolved # NIL) THEN
  2870. resolved := symbol(IntermediateCode.Section).resolved(BinaryCode.Section);
  2871. in := symbol(IntermediateCode.Section);
  2872. symbolOffset := fixup.symbolOffset;
  2873. IF symbolOffset = in.pc THEN
  2874. displacement := resolved.pc
  2875. ELSIF (symbolOffset # 0) THEN
  2876. ASSERT(in.pc > symbolOffset);
  2877. displacement := in.instructions[symbolOffset].pc;
  2878. ELSE
  2879. displacement := 0;
  2880. END;
  2881. fixup.SetSymbol(fixup.symbol.name,fixup.symbol.fingerprint,0,fixup.displacement+displacement);
  2882. END;
  2883. fixup := fixup.nextFixup;
  2884. END;
  2885. END PatchFixups;
  2886. (*
  2887. PROCEDURE Resolve(VAR fixup: BinaryCode.Fixup);
  2888. BEGIN
  2889. IF (fixup.symbol.name # "") & (fixup.resolved = NIL) THEN fixup.resolved := module.allSections.FindByName(fixup.symbol.name) END;
  2890. END Resolve;
  2891. (* recompute fixup positions and assign binary sections *)
  2892. PROCEDURE PatchFixups(section: BinaryCode.Section);
  2893. VAR resolved: BinaryCode.Section; fixup: BinaryCode.Fixup; symbolOffset, offsetWithinSection: LONGINT; in: IntermediateCode.Section;
  2894. BEGIN
  2895. fixup := section.fixupList.firstFixup;
  2896. WHILE fixup # NIL DO
  2897. Resolve(fixup);
  2898. IF (fixup.resolved # NIL) & (fixup.resolved(IntermediateCode.Section).resolved # NIL) THEN
  2899. resolved := fixup.resolved(IntermediateCode.Section).resolved(BinaryCode.Section);
  2900. in := fixup.resolved(IntermediateCode.Section);
  2901. (* TODO: is this correct? *)
  2902. symbolOffset := fixup.symbolOffset;
  2903. ASSERT(fixup.symbolOffset < in.pc);
  2904. IF (fixup.symbolOffset # 0) & (symbolOffset < in.pc) THEN
  2905. offsetWithinSection := in.instructions[fixup.symbolOffset].pc;
  2906. (*
  2907. (* TENTATIVE *)
  2908. D.String("FIXUP PATCH:"); D.Ln;
  2909. D.String(" symbol name: "); fixup.symbol.DumpName(D.Log); D.String("/");
  2910. D.String(" symbol offset: "); D.Int(fixup.symbolOffset, 0); D.Ln;
  2911. D.String(" offsetWithinSection"); D.Int(offsetWithinSection, 0); D.Ln;
  2912. D.String(" fixup.displacement (before)"); D.Int(fixup.displacement, 0); D.Ln; ; D.Ln;
  2913. D.Update;
  2914. *)
  2915. (* remove the fixup's symbol offset (in IR units) and change the displacement (in system units) accordingly: *)
  2916. fixup.SetSymbol(fixup.symbol.name, fixup.symbol.fingerprint, 0, offsetWithinSection + fixup.displacement)
  2917. END
  2918. END;
  2919. fixup := fixup.nextFixup;
  2920. END;
  2921. END PatchFixups;
  2922. *)
  2923. BEGIN
  2924. cg.SetModule(module);
  2925. cg.dump := dump;
  2926. FOR i := 0 TO module.allSections.Length() - 1 DO
  2927. in := module.allSections.GetSection(i);
  2928. IF in.type = Sections.InlineCodeSection THEN
  2929. Basic.SegmentedNameToString(in.name, name);
  2930. out := ResolvedSection(in(IntermediateCode.Section));
  2931. cg.dump := out.comments;
  2932. cg.Section(in(IntermediateCode.Section), out);
  2933. IF in.symbol # NIL THEN
  2934. procedure := in.symbol(SyntaxTree.Procedure);
  2935. procedure.procedureScope.body.code.SetBinaryCode(out.os.bits);
  2936. END;
  2937. END
  2938. END;
  2939. initialSectionCount := 0;
  2940. REPEAT
  2941. j := initialSectionCount;
  2942. initialSectionCount := module.allSections.Length() ;
  2943. FOR i := j TO initialSectionCount - 1 DO
  2944. in := module.allSections.GetSection(i);
  2945. Basic.SegmentedNameToString(in.name, name);
  2946. IF (in.type # Sections.InlineCodeSection) & (in(IntermediateCode.Section).resolved = NIL) THEN
  2947. out := ResolvedSection(in(IntermediateCode.Section));
  2948. cg.Section(in(IntermediateCode.Section),out);
  2949. END
  2950. END
  2951. UNTIL initialSectionCount = module.allSections.Length(); (* process remaining sections that have been added during traversal of sections *)
  2952. FOR i := 0 TO module.allSections.Length() - 1 DO
  2953. in := module.allSections.GetSection(i);
  2954. Basic.SegmentedNameToString(in.name, name);
  2955. in := module.allSections.GetSection(i);
  2956. PatchFixups(in(IntermediateCode.Section).resolved)
  2957. END;
  2958. IF cg.error THEN Error("", Diagnostics.Invalid, Diagnostics.Invalid, "") END
  2959. END GenerateBinary;
  2960. (** create an ARM code module from an intermediate code module **)
  2961. PROCEDURE ProcessIntermediateCodeModule*(intermediateCodeModule: Formats.GeneratedModule): Formats.GeneratedModule;
  2962. VAR
  2963. result: Formats.GeneratedModule;
  2964. BEGIN
  2965. ASSERT(intermediateCodeModule IS Sections.Module);
  2966. result := ProcessIntermediateCodeModule^(intermediateCodeModule);
  2967. IF ~error THEN
  2968. GenerateBinary(result(Sections.Module), dump);
  2969. IF dump # NIL THEN
  2970. dump.Ln; dump.Ln;
  2971. dump.String("------------------ binary code -------------------"); dump.Ln;
  2972. IF (traceString="") OR (traceString="*") THEN
  2973. result.Dump(dump);
  2974. dump.Update
  2975. ELSE
  2976. Sections.DumpFiltered(dump, result(Sections.Module), traceString);
  2977. dump.Update;
  2978. END
  2979. END;
  2980. END;
  2981. RETURN result
  2982. FINALLY
  2983. IF dump # NIL THEN
  2984. dump.Ln; dump.Ln;
  2985. dump.String("------------------ rescued code (code generation trapped) -------------------"); dump.Ln;
  2986. IF (traceString="") OR (traceString="*") THEN
  2987. result.Dump(dump);
  2988. dump.Update
  2989. ELSE
  2990. Sections.DumpFiltered(dump,result(Sections.Module),traceString);
  2991. dump.Update;
  2992. END
  2993. END;
  2994. RETURN result
  2995. END ProcessIntermediateCodeModule;
  2996. PROCEDURE DefineOptions(options: Options.Options);
  2997. BEGIN
  2998. options.Add(0X, UseFPUFlag, Options.Flag);
  2999. options.Add(0X, "noInitLocals", Options.Flag);
  3000. DefineOptions^(options);
  3001. END DefineOptions;
  3002. PROCEDURE GetOptions(options: Options.Options);
  3003. BEGIN
  3004. IF options.GetFlag(UseFPUFlag) THEN useFPU := TRUE END;
  3005. IF options.GetFlag("noInitLocals") THEN initLocals := FALSE END;
  3006. GetOptions^(options);
  3007. END GetOptions;
  3008. PROCEDURE DefaultObjectFileFormat(): Formats.ObjectFileFormat;
  3009. BEGIN RETURN ObjectFileFormat.Get();
  3010. END DefaultObjectFileFormat;
  3011. PROCEDURE DefaultSymbolFileFormat(): Formats.SymbolFileFormat;
  3012. BEGIN RETURN NIL
  3013. END DefaultSymbolFileFormat;
  3014. (** get the name of the backend **)
  3015. PROCEDURE GetDescription(VAR instructionSet: ARRAY OF CHAR);
  3016. BEGIN instructionSet := "ARM"
  3017. END GetDescription;
  3018. PROCEDURE FindPC(x: SyntaxTree.Module; CONST sectionName: ARRAY OF CHAR; sectionOffset: LONGINT);
  3019. VAR
  3020. section: Sections.Section; binarySection: BinaryCode.Section; label: BinaryCode.LabelList; module: Formats.GeneratedModule;
  3021. i: LONGINT; pooledName: Basic.SegmentedName;
  3022. BEGIN
  3023. module := ProcessSyntaxTreeModule(x);
  3024. Basic.ToSegmentedName(sectionName, pooledName);
  3025. i := 0;
  3026. REPEAT
  3027. section := module(Sections.Module).allSections.GetSection(i);
  3028. INC(i);
  3029. UNTIL (i = module(Sections.Module).allSections.Length()) OR (section.name = pooledName);
  3030. IF section.name # pooledName THEN
  3031. diagnostics.Error(module.module.sourceName,Diagnostics.Invalid,Diagnostics.Invalid," could not locate pc");
  3032. ELSE
  3033. binarySection := section(IntermediateCode.Section).resolved;
  3034. label := binarySection.labels;
  3035. WHILE (label # NIL) & (label.offset >= sectionOffset) DO
  3036. label := label.prev;
  3037. END;
  3038. IF label # NIL THEN
  3039. diagnostics.Information(module.module.sourceName,label.position,Diagnostics.Invalid," pc position");
  3040. ELSE
  3041. diagnostics.Error(module.module.sourceName,Diagnostics.Invalid,Diagnostics.Invalid," could not locate pc");
  3042. END;
  3043. END;
  3044. END FindPC;
  3045. END BackendARM;
  3046. VAR
  3047. emptyOperand: Operand;
  3048. PROCEDURE Assert(condition: BOOLEAN; CONST message: ARRAY OF CHAR);
  3049. BEGIN ASSERT(condition, 100)
  3050. END Assert;
  3051. PROCEDURE Halt(CONST message: ARRAY OF CHAR);
  3052. BEGIN HALT(100)
  3053. END Halt;
  3054. PROCEDURE PowerOf2(val: HUGEINT; VAR exp: LONGINT): BOOLEAN;
  3055. BEGIN
  3056. IF val <= 0 THEN RETURN FALSE END;
  3057. exp := 0;
  3058. WHILE ~ODD(val) DO
  3059. val := val DIV 2;
  3060. INC(exp)
  3061. END;
  3062. RETURN val = 1
  3063. END PowerOf2;
  3064. (** get the ARM code section that corresponds to an intermediate code section **)
  3065. PROCEDURE ResolvedSection(irSection: IntermediateCode.Section): BinaryCode.Section;
  3066. VAR
  3067. result: BinaryCode.Section;
  3068. BEGIN
  3069. IF irSection.resolved = NIL THEN
  3070. NEW(result, irSection.type, irSection.priority, 8, irSection.name, irSection.comments # NIL, FALSE);
  3071. (* set fixed position or alignment
  3072. (also make sure that any section has an alignment of at least 4 bytes) *)
  3073. IF ~irSection.fixed & (irSection.positionOrAlignment < 4) THEN
  3074. result.SetAlignment(FALSE, 4)
  3075. ELSE
  3076. result.SetAlignment(irSection.fixed, irSection.positionOrAlignment);
  3077. END;
  3078. irSection.SetResolved(result)
  3079. ELSE
  3080. result := irSection.resolved
  3081. END;
  3082. RETURN result
  3083. END ResolvedSection;
  3084. (** initialize the module **)
  3085. PROCEDURE Init;
  3086. BEGIN InstructionSet.InitOperand(emptyOperand)
  3087. END Init;
  3088. (** get an instance of the ARM backend **)
  3089. PROCEDURE Get*(): Backend.Backend;
  3090. VAR
  3091. result: BackendARM;
  3092. BEGIN
  3093. NEW(result);
  3094. RETURN result
  3095. END Get;
  3096. (* only for testing purposes *)
  3097. PROCEDURE Test*;
  3098. VAR
  3099. codeGenerator: CodeGeneratorARM;
  3100. value, count: LONGINT;
  3101. BEGIN
  3102. NEW(codeGenerator, "", NIL, NIL);
  3103. FOR value := 0 TO 300 BY 1 DO
  3104. count := codeGenerator.ValueComposition(value, FALSE, emptyOperand);
  3105. D.String("value: "); D.Int(value, 0); D.String(" -> "); D.Int(count, 0); D.String(" instructions"); D.Ln;
  3106. END;
  3107. D.Ln; D.Update
  3108. END Test;
  3109. (* TODO: move this to Debugging.Mod or even Streams.Mod *)
  3110. (** write an integer in binary right-justified in a field of at least ABS(w) characters.
  3111. If w < 0 THEN ABS(w) least significant hex digits of 'value' are written (potentially including leading zeros or ones)
  3112. **)
  3113. PROCEDURE DBin*(value: HUGEINT; numberDigits: LONGINT);
  3114. CONST
  3115. MaxBitSize = SIZEOF(HUGEINT) * 8;
  3116. VAR
  3117. i, firstRelevantPos: LONGINT;
  3118. prefixWithSpaces: BOOLEAN;
  3119. chars: ARRAY MaxBitSize OF CHAR;
  3120. prefixChar: CHAR;
  3121. BEGIN
  3122. prefixWithSpaces := numberDigits >= 0;
  3123. numberDigits := ABS(numberDigits);
  3124. (*
  3125. - calculate an array containing the full bitstring
  3126. - determine the position of the first relevant digit
  3127. *)
  3128. firstRelevantPos := 0;
  3129. FOR i := MaxBitSize - 1 TO 0 BY -1 DO
  3130. IF ODD(value) THEN
  3131. chars[i] := '1';
  3132. firstRelevantPos := i (* occurence of a '1' -> changes the first relevant position *)
  3133. ELSE
  3134. chars[i] := '0'
  3135. END;
  3136. value := value DIV 2
  3137. END;
  3138. (* if space prefixing is enabled, limit the number of digits to the relevant digits *)
  3139. IF prefixWithSpaces THEN numberDigits := MAX(numberDigits, MaxBitSize - firstRelevantPos) END;
  3140. IF numberDigits > MaxBitSize THEN
  3141. IF prefixWithSpaces THEN prefixChar := ' ' ELSE prefixChar := chars[0] END; (* use spaces or sign bit *)
  3142. FOR i := 1 TO numberDigits - MaxBitSize DO D.Char(prefixChar) END;
  3143. numberDigits := MaxBitSize
  3144. END;
  3145. ASSERT((numberDigits >= 0) & (numberDigits <= MaxBitSize));
  3146. FOR i := MaxBitSize - numberDigits TO MaxBitSize - 1 DO
  3147. IF prefixWithSpaces & (i < firstRelevantPos) THEN D.Char(' ') ELSE D.Char(chars[i]) END
  3148. END;
  3149. D.Ln;
  3150. END DBin;
  3151. BEGIN
  3152. Init;
  3153. END FoxARMBackend.
  3154. SystemTools.FreeDownTo FoxARMBackend ~