aasmcpu.pas 199 KB

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