aasmcpu.pas 181 KB

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