X86Assembler.h 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563
  1. /*
  2. * Copyright (C) 2008, 2012 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef X86Assembler_h
  26. #define X86Assembler_h
  27. #if ENABLE(ASSEMBLER) && (CPU(X86) || CPU(X86_64))
  28. #include "AssemblerBuffer.h"
  29. #include "JITCompilationEffort.h"
  30. #include <stdint.h>
  31. #include <wtf/Assertions.h>
  32. #include <wtf/Vector.h>
  33. namespace JSC {
  34. inline bool CAN_SIGN_EXTEND_8_32(int32_t value) { return value == (int32_t)(signed char)value; }
  35. namespace X86Registers {
  36. typedef enum {
  37. eax,
  38. ecx,
  39. edx,
  40. ebx,
  41. esp,
  42. ebp,
  43. esi,
  44. edi,
  45. #if CPU(X86_64)
  46. r8,
  47. r9,
  48. r10,
  49. r11,
  50. r12,
  51. r13,
  52. r14,
  53. r15,
  54. #endif
  55. } RegisterID;
  56. typedef enum {
  57. xmm0,
  58. xmm1,
  59. xmm2,
  60. xmm3,
  61. xmm4,
  62. xmm5,
  63. xmm6,
  64. xmm7,
  65. } XMMRegisterID;
  66. }
  67. class X86Assembler {
  68. public:
  69. typedef X86Registers::RegisterID RegisterID;
  70. typedef X86Registers::XMMRegisterID XMMRegisterID;
  71. typedef XMMRegisterID FPRegisterID;
  72. typedef enum {
  73. ConditionO,
  74. ConditionNO,
  75. ConditionB,
  76. ConditionAE,
  77. ConditionE,
  78. ConditionNE,
  79. ConditionBE,
  80. ConditionA,
  81. ConditionS,
  82. ConditionNS,
  83. ConditionP,
  84. ConditionNP,
  85. ConditionL,
  86. ConditionGE,
  87. ConditionLE,
  88. ConditionG,
  89. ConditionC = ConditionB,
  90. ConditionNC = ConditionAE,
  91. } Condition;
  92. private:
  93. typedef enum {
  94. OP_ADD_EvGv = 0x01,
  95. OP_ADD_GvEv = 0x03,
  96. OP_OR_EvGv = 0x09,
  97. OP_OR_GvEv = 0x0B,
  98. OP_2BYTE_ESCAPE = 0x0F,
  99. OP_AND_EvGv = 0x21,
  100. OP_AND_GvEv = 0x23,
  101. OP_SUB_EvGv = 0x29,
  102. OP_SUB_GvEv = 0x2B,
  103. PRE_PREDICT_BRANCH_NOT_TAKEN = 0x2E,
  104. OP_XOR_EvGv = 0x31,
  105. OP_XOR_GvEv = 0x33,
  106. OP_CMP_EvGv = 0x39,
  107. OP_CMP_GvEv = 0x3B,
  108. #if CPU(X86_64)
  109. PRE_REX = 0x40,
  110. #endif
  111. OP_PUSH_EAX = 0x50,
  112. OP_POP_EAX = 0x58,
  113. #if CPU(X86_64)
  114. OP_MOVSXD_GvEv = 0x63,
  115. #endif
  116. PRE_OPERAND_SIZE = 0x66,
  117. PRE_SSE_66 = 0x66,
  118. OP_PUSH_Iz = 0x68,
  119. OP_IMUL_GvEvIz = 0x69,
  120. OP_GROUP1_EbIb = 0x80,
  121. OP_GROUP1_EvIz = 0x81,
  122. OP_GROUP1_EvIb = 0x83,
  123. OP_TEST_EbGb = 0x84,
  124. OP_TEST_EvGv = 0x85,
  125. OP_XCHG_EvGv = 0x87,
  126. OP_MOV_EbGb = 0x88,
  127. OP_MOV_EvGv = 0x89,
  128. OP_MOV_GvEv = 0x8B,
  129. OP_LEA = 0x8D,
  130. OP_GROUP1A_Ev = 0x8F,
  131. OP_NOP = 0x90,
  132. OP_CDQ = 0x99,
  133. OP_MOV_EAXOv = 0xA1,
  134. OP_MOV_OvEAX = 0xA3,
  135. OP_MOV_EAXIv = 0xB8,
  136. OP_GROUP2_EvIb = 0xC1,
  137. OP_RET = 0xC3,
  138. OP_GROUP11_EvIb = 0xC6,
  139. OP_GROUP11_EvIz = 0xC7,
  140. OP_INT3 = 0xCC,
  141. #if OS(ORBIS)
  142. OP_INT = 0xCD,
  143. #endif
  144. OP_GROUP2_Ev1 = 0xD1,
  145. OP_GROUP2_EvCL = 0xD3,
  146. OP_ESCAPE_DD = 0xDD,
  147. OP_CALL_rel32 = 0xE8,
  148. OP_JMP_rel32 = 0xE9,
  149. PRE_SSE_F2 = 0xF2,
  150. PRE_SSE_F3 = 0xF3,
  151. OP_HLT = 0xF4,
  152. OP_GROUP3_EbIb = 0xF6,
  153. OP_GROUP3_Ev = 0xF7,
  154. OP_GROUP3_EvIz = 0xF7, // OP_GROUP3_Ev has an immediate, when instruction is a test.
  155. OP_GROUP5_Ev = 0xFF,
  156. } OneByteOpcodeID;
  157. typedef enum {
  158. OP2_MOVSD_VsdWsd = 0x10,
  159. OP2_MOVSD_WsdVsd = 0x11,
  160. OP2_MOVSS_VsdWsd = 0x10,
  161. OP2_MOVSS_WsdVsd = 0x11,
  162. OP2_CVTSI2SD_VsdEd = 0x2A,
  163. OP2_CVTTSD2SI_GdWsd = 0x2C,
  164. OP2_UCOMISD_VsdWsd = 0x2E,
  165. OP2_ADDSD_VsdWsd = 0x58,
  166. OP2_MULSD_VsdWsd = 0x59,
  167. OP2_CVTSD2SS_VsdWsd = 0x5A,
  168. OP2_CVTSS2SD_VsdWsd = 0x5A,
  169. OP2_SUBSD_VsdWsd = 0x5C,
  170. OP2_DIVSD_VsdWsd = 0x5E,
  171. OP2_SQRTSD_VsdWsd = 0x51,
  172. OP2_ANDNPD_VpdWpd = 0x55,
  173. OP2_XORPD_VpdWpd = 0x57,
  174. OP2_MOVD_VdEd = 0x6E,
  175. OP2_MOVD_EdVd = 0x7E,
  176. OP2_JCC_rel32 = 0x80,
  177. OP_SETCC = 0x90,
  178. OP2_IMUL_GvEv = 0xAF,
  179. OP2_MOVZX_GvEb = 0xB6,
  180. OP2_MOVSX_GvEb = 0xBE,
  181. OP2_MOVZX_GvEw = 0xB7,
  182. OP2_MOVSX_GvEw = 0xBF,
  183. OP2_PEXTRW_GdUdIb = 0xC5,
  184. OP2_PSLLQ_UdqIb = 0x73,
  185. OP2_PSRLQ_UdqIb = 0x73,
  186. OP2_POR_VdqWdq = 0XEB,
  187. } TwoByteOpcodeID;
  188. TwoByteOpcodeID jccRel32(Condition cond)
  189. {
  190. return (TwoByteOpcodeID)(OP2_JCC_rel32 + cond);
  191. }
  192. TwoByteOpcodeID setccOpcode(Condition cond)
  193. {
  194. return (TwoByteOpcodeID)(OP_SETCC + cond);
  195. }
  196. typedef enum {
  197. GROUP1_OP_ADD = 0,
  198. GROUP1_OP_OR = 1,
  199. GROUP1_OP_ADC = 2,
  200. GROUP1_OP_AND = 4,
  201. GROUP1_OP_SUB = 5,
  202. GROUP1_OP_XOR = 6,
  203. GROUP1_OP_CMP = 7,
  204. GROUP1A_OP_POP = 0,
  205. GROUP2_OP_ROL = 0,
  206. GROUP2_OP_ROR = 1,
  207. GROUP2_OP_RCL = 2,
  208. GROUP2_OP_RCR = 3,
  209. GROUP2_OP_SHL = 4,
  210. GROUP2_OP_SHR = 5,
  211. GROUP2_OP_SAR = 7,
  212. GROUP3_OP_TEST = 0,
  213. GROUP3_OP_NOT = 2,
  214. GROUP3_OP_NEG = 3,
  215. GROUP3_OP_IDIV = 7,
  216. GROUP5_OP_CALLN = 2,
  217. GROUP5_OP_JMPN = 4,
  218. GROUP5_OP_PUSH = 6,
  219. GROUP11_MOV = 0,
  220. GROUP14_OP_PSLLQ = 6,
  221. GROUP14_OP_PSRLQ = 2,
  222. ESCAPE_DD_FSTP_doubleReal = 3,
  223. } GroupOpcodeID;
  224. class X86InstructionFormatter;
  225. public:
  226. X86Assembler()
  227. : m_indexOfLastWatchpoint(INT_MIN)
  228. , m_indexOfTailOfLastWatchpoint(INT_MIN)
  229. {
  230. }
  231. // Stack operations:
  232. void push_r(RegisterID reg)
  233. {
  234. m_formatter.oneByteOp(OP_PUSH_EAX, reg);
  235. }
  236. void pop_r(RegisterID reg)
  237. {
  238. m_formatter.oneByteOp(OP_POP_EAX, reg);
  239. }
  240. void push_i32(int imm)
  241. {
  242. m_formatter.oneByteOp(OP_PUSH_Iz);
  243. m_formatter.immediate32(imm);
  244. }
  245. void push_m(int offset, RegisterID base)
  246. {
  247. m_formatter.oneByteOp(OP_GROUP5_Ev, GROUP5_OP_PUSH, base, offset);
  248. }
  249. void pop_m(int offset, RegisterID base)
  250. {
  251. m_formatter.oneByteOp(OP_GROUP1A_Ev, GROUP1A_OP_POP, base, offset);
  252. }
  253. // Arithmetic operations:
  254. #if !CPU(X86_64)
  255. void adcl_im(int imm, const void* addr)
  256. {
  257. if (CAN_SIGN_EXTEND_8_32(imm)) {
  258. m_formatter.oneByteOp(OP_GROUP1_EvIb, GROUP1_OP_ADC, addr);
  259. m_formatter.immediate8(imm);
  260. } else {
  261. m_formatter.oneByteOp(OP_GROUP1_EvIz, GROUP1_OP_ADC, addr);
  262. m_formatter.immediate32(imm);
  263. }
  264. }
  265. #endif
  266. void addl_rr(RegisterID src, RegisterID dst)
  267. {
  268. m_formatter.oneByteOp(OP_ADD_EvGv, src, dst);
  269. }
  270. void addl_mr(int offset, RegisterID base, RegisterID dst)
  271. {
  272. m_formatter.oneByteOp(OP_ADD_GvEv, dst, base, offset);
  273. }
  274. #if !CPU(X86_64)
  275. void addl_mr(const void* addr, RegisterID dst)
  276. {
  277. m_formatter.oneByteOp(OP_ADD_GvEv, dst, addr);
  278. }
  279. #endif
  280. void addl_rm(RegisterID src, int offset, RegisterID base)
  281. {
  282. m_formatter.oneByteOp(OP_ADD_EvGv, src, base, offset);
  283. }
  284. void addl_ir(int imm, RegisterID dst)
  285. {
  286. if (CAN_SIGN_EXTEND_8_32(imm)) {
  287. m_formatter.oneByteOp(OP_GROUP1_EvIb, GROUP1_OP_ADD, dst);
  288. m_formatter.immediate8(imm);
  289. } else {
  290. m_formatter.oneByteOp(OP_GROUP1_EvIz, GROUP1_OP_ADD, dst);
  291. m_formatter.immediate32(imm);
  292. }
  293. }
  294. void addl_im(int imm, int offset, RegisterID base)
  295. {
  296. if (CAN_SIGN_EXTEND_8_32(imm)) {
  297. m_formatter.oneByteOp(OP_GROUP1_EvIb, GROUP1_OP_ADD, base, offset);
  298. m_formatter.immediate8(imm);
  299. } else {
  300. m_formatter.oneByteOp(OP_GROUP1_EvIz, GROUP1_OP_ADD, base, offset);
  301. m_formatter.immediate32(imm);
  302. }
  303. }
  304. #if CPU(X86_64)
  305. void addq_rr(RegisterID src, RegisterID dst)
  306. {
  307. m_formatter.oneByteOp64(OP_ADD_EvGv, src, dst);
  308. }
  309. void addq_mr(int offset, RegisterID base, RegisterID dst)
  310. {
  311. m_formatter.oneByteOp64(OP_ADD_GvEv, dst, base, offset);
  312. }
  313. void addq_ir(int imm, RegisterID dst)
  314. {
  315. if (CAN_SIGN_EXTEND_8_32(imm)) {
  316. m_formatter.oneByteOp64(OP_GROUP1_EvIb, GROUP1_OP_ADD, dst);
  317. m_formatter.immediate8(imm);
  318. } else {
  319. m_formatter.oneByteOp64(OP_GROUP1_EvIz, GROUP1_OP_ADD, dst);
  320. m_formatter.immediate32(imm);
  321. }
  322. }
  323. void addq_im(int imm, int offset, RegisterID base)
  324. {
  325. if (CAN_SIGN_EXTEND_8_32(imm)) {
  326. m_formatter.oneByteOp64(OP_GROUP1_EvIb, GROUP1_OP_ADD, base, offset);
  327. m_formatter.immediate8(imm);
  328. } else {
  329. m_formatter.oneByteOp64(OP_GROUP1_EvIz, GROUP1_OP_ADD, base, offset);
  330. m_formatter.immediate32(imm);
  331. }
  332. }
  333. #else
  334. void addl_im(int imm, const void* addr)
  335. {
  336. if (CAN_SIGN_EXTEND_8_32(imm)) {
  337. m_formatter.oneByteOp(OP_GROUP1_EvIb, GROUP1_OP_ADD, addr);
  338. m_formatter.immediate8(imm);
  339. } else {
  340. m_formatter.oneByteOp(OP_GROUP1_EvIz, GROUP1_OP_ADD, addr);
  341. m_formatter.immediate32(imm);
  342. }
  343. }
  344. #endif
  345. void andl_rr(RegisterID src, RegisterID dst)
  346. {
  347. m_formatter.oneByteOp(OP_AND_EvGv, src, dst);
  348. }
  349. void andl_mr(int offset, RegisterID base, RegisterID dst)
  350. {
  351. m_formatter.oneByteOp(OP_AND_GvEv, dst, base, offset);
  352. }
  353. void andl_rm(RegisterID src, int offset, RegisterID base)
  354. {
  355. m_formatter.oneByteOp(OP_AND_EvGv, src, base, offset);
  356. }
  357. void andl_ir(int imm, RegisterID dst)
  358. {
  359. if (CAN_SIGN_EXTEND_8_32(imm)) {
  360. m_formatter.oneByteOp(OP_GROUP1_EvIb, GROUP1_OP_AND, dst);
  361. m_formatter.immediate8(imm);
  362. } else {
  363. m_formatter.oneByteOp(OP_GROUP1_EvIz, GROUP1_OP_AND, dst);
  364. m_formatter.immediate32(imm);
  365. }
  366. }
  367. void andl_im(int imm, int offset, RegisterID base)
  368. {
  369. if (CAN_SIGN_EXTEND_8_32(imm)) {
  370. m_formatter.oneByteOp(OP_GROUP1_EvIb, GROUP1_OP_AND, base, offset);
  371. m_formatter.immediate8(imm);
  372. } else {
  373. m_formatter.oneByteOp(OP_GROUP1_EvIz, GROUP1_OP_AND, base, offset);
  374. m_formatter.immediate32(imm);
  375. }
  376. }
  377. #if CPU(X86_64)
  378. void andq_rr(RegisterID src, RegisterID dst)
  379. {
  380. m_formatter.oneByteOp64(OP_AND_EvGv, src, dst);
  381. }
  382. void andq_ir(int imm, RegisterID dst)
  383. {
  384. if (CAN_SIGN_EXTEND_8_32(imm)) {
  385. m_formatter.oneByteOp64(OP_GROUP1_EvIb, GROUP1_OP_AND, dst);
  386. m_formatter.immediate8(imm);
  387. } else {
  388. m_formatter.oneByteOp64(OP_GROUP1_EvIz, GROUP1_OP_AND, dst);
  389. m_formatter.immediate32(imm);
  390. }
  391. }
  392. #else
  393. void andl_im(int imm, const void* addr)
  394. {
  395. if (CAN_SIGN_EXTEND_8_32(imm)) {
  396. m_formatter.oneByteOp(OP_GROUP1_EvIb, GROUP1_OP_AND, addr);
  397. m_formatter.immediate8(imm);
  398. } else {
  399. m_formatter.oneByteOp(OP_GROUP1_EvIz, GROUP1_OP_AND, addr);
  400. m_formatter.immediate32(imm);
  401. }
  402. }
  403. #endif
  404. void negl_r(RegisterID dst)
  405. {
  406. m_formatter.oneByteOp(OP_GROUP3_Ev, GROUP3_OP_NEG, dst);
  407. }
  408. #if CPU(X86_64)
  409. void negq_r(RegisterID dst)
  410. {
  411. m_formatter.oneByteOp64(OP_GROUP3_Ev, GROUP3_OP_NEG, dst);
  412. }
  413. #endif
  414. void negl_m(int offset, RegisterID base)
  415. {
  416. m_formatter.oneByteOp(OP_GROUP3_Ev, GROUP3_OP_NEG, base, offset);
  417. }
  418. void notl_r(RegisterID dst)
  419. {
  420. m_formatter.oneByteOp(OP_GROUP3_Ev, GROUP3_OP_NOT, dst);
  421. }
  422. void notl_m(int offset, RegisterID base)
  423. {
  424. m_formatter.oneByteOp(OP_GROUP3_Ev, GROUP3_OP_NOT, base, offset);
  425. }
  426. void orl_rr(RegisterID src, RegisterID dst)
  427. {
  428. m_formatter.oneByteOp(OP_OR_EvGv, src, dst);
  429. }
  430. void orl_mr(int offset, RegisterID base, RegisterID dst)
  431. {
  432. m_formatter.oneByteOp(OP_OR_GvEv, dst, base, offset);
  433. }
  434. void orl_rm(RegisterID src, int offset, RegisterID base)
  435. {
  436. m_formatter.oneByteOp(OP_OR_EvGv, src, base, offset);
  437. }
  438. void orl_ir(int imm, RegisterID dst)
  439. {
  440. if (CAN_SIGN_EXTEND_8_32(imm)) {
  441. m_formatter.oneByteOp(OP_GROUP1_EvIb, GROUP1_OP_OR, dst);
  442. m_formatter.immediate8(imm);
  443. } else {
  444. m_formatter.oneByteOp(OP_GROUP1_EvIz, GROUP1_OP_OR, dst);
  445. m_formatter.immediate32(imm);
  446. }
  447. }
  448. void orl_im(int imm, int offset, RegisterID base)
  449. {
  450. if (CAN_SIGN_EXTEND_8_32(imm)) {
  451. m_formatter.oneByteOp(OP_GROUP1_EvIb, GROUP1_OP_OR, base, offset);
  452. m_formatter.immediate8(imm);
  453. } else {
  454. m_formatter.oneByteOp(OP_GROUP1_EvIz, GROUP1_OP_OR, base, offset);
  455. m_formatter.immediate32(imm);
  456. }
  457. }
  458. #if CPU(X86_64)
  459. void orq_rr(RegisterID src, RegisterID dst)
  460. {
  461. m_formatter.oneByteOp64(OP_OR_EvGv, src, dst);
  462. }
  463. void orq_ir(int imm, RegisterID dst)
  464. {
  465. if (CAN_SIGN_EXTEND_8_32(imm)) {
  466. m_formatter.oneByteOp64(OP_GROUP1_EvIb, GROUP1_OP_OR, dst);
  467. m_formatter.immediate8(imm);
  468. } else {
  469. m_formatter.oneByteOp64(OP_GROUP1_EvIz, GROUP1_OP_OR, dst);
  470. m_formatter.immediate32(imm);
  471. }
  472. }
  473. #else
  474. void orl_im(int imm, const void* addr)
  475. {
  476. if (CAN_SIGN_EXTEND_8_32(imm)) {
  477. m_formatter.oneByteOp(OP_GROUP1_EvIb, GROUP1_OP_OR, addr);
  478. m_formatter.immediate8(imm);
  479. } else {
  480. m_formatter.oneByteOp(OP_GROUP1_EvIz, GROUP1_OP_OR, addr);
  481. m_formatter.immediate32(imm);
  482. }
  483. }
  484. void orl_rm(RegisterID src, const void* addr)
  485. {
  486. m_formatter.oneByteOp(OP_OR_EvGv, src, addr);
  487. }
  488. #endif
  489. void subl_rr(RegisterID src, RegisterID dst)
  490. {
  491. m_formatter.oneByteOp(OP_SUB_EvGv, src, dst);
  492. }
  493. void subl_mr(int offset, RegisterID base, RegisterID dst)
  494. {
  495. m_formatter.oneByteOp(OP_SUB_GvEv, dst, base, offset);
  496. }
  497. void subl_rm(RegisterID src, int offset, RegisterID base)
  498. {
  499. m_formatter.oneByteOp(OP_SUB_EvGv, src, base, offset);
  500. }
  501. void subl_ir(int imm, RegisterID dst)
  502. {
  503. if (CAN_SIGN_EXTEND_8_32(imm)) {
  504. m_formatter.oneByteOp(OP_GROUP1_EvIb, GROUP1_OP_SUB, dst);
  505. m_formatter.immediate8(imm);
  506. } else {
  507. m_formatter.oneByteOp(OP_GROUP1_EvIz, GROUP1_OP_SUB, dst);
  508. m_formatter.immediate32(imm);
  509. }
  510. }
  511. void subl_im(int imm, int offset, RegisterID base)
  512. {
  513. if (CAN_SIGN_EXTEND_8_32(imm)) {
  514. m_formatter.oneByteOp(OP_GROUP1_EvIb, GROUP1_OP_SUB, base, offset);
  515. m_formatter.immediate8(imm);
  516. } else {
  517. m_formatter.oneByteOp(OP_GROUP1_EvIz, GROUP1_OP_SUB, base, offset);
  518. m_formatter.immediate32(imm);
  519. }
  520. }
  521. #if CPU(X86_64)
  522. void subq_rr(RegisterID src, RegisterID dst)
  523. {
  524. m_formatter.oneByteOp64(OP_SUB_EvGv, src, dst);
  525. }
  526. void subq_ir(int imm, RegisterID dst)
  527. {
  528. if (CAN_SIGN_EXTEND_8_32(imm)) {
  529. m_formatter.oneByteOp64(OP_GROUP1_EvIb, GROUP1_OP_SUB, dst);
  530. m_formatter.immediate8(imm);
  531. } else {
  532. m_formatter.oneByteOp64(OP_GROUP1_EvIz, GROUP1_OP_SUB, dst);
  533. m_formatter.immediate32(imm);
  534. }
  535. }
  536. #else
  537. void subl_im(int imm, const void* addr)
  538. {
  539. if (CAN_SIGN_EXTEND_8_32(imm)) {
  540. m_formatter.oneByteOp(OP_GROUP1_EvIb, GROUP1_OP_SUB, addr);
  541. m_formatter.immediate8(imm);
  542. } else {
  543. m_formatter.oneByteOp(OP_GROUP1_EvIz, GROUP1_OP_SUB, addr);
  544. m_formatter.immediate32(imm);
  545. }
  546. }
  547. #endif
  548. void xorl_rr(RegisterID src, RegisterID dst)
  549. {
  550. m_formatter.oneByteOp(OP_XOR_EvGv, src, dst);
  551. }
  552. void xorl_mr(int offset, RegisterID base, RegisterID dst)
  553. {
  554. m_formatter.oneByteOp(OP_XOR_GvEv, dst, base, offset);
  555. }
  556. void xorl_rm(RegisterID src, int offset, RegisterID base)
  557. {
  558. m_formatter.oneByteOp(OP_XOR_EvGv, src, base, offset);
  559. }
  560. void xorl_im(int imm, int offset, RegisterID base)
  561. {
  562. if (CAN_SIGN_EXTEND_8_32(imm)) {
  563. m_formatter.oneByteOp(OP_GROUP1_EvIb, GROUP1_OP_XOR, base, offset);
  564. m_formatter.immediate8(imm);
  565. } else {
  566. m_formatter.oneByteOp(OP_GROUP1_EvIz, GROUP1_OP_XOR, base, offset);
  567. m_formatter.immediate32(imm);
  568. }
  569. }
  570. void xorl_ir(int imm, RegisterID dst)
  571. {
  572. if (CAN_SIGN_EXTEND_8_32(imm)) {
  573. m_formatter.oneByteOp(OP_GROUP1_EvIb, GROUP1_OP_XOR, dst);
  574. m_formatter.immediate8(imm);
  575. } else {
  576. m_formatter.oneByteOp(OP_GROUP1_EvIz, GROUP1_OP_XOR, dst);
  577. m_formatter.immediate32(imm);
  578. }
  579. }
  580. #if CPU(X86_64)
  581. void xorq_rr(RegisterID src, RegisterID dst)
  582. {
  583. m_formatter.oneByteOp64(OP_XOR_EvGv, src, dst);
  584. }
  585. void xorq_ir(int imm, RegisterID dst)
  586. {
  587. if (CAN_SIGN_EXTEND_8_32(imm)) {
  588. m_formatter.oneByteOp64(OP_GROUP1_EvIb, GROUP1_OP_XOR, dst);
  589. m_formatter.immediate8(imm);
  590. } else {
  591. m_formatter.oneByteOp64(OP_GROUP1_EvIz, GROUP1_OP_XOR, dst);
  592. m_formatter.immediate32(imm);
  593. }
  594. }
  595. void xorq_rm(RegisterID src, int offset, RegisterID base)
  596. {
  597. m_formatter.oneByteOp64(OP_XOR_EvGv, src, base, offset);
  598. }
  599. void rorq_i8r(int imm, RegisterID dst)
  600. {
  601. if (imm == 1)
  602. m_formatter.oneByteOp64(OP_GROUP2_Ev1, GROUP2_OP_ROR, dst);
  603. else {
  604. m_formatter.oneByteOp64(OP_GROUP2_EvIb, GROUP2_OP_ROR, dst);
  605. m_formatter.immediate8(imm);
  606. }
  607. }
  608. #endif
  609. void sarl_i8r(int imm, RegisterID dst)
  610. {
  611. if (imm == 1)
  612. m_formatter.oneByteOp(OP_GROUP2_Ev1, GROUP2_OP_SAR, dst);
  613. else {
  614. m_formatter.oneByteOp(OP_GROUP2_EvIb, GROUP2_OP_SAR, dst);
  615. m_formatter.immediate8(imm);
  616. }
  617. }
  618. void sarl_CLr(RegisterID dst)
  619. {
  620. m_formatter.oneByteOp(OP_GROUP2_EvCL, GROUP2_OP_SAR, dst);
  621. }
  622. void shrl_i8r(int imm, RegisterID dst)
  623. {
  624. if (imm == 1)
  625. m_formatter.oneByteOp(OP_GROUP2_Ev1, GROUP2_OP_SHR, dst);
  626. else {
  627. m_formatter.oneByteOp(OP_GROUP2_EvIb, GROUP2_OP_SHR, dst);
  628. m_formatter.immediate8(imm);
  629. }
  630. }
  631. void shrl_CLr(RegisterID dst)
  632. {
  633. m_formatter.oneByteOp(OP_GROUP2_EvCL, GROUP2_OP_SHR, dst);
  634. }
  635. void shll_i8r(int imm, RegisterID dst)
  636. {
  637. if (imm == 1)
  638. m_formatter.oneByteOp(OP_GROUP2_Ev1, GROUP2_OP_SHL, dst);
  639. else {
  640. m_formatter.oneByteOp(OP_GROUP2_EvIb, GROUP2_OP_SHL, dst);
  641. m_formatter.immediate8(imm);
  642. }
  643. }
  644. void shll_CLr(RegisterID dst)
  645. {
  646. m_formatter.oneByteOp(OP_GROUP2_EvCL, GROUP2_OP_SHL, dst);
  647. }
  648. #if CPU(X86_64)
  649. void sarq_CLr(RegisterID dst)
  650. {
  651. m_formatter.oneByteOp64(OP_GROUP2_EvCL, GROUP2_OP_SAR, dst);
  652. }
  653. void sarq_i8r(int imm, RegisterID dst)
  654. {
  655. if (imm == 1)
  656. m_formatter.oneByteOp64(OP_GROUP2_Ev1, GROUP2_OP_SAR, dst);
  657. else {
  658. m_formatter.oneByteOp64(OP_GROUP2_EvIb, GROUP2_OP_SAR, dst);
  659. m_formatter.immediate8(imm);
  660. }
  661. }
  662. #endif
  663. void imull_rr(RegisterID src, RegisterID dst)
  664. {
  665. m_formatter.twoByteOp(OP2_IMUL_GvEv, dst, src);
  666. }
  667. void imull_mr(int offset, RegisterID base, RegisterID dst)
  668. {
  669. m_formatter.twoByteOp(OP2_IMUL_GvEv, dst, base, offset);
  670. }
  671. void imull_i32r(RegisterID src, int32_t value, RegisterID dst)
  672. {
  673. m_formatter.oneByteOp(OP_IMUL_GvEvIz, dst, src);
  674. m_formatter.immediate32(value);
  675. }
  676. void idivl_r(RegisterID dst)
  677. {
  678. m_formatter.oneByteOp(OP_GROUP3_Ev, GROUP3_OP_IDIV, dst);
  679. }
  680. // Comparisons:
  681. void cmpl_rr(RegisterID src, RegisterID dst)
  682. {
  683. m_formatter.oneByteOp(OP_CMP_EvGv, src, dst);
  684. }
  685. void cmpl_rm(RegisterID src, int offset, RegisterID base)
  686. {
  687. m_formatter.oneByteOp(OP_CMP_EvGv, src, base, offset);
  688. }
  689. void cmpl_mr(int offset, RegisterID base, RegisterID src)
  690. {
  691. m_formatter.oneByteOp(OP_CMP_GvEv, src, base, offset);
  692. }
  693. void cmpl_ir(int imm, RegisterID dst)
  694. {
  695. if (CAN_SIGN_EXTEND_8_32(imm)) {
  696. m_formatter.oneByteOp(OP_GROUP1_EvIb, GROUP1_OP_CMP, dst);
  697. m_formatter.immediate8(imm);
  698. } else {
  699. m_formatter.oneByteOp(OP_GROUP1_EvIz, GROUP1_OP_CMP, dst);
  700. m_formatter.immediate32(imm);
  701. }
  702. }
  703. void cmpl_ir_force32(int imm, RegisterID dst)
  704. {
  705. m_formatter.oneByteOp(OP_GROUP1_EvIz, GROUP1_OP_CMP, dst);
  706. m_formatter.immediate32(imm);
  707. }
  708. void cmpl_im(int imm, int offset, RegisterID base)
  709. {
  710. if (CAN_SIGN_EXTEND_8_32(imm)) {
  711. m_formatter.oneByteOp(OP_GROUP1_EvIb, GROUP1_OP_CMP, base, offset);
  712. m_formatter.immediate8(imm);
  713. } else {
  714. m_formatter.oneByteOp(OP_GROUP1_EvIz, GROUP1_OP_CMP, base, offset);
  715. m_formatter.immediate32(imm);
  716. }
  717. }
  718. void cmpb_im(int imm, int offset, RegisterID base)
  719. {
  720. m_formatter.oneByteOp(OP_GROUP1_EbIb, GROUP1_OP_CMP, base, offset);
  721. m_formatter.immediate8(imm);
  722. }
  723. void cmpb_im(int imm, int offset, RegisterID base, RegisterID index, int scale)
  724. {
  725. m_formatter.oneByteOp(OP_GROUP1_EbIb, GROUP1_OP_CMP, base, index, scale, offset);
  726. m_formatter.immediate8(imm);
  727. }
  728. #if CPU(X86)
  729. void cmpb_im(int imm, const void* addr)
  730. {
  731. m_formatter.oneByteOp(OP_GROUP1_EbIb, GROUP1_OP_CMP, addr);
  732. m_formatter.immediate8(imm);
  733. }
  734. #endif
  735. void cmpl_im(int imm, int offset, RegisterID base, RegisterID index, int scale)
  736. {
  737. if (CAN_SIGN_EXTEND_8_32(imm)) {
  738. m_formatter.oneByteOp(OP_GROUP1_EvIb, GROUP1_OP_CMP, base, index, scale, offset);
  739. m_formatter.immediate8(imm);
  740. } else {
  741. m_formatter.oneByteOp(OP_GROUP1_EvIz, GROUP1_OP_CMP, base, index, scale, offset);
  742. m_formatter.immediate32(imm);
  743. }
  744. }
  745. void cmpl_im_force32(int imm, int offset, RegisterID base)
  746. {
  747. m_formatter.oneByteOp(OP_GROUP1_EvIz, GROUP1_OP_CMP, base, offset);
  748. m_formatter.immediate32(imm);
  749. }
  750. #if CPU(X86_64)
  751. void cmpq_rr(RegisterID src, RegisterID dst)
  752. {
  753. m_formatter.oneByteOp64(OP_CMP_EvGv, src, dst);
  754. }
  755. void cmpq_rm(RegisterID src, int offset, RegisterID base)
  756. {
  757. m_formatter.oneByteOp64(OP_CMP_EvGv, src, base, offset);
  758. }
  759. void cmpq_mr(int offset, RegisterID base, RegisterID src)
  760. {
  761. m_formatter.oneByteOp64(OP_CMP_GvEv, src, base, offset);
  762. }
  763. void cmpq_ir(int imm, RegisterID dst)
  764. {
  765. if (CAN_SIGN_EXTEND_8_32(imm)) {
  766. m_formatter.oneByteOp64(OP_GROUP1_EvIb, GROUP1_OP_CMP, dst);
  767. m_formatter.immediate8(imm);
  768. } else {
  769. m_formatter.oneByteOp64(OP_GROUP1_EvIz, GROUP1_OP_CMP, dst);
  770. m_formatter.immediate32(imm);
  771. }
  772. }
  773. void cmpq_im(int imm, int offset, RegisterID base)
  774. {
  775. if (CAN_SIGN_EXTEND_8_32(imm)) {
  776. m_formatter.oneByteOp64(OP_GROUP1_EvIb, GROUP1_OP_CMP, base, offset);
  777. m_formatter.immediate8(imm);
  778. } else {
  779. m_formatter.oneByteOp64(OP_GROUP1_EvIz, GROUP1_OP_CMP, base, offset);
  780. m_formatter.immediate32(imm);
  781. }
  782. }
  783. void cmpq_im(int imm, int offset, RegisterID base, RegisterID index, int scale)
  784. {
  785. if (CAN_SIGN_EXTEND_8_32(imm)) {
  786. m_formatter.oneByteOp64(OP_GROUP1_EvIb, GROUP1_OP_CMP, base, index, scale, offset);
  787. m_formatter.immediate8(imm);
  788. } else {
  789. m_formatter.oneByteOp64(OP_GROUP1_EvIz, GROUP1_OP_CMP, base, index, scale, offset);
  790. m_formatter.immediate32(imm);
  791. }
  792. }
  793. #else
  794. void cmpl_rm(RegisterID reg, const void* addr)
  795. {
  796. m_formatter.oneByteOp(OP_CMP_EvGv, reg, addr);
  797. }
  798. void cmpl_im(int imm, const void* addr)
  799. {
  800. if (CAN_SIGN_EXTEND_8_32(imm)) {
  801. m_formatter.oneByteOp(OP_GROUP1_EvIb, GROUP1_OP_CMP, addr);
  802. m_formatter.immediate8(imm);
  803. } else {
  804. m_formatter.oneByteOp(OP_GROUP1_EvIz, GROUP1_OP_CMP, addr);
  805. m_formatter.immediate32(imm);
  806. }
  807. }
  808. #endif
  809. void cmpw_ir(int imm, RegisterID dst)
  810. {
  811. if (CAN_SIGN_EXTEND_8_32(imm)) {
  812. m_formatter.prefix(PRE_OPERAND_SIZE);
  813. m_formatter.oneByteOp(OP_GROUP1_EvIb, GROUP1_OP_CMP, dst);
  814. m_formatter.immediate8(imm);
  815. } else {
  816. m_formatter.prefix(PRE_OPERAND_SIZE);
  817. m_formatter.oneByteOp(OP_GROUP1_EvIz, GROUP1_OP_CMP, dst);
  818. m_formatter.immediate16(imm);
  819. }
  820. }
  821. void cmpw_rm(RegisterID src, int offset, RegisterID base, RegisterID index, int scale)
  822. {
  823. m_formatter.prefix(PRE_OPERAND_SIZE);
  824. m_formatter.oneByteOp(OP_CMP_EvGv, src, base, index, scale, offset);
  825. }
  826. void cmpw_im(int imm, int offset, RegisterID base, RegisterID index, int scale)
  827. {
  828. if (CAN_SIGN_EXTEND_8_32(imm)) {
  829. m_formatter.prefix(PRE_OPERAND_SIZE);
  830. m_formatter.oneByteOp(OP_GROUP1_EvIb, GROUP1_OP_CMP, base, index, scale, offset);
  831. m_formatter.immediate8(imm);
  832. } else {
  833. m_formatter.prefix(PRE_OPERAND_SIZE);
  834. m_formatter.oneByteOp(OP_GROUP1_EvIz, GROUP1_OP_CMP, base, index, scale, offset);
  835. m_formatter.immediate16(imm);
  836. }
  837. }
  838. void testl_rr(RegisterID src, RegisterID dst)
  839. {
  840. m_formatter.oneByteOp(OP_TEST_EvGv, src, dst);
  841. }
  842. void testl_i32r(int imm, RegisterID dst)
  843. {
  844. m_formatter.oneByteOp(OP_GROUP3_EvIz, GROUP3_OP_TEST, dst);
  845. m_formatter.immediate32(imm);
  846. }
  847. void testl_i32m(int imm, int offset, RegisterID base)
  848. {
  849. m_formatter.oneByteOp(OP_GROUP3_EvIz, GROUP3_OP_TEST, base, offset);
  850. m_formatter.immediate32(imm);
  851. }
  852. void testb_rr(RegisterID src, RegisterID dst)
  853. {
  854. m_formatter.oneByteOp8(OP_TEST_EbGb, src, dst);
  855. }
  856. void testb_im(int imm, int offset, RegisterID base)
  857. {
  858. m_formatter.oneByteOp(OP_GROUP3_EbIb, GROUP3_OP_TEST, base, offset);
  859. m_formatter.immediate8(imm);
  860. }
  861. void testb_im(int imm, int offset, RegisterID base, RegisterID index, int scale)
  862. {
  863. m_formatter.oneByteOp(OP_GROUP3_EbIb, GROUP3_OP_TEST, base, index, scale, offset);
  864. m_formatter.immediate8(imm);
  865. }
  866. #if CPU(X86)
  867. void testb_im(int imm, const void* addr)
  868. {
  869. m_formatter.oneByteOp(OP_GROUP3_EbIb, GROUP3_OP_TEST, addr);
  870. m_formatter.immediate8(imm);
  871. }
  872. #endif
  873. void testl_i32m(int imm, int offset, RegisterID base, RegisterID index, int scale)
  874. {
  875. m_formatter.oneByteOp(OP_GROUP3_EvIz, GROUP3_OP_TEST, base, index, scale, offset);
  876. m_formatter.immediate32(imm);
  877. }
  878. #if CPU(X86_64)
  879. void testq_rr(RegisterID src, RegisterID dst)
  880. {
  881. m_formatter.oneByteOp64(OP_TEST_EvGv, src, dst);
  882. }
  883. void testq_rm(RegisterID src, int offset, RegisterID base)
  884. {
  885. m_formatter.oneByteOp64(OP_TEST_EvGv, src, base, offset);
  886. }
  887. void testq_i32r(int imm, RegisterID dst)
  888. {
  889. m_formatter.oneByteOp64(OP_GROUP3_EvIz, GROUP3_OP_TEST, dst);
  890. m_formatter.immediate32(imm);
  891. }
  892. void testq_i32m(int imm, int offset, RegisterID base)
  893. {
  894. m_formatter.oneByteOp64(OP_GROUP3_EvIz, GROUP3_OP_TEST, base, offset);
  895. m_formatter.immediate32(imm);
  896. }
  897. void testq_i32m(int imm, int offset, RegisterID base, RegisterID index, int scale)
  898. {
  899. m_formatter.oneByteOp64(OP_GROUP3_EvIz, GROUP3_OP_TEST, base, index, scale, offset);
  900. m_formatter.immediate32(imm);
  901. }
  902. #endif
  903. void testw_rr(RegisterID src, RegisterID dst)
  904. {
  905. m_formatter.prefix(PRE_OPERAND_SIZE);
  906. m_formatter.oneByteOp(OP_TEST_EvGv, src, dst);
  907. }
  908. void testb_i8r(int imm, RegisterID dst)
  909. {
  910. m_formatter.oneByteOp8(OP_GROUP3_EbIb, GROUP3_OP_TEST, dst);
  911. m_formatter.immediate8(imm);
  912. }
  913. void setCC_r(Condition cond, RegisterID dst)
  914. {
  915. m_formatter.twoByteOp8(setccOpcode(cond), (GroupOpcodeID)0, dst);
  916. }
  917. void sete_r(RegisterID dst)
  918. {
  919. m_formatter.twoByteOp8(setccOpcode(ConditionE), (GroupOpcodeID)0, dst);
  920. }
  921. void setz_r(RegisterID dst)
  922. {
  923. sete_r(dst);
  924. }
  925. void setne_r(RegisterID dst)
  926. {
  927. m_formatter.twoByteOp8(setccOpcode(ConditionNE), (GroupOpcodeID)0, dst);
  928. }
  929. void setnz_r(RegisterID dst)
  930. {
  931. setne_r(dst);
  932. }
  933. // Various move ops:
  934. void cdq()
  935. {
  936. m_formatter.oneByteOp(OP_CDQ);
  937. }
  938. void fstpl(int offset, RegisterID base)
  939. {
  940. m_formatter.oneByteOp(OP_ESCAPE_DD, ESCAPE_DD_FSTP_doubleReal, base, offset);
  941. }
  942. void xchgl_rr(RegisterID src, RegisterID dst)
  943. {
  944. m_formatter.oneByteOp(OP_XCHG_EvGv, src, dst);
  945. }
  946. #if CPU(X86_64)
  947. void xchgq_rr(RegisterID src, RegisterID dst)
  948. {
  949. m_formatter.oneByteOp64(OP_XCHG_EvGv, src, dst);
  950. }
  951. #endif
  952. void movl_rr(RegisterID src, RegisterID dst)
  953. {
  954. m_formatter.oneByteOp(OP_MOV_EvGv, src, dst);
  955. }
  956. void movl_rm(RegisterID src, int offset, RegisterID base)
  957. {
  958. m_formatter.oneByteOp(OP_MOV_EvGv, src, base, offset);
  959. }
  960. void movl_rm_disp32(RegisterID src, int offset, RegisterID base)
  961. {
  962. m_formatter.oneByteOp_disp32(OP_MOV_EvGv, src, base, offset);
  963. }
  964. void movl_rm(RegisterID src, int offset, RegisterID base, RegisterID index, int scale)
  965. {
  966. m_formatter.oneByteOp(OP_MOV_EvGv, src, base, index, scale, offset);
  967. }
  968. void movl_mEAX(const void* addr)
  969. {
  970. m_formatter.oneByteOp(OP_MOV_EAXOv);
  971. #if CPU(X86_64)
  972. m_formatter.immediate64(reinterpret_cast<int64_t>(addr));
  973. #else
  974. m_formatter.immediate32(reinterpret_cast<int>(addr));
  975. #endif
  976. }
  977. void movl_mr(int offset, RegisterID base, RegisterID dst)
  978. {
  979. m_formatter.oneByteOp(OP_MOV_GvEv, dst, base, offset);
  980. }
  981. void movl_mr_disp32(int offset, RegisterID base, RegisterID dst)
  982. {
  983. m_formatter.oneByteOp_disp32(OP_MOV_GvEv, dst, base, offset);
  984. }
  985. void movl_mr_disp8(int offset, RegisterID base, RegisterID dst)
  986. {
  987. m_formatter.oneByteOp_disp8(OP_MOV_GvEv, dst, base, offset);
  988. }
  989. void movl_mr(int offset, RegisterID base, RegisterID index, int scale, RegisterID dst)
  990. {
  991. m_formatter.oneByteOp(OP_MOV_GvEv, dst, base, index, scale, offset);
  992. }
  993. void movl_i32r(int imm, RegisterID dst)
  994. {
  995. m_formatter.oneByteOp(OP_MOV_EAXIv, dst);
  996. m_formatter.immediate32(imm);
  997. }
  998. void movl_i32m(int imm, int offset, RegisterID base)
  999. {
  1000. m_formatter.oneByteOp(OP_GROUP11_EvIz, GROUP11_MOV, base, offset);
  1001. m_formatter.immediate32(imm);
  1002. }
  1003. void movl_i32m(int imm, int offset, RegisterID base, RegisterID index, int scale)
  1004. {
  1005. m_formatter.oneByteOp(OP_GROUP11_EvIz, GROUP11_MOV, base, index, scale, offset);
  1006. m_formatter.immediate32(imm);
  1007. }
  1008. #if !CPU(X86_64)
  1009. void movb_i8m(int imm, const void* addr)
  1010. {
  1011. ASSERT(-128 <= imm && imm < 128);
  1012. m_formatter.oneByteOp(OP_GROUP11_EvIb, GROUP11_MOV, addr);
  1013. m_formatter.immediate8(imm);
  1014. }
  1015. #endif
  1016. void movb_i8m(int imm, int offset, RegisterID base)
  1017. {
  1018. ASSERT(-128 <= imm && imm < 128);
  1019. m_formatter.oneByteOp(OP_GROUP11_EvIb, GROUP11_MOV, base, offset);
  1020. m_formatter.immediate8(imm);
  1021. }
  1022. void movb_i8m(int imm, int offset, RegisterID base, RegisterID index, int scale)
  1023. {
  1024. ASSERT(-128 <= imm && imm < 128);
  1025. m_formatter.oneByteOp(OP_GROUP11_EvIb, GROUP11_MOV, base, index, scale, offset);
  1026. m_formatter.immediate8(imm);
  1027. }
  1028. void movb_rm(RegisterID src, int offset, RegisterID base, RegisterID index, int scale)
  1029. {
  1030. m_formatter.oneByteOp8(OP_MOV_EbGb, src, base, index, scale, offset);
  1031. }
  1032. void movw_rm(RegisterID src, int offset, RegisterID base, RegisterID index, int scale)
  1033. {
  1034. m_formatter.prefix(PRE_OPERAND_SIZE);
  1035. m_formatter.oneByteOp8(OP_MOV_EvGv, src, base, index, scale, offset);
  1036. }
  1037. void movl_EAXm(const void* addr)
  1038. {
  1039. m_formatter.oneByteOp(OP_MOV_OvEAX);
  1040. #if CPU(X86_64)
  1041. m_formatter.immediate64(reinterpret_cast<int64_t>(addr));
  1042. #else
  1043. m_formatter.immediate32(reinterpret_cast<int>(addr));
  1044. #endif
  1045. }
  1046. #if CPU(X86_64)
  1047. void movq_rr(RegisterID src, RegisterID dst)
  1048. {
  1049. m_formatter.oneByteOp64(OP_MOV_EvGv, src, dst);
  1050. }
  1051. void movq_rm(RegisterID src, int offset, RegisterID base)
  1052. {
  1053. m_formatter.oneByteOp64(OP_MOV_EvGv, src, base, offset);
  1054. }
  1055. void movq_rm_disp32(RegisterID src, int offset, RegisterID base)
  1056. {
  1057. m_formatter.oneByteOp64_disp32(OP_MOV_EvGv, src, base, offset);
  1058. }
  1059. void movq_rm(RegisterID src, int offset, RegisterID base, RegisterID index, int scale)
  1060. {
  1061. m_formatter.oneByteOp64(OP_MOV_EvGv, src, base, index, scale, offset);
  1062. }
  1063. void movq_mEAX(const void* addr)
  1064. {
  1065. m_formatter.oneByteOp64(OP_MOV_EAXOv);
  1066. m_formatter.immediate64(reinterpret_cast<int64_t>(addr));
  1067. }
  1068. void movq_EAXm(const void* addr)
  1069. {
  1070. m_formatter.oneByteOp64(OP_MOV_OvEAX);
  1071. m_formatter.immediate64(reinterpret_cast<int64_t>(addr));
  1072. }
  1073. void movq_mr(int offset, RegisterID base, RegisterID dst)
  1074. {
  1075. m_formatter.oneByteOp64(OP_MOV_GvEv, dst, base, offset);
  1076. }
  1077. void movq_mr_disp32(int offset, RegisterID base, RegisterID dst)
  1078. {
  1079. m_formatter.oneByteOp64_disp32(OP_MOV_GvEv, dst, base, offset);
  1080. }
  1081. void movq_mr_disp8(int offset, RegisterID base, RegisterID dst)
  1082. {
  1083. m_formatter.oneByteOp64_disp8(OP_MOV_GvEv, dst, base, offset);
  1084. }
  1085. void movq_mr(int offset, RegisterID base, RegisterID index, int scale, RegisterID dst)
  1086. {
  1087. m_formatter.oneByteOp64(OP_MOV_GvEv, dst, base, index, scale, offset);
  1088. }
  1089. void movq_i32m(int imm, int offset, RegisterID base)
  1090. {
  1091. m_formatter.oneByteOp64(OP_GROUP11_EvIz, GROUP11_MOV, base, offset);
  1092. m_formatter.immediate32(imm);
  1093. }
  1094. void movq_i64r(int64_t imm, RegisterID dst)
  1095. {
  1096. m_formatter.oneByteOp64(OP_MOV_EAXIv, dst);
  1097. m_formatter.immediate64(imm);
  1098. }
  1099. void movsxd_rr(RegisterID src, RegisterID dst)
  1100. {
  1101. m_formatter.oneByteOp64(OP_MOVSXD_GvEv, dst, src);
  1102. }
  1103. #else
  1104. void movl_rm(RegisterID src, const void* addr)
  1105. {
  1106. if (src == X86Registers::eax)
  1107. movl_EAXm(addr);
  1108. else
  1109. m_formatter.oneByteOp(OP_MOV_EvGv, src, addr);
  1110. }
  1111. void movl_mr(const void* addr, RegisterID dst)
  1112. {
  1113. if (dst == X86Registers::eax)
  1114. movl_mEAX(addr);
  1115. else
  1116. m_formatter.oneByteOp(OP_MOV_GvEv, dst, addr);
  1117. }
  1118. void movl_i32m(int imm, const void* addr)
  1119. {
  1120. m_formatter.oneByteOp(OP_GROUP11_EvIz, GROUP11_MOV, addr);
  1121. m_formatter.immediate32(imm);
  1122. }
  1123. #endif
  1124. void movzwl_mr(int offset, RegisterID base, RegisterID dst)
  1125. {
  1126. m_formatter.twoByteOp(OP2_MOVZX_GvEw, dst, base, offset);
  1127. }
  1128. void movzwl_mr(int offset, RegisterID base, RegisterID index, int scale, RegisterID dst)
  1129. {
  1130. m_formatter.twoByteOp(OP2_MOVZX_GvEw, dst, base, index, scale, offset);
  1131. }
  1132. void movswl_mr(int offset, RegisterID base, RegisterID dst)
  1133. {
  1134. m_formatter.twoByteOp(OP2_MOVSX_GvEw, dst, base, offset);
  1135. }
  1136. void movswl_mr(int offset, RegisterID base, RegisterID index, int scale, RegisterID dst)
  1137. {
  1138. m_formatter.twoByteOp(OP2_MOVSX_GvEw, dst, base, index, scale, offset);
  1139. }
  1140. void movzbl_mr(int offset, RegisterID base, RegisterID dst)
  1141. {
  1142. m_formatter.twoByteOp(OP2_MOVZX_GvEb, dst, base, offset);
  1143. }
  1144. void movzbl_mr(int offset, RegisterID base, RegisterID index, int scale, RegisterID dst)
  1145. {
  1146. m_formatter.twoByteOp(OP2_MOVZX_GvEb, dst, base, index, scale, offset);
  1147. }
  1148. void movsbl_mr(int offset, RegisterID base, RegisterID dst)
  1149. {
  1150. m_formatter.twoByteOp(OP2_MOVSX_GvEb, dst, base, offset);
  1151. }
  1152. void movsbl_mr(int offset, RegisterID base, RegisterID index, int scale, RegisterID dst)
  1153. {
  1154. m_formatter.twoByteOp(OP2_MOVSX_GvEb, dst, base, index, scale, offset);
  1155. }
  1156. void movzbl_rr(RegisterID src, RegisterID dst)
  1157. {
  1158. // In 64-bit, this may cause an unnecessary REX to be planted (if the dst register
  1159. // is in the range ESP-EDI, and the src would not have required a REX). Unneeded
  1160. // REX prefixes are defined to be silently ignored by the processor.
  1161. m_formatter.twoByteOp8(OP2_MOVZX_GvEb, dst, src);
  1162. }
  1163. void leal_mr(int offset, RegisterID base, RegisterID dst)
  1164. {
  1165. m_formatter.oneByteOp(OP_LEA, dst, base, offset);
  1166. }
  1167. #if CPU(X86_64)
  1168. void leaq_mr(int offset, RegisterID base, RegisterID dst)
  1169. {
  1170. m_formatter.oneByteOp64(OP_LEA, dst, base, offset);
  1171. }
  1172. #endif
  1173. // Flow control:
  1174. AssemblerLabel call()
  1175. {
  1176. m_formatter.oneByteOp(OP_CALL_rel32);
  1177. return m_formatter.immediateRel32();
  1178. }
  1179. AssemblerLabel call(RegisterID dst)
  1180. {
  1181. m_formatter.oneByteOp(OP_GROUP5_Ev, GROUP5_OP_CALLN, dst);
  1182. return m_formatter.label();
  1183. }
  1184. void call_m(int offset, RegisterID base)
  1185. {
  1186. m_formatter.oneByteOp(OP_GROUP5_Ev, GROUP5_OP_CALLN, base, offset);
  1187. }
  1188. AssemblerLabel jmp()
  1189. {
  1190. m_formatter.oneByteOp(OP_JMP_rel32);
  1191. return m_formatter.immediateRel32();
  1192. }
  1193. // Return a AssemblerLabel so we have a label to the jump, so we can use this
  1194. // To make a tail recursive call on x86-64. The MacroAssembler
  1195. // really shouldn't wrap this as a Jump, since it can't be linked. :-/
  1196. AssemblerLabel jmp_r(RegisterID dst)
  1197. {
  1198. m_formatter.oneByteOp(OP_GROUP5_Ev, GROUP5_OP_JMPN, dst);
  1199. return m_formatter.label();
  1200. }
  1201. void jmp_m(int offset, RegisterID base)
  1202. {
  1203. m_formatter.oneByteOp(OP_GROUP5_Ev, GROUP5_OP_JMPN, base, offset);
  1204. }
  1205. #if !CPU(X86_64)
  1206. void jmp_m(const void* address)
  1207. {
  1208. m_formatter.oneByteOp(OP_GROUP5_Ev, GROUP5_OP_JMPN, address);
  1209. }
  1210. #endif
  1211. AssemblerLabel jne()
  1212. {
  1213. m_formatter.twoByteOp(jccRel32(ConditionNE));
  1214. return m_formatter.immediateRel32();
  1215. }
  1216. AssemblerLabel jnz()
  1217. {
  1218. return jne();
  1219. }
  1220. AssemblerLabel je()
  1221. {
  1222. m_formatter.twoByteOp(jccRel32(ConditionE));
  1223. return m_formatter.immediateRel32();
  1224. }
  1225. AssemblerLabel jz()
  1226. {
  1227. return je();
  1228. }
  1229. AssemblerLabel jl()
  1230. {
  1231. m_formatter.twoByteOp(jccRel32(ConditionL));
  1232. return m_formatter.immediateRel32();
  1233. }
  1234. AssemblerLabel jb()
  1235. {
  1236. m_formatter.twoByteOp(jccRel32(ConditionB));
  1237. return m_formatter.immediateRel32();
  1238. }
  1239. AssemblerLabel jle()
  1240. {
  1241. m_formatter.twoByteOp(jccRel32(ConditionLE));
  1242. return m_formatter.immediateRel32();
  1243. }
  1244. AssemblerLabel jbe()
  1245. {
  1246. m_formatter.twoByteOp(jccRel32(ConditionBE));
  1247. return m_formatter.immediateRel32();
  1248. }
  1249. AssemblerLabel jge()
  1250. {
  1251. m_formatter.twoByteOp(jccRel32(ConditionGE));
  1252. return m_formatter.immediateRel32();
  1253. }
  1254. AssemblerLabel jg()
  1255. {
  1256. m_formatter.twoByteOp(jccRel32(ConditionG));
  1257. return m_formatter.immediateRel32();
  1258. }
  1259. AssemblerLabel ja()
  1260. {
  1261. m_formatter.twoByteOp(jccRel32(ConditionA));
  1262. return m_formatter.immediateRel32();
  1263. }
  1264. AssemblerLabel jae()
  1265. {
  1266. m_formatter.twoByteOp(jccRel32(ConditionAE));
  1267. return m_formatter.immediateRel32();
  1268. }
  1269. AssemblerLabel jo()
  1270. {
  1271. m_formatter.twoByteOp(jccRel32(ConditionO));
  1272. return m_formatter.immediateRel32();
  1273. }
  1274. AssemblerLabel jnp()
  1275. {
  1276. m_formatter.twoByteOp(jccRel32(ConditionNP));
  1277. return m_formatter.immediateRel32();
  1278. }
  1279. AssemblerLabel jp()
  1280. {
  1281. m_formatter.twoByteOp(jccRel32(ConditionP));
  1282. return m_formatter.immediateRel32();
  1283. }
  1284. AssemblerLabel js()
  1285. {
  1286. m_formatter.twoByteOp(jccRel32(ConditionS));
  1287. return m_formatter.immediateRel32();
  1288. }
  1289. AssemblerLabel jCC(Condition cond)
  1290. {
  1291. m_formatter.twoByteOp(jccRel32(cond));
  1292. return m_formatter.immediateRel32();
  1293. }
  1294. // SSE operations:
  1295. void addsd_rr(XMMRegisterID src, XMMRegisterID dst)
  1296. {
  1297. m_formatter.prefix(PRE_SSE_F2);
  1298. m_formatter.twoByteOp(OP2_ADDSD_VsdWsd, (RegisterID)dst, (RegisterID)src);
  1299. }
  1300. void addsd_mr(int offset, RegisterID base, XMMRegisterID dst)
  1301. {
  1302. m_formatter.prefix(PRE_SSE_F2);
  1303. m_formatter.twoByteOp(OP2_ADDSD_VsdWsd, (RegisterID)dst, base, offset);
  1304. }
  1305. #if !CPU(X86_64)
  1306. void addsd_mr(const void* address, XMMRegisterID dst)
  1307. {
  1308. m_formatter.prefix(PRE_SSE_F2);
  1309. m_formatter.twoByteOp(OP2_ADDSD_VsdWsd, (RegisterID)dst, address);
  1310. }
  1311. #endif
  1312. void cvtsi2sd_rr(RegisterID src, XMMRegisterID dst)
  1313. {
  1314. m_formatter.prefix(PRE_SSE_F2);
  1315. m_formatter.twoByteOp(OP2_CVTSI2SD_VsdEd, (RegisterID)dst, src);
  1316. }
  1317. void cvtsi2sd_mr(int offset, RegisterID base, XMMRegisterID dst)
  1318. {
  1319. m_formatter.prefix(PRE_SSE_F2);
  1320. m_formatter.twoByteOp(OP2_CVTSI2SD_VsdEd, (RegisterID)dst, base, offset);
  1321. }
  1322. #if !CPU(X86_64)
  1323. void cvtsi2sd_mr(const void* address, XMMRegisterID dst)
  1324. {
  1325. m_formatter.prefix(PRE_SSE_F2);
  1326. m_formatter.twoByteOp(OP2_CVTSI2SD_VsdEd, (RegisterID)dst, address);
  1327. }
  1328. #endif
  1329. void cvttsd2si_rr(XMMRegisterID src, RegisterID dst)
  1330. {
  1331. m_formatter.prefix(PRE_SSE_F2);
  1332. m_formatter.twoByteOp(OP2_CVTTSD2SI_GdWsd, dst, (RegisterID)src);
  1333. }
  1334. void cvtsd2ss_rr(XMMRegisterID src, XMMRegisterID dst)
  1335. {
  1336. m_formatter.prefix(PRE_SSE_F2);
  1337. m_formatter.twoByteOp(OP2_CVTSD2SS_VsdWsd, dst, (RegisterID)src);
  1338. }
  1339. void cvtss2sd_rr(XMMRegisterID src, XMMRegisterID dst)
  1340. {
  1341. m_formatter.prefix(PRE_SSE_F3);
  1342. m_formatter.twoByteOp(OP2_CVTSS2SD_VsdWsd, dst, (RegisterID)src);
  1343. }
  1344. #if CPU(X86_64)
  1345. void cvttsd2siq_rr(XMMRegisterID src, RegisterID dst)
  1346. {
  1347. m_formatter.prefix(PRE_SSE_F2);
  1348. m_formatter.twoByteOp64(OP2_CVTTSD2SI_GdWsd, dst, (RegisterID)src);
  1349. }
  1350. #endif
  1351. void movd_rr(XMMRegisterID src, RegisterID dst)
  1352. {
  1353. m_formatter.prefix(PRE_SSE_66);
  1354. m_formatter.twoByteOp(OP2_MOVD_EdVd, (RegisterID)src, dst);
  1355. }
  1356. void movd_rr(RegisterID src, XMMRegisterID dst)
  1357. {
  1358. m_formatter.prefix(PRE_SSE_66);
  1359. m_formatter.twoByteOp(OP2_MOVD_VdEd, (RegisterID)dst, src);
  1360. }
  1361. #if CPU(X86_64)
  1362. void movq_rr(XMMRegisterID src, RegisterID dst)
  1363. {
  1364. m_formatter.prefix(PRE_SSE_66);
  1365. m_formatter.twoByteOp64(OP2_MOVD_EdVd, (RegisterID)src, dst);
  1366. }
  1367. void movq_rr(RegisterID src, XMMRegisterID dst)
  1368. {
  1369. m_formatter.prefix(PRE_SSE_66);
  1370. m_formatter.twoByteOp64(OP2_MOVD_VdEd, (RegisterID)dst, src);
  1371. }
  1372. #endif
  1373. void movsd_rr(XMMRegisterID src, XMMRegisterID dst)
  1374. {
  1375. m_formatter.prefix(PRE_SSE_F2);
  1376. m_formatter.twoByteOp(OP2_MOVSD_VsdWsd, (RegisterID)dst, (RegisterID)src);
  1377. }
  1378. void movsd_rm(XMMRegisterID src, int offset, RegisterID base)
  1379. {
  1380. m_formatter.prefix(PRE_SSE_F2);
  1381. m_formatter.twoByteOp(OP2_MOVSD_WsdVsd, (RegisterID)src, base, offset);
  1382. }
  1383. void movsd_rm(XMMRegisterID src, int offset, RegisterID base, RegisterID index, int scale)
  1384. {
  1385. m_formatter.prefix(PRE_SSE_F2);
  1386. m_formatter.twoByteOp(OP2_MOVSD_WsdVsd, (RegisterID)src, base, index, scale, offset);
  1387. }
  1388. void movss_rm(XMMRegisterID src, int offset, RegisterID base, RegisterID index, int scale)
  1389. {
  1390. m_formatter.prefix(PRE_SSE_F3);
  1391. m_formatter.twoByteOp(OP2_MOVSD_WsdVsd, (RegisterID)src, base, index, scale, offset);
  1392. }
  1393. void movsd_mr(int offset, RegisterID base, XMMRegisterID dst)
  1394. {
  1395. m_formatter.prefix(PRE_SSE_F2);
  1396. m_formatter.twoByteOp(OP2_MOVSD_VsdWsd, (RegisterID)dst, base, offset);
  1397. }
  1398. void movsd_mr(int offset, RegisterID base, RegisterID index, int scale, XMMRegisterID dst)
  1399. {
  1400. m_formatter.prefix(PRE_SSE_F2);
  1401. m_formatter.twoByteOp(OP2_MOVSD_VsdWsd, dst, base, index, scale, offset);
  1402. }
  1403. void movss_mr(int offset, RegisterID base, RegisterID index, int scale, XMMRegisterID dst)
  1404. {
  1405. m_formatter.prefix(PRE_SSE_F3);
  1406. m_formatter.twoByteOp(OP2_MOVSD_VsdWsd, dst, base, index, scale, offset);
  1407. }
  1408. #if !CPU(X86_64)
  1409. void movsd_mr(const void* address, XMMRegisterID dst)
  1410. {
  1411. m_formatter.prefix(PRE_SSE_F2);
  1412. m_formatter.twoByteOp(OP2_MOVSD_VsdWsd, (RegisterID)dst, address);
  1413. }
  1414. void movsd_rm(XMMRegisterID src, const void* address)
  1415. {
  1416. m_formatter.prefix(PRE_SSE_F2);
  1417. m_formatter.twoByteOp(OP2_MOVSD_WsdVsd, (RegisterID)src, address);
  1418. }
  1419. #endif
  1420. void mulsd_rr(XMMRegisterID src, XMMRegisterID dst)
  1421. {
  1422. m_formatter.prefix(PRE_SSE_F2);
  1423. m_formatter.twoByteOp(OP2_MULSD_VsdWsd, (RegisterID)dst, (RegisterID)src);
  1424. }
  1425. void mulsd_mr(int offset, RegisterID base, XMMRegisterID dst)
  1426. {
  1427. m_formatter.prefix(PRE_SSE_F2);
  1428. m_formatter.twoByteOp(OP2_MULSD_VsdWsd, (RegisterID)dst, base, offset);
  1429. }
  1430. void pextrw_irr(int whichWord, XMMRegisterID src, RegisterID dst)
  1431. {
  1432. m_formatter.prefix(PRE_SSE_66);
  1433. m_formatter.twoByteOp(OP2_PEXTRW_GdUdIb, (RegisterID)dst, (RegisterID)src);
  1434. m_formatter.immediate8(whichWord);
  1435. }
  1436. void psllq_i8r(int imm, XMMRegisterID dst)
  1437. {
  1438. m_formatter.prefix(PRE_SSE_66);
  1439. m_formatter.twoByteOp8(OP2_PSLLQ_UdqIb, GROUP14_OP_PSLLQ, (RegisterID)dst);
  1440. m_formatter.immediate8(imm);
  1441. }
  1442. void psrlq_i8r(int imm, XMMRegisterID dst)
  1443. {
  1444. m_formatter.prefix(PRE_SSE_66);
  1445. m_formatter.twoByteOp8(OP2_PSRLQ_UdqIb, GROUP14_OP_PSRLQ, (RegisterID)dst);
  1446. m_formatter.immediate8(imm);
  1447. }
  1448. void por_rr(XMMRegisterID src, XMMRegisterID dst)
  1449. {
  1450. m_formatter.prefix(PRE_SSE_66);
  1451. m_formatter.twoByteOp(OP2_POR_VdqWdq, (RegisterID)dst, (RegisterID)src);
  1452. }
  1453. void subsd_rr(XMMRegisterID src, XMMRegisterID dst)
  1454. {
  1455. m_formatter.prefix(PRE_SSE_F2);
  1456. m_formatter.twoByteOp(OP2_SUBSD_VsdWsd, (RegisterID)dst, (RegisterID)src);
  1457. }
  1458. void subsd_mr(int offset, RegisterID base, XMMRegisterID dst)
  1459. {
  1460. m_formatter.prefix(PRE_SSE_F2);
  1461. m_formatter.twoByteOp(OP2_SUBSD_VsdWsd, (RegisterID)dst, base, offset);
  1462. }
  1463. void ucomisd_rr(XMMRegisterID src, XMMRegisterID dst)
  1464. {
  1465. m_formatter.prefix(PRE_SSE_66);
  1466. m_formatter.twoByteOp(OP2_UCOMISD_VsdWsd, (RegisterID)dst, (RegisterID)src);
  1467. }
  1468. void ucomisd_mr(int offset, RegisterID base, XMMRegisterID dst)
  1469. {
  1470. m_formatter.prefix(PRE_SSE_66);
  1471. m_formatter.twoByteOp(OP2_UCOMISD_VsdWsd, (RegisterID)dst, base, offset);
  1472. }
  1473. void divsd_rr(XMMRegisterID src, XMMRegisterID dst)
  1474. {
  1475. m_formatter.prefix(PRE_SSE_F2);
  1476. m_formatter.twoByteOp(OP2_DIVSD_VsdWsd, (RegisterID)dst, (RegisterID)src);
  1477. }
  1478. void divsd_mr(int offset, RegisterID base, XMMRegisterID dst)
  1479. {
  1480. m_formatter.prefix(PRE_SSE_F2);
  1481. m_formatter.twoByteOp(OP2_DIVSD_VsdWsd, (RegisterID)dst, base, offset);
  1482. }
  1483. void xorpd_rr(XMMRegisterID src, XMMRegisterID dst)
  1484. {
  1485. m_formatter.prefix(PRE_SSE_66);
  1486. m_formatter.twoByteOp(OP2_XORPD_VpdWpd, (RegisterID)dst, (RegisterID)src);
  1487. }
  1488. void andnpd_rr(XMMRegisterID src, XMMRegisterID dst)
  1489. {
  1490. m_formatter.prefix(PRE_SSE_66);
  1491. m_formatter.twoByteOp(OP2_ANDNPD_VpdWpd, (RegisterID)dst, (RegisterID)src);
  1492. }
  1493. void sqrtsd_rr(XMMRegisterID src, XMMRegisterID dst)
  1494. {
  1495. m_formatter.prefix(PRE_SSE_F2);
  1496. m_formatter.twoByteOp(OP2_SQRTSD_VsdWsd, (RegisterID)dst, (RegisterID)src);
  1497. }
  1498. // Misc instructions:
  1499. void hlt()
  1500. {
  1501. m_formatter.oneByteOp(OP_HLT);
  1502. }
  1503. #if OS(ORBIS)
  1504. void int_imm(unsigned char imm)
  1505. {
  1506. m_formatter.oneByteOp(OP_INT, imm);
  1507. }
  1508. #endif
  1509. void int3()
  1510. {
  1511. m_formatter.oneByteOp(OP_INT3);
  1512. }
  1513. void ret()
  1514. {
  1515. m_formatter.oneByteOp(OP_RET);
  1516. }
  1517. void predictNotTaken()
  1518. {
  1519. m_formatter.prefix(PRE_PREDICT_BRANCH_NOT_TAKEN);
  1520. }
  1521. // Assembler admin methods:
  1522. size_t codeSize() const
  1523. {
  1524. return m_formatter.codeSize();
  1525. }
  1526. AssemblerLabel labelForWatchpoint()
  1527. {
  1528. AssemblerLabel result = m_formatter.label();
  1529. if (static_cast<int>(result.m_offset) != m_indexOfLastWatchpoint)
  1530. result = label();
  1531. m_indexOfLastWatchpoint = result.m_offset;
  1532. m_indexOfTailOfLastWatchpoint = result.m_offset + maxJumpReplacementSize();
  1533. return result;
  1534. }
  1535. AssemblerLabel labelIgnoringWatchpoints()
  1536. {
  1537. return m_formatter.label();
  1538. }
  1539. AssemblerLabel label()
  1540. {
  1541. AssemblerLabel result = m_formatter.label();
  1542. while (UNLIKELY(static_cast<int>(result.m_offset) < m_indexOfTailOfLastWatchpoint)) {
  1543. nop();
  1544. result = m_formatter.label();
  1545. }
  1546. return result;
  1547. }
  1548. AssemblerLabel align(int alignment)
  1549. {
  1550. while (!m_formatter.isAligned(alignment))
  1551. m_formatter.oneByteOp(OP_HLT);
  1552. return label();
  1553. }
  1554. // Linking & patching:
  1555. //
  1556. // 'link' and 'patch' methods are for use on unprotected code - such as the code
  1557. // within the AssemblerBuffer, and code being patched by the patch buffer. Once
  1558. // code has been finalized it is (platform support permitting) within a non-
  1559. // writable region of memory; to modify the code in an execute-only execuable
  1560. // pool the 'repatch' and 'relink' methods should be used.
  1561. void linkJump(AssemblerLabel from, AssemblerLabel to)
  1562. {
  1563. ASSERT(from.isSet());
  1564. ASSERT(to.isSet());
  1565. char* code = reinterpret_cast<char*>(m_formatter.data());
  1566. ASSERT(!reinterpret_cast<int32_t*>(code + from.m_offset)[-1]);
  1567. setRel32(code + from.m_offset, code + to.m_offset);
  1568. }
  1569. static void linkJump(void* code, AssemblerLabel from, void* to)
  1570. {
  1571. ASSERT(from.isSet());
  1572. setRel32(reinterpret_cast<char*>(code) + from.m_offset, to);
  1573. }
  1574. static void linkCall(void* code, AssemblerLabel from, void* to)
  1575. {
  1576. ASSERT(from.isSet());
  1577. setRel32(reinterpret_cast<char*>(code) + from.m_offset, to);
  1578. }
  1579. static void linkPointer(void* code, AssemblerLabel where, void* value)
  1580. {
  1581. ASSERT(where.isSet());
  1582. setPointer(reinterpret_cast<char*>(code) + where.m_offset, value);
  1583. }
  1584. static void relinkJump(void* from, void* to)
  1585. {
  1586. setRel32(from, to);
  1587. }
  1588. static void relinkCall(void* from, void* to)
  1589. {
  1590. setRel32(from, to);
  1591. }
  1592. static void repatchCompact(void* where, int32_t value)
  1593. {
  1594. ASSERT(value >= std::numeric_limits<int8_t>::min());
  1595. ASSERT(value <= std::numeric_limits<int8_t>::max());
  1596. setInt8(where, value);
  1597. }
  1598. static void repatchInt32(void* where, int32_t value)
  1599. {
  1600. setInt32(where, value);
  1601. }
  1602. static void repatchPointer(void* where, void* value)
  1603. {
  1604. setPointer(where, value);
  1605. }
  1606. static void* readPointer(void* where)
  1607. {
  1608. return reinterpret_cast<void**>(where)[-1];
  1609. }
  1610. static void replaceWithJump(void* instructionStart, void* to)
  1611. {
  1612. uint8_t* ptr = reinterpret_cast<uint8_t*>(instructionStart);
  1613. uint8_t* dstPtr = reinterpret_cast<uint8_t*>(to);
  1614. intptr_t distance = (intptr_t)(dstPtr - (ptr + 5));
  1615. ptr[0] = static_cast<uint8_t>(OP_JMP_rel32);
  1616. *reinterpret_cast<int32_t*>(ptr + 1) = static_cast<int32_t>(distance);
  1617. }
  1618. static ptrdiff_t maxJumpReplacementSize()
  1619. {
  1620. return 5;
  1621. }
  1622. #if CPU(X86_64)
  1623. static void revertJumpTo_movq_i64r(void* instructionStart, int64_t imm, RegisterID dst)
  1624. {
  1625. const int rexBytes = 1;
  1626. const int opcodeBytes = 1;
  1627. ASSERT(rexBytes + opcodeBytes <= maxJumpReplacementSize());
  1628. uint8_t* ptr = reinterpret_cast<uint8_t*>(instructionStart);
  1629. ptr[0] = PRE_REX | (1 << 3) | (dst >> 3);
  1630. ptr[1] = OP_MOV_EAXIv | (dst & 7);
  1631. union {
  1632. uint64_t asWord;
  1633. uint8_t asBytes[8];
  1634. } u;
  1635. u.asWord = imm;
  1636. for (unsigned i = rexBytes + opcodeBytes; i < static_cast<unsigned>(maxJumpReplacementSize()); ++i)
  1637. ptr[i] = u.asBytes[i - rexBytes - opcodeBytes];
  1638. }
  1639. #endif
  1640. static void revertJumpTo_cmpl_ir_force32(void* instructionStart, int32_t imm, RegisterID dst)
  1641. {
  1642. const int opcodeBytes = 1;
  1643. const int modRMBytes = 1;
  1644. ASSERT(opcodeBytes + modRMBytes <= maxJumpReplacementSize());
  1645. uint8_t* ptr = reinterpret_cast<uint8_t*>(instructionStart);
  1646. ptr[0] = OP_GROUP1_EvIz;
  1647. ptr[1] = (X86InstructionFormatter::ModRmRegister << 6) | (GROUP1_OP_CMP << 3) | dst;
  1648. union {
  1649. uint32_t asWord;
  1650. uint8_t asBytes[4];
  1651. } u;
  1652. u.asWord = imm;
  1653. for (unsigned i = opcodeBytes + modRMBytes; i < static_cast<unsigned>(maxJumpReplacementSize()); ++i)
  1654. ptr[i] = u.asBytes[i - opcodeBytes - modRMBytes];
  1655. }
  1656. static void revertJumpTo_cmpl_im_force32(void* instructionStart, int32_t imm, int offset, RegisterID dst)
  1657. {
  1658. ASSERT_UNUSED(offset, !offset);
  1659. const int opcodeBytes = 1;
  1660. const int modRMBytes = 1;
  1661. ASSERT(opcodeBytes + modRMBytes <= maxJumpReplacementSize());
  1662. uint8_t* ptr = reinterpret_cast<uint8_t*>(instructionStart);
  1663. ptr[0] = OP_GROUP1_EvIz;
  1664. ptr[1] = (X86InstructionFormatter::ModRmMemoryNoDisp << 6) | (GROUP1_OP_CMP << 3) | dst;
  1665. union {
  1666. uint32_t asWord;
  1667. uint8_t asBytes[4];
  1668. } u;
  1669. u.asWord = imm;
  1670. for (unsigned i = opcodeBytes + modRMBytes; i < static_cast<unsigned>(maxJumpReplacementSize()); ++i)
  1671. ptr[i] = u.asBytes[i - opcodeBytes - modRMBytes];
  1672. }
  1673. static void replaceWithLoad(void* instructionStart)
  1674. {
  1675. uint8_t* ptr = reinterpret_cast<uint8_t*>(instructionStart);
  1676. #if CPU(X86_64)
  1677. if ((*ptr & ~15) == PRE_REX)
  1678. ptr++;
  1679. #endif
  1680. switch (*ptr) {
  1681. case OP_MOV_GvEv:
  1682. break;
  1683. case OP_LEA:
  1684. *ptr = OP_MOV_GvEv;
  1685. break;
  1686. default:
  1687. RELEASE_ASSERT_NOT_REACHED();
  1688. }
  1689. }
  1690. static void replaceWithAddressComputation(void* instructionStart)
  1691. {
  1692. uint8_t* ptr = reinterpret_cast<uint8_t*>(instructionStart);
  1693. #if CPU(X86_64)
  1694. if ((*ptr & ~15) == PRE_REX)
  1695. ptr++;
  1696. #endif
  1697. switch (*ptr) {
  1698. case OP_MOV_GvEv:
  1699. *ptr = OP_LEA;
  1700. break;
  1701. case OP_LEA:
  1702. break;
  1703. default:
  1704. RELEASE_ASSERT_NOT_REACHED();
  1705. }
  1706. }
  1707. static unsigned getCallReturnOffset(AssemblerLabel call)
  1708. {
  1709. ASSERT(call.isSet());
  1710. return call.m_offset;
  1711. }
  1712. static void* getRelocatedAddress(void* code, AssemblerLabel label)
  1713. {
  1714. ASSERT(label.isSet());
  1715. return reinterpret_cast<void*>(reinterpret_cast<ptrdiff_t>(code) + label.m_offset);
  1716. }
  1717. static int getDifferenceBetweenLabels(AssemblerLabel a, AssemblerLabel b)
  1718. {
  1719. return b.m_offset - a.m_offset;
  1720. }
  1721. PassRefPtr<ExecutableMemoryHandle> executableCopy(VM& vm, void* ownerUID, JITCompilationEffort effort)
  1722. {
  1723. return m_formatter.executableCopy(vm, ownerUID, effort);
  1724. }
  1725. unsigned debugOffset() { return m_formatter.debugOffset(); }
  1726. void nop()
  1727. {
  1728. m_formatter.oneByteOp(OP_NOP);
  1729. }
  1730. // This is a no-op on x86
  1731. ALWAYS_INLINE static void cacheFlush(void*, size_t) { }
  1732. private:
  1733. static void setPointer(void* where, void* value)
  1734. {
  1735. reinterpret_cast<void**>(where)[-1] = value;
  1736. }
  1737. static void setInt32(void* where, int32_t value)
  1738. {
  1739. reinterpret_cast<int32_t*>(where)[-1] = value;
  1740. }
  1741. static void setInt8(void* where, int8_t value)
  1742. {
  1743. reinterpret_cast<int8_t*>(where)[-1] = value;
  1744. }
  1745. static void setRel32(void* from, void* to)
  1746. {
  1747. intptr_t offset = reinterpret_cast<intptr_t>(to) - reinterpret_cast<intptr_t>(from);
  1748. ASSERT(offset == static_cast<int32_t>(offset));
  1749. setInt32(from, offset);
  1750. }
  1751. class X86InstructionFormatter {
  1752. static const int maxInstructionSize = 16;
  1753. public:
  1754. enum ModRmMode {
  1755. ModRmMemoryNoDisp,
  1756. ModRmMemoryDisp8,
  1757. ModRmMemoryDisp32,
  1758. ModRmRegister,
  1759. };
  1760. // Legacy prefix bytes:
  1761. //
  1762. // These are emmitted prior to the instruction.
  1763. void prefix(OneByteOpcodeID pre)
  1764. {
  1765. m_buffer.putByte(pre);
  1766. }
  1767. // Word-sized operands / no operand instruction formatters.
  1768. //
  1769. // In addition to the opcode, the following operand permutations are supported:
  1770. // * None - instruction takes no operands.
  1771. // * One register - the low three bits of the RegisterID are added into the opcode.
  1772. // * Two registers - encode a register form ModRm (for all ModRm formats, the reg field is passed first, and a GroupOpcodeID may be passed in its place).
  1773. // * Three argument ModRM - a register, and a register and an offset describing a memory operand.
  1774. // * Five argument ModRM - a register, and a base register, an index, scale, and offset describing a memory operand.
  1775. //
  1776. // For 32-bit x86 targets, the address operand may also be provided as a void*.
  1777. // On 64-bit targets REX prefixes will be planted as necessary, where high numbered registers are used.
  1778. //
  1779. // The twoByteOp methods plant two-byte Intel instructions sequences (first opcode byte 0x0F).
  1780. void oneByteOp(OneByteOpcodeID opcode)
  1781. {
  1782. m_buffer.ensureSpace(maxInstructionSize);
  1783. m_buffer.putByteUnchecked(opcode);
  1784. }
  1785. void oneByteOp(OneByteOpcodeID opcode, int8_t imm)
  1786. {
  1787. m_buffer.ensureSpace(maxInstructionSize);
  1788. m_buffer.putByteUnchecked(opcode);
  1789. m_buffer.putByteUnchecked(imm);
  1790. }
  1791. void oneByteOp(OneByteOpcodeID opcode, RegisterID reg)
  1792. {
  1793. m_buffer.ensureSpace(maxInstructionSize);
  1794. emitRexIfNeeded(0, 0, reg);
  1795. m_buffer.putByteUnchecked(opcode + (reg & 7));
  1796. }
  1797. void oneByteOp(OneByteOpcodeID opcode, int reg, RegisterID rm)
  1798. {
  1799. m_buffer.ensureSpace(maxInstructionSize);
  1800. emitRexIfNeeded(reg, 0, rm);
  1801. m_buffer.putByteUnchecked(opcode);
  1802. registerModRM(reg, rm);
  1803. }
  1804. void oneByteOp(OneByteOpcodeID opcode, int reg, RegisterID base, int offset)
  1805. {
  1806. m_buffer.ensureSpace(maxInstructionSize);
  1807. emitRexIfNeeded(reg, 0, base);
  1808. m_buffer.putByteUnchecked(opcode);
  1809. memoryModRM(reg, base, offset);
  1810. }
  1811. void oneByteOp_disp32(OneByteOpcodeID opcode, int reg, RegisterID base, int offset)
  1812. {
  1813. m_buffer.ensureSpace(maxInstructionSize);
  1814. emitRexIfNeeded(reg, 0, base);
  1815. m_buffer.putByteUnchecked(opcode);
  1816. memoryModRM_disp32(reg, base, offset);
  1817. }
  1818. void oneByteOp_disp8(OneByteOpcodeID opcode, int reg, RegisterID base, int offset)
  1819. {
  1820. m_buffer.ensureSpace(maxInstructionSize);
  1821. emitRexIfNeeded(reg, 0, base);
  1822. m_buffer.putByteUnchecked(opcode);
  1823. memoryModRM_disp8(reg, base, offset);
  1824. }
  1825. void oneByteOp(OneByteOpcodeID opcode, int reg, RegisterID base, RegisterID index, int scale, int offset)
  1826. {
  1827. m_buffer.ensureSpace(maxInstructionSize);
  1828. emitRexIfNeeded(reg, index, base);
  1829. m_buffer.putByteUnchecked(opcode);
  1830. memoryModRM(reg, base, index, scale, offset);
  1831. }
  1832. #if !CPU(X86_64)
  1833. void oneByteOp(OneByteOpcodeID opcode, int reg, const void* address)
  1834. {
  1835. m_buffer.ensureSpace(maxInstructionSize);
  1836. m_buffer.putByteUnchecked(opcode);
  1837. memoryModRM(reg, address);
  1838. }
  1839. #endif
  1840. void twoByteOp(TwoByteOpcodeID opcode)
  1841. {
  1842. m_buffer.ensureSpace(maxInstructionSize);
  1843. m_buffer.putByteUnchecked(OP_2BYTE_ESCAPE);
  1844. m_buffer.putByteUnchecked(opcode);
  1845. }
  1846. void twoByteOp(TwoByteOpcodeID opcode, int reg, RegisterID rm)
  1847. {
  1848. m_buffer.ensureSpace(maxInstructionSize);
  1849. emitRexIfNeeded(reg, 0, rm);
  1850. m_buffer.putByteUnchecked(OP_2BYTE_ESCAPE);
  1851. m_buffer.putByteUnchecked(opcode);
  1852. registerModRM(reg, rm);
  1853. }
  1854. void twoByteOp(TwoByteOpcodeID opcode, int reg, RegisterID base, int offset)
  1855. {
  1856. m_buffer.ensureSpace(maxInstructionSize);
  1857. emitRexIfNeeded(reg, 0, base);
  1858. m_buffer.putByteUnchecked(OP_2BYTE_ESCAPE);
  1859. m_buffer.putByteUnchecked(opcode);
  1860. memoryModRM(reg, base, offset);
  1861. }
  1862. void twoByteOp(TwoByteOpcodeID opcode, int reg, RegisterID base, RegisterID index, int scale, int offset)
  1863. {
  1864. m_buffer.ensureSpace(maxInstructionSize);
  1865. emitRexIfNeeded(reg, index, base);
  1866. m_buffer.putByteUnchecked(OP_2BYTE_ESCAPE);
  1867. m_buffer.putByteUnchecked(opcode);
  1868. memoryModRM(reg, base, index, scale, offset);
  1869. }
  1870. #if !CPU(X86_64)
  1871. void twoByteOp(TwoByteOpcodeID opcode, int reg, const void* address)
  1872. {
  1873. m_buffer.ensureSpace(maxInstructionSize);
  1874. m_buffer.putByteUnchecked(OP_2BYTE_ESCAPE);
  1875. m_buffer.putByteUnchecked(opcode);
  1876. memoryModRM(reg, address);
  1877. }
  1878. #endif
  1879. #if CPU(X86_64)
  1880. // Quad-word-sized operands:
  1881. //
  1882. // Used to format 64-bit operantions, planting a REX.w prefix.
  1883. // When planting d64 or f64 instructions, not requiring a REX.w prefix,
  1884. // the normal (non-'64'-postfixed) formatters should be used.
  1885. void oneByteOp64(OneByteOpcodeID opcode)
  1886. {
  1887. m_buffer.ensureSpace(maxInstructionSize);
  1888. emitRexW(0, 0, 0);
  1889. m_buffer.putByteUnchecked(opcode);
  1890. }
  1891. void oneByteOp64(OneByteOpcodeID opcode, RegisterID reg)
  1892. {
  1893. m_buffer.ensureSpace(maxInstructionSize);
  1894. emitRexW(0, 0, reg);
  1895. m_buffer.putByteUnchecked(opcode + (reg & 7));
  1896. }
  1897. void oneByteOp64(OneByteOpcodeID opcode, int reg, RegisterID rm)
  1898. {
  1899. m_buffer.ensureSpace(maxInstructionSize);
  1900. emitRexW(reg, 0, rm);
  1901. m_buffer.putByteUnchecked(opcode);
  1902. registerModRM(reg, rm);
  1903. }
  1904. void oneByteOp64(OneByteOpcodeID opcode, int reg, RegisterID base, int offset)
  1905. {
  1906. m_buffer.ensureSpace(maxInstructionSize);
  1907. emitRexW(reg, 0, base);
  1908. m_buffer.putByteUnchecked(opcode);
  1909. memoryModRM(reg, base, offset);
  1910. }
  1911. void oneByteOp64_disp32(OneByteOpcodeID opcode, int reg, RegisterID base, int offset)
  1912. {
  1913. m_buffer.ensureSpace(maxInstructionSize);
  1914. emitRexW(reg, 0, base);
  1915. m_buffer.putByteUnchecked(opcode);
  1916. memoryModRM_disp32(reg, base, offset);
  1917. }
  1918. void oneByteOp64_disp8(OneByteOpcodeID opcode, int reg, RegisterID base, int offset)
  1919. {
  1920. m_buffer.ensureSpace(maxInstructionSize);
  1921. emitRexW(reg, 0, base);
  1922. m_buffer.putByteUnchecked(opcode);
  1923. memoryModRM_disp8(reg, base, offset);
  1924. }
  1925. void oneByteOp64(OneByteOpcodeID opcode, int reg, RegisterID base, RegisterID index, int scale, int offset)
  1926. {
  1927. m_buffer.ensureSpace(maxInstructionSize);
  1928. emitRexW(reg, index, base);
  1929. m_buffer.putByteUnchecked(opcode);
  1930. memoryModRM(reg, base, index, scale, offset);
  1931. }
  1932. void twoByteOp64(TwoByteOpcodeID opcode, int reg, RegisterID rm)
  1933. {
  1934. m_buffer.ensureSpace(maxInstructionSize);
  1935. emitRexW(reg, 0, rm);
  1936. m_buffer.putByteUnchecked(OP_2BYTE_ESCAPE);
  1937. m_buffer.putByteUnchecked(opcode);
  1938. registerModRM(reg, rm);
  1939. }
  1940. #endif
  1941. // Byte-operands:
  1942. //
  1943. // These methods format byte operations. Byte operations differ from the normal
  1944. // formatters in the circumstances under which they will decide to emit REX prefixes.
  1945. // These should be used where any register operand signifies a byte register.
  1946. //
  1947. // The disctinction is due to the handling of register numbers in the range 4..7 on
  1948. // x86-64. These register numbers may either represent the second byte of the first
  1949. // four registers (ah..bh) or the first byte of the second four registers (spl..dil).
  1950. //
  1951. // Since ah..bh cannot be used in all permutations of operands (specifically cannot
  1952. // be accessed where a REX prefix is present), these are likely best treated as
  1953. // deprecated. In order to ensure the correct registers spl..dil are selected a
  1954. // REX prefix will be emitted for any byte register operand in the range 4..15.
  1955. //
  1956. // These formatters may be used in instructions where a mix of operand sizes, in which
  1957. // case an unnecessary REX will be emitted, for example:
  1958. // movzbl %al, %edi
  1959. // In this case a REX will be planted since edi is 7 (and were this a byte operand
  1960. // a REX would be required to specify dil instead of bh). Unneeded REX prefixes will
  1961. // be silently ignored by the processor.
  1962. //
  1963. // Address operands should still be checked using regRequiresRex(), while byteRegRequiresRex()
  1964. // is provided to check byte register operands.
  1965. void oneByteOp8(OneByteOpcodeID opcode, GroupOpcodeID groupOp, RegisterID rm)
  1966. {
  1967. m_buffer.ensureSpace(maxInstructionSize);
  1968. emitRexIf(byteRegRequiresRex(rm), 0, 0, rm);
  1969. m_buffer.putByteUnchecked(opcode);
  1970. registerModRM(groupOp, rm);
  1971. }
  1972. void oneByteOp8(OneByteOpcodeID opcode, int reg, RegisterID rm)
  1973. {
  1974. m_buffer.ensureSpace(maxInstructionSize);
  1975. emitRexIf(byteRegRequiresRex(reg) || byteRegRequiresRex(rm), reg, 0, rm);
  1976. m_buffer.putByteUnchecked(opcode);
  1977. registerModRM(reg, rm);
  1978. }
  1979. void oneByteOp8(OneByteOpcodeID opcode, int reg, RegisterID base, RegisterID index, int scale, int offset)
  1980. {
  1981. m_buffer.ensureSpace(maxInstructionSize);
  1982. emitRexIf(byteRegRequiresRex(reg) || regRequiresRex(index) || regRequiresRex(base), reg, index, base);
  1983. m_buffer.putByteUnchecked(opcode);
  1984. memoryModRM(reg, base, index, scale, offset);
  1985. }
  1986. void twoByteOp8(TwoByteOpcodeID opcode, RegisterID reg, RegisterID rm)
  1987. {
  1988. m_buffer.ensureSpace(maxInstructionSize);
  1989. emitRexIf(byteRegRequiresRex(reg)|byteRegRequiresRex(rm), reg, 0, rm);
  1990. m_buffer.putByteUnchecked(OP_2BYTE_ESCAPE);
  1991. m_buffer.putByteUnchecked(opcode);
  1992. registerModRM(reg, rm);
  1993. }
  1994. void twoByteOp8(TwoByteOpcodeID opcode, GroupOpcodeID groupOp, RegisterID rm)
  1995. {
  1996. m_buffer.ensureSpace(maxInstructionSize);
  1997. emitRexIf(byteRegRequiresRex(rm), 0, 0, rm);
  1998. m_buffer.putByteUnchecked(OP_2BYTE_ESCAPE);
  1999. m_buffer.putByteUnchecked(opcode);
  2000. registerModRM(groupOp, rm);
  2001. }
  2002. // Immediates:
  2003. //
  2004. // An immedaite should be appended where appropriate after an op has been emitted.
  2005. // The writes are unchecked since the opcode formatters above will have ensured space.
  2006. void immediate8(int imm)
  2007. {
  2008. m_buffer.putByteUnchecked(imm);
  2009. }
  2010. void immediate16(int imm)
  2011. {
  2012. m_buffer.putShortUnchecked(imm);
  2013. }
  2014. void immediate32(int imm)
  2015. {
  2016. m_buffer.putIntUnchecked(imm);
  2017. }
  2018. void immediate64(int64_t imm)
  2019. {
  2020. m_buffer.putInt64Unchecked(imm);
  2021. }
  2022. AssemblerLabel immediateRel32()
  2023. {
  2024. m_buffer.putIntUnchecked(0);
  2025. return label();
  2026. }
  2027. // Administrative methods:
  2028. size_t codeSize() const { return m_buffer.codeSize(); }
  2029. AssemblerLabel label() const { return m_buffer.label(); }
  2030. bool isAligned(int alignment) const { return m_buffer.isAligned(alignment); }
  2031. void* data() const { return m_buffer.data(); }
  2032. PassRefPtr<ExecutableMemoryHandle> executableCopy(VM& vm, void* ownerUID, JITCompilationEffort effort)
  2033. {
  2034. return m_buffer.executableCopy(vm, ownerUID, effort);
  2035. }
  2036. unsigned debugOffset() { return m_buffer.debugOffset(); }
  2037. private:
  2038. // Internals; ModRm and REX formatters.
  2039. static const RegisterID noBase = X86Registers::ebp;
  2040. static const RegisterID hasSib = X86Registers::esp;
  2041. static const RegisterID noIndex = X86Registers::esp;
  2042. #if CPU(X86_64)
  2043. static const RegisterID noBase2 = X86Registers::r13;
  2044. static const RegisterID hasSib2 = X86Registers::r12;
  2045. // Registers r8 & above require a REX prefixe.
  2046. inline bool regRequiresRex(int reg)
  2047. {
  2048. return (reg >= X86Registers::r8);
  2049. }
  2050. // Byte operand register spl & above require a REX prefix (to prevent the 'H' registers be accessed).
  2051. inline bool byteRegRequiresRex(int reg)
  2052. {
  2053. return (reg >= X86Registers::esp);
  2054. }
  2055. // Format a REX prefix byte.
  2056. inline void emitRex(bool w, int r, int x, int b)
  2057. {
  2058. ASSERT(r >= 0);
  2059. ASSERT(x >= 0);
  2060. ASSERT(b >= 0);
  2061. m_buffer.putByteUnchecked(PRE_REX | ((int)w << 3) | ((r>>3)<<2) | ((x>>3)<<1) | (b>>3));
  2062. }
  2063. // Used to plant a REX byte with REX.w set (for 64-bit operations).
  2064. inline void emitRexW(int r, int x, int b)
  2065. {
  2066. emitRex(true, r, x, b);
  2067. }
  2068. // Used for operations with byte operands - use byteRegRequiresRex() to check register operands,
  2069. // regRequiresRex() to check other registers (i.e. address base & index).
  2070. inline void emitRexIf(bool condition, int r, int x, int b)
  2071. {
  2072. if (condition) emitRex(false, r, x, b);
  2073. }
  2074. // Used for word sized operations, will plant a REX prefix if necessary (if any register is r8 or above).
  2075. inline void emitRexIfNeeded(int r, int x, int b)
  2076. {
  2077. emitRexIf(regRequiresRex(r) || regRequiresRex(x) || regRequiresRex(b), r, x, b);
  2078. }
  2079. #else
  2080. // No REX prefix bytes on 32-bit x86.
  2081. inline bool regRequiresRex(int) { return false; }
  2082. inline bool byteRegRequiresRex(int) { return false; }
  2083. inline void emitRexIf(bool, int, int, int) {}
  2084. inline void emitRexIfNeeded(int, int, int) {}
  2085. #endif
  2086. void putModRm(ModRmMode mode, int reg, RegisterID rm)
  2087. {
  2088. m_buffer.putByteUnchecked((mode << 6) | ((reg & 7) << 3) | (rm & 7));
  2089. }
  2090. void putModRmSib(ModRmMode mode, int reg, RegisterID base, RegisterID index, int scale)
  2091. {
  2092. ASSERT(mode != ModRmRegister);
  2093. putModRm(mode, reg, hasSib);
  2094. m_buffer.putByteUnchecked((scale << 6) | ((index & 7) << 3) | (base & 7));
  2095. }
  2096. void registerModRM(int reg, RegisterID rm)
  2097. {
  2098. putModRm(ModRmRegister, reg, rm);
  2099. }
  2100. void memoryModRM(int reg, RegisterID base, int offset)
  2101. {
  2102. // A base of esp or r12 would be interpreted as a sib, so force a sib with no index & put the base in there.
  2103. #if CPU(X86_64)
  2104. if ((base == hasSib) || (base == hasSib2)) {
  2105. #else
  2106. if (base == hasSib) {
  2107. #endif
  2108. if (!offset) // No need to check if the base is noBase, since we know it is hasSib!
  2109. putModRmSib(ModRmMemoryNoDisp, reg, base, noIndex, 0);
  2110. else if (CAN_SIGN_EXTEND_8_32(offset)) {
  2111. putModRmSib(ModRmMemoryDisp8, reg, base, noIndex, 0);
  2112. m_buffer.putByteUnchecked(offset);
  2113. } else {
  2114. putModRmSib(ModRmMemoryDisp32, reg, base, noIndex, 0);
  2115. m_buffer.putIntUnchecked(offset);
  2116. }
  2117. } else {
  2118. #if CPU(X86_64)
  2119. if (!offset && (base != noBase) && (base != noBase2))
  2120. #else
  2121. if (!offset && (base != noBase))
  2122. #endif
  2123. putModRm(ModRmMemoryNoDisp, reg, base);
  2124. else if (CAN_SIGN_EXTEND_8_32(offset)) {
  2125. putModRm(ModRmMemoryDisp8, reg, base);
  2126. m_buffer.putByteUnchecked(offset);
  2127. } else {
  2128. putModRm(ModRmMemoryDisp32, reg, base);
  2129. m_buffer.putIntUnchecked(offset);
  2130. }
  2131. }
  2132. }
  2133. void memoryModRM_disp8(int reg, RegisterID base, int offset)
  2134. {
  2135. // A base of esp or r12 would be interpreted as a sib, so force a sib with no index & put the base in there.
  2136. ASSERT(CAN_SIGN_EXTEND_8_32(offset));
  2137. #if CPU(X86_64)
  2138. if ((base == hasSib) || (base == hasSib2)) {
  2139. #else
  2140. if (base == hasSib) {
  2141. #endif
  2142. putModRmSib(ModRmMemoryDisp8, reg, base, noIndex, 0);
  2143. m_buffer.putByteUnchecked(offset);
  2144. } else {
  2145. putModRm(ModRmMemoryDisp8, reg, base);
  2146. m_buffer.putByteUnchecked(offset);
  2147. }
  2148. }
  2149. void memoryModRM_disp32(int reg, RegisterID base, int offset)
  2150. {
  2151. // A base of esp or r12 would be interpreted as a sib, so force a sib with no index & put the base in there.
  2152. #if CPU(X86_64)
  2153. if ((base == hasSib) || (base == hasSib2)) {
  2154. #else
  2155. if (base == hasSib) {
  2156. #endif
  2157. putModRmSib(ModRmMemoryDisp32, reg, base, noIndex, 0);
  2158. m_buffer.putIntUnchecked(offset);
  2159. } else {
  2160. putModRm(ModRmMemoryDisp32, reg, base);
  2161. m_buffer.putIntUnchecked(offset);
  2162. }
  2163. }
  2164. void memoryModRM(int reg, RegisterID base, RegisterID index, int scale, int offset)
  2165. {
  2166. ASSERT(index != noIndex);
  2167. #if CPU(X86_64)
  2168. if (!offset && (base != noBase) && (base != noBase2))
  2169. #else
  2170. if (!offset && (base != noBase))
  2171. #endif
  2172. putModRmSib(ModRmMemoryNoDisp, reg, base, index, scale);
  2173. else if (CAN_SIGN_EXTEND_8_32(offset)) {
  2174. putModRmSib(ModRmMemoryDisp8, reg, base, index, scale);
  2175. m_buffer.putByteUnchecked(offset);
  2176. } else {
  2177. putModRmSib(ModRmMemoryDisp32, reg, base, index, scale);
  2178. m_buffer.putIntUnchecked(offset);
  2179. }
  2180. }
  2181. #if !CPU(X86_64)
  2182. void memoryModRM(int reg, const void* address)
  2183. {
  2184. // noBase + ModRmMemoryNoDisp means noBase + ModRmMemoryDisp32!
  2185. putModRm(ModRmMemoryNoDisp, reg, noBase);
  2186. m_buffer.putIntUnchecked(reinterpret_cast<int32_t>(address));
  2187. }
  2188. #endif
  2189. AssemblerBuffer m_buffer;
  2190. } m_formatter;
  2191. int m_indexOfLastWatchpoint;
  2192. int m_indexOfTailOfLastWatchpoint;
  2193. };
  2194. } // namespace JSC
  2195. #endif // ENABLE(ASSEMBLER) && CPU(X86)
  2196. #endif // X86Assembler_h