FoxAMDBackend.Mod 143 KB

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