aasmcpu.pas 126 KB

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