aasmcpu.pas 117 KB

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