FoxAMDBackend.Mod 134 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512
  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. (* returns the following register (or part thereof)
  856. 0: regRAX;
  857. 1: regRCX;
  858. 2: regRDX;
  859. 3: regRBX;
  860. 4: regRSP;
  861. 5: regRBP;
  862. 6: regRSI;
  863. 7: regRDI;
  864. 8 .. 15: regRx;
  865. *)
  866. PROCEDURE ParameterRegister(CONST type: IntermediateCode.Type; index: LONGINT): LONGINT;
  867. VAR physical: LONGINT;
  868. BEGIN
  869. RETURN GetTypedHardwareRegister(RAX + index, type);
  870. ASSERT(0 <= index);
  871. ASSERT(index <= 15);
  872. RETURN physical;
  873. END ParameterRegister;
  874. PROCEDURE GetRegister(CONST virtual: IntermediateCode.Operand; part:LONGINT; VAR physical: Assembler.Operand; VAR ticket: Ticket);
  875. VAR type: IntermediateCode.Type; virtualRegister, tempReg: LONGINT;
  876. tmp,imm: Assembler.Operand; index: LONGINT;
  877. BEGIN
  878. ASSERT(virtual.mode = IntermediateCode.ModeRegister);
  879. GetPartType(virtual.type,part,type);
  880. virtualRegister := virtual.register;
  881. IF (virtual.register > 0) THEN
  882. TicketToOperand(virtualRegisters.Mapped(virtual.register,part), physical);
  883. ELSIF virtual.register = IntermediateCode.FP THEN
  884. Assert(part=Low,"forbidden partitioned register on BP");
  885. physical := opBP;
  886. ELSIF virtual.register = IntermediateCode.SP THEN
  887. Assert(part=Low,"forbidden partitioned register on SP");
  888. physical := opSP;
  889. ELSIF virtual.register = IntermediateCode.AP THEN
  890. ASSERT(backend.cooperative);
  891. Assert(part=Low,"forbidden partitioned register on AP");
  892. TicketToOperand(ap, physical);
  893. ELSE HALT(100);
  894. END;
  895. IF virtual.offset # 0 THEN
  896. Assert(type.form # IntermediateCode.Float,"forbidden offset on float");
  897. IF ticket = NIL THEN
  898. tempReg := ForceFreeRegister(type);
  899. TicketToOperand(ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,type,tempReg,inPC),tmp);
  900. ELSE
  901. TicketToOperand(ticket, tmp);
  902. ticket := NIL;
  903. END;
  904. IF Assembler.IsRegisterOperand(physical) THEN
  905. Assembler.InitMem(physical,SHORTINT(type.length * type.sizeInBits DIV 8) , physical.register, virtual.offset);
  906. emitter.Emit2(InstructionSet.opLEA, tmp,physical);
  907. ELSE
  908. emitter.Emit2(InstructionSet.opMOV,tmp,physical);
  909. Assembler.InitImm(imm,0 ,virtual.offset);
  910. emitter.Emit2(InstructionSet.opADD,tmp,imm);
  911. END;
  912. physical := tmp;
  913. END;
  914. END GetRegister;
  915. (* make physical operand from virtual operand, if ticket given then write result into phyiscal register represented by ticket *)
  916. PROCEDURE MakeOperand(CONST vop: IntermediateCode.Operand; part: LONGINT; VAR op: Assembler.Operand; ticket: Ticket);
  917. VAR tmp: Assembler.Operand;
  918. BEGIN
  919. TryAllocate(vop,part);
  920. CASE vop.mode OF
  921. IntermediateCode.ModeMemory: GetMemory(vop,part,op);
  922. |IntermediateCode.ModeRegister: GetRegister(vop,part,op,ticket);
  923. |IntermediateCode.ModeImmediate: GetImmediate(vop,part,op,FALSE);
  924. END;
  925. IF ticket # NIL THEN
  926. TicketToOperand(ticket, tmp);
  927. emitter.Emit2(InstructionSet.opMOV, tmp, op);
  928. (* should work but does not
  929. IF Assembler.IsRegisterOperand(op) THEN ReleaseHint(op.register) END;
  930. *)
  931. op := tmp;
  932. END;
  933. END MakeOperand;
  934. (* make physical register operand from virtual operand *)
  935. PROCEDURE MakeRegister(CONST vop: IntermediateCode.Operand; part: LONGINT; VAR op: Assembler.Operand);
  936. VAR previous: Assembler.Operand; temp: Ticket;
  937. BEGIN
  938. MakeOperand(vop,part,op,NIL);
  939. IF ~Assembler.IsRegisterOperand(op) THEN
  940. previous := op;
  941. temp := TemporaryTicket(vop.registerClass,vop.type);
  942. TicketToOperand(temp,op);
  943. IF vop.type.length > 1 THEN
  944. emitter.Emit2(InstructionSet.opMOVUPS, op, previous);
  945. ELSE
  946. Move(op, previous, vop.type);
  947. END;
  948. END;
  949. END MakeRegister;
  950. (*------------------- helpers for code generation ----------------------*)
  951. (* move, potentially with conversion. parameter back used for moving back from temporary operand*)
  952. PROCEDURE SpecialMove(op, back: LONGINT; canStoreToMemory: BOOLEAN; VAR dest,src: Assembler.Operand; type: IntermediateCode.Type);
  953. VAR temp: Assembler.Operand; ticket: Ticket;
  954. BEGIN
  955. IF Assembler.SameOperand(src,dest) THEN (* do nothing *)
  956. ELSIF ~Assembler.IsMemoryOperand(dest) OR (~Assembler.IsMemoryOperand(src) & canStoreToMemory) THEN
  957. emitter.Emit2(op,dest,src);
  958. ELSE
  959. ticket := TemporaryTicket(IntermediateCode.GeneralPurposeRegister,type);
  960. TicketToOperand(ticket,temp);
  961. emitter.Emit2(op,temp,src);
  962. emitter.Emit2(back,dest,temp);
  963. UnmapTicket(ticket);
  964. END;
  965. END SpecialMove;
  966. PROCEDURE AllocateStack(sizeInBytes: LONGINT);
  967. VAR sizeOp: Assembler.Operand; opcode: LONGINT;
  968. BEGIN
  969. ASSERT(sizeInBytes MOD 4 (* (cpuBits DIV 8) *) = 0);
  970. IF sizeInBytes < 0 THEN
  971. sizeInBytes := -sizeInBytes; opcode := InstructionSet.opADD;
  972. ELSIF sizeInBytes > 0 THEN
  973. opcode := InstructionSet.opSUB;
  974. ELSE RETURN
  975. END;
  976. IF sizeInBytes < 128 THEN sizeOp := Assembler.NewImm8(sizeInBytes);
  977. ELSE sizeOp := Assembler.NewImm32(sizeInBytes);
  978. END;
  979. emitter.Emit2(opcode,opSP,sizeOp);
  980. END AllocateStack;
  981. (*------------------- generation = emit dispatch / emit procedures ----------------------*)
  982. PROCEDURE IsFloat(CONST operand: IntermediateCode.Operand): BOOLEAN;
  983. BEGIN RETURN operand.type.form = IntermediateCode.Float
  984. END IsFloat;
  985. PROCEDURE IsComplex(CONST operand: IntermediateCode.Operand): BOOLEAN;
  986. BEGIN RETURN (operand.type.form IN IntermediateCode.Integer) & (operand.type.sizeInBits > cpuBits)
  987. END IsComplex;
  988. PROCEDURE Generate(VAR instruction: IntermediateCode.Instruction);
  989. VAR opcode: SHORTINT; ticket: Ticket; hwreg, lastUse, i, part: LONGINT;
  990. BEGIN
  991. (*!IF ((instruction.opcode = IntermediateCode.mov) OR (instruction.opcode = IntermediateCode.pop)) & (instruction.op1.register <= IntermediateCode.ParameterRegister) THEN
  992. hwreg := ParameterRegister(IntermediateCode.ParameterRegister-instruction.op1.register, instruction.op1.type);
  993. Spill(physicalRegisters.Mapped(hwreg));
  994. lastUse := inPC+1;
  995. WHILE (lastUse < in.pc) &
  996. ((in.instructions[lastUse].opcode # IntermediateCode.push) OR (in.instructions[lastUse].op1.register # instruction.op1.register)) & (in.instructions[lastUse].opcode # IntermediateCode.call) DO
  997. INC(lastUse)
  998. END;
  999. ticket := ReservePhysicalRegister(instruction.op1.type,hwreg,lastUse);
  1000. END;
  1001. *)
  1002. ReserveOperandRegisters(instruction.op1,TRUE); ReserveOperandRegisters(instruction.op2,TRUE);ReserveOperandRegisters(instruction.op3,TRUE);
  1003. (*TryAllocate(instruction.op1,Low);
  1004. IF IsComplex(instruction.op1) THEN TryAllocate(instruction.op1,High) END;
  1005. *)
  1006. opcode := instruction.opcode;
  1007. CASE opcode OF
  1008. IntermediateCode.nop: (* do nothing *)
  1009. |IntermediateCode.mov:
  1010. IF IsFloat(instruction.op1) OR IsFloat(instruction.op2) THEN
  1011. EmitMovFloat(instruction.op1,instruction.op2)
  1012. ELSE EmitMov(instruction.op1,instruction.op2,Low);
  1013. IF IsComplex(instruction.op1) THEN EmitMov(instruction.op1,instruction.op2, High) END;
  1014. END;
  1015. |IntermediateCode.conv:
  1016. IF IsFloat(instruction.op1) OR IsFloat(instruction.op2) THEN
  1017. EmitConvertFloat(instruction)
  1018. ELSE
  1019. EmitConvert(instruction.op1,instruction.op2,Low);
  1020. IF IsComplex(instruction.op1) THEN EmitConvert(instruction.op1,instruction.op2,High) END;
  1021. END;
  1022. |IntermediateCode.call: EmitCall(instruction);
  1023. |IntermediateCode.enter: EmitEnter(instruction);
  1024. |IntermediateCode.leave: EmitLeave(instruction);
  1025. |IntermediateCode.exit: EmitExit(instruction);
  1026. |IntermediateCode.result:
  1027. IF IsFloat(instruction.op1) & backend.forceFPU THEN
  1028. EmitResultFPU(instruction)
  1029. ELSE
  1030. EmitResult(instruction,Low);
  1031. IF IsComplex(instruction.op1) THEN EmitResult(instruction,High) END;
  1032. END;
  1033. |IntermediateCode.return:
  1034. IF IsFloat(instruction.op1) & backend.forceFPU THEN
  1035. EmitReturnFPU(instruction)
  1036. ELSE
  1037. EmitReturn(instruction,Low);
  1038. IF IsComplex(instruction.op1) THEN EmitReturn(instruction, High) END;
  1039. END;
  1040. |IntermediateCode.trap: EmitTrap(instruction);
  1041. |IntermediateCode.br .. IntermediateCode.brlt: EmitBr(instruction)
  1042. |IntermediateCode.pop:
  1043. IF IsFloat(instruction.op1) THEN
  1044. EmitPopFloat(instruction.op1)
  1045. ELSE
  1046. EmitPop(instruction.op1,Low);
  1047. IF IsComplex(instruction.op1) THEN
  1048. EmitPop(instruction.op1,High)
  1049. END;
  1050. END;
  1051. |IntermediateCode.push:
  1052. IF IsFloat(instruction.op1) THEN
  1053. EmitPushFloat(instruction.op1)
  1054. ELSE
  1055. IF IsComplex(instruction.op1) THEN
  1056. EmitPush(instruction.op1,High);
  1057. END;
  1058. EmitPush(instruction.op1,Low)
  1059. END;
  1060. |IntermediateCode.neg:
  1061. IF IsFloat(instruction.op1) THEN
  1062. IF backend.forceFPU THEN
  1063. EmitArithmetic2FPU(instruction,InstructionSet.opFCHS)
  1064. ELSE
  1065. EmitNegXMM(instruction)
  1066. END;
  1067. ELSE EmitNeg(instruction);
  1068. END;
  1069. |IntermediateCode.not:
  1070. Assert(~IsFloat(instruction.op1),"instruction not supported for float");
  1071. EmitArithmetic2(instruction,Low,InstructionSet.opNOT);
  1072. IF IsComplex(instruction.op1) THEN EmitArithmetic2(instruction, High, InstructionSet.opNOT) END;
  1073. |IntermediateCode.abs:
  1074. IF IsFloat(instruction.op1) THEN
  1075. IF backend.forceFPU THEN
  1076. EmitArithmetic2FPU(instruction,InstructionSet.opFABS)
  1077. ELSE
  1078. EmitAbsXMM(instruction)
  1079. END;
  1080. ELSE EmitAbs(instruction);
  1081. END;
  1082. |IntermediateCode.mul:
  1083. IF IsFloat(instruction.op1) THEN
  1084. IF backend.forceFPU THEN
  1085. EmitArithmetic3FPU(instruction,InstructionSet.opFMUL)
  1086. ELSE
  1087. EmitArithmetic3XMM(instruction, InstructionSet.opMULSS, InstructionSet.opMULSD)
  1088. END;
  1089. ELSE
  1090. EmitMul(instruction);
  1091. END;
  1092. |IntermediateCode.div:
  1093. IF IsFloat(instruction.op1 )THEN
  1094. IF backend.forceFPU THEN
  1095. EmitArithmetic3FPU(instruction,InstructionSet.opFDIV)
  1096. ELSE
  1097. EmitArithmetic3XMM(instruction, InstructionSet.opDIVSS, InstructionSet.opDIVSD)
  1098. END;
  1099. ELSE
  1100. EmitDivMod(instruction);
  1101. END;
  1102. |IntermediateCode.mod:
  1103. Assert(~IsFloat(instruction.op1),"instruction not supported for float");
  1104. EmitDivMod(instruction);
  1105. |IntermediateCode.sub:
  1106. IF IsFloat(instruction.op1) THEN
  1107. IF backend.forceFPU THEN
  1108. EmitArithmetic3FPU(instruction,InstructionSet.opFSUB)
  1109. ELSE
  1110. EmitArithmetic3XMM(instruction, InstructionSet.opSUBSS, InstructionSet.opSUBSD)
  1111. END;
  1112. ELSE EmitArithmetic3(instruction,Low,InstructionSet.opSUB);
  1113. IF IsComplex(instruction.op1) THEN EmitArithmetic3(instruction, High, InstructionSet.opSBB) END;
  1114. END;
  1115. |IntermediateCode.add:
  1116. IF IsFloat(instruction.op1) THEN
  1117. IF backend.forceFPU THEN
  1118. EmitArithmetic3FPU(instruction,InstructionSet.opFADD)
  1119. ELSE
  1120. EmitArithmetic3XMM(instruction, InstructionSet.opADDSS, InstructionSet.opADDSD)
  1121. END;
  1122. ELSE EmitArithmetic3(instruction,Low,InstructionSet.opADD);
  1123. IF IsComplex(instruction.op1) THEN EmitArithmetic3(instruction, High, InstructionSet.opADC) END;
  1124. END;
  1125. |IntermediateCode.and:
  1126. Assert(~IsFloat(instruction.op1),"operation not defined on float");
  1127. EmitArithmetic3(instruction,Low,InstructionSet.opAND);
  1128. IF IsComplex(instruction.op1) THEN EmitArithmetic3(instruction, High, InstructionSet.opAND) END;
  1129. |IntermediateCode.or:
  1130. Assert(~IsFloat(instruction.op1),"operation not defined on float");
  1131. EmitArithmetic3(instruction,Low,InstructionSet.opOR);
  1132. IF IsComplex(instruction.op1) THEN EmitArithmetic3(instruction, High, InstructionSet.opOR) END;
  1133. |IntermediateCode.xor:
  1134. Assert(~IsFloat(instruction.op1),"operation not defined on float");
  1135. EmitArithmetic3(instruction,Low,InstructionSet.opXOR);
  1136. IF IsComplex(instruction.op1) THEN EmitArithmetic3(instruction, High, InstructionSet.opXOR) END;
  1137. |IntermediateCode.shl: EmitShift(instruction);
  1138. |IntermediateCode.shr: EmitShift(instruction);
  1139. |IntermediateCode.rol: EmitShift(instruction);
  1140. |IntermediateCode.ror: EmitShift(instruction);
  1141. |IntermediateCode.cas: EmitCas(instruction);
  1142. |IntermediateCode.copy: EmitCopy(instruction);
  1143. |IntermediateCode.fill: EmitFill(instruction,FALSE);
  1144. |IntermediateCode.asm: EmitAsm(instruction);
  1145. END;
  1146. ReserveOperandRegisters(instruction.op3,FALSE); ReserveOperandRegisters(instruction.op2,FALSE); ReserveOperandRegisters(instruction.op1,FALSE);
  1147. END Generate;
  1148. PROCEDURE PostGenerate(CONST instruction: IntermediateCode.Instruction);
  1149. VAR ticket: Ticket;
  1150. BEGIN
  1151. TryUnmap(instruction.op3); TryUnmap(instruction.op2); TryUnmap(instruction.op1);
  1152. ticket := tickets.live;
  1153. WHILE (ticket # NIL) & (ticket.lastuse = inPC) DO
  1154. UnmapTicket(ticket);
  1155. ticket := tickets.live
  1156. END;
  1157. END PostGenerate;
  1158. (* enter procedure: generate PAF and clear stack *)
  1159. PROCEDURE EmitEnter(CONST instruction: IntermediateCode.Instruction);
  1160. VAR op1,imm,target: Assembler.Operand; cc,size,numberMachineWords,destPC,firstPC,secondPC,x: LONGINT; body: SyntaxTree.Body; name: Basic.SegmentedName;
  1161. parametersSize: SIZE;
  1162. CONST initialize=TRUE; FirstOffset = 5; SecondOffset = 11;
  1163. BEGIN
  1164. stackSize := SHORT(instruction.op2.intValue);
  1165. size := stackSize;
  1166. INC(traceStackSize, stackSize);
  1167. IF initialize THEN
  1168. (* always including this instruction make trace insertion possible *)
  1169. IF backend.traceable THEN
  1170. emitter.Emit2(InstructionSet.opXOR,opRA,opRA);
  1171. END;
  1172. ASSERT(size MOD opRA.sizeInBytes = 0);
  1173. numberMachineWords := size DIV opRA.sizeInBytes;
  1174. IF numberMachineWords >0 THEN
  1175. IF ~backend.traceable THEN
  1176. emitter.Emit2(InstructionSet.opXOR,opRA,opRA);
  1177. END;
  1178. WHILE numberMachineWords MOD 4 # 0 DO
  1179. emitter.Emit1(InstructionSet.opPUSH, opRA);
  1180. DEC(numberMachineWords);
  1181. END;
  1182. IF numberMachineWords >4 THEN
  1183. Assembler.InitImm(imm, 0, numberMachineWords DIV 4);
  1184. emitter.Emit2(InstructionSet.opMOV, opRB, imm);
  1185. destPC := out.pc;
  1186. emitter.Emit1(InstructionSet.opDEC, opRB);
  1187. emitter.Emit1(InstructionSet.opPUSH, opRA);
  1188. emitter.Emit1(InstructionSet.opPUSH, opRA);
  1189. emitter.Emit1(InstructionSet.opPUSH, opRA);
  1190. emitter.Emit1(InstructionSet.opPUSH, opRA);
  1191. Assembler.InitOffset8(target,destPC);
  1192. emitter.Emit1(InstructionSet.opJNZ, target)
  1193. ELSE
  1194. WHILE numberMachineWords >0 DO
  1195. emitter.Emit1(InstructionSet.opPUSH, opRA);
  1196. DEC(numberMachineWords);
  1197. END;
  1198. END;
  1199. END;
  1200. IF spillStack.MaxSize()>0 THEN (* register spill stack, does not have to be initialized *)
  1201. op1 := Assembler.NewImm32(spillStack.MaxSize()*cpuBits DIV 8);
  1202. emitter.Emit2(InstructionSet.opSUB,opSP,op1);
  1203. END;
  1204. ELSE
  1205. op1 := Assembler.NewImm32(size+ spillStack.MaxSize());
  1206. emitter.Emit2(InstructionSet.opSUB,opSP,op1);
  1207. END;
  1208. IF (cc = SyntaxTree.WinAPICallingConvention) OR (cc = SyntaxTree.CCallingConvention) THEN
  1209. (* the winapi calling convention presumes that all registers except EAX, EDX and ECX are retained by the callee *)
  1210. emitter.Emit1(InstructionSet.opPUSH,opEBX);
  1211. emitter.Emit1(InstructionSet.opPUSH,opEDI);
  1212. emitter.Emit1(InstructionSet.opPUSH,opESI);
  1213. END;
  1214. spillStackStart := stackSize;
  1215. END EmitEnter;
  1216. PROCEDURE EmitLeave(CONST instruction: IntermediateCode.Instruction);
  1217. VAR cc: LONGINT; offset: Assembler.Operand;
  1218. BEGIN
  1219. cc := SHORT(instruction.op1.intValue);
  1220. IF (cc = SyntaxTree.WinAPICallingConvention) OR (cc = SyntaxTree.CCallingConvention) THEN
  1221. emitter.Emit1(InstructionSet.opPOP,opESI);
  1222. emitter.Emit1(InstructionSet.opPOP,opEDI);
  1223. emitter.Emit1(InstructionSet.opPOP,opEBX);
  1224. END;
  1225. END EmitLeave;
  1226. PROCEDURE EmitExit(CONST instruction: IntermediateCode.Instruction);
  1227. BEGIN
  1228. emitter.Emit0(InstructionSet.opRET);
  1229. IF fpStackPointer # 0 THEN Error(instruction.textPosition,"compiler implementation error: fp stack not cleared") END;
  1230. END EmitExit;
  1231. PROCEDURE EmitReturnFPU(CONST instruction: IntermediateCode.Instruction);
  1232. VAR operand: Assembler.Operand;
  1233. BEGIN
  1234. IF IsRegister(instruction.op1) & MappedTo(instruction.op1.register,Low, ST0) THEN
  1235. (* nothing to do: result is already in return register *)
  1236. ELSE
  1237. MakeOperand(instruction.op1, Low, operand,NIL);
  1238. emitter.Emit1(InstructionSet.opFLD,operand);
  1239. (*
  1240. not necessary to clear from top of stack as callee will clear
  1241. INC(fpStackPointer);
  1242. emitter.Emit1(InstructionSet.opFSTP,registerOperands[ST0+1]);
  1243. DEC(fpStackPointer);
  1244. *)
  1245. END;
  1246. END EmitReturnFPU;
  1247. (* return operand
  1248. store operand in return register or on fp stack
  1249. *)
  1250. PROCEDURE EmitReturn(CONST instruction: IntermediateCode.Instruction; part: LONGINT);
  1251. VAR return,operand: Assembler.Operand; register: LONGINT; ticket: Ticket; type: IntermediateCode.Type;
  1252. BEGIN
  1253. register := ResultRegister(instruction.op1.type, part);
  1254. IF IsRegister(instruction.op1) & MappedTo(instruction.op1.register,part, register) THEN
  1255. (* nothing to do: result is already in return register *)
  1256. ELSE
  1257. GetPartType(instruction.op1.type,part, type);
  1258. MakeOperand(instruction.op1, part, operand,NIL);
  1259. Spill(physicalRegisters.Mapped(register));
  1260. ticket := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,type,register,inPC);
  1261. TicketToOperand(ticket, return);
  1262. (* Mov takes care of potential register overlaps *)
  1263. Move(return, operand, type);
  1264. UnmapTicket(ticket);
  1265. END;
  1266. END EmitReturn;
  1267. PROCEDURE EmitMovFloat(CONST vdest,vsrc:IntermediateCode.Operand);
  1268. VAR dest,src, espm: Assembler.Operand; sizeInBytes: SHORTINT; vcopy: IntermediateCode.Operand;
  1269. BEGIN
  1270. sizeInBytes := SHORTINT(vdest.type.sizeInBits DIV 8);
  1271. IF vdest.type.form IN IntermediateCode.Integer THEN
  1272. (* e.g. in SYSTEM.VAL(LONGINT, r) *)
  1273. IF vsrc.mode = IntermediateCode.ModeMemory THEN
  1274. vcopy := vsrc; IntermediateCode.SetType(vcopy,vdest.type);
  1275. EmitMov(vdest, vcopy,Low);
  1276. IF IsComplex(vdest) THEN
  1277. EmitMov(vdest,vcopy,High);
  1278. END;
  1279. ELSE
  1280. IF backend.forceFPU THEN
  1281. MakeOperand(vsrc,Low,src,NIL);
  1282. emitter.Emit1(InstructionSet.opFLD,src);
  1283. INC(fpStackPointer);
  1284. IF vdest.mode = IntermediateCode.ModeMemory THEN
  1285. MakeOperand(vdest,Low,dest,NIL);
  1286. Assembler.SetSize(dest,sizeInBytes);
  1287. emitter.Emit1(InstructionSet.opFSTP,dest);
  1288. DEC(fpStackPointer);
  1289. ELSE
  1290. AllocateStack(sizeInBytes);
  1291. Assembler.InitMem(espm, sizeInBytes,SP,0);
  1292. emitter.Emit1(InstructionSet.opFSTP,espm);
  1293. DEC(fpStackPointer);
  1294. MakeOperand(vdest,Low,dest,NIL);
  1295. EmitPop(vdest,Low);
  1296. IF IsComplex(vdest) THEN
  1297. EmitPop(vdest,High);
  1298. END;
  1299. END;
  1300. ELSE
  1301. MakeOperand(vsrc, Low, src, NIL);
  1302. IF vdest.mode = IntermediateCode.ModeMemory THEN
  1303. MakeOperand(vdest, Low, dest, NIL);
  1304. Move(dest, src, vsrc.type);
  1305. ELSE (* need temporary stack argument *)
  1306. AllocateStack(sizeInBytes);
  1307. Assembler.InitMem(espm, sizeInBytes,SP,0);
  1308. Move(espm, src, vsrc.type);
  1309. MakeOperand(vdest,Low,dest,NIL);
  1310. EmitPop(vdest,Low);
  1311. IF IsComplex(vdest) THEN
  1312. EmitPop(vdest,High);
  1313. END;
  1314. END;
  1315. END;
  1316. END;
  1317. ELSIF vsrc.type.form IN IntermediateCode.Integer THEN
  1318. (* e.g. in SYSTEM.VAL(REAL, i) *)
  1319. IF vdest.mode = IntermediateCode.ModeMemory THEN
  1320. vcopy := vdest; IntermediateCode.SetType(vcopy,vsrc.type);
  1321. EmitMov(vcopy, vsrc,Low);
  1322. IF IsComplex(vsrc) THEN
  1323. EmitMov(vcopy,vsrc,High);
  1324. END;
  1325. ELSE
  1326. IF backend.forceFPU THEN
  1327. IF vsrc.mode = IntermediateCode.ModeMemory THEN
  1328. MakeOperand(vsrc,Low,src,NIL);
  1329. Assembler.SetSize(src,sizeInBytes);
  1330. emitter.Emit1(InstructionSet.opFLD,src);
  1331. ELSE
  1332. IF IsComplex(vsrc) THEN
  1333. EmitPush(vsrc,High);
  1334. END;
  1335. EmitPush(vsrc,Low);
  1336. Assembler.InitMem(espm, sizeInBytes,SP,0);
  1337. emitter.Emit1(InstructionSet.opFLD,espm);
  1338. ASSERT(sizeInBytes >0);
  1339. AllocateStack(-sizeInBytes);
  1340. END;
  1341. INC(fpStackPointer);
  1342. MakeOperand(vdest,Low,dest,NIL);
  1343. emitter.Emit1(InstructionSet.opFSTP,dest);
  1344. DEC(fpStackPointer);
  1345. ELSE
  1346. IF vsrc.mode = IntermediateCode.ModeMemory THEN
  1347. MakeOperand(vsrc,Low,src,NIL);
  1348. Assembler.SetSize(src,sizeInBytes);
  1349. MakeOperand(vdest,Low,dest,NIL);
  1350. Move(dest, src, vdest.type);
  1351. ELSE
  1352. IF IsComplex(vsrc) THEN
  1353. EmitPush(vsrc,High);
  1354. END;
  1355. EmitPush(vsrc,Low);
  1356. Assembler.InitMem(espm, sizeInBytes,SP,0);
  1357. MakeOperand(vdest, Low, dest, NIL);
  1358. Move(dest, espm, vdest.type);
  1359. AllocateStack(-sizeInBytes);
  1360. END;
  1361. END;
  1362. END;
  1363. ELSE
  1364. IF backend.forceFPU THEN
  1365. MakeOperand(vsrc,Low,src,NIL);
  1366. emitter.Emit1(InstructionSet.opFLD,src);
  1367. INC(fpStackPointer);
  1368. MakeOperand(vdest,Low,dest,NIL);
  1369. emitter.Emit1(InstructionSet.opFSTP,dest);
  1370. DEC(fpStackPointer);
  1371. ELSE
  1372. MakeOperand(vsrc, Low, src, NIL);
  1373. MakeOperand(vdest, Low, dest, NIL);
  1374. Move(dest, src, vdest.type)
  1375. END;
  1376. END;
  1377. END EmitMovFloat;
  1378. PROCEDURE EmitMov(CONST vdest,vsrc: IntermediateCode.Operand; part: LONGINT);
  1379. VAR op1,op2: Assembler.Operand; tmp: IntermediateCode.Operand;
  1380. t: CodeGenerators.Ticket;
  1381. type: IntermediateCode.Type;
  1382. BEGIN
  1383. IF (vdest.mode = IntermediateCode.ModeRegister) & (vsrc.mode = IntermediateCode.ModeRegister) & (vsrc.offset # 0) THEN
  1384. (* MOV R1, R2+offset => LEA EAX, [EBX+offset] *)
  1385. tmp := vsrc;
  1386. IntermediateCode.MakeMemory(tmp,vsrc.type);
  1387. MakeOperand(tmp,part,op2,NIL);
  1388. (*
  1389. ReleaseHint(op2.register);
  1390. *)
  1391. MakeOperand(vdest,part,op1,NIL);
  1392. t := virtualRegisters.Mapped(vdest.register,part);
  1393. IF (t # NIL) & (t.spilled) THEN
  1394. UnSpill(t); (* make sure this has not spilled *)
  1395. MakeOperand(vdest,part, op1,NIL);
  1396. END;
  1397. emitter.Emit2(InstructionSet.opLEA,op1,op2);
  1398. ELSE
  1399. MakeOperand(vsrc,part,op2,NIL);
  1400. MakeOperand(vdest,part,op1,NIL);
  1401. GetPartType(vsrc.type, part, type);
  1402. Move(op1,op2, type);
  1403. END;
  1404. END EmitMov;
  1405. PROCEDURE EmitConvertFloat(CONST instruction: IntermediateCode.Instruction);
  1406. VAR destType, srcType, dtype: IntermediateCode.Type; dest,src,espm,imm: Assembler.Operand; sizeInBytes, index: LONGINT;
  1407. temp, temp2, temp3, temp4: Assembler.Operand; ticket: Ticket; vdest, vsrc: IntermediateCode.Operand;
  1408. BEGIN
  1409. vdest := instruction.op1; vsrc := instruction.op2;
  1410. srcType := vsrc.type;
  1411. destType := vdest.type;
  1412. IF destType.form = IntermediateCode.Float THEN
  1413. CASE srcType.form OF
  1414. |IntermediateCode.Float: (* just a move *)
  1415. IF backend.forceFPU THEN
  1416. EmitMovFloat(vdest, vsrc);
  1417. ELSE
  1418. MakeOperand(vsrc,Low,src,NIL);
  1419. MakeOperand(vdest, Low, dest, NIL);
  1420. IF srcType.sizeInBits = 32 THEN
  1421. SpecialMove(InstructionSet.opCVTSS2SD, InstructionSet.opMOVSS, FALSE, dest, src, destType)
  1422. ELSE
  1423. SpecialMove(InstructionSet.opCVTSD2SS, InstructionSet.opMOVSD, FALSE, dest, src, destType)
  1424. END;
  1425. END;
  1426. |IntermediateCode.SignedInteger:
  1427. (* put value to stack and then read from stack via Float *)
  1428. IF vsrc.type.sizeInBits < IntermediateCode.Bits32 THEN
  1429. MakeOperand(vsrc,Low,src,NIL);
  1430. ticket := TemporaryTicket(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int32);
  1431. TicketToOperand(ticket,temp);
  1432. emitter.Emit2(InstructionSet.opMOVSX,temp,src);
  1433. IF backend.forceFPU THEN (* via stack *)
  1434. emitter.Emit1(InstructionSet.opPUSH,temp);
  1435. UnmapTicket(ticket);
  1436. sizeInBytes := temp.sizeInBytes;
  1437. ELSE (* via register *)
  1438. espm := temp;
  1439. sizeInBytes := 0
  1440. END;
  1441. ELSIF IsComplex(vsrc) THEN (* via stack *)
  1442. EmitPush(vsrc,High);
  1443. EmitPush(vsrc,Low);
  1444. sizeInBytes := 8
  1445. ELSE
  1446. IF backend.forceFPU THEN (* via stack *)
  1447. EmitPush(vsrc,Low);
  1448. sizeInBytes := SHORTINT(4 (* cpuBits DIV 8*)) (*SHORT(srcType.sizeInBits DIV 8)*);
  1449. ELSE (* via memory or register *)
  1450. sizeInBytes := 0;
  1451. MakeOperand(vsrc,Low,src,NIL);
  1452. IF Assembler.IsImmediateOperand(src) THEN (* use temporary register *)
  1453. ticket := TemporaryTicket(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int32);
  1454. TicketToOperand(ticket,temp);
  1455. emitter.Emit2(InstructionSet.opMOVSX,temp,src);
  1456. espm := temp
  1457. ELSE
  1458. espm := src
  1459. END;
  1460. END
  1461. END;
  1462. IF sizeInBytes > 0 THEN
  1463. Assembler.InitMem(espm, SHORTINT(sizeInBytes),SP,0);
  1464. END;
  1465. IF backend.forceFPU THEN
  1466. emitter.Emit1(InstructionSet.opFILD,espm);
  1467. INC(fpStackPointer);
  1468. ASSERT(sizeInBytes >0);
  1469. Basic.Align(sizeInBytes, 4 (* cpuBits DIV 8*));
  1470. AllocateStack(-sizeInBytes);
  1471. MakeOperand(vdest,Low,dest,NIL);
  1472. emitter.Emit1(InstructionSet.opFSTP,dest);
  1473. DEC(fpStackPointer);
  1474. ELSIF IsComplex(vsrc) THEN
  1475. emitter.Emit1(InstructionSet.opFILD,espm);
  1476. MakeOperand(vdest,Low,dest,NIL);
  1477. IF Assembler.IsMemoryOperand(dest) THEN
  1478. emitter.Emit1(InstructionSet.opFSTP,dest);
  1479. ELSE (* must be register *)
  1480. emitter.Emit1(InstructionSet.opFSTP,espm);
  1481. emitter.Emit2(InstructionSet.opMOVQ,dest,espm);
  1482. IF destType.sizeInBits = 32 THEN
  1483. emitter.Emit2(InstructionSet.opCVTSD2SS, dest,dest);
  1484. END;
  1485. END;
  1486. AllocateStack(-sizeInBytes);
  1487. ELSE
  1488. MakeOperand(vdest,Low,dest,NIL);
  1489. IF destType.sizeInBits = 32 THEN
  1490. emitter.Emit2(InstructionSet.opCVTSI2SS, dest, espm)
  1491. ELSE
  1492. emitter.Emit2(InstructionSet.opCVTSI2SD, dest, espm)
  1493. END;
  1494. AllocateStack(-sizeInBytes);
  1495. END;
  1496. END;
  1497. ELSE
  1498. ASSERT(destType.form IN IntermediateCode.Integer);
  1499. ASSERT(srcType.form = IntermediateCode.Float);
  1500. Assert(vdest.type.form = IntermediateCode.SignedInteger, "no entier as result for unsigned integer");
  1501. MakeOperand(vsrc,Low,src,NIL);
  1502. IF ~backend.forceFPU THEN
  1503. MakeOperand(vdest,Low,dest,ticket);
  1504. GetTemporaryRegister(srcType, temp);
  1505. GetTemporaryRegister(srcType, temp3);
  1506. IF destType.sizeInBits < 32 THEN
  1507. IntermediateCode.InitType(dtype, destType.form, 32);
  1508. GetTemporaryRegister(dtype, temp4);
  1509. ELSE
  1510. dtype := destType;
  1511. temp4 := dest;
  1512. END;
  1513. GetTemporaryRegister(dtype, temp2);
  1514. IF srcType.sizeInBits = 32 THEN
  1515. (* convert truncated -> negative numbers round up !*)
  1516. emitter.Emit2(InstructionSet.opCVTTSS2SI, temp4, src);
  1517. (* back to temporary mmx register *)
  1518. emitter.Emit2(InstructionSet.opCVTSI2SS, temp, temp4);
  1519. (* subtract *)
  1520. emitter.Emit2(InstructionSet.opMOVSS, temp3, src);
  1521. emitter.Emit2(InstructionSet.opSUBSS, temp3, temp);
  1522. (* back to a GP register in order to determine the sign bit *)
  1523. ELSE
  1524. emitter.Emit2(InstructionSet.opCVTTSD2SI, temp4, src);
  1525. emitter.Emit2(InstructionSet.opCVTSI2SD, temp, temp4);
  1526. emitter.Emit2(InstructionSet.opMOVSD, temp3, src);
  1527. emitter.Emit2(InstructionSet.opSUBSD, temp3, temp);
  1528. emitter.Emit2(InstructionSet.opCVTSD2SS, temp3, temp3);
  1529. END;
  1530. emitter.Emit2(InstructionSet.opMOVD, temp2, temp3);
  1531. Assembler.InitImm(imm, 0 ,srcType.sizeInBits-1);
  1532. emitter.Emit2(InstructionSet.opBT, temp2, imm);
  1533. Assembler.InitImm(imm, 0 ,0);
  1534. emitter.Emit2(InstructionSet.opSBB, temp4, imm);
  1535. IF dtype.sizeInBits # destType.sizeInBits THEN
  1536. index := temp4.register;
  1537. CASE destType.sizeInBits OF (* choose low part accordingly *)
  1538. IntermediateCode.Bits8: index := index MOD 32 + AL;
  1539. |IntermediateCode.Bits16: index := index MOD 32 + AX;
  1540. |IntermediateCode.Bits32: index := index MOD 32 + EAX;
  1541. END;
  1542. temp4 := registerOperands[index];
  1543. emitter.Emit2(InstructionSet.opMOV, dest, temp4);
  1544. END
  1545. ELSE
  1546. emitter.Emit1(InstructionSet.opFLD,src); INC(fpStackPointer);
  1547. MakeOperand(vdest,Low,dest,NIL);
  1548. IF destType.sizeInBits = IntermediateCode.Bits64 THEN AllocateStack(12) ELSE AllocateStack(8) END;
  1549. Assembler.InitMem(espm,IntermediateCode.Bits16 DIV 8,SP,0);
  1550. emitter.Emit1(InstructionSet.opFNSTCW,espm);
  1551. emitter.Emit0(InstructionSet.opFWAIT);
  1552. Assembler.InitMem(espm,IntermediateCode.Bits32 DIV 8,SP,0);
  1553. ticket := TemporaryTicket(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int32);
  1554. TicketToOperand(ticket,temp);
  1555. emitter.Emit2(InstructionSet.opMOV,temp,espm);
  1556. imm := Assembler.NewImm32(0F3FFH);
  1557. emitter.Emit2(InstructionSet.opAND,temp,imm);
  1558. imm := Assembler.NewImm32(0400H);
  1559. emitter.Emit2(InstructionSet.opOR,temp,imm);
  1560. Assembler.InitMem(espm,IntermediateCode.Bits32 DIV 8,SP,4);
  1561. emitter.Emit2(InstructionSet.opMOV,espm,temp);
  1562. Assembler.InitMem(espm,IntermediateCode.Bits16 DIV 8,SP,4);
  1563. emitter.Emit1(InstructionSet.opFLDCW,espm);
  1564. IF destType.sizeInBits = IntermediateCode.Bits64 THEN
  1565. Assembler.InitMem(espm,IntermediateCode.Bits64 DIV 8,SP,4);
  1566. emitter.Emit1(InstructionSet.opFISTP,espm);DEC(fpStackPointer);
  1567. emitter.Emit0(InstructionSet.opFWAIT);
  1568. ELSE
  1569. Assembler.InitMem(espm,IntermediateCode.Bits32 DIV 8,SP,4);
  1570. emitter.Emit1(InstructionSet.opFISTP,espm); DEC(fpStackPointer);
  1571. emitter.Emit0(InstructionSet.opFWAIT);
  1572. END;
  1573. Assembler.InitMem(espm,IntermediateCode.Bits16 DIV 8,SP,0);
  1574. emitter.Emit1(InstructionSet.opFLDCW,espm);
  1575. emitter.Emit1(InstructionSet.opPOP,temp);
  1576. UnmapTicket(ticket);
  1577. emitter.Emit1(InstructionSet.opPOP,dest);
  1578. IF IsComplex(vdest) THEN
  1579. MakeOperand(vdest,High,dest,NIL);
  1580. emitter.Emit1(InstructionSet.opPOP,dest);
  1581. END;
  1582. END;
  1583. END;
  1584. END EmitConvertFloat;
  1585. PROCEDURE EmitConvert(CONST vdest, vsrc: IntermediateCode.Operand; part: LONGINT);
  1586. VAR destType, srcType: IntermediateCode.Type; op1,op2: Assembler.Operand; index: LONGINT; nul: Assembler.Operand;
  1587. ticket: Ticket; vop: IntermediateCode.Operand; ediReserved, esiReserved: BOOLEAN;
  1588. eax, edx: Ticket; symbol: ObjectFile.Identifier; offset: LONGINT;
  1589. BEGIN
  1590. GetPartType(vdest.type,part, destType);
  1591. GetPartType(vsrc.type,part,srcType);
  1592. ASSERT(vdest.type.form IN IntermediateCode.Integer);
  1593. ASSERT(destType.form IN IntermediateCode.Integer);
  1594. IF destType.sizeInBits < srcType.sizeInBits THEN (* SHORT *)
  1595. ASSERT(part # High);
  1596. MakeOperand(vdest,part,op1,NIL);
  1597. IF vsrc.mode = IntermediateCode.ModeImmediate THEN
  1598. vop := vsrc;
  1599. IntermediateCode.SetType(vop,destType);
  1600. MakeOperand(vop,part,op2,NIL);
  1601. ELSE
  1602. MakeOperand(vsrc,part,op2,NIL);
  1603. 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
  1604. (* try EAX <- EDI for dest = AL or AX, src=EDI *)
  1605. index := op1.register;
  1606. CASE srcType.sizeInBits OF
  1607. IntermediateCode.Bits16: index := index MOD 32 + AX;
  1608. |IntermediateCode.Bits32: index := index MOD 32 + EAX;
  1609. |IntermediateCode.Bits64: index := index MOD 32 + RAX;
  1610. END;
  1611. op1 := registerOperands[index];
  1612. ELSE
  1613. (* reserve register with a low part *)
  1614. IF destType.sizeInBits=8 THEN (* make sure that allocated temporary register has a low part with 8 bits, i.e. exclude ESI or EDI *)
  1615. ediReserved := physicalRegisters.Reserved(EDI);
  1616. esiReserved := physicalRegisters.Reserved(ESI);
  1617. physicalRegisters.SetReserved(EDI,TRUE); physicalRegisters.SetReserved(ESI,TRUE);
  1618. ticket := TemporaryTicket(IntermediateCode.GeneralPurposeRegister,srcType); (* register with low part *)
  1619. physicalRegisters.SetReserved(EDI,ediReserved); physicalRegisters.SetReserved(ESI,esiReserved);
  1620. ELSE
  1621. ticket := TemporaryTicket(IntermediateCode.GeneralPurposeRegister,srcType); (* any register with low part *)
  1622. END;
  1623. MakeOperand(vsrc,part,op2,ticket); (* stores op2 in ticket register *)
  1624. index := op2.register;
  1625. CASE destType.sizeInBits OF (* choose low part accordingly *)
  1626. IntermediateCode.Bits8: index := index MOD 32 + AL;
  1627. |IntermediateCode.Bits16: index := index MOD 32 + AX;
  1628. |IntermediateCode.Bits32: index := index MOD 32 + EAX;
  1629. END;
  1630. op2 := registerOperands[index];
  1631. END;
  1632. Move(op1,op2,PhysicalOperandType(op1));
  1633. END;
  1634. ELSIF destType.sizeInBits > srcType.sizeInBits THEN (* (implicit) LONG *)
  1635. IF part = High THEN
  1636. IF destType.form = IntermediateCode.SignedInteger THEN
  1637. Spill(physicalRegisters.Mapped(EAX));
  1638. eax := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int32,EAX,inPC);
  1639. Spill(physicalRegisters.Mapped(EDX));
  1640. edx := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int32,EDX,inPC);
  1641. IF vsrc.type.sizeInBits < 32 THEN
  1642. MakeOperand(vsrc,Low,op2,NIL);
  1643. SpecialMove(InstructionSet.opMOVSX,InstructionSet.opMOV, FALSE, opEAX,op2,PhysicalOperandType(opEAX));
  1644. ELSE
  1645. MakeOperand(vsrc,Low,op2,eax);
  1646. END;
  1647. emitter.Emit0(InstructionSet.opCDQ);
  1648. MakeOperand(vdest,High,op1,NIL);
  1649. emitter.Emit2(InstructionSet.opMOV,op1,opEDX);
  1650. UnmapTicket(eax); UnmapTicket(edx);
  1651. ELSE
  1652. MakeOperand(vdest,part,op1,NIL);
  1653. IF (vdest.mode = IntermediateCode.ModeRegister) THEN
  1654. emitter.Emit2(InstructionSet.opXOR,op1,op1)
  1655. ELSE
  1656. Assembler.InitImm(nul,0,0);
  1657. emitter.Emit2(InstructionSet.opMOV,op1,nul);
  1658. END;
  1659. END;
  1660. ELSE
  1661. ASSERT(part=Low);
  1662. MakeOperand(vdest,part,op1,NIL);
  1663. MakeOperand(vsrc,part,op2,NIL);
  1664. IF srcType.sizeInBits = destType.sizeInBits THEN
  1665. Move(op1,op2,PhysicalOperandType(op1));
  1666. ELSIF srcType.form = IntermediateCode.SignedInteger THEN
  1667. IF srcType.sizeInBits=32 THEN (* 64 bits only *)
  1668. ASSERT(cpuBits=64);
  1669. SpecialMove(InstructionSet.opMOVSXD,InstructionSet.opMOV, FALSE, op1,op2,PhysicalOperandType(op1));
  1670. ELSE
  1671. SpecialMove(InstructionSet.opMOVSX,InstructionSet.opMOV, FALSE, op1,op2,PhysicalOperandType(op1));
  1672. END;
  1673. ELSE
  1674. ASSERT(srcType.form = IntermediateCode.UnsignedInteger);
  1675. IF srcType.sizeInBits=32 THEN (* 64 bits only *)
  1676. ASSERT(cpuBits=64);
  1677. IF Assembler.IsRegisterOperand(op1) THEN
  1678. Move( registerOperands[op1.register MOD 32 + EAX], op2,srcType);
  1679. ELSE
  1680. ASSERT(Assembler.IsMemoryOperand(op1));
  1681. symbol := op1.symbol; offset := op1.offset;
  1682. Assembler.InitMem(op1,Assembler.bits32,op1.register, op1.displacement);
  1683. Assembler.SetSymbol(op1,symbol.name,symbol.fingerprint,offset,op1.displacement);
  1684. Move( op1, op2, srcType);
  1685. Assembler.InitMem(op1,Assembler.bits32,op1.register, op1.displacement+Assembler.bits32);
  1686. Assembler.SetSymbol(op1,symbol.name, symbol.fingerprint,offset,op1.displacement);
  1687. Assembler.InitImm(op2,0,0);
  1688. Move( op1, op2,srcType);
  1689. END;
  1690. ELSE
  1691. SpecialMove(InstructionSet.opMOVZX, InstructionSet.opMOV, FALSE, op1, op2,PhysicalOperandType(op1))
  1692. END;
  1693. END;
  1694. END;
  1695. ELSE (* destType.sizeInBits = srcType.sizeInBits) *)
  1696. EmitMov(vdest,vsrc,part);
  1697. END;
  1698. END EmitConvert;
  1699. PROCEDURE EmitResult(CONST instruction: IntermediateCode.Instruction; part: LONGINT);
  1700. VAR result,op: Assembler.Operand; register, highRegister: LONGINT; highReserved: BOOLEAN; type: IntermediateCode.Type;
  1701. BEGIN
  1702. register := ResultRegister(instruction.op1.type,part);
  1703. IF (part = Low) & IsComplex(instruction.op1) THEN
  1704. (* protect upper result (EDX) register *)
  1705. highRegister := ResultRegister(instruction.op1.type, High);
  1706. highReserved := physicalRegisters.Reserved(highRegister);
  1707. physicalRegisters.SetReserved(highRegister,TRUE);
  1708. END;
  1709. result := registerOperands[register];
  1710. MakeOperand(instruction.op1,part,op,NIL);
  1711. GetPartType(instruction.op1.type, part, type);
  1712. Move(op,result,type);
  1713. IF (part = Low) & IsComplex(instruction.op1) THEN
  1714. physicalRegisters.SetReserved(highRegister, highReserved);
  1715. END
  1716. END EmitResult;
  1717. PROCEDURE EmitResultFPU(CONST instruction: IntermediateCode.Instruction);
  1718. VAR op: Assembler.Operand;
  1719. BEGIN
  1720. INC(fpStackPointer); (* callee has left the result on top of stack, don't have to allocate here *)
  1721. MakeOperand(instruction.op1,Low,op,NIL);
  1722. emitter.Emit1(InstructionSet.opFSTP,op);
  1723. DEC(fpStackPointer);
  1724. (*
  1725. UnmapTicket(ticket);
  1726. *)
  1727. END EmitResultFPU;
  1728. PROCEDURE EmitCall(CONST instruction: IntermediateCode.Instruction);
  1729. VAR fixup: Sections.Section; target, op, parSize: Assembler.Operand;
  1730. code: SyntaxTree.Code; emitterFixup,newFixup: BinaryCode.Fixup; resolved: BinaryCode.Section; pc: LONGINT;
  1731. BEGIN
  1732. IF fpStackPointer # 0 THEN Error(instruction.textPosition,"compiler implementation error: fp stack not cleared before call") END;
  1733. IF instruction.op1.mode = IntermediateCode.ModeImmediate THEN
  1734. fixup := module.allSections.FindByName(instruction.op1.symbol.name);
  1735. IF (fixup # NIL) & (fixup.type = Sections.InlineCodeSection) THEN
  1736. pc := out.pc;
  1737. (* resolved must be available at this point ! *)
  1738. resolved := fixup(IntermediateCode.Section).resolved;
  1739. IF resolved # NIL THEN
  1740. emitter.code.CopyBits(resolved.os.bits,0,resolved.os.bits.GetSize());
  1741. emitterFixup := resolved.fixupList.firstFixup;
  1742. WHILE (emitterFixup # NIL) DO
  1743. newFixup := BinaryCode.NewFixup(emitterFixup.mode,emitterFixup.offset+pc,emitterFixup.symbol,emitterFixup.symbolOffset,emitterFixup.displacement,emitterFixup.scale,emitterFixup.pattern);
  1744. out.fixupList.AddFixup(newFixup);
  1745. emitterFixup := emitterFixup.nextFixup;
  1746. END;
  1747. END;
  1748. ELSE
  1749. Assembler.InitOffset32(target,instruction.op1.intValue);
  1750. Assembler.SetSymbol(target,instruction.op1.symbol.name,instruction.op1.symbol.fingerprint,instruction.op1.offset,0);
  1751. emitter.Emit1(InstructionSet.opCALL,target);
  1752. Assembler.InitOffset32(parSize,instruction.op2.intValue);
  1753. IF parSize.val # 0 THEN emitter.Emit2(InstructionSet.opADD,opSP,parSize) END;
  1754. END;
  1755. ELSE
  1756. MakeOperand(instruction.op1,Low,op,NIL);
  1757. emitter.Emit1(InstructionSet.opCALL,op);
  1758. Assembler.InitOffset32(parSize,instruction.op2.intValue);
  1759. IF parSize.val # 0 THEN emitter.Emit2(InstructionSet.opADD,opSP,parSize) END;
  1760. END;
  1761. END EmitCall;
  1762. (*
  1763. register allocation
  1764. instruction dest, src1, src2
  1765. preconditions
  1766. dest is memory operand or dest is register with offset = 0
  1767. src1 and src2 may be immediates, registers with or without offset and memory operands
  1768. 1.) translation into two-operand code
  1769. a) dest = src1 (no assumption on src2, src2=src1 is permitted )
  1770. i) dest and src2 are both memory operands or src2 is a register with offset # 0
  1771. alloc temp register
  1772. mov temp, src2
  1773. instruction2 dest, temp
  1774. ii) dest or src2 is not a memory operand
  1775. instruction2 dest, src2
  1776. b) dest = src2
  1777. => src2 is not a register with offset # 0
  1778. alloc temp register
  1779. mov dest, src1
  1780. mov temp, src2
  1781. instruction2 dest, temp
  1782. c) dest # src2
  1783. mov dest, src1
  1784. i) dest and src2 are both memory operands or src2 is a register with offset # 0
  1785. allocate temp register
  1786. mov temp, src2
  1787. instruction2 dest, temp
  1788. ii)
  1789. instruction2 dest, src2
  1790. 1'.) translation into one operand code
  1791. instruction dest, src1
  1792. a) dest = src1
  1793. => src1 is not a register with offset # 0
  1794. instruction1 dest
  1795. b) dest # src1
  1796. mov dest, src1
  1797. instruction1 dest
  1798. 2.) register allocation
  1799. precondition: src1 and src2 are already allocated
  1800. a) dest is already allocated
  1801. go on according to 1.
  1802. b) dest needs to be allocated
  1803. check if register is free
  1804. i) yes: allocate free register and go on with 1.
  1805. ii) no: spill last register in livelist, map register and go on with 1.
  1806. *)
  1807. PROCEDURE PrepareOp3(CONST instruction: IntermediateCode.Instruction;part: LONGINT; VAR left, right: Assembler.Operand; VAR ticket: Ticket);
  1808. VAR vop1,vop2, vop3: IntermediateCode.Operand; op1,op2,op3,temp: Assembler.Operand; type: IntermediateCode.Type;
  1809. t: Ticket;
  1810. BEGIN
  1811. ticket := NIL;
  1812. GetPartType(instruction.op1.type,part,type);
  1813. vop1 := instruction.op1; vop2 := instruction.op2; vop3 := instruction.op3;
  1814. IF IntermediateCode.OperandEquals(vop1,vop3) & (IntermediateCode.Commute23 IN IntermediateCode.instructionFormat[instruction.opcode].flags) THEN
  1815. vop3 := instruction.op2; vop2 := instruction.op3;
  1816. END;
  1817. MakeOperand(vop3,part, op3,NIL);
  1818. IF (vop1.mode = IntermediateCode.ModeRegister) & (~IsMemoryOperand(vop1,part)) & (vop1.register # vop3.register) THEN
  1819. IF (vop2.mode = IntermediateCode.ModeRegister) & (vop2.register = vop1.register) & (vop2.offset = 0) THEN
  1820. (* same register *)
  1821. MakeOperand(vop1,part, op1,NIL);
  1822. ELSE
  1823. MakeOperand(vop2,part, op2,NIL);
  1824. (*
  1825. ReleaseHint(op2.register);
  1826. *)
  1827. MakeOperand(vop1,part, op1,NIL);
  1828. Move(op1, op2, type);
  1829. t := virtualRegisters.Mapped(vop1.register,part);
  1830. IF (t # NIL) & (t.spilled) THEN
  1831. UnSpill(t); (* make sure this has not spilled *)
  1832. MakeOperand(vop1,part, op1,NIL);
  1833. END;
  1834. END;
  1835. left := op1; right := op3;
  1836. ELSIF IntermediateCode.OperandEquals(vop1,vop2) & (~IsMemoryOperand(vop1,part) OR ~IsMemoryOperand(vop3,part)) THEN
  1837. MakeOperand(vop1,part, op1,NIL);
  1838. left := op1; right := op3;
  1839. ELSE
  1840. MakeOperand(vop1,part, op1,NIL);
  1841. MakeOperand(vop2,part, op2,NIL);
  1842. (*ReleaseHint(op2.register);*)
  1843. ticket := TemporaryTicket(IntermediateCode.GeneralPurposeRegister,type);
  1844. TicketToOperand(ticket,temp);
  1845. Move(temp, op2, type);
  1846. left := temp; right := op3;
  1847. END;
  1848. END PrepareOp3;
  1849. PROCEDURE PrepareOp2(CONST instruction: IntermediateCode.Instruction; part: LONGINT; VAR left: Assembler.Operand;VAR ticket: Ticket);
  1850. VAR op2: Assembler.Operand; imm: Assembler.Operand; sizeInBits: INTEGER; type: IntermediateCode.Type;
  1851. BEGIN
  1852. ticket := NIL;
  1853. GetPartType(instruction.op1.type,part,type);
  1854. IF (instruction.op1.mode = IntermediateCode.ModeRegister) THEN
  1855. MakeOperand(instruction.op1,part,left,NIL);
  1856. MakeOperand(instruction.op2,part,op2,NIL);
  1857. IF (instruction.op2.mode = IntermediateCode.ModeRegister) & (instruction.op2.register = instruction.op1.register) & (instruction.op2.offset = 0) THEN
  1858. ELSE
  1859. Move(left, op2, type);
  1860. IF (instruction.op2.offset # 0) & ~IsMemoryOperand(instruction.op2,part) THEN
  1861. GetPartType(instruction.op2.type,part,type);
  1862. sizeInBits := type.sizeInBits;
  1863. Assembler.InitImm(imm,0,instruction.op2.offset);
  1864. emitter.Emit2(InstructionSet.opADD,left,imm);
  1865. END;
  1866. END;
  1867. ELSIF IntermediateCode.OperandEquals(instruction.op1,instruction.op2) & ((instruction.op1.mode # IntermediateCode.ModeMemory) OR (instruction.op3.mode # IntermediateCode.ModeMemory)) THEN
  1868. MakeOperand(instruction.op1,part,left,NIL);
  1869. ELSE
  1870. MakeOperand(instruction.op2,part, op2,NIL);
  1871. ticket := TemporaryTicket(IntermediateCode.GeneralPurposeRegister,type);
  1872. TicketToOperand(ticket,left);
  1873. Move(left, op2, type);
  1874. END;
  1875. END PrepareOp2;
  1876. PROCEDURE FinishOp(CONST vop: IntermediateCode.Operand; part: LONGINT; left: Assembler.Operand; ticket: Ticket);
  1877. VAR op1: Assembler.Operand;
  1878. BEGIN
  1879. IF ticket # NIL THEN
  1880. MakeOperand(vop,part, op1,NIL);
  1881. Move(op1,left,vop.type);
  1882. UnmapTicket(ticket);
  1883. END;
  1884. END FinishOp;
  1885. PROCEDURE EmitArithmetic3(CONST instruction: IntermediateCode.Instruction; part: LONGINT; opcode: LONGINT);
  1886. VAR left,right: Assembler.Operand; ticket: Ticket;
  1887. BEGIN
  1888. PrepareOp3(instruction, part, left,right,ticket);
  1889. emitter.Emit2(opcode,left,right);
  1890. FinishOp(instruction.op1,part,left,ticket);
  1891. END EmitArithmetic3;
  1892. PROCEDURE EmitArithmetic3XMM(CONST instruction: IntermediateCode.Instruction; op32, op64: LONGINT);
  1893. VAR op: LONGINT;
  1894. BEGIN
  1895. IF instruction.op1.type.sizeInBits = 32 THEN op := op32 ELSE op := op64 END;
  1896. EmitArithmetic3(instruction, Low, op);
  1897. END EmitArithmetic3XMM;
  1898. PROCEDURE EmitArithmetic2(CONST instruction: IntermediateCode.Instruction; part: LONGINT; opcode: LONGINT);
  1899. VAR left:Assembler.Operand;ticket: Ticket;
  1900. BEGIN
  1901. PrepareOp2(instruction,part,left,ticket);
  1902. emitter.Emit1(opcode,left);
  1903. FinishOp(instruction.op1,part,left,ticket);
  1904. END EmitArithmetic2;
  1905. PROCEDURE EmitArithmetic2XMM(CONST instruction: IntermediateCode.Instruction; op32, op64: LONGINT);
  1906. VAR op: LONGINT;
  1907. BEGIN
  1908. IF instruction.op1.type.sizeInBits = 32 THEN op := op32 ELSE op := op64 END;
  1909. EmitArithmetic2(instruction, Low, op);
  1910. END EmitArithmetic2XMM;
  1911. PROCEDURE EmitArithmetic3FPU(CONST instruction: IntermediateCode.Instruction; op: LONGINT);
  1912. VAR op1,op2,op3: Assembler.Operand;
  1913. BEGIN
  1914. MakeOperand(instruction.op2,Low,op2,NIL);
  1915. emitter.Emit1(InstructionSet.opFLD,op2);
  1916. INC(fpStackPointer);
  1917. MakeOperand(instruction.op3,Low,op3,NIL);
  1918. IF instruction.op3.mode = IntermediateCode.ModeRegister THEN
  1919. emitter.Emit2(op,opST0,op3);
  1920. ELSE
  1921. emitter.Emit1(op,op3);
  1922. END;
  1923. MakeOperand(instruction.op1,Low,op1,NIL);
  1924. emitter.Emit1(InstructionSet.opFSTP,op1);
  1925. DEC(fpStackPointer);
  1926. END EmitArithmetic3FPU;
  1927. PROCEDURE EmitArithmetic2FPU(CONST instruction: IntermediateCode.Instruction; opcode: LONGINT);
  1928. VAR op1,op2: Assembler.Operand;
  1929. BEGIN
  1930. MakeOperand(instruction.op2,Low,op2,NIL);
  1931. emitter.Emit1(InstructionSet.opFLD,op2);
  1932. INC(fpStackPointer);
  1933. emitter.Emit0(opcode);
  1934. MakeOperand(instruction.op1,Low,op1,NIL);
  1935. emitter.Emit1(InstructionSet.opFSTP,op1);
  1936. DEC(fpStackPointer);
  1937. END EmitArithmetic2FPU;
  1938. PROCEDURE EmitMul(CONST instruction: IntermediateCode.Instruction);
  1939. VAR op1,op2,op3,temp: Assembler.Operand; ra,rd: Ticket;
  1940. BEGIN
  1941. ASSERT(~IsComplex(instruction.op1));
  1942. ASSERT(instruction.op1.type.form IN IntermediateCode.Integer);
  1943. IF (instruction.op1.type.sizeInBits = IntermediateCode.Bits8) THEN
  1944. Spill(physicalRegisters.Mapped(AL));
  1945. Spill(physicalRegisters.Mapped(AH));
  1946. ra := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int8,AL,inPC);
  1947. rd := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int8,AH,inPC);
  1948. MakeOperand(instruction.op1,Low,op1,NIL);
  1949. MakeOperand(instruction.op2,Low,op2,ra);
  1950. IF instruction.op3.mode = IntermediateCode.ModeImmediate THEN
  1951. MakeOperand(instruction.op3,Low,op3,rd);
  1952. ELSE
  1953. MakeOperand(instruction.op3,Low,op3,NIL);
  1954. END;
  1955. emitter.Emit1(InstructionSet.opIMUL,op3);
  1956. emitter.Emit2(InstructionSet.opMOV,op1,opAL);
  1957. UnmapTicket(ra);
  1958. UnmapTicket(rd);
  1959. ELSE
  1960. MakeOperand(instruction.op1,Low,op1,NIL);
  1961. MakeOperand(instruction.op2,Low,op2,NIL);
  1962. MakeOperand(instruction.op3,Low,op3,NIL);
  1963. IF ~Assembler.IsRegisterOperand(op1) THEN
  1964. temp := op1;
  1965. ra := TemporaryTicket(instruction.op1.registerClass,instruction.op1.type);
  1966. TicketToOperand(ra,op1);
  1967. END;
  1968. IF Assembler.SameOperand(op1,op3) THEN temp := op2; op2 := op3; op3 := temp END;
  1969. IF Assembler.IsRegisterOperand(op2) OR Assembler.IsMemoryOperand(op2) THEN
  1970. IF Assembler.IsImmediateOperand(op3) THEN
  1971. emitter.Emit3(InstructionSet.opIMUL,op1,op2,op3);
  1972. ELSIF Assembler.IsRegisterOperand(op2) & (op2.register = op1.register) THEN
  1973. IF Assembler.IsRegisterOperand(op3) OR Assembler.IsMemoryOperand(op3) THEN
  1974. emitter.Emit2(InstructionSet.opIMUL,op1,op3);
  1975. ELSE
  1976. rd := TemporaryTicket(instruction.op1.registerClass,instruction.op1.type);
  1977. TicketToOperand(rd,temp);
  1978. Move(temp,op3,instruction.op1.type);
  1979. emitter.Emit2(InstructionSet.opIMUL,op1,temp);
  1980. UnmapTicket(rd);
  1981. END;
  1982. ELSE
  1983. Move(op1,op3,PhysicalOperandType(op1));
  1984. emitter.Emit2(InstructionSet.opIMUL,op1,op2);
  1985. END
  1986. ELSIF Assembler.IsRegisterOperand(op3) OR Assembler.IsMemoryOperand(op3) THEN
  1987. IF Assembler.IsImmediateOperand(op2) THEN
  1988. emitter.Emit3(InstructionSet.opIMUL,op1,op3,op2);
  1989. ELSIF Assembler.IsRegisterOperand(op3) & (op2.register = op1.register) THEN
  1990. IF Assembler.IsRegisterOperand(op2) OR Assembler.IsMemoryOperand(op2) THEN
  1991. emitter.Emit2(InstructionSet.opIMUL,op1,op2);
  1992. ELSE
  1993. rd := TemporaryTicket(instruction.op1.registerClass,instruction.op1.type);
  1994. TicketToOperand(rd,temp);
  1995. Move(temp,op2,instruction.op1.type);
  1996. emitter.Emit2(InstructionSet.opIMUL,op1,temp);
  1997. UnmapTicket(rd);
  1998. END;
  1999. ELSE
  2000. Move(op1,op2,PhysicalOperandType(op1));
  2001. emitter.Emit2(InstructionSet.opIMUL,op1,op3);
  2002. END;
  2003. END;
  2004. IF ra # NIL THEN
  2005. Move(temp,op1,PhysicalOperandType(op1));
  2006. UnmapTicket(ra);
  2007. END;
  2008. END;
  2009. END EmitMul;
  2010. PROCEDURE EmitDivMod(CONST instruction: IntermediateCode.Instruction);
  2011. VAR
  2012. dividend,quotient,remainder,imm,target,memop: Assembler.Operand;
  2013. op1,op2,op3: Assembler.Operand; ra,rd: Ticket;
  2014. size: LONGINT;
  2015. BEGIN
  2016. (*
  2017. In general it must obviously hold that
  2018. a = (a div b) * b + a mod b and
  2019. for all integers a,b#0, and c.
  2020. For positive numbers a and b this holds if
  2021. a div b = max{integer i: i*b <= b} = Entier(a/b)
  2022. and
  2023. a mod b = a-(a div b)*b = min{c >=0: c = a-i*b, integer i}
  2024. Example
  2025. 11 div 3 = 3 (3*3 = 9)
  2026. 11 mod 3 = 2 (=11-9)
  2027. for negative a there are two definitions for mod possible:
  2028. (i) mathematical definition with
  2029. a mod b >= 0:
  2030. a mod b = min{ c >=0: c = a-i*b, integer i} >= 0
  2031. this corresponds with rounding down
  2032. a div b = Entier(a/b) <= a/b
  2033. (ii) symmetric definition with
  2034. (-a) mod' b = -(a mod' b) and
  2035. (-a) div' b = -(a div' b)
  2036. corresponding with rounding to zero
  2037. a div' b = RoundToZero(a/b)
  2038. Examples
  2039. (i) -11 div 3 = -4 (3*(-4) = -12)
  2040. -11 mod 3 = 1 (=-11-(-12))
  2041. (ii) -11 div' 3 = -(11 div 3) = -3 (3*(-3)= -9)
  2042. -11 mod' 3 = -2 (=-11-(-9))
  2043. The behaviour for negative b can, in the symmetrical case, be deduced as
  2044. (ii) symmetric definition
  2045. a div' (-b) = (-a) div' b = -(a div' b)
  2046. a mod' (-b) = a- a div' (-b) * (-b) = a mod' b
  2047. In the mathematical case it is not so easy. It turns out that the definitions
  2048. a DIV b = Entier(a/b) = max{integer i: i*b <= b}
  2049. and
  2050. a MOD b = min { c >=0 : c = a-i*b, integer i} >= 0
  2051. are not compliant with
  2052. a = (a DIV b) * b + a MOD b
  2053. if b <= 0.
  2054. Proof: assume that b<0, then
  2055. a - Entier(a/b) * b >= 0
  2056. <=_> a >= Entier(a/b) * b
  2057. <=> Entier(a/b) >= a/b (contradiction to definition of Entier).
  2058. OBERON ADOPTS THE MATHEMATICAL DEFINITION !
  2059. For integers a and b (b>0) it holds that
  2060. a DIV b = Entier(a/b) <= a/b
  2061. a MOD b = min{ c >=0: c = b-i*a, integer i} = a - a DIV b * b
  2062. The behaviour for b < 0 is explicitely undefined.
  2063. *)
  2064. (*
  2065. AX / regMem8 = AL (remainder AH)
  2066. DX:AX / regmem16 = AX (remainder DX)
  2067. EDX:EAX / regmem32 = EAX (remainder EDX)
  2068. RDX:EAX / regmem64 = RAX (remainder RDX)
  2069. 1.) EAX <- source1
  2070. 2.) CDQ
  2071. 3.) IDIV source2
  2072. 3.) SHL EDX
  2073. 4.) SBB EAX,1
  2074. result is in EAX
  2075. *)
  2076. MakeOperand(instruction.op2,Low,op2,NIL);
  2077. CASE instruction.op1.type.sizeInBits OF
  2078. IntermediateCode.Bits8:
  2079. Spill(physicalRegisters.Mapped(AL)); ra := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int8,AL,inPC);
  2080. emitter.Emit2(InstructionSet.opMOV,opAL,op2);
  2081. dividend := opAX;
  2082. quotient := opAL;
  2083. remainder := opAH;
  2084. emitter.Emit0(InstructionSet.opCBW);
  2085. | IntermediateCode.Bits16:
  2086. Spill(physicalRegisters.Mapped(AX)); ra := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int16,AX,inPC);
  2087. emitter.Emit2(InstructionSet.opMOV,opAX,op2);
  2088. Spill(physicalRegisters.Mapped(DX)); rd := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int16,DX,inPC);
  2089. dividend := opAX;
  2090. quotient := dividend;
  2091. remainder := opDX;
  2092. emitter.Emit0(InstructionSet.opCWD);
  2093. | IntermediateCode.Bits32:
  2094. Spill(physicalRegisters.Mapped(EAX)); ra := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int32,EAX,inPC);
  2095. emitter.Emit2(InstructionSet.opMOV,opEAX,op2);
  2096. Spill(physicalRegisters.Mapped(EDX)); rd := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int32,EDX,inPC);
  2097. dividend := opEAX;
  2098. quotient := dividend;
  2099. remainder := opEDX;
  2100. emitter.Emit0(InstructionSet.opCDQ);
  2101. | IntermediateCode.Bits64:
  2102. Spill(physicalRegisters.Mapped(RAX)); ra := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int64,RAX,inPC);
  2103. emitter.Emit2(InstructionSet.opMOV,opRA,op2);
  2104. Spill(physicalRegisters.Mapped(RDX)); rd := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int64,RDX,inPC);
  2105. dividend := opRA;
  2106. quotient := dividend;
  2107. remainder := registerOperands[RDX];
  2108. emitter.Emit0(InstructionSet.opCQO);
  2109. END;
  2110. (* registers might have been changed, so we make the operands now *)
  2111. MakeOperand(instruction.op1,Low,op1,NIL);
  2112. MakeOperand(instruction.op2,Low,op2,NIL);
  2113. MakeOperand(instruction.op3,Low,op3,NIL);
  2114. IF instruction.op3.mode = IntermediateCode.ModeImmediate THEN
  2115. size := instruction.op3.type.sizeInBits DIV 8;
  2116. Basic.Align(size, 4 (* cpuBits DIV 8 *) );
  2117. AllocateStack(size);
  2118. Assembler.InitMem(memop,SHORT(instruction.op3.type.sizeInBits DIV 8),SP,0);
  2119. emitter.Emit2(InstructionSet.opMOV,memop,op3);
  2120. op3 := memop;
  2121. END;
  2122. emitter.Emit1(InstructionSet.opIDIV,op3);
  2123. IF instruction.opcode = IntermediateCode.mod THEN
  2124. imm := Assembler.NewImm8 (0);
  2125. emitter.Emit2(InstructionSet.opCMP, remainder, imm);
  2126. Assembler.InitImm8(target,0);
  2127. emitter.Emit1(InstructionSet.opJGE, target);
  2128. emitter.Emit2( InstructionSet.opADD, remainder, op3);
  2129. emitter.code.PutByteAt(target.pc,(emitter.code.pc -target.pc )-1);
  2130. emitter.Emit2(InstructionSet.opMOV, op1, remainder);
  2131. ELSE
  2132. imm := Assembler.NewImm8 (1);
  2133. emitter.Emit2(InstructionSet.opSHL, remainder, imm);
  2134. imm := Assembler.NewImm8 (0);
  2135. emitter.Emit2(InstructionSet.opSBB, quotient, imm);
  2136. emitter.Emit2(InstructionSet.opMOV, op1, quotient);
  2137. END;
  2138. IF instruction.op3.mode = IntermediateCode.ModeImmediate THEN
  2139. size := instruction.op3.type.sizeInBits DIV 8;
  2140. Basic.Align(size, 4 (* cpuBits DIV 8*) );
  2141. AllocateStack(-size);
  2142. END;
  2143. END EmitDivMod;
  2144. PROCEDURE EmitShift(CONST instruction: IntermediateCode.Instruction);
  2145. VAR
  2146. shift: Assembler.Operand;
  2147. op: LONGINT;
  2148. op1,op2,op3,dest,temporary,op1High,op2High: Assembler.Operand;
  2149. index: SHORTINT; temp: Assembler.Operand;
  2150. left: BOOLEAN;
  2151. ecx,ticket: Ticket;
  2152. BEGIN
  2153. Assert(instruction.op1.type.form IN IntermediateCode.Integer,"must be integer operand");
  2154. IF instruction.op1.type.form = IntermediateCode.UnsignedInteger THEN
  2155. IF instruction.opcode = IntermediateCode.shr THEN op := InstructionSet.opSHR; left := FALSE;
  2156. ELSIF instruction.opcode = IntermediateCode.shl THEN op := InstructionSet.opSHL; left := TRUE;
  2157. ELSIF instruction.opcode = IntermediateCode.ror THEN op := InstructionSet.opROR; left := FALSE;
  2158. ELSIF instruction.opcode = IntermediateCode.rol THEN op := InstructionSet.opROL; left := TRUE;
  2159. END;
  2160. ELSE
  2161. IF instruction.opcode = IntermediateCode.shr THEN op := InstructionSet.opSAR; left := FALSE;
  2162. ELSIF instruction.opcode = IntermediateCode.shl THEN op := InstructionSet.opSAL; left := TRUE;
  2163. ELSIF instruction.opcode = IntermediateCode.ror THEN op := InstructionSet.opROR; left := FALSE;
  2164. ELSIF instruction.opcode = IntermediateCode.rol THEN op := InstructionSet.opROL; left := TRUE;
  2165. END;
  2166. END;
  2167. IF instruction.op3.mode # IntermediateCode.ModeImmediate THEN
  2168. IF backend.cooperative THEN ap.spillable := TRUE END;
  2169. Spill(physicalRegisters.Mapped(ECX));
  2170. ecx := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int32,ECX,inPC);
  2171. END;
  2172. (*GetTemporaryRegister(instruction.op2.type,dest);*)
  2173. MakeOperand(instruction.op1,Low,op1,NIL);
  2174. IF ~Assembler.IsRegisterOperand(op1) THEN GetTemporaryRegister(instruction.op2.type,dest) ELSE dest := op1 END;
  2175. MakeOperand(instruction.op2,Low,op2,NIL);
  2176. MakeOperand(instruction.op3,Low,op3,NIL);
  2177. IF instruction.op3.mode = IntermediateCode.ModeImmediate THEN
  2178. Assembler.InitImm8(shift,instruction.op3.intValue);
  2179. ELSE
  2180. CASE instruction.op3.type.sizeInBits OF
  2181. IntermediateCode.Bits8: index := CL;
  2182. |IntermediateCode.Bits16: index := CX;
  2183. |IntermediateCode.Bits32: index := ECX;
  2184. |IntermediateCode.Bits64: index := RCX;
  2185. END;
  2186. (*
  2187. IF (physicalRegisters.toVirtual[index] # free) & ((physicalRegisters.toVirtual[index] # instruction.op1.register) OR (instruction.op1.mode # IntermediateCode.ModeRegister)) THEN
  2188. Spill();
  2189. (*
  2190. emitter.Emit1(InstructionSet.opPUSH,opECX);
  2191. ecxPushed := TRUE;
  2192. *)
  2193. END;
  2194. *)
  2195. ticket := virtualRegisters.Mapped(instruction.op3.register,Low);
  2196. IF (instruction.op3.mode # IntermediateCode.ModeRegister) OR (ticket = NIL) OR (ticket.spilled) OR (ticket.register # index) THEN
  2197. emitter.Emit2(InstructionSet.opMOV,registerOperands[index],op3);
  2198. END;
  2199. shift := opCL;
  2200. END;
  2201. IF ~IsComplex(instruction.op1) THEN
  2202. Move(dest,op2,PhysicalOperandType(dest));
  2203. emitter.Emit2 (op, dest,shift);
  2204. Move(op1,dest,PhysicalOperandType(op1));
  2205. ELSIF left THEN
  2206. MakeOperand(instruction.op1,High,op1High,NIL);
  2207. MakeOperand(instruction.op2,High,op2High,NIL);
  2208. IF ~IntermediateCode.OperandEquals(instruction.op1,instruction.op2) THEN
  2209. Move(op1,op2,PhysicalOperandType(op1));
  2210. Move(op1High,op2High,PhysicalOperandType(op1High))
  2211. END;
  2212. IF (instruction.opcode=IntermediateCode.rol) THEN
  2213. (* |high| <- |low| <- |temp=high| *)
  2214. ticket := TemporaryTicket(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int32);
  2215. TicketToOperand(ticket,temp);
  2216. emitter.Emit2( InstructionSet.opMOV, temp, op1High);
  2217. emitter.Emit3( InstructionSet.opSHLD,op1High, op1, shift);
  2218. emitter.Emit3( InstructionSet.opSHLD, op1, temp, shift);
  2219. UnmapTicket(ticket);
  2220. ELSE
  2221. (* |high| <- |low| *)
  2222. emitter.Emit3( InstructionSet.opSHLD, op1,op1High,shift);
  2223. emitter.Emit2( op, op1,shift);
  2224. END;
  2225. ELSE
  2226. IF ~IntermediateCode.OperandEquals(instruction.op1,instruction.op2) THEN
  2227. Move(op1,op2,PhysicalOperandType(op1))
  2228. END;
  2229. IF instruction.opcode=IntermediateCode.ror THEN
  2230. (* |temp=low| -> |high| -> |low| *)
  2231. ticket := TemporaryTicket(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int32);
  2232. TicketToOperand(ticket,temp);
  2233. emitter.Emit2( InstructionSet.opMOV, temporary, op1);
  2234. emitter.Emit3( InstructionSet.opSHRD,op1, op1High, shift);
  2235. emitter.Emit3( InstructionSet.opSHRD, op1High, temporary, shift);
  2236. UnmapTicket(ticket);
  2237. ELSE
  2238. (* |high| -> |low| *)
  2239. emitter.Emit3( InstructionSet.opSHRD, op1,op1High,shift);
  2240. emitter.Emit2( op, op1High, shift);
  2241. END;
  2242. END;
  2243. IF backend.cooperative & (instruction.op3.mode # IntermediateCode.ModeImmediate) THEN
  2244. UnmapTicket(ecx);
  2245. UnSpill(ap);
  2246. ap.spillable := FALSE;
  2247. END;
  2248. END EmitShift;
  2249. PROCEDURE EmitCas(CONST instruction: IntermediateCode.Instruction);
  2250. VAR ra: Ticket; op1,op2,op3,mem: Assembler.Operand; register: LONGINT;
  2251. BEGIN
  2252. CASE instruction.op2.type.sizeInBits OF
  2253. | IntermediateCode.Bits8: register := AL;
  2254. | IntermediateCode.Bits16: register := AX;
  2255. | IntermediateCode.Bits32: register := EAX;
  2256. | IntermediateCode.Bits64: register := RAX;
  2257. END;
  2258. Spill(physicalRegisters.Mapped(register));
  2259. ra := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,instruction.op2.type,register,inPC);
  2260. IF IntermediateCode.OperandEquals (instruction.op2,instruction.op3) THEN
  2261. MakeOperand(instruction.op1,Low,op1,ra);
  2262. Assembler.InitMem(mem,SHORT(instruction.op1.type.sizeInBits DIV 8),op1.register,0);
  2263. emitter.Emit2(InstructionSet.opMOV,op1,mem);
  2264. ELSE
  2265. MakeOperand(instruction.op2,Low,op2,ra);
  2266. MakeRegister(instruction.op1,Low,op1);
  2267. Assembler.InitMem(mem,SHORT(instruction.op2.type.sizeInBits DIV 8),op1.register,0);
  2268. MakeRegister(instruction.op3,Low,op3);
  2269. emitter.EmitPrefix (InstructionSet.prfLOCK);
  2270. emitter.Emit2(InstructionSet.opCMPXCHG,mem,op3);
  2271. END;
  2272. END EmitCas;
  2273. PROCEDURE EmitCopy(CONST instruction: IntermediateCode.Instruction);
  2274. VAR op1,op2,op3: Assembler.Operand; esi, edi, ecx, t: Ticket; temp,imm: Assembler.Operand; source, dest: IntermediateCode.Operand; size: HUGEINT;
  2275. BEGIN
  2276. IF IntermediateCode.IsConstantInteger(instruction.op3, size) & (size = 4) THEN
  2277. Spill(physicalRegisters.Mapped(ESI));
  2278. Spill(physicalRegisters.Mapped(EDI));
  2279. esi := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int32,RS,inPC);
  2280. edi := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int32,RD,inPC);
  2281. MakeOperand(instruction.op1,Low,op1,edi);
  2282. MakeOperand(instruction.op2,Low,op2,esi);
  2283. emitter.Emit0(InstructionSet.opMOVSD);
  2284. UnmapTicket(esi);
  2285. UnmapTicket(edi);
  2286. ELSE
  2287. Spill(physicalRegisters.Mapped(ESI));
  2288. Spill(physicalRegisters.Mapped(EDI));
  2289. IF backend.cooperative THEN ap.spillable := TRUE END;
  2290. Spill(physicalRegisters.Mapped(ECX));
  2291. esi := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int32,RS,inPC);
  2292. edi := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int32,RD,inPC);
  2293. ecx := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int32,RC,inPC);
  2294. MakeOperand(instruction.op1,Low,op1,edi);
  2295. MakeOperand(instruction.op2,Low,op2,esi);
  2296. IF (instruction.op1.mode = IntermediateCode.ModeRegister) & (instruction.op1.register = IntermediateCode.SP) & IntermediateCode.IsConstantInteger(instruction.op3, size) & (size >= 4096) THEN
  2297. (* special case on stack: copy downwards for possible stack allocation *)
  2298. IF size MOD 4 # 0 THEN
  2299. imm := Assembler.NewImm32(size-1);
  2300. emitter.Emit2(InstructionSet.opADD, opEDI, imm);
  2301. emitter.Emit2(InstructionSet.opADD, opESI, imm);
  2302. imm := Assembler.NewImm32(size MOD 4);
  2303. emitter.Emit2(InstructionSet.opMOV, opECX, imm);
  2304. emitter.Emit0(InstructionSet.opSTD); (* copy down *)
  2305. emitter.EmitPrefix (InstructionSet.prfREP);
  2306. emitter.Emit0(InstructionSet.opMOVSB);
  2307. imm := Assembler.NewImm32(size DIV 4);
  2308. emitter.Emit2(InstructionSet.opMOV, opECX, imm);
  2309. emitter.EmitPrefix (InstructionSet.prfREP);
  2310. emitter.Emit0(InstructionSet.opMOVSD);
  2311. ELSE
  2312. imm := Assembler.NewImm32(size-4);
  2313. emitter.Emit2(InstructionSet.opADD, opEDI, imm);
  2314. emitter.Emit2(InstructionSet.opADD, opESI, imm);
  2315. imm := Assembler.NewImm32(size DIV 4);
  2316. emitter.Emit2(InstructionSet.opMOV, opECX, imm);
  2317. emitter.Emit0(InstructionSet.opSTD); (* copy down *)
  2318. emitter.EmitPrefix (InstructionSet.prfREP);
  2319. emitter.Emit0(InstructionSet.opMOVSD);
  2320. END
  2321. ELSIF IntermediateCode.IsConstantInteger(instruction.op3, size) THEN
  2322. imm := Assembler.NewImm32(size DIV 4);
  2323. emitter.Emit2(InstructionSet.opMOV, opECX, imm);
  2324. emitter.Emit0(InstructionSet.opCLD); (* copy upwards *)
  2325. emitter.EmitPrefix (InstructionSet.prfREP);
  2326. emitter.Emit0(InstructionSet.opMOVSD);
  2327. IF size MOD 4 # 0 THEN
  2328. imm := Assembler.NewImm32(size MOD 4);
  2329. emitter.Emit2(InstructionSet.opMOV, opECX, imm);
  2330. emitter.EmitPrefix (InstructionSet.prfREP);
  2331. emitter.Emit0(InstructionSet.opMOVSB);
  2332. END;
  2333. (* this does not work in the kernel -- for whatever reasons *)
  2334. ELSIF (instruction.op1.mode = IntermediateCode.ModeRegister) & (instruction.op1.register = IntermediateCode.SP) THEN
  2335. MakeOperand(instruction.op3,Low,op3,ecx);
  2336. t := TemporaryTicket(IntermediateCode.GeneralPurposeRegister, IntermediateCode.int32);
  2337. TicketToOperand(t, temp);
  2338. emitter.Emit2(InstructionSet.opADD, opESI, opECX);
  2339. emitter.Emit2(InstructionSet.opADD, opEDI, opECX);
  2340. imm := Assembler.NewImm8(1);
  2341. emitter.Emit2(InstructionSet.opSUB, opESI, imm);
  2342. emitter.Emit2(InstructionSet.opSUB, opEDI, imm);
  2343. emitter.Emit2(InstructionSet.opMOV, temp, opECX);
  2344. imm := Assembler.NewImm8(3);
  2345. emitter.Emit2(InstructionSet.opAND, opECX, imm);
  2346. emitter.Emit0(InstructionSet.opSTD); (* copy downwards *)
  2347. emitter.EmitPrefix (InstructionSet.prfREP);
  2348. emitter.Emit0(InstructionSet.opMOVSB);
  2349. imm := Assembler.NewImm8(2);
  2350. emitter.Emit2(InstructionSet.opMOV, opECX, temp);
  2351. emitter.Emit2(InstructionSet.opSHR, opECX, imm);
  2352. imm := Assembler.NewImm8(3);
  2353. emitter.Emit2(InstructionSet.opSUB, opESI, imm);
  2354. emitter.Emit2(InstructionSet.opSUB, opEDI, imm);
  2355. emitter.EmitPrefix (InstructionSet.prfREP);
  2356. emitter.Emit0(InstructionSet.opMOVSD);
  2357. emitter.Emit0(InstructionSet.opCLD);
  2358. ELSE
  2359. MakeOperand(instruction.op3,Low,op3,ecx);
  2360. t := TemporaryTicket(IntermediateCode.GeneralPurposeRegister, IntermediateCode.int32);
  2361. TicketToOperand(t, temp);
  2362. emitter.Emit2(InstructionSet.opMOV, temp, opECX);
  2363. imm := Assembler.NewImm8(3);
  2364. emitter.Emit2(InstructionSet.opAND, temp, imm);
  2365. imm := Assembler.NewImm8(2);
  2366. emitter.Emit2(InstructionSet.opSHR, opECX, imm);
  2367. emitter.Emit0(InstructionSet.opCLD); (* copy upwards *)
  2368. emitter.EmitPrefix (InstructionSet.prfREP);
  2369. emitter.Emit0(InstructionSet.opMOVSD);
  2370. emitter.Emit2(InstructionSet.opMOV, opECX, temp);
  2371. emitter.EmitPrefix (InstructionSet.prfREP);
  2372. emitter.Emit0(InstructionSet.opMOVSB);
  2373. END;
  2374. UnmapTicket(esi);
  2375. UnmapTicket(edi);
  2376. UnmapTicket(ecx);
  2377. IF backend.cooperative THEN
  2378. UnSpill(ap);
  2379. ap.spillable := FALSE;
  2380. END;
  2381. END;
  2382. END EmitCopy;
  2383. PROCEDURE EmitFill(CONST instruction: IntermediateCode.Instruction; down: BOOLEAN);
  2384. VAR reg,sizeInBits,i: LONGINT;val, value, size, dest: Assembler.Operand;
  2385. op: LONGINT;
  2386. edi, ecx: Ticket;
  2387. BEGIN
  2388. IF FALSE & (instruction.op2.mode = IntermediateCode.ModeImmediate) & (instruction.op2.symbol.name = "") & (instruction.op2.intValue < 5) THEN
  2389. sizeInBits := instruction.op3.type.sizeInBits;
  2390. IF sizeInBits = IntermediateCode.Bits8 THEN value := opAL;
  2391. ELSIF sizeInBits = IntermediateCode.Bits16 THEN value := opAX;
  2392. ELSIF sizeInBits = IntermediateCode.Bits32 THEN value := opEAX;
  2393. ELSE HALT(200)
  2394. END;
  2395. MakeOperand(instruction.op1,Low,dest,NIL);
  2396. IF instruction.op1.mode = IntermediateCode.ModeRegister THEN reg := dest.register
  2397. ELSE emitter.Emit2(InstructionSet.opMOV,opEDX,dest); reg := EDX;
  2398. END;
  2399. IF (instruction.op3.mode = IntermediateCode.ModeImmediate) & (instruction.op3.type.form IN IntermediateCode.Integer) & (instruction.op3.intValue = 0) THEN
  2400. emitter.Emit2(InstructionSet.opXOR,opEAX,opEAX);
  2401. ELSE
  2402. MakeOperand(instruction.op3,Low,value,NIL);
  2403. END;
  2404. FOR i := 0 TO SHORT(instruction.op2.intValue)-1 DO
  2405. IF down THEN
  2406. Assembler.InitMem(dest,SHORT(SHORT(sizeInBits DIV 8)),reg,-i*sizeInBits DIV 8);
  2407. ELSE
  2408. Assembler.InitMem(dest,SHORT(SHORT(sizeInBits DIV 8 )),reg,i*sizeInBits DIV 8);
  2409. END;
  2410. emitter.Emit2(InstructionSet.opMOV,dest,value);
  2411. END;
  2412. ELSE
  2413. Spill(physicalRegisters.Mapped(EDI));
  2414. IF backend.cooperative THEN ap.spillable := TRUE END;
  2415. Spill(physicalRegisters.Mapped(ECX));
  2416. edi := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int32,EDI,inPC);
  2417. ecx := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int32,ECX,inPC);
  2418. MakeOperand(instruction.op1,Low,dest,edi);
  2419. MakeOperand(instruction.op2,Low,size,ecx);
  2420. MakeOperand(instruction.op3,Low,value,NIL);
  2421. (*
  2422. emitter.Emit2(InstructionSet.opMOV,opEDI, op1[Low]);
  2423. emitter.Emit2(InstructionSet.opMOV,opECX, op3[Low]);
  2424. *)
  2425. CASE instruction.op3.type.sizeInBits OF
  2426. IntermediateCode.Bits8: val := opAL; op := InstructionSet.opSTOSB;
  2427. |IntermediateCode.Bits16: val := opAX; op := InstructionSet.opSTOSW;
  2428. |IntermediateCode.Bits32: val := opEAX; op := InstructionSet.opSTOSD;
  2429. ELSE Halt("only supported for upto 32 bit integers ");
  2430. END;
  2431. IF (instruction.op3.mode = IntermediateCode.ModeImmediate) & (instruction.op3.type.form IN IntermediateCode.Integer) & (instruction.op3.intValue = 0) THEN
  2432. emitter.Emit2(InstructionSet.opXOR,opEAX,opEAX);
  2433. ELSE
  2434. emitter.Emit2(InstructionSet.opMOV,val,value);
  2435. END;
  2436. IF down THEN
  2437. emitter.Emit0(InstructionSet.opSTD); (* fill downwards *)
  2438. ELSE
  2439. emitter.Emit0(InstructionSet.opCLD); (* fill upwards *)
  2440. END;
  2441. emitter.EmitPrefix (InstructionSet.prfREP);
  2442. emitter.Emit0(op);
  2443. IF down THEN (* needed as calls to windows crash otherwise *)
  2444. emitter.Emit0(InstructionSet.opCLD);
  2445. END;
  2446. UnmapTicket(ecx);
  2447. IF backend.cooperative THEN
  2448. UnSpill(ap);
  2449. ap.spillable := FALSE;
  2450. END;
  2451. END;
  2452. END EmitFill;
  2453. PROCEDURE EmitBr (CONST instruction: IntermediateCode.Instruction);
  2454. VAR dest,destPC,offset: LONGINT; target: Assembler.Operand;hit,fail: LONGINT; reverse: BOOLEAN;
  2455. (* jump operands *) left,right,temp: Assembler.Operand;
  2456. failOp: Assembler.Operand; failPC: LONGINT;
  2457. PROCEDURE JmpDest(brop: LONGINT);
  2458. BEGIN
  2459. IF instruction.op1.mode = IntermediateCode.ModeImmediate THEN
  2460. IF instruction.op1.symbol.name # in.name THEN
  2461. Assembler.InitOffset32(target,instruction.op1.intValue);
  2462. Assembler.SetSymbol(target,instruction.op1.symbol.name,instruction.op1.symbol.fingerprint,instruction.op1.symbolOffset,instruction.op1.offset);
  2463. emitter.Emit1(brop,target);
  2464. ELSE
  2465. dest := (instruction.op1.symbolOffset); (* this is the offset in the in-data section (intermediate code), it is not byte- *)
  2466. destPC := (in.instructions[dest].pc );
  2467. offset := destPC - (out.pc );
  2468. IF dest > inPC THEN (* forward jump *)
  2469. Assembler.InitOffset32(target,0);
  2470. Assembler.SetSymbol(target,instruction.op1.symbol.name,instruction.op1.symbol.fingerprint,instruction.op1.symbolOffset,instruction.op1.offset);
  2471. emitter.Emit1(brop,target);
  2472. ELSIF ABS(offset) <= 126 THEN
  2473. Assembler.InitOffset8(target,destPC);
  2474. emitter.Emit1(brop,target);
  2475. ELSE
  2476. Assembler.InitOffset32(target,destPC);
  2477. emitter.Emit1(brop,target);
  2478. END;
  2479. END;
  2480. ELSE
  2481. MakeOperand(instruction.op1,Low,target,NIL);
  2482. emitter.Emit1(brop,target);
  2483. END;
  2484. END JmpDest;
  2485. PROCEDURE CmpFloat;
  2486. BEGIN
  2487. IF backend.forceFPU THEN
  2488. MakeOperand(instruction.op2,Low,left,NIL);
  2489. emitter.Emit1(InstructionSet.opFLD,left); INC(fpStackPointer);
  2490. MakeOperand(instruction.op3,Low,right,NIL);
  2491. emitter.Emit1(InstructionSet.opFCOMP,right); DEC(fpStackPointer);
  2492. emitter.Emit1(InstructionSet.opFNSTSW,opAX);
  2493. emitter.Emit0(InstructionSet.opSAHF);
  2494. ELSE
  2495. MakeRegister(instruction.op2,Low,left);
  2496. MakeOperand(instruction.op3,Low,right,NIL);
  2497. IF instruction.op2.type.sizeInBits = 32 THEN
  2498. emitter.Emit2(InstructionSet.opCOMISS, left, right);
  2499. ELSE
  2500. emitter.Emit2(InstructionSet.opCOMISD, left, right);
  2501. END
  2502. END;
  2503. END CmpFloat;
  2504. PROCEDURE Cmp(part: LONGINT; VAR reverse: BOOLEAN);
  2505. VAR type: IntermediateCode.Type; left,right: Assembler.Operand;
  2506. BEGIN
  2507. IF (instruction.op2.mode = IntermediateCode.ModeImmediate) & (instruction.op3.mode = IntermediateCode.ModeImmediate) THEN
  2508. reverse := FALSE;
  2509. GetPartType(instruction.op2.type,part,type);
  2510. GetTemporaryRegister(type,temp);
  2511. MakeOperand(instruction.op2,part,left,NIL);
  2512. MakeOperand(instruction.op3,part,right,NIL);
  2513. Move(temp,left, type);
  2514. left := temp;
  2515. ELSIF instruction.op2.mode = IntermediateCode.ModeImmediate THEN
  2516. reverse := TRUE;
  2517. MakeOperand(instruction.op2,part,right,NIL);
  2518. MakeOperand(instruction.op3,part,left,NIL);
  2519. ELSIF IsMemoryOperand(instruction.op2,part) & IsMemoryOperand(instruction.op3,part) THEN
  2520. reverse := FALSE;
  2521. GetPartType(instruction.op2.type,part,type);
  2522. GetTemporaryRegister(type,temp);
  2523. MakeOperand(instruction.op2,part,left,NIL);
  2524. MakeOperand(instruction.op3,part,right,NIL);
  2525. Move(temp,right,type);
  2526. right := temp;
  2527. ELSE
  2528. reverse := FALSE;
  2529. MakeOperand(instruction.op2,part,left,NIL);
  2530. MakeOperand(instruction.op3,part,right,NIL);
  2531. END;
  2532. emitter.Emit2(InstructionSet.opCMP,left,right);
  2533. END Cmp;
  2534. BEGIN
  2535. IF (instruction.op1.symbol.name = in.name) & (instruction.op1.symbolOffset = inPC +1) THEN (* jump to next instruction can be ignored *)
  2536. IF dump # NIL THEN dump.String("jump to next instruction ignored"); dump.Ln END;
  2537. RETURN
  2538. END;
  2539. failPC := 0;
  2540. IF instruction.opcode = IntermediateCode.br THEN
  2541. hit := InstructionSet.opJMP
  2542. ELSIF instruction.op2.type.form = IntermediateCode.Float THEN
  2543. CmpFloat;
  2544. CASE instruction.opcode OF
  2545. IntermediateCode.breq: hit := InstructionSet.opJE;
  2546. |IntermediateCode.brne:hit := InstructionSet.opJNE;
  2547. |IntermediateCode.brge: hit := InstructionSet.opJAE
  2548. |IntermediateCode.brlt: hit := InstructionSet.opJB
  2549. END;
  2550. ELSE
  2551. IF ~IsComplex(instruction.op2) THEN
  2552. Cmp(Low,reverse);
  2553. CASE instruction.opcode OF
  2554. IntermediateCode.breq: hit := InstructionSet.opJE;
  2555. |IntermediateCode.brne: hit := InstructionSet.opJNE;
  2556. |IntermediateCode.brge:
  2557. IF instruction.op2.type.form = IntermediateCode.SignedInteger THEN
  2558. IF reverse THEN hit := InstructionSet.opJLE ELSE hit := InstructionSet.opJGE END;
  2559. ELSIF instruction.op2.type.form = IntermediateCode.UnsignedInteger THEN
  2560. IF reverse THEN hit := InstructionSet.opJBE ELSE hit := InstructionSet.opJAE END;
  2561. END;
  2562. |IntermediateCode.brlt:
  2563. IF instruction.op2.type.form = IntermediateCode.SignedInteger THEN
  2564. IF reverse THEN hit := InstructionSet.opJG ELSE hit := InstructionSet.opJL END;
  2565. ELSIF instruction.op2.type.form = IntermediateCode.UnsignedInteger THEN
  2566. IF reverse THEN hit := InstructionSet.opJA ELSE hit := InstructionSet.opJB END;
  2567. END;
  2568. END;
  2569. ELSE
  2570. Assert(instruction.op2.type.form = IntermediateCode.SignedInteger,"no unsigned integer64");
  2571. Cmp(High,reverse);
  2572. CASE instruction.opcode OF
  2573. IntermediateCode.breq: hit := 0; fail := InstructionSet.opJNE;
  2574. |IntermediateCode.brne: hit := InstructionSet.opJNE; fail := 0;
  2575. |IntermediateCode.brge:
  2576. IF reverse THEN hit := InstructionSet.opJL; fail := InstructionSet.opJG;
  2577. ELSE hit := InstructionSet.opJG; fail := InstructionSet.opJL
  2578. END;
  2579. |IntermediateCode.brlt:
  2580. IF reverse THEN hit := InstructionSet.opJG; fail := InstructionSet.opJL
  2581. ELSE hit := InstructionSet.opJL; fail := InstructionSet.opJG
  2582. END;
  2583. END;
  2584. IF hit # 0 THEN JmpDest(hit) END;
  2585. IF fail # 0 THEN
  2586. failPC := out.pc; (* to avoid potential value overflow problem, will be patched anyway *)
  2587. Assembler.InitOffset8(failOp,failPC );
  2588. emitter.Emit1(fail,failOp);
  2589. failPC := failOp.pc;
  2590. END;
  2591. Cmp(Low,reverse);
  2592. CASE instruction.opcode OF
  2593. IntermediateCode.breq: hit := InstructionSet.opJE
  2594. |IntermediateCode.brne: hit := InstructionSet.opJNE
  2595. |IntermediateCode.brge:
  2596. IF reverse THEN hit := InstructionSet.opJBE ELSE hit := InstructionSet.opJAE END;
  2597. |IntermediateCode.brlt:
  2598. IF reverse THEN hit := InstructionSet.opJA ELSE hit := InstructionSet.opJB END;
  2599. END;
  2600. END;
  2601. END;
  2602. JmpDest(hit);
  2603. IF failPC > 0 THEN out.PutByteAt(failPC,(out.pc-failPC)-1); END;
  2604. END EmitBr;
  2605. PROCEDURE EmitPush(CONST vop: IntermediateCode.Operand; part: LONGINT);
  2606. VAR index: LONGINT; type,cpuType: IntermediateCode.Type; op1: Assembler.Operand; ra: Ticket;
  2607. BEGIN
  2608. GetPartType(vop.type,part,type);
  2609. ASSERT(type.form IN IntermediateCode.Integer);
  2610. IF vop.mode = IntermediateCode.ModeImmediate THEN (* may not push 16 bit immediate: strange instruction in 32 / 64 bit mode *)
  2611. GetImmediate(vop,part,op1,TRUE);
  2612. emitter.Emit1(InstructionSet.opPUSH,op1);
  2613. ELSIF (type.sizeInBits = cpuBits) THEN
  2614. MakeOperand(vop,part,op1,NIL);
  2615. emitter.Emit1(InstructionSet.opPUSH,op1);
  2616. ELSE
  2617. ASSERT(type.sizeInBits < cpuBits);
  2618. MakeOperand(vop,part,op1,NIL);
  2619. IF Assembler.IsRegisterOperand(op1) & ~((cpuBits=32) & (type.sizeInBits=8) & (op1.register >= AH)) THEN
  2620. index := op1.register MOD 32 + opRA.register;
  2621. emitter.Emit1(InstructionSet.opPUSH, registerOperands[index]);
  2622. ELSE
  2623. WHILE physicalRegisters.Mapped(opRA.register) # free DO Spill(physicalRegisters.Mapped(opRA.register)) END;
  2624. IntermediateCode.InitType(cpuType,IntermediateCode.SignedInteger,SHORT(cpuBits));
  2625. ra := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,cpuType,opRA.register,inPC);
  2626. CASE type.sizeInBits OF
  2627. 8: index := AL
  2628. |16: index := AX
  2629. |32: index := EAX
  2630. |64: index := RAX
  2631. END;
  2632. emitter.Emit2(InstructionSet.opMOV,registerOperands[index],op1);
  2633. emitter.Emit1(InstructionSet.opPUSH,opRA);
  2634. UnmapTicket(ra);
  2635. END;
  2636. END;
  2637. END EmitPush;
  2638. PROCEDURE EmitPop(CONST vop: IntermediateCode.Operand; part: LONGINT);
  2639. VAR index: LONGINT; type,cpuType: IntermediateCode.Type; op1: Assembler.Operand; ra: Ticket;
  2640. BEGIN
  2641. GetPartType(vop.type,part,type);
  2642. ASSERT(type.form IN IntermediateCode.Integer);
  2643. IF (type.sizeInBits = cpuBits) THEN
  2644. MakeOperand(vop,part,op1,NIL);
  2645. emitter.Emit1(InstructionSet.opPOP,op1);
  2646. ELSE
  2647. ASSERT(type.sizeInBits < cpuBits);
  2648. MakeOperand(vop,part,op1,NIL);
  2649. IF Assembler.IsRegisterOperand(op1) & ~((cpuBits=32) & (type.sizeInBits=8) & (op1.register >= AH)) THEN
  2650. index := op1.register MOD 32 + opRA.register;
  2651. emitter.Emit1(InstructionSet.opPOP, registerOperands[index]);
  2652. ELSE
  2653. WHILE physicalRegisters.Mapped(opRA.register) # free DO Spill(physicalRegisters.Mapped(opRA.register)) END;
  2654. IntermediateCode.InitType(cpuType, IntermediateCode.SignedInteger, SHORT(cpuBits));
  2655. ra := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,cpuType,opRA.register,inPC);
  2656. emitter.Emit1(InstructionSet.opPOP,opRA);
  2657. CASE type.sizeInBits OF
  2658. 8: index := AL
  2659. |16: index := AX
  2660. |32: index := EAX
  2661. |64: index := RAX
  2662. END;
  2663. emitter.Emit2(InstructionSet.opMOV, op1, registerOperands[index]);
  2664. UnmapTicket(ra);
  2665. END;
  2666. END;
  2667. END EmitPop;
  2668. PROCEDURE EmitPushFloat(CONST vop: IntermediateCode.Operand);
  2669. VAR sizeInBytes,length: LONGINT; memop: Assembler.Operand; op: Assembler.Operand;
  2670. BEGIN
  2671. MakeOperand(vop,Low,op,NIL);
  2672. length := vop.type.length;
  2673. IF (vop.mode = IntermediateCode.ModeMemory) & (vop.type.sizeInBits*length =cpuBits) THEN
  2674. emitter.Emit1(InstructionSet.opPUSH,op);
  2675. ELSE
  2676. sizeInBytes := vop.type.sizeInBits DIV 8;
  2677. length := vop.type.length;
  2678. AllocateStack(sizeInBytes*length);
  2679. Assembler.InitMem(memop, SHORTINT(sizeInBytes*length),SP,0);
  2680. IF backend.forceFPU THEN
  2681. emitter.Emit1(InstructionSet.opFLD,op); INC(fpStackPointer);
  2682. emitter.Emit1(InstructionSet.opFSTP,memop); DEC(fpStackPointer);
  2683. ELSE
  2684. Move(memop, op, vop.type)
  2685. END
  2686. END;
  2687. END EmitPushFloat;
  2688. PROCEDURE EmitPopFloat(CONST vop: IntermediateCode.Operand);
  2689. VAR sizeInBytes,length: LONGINT; memop: Assembler.Operand; op: Assembler.Operand;
  2690. BEGIN
  2691. sizeInBytes := vop.type.sizeInBits DIV 8;
  2692. length := vop.type.length;
  2693. IF (vop.mode = IntermediateCode.ModeMemory) & (vop.type.sizeInBits*length =cpuBits) THEN
  2694. MakeOperand(vop,Low,op,NIL);
  2695. emitter.Emit1(InstructionSet.opPOP,op);
  2696. ELSE
  2697. Assembler.InitMem(memop, SHORTINT(sizeInBytes*length),SP,0);
  2698. IF backend.forceFPU THEN
  2699. emitter.Emit1(InstructionSet.opFLD,memop);
  2700. INC(fpStackPointer);
  2701. MakeOperand(vop,Low,op,NIL);
  2702. emitter.Emit1(InstructionSet.opFSTP,op);
  2703. DEC(fpStackPointer);
  2704. ASSERT(sizeInBytes > 0);
  2705. ELSE
  2706. MakeOperand(vop,Low,op,NIL);
  2707. Move(op, memop, vop.type)
  2708. END;
  2709. AllocateStack(-sizeInBytes*length);
  2710. END;
  2711. END EmitPopFloat;
  2712. PROCEDURE EmitNeg(CONST instruction: IntermediateCode.Instruction);
  2713. VAR opLow,opHigh: Assembler.Operand; minusOne: Assembler.Operand; ticketLow,ticketHigh: Ticket;
  2714. BEGIN
  2715. IF IsComplex(instruction.op1) THEN
  2716. PrepareOp2(instruction,High,opHigh,ticketHigh);
  2717. PrepareOp2(instruction,Low,opLow,ticketLow);
  2718. emitter.Emit1(InstructionSet.opNOT,opHigh);
  2719. emitter.Emit1(InstructionSet.opNEG,opLow);
  2720. Assembler.InitImm8(minusOne,-1);
  2721. emitter.Emit2(InstructionSet.opSBB,opHigh,minusOne);
  2722. FinishOp(instruction.op1,High,opHigh,ticketHigh);
  2723. FinishOp(instruction.op1,Low,opLow,ticketLow);
  2724. ELSE
  2725. EmitArithmetic2(instruction,Low,InstructionSet.opNEG);
  2726. END;
  2727. END EmitNeg;
  2728. PROCEDURE EmitNegXMM(CONST instruction: IntermediateCode.Instruction);
  2729. VAR temp, op: Assembler.Operand; ticket: Ticket;
  2730. BEGIN
  2731. PrepareOp2(instruction, Low, op, ticket);
  2732. GetTemporaryRegister(instruction.op1.type,temp);
  2733. IF instruction.op1.type.sizeInBits = 32 THEN
  2734. emitter.Emit2(InstructionSet.opXORPS, temp, temp);
  2735. emitter.Emit2(InstructionSet.opSUBPS, temp, op);
  2736. emitter.Emit2(InstructionSet.opMOVAPS, op, temp);
  2737. ELSE
  2738. emitter.Emit2(InstructionSet.opXORPD, temp, temp);
  2739. emitter.Emit2(InstructionSet.opSUBPD, temp, op);
  2740. emitter.Emit2(InstructionSet.opMOVAPS, op, temp);
  2741. END;
  2742. FinishOp(instruction.op1, Low, op, ticket);
  2743. END EmitNegXMM;
  2744. PROCEDURE EmitAbs(CONST instruction: IntermediateCode.Instruction);
  2745. VAR op1,op2: Assembler.Operand; source,imm: Assembler.Operand; eax: Ticket;
  2746. BEGIN
  2747. Assert(~IsComplex(instruction.op1),"complex Abs not supported");
  2748. IF instruction.op1.type.form = IntermediateCode.SignedInteger THEN
  2749. Spill(physicalRegisters.Mapped(EAX));
  2750. eax := ReservePhysicalRegister(IntermediateCode.GeneralPurposeRegister,IntermediateCode.int32,EAX,inPC);
  2751. MakeOperand(instruction.op1,Low,op1,NIL);
  2752. MakeOperand(instruction.op2,Low,op2,NIL);
  2753. CASE instruction.op1.type.sizeInBits OF
  2754. | IntermediateCode.Bits8: imm := Assembler.NewImm8 (7); source := opAL;
  2755. | IntermediateCode.Bits16: imm := Assembler.NewImm8 (15); source := opAX;
  2756. | IntermediateCode.Bits32: imm := Assembler.NewImm8 (31); source := opEAX;
  2757. END;
  2758. emitter.Emit2 (InstructionSet.opMOV, source,op2);
  2759. emitter.Emit2 (InstructionSet.opMOV, op1,source);
  2760. emitter.Emit2 (InstructionSet.opSAR, source, imm);
  2761. emitter.Emit2 (InstructionSet.opXOR, op1, source);
  2762. emitter.Emit2 (InstructionSet.opSUB, op1, source);
  2763. UnmapTicket(eax);
  2764. ELSE Halt("Abs does not make sense on unsigned integer")
  2765. END;
  2766. END EmitAbs;
  2767. PROCEDURE EmitAbsXMM(CONST instruction: IntermediateCode.Instruction);
  2768. VAR temp, op: Assembler.Operand; ticket: Ticket;
  2769. BEGIN
  2770. PrepareOp2(instruction, Low, op, ticket);
  2771. GetTemporaryRegister(instruction.op1.type,temp);
  2772. IF instruction.op1.type.sizeInBits = 32 THEN
  2773. emitter.Emit2(InstructionSet.opXORPS, temp, temp);
  2774. emitter.Emit2(InstructionSet.opSUBPS, temp, op);
  2775. emitter.Emit2(InstructionSet.opMAXPS, op, temp);
  2776. ELSE
  2777. emitter.Emit2(InstructionSet.opXORPD, temp, temp);
  2778. emitter.Emit2(InstructionSet.opSUBPD, temp, op);
  2779. emitter.Emit2(InstructionSet.opMAXPD, op, temp);
  2780. END;
  2781. FinishOp(instruction.op1, Low, op, ticket);
  2782. END EmitAbsXMM;
  2783. PROCEDURE EmitTrap(CONST instruction: IntermediateCode.Instruction);
  2784. VAR operand: Assembler.Operand;
  2785. BEGIN
  2786. IF instruction.op1.intValue < 80H THEN
  2787. operand := Assembler.NewImm8(instruction.op1.intValue);
  2788. ELSE
  2789. operand := Assembler.NewImm32(instruction.op1.intValue);
  2790. END;
  2791. emitter.Emit1(InstructionSet.opPUSH, operand);
  2792. emitter.Emit0(InstructionSet.opINT3);
  2793. END EmitTrap;
  2794. PROCEDURE EmitAsm(CONST instruction: IntermediateCode.Instruction);
  2795. VAR reader: Streams.StringReader; procedure: SyntaxTree.Procedure; scope: SyntaxTree.Scope;
  2796. len: LONGINT; symbol: SyntaxTree.Symbol; assembler: Assembler.Assembly;
  2797. inr, outr: IntermediateCode.Rules;
  2798. string: SyntaxTree.SourceCode;
  2799. i: LONGINT;
  2800. reg, dest: Assembler.Operand;
  2801. map: Assembler.RegisterMap;
  2802. register: LONGINT;
  2803. ticket: Ticket;
  2804. BEGIN
  2805. IF instruction.op2.mode = IntermediateCode.ModeRule THEN inr := instruction.op2.rule ELSE inr := NIL END;
  2806. IF instruction.op3.mode = IntermediateCode.ModeRule THEN outr := instruction.op3.rule ELSE outr := NIL END;
  2807. string := instruction.op1.string;
  2808. NEW(map);
  2809. IF inr # NIL THEN
  2810. FOR i := 0 TO LEN(inr)-1 DO
  2811. MakeRegister(inr[i], 0, reg);
  2812. ASSERT(map.Find(inr[i].string^) < 0);
  2813. map.Add(inr[i].string, reg.register)
  2814. END;
  2815. END;
  2816. IF outr # NIL THEN
  2817. FOR i := 0 TO LEN(outr)-1 DO
  2818. IF (map.Find(outr[i].string^) < 0) THEN
  2819. GetTemporaryRegister(outr[i].type,reg);
  2820. map.Add(outr[i].string, reg.register)
  2821. END;
  2822. END;
  2823. END;
  2824. len := Strings.Length(string^);
  2825. NEW(reader,len);
  2826. reader.Set(string^);
  2827. symbol := in.symbol;
  2828. procedure := symbol(SyntaxTree.Procedure);
  2829. scope := procedure.procedureScope;
  2830. NEW(assembler,diagnostics,emitter);
  2831. assembler.Assemble(reader,SHORT(instruction.op1.intValue),scope,in,in,module,procedure.access * SyntaxTree.Public # {}, procedure.isInline, map) ;
  2832. error := error OR assembler.error;
  2833. IF outr # NIL THEN
  2834. FOR i := 0 TO LEN(outr)-1 DO
  2835. IF outr[i].mode # IntermediateCode.Undefined THEN
  2836. register := map.Find(outr[i].string^);
  2837. ticket := physicalRegisters.Mapped(register);
  2838. IF ticket.lastuse = inPC THEN UnmapTicket(ticket); physicalRegisters.AllocationHint(register) END; (* try to reuse register here *)
  2839. Assembler.InitRegister(reg, register);
  2840. MakeOperand(outr[i], Low, dest, NIL);
  2841. IF outr[i].type.length > 1 THEN
  2842. SpecialMove(InstructionSet.opMOVUPS,InstructionSet.opMOVUPS, TRUE, dest, reg, outr[i].type)
  2843. ELSE
  2844. Move( dest, reg,outr[i].type)
  2845. END;
  2846. END;
  2847. END;
  2848. END;
  2849. (*
  2850. IntermediateCode.SetString(instruction.op1, string);
  2851. *)
  2852. END EmitAsm;
  2853. END CodeGeneratorAMD64;
  2854. BackendAMD64= OBJECT (IntermediateBackend.IntermediateBackend)
  2855. VAR
  2856. cg: CodeGeneratorAMD64;
  2857. bits: LONGINT;
  2858. traceable: BOOLEAN;
  2859. forceFPU: BOOLEAN;
  2860. winAPIRegisters, cRegisters: Backend.Registers;
  2861. PROCEDURE &InitBackendAMD64;
  2862. BEGIN
  2863. InitIntermediateBackend;
  2864. bits := 32;
  2865. forceFPU := FALSE;
  2866. NEW(winAPIRegisters, 4);
  2867. winAPIRegisters[0] := RCX - RAX;
  2868. winAPIRegisters[1] := RDX - RAX;
  2869. winAPIRegisters[2] := R8 - RAX;
  2870. winAPIRegisters[3] := R9 - RAX;
  2871. NEW(cRegisters, 6);
  2872. cRegisters[0] := RDI - RAX;
  2873. cRegisters[1] := RSI - RAX;
  2874. cRegisters[2] := RDX - RAX;
  2875. cRegisters[3] := RCX - RAX;
  2876. cRegisters[4] := R8 - RAX;
  2877. cRegisters[5] := R9 - RAX;
  2878. END InitBackendAMD64;
  2879. PROCEDURE Initialize(diagnostics: Diagnostics.Diagnostics; log: Streams.Writer; flags: SET; checker: SemanticChecker.Checker; system: Global.System);
  2880. BEGIN
  2881. Initialize^(diagnostics,log, flags,checker,system); NEW(cg, runtimeModuleName, diagnostics, SELF);
  2882. END Initialize;
  2883. PROCEDURE GetSystem(): Global.System;
  2884. VAR system: Global.System;
  2885. PROCEDURE AddRegister(CONST name: Scanner.IdentifierString; val: LONGINT);
  2886. BEGIN
  2887. Global.NewConstant(name,val,system.shortintType,system.systemScope)
  2888. END AddRegister;
  2889. PROCEDURE AddRegisters;
  2890. BEGIN
  2891. (* system constants *)
  2892. AddRegister("EAX",InstructionSet.regEAX); AddRegister("ECX", InstructionSet.regECX);
  2893. AddRegister( "EDX", InstructionSet.regEDX); AddRegister( "EBX", InstructionSet.regEBX);
  2894. AddRegister( "ESP", InstructionSet.regESP); AddRegister( "EBP", InstructionSet.regEBP);
  2895. AddRegister( "ESI", InstructionSet.regESI); AddRegister( "EDI", InstructionSet.regEDI);
  2896. AddRegister( "AX", InstructionSet.regAX); AddRegister( "CX", InstructionSet.regCX);
  2897. AddRegister( "DX", InstructionSet.regDX); AddRegister( "BX", InstructionSet.regBX);
  2898. AddRegister( "AL", InstructionSet.regAL); AddRegister( "CL", InstructionSet.regCL);
  2899. AddRegister( "DL", InstructionSet.regDL); AddRegister( "BL", InstructionSet.regBL);
  2900. AddRegister( "AH", InstructionSet.regAH); AddRegister( "CH", InstructionSet.regCH);
  2901. AddRegister( "DH", InstructionSet.regDH); AddRegister( "BH", InstructionSet.regBH);
  2902. AddRegister( "RAX", InstructionSet.regRAX); AddRegister( "RCX", InstructionSet.regRCX);
  2903. AddRegister( "RDX", InstructionSet.regRDX); AddRegister( "RBX", InstructionSet.regRBX);
  2904. AddRegister( "RSP", InstructionSet.regRSP); AddRegister( "RBP", InstructionSet.regRBP);
  2905. AddRegister( "RSI", InstructionSet.regRSI); AddRegister( "RDI", InstructionSet.regRDI);
  2906. AddRegister( "R8", InstructionSet.regR8); AddRegister( "R9", InstructionSet.regR9);
  2907. AddRegister( "R10", InstructionSet.regR10); AddRegister( "R11", InstructionSet.regR11);
  2908. AddRegister( "R12", InstructionSet.regR12); AddRegister( "R13", InstructionSet.regR13);
  2909. AddRegister( "R14", InstructionSet.regR14); AddRegister( "R15", InstructionSet.regR15);
  2910. AddRegister( "R8D", InstructionSet.regR8D); AddRegister( "R9D", InstructionSet.regR9D);
  2911. AddRegister( "R10D", InstructionSet.regR10D); AddRegister( "R11D", InstructionSet.regR11D);
  2912. AddRegister( "R12D", InstructionSet.regR12D); AddRegister( "R13D", InstructionSet.regR13D);
  2913. AddRegister( "R14D", InstructionSet.regR14D); AddRegister( "R15D", InstructionSet.regR15D);
  2914. AddRegister( "R8W", InstructionSet.regR8W); AddRegister( "R9W", InstructionSet.regR9W);
  2915. AddRegister( "R10W", InstructionSet.regR10W); AddRegister( "R11W", InstructionSet.regR11W);
  2916. AddRegister( "R12W", InstructionSet.regR12W); AddRegister( "R13W", InstructionSet.regR13W);
  2917. AddRegister( "R14W", InstructionSet.regR14W); AddRegister( "R15W", InstructionSet.regR15W);
  2918. AddRegister( "R8B", InstructionSet.regR8B); AddRegister( "R9B", InstructionSet.regR9B);
  2919. AddRegister( "R10B", InstructionSet.regR10B); AddRegister( "R11B", InstructionSet.regR11B);
  2920. AddRegister( "R12B", InstructionSet.regR12B); AddRegister( "R13B", InstructionSet.regR13B);
  2921. AddRegister( "R14B", InstructionSet.regR14B); AddRegister( "R15B", InstructionSet.regR15B);
  2922. END AddRegisters;
  2923. BEGIN
  2924. IF system = NIL THEN
  2925. IF bits=32 THEN
  2926. NEW(system,8,8,32, 8,32,32,32,64,cooperative);
  2927. Global.SetDefaultDeclarations(system,8);
  2928. Global.SetDefaultOperators(system);
  2929. ELSE
  2930. NEW(system,8,8,64,8,64,64,64,128,cooperative);
  2931. Global.SetDefaultDeclarations(system,8);
  2932. Global.SetDefaultOperators(system);
  2933. END;
  2934. system.SetRegisterPassCallback(CanPassInRegister);
  2935. AddRegisters
  2936. END;
  2937. RETURN system
  2938. END GetSystem;
  2939. (* return index of general purpose register used as parameter register in calling convention *)
  2940. PROCEDURE GetParameterRegisters*(callingConvention: SyntaxTree.CallingConvention): Backend.Registers;
  2941. BEGIN
  2942. IF bits = 32 THEN
  2943. RETURN NIL;
  2944. ELSE
  2945. CASE callingConvention OF
  2946. SyntaxTree.CCallingConvention: RETURN cRegisters;
  2947. |SyntaxTree.WinAPICallingConvention: RETURN winAPIRegisters;
  2948. |SyntaxTree.DarwinCCallingConvention: RETURN cRegisters;
  2949. ELSE
  2950. RETURN NIL;
  2951. END;
  2952. END
  2953. END GetParameterRegisters;
  2954. PROCEDURE SupportedInstruction(CONST instruction: IntermediateCode.Instruction; VAR moduleName, procedureName: ARRAY OF CHAR): BOOLEAN;
  2955. BEGIN
  2956. RETURN cg.Supported(instruction,moduleName,procedureName);
  2957. END SupportedInstruction;
  2958. PROCEDURE GenerateBinary(module: Sections.Module; dump: Streams.Writer);
  2959. VAR
  2960. in: Sections.Section;
  2961. out: BinaryCode.Section;
  2962. name: Basic.SegmentedName;
  2963. procedure: SyntaxTree.Procedure;
  2964. i, j, initialSectionCount: LONGINT;
  2965. (* recompute fixup positions and assign binary sections *)
  2966. PROCEDURE PatchFixups(section: BinaryCode.Section);
  2967. VAR resolved: BinaryCode.Section; fixup: BinaryCode.Fixup; displacement,symbolOffset: LONGINT; in: IntermediateCode.Section;
  2968. symbol: Sections.Section;
  2969. BEGIN
  2970. fixup := section.fixupList.firstFixup;
  2971. WHILE fixup # NIL DO
  2972. symbol := module.allSections.FindByName(fixup.symbol.name);
  2973. IF (symbol # NIL) & (symbol(IntermediateCode.Section).resolved # NIL) THEN
  2974. resolved := symbol(IntermediateCode.Section).resolved(BinaryCode.Section);
  2975. in := symbol(IntermediateCode.Section);
  2976. symbolOffset := fixup.symbolOffset;
  2977. IF symbolOffset = in.pc THEN
  2978. displacement := resolved.pc
  2979. ELSIF (symbolOffset # 0) THEN
  2980. ASSERT(in.pc > symbolOffset);
  2981. displacement := in.instructions[symbolOffset].pc;
  2982. ELSE
  2983. displacement := 0;
  2984. END;
  2985. fixup.SetSymbol(fixup.symbol.name,fixup.symbol.fingerprint,0,fixup.displacement+displacement);
  2986. END;
  2987. fixup := fixup.nextFixup;
  2988. END;
  2989. END PatchFixups;
  2990. BEGIN
  2991. cg.SetModule(module);
  2992. FOR i := 0 TO module.allSections.Length() - 1 DO
  2993. in := module.allSections.GetSection(i);
  2994. IF in.type = Sections.InlineCodeSection THEN
  2995. name := in.name;
  2996. out := ResolvedSection(in(IntermediateCode.Section));
  2997. cg.Section(in(IntermediateCode.Section),out);
  2998. procedure := in.symbol(SyntaxTree.Procedure);
  2999. IF procedure.procedureScope.body.code # NIL THEN
  3000. procedure.procedureScope.body.code.SetBinaryCode(out.os.bits);
  3001. END;
  3002. END
  3003. END;
  3004. initialSectionCount := 0;
  3005. REPEAT
  3006. j := initialSectionCount;
  3007. initialSectionCount := module.allSections.Length() ;
  3008. FOR i := j TO initialSectionCount - 1 DO
  3009. in := module.allSections.GetSection(i);
  3010. IF (in.type # Sections.InlineCodeSection) & (in(IntermediateCode.Section).resolved = NIL) THEN
  3011. name := in.name;
  3012. out := ResolvedSection(in(IntermediateCode.Section));
  3013. cg.Section(in(IntermediateCode.Section),out);
  3014. IF out.os.type = Sections.VarSection THEN
  3015. IF out.pc = 1 THEN out.SetAlignment(FALSE,1)
  3016. ELSIF out.pc = 2 THEN out.SetAlignment(FALSE,2)
  3017. ELSIF out.pc > 2 THEN out.SetAlignment(FALSE,4)
  3018. END;
  3019. ELSIF out.os.type = Sections.ConstSection THEN
  3020. out.SetAlignment(FALSE,4);
  3021. END;
  3022. END
  3023. END
  3024. UNTIL initialSectionCount = module.allSections.Length(); (* process remaining sections that have been added during traversal of sections *)
  3025. (*
  3026. FOR i := 0 TO module.allSections.Length() - 1 DO
  3027. in := module.allSections.GetSection(i);
  3028. IF in.kind = Sections.CaseTableKind THEN
  3029. IF in(IntermediateCode.Section).resolved = NIL THEN
  3030. out := ResolvedSection(in(IntermediateCode.Section));
  3031. cg.Section(in(IntermediateCode.Section),out);
  3032. END
  3033. END
  3034. END;
  3035. *)
  3036. FOR i := 0 TO module.allSections.Length() - 1 DO
  3037. in := module.allSections.GetSection(i);
  3038. PatchFixups(in(IntermediateCode.Section).resolved)
  3039. END;
  3040. (*
  3041. FOR i := 0 TO module.allSections.Length() - 1 DO
  3042. in := module.allSections.GetSection(i);
  3043. IF in.kind = Sections.CaseTableKind THEN
  3044. PatchFixups(in(IntermediateCode.Section).resolved)
  3045. END
  3046. END;
  3047. *)
  3048. IF cg.error THEN Error("",Diagnostics.Invalid, Diagnostics.Invalid,"") END;
  3049. END GenerateBinary;
  3050. (* genasm *)
  3051. PROCEDURE ProcessIntermediateCodeModule*(intermediateCodeModule: Formats.GeneratedModule): Formats.GeneratedModule;
  3052. VAR
  3053. result: Formats.GeneratedModule;
  3054. BEGIN
  3055. ASSERT(intermediateCodeModule IS Sections.Module);
  3056. result := ProcessIntermediateCodeModule^(intermediateCodeModule);
  3057. IF ~error THEN
  3058. GenerateBinary(result(Sections.Module),dump);
  3059. IF dump # NIL THEN
  3060. dump.Ln; dump.Ln;
  3061. dump.String(";------------------ binary code -------------------"); dump.Ln;
  3062. IF (traceString="") OR (traceString="*") THEN
  3063. result.Dump(dump);
  3064. dump.Update
  3065. ELSE
  3066. Sections.DumpFiltered(dump, result(Sections.Module), traceString);
  3067. dump.Update;
  3068. END
  3069. END;
  3070. END;
  3071. RETURN result
  3072. FINALLY
  3073. IF dump # NIL THEN
  3074. dump.Ln; dump.Ln;
  3075. dump.String("; ------------------ rescued code (code generation trapped) -------------------"); dump.Ln;
  3076. IF (traceString="") OR (traceString="*") THEN
  3077. result.Dump(dump);
  3078. dump.Update
  3079. ELSE
  3080. Sections.DumpFiltered(dump, result(Sections.Module), traceString);
  3081. dump.Update;
  3082. END
  3083. END;
  3084. HALT(100); (* do not continue compiling after trap *)
  3085. RETURN result
  3086. END ProcessIntermediateCodeModule;
  3087. PROCEDURE FindPC(x: SyntaxTree.Module; CONST sectionName: ARRAY OF CHAR; sectionOffset: LONGINT);
  3088. VAR
  3089. section: Sections.Section; binarySection: BinaryCode.Section; label: BinaryCode.LabelList; module: Formats.GeneratedModule;
  3090. i: LONGINT; pooledName: Basic.SegmentedName;
  3091. BEGIN
  3092. module := ProcessSyntaxTreeModule(x);
  3093. Basic.ToSegmentedName(sectionName, pooledName);
  3094. i := 0;
  3095. REPEAT
  3096. section := module(Sections.Module).allSections.GetSection(i);
  3097. INC(i);
  3098. UNTIL (i = module(Sections.Module).allSections.Length()) OR (section.name = pooledName);
  3099. IF section.name # pooledName THEN
  3100. diagnostics.Error(module.module.sourceName,Diagnostics.Invalid,Diagnostics.Invalid," could not locate pc");
  3101. ELSE
  3102. binarySection := section(IntermediateCode.Section).resolved;
  3103. label := binarySection.labels;
  3104. WHILE (label # NIL) & (label.offset >= sectionOffset) DO
  3105. label := label.prev;
  3106. END;
  3107. IF label # NIL THEN
  3108. diagnostics.Information(module.module.sourceName,label.position,Diagnostics.Invalid," pc position");
  3109. ELSE
  3110. diagnostics.Error(module.module.sourceName,Diagnostics.Invalid,Diagnostics.Invalid," could not locate pc");
  3111. END;
  3112. END;
  3113. END FindPC;
  3114. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  3115. VAR length: LONGINT; baseType: SyntaxTree.Type; b: BOOLEAN;
  3116. BEGIN
  3117. b := SemanticChecker.IsStaticMathArray(type, length, baseType) & (baseType IS SyntaxTree.FloatType) & (baseType.sizeInBits = 32) & (length = 4);
  3118. RETURN b
  3119. END CanPassInRegister;
  3120. PROCEDURE GetDescription*(VAR instructionSet: ARRAY OF CHAR);
  3121. BEGIN instructionSet := "AMD";
  3122. END GetDescription;
  3123. PROCEDURE DefineOptions(options: Options.Options);
  3124. BEGIN
  3125. options.Add(0X,"bits",Options.Integer);
  3126. options.Add(0X,"traceable", Options.Flag);
  3127. options.Add(0X,"useFPU", Options.Flag);
  3128. DefineOptions^(options);
  3129. END DefineOptions;
  3130. PROCEDURE GetOptions(options: Options.Options);
  3131. BEGIN
  3132. IF ~options.GetInteger("bits",bits) THEN bits := 32 END;
  3133. traceable := options.GetFlag("traceable");
  3134. forceFPU := options.GetFlag("useFPU");
  3135. GetOptions^(options);
  3136. END GetOptions;
  3137. PROCEDURE DefaultObjectFileFormat(): Formats.ObjectFileFormat;
  3138. BEGIN RETURN ObjectFileFormat.Get();
  3139. END DefaultObjectFileFormat;
  3140. PROCEDURE DefaultSymbolFileFormat(): Formats.SymbolFileFormat;
  3141. BEGIN
  3142. RETURN NIL
  3143. END DefaultSymbolFileFormat;
  3144. END BackendAMD64;
  3145. (** the number of regular sections in a section list **)
  3146. PROCEDURE RegularSectionCount(sectionList: Sections.SectionList): LONGINT;
  3147. VAR
  3148. section: Sections.Section;
  3149. i, result: LONGINT;
  3150. BEGIN
  3151. result := 0;
  3152. FOR i := 0 TO sectionList.Length() - 1 DO
  3153. section := sectionList.GetSection(i);
  3154. INC(result)
  3155. END;
  3156. RETURN result
  3157. END RegularSectionCount;
  3158. PROCEDURE Assert(b: BOOLEAN; CONST s: ARRAY OF CHAR);
  3159. BEGIN
  3160. ASSERT(b,100);
  3161. END Assert;
  3162. PROCEDURE Halt(CONST s: ARRAY OF CHAR);
  3163. BEGIN
  3164. HALT(100);
  3165. END Halt;
  3166. PROCEDURE ResolvedSection(in: IntermediateCode.Section): BinaryCode.Section;
  3167. VAR section: BinaryCode.Section;
  3168. BEGIN
  3169. IF in.resolved = NIL THEN
  3170. NEW(section,in.type, in.priority, 8, in.name,in.comments # NIL,FALSE);
  3171. section.SetAlignment(in.fixed, in.positionOrAlignment);
  3172. in.SetResolved(section);
  3173. ELSE
  3174. section := in.resolved
  3175. END;
  3176. RETURN section
  3177. END ResolvedSection;
  3178. PROCEDURE Init;
  3179. VAR i: LONGINT;
  3180. BEGIN
  3181. FOR i := 0 TO LEN(registerOperands)-1 DO
  3182. Assembler.InitRegister(registerOperands[i],i);
  3183. END;
  3184. opEAX := registerOperands[EAX];
  3185. opEBX := registerOperands[EBX];
  3186. opECX := registerOperands[ECX];
  3187. opEDX := registerOperands[EDX];
  3188. opESI := registerOperands[ESI];
  3189. opEDI := registerOperands[EDI];
  3190. opEBP := registerOperands[EBP];
  3191. opESP := registerOperands[ESP];
  3192. opRSP := registerOperands[RSP];
  3193. opRBP := registerOperands[RBP];
  3194. opAX := registerOperands[AX];
  3195. opBX := registerOperands[BX];
  3196. opCX := registerOperands[CX];
  3197. opDX := registerOperands[DX];
  3198. opSI := registerOperands[SI];
  3199. opDI := registerOperands[DI];
  3200. opAL := registerOperands[AL];
  3201. opBL := registerOperands[BL];
  3202. opCL := registerOperands[CL];
  3203. opDL := registerOperands[DL];
  3204. opAH := registerOperands[AH];
  3205. opBH := registerOperands[BH];
  3206. opCH := registerOperands[CH];
  3207. opDH := registerOperands[DH];
  3208. opST0 := registerOperands[ST0];
  3209. NEW(unusable); NEW(blocked); NEW(split); free := NIL;
  3210. END Init;
  3211. PROCEDURE Get*(): Backend.Backend;
  3212. VAR backend: BackendAMD64;
  3213. BEGIN NEW(backend); RETURN backend
  3214. END Get;
  3215. PROCEDURE Trace*;
  3216. BEGIN
  3217. TRACE(traceStackSize);
  3218. END Trace;
  3219. BEGIN
  3220. traceStackSize := 0;
  3221. Init;
  3222. usePool := Machine.NumberOfProcessors()>1;
  3223. END FoxAMDBackend.