aasmcpu.pas 196 KB

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