aasmcpu.pas 145 KB

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