FoxAMDBackend.Mod 133 KB

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