aasmcpu.pas 190 KB

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