aasmcpu.pas 196 KB

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