aasmcpu.pas 116 KB

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