aasmcpu.pas 113 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl and Peter Vreman
  3. Contains the abstract assembler implementation for the i386
  4. * Portions of this code was inspired by the NASM sources
  5. The Netwide Assembler is Copyright (c) 1996 Simon Tatham and
  6. Julian Hall. All rights reserved.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. ****************************************************************************
  19. }
  20. unit aasmcpu;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. globtype,verbose,
  25. cpubase,
  26. cgbase,cgutils,
  27. symtype,
  28. aasmbase,aasmtai,aasmdata,aasmsym,
  29. ogbase;
  30. const
  31. { "mov reg,reg" source operand number }
  32. O_MOV_SOURCE = 0;
  33. { "mov reg,reg" destination operand number }
  34. O_MOV_DEST = 1;
  35. { Operand types }
  36. OT_NONE = $00000000;
  37. { Bits 0..7: sizes }
  38. OT_BITS8 = $00000001;
  39. OT_BITS16 = $00000002;
  40. OT_BITS32 = $00000004;
  41. OT_BITS64 = $00000008; { x86_64 and FPU }
  42. OT_BITS128 = $10000000; { 16 byte SSE }
  43. OT_BITS256 = $20000000; { 32 byte AVX }
  44. OT_BITS80 = $00000010; { FPU only }
  45. OT_FAR = $00000020; { this means 16:16 or 16:32, like in CALL/JMP }
  46. OT_NEAR = $00000040;
  47. OT_SHORT = $00000080;
  48. { TODO: FAR/NEAR/SHORT are sizes too, they should be included into size mask,
  49. but this requires adjusting the opcode table }
  50. OT_SIZE_MASK = $3000001F; { all the size attributes }
  51. OT_NON_SIZE = longint(not OT_SIZE_MASK);
  52. { Bits 8..11: modifiers }
  53. OT_SIGNED = $00000100; { the operand need to be signed -128-127 }
  54. OT_TO = $00000200; { reverse effect in FADD, FSUB &c }
  55. OT_COLON = $00000400; { operand is followed by a colon }
  56. OT_MODIFIER_MASK = $00000F00;
  57. { Bits 12..15: type of operand }
  58. OT_REGISTER = $00001000;
  59. OT_IMMEDIATE = $00002000;
  60. OT_MEMORY = $0000C000; { always includes 'OT_REGMEM' bit as well }
  61. OT_REGMEM = $00008000; { for r/m, ie EA, operands }
  62. OT_TYPE_MASK = OT_REGISTER or OT_IMMEDIATE or OT_MEMORY or OT_REGMEM;
  63. OT_REGNORM = OT_REGISTER or OT_REGMEM; { 'normal' reg, qualifies as EA }
  64. { Bits 20..22, 24..26: register classes
  65. otf_* consts are not used alone, only to build other constants. }
  66. otf_reg_cdt = $00100000;
  67. otf_reg_gpr = $00200000;
  68. otf_reg_sreg = $00400000;
  69. otf_reg_fpu = $01000000;
  70. otf_reg_mmx = $02000000;
  71. otf_reg_xmm = $04000000;
  72. otf_reg_ymm = $08000000;
  73. { Bits 16..19: subclasses, meaning depends on classes field }
  74. otf_sub0 = $00010000;
  75. otf_sub1 = $00020000;
  76. otf_sub2 = $00040000;
  77. otf_sub3 = $00080000;
  78. OT_REG_SMASK = otf_sub0 or otf_sub1 or otf_sub2 or otf_sub3;
  79. OT_REG_TYPMASK = otf_reg_cdt or otf_reg_gpr or otf_reg_sreg or otf_reg_fpu or otf_reg_mmx or otf_reg_xmm or otf_reg_ymm;
  80. { register class 0: CRx, DRx and TRx }
  81. {$ifdef x86_64}
  82. OT_REG_CDT = OT_REGISTER or otf_reg_cdt or OT_BITS64;
  83. {$else x86_64}
  84. OT_REG_CDT = OT_REGISTER or otf_reg_cdt or OT_BITS32;
  85. {$endif x86_64}
  86. OT_REG_CREG = OT_REG_CDT or otf_sub0; { CRn }
  87. OT_REG_DREG = OT_REG_CDT or otf_sub1; { DRn }
  88. OT_REG_TREG = OT_REG_CDT or otf_sub2; { TRn }
  89. OT_REG_CR4 = OT_REG_CDT or otf_sub3; { CR4 (Pentium only) }
  90. { register class 1: general-purpose registers }
  91. OT_REG_GPR = OT_REGNORM or otf_reg_gpr;
  92. OT_RM_GPR = OT_REGMEM or otf_reg_gpr;
  93. OT_REG8 = OT_REG_GPR or OT_BITS8; { 8-bit GPR }
  94. OT_REG16 = OT_REG_GPR or OT_BITS16;
  95. OT_REG32 = OT_REG_GPR or OT_BITS32;
  96. OT_REG64 = OT_REG_GPR or OT_BITS64;
  97. { GPR subclass 0: accumulator: AL, AX, EAX or RAX }
  98. OT_REG_ACCUM = OT_REG_GPR or otf_sub0;
  99. OT_REG_AL = OT_REG_ACCUM or OT_BITS8;
  100. OT_REG_AX = OT_REG_ACCUM or OT_BITS16;
  101. OT_REG_EAX = OT_REG_ACCUM or OT_BITS32;
  102. {$ifdef x86_64}
  103. OT_REG_RAX = OT_REG_ACCUM or OT_BITS64;
  104. {$endif x86_64}
  105. { GPR subclass 1: counter: CL, CX, ECX or RCX }
  106. OT_REG_COUNT = OT_REG_GPR or otf_sub1;
  107. OT_REG_CL = OT_REG_COUNT or OT_BITS8;
  108. OT_REG_CX = OT_REG_COUNT or OT_BITS16;
  109. OT_REG_ECX = OT_REG_COUNT or OT_BITS32;
  110. {$ifdef x86_64}
  111. OT_REG_RCX = OT_REG_COUNT or OT_BITS64;
  112. {$endif x86_64}
  113. { GPR subclass 2: data register: DL, DX, EDX or RDX }
  114. OT_REG_DX = OT_REG_GPR or otf_sub2 or OT_BITS16;
  115. OT_REG_EDX = OT_REG_GPR or otf_sub2 or OT_BITS32;
  116. { register class 2: Segment registers }
  117. OT_REG_SREG = OT_REGISTER or otf_reg_sreg or OT_BITS16;
  118. OT_REG_CS = OT_REG_SREG or otf_sub0; { CS }
  119. OT_REG_DESS = OT_REG_SREG or otf_sub1; { DS, ES, SS (non-CS 86 registers) }
  120. OT_REG_FSGS = OT_REG_SREG or otf_sub2; { FS, GS (386 extended registers) }
  121. { register class 3: FPU registers }
  122. OT_FPUREG = OT_REGISTER or otf_reg_fpu;
  123. OT_FPU0 = OT_FPUREG or otf_sub0; { FPU stack register zero }
  124. { register class 4: MMX (both reg and r/m) }
  125. OT_MMXREG = OT_REGNORM or otf_reg_mmx;
  126. OT_MMXRM = OT_REGMEM or otf_reg_mmx;
  127. { register class 5: XMM (both reg and r/m) }
  128. OT_XMMREG = OT_REGNORM or otf_reg_xmm;
  129. OT_XMMRM = OT_REGMEM or otf_reg_xmm;
  130. { register class 5: XMM (both reg and r/m) }
  131. OT_YMMREG = OT_REGNORM or otf_reg_ymm;
  132. OT_YMMRM = OT_REGMEM or otf_reg_ymm;
  133. { Memory operands }
  134. OT_MEM8 = OT_MEMORY or OT_BITS8;
  135. OT_MEM16 = OT_MEMORY or OT_BITS16;
  136. OT_MEM32 = OT_MEMORY or OT_BITS32;
  137. OT_MEM64 = OT_MEMORY or OT_BITS64;
  138. OT_MEM128 = OT_MEMORY or OT_BITS128;
  139. OT_MEM256 = OT_MEMORY or OT_BITS256;
  140. OT_MEM80 = OT_MEMORY or OT_BITS80;
  141. OT_MEM_OFFS = OT_MEMORY or otf_sub0; { special type of EA }
  142. { simple [address] offset }
  143. { Matches any type of r/m operand }
  144. OT_MEMORY_ANY = OT_MEMORY or OT_RM_GPR or OT_XMMRM or OT_MMXRM or OT_YMMRM;
  145. { Immediate operands }
  146. OT_IMM8 = OT_IMMEDIATE or OT_BITS8;
  147. OT_IMM16 = OT_IMMEDIATE or OT_BITS16;
  148. OT_IMM32 = OT_IMMEDIATE or OT_BITS32;
  149. OT_IMM64 = OT_IMMEDIATE or OT_BITS64;
  150. OT_ONENESS = otf_sub0; { special type of immediate operand }
  151. OT_UNITY = OT_IMMEDIATE or OT_ONENESS; { for shift/rotate instructions }
  152. { Size of the instruction table converted by nasmconv.pas }
  153. {$if defined(x86_64)}
  154. instabentries = {$i x8664nop.inc}
  155. {$elseif defined(i386)}
  156. instabentries = {$i i386nop.inc}
  157. {$elseif defined(i8086)}
  158. instabentries = {$i i8086nop.inc}
  159. {$endif}
  160. maxinfolen = 8;
  161. MaxInsChanges = 3; { Max things a instruction can change }
  162. type
  163. { What an instruction can change. Needed for optimizer and spilling code.
  164. Note: The order of this enumeration is should not be changed! }
  165. TInsChange = (Ch_None,
  166. {Read from a register}
  167. Ch_REAX, Ch_RECX, Ch_REDX, Ch_REBX, Ch_RESP, Ch_REBP, Ch_RESI, Ch_REDI,
  168. {write from a register}
  169. Ch_WEAX, Ch_WECX, Ch_WEDX, Ch_WEBX, Ch_WESP, Ch_WEBP, Ch_WESI, Ch_WEDI,
  170. {read and write from/to a register}
  171. Ch_RWEAX, Ch_RWECX, Ch_RWEDX, Ch_RWEBX, Ch_RWESP, Ch_RWEBP, Ch_RWESI, Ch_RWEDI,
  172. {modify the contents of a register with the purpose of using
  173. this changed content afterwards (add/sub/..., but e.g. not rep
  174. or movsd)}
  175. Ch_MEAX, Ch_MECX, Ch_MEDX, Ch_MEBX, Ch_MESP, Ch_MEBP, Ch_MESI, Ch_MEDI,
  176. Ch_CDirFlag {clear direction flag}, Ch_SDirFlag {set dir flag},
  177. Ch_RFlags, Ch_WFlags, Ch_RWFlags, Ch_FPU,
  178. Ch_Rop1, Ch_Wop1, Ch_RWop1,Ch_Mop1,
  179. Ch_Rop2, Ch_Wop2, Ch_RWop2,Ch_Mop2,
  180. Ch_Rop3, Ch_WOp3, Ch_RWOp3,Ch_Mop3,
  181. Ch_WMemEDI,
  182. Ch_All,
  183. { x86_64 registers }
  184. Ch_RRAX, Ch_RRCX, Ch_RRDX, Ch_RRBX, Ch_RRSP, Ch_RRBP, Ch_RRSI, Ch_RRDI,
  185. Ch_WRAX, Ch_WRCX, Ch_WRDX, Ch_WRBX, Ch_WRSP, Ch_WRBP, Ch_WRSI, Ch_WRDI,
  186. Ch_RWRAX, Ch_RWRCX, Ch_RWRDX, Ch_RWRBX, Ch_RWRSP, Ch_RWRBP, Ch_RWRSI, Ch_RWRDI,
  187. Ch_MRAX, Ch_MRCX, Ch_MRDX, Ch_MRBX, Ch_MRSP, Ch_MRBP, Ch_MRSI, Ch_MRDI
  188. );
  189. TInsProp = packed record
  190. Ch : Array[1..MaxInsChanges] of TInsChange;
  191. end;
  192. TMemRefSizeInfo = (msiUnkown, msiUnsupported, msiNoSize,
  193. msiMultiple, msiMultiple8, msiMultiple16, msiMultiple32,
  194. msiMultiple64, msiMultiple128, msiMultiple256,
  195. msiMemRegSize, msiMemRegx64y128, msiMemRegx64y256,
  196. msiMem8, msiMem16, msiMem32, msiMem64, msiMem128, msiMem256);
  197. TConstSizeInfo = (csiUnkown, csiMultiple, csiNoSize, csiMem8, csiMem16, csiMem32, csiMem64);
  198. TInsTabMemRefSizeInfoRec = record
  199. MemRefSize : TMemRefSizeInfo;
  200. ExistsSSEAVX: boolean;
  201. ConstSize : TConstSizeInfo;
  202. end;
  203. const
  204. MemRefMultiples: set of TMemRefSizeInfo = [msiMultiple, msiMultiple8,
  205. msiMultiple16, msiMultiple32,
  206. msiMultiple64, msiMultiple128,
  207. msiMultiple256];
  208. InsProp : array[tasmop] of TInsProp =
  209. {$if defined(x86_64)}
  210. {$i x8664pro.inc}
  211. {$elseif defined(i386)}
  212. {$i i386prop.inc}
  213. {$elseif defined(i8086)}
  214. {$i i8086prop.inc}
  215. {$endif}
  216. type
  217. TOperandOrder = (op_intel,op_att);
  218. tinsentry=packed record
  219. opcode : tasmop;
  220. ops : byte;
  221. optypes : array[0..max_operands-1] of longint;
  222. code : array[0..maxinfolen] of char;
  223. flags : int64;
  224. end;
  225. pinsentry=^tinsentry;
  226. { alignment for operator }
  227. tai_align = class(tai_align_abstract)
  228. reg : tregister;
  229. constructor create(b:byte);override;
  230. constructor create_op(b: byte; _op: byte);override;
  231. function calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;override;
  232. end;
  233. taicpu = class(tai_cpu_abstract_sym)
  234. opsize : topsize;
  235. constructor op_none(op : tasmop);
  236. constructor op_none(op : tasmop;_size : topsize);
  237. constructor op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  238. constructor op_const(op : tasmop;_size : topsize;_op1 : aint);
  239. constructor op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  240. constructor op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  241. constructor op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  242. constructor op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aint);
  243. constructor op_const_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister);
  244. constructor op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aint);
  245. constructor op_const_ref(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference);
  246. constructor op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  247. constructor op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  248. constructor op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;_op3 : tregister);
  249. constructor op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference;_op3 : tregister);
  250. constructor op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister; const _op3 : treference);
  251. constructor op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;const _op3 : treference);
  252. { this is for Jmp instructions }
  253. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  254. constructor op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  255. constructor op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  256. constructor op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  257. constructor op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  258. procedure changeopsize(siz:topsize);
  259. function GetString:string;
  260. { This is a workaround for the GAS non commutative fpu instruction braindamage.
  261. Early versions of the UnixWare assembler had a bug where some fpu instructions
  262. were reversed and GAS still keeps this "feature" for compatibility.
  263. for details: http://sourceware.org/binutils/docs/as/i386_002dBugs.html#i386_002dBugs
  264. http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=372528
  265. http://en.wikibooks.org/wiki/X86_Assembly/GAS_Syntax#Caveats
  266. Since FPC is "GAS centric" due to its history it generates instructions with the same operand order so
  267. when generating output for other assemblers, the opcodes must be fixed before writing them.
  268. This function returns the fixed opcodes. Changing the opcodes permanently is no good idea
  269. because in case of smartlinking assembler is generated twice so at the second run wrong
  270. assembler is generated.
  271. }
  272. function FixNonCommutativeOpcodes: tasmop;
  273. private
  274. FOperandOrder : TOperandOrder;
  275. procedure init(_size : topsize); { this need to be called by all constructor }
  276. public
  277. { the next will reset all instructions that can change in pass 2 }
  278. procedure ResetPass1;override;
  279. procedure ResetPass2;override;
  280. function CheckIfValid:boolean;
  281. function Pass1(objdata:TObjData):longint;override;
  282. procedure Pass2(objdata:TObjData);override;
  283. procedure SetOperandOrder(order:TOperandOrder);
  284. function is_same_reg_move(regtype: Tregistertype):boolean;override;
  285. { register spilling code }
  286. function spilling_get_operation_type(opnr: longint): topertype;override;
  287. private
  288. { next fields are filled in pass1, so pass2 is faster }
  289. insentry : PInsEntry;
  290. insoffset : longint;
  291. LastInsOffset : longint; { need to be public to be reset }
  292. inssize : shortint;
  293. {$ifdef x86_64}
  294. rex : byte;
  295. {$endif x86_64}
  296. function InsEnd:longint;
  297. procedure create_ot(objdata:TObjData);
  298. function Matches(p:PInsEntry):boolean;
  299. function calcsize(p:PInsEntry):shortint;
  300. procedure gencode(objdata:TObjData);
  301. function NeedAddrPrefix(opidx:byte):boolean;
  302. procedure Swapoperands;
  303. function FindInsentry(objdata:TObjData):boolean;
  304. end;
  305. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  306. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  307. function MemRefInfo(aAsmop: TAsmOp): TInsTabMemRefSizeInfoRec;
  308. procedure InitAsm;
  309. procedure DoneAsm;
  310. implementation
  311. uses
  312. cutils,
  313. globals,
  314. systems,
  315. procinfo,
  316. itcpugas,
  317. symsym;
  318. {*****************************************************************************
  319. Instruction table
  320. *****************************************************************************}
  321. const
  322. {Instruction flags }
  323. IF_NONE = $00000000;
  324. IF_SM = $00000001; { size match first two operands }
  325. IF_SM2 = $00000002;
  326. IF_SB = $00000004; { unsized operands can't be non-byte }
  327. IF_SW = $00000008; { unsized operands can't be non-word }
  328. IF_SD = $00000010; { unsized operands can't be nondword }
  329. IF_SMASK = $0000001f;
  330. IF_AR0 = $00000020; { SB, SW, SD applies to argument 0 }
  331. IF_AR1 = $00000040; { SB, SW, SD applies to argument 1 }
  332. IF_AR2 = $00000060; { SB, SW, SD applies to argument 2 }
  333. IF_ARMASK = $00000060; { mask for unsized argument spec }
  334. IF_ARSHIFT = 5; { LSB of IF_ARMASK }
  335. IF_PRIV = $00000100; { it's a privileged instruction }
  336. IF_SMM = $00000200; { it's only valid in SMM }
  337. IF_PROT = $00000400; { it's protected mode only }
  338. IF_NOX86_64 = $00000800; { removed instruction in x86_64 }
  339. IF_UNDOC = $00001000; { it's an undocumented instruction }
  340. IF_FPU = $00002000; { it's an FPU instruction }
  341. IF_MMX = $00004000; { it's an MMX instruction }
  342. { it's a 3DNow! instruction }
  343. IF_3DNOW = $00008000;
  344. { it's a SSE (KNI, MMX2) instruction }
  345. IF_SSE = $00010000;
  346. { SSE2 instructions }
  347. IF_SSE2 = $00020000;
  348. { SSE3 instructions }
  349. IF_SSE3 = $00040000;
  350. { SSE64 instructions }
  351. IF_SSE64 = $00080000;
  352. { the mask for processor types }
  353. {IF_PMASK = longint($FF000000);}
  354. { the mask for disassembly "prefer" }
  355. {IF_PFMASK = longint($F001FF00);}
  356. { SVM instructions }
  357. IF_SVM = $00100000;
  358. { SSE4 instructions }
  359. IF_SSE4 = $00200000;
  360. { TODO: These flags were added to make x86ins.dat more readable.
  361. Values must be reassigned to make any other use of them. }
  362. IF_SSSE3 = $00200000;
  363. IF_SSE41 = $00200000;
  364. IF_SSE42 = $00200000;
  365. IF_AVX = $00200000;
  366. IF_SANDYBRIDGE = $00200000;
  367. IF_8086 = $00000000; { 8086 instruction }
  368. IF_186 = $01000000; { 186+ instruction }
  369. IF_286 = $02000000; { 286+ instruction }
  370. IF_386 = $03000000; { 386+ instruction }
  371. IF_486 = $04000000; { 486+ instruction }
  372. IF_PENT = $05000000; { Pentium instruction }
  373. IF_P6 = $06000000; { P6 instruction }
  374. IF_KATMAI = $07000000; { Katmai instructions }
  375. { Willamette instructions }
  376. IF_WILLAMETTE = $08000000;
  377. { Prescott instructions }
  378. IF_PRESCOTT = $09000000;
  379. IF_X86_64 = $0a000000;
  380. IF_CYRIX = $0b000000; { Cyrix-specific instruction }
  381. IF_AMD = $0c000000; { AMD-specific instruction }
  382. IF_CENTAUR = $0d000000; { centaur-specific instruction }
  383. { added flags }
  384. IF_PRE = $40000000; { it's a prefix instruction }
  385. IF_PASS2 = $80000000; { if the instruction can change in a second pass }
  386. type
  387. TInsTabCache=array[TasmOp] of longint;
  388. PInsTabCache=^TInsTabCache;
  389. TInsTabMemRefSizeInfoCache=array[TasmOp] of TInsTabMemRefSizeInfoRec;
  390. PInsTabMemRefSizeInfoCache=^TInsTabMemRefSizeInfoCache;
  391. const
  392. {$if defined(x86_64)}
  393. InsTab:array[0..instabentries-1] of TInsEntry={$i x8664tab.inc}
  394. {$elseif defined(i386)}
  395. InsTab:array[0..instabentries-1] of TInsEntry={$i i386tab.inc}
  396. {$elseif defined(i8086)}
  397. InsTab:array[0..instabentries-1] of TInsEntry={$i i8086tab.inc}
  398. {$endif}
  399. var
  400. InsTabCache : PInsTabCache;
  401. InsTabMemRefSizeInfoCache: PInsTabMemRefSizeInfoCache;
  402. const
  403. {$if defined(x86_64)}
  404. { Intel style operands ! }
  405. opsize_2_type:array[0..2,topsize] of longint=(
  406. (OT_NONE,
  407. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,OT_BITS64,OT_BITS64,OT_BITS64,
  408. OT_BITS16,OT_BITS32,OT_BITS64,
  409. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  410. OT_BITS64,
  411. OT_NEAR,OT_FAR,OT_SHORT,
  412. OT_NONE,
  413. OT_BITS128,
  414. OT_BITS256
  415. ),
  416. (OT_NONE,
  417. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,OT_BITS8,OT_BITS16,OT_BITS32,
  418. OT_BITS16,OT_BITS32,OT_BITS64,
  419. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  420. OT_BITS64,
  421. OT_NEAR,OT_FAR,OT_SHORT,
  422. OT_NONE,
  423. OT_BITS128,
  424. OT_BITS256
  425. ),
  426. (OT_NONE,
  427. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,
  428. OT_BITS16,OT_BITS32,OT_BITS64,
  429. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  430. OT_BITS64,
  431. OT_NEAR,OT_FAR,OT_SHORT,
  432. OT_NONE,
  433. OT_BITS128,
  434. OT_BITS256
  435. )
  436. );
  437. reg_ot_table : array[tregisterindex] of longint = (
  438. {$i r8664ot.inc}
  439. );
  440. {$elseif defined(i386)}
  441. { Intel style operands ! }
  442. opsize_2_type:array[0..2,topsize] of longint=(
  443. (OT_NONE,
  444. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,
  445. OT_BITS16,OT_BITS32,OT_BITS64,
  446. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  447. OT_BITS64,
  448. OT_NEAR,OT_FAR,OT_SHORT,
  449. OT_NONE,
  450. OT_BITS128,
  451. OT_BITS256
  452. ),
  453. (OT_NONE,
  454. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,
  455. OT_BITS16,OT_BITS32,OT_BITS64,
  456. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  457. OT_BITS64,
  458. OT_NEAR,OT_FAR,OT_SHORT,
  459. OT_NONE,
  460. OT_BITS128,
  461. OT_BITS256
  462. ),
  463. (OT_NONE,
  464. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,
  465. OT_BITS16,OT_BITS32,OT_BITS64,
  466. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  467. OT_BITS64,
  468. OT_NEAR,OT_FAR,OT_SHORT,
  469. OT_NONE,
  470. OT_BITS128,
  471. OT_BITS256
  472. )
  473. );
  474. reg_ot_table : array[tregisterindex] of longint = (
  475. {$i r386ot.inc}
  476. );
  477. {$elseif defined(i8086)}
  478. { Intel style operands ! }
  479. opsize_2_type:array[0..2,topsize] of longint=(
  480. (OT_NONE,
  481. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,
  482. OT_BITS16,OT_BITS32,OT_BITS64,
  483. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  484. OT_BITS64,
  485. OT_NEAR,OT_FAR,OT_SHORT,
  486. OT_NONE,
  487. OT_BITS128,
  488. OT_BITS256
  489. ),
  490. (OT_NONE,
  491. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,
  492. OT_BITS16,OT_BITS32,OT_BITS64,
  493. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  494. OT_BITS64,
  495. OT_NEAR,OT_FAR,OT_SHORT,
  496. OT_NONE,
  497. OT_BITS128,
  498. OT_BITS256
  499. ),
  500. (OT_NONE,
  501. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,
  502. OT_BITS16,OT_BITS32,OT_BITS64,
  503. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  504. OT_BITS64,
  505. OT_NEAR,OT_FAR,OT_SHORT,
  506. OT_NONE,
  507. OT_BITS128,
  508. OT_BITS256
  509. )
  510. );
  511. reg_ot_table : array[tregisterindex] of longint = (
  512. {$i r8086ot.inc}
  513. );
  514. {$endif}
  515. function MemRefInfo(aAsmop: TAsmOp): TInsTabMemRefSizeInfoRec;
  516. begin
  517. result := InsTabMemRefSizeInfoCache^[aAsmop];
  518. end;
  519. { Operation type for spilling code }
  520. type
  521. toperation_type_table=array[tasmop,0..Max_Operands] of topertype;
  522. var
  523. operation_type_table : ^toperation_type_table;
  524. {****************************************************************************
  525. TAI_ALIGN
  526. ****************************************************************************}
  527. constructor tai_align.create(b: byte);
  528. begin
  529. inherited create(b);
  530. reg:=NR_ECX;
  531. end;
  532. constructor tai_align.create_op(b: byte; _op: byte);
  533. begin
  534. inherited create_op(b,_op);
  535. reg:=NR_NO;
  536. end;
  537. function tai_align.calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;
  538. const
  539. {$ifdef x86_64}
  540. alignarray:array[0..3] of string[4]=(
  541. #$66#$66#$66#$90,
  542. #$66#$66#$90,
  543. #$66#$90,
  544. #$90
  545. );
  546. {$else x86_64}
  547. alignarray:array[0..5] of string[8]=(
  548. #$8D#$B4#$26#$00#$00#$00#$00,
  549. #$8D#$B6#$00#$00#$00#$00,
  550. #$8D#$74#$26#$00,
  551. #$8D#$76#$00,
  552. #$89#$F6,
  553. #$90);
  554. {$endif x86_64}
  555. var
  556. bufptr : pchar;
  557. j : longint;
  558. localsize: byte;
  559. begin
  560. inherited calculatefillbuf(buf,executable);
  561. if not(use_op) and executable then
  562. begin
  563. bufptr:=pchar(@buf);
  564. { fillsize may still be used afterwards, so don't modify }
  565. { e.g. writebytes(hp.calculatefillbuf(buf)^,hp.fillsize) }
  566. localsize:=fillsize;
  567. while (localsize>0) do
  568. begin
  569. for j:=low(alignarray) to high(alignarray) do
  570. if (localsize>=length(alignarray[j])) then
  571. break;
  572. move(alignarray[j][1],bufptr^,length(alignarray[j]));
  573. inc(bufptr,length(alignarray[j]));
  574. dec(localsize,length(alignarray[j]));
  575. end;
  576. end;
  577. calculatefillbuf:=pchar(@buf);
  578. end;
  579. {*****************************************************************************
  580. Taicpu Constructors
  581. *****************************************************************************}
  582. procedure taicpu.changeopsize(siz:topsize);
  583. begin
  584. opsize:=siz;
  585. end;
  586. procedure taicpu.init(_size : topsize);
  587. begin
  588. { default order is att }
  589. FOperandOrder:=op_att;
  590. segprefix:=NR_NO;
  591. opsize:=_size;
  592. insentry:=nil;
  593. LastInsOffset:=-1;
  594. InsOffset:=0;
  595. InsSize:=0;
  596. end;
  597. constructor taicpu.op_none(op : tasmop);
  598. begin
  599. inherited create(op);
  600. init(S_NO);
  601. end;
  602. constructor taicpu.op_none(op : tasmop;_size : topsize);
  603. begin
  604. inherited create(op);
  605. init(_size);
  606. end;
  607. constructor taicpu.op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  608. begin
  609. inherited create(op);
  610. init(_size);
  611. ops:=1;
  612. loadreg(0,_op1);
  613. end;
  614. constructor taicpu.op_const(op : tasmop;_size : topsize;_op1 : aint);
  615. begin
  616. inherited create(op);
  617. init(_size);
  618. ops:=1;
  619. loadconst(0,_op1);
  620. end;
  621. constructor taicpu.op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  622. begin
  623. inherited create(op);
  624. init(_size);
  625. ops:=1;
  626. loadref(0,_op1);
  627. end;
  628. constructor taicpu.op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  629. begin
  630. inherited create(op);
  631. init(_size);
  632. ops:=2;
  633. loadreg(0,_op1);
  634. loadreg(1,_op2);
  635. end;
  636. constructor taicpu.op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aint);
  637. begin
  638. inherited create(op);
  639. init(_size);
  640. ops:=2;
  641. loadreg(0,_op1);
  642. loadconst(1,_op2);
  643. end;
  644. constructor taicpu.op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  645. begin
  646. inherited create(op);
  647. init(_size);
  648. ops:=2;
  649. loadreg(0,_op1);
  650. loadref(1,_op2);
  651. end;
  652. constructor taicpu.op_const_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister);
  653. begin
  654. inherited create(op);
  655. init(_size);
  656. ops:=2;
  657. loadconst(0,_op1);
  658. loadreg(1,_op2);
  659. end;
  660. constructor taicpu.op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aint);
  661. begin
  662. inherited create(op);
  663. init(_size);
  664. ops:=2;
  665. loadconst(0,_op1);
  666. loadconst(1,_op2);
  667. end;
  668. constructor taicpu.op_const_ref(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference);
  669. begin
  670. inherited create(op);
  671. init(_size);
  672. ops:=2;
  673. loadconst(0,_op1);
  674. loadref(1,_op2);
  675. end;
  676. constructor taicpu.op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  677. begin
  678. inherited create(op);
  679. init(_size);
  680. ops:=2;
  681. loadref(0,_op1);
  682. loadreg(1,_op2);
  683. end;
  684. constructor taicpu.op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  685. begin
  686. inherited create(op);
  687. init(_size);
  688. ops:=3;
  689. loadreg(0,_op1);
  690. loadreg(1,_op2);
  691. loadreg(2,_op3);
  692. end;
  693. constructor taicpu.op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;_op3 : tregister);
  694. begin
  695. inherited create(op);
  696. init(_size);
  697. ops:=3;
  698. loadconst(0,_op1);
  699. loadreg(1,_op2);
  700. loadreg(2,_op3);
  701. end;
  702. constructor taicpu.op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister;const _op3 : treference);
  703. begin
  704. inherited create(op);
  705. init(_size);
  706. ops:=3;
  707. loadreg(0,_op1);
  708. loadreg(1,_op2);
  709. loadref(2,_op3);
  710. end;
  711. constructor taicpu.op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference;_op3 : tregister);
  712. begin
  713. inherited create(op);
  714. init(_size);
  715. ops:=3;
  716. loadconst(0,_op1);
  717. loadref(1,_op2);
  718. loadreg(2,_op3);
  719. end;
  720. constructor taicpu.op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;const _op3 : treference);
  721. begin
  722. inherited create(op);
  723. init(_size);
  724. ops:=3;
  725. loadconst(0,_op1);
  726. loadreg(1,_op2);
  727. loadref(2,_op3);
  728. end;
  729. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  730. begin
  731. inherited create(op);
  732. init(_size);
  733. condition:=cond;
  734. ops:=1;
  735. loadsymbol(0,_op1,0);
  736. end;
  737. constructor taicpu.op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  738. begin
  739. inherited create(op);
  740. init(_size);
  741. ops:=1;
  742. loadsymbol(0,_op1,0);
  743. end;
  744. constructor taicpu.op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  745. begin
  746. inherited create(op);
  747. init(_size);
  748. ops:=1;
  749. loadsymbol(0,_op1,_op1ofs);
  750. end;
  751. constructor taicpu.op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  752. begin
  753. inherited create(op);
  754. init(_size);
  755. ops:=2;
  756. loadsymbol(0,_op1,_op1ofs);
  757. loadreg(1,_op2);
  758. end;
  759. constructor taicpu.op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  760. begin
  761. inherited create(op);
  762. init(_size);
  763. ops:=2;
  764. loadsymbol(0,_op1,_op1ofs);
  765. loadref(1,_op2);
  766. end;
  767. function taicpu.GetString:string;
  768. var
  769. i : longint;
  770. s : string;
  771. addsize : boolean;
  772. begin
  773. s:='['+std_op2str[opcode];
  774. for i:=0 to ops-1 do
  775. begin
  776. with oper[i]^ do
  777. begin
  778. if i=0 then
  779. s:=s+' '
  780. else
  781. s:=s+',';
  782. { type }
  783. addsize:=false;
  784. if (ot and OT_XMMREG)=OT_XMMREG then
  785. s:=s+'xmmreg'
  786. else
  787. if (ot and OT_YMMREG)=OT_YMMREG then
  788. s:=s+'ymmreg'
  789. else
  790. if (ot and OT_MMXREG)=OT_MMXREG then
  791. s:=s+'mmxreg'
  792. else
  793. if (ot and OT_FPUREG)=OT_FPUREG then
  794. s:=s+'fpureg'
  795. else
  796. if (ot and OT_REGISTER)=OT_REGISTER then
  797. begin
  798. s:=s+'reg';
  799. addsize:=true;
  800. end
  801. else
  802. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  803. begin
  804. s:=s+'imm';
  805. addsize:=true;
  806. end
  807. else
  808. if (ot and OT_MEMORY)=OT_MEMORY then
  809. begin
  810. s:=s+'mem';
  811. addsize:=true;
  812. end
  813. else
  814. s:=s+'???';
  815. { size }
  816. if addsize then
  817. begin
  818. if (ot and OT_BITS8)<>0 then
  819. s:=s+'8'
  820. else
  821. if (ot and OT_BITS16)<>0 then
  822. s:=s+'16'
  823. else
  824. if (ot and OT_BITS32)<>0 then
  825. s:=s+'32'
  826. else
  827. if (ot and OT_BITS64)<>0 then
  828. s:=s+'64'
  829. else
  830. if (ot and OT_BITS128)<>0 then
  831. s:=s+'128'
  832. else
  833. if (ot and OT_BITS256)<>0 then
  834. s:=s+'256'
  835. else
  836. s:=s+'??';
  837. { signed }
  838. if (ot and OT_SIGNED)<>0 then
  839. s:=s+'s';
  840. end;
  841. end;
  842. end;
  843. GetString:=s+']';
  844. end;
  845. procedure taicpu.Swapoperands;
  846. var
  847. p : POper;
  848. begin
  849. { Fix the operands which are in AT&T style and we need them in Intel style }
  850. case ops of
  851. 0,1:
  852. ;
  853. 2 : begin
  854. { 0,1 -> 1,0 }
  855. p:=oper[0];
  856. oper[0]:=oper[1];
  857. oper[1]:=p;
  858. end;
  859. 3 : begin
  860. { 0,1,2 -> 2,1,0 }
  861. p:=oper[0];
  862. oper[0]:=oper[2];
  863. oper[2]:=p;
  864. end;
  865. 4 : begin
  866. { 0,1,2,3 -> 3,2,1,0 }
  867. p:=oper[0];
  868. oper[0]:=oper[3];
  869. oper[3]:=p;
  870. p:=oper[1];
  871. oper[1]:=oper[2];
  872. oper[2]:=p;
  873. end;
  874. else
  875. internalerror(201108141);
  876. end;
  877. end;
  878. procedure taicpu.SetOperandOrder(order:TOperandOrder);
  879. begin
  880. if FOperandOrder<>order then
  881. begin
  882. Swapoperands;
  883. FOperandOrder:=order;
  884. end;
  885. end;
  886. function taicpu.FixNonCommutativeOpcodes: tasmop;
  887. begin
  888. result:=opcode;
  889. { we need ATT order }
  890. SetOperandOrder(op_att);
  891. if (
  892. (ops=2) and
  893. (oper[0]^.typ=top_reg) and
  894. (oper[1]^.typ=top_reg) and
  895. { if the first is ST and the second is also a register
  896. it is necessarily ST1 .. ST7 }
  897. ((oper[0]^.reg=NR_ST) or
  898. (oper[0]^.reg=NR_ST0))
  899. ) or
  900. { ((ops=1) and
  901. (oper[0]^.typ=top_reg) and
  902. (oper[0]^.reg in [R_ST1..R_ST7])) or}
  903. (ops=0) then
  904. begin
  905. if opcode=A_FSUBR then
  906. result:=A_FSUB
  907. else if opcode=A_FSUB then
  908. result:=A_FSUBR
  909. else if opcode=A_FDIVR then
  910. result:=A_FDIV
  911. else if opcode=A_FDIV then
  912. result:=A_FDIVR
  913. else if opcode=A_FSUBRP then
  914. result:=A_FSUBP
  915. else if opcode=A_FSUBP then
  916. result:=A_FSUBRP
  917. else if opcode=A_FDIVRP then
  918. result:=A_FDIVP
  919. else if opcode=A_FDIVP then
  920. result:=A_FDIVRP;
  921. end;
  922. if (
  923. (ops=1) and
  924. (oper[0]^.typ=top_reg) and
  925. (getregtype(oper[0]^.reg)=R_FPUREGISTER) and
  926. (oper[0]^.reg<>NR_ST)
  927. ) then
  928. begin
  929. if opcode=A_FSUBRP then
  930. result:=A_FSUBP
  931. else if opcode=A_FSUBP then
  932. result:=A_FSUBRP
  933. else if opcode=A_FDIVRP then
  934. result:=A_FDIVP
  935. else if opcode=A_FDIVP then
  936. result:=A_FDIVRP;
  937. end;
  938. end;
  939. {*****************************************************************************
  940. Assembler
  941. *****************************************************************************}
  942. type
  943. ea = packed record
  944. sib_present : boolean;
  945. bytes : byte;
  946. size : byte;
  947. modrm : byte;
  948. sib : byte;
  949. {$ifdef x86_64}
  950. rex : byte;
  951. {$endif x86_64}
  952. end;
  953. procedure taicpu.create_ot(objdata:TObjData);
  954. {
  955. this function will also fix some other fields which only needs to be once
  956. }
  957. var
  958. i,l,relsize : longint;
  959. currsym : TObjSymbol;
  960. begin
  961. if ops=0 then
  962. exit;
  963. { update oper[].ot field }
  964. for i:=0 to ops-1 do
  965. with oper[i]^ do
  966. begin
  967. case typ of
  968. top_reg :
  969. begin
  970. ot:=reg_ot_table[findreg_by_number(reg)];
  971. end;
  972. top_ref :
  973. begin
  974. if (ref^.refaddr=addr_no)
  975. {$ifdef i386}
  976. or (
  977. (ref^.refaddr in [addr_pic]) and
  978. { allow any base for assembler blocks }
  979. ((assigned(current_procinfo) and
  980. (pi_has_assembler_block in current_procinfo.flags) and
  981. (ref^.base<>NR_NO)) or (ref^.base=NR_EBX))
  982. )
  983. {$endif i386}
  984. {$ifdef x86_64}
  985. or (
  986. (ref^.refaddr in [addr_pic,addr_pic_no_got]) and
  987. (ref^.base<>NR_NO)
  988. )
  989. {$endif x86_64}
  990. then
  991. begin
  992. { create ot field }
  993. if (ot and OT_SIZE_MASK)=0 then
  994. ot:=OT_MEMORY_ANY or opsize_2_type[i,opsize]
  995. else
  996. ot:=OT_MEMORY_ANY or (ot and OT_SIZE_MASK);
  997. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  998. ot:=ot or OT_MEM_OFFS;
  999. { fix scalefactor }
  1000. if (ref^.index=NR_NO) then
  1001. ref^.scalefactor:=0
  1002. else
  1003. if (ref^.scalefactor=0) then
  1004. ref^.scalefactor:=1;
  1005. end
  1006. else
  1007. begin
  1008. { Jumps use a relative offset which can be 8bit,
  1009. for other opcodes we always need to generate the full
  1010. 32bit address }
  1011. if assigned(objdata) and
  1012. is_jmp then
  1013. begin
  1014. currsym:=objdata.symbolref(ref^.symbol);
  1015. l:=ref^.offset;
  1016. {$push}
  1017. {$r-}
  1018. if assigned(currsym) then
  1019. inc(l,currsym.address);
  1020. {$pop}
  1021. { when it is a forward jump we need to compensate the
  1022. offset of the instruction since the previous time,
  1023. because the symbol address is then still using the
  1024. 'old-style' addressing.
  1025. For backwards jumps this is not required because the
  1026. address of the symbol is already adjusted to the
  1027. new offset }
  1028. if (l>InsOffset) and (LastInsOffset<>-1) then
  1029. inc(l,InsOffset-LastInsOffset);
  1030. { instruction size will then always become 2 (PFV) }
  1031. relsize:=(InsOffset+2)-l;
  1032. if (relsize>=-128) and (relsize<=127) and
  1033. (
  1034. not assigned(currsym) or
  1035. (currsym.objsection=objdata.currobjsec)
  1036. ) then
  1037. ot:=OT_IMM8 or OT_SHORT
  1038. else
  1039. ot:=OT_IMM32 or OT_NEAR;
  1040. end
  1041. else
  1042. ot:=OT_IMM32 or OT_NEAR;
  1043. end;
  1044. end;
  1045. top_local :
  1046. begin
  1047. if (ot and OT_SIZE_MASK)=0 then
  1048. ot:=OT_MEMORY or opsize_2_type[i,opsize]
  1049. else
  1050. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  1051. end;
  1052. top_const :
  1053. begin
  1054. // if opcode is a SSE or AVX-instruction then we need a
  1055. // special handling (opsize can different from const-size)
  1056. // (e.g. "pextrw reg/m16, xmmreg, imm8" =>> opsize (16 bit), const-size (8 bit)
  1057. if (InsTabMemRefSizeInfoCache^[opcode].ExistsSSEAVX) and
  1058. (not(InsTabMemRefSizeInfoCache^[opcode].ConstSize in [csiMultiple, csiUnkown])) then
  1059. begin
  1060. case InsTabMemRefSizeInfoCache^[opcode].ConstSize of
  1061. csiNoSize: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE;
  1062. csiMem8: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE or OT_BITS8;
  1063. csiMem16: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE or OT_BITS16;
  1064. csiMem32: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE or OT_BITS32;
  1065. csiMem64: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE or OT_BITS64;
  1066. end;
  1067. end
  1068. else
  1069. begin
  1070. { allow 2nd, 3rd or 4th operand being a constant and expect no size for shuf* etc. }
  1071. { further, allow AAD and AAM with imm. operand }
  1072. if (opsize=S_NO) and not((i in [1,2,3]) or ((i=0) and (opcode in [A_AAD,A_AAM]))) then
  1073. message(asmr_e_invalid_opcode_and_operand);
  1074. if (opsize<>S_W) and (aint(val)>=-128) and (val<=127) then
  1075. ot:=OT_IMM8 or OT_SIGNED
  1076. else
  1077. ot:=OT_IMMEDIATE or opsize_2_type[i,opsize];
  1078. if (val=1) and (i=1) then
  1079. ot := ot or OT_ONENESS;
  1080. end;
  1081. end;
  1082. top_none :
  1083. begin
  1084. { generated when there was an error in the
  1085. assembler reader. It never happends when generating
  1086. assembler }
  1087. end;
  1088. else
  1089. internalerror(200402261);
  1090. end;
  1091. end;
  1092. end;
  1093. function taicpu.InsEnd:longint;
  1094. begin
  1095. InsEnd:=InsOffset+InsSize;
  1096. end;
  1097. function taicpu.Matches(p:PInsEntry):boolean;
  1098. { * IF_SM stands for Size Match: any operand whose size is not
  1099. * explicitly specified by the template is `really' intended to be
  1100. * the same size as the first size-specified operand.
  1101. * Non-specification is tolerated in the input instruction, but
  1102. * _wrong_ specification is not.
  1103. *
  1104. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  1105. * three-operand instructions such as SHLD: it implies that the
  1106. * first two operands must match in size, but that the third is
  1107. * required to be _unspecified_.
  1108. *
  1109. * IF_SB invokes Size Byte: operands with unspecified size in the
  1110. * template are really bytes, and so no non-byte specification in
  1111. * the input instruction will be tolerated. IF_SW similarly invokes
  1112. * Size Word, and IF_SD invokes Size Doubleword.
  1113. *
  1114. * (The default state if neither IF_SM nor IF_SM2 is specified is
  1115. * that any operand with unspecified size in the template is
  1116. * required to have unspecified size in the instruction too...)
  1117. }
  1118. var
  1119. insot,
  1120. currot,
  1121. i,j,asize,oprs : longint;
  1122. insflags:cardinal;
  1123. siz : array[0..max_operands-1] of longint;
  1124. begin
  1125. result:=false;
  1126. { Check the opcode and operands }
  1127. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  1128. exit;
  1129. for i:=0 to p^.ops-1 do
  1130. begin
  1131. insot:=p^.optypes[i];
  1132. currot:=oper[i]^.ot;
  1133. { Check the operand flags }
  1134. if (insot and (not currot) and OT_NON_SIZE)<>0 then
  1135. exit;
  1136. { Check if the passed operand size matches with one of
  1137. the supported operand sizes }
  1138. if ((insot and OT_SIZE_MASK)<>0) and
  1139. ((insot and currot and OT_SIZE_MASK)<>(currot and OT_SIZE_MASK)) then
  1140. exit;
  1141. end;
  1142. { Check operand sizes }
  1143. insflags:=p^.flags;
  1144. if insflags and IF_SMASK<>0 then
  1145. begin
  1146. { as default an untyped size can get all the sizes, this is different
  1147. from nasm, but else we need to do a lot checking which opcodes want
  1148. size or not with the automatic size generation }
  1149. asize:=-1;
  1150. if (insflags and IF_SB)<>0 then
  1151. asize:=OT_BITS8
  1152. else if (insflags and IF_SW)<>0 then
  1153. asize:=OT_BITS16
  1154. else if (insflags and IF_SD)<>0 then
  1155. asize:=OT_BITS32;
  1156. if (insflags and IF_ARMASK)<>0 then
  1157. begin
  1158. siz[0]:=-1;
  1159. siz[1]:=-1;
  1160. siz[2]:=-1;
  1161. siz[((insflags and IF_ARMASK) shr IF_ARSHIFT)-1]:=asize;
  1162. end
  1163. else
  1164. begin
  1165. siz[0]:=asize;
  1166. siz[1]:=asize;
  1167. siz[2]:=asize;
  1168. end;
  1169. if (insflags and (IF_SM or IF_SM2))<>0 then
  1170. begin
  1171. if (insflags and IF_SM2)<>0 then
  1172. oprs:=2
  1173. else
  1174. oprs:=p^.ops;
  1175. for i:=0 to oprs-1 do
  1176. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  1177. begin
  1178. for j:=0 to oprs-1 do
  1179. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  1180. break;
  1181. end;
  1182. end
  1183. else
  1184. oprs:=2;
  1185. { Check operand sizes }
  1186. for i:=0 to p^.ops-1 do
  1187. begin
  1188. insot:=p^.optypes[i];
  1189. currot:=oper[i]^.ot;
  1190. if ((insot and OT_SIZE_MASK)=0) and
  1191. ((currot and OT_SIZE_MASK and (not siz[i]))<>0) and
  1192. { Immediates can always include smaller size }
  1193. ((currot and OT_IMMEDIATE)=0) and
  1194. (((insot and OT_SIZE_MASK) or siz[i])<(currot and OT_SIZE_MASK)) then
  1195. exit;
  1196. end;
  1197. end;
  1198. if (InsTabMemRefSizeInfoCache^[opcode].MemRefSize in MemRefMultiples) and
  1199. (InsTabMemRefSizeInfoCache^[opcode].ExistsSSEAVX) then
  1200. begin
  1201. for i:=0 to p^.ops-1 do
  1202. begin
  1203. insot:=p^.optypes[i];
  1204. if ((insot and OT_XMMRM) = OT_XMMRM) OR
  1205. ((insot and OT_YMMRM) = OT_YMMRM) then
  1206. begin
  1207. if (insot and OT_SIZE_MASK) = 0 then
  1208. begin
  1209. case insot and (OT_XMMRM or OT_YMMRM) of
  1210. OT_XMMRM: insot := insot or OT_BITS128;
  1211. OT_YMMRM: insot := insot or OT_BITS256;
  1212. end;
  1213. end;
  1214. end;
  1215. currot:=oper[i]^.ot;
  1216. { Check the operand flags }
  1217. if (insot and (not currot) and OT_NON_SIZE)<>0 then
  1218. exit;
  1219. { Check if the passed operand size matches with one of
  1220. the supported operand sizes }
  1221. if ((insot and OT_SIZE_MASK)<>0) and
  1222. ((insot and currot and OT_SIZE_MASK)<>(currot and OT_SIZE_MASK)) then
  1223. exit;
  1224. end;
  1225. end;
  1226. result:=true;
  1227. end;
  1228. procedure taicpu.ResetPass1;
  1229. begin
  1230. { we need to reset everything here, because the choosen insentry
  1231. can be invalid for a new situation where the previously optimized
  1232. insentry is not correct }
  1233. InsEntry:=nil;
  1234. InsSize:=0;
  1235. LastInsOffset:=-1;
  1236. end;
  1237. procedure taicpu.ResetPass2;
  1238. begin
  1239. { we are here in a second pass, check if the instruction can be optimized }
  1240. if assigned(InsEntry) and
  1241. ((InsEntry^.flags and IF_PASS2)<>0) then
  1242. begin
  1243. InsEntry:=nil;
  1244. InsSize:=0;
  1245. end;
  1246. LastInsOffset:=-1;
  1247. end;
  1248. function taicpu.CheckIfValid:boolean;
  1249. begin
  1250. result:=FindInsEntry(nil);
  1251. end;
  1252. function taicpu.FindInsentry(objdata:TObjData):boolean;
  1253. var
  1254. i : longint;
  1255. begin
  1256. result:=false;
  1257. { Things which may only be done once, not when a second pass is done to
  1258. optimize }
  1259. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  1260. begin
  1261. current_filepos:=fileinfo;
  1262. { We need intel style operands }
  1263. SetOperandOrder(op_intel);
  1264. { create the .ot fields }
  1265. create_ot(objdata);
  1266. { set the file postion }
  1267. end
  1268. else
  1269. begin
  1270. { we've already an insentry so it's valid }
  1271. result:=true;
  1272. exit;
  1273. end;
  1274. { Lookup opcode in the table }
  1275. InsSize:=-1;
  1276. i:=instabcache^[opcode];
  1277. if i=-1 then
  1278. begin
  1279. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  1280. exit;
  1281. end;
  1282. insentry:=@instab[i];
  1283. while (insentry^.opcode=opcode) do
  1284. begin
  1285. if matches(insentry) then
  1286. begin
  1287. result:=true;
  1288. exit;
  1289. end;
  1290. inc(insentry);
  1291. end;
  1292. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1293. { No instruction found, set insentry to nil and inssize to -1 }
  1294. insentry:=nil;
  1295. inssize:=-1;
  1296. end;
  1297. function taicpu.Pass1(objdata:TObjData):longint;
  1298. begin
  1299. Pass1:=0;
  1300. { Save the old offset and set the new offset }
  1301. InsOffset:=ObjData.CurrObjSec.Size;
  1302. { Error? }
  1303. if (Insentry=nil) and (InsSize=-1) then
  1304. exit;
  1305. { set the file postion }
  1306. current_filepos:=fileinfo;
  1307. { Get InsEntry }
  1308. if FindInsEntry(ObjData) then
  1309. begin
  1310. { Calculate instruction size }
  1311. InsSize:=calcsize(insentry);
  1312. if segprefix<>NR_NO then
  1313. inc(InsSize);
  1314. { Fix opsize if size if forced }
  1315. if (insentry^.flags and (IF_SB or IF_SW or IF_SD))<>0 then
  1316. begin
  1317. if (insentry^.flags and IF_ARMASK)=0 then
  1318. begin
  1319. if (insentry^.flags and IF_SB)<>0 then
  1320. begin
  1321. if opsize=S_NO then
  1322. opsize:=S_B;
  1323. end
  1324. else if (insentry^.flags and IF_SW)<>0 then
  1325. begin
  1326. if opsize=S_NO then
  1327. opsize:=S_W;
  1328. end
  1329. else if (insentry^.flags and IF_SD)<>0 then
  1330. begin
  1331. if opsize=S_NO then
  1332. opsize:=S_L;
  1333. end;
  1334. end;
  1335. end;
  1336. LastInsOffset:=InsOffset;
  1337. Pass1:=InsSize;
  1338. exit;
  1339. end;
  1340. LastInsOffset:=-1;
  1341. end;
  1342. const
  1343. segprefixes: array[NR_CS..NR_GS] of Byte=(
  1344. //cs ds es ss fs gs
  1345. $2E, $3E, $26, $36, $64, $65
  1346. );
  1347. procedure taicpu.Pass2(objdata:TObjData);
  1348. begin
  1349. { error in pass1 ? }
  1350. if insentry=nil then
  1351. exit;
  1352. current_filepos:=fileinfo;
  1353. { Segment override }
  1354. if (segprefix>=NR_CS) and (segprefix<=NR_GS) then
  1355. begin
  1356. objdata.writebytes(segprefixes[segprefix],1);
  1357. { fix the offset for GenNode }
  1358. inc(InsOffset);
  1359. end
  1360. else if segprefix<>NR_NO then
  1361. InternalError(201001071);
  1362. { Generate the instruction }
  1363. GenCode(objdata);
  1364. end;
  1365. function taicpu.needaddrprefix(opidx:byte):boolean;
  1366. begin
  1367. result:=(oper[opidx]^.typ=top_ref) and
  1368. (oper[opidx]^.ref^.refaddr=addr_no) and
  1369. {$ifdef x86_64}
  1370. (oper[opidx]^.ref^.base<>NR_RIP) and
  1371. {$endif x86_64}
  1372. (
  1373. (
  1374. (oper[opidx]^.ref^.index<>NR_NO) and
  1375. (getsubreg(oper[opidx]^.ref^.index)<>R_SUBADDR)
  1376. ) or
  1377. (
  1378. (oper[opidx]^.ref^.base<>NR_NO) and
  1379. (getsubreg(oper[opidx]^.ref^.base)<>R_SUBADDR)
  1380. )
  1381. );
  1382. end;
  1383. function regval(r:Tregister):byte;
  1384. const
  1385. {$if defined(x86_64)}
  1386. opcode_table:array[tregisterindex] of tregisterindex = (
  1387. {$i r8664op.inc}
  1388. );
  1389. {$elseif defined(i386)}
  1390. opcode_table:array[tregisterindex] of tregisterindex = (
  1391. {$i r386op.inc}
  1392. );
  1393. {$elseif defined(i8086)}
  1394. opcode_table:array[tregisterindex] of tregisterindex = (
  1395. {$i r8086op.inc}
  1396. );
  1397. {$endif}
  1398. var
  1399. regidx : tregisterindex;
  1400. begin
  1401. regidx:=findreg_by_number(r);
  1402. if regidx<>0 then
  1403. result:=opcode_table[regidx]
  1404. else
  1405. begin
  1406. Message1(asmw_e_invalid_register,generic_regname(r));
  1407. result:=0;
  1408. end;
  1409. end;
  1410. {$ifdef x86_64}
  1411. function rexbits(r: tregister): byte;
  1412. begin
  1413. result:=0;
  1414. case getregtype(r) of
  1415. R_INTREGISTER:
  1416. if (getsupreg(r)>=RS_R8) then
  1417. { Either B,X or R bits can be set, depending on register role in instruction.
  1418. Set all three bits here, caller will discard unnecessary ones. }
  1419. result:=result or $47
  1420. else if (getsubreg(r)=R_SUBL) and
  1421. (getsupreg(r) in [RS_RDI,RS_RSI,RS_RBP,RS_RSP]) then
  1422. result:=result or $40
  1423. else if (getsubreg(r)=R_SUBH) then
  1424. { Not an actual REX bit, used to detect incompatible usage of
  1425. AH/BH/CH/DH }
  1426. result:=result or $80;
  1427. R_MMREGISTER:
  1428. if getsupreg(r)>=RS_XMM8 then
  1429. result:=result or $47;
  1430. end;
  1431. end;
  1432. function process_ea(const input:toper;out output:ea;rfield:longint):boolean;
  1433. var
  1434. sym : tasmsymbol;
  1435. md,s,rv : byte;
  1436. base,index,scalefactor,
  1437. o : longint;
  1438. ir,br : Tregister;
  1439. isub,bsub : tsubregister;
  1440. begin
  1441. process_ea:=false;
  1442. fillchar(output,sizeof(output),0);
  1443. {Register ?}
  1444. if (input.typ=top_reg) then
  1445. begin
  1446. rv:=regval(input.reg);
  1447. output.modrm:=$c0 or (rfield shl 3) or rv;
  1448. output.size:=1;
  1449. output.rex:=output.rex or (rexbits(input.reg) and $F1);
  1450. process_ea:=true;
  1451. exit;
  1452. end;
  1453. {No register, so memory reference.}
  1454. if input.typ<>top_ref then
  1455. internalerror(200409263);
  1456. ir:=input.ref^.index;
  1457. br:=input.ref^.base;
  1458. isub:=getsubreg(ir);
  1459. bsub:=getsubreg(br);
  1460. s:=input.ref^.scalefactor;
  1461. o:=input.ref^.offset;
  1462. sym:=input.ref^.symbol;
  1463. if ((ir<>NR_NO) and (getregtype(ir)<>R_INTREGISTER)) or
  1464. ((br<>NR_NO) and (br<>NR_RIP) and (getregtype(br)<>R_INTREGISTER)) then
  1465. internalerror(200301081);
  1466. { it's direct address }
  1467. if (br=NR_NO) and (ir=NR_NO) then
  1468. begin
  1469. output.sib_present:=true;
  1470. output.bytes:=4;
  1471. output.modrm:=4 or (rfield shl 3);
  1472. output.sib:=$25;
  1473. end
  1474. else if (br=NR_RIP) and (ir=NR_NO) then
  1475. begin
  1476. { rip based }
  1477. output.sib_present:=false;
  1478. output.bytes:=4;
  1479. output.modrm:=5 or (rfield shl 3);
  1480. end
  1481. else
  1482. { it's an indirection }
  1483. begin
  1484. { 16 bit or 32 bit address? }
  1485. if ((ir<>NR_NO) and (isub<>R_SUBADDR)) or
  1486. ((br<>NR_NO) and (bsub<>R_SUBADDR)) then
  1487. message(asmw_e_16bit_32bit_not_supported);
  1488. { wrong, for various reasons }
  1489. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  1490. exit;
  1491. output.rex:=output.rex or (rexbits(br) and $F1) or (rexbits(ir) and $F2);
  1492. process_ea:=true;
  1493. { base }
  1494. case br of
  1495. NR_R8,
  1496. NR_RAX : base:=0;
  1497. NR_R9,
  1498. NR_RCX : base:=1;
  1499. NR_R10,
  1500. NR_RDX : base:=2;
  1501. NR_R11,
  1502. NR_RBX : base:=3;
  1503. NR_R12,
  1504. NR_RSP : base:=4;
  1505. NR_R13,
  1506. NR_NO,
  1507. NR_RBP : base:=5;
  1508. NR_R14,
  1509. NR_RSI : base:=6;
  1510. NR_R15,
  1511. NR_RDI : base:=7;
  1512. else
  1513. exit;
  1514. end;
  1515. { index }
  1516. case ir of
  1517. NR_R8,
  1518. NR_RAX : index:=0;
  1519. NR_R9,
  1520. NR_RCX : index:=1;
  1521. NR_R10,
  1522. NR_RDX : index:=2;
  1523. NR_R11,
  1524. NR_RBX : index:=3;
  1525. NR_R12,
  1526. NR_NO : index:=4;
  1527. NR_R13,
  1528. NR_RBP : index:=5;
  1529. NR_R14,
  1530. NR_RSI : index:=6;
  1531. NR_R15,
  1532. NR_RDI : index:=7;
  1533. else
  1534. exit;
  1535. end;
  1536. case s of
  1537. 0,
  1538. 1 : scalefactor:=0;
  1539. 2 : scalefactor:=1;
  1540. 4 : scalefactor:=2;
  1541. 8 : scalefactor:=3;
  1542. else
  1543. exit;
  1544. end;
  1545. { If rbp or r13 is used we must always include an offset }
  1546. if (br=NR_NO) or
  1547. ((br<>NR_RBP) and (br<>NR_R13) and (o=0) and (sym=nil)) then
  1548. md:=0
  1549. else
  1550. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1551. md:=1
  1552. else
  1553. md:=2;
  1554. if (br=NR_NO) or (md=2) then
  1555. output.bytes:=4
  1556. else
  1557. output.bytes:=md;
  1558. { SIB needed ? }
  1559. if (ir=NR_NO) and (br<>NR_RSP) and (br<>NR_R12) then
  1560. begin
  1561. output.sib_present:=false;
  1562. output.modrm:=(md shl 6) or (rfield shl 3) or base;
  1563. end
  1564. else
  1565. begin
  1566. output.sib_present:=true;
  1567. output.modrm:=(md shl 6) or (rfield shl 3) or 4;
  1568. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1569. end;
  1570. end;
  1571. output.size:=1+ord(output.sib_present)+output.bytes;
  1572. process_ea:=true;
  1573. end;
  1574. {$else x86_64}
  1575. function process_ea(const input:toper;out output:ea;rfield:longint):boolean;
  1576. var
  1577. sym : tasmsymbol;
  1578. md,s,rv : byte;
  1579. base,index,scalefactor,
  1580. o : longint;
  1581. ir,br : Tregister;
  1582. isub,bsub : tsubregister;
  1583. begin
  1584. process_ea:=false;
  1585. fillchar(output,sizeof(output),0);
  1586. {Register ?}
  1587. if (input.typ=top_reg) then
  1588. begin
  1589. rv:=regval(input.reg);
  1590. output.modrm:=$c0 or (rfield shl 3) or rv;
  1591. output.size:=1;
  1592. process_ea:=true;
  1593. exit;
  1594. end;
  1595. {No register, so memory reference.}
  1596. if (input.typ<>top_ref) then
  1597. internalerror(200409262);
  1598. if ((input.ref^.index<>NR_NO) and (getregtype(input.ref^.index)<>R_INTREGISTER)) or
  1599. ((input.ref^.base<>NR_NO) and (getregtype(input.ref^.base)<>R_INTREGISTER)) then
  1600. internalerror(200301081);
  1601. ir:=input.ref^.index;
  1602. br:=input.ref^.base;
  1603. isub:=getsubreg(ir);
  1604. bsub:=getsubreg(br);
  1605. s:=input.ref^.scalefactor;
  1606. o:=input.ref^.offset;
  1607. sym:=input.ref^.symbol;
  1608. { it's direct address }
  1609. if (br=NR_NO) and (ir=NR_NO) then
  1610. begin
  1611. { it's a pure offset }
  1612. output.sib_present:=false;
  1613. output.bytes:=4;
  1614. output.modrm:=5 or (rfield shl 3);
  1615. end
  1616. else
  1617. { it's an indirection }
  1618. begin
  1619. { 16 bit address? }
  1620. if ((ir<>NR_NO) and (isub<>R_SUBADDR)) or
  1621. ((br<>NR_NO) and (bsub<>R_SUBADDR)) then
  1622. message(asmw_e_16bit_not_supported);
  1623. {$ifdef OPTEA}
  1624. { make single reg base }
  1625. if (br=NR_NO) and (s=1) then
  1626. begin
  1627. br:=ir;
  1628. ir:=NR_NO;
  1629. end;
  1630. { convert [3,5,9]*EAX to EAX+[2,4,8]*EAX }
  1631. if (br=NR_NO) and
  1632. (((s=2) and (ir<>NR_ESP)) or
  1633. (s=3) or (s=5) or (s=9)) then
  1634. begin
  1635. br:=ir;
  1636. dec(s);
  1637. end;
  1638. { swap ESP into base if scalefactor is 1 }
  1639. if (s=1) and (ir=NR_ESP) then
  1640. begin
  1641. ir:=br;
  1642. br:=NR_ESP;
  1643. end;
  1644. {$endif OPTEA}
  1645. { wrong, for various reasons }
  1646. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  1647. exit;
  1648. { base }
  1649. case br of
  1650. NR_EAX : base:=0;
  1651. NR_ECX : base:=1;
  1652. NR_EDX : base:=2;
  1653. NR_EBX : base:=3;
  1654. NR_ESP : base:=4;
  1655. NR_NO,
  1656. NR_EBP : base:=5;
  1657. NR_ESI : base:=6;
  1658. NR_EDI : base:=7;
  1659. else
  1660. exit;
  1661. end;
  1662. { index }
  1663. case ir of
  1664. NR_EAX : index:=0;
  1665. NR_ECX : index:=1;
  1666. NR_EDX : index:=2;
  1667. NR_EBX : index:=3;
  1668. NR_NO : index:=4;
  1669. NR_EBP : index:=5;
  1670. NR_ESI : index:=6;
  1671. NR_EDI : index:=7;
  1672. else
  1673. exit;
  1674. end;
  1675. case s of
  1676. 0,
  1677. 1 : scalefactor:=0;
  1678. 2 : scalefactor:=1;
  1679. 4 : scalefactor:=2;
  1680. 8 : scalefactor:=3;
  1681. else
  1682. exit;
  1683. end;
  1684. if (br=NR_NO) or
  1685. ((br<>NR_EBP) and (o=0) and (sym=nil)) then
  1686. md:=0
  1687. else
  1688. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1689. md:=1
  1690. else
  1691. md:=2;
  1692. if (br=NR_NO) or (md=2) then
  1693. output.bytes:=4
  1694. else
  1695. output.bytes:=md;
  1696. { SIB needed ? }
  1697. if (ir=NR_NO) and (br<>NR_ESP) then
  1698. begin
  1699. output.sib_present:=false;
  1700. output.modrm:=(longint(md) shl 6) or (rfield shl 3) or base;
  1701. end
  1702. else
  1703. begin
  1704. output.sib_present:=true;
  1705. output.modrm:=(longint(md) shl 6) or (rfield shl 3) or 4;
  1706. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1707. end;
  1708. end;
  1709. if output.sib_present then
  1710. output.size:=2+output.bytes
  1711. else
  1712. output.size:=1+output.bytes;
  1713. process_ea:=true;
  1714. end;
  1715. {$endif x86_64}
  1716. function taicpu.calcsize(p:PInsEntry):shortint;
  1717. var
  1718. codes : pchar;
  1719. c : byte;
  1720. len : shortint;
  1721. ea_data : ea;
  1722. exists_vex: boolean;
  1723. exists_vex_extention: boolean;
  1724. exists_prefix_66: boolean;
  1725. exists_prefix_F2: boolean;
  1726. exists_prefix_F3: boolean;
  1727. {$ifdef x86_64}
  1728. omit_rexw : boolean;
  1729. {$endif x86_64}
  1730. begin
  1731. len:=0;
  1732. codes:=@p^.code[0];
  1733. exists_vex := false;
  1734. exists_vex_extention := false;
  1735. exists_prefix_66 := false;
  1736. exists_prefix_F2 := false;
  1737. exists_prefix_F3 := false;
  1738. {$ifdef x86_64}
  1739. rex:=0;
  1740. omit_rexw:=false;
  1741. {$endif x86_64}
  1742. repeat
  1743. c:=ord(codes^);
  1744. inc(codes);
  1745. case c of
  1746. 0 :
  1747. break;
  1748. 1,2,3 :
  1749. begin
  1750. inc(codes,c);
  1751. inc(len,c);
  1752. end;
  1753. 8,9,10 :
  1754. begin
  1755. {$ifdef x86_64}
  1756. rex:=rex or (rexbits(oper[c-8]^.reg) and $F1);
  1757. {$endif x86_64}
  1758. inc(codes);
  1759. inc(len);
  1760. end;
  1761. 11 :
  1762. begin
  1763. inc(codes);
  1764. inc(len);
  1765. end;
  1766. 4,5,6,7 :
  1767. begin
  1768. if opsize=S_W then
  1769. inc(len,2)
  1770. else
  1771. inc(len);
  1772. end;
  1773. 12,13,14,
  1774. 16,17,18,
  1775. 20,21,22,23,
  1776. 40,41,42 :
  1777. inc(len);
  1778. 24,25,26,
  1779. 31,
  1780. 48,49,50 :
  1781. inc(len,2);
  1782. 28,29,30:
  1783. begin
  1784. if opsize=S_Q then
  1785. inc(len,8)
  1786. else
  1787. inc(len,4);
  1788. end;
  1789. 36,37,38:
  1790. inc(len,sizeof(pint));
  1791. 44,45,46:
  1792. inc(len,8);
  1793. 32,33,34,
  1794. 52,53,54,
  1795. 56,57,58,
  1796. 172,173,174 :
  1797. inc(len,4);
  1798. 60,61,62,63: ; // ignore vex-coded operand-idx
  1799. 208,209,210 :
  1800. begin
  1801. case (oper[c-208]^.ot and OT_SIZE_MASK) of
  1802. OT_BITS16:
  1803. inc(len);
  1804. {$ifdef x86_64}
  1805. OT_BITS64:
  1806. begin
  1807. rex:=rex or $48;
  1808. end;
  1809. {$endif x86_64}
  1810. end;
  1811. end;
  1812. 200 :
  1813. {$ifndef x86_64}
  1814. inc(len);
  1815. {$else x86_64}
  1816. { every insentry with code 0310 must be marked with NOX86_64 }
  1817. InternalError(2011051301);
  1818. {$endif x86_64}
  1819. 201 :
  1820. {$ifdef x86_64}
  1821. inc(len)
  1822. {$endif x86_64}
  1823. ;
  1824. 212 :
  1825. inc(len);
  1826. 214 :
  1827. begin
  1828. {$ifdef x86_64}
  1829. rex:=rex or $48;
  1830. {$endif x86_64}
  1831. end;
  1832. 202,
  1833. 211,
  1834. 213,
  1835. 215,
  1836. 217,218: ;
  1837. 219:
  1838. begin
  1839. inc(len);
  1840. exists_prefix_F2 := true;
  1841. end;
  1842. 220:
  1843. begin
  1844. inc(len);
  1845. exists_prefix_F3 := true;
  1846. end;
  1847. 241:
  1848. begin
  1849. inc(len);
  1850. exists_prefix_66 := true;
  1851. end;
  1852. 221:
  1853. {$ifdef x86_64}
  1854. omit_rexw:=true
  1855. {$endif x86_64}
  1856. ;
  1857. 64..151 :
  1858. begin
  1859. {$ifdef x86_64}
  1860. if (c<127) then
  1861. begin
  1862. if (oper[c and 7]^.typ=top_reg) then
  1863. begin
  1864. rex:=rex or (rexbits(oper[c and 7]^.reg) and $F4);
  1865. end;
  1866. end;
  1867. {$endif x86_64}
  1868. if not process_ea(oper[(c shr 3) and 7]^, ea_data, 0) then
  1869. Message(asmw_e_invalid_effective_address)
  1870. else
  1871. inc(len,ea_data.size);
  1872. {$ifdef x86_64}
  1873. rex:=rex or ea_data.rex;
  1874. {$endif x86_64}
  1875. end;
  1876. 242: // VEX prefix for AVX (length = 2 or 3 bytes, dependens on REX.XBW or opcode-prefix ($0F38 or $0F3A))
  1877. // =>> DEFAULT = 2 Bytes
  1878. begin
  1879. if not(exists_vex) then
  1880. begin
  1881. inc(len, 2);
  1882. exists_vex := true;
  1883. end;
  1884. end;
  1885. 243: // REX.W = 1
  1886. // =>> VEX prefix length = 3
  1887. begin
  1888. if not(exists_vex_extention) then
  1889. begin
  1890. inc(len);
  1891. exists_vex_extention := true;
  1892. end;
  1893. end;
  1894. 244: ; // VEX length bit
  1895. 247: inc(len); // operand 3 (ymmreg) encoded immediate byte (bit 4-7)
  1896. 248: // VEX-Extention prefix $0F
  1897. // ignore for calculating length
  1898. ;
  1899. 249, // VEX-Extention prefix $0F38
  1900. 250: // VEX-Extention prefix $0F3A
  1901. begin
  1902. if not(exists_vex_extention) then
  1903. begin
  1904. inc(len);
  1905. exists_vex_extention := true;
  1906. end;
  1907. end;
  1908. else
  1909. InternalError(200603141);
  1910. end;
  1911. until false;
  1912. {$ifdef x86_64}
  1913. if ((rex and $80)<>0) and ((rex and $4F)<>0) then
  1914. Message(asmw_e_bad_reg_with_rex);
  1915. rex:=rex and $4F; { reset extra bits in upper nibble }
  1916. if omit_rexw then
  1917. begin
  1918. if rex=$48 then { remove rex entirely? }
  1919. rex:=0
  1920. else
  1921. rex:=rex and $F7;
  1922. end;
  1923. if not(exists_vex) then
  1924. begin
  1925. if rex<>0 then
  1926. Inc(len);
  1927. end;
  1928. {$endif}
  1929. if exists_vex then
  1930. begin
  1931. if exists_prefix_66 then dec(len);
  1932. if exists_prefix_F2 then dec(len);
  1933. if exists_prefix_F3 then dec(len);
  1934. {$ifdef x86_64}
  1935. if not(exists_vex_extention) then
  1936. if rex and $0B <> 0 then inc(len); // REX.WXB <> 0 =>> needed VEX-Extention
  1937. {$endif x86_64}
  1938. end;
  1939. calcsize:=len;
  1940. end;
  1941. procedure taicpu.GenCode(objdata:TObjData);
  1942. {
  1943. * the actual codes (C syntax, i.e. octal):
  1944. * \0 - terminates the code. (Unless it's a literal of course.)
  1945. * \1, \2, \3 - that many literal bytes follow in the code stream
  1946. * \4, \6 - the POP/PUSH (respectively) codes for CS, DS, ES, SS
  1947. * (POP is never used for CS) depending on operand 0
  1948. * \5, \7 - the second byte of POP/PUSH codes for FS, GS, depending
  1949. * on operand 0
  1950. * \10, \11, \12 - a literal byte follows in the code stream, to be added
  1951. * to the register value of operand 0, 1 or 2
  1952. * \13 - a literal byte follows in the code stream, to be added
  1953. * to the condition code value of the instruction.
  1954. * \14, \15, \16 - a signed byte immediate operand, from operand 0, 1 or 2
  1955. * \20, \21, \22 - a byte immediate operand, from operand 0, 1 or 2
  1956. * \24, \25, \26, \27 - an unsigned byte immediate operand, from operand 0, 1, 2 or 3
  1957. * \30, \31, \32 - a word immediate operand, from operand 0, 1 or 2
  1958. * \34, \35, \36 - select between \3[012] and \4[012] depending on 16/32 bit
  1959. * assembly mode or the address-size override on the operand
  1960. * \37 - a word constant, from the _segment_ part of operand 0
  1961. * \40, \41, \42 - a long immediate operand, from operand 0, 1 or 2
  1962. * \44, \45, \46 - select between \3[012], \4[012] or \5[456] depending
  1963. on the address size of instruction
  1964. * \50, \51, \52 - a byte relative operand, from operand 0, 1 or 2
  1965. * \54, \55, \56 - a qword immediate, from operand 0, 1 or 2
  1966. * \60, \61, \62 - a word relative operand, from operand 0, 1 or 2
  1967. * \64, \65, \66 - select between \6[012] and \7[012] depending on 16/32 bit
  1968. * assembly mode or the address-size override on the operand
  1969. * \70, \71, \72 - a long relative operand, from operand 0, 1 or 2
  1970. * \74, \75, \76 - a vex-coded vector operand, from operand 0, 1 or 2
  1971. * \1ab - a ModRM, calculated on EA in operand a, with the spare
  1972. * field the register value of operand b.
  1973. * \2ab - a ModRM, calculated on EA in operand a, with the spare
  1974. * field equal to digit b.
  1975. * \254,\255,\256 - a signed 32-bit immediate to be extended to 64 bits
  1976. * \300,\301,\302 - might be an 0x67, depending on the address size of
  1977. * the memory reference in operand x.
  1978. * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
  1979. * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
  1980. * \312 - (disassembler only) invalid with non-default address size.
  1981. * \320,\321,\322 - might be an 0x66 or 0x48 byte, depending on the operand
  1982. * size of operand x.
  1983. * \324 - indicates fixed 16-bit operand size, i.e. optional 0x66.
  1984. * \325 - indicates fixed 32-bit operand size, i.e. optional 0x66.
  1985. * \326 - indicates fixed 64-bit operand size, i.e. optional 0x48.
  1986. * \327 - indicates that this instruction is only valid when the
  1987. * operand size is the default (instruction to disassembler,
  1988. * generates no code in the assembler)
  1989. * \331 - instruction not valid with REP prefix. Hint for
  1990. * disassembler only; for SSE instructions.
  1991. * \332 - disassemble a rep (0xF3 byte) prefix as repe not rep.
  1992. * \333 - 0xF3 prefix for SSE instructions
  1993. * \334 - 0xF2 prefix for SSE instructions
  1994. * \335 - Indicates 64-bit operand size with REX.W not necessary
  1995. * \361 - 0x66 prefix for SSE instructions
  1996. * \362 - VEX prefix for AVX instructions
  1997. * \363 - VEX W1
  1998. * \364 - VEX Vector length 256
  1999. * \367 - operand 3 (ymmreg) encoded in bit 4-7 of the immediate byte
  2000. * \370 - VEX 0F-FLAG
  2001. * \371 - VEX 0F38-FLAG
  2002. * \372 - VEX 0F3A-FLAG
  2003. }
  2004. var
  2005. currval : aint;
  2006. currsym : tobjsymbol;
  2007. currrelreloc,
  2008. currabsreloc,
  2009. currabsreloc32 : TObjRelocationType;
  2010. {$ifdef x86_64}
  2011. rexwritten : boolean;
  2012. {$endif x86_64}
  2013. procedure getvalsym(opidx:longint);
  2014. begin
  2015. case oper[opidx]^.typ of
  2016. top_ref :
  2017. begin
  2018. currval:=oper[opidx]^.ref^.offset;
  2019. currsym:=ObjData.symbolref(oper[opidx]^.ref^.symbol);
  2020. {$ifdef i386}
  2021. if (oper[opidx]^.ref^.refaddr=addr_pic) and
  2022. (tf_pic_uses_got in target_info.flags) then
  2023. begin
  2024. currrelreloc:=RELOC_PLT32;
  2025. currabsreloc:=RELOC_GOT32;
  2026. currabsreloc32:=RELOC_GOT32;
  2027. end
  2028. else
  2029. {$endif i386}
  2030. {$ifdef x86_64}
  2031. if oper[opidx]^.ref^.refaddr=addr_pic then
  2032. begin
  2033. currrelreloc:=RELOC_PLT32;
  2034. currabsreloc:=RELOC_GOTPCREL;
  2035. currabsreloc32:=RELOC_GOTPCREL;
  2036. end
  2037. else if oper[opidx]^.ref^.refaddr=addr_pic_no_got then
  2038. begin
  2039. currrelreloc:=RELOC_RELATIVE;
  2040. currabsreloc:=RELOC_RELATIVE;
  2041. currabsreloc32:=RELOC_RELATIVE;
  2042. end
  2043. else
  2044. {$endif x86_64}
  2045. begin
  2046. currrelreloc:=RELOC_RELATIVE;
  2047. currabsreloc:=RELOC_ABSOLUTE;
  2048. currabsreloc32:=RELOC_ABSOLUTE32;
  2049. end;
  2050. end;
  2051. top_const :
  2052. begin
  2053. currval:=aint(oper[opidx]^.val);
  2054. currsym:=nil;
  2055. currabsreloc:=RELOC_ABSOLUTE;
  2056. currabsreloc32:=RELOC_ABSOLUTE32;
  2057. end;
  2058. else
  2059. Message(asmw_e_immediate_or_reference_expected);
  2060. end;
  2061. end;
  2062. {$ifdef x86_64}
  2063. procedure maybewriterex;
  2064. begin
  2065. if (rex<>0) and not(rexwritten) then
  2066. begin
  2067. rexwritten:=true;
  2068. objdata.writebytes(rex,1);
  2069. end;
  2070. end;
  2071. {$endif x86_64}
  2072. procedure objdata_writereloc(Data:aint;len:aword;p:TObjSymbol;Reloctype:TObjRelocationType);
  2073. begin
  2074. {$ifdef i386}
  2075. { Special case of '_GLOBAL_OFFSET_TABLE_'
  2076. which needs a special relocation type R_386_GOTPC }
  2077. if assigned (p) and
  2078. (p.name='_GLOBAL_OFFSET_TABLE_') and
  2079. (tf_pic_uses_got in target_info.flags) then
  2080. begin
  2081. { nothing else than a 4 byte relocation should occur
  2082. for GOT }
  2083. if len<>4 then
  2084. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  2085. Reloctype:=RELOC_GOTPC;
  2086. { We need to add the offset of the relocation
  2087. of _GLOBAL_OFFSET_TABLE symbol within
  2088. the current instruction }
  2089. inc(data,objdata.currobjsec.size-insoffset);
  2090. end;
  2091. {$endif i386}
  2092. objdata.writereloc(data,len,p,Reloctype);
  2093. end;
  2094. const
  2095. CondVal:array[TAsmCond] of byte=($0,
  2096. $7, $3, $2, $6, $2, $4, $F, $D, $C, $E, $6, $2,
  2097. $3, $7, $3, $5, $E, $C, $D, $F, $1, $B, $9, $5,
  2098. $0, $A, $A, $B, $8, $4);
  2099. var
  2100. c : byte;
  2101. pb : pbyte;
  2102. codes : pchar;
  2103. bytes : array[0..3] of byte;
  2104. rfield,
  2105. data,s,opidx : longint;
  2106. ea_data : ea;
  2107. relsym : TObjSymbol;
  2108. needed_VEX_Extention: boolean;
  2109. needed_VEX: boolean;
  2110. opmode: integer;
  2111. VEXvvvv: byte;
  2112. VEXmmmmm: byte;
  2113. begin
  2114. { safety check }
  2115. if objdata.currobjsec.size<>longword(insoffset) then
  2116. internalerror(200130121);
  2117. { load data to write }
  2118. codes:=insentry^.code;
  2119. {$ifdef x86_64}
  2120. rexwritten:=false;
  2121. {$endif x86_64}
  2122. { Force word push/pop for registers }
  2123. if (opsize=S_W) and ((codes[0]=#4) or (codes[0]=#6) or
  2124. ((codes[0]=#1) and ((codes[2]=#5) or (codes[2]=#7)))) then
  2125. begin
  2126. bytes[0]:=$66;
  2127. objdata.writebytes(bytes,1);
  2128. end;
  2129. // needed VEX Prefix (for AVX etc.)
  2130. needed_VEX := false;
  2131. needed_VEX_Extention := false;
  2132. opmode := -1;
  2133. VEXvvvv := 0;
  2134. VEXmmmmm := 0;
  2135. repeat
  2136. c:=ord(codes^);
  2137. inc(codes);
  2138. case c of
  2139. 0: break;
  2140. 1,
  2141. 2,
  2142. 3: inc(codes,c);
  2143. 60: opmode := 0;
  2144. 61: opmode := 1;
  2145. 62: opmode := 2;
  2146. 219: VEXvvvv := VEXvvvv OR $02; // set SIMD-prefix $F3
  2147. 220: VEXvvvv := VEXvvvv OR $03; // set SIMD-prefix $F2
  2148. 241: VEXvvvv := VEXvvvv OR $01; // set SIMD-prefix $66
  2149. 242: needed_VEX := true;
  2150. 243: begin
  2151. needed_VEX_Extention := true;
  2152. VEXvvvv := VEXvvvv OR (1 shl 7); // set REX.W
  2153. end;
  2154. 244: VEXvvvv := VEXvvvv OR $04; // vectorlength = 256 bits AND no scalar
  2155. 248: VEXmmmmm := VEXmmmmm OR $01; // set leading opcode byte $0F
  2156. 249: begin
  2157. needed_VEX_Extention := true;
  2158. VEXmmmmm := VEXmmmmm OR $02; // set leading opcode byte $0F38
  2159. end;
  2160. 250: begin
  2161. needed_VEX_Extention := true;
  2162. VEXmmmmm := VEXmmmmm OR $03; // set leading opcode byte $0F3A
  2163. end;
  2164. end;
  2165. until false;
  2166. if needed_VEX then
  2167. begin
  2168. if (opmode > ops) or
  2169. (opmode < -1) then
  2170. begin
  2171. Internalerror(777100);
  2172. end
  2173. else if opmode = -1 then
  2174. begin
  2175. VEXvvvv := VEXvvvv or ($0F shl 3); // set VEXvvvv bits (bits 6-3) to 1
  2176. end
  2177. else if oper[opmode]^.typ = top_reg then
  2178. begin
  2179. VEXvvvv := VEXvvvv or ((not(regval(oper[opmode]^.reg)) and $07) shl 3);
  2180. {$ifdef x86_64}
  2181. if rexbits(oper[opmode]^.reg) = 0 then VEXvvvv := VEXvvvv or (1 shl 6);
  2182. {$else}
  2183. VEXvvvv := VEXvvvv or (1 shl 6);
  2184. {$endif x86_64}
  2185. end
  2186. else Internalerror(777101);
  2187. if not(needed_VEX_Extention) then
  2188. begin
  2189. {$ifdef x86_64}
  2190. if rex and $0B <> 0 then needed_VEX_Extention := true;
  2191. {$endif x86_64}
  2192. end;
  2193. if needed_VEX_Extention then
  2194. begin
  2195. // VEX-Prefix-Length = 3 Bytes
  2196. bytes[0]:=$C4;
  2197. objdata.writebytes(bytes,1);
  2198. {$ifdef x86_64}
  2199. VEXmmmmm := VEXmmmmm or ((not(rex) and $07) shl 5); // set REX.rxb
  2200. {$else}
  2201. VEXmmmmm := VEXmmmmm or (7 shl 5); //
  2202. {$endif x86_64}
  2203. bytes[0] := VEXmmmmm;
  2204. objdata.writebytes(bytes,1);
  2205. {$ifdef x86_64}
  2206. VEXvvvv := VEXvvvv OR ((rex and $08) shl 7); // set REX.w
  2207. {$endif x86_64}
  2208. bytes[0] := VEXvvvv;
  2209. objdata.writebytes(bytes,1);
  2210. end
  2211. else
  2212. begin
  2213. // VEX-Prefix-Length = 2 Bytes
  2214. bytes[0]:=$C5;
  2215. objdata.writebytes(bytes,1);
  2216. {$ifdef x86_64}
  2217. if rex and $04 = 0 then
  2218. {$endif x86_64}
  2219. begin
  2220. VEXvvvv := VEXvvvv or (1 shl 7);
  2221. end;
  2222. bytes[0] := VEXvvvv;
  2223. objdata.writebytes(bytes,1);
  2224. end;
  2225. end
  2226. else
  2227. begin
  2228. needed_VEX_Extention := false;
  2229. opmode := -1;
  2230. end;
  2231. { load data to write }
  2232. codes:=insentry^.code;
  2233. repeat
  2234. c:=ord(codes^);
  2235. inc(codes);
  2236. case c of
  2237. 0 :
  2238. break;
  2239. 1,2,3 :
  2240. begin
  2241. {$ifdef x86_64}
  2242. if not(needed_VEX) then // TG
  2243. maybewriterex;
  2244. {$endif x86_64}
  2245. objdata.writebytes(codes^,c);
  2246. inc(codes,c);
  2247. end;
  2248. 4,6 :
  2249. begin
  2250. case oper[0]^.reg of
  2251. NR_CS:
  2252. bytes[0]:=$e;
  2253. NR_NO,
  2254. NR_DS:
  2255. bytes[0]:=$1e;
  2256. NR_ES:
  2257. bytes[0]:=$6;
  2258. NR_SS:
  2259. bytes[0]:=$16;
  2260. else
  2261. internalerror(777004);
  2262. end;
  2263. if c=4 then
  2264. inc(bytes[0]);
  2265. objdata.writebytes(bytes,1);
  2266. end;
  2267. 5,7 :
  2268. begin
  2269. case oper[0]^.reg of
  2270. NR_FS:
  2271. bytes[0]:=$a0;
  2272. NR_GS:
  2273. bytes[0]:=$a8;
  2274. else
  2275. internalerror(777005);
  2276. end;
  2277. if c=5 then
  2278. inc(bytes[0]);
  2279. objdata.writebytes(bytes,1);
  2280. end;
  2281. 8,9,10 :
  2282. begin
  2283. {$ifdef x86_64}
  2284. if not(needed_VEX) then // TG
  2285. maybewriterex;
  2286. {$endif x86_64}
  2287. bytes[0]:=ord(codes^)+regval(oper[c-8]^.reg);
  2288. inc(codes);
  2289. objdata.writebytes(bytes,1);
  2290. end;
  2291. 11 :
  2292. begin
  2293. bytes[0]:=ord(codes^)+condval[condition];
  2294. inc(codes);
  2295. objdata.writebytes(bytes,1);
  2296. end;
  2297. 12,13,14 :
  2298. begin
  2299. getvalsym(c-12);
  2300. if (currval<-128) or (currval>127) then
  2301. Message2(asmw_e_value_exceeds_bounds,'signed byte',tostr(currval));
  2302. if assigned(currsym) then
  2303. objdata_writereloc(currval,1,currsym,currabsreloc)
  2304. else
  2305. objdata.writebytes(currval,1);
  2306. end;
  2307. 16,17,18 :
  2308. begin
  2309. getvalsym(c-16);
  2310. if (currval<-256) or (currval>255) then
  2311. Message2(asmw_e_value_exceeds_bounds,'byte',tostr(currval));
  2312. if assigned(currsym) then
  2313. objdata_writereloc(currval,1,currsym,currabsreloc)
  2314. else
  2315. objdata.writebytes(currval,1);
  2316. end;
  2317. 20,21,22,23 :
  2318. begin
  2319. getvalsym(c-20);
  2320. if (currval<0) or (currval>255) then
  2321. Message2(asmw_e_value_exceeds_bounds,'unsigned byte',tostr(currval));
  2322. if assigned(currsym) then
  2323. objdata_writereloc(currval,1,currsym,currabsreloc)
  2324. else
  2325. objdata.writebytes(currval,1);
  2326. end;
  2327. 24,25,26 : // 030..032
  2328. begin
  2329. getvalsym(c-24);
  2330. {$ifndef i8086}
  2331. { currval is an aint so this cannot happen on i8086 and causes only a warning }
  2332. if (currval<-65536) or (currval>65535) then
  2333. Message2(asmw_e_value_exceeds_bounds,'word',tostr(currval));
  2334. {$endif i8086}
  2335. if assigned(currsym) then
  2336. objdata_writereloc(currval,2,currsym,currabsreloc)
  2337. else
  2338. objdata.writebytes(currval,2);
  2339. end;
  2340. 28,29,30 : // 034..036
  2341. { !!! These are intended (and used in opcode table) to select depending
  2342. on address size, *not* operand size. Works by coincidence only. }
  2343. begin
  2344. getvalsym(c-28);
  2345. if opsize=S_Q then
  2346. begin
  2347. if assigned(currsym) then
  2348. objdata_writereloc(currval,8,currsym,currabsreloc)
  2349. else
  2350. objdata.writebytes(currval,8);
  2351. end
  2352. else
  2353. begin
  2354. if assigned(currsym) then
  2355. objdata_writereloc(currval,4,currsym,currabsreloc32)
  2356. else
  2357. objdata.writebytes(currval,4);
  2358. end
  2359. end;
  2360. 32,33,34 : // 040..042
  2361. begin
  2362. getvalsym(c-32);
  2363. if assigned(currsym) then
  2364. objdata_writereloc(currval,4,currsym,currabsreloc32)
  2365. else
  2366. objdata.writebytes(currval,4);
  2367. end;
  2368. 36,37,38 : // 044..046 - select between word/dword/qword depending on
  2369. begin // address size (we support only default address sizes).
  2370. getvalsym(c-36);
  2371. {$ifdef x86_64}
  2372. if assigned(currsym) then
  2373. objdata_writereloc(currval,8,currsym,currabsreloc)
  2374. else
  2375. objdata.writebytes(currval,8);
  2376. {$else x86_64}
  2377. if assigned(currsym) then
  2378. objdata_writereloc(currval,4,currsym,currabsreloc32)
  2379. else
  2380. objdata.writebytes(currval,4);
  2381. {$endif x86_64}
  2382. end;
  2383. 40,41,42 : // 050..052 - byte relative operand
  2384. begin
  2385. getvalsym(c-40);
  2386. data:=currval-insend;
  2387. {$push}
  2388. {$r-}
  2389. if assigned(currsym) then
  2390. inc(data,currsym.address);
  2391. {$pop}
  2392. if (data>127) or (data<-128) then
  2393. Message1(asmw_e_short_jmp_out_of_range,tostr(data));
  2394. objdata.writebytes(data,1);
  2395. end;
  2396. 44,45,46: // 054..056 - qword immediate operand
  2397. begin
  2398. getvalsym(c-44);
  2399. if assigned(currsym) then
  2400. objdata_writereloc(currval,8,currsym,currabsreloc)
  2401. else
  2402. objdata.writebytes(currval,8);
  2403. end;
  2404. 52,53,54 : // 064..066 - select between 16/32 address mode, but we support only 32
  2405. begin
  2406. getvalsym(c-52);
  2407. if assigned(currsym) then
  2408. objdata_writereloc(currval,4,currsym,currrelreloc)
  2409. else
  2410. objdata_writereloc(currval-insend,4,nil,currabsreloc32)
  2411. end;
  2412. 56,57,58 : // 070..072 - long relative operand
  2413. begin
  2414. getvalsym(c-56);
  2415. if assigned(currsym) then
  2416. objdata_writereloc(currval,4,currsym,currrelreloc)
  2417. else
  2418. objdata_writereloc(currval-insend,4,nil,currabsreloc32)
  2419. end;
  2420. 60,61,62 : ; // 074..076 - vex-coded vector operand
  2421. // ignore
  2422. 172,173,174 : // 0254..0256 - dword implicitly sign-extended to 64-bit (x86_64 only)
  2423. begin
  2424. getvalsym(c-172);
  2425. {$ifdef x86_64}
  2426. { for i386 as aint type is longint the
  2427. following test is useless }
  2428. if (currval<low(longint)) or (currval>high(longint)) then
  2429. Message2(asmw_e_value_exceeds_bounds,'signed dword',tostr(currval));
  2430. {$endif x86_64}
  2431. if assigned(currsym) then
  2432. objdata_writereloc(currval,4,currsym,currabsreloc32)
  2433. else
  2434. objdata.writebytes(currval,4);
  2435. end;
  2436. 200 : { fixed 16-bit addr }
  2437. {$ifndef x86_64}
  2438. begin
  2439. bytes[0]:=$67;
  2440. objdata.writebytes(bytes,1);
  2441. end;
  2442. {$else x86_64}
  2443. { every insentry having code 0310 must be marked with NOX86_64 }
  2444. InternalError(2011051302);
  2445. {$endif}
  2446. 201 : { fixed 32-bit addr }
  2447. {$ifdef x86_64}
  2448. begin
  2449. bytes[0]:=$67;
  2450. objdata.writebytes(bytes,1);
  2451. end
  2452. {$endif x86_64}
  2453. ;
  2454. 208,209,210 :
  2455. begin
  2456. case oper[c-208]^.ot and OT_SIZE_MASK of
  2457. OT_BITS16 :
  2458. begin
  2459. bytes[0]:=$66;
  2460. objdata.writebytes(bytes,1);
  2461. end;
  2462. {$ifndef x86_64}
  2463. OT_BITS64 :
  2464. Message(asmw_e_64bit_not_supported);
  2465. {$endif x86_64}
  2466. end;
  2467. end;
  2468. 211,
  2469. 213 : {no action needed};
  2470. 212,
  2471. 241:
  2472. begin
  2473. if not(needed_VEX) then
  2474. begin
  2475. bytes[0]:=$66;
  2476. objdata.writebytes(bytes,1);
  2477. end;
  2478. end;
  2479. 214 :
  2480. begin
  2481. {$ifndef x86_64}
  2482. Message(asmw_e_64bit_not_supported);
  2483. {$endif x86_64}
  2484. end;
  2485. 219 :
  2486. begin
  2487. if not(needed_VEX) then
  2488. begin
  2489. bytes[0]:=$f3;
  2490. objdata.writebytes(bytes,1);
  2491. end;
  2492. end;
  2493. 220 :
  2494. begin
  2495. if not(needed_VEX) then
  2496. begin
  2497. bytes[0]:=$f2;
  2498. objdata.writebytes(bytes,1);
  2499. end;
  2500. end;
  2501. 221:
  2502. ;
  2503. 202,
  2504. 215,
  2505. 217,218 :
  2506. begin
  2507. { these are dissambler hints or 32 bit prefixes which
  2508. are not needed }
  2509. end;
  2510. 242..244: ; // VEX flags =>> nothing todo
  2511. 247: begin
  2512. if needed_VEX then
  2513. begin
  2514. if ops = 4 then
  2515. begin
  2516. if (oper[3]^.typ=top_reg) then
  2517. begin
  2518. if (oper[3]^.ot and otf_reg_xmm <> 0) or
  2519. (oper[3]^.ot and otf_reg_ymm <> 0) then
  2520. begin
  2521. bytes[0] := ((getsupreg(oper[3]^.reg) and 15) shl 4);
  2522. objdata.writebytes(bytes,1);
  2523. end
  2524. else Internalerror(777102);
  2525. end
  2526. else Internalerror(777103);
  2527. end
  2528. else Internalerror(777104);
  2529. end
  2530. else Internalerror(777105);
  2531. end;
  2532. 248..250: ; // VEX flags =>> nothing todo
  2533. 31,
  2534. 48,49,50 :
  2535. begin
  2536. InternalError(777006);
  2537. end
  2538. else
  2539. begin
  2540. { rex should be written at this point }
  2541. {$ifdef x86_64}
  2542. if not(needed_VEX) then // TG
  2543. if (rex<>0) and not(rexwritten) then
  2544. internalerror(200603191);
  2545. {$endif x86_64}
  2546. if (c>=64) and (c<=151) then // 0100..0227
  2547. begin
  2548. if (c<127) then // 0177
  2549. begin
  2550. if (oper[c and 7]^.typ=top_reg) then
  2551. rfield:=regval(oper[c and 7]^.reg)
  2552. else
  2553. rfield:=regval(oper[c and 7]^.ref^.base);
  2554. end
  2555. else
  2556. rfield:=c and 7;
  2557. opidx:=(c shr 3) and 7;
  2558. if not process_ea(oper[opidx]^,ea_data,rfield) then
  2559. Message(asmw_e_invalid_effective_address);
  2560. pb:=@bytes[0];
  2561. pb^:=ea_data.modrm;
  2562. inc(pb);
  2563. if ea_data.sib_present then
  2564. begin
  2565. pb^:=ea_data.sib;
  2566. inc(pb);
  2567. end;
  2568. s:=pb-@bytes[0];
  2569. objdata.writebytes(bytes,s);
  2570. case ea_data.bytes of
  2571. 0 : ;
  2572. 1 :
  2573. begin
  2574. if (oper[opidx]^.ot and OT_MEMORY)=OT_MEMORY then
  2575. begin
  2576. currsym:=objdata.symbolref(oper[opidx]^.ref^.symbol);
  2577. {$ifdef i386}
  2578. if (oper[opidx]^.ref^.refaddr=addr_pic) and
  2579. (tf_pic_uses_got in target_info.flags) then
  2580. currabsreloc:=RELOC_GOT32
  2581. else
  2582. {$endif i386}
  2583. {$ifdef x86_64}
  2584. if oper[opidx]^.ref^.refaddr=addr_pic then
  2585. currabsreloc:=RELOC_GOTPCREL
  2586. else
  2587. {$endif x86_64}
  2588. currabsreloc:=RELOC_ABSOLUTE;
  2589. objdata_writereloc(oper[opidx]^.ref^.offset,1,currsym,currabsreloc);
  2590. end
  2591. else
  2592. begin
  2593. bytes[0]:=oper[opidx]^.ref^.offset;
  2594. objdata.writebytes(bytes,1);
  2595. end;
  2596. inc(s);
  2597. end;
  2598. 2,4 :
  2599. begin
  2600. currsym:=objdata.symbolref(oper[opidx]^.ref^.symbol);
  2601. currval:=oper[opidx]^.ref^.offset;
  2602. {$ifdef x86_64}
  2603. if oper[opidx]^.ref^.refaddr=addr_pic then
  2604. currabsreloc:=RELOC_GOTPCREL
  2605. else
  2606. if oper[opidx]^.ref^.base=NR_RIP then
  2607. begin
  2608. currabsreloc:=RELOC_RELATIVE;
  2609. { Adjust reloc value by number of bytes following the displacement,
  2610. but not if displacement is specified by literal constant }
  2611. if Assigned(currsym) then
  2612. Dec(currval,InsEnd-objdata.CurrObjSec.Size-ea_data.bytes);
  2613. end
  2614. else
  2615. {$endif x86_64}
  2616. {$ifdef i386}
  2617. if (oper[opidx]^.ref^.refaddr=addr_pic) and
  2618. (tf_pic_uses_got in target_info.flags) then
  2619. currabsreloc:=RELOC_GOT32
  2620. else
  2621. {$endif i386}
  2622. currabsreloc:=RELOC_ABSOLUTE32;
  2623. if (currabsreloc=RELOC_ABSOLUTE32) and
  2624. (Assigned(oper[opidx]^.ref^.relsymbol)) then
  2625. begin
  2626. relsym:=objdata.symbolref(oper[opidx]^.ref^.relsymbol);
  2627. if relsym.objsection=objdata.CurrObjSec then
  2628. begin
  2629. currval:=objdata.CurrObjSec.size+ea_data.bytes-relsym.offset+currval;
  2630. currabsreloc:=RELOC_RELATIVE;
  2631. end
  2632. else
  2633. begin
  2634. currabsreloc:=RELOC_PIC_PAIR;
  2635. currval:=relsym.offset;
  2636. end;
  2637. end;
  2638. objdata_writereloc(currval,ea_data.bytes,currsym,currabsreloc);
  2639. inc(s,ea_data.bytes);
  2640. end;
  2641. end;
  2642. end
  2643. else
  2644. InternalError(777007);
  2645. end;
  2646. end;
  2647. until false;
  2648. end;
  2649. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  2650. begin
  2651. result:=(((opcode=A_MOV) or (opcode=A_XCHG)) and
  2652. (regtype = R_INTREGISTER) and
  2653. (ops=2) and
  2654. (oper[0]^.typ=top_reg) and
  2655. (oper[1]^.typ=top_reg) and
  2656. (oper[0]^.reg=oper[1]^.reg)
  2657. ) or
  2658. (((opcode=A_MOVSS) or (opcode=A_MOVSD) or (opcode=A_MOVQ) or
  2659. (opcode=A_MOVAPS) or (OPCODE=A_MOVAPD)) and
  2660. (regtype = R_MMREGISTER) and
  2661. (ops=2) and
  2662. (oper[0]^.typ=top_reg) and
  2663. (oper[1]^.typ=top_reg) and
  2664. (oper[0]^.reg=oper[1]^.reg)
  2665. );
  2666. end;
  2667. procedure build_spilling_operation_type_table;
  2668. var
  2669. opcode : tasmop;
  2670. i : integer;
  2671. begin
  2672. new(operation_type_table);
  2673. fillchar(operation_type_table^,sizeof(toperation_type_table),byte(operand_read));
  2674. for opcode:=low(tasmop) to high(tasmop) do
  2675. begin
  2676. for i:=1 to MaxInsChanges do
  2677. begin
  2678. case InsProp[opcode].Ch[i] of
  2679. Ch_Rop1 :
  2680. operation_type_table^[opcode,0]:=operand_read;
  2681. Ch_Wop1 :
  2682. operation_type_table^[opcode,0]:=operand_write;
  2683. Ch_RWop1,
  2684. Ch_Mop1 :
  2685. operation_type_table^[opcode,0]:=operand_readwrite;
  2686. Ch_Rop2 :
  2687. operation_type_table^[opcode,1]:=operand_read;
  2688. Ch_Wop2 :
  2689. operation_type_table^[opcode,1]:=operand_write;
  2690. Ch_RWop2,
  2691. Ch_Mop2 :
  2692. operation_type_table^[opcode,1]:=operand_readwrite;
  2693. Ch_Rop3 :
  2694. operation_type_table^[opcode,2]:=operand_read;
  2695. Ch_Wop3 :
  2696. operation_type_table^[opcode,2]:=operand_write;
  2697. Ch_RWop3,
  2698. Ch_Mop3 :
  2699. operation_type_table^[opcode,2]:=operand_readwrite;
  2700. end;
  2701. end;
  2702. end;
  2703. { Special cases that can't be decoded from the InsChanges flags }
  2704. operation_type_table^[A_IMUL,1]:=operand_readwrite;
  2705. end;
  2706. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  2707. begin
  2708. { the information in the instruction table is made for the string copy
  2709. operation MOVSD so hack here (FK)
  2710. }
  2711. if (opcode=A_MOVSD) and (ops=2) then
  2712. begin
  2713. case opnr of
  2714. 0:
  2715. result:=operand_read;
  2716. 1:
  2717. result:=operand_write;
  2718. else
  2719. internalerror(200506055);
  2720. end
  2721. end
  2722. else
  2723. result:=operation_type_table^[opcode,opnr];
  2724. end;
  2725. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  2726. var
  2727. tmpref: treference;
  2728. begin
  2729. case getregtype(r) of
  2730. R_INTREGISTER :
  2731. begin
  2732. tmpref:=ref;
  2733. if getsubreg(r)=R_SUBH then
  2734. inc(tmpref.offset);
  2735. { we don't need special code here for 32 bit loads on x86_64, since
  2736. those will automatically zero-extend the upper 32 bits. }
  2737. result:=taicpu.op_ref_reg(A_MOV,reg2opsize(r),tmpref,r);
  2738. end;
  2739. R_MMREGISTER :
  2740. case getsubreg(r) of
  2741. R_SUBMMD:
  2742. result:=taicpu.op_ref_reg(A_MOVSD,reg2opsize(r),ref,r);
  2743. R_SUBMMS:
  2744. result:=taicpu.op_ref_reg(A_MOVSS,reg2opsize(r),ref,r);
  2745. R_SUBQ,
  2746. R_SUBMMWHOLE:
  2747. result:=taicpu.op_ref_reg(A_MOVQ,S_NO,ref,r);
  2748. else
  2749. internalerror(200506043);
  2750. end;
  2751. else
  2752. internalerror(200401041);
  2753. end;
  2754. end;
  2755. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  2756. var
  2757. size: topsize;
  2758. tmpref: treference;
  2759. begin
  2760. case getregtype(r) of
  2761. R_INTREGISTER :
  2762. begin
  2763. tmpref:=ref;
  2764. if getsubreg(r)=R_SUBH then
  2765. inc(tmpref.offset);
  2766. size:=reg2opsize(r);
  2767. {$ifdef x86_64}
  2768. { even if it's a 32 bit reg, we still have to spill 64 bits
  2769. because we often perform 64 bit operations on them }
  2770. if (size=S_L) then
  2771. begin
  2772. size:=S_Q;
  2773. r:=newreg(getregtype(r),getsupreg(r),R_SUBWHOLE);
  2774. end;
  2775. {$endif x86_64}
  2776. result:=taicpu.op_reg_ref(A_MOV,size,r,tmpref);
  2777. end;
  2778. R_MMREGISTER :
  2779. case getsubreg(r) of
  2780. R_SUBMMD:
  2781. result:=taicpu.op_reg_ref(A_MOVSD,reg2opsize(r),r,ref);
  2782. R_SUBMMS:
  2783. result:=taicpu.op_reg_ref(A_MOVSS,reg2opsize(r),r,ref);
  2784. R_SUBQ,
  2785. R_SUBMMWHOLE:
  2786. result:=taicpu.op_reg_ref(A_MOVQ,S_NO,r,ref);
  2787. else
  2788. internalerror(200506042);
  2789. end;
  2790. else
  2791. internalerror(200401041);
  2792. end;
  2793. end;
  2794. {*****************************************************************************
  2795. Instruction table
  2796. *****************************************************************************}
  2797. procedure BuildInsTabCache;
  2798. var
  2799. i : longint;
  2800. begin
  2801. new(instabcache);
  2802. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  2803. i:=0;
  2804. while (i<InsTabEntries) do
  2805. begin
  2806. if InsTabCache^[InsTab[i].OPcode]=-1 then
  2807. InsTabCache^[InsTab[i].OPcode]:=i;
  2808. inc(i);
  2809. end;
  2810. end;
  2811. procedure BuildInsTabMemRefSizeInfoCache;
  2812. var
  2813. AsmOp: TasmOp;
  2814. i,j: longint;
  2815. insentry : PInsEntry;
  2816. MRefInfo: TMemRefSizeInfo;
  2817. SConstInfo: TConstSizeInfo;
  2818. actRegSize: int64;
  2819. actMemSize: int64;
  2820. actConstSize: int64;
  2821. actRegCount: integer;
  2822. actMemCount: integer;
  2823. actConstCount: integer;
  2824. actRegTypes : int64;
  2825. actRegMemTypes: int64;
  2826. NewRegSize: int64;
  2827. NewMemSize: int64;
  2828. NewConstSize: int64;
  2829. RegSize: int64;
  2830. MemSize: int64;
  2831. ConstSize: int64;
  2832. RegMMXSizeMask: int64;
  2833. RegXMMSizeMask: int64;
  2834. RegYMMSizeMask: int64;
  2835. bitcount: integer;
  2836. IsRegSizeMemSize: boolean;
  2837. ExistsRegMem: boolean;
  2838. s: string;
  2839. function bitcnt(aValue: int64): integer;
  2840. var
  2841. i: integer;
  2842. begin
  2843. result := 0;
  2844. for i := 0 to 63 do
  2845. begin
  2846. if (aValue mod 2) = 1 then
  2847. begin
  2848. inc(result);
  2849. end;
  2850. aValue := aValue shr 1;
  2851. end;
  2852. end;
  2853. begin
  2854. new(InsTabMemRefSizeInfoCache);
  2855. FillChar(InsTabMemRefSizeInfoCache^,sizeof(TInsTabMemRefSizeInfoCache),0);
  2856. for AsmOp := low(TAsmOp) to high(TAsmOp) do
  2857. begin
  2858. i := InsTabCache^[AsmOp];
  2859. if i >= 0 then
  2860. begin
  2861. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiUnkown;
  2862. InsTabMemRefSizeInfoCache^[AsmOp].ConstSize := csiUnkown;
  2863. InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX := false;
  2864. RegSize := 0;
  2865. IsRegSizeMemSize := true;
  2866. ExistsRegMem := false;
  2867. insentry:=@instab[i];
  2868. RegMMXSizeMask := 0;
  2869. RegXMMSizeMask := 0;
  2870. RegYMMSizeMask := 0;
  2871. while (insentry^.opcode=AsmOp) do
  2872. begin
  2873. MRefInfo := msiUnkown;
  2874. actRegSize := 0;
  2875. actRegCount := 0;
  2876. actRegTypes := 0;
  2877. NewRegSize := 0;
  2878. actMemSize := 0;
  2879. actMemCount := 0;
  2880. actRegMemTypes := 0;
  2881. NewMemSize := 0;
  2882. actConstSize := 0;
  2883. actConstCount := 0;
  2884. NewConstSize := 0;
  2885. if asmop = a_movups then
  2886. begin
  2887. RegXMMSizeMask := RegXMMSizeMask;
  2888. end;
  2889. for j := 0 to insentry^.ops -1 do
  2890. begin
  2891. if (insentry^.optypes[j] and OT_REGISTER) = OT_REGISTER then
  2892. begin
  2893. inc(actRegCount);
  2894. NewRegSize := (insentry^.optypes[j] and OT_SIZE_MASK);
  2895. if NewRegSize = 0 then
  2896. begin
  2897. case insentry^.optypes[j] and (OT_MMXREG OR OT_XMMREG OR OT_YMMREG) of
  2898. OT_MMXREG: begin
  2899. NewRegSize := OT_BITS64;
  2900. end;
  2901. OT_XMMREG: begin
  2902. NewRegSize := OT_BITS128;
  2903. InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX := true;
  2904. end;
  2905. OT_YMMREG: begin
  2906. NewRegSize := OT_BITS256;
  2907. InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX := true;
  2908. end;
  2909. else NewRegSize := not(0);
  2910. end;
  2911. end;
  2912. actRegSize := actRegSize or NewRegSize;
  2913. actRegTypes := actRegTypes or (insentry^.optypes[j] and (OT_MMXREG OR OT_XMMREG OR OT_YMMREG));
  2914. end
  2915. else if ((insentry^.optypes[j] and OT_MEMORY) <> 0) then
  2916. begin
  2917. inc(actMemCount);
  2918. actMemSize := actMemSize or (insentry^.optypes[j] and OT_SIZE_MASK);
  2919. if (insentry^.optypes[j] and OT_REGMEM) = OT_REGMEM then
  2920. begin
  2921. actRegMemTypes := actRegMemTypes or insentry^.optypes[j];
  2922. end;
  2923. end
  2924. else if ((insentry^.optypes[j] and OT_IMMEDIATE) = OT_IMMEDIATE) then
  2925. begin
  2926. inc(actConstCount);
  2927. actConstSize := actConstSize or (insentry^.optypes[j] and OT_SIZE_MASK);
  2928. end
  2929. end;
  2930. if actConstCount > 0 then
  2931. begin
  2932. case actConstSize of
  2933. 0: SConstInfo := csiNoSize;
  2934. OT_BITS8: SConstInfo := csiMem8;
  2935. OT_BITS16: SConstInfo := csiMem16;
  2936. OT_BITS32: SConstInfo := csiMem32;
  2937. OT_BITS64: SConstInfo := csiMem64;
  2938. else SConstInfo := csiMultiple;
  2939. end;
  2940. if InsTabMemRefSizeInfoCache^[AsmOp].ConstSize = csiUnkown then
  2941. begin
  2942. InsTabMemRefSizeInfoCache^[AsmOp].ConstSize := SConstInfo;
  2943. end
  2944. else if InsTabMemRefSizeInfoCache^[AsmOp].ConstSize <> SConstInfo then
  2945. begin
  2946. InsTabMemRefSizeInfoCache^[AsmOp].ConstSize := csiMultiple;
  2947. end;
  2948. end;
  2949. case actMemCount of
  2950. 0: ; // nothing todo
  2951. 1: begin
  2952. MRefInfo := msiUnkown;
  2953. case actRegMemTypes and (OT_MMXRM OR OT_XMMRM OR OT_YMMRM) of
  2954. OT_MMXRM: actMemSize := actMemSize or OT_BITS64;
  2955. OT_XMMRM: actMemSize := actMemSize or OT_BITS128;
  2956. OT_YMMRM: actMemSize := actMemSize or OT_BITS256;
  2957. end;
  2958. case actMemSize of
  2959. 0: MRefInfo := msiNoSize;
  2960. OT_BITS8: MRefInfo := msiMem8;
  2961. OT_BITS16: MRefInfo := msiMem16;
  2962. OT_BITS32: MRefInfo := msiMem32;
  2963. OT_BITS64: MRefInfo := msiMem64;
  2964. OT_BITS128: MRefInfo := msiMem128;
  2965. OT_BITS256: MRefInfo := msiMem256;
  2966. OT_BITS80,
  2967. OT_FAR,
  2968. OT_NEAR,
  2969. OT_SHORT: ; // ignore
  2970. else begin
  2971. bitcount := bitcnt(actMemSize);
  2972. if bitcount > 1 then MRefInfo := msiMultiple
  2973. else InternalError(777203);
  2974. end;
  2975. end;
  2976. if InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize = msiUnkown then
  2977. begin
  2978. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := MRefInfo;
  2979. end
  2980. else if InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize <> MRefInfo then
  2981. begin
  2982. with InsTabMemRefSizeInfoCache^[AsmOp] do
  2983. begin
  2984. if ((MemRefSize = msiMem8) OR (MRefInfo = msiMem8)) then MemRefSize := msiMultiple8
  2985. else if ((MemRefSize = msiMem16) OR (MRefInfo = msiMem16)) then MemRefSize := msiMultiple16
  2986. else if ((MemRefSize = msiMem32) OR (MRefInfo = msiMem32)) then MemRefSize := msiMultiple32
  2987. else if ((MemRefSize = msiMem64) OR (MRefInfo = msiMem64)) then MemRefSize := msiMultiple64
  2988. else if ((MemRefSize = msiMem128) OR (MRefInfo = msiMem128)) then MemRefSize := msiMultiple128
  2989. else if ((MemRefSize = msiMem256) OR (MRefInfo = msiMem256)) then MemRefSize := msiMultiple256
  2990. else MemRefSize := msiMultiple;
  2991. end;
  2992. end;
  2993. if actRegCount > 0 then
  2994. begin
  2995. case actRegTypes and (OT_MMXREG or OT_XMMREG or OT_YMMREG) of
  2996. OT_MMXREG: RegMMXSizeMask := RegMMXSizeMask or actMemSize;
  2997. OT_XMMREG: RegXMMSizeMask := RegXMMSizeMask or actMemSize;
  2998. OT_YMMREG: RegYMMSizeMask := RegYMMSizeMask or actMemSize;
  2999. else begin
  3000. RegMMXSizeMask := not(0);
  3001. RegXMMSizeMask := not(0);
  3002. RegYMMSizeMask := not(0);
  3003. end;
  3004. end;
  3005. end;
  3006. end;
  3007. else InternalError(777202);
  3008. end;
  3009. inc(insentry);
  3010. end;
  3011. if (InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize in MemRefMultiples) and
  3012. (InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX)then
  3013. begin
  3014. case RegXMMSizeMask of
  3015. OT_BITS64: case RegYMMSizeMask of
  3016. OT_BITS128: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegx64y128;
  3017. OT_BITS256: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegx64y256;
  3018. end;
  3019. OT_BITS128: begin
  3020. if RegMMXSizeMask = 0 then
  3021. begin
  3022. case RegYMMSizeMask of
  3023. OT_BITS128: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegx64y128;
  3024. OT_BITS256: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegSize;
  3025. end;
  3026. end
  3027. else if RegYMMSizeMask = 0 then
  3028. begin
  3029. case RegMMXSizeMask of
  3030. OT_BITS64: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegSize;
  3031. end;
  3032. end
  3033. else InternalError(777205);
  3034. end;
  3035. end;
  3036. end;
  3037. end;
  3038. end;
  3039. for AsmOp := low(TAsmOp) to high(TAsmOp) do
  3040. begin
  3041. // only supported intructiones with SSE- or AVX-operands
  3042. if not(InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX) then
  3043. begin
  3044. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiUnkown;
  3045. InsTabMemRefSizeInfoCache^[AsmOp].ConstSize := csiUnkown;
  3046. end;
  3047. end;
  3048. end;
  3049. procedure InitAsm;
  3050. begin
  3051. build_spilling_operation_type_table;
  3052. if not assigned(instabcache) then
  3053. BuildInsTabCache;
  3054. if not assigned(InsTabMemRefSizeInfoCache) then
  3055. BuildInsTabMemRefSizeInfoCache;
  3056. end;
  3057. procedure DoneAsm;
  3058. begin
  3059. if assigned(operation_type_table) then
  3060. begin
  3061. dispose(operation_type_table);
  3062. operation_type_table:=nil;
  3063. end;
  3064. if assigned(instabcache) then
  3065. begin
  3066. dispose(instabcache);
  3067. instabcache:=nil;
  3068. end;
  3069. if assigned(InsTabMemRefSizeInfoCache) then
  3070. begin
  3071. dispose(InsTabMemRefSizeInfoCache);
  3072. InsTabMemRefSizeInfoCache:=nil;
  3073. end;
  3074. end;
  3075. begin
  3076. cai_align:=tai_align;
  3077. cai_cpu:=taicpu;
  3078. end.