aasmcpu.pas 199 KB

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