FoxAMDBackend.Mod 133 KB

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