FoxARMBackend.Mod 154 KB

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