FoxAMDBackend.Mod 136 KB

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