aasmcpu.pas 198 KB

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