aasmcpu.pas 149 KB

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