aasmcpu.pas 190 KB

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