aasmcpu.pas 196 KB

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