aasmcpu.pas 192 KB

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