diff options
author | Mike Pall <mike> | 2011-06-02 03:18:26 +0200 |
---|---|---|
committer | Mike Pall <mike> | 2011-06-02 03:18:26 +0200 |
commit | 5d82cfd091815f44cedf698cb3b3bd1ec8177430 (patch) | |
tree | c33fda3cebc45aef117f2d1fd1afa550dbba083c | |
parent | fff2fb31f9c453e38643787b76f41cfc35141c06 (diff) | |
download | luajit-5d82cfd091815f44cedf698cb3b3bd1ec8177430.tar.gz luajit-5d82cfd091815f44cedf698cb3b3bd1ec8177430.tar.bz2 luajit-5d82cfd091815f44cedf698cb3b3bd1ec8177430.zip |
ARM: Add ARM-specific assembler backend.
-rw-r--r-- | src/lj_asm.c | 4 | ||||
-rw-r--r-- | src/lj_asm_arm.h | 1763 | ||||
-rw-r--r-- | src/lj_emit_arm.h | 300 | ||||
-rw-r--r-- | src/lj_target.h | 2 | ||||
-rw-r--r-- | src/lj_target_arm.h | 205 |
5 files changed, 2274 insertions, 0 deletions
diff --git a/src/lj_asm.c b/src/lj_asm.c index 3c0575ab..145129df 100644 --- a/src/lj_asm.c +++ b/src/lj_asm.c | |||
@@ -159,6 +159,8 @@ IRFLDEF(FLOFS) | |||
159 | 159 | ||
160 | #if LJ_TARGET_X86ORX64 | 160 | #if LJ_TARGET_X86ORX64 |
161 | #include "lj_emit_x86.h" | 161 | #include "lj_emit_x86.h" |
162 | #elif LJ_TARGET_ARM | ||
163 | #include "lj_emit_arm.h" | ||
162 | #else | 164 | #else |
163 | #error "Missing instruction emitter for target CPU" | 165 | #error "Missing instruction emitter for target CPU" |
164 | #endif | 166 | #endif |
@@ -1098,6 +1100,8 @@ static void asm_loop(ASMState *as) | |||
1098 | 1100 | ||
1099 | #if LJ_TARGET_X86ORX64 | 1101 | #if LJ_TARGET_X86ORX64 |
1100 | #include "lj_asm_x86.h" | 1102 | #include "lj_asm_x86.h" |
1103 | #elif LJ_TARGET_ARM | ||
1104 | #include "lj_asm_arm.h" | ||
1101 | #else | 1105 | #else |
1102 | #error "Missing instruction emitter for target CPU" | 1106 | #error "Missing instruction emitter for target CPU" |
1103 | #endif | 1107 | #endif |
diff --git a/src/lj_asm_arm.h b/src/lj_asm_arm.h new file mode 100644 index 00000000..5890b54f --- /dev/null +++ b/src/lj_asm_arm.h | |||
@@ -0,0 +1,1763 @@ | |||
1 | /* | ||
2 | ** ARM IR assembler (SSA IR -> machine code). | ||
3 | ** Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h | ||
4 | */ | ||
5 | |||
6 | /* -- Register allocator extensions --------------------------------------- */ | ||
7 | |||
8 | /* Allocate a register with a hint. */ | ||
9 | static Reg ra_hintalloc(ASMState *as, IRRef ref, Reg hint, RegSet allow) | ||
10 | { | ||
11 | Reg r = IR(ref)->r; | ||
12 | if (ra_noreg(r)) { | ||
13 | if (!ra_hashint(r) && !iscrossref(as, ref)) | ||
14 | ra_sethint(IR(ref)->r, hint); /* Propagate register hint. */ | ||
15 | r = ra_allocref(as, ref, allow); | ||
16 | } | ||
17 | ra_noweak(as, r); | ||
18 | return r; | ||
19 | } | ||
20 | |||
21 | /* Similar to ra_left, except we override any hints. */ | ||
22 | static void ra_leftov(ASMState *as, Reg dest, IRRef lref) | ||
23 | { | ||
24 | IRIns *ir = IR(lref); | ||
25 | Reg left = ir->r; | ||
26 | if (ra_noreg(left)) { | ||
27 | ra_sethint(ir->r, dest); /* Propagate register hint. */ | ||
28 | left = ra_allocref(as, lref, RSET_GPR); | ||
29 | } | ||
30 | ra_noweak(as, left); | ||
31 | if (dest != left) { | ||
32 | /* Use register renaming if dest is the PHI reg. */ | ||
33 | if (irt_isphi(ir->t) && as->phireg[dest] == lref) { | ||
34 | ra_modified(as, left); | ||
35 | ra_rename(as, left, dest); | ||
36 | } else { | ||
37 | emit_movrr(as, ir, dest, left); | ||
38 | } | ||
39 | } | ||
40 | } | ||
41 | |||
42 | /* Allocate a scratch register pair. */ | ||
43 | static Reg ra_scratchpair(ASMState *as, RegSet allow) | ||
44 | { | ||
45 | RegSet pick1 = as->freeset & allow; | ||
46 | RegSet pick2 = pick1 & (pick1 >> 1) & RSET_GPREVEN; | ||
47 | Reg r; | ||
48 | if (pick2) { | ||
49 | r = rset_picktop(pick2); | ||
50 | } else { | ||
51 | RegSet pick = pick1 & (allow >> 1) & RSET_GPREVEN; | ||
52 | if (pick) { | ||
53 | r = rset_picktop(pick); | ||
54 | ra_restore(as, regcost_ref(as->cost[r+1])); | ||
55 | } else { | ||
56 | pick = pick1 & (allow << 1) & RSET_GPRODD; | ||
57 | if (pick) { | ||
58 | r = ra_restore(as, regcost_ref(as->cost[rset_picktop(pick)-1])); | ||
59 | } else { | ||
60 | r = ra_evict(as, allow & (allow >> 1) & RSET_GPREVEN); | ||
61 | ra_restore(as, regcost_ref(as->cost[r+1])); | ||
62 | } | ||
63 | } | ||
64 | } | ||
65 | lua_assert(rset_test(RSET_GPREVEN, r)); | ||
66 | ra_modified(as, r); | ||
67 | ra_modified(as, r+1); | ||
68 | RA_DBGX((as, "scratchpair $r $r", r, r+1)); | ||
69 | return r; | ||
70 | } | ||
71 | |||
72 | /* Force a RID_RET/RID_RETHI destination register pair (marked as free). */ | ||
73 | static void ra_destpair(ASMState *as, IRIns *ir) | ||
74 | { | ||
75 | Reg destlo = ir->r, desthi = (ir+1)->r; | ||
76 | /* First spill unrelated refs blocking the destination registers. */ | ||
77 | if (!rset_test(as->freeset, RID_RET) && | ||
78 | destlo != RID_RET && desthi != RID_RET) | ||
79 | ra_restore(as, regcost_ref(as->cost[RID_RET])); | ||
80 | if (!rset_test(as->freeset, RID_RETHI) && | ||
81 | destlo != RID_RETHI && desthi != RID_RETHI) | ||
82 | ra_restore(as, regcost_ref(as->cost[RID_RETHI])); | ||
83 | /* Next free the destination registers (if any). */ | ||
84 | if (ra_hasreg(destlo)) { | ||
85 | ra_free(as, destlo); | ||
86 | ra_modified(as, destlo); | ||
87 | } else { | ||
88 | destlo = RID_RET; | ||
89 | } | ||
90 | if (ra_hasreg(desthi)) { | ||
91 | ra_free(as, desthi); | ||
92 | ra_modified(as, desthi); | ||
93 | } else { | ||
94 | desthi = RID_RETHI; | ||
95 | } | ||
96 | /* Check for conflicts and shuffle the registers as needed. */ | ||
97 | if (destlo == RID_RETHI) { | ||
98 | if (desthi == RID_RET) { | ||
99 | emit_movrr(as, ir, RID_RETHI, RID_TMP); | ||
100 | emit_movrr(as, ir, RID_RET, RID_RETHI); | ||
101 | emit_movrr(as, ir, RID_TMP, RID_RET); | ||
102 | } else { | ||
103 | emit_movrr(as, ir, RID_RETHI, RID_RET); | ||
104 | if (desthi != RID_RETHI) emit_movrr(as, ir, desthi, RID_RETHI); | ||
105 | } | ||
106 | } else if (desthi == RID_RET) { | ||
107 | emit_movrr(as, ir, RID_RET, RID_RETHI); | ||
108 | if (destlo != RID_RET) emit_movrr(as, ir, destlo, RID_RET); | ||
109 | } else { | ||
110 | if (desthi != RID_RETHI) emit_movrr(as, ir, desthi, RID_RETHI); | ||
111 | if (destlo != RID_RET) emit_movrr(as, ir, destlo, RID_RET); | ||
112 | } | ||
113 | /* Restore spill slots (if any). */ | ||
114 | if (ra_hasspill((ir+1)->s)) ra_save(as, ir+1, RID_RETHI); | ||
115 | if (ra_hasspill(ir->s)) ra_save(as, ir, RID_RET); | ||
116 | } | ||
117 | |||
118 | /* -- Guard handling ------------------------------------------------------ */ | ||
119 | |||
120 | /* Generate an exit stub group at the bottom of the reserved MCode memory. */ | ||
121 | static MCode *asm_exitstub_gen(ASMState *as, ExitNo group) | ||
122 | { | ||
123 | MCode *mxp = as->mcbot; | ||
124 | int i; | ||
125 | if (mxp + 4*4+4*EXITSTUBS_PER_GROUP >= as->mctop) | ||
126 | asm_mclimit(as); | ||
127 | /* str lr, [sp]; bl ->vm_exit_handler; .long DISPATCH_address, group. */ | ||
128 | *mxp++ = ARMI_STR|ARMI_LS_P|ARMI_LS_U|ARMF_D(RID_LR)|ARMF_N(RID_SP); | ||
129 | *mxp = ARMI_BL|((((MCode *)(void *)lj_vm_exit_handler-mxp)-2)&0x00ffffffu); | ||
130 | mxp++; | ||
131 | *mxp++ = (MCode)i32ptr(J2GG(as->J)->dispatch); /* DISPATCH address */ | ||
132 | *mxp++ = group*EXITSTUBS_PER_GROUP; | ||
133 | for (i = 0; i < EXITSTUBS_PER_GROUP; i++) | ||
134 | *mxp++ = ARMI_B|((-6-i)&0x00ffffffu); | ||
135 | lj_mcode_commitbot(as->J, mxp); | ||
136 | as->mcbot = mxp; | ||
137 | as->mclim = as->mcbot + MCLIM_REDZONE; | ||
138 | return mxp - EXITSTUBS_PER_GROUP; | ||
139 | } | ||
140 | |||
141 | /* Setup all needed exit stubs. */ | ||
142 | static void asm_exitstub_setup(ASMState *as, ExitNo nexits) | ||
143 | { | ||
144 | ExitNo i; | ||
145 | if (nexits >= EXITSTUBS_PER_GROUP*LJ_MAX_EXITSTUBGR) | ||
146 | lj_trace_err(as->J, LJ_TRERR_SNAPOV); | ||
147 | for (i = 0; i < (nexits+EXITSTUBS_PER_GROUP-1)/EXITSTUBS_PER_GROUP; i++) | ||
148 | if (as->J->exitstubgroup[i] == NULL) | ||
149 | as->J->exitstubgroup[i] = asm_exitstub_gen(as, i); | ||
150 | } | ||
151 | |||
152 | /* Emit conditional branch to exit for guard. */ | ||
153 | static void asm_guardcc(ASMState *as, ARMCC cc) | ||
154 | { | ||
155 | MCode *target = exitstub_addr(as->J, as->snapno); | ||
156 | MCode *p = as->mcp; | ||
157 | if (LJ_UNLIKELY(p == as->invmcp)) { | ||
158 | as->loopinv = 1; | ||
159 | *p = ARMI_BL | ((target-p-2) & 0x00ffffffu); | ||
160 | emit_branch(as, ARMF_CC(ARMI_B, cc^1), p+1); | ||
161 | return; | ||
162 | } | ||
163 | emit_branch(as, ARMF_CC(ARMI_BL, cc), target); | ||
164 | } | ||
165 | |||
166 | /* -- Operand fusion ------------------------------------------------------ */ | ||
167 | |||
168 | /* Limit linear search to this distance. Avoids O(n^2) behavior. */ | ||
169 | #define CONFLICT_SEARCH_LIM 31 | ||
170 | |||
171 | /* Check if there's no conflicting instruction between curins and ref. */ | ||
172 | static int noconflict(ASMState *as, IRRef ref, IROp conflict) | ||
173 | { | ||
174 | IRIns *ir = as->ir; | ||
175 | IRRef i = as->curins; | ||
176 | if (i > ref + CONFLICT_SEARCH_LIM) | ||
177 | return 0; /* Give up, ref is too far away. */ | ||
178 | while (--i > ref) | ||
179 | if (ir[i].o == conflict) | ||
180 | return 0; /* Conflict found. */ | ||
181 | return 1; /* Ok, no conflict. */ | ||
182 | } | ||
183 | |||
184 | /* Fuse the array base of colocated arrays. */ | ||
185 | static int32_t asm_fuseabase(ASMState *as, IRRef ref) | ||
186 | { | ||
187 | IRIns *ir = IR(ref); | ||
188 | if (ir->o == IR_TNEW && ir->op1 <= LJ_MAX_COLOSIZE && | ||
189 | noconflict(as, ref, IR_NEWREF)) | ||
190 | return (int32_t)sizeof(GCtab); | ||
191 | return 0; | ||
192 | } | ||
193 | |||
194 | /* Fuse array/hash/upvalue reference into register+offset operand. */ | ||
195 | static Reg asm_fuseahuref(ASMState *as, IRRef ref, int32_t *ofsp, RegSet allow) | ||
196 | { | ||
197 | IRIns *ir = IR(ref); | ||
198 | if (ra_noreg(ir->r)) { | ||
199 | if (ir->o == IR_AREF) { | ||
200 | if (mayfuse(as, ref)) { | ||
201 | if (irref_isk(ir->op2)) { | ||
202 | IRRef tab = IR(ir->op1)->op1; | ||
203 | int32_t ofs = asm_fuseabase(as, tab); | ||
204 | IRRef refa = ofs ? tab : ir->op1; | ||
205 | ofs += 8*IR(ir->op2)->i; | ||
206 | if (ofs > -4096 && ofs < 4096) { | ||
207 | *ofsp = ofs; | ||
208 | return ra_alloc1(as, refa, allow); | ||
209 | } | ||
210 | } | ||
211 | } | ||
212 | } else if (ir->o == IR_HREFK) { | ||
213 | if (mayfuse(as, ref)) { | ||
214 | int32_t ofs = (int32_t)(IR(ir->op2)->op2 * sizeof(Node)); | ||
215 | if (ofs < 4096) { | ||
216 | *ofsp = ofs; | ||
217 | return ra_alloc1(as, ir->op1, allow); | ||
218 | } | ||
219 | } | ||
220 | } else if (ir->o == IR_UREFC) { | ||
221 | if (irref_isk(ir->op1)) { | ||
222 | GCfunc *fn = ir_kfunc(IR(ir->op1)); | ||
223 | int32_t ofs = i32ptr(&gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.tv); | ||
224 | *ofsp = (ofs & 255); /* Mask out less bits to allow LDRD. */ | ||
225 | return ra_allock(as, (ofs & ~255), allow); | ||
226 | } | ||
227 | } | ||
228 | } | ||
229 | *ofsp = 0; | ||
230 | return ra_alloc1(as, ref, allow); | ||
231 | } | ||
232 | |||
233 | /* Fuse m operand into arithmetic/logic instructions. */ | ||
234 | static uint32_t asm_fuseopm(ASMState *as, ARMIns ai, IRRef ref, RegSet allow) | ||
235 | { | ||
236 | IRIns *ir = IR(ref); | ||
237 | if (ra_hasreg(ir->r)) { | ||
238 | ra_noweak(as, ir->r); | ||
239 | return ARMF_M(ir->r); | ||
240 | } else if (irref_isk(ref)) { | ||
241 | uint32_t k = emit_isk12(ai, ir->i); | ||
242 | if (k) | ||
243 | return k; | ||
244 | } else if (mayfuse(as, ref)) { | ||
245 | if (ir->o >= IR_BSHL && ir->o <= IR_BROR) { | ||
246 | Reg m = ra_alloc1(as, ir->op1, allow); | ||
247 | ARMShift sh = ir->o == IR_BSHL ? ARMSH_LSL : | ||
248 | ir->o == IR_BSHR ? ARMSH_LSR : | ||
249 | ir->o == IR_BSAR ? ARMSH_ASR : ARMSH_ROR; | ||
250 | if (irref_isk(ir->op2)) { | ||
251 | return m | ARMF_SH(sh, (IR(ir->op2)->i & 31)); | ||
252 | } else { | ||
253 | Reg s = ra_alloc1(as, ir->op2, rset_exclude(allow, m)); | ||
254 | return m | ARMF_RSH(sh, s); | ||
255 | } | ||
256 | } else if (ir->o == IR_ADD && ir->op1 == ir->op2) { | ||
257 | Reg m = ra_alloc1(as, ir->op1, allow); | ||
258 | return m | ARMF_SH(ARMSH_LSL, 1); | ||
259 | } | ||
260 | } | ||
261 | return ra_allocref(as, ref, allow); | ||
262 | } | ||
263 | |||
264 | /* -- Calls --------------------------------------------------------------- */ | ||
265 | |||
266 | /* Generate a call to a C function. */ | ||
267 | static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args) | ||
268 | { | ||
269 | uint32_t n, nargs = CCI_NARGS(ci); | ||
270 | int32_t ofs = 0; | ||
271 | Reg gpr = REGARG_FIRSTGPR; | ||
272 | if ((void *)ci->func) | ||
273 | emit_call(as, (void *)ci->func); | ||
274 | for (n = 0; n < nargs; n++) { /* Setup args. */ | ||
275 | IRRef ref = args[n]; | ||
276 | IRIns *ir = IR(ref); | ||
277 | if (gpr <= REGARG_LASTGPR) { | ||
278 | lua_assert(rset_test(as->freeset, gpr)); /* Must have been evicted. */ | ||
279 | if (ref) ra_leftov(as, gpr, ref); | ||
280 | gpr++; | ||
281 | } else { | ||
282 | if (ref) { | ||
283 | Reg r = ra_alloc1(as, ref, RSET_GPR); | ||
284 | emit_spstore(as, ir, r, ofs); | ||
285 | } | ||
286 | ofs += 4; | ||
287 | } | ||
288 | } | ||
289 | } | ||
290 | |||
291 | /* Setup result reg/sp for call. Evict scratch regs. */ | ||
292 | static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci) | ||
293 | { | ||
294 | RegSet drop = RSET_SCRATCH; | ||
295 | int hiop = ((ir+1)->o == IR_HIOP); | ||
296 | if (ra_hasreg(ir->r)) | ||
297 | rset_clear(drop, ir->r); /* Dest reg handled below. */ | ||
298 | if (hiop && ra_hasreg((ir+1)->r)) | ||
299 | rset_clear(drop, (ir+1)->r); /* Dest reg handled below. */ | ||
300 | ra_evictset(as, drop); /* Evictions must be performed first. */ | ||
301 | if (ra_used(ir)) { | ||
302 | lua_assert(!irt_ispri(ir->t)); | ||
303 | if (hiop) | ||
304 | ra_destpair(as, ir); | ||
305 | else | ||
306 | ra_destreg(as, ir, RID_RET); | ||
307 | } | ||
308 | UNUSED(ci); | ||
309 | } | ||
310 | |||
311 | static void asm_call(ASMState *as, IRIns *ir) | ||
312 | { | ||
313 | IRRef args[CCI_NARGS_MAX]; | ||
314 | const CCallInfo *ci = &lj_ir_callinfo[ir->op2]; | ||
315 | asm_collectargs(as, ir, ci, args); | ||
316 | asm_setupresult(as, ir, ci); | ||
317 | asm_gencall(as, ci, args); | ||
318 | } | ||
319 | |||
320 | static void asm_callx(ASMState *as, IRIns *ir) | ||
321 | { | ||
322 | IRRef args[CCI_NARGS_MAX]; | ||
323 | CCallInfo ci; | ||
324 | ci.flags = asm_callx_flags(as, ir); | ||
325 | asm_collectargs(as, ir, &ci, args); | ||
326 | asm_setupresult(as, ir, &ci); | ||
327 | if (irref_isk(ir->op2)) { /* Call to constant address. */ | ||
328 | ci.func = (ASMFunction)(void *)(IR(ir->op2)->i); | ||
329 | } else { /* Need a non-argument register for indirect calls. */ | ||
330 | Reg freg = ra_alloc1(as, ir->op2, RSET_RANGE(RID_R4, RID_R12+1)); | ||
331 | emit_m(as, ARMI_BLXr, freg); | ||
332 | ci.func = (ASMFunction)(void *)0; | ||
333 | } | ||
334 | asm_gencall(as, &ci, args); | ||
335 | } | ||
336 | |||
337 | /* -- Returns ------------------------------------------------------------- */ | ||
338 | |||
339 | /* Return to lower frame. Guard that it goes to the right spot. */ | ||
340 | static void asm_retf(ASMState *as, IRIns *ir) | ||
341 | { | ||
342 | Reg base = ra_alloc1(as, REF_BASE, RSET_GPR); | ||
343 | void *pc = ir_kptr(IR(ir->op2)); | ||
344 | int32_t delta = 1+bc_a(*((const BCIns *)pc - 1)); | ||
345 | as->topslot -= (BCReg)delta; | ||
346 | if ((int32_t)as->topslot < 0) as->topslot = 0; | ||
347 | /* Need to force a spill on REF_BASE now to update the stack slot. */ | ||
348 | emit_lso(as, ARMI_STR, base, RID_SP, ra_spill(as, IR(REF_BASE))); | ||
349 | emit_setgl(as, base, jit_base); | ||
350 | emit_addptr(as, base, -8*delta); | ||
351 | asm_guardcc(as, CC_NE); | ||
352 | emit_nm(as, ARMI_CMP, RID_TMP, | ||
353 | ra_allock(as, i32ptr(pc), rset_exclude(RSET_GPR, base))); | ||
354 | emit_lso(as, ARMI_LDR, RID_TMP, base, -4); | ||
355 | } | ||
356 | |||
357 | /* -- Type conversions ---------------------------------------------------- */ | ||
358 | |||
359 | static void asm_conv(ASMState *as, IRIns *ir) | ||
360 | { | ||
361 | Reg dest = ra_dest(as, ir, RSET_GPR); | ||
362 | IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK); | ||
363 | /* FP conversions and 64 bit integer conversions are handled by SPLIT. */ | ||
364 | lua_assert(!irt_isfp(ir->t) && !(st == IRT_NUM || st == IRT_FLOAT)); | ||
365 | lua_assert(!irt_isint64(ir->t) && !(st == IRT_I64 || st == IRT_U64)); | ||
366 | if (st >= IRT_I8 && st <= IRT_U16) { /* Extend to 32 bit integer. */ | ||
367 | Reg left = ra_alloc1(as, ir->op1, RSET_GPR); | ||
368 | lua_assert(irt_isint(ir->t) || irt_isu32(ir->t)); | ||
369 | if ((as->flags & JIT_F_ARMV6)) { | ||
370 | ARMIns ai = st == IRT_I8 ? ARMI_SXTB : | ||
371 | st == IRT_U8 ? ARMI_UXTB : | ||
372 | st == IRT_I16 ? ARMI_SXTH : ARMI_UXTH; | ||
373 | emit_dm(as, ai, dest, left); | ||
374 | } else if (st == IRT_U8) { | ||
375 | emit_dn(as, ARMI_AND|ARMI_K12|255, dest, left); | ||
376 | } else { | ||
377 | uint32_t shift = st == IRT_I8 ? 24 : 16; | ||
378 | ARMShift sh = st == IRT_U16 ? ARMSH_LSR : ARMSH_ASR; | ||
379 | emit_dm(as, ARMI_MOV|ARMF_SH(sh, shift), dest, RID_TMP); | ||
380 | emit_dm(as, ARMI_MOV|ARMF_SH(ARMSH_LSL, shift), RID_TMP, left); | ||
381 | } | ||
382 | } else { /* Handle 32/32 bit no-op (cast). */ | ||
383 | ra_leftov(as, dest, ir->op1); /* Do nothing, but may need to move regs. */ | ||
384 | } | ||
385 | } | ||
386 | |||
387 | static void asm_strto(ASMState *as, IRIns *ir) | ||
388 | { | ||
389 | const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_tonum]; | ||
390 | IRRef args[2]; | ||
391 | Reg rlo = 0, rhi = 0, tmp; | ||
392 | int destused = ra_used(ir); | ||
393 | int32_t ofs = 0; | ||
394 | ra_evictset(as, RSET_SCRATCH); | ||
395 | if (destused) { | ||
396 | if (ra_hasspill(ir->s) && ra_hasspill((ir+1)->s) && | ||
397 | (ir->s & 1) == 0 && ir->s + 1 == (ir+1)->s) { | ||
398 | int i; | ||
399 | for (i = 0; i < 2; i++) { | ||
400 | Reg r = (ir+i)->r; | ||
401 | if (ra_hasreg(r)) { | ||
402 | ra_free(as, r); | ||
403 | ra_modified(as, r); | ||
404 | emit_spload(as, ir+i, r, sps_scale((ir+i)->s)); | ||
405 | } | ||
406 | } | ||
407 | ofs = sps_scale(ir->s); | ||
408 | destused = 0; | ||
409 | } else { | ||
410 | rhi = ra_dest(as, ir+1, RSET_GPR); | ||
411 | rlo = ra_dest(as, ir, rset_exclude(RSET_GPR, rhi)); | ||
412 | } | ||
413 | } | ||
414 | asm_guardcc(as, CC_EQ); | ||
415 | if (destused) { | ||
416 | emit_lso(as, ARMI_LDR, rhi, RID_SP, 4); | ||
417 | emit_lso(as, ARMI_LDR, rlo, RID_SP, 0); | ||
418 | } | ||
419 | emit_n(as, ARMI_CMP|ARMI_K12|0, RID_RET); /* Test return status. */ | ||
420 | args[0] = ir->op1; /* GCstr *str */ | ||
421 | args[1] = ASMREF_TMP1; /* TValue *n */ | ||
422 | asm_gencall(as, ci, args); | ||
423 | tmp = ra_releasetmp(as, ASMREF_TMP1); | ||
424 | if (ofs == 0) | ||
425 | emit_dm(as, ARMI_MOV, tmp, RID_SP); | ||
426 | else | ||
427 | emit_opk(as, ARMI_ADD, tmp, RID_SP, ofs, RSET_GPR); | ||
428 | } | ||
429 | |||
430 | /* Get pointer to TValue. */ | ||
431 | static void asm_tvptr(ASMState *as, Reg dest, IRRef ref) | ||
432 | { | ||
433 | IRIns *ir = IR(ref); | ||
434 | if (irt_isnum(ir->t)) { /* Use the number constant itself as a TValue. */ | ||
435 | lua_assert(irref_isk(ref)); | ||
436 | ra_allockreg(as, i32ptr(ir_knum(ir)), dest); | ||
437 | } else { | ||
438 | /* Otherwise use [sp] and [sp+4] to hold the TValue. */ | ||
439 | RegSet allow = rset_exclude(RSET_GPR, dest); | ||
440 | Reg type; | ||
441 | emit_dm(as, ARMI_MOV, dest, RID_SP); | ||
442 | if (!irt_ispri(ir->t)) { | ||
443 | Reg src = ra_alloc1(as, ref, allow); | ||
444 | emit_lso(as, ARMI_STR, src, RID_SP, 0); | ||
445 | } | ||
446 | if ((ir+1)->o == IR_HIOP) | ||
447 | type = ra_alloc1(as, ref+1, allow); | ||
448 | else | ||
449 | type = ra_allock(as, irt_toitype(ir->t), allow); | ||
450 | emit_lso(as, ARMI_STR, type, RID_SP, 4); | ||
451 | } | ||
452 | } | ||
453 | |||
454 | static void asm_tostr(ASMState *as, IRIns *ir) | ||
455 | { | ||
456 | IRRef args[2]; | ||
457 | args[0] = ASMREF_L; | ||
458 | as->gcsteps++; | ||
459 | if (irt_isnum(IR(ir->op1)->t) || (ir+1)->o == IR_HIOP) { | ||
460 | const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_fromnum]; | ||
461 | args[1] = ASMREF_TMP1; /* const lua_Number * */ | ||
462 | asm_setupresult(as, ir, ci); /* GCstr * */ | ||
463 | asm_gencall(as, ci, args); | ||
464 | asm_tvptr(as, ra_releasetmp(as, ASMREF_TMP1), ir->op1); | ||
465 | } else { | ||
466 | const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_fromint]; | ||
467 | args[1] = ir->op1; /* int32_t k */ | ||
468 | asm_setupresult(as, ir, ci); /* GCstr * */ | ||
469 | asm_gencall(as, ci, args); | ||
470 | } | ||
471 | } | ||
472 | |||
473 | /* -- Memory references --------------------------------------------------- */ | ||
474 | |||
475 | static void asm_aref(ASMState *as, IRIns *ir) | ||
476 | { | ||
477 | Reg dest = ra_dest(as, ir, RSET_GPR); | ||
478 | Reg idx, base; | ||
479 | if (irref_isk(ir->op2)) { | ||
480 | IRRef tab = IR(ir->op1)->op1; | ||
481 | int32_t ofs = asm_fuseabase(as, tab); | ||
482 | IRRef refa = ofs ? tab : ir->op1; | ||
483 | uint32_t k = emit_isk12(ARMI_ADD, ofs + 8*IR(ir->op2)->i); | ||
484 | if (k) { | ||
485 | base = ra_alloc1(as, refa, RSET_GPR); | ||
486 | emit_dn(as, ARMI_ADD^k, dest, base); | ||
487 | return; | ||
488 | } | ||
489 | } | ||
490 | base = ra_alloc1(as, ir->op1, RSET_GPR); | ||
491 | idx = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, base)); | ||
492 | emit_dnm(as, ARMI_ADD|ARMF_SH(ARMSH_LSL, 3), dest, base, idx); | ||
493 | } | ||
494 | |||
495 | /* Inlined hash lookup. Specialized for key type and for const keys. | ||
496 | ** The equivalent C code is: | ||
497 | ** Node *n = hashkey(t, key); | ||
498 | ** do { | ||
499 | ** if (lj_obj_equal(&n->key, key)) return &n->val; | ||
500 | ** } while ((n = nextnode(n))); | ||
501 | ** return niltv(L); | ||
502 | */ | ||
503 | static void asm_href(ASMState *as, IRIns *ir, IROp merge) | ||
504 | { | ||
505 | RegSet allow = RSET_GPR; | ||
506 | int destused = ra_used(ir); | ||
507 | Reg dest = ra_dest(as, ir, allow); | ||
508 | Reg tab = ra_alloc1(as, ir->op1, rset_clear(allow, dest)); | ||
509 | Reg key = 0, keyhi = 0, keynumhi = RID_NONE, tmp = RID_LR; | ||
510 | IRRef refkey = ir->op2; | ||
511 | IRIns *irkey = IR(refkey); | ||
512 | IRType1 kt = irkey->t; | ||
513 | int32_t k = 0, khi = emit_isk12(ARMI_CMP, irt_toitype(kt)); | ||
514 | uint32_t khash; | ||
515 | MCLabel l_end, l_loop; | ||
516 | rset_clear(allow, tab); | ||
517 | if (!irref_isk(refkey) || irt_isstr(kt)) { | ||
518 | key = ra_alloc1(as, refkey, allow); | ||
519 | rset_clear(allow, key); | ||
520 | if (irkey[1].o == IR_HIOP) { | ||
521 | if (ra_hasreg((irkey+1)->r)) { | ||
522 | keynumhi = (irkey+1)->r; | ||
523 | keyhi = RID_TMP; | ||
524 | ra_noweak(as, keynumhi); | ||
525 | } else { | ||
526 | keyhi = keynumhi = ra_allocref(as, refkey+1, allow); | ||
527 | } | ||
528 | rset_clear(allow, keynumhi); | ||
529 | khi = 0; | ||
530 | } | ||
531 | } else if (irt_isnum(kt)) { | ||
532 | int32_t val = (int32_t)ir_knum(irkey)->u32.lo; | ||
533 | k = emit_isk12(ARMI_CMP, val); | ||
534 | if (!k) { | ||
535 | key = ra_allock(as, val, allow); | ||
536 | rset_clear(allow, key); | ||
537 | } | ||
538 | val = (int32_t)ir_knum(irkey)->u32.hi; | ||
539 | khi = emit_isk12(ARMI_CMP, val); | ||
540 | if (!khi) { | ||
541 | keyhi = ra_allock(as, val, allow); | ||
542 | rset_clear(allow, keyhi); | ||
543 | } | ||
544 | } else if (!irt_ispri(kt)) { | ||
545 | k = emit_isk12(ARMI_CMP, irkey->i); | ||
546 | if (!k) { | ||
547 | key = ra_alloc1(as, refkey, allow); | ||
548 | rset_clear(allow, key); | ||
549 | } | ||
550 | } | ||
551 | if (!irt_ispri(kt)) | ||
552 | tmp = ra_scratchpair(as, allow); | ||
553 | |||
554 | /* Key not found in chain: jump to exit (if merged) or load niltv. */ | ||
555 | l_end = emit_label(as); | ||
556 | as->invmcp = NULL; | ||
557 | if (merge == IR_NE) | ||
558 | asm_guardcc(as, CC_AL); | ||
559 | else if (destused) | ||
560 | emit_loada(as, dest, niltvg(J2G(as->J))); | ||
561 | |||
562 | /* Follow hash chain until the end. */ | ||
563 | l_loop = --as->mcp; | ||
564 | emit_n(as, ARMI_CMP|ARMI_K12|0, dest); | ||
565 | emit_lso(as, ARMI_LDR, dest, dest, (int32_t)offsetof(Node, next)); | ||
566 | |||
567 | /* Type and value comparison. */ | ||
568 | if (merge == IR_EQ) | ||
569 | asm_guardcc(as, CC_EQ); | ||
570 | else | ||
571 | emit_branch(as, ARMF_CC(ARMI_B, CC_EQ), l_end); | ||
572 | if (!irt_ispri(kt)) { | ||
573 | emit_nm(as, ARMF_CC(ARMI_CMP, CC_EQ)^khi, tmp+1, keyhi); | ||
574 | emit_nm(as, ARMI_CMP^k, tmp, key); | ||
575 | emit_lsox(as, ARMI_LDRD, tmp, dest, (int32_t)offsetof(Node, key)); | ||
576 | } else { | ||
577 | emit_n(as, ARMI_CMP^khi, tmp); | ||
578 | emit_lso(as, ARMI_LDR, tmp, dest, (int32_t)offsetof(Node, key.it)); | ||
579 | } | ||
580 | *l_loop = ARMF_CC(ARMI_B, CC_NE) | ((as->mcp-l_loop-2) & 0x00ffffffu); | ||
581 | |||
582 | /* Load main position relative to tab->node into dest. */ | ||
583 | khash = irref_isk(refkey) ? ir_khash(irkey) : 1; | ||
584 | if (khash == 0) { | ||
585 | emit_lso(as, ARMI_LDR, dest, tab, (int32_t)offsetof(GCtab, node)); | ||
586 | } else { | ||
587 | emit_dnm(as, ARMI_ADD|ARMF_SH(ARMSH_LSL, 3), dest, dest, tmp); | ||
588 | emit_dnm(as, ARMI_ADD|ARMF_SH(ARMSH_LSL, 1), tmp, tmp, tmp); | ||
589 | if (irt_isstr(kt)) { /* Fetch of str->hash is cheaper than ra_allock. */ | ||
590 | emit_dnm(as, ARMI_AND, tmp, tmp+1, RID_TMP); | ||
591 | emit_lso(as, ARMI_LDR, dest, tab, (int32_t)offsetof(GCtab, node)); | ||
592 | emit_lso(as, ARMI_LDR, tmp+1, key, (int32_t)offsetof(GCstr, hash)); | ||
593 | emit_lso(as, ARMI_LDR, RID_TMP, tab, (int32_t)offsetof(GCtab, hmask)); | ||
594 | } else if (irref_isk(refkey)) { | ||
595 | emit_opk(as, ARMI_AND, tmp, RID_TMP, (int32_t)khash, | ||
596 | rset_exclude(rset_exclude(RSET_GPR, tab), dest)); | ||
597 | emit_lso(as, ARMI_LDR, dest, tab, (int32_t)offsetof(GCtab, node)); | ||
598 | emit_lso(as, ARMI_LDR, RID_TMP, tab, (int32_t)offsetof(GCtab, hmask)); | ||
599 | } else { /* Must match with hash*() in lj_tab.c. */ | ||
600 | if (ra_hasreg(keynumhi)) { /* Canonicalize +-0.0 to 0.0. */ | ||
601 | if (keyhi == RID_TMP) | ||
602 | emit_dm(as, ARMF_CC(ARMI_MOV, CC_NE), keyhi, keynumhi); | ||
603 | emit_d(as, ARMF_CC(ARMI_MOV, CC_EQ)|ARMI_K12|0, keyhi); | ||
604 | } | ||
605 | emit_dnm(as, ARMI_AND, tmp, tmp, RID_TMP); | ||
606 | emit_dnm(as, ARMI_SUB|ARMF_SH(ARMSH_ROR, 32-HASH_ROT3), tmp, tmp, tmp+1); | ||
607 | emit_lso(as, ARMI_LDR, dest, tab, (int32_t)offsetof(GCtab, node)); | ||
608 | emit_dnm(as, ARMI_EOR|ARMF_SH(ARMSH_ROR, 32-((HASH_ROT2+HASH_ROT1)&31)), | ||
609 | tmp, tmp+1, tmp); | ||
610 | emit_lso(as, ARMI_LDR, RID_TMP, tab, (int32_t)offsetof(GCtab, hmask)); | ||
611 | emit_dnm(as, ARMI_SUB|ARMF_SH(ARMSH_ROR, 32-HASH_ROT1), tmp+1, tmp+1, tmp); | ||
612 | if (ra_hasreg(keynumhi)) { | ||
613 | emit_dnm(as, ARMI_EOR, tmp+1, tmp, key); | ||
614 | emit_dnm(as, ARMI_ORR|ARMI_S, RID_TMP, tmp, key); /* Test for +-0.0. */ | ||
615 | emit_dnm(as, ARMI_ADD, tmp, keynumhi, keynumhi); | ||
616 | } else { | ||
617 | emit_dnm(as, ARMI_EOR, tmp+1, tmp, key); | ||
618 | emit_opk(as, ARMI_ADD, tmp, key, (int32_t)HASH_BIAS, | ||
619 | rset_exclude(rset_exclude(RSET_GPR, tab), key)); | ||
620 | } | ||
621 | } | ||
622 | } | ||
623 | } | ||
624 | |||
625 | static void asm_hrefk(ASMState *as, IRIns *ir) | ||
626 | { | ||
627 | IRIns *kslot = IR(ir->op2); | ||
628 | IRIns *irkey = IR(kslot->op1); | ||
629 | int32_t ofs = (int32_t)(kslot->op2 * sizeof(Node)); | ||
630 | int32_t kofs = ofs + (int32_t)offsetof(Node, key); | ||
631 | Reg dest = (ra_used(ir) || ofs > 4095) ? ra_dest(as, ir, RSET_GPR) : RID_NONE; | ||
632 | Reg node = ra_alloc1(as, ir->op1, RSET_GPR); | ||
633 | Reg key = RID_NONE, type = RID_TMP, idx = node; | ||
634 | RegSet allow = rset_exclude(RSET_GPR, node); | ||
635 | lua_assert(ofs % sizeof(Node) == 0); | ||
636 | if (ofs > 4095) { | ||
637 | idx = dest; | ||
638 | rset_clear(allow, dest); | ||
639 | kofs = (int32_t)offsetof(Node, key); | ||
640 | } else if (ra_hasreg(dest)) { | ||
641 | emit_opk(as, ARMI_ADD, dest, node, ofs, allow); | ||
642 | } | ||
643 | asm_guardcc(as, CC_NE); | ||
644 | if (!irt_ispri(irkey->t)) { | ||
645 | RegSet even = (as->freeset & (as->freeset >> 1) & allow & RSET_GPREVEN); | ||
646 | if (even) { | ||
647 | key = ra_scratch(as, even); | ||
648 | if (rset_test(as->freeset, key+1)) { | ||
649 | type = key+1; | ||
650 | ra_modified(as, type); | ||
651 | } | ||
652 | } else { | ||
653 | key = ra_scratch(as, allow); | ||
654 | } | ||
655 | rset_clear(allow, key); | ||
656 | } | ||
657 | rset_clear(allow, type); | ||
658 | if (irt_isnum(irkey->t)) { | ||
659 | emit_opk(as, ARMF_CC(ARMI_CMP, CC_EQ), 0, type, | ||
660 | (int32_t)ir_knum(irkey)->u32.hi, allow); | ||
661 | emit_opk(as, ARMI_CMP, 0, key, | ||
662 | (int32_t)ir_knum(irkey)->u32.lo, allow); | ||
663 | } if (ra_hasreg(key)) { | ||
664 | emit_n(as, ARMF_CC(ARMI_CMN, CC_EQ)|ARMI_K12|-irt_toitype(irkey->t), type); | ||
665 | emit_opk(as, ARMI_CMP, 0, key, irkey->i, allow); | ||
666 | } else { | ||
667 | emit_n(as, ARMI_CMN|ARMI_K12|-irt_toitype(irkey->t), type); | ||
668 | } | ||
669 | emit_lso(as, ARMI_LDR, type, idx, kofs+4); | ||
670 | if (ra_hasreg(key)) emit_lso(as, ARMI_LDR, key, idx, kofs); | ||
671 | if (ofs > 4095) | ||
672 | emit_opk(as, ARMI_ADD, dest, node, ofs, RSET_GPR); | ||
673 | } | ||
674 | |||
675 | static void asm_newref(ASMState *as, IRIns *ir) | ||
676 | { | ||
677 | const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_tab_newkey]; | ||
678 | IRRef args[3]; | ||
679 | args[0] = ASMREF_L; /* lua_State *L */ | ||
680 | args[1] = ir->op1; /* GCtab *t */ | ||
681 | args[2] = ASMREF_TMP1; /* cTValue *key */ | ||
682 | asm_setupresult(as, ir, ci); /* TValue * */ | ||
683 | asm_gencall(as, ci, args); | ||
684 | asm_tvptr(as, ra_releasetmp(as, ASMREF_TMP1), ir->op2); | ||
685 | } | ||
686 | |||
687 | static void asm_uref(ASMState *as, IRIns *ir) | ||
688 | { | ||
689 | /* NYI: Check that UREFO is still open and not aliasing a slot. */ | ||
690 | Reg dest = ra_dest(as, ir, RSET_GPR); | ||
691 | if (irref_isk(ir->op1)) { | ||
692 | GCfunc *fn = ir_kfunc(IR(ir->op1)); | ||
693 | MRef *v = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.v; | ||
694 | emit_lsptr(as, ARMI_LDR, dest, v); | ||
695 | } else { | ||
696 | Reg uv = ra_scratch(as, RSET_GPR); | ||
697 | Reg func = ra_alloc1(as, ir->op1, RSET_GPR); | ||
698 | if (ir->o == IR_UREFC) { | ||
699 | asm_guardcc(as, CC_NE); | ||
700 | emit_n(as, ARMI_CMP|ARMI_K12|1, RID_TMP); | ||
701 | emit_opk(as, ARMI_ADD, dest, uv, | ||
702 | (int32_t)offsetof(GCupval, tv), RSET_GPR); | ||
703 | emit_lso(as, ARMI_LDRB, RID_TMP, uv, (int32_t)offsetof(GCupval, closed)); | ||
704 | } else { | ||
705 | emit_lso(as, ARMI_LDR, dest, uv, (int32_t)offsetof(GCupval, v)); | ||
706 | } | ||
707 | emit_lso(as, ARMI_LDR, uv, func, | ||
708 | (int32_t)offsetof(GCfuncL, uvptr) + 4*(int32_t)(ir->op2 >> 8)); | ||
709 | } | ||
710 | } | ||
711 | |||
712 | static void asm_fref(ASMState *as, IRIns *ir) | ||
713 | { | ||
714 | UNUSED(as); UNUSED(ir); | ||
715 | lua_assert(!ra_used(ir)); | ||
716 | } | ||
717 | |||
718 | static void asm_strref(ASMState *as, IRIns *ir) | ||
719 | { | ||
720 | Reg dest = ra_dest(as, ir, RSET_GPR); | ||
721 | IRRef ref = ir->op2, refk = ir->op1; | ||
722 | Reg r; | ||
723 | if (irref_isk(ref)) { | ||
724 | IRRef tmp = refk; refk = ref; ref = tmp; | ||
725 | } else if (!irref_isk(refk)) { | ||
726 | uint32_t k, m = ARMI_K12|sizeof(GCstr); | ||
727 | Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR); | ||
728 | IRIns *irr = IR(ir->op2); | ||
729 | if (ra_hasreg(irr->r)) { | ||
730 | ra_noweak(as, irr->r); | ||
731 | right = irr->r; | ||
732 | } else if (mayfuse(as, irr->op2) && | ||
733 | irr->o == IR_ADD && irref_isk(irr->op2) && | ||
734 | (k = emit_isk12(ARMI_ADD, | ||
735 | (int32_t)sizeof(GCstr) + IR(irr->op2)->i))) { | ||
736 | m = k; | ||
737 | right = ra_alloc1(as, irr->op1, rset_exclude(RSET_GPR, left)); | ||
738 | } else { | ||
739 | right = ra_allocref(as, ir->op2, rset_exclude(RSET_GPR, left)); | ||
740 | } | ||
741 | emit_dn(as, ARMI_ADD^m, dest, dest); | ||
742 | emit_dnm(as, ARMI_ADD, dest, left, right); | ||
743 | return; | ||
744 | } | ||
745 | r = ra_alloc1(as, ref, RSET_GPR); | ||
746 | emit_opk(as, ARMI_ADD, dest, r, | ||
747 | sizeof(GCstr) + IR(refk)->i, rset_exclude(RSET_GPR, r)); | ||
748 | } | ||
749 | |||
750 | /* -- Loads and stores ---------------------------------------------------- */ | ||
751 | |||
752 | static void asm_fxload(ASMState *as, IRIns *ir) | ||
753 | { | ||
754 | Reg idx, dest = ra_dest(as, ir, RSET_GPR); | ||
755 | int32_t ofs; | ||
756 | ARMIns ai; | ||
757 | if (ir->o == IR_FLOAD) { | ||
758 | idx = ra_alloc1(as, ir->op1, RSET_GPR); | ||
759 | if (ir->op2 == IRFL_TAB_ARRAY) { | ||
760 | ofs = asm_fuseabase(as, ir->op1); | ||
761 | if (ofs) { /* Turn the t->array load into an add for colocated arrays. */ | ||
762 | emit_dn(as, ARMI_ADD|ARMI_K12|ofs, dest, idx); | ||
763 | return; | ||
764 | } | ||
765 | } | ||
766 | ofs = field_ofs[ir->op2]; | ||
767 | } else { | ||
768 | /* NYI: Fuse xload operands. */ | ||
769 | lua_assert(!(ir->op2 & IRXLOAD_UNALIGNED)); | ||
770 | idx = ra_alloc1(as, ir->op1, RSET_GPR); | ||
771 | ofs = 0; | ||
772 | } | ||
773 | switch (irt_type(ir->t)) { | ||
774 | case IRT_I8: ai = ARMI_LDRSB; break; | ||
775 | case IRT_U8: ai = ARMI_LDRB; goto use_lso; | ||
776 | case IRT_I16: ai = ARMI_LDRSH; break; | ||
777 | case IRT_U16: ai = ARMI_LDRH; break; | ||
778 | case IRT_NUM: lua_assert(0); | ||
779 | case IRT_FLOAT: | ||
780 | default: ai = ARMI_LDR; | ||
781 | use_lso: | ||
782 | emit_lso(as, ai, dest, idx, ofs); | ||
783 | return; | ||
784 | } | ||
785 | emit_lsox(as, ai, dest, idx, ofs); | ||
786 | } | ||
787 | |||
788 | static void asm_fxstore(ASMState *as, IRIns *ir) | ||
789 | { | ||
790 | Reg idx, src = ra_alloc1(as, ir->op2, RSET_GPR); | ||
791 | RegSet allow = rset_exclude(RSET_GPR, src); | ||
792 | int32_t ofs; | ||
793 | ARMIns ai; | ||
794 | if (ir->o == IR_FSTORE) { | ||
795 | IRIns *irf = IR(ir->op1); | ||
796 | idx = ra_alloc1(as, irf->op1, allow); | ||
797 | ofs = field_ofs[irf->op2]; | ||
798 | } else { | ||
799 | /* NYI: Fuse xstore operands. */ | ||
800 | idx = ra_alloc1(as, ir->op1, allow); | ||
801 | ofs = 0; | ||
802 | } | ||
803 | switch (irt_type(ir->t)) { | ||
804 | case IRT_I8: case IRT_U8: ai = ARMI_STRB; goto use_lso; | ||
805 | case IRT_I16: case IRT_U16: ai = ARMI_STRH; break; | ||
806 | case IRT_NUM: lua_assert(0); | ||
807 | case IRT_FLOAT: | ||
808 | default: ai = ARMI_STR; | ||
809 | use_lso: | ||
810 | emit_lso(as, ai, src, idx, ofs); | ||
811 | return; | ||
812 | } | ||
813 | emit_lsox(as, ai, src, idx, ofs); | ||
814 | } | ||
815 | |||
816 | static void asm_ahuvload(ASMState *as, IRIns *ir) | ||
817 | { | ||
818 | int hiop = ((ir+1)->o == IR_HIOP); | ||
819 | IRType t = hiop ? IRT_NUM : irt_type(ir->t); | ||
820 | Reg dest = RID_NONE, type = RID_NONE, idx; | ||
821 | RegSet allow = RSET_GPR; | ||
822 | int32_t ofs = 0; | ||
823 | if (hiop && ra_used(ir+1)) { | ||
824 | type = ra_dest(as, ir+1, allow); | ||
825 | rset_clear(allow, type); | ||
826 | } | ||
827 | if (ra_used(ir)) { | ||
828 | lua_assert(irt_isint(ir->t) || irt_isaddr(ir->t)); | ||
829 | dest = ra_dest(as, ir, allow); | ||
830 | rset_clear(allow, dest); | ||
831 | } | ||
832 | idx = asm_fuseahuref(as, ir->op1, &ofs, allow); | ||
833 | if (!hiop) { | ||
834 | rset_clear(allow, idx); | ||
835 | if (ofs < 256 && ra_hasreg(dest) && (dest & 1) == 0 && | ||
836 | rset_test((as->freeset & allow), dest+1)) { | ||
837 | type = dest+1; | ||
838 | ra_modified(as, type); | ||
839 | } else { | ||
840 | type = RID_TMP; | ||
841 | } | ||
842 | } | ||
843 | asm_guardcc(as, t == IRT_NUM ? CC_HS : CC_NE); | ||
844 | emit_n(as, ARMI_CMN|ARMI_K12|-irt_toitype_(t), type); | ||
845 | if (ra_hasreg(dest)) emit_lso(as, ARMI_LDR, dest, idx, ofs); | ||
846 | emit_lso(as, ARMI_LDR, type, idx, ofs+4); | ||
847 | } | ||
848 | |||
849 | static void asm_ahustore(ASMState *as, IRIns *ir) | ||
850 | { | ||
851 | RegSet allow = RSET_GPR; | ||
852 | Reg idx, src = RID_NONE, type = RID_NONE; | ||
853 | int32_t ofs = 0; | ||
854 | int hiop = ((ir+1)->o == IR_HIOP); | ||
855 | if (!irt_ispri(ir->t)) { | ||
856 | src = ra_alloc1(as, ir->op2, allow); | ||
857 | rset_clear(allow, src); | ||
858 | } | ||
859 | if (hiop) | ||
860 | type = ra_alloc1(as, (ir+1)->op2, allow); | ||
861 | else | ||
862 | type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow); | ||
863 | idx = asm_fuseahuref(as, ir->op1, &ofs, rset_exclude(allow, type)); | ||
864 | if (ra_hasreg(src)) emit_lso(as, ARMI_STR, src, idx, ofs); | ||
865 | emit_lso(as, ARMI_STR, type, idx, ofs+4); | ||
866 | } | ||
867 | |||
868 | static void asm_sload(ASMState *as, IRIns *ir) | ||
869 | { | ||
870 | int32_t ofs = 8*((int32_t)ir->op1-1) + ((ir->op2 & IRSLOAD_FRAME) ? 4 : 0); | ||
871 | int hiop = ((ir+1)->o == IR_HIOP); | ||
872 | IRType t = hiop ? IRT_NUM : irt_type(ir->t); | ||
873 | Reg dest = RID_NONE, type = RID_NONE, base; | ||
874 | RegSet allow = RSET_GPR; | ||
875 | lua_assert(!(ir->op2 & IRSLOAD_PARENT)); /* Handled by asm_head_side(). */ | ||
876 | lua_assert(irt_isguard(ir->t) || !(ir->op2 & IRSLOAD_TYPECHECK)); | ||
877 | lua_assert(!(ir->op2 & IRSLOAD_CONVERT)); /* Handled by LJ_SOFTFP SPLIT. */ | ||
878 | if (hiop && ra_used(ir+1)) { | ||
879 | type = ra_dest(as, ir+1, allow); | ||
880 | rset_clear(allow, type); | ||
881 | } | ||
882 | if (ra_used(ir)) { | ||
883 | lua_assert(irt_isint(ir->t) || irt_isaddr(ir->t)); | ||
884 | dest = ra_dest(as, ir, allow); | ||
885 | rset_clear(allow, dest); | ||
886 | } | ||
887 | base = ra_alloc1(as, REF_BASE, allow); | ||
888 | if ((ir->op2 & IRSLOAD_TYPECHECK)) { | ||
889 | if (ra_noreg(type)) { | ||
890 | rset_clear(allow, base); | ||
891 | if (ofs < 256 && ra_hasreg(dest) && (dest & 1) == 0 && | ||
892 | rset_test((as->freeset & allow), dest+1)) { | ||
893 | type = dest+1; | ||
894 | ra_modified(as, type); | ||
895 | } else { | ||
896 | type = RID_TMP; | ||
897 | } | ||
898 | } | ||
899 | asm_guardcc(as, t == IRT_NUM ? CC_HS : CC_NE); | ||
900 | emit_n(as, ARMI_CMN|ARMI_K12|-irt_toitype_(t), type); | ||
901 | } | ||
902 | if (ra_hasreg(dest)) emit_lso(as, ARMI_LDR, dest, base, ofs); | ||
903 | if (ra_hasreg(type)) emit_lso(as, ARMI_LDR, type, base, ofs+4); | ||
904 | } | ||
905 | |||
906 | /* -- Allocations --------------------------------------------------------- */ | ||
907 | |||
908 | #if LJ_HASFFI | ||
909 | static void asm_cnew(ASMState *as, IRIns *ir) | ||
910 | { | ||
911 | CTState *cts = ctype_ctsG(J2G(as->J)); | ||
912 | CTypeID typeid = (CTypeID)IR(ir->op1)->i; | ||
913 | CTSize sz = (ir->o == IR_CNEWI || ir->op2 == REF_NIL) ? | ||
914 | lj_ctype_size(cts, typeid) : (CTSize)IR(ir->op2)->i; | ||
915 | const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_mem_newgco]; | ||
916 | IRRef args[2]; | ||
917 | RegSet allow = (RSET_GPR & ~RSET_SCRATCH); | ||
918 | lua_assert(sz != CTSIZE_INVALID); | ||
919 | |||
920 | args[0] = ASMREF_L; /* lua_State *L */ | ||
921 | args[1] = ASMREF_TMP1; /* MSize size */ | ||
922 | as->gcsteps++; | ||
923 | asm_setupresult(as, ir, ci); /* GCcdata * */ | ||
924 | |||
925 | /* Initialize immutable cdata object. */ | ||
926 | if (ir->o == IR_CNEWI) { | ||
927 | int32_t ofs = sizeof(GCcdata); | ||
928 | lua_assert(sz == 4 || sz == 8); | ||
929 | if (sz == 8) { | ||
930 | ofs += 4; ir++; | ||
931 | lua_assert(ir->o == IR_HIOP); | ||
932 | } | ||
933 | for (;;) { | ||
934 | Reg r = ra_alloc1(as, ir->op2, allow); | ||
935 | emit_lso(as, ARMI_STR, r, RID_RET, ofs); | ||
936 | rset_clear(allow, r); | ||
937 | if (ofs == sizeof(GCcdata)) break; | ||
938 | ofs -= 4; ir--; | ||
939 | } | ||
940 | } | ||
941 | /* Initialize gct and typeid. lj_mem_newgco() already sets marked. */ | ||
942 | { | ||
943 | uint32_t k = emit_isk12(ARMI_MOV, typeid); | ||
944 | Reg r = k ? RID_R1 : ra_allock(as, typeid, allow); | ||
945 | emit_lso(as, ARMI_STRB, RID_TMP, RID_RET, offsetof(GCcdata, gct)); | ||
946 | emit_lsox(as, ARMI_STRH, r, RID_RET, offsetof(GCcdata, typeid)); | ||
947 | emit_d(as, ARMI_MOV|ARMI_K12|~LJ_TCDATA, RID_TMP); | ||
948 | if (k) emit_d(as, ARMI_MOV^k, RID_R1); | ||
949 | } | ||
950 | asm_gencall(as, ci, args); | ||
951 | ra_allockreg(as, (int32_t)(sz+sizeof(GCcdata)), | ||
952 | ra_releasetmp(as, ASMREF_TMP1)); | ||
953 | } | ||
954 | #else | ||
955 | #define asm_cnew(as, ir) ((void)0) | ||
956 | #endif | ||
957 | |||
958 | /* -- Write barriers ------------------------------------------------------ */ | ||
959 | |||
960 | static void asm_tbar(ASMState *as, IRIns *ir) | ||
961 | { | ||
962 | Reg tab = ra_alloc1(as, ir->op1, RSET_GPR); | ||
963 | Reg link = ra_scratch(as, rset_exclude(RSET_GPR, tab)); | ||
964 | Reg gr = ra_allock(as, i32ptr(J2G(as->J)), | ||
965 | rset_exclude(rset_exclude(RSET_GPR, tab), link)); | ||
966 | Reg mark = RID_TMP; | ||
967 | MCLabel l_end = emit_label(as); | ||
968 | emit_lso(as, ARMI_STR, link, tab, (int32_t)offsetof(GCtab, gclist)); | ||
969 | emit_lso(as, ARMI_STRB, mark, tab, (int32_t)offsetof(GCtab, marked)); | ||
970 | emit_lso(as, ARMI_STR, tab, gr, | ||
971 | (int32_t)offsetof(global_State, gc.grayagain)); | ||
972 | emit_dn(as, ARMI_BIC|ARMI_K12|LJ_GC_BLACK, mark, mark); | ||
973 | emit_lso(as, ARMI_LDR, link, gr, | ||
974 | (int32_t)offsetof(global_State, gc.grayagain)); | ||
975 | emit_branch(as, ARMF_CC(ARMI_B, CC_EQ), l_end); | ||
976 | emit_n(as, ARMI_TST|ARMI_K12|LJ_GC_BLACK, mark); | ||
977 | emit_lso(as, ARMI_LDRB, mark, tab, (int32_t)offsetof(GCtab, marked)); | ||
978 | } | ||
979 | |||
980 | static void asm_obar(ASMState *as, IRIns *ir) | ||
981 | { | ||
982 | const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_barrieruv]; | ||
983 | IRRef args[2]; | ||
984 | MCLabel l_end; | ||
985 | Reg obj, val, tmp; | ||
986 | /* No need for other object barriers (yet). */ | ||
987 | lua_assert(IR(ir->op1)->o == IR_UREFC); | ||
988 | ra_evictset(as, RSET_SCRATCH); | ||
989 | l_end = emit_label(as); | ||
990 | args[0] = ASMREF_TMP1; /* global_State *g */ | ||
991 | args[1] = ir->op1; /* TValue *tv */ | ||
992 | asm_gencall(as, ci, args); | ||
993 | if ((*as->mcp >> 28) == CC_AL) | ||
994 | *as->mcp = ARMF_CC(*as->mcp, CC_NE); | ||
995 | else | ||
996 | emit_branch(as, ARMF_CC(ARMI_B, CC_EQ), l_end); | ||
997 | ra_allockreg(as, i32ptr(J2G(as->J)), ra_releasetmp(as, ASMREF_TMP1)); | ||
998 | obj = IR(ir->op1)->r; | ||
999 | tmp = ra_scratch(as, rset_exclude(RSET_GPR, obj)); | ||
1000 | emit_n(as, ARMF_CC(ARMI_TST, CC_NE)|ARMI_K12|LJ_GC_BLACK, tmp); | ||
1001 | emit_n(as, ARMI_TST|ARMI_K12|LJ_GC_WHITES, RID_TMP); | ||
1002 | val = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, obj)); | ||
1003 | emit_lso(as, ARMI_LDRB, tmp, obj, | ||
1004 | (int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv)); | ||
1005 | emit_lso(as, ARMI_LDRB, RID_TMP, val, (int32_t)offsetof(GChead, marked)); | ||
1006 | } | ||
1007 | |||
1008 | /* -- Arithmetic and logic operations ------------------------------------- */ | ||
1009 | |||
1010 | static int asm_swapops(ASMState *as, IRRef lref, IRRef rref) | ||
1011 | { | ||
1012 | IRIns *ir; | ||
1013 | if (irref_isk(rref)) | ||
1014 | return 0; /* Don't swap constants to the left. */ | ||
1015 | if (irref_isk(lref)) | ||
1016 | return 1; /* But swap constants to the right. */ | ||
1017 | ir = IR(rref); | ||
1018 | if ((ir->o >= IR_BSHL && ir->o <= IR_BROR) || | ||
1019 | (ir->o == IR_ADD && ir->op1 == ir->op2)) | ||
1020 | return 0; /* Don't swap fusable operands to the left. */ | ||
1021 | ir = IR(lref); | ||
1022 | if ((ir->o >= IR_BSHL && ir->o <= IR_BROR) || | ||
1023 | (ir->o == IR_ADD && ir->op1 == ir->op2)) | ||
1024 | return 1; /* But swap fusable operands to the right. */ | ||
1025 | return 0; /* Otherwise don't swap. */ | ||
1026 | } | ||
1027 | |||
1028 | static void asm_intop(ASMState *as, IRIns *ir, ARMIns ai) | ||
1029 | { | ||
1030 | IRRef lref = ir->op1, rref = ir->op2; | ||
1031 | Reg left, dest = ra_dest(as, ir, RSET_GPR); | ||
1032 | uint32_t m; | ||
1033 | if (asm_swapops(as, lref, rref)) { | ||
1034 | IRRef tmp = lref; lref = rref; rref = tmp; | ||
1035 | if ((ai & ~ARMI_S) == ARMI_SUB || (ai & ~ARMI_S) == ARMI_SBC) | ||
1036 | ai ^= (ARMI_SUB^ARMI_RSB); | ||
1037 | } | ||
1038 | left = ra_hintalloc(as, lref, dest, RSET_GPR); | ||
1039 | m = asm_fuseopm(as, ai, rref, rset_exclude(RSET_GPR, left)); | ||
1040 | if (irt_isguard(ir->t)) { /* For IR_ADDOV etc. */ | ||
1041 | asm_guardcc(as, CC_VS); | ||
1042 | ai |= ARMI_S; | ||
1043 | } | ||
1044 | emit_dn(as, ai^m, dest, left); | ||
1045 | } | ||
1046 | |||
1047 | static void asm_bitop(ASMState *as, IRIns *ir, ARMIns ai) | ||
1048 | { | ||
1049 | if (as->flagmcp == as->mcp) { /* Try to drop cmp r, #0. */ | ||
1050 | uint32_t cc = (as->mcp[1] >> 28); | ||
1051 | as->flagmcp = NULL; | ||
1052 | if (cc <= CC_NE) { | ||
1053 | as->mcp++; | ||
1054 | ai |= ARMI_S; | ||
1055 | } else if (cc == CC_GE) { | ||
1056 | *++as->mcp ^= ((CC_GE^CC_PL) << 28); | ||
1057 | ai |= ARMI_S; | ||
1058 | } else if (cc == CC_LT) { | ||
1059 | *++as->mcp ^= ((CC_LT^CC_MI) << 28); | ||
1060 | ai |= ARMI_S; | ||
1061 | } /* else: other conds don't work with bit ops. */ | ||
1062 | } | ||
1063 | if (ir->op2 == 0) { | ||
1064 | Reg dest = ra_dest(as, ir, RSET_GPR); | ||
1065 | uint32_t m = asm_fuseopm(as, ai, ir->op1, RSET_GPR); | ||
1066 | emit_d(as, ai^m, dest); | ||
1067 | } else { | ||
1068 | /* NYI: Turn BAND !k12 into uxtb, uxth or bfc or shl+shr. */ | ||
1069 | asm_intop(as, ir, ai); | ||
1070 | } | ||
1071 | } | ||
1072 | |||
1073 | static void asm_arithop(ASMState *as, IRIns *ir, ARMIns ai) | ||
1074 | { | ||
1075 | if (as->flagmcp == as->mcp) { /* Drop cmp r, #0. */ | ||
1076 | as->flagmcp = NULL; | ||
1077 | as->mcp++; | ||
1078 | ai |= ARMI_S; | ||
1079 | } | ||
1080 | asm_intop(as, ir, ai); | ||
1081 | } | ||
1082 | |||
1083 | static void asm_intneg(ASMState *as, IRIns *ir, ARMIns ai) | ||
1084 | { | ||
1085 | Reg dest = ra_dest(as, ir, RSET_GPR); | ||
1086 | Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); | ||
1087 | emit_dn(as, ai|ARMI_K12|0, dest, left); | ||
1088 | } | ||
1089 | |||
1090 | /* NYI: use add/shift for MUL(OV) with constants. FOLD only does 2^k. */ | ||
1091 | static void asm_intmul(ASMState *as, IRIns *ir) | ||
1092 | { | ||
1093 | Reg dest = ra_dest(as, ir, RSET_GPR); | ||
1094 | Reg left = ra_alloc1(as, ir->op1, rset_exclude(RSET_GPR, dest)); | ||
1095 | Reg right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); | ||
1096 | Reg tmp = RID_NONE; | ||
1097 | /* ARMv5 restriction: dest != left and dest_hi != left. */ | ||
1098 | if (dest == left && left != right) { left = right; right = dest; } | ||
1099 | if (irt_isguard(ir->t)) { /* IR_MULOV */ | ||
1100 | if (!(as->flags & JIT_F_ARMV6) && dest == left) | ||
1101 | tmp = left = ra_scratch(as, rset_exclude(RSET_FPR, left)); | ||
1102 | asm_guardcc(as, CC_NE); | ||
1103 | emit_nm(as, ARMI_TEQ|ARMF_SH(ARMSH_ASR, 31), RID_TMP, dest); | ||
1104 | emit_dnm(as, ARMI_SMULL|ARMF_S(right), dest, RID_TMP, left); | ||
1105 | } else { | ||
1106 | if (!(as->flags & JIT_F_ARMV6) && dest == left) tmp = left = RID_TMP; | ||
1107 | emit_nm(as, ARMI_MUL|ARMF_S(right), dest, left); | ||
1108 | } | ||
1109 | /* Only need this for the dest == left == right case. */ | ||
1110 | if (ra_hasreg(tmp)) emit_dm(as, ARMI_MOV, tmp, right); | ||
1111 | } | ||
1112 | |||
1113 | static void asm_bitswap(ASMState *as, IRIns *ir) | ||
1114 | { | ||
1115 | Reg dest = ra_dest(as, ir, RSET_GPR); | ||
1116 | Reg left = ra_alloc1(as, ir->op1, RSET_GPR); | ||
1117 | if ((as->flags & JIT_F_ARMV6)) { | ||
1118 | emit_dm(as, ARMI_REV, dest, left); | ||
1119 | } else { | ||
1120 | Reg tmp2 = dest; | ||
1121 | if (tmp2 == left) | ||
1122 | tmp2 = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR, dest), left)); | ||
1123 | emit_dnm(as, ARMI_EOR|ARMF_SH(ARMSH_LSR, 8), dest, tmp2, RID_TMP); | ||
1124 | emit_dm(as, ARMI_MOV|ARMF_SH(ARMSH_ROR, 8), tmp2, left); | ||
1125 | emit_dn(as, ARMI_BIC|ARMI_K12|256*8|255, RID_TMP, RID_TMP); | ||
1126 | emit_dnm(as, ARMI_EOR|ARMF_SH(ARMSH_ROR, 16), RID_TMP, left, left); | ||
1127 | } | ||
1128 | } | ||
1129 | |||
1130 | static void asm_bitshift(ASMState *as, IRIns *ir, ARMShift sh) | ||
1131 | { | ||
1132 | if (irref_isk(ir->op2)) { /* Constant shifts. */ | ||
1133 | /* NYI: Turn SHL+SHR or BAND+SHR into uxtb, uxth or ubfx. */ | ||
1134 | /* NYI: Turn SHL+ASR into sxtb, sxth or sbfx. */ | ||
1135 | Reg dest = ra_dest(as, ir, RSET_GPR); | ||
1136 | Reg left = ra_alloc1(as, ir->op1, RSET_GPR); | ||
1137 | int32_t shift = (IR(ir->op2)->i & 31); | ||
1138 | emit_dm(as, ARMI_MOV|ARMF_SH(sh, shift), dest, left); | ||
1139 | } else { | ||
1140 | Reg dest = ra_dest(as, ir, RSET_GPR); | ||
1141 | Reg left = ra_alloc1(as, ir->op1, RSET_GPR); | ||
1142 | Reg right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); | ||
1143 | emit_dm(as, ARMI_MOV|ARMF_RSH(sh, right), dest, left); | ||
1144 | } | ||
1145 | } | ||
1146 | |||
1147 | static void asm_intmin_max(ASMState *as, IRIns *ir, int cc) | ||
1148 | { | ||
1149 | uint32_t kcmp = 0, kmov = 0; | ||
1150 | Reg dest = ra_dest(as, ir, RSET_GPR); | ||
1151 | Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); | ||
1152 | Reg right = 0; | ||
1153 | if (irref_isk(ir->op2)) { | ||
1154 | kcmp = emit_isk12(ARMI_CMP, IR(ir->op2)->i); | ||
1155 | if (kcmp) kmov = emit_isk12(ARMI_MOV, IR(ir->op2)->i); | ||
1156 | } | ||
1157 | if (!kmov) { | ||
1158 | kcmp = 0; | ||
1159 | right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); | ||
1160 | } | ||
1161 | if (dest != right) { | ||
1162 | emit_dm(as, ARMF_CC(ARMI_MOV, cc)^kmov, dest, right); | ||
1163 | cc ^= 1; /* Must use opposite conditions for paired moves. */ | ||
1164 | } else { | ||
1165 | cc ^= (CC_LT^CC_GT); /* Otherwise may swap CC_LT <-> CC_GT. */ | ||
1166 | } | ||
1167 | if (dest != left) emit_dm(as, ARMF_CC(ARMI_MOV, cc)^kmov, dest, left); | ||
1168 | emit_nm(as, ARMI_CMP^kcmp, left, right); | ||
1169 | } | ||
1170 | |||
1171 | static void asm_fpmin_max(ASMState *as, IRIns *ir, int cc) | ||
1172 | { | ||
1173 | const CCallInfo *ci = &lj_ir_callinfo[IRCALL_softfp_cmp]; | ||
1174 | RegSet drop = RSET_SCRATCH; | ||
1175 | Reg r; | ||
1176 | IRRef args[4]; | ||
1177 | args[0] = ir->op1; args[1] = (ir+1)->op1; | ||
1178 | args[2] = ir->op2; args[3] = (ir+1)->op2; | ||
1179 | /* __aeabi_cdcmple preserves r0-r3. */ | ||
1180 | if (ra_hasreg(ir->r)) rset_clear(drop, ir->r); | ||
1181 | if (ra_hasreg((ir+1)->r)) rset_clear(drop, (ir+1)->r); | ||
1182 | if (!rset_test(as->freeset, RID_R2) && | ||
1183 | regcost_ref(as->cost[RID_R2]) == args[2]) rset_clear(drop, RID_R2); | ||
1184 | if (!rset_test(as->freeset, RID_R3) && | ||
1185 | regcost_ref(as->cost[RID_R3]) == args[3]) rset_clear(drop, RID_R3); | ||
1186 | ra_evictset(as, drop); | ||
1187 | ra_destpair(as, ir); | ||
1188 | emit_dm(as, ARMF_CC(ARMI_MOV, cc), RID_RETHI, RID_R3); | ||
1189 | emit_dm(as, ARMF_CC(ARMI_MOV, cc), RID_RET, RID_R2); | ||
1190 | emit_call(as, (void *)ci->func); | ||
1191 | for (r = RID_R0; r <= RID_R3; r++) | ||
1192 | ra_leftov(as, r, args[r-RID_R0]); | ||
1193 | } | ||
1194 | |||
1195 | /* -- Comparisons --------------------------------------------------------- */ | ||
1196 | |||
1197 | /* Map of comparisons to flags. ORDER IR. */ | ||
1198 | static const uint8_t asm_compmap[IR_ABC+1] = { | ||
1199 | /* op FP swp int cc FP cc */ | ||
1200 | /* LT */ CC_GE + (CC_HS << 4), | ||
1201 | /* GE x */ CC_LT + (CC_HI << 4), | ||
1202 | /* LE */ CC_GT + (CC_HI << 4), | ||
1203 | /* GT x */ CC_LE + (CC_HS << 4), | ||
1204 | /* ULT x */ CC_HS + (CC_LS << 4), | ||
1205 | /* UGE */ CC_LO + (CC_LO << 4), | ||
1206 | /* ULE x */ CC_HI + (CC_LO << 4), | ||
1207 | /* UGT */ CC_LS + (CC_LS << 4), | ||
1208 | /* EQ */ CC_NE + (CC_NE << 4), | ||
1209 | /* NE */ CC_EQ + (CC_EQ << 4), | ||
1210 | /* ABC */ CC_LS + (CC_LS << 4) /* Same as UGT. */ | ||
1211 | }; | ||
1212 | |||
1213 | /* FP comparisons. */ | ||
1214 | static void asm_fpcomp(ASMState *as, IRIns *ir) | ||
1215 | { | ||
1216 | const CCallInfo *ci = &lj_ir_callinfo[IRCALL_softfp_cmp]; | ||
1217 | RegSet drop = RSET_SCRATCH; | ||
1218 | Reg r; | ||
1219 | IRRef args[4]; | ||
1220 | int swp = (((ir->o ^ (ir->o >> 2)) & ~(ir->o >> 3) & 1) << 1); | ||
1221 | args[swp^0] = ir->op1; args[swp^1] = (ir+1)->op1; | ||
1222 | args[swp^2] = ir->op2; args[swp^3] = (ir+1)->op2; | ||
1223 | /* __aeabi_cdcmple preserves r0-r3. This helps to reduce spills. */ | ||
1224 | for (r = RID_R0; r <= RID_R3; r++) | ||
1225 | if (!rset_test(as->freeset, r) && | ||
1226 | regcost_ref(as->cost[r]) == args[r-RID_R0]) rset_clear(drop, r); | ||
1227 | ra_evictset(as, drop); | ||
1228 | asm_guardcc(as, (asm_compmap[ir->o] >> 4)); | ||
1229 | emit_call(as, (void *)ci->func); | ||
1230 | for (r = RID_R0; r <= RID_R3; r++) | ||
1231 | ra_leftov(as, r, args[r-RID_R0]); | ||
1232 | } | ||
1233 | |||
1234 | /* Integer comparisons. */ | ||
1235 | static void asm_intcomp(ASMState *as, IRIns *ir) | ||
1236 | { | ||
1237 | ARMCC cc = (asm_compmap[ir->o] & 15); | ||
1238 | IRRef lref = ir->op1, rref = ir->op2; | ||
1239 | Reg left; | ||
1240 | uint32_t m; | ||
1241 | int cmpprev0 = 0; | ||
1242 | lua_assert(irt_isint(ir->t) || irt_isaddr(ir->t)); | ||
1243 | if (asm_swapops(as, lref, rref)) { | ||
1244 | Reg tmp = lref; lref = rref; rref = tmp; | ||
1245 | if (cc >= CC_GE) cc ^= 7; /* LT <-> GT, LE <-> GE */ | ||
1246 | else if (cc > CC_NE) cc ^= 11; /* LO <-> HI, LS <-> HS */ | ||
1247 | } | ||
1248 | if (irref_isk(rref) && IR(rref)->i == 0) { | ||
1249 | IRIns *irl = IR(lref); | ||
1250 | cmpprev0 = (irl+1 == ir); | ||
1251 | /* Combine comp(BAND(left, right), 0) into tst left, right. */ | ||
1252 | if (cmpprev0 && irl->o == IR_BAND && !ra_used(irl)) { | ||
1253 | IRRef blref = irl->op1, brref = irl->op2; | ||
1254 | uint32_t m2 = 0; | ||
1255 | Reg bleft; | ||
1256 | if (asm_swapops(as, blref, brref)) { | ||
1257 | Reg tmp = blref; blref = brref; brref = tmp; | ||
1258 | } | ||
1259 | if (irref_isk(brref)) { | ||
1260 | m2 = emit_isk12(ARMI_AND, IR(brref)->i); | ||
1261 | if ((m2 & (ARMI_AND^ARMI_BIC))) | ||
1262 | goto notst; /* Not beneficial if we miss a constant operand. */ | ||
1263 | } | ||
1264 | if (cc == CC_GE) cc = CC_PL; | ||
1265 | else if (cc == CC_LT) cc = CC_MI; | ||
1266 | else if (cc > CC_NE) goto notst; /* Other conds don't work with tst. */ | ||
1267 | bleft = ra_alloc1(as, blref, RSET_GPR); | ||
1268 | if (!m2) m2 = asm_fuseopm(as, 0, brref, rset_exclude(RSET_GPR, bleft)); | ||
1269 | asm_guardcc(as, cc); | ||
1270 | emit_n(as, ARMI_TST^m2, bleft); | ||
1271 | return; | ||
1272 | } | ||
1273 | } | ||
1274 | notst: | ||
1275 | left = ra_alloc1(as, lref, RSET_GPR); | ||
1276 | m = asm_fuseopm(as, ARMI_CMP, rref, rset_exclude(RSET_GPR, left)); | ||
1277 | asm_guardcc(as, cc); | ||
1278 | emit_n(as, ARMI_CMP^m, left); | ||
1279 | /* Signed comparison with zero and referencing previous ins? */ | ||
1280 | if (cmpprev0 && (cc <= CC_NE || cc >= CC_GE)) | ||
1281 | as->flagmcp = as->mcp; /* Allow elimination of the compare. */ | ||
1282 | } | ||
1283 | |||
1284 | /* 64 bit integer comparisons. */ | ||
1285 | static void asm_int64comp(ASMState *as, IRIns *ir) | ||
1286 | { | ||
1287 | int signedcomp = (ir->o <= IR_GT); | ||
1288 | ARMCC cclo, cchi; | ||
1289 | Reg leftlo, lefthi; | ||
1290 | uint32_t mlo, mhi; | ||
1291 | RegSet allow = RSET_GPR, oldfree; | ||
1292 | |||
1293 | /* Always use unsigned comparison for loword. */ | ||
1294 | cclo = asm_compmap[ir->o + (signedcomp ? 4 : 0)] & 15; | ||
1295 | leftlo = ra_alloc1(as, ir->op1, allow); | ||
1296 | oldfree = as->freeset; | ||
1297 | mlo = asm_fuseopm(as, ARMI_CMP, ir->op2, rset_clear(allow, leftlo)); | ||
1298 | allow &= ~(oldfree & ~as->freeset); /* Update for allocs of asm_fuseopm. */ | ||
1299 | |||
1300 | /* Use signed or unsigned comparison for hiword. */ | ||
1301 | cchi = asm_compmap[ir->o] & 15; | ||
1302 | lefthi = ra_alloc1(as, (ir+1)->op1, allow); | ||
1303 | mhi = asm_fuseopm(as, ARMI_CMP, (ir+1)->op2, rset_clear(allow, lefthi)); | ||
1304 | |||
1305 | /* All register allocations must be performed _before_ this point. */ | ||
1306 | if (signedcomp) { | ||
1307 | MCLabel l_around = emit_label(as); | ||
1308 | asm_guardcc(as, cclo); | ||
1309 | emit_n(as, ARMI_CMP^mlo, leftlo); | ||
1310 | emit_branch(as, ARMF_CC(ARMI_B, CC_NE), l_around); | ||
1311 | if (cchi == CC_GE || cchi == CC_LE) cchi ^= 6; /* GE -> GT, LE -> LT */ | ||
1312 | asm_guardcc(as, cchi); | ||
1313 | } else { | ||
1314 | asm_guardcc(as, cclo); | ||
1315 | emit_n(as, ARMF_CC(ARMI_CMP, CC_EQ)^mlo, leftlo); | ||
1316 | } | ||
1317 | emit_n(as, ARMI_CMP^mhi, lefthi); | ||
1318 | } | ||
1319 | |||
1320 | /* -- Support for 64 bit ops in 32 bit mode ------------------------------- */ | ||
1321 | |||
1322 | /* Hiword op of a split 64 bit op. Previous op must be the loword op. */ | ||
1323 | static void asm_hiop(ASMState *as, IRIns *ir) | ||
1324 | { | ||
1325 | /* HIOP is marked as a store because it needs its own DCE logic. */ | ||
1326 | int uselo = ra_used(ir-1), usehi = ra_used(ir); /* Loword/hiword used? */ | ||
1327 | if (LJ_UNLIKELY(!(as->flags & JIT_F_OPT_DCE))) uselo = usehi = 1; | ||
1328 | if ((ir-1)->o <= IR_NE) { /* 64 bit integer or FP comparisons. ORDER IR. */ | ||
1329 | as->curins--; /* Always skip the loword comparison. */ | ||
1330 | if (irt_isint(ir->t)) | ||
1331 | asm_int64comp(as, ir-1); | ||
1332 | else | ||
1333 | asm_fpcomp(as, ir-1); | ||
1334 | return; | ||
1335 | } else if ((ir-1)->o == IR_MIN || (ir-1)->o == IR_MAX) { | ||
1336 | if (uselo || usehi || !(as->flags & JIT_F_OPT_DCE)) { | ||
1337 | as->curins--; /* Always skip the loword min/max. */ | ||
1338 | asm_fpmin_max(as, ir-1, (ir-1)->o == IR_MIN ? CC_HI : CC_LO); | ||
1339 | } | ||
1340 | return; | ||
1341 | } | ||
1342 | if (!usehi && (as->flags & JIT_F_OPT_DCE)) | ||
1343 | return; /* Skip unused hiword op for all remaining ops. */ | ||
1344 | switch ((ir-1)->o) { | ||
1345 | #if LJ_HASFFI | ||
1346 | case IR_ADD: | ||
1347 | if (uselo) { | ||
1348 | as->curins--; | ||
1349 | asm_intop(as, ir, ARMI_ADC); | ||
1350 | asm_intop(as, ir-1, ARMI_ADD|ARMI_S); | ||
1351 | } else { | ||
1352 | asm_intop(as, ir, ARMI_ADD); | ||
1353 | } | ||
1354 | break; | ||
1355 | case IR_SUB: | ||
1356 | if (uselo) { | ||
1357 | as->curins--; | ||
1358 | asm_intop(as, ir, ARMI_SBC); | ||
1359 | asm_intop(as, ir-1, ARMI_SUB|ARMI_S); | ||
1360 | } else { | ||
1361 | asm_intop(as, ir, ARMI_SUB); | ||
1362 | } | ||
1363 | break; | ||
1364 | case IR_NEG: | ||
1365 | if (uselo) { | ||
1366 | as->curins--; | ||
1367 | asm_intneg(as, ir, ARMI_RSC); | ||
1368 | asm_intneg(as, ir-1, ARMI_RSB|ARMI_S); | ||
1369 | } else { | ||
1370 | asm_intneg(as, ir, ARMI_RSB); | ||
1371 | } | ||
1372 | break; | ||
1373 | #endif | ||
1374 | case IR_SLOAD: case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD: | ||
1375 | case IR_STRTO: | ||
1376 | if (!uselo) | ||
1377 | ra_allocref(as, ir->op1, RSET_GPR); /* Mark lo op as used. */ | ||
1378 | break; | ||
1379 | case IR_CALLN: | ||
1380 | case IR_CALLS: | ||
1381 | case IR_CALLXS: | ||
1382 | if (!uselo) | ||
1383 | ra_allocref(as, ir->op1, RID2RSET(RID_RET)); /* Mark lo op as used. */ | ||
1384 | break; | ||
1385 | case IR_ASTORE: case IR_HSTORE: case IR_USTORE: | ||
1386 | case IR_TOSTR: case IR_CNEWI: | ||
1387 | /* Nothing to do here. Handled by lo op itself. */ | ||
1388 | break; | ||
1389 | default: lua_assert(0); break; | ||
1390 | } | ||
1391 | } | ||
1392 | |||
1393 | /* -- Stack handling ------------------------------------------------------ */ | ||
1394 | |||
1395 | /* Check Lua stack size for overflow. Use exit handler as fallback. */ | ||
1396 | static void asm_stack_check(ASMState *as, BCReg topslot, | ||
1397 | IRIns *irp, RegSet allow, ExitNo exitno) | ||
1398 | { | ||
1399 | Reg pbase; | ||
1400 | uint32_t k; | ||
1401 | if (irp) { | ||
1402 | exitno = as->T->nsnap; /* Highest exit + 1 indicates stack check. */ | ||
1403 | if (ra_hasreg(irp->r)) { | ||
1404 | pbase = irp->r; | ||
1405 | } else if (allow) { | ||
1406 | pbase = rset_pickbot(allow); | ||
1407 | } else { | ||
1408 | pbase = RID_RET; | ||
1409 | emit_lso(as, ARMI_LDR, RID_RET, RID_SP, 0); /* Restore temp. register. */ | ||
1410 | } | ||
1411 | } else { | ||
1412 | pbase = RID_BASE; | ||
1413 | } | ||
1414 | emit_branch(as, ARMF_CC(ARMI_BL, CC_LS), exitstub_addr(as->J, exitno)); | ||
1415 | k = emit_isk12(0, (int32_t)(8*topslot)); | ||
1416 | lua_assert(k); | ||
1417 | emit_n(as, ARMI_CMP^k, RID_TMP); | ||
1418 | emit_dnm(as, ARMI_SUB, RID_TMP, RID_TMP, pbase); | ||
1419 | emit_lso(as, ARMI_LDR, RID_TMP, RID_TMP, | ||
1420 | (int32_t)offsetof(lua_State, maxstack)); | ||
1421 | if (irp) { /* Must not spill arbitrary registers in head of side trace. */ | ||
1422 | int32_t i = i32ptr(&J2G(as->J)->jit_L); | ||
1423 | if (ra_noreg(irp->r)) { | ||
1424 | lua_assert(ra_hasspill(irp->s)); | ||
1425 | emit_lso(as, ARMI_LDR, RID_RET, RID_SP, sps_scale(irp->s)); | ||
1426 | } | ||
1427 | emit_lso(as, ARMI_LDR, RID_TMP, RID_TMP, (i & 4095)); | ||
1428 | if (ra_noreg(irp->r)) { | ||
1429 | emit_lso(as, ARMI_STR, RID_RET, RID_SP, 0); /* Save temp. register. */ | ||
1430 | } | ||
1431 | emit_loadi(as, RID_TMP, (i & ~4095)); | ||
1432 | } else { | ||
1433 | emit_getgl(as, RID_TMP, jit_L); | ||
1434 | } | ||
1435 | } | ||
1436 | |||
1437 | /* Restore Lua stack from on-trace state. */ | ||
1438 | static void asm_stack_restore(ASMState *as, SnapShot *snap) | ||
1439 | { | ||
1440 | SnapEntry *map = &as->T->snapmap[snap->mapofs]; | ||
1441 | MSize n, nent = snap->nent; | ||
1442 | SnapEntry *flinks = map + nent + snap->depth; | ||
1443 | /* Store the value of all modified slots to the Lua stack. */ | ||
1444 | for (n = 0; n < nent; n++) { | ||
1445 | SnapEntry sn = map[n]; | ||
1446 | BCReg s = snap_slot(sn); | ||
1447 | int32_t ofs = 8*((int32_t)s-1); | ||
1448 | IRRef ref = snap_ref(sn); | ||
1449 | IRIns *ir = IR(ref); | ||
1450 | if ((sn & SNAP_NORESTORE)) | ||
1451 | continue; | ||
1452 | if (irt_isnum(ir->t)) { | ||
1453 | RegSet odd = rset_exclude(RSET_GPRODD, RID_BASE); | ||
1454 | Reg tmp; | ||
1455 | lua_assert(irref_isk(ref)); /* LJ_SOFTFP: must be a number constant. */ | ||
1456 | tmp = ra_allock(as, (int32_t)ir_knum(ir)->u32.lo, | ||
1457 | rset_exclude(RSET_GPREVEN, RID_BASE)); | ||
1458 | emit_lso(as, ARMI_STR, tmp, RID_BASE, ofs); | ||
1459 | if (rset_test(as->freeset, tmp+1)) odd = RID2RSET(tmp+1); | ||
1460 | tmp = ra_allock(as, (int32_t)ir_knum(ir)->u32.hi, odd); | ||
1461 | emit_lso(as, ARMI_STR, tmp, RID_BASE, ofs+4); | ||
1462 | } else { | ||
1463 | RegSet odd = rset_exclude(RSET_GPRODD, RID_BASE); | ||
1464 | Reg type; | ||
1465 | lua_assert(irt_ispri(ir->t) || irt_isaddr(ir->t) || irt_isinteger(ir->t)); | ||
1466 | if (!irt_ispri(ir->t)) { | ||
1467 | Reg src = ra_alloc1(as, ref, rset_exclude(RSET_GPREVEN, RID_BASE)); | ||
1468 | emit_lso(as, ARMI_STR, src, RID_BASE, ofs); | ||
1469 | if (rset_test(as->freeset, src+1)) odd = RID2RSET(src+1); | ||
1470 | } | ||
1471 | if ((sn & (SNAP_CONT|SNAP_FRAME))) { | ||
1472 | if (s == 0) continue; /* Do not overwrite link to previous frame. */ | ||
1473 | type = ra_allock(as, (int32_t)(*flinks--), odd); | ||
1474 | } else if ((sn & SNAP_SOFTFPNUM)) { | ||
1475 | type = ra_alloc1(as, ref+1, rset_exclude(RSET_GPRODD, RID_BASE)); | ||
1476 | } else { | ||
1477 | type = ra_allock(as, (int32_t)irt_toitype(ir->t), odd); | ||
1478 | } | ||
1479 | emit_lso(as, ARMI_STR, type, RID_BASE, ofs+4); | ||
1480 | } | ||
1481 | checkmclim(as); | ||
1482 | } | ||
1483 | lua_assert(map + nent == flinks); | ||
1484 | } | ||
1485 | |||
1486 | /* -- GC handling --------------------------------------------------------- */ | ||
1487 | |||
1488 | /* Check GC threshold and do one or more GC steps. */ | ||
1489 | static void asm_gc_check(ASMState *as) | ||
1490 | { | ||
1491 | const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_step_jit]; | ||
1492 | IRRef args[2]; | ||
1493 | MCLabel l_end; | ||
1494 | Reg tmp1, tmp2; | ||
1495 | ra_evictset(as, RSET_SCRATCH); | ||
1496 | l_end = emit_label(as); | ||
1497 | /* Exit trace if in GCSatomic or GCSfinalize. Avoids syncing GC objects. */ | ||
1498 | asm_guardcc(as, CC_NE); /* Assumes asm_snap_prep() already done. */ | ||
1499 | emit_n(as, ARMI_CMP|ARMI_K12|0, RID_RET); | ||
1500 | args[0] = ASMREF_TMP1; /* global_State *g */ | ||
1501 | args[1] = ASMREF_TMP2; /* MSize steps */ | ||
1502 | asm_gencall(as, ci, args); | ||
1503 | tmp1 = ra_releasetmp(as, ASMREF_TMP1); | ||
1504 | tmp2 = ra_releasetmp(as, ASMREF_TMP2); | ||
1505 | emit_loadi(as, tmp2, (int32_t)as->gcsteps); | ||
1506 | /* Jump around GC step if GC total < GC threshold. */ | ||
1507 | emit_branch(as, ARMF_CC(ARMI_B, CC_LS), l_end); | ||
1508 | emit_nm(as, ARMI_CMP, RID_TMP, tmp2); | ||
1509 | emit_lso(as, ARMI_LDR, tmp2, tmp1, | ||
1510 | (int32_t)offsetof(global_State, gc.threshold)); | ||
1511 | emit_lso(as, ARMI_LDR, RID_TMP, tmp1, | ||
1512 | (int32_t)offsetof(global_State, gc.total)); | ||
1513 | ra_allockreg(as, i32ptr(J2G(as->J)), tmp1); | ||
1514 | as->gcsteps = 0; | ||
1515 | checkmclim(as); | ||
1516 | } | ||
1517 | |||
1518 | /* -- Loop handling ------------------------------------------------------- */ | ||
1519 | |||
1520 | /* Fixup the loop branch. */ | ||
1521 | static void asm_loop_fixup(ASMState *as) | ||
1522 | { | ||
1523 | MCode *p = as->mctop; | ||
1524 | MCode *target = as->mcp; | ||
1525 | if (as->loopinv) { /* Inverted loop branch? */ | ||
1526 | /* asm_guardcc already inverted the bcc and patched the final bl. */ | ||
1527 | p[-2] |= ((uint32_t)(target-p) & 0x00ffffffu); | ||
1528 | } else { | ||
1529 | p[-1] = ARMI_B | ((uint32_t)((target-p)-1) & 0x00ffffffu); | ||
1530 | } | ||
1531 | } | ||
1532 | |||
1533 | /* -- Head of trace ------------------------------------------------------- */ | ||
1534 | |||
1535 | /* Reload L register from g->jit_L. */ | ||
1536 | static void asm_head_lreg(ASMState *as) | ||
1537 | { | ||
1538 | IRIns *ir = IR(ASMREF_L); | ||
1539 | if (ra_used(ir)) { | ||
1540 | Reg r = ra_dest(as, ir, RSET_GPR); | ||
1541 | emit_getgl(as, r, jit_L); | ||
1542 | ra_evictk(as); | ||
1543 | } | ||
1544 | } | ||
1545 | |||
1546 | /* Coalesce BASE register for a root trace. */ | ||
1547 | static void asm_head_root_base(ASMState *as) | ||
1548 | { | ||
1549 | IRIns *ir; | ||
1550 | asm_head_lreg(as); | ||
1551 | ir = IR(REF_BASE); | ||
1552 | if (ra_hasreg(ir->r) && rset_test(as->modset, ir->r)) ra_spill(as, ir); | ||
1553 | ra_destreg(as, ir, RID_BASE); | ||
1554 | } | ||
1555 | |||
1556 | /* Coalesce BASE register for a side trace. */ | ||
1557 | static RegSet asm_head_side_base(ASMState *as, IRIns *irp, RegSet allow) | ||
1558 | { | ||
1559 | IRIns *ir; | ||
1560 | asm_head_lreg(as); | ||
1561 | ir = IR(REF_BASE); | ||
1562 | if (ra_hasreg(ir->r) && rset_test(as->modset, ir->r)) ra_spill(as, ir); | ||
1563 | if (ra_hasspill(irp->s)) { | ||
1564 | rset_clear(allow, ra_dest(as, ir, allow)); | ||
1565 | } else { | ||
1566 | lua_assert(ra_hasreg(irp->r)); | ||
1567 | rset_clear(allow, irp->r); | ||
1568 | ra_destreg(as, ir, irp->r); | ||
1569 | } | ||
1570 | return allow; | ||
1571 | } | ||
1572 | |||
1573 | /* -- Tail of trace ------------------------------------------------------- */ | ||
1574 | |||
1575 | /* Fixup the tail code. */ | ||
1576 | static void asm_tail_fixup(ASMState *as, TraceNo lnk) | ||
1577 | { | ||
1578 | MCode *p = as->mctop; | ||
1579 | MCode *target; | ||
1580 | int32_t spadj = as->T->spadjust; | ||
1581 | if (spadj == 0) { | ||
1582 | as->mctop = --p; | ||
1583 | } else { | ||
1584 | /* Patch stack adjustment. */ | ||
1585 | uint32_t k = emit_isk12(ARMI_ADD, spadj); | ||
1586 | lua_assert(k); | ||
1587 | p[-2] = (ARMI_ADD^k) | ARMF_D(RID_SP) | ARMF_N(RID_SP); | ||
1588 | } | ||
1589 | /* Patch exit branch. */ | ||
1590 | target = lnk == TRACE_INTERP ? (MCode *)lj_vm_exit_interp : | ||
1591 | traceref(as->J, lnk)->mcode; | ||
1592 | p[-1] = ARMI_B|(((target-p)-1)&0x00ffffffu); | ||
1593 | } | ||
1594 | |||
1595 | /* Prepare tail of code. */ | ||
1596 | static void asm_tail_prep(ASMState *as) | ||
1597 | { | ||
1598 | MCode *p = as->mctop - 1; /* Leave room for exit branch. */ | ||
1599 | if (as->loopref) { | ||
1600 | as->invmcp = as->mcp = p; | ||
1601 | } else { | ||
1602 | as->mcp = p-1; /* Leave room for stack pointer adjustment. */ | ||
1603 | as->invmcp = NULL; | ||
1604 | } | ||
1605 | *p = 0; /* Prevent load/store merging. */ | ||
1606 | } | ||
1607 | |||
1608 | /* -- Instruction dispatch ------------------------------------------------ */ | ||
1609 | |||
1610 | /* Assemble a single instruction. */ | ||
1611 | static void asm_ir(ASMState *as, IRIns *ir) | ||
1612 | { | ||
1613 | switch ((IROp)ir->o) { | ||
1614 | /* Miscellaneous ops. */ | ||
1615 | case IR_LOOP: asm_loop(as); break; | ||
1616 | case IR_NOP: case IR_XBAR: lua_assert(!ra_used(ir)); break; | ||
1617 | case IR_USE: ra_alloc1(as, ir->op1, RSET_GPR); break; | ||
1618 | case IR_PHI: asm_phi(as, ir); break; | ||
1619 | case IR_HIOP: asm_hiop(as, ir); break; | ||
1620 | |||
1621 | /* Guarded assertions. */ | ||
1622 | case IR_EQ: case IR_NE: | ||
1623 | if ((ir-1)->o == IR_HREF && ir->op1 == as->curins-1) { | ||
1624 | as->curins--; | ||
1625 | asm_href(as, ir-1, (IROp)ir->o); | ||
1626 | break; | ||
1627 | } | ||
1628 | /* fallthrough */ | ||
1629 | case IR_LT: case IR_GE: case IR_LE: case IR_GT: | ||
1630 | case IR_ULT: case IR_UGE: case IR_ULE: case IR_UGT: | ||
1631 | case IR_ABC: | ||
1632 | asm_intcomp(as, ir); | ||
1633 | break; | ||
1634 | |||
1635 | case IR_RETF: asm_retf(as, ir); break; | ||
1636 | |||
1637 | /* Bit ops. */ | ||
1638 | case IR_BNOT: asm_bitop(as, ir, ARMI_MVN); break; | ||
1639 | case IR_BSWAP: asm_bitswap(as, ir); break; | ||
1640 | |||
1641 | case IR_BAND: asm_bitop(as, ir, ARMI_AND); break; | ||
1642 | case IR_BOR: asm_bitop(as, ir, ARMI_ORR); break; | ||
1643 | case IR_BXOR: asm_bitop(as, ir, ARMI_EOR); break; | ||
1644 | |||
1645 | case IR_BSHL: asm_bitshift(as, ir, ARMSH_LSL); break; | ||
1646 | case IR_BSHR: asm_bitshift(as, ir, ARMSH_LSR); break; | ||
1647 | case IR_BSAR: asm_bitshift(as, ir, ARMSH_ASR); break; | ||
1648 | case IR_BROR: asm_bitshift(as, ir, ARMSH_ROR); break; | ||
1649 | case IR_BROL: lua_assert(0); break; | ||
1650 | |||
1651 | /* Arithmetic ops. */ | ||
1652 | case IR_ADD: case IR_ADDOV: asm_arithop(as, ir, ARMI_ADD); break; | ||
1653 | case IR_SUB: case IR_SUBOV: asm_arithop(as, ir, ARMI_SUB); break; | ||
1654 | case IR_MUL: case IR_MULOV: asm_intmul(as, ir); break; | ||
1655 | |||
1656 | case IR_NEG: asm_intneg(as, ir, ARMI_RSB); break; | ||
1657 | |||
1658 | case IR_MIN: asm_intmin_max(as, ir, CC_GT); break; | ||
1659 | case IR_MAX: asm_intmin_max(as, ir, CC_LT); break; | ||
1660 | |||
1661 | case IR_FPMATH: case IR_ATAN2: case IR_LDEXP: | ||
1662 | case IR_DIV: case IR_MOD: case IR_POW: case IR_ABS: case IR_TOBIT: | ||
1663 | lua_assert(0); /* Unused for LJ_SOFTFP. */ | ||
1664 | break; | ||
1665 | |||
1666 | /* Memory references. */ | ||
1667 | case IR_AREF: asm_aref(as, ir); break; | ||
1668 | case IR_HREF: asm_href(as, ir, 0); break; | ||
1669 | case IR_HREFK: asm_hrefk(as, ir); break; | ||
1670 | case IR_NEWREF: asm_newref(as, ir); break; | ||
1671 | case IR_UREFO: case IR_UREFC: asm_uref(as, ir); break; | ||
1672 | case IR_FREF: asm_fref(as, ir); break; | ||
1673 | case IR_STRREF: asm_strref(as, ir); break; | ||
1674 | |||
1675 | /* Loads and stores. */ | ||
1676 | case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD: | ||
1677 | asm_ahuvload(as, ir); | ||
1678 | break; | ||
1679 | case IR_FLOAD: case IR_XLOAD: asm_fxload(as, ir); break; | ||
1680 | case IR_SLOAD: asm_sload(as, ir); break; | ||
1681 | |||
1682 | case IR_ASTORE: case IR_HSTORE: case IR_USTORE: asm_ahustore(as, ir); break; | ||
1683 | case IR_FSTORE: case IR_XSTORE: asm_fxstore(as, ir); break; | ||
1684 | |||
1685 | /* Allocations. */ | ||
1686 | case IR_SNEW: case IR_XSNEW: asm_snew(as, ir); break; | ||
1687 | case IR_TNEW: asm_tnew(as, ir); break; | ||
1688 | case IR_TDUP: asm_tdup(as, ir); break; | ||
1689 | case IR_CNEW: case IR_CNEWI: asm_cnew(as, ir); break; | ||
1690 | |||
1691 | /* Write barriers. */ | ||
1692 | case IR_TBAR: asm_tbar(as, ir); break; | ||
1693 | case IR_OBAR: asm_obar(as, ir); break; | ||
1694 | |||
1695 | /* Type conversions. */ | ||
1696 | case IR_CONV: asm_conv(as, ir); break; | ||
1697 | case IR_TOSTR: asm_tostr(as, ir); break; | ||
1698 | case IR_STRTO: asm_strto(as, ir); break; | ||
1699 | |||
1700 | /* Calls. */ | ||
1701 | case IR_CALLN: case IR_CALLL: case IR_CALLS: asm_call(as, ir); break; | ||
1702 | case IR_CALLXS: asm_callx(as, ir); break; | ||
1703 | case IR_CARG: break; | ||
1704 | |||
1705 | default: | ||
1706 | setintV(&as->J->errinfo, ir->o); | ||
1707 | lj_trace_err_info(as->J, LJ_TRERR_NYIIR); | ||
1708 | break; | ||
1709 | } | ||
1710 | } | ||
1711 | |||
1712 | /* -- Trace setup --------------------------------------------------------- */ | ||
1713 | |||
1714 | /* Ensure there are enough stack slots for call arguments. */ | ||
1715 | static Reg asm_setup_call_slots(ASMState *as, IRIns *ir, const CCallInfo *ci) | ||
1716 | { | ||
1717 | IRRef args[CCI_NARGS_MAX]; | ||
1718 | uint32_t i, nargs = (int)CCI_NARGS(ci); | ||
1719 | int nslots = 0, ngpr = REGARG_NUMGPR; | ||
1720 | asm_collectargs(as, ir, ci, args); | ||
1721 | for (i = 0; i < nargs; i++) | ||
1722 | if (!LJ_SOFTFP && irt_isfp(IR(args[i])->t)) { | ||
1723 | ngpr &= ~1; | ||
1724 | if (ngpr > 0) ngpr -= 2; else nslots += 2; | ||
1725 | } else { | ||
1726 | if (ngpr > 0) ngpr--; else nslots++; | ||
1727 | } | ||
1728 | if (nslots > as->evenspill) /* Leave room for args in stack slots. */ | ||
1729 | as->evenspill = nslots; | ||
1730 | return REGSP_HINT(RID_RET); | ||
1731 | } | ||
1732 | |||
1733 | static void asm_setup_target(ASMState *as) | ||
1734 | { | ||
1735 | /* May need extra exit for asm_stack_check on side traces. */ | ||
1736 | asm_exitstub_setup(as, as->T->nsnap + (as->parent ? 1 : 0)); | ||
1737 | } | ||
1738 | |||
1739 | /* -- Trace patching ------------------------------------------------------ */ | ||
1740 | |||
1741 | /* Patch exit jumps of existing machine code to a new target. */ | ||
1742 | void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, MCode *target) | ||
1743 | { | ||
1744 | MCode *p = T->mcode; | ||
1745 | MCode *pe = (MCode *)((char *)p + T->szmcode); | ||
1746 | MCode *cstart = NULL, *cend = p; | ||
1747 | MCode *mcarea = lj_mcode_patch(J, p, 0); | ||
1748 | MCode *px = exitstub_addr(J, exitno) - 2; | ||
1749 | for (; p < pe; p++) { | ||
1750 | /* Look for bl_cc exitstub, replace with b_cc target. */ | ||
1751 | uint32_t ins = *p; | ||
1752 | if ((ins & 0x0f000000u) == 0x0b000000u && ins < 0xf0000000u && | ||
1753 | ((ins ^ (px-p)) & 0x00ffffffu) == 0) { | ||
1754 | *p = (ins & 0xfe000000u) | (((target-p)-2) & 0x00ffffffu); | ||
1755 | cend = p+1; | ||
1756 | if (!cstart) cstart = p; | ||
1757 | } | ||
1758 | } | ||
1759 | lua_assert(cstart != NULL); | ||
1760 | asm_cache_flush(cstart, cend); | ||
1761 | lj_mcode_patch(J, mcarea, 1); | ||
1762 | } | ||
1763 | |||
diff --git a/src/lj_emit_arm.h b/src/lj_emit_arm.h new file mode 100644 index 00000000..ea908520 --- /dev/null +++ b/src/lj_emit_arm.h | |||
@@ -0,0 +1,300 @@ | |||
1 | /* | ||
2 | ** ARM instruction emitter. | ||
3 | ** Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h | ||
4 | */ | ||
5 | |||
6 | /* -- Constant encoding --------------------------------------------------- */ | ||
7 | |||
8 | static uint8_t emit_invai[16] = { | ||
9 | /* AND */ (ARMI_AND^ARMI_BIC) >> 21, | ||
10 | /* EOR */ 0, | ||
11 | /* SUB */ (ARMI_SUB^ARMI_ADD) >> 21, | ||
12 | /* RSB */ 0, | ||
13 | /* ADD */ (ARMI_ADD^ARMI_SUB) >> 21, | ||
14 | /* ADC */ (ARMI_ADC^ARMI_SBC) >> 21, | ||
15 | /* SBC */ (ARMI_SBC^ARMI_ADC) >> 21, | ||
16 | /* RSC */ 0, | ||
17 | /* TST */ 0, | ||
18 | /* TEQ */ 0, | ||
19 | /* CMP */ (ARMI_CMP^ARMI_CMN) >> 21, | ||
20 | /* CMN */ (ARMI_CMN^ARMI_CMP) >> 21, | ||
21 | /* ORR */ 0, | ||
22 | /* MOV */ (ARMI_MOV^ARMI_MVN) >> 21, | ||
23 | /* BIC */ (ARMI_BIC^ARMI_AND) >> 21, | ||
24 | /* MVN */ (ARMI_MVN^ARMI_MOV) >> 21 | ||
25 | }; | ||
26 | |||
27 | /* Encode constant in K12 format for data processing instructions. */ | ||
28 | static uint32_t emit_isk12(ARMIns ai, int32_t n) | ||
29 | { | ||
30 | uint32_t invai, i, m = (uint32_t)n; | ||
31 | /* K12: unsigned 8 bit value, rotated in steps of two bits. */ | ||
32 | for (i = 0; i < 4096; i += 256, m = lj_rol(m, 2)) | ||
33 | if (m <= 255) return ARMI_K12|m|i; | ||
34 | /* Otherwise try negation/complement with the inverse instruction. */ | ||
35 | invai = emit_invai[((ai >> 21) & 15)]; | ||
36 | if (!invai) return 0; /* Failed. No inverse instruction. */ | ||
37 | m = ~(uint32_t)n; | ||
38 | if (invai == ((ARMI_SUB^ARMI_ADD) >> 21) || | ||
39 | invai == (ARMI_CMP^ARMI_CMN) >> 21) m++; | ||
40 | for (i = 0; i < 4096; i += 256, m = lj_rol(m, 2)) | ||
41 | if (m <= 255) return ARMI_K12|(invai<<21)|m|i; | ||
42 | return 0; /* Failed. */ | ||
43 | } | ||
44 | |||
45 | /* -- Emit basic instructions --------------------------------------------- */ | ||
46 | |||
47 | static void emit_dnm(ASMState *as, ARMIns ai, Reg rd, Reg rn, Reg rm) | ||
48 | { | ||
49 | *--as->mcp = ai | ARMF_D(rd) | ARMF_N(rn) | ARMF_M(rm); | ||
50 | } | ||
51 | |||
52 | static void emit_dm(ASMState *as, ARMIns ai, Reg rd, Reg rm) | ||
53 | { | ||
54 | *--as->mcp = ai | ARMF_D(rd) | ARMF_M(rm); | ||
55 | } | ||
56 | |||
57 | static void emit_dn(ASMState *as, ARMIns ai, Reg rd, Reg rn) | ||
58 | { | ||
59 | *--as->mcp = ai | ARMF_D(rd) | ARMF_N(rn); | ||
60 | } | ||
61 | |||
62 | static void emit_nm(ASMState *as, ARMIns ai, Reg rn, Reg rm) | ||
63 | { | ||
64 | *--as->mcp = ai | ARMF_N(rn) | ARMF_M(rm); | ||
65 | } | ||
66 | |||
67 | static void emit_d(ASMState *as, ARMIns ai, Reg rd) | ||
68 | { | ||
69 | *--as->mcp = ai | ARMF_D(rd); | ||
70 | } | ||
71 | |||
72 | static void emit_n(ASMState *as, ARMIns ai, Reg rn) | ||
73 | { | ||
74 | *--as->mcp = ai | ARMF_N(rn); | ||
75 | } | ||
76 | |||
77 | static void emit_m(ASMState *as, ARMIns ai, Reg rm) | ||
78 | { | ||
79 | *--as->mcp = ai | ARMF_M(rm); | ||
80 | } | ||
81 | |||
82 | static void emit_lsox(ASMState *as, ARMIns ai, Reg rd, Reg rn, int32_t ofs) | ||
83 | { | ||
84 | lua_assert(ofs >= -255 && ofs <= 255); | ||
85 | if (ofs < 0) ofs = -ofs; else ai |= ARMI_LS_U; | ||
86 | *--as->mcp = ai | ARMI_LS_P | ARMI_LSX_I | ARMF_D(rd) | ARMF_N(rn) | | ||
87 | ((ofs & 0xf0) << 4) | (ofs & 0x0f); | ||
88 | } | ||
89 | |||
90 | static void emit_lso(ASMState *as, ARMIns ai, Reg rd, Reg rn, int32_t ofs) | ||
91 | { | ||
92 | lua_assert(ofs >= -4095 && ofs <= 4095); | ||
93 | /* Combine LDR/STR pairs to LDRD/STRD. */ | ||
94 | if (*as->mcp == (ai|ARMI_LS_P|ARMI_LS_U|ARMF_D(rd^1)|ARMF_N(rn)|(ofs^4)) && | ||
95 | (ai & ~(ARMI_LDR^ARMI_STR)) == ARMI_STR && rd != rn && | ||
96 | (uint32_t)ofs <= 252 && !(ofs & 3) && !((rd ^ (ofs >>2)) & 1) && | ||
97 | as->mcp != as->mcloop) { | ||
98 | as->mcp++; | ||
99 | emit_lsox(as, ai == ARMI_LDR ? ARMI_LDRD : ARMI_STRD, rd&~1, rn, ofs&~4); | ||
100 | return; | ||
101 | } | ||
102 | if (ofs < 0) ofs = -ofs; else ai |= ARMI_LS_U; | ||
103 | *--as->mcp = ai | ARMI_LS_P | ARMF_D(rd) | ARMF_N(rn) | ofs; | ||
104 | } | ||
105 | |||
106 | /* -- Emit loads/stores --------------------------------------------------- */ | ||
107 | |||
108 | /* Prefer spills of BASE/L. */ | ||
109 | #define emit_canremat(ref) ((ref) < ASMREF_L) | ||
110 | |||
111 | /* Try to find a one step delta relative to another constant. */ | ||
112 | static int emit_kdelta1(ASMState *as, Reg d, int32_t i) | ||
113 | { | ||
114 | RegSet work = ~as->freeset & RSET_GPR; | ||
115 | while (work) { | ||
116 | Reg r = rset_picktop(work); | ||
117 | IRRef ref = regcost_ref(as->cost[r]); | ||
118 | lua_assert(r != d); | ||
119 | if (emit_canremat(ref)) { | ||
120 | int32_t delta = i - (ra_iskref(ref) ? ra_krefk(as, ref) : IR(ref)->i); | ||
121 | uint32_t k = emit_isk12(ARMI_ADD, delta); | ||
122 | if (k) { | ||
123 | if (k == ARMI_K12) | ||
124 | emit_dm(as, ARMI_MOV, d, r); | ||
125 | else | ||
126 | emit_dn(as, ARMI_ADD^k, d, r); | ||
127 | return 1; | ||
128 | } | ||
129 | } | ||
130 | rset_clear(work, r); | ||
131 | } | ||
132 | return 0; /* Failed. */ | ||
133 | } | ||
134 | |||
135 | /* Try to find a two step delta relative to another constant. */ | ||
136 | static int emit_kdelta2(ASMState *as, Reg d, int32_t i) | ||
137 | { | ||
138 | RegSet work = ~as->freeset & RSET_GPR; | ||
139 | while (work) { | ||
140 | Reg r = rset_picktop(work); | ||
141 | IRRef ref = regcost_ref(as->cost[r]); | ||
142 | lua_assert(r != d); | ||
143 | if (emit_canremat(ref)) { | ||
144 | int32_t delta = i - (ra_iskref(ref) ? ra_krefk(as, ref) : IR(ref)->i); | ||
145 | uint32_t sh, inv = 0, k2, k; | ||
146 | if (delta < 0) { delta = -delta; inv = ARMI_ADD^ARMI_SUB; } | ||
147 | sh = lj_ffs(delta) & ~1; | ||
148 | k2 = emit_isk12(0, delta & (255 << sh)); | ||
149 | k = emit_isk12(0, delta & ~(255 << sh)); | ||
150 | if (k) { | ||
151 | emit_dn(as, ARMI_ADD^k2^inv, d, d); | ||
152 | emit_dn(as, ARMI_ADD^k^inv, d, r); | ||
153 | return 1; | ||
154 | } | ||
155 | } | ||
156 | rset_clear(work, r); | ||
157 | } | ||
158 | return 0; /* Failed. */ | ||
159 | } | ||
160 | |||
161 | /* Load a 32 bit constant into a GPR. */ | ||
162 | static void emit_loadi(ASMState *as, Reg r, int32_t i) | ||
163 | { | ||
164 | uint32_t k = emit_isk12(ARMI_MOV, i); | ||
165 | lua_assert(rset_test(as->freeset, r) || r == RID_TMP); | ||
166 | if (k) { | ||
167 | /* Standard K12 constant. */ | ||
168 | emit_d(as, ARMI_MOV^k, r); | ||
169 | } else if ((as->flags & JIT_F_ARMV6T2) && (uint32_t)i < 0x00010000u) { | ||
170 | /* 16 bit loword constant for ARMv6T2. */ | ||
171 | emit_d(as, ARMI_MOVW|(i & 0x0fff)|((i & 0xf000)<<4), r); | ||
172 | } else if (emit_kdelta1(as, r, i)) { | ||
173 | /* One step delta relative to another constant. */ | ||
174 | } else if ((as->flags & JIT_F_ARMV6T2)) { | ||
175 | /* 32 bit hiword/loword constant for ARMv6T2. */ | ||
176 | emit_d(as, ARMI_MOVT|((i>>16) & 0x0fff)|(((i>>16) & 0xf000)<<4), r); | ||
177 | emit_d(as, ARMI_MOVW|(i & 0x0fff)|((i & 0xf000)<<4), r); | ||
178 | } else if (emit_kdelta2(as, r, i)) { | ||
179 | /* Two step delta relative to another constant. */ | ||
180 | } else { | ||
181 | /* Otherwise construct the constant with up to 4 instructions. */ | ||
182 | /* NYI: use mvn+bic, use pc-relative loads. */ | ||
183 | for (;;) { | ||
184 | uint32_t sh = lj_ffs(i) & ~1; | ||
185 | int32_t m = i & (255 << sh); | ||
186 | i &= ~(255 << sh); | ||
187 | if (i == 0) { | ||
188 | emit_d(as, ARMI_MOV ^ emit_isk12(0, m), r); | ||
189 | break; | ||
190 | } | ||
191 | emit_dn(as, ARMI_ORR ^ emit_isk12(0, m), r, r); | ||
192 | } | ||
193 | } | ||
194 | } | ||
195 | |||
196 | #define emit_loada(as, r, addr) emit_loadi(as, (r), i32ptr((addr))) | ||
197 | |||
198 | static Reg ra_allock(ASMState *as, int32_t k, RegSet allow); | ||
199 | |||
200 | /* Get/set from constant pointer. */ | ||
201 | static void emit_lsptr(ASMState *as, ARMIns ai, Reg r, void *p) | ||
202 | { | ||
203 | int32_t i = i32ptr(p); | ||
204 | emit_lso(as, ai, r, ra_allock(as, (i & ~4095), rset_exclude(RSET_GPR, r)), | ||
205 | (i & 4095)); | ||
206 | } | ||
207 | |||
208 | /* Get/set global_State fields. */ | ||
209 | #define emit_getgl(as, r, field) \ | ||
210 | emit_lsptr(as, ARMI_LDR, (r), (void *)&J2G(as->J)->field) | ||
211 | #define emit_setgl(as, r, field) \ | ||
212 | emit_lsptr(as, ARMI_STR, (r), (void *)&J2G(as->J)->field) | ||
213 | |||
214 | /* Trace number is determined from pc of exit instruction. */ | ||
215 | #define emit_setvmstate(as, i) UNUSED(i) | ||
216 | |||
217 | /* -- Emit control-flow instructions -------------------------------------- */ | ||
218 | |||
219 | /* Label for internal jumps. */ | ||
220 | typedef MCode *MCLabel; | ||
221 | |||
222 | /* Return label pointing to current PC. */ | ||
223 | #define emit_label(as) ((as)->mcp) | ||
224 | |||
225 | static void emit_branch(ASMState *as, ARMIns ai, MCode *target) | ||
226 | { | ||
227 | MCode *p = as->mcp; | ||
228 | ptrdiff_t delta = (target - p) - 1; | ||
229 | lua_assert(((delta + 0x00800000) >> 24) == 0); | ||
230 | *--p = ai | ((uint32_t)delta & 0x00ffffffu); | ||
231 | as->mcp = p; | ||
232 | } | ||
233 | |||
234 | static void emit_call(ASMState *as, void *target) | ||
235 | { | ||
236 | MCode *p = --as->mcp; | ||
237 | ptrdiff_t delta = ((char *)target - (char *)p) - 8; | ||
238 | if ((((delta>>2) + 0x00800000) >> 24) == 0) { | ||
239 | if ((delta & 1)) | ||
240 | *p = ARMI_BLX | ((uint32_t)(delta>>2) & 0x00ffffffu) | ((delta&2) << 27); | ||
241 | else | ||
242 | *p = ARMI_BL | ((uint32_t)(delta>>2) & 0x00ffffffu); | ||
243 | } else { /* Target out of range: need indirect call. But don't use R0-R3. */ | ||
244 | Reg r = ra_allock(as, i32ptr(target), RSET_RANGE(RID_R4, RID_R12+1)); | ||
245 | *p = ARMI_BLXr | ARMF_M(r); | ||
246 | } | ||
247 | } | ||
248 | |||
249 | /* -- Emit generic operations --------------------------------------------- */ | ||
250 | |||
251 | /* Generic move between two regs. */ | ||
252 | static void emit_movrr(ASMState *as, IRIns *ir, Reg dst, Reg src) | ||
253 | { | ||
254 | lua_assert(!irt_isnum(ir->t)); UNUSED(ir); | ||
255 | if (as->mcp != as->mcloop) { /* Swap early registers for loads/stores. */ | ||
256 | MCode ins = *as->mcp, swp = (src^dst); | ||
257 | if ((ins & 0x0c000000) == 0x04000000 && (ins & 0x02000010) != 0x02000010) { | ||
258 | if (!((ins ^ (dst << 16)) & 0x000f0000)) | ||
259 | *as->mcp = ins ^ (swp << 16); /* Swap N in load/store. */ | ||
260 | if (!(ins & 0x00100000) && !((ins ^ (dst << 12)) & 0x0000f000)) | ||
261 | *as->mcp = ins ^ (swp << 12); /* Swap D in store. */ | ||
262 | } | ||
263 | } | ||
264 | emit_dm(as, ARMI_MOV, dst, src); | ||
265 | } | ||
266 | |||
267 | /* Generic load of register from stack slot. */ | ||
268 | static void emit_spload(ASMState *as, IRIns *ir, Reg r, int32_t ofs) | ||
269 | { | ||
270 | lua_assert(!irt_isnum(ir->t)); UNUSED(ir); | ||
271 | emit_lso(as, ARMI_LDR, r, RID_SP, ofs); | ||
272 | } | ||
273 | |||
274 | /* Generic store of register to stack slot. */ | ||
275 | static void emit_spstore(ASMState *as, IRIns *ir, Reg r, int32_t ofs) | ||
276 | { | ||
277 | lua_assert(!irt_isnum(ir->t)); UNUSED(ir); | ||
278 | emit_lso(as, ARMI_STR, r, RID_SP, ofs); | ||
279 | } | ||
280 | |||
281 | /* Emit an arithmetic/logic operation with a constant operand. */ | ||
282 | static void emit_opk(ASMState *as, ARMIns ai, Reg dest, Reg src, | ||
283 | int32_t i, RegSet allow) | ||
284 | { | ||
285 | uint32_t k = emit_isk12(ai, i); | ||
286 | if (k) | ||
287 | emit_dn(as, ai^k, dest, src); | ||
288 | else | ||
289 | emit_dnm(as, ai, dest, src, ra_allock(as, i, allow)); | ||
290 | } | ||
291 | |||
292 | /* Add offset to pointer. */ | ||
293 | static void emit_addptr(ASMState *as, Reg r, int32_t ofs) | ||
294 | { | ||
295 | if (ofs) | ||
296 | emit_opk(as, ARMI_ADD, r, r, ofs, rset_exclude(RSET_GPR, r)); | ||
297 | } | ||
298 | |||
299 | #define emit_spsub(as, ofs) emit_addptr(as, RID_SP, -(ofs)) | ||
300 | |||
diff --git a/src/lj_target.h b/src/lj_target.h index bfa1c9f0..410ad0a0 100644 --- a/src/lj_target.h +++ b/src/lj_target.h | |||
@@ -125,6 +125,8 @@ typedef uint32_t RegCost; | |||
125 | 125 | ||
126 | #if LJ_TARGET_X86ORX64 | 126 | #if LJ_TARGET_X86ORX64 |
127 | #include "lj_target_x86.h" | 127 | #include "lj_target_x86.h" |
128 | #elif LJ_TARGET_ARM | ||
129 | #include "lj_target_arm.h" | ||
128 | #else | 130 | #else |
129 | #error "Missing include for target CPU" | 131 | #error "Missing include for target CPU" |
130 | #endif | 132 | #endif |
diff --git a/src/lj_target_arm.h b/src/lj_target_arm.h new file mode 100644 index 00000000..78a5679d --- /dev/null +++ b/src/lj_target_arm.h | |||
@@ -0,0 +1,205 @@ | |||
1 | /* | ||
2 | ** Definitions for ARM CPUs. | ||
3 | ** Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h | ||
4 | */ | ||
5 | |||
6 | #ifndef _LJ_TARGET_ARM_H | ||
7 | #define _LJ_TARGET_ARM_H | ||
8 | |||
9 | /* -- Registers IDs ------------------------------------------------------- */ | ||
10 | |||
11 | #define GPRDEF(_) \ | ||
12 | _(R0) _(R1) _(R2) _(R3) _(R4) _(R5) _(R6) _(R7) \ | ||
13 | _(R8) _(R9) _(R10) _(R11) _(R12) _(SP) _(LR) _(PC) | ||
14 | #if LJ_SOFTFP | ||
15 | #define FPRDEF(_) | ||
16 | #else | ||
17 | #error "NYI: hard-float support for ARM" | ||
18 | #endif | ||
19 | #define VRIDDEF(_) | ||
20 | |||
21 | #define RIDENUM(name) RID_##name, | ||
22 | |||
23 | enum { | ||
24 | GPRDEF(RIDENUM) /* General-purpose registers (GPRs). */ | ||
25 | FPRDEF(RIDENUM) /* Floating-point registers (FPRs). */ | ||
26 | RID_MAX, | ||
27 | RID_TMP = RID_LR, | ||
28 | |||
29 | /* Calling conventions. */ | ||
30 | RID_RET = RID_R0, | ||
31 | RID_RETHI = RID_R1, | ||
32 | RID_FPRET = RID_R0, | ||
33 | |||
34 | /* These definitions must match with the *.dasc file(s): */ | ||
35 | RID_BASE = RID_R9, /* Interpreter BASE. */ | ||
36 | RID_LPC = RID_R6, /* Interpreter PC. */ | ||
37 | RID_DISPATCH = RID_R7, /* Interpreter DISPATCH table. */ | ||
38 | RID_LREG = RID_R8, /* Interpreter L. */ | ||
39 | |||
40 | /* Register ranges [min, max) and number of registers. */ | ||
41 | RID_MIN_GPR = RID_R0, | ||
42 | RID_MAX_GPR = RID_PC+1, | ||
43 | RID_MIN_FPR = RID_MAX_GPR, | ||
44 | #if LJ_SOFTFP | ||
45 | RID_MAX_FPR = RID_MIN_FPR, | ||
46 | #else | ||
47 | #error "NYI: VFP support for ARM" | ||
48 | #endif | ||
49 | RID_NUM_GPR = RID_MAX_GPR - RID_MIN_GPR, | ||
50 | RID_NUM_FPR = RID_MAX_FPR - RID_MIN_FPR | ||
51 | }; | ||
52 | |||
53 | #define RID_NUM_KREF RID_NUM_GPR | ||
54 | #define RID_MIN_KREF RID_R0 | ||
55 | |||
56 | /* -- Register sets ------------------------------------------------------- */ | ||
57 | |||
58 | /* Make use of all registers, except sp, lr and pc. */ | ||
59 | #define RSET_GPR (RSET_RANGE(RID_MIN_GPR, RID_R12+1)) | ||
60 | #define RSET_GPREVEN \ | ||
61 | (RID2RSET(RID_R0)|RID2RSET(RID_R2)|RID2RSET(RID_R4)|RID2RSET(RID_R6)| \ | ||
62 | RID2RSET(RID_R8)|RID2RSET(RID_R10)) | ||
63 | #define RSET_GPRODD \ | ||
64 | (RID2RSET(RID_R1)|RID2RSET(RID_R3)|RID2RSET(RID_R5)|RID2RSET(RID_R7)| \ | ||
65 | RID2RSET(RID_R9)|RID2RSET(RID_R11)) | ||
66 | #if LJ_SOFTFP | ||
67 | #define RSET_FPR 0 | ||
68 | #define RSET_ALL RSET_GPR | ||
69 | #else | ||
70 | #error "NYI: VFP support for ARM" | ||
71 | #endif | ||
72 | #define RSET_INIT RSET_ALL | ||
73 | |||
74 | /* ABI-specific register sets. lr is an implicit scratch register. */ | ||
75 | #define RSET_SCRATCH_GPR_ (RSET_RANGE(RID_R0, RID_R3+1)|RID2RSET(RID_R12)) | ||
76 | #ifdef __APPLE__ | ||
77 | #define RSET_SCRATCH_GPR (RSET_SCRATCH_GPR_|RID2RSET(RID_R9)) | ||
78 | #else | ||
79 | #define RSET_SCRATCH_GPR RSET_SCRATCH_GPR_ | ||
80 | #endif | ||
81 | #if LJ_SOFTFP | ||
82 | #define RSET_SCRATCH_FPR 0 | ||
83 | #else | ||
84 | #error "NYI: VFP support for ARM" | ||
85 | #endif | ||
86 | #define RSET_SCRATCH (RSET_SCRATCH_GPR|RSET_SCRATCH_FPR) | ||
87 | #define REGARG_FIRSTGPR RID_R0 | ||
88 | #define REGARG_LASTGPR RID_R3 | ||
89 | #define REGARG_NUMGPR 4 | ||
90 | |||
91 | /* -- Spill slots --------------------------------------------------------- */ | ||
92 | |||
93 | /* Spill slots are 32 bit wide. An even/odd pair is used for FPRs. | ||
94 | ** | ||
95 | ** SPS_FIXED: Available fixed spill slots in interpreter frame. | ||
96 | ** This definition must match with the *.dasc file(s). | ||
97 | ** | ||
98 | ** SPS_FIRST: First spill slot for general use. Reserve min. two 32 bit slots. | ||
99 | */ | ||
100 | #define SPS_FIXED 2 | ||
101 | #define SPS_FIRST 2 | ||
102 | |||
103 | #define sps_scale(slot) (4 * (int32_t)(slot)) | ||
104 | #define sps_align(slot) (((slot) - SPS_FIXED + 1) & ~1) | ||
105 | |||
106 | /* -- Exit state ---------------------------------------------------------- */ | ||
107 | |||
108 | /* This definition must match with the *.dasc file(s). */ | ||
109 | typedef struct { | ||
110 | #if !LJ_SOFTFP | ||
111 | lua_Number fpr[RID_NUM_FPR]; /* Floating-point registers. */ | ||
112 | #endif | ||
113 | int32_t gpr[RID_NUM_GPR]; /* General-purpose registers. */ | ||
114 | int32_t spill[256]; /* Spill slots. */ | ||
115 | } ExitState; | ||
116 | |||
117 | /* PC after instruction that caused an exit. Used to find the trace number. */ | ||
118 | #define EXITSTATE_PCREG RID_PC | ||
119 | |||
120 | #define EXITSTUB_SPACING 4 | ||
121 | #define EXITSTUBS_PER_GROUP 32 | ||
122 | |||
123 | /* -- Instructions -------------------------------------------------------- */ | ||
124 | |||
125 | /* Instruction fields. */ | ||
126 | #define ARMF_CC(ai, cc) (((ai) ^ ARMI_CCAL) | ((cc) << 28)) | ||
127 | #define ARMF_N(r) ((r) << 16) | ||
128 | #define ARMF_D(r) ((r) << 12) | ||
129 | #define ARMF_S(r) ((r) << 8) | ||
130 | #define ARMF_M(r) (r) | ||
131 | #define ARMF_SH(sh, n) (((sh) << 5) | ((n) << 7)) | ||
132 | #define ARMF_RSH(sh, r) (0x10 | ((sh) << 5) | ARMF_S(r)) | ||
133 | |||
134 | typedef enum ARMIns { | ||
135 | ARMI_CCAL = 0xe0000000, | ||
136 | ARMI_S = 0x000100000, | ||
137 | ARMI_K12 = 0x02000000, | ||
138 | ARMI_KNEG = 0x00200000, | ||
139 | ARMI_LS_U = 0x00800000, | ||
140 | ARMI_LS_P = 0x01000000, | ||
141 | ARMI_LS_R = 0x02000000, | ||
142 | ARMI_LSX_I = 0x00400000, | ||
143 | |||
144 | ARMI_AND = 0xe0000000, | ||
145 | ARMI_EOR = 0xe0200000, | ||
146 | ARMI_SUB = 0xe0400000, | ||
147 | ARMI_RSB = 0xe0600000, | ||
148 | ARMI_ADD = 0xe0800000, | ||
149 | ARMI_ADC = 0xe0a00000, | ||
150 | ARMI_SBC = 0xe0c00000, | ||
151 | ARMI_RSC = 0xe0e00000, | ||
152 | ARMI_TST = 0xe1100000, | ||
153 | ARMI_TEQ = 0xe1300000, | ||
154 | ARMI_CMP = 0xe1500000, | ||
155 | ARMI_CMN = 0xe1700000, | ||
156 | ARMI_ORR = 0xe1800000, | ||
157 | ARMI_MOV = 0xe1a00000, | ||
158 | ARMI_BIC = 0xe1c00000, | ||
159 | ARMI_MVN = 0xe1e00000, | ||
160 | |||
161 | ARMI_NOP = 0xe1a00000, | ||
162 | |||
163 | ARMI_MUL = 0xe0000090, | ||
164 | ARMI_SMULL = 0xe0c00090, | ||
165 | |||
166 | ARMI_LDR = 0xe4100000, | ||
167 | ARMI_LDRB = 0xe4500000, | ||
168 | ARMI_LDRH = 0xe01000b0, | ||
169 | ARMI_LDRSB = 0xe01000d0, | ||
170 | ARMI_LDRSH = 0xe01000f0, | ||
171 | ARMI_LDRD = 0xe00000d0, | ||
172 | ARMI_STR = 0xe4000000, | ||
173 | ARMI_STRB = 0xe4400000, | ||
174 | ARMI_STRH = 0xe00000b0, | ||
175 | ARMI_STRD = 0xe00000f0, | ||
176 | |||
177 | ARMI_B = 0xea000000, | ||
178 | ARMI_BL = 0xeb000000, | ||
179 | ARMI_BLX = 0xfa000000, | ||
180 | ARMI_BLXr = 0xe12fff30, | ||
181 | |||
182 | /* ARMv6 */ | ||
183 | ARMI_REV = 0xe6bf0f30, | ||
184 | ARMI_SXTB = 0xe6af0070, | ||
185 | ARMI_SXTH = 0xe6bf0070, | ||
186 | ARMI_UXTB = 0xe6ef0070, | ||
187 | ARMI_UXTH = 0xe6ff0070, | ||
188 | |||
189 | /* ARMv6T2 */ | ||
190 | ARMI_MOVW = 0xe3000000, | ||
191 | ARMI_MOVT = 0xe3400000, | ||
192 | } ARMIns; | ||
193 | |||
194 | typedef enum ARMShift { | ||
195 | ARMSH_LSL, ARMSH_LSR, ARMSH_ASR, ARMSH_ROR | ||
196 | } ARMShift; | ||
197 | |||
198 | /* ARM condition codes. */ | ||
199 | typedef enum ARMCC { | ||
200 | CC_EQ, CC_NE, CC_CS, CC_CC, CC_MI, CC_PL, CC_VS, CC_VC, | ||
201 | CC_HI, CC_LS, CC_GE, CC_LT, CC_GT, CC_LE, CC_AL, | ||
202 | CC_HS = CC_CS, CC_LO = CC_CC | ||
203 | } ARMCC; | ||
204 | |||
205 | #endif | ||