FoxARMBackend.Mod 155 KB

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