FoxAMDBackend.Mod 141 KB

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