aboutsummaryrefslogtreecommitdiff
path: root/src/lj_emit_mips.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/lj_emit_mips.h')
-rw-r--r--src/lj_emit_mips.h161
1 files changed, 130 insertions, 31 deletions
diff --git a/src/lj_emit_mips.h b/src/lj_emit_mips.h
index 366cf7ab..0cea5479 100644
--- a/src/lj_emit_mips.h
+++ b/src/lj_emit_mips.h
@@ -3,6 +3,32 @@
3** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h 3** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h
4*/ 4*/
5 5
6#if LJ_64
7static intptr_t get_k64val(ASMState *as, IRRef ref)
8{
9 IRIns *ir = IR(ref);
10 if (ir->o == IR_KINT64) {
11 return (intptr_t)ir_kint64(ir)->u64;
12 } else if (ir->o == IR_KGC) {
13 return (intptr_t)ir_kgc(ir);
14 } else if (ir->o == IR_KPTR || ir->o == IR_KKPTR) {
15 return (intptr_t)ir_kptr(ir);
16 } else if (LJ_SOFTFP && ir->o == IR_KNUM) {
17 return (intptr_t)ir_knum(ir)->u64;
18 } else {
19 lj_assertA(ir->o == IR_KINT || ir->o == IR_KNULL,
20 "bad 64 bit const IR op %d", ir->o);
21 return ir->i; /* Sign-extended. */
22 }
23}
24#endif
25
26#if LJ_64
27#define get_kval(as, ref) get_k64val(as, ref)
28#else
29#define get_kval(as, ref) (IR((ref))->i)
30#endif
31
6/* -- Emit basic instructions --------------------------------------------- */ 32/* -- Emit basic instructions --------------------------------------------- */
7 33
8static void emit_dst(ASMState *as, MIPSIns mi, Reg rd, Reg rs, Reg rt) 34static void emit_dst(ASMState *as, MIPSIns mi, Reg rd, Reg rs, Reg rt)
@@ -35,7 +61,7 @@ static void emit_fgh(ASMState *as, MIPSIns mi, Reg rf, Reg rg, Reg rh)
35 61
36static void emit_rotr(ASMState *as, Reg dest, Reg src, Reg tmp, uint32_t shift) 62static void emit_rotr(ASMState *as, Reg dest, Reg src, Reg tmp, uint32_t shift)
37{ 63{
38 if ((as->flags & JIT_F_MIPS32R2)) { 64 if (LJ_64 || (as->flags & JIT_F_MIPSXXR2)) {
39 emit_dta(as, MIPSI_ROTR, dest, src, shift); 65 emit_dta(as, MIPSI_ROTR, dest, src, shift);
40 } else { 66 } else {
41 emit_dst(as, MIPSI_OR, dest, dest, tmp); 67 emit_dst(as, MIPSI_OR, dest, dest, tmp);
@@ -44,23 +70,32 @@ static void emit_rotr(ASMState *as, Reg dest, Reg src, Reg tmp, uint32_t shift)
44 } 70 }
45} 71}
46 72
73#if LJ_64 || LJ_HASBUFFER
74static void emit_tsml(ASMState *as, MIPSIns mi, Reg rt, Reg rs, uint32_t msb,
75 uint32_t lsb)
76{
77 *--as->mcp = mi | MIPSF_T(rt) | MIPSF_S(rs) | MIPSF_M(msb) | MIPSF_L(lsb);
78}
79#endif
80
47/* -- Emit loads/stores --------------------------------------------------- */ 81/* -- Emit loads/stores --------------------------------------------------- */
48 82
49/* Prefer rematerialization of BASE/L from global_State over spills. */ 83/* Prefer rematerialization of BASE/L from global_State over spills. */
50#define emit_canremat(ref) ((ref) <= REF_BASE) 84#define emit_canremat(ref) ((ref) <= REF_BASE)
51 85
52/* Try to find a one step delta relative to another constant. */ 86/* Try to find a one step delta relative to another constant. */
53static int emit_kdelta1(ASMState *as, Reg t, int32_t i) 87static int emit_kdelta1(ASMState *as, Reg rd, intptr_t i)
54{ 88{
55 RegSet work = ~as->freeset & RSET_GPR; 89 RegSet work = ~as->freeset & RSET_GPR;
56 while (work) { 90 while (work) {
57 Reg r = rset_picktop(work); 91 Reg r = rset_picktop(work);
58 IRRef ref = regcost_ref(as->cost[r]); 92 IRRef ref = regcost_ref(as->cost[r]);
59 lua_assert(r != t); 93 lj_assertA(r != rd, "dest reg %d not free", rd);
60 if (ref < ASMREF_L) { 94 if (ref < ASMREF_L) {
61 int32_t delta = i - (ra_iskref(ref) ? ra_krefk(as, ref) : IR(ref)->i); 95 intptr_t delta = (intptr_t)((uintptr_t)i -
96 (uintptr_t)(ra_iskref(ref) ? ra_krefk(as, ref) : get_kval(as, ref)));
62 if (checki16(delta)) { 97 if (checki16(delta)) {
63 emit_tsi(as, MIPSI_ADDIU, t, r, delta); 98 emit_tsi(as, MIPSI_AADDIU, rd, r, delta);
64 return 1; 99 return 1;
65 } 100 }
66 } 101 }
@@ -76,8 +111,8 @@ static void emit_loadi(ASMState *as, Reg r, int32_t i)
76 emit_ti(as, MIPSI_LI, r, i); 111 emit_ti(as, MIPSI_LI, r, i);
77 } else { 112 } else {
78 if ((i & 0xffff)) { 113 if ((i & 0xffff)) {
79 int32_t jgl = i32ptr(J2G(as->J)); 114 intptr_t jgl = (intptr_t)(void *)J2G(as->J);
80 if ((uint32_t)(i-jgl) < 65536) { 115 if ((uintptr_t)(i-jgl) < 65536) {
81 emit_tsi(as, MIPSI_ADDIU, r, RID_JGL, i-jgl-32768); 116 emit_tsi(as, MIPSI_ADDIU, r, RID_JGL, i-jgl-32768);
82 return; 117 return;
83 } else if (emit_kdelta1(as, r, i)) { 118 } else if (emit_kdelta1(as, r, i)) {
@@ -92,16 +127,49 @@ static void emit_loadi(ASMState *as, Reg r, int32_t i)
92 } 127 }
93} 128}
94 129
130#if LJ_64
131/* Load a 64 bit constant into a GPR. */
132static void emit_loadu64(ASMState *as, Reg r, uint64_t u64)
133{
134 if (checki32((int64_t)u64)) {
135 emit_loadi(as, r, (int32_t)u64);
136 } else {
137 uint64_t delta = u64 - (uint64_t)(void *)J2G(as->J);
138 if (delta < 65536) {
139 emit_tsi(as, MIPSI_DADDIU, r, RID_JGL, (int32_t)(delta-32768));
140 } else if (emit_kdelta1(as, r, (intptr_t)u64)) {
141 return;
142 } else {
143 /* TODO MIPSR6: Use DAHI & DATI. Caveat: sign-extension. */
144 if ((u64 & 0xffff)) {
145 emit_tsi(as, MIPSI_ORI, r, r, u64 & 0xffff);
146 }
147 if (((u64 >> 16) & 0xffff)) {
148 emit_dta(as, MIPSI_DSLL, r, r, 16);
149 emit_tsi(as, MIPSI_ORI, r, r, (u64 >> 16) & 0xffff);
150 emit_dta(as, MIPSI_DSLL, r, r, 16);
151 } else {
152 emit_dta(as, MIPSI_DSLL32, r, r, 0);
153 }
154 emit_loadi(as, r, (int32_t)(u64 >> 32));
155 }
156 /* TODO: There are probably more optimization opportunities. */
157 }
158}
159
160#define emit_loada(as, r, addr) emit_loadu64(as, (r), u64ptr((addr)))
161#else
95#define emit_loada(as, r, addr) emit_loadi(as, (r), i32ptr((addr))) 162#define emit_loada(as, r, addr) emit_loadi(as, (r), i32ptr((addr)))
163#endif
96 164
97static Reg ra_allock(ASMState *as, int32_t k, RegSet allow); 165static Reg ra_allock(ASMState *as, intptr_t k, RegSet allow);
98static void ra_allockreg(ASMState *as, int32_t k, Reg r); 166static void ra_allockreg(ASMState *as, intptr_t k, Reg r);
99 167
100/* Get/set from constant pointer. */ 168/* Get/set from constant pointer. */
101static void emit_lsptr(ASMState *as, MIPSIns mi, Reg r, void *p, RegSet allow) 169static void emit_lsptr(ASMState *as, MIPSIns mi, Reg r, void *p, RegSet allow)
102{ 170{
103 int32_t jgl = i32ptr(J2G(as->J)); 171 intptr_t jgl = (intptr_t)(J2G(as->J));
104 int32_t i = i32ptr(p); 172 intptr_t i = (intptr_t)(p);
105 Reg base; 173 Reg base;
106 if ((uint32_t)(i-jgl) < 65536) { 174 if ((uint32_t)(i-jgl) < 65536) {
107 i = i-jgl-32768; 175 i = i-jgl-32768;
@@ -112,8 +180,24 @@ static void emit_lsptr(ASMState *as, MIPSIns mi, Reg r, void *p, RegSet allow)
112 emit_tsi(as, mi, r, base, i); 180 emit_tsi(as, mi, r, base, i);
113} 181}
114 182
115#define emit_loadn(as, r, tv) \ 183#if LJ_64
116 emit_lsptr(as, MIPSI_LDC1, ((r) & 31), (void *)(tv), RSET_GPR) 184static void emit_loadk64(ASMState *as, Reg r, IRIns *ir)
185{
186 const uint64_t *k = &ir_k64(ir)->u64;
187 Reg r64 = r;
188 if (rset_test(RSET_FPR, r)) {
189 r64 = RID_TMP;
190 emit_tg(as, MIPSI_DMTC1, r64, r);
191 }
192 if ((uint32_t)((intptr_t)k-(intptr_t)J2G(as->J)) < 65536)
193 emit_lsptr(as, MIPSI_LD, r64, (void *)k, 0);
194 else
195 emit_loadu64(as, r64, *k);
196}
197#else
198#define emit_loadk64(as, r, ir) \
199 emit_lsptr(as, MIPSI_LDC1, ((r) & 31), (void *)&ir_knum((ir))->u64, RSET_GPR)
200#endif
117 201
118/* Get/set global_State fields. */ 202/* Get/set global_State fields. */
119static void emit_lsglptr(ASMState *as, MIPSIns mi, Reg r, int32_t ofs) 203static void emit_lsglptr(ASMState *as, MIPSIns mi, Reg r, int32_t ofs)
@@ -122,9 +206,9 @@ static void emit_lsglptr(ASMState *as, MIPSIns mi, Reg r, int32_t ofs)
122} 206}
123 207
124#define emit_getgl(as, r, field) \ 208#define emit_getgl(as, r, field) \
125 emit_lsglptr(as, MIPSI_LW, (r), (int32_t)offsetof(global_State, field)) 209 emit_lsglptr(as, MIPSI_AL, (r), (int32_t)offsetof(global_State, field))
126#define emit_setgl(as, r, field) \ 210#define emit_setgl(as, r, field) \
127 emit_lsglptr(as, MIPSI_SW, (r), (int32_t)offsetof(global_State, field)) 211 emit_lsglptr(as, MIPSI_AS, (r), (int32_t)offsetof(global_State, field))
128 212
129/* Trace number is determined from per-trace exit stubs. */ 213/* Trace number is determined from per-trace exit stubs. */
130#define emit_setvmstate(as, i) UNUSED(i) 214#define emit_setvmstate(as, i) UNUSED(i)
@@ -141,7 +225,7 @@ static void emit_branch(ASMState *as, MIPSIns mi, Reg rs, Reg rt, MCode *target)
141{ 225{
142 MCode *p = as->mcp; 226 MCode *p = as->mcp;
143 ptrdiff_t delta = target - p; 227 ptrdiff_t delta = target - p;
144 lua_assert(((delta + 0x8000) >> 16) == 0); 228 lj_assertA(((delta + 0x8000) >> 16) == 0, "branch target out of range");
145 *--p = mi | MIPSF_S(rs) | MIPSF_T(rt) | ((uint32_t)delta & 0xffffu); 229 *--p = mi | MIPSF_S(rs) | MIPSF_T(rt) | ((uint32_t)delta & 0xffffu);
146 as->mcp = p; 230 as->mcp = p;
147} 231}
@@ -152,16 +236,31 @@ static void emit_jmp(ASMState *as, MCode *target)
152 emit_branch(as, MIPSI_B, RID_ZERO, RID_ZERO, (target)); 236 emit_branch(as, MIPSI_B, RID_ZERO, RID_ZERO, (target));
153} 237}
154 238
155static void emit_call(ASMState *as, void *target) 239static void emit_call(ASMState *as, void *target, int needcfa)
156{ 240{
157 MCode *p = as->mcp; 241 MCode *p = as->mcp;
158 *--p = MIPSI_NOP; 242#if LJ_TARGET_MIPSR6
159 if ((((uintptr_t)target ^ (uintptr_t)p) >> 28) == 0) 243 ptrdiff_t delta = (char *)target - (char *)p;
244 if ((((delta>>2) + 0x02000000) >> 26) == 0) { /* Try compact call first. */
245 *--p = MIPSI_BALC | (((uintptr_t)delta >>2) & 0x03ffffffu);
246 as->mcp = p;
247 return;
248 }
249#endif
250 *--p = MIPSI_NOP; /* Delay slot. */
251 if ((((uintptr_t)target ^ (uintptr_t)p) >> 28) == 0) {
252#if !LJ_TARGET_MIPSR6
253 *--p = (((uintptr_t)target & 1) ? MIPSI_JALX : MIPSI_JAL) |
254 (((uintptr_t)target >>2) & 0x03ffffffu);
255#else
160 *--p = MIPSI_JAL | (((uintptr_t)target >>2) & 0x03ffffffu); 256 *--p = MIPSI_JAL | (((uintptr_t)target >>2) & 0x03ffffffu);
161 else /* Target out of range: need indirect call. */ 257#endif
258 } else { /* Target out of range: need indirect call. */
162 *--p = MIPSI_JALR | MIPSF_S(RID_CFUNCADDR); 259 *--p = MIPSI_JALR | MIPSF_S(RID_CFUNCADDR);
260 needcfa = 1;
261 }
163 as->mcp = p; 262 as->mcp = p;
164 ra_allockreg(as, i32ptr(target), RID_CFUNCADDR); 263 if (needcfa) ra_allockreg(as, (intptr_t)target, RID_CFUNCADDR);
165} 264}
166 265
167/* -- Emit generic operations --------------------------------------------- */ 266/* -- Emit generic operations --------------------------------------------- */
@@ -178,32 +277,32 @@ static void emit_movrr(ASMState *as, IRIns *ir, Reg dst, Reg src)
178 emit_fg(as, irt_isnum(ir->t) ? MIPSI_MOV_D : MIPSI_MOV_S, dst, src); 277 emit_fg(as, irt_isnum(ir->t) ? MIPSI_MOV_D : MIPSI_MOV_S, dst, src);
179} 278}
180 279
181/* Generic load of register from stack slot. */ 280/* Generic load of register with base and (small) offset address. */
182static void emit_spload(ASMState *as, IRIns *ir, Reg r, int32_t ofs) 281static void emit_loadofs(ASMState *as, IRIns *ir, Reg r, Reg base, int32_t ofs)
183{ 282{
184 if (r < RID_MAX_GPR) 283 if (r < RID_MAX_GPR)
185 emit_tsi(as, MIPSI_LW, r, RID_SP, ofs); 284 emit_tsi(as, irt_is64(ir->t) ? MIPSI_LD : MIPSI_LW, r, base, ofs);
186 else 285 else
187 emit_tsi(as, irt_isnum(ir->t) ? MIPSI_LDC1 : MIPSI_LWC1, 286 emit_tsi(as, irt_isnum(ir->t) ? MIPSI_LDC1 : MIPSI_LWC1,
188 (r & 31), RID_SP, ofs); 287 (r & 31), base, ofs);
189} 288}
190 289
191/* Generic store of register to stack slot. */ 290/* Generic store of register with base and (small) offset address. */
192static void emit_spstore(ASMState *as, IRIns *ir, Reg r, int32_t ofs) 291static void emit_storeofs(ASMState *as, IRIns *ir, Reg r, Reg base, int32_t ofs)
193{ 292{
194 if (r < RID_MAX_GPR) 293 if (r < RID_MAX_GPR)
195 emit_tsi(as, MIPSI_SW, r, RID_SP, ofs); 294 emit_tsi(as, irt_is64(ir->t) ? MIPSI_SD : MIPSI_SW, r, base, ofs);
196 else 295 else
197 emit_tsi(as, irt_isnum(ir->t) ? MIPSI_SDC1 : MIPSI_SWC1, 296 emit_tsi(as, irt_isnum(ir->t) ? MIPSI_SDC1 : MIPSI_SWC1,
198 (r&31), RID_SP, ofs); 297 (r&31), base, ofs);
199} 298}
200 299
201/* Add offset to pointer. */ 300/* Add offset to pointer. */
202static void emit_addptr(ASMState *as, Reg r, int32_t ofs) 301static void emit_addptr(ASMState *as, Reg r, int32_t ofs)
203{ 302{
204 if (ofs) { 303 if (ofs) {
205 lua_assert(checki16(ofs)); 304 lj_assertA(checki16(ofs), "offset %d out of range", ofs);
206 emit_tsi(as, MIPSI_ADDIU, r, r, ofs); 305 emit_tsi(as, MIPSI_AADDIU, r, r, ofs);
207 } 306 }
208} 307}
209 308