aasmcpu.pas 140 KB

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