FoxAMDBackend.Mod 143 KB

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