aasmcpu.pas 109 KB

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