aasmcpu.pas 203 KB

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