FoxAMDBackend.Mod 135 KB

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