aasmcpu.pas 141 KB

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