aasmcpu.pas 110 KB

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