diff options
author | Mike Pall <mike> | 2011-10-24 16:43:51 +0200 |
---|---|---|
committer | Mike Pall <mike> | 2011-10-24 16:43:51 +0200 |
commit | 0cf8c20be8ee009e01752874186a1dea352009fb (patch) | |
tree | 28280798d8765fa4693d41a7310ccb87e6f5d02e /src | |
parent | fa9ade356b0543f11295023de3b4441a1e7d39a3 (diff) | |
download | luajit-0cf8c20be8ee009e01752874186a1dea352009fb.tar.gz luajit-0cf8c20be8ee009e01752874186a1dea352009fb.tar.bz2 luajit-0cf8c20be8ee009e01752874186a1dea352009fb.zip |
PPC: Integrate and enable JIT compiler.
Diffstat (limited to 'src')
-rw-r--r-- | src/lj_arch.h | 1 | ||||
-rw-r--r-- | src/lj_asm.c | 6 | ||||
-rw-r--r-- | src/lj_asm_ppc.h | 2074 | ||||
-rw-r--r-- | src/lj_crecord.c | 8 | ||||
-rw-r--r-- | src/lj_emit_ppc.h | 232 | ||||
-rw-r--r-- | src/lj_target.h | 15 | ||||
-rw-r--r-- | src/lj_target_ppc.h | 273 |
7 files changed, 2603 insertions, 6 deletions
diff --git a/src/lj_arch.h b/src/lj_arch.h index f6448848..57940e94 100644 --- a/src/lj_arch.h +++ b/src/lj_arch.h | |||
@@ -163,7 +163,6 @@ | |||
163 | #define LJ_TARGET_MASKROT 1 | 163 | #define LJ_TARGET_MASKROT 1 |
164 | #define LJ_TARGET_UNIFYROT 1 /* Want only IR_BROL. */ | 164 | #define LJ_TARGET_UNIFYROT 1 /* Want only IR_BROL. */ |
165 | #define LJ_ARCH_NUMMODE LJ_NUMMODE_DUAL_SINGLE | 165 | #define LJ_ARCH_NUMMODE LJ_NUMMODE_DUAL_SINGLE |
166 | #define LJ_ARCH_NOJIT 1 | ||
167 | 166 | ||
168 | #elif LUAJIT_TARGET == LUAJIT_ARCH_PPCSPE | 167 | #elif LUAJIT_TARGET == LUAJIT_ARCH_PPCSPE |
169 | 168 | ||
diff --git a/src/lj_asm.c b/src/lj_asm.c index 7ee9fd2f..2e204239 100644 --- a/src/lj_asm.c +++ b/src/lj_asm.c | |||
@@ -161,6 +161,8 @@ IRFLDEF(FLOFS) | |||
161 | #include "lj_emit_x86.h" | 161 | #include "lj_emit_x86.h" |
162 | #elif LJ_TARGET_ARM | 162 | #elif LJ_TARGET_ARM |
163 | #include "lj_emit_arm.h" | 163 | #include "lj_emit_arm.h" |
164 | #elif LJ_TARGET_PPC | ||
165 | #include "lj_emit_ppc.h" | ||
164 | #else | 166 | #else |
165 | #error "Missing instruction emitter for target CPU" | 167 | #error "Missing instruction emitter for target CPU" |
166 | #endif | 168 | #endif |
@@ -1205,8 +1207,10 @@ static void asm_loop(ASMState *as) | |||
1205 | #include "lj_asm_x86.h" | 1207 | #include "lj_asm_x86.h" |
1206 | #elif LJ_TARGET_ARM | 1208 | #elif LJ_TARGET_ARM |
1207 | #include "lj_asm_arm.h" | 1209 | #include "lj_asm_arm.h" |
1210 | #elif LJ_TARGET_PPC | ||
1211 | #include "lj_asm_ppc.h" | ||
1208 | #else | 1212 | #else |
1209 | #error "Missing instruction emitter for target CPU" | 1213 | #error "Missing assembler for target CPU" |
1210 | #endif | 1214 | #endif |
1211 | 1215 | ||
1212 | /* -- Head of trace ------------------------------------------------------- */ | 1216 | /* -- Head of trace ------------------------------------------------------- */ |
diff --git a/src/lj_asm_ppc.h b/src/lj_asm_ppc.h new file mode 100644 index 00000000..72e4c956 --- /dev/null +++ b/src/lj_asm_ppc.h | |||
@@ -0,0 +1,2074 @@ | |||
1 | /* | ||
2 | ** PPC 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 | /* Allocate two source registers for three-operand instructions. */ | ||
22 | static Reg ra_alloc2(ASMState *as, IRIns *ir, RegSet allow) | ||
23 | { | ||
24 | IRIns *irl = IR(ir->op1), *irr = IR(ir->op2); | ||
25 | Reg left = irl->r, right = irr->r; | ||
26 | if (ra_hasreg(left)) { | ||
27 | ra_noweak(as, left); | ||
28 | if (ra_noreg(right)) | ||
29 | right = ra_allocref(as, ir->op2, rset_exclude(allow, left)); | ||
30 | else | ||
31 | ra_noweak(as, right); | ||
32 | } else if (ra_hasreg(right)) { | ||
33 | ra_noweak(as, right); | ||
34 | left = ra_allocref(as, ir->op1, rset_exclude(allow, right)); | ||
35 | } else if (ra_hashint(right)) { | ||
36 | right = ra_allocref(as, ir->op2, allow); | ||
37 | left = ra_alloc1(as, ir->op1, rset_exclude(allow, right)); | ||
38 | } else { | ||
39 | left = ra_allocref(as, ir->op1, allow); | ||
40 | right = ra_alloc1(as, ir->op2, rset_exclude(allow, left)); | ||
41 | } | ||
42 | return left | (right << 8); | ||
43 | } | ||
44 | |||
45 | /* -- Guard handling ------------------------------------------------------ */ | ||
46 | |||
47 | /* Setup exit stubs after the end of each trace. */ | ||
48 | static void asm_exitstub_setup(ASMState *as, ExitNo nexits) | ||
49 | { | ||
50 | ExitNo i; | ||
51 | MCode *mxp = as->mctop; | ||
52 | /* 1: mflr r0; bl ->vm_exit_handler; li r0, traceno; bl <1; bl <1; ... */ | ||
53 | for (i = nexits-1; (int32_t)i >= 0; i--) | ||
54 | *--mxp = PPCI_BL|(((-3-i)&0x00ffffffu)<<2); | ||
55 | *--mxp = PPCI_LI|PPCF_T(RID_TMP)|as->T->traceno; /* Read by exit handler. */ | ||
56 | mxp--; | ||
57 | *mxp = PPCI_BL|((((MCode *)(void *)lj_vm_exit_handler-mxp)&0x00ffffffu)<<2); | ||
58 | *--mxp = PPCI_MFLR|PPCF_T(RID_TMP); | ||
59 | as->mctop = mxp; | ||
60 | } | ||
61 | |||
62 | static MCode *asm_exitstub_addr(ASMState *as, ExitNo exitno) | ||
63 | { | ||
64 | /* Keep this in-sync with exitstub_trace_addr(). */ | ||
65 | return as->mctop + exitno + 3; | ||
66 | } | ||
67 | |||
68 | /* Emit conditional branch to exit for guard. */ | ||
69 | static void asm_guardcc(ASMState *as, PPCCC cc) | ||
70 | { | ||
71 | MCode *target = asm_exitstub_addr(as, as->snapno); | ||
72 | MCode *p = as->mcp; | ||
73 | if (LJ_UNLIKELY(p == as->invmcp)) { | ||
74 | as->loopinv = 1; | ||
75 | *p = PPCI_B | (((target-p) & 0x00ffffffu) << 2); | ||
76 | emit_condbranch(as, PPCI_BC, cc^4, p); | ||
77 | return; | ||
78 | } | ||
79 | emit_condbranch(as, PPCI_BC, cc, target); | ||
80 | } | ||
81 | |||
82 | /* -- Operand fusion ------------------------------------------------------ */ | ||
83 | |||
84 | /* Limit linear search to this distance. Avoids O(n^2) behavior. */ | ||
85 | #define CONFLICT_SEARCH_LIM 31 | ||
86 | |||
87 | /* Check if there's no conflicting instruction between curins and ref. */ | ||
88 | static int noconflict(ASMState *as, IRRef ref, IROp conflict) | ||
89 | { | ||
90 | IRIns *ir = as->ir; | ||
91 | IRRef i = as->curins; | ||
92 | if (i > ref + CONFLICT_SEARCH_LIM) | ||
93 | return 0; /* Give up, ref is too far away. */ | ||
94 | while (--i > ref) | ||
95 | if (ir[i].o == conflict) | ||
96 | return 0; /* Conflict found. */ | ||
97 | return 1; /* Ok, no conflict. */ | ||
98 | } | ||
99 | |||
100 | /* Fuse the array base of colocated arrays. */ | ||
101 | static int32_t asm_fuseabase(ASMState *as, IRRef ref) | ||
102 | { | ||
103 | IRIns *ir = IR(ref); | ||
104 | if (ir->o == IR_TNEW && ir->op1 <= LJ_MAX_COLOSIZE && | ||
105 | !neverfuse(as) && noconflict(as, ref, IR_NEWREF)) | ||
106 | return (int32_t)sizeof(GCtab); | ||
107 | return 0; | ||
108 | } | ||
109 | |||
110 | /* Indicates load/store indexed is ok. */ | ||
111 | #define AHUREF_LSX ((int32_t)0x80000000) | ||
112 | |||
113 | /* Fuse array/hash/upvalue reference into register+offset operand. */ | ||
114 | static Reg asm_fuseahuref(ASMState *as, IRRef ref, int32_t *ofsp, RegSet allow) | ||
115 | { | ||
116 | IRIns *ir = IR(ref); | ||
117 | if (ra_noreg(ir->r)) { | ||
118 | if (ir->o == IR_AREF) { | ||
119 | if (mayfuse(as, ref)) { | ||
120 | if (irref_isk(ir->op2)) { | ||
121 | IRRef tab = IR(ir->op1)->op1; | ||
122 | int32_t ofs = asm_fuseabase(as, tab); | ||
123 | IRRef refa = ofs ? tab : ir->op1; | ||
124 | ofs += 8*IR(ir->op2)->i; | ||
125 | if (checki16(ofs)) { | ||
126 | *ofsp = ofs; | ||
127 | return ra_alloc1(as, refa, allow); | ||
128 | } | ||
129 | } | ||
130 | if (*ofsp == AHUREF_LSX) { | ||
131 | Reg base = ra_alloc1(as, ir->op1, allow); | ||
132 | Reg idx = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, base)); | ||
133 | return base | (idx << 8); | ||
134 | } | ||
135 | } | ||
136 | } else if (ir->o == IR_HREFK) { | ||
137 | if (mayfuse(as, ref)) { | ||
138 | int32_t ofs = (int32_t)(IR(ir->op2)->op2 * sizeof(Node)); | ||
139 | if (checki16(ofs)) { | ||
140 | *ofsp = ofs; | ||
141 | return ra_alloc1(as, ir->op1, allow); | ||
142 | } | ||
143 | } | ||
144 | } else if (ir->o == IR_UREFC) { | ||
145 | if (irref_isk(ir->op1)) { | ||
146 | GCfunc *fn = ir_kfunc(IR(ir->op1)); | ||
147 | int32_t ofs = i32ptr(&gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.tv); | ||
148 | int32_t jgl = (intptr_t)J2G(as->J); | ||
149 | if ((uint32_t)(ofs-jgl) < 65536) { | ||
150 | *ofsp = ofs-jgl-32768; | ||
151 | return RID_JGL; | ||
152 | } else { | ||
153 | *ofsp = (int16_t)ofs; | ||
154 | return ra_allock(as, ofs-(int16_t)ofs, allow); | ||
155 | } | ||
156 | } | ||
157 | } | ||
158 | } | ||
159 | *ofsp = 0; | ||
160 | return ra_alloc1(as, ref, allow); | ||
161 | } | ||
162 | |||
163 | /* Fuse XLOAD/XSTORE reference into load/store operand. */ | ||
164 | static void asm_fusexref(ASMState *as, PPCIns pi, Reg rt, IRRef ref, | ||
165 | RegSet allow) | ||
166 | { | ||
167 | IRIns *ir = IR(ref); | ||
168 | int32_t ofs = 0; | ||
169 | Reg base; | ||
170 | if (ra_noreg(ir->r) && mayfuse(as, ref)) { | ||
171 | if (ir->o == IR_ADD) { | ||
172 | if (irref_isk(ir->op2) && (ofs = IR(ir->op2)->i, checki16(ofs))) { | ||
173 | ref = ir->op1; | ||
174 | } else { | ||
175 | Reg right, left = ra_alloc2(as, ir, allow); | ||
176 | right = (left >> 8); left &= 255; | ||
177 | emit_fab(as, PPCI_LWZX | ((pi >> 20) & 0x780), rt, left, right); | ||
178 | return; | ||
179 | } | ||
180 | } else if (ir->o == IR_STRREF) { | ||
181 | ofs = (int32_t)sizeof(GCstr); | ||
182 | if (irref_isk(ir->op2)) { | ||
183 | ofs += IR(ir->op2)->i; | ||
184 | ref = ir->op1; | ||
185 | } else if (irref_isk(ir->op1)) { | ||
186 | ofs += IR(ir->op1)->i; | ||
187 | ref = ir->op2; | ||
188 | } else { | ||
189 | /* NYI: Fuse ADD with constant. */ | ||
190 | Reg right, left = ra_alloc2(as, ir, allow); | ||
191 | right = (left >> 8); left &= 255; | ||
192 | emit_fai(as, pi, rt, rt, ofs); | ||
193 | emit_tab(as, PPCI_ADD, rt, left, right); | ||
194 | return; | ||
195 | } | ||
196 | if (!checki16(ofs)) { | ||
197 | Reg left = ra_alloc1(as, ref, allow); | ||
198 | Reg right = ra_allock(as, ofs, rset_exclude(allow, left)); | ||
199 | emit_fab(as, PPCI_LWZX | ((pi >> 20) & 0x780), rt, left, right); | ||
200 | return; | ||
201 | } | ||
202 | } | ||
203 | } | ||
204 | base = ra_alloc1(as, ref, allow); | ||
205 | emit_fai(as, pi, rt, base, ofs); | ||
206 | } | ||
207 | |||
208 | /* Fuse to multiply-add/sub instruction. */ | ||
209 | static int asm_fusemadd(ASMState *as, IRIns *ir, PPCIns pi, PPCIns pir) | ||
210 | { | ||
211 | IRRef lref = ir->op1, rref = ir->op2; | ||
212 | IRIns *irm; | ||
213 | if (lref != rref && | ||
214 | ((mayfuse(as, lref) && (irm = IR(lref), irm->o == IR_MUL) && | ||
215 | ra_noreg(irm->r)) || | ||
216 | (mayfuse(as, rref) && (irm = IR(rref), irm->o == IR_MUL) && | ||
217 | (rref = lref, pi = pir, ra_noreg(irm->r))))) { | ||
218 | Reg dest = ra_dest(as, ir, RSET_FPR); | ||
219 | Reg add = ra_alloc1(as, rref, RSET_FPR); | ||
220 | Reg right, left = ra_alloc2(as, irm, rset_exclude(RSET_FPR, add)); | ||
221 | right = (left >> 8); left &= 255; | ||
222 | emit_facb(as, pi, dest, left, right, add); | ||
223 | return 1; | ||
224 | } | ||
225 | return 0; | ||
226 | } | ||
227 | |||
228 | /* -- Calls --------------------------------------------------------------- */ | ||
229 | |||
230 | /* Generate a call to a C function. */ | ||
231 | static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args) | ||
232 | { | ||
233 | uint32_t n, nargs = CCI_NARGS(ci); | ||
234 | int32_t ofs = 8; | ||
235 | Reg gpr = REGARG_FIRSTGPR, fpr = REGARG_FIRSTFPR; | ||
236 | if ((void *)ci->func) | ||
237 | emit_call(as, (void *)ci->func); | ||
238 | for (n = 0; n < nargs; n++) { /* Setup args. */ | ||
239 | IRRef ref = args[n]; | ||
240 | if (ref) { | ||
241 | IRIns *ir = IR(ref); | ||
242 | if (irt_isfp(ir->t)) { | ||
243 | if (fpr <= REGARG_LASTFPR) { | ||
244 | lua_assert(rset_test(as->freeset, fpr)); /* Already evicted. */ | ||
245 | ra_leftov(as, fpr, ref); | ||
246 | fpr++; | ||
247 | } else { | ||
248 | Reg r = ra_alloc1(as, ref, RSET_FPR); | ||
249 | if (irt_isnum(ir->t)) ofs = (ofs + 4) & ~4; | ||
250 | emit_spstore(as, ir, r, ofs); | ||
251 | ofs += irt_isnum(ir->t) ? 8 : 4; | ||
252 | } | ||
253 | } else { | ||
254 | if (gpr <= REGARG_LASTGPR) { | ||
255 | lua_assert(rset_test(as->freeset, gpr)); /* Already evicted. */ | ||
256 | ra_leftov(as, gpr, ref); | ||
257 | gpr++; | ||
258 | } else { | ||
259 | Reg r = ra_alloc1(as, ref, RSET_GPR); | ||
260 | emit_spstore(as, ir, r, ofs); | ||
261 | ofs += 4; | ||
262 | } | ||
263 | } | ||
264 | } else { | ||
265 | if (gpr <= REGARG_LASTGPR) | ||
266 | gpr++; | ||
267 | else | ||
268 | ofs += 4; | ||
269 | } | ||
270 | } | ||
271 | } | ||
272 | |||
273 | /* Setup result reg/sp for call. Evict scratch regs. */ | ||
274 | static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci) | ||
275 | { | ||
276 | RegSet drop = RSET_SCRATCH; | ||
277 | int hiop = ((ir+1)->o == IR_HIOP); | ||
278 | if ((ci->flags & CCI_NOFPRCLOBBER)) | ||
279 | drop &= ~RSET_FPR; | ||
280 | if (ra_hasreg(ir->r)) | ||
281 | rset_clear(drop, ir->r); /* Dest reg handled below. */ | ||
282 | if (hiop && ra_hasreg((ir+1)->r)) | ||
283 | rset_clear(drop, (ir+1)->r); /* Dest reg handled below. */ | ||
284 | ra_evictset(as, drop); /* Evictions must be performed first. */ | ||
285 | if (ra_used(ir)) { | ||
286 | lua_assert(!irt_ispri(ir->t)); | ||
287 | if (irt_isfp(ir->t)) { | ||
288 | if ((ci->flags & CCI_CASTU64)) { | ||
289 | /* Use spill slot or temp slots. */ | ||
290 | int32_t ofs = ir->s ? sps_scale(ir->s) : SPOFS_TMP; | ||
291 | Reg dest = ir->r; | ||
292 | if (ra_hasreg(dest)) { | ||
293 | ra_free(as, dest); | ||
294 | ra_modified(as, dest); | ||
295 | emit_fai(as, PPCI_LFD, dest, RID_SP, ofs); | ||
296 | } | ||
297 | emit_tai(as, PPCI_STW, RID_RETHI, RID_SP, ofs); | ||
298 | emit_tai(as, PPCI_STW, RID_RETLO, RID_SP, ofs+4); | ||
299 | } else { | ||
300 | ra_destreg(as, ir, RID_FPRET); | ||
301 | } | ||
302 | } else if (hiop) { | ||
303 | ra_destpair(as, ir); | ||
304 | } else { | ||
305 | ra_destreg(as, ir, RID_RET); | ||
306 | } | ||
307 | } | ||
308 | } | ||
309 | |||
310 | static void asm_call(ASMState *as, IRIns *ir) | ||
311 | { | ||
312 | IRRef args[CCI_NARGS_MAX]; | ||
313 | const CCallInfo *ci = &lj_ir_callinfo[ir->op2]; | ||
314 | asm_collectargs(as, ir, ci, args); | ||
315 | asm_setupresult(as, ir, ci); | ||
316 | asm_gencall(as, ci, args); | ||
317 | } | ||
318 | |||
319 | static void asm_callx(ASMState *as, IRIns *ir) | ||
320 | { | ||
321 | IRRef args[CCI_NARGS_MAX]; | ||
322 | CCallInfo ci; | ||
323 | ci.flags = asm_callx_flags(as, ir); | ||
324 | asm_collectargs(as, ir, &ci, args); | ||
325 | asm_setupresult(as, ir, &ci); | ||
326 | if (irref_isk(ir->op2)) { /* Call to constant address. */ | ||
327 | ci.func = (ASMFunction)(void *)(IR(ir->op2)->i); | ||
328 | } else { /* Need a non-argument register for indirect calls. */ | ||
329 | RegSet allow = RSET_GPR & ~RSET_RANGE(RID_R0, REGARG_LASTGPR+1); | ||
330 | Reg freg = ra_alloc1(as, ir->op2, allow); | ||
331 | *--as->mcp = PPCI_BCTRL; | ||
332 | *--as->mcp = PPCI_MTCTR | PPCF_T(freg); | ||
333 | ci.func = (ASMFunction)(void *)0; | ||
334 | } | ||
335 | asm_gencall(as, &ci, args); | ||
336 | } | ||
337 | |||
338 | static void asm_callid(ASMState *as, IRIns *ir, IRCallID id) | ||
339 | { | ||
340 | const CCallInfo *ci = &lj_ir_callinfo[id]; | ||
341 | IRRef args[2]; | ||
342 | args[0] = ir->op1; | ||
343 | args[1] = ir->op2; | ||
344 | asm_setupresult(as, ir, ci); | ||
345 | asm_gencall(as, ci, args); | ||
346 | } | ||
347 | |||
348 | /* -- Returns ------------------------------------------------------------- */ | ||
349 | |||
350 | /* Return to lower frame. Guard that it goes to the right spot. */ | ||
351 | static void asm_retf(ASMState *as, IRIns *ir) | ||
352 | { | ||
353 | Reg base = ra_alloc1(as, REF_BASE, RSET_GPR); | ||
354 | void *pc = ir_kptr(IR(ir->op2)); | ||
355 | int32_t delta = 1+bc_a(*((const BCIns *)pc - 1)); | ||
356 | as->topslot -= (BCReg)delta; | ||
357 | if ((int32_t)as->topslot < 0) as->topslot = 0; | ||
358 | emit_setgl(as, base, jit_base); | ||
359 | emit_addptr(as, base, -8*delta); | ||
360 | asm_guardcc(as, CC_NE); | ||
361 | emit_ab(as, PPCI_CMPW, RID_TMP, | ||
362 | ra_allock(as, i32ptr(pc), rset_exclude(RSET_GPR, base))); | ||
363 | emit_tai(as, PPCI_LWZ, RID_TMP, base, -8); | ||
364 | } | ||
365 | |||
366 | /* -- Type conversions ---------------------------------------------------- */ | ||
367 | |||
368 | static void asm_tointg(ASMState *as, IRIns *ir, Reg left) | ||
369 | { | ||
370 | RegSet allow = RSET_FPR; | ||
371 | Reg tmp = ra_scratch(as, rset_clear(allow, left)); | ||
372 | Reg fbias = ra_scratch(as, rset_clear(allow, tmp)); | ||
373 | Reg dest = ra_dest(as, ir, RSET_GPR); | ||
374 | Reg hibias = ra_allock(as, 0x43300000, rset_exclude(RSET_GPR, dest)); | ||
375 | asm_guardcc(as, CC_NE); | ||
376 | emit_fab(as, PPCI_FCMPU, 0, tmp, left); | ||
377 | emit_fab(as, PPCI_FSUB, tmp, tmp, fbias); | ||
378 | emit_fai(as, PPCI_LFD, tmp, RID_SP, SPOFS_TMP); | ||
379 | emit_tai(as, PPCI_STW, RID_TMP, RID_SP, SPOFS_TMPLO); | ||
380 | emit_tai(as, PPCI_STW, hibias, RID_SP, SPOFS_TMPHI); | ||
381 | emit_asi(as, PPCI_XORIS, RID_TMP, dest, 0x8000); | ||
382 | emit_tai(as, PPCI_LWZ, dest, RID_SP, SPOFS_TMPLO); | ||
383 | emit_lsptr(as, PPCI_LFS, (fbias & 31), | ||
384 | (void *)lj_ir_k64_find(as->J, U64x(59800004,59800000)), | ||
385 | RSET_GPR); | ||
386 | emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP); | ||
387 | emit_fb(as, PPCI_FCTIWZ, tmp, left); | ||
388 | } | ||
389 | |||
390 | static void asm_tobit(ASMState *as, IRIns *ir) | ||
391 | { | ||
392 | RegSet allow = RSET_FPR; | ||
393 | Reg dest = ra_dest(as, ir, RSET_GPR); | ||
394 | Reg left = ra_alloc1(as, ir->op1, allow); | ||
395 | Reg right = ra_alloc1(as, ir->op2, rset_clear(allow, left)); | ||
396 | Reg tmp = ra_scratch(as, rset_clear(allow, right)); | ||
397 | emit_tai(as, PPCI_LWZ, dest, RID_SP, SPOFS_TMPLO); | ||
398 | emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP); | ||
399 | emit_fab(as, PPCI_FADD, tmp, left, right); | ||
400 | } | ||
401 | |||
402 | static void asm_conv(ASMState *as, IRIns *ir) | ||
403 | { | ||
404 | IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK); | ||
405 | int stfp = (st == IRT_NUM || st == IRT_FLOAT); | ||
406 | IRRef lref = ir->op1; | ||
407 | lua_assert(irt_type(ir->t) != st); | ||
408 | lua_assert(!(irt_isint64(ir->t) || | ||
409 | (st == IRT_I64 || st == IRT_U64))); /* Handled by SPLIT. */ | ||
410 | if (irt_isfp(ir->t)) { | ||
411 | Reg dest = ra_dest(as, ir, RSET_FPR); | ||
412 | if (stfp) { /* FP to FP conversion. */ | ||
413 | if (st == IRT_NUM) /* double -> float conversion. */ | ||
414 | emit_fb(as, PPCI_FRSP, dest, ra_alloc1(as, lref, RSET_FPR)); | ||
415 | else /* float -> double conversion is a no-op on PPC. */ | ||
416 | ra_leftov(as, dest, lref); /* Do nothing, but may need to move regs. */ | ||
417 | } else { /* Integer to FP conversion. */ | ||
418 | /* IRT_INT: Flip hibit, bias with 2^52, subtract 2^52+2^31. */ | ||
419 | /* IRT_U32: Bias with 2^52, subtract 2^52. */ | ||
420 | RegSet allow = RSET_GPR; | ||
421 | Reg left = ra_alloc1(as, lref, allow); | ||
422 | Reg hibias = ra_allock(as, 0x43300000, rset_clear(allow, left)); | ||
423 | Reg fbias = ra_scratch(as, rset_exclude(RSET_FPR, dest)); | ||
424 | const float *kbias; | ||
425 | if (irt_isfloat(ir->t)) emit_fb(as, PPCI_FRSP, dest, dest); | ||
426 | emit_fab(as, PPCI_FSUB, dest, dest, fbias); | ||
427 | emit_fai(as, PPCI_LFD, dest, RID_SP, SPOFS_TMP); | ||
428 | kbias = (const float *)lj_ir_k64_find(as->J, U64x(59800004,59800000)); | ||
429 | if (st == IRT_U32) kbias++; | ||
430 | emit_lsptr(as, PPCI_LFS, (fbias & 31), (void *)kbias, | ||
431 | rset_clear(allow, hibias)); | ||
432 | emit_tai(as, PPCI_STW, st == IRT_U32 ? left : RID_TMP, | ||
433 | RID_SP, SPOFS_TMPLO); | ||
434 | emit_tai(as, PPCI_STW, hibias, RID_SP, SPOFS_TMPHI); | ||
435 | if (st != IRT_U32) emit_asi(as, PPCI_XORIS, RID_TMP, left, 0x8000); | ||
436 | } | ||
437 | } else if (stfp) { /* FP to integer conversion. */ | ||
438 | if (irt_isguard(ir->t)) { | ||
439 | /* Checked conversions are only supported from number to int. */ | ||
440 | lua_assert(irt_isint(ir->t) && st == IRT_NUM); | ||
441 | asm_tointg(as, ir, ra_alloc1(as, lref, RSET_FPR)); | ||
442 | } else { | ||
443 | Reg dest = ra_dest(as, ir, RSET_GPR); | ||
444 | Reg left = ra_alloc1(as, lref, RSET_FPR); | ||
445 | Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left)); | ||
446 | if (irt_isu32(ir->t)) { | ||
447 | /* Convert both x and x-2^31 to int and merge results. */ | ||
448 | Reg tmpi = ra_scratch(as, rset_exclude(RSET_GPR, dest)); | ||
449 | emit_asb(as, PPCI_OR, dest, dest, tmpi); /* Select with mask idiom. */ | ||
450 | emit_asb(as, PPCI_AND, tmpi, tmpi, RID_TMP); | ||
451 | emit_asb(as, PPCI_ANDC, dest, dest, RID_TMP); | ||
452 | emit_tai(as, PPCI_LWZ, tmpi, RID_SP, SPOFS_TMPLO); /* tmp = (int)(x) */ | ||
453 | emit_tai(as, PPCI_ADDIS, dest, dest, 0x8000); /* dest += 2^31 */ | ||
454 | emit_asb(as, PPCI_SRAWI, RID_TMP, dest, 31); /* mask = -(dest < 0) */ | ||
455 | emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP); | ||
456 | emit_tai(as, PPCI_LWZ, dest, | ||
457 | RID_SP, SPOFS_TMPLO); /* dest = (int)(x-2^31) */ | ||
458 | emit_fb(as, PPCI_FCTIWZ, tmp, left); | ||
459 | emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP); | ||
460 | emit_fb(as, PPCI_FCTIWZ, tmp, tmp); | ||
461 | emit_fab(as, PPCI_FSUB, tmp, left, tmp); | ||
462 | emit_lsptr(as, PPCI_LFS, (tmp & 31), | ||
463 | (void *)lj_ir_k64_find(as->J, U64x(4f000000,00000000)), | ||
464 | RSET_GPR); | ||
465 | } else { | ||
466 | emit_tai(as, PPCI_LWZ, dest, RID_SP, SPOFS_TMPLO); | ||
467 | emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP); | ||
468 | emit_fb(as, PPCI_FCTIWZ, tmp, left); | ||
469 | } | ||
470 | } | ||
471 | } else { | ||
472 | Reg dest = ra_dest(as, ir, RSET_GPR); | ||
473 | if (st >= IRT_I8 && st <= IRT_U16) { /* Extend to 32 bit integer. */ | ||
474 | Reg left = ra_alloc1(as, ir->op1, RSET_GPR); | ||
475 | lua_assert(irt_isint(ir->t) || irt_isu32(ir->t)); | ||
476 | if ((ir->op2 & IRCONV_SEXT)) | ||
477 | emit_as(as, st == IRT_I8 ? PPCI_EXTSB : PPCI_EXTSH, dest, left); | ||
478 | else | ||
479 | emit_rot(as, PPCI_RLWINM, dest, left, 0, st == IRT_U8 ? 24 : 16, 31); | ||
480 | } else { /* 32/64 bit integer conversions. */ | ||
481 | /* Only need to handle 32/32 bit no-op (cast) on 32 bit archs. */ | ||
482 | ra_leftov(as, dest, lref); /* Do nothing, but may need to move regs. */ | ||
483 | } | ||
484 | } | ||
485 | } | ||
486 | |||
487 | #if LJ_HASFFI | ||
488 | static void asm_conv64(ASMState *as, IRIns *ir) | ||
489 | { | ||
490 | IRType st = (IRType)((ir-1)->op2 & IRCONV_SRCMASK); | ||
491 | IRType dt = (((ir-1)->op2 & IRCONV_DSTMASK) >> IRCONV_DSH); | ||
492 | IRCallID id; | ||
493 | const CCallInfo *ci; | ||
494 | IRRef args[2]; | ||
495 | args[0] = ir->op1; | ||
496 | args[1] = (ir-1)->op1; | ||
497 | if (st == IRT_NUM || st == IRT_FLOAT) { | ||
498 | id = IRCALL_fp64_d2l + ((st == IRT_FLOAT) ? 2 : 0) + (dt - IRT_I64); | ||
499 | ir--; | ||
500 | } else { | ||
501 | id = IRCALL_fp64_l2d + ((dt == IRT_FLOAT) ? 2 : 0) + (st - IRT_I64); | ||
502 | } | ||
503 | ci = &lj_ir_callinfo[id]; | ||
504 | asm_setupresult(as, ir, ci); | ||
505 | asm_gencall(as, ci, args); | ||
506 | } | ||
507 | #endif | ||
508 | |||
509 | static void asm_strto(ASMState *as, IRIns *ir) | ||
510 | { | ||
511 | const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_tonum]; | ||
512 | IRRef args[2]; | ||
513 | int32_t ofs; | ||
514 | RegSet drop = RSET_SCRATCH; | ||
515 | if (ra_hasreg(ir->r)) rset_set(drop, ir->r); /* Spill dest reg (if any). */ | ||
516 | ra_evictset(as, drop); | ||
517 | asm_guardcc(as, CC_EQ); | ||
518 | emit_ai(as, PPCI_CMPWI, RID_RET, 0); /* Test return status. */ | ||
519 | args[0] = ir->op1; /* GCstr *str */ | ||
520 | args[1] = ASMREF_TMP1; /* TValue *n */ | ||
521 | asm_gencall(as, ci, args); | ||
522 | /* Store the result to the spill slot or temp slots. */ | ||
523 | ofs = ir->s ? sps_scale(ir->s) : SPOFS_TMP; | ||
524 | emit_tai(as, PPCI_ADDI, ra_releasetmp(as, ASMREF_TMP1), RID_SP, ofs); | ||
525 | } | ||
526 | |||
527 | /* Get pointer to TValue. */ | ||
528 | static void asm_tvptr(ASMState *as, Reg dest, IRRef ref) | ||
529 | { | ||
530 | IRIns *ir = IR(ref); | ||
531 | if (irt_isnum(ir->t)) { | ||
532 | if (irref_isk(ref)) /* Use the number constant itself as a TValue. */ | ||
533 | ra_allockreg(as, i32ptr(ir_knum(ir)), dest); | ||
534 | else /* Otherwise force a spill and use the spill slot. */ | ||
535 | emit_tai(as, PPCI_ADDI, dest, RID_SP, ra_spill(as, ir)); | ||
536 | } else { | ||
537 | /* Otherwise use g->tmptv to hold the TValue. */ | ||
538 | RegSet allow = rset_exclude(RSET_GPR, dest); | ||
539 | Reg type; | ||
540 | emit_tai(as, PPCI_ADDI, dest, RID_JGL, offsetof(global_State, tmptv)-32768); | ||
541 | if (!irt_ispri(ir->t)) { | ||
542 | Reg src = ra_alloc1(as, ref, allow); | ||
543 | emit_setgl(as, src, tmptv.gcr); | ||
544 | } | ||
545 | type = ra_allock(as, irt_toitype(ir->t), allow); | ||
546 | emit_setgl(as, type, tmptv.it); | ||
547 | } | ||
548 | } | ||
549 | |||
550 | static void asm_tostr(ASMState *as, IRIns *ir) | ||
551 | { | ||
552 | IRRef args[2]; | ||
553 | args[0] = ASMREF_L; | ||
554 | as->gcsteps++; | ||
555 | if (irt_isnum(IR(ir->op1)->t) || (ir+1)->o == IR_HIOP) { | ||
556 | const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_fromnum]; | ||
557 | args[1] = ASMREF_TMP1; /* const lua_Number * */ | ||
558 | asm_setupresult(as, ir, ci); /* GCstr * */ | ||
559 | asm_gencall(as, ci, args); | ||
560 | asm_tvptr(as, ra_releasetmp(as, ASMREF_TMP1), ir->op1); | ||
561 | } else { | ||
562 | const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_fromint]; | ||
563 | args[1] = ir->op1; /* int32_t k */ | ||
564 | asm_setupresult(as, ir, ci); /* GCstr * */ | ||
565 | asm_gencall(as, ci, args); | ||
566 | } | ||
567 | } | ||
568 | |||
569 | /* -- Memory references --------------------------------------------------- */ | ||
570 | |||
571 | static void asm_aref(ASMState *as, IRIns *ir) | ||
572 | { | ||
573 | Reg dest = ra_dest(as, ir, RSET_GPR); | ||
574 | Reg idx, base; | ||
575 | if (irref_isk(ir->op2)) { | ||
576 | IRRef tab = IR(ir->op1)->op1; | ||
577 | int32_t ofs = asm_fuseabase(as, tab); | ||
578 | IRRef refa = ofs ? tab : ir->op1; | ||
579 | ofs += 8*IR(ir->op2)->i; | ||
580 | if (checki16(ofs)) { | ||
581 | base = ra_alloc1(as, refa, RSET_GPR); | ||
582 | emit_tai(as, PPCI_ADDI, dest, base, ofs); | ||
583 | return; | ||
584 | } | ||
585 | } | ||
586 | base = ra_alloc1(as, ir->op1, RSET_GPR); | ||
587 | idx = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, base)); | ||
588 | emit_tab(as, PPCI_ADD, dest, RID_TMP, base); | ||
589 | emit_slwi(as, RID_TMP, idx, 3); | ||
590 | } | ||
591 | |||
592 | /* Inlined hash lookup. Specialized for key type and for const keys. | ||
593 | ** The equivalent C code is: | ||
594 | ** Node *n = hashkey(t, key); | ||
595 | ** do { | ||
596 | ** if (lj_obj_equal(&n->key, key)) return &n->val; | ||
597 | ** } while ((n = nextnode(n))); | ||
598 | ** return niltv(L); | ||
599 | */ | ||
600 | static void asm_href(ASMState *as, IRIns *ir, IROp merge) | ||
601 | { | ||
602 | RegSet allow = RSET_GPR; | ||
603 | int destused = ra_used(ir); | ||
604 | Reg dest = ra_dest(as, ir, allow); | ||
605 | Reg tab = ra_alloc1(as, ir->op1, rset_clear(allow, dest)); | ||
606 | Reg key = RID_NONE, tmp1 = RID_TMP, tmp2; | ||
607 | Reg tisnum = RID_NONE, tmpnum = RID_NONE; | ||
608 | IRRef refkey = ir->op2; | ||
609 | IRIns *irkey = IR(refkey); | ||
610 | IRType1 kt = irkey->t; | ||
611 | uint32_t khash; | ||
612 | MCLabel l_end, l_loop, l_next; | ||
613 | |||
614 | rset_clear(allow, tab); | ||
615 | if (irt_isnum(kt)) { | ||
616 | key = ra_alloc1(as, refkey, RSET_FPR); | ||
617 | tmpnum = ra_scratch(as, rset_exclude(RSET_FPR, key)); | ||
618 | tisnum = ra_allock(as, (int32_t)LJ_TISNUM, allow); | ||
619 | rset_clear(allow, tisnum); | ||
620 | } else if (!irt_ispri(kt)) { | ||
621 | key = ra_alloc1(as, refkey, allow); | ||
622 | rset_clear(allow, key); | ||
623 | } | ||
624 | tmp2 = ra_scratch(as, allow); | ||
625 | rset_clear(allow, tmp2); | ||
626 | |||
627 | /* Key not found in chain: jump to exit (if merged) or load niltv. */ | ||
628 | l_end = emit_label(as); | ||
629 | as->invmcp = NULL; | ||
630 | if (merge == IR_NE) | ||
631 | asm_guardcc(as, CC_EQ); | ||
632 | else if (destused) | ||
633 | emit_loada(as, dest, niltvg(J2G(as->J))); | ||
634 | |||
635 | /* Follow hash chain until the end. */ | ||
636 | l_loop = --as->mcp; | ||
637 | emit_ai(as, PPCI_CMPWI, dest, 0); | ||
638 | emit_tai(as, PPCI_LWZ, dest, dest, (int32_t)offsetof(Node, next)); | ||
639 | l_next = emit_label(as); | ||
640 | |||
641 | /* Type and value comparison. */ | ||
642 | if (merge == IR_EQ) | ||
643 | asm_guardcc(as, CC_EQ); | ||
644 | else | ||
645 | emit_condbranch(as, PPCI_BC|PPCF_Y, CC_EQ, l_end); | ||
646 | if (irt_isnum(kt)) { | ||
647 | emit_fab(as, PPCI_FCMPU, 0, tmpnum, key); | ||
648 | emit_condbranch(as, PPCI_BC, CC_GE, l_next); | ||
649 | emit_ab(as, PPCI_CMPLW, tmp1, tisnum); | ||
650 | emit_fai(as, PPCI_LFD, tmpnum, dest, (int32_t)offsetof(Node, key.n)); | ||
651 | } else { | ||
652 | if (!irt_ispri(kt)) { | ||
653 | emit_ab(as, PPCI_CMPW, tmp2, key); | ||
654 | emit_condbranch(as, PPCI_BC, CC_NE, l_next); | ||
655 | } | ||
656 | emit_ai(as, PPCI_CMPWI, tmp1, irt_toitype(irkey->t)); | ||
657 | if (!irt_ispri(kt)) | ||
658 | emit_tai(as, PPCI_LWZ, tmp2, dest, (int32_t)offsetof(Node, key.gcr)); | ||
659 | } | ||
660 | emit_tai(as, PPCI_LWZ, tmp1, dest, (int32_t)offsetof(Node, key.it)); | ||
661 | *l_loop = PPCI_BC | PPCF_Y | PPCF_CC(CC_NE) | | ||
662 | (((char *)as->mcp-(char *)l_loop) & 0xffffu); | ||
663 | |||
664 | /* Load main position relative to tab->node into dest. */ | ||
665 | khash = irref_isk(refkey) ? ir_khash(irkey) : 1; | ||
666 | if (khash == 0) { | ||
667 | emit_tai(as, PPCI_LWZ, dest, tab, (int32_t)offsetof(GCtab, node)); | ||
668 | } else { | ||
669 | Reg tmphash = tmp1; | ||
670 | if (irref_isk(refkey)) | ||
671 | tmphash = ra_allock(as, khash, allow); | ||
672 | emit_tab(as, PPCI_ADD, dest, dest, tmp1); | ||
673 | emit_tai(as, PPCI_MULLI, tmp1, tmp1, sizeof(Node)); | ||
674 | emit_asb(as, PPCI_AND, tmp1, tmp2, tmphash); | ||
675 | emit_tai(as, PPCI_LWZ, dest, tab, (int32_t)offsetof(GCtab, node)); | ||
676 | emit_tai(as, PPCI_LWZ, tmp2, tab, (int32_t)offsetof(GCtab, hmask)); | ||
677 | if (irref_isk(refkey)) { | ||
678 | /* Nothing to do. */ | ||
679 | } else if (irt_isstr(kt)) { | ||
680 | emit_tai(as, PPCI_LWZ, tmp1, key, (int32_t)offsetof(GCstr, hash)); | ||
681 | } else { /* Must match with hash*() in lj_tab.c. */ | ||
682 | emit_tab(as, PPCI_SUBF, tmp1, tmp2, tmp1); | ||
683 | emit_rotlwi(as, tmp2, tmp2, HASH_ROT3); | ||
684 | emit_asb(as, PPCI_XOR, tmp1, tmp1, tmp2); | ||
685 | emit_rotlwi(as, tmp1, tmp1, (HASH_ROT2+HASH_ROT1)&31); | ||
686 | emit_tab(as, PPCI_SUBF, tmp2, dest, tmp2); | ||
687 | if (irt_isnum(kt)) { | ||
688 | int32_t ofs = ra_spill(as, irkey); | ||
689 | emit_asb(as, PPCI_XOR, tmp2, tmp2, tmp1); | ||
690 | emit_rotlwi(as, dest, tmp1, HASH_ROT1); | ||
691 | emit_tab(as, PPCI_ADD, tmp1, tmp1, tmp1); | ||
692 | emit_tai(as, PPCI_LWZ, tmp2, RID_SP, ofs+4); | ||
693 | emit_tai(as, PPCI_LWZ, tmp1, RID_SP, ofs); | ||
694 | } else { | ||
695 | emit_asb(as, PPCI_XOR, tmp2, key, tmp1); | ||
696 | emit_rotlwi(as, dest, tmp1, HASH_ROT1); | ||
697 | emit_tai(as, PPCI_ADDI, tmp1, tmp2, HASH_BIAS); | ||
698 | emit_tai(as, PPCI_ADDIS, tmp2, key, (HASH_BIAS + 32768)>>16); | ||
699 | } | ||
700 | } | ||
701 | } | ||
702 | } | ||
703 | |||
704 | static void asm_hrefk(ASMState *as, IRIns *ir) | ||
705 | { | ||
706 | IRIns *kslot = IR(ir->op2); | ||
707 | IRIns *irkey = IR(kslot->op1); | ||
708 | int32_t ofs = (int32_t)(kslot->op2 * sizeof(Node)); | ||
709 | int32_t kofs = ofs + (int32_t)offsetof(Node, key); | ||
710 | Reg dest = (ra_used(ir)||ofs > 65535) ? ra_dest(as, ir, RSET_GPR) : RID_NONE; | ||
711 | Reg node = ra_alloc1(as, ir->op1, RSET_GPR); | ||
712 | Reg key = RID_NONE, type = RID_TMP, idx = node; | ||
713 | RegSet allow = rset_exclude(RSET_GPR, node); | ||
714 | lua_assert(ofs % sizeof(Node) == 0); | ||
715 | if (ofs > 65535) { | ||
716 | idx = dest; | ||
717 | rset_clear(allow, dest); | ||
718 | kofs = (int32_t)offsetof(Node, key); | ||
719 | } else if (ra_hasreg(dest)) { | ||
720 | emit_tai(as, PPCI_ADDI, dest, node, ofs); | ||
721 | } | ||
722 | asm_guardcc(as, CC_NE); | ||
723 | if (!irt_ispri(irkey->t)) { | ||
724 | key = ra_scratch(as, allow); | ||
725 | rset_clear(allow, key); | ||
726 | } | ||
727 | rset_clear(allow, type); | ||
728 | if (irt_isnum(irkey->t)) { | ||
729 | emit_cmpi(as, key, (int32_t)ir_knum(irkey)->u32.lo); | ||
730 | asm_guardcc(as, CC_NE); | ||
731 | emit_cmpi(as, type, (int32_t)ir_knum(irkey)->u32.hi); | ||
732 | } else { | ||
733 | if (ra_hasreg(key)) { | ||
734 | emit_cmpi(as, key, irkey->i); /* May use RID_TMP, i.e. type. */ | ||
735 | asm_guardcc(as, CC_NE); | ||
736 | } | ||
737 | emit_ai(as, PPCI_CMPWI, type, irt_toitype(irkey->t)); | ||
738 | } | ||
739 | if (ra_hasreg(key)) emit_tai(as, PPCI_LWZ, key, idx, kofs+4); | ||
740 | emit_tai(as, PPCI_LWZ, type, idx, kofs); | ||
741 | if (ofs > 65535) { | ||
742 | emit_tai(as, PPCI_ADDIS, dest, dest, (ofs + 32768) >> 16); | ||
743 | emit_tai(as, PPCI_ADDI, dest, node, ofs); | ||
744 | } | ||
745 | } | ||
746 | |||
747 | static void asm_newref(ASMState *as, IRIns *ir) | ||
748 | { | ||
749 | const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_tab_newkey]; | ||
750 | IRRef args[3]; | ||
751 | args[0] = ASMREF_L; /* lua_State *L */ | ||
752 | args[1] = ir->op1; /* GCtab *t */ | ||
753 | args[2] = ASMREF_TMP1; /* cTValue *key */ | ||
754 | asm_setupresult(as, ir, ci); /* TValue * */ | ||
755 | asm_gencall(as, ci, args); | ||
756 | asm_tvptr(as, ra_releasetmp(as, ASMREF_TMP1), ir->op2); | ||
757 | } | ||
758 | |||
759 | static void asm_uref(ASMState *as, IRIns *ir) | ||
760 | { | ||
761 | /* NYI: Check that UREFO is still open and not aliasing a slot. */ | ||
762 | Reg dest = ra_dest(as, ir, RSET_GPR); | ||
763 | if (irref_isk(ir->op1)) { | ||
764 | GCfunc *fn = ir_kfunc(IR(ir->op1)); | ||
765 | MRef *v = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.v; | ||
766 | emit_lsptr(as, PPCI_LWZ, dest, v, RSET_GPR); | ||
767 | } else { | ||
768 | Reg uv = ra_scratch(as, RSET_GPR); | ||
769 | Reg func = ra_alloc1(as, ir->op1, RSET_GPR); | ||
770 | if (ir->o == IR_UREFC) { | ||
771 | asm_guardcc(as, CC_NE); | ||
772 | emit_ai(as, PPCI_CMPWI, RID_TMP, 1); | ||
773 | emit_tai(as, PPCI_ADDI, dest, uv, (int32_t)offsetof(GCupval, tv)); | ||
774 | emit_tai(as, PPCI_LBZ, RID_TMP, uv, (int32_t)offsetof(GCupval, closed)); | ||
775 | } else { | ||
776 | emit_tai(as, PPCI_LWZ, dest, uv, (int32_t)offsetof(GCupval, v)); | ||
777 | } | ||
778 | emit_tai(as, PPCI_LWZ, uv, func, | ||
779 | (int32_t)offsetof(GCfuncL, uvptr) + 4*(int32_t)(ir->op2 >> 8)); | ||
780 | } | ||
781 | } | ||
782 | |||
783 | static void asm_fref(ASMState *as, IRIns *ir) | ||
784 | { | ||
785 | UNUSED(as); UNUSED(ir); | ||
786 | lua_assert(!ra_used(ir)); | ||
787 | } | ||
788 | |||
789 | static void asm_strref(ASMState *as, IRIns *ir) | ||
790 | { | ||
791 | Reg dest = ra_dest(as, ir, RSET_GPR); | ||
792 | IRRef ref = ir->op2, refk = ir->op1; | ||
793 | int32_t ofs = (int32_t)sizeof(GCstr); | ||
794 | Reg r; | ||
795 | if (irref_isk(ref)) { | ||
796 | IRRef tmp = refk; refk = ref; ref = tmp; | ||
797 | } else if (!irref_isk(refk)) { | ||
798 | Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR); | ||
799 | IRIns *irr = IR(ir->op2); | ||
800 | if (ra_hasreg(irr->r)) { | ||
801 | ra_noweak(as, irr->r); | ||
802 | right = irr->r; | ||
803 | } else if (mayfuse(as, irr->op2) && | ||
804 | irr->o == IR_ADD && irref_isk(irr->op2) && | ||
805 | checki16(ofs + IR(irr->op2)->i)) { | ||
806 | ofs += IR(irr->op2)->i; | ||
807 | right = ra_alloc1(as, irr->op1, rset_exclude(RSET_GPR, left)); | ||
808 | } else { | ||
809 | right = ra_allocref(as, ir->op2, rset_exclude(RSET_GPR, left)); | ||
810 | } | ||
811 | emit_tai(as, PPCI_ADDI, dest, dest, ofs); | ||
812 | emit_tab(as, PPCI_ADD, dest, left, right); | ||
813 | return; | ||
814 | } | ||
815 | r = ra_alloc1(as, ref, RSET_GPR); | ||
816 | ofs += IR(refk)->i; | ||
817 | if (checki16(ofs)) | ||
818 | emit_tai(as, PPCI_ADDI, dest, r, ofs); | ||
819 | else | ||
820 | emit_tab(as, PPCI_ADD, dest, r, | ||
821 | ra_allock(as, ofs, rset_exclude(RSET_GPR, r))); | ||
822 | } | ||
823 | |||
824 | /* -- Loads and stores ---------------------------------------------------- */ | ||
825 | |||
826 | static PPCIns asm_fxloadins(IRIns *ir) | ||
827 | { | ||
828 | switch (irt_type(ir->t)) { | ||
829 | case IRT_I8: return PPCI_LBZ; /* Needs sign-extension. */ | ||
830 | case IRT_U8: return PPCI_LBZ; | ||
831 | case IRT_I16: return PPCI_LHA; | ||
832 | case IRT_U16: return PPCI_LHZ; | ||
833 | case IRT_NUM: return PPCI_LFD; | ||
834 | case IRT_FLOAT: return PPCI_LFS; | ||
835 | default: return PPCI_LWZ; | ||
836 | } | ||
837 | } | ||
838 | |||
839 | static PPCIns asm_fxstoreins(IRIns *ir) | ||
840 | { | ||
841 | switch (irt_type(ir->t)) { | ||
842 | case IRT_I8: case IRT_U8: return PPCI_STB; | ||
843 | case IRT_I16: case IRT_U16: return PPCI_STH; | ||
844 | case IRT_NUM: return PPCI_STFD; | ||
845 | case IRT_FLOAT: return PPCI_STFS; | ||
846 | default: return PPCI_STW; | ||
847 | } | ||
848 | } | ||
849 | |||
850 | static void asm_fload(ASMState *as, IRIns *ir) | ||
851 | { | ||
852 | Reg dest = ra_dest(as, ir, RSET_GPR); | ||
853 | Reg idx = ra_alloc1(as, ir->op1, RSET_GPR); | ||
854 | PPCIns pi = asm_fxloadins(ir); | ||
855 | int32_t ofs; | ||
856 | if (ir->op2 == IRFL_TAB_ARRAY) { | ||
857 | ofs = asm_fuseabase(as, ir->op1); | ||
858 | if (ofs) { /* Turn the t->array load into an add for colocated arrays. */ | ||
859 | emit_tai(as, PPCI_ADDI, dest, idx, ofs); | ||
860 | return; | ||
861 | } | ||
862 | } | ||
863 | ofs = field_ofs[ir->op2]; | ||
864 | lua_assert(!irt_isi8(ir->t)); | ||
865 | emit_tai(as, pi, dest, idx, ofs); | ||
866 | } | ||
867 | |||
868 | static void asm_fstore(ASMState *as, IRIns *ir) | ||
869 | { | ||
870 | Reg src = ra_alloc1(as, ir->op2, RSET_GPR); | ||
871 | IRIns *irf = IR(ir->op1); | ||
872 | Reg idx = ra_alloc1(as, irf->op1, rset_exclude(RSET_GPR, src)); | ||
873 | int32_t ofs = field_ofs[irf->op2]; | ||
874 | PPCIns pi = asm_fxstoreins(ir); | ||
875 | emit_tai(as, pi, src, idx, ofs); | ||
876 | } | ||
877 | |||
878 | static void asm_xload(ASMState *as, IRIns *ir) | ||
879 | { | ||
880 | Reg dest = ra_dest(as, ir, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); | ||
881 | lua_assert(!(ir->op2 & IRXLOAD_UNALIGNED)); | ||
882 | if (irt_isi8(ir->t)) | ||
883 | emit_as(as, PPCI_EXTSB, dest, dest); | ||
884 | asm_fusexref(as, asm_fxloadins(ir), dest, ir->op1, RSET_GPR); | ||
885 | } | ||
886 | |||
887 | static void asm_xstore(ASMState *as, IRIns *ir) | ||
888 | { | ||
889 | // NYI: fuse with bswap to stwbrx. | ||
890 | Reg src = ra_alloc1(as, ir->op2, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); | ||
891 | asm_fusexref(as, asm_fxstoreins(ir), src, ir->op1, | ||
892 | rset_exclude(RSET_GPR, src)); | ||
893 | } | ||
894 | |||
895 | static void asm_ahuvload(ASMState *as, IRIns *ir) | ||
896 | { | ||
897 | IRType1 t = ir->t; | ||
898 | Reg dest = RID_NONE, type = RID_TMP, tmp = RID_TMP, idx; | ||
899 | RegSet allow = RSET_GPR; | ||
900 | int32_t ofs = AHUREF_LSX; | ||
901 | if (ra_used(ir)) { | ||
902 | lua_assert(irt_isnum(t) || irt_isint(t) || irt_isaddr(t)); | ||
903 | if (!irt_isnum(t)) ofs = 0; | ||
904 | dest = ra_dest(as, ir, irt_isnum(t) ? RSET_FPR : RSET_GPR); | ||
905 | rset_clear(allow, dest); | ||
906 | } | ||
907 | idx = asm_fuseahuref(as, ir->op1, &ofs, allow); | ||
908 | if (irt_isnum(t)) { | ||
909 | Reg tisnum = ra_allock(as, (int32_t)LJ_TISNUM, rset_exclude(allow, idx)); | ||
910 | asm_guardcc(as, CC_GE); | ||
911 | emit_ab(as, PPCI_CMPLW, type, tisnum); | ||
912 | if (ra_hasreg(dest)) { | ||
913 | if (ofs == AHUREF_LSX) { | ||
914 | tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR, | ||
915 | (idx&255)), (idx>>8))); | ||
916 | emit_fab(as, PPCI_LFDX, dest, (idx&255), tmp); | ||
917 | } else { | ||
918 | emit_fai(as, PPCI_LFD, dest, idx, ofs); | ||
919 | } | ||
920 | } | ||
921 | } else { | ||
922 | asm_guardcc(as, CC_NE); | ||
923 | emit_ai(as, PPCI_CMPWI, type, irt_toitype(t)); | ||
924 | if (ra_hasreg(dest)) emit_tai(as, PPCI_LWZ, dest, idx, ofs+4); | ||
925 | } | ||
926 | if (ofs == AHUREF_LSX) { | ||
927 | emit_tab(as, PPCI_LWZX, type, (idx&255), tmp); | ||
928 | emit_slwi(as, tmp, (idx>>8), 3); | ||
929 | } else { | ||
930 | emit_tai(as, PPCI_LWZ, type, idx, ofs); | ||
931 | } | ||
932 | } | ||
933 | |||
934 | static void asm_ahustore(ASMState *as, IRIns *ir) | ||
935 | { | ||
936 | RegSet allow = RSET_GPR; | ||
937 | Reg idx, src = RID_NONE, type = RID_NONE; | ||
938 | int32_t ofs = AHUREF_LSX; | ||
939 | if (irt_isnum(ir->t)) { | ||
940 | src = ra_alloc1(as, ir->op2, RSET_FPR); | ||
941 | } else { | ||
942 | if (!irt_ispri(ir->t)) { | ||
943 | src = ra_alloc1(as, ir->op2, allow); | ||
944 | rset_clear(allow, src); | ||
945 | ofs = 0; | ||
946 | } | ||
947 | type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow); | ||
948 | rset_clear(allow, type); | ||
949 | } | ||
950 | idx = asm_fuseahuref(as, ir->op1, &ofs, allow); | ||
951 | if (irt_isnum(ir->t)) { | ||
952 | if (ofs == AHUREF_LSX) { | ||
953 | emit_fab(as, PPCI_STFDX, src, (idx&255), RID_TMP); | ||
954 | emit_slwi(as, RID_TMP, (idx>>8), 3); | ||
955 | } else { | ||
956 | emit_fai(as, PPCI_STFD, src, idx, ofs); | ||
957 | } | ||
958 | } else { | ||
959 | if (ra_hasreg(src)) | ||
960 | emit_tai(as, PPCI_STW, src, idx, ofs+4); | ||
961 | if (ofs == AHUREF_LSX) { | ||
962 | emit_tab(as, PPCI_STWX, type, (idx&255), RID_TMP); | ||
963 | emit_slwi(as, RID_TMP, (idx>>8), 3); | ||
964 | } else { | ||
965 | emit_tai(as, PPCI_STW, type, idx, ofs); | ||
966 | } | ||
967 | } | ||
968 | } | ||
969 | |||
970 | static void asm_sload(ASMState *as, IRIns *ir) | ||
971 | { | ||
972 | int32_t ofs = 8*((int32_t)ir->op1-1) + ((ir->op2 & IRSLOAD_FRAME) ? 0 : 4); | ||
973 | IRType1 t = ir->t; | ||
974 | Reg dest = RID_NONE, type = RID_NONE, base; | ||
975 | RegSet allow = RSET_GPR; | ||
976 | lua_assert(!(ir->op2 & IRSLOAD_PARENT)); /* Handled by asm_head_side(). */ | ||
977 | lua_assert(irt_isguard(t) || !(ir->op2 & IRSLOAD_TYPECHECK)); | ||
978 | lua_assert(LJ_DUALNUM || | ||
979 | !irt_isint(t) || (ir->op2 & (IRSLOAD_CONVERT|IRSLOAD_FRAME))); | ||
980 | if ((ir->op2 & IRSLOAD_CONVERT) && irt_isguard(t) && irt_isint(t)) { | ||
981 | dest = ra_scratch(as, RSET_FPR); | ||
982 | asm_tointg(as, ir, dest); | ||
983 | t.irt = IRT_NUM; /* Continue with a regular number type check. */ | ||
984 | } else if (ra_used(ir)) { | ||
985 | lua_assert(irt_isnum(t) || irt_isint(t) || irt_isaddr(t)); | ||
986 | dest = ra_dest(as, ir, irt_isnum(t) ? RSET_FPR : RSET_GPR); | ||
987 | rset_clear(allow, dest); | ||
988 | base = ra_alloc1(as, REF_BASE, allow); | ||
989 | rset_clear(allow, base); | ||
990 | if ((ir->op2 & IRSLOAD_CONVERT)) { | ||
991 | if (irt_isint(t)) { | ||
992 | emit_tai(as, PPCI_LWZ, dest, RID_SP, SPOFS_TMPLO); | ||
993 | dest = ra_scratch(as, RSET_FPR); | ||
994 | emit_fai(as, PPCI_STFD, dest, RID_SP, SPOFS_TMP); | ||
995 | emit_fb(as, PPCI_FCTIWZ, dest, dest); | ||
996 | t.irt = IRT_NUM; /* Check for original type. */ | ||
997 | } else { | ||
998 | Reg tmp = ra_scratch(as, allow); | ||
999 | Reg hibias = ra_allock(as, 0x43300000, rset_clear(allow, tmp)); | ||
1000 | Reg fbias = ra_scratch(as, rset_exclude(RSET_FPR, dest)); | ||
1001 | emit_fab(as, PPCI_FSUB, dest, dest, fbias); | ||
1002 | emit_fai(as, PPCI_LFD, dest, RID_SP, SPOFS_TMP); | ||
1003 | emit_lsptr(as, PPCI_LFS, (fbias & 31), | ||
1004 | (void *)lj_ir_k64_find(as->J, U64x(59800004,59800000)), | ||
1005 | rset_clear(allow, hibias)); | ||
1006 | emit_tai(as, PPCI_STW, tmp, RID_SP, SPOFS_TMPLO); | ||
1007 | emit_tai(as, PPCI_STW, hibias, RID_SP, SPOFS_TMPHI); | ||
1008 | emit_asi(as, PPCI_XORIS, tmp, tmp, 0x8000); | ||
1009 | dest = tmp; | ||
1010 | t.irt = IRT_INT; /* Check for original type. */ | ||
1011 | } | ||
1012 | } | ||
1013 | goto dotypecheck; | ||
1014 | } | ||
1015 | base = ra_alloc1(as, REF_BASE, allow); | ||
1016 | rset_clear(allow, base); | ||
1017 | dotypecheck: | ||
1018 | if (irt_isnum(t)) { | ||
1019 | if ((ir->op2 & IRSLOAD_TYPECHECK)) { | ||
1020 | Reg tisnum = ra_allock(as, (int32_t)LJ_TISNUM, allow); | ||
1021 | asm_guardcc(as, CC_GE); | ||
1022 | emit_ab(as, PPCI_CMPLW, RID_TMP, tisnum); | ||
1023 | type = RID_TMP; | ||
1024 | } | ||
1025 | if (ra_hasreg(dest)) emit_fai(as, PPCI_LFD, dest, base, ofs-4); | ||
1026 | } else { | ||
1027 | if ((ir->op2 & IRSLOAD_TYPECHECK)) { | ||
1028 | asm_guardcc(as, CC_NE); | ||
1029 | emit_ai(as, PPCI_CMPWI, RID_TMP, irt_toitype(t)); | ||
1030 | type = RID_TMP; | ||
1031 | } | ||
1032 | if (ra_hasreg(dest)) emit_tai(as, PPCI_LWZ, dest, base, ofs); | ||
1033 | } | ||
1034 | if (ra_hasreg(type)) emit_tai(as, PPCI_LWZ, type, base, ofs-4); | ||
1035 | } | ||
1036 | |||
1037 | /* -- Allocations --------------------------------------------------------- */ | ||
1038 | |||
1039 | #if LJ_HASFFI | ||
1040 | static void asm_cnew(ASMState *as, IRIns *ir) | ||
1041 | { | ||
1042 | CTState *cts = ctype_ctsG(J2G(as->J)); | ||
1043 | CTypeID typeid = (CTypeID)IR(ir->op1)->i; | ||
1044 | CTSize sz = (ir->o == IR_CNEWI || ir->op2 == REF_NIL) ? | ||
1045 | lj_ctype_size(cts, typeid) : (CTSize)IR(ir->op2)->i; | ||
1046 | const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_mem_newgco]; | ||
1047 | IRRef args[2]; | ||
1048 | RegSet allow = (RSET_GPR & ~RSET_SCRATCH); | ||
1049 | RegSet drop = RSET_SCRATCH; | ||
1050 | lua_assert(sz != CTSIZE_INVALID); | ||
1051 | |||
1052 | args[0] = ASMREF_L; /* lua_State *L */ | ||
1053 | args[1] = ASMREF_TMP1; /* MSize size */ | ||
1054 | as->gcsteps++; | ||
1055 | |||
1056 | if (ra_hasreg(ir->r)) | ||
1057 | rset_clear(drop, ir->r); /* Dest reg handled below. */ | ||
1058 | ra_evictset(as, drop); | ||
1059 | if (ra_used(ir)) | ||
1060 | ra_destreg(as, ir, RID_RET); /* GCcdata * */ | ||
1061 | |||
1062 | /* Initialize immutable cdata object. */ | ||
1063 | if (ir->o == IR_CNEWI) { | ||
1064 | int32_t ofs = sizeof(GCcdata); | ||
1065 | lua_assert(sz == 4 || sz == 8); | ||
1066 | if (sz == 8) { | ||
1067 | ofs += 4; | ||
1068 | lua_assert((ir+1)->o == IR_HIOP); | ||
1069 | } | ||
1070 | for (;;) { | ||
1071 | Reg r = ra_alloc1(as, ir->op2, allow); | ||
1072 | emit_tai(as, PPCI_STW, r, RID_RET, ofs); | ||
1073 | rset_clear(allow, r); | ||
1074 | if (ofs == sizeof(GCcdata)) break; | ||
1075 | ofs -= 4; ir++; | ||
1076 | } | ||
1077 | } | ||
1078 | /* Initialize gct and typeid. lj_mem_newgco() already sets marked. */ | ||
1079 | emit_tai(as, PPCI_STB, RID_RET+1, RID_RET, offsetof(GCcdata, gct)); | ||
1080 | emit_tai(as, PPCI_STH, RID_TMP, RID_RET, offsetof(GCcdata, typeid)); | ||
1081 | emit_ti(as, PPCI_LI, RID_RET+1, ~LJ_TCDATA); | ||
1082 | emit_ti(as, PPCI_LI, RID_TMP, typeid); /* Lower 16 bit used. Sign-ext ok. */ | ||
1083 | asm_gencall(as, ci, args); | ||
1084 | ra_allockreg(as, (int32_t)(sz+sizeof(GCcdata)), | ||
1085 | ra_releasetmp(as, ASMREF_TMP1)); | ||
1086 | } | ||
1087 | #else | ||
1088 | #define asm_cnew(as, ir) ((void)0) | ||
1089 | #endif | ||
1090 | |||
1091 | /* -- Write barriers ------------------------------------------------------ */ | ||
1092 | |||
1093 | static void asm_tbar(ASMState *as, IRIns *ir) | ||
1094 | { | ||
1095 | Reg tab = ra_alloc1(as, ir->op1, RSET_GPR); | ||
1096 | Reg mark = ra_scratch(as, rset_exclude(RSET_GPR, tab)); | ||
1097 | Reg link = RID_TMP; | ||
1098 | MCLabel l_end = emit_label(as); | ||
1099 | emit_tai(as, PPCI_STW, link, tab, (int32_t)offsetof(GCtab, gclist)); | ||
1100 | emit_tai(as, PPCI_STB, mark, tab, (int32_t)offsetof(GCtab, marked)); | ||
1101 | emit_setgl(as, tab, gc.grayagain); | ||
1102 | lua_assert(LJ_GC_BLACK == 0x04); | ||
1103 | emit_rot(as, PPCI_RLWINM, mark, mark, 0, 30, 28); /* Clear black bit. */ | ||
1104 | emit_getgl(as, link, gc.grayagain); | ||
1105 | emit_condbranch(as, PPCI_BC|PPCF_Y, CC_EQ, l_end); | ||
1106 | emit_asi(as, PPCI_ANDIDOT, RID_TMP, mark, LJ_GC_BLACK); | ||
1107 | emit_tai(as, PPCI_LBZ, mark, tab, (int32_t)offsetof(GCtab, marked)); | ||
1108 | } | ||
1109 | |||
1110 | static void asm_obar(ASMState *as, IRIns *ir) | ||
1111 | { | ||
1112 | const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_barrieruv]; | ||
1113 | IRRef args[2]; | ||
1114 | MCLabel l_end; | ||
1115 | Reg obj, val, tmp; | ||
1116 | /* No need for other object barriers (yet). */ | ||
1117 | lua_assert(IR(ir->op1)->o == IR_UREFC); | ||
1118 | ra_evictset(as, RSET_SCRATCH); | ||
1119 | l_end = emit_label(as); | ||
1120 | args[0] = ASMREF_TMP1; /* global_State *g */ | ||
1121 | args[1] = ir->op1; /* TValue *tv */ | ||
1122 | asm_gencall(as, ci, args); | ||
1123 | emit_tai(as, PPCI_ADDI, ra_releasetmp(as, ASMREF_TMP1), RID_JGL, -32768); | ||
1124 | obj = IR(ir->op1)->r; | ||
1125 | tmp = ra_scratch(as, rset_exclude(RSET_GPR, obj)); | ||
1126 | emit_condbranch(as, PPCI_BC|PPCF_Y, CC_EQ, l_end); | ||
1127 | emit_asi(as, PPCI_ANDIDOT, tmp, tmp, LJ_GC_BLACK); | ||
1128 | emit_condbranch(as, PPCI_BC, CC_EQ, l_end); | ||
1129 | emit_asi(as, PPCI_ANDIDOT, RID_TMP, RID_TMP, LJ_GC_WHITES); | ||
1130 | val = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, obj)); | ||
1131 | emit_tai(as, PPCI_LBZ, tmp, obj, | ||
1132 | (int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv)); | ||
1133 | emit_tai(as, PPCI_LBZ, RID_TMP, val, (int32_t)offsetof(GChead, marked)); | ||
1134 | } | ||
1135 | |||
1136 | /* -- Arithmetic and logic operations ------------------------------------- */ | ||
1137 | |||
1138 | static void asm_fparith(ASMState *as, IRIns *ir, PPCIns pi) | ||
1139 | { | ||
1140 | Reg dest = ra_dest(as, ir, RSET_FPR); | ||
1141 | Reg right, left = ra_alloc2(as, ir, RSET_FPR); | ||
1142 | right = (left >> 8); left &= 255; | ||
1143 | if (pi == PPCI_FMUL) | ||
1144 | emit_fac(as, pi, dest, left, right); | ||
1145 | else | ||
1146 | emit_fab(as, pi, dest, left, right); | ||
1147 | } | ||
1148 | |||
1149 | static void asm_fpunary(ASMState *as, IRIns *ir, PPCIns pi) | ||
1150 | { | ||
1151 | Reg dest = ra_dest(as, ir, RSET_FPR); | ||
1152 | Reg left = ra_hintalloc(as, ir->op1, dest, RSET_FPR); | ||
1153 | emit_fb(as, pi, dest, left); | ||
1154 | } | ||
1155 | |||
1156 | static int asm_fpjoin_pow(ASMState *as, IRIns *ir) | ||
1157 | { | ||
1158 | IRIns *irp = IR(ir->op1); | ||
1159 | if (irp == ir-1 && irp->o == IR_MUL && !ra_used(irp)) { | ||
1160 | IRIns *irpp = IR(irp->op1); | ||
1161 | if (irpp == ir-2 && irpp->o == IR_FPMATH && | ||
1162 | irpp->op2 == IRFPM_LOG2 && !ra_used(irpp)) { | ||
1163 | const CCallInfo *ci = &lj_ir_callinfo[IRCALL_pow]; | ||
1164 | IRRef args[2]; | ||
1165 | args[0] = irpp->op1; | ||
1166 | args[1] = irp->op2; | ||
1167 | asm_setupresult(as, ir, ci); | ||
1168 | asm_gencall(as, ci, args); | ||
1169 | return 1; | ||
1170 | } | ||
1171 | } | ||
1172 | return 0; | ||
1173 | } | ||
1174 | |||
1175 | static void asm_add(ASMState *as, IRIns *ir) | ||
1176 | { | ||
1177 | if (irt_isnum(ir->t)) { | ||
1178 | if (!asm_fusemadd(as, ir, PPCI_FMADD, PPCI_FMADD)) | ||
1179 | asm_fparith(as, ir, PPCI_FADD); | ||
1180 | } else { | ||
1181 | Reg dest = ra_dest(as, ir, RSET_GPR); | ||
1182 | Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); | ||
1183 | PPCIns pi; | ||
1184 | if (irref_isk(ir->op2)) { | ||
1185 | int32_t k = IR(ir->op2)->i; | ||
1186 | if (checki16(k)) { | ||
1187 | pi = PPCI_ADDI; | ||
1188 | /* May fail due to spills/restores above, but simplifies the logic. */ | ||
1189 | if (as->flagmcp == as->mcp) { | ||
1190 | as->flagmcp = NULL; | ||
1191 | as->mcp++; | ||
1192 | pi = PPCI_ADDICDOT; | ||
1193 | } | ||
1194 | emit_tai(as, pi, dest, left, k); | ||
1195 | return; | ||
1196 | } else if ((k & 0xffff) == 0) { | ||
1197 | emit_tai(as, PPCI_ADDIS, dest, left, (k >> 16)); | ||
1198 | return; | ||
1199 | } else if (!as->sectref) { | ||
1200 | emit_tai(as, PPCI_ADDIS, dest, dest, (k + 32768) >> 16); | ||
1201 | emit_tai(as, PPCI_ADDI, dest, left, k); | ||
1202 | return; | ||
1203 | } | ||
1204 | } | ||
1205 | pi = PPCI_ADD; | ||
1206 | /* May fail due to spills/restores above, but simplifies the logic. */ | ||
1207 | if (as->flagmcp == as->mcp) { | ||
1208 | as->flagmcp = NULL; | ||
1209 | as->mcp++; | ||
1210 | pi |= PPCF_DOT; | ||
1211 | } | ||
1212 | right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); | ||
1213 | emit_tab(as, pi, dest, left, right); | ||
1214 | } | ||
1215 | } | ||
1216 | |||
1217 | static void asm_sub(ASMState *as, IRIns *ir) | ||
1218 | { | ||
1219 | if (irt_isnum(ir->t)) { | ||
1220 | if (!asm_fusemadd(as, ir, PPCI_FMSUB, PPCI_FNMSUB)) | ||
1221 | asm_fparith(as, ir, PPCI_FSUB); | ||
1222 | } else { | ||
1223 | PPCIns pi = PPCI_SUBF; | ||
1224 | Reg dest = ra_dest(as, ir, RSET_GPR); | ||
1225 | Reg left, right; | ||
1226 | if (irref_isk(ir->op1)) { | ||
1227 | int32_t k = IR(ir->op1)->i; | ||
1228 | if (checki16(k)) { | ||
1229 | right = ra_alloc1(as, ir->op2, RSET_GPR); | ||
1230 | emit_tai(as, PPCI_SUBFIC, dest, right, k); | ||
1231 | return; | ||
1232 | } | ||
1233 | } | ||
1234 | /* May fail due to spills/restores above, but simplifies the logic. */ | ||
1235 | if (as->flagmcp == as->mcp) { | ||
1236 | as->flagmcp = NULL; | ||
1237 | as->mcp++; | ||
1238 | pi |= PPCF_DOT; | ||
1239 | } | ||
1240 | left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); | ||
1241 | right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); | ||
1242 | emit_tab(as, pi, dest, right, left); /* Subtract right _from_ left. */ | ||
1243 | } | ||
1244 | } | ||
1245 | |||
1246 | static void asm_mul(ASMState *as, IRIns *ir) | ||
1247 | { | ||
1248 | if (irt_isnum(ir->t)) { | ||
1249 | asm_fparith(as, ir, PPCI_FMUL); | ||
1250 | } else { | ||
1251 | PPCIns pi = PPCI_MULLW; | ||
1252 | Reg dest = ra_dest(as, ir, RSET_GPR); | ||
1253 | Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); | ||
1254 | if (irref_isk(ir->op2)) { | ||
1255 | int32_t k = IR(ir->op2)->i; | ||
1256 | if (checki16(k)) { | ||
1257 | emit_tai(as, PPCI_MULLI, dest, left, k); | ||
1258 | return; | ||
1259 | } | ||
1260 | } | ||
1261 | /* May fail due to spills/restores above, but simplifies the logic. */ | ||
1262 | if (as->flagmcp == as->mcp) { | ||
1263 | as->flagmcp = NULL; | ||
1264 | as->mcp++; | ||
1265 | pi |= PPCF_DOT; | ||
1266 | } | ||
1267 | right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); | ||
1268 | emit_tab(as, pi, dest, left, right); | ||
1269 | } | ||
1270 | } | ||
1271 | |||
1272 | static void asm_neg(ASMState *as, IRIns *ir) | ||
1273 | { | ||
1274 | if (irt_isnum(ir->t)) { | ||
1275 | asm_fpunary(as, ir, PPCI_FNEG); | ||
1276 | } else { | ||
1277 | Reg dest, left; | ||
1278 | PPCIns pi = PPCI_NEG; | ||
1279 | if (as->flagmcp == as->mcp) { | ||
1280 | as->flagmcp = NULL; | ||
1281 | as->mcp++; | ||
1282 | pi |= PPCF_DOT; | ||
1283 | } | ||
1284 | dest = ra_dest(as, ir, RSET_GPR); | ||
1285 | left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); | ||
1286 | emit_tab(as, pi, dest, left, 0); | ||
1287 | } | ||
1288 | } | ||
1289 | |||
1290 | static void asm_arithov(ASMState *as, IRIns *ir, PPCIns pi) | ||
1291 | { | ||
1292 | Reg dest, left, right; | ||
1293 | if (as->flagmcp == as->mcp) { | ||
1294 | as->flagmcp = NULL; | ||
1295 | as->mcp++; | ||
1296 | } | ||
1297 | asm_guardcc(as, CC_SO); | ||
1298 | dest = ra_dest(as, ir, RSET_GPR); | ||
1299 | left = ra_alloc2(as, ir, RSET_GPR); | ||
1300 | right = (left >> 8); left &= 255; | ||
1301 | if (pi == PPCI_SUBFO) { Reg tmp = left; left = right; right = tmp; } | ||
1302 | emit_tab(as, pi|PPCF_DOT, dest, left, right); | ||
1303 | } | ||
1304 | |||
1305 | #if LJ_HASFFI | ||
1306 | static void asm_add64(ASMState *as, IRIns *ir) | ||
1307 | { | ||
1308 | Reg dest = ra_dest(as, ir, RSET_GPR); | ||
1309 | Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR); | ||
1310 | PPCIns pi = PPCI_ADDE; | ||
1311 | if (irref_isk(ir->op2)) { | ||
1312 | int32_t k = IR(ir->op2)->i; | ||
1313 | if (k == 0) | ||
1314 | pi = PPCI_ADDZE; | ||
1315 | else if (k == -1) | ||
1316 | pi = PPCI_ADDME; | ||
1317 | else | ||
1318 | goto needright; | ||
1319 | right = 0; | ||
1320 | } else { | ||
1321 | needright: | ||
1322 | right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); | ||
1323 | } | ||
1324 | emit_tab(as, pi, dest, left, right); | ||
1325 | ir--; | ||
1326 | dest = ra_dest(as, ir, RSET_GPR); | ||
1327 | left = ra_alloc1(as, ir->op1, RSET_GPR); | ||
1328 | if (irref_isk(ir->op2)) { | ||
1329 | int32_t k = IR(ir->op2)->i; | ||
1330 | if (checki16(k)) { | ||
1331 | emit_tai(as, PPCI_ADDIC, dest, left, k); | ||
1332 | return; | ||
1333 | } | ||
1334 | } | ||
1335 | right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); | ||
1336 | emit_tab(as, PPCI_ADDC, dest, left, right); | ||
1337 | } | ||
1338 | |||
1339 | static void asm_sub64(ASMState *as, IRIns *ir) | ||
1340 | { | ||
1341 | Reg dest = ra_dest(as, ir, RSET_GPR); | ||
1342 | Reg left, right = ra_alloc1(as, ir->op2, RSET_GPR); | ||
1343 | PPCIns pi = PPCI_SUBFE; | ||
1344 | if (irref_isk(ir->op1)) { | ||
1345 | int32_t k = IR(ir->op1)->i; | ||
1346 | if (k == 0) | ||
1347 | pi = PPCI_SUBFZE; | ||
1348 | else if (k == -1) | ||
1349 | pi = PPCI_SUBFME; | ||
1350 | else | ||
1351 | goto needleft; | ||
1352 | left = 0; | ||
1353 | } else { | ||
1354 | needleft: | ||
1355 | left = ra_alloc1(as, ir->op1, rset_exclude(RSET_GPR, right)); | ||
1356 | } | ||
1357 | emit_tab(as, pi, dest, right, left); /* Subtract right _from_ left. */ | ||
1358 | ir--; | ||
1359 | dest = ra_dest(as, ir, RSET_GPR); | ||
1360 | right = ra_alloc1(as, ir->op2, RSET_GPR); | ||
1361 | if (irref_isk(ir->op1)) { | ||
1362 | int32_t k = IR(ir->op1)->i; | ||
1363 | if (checki16(k)) { | ||
1364 | emit_tai(as, PPCI_SUBFIC, dest, right, k); | ||
1365 | return; | ||
1366 | } | ||
1367 | } | ||
1368 | left = ra_alloc1(as, ir->op1, rset_exclude(RSET_GPR, right)); | ||
1369 | emit_tab(as, PPCI_SUBFC, dest, right, left); | ||
1370 | } | ||
1371 | |||
1372 | static void asm_neg64(ASMState *as, IRIns *ir) | ||
1373 | { | ||
1374 | Reg dest = ra_dest(as, ir, RSET_GPR); | ||
1375 | Reg left = ra_alloc1(as, ir->op1, RSET_GPR); | ||
1376 | emit_tab(as, PPCI_SUBFZE, dest, left, 0); | ||
1377 | ir--; | ||
1378 | dest = ra_dest(as, ir, RSET_GPR); | ||
1379 | left = ra_alloc1(as, ir->op1, RSET_GPR); | ||
1380 | emit_tai(as, PPCI_SUBFIC, dest, left, 0); | ||
1381 | } | ||
1382 | #endif | ||
1383 | |||
1384 | static void asm_bitnot(ASMState *as, IRIns *ir) | ||
1385 | { | ||
1386 | Reg dest, left, right; | ||
1387 | PPCIns pi = PPCI_NOR; | ||
1388 | if (as->flagmcp == as->mcp) { | ||
1389 | as->flagmcp = NULL; | ||
1390 | as->mcp++; | ||
1391 | pi |= PPCF_DOT; | ||
1392 | } | ||
1393 | dest = ra_dest(as, ir, RSET_GPR); | ||
1394 | if (mayfuse(as, ir->op1)) { | ||
1395 | IRIns *irl = IR(ir->op1); | ||
1396 | if (irl->o == IR_BAND) | ||
1397 | pi ^= (PPCI_NOR ^ PPCI_NAND); | ||
1398 | else if (irl->o == IR_BXOR) | ||
1399 | pi ^= (PPCI_NOR ^ PPCI_EQV); | ||
1400 | else if (irl->o != IR_BOR) | ||
1401 | goto nofuse; | ||
1402 | left = ra_hintalloc(as, irl->op1, dest, RSET_GPR); | ||
1403 | right = ra_alloc1(as, irl->op2, rset_exclude(RSET_GPR, left)); | ||
1404 | } else { | ||
1405 | nofuse: | ||
1406 | left = right = ra_hintalloc(as, ir->op1, dest, RSET_GPR); | ||
1407 | } | ||
1408 | emit_asb(as, pi, dest, left, right); | ||
1409 | } | ||
1410 | |||
1411 | static void asm_bitswap(ASMState *as, IRIns *ir) | ||
1412 | { | ||
1413 | // NYI: fuse with XLOAD to lwbrx. | ||
1414 | Reg dest = ra_dest(as, ir, RSET_GPR); | ||
1415 | Reg left = ra_alloc1(as, ir->op1, RSET_GPR); | ||
1416 | Reg tmp = dest; | ||
1417 | if (tmp == left) { | ||
1418 | tmp = RID_TMP; | ||
1419 | emit_mr(as, dest, RID_TMP); | ||
1420 | } | ||
1421 | emit_rot(as, PPCI_RLWIMI, tmp, left, 24, 16, 23); | ||
1422 | emit_rot(as, PPCI_RLWIMI, tmp, left, 24, 0, 7); | ||
1423 | emit_rotlwi(as, tmp, left, 8); | ||
1424 | } | ||
1425 | |||
1426 | static void asm_bitop(ASMState *as, IRIns *ir, PPCIns pi, PPCIns pik) | ||
1427 | { | ||
1428 | Reg dest = ra_dest(as, ir, RSET_GPR); | ||
1429 | Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); | ||
1430 | if (irref_isk(ir->op2)) { | ||
1431 | int32_t k = IR(ir->op2)->i; | ||
1432 | Reg tmp = left; | ||
1433 | if ((checku16(k) || (k & 0xffff) == 0) || (tmp = dest, !as->sectref)) { | ||
1434 | if (!checku16(k)) { | ||
1435 | emit_asi(as, pik ^ (PPCI_ORI ^ PPCI_ORIS), dest, tmp, (k >> 16)); | ||
1436 | if ((k & 0xffff) == 0) return; | ||
1437 | } | ||
1438 | emit_asi(as, pik, dest, left, k); | ||
1439 | return; | ||
1440 | } | ||
1441 | } | ||
1442 | /* May fail due to spills/restores above, but simplifies the logic. */ | ||
1443 | if (as->flagmcp == as->mcp) { | ||
1444 | as->flagmcp = NULL; | ||
1445 | as->mcp++; | ||
1446 | pi |= PPCF_DOT; | ||
1447 | } | ||
1448 | right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); | ||
1449 | emit_asb(as, pi, dest, left, right); | ||
1450 | } | ||
1451 | |||
1452 | static void asm_bitand(ASMState *as, IRIns *ir) | ||
1453 | { | ||
1454 | Reg dest, left, right; | ||
1455 | PPCIns dot = 0; | ||
1456 | IRRef op2; | ||
1457 | if (as->flagmcp == as->mcp) { | ||
1458 | as->flagmcp = NULL; | ||
1459 | as->mcp++; | ||
1460 | dot = PPCF_DOT; | ||
1461 | } | ||
1462 | dest = ra_dest(as, ir, RSET_GPR); | ||
1463 | left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); | ||
1464 | if (irref_isk(ir->op2)) { | ||
1465 | int32_t k = IR(ir->op2)->i; | ||
1466 | if (k) { | ||
1467 | // NYI: fuse with shifts/rotates. | ||
1468 | uint32_t s1 = lj_ffs((uint32_t)k); | ||
1469 | uint32_t k1 = ((uint32_t)k >> s1); | ||
1470 | if ((k1 & (k1+1)) == 0) { | ||
1471 | emit_rot(as, PPCI_RLWINM|dot, dest, left, 0, | ||
1472 | 31-lj_fls((uint32_t)k), 31-s1); | ||
1473 | return; | ||
1474 | } | ||
1475 | if (~(uint32_t)k) { | ||
1476 | uint32_t s2 = lj_ffs(~(uint32_t)k); | ||
1477 | uint32_t k2 = (~(uint32_t)k >> s2); | ||
1478 | if ((k2 & (k2+1)) == 0) { | ||
1479 | emit_rot(as, PPCI_RLWINM|dot, dest, left, 0, | ||
1480 | 32-s2, 30-lj_fls(~(uint32_t)k)); | ||
1481 | return; | ||
1482 | } | ||
1483 | } | ||
1484 | } | ||
1485 | if (checku16(k)) { | ||
1486 | emit_asi(as, PPCI_ANDIDOT, dest, left, k); | ||
1487 | return; | ||
1488 | } else if ((k & 0xffff) == 0) { | ||
1489 | emit_asi(as, PPCI_ANDISDOT, dest, left, (k >> 16)); | ||
1490 | return; | ||
1491 | } | ||
1492 | } | ||
1493 | op2 = ir->op2; | ||
1494 | if (mayfuse(as, op2) && IR(op2)->o == IR_BNOT) { | ||
1495 | dot ^= (PPCI_AND ^ PPCI_ANDC); | ||
1496 | op2 = IR(op2)->op1; | ||
1497 | } | ||
1498 | right = ra_alloc1(as, op2, rset_exclude(RSET_GPR, left)); | ||
1499 | emit_asb(as, PPCI_AND ^ dot, dest, left, right); | ||
1500 | } | ||
1501 | |||
1502 | static void asm_bitshift(ASMState *as, IRIns *ir, PPCIns pi, PPCIns pik) | ||
1503 | { | ||
1504 | // NYI: fuse with IR_BAND. | ||
1505 | Reg dest, left; | ||
1506 | Reg dot = 0; | ||
1507 | if (as->flagmcp == as->mcp) { | ||
1508 | as->flagmcp = NULL; | ||
1509 | as->mcp++; | ||
1510 | dot = PPCF_DOT; | ||
1511 | } | ||
1512 | dest = ra_dest(as, ir, RSET_GPR); | ||
1513 | left = ra_alloc1(as, ir->op1, RSET_GPR); | ||
1514 | if (irref_isk(ir->op2)) { /* Constant shifts. */ | ||
1515 | int32_t shift = (IR(ir->op2)->i & 31); | ||
1516 | if (pik == 0) /* SLWI */ | ||
1517 | emit_rot(as, PPCI_RLWINM|dot, dest, left, shift, 0, 31-shift); | ||
1518 | else if (pik == 1) /* SRWI */ | ||
1519 | emit_rot(as, PPCI_RLWINM|dot, dest, left, (32-shift)&31, shift, 31); | ||
1520 | else | ||
1521 | emit_asb(as, pik|dot, dest, left, shift); | ||
1522 | } else { | ||
1523 | Reg right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); | ||
1524 | emit_asb(as, pi|dot, dest, left, right); | ||
1525 | } | ||
1526 | } | ||
1527 | |||
1528 | static void asm_min_max(ASMState *as, IRIns *ir, int ismax) | ||
1529 | { | ||
1530 | if (irt_isnum(ir->t)) { | ||
1531 | Reg dest = ra_dest(as, ir, RSET_FPR); | ||
1532 | Reg tmp = dest; | ||
1533 | Reg right, left = ra_alloc2(as, ir, RSET_FPR); | ||
1534 | right = (left >> 8); left &= 255; | ||
1535 | if (tmp == left || tmp == right) | ||
1536 | tmp = ra_scratch(as, rset_exclude(rset_exclude(rset_exclude(RSET_FPR, | ||
1537 | dest), left), right)); | ||
1538 | emit_facb(as, PPCI_FSEL, dest, tmp, | ||
1539 | ismax ? left : right, ismax ? right : left); | ||
1540 | emit_fab(as, PPCI_FSUB, tmp, left, right); | ||
1541 | } else { | ||
1542 | Reg dest = ra_dest(as, ir, RSET_GPR); | ||
1543 | Reg tmp1 = RID_TMP, tmp2 = dest; | ||
1544 | Reg right, left = ra_alloc2(as, ir, RSET_GPR); | ||
1545 | right = (left >> 8); left &= 255; | ||
1546 | if (tmp2 == left || tmp2 == right) | ||
1547 | tmp2 = ra_scratch(as, rset_exclude(rset_exclude(rset_exclude(RSET_GPR, | ||
1548 | dest), left), right)); | ||
1549 | emit_tab(as, PPCI_ADD, dest, tmp2, right); | ||
1550 | emit_asb(as, ismax ? PPCI_ANDC : PPCI_AND, tmp2, tmp2, tmp1); | ||
1551 | emit_tab(as, PPCI_SUBFE, tmp1, tmp1, tmp1); | ||
1552 | emit_tab(as, PPCI_SUBFC, tmp2, tmp2, tmp1); | ||
1553 | emit_asi(as, PPCI_XORIS, tmp2, right, 0x8000); | ||
1554 | emit_asi(as, PPCI_XORIS, tmp1, left, 0x8000); | ||
1555 | } | ||
1556 | } | ||
1557 | |||
1558 | /* -- Comparisons --------------------------------------------------------- */ | ||
1559 | |||
1560 | #define CC_UNSIGNED 0x08 /* Unsigned integer comparison. */ | ||
1561 | #define CC_TWO 0x80 /* Check two flags for FP comparison. */ | ||
1562 | |||
1563 | /* Map of comparisons to flags. ORDER IR. */ | ||
1564 | static const uint8_t asm_compmap[IR_ABC+1] = { | ||
1565 | /* op int cc FP cc */ | ||
1566 | /* LT */ CC_GE + (CC_GE<<4), | ||
1567 | /* GE */ CC_LT + (CC_LE<<4) + CC_TWO, | ||
1568 | /* LE */ CC_GT + (CC_GE<<4) + CC_TWO, | ||
1569 | /* GT */ CC_LE + (CC_LE<<4), | ||
1570 | /* ULT */ CC_GE + CC_UNSIGNED + (CC_GT<<4) + CC_TWO, | ||
1571 | /* UGE */ CC_LT + CC_UNSIGNED + (CC_LT<<4), | ||
1572 | /* ULE */ CC_GT + CC_UNSIGNED + (CC_GT<<4), | ||
1573 | /* UGT */ CC_LE + CC_UNSIGNED + (CC_LT<<4) + CC_TWO, | ||
1574 | /* EQ */ CC_NE + (CC_NE<<4), | ||
1575 | /* NE */ CC_EQ + (CC_EQ<<4), | ||
1576 | /* ABC */ CC_LE + CC_UNSIGNED + (CC_LT<<4) + CC_TWO /* Same as UGT. */ | ||
1577 | }; | ||
1578 | |||
1579 | static void asm_intcomp_(ASMState *as, IRRef lref, IRRef rref, Reg cr, PPCCC cc) | ||
1580 | { | ||
1581 | Reg right, left = ra_alloc1(as, lref, RSET_GPR); | ||
1582 | if (irref_isk(rref)) { | ||
1583 | int32_t k = IR(rref)->i; | ||
1584 | if ((cc & CC_UNSIGNED) == 0) { /* Signed comparison with constant. */ | ||
1585 | if (checki16(k)) { | ||
1586 | emit_tai(as, PPCI_CMPWI, cr, left, k); | ||
1587 | /* Signed comparison with zero and referencing previous ins? */ | ||
1588 | if (k == 0 && lref == as->curins-1) | ||
1589 | as->flagmcp = as->mcp; /* Allow elimination of the compare. */ | ||
1590 | return; | ||
1591 | } else if ((cc & 3) == (CC_EQ & 3)) { /* Use CMPLWI for EQ or NE. */ | ||
1592 | if (checku16(k)) { | ||
1593 | emit_tai(as, PPCI_CMPLWI, cr, left, k); | ||
1594 | return; | ||
1595 | } else if (!as->sectref && ra_noreg(IR(rref)->r)) { | ||
1596 | emit_tai(as, PPCI_CMPLWI, cr, RID_TMP, k); | ||
1597 | emit_asi(as, PPCI_XORIS, RID_TMP, left, (k >> 16)); | ||
1598 | return; | ||
1599 | } | ||
1600 | } | ||
1601 | } else { /* Unsigned comparison with constant. */ | ||
1602 | if (checku16(k)) { | ||
1603 | emit_tai(as, PPCI_CMPLWI, cr, left, k); | ||
1604 | return; | ||
1605 | } | ||
1606 | } | ||
1607 | } | ||
1608 | right = ra_alloc1(as, rref, rset_exclude(RSET_GPR, left)); | ||
1609 | emit_tab(as, (cc & CC_UNSIGNED) ? PPCI_CMPLW : PPCI_CMPW, cr, left, right); | ||
1610 | } | ||
1611 | |||
1612 | static void asm_comp(ASMState *as, IRIns *ir) | ||
1613 | { | ||
1614 | PPCCC cc = asm_compmap[ir->o]; | ||
1615 | if (irt_isnum(ir->t)) { | ||
1616 | Reg right, left = ra_alloc2(as, ir, RSET_FPR); | ||
1617 | right = (left >> 8); left &= 255; | ||
1618 | asm_guardcc(as, (cc >> 4)); | ||
1619 | if ((cc & CC_TWO)) | ||
1620 | emit_tab(as, PPCI_CROR, ((cc>>4)&3), ((cc>>4)&3), (CC_EQ&3)); | ||
1621 | emit_fab(as, PPCI_FCMPU, 0, left, right); | ||
1622 | } else { | ||
1623 | IRRef lref = ir->op1, rref = ir->op2; | ||
1624 | if (irref_isk(lref) && !irref_isk(rref)) { | ||
1625 | /* Swap constants to the right (only for ABC). */ | ||
1626 | IRRef tmp = lref; lref = rref; rref = tmp; | ||
1627 | if ((cc & 2) == 0) cc ^= 1; /* LT <-> GT, LE <-> GE */ | ||
1628 | } | ||
1629 | asm_guardcc(as, cc); | ||
1630 | asm_intcomp_(as, lref, rref, 0, cc); | ||
1631 | } | ||
1632 | } | ||
1633 | |||
1634 | #if LJ_HASFFI | ||
1635 | /* 64 bit integer comparisons. */ | ||
1636 | static void asm_comp64(ASMState *as, IRIns *ir) | ||
1637 | { | ||
1638 | PPCCC cc = asm_compmap[(ir-1)->o]; | ||
1639 | if ((cc&3) == (CC_EQ&3)) { | ||
1640 | asm_guardcc(as, cc); | ||
1641 | emit_tab(as, (cc&4) ? PPCI_CRAND : PPCI_CROR, | ||
1642 | (CC_EQ&3), (CC_EQ&3), 4+(CC_EQ&3)); | ||
1643 | } else { | ||
1644 | asm_guardcc(as, CC_EQ); | ||
1645 | emit_tab(as, PPCI_CROR, (CC_EQ&3), (CC_EQ&3), ((cc^~(cc>>2))&1)); | ||
1646 | emit_tab(as, (cc&4) ? PPCI_CRAND : PPCI_CRANDC, | ||
1647 | (CC_EQ&3), (CC_EQ&3), 4+(cc&3)); | ||
1648 | } | ||
1649 | /* Loword comparison sets cr1 and is unsigned, except for equality. */ | ||
1650 | asm_intcomp_(as, (ir-1)->op1, (ir-1)->op2, 4, | ||
1651 | cc | ((cc&3) == (CC_EQ&3) ? 0 : CC_UNSIGNED)); | ||
1652 | /* Hiword comparison sets cr0. */ | ||
1653 | asm_intcomp_(as, ir->op1, ir->op2, 0, cc); | ||
1654 | as->flagmcp = NULL; /* Doesn't work here. */ | ||
1655 | } | ||
1656 | #endif | ||
1657 | |||
1658 | /* -- Support for 64 bit ops in 32 bit mode ------------------------------- */ | ||
1659 | |||
1660 | /* Hiword op of a split 64 bit op. Previous op must be the loword op. */ | ||
1661 | static void asm_hiop(ASMState *as, IRIns *ir) | ||
1662 | { | ||
1663 | #if LJ_HASFFI | ||
1664 | /* HIOP is marked as a store because it needs its own DCE logic. */ | ||
1665 | int uselo = ra_used(ir-1), usehi = ra_used(ir); /* Loword/hiword used? */ | ||
1666 | if (LJ_UNLIKELY(!(as->flags & JIT_F_OPT_DCE))) uselo = usehi = 1; | ||
1667 | if ((ir-1)->o == IR_CONV) { /* Conversions to/from 64 bit. */ | ||
1668 | as->curins--; /* Always skip the CONV. */ | ||
1669 | if (usehi || uselo) | ||
1670 | asm_conv64(as, ir); | ||
1671 | return; | ||
1672 | } else if ((ir-1)->o <= IR_NE) { /* 64 bit integer comparisons. ORDER IR. */ | ||
1673 | as->curins--; /* Always skip the loword comparison. */ | ||
1674 | asm_comp64(as, ir); | ||
1675 | return; | ||
1676 | } | ||
1677 | if (!usehi) return; /* Skip unused hiword op for all remaining ops. */ | ||
1678 | switch ((ir-1)->o) { | ||
1679 | case IR_ADD: as->curins--; asm_add64(as, ir); break; | ||
1680 | case IR_SUB: as->curins--; asm_sub64(as, ir); break; | ||
1681 | case IR_NEG: as->curins--; asm_neg64(as, ir); break; | ||
1682 | case IR_CALLN: | ||
1683 | case IR_CALLXS: | ||
1684 | if (!uselo) | ||
1685 | ra_allocref(as, ir->op1, RID2RSET(RID_RETLO)); /* Mark lo op as used. */ | ||
1686 | break; | ||
1687 | case IR_CNEWI: | ||
1688 | /* Nothing to do here. Handled by lo op itself. */ | ||
1689 | break; | ||
1690 | default: lua_assert(0); break; | ||
1691 | } | ||
1692 | #else | ||
1693 | UNUSED(as); UNUSED(ir); lua_assert(0); /* Unused without FFI. */ | ||
1694 | #endif | ||
1695 | } | ||
1696 | |||
1697 | /* -- Stack handling ------------------------------------------------------ */ | ||
1698 | |||
1699 | /* Check Lua stack size for overflow. Use exit handler as fallback. */ | ||
1700 | static void asm_stack_check(ASMState *as, BCReg topslot, | ||
1701 | IRIns *irp, RegSet allow, ExitNo exitno) | ||
1702 | { | ||
1703 | /* Try to get an unused temp. register, otherwise spill/restore RID_RET*. */ | ||
1704 | Reg tmp, pbase = irp ? (ra_hasreg(irp->r) ? irp->r : RID_TMP) : RID_BASE; | ||
1705 | rset_clear(allow, pbase); | ||
1706 | tmp = allow ? rset_pickbot(allow) : | ||
1707 | (pbase == RID_RETHI ? RID_RETLO : RID_RETHI); | ||
1708 | emit_condbranch(as, PPCI_BC, CC_LT, asm_exitstub_addr(as, exitno)); | ||
1709 | if (allow == RSET_EMPTY) /* Restore temp. register. */ | ||
1710 | emit_tai(as, PPCI_LWZ, tmp, RID_SP, SPOFS_TMPW); | ||
1711 | else | ||
1712 | ra_modified(as, tmp); | ||
1713 | emit_ai(as, PPCI_CMPLWI, RID_TMP, (int32_t)(8*topslot)); | ||
1714 | emit_tab(as, PPCI_SUBF, RID_TMP, pbase, tmp); | ||
1715 | emit_tai(as, PPCI_LWZ, tmp, tmp, offsetof(lua_State, maxstack)); | ||
1716 | if (pbase == RID_TMP) | ||
1717 | emit_getgl(as, RID_TMP, jit_base); | ||
1718 | emit_getgl(as, tmp, jit_L); | ||
1719 | if (allow == RSET_EMPTY) /* Spill temp. register. */ | ||
1720 | emit_tai(as, PPCI_STW, tmp, RID_SP, SPOFS_TMPW); | ||
1721 | } | ||
1722 | |||
1723 | /* Restore Lua stack from on-trace state. */ | ||
1724 | static void asm_stack_restore(ASMState *as, SnapShot *snap) | ||
1725 | { | ||
1726 | SnapEntry *map = &as->T->snapmap[snap->mapofs]; | ||
1727 | MSize n, nent = snap->nent; | ||
1728 | SnapEntry *flinks = map + nent + snap->depth; | ||
1729 | /* Store the value of all modified slots to the Lua stack. */ | ||
1730 | for (n = 0; n < nent; n++) { | ||
1731 | SnapEntry sn = map[n]; | ||
1732 | BCReg s = snap_slot(sn); | ||
1733 | int32_t ofs = 8*((int32_t)s-1); | ||
1734 | IRRef ref = snap_ref(sn); | ||
1735 | IRIns *ir = IR(ref); | ||
1736 | if ((sn & SNAP_NORESTORE)) | ||
1737 | continue; | ||
1738 | if (irt_isnum(ir->t)) { | ||
1739 | Reg src = ra_alloc1(as, ref, RSET_FPR); | ||
1740 | emit_fai(as, PPCI_STFD, src, RID_BASE, ofs); | ||
1741 | } else { | ||
1742 | Reg type; | ||
1743 | RegSet allow = rset_exclude(RSET_GPR, RID_BASE); | ||
1744 | lua_assert(irt_ispri(ir->t) || irt_isaddr(ir->t) || irt_isinteger(ir->t)); | ||
1745 | if (!irt_ispri(ir->t)) { | ||
1746 | Reg src = ra_alloc1(as, ref, allow); | ||
1747 | rset_clear(allow, src); | ||
1748 | emit_tai(as, PPCI_STW, src, RID_BASE, ofs+4); | ||
1749 | } | ||
1750 | if ((sn & (SNAP_CONT|SNAP_FRAME))) { | ||
1751 | if (s == 0) continue; /* Do not overwrite link to previous frame. */ | ||
1752 | type = ra_allock(as, (int32_t)(*flinks--), allow); | ||
1753 | } else { | ||
1754 | type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow); | ||
1755 | } | ||
1756 | emit_tai(as, PPCI_STW, type, RID_BASE, ofs); | ||
1757 | } | ||
1758 | checkmclim(as); | ||
1759 | } | ||
1760 | lua_assert(map + nent == flinks); | ||
1761 | } | ||
1762 | |||
1763 | /* -- GC handling --------------------------------------------------------- */ | ||
1764 | |||
1765 | /* Check GC threshold and do one or more GC steps. */ | ||
1766 | static void asm_gc_check(ASMState *as) | ||
1767 | { | ||
1768 | const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_step_jit]; | ||
1769 | IRRef args[2]; | ||
1770 | MCLabel l_end; | ||
1771 | Reg tmp; | ||
1772 | ra_evictset(as, RSET_SCRATCH); | ||
1773 | l_end = emit_label(as); | ||
1774 | /* Exit trace if in GCSatomic or GCSfinalize. Avoids syncing GC objects. */ | ||
1775 | asm_guardcc(as, CC_NE); /* Assumes asm_snap_prep() already done. */ | ||
1776 | emit_ai(as, PPCI_CMPWI, RID_RET, 0); | ||
1777 | args[0] = ASMREF_TMP1; /* global_State *g */ | ||
1778 | args[1] = ASMREF_TMP2; /* MSize steps */ | ||
1779 | asm_gencall(as, ci, args); | ||
1780 | emit_tai(as, PPCI_ADDI, ra_releasetmp(as, ASMREF_TMP1), RID_JGL, -32768); | ||
1781 | tmp = ra_releasetmp(as, ASMREF_TMP2); | ||
1782 | emit_loadi(as, tmp, (int32_t)as->gcsteps); | ||
1783 | /* Jump around GC step if GC total < GC threshold. */ | ||
1784 | emit_condbranch(as, PPCI_BC|PPCF_Y, CC_LT, l_end); | ||
1785 | emit_ab(as, PPCI_CMPLW, RID_TMP, tmp); | ||
1786 | emit_getgl(as, tmp, gc.threshold); | ||
1787 | emit_getgl(as, RID_TMP, gc.total); | ||
1788 | as->gcsteps = 0; | ||
1789 | checkmclim(as); | ||
1790 | } | ||
1791 | |||
1792 | /* -- Loop handling ------------------------------------------------------- */ | ||
1793 | |||
1794 | /* Fixup the loop branch. */ | ||
1795 | static void asm_loop_fixup(ASMState *as) | ||
1796 | { | ||
1797 | MCode *p = as->mctop; | ||
1798 | MCode *target = as->mcp; | ||
1799 | if (as->loopinv) { /* Inverted loop branch? */ | ||
1800 | /* asm_guardcc already inverted the cond branch and patched the final b. */ | ||
1801 | p[-2] = (p[-2] & (0xffff0000u & ~PPCF_Y)) | (((target-p+2) & 0x3fffu) << 2); | ||
1802 | } else { | ||
1803 | p[-1] = PPCI_B|(((target-p+1)&0x00ffffffu)<<2); | ||
1804 | } | ||
1805 | } | ||
1806 | |||
1807 | /* -- Head of trace ------------------------------------------------------- */ | ||
1808 | |||
1809 | /* Coalesce BASE register for a root trace. */ | ||
1810 | static void asm_head_root_base(ASMState *as) | ||
1811 | { | ||
1812 | IRIns *ir = IR(REF_BASE); | ||
1813 | Reg r = ir->r; | ||
1814 | if (ra_hasreg(r)) { | ||
1815 | ra_free(as, r); | ||
1816 | if (rset_test(as->modset, r)) | ||
1817 | ir->r = RID_INIT; /* No inheritance for modified BASE register. */ | ||
1818 | if (r != RID_BASE) | ||
1819 | emit_mr(as, r, RID_BASE); | ||
1820 | } | ||
1821 | } | ||
1822 | |||
1823 | /* Coalesce BASE register for a side trace. */ | ||
1824 | static RegSet asm_head_side_base(ASMState *as, IRIns *irp, RegSet allow) | ||
1825 | { | ||
1826 | IRIns *ir = IR(REF_BASE); | ||
1827 | Reg r = ir->r; | ||
1828 | if (ra_hasreg(r)) { | ||
1829 | ra_free(as, r); | ||
1830 | if (rset_test(as->modset, r)) | ||
1831 | ir->r = RID_INIT; /* No inheritance for modified BASE register. */ | ||
1832 | if (irp->r == r) { | ||
1833 | rset_clear(allow, r); /* Mark same BASE register as coalesced. */ | ||
1834 | } else if (ra_hasreg(irp->r) && rset_test(as->freeset, irp->r)) { | ||
1835 | rset_clear(allow, irp->r); | ||
1836 | emit_mr(as, r, irp->r); /* Move from coalesced parent reg. */ | ||
1837 | } else { | ||
1838 | emit_getgl(as, r, jit_base); /* Otherwise reload BASE. */ | ||
1839 | } | ||
1840 | } | ||
1841 | return allow; | ||
1842 | } | ||
1843 | |||
1844 | /* -- Tail of trace ------------------------------------------------------- */ | ||
1845 | |||
1846 | /* Fixup the tail code. */ | ||
1847 | static void asm_tail_fixup(ASMState *as, TraceNo lnk) | ||
1848 | { | ||
1849 | MCode *p = as->mctop; | ||
1850 | MCode *target; | ||
1851 | int32_t spadj = as->T->spadjust; | ||
1852 | if (spadj == 0) { | ||
1853 | *--p = PPCI_NOP; | ||
1854 | *--p = PPCI_NOP; | ||
1855 | as->mctop = p; | ||
1856 | } else { | ||
1857 | /* Patch stack adjustment. */ | ||
1858 | lua_assert(checki16(CFRAME_SIZE+spadj)); | ||
1859 | p[-3] = PPCI_ADDI | PPCF_T(RID_TMP) | PPCF_A(RID_SP) | (CFRAME_SIZE+spadj); | ||
1860 | p[-2] = PPCI_STWU | PPCF_T(RID_TMP) | PPCF_A(RID_SP) | spadj; | ||
1861 | } | ||
1862 | /* Patch exit branch. */ | ||
1863 | target = lnk ? traceref(as->J, lnk)->mcode : (MCode *)lj_vm_exit_interp; | ||
1864 | p[-1] = PPCI_B|(((target-p+1)&0x00ffffffu)<<2); | ||
1865 | } | ||
1866 | |||
1867 | /* Prepare tail of code. */ | ||
1868 | static void asm_tail_prep(ASMState *as) | ||
1869 | { | ||
1870 | MCode *p = as->mctop - 1; /* Leave room for exit branch. */ | ||
1871 | if (as->loopref) { | ||
1872 | as->invmcp = as->mcp = p; | ||
1873 | } else { | ||
1874 | as->mcp = p-2; /* Leave room for stack pointer adjustment. */ | ||
1875 | as->invmcp = NULL; | ||
1876 | } | ||
1877 | } | ||
1878 | |||
1879 | /* -- Instruction dispatch ------------------------------------------------ */ | ||
1880 | |||
1881 | /* Assemble a single instruction. */ | ||
1882 | static void asm_ir(ASMState *as, IRIns *ir) | ||
1883 | { | ||
1884 | switch ((IROp)ir->o) { | ||
1885 | /* Miscellaneous ops. */ | ||
1886 | case IR_LOOP: asm_loop(as); break; | ||
1887 | case IR_NOP: case IR_XBAR: lua_assert(!ra_used(ir)); break; | ||
1888 | case IR_USE: | ||
1889 | ra_alloc1(as, ir->op1, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); break; | ||
1890 | case IR_PHI: asm_phi(as, ir); break; | ||
1891 | case IR_HIOP: asm_hiop(as, ir); break; | ||
1892 | |||
1893 | /* Guarded assertions. */ | ||
1894 | case IR_EQ: case IR_NE: | ||
1895 | if ((ir-1)->o == IR_HREF && ir->op1 == as->curins-1) { | ||
1896 | as->curins--; | ||
1897 | asm_href(as, ir-1, (IROp)ir->o); | ||
1898 | break; | ||
1899 | } | ||
1900 | /* fallthrough */ | ||
1901 | case IR_LT: case IR_GE: case IR_LE: case IR_GT: | ||
1902 | case IR_ULT: case IR_UGE: case IR_ULE: case IR_UGT: | ||
1903 | case IR_ABC: | ||
1904 | asm_comp(as, ir); | ||
1905 | break; | ||
1906 | |||
1907 | case IR_RETF: asm_retf(as, ir); break; | ||
1908 | |||
1909 | /* Bit ops. */ | ||
1910 | case IR_BNOT: asm_bitnot(as, ir); break; | ||
1911 | case IR_BSWAP: asm_bitswap(as, ir); break; | ||
1912 | |||
1913 | case IR_BAND: asm_bitand(as, ir); break; | ||
1914 | case IR_BOR: asm_bitop(as, ir, PPCI_OR, PPCI_ORI); break; | ||
1915 | case IR_BXOR: asm_bitop(as, ir, PPCI_XOR, PPCI_XORI); break; | ||
1916 | |||
1917 | case IR_BSHL: asm_bitshift(as, ir, PPCI_SLW, 0); break; | ||
1918 | case IR_BSHR: asm_bitshift(as, ir, PPCI_SRW, 1); break; | ||
1919 | case IR_BSAR: asm_bitshift(as, ir, PPCI_SRAW, PPCI_SRAWI); break; | ||
1920 | case IR_BROL: asm_bitshift(as, ir, PPCI_RLWNM|PPCF_MB(0)|PPCF_ME(31), | ||
1921 | PPCI_RLWINM|PPCF_MB(0)|PPCF_ME(31)); break; | ||
1922 | case IR_BROR: lua_assert(0); break; | ||
1923 | |||
1924 | /* Arithmetic ops. */ | ||
1925 | case IR_ADD: asm_add(as, ir); break; | ||
1926 | case IR_SUB: asm_sub(as, ir); break; | ||
1927 | case IR_MUL: asm_mul(as, ir); break; | ||
1928 | case IR_DIV: asm_fparith(as, ir, PPCI_FDIV); break; | ||
1929 | case IR_MOD: asm_callid(as, ir, IRCALL_lj_vm_modi); break; | ||
1930 | case IR_POW: asm_callid(as, ir, IRCALL_lj_vm_powi); break; | ||
1931 | case IR_NEG: asm_neg(as, ir); break; | ||
1932 | |||
1933 | case IR_ABS: asm_fpunary(as, ir, PPCI_FABS); break; | ||
1934 | case IR_ATAN2: asm_callid(as, ir, IRCALL_atan2); break; | ||
1935 | case IR_LDEXP: asm_callid(as, ir, IRCALL_ldexp); break; | ||
1936 | case IR_MIN: asm_min_max(as, ir, 0); break; | ||
1937 | case IR_MAX: asm_min_max(as, ir, 1); break; | ||
1938 | case IR_FPMATH: | ||
1939 | if (ir->op2 == IRFPM_EXP2 && asm_fpjoin_pow(as, ir)) | ||
1940 | break; | ||
1941 | asm_callid(as, ir, IRCALL_lj_vm_floor + ir->op2); | ||
1942 | break; | ||
1943 | |||
1944 | /* Overflow-checking arithmetic ops. */ | ||
1945 | case IR_ADDOV: asm_arithov(as, ir, PPCI_ADDO); break; | ||
1946 | case IR_SUBOV: asm_arithov(as, ir, PPCI_SUBFO); break; | ||
1947 | case IR_MULOV: asm_arithov(as, ir, PPCI_MULLWO); break; | ||
1948 | |||
1949 | /* Memory references. */ | ||
1950 | case IR_AREF: asm_aref(as, ir); break; | ||
1951 | case IR_HREF: asm_href(as, ir, 0); break; | ||
1952 | case IR_HREFK: asm_hrefk(as, ir); break; | ||
1953 | case IR_NEWREF: asm_newref(as, ir); break; | ||
1954 | case IR_UREFO: case IR_UREFC: asm_uref(as, ir); break; | ||
1955 | case IR_FREF: asm_fref(as, ir); break; | ||
1956 | case IR_STRREF: asm_strref(as, ir); break; | ||
1957 | |||
1958 | /* Loads and stores. */ | ||
1959 | case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD: | ||
1960 | asm_ahuvload(as, ir); | ||
1961 | break; | ||
1962 | case IR_FLOAD: asm_fload(as, ir); break; | ||
1963 | case IR_XLOAD: asm_xload(as, ir); break; | ||
1964 | case IR_SLOAD: asm_sload(as, ir); break; | ||
1965 | |||
1966 | case IR_ASTORE: case IR_HSTORE: case IR_USTORE: asm_ahustore(as, ir); break; | ||
1967 | case IR_FSTORE: asm_fstore(as, ir); break; | ||
1968 | case IR_XSTORE: asm_xstore(as, ir); break; | ||
1969 | |||
1970 | /* Allocations. */ | ||
1971 | case IR_SNEW: case IR_XSNEW: asm_snew(as, ir); break; | ||
1972 | case IR_TNEW: asm_tnew(as, ir); break; | ||
1973 | case IR_TDUP: asm_tdup(as, ir); break; | ||
1974 | case IR_CNEW: case IR_CNEWI: asm_cnew(as, ir); break; | ||
1975 | |||
1976 | /* Write barriers. */ | ||
1977 | case IR_TBAR: asm_tbar(as, ir); break; | ||
1978 | case IR_OBAR: asm_obar(as, ir); break; | ||
1979 | |||
1980 | /* Type conversions. */ | ||
1981 | case IR_CONV: asm_conv(as, ir); break; | ||
1982 | case IR_TOBIT: asm_tobit(as, ir); break; | ||
1983 | case IR_TOSTR: asm_tostr(as, ir); break; | ||
1984 | case IR_STRTO: asm_strto(as, ir); break; | ||
1985 | |||
1986 | /* Calls. */ | ||
1987 | case IR_CALLN: case IR_CALLL: case IR_CALLS: asm_call(as, ir); break; | ||
1988 | case IR_CALLXS: asm_callx(as, ir); break; | ||
1989 | case IR_CARG: break; | ||
1990 | |||
1991 | default: | ||
1992 | setintV(&as->J->errinfo, ir->o); | ||
1993 | lj_trace_err_info(as->J, LJ_TRERR_NYIIR); | ||
1994 | break; | ||
1995 | } | ||
1996 | } | ||
1997 | |||
1998 | /* -- Trace setup --------------------------------------------------------- */ | ||
1999 | |||
2000 | /* Ensure there are enough stack slots for call arguments. */ | ||
2001 | static Reg asm_setup_call_slots(ASMState *as, IRIns *ir, const CCallInfo *ci) | ||
2002 | { | ||
2003 | IRRef args[CCI_NARGS_MAX]; | ||
2004 | uint32_t i, nargs = (int)CCI_NARGS(ci); | ||
2005 | int nslots = 2, ngpr = REGARG_NUMGPR, nfpr = REGARG_NUMFPR; | ||
2006 | asm_collectargs(as, ir, ci, args); | ||
2007 | for (i = 0; i < nargs; i++) | ||
2008 | if (args[i] && irt_isfp(IR(args[i])->t)) { | ||
2009 | if (nfpr > 0) nfpr--; else nslots = (nslots+3) & ~1; | ||
2010 | } else { | ||
2011 | if (ngpr > 0) ngpr--; else nslots++; | ||
2012 | } | ||
2013 | if (nslots > as->evenspill) /* Leave room for args in stack slots. */ | ||
2014 | as->evenspill = nslots; | ||
2015 | return irt_isfp(ir->t) ? REGSP_HINT(RID_FPRET) : REGSP_HINT(RID_RET); | ||
2016 | } | ||
2017 | |||
2018 | static void asm_setup_target(ASMState *as) | ||
2019 | { | ||
2020 | asm_exitstub_setup(as, as->T->nsnap + (as->parent ? 1 : 0)); | ||
2021 | } | ||
2022 | |||
2023 | /* -- Trace patching ------------------------------------------------------ */ | ||
2024 | |||
2025 | /* Patch exit jumps of existing machine code to a new target. */ | ||
2026 | void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, MCode *target) | ||
2027 | { | ||
2028 | MCode *p = T->mcode; | ||
2029 | MCode *pe = (MCode *)((char *)p + T->szmcode); | ||
2030 | MCode *px = exitstub_trace_addr(T, exitno); | ||
2031 | MCode *cstart = NULL; | ||
2032 | MCode *mcarea = lj_mcode_patch(J, p, 0); | ||
2033 | int clearso = 0; | ||
2034 | for (; p < pe; p++) { | ||
2035 | /* Look for exitstub branch, try to replace with branch to target. */ | ||
2036 | uint32_t ins = *p; | ||
2037 | if ((ins & 0xfc000000u) == 0x40000000u && | ||
2038 | ((ins ^ ((char *)px-(char *)p)) & 0xffffu) == 0) { | ||
2039 | ptrdiff_t delta = (char *)target - (char *)p; | ||
2040 | if (((ins >> 16) & 3) == (CC_SO&3)) { | ||
2041 | clearso = sizeof(MCode); | ||
2042 | delta -= sizeof(MCode); | ||
2043 | } | ||
2044 | /* Many, but not all short-range branches can be patched directly. */ | ||
2045 | if (((delta + 0x8000) >> 16) == 0) { | ||
2046 | *p = (ins & 0xffdf0000u) | ((uint32_t)delta & 0xffffu) | | ||
2047 | ((delta & 0x8000) * (PPCF_Y/0x8000)); | ||
2048 | if (!cstart) cstart = p; | ||
2049 | } | ||
2050 | } else if ((ins & 0xfc000000u) == PPCI_B && | ||
2051 | ((ins ^ ((char *)px-(char *)p)) & 0x03ffffffu) == 0) { | ||
2052 | ptrdiff_t delta = (char *)target - (char *)p; | ||
2053 | lua_assert(((delta + 0x02000000) >> 26) == 0); | ||
2054 | *p = PPCI_B | ((uint32_t)delta & 0x03ffffffu); | ||
2055 | if (!cstart) cstart = p; | ||
2056 | } | ||
2057 | } | ||
2058 | { /* Always patch long-range branch in exit stub itself. */ | ||
2059 | ptrdiff_t delta = (char *)target - (char *)px - clearso; | ||
2060 | lua_assert(((delta + 0x02000000) >> 26) == 0); | ||
2061 | *px = PPCI_B | ((uint32_t)delta & 0x03ffffffu); | ||
2062 | } | ||
2063 | if (!cstart) cstart = px; | ||
2064 | asm_cache_flush(cstart, px+1); | ||
2065 | if (clearso) { /* Extend the current trace. Ugly workaround. */ | ||
2066 | MCode *pp = J->cur.mcode; | ||
2067 | J->cur.szmcode += sizeof(MCode); | ||
2068 | *--pp = PPCI_MCRXR; /* Clear SO flag. */ | ||
2069 | J->cur.mcode = pp; | ||
2070 | asm_cache_flush(pp, pp+1); | ||
2071 | } | ||
2072 | lj_mcode_patch(J, mcarea, 1); | ||
2073 | } | ||
2074 | |||
diff --git a/src/lj_crecord.c b/src/lj_crecord.c index 001cf600..4e6a644a 100644 --- a/src/lj_crecord.c +++ b/src/lj_crecord.c | |||
@@ -526,9 +526,13 @@ again: | |||
526 | idx = emitir(IRT(IR_BAND, IRT_INTP), idx, lj_ir_kintp(J, 1)); | 526 | idx = emitir(IRT(IR_BAND, IRT_INTP), idx, lj_ir_kintp(J, 1)); |
527 | sz = lj_ctype_size(cts, (sid = ctype_cid(ct->info))); | 527 | sz = lj_ctype_size(cts, (sid = ctype_cid(ct->info))); |
528 | idx = crec_reassoc_ofs(J, idx, &ofs, sz); | 528 | idx = crec_reassoc_ofs(J, idx, &ofs, sz); |
529 | #if LJ_TARGET_ARM || LJ_TARGET_PPC | ||
530 | /* Hoist base add to allow fusion of index/shift into operands. */ | ||
531 | if (LJ_LIKELY(J->flags & JIT_F_OPT_LOOP) && ofs | ||
529 | #if LJ_TARGET_ARM | 532 | #if LJ_TARGET_ARM |
530 | /* Hoist base add to allow fusion of shifts into operands. */ | 533 | && (sz == 1 || sz == 4) |
531 | if (LJ_LIKELY(J->flags & JIT_F_OPT_LOOP) && ofs && (sz == 1 || sz == 4)) { | 534 | #endif |
535 | ) { | ||
532 | ptr = emitir(IRT(IR_ADD, IRT_PTR), ptr, lj_ir_kintp(J, ofs)); | 536 | ptr = emitir(IRT(IR_ADD, IRT_PTR), ptr, lj_ir_kintp(J, ofs)); |
533 | ofs = 0; | 537 | ofs = 0; |
534 | } | 538 | } |
diff --git a/src/lj_emit_ppc.h b/src/lj_emit_ppc.h new file mode 100644 index 00000000..e44221cd --- /dev/null +++ b/src/lj_emit_ppc.h | |||
@@ -0,0 +1,232 @@ | |||
1 | /* | ||
2 | ** PPC instruction emitter. | ||
3 | ** Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h | ||
4 | */ | ||
5 | |||
6 | /* -- Emit basic instructions --------------------------------------------- */ | ||
7 | |||
8 | static void emit_tab(ASMState *as, PPCIns pi, Reg rt, Reg ra, Reg rb) | ||
9 | { | ||
10 | *--as->mcp = pi | PPCF_T(rt) | PPCF_A(ra) | PPCF_B(rb); | ||
11 | } | ||
12 | |||
13 | #define emit_asb(as, pi, ra, rs, rb) emit_tab(as, (pi), (rs), (ra), (rb)) | ||
14 | #define emit_as(as, pi, ra, rs) emit_tab(as, (pi), (rs), (ra), 0) | ||
15 | #define emit_ab(as, pi, ra, rb) emit_tab(as, (pi), 0, (ra), (rb)) | ||
16 | |||
17 | static void emit_tai(ASMState *as, PPCIns pi, Reg rt, Reg ra, int32_t i) | ||
18 | { | ||
19 | *--as->mcp = pi | PPCF_T(rt) | PPCF_A(ra) | (i & 0xffff); | ||
20 | } | ||
21 | |||
22 | #define emit_ti(as, pi, rt, i) emit_tai(as, (pi), (rt), 0, (i)) | ||
23 | #define emit_ai(as, pi, ra, i) emit_tai(as, (pi), 0, (ra), (i)) | ||
24 | #define emit_asi(as, pi, ra, rs, i) emit_tai(as, (pi), (rs), (ra), (i)) | ||
25 | |||
26 | #define emit_fab(as, pi, rf, ra, rb) \ | ||
27 | emit_tab(as, (pi), (rf)&31, (ra)&31, (rb)&31) | ||
28 | #define emit_fb(as, pi, rf, rb) emit_tab(as, (pi), (rf)&31, 0, (rb)&31) | ||
29 | #define emit_fac(as, pi, rf, ra, rc) \ | ||
30 | emit_tab(as, (pi) | PPCF_C((rc) & 31), (rf)&31, (ra)&31, 0) | ||
31 | #define emit_facb(as, pi, rf, ra, rc, rb) \ | ||
32 | emit_tab(as, (pi) | PPCF_C((rc) & 31), (rf)&31, (ra)&31, (rb)&31) | ||
33 | #define emit_fai(as, pi, rf, ra, i) emit_tai(as, (pi), (rf)&31, (ra), (i)) | ||
34 | |||
35 | static void emit_rot(ASMState *as, PPCIns pi, Reg ra, Reg rs, | ||
36 | int32_t n, int32_t b, int32_t e) | ||
37 | { | ||
38 | *--as->mcp = pi | PPCF_T(rs) | PPCF_A(ra) | PPCF_B(n) | | ||
39 | PPCF_MB(b) | PPCF_ME(e); | ||
40 | } | ||
41 | |||
42 | static void emit_slwi(ASMState *as, Reg ra, Reg rs, int32_t n) | ||
43 | { | ||
44 | lua_assert(n >= 0 && n < 32); | ||
45 | emit_rot(as, PPCI_RLWINM, ra, rs, n, 0, 31-n); | ||
46 | } | ||
47 | |||
48 | static void emit_rotlwi(ASMState *as, Reg ra, Reg rs, int32_t n) | ||
49 | { | ||
50 | lua_assert(n >= 0 && n < 32); | ||
51 | emit_rot(as, PPCI_RLWINM, ra, rs, n, 0, 31); | ||
52 | } | ||
53 | |||
54 | /* -- Emit loads/stores --------------------------------------------------- */ | ||
55 | |||
56 | /* Prefer rematerialization of BASE/L from global_State over spills. */ | ||
57 | #define emit_canremat(ref) ((ref) <= REF_BASE) | ||
58 | |||
59 | /* Try to find a one step delta relative to another constant. */ | ||
60 | static int emit_kdelta1(ASMState *as, Reg t, int32_t i) | ||
61 | { | ||
62 | RegSet work = ~as->freeset & RSET_GPR; | ||
63 | while (work) { | ||
64 | Reg r = rset_picktop(work); | ||
65 | IRRef ref = regcost_ref(as->cost[r]); | ||
66 | lua_assert(r != t); | ||
67 | if (ref < ASMREF_L) { | ||
68 | int32_t delta = i - (ra_iskref(ref) ? ra_krefk(as, ref) : IR(ref)->i); | ||
69 | if (checki16(delta)) { | ||
70 | emit_tai(as, PPCI_ADDI, t, r, delta); | ||
71 | return 1; | ||
72 | } | ||
73 | } | ||
74 | rset_clear(work, r); | ||
75 | } | ||
76 | return 0; /* Failed. */ | ||
77 | } | ||
78 | |||
79 | /* Load a 32 bit constant into a GPR. */ | ||
80 | static void emit_loadi(ASMState *as, Reg r, int32_t i) | ||
81 | { | ||
82 | if (checki16(i)) { | ||
83 | emit_ti(as, PPCI_LI, r, i); | ||
84 | } else { | ||
85 | if ((i & 0xffff)) { | ||
86 | int32_t jgl = i32ptr(J2G(as->J)); | ||
87 | if ((uint32_t)(i-jgl) < 65536) { | ||
88 | emit_tai(as, PPCI_ADDI, r, RID_JGL, i-jgl-32768); | ||
89 | return; | ||
90 | } else if (emit_kdelta1(as, r, i)) { | ||
91 | return; | ||
92 | } | ||
93 | emit_asi(as, PPCI_ORI, r, r, i); | ||
94 | } | ||
95 | emit_ti(as, PPCI_LIS, r, (i >> 16)); | ||
96 | } | ||
97 | } | ||
98 | |||
99 | #define emit_loada(as, r, addr) emit_loadi(as, (r), i32ptr((addr))) | ||
100 | |||
101 | static Reg ra_allock(ASMState *as, int32_t k, RegSet allow); | ||
102 | |||
103 | /* Get/set from constant pointer. */ | ||
104 | static void emit_lsptr(ASMState *as, PPCIns pi, Reg r, void *p, RegSet allow) | ||
105 | { | ||
106 | int32_t jgl = i32ptr(J2G(as->J)); | ||
107 | int32_t i = i32ptr(p); | ||
108 | Reg base; | ||
109 | if ((uint32_t)(i-jgl) < 65536) { | ||
110 | i = i-jgl-32768; | ||
111 | base = RID_JGL; | ||
112 | } else { | ||
113 | base = ra_allock(as, i-(int16_t)i, allow); | ||
114 | } | ||
115 | emit_tai(as, pi, r, base, i); | ||
116 | } | ||
117 | |||
118 | #define emit_loadn(as, r, tv) \ | ||
119 | emit_lsptr(as, PPCI_LFD, ((r) & 31), (void *)(tv), RSET_GPR) | ||
120 | |||
121 | /* Get/set global_State fields. */ | ||
122 | static void emit_lsglptr(ASMState *as, PPCIns pi, Reg r, int32_t ofs) | ||
123 | { | ||
124 | emit_tai(as, pi, r, RID_JGL, ofs-32768); | ||
125 | } | ||
126 | |||
127 | #define emit_getgl(as, r, field) \ | ||
128 | emit_lsglptr(as, PPCI_LWZ, (r), (int32_t)offsetof(global_State, field)) | ||
129 | #define emit_setgl(as, r, field) \ | ||
130 | emit_lsglptr(as, PPCI_STW, (r), (int32_t)offsetof(global_State, field)) | ||
131 | |||
132 | /* Trace number is determined from per-trace exit stubs. */ | ||
133 | #define emit_setvmstate(as, i) UNUSED(i) | ||
134 | |||
135 | /* -- Emit control-flow instructions -------------------------------------- */ | ||
136 | |||
137 | /* Label for internal jumps. */ | ||
138 | typedef MCode *MCLabel; | ||
139 | |||
140 | /* Return label pointing to current PC. */ | ||
141 | #define emit_label(as) ((as)->mcp) | ||
142 | |||
143 | static void emit_condbranch(ASMState *as, PPCIns pi, PPCCC cc, MCode *target) | ||
144 | { | ||
145 | MCode *p = as->mcp; | ||
146 | ptrdiff_t delta = ((char *)target - (char *)p) + 4; | ||
147 | lua_assert(((delta + 0x8000) >> 16) == 0); | ||
148 | pi ^= (delta & 0x8000) * (PPCF_Y/0x8000); | ||
149 | *--p = pi | PPCF_CC(cc) | ((uint32_t)delta & 0xffffu); | ||
150 | as->mcp = p; | ||
151 | } | ||
152 | |||
153 | static void emit_call(ASMState *as, void *target) | ||
154 | { | ||
155 | MCode *p = --as->mcp; | ||
156 | ptrdiff_t delta = (char *)target - (char *)p; | ||
157 | if ((((delta>>2) + 0x00800000) >> 24) == 0) { | ||
158 | *p = PPCI_BL | (delta & 0x03fffffcu); | ||
159 | } else { /* Target out of range: need indirect call. Don't use arg reg. */ | ||
160 | RegSet allow = RSET_GPR & ~RSET_RANGE(RID_R0, REGARG_LASTGPR+1); | ||
161 | Reg r = ra_allock(as, i32ptr(target), allow); | ||
162 | *p = PPCI_BCTRL; | ||
163 | p[-1] = PPCI_MTCTR | PPCF_T(r); | ||
164 | as->mcp = p-1; | ||
165 | } | ||
166 | } | ||
167 | |||
168 | /* -- Emit generic operations --------------------------------------------- */ | ||
169 | |||
170 | #define emit_mr(as, dst, src) \ | ||
171 | emit_asb(as, PPCI_MR, (dst), (src), (src)) | ||
172 | |||
173 | /* Generic move between two regs. */ | ||
174 | static void emit_movrr(ASMState *as, IRIns *ir, Reg dst, Reg src) | ||
175 | { | ||
176 | UNUSED(ir); | ||
177 | if (dst < RID_MAX_GPR) | ||
178 | emit_mr(as, dst, src); | ||
179 | else | ||
180 | emit_fb(as, PPCI_FMR, dst, src); | ||
181 | } | ||
182 | |||
183 | /* Generic load of register from stack slot. */ | ||
184 | static void emit_spload(ASMState *as, IRIns *ir, Reg r, int32_t ofs) | ||
185 | { | ||
186 | if (r < RID_MAX_GPR) | ||
187 | emit_tai(as, PPCI_LWZ, r, RID_SP, ofs); | ||
188 | else | ||
189 | emit_fai(as, irt_isnum(ir->t) ? PPCI_LFD : PPCI_LFS, r, RID_SP, ofs); | ||
190 | } | ||
191 | |||
192 | /* Generic store of register to stack slot. */ | ||
193 | static void emit_spstore(ASMState *as, IRIns *ir, Reg r, int32_t ofs) | ||
194 | { | ||
195 | if (r < RID_MAX_GPR) | ||
196 | emit_tai(as, PPCI_STW, r, RID_SP, ofs); | ||
197 | else | ||
198 | emit_fai(as, irt_isnum(ir->t) ? PPCI_STFD : PPCI_STFS, r, RID_SP, ofs); | ||
199 | } | ||
200 | |||
201 | /* Emit a compare (for equality) with a constant operand. */ | ||
202 | static void emit_cmpi(ASMState *as, Reg r, int32_t k) | ||
203 | { | ||
204 | if (checki16(k)) { | ||
205 | emit_ai(as, PPCI_CMPWI, r, k); | ||
206 | } else if (checku16(k)) { | ||
207 | emit_ai(as, PPCI_CMPLWI, r, k); | ||
208 | } else { | ||
209 | emit_ai(as, PPCI_CMPLWI, RID_TMP, k); | ||
210 | emit_asi(as, PPCI_XORIS, RID_TMP, r, (k >> 16)); | ||
211 | } | ||
212 | } | ||
213 | |||
214 | /* Add offset to pointer. */ | ||
215 | static void emit_addptr(ASMState *as, Reg r, int32_t ofs) | ||
216 | { | ||
217 | if (ofs) { | ||
218 | emit_tai(as, PPCI_ADDI, r, r, ofs); | ||
219 | if (!checki16(ofs)) | ||
220 | emit_tai(as, PPCI_ADDIS, r, r, (ofs + 32768) >> 16); | ||
221 | } | ||
222 | } | ||
223 | |||
224 | static void emit_spsub(ASMState *as, int32_t ofs) | ||
225 | { | ||
226 | if (ofs) { | ||
227 | emit_tai(as, PPCI_STWU, RID_TMP, RID_SP, -ofs); | ||
228 | emit_tai(as, PPCI_ADDI, RID_TMP, RID_SP, | ||
229 | CFRAME_SIZE + (as->parent ? as->parent->spadjust : 0)); | ||
230 | } | ||
231 | } | ||
232 | |||
diff --git a/src/lj_target.h b/src/lj_target.h index 33feb17c..a80cc027 100644 --- a/src/lj_target.h +++ b/src/lj_target.h | |||
@@ -50,21 +50,30 @@ typedef uint32_t RegSP; | |||
50 | 50 | ||
51 | /* -- Register sets ------------------------------------------------------- */ | 51 | /* -- Register sets ------------------------------------------------------- */ |
52 | 52 | ||
53 | /* Bitset for registers. 32 registers suffice right now. | 53 | /* Bitset for registers. 32 registers suffice for most architectures. |
54 | ** Note that one set holds bits for both GPRs and FPRs. | 54 | ** Note that one set holds bits for both GPRs and FPRs. |
55 | */ | 55 | */ |
56 | #if LJ_TARGET_PPC | ||
57 | typedef uint64_t RegSet; | ||
58 | #else | ||
56 | typedef uint32_t RegSet; | 59 | typedef uint32_t RegSet; |
60 | #endif | ||
57 | 61 | ||
58 | #define RID2RSET(r) (((RegSet)1) << (r)) | 62 | #define RID2RSET(r) (((RegSet)1) << (r)) |
59 | #define RSET_EMPTY 0 | 63 | #define RSET_EMPTY ((RegSet)0) |
60 | #define RSET_RANGE(lo, hi) ((RID2RSET((hi)-(lo))-1) << (lo)) | 64 | #define RSET_RANGE(lo, hi) ((RID2RSET((hi)-(lo))-1) << (lo)) |
61 | 65 | ||
62 | #define rset_test(rs, r) (((rs) >> (r)) & 1) | 66 | #define rset_test(rs, r) (((rs) >> (r)) & 1) |
63 | #define rset_set(rs, r) (rs |= RID2RSET(r)) | 67 | #define rset_set(rs, r) (rs |= RID2RSET(r)) |
64 | #define rset_clear(rs, r) (rs &= ~RID2RSET(r)) | 68 | #define rset_clear(rs, r) (rs &= ~RID2RSET(r)) |
65 | #define rset_exclude(rs, r) (rs & ~RID2RSET(r)) | 69 | #define rset_exclude(rs, r) (rs & ~RID2RSET(r)) |
70 | #if LJ_TARGET_PPC | ||
71 | #define rset_picktop(rs) ((Reg)(__builtin_clzll(rs)^63)) | ||
72 | #define rset_pickbot(rs) ((Reg)__builtin_ctzll(rs)) | ||
73 | #else | ||
66 | #define rset_picktop(rs) ((Reg)lj_fls(rs)) | 74 | #define rset_picktop(rs) ((Reg)lj_fls(rs)) |
67 | #define rset_pickbot(rs) ((Reg)lj_ffs(rs)) | 75 | #define rset_pickbot(rs) ((Reg)lj_ffs(rs)) |
76 | #endif | ||
68 | 77 | ||
69 | /* -- Register allocation cost -------------------------------------------- */ | 78 | /* -- Register allocation cost -------------------------------------------- */ |
70 | 79 | ||
@@ -127,6 +136,8 @@ typedef uint32_t RegCost; | |||
127 | #include "lj_target_x86.h" | 136 | #include "lj_target_x86.h" |
128 | #elif LJ_TARGET_ARM | 137 | #elif LJ_TARGET_ARM |
129 | #include "lj_target_arm.h" | 138 | #include "lj_target_arm.h" |
139 | #elif LJ_TARGET_PPC | ||
140 | #include "lj_target_ppc.h" | ||
130 | #else | 141 | #else |
131 | #error "Missing include for target CPU" | 142 | #error "Missing include for target CPU" |
132 | #endif | 143 | #endif |
diff --git a/src/lj_target_ppc.h b/src/lj_target_ppc.h new file mode 100644 index 00000000..d0b3f4d0 --- /dev/null +++ b/src/lj_target_ppc.h | |||
@@ -0,0 +1,273 @@ | |||
1 | /* | ||
2 | ** Definitions for PPC CPUs. | ||
3 | ** Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h | ||
4 | */ | ||
5 | |||
6 | #ifndef _LJ_TARGET_PPC_H | ||
7 | #define _LJ_TARGET_PPC_H | ||
8 | |||
9 | /* -- Registers IDs ------------------------------------------------------- */ | ||
10 | |||
11 | #define GPRDEF(_) \ | ||
12 | _(R0) _(SP) _(SYS1) _(R3) _(R4) _(R5) _(R6) _(R7) \ | ||
13 | _(R8) _(R9) _(R10) _(R11) _(R12) _(SYS2) _(R14) _(R15) \ | ||
14 | _(R16) _(R17) _(R18) _(R19) _(R20) _(R21) _(R22) _(R23) \ | ||
15 | _(R24) _(R25) _(R26) _(R27) _(R28) _(R29) _(R30) _(R31) | ||
16 | #define FPRDEF(_) \ | ||
17 | _(F0) _(F1) _(F2) _(F3) _(F4) _(F5) _(F6) _(F7) \ | ||
18 | _(F8) _(F9) _(F10) _(F11) _(F12) _(F13) _(F14) _(F15) \ | ||
19 | _(F16) _(F17) _(F18) _(F19) _(F20) _(F21) _(F22) _(F23) \ | ||
20 | _(F24) _(F25) _(F26) _(F27) _(F28) _(F29) _(F30) _(F31) | ||
21 | #define VRIDDEF(_) | ||
22 | |||
23 | #define RIDENUM(name) RID_##name, | ||
24 | |||
25 | enum { | ||
26 | GPRDEF(RIDENUM) /* General-purpose registers (GPRs). */ | ||
27 | FPRDEF(RIDENUM) /* Floating-point registers (FPRs). */ | ||
28 | RID_MAX, | ||
29 | RID_TMP = RID_R0, | ||
30 | |||
31 | /* Calling conventions. */ | ||
32 | RID_RET = RID_R3, | ||
33 | RID_RETHI = RID_R3, | ||
34 | RID_RETLO = RID_R4, | ||
35 | RID_FPRET = RID_F1, | ||
36 | |||
37 | /* These definitions must match with the *.dasc file(s): */ | ||
38 | RID_BASE = RID_R14, /* Interpreter BASE. */ | ||
39 | RID_LPC = RID_R16, /* Interpreter PC. */ | ||
40 | RID_DISPATCH = RID_R17, /* Interpreter DISPATCH table. */ | ||
41 | RID_LREG = RID_R18, /* Interpreter L. */ | ||
42 | RID_JGL = RID_R31, /* On-trace: global_State + 32768. */ | ||
43 | |||
44 | /* Register ranges [min, max) and number of registers. */ | ||
45 | RID_MIN_GPR = RID_R0, | ||
46 | RID_MAX_GPR = RID_R31+1, | ||
47 | RID_MIN_FPR = RID_F0, | ||
48 | RID_MAX_FPR = RID_F31+1, | ||
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 TMP, SP, SYS1, SYS2 and JGL. */ | ||
59 | #define RSET_FIXED \ | ||
60 | (RID2RSET(RID_TMP)|RID2RSET(RID_SP)|RID2RSET(RID_SYS1)|\ | ||
61 | RID2RSET(RID_SYS2)|RID2RSET(RID_JGL)) | ||
62 | #define RSET_GPR (RSET_RANGE(RID_MIN_GPR, RID_MAX_GPR) - RSET_FIXED) | ||
63 | #define RSET_FPR RSET_RANGE(RID_MIN_FPR, RID_MAX_FPR) | ||
64 | #define RSET_ALL (RSET_GPR|RSET_FPR) | ||
65 | #define RSET_INIT RSET_ALL | ||
66 | |||
67 | #define RSET_SCRATCH_GPR (RSET_RANGE(RID_R3, RID_R12+1)) | ||
68 | #define RSET_SCRATCH_FPR (RSET_RANGE(RID_F0, RID_F13+1)) | ||
69 | #define RSET_SCRATCH (RSET_SCRATCH_GPR|RSET_SCRATCH_FPR) | ||
70 | #define REGARG_FIRSTGPR RID_R3 | ||
71 | #define REGARG_LASTGPR RID_R10 | ||
72 | #define REGARG_NUMGPR 8 | ||
73 | #define REGARG_FIRSTFPR RID_F1 | ||
74 | #define REGARG_LASTFPR RID_F8 | ||
75 | #define REGARG_NUMFPR 8 | ||
76 | |||
77 | /* -- Spill slots --------------------------------------------------------- */ | ||
78 | |||
79 | /* Spill slots are 32 bit wide. An even/odd pair is used for FPRs. | ||
80 | ** | ||
81 | ** SPS_FIXED: Available fixed spill slots in interpreter frame. | ||
82 | ** This definition must match with the *.dasc file(s). | ||
83 | ** | ||
84 | ** SPS_FIRST: First spill slot for general use. | ||
85 | ** [sp+12] tmplo word \ | ||
86 | ** [sp+ 8] tmphi word / tmp dword, parameter area for callee | ||
87 | ** [sp+ 4] tmpw, LR of callee | ||
88 | ** [sp+ 0] stack chain | ||
89 | */ | ||
90 | #define SPS_FIXED 7 | ||
91 | #define SPS_FIRST 4 | ||
92 | |||
93 | /* Stack offsets for temporary slots. Used for FP<->int conversions etc. */ | ||
94 | #define SPOFS_TMPW 4 | ||
95 | #define SPOFS_TMP 8 | ||
96 | #define SPOFS_TMPHI 8 | ||
97 | #define SPOFS_TMPLO 12 | ||
98 | |||
99 | #define sps_scale(slot) (4 * (int32_t)(slot)) | ||
100 | #define sps_align(slot) (((slot) - SPS_FIXED + 3) & ~3) | ||
101 | |||
102 | /* -- Exit state ---------------------------------------------------------- */ | ||
103 | |||
104 | /* This definition must match with the *.dasc file(s). */ | ||
105 | typedef struct { | ||
106 | lua_Number fpr[RID_NUM_FPR]; /* Floating-point registers. */ | ||
107 | int32_t gpr[RID_NUM_GPR]; /* General-purpose registers. */ | ||
108 | int32_t spill[256]; /* Spill slots. */ | ||
109 | } ExitState; | ||
110 | |||
111 | /* Highest exit + 1 indicates stack check. */ | ||
112 | #define EXITSTATE_CHECKEXIT 1 | ||
113 | |||
114 | /* Return the address of a per-trace exit stub. */ | ||
115 | static LJ_AINLINE MCode *exitstub_trace_addr(GCtrace *T, ExitNo exitno) | ||
116 | { | ||
117 | /* Keep this in-sync with asm_exitstub_*. */ | ||
118 | MCode *p = (MCode *)((char *)T->mcode + T->szmcode); | ||
119 | while (*p == 0x60000000) p++; /* Skip PPCI_NOP. */ | ||
120 | return p + 3 + exitno; | ||
121 | } | ||
122 | |||
123 | /* -- Instructions -------------------------------------------------------- */ | ||
124 | |||
125 | /* Instruction fields. */ | ||
126 | #define PPCF_CC(cc) ((((cc) & 3) << 16) | (((cc) & 4) << 22)) | ||
127 | #define PPCF_T(r) ((r) << 21) | ||
128 | #define PPCF_A(r) ((r) << 16) | ||
129 | #define PPCF_B(r) ((r) << 11) | ||
130 | #define PPCF_C(r) ((r) << 6) | ||
131 | #define PPCF_MB(n) ((n) << 6) | ||
132 | #define PPCF_ME(n) ((n) << 1) | ||
133 | #define PPCF_Y 0x00200000 | ||
134 | #define PPCF_DOT 0x00000001 | ||
135 | |||
136 | typedef enum PPCIns { | ||
137 | /* Integer instructions. */ | ||
138 | PPCI_MR = 0x7c000378, | ||
139 | PPCI_NOP = 0x60000000, | ||
140 | |||
141 | PPCI_LI = 0x38000000, | ||
142 | PPCI_LIS = 0x3c000000, | ||
143 | |||
144 | PPCI_ADD = 0x7c000214, | ||
145 | PPCI_ADDC = 0x7c000014, | ||
146 | PPCI_ADDO = 0x7c000614, | ||
147 | PPCI_ADDE = 0x7c000114, | ||
148 | PPCI_ADDZE = 0x7c000194, | ||
149 | PPCI_ADDME = 0x7c0001d4, | ||
150 | PPCI_ADDI = 0x38000000, | ||
151 | PPCI_ADDIS = 0x3c000000, | ||
152 | PPCI_ADDIC = 0x30000000, | ||
153 | PPCI_ADDICDOT = 0x34000000, | ||
154 | |||
155 | PPCI_SUBF = 0x7c000050, | ||
156 | PPCI_SUBFC = 0x7c000010, | ||
157 | PPCI_SUBFO = 0x7c000450, | ||
158 | PPCI_SUBFE = 0x7c000110, | ||
159 | PPCI_SUBFZE = 0x7c000190, | ||
160 | PPCI_SUBFME = 0x7c0001d0, | ||
161 | PPCI_SUBFIC = 0x20000000, | ||
162 | |||
163 | PPCI_NEG = 0x7c0000d0, | ||
164 | |||
165 | PPCI_AND = 0x7c000038, | ||
166 | PPCI_ANDC = 0x7c000078, | ||
167 | PPCI_NAND = 0x7c0003b8, | ||
168 | PPCI_ANDIDOT = 0x70000000, | ||
169 | PPCI_ANDISDOT = 0x74000000, | ||
170 | |||
171 | PPCI_OR = 0x7c000378, | ||
172 | PPCI_NOR = 0x7c0000f8, | ||
173 | PPCI_ORI = 0x60000000, | ||
174 | PPCI_ORIS = 0x64000000, | ||
175 | |||
176 | PPCI_XOR = 0x7c000278, | ||
177 | PPCI_EQV = 0x7c000238, | ||
178 | PPCI_XORI = 0x68000000, | ||
179 | PPCI_XORIS = 0x6c000000, | ||
180 | |||
181 | PPCI_CMPW = 0x7c000000, | ||
182 | PPCI_CMPLW = 0x7c000040, | ||
183 | PPCI_CMPWI = 0x2c000000, | ||
184 | PPCI_CMPLWI = 0x28000000, | ||
185 | |||
186 | PPCI_MULLW = 0x7c0001d6, | ||
187 | PPCI_MULLI = 0x1c000000, | ||
188 | PPCI_MULLWO = 0x7c0005d6, | ||
189 | |||
190 | PPCI_EXTSB = 0x7c000774, | ||
191 | PPCI_EXTSH = 0x7c000734, | ||
192 | |||
193 | PPCI_SLW = 0x7c000030, | ||
194 | PPCI_SRW = 0x7c000430, | ||
195 | PPCI_SRAW = 0x7c000630, | ||
196 | PPCI_SRAWI = 0x7c000670, | ||
197 | |||
198 | PPCI_RLWNM = 0x5c000000, | ||
199 | PPCI_RLWINM = 0x54000000, | ||
200 | PPCI_RLWIMI = 0x50000000, | ||
201 | |||
202 | PPCI_B = 0x48000000, | ||
203 | PPCI_BL = 0x48000001, | ||
204 | PPCI_BC = 0x40800000, | ||
205 | PPCI_BCL = 0x40800001, | ||
206 | PPCI_BCTR = 0x4e800420, | ||
207 | PPCI_BCTRL = 0x4e800421, | ||
208 | |||
209 | PPCI_CRANDC = 0x4c000102, | ||
210 | PPCI_CRAND = 0x4c000202, | ||
211 | PPCI_CRORC = 0x4c000342, | ||
212 | PPCI_CROR = 0x4c000382, | ||
213 | |||
214 | PPCI_MFLR = 0x7c0802a6, | ||
215 | PPCI_MTCTR = 0x7c0903a6, | ||
216 | |||
217 | PPCI_MCRXR = 0x7c000400, | ||
218 | |||
219 | /* Load/store instructions. */ | ||
220 | PPCI_LWZ = 0x80000000, | ||
221 | PPCI_LBZ = 0x88000000, | ||
222 | PPCI_STW = 0x90000000, | ||
223 | PPCI_STB = 0x98000000, | ||
224 | PPCI_LHZ = 0xa0000000, | ||
225 | PPCI_LHA = 0xa8000000, | ||
226 | PPCI_STH = 0xb0000000, | ||
227 | |||
228 | PPCI_STWU = 0x94000000, | ||
229 | |||
230 | PPCI_LFS = 0xc0000000, | ||
231 | PPCI_LFD = 0xc8000000, | ||
232 | PPCI_STFS = 0xd0000000, | ||
233 | PPCI_STFD = 0xd8000000, | ||
234 | |||
235 | PPCI_LWZX = 0x7c00002e, | ||
236 | PPCI_LBZX = 0x7c0000ae, | ||
237 | PPCI_STWX = 0x7c00012e, | ||
238 | PPCI_STBX = 0x7c0001ae, | ||
239 | PPCI_LHZX = 0x7c00022e, | ||
240 | PPCI_LHAX = 0x7c0002ae, | ||
241 | PPCI_STHX = 0x7c00032e, | ||
242 | |||
243 | PPCI_LFSX = 0x7c00042e, | ||
244 | PPCI_LFDX = 0x7c0004ae, | ||
245 | PPCI_STFSX = 0x7c00052e, | ||
246 | PPCI_STFDX = 0x7c0005ae, | ||
247 | |||
248 | /* FP instructions. */ | ||
249 | PPCI_FMR = 0xfc000090, | ||
250 | PPCI_FNEG = 0xfc000050, | ||
251 | PPCI_FABS = 0xfc000210, | ||
252 | |||
253 | PPCI_FRSP = 0xfc000018, | ||
254 | PPCI_FCTIWZ = 0xfc00001e, | ||
255 | |||
256 | PPCI_FADD = 0xfc00002a, | ||
257 | PPCI_FSUB = 0xfc000028, | ||
258 | PPCI_FMUL = 0xfc000032, | ||
259 | PPCI_FDIV = 0xfc000024, | ||
260 | |||
261 | PPCI_FMADD = 0xfc00003a, | ||
262 | PPCI_FMSUB = 0xfc000038, | ||
263 | PPCI_FNMSUB = 0xfc00003c, | ||
264 | |||
265 | PPCI_FCMPU = 0xfc000000, | ||
266 | PPCI_FSEL = 0xfc00002e, | ||
267 | } PPCIns; | ||
268 | |||
269 | typedef enum PPCCC { | ||
270 | CC_GE, CC_LE, CC_NE, CC_NS, CC_LT, CC_GT, CC_EQ, CC_SO | ||
271 | } PPCCC; | ||
272 | |||
273 | #endif | ||