FoxAMDBackend.Mod 141 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662
  1. MODULE FoxAMDBackend; (** AUTHOR ""; PURPOSE ""; *)
  2. IMPORT
  3. Basic := FoxBasic, Scanner := FoxScanner, SyntaxTree := FoxSyntaxTree, Global := FoxGlobal, Backend := FoxBackend, Sections := FoxSections,
  4. IntermediateCode := FoxIntermediateCode, IntermediateBackend := FoxIntermediateBackend, BinaryCode := FoxBinaryCode,
  5. InstructionSet := FoxAMD64InstructionSet, Assembler := FoxAMD64Assembler, SemanticChecker := FoxSemanticChecker, Formats := FoxFormats,
  6. Diagnostics, Streams, Options, Strings, ObjectFileFormat := FoxBinaryObjectFile, Compiler,
  7. Machine, D := Debugging, CodeGenerators := FoxCodeGenerators, ObjectFile;
  8. CONST
  9. (* constants for the register allocator *)
  10. none=-1;
  11. RAX=InstructionSet.regRAX; RCX=InstructionSet.regRCX; RDX=InstructionSet.regRDX; RBX=InstructionSet.regRBX;
  12. RSP=InstructionSet.regRSP; RBP=InstructionSet.regRBP; RSI=InstructionSet.regRSI; RDI=InstructionSet.regRDI;
  13. R8=InstructionSet.regR8; R9=InstructionSet.regR9; R10=InstructionSet.regR10; R11=InstructionSet.regR11;
  14. R12=InstructionSet.regR12; R13=InstructionSet.regR13; R14=InstructionSet.regR14; R15=InstructionSet.regR15;
  15. EAX=InstructionSet.regEAX; ECX=InstructionSet.regECX; EDX=InstructionSet.regEDX; EBX=InstructionSet.regEBX;
  16. ESP=InstructionSet.regESP; EBP=InstructionSet.regEBP; ESI=InstructionSet.regESI; EDI=InstructionSet.regEDI;
  17. R8D=InstructionSet.regR8D; R9D=InstructionSet.regR9D; R10D=InstructionSet.regR10D; R11D=InstructionSet.regR11D;
  18. R12D=InstructionSet.regR12D; R13D=InstructionSet.regR13D; R14D=InstructionSet.regR14D; R15D=InstructionSet.regR15D;
  19. AX=InstructionSet.regAX; CX=InstructionSet.regCX; DX=InstructionSet.regDX; BX=InstructionSet.regBX;
  20. SI=InstructionSet.regSI; DI=InstructionSet.regDI; BP=InstructionSet.regBP; SP=InstructionSet.regSP;
  21. R8W=InstructionSet.regR8W; R9W=InstructionSet.regR9W; R10W=InstructionSet.regR10W; R11W=InstructionSet.regR11W;
  22. R12W=InstructionSet.regR12W; R13W=InstructionSet.regR13W; R14W=InstructionSet.regR14W; R15W=InstructionSet.regR15W;
  23. AL=InstructionSet.regAL; CL=InstructionSet.regCL; DL=InstructionSet.regDL; BL=InstructionSet.regBL; SIL=InstructionSet.regSIL;
  24. DIL=InstructionSet.regDIL; BPL=InstructionSet.regBPL; SPL=InstructionSet.regSPL;
  25. R8B=InstructionSet.regR8B; R9B=InstructionSet.regR9B; R10B=InstructionSet.regR10B; R11B=InstructionSet.regR11B;
  26. R12B=InstructionSet.regR12B; R13B=InstructionSet.regR13B; R14B=InstructionSet.regR14B; R15B=InstructionSet.regR15B;
  27. AH=InstructionSet.regAH; CH=InstructionSet.regCH; DH=InstructionSet.regDH; BH=InstructionSet.regBH;
  28. ST0=InstructionSet.regST0;
  29. XMM0 = InstructionSet.regXMM0;
  30. XMM7 = InstructionSet.regXMM7;
  31. YMM0 = InstructionSet.regYMM0;
  32. YMM7 = InstructionSet.regYMM7;
  33. Low=0; High=1;
  34. FrameSpillStack=TRUE;
  35. VAR registerOperands: ARRAY InstructionSet.numberRegisters OF Assembler.Operand;
  36. usePool: BOOLEAN;
  37. opEAX, opECX, opEDX, opEBX, opESP, opEBP,
  38. opESI, opEDI, opAX, opCX, opDX, opBX, opSI, opDI, opAL, opCL, opDL, opBL, opAH, opCH, opDH, opBH,opST0
  39. , opRSP, opRBP: Assembler.Operand;
  40. unusable,split,blocked,free: CodeGenerators.Ticket;
  41. traceStackSize: LONGINT;
  42. TYPE
  43. Ticket=CodeGenerators.Ticket;
  44. PhysicalRegisters*=OBJECT (CodeGenerators.PhysicalRegisters)
  45. VAR
  46. toVirtual: ARRAY InstructionSet.numberRegisters OF Ticket; (* registers real register -> none / reserved / split / blocked / virtual register (>0) *)
  47. reserved: ARRAY InstructionSet.numberRegisters OF BOOLEAN;
  48. hint: LONGINT;
  49. useFPU: BOOLEAN;
  50. PROCEDURE &InitPhysicalRegisters(fpu,cooperative: BOOLEAN);
  51. VAR i: LONGINT;
  52. BEGIN
  53. FOR i := 0 TO LEN(toVirtual)-1 DO
  54. toVirtual[i] := NIL;
  55. reserved[i] := FALSE;
  56. END;
  57. (* reserve stack and base pointer registers *)
  58. toVirtual[BPL] := unusable;
  59. toVirtual[SPL] := unusable;
  60. toVirtual[BP] := unusable;
  61. toVirtual[SP] := unusable;
  62. toVirtual[EBP] := unusable;
  63. toVirtual[ESP] := unusable;
  64. toVirtual[RBP] := unusable;
  65. toVirtual[RSP] := unusable;
  66. hint := none;
  67. useFPU := fpu
  68. END InitPhysicalRegisters;
  69. PROCEDURE AllocationHint*(index: LONGINT);
  70. BEGIN hint := index
  71. END AllocationHint;
  72. PROCEDURE NumberRegisters*(): LONGINT;
  73. BEGIN
  74. RETURN LEN(toVirtual)
  75. END NumberRegisters;
  76. END PhysicalRegisters;
  77. PhysicalRegisters32=OBJECT (PhysicalRegisters) (* 32 bit implementation *)
  78. PROCEDURE & InitPhysicalRegisters32(fpu,cooperative: BOOLEAN);
  79. VAR i: LONGINT;
  80. BEGIN
  81. InitPhysicalRegisters(fpu,cooperative);
  82. (* disable registers that are only usable in 64 bit mode *)
  83. FOR i := 0 TO 31 DO
  84. toVirtual[i+RAX] := unusable;
  85. END;
  86. FOR i := 8 TO 15 DO
  87. toVirtual[i+AL] := unusable;
  88. toVirtual[i+AH] := unusable;
  89. toVirtual[i+EAX] := unusable;
  90. toVirtual[i+AX] := unusable;
  91. END;
  92. FOR i := 4 TO 7 DO
  93. toVirtual[i+AL] := unusable;
  94. toVirtual[i+AH] := unusable;
  95. END;
  96. FOR i := 0 TO LEN(reserved)-1 DO reserved[i] := FALSE END;
  97. END InitPhysicalRegisters32;
  98. PROCEDURE Allocate*(index: LONGINT; virtualRegister: Ticket);
  99. BEGIN
  100. (*
  101. D.String("allocate register x : index="); D.Int(index,1); D.Ln;
  102. *)
  103. Assert(toVirtual[index] = free,"register already allocated");
  104. toVirtual[index] := virtualRegister;
  105. IF index DIV 32 = 2 THEN (* 32 bit *)
  106. Assert(toVirtual[index MOD 32 + AX] = free,"free register split");
  107. toVirtual[index MOD 32 + AX] := blocked;
  108. IF index MOD 32 < 4 THEN
  109. Assert(toVirtual[index MOD 32 + AL] = free,"register already allocated");
  110. Assert(toVirtual[index MOD 32 + AH] = free,"register already allocated");
  111. toVirtual[index MOD 32 + AL] := blocked;
  112. toVirtual[index MOD 32 + AH] := blocked;
  113. END;
  114. ELSIF index DIV 32 = 1 THEN (* 16 bit *)
  115. Assert(toVirtual[index MOD 8 + EAX] = free,"free register split");
  116. toVirtual[index MOD 32 + EAX] := split;
  117. IF index MOD 32 < 4 THEN
  118. Assert(toVirtual[index MOD 32 + AL] = free,"register already allocated");
  119. Assert(toVirtual[index MOD 32 + AH] = free,"register already allocated");
  120. toVirtual[index MOD 32 + AL] := blocked;
  121. toVirtual[index MOD 32 + AH] := blocked;
  122. END;
  123. ELSIF index DIV 32 = 0 THEN (* 8 bit *)
  124. Assert((toVirtual[index MOD 4 + EAX] = free) OR (toVirtual[index MOD 4 + EAX] = split),"free register blocked");
  125. Assert((toVirtual[index MOD 4 + AX] = free) OR (toVirtual[index MOD 4 + AX] = split),"free register blocked");
  126. toVirtual[index MOD 4 + EAX] := split;
  127. toVirtual[index MOD 4 + AX] := split;
  128. ELSIF (index >= XMM0) & (index <= XMM7) THEN (* vector register *)
  129. ELSIF (index >= YMM0) & (index <= YMM7) THEN (* vector register *)
  130. ELSE Assert( (index >=InstructionSet.regST0) & (index <= InstructionSet.regST7 ),"not a float register"); (* floats *)
  131. END;
  132. END Allocate;
  133. PROCEDURE SetReserved*(index: LONGINT; res: BOOLEAN);
  134. BEGIN
  135. IF index DIV 32 <=2 THEN
  136. index := index MOD 16;
  137. reserved[index+AH] := res;
  138. reserved[index+AL] := res;
  139. reserved[index+AX] := res;
  140. reserved[index+EAX] := res;
  141. ELSE
  142. reserved[index] := res;
  143. END;
  144. END SetReserved;
  145. PROCEDURE Reserved*(index: LONGINT): BOOLEAN;
  146. BEGIN
  147. RETURN (index>0) & reserved[index]
  148. END Reserved;
  149. PROCEDURE Free*(index: LONGINT);
  150. VAR x: Ticket;
  151. BEGIN
  152. (*
  153. D.String("free register x : index="); D.Int(index,1); D.Ln;
  154. *)
  155. x := toVirtual[index];
  156. Assert((toVirtual[index] # NIL),"register not reserved");
  157. toVirtual[index] := free;
  158. IF index DIV 32 =2 THEN (* 32 bit *)
  159. Assert(toVirtual[index MOD 32 + AX] = blocked,"reserved register did not block");
  160. toVirtual[index MOD 32 + AX] := free;
  161. IF index MOD 32 < 4 THEN
  162. Assert(toVirtual[index MOD 32 + AL] = blocked,"reserved register did not block");
  163. Assert(toVirtual[index MOD 32 + AH] = blocked,"reserved register did not block");
  164. toVirtual[index MOD 32 + AL] := free;
  165. toVirtual[index MOD 32 + AH] := free;
  166. END;
  167. ELSIF index DIV 32 = 1 THEN (* 16 bit *)
  168. Assert(toVirtual[index MOD 32 + EAX] = split,"reserved register did not split");
  169. toVirtual[index MOD 32 + EAX] := free;
  170. IF index MOD 32 < 4 THEN
  171. Assert(toVirtual[index MOD 32 + AL] = blocked,"reserved register did not block");
  172. Assert(toVirtual[index MOD 32 + AH] = blocked,"reserved register did not block");
  173. toVirtual[index MOD 32 + AL] := free;
  174. toVirtual[index MOD 32 + AH] := free;
  175. END;
  176. ELSIF index DIV 32 = 0 THEN (* 8 bit *)
  177. IF (toVirtual[index MOD 4 + AL] = free) & (toVirtual[index MOD 4 + AH] = free) THEN
  178. Assert(toVirtual[index MOD 4 + EAX] = split,"reserved register did not split");
  179. Assert(toVirtual[index MOD 4 + AX] = split,"reserved register did not split");
  180. toVirtual[index MOD 4 + EAX] := free;
  181. toVirtual[index MOD 4 + AX] := free;
  182. END;
  183. ELSIF (index >= XMM0) & (index <= XMM7) THEN (* vector register *)
  184. ELSIF (index >= YMM0) & (index <= YMM7) THEN (* vector register *)
  185. ELSE Assert( (index >=InstructionSet.regST0) & (index <= InstructionSet.regST7 ),"not a float register"); (* floats *)
  186. END;
  187. END Free;
  188. PROCEDURE NextFree*(CONST type: IntermediateCode.Type):LONGINT;
  189. VAR i,sizeInBits,length, form: LONGINT;
  190. PROCEDURE GetGPHint(offset: LONGINT): LONGINT;
  191. VAR res: LONGINT;
  192. BEGIN
  193. IF (hint # none) & (hint >= AL) & (hint <= EDI) & (toVirtual[hint MOD 32 + offset]=free) & ~Reserved(hint) THEN res := hint MOD 32 + offset ELSE res := none END;
  194. hint := none;
  195. RETURN res
  196. END GetGPHint;
  197. PROCEDURE GetHint(from,to: LONGINT): LONGINT;
  198. VAR res: LONGINT;
  199. BEGIN
  200. IF (hint # none) & (hint >= from) & (hint <= to) & (toVirtual[hint]=free) & ~Reserved(hint) THEN res := hint ELSE res := none END;
  201. hint := none;
  202. RETURN res
  203. END GetHint;
  204. PROCEDURE Get(from,to: LONGINT): LONGINT;
  205. VAR i: LONGINT;
  206. BEGIN
  207. i := from;
  208. IF from <= to THEN
  209. WHILE (i <= to) & ((toVirtual[i]#free) OR Reserved(i)) DO INC(i) END;
  210. IF i > to THEN i := none END;
  211. ELSE
  212. WHILE (i >=to) & ((toVirtual[i]#free) OR Reserved(i)) DO DEC(i) END;
  213. IF i < to THEN i := none END;
  214. END;
  215. RETURN i
  216. END Get;
  217. BEGIN
  218. length := type.length;
  219. sizeInBits := type.sizeInBits;
  220. form := type.form;
  221. IF (type.length > 1) THEN
  222. IF (* (type.form = IntermediateCode.Float) &*) (type.sizeInBits<=32) & (type.length =4) THEN
  223. i := Get(XMM7, XMM0);
  224. ELSIF (* (type.form = IntermediateCode.Float) &*) (type.sizeInBits<=32) & (type.length =8) THEN
  225. i := Get(YMM7, YMM0);
  226. ELSE
  227. HALT(100)
  228. END
  229. ELSIF type.form IN IntermediateCode.Integer THEN
  230. sizeInBits := type.sizeInBits;
  231. IF type.sizeInBits = IntermediateCode.Bits8 THEN
  232. i := GetGPHint(AL);
  233. IF i = none THEN i := Get(BL, AL) END;
  234. IF i = none THEN i := Get(BH, AH) END;
  235. ELSIF type.sizeInBits = IntermediateCode.Bits16 THEN
  236. i := GetGPHint(AX);
  237. IF i = none THEN i := Get(DI, SI) END;
  238. IF i = none THEN i := Get(BX, AX) END;
  239. ELSIF type.sizeInBits = IntermediateCode.Bits32 THEN
  240. i := GetGPHint(EAX);
  241. IF i = none THEN i := Get(EDI,ESI) END;
  242. IF i = none THEN i := Get(EBX,EAX) END;
  243. ELSE HALT(100)
  244. END;
  245. ELSE
  246. ASSERT(type.form = IntermediateCode.Float);
  247. IF useFPU THEN
  248. i := Get(InstructionSet.regST0, InstructionSet.regST6);
  249. (* ST7 unusable as it is overwritten during arithmetic instructions *)
  250. ELSE
  251. i := GetHint(XMM0, XMM7);
  252. IF i = none THEN i := Get(XMM7, XMM0) END
  253. END;
  254. END;
  255. hint := none; (* reset *)
  256. RETURN i
  257. END NextFree;
  258. PROCEDURE Mapped*(physical: LONGINT): Ticket;
  259. VAR virtual: Ticket;
  260. BEGIN
  261. virtual := toVirtual[physical];
  262. IF virtual = blocked THEN virtual := Mapped(physical+32)
  263. ELSIF virtual = split THEN
  264. IF physical < 32 THEN virtual := Mapped(physical+16 MOD 32)
  265. ELSE virtual := Mapped(physical-32)
  266. END;
  267. END;
  268. ASSERT((virtual = free) OR (virtual = unusable) OR (toVirtual[virtual.register] = virtual));
  269. RETURN virtual
  270. END Mapped;
  271. PROCEDURE Dump*(w: Streams.Writer);
  272. VAR i: LONGINT; virtual: Ticket;
  273. BEGIN
  274. w.String("; ---- registers ----"); w.Ln;
  275. FOR i := 0 TO LEN(toVirtual)-1 DO
  276. virtual := toVirtual[i];
  277. IF virtual # unusable THEN
  278. w.String("reg "); w.Int(i,1); w.String(": ");
  279. IF virtual = free THEN w.String("free")
  280. ELSIF virtual = blocked THEN w.String("blocked")
  281. ELSIF virtual = split THEN w.String("split")
  282. ELSE w.String(" r"); w.Int(virtual.register,1);
  283. END;
  284. IF reserved[i] THEN w.String("reserved") END;
  285. w.Ln;
  286. END;
  287. END;
  288. END Dump;
  289. END PhysicalRegisters32;
  290. PhysicalRegisters64=OBJECT (PhysicalRegisters) (* 64 bit implementation *)
  291. PROCEDURE & InitPhysicalRegisters64(fpu,cooperative: BOOLEAN);
  292. BEGIN
  293. InitPhysicalRegisters(fpu,cooperative);
  294. END InitPhysicalRegisters64;
  295. PROCEDURE SetReserved*(index: LONGINT; res: BOOLEAN);
  296. BEGIN
  297. (*
  298. IF res THEN D.String("reserve ") ELSE D.String("unreserve ") END;
  299. D.String("register: index="); D.Int(index,1); D.Ln;
  300. *)
  301. IF index DIV 32 <=2 THEN
  302. index := index MOD 16;
  303. reserved[index+AH] := res;
  304. reserved[index+AL] := res;
  305. reserved[index+AX] := res;
  306. reserved[index+EAX] := res;
  307. reserved[index+RAX] := res;
  308. ELSE
  309. reserved[index] := res
  310. END;
  311. END SetReserved;
  312. PROCEDURE Reserved*(index: LONGINT): BOOLEAN;
  313. BEGIN
  314. RETURN reserved[index]
  315. END Reserved;
  316. PROCEDURE Allocate*(index: LONGINT; virtualRegister: Ticket);
  317. BEGIN
  318. (*
  319. D.String("allocate register x : index="); D.Int(index,1); D.Ln;
  320. *)
  321. Assert(toVirtual[index] = free,"register already allocated");
  322. toVirtual[index] := virtualRegister;
  323. IF index DIV 32 = 3 THEN (* 64 bit *)
  324. Assert(toVirtual[index MOD 32 + EAX] = free,"free register split");
  325. toVirtual[index MOD 32 + EAX] := blocked;
  326. toVirtual[index MOD 32 + AX] := blocked;
  327. toVirtual[index MOD 32 + AL] := blocked;
  328. ELSIF index DIV 32 = 2 THEN (* 32 bit *)
  329. Assert(toVirtual[index MOD 32 + AX] = free,"free register split");
  330. toVirtual[index MOD 32 + RAX] := split;
  331. toVirtual[index MOD 32 + AX] := blocked;
  332. toVirtual[index MOD 32 + AL] := blocked;
  333. ELSIF index DIV 32 = 1 THEN (* 16 bit *)
  334. toVirtual[index MOD 32 + RAX] := split;
  335. toVirtual[index MOD 32 + EAX] := split;
  336. toVirtual[index MOD 32 + AL] := blocked;
  337. ELSIF index DIV 32 = 0 THEN (* 8 bit *)
  338. toVirtual[index MOD 32 + RAX] := split;
  339. toVirtual[index MOD 32 + EAX] := split;
  340. toVirtual[index MOD 32 + AX] := split;
  341. ELSIF (index >= XMM0) & (index <= XMM7) THEN (* vector register *)
  342. ELSIF (index >= YMM0) & (index <= YMM7) THEN (* vector register *)
  343. ELSE Assert( (index >=InstructionSet.regST0) & (index <= InstructionSet.regST7 ),"not a float register"); (* floats *)
  344. END;
  345. END Allocate;
  346. PROCEDURE Free*(index: LONGINT);
  347. BEGIN
  348. (*
  349. D.String("release register x : index="); D.Int(index,1); D.Ln;
  350. *)
  351. Assert(toVirtual[index]#NIL,"register not reserved");
  352. toVirtual[index] := free;
  353. IF index DIV 32 =3 THEN (* 64 bit *)
  354. Assert(toVirtual[index MOD 32 + EAX] = blocked,"reserved register did not block");
  355. toVirtual[index MOD 32 + EAX] := free;
  356. toVirtual[index MOD 32 + AX] := free;
  357. toVirtual[index MOD 32 + AL] := free;
  358. ELSIF index DIV 32 =2 THEN (* 32 bit *)
  359. Assert(toVirtual[index MOD 32 + RAX] = split,"reserved register did not split");
  360. Assert(toVirtual[index MOD 32 + AX] = blocked,"reserved register did not block");
  361. Assert(toVirtual[index MOD 32 + AL] = blocked,"reserved register did not block");
  362. toVirtual[index MOD 32 + RAX] := free;
  363. toVirtual[index MOD 32 + AX] := free;
  364. toVirtual[index MOD 32 + AL] := free;
  365. ELSIF index DIV 32 = 1 THEN (* 16 bit *)
  366. Assert(toVirtual[index MOD 32 + RAX] = split,"reserved register did not split");
  367. Assert(toVirtual[index MOD 32 + EAX] = split,"reserved register did not split");
  368. Assert(toVirtual[index MOD 32 + AL] = blocked,"reserved register did not split");
  369. toVirtual[index MOD 32 + RAX] := free;
  370. toVirtual[index MOD 32 + EAX] := free;
  371. toVirtual[index MOD 32 + AL] := free;
  372. ELSIF index DIV 32 = 0 THEN (* 8 bit *)
  373. Assert(toVirtual[index MOD 32 + RAX] = split,"reserved register did not split");
  374. Assert(toVirtual[index MOD 32 + EAX] = split,"reserved register did not split");
  375. Assert(toVirtual[index MOD 32 + AX] = split,"reserved register did not split");
  376. toVirtual[index MOD 32 + RAX] := free;
  377. toVirtual[index MOD 32 + EAX] := free;
  378. toVirtual[index MOD 32 + AX] := free;
  379. ELSIF (index >= XMM0) & (index <= XMM7) THEN (* vector register *)
  380. ELSIF (index >= YMM0) & (index <= YMM7) THEN (* vector register *)
  381. ELSE Assert( (index >=InstructionSet.regST0) & (index <= InstructionSet.regST7 ),"not a float register"); (* floats *)
  382. END;
  383. END Free;
  384. PROCEDURE NextFree*(CONST type: IntermediateCode.Type): LONGINT;
  385. VAR i: LONGINT;
  386. PROCEDURE GetGPHint(offset: LONGINT): LONGINT;
  387. VAR res: LONGINT;
  388. BEGIN
  389. IF (hint # none) & (hint >= AL) & (hint <= R15) & (toVirtual[hint MOD 32 + offset]=free) & ~Reserved(hint) THEN res := hint MOD 32 + offset ELSE res := none END;
  390. hint := none;
  391. RETURN res
  392. END GetGPHint;
  393. PROCEDURE Get(from,to: LONGINT): LONGINT;
  394. VAR i: LONGINT;
  395. BEGIN
  396. i := from;
  397. IF from <= to THEN
  398. WHILE (i <= to) & ((toVirtual[i]#free) OR Reserved(i)) DO INC(i) END;
  399. IF i > to THEN i := none END;
  400. ELSE
  401. WHILE (i >=to) & ((toVirtual[i]#free) OR Reserved(i)) DO DEC(i) END;
  402. IF i < to THEN i := none END;
  403. END;
  404. RETURN i
  405. END Get;
  406. BEGIN
  407. IF type.form IN IntermediateCode.Integer THEN
  408. IF type.sizeInBits = IntermediateCode.Bits8 THEN
  409. i := GetGPHint(AL);
  410. IF i = none THEN i := Get(BL, AL) END;
  411. IF i = none THEN i := Get(BH, AH) END;
  412. IF i = none THEN
  413. i := Get(AL,R15B)
  414. END;
  415. ELSIF type.sizeInBits = IntermediateCode.Bits16 THEN
  416. i := GetGPHint(AX);
  417. IF i = none THEN i := Get(DI, SI) END;
  418. IF i = none THEN i := Get(BX, AX) END;
  419. IF i = none THEN
  420. i := Get(AX,R15W);
  421. END;
  422. ELSIF type.sizeInBits = IntermediateCode.Bits32 THEN
  423. i := GetGPHint(EAX);
  424. IF i = none THEN i := Get(EDI,ESI) END;
  425. IF i = none THEN i := Get(EBX,EAX) END;
  426. IF i = none THEN
  427. i := Get(EAX,R15D);
  428. END;
  429. ELSIF type.sizeInBits = IntermediateCode.Bits64 THEN
  430. i := GetGPHint(RAX);
  431. IF i = none THEN i := Get(RDI,RSI) END;
  432. IF i = none THEN i := Get(RBX,RAX) END;
  433. IF i = none THEN
  434. i := Get(RAX, R15)
  435. END;
  436. ELSE HALT(100)
  437. END;
  438. ELSE
  439. ASSERT(type.form = IntermediateCode.Float);
  440. IF useFPU THEN
  441. i := Get(InstructionSet.regST0, InstructionSet.regST6);
  442. (* ST7 unusable as it is overwritten during arithmetic instructions *)
  443. ELSE
  444. i := Get(XMM7, XMM0)
  445. END;
  446. END;
  447. RETURN i;
  448. END NextFree;
  449. PROCEDURE Mapped*(physical: LONGINT): Ticket;
  450. VAR virtual: Ticket;
  451. BEGIN
  452. virtual := toVirtual[physical];
  453. IF virtual = blocked THEN RETURN Mapped(physical+32) END;
  454. IF virtual = split THEN RETURN Mapped(physical-32) END;
  455. RETURN virtual
  456. END Mapped;
  457. END PhysicalRegisters64;
  458. CodeGeneratorAMD64 = OBJECT (CodeGenerators.GeneratorWithTickets)
  459. VAR
  460. (* static generator state variables, considered constant during generation *)
  461. runtimeModuleName: SyntaxTree.IdentifierString;
  462. cpuBits: LONGINT;
  463. opBP, opSP, opRA, opRB, opRC, opRD, opRSI, opRDI, opR8, opR9, opR10, opR11, opR12, opR13, opR14, opR15: Assembler.Operand; (* base pointer, stack pointer, register A, depends on cpuBits*)
  464. BP, SP, RA, RD, RS, RC: LONGINT; (* base pointer and stack pointer register index, depends on cpuBits *)
  465. emitter: Assembler.Emitter; (* assembler generating and containing the machine code *)
  466. backend: BackendAMD64;
  467. (* register spill state *)
  468. stackSize: LONGINT;
  469. spillStackStart: LONGINT;
  470. (* floating point stack state *)
  471. fpStackPointer: LONGINT; (* floating point stack pointer, increases with allocation, decreases with releasing, used to determine current relative position on stack (as is necessary for intel FP instructions) *)
  472. (*
  473. FP register usage scheme:
  474. sp=1> FP0 - temp
  475. sp=0> FP0 - reg0 FP1 - reg0 sp=0> FP0 - reg0
  476. FP1 - reg1 FP2 - reg1 FP1 - reg1
  477. FP2 - reg2 FP3 - reg2 FP2 - reg2
  478. FP3 - reg3 = load op1 => FP4 - reg3 = op => FP3 - reg3
  479. FP4 - reg4 FP5 - reg4 FP4 - reg4
  480. FP5 - reg5 FP6 - reg5 FP5 - reg5
  481. FP6 - reg6 FP7 - reg6 FP6 - reg6
  482. FP7 - reg7 (reg7 lost) FP7 - reg7
  483. *)
  484. ap: Ticket;
  485. (* -------------------------- constructor -------------------------------*)
  486. PROCEDURE &InitGeneratorAMD64(CONST runtime: SyntaxTree.IdentifierString; diagnostics: Diagnostics.Diagnostics; backend: BackendAMD64);
  487. VAR physicalRegisters: PhysicalRegisters; physicalRegisters32: PhysicalRegisters32; physicalRegisters64: PhysicalRegisters64;
  488. BEGIN
  489. SELF.backend := backend;
  490. runtimeModuleName := runtime;
  491. SELF.cpuBits := backend.bits;
  492. NEW(emitter,diagnostics);
  493. IF cpuBits=32 THEN
  494. NEW(physicalRegisters32, backend.forceFPU, backend.cooperative); physicalRegisters := physicalRegisters32; error := ~emitter.SetBits(32);
  495. opBP := opEBP; opSP := opESP; opRA := opEAX; opRB := opEBX; opRD := opEDX; opRDI := opEDI; opRSI := opESI; opRC := opECX;
  496. SP := ESP; BP := EBP; RA := EAX;
  497. RD := EDI; RS := ESI; RC := ECX;
  498. ASSERT(~error);
  499. ELSIF cpuBits=64 THEN
  500. NEW(physicalRegisters64, backend.forceFPU, backend.cooperative); physicalRegisters := physicalRegisters64; error := ~emitter.SetBits(64);
  501. opBP := opRBP; opSP := opRSP;
  502. opRA := registerOperands[RAX]; opRC := registerOperands[RCX];
  503. opRB := registerOperands[RBX]; opRD := registerOperands[RDX];
  504. opRDI := registerOperands[RDI]; opRSI := registerOperands[RSI];
  505. opR8 := registerOperands[R8]; opR9 := registerOperands[R9];
  506. opR10 := registerOperands[R10]; opR11 := registerOperands[R11];
  507. opR12 := registerOperands[R12]; opR13 := registerOperands[R13];
  508. opR14 := registerOperands[R14]; opR15 := registerOperands[R15];
  509. SP := RSP; BP := RBP; RA := RAX;
  510. RD := RDI; RS := RSI; RC := RCX;
  511. ASSERT(~error);
  512. ELSE Halt("no register allocator for bits other than 32 / 64 ");
  513. END;
  514. fpStackPointer := 0;
  515. InitTicketGenerator(diagnostics,backend.optimize,2,physicalRegisters);
  516. END InitGeneratorAMD64;
  517. (*------------------- overwritten methods ----------------------*)
  518. PROCEDURE Section*(in: IntermediateCode.Section; out: BinaryCode.Section);
  519. VAR oldSpillStackSize: LONGINT;
  520. PROCEDURE CheckEmptySpillStack;
  521. BEGIN
  522. IF spillStack.Size()#0 THEN Error(Basic.invalidPosition,"implementation error, spill stack not cleared") END;
  523. END CheckEmptySpillStack;
  524. BEGIN
  525. spillStack.Init;
  526. IF backend.cooperative THEN
  527. ap := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,IntermediateCode.UnsignedIntegerType(cpuBits),RC,in.pc);
  528. ap.spillable := FALSE;
  529. END;
  530. emitter.SetCode(out);
  531. Section^(in,out);
  532. IF FrameSpillStack & (spillStack.MaxSize() >0) THEN
  533. oldSpillStackSize := spillStack.MaxSize();
  534. out.Reset;
  535. CheckEmptySpillStack;
  536. Section^(in,out);
  537. ASSERT(spillStack.MaxSize() = oldSpillStackSize);
  538. END;
  539. ASSERT(fpStackPointer = 0);
  540. CheckEmptySpillStack;
  541. IF backend.cooperative THEN
  542. UnmapTicket(ap);
  543. END;
  544. error := error OR emitter.error;
  545. END Section;
  546. PROCEDURE Supported*(CONST instruction: IntermediateCode.Instruction; VAR moduleName, procedureName: ARRAY OF CHAR): BOOLEAN;
  547. BEGIN
  548. COPY(runtimeModuleName, moduleName);
  549. IF (cpuBits=32) & (instruction.op2.type.sizeInBits = IntermediateCode.Bits64) & (instruction.op2.type.form IN IntermediateCode.Integer) THEN
  550. CASE instruction.opcode OF
  551. IntermediateCode.div:
  552. procedureName := "DivH"; RETURN FALSE
  553. | IntermediateCode.mul:
  554. procedureName := "MulH"; RETURN FALSE
  555. | IntermediateCode.mod :
  556. procedureName := "ModH"; RETURN FALSE
  557. | IntermediateCode.abs :
  558. procedureName := "AbsH"; RETURN FALSE;
  559. | IntermediateCode.shl :
  560. IF instruction.op1.type.form = IntermediateCode.SignedInteger THEN
  561. procedureName := "AslH"; RETURN FALSE;
  562. ELSE
  563. procedureName := "LslH"; RETURN FALSE;
  564. END;
  565. | IntermediateCode.shr :
  566. IF instruction.op1.type.form = IntermediateCode.SignedInteger THEN
  567. procedureName := "AsrH"; RETURN FALSE;
  568. ELSE
  569. procedureName := "LsrH"; RETURN FALSE;
  570. END;
  571. | IntermediateCode.ror :
  572. procedureName := "RorH"; RETURN FALSE;
  573. | IntermediateCode.rol :
  574. procedureName := "RolH"; RETURN FALSE;
  575. | IntermediateCode.cas :
  576. procedureName := "CasH"; RETURN FALSE;
  577. ELSE RETURN TRUE
  578. END;
  579. ELSIF ~backend.forceFPU & (instruction.opcode = IntermediateCode.conv) & (instruction.op1.type.form IN IntermediateCode.Integer) & (instruction.op2.type.form = IntermediateCode.Float) & IsComplex(instruction.op1) THEN
  580. IF instruction.op2.type.sizeInBits=32 THEN
  581. procedureName := "EntierRH"
  582. ELSE
  583. procedureName := "EntierXH"
  584. END;
  585. RETURN FALSE
  586. END;
  587. RETURN TRUE
  588. END Supported;
  589. (* input: type (such as that of an intermediate operand), output: low and high type (such as in low and high type of an operand) *)
  590. PROCEDURE GetPartType*(CONST type: IntermediateCode.Type; part: LONGINT; VAR typePart: IntermediateCode.Type);
  591. BEGIN
  592. ASSERT(type.sizeInBits >0);
  593. IF (type.sizeInBits > cpuBits) & (type.form IN IntermediateCode.Integer) THEN
  594. IntermediateCode.InitType(typePart,type.form,32);
  595. ELSE ASSERT((type.form IN IntermediateCode.Integer) OR (type.form = IntermediateCode.Float));
  596. IF part=Low THEN typePart := type ELSE typePart := IntermediateCode.undef END;
  597. END;
  598. END GetPartType;
  599. (* simple move without conversion *)
  600. PROCEDURE Move(VAR dest, src: Assembler.Operand; CONST type: IntermediateCode.Type);
  601. BEGIN
  602. IF type.length > 1 THEN
  603. IF type.length = 4 THEN
  604. (*ASSERT(type.form = IntermediateCode.Float);*)
  605. IF (*(type.form = IntermediateCode.Float) & *) (type.sizeInBits = 32) THEN
  606. SpecialMove(InstructionSet.opMOVUPS, InstructionSet.opMOVUPS, TRUE, dest, src, type);
  607. ELSIF (type.sizeInBits = 16) THEN
  608. SpecialMove(InstructionSet.opMOVQ, InstructionSet.opMOVQ, TRUE, dest, src, type);
  609. ELSIF (type.sizeInBits = 8) THEN
  610. SpecialMove(InstructionSet.opMOVD, InstructionSet.opMOVD, TRUE, dest, src, type);
  611. END;
  612. ELSIF type.length = 8 THEN
  613. (*ASSERT(type.form = IntermediateCode.Float);*)
  614. IF (*(type.form = IntermediateCode.Float) & *) (type.sizeInBits = 32) THEN
  615. SpecialMove(InstructionSet.opVMOVUPS, InstructionSet.opVMOVUPS, TRUE, dest, src, type);
  616. ELSIF (type.sizeInBits = 16) THEN
  617. SpecialMove(InstructionSet.opVMOVQ, InstructionSet.opVMOVQ, TRUE, dest, src, type);
  618. ELSIF (type.sizeInBits = 8) THEN
  619. SpecialMove(InstructionSet.opVMOVD, InstructionSet.opVMOVD, TRUE, dest, src, type);
  620. END;
  621. ELSE
  622. (*
  623. ASSERT(type.form = IntermediateCode.Float);
  624. *)
  625. ASSERT(type.sizeInBits = 64);
  626. SpecialMove(InstructionSet.opMOVUPD, InstructionSet.opMOVUPS, TRUE, dest, src, type);
  627. END;
  628. ELSIF type.form = IntermediateCode.Float THEN
  629. IF type.sizeInBits = 32 THEN
  630. SpecialMove(InstructionSet.opMOVSS, InstructionSet.opMOVSS, TRUE, dest, src, type);
  631. ELSE
  632. SpecialMove(InstructionSet.opMOVSD, InstructionSet.opMOVSD, TRUE, dest, src, type);
  633. END;
  634. ELSE
  635. SpecialMove(InstructionSet.opMOV, InstructionSet.opMOV, TRUE, dest, src, type);
  636. END;
  637. END Move;
  638. PROCEDURE ToSpillStack*(ticket: Ticket);
  639. VAR op: Assembler.Operand;
  640. BEGIN
  641. IF (ticket.type.form = IntermediateCode.Float) & backend.forceFPU THEN
  642. emitter.Emit1(InstructionSet.opFLD,registerOperands[ticket.register]);
  643. INC(fpStackPointer);
  644. GetSpillOperand(ticket,op);
  645. emitter.Emit1(InstructionSet.opFSTP,op);
  646. DEC(fpStackPointer);
  647. ELSE
  648. GetSpillOperand(ticket,op);
  649. Move(op, registerOperands[ticket.register], ticket.type)
  650. END;
  651. END ToSpillStack;
  652. PROCEDURE AllocateSpillStack*(size: LONGINT);
  653. BEGIN
  654. IF ~FrameSpillStack THEN
  655. AllocateStack(cpuBits DIV 8*size)
  656. END;
  657. END AllocateSpillStack;
  658. PROCEDURE ToRegister*(ticket: Ticket);
  659. VAR op: Assembler.Operand;
  660. BEGIN
  661. GetSpillOperand(ticket,op);
  662. emitter.Emit2(InstructionSet.opMOV,registerOperands[ticket.register],op);
  663. END ToRegister;
  664. PROCEDURE ExchangeTickets*(ticket1,ticket2: Ticket);
  665. VAR op1,op2: Assembler.Operand;
  666. BEGIN
  667. TicketToOperand(ticket1, op1);
  668. TicketToOperand(ticket2, op2);
  669. emitter.Emit2(InstructionSet.opXCHG, op1,op2);
  670. END ExchangeTickets;
  671. (*------------------- particular register mappings / operands ----------------------*)
  672. (* returns if a virtual register is mapped to the register set described by virtualRegisterMapping*)
  673. PROCEDURE MappedTo(CONST virtualRegister: LONGINT; part:LONGINT; physicalRegister: LONGINT): BOOLEAN;
  674. VAR ticket: Ticket;
  675. BEGIN
  676. IF (virtualRegister > 0) THEN
  677. ticket := virtualRegisters.Mapped(virtualRegister,part);
  678. RETURN (ticket # NIL) & ~(ticket.spilled) & (ticket.register = physicalRegister)
  679. ELSIF (virtualRegister = IntermediateCode.FP) THEN
  680. RETURN physicalRegister= BP
  681. ELSIF (virtualRegister = IntermediateCode.SP) THEN
  682. RETURN physicalRegister = SP
  683. ELSIF (virtualRegister = IntermediateCode.AP) THEN
  684. ASSERT(backend.cooperative);
  685. RETURN ~(ap.spilled) & (ap.register = physicalRegister)
  686. ELSE
  687. RETURN FALSE
  688. END;
  689. END MappedTo;
  690. PROCEDURE ResultRegister(CONST type: IntermediateCode.Type; part: LONGINT): LONGINT;
  691. BEGIN
  692. IF type.form IN IntermediateCode.Integer THEN
  693. CASE type.sizeInBits OF
  694. | 64:
  695. IF cpuBits = 32 THEN
  696. IF part = Low THEN RETURN EAX
  697. ELSE RETURN EDX
  698. END;
  699. ELSE
  700. ASSERT(part = Low);
  701. RETURN RAX
  702. END;
  703. | 32: ASSERT(part=Low); RETURN EAX
  704. | 16: ASSERT(part=Low); RETURN AX
  705. | 8: ASSERT(part=Low); RETURN AL
  706. END;
  707. ELSIF ~backend.forceFPU THEN
  708. RETURN XMM0
  709. ELSE ASSERT(type.form = IntermediateCode.Float);ASSERT(part=Low);
  710. RETURN ST0
  711. END;
  712. END ResultRegister;
  713. (*------------------- operand reflection ----------------------*)
  714. PROCEDURE IsMemoryOperand(vop: IntermediateCode.Operand; part: LONGINT): BOOLEAN;
  715. VAR ticket: Ticket;
  716. BEGIN
  717. IF vop.mode = IntermediateCode.ModeMemory THEN RETURN TRUE
  718. ELSIF vop.mode = IntermediateCode.ModeRegister THEN
  719. ticket := virtualRegisters.Mapped(vop.register,part);
  720. RETURN (ticket # NIL) & (ticket.spilled);
  721. ELSE RETURN FALSE
  722. END;
  723. END IsMemoryOperand;
  724. PROCEDURE IsRegister(CONST vop: IntermediateCode.Operand): BOOLEAN;
  725. BEGIN
  726. RETURN (vop.mode = IntermediateCode.ModeRegister) & (vop.offset = 0)
  727. END IsRegister;
  728. (* infer intermediate code type from physical operand as far as possible *)
  729. PROCEDURE PhysicalOperandType(CONST op:Assembler.Operand): IntermediateCode.Type;
  730. VAR type:IntermediateCode.Type;
  731. BEGIN
  732. IF op.type = Assembler.sti THEN
  733. IntermediateCode.InitType(type, IntermediateCode.Float, op.sizeInBytes*8)
  734. ELSE
  735. IntermediateCode.InitType(type, IntermediateCode.SignedInteger, op.sizeInBytes*8)
  736. END;
  737. RETURN type
  738. END PhysicalOperandType;
  739. (*------------------- operand generation ----------------------*)
  740. PROCEDURE GetSpillOperand(ticket: Ticket; VAR op: Assembler.Operand);
  741. BEGIN
  742. IF FrameSpillStack THEN
  743. op := Assembler.NewMem(SHORTINT(ticket.type.sizeInBits*ticket.type.length DIV 8), BP , -(spillStackStart + cpuBits DIV 8 + ticket.offset*cpuBits DIV 8));
  744. ELSE
  745. op := Assembler.NewMem(SHORTINT(ticket.type.sizeInBits*ticket.type.length DIV 8),SP , (spillStack.Size()-ticket.offset)*cpuBits DIV 8);
  746. END;
  747. END GetSpillOperand;
  748. PROCEDURE TicketToOperand(ticket: Ticket; VAR op: Assembler.Operand);
  749. BEGIN
  750. IF (ticket = NIL) THEN
  751. Assembler.InitOperand(op)
  752. ELSIF ticket.spilled THEN
  753. GetSpillOperand(ticket,op)
  754. ELSE
  755. IF ticket.register = none THEN physicalRegisters.Dump(D.Log); tickets.Dump(D.Log); virtualRegisters.Dump(D.Log); D.Update; END;
  756. ASSERT(ticket.register # none);
  757. IF (ticket.type.form = IntermediateCode.Float) & backend.forceFPU THEN
  758. op := registerOperands[ticket.register+fpStackPointer]
  759. ELSE
  760. op := registerOperands[ticket.register];
  761. END;
  762. END;
  763. END TicketToOperand;
  764. PROCEDURE GetTemporaryRegister(type: IntermediateCode.Type; VAR op: Assembler.Operand);
  765. BEGIN
  766. TicketToOperand(TemporaryTicket(IntermediateCode.GeneralPurposeRegister,type),op)
  767. END GetTemporaryRegister;
  768. PROCEDURE GetImmediateMem(CONST vop: IntermediateCode.Operand; part: LONGINT; VAR imm: Assembler.Operand);
  769. VAR data: IntermediateCode.Section;pc: LONGINT; source, dest: Assembler.Operand; ticket: Ticket;
  770. BEGIN
  771. data := GetDataSection();
  772. pc := IntermediateBackend.EnterImmediate(data,vop);
  773. IF cpuBits = 64 THEN
  774. Assembler.InitImm(source,8,0);
  775. Assembler.SetSymbol(source,data.name,0,pc,0);
  776. ticket := TemporaryTicket(IntermediateCode.GeneralPurposeRegister,IntermediateBackend.GetType(module.system,module.system.addressType));
  777. TicketToOperand(ticket,dest);
  778. emitter.Emit2(InstructionSet.opMOV,dest,source);
  779. Assembler.InitMem(imm, SHORT(vop.type.sizeInBits DIV 8), ticket.register, 0);
  780. ELSE
  781. Assembler.InitMem(imm, SHORT(vop.type.sizeInBits DIV 8) , Assembler.none,0);
  782. Assembler.SetSymbol(imm,data.name,0,pc,0);
  783. END;
  784. END GetImmediateMem;
  785. PROCEDURE GetImmediate(CONST virtual: IntermediateCode.Operand; part: LONGINT; VAR physical: Assembler.Operand; forbidden16Bit: BOOLEAN);
  786. VAR type: IntermediateCode.Type; temp: Assembler.Operand; size: SHORTINT; value: HUGEINT;
  787. PROCEDURE IsImm8(value: HUGEINT): BOOLEAN;
  788. BEGIN
  789. RETURN (value >= -80H) & (value < 80H)
  790. END IsImm8;
  791. PROCEDURE IsImm16(value: HUGEINT): BOOLEAN;
  792. BEGIN
  793. RETURN (value >= -8000H) & (value < 10000H)
  794. END IsImm16;
  795. PROCEDURE IsImm32(value: HUGEINT): BOOLEAN;
  796. BEGIN
  797. value := value DIV 10000H DIV 10000H;
  798. RETURN (value = 0) OR (value=-1);
  799. END IsImm32;
  800. BEGIN
  801. ASSERT(virtual.mode = IntermediateCode.ModeImmediate);
  802. GetPartType(virtual.type,part,type);
  803. IF virtual.type.form IN IntermediateCode.Integer THEN
  804. IF IsComplex(virtual) THEN
  805. IF part = High THEN value := SHORT(virtual.intValue DIV 10000H DIV 10000H)
  806. ELSE value := virtual.intValue
  807. END;
  808. ELSE value := virtual.intValue
  809. END;
  810. IF virtual.symbol.name # "" THEN size := SHORT(type.sizeInBits DIV 8);
  811. ELSIF forbidden16Bit & IsImm16(value) & ~(IsImm8(value)) THEN size := Assembler.bits32;
  812. ELSIF (type.sizeInBits = 64) & (type.form = IntermediateCode.UnsignedInteger) & (value > MAX(LONGINT)) THEN
  813. size := 8; (* don't use negative signed 32-bit value to encode 64-bit unsigned value! *)
  814. ELSE size := 0
  815. END;
  816. Assembler.InitImm(physical,size ,value);
  817. IF virtual.symbol.name # "" THEN Assembler.SetSymbol(physical,virtual.symbol.name,virtual.symbol.fingerprint,virtual.symbolOffset,virtual.offset+part*Assembler.bits32) END;
  818. IF (cpuBits=64) & ((physical.sizeInBytes=8) OR ~IsImm32(value)) THEN
  819. ASSERT(cpuBits=64);
  820. GetTemporaryRegister(IntermediateCode.int64,temp);
  821. emitter.Emit2(InstructionSet.opMOV,temp,physical);
  822. physical := temp;
  823. END;
  824. ELSE
  825. GetImmediateMem(virtual,part,physical);
  826. END;
  827. END GetImmediate;
  828. PROCEDURE GetMemory(CONST virtual: IntermediateCode.Operand; part: LONGINT; VAR physical: Assembler.Operand);
  829. VAR type: IntermediateCode.Type; virtualRegister, physicalRegister,offset: LONGINT; ticket,orig: Ticket; dest, source: Assembler.Operand;
  830. BEGIN
  831. ASSERT(virtual.mode = IntermediateCode.ModeMemory);
  832. GetPartType(virtual.type,part,type);
  833. IF virtual.register # IntermediateCode.None THEN
  834. virtualRegister := virtual.register;
  835. IF virtualRegister = IntermediateCode.FP THEN physicalRegister := BP;
  836. ELSIF virtualRegister = IntermediateCode.SP THEN physicalRegister := SP;
  837. ELSE
  838. IF virtualRegister = IntermediateCode.AP THEN
  839. ticket := ap;
  840. ELSE
  841. ticket := virtualRegisters.Mapped(virtualRegister,Low);
  842. END;
  843. IF ticket.spilled THEN
  844. IF physicalRegisters.Reserved(ticket.register) THEN
  845. orig := ticket;
  846. ticket := TemporaryTicket(IntermediateCode.GeneralPurposeRegister,IntermediateBackend.GetType(module.system,module.system.addressType));
  847. TicketToOperand(orig,source);
  848. TicketToOperand(ticket,dest);
  849. Move(dest,source,PhysicalOperandType(dest));
  850. physicalRegister := ticket.register;
  851. ELSE
  852. UnSpill(ticket);
  853. physicalRegister := ticket.register;
  854. END;
  855. ELSE
  856. physicalRegister := ticket.register;
  857. END;
  858. END;
  859. offset := virtual.offset;
  860. ASSERT(virtual.intValue = 0);
  861. ELSIF virtual.symbol.name = "" THEN
  862. physicalRegister := Assembler.none;
  863. offset := SHORT(virtual.intValue);
  864. ASSERT(virtual.offset = 0);
  865. ELSIF cpuBits = 64 THEN
  866. Assembler.InitImm(source,8,0);
  867. Assembler.SetSymbol(source,virtual.symbol.name,virtual.symbol.fingerprint,virtual.symbolOffset,virtual.offset);
  868. ticket := TemporaryTicket(IntermediateCode.GeneralPurposeRegister,IntermediateBackend.GetType(module.system,module.system.addressType));
  869. TicketToOperand(ticket,dest);
  870. emitter.Emit2(InstructionSet.opMOV,dest,source);
  871. physicalRegister := ticket.register;
  872. offset := 0;
  873. ASSERT(virtual.intValue = 0);
  874. ELSE
  875. physicalRegister := Assembler.none;
  876. offset := virtual.offset;
  877. ASSERT(virtual.intValue = 0);
  878. END;
  879. Assembler.InitMem(physical, SHORTINT(type.length * type.sizeInBits DIV 8) , physicalRegister, offset+4*part);
  880. IF (virtual.symbol.name # "") & (cpuBits # 64) THEN
  881. Assembler.SetSymbol(physical,virtual.symbol.name,virtual.symbol.fingerprint,virtual.symbolOffset,virtual.offset+4*part);
  882. END;
  883. END GetMemory;
  884. PROCEDURE GetRegister(CONST virtual: IntermediateCode.Operand; part:LONGINT; VAR physical: Assembler.Operand; VAR ticket: Ticket);
  885. VAR type: IntermediateCode.Type; virtualRegister, tempReg: LONGINT;
  886. tmp,imm: Assembler.Operand; index: LONGINT;
  887. BEGIN
  888. ASSERT(virtual.mode = IntermediateCode.ModeRegister);
  889. GetPartType(virtual.type,part,type);
  890. virtualRegister := virtual.register;
  891. IF (virtual.register > 0) THEN
  892. TicketToOperand(virtualRegisters.Mapped(virtual.register,part), physical);
  893. ELSIF virtual.register = IntermediateCode.FP THEN
  894. Assert(part=Low,"forbidden partitioned register on BP");
  895. physical := opBP;
  896. ELSIF virtual.register = IntermediateCode.SP THEN
  897. Assert(part=Low,"forbidden partitioned register on SP");
  898. physical := opSP;
  899. ELSIF virtual.register = IntermediateCode.AP THEN
  900. ASSERT(backend.cooperative);
  901. Assert(part=Low,"forbidden partitioned register on AP");
  902. TicketToOperand(ap, physical);
  903. ELSE HALT(100);
  904. END;
  905. IF virtual.offset # 0 THEN
  906. Assert(type.form # IntermediateCode.Float,"forbidden offset on float");
  907. IF ticket = NIL THEN
  908. tempReg := ForceFreeRegister(type);
  909. TicketToOperand(ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,type,tempReg,inPC),tmp);
  910. ELSE
  911. TicketToOperand(ticket, tmp);
  912. ticket := NIL;
  913. END;
  914. IF Assembler.IsRegisterOperand(physical) & (type.sizeInBits > 8) THEN
  915. Assembler.InitMem(physical,SHORTINT(type.length * type.sizeInBits DIV 8) , physical.register, virtual.offset);
  916. emitter.Emit2(InstructionSet.opLEA, tmp,physical);
  917. ELSE
  918. emitter.Emit2(InstructionSet.opMOV,tmp,physical);
  919. Assembler.InitImm(imm,0 ,virtual.offset);
  920. emitter.Emit2(InstructionSet.opADD,tmp,imm);
  921. END;
  922. physical := tmp;
  923. END;
  924. END GetRegister;
  925. (* make physical operand from virtual operand, if ticket given then write result into phyiscal register represented by ticket *)
  926. PROCEDURE MakeOperand(CONST vop: IntermediateCode.Operand; part: LONGINT; VAR op: Assembler.Operand; ticket: Ticket);
  927. VAR tmp: Assembler.Operand;
  928. BEGIN
  929. TryAllocate(vop,part);
  930. CASE vop.mode OF
  931. IntermediateCode.ModeMemory: GetMemory(vop,part,op);
  932. |IntermediateCode.ModeRegister: GetRegister(vop,part,op,ticket);
  933. |IntermediateCode.ModeImmediate: GetImmediate(vop,part,op,FALSE);
  934. END;
  935. IF ticket # NIL THEN
  936. TicketToOperand(ticket, tmp);
  937. emitter.Emit2(InstructionSet.opMOV, tmp, op);
  938. (* should work but does not
  939. IF Assembler.IsRegisterOperand(op) THEN ReleaseHint(op.register) END;
  940. *)
  941. op := tmp;
  942. END;
  943. END MakeOperand;
  944. (* make physical register operand from virtual operand *)
  945. PROCEDURE MakeRegister(CONST vop: IntermediateCode.Operand; part: LONGINT; VAR op: Assembler.Operand);
  946. VAR previous: Assembler.Operand; temp: Ticket;
  947. BEGIN
  948. MakeOperand(vop,part,op,NIL);
  949. IF ~Assembler.IsRegisterOperand(op) THEN
  950. previous := op;
  951. temp := TemporaryTicket(vop.registerClass,vop.type);
  952. TicketToOperand(temp,op);
  953. Move(op, previous, vop.type);
  954. END;
  955. END MakeRegister;
  956. (*------------------- helpers for code generation ----------------------*)
  957. (* move, potentially with conversion. parameter back used for moving back from temporary operand*)
  958. PROCEDURE SpecialMove(op, back: LONGINT; canStoreToMemory: BOOLEAN; VAR dest,src: Assembler.Operand; type: IntermediateCode.Type);
  959. VAR temp: Assembler.Operand; ticket: Ticket;
  960. BEGIN
  961. IF Assembler.SameOperand(src,dest) THEN (* do nothing *)
  962. ELSIF ~Assembler.IsMemoryOperand(dest) OR (~Assembler.IsMemoryOperand(src) & canStoreToMemory) THEN
  963. emitter.Emit2(op,dest,src);
  964. ELSE
  965. ticket := TemporaryTicket(IntermediateCode.GeneralPurposeRegister,type);
  966. TicketToOperand(ticket,temp);
  967. emitter.Emit2(op,temp,src);
  968. emitter.Emit2(back,dest,temp);
  969. UnmapTicket(ticket);
  970. END;
  971. END SpecialMove;
  972. PROCEDURE AllocateStack(sizeInBytes: LONGINT);
  973. VAR sizeOp: Assembler.Operand; opcode: LONGINT;
  974. BEGIN
  975. ASSERT(sizeInBytes MOD 4 (* (cpuBits DIV 8) *) = 0);
  976. IF sizeInBytes < 0 THEN
  977. sizeInBytes := -sizeInBytes; opcode := InstructionSet.opADD;
  978. ELSIF sizeInBytes > 0 THEN
  979. opcode := InstructionSet.opSUB;
  980. ELSE RETURN
  981. END;
  982. IF sizeInBytes < 128 THEN sizeOp := Assembler.NewImm8(sizeInBytes);
  983. ELSE sizeOp := Assembler.NewImm32(sizeInBytes);
  984. END;
  985. emitter.Emit2(opcode,opSP,sizeOp);
  986. END AllocateStack;
  987. (*------------------- generation = emit dispatch / emit procedures ----------------------*)
  988. PROCEDURE IsFloat(CONST operand: IntermediateCode.Operand): BOOLEAN;
  989. BEGIN RETURN operand.type.form = IntermediateCode.Float
  990. END IsFloat;
  991. PROCEDURE IsComplex(CONST operand: IntermediateCode.Operand): BOOLEAN;
  992. BEGIN RETURN (operand.type.form IN IntermediateCode.Integer) & (operand.type.sizeInBits > cpuBits)
  993. END IsComplex;
  994. PROCEDURE Generate*(VAR instruction: IntermediateCode.Instruction);
  995. VAR opcode: SHORTINT; ticket: Ticket; hwreg, lastUse, i, part: LONGINT;
  996. BEGIN
  997. (*!IF ((instruction.opcode = IntermediateCode.mov) OR (instruction.opcode = IntermediateCode.pop)) & (instruction.op1.register <= IntermediateCode.ParameterRegister) THEN
  998. hwreg := ParameterRegister(IntermediateCode.ParameterRegister-instruction.op1.register, instruction.op1.type);
  999. Spill(physicalRegisters.Mapped(hwreg));
  1000. lastUse := inPC+1;
  1001. WHILE (lastUse < in.pc) &
  1002. ((in.instructions[lastUse].opcode # IntermediateCode.push) OR (in.instructions[lastUse].op1.register # instruction.op1.register)) & (in.instructions[lastUse].opcode # IntermediateCode.call) DO
  1003. INC(lastUse)
  1004. END;
  1005. ticket := ReservePhysicalRegister(instruction.op1.type,hwreg,lastUse);
  1006. END;
  1007. *)
  1008. ReserveOperandRegisters(instruction.op1,TRUE); ReserveOperandRegisters(instruction.op2,TRUE);ReserveOperandRegisters(instruction.op3,TRUE);
  1009. (*TryAllocate(instruction.op1,Low);
  1010. IF IsComplex(instruction.op1) THEN TryAllocate(instruction.op1,High) END;
  1011. *)
  1012. opcode := instruction.opcode;
  1013. CASE opcode OF
  1014. IntermediateCode.nop: (* do nothing *)
  1015. |IntermediateCode.mov:
  1016. IF IsFloat(instruction.op1) OR IsFloat(instruction.op2) THEN
  1017. EmitMovFloat(instruction.op1,instruction.op2)
  1018. ELSE EmitMov(instruction.op1,instruction.op2,Low);
  1019. IF IsComplex(instruction.op1) THEN EmitMov(instruction.op1,instruction.op2, High) END;
  1020. END;
  1021. |IntermediateCode.conv:
  1022. IF IsFloat(instruction.op1) OR IsFloat(instruction.op2) THEN
  1023. EmitConvertFloat(instruction)
  1024. ELSE
  1025. EmitConvert(instruction.op1,instruction.op2,Low);
  1026. IF IsComplex(instruction.op1) THEN EmitConvert(instruction.op1,instruction.op2,High) END;
  1027. END;
  1028. |IntermediateCode.call: EmitCall(instruction);
  1029. |IntermediateCode.enter: EmitEnter(instruction);
  1030. |IntermediateCode.leave: EmitLeave(instruction);
  1031. |IntermediateCode.exit: EmitExit(instruction);
  1032. |IntermediateCode.result:
  1033. IF IsFloat(instruction.op1) & backend.forceFPU THEN
  1034. EmitResultFPU(instruction)
  1035. ELSE
  1036. EmitResult(instruction);
  1037. END;
  1038. |IntermediateCode.return:
  1039. IF IsFloat(instruction.op1) & backend.forceFPU THEN
  1040. EmitReturnFPU(instruction)
  1041. ELSE
  1042. EmitReturn(instruction,Low);
  1043. IF IsComplex(instruction.op1) THEN EmitReturn(instruction, High) END;
  1044. END;
  1045. |IntermediateCode.trap: EmitTrap(instruction);
  1046. |IntermediateCode.br .. IntermediateCode.brlt: EmitBr(instruction)
  1047. |IntermediateCode.pop:
  1048. IF IsFloat(instruction.op1) THEN
  1049. EmitPopFloat(instruction.op1)
  1050. ELSE
  1051. EmitPop(instruction.op1,Low);
  1052. IF IsComplex(instruction.op1) THEN
  1053. EmitPop(instruction.op1,High)
  1054. END;
  1055. END;
  1056. |IntermediateCode.push:
  1057. IF IsFloat(instruction.op1) THEN
  1058. EmitPushFloat(instruction.op1)
  1059. ELSE
  1060. IF IsComplex(instruction.op1) THEN
  1061. EmitPush(instruction.op1,High);
  1062. END;
  1063. EmitPush(instruction.op1,Low)
  1064. END;
  1065. |IntermediateCode.neg:
  1066. IF IsFloat(instruction.op1) THEN
  1067. IF backend.forceFPU THEN
  1068. EmitArithmetic2FPU(instruction,InstructionSet.opFCHS)
  1069. ELSE
  1070. EmitNegXMM(instruction)
  1071. END;
  1072. ELSE EmitNeg(instruction);
  1073. END;
  1074. |IntermediateCode.not:
  1075. Assert(~IsFloat(instruction.op1),"instruction not supported for float");
  1076. EmitArithmetic2(instruction,Low,InstructionSet.opNOT);
  1077. IF IsComplex(instruction.op1) THEN EmitArithmetic2(instruction, High, InstructionSet.opNOT) END;
  1078. |IntermediateCode.abs:
  1079. IF IsFloat(instruction.op1) THEN
  1080. IF backend.forceFPU THEN
  1081. EmitArithmetic2FPU(instruction,InstructionSet.opFABS)
  1082. ELSE
  1083. EmitAbsXMM(instruction)
  1084. END;
  1085. ELSE EmitAbs(instruction);
  1086. END;
  1087. |IntermediateCode.mul:
  1088. IF IsFloat(instruction.op1) THEN
  1089. IF backend.forceFPU THEN
  1090. EmitArithmetic3FPU(instruction,InstructionSet.opFMUL)
  1091. ELSE
  1092. EmitArithmetic3XMM(instruction, InstructionSet.opMULSS, InstructionSet.opMULSD)
  1093. END;
  1094. ELSE
  1095. EmitMul(instruction);
  1096. END;
  1097. |IntermediateCode.div:
  1098. IF IsFloat(instruction.op1 )THEN
  1099. IF backend.forceFPU THEN
  1100. EmitArithmetic3FPU(instruction,InstructionSet.opFDIV)
  1101. ELSE
  1102. EmitArithmetic3XMM(instruction, InstructionSet.opDIVSS, InstructionSet.opDIVSD)
  1103. END;
  1104. ELSE
  1105. EmitDivMod(instruction);
  1106. END;
  1107. |IntermediateCode.mod:
  1108. Assert(~IsFloat(instruction.op1),"instruction not supported for float");
  1109. EmitDivMod(instruction);
  1110. |IntermediateCode.sub:
  1111. IF IsFloat(instruction.op1) THEN
  1112. IF backend.forceFPU THEN
  1113. EmitArithmetic3FPU(instruction,InstructionSet.opFSUB)
  1114. ELSE
  1115. EmitArithmetic3XMM(instruction, InstructionSet.opSUBSS, InstructionSet.opSUBSD)
  1116. END;
  1117. ELSE EmitArithmetic3Part(instruction,Low,InstructionSet.opSUB);
  1118. IF IsComplex(instruction.op1) THEN EmitArithmetic3Part(instruction, High, InstructionSet.opSBB) END;
  1119. END;
  1120. |IntermediateCode.add:
  1121. IF IsFloat(instruction.op1) THEN
  1122. IF backend.forceFPU THEN
  1123. EmitArithmetic3FPU(instruction,InstructionSet.opFADD)
  1124. ELSE
  1125. EmitArithmetic3XMM(instruction, InstructionSet.opADDSS, InstructionSet.opADDSD)
  1126. END;
  1127. ELSE EmitArithmetic3Part(instruction,Low,InstructionSet.opADD);
  1128. IF IsComplex(instruction.op1) THEN EmitArithmetic3Part(instruction, High, InstructionSet.opADC) END;
  1129. END;
  1130. |IntermediateCode.and:
  1131. Assert(~IsFloat(instruction.op1),"operation not defined on float");
  1132. EmitArithmetic3(instruction,InstructionSet.opAND);
  1133. |IntermediateCode.or:
  1134. Assert(~IsFloat(instruction.op1),"operation not defined on float");
  1135. EmitArithmetic3(instruction,InstructionSet.opOR);
  1136. |IntermediateCode.xor:
  1137. Assert(~IsFloat(instruction.op1),"operation not defined on float");
  1138. EmitArithmetic3(instruction,InstructionSet.opXOR);
  1139. |IntermediateCode.shl: EmitShift(instruction);
  1140. |IntermediateCode.shr: EmitShift(instruction);
  1141. |IntermediateCode.rol: EmitShift(instruction);
  1142. |IntermediateCode.ror: EmitShift(instruction);
  1143. |IntermediateCode.cas: EmitCas(instruction);
  1144. |IntermediateCode.copy: EmitCopy(instruction);
  1145. |IntermediateCode.fill: EmitFill(instruction,FALSE);
  1146. |IntermediateCode.asm: EmitAsm(instruction);
  1147. END;
  1148. ReserveOperandRegisters(instruction.op3,FALSE); ReserveOperandRegisters(instruction.op2,FALSE); ReserveOperandRegisters(instruction.op1,FALSE);
  1149. END Generate;
  1150. PROCEDURE PostGenerate*(CONST instruction: IntermediateCode.Instruction);
  1151. VAR ticket: Ticket;
  1152. BEGIN
  1153. TryUnmap(instruction.op3); TryUnmap(instruction.op2); TryUnmap(instruction.op1);
  1154. ticket := tickets.live;
  1155. WHILE (ticket # NIL) & (ticket.lastuse = inPC) DO
  1156. UnmapTicket(ticket);
  1157. ticket := tickets.live
  1158. END;
  1159. END PostGenerate;
  1160. (* enter procedure: generate PAF and clear stack *)
  1161. PROCEDURE EmitEnter(CONST instruction: IntermediateCode.Instruction);
  1162. VAR op1,imm,target: Assembler.Operand; cc,size,numberMachineWords,destPC,firstPC,secondPC,x: LONGINT; body: SyntaxTree.Body; name: Basic.SegmentedName;
  1163. parametersSize: SIZE;
  1164. CONST initialize=TRUE; FirstOffset = 5; SecondOffset = 11;
  1165. BEGIN
  1166. stackSize := SHORT(instruction.op2.intValue);
  1167. size := stackSize;
  1168. INC(traceStackSize, stackSize);
  1169. IF initialize THEN
  1170. (* always including this instruction make trace insertion possible *)
  1171. IF backend.traceable THEN
  1172. emitter.Emit2(InstructionSet.opXOR,opRA,opRA);
  1173. END;
  1174. ASSERT(size MOD opRA.sizeInBytes = 0);
  1175. numberMachineWords := size DIV opRA.sizeInBytes;
  1176. IF numberMachineWords >0 THEN
  1177. IF ~backend.traceable THEN
  1178. emitter.Emit2(InstructionSet.opXOR,opRA,opRA);
  1179. END;
  1180. WHILE numberMachineWords MOD 4 # 0 DO
  1181. emitter.Emit1(InstructionSet.opPUSH, opRA);
  1182. DEC(numberMachineWords);
  1183. END;
  1184. IF numberMachineWords >4 THEN
  1185. Assembler.InitImm(imm, 0, numberMachineWords DIV 4);
  1186. (* do not use EBX because it is not volative in WINAPI, do not use ECX: special register in COOP *)
  1187. emitter.Emit2(InstructionSet.opMOV, opRD, imm);
  1188. destPC := out.pc;
  1189. emitter.Emit1(InstructionSet.opDEC, opRD);
  1190. emitter.Emit1(InstructionSet.opPUSH, opRA);
  1191. emitter.Emit1(InstructionSet.opPUSH, opRA);
  1192. emitter.Emit1(InstructionSet.opPUSH, opRA);
  1193. emitter.Emit1(InstructionSet.opPUSH, opRA);
  1194. Assembler.InitOffset8(target,destPC);
  1195. emitter.Emit1(InstructionSet.opJNZ, target)
  1196. ELSE
  1197. WHILE numberMachineWords >0 DO
  1198. emitter.Emit1(InstructionSet.opPUSH, opRA);
  1199. DEC(numberMachineWords);
  1200. END;
  1201. END;
  1202. END;
  1203. IF spillStack.MaxSize()>0 THEN (* register spill stack, does not have to be initialized *)
  1204. op1 := Assembler.NewImm32(spillStack.MaxSize()*cpuBits DIV 8);
  1205. emitter.Emit2(InstructionSet.opSUB,opSP,op1);
  1206. END;
  1207. ELSE
  1208. op1 := Assembler.NewImm32(size+ spillStack.MaxSize());
  1209. emitter.Emit2(InstructionSet.opSUB,opSP,op1);
  1210. END;
  1211. cc := SHORT(instruction.op1.intValue);
  1212. IF (cc = SyntaxTree.WinAPICallingConvention) OR (cc = SyntaxTree.CCallingConvention) THEN
  1213. IF cpuBits = 32 THEN
  1214. (* the winapi calling convention presumes that all registers except EAX, EDX and ECX are retained by the callee *)
  1215. emitter.Emit1(InstructionSet.opPUSH,opEBX);
  1216. emitter.Emit1(InstructionSet.opPUSH,opEDI);
  1217. emitter.Emit1(InstructionSet.opPUSH,opESI);
  1218. ELSE ASSERT(cpuBits =64);
  1219. emitter.Emit1(InstructionSet.opPUSH,opRB);
  1220. emitter.Emit1(InstructionSet.opPUSH,opRDI);
  1221. emitter.Emit1(InstructionSet.opPUSH,opRSI);
  1222. emitter.Emit1(InstructionSet.opPUSH,opR12);
  1223. emitter.Emit1(InstructionSet.opPUSH,opR13);
  1224. emitter.Emit1(InstructionSet.opPUSH,opR14);
  1225. emitter.Emit1(InstructionSet.opPUSH,opR15);
  1226. END;
  1227. END;
  1228. spillStackStart := stackSize;
  1229. END EmitEnter;
  1230. PROCEDURE EmitLeave(CONST instruction: IntermediateCode.Instruction);
  1231. VAR cc: LONGINT; offset: Assembler.Operand;
  1232. BEGIN
  1233. cc := SHORT(instruction.op1.intValue);
  1234. IF (cc = SyntaxTree.WinAPICallingConvention) OR (cc = SyntaxTree.CCallingConvention) THEN
  1235. IF cpuBits = 32 THEN
  1236. emitter.Emit1(InstructionSet.opPOP,opESI);
  1237. emitter.Emit1(InstructionSet.opPOP,opEDI);
  1238. emitter.Emit1(InstructionSet.opPOP,opEBX);
  1239. ELSE ASSERT(cpuBits =64);
  1240. emitter.Emit1(InstructionSet.opPOP,opR15);
  1241. emitter.Emit1(InstructionSet.opPOP,opR14);
  1242. emitter.Emit1(InstructionSet.opPOP,opR13);
  1243. emitter.Emit1(InstructionSet.opPOP,opR12);
  1244. emitter.Emit1(InstructionSet.opPOP,opRSI);
  1245. emitter.Emit1(InstructionSet.opPOP,opRDI);
  1246. emitter.Emit1(InstructionSet.opPOP,opRB);
  1247. END;
  1248. END;
  1249. END EmitLeave;
  1250. PROCEDURE EmitExit(CONST instruction: IntermediateCode.Instruction);
  1251. VAR parSize,cc: LONGINT; operand: Assembler.Operand;
  1252. BEGIN
  1253. cc := SHORT(instruction.op2.intValue);
  1254. parSize := SHORT(instruction.op3.intValue);
  1255. IF (parSize = 0) OR (cc = SyntaxTree.WinAPICallingConvention) & (cpuBits = 64) THEN
  1256. emitter.Emit0(InstructionSet.opRET)
  1257. ELSE (* e.g. for WINAPI calling convention *)
  1258. operand := Assembler.NewImm16(parSize);
  1259. emitter.Emit1(InstructionSet.opRET,operand)
  1260. END;
  1261. IF fpStackPointer # 0 THEN Error(instruction.textPosition,"compiler implementation error: fp stack not cleared") END;
  1262. END EmitExit;
  1263. PROCEDURE EmitReturnFPU(CONST instruction: IntermediateCode.Instruction);
  1264. VAR operand: Assembler.Operand;
  1265. BEGIN
  1266. IF IsRegister(instruction.op1) & MappedTo(instruction.op1.register,Low, ST0) THEN
  1267. (* nothing to do: result is already in return register *)
  1268. ELSE
  1269. MakeOperand(instruction.op1, Low, operand,NIL);
  1270. emitter.Emit1(InstructionSet.opFLD,operand);
  1271. (*
  1272. not necessary to clear from top of stack as callee will clear
  1273. INC(fpStackPointer);
  1274. emitter.Emit1(InstructionSet.opFSTP,registerOperands[ST0+1]);
  1275. DEC(fpStackPointer);
  1276. *)
  1277. END;
  1278. END EmitReturnFPU;
  1279. (* return operand
  1280. store operand in return register or on fp stack
  1281. *)
  1282. PROCEDURE EmitReturn(CONST instruction: IntermediateCode.Instruction; part: LONGINT);
  1283. VAR return,operand: Assembler.Operand; register: LONGINT; ticket: Ticket; type: IntermediateCode.Type;
  1284. BEGIN
  1285. register := ResultRegister(instruction.op1.type, part);
  1286. IF IsRegister(instruction.op1) & MappedTo(instruction.op1.register,part, register) THEN
  1287. (* nothing to do: result is already in return register *)
  1288. ELSE
  1289. GetPartType(instruction.op1.type,part, type);
  1290. MakeOperand(instruction.op1, part, operand,NIL);
  1291. Spill(physicalRegisters.Mapped(register));
  1292. ticket := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,type,register,inPC);
  1293. TicketToOperand(ticket, return);
  1294. (* Mov takes care of potential register overlaps *)
  1295. Move(return, operand, type);
  1296. UnmapTicket(ticket);
  1297. END;
  1298. END EmitReturn;
  1299. PROCEDURE EmitMovFloat(CONST vdest,vsrc:IntermediateCode.Operand);
  1300. VAR dest,src, espm: Assembler.Operand; sizeInBytes: SHORTINT; vcopy: IntermediateCode.Operand;
  1301. BEGIN
  1302. sizeInBytes := SHORTINT(vdest.type.sizeInBits DIV 8);
  1303. IF vdest.type.form IN IntermediateCode.Integer THEN
  1304. (* e.g. in SYSTEM.VAL(LONGINT, r) *)
  1305. IF vsrc.mode = IntermediateCode.ModeMemory THEN
  1306. vcopy := vsrc; IntermediateCode.SetType(vcopy,vdest.type);
  1307. EmitMov(vdest, vcopy,Low);
  1308. IF IsComplex(vdest) THEN
  1309. EmitMov(vdest,vcopy,High);
  1310. END;
  1311. ELSE
  1312. IF backend.forceFPU THEN
  1313. MakeOperand(vsrc,Low,src,NIL);
  1314. emitter.Emit1(InstructionSet.opFLD,src);
  1315. INC(fpStackPointer);
  1316. IF vdest.mode = IntermediateCode.ModeMemory THEN
  1317. MakeOperand(vdest,Low,dest,NIL);
  1318. Assembler.SetSize(dest,sizeInBytes);
  1319. emitter.Emit1(InstructionSet.opFSTP,dest);
  1320. DEC(fpStackPointer);
  1321. ELSE
  1322. AllocateStack(sizeInBytes);
  1323. Assembler.InitMem(espm, sizeInBytes,SP,0);
  1324. emitter.Emit1(InstructionSet.opFSTP,espm);
  1325. DEC(fpStackPointer);
  1326. MakeOperand(vdest,Low,dest,NIL);
  1327. EmitPop(vdest,Low);
  1328. IF IsComplex(vdest) THEN
  1329. EmitPop(vdest,High);
  1330. END;
  1331. END;
  1332. ELSE
  1333. MakeOperand(vsrc, Low, src, NIL);
  1334. IF vdest.mode = IntermediateCode.ModeMemory THEN
  1335. MakeOperand(vdest, Low, dest, NIL);
  1336. Move(dest, src, vsrc.type);
  1337. ELSE (* need temporary stack argument *)
  1338. AllocateStack(sizeInBytes);
  1339. Assembler.InitMem(espm, sizeInBytes,SP,0);
  1340. Move(espm, src, vsrc.type);
  1341. MakeOperand(vdest,Low,dest,NIL);
  1342. EmitPop(vdest,Low);
  1343. IF IsComplex(vdest) THEN
  1344. EmitPop(vdest,High);
  1345. END;
  1346. END;
  1347. END;
  1348. END;
  1349. ELSIF vsrc.type.form IN IntermediateCode.Integer THEN
  1350. (* e.g. in SYSTEM.VAL(REAL, i) *)
  1351. IF vdest.mode = IntermediateCode.ModeMemory THEN
  1352. vcopy := vdest; IntermediateCode.SetType(vcopy,vsrc.type);
  1353. EmitMov(vcopy, vsrc,Low);
  1354. IF IsComplex(vsrc) THEN
  1355. EmitMov(vcopy,vsrc,High);
  1356. END;
  1357. ELSE
  1358. IF backend.forceFPU THEN
  1359. IF vsrc.mode = IntermediateCode.ModeMemory THEN
  1360. MakeOperand(vsrc,Low,src,NIL);
  1361. Assembler.SetSize(src,sizeInBytes);
  1362. emitter.Emit1(InstructionSet.opFLD,src);
  1363. ELSE
  1364. IF IsComplex(vsrc) THEN
  1365. EmitPush(vsrc,High);
  1366. END;
  1367. EmitPush(vsrc,Low);
  1368. Assembler.InitMem(espm, sizeInBytes,SP,0);
  1369. emitter.Emit1(InstructionSet.opFLD,espm);
  1370. ASSERT(sizeInBytes >0);
  1371. AllocateStack(-sizeInBytes);
  1372. END;
  1373. INC(fpStackPointer);
  1374. MakeOperand(vdest,Low,dest,NIL);
  1375. emitter.Emit1(InstructionSet.opFSTP,dest);
  1376. DEC(fpStackPointer);
  1377. ELSE
  1378. IF vsrc.mode = IntermediateCode.ModeMemory THEN
  1379. MakeOperand(vsrc,Low,src,NIL);
  1380. Assembler.SetSize(src,sizeInBytes);
  1381. MakeOperand(vdest,Low,dest,NIL);
  1382. Move(dest, src, vdest.type);
  1383. ELSE
  1384. IF IsComplex(vsrc) THEN
  1385. EmitPush(vsrc,High);
  1386. END;
  1387. EmitPush(vsrc,Low);
  1388. Assembler.InitMem(espm, sizeInBytes,SP,0);
  1389. MakeOperand(vdest, Low, dest, NIL);
  1390. Move(dest, espm, vdest.type);
  1391. AllocateStack(-sizeInBytes);
  1392. END;
  1393. END;
  1394. END;
  1395. ELSE
  1396. IF backend.forceFPU THEN
  1397. MakeOperand(vsrc,Low,src,NIL);
  1398. emitter.Emit1(InstructionSet.opFLD,src);
  1399. INC(fpStackPointer);
  1400. MakeOperand(vdest,Low,dest,NIL);
  1401. emitter.Emit1(InstructionSet.opFSTP,dest);
  1402. DEC(fpStackPointer);
  1403. ELSE
  1404. MakeOperand(vsrc, Low, src, NIL);
  1405. MakeOperand(vdest, Low, dest, NIL);
  1406. Move(dest, src, vdest.type)
  1407. END;
  1408. END;
  1409. END EmitMovFloat;
  1410. PROCEDURE EmitMov(CONST vdest,vsrc: IntermediateCode.Operand; part: LONGINT);
  1411. VAR op1,op2: Assembler.Operand; tmp: IntermediateCode.Operand;
  1412. t: CodeGenerators.Ticket;
  1413. type: IntermediateCode.Type;
  1414. offset: LONGINT;
  1415. BEGIN
  1416. IF (vdest.mode = IntermediateCode.ModeRegister) & (vsrc.mode = IntermediateCode.ModeRegister) & (vsrc.type.sizeInBits > 8) & (vsrc.offset # 0)THEN
  1417. (* MOV R1, R2+offset => LEA EAX, [EBX+offset] *)
  1418. tmp := vsrc;
  1419. IntermediateCode.MakeMemory(tmp,vsrc.type);
  1420. MakeOperand(tmp,part,op2,NIL);
  1421. (*
  1422. ReleaseHint(op2.register);
  1423. *)
  1424. MakeOperand(vdest,part,op1,NIL);
  1425. t := virtualRegisters.Mapped(vdest.register,part);
  1426. IF (t # NIL) & (t.spilled) THEN
  1427. UnSpill(t); (* make sure this has not spilled *)
  1428. MakeOperand(vdest,part, op1,NIL);
  1429. END;
  1430. emitter.Emit2(InstructionSet.opLEA,op1,op2);
  1431. ELSE
  1432. MakeOperand(vsrc,part,op2,NIL);
  1433. MakeOperand(vdest,part,op1,NIL);
  1434. GetPartType(vsrc.type, part, type);
  1435. Move(op1,op2, type);
  1436. END;
  1437. END EmitMov;
  1438. PROCEDURE EmitConvertFloat(CONST instruction: IntermediateCode.Instruction);
  1439. VAR destType, srcType, dtype: IntermediateCode.Type; dest,src,espm,imm: Assembler.Operand; sizeInBytes, index: LONGINT;
  1440. temp, temp2, temp3, temp4: Assembler.Operand; ticket: Ticket; vdest, vsrc: IntermediateCode.Operand;
  1441. BEGIN
  1442. vdest := instruction.op1; vsrc := instruction.op2;
  1443. srcType := vsrc.type;
  1444. destType := vdest.type;
  1445. IF destType.form = IntermediateCode.Float THEN
  1446. CASE srcType.form OF
  1447. |IntermediateCode.Float: (* just a move *)
  1448. IF backend.forceFPU THEN
  1449. EmitMovFloat(vdest, vsrc);
  1450. ELSE
  1451. MakeOperand(vsrc,Low,src,NIL);
  1452. MakeOperand(vdest, Low, dest, NIL);
  1453. IF srcType.sizeInBits = 32 THEN
  1454. SpecialMove(InstructionSet.opCVTSS2SD, InstructionSet.opMOVSS, FALSE, dest, src, destType)
  1455. ELSE
  1456. SpecialMove(InstructionSet.opCVTSD2SS, InstructionSet.opMOVSD, FALSE, dest, src, destType)
  1457. END;
  1458. END;
  1459. |IntermediateCode.SignedInteger:
  1460. (* put value to stack and then read from stack via Float *)
  1461. IF vsrc.type.sizeInBits < IntermediateCode.Bits32 THEN
  1462. MakeOperand(vsrc,Low,src,NIL);
  1463. ticket := TemporaryTicket(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int32);
  1464. TicketToOperand(ticket,temp);
  1465. emitter.Emit2(InstructionSet.opMOVSX,temp,src);
  1466. IF backend.forceFPU THEN (* via stack *)
  1467. emitter.Emit1(InstructionSet.opPUSH,temp);
  1468. UnmapTicket(ticket);
  1469. sizeInBytes := temp.sizeInBytes;
  1470. ELSE (* via register *)
  1471. espm := temp;
  1472. sizeInBytes := 0
  1473. END;
  1474. ELSIF IsComplex(vsrc) THEN (* via stack *)
  1475. EmitPush(vsrc,High);
  1476. EmitPush(vsrc,Low);
  1477. sizeInBytes := 8
  1478. ELSE
  1479. IF backend.forceFPU THEN (* via stack *)
  1480. EmitPush(vsrc,Low);
  1481. sizeInBytes := SHORTINT(4 (* cpuBits DIV 8*)) (*SHORT(srcType.sizeInBits DIV 8)*);
  1482. ELSE (* via memory or register *)
  1483. sizeInBytes := 0;
  1484. MakeOperand(vsrc,Low,src,NIL);
  1485. IF Assembler.IsImmediateOperand(src) THEN (* use temporary register *)
  1486. ticket := TemporaryTicket(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int32);
  1487. TicketToOperand(ticket,temp);
  1488. emitter.Emit2(InstructionSet.opMOVSX,temp,src);
  1489. espm := temp
  1490. ELSE
  1491. espm := src
  1492. END;
  1493. END
  1494. END;
  1495. IF sizeInBytes > 0 THEN
  1496. Assembler.InitMem(espm, SHORTINT(sizeInBytes),SP,0);
  1497. END;
  1498. IF backend.forceFPU THEN
  1499. emitter.Emit1(InstructionSet.opFILD,espm);
  1500. INC(fpStackPointer);
  1501. ASSERT(sizeInBytes >0);
  1502. Basic.Align(sizeInBytes, 4 (* cpuBits DIV 8*));
  1503. AllocateStack(-sizeInBytes);
  1504. MakeOperand(vdest,Low,dest,NIL);
  1505. emitter.Emit1(InstructionSet.opFSTP,dest);
  1506. DEC(fpStackPointer);
  1507. ELSIF IsComplex(vsrc) THEN
  1508. emitter.Emit1(InstructionSet.opFILD,espm);
  1509. MakeOperand(vdest,Low,dest,NIL);
  1510. IF Assembler.IsMemoryOperand(dest) THEN
  1511. emitter.Emit1(InstructionSet.opFSTP,dest);
  1512. ELSE (* must be register *)
  1513. emitter.Emit1(InstructionSet.opFSTP,espm);
  1514. emitter.Emit2(InstructionSet.opMOVQ,dest,espm);
  1515. IF destType.sizeInBits = 32 THEN
  1516. emitter.Emit2(InstructionSet.opCVTSD2SS, dest,dest);
  1517. END;
  1518. END;
  1519. AllocateStack(-sizeInBytes);
  1520. ELSE
  1521. MakeOperand(vdest,Low,dest,NIL);
  1522. IF destType.sizeInBits = 32 THEN
  1523. emitter.Emit2(InstructionSet.opCVTSI2SS, dest, espm)
  1524. ELSE
  1525. emitter.Emit2(InstructionSet.opCVTSI2SD, dest, espm)
  1526. END;
  1527. AllocateStack(-sizeInBytes);
  1528. END;
  1529. END;
  1530. ELSE
  1531. ASSERT(destType.form IN IntermediateCode.Integer);
  1532. ASSERT(srcType.form = IntermediateCode.Float);
  1533. Assert(vdest.type.form = IntermediateCode.SignedInteger, "no entier as result for unsigned integer");
  1534. MakeOperand(vsrc,Low,src,NIL);
  1535. IF ~backend.forceFPU THEN
  1536. MakeOperand(vdest,Low,dest,ticket);
  1537. GetTemporaryRegister(srcType, temp);
  1538. GetTemporaryRegister(srcType, temp3);
  1539. IF destType.sizeInBits < 32 THEN
  1540. IntermediateCode.InitType(dtype, destType.form, 32);
  1541. GetTemporaryRegister(dtype, temp4);
  1542. ELSE
  1543. dtype := destType;
  1544. temp4 := dest;
  1545. END;
  1546. GetTemporaryRegister(dtype, temp2);
  1547. IF srcType.sizeInBits = 32 THEN
  1548. (* convert truncated -> negative numbers round up !*)
  1549. emitter.Emit2(InstructionSet.opCVTTSS2SI, temp4, src);
  1550. (* back to temporary mmx register *)
  1551. emitter.Emit2(InstructionSet.opCVTSI2SS, temp, temp4);
  1552. (* subtract *)
  1553. emitter.Emit2(InstructionSet.opMOVSS, temp3, src);
  1554. emitter.Emit2(InstructionSet.opSUBSS, temp3, temp);
  1555. (* back to a GP register in order to determine the sign bit *)
  1556. ELSE
  1557. emitter.Emit2(InstructionSet.opCVTTSD2SI, temp4, src);
  1558. emitter.Emit2(InstructionSet.opCVTSI2SD, temp, temp4);
  1559. emitter.Emit2(InstructionSet.opMOVSD, temp3, src);
  1560. emitter.Emit2(InstructionSet.opSUBSD, temp3, temp);
  1561. emitter.Emit2(InstructionSet.opCVTSD2SS, temp3, temp3);
  1562. END;
  1563. emitter.Emit2(InstructionSet.opMOVD, temp2, temp3);
  1564. Assembler.InitImm(imm, 0 ,srcType.sizeInBits-1);
  1565. emitter.Emit2(InstructionSet.opBT, temp2, imm);
  1566. Assembler.InitImm(imm, 0 ,0);
  1567. emitter.Emit2(InstructionSet.opSBB, temp4, imm);
  1568. IF dtype.sizeInBits # destType.sizeInBits THEN
  1569. index := temp4.register;
  1570. CASE destType.sizeInBits OF (* choose low part accordingly *)
  1571. IntermediateCode.Bits8: index := index MOD 32 + AL;
  1572. |IntermediateCode.Bits16: index := index MOD 32 + AX;
  1573. |IntermediateCode.Bits32: index := index MOD 32 + EAX;
  1574. END;
  1575. temp4 := registerOperands[index];
  1576. emitter.Emit2(InstructionSet.opMOV, dest, temp4);
  1577. END
  1578. ELSE
  1579. emitter.Emit1(InstructionSet.opFLD,src); INC(fpStackPointer);
  1580. MakeOperand(vdest,Low,dest,NIL);
  1581. IF destType.sizeInBits = IntermediateCode.Bits64 THEN AllocateStack(12) ELSE AllocateStack(8) END;
  1582. Assembler.InitMem(espm,IntermediateCode.Bits16 DIV 8,SP,0);
  1583. emitter.Emit1(InstructionSet.opFNSTCW,espm);
  1584. emitter.Emit0(InstructionSet.opFWAIT);
  1585. Assembler.InitMem(espm,IntermediateCode.Bits32 DIV 8,SP,0);
  1586. ticket := TemporaryTicket(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int32);
  1587. TicketToOperand(ticket,temp);
  1588. emitter.Emit2(InstructionSet.opMOV,temp,espm);
  1589. imm := Assembler.NewImm32(0F3FFH);
  1590. emitter.Emit2(InstructionSet.opAND,temp,imm);
  1591. imm := Assembler.NewImm32(0400H);
  1592. emitter.Emit2(InstructionSet.opOR,temp,imm);
  1593. Assembler.InitMem(espm,IntermediateCode.Bits32 DIV 8,SP,4);
  1594. emitter.Emit2(InstructionSet.opMOV,espm,temp);
  1595. Assembler.InitMem(espm,IntermediateCode.Bits16 DIV 8,SP,4);
  1596. emitter.Emit1(InstructionSet.opFLDCW,espm);
  1597. IF destType.sizeInBits = IntermediateCode.Bits64 THEN
  1598. Assembler.InitMem(espm,IntermediateCode.Bits64 DIV 8,SP,4);
  1599. emitter.Emit1(InstructionSet.opFISTP,espm);DEC(fpStackPointer);
  1600. emitter.Emit0(InstructionSet.opFWAIT);
  1601. ELSE
  1602. Assembler.InitMem(espm,IntermediateCode.Bits32 DIV 8,SP,4);
  1603. emitter.Emit1(InstructionSet.opFISTP,espm); DEC(fpStackPointer);
  1604. emitter.Emit0(InstructionSet.opFWAIT);
  1605. END;
  1606. Assembler.InitMem(espm,IntermediateCode.Bits16 DIV 8,SP,0);
  1607. emitter.Emit1(InstructionSet.opFLDCW,espm);
  1608. emitter.Emit1(InstructionSet.opPOP,temp);
  1609. UnmapTicket(ticket);
  1610. emitter.Emit1(InstructionSet.opPOP,dest);
  1611. IF IsComplex(vdest) THEN
  1612. MakeOperand(vdest,High,dest,NIL);
  1613. emitter.Emit1(InstructionSet.opPOP,dest);
  1614. END;
  1615. END;
  1616. END;
  1617. END EmitConvertFloat;
  1618. PROCEDURE EmitConvert(CONST vdest, vsrc: IntermediateCode.Operand; part: LONGINT);
  1619. VAR destType, srcType: IntermediateCode.Type; op1,op2: Assembler.Operand; index: LONGINT; nul: Assembler.Operand;
  1620. ticket: Ticket; vop: IntermediateCode.Operand; ediReserved, esiReserved: BOOLEAN;
  1621. eax, edx: Ticket; symbol: ObjectFile.Identifier; offset: LONGINT;
  1622. BEGIN
  1623. GetPartType(vdest.type,part, destType);
  1624. GetPartType(vsrc.type,part,srcType);
  1625. ASSERT(vdest.type.form IN IntermediateCode.Integer);
  1626. ASSERT(destType.form IN IntermediateCode.Integer);
  1627. IF destType.sizeInBits < srcType.sizeInBits THEN (* SHORT *)
  1628. ASSERT(part # High);
  1629. MakeOperand(vdest,part,op1,NIL);
  1630. IF vsrc.mode = IntermediateCode.ModeImmediate THEN
  1631. vop := vsrc;
  1632. IntermediateCode.SetType(vop,destType);
  1633. MakeOperand(vop,part,op2,NIL);
  1634. ELSE
  1635. MakeOperand(vsrc,part,op2,NIL);
  1636. IF Assembler.IsRegisterOperand(op1) & ((op1.register DIV 32 >0) (* not 8 bit register *) OR (op1.register DIV 16 = 0) & (physicalRegisters.Mapped(op1.register MOD 16 + AH)=free) (* low 8 bit register with free upper part *)) THEN
  1637. (* try EAX <- EDI for dest = AL or AX, src=EDI *)
  1638. index := op1.register;
  1639. CASE srcType.sizeInBits OF
  1640. IntermediateCode.Bits16: index := index MOD 32 + AX;
  1641. |IntermediateCode.Bits32: index := index MOD 32 + EAX;
  1642. |IntermediateCode.Bits64: index := index MOD 32 + RAX;
  1643. END;
  1644. op1 := registerOperands[index];
  1645. ELSE
  1646. (* reserve register with a low part *)
  1647. IF destType.sizeInBits=8 THEN (* make sure that allocated temporary register has a low part with 8 bits, i.e. exclude ESI or EDI *)
  1648. ediReserved := physicalRegisters.Reserved(EDI);
  1649. esiReserved := physicalRegisters.Reserved(ESI);
  1650. physicalRegisters.SetReserved(EDI,TRUE); physicalRegisters.SetReserved(ESI,TRUE);
  1651. ticket := TemporaryTicket(IntermediateCode.GeneralPurposeRegister,srcType); (* register with low part *)
  1652. physicalRegisters.SetReserved(EDI,ediReserved); physicalRegisters.SetReserved(ESI,esiReserved);
  1653. ELSE
  1654. ticket := TemporaryTicket(IntermediateCode.GeneralPurposeRegister,srcType); (* any register with low part *)
  1655. END;
  1656. MakeOperand(vsrc,part,op2,ticket); (* stores op2 in ticket register *)
  1657. index := op2.register;
  1658. CASE destType.sizeInBits OF (* choose low part accordingly *)
  1659. IntermediateCode.Bits8: index := index MOD 32 + AL;
  1660. |IntermediateCode.Bits16: index := index MOD 32 + AX;
  1661. |IntermediateCode.Bits32: index := index MOD 32 + EAX;
  1662. END;
  1663. op2 := registerOperands[index];
  1664. END;
  1665. Move(op1,op2,PhysicalOperandType(op1));
  1666. END;
  1667. ELSIF destType.sizeInBits > srcType.sizeInBits THEN (* (implicit) LONG *)
  1668. IF part = High THEN
  1669. IF destType.form = IntermediateCode.SignedInteger THEN
  1670. Spill(physicalRegisters.Mapped(EAX));
  1671. eax := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int32,EAX,inPC);
  1672. Spill(physicalRegisters.Mapped(EDX));
  1673. edx := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int32,EDX,inPC);
  1674. IF vsrc.type.sizeInBits < 32 THEN
  1675. MakeOperand(vsrc,Low,op2,NIL);
  1676. SpecialMove(InstructionSet.opMOVSX,InstructionSet.opMOV, FALSE, opEAX,op2,PhysicalOperandType(opEAX));
  1677. ELSE
  1678. MakeOperand(vsrc,Low,op2,eax);
  1679. END;
  1680. emitter.Emit0(InstructionSet.opCDQ);
  1681. MakeOperand(vdest,High,op1,NIL);
  1682. emitter.Emit2(InstructionSet.opMOV,op1,opEDX);
  1683. UnmapTicket(eax); UnmapTicket(edx);
  1684. ELSE
  1685. MakeOperand(vdest,part,op1,NIL);
  1686. IF (vdest.mode = IntermediateCode.ModeRegister) THEN
  1687. emitter.Emit2(InstructionSet.opXOR,op1,op1)
  1688. ELSE
  1689. Assembler.InitImm(nul,0,0);
  1690. emitter.Emit2(InstructionSet.opMOV,op1,nul);
  1691. END;
  1692. END;
  1693. ELSE
  1694. ASSERT(part=Low);
  1695. MakeOperand(vdest,part,op1,NIL);
  1696. MakeOperand(vsrc,part,op2,NIL);
  1697. IF srcType.sizeInBits = destType.sizeInBits THEN
  1698. Move(op1,op2,PhysicalOperandType(op1));
  1699. ELSIF srcType.form = IntermediateCode.SignedInteger THEN
  1700. IF srcType.sizeInBits=32 THEN (* 64 bits only *)
  1701. ASSERT(cpuBits=64);
  1702. SpecialMove(InstructionSet.opMOVSXD,InstructionSet.opMOV, FALSE, op1,op2,PhysicalOperandType(op1));
  1703. ELSE
  1704. SpecialMove(InstructionSet.opMOVSX,InstructionSet.opMOV, FALSE, op1,op2,PhysicalOperandType(op1));
  1705. END;
  1706. ELSE
  1707. ASSERT(srcType.form = IntermediateCode.UnsignedInteger);
  1708. IF srcType.sizeInBits=32 THEN (* 64 bits only *)
  1709. ASSERT(cpuBits=64);
  1710. IF Assembler.IsRegisterOperand(op1) THEN
  1711. Move( registerOperands[op1.register MOD 32 + EAX], op2,srcType);
  1712. ELSE
  1713. ASSERT(Assembler.IsMemoryOperand(op1));
  1714. symbol := op1.symbol; offset := op1.offset;
  1715. Assembler.InitMem(op1,Assembler.bits32,op1.register, op1.displacement);
  1716. Assembler.SetSymbol(op1,symbol.name,symbol.fingerprint,offset,op1.displacement);
  1717. Move( op1, op2, srcType);
  1718. Assembler.InitMem(op1,Assembler.bits32,op1.register, op1.displacement+Assembler.bits32);
  1719. Assembler.SetSymbol(op1,symbol.name, symbol.fingerprint,offset,op1.displacement);
  1720. Assembler.InitImm(op2,0,0);
  1721. Move( op1, op2,srcType);
  1722. END;
  1723. ELSE
  1724. SpecialMove(InstructionSet.opMOVZX, InstructionSet.opMOV, FALSE, op1, op2,PhysicalOperandType(op1))
  1725. END;
  1726. END;
  1727. END;
  1728. ELSE (* destType.sizeInBits = srcType.sizeInBits) *)
  1729. EmitMov(vdest,vsrc,part);
  1730. END;
  1731. END EmitConvert;
  1732. PROCEDURE EmitResult(CONST instruction: IntermediateCode.Instruction);
  1733. VAR result, resultHigh, op, opHigh: Assembler.Operand; register, highRegister: LONGINT; lowReserved, highReserved: BOOLEAN; type: IntermediateCode.Type;
  1734. BEGIN
  1735. IF ~IsComplex(instruction.op1) THEN
  1736. register := ResultRegister(instruction.op1.type,Low);
  1737. result := registerOperands[register];
  1738. MakeOperand(instruction.op1,Low,op,NIL);
  1739. GetPartType(instruction.op1.type, Low, type);
  1740. Move(op,result,type);
  1741. ELSE
  1742. register := ResultRegister(instruction.op1.type,Low);
  1743. result := registerOperands[register];
  1744. highRegister := ResultRegister(instruction.op1.type, High);
  1745. resultHigh := registerOperands[highRegister];
  1746. (* make sure that result registers are not used during emission of Low / High *)
  1747. lowReserved := physicalRegisters.Reserved(register);
  1748. physicalRegisters.SetReserved(register, TRUE);
  1749. highReserved := physicalRegisters.Reserved(highRegister);
  1750. physicalRegisters.SetReserved(highRegister,TRUE);
  1751. MakeOperand(instruction.op1,Low,op, NIL);
  1752. IF Assembler.SameOperand(op, resultHigh) THEN
  1753. emitter.Emit2(InstructionSet.opXCHG, result, resultHigh); (* low register already mapped ok *)
  1754. MakeOperand(instruction.op1, High, opHigh, NIL);
  1755. GetPartType(instruction.op1.type, High, type);
  1756. Move(opHigh, result, type);
  1757. ELSE
  1758. GetPartType(instruction.op1.type, Low, type);
  1759. Move(op, result, type);
  1760. MakeOperand(instruction.op1,High, opHigh, NIL);
  1761. GetPartType(instruction.op1.type, High, type);
  1762. Move(opHigh, resultHigh, type);
  1763. END;
  1764. physicalRegisters.SetReserved(register, lowReserved);
  1765. physicalRegisters.SetReserved(highRegister, highReserved);
  1766. END;
  1767. END EmitResult;
  1768. PROCEDURE EmitResultFPU(CONST instruction: IntermediateCode.Instruction);
  1769. VAR op: Assembler.Operand;
  1770. BEGIN
  1771. INC(fpStackPointer); (* callee has left the result on top of stack, don't have to allocate here *)
  1772. MakeOperand(instruction.op1,Low,op,NIL);
  1773. emitter.Emit1(InstructionSet.opFSTP,op);
  1774. DEC(fpStackPointer);
  1775. (*
  1776. UnmapTicket(ticket);
  1777. *)
  1778. END EmitResultFPU;
  1779. PROCEDURE EmitCall(CONST instruction: IntermediateCode.Instruction);
  1780. VAR fixup: Sections.Section; target, op, parSize: Assembler.Operand;
  1781. code: SyntaxTree.Code; emitterFixup,newFixup: BinaryCode.Fixup; resolved: BinaryCode.Section; pc: LONGINT;
  1782. BEGIN
  1783. IF fpStackPointer # 0 THEN Error(instruction.textPosition,"compiler implementation error: fp stack not cleared before call") END;
  1784. IF instruction.op1.mode = IntermediateCode.ModeImmediate THEN
  1785. fixup := module.allSections.FindByName(instruction.op1.symbol.name);
  1786. IF (fixup # NIL) & (fixup.type = Sections.InlineCodeSection) THEN
  1787. pc := out.pc;
  1788. (* resolved must be available at this point ! *)
  1789. resolved := fixup(IntermediateCode.Section).resolved;
  1790. IF resolved # NIL THEN
  1791. emitter.code.CopyBits(resolved.os.bits,0,resolved.os.bits.GetSize());
  1792. emitterFixup := resolved.fixupList.firstFixup;
  1793. WHILE (emitterFixup # NIL) DO
  1794. newFixup := BinaryCode.NewFixup(emitterFixup.mode,emitterFixup.offset+pc,emitterFixup.symbol,emitterFixup.symbolOffset,emitterFixup.displacement,emitterFixup.scale,emitterFixup.pattern);
  1795. out.fixupList.AddFixup(newFixup);
  1796. emitterFixup := emitterFixup.nextFixup;
  1797. END;
  1798. END;
  1799. ELSIF cpuBits = 64 THEN
  1800. MakeOperand(instruction.op1,Low,op,NIL);
  1801. emitter.Emit1(InstructionSet.opCALL,op);
  1802. Assembler.InitOffset32(parSize,instruction.op2.intValue);
  1803. IF parSize.val # 0 THEN emitter.Emit2(InstructionSet.opADD,opSP,parSize) END;
  1804. ELSE
  1805. Assembler.InitOffset32(target,instruction.op1.intValue);
  1806. Assembler.SetSymbol(target,instruction.op1.symbol.name,instruction.op1.symbol.fingerprint,instruction.op1.offset,0);
  1807. emitter.Emit1(InstructionSet.opCALL,target);
  1808. Assembler.InitOffset32(parSize,instruction.op2.intValue);
  1809. IF parSize.val # 0 THEN emitter.Emit2(InstructionSet.opADD,opSP,parSize) END;
  1810. END;
  1811. ELSE
  1812. MakeOperand(instruction.op1,Low,op,NIL);
  1813. emitter.Emit1(InstructionSet.opCALL,op);
  1814. Assembler.InitOffset32(parSize,instruction.op2.intValue);
  1815. IF parSize.val # 0 THEN emitter.Emit2(InstructionSet.opADD,opSP,parSize) END;
  1816. END;
  1817. END EmitCall;
  1818. (*
  1819. register allocation
  1820. instruction dest, src1, src2
  1821. preconditions
  1822. dest is memory operand or dest is register with offset = 0
  1823. src1 and src2 may be immediates, registers with or without offset and memory operands
  1824. 1.) translation into two-operand code
  1825. a) dest = src1 (no assumption on src2, src2=src1 is permitted )
  1826. i) dest and src2 are both memory operands or src2 is a register with offset # 0
  1827. alloc temp register
  1828. mov temp, src2
  1829. instruction2 dest, temp
  1830. ii) dest or src2 is not a memory operand
  1831. instruction2 dest, src2
  1832. b) dest = src2
  1833. => src2 is not a register with offset # 0
  1834. alloc temp register
  1835. mov dest, src1
  1836. mov temp, src2
  1837. instruction2 dest, temp
  1838. c) dest # src2
  1839. mov dest, src1
  1840. i) dest and src2 are both memory operands or src2 is a register with offset # 0
  1841. allocate temp register
  1842. mov temp, src2
  1843. instruction2 dest, temp
  1844. ii)
  1845. instruction2 dest, src2
  1846. 1'.) translation into one operand code
  1847. instruction dest, src1
  1848. a) dest = src1
  1849. => src1 is not a register with offset # 0
  1850. instruction1 dest
  1851. b) dest # src1
  1852. mov dest, src1
  1853. instruction1 dest
  1854. 2.) register allocation
  1855. precondition: src1 and src2 are already allocated
  1856. a) dest is already allocated
  1857. go on according to 1.
  1858. b) dest needs to be allocated
  1859. check if register is free
  1860. i) yes: allocate free register and go on with 1.
  1861. ii) no: spill last register in livelist, map register and go on with 1.
  1862. *)
  1863. PROCEDURE PrepareOp3(CONST instruction: IntermediateCode.Instruction;part: LONGINT; VAR left, right: Assembler.Operand; VAR ticket: Ticket);
  1864. VAR vop1,vop2, vop3: IntermediateCode.Operand; op1,op2,op3,temp: Assembler.Operand; type: IntermediateCode.Type;
  1865. t: Ticket;
  1866. BEGIN
  1867. ticket := NIL;
  1868. GetPartType(instruction.op1.type,part,type);
  1869. vop1 := instruction.op1; vop2 := instruction.op2; vop3 := instruction.op3;
  1870. IF IntermediateCode.OperandEquals(vop1,vop3) & (IntermediateCode.Commute23 IN IntermediateCode.instructionFormat[instruction.opcode].flags) THEN
  1871. vop3 := instruction.op2; vop2 := instruction.op3;
  1872. END;
  1873. MakeOperand(vop3,part, op3,NIL);
  1874. IF (vop1.mode = IntermediateCode.ModeRegister) & (~IsMemoryOperand(vop1,part)) & (vop1.register # vop3.register) THEN
  1875. IF (vop2.mode = IntermediateCode.ModeRegister) & (vop2.register = vop1.register) & (vop2.offset = 0) THEN
  1876. (* same register *)
  1877. MakeOperand(vop1,part, op1,NIL);
  1878. ELSE
  1879. MakeOperand(vop2,part, op2,NIL);
  1880. (*
  1881. ReleaseHint(op2.register);
  1882. *)
  1883. MakeOperand(vop1,part, op1,NIL);
  1884. Move(op1, op2, type);
  1885. t := virtualRegisters.Mapped(vop1.register,part);
  1886. IF (t # NIL) & (t.spilled) THEN
  1887. UnSpill(t); (* make sure this has not spilled *)
  1888. MakeOperand(vop1,part, op1,NIL);
  1889. END;
  1890. END;
  1891. left := op1; right := op3;
  1892. ELSIF IntermediateCode.OperandEquals(vop1,vop2) & (~IsMemoryOperand(vop1,part) OR ~IsMemoryOperand(vop3,part)) THEN
  1893. MakeOperand(vop1,part, op1,NIL);
  1894. left := op1; right := op3;
  1895. ELSE
  1896. MakeOperand(vop1,part, op1,NIL);
  1897. MakeOperand(vop2,part, op2,NIL);
  1898. (*ReleaseHint(op2.register);*)
  1899. ticket := TemporaryTicket(IntermediateCode.GeneralPurposeRegister,type);
  1900. TicketToOperand(ticket,temp);
  1901. Move(temp, op2, type);
  1902. left := temp; right := op3;
  1903. END;
  1904. END PrepareOp3;
  1905. PROCEDURE PrepareOp2(CONST instruction: IntermediateCode.Instruction; part: LONGINT; VAR left: Assembler.Operand;VAR ticket: Ticket);
  1906. VAR op2: Assembler.Operand; imm: Assembler.Operand; sizeInBits: INTEGER; type: IntermediateCode.Type;
  1907. BEGIN
  1908. ticket := NIL;
  1909. GetPartType(instruction.op1.type,part,type);
  1910. IF (instruction.op1.mode = IntermediateCode.ModeRegister) THEN
  1911. MakeOperand(instruction.op1,part,left,NIL);
  1912. MakeOperand(instruction.op2,part,op2,NIL);
  1913. IF (instruction.op2.mode = IntermediateCode.ModeRegister) & (instruction.op2.register = instruction.op1.register) & (instruction.op2.offset = 0) THEN
  1914. ELSE
  1915. Move(left, op2, type);
  1916. IF (instruction.op2.offset # 0) & ~IsMemoryOperand(instruction.op2,part) THEN
  1917. GetPartType(instruction.op2.type,part,type);
  1918. sizeInBits := type.sizeInBits;
  1919. Assembler.InitImm(imm,0,instruction.op2.offset);
  1920. emitter.Emit2(InstructionSet.opADD,left,imm);
  1921. END;
  1922. END;
  1923. ELSIF IntermediateCode.OperandEquals(instruction.op1,instruction.op2) & ((instruction.op1.mode # IntermediateCode.ModeMemory) OR (instruction.op3.mode # IntermediateCode.ModeMemory)) THEN
  1924. MakeOperand(instruction.op1,part,left,NIL);
  1925. ELSE
  1926. MakeOperand(instruction.op2,part, op2,NIL);
  1927. ticket := TemporaryTicket(IntermediateCode.GeneralPurposeRegister,type);
  1928. TicketToOperand(ticket,left);
  1929. Move(left, op2, type);
  1930. END;
  1931. END PrepareOp2;
  1932. PROCEDURE FinishOp(CONST vop: IntermediateCode.Operand; part: LONGINT; left: Assembler.Operand; ticket: Ticket);
  1933. VAR op1: Assembler.Operand;
  1934. BEGIN
  1935. IF ticket # NIL THEN
  1936. MakeOperand(vop,part, op1,NIL);
  1937. Move(op1,left,vop.type);
  1938. UnmapTicket(ticket);
  1939. END;
  1940. END FinishOp;
  1941. PROCEDURE EmitArithmetic3Part(CONST instruction: IntermediateCode.Instruction; part: LONGINT; opcode: LONGINT);
  1942. VAR left,right: Assembler.Operand; ticket: Ticket;
  1943. BEGIN
  1944. PrepareOp3(instruction, part, left,right,ticket);
  1945. emitter.Emit2(opcode,left,right);
  1946. FinishOp(instruction.op1,part,left,ticket);
  1947. END EmitArithmetic3Part;
  1948. PROCEDURE EmitArithmetic3(CONST instruction: IntermediateCode.Instruction; opcode: LONGINT);
  1949. BEGIN
  1950. EmitArithmetic3Part(instruction,Low,opcode);
  1951. IF IsComplex(instruction.op1) THEN EmitArithmetic3Part(instruction, High, opcode) END;
  1952. END EmitArithmetic3;
  1953. PROCEDURE EmitArithmetic3XMM(CONST instruction: IntermediateCode.Instruction; op32, op64: LONGINT);
  1954. VAR op: LONGINT;
  1955. BEGIN
  1956. IF instruction.op1.type.sizeInBits = 32 THEN op := op32 ELSE op := op64 END;
  1957. EmitArithmetic3Part(instruction, Low, op);
  1958. END EmitArithmetic3XMM;
  1959. PROCEDURE EmitArithmetic2(CONST instruction: IntermediateCode.Instruction; part: LONGINT; opcode: LONGINT);
  1960. VAR left:Assembler.Operand;ticket: Ticket;
  1961. BEGIN
  1962. PrepareOp2(instruction,part,left,ticket);
  1963. emitter.Emit1(opcode,left);
  1964. FinishOp(instruction.op1,part,left,ticket);
  1965. END EmitArithmetic2;
  1966. PROCEDURE EmitArithmetic2XMM(CONST instruction: IntermediateCode.Instruction; op32, op64: LONGINT);
  1967. VAR op: LONGINT;
  1968. BEGIN
  1969. IF instruction.op1.type.sizeInBits = 32 THEN op := op32 ELSE op := op64 END;
  1970. EmitArithmetic2(instruction, Low, op);
  1971. END EmitArithmetic2XMM;
  1972. PROCEDURE EmitArithmetic3FPU(CONST instruction: IntermediateCode.Instruction; op: LONGINT);
  1973. VAR op1,op2,op3: Assembler.Operand;
  1974. BEGIN
  1975. MakeOperand(instruction.op2,Low,op2,NIL);
  1976. emitter.Emit1(InstructionSet.opFLD,op2);
  1977. INC(fpStackPointer);
  1978. MakeOperand(instruction.op3,Low,op3,NIL);
  1979. IF instruction.op3.mode = IntermediateCode.ModeRegister THEN
  1980. emitter.Emit2(op,opST0,op3);
  1981. ELSE
  1982. emitter.Emit1(op,op3);
  1983. END;
  1984. MakeOperand(instruction.op1,Low,op1,NIL);
  1985. emitter.Emit1(InstructionSet.opFSTP,op1);
  1986. DEC(fpStackPointer);
  1987. END EmitArithmetic3FPU;
  1988. PROCEDURE EmitArithmetic2FPU(CONST instruction: IntermediateCode.Instruction; opcode: LONGINT);
  1989. VAR op1,op2: Assembler.Operand;
  1990. BEGIN
  1991. MakeOperand(instruction.op2,Low,op2,NIL);
  1992. emitter.Emit1(InstructionSet.opFLD,op2);
  1993. INC(fpStackPointer);
  1994. emitter.Emit0(opcode);
  1995. MakeOperand(instruction.op1,Low,op1,NIL);
  1996. emitter.Emit1(InstructionSet.opFSTP,op1);
  1997. DEC(fpStackPointer);
  1998. END EmitArithmetic2FPU;
  1999. PROCEDURE EmitMul(CONST instruction: IntermediateCode.Instruction);
  2000. VAR op1,op2,op3,temp: Assembler.Operand; ra,rd: Ticket;
  2001. value: HUGEINT; exp: LONGINT; iop3: IntermediateCode.Operand;
  2002. inst: IntermediateCode.Instruction;
  2003. BEGIN
  2004. IF IntermediateCode.IsConstantInteger(instruction.op3,value) & IntermediateBackend.PowerOf2(value,exp) THEN
  2005. IntermediateCode.InitImmediate(iop3, IntermediateCode.uint32, exp);
  2006. IntermediateCode.InitInstruction(inst, Basic.invalidPosition, IntermediateCode.shl, instruction.op1, instruction.op2, iop3);
  2007. EmitShift(inst);
  2008. RETURN;
  2009. END;
  2010. ASSERT(~IsComplex(instruction.op1));
  2011. ASSERT(instruction.op1.type.form IN IntermediateCode.Integer);
  2012. IF (instruction.op1.type.sizeInBits = IntermediateCode.Bits8) THEN
  2013. Spill(physicalRegisters.Mapped(AL));
  2014. Spill(physicalRegisters.Mapped(AH));
  2015. ra := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int8,AL,inPC);
  2016. rd := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int8,AH,inPC);
  2017. MakeOperand(instruction.op1,Low,op1,NIL);
  2018. MakeOperand(instruction.op2,Low,op2,ra);
  2019. IF instruction.op3.mode = IntermediateCode.ModeImmediate THEN
  2020. MakeOperand(instruction.op3,Low,op3,rd);
  2021. ELSE
  2022. MakeOperand(instruction.op3,Low,op3,NIL);
  2023. END;
  2024. emitter.Emit1(InstructionSet.opIMUL,op3);
  2025. emitter.Emit2(InstructionSet.opMOV,op1,opAL);
  2026. UnmapTicket(ra);
  2027. UnmapTicket(rd);
  2028. ELSE
  2029. MakeOperand(instruction.op1,Low,op1,NIL);
  2030. MakeOperand(instruction.op2,Low,op2,NIL);
  2031. MakeOperand(instruction.op3,Low,op3,NIL);
  2032. IF ~Assembler.IsRegisterOperand(op1) THEN
  2033. temp := op1;
  2034. ra := TemporaryTicket(instruction.op1.registerClass,instruction.op1.type);
  2035. TicketToOperand(ra,op1);
  2036. END;
  2037. IF Assembler.SameOperand(op1,op3) THEN temp := op2; op2 := op3; op3 := temp END;
  2038. IF Assembler.IsRegisterOperand(op2) OR Assembler.IsMemoryOperand(op2) THEN
  2039. IF Assembler.IsImmediateOperand(op3) THEN
  2040. emitter.Emit3(InstructionSet.opIMUL,op1,op2,op3);
  2041. ELSIF Assembler.IsRegisterOperand(op2) & (op2.register = op1.register) THEN
  2042. IF Assembler.IsRegisterOperand(op3) OR Assembler.IsMemoryOperand(op3) THEN
  2043. emitter.Emit2(InstructionSet.opIMUL,op1,op3);
  2044. ELSE
  2045. rd := TemporaryTicket(instruction.op1.registerClass,instruction.op1.type);
  2046. TicketToOperand(rd,temp);
  2047. Move(temp,op3,instruction.op1.type);
  2048. emitter.Emit2(InstructionSet.opIMUL,op1,temp);
  2049. UnmapTicket(rd);
  2050. END;
  2051. ELSE
  2052. Move(op1,op3,PhysicalOperandType(op1));
  2053. emitter.Emit2(InstructionSet.opIMUL,op1,op2);
  2054. END
  2055. ELSIF Assembler.IsRegisterOperand(op3) OR Assembler.IsMemoryOperand(op3) THEN
  2056. IF Assembler.IsImmediateOperand(op2) THEN
  2057. emitter.Emit3(InstructionSet.opIMUL,op1,op3,op2);
  2058. ELSIF Assembler.IsRegisterOperand(op3) & (op2.register = op1.register) THEN
  2059. IF Assembler.IsRegisterOperand(op2) OR Assembler.IsMemoryOperand(op2) THEN
  2060. emitter.Emit2(InstructionSet.opIMUL,op1,op2);
  2061. ELSE
  2062. rd := TemporaryTicket(instruction.op1.registerClass,instruction.op1.type);
  2063. TicketToOperand(rd,temp);
  2064. Move(temp,op2,instruction.op1.type);
  2065. emitter.Emit2(InstructionSet.opIMUL,op1,temp);
  2066. UnmapTicket(rd);
  2067. END;
  2068. ELSE
  2069. Move(op1,op2,PhysicalOperandType(op1));
  2070. emitter.Emit2(InstructionSet.opIMUL,op1,op3);
  2071. END;
  2072. END;
  2073. IF ra # NIL THEN
  2074. Move(temp,op1,PhysicalOperandType(op1));
  2075. UnmapTicket(ra);
  2076. END;
  2077. END;
  2078. END EmitMul;
  2079. PROCEDURE EmitDivMod(CONST instruction: IntermediateCode.Instruction);
  2080. VAR
  2081. dividend,quotient,remainder,imm,target,memop: Assembler.Operand;
  2082. op1,op2,op3: Assembler.Operand; ra,rd: Ticket;
  2083. size: LONGINT;
  2084. value: HUGEINT; exp: LONGINT; iop3: IntermediateCode.Operand;
  2085. inst: IntermediateCode.Instruction;
  2086. BEGIN
  2087. IF IntermediateCode.IsConstantInteger(instruction.op3,value) & IntermediateBackend.PowerOf2(value,exp) THEN
  2088. IF instruction.opcode = IntermediateCode.div THEN
  2089. IntermediateCode.InitImmediate(iop3, IntermediateCode.uint32, exp);
  2090. IntermediateCode.InitInstruction(inst, Basic.invalidPosition, IntermediateCode.shr, instruction.op1, instruction.op2, iop3);
  2091. EmitShift(inst);
  2092. RETURN;
  2093. ELSE
  2094. IntermediateCode.InitImmediate(iop3, instruction.op3.type, value-1);
  2095. IntermediateCode.InitInstruction(inst, Basic.invalidPosition, IntermediateCode.and, instruction.op1, instruction.op2, iop3);
  2096. EmitArithmetic3(inst,InstructionSet.opAND);
  2097. RETURN;
  2098. END;
  2099. END;
  2100. (*
  2101. In general it must obviously hold that
  2102. a = (a div b) * b + a mod b and
  2103. for all integers a,b#0, and c.
  2104. For positive numbers a and b this holds if
  2105. a div b = max{integer i: i*b <= b} = Entier(a/b)
  2106. and
  2107. a mod b = a-(a div b)*b = min{c >=0: c = a-i*b, integer i}
  2108. Example
  2109. 11 div 3 = 3 (3*3 = 9)
  2110. 11 mod 3 = 2 (=11-9)
  2111. for negative a there are two definitions for mod possible:
  2112. (i) mathematical definition with
  2113. a mod b >= 0:
  2114. a mod b = min{ c >=0: c = a-i*b, integer i} >= 0
  2115. this corresponds with rounding down
  2116. a div b = Entier(a/b) <= a/b
  2117. (ii) symmetric definition with
  2118. (-a) mod' b = -(a mod' b) and
  2119. (-a) div' b = -(a div' b)
  2120. corresponding with rounding to zero
  2121. a div' b = RoundToZero(a/b)
  2122. Examples
  2123. (i) -11 div 3 = -4 (3*(-4) = -12)
  2124. -11 mod 3 = 1 (=-11-(-12))
  2125. (ii) -11 div' 3 = -(11 div 3) = -3 (3*(-3)= -9)
  2126. -11 mod' 3 = -2 (=-11-(-9))
  2127. The behaviour for negative b can, in the symmetrical case, be deduced as
  2128. (ii) symmetric definition
  2129. a div' (-b) = (-a) div' b = -(a div' b)
  2130. a mod' (-b) = a- a div' (-b) * (-b) = a mod' b
  2131. In the mathematical case it is not so easy. It turns out that the definitions
  2132. a DIV b = Entier(a/b) = max{integer i: i*b <= b}
  2133. and
  2134. a MOD b = min { c >=0 : c = a-i*b, integer i} >= 0
  2135. are not compliant with
  2136. a = (a DIV b) * b + a MOD b
  2137. if b <= 0.
  2138. Proof: assume that b<0, then
  2139. a - Entier(a/b) * b >= 0
  2140. <=_> a >= Entier(a/b) * b
  2141. <=> Entier(a/b) >= a/b (contradiction to definition of Entier).
  2142. OBERON ADOPTS THE MATHEMATICAL DEFINITION !
  2143. For integers a and b (b>0) it holds that
  2144. a DIV b = Entier(a/b) <= a/b
  2145. a MOD b = min{ c >=0: c = b-i*a, integer i} = a - a DIV b * b
  2146. The behaviour for b < 0 is explicitely undefined.
  2147. *)
  2148. (*
  2149. AX / regMem8 = AL (remainder AH)
  2150. DX:AX / regmem16 = AX (remainder DX)
  2151. EDX:EAX / regmem32 = EAX (remainder EDX)
  2152. RDX:EAX / regmem64 = RAX (remainder RDX)
  2153. 1.) EAX <- source1
  2154. 2.) CDQ
  2155. 3.) IDIV source2
  2156. 3.) SHL EDX
  2157. 4.) SBB EAX,1
  2158. result is in EAX
  2159. *)
  2160. MakeOperand(instruction.op2,Low,op2,NIL);
  2161. CASE instruction.op1.type.sizeInBits OF
  2162. IntermediateCode.Bits8:
  2163. Spill(physicalRegisters.Mapped(AL)); ra := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int8,AL,inPC);
  2164. emitter.Emit2(InstructionSet.opMOV,opAL,op2);
  2165. dividend := opAX;
  2166. quotient := opAL;
  2167. remainder := opAH;
  2168. emitter.Emit0(InstructionSet.opCBW);
  2169. | IntermediateCode.Bits16:
  2170. Spill(physicalRegisters.Mapped(AX)); ra := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int16,AX,inPC);
  2171. emitter.Emit2(InstructionSet.opMOV,opAX,op2);
  2172. Spill(physicalRegisters.Mapped(DX)); rd := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int16,DX,inPC);
  2173. dividend := opAX;
  2174. quotient := dividend;
  2175. remainder := opDX;
  2176. emitter.Emit0(InstructionSet.opCWD);
  2177. | IntermediateCode.Bits32:
  2178. Spill(physicalRegisters.Mapped(EAX)); ra := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int32,EAX,inPC);
  2179. emitter.Emit2(InstructionSet.opMOV,opEAX,op2);
  2180. Spill(physicalRegisters.Mapped(EDX)); rd := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int32,EDX,inPC);
  2181. dividend := opEAX;
  2182. quotient := dividend;
  2183. remainder := opEDX;
  2184. emitter.Emit0(InstructionSet.opCDQ);
  2185. | IntermediateCode.Bits64:
  2186. Spill(physicalRegisters.Mapped(RAX)); ra := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int64,RAX,inPC);
  2187. emitter.Emit2(InstructionSet.opMOV,opRA,op2);
  2188. Spill(physicalRegisters.Mapped(RDX)); rd := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int64,RDX,inPC);
  2189. dividend := opRA;
  2190. quotient := dividend;
  2191. remainder := registerOperands[RDX];
  2192. emitter.Emit0(InstructionSet.opCQO);
  2193. END;
  2194. (* registers might have been changed, so we make the operands now *)
  2195. MakeOperand(instruction.op1,Low,op1,NIL);
  2196. MakeOperand(instruction.op2,Low,op2,NIL);
  2197. MakeOperand(instruction.op3,Low,op3,NIL);
  2198. IF instruction.op3.mode = IntermediateCode.ModeImmediate THEN
  2199. size := instruction.op3.type.sizeInBits DIV 8;
  2200. Basic.Align(size, 4 (* cpuBits DIV 8 *) );
  2201. AllocateStack(size);
  2202. Assembler.InitMem(memop,SHORT(instruction.op3.type.sizeInBits DIV 8),SP,0);
  2203. emitter.Emit2(InstructionSet.opMOV,memop,op3);
  2204. op3 := memop;
  2205. END;
  2206. emitter.Emit1(InstructionSet.opIDIV,op3);
  2207. IF instruction.opcode = IntermediateCode.mod THEN
  2208. imm := Assembler.NewImm8 (0);
  2209. emitter.Emit2(InstructionSet.opCMP, remainder, imm);
  2210. Assembler.InitImm8(target,0);
  2211. emitter.Emit1(InstructionSet.opJGE, target);
  2212. emitter.Emit2( InstructionSet.opADD, remainder, op3);
  2213. emitter.code.PutByteAt(target.pc,(emitter.code.pc -target.pc )-1);
  2214. emitter.Emit2(InstructionSet.opMOV, op1, remainder);
  2215. ELSE
  2216. imm := Assembler.NewImm8 (1);
  2217. emitter.Emit2(InstructionSet.opSHL, remainder, imm);
  2218. imm := Assembler.NewImm8 (0);
  2219. emitter.Emit2(InstructionSet.opSBB, quotient, imm);
  2220. emitter.Emit2(InstructionSet.opMOV, op1, quotient);
  2221. END;
  2222. IF instruction.op3.mode = IntermediateCode.ModeImmediate THEN
  2223. size := instruction.op3.type.sizeInBits DIV 8;
  2224. Basic.Align(size, 4 (* cpuBits DIV 8*) );
  2225. AllocateStack(-size);
  2226. END;
  2227. END EmitDivMod;
  2228. PROCEDURE EmitShift(CONST instruction: IntermediateCode.Instruction);
  2229. VAR
  2230. shift: Assembler.Operand;
  2231. op: LONGINT;
  2232. op1,op2,op3,dest,temporary,op1High,op2High: Assembler.Operand;
  2233. index: SHORTINT; temp: Assembler.Operand;
  2234. left: BOOLEAN;
  2235. ecx,ticket: Ticket;
  2236. BEGIN
  2237. Assert(instruction.op1.type.form IN IntermediateCode.Integer,"must be integer operand");
  2238. IF instruction.op1.type.form = IntermediateCode.UnsignedInteger THEN
  2239. IF instruction.opcode = IntermediateCode.shr THEN op := InstructionSet.opSHR; left := FALSE;
  2240. ELSIF instruction.opcode = IntermediateCode.shl THEN op := InstructionSet.opSHL; left := TRUE;
  2241. ELSIF instruction.opcode = IntermediateCode.ror THEN op := InstructionSet.opROR; left := FALSE;
  2242. ELSIF instruction.opcode = IntermediateCode.rol THEN op := InstructionSet.opROL; left := TRUE;
  2243. END;
  2244. ELSE
  2245. IF instruction.opcode = IntermediateCode.shr THEN op := InstructionSet.opSAR; left := FALSE;
  2246. ELSIF instruction.opcode = IntermediateCode.shl THEN op := InstructionSet.opSAL; left := TRUE;
  2247. ELSIF instruction.opcode = IntermediateCode.ror THEN op := InstructionSet.opROR; left := FALSE;
  2248. ELSIF instruction.opcode = IntermediateCode.rol THEN op := InstructionSet.opROL; left := TRUE;
  2249. END;
  2250. END;
  2251. IF instruction.op3.mode # IntermediateCode.ModeImmediate THEN
  2252. IF backend.cooperative THEN ap.spillable := TRUE END;
  2253. Spill(physicalRegisters.Mapped(ECX));
  2254. ecx := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int32,ECX,inPC);
  2255. END;
  2256. (*GetTemporaryRegister(instruction.op2.type,dest);*)
  2257. MakeOperand(instruction.op1,Low,op1,NIL);
  2258. IF ~Assembler.IsRegisterOperand(op1) THEN GetTemporaryRegister(instruction.op2.type,dest) ELSE dest := op1 END;
  2259. MakeOperand(instruction.op2,Low,op2,NIL);
  2260. MakeOperand(instruction.op3,Low,op3,NIL);
  2261. IF instruction.op3.mode = IntermediateCode.ModeImmediate THEN
  2262. Assembler.InitImm8(shift,instruction.op3.intValue);
  2263. ELSE
  2264. CASE instruction.op3.type.sizeInBits OF
  2265. IntermediateCode.Bits8: index := CL;
  2266. |IntermediateCode.Bits16: index := CX;
  2267. |IntermediateCode.Bits32: index := ECX;
  2268. |IntermediateCode.Bits64: index := RCX;
  2269. END;
  2270. (*
  2271. IF (physicalRegisters.toVirtual[index] # free) & ((physicalRegisters.toVirtual[index] # instruction.op1.register) OR (instruction.op1.mode # IntermediateCode.ModeRegister)) THEN
  2272. Spill();
  2273. (*
  2274. emitter.Emit1(InstructionSet.opPUSH,opECX);
  2275. ecxPushed := TRUE;
  2276. *)
  2277. END;
  2278. *)
  2279. ticket := virtualRegisters.Mapped(instruction.op3.register,Low);
  2280. IF (instruction.op3.mode # IntermediateCode.ModeRegister) OR (ticket = NIL) OR (ticket.spilled) OR (ticket.register # index) THEN
  2281. emitter.Emit2(InstructionSet.opMOV,registerOperands[index],op3);
  2282. END;
  2283. shift := opCL;
  2284. END;
  2285. IF ~IsComplex(instruction.op1) THEN
  2286. Move(dest,op2,PhysicalOperandType(dest));
  2287. emitter.Emit2 (op, dest,shift);
  2288. Move(op1,dest,PhysicalOperandType(op1));
  2289. ELSIF left THEN
  2290. MakeOperand(instruction.op1,High,op1High,NIL);
  2291. MakeOperand(instruction.op2,High,op2High,NIL);
  2292. IF ~IntermediateCode.OperandEquals(instruction.op1,instruction.op2) THEN
  2293. Move(op1,op2,PhysicalOperandType(op1));
  2294. Move(op1High,op2High,PhysicalOperandType(op1High))
  2295. END;
  2296. IF (instruction.opcode=IntermediateCode.rol) THEN
  2297. (* |high| <- |low| <- |temp=high| *)
  2298. ticket := TemporaryTicket(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int32);
  2299. TicketToOperand(ticket,temp);
  2300. emitter.Emit2( InstructionSet.opMOV, temp, op1High);
  2301. emitter.Emit3( InstructionSet.opSHLD,op1High, op1, shift);
  2302. emitter.Emit3( InstructionSet.opSHLD, op1, temp, shift);
  2303. UnmapTicket(ticket);
  2304. ELSE
  2305. (* |high| <- |low| *)
  2306. emitter.Emit3( InstructionSet.opSHLD, op1,op1High,shift);
  2307. emitter.Emit2( op, op1,shift);
  2308. END;
  2309. ELSE
  2310. IF ~IntermediateCode.OperandEquals(instruction.op1,instruction.op2) THEN
  2311. Move(op1,op2,PhysicalOperandType(op1))
  2312. END;
  2313. IF instruction.opcode=IntermediateCode.ror THEN
  2314. (* |temp=low| -> |high| -> |low| *)
  2315. ticket := TemporaryTicket(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int32);
  2316. TicketToOperand(ticket,temp);
  2317. emitter.Emit2( InstructionSet.opMOV, temporary, op1);
  2318. emitter.Emit3( InstructionSet.opSHRD,op1, op1High, shift);
  2319. emitter.Emit3( InstructionSet.opSHRD, op1High, temporary, shift);
  2320. UnmapTicket(ticket);
  2321. ELSE
  2322. (* |high| -> |low| *)
  2323. emitter.Emit3( InstructionSet.opSHRD, op1,op1High,shift);
  2324. emitter.Emit2( op, op1High, shift);
  2325. END;
  2326. END;
  2327. IF backend.cooperative & (instruction.op3.mode # IntermediateCode.ModeImmediate) THEN
  2328. UnmapTicket(ecx);
  2329. UnSpill(ap);
  2330. ap.spillable := FALSE;
  2331. END;
  2332. END EmitShift;
  2333. PROCEDURE EmitCas(CONST instruction: IntermediateCode.Instruction);
  2334. VAR ra: Ticket; op1,op2,op3,mem: Assembler.Operand; register: LONGINT;
  2335. BEGIN
  2336. CASE instruction.op2.type.sizeInBits OF
  2337. | IntermediateCode.Bits8: register := AL;
  2338. | IntermediateCode.Bits16: register := AX;
  2339. | IntermediateCode.Bits32: register := EAX;
  2340. | IntermediateCode.Bits64: register := RAX;
  2341. END;
  2342. Spill(physicalRegisters.Mapped(register));
  2343. ra := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,instruction.op2.type,register,inPC);
  2344. IF IntermediateCode.OperandEquals (instruction.op2,instruction.op3) THEN
  2345. MakeOperand(instruction.op1,Low,op1,ra);
  2346. Assembler.InitMem(mem,SHORT(instruction.op1.type.sizeInBits DIV 8),op1.register,0);
  2347. emitter.Emit2(InstructionSet.opMOV,op1,mem);
  2348. ELSE
  2349. MakeOperand(instruction.op2,Low,op2,ra);
  2350. MakeRegister(instruction.op1,Low,op1);
  2351. Assembler.InitMem(mem,SHORT(instruction.op2.type.sizeInBits DIV 8),op1.register,0);
  2352. MakeRegister(instruction.op3,Low,op3);
  2353. emitter.EmitPrefix (InstructionSet.prfLOCK);
  2354. emitter.Emit2(InstructionSet.opCMPXCHG,mem,op3);
  2355. END;
  2356. END EmitCas;
  2357. PROCEDURE EmitCopy(CONST instruction: IntermediateCode.Instruction);
  2358. VAR op1,op2,op3: Assembler.Operand; rs, rd, rc, t: Ticket; temp,imm: Assembler.Operand; source, dest: IntermediateCode.Operand; size: HUGEINT;
  2359. BEGIN
  2360. IF IntermediateCode.IsConstantInteger(instruction.op3, size) & (size = 4) THEN
  2361. Spill(physicalRegisters.Mapped(RS));
  2362. Spill(physicalRegisters.Mapped(RD));
  2363. rs := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,instruction.op1.type,RS,inPC);
  2364. rd := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,instruction.op1.type,RD,inPC);
  2365. MakeOperand(instruction.op1,Low,op1,rd);
  2366. MakeOperand(instruction.op2,Low,op2,rs);
  2367. emitter.Emit0(InstructionSet.opMOVSD);
  2368. UnmapTicket(rs);
  2369. UnmapTicket(rd);
  2370. ELSE
  2371. Spill(physicalRegisters.Mapped(RS));
  2372. Spill(physicalRegisters.Mapped(RD));
  2373. IF backend.cooperative THEN ap.spillable := TRUE END;
  2374. Spill(physicalRegisters.Mapped(RC));
  2375. rs := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,instruction.op1.type,RS,inPC);
  2376. rd := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,instruction.op1.type,RD,inPC);
  2377. rc := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,instruction.op1.type,RC,inPC);
  2378. MakeOperand(instruction.op1,Low,op1,rd);
  2379. MakeOperand(instruction.op2,Low,op2,rs);
  2380. IF (instruction.op1.mode = IntermediateCode.ModeRegister) & (instruction.op1.register = IntermediateCode.SP) & IntermediateCode.IsConstantInteger(instruction.op3, size) & (size >= 4096) THEN
  2381. (* special case on stack: copy downwards for possible stack allocation *)
  2382. IF size MOD 4 # 0 THEN
  2383. imm := Assembler.NewImm32(size-1);
  2384. emitter.Emit2(InstructionSet.opADD, opRDI, imm);
  2385. emitter.Emit2(InstructionSet.opADD, opRSI, imm);
  2386. imm := Assembler.NewImm32(size MOD 4);
  2387. emitter.Emit2(InstructionSet.opMOV, opRC, imm);
  2388. emitter.Emit0(InstructionSet.opSTD); (* copy down *)
  2389. emitter.EmitPrefix (InstructionSet.prfREP);
  2390. emitter.Emit0(InstructionSet.opMOVSB);
  2391. imm := Assembler.NewImm32(size DIV 4);
  2392. emitter.Emit2(InstructionSet.opMOV, opRC, imm);
  2393. emitter.EmitPrefix (InstructionSet.prfREP);
  2394. emitter.Emit0(InstructionSet.opMOVSD);
  2395. ELSE
  2396. imm := Assembler.NewImm32(size-4);
  2397. emitter.Emit2(InstructionSet.opADD, opRDI, imm);
  2398. emitter.Emit2(InstructionSet.opADD, opRSI, imm);
  2399. imm := Assembler.NewImm32(size DIV 4);
  2400. emitter.Emit2(InstructionSet.opMOV, opRC, imm);
  2401. emitter.Emit0(InstructionSet.opSTD); (* copy down *)
  2402. emitter.EmitPrefix (InstructionSet.prfREP);
  2403. emitter.Emit0(InstructionSet.opMOVSD);
  2404. END
  2405. ELSIF IntermediateCode.IsConstantInteger(instruction.op3, size) THEN
  2406. imm := Assembler.NewImm32(size DIV 4);
  2407. emitter.Emit2(InstructionSet.opMOV, opRC, imm);
  2408. emitter.Emit0(InstructionSet.opCLD); (* copy upwards *)
  2409. emitter.EmitPrefix (InstructionSet.prfREP);
  2410. emitter.Emit0(InstructionSet.opMOVSD);
  2411. IF size MOD 4 # 0 THEN
  2412. imm := Assembler.NewImm32(size MOD 4);
  2413. emitter.Emit2(InstructionSet.opMOV, opRC, imm);
  2414. emitter.EmitPrefix (InstructionSet.prfREP);
  2415. emitter.Emit0(InstructionSet.opMOVSB);
  2416. END;
  2417. (* this does not work in the kernel -- for whatever reasons *)
  2418. ELSIF (instruction.op1.mode = IntermediateCode.ModeRegister) & (instruction.op1.register = IntermediateCode.SP) THEN
  2419. MakeOperand(instruction.op3,Low,op3,rc);
  2420. t := TemporaryTicket(IntermediateCode.GeneralPurposeRegister, instruction.op1.type);
  2421. TicketToOperand(t, temp);
  2422. emitter.Emit2(InstructionSet.opADD, opRSI, opRC);
  2423. emitter.Emit2(InstructionSet.opADD, opRDI, opRC);
  2424. imm := Assembler.NewImm8(1);
  2425. emitter.Emit2(InstructionSet.opSUB, opRSI, imm);
  2426. emitter.Emit2(InstructionSet.opSUB, opRDI, imm);
  2427. emitter.Emit2(InstructionSet.opMOV, temp, opRC);
  2428. imm := Assembler.NewImm8(3);
  2429. emitter.Emit2(InstructionSet.opAND, opRC, imm);
  2430. emitter.Emit0(InstructionSet.opSTD); (* copy downwards *)
  2431. emitter.EmitPrefix (InstructionSet.prfREP);
  2432. emitter.Emit0(InstructionSet.opMOVSB);
  2433. imm := Assembler.NewImm8(2);
  2434. emitter.Emit2(InstructionSet.opMOV, opRC, temp);
  2435. emitter.Emit2(InstructionSet.opSHR, opRC, imm);
  2436. imm := Assembler.NewImm8(3);
  2437. emitter.Emit2(InstructionSet.opSUB, opRSI, imm);
  2438. emitter.Emit2(InstructionSet.opSUB, opRDI, imm);
  2439. emitter.EmitPrefix (InstructionSet.prfREP);
  2440. emitter.Emit0(InstructionSet.opMOVSD);
  2441. emitter.Emit0(InstructionSet.opCLD);
  2442. ELSE
  2443. MakeOperand(instruction.op3,Low,op3,rc);
  2444. t := TemporaryTicket(IntermediateCode.GeneralPurposeRegister, instruction.op1.type);
  2445. TicketToOperand(t, temp);
  2446. emitter.Emit2(InstructionSet.opMOV, temp, opRC);
  2447. imm := Assembler.NewImm8(3);
  2448. emitter.Emit2(InstructionSet.opAND, temp, imm);
  2449. imm := Assembler.NewImm8(2);
  2450. emitter.Emit2(InstructionSet.opSHR, opRC, imm);
  2451. emitter.Emit0(InstructionSet.opCLD); (* copy upwards *)
  2452. emitter.EmitPrefix (InstructionSet.prfREP);
  2453. emitter.Emit0(InstructionSet.opMOVSD);
  2454. emitter.Emit2(InstructionSet.opMOV, opRC, temp);
  2455. emitter.EmitPrefix (InstructionSet.prfREP);
  2456. emitter.Emit0(InstructionSet.opMOVSB);
  2457. END;
  2458. UnmapTicket(rs);
  2459. UnmapTicket(rd);
  2460. UnmapTicket(rc);
  2461. IF backend.cooperative THEN
  2462. UnSpill(ap);
  2463. ap.spillable := FALSE;
  2464. END;
  2465. END;
  2466. END EmitCopy;
  2467. PROCEDURE EmitFill(CONST instruction: IntermediateCode.Instruction; down: BOOLEAN);
  2468. VAR reg,sizeInBits,i: LONGINT;val, value, size, dest: Assembler.Operand;
  2469. op: LONGINT;
  2470. rd, rc: Ticket;
  2471. BEGIN
  2472. IF FALSE & (instruction.op2.mode = IntermediateCode.ModeImmediate) & (instruction.op2.symbol.name = "") & (instruction.op2.intValue < 5) THEN
  2473. sizeInBits := instruction.op3.type.sizeInBits;
  2474. IF sizeInBits = IntermediateCode.Bits8 THEN value := opAL;
  2475. ELSIF sizeInBits = IntermediateCode.Bits16 THEN value := opAX;
  2476. ELSIF sizeInBits = IntermediateCode.Bits32 THEN value := opEAX;
  2477. ELSE HALT(200)
  2478. END;
  2479. MakeOperand(instruction.op1,Low,dest,NIL);
  2480. IF instruction.op1.mode = IntermediateCode.ModeRegister THEN reg := dest.register
  2481. ELSE emitter.Emit2(InstructionSet.opMOV,opEDX,dest); reg := EDX;
  2482. END;
  2483. IF (instruction.op3.mode = IntermediateCode.ModeImmediate) & (instruction.op3.type.form IN IntermediateCode.Integer) & (instruction.op3.intValue = 0) THEN
  2484. emitter.Emit2(InstructionSet.opXOR,opEAX,opEAX);
  2485. ELSE
  2486. MakeOperand(instruction.op3,Low,value,NIL);
  2487. END;
  2488. FOR i := 0 TO SHORT(instruction.op2.intValue)-1 DO
  2489. IF down THEN
  2490. Assembler.InitMem(dest,SHORT(SHORT(sizeInBits DIV 8)),reg,-i*sizeInBits DIV 8);
  2491. ELSE
  2492. Assembler.InitMem(dest,SHORT(SHORT(sizeInBits DIV 8 )),reg,i*sizeInBits DIV 8);
  2493. END;
  2494. emitter.Emit2(InstructionSet.opMOV,dest,value);
  2495. END;
  2496. ELSE
  2497. Spill(physicalRegisters.Mapped(RD));
  2498. IF backend.cooperative THEN ap.spillable := TRUE END;
  2499. Spill(physicalRegisters.Mapped(RC));
  2500. rd := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,instruction.op1.type,RD,inPC);
  2501. rc := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,instruction.op1.type,RC,inPC);
  2502. MakeOperand(instruction.op1,Low,dest,rd);
  2503. MakeOperand(instruction.op2,Low,size,rc);
  2504. MakeOperand(instruction.op3,Low,value,NIL);
  2505. (*
  2506. emitter.Emit2(InstructionSet.opMOV,opRDI, op1[Low]);
  2507. emitter.Emit2(InstructionSet.opMOV,opRC, op3[Low]);
  2508. *)
  2509. CASE instruction.op3.type.sizeInBits OF
  2510. IntermediateCode.Bits8: val := opAL; op := InstructionSet.opSTOSB;
  2511. |IntermediateCode.Bits16: val := opAX; op := InstructionSet.opSTOSW;
  2512. |IntermediateCode.Bits32: val := opEAX; op := InstructionSet.opSTOSD;
  2513. ELSE Halt("only supported for upto 32 bit integers ");
  2514. END;
  2515. IF (instruction.op3.mode = IntermediateCode.ModeImmediate) & (instruction.op3.type.form IN IntermediateCode.Integer) & (instruction.op3.intValue = 0) THEN
  2516. emitter.Emit2(InstructionSet.opXOR,opEAX,opEAX);
  2517. ELSE
  2518. emitter.Emit2(InstructionSet.opMOV,val,value);
  2519. END;
  2520. IF down THEN
  2521. emitter.Emit0(InstructionSet.opSTD); (* fill downwards *)
  2522. ELSE
  2523. emitter.Emit0(InstructionSet.opCLD); (* fill upwards *)
  2524. END;
  2525. emitter.EmitPrefix (InstructionSet.prfREP);
  2526. emitter.Emit0(op);
  2527. IF down THEN (* needed as calls to windows crash otherwise *)
  2528. emitter.Emit0(InstructionSet.opCLD);
  2529. END;
  2530. UnmapTicket(rc);
  2531. IF backend.cooperative THEN
  2532. UnSpill(ap);
  2533. ap.spillable := FALSE;
  2534. END;
  2535. END;
  2536. END EmitFill;
  2537. PROCEDURE EmitBr (CONST instruction: IntermediateCode.Instruction);
  2538. VAR dest,destPC,offset: LONGINT; target: Assembler.Operand;hit,fail: LONGINT; reverse: BOOLEAN;
  2539. (* jump operands *) left,right,temp: Assembler.Operand;
  2540. failOp: Assembler.Operand; failPC: LONGINT;
  2541. PROCEDURE JmpDest(brop: LONGINT);
  2542. BEGIN
  2543. IF instruction.op1.mode = IntermediateCode.ModeImmediate THEN
  2544. IF instruction.op1.symbol.name = in.name THEN
  2545. dest := (instruction.op1.symbolOffset); (* this is the offset in the in-data section (intermediate code), it is not byte- *)
  2546. destPC := (in.instructions[dest].pc );
  2547. offset := destPC - (out.pc );
  2548. IF dest > inPC THEN (* forward jump *)
  2549. Assembler.InitOffset32(target,0);
  2550. Assembler.SetSymbol(target,instruction.op1.symbol.name,instruction.op1.symbol.fingerprint,instruction.op1.symbolOffset,instruction.op1.offset);
  2551. emitter.Emit1(brop,target);
  2552. ELSIF ABS(offset) <= 126 THEN
  2553. Assembler.InitOffset8(target,destPC);
  2554. emitter.Emit1(brop,target);
  2555. ELSE
  2556. Assembler.InitOffset32(target,destPC);
  2557. emitter.Emit1(brop,target);
  2558. END;
  2559. ELSIF cpuBits = 64 THEN
  2560. MakeOperand(instruction.op1,Low,target,NIL);
  2561. emitter.Emit1(brop,target);
  2562. ELSE
  2563. Assembler.InitOffset32(target,instruction.op1.intValue);
  2564. Assembler.SetSymbol(target,instruction.op1.symbol.name,instruction.op1.symbol.fingerprint,instruction.op1.symbolOffset,instruction.op1.offset);
  2565. emitter.Emit1(brop,target);
  2566. END;
  2567. ELSE
  2568. MakeOperand(instruction.op1,Low,target,NIL);
  2569. emitter.Emit1(brop,target);
  2570. END;
  2571. END JmpDest;
  2572. PROCEDURE CmpFloat;
  2573. BEGIN
  2574. IF backend.forceFPU THEN
  2575. MakeOperand(instruction.op2,Low,left,NIL);
  2576. emitter.Emit1(InstructionSet.opFLD,left); INC(fpStackPointer);
  2577. MakeOperand(instruction.op3,Low,right,NIL);
  2578. emitter.Emit1(InstructionSet.opFCOMP,right); DEC(fpStackPointer);
  2579. emitter.Emit1(InstructionSet.opFNSTSW,opAX);
  2580. emitter.Emit0(InstructionSet.opSAHF);
  2581. ELSE
  2582. MakeRegister(instruction.op2,Low,left);
  2583. MakeOperand(instruction.op3,Low,right,NIL);
  2584. IF instruction.op2.type.sizeInBits = 32 THEN
  2585. emitter.Emit2(InstructionSet.opCOMISS, left, right);
  2586. ELSE
  2587. emitter.Emit2(InstructionSet.opCOMISD, left, right);
  2588. END
  2589. END;
  2590. END CmpFloat;
  2591. PROCEDURE Cmp(part: LONGINT; VAR reverse: BOOLEAN);
  2592. VAR type: IntermediateCode.Type; left,right: Assembler.Operand;
  2593. BEGIN
  2594. IF (instruction.op2.mode = IntermediateCode.ModeImmediate) & (instruction.op3.mode = IntermediateCode.ModeImmediate) THEN
  2595. reverse := FALSE;
  2596. GetPartType(instruction.op2.type,part,type);
  2597. GetTemporaryRegister(type,temp);
  2598. MakeOperand(instruction.op2,part,left,NIL);
  2599. MakeOperand(instruction.op3,part,right,NIL);
  2600. Move(temp,left, type);
  2601. left := temp;
  2602. ELSIF instruction.op2.mode = IntermediateCode.ModeImmediate THEN
  2603. reverse := TRUE;
  2604. MakeOperand(instruction.op2,part,right,NIL);
  2605. MakeOperand(instruction.op3,part,left,NIL);
  2606. ELSIF IsMemoryOperand(instruction.op2,part) & IsMemoryOperand(instruction.op3,part) THEN
  2607. reverse := FALSE;
  2608. GetPartType(instruction.op2.type,part,type);
  2609. GetTemporaryRegister(type,temp);
  2610. MakeOperand(instruction.op2,part,left,NIL);
  2611. MakeOperand(instruction.op3,part,right,NIL);
  2612. Move(temp,right,type);
  2613. right := temp;
  2614. ELSE
  2615. reverse := FALSE;
  2616. MakeOperand(instruction.op2,part,left,NIL);
  2617. MakeOperand(instruction.op3,part,right,NIL);
  2618. END;
  2619. emitter.Emit2(InstructionSet.opCMP,left,right);
  2620. END Cmp;
  2621. BEGIN
  2622. IF (instruction.op1.symbol.name = in.name) & (instruction.op1.symbolOffset = inPC +1) THEN (* jump to next instruction can be ignored *)
  2623. IF dump # NIL THEN dump.String("jump to next instruction ignored"); dump.Ln END;
  2624. RETURN
  2625. END;
  2626. failPC := 0;
  2627. IF instruction.opcode = IntermediateCode.br THEN
  2628. hit := InstructionSet.opJMP
  2629. ELSIF instruction.op2.type.form = IntermediateCode.Float THEN
  2630. CmpFloat;
  2631. CASE instruction.opcode OF
  2632. IntermediateCode.breq: hit := InstructionSet.opJE;
  2633. |IntermediateCode.brne:hit := InstructionSet.opJNE;
  2634. |IntermediateCode.brge: hit := InstructionSet.opJAE
  2635. |IntermediateCode.brlt: hit := InstructionSet.opJB
  2636. END;
  2637. ELSE
  2638. IF ~IsComplex(instruction.op2) THEN
  2639. Cmp(Low,reverse);
  2640. CASE instruction.opcode OF
  2641. IntermediateCode.breq: hit := InstructionSet.opJE;
  2642. |IntermediateCode.brne: hit := InstructionSet.opJNE;
  2643. |IntermediateCode.brge:
  2644. IF instruction.op2.type.form = IntermediateCode.SignedInteger THEN
  2645. IF reverse THEN hit := InstructionSet.opJLE ELSE hit := InstructionSet.opJGE END;
  2646. ELSIF instruction.op2.type.form = IntermediateCode.UnsignedInteger THEN
  2647. IF reverse THEN hit := InstructionSet.opJBE ELSE hit := InstructionSet.opJAE END;
  2648. END;
  2649. |IntermediateCode.brlt:
  2650. IF instruction.op2.type.form = IntermediateCode.SignedInteger THEN
  2651. IF reverse THEN hit := InstructionSet.opJG ELSE hit := InstructionSet.opJL END;
  2652. ELSIF instruction.op2.type.form = IntermediateCode.UnsignedInteger THEN
  2653. IF reverse THEN hit := InstructionSet.opJA ELSE hit := InstructionSet.opJB END;
  2654. END;
  2655. END;
  2656. ELSE
  2657. Assert(instruction.op2.type.form = IntermediateCode.SignedInteger,"no unsigned integer64");
  2658. Cmp(High,reverse);
  2659. CASE instruction.opcode OF
  2660. IntermediateCode.breq: hit := 0; fail := InstructionSet.opJNE;
  2661. |IntermediateCode.brne: hit := InstructionSet.opJNE; fail := 0;
  2662. |IntermediateCode.brge:
  2663. IF reverse THEN hit := InstructionSet.opJL; fail := InstructionSet.opJG;
  2664. ELSE hit := InstructionSet.opJG; fail := InstructionSet.opJL
  2665. END;
  2666. |IntermediateCode.brlt:
  2667. IF reverse THEN hit := InstructionSet.opJG; fail := InstructionSet.opJL
  2668. ELSE hit := InstructionSet.opJL; fail := InstructionSet.opJG
  2669. END;
  2670. END;
  2671. IF hit # 0 THEN JmpDest(hit) END;
  2672. IF fail # 0 THEN
  2673. failPC := out.pc; (* to avoid potential value overflow problem, will be patched anyway *)
  2674. Assembler.InitOffset8(failOp,failPC );
  2675. emitter.Emit1(fail,failOp);
  2676. failPC := failOp.pc;
  2677. END;
  2678. Cmp(Low,reverse);
  2679. CASE instruction.opcode OF
  2680. IntermediateCode.breq: hit := InstructionSet.opJE
  2681. |IntermediateCode.brne: hit := InstructionSet.opJNE
  2682. |IntermediateCode.brge:
  2683. IF reverse THEN hit := InstructionSet.opJBE ELSE hit := InstructionSet.opJAE END;
  2684. |IntermediateCode.brlt:
  2685. IF reverse THEN hit := InstructionSet.opJA ELSE hit := InstructionSet.opJB END;
  2686. END;
  2687. END;
  2688. END;
  2689. JmpDest(hit);
  2690. IF failPC > 0 THEN out.PutByteAt(failPC,(out.pc-failPC)-1); END;
  2691. END EmitBr;
  2692. PROCEDURE EmitPush(CONST vop: IntermediateCode.Operand; part: LONGINT);
  2693. VAR index: LONGINT; type,cpuType: IntermediateCode.Type; op1: Assembler.Operand; ra: Ticket;
  2694. BEGIN
  2695. GetPartType(vop.type,part,type);
  2696. ASSERT(type.form IN IntermediateCode.Integer);
  2697. IF vop.mode = IntermediateCode.ModeImmediate THEN (* may not push 16 bit immediate: strange instruction in 32 / 64 bit mode *)
  2698. GetImmediate(vop,part,op1,TRUE);
  2699. emitter.Emit1(InstructionSet.opPUSH,op1);
  2700. ELSIF (type.sizeInBits = cpuBits) THEN
  2701. MakeOperand(vop,part,op1,NIL);
  2702. emitter.Emit1(InstructionSet.opPUSH,op1);
  2703. ELSE
  2704. ASSERT(type.sizeInBits < cpuBits);
  2705. MakeOperand(vop,part,op1,NIL);
  2706. IF Assembler.IsRegisterOperand(op1) & ~((cpuBits=32) & (type.sizeInBits=8) & (op1.register >= AH)) THEN
  2707. index := op1.register MOD 32 + opRA.register;
  2708. emitter.Emit1(InstructionSet.opPUSH, registerOperands[index]);
  2709. ELSE
  2710. WHILE physicalRegisters.Mapped(opRA.register) # free DO Spill(physicalRegisters.Mapped(opRA.register)) END;
  2711. IntermediateCode.InitType(cpuType,IntermediateCode.SignedInteger,SHORT(cpuBits));
  2712. ra := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,cpuType,opRA.register,inPC);
  2713. CASE type.sizeInBits OF
  2714. 8: index := AL
  2715. |16: index := AX
  2716. |32: index := EAX
  2717. |64: index := RAX
  2718. END;
  2719. emitter.Emit2(InstructionSet.opMOV,registerOperands[index],op1);
  2720. emitter.Emit1(InstructionSet.opPUSH,opRA);
  2721. UnmapTicket(ra);
  2722. END;
  2723. END;
  2724. END EmitPush;
  2725. PROCEDURE EmitPop(CONST vop: IntermediateCode.Operand; part: LONGINT);
  2726. VAR index: LONGINT; type,cpuType: IntermediateCode.Type; op1: Assembler.Operand; ra: Ticket;
  2727. BEGIN
  2728. GetPartType(vop.type,part,type);
  2729. ASSERT(type.form IN IntermediateCode.Integer);
  2730. IF (type.sizeInBits = cpuBits) THEN
  2731. MakeOperand(vop,part,op1,NIL);
  2732. emitter.Emit1(InstructionSet.opPOP,op1);
  2733. ELSE
  2734. ASSERT(type.sizeInBits < cpuBits);
  2735. MakeOperand(vop,part,op1,NIL);
  2736. IF Assembler.IsRegisterOperand(op1) & ~((cpuBits=32) & (type.sizeInBits=8) & (op1.register >= AH)) THEN
  2737. index := op1.register MOD 32 + opRA.register;
  2738. emitter.Emit1(InstructionSet.opPOP, registerOperands[index]);
  2739. ELSE
  2740. WHILE physicalRegisters.Mapped(opRA.register) # free DO Spill(physicalRegisters.Mapped(opRA.register)) END;
  2741. IntermediateCode.InitType(cpuType, IntermediateCode.SignedInteger, SHORT(cpuBits));
  2742. ra := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,cpuType,opRA.register,inPC);
  2743. emitter.Emit1(InstructionSet.opPOP,opRA);
  2744. CASE type.sizeInBits OF
  2745. 8: index := AL
  2746. |16: index := AX
  2747. |32: index := EAX
  2748. |64: index := RAX
  2749. END;
  2750. emitter.Emit2(InstructionSet.opMOV, op1, registerOperands[index]);
  2751. UnmapTicket(ra);
  2752. END;
  2753. END;
  2754. END EmitPop;
  2755. PROCEDURE EmitPushFloat(CONST vop: IntermediateCode.Operand);
  2756. VAR sizeInBytes,length: LONGINT; memop: Assembler.Operand; op: Assembler.Operand;
  2757. BEGIN
  2758. MakeOperand(vop,Low,op,NIL);
  2759. length := vop.type.length;
  2760. IF (vop.mode = IntermediateCode.ModeMemory) & (vop.type.sizeInBits*length =cpuBits) THEN
  2761. emitter.Emit1(InstructionSet.opPUSH,op);
  2762. ELSE
  2763. sizeInBytes := vop.type.sizeInBits DIV 8;
  2764. length := vop.type.length;
  2765. IF sizeInBytes * length * 8 < cpuBits THEN
  2766. AllocateStack(cpuBits DIV 8);
  2767. ELSE
  2768. AllocateStack(sizeInBytes*length);
  2769. END;
  2770. Assembler.InitMem(memop, SHORTINT(sizeInBytes*length),SP,0);
  2771. IF backend.forceFPU THEN
  2772. emitter.Emit1(InstructionSet.opFLD,op); INC(fpStackPointer);
  2773. emitter.Emit1(InstructionSet.opFSTP,memop); DEC(fpStackPointer);
  2774. ELSE
  2775. Move(memop, op, vop.type)
  2776. END
  2777. END;
  2778. END EmitPushFloat;
  2779. PROCEDURE EmitPopFloat(CONST vop: IntermediateCode.Operand);
  2780. VAR sizeInBytes,length: LONGINT; memop: Assembler.Operand; op: Assembler.Operand;
  2781. BEGIN
  2782. sizeInBytes := vop.type.sizeInBits DIV 8;
  2783. length := vop.type.length;
  2784. IF (vop.mode = IntermediateCode.ModeMemory) & (vop.type.sizeInBits*length =cpuBits) THEN
  2785. MakeOperand(vop,Low,op,NIL);
  2786. emitter.Emit1(InstructionSet.opPOP,op);
  2787. ELSE
  2788. Assembler.InitMem(memop, SHORTINT(sizeInBytes*length),SP,0);
  2789. IF backend.forceFPU THEN
  2790. emitter.Emit1(InstructionSet.opFLD,memop);
  2791. INC(fpStackPointer);
  2792. MakeOperand(vop,Low,op,NIL);
  2793. emitter.Emit1(InstructionSet.opFSTP,op);
  2794. DEC(fpStackPointer);
  2795. ASSERT(sizeInBytes > 0);
  2796. ELSE
  2797. MakeOperand(vop,Low,op,NIL);
  2798. Move(op, memop, vop.type)
  2799. END;
  2800. IF sizeInBytes * length * 8 < cpuBits THEN
  2801. AllocateStack(-cpuBits DIV 8);
  2802. ELSE
  2803. AllocateStack(-sizeInBytes*length);
  2804. END;
  2805. END;
  2806. END EmitPopFloat;
  2807. PROCEDURE EmitNeg(CONST instruction: IntermediateCode.Instruction);
  2808. VAR opLow,opHigh: Assembler.Operand; minusOne: Assembler.Operand; ticketLow,ticketHigh: Ticket;
  2809. BEGIN
  2810. IF IsComplex(instruction.op1) THEN
  2811. PrepareOp2(instruction,High,opHigh,ticketHigh);
  2812. PrepareOp2(instruction,Low,opLow,ticketLow);
  2813. emitter.Emit1(InstructionSet.opNOT,opHigh);
  2814. emitter.Emit1(InstructionSet.opNEG,opLow);
  2815. Assembler.InitImm8(minusOne,-1);
  2816. emitter.Emit2(InstructionSet.opSBB,opHigh,minusOne);
  2817. FinishOp(instruction.op1,High,opHigh,ticketHigh);
  2818. FinishOp(instruction.op1,Low,opLow,ticketLow);
  2819. ELSE
  2820. EmitArithmetic2(instruction,Low,InstructionSet.opNEG);
  2821. END;
  2822. END EmitNeg;
  2823. PROCEDURE EmitNegXMM(CONST instruction: IntermediateCode.Instruction);
  2824. VAR temp, op: Assembler.Operand; ticket: Ticket;
  2825. BEGIN
  2826. PrepareOp2(instruction, Low, op, ticket);
  2827. GetTemporaryRegister(instruction.op1.type,temp);
  2828. IF instruction.op1.type.sizeInBits = 32 THEN
  2829. emitter.Emit2(InstructionSet.opXORPS, temp, temp);
  2830. emitter.Emit2(InstructionSet.opSUBPS, temp, op);
  2831. emitter.Emit2(InstructionSet.opMOVAPS, op, temp);
  2832. ELSE
  2833. emitter.Emit2(InstructionSet.opXORPD, temp, temp);
  2834. emitter.Emit2(InstructionSet.opSUBPD, temp, op);
  2835. emitter.Emit2(InstructionSet.opMOVAPS, op, temp);
  2836. END;
  2837. FinishOp(instruction.op1, Low, op, ticket);
  2838. END EmitNegXMM;
  2839. PROCEDURE EmitAbs(CONST instruction: IntermediateCode.Instruction);
  2840. VAR op1,op2: Assembler.Operand; source,imm: Assembler.Operand; eax: Ticket;
  2841. BEGIN
  2842. Assert(~IsComplex(instruction.op1),"complex Abs not supported");
  2843. IF instruction.op1.type.form = IntermediateCode.SignedInteger THEN
  2844. Spill(physicalRegisters.Mapped(EAX));
  2845. eax := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int32,EAX,inPC);
  2846. MakeOperand(instruction.op1,Low,op1,NIL);
  2847. MakeOperand(instruction.op2,Low,op2,NIL);
  2848. CASE instruction.op1.type.sizeInBits OF
  2849. | IntermediateCode.Bits8: imm := Assembler.NewImm8 (7); source := opAL;
  2850. | IntermediateCode.Bits16: imm := Assembler.NewImm8 (15); source := opAX;
  2851. | IntermediateCode.Bits32: imm := Assembler.NewImm8 (31); source := opEAX;
  2852. | IntermediateCode.Bits64: imm := Assembler.NewImm8 (63); source := registerOperands[RAX];
  2853. END;
  2854. emitter.Emit2 (InstructionSet.opMOV, source,op2);
  2855. emitter.Emit2 (InstructionSet.opMOV, op1,source);
  2856. emitter.Emit2 (InstructionSet.opSAR, source, imm);
  2857. emitter.Emit2 (InstructionSet.opXOR, op1, source);
  2858. emitter.Emit2 (InstructionSet.opSUB, op1, source);
  2859. UnmapTicket(eax);
  2860. ELSE Halt("Abs does not make sense on unsigned integer")
  2861. END;
  2862. END EmitAbs;
  2863. PROCEDURE EmitAbsXMM(CONST instruction: IntermediateCode.Instruction);
  2864. VAR temp, op: Assembler.Operand; ticket: Ticket;
  2865. BEGIN
  2866. PrepareOp2(instruction, Low, op, ticket);
  2867. GetTemporaryRegister(instruction.op1.type,temp);
  2868. IF instruction.op1.type.sizeInBits = 32 THEN
  2869. emitter.Emit2(InstructionSet.opXORPS, temp, temp);
  2870. emitter.Emit2(InstructionSet.opSUBPS, temp, op);
  2871. emitter.Emit2(InstructionSet.opMAXPS, op, temp);
  2872. ELSE
  2873. emitter.Emit2(InstructionSet.opXORPD, temp, temp);
  2874. emitter.Emit2(InstructionSet.opSUBPD, temp, op);
  2875. emitter.Emit2(InstructionSet.opMAXPD, op, temp);
  2876. END;
  2877. FinishOp(instruction.op1, Low, op, ticket);
  2878. END EmitAbsXMM;
  2879. PROCEDURE EmitTrap(CONST instruction: IntermediateCode.Instruction);
  2880. VAR operand: Assembler.Operand;
  2881. BEGIN
  2882. IF instruction.op1.intValue < 80H THEN
  2883. operand := Assembler.NewImm8(instruction.op1.intValue);
  2884. ELSE
  2885. operand := Assembler.NewImm32(instruction.op1.intValue);
  2886. END;
  2887. emitter.Emit1(InstructionSet.opPUSH, operand);
  2888. emitter.Emit0(InstructionSet.opINT3);
  2889. END EmitTrap;
  2890. PROCEDURE EmitAsm(CONST instruction: IntermediateCode.Instruction);
  2891. VAR reader: Streams.StringReader; procedure: SyntaxTree.Procedure; scope: SyntaxTree.Scope;
  2892. len: LONGINT; symbol: SyntaxTree.Symbol; assembler: Assembler.Assembly;
  2893. inr, outr: IntermediateCode.Rules;
  2894. string: SyntaxTree.SourceCode;
  2895. i: LONGINT;
  2896. reg, dest: Assembler.Operand;
  2897. map: Assembler.RegisterMap;
  2898. register: LONGINT;
  2899. ticket: Ticket;
  2900. BEGIN
  2901. IF instruction.op2.mode = IntermediateCode.ModeRule THEN inr := instruction.op2.rule ELSE inr := NIL END;
  2902. IF instruction.op3.mode = IntermediateCode.ModeRule THEN outr := instruction.op3.rule ELSE outr := NIL END;
  2903. string := instruction.op1.string;
  2904. NEW(map);
  2905. IF inr # NIL THEN
  2906. FOR i := 0 TO LEN(inr)-1 DO
  2907. MakeRegister(inr[i], 0, reg);
  2908. ASSERT(map.Find(inr[i].string^) < 0);
  2909. map.Add(inr[i].string, reg.register)
  2910. END;
  2911. END;
  2912. IF outr # NIL THEN
  2913. FOR i := 0 TO LEN(outr)-1 DO
  2914. IF (map.Find(outr[i].string^) < 0) THEN
  2915. GetTemporaryRegister(outr[i].type,reg);
  2916. map.Add(outr[i].string, reg.register)
  2917. END;
  2918. END;
  2919. END;
  2920. len := Strings.Length(string^);
  2921. NEW(reader,len);
  2922. reader.Set(string^);
  2923. symbol := in.symbol;
  2924. procedure := symbol(SyntaxTree.Procedure);
  2925. scope := procedure.procedureScope;
  2926. NEW(assembler,diagnostics,emitter);
  2927. assembler.useLineNumbers := Compiler.UseLineNumbers IN backend.flags;
  2928. assembler.Assemble(reader,instruction.textPosition,scope,in,in,module,procedure.access * SyntaxTree.Public # {}, procedure.isInline, map) ;
  2929. error := error OR assembler.error;
  2930. IF outr # NIL THEN
  2931. FOR i := 0 TO LEN(outr)-1 DO
  2932. IF outr[i].mode # IntermediateCode.Undefined THEN
  2933. register := map.Find(outr[i].string^);
  2934. ticket := physicalRegisters.Mapped(register);
  2935. IF ticket.lastuse = inPC THEN UnmapTicket(ticket); physicalRegisters.AllocationHint(register) END; (* try to reuse register here *)
  2936. Assembler.InitRegister(reg, register);
  2937. MakeOperand(outr[i], Low, dest, NIL);
  2938. Move( dest, reg,outr[i].type)
  2939. END;
  2940. END;
  2941. END;
  2942. (*
  2943. IntermediateCode.SetString(instruction.op1, string);
  2944. *)
  2945. END EmitAsm;
  2946. END CodeGeneratorAMD64;
  2947. BackendAMD64= OBJECT (IntermediateBackend.IntermediateBackend)
  2948. VAR
  2949. cg: CodeGeneratorAMD64;
  2950. bits: LONGINT;
  2951. traceable: BOOLEAN;
  2952. forceFPU: BOOLEAN;
  2953. winAPIRegisters: ARRAY 4 OF LONGINT;
  2954. cRegisters: ARRAY 6 OF LONGINT;
  2955. PROCEDURE &InitBackendAMD64;
  2956. BEGIN
  2957. InitIntermediateBackend;
  2958. bits := 32;
  2959. forceFPU := FALSE;
  2960. winAPIRegisters[0] := RCX - RAX;
  2961. winAPIRegisters[1] := RDX - RAX;
  2962. winAPIRegisters[2] := R8 - RAX;
  2963. winAPIRegisters[3] := R9 - RAX;
  2964. cRegisters[0] := RDI - RAX;
  2965. cRegisters[1] := RSI - RAX;
  2966. cRegisters[2] := RDX - RAX;
  2967. cRegisters[3] := RCX - RAX;
  2968. cRegisters[4] := R8 - RAX;
  2969. cRegisters[5] := R9 - RAX;
  2970. SetName("AMD");
  2971. END InitBackendAMD64;
  2972. PROCEDURE Initialize*(diagnostics: Diagnostics.Diagnostics; log: Streams.Writer; flags: SET; checker: SemanticChecker.Checker; system: Global.System);
  2973. BEGIN
  2974. Initialize^(diagnostics,log, flags,checker,system); NEW(cg, runtimeModuleName, diagnostics, SELF);
  2975. END Initialize;
  2976. PROCEDURE GetSystem*(): Global.System;
  2977. VAR system: Global.System;
  2978. PROCEDURE AddRegister(CONST name: Scanner.IdentifierString; val: LONGINT);
  2979. BEGIN
  2980. Global.NewConstant(name,val,system.shortintType,system.systemScope)
  2981. END AddRegister;
  2982. PROCEDURE AddRegisters;
  2983. BEGIN
  2984. (* system constants *)
  2985. AddRegister("EAX",InstructionSet.regEAX); AddRegister("ECX", InstructionSet.regECX);
  2986. AddRegister( "EDX", InstructionSet.regEDX); AddRegister( "EBX", InstructionSet.regEBX);
  2987. AddRegister( "ESP", InstructionSet.regESP); AddRegister( "EBP", InstructionSet.regEBP);
  2988. AddRegister( "ESI", InstructionSet.regESI); AddRegister( "EDI", InstructionSet.regEDI);
  2989. AddRegister( "AX", InstructionSet.regAX); AddRegister( "CX", InstructionSet.regCX);
  2990. AddRegister( "DX", InstructionSet.regDX); AddRegister( "BX", InstructionSet.regBX);
  2991. AddRegister( "AL", InstructionSet.regAL); AddRegister( "CL", InstructionSet.regCL);
  2992. AddRegister( "DL", InstructionSet.regDL); AddRegister( "BL", InstructionSet.regBL);
  2993. AddRegister( "AH", InstructionSet.regAH); AddRegister( "CH", InstructionSet.regCH);
  2994. AddRegister( "DH", InstructionSet.regDH); AddRegister( "BH", InstructionSet.regBH);
  2995. AddRegister( "RAX", InstructionSet.regRAX); AddRegister( "RCX", InstructionSet.regRCX);
  2996. AddRegister( "RDX", InstructionSet.regRDX); AddRegister( "RBX", InstructionSet.regRBX);
  2997. AddRegister( "RSP", InstructionSet.regRSP); AddRegister( "RBP", InstructionSet.regRBP);
  2998. AddRegister( "RSI", InstructionSet.regRSI); AddRegister( "RDI", InstructionSet.regRDI);
  2999. AddRegister( "R8", InstructionSet.regR8); AddRegister( "R9", InstructionSet.regR9);
  3000. AddRegister( "R10", InstructionSet.regR10); AddRegister( "R11", InstructionSet.regR11);
  3001. AddRegister( "R12", InstructionSet.regR12); AddRegister( "R13", InstructionSet.regR13);
  3002. AddRegister( "R14", InstructionSet.regR14); AddRegister( "R15", InstructionSet.regR15);
  3003. AddRegister( "R8D", InstructionSet.regR8D); AddRegister( "R9D", InstructionSet.regR9D);
  3004. AddRegister( "R10D", InstructionSet.regR10D); AddRegister( "R11D", InstructionSet.regR11D);
  3005. AddRegister( "R12D", InstructionSet.regR12D); AddRegister( "R13D", InstructionSet.regR13D);
  3006. AddRegister( "R14D", InstructionSet.regR14D); AddRegister( "R15D", InstructionSet.regR15D);
  3007. AddRegister( "R8W", InstructionSet.regR8W); AddRegister( "R9W", InstructionSet.regR9W);
  3008. AddRegister( "R10W", InstructionSet.regR10W); AddRegister( "R11W", InstructionSet.regR11W);
  3009. AddRegister( "R12W", InstructionSet.regR12W); AddRegister( "R13W", InstructionSet.regR13W);
  3010. AddRegister( "R14W", InstructionSet.regR14W); AddRegister( "R15W", InstructionSet.regR15W);
  3011. AddRegister( "R8B", InstructionSet.regR8B); AddRegister( "R9B", InstructionSet.regR9B);
  3012. AddRegister( "R10B", InstructionSet.regR10B); AddRegister( "R11B", InstructionSet.regR11B);
  3013. AddRegister( "R12B", InstructionSet.regR12B); AddRegister( "R13B", InstructionSet.regR13B);
  3014. AddRegister( "R14B", InstructionSet.regR14B); AddRegister( "R15B", InstructionSet.regR15B);
  3015. END AddRegisters;
  3016. BEGIN
  3017. IF system = NIL THEN
  3018. IF bits=32 THEN
  3019. NEW(system,8,8,32, 8,32,32,32,64,cooperative);
  3020. Global.SetDefaultDeclarations(system,8);
  3021. Global.SetDefaultOperators(system);
  3022. ELSE
  3023. NEW(system,8,8,64,8,64,64,64,128,cooperative);
  3024. Global.SetDefaultDeclarations(system,8);
  3025. Global.SetDefaultOperators(system);
  3026. END;
  3027. system.SetRegisterPassCallback(CanPassInRegister);
  3028. AddRegisters
  3029. END;
  3030. RETURN system
  3031. END GetSystem;
  3032. (* return number of general purpose registery used as parameter register in calling convention *)
  3033. PROCEDURE NumberParameterRegisters*(callingConvention: SyntaxTree.CallingConvention): SIZE;
  3034. BEGIN
  3035. IF bits = 32 THEN
  3036. RETURN 0;
  3037. ELSE
  3038. CASE callingConvention OF
  3039. |SyntaxTree.WinAPICallingConvention: RETURN 4;
  3040. |SyntaxTree.CCallingConvention, SyntaxTree.DarwinCCallingConvention: RETURN 6;
  3041. ELSE
  3042. RETURN 0;
  3043. END;
  3044. END
  3045. END NumberParameterRegisters;
  3046. (* returns the following register (or part thereof)
  3047. 0: regRAX;
  3048. 1: regRCX;
  3049. 2: regRDX;
  3050. 3: regRBX;
  3051. 4: regRSP;
  3052. 5: regRBP;
  3053. 6: regRSI;
  3054. 7: regRDI;
  3055. 8 .. 15: regRx;
  3056. *)
  3057. PROCEDURE HardwareIntegerRegister(index: LONGINT; sizeInBits: LONGINT): LONGINT;
  3058. BEGIN
  3059. index := index MOD 32;
  3060. sizeInBits := sizeInBits DIV 8;
  3061. WHILE sizeInBits > 1 DO (* jump to register section that corresponds to the number of bits *)
  3062. INC(index,32);
  3063. sizeInBits := sizeInBits DIV 2;
  3064. END;
  3065. RETURN index
  3066. END HardwareIntegerRegister;
  3067. PROCEDURE HardwareFloatRegister(index: LONGINT; sizeInBits: LONGINT): LONGINT;
  3068. BEGIN
  3069. ASSERT((sizeInBits = 32) OR (sizeInBits = 64));
  3070. RETURN XMM0 + index;
  3071. END HardwareFloatRegister;
  3072. PROCEDURE ParameterRegister*(callingConvention: SyntaxTree.CallingConvention; type: IntermediateCode.Type; index: LONGINT): LONGINT;
  3073. VAR size: LONGINT;
  3074. BEGIN
  3075. IF type.form IN IntermediateCode.Integer THEN
  3076. CASE callingConvention OF
  3077. |SyntaxTree.WinAPICallingConvention: index := winAPIRegisters[index];
  3078. |SyntaxTree.CCallingConvention, SyntaxTree.DarwinCCallingConvention: index := cRegisters[index]
  3079. END;
  3080. RETURN HardwareIntegerRegister(RAX + index, type.sizeInBits)
  3081. ELSIF type.form = IntermediateCode.Float THEN
  3082. RETURN HardwareFloatRegister(index, type.sizeInBits)
  3083. ELSE
  3084. HALT(100);
  3085. END;
  3086. END ParameterRegister;
  3087. PROCEDURE SupportedInstruction*(CONST instruction: IntermediateCode.Instruction; VAR moduleName, procedureName: ARRAY OF CHAR): BOOLEAN;
  3088. BEGIN
  3089. RETURN cg.Supported(instruction,moduleName,procedureName);
  3090. END SupportedInstruction;
  3091. PROCEDURE GenerateBinary(module: Sections.Module; dump: Streams.Writer);
  3092. VAR
  3093. in: Sections.Section;
  3094. out: BinaryCode.Section;
  3095. name: Basic.SegmentedName;
  3096. procedure: SyntaxTree.Procedure;
  3097. i, j, initialSectionCount: LONGINT;
  3098. (* recompute fixup positions and assign binary sections *)
  3099. PROCEDURE PatchFixups(section: BinaryCode.Section);
  3100. VAR resolved: BinaryCode.Section; fixup: BinaryCode.Fixup; displacement,symbolOffset: LONGINT; in: IntermediateCode.Section;
  3101. symbol: Sections.Section;
  3102. BEGIN
  3103. fixup := section.fixupList.firstFixup;
  3104. WHILE fixup # NIL DO
  3105. symbol := module.allSections.FindByName(fixup.symbol.name);
  3106. IF (symbol # NIL) & (symbol(IntermediateCode.Section).resolved # NIL) THEN
  3107. resolved := symbol(IntermediateCode.Section).resolved(BinaryCode.Section);
  3108. in := symbol(IntermediateCode.Section);
  3109. symbolOffset := fixup.symbolOffset;
  3110. IF symbolOffset = in.pc THEN
  3111. displacement := resolved.pc
  3112. ELSIF (symbolOffset # 0) THEN
  3113. ASSERT(in.pc > symbolOffset);
  3114. displacement := in.instructions[symbolOffset].pc;
  3115. ELSE
  3116. displacement := 0;
  3117. END;
  3118. fixup.SetSymbol(fixup.symbol.name,fixup.symbol.fingerprint,0,fixup.displacement+displacement);
  3119. END;
  3120. fixup := fixup.nextFixup;
  3121. END;
  3122. END PatchFixups;
  3123. BEGIN
  3124. cg.SetModule(module);
  3125. FOR i := 0 TO module.allSections.Length() - 1 DO
  3126. in := module.allSections.GetSection(i);
  3127. IF in.type = Sections.InlineCodeSection THEN
  3128. name := in.name;
  3129. out := ResolvedSection(in(IntermediateCode.Section));
  3130. cg.Section(in(IntermediateCode.Section),out);
  3131. procedure := in.symbol(SyntaxTree.Procedure);
  3132. IF procedure.procedureScope.body.code # NIL THEN
  3133. procedure.procedureScope.body.code.SetBinaryCode(out.os.bits);
  3134. END;
  3135. END
  3136. END;
  3137. initialSectionCount := 0;
  3138. REPEAT
  3139. j := initialSectionCount;
  3140. initialSectionCount := module.allSections.Length() ;
  3141. FOR i := j TO initialSectionCount - 1 DO
  3142. in := module.allSections.GetSection(i);
  3143. IF (in.type # Sections.InlineCodeSection) & (in(IntermediateCode.Section).resolved = NIL) THEN
  3144. name := in.name;
  3145. out := ResolvedSection(in(IntermediateCode.Section));
  3146. cg.Section(in(IntermediateCode.Section),out);
  3147. IF out.os.type = Sections.VarSection THEN
  3148. IF out.pc = 1 THEN out.SetAlignment(FALSE,1)
  3149. ELSIF out.pc = 2 THEN out.SetAlignment(FALSE,2)
  3150. ELSIF (out.pc > 4) & (bits > 32) THEN out.SetAlignment(FALSE,8)
  3151. ELSIF (out.pc > 2) THEN out.SetAlignment(FALSE,4)
  3152. END;
  3153. ELSIF out.os.type = Sections.ConstSection THEN
  3154. out.SetAlignment(FALSE,bits DIV 8);
  3155. END;
  3156. END
  3157. END
  3158. UNTIL initialSectionCount = module.allSections.Length(); (* process remaining sections that have been added during traversal of sections *)
  3159. (*
  3160. FOR i := 0 TO module.allSections.Length() - 1 DO
  3161. in := module.allSections.GetSection(i);
  3162. IF in.kind = Sections.CaseTableKind THEN
  3163. IF in(IntermediateCode.Section).resolved = NIL THEN
  3164. out := ResolvedSection(in(IntermediateCode.Section));
  3165. cg.Section(in(IntermediateCode.Section),out);
  3166. END
  3167. END
  3168. END;
  3169. *)
  3170. FOR i := 0 TO module.allSections.Length() - 1 DO
  3171. in := module.allSections.GetSection(i);
  3172. PatchFixups(in(IntermediateCode.Section).resolved)
  3173. END;
  3174. (*
  3175. FOR i := 0 TO module.allSections.Length() - 1 DO
  3176. in := module.allSections.GetSection(i);
  3177. IF in.kind = Sections.CaseTableKind THEN
  3178. PatchFixups(in(IntermediateCode.Section).resolved)
  3179. END
  3180. END;
  3181. *)
  3182. IF cg.error THEN Error("",Basic.invalidPosition, Diagnostics.Invalid,"") END;
  3183. END GenerateBinary;
  3184. (* genasm *)
  3185. PROCEDURE ProcessIntermediateCodeModule*(intermediateCodeModule: Formats.GeneratedModule): Formats.GeneratedModule;
  3186. VAR
  3187. result: Formats.GeneratedModule;
  3188. BEGIN
  3189. ASSERT(intermediateCodeModule IS Sections.Module);
  3190. result := ProcessIntermediateCodeModule^(intermediateCodeModule);
  3191. IF ~error THEN
  3192. GenerateBinary(result(Sections.Module),dump);
  3193. IF dump # NIL THEN
  3194. dump.Ln; dump.Ln;
  3195. dump.String(";------------------ binary code -------------------"); dump.Ln;
  3196. IF (traceString="") OR (traceString="*") THEN
  3197. result.Dump(dump);
  3198. dump.Update
  3199. ELSE
  3200. Sections.DumpFiltered(dump, result(Sections.Module), traceString);
  3201. dump.Update;
  3202. END
  3203. END;
  3204. END;
  3205. RETURN result
  3206. FINALLY
  3207. IF dump # NIL THEN
  3208. dump.Ln; dump.Ln;
  3209. dump.String("; ------------------ rescued code (code generation trapped) -------------------"); dump.Ln;
  3210. IF (traceString="") OR (traceString="*") THEN
  3211. result.Dump(dump);
  3212. dump.Update
  3213. ELSE
  3214. Sections.DumpFiltered(dump, result(Sections.Module), traceString);
  3215. dump.Update;
  3216. END
  3217. END;
  3218. HALT(100); (* do not continue compiling after trap *)
  3219. RETURN result
  3220. END ProcessIntermediateCodeModule;
  3221. PROCEDURE FindPC*(x: SyntaxTree.Module; CONST sectionName: ARRAY OF CHAR; sectionOffset: LONGINT);
  3222. VAR
  3223. section: Sections.Section; binarySection: BinaryCode.Section; label: BinaryCode.LabelList; module: Formats.GeneratedModule;
  3224. i: LONGINT; pooledName: Basic.SegmentedName;
  3225. BEGIN
  3226. module := ProcessSyntaxTreeModule(x);
  3227. Basic.ToSegmentedName(sectionName, pooledName);
  3228. i := 0;
  3229. REPEAT
  3230. section := module(Sections.Module).allSections.GetSection(i);
  3231. INC(i);
  3232. UNTIL (i = module(Sections.Module).allSections.Length()) OR (section.name = pooledName);
  3233. IF section.name # pooledName THEN
  3234. Basic.Error(diagnostics, module.module.sourceName,Basic.invalidPosition, " could not locate pc");
  3235. ELSE
  3236. binarySection := section(IntermediateCode.Section).resolved;
  3237. IF binarySection # NIL THEN
  3238. label := binarySection.labels;
  3239. WHILE (label # NIL) & (label.offset >= sectionOffset) DO
  3240. label := label.prev;
  3241. END;
  3242. END;
  3243. IF label # NIL THEN
  3244. Basic.Information(diagnostics, module.module.sourceName,label.position, " pc position");
  3245. ELSE
  3246. Basic.Error(diagnostics, module.module.sourceName,Basic.invalidPosition, " could not locate pc");
  3247. END;
  3248. END;
  3249. END FindPC;
  3250. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  3251. VAR length: LONGINT; baseType: SyntaxTree.Type; b: BOOLEAN;
  3252. BEGIN
  3253. b := SemanticChecker.IsStaticMathArray(type, length, baseType) & (baseType IS SyntaxTree.FloatType) &
  3254. (baseType.sizeInBits <= 32) & (length = 4);
  3255. b := b OR SemanticChecker.IsStaticMathArray(type, length, baseType) & (baseType IS SyntaxTree.CharacterType) &
  3256. (baseType.sizeInBits = 8) & (length = 4);
  3257. b := b OR SemanticChecker.IsStaticArray(type, baseType, length) & (baseType.resolved IS SyntaxTree.CharacterType) &
  3258. (baseType.resolved.sizeInBits = 8) & (length = 4);
  3259. RETURN b
  3260. END CanPassInRegister;
  3261. PROCEDURE GetDescription*(VAR instructionSet: ARRAY OF CHAR);
  3262. BEGIN instructionSet := "AMD";
  3263. END GetDescription;
  3264. PROCEDURE DefineOptions*(options: Options.Options);
  3265. BEGIN
  3266. options.Add(0X,"bits",Options.Integer);
  3267. options.Add(0X,"traceable", Options.Flag);
  3268. options.Add(0X,"useFPU", Options.Flag);
  3269. DefineOptions^(options);
  3270. END DefineOptions;
  3271. PROCEDURE GetOptions*(options: Options.Options);
  3272. BEGIN
  3273. IF ~options.GetInteger("bits",bits) THEN bits := 32 END;
  3274. traceable := options.GetFlag("traceable");
  3275. forceFPU := options.GetFlag("useFPU");
  3276. GetOptions^(options);
  3277. END GetOptions;
  3278. PROCEDURE DefaultObjectFileFormat*(): Formats.ObjectFileFormat;
  3279. BEGIN RETURN ObjectFileFormat.Get();
  3280. END DefaultObjectFileFormat;
  3281. PROCEDURE DefaultSymbolFileFormat*(): Formats.SymbolFileFormat;
  3282. BEGIN
  3283. RETURN NIL
  3284. END DefaultSymbolFileFormat;
  3285. END BackendAMD64;
  3286. (** the number of regular sections in a section list **)
  3287. PROCEDURE RegularSectionCount(sectionList: Sections.SectionList): LONGINT;
  3288. VAR
  3289. section: Sections.Section;
  3290. i, result: LONGINT;
  3291. BEGIN
  3292. result := 0;
  3293. FOR i := 0 TO sectionList.Length() - 1 DO
  3294. section := sectionList.GetSection(i);
  3295. INC(result)
  3296. END;
  3297. RETURN result
  3298. END RegularSectionCount;
  3299. PROCEDURE Assert(b: BOOLEAN; CONST s: ARRAY OF CHAR);
  3300. BEGIN
  3301. ASSERT(b,100);
  3302. END Assert;
  3303. PROCEDURE Halt(CONST s: ARRAY OF CHAR);
  3304. BEGIN
  3305. HALT(100);
  3306. END Halt;
  3307. PROCEDURE ResolvedSection(in: IntermediateCode.Section): BinaryCode.Section;
  3308. VAR section: BinaryCode.Section;
  3309. BEGIN
  3310. IF in.resolved = NIL THEN
  3311. NEW(section,in.type, 8, in.name,in.comments # NIL,FALSE);
  3312. section.SetAlignment(in.fixed, in.positionOrAlignment);
  3313. in.SetResolved(section);
  3314. ELSE
  3315. section := in.resolved
  3316. END;
  3317. RETURN section
  3318. END ResolvedSection;
  3319. PROCEDURE Init;
  3320. VAR i: LONGINT;
  3321. BEGIN
  3322. FOR i := 0 TO LEN(registerOperands)-1 DO
  3323. Assembler.InitRegister(registerOperands[i],i);
  3324. END;
  3325. opEAX := registerOperands[EAX];
  3326. opEBX := registerOperands[EBX];
  3327. opECX := registerOperands[ECX];
  3328. opEDX := registerOperands[EDX];
  3329. opESI := registerOperands[ESI];
  3330. opEDI := registerOperands[EDI];
  3331. opEBP := registerOperands[EBP];
  3332. opESP := registerOperands[ESP];
  3333. opRSP := registerOperands[RSP];
  3334. opRBP := registerOperands[RBP];
  3335. opAX := registerOperands[AX];
  3336. opBX := registerOperands[BX];
  3337. opCX := registerOperands[CX];
  3338. opDX := registerOperands[DX];
  3339. opSI := registerOperands[SI];
  3340. opDI := registerOperands[DI];
  3341. opAL := registerOperands[AL];
  3342. opBL := registerOperands[BL];
  3343. opCL := registerOperands[CL];
  3344. opDL := registerOperands[DL];
  3345. opAH := registerOperands[AH];
  3346. opBH := registerOperands[BH];
  3347. opCH := registerOperands[CH];
  3348. opDH := registerOperands[DH];
  3349. opST0 := registerOperands[ST0];
  3350. NEW(unusable); NEW(blocked); NEW(split); free := NIL;
  3351. END Init;
  3352. PROCEDURE Get*(): Backend.Backend;
  3353. VAR backend: BackendAMD64;
  3354. BEGIN NEW(backend); RETURN backend
  3355. END Get;
  3356. PROCEDURE Trace*;
  3357. BEGIN
  3358. TRACE(traceStackSize);
  3359. END Trace;
  3360. BEGIN
  3361. traceStackSize := 0;
  3362. Init;
  3363. usePool := Machine.NumberOfProcessors()>1;
  3364. END FoxAMDBackend.
  3365. SystemTools.FreeDownTo FoxAMDBackend ~