aasmcpu.pas 117 KB

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