aasmcpu.pas 206 KB

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