aasmcpu.pas 118 KB

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