FoxAMDBackend.Mod 134 KB

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