aasmcpu.pas 139 KB

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