FoxARMBackend.Mod 161 KB

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