aasmcpu.pas 198 KB

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