aasmcpu.pas 149 KB

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