aasmcpu.pas 198 KB

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