FoxAMDBackend.Mod 139 KB

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