FoxARMBackend.Mod 144 KB

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