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_T1S8, { disp8 - tuple - 1 scalar byte }
  431. IF_T1S16, { disp8 - tuple - 1 scalar word }
  432. IF_T1F32,
  433. IF_T1F64,
  434. IF_TMDDUP,
  435. IF_TFV, { disp8 - tuple - full vector }
  436. IF_TFVM, { disp8 - tuple - full vector memory }
  437. IF_TQVM,
  438. IF_TMEM128,
  439. IF_THV,
  440. IF_THVM,
  441. IF_TOVM
  442. );
  443. tinsflags=set of tinsflag;
  444. const
  445. IF_SMASK=[IF_SM,IF_SM2,IF_SB,IF_SW,IF_SD];
  446. IF_ARMASK=[IF_AR0,IF_AR1,IF_AR2]; { mask for unsized argument spec }
  447. IF_PLEVEL=[IF_8086..IF_NEC]; { mask for processor level }
  448. IF_TUPLEMASK=[IF_T2..IF_TOVM]; { mask for AVX512 disp8-tuples }
  449. type
  450. tinsentry=packed record
  451. opcode : tasmop;
  452. ops : byte;
  453. optypes : array[0..max_operands-1] of int64;
  454. code : array[0..maxinfolen] of char;
  455. flags : tinsflags;
  456. end;
  457. pinsentry=^tinsentry;
  458. { alignment for operator }
  459. tai_align = class(tai_align_abstract)
  460. function calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;override;
  461. end;
  462. { taicpu }
  463. taicpu = class(tai_cpu_abstract_sym)
  464. opsize : topsize;
  465. constructor op_none(op : tasmop);
  466. constructor op_none(op : tasmop;_size : topsize);
  467. constructor op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  468. constructor op_const(op : tasmop;_size : topsize;_op1 : aint);
  469. constructor op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  470. constructor op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  471. constructor op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  472. constructor op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aint);
  473. constructor op_const_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister);
  474. constructor op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aint);
  475. constructor op_const_ref(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference);
  476. constructor op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  477. constructor op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  478. constructor op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;_op3 : tregister);
  479. constructor op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference;_op3 : tregister);
  480. constructor op_ref_reg_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2,_op3 : tregister);
  481. constructor op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;const _op3 : treference);
  482. constructor op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister;const _op3 : treference);
  483. constructor op_const_reg_reg_reg(op : tasmop;_size : topsize;_op1 : aint;_op2, _op3, _op4 : tregister);
  484. { this is for Jmp instructions }
  485. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  486. constructor op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  487. constructor op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  488. constructor op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  489. constructor op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  490. procedure changeopsize(siz:topsize);
  491. function GetString:string;
  492. { This is a workaround for the GAS non commutative fpu instruction braindamage.
  493. Early versions of the UnixWare assembler had a bug where some fpu instructions
  494. were reversed and GAS still keeps this "feature" for compatibility.
  495. for details: http://sourceware.org/binutils/docs/as/i386_002dBugs.html#i386_002dBugs
  496. http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=372528
  497. http://en.wikibooks.org/wiki/X86_Assembly/GAS_Syntax#Caveats
  498. Since FPC is "GAS centric" due to its history it generates instructions with the same operand order so
  499. when generating output for other assemblers, the opcodes must be fixed before writing them.
  500. This function returns the fixed opcodes. Changing the opcodes permanently is no good idea
  501. because in case of smartlinking assembler is generated twice so at the second run wrong
  502. assembler is generated.
  503. }
  504. function FixNonCommutativeOpcodes: tasmop;
  505. private
  506. FOperandOrder : TOperandOrder;
  507. procedure init(_size : topsize); { this need to be called by all constructor }
  508. public
  509. { the next will reset all instructions that can change in pass 2 }
  510. procedure ResetPass1;override;
  511. procedure ResetPass2;override;
  512. function CheckIfValid:boolean;
  513. function Pass1(objdata:TObjData):longint;override;
  514. procedure Pass2(objdata:TObjData);override;
  515. procedure SetOperandOrder(order:TOperandOrder);
  516. function is_same_reg_move(regtype: Tregistertype):boolean;override;
  517. { register spilling code }
  518. function spilling_get_operation_type(opnr: longint): topertype;override;
  519. {$ifdef i8086}
  520. procedure loadsegsymbol(opidx:longint;s:tasmsymbol);
  521. {$endif i8086}
  522. property OperandOrder : TOperandOrder read FOperandOrder;
  523. private
  524. { next fields are filled in pass1, so pass2 is faster }
  525. insentry : PInsEntry;
  526. insoffset : longint;
  527. LastInsOffset : longint; { need to be public to be reset }
  528. inssize : shortint;
  529. EVEXTupleState: TEVEXTupleState; { AVX512 disp8*N }
  530. {$ifdef x86_64}
  531. rex : byte;
  532. {$endif x86_64}
  533. function InsEnd:longint;
  534. procedure create_ot(objdata:TObjData);
  535. function Matches(p:PInsEntry):boolean;
  536. function calcsize(p:PInsEntry):shortint;
  537. procedure gencode(objdata:TObjData);
  538. function NeedAddrPrefix(opidx:byte):boolean;
  539. function NeedAddrPrefix:boolean;
  540. procedure write0x66prefix(objdata:TObjData);
  541. procedure write0x67prefix(objdata:TObjData);
  542. procedure Swapoperands;
  543. function FindInsentry(objdata:TObjData):boolean;
  544. function CheckUseEVEX: boolean;
  545. procedure CheckEVEXTuple(const aInput:toper; aInsEntry: pInsentry; aIsVector128, aIsVector256, aIsVector512, aIsEVEXW1: boolean);
  546. end;
  547. function is_64_bit_ref(const ref:treference):boolean;
  548. function is_32_bit_ref(const ref:treference):boolean;
  549. function is_16_bit_ref(const ref:treference):boolean;
  550. function get_ref_address_size(const ref:treference):byte;
  551. function get_default_segment_of_ref(const ref:treference):tregister;
  552. procedure optimize_ref(var ref:treference; inlineasm: boolean);
  553. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  554. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  555. function MemRefInfo(aAsmop: TAsmOp): TInsTabMemRefSizeInfoRec;
  556. function MightHaveExtension(AsmOp : TAsmOp) : Boolean;
  557. procedure InitAsm;
  558. procedure DoneAsm;
  559. {*****************************************************************************
  560. External Symbol Chain
  561. used for agx86nsm and agx86int
  562. *****************************************************************************}
  563. type
  564. PExternChain = ^TExternChain;
  565. TExternChain = Record
  566. psym : pshortstring;
  567. is_defined : boolean;
  568. next : PExternChain;
  569. end;
  570. const
  571. FEC : PExternChain = nil;
  572. procedure AddSymbol(symname : string; defined : boolean);
  573. procedure FreeExternChainList;
  574. implementation
  575. uses
  576. cutils,
  577. globals,
  578. systems,
  579. itcpugas,
  580. cpuinfo;
  581. procedure AddSymbol(symname : string; defined : boolean);
  582. var
  583. EC : PExternChain;
  584. begin
  585. EC:=FEC;
  586. while assigned(EC) do
  587. begin
  588. if EC^.psym^=symname then
  589. begin
  590. if defined then
  591. EC^.is_defined:=true;
  592. exit;
  593. end;
  594. EC:=EC^.next;
  595. end;
  596. New(EC);
  597. EC^.next:=FEC;
  598. FEC:=EC;
  599. FEC^.psym:=stringdup(symname);
  600. FEC^.is_defined := defined;
  601. end;
  602. procedure FreeExternChainList;
  603. var
  604. EC : PExternChain;
  605. begin
  606. EC:=FEC;
  607. while assigned(EC) do
  608. begin
  609. FEC:=EC^.next;
  610. stringdispose(EC^.psym);
  611. Dispose(EC);
  612. EC:=FEC;
  613. end;
  614. end;
  615. {*****************************************************************************
  616. Instruction table
  617. *****************************************************************************}
  618. type
  619. TInsTabCache=array[TasmOp] of longint;
  620. PInsTabCache=^TInsTabCache;
  621. TInsTabMemRefSizeInfoCache=array[TasmOp] of TInsTabMemRefSizeInfoRec;
  622. PInsTabMemRefSizeInfoCache=^TInsTabMemRefSizeInfoCache;
  623. const
  624. {$if defined(x86_64)}
  625. InsTab:array[0..instabentries-1] of TInsEntry={$i x8664tab.inc}
  626. {$elseif defined(i386)}
  627. InsTab:array[0..instabentries-1] of TInsEntry={$i i386tab.inc}
  628. {$elseif defined(i8086)}
  629. InsTab:array[0..instabentries-1] of TInsEntry={$i i8086tab.inc}
  630. {$endif}
  631. var
  632. InsTabCache : PInsTabCache;
  633. InsTabMemRefSizeInfoCache: PInsTabMemRefSizeInfoCache;
  634. const
  635. {$if defined(x86_64)}
  636. { Intel style operands ! }
  637. opsize_2_type:array[0..2,topsize] of int64=(
  638. (OT_NONE,
  639. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,OT_BITS64,OT_BITS64,OT_BITS64,
  640. OT_BITS16,OT_BITS32,OT_BITS64,
  641. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  642. OT_BITS64,
  643. OT_NEAR,OT_FAR,OT_SHORT,
  644. OT_NONE,
  645. OT_BITS128,
  646. OT_BITS256,
  647. OT_BITS512
  648. ),
  649. (OT_NONE,
  650. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,OT_BITS8,OT_BITS16,OT_BITS32,
  651. OT_BITS16,OT_BITS32,OT_BITS64,
  652. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  653. OT_BITS64,
  654. OT_NEAR,OT_FAR,OT_SHORT,
  655. OT_NONE,
  656. OT_BITS128,
  657. OT_BITS256,
  658. OT_BITS512
  659. ),
  660. (OT_NONE,
  661. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,
  662. OT_BITS16,OT_BITS32,OT_BITS64,
  663. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  664. OT_BITS64,
  665. OT_NEAR,OT_FAR,OT_SHORT,
  666. OT_NONE,
  667. OT_BITS128,
  668. OT_BITS256,
  669. OT_BITS512
  670. )
  671. );
  672. reg_ot_table : array[tregisterindex] of longint = (
  673. {$i r8664ot.inc}
  674. );
  675. {$elseif defined(i386)}
  676. { Intel style operands ! }
  677. opsize_2_type:array[0..2,topsize] of int64=(
  678. (OT_NONE,
  679. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,
  680. OT_BITS16,OT_BITS32,OT_BITS64,
  681. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  682. OT_BITS64,
  683. OT_NEAR,OT_FAR,OT_SHORT,
  684. OT_NONE,
  685. OT_BITS128,
  686. OT_BITS256,
  687. OT_BITS512
  688. ),
  689. (OT_NONE,
  690. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,
  691. OT_BITS16,OT_BITS32,OT_BITS64,
  692. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  693. OT_BITS64,
  694. OT_NEAR,OT_FAR,OT_SHORT,
  695. OT_NONE,
  696. OT_BITS128,
  697. OT_BITS256,
  698. OT_BITS512
  699. ),
  700. (OT_NONE,
  701. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,
  702. OT_BITS16,OT_BITS32,OT_BITS64,
  703. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  704. OT_BITS64,
  705. OT_NEAR,OT_FAR,OT_SHORT,
  706. OT_NONE,
  707. OT_BITS128,
  708. OT_BITS256,
  709. OT_BITS512
  710. )
  711. );
  712. reg_ot_table : array[tregisterindex] of longint = (
  713. {$i r386ot.inc}
  714. );
  715. {$elseif defined(i8086)}
  716. { Intel style operands ! }
  717. opsize_2_type:array[0..2,topsize] of int64=(
  718. (OT_NONE,
  719. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,
  720. OT_BITS16,OT_BITS32,OT_BITS64,
  721. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  722. OT_BITS64,
  723. OT_NEAR,OT_FAR,OT_SHORT,
  724. OT_NONE,
  725. OT_BITS128,
  726. OT_BITS256,
  727. OT_BITS512
  728. ),
  729. (OT_NONE,
  730. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,
  731. OT_BITS16,OT_BITS32,OT_BITS64,
  732. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  733. OT_BITS64,
  734. OT_NEAR,OT_FAR,OT_SHORT,
  735. OT_NONE,
  736. OT_BITS128,
  737. OT_BITS256,
  738. OT_BITS512
  739. ),
  740. (OT_NONE,
  741. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,
  742. OT_BITS16,OT_BITS32,OT_BITS64,
  743. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  744. OT_BITS64,
  745. OT_NEAR,OT_FAR,OT_SHORT,
  746. OT_NONE,
  747. OT_BITS128,
  748. OT_BITS256,
  749. OT_BITS512
  750. )
  751. );
  752. reg_ot_table : array[tregisterindex] of longint = (
  753. {$i r8086ot.inc}
  754. );
  755. {$endif}
  756. function MemRefInfo(aAsmop: TAsmOp): TInsTabMemRefSizeInfoRec;
  757. begin
  758. result := InsTabMemRefSizeInfoCache^[aAsmop];
  759. end;
  760. function MightHaveExtension(AsmOp : TAsmOp): Boolean;
  761. var
  762. i,j: LongInt;
  763. insentry: pinsentry;
  764. begin
  765. Result:=true;
  766. i:=InsTabCache^[AsmOp];
  767. if i>=0 then
  768. begin
  769. insentry:=@instab[i];
  770. while insentry^.opcode=AsmOp do
  771. begin
  772. for j:=0 to insentry^.ops-1 do
  773. begin
  774. if (insentry^.optypes[j] and OT_VECTOR_EXT)<>0 then
  775. exit;
  776. end;
  777. inc(i);
  778. insentry:=@instab[i];
  779. end;
  780. end;
  781. Result:=false;
  782. end;
  783. { Operation type for spilling code }
  784. type
  785. toperation_type_table=array[tasmop,0..Max_Operands] of topertype;
  786. var
  787. operation_type_table : ^toperation_type_table;
  788. {****************************************************************************
  789. TAI_ALIGN
  790. ****************************************************************************}
  791. function tai_align.calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;
  792. const
  793. { Updated according to
  794. Software Optimization Guide for AMD Family 15h Processors, Verison 3.08, January 2014
  795. and
  796. Intel 64 and IA-32 Architectures Software Developer’s Manual
  797. Volume 2B: Instruction Set Reference, N-Z, January 2015
  798. }
  799. alignarray_cmovcpus:array[0..10] of string[11]=(
  800. #$66#$66#$66#$0F#$1F#$84#$00#$00#$00#$00#$00,
  801. #$66#$66#$0F#$1F#$84#$00#$00#$00#$00#$00,
  802. #$66#$0F#$1F#$84#$00#$00#$00#$00#$00,
  803. #$0F#$1F#$84#$00#$00#$00#$00#$00,
  804. #$0F#$1F#$80#$00#$00#$00#$00,
  805. #$66#$0F#$1F#$44#$00#$00,
  806. #$0F#$1F#$44#$00#$00,
  807. #$0F#$1F#$40#$00,
  808. #$0F#$1F#$00,
  809. #$66#$90,
  810. #$90);
  811. {$ifdef i8086}
  812. alignarray:array[0..5] of string[8]=(
  813. #$90#$90#$90#$90#$90#$90#$90,
  814. #$90#$90#$90#$90#$90#$90,
  815. #$90#$90#$90#$90,
  816. #$90#$90#$90,
  817. #$90#$90,
  818. #$90);
  819. {$else i8086}
  820. alignarray:array[0..5] of string[8]=(
  821. #$8D#$B4#$26#$00#$00#$00#$00,
  822. #$8D#$B6#$00#$00#$00#$00,
  823. #$8D#$74#$26#$00,
  824. #$8D#$76#$00,
  825. #$89#$F6,
  826. #$90);
  827. {$endif i8086}
  828. var
  829. bufptr : pchar;
  830. j : longint;
  831. localsize: byte;
  832. begin
  833. inherited calculatefillbuf(buf,executable);
  834. if not(use_op) and executable then
  835. begin
  836. bufptr:=pchar(@buf);
  837. { fillsize may still be used afterwards, so don't modify }
  838. { e.g. writebytes(hp.calculatefillbuf(buf)^,hp.fillsize) }
  839. localsize:=fillsize;
  840. while (localsize>0) do
  841. begin
  842. {$ifndef i8086}
  843. if (CPUX86_HAS_CMOV in cpu_capabilities[current_settings.cputype])
  844. {$ifdef i386} and not (target_info.system in systems_i386_no_cmov_align) {$endif} then
  845. begin
  846. for j:=low(alignarray_cmovcpus) to high(alignarray_cmovcpus) do
  847. if (localsize>=length(alignarray_cmovcpus[j])) then
  848. break;
  849. move(alignarray_cmovcpus[j][1],bufptr^,length(alignarray_cmovcpus[j]));
  850. inc(bufptr,length(alignarray_cmovcpus[j]));
  851. dec(localsize,length(alignarray_cmovcpus[j]));
  852. end
  853. else
  854. {$endif not i8086}
  855. begin
  856. for j:=low(alignarray) to high(alignarray) do
  857. if (localsize>=length(alignarray[j])) then
  858. break;
  859. move(alignarray[j][1],bufptr^,length(alignarray[j]));
  860. inc(bufptr,length(alignarray[j]));
  861. dec(localsize,length(alignarray[j]));
  862. end
  863. end;
  864. end;
  865. calculatefillbuf:=pchar(@buf);
  866. end;
  867. {*****************************************************************************
  868. Taicpu Constructors
  869. *****************************************************************************}
  870. procedure taicpu.changeopsize(siz:topsize);
  871. begin
  872. opsize:=siz;
  873. end;
  874. procedure taicpu.init(_size : topsize);
  875. begin
  876. { default order is att }
  877. FOperandOrder:=op_att;
  878. segprefix:=NR_NO;
  879. opsize:=_size;
  880. insentry:=nil;
  881. LastInsOffset:=-1;
  882. InsOffset:=0;
  883. InsSize:=0;
  884. EVEXTupleState := etsUnknown;
  885. end;
  886. constructor taicpu.op_none(op : tasmop);
  887. begin
  888. inherited create(op);
  889. init(S_NO);
  890. end;
  891. constructor taicpu.op_none(op : tasmop;_size : topsize);
  892. begin
  893. inherited create(op);
  894. init(_size);
  895. end;
  896. constructor taicpu.op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  897. begin
  898. inherited create(op);
  899. init(_size);
  900. ops:=1;
  901. loadreg(0,_op1);
  902. end;
  903. constructor taicpu.op_const(op : tasmop;_size : topsize;_op1 : aint);
  904. begin
  905. inherited create(op);
  906. init(_size);
  907. ops:=1;
  908. loadconst(0,_op1);
  909. end;
  910. constructor taicpu.op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  911. begin
  912. inherited create(op);
  913. init(_size);
  914. ops:=1;
  915. loadref(0,_op1);
  916. end;
  917. constructor taicpu.op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  918. begin
  919. inherited create(op);
  920. init(_size);
  921. ops:=2;
  922. loadreg(0,_op1);
  923. loadreg(1,_op2);
  924. end;
  925. constructor taicpu.op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aint);
  926. begin
  927. inherited create(op);
  928. init(_size);
  929. ops:=2;
  930. loadreg(0,_op1);
  931. loadconst(1,_op2);
  932. end;
  933. constructor taicpu.op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  934. begin
  935. inherited create(op);
  936. init(_size);
  937. ops:=2;
  938. loadreg(0,_op1);
  939. loadref(1,_op2);
  940. end;
  941. constructor taicpu.op_const_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister);
  942. begin
  943. inherited create(op);
  944. init(_size);
  945. ops:=2;
  946. loadconst(0,_op1);
  947. loadreg(1,_op2);
  948. end;
  949. constructor taicpu.op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aint);
  950. begin
  951. inherited create(op);
  952. init(_size);
  953. ops:=2;
  954. loadconst(0,_op1);
  955. loadconst(1,_op2);
  956. end;
  957. constructor taicpu.op_const_ref(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference);
  958. begin
  959. inherited create(op);
  960. init(_size);
  961. ops:=2;
  962. loadconst(0,_op1);
  963. loadref(1,_op2);
  964. end;
  965. constructor taicpu.op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  966. begin
  967. inherited create(op);
  968. init(_size);
  969. ops:=2;
  970. loadref(0,_op1);
  971. loadreg(1,_op2);
  972. end;
  973. constructor taicpu.op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  974. begin
  975. inherited create(op);
  976. init(_size);
  977. ops:=3;
  978. loadreg(0,_op1);
  979. loadreg(1,_op2);
  980. loadreg(2,_op3);
  981. end;
  982. constructor taicpu.op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;_op3 : tregister);
  983. begin
  984. inherited create(op);
  985. init(_size);
  986. ops:=3;
  987. loadconst(0,_op1);
  988. loadreg(1,_op2);
  989. loadreg(2,_op3);
  990. end;
  991. constructor taicpu.op_ref_reg_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2,_op3 : tregister);
  992. begin
  993. inherited create(op);
  994. init(_size);
  995. ops:=3;
  996. loadref(0,_op1);
  997. loadreg(1,_op2);
  998. loadreg(2,_op3);
  999. end;
  1000. constructor taicpu.op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference;_op3 : tregister);
  1001. begin
  1002. inherited create(op);
  1003. init(_size);
  1004. ops:=3;
  1005. loadconst(0,_op1);
  1006. loadref(1,_op2);
  1007. loadreg(2,_op3);
  1008. end;
  1009. constructor taicpu.op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;const _op3 : treference);
  1010. begin
  1011. inherited create(op);
  1012. init(_size);
  1013. ops:=3;
  1014. loadconst(0,_op1);
  1015. loadreg(1,_op2);
  1016. loadref(2,_op3);
  1017. end;
  1018. constructor taicpu.op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister;const _op3 : treference);
  1019. begin
  1020. inherited create(op);
  1021. init(_size);
  1022. ops:=3;
  1023. loadreg(0,_op1);
  1024. loadreg(1,_op2);
  1025. loadref(2,_op3);
  1026. end;
  1027. constructor taicpu.op_const_reg_reg_reg(op : tasmop; _size : topsize; _op1 : aint; _op2, _op3, _op4 : tregister);
  1028. begin
  1029. inherited create(op);
  1030. init(_size);
  1031. ops:=4;
  1032. loadconst(0,_op1);
  1033. loadreg(1,_op2);
  1034. loadreg(2,_op3);
  1035. loadreg(3,_op4);
  1036. end;
  1037. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  1038. begin
  1039. inherited create(op);
  1040. init(_size);
  1041. condition:=cond;
  1042. ops:=1;
  1043. loadsymbol(0,_op1,0);
  1044. end;
  1045. constructor taicpu.op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  1046. begin
  1047. inherited create(op);
  1048. init(_size);
  1049. ops:=1;
  1050. loadsymbol(0,_op1,0);
  1051. end;
  1052. constructor taicpu.op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  1053. begin
  1054. inherited create(op);
  1055. init(_size);
  1056. ops:=1;
  1057. loadsymbol(0,_op1,_op1ofs);
  1058. end;
  1059. constructor taicpu.op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  1060. begin
  1061. inherited create(op);
  1062. init(_size);
  1063. ops:=2;
  1064. loadsymbol(0,_op1,_op1ofs);
  1065. loadreg(1,_op2);
  1066. end;
  1067. constructor taicpu.op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  1068. begin
  1069. inherited create(op);
  1070. init(_size);
  1071. ops:=2;
  1072. loadsymbol(0,_op1,_op1ofs);
  1073. loadref(1,_op2);
  1074. end;
  1075. function taicpu.GetString:string;
  1076. var
  1077. i : longint;
  1078. s : string;
  1079. regnr: string;
  1080. addsize : boolean;
  1081. begin
  1082. s:='['+std_op2str[opcode];
  1083. for i:=0 to ops-1 do
  1084. begin
  1085. with oper[i]^ do
  1086. begin
  1087. if i=0 then
  1088. s:=s+' '
  1089. else
  1090. s:=s+',';
  1091. { type }
  1092. addsize:=false;
  1093. regnr := '';
  1094. if getregtype(reg) = R_MMREGISTER then
  1095. str(getsupreg(reg),regnr);
  1096. if (ot and OT_XMMREG)=OT_XMMREG then
  1097. s:=s+'xmmreg' + regnr
  1098. else
  1099. if (ot and OT_YMMREG)=OT_YMMREG then
  1100. s:=s+'ymmreg' + regnr
  1101. else
  1102. if (ot and OT_ZMMREG)=OT_ZMMREG then
  1103. s:=s+'zmmreg' + regnr
  1104. else
  1105. if (ot and OT_REG_EXTRA_MASK)=OT_MMXREG then
  1106. s:=s+'mmxreg'
  1107. else
  1108. if (ot and OT_REG_EXTRA_MASK)=OT_FPUREG then
  1109. s:=s+'fpureg'
  1110. else
  1111. if (ot and OT_REGISTER)=OT_REGISTER then
  1112. begin
  1113. s:=s+'reg';
  1114. addsize:=true;
  1115. end
  1116. else
  1117. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  1118. begin
  1119. s:=s+'imm';
  1120. addsize:=true;
  1121. end
  1122. else
  1123. if (ot and OT_MEMORY)=OT_MEMORY then
  1124. begin
  1125. s:=s+'mem';
  1126. addsize:=true;
  1127. end
  1128. else
  1129. s:=s+'???';
  1130. { size }
  1131. if addsize then
  1132. begin
  1133. if (ot and OT_BITS8)<>0 then
  1134. s:=s+'8'
  1135. else
  1136. if (ot and OT_BITS16)<>0 then
  1137. s:=s+'16'
  1138. else
  1139. if (ot and OT_BITS32)<>0 then
  1140. s:=s+'32'
  1141. else
  1142. if (ot and OT_BITS64)<>0 then
  1143. s:=s+'64'
  1144. else
  1145. if (ot and OT_BITS128)<>0 then
  1146. s:=s+'128'
  1147. else
  1148. if (ot and OT_BITS256)<>0 then
  1149. s:=s+'256'
  1150. else
  1151. if (ot and OT_BITS512)<>0 then
  1152. s:=s+'512'
  1153. else
  1154. s:=s+'??';
  1155. { signed }
  1156. if (ot and OT_SIGNED)<>0 then
  1157. s:=s+'s';
  1158. end;
  1159. if vopext <> 0 then
  1160. begin
  1161. str(vopext and $07, regnr);
  1162. if vopext and OTVE_VECTOR_WRITEMASK = OTVE_VECTOR_WRITEMASK then
  1163. s := s + ' {k' + regnr + '}';
  1164. if vopext and OTVE_VECTOR_ZERO = OTVE_VECTOR_ZERO then
  1165. s := s + ' {z}';
  1166. if vopext and OTVE_VECTOR_SAE = OTVE_VECTOR_SAE then
  1167. s := s + ' {sae}';
  1168. if vopext and OTVE_VECTOR_BCST = OTVE_VECTOR_BCST then
  1169. case vopext and OTVE_VECTOR_BCST_MASK of
  1170. OTVE_VECTOR_BCST2: s := s + ' {1to2}';
  1171. OTVE_VECTOR_BCST4: s := s + ' {1to4}';
  1172. OTVE_VECTOR_BCST8: s := s + ' {1to8}';
  1173. OTVE_VECTOR_BCST16: s := s + ' {1to16}';
  1174. end;
  1175. if vopext and OTVE_VECTOR_ER = OTVE_VECTOR_ER then
  1176. case vopext and OTVE_VECTOR_ER_MASK of
  1177. OTVE_VECTOR_RNSAE: s := s + ' {rn-sae}';
  1178. OTVE_VECTOR_RDSAE: s := s + ' {rd-sae}';
  1179. OTVE_VECTOR_RUSAE: s := s + ' {ru-sae}';
  1180. OTVE_VECTOR_RZSAE: s := s + ' {rz-sae}';
  1181. end;
  1182. end;
  1183. end;
  1184. end;
  1185. GetString:=s+']';
  1186. end;
  1187. procedure taicpu.Swapoperands;
  1188. var
  1189. p : POper;
  1190. begin
  1191. { Fix the operands which are in AT&T style and we need them in Intel style }
  1192. case ops of
  1193. 0,1:
  1194. ;
  1195. 2 : begin
  1196. { 0,1 -> 1,0 }
  1197. p:=oper[0];
  1198. oper[0]:=oper[1];
  1199. oper[1]:=p;
  1200. end;
  1201. 3 : begin
  1202. { 0,1,2 -> 2,1,0 }
  1203. p:=oper[0];
  1204. oper[0]:=oper[2];
  1205. oper[2]:=p;
  1206. end;
  1207. 4 : begin
  1208. { 0,1,2,3 -> 3,2,1,0 }
  1209. p:=oper[0];
  1210. oper[0]:=oper[3];
  1211. oper[3]:=p;
  1212. p:=oper[1];
  1213. oper[1]:=oper[2];
  1214. oper[2]:=p;
  1215. end;
  1216. else
  1217. internalerror(201108141);
  1218. end;
  1219. end;
  1220. procedure taicpu.SetOperandOrder(order:TOperandOrder);
  1221. begin
  1222. if FOperandOrder<>order then
  1223. begin
  1224. Swapoperands;
  1225. FOperandOrder:=order;
  1226. end;
  1227. end;
  1228. function taicpu.FixNonCommutativeOpcodes: tasmop;
  1229. begin
  1230. result:=opcode;
  1231. { we need ATT order }
  1232. SetOperandOrder(op_att);
  1233. if (
  1234. (ops=2) and
  1235. (oper[0]^.typ=top_reg) and
  1236. (oper[1]^.typ=top_reg) and
  1237. { if the first is ST and the second is also a register
  1238. it is necessarily ST1 .. ST7 }
  1239. ((oper[0]^.reg=NR_ST) or
  1240. (oper[0]^.reg=NR_ST0))
  1241. ) or
  1242. { ((ops=1) and
  1243. (oper[0]^.typ=top_reg) and
  1244. (oper[0]^.reg in [R_ST1..R_ST7])) or}
  1245. (ops=0) then
  1246. begin
  1247. if opcode=A_FSUBR then
  1248. result:=A_FSUB
  1249. else if opcode=A_FSUB then
  1250. result:=A_FSUBR
  1251. else if opcode=A_FDIVR then
  1252. result:=A_FDIV
  1253. else if opcode=A_FDIV then
  1254. result:=A_FDIVR
  1255. else if opcode=A_FSUBRP then
  1256. result:=A_FSUBP
  1257. else if opcode=A_FSUBP then
  1258. result:=A_FSUBRP
  1259. else if opcode=A_FDIVRP then
  1260. result:=A_FDIVP
  1261. else if opcode=A_FDIVP then
  1262. result:=A_FDIVRP;
  1263. end;
  1264. if (
  1265. (ops=1) and
  1266. (oper[0]^.typ=top_reg) and
  1267. (getregtype(oper[0]^.reg)=R_FPUREGISTER) and
  1268. (oper[0]^.reg<>NR_ST)
  1269. ) then
  1270. begin
  1271. if opcode=A_FSUBRP then
  1272. result:=A_FSUBP
  1273. else if opcode=A_FSUBP then
  1274. result:=A_FSUBRP
  1275. else if opcode=A_FDIVRP then
  1276. result:=A_FDIVP
  1277. else if opcode=A_FDIVP then
  1278. result:=A_FDIVRP;
  1279. end;
  1280. end;
  1281. {*****************************************************************************
  1282. Assembler
  1283. *****************************************************************************}
  1284. type
  1285. ea = packed record
  1286. sib_present : boolean;
  1287. bytes : byte;
  1288. size : byte;
  1289. modrm : byte;
  1290. sib : byte;
  1291. {$ifdef x86_64}
  1292. rex : byte;
  1293. {$endif x86_64}
  1294. end;
  1295. procedure taicpu.create_ot(objdata:TObjData);
  1296. {
  1297. this function will also fix some other fields which only needs to be once
  1298. }
  1299. var
  1300. i,l,relsize : longint;
  1301. currsym : TObjSymbol;
  1302. begin
  1303. if ops=0 then
  1304. exit;
  1305. { update oper[].ot field }
  1306. for i:=0 to ops-1 do
  1307. with oper[i]^ do
  1308. begin
  1309. case typ of
  1310. top_reg :
  1311. begin
  1312. ot:=reg_ot_table[findreg_by_number(reg)];
  1313. end;
  1314. top_ref :
  1315. begin
  1316. if (ref^.refaddr in [addr_no{$ifdef x86_64},addr_tpoff{$endif x86_64}{$ifdef i386},addr_ntpoff{$endif i386}])
  1317. {$ifdef i386}
  1318. or (
  1319. (ref^.refaddr in [addr_pic,addr_tlsgd]) and
  1320. ((ref^.base<>NR_NO) or (ref^.index<>NR_NO))
  1321. )
  1322. {$endif i386}
  1323. {$ifdef x86_64}
  1324. or (
  1325. (ref^.refaddr in [addr_pic,addr_pic_no_got,addr_tlsgd]) and
  1326. (ref^.base<>NR_NO)
  1327. )
  1328. {$endif x86_64}
  1329. then
  1330. begin
  1331. { create ot field }
  1332. if (reg_ot_table[findreg_by_number(ref^.base)] and OT_REG_GPR = OT_REG_GPR) and
  1333. ((reg_ot_table[findreg_by_number(ref^.index)] = OT_XMMREG) or
  1334. (reg_ot_table[findreg_by_number(ref^.index)] = OT_YMMREG) or
  1335. (reg_ot_table[findreg_by_number(ref^.index)] = OT_ZMMREG)
  1336. ) then
  1337. // AVX2 - vector-memory-referenz (e.g. vgatherdpd xmm0, [rax xmm1], xmm2)
  1338. ot := (reg_ot_table[findreg_by_number(ref^.base)] and OT_REG_GPR) or
  1339. (reg_ot_table[findreg_by_number(ref^.index)])
  1340. else if (ref^.base = NR_NO) and
  1341. ((reg_ot_table[findreg_by_number(ref^.index)] = OT_XMMREG) or
  1342. (reg_ot_table[findreg_by_number(ref^.index)] = OT_YMMREG) or
  1343. (reg_ot_table[findreg_by_number(ref^.index)] = OT_ZMMREG)
  1344. ) then
  1345. // AVX2 - vector-memory-referenz without base-register (e.g. vgatherdpd xmm0, [xmm1], xmm2)
  1346. ot := (OT_REG_GPR) or
  1347. (reg_ot_table[findreg_by_number(ref^.index)])
  1348. else if (ot and OT_SIZE_MASK)=0 then
  1349. ot:=OT_MEMORY_ANY or opsize_2_type[i,opsize]
  1350. else
  1351. ot:=OT_MEMORY_ANY or (ot and OT_SIZE_MASK);
  1352. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  1353. ot:=ot or OT_MEM_OFFS;
  1354. { fix scalefactor }
  1355. if (ref^.index=NR_NO) then
  1356. ref^.scalefactor:=0
  1357. else
  1358. if (ref^.scalefactor=0) then
  1359. ref^.scalefactor:=1;
  1360. end
  1361. else
  1362. begin
  1363. { Jumps use a relative offset which can be 8bit,
  1364. for other opcodes we always need to generate the full
  1365. 32bit address }
  1366. if assigned(objdata) and
  1367. is_jmp then
  1368. begin
  1369. currsym:=objdata.symbolref(ref^.symbol);
  1370. l:=ref^.offset;
  1371. {$push}
  1372. {$r-,q-} { disable also overflow as address returns a qword for x86_64 }
  1373. if assigned(currsym) then
  1374. inc(l,currsym.address);
  1375. {$pop}
  1376. { when it is a forward jump we need to compensate the
  1377. offset of the instruction since the previous time,
  1378. because the symbol address is then still using the
  1379. 'old-style' addressing.
  1380. For backwards jumps this is not required because the
  1381. address of the symbol is already adjusted to the
  1382. new offset }
  1383. if (l>InsOffset) and (LastInsOffset<>-1) then
  1384. inc(l,InsOffset-LastInsOffset);
  1385. { instruction size will then always become 2 (PFV) }
  1386. relsize:=(InsOffset+2)-l;
  1387. if (relsize>=-128) and (relsize<=127) and
  1388. (
  1389. not assigned(currsym) or
  1390. (currsym.objsection=objdata.currobjsec)
  1391. ) then
  1392. ot:=OT_IMM8 or OT_SHORT
  1393. else
  1394. {$ifdef i8086}
  1395. ot:=OT_IMM16 or OT_NEAR;
  1396. {$else i8086}
  1397. ot:=OT_IMM32 or OT_NEAR;
  1398. {$endif i8086}
  1399. end
  1400. else
  1401. {$ifdef i8086}
  1402. if opsize=S_FAR then
  1403. ot:=OT_IMM16 or OT_FAR
  1404. else
  1405. ot:=OT_IMM16 or OT_NEAR;
  1406. {$else i8086}
  1407. ot:=OT_IMM32 or OT_NEAR;
  1408. {$endif i8086}
  1409. end;
  1410. end;
  1411. top_local :
  1412. begin
  1413. if (ot and OT_SIZE_MASK)=0 then
  1414. ot:=OT_MEMORY or opsize_2_type[i,opsize]
  1415. else
  1416. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  1417. end;
  1418. top_const :
  1419. begin
  1420. // if opcode is a SSE or AVX-instruction then we need a
  1421. // special handling (opsize can different from const-size)
  1422. // (e.g. "pextrw reg/m16, xmmreg, imm8" =>> opsize (16 bit), const-size (8 bit)
  1423. if (InsTabMemRefSizeInfoCache^[opcode].ExistsSSEAVX) and
  1424. (not(InsTabMemRefSizeInfoCache^[opcode].ConstSize in [csiMultiple, csiUnknown])) then
  1425. begin
  1426. case InsTabMemRefSizeInfoCache^[opcode].ConstSize of
  1427. csiNoSize: ot := ot and OT_NON_SIZE or OT_IMMEDIATE;
  1428. csiMem8: ot := ot and OT_NON_SIZE or OT_IMMEDIATE or OT_BITS8;
  1429. csiMem16: ot := ot and OT_NON_SIZE or OT_IMMEDIATE or OT_BITS16;
  1430. csiMem32: ot := ot and OT_NON_SIZE or OT_IMMEDIATE or OT_BITS32;
  1431. csiMem64: ot := ot and OT_NON_SIZE or OT_IMMEDIATE or OT_BITS64;
  1432. else
  1433. ;
  1434. end;
  1435. end
  1436. else
  1437. begin
  1438. { allow 2nd, 3rd or 4th operand being a constant and expect no size for shuf* etc. }
  1439. { further, allow AAD and AAM with imm. operand }
  1440. if (opsize=S_NO) and not((i in [1,2,3])
  1441. {$ifndef x86_64}
  1442. or ((i=0) and (opcode in [A_AAD,A_AAM]))
  1443. {$endif x86_64}
  1444. ) then
  1445. message(asmr_e_invalid_opcode_and_operand);
  1446. if
  1447. {$ifdef i8086}
  1448. (longint(val)>=-128) and (val<=127) then
  1449. {$else i8086}
  1450. (opsize<>S_W) and
  1451. (aint(val)>=-128) and (val<=127) then
  1452. {$endif not i8086}
  1453. ot:=OT_IMM8 or OT_SIGNED
  1454. else
  1455. ot:=OT_IMMEDIATE or opsize_2_type[i,opsize];
  1456. if (val=1) and (i=1) then
  1457. ot := ot or OT_ONENESS;
  1458. end;
  1459. end;
  1460. top_none :
  1461. begin
  1462. { generated when there was an error in the
  1463. assembler reader. It never happends when generating
  1464. assembler }
  1465. end;
  1466. else
  1467. internalerror(200402266);
  1468. end;
  1469. end;
  1470. end;
  1471. function taicpu.InsEnd:longint;
  1472. begin
  1473. InsEnd:=InsOffset+InsSize;
  1474. end;
  1475. function taicpu.Matches(p:PInsEntry):boolean;
  1476. { * IF_SM stands for Size Match: any operand whose size is not
  1477. * explicitly specified by the template is `really' intended to be
  1478. * the same size as the first size-specified operand.
  1479. * Non-specification is tolerated in the input instruction, but
  1480. * _wrong_ specification is not.
  1481. *
  1482. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  1483. * three-operand instructions such as SHLD: it implies that the
  1484. * first two operands must match in size, but that the third is
  1485. * required to be _unspecified_.
  1486. *
  1487. * IF_SB invokes Size Byte: operands with unspecified size in the
  1488. * template are really bytes, and so no non-byte specification in
  1489. * the input instruction will be tolerated. IF_SW similarly invokes
  1490. * Size Word, and IF_SD invokes Size Doubleword.
  1491. *
  1492. * (The default state if neither IF_SM nor IF_SM2 is specified is
  1493. * that any operand with unspecified size in the template is
  1494. * required to have unspecified size in the instruction too...)
  1495. }
  1496. var
  1497. insot,
  1498. currot: int64;
  1499. i,j,asize,oprs : longint;
  1500. insflags:tinsflags;
  1501. vopext: int64;
  1502. siz : array[0..max_operands-1] of longint;
  1503. begin
  1504. result:=false;
  1505. { Check the opcode and operands }
  1506. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  1507. exit;
  1508. {$ifdef i8086}
  1509. { On i8086, we need to skip the i386+ version of Jcc near, if the target
  1510. cpu is earlier than 386. There's another entry, later in the table for
  1511. i8086, which simulates it with i8086 instructions:
  1512. JNcc short +3
  1513. JMP near target }
  1514. if (p^.opcode=A_Jcc) and (current_settings.cputype<cpu_386) and
  1515. (IF_386 in p^.flags) then
  1516. exit;
  1517. {$endif i8086}
  1518. for i:=0 to p^.ops-1 do
  1519. begin
  1520. insot:=p^.optypes[i];
  1521. currot:=oper[i]^.ot;
  1522. { Check the operand flags }
  1523. if (insot and (not currot) and OT_NON_SIZE)<>0 then
  1524. exit;
  1525. // IGNORE VECTOR-MEMORY-SIZE
  1526. if insot and OT_TYPE_MASK = OT_MEMORY then
  1527. insot := insot and not(int64(OT_BITS128 or OT_BITS256 or OT_BITS512));
  1528. { Check if the passed operand size matches with one of
  1529. the supported operand sizes }
  1530. if ((insot and OT_SIZE_MASK)<>0) and
  1531. ((insot and currot and OT_SIZE_MASK)<>(currot and OT_SIZE_MASK)) then
  1532. exit;
  1533. { "far" matches only with "far" }
  1534. if (insot and OT_FAR)<>(currot and OT_FAR) then
  1535. exit;
  1536. end;
  1537. { Check operand sizes }
  1538. insflags:=p^.flags;
  1539. if (insflags*IF_SMASK)<>[] then
  1540. begin
  1541. { as default an untyped size can get all the sizes, this is different
  1542. from nasm, but else we need to do a lot checking which opcodes want
  1543. size or not with the automatic size generation }
  1544. asize:=-1;
  1545. if IF_SB in insflags then
  1546. asize:=OT_BITS8
  1547. else if IF_SW in insflags then
  1548. asize:=OT_BITS16
  1549. else if IF_SD in insflags then
  1550. asize:=OT_BITS32;
  1551. if insflags*IF_ARMASK<>[] then
  1552. begin
  1553. siz[0]:=-1;
  1554. siz[1]:=-1;
  1555. siz[2]:=-1;
  1556. if IF_AR0 in insflags then
  1557. siz[0]:=asize
  1558. else if IF_AR1 in insflags then
  1559. siz[1]:=asize
  1560. else if IF_AR2 in insflags then
  1561. siz[2]:=asize
  1562. else
  1563. internalerror(2017092101);
  1564. end
  1565. else
  1566. begin
  1567. siz[0]:=asize;
  1568. siz[1]:=asize;
  1569. siz[2]:=asize;
  1570. end;
  1571. if insflags*[IF_SM,IF_SM2]<>[] then
  1572. begin
  1573. if IF_SM2 in insflags then
  1574. oprs:=2
  1575. else
  1576. oprs:=p^.ops;
  1577. for i:=0 to oprs-1 do
  1578. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  1579. begin
  1580. for j:=0 to oprs-1 do
  1581. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  1582. break;
  1583. end;
  1584. end
  1585. else
  1586. oprs:=2;
  1587. { Check operand sizes }
  1588. for i:=0 to p^.ops-1 do
  1589. begin
  1590. insot:=p^.optypes[i];
  1591. currot:=oper[i]^.ot;
  1592. if ((insot and OT_SIZE_MASK)=0) and
  1593. ((currot and OT_SIZE_MASK and (not siz[i]))<>0) and
  1594. { Immediates can always include smaller size }
  1595. ((currot and OT_IMMEDIATE)=0) and
  1596. (((insot and OT_SIZE_MASK) or siz[i])<(currot and OT_SIZE_MASK)) then
  1597. exit;
  1598. if (insot and OT_FAR)<>(currot and OT_FAR) then
  1599. exit;
  1600. end;
  1601. end;
  1602. if (InsTabMemRefSizeInfoCache^[opcode].MemRefSize in MemRefMultiples) and
  1603. (InsTabMemRefSizeInfoCache^[opcode].ExistsSSEAVX) then
  1604. begin
  1605. for i:=0 to p^.ops-1 do
  1606. begin
  1607. insot:=p^.optypes[i];
  1608. if ((insot and (OT_XMMRM or OT_REG_EXTRA_MASK)) = OT_XMMRM) OR
  1609. ((insot and (OT_YMMRM or OT_REG_EXTRA_MASK)) = OT_YMMRM) OR
  1610. ((insot and (OT_ZMMRM or OT_REG_EXTRA_MASK)) = OT_ZMMRM) then
  1611. begin
  1612. if (insot and OT_SIZE_MASK) = 0 then
  1613. begin
  1614. case insot and (OT_XMMRM or OT_YMMRM or OT_ZMMRM or OT_REG_EXTRA_MASK) of
  1615. OT_XMMRM: insot := insot or OT_BITS128;
  1616. OT_YMMRM: insot := insot or OT_BITS256;
  1617. OT_ZMMRM: insot := insot or OT_BITS512;
  1618. else
  1619. ;
  1620. end;
  1621. end;
  1622. end;
  1623. currot:=oper[i]^.ot;
  1624. { Check the operand flags }
  1625. if (insot and (not currot) and OT_NON_SIZE)<>0 then
  1626. exit;
  1627. { Check if the passed operand size matches with one of
  1628. the supported operand sizes }
  1629. if ((insot and OT_SIZE_MASK)<>0) and
  1630. ((insot and currot and OT_SIZE_MASK)<>(currot and OT_SIZE_MASK)) then
  1631. exit;
  1632. end;
  1633. end;
  1634. if (InsTabMemRefSizeInfoCache^[opcode].ExistsSSEAVX) then
  1635. begin
  1636. for i:=0 to p^.ops-1 do
  1637. begin
  1638. // check vectoroperand-extention e.g. {k1} {z}
  1639. vopext := 0;
  1640. if (oper[i]^.vopext and OTVE_VECTOR_WRITEMASK) = OTVE_VECTOR_WRITEMASK then
  1641. begin
  1642. vopext := vopext or OT_VECTORMASK;
  1643. if (oper[i]^.vopext and OTVE_VECTOR_ZERO) = OTVE_VECTOR_ZERO then
  1644. vopext := vopext or OT_VECTORZERO;
  1645. end;
  1646. if (oper[i]^.vopext and OTVE_VECTOR_BCST) = OTVE_VECTOR_BCST then
  1647. begin
  1648. vopext := vopext or OT_VECTORBCST;
  1649. if (InsTabMemRefSizeInfoCache^[opcode].BCSTTypes <> []) then
  1650. begin
  1651. // any opcodes needs a special handling
  1652. // default broadcast calculation is
  1653. // bmem32
  1654. // xmmreg: {1to4}
  1655. // ymmreg: {1to8}
  1656. // zmmreg: {1to16}
  1657. // bmem64
  1658. // xmmreg: {1to2}
  1659. // ymmreg: {1to4}
  1660. // zmmreg: {1to8}
  1661. // in any opcodes not exists a mmregister
  1662. // e.g. vfpclasspd k1, [RAX] {1to8}, 0
  1663. // =>> check flags
  1664. case oper[i]^.vopext and (OTVE_VECTOR_BCST2 or OTVE_VECTOR_BCST4 or OTVE_VECTOR_BCST8 or OTVE_VECTOR_BCST16) of
  1665. OTVE_VECTOR_BCST2: if not(IF_BCST2 in p^.flags) then exit;
  1666. OTVE_VECTOR_BCST4: if not(IF_BCST4 in p^.flags) then exit;
  1667. OTVE_VECTOR_BCST8: if not(IF_BCST8 in p^.flags) then exit;
  1668. OTVE_VECTOR_BCST16: if not(IF_BCST16 in p^.flags) then exit;
  1669. else exit;
  1670. end;
  1671. end;
  1672. end;
  1673. if (oper[i]^.vopext and OTVE_VECTOR_ER) = OTVE_VECTOR_ER then
  1674. vopext := vopext or OT_VECTORER;
  1675. if (oper[i]^.vopext and OTVE_VECTOR_SAE) = OTVE_VECTOR_SAE then
  1676. vopext := vopext or OT_VECTORSAE;
  1677. if p^.optypes[i] and vopext <> vopext then
  1678. exit;
  1679. end;
  1680. end;
  1681. result:=true;
  1682. end;
  1683. procedure taicpu.ResetPass1;
  1684. begin
  1685. { we need to reset everything here, because the choosen insentry
  1686. can be invalid for a new situation where the previously optimized
  1687. insentry is not correct }
  1688. InsEntry:=nil;
  1689. InsSize:=0;
  1690. LastInsOffset:=-1;
  1691. end;
  1692. procedure taicpu.ResetPass2;
  1693. begin
  1694. { we are here in a second pass, check if the instruction can be optimized }
  1695. if assigned(InsEntry) and
  1696. (IF_PASS2 in InsEntry^.flags) then
  1697. begin
  1698. InsEntry:=nil;
  1699. InsSize:=0;
  1700. end;
  1701. LastInsOffset:=-1;
  1702. end;
  1703. function taicpu.CheckIfValid:boolean;
  1704. begin
  1705. result:=FindInsEntry(nil);
  1706. end;
  1707. function taicpu.FindInsentry(objdata:TObjData):boolean;
  1708. var
  1709. i : longint;
  1710. begin
  1711. result:=false;
  1712. { Things which may only be done once, not when a second pass is done to
  1713. optimize }
  1714. if (Insentry=nil) or (IF_PASS2 in InsEntry^.flags) then
  1715. begin
  1716. current_filepos:=fileinfo;
  1717. { We need intel style operands }
  1718. SetOperandOrder(op_intel);
  1719. { create the .ot fields }
  1720. create_ot(objdata);
  1721. { set the file postion }
  1722. end
  1723. else
  1724. begin
  1725. { we've already an insentry so it's valid }
  1726. result:=true;
  1727. exit;
  1728. end;
  1729. { Lookup opcode in the table }
  1730. InsSize:=-1;
  1731. i:=instabcache^[opcode];
  1732. if i=-1 then
  1733. begin
  1734. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  1735. exit;
  1736. end;
  1737. insentry:=@instab[i];
  1738. while (insentry^.opcode=opcode) do
  1739. begin
  1740. if matches(insentry) then
  1741. begin
  1742. result:=true;
  1743. exit;
  1744. end;
  1745. inc(insentry);
  1746. end;
  1747. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1748. { No instruction found, set insentry to nil and inssize to -1 }
  1749. insentry:=nil;
  1750. inssize:=-1;
  1751. end;
  1752. function taicpu.CheckUseEVEX: boolean;
  1753. var
  1754. i: integer;
  1755. begin
  1756. result := false;
  1757. for i := 0 to ops - 1 do
  1758. begin
  1759. if (oper[i]^.typ=top_reg) and
  1760. (getregtype(oper[i]^.reg) = R_MMREGISTER) then
  1761. if getsupreg(oper[i]^.reg)>=16 then
  1762. result := true;
  1763. if (oper[i]^.vopext and OTVE_VECTOR_MASK) <> 0 then
  1764. result := true;
  1765. end;
  1766. end;
  1767. procedure taicpu.CheckEVEXTuple(const aInput:toper; aInsEntry: pInsentry; aIsVector128, aIsVector256, aIsVector512, aIsEVEXW1: boolean);
  1768. var
  1769. i: integer;
  1770. tuplesize: integer;
  1771. memsize: integer;
  1772. begin
  1773. if EVEXTupleState = etsUnknown then
  1774. begin
  1775. EVEXTupleState := etsNotTuple;
  1776. if aInsEntry^.Flags * IF_TUPLEMASK <> [] then
  1777. begin
  1778. tuplesize := 0;
  1779. if IF_TFV in aInsEntry^.Flags then
  1780. begin
  1781. for i := 0 to aInsEntry^.ops - 1 do
  1782. if (aInsEntry^.optypes[i] and OT_BMEM32 = OT_BMEM32) then
  1783. begin
  1784. tuplesize := 4;
  1785. break;
  1786. end
  1787. else if (aInsEntry^.optypes[i] and OT_BMEM64 = OT_BMEM64) then
  1788. begin
  1789. tuplesize := 8;
  1790. break;
  1791. end
  1792. else if (aInsEntry^.optypes[i] and OT_MEMORY = OT_MEMORY) then
  1793. begin
  1794. if aIsVector512 then tuplesize := 64
  1795. else if aIsVector256 then tuplesize := 32
  1796. else tuplesize := 16;
  1797. break;
  1798. end
  1799. else if (aInsEntry^.optypes[i] and OT_REGNORM = OT_REGMEM) then
  1800. begin
  1801. if aIsVector512 then tuplesize := 64
  1802. else if aIsVector256 then tuplesize := 32
  1803. else tuplesize := 16;
  1804. break;
  1805. end;
  1806. end
  1807. else if IF_THV in aInsEntry^.Flags then
  1808. begin
  1809. for i := 0 to aInsEntry^.ops - 1 do
  1810. if (aInsEntry^.optypes[i] and OT_BMEM32 = OT_BMEM32) then
  1811. begin
  1812. tuplesize := 4;
  1813. break;
  1814. end
  1815. else if (aInsEntry^.optypes[i] and OT_REGNORM = OT_REGMEM) then
  1816. begin
  1817. if aIsVector512 then tuplesize := 32
  1818. else if aIsVector256 then tuplesize := 16
  1819. else tuplesize := 8;
  1820. break;
  1821. end
  1822. end
  1823. else if IF_TFVM in aInsEntry^.Flags then
  1824. begin
  1825. if aIsVector512 then tuplesize := 64
  1826. else if aIsVector256 then tuplesize := 32
  1827. else tuplesize := 16;
  1828. end
  1829. else
  1830. begin
  1831. memsize := 0;
  1832. for i := 0 to aInsEntry^.ops - 1 do
  1833. begin
  1834. if aInsEntry^.optypes[i] and (OT_REGNORM or OT_MEMORY) = OT_REGMEM then
  1835. begin
  1836. case aInsEntry^.optypes[i] and (OT_BITS32 or OT_BITS64) of
  1837. OT_BITS32: begin
  1838. memsize := 32;
  1839. break;
  1840. end;
  1841. OT_BITS64: begin
  1842. memsize := 64;
  1843. break;
  1844. end;
  1845. end;
  1846. end
  1847. else
  1848. case aInsEntry^.optypes[i] and (OT_MEM8 or OT_MEM16 or OT_MEM32 or OT_MEM64) of
  1849. OT_MEM8: begin
  1850. memsize := 8;
  1851. break;
  1852. end;
  1853. OT_MEM16: begin
  1854. memsize := 16;
  1855. break;
  1856. end;
  1857. OT_MEM32: begin
  1858. memsize := 32;
  1859. break;
  1860. end;
  1861. OT_MEM64: //if aIsEVEXW1 then
  1862. begin
  1863. memsize := 64;
  1864. break;
  1865. end;
  1866. end;
  1867. end;
  1868. if IF_T1S in aInsEntry^.Flags then
  1869. begin
  1870. case memsize of
  1871. 8: tuplesize := 1;
  1872. 16: tuplesize := 2;
  1873. else if aIsEVEXW1 then tuplesize := 8
  1874. else tuplesize := 4;
  1875. end;
  1876. end
  1877. else if IF_T1S8 in aInsEntry^.Flags then tuplesize := 1
  1878. else if IF_T1S16 in aInsEntry^.Flags then tuplesize := 2
  1879. else if IF_T1F32 in aInsEntry^.Flags then tuplesize := 4
  1880. else if IF_T1F64 in aInsEntry^.Flags then tuplesize := 8
  1881. else if IF_T2 in aInsEntry^.Flags then
  1882. begin
  1883. case aIsEVEXW1 of
  1884. false: tuplesize := 8;
  1885. else if aIsVector256 or aIsVector512 then tuplesize := 16;
  1886. end;
  1887. end
  1888. else if IF_T4 in aInsEntry^.Flags then
  1889. begin
  1890. case aIsEVEXW1 of
  1891. false: if aIsVector256 or aIsVector512 then tuplesize := 16;
  1892. else if aIsVector512 then tuplesize := 32;
  1893. end;
  1894. end
  1895. else if IF_T8 in aInsEntry^.Flags then
  1896. begin
  1897. case aIsEVEXW1 of
  1898. false: if aIsVector512 then tuplesize := 32;
  1899. else
  1900. Internalerror(2019081013);
  1901. end;
  1902. end
  1903. else if IF_THVM in aInsEntry^.Flags then
  1904. begin
  1905. tuplesize := 8; // default 128bit-vectorlength
  1906. if aIsVector256 then tuplesize := 16
  1907. else if aIsVector512 then tuplesize := 32;
  1908. end
  1909. else if IF_TQVM in aInsEntry^.Flags then
  1910. begin
  1911. tuplesize := 4; // default 128bit-vectorlength
  1912. if aIsVector256 then tuplesize := 8
  1913. else if aIsVector512 then tuplesize := 16;
  1914. end
  1915. else if IF_TOVM in aInsEntry^.Flags then
  1916. begin
  1917. tuplesize := 2; // default 128bit-vectorlength
  1918. if aIsVector256 then tuplesize := 4
  1919. else if aIsVector512 then tuplesize := 8;
  1920. end
  1921. else if IF_TMEM128 in aInsEntry^.Flags then tuplesize := 16
  1922. else if IF_TMDDUP in aInsEntry^.Flags then
  1923. begin
  1924. tuplesize := 8; // default 128bit-vectorlength
  1925. if aIsVector256 then tuplesize := 32
  1926. else if aIsVector512 then tuplesize := 64;
  1927. end;
  1928. end;
  1929. if tuplesize > 0 then
  1930. begin
  1931. if aInput.typ = top_ref then
  1932. begin
  1933. if aInput.ref^.base <> NR_NO then
  1934. begin
  1935. if (aInput.ref^.offset <> 0) and
  1936. ((aInput.ref^.offset mod tuplesize) = 0) and
  1937. (abs(aInput.ref^.offset) div tuplesize <= 127) then
  1938. begin
  1939. aInput.ref^.offset := aInput.ref^.offset div tuplesize;
  1940. EVEXTupleState := etsIsTuple;
  1941. end;
  1942. end;
  1943. end;
  1944. end;
  1945. end;
  1946. end;
  1947. end;
  1948. function taicpu.Pass1(objdata:TObjData):longint;
  1949. begin
  1950. Pass1:=0;
  1951. { Save the old offset and set the new offset }
  1952. InsOffset:=ObjData.CurrObjSec.Size;
  1953. { Error? }
  1954. if (Insentry=nil) and (InsSize=-1) then
  1955. exit;
  1956. { set the file postion }
  1957. current_filepos:=fileinfo;
  1958. { Get InsEntry }
  1959. if FindInsEntry(ObjData) then
  1960. begin
  1961. { Calculate instruction size }
  1962. InsSize:=calcsize(insentry);
  1963. if segprefix<>NR_NO then
  1964. inc(InsSize);
  1965. if NeedAddrPrefix then
  1966. inc(InsSize);
  1967. { Fix opsize if size if forced }
  1968. if insentry^.flags*[IF_SB,IF_SW,IF_SD]<>[] then
  1969. begin
  1970. if insentry^.flags*IF_ARMASK=[] then
  1971. begin
  1972. if IF_SB in insentry^.flags then
  1973. begin
  1974. if opsize=S_NO then
  1975. opsize:=S_B;
  1976. end
  1977. else if IF_SW in insentry^.flags then
  1978. begin
  1979. if opsize=S_NO then
  1980. opsize:=S_W;
  1981. end
  1982. else if IF_SD in insentry^.flags then
  1983. begin
  1984. if opsize=S_NO then
  1985. opsize:=S_L;
  1986. end;
  1987. end;
  1988. end;
  1989. LastInsOffset:=InsOffset;
  1990. Pass1:=InsSize;
  1991. exit;
  1992. end;
  1993. LastInsOffset:=-1;
  1994. end;
  1995. const
  1996. segprefixes: array[NR_ES..NR_GS] of Byte=(
  1997. // es cs ss ds fs gs
  1998. $26, $2E, $36, $3E, $64, $65
  1999. );
  2000. procedure taicpu.Pass2(objdata:TObjData);
  2001. begin
  2002. { error in pass1 ? }
  2003. if insentry=nil then
  2004. exit;
  2005. current_filepos:=fileinfo;
  2006. { Segment override }
  2007. if (segprefix>=NR_ES) and (segprefix<=NR_GS) then
  2008. begin
  2009. {$ifdef i8086}
  2010. if (objdata.CPUType<>cpu_none) and (objdata.CPUType<cpu_386) and
  2011. ((segprefix=NR_FS) or (segprefix=NR_GS)) then
  2012. Message(asmw_e_instruction_not_supported_by_cpu);
  2013. {$endif i8086}
  2014. objdata.writebytes(segprefixes[segprefix],1);
  2015. { fix the offset for GenNode }
  2016. inc(InsOffset);
  2017. end
  2018. else if segprefix<>NR_NO then
  2019. InternalError(201001071);
  2020. { Address size prefix? }
  2021. if NeedAddrPrefix then
  2022. begin
  2023. write0x67prefix(objdata);
  2024. { fix the offset for GenNode }
  2025. inc(InsOffset);
  2026. end;
  2027. { Generate the instruction }
  2028. GenCode(objdata);
  2029. end;
  2030. function is_64_bit_ref(const ref:treference):boolean;
  2031. begin
  2032. {$if defined(x86_64)}
  2033. result:=not is_32_bit_ref(ref);
  2034. {$elseif defined(i386) or defined(i8086)}
  2035. result:=false;
  2036. {$endif}
  2037. end;
  2038. function is_32_bit_ref(const ref:treference):boolean;
  2039. begin
  2040. {$if defined(x86_64)}
  2041. result:=(ref.refaddr=addr_no) and
  2042. (ref.base<>NR_RIP) and
  2043. (
  2044. ((ref.index<>NR_NO) and (getsubreg(ref.index)=R_SUBD)) or
  2045. ((ref.base<>NR_NO) and (getsubreg(ref.base)=R_SUBD))
  2046. );
  2047. {$elseif defined(i386) or defined(i8086)}
  2048. result:=not is_16_bit_ref(ref);
  2049. {$endif}
  2050. end;
  2051. function is_16_bit_ref(const ref:treference):boolean;
  2052. var
  2053. ir,br : Tregister;
  2054. isub,bsub : tsubregister;
  2055. begin
  2056. if (ref.index<>NR_NO) and (getregtype(ref.index)=R_MMREGISTER) then
  2057. exit(false);
  2058. ir:=ref.index;
  2059. br:=ref.base;
  2060. isub:=getsubreg(ir);
  2061. bsub:=getsubreg(br);
  2062. { it's a direct address }
  2063. if (br=NR_NO) and (ir=NR_NO) then
  2064. begin
  2065. {$ifdef i8086}
  2066. result:=true;
  2067. {$else i8086}
  2068. result:=false;
  2069. {$endif}
  2070. end
  2071. else
  2072. { it's an indirection }
  2073. begin
  2074. result := ((ir<>NR_NO) and (isub=R_SUBW)) or
  2075. ((br<>NR_NO) and (bsub=R_SUBW));
  2076. end;
  2077. end;
  2078. function get_ref_address_size(const ref:treference):byte;
  2079. begin
  2080. if is_64_bit_ref(ref) then
  2081. result:=64
  2082. else if is_32_bit_ref(ref) then
  2083. result:=32
  2084. else if is_16_bit_ref(ref) then
  2085. result:=16
  2086. else
  2087. internalerror(2017101601);
  2088. end;
  2089. function get_default_segment_of_ref(const ref:treference):tregister;
  2090. begin
  2091. { for 16-bit registers, we allow base and index to be swapped, that's
  2092. why we also we check whether ref.index=NR_BP. For 32-bit registers,
  2093. however, index=NR_EBP is encoded differently than base=NR_EBP and has
  2094. a different default segment. }
  2095. if (ref.base=NR_BP) or (ref.index=NR_BP) or
  2096. (ref.base=NR_EBP) or (ref.base=NR_ESP)
  2097. {$ifdef x86_64}
  2098. or (ref.base=NR_RBP) or (ref.base=NR_RSP)
  2099. {$endif x86_64}
  2100. then
  2101. result:=NR_SS
  2102. else
  2103. result:=NR_DS;
  2104. end;
  2105. procedure optimize_ref(var ref:treference; inlineasm: boolean);
  2106. var
  2107. ss_equals_ds: boolean;
  2108. tmpreg: TRegister;
  2109. begin
  2110. {$ifdef x86_64}
  2111. { x86_64 in long mode ignores all segment base, limit and access rights
  2112. checks for the DS, ES and SS registers, so we can set ss_equals_ds to
  2113. true (and thus, perform stronger optimizations on the reference),
  2114. regardless of whether this is inline asm or not (so, even if the user
  2115. is doing tricks by loading different values into DS and SS, it still
  2116. doesn't matter while the processor is in long mode) }
  2117. ss_equals_ds:=True;
  2118. {$else x86_64}
  2119. { for i8086 and i386 inline asm, we assume SS<>DS, even if we're
  2120. compiling for a memory model, where SS=DS, because the user might be
  2121. doing something tricky with the segment registers (and may have
  2122. temporarily set them differently) }
  2123. if inlineasm then
  2124. ss_equals_ds:=False
  2125. else
  2126. ss_equals_ds:=segment_regs_equal(NR_DS,NR_SS);
  2127. {$endif x86_64}
  2128. { remove redundant segment overrides }
  2129. if (ref.segment<>NR_NO) and
  2130. ((inlineasm and (ref.segment=get_default_segment_of_ref(ref))) or
  2131. ((not inlineasm) and (segment_regs_equal(ref.segment,get_default_segment_of_ref(ref))))) then
  2132. ref.segment:=NR_NO;
  2133. if not is_16_bit_ref(ref) then
  2134. begin
  2135. { Switching index to base position gives shorter assembler instructions.
  2136. Converting index*2 to base+index also gives shorter instructions. }
  2137. if (ref.base=NR_NO) and (ref.index<>NR_NO) and (ref.scalefactor<=2) and
  2138. (ss_equals_ds or (ref.segment<>NR_NO) or (ref.index<>NR_EBP))
  2139. { do not mess with tls references, they have the (,reg,1) format on purpose
  2140. else the linker cannot resolve/replace them }
  2141. {$ifdef i386} and (ref.refaddr<>addr_tlsgd) {$endif i386} then
  2142. begin
  2143. ref.base:=ref.index;
  2144. if ref.scalefactor=2 then
  2145. ref.scalefactor:=1
  2146. else
  2147. begin
  2148. ref.index:=NR_NO;
  2149. ref.scalefactor:=0;
  2150. end;
  2151. end;
  2152. { Switching rBP+reg to reg+rBP sometimes gives shorter instructions (if there's no offset)
  2153. On x86_64 this also works for switching r13+reg to reg+r13. }
  2154. 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
  2155. (ref.index<>NR_NO) and
  2156. (ref.index<>NR_EBP) and {$ifdef x86_64}(ref.index<>NR_RBP) and (ref.index<>NR_R13) and (ref.index<>NR_R13D) and{$endif}
  2157. (ref.scalefactor<=1) and (ref.offset=0) and (ref.refaddr=addr_no) and
  2158. (ss_equals_ds or (ref.segment<>NR_NO)) then
  2159. begin
  2160. tmpreg:=ref.base;
  2161. ref.base:=ref.index;
  2162. ref.index:=tmpreg;
  2163. end;
  2164. end;
  2165. { remove redundant segment overrides again }
  2166. if (ref.segment<>NR_NO) and
  2167. ((inlineasm and (ref.segment=get_default_segment_of_ref(ref))) or
  2168. ((not inlineasm) and (segment_regs_equal(ref.segment,get_default_segment_of_ref(ref))))) then
  2169. ref.segment:=NR_NO;
  2170. end;
  2171. function taicpu.NeedAddrPrefix(opidx: byte): boolean;
  2172. begin
  2173. {$if defined(x86_64)}
  2174. result:=(oper[opidx]^.typ=top_ref) and is_32_bit_ref(oper[opidx]^.ref^);
  2175. {$elseif defined(i386)}
  2176. result:=(oper[opidx]^.typ=top_ref) and is_16_bit_ref(oper[opidx]^.ref^);
  2177. {$elseif defined(i8086)}
  2178. result:=(oper[opidx]^.typ=top_ref) and is_32_bit_ref(oper[opidx]^.ref^);
  2179. {$endif}
  2180. end;
  2181. function taicpu.NeedAddrPrefix:boolean;
  2182. var
  2183. i: Integer;
  2184. begin
  2185. for i:=0 to ops-1 do
  2186. if needaddrprefix(i) then
  2187. exit(true);
  2188. result:=false;
  2189. end;
  2190. procedure badreg(r:Tregister);
  2191. begin
  2192. Message1(asmw_e_invalid_register,generic_regname(r));
  2193. end;
  2194. function regval(r:Tregister):byte;
  2195. const
  2196. intsupreg2opcode: array[0..7] of byte=
  2197. // ax cx dx bx si di bp sp -- in x86reg.dat
  2198. // ax cx dx bx sp bp si di -- needed order
  2199. (0, 1, 2, 3, 6, 7, 5, 4);
  2200. maxsupreg: array[tregistertype] of tsuperregister=
  2201. {$ifdef x86_64}
  2202. (0, 16, 9, 8, 32, 32, 8, 0, 0, 0);
  2203. {$else x86_64}
  2204. (0, 8, 9, 8, 8, 32, 8, 0, 0, 0);
  2205. {$endif x86_64}
  2206. var
  2207. rs: tsuperregister;
  2208. rt: tregistertype;
  2209. begin
  2210. rs:=getsupreg(r);
  2211. rt:=getregtype(r);
  2212. if (rs>=maxsupreg[rt]) then
  2213. badreg(r);
  2214. result:=rs and 7;
  2215. if (rt=R_INTREGISTER) then
  2216. begin
  2217. if (rs<8) then
  2218. result:=intsupreg2opcode[rs];
  2219. if getsubreg(r)=R_SUBH then
  2220. inc(result,4);
  2221. end;
  2222. end;
  2223. {$if defined(x86_64)}
  2224. function rexbits(r: tregister): byte;
  2225. begin
  2226. result:=0;
  2227. case getregtype(r) of
  2228. R_INTREGISTER:
  2229. if (getsupreg(r)>=RS_R8) then
  2230. { Either B,X or R bits can be set, depending on register role in instruction.
  2231. Set all three bits here, caller will discard unnecessary ones. }
  2232. result:=result or $47
  2233. else if (getsubreg(r)=R_SUBL) and
  2234. (getsupreg(r) in [RS_RDI,RS_RSI,RS_RBP,RS_RSP]) then
  2235. result:=result or $40
  2236. else if (getsubreg(r)=R_SUBH) then
  2237. { Not an actual REX bit, used to detect incompatible usage of
  2238. AH/BH/CH/DH }
  2239. result:=result or $80;
  2240. R_MMREGISTER:
  2241. //if getsupreg(r)>=RS_XMM8 then
  2242. // AVX512 = 32 register
  2243. // rexbit = 0 => MMRegister 0..7 or 16..23
  2244. // rexbit = 1 => MMRegister 8..15 or 24..31
  2245. if (getsupreg(r) and $08) = $08 then
  2246. result:=result or $47;
  2247. else
  2248. ;
  2249. end;
  2250. end;
  2251. function process_ea_ref_64_32(const input:toper;var output:ea;rfield:longint; uselargeoffset: boolean):boolean;
  2252. var
  2253. sym : tasmsymbol;
  2254. md,s : byte;
  2255. base,index,scalefactor,
  2256. o : longint;
  2257. ir,br : Tregister;
  2258. isub,bsub : tsubregister;
  2259. begin
  2260. result:=false;
  2261. ir:=input.ref^.index;
  2262. br:=input.ref^.base;
  2263. isub:=getsubreg(ir);
  2264. bsub:=getsubreg(br);
  2265. s:=input.ref^.scalefactor;
  2266. o:=input.ref^.offset;
  2267. sym:=input.ref^.symbol;
  2268. //if ((ir<>NR_NO) and (getregtype(ir)<>R_INTREGISTER)) or
  2269. // ((br<>NR_NO) and (br<>NR_RIP) and (getregtype(br)<>R_INTREGISTER)) then
  2270. if ((ir<>NR_NO) and (getregtype(ir)=R_MMREGISTER) and (br<>NR_NO) and (getregtype(br)<>R_INTREGISTER)) or // vector memory (AVX2)
  2271. ((ir<>NR_NO) and (getregtype(ir)<>R_INTREGISTER) and (getregtype(ir)<>R_MMREGISTER)) or
  2272. ((br<>NR_NO) and (br<>NR_RIP) and (getregtype(br)<>R_INTREGISTER)) then
  2273. internalerror(200301081);
  2274. { it's direct address }
  2275. if (br=NR_NO) and (ir=NR_NO) then
  2276. begin
  2277. output.sib_present:=true;
  2278. output.bytes:=4;
  2279. output.modrm:=4 or (rfield shl 3);
  2280. output.sib:=$25;
  2281. end
  2282. else if (br=NR_RIP) and (ir=NR_NO) then
  2283. begin
  2284. { rip based }
  2285. output.sib_present:=false;
  2286. output.bytes:=4;
  2287. output.modrm:=5 or (rfield shl 3);
  2288. end
  2289. else
  2290. { it's an indirection }
  2291. begin
  2292. if ((br=NR_RIP) and (ir<>NR_NO)) or
  2293. (ir=NR_RIP) then
  2294. message(asmw_e_illegal_use_of_rip);
  2295. { 16 bit? }
  2296. if ((ir<>NR_NO) and (isub in [R_SUBMMX,R_SUBMMY,R_SUBMMZ]) and
  2297. (br<>NR_NO) and (bsub=R_SUBQ)
  2298. ) then
  2299. begin
  2300. // vector memory (AVX2) =>> ignore
  2301. end
  2302. else if ((ir<>NR_NO) and (isub<>R_SUBQ) and (isub<>R_SUBD)) or
  2303. ((br<>NR_NO) and (bsub<>R_SUBQ) and (bsub<>R_SUBD)) then
  2304. begin
  2305. message(asmw_e_16bit_32bit_not_supported);
  2306. end;
  2307. { wrong, for various reasons }
  2308. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  2309. exit;
  2310. output.rex:=output.rex or (rexbits(br) and $F1) or (rexbits(ir) and $F2);
  2311. result:=true;
  2312. { base }
  2313. case br of
  2314. NR_R8D,
  2315. NR_EAX,
  2316. NR_R8,
  2317. NR_RAX : base:=0;
  2318. NR_R9D,
  2319. NR_ECX,
  2320. NR_R9,
  2321. NR_RCX : base:=1;
  2322. NR_R10D,
  2323. NR_EDX,
  2324. NR_R10,
  2325. NR_RDX : base:=2;
  2326. NR_R11D,
  2327. NR_EBX,
  2328. NR_R11,
  2329. NR_RBX : base:=3;
  2330. NR_R12D,
  2331. NR_ESP,
  2332. NR_R12,
  2333. NR_RSP : base:=4;
  2334. NR_R13D,
  2335. NR_EBP,
  2336. NR_R13,
  2337. NR_NO,
  2338. NR_RBP : base:=5;
  2339. NR_R14D,
  2340. NR_ESI,
  2341. NR_R14,
  2342. NR_RSI : base:=6;
  2343. NR_R15D,
  2344. NR_EDI,
  2345. NR_R15,
  2346. NR_RDI : base:=7;
  2347. else
  2348. exit;
  2349. end;
  2350. { index }
  2351. case ir of
  2352. NR_R8D,
  2353. NR_EAX,
  2354. NR_R8,
  2355. NR_RAX,
  2356. NR_XMM0,
  2357. NR_XMM8,
  2358. NR_XMM16,
  2359. NR_XMM24,
  2360. NR_YMM0,
  2361. NR_YMM8,
  2362. NR_YMM16,
  2363. NR_YMM24,
  2364. NR_ZMM0,
  2365. NR_ZMM8,
  2366. NR_ZMM16,
  2367. NR_ZMM24: index:=0;
  2368. NR_R9D,
  2369. NR_ECX,
  2370. NR_R9,
  2371. NR_RCX,
  2372. NR_XMM1,
  2373. NR_XMM9,
  2374. NR_XMM17,
  2375. NR_XMM25,
  2376. NR_YMM1,
  2377. NR_YMM9,
  2378. NR_YMM17,
  2379. NR_YMM25,
  2380. NR_ZMM1,
  2381. NR_ZMM9,
  2382. NR_ZMM17,
  2383. NR_ZMM25: index:=1;
  2384. NR_R10D,
  2385. NR_EDX,
  2386. NR_R10,
  2387. NR_RDX,
  2388. NR_XMM2,
  2389. NR_XMM10,
  2390. NR_XMM18,
  2391. NR_XMM26,
  2392. NR_YMM2,
  2393. NR_YMM10,
  2394. NR_YMM18,
  2395. NR_YMM26,
  2396. NR_ZMM2,
  2397. NR_ZMM10,
  2398. NR_ZMM18,
  2399. NR_ZMM26: index:=2;
  2400. NR_R11D,
  2401. NR_EBX,
  2402. NR_R11,
  2403. NR_RBX,
  2404. NR_XMM3,
  2405. NR_XMM11,
  2406. NR_XMM19,
  2407. NR_XMM27,
  2408. NR_YMM3,
  2409. NR_YMM11,
  2410. NR_YMM19,
  2411. NR_YMM27,
  2412. NR_ZMM3,
  2413. NR_ZMM11,
  2414. NR_ZMM19,
  2415. NR_ZMM27: index:=3;
  2416. NR_R12D,
  2417. NR_ESP,
  2418. NR_R12,
  2419. NR_NO,
  2420. NR_XMM4,
  2421. NR_XMM12,
  2422. NR_XMM20,
  2423. NR_XMM28,
  2424. NR_YMM4,
  2425. NR_YMM12,
  2426. NR_YMM20,
  2427. NR_YMM28,
  2428. NR_ZMM4,
  2429. NR_ZMM12,
  2430. NR_ZMM20,
  2431. NR_ZMM28: index:=4;
  2432. NR_R13D,
  2433. NR_EBP,
  2434. NR_R13,
  2435. NR_RBP,
  2436. NR_XMM5,
  2437. NR_XMM13,
  2438. NR_XMM21,
  2439. NR_XMM29,
  2440. NR_YMM5,
  2441. NR_YMM13,
  2442. NR_YMM21,
  2443. NR_YMM29,
  2444. NR_ZMM5,
  2445. NR_ZMM13,
  2446. NR_ZMM21,
  2447. NR_ZMM29: index:=5;
  2448. NR_R14D,
  2449. NR_ESI,
  2450. NR_R14,
  2451. NR_RSI,
  2452. NR_XMM6,
  2453. NR_XMM14,
  2454. NR_XMM22,
  2455. NR_XMM30,
  2456. NR_YMM6,
  2457. NR_YMM14,
  2458. NR_YMM22,
  2459. NR_YMM30,
  2460. NR_ZMM6,
  2461. NR_ZMM14,
  2462. NR_ZMM22,
  2463. NR_ZMM30: index:=6;
  2464. NR_R15D,
  2465. NR_EDI,
  2466. NR_R15,
  2467. NR_RDI,
  2468. NR_XMM7,
  2469. NR_XMM15,
  2470. NR_XMM23,
  2471. NR_XMM31,
  2472. NR_YMM7,
  2473. NR_YMM15,
  2474. NR_YMM23,
  2475. NR_YMM31,
  2476. NR_ZMM7,
  2477. NR_ZMM15,
  2478. NR_ZMM23,
  2479. NR_ZMM31: index:=7;
  2480. else
  2481. exit;
  2482. end;
  2483. case s of
  2484. 0,
  2485. 1 : scalefactor:=0;
  2486. 2 : scalefactor:=1;
  2487. 4 : scalefactor:=2;
  2488. 8 : scalefactor:=3;
  2489. else
  2490. exit;
  2491. end;
  2492. { If rbp or r13 is used we must always include an offset }
  2493. if (br=NR_NO) or
  2494. ((br<>NR_RBP) and (br<>NR_R13) and (br<>NR_EBP) and (br<>NR_R13D) and (o=0) and (sym=nil)) then
  2495. md:=0
  2496. else
  2497. if ((o>=-128) and (o<=127) and (sym=nil) and (not(uselargeoffset) or (o = 0))) then
  2498. md:=1
  2499. else
  2500. md:=2;
  2501. if (br=NR_NO) or (md=2) then
  2502. output.bytes:=4
  2503. else
  2504. output.bytes:=md;
  2505. { SIB needed ? }
  2506. if (ir=NR_NO) and (br<>NR_RSP) and (br<>NR_R12) and (br<>NR_ESP) and (br<>NR_R12D) then
  2507. begin
  2508. output.sib_present:=false;
  2509. output.modrm:=(md shl 6) or (rfield shl 3) or base;
  2510. end
  2511. else
  2512. begin
  2513. output.sib_present:=true;
  2514. output.modrm:=(md shl 6) or (rfield shl 3) or 4;
  2515. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  2516. end;
  2517. end;
  2518. output.size:=1+ord(output.sib_present)+output.bytes;
  2519. result:=true;
  2520. end;
  2521. {$elseif defined(i386) or defined(i8086)}
  2522. function process_ea_ref_32(const input:toper;out output:ea;rfield:longint; uselargeoffset: boolean):boolean;
  2523. var
  2524. sym : tasmsymbol;
  2525. md,s : byte;
  2526. base,index,scalefactor,
  2527. o : longint;
  2528. ir,br : Tregister;
  2529. isub,bsub : tsubregister;
  2530. begin
  2531. result:=false;
  2532. 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)
  2533. ((input.ref^.index<>NR_NO) and (getregtype(input.ref^.index)<>R_INTREGISTER) and (getregtype(input.ref^.index)<>R_MMREGISTER)) or
  2534. ((input.ref^.base<>NR_NO) and (getregtype(input.ref^.base)<>R_INTREGISTER)) then
  2535. internalerror(2003010802);
  2536. ir:=input.ref^.index;
  2537. br:=input.ref^.base;
  2538. isub:=getsubreg(ir);
  2539. bsub:=getsubreg(br);
  2540. s:=input.ref^.scalefactor;
  2541. o:=input.ref^.offset;
  2542. sym:=input.ref^.symbol;
  2543. { it's direct address }
  2544. if (br=NR_NO) and (ir=NR_NO) then
  2545. begin
  2546. { it's a pure offset }
  2547. output.sib_present:=false;
  2548. output.bytes:=4;
  2549. output.modrm:=5 or (rfield shl 3);
  2550. end
  2551. else
  2552. { it's an indirection }
  2553. begin
  2554. { 16 bit address? }
  2555. if ((ir<>NR_NO) and (isub in [R_SUBMMX,R_SUBMMY,R_SUBMMZ]) and
  2556. (br<>NR_NO) and (bsub=R_SUBD)
  2557. ) then
  2558. begin
  2559. // vector memory (AVX2) =>> ignore
  2560. end
  2561. else if ((ir<>NR_NO) and (isub<>R_SUBD)) or
  2562. ((br<>NR_NO) and (bsub<>R_SUBD)) then
  2563. message(asmw_e_16bit_not_supported);
  2564. {$ifdef OPTEA}
  2565. { make single reg base }
  2566. if (br=NR_NO) and (s=1) then
  2567. begin
  2568. br:=ir;
  2569. ir:=NR_NO;
  2570. end;
  2571. { convert [3,5,9]*EAX to EAX+[2,4,8]*EAX }
  2572. if (br=NR_NO) and
  2573. (((s=2) and (ir<>NR_ESP)) or
  2574. (s=3) or (s=5) or (s=9)) then
  2575. begin
  2576. br:=ir;
  2577. dec(s);
  2578. end;
  2579. { swap ESP into base if scalefactor is 1 }
  2580. if (s=1) and (ir=NR_ESP) then
  2581. begin
  2582. ir:=br;
  2583. br:=NR_ESP;
  2584. end;
  2585. {$endif OPTEA}
  2586. { wrong, for various reasons }
  2587. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  2588. exit;
  2589. { base }
  2590. case br of
  2591. NR_EAX : base:=0;
  2592. NR_ECX : base:=1;
  2593. NR_EDX : base:=2;
  2594. NR_EBX : base:=3;
  2595. NR_ESP : base:=4;
  2596. NR_NO,
  2597. NR_EBP : base:=5;
  2598. NR_ESI : base:=6;
  2599. NR_EDI : base:=7;
  2600. else
  2601. exit;
  2602. end;
  2603. { index }
  2604. case ir of
  2605. NR_EAX,
  2606. NR_XMM0,
  2607. NR_YMM0,
  2608. NR_ZMM0: index:=0;
  2609. NR_ECX,
  2610. NR_XMM1,
  2611. NR_YMM1,
  2612. NR_ZMM1: index:=1;
  2613. NR_EDX,
  2614. NR_XMM2,
  2615. NR_YMM2,
  2616. NR_ZMM2: index:=2;
  2617. NR_EBX,
  2618. NR_XMM3,
  2619. NR_YMM3,
  2620. NR_ZMM3: index:=3;
  2621. NR_NO,
  2622. NR_XMM4,
  2623. NR_YMM4,
  2624. NR_ZMM4: index:=4;
  2625. NR_EBP,
  2626. NR_XMM5,
  2627. NR_YMM5,
  2628. NR_ZMM5: index:=5;
  2629. NR_ESI,
  2630. NR_XMM6,
  2631. NR_YMM6,
  2632. NR_ZMM6: index:=6;
  2633. NR_EDI,
  2634. NR_XMM7,
  2635. NR_YMM7,
  2636. NR_ZMM7: index:=7;
  2637. else
  2638. exit;
  2639. end;
  2640. case s of
  2641. 0,
  2642. 1 : scalefactor:=0;
  2643. 2 : scalefactor:=1;
  2644. 4 : scalefactor:=2;
  2645. 8 : scalefactor:=3;
  2646. else
  2647. exit;
  2648. end;
  2649. if (br=NR_NO) or
  2650. ((br<>NR_EBP) and (o=0) and (sym=nil)) then
  2651. md:=0
  2652. else
  2653. if ((o>=-128) and (o<=127) and (sym=nil) and (not(uselargeoffset) or (o = 0))) then
  2654. md:=1
  2655. else
  2656. md:=2;
  2657. if (br=NR_NO) or (md=2) then
  2658. output.bytes:=4
  2659. else
  2660. output.bytes:=md;
  2661. { SIB needed ? }
  2662. if (ir=NR_NO) and (br<>NR_ESP) then
  2663. begin
  2664. output.sib_present:=false;
  2665. output.modrm:=(longint(md) shl 6) or (rfield shl 3) or base;
  2666. end
  2667. else
  2668. begin
  2669. output.sib_present:=true;
  2670. output.modrm:=(longint(md) shl 6) or (rfield shl 3) or 4;
  2671. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  2672. end;
  2673. end;
  2674. if output.sib_present then
  2675. output.size:=2+output.bytes
  2676. else
  2677. output.size:=1+output.bytes;
  2678. result:=true;
  2679. end;
  2680. procedure maybe_swap_index_base(var br,ir:Tregister);
  2681. var
  2682. tmpreg: Tregister;
  2683. begin
  2684. if ((br=NR_NO) or (br=NR_SI) or (br=NR_DI)) and
  2685. ((ir=NR_NO) or (ir=NR_BP) or (ir=NR_BX)) then
  2686. begin
  2687. tmpreg:=br;
  2688. br:=ir;
  2689. ir:=tmpreg;
  2690. end;
  2691. end;
  2692. function process_ea_ref_16(const input:toper;out output:ea;rfield:longint; uselargeoffset: boolean):boolean;
  2693. var
  2694. sym : tasmsymbol;
  2695. md,s : byte;
  2696. base,
  2697. o : longint;
  2698. ir,br : Tregister;
  2699. isub,bsub : tsubregister;
  2700. begin
  2701. result:=false;
  2702. if ((input.ref^.index<>NR_NO) and (getregtype(input.ref^.index)<>R_INTREGISTER)) or
  2703. ((input.ref^.base<>NR_NO) and (getregtype(input.ref^.base)<>R_INTREGISTER)) then
  2704. internalerror(2003010803);
  2705. ir:=input.ref^.index;
  2706. br:=input.ref^.base;
  2707. isub:=getsubreg(ir);
  2708. bsub:=getsubreg(br);
  2709. s:=input.ref^.scalefactor;
  2710. o:=input.ref^.offset;
  2711. sym:=input.ref^.symbol;
  2712. { it's a direct address }
  2713. if (br=NR_NO) and (ir=NR_NO) then
  2714. begin
  2715. { it's a pure offset }
  2716. output.bytes:=2;
  2717. output.modrm:=6 or (rfield shl 3);
  2718. end
  2719. else
  2720. { it's an indirection }
  2721. begin
  2722. { 32 bit address? }
  2723. if ((ir<>NR_NO) and (isub<>R_SUBW)) or
  2724. ((br<>NR_NO) and (bsub<>R_SUBW)) then
  2725. message(asmw_e_32bit_not_supported);
  2726. { scalefactor can only be 1 in 16-bit addresses }
  2727. if (s<>1) and (ir<>NR_NO) then
  2728. exit;
  2729. maybe_swap_index_base(br,ir);
  2730. if (br=NR_BX) and (ir=NR_SI) then
  2731. base:=0
  2732. else if (br=NR_BX) and (ir=NR_DI) then
  2733. base:=1
  2734. else if (br=NR_BP) and (ir=NR_SI) then
  2735. base:=2
  2736. else if (br=NR_BP) and (ir=NR_DI) then
  2737. base:=3
  2738. else if (br=NR_NO) and (ir=NR_SI) then
  2739. base:=4
  2740. else if (br=NR_NO) and (ir=NR_DI) then
  2741. base:=5
  2742. else if (br=NR_BP) and (ir=NR_NO) then
  2743. base:=6
  2744. else if (br=NR_BX) and (ir=NR_NO) then
  2745. base:=7
  2746. else
  2747. exit;
  2748. if (base<>6) and (o=0) and (sym=nil) then
  2749. md:=0
  2750. else if ((o>=-128) and (o<=127) and (sym=nil) and (not(uselargeoffset) or (o = 0))) then
  2751. md:=1
  2752. else
  2753. md:=2;
  2754. output.bytes:=md;
  2755. output.modrm:=(longint(md) shl 6) or (rfield shl 3) or base;
  2756. end;
  2757. output.size:=1+output.bytes;
  2758. output.sib_present:=false;
  2759. result:=true;
  2760. end;
  2761. {$endif}
  2762. function process_ea(const input:toper;out output:ea;rfield:longint; uselargeoffset: boolean):boolean;
  2763. var
  2764. rv : byte;
  2765. begin
  2766. result:=false;
  2767. fillchar(output,sizeof(output),0);
  2768. {Register ?}
  2769. if (input.typ=top_reg) then
  2770. begin
  2771. rv:=regval(input.reg);
  2772. output.modrm:=$c0 or (rfield shl 3) or rv;
  2773. output.size:=1;
  2774. {$ifdef x86_64}
  2775. output.rex:=output.rex or (rexbits(input.reg) and $F1);
  2776. {$endif x86_64}
  2777. result:=true;
  2778. exit;
  2779. end;
  2780. {No register, so memory reference.}
  2781. if input.typ<>top_ref then
  2782. internalerror(200409263);
  2783. {$if defined(x86_64)}
  2784. result:=process_ea_ref_64_32(input,output,rfield, uselargeoffset);
  2785. {$elseif defined(i386) or defined(i8086)}
  2786. if is_16_bit_ref(input.ref^) then
  2787. result:=process_ea_ref_16(input,output,rfield, uselargeoffset)
  2788. else
  2789. result:=process_ea_ref_32(input,output,rfield, uselargeoffset);
  2790. {$endif}
  2791. end;
  2792. function taicpu.calcsize(p:PInsEntry):shortint;
  2793. var
  2794. codes : pchar;
  2795. c : byte;
  2796. len : shortint;
  2797. ea_data : ea;
  2798. exists_evex: boolean;
  2799. exists_vex: boolean;
  2800. exists_vex_extension: boolean;
  2801. exists_prefix_66: boolean;
  2802. exists_prefix_F2: boolean;
  2803. exists_prefix_F3: boolean;
  2804. exists_l256: boolean;
  2805. exists_l512: boolean;
  2806. exists_EVEXW1: boolean;
  2807. {$ifdef x86_64}
  2808. omit_rexw : boolean;
  2809. {$endif x86_64}
  2810. begin
  2811. len:=0;
  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. EVEXw1: byte;
  3373. EVEXz : byte;
  3374. EVEXaaa : byte;
  3375. EVEXb : byte;
  3376. EVEXmm : byte;
  3377. begin
  3378. { safety check }
  3379. if objdata.currobjsec.size<>longword(insoffset) then
  3380. internalerror(200130121);
  3381. { those variables are initialized inside local procedures, the dfa cannot handle this yet }
  3382. currsym:=nil;
  3383. currabsreloc:=RELOC_NONE;
  3384. currabsreloc32:=RELOC_NONE;
  3385. currrelreloc:=RELOC_NONE;
  3386. currval:=0;
  3387. { check instruction's processor level }
  3388. { todo: maybe adapt and enable this code for i386 and x86_64 as well }
  3389. {$ifdef i8086}
  3390. if objdata.CPUType<>cpu_none then
  3391. begin
  3392. if IF_8086 in insentry^.flags then
  3393. else if IF_186 in insentry^.flags then
  3394. begin
  3395. if objdata.CPUType<cpu_186 then
  3396. Message(asmw_e_instruction_not_supported_by_cpu);
  3397. end
  3398. else if IF_286 in insentry^.flags then
  3399. begin
  3400. if objdata.CPUType<cpu_286 then
  3401. Message(asmw_e_instruction_not_supported_by_cpu);
  3402. end
  3403. else if IF_386 in insentry^.flags then
  3404. begin
  3405. if objdata.CPUType<cpu_386 then
  3406. Message(asmw_e_instruction_not_supported_by_cpu);
  3407. end
  3408. else if IF_486 in insentry^.flags then
  3409. begin
  3410. if objdata.CPUType<cpu_486 then
  3411. Message(asmw_e_instruction_not_supported_by_cpu);
  3412. end
  3413. else if IF_PENT in insentry^.flags then
  3414. begin
  3415. if objdata.CPUType<cpu_Pentium then
  3416. Message(asmw_e_instruction_not_supported_by_cpu);
  3417. end
  3418. else if IF_P6 in insentry^.flags then
  3419. begin
  3420. if objdata.CPUType<cpu_Pentium2 then
  3421. Message(asmw_e_instruction_not_supported_by_cpu);
  3422. end
  3423. else if IF_KATMAI in insentry^.flags then
  3424. begin
  3425. if objdata.CPUType<cpu_Pentium3 then
  3426. Message(asmw_e_instruction_not_supported_by_cpu);
  3427. end
  3428. else if insentry^.flags*[IF_WILLAMETTE,IF_PRESCOTT]<>[] then
  3429. begin
  3430. if objdata.CPUType<cpu_Pentium4 then
  3431. Message(asmw_e_instruction_not_supported_by_cpu);
  3432. end
  3433. else if IF_NEC in insentry^.flags then
  3434. begin
  3435. { the NEC V20/V30 extensions are incompatible with 386+, due to overlapping opcodes }
  3436. if objdata.CPUType>=cpu_386 then
  3437. Message(asmw_e_instruction_not_supported_by_cpu);
  3438. end
  3439. else if IF_SANDYBRIDGE in insentry^.flags then
  3440. begin
  3441. { todo: handle these properly }
  3442. end;
  3443. end;
  3444. {$endif i8086}
  3445. { load data to write }
  3446. codes:=insentry^.code;
  3447. {$ifdef x86_64}
  3448. rexwritten:=false;
  3449. {$endif x86_64}
  3450. { Force word push/pop for registers }
  3451. if (opsize={$ifdef i8086}S_L{$else}S_W{$endif}) and ((codes[0]=#4) or (codes[0]=#6) or
  3452. ((codes[0]=#1) and ((codes[2]=#5) or (codes[2]=#7)))) then
  3453. write0x66prefix(objdata);
  3454. // needed VEX Prefix (for AVX etc.)
  3455. needed_VEX := false;
  3456. needed_EVEX := false;
  3457. needed_VEX_Extension := false;
  3458. needed_VSIB := false;
  3459. opmode := -1;
  3460. VEXvvvv := 0;
  3461. VEXmmmmm := 0;
  3462. VEXll := 0;
  3463. VEXw := 0;
  3464. VEXpp := 0;
  3465. EVEXpp := 0;
  3466. EVEXvvvv := 0;
  3467. EVEXr := 0;
  3468. EVEXx := 0;
  3469. EVEXv := 0;
  3470. EVEXll := 0;
  3471. EVEXw1 := 0;
  3472. EVEXz := 0;
  3473. EVEXaaa := 0;
  3474. EVEXb := 0;
  3475. EVEXmm := 0;
  3476. repeat
  3477. c:=ord(codes^);
  3478. inc(codes);
  3479. case c of
  3480. &0: break;
  3481. &1,
  3482. &2,
  3483. &3: inc(codes,c);
  3484. &10,
  3485. &11,
  3486. &12: inc(codes, 1);
  3487. &74: opmode := 0;
  3488. &75: opmode := 1;
  3489. &76: opmode := 2;
  3490. &100..&227: begin
  3491. // AVX 512 - EVEX
  3492. // check operands
  3493. if (c shr 6) = 1 then
  3494. begin
  3495. opidx := c and 7;
  3496. if ops > opidx then
  3497. begin
  3498. if (oper[opidx]^.typ=top_reg) then
  3499. if getsupreg(oper[opidx]^.reg) and $10 = $0 then EVEXr := 1;
  3500. end
  3501. end
  3502. else EVEXr := 1; // modrm:reg not used =>> 1
  3503. opidx := (c shr 3) and 7;
  3504. if ops > opidx then
  3505. case oper[opidx]^.typ of
  3506. top_reg: if getsupreg(oper[opidx]^.reg) and $10 = $0 then EVEXx := 1;
  3507. top_ref: begin
  3508. if getsupreg(oper[opidx]^.ref^.index) and $08 = $0 then EVEXx := 1;
  3509. if getsubreg(oper[opidx]^.ref^.index) in [R_SUBMMX,R_SUBMMY,R_SUBMMZ] then
  3510. begin
  3511. // VSIB memory addresing
  3512. if getsupreg(oper[opidx]^.ref^.index) and $10 = $0 then EVEXv := 1; // VECTOR-Index
  3513. needed_VSIB := true;
  3514. end;
  3515. end;
  3516. else
  3517. Internalerror(2019081014);
  3518. end;
  3519. end;
  3520. &333: begin
  3521. VEXvvvv := VEXvvvv OR $02; // set SIMD-prefix $F3
  3522. VEXpp := $02; // set SIMD-prefix $F3
  3523. EVEXpp := $02; // set SIMD-prefix $F3
  3524. end;
  3525. &334: begin
  3526. VEXvvvv := VEXvvvv OR $03; // set SIMD-prefix $F2
  3527. VEXpp := $03; // set SIMD-prefix $F2
  3528. EVEXpp := $03; // set SIMD-prefix $F2
  3529. end;
  3530. &350: needed_EVEX := true; // AVX512 instruction or AVX128/256/512-instruction (depended on operands [x,y,z]mm16..)
  3531. &351: EVEXll := $02; // vectorlength = 512 bits AND no scalar
  3532. &352: EVEXw1 := $01;
  3533. &361: begin
  3534. VEXvvvv := VEXvvvv OR $01; // set SIMD-prefix $66
  3535. VEXpp := $01; // set SIMD-prefix $66
  3536. EVEXpp := $01; // set SIMD-prefix $66
  3537. end;
  3538. &362: needed_VEX := true;
  3539. &363: begin
  3540. needed_VEX_Extension := true;
  3541. VEXvvvv := VEXvvvv OR (1 shl 7); // set REX.W
  3542. VEXw := 1;
  3543. end;
  3544. &364: begin
  3545. VEXvvvv := VEXvvvv OR $04; // vectorlength = 256 bits AND no scalar
  3546. VEXll := $01;
  3547. EVEXll := $01;
  3548. end;
  3549. &366,
  3550. &367: begin
  3551. opidx:=c-&364; { 0366->operand 2, 0367->operand 3 }
  3552. if (ops > opidx) and
  3553. (oper[opidx]^.typ=top_reg) and
  3554. ((oper[opidx]^.ot and OT_REG_EXTRA_MASK = otf_reg_xmm) or
  3555. (oper[opidx]^.ot and OT_REG_EXTRA_MASK = otf_reg_ymm) or
  3556. (oper[opidx]^.ot and OT_REG_EXTRA_MASK = otf_reg_zmm)) then
  3557. if (getsupreg(oper[opidx]^.reg) and $10 = $0) then EVEXx := 1;
  3558. end;
  3559. &370: begin
  3560. VEXmmmmm := VEXmmmmm OR $01; // set leading opcode byte $0F
  3561. EVEXmm := $01;
  3562. end;
  3563. &371: begin
  3564. needed_VEX_Extension := true;
  3565. VEXmmmmm := VEXmmmmm OR $02; // set leading opcode byte $0F38
  3566. EVEXmm := $02;
  3567. end;
  3568. &372: begin
  3569. needed_VEX_Extension := true;
  3570. VEXmmmmm := VEXmmmmm OR $03; // set leading opcode byte $0F3A
  3571. EVEXmm := $03;
  3572. end;
  3573. end;
  3574. until false;
  3575. {$ifndef x86_64}
  3576. EVEXv := 1;
  3577. EVEXx := 1;
  3578. EVEXr := 1;
  3579. {$endif}
  3580. if needed_VEX or needed_EVEX then
  3581. begin
  3582. if (opmode > ops) or
  3583. (opmode < -1) then
  3584. begin
  3585. Internalerror(777100);
  3586. end
  3587. else if opmode = -1 then
  3588. begin
  3589. VEXvvvv := VEXvvvv or ($0F shl 3); // set VEXvvvv bits (bits 6-3) to 1
  3590. EVEXvvvv := $0F;
  3591. {$ifdef x86_64}
  3592. if not(needed_vsib) then EVEXv := 1;
  3593. {$endif x86_64}
  3594. end
  3595. else if oper[opmode]^.typ = top_reg then
  3596. begin
  3597. VEXvvvv := VEXvvvv or ((not(regval(oper[opmode]^.reg)) and $07) shl 3);
  3598. EVEXvvvv := not(regval(oper[opmode]^.reg)) and $07;
  3599. {$ifdef x86_64}
  3600. if rexbits(oper[opmode]^.reg) = 0 then VEXvvvv := VEXvvvv or (1 shl 6);
  3601. if rexbits(oper[opmode]^.reg) = 0 then EVEXvvvv := EVEXvvvv or (1 shl 3);
  3602. if getsupreg(oper[opmode]^.reg) and $10 = 0 then EVEXv := 1;
  3603. {$else}
  3604. VEXvvvv := VEXvvvv or (1 shl 6);
  3605. EVEXvvvv := EVEXvvvv or (1 shl 3);
  3606. {$endif x86_64}
  3607. end
  3608. else Internalerror(777101);
  3609. if not(needed_VEX_Extension) then
  3610. begin
  3611. {$ifdef x86_64}
  3612. if rex and $0B <> 0 then needed_VEX_Extension := true;
  3613. {$endif x86_64}
  3614. end;
  3615. //TG
  3616. if needed_EVEX and needed_VEX then
  3617. begin
  3618. needed_EVEX := false;
  3619. if CheckUseEVEX then
  3620. begin
  3621. // EVEX-Flags r,v,x indicate extended-MMregister
  3622. // Flag = 0 =>> [x,y,z]mm16..[x,y,z]mm31
  3623. // Flag = 1 =>> [x,y,z]mm00..[x,y,z]mm15
  3624. needed_EVEX := true;
  3625. needed_VEX := false;
  3626. needed_VEX_Extension := false;
  3627. end;
  3628. end;
  3629. if needed_EVEX then
  3630. begin
  3631. EVEXaaa:= 0;
  3632. EVEXz := 0;
  3633. for i := 0 to ops - 1 do
  3634. if (oper[i]^.vopext and OTVE_VECTOR_MASK) <> 0 then
  3635. begin
  3636. if oper[i]^.vopext and OTVE_VECTOR_WRITEMASK = OTVE_VECTOR_WRITEMASK then
  3637. begin
  3638. EVEXaaa := oper[i]^.vopext and $07;
  3639. if oper[i]^.vopext and OTVE_VECTOR_ZERO = OTVE_VECTOR_ZERO then EVEXz := 1;
  3640. end;
  3641. if oper[i]^.vopext and OTVE_VECTOR_BCST = OTVE_VECTOR_BCST then
  3642. begin
  3643. EVEXb := 1;
  3644. end;
  3645. // flag EVEXb is multiple use (broadcast, sae and er)
  3646. if oper[i]^.vopext and OTVE_VECTOR_SAE = OTVE_VECTOR_SAE then
  3647. begin
  3648. EVEXb := 1;
  3649. end;
  3650. if oper[i]^.vopext and OTVE_VECTOR_ER = OTVE_VECTOR_ER then
  3651. begin
  3652. EVEXb := 1;
  3653. case oper[i]^.vopext and OTVE_VECTOR_ER_MASK of
  3654. OTVE_VECTOR_RNSAE: EVEXll := 0;
  3655. OTVE_VECTOR_RDSAE: EVEXll := 1;
  3656. OTVE_VECTOR_RUSAE: EVEXll := 2;
  3657. OTVE_VECTOR_RZSAE: EVEXll := 3;
  3658. else EVEXll := 0;
  3659. end;
  3660. end;
  3661. end;
  3662. bytes[0] := $62;
  3663. bytes[1] := ((EVEXmm and $03) shl 0) or
  3664. {$ifdef x86_64}
  3665. ((not(rex) and $05) shl 5) or
  3666. {$else}
  3667. (($05) shl 5) or
  3668. {$endif x86_64}
  3669. ((EVEXr and $01) shl 4) or
  3670. ((EVEXx and $01) shl 6);
  3671. bytes[2] := ((EVEXpp and $03) shl 0) or
  3672. ((1 and $01) shl 2) or // fixed in AVX512
  3673. ((EVEXvvvv and $0F) shl 3) or
  3674. ((EVEXw1 and $01) shl 7);
  3675. bytes[3] := ((EVEXaaa and $07) shl 0) or
  3676. ((EVEXv and $01) shl 3) or
  3677. ((EVEXb and $01) shl 4) or
  3678. ((EVEXll and $03) shl 5) or
  3679. ((EVEXz and $01) shl 7);
  3680. objdata.writebytes(bytes,4);
  3681. end
  3682. else if needed_VEX_Extension then
  3683. begin
  3684. // VEX-Prefix-Length = 3 Bytes
  3685. {$ifdef x86_64}
  3686. VEXmmmmm := VEXmmmmm or ((not(rex) and $07) shl 5); // set REX.rxb
  3687. VEXvvvv := VEXvvvv or ((rex and $08) shl 7); // set REX.w
  3688. {$else}
  3689. VEXmmmmm := VEXmmmmm or (7 shl 5); //
  3690. {$endif x86_64}
  3691. bytes[0]:=$C4;
  3692. bytes[1]:=VEXmmmmm;
  3693. bytes[2]:=VEXvvvv;
  3694. objdata.writebytes(bytes,3);
  3695. end
  3696. else
  3697. begin
  3698. // VEX-Prefix-Length = 2 Bytes
  3699. {$ifdef x86_64}
  3700. if rex and $04 = 0 then
  3701. {$endif x86_64}
  3702. begin
  3703. VEXvvvv := VEXvvvv or (1 shl 7);
  3704. end;
  3705. bytes[0]:=$C5;
  3706. bytes[1]:=VEXvvvv;
  3707. objdata.writebytes(bytes,2);
  3708. end;
  3709. end
  3710. else
  3711. begin
  3712. needed_VEX_Extension := false;
  3713. opmode := -1;
  3714. end;
  3715. if not(needed_EVEX) then
  3716. begin
  3717. for opidx := 0 to ops - 1 do
  3718. begin
  3719. if ops > opidx then
  3720. if (oper[opidx]^.typ=top_reg) and
  3721. (getregtype(oper[opidx]^.reg) = R_MMREGISTER) then
  3722. if getsupreg(oper[opidx]^.reg) and $10 = $10 then
  3723. begin
  3724. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  3725. break;
  3726. end;
  3727. //badreg(oper[opidx]^.reg);
  3728. end;
  3729. end;
  3730. { load data to write }
  3731. codes:=insentry^.code;
  3732. repeat
  3733. c:=ord(codes^);
  3734. inc(codes);
  3735. case c of
  3736. &0 :
  3737. break;
  3738. &1,&2,&3 :
  3739. begin
  3740. {$ifdef x86_64}
  3741. if not(needed_VEX or needed_EVEX) then // TG
  3742. maybewriterex;
  3743. {$endif x86_64}
  3744. objdata.writebytes(codes^,c);
  3745. inc(codes,c);
  3746. end;
  3747. &4,&6 :
  3748. begin
  3749. case oper[0]^.reg of
  3750. NR_CS:
  3751. bytes[0]:=$e;
  3752. NR_NO,
  3753. NR_DS:
  3754. bytes[0]:=$1e;
  3755. NR_ES:
  3756. bytes[0]:=$6;
  3757. NR_SS:
  3758. bytes[0]:=$16;
  3759. else
  3760. internalerror(777004);
  3761. end;
  3762. if c=&4 then
  3763. inc(bytes[0]);
  3764. objdata.writebytes(bytes,1);
  3765. end;
  3766. &5,&7 :
  3767. begin
  3768. case oper[0]^.reg of
  3769. NR_FS:
  3770. bytes[0]:=$a0;
  3771. NR_GS:
  3772. bytes[0]:=$a8;
  3773. else
  3774. internalerror(777005);
  3775. end;
  3776. if c=&5 then
  3777. inc(bytes[0]);
  3778. objdata.writebytes(bytes,1);
  3779. end;
  3780. &10,&11,&12 :
  3781. begin
  3782. {$ifdef x86_64}
  3783. if not(needed_VEX or needed_EVEX) then // TG
  3784. maybewriterex;
  3785. {$endif x86_64}
  3786. bytes[0]:=ord(codes^)+regval(oper[c-&10]^.reg);
  3787. inc(codes);
  3788. objdata.writebytes(bytes,1);
  3789. end;
  3790. &13 :
  3791. begin
  3792. bytes[0]:=ord(codes^)+condval[condition];
  3793. inc(codes);
  3794. objdata.writebytes(bytes,1);
  3795. end;
  3796. &14,&15,&16 :
  3797. begin
  3798. getvalsym(c-&14);
  3799. if (currval<-128) or (currval>127) then
  3800. Message2(asmw_e_value_exceeds_bounds,'signed byte',tostr(currval));
  3801. if assigned(currsym) then
  3802. objdata_writereloc(currval,1,currsym,currabsreloc)
  3803. else
  3804. objdata.writebytes(currval,1);
  3805. end;
  3806. &20,&21,&22 :
  3807. begin
  3808. getvalsym(c-&20);
  3809. if (currval<-256) or (currval>255) then
  3810. Message2(asmw_e_value_exceeds_bounds,'byte',tostr(currval));
  3811. if assigned(currsym) then
  3812. objdata_writereloc(currval,1,currsym,currabsreloc)
  3813. else
  3814. objdata.writebytes(currval,1);
  3815. end;
  3816. &23 :
  3817. begin
  3818. bytes[0]:=ord(codes^)+condval[inverse_cond(condition)];
  3819. inc(codes);
  3820. objdata.writebytes(bytes,1);
  3821. end;
  3822. &24,&25,&26,&27 :
  3823. begin
  3824. getvalsym(c-&24);
  3825. if IF_IMM3 in insentry^.flags then
  3826. begin
  3827. if (currval<0) or (currval>7) then
  3828. Message2(asmw_e_value_exceeds_bounds,'unsigned triad',tostr(currval));
  3829. end
  3830. else if IF_IMM4 in insentry^.flags then
  3831. begin
  3832. if (currval<0) or (currval>15) then
  3833. Message2(asmw_e_value_exceeds_bounds,'unsigned nibble',tostr(currval));
  3834. end
  3835. else
  3836. if (currval<0) or (currval>255) then
  3837. Message2(asmw_e_value_exceeds_bounds,'unsigned byte',tostr(currval));
  3838. if assigned(currsym) then
  3839. objdata_writereloc(currval,1,currsym,currabsreloc)
  3840. else
  3841. objdata.writebytes(currval,1);
  3842. end;
  3843. &30,&31,&32 : // 030..032
  3844. begin
  3845. getvalsym(c-&30);
  3846. {$ifndef i8086}
  3847. { currval is an aint so this cannot happen on i8086 and causes only a warning }
  3848. if (currval<-65536) or (currval>65535) then
  3849. Message2(asmw_e_value_exceeds_bounds,'word',tostr(currval));
  3850. {$endif i8086}
  3851. if assigned(currsym)
  3852. {$ifdef i8086}
  3853. or (currabsreloc in [RELOC_DGROUP,RELOC_FARDATASEG])
  3854. {$endif i8086}
  3855. then
  3856. objdata_writereloc(currval,2,currsym,currabsreloc)
  3857. else
  3858. objdata.writebytes(currval,2);
  3859. end;
  3860. &34,&35,&36 : // 034..036
  3861. { !!! These are intended (and used in opcode table) to select depending
  3862. on address size, *not* operand size. Works by coincidence only. }
  3863. begin
  3864. getvalsym(c-&34);
  3865. {$ifdef i8086}
  3866. if assigned(currsym) then
  3867. objdata_writereloc(currval,2,currsym,currabsreloc)
  3868. else
  3869. objdata.writebytes(currval,2);
  3870. {$else i8086}
  3871. if opsize=S_Q then
  3872. begin
  3873. if assigned(currsym) then
  3874. objdata_writereloc(currval,8,currsym,currabsreloc)
  3875. else
  3876. objdata.writebytes(currval,8);
  3877. end
  3878. else
  3879. begin
  3880. if assigned(currsym) then
  3881. objdata_writereloc(currval,4,currsym,currabsreloc32)
  3882. else
  3883. objdata.writebytes(currval,4);
  3884. end
  3885. {$endif i8086}
  3886. end;
  3887. &40,&41,&42 : // 040..042
  3888. begin
  3889. getvalsym(c-&40);
  3890. if assigned(currsym)
  3891. {$ifdef i8086}
  3892. or (currabsreloc in [RELOC_DGROUP,RELOC_FARDATASEG])
  3893. {$endif i8086}
  3894. then
  3895. objdata_writereloc(currval,4,currsym,currabsreloc32)
  3896. else
  3897. objdata.writebytes(currval,4);
  3898. end;
  3899. &44,&45,&46 :// 044..046 - select between word/dword/qword depending on
  3900. begin // address size (we support only default address sizes).
  3901. getvalsym(c-&44);
  3902. {$if defined(x86_64)}
  3903. if assigned(currsym) then
  3904. objdata_writereloc(currval,8,currsym,currabsreloc)
  3905. else
  3906. objdata.writebytes(currval,8);
  3907. {$elseif defined(i386)}
  3908. if assigned(currsym) then
  3909. objdata_writereloc(currval,4,currsym,currabsreloc32)
  3910. else
  3911. objdata.writebytes(currval,4);
  3912. {$elseif defined(i8086)}
  3913. if assigned(currsym) then
  3914. objdata_writereloc(currval,2,currsym,currabsreloc)
  3915. else
  3916. objdata.writebytes(currval,2);
  3917. {$endif}
  3918. end;
  3919. &50,&51,&52 : // 050..052 - byte relative operand
  3920. begin
  3921. getvalsym(c-&50);
  3922. data:=currval-insend;
  3923. {$push}
  3924. {$r-,q-} { disable also overflow as address returns a qword for x86_64 }
  3925. if assigned(currsym) then
  3926. inc(data,currsym.address);
  3927. {$pop}
  3928. if (data>127) or (data<-128) then
  3929. Message1(asmw_e_short_jmp_out_of_range,tostr(data));
  3930. objdata.writebytes(data,1);
  3931. end;
  3932. &54,&55,&56: // 054..056 - qword immediate operand
  3933. begin
  3934. getvalsym(c-&54);
  3935. if assigned(currsym) then
  3936. objdata_writereloc(currval,8,currsym,currabsreloc)
  3937. else
  3938. objdata.writebytes(currval,8);
  3939. end;
  3940. &60,&61,&62 :
  3941. begin
  3942. getvalsym(c-&60);
  3943. {$ifdef i8086}
  3944. if assigned(currsym) then
  3945. objdata_writereloc(currval,2,currsym,currrelreloc)
  3946. else
  3947. objdata_writereloc(currval-insend,2,nil,currabsreloc)
  3948. {$else i8086}
  3949. InternalError(2020100821);
  3950. {$endif i8086}
  3951. end;
  3952. &64,&65,&66 : // 064..066 - select between 16/32 address mode, but we support only 32 (only 16 on i8086)
  3953. begin
  3954. getvalsym(c-&64);
  3955. {$ifdef i8086}
  3956. if assigned(currsym) then
  3957. objdata_writereloc(currval,2,currsym,currrelreloc)
  3958. else
  3959. objdata_writereloc(currval-insend,2,nil,currabsreloc)
  3960. {$else i8086}
  3961. if assigned(currsym) then
  3962. objdata_writereloc(currval,4,currsym,currrelreloc)
  3963. else
  3964. objdata_writereloc(currval-insend,4,nil,currabsreloc32)
  3965. {$endif i8086}
  3966. end;
  3967. &70,&71,&72 : // 070..072 - long relative operand
  3968. begin
  3969. getvalsym(c-&70);
  3970. if assigned(currsym) then
  3971. objdata_writereloc(currval,4,currsym,currrelreloc)
  3972. else
  3973. objdata_writereloc(currval-insend,4,nil,currabsreloc32)
  3974. end;
  3975. &74,&75,&76 : ; // 074..076 - vex-coded vector operand
  3976. // ignore
  3977. &254,&255,&256 : // 0254..0256 - dword implicitly sign-extended to 64-bit (x86_64 only)
  3978. begin
  3979. getvalsym(c-&254);
  3980. {$ifdef x86_64}
  3981. { for i386 as aint type is longint the
  3982. following test is useless }
  3983. if (currval<low(longint)) or (currval>high(longint)) then
  3984. Message2(asmw_e_value_exceeds_bounds,'signed dword',tostr(currval));
  3985. {$endif x86_64}
  3986. if assigned(currsym) then
  3987. objdata_writereloc(currval,4,currsym,currabsreloc32)
  3988. else
  3989. objdata.writebytes(currval,4);
  3990. end;
  3991. &300,&301,&302:
  3992. begin
  3993. {$if defined(x86_64) or defined(i8086)}
  3994. if (oper[c and 3]^.ot and OT_SIZE_MASK)=OT_BITS32 then
  3995. write0x67prefix(objdata);
  3996. {$endif x86_64 or i8086}
  3997. end;
  3998. &310 : { fixed 16-bit addr }
  3999. {$if defined(x86_64)}
  4000. { every insentry having code 0310 must be marked with NOX86_64 }
  4001. InternalError(2011051302);
  4002. {$elseif defined(i386)}
  4003. write0x67prefix(objdata);
  4004. {$elseif defined(i8086)}
  4005. {nothing};
  4006. {$endif}
  4007. &311 : { fixed 32-bit addr }
  4008. {$if defined(x86_64) or defined(i8086)}
  4009. write0x67prefix(objdata)
  4010. {$endif x86_64 or i8086}
  4011. ;
  4012. &320,&321,&322 :
  4013. begin
  4014. case oper[c-&320]^.ot and OT_SIZE_MASK of
  4015. {$if defined(i386) or defined(x86_64)}
  4016. OT_BITS16 :
  4017. {$elseif defined(i8086)}
  4018. OT_BITS32 :
  4019. {$endif}
  4020. write0x66prefix(objdata);
  4021. {$ifndef x86_64}
  4022. OT_BITS64 :
  4023. Message(asmw_e_64bit_not_supported);
  4024. {$endif x86_64}
  4025. end;
  4026. end;
  4027. &323 : {no action needed};
  4028. &325:
  4029. {$ifdef i8086}
  4030. write0x66prefix(objdata);
  4031. {$else i8086}
  4032. {no action needed};
  4033. {$endif i8086}
  4034. &324,
  4035. &361:
  4036. begin
  4037. {$ifndef i8086}
  4038. if not(needed_VEX or needed_EVEX) then
  4039. write0x66prefix(objdata);
  4040. {$endif not i8086}
  4041. end;
  4042. &326 :
  4043. begin
  4044. {$ifndef x86_64}
  4045. Message(asmw_e_64bit_not_supported);
  4046. {$endif x86_64}
  4047. end;
  4048. &333 :
  4049. begin
  4050. if not(needed_VEX or needed_EVEX) then
  4051. begin
  4052. bytes[0]:=$f3;
  4053. objdata.writebytes(bytes,1);
  4054. end;
  4055. end;
  4056. &334 :
  4057. begin
  4058. if not(needed_VEX or needed_EVEX) then
  4059. begin
  4060. bytes[0]:=$f2;
  4061. objdata.writebytes(bytes,1);
  4062. end;
  4063. end;
  4064. &335:
  4065. ;
  4066. &336: ; // indicates 32-bit scalar vector operand {no action needed}
  4067. &337: ; // indicates 64-bit scalar vector operand {no action needed}
  4068. &312,
  4069. &327,
  4070. &331,&332 :
  4071. begin
  4072. { these are dissambler hints or 32 bit prefixes which
  4073. are not needed }
  4074. end;
  4075. &362..&364: ; // VEX flags =>> nothing todo
  4076. &366, &367:
  4077. begin
  4078. opidx:=c-&364; { 0366->operand 2, 0367->operand 3 }
  4079. if (needed_VEX or needed_EVEX) and
  4080. (ops=4) and
  4081. (oper[opidx]^.typ=top_reg) and
  4082. (
  4083. ((oper[opidx]^.ot and OT_REG_EXTRA_MASK)=otf_reg_xmm) or
  4084. ((oper[opidx]^.ot and OT_REG_EXTRA_MASK)=otf_reg_ymm) or
  4085. ((oper[opidx]^.ot and OT_REG_EXTRA_MASK)=otf_reg_zmm)
  4086. ) then
  4087. begin
  4088. bytes[0] := ((getsupreg(oper[opidx]^.reg) and 15) shl 4);
  4089. objdata.writebytes(bytes,1);
  4090. end
  4091. else
  4092. Internalerror(2014032001);
  4093. end;
  4094. &350..&352: ; // EVEX flags =>> nothing todo
  4095. &370..&372: ; // VEX flags =>> nothing todo
  4096. &37:
  4097. begin
  4098. {$ifdef i8086}
  4099. if assigned(currsym) then
  4100. objdata_writereloc(0,2,currsym,RELOC_SEG)
  4101. else
  4102. InternalError(2015041503);
  4103. {$else i8086}
  4104. InternalError(2020100822);
  4105. {$endif i8086}
  4106. end;
  4107. else
  4108. begin
  4109. { rex should be written at this point }
  4110. {$ifdef x86_64}
  4111. if not(needed_VEX or needed_EVEX) then // TG
  4112. if (rex<>0) and not(rexwritten) then
  4113. internalerror(200603191);
  4114. {$endif x86_64}
  4115. if (c>=&100) and (c<=&227) then // 0100..0227
  4116. begin
  4117. if (c<&177) then // 0177
  4118. begin
  4119. if (oper[c and 7]^.typ=top_reg) then
  4120. rfield:=regval(oper[c and 7]^.reg)
  4121. else
  4122. rfield:=regval(oper[c and 7]^.ref^.base);
  4123. end
  4124. else
  4125. rfield:=c and 7;
  4126. opidx:=(c shr 3) and 7;
  4127. if not process_ea(oper[opidx]^,ea_data,rfield, EVEXTupleState = etsNotTuple) then
  4128. Message(asmw_e_invalid_effective_address);
  4129. pb:=@bytes[0];
  4130. pb^:=ea_data.modrm;
  4131. inc(pb);
  4132. if ea_data.sib_present then
  4133. begin
  4134. pb^:=ea_data.sib;
  4135. inc(pb);
  4136. end;
  4137. s:=pb-@bytes[0];
  4138. objdata.writebytes(bytes,s);
  4139. case ea_data.bytes of
  4140. 0 : ;
  4141. 1 :
  4142. begin
  4143. if (oper[opidx]^.ot and OT_MEMORY)=OT_MEMORY then
  4144. begin
  4145. currsym:=objdata.symbolref(oper[opidx]^.ref^.symbol);
  4146. {$ifdef i386}
  4147. if (oper[opidx]^.ref^.refaddr=addr_pic) and
  4148. (tf_pic_uses_got in target_info.flags) then
  4149. currabsreloc:=RELOC_GOT32
  4150. else
  4151. {$endif i386}
  4152. {$ifdef x86_64}
  4153. if oper[opidx]^.ref^.refaddr=addr_pic then
  4154. currabsreloc:=RELOC_GOTPCREL
  4155. else
  4156. {$endif x86_64}
  4157. currabsreloc:=RELOC_ABSOLUTE;
  4158. objdata_writereloc(oper[opidx]^.ref^.offset,1,currsym,currabsreloc);
  4159. end
  4160. else
  4161. begin
  4162. bytes[0]:=oper[opidx]^.ref^.offset;
  4163. objdata.writebytes(bytes,1);
  4164. end;
  4165. inc(s);
  4166. end;
  4167. 2,4 :
  4168. begin
  4169. currsym:=objdata.symbolref(oper[opidx]^.ref^.symbol);
  4170. currval:=oper[opidx]^.ref^.offset;
  4171. {$ifdef x86_64}
  4172. if oper[opidx]^.ref^.refaddr=addr_pic then
  4173. currabsreloc:=RELOC_GOTPCREL
  4174. else if oper[opidx]^.ref^.refaddr=addr_tlsgd then
  4175. currabsreloc:=RELOC_TLSGD
  4176. else if oper[opidx]^.ref^.refaddr=addr_tpoff then
  4177. currabsreloc:=RELOC_TPOFF
  4178. else
  4179. if oper[opidx]^.ref^.base=NR_RIP then
  4180. begin
  4181. currabsreloc:=RELOC_RELATIVE;
  4182. { Adjust reloc value by number of bytes following the displacement,
  4183. but not if displacement is specified by literal constant }
  4184. if Assigned(currsym) then
  4185. Dec(currval,InsEnd-objdata.CurrObjSec.Size-ea_data.bytes);
  4186. end
  4187. else
  4188. {$endif x86_64}
  4189. {$ifdef i386}
  4190. if (oper[opidx]^.ref^.refaddr=addr_pic) and
  4191. (tf_pic_uses_got in target_info.flags) then
  4192. currabsreloc:=RELOC_GOT32
  4193. else if oper[opidx]^.ref^.refaddr=addr_tlsgd then
  4194. currabsreloc:=RELOC_TLSGD
  4195. else if oper[opidx]^.ref^.refaddr=addr_ntpoff then
  4196. currabsreloc:=RELOC_NTPOFF
  4197. else
  4198. {$endif i386}
  4199. {$ifdef i8086}
  4200. if ea_data.bytes=2 then
  4201. currabsreloc:=RELOC_ABSOLUTE
  4202. else
  4203. {$endif i8086}
  4204. currabsreloc:=RELOC_ABSOLUTE32;
  4205. if (currabsreloc in [RELOC_ABSOLUTE32{$ifdef i8086},RELOC_ABSOLUTE{$endif}]) and
  4206. (Assigned(oper[opidx]^.ref^.relsymbol)) then
  4207. begin
  4208. relsym:=objdata.symbolref(oper[opidx]^.ref^.relsymbol);
  4209. if relsym.objsection=objdata.CurrObjSec then
  4210. begin
  4211. currval:=objdata.CurrObjSec.size+ea_data.bytes-relsym.offset+currval;
  4212. {$ifdef i8086}
  4213. if ea_data.bytes=4 then
  4214. currabsreloc:=RELOC_RELATIVE32
  4215. else
  4216. {$endif i8086}
  4217. currabsreloc:=RELOC_RELATIVE;
  4218. end
  4219. else
  4220. begin
  4221. currabsreloc:=RELOC_PIC_PAIR;
  4222. currval:=relsym.offset;
  4223. end;
  4224. end;
  4225. objdata_writereloc(currval,ea_data.bytes,currsym,currabsreloc);
  4226. inc(s,ea_data.bytes);
  4227. end;
  4228. end;
  4229. end
  4230. else
  4231. InternalError(777007);
  4232. end;
  4233. end;
  4234. until false;
  4235. end;
  4236. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  4237. begin
  4238. result:=(((opcode=A_MOV) or (opcode=A_XCHG)) and
  4239. (regtype = R_INTREGISTER) and
  4240. (ops=2) and
  4241. (oper[0]^.typ=top_reg) and
  4242. (oper[1]^.typ=top_reg) and
  4243. (oper[0]^.reg=oper[1]^.reg)
  4244. ) or
  4245. ({ checking the opcodes is a long "or" chain, so check first the registers which is more selective }
  4246. ((regtype = R_MMREGISTER) and
  4247. (ops=2) and
  4248. (oper[0]^.typ=top_reg) and
  4249. (oper[1]^.typ=top_reg) and
  4250. (oper[0]^.reg=oper[1]^.reg)) and
  4251. (
  4252. (opcode=A_MOVSS) or (opcode=A_MOVSD) or
  4253. (opcode=A_MOVQ) or (opcode=A_MOVD) or
  4254. (opcode=A_MOVAPS) or (opcode=A_MOVAPD) or
  4255. (opcode=A_MOVUPS) or (opcode=A_MOVUPD) or
  4256. (opcode=A_MOVDQA) or (opcode=A_MOVDQU) or
  4257. (opcode=A_VMOVSS) or (opcode=A_VMOVSD) or
  4258. (opcode=A_VMOVQ) or (opcode=A_VMOVD) or
  4259. (opcode=A_VMOVAPS) or (opcode=A_VMOVAPD) or
  4260. (opcode=A_VMOVUPS) or (opcode=A_VMOVUPD) or
  4261. (opcode=A_VMOVDQA) or (opcode=A_VMOVDQU)
  4262. )
  4263. );
  4264. end;
  4265. procedure build_spilling_operation_type_table;
  4266. var
  4267. opcode : tasmop;
  4268. begin
  4269. new(operation_type_table);
  4270. fillchar(operation_type_table^,sizeof(toperation_type_table),byte(operand_read));
  4271. for opcode:=low(tasmop) to high(tasmop) do
  4272. with InsProp[opcode] do
  4273. begin
  4274. if Ch_Rop1 in Ch then
  4275. operation_type_table^[opcode,0]:=operand_read;
  4276. if Ch_Wop1 in Ch then
  4277. operation_type_table^[opcode,0]:=operand_write;
  4278. if [Ch_RWop1,Ch_Mop1]*Ch<>[] then
  4279. operation_type_table^[opcode,0]:=operand_readwrite;
  4280. if Ch_Rop2 in Ch then
  4281. operation_type_table^[opcode,1]:=operand_read;
  4282. if Ch_Wop2 in Ch then
  4283. operation_type_table^[opcode,1]:=operand_write;
  4284. if [Ch_RWop2,Ch_Mop2]*Ch<>[] then
  4285. operation_type_table^[opcode,1]:=operand_readwrite;
  4286. if Ch_Rop3 in Ch then
  4287. operation_type_table^[opcode,2]:=operand_read;
  4288. if Ch_Wop3 in Ch then
  4289. operation_type_table^[opcode,2]:=operand_write;
  4290. if [Ch_RWop3,Ch_Mop3]*Ch<>[] then
  4291. operation_type_table^[opcode,2]:=operand_readwrite;
  4292. if Ch_Rop4 in Ch then
  4293. operation_type_table^[opcode,3]:=operand_read;
  4294. if Ch_Wop4 in Ch then
  4295. operation_type_table^[opcode,3]:=operand_write;
  4296. if [Ch_RWop4,Ch_Mop4]*Ch<>[] then
  4297. operation_type_table^[opcode,3]:=operand_readwrite;
  4298. end;
  4299. end;
  4300. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  4301. begin
  4302. { the information in the instruction table is made for the string copy
  4303. operation MOVSD so hack here (FK)
  4304. VMOVSS and VMOVSD has two and three operand flavours, this cannot modelled by x86ins.dat
  4305. so fix it here (FK)
  4306. }
  4307. if ((opcode=A_MOVSD) or (opcode=A_VMOVSS) or (opcode=A_VMOVSD)) and (ops=2) then
  4308. begin
  4309. case opnr of
  4310. 0:
  4311. result:=operand_read;
  4312. 1:
  4313. result:=operand_write;
  4314. else
  4315. internalerror(200506055);
  4316. end
  4317. end
  4318. { IMUL has 1, 2 and 3-operand forms }
  4319. else if opcode=A_IMUL then
  4320. begin
  4321. case ops of
  4322. 1:
  4323. if opnr=0 then
  4324. result:=operand_read
  4325. else
  4326. internalerror(2014011802);
  4327. 2:
  4328. begin
  4329. case opnr of
  4330. 0:
  4331. result:=operand_read;
  4332. 1:
  4333. result:=operand_readwrite;
  4334. else
  4335. internalerror(2014011803);
  4336. end;
  4337. end;
  4338. 3:
  4339. begin
  4340. case opnr of
  4341. 0,1:
  4342. result:=operand_read;
  4343. 2:
  4344. result:=operand_write;
  4345. else
  4346. internalerror(2014011804);
  4347. end;
  4348. end;
  4349. else
  4350. internalerror(2014011805);
  4351. end;
  4352. end
  4353. else
  4354. result:=operation_type_table^[opcode,opnr];
  4355. end;
  4356. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  4357. var
  4358. tmpref: treference;
  4359. begin
  4360. tmpref:=ref;
  4361. {$ifdef i8086}
  4362. if tmpref.segment=NR_SS then
  4363. tmpref.segment:=NR_NO;
  4364. {$endif i8086}
  4365. case getregtype(r) of
  4366. R_INTREGISTER :
  4367. begin
  4368. if getsubreg(r)=R_SUBH then
  4369. inc(tmpref.offset);
  4370. { we don't need special code here for 32 bit loads on x86_64, since
  4371. those will automatically zero-extend the upper 32 bits. }
  4372. result:=taicpu.op_ref_reg(A_MOV,reg2opsize(r),tmpref,r);
  4373. end;
  4374. R_MMREGISTER :
  4375. if current_settings.fputype in fpu_avx_instructionsets then
  4376. case getsubreg(r) of
  4377. R_SUBMMD:
  4378. result:=taicpu.op_ref_reg(A_VMOVSD,S_NO,tmpref,r);
  4379. R_SUBMMS:
  4380. result:=taicpu.op_ref_reg(A_VMOVSS,S_NO,tmpref,r);
  4381. R_SUBQ,
  4382. R_SUBMMWHOLE:
  4383. result:=taicpu.op_ref_reg(A_VMOVQ,S_NO,tmpref,r);
  4384. R_SUBMMX:
  4385. result:=taicpu.op_ref_reg(A_VMOVDQU,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. R_SUBMMX:
  4399. result:=taicpu.op_ref_reg(A_MOVDQA,S_NO,tmpref,r);
  4400. else
  4401. internalerror(2005060405);
  4402. end;
  4403. else
  4404. internalerror(2004010411);
  4405. end;
  4406. end;
  4407. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  4408. var
  4409. size: topsize;
  4410. tmpref: treference;
  4411. begin
  4412. tmpref:=ref;
  4413. {$ifdef i8086}
  4414. if tmpref.segment=NR_SS then
  4415. tmpref.segment:=NR_NO;
  4416. {$endif i8086}
  4417. case getregtype(r) of
  4418. R_INTREGISTER :
  4419. begin
  4420. if getsubreg(r)=R_SUBH then
  4421. inc(tmpref.offset);
  4422. size:=reg2opsize(r);
  4423. {$ifdef x86_64}
  4424. { even if it's a 32 bit reg, we still have to spill 64 bits
  4425. because we often perform 64 bit operations on them }
  4426. if (size=S_L) then
  4427. begin
  4428. size:=S_Q;
  4429. r:=newreg(getregtype(r),getsupreg(r),R_SUBWHOLE);
  4430. end;
  4431. {$endif x86_64}
  4432. result:=taicpu.op_reg_ref(A_MOV,size,r,tmpref);
  4433. end;
  4434. R_MMREGISTER :
  4435. if current_settings.fputype in fpu_avx_instructionsets then
  4436. case getsubreg(r) of
  4437. R_SUBMMD:
  4438. result:=taicpu.op_reg_ref(A_VMOVSD,S_NO,r,tmpref);
  4439. R_SUBMMS:
  4440. result:=taicpu.op_reg_ref(A_VMOVSS,S_NO,r,tmpref);
  4441. R_SUBQ,
  4442. R_SUBMMWHOLE:
  4443. result:=taicpu.op_reg_ref(A_VMOVQ,S_NO,r,tmpref);
  4444. else
  4445. internalerror(200506042);
  4446. end
  4447. else
  4448. case getsubreg(r) of
  4449. R_SUBMMD:
  4450. result:=taicpu.op_reg_ref(A_MOVSD,S_NO,r,tmpref);
  4451. R_SUBMMS:
  4452. result:=taicpu.op_reg_ref(A_MOVSS,S_NO,r,tmpref);
  4453. R_SUBQ,
  4454. R_SUBMMWHOLE:
  4455. result:=taicpu.op_reg_ref(A_MOVQ,S_NO,r,tmpref);
  4456. else
  4457. internalerror(2005060404);
  4458. end;
  4459. else
  4460. internalerror(2004010412);
  4461. end;
  4462. end;
  4463. {$ifdef i8086}
  4464. procedure taicpu.loadsegsymbol(opidx:longint;s:tasmsymbol);
  4465. var
  4466. r: treference;
  4467. begin
  4468. reference_reset_symbol(r,s,0,1,[]);
  4469. r.refaddr:=addr_seg;
  4470. loadref(opidx,r);
  4471. end;
  4472. {$endif i8086}
  4473. {*****************************************************************************
  4474. Instruction table
  4475. *****************************************************************************}
  4476. procedure BuildInsTabCache;
  4477. var
  4478. i : longint;
  4479. begin
  4480. new(instabcache);
  4481. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  4482. i:=0;
  4483. while (i<InsTabEntries) do
  4484. begin
  4485. if InsTabCache^[InsTab[i].OPcode]=-1 then
  4486. InsTabCache^[InsTab[i].OPcode]:=i;
  4487. inc(i);
  4488. end;
  4489. end;
  4490. procedure BuildInsTabMemRefSizeInfoCache;
  4491. var
  4492. AsmOp: TasmOp;
  4493. i,j: longint;
  4494. insentry : PInsEntry;
  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(2020100823);
  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(2020100824);
  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.