aasmcpu.pas 205 KB

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