aasmcpu.pas 210 KB

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