aasmcpu.pas 114 KB

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