aasmcpu.pas 114 KB

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