aasmcpu.pas 149 KB

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