aasmcpu.pas 193 KB

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