aasmcpu.pas 147 KB

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