aasmcpu.pas 201 KB

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