aasmcpu.pas 197 KB

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