lj_emit_mips.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. ** MIPS instruction emitter.
  3. ** Copyright (C) 2005-2014 Mike Pall. See Copyright Notice in luajit.h
  4. */
  5. /* -- Emit basic instructions --------------------------------------------- */
  6. static void emit_dst(ASMState *as, MIPSIns mi, Reg rd, Reg rs, Reg rt)
  7. {
  8. *--as->mcp = mi | MIPSF_D(rd) | MIPSF_S(rs) | MIPSF_T(rt);
  9. }
  10. static void emit_dta(ASMState *as, MIPSIns mi, Reg rd, Reg rt, uint32_t a)
  11. {
  12. *--as->mcp = mi | MIPSF_D(rd) | MIPSF_T(rt) | MIPSF_A(a);
  13. }
  14. #define emit_ds(as, mi, rd, rs) emit_dst(as, (mi), (rd), (rs), 0)
  15. #define emit_tg(as, mi, rt, rg) emit_dst(as, (mi), (rg)&31, 0, (rt))
  16. static void emit_tsi(ASMState *as, MIPSIns mi, Reg rt, Reg rs, int32_t i)
  17. {
  18. *--as->mcp = mi | MIPSF_T(rt) | MIPSF_S(rs) | (i & 0xffff);
  19. }
  20. #define emit_ti(as, mi, rt, i) emit_tsi(as, (mi), (rt), 0, (i))
  21. #define emit_hsi(as, mi, rh, rs, i) emit_tsi(as, (mi), (rh) & 31, (rs), (i))
  22. static void emit_fgh(ASMState *as, MIPSIns mi, Reg rf, Reg rg, Reg rh)
  23. {
  24. *--as->mcp = mi | MIPSF_F(rf&31) | MIPSF_G(rg&31) | MIPSF_H(rh&31);
  25. }
  26. #define emit_fg(as, mi, rf, rg) emit_fgh(as, (mi), (rf), (rg), 0)
  27. static void emit_rotr(ASMState *as, Reg dest, Reg src, Reg tmp, uint32_t shift)
  28. {
  29. if ((as->flags & JIT_F_MIPS32R2)) {
  30. emit_dta(as, MIPSI_ROTR, dest, src, shift);
  31. } else {
  32. emit_dst(as, MIPSI_OR, dest, dest, tmp);
  33. emit_dta(as, MIPSI_SLL, dest, src, (-shift)&31);
  34. emit_dta(as, MIPSI_SRL, tmp, src, shift);
  35. }
  36. }
  37. /* -- Emit loads/stores --------------------------------------------------- */
  38. /* Prefer rematerialization of BASE/L from global_State over spills. */
  39. #define emit_canremat(ref) ((ref) <= REF_BASE)
  40. /* Try to find a one step delta relative to another constant. */
  41. static int emit_kdelta1(ASMState *as, Reg t, int32_t i)
  42. {
  43. RegSet work = ~as->freeset & RSET_GPR;
  44. while (work) {
  45. Reg r = rset_picktop(work);
  46. IRRef ref = regcost_ref(as->cost[r]);
  47. lua_assert(r != t);
  48. if (ref < ASMREF_L) {
  49. int32_t delta = i - (ra_iskref(ref) ? ra_krefk(as, ref) : IR(ref)->i);
  50. if (checki16(delta)) {
  51. emit_tsi(as, MIPSI_ADDIU, t, r, delta);
  52. return 1;
  53. }
  54. }
  55. rset_clear(work, r);
  56. }
  57. return 0; /* Failed. */
  58. }
  59. /* Load a 32 bit constant into a GPR. */
  60. static void emit_loadi(ASMState *as, Reg r, int32_t i)
  61. {
  62. if (checki16(i)) {
  63. emit_ti(as, MIPSI_LI, r, i);
  64. } else {
  65. if ((i & 0xffff)) {
  66. int32_t jgl = i32ptr(J2G(as->J));
  67. if ((uint32_t)(i-jgl) < 65536) {
  68. emit_tsi(as, MIPSI_ADDIU, r, RID_JGL, i-jgl-32768);
  69. return;
  70. } else if (emit_kdelta1(as, r, i)) {
  71. return;
  72. } else if ((i >> 16) == 0) {
  73. emit_tsi(as, MIPSI_ORI, r, RID_ZERO, i);
  74. return;
  75. }
  76. emit_tsi(as, MIPSI_ORI, r, r, i);
  77. }
  78. emit_ti(as, MIPSI_LUI, r, (i >> 16));
  79. }
  80. }
  81. #define emit_loada(as, r, addr) emit_loadi(as, (r), i32ptr((addr)))
  82. static Reg ra_allock(ASMState *as, int32_t k, RegSet allow);
  83. static void ra_allockreg(ASMState *as, int32_t k, Reg r);
  84. /* Get/set from constant pointer. */
  85. static void emit_lsptr(ASMState *as, MIPSIns mi, Reg r, void *p, RegSet allow)
  86. {
  87. int32_t jgl = i32ptr(J2G(as->J));
  88. int32_t i = i32ptr(p);
  89. Reg base;
  90. if ((uint32_t)(i-jgl) < 65536) {
  91. i = i-jgl-32768;
  92. base = RID_JGL;
  93. } else {
  94. base = ra_allock(as, i-(int16_t)i, allow);
  95. }
  96. emit_tsi(as, mi, r, base, i);
  97. }
  98. #define emit_loadn(as, r, tv) \
  99. emit_lsptr(as, MIPSI_LDC1, ((r) & 31), (void *)(tv), RSET_GPR)
  100. /* Get/set global_State fields. */
  101. static void emit_lsglptr(ASMState *as, MIPSIns mi, Reg r, int32_t ofs)
  102. {
  103. emit_tsi(as, mi, r, RID_JGL, ofs-32768);
  104. }
  105. #define emit_getgl(as, r, field) \
  106. emit_lsglptr(as, MIPSI_LW, (r), (int32_t)offsetof(global_State, field))
  107. #define emit_setgl(as, r, field) \
  108. emit_lsglptr(as, MIPSI_SW, (r), (int32_t)offsetof(global_State, field))
  109. /* Trace number is determined from per-trace exit stubs. */
  110. #define emit_setvmstate(as, i) UNUSED(i)
  111. /* -- Emit control-flow instructions -------------------------------------- */
  112. /* Label for internal jumps. */
  113. typedef MCode *MCLabel;
  114. /* Return label pointing to current PC. */
  115. #define emit_label(as) ((as)->mcp)
  116. static void emit_branch(ASMState *as, MIPSIns mi, Reg rs, Reg rt, MCode *target)
  117. {
  118. MCode *p = as->mcp;
  119. ptrdiff_t delta = target - p;
  120. lua_assert(((delta + 0x8000) >> 16) == 0);
  121. *--p = mi | MIPSF_S(rs) | MIPSF_T(rt) | ((uint32_t)delta & 0xffffu);
  122. as->mcp = p;
  123. }
  124. static void emit_jmp(ASMState *as, MCode *target)
  125. {
  126. *--as->mcp = MIPSI_NOP;
  127. emit_branch(as, MIPSI_B, RID_ZERO, RID_ZERO, (target));
  128. }
  129. static void emit_call(ASMState *as, void *target)
  130. {
  131. MCode *p = as->mcp;
  132. *--p = MIPSI_NOP;
  133. if ((((uintptr_t)target ^ (uintptr_t)p) >> 28) == 0)
  134. *--p = MIPSI_JAL | (((uintptr_t)target >>2) & 0x03ffffffu);
  135. else /* Target out of range: need indirect call. */
  136. *--p = MIPSI_JALR | MIPSF_S(RID_CFUNCADDR);
  137. as->mcp = p;
  138. ra_allockreg(as, i32ptr(target), RID_CFUNCADDR);
  139. }
  140. /* -- Emit generic operations --------------------------------------------- */
  141. #define emit_move(as, dst, src) \
  142. emit_ds(as, MIPSI_MOVE, (dst), (src))
  143. /* Generic move between two regs. */
  144. static void emit_movrr(ASMState *as, IRIns *ir, Reg dst, Reg src)
  145. {
  146. if (dst < RID_MAX_GPR)
  147. emit_move(as, dst, src);
  148. else
  149. emit_fg(as, irt_isnum(ir->t) ? MIPSI_MOV_D : MIPSI_MOV_S, dst, src);
  150. }
  151. /* Generic load of register from stack slot. */
  152. static void emit_spload(ASMState *as, IRIns *ir, Reg r, int32_t ofs)
  153. {
  154. if (r < RID_MAX_GPR)
  155. emit_tsi(as, MIPSI_LW, r, RID_SP, ofs);
  156. else
  157. emit_tsi(as, irt_isnum(ir->t) ? MIPSI_LDC1 : MIPSI_LWC1,
  158. (r & 31), RID_SP, ofs);
  159. }
  160. /* Generic store of register to stack slot. */
  161. static void emit_spstore(ASMState *as, IRIns *ir, Reg r, int32_t ofs)
  162. {
  163. if (r < RID_MAX_GPR)
  164. emit_tsi(as, MIPSI_SW, r, RID_SP, ofs);
  165. else
  166. emit_tsi(as, irt_isnum(ir->t) ? MIPSI_SDC1 : MIPSI_SWC1,
  167. (r&31), RID_SP, ofs);
  168. }
  169. /* Add offset to pointer. */
  170. static void emit_addptr(ASMState *as, Reg r, int32_t ofs)
  171. {
  172. if (ofs) {
  173. lua_assert(checki16(ofs));
  174. emit_tsi(as, MIPSI_ADDIU, r, r, ofs);
  175. }
  176. }
  177. #define emit_spsub(as, ofs) emit_addptr(as, RID_SP, -(ofs))