lj_emit_x86.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*
  2. ** x86/x64 instruction emitter.
  3. ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
  4. */
  5. /* -- Emit basic instructions --------------------------------------------- */
  6. #define MODRM(mode, r1, r2) ((MCode)((mode)+(((r1)&7)<<3)+((r2)&7)))
  7. #if LJ_64
  8. #define REXRB(p, rr, rb) \
  9. { MCode rex = 0x40 + (((rr)>>1)&4) + (((rb)>>3)&1); \
  10. if (rex != 0x40) *--(p) = rex; }
  11. #define FORCE_REX 0x200
  12. #define REX_64 (FORCE_REX|0x080000)
  13. #define VEX_64 0x800000
  14. #else
  15. #define REXRB(p, rr, rb) ((void)0)
  16. #define FORCE_REX 0
  17. #define REX_64 0
  18. #define VEX_64 0
  19. #endif
  20. #if LJ_GC64
  21. #define REX_GC64 REX_64
  22. #else
  23. #define REX_GC64 0
  24. #endif
  25. #define emit_i8(as, i) (*--as->mcp = (MCode)(i))
  26. #define emit_i32(as, i) (*(int32_t *)(as->mcp-4) = (i), as->mcp -= 4)
  27. #define emit_u32(as, u) (*(uint32_t *)(as->mcp-4) = (u), as->mcp -= 4)
  28. #define emit_x87op(as, xo) \
  29. (*(uint16_t *)(as->mcp-2) = (uint16_t)(xo), as->mcp -= 2)
  30. /* op */
  31. static LJ_AINLINE MCode *emit_op(x86Op xo, Reg rr, Reg rb, Reg rx,
  32. MCode *p, int delta)
  33. {
  34. int n = (int8_t)xo;
  35. if (n == -60) { /* VEX-encoded instruction */
  36. #if LJ_64
  37. xo ^= (((rr>>1)&4)+((rx>>2)&2)+((rb>>3)&1))<<13;
  38. #endif
  39. *(uint32_t *)(p+delta-5) = (uint32_t)xo;
  40. return p+delta-5;
  41. }
  42. #if defined(__GNUC__) || defined(__clang__)
  43. if (__builtin_constant_p(xo) && n == -2)
  44. p[delta-2] = (MCode)(xo >> 24);
  45. else if (__builtin_constant_p(xo) && n == -3)
  46. *(uint16_t *)(p+delta-3) = (uint16_t)(xo >> 16);
  47. else
  48. #endif
  49. *(uint32_t *)(p+delta-5) = (uint32_t)xo;
  50. p += n + delta;
  51. #if LJ_64
  52. {
  53. uint32_t rex = 0x40 + ((rr>>1)&(4+(FORCE_REX>>1)))+((rx>>2)&2)+((rb>>3)&1);
  54. if (rex != 0x40) {
  55. rex |= (rr >> 16);
  56. if (n == -4) { *p = (MCode)rex; rex = (MCode)(xo >> 8); }
  57. else if ((xo & 0xffffff) == 0x6600fd) { *p = (MCode)rex; rex = 0x66; }
  58. *--p = (MCode)rex;
  59. }
  60. }
  61. #else
  62. UNUSED(rr); UNUSED(rb); UNUSED(rx);
  63. #endif
  64. return p;
  65. }
  66. /* op + modrm */
  67. #define emit_opm(xo, mode, rr, rb, p, delta) \
  68. (p[(delta)-1] = MODRM((mode), (rr), (rb)), \
  69. emit_op((xo), (rr), (rb), 0, (p), (delta)))
  70. /* op + modrm + sib */
  71. #define emit_opmx(xo, mode, scale, rr, rb, rx, p) \
  72. (p[-1] = MODRM((scale), (rx), (rb)), \
  73. p[-2] = MODRM((mode), (rr), RID_ESP), \
  74. emit_op((xo), (rr), (rb), (rx), (p), -1))
  75. /* op r1, r2 */
  76. static void emit_rr(ASMState *as, x86Op xo, Reg r1, Reg r2)
  77. {
  78. MCode *p = as->mcp;
  79. as->mcp = emit_opm(xo, XM_REG, r1, r2, p, 0);
  80. }
  81. #if LJ_64 && defined(LUA_USE_ASSERT)
  82. /* [addr] is sign-extended in x64 and must be in lower 2G (not 4G). */
  83. static int32_t ptr2addr(const void *p)
  84. {
  85. lj_assertX((uintptr_t)p < (uintptr_t)0x80000000, "pointer outside 2G range");
  86. return i32ptr(p);
  87. }
  88. #else
  89. #define ptr2addr(p) (i32ptr((p)))
  90. #endif
  91. /* op r, [base+ofs] */
  92. static void emit_rmro(ASMState *as, x86Op xo, Reg rr, Reg rb, int32_t ofs)
  93. {
  94. MCode *p = as->mcp;
  95. x86Mode mode;
  96. if (ra_hasreg(rb)) {
  97. if (LJ_GC64 && rb == RID_RIP) {
  98. mode = XM_OFS0;
  99. p -= 4;
  100. *(int32_t *)p = ofs;
  101. } else if (ofs == 0 && (rb&7) != RID_EBP) {
  102. mode = XM_OFS0;
  103. } else if (checki8(ofs)) {
  104. *--p = (MCode)ofs;
  105. mode = XM_OFS8;
  106. } else {
  107. p -= 4;
  108. *(int32_t *)p = ofs;
  109. mode = XM_OFS32;
  110. }
  111. if ((rb&7) == RID_ESP)
  112. *--p = MODRM(XM_SCALE1, RID_ESP, RID_ESP);
  113. } else {
  114. *(int32_t *)(p-4) = ofs;
  115. #if LJ_64
  116. p[-5] = MODRM(XM_SCALE1, RID_ESP, RID_EBP);
  117. p -= 5;
  118. rb = RID_ESP;
  119. #else
  120. p -= 4;
  121. rb = RID_EBP;
  122. #endif
  123. mode = XM_OFS0;
  124. }
  125. as->mcp = emit_opm(xo, mode, rr, rb, p, 0);
  126. }
  127. /* op r, [base+idx*scale+ofs] */
  128. static void emit_rmrxo(ASMState *as, x86Op xo, Reg rr, Reg rb, Reg rx,
  129. x86Mode scale, int32_t ofs)
  130. {
  131. MCode *p = as->mcp;
  132. x86Mode mode;
  133. if (ofs == 0 && (rb&7) != RID_EBP) {
  134. mode = XM_OFS0;
  135. } else if (checki8(ofs)) {
  136. mode = XM_OFS8;
  137. *--p = (MCode)ofs;
  138. } else {
  139. mode = XM_OFS32;
  140. p -= 4;
  141. *(int32_t *)p = ofs;
  142. }
  143. as->mcp = emit_opmx(xo, mode, scale, rr, rb, rx, p);
  144. }
  145. /* op r, i */
  146. static void emit_gri(ASMState *as, x86Group xg, Reg rb, int32_t i)
  147. {
  148. MCode *p = as->mcp;
  149. x86Op xo;
  150. if (checki8(i)) {
  151. *--p = (MCode)i;
  152. xo = XG_TOXOi8(xg);
  153. } else {
  154. p -= 4;
  155. *(int32_t *)p = i;
  156. xo = XG_TOXOi(xg);
  157. }
  158. as->mcp = emit_opm(xo, XM_REG, (Reg)(xg & 7) | (rb & REX_64), rb, p, 0);
  159. }
  160. /* op [base+ofs], i */
  161. static void emit_gmroi(ASMState *as, x86Group xg, Reg rb, int32_t ofs,
  162. int32_t i)
  163. {
  164. x86Op xo;
  165. if (checki8(i)) {
  166. emit_i8(as, i);
  167. xo = XG_TOXOi8(xg);
  168. } else {
  169. emit_i32(as, i);
  170. xo = XG_TOXOi(xg);
  171. }
  172. emit_rmro(as, xo, (Reg)(xg & 7), rb, ofs);
  173. }
  174. #define emit_shifti(as, xg, r, i) \
  175. (emit_i8(as, (i)), emit_rr(as, XO_SHIFTi, (Reg)(xg), (r)))
  176. /* op r, rm/mrm */
  177. static void emit_mrm(ASMState *as, x86Op xo, Reg rr, Reg rb)
  178. {
  179. MCode *p = as->mcp;
  180. x86Mode mode = XM_REG;
  181. if (rb == RID_MRM) {
  182. rb = as->mrm.base;
  183. if (rb == RID_NONE) {
  184. rb = RID_EBP;
  185. mode = XM_OFS0;
  186. p -= 4;
  187. *(int32_t *)p = as->mrm.ofs;
  188. if (as->mrm.idx != RID_NONE)
  189. goto mrmidx;
  190. #if LJ_64
  191. *--p = MODRM(XM_SCALE1, RID_ESP, RID_EBP);
  192. rb = RID_ESP;
  193. #endif
  194. } else if (LJ_GC64 && rb == RID_RIP) {
  195. lj_assertA(as->mrm.idx == RID_NONE, "RIP-rel mrm cannot have index");
  196. mode = XM_OFS0;
  197. p -= 4;
  198. *(int32_t *)p = as->mrm.ofs;
  199. } else {
  200. if (as->mrm.ofs == 0 && (rb&7) != RID_EBP) {
  201. mode = XM_OFS0;
  202. } else if (checki8(as->mrm.ofs)) {
  203. *--p = (MCode)as->mrm.ofs;
  204. mode = XM_OFS8;
  205. } else {
  206. p -= 4;
  207. *(int32_t *)p = as->mrm.ofs;
  208. mode = XM_OFS32;
  209. }
  210. if (as->mrm.idx != RID_NONE) {
  211. mrmidx:
  212. as->mcp = emit_opmx(xo, mode, as->mrm.scale, rr, rb, as->mrm.idx, p);
  213. return;
  214. }
  215. if ((rb&7) == RID_ESP)
  216. *--p = MODRM(XM_SCALE1, RID_ESP, RID_ESP);
  217. }
  218. }
  219. as->mcp = emit_opm(xo, mode, rr, rb, p, 0);
  220. }
  221. /* op rm/mrm, i */
  222. static void emit_gmrmi(ASMState *as, x86Group xg, Reg rb, int32_t i)
  223. {
  224. x86Op xo;
  225. if (checki8(i)) {
  226. emit_i8(as, i);
  227. xo = XG_TOXOi8(xg);
  228. } else {
  229. emit_i32(as, i);
  230. xo = XG_TOXOi(xg);
  231. }
  232. emit_mrm(as, xo, (Reg)(xg & 7) | (rb & REX_64), (rb & ~REX_64));
  233. }
  234. /* -- Emit loads/stores --------------------------------------------------- */
  235. /* mov [base+ofs], i */
  236. static void emit_movmroi(ASMState *as, Reg base, int32_t ofs, int32_t i)
  237. {
  238. emit_i32(as, i);
  239. emit_rmro(as, XO_MOVmi, 0, base, ofs);
  240. }
  241. /* mov [base+ofs], r */
  242. #define emit_movtomro(as, r, base, ofs) \
  243. emit_rmro(as, XO_MOVto, (r), (base), (ofs))
  244. /* Get/set global_State fields. */
  245. #define emit_opgl(as, xo, r, field) \
  246. emit_rma(as, (xo), (r), (void *)&J2G(as->J)->field)
  247. #define emit_getgl(as, r, field) emit_opgl(as, XO_MOV, (r)|REX_GC64, field)
  248. #define emit_setgl(as, r, field) emit_opgl(as, XO_MOVto, (r)|REX_GC64, field)
  249. #define emit_setvmstate(as, i) \
  250. (emit_i32(as, i), emit_opgl(as, XO_MOVmi, 0, vmstate))
  251. /* mov r, i / xor r, r */
  252. static void emit_loadi(ASMState *as, Reg r, int32_t i)
  253. {
  254. /* XOR r,r is shorter, but modifies the flags. This is bad for HIOP/jcc. */
  255. if (i == 0 && !(LJ_32 && (IR(as->curins)->o == IR_HIOP ||
  256. (as->curins+1 < as->T->nins &&
  257. IR(as->curins+1)->o == IR_HIOP))) &&
  258. !((*as->mcp == 0x0f && (as->mcp[1] & 0xf0) == XI_JCCn) ||
  259. (*as->mcp & 0xf0) == XI_JCCs)) {
  260. emit_rr(as, XO_ARITH(XOg_XOR), r, r);
  261. } else {
  262. MCode *p = as->mcp;
  263. *(int32_t *)(p-4) = i;
  264. p[-5] = (MCode)(XI_MOVri+(r&7));
  265. p -= 5;
  266. REXRB(p, 0, r);
  267. as->mcp = p;
  268. }
  269. }
  270. #if LJ_GC64
  271. #define dispofs(as, k) \
  272. ((intptr_t)((uintptr_t)(k) - (uintptr_t)J2GG(as->J)->dispatch))
  273. #define mcpofs(as, k) \
  274. ((intptr_t)((uintptr_t)(k) - (uintptr_t)as->mcp))
  275. #define mctopofs(as, k) \
  276. ((intptr_t)((uintptr_t)(k) - (uintptr_t)as->mctop))
  277. /* mov r, addr */
  278. #define emit_loada(as, r, addr) \
  279. emit_loadu64(as, (r), (uintptr_t)(addr))
  280. #else
  281. /* mov r, addr */
  282. #define emit_loada(as, r, addr) \
  283. emit_loadi(as, (r), ptr2addr((addr)))
  284. #endif
  285. #if LJ_64
  286. /* mov r, imm64 or shorter 32 bit extended load. */
  287. static void emit_loadu64(ASMState *as, Reg r, uint64_t u64)
  288. {
  289. if (checku32(u64)) { /* 32 bit load clears upper 32 bits. */
  290. emit_loadi(as, r, (int32_t)u64);
  291. } else if (checki32((int64_t)u64)) { /* Sign-extended 32 bit load. */
  292. MCode *p = as->mcp;
  293. *(int32_t *)(p-4) = (int32_t)u64;
  294. as->mcp = emit_opm(XO_MOVmi, XM_REG, REX_64, r, p, -4);
  295. #if LJ_GC64
  296. } else if (checki32(dispofs(as, u64))) {
  297. emit_rmro(as, XO_LEA, r|REX_64, RID_DISPATCH, (int32_t)dispofs(as, u64));
  298. } else if (checki32(mcpofs(as, u64)) && checki32(mctopofs(as, u64))) {
  299. /* Since as->realign assumes the code size doesn't change, check
  300. ** RIP-relative addressing reachability for both as->mcp and as->mctop.
  301. */
  302. emit_rmro(as, XO_LEA, r|REX_64, RID_RIP, (int32_t)mcpofs(as, u64));
  303. #endif
  304. } else { /* Full-size 64 bit load. */
  305. MCode *p = as->mcp;
  306. *(uint64_t *)(p-8) = u64;
  307. p[-9] = (MCode)(XI_MOVri+(r&7));
  308. p[-10] = 0x48 + ((r>>3)&1);
  309. p -= 10;
  310. as->mcp = p;
  311. }
  312. }
  313. #endif
  314. /* op r, [addr] */
  315. static void emit_rma(ASMState *as, x86Op xo, Reg rr, const void *addr)
  316. {
  317. #if LJ_GC64
  318. if (checki32(dispofs(as, addr))) {
  319. emit_rmro(as, xo, rr, RID_DISPATCH, (int32_t)dispofs(as, addr));
  320. } else if (checki32(mcpofs(as, addr)) && checki32(mctopofs(as, addr))) {
  321. emit_rmro(as, xo, rr, RID_RIP, (int32_t)mcpofs(as, addr));
  322. } else if (!checki32((intptr_t)addr)) {
  323. Reg ra = (rr & 15);
  324. if (xo != XO_MOV) {
  325. /* We can't allocate a register here. Use and restore DISPATCH. Ugly. */
  326. uint64_t dispaddr = (uintptr_t)J2GG(as->J)->dispatch;
  327. uint8_t i8 = xo == XO_GROUP3b ? *as->mcp++ : 0;
  328. ra = RID_DISPATCH;
  329. if (checku32(dispaddr)) {
  330. emit_loadi(as, ra, (int32_t)dispaddr);
  331. } else { /* Full-size 64 bit load. */
  332. MCode *p = as->mcp;
  333. *(uint64_t *)(p-8) = dispaddr;
  334. p[-9] = (MCode)(XI_MOVri+(ra&7));
  335. p[-10] = 0x48 + ((ra>>3)&1);
  336. p -= 10;
  337. as->mcp = p;
  338. }
  339. if (xo == XO_GROUP3b) emit_i8(as, i8);
  340. }
  341. emit_rmro(as, xo, rr, ra, 0);
  342. emit_loadu64(as, ra, (uintptr_t)addr);
  343. } else
  344. #endif
  345. {
  346. MCode *p = as->mcp;
  347. *(int32_t *)(p-4) = ptr2addr(addr);
  348. #if LJ_64
  349. p[-5] = MODRM(XM_SCALE1, RID_ESP, RID_EBP);
  350. as->mcp = emit_opm(xo, XM_OFS0, rr, RID_ESP, p, -5);
  351. #else
  352. as->mcp = emit_opm(xo, XM_OFS0, rr, RID_EBP, p, -4);
  353. #endif
  354. }
  355. }
  356. /* Load 64 bit IR constant into register. */
  357. static void emit_loadk64(ASMState *as, Reg r, IRIns *ir)
  358. {
  359. Reg r64;
  360. x86Op xo;
  361. const uint64_t *k = &ir_k64(ir)->u64;
  362. if (rset_test(RSET_FPR, r)) {
  363. r64 = r;
  364. xo = XO_MOVSD;
  365. } else {
  366. r64 = r | REX_64;
  367. xo = XO_MOV;
  368. }
  369. if (*k == 0) {
  370. emit_rr(as, rset_test(RSET_FPR, r) ? XO_XORPS : XO_ARITH(XOg_XOR), r, r);
  371. #if LJ_GC64
  372. } else if (checki32((intptr_t)k) || checki32(dispofs(as, k)) ||
  373. (checki32(mcpofs(as, k)) && checki32(mctopofs(as, k)))) {
  374. emit_rma(as, xo, r64, k);
  375. } else {
  376. if (ir->i) {
  377. lj_assertA(*k == *(uint64_t*)(as->mctop - ir->i),
  378. "bad interned 64 bit constant");
  379. } else if (as->curins <= as->stopins && rset_test(RSET_GPR, r)) {
  380. emit_loadu64(as, r, *k);
  381. return;
  382. } else {
  383. /* If all else fails, add the FP constant at the MCode area bottom. */
  384. while ((uintptr_t)as->mcbot & 7) *as->mcbot++ = XI_INT3;
  385. *(uint64_t *)as->mcbot = *k;
  386. ir->i = (int32_t)(as->mctop - as->mcbot);
  387. as->mcbot += 8;
  388. as->mclim = as->mcbot + MCLIM_REDZONE;
  389. lj_mcode_commitbot(as->J, as->mcbot);
  390. }
  391. emit_rmro(as, xo, r64, RID_RIP, (int32_t)mcpofs(as, as->mctop - ir->i));
  392. #else
  393. } else {
  394. emit_rma(as, xo, r64, k);
  395. #endif
  396. }
  397. }
  398. /* -- Emit control-flow instructions -------------------------------------- */
  399. /* Label for short jumps. */
  400. typedef MCode *MCLabel;
  401. #if LJ_32 && LJ_HASFFI
  402. /* jmp short target */
  403. static void emit_sjmp(ASMState *as, MCLabel target)
  404. {
  405. MCode *p = as->mcp;
  406. ptrdiff_t delta = target - p;
  407. lj_assertA(delta == (int8_t)delta, "short jump target out of range");
  408. p[-1] = (MCode)(int8_t)delta;
  409. p[-2] = XI_JMPs;
  410. as->mcp = p - 2;
  411. }
  412. #endif
  413. /* jcc short target */
  414. static void emit_sjcc(ASMState *as, int cc, MCLabel target)
  415. {
  416. MCode *p = as->mcp;
  417. ptrdiff_t delta = target - p;
  418. lj_assertA(delta == (int8_t)delta, "short jump target out of range");
  419. p[-1] = (MCode)(int8_t)delta;
  420. p[-2] = (MCode)(XI_JCCs+(cc&15));
  421. as->mcp = p - 2;
  422. }
  423. /* jcc short (pending target) */
  424. static MCLabel emit_sjcc_label(ASMState *as, int cc)
  425. {
  426. MCode *p = as->mcp;
  427. p[-1] = 0;
  428. p[-2] = (MCode)(XI_JCCs+(cc&15));
  429. as->mcp = p - 2;
  430. return p;
  431. }
  432. /* Fixup jcc short target. */
  433. static void emit_sfixup(ASMState *as, MCLabel source)
  434. {
  435. source[-1] = (MCode)(as->mcp-source);
  436. }
  437. /* Return label pointing to current PC. */
  438. #define emit_label(as) ((as)->mcp)
  439. /* Compute relative 32 bit offset for jump and call instructions. */
  440. static LJ_AINLINE int32_t jmprel(jit_State *J, MCode *p, MCode *target)
  441. {
  442. ptrdiff_t delta = target - p;
  443. UNUSED(J);
  444. lj_assertJ(delta == (int32_t)delta, "jump target out of range");
  445. return (int32_t)delta;
  446. }
  447. /* jcc target */
  448. static void emit_jcc(ASMState *as, int cc, MCode *target)
  449. {
  450. MCode *p = as->mcp;
  451. *(int32_t *)(p-4) = jmprel(as->J, p, target);
  452. p[-5] = (MCode)(XI_JCCn+(cc&15));
  453. p[-6] = 0x0f;
  454. as->mcp = p - 6;
  455. }
  456. /* jmp target */
  457. static void emit_jmp(ASMState *as, MCode *target)
  458. {
  459. MCode *p = as->mcp;
  460. *(int32_t *)(p-4) = jmprel(as->J, p, target);
  461. p[-5] = XI_JMP;
  462. as->mcp = p - 5;
  463. }
  464. /* call target */
  465. static void emit_call_(ASMState *as, MCode *target)
  466. {
  467. MCode *p = as->mcp;
  468. #if LJ_64
  469. if (target-p != (int32_t)(target-p)) {
  470. /* Assumes RID_RET is never an argument to calls and always clobbered. */
  471. emit_rr(as, XO_GROUP5, XOg_CALL, RID_RET);
  472. emit_loadu64(as, RID_RET, (uint64_t)target);
  473. return;
  474. }
  475. #endif
  476. *(int32_t *)(p-4) = jmprel(as->J, p, target);
  477. p[-5] = XI_CALL;
  478. as->mcp = p - 5;
  479. }
  480. #define emit_call(as, f) emit_call_(as, (MCode *)(void *)(f))
  481. /* -- Emit generic operations --------------------------------------------- */
  482. /* Use 64 bit operations to handle 64 bit IR types. */
  483. #if LJ_64
  484. #define REX_64IR(ir, r) ((r) + (irt_is64((ir)->t) ? REX_64 : 0))
  485. #define VEX_64IR(ir, r) ((r) + (irt_is64((ir)->t) ? VEX_64 : 0))
  486. #else
  487. #define REX_64IR(ir, r) (r)
  488. #define VEX_64IR(ir, r) (r)
  489. #endif
  490. /* Generic move between two regs. */
  491. static void emit_movrr(ASMState *as, IRIns *ir, Reg dst, Reg src)
  492. {
  493. UNUSED(ir);
  494. if (dst < RID_MAX_GPR)
  495. emit_rr(as, XO_MOV, REX_64IR(ir, dst), src);
  496. else
  497. emit_rr(as, XO_MOVAPS, dst, src);
  498. }
  499. /* Generic load of register with base and (small) offset address. */
  500. static void emit_loadofs(ASMState *as, IRIns *ir, Reg r, Reg base, int32_t ofs)
  501. {
  502. if (r < RID_MAX_GPR)
  503. emit_rmro(as, XO_MOV, REX_64IR(ir, r), base, ofs);
  504. else
  505. emit_rmro(as, irt_isnum(ir->t) ? XO_MOVSD : XO_MOVSS, r, base, ofs);
  506. }
  507. /* Generic store of register with base and (small) offset address. */
  508. static void emit_storeofs(ASMState *as, IRIns *ir, Reg r, Reg base, int32_t ofs)
  509. {
  510. if (r < RID_MAX_GPR)
  511. emit_rmro(as, XO_MOVto, REX_64IR(ir, r), base, ofs);
  512. else
  513. emit_rmro(as, irt_isnum(ir->t) ? XO_MOVSDto : XO_MOVSSto, r, base, ofs);
  514. }
  515. /* Add offset to pointer. */
  516. static void emit_addptr(ASMState *as, Reg r, int32_t ofs)
  517. {
  518. if (ofs) {
  519. emit_gri(as, XG_ARITHi(XOg_ADD), r|REX_GC64, ofs);
  520. }
  521. }
  522. #define emit_spsub(as, ofs) emit_addptr(as, RID_ESP|REX_64, -(ofs))
  523. /* Prefer rematerialization of BASE/L from global_State over spills. */
  524. #define emit_canremat(ref) ((ref) <= REF_BASE)