aasmcpu.pas 140 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101
  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. OT_XMEM32 = OT_REGNORM or otf_reg_xmm or otf_reg_gpr or OT_BITS32;
  131. OT_XMEM64 = OT_REGNORM or otf_reg_xmm or otf_reg_gpr or OT_BITS64;
  132. { register class 5: XMM (both reg and r/m) }
  133. OT_YMMREG = OT_REGNORM or otf_reg_ymm;
  134. OT_YMMRM = OT_REGMEM or otf_reg_ymm;
  135. OT_YMEM32 = OT_REGNORM or otf_reg_ymm or otf_reg_gpr or OT_BITS32;
  136. OT_YMEM64 = OT_REGNORM or otf_reg_ymm or otf_reg_gpr or OT_BITS64;
  137. { Vector-Memory operands }
  138. OT_VMEM_ANY = OT_XMEM32 or OT_XMEM64 or OT_YMEM32 or OT_YMEM64;
  139. { Memory operands }
  140. OT_MEM8 = OT_MEMORY or OT_BITS8;
  141. OT_MEM16 = OT_MEMORY or OT_BITS16;
  142. OT_MEM32 = OT_MEMORY or OT_BITS32;
  143. OT_MEM64 = OT_MEMORY or OT_BITS64;
  144. OT_MEM128 = OT_MEMORY or OT_BITS128;
  145. OT_MEM256 = OT_MEMORY or OT_BITS256;
  146. OT_MEM80 = OT_MEMORY or OT_BITS80;
  147. OT_MEM_OFFS = OT_MEMORY or otf_sub0; { special type of EA }
  148. { simple [address] offset }
  149. { Matches any type of r/m operand }
  150. OT_MEMORY_ANY = OT_MEMORY or OT_RM_GPR or OT_XMMRM or OT_MMXRM or OT_YMMRM;
  151. { Immediate operands }
  152. OT_IMM8 = OT_IMMEDIATE or OT_BITS8;
  153. OT_IMM16 = OT_IMMEDIATE or OT_BITS16;
  154. OT_IMM32 = OT_IMMEDIATE or OT_BITS32;
  155. OT_IMM64 = OT_IMMEDIATE or OT_BITS64;
  156. OT_ONENESS = otf_sub0; { special type of immediate operand }
  157. OT_UNITY = OT_IMMEDIATE or OT_ONENESS; { for shift/rotate instructions }
  158. { Size of the instruction table converted by nasmconv.pas }
  159. {$if defined(x86_64)}
  160. instabentries = {$i x8664nop.inc}
  161. {$elseif defined(i386)}
  162. instabentries = {$i i386nop.inc}
  163. {$elseif defined(i8086)}
  164. instabentries = {$i i8086nop.inc}
  165. {$endif}
  166. maxinfolen = 8;
  167. MaxInsChanges = 3; { Max things a instruction can change }
  168. type
  169. { What an instruction can change. Needed for optimizer and spilling code.
  170. Note: The order of this enumeration is should not be changed! }
  171. TInsChange = (Ch_None,
  172. {Read from a register}
  173. Ch_REAX, Ch_RECX, Ch_REDX, Ch_REBX, Ch_RESP, Ch_REBP, Ch_RESI, Ch_REDI,
  174. {write from a register}
  175. Ch_WEAX, Ch_WECX, Ch_WEDX, Ch_WEBX, Ch_WESP, Ch_WEBP, Ch_WESI, Ch_WEDI,
  176. {read and write from/to a register}
  177. Ch_RWEAX, Ch_RWECX, Ch_RWEDX, Ch_RWEBX, Ch_RWESP, Ch_RWEBP, Ch_RWESI, Ch_RWEDI,
  178. {modify the contents of a register with the purpose of using
  179. this changed content afterwards (add/sub/..., but e.g. not rep
  180. or movsd)}
  181. Ch_MEAX, Ch_MECX, Ch_MEDX, Ch_MEBX, Ch_MESP, Ch_MEBP, Ch_MESI, Ch_MEDI,
  182. Ch_CDirFlag {clear direction flag}, Ch_SDirFlag {set dir flag},
  183. Ch_RFlags, Ch_WFlags, Ch_RWFlags, Ch_FPU,
  184. Ch_Rop1, Ch_Wop1, Ch_RWop1,Ch_Mop1,
  185. Ch_Rop2, Ch_Wop2, Ch_RWop2,Ch_Mop2,
  186. Ch_Rop3, Ch_WOp3, Ch_RWOp3,Ch_Mop3,
  187. Ch_WMemEDI,
  188. Ch_All,
  189. { x86_64 registers }
  190. Ch_RRAX, Ch_RRCX, Ch_RRDX, Ch_RRBX, Ch_RRSP, Ch_RRBP, Ch_RRSI, Ch_RRDI,
  191. Ch_WRAX, Ch_WRCX, Ch_WRDX, Ch_WRBX, Ch_WRSP, Ch_WRBP, Ch_WRSI, Ch_WRDI,
  192. Ch_RWRAX, Ch_RWRCX, Ch_RWRDX, Ch_RWRBX, Ch_RWRSP, Ch_RWRBP, Ch_RWRSI, Ch_RWRDI,
  193. Ch_MRAX, Ch_MRCX, Ch_MRDX, Ch_MRBX, Ch_MRSP, Ch_MRBP, Ch_MRSI, Ch_MRDI
  194. );
  195. TInsProp = packed record
  196. Ch : Array[1..MaxInsChanges] of TInsChange;
  197. end;
  198. TMemRefSizeInfo = (msiUnkown, msiUnsupported, msiNoSize,
  199. msiMultiple, msiMultiple8, msiMultiple16, msiMultiple32,
  200. msiMultiple64, msiMultiple128, msiMultiple256,
  201. msiMemRegSize, msiMemRegx16y32, msiMemRegx32y64, msiMemRegx64y128, msiMemRegx64y256,
  202. msiMem8, msiMem16, msiMem32, msiMem64, msiMem128, msiMem256,
  203. msiXMem32, msiXMem64, msiYMem32, msiYMem64,
  204. msiVMemMultiple, msiVMemRegSize);
  205. TConstSizeInfo = (csiUnkown, csiMultiple, csiNoSize, csiMem8, csiMem16, csiMem32, csiMem64);
  206. TInsTabMemRefSizeInfoRec = record
  207. MemRefSize : TMemRefSizeInfo;
  208. ExistsSSEAVX: boolean;
  209. ConstSize : TConstSizeInfo;
  210. end;
  211. const
  212. MemRefMultiples: set of TMemRefSizeInfo = [msiMultiple, msiMultiple8,
  213. msiMultiple16, msiMultiple32,
  214. msiMultiple64, msiMultiple128,
  215. msiMultiple256, msiVMemMultiple];
  216. MemRefSizeInfoVMems: Set of TMemRefSizeInfo = [msiXMem32, msiXMem64, msiYMem32, msiYMem64,
  217. msiVMemMultiple, msiVMemRegSize];
  218. InsProp : array[tasmop] of TInsProp =
  219. {$if defined(x86_64)}
  220. {$i x8664pro.inc}
  221. {$elseif defined(i386)}
  222. {$i i386prop.inc}
  223. {$elseif defined(i8086)}
  224. {$i i8086prop.inc}
  225. {$endif}
  226. type
  227. TOperandOrder = (op_intel,op_att);
  228. tinsentry=packed record
  229. opcode : tasmop;
  230. ops : byte;
  231. optypes : array[0..max_operands-1] of longint;
  232. code : array[0..maxinfolen] of char;
  233. flags : int64;
  234. end;
  235. pinsentry=^tinsentry;
  236. { alignment for operator }
  237. tai_align = class(tai_align_abstract)
  238. reg : tregister;
  239. constructor create(b:byte);override;
  240. constructor create_op(b: byte; _op: byte);override;
  241. function calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;override;
  242. end;
  243. taicpu = class(tai_cpu_abstract_sym)
  244. opsize : topsize;
  245. constructor op_none(op : tasmop);
  246. constructor op_none(op : tasmop;_size : topsize);
  247. constructor op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  248. constructor op_const(op : tasmop;_size : topsize;_op1 : aint);
  249. constructor op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  250. constructor op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  251. constructor op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  252. constructor op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aint);
  253. constructor op_const_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister);
  254. constructor op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aint);
  255. constructor op_const_ref(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference);
  256. constructor op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  257. constructor op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  258. constructor op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;_op3 : tregister);
  259. constructor op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference;_op3 : tregister);
  260. constructor op_ref_reg_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2,_op3 : tregister);
  261. constructor op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;const _op3 : treference);
  262. { this is for Jmp instructions }
  263. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  264. constructor op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  265. constructor op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  266. constructor op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  267. constructor op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  268. procedure changeopsize(siz:topsize);
  269. function GetString:string;
  270. { This is a workaround for the GAS non commutative fpu instruction braindamage.
  271. Early versions of the UnixWare assembler had a bug where some fpu instructions
  272. were reversed and GAS still keeps this "feature" for compatibility.
  273. for details: http://sourceware.org/binutils/docs/as/i386_002dBugs.html#i386_002dBugs
  274. http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=372528
  275. http://en.wikibooks.org/wiki/X86_Assembly/GAS_Syntax#Caveats
  276. Since FPC is "GAS centric" due to its history it generates instructions with the same operand order so
  277. when generating output for other assemblers, the opcodes must be fixed before writing them.
  278. This function returns the fixed opcodes. Changing the opcodes permanently is no good idea
  279. because in case of smartlinking assembler is generated twice so at the second run wrong
  280. assembler is generated.
  281. }
  282. function FixNonCommutativeOpcodes: tasmop;
  283. private
  284. FOperandOrder : TOperandOrder;
  285. procedure init(_size : topsize); { this need to be called by all constructor }
  286. public
  287. { the next will reset all instructions that can change in pass 2 }
  288. procedure ResetPass1;override;
  289. procedure ResetPass2;override;
  290. function CheckIfValid:boolean;
  291. function Pass1(objdata:TObjData):longint;override;
  292. procedure Pass2(objdata:TObjData);override;
  293. procedure SetOperandOrder(order:TOperandOrder);
  294. function is_same_reg_move(regtype: Tregistertype):boolean;override;
  295. { register spilling code }
  296. function spilling_get_operation_type(opnr: longint): topertype;override;
  297. {$ifdef i8086}
  298. procedure loadsegsymbol(opidx:longint;s:tasmsymbol);
  299. {$endif i8086}
  300. private
  301. { next fields are filled in pass1, so pass2 is faster }
  302. insentry : PInsEntry;
  303. insoffset : longint;
  304. LastInsOffset : longint; { need to be public to be reset }
  305. inssize : shortint;
  306. {$ifdef x86_64}
  307. rex : byte;
  308. {$endif x86_64}
  309. function InsEnd:longint;
  310. procedure create_ot(objdata:TObjData);
  311. function Matches(p:PInsEntry):boolean;
  312. function calcsize(p:PInsEntry):shortint;
  313. procedure gencode(objdata:TObjData);
  314. function NeedAddrPrefix(opidx:byte):boolean;
  315. procedure Swapoperands;
  316. function FindInsentry(objdata:TObjData):boolean;
  317. end;
  318. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  319. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  320. function MemRefInfo(aAsmop: TAsmOp): TInsTabMemRefSizeInfoRec;
  321. procedure InitAsm;
  322. procedure DoneAsm;
  323. {*****************************************************************************
  324. External Symbol Chain
  325. used for agx86nsm and agx86int
  326. *****************************************************************************}
  327. type
  328. PExternChain = ^TExternChain;
  329. TExternChain = Record
  330. psym : pshortstring;
  331. is_defined : boolean;
  332. next : PExternChain;
  333. end;
  334. const
  335. FEC : PExternChain = nil;
  336. procedure AddSymbol(symname : string; defined : boolean);
  337. procedure FreeExternChainList;
  338. implementation
  339. uses
  340. cutils,
  341. globals,
  342. systems,
  343. procinfo,
  344. itcpugas,
  345. symsym,
  346. cpuinfo;
  347. procedure AddSymbol(symname : string; defined : boolean);
  348. var
  349. EC : PExternChain;
  350. begin
  351. EC:=FEC;
  352. while assigned(EC) do
  353. begin
  354. if EC^.psym^=symname then
  355. begin
  356. if defined then
  357. EC^.is_defined:=true;
  358. exit;
  359. end;
  360. EC:=EC^.next;
  361. end;
  362. New(EC);
  363. EC^.next:=FEC;
  364. FEC:=EC;
  365. FEC^.psym:=stringdup(symname);
  366. FEC^.is_defined := defined;
  367. end;
  368. procedure FreeExternChainList;
  369. var
  370. EC : PExternChain;
  371. begin
  372. EC:=FEC;
  373. while assigned(EC) do
  374. begin
  375. FEC:=EC^.next;
  376. stringdispose(EC^.psym);
  377. Dispose(EC);
  378. EC:=FEC;
  379. end;
  380. end;
  381. {*****************************************************************************
  382. Instruction table
  383. *****************************************************************************}
  384. const
  385. {Instruction flags }
  386. IF_NONE = $00000000;
  387. IF_SM = $00000001; { size match first two operands }
  388. IF_SM2 = $00000002;
  389. IF_SB = $00000004; { unsized operands can't be non-byte }
  390. IF_SW = $00000008; { unsized operands can't be non-word }
  391. IF_SD = $00000010; { unsized operands can't be nondword }
  392. IF_SMASK = $0000001f;
  393. IF_AR0 = $00000020; { SB, SW, SD applies to argument 0 }
  394. IF_AR1 = $00000040; { SB, SW, SD applies to argument 1 }
  395. IF_AR2 = $00000060; { SB, SW, SD applies to argument 2 }
  396. IF_ARMASK = $00000060; { mask for unsized argument spec }
  397. IF_ARSHIFT = 5; { LSB of IF_ARMASK }
  398. IF_PRIV = $00000100; { it's a privileged instruction }
  399. IF_SMM = $00000200; { it's only valid in SMM }
  400. IF_PROT = $00000400; { it's protected mode only }
  401. IF_NOX86_64 = $00000800; { removed instruction in x86_64 }
  402. IF_UNDOC = $00001000; { it's an undocumented instruction }
  403. IF_FPU = $00002000; { it's an FPU instruction }
  404. IF_MMX = $00004000; { it's an MMX instruction }
  405. { it's a 3DNow! instruction }
  406. IF_3DNOW = $00008000;
  407. { it's a SSE (KNI, MMX2) instruction }
  408. IF_SSE = $00010000;
  409. { SSE2 instructions }
  410. IF_SSE2 = $00020000;
  411. { SSE3 instructions }
  412. IF_SSE3 = $00040000;
  413. { SSE64 instructions }
  414. IF_SSE64 = $00080000;
  415. { the mask for processor types }
  416. {IF_PMASK = longint($FF000000);}
  417. { the mask for disassembly "prefer" }
  418. {IF_PFMASK = longint($F001FF00);}
  419. { SVM instructions }
  420. IF_SVM = $00100000;
  421. { SSE4 instructions }
  422. IF_SSE4 = $00200000;
  423. { TODO: These flags were added to make x86ins.dat more readable.
  424. Values must be reassigned to make any other use of them. }
  425. IF_SSSE3 = $00200000;
  426. IF_SSE41 = $00200000;
  427. IF_SSE42 = $00200000;
  428. IF_AVX = $00200000;
  429. IF_AVX2 = $00200000;
  430. IF_BMI1 = $00200000;
  431. IF_BMI2 = $00200000;
  432. IF_16BITONLY = $00200000;
  433. IF_FMA = $00200000;
  434. IF_FMA4 = $00200000;
  435. IF_TSX = $00200000;
  436. IF_RAND = $00200000;
  437. IF_XSAVE = $00200000;
  438. IF_PLEVEL = $0F000000; { mask for processor level }
  439. IF_8086 = $00000000; { 8086 instruction }
  440. IF_186 = $01000000; { 186+ instruction }
  441. IF_286 = $02000000; { 286+ instruction }
  442. IF_386 = $03000000; { 386+ instruction }
  443. IF_486 = $04000000; { 486+ instruction }
  444. IF_PENT = $05000000; { Pentium instruction }
  445. IF_P6 = $06000000; { P6 instruction }
  446. IF_KATMAI = $07000000; { Katmai instructions }
  447. IF_WILLAMETTE = $08000000; { Willamette instructions }
  448. IF_PRESCOTT = $09000000; { Prescott instructions }
  449. IF_X86_64 = $0a000000;
  450. IF_SANDYBRIDGE = $0e000000; { Sandybridge-specific instruction }
  451. IF_NEC = $0f000000; { NEC V20/V30 instruction }
  452. { the following are not strictly part of the processor level, because
  453. they are never used standalone, but always in combination with a
  454. separate processor level flag. Therefore, they use bits outside of
  455. IF_PLEVEL, otherwise they would mess up the processor level they're
  456. used in combination with.
  457. The following combinations are currently used:
  458. IF_AMD or IF_P6,
  459. IF_CYRIX or IF_486,
  460. IF_CYRIX or IF_PENT,
  461. IF_CYRIX or IF_P6 }
  462. IF_CYRIX = $10000000; { Cyrix, Centaur or VIA-specific instruction }
  463. IF_AMD = $20000000; { AMD-specific instruction }
  464. { added flags }
  465. IF_PRE = $40000000; { it's a prefix instruction }
  466. IF_PASS2 = $80000000; { if the instruction can change in a second pass }
  467. IF_IMM4 = $100000000; { immediate operand is a nibble (must be in range [0..15]) }
  468. IF_IMM3 = $200000000; { immediate operand is a triad (must be in range [0..7]) }
  469. type
  470. TInsTabCache=array[TasmOp] of longint;
  471. PInsTabCache=^TInsTabCache;
  472. TInsTabMemRefSizeInfoCache=array[TasmOp] of TInsTabMemRefSizeInfoRec;
  473. PInsTabMemRefSizeInfoCache=^TInsTabMemRefSizeInfoCache;
  474. const
  475. {$if defined(x86_64)}
  476. InsTab:array[0..instabentries-1] of TInsEntry={$i x8664tab.inc}
  477. {$elseif defined(i386)}
  478. InsTab:array[0..instabentries-1] of TInsEntry={$i i386tab.inc}
  479. {$elseif defined(i8086)}
  480. InsTab:array[0..instabentries-1] of TInsEntry={$i i8086tab.inc}
  481. {$endif}
  482. var
  483. InsTabCache : PInsTabCache;
  484. InsTabMemRefSizeInfoCache: PInsTabMemRefSizeInfoCache;
  485. const
  486. {$if defined(x86_64)}
  487. { Intel style operands ! }
  488. opsize_2_type:array[0..2,topsize] of longint=(
  489. (OT_NONE,
  490. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,OT_BITS64,OT_BITS64,OT_BITS64,
  491. OT_BITS16,OT_BITS32,OT_BITS64,
  492. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  493. OT_BITS64,
  494. OT_NEAR,OT_FAR,OT_SHORT,
  495. OT_NONE,
  496. OT_BITS128,
  497. OT_BITS256
  498. ),
  499. (OT_NONE,
  500. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,OT_BITS8,OT_BITS16,OT_BITS32,
  501. OT_BITS16,OT_BITS32,OT_BITS64,
  502. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  503. OT_BITS64,
  504. OT_NEAR,OT_FAR,OT_SHORT,
  505. OT_NONE,
  506. OT_BITS128,
  507. OT_BITS256
  508. ),
  509. (OT_NONE,
  510. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,
  511. OT_BITS16,OT_BITS32,OT_BITS64,
  512. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  513. OT_BITS64,
  514. OT_NEAR,OT_FAR,OT_SHORT,
  515. OT_NONE,
  516. OT_BITS128,
  517. OT_BITS256
  518. )
  519. );
  520. reg_ot_table : array[tregisterindex] of longint = (
  521. {$i r8664ot.inc}
  522. );
  523. {$elseif defined(i386)}
  524. { Intel style operands ! }
  525. opsize_2_type:array[0..2,topsize] of longint=(
  526. (OT_NONE,
  527. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,
  528. OT_BITS16,OT_BITS32,OT_BITS64,
  529. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  530. OT_BITS64,
  531. OT_NEAR,OT_FAR,OT_SHORT,
  532. OT_NONE,
  533. OT_BITS128,
  534. OT_BITS256
  535. ),
  536. (OT_NONE,
  537. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,
  538. OT_BITS16,OT_BITS32,OT_BITS64,
  539. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  540. OT_BITS64,
  541. OT_NEAR,OT_FAR,OT_SHORT,
  542. OT_NONE,
  543. OT_BITS128,
  544. OT_BITS256
  545. ),
  546. (OT_NONE,
  547. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,
  548. OT_BITS16,OT_BITS32,OT_BITS64,
  549. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  550. OT_BITS64,
  551. OT_NEAR,OT_FAR,OT_SHORT,
  552. OT_NONE,
  553. OT_BITS128,
  554. OT_BITS256
  555. )
  556. );
  557. reg_ot_table : array[tregisterindex] of longint = (
  558. {$i r386ot.inc}
  559. );
  560. {$elseif defined(i8086)}
  561. { Intel style operands ! }
  562. opsize_2_type:array[0..2,topsize] of longint=(
  563. (OT_NONE,
  564. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,
  565. OT_BITS16,OT_BITS32,OT_BITS64,
  566. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  567. OT_BITS64,
  568. OT_NEAR,OT_FAR,OT_SHORT,
  569. OT_NONE,
  570. OT_BITS128,
  571. OT_BITS256
  572. ),
  573. (OT_NONE,
  574. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,
  575. OT_BITS16,OT_BITS32,OT_BITS64,
  576. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  577. OT_BITS64,
  578. OT_NEAR,OT_FAR,OT_SHORT,
  579. OT_NONE,
  580. OT_BITS128,
  581. OT_BITS256
  582. ),
  583. (OT_NONE,
  584. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,
  585. OT_BITS16,OT_BITS32,OT_BITS64,
  586. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  587. OT_BITS64,
  588. OT_NEAR,OT_FAR,OT_SHORT,
  589. OT_NONE,
  590. OT_BITS128,
  591. OT_BITS256
  592. )
  593. );
  594. reg_ot_table : array[tregisterindex] of longint = (
  595. {$i r8086ot.inc}
  596. );
  597. {$endif}
  598. function MemRefInfo(aAsmop: TAsmOp): TInsTabMemRefSizeInfoRec;
  599. begin
  600. result := InsTabMemRefSizeInfoCache^[aAsmop];
  601. end;
  602. { Operation type for spilling code }
  603. type
  604. toperation_type_table=array[tasmop,0..Max_Operands] of topertype;
  605. var
  606. operation_type_table : ^toperation_type_table;
  607. {****************************************************************************
  608. TAI_ALIGN
  609. ****************************************************************************}
  610. constructor tai_align.create(b: byte);
  611. begin
  612. inherited create(b);
  613. reg:=NR_ECX;
  614. end;
  615. constructor tai_align.create_op(b: byte; _op: byte);
  616. begin
  617. inherited create_op(b,_op);
  618. reg:=NR_NO;
  619. end;
  620. function tai_align.calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;
  621. const
  622. { Updated according to
  623. Software Optimization Guide for AMD Family 15h Processors, Verison 3.08, January 2014
  624. and
  625. Intel 64 and IA-32 Architectures Software Developer’s Manual
  626. Volume 2B: Instruction Set Reference, N-Z, January 2015
  627. }
  628. alignarray_cmovcpus:array[0..10] of string[11]=(
  629. #$66#$66#$66#$0F#$1F#$84#$00#$00#$00#$00#$00,
  630. #$66#$66#$0F#$1F#$84#$00#$00#$00#$00#$00,
  631. #$66#$0F#$1F#$84#$00#$00#$00#$00#$00,
  632. #$0F#$1F#$84#$00#$00#$00#$00#$00,
  633. #$0F#$1F#$80#$00#$00#$00#$00,
  634. #$66#$0F#$1F#$44#$00#$00,
  635. #$0F#$1F#$44#$00#$00,
  636. #$0F#$1F#$40#$00,
  637. #$0F#$1F#$00,
  638. #$66#$90,
  639. #$90);
  640. {$ifdef i8086}
  641. alignarray:array[0..5] of string[8]=(
  642. #$90#$90#$90#$90#$90#$90#$90,
  643. #$90#$90#$90#$90#$90#$90,
  644. #$90#$90#$90#$90,
  645. #$90#$90#$90,
  646. #$90#$90,
  647. #$90);
  648. {$else i8086}
  649. alignarray:array[0..5] of string[8]=(
  650. #$8D#$B4#$26#$00#$00#$00#$00,
  651. #$8D#$B6#$00#$00#$00#$00,
  652. #$8D#$74#$26#$00,
  653. #$8D#$76#$00,
  654. #$89#$F6,
  655. #$90);
  656. {$endif i8086}
  657. var
  658. bufptr : pchar;
  659. j : longint;
  660. localsize: byte;
  661. begin
  662. inherited calculatefillbuf(buf,executable);
  663. if not(use_op) and executable then
  664. begin
  665. bufptr:=pchar(@buf);
  666. { fillsize may still be used afterwards, so don't modify }
  667. { e.g. writebytes(hp.calculatefillbuf(buf)^,hp.fillsize) }
  668. localsize:=fillsize;
  669. while (localsize>0) do
  670. begin
  671. {$ifndef i8086}
  672. if CPUX86_HAS_CMOV in cpu_capabilities[current_settings.cputype] then
  673. begin
  674. for j:=low(alignarray_cmovcpus) to high(alignarray_cmovcpus) do
  675. if (localsize>=length(alignarray_cmovcpus[j])) then
  676. break;
  677. move(alignarray_cmovcpus[j][1],bufptr^,length(alignarray_cmovcpus[j]));
  678. inc(bufptr,length(alignarray_cmovcpus[j]));
  679. dec(localsize,length(alignarray_cmovcpus[j]));
  680. end
  681. else
  682. {$endif not i8086}
  683. begin
  684. for j:=low(alignarray) to high(alignarray) do
  685. if (localsize>=length(alignarray[j])) then
  686. break;
  687. move(alignarray[j][1],bufptr^,length(alignarray[j]));
  688. inc(bufptr,length(alignarray[j]));
  689. dec(localsize,length(alignarray[j]));
  690. end
  691. end;
  692. end;
  693. calculatefillbuf:=pchar(@buf);
  694. end;
  695. {*****************************************************************************
  696. Taicpu Constructors
  697. *****************************************************************************}
  698. procedure taicpu.changeopsize(siz:topsize);
  699. begin
  700. opsize:=siz;
  701. end;
  702. procedure taicpu.init(_size : topsize);
  703. begin
  704. { default order is att }
  705. FOperandOrder:=op_att;
  706. segprefix:=NR_NO;
  707. opsize:=_size;
  708. insentry:=nil;
  709. LastInsOffset:=-1;
  710. InsOffset:=0;
  711. InsSize:=0;
  712. end;
  713. constructor taicpu.op_none(op : tasmop);
  714. begin
  715. inherited create(op);
  716. init(S_NO);
  717. end;
  718. constructor taicpu.op_none(op : tasmop;_size : topsize);
  719. begin
  720. inherited create(op);
  721. init(_size);
  722. end;
  723. constructor taicpu.op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  724. begin
  725. inherited create(op);
  726. init(_size);
  727. ops:=1;
  728. loadreg(0,_op1);
  729. end;
  730. constructor taicpu.op_const(op : tasmop;_size : topsize;_op1 : aint);
  731. begin
  732. inherited create(op);
  733. init(_size);
  734. ops:=1;
  735. loadconst(0,_op1);
  736. end;
  737. constructor taicpu.op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  738. begin
  739. inherited create(op);
  740. init(_size);
  741. ops:=1;
  742. loadref(0,_op1);
  743. end;
  744. constructor taicpu.op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  745. begin
  746. inherited create(op);
  747. init(_size);
  748. ops:=2;
  749. loadreg(0,_op1);
  750. loadreg(1,_op2);
  751. end;
  752. constructor taicpu.op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aint);
  753. begin
  754. inherited create(op);
  755. init(_size);
  756. ops:=2;
  757. loadreg(0,_op1);
  758. loadconst(1,_op2);
  759. end;
  760. constructor taicpu.op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  761. begin
  762. inherited create(op);
  763. init(_size);
  764. ops:=2;
  765. loadreg(0,_op1);
  766. loadref(1,_op2);
  767. end;
  768. constructor taicpu.op_const_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister);
  769. begin
  770. inherited create(op);
  771. init(_size);
  772. ops:=2;
  773. loadconst(0,_op1);
  774. loadreg(1,_op2);
  775. end;
  776. constructor taicpu.op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aint);
  777. begin
  778. inherited create(op);
  779. init(_size);
  780. ops:=2;
  781. loadconst(0,_op1);
  782. loadconst(1,_op2);
  783. end;
  784. constructor taicpu.op_const_ref(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference);
  785. begin
  786. inherited create(op);
  787. init(_size);
  788. ops:=2;
  789. loadconst(0,_op1);
  790. loadref(1,_op2);
  791. end;
  792. constructor taicpu.op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  793. begin
  794. inherited create(op);
  795. init(_size);
  796. ops:=2;
  797. loadref(0,_op1);
  798. loadreg(1,_op2);
  799. end;
  800. constructor taicpu.op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  801. begin
  802. inherited create(op);
  803. init(_size);
  804. ops:=3;
  805. loadreg(0,_op1);
  806. loadreg(1,_op2);
  807. loadreg(2,_op3);
  808. end;
  809. constructor taicpu.op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;_op3 : tregister);
  810. begin
  811. inherited create(op);
  812. init(_size);
  813. ops:=3;
  814. loadconst(0,_op1);
  815. loadreg(1,_op2);
  816. loadreg(2,_op3);
  817. end;
  818. constructor taicpu.op_ref_reg_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2,_op3 : tregister);
  819. begin
  820. inherited create(op);
  821. init(_size);
  822. ops:=3;
  823. loadref(0,_op1);
  824. loadreg(1,_op2);
  825. loadreg(2,_op3);
  826. end;
  827. constructor taicpu.op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference;_op3 : tregister);
  828. begin
  829. inherited create(op);
  830. init(_size);
  831. ops:=3;
  832. loadconst(0,_op1);
  833. loadref(1,_op2);
  834. loadreg(2,_op3);
  835. end;
  836. constructor taicpu.op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;const _op3 : treference);
  837. begin
  838. inherited create(op);
  839. init(_size);
  840. ops:=3;
  841. loadconst(0,_op1);
  842. loadreg(1,_op2);
  843. loadref(2,_op3);
  844. end;
  845. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  846. begin
  847. inherited create(op);
  848. init(_size);
  849. condition:=cond;
  850. ops:=1;
  851. loadsymbol(0,_op1,0);
  852. end;
  853. constructor taicpu.op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  854. begin
  855. inherited create(op);
  856. init(_size);
  857. ops:=1;
  858. loadsymbol(0,_op1,0);
  859. end;
  860. constructor taicpu.op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  861. begin
  862. inherited create(op);
  863. init(_size);
  864. ops:=1;
  865. loadsymbol(0,_op1,_op1ofs);
  866. end;
  867. constructor taicpu.op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  868. begin
  869. inherited create(op);
  870. init(_size);
  871. ops:=2;
  872. loadsymbol(0,_op1,_op1ofs);
  873. loadreg(1,_op2);
  874. end;
  875. constructor taicpu.op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  876. begin
  877. inherited create(op);
  878. init(_size);
  879. ops:=2;
  880. loadsymbol(0,_op1,_op1ofs);
  881. loadref(1,_op2);
  882. end;
  883. function taicpu.GetString:string;
  884. var
  885. i : longint;
  886. s : string;
  887. addsize : boolean;
  888. begin
  889. s:='['+std_op2str[opcode];
  890. for i:=0 to ops-1 do
  891. begin
  892. with oper[i]^ do
  893. begin
  894. if i=0 then
  895. s:=s+' '
  896. else
  897. s:=s+',';
  898. { type }
  899. addsize:=false;
  900. if (ot and OT_XMMREG)=OT_XMMREG then
  901. s:=s+'xmmreg'
  902. else
  903. if (ot and OT_YMMREG)=OT_YMMREG then
  904. s:=s+'ymmreg'
  905. else
  906. if (ot and OT_MMXREG)=OT_MMXREG then
  907. s:=s+'mmxreg'
  908. else
  909. if (ot and OT_FPUREG)=OT_FPUREG then
  910. s:=s+'fpureg'
  911. else
  912. if (ot and OT_REGISTER)=OT_REGISTER then
  913. begin
  914. s:=s+'reg';
  915. addsize:=true;
  916. end
  917. else
  918. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  919. begin
  920. s:=s+'imm';
  921. addsize:=true;
  922. end
  923. else
  924. if (ot and OT_MEMORY)=OT_MEMORY then
  925. begin
  926. s:=s+'mem';
  927. addsize:=true;
  928. end
  929. else
  930. s:=s+'???';
  931. { size }
  932. if addsize then
  933. begin
  934. if (ot and OT_BITS8)<>0 then
  935. s:=s+'8'
  936. else
  937. if (ot and OT_BITS16)<>0 then
  938. s:=s+'16'
  939. else
  940. if (ot and OT_BITS32)<>0 then
  941. s:=s+'32'
  942. else
  943. if (ot and OT_BITS64)<>0 then
  944. s:=s+'64'
  945. else
  946. if (ot and OT_BITS128)<>0 then
  947. s:=s+'128'
  948. else
  949. if (ot and OT_BITS256)<>0 then
  950. s:=s+'256'
  951. else
  952. s:=s+'??';
  953. { signed }
  954. if (ot and OT_SIGNED)<>0 then
  955. s:=s+'s';
  956. end;
  957. end;
  958. end;
  959. GetString:=s+']';
  960. end;
  961. procedure taicpu.Swapoperands;
  962. var
  963. p : POper;
  964. begin
  965. { Fix the operands which are in AT&T style and we need them in Intel style }
  966. case ops of
  967. 0,1:
  968. ;
  969. 2 : begin
  970. { 0,1 -> 1,0 }
  971. p:=oper[0];
  972. oper[0]:=oper[1];
  973. oper[1]:=p;
  974. end;
  975. 3 : begin
  976. { 0,1,2 -> 2,1,0 }
  977. p:=oper[0];
  978. oper[0]:=oper[2];
  979. oper[2]:=p;
  980. end;
  981. 4 : begin
  982. { 0,1,2,3 -> 3,2,1,0 }
  983. p:=oper[0];
  984. oper[0]:=oper[3];
  985. oper[3]:=p;
  986. p:=oper[1];
  987. oper[1]:=oper[2];
  988. oper[2]:=p;
  989. end;
  990. else
  991. internalerror(201108141);
  992. end;
  993. end;
  994. procedure taicpu.SetOperandOrder(order:TOperandOrder);
  995. begin
  996. if FOperandOrder<>order then
  997. begin
  998. Swapoperands;
  999. FOperandOrder:=order;
  1000. end;
  1001. end;
  1002. function taicpu.FixNonCommutativeOpcodes: tasmop;
  1003. begin
  1004. result:=opcode;
  1005. { we need ATT order }
  1006. SetOperandOrder(op_att);
  1007. if (
  1008. (ops=2) and
  1009. (oper[0]^.typ=top_reg) and
  1010. (oper[1]^.typ=top_reg) and
  1011. { if the first is ST and the second is also a register
  1012. it is necessarily ST1 .. ST7 }
  1013. ((oper[0]^.reg=NR_ST) or
  1014. (oper[0]^.reg=NR_ST0))
  1015. ) or
  1016. { ((ops=1) and
  1017. (oper[0]^.typ=top_reg) and
  1018. (oper[0]^.reg in [R_ST1..R_ST7])) or}
  1019. (ops=0) then
  1020. begin
  1021. if opcode=A_FSUBR then
  1022. result:=A_FSUB
  1023. else if opcode=A_FSUB then
  1024. result:=A_FSUBR
  1025. else if opcode=A_FDIVR then
  1026. result:=A_FDIV
  1027. else if opcode=A_FDIV then
  1028. result:=A_FDIVR
  1029. else if opcode=A_FSUBRP then
  1030. result:=A_FSUBP
  1031. else if opcode=A_FSUBP then
  1032. result:=A_FSUBRP
  1033. else if opcode=A_FDIVRP then
  1034. result:=A_FDIVP
  1035. else if opcode=A_FDIVP then
  1036. result:=A_FDIVRP;
  1037. end;
  1038. if (
  1039. (ops=1) and
  1040. (oper[0]^.typ=top_reg) and
  1041. (getregtype(oper[0]^.reg)=R_FPUREGISTER) and
  1042. (oper[0]^.reg<>NR_ST)
  1043. ) then
  1044. begin
  1045. if opcode=A_FSUBRP then
  1046. result:=A_FSUBP
  1047. else if opcode=A_FSUBP then
  1048. result:=A_FSUBRP
  1049. else if opcode=A_FDIVRP then
  1050. result:=A_FDIVP
  1051. else if opcode=A_FDIVP then
  1052. result:=A_FDIVRP;
  1053. end;
  1054. end;
  1055. {*****************************************************************************
  1056. Assembler
  1057. *****************************************************************************}
  1058. type
  1059. ea = packed record
  1060. sib_present : boolean;
  1061. bytes : byte;
  1062. size : byte;
  1063. modrm : byte;
  1064. sib : byte;
  1065. {$ifdef x86_64}
  1066. rex : byte;
  1067. {$endif x86_64}
  1068. end;
  1069. procedure taicpu.create_ot(objdata:TObjData);
  1070. {
  1071. this function will also fix some other fields which only needs to be once
  1072. }
  1073. var
  1074. i,l,relsize : longint;
  1075. currsym : TObjSymbol;
  1076. begin
  1077. if ops=0 then
  1078. exit;
  1079. { update oper[].ot field }
  1080. for i:=0 to ops-1 do
  1081. with oper[i]^ do
  1082. begin
  1083. case typ of
  1084. top_reg :
  1085. begin
  1086. ot:=reg_ot_table[findreg_by_number(reg)];
  1087. end;
  1088. top_ref :
  1089. begin
  1090. if (ref^.refaddr=addr_no)
  1091. {$ifdef i386}
  1092. or (
  1093. (ref^.refaddr in [addr_pic]) and
  1094. (ref^.base<>NR_NO)
  1095. )
  1096. {$endif i386}
  1097. {$ifdef x86_64}
  1098. or (
  1099. (ref^.refaddr in [addr_pic,addr_pic_no_got]) and
  1100. (ref^.base<>NR_NO)
  1101. )
  1102. {$endif x86_64}
  1103. then
  1104. begin
  1105. { create ot field }
  1106. if (reg_ot_table[findreg_by_number(ref^.base)] and OT_REG_GPR = OT_REG_GPR) and
  1107. ((reg_ot_table[findreg_by_number(ref^.index)] = OT_XMMREG) or
  1108. (reg_ot_table[findreg_by_number(ref^.index)] = OT_YMMREG)
  1109. ) then
  1110. // AVX2 - vector-memory-referenz (e.g. vgatherdpd xmm0, [rax xmm1], xmm2)
  1111. ot := (reg_ot_table[findreg_by_number(ref^.base)] and OT_REG_GPR) or
  1112. (reg_ot_table[findreg_by_number(ref^.index)])
  1113. else if (ref^.base = NR_NO) and
  1114. ((reg_ot_table[findreg_by_number(ref^.index)] = OT_XMMREG) or
  1115. (reg_ot_table[findreg_by_number(ref^.index)] = OT_YMMREG)
  1116. ) then
  1117. // AVX2 - vector-memory-referenz without base-register (e.g. vgatherdpd xmm0, [xmm1], xmm2)
  1118. ot := (OT_REG_GPR) or
  1119. (reg_ot_table[findreg_by_number(ref^.index)])
  1120. else if (ot and OT_SIZE_MASK)=0 then
  1121. ot:=OT_MEMORY_ANY or opsize_2_type[i,opsize]
  1122. else
  1123. ot:=OT_MEMORY_ANY or (ot and OT_SIZE_MASK);
  1124. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  1125. ot:=ot or OT_MEM_OFFS;
  1126. { fix scalefactor }
  1127. if (ref^.index=NR_NO) then
  1128. ref^.scalefactor:=0
  1129. else
  1130. if (ref^.scalefactor=0) then
  1131. ref^.scalefactor:=1;
  1132. end
  1133. else
  1134. begin
  1135. { Jumps use a relative offset which can be 8bit,
  1136. for other opcodes we always need to generate the full
  1137. 32bit address }
  1138. if assigned(objdata) and
  1139. is_jmp then
  1140. begin
  1141. currsym:=objdata.symbolref(ref^.symbol);
  1142. l:=ref^.offset;
  1143. {$push}
  1144. {$r-,q-} { disable also overflow as address returns a qword for x86_64 }
  1145. if assigned(currsym) then
  1146. inc(l,currsym.address);
  1147. {$pop}
  1148. { when it is a forward jump we need to compensate the
  1149. offset of the instruction since the previous time,
  1150. because the symbol address is then still using the
  1151. 'old-style' addressing.
  1152. For backwards jumps this is not required because the
  1153. address of the symbol is already adjusted to the
  1154. new offset }
  1155. if (l>InsOffset) and (LastInsOffset<>-1) then
  1156. inc(l,InsOffset-LastInsOffset);
  1157. { instruction size will then always become 2 (PFV) }
  1158. relsize:=(InsOffset+2)-l;
  1159. if (relsize>=-128) and (relsize<=127) and
  1160. (
  1161. not assigned(currsym) or
  1162. (currsym.objsection=objdata.currobjsec)
  1163. ) then
  1164. ot:=OT_IMM8 or OT_SHORT
  1165. else
  1166. {$ifdef i8086}
  1167. ot:=OT_IMM16 or OT_NEAR;
  1168. {$else i8086}
  1169. ot:=OT_IMM32 or OT_NEAR;
  1170. {$endif i8086}
  1171. end
  1172. else
  1173. {$ifdef i8086}
  1174. if opsize=S_FAR then
  1175. ot:=OT_IMM16 or OT_FAR
  1176. else
  1177. ot:=OT_IMM16 or OT_NEAR;
  1178. {$else i8086}
  1179. ot:=OT_IMM32 or OT_NEAR;
  1180. {$endif i8086}
  1181. end;
  1182. end;
  1183. top_local :
  1184. begin
  1185. if (ot and OT_SIZE_MASK)=0 then
  1186. ot:=OT_MEMORY or opsize_2_type[i,opsize]
  1187. else
  1188. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  1189. end;
  1190. top_const :
  1191. begin
  1192. // if opcode is a SSE or AVX-instruction then we need a
  1193. // special handling (opsize can different from const-size)
  1194. // (e.g. "pextrw reg/m16, xmmreg, imm8" =>> opsize (16 bit), const-size (8 bit)
  1195. if (InsTabMemRefSizeInfoCache^[opcode].ExistsSSEAVX) and
  1196. (not(InsTabMemRefSizeInfoCache^[opcode].ConstSize in [csiMultiple, csiUnkown])) then
  1197. begin
  1198. case InsTabMemRefSizeInfoCache^[opcode].ConstSize of
  1199. csiNoSize: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE;
  1200. csiMem8: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE or OT_BITS8;
  1201. csiMem16: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE or OT_BITS16;
  1202. csiMem32: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE or OT_BITS32;
  1203. csiMem64: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE or OT_BITS64;
  1204. end;
  1205. end
  1206. else
  1207. begin
  1208. { allow 2nd, 3rd or 4th operand being a constant and expect no size for shuf* etc. }
  1209. { further, allow AAD and AAM with imm. operand }
  1210. if (opsize=S_NO) and not((i in [1,2,3])
  1211. {$ifndef x86_64}
  1212. or ((i=0) and (opcode in [A_AAD,A_AAM]))
  1213. {$endif x86_64}
  1214. ) then
  1215. message(asmr_e_invalid_opcode_and_operand);
  1216. if
  1217. {$ifndef i8086}
  1218. (opsize<>S_W) and
  1219. {$endif not i8086}
  1220. (aint(val)>=-128) and (val<=127) then
  1221. ot:=OT_IMM8 or OT_SIGNED
  1222. else
  1223. ot:=OT_IMMEDIATE or opsize_2_type[i,opsize];
  1224. if (val=1) and (i=1) then
  1225. ot := ot or OT_ONENESS;
  1226. end;
  1227. end;
  1228. top_none :
  1229. begin
  1230. { generated when there was an error in the
  1231. assembler reader. It never happends when generating
  1232. assembler }
  1233. end;
  1234. else
  1235. internalerror(200402266);
  1236. end;
  1237. end;
  1238. end;
  1239. function taicpu.InsEnd:longint;
  1240. begin
  1241. InsEnd:=InsOffset+InsSize;
  1242. end;
  1243. function taicpu.Matches(p:PInsEntry):boolean;
  1244. { * IF_SM stands for Size Match: any operand whose size is not
  1245. * explicitly specified by the template is `really' intended to be
  1246. * the same size as the first size-specified operand.
  1247. * Non-specification is tolerated in the input instruction, but
  1248. * _wrong_ specification is not.
  1249. *
  1250. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  1251. * three-operand instructions such as SHLD: it implies that the
  1252. * first two operands must match in size, but that the third is
  1253. * required to be _unspecified_.
  1254. *
  1255. * IF_SB invokes Size Byte: operands with unspecified size in the
  1256. * template are really bytes, and so no non-byte specification in
  1257. * the input instruction will be tolerated. IF_SW similarly invokes
  1258. * Size Word, and IF_SD invokes Size Doubleword.
  1259. *
  1260. * (The default state if neither IF_SM nor IF_SM2 is specified is
  1261. * that any operand with unspecified size in the template is
  1262. * required to have unspecified size in the instruction too...)
  1263. }
  1264. var
  1265. insot,
  1266. currot,
  1267. i,j,asize,oprs : longint;
  1268. insflags:cardinal;
  1269. siz : array[0..max_operands-1] of longint;
  1270. begin
  1271. result:=false;
  1272. { Check the opcode and operands }
  1273. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  1274. exit;
  1275. {$ifdef i8086}
  1276. { On i8086, we need to skip the i386+ version of Jcc near, if the target
  1277. cpu is earlier than 386. There's another entry, later in the table for
  1278. i8086, which simulates it with i8086 instructions:
  1279. JNcc short +3
  1280. JMP near target }
  1281. if (p^.opcode=A_Jcc) and (current_settings.cputype<cpu_386) and
  1282. ((p^.flags and IF_386)<>0) then
  1283. exit;
  1284. {$endif i8086}
  1285. for i:=0 to p^.ops-1 do
  1286. begin
  1287. insot:=p^.optypes[i];
  1288. currot:=oper[i]^.ot;
  1289. { Check the operand flags }
  1290. if (insot and (not currot) and OT_NON_SIZE)<>0 then
  1291. exit;
  1292. { Check if the passed operand size matches with one of
  1293. the supported operand sizes }
  1294. if ((insot and OT_SIZE_MASK)<>0) and
  1295. ((insot and currot and OT_SIZE_MASK)<>(currot and OT_SIZE_MASK)) then
  1296. exit;
  1297. { "far" matches only with "far" }
  1298. if (insot and OT_FAR)<>(currot and OT_FAR) then
  1299. exit;
  1300. end;
  1301. { Check operand sizes }
  1302. insflags:=p^.flags;
  1303. if insflags and IF_SMASK<>0 then
  1304. begin
  1305. { as default an untyped size can get all the sizes, this is different
  1306. from nasm, but else we need to do a lot checking which opcodes want
  1307. size or not with the automatic size generation }
  1308. asize:=-1;
  1309. if (insflags and IF_SB)<>0 then
  1310. asize:=OT_BITS8
  1311. else if (insflags and IF_SW)<>0 then
  1312. asize:=OT_BITS16
  1313. else if (insflags and IF_SD)<>0 then
  1314. asize:=OT_BITS32;
  1315. if (insflags and IF_ARMASK)<>0 then
  1316. begin
  1317. siz[0]:=-1;
  1318. siz[1]:=-1;
  1319. siz[2]:=-1;
  1320. siz[((insflags and IF_ARMASK) shr IF_ARSHIFT)-1]:=asize;
  1321. end
  1322. else
  1323. begin
  1324. siz[0]:=asize;
  1325. siz[1]:=asize;
  1326. siz[2]:=asize;
  1327. end;
  1328. if (insflags and (IF_SM or IF_SM2))<>0 then
  1329. begin
  1330. if (insflags and IF_SM2)<>0 then
  1331. oprs:=2
  1332. else
  1333. oprs:=p^.ops;
  1334. for i:=0 to oprs-1 do
  1335. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  1336. begin
  1337. for j:=0 to oprs-1 do
  1338. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  1339. break;
  1340. end;
  1341. end
  1342. else
  1343. oprs:=2;
  1344. { Check operand sizes }
  1345. for i:=0 to p^.ops-1 do
  1346. begin
  1347. insot:=p^.optypes[i];
  1348. currot:=oper[i]^.ot;
  1349. if ((insot and OT_SIZE_MASK)=0) and
  1350. ((currot and OT_SIZE_MASK and (not siz[i]))<>0) and
  1351. { Immediates can always include smaller size }
  1352. ((currot and OT_IMMEDIATE)=0) and
  1353. (((insot and OT_SIZE_MASK) or siz[i])<(currot and OT_SIZE_MASK)) then
  1354. exit;
  1355. if (insot and OT_FAR)<>(currot and OT_FAR) then
  1356. exit;
  1357. end;
  1358. end;
  1359. if (InsTabMemRefSizeInfoCache^[opcode].MemRefSize in MemRefMultiples) and
  1360. (InsTabMemRefSizeInfoCache^[opcode].ExistsSSEAVX) then
  1361. begin
  1362. for i:=0 to p^.ops-1 do
  1363. begin
  1364. insot:=p^.optypes[i];
  1365. if ((insot and OT_XMMRM) = OT_XMMRM) OR
  1366. ((insot and OT_YMMRM) = OT_YMMRM) then
  1367. begin
  1368. if (insot and OT_SIZE_MASK) = 0 then
  1369. begin
  1370. case insot and (OT_XMMRM or OT_YMMRM) of
  1371. OT_XMMRM: insot := insot or OT_BITS128;
  1372. OT_YMMRM: insot := insot or OT_BITS256;
  1373. end;
  1374. end;
  1375. end;
  1376. currot:=oper[i]^.ot;
  1377. { Check the operand flags }
  1378. if (insot and (not currot) and OT_NON_SIZE)<>0 then
  1379. exit;
  1380. { Check if the passed operand size matches with one of
  1381. the supported operand sizes }
  1382. if ((insot and OT_SIZE_MASK)<>0) and
  1383. ((insot and currot and OT_SIZE_MASK)<>(currot and OT_SIZE_MASK)) then
  1384. exit;
  1385. end;
  1386. end;
  1387. result:=true;
  1388. end;
  1389. procedure taicpu.ResetPass1;
  1390. begin
  1391. { we need to reset everything here, because the choosen insentry
  1392. can be invalid for a new situation where the previously optimized
  1393. insentry is not correct }
  1394. InsEntry:=nil;
  1395. InsSize:=0;
  1396. LastInsOffset:=-1;
  1397. end;
  1398. procedure taicpu.ResetPass2;
  1399. begin
  1400. { we are here in a second pass, check if the instruction can be optimized }
  1401. if assigned(InsEntry) and
  1402. ((InsEntry^.flags and IF_PASS2)<>0) then
  1403. begin
  1404. InsEntry:=nil;
  1405. InsSize:=0;
  1406. end;
  1407. LastInsOffset:=-1;
  1408. end;
  1409. function taicpu.CheckIfValid:boolean;
  1410. begin
  1411. result:=FindInsEntry(nil);
  1412. end;
  1413. function taicpu.FindInsentry(objdata:TObjData):boolean;
  1414. var
  1415. i : longint;
  1416. begin
  1417. result:=false;
  1418. { Things which may only be done once, not when a second pass is done to
  1419. optimize }
  1420. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  1421. begin
  1422. current_filepos:=fileinfo;
  1423. { We need intel style operands }
  1424. SetOperandOrder(op_intel);
  1425. { create the .ot fields }
  1426. create_ot(objdata);
  1427. { set the file postion }
  1428. end
  1429. else
  1430. begin
  1431. { we've already an insentry so it's valid }
  1432. result:=true;
  1433. exit;
  1434. end;
  1435. { Lookup opcode in the table }
  1436. InsSize:=-1;
  1437. i:=instabcache^[opcode];
  1438. if i=-1 then
  1439. begin
  1440. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  1441. exit;
  1442. end;
  1443. insentry:=@instab[i];
  1444. while (insentry^.opcode=opcode) do
  1445. begin
  1446. if matches(insentry) then
  1447. begin
  1448. result:=true;
  1449. exit;
  1450. end;
  1451. inc(insentry);
  1452. end;
  1453. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1454. { No instruction found, set insentry to nil and inssize to -1 }
  1455. insentry:=nil;
  1456. inssize:=-1;
  1457. end;
  1458. function taicpu.Pass1(objdata:TObjData):longint;
  1459. begin
  1460. Pass1:=0;
  1461. { Save the old offset and set the new offset }
  1462. InsOffset:=ObjData.CurrObjSec.Size;
  1463. { Error? }
  1464. if (Insentry=nil) and (InsSize=-1) then
  1465. exit;
  1466. { set the file postion }
  1467. current_filepos:=fileinfo;
  1468. { Get InsEntry }
  1469. if FindInsEntry(ObjData) then
  1470. begin
  1471. { Calculate instruction size }
  1472. InsSize:=calcsize(insentry);
  1473. if segprefix<>NR_NO then
  1474. inc(InsSize);
  1475. { Fix opsize if size if forced }
  1476. if (insentry^.flags and (IF_SB or IF_SW or IF_SD))<>0 then
  1477. begin
  1478. if (insentry^.flags and IF_ARMASK)=0 then
  1479. begin
  1480. if (insentry^.flags and IF_SB)<>0 then
  1481. begin
  1482. if opsize=S_NO then
  1483. opsize:=S_B;
  1484. end
  1485. else if (insentry^.flags and IF_SW)<>0 then
  1486. begin
  1487. if opsize=S_NO then
  1488. opsize:=S_W;
  1489. end
  1490. else if (insentry^.flags and IF_SD)<>0 then
  1491. begin
  1492. if opsize=S_NO then
  1493. opsize:=S_L;
  1494. end;
  1495. end;
  1496. end;
  1497. LastInsOffset:=InsOffset;
  1498. Pass1:=InsSize;
  1499. exit;
  1500. end;
  1501. LastInsOffset:=-1;
  1502. end;
  1503. const
  1504. segprefixes: array[NR_ES..NR_GS] of Byte=(
  1505. // es cs ss ds fs gs
  1506. $26, $2E, $36, $3E, $64, $65
  1507. );
  1508. procedure taicpu.Pass2(objdata:TObjData);
  1509. begin
  1510. { error in pass1 ? }
  1511. if insentry=nil then
  1512. exit;
  1513. current_filepos:=fileinfo;
  1514. { Segment override }
  1515. if (segprefix>=NR_ES) and (segprefix<=NR_GS) then
  1516. begin
  1517. {$ifdef i8086}
  1518. if (objdata.CPUType<>cpu_none) and (objdata.CPUType<cpu_386) and
  1519. ((segprefix=NR_FS) or (segprefix=NR_GS)) then
  1520. Message(asmw_e_instruction_not_supported_by_cpu);
  1521. {$endif i8086}
  1522. objdata.writebytes(segprefixes[segprefix],1);
  1523. { fix the offset for GenNode }
  1524. inc(InsOffset);
  1525. end
  1526. else if segprefix<>NR_NO then
  1527. InternalError(201001071);
  1528. { Generate the instruction }
  1529. GenCode(objdata);
  1530. end;
  1531. function taicpu.needaddrprefix(opidx:byte):boolean;
  1532. begin
  1533. result:=(oper[opidx]^.typ=top_ref) and
  1534. (oper[opidx]^.ref^.refaddr=addr_no) and
  1535. {$ifdef x86_64}
  1536. (oper[opidx]^.ref^.base<>NR_RIP) and
  1537. {$endif x86_64}
  1538. (
  1539. (
  1540. (oper[opidx]^.ref^.index<>NR_NO) and
  1541. (getsubreg(oper[opidx]^.ref^.index)<>R_SUBADDR)
  1542. ) or
  1543. (
  1544. (oper[opidx]^.ref^.base<>NR_NO) and
  1545. (getsubreg(oper[opidx]^.ref^.base)<>R_SUBADDR)
  1546. )
  1547. );
  1548. end;
  1549. procedure badreg(r:Tregister);
  1550. begin
  1551. Message1(asmw_e_invalid_register,generic_regname(r));
  1552. end;
  1553. function regval(r:Tregister):byte;
  1554. const
  1555. intsupreg2opcode: array[0..7] of byte=
  1556. // ax cx dx bx si di bp sp -- in x86reg.dat
  1557. // ax cx dx bx sp bp si di -- needed order
  1558. (0, 1, 2, 3, 6, 7, 5, 4);
  1559. maxsupreg: array[tregistertype] of tsuperregister=
  1560. {$ifdef x86_64}
  1561. (0, 16, 9, 8, 16, 32, 0, 0);
  1562. {$else x86_64}
  1563. (0, 8, 9, 8, 8, 32, 0, 0);
  1564. {$endif x86_64}
  1565. var
  1566. rs: tsuperregister;
  1567. rt: tregistertype;
  1568. begin
  1569. rs:=getsupreg(r);
  1570. rt:=getregtype(r);
  1571. if (rs>=maxsupreg[rt]) then
  1572. badreg(r);
  1573. result:=rs and 7;
  1574. if (rt=R_INTREGISTER) then
  1575. begin
  1576. if (rs<8) then
  1577. result:=intsupreg2opcode[rs];
  1578. if getsubreg(r)=R_SUBH then
  1579. inc(result,4);
  1580. end;
  1581. end;
  1582. {$if defined(x86_64)}
  1583. function rexbits(r: tregister): byte;
  1584. begin
  1585. result:=0;
  1586. case getregtype(r) of
  1587. R_INTREGISTER:
  1588. if (getsupreg(r)>=RS_R8) then
  1589. { Either B,X or R bits can be set, depending on register role in instruction.
  1590. Set all three bits here, caller will discard unnecessary ones. }
  1591. result:=result or $47
  1592. else if (getsubreg(r)=R_SUBL) and
  1593. (getsupreg(r) in [RS_RDI,RS_RSI,RS_RBP,RS_RSP]) then
  1594. result:=result or $40
  1595. else if (getsubreg(r)=R_SUBH) then
  1596. { Not an actual REX bit, used to detect incompatible usage of
  1597. AH/BH/CH/DH }
  1598. result:=result or $80;
  1599. R_MMREGISTER:
  1600. if getsupreg(r)>=RS_XMM8 then
  1601. result:=result or $47;
  1602. end;
  1603. end;
  1604. function process_ea_ref(const input:toper;var output:ea;rfield:longint):boolean;
  1605. var
  1606. sym : tasmsymbol;
  1607. md,s : byte;
  1608. base,index,scalefactor,
  1609. o : longint;
  1610. ir,br : Tregister;
  1611. isub,bsub : tsubregister;
  1612. begin
  1613. result:=false;
  1614. ir:=input.ref^.index;
  1615. br:=input.ref^.base;
  1616. isub:=getsubreg(ir);
  1617. bsub:=getsubreg(br);
  1618. s:=input.ref^.scalefactor;
  1619. o:=input.ref^.offset;
  1620. sym:=input.ref^.symbol;
  1621. //if ((ir<>NR_NO) and (getregtype(ir)<>R_INTREGISTER)) or
  1622. // ((br<>NR_NO) and (br<>NR_RIP) and (getregtype(br)<>R_INTREGISTER)) then
  1623. if ((ir<>NR_NO) and (getregtype(ir)=R_MMREGISTER) and (br<>NR_NO) and (getregtype(br)<>R_INTREGISTER)) or // vector memory (AVX2)
  1624. ((ir<>NR_NO) and (getregtype(ir)<>R_INTREGISTER) and (getregtype(ir)<>R_MMREGISTER)) or
  1625. ((br<>NR_NO) and (br<>NR_RIP) and (getregtype(br)<>R_INTREGISTER)) then
  1626. internalerror(200301081);
  1627. { it's direct address }
  1628. if (br=NR_NO) and (ir=NR_NO) then
  1629. begin
  1630. output.sib_present:=true;
  1631. output.bytes:=4;
  1632. output.modrm:=4 or (rfield shl 3);
  1633. output.sib:=$25;
  1634. end
  1635. else if (br=NR_RIP) and (ir=NR_NO) then
  1636. begin
  1637. { rip based }
  1638. output.sib_present:=false;
  1639. output.bytes:=4;
  1640. output.modrm:=5 or (rfield shl 3);
  1641. end
  1642. else
  1643. { it's an indirection }
  1644. begin
  1645. { 16 bit? }
  1646. if ((ir<>NR_NO) and (isub in [R_SUBMMX,R_SUBMMY]) and
  1647. (br<>NR_NO) and (bsub=R_SUBADDR)
  1648. ) then
  1649. begin
  1650. // vector memory (AVX2) =>> ignore
  1651. end
  1652. else if ((ir<>NR_NO) and (isub<>R_SUBADDR) and (isub<>R_SUBD)) or
  1653. ((br<>NR_NO) and (bsub<>R_SUBADDR) and (bsub<>R_SUBD)) then
  1654. begin
  1655. message(asmw_e_16bit_32bit_not_supported);
  1656. end;
  1657. { wrong, for various reasons }
  1658. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  1659. exit;
  1660. output.rex:=output.rex or (rexbits(br) and $F1) or (rexbits(ir) and $F2);
  1661. result:=true;
  1662. { base }
  1663. case br of
  1664. NR_R8D,
  1665. NR_EAX,
  1666. NR_R8,
  1667. NR_RAX : base:=0;
  1668. NR_R9D,
  1669. NR_ECX,
  1670. NR_R9,
  1671. NR_RCX : base:=1;
  1672. NR_R10D,
  1673. NR_EDX,
  1674. NR_R10,
  1675. NR_RDX : base:=2;
  1676. NR_R11D,
  1677. NR_EBX,
  1678. NR_R11,
  1679. NR_RBX : base:=3;
  1680. NR_R12D,
  1681. NR_ESP,
  1682. NR_R12,
  1683. NR_RSP : base:=4;
  1684. NR_R13D,
  1685. NR_EBP,
  1686. NR_R13,
  1687. NR_NO,
  1688. NR_RBP : base:=5;
  1689. NR_R14D,
  1690. NR_ESI,
  1691. NR_R14,
  1692. NR_RSI : base:=6;
  1693. NR_R15D,
  1694. NR_EDI,
  1695. NR_R15,
  1696. NR_RDI : base:=7;
  1697. else
  1698. exit;
  1699. end;
  1700. { index }
  1701. case ir of
  1702. NR_R8D,
  1703. NR_EAX,
  1704. NR_R8,
  1705. NR_RAX,
  1706. NR_XMM0,
  1707. NR_XMM8,
  1708. NR_YMM0,
  1709. NR_YMM8 : index:=0;
  1710. NR_R9D,
  1711. NR_ECX,
  1712. NR_R9,
  1713. NR_RCX,
  1714. NR_XMM1,
  1715. NR_XMM9,
  1716. NR_YMM1,
  1717. NR_YMM9 : index:=1;
  1718. NR_R10D,
  1719. NR_EDX,
  1720. NR_R10,
  1721. NR_RDX,
  1722. NR_XMM2,
  1723. NR_XMM10,
  1724. NR_YMM2,
  1725. NR_YMM10 : index:=2;
  1726. NR_R11D,
  1727. NR_EBX,
  1728. NR_R11,
  1729. NR_RBX,
  1730. NR_XMM3,
  1731. NR_XMM11,
  1732. NR_YMM3,
  1733. NR_YMM11 : index:=3;
  1734. NR_R12D,
  1735. NR_ESP,
  1736. NR_R12,
  1737. NR_NO,
  1738. NR_XMM4,
  1739. NR_XMM12,
  1740. NR_YMM4,
  1741. NR_YMM12 : index:=4;
  1742. NR_R13D,
  1743. NR_EBP,
  1744. NR_R13,
  1745. NR_RBP,
  1746. NR_XMM5,
  1747. NR_XMM13,
  1748. NR_YMM5,
  1749. NR_YMM13: index:=5;
  1750. NR_R14D,
  1751. NR_ESI,
  1752. NR_R14,
  1753. NR_RSI,
  1754. NR_XMM6,
  1755. NR_XMM14,
  1756. NR_YMM6,
  1757. NR_YMM14: index:=6;
  1758. NR_R15D,
  1759. NR_EDI,
  1760. NR_R15,
  1761. NR_RDI,
  1762. NR_XMM7,
  1763. NR_XMM15,
  1764. NR_YMM7,
  1765. NR_YMM15: index:=7;
  1766. else
  1767. exit;
  1768. end;
  1769. case s of
  1770. 0,
  1771. 1 : scalefactor:=0;
  1772. 2 : scalefactor:=1;
  1773. 4 : scalefactor:=2;
  1774. 8 : scalefactor:=3;
  1775. else
  1776. exit;
  1777. end;
  1778. { If rbp or r13 is used we must always include an offset }
  1779. if (br=NR_NO) or
  1780. ((br<>NR_RBP) and (br<>NR_R13) and (br<>NR_EBP) and (br<>NR_R13D) and (o=0) and (sym=nil)) then
  1781. md:=0
  1782. else
  1783. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1784. md:=1
  1785. else
  1786. md:=2;
  1787. if (br=NR_NO) or (md=2) then
  1788. output.bytes:=4
  1789. else
  1790. output.bytes:=md;
  1791. { SIB needed ? }
  1792. if (ir=NR_NO) and (br<>NR_RSP) and (br<>NR_R12) and (br<>NR_ESP) and (br<>NR_R12D) then
  1793. begin
  1794. output.sib_present:=false;
  1795. output.modrm:=(md shl 6) or (rfield shl 3) or base;
  1796. end
  1797. else
  1798. begin
  1799. output.sib_present:=true;
  1800. output.modrm:=(md shl 6) or (rfield shl 3) or 4;
  1801. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1802. end;
  1803. end;
  1804. output.size:=1+ord(output.sib_present)+output.bytes;
  1805. result:=true;
  1806. end;
  1807. {$elseif defined(i386)}
  1808. function process_ea_ref(const input:toper;out output:ea;rfield:longint):boolean;
  1809. var
  1810. sym : tasmsymbol;
  1811. md,s : byte;
  1812. base,index,scalefactor,
  1813. o : longint;
  1814. ir,br : Tregister;
  1815. isub,bsub : tsubregister;
  1816. begin
  1817. result:=false;
  1818. if ((input.ref^.index<>NR_NO) and (getregtype(input.ref^.index)=R_MMREGISTER) and (input.ref^.base<>NR_NO) and (getregtype(input.ref^.base)<>R_INTREGISTER)) or // vector memory (AVX2)
  1819. ((input.ref^.index<>NR_NO) and (getregtype(input.ref^.index)<>R_INTREGISTER) and (getregtype(input.ref^.index)<>R_MMREGISTER)) or
  1820. ((input.ref^.base<>NR_NO) and (getregtype(input.ref^.base)<>R_INTREGISTER)) then
  1821. internalerror(200301081);
  1822. ir:=input.ref^.index;
  1823. br:=input.ref^.base;
  1824. isub:=getsubreg(ir);
  1825. bsub:=getsubreg(br);
  1826. s:=input.ref^.scalefactor;
  1827. o:=input.ref^.offset;
  1828. sym:=input.ref^.symbol;
  1829. { it's direct address }
  1830. if (br=NR_NO) and (ir=NR_NO) then
  1831. begin
  1832. { it's a pure offset }
  1833. output.sib_present:=false;
  1834. output.bytes:=4;
  1835. output.modrm:=5 or (rfield shl 3);
  1836. end
  1837. else
  1838. { it's an indirection }
  1839. begin
  1840. { 16 bit address? }
  1841. if ((ir<>NR_NO) and (isub in [R_SUBMMX,R_SUBMMY]) and
  1842. (br<>NR_NO) and (bsub=R_SUBADDR)
  1843. ) then
  1844. begin
  1845. // vector memory (AVX2) =>> ignore
  1846. end
  1847. else if ((ir<>NR_NO) and (isub<>R_SUBADDR)) or
  1848. ((br<>NR_NO) and (bsub<>R_SUBADDR)) then
  1849. message(asmw_e_16bit_not_supported);
  1850. {$ifdef OPTEA}
  1851. { make single reg base }
  1852. if (br=NR_NO) and (s=1) then
  1853. begin
  1854. br:=ir;
  1855. ir:=NR_NO;
  1856. end;
  1857. { convert [3,5,9]*EAX to EAX+[2,4,8]*EAX }
  1858. if (br=NR_NO) and
  1859. (((s=2) and (ir<>NR_ESP)) or
  1860. (s=3) or (s=5) or (s=9)) then
  1861. begin
  1862. br:=ir;
  1863. dec(s);
  1864. end;
  1865. { swap ESP into base if scalefactor is 1 }
  1866. if (s=1) and (ir=NR_ESP) then
  1867. begin
  1868. ir:=br;
  1869. br:=NR_ESP;
  1870. end;
  1871. {$endif OPTEA}
  1872. { wrong, for various reasons }
  1873. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  1874. exit;
  1875. { base }
  1876. case br of
  1877. NR_EAX : base:=0;
  1878. NR_ECX : base:=1;
  1879. NR_EDX : base:=2;
  1880. NR_EBX : base:=3;
  1881. NR_ESP : base:=4;
  1882. NR_NO,
  1883. NR_EBP : base:=5;
  1884. NR_ESI : base:=6;
  1885. NR_EDI : base:=7;
  1886. else
  1887. exit;
  1888. end;
  1889. { index }
  1890. case ir of
  1891. NR_EAX,
  1892. NR_XMM0,
  1893. NR_YMM0: index:=0;
  1894. NR_ECX,
  1895. NR_XMM1,
  1896. NR_YMM1: index:=1;
  1897. NR_EDX,
  1898. NR_XMM2,
  1899. NR_YMM2: index:=2;
  1900. NR_EBX,
  1901. NR_XMM3,
  1902. NR_YMM3: index:=3;
  1903. NR_NO,
  1904. NR_XMM4,
  1905. NR_YMM4: index:=4;
  1906. NR_EBP,
  1907. NR_XMM5,
  1908. NR_YMM5: index:=5;
  1909. NR_ESI,
  1910. NR_XMM6,
  1911. NR_YMM6: index:=6;
  1912. NR_EDI,
  1913. NR_XMM7,
  1914. NR_YMM7: index:=7;
  1915. else
  1916. exit;
  1917. end;
  1918. case s of
  1919. 0,
  1920. 1 : scalefactor:=0;
  1921. 2 : scalefactor:=1;
  1922. 4 : scalefactor:=2;
  1923. 8 : scalefactor:=3;
  1924. else
  1925. exit;
  1926. end;
  1927. if (br=NR_NO) or
  1928. ((br<>NR_EBP) and (o=0) and (sym=nil)) then
  1929. md:=0
  1930. else
  1931. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1932. md:=1
  1933. else
  1934. md:=2;
  1935. if (br=NR_NO) or (md=2) then
  1936. output.bytes:=4
  1937. else
  1938. output.bytes:=md;
  1939. { SIB needed ? }
  1940. if (ir=NR_NO) and (br<>NR_ESP) then
  1941. begin
  1942. output.sib_present:=false;
  1943. output.modrm:=(longint(md) shl 6) or (rfield shl 3) or base;
  1944. end
  1945. else
  1946. begin
  1947. output.sib_present:=true;
  1948. output.modrm:=(longint(md) shl 6) or (rfield shl 3) or 4;
  1949. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1950. end;
  1951. end;
  1952. if output.sib_present then
  1953. output.size:=2+output.bytes
  1954. else
  1955. output.size:=1+output.bytes;
  1956. result:=true;
  1957. end;
  1958. {$elseif defined(i8086)}
  1959. procedure maybe_swap_index_base(var br,ir:Tregister);
  1960. var
  1961. tmpreg: Tregister;
  1962. begin
  1963. if ((br=NR_NO) or (br=NR_SI) or (br=NR_DI)) and
  1964. ((ir=NR_NO) or (ir=NR_BP) or (ir=NR_BX)) then
  1965. begin
  1966. tmpreg:=br;
  1967. br:=ir;
  1968. ir:=tmpreg;
  1969. end;
  1970. end;
  1971. function process_ea_ref(const input:toper;out output:ea;rfield:longint):boolean;
  1972. var
  1973. sym : tasmsymbol;
  1974. md,s,rv : byte;
  1975. base,
  1976. o : longint;
  1977. ir,br : Tregister;
  1978. isub,bsub : tsubregister;
  1979. begin
  1980. result:=false;
  1981. if ((input.ref^.index<>NR_NO) and (getregtype(input.ref^.index)<>R_INTREGISTER)) or
  1982. ((input.ref^.base<>NR_NO) and (getregtype(input.ref^.base)<>R_INTREGISTER)) then
  1983. internalerror(200301081);
  1984. ir:=input.ref^.index;
  1985. br:=input.ref^.base;
  1986. isub:=getsubreg(ir);
  1987. bsub:=getsubreg(br);
  1988. s:=input.ref^.scalefactor;
  1989. o:=input.ref^.offset;
  1990. sym:=input.ref^.symbol;
  1991. { it's a direct address }
  1992. if (br=NR_NO) and (ir=NR_NO) then
  1993. begin
  1994. { it's a pure offset }
  1995. output.bytes:=2;
  1996. output.modrm:=6 or (rfield shl 3);
  1997. end
  1998. else
  1999. { it's an indirection }
  2000. begin
  2001. { 32 bit address? }
  2002. if ((ir<>NR_NO) and (isub<>R_SUBADDR)) or
  2003. ((br<>NR_NO) and (bsub<>R_SUBADDR)) then
  2004. message(asmw_e_32bit_not_supported);
  2005. { scalefactor can only be 1 in 16-bit addresses }
  2006. if (s<>1) and (ir<>NR_NO) then
  2007. exit;
  2008. maybe_swap_index_base(br,ir);
  2009. if (br=NR_BX) and (ir=NR_SI) then
  2010. base:=0
  2011. else if (br=NR_BX) and (ir=NR_DI) then
  2012. base:=1
  2013. else if (br=NR_BP) and (ir=NR_SI) then
  2014. base:=2
  2015. else if (br=NR_BP) and (ir=NR_DI) then
  2016. base:=3
  2017. else if (br=NR_NO) and (ir=NR_SI) then
  2018. base:=4
  2019. else if (br=NR_NO) and (ir=NR_DI) then
  2020. base:=5
  2021. else if (br=NR_BP) and (ir=NR_NO) then
  2022. base:=6
  2023. else if (br=NR_BX) and (ir=NR_NO) then
  2024. base:=7
  2025. else
  2026. exit;
  2027. if (base<>6) and (o=0) and (sym=nil) then
  2028. md:=0
  2029. else if ((o>=-128) and (o<=127) and (sym=nil)) then
  2030. md:=1
  2031. else
  2032. md:=2;
  2033. output.bytes:=md;
  2034. output.modrm:=(longint(md) shl 6) or (rfield shl 3) or base;
  2035. end;
  2036. output.size:=1+output.bytes;
  2037. output.sib_present:=false;
  2038. result:=true;
  2039. end;
  2040. {$endif}
  2041. function process_ea(const input:toper;out output:ea;rfield:longint):boolean;
  2042. var
  2043. rv : byte;
  2044. begin
  2045. result:=false;
  2046. fillchar(output,sizeof(output),0);
  2047. {Register ?}
  2048. if (input.typ=top_reg) then
  2049. begin
  2050. rv:=regval(input.reg);
  2051. output.modrm:=$c0 or (rfield shl 3) or rv;
  2052. output.size:=1;
  2053. {$ifdef x86_64}
  2054. output.rex:=output.rex or (rexbits(input.reg) and $F1);
  2055. {$endif x86_64}
  2056. result:=true;
  2057. exit;
  2058. end;
  2059. {No register, so memory reference.}
  2060. if input.typ<>top_ref then
  2061. internalerror(200409263);
  2062. result:=process_ea_ref(input,output,rfield);
  2063. end;
  2064. function taicpu.calcsize(p:PInsEntry):shortint;
  2065. var
  2066. codes : pchar;
  2067. c : byte;
  2068. len : shortint;
  2069. ea_data : ea;
  2070. exists_vex: boolean;
  2071. exists_vex_extension: boolean;
  2072. exists_prefix_66: boolean;
  2073. exists_prefix_F2: boolean;
  2074. exists_prefix_F3: boolean;
  2075. {$ifdef x86_64}
  2076. omit_rexw : boolean;
  2077. {$endif x86_64}
  2078. begin
  2079. len:=0;
  2080. codes:=@p^.code[0];
  2081. exists_vex := false;
  2082. exists_vex_extension := false;
  2083. exists_prefix_66 := false;
  2084. exists_prefix_F2 := false;
  2085. exists_prefix_F3 := false;
  2086. {$ifdef x86_64}
  2087. rex:=0;
  2088. omit_rexw:=false;
  2089. {$endif x86_64}
  2090. repeat
  2091. c:=ord(codes^);
  2092. inc(codes);
  2093. case c of
  2094. &0 :
  2095. break;
  2096. &1,&2,&3 :
  2097. begin
  2098. inc(codes,c);
  2099. inc(len,c);
  2100. end;
  2101. &10,&11,&12 :
  2102. begin
  2103. {$ifdef x86_64}
  2104. rex:=rex or (rexbits(oper[c-&10]^.reg) and $F1);
  2105. {$endif x86_64}
  2106. inc(codes);
  2107. inc(len);
  2108. end;
  2109. &13,&23 :
  2110. begin
  2111. inc(codes);
  2112. inc(len);
  2113. end;
  2114. &4,&5,&6,&7 :
  2115. begin
  2116. if opsize={$ifdef i8086}S_L{$else}S_W{$endif} then
  2117. inc(len,2)
  2118. else
  2119. inc(len);
  2120. end;
  2121. &14,&15,&16,
  2122. &20,&21,&22,
  2123. &24,&25,&26,&27,
  2124. &50,&51,&52 :
  2125. inc(len);
  2126. &30,&31,&32,
  2127. &37,
  2128. &60,&61,&62 :
  2129. inc(len,2);
  2130. &34,&35,&36:
  2131. begin
  2132. {$ifdef i8086}
  2133. inc(len,2);
  2134. {$else i8086}
  2135. if opsize=S_Q then
  2136. inc(len,8)
  2137. else
  2138. inc(len,4);
  2139. {$endif i8086}
  2140. end;
  2141. &44,&45,&46:
  2142. inc(len,sizeof(pint));
  2143. &54,&55,&56:
  2144. inc(len,8);
  2145. &40,&41,&42,
  2146. &70,&71,&72,
  2147. &254,&255,&256 :
  2148. inc(len,4);
  2149. &64,&65,&66:
  2150. {$ifdef i8086}
  2151. inc(len,2);
  2152. {$else i8086}
  2153. inc(len,4);
  2154. {$endif i8086}
  2155. &74,&75,&76,&77: ; // ignore vex-coded operand-idx
  2156. &320,&321,&322 :
  2157. begin
  2158. case (oper[c-&320]^.ot and OT_SIZE_MASK) of
  2159. {$if defined(i386) or defined(x86_64)}
  2160. OT_BITS16 :
  2161. {$elseif defined(i8086)}
  2162. OT_BITS32 :
  2163. {$endif}
  2164. inc(len);
  2165. {$ifdef x86_64}
  2166. OT_BITS64:
  2167. begin
  2168. rex:=rex or $48;
  2169. end;
  2170. {$endif x86_64}
  2171. end;
  2172. end;
  2173. &310 :
  2174. {$if defined(x86_64)}
  2175. { every insentry with code 0310 must be marked with NOX86_64 }
  2176. InternalError(2011051301);
  2177. {$elseif defined(i386)}
  2178. inc(len);
  2179. {$elseif defined(i8086)}
  2180. {nothing};
  2181. {$endif}
  2182. &311 :
  2183. {$if defined(x86_64) or defined(i8086)}
  2184. inc(len)
  2185. {$endif x86_64 or i8086}
  2186. ;
  2187. &324 :
  2188. {$ifndef i8086}
  2189. inc(len)
  2190. {$endif not i8086}
  2191. ;
  2192. &326 :
  2193. begin
  2194. {$ifdef x86_64}
  2195. rex:=rex or $48;
  2196. {$endif x86_64}
  2197. end;
  2198. &312,
  2199. &323,
  2200. &327,
  2201. &331,&332: ;
  2202. &325:
  2203. {$ifdef i8086}
  2204. inc(len)
  2205. {$endif i8086}
  2206. ;
  2207. &333:
  2208. begin
  2209. inc(len);
  2210. exists_prefix_F2 := true;
  2211. end;
  2212. &334:
  2213. begin
  2214. inc(len);
  2215. exists_prefix_F3 := true;
  2216. end;
  2217. &361:
  2218. begin
  2219. {$ifndef i8086}
  2220. inc(len);
  2221. exists_prefix_66 := true;
  2222. {$endif not i8086}
  2223. end;
  2224. &335:
  2225. {$ifdef x86_64}
  2226. omit_rexw:=true
  2227. {$endif x86_64}
  2228. ;
  2229. &100..&227 :
  2230. begin
  2231. {$ifdef x86_64}
  2232. if (c<&177) then
  2233. begin
  2234. if (oper[c and 7]^.typ=top_reg) then
  2235. begin
  2236. rex:=rex or (rexbits(oper[c and 7]^.reg) and $F4);
  2237. end;
  2238. end;
  2239. {$endif x86_64}
  2240. if not process_ea(oper[(c shr 3) and 7]^, ea_data, 0) then
  2241. Message(asmw_e_invalid_effective_address)
  2242. else
  2243. inc(len,ea_data.size);
  2244. {$ifdef x86_64}
  2245. rex:=rex or ea_data.rex;
  2246. {$endif x86_64}
  2247. end;
  2248. &362: // VEX prefix for AVX (length = 2 or 3 bytes, dependens on REX.XBW or opcode-prefix ($0F38 or $0F3A))
  2249. // =>> DEFAULT = 2 Bytes
  2250. begin
  2251. if not(exists_vex) then
  2252. begin
  2253. inc(len, 2);
  2254. exists_vex := true;
  2255. end;
  2256. end;
  2257. &363: // REX.W = 1
  2258. // =>> VEX prefix length = 3
  2259. begin
  2260. if not(exists_vex_extension) then
  2261. begin
  2262. inc(len);
  2263. exists_vex_extension := true;
  2264. end;
  2265. end;
  2266. &364: ; // VEX length bit
  2267. &366, // operand 2 (ymmreg) encoded immediate byte (bit 4-7)
  2268. &367: inc(len); // operand 3 (ymmreg) encoded immediate byte (bit 4-7)
  2269. &370: // VEX-Extension prefix $0F
  2270. // ignore for calculating length
  2271. ;
  2272. &371, // VEX-Extension prefix $0F38
  2273. &372: // VEX-Extension prefix $0F3A
  2274. begin
  2275. if not(exists_vex_extension) then
  2276. begin
  2277. inc(len);
  2278. exists_vex_extension := true;
  2279. end;
  2280. end;
  2281. &300,&301,&302:
  2282. begin
  2283. {$if defined(x86_64) or defined(i8086)}
  2284. if (oper[c and 3]^.ot and OT_SIZE_MASK)=OT_BITS32 then
  2285. inc(len);
  2286. {$endif x86_64 or i8086}
  2287. end;
  2288. else
  2289. InternalError(200603141);
  2290. end;
  2291. until false;
  2292. {$ifdef x86_64}
  2293. if ((rex and $80)<>0) and ((rex and $4F)<>0) then
  2294. Message(asmw_e_bad_reg_with_rex);
  2295. rex:=rex and $4F; { reset extra bits in upper nibble }
  2296. if omit_rexw then
  2297. begin
  2298. if rex=$48 then { remove rex entirely? }
  2299. rex:=0
  2300. else
  2301. rex:=rex and $F7;
  2302. end;
  2303. if not(exists_vex) then
  2304. begin
  2305. if rex<>0 then
  2306. Inc(len);
  2307. end;
  2308. {$endif}
  2309. if exists_vex then
  2310. begin
  2311. if exists_prefix_66 then dec(len);
  2312. if exists_prefix_F2 then dec(len);
  2313. if exists_prefix_F3 then dec(len);
  2314. {$ifdef x86_64}
  2315. if not(exists_vex_extension) then
  2316. if rex and $0B <> 0 then inc(len); // REX.WXB <> 0 =>> needed VEX-Extension
  2317. {$endif x86_64}
  2318. end;
  2319. calcsize:=len;
  2320. end;
  2321. procedure taicpu.GenCode(objdata:TObjData);
  2322. {
  2323. * the actual codes (C syntax, i.e. octal):
  2324. * \0 - terminates the code. (Unless it's a literal of course.)
  2325. * \1, \2, \3 - that many literal bytes follow in the code stream
  2326. * \4, \6 - the POP/PUSH (respectively) codes for CS, DS, ES, SS
  2327. * (POP is never used for CS) depending on operand 0
  2328. * \5, \7 - the second byte of POP/PUSH codes for FS, GS, depending
  2329. * on operand 0
  2330. * \10, \11, \12 - a literal byte follows in the code stream, to be added
  2331. * to the register value of operand 0, 1 or 2
  2332. * \13 - a literal byte follows in the code stream, to be added
  2333. * to the condition code value of the instruction.
  2334. * \14, \15, \16 - a signed byte immediate operand, from operand 0, 1 or 2
  2335. * \20, \21, \22 - a byte immediate operand, from operand 0, 1 or 2
  2336. * \23 - a literal byte follows in the code stream, to be added
  2337. * to the inverted condition code value of the instruction
  2338. * (inverted version of \13).
  2339. * \24, \25, \26, \27 - an unsigned byte immediate operand, from operand 0, 1, 2 or 3
  2340. * \30, \31, \32 - a word immediate operand, from operand 0, 1 or 2
  2341. * \34, \35, \36 - select between \3[012] and \4[012] depending on 16/32 bit
  2342. * assembly mode or the address-size override on the operand
  2343. * \37 - a word constant, from the _segment_ part of operand 0
  2344. * \40, \41, \42 - a long immediate operand, from operand 0, 1 or 2
  2345. * \44, \45, \46 - select between \3[012], \4[012] or \5[456] depending
  2346. on the address size of instruction
  2347. * \50, \51, \52 - a byte relative operand, from operand 0, 1 or 2
  2348. * \54, \55, \56 - a qword immediate, from operand 0, 1 or 2
  2349. * \60, \61, \62 - a word relative operand, from operand 0, 1 or 2
  2350. * \64, \65, \66 - select between \6[012] and \7[012] depending on 16/32 bit
  2351. * assembly mode or the address-size override on the operand
  2352. * \70, \71, \72 - a long relative operand, from operand 0, 1 or 2
  2353. * \74, \75, \76 - a vex-coded vector operand, from operand 0, 1 or 2
  2354. * \1ab - a ModRM, calculated on EA in operand a, with the spare
  2355. * field the register value of operand b.
  2356. * \2ab - a ModRM, calculated on EA in operand a, with the spare
  2357. * field equal to digit b.
  2358. * \254,\255,\256 - a signed 32-bit immediate to be extended to 64 bits
  2359. * \300,\301,\302 - might be an 0x67, depending on the address size of
  2360. * the memory reference in operand x.
  2361. * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
  2362. * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
  2363. * \312 - (disassembler only) invalid with non-default address size.
  2364. * \320,\321,\322 - might be an 0x66 or 0x48 byte, depending on the operand
  2365. * size of operand x.
  2366. * \324 - indicates fixed 16-bit operand size, i.e. optional 0x66.
  2367. * \325 - indicates fixed 32-bit operand size, i.e. optional 0x66.
  2368. * \326 - indicates fixed 64-bit operand size, i.e. optional 0x48.
  2369. * \327 - indicates that this instruction is only valid when the
  2370. * operand size is the default (instruction to disassembler,
  2371. * generates no code in the assembler)
  2372. * \331 - instruction not valid with REP prefix. Hint for
  2373. * disassembler only; for SSE instructions.
  2374. * \332 - disassemble a rep (0xF3 byte) prefix as repe not rep.
  2375. * \333 - 0xF3 prefix for SSE instructions
  2376. * \334 - 0xF2 prefix for SSE instructions
  2377. * \335 - Indicates 64-bit operand size with REX.W not necessary
  2378. * \361 - 0x66 prefix for SSE instructions
  2379. * \362 - VEX prefix for AVX instructions
  2380. * \363 - VEX W1
  2381. * \364 - VEX Vector length 256
  2382. * \366 - operand 2 (ymmreg) encoded in bit 4-7 of the immediate byte
  2383. * \367 - operand 3 (ymmreg) encoded in bit 4-7 of the immediate byte
  2384. * \370 - VEX 0F-FLAG
  2385. * \371 - VEX 0F38-FLAG
  2386. * \372 - VEX 0F3A-FLAG
  2387. }
  2388. var
  2389. currval : aint;
  2390. currsym : tobjsymbol;
  2391. currrelreloc,
  2392. currabsreloc,
  2393. currabsreloc32 : TObjRelocationType;
  2394. {$ifdef x86_64}
  2395. rexwritten : boolean;
  2396. {$endif x86_64}
  2397. procedure getvalsym(opidx:longint);
  2398. begin
  2399. case oper[opidx]^.typ of
  2400. top_ref :
  2401. begin
  2402. currval:=oper[opidx]^.ref^.offset;
  2403. currsym:=ObjData.symbolref(oper[opidx]^.ref^.symbol);
  2404. {$ifdef i8086}
  2405. if oper[opidx]^.ref^.refaddr=addr_seg then
  2406. begin
  2407. currrelreloc:=RELOC_SEGREL;
  2408. currabsreloc:=RELOC_SEG;
  2409. currabsreloc32:=RELOC_SEG;
  2410. end
  2411. else if oper[opidx]^.ref^.refaddr=addr_dgroup then
  2412. begin
  2413. currrelreloc:=RELOC_DGROUPREL;
  2414. currabsreloc:=RELOC_DGROUP;
  2415. currabsreloc32:=RELOC_DGROUP;
  2416. end
  2417. else if oper[opidx]^.ref^.refaddr=addr_fardataseg then
  2418. begin
  2419. currrelreloc:=RELOC_FARDATASEGREL;
  2420. currabsreloc:=RELOC_FARDATASEG;
  2421. currabsreloc32:=RELOC_FARDATASEG;
  2422. end
  2423. else
  2424. {$endif i8086}
  2425. {$ifdef i386}
  2426. if (oper[opidx]^.ref^.refaddr=addr_pic) and
  2427. (tf_pic_uses_got in target_info.flags) then
  2428. begin
  2429. currrelreloc:=RELOC_PLT32;
  2430. currabsreloc:=RELOC_GOT32;
  2431. currabsreloc32:=RELOC_GOT32;
  2432. end
  2433. else
  2434. {$endif i386}
  2435. {$ifdef x86_64}
  2436. if oper[opidx]^.ref^.refaddr=addr_pic then
  2437. begin
  2438. currrelreloc:=RELOC_PLT32;
  2439. currabsreloc:=RELOC_GOTPCREL;
  2440. currabsreloc32:=RELOC_GOTPCREL;
  2441. end
  2442. else if oper[opidx]^.ref^.refaddr=addr_pic_no_got then
  2443. begin
  2444. currrelreloc:=RELOC_RELATIVE;
  2445. currabsreloc:=RELOC_RELATIVE;
  2446. currabsreloc32:=RELOC_RELATIVE;
  2447. end
  2448. else
  2449. {$endif x86_64}
  2450. begin
  2451. currrelreloc:=RELOC_RELATIVE;
  2452. currabsreloc:=RELOC_ABSOLUTE;
  2453. currabsreloc32:=RELOC_ABSOLUTE32;
  2454. end;
  2455. end;
  2456. top_const :
  2457. begin
  2458. currval:=aint(oper[opidx]^.val);
  2459. currsym:=nil;
  2460. currabsreloc:=RELOC_ABSOLUTE;
  2461. currabsreloc32:=RELOC_ABSOLUTE32;
  2462. end;
  2463. else
  2464. Message(asmw_e_immediate_or_reference_expected);
  2465. end;
  2466. end;
  2467. {$ifdef x86_64}
  2468. procedure maybewriterex;
  2469. begin
  2470. if (rex<>0) and not(rexwritten) then
  2471. begin
  2472. rexwritten:=true;
  2473. objdata.writebytes(rex,1);
  2474. end;
  2475. end;
  2476. {$endif x86_64}
  2477. procedure write0x66prefix;
  2478. const
  2479. b66: Byte=$66;
  2480. begin
  2481. {$ifdef i8086}
  2482. if (objdata.CPUType<>cpu_none) and (objdata.CPUType<cpu_386) then
  2483. Message(asmw_e_instruction_not_supported_by_cpu);
  2484. {$endif i8086}
  2485. objdata.writebytes(b66,1);
  2486. end;
  2487. procedure write0x67prefix;
  2488. const
  2489. b67: Byte=$67;
  2490. begin
  2491. {$ifdef i8086}
  2492. if (objdata.CPUType<>cpu_none) and (objdata.CPUType<cpu_386) then
  2493. Message(asmw_e_instruction_not_supported_by_cpu);
  2494. {$endif i8086}
  2495. objdata.writebytes(b67,1);
  2496. end;
  2497. procedure objdata_writereloc(Data:TRelocDataInt;len:aword;p:TObjSymbol;Reloctype:TObjRelocationType);
  2498. begin
  2499. {$ifdef i386}
  2500. { Special case of '_GLOBAL_OFFSET_TABLE_'
  2501. which needs a special relocation type R_386_GOTPC }
  2502. if assigned (p) and
  2503. (p.name='_GLOBAL_OFFSET_TABLE_') and
  2504. (tf_pic_uses_got in target_info.flags) then
  2505. begin
  2506. { nothing else than a 4 byte relocation should occur
  2507. for GOT }
  2508. if len<>4 then
  2509. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  2510. Reloctype:=RELOC_GOTPC;
  2511. { We need to add the offset of the relocation
  2512. of _GLOBAL_OFFSET_TABLE symbol within
  2513. the current instruction }
  2514. inc(data,objdata.currobjsec.size-insoffset);
  2515. end;
  2516. {$endif i386}
  2517. objdata.writereloc(data,len,p,Reloctype);
  2518. end;
  2519. const
  2520. CondVal:array[TAsmCond] of byte=($0,
  2521. $7, $3, $2, $6, $2, $4, $F, $D, $C, $E, $6, $2,
  2522. $3, $7, $3, $5, $E, $C, $D, $F, $1, $B, $9, $5,
  2523. $0, $A, $A, $B, $8, $4);
  2524. var
  2525. c : byte;
  2526. pb : pbyte;
  2527. codes : pchar;
  2528. bytes : array[0..3] of byte;
  2529. rfield,
  2530. data,s,opidx : longint;
  2531. ea_data : ea;
  2532. relsym : TObjSymbol;
  2533. needed_VEX_Extension: boolean;
  2534. needed_VEX: boolean;
  2535. opmode: integer;
  2536. VEXvvvv: byte;
  2537. VEXmmmmm: byte;
  2538. begin
  2539. { safety check }
  2540. if objdata.currobjsec.size<>longword(insoffset) then
  2541. internalerror(200130121);
  2542. { those variables are initialized inside local procedures, the dfa cannot handle this yet }
  2543. currsym:=nil;
  2544. currabsreloc:=RELOC_NONE;
  2545. currabsreloc32:=RELOC_NONE;
  2546. currrelreloc:=RELOC_NONE;
  2547. currval:=0;
  2548. { check instruction's processor level }
  2549. { todo: maybe adapt and enable this code for i386 and x86_64 as well }
  2550. {$ifdef i8086}
  2551. if objdata.CPUType<>cpu_none then
  2552. begin
  2553. case insentry^.flags and IF_PLEVEL of
  2554. IF_8086:
  2555. ;
  2556. IF_186:
  2557. if objdata.CPUType<cpu_186 then
  2558. Message(asmw_e_instruction_not_supported_by_cpu);
  2559. IF_286:
  2560. if objdata.CPUType<cpu_286 then
  2561. Message(asmw_e_instruction_not_supported_by_cpu);
  2562. IF_386:
  2563. if objdata.CPUType<cpu_386 then
  2564. Message(asmw_e_instruction_not_supported_by_cpu);
  2565. IF_486:
  2566. if objdata.CPUType<cpu_486 then
  2567. Message(asmw_e_instruction_not_supported_by_cpu);
  2568. IF_PENT:
  2569. if objdata.CPUType<cpu_Pentium then
  2570. Message(asmw_e_instruction_not_supported_by_cpu);
  2571. IF_P6:
  2572. if objdata.CPUType<cpu_Pentium2 then
  2573. Message(asmw_e_instruction_not_supported_by_cpu);
  2574. IF_KATMAI:
  2575. if objdata.CPUType<cpu_Pentium3 then
  2576. Message(asmw_e_instruction_not_supported_by_cpu);
  2577. IF_WILLAMETTE,
  2578. IF_PRESCOTT:
  2579. if objdata.CPUType<cpu_Pentium4 then
  2580. Message(asmw_e_instruction_not_supported_by_cpu);
  2581. { the NEC V20/V30 extensions are incompatible with 386+, due to overlapping opcodes }
  2582. IF_NEC:
  2583. if objdata.CPUType>=cpu_386 then
  2584. Message(asmw_e_instruction_not_supported_by_cpu);
  2585. { todo: handle these properly }
  2586. IF_SANDYBRIDGE:
  2587. ;
  2588. end;
  2589. end;
  2590. {$endif i8086}
  2591. { load data to write }
  2592. codes:=insentry^.code;
  2593. {$ifdef x86_64}
  2594. rexwritten:=false;
  2595. {$endif x86_64}
  2596. { Force word push/pop for registers }
  2597. if (opsize={$ifdef i8086}S_L{$else}S_W{$endif}) and ((codes[0]=#4) or (codes[0]=#6) or
  2598. ((codes[0]=#1) and ((codes[2]=#5) or (codes[2]=#7)))) then
  2599. write0x66prefix;
  2600. // needed VEX Prefix (for AVX etc.)
  2601. needed_VEX := false;
  2602. needed_VEX_Extension := false;
  2603. opmode := -1;
  2604. VEXvvvv := 0;
  2605. VEXmmmmm := 0;
  2606. repeat
  2607. c:=ord(codes^);
  2608. inc(codes);
  2609. case c of
  2610. &0: break;
  2611. &1,
  2612. &2,
  2613. &3: inc(codes,c);
  2614. &74: opmode := 0;
  2615. &75: opmode := 1;
  2616. &76: opmode := 2;
  2617. &333: VEXvvvv := VEXvvvv OR $02; // set SIMD-prefix $F3
  2618. &334: VEXvvvv := VEXvvvv OR $03; // set SIMD-prefix $F2
  2619. &361: VEXvvvv := VEXvvvv OR $01; // set SIMD-prefix $66
  2620. &362: needed_VEX := true;
  2621. &363: begin
  2622. needed_VEX_Extension := true;
  2623. VEXvvvv := VEXvvvv OR (1 shl 7); // set REX.W
  2624. end;
  2625. &364: VEXvvvv := VEXvvvv OR $04; // vectorlength = 256 bits AND no scalar
  2626. &370: VEXmmmmm := VEXmmmmm OR $01; // set leading opcode byte $0F
  2627. &371: begin
  2628. needed_VEX_Extension := true;
  2629. VEXmmmmm := VEXmmmmm OR $02; // set leading opcode byte $0F38
  2630. end;
  2631. &372: begin
  2632. needed_VEX_Extension := true;
  2633. VEXmmmmm := VEXmmmmm OR $03; // set leading opcode byte $0F3A
  2634. end;
  2635. end;
  2636. until false;
  2637. if needed_VEX then
  2638. begin
  2639. if (opmode > ops) or
  2640. (opmode < -1) then
  2641. begin
  2642. Internalerror(777100);
  2643. end
  2644. else if opmode = -1 then
  2645. begin
  2646. VEXvvvv := VEXvvvv or ($0F shl 3); // set VEXvvvv bits (bits 6-3) to 1
  2647. end
  2648. else if oper[opmode]^.typ = top_reg then
  2649. begin
  2650. VEXvvvv := VEXvvvv or ((not(regval(oper[opmode]^.reg)) and $07) shl 3);
  2651. {$ifdef x86_64}
  2652. if rexbits(oper[opmode]^.reg) = 0 then VEXvvvv := VEXvvvv or (1 shl 6);
  2653. {$else}
  2654. VEXvvvv := VEXvvvv or (1 shl 6);
  2655. {$endif x86_64}
  2656. end
  2657. else Internalerror(777101);
  2658. if not(needed_VEX_Extension) then
  2659. begin
  2660. {$ifdef x86_64}
  2661. if rex and $0B <> 0 then needed_VEX_Extension := true;
  2662. {$endif x86_64}
  2663. end;
  2664. if needed_VEX_Extension then
  2665. begin
  2666. // VEX-Prefix-Length = 3 Bytes
  2667. bytes[0]:=$C4;
  2668. objdata.writebytes(bytes,1);
  2669. {$ifdef x86_64}
  2670. VEXmmmmm := VEXmmmmm or ((not(rex) and $07) shl 5); // set REX.rxb
  2671. {$else}
  2672. VEXmmmmm := VEXmmmmm or (7 shl 5); //
  2673. {$endif x86_64}
  2674. bytes[0] := VEXmmmmm;
  2675. objdata.writebytes(bytes,1);
  2676. {$ifdef x86_64}
  2677. VEXvvvv := VEXvvvv OR ((rex and $08) shl 7); // set REX.w
  2678. {$endif x86_64}
  2679. bytes[0] := VEXvvvv;
  2680. objdata.writebytes(bytes,1);
  2681. end
  2682. else
  2683. begin
  2684. // VEX-Prefix-Length = 2 Bytes
  2685. bytes[0]:=$C5;
  2686. objdata.writebytes(bytes,1);
  2687. {$ifdef x86_64}
  2688. if rex and $04 = 0 then
  2689. {$endif x86_64}
  2690. begin
  2691. VEXvvvv := VEXvvvv or (1 shl 7);
  2692. end;
  2693. bytes[0] := VEXvvvv;
  2694. objdata.writebytes(bytes,1);
  2695. end;
  2696. end
  2697. else
  2698. begin
  2699. needed_VEX_Extension := false;
  2700. opmode := -1;
  2701. end;
  2702. { load data to write }
  2703. codes:=insentry^.code;
  2704. repeat
  2705. c:=ord(codes^);
  2706. inc(codes);
  2707. case c of
  2708. &0 :
  2709. break;
  2710. &1,&2,&3 :
  2711. begin
  2712. {$ifdef x86_64}
  2713. if not(needed_VEX) then // TG
  2714. maybewriterex;
  2715. {$endif x86_64}
  2716. objdata.writebytes(codes^,c);
  2717. inc(codes,c);
  2718. end;
  2719. &4,&6 :
  2720. begin
  2721. case oper[0]^.reg of
  2722. NR_CS:
  2723. bytes[0]:=$e;
  2724. NR_NO,
  2725. NR_DS:
  2726. bytes[0]:=$1e;
  2727. NR_ES:
  2728. bytes[0]:=$6;
  2729. NR_SS:
  2730. bytes[0]:=$16;
  2731. else
  2732. internalerror(777004);
  2733. end;
  2734. if c=&4 then
  2735. inc(bytes[0]);
  2736. objdata.writebytes(bytes,1);
  2737. end;
  2738. &5,&7 :
  2739. begin
  2740. case oper[0]^.reg of
  2741. NR_FS:
  2742. bytes[0]:=$a0;
  2743. NR_GS:
  2744. bytes[0]:=$a8;
  2745. else
  2746. internalerror(777005);
  2747. end;
  2748. if c=&5 then
  2749. inc(bytes[0]);
  2750. objdata.writebytes(bytes,1);
  2751. end;
  2752. &10,&11,&12 :
  2753. begin
  2754. {$ifdef x86_64}
  2755. if not(needed_VEX) then // TG
  2756. maybewriterex;
  2757. {$endif x86_64}
  2758. bytes[0]:=ord(codes^)+regval(oper[c-&10]^.reg);
  2759. inc(codes);
  2760. objdata.writebytes(bytes,1);
  2761. end;
  2762. &13 :
  2763. begin
  2764. bytes[0]:=ord(codes^)+condval[condition];
  2765. inc(codes);
  2766. objdata.writebytes(bytes,1);
  2767. end;
  2768. &14,&15,&16 :
  2769. begin
  2770. getvalsym(c-&14);
  2771. if (currval<-128) or (currval>127) then
  2772. Message2(asmw_e_value_exceeds_bounds,'signed byte',tostr(currval));
  2773. if assigned(currsym) then
  2774. objdata_writereloc(currval,1,currsym,currabsreloc)
  2775. else
  2776. objdata.writebytes(currval,1);
  2777. end;
  2778. &20,&21,&22 :
  2779. begin
  2780. getvalsym(c-&20);
  2781. if (currval<-256) or (currval>255) then
  2782. Message2(asmw_e_value_exceeds_bounds,'byte',tostr(currval));
  2783. if assigned(currsym) then
  2784. objdata_writereloc(currval,1,currsym,currabsreloc)
  2785. else
  2786. objdata.writebytes(currval,1);
  2787. end;
  2788. &23 :
  2789. begin
  2790. bytes[0]:=ord(codes^)+condval[inverse_cond(condition)];
  2791. inc(codes);
  2792. objdata.writebytes(bytes,1);
  2793. end;
  2794. &24,&25,&26,&27 :
  2795. begin
  2796. getvalsym(c-&24);
  2797. if (insentry^.flags and IF_IMM3)<>0 then
  2798. begin
  2799. if (currval<0) or (currval>7) then
  2800. Message2(asmw_e_value_exceeds_bounds,'unsigned triad',tostr(currval));
  2801. end
  2802. else if (insentry^.flags and IF_IMM4)<>0 then
  2803. begin
  2804. if (currval<0) or (currval>15) then
  2805. Message2(asmw_e_value_exceeds_bounds,'unsigned nibble',tostr(currval));
  2806. end
  2807. else
  2808. if (currval<0) or (currval>255) then
  2809. Message2(asmw_e_value_exceeds_bounds,'unsigned byte',tostr(currval));
  2810. if assigned(currsym) then
  2811. objdata_writereloc(currval,1,currsym,currabsreloc)
  2812. else
  2813. objdata.writebytes(currval,1);
  2814. end;
  2815. &30,&31,&32 : // 030..032
  2816. begin
  2817. getvalsym(c-&30);
  2818. {$ifndef i8086}
  2819. { currval is an aint so this cannot happen on i8086 and causes only a warning }
  2820. if (currval<-65536) or (currval>65535) then
  2821. Message2(asmw_e_value_exceeds_bounds,'word',tostr(currval));
  2822. {$endif i8086}
  2823. if assigned(currsym)
  2824. {$ifdef i8086}
  2825. or (currabsreloc in [RELOC_DGROUP,RELOC_FARDATASEG])
  2826. {$endif i8086}
  2827. then
  2828. objdata_writereloc(currval,2,currsym,currabsreloc)
  2829. else
  2830. objdata.writebytes(currval,2);
  2831. end;
  2832. &34,&35,&36 : // 034..036
  2833. { !!! These are intended (and used in opcode table) to select depending
  2834. on address size, *not* operand size. Works by coincidence only. }
  2835. begin
  2836. getvalsym(c-&34);
  2837. {$ifdef i8086}
  2838. if assigned(currsym) then
  2839. objdata_writereloc(currval,2,currsym,currabsreloc)
  2840. else
  2841. objdata.writebytes(currval,2);
  2842. {$else i8086}
  2843. if opsize=S_Q then
  2844. begin
  2845. if assigned(currsym) then
  2846. objdata_writereloc(currval,8,currsym,currabsreloc)
  2847. else
  2848. objdata.writebytes(currval,8);
  2849. end
  2850. else
  2851. begin
  2852. if assigned(currsym) then
  2853. objdata_writereloc(currval,4,currsym,currabsreloc32)
  2854. else
  2855. objdata.writebytes(currval,4);
  2856. end
  2857. {$endif i8086}
  2858. end;
  2859. &40,&41,&42 : // 040..042
  2860. begin
  2861. getvalsym(c-&40);
  2862. if assigned(currsym) then
  2863. objdata_writereloc(currval,4,currsym,currabsreloc32)
  2864. else
  2865. objdata.writebytes(currval,4);
  2866. end;
  2867. &44,&45,&46 :// 044..046 - select between word/dword/qword depending on
  2868. begin // address size (we support only default address sizes).
  2869. getvalsym(c-&44);
  2870. {$if defined(x86_64)}
  2871. if assigned(currsym) then
  2872. objdata_writereloc(currval,8,currsym,currabsreloc)
  2873. else
  2874. objdata.writebytes(currval,8);
  2875. {$elseif defined(i386)}
  2876. if assigned(currsym) then
  2877. objdata_writereloc(currval,4,currsym,currabsreloc32)
  2878. else
  2879. objdata.writebytes(currval,4);
  2880. {$elseif defined(i8086)}
  2881. if assigned(currsym) then
  2882. objdata_writereloc(currval,2,currsym,currabsreloc)
  2883. else
  2884. objdata.writebytes(currval,2);
  2885. {$endif}
  2886. end;
  2887. &50,&51,&52 : // 050..052 - byte relative operand
  2888. begin
  2889. getvalsym(c-&50);
  2890. data:=currval-insend;
  2891. {$push}
  2892. {$r-,q-} { disable also overflow as address returns a qword for x86_64 }
  2893. if assigned(currsym) then
  2894. inc(data,currsym.address);
  2895. {$pop}
  2896. if (data>127) or (data<-128) then
  2897. Message1(asmw_e_short_jmp_out_of_range,tostr(data));
  2898. objdata.writebytes(data,1);
  2899. end;
  2900. &54,&55,&56: // 054..056 - qword immediate operand
  2901. begin
  2902. getvalsym(c-&54);
  2903. if assigned(currsym) then
  2904. objdata_writereloc(currval,8,currsym,currabsreloc)
  2905. else
  2906. objdata.writebytes(currval,8);
  2907. end;
  2908. &60,&61,&62 :
  2909. begin
  2910. getvalsym(c-&60);
  2911. {$ifdef i8086}
  2912. if assigned(currsym) then
  2913. objdata_writereloc(currval,2,currsym,currrelreloc)
  2914. else
  2915. objdata_writereloc(currval-insend,2,nil,currabsreloc)
  2916. {$else i8086}
  2917. InternalError(777006);
  2918. {$endif i8086}
  2919. end;
  2920. &64,&65,&66 : // 064..066 - select between 16/32 address mode, but we support only 32 (only 16 on i8086)
  2921. begin
  2922. getvalsym(c-&64);
  2923. {$ifdef i8086}
  2924. if assigned(currsym) then
  2925. objdata_writereloc(currval,2,currsym,currrelreloc)
  2926. else
  2927. objdata_writereloc(currval-insend,2,nil,currabsreloc)
  2928. {$else i8086}
  2929. if assigned(currsym) then
  2930. objdata_writereloc(currval,4,currsym,currrelreloc)
  2931. else
  2932. objdata_writereloc(currval-insend,4,nil,currabsreloc32)
  2933. {$endif i8086}
  2934. end;
  2935. &70,&71,&72 : // 070..072 - long relative operand
  2936. begin
  2937. getvalsym(c-&70);
  2938. if assigned(currsym) then
  2939. objdata_writereloc(currval,4,currsym,currrelreloc)
  2940. else
  2941. objdata_writereloc(currval-insend,4,nil,currabsreloc32)
  2942. end;
  2943. &74,&75,&76 : ; // 074..076 - vex-coded vector operand
  2944. // ignore
  2945. &254,&255,&256 : // 0254..0256 - dword implicitly sign-extended to 64-bit (x86_64 only)
  2946. begin
  2947. getvalsym(c-&254);
  2948. {$ifdef x86_64}
  2949. { for i386 as aint type is longint the
  2950. following test is useless }
  2951. if (currval<low(longint)) or (currval>high(longint)) then
  2952. Message2(asmw_e_value_exceeds_bounds,'signed dword',tostr(currval));
  2953. {$endif x86_64}
  2954. if assigned(currsym) then
  2955. objdata_writereloc(currval,4,currsym,currabsreloc32)
  2956. else
  2957. objdata.writebytes(currval,4);
  2958. end;
  2959. &300,&301,&302:
  2960. begin
  2961. {$if defined(x86_64) or defined(i8086)}
  2962. if (oper[c and 3]^.ot and OT_SIZE_MASK)=OT_BITS32 then
  2963. write0x67prefix;
  2964. {$endif x86_64 or i8086}
  2965. end;
  2966. &310 : { fixed 16-bit addr }
  2967. {$if defined(x86_64)}
  2968. { every insentry having code 0310 must be marked with NOX86_64 }
  2969. InternalError(2011051302);
  2970. {$elseif defined(i386)}
  2971. write0x67prefix;
  2972. {$elseif defined(i8086)}
  2973. {nothing};
  2974. {$endif}
  2975. &311 : { fixed 32-bit addr }
  2976. {$if defined(x86_64) or defined(i8086)}
  2977. write0x67prefix
  2978. {$endif x86_64 or i8086}
  2979. ;
  2980. &320,&321,&322 :
  2981. begin
  2982. case oper[c-&320]^.ot and OT_SIZE_MASK of
  2983. {$if defined(i386) or defined(x86_64)}
  2984. OT_BITS16 :
  2985. {$elseif defined(i8086)}
  2986. OT_BITS32 :
  2987. {$endif}
  2988. write0x66prefix;
  2989. {$ifndef x86_64}
  2990. OT_BITS64 :
  2991. Message(asmw_e_64bit_not_supported);
  2992. {$endif x86_64}
  2993. end;
  2994. end;
  2995. &323 : {no action needed};
  2996. &325:
  2997. {$ifdef i8086}
  2998. write0x66prefix;
  2999. {$else i8086}
  3000. {no action needed};
  3001. {$endif i8086}
  3002. &324,
  3003. &361:
  3004. begin
  3005. {$ifndef i8086}
  3006. if not(needed_VEX) then
  3007. write0x66prefix;
  3008. {$endif not i8086}
  3009. end;
  3010. &326 :
  3011. begin
  3012. {$ifndef x86_64}
  3013. Message(asmw_e_64bit_not_supported);
  3014. {$endif x86_64}
  3015. end;
  3016. &333 :
  3017. begin
  3018. if not(needed_VEX) then
  3019. begin
  3020. bytes[0]:=$f3;
  3021. objdata.writebytes(bytes,1);
  3022. end;
  3023. end;
  3024. &334 :
  3025. begin
  3026. if not(needed_VEX) then
  3027. begin
  3028. bytes[0]:=$f2;
  3029. objdata.writebytes(bytes,1);
  3030. end;
  3031. end;
  3032. &335:
  3033. ;
  3034. &312,
  3035. &327,
  3036. &331,&332 :
  3037. begin
  3038. { these are dissambler hints or 32 bit prefixes which
  3039. are not needed }
  3040. end;
  3041. &362..&364: ; // VEX flags =>> nothing todo
  3042. &366: begin
  3043. if needed_VEX then
  3044. begin
  3045. if ops = 4 then
  3046. begin
  3047. if (oper[2]^.typ=top_reg) then
  3048. begin
  3049. if (oper[2]^.ot and otf_reg_xmm <> 0) or
  3050. (oper[2]^.ot and otf_reg_ymm <> 0) then
  3051. begin
  3052. bytes[0] := ((getsupreg(oper[2]^.reg) and 15) shl 4);
  3053. objdata.writebytes(bytes,1);
  3054. end
  3055. else Internalerror(2014032001);
  3056. end
  3057. else Internalerror(2014032002);
  3058. end
  3059. else Internalerror(2014032003);
  3060. end
  3061. else Internalerror(2014032004);
  3062. end;
  3063. &367: begin
  3064. if needed_VEX then
  3065. begin
  3066. if ops = 4 then
  3067. begin
  3068. if (oper[3]^.typ=top_reg) then
  3069. begin
  3070. if (oper[3]^.ot and otf_reg_xmm <> 0) or
  3071. (oper[3]^.ot and otf_reg_ymm <> 0) then
  3072. begin
  3073. bytes[0] := ((getsupreg(oper[3]^.reg) and 15) shl 4);
  3074. objdata.writebytes(bytes,1);
  3075. end
  3076. else Internalerror(2014032005);
  3077. end
  3078. else Internalerror(2014032006);
  3079. end
  3080. else Internalerror(2014032007);
  3081. end
  3082. else Internalerror(2014032008);
  3083. end;
  3084. &370..&372: ; // VEX flags =>> nothing todo
  3085. &37:
  3086. begin
  3087. {$ifdef i8086}
  3088. if assigned(currsym) then
  3089. objdata_writereloc(0,2,currsym,RELOC_SEG)
  3090. else
  3091. InternalError(2015041503);
  3092. {$else i8086}
  3093. InternalError(777006);
  3094. {$endif i8086}
  3095. end;
  3096. else
  3097. begin
  3098. { rex should be written at this point }
  3099. {$ifdef x86_64}
  3100. if not(needed_VEX) then // TG
  3101. if (rex<>0) and not(rexwritten) then
  3102. internalerror(200603191);
  3103. {$endif x86_64}
  3104. if (c>=&100) and (c<=&227) then // 0100..0227
  3105. begin
  3106. if (c<&177) then // 0177
  3107. begin
  3108. if (oper[c and 7]^.typ=top_reg) then
  3109. rfield:=regval(oper[c and 7]^.reg)
  3110. else
  3111. rfield:=regval(oper[c and 7]^.ref^.base);
  3112. end
  3113. else
  3114. rfield:=c and 7;
  3115. opidx:=(c shr 3) and 7;
  3116. if not process_ea(oper[opidx]^,ea_data,rfield) then
  3117. Message(asmw_e_invalid_effective_address);
  3118. pb:=@bytes[0];
  3119. pb^:=ea_data.modrm;
  3120. inc(pb);
  3121. if ea_data.sib_present then
  3122. begin
  3123. pb^:=ea_data.sib;
  3124. inc(pb);
  3125. end;
  3126. s:=pb-@bytes[0];
  3127. objdata.writebytes(bytes,s);
  3128. case ea_data.bytes of
  3129. 0 : ;
  3130. 1 :
  3131. begin
  3132. if (oper[opidx]^.ot and OT_MEMORY)=OT_MEMORY then
  3133. begin
  3134. currsym:=objdata.symbolref(oper[opidx]^.ref^.symbol);
  3135. {$ifdef i386}
  3136. if (oper[opidx]^.ref^.refaddr=addr_pic) and
  3137. (tf_pic_uses_got in target_info.flags) then
  3138. currabsreloc:=RELOC_GOT32
  3139. else
  3140. {$endif i386}
  3141. {$ifdef x86_64}
  3142. if oper[opidx]^.ref^.refaddr=addr_pic then
  3143. currabsreloc:=RELOC_GOTPCREL
  3144. else
  3145. {$endif x86_64}
  3146. currabsreloc:=RELOC_ABSOLUTE;
  3147. objdata_writereloc(oper[opidx]^.ref^.offset,1,currsym,currabsreloc);
  3148. end
  3149. else
  3150. begin
  3151. bytes[0]:=oper[opidx]^.ref^.offset;
  3152. objdata.writebytes(bytes,1);
  3153. end;
  3154. inc(s);
  3155. end;
  3156. 2,4 :
  3157. begin
  3158. currsym:=objdata.symbolref(oper[opidx]^.ref^.symbol);
  3159. currval:=oper[opidx]^.ref^.offset;
  3160. {$ifdef x86_64}
  3161. if oper[opidx]^.ref^.refaddr=addr_pic then
  3162. currabsreloc:=RELOC_GOTPCREL
  3163. else
  3164. if oper[opidx]^.ref^.base=NR_RIP then
  3165. begin
  3166. currabsreloc:=RELOC_RELATIVE;
  3167. { Adjust reloc value by number of bytes following the displacement,
  3168. but not if displacement is specified by literal constant }
  3169. if Assigned(currsym) then
  3170. Dec(currval,InsEnd-objdata.CurrObjSec.Size-ea_data.bytes);
  3171. end
  3172. else
  3173. {$endif x86_64}
  3174. {$ifdef i386}
  3175. if (oper[opidx]^.ref^.refaddr=addr_pic) and
  3176. (tf_pic_uses_got in target_info.flags) then
  3177. currabsreloc:=RELOC_GOT32
  3178. else
  3179. {$endif i386}
  3180. {$ifdef i8086}
  3181. if ea_data.bytes=2 then
  3182. currabsreloc:=RELOC_ABSOLUTE
  3183. else
  3184. {$endif i8086}
  3185. currabsreloc:=RELOC_ABSOLUTE32;
  3186. if (currabsreloc in [RELOC_ABSOLUTE32{$ifdef i8086},RELOC_ABSOLUTE{$endif}]) and
  3187. (Assigned(oper[opidx]^.ref^.relsymbol)) then
  3188. begin
  3189. relsym:=objdata.symbolref(oper[opidx]^.ref^.relsymbol);
  3190. if relsym.objsection=objdata.CurrObjSec then
  3191. begin
  3192. currval:=objdata.CurrObjSec.size+ea_data.bytes-relsym.offset+currval;
  3193. {$ifdef i8086}
  3194. if ea_data.bytes=4 then
  3195. currabsreloc:=RELOC_RELATIVE32
  3196. else
  3197. {$endif i8086}
  3198. currabsreloc:=RELOC_RELATIVE;
  3199. end
  3200. else
  3201. begin
  3202. currabsreloc:=RELOC_PIC_PAIR;
  3203. currval:=relsym.offset;
  3204. end;
  3205. end;
  3206. objdata_writereloc(currval,ea_data.bytes,currsym,currabsreloc);
  3207. inc(s,ea_data.bytes);
  3208. end;
  3209. end;
  3210. end
  3211. else
  3212. InternalError(777007);
  3213. end;
  3214. end;
  3215. until false;
  3216. end;
  3217. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  3218. begin
  3219. result:=(((opcode=A_MOV) or (opcode=A_XCHG)) and
  3220. (regtype = R_INTREGISTER) and
  3221. (ops=2) and
  3222. (oper[0]^.typ=top_reg) and
  3223. (oper[1]^.typ=top_reg) and
  3224. (oper[0]^.reg=oper[1]^.reg)
  3225. ) or
  3226. (((opcode=A_MOVSS) or (opcode=A_MOVSD) or (opcode=A_MOVQ) or
  3227. (opcode=A_MOVAPS) or (opcode=A_MOVAPD) or
  3228. (opcode=A_VMOVSS) or (opcode=A_VMOVSD) or (opcode=A_VMOVQ) or
  3229. (opcode=A_VMOVAPS) or (opcode=A_VMOVAPD)) and
  3230. (regtype = R_MMREGISTER) and
  3231. (ops=2) and
  3232. (oper[0]^.typ=top_reg) and
  3233. (oper[1]^.typ=top_reg) and
  3234. (oper[0]^.reg=oper[1]^.reg)
  3235. );
  3236. end;
  3237. procedure build_spilling_operation_type_table;
  3238. var
  3239. opcode : tasmop;
  3240. i : integer;
  3241. begin
  3242. new(operation_type_table);
  3243. fillchar(operation_type_table^,sizeof(toperation_type_table),byte(operand_read));
  3244. for opcode:=low(tasmop) to high(tasmop) do
  3245. begin
  3246. for i:=1 to MaxInsChanges do
  3247. begin
  3248. case InsProp[opcode].Ch[i] of
  3249. Ch_Rop1 :
  3250. operation_type_table^[opcode,0]:=operand_read;
  3251. Ch_Wop1 :
  3252. operation_type_table^[opcode,0]:=operand_write;
  3253. Ch_RWop1,
  3254. Ch_Mop1 :
  3255. operation_type_table^[opcode,0]:=operand_readwrite;
  3256. Ch_Rop2 :
  3257. operation_type_table^[opcode,1]:=operand_read;
  3258. Ch_Wop2 :
  3259. operation_type_table^[opcode,1]:=operand_write;
  3260. Ch_RWop2,
  3261. Ch_Mop2 :
  3262. operation_type_table^[opcode,1]:=operand_readwrite;
  3263. Ch_Rop3 :
  3264. operation_type_table^[opcode,2]:=operand_read;
  3265. Ch_Wop3 :
  3266. operation_type_table^[opcode,2]:=operand_write;
  3267. Ch_RWop3,
  3268. Ch_Mop3 :
  3269. operation_type_table^[opcode,2]:=operand_readwrite;
  3270. end;
  3271. end;
  3272. end;
  3273. end;
  3274. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  3275. begin
  3276. { the information in the instruction table is made for the string copy
  3277. operation MOVSD so hack here (FK)
  3278. VMOVSS and VMOVSD has two and three operand flavours, this cannot modelled by x86ins.dat
  3279. so fix it here (FK)
  3280. }
  3281. if ((opcode=A_MOVSD) or (opcode=A_VMOVSS) or (opcode=A_VMOVSD)) and (ops=2) then
  3282. begin
  3283. case opnr of
  3284. 0:
  3285. result:=operand_read;
  3286. 1:
  3287. result:=operand_write;
  3288. else
  3289. internalerror(200506055);
  3290. end
  3291. end
  3292. { IMUL has 1, 2 and 3-operand forms }
  3293. else if opcode=A_IMUL then
  3294. begin
  3295. case ops of
  3296. 1:
  3297. if opnr=0 then
  3298. result:=operand_read
  3299. else
  3300. internalerror(2014011802);
  3301. 2:
  3302. begin
  3303. case opnr of
  3304. 0:
  3305. result:=operand_read;
  3306. 1:
  3307. result:=operand_readwrite;
  3308. else
  3309. internalerror(2014011803);
  3310. end;
  3311. end;
  3312. 3:
  3313. begin
  3314. case opnr of
  3315. 0,1:
  3316. result:=operand_read;
  3317. 2:
  3318. result:=operand_write;
  3319. else
  3320. internalerror(2014011804);
  3321. end;
  3322. end;
  3323. else
  3324. internalerror(2014011805);
  3325. end;
  3326. end
  3327. else
  3328. result:=operation_type_table^[opcode,opnr];
  3329. end;
  3330. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  3331. var
  3332. tmpref: treference;
  3333. begin
  3334. tmpref:=ref;
  3335. {$ifdef i8086}
  3336. if tmpref.segment=NR_SS then
  3337. tmpref.segment:=NR_NO;
  3338. {$endif i8086}
  3339. case getregtype(r) of
  3340. R_INTREGISTER :
  3341. begin
  3342. if getsubreg(r)=R_SUBH then
  3343. inc(tmpref.offset);
  3344. { we don't need special code here for 32 bit loads on x86_64, since
  3345. those will automatically zero-extend the upper 32 bits. }
  3346. result:=taicpu.op_ref_reg(A_MOV,reg2opsize(r),tmpref,r);
  3347. end;
  3348. R_MMREGISTER :
  3349. if current_settings.fputype in fpu_avx_instructionsets then
  3350. case getsubreg(r) of
  3351. R_SUBMMD:
  3352. result:=taicpu.op_ref_reg(A_VMOVSD,reg2opsize(r),tmpref,r);
  3353. R_SUBMMS:
  3354. result:=taicpu.op_ref_reg(A_VMOVSS,reg2opsize(r),tmpref,r);
  3355. R_SUBQ,
  3356. R_SUBMMWHOLE:
  3357. result:=taicpu.op_ref_reg(A_VMOVQ,S_NO,tmpref,r);
  3358. else
  3359. internalerror(200506043);
  3360. end
  3361. else
  3362. case getsubreg(r) of
  3363. R_SUBMMD:
  3364. result:=taicpu.op_ref_reg(A_MOVSD,reg2opsize(r),tmpref,r);
  3365. R_SUBMMS:
  3366. result:=taicpu.op_ref_reg(A_MOVSS,reg2opsize(r),tmpref,r);
  3367. R_SUBQ,
  3368. R_SUBMMWHOLE:
  3369. result:=taicpu.op_ref_reg(A_MOVQ,S_NO,tmpref,r);
  3370. else
  3371. internalerror(200506043);
  3372. end;
  3373. else
  3374. internalerror(200401041);
  3375. end;
  3376. end;
  3377. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  3378. var
  3379. size: topsize;
  3380. tmpref: treference;
  3381. begin
  3382. tmpref:=ref;
  3383. {$ifdef i8086}
  3384. if tmpref.segment=NR_SS then
  3385. tmpref.segment:=NR_NO;
  3386. {$endif i8086}
  3387. case getregtype(r) of
  3388. R_INTREGISTER :
  3389. begin
  3390. if getsubreg(r)=R_SUBH then
  3391. inc(tmpref.offset);
  3392. size:=reg2opsize(r);
  3393. {$ifdef x86_64}
  3394. { even if it's a 32 bit reg, we still have to spill 64 bits
  3395. because we often perform 64 bit operations on them }
  3396. if (size=S_L) then
  3397. begin
  3398. size:=S_Q;
  3399. r:=newreg(getregtype(r),getsupreg(r),R_SUBWHOLE);
  3400. end;
  3401. {$endif x86_64}
  3402. result:=taicpu.op_reg_ref(A_MOV,size,r,tmpref);
  3403. end;
  3404. R_MMREGISTER :
  3405. if current_settings.fputype in fpu_avx_instructionsets then
  3406. case getsubreg(r) of
  3407. R_SUBMMD:
  3408. result:=taicpu.op_reg_ref(A_VMOVSD,reg2opsize(r),r,tmpref);
  3409. R_SUBMMS:
  3410. result:=taicpu.op_reg_ref(A_VMOVSS,reg2opsize(r),r,tmpref);
  3411. R_SUBQ,
  3412. R_SUBMMWHOLE:
  3413. result:=taicpu.op_reg_ref(A_VMOVQ,S_NO,r,tmpref);
  3414. else
  3415. internalerror(200506042);
  3416. end
  3417. else
  3418. case getsubreg(r) of
  3419. R_SUBMMD:
  3420. result:=taicpu.op_reg_ref(A_MOVSD,reg2opsize(r),r,tmpref);
  3421. R_SUBMMS:
  3422. result:=taicpu.op_reg_ref(A_MOVSS,reg2opsize(r),r,tmpref);
  3423. R_SUBQ,
  3424. R_SUBMMWHOLE:
  3425. result:=taicpu.op_reg_ref(A_MOVQ,S_NO,r,tmpref);
  3426. else
  3427. internalerror(200506042);
  3428. end;
  3429. else
  3430. internalerror(200401041);
  3431. end;
  3432. end;
  3433. {$ifdef i8086}
  3434. procedure taicpu.loadsegsymbol(opidx:longint;s:tasmsymbol);
  3435. var
  3436. r: treference;
  3437. begin
  3438. reference_reset_symbol(r,s,0,1);
  3439. r.refaddr:=addr_seg;
  3440. loadref(opidx,r);
  3441. end;
  3442. {$endif i8086}
  3443. {*****************************************************************************
  3444. Instruction table
  3445. *****************************************************************************}
  3446. procedure BuildInsTabCache;
  3447. var
  3448. i : longint;
  3449. begin
  3450. new(instabcache);
  3451. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  3452. i:=0;
  3453. while (i<InsTabEntries) do
  3454. begin
  3455. if InsTabCache^[InsTab[i].OPcode]=-1 then
  3456. InsTabCache^[InsTab[i].OPcode]:=i;
  3457. inc(i);
  3458. end;
  3459. end;
  3460. procedure BuildInsTabMemRefSizeInfoCache;
  3461. var
  3462. AsmOp: TasmOp;
  3463. i,j: longint;
  3464. insentry : PInsEntry;
  3465. MRefInfo: TMemRefSizeInfo;
  3466. SConstInfo: TConstSizeInfo;
  3467. actRegSize: int64;
  3468. actMemSize: int64;
  3469. actConstSize: int64;
  3470. actRegCount: integer;
  3471. actMemCount: integer;
  3472. actConstCount: integer;
  3473. actRegTypes : int64;
  3474. actRegMemTypes: int64;
  3475. NewRegSize: int64;
  3476. actVMemCount : integer;
  3477. actVMemTypes : int64;
  3478. RegMMXSizeMask: int64;
  3479. RegXMMSizeMask: int64;
  3480. RegYMMSizeMask: int64;
  3481. bitcount: integer;
  3482. function bitcnt(aValue: int64): integer;
  3483. var
  3484. i: integer;
  3485. begin
  3486. result := 0;
  3487. for i := 0 to 63 do
  3488. begin
  3489. if (aValue mod 2) = 1 then
  3490. begin
  3491. inc(result);
  3492. end;
  3493. aValue := aValue shr 1;
  3494. end;
  3495. end;
  3496. begin
  3497. new(InsTabMemRefSizeInfoCache);
  3498. FillChar(InsTabMemRefSizeInfoCache^,sizeof(TInsTabMemRefSizeInfoCache),0);
  3499. for AsmOp := low(TAsmOp) to high(TAsmOp) do
  3500. begin
  3501. i := InsTabCache^[AsmOp];
  3502. if i >= 0 then
  3503. begin
  3504. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiUnkown;
  3505. InsTabMemRefSizeInfoCache^[AsmOp].ConstSize := csiUnkown;
  3506. InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX := false;
  3507. insentry:=@instab[i];
  3508. RegMMXSizeMask := 0;
  3509. RegXMMSizeMask := 0;
  3510. RegYMMSizeMask := 0;
  3511. while (insentry^.opcode=AsmOp) do
  3512. begin
  3513. MRefInfo := msiUnkown;
  3514. actRegSize := 0;
  3515. actRegCount := 0;
  3516. actRegTypes := 0;
  3517. NewRegSize := 0;
  3518. actMemSize := 0;
  3519. actMemCount := 0;
  3520. actRegMemTypes := 0;
  3521. actVMemCount := 0;
  3522. actVMemTypes := 0;
  3523. actConstSize := 0;
  3524. actConstCount := 0;
  3525. for j := 0 to insentry^.ops -1 do
  3526. begin
  3527. if ((insentry^.optypes[j] and OT_XMEM32) = OT_XMEM32) OR
  3528. ((insentry^.optypes[j] and OT_XMEM64) = OT_XMEM64) OR
  3529. ((insentry^.optypes[j] and OT_YMEM32) = OT_YMEM32) OR
  3530. ((insentry^.optypes[j] and OT_YMEM64) = OT_YMEM64) then
  3531. begin
  3532. inc(actVMemCount);
  3533. case insentry^.optypes[j] and (OT_XMEM32 OR OT_XMEM64 OR OT_YMEM32 OR OT_YMEM64) of
  3534. OT_XMEM32: actVMemTypes := actVMemTypes or OT_XMEM32;
  3535. OT_XMEM64: actVMemTypes := actVMemTypes or OT_XMEM64;
  3536. OT_YMEM32: actVMemTypes := actVMemTypes or OT_YMEM32;
  3537. OT_YMEM64: actVMemTypes := actVMemTypes or OT_YMEM64;
  3538. else InternalError(777206);
  3539. end;
  3540. end
  3541. else if (insentry^.optypes[j] and OT_REGISTER) = OT_REGISTER then
  3542. begin
  3543. inc(actRegCount);
  3544. NewRegSize := (insentry^.optypes[j] and OT_SIZE_MASK);
  3545. if NewRegSize = 0 then
  3546. begin
  3547. case insentry^.optypes[j] and (OT_MMXREG OR OT_XMMREG OR OT_YMMREG) of
  3548. OT_MMXREG: begin
  3549. NewRegSize := OT_BITS64;
  3550. end;
  3551. OT_XMMREG: begin
  3552. NewRegSize := OT_BITS128;
  3553. InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX := true;
  3554. end;
  3555. OT_YMMREG: begin
  3556. NewRegSize := OT_BITS256;
  3557. InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX := true;
  3558. end;
  3559. else NewRegSize := not(0);
  3560. end;
  3561. end;
  3562. actRegSize := actRegSize or NewRegSize;
  3563. actRegTypes := actRegTypes or (insentry^.optypes[j] and (OT_MMXREG OR OT_XMMREG OR OT_YMMREG));
  3564. end
  3565. else if ((insentry^.optypes[j] and OT_MEMORY) <> 0) then
  3566. begin
  3567. inc(actMemCount);
  3568. actMemSize:=actMemSize or (insentry^.optypes[j] and OT_SIZE_MASK);
  3569. if (insentry^.optypes[j] and OT_REGMEM) = OT_REGMEM then
  3570. begin
  3571. actRegMemTypes := actRegMemTypes or insentry^.optypes[j];
  3572. end;
  3573. end
  3574. else if ((insentry^.optypes[j] and OT_IMMEDIATE) = OT_IMMEDIATE) then
  3575. begin
  3576. inc(actConstCount);
  3577. actConstSize := actConstSize or (insentry^.optypes[j] and OT_SIZE_MASK);
  3578. end
  3579. end;
  3580. if actConstCount > 0 then
  3581. begin
  3582. case actConstSize of
  3583. 0: SConstInfo := csiNoSize;
  3584. OT_BITS8: SConstInfo := csiMem8;
  3585. OT_BITS16: SConstInfo := csiMem16;
  3586. OT_BITS32: SConstInfo := csiMem32;
  3587. OT_BITS64: SConstInfo := csiMem64;
  3588. else SConstInfo := csiMultiple;
  3589. end;
  3590. if InsTabMemRefSizeInfoCache^[AsmOp].ConstSize = csiUnkown then
  3591. begin
  3592. InsTabMemRefSizeInfoCache^[AsmOp].ConstSize := SConstInfo;
  3593. end
  3594. else if InsTabMemRefSizeInfoCache^[AsmOp].ConstSize <> SConstInfo then
  3595. begin
  3596. InsTabMemRefSizeInfoCache^[AsmOp].ConstSize := csiMultiple;
  3597. end;
  3598. end;
  3599. if actVMemCount > 0 then
  3600. begin
  3601. if actVMemCount = 1 then
  3602. begin
  3603. if actVMemTypes > 0 then
  3604. begin
  3605. case actVMemTypes of
  3606. OT_XMEM32: MRefInfo := msiXMem32;
  3607. OT_XMEM64: MRefInfo := msiXMem64;
  3608. OT_YMEM32: MRefInfo := msiYMem32;
  3609. OT_YMEM64: MRefInfo := msiYMem64;
  3610. else InternalError(777208);
  3611. end;
  3612. case actRegTypes of
  3613. OT_XMMREG: case MRefInfo of
  3614. msiXMem32,
  3615. msiXMem64: RegXMMSizeMask := RegXMMSizeMask or OT_BITS128;
  3616. msiYMem32,
  3617. msiYMem64: RegXMMSizeMask := RegXMMSizeMask or OT_BITS256;
  3618. else InternalError(777210);
  3619. end;
  3620. OT_YMMREG: case MRefInfo of
  3621. msiXMem32,
  3622. msiXMem64: RegYMMSizeMask := RegYMMSizeMask or OT_BITS128;
  3623. msiYMem32,
  3624. msiYMem64: RegYMMSizeMask := RegYMMSizeMask or OT_BITS256;
  3625. else InternalError(777211);
  3626. end;
  3627. //else InternalError(777209);
  3628. end;
  3629. if InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize = msiUnkown then
  3630. begin
  3631. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := MRefInfo;
  3632. end
  3633. else if InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize <> MRefInfo then
  3634. begin
  3635. if InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize in [msiXMem32, msiXMem64, msiYMem32, msiYMem64] then
  3636. begin
  3637. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiVMemMultiple;
  3638. end
  3639. else InternalError(777212);
  3640. end;
  3641. end;
  3642. end
  3643. else InternalError(777207);
  3644. end
  3645. else
  3646. case actMemCount of
  3647. 0: ; // nothing todo
  3648. 1: begin
  3649. MRefInfo := msiUnkown;
  3650. case actRegMemTypes and (OT_MMXRM OR OT_XMMRM OR OT_YMMRM) of
  3651. OT_MMXRM: actMemSize := actMemSize or OT_BITS64;
  3652. OT_XMMRM: actMemSize := actMemSize or OT_BITS128;
  3653. OT_YMMRM: actMemSize := actMemSize or OT_BITS256;
  3654. end;
  3655. case actMemSize of
  3656. 0: MRefInfo := msiNoSize;
  3657. OT_BITS8: MRefInfo := msiMem8;
  3658. OT_BITS16: MRefInfo := msiMem16;
  3659. OT_BITS32: MRefInfo := msiMem32;
  3660. OT_BITS64: MRefInfo := msiMem64;
  3661. OT_BITS128: MRefInfo := msiMem128;
  3662. OT_BITS256: MRefInfo := msiMem256;
  3663. OT_BITS80,
  3664. OT_FAR,
  3665. OT_NEAR,
  3666. OT_SHORT: ; // ignore
  3667. else
  3668. begin
  3669. bitcount := bitcnt(actMemSize);
  3670. if bitcount > 1 then MRefInfo := msiMultiple
  3671. else InternalError(777203);
  3672. end;
  3673. end;
  3674. if InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize = msiUnkown then
  3675. begin
  3676. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := MRefInfo;
  3677. end
  3678. else if InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize <> MRefInfo then
  3679. begin
  3680. with InsTabMemRefSizeInfoCache^[AsmOp] do
  3681. begin
  3682. if ((MemRefSize = msiMem8) OR (MRefInfo = msiMem8)) then MemRefSize := msiMultiple8
  3683. else if ((MemRefSize = msiMem16) OR (MRefInfo = msiMem16)) then MemRefSize := msiMultiple16
  3684. else if ((MemRefSize = msiMem32) OR (MRefInfo = msiMem32)) then MemRefSize := msiMultiple32
  3685. else if ((MemRefSize = msiMem64) OR (MRefInfo = msiMem64)) then MemRefSize := msiMultiple64
  3686. else if ((MemRefSize = msiMem128) OR (MRefInfo = msiMem128)) then MemRefSize := msiMultiple128
  3687. else if ((MemRefSize = msiMem256) OR (MRefInfo = msiMem256)) then MemRefSize := msiMultiple256
  3688. else MemRefSize := msiMultiple;
  3689. end;
  3690. end;
  3691. if actRegCount > 0 then
  3692. begin
  3693. case actRegTypes and (OT_MMXREG or OT_XMMREG or OT_YMMREG) of
  3694. OT_MMXREG: RegMMXSizeMask := RegMMXSizeMask or actMemSize;
  3695. OT_XMMREG: RegXMMSizeMask := RegXMMSizeMask or actMemSize;
  3696. OT_YMMREG: RegYMMSizeMask := RegYMMSizeMask or actMemSize;
  3697. else begin
  3698. RegMMXSizeMask := not(0);
  3699. RegXMMSizeMask := not(0);
  3700. RegYMMSizeMask := not(0);
  3701. end;
  3702. end;
  3703. end;
  3704. end;
  3705. else InternalError(777202);
  3706. end;
  3707. inc(insentry);
  3708. end;
  3709. if (InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize in MemRefMultiples) and
  3710. (InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX)then
  3711. begin
  3712. case RegXMMSizeMask of
  3713. OT_BITS16: case RegYMMSizeMask of
  3714. OT_BITS32: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegx16y32;
  3715. end;
  3716. OT_BITS32: case RegYMMSizeMask of
  3717. OT_BITS64: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegx32y64;
  3718. end;
  3719. OT_BITS64: case RegYMMSizeMask of
  3720. OT_BITS128: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegx64y128;
  3721. OT_BITS256: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegx64y256;
  3722. end;
  3723. OT_BITS128: begin
  3724. if InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize = msiVMemMultiple then
  3725. begin
  3726. // vector-memory-operand AVX2 (e.g. VGATHER..)
  3727. case RegYMMSizeMask of
  3728. OT_BITS256: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiVMemRegSize;
  3729. end;
  3730. end
  3731. else if RegMMXSizeMask = 0 then
  3732. begin
  3733. case RegYMMSizeMask of
  3734. OT_BITS128: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegx64y128;
  3735. OT_BITS256: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegSize;
  3736. end;
  3737. end
  3738. else if RegYMMSizeMask = 0 then
  3739. begin
  3740. case RegMMXSizeMask of
  3741. OT_BITS64: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegSize;
  3742. end;
  3743. end
  3744. else InternalError(777205);
  3745. end;
  3746. end;
  3747. end;
  3748. end;
  3749. end;
  3750. for AsmOp := low(TAsmOp) to high(TAsmOp) do
  3751. begin
  3752. // only supported intructiones with SSE- or AVX-operands
  3753. if not(InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX) then
  3754. begin
  3755. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiUnkown;
  3756. InsTabMemRefSizeInfoCache^[AsmOp].ConstSize := csiUnkown;
  3757. end;
  3758. end;
  3759. end;
  3760. procedure InitAsm;
  3761. begin
  3762. build_spilling_operation_type_table;
  3763. if not assigned(instabcache) then
  3764. BuildInsTabCache;
  3765. if not assigned(InsTabMemRefSizeInfoCache) then
  3766. BuildInsTabMemRefSizeInfoCache;
  3767. end;
  3768. procedure DoneAsm;
  3769. begin
  3770. if assigned(operation_type_table) then
  3771. begin
  3772. dispose(operation_type_table);
  3773. operation_type_table:=nil;
  3774. end;
  3775. if assigned(instabcache) then
  3776. begin
  3777. dispose(instabcache);
  3778. instabcache:=nil;
  3779. end;
  3780. if assigned(InsTabMemRefSizeInfoCache) then
  3781. begin
  3782. dispose(InsTabMemRefSizeInfoCache);
  3783. InsTabMemRefSizeInfoCache:=nil;
  3784. end;
  3785. end;
  3786. begin
  3787. cai_align:=tai_align;
  3788. cai_cpu:=taicpu;
  3789. end.