diff options
Diffstat (limited to 'libraries/luajit-2.0/src/buildvm_arm.dasc')
-rw-r--r-- | libraries/luajit-2.0/src/buildvm_arm.dasc | 4115 |
1 files changed, 0 insertions, 4115 deletions
diff --git a/libraries/luajit-2.0/src/buildvm_arm.dasc b/libraries/luajit-2.0/src/buildvm_arm.dasc deleted file mode 100644 index 60857c0..0000000 --- a/libraries/luajit-2.0/src/buildvm_arm.dasc +++ /dev/null | |||
@@ -1,4115 +0,0 @@ | |||
1 | |// Low-level VM code for ARM CPUs. | ||
2 | |// Bytecode interpreter, fast functions and helper functions. | ||
3 | |// Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h | ||
4 | | | ||
5 | |.arch arm | ||
6 | |.section code_op, code_sub | ||
7 | | | ||
8 | |.actionlist build_actionlist | ||
9 | |.globals GLOB_ | ||
10 | |.globalnames globnames | ||
11 | |.externnames extnames | ||
12 | | | ||
13 | |// Note: The ragged indentation of the instructions is intentional. | ||
14 | |// The starting columns indicate data dependencies. | ||
15 | | | ||
16 | |//----------------------------------------------------------------------- | ||
17 | | | ||
18 | |// Fixed register assignments for the interpreter. | ||
19 | | | ||
20 | |// The following must be C callee-save. | ||
21 | |.define MASKR8, r4 // 255*8 constant for fast bytecode decoding. | ||
22 | |.define KBASE, r5 // Constants of current Lua function. | ||
23 | |.define PC, r6 // Next PC. | ||
24 | |.define DISPATCH, r7 // Opcode dispatch table. | ||
25 | |.define LREG, r8 // Register holding lua_State (also in SAVE_L). | ||
26 | | | ||
27 | |// C callee-save in EABI, but often refetched. Temporary in iOS 3.0+. | ||
28 | |.define BASE, r9 // Base of current Lua stack frame. | ||
29 | | | ||
30 | |// The following temporaries are not saved across C calls, except for RA/RC. | ||
31 | |.define RA, r10 // Callee-save. | ||
32 | |.define RC, r11 // Callee-save. | ||
33 | |.define RB, r12 | ||
34 | |.define OP, r12 // Overlaps RB, must not be lr. | ||
35 | |.define INS, lr | ||
36 | | | ||
37 | |// Calling conventions. Also used as temporaries. | ||
38 | |.define CARG1, r0 | ||
39 | |.define CARG2, r1 | ||
40 | |.define CARG3, r2 | ||
41 | |.define CARG4, r3 | ||
42 | |.define CARG12, r0 // For 1st soft-fp double. | ||
43 | |.define CARG34, r2 // For 2nd soft-fp double. | ||
44 | | | ||
45 | |.define CRET1, r0 | ||
46 | |.define CRET2, r1 | ||
47 | | | ||
48 | |// Stack layout while in interpreter. Must match with lj_frame.h. | ||
49 | |.define CFRAME_SPACE, #28 | ||
50 | |.define SAVE_ERRF, [sp, #24] | ||
51 | |.define SAVE_NRES, [sp, #20] | ||
52 | |.define SAVE_CFRAME, [sp, #16] | ||
53 | |.define SAVE_L, [sp, #12] | ||
54 | |.define SAVE_PC, [sp, #8] | ||
55 | |.define SAVE_MULTRES, [sp, #4] | ||
56 | |.define ARG5, [sp] | ||
57 | | | ||
58 | |.define TMPDhi, [sp, #4] | ||
59 | |.define TMPDlo, [sp] | ||
60 | |.define TMPD, [sp] | ||
61 | |.define TMPDp, sp | ||
62 | | | ||
63 | |.macro saveregs | ||
64 | | push {r4, r5, r6, r7, r8, r9, r10, r11, lr} | ||
65 | | sub sp, sp, CFRAME_SPACE | ||
66 | |.endmacro | ||
67 | |.macro restoreregs_ret | ||
68 | | add sp, sp, CFRAME_SPACE | ||
69 | | pop {r4, r5, r6, r7, r8, r9, r10, r11, pc} | ||
70 | |.endmacro | ||
71 | | | ||
72 | |// Type definitions. Some of these are only used for documentation. | ||
73 | |.type L, lua_State, LREG | ||
74 | |.type GL, global_State | ||
75 | |.type TVALUE, TValue | ||
76 | |.type GCOBJ, GCobj | ||
77 | |.type STR, GCstr | ||
78 | |.type TAB, GCtab | ||
79 | |.type LFUNC, GCfuncL | ||
80 | |.type CFUNC, GCfuncC | ||
81 | |.type PROTO, GCproto | ||
82 | |.type UPVAL, GCupval | ||
83 | |.type NODE, Node | ||
84 | |.type NARGS8, int | ||
85 | |.type TRACE, GCtrace | ||
86 | | | ||
87 | |//----------------------------------------------------------------------- | ||
88 | | | ||
89 | |// Trap for not-yet-implemented parts. | ||
90 | |.macro NYI; ud; .endmacro | ||
91 | | | ||
92 | |//----------------------------------------------------------------------- | ||
93 | | | ||
94 | |// Access to frame relative to BASE. | ||
95 | |.define FRAME_FUNC, #-8 | ||
96 | |.define FRAME_PC, #-4 | ||
97 | | | ||
98 | |.macro decode_RA8, dst, ins; and dst, MASKR8, ins, lsr #5; .endmacro | ||
99 | |.macro decode_RB8, dst, ins; and dst, MASKR8, ins, lsr #21; .endmacro | ||
100 | |.macro decode_RC8, dst, ins; and dst, MASKR8, ins, lsr #13; .endmacro | ||
101 | |.macro decode_RD, dst, ins; lsr dst, ins, #16; .endmacro | ||
102 | |.macro decode_OP, dst, ins; and dst, ins, #255; .endmacro | ||
103 | | | ||
104 | |// Instruction fetch. | ||
105 | |.macro ins_NEXT1 | ||
106 | | ldrb OP, [PC] | ||
107 | |.endmacro | ||
108 | |.macro ins_NEXT2 | ||
109 | | ldr INS, [PC], #4 | ||
110 | |.endmacro | ||
111 | |// Instruction decode+dispatch. | ||
112 | |.macro ins_NEXT3 | ||
113 | | ldr OP, [DISPATCH, OP, lsl #2] | ||
114 | | decode_RA8 RA, INS | ||
115 | | decode_RD RC, INS | ||
116 | | bx OP | ||
117 | |.endmacro | ||
118 | |.macro ins_NEXT | ||
119 | | ins_NEXT1 | ||
120 | | ins_NEXT2 | ||
121 | | ins_NEXT3 | ||
122 | |.endmacro | ||
123 | | | ||
124 | |// Instruction footer. | ||
125 | |.if 1 | ||
126 | | // Replicated dispatch. Less unpredictable branches, but higher I-Cache use. | ||
127 | | .define ins_next, ins_NEXT | ||
128 | | .define ins_next_, ins_NEXT | ||
129 | | .define ins_next1, ins_NEXT1 | ||
130 | | .define ins_next2, ins_NEXT2 | ||
131 | | .define ins_next3, ins_NEXT3 | ||
132 | |.else | ||
133 | | // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch. | ||
134 | | // Affects only certain kinds of benchmarks (and only with -j off). | ||
135 | | .macro ins_next | ||
136 | | b ->ins_next | ||
137 | | .endmacro | ||
138 | | .macro ins_next1 | ||
139 | | .endmacro | ||
140 | | .macro ins_next2 | ||
141 | | .endmacro | ||
142 | | .macro ins_next3 | ||
143 | | b ->ins_next | ||
144 | | .endmacro | ||
145 | | .macro ins_next_ | ||
146 | | ->ins_next: | ||
147 | | ins_NEXT | ||
148 | | .endmacro | ||
149 | |.endif | ||
150 | | | ||
151 | |// Avoid register name substitution for field name. | ||
152 | #define field_pc pc | ||
153 | | | ||
154 | |// Call decode and dispatch. | ||
155 | |.macro ins_callt | ||
156 | | // BASE = new base, CARG3 = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC | ||
157 | | ldr PC, LFUNC:CARG3->field_pc | ||
158 | | ldrb OP, [PC] // STALL: load PC. early PC. | ||
159 | | ldr INS, [PC], #4 | ||
160 | | ldr OP, [DISPATCH, OP, lsl #2] // STALL: load OP. early OP. | ||
161 | | decode_RA8 RA, INS | ||
162 | | add RA, RA, BASE | ||
163 | | bx OP | ||
164 | |.endmacro | ||
165 | | | ||
166 | |.macro ins_call | ||
167 | | // BASE = new base, CARG3 = LFUNC/CFUNC, RC = nargs*8, PC = caller PC | ||
168 | | str PC, [BASE, FRAME_PC] | ||
169 | | ins_callt // STALL: locked PC. | ||
170 | |.endmacro | ||
171 | | | ||
172 | |//----------------------------------------------------------------------- | ||
173 | | | ||
174 | |// Macros to test operand types. | ||
175 | |.macro checktp, reg, tp; cmn reg, #-tp; .endmacro | ||
176 | |.macro checktpeq, reg, tp; cmneq reg, #-tp; .endmacro | ||
177 | |.macro checktpne, reg, tp; cmnne reg, #-tp; .endmacro | ||
178 | |.macro checkstr, reg, target; checktp reg, LJ_TSTR; bne target; .endmacro | ||
179 | |.macro checktab, reg, target; checktp reg, LJ_TTAB; bne target; .endmacro | ||
180 | |.macro checkfunc, reg, target; checktp reg, LJ_TFUNC; bne target; .endmacro | ||
181 | | | ||
182 | |// Assumes DISPATCH is relative to GL. | ||
183 | #define DISPATCH_GL(field) (GG_DISP2G + (int)offsetof(global_State, field)) | ||
184 | #define DISPATCH_J(field) (GG_DISP2J + (int)offsetof(jit_State, field)) | ||
185 | | | ||
186 | #define PC2PROTO(field) ((int)offsetof(GCproto, field)-(int)sizeof(GCproto)) | ||
187 | | | ||
188 | |.macro hotcheck, delta | ||
189 | | lsr CARG1, PC, #1 | ||
190 | | and CARG1, CARG1, #126 | ||
191 | | sub CARG1, CARG1, #-GG_DISP2HOT | ||
192 | | ldrh CARG2, [DISPATCH, CARG1] | ||
193 | | subs CARG2, CARG2, #delta | ||
194 | | strh CARG2, [DISPATCH, CARG1] | ||
195 | |.endmacro | ||
196 | | | ||
197 | |.macro hotloop | ||
198 | | hotcheck HOTCOUNT_LOOP | ||
199 | | blo ->vm_hotloop | ||
200 | |.endmacro | ||
201 | | | ||
202 | |.macro hotcall | ||
203 | | hotcheck HOTCOUNT_CALL | ||
204 | | blo ->vm_hotcall | ||
205 | |.endmacro | ||
206 | | | ||
207 | |// Set current VM state. | ||
208 | |.macro mv_vmstate, reg, st; mvn reg, #LJ_VMST_..st; .endmacro | ||
209 | |.macro st_vmstate, reg; str reg, [DISPATCH, #DISPATCH_GL(vmstate)]; .endmacro | ||
210 | | | ||
211 | |// Move table write barrier back. Overwrites mark and tmp. | ||
212 | |.macro barrierback, tab, mark, tmp | ||
213 | | ldr tmp, [DISPATCH, #DISPATCH_GL(gc.grayagain)] | ||
214 | | bic mark, mark, #LJ_GC_BLACK // black2gray(tab) | ||
215 | | str tab, [DISPATCH, #DISPATCH_GL(gc.grayagain)] | ||
216 | | strb mark, tab->marked | ||
217 | | str tmp, tab->gclist | ||
218 | |.endmacro | ||
219 | | | ||
220 | |.macro IOS, a, b | ||
221 | ||if (LJ_TARGET_OSX) { | ||
222 | | a, b | ||
223 | ||} | ||
224 | |.endmacro | ||
225 | | | ||
226 | |//----------------------------------------------------------------------- | ||
227 | |||
228 | #if !LJ_DUALNUM | ||
229 | #error "Only dual-number mode supported for ARM target" | ||
230 | #endif | ||
231 | |||
232 | /* Generate subroutines used by opcodes and other parts of the VM. */ | ||
233 | /* The .code_sub section should be last to help static branch prediction. */ | ||
234 | static void build_subroutines(BuildCtx *ctx) | ||
235 | { | ||
236 | |.code_sub | ||
237 | | | ||
238 | |//----------------------------------------------------------------------- | ||
239 | |//-- Return handling ---------------------------------------------------- | ||
240 | |//----------------------------------------------------------------------- | ||
241 | | | ||
242 | |->vm_returnp: | ||
243 | | // See vm_return. Also: RB = previous base. | ||
244 | | tst PC, #FRAME_P | ||
245 | | beq ->cont_dispatch | ||
246 | | | ||
247 | | // Return from pcall or xpcall fast func. | ||
248 | | ldr PC, [RB, FRAME_PC] // Fetch PC of previous frame. | ||
249 | | mvn CARG2, #~LJ_TTRUE | ||
250 | | mov BASE, RB | ||
251 | | // Prepending may overwrite the pcall frame, so do it at the end. | ||
252 | | str CARG2, [RA, FRAME_PC] // Prepend true to results. | ||
253 | | sub RA, RA, #8 | ||
254 | | | ||
255 | |->vm_returnc: | ||
256 | | add RC, RC, #8 // RC = (nresults+1)*8. | ||
257 | | ands CARG1, PC, #FRAME_TYPE | ||
258 | | str RC, SAVE_MULTRES | ||
259 | | beq ->BC_RET_Z // Handle regular return to Lua. | ||
260 | | | ||
261 | |->vm_return: | ||
262 | | // BASE = base, RA = resultptr, RC/MULTRES = (nresults+1)*8, PC = return | ||
263 | | // CARG1 = PC & FRAME_TYPE | ||
264 | | bic RB, PC, #FRAME_TYPEP | ||
265 | | cmp CARG1, #FRAME_C | ||
266 | | sub RB, BASE, RB // RB = previous base. | ||
267 | | bne ->vm_returnp | ||
268 | | | ||
269 | | str RB, L->base | ||
270 | | ldr KBASE, SAVE_NRES | ||
271 | | mv_vmstate CARG4, C | ||
272 | | sub BASE, BASE, #8 | ||
273 | | subs CARG3, RC, #8 | ||
274 | | lsl KBASE, KBASE, #3 // KBASE = (nresults_wanted+1)*8 | ||
275 | | st_vmstate CARG4 | ||
276 | | beq >2 | ||
277 | |1: | ||
278 | | subs CARG3, CARG3, #8 | ||
279 | | ldrd CARG12, [RA], #8 | ||
280 | | strd CARG12, [BASE], #8 | ||
281 | | bne <1 | ||
282 | |2: | ||
283 | | cmp KBASE, RC // More/less results wanted? | ||
284 | | bne >6 | ||
285 | |3: | ||
286 | | str BASE, L->top // Store new top. | ||
287 | | | ||
288 | |->vm_leave_cp: | ||
289 | | ldr RC, SAVE_CFRAME // Restore previous C frame. | ||
290 | | mov CRET1, #0 // Ok return status for vm_pcall. | ||
291 | | str RC, L->cframe | ||
292 | | | ||
293 | |->vm_leave_unw: | ||
294 | | restoreregs_ret | ||
295 | | | ||
296 | |6: | ||
297 | | blt >7 // Less results wanted? | ||
298 | | // More results wanted. Check stack size and fill up results with nil. | ||
299 | | ldr CARG3, L->maxstack | ||
300 | | mvn CARG2, #~LJ_TNIL | ||
301 | | cmp BASE, CARG3 | ||
302 | | bhs >8 | ||
303 | | str CARG2, [BASE, #4] | ||
304 | | add RC, RC, #8 | ||
305 | | add BASE, BASE, #8 | ||
306 | | b <2 | ||
307 | | | ||
308 | |7: // Less results wanted. | ||
309 | | sub CARG1, RC, KBASE | ||
310 | | cmp KBASE, #0 // LUA_MULTRET+1 case? | ||
311 | | subne BASE, BASE, CARG1 // Either keep top or shrink it. | ||
312 | | b <3 | ||
313 | | | ||
314 | |8: // Corner case: need to grow stack for filling up results. | ||
315 | | // This can happen if: | ||
316 | | // - A C function grows the stack (a lot). | ||
317 | | // - The GC shrinks the stack in between. | ||
318 | | // - A return back from a lua_call() with (high) nresults adjustment. | ||
319 | | str BASE, L->top // Save current top held in BASE (yes). | ||
320 | | mov CARG2, KBASE | ||
321 | | mov CARG1, L | ||
322 | | bl extern lj_state_growstack // (lua_State *L, int n) | ||
323 | | ldr BASE, L->top // Need the (realloced) L->top in BASE. | ||
324 | | b <2 | ||
325 | | | ||
326 | |->vm_unwind_c: // Unwind C stack, return from vm_pcall. | ||
327 | | // (void *cframe, int errcode) | ||
328 | | mov sp, CARG1 | ||
329 | | mov CRET1, CARG2 | ||
330 | |->vm_unwind_c_eh: // Landing pad for external unwinder. | ||
331 | | ldr L, SAVE_L | ||
332 | | mv_vmstate CARG4, C | ||
333 | | ldr GL:CARG3, L->glref | ||
334 | | str CARG4, GL:CARG3->vmstate | ||
335 | | b ->vm_leave_unw | ||
336 | | | ||
337 | |->vm_unwind_ff: // Unwind C stack, return from ff pcall. | ||
338 | | // (void *cframe) | ||
339 | | bic CARG1, CARG1, #~CFRAME_RAWMASK // Use two steps: bic sp is deprecated. | ||
340 | | mov sp, CARG1 | ||
341 | |->vm_unwind_ff_eh: // Landing pad for external unwinder. | ||
342 | | ldr L, SAVE_L | ||
343 | | mov MASKR8, #255 | ||
344 | | mov RC, #16 // 2 results: false + error message. | ||
345 | | lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8. | ||
346 | | ldr BASE, L->base | ||
347 | | ldr DISPATCH, L->glref // Setup pointer to dispatch table. | ||
348 | | mvn CARG1, #~LJ_TFALSE | ||
349 | | sub RA, BASE, #8 // Results start at BASE-8. | ||
350 | | ldr PC, [BASE, FRAME_PC] // Fetch PC of previous frame. | ||
351 | | add DISPATCH, DISPATCH, #GG_G2DISP | ||
352 | | mv_vmstate CARG2, INTERP | ||
353 | | str CARG1, [BASE, #-4] // Prepend false to error message. | ||
354 | | st_vmstate CARG2 | ||
355 | | b ->vm_returnc | ||
356 | | | ||
357 | |//----------------------------------------------------------------------- | ||
358 | |//-- Grow stack for calls ----------------------------------------------- | ||
359 | |//----------------------------------------------------------------------- | ||
360 | | | ||
361 | |->vm_growstack_c: // Grow stack for C function. | ||
362 | | // CARG1 = L | ||
363 | | mov CARG2, #LUA_MINSTACK | ||
364 | | b >2 | ||
365 | | | ||
366 | |->vm_growstack_l: // Grow stack for Lua function. | ||
367 | | // BASE = new base, RA = BASE+framesize*8, RC = nargs*8, PC = first PC | ||
368 | | add RC, BASE, RC | ||
369 | | sub RA, RA, BASE | ||
370 | | mov CARG1, L | ||
371 | | str BASE, L->base | ||
372 | | add PC, PC, #4 // Must point after first instruction. | ||
373 | | str RC, L->top | ||
374 | | lsr CARG3, RA, #3 | ||
375 | |2: | ||
376 | | // L->base = new base, L->top = top | ||
377 | | str PC, SAVE_PC | ||
378 | | bl extern lj_state_growstack // (lua_State *L, int n) | ||
379 | | ldr BASE, L->base | ||
380 | | ldr RC, L->top | ||
381 | | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] | ||
382 | | sub NARGS8:RC, RC, BASE | ||
383 | | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC | ||
384 | | ins_callt // Just retry the call. | ||
385 | | | ||
386 | |//----------------------------------------------------------------------- | ||
387 | |//-- Entry points into the assembler VM --------------------------------- | ||
388 | |//----------------------------------------------------------------------- | ||
389 | | | ||
390 | |->vm_resume: // Setup C frame and resume thread. | ||
391 | | // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0) | ||
392 | | saveregs | ||
393 | | mov L, CARG1 | ||
394 | | ldr DISPATCH, L:CARG1->glref // Setup pointer to dispatch table. | ||
395 | | mov BASE, CARG2 | ||
396 | | add DISPATCH, DISPATCH, #GG_G2DISP | ||
397 | | str L, SAVE_L | ||
398 | | mov PC, #FRAME_CP | ||
399 | | str CARG3, SAVE_NRES | ||
400 | | add CARG2, sp, #CFRAME_RESUME | ||
401 | | ldrb CARG1, L->status | ||
402 | | str CARG3, SAVE_ERRF | ||
403 | | str CARG2, L->cframe | ||
404 | | str CARG3, SAVE_CFRAME | ||
405 | | cmp CARG1, #0 | ||
406 | | str L, SAVE_PC // Any value outside of bytecode is ok. | ||
407 | | beq >3 | ||
408 | | | ||
409 | | // Resume after yield (like a return). | ||
410 | | mov RA, BASE | ||
411 | | ldr BASE, L->base | ||
412 | | ldr CARG1, L->top | ||
413 | | mov MASKR8, #255 | ||
414 | | strb CARG3, L->status | ||
415 | | sub RC, CARG1, BASE | ||
416 | | ldr PC, [BASE, FRAME_PC] | ||
417 | | lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8. | ||
418 | | mv_vmstate CARG2, INTERP | ||
419 | | add RC, RC, #8 | ||
420 | | ands CARG1, PC, #FRAME_TYPE | ||
421 | | st_vmstate CARG2 | ||
422 | | str RC, SAVE_MULTRES | ||
423 | | beq ->BC_RET_Z | ||
424 | | b ->vm_return | ||
425 | | | ||
426 | |->vm_pcall: // Setup protected C frame and enter VM. | ||
427 | | // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef) | ||
428 | | saveregs | ||
429 | | mov PC, #FRAME_CP | ||
430 | | str CARG4, SAVE_ERRF | ||
431 | | b >1 | ||
432 | | | ||
433 | |->vm_call: // Setup C frame and enter VM. | ||
434 | | // (lua_State *L, TValue *base, int nres1) | ||
435 | | saveregs | ||
436 | | mov PC, #FRAME_C | ||
437 | | | ||
438 | |1: // Entry point for vm_pcall above (PC = ftype). | ||
439 | | ldr RC, L:CARG1->cframe | ||
440 | | str CARG3, SAVE_NRES | ||
441 | | mov L, CARG1 | ||
442 | | str CARG1, SAVE_L | ||
443 | | mov BASE, CARG2 | ||
444 | | str sp, L->cframe // Add our C frame to cframe chain. | ||
445 | | ldr DISPATCH, L->glref // Setup pointer to dispatch table. | ||
446 | | str CARG1, SAVE_PC // Any value outside of bytecode is ok. | ||
447 | | str RC, SAVE_CFRAME | ||
448 | | add DISPATCH, DISPATCH, #GG_G2DISP | ||
449 | | | ||
450 | |3: // Entry point for vm_cpcall/vm_resume (BASE = base, PC = ftype). | ||
451 | | ldr RB, L->base // RB = old base (for vmeta_call). | ||
452 | | ldr CARG1, L->top | ||
453 | | mov MASKR8, #255 | ||
454 | | add PC, PC, BASE | ||
455 | | lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8. | ||
456 | | sub PC, PC, RB // PC = frame delta + frame type | ||
457 | | mv_vmstate CARG2, INTERP | ||
458 | | sub NARGS8:RC, CARG1, BASE | ||
459 | | st_vmstate CARG2 | ||
460 | | | ||
461 | |->vm_call_dispatch: | ||
462 | | // RB = old base, BASE = new base, RC = nargs*8, PC = caller PC | ||
463 | | ldrd CARG34, [BASE, FRAME_FUNC] | ||
464 | | checkfunc CARG4, ->vmeta_call | ||
465 | | | ||
466 | |->vm_call_dispatch_f: | ||
467 | | ins_call | ||
468 | | // BASE = new base, CARG3 = func, RC = nargs*8, PC = caller PC | ||
469 | | | ||
470 | |->vm_cpcall: // Setup protected C frame, call C. | ||
471 | | // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp) | ||
472 | | saveregs | ||
473 | | mov L, CARG1 | ||
474 | | ldr RA, L:CARG1->stack | ||
475 | | str CARG1, SAVE_L | ||
476 | | ldr RB, L->top | ||
477 | | str CARG1, SAVE_PC // Any value outside of bytecode is ok. | ||
478 | | ldr RC, L->cframe | ||
479 | | sub RA, RA, RB // Compute -savestack(L, L->top). | ||
480 | | str sp, L->cframe // Add our C frame to cframe chain. | ||
481 | | mov RB, #0 | ||
482 | | str RA, SAVE_NRES // Neg. delta means cframe w/o frame. | ||
483 | | str RB, SAVE_ERRF // No error function. | ||
484 | | str RC, SAVE_CFRAME | ||
485 | | blx CARG4 // (lua_State *L, lua_CFunction func, void *ud) | ||
486 | | ldr DISPATCH, L->glref // Setup pointer to dispatch table. | ||
487 | | movs BASE, CRET1 | ||
488 | | mov PC, #FRAME_CP | ||
489 | | add DISPATCH, DISPATCH, #GG_G2DISP | ||
490 | | bne <3 // Else continue with the call. | ||
491 | | b ->vm_leave_cp // No base? Just remove C frame. | ||
492 | | | ||
493 | |//----------------------------------------------------------------------- | ||
494 | |//-- Metamethod handling ------------------------------------------------ | ||
495 | |//----------------------------------------------------------------------- | ||
496 | | | ||
497 | |//-- Continuation dispatch ---------------------------------------------- | ||
498 | | | ||
499 | |->cont_dispatch: | ||
500 | | // BASE = meta base, RA = resultptr, RC = (nresults+1)*8 | ||
501 | | ldr LFUNC:CARG3, [RB, FRAME_FUNC] | ||
502 | | ldr CARG1, [BASE, #-16] // Get continuation. | ||
503 | | mov CARG4, BASE | ||
504 | | mov BASE, RB // Restore caller BASE. | ||
505 | #if LJ_HASFFI | ||
506 | | cmp CARG1, #1 | ||
507 | #endif | ||
508 | | ldr PC, [CARG4, #-12] // Restore PC from [cont|PC]. | ||
509 | | ldr CARG3, LFUNC:CARG3->field_pc | ||
510 | | mvn INS, #~LJ_TNIL | ||
511 | | add CARG2, RA, RC | ||
512 | | str INS, [CARG2, #-4] // Ensure one valid arg. | ||
513 | #if LJ_HASFFI | ||
514 | | bls >1 | ||
515 | #endif | ||
516 | | ldr KBASE, [CARG3, #PC2PROTO(k)] | ||
517 | | // BASE = base, RA = resultptr, CARG4 = meta base | ||
518 | | bx CARG1 | ||
519 | | | ||
520 | #if LJ_HASFFI | ||
521 | |1: | ||
522 | | beq ->cont_ffi_callback // cont = 1: return from FFI callback. | ||
523 | | // cont = 0: tailcall from C function. | ||
524 | | ldr CARG3, [BASE, FRAME_FUNC] | ||
525 | | sub CARG4, CARG4, #16 | ||
526 | | sub RC, CARG4, BASE | ||
527 | | b ->vm_call_tail | ||
528 | #endif | ||
529 | | | ||
530 | |->cont_cat: // RA = resultptr, CARG4 = meta base | ||
531 | | ldr INS, [PC, #-4] | ||
532 | | sub CARG2, CARG4, #16 | ||
533 | | ldrd CARG34, [RA] | ||
534 | | str BASE, L->base | ||
535 | | decode_RB8 RC, INS | ||
536 | | decode_RA8 RA, INS | ||
537 | | add CARG1, BASE, RC | ||
538 | | subs CARG1, CARG2, CARG1 | ||
539 | | strdne CARG34, [CARG2] | ||
540 | | movne CARG3, CARG1 | ||
541 | | bne ->BC_CAT_Z | ||
542 | | strd CARG34, [BASE, RA] | ||
543 | | b ->cont_nop | ||
544 | | | ||
545 | |//-- Table indexing metamethods ----------------------------------------- | ||
546 | | | ||
547 | |->vmeta_tgets1: | ||
548 | | add CARG2, BASE, RB | ||
549 | | b >2 | ||
550 | | | ||
551 | |->vmeta_tgets: | ||
552 | | sub CARG2, DISPATCH, #-DISPATCH_GL(tmptv) | ||
553 | | mvn CARG4, #~LJ_TTAB | ||
554 | | str TAB:RB, [CARG2] | ||
555 | | str CARG4, [CARG2, #4] | ||
556 | |2: | ||
557 | | mvn CARG4, #~LJ_TSTR | ||
558 | | str STR:RC, TMPDlo | ||
559 | | str CARG4, TMPDhi | ||
560 | | mov CARG3, TMPDp | ||
561 | | b >1 | ||
562 | | | ||
563 | |->vmeta_tgetb: // RC = index | ||
564 | | decode_RB8 RB, INS | ||
565 | | str RC, TMPDlo | ||
566 | | mvn CARG4, #~LJ_TISNUM | ||
567 | | add CARG2, BASE, RB | ||
568 | | str CARG4, TMPDhi | ||
569 | | mov CARG3, TMPDp | ||
570 | | b >1 | ||
571 | | | ||
572 | |->vmeta_tgetv: | ||
573 | | add CARG2, BASE, RB | ||
574 | | add CARG3, BASE, RC | ||
575 | |1: | ||
576 | | str BASE, L->base | ||
577 | | mov CARG1, L | ||
578 | | str PC, SAVE_PC | ||
579 | | bl extern lj_meta_tget // (lua_State *L, TValue *o, TValue *k) | ||
580 | | // Returns TValue * (finished) or NULL (metamethod). | ||
581 | | IOS ldr BASE, L->base | ||
582 | | cmp CRET1, #0 | ||
583 | | beq >3 | ||
584 | | ldrd CARG34, [CRET1] | ||
585 | | ins_next1 | ||
586 | | ins_next2 | ||
587 | | strd CARG34, [BASE, RA] | ||
588 | | ins_next3 | ||
589 | | | ||
590 | |3: // Call __index metamethod. | ||
591 | | // BASE = base, L->top = new base, stack = cont/func/t/k | ||
592 | | rsb CARG1, BASE, #FRAME_CONT | ||
593 | | ldr BASE, L->top | ||
594 | | mov NARGS8:RC, #16 // 2 args for func(t, k). | ||
595 | | str PC, [BASE, #-12] // [cont|PC] | ||
596 | | add PC, CARG1, BASE | ||
597 | | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Guaranteed to be a function here. | ||
598 | | b ->vm_call_dispatch_f | ||
599 | | | ||
600 | |//----------------------------------------------------------------------- | ||
601 | | | ||
602 | |->vmeta_tsets1: | ||
603 | | add CARG2, BASE, RB | ||
604 | | b >2 | ||
605 | | | ||
606 | |->vmeta_tsets: | ||
607 | | sub CARG2, DISPATCH, #-DISPATCH_GL(tmptv) | ||
608 | | mvn CARG4, #~LJ_TTAB | ||
609 | | str TAB:RB, [CARG2] | ||
610 | | str CARG4, [CARG2, #4] | ||
611 | |2: | ||
612 | | mvn CARG4, #~LJ_TSTR | ||
613 | | str STR:RC, TMPDlo | ||
614 | | str CARG4, TMPDhi | ||
615 | | mov CARG3, TMPDp | ||
616 | | b >1 | ||
617 | | | ||
618 | |->vmeta_tsetb: // RC = index | ||
619 | | decode_RB8 RB, INS | ||
620 | | str RC, TMPDlo | ||
621 | | mvn CARG4, #~LJ_TISNUM | ||
622 | | add CARG2, BASE, RB | ||
623 | | str CARG4, TMPDhi | ||
624 | | mov CARG3, TMPDp | ||
625 | | b >1 | ||
626 | | | ||
627 | |->vmeta_tsetv: | ||
628 | | add CARG2, BASE, RB | ||
629 | | add CARG3, BASE, RC | ||
630 | |1: | ||
631 | | str BASE, L->base | ||
632 | | mov CARG1, L | ||
633 | | str PC, SAVE_PC | ||
634 | | bl extern lj_meta_tset // (lua_State *L, TValue *o, TValue *k) | ||
635 | | // Returns TValue * (finished) or NULL (metamethod). | ||
636 | | IOS ldr BASE, L->base | ||
637 | | cmp CRET1, #0 | ||
638 | | ldrd CARG34, [BASE, RA] | ||
639 | | beq >3 | ||
640 | | ins_next1 | ||
641 | | // NOBARRIER: lj_meta_tset ensures the table is not black. | ||
642 | | strd CARG34, [CRET1] | ||
643 | | ins_next2 | ||
644 | | ins_next3 | ||
645 | | | ||
646 | |3: // Call __newindex metamethod. | ||
647 | | // BASE = base, L->top = new base, stack = cont/func/t/k/(v) | ||
648 | | rsb CARG1, BASE, #FRAME_CONT | ||
649 | | ldr BASE, L->top | ||
650 | | mov NARGS8:RC, #24 // 3 args for func(t, k, v). | ||
651 | | strd CARG34, [BASE, #16] // Copy value to third argument. | ||
652 | | str PC, [BASE, #-12] // [cont|PC] | ||
653 | | add PC, CARG1, BASE | ||
654 | | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Guaranteed to be a function here. | ||
655 | | b ->vm_call_dispatch_f | ||
656 | | | ||
657 | |//-- Comparison metamethods --------------------------------------------- | ||
658 | | | ||
659 | |->vmeta_comp: | ||
660 | | mov CARG1, L | ||
661 | | sub PC, PC, #4 | ||
662 | | mov CARG2, RA | ||
663 | | str BASE, L->base | ||
664 | | mov CARG3, RC | ||
665 | | str PC, SAVE_PC | ||
666 | | decode_OP CARG4, INS | ||
667 | | bl extern lj_meta_comp // (lua_State *L, TValue *o1, *o2, int op) | ||
668 | | // Returns 0/1 or TValue * (metamethod). | ||
669 | |3: | ||
670 | | IOS ldr BASE, L->base | ||
671 | | cmp CRET1, #1 | ||
672 | | bhi ->vmeta_binop | ||
673 | |4: | ||
674 | | ldrh RB, [PC, #2] | ||
675 | | add PC, PC, #4 | ||
676 | | add RB, PC, RB, lsl #2 | ||
677 | | subhs PC, RB, #0x20000 | ||
678 | |->cont_nop: | ||
679 | | ins_next | ||
680 | | | ||
681 | |->cont_ra: // RA = resultptr | ||
682 | | ldr INS, [PC, #-4] | ||
683 | | ldrd CARG12, [RA] | ||
684 | | decode_RA8 CARG3, INS | ||
685 | | strd CARG12, [BASE, CARG3] | ||
686 | | b ->cont_nop | ||
687 | | | ||
688 | |->cont_condt: // RA = resultptr | ||
689 | | ldr CARG2, [RA, #4] | ||
690 | | mvn CARG1, #~LJ_TTRUE | ||
691 | | cmp CARG1, CARG2 // Branch if result is true. | ||
692 | | b <4 | ||
693 | | | ||
694 | |->cont_condf: // RA = resultptr | ||
695 | | ldr CARG2, [RA, #4] | ||
696 | | checktp CARG2, LJ_TFALSE // Branch if result is false. | ||
697 | | b <4 | ||
698 | | | ||
699 | |->vmeta_equal: | ||
700 | | // CARG2, CARG3, CARG4 are already set by BC_ISEQV/BC_ISNEV. | ||
701 | | sub PC, PC, #4 | ||
702 | | str BASE, L->base | ||
703 | | mov CARG1, L | ||
704 | | str PC, SAVE_PC | ||
705 | | bl extern lj_meta_equal // (lua_State *L, GCobj *o1, *o2, int ne) | ||
706 | | // Returns 0/1 or TValue * (metamethod). | ||
707 | | b <3 | ||
708 | | | ||
709 | |->vmeta_equal_cd: | ||
710 | #if LJ_HASFFI | ||
711 | | sub PC, PC, #4 | ||
712 | | str BASE, L->base | ||
713 | | mov CARG1, L | ||
714 | | mov CARG2, INS | ||
715 | | str PC, SAVE_PC | ||
716 | | bl extern lj_meta_equal_cd // (lua_State *L, BCIns op) | ||
717 | | // Returns 0/1 or TValue * (metamethod). | ||
718 | | b <3 | ||
719 | #endif | ||
720 | | | ||
721 | |//-- Arithmetic metamethods --------------------------------------------- | ||
722 | | | ||
723 | |->vmeta_arith_vn: | ||
724 | | decode_RB8 RB, INS | ||
725 | | decode_RC8 RC, INS | ||
726 | | add CARG3, BASE, RB | ||
727 | | add CARG4, KBASE, RC | ||
728 | | b >1 | ||
729 | | | ||
730 | |->vmeta_arith_nv: | ||
731 | | decode_RB8 RB, INS | ||
732 | | decode_RC8 RC, INS | ||
733 | | add CARG4, BASE, RB | ||
734 | | add CARG3, KBASE, RC | ||
735 | | b >1 | ||
736 | | | ||
737 | |->vmeta_unm: | ||
738 | | ldr INS, [PC, #-8] | ||
739 | | sub PC, PC, #4 | ||
740 | | add CARG3, BASE, RC | ||
741 | | add CARG4, BASE, RC | ||
742 | | b >1 | ||
743 | | | ||
744 | |->vmeta_arith_vv: | ||
745 | | decode_RB8 RB, INS | ||
746 | | decode_RC8 RC, INS | ||
747 | | add CARG3, BASE, RB | ||
748 | | add CARG4, BASE, RC | ||
749 | |1: | ||
750 | | decode_OP OP, INS | ||
751 | | add CARG2, BASE, RA | ||
752 | | str BASE, L->base | ||
753 | | mov CARG1, L | ||
754 | | str PC, SAVE_PC | ||
755 | | str OP, ARG5 | ||
756 | | bl extern lj_meta_arith // (lua_State *L, TValue *ra,*rb,*rc, BCReg op) | ||
757 | | // Returns NULL (finished) or TValue * (metamethod). | ||
758 | | IOS ldr BASE, L->base | ||
759 | | cmp CRET1, #0 | ||
760 | | beq ->cont_nop | ||
761 | | | ||
762 | | // Call metamethod for binary op. | ||
763 | |->vmeta_binop: | ||
764 | | // BASE = old base, CRET1 = new base, stack = cont/func/o1/o2 | ||
765 | | sub CARG2, CRET1, BASE | ||
766 | | str PC, [CRET1, #-12] // [cont|PC] | ||
767 | | add PC, CARG2, #FRAME_CONT | ||
768 | | mov BASE, CRET1 | ||
769 | | mov NARGS8:RC, #16 // 2 args for func(o1, o2). | ||
770 | | b ->vm_call_dispatch | ||
771 | | | ||
772 | |->vmeta_len: | ||
773 | | add CARG2, BASE, RC | ||
774 | | str BASE, L->base | ||
775 | | mov CARG1, L | ||
776 | | str PC, SAVE_PC | ||
777 | | bl extern lj_meta_len // (lua_State *L, TValue *o) | ||
778 | | // Returns NULL (retry) or TValue * (metamethod base). | ||
779 | | IOS ldr BASE, L->base | ||
780 | #ifdef LUAJIT_ENABLE_LUA52COMPAT | ||
781 | | cmp CRET1, #0 | ||
782 | | bne ->vmeta_binop // Binop call for compatibility. | ||
783 | | ldr TAB:CARG1, [BASE, RC] | ||
784 | | b ->BC_LEN_Z | ||
785 | #else | ||
786 | | b ->vmeta_binop // Binop call for compatibility. | ||
787 | #endif | ||
788 | | | ||
789 | |//-- Call metamethod ---------------------------------------------------- | ||
790 | | | ||
791 | |->vmeta_call: // Resolve and call __call metamethod. | ||
792 | | // RB = old base, BASE = new base, RC = nargs*8 | ||
793 | | mov CARG1, L | ||
794 | | str RB, L->base // This is the callers base! | ||
795 | | sub CARG2, BASE, #8 | ||
796 | | str PC, SAVE_PC | ||
797 | | add CARG3, BASE, NARGS8:RC | ||
798 | | IOS mov RA, BASE | ||
799 | | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top) | ||
800 | | IOS mov BASE, RA | ||
801 | | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Guaranteed to be a function here. | ||
802 | | add NARGS8:RC, NARGS8:RC, #8 // Got one more argument now. | ||
803 | | ins_call | ||
804 | | | ||
805 | |->vmeta_callt: // Resolve __call for BC_CALLT. | ||
806 | | // BASE = old base, RA = new base, RC = nargs*8 | ||
807 | | mov CARG1, L | ||
808 | | str BASE, L->base | ||
809 | | sub CARG2, RA, #8 | ||
810 | | str PC, SAVE_PC | ||
811 | | add CARG3, RA, NARGS8:RC | ||
812 | | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top) | ||
813 | | IOS ldr BASE, L->base | ||
814 | | ldr LFUNC:CARG3, [RA, FRAME_FUNC] // Guaranteed to be a function here. | ||
815 | | ldr PC, [BASE, FRAME_PC] | ||
816 | | add NARGS8:RC, NARGS8:RC, #8 // Got one more argument now. | ||
817 | | b ->BC_CALLT2_Z | ||
818 | | | ||
819 | |//-- Argument coercion for 'for' statement ------------------------------ | ||
820 | | | ||
821 | |->vmeta_for: | ||
822 | | mov CARG1, L | ||
823 | | str BASE, L->base | ||
824 | | mov CARG2, RA | ||
825 | | str PC, SAVE_PC | ||
826 | | bl extern lj_meta_for // (lua_State *L, TValue *base) | ||
827 | | IOS ldr BASE, L->base | ||
828 | #if LJ_HASJIT | ||
829 | | ldrb OP, [PC, #-4] | ||
830 | #endif | ||
831 | | ldr INS, [PC, #-4] | ||
832 | #if LJ_HASJIT | ||
833 | | cmp OP, #BC_JFORI | ||
834 | #endif | ||
835 | | decode_RA8 RA, INS | ||
836 | | decode_RD RC, INS | ||
837 | #if LJ_HASJIT | ||
838 | | beq =>BC_JFORI | ||
839 | #endif | ||
840 | | b =>BC_FORI | ||
841 | | | ||
842 | |//----------------------------------------------------------------------- | ||
843 | |//-- Fast functions ----------------------------------------------------- | ||
844 | |//----------------------------------------------------------------------- | ||
845 | | | ||
846 | |.macro .ffunc, name | ||
847 | |->ff_ .. name: | ||
848 | |.endmacro | ||
849 | | | ||
850 | |.macro .ffunc_1, name | ||
851 | |->ff_ .. name: | ||
852 | | ldrd CARG12, [BASE] | ||
853 | | cmp NARGS8:RC, #8 | ||
854 | | blo ->fff_fallback | ||
855 | |.endmacro | ||
856 | | | ||
857 | |.macro .ffunc_2, name | ||
858 | |->ff_ .. name: | ||
859 | | ldrd CARG12, [BASE] | ||
860 | | ldrd CARG34, [BASE, #8] | ||
861 | | cmp NARGS8:RC, #16 | ||
862 | | blo ->fff_fallback | ||
863 | |.endmacro | ||
864 | | | ||
865 | |.macro .ffunc_n, name | ||
866 | | .ffunc_1 name | ||
867 | | checktp CARG2, LJ_TISNUM | ||
868 | | bhs ->fff_fallback | ||
869 | |.endmacro | ||
870 | | | ||
871 | |.macro .ffunc_nn, name | ||
872 | | .ffunc_2 name | ||
873 | | checktp CARG2, LJ_TISNUM | ||
874 | | cmnlo CARG4, #-LJ_TISNUM | ||
875 | | bhs ->fff_fallback | ||
876 | |.endmacro | ||
877 | | | ||
878 | |// Inlined GC threshold check. Caveat: uses CARG1 and CARG2. | ||
879 | |.macro ffgccheck | ||
880 | | ldr CARG1, [DISPATCH, #DISPATCH_GL(gc.total)] | ||
881 | | ldr CARG2, [DISPATCH, #DISPATCH_GL(gc.threshold)] | ||
882 | | cmp CARG1, CARG2 | ||
883 | | blge ->fff_gcstep | ||
884 | |.endmacro | ||
885 | | | ||
886 | |//-- Base library: checks ----------------------------------------------- | ||
887 | | | ||
888 | |.ffunc_1 assert | ||
889 | | checktp CARG2, LJ_TTRUE | ||
890 | | bhi ->fff_fallback | ||
891 | | ldr PC, [BASE, FRAME_PC] | ||
892 | | strd CARG12, [BASE, #-8] | ||
893 | | mov RB, BASE | ||
894 | | subs RA, NARGS8:RC, #8 | ||
895 | | add RC, NARGS8:RC, #8 // Compute (nresults+1)*8. | ||
896 | | beq ->fff_res // Done if exactly 1 argument. | ||
897 | |1: | ||
898 | | ldrd CARG12, [RB, #8] | ||
899 | | subs RA, RA, #8 | ||
900 | | strd CARG12, [RB], #8 | ||
901 | | bne <1 | ||
902 | | b ->fff_res | ||
903 | | | ||
904 | |.ffunc type | ||
905 | | ldr CARG2, [BASE, #4] | ||
906 | | cmp NARGS8:RC, #8 | ||
907 | | blo ->fff_fallback | ||
908 | | checktp CARG2, LJ_TISNUM | ||
909 | | mvnlo CARG2, #~LJ_TISNUM | ||
910 | | rsb CARG4, CARG2, #(int)(offsetof(GCfuncC, upvalue)>>3)-1 | ||
911 | | lsl CARG4, CARG4, #3 | ||
912 | | ldrd CARG12, [CFUNC:CARG3, CARG4] | ||
913 | | b ->fff_restv | ||
914 | | | ||
915 | |//-- Base library: getters and setters --------------------------------- | ||
916 | | | ||
917 | |.ffunc_1 getmetatable | ||
918 | | checktp CARG2, LJ_TTAB | ||
919 | | cmnne CARG2, #-LJ_TUDATA | ||
920 | | bne >6 | ||
921 | |1: // Field metatable must be at same offset for GCtab and GCudata! | ||
922 | | ldr TAB:RB, TAB:CARG1->metatable | ||
923 | |2: | ||
924 | | mvn CARG2, #~LJ_TNIL | ||
925 | | ldr STR:RC, [DISPATCH, #DISPATCH_GL(gcroot[GCROOT_MMNAME+MM_metatable])] | ||
926 | | cmp TAB:RB, #0 | ||
927 | | beq ->fff_restv | ||
928 | | ldr CARG3, TAB:RB->hmask | ||
929 | | ldr CARG4, STR:RC->hash | ||
930 | | ldr NODE:INS, TAB:RB->node | ||
931 | | and CARG3, CARG3, CARG4 // idx = str->hash & tab->hmask | ||
932 | | add CARG3, CARG3, CARG3, lsl #1 | ||
933 | | add NODE:INS, NODE:INS, CARG3, lsl #3 // node = tab->node + idx*3*8 | ||
934 | |3: // Rearranged logic, because we expect _not_ to find the key. | ||
935 | | ldrd CARG34, NODE:INS->key // STALL: early NODE:INS. | ||
936 | | ldrd CARG12, NODE:INS->val | ||
937 | | ldr NODE:INS, NODE:INS->next | ||
938 | | cmp CARG3, STR:RC | ||
939 | | checktpeq CARG4, LJ_TSTR | ||
940 | | beq >5 | ||
941 | | cmp NODE:INS, #0 | ||
942 | | bne <3 | ||
943 | |4: | ||
944 | | mov CARG1, RB // Use metatable as default result. | ||
945 | | mvn CARG2, #~LJ_TTAB | ||
946 | | b ->fff_restv | ||
947 | |5: | ||
948 | | checktp CARG2, LJ_TNIL | ||
949 | | bne ->fff_restv | ||
950 | | b <4 | ||
951 | | | ||
952 | |6: | ||
953 | | checktp CARG2, LJ_TISNUM | ||
954 | | mvnhs CARG2, CARG2 | ||
955 | | movlo CARG2, #~LJ_TISNUM | ||
956 | | add CARG4, DISPATCH, CARG2, lsl #2 | ||
957 | | ldr TAB:RB, [CARG4, #DISPATCH_GL(gcroot[GCROOT_BASEMT])] | ||
958 | | b <2 | ||
959 | | | ||
960 | |.ffunc_2 setmetatable | ||
961 | | // Fast path: no mt for table yet and not clearing the mt. | ||
962 | | checktp CARG2, LJ_TTAB | ||
963 | | ldreq TAB:RB, TAB:CARG1->metatable | ||
964 | | checktpeq CARG4, LJ_TTAB | ||
965 | | ldrbeq CARG4, TAB:CARG1->marked | ||
966 | | cmpeq TAB:RB, #0 | ||
967 | | bne ->fff_fallback | ||
968 | | tst CARG4, #LJ_GC_BLACK // isblack(table) | ||
969 | | str TAB:CARG3, TAB:CARG1->metatable | ||
970 | | beq ->fff_restv | ||
971 | | barrierback TAB:CARG1, CARG4, CARG3 | ||
972 | | b ->fff_restv | ||
973 | | | ||
974 | |.ffunc rawget | ||
975 | | ldrd CARG34, [BASE] | ||
976 | | cmp NARGS8:RC, #16 | ||
977 | | blo ->fff_fallback | ||
978 | | mov CARG2, CARG3 | ||
979 | | checktab CARG4, ->fff_fallback | ||
980 | | mov CARG1, L | ||
981 | | add CARG3, BASE, #8 | ||
982 | | IOS mov RA, BASE | ||
983 | | bl extern lj_tab_get // (lua_State *L, GCtab *t, cTValue *key) | ||
984 | | // Returns cTValue *. | ||
985 | | IOS mov BASE, RA | ||
986 | | ldrd CARG12, [CRET1] | ||
987 | | b ->fff_restv | ||
988 | | | ||
989 | |//-- Base library: conversions ------------------------------------------ | ||
990 | | | ||
991 | |.ffunc_1 tonumber | ||
992 | | // Only handles the number case inline (without a base argument). | ||
993 | | checktp CARG2, LJ_TISNUM | ||
994 | | bls ->fff_restv | ||
995 | | b ->fff_fallback | ||
996 | | | ||
997 | |.ffunc_1 tostring | ||
998 | | // Only handles the string or number case inline. | ||
999 | | checktp CARG2, LJ_TSTR | ||
1000 | | // A __tostring method in the string base metatable is ignored. | ||
1001 | | beq ->fff_restv | ||
1002 | | // Handle numbers inline, unless a number base metatable is present. | ||
1003 | | ldr CARG4, [DISPATCH, #DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM])] | ||
1004 | | str BASE, L->base | ||
1005 | | checktp CARG2, LJ_TISNUM | ||
1006 | | cmpls CARG4, #0 | ||
1007 | | str PC, SAVE_PC // Redundant (but a defined value). | ||
1008 | | bhi ->fff_fallback | ||
1009 | | ffgccheck | ||
1010 | | mov CARG1, L | ||
1011 | | mov CARG2, BASE | ||
1012 | | bl extern lj_str_fromnumber // (lua_State *L, cTValue *o) | ||
1013 | | // Returns GCstr *. | ||
1014 | | ldr BASE, L->base | ||
1015 | | mvn CARG2, #~LJ_TSTR | ||
1016 | | b ->fff_restv | ||
1017 | | | ||
1018 | |//-- Base library: iterators ------------------------------------------- | ||
1019 | | | ||
1020 | |.ffunc_1 next | ||
1021 | | mvn CARG4, #~LJ_TNIL | ||
1022 | | checktab CARG2, ->fff_fallback | ||
1023 | | strd CARG34, [BASE, NARGS8:RC] // Set missing 2nd arg to nil. | ||
1024 | | ldr PC, [BASE, FRAME_PC] | ||
1025 | | mov CARG2, CARG1 | ||
1026 | | str BASE, L->base // Add frame since C call can throw. | ||
1027 | | mov CARG1, L | ||
1028 | | str BASE, L->top // Dummy frame length is ok. | ||
1029 | | add CARG3, BASE, #8 | ||
1030 | | str PC, SAVE_PC | ||
1031 | | bl extern lj_tab_next // (lua_State *L, GCtab *t, TValue *key) | ||
1032 | | // Returns 0 at end of traversal. | ||
1033 | | IOS ldr BASE, L->base | ||
1034 | | cmp CRET1, #0 | ||
1035 | | mvneq CRET2, #~LJ_TNIL | ||
1036 | | beq ->fff_restv // End of traversal: return nil. | ||
1037 | | ldrd CARG12, [BASE, #8] // Copy key and value to results. | ||
1038 | | ldrd CARG34, [BASE, #16] | ||
1039 | | mov RC, #(2+1)*8 | ||
1040 | | strd CARG12, [BASE, #-8] | ||
1041 | | strd CARG34, [BASE] | ||
1042 | | b ->fff_res | ||
1043 | | | ||
1044 | |.ffunc_1 pairs | ||
1045 | | checktab CARG2, ->fff_fallback | ||
1046 | #ifdef LUAJIT_ENABLE_LUA52COMPAT | ||
1047 | | ldr TAB:RB, TAB:CARG1->metatable | ||
1048 | #endif | ||
1049 | | ldrd CFUNC:CARG34, CFUNC:CARG3->upvalue[0] | ||
1050 | | ldr PC, [BASE, FRAME_PC] | ||
1051 | #ifdef LUAJIT_ENABLE_LUA52COMPAT | ||
1052 | | cmp TAB:RB, #0 | ||
1053 | | bne ->fff_fallback | ||
1054 | #endif | ||
1055 | | mvn CARG2, #~LJ_TNIL | ||
1056 | | mov RC, #(3+1)*8 | ||
1057 | | strd CFUNC:CARG34, [BASE, #-8] | ||
1058 | | str CARG2, [BASE, #12] | ||
1059 | | b ->fff_res | ||
1060 | | | ||
1061 | |.ffunc_2 ipairs_aux | ||
1062 | | checktp CARG2, LJ_TTAB | ||
1063 | | checktpeq CARG4, LJ_TISNUM | ||
1064 | | bne ->fff_fallback | ||
1065 | | ldr RB, TAB:CARG1->asize | ||
1066 | | ldr RC, TAB:CARG1->array | ||
1067 | | add CARG3, CARG3, #1 | ||
1068 | | ldr PC, [BASE, FRAME_PC] | ||
1069 | | cmp CARG3, RB | ||
1070 | | add RC, RC, CARG3, lsl #3 | ||
1071 | | strd CARG34, [BASE, #-8] | ||
1072 | | ldrdlo CARG12, [RC] | ||
1073 | | mov RC, #(0+1)*8 | ||
1074 | | bhs >2 // Not in array part? | ||
1075 | |1: | ||
1076 | | checktp CARG2, LJ_TNIL | ||
1077 | | movne RC, #(2+1)*8 | ||
1078 | | strdne CARG12, [BASE] | ||
1079 | | b ->fff_res | ||
1080 | |2: // Check for empty hash part first. Otherwise call C function. | ||
1081 | | ldr RB, TAB:CARG1->hmask | ||
1082 | | mov CARG2, CARG3 | ||
1083 | | cmp RB, #0 | ||
1084 | | beq ->fff_res | ||
1085 | | IOS mov RA, BASE | ||
1086 | | bl extern lj_tab_getinth // (GCtab *t, int32_t key) | ||
1087 | | // Returns cTValue * or NULL. | ||
1088 | | IOS mov BASE, RA | ||
1089 | | cmp CRET1, #0 | ||
1090 | | beq ->fff_res | ||
1091 | | ldrd CARG12, [CRET1] | ||
1092 | | b <1 | ||
1093 | | | ||
1094 | |.ffunc_1 ipairs | ||
1095 | | checktab CARG2, ->fff_fallback | ||
1096 | #ifdef LUAJIT_ENABLE_LUA52COMPAT | ||
1097 | | ldr TAB:RB, TAB:CARG1->metatable | ||
1098 | #endif | ||
1099 | | ldrd CFUNC:CARG34, CFUNC:CARG3->upvalue[0] | ||
1100 | | ldr PC, [BASE, FRAME_PC] | ||
1101 | #ifdef LUAJIT_ENABLE_LUA52COMPAT | ||
1102 | | cmp TAB:RB, #0 | ||
1103 | | bne ->fff_fallback | ||
1104 | #endif | ||
1105 | | mov CARG1, #0 | ||
1106 | | mvn CARG2, #~LJ_TISNUM | ||
1107 | | mov RC, #(3+1)*8 | ||
1108 | | strd CFUNC:CARG34, [BASE, #-8] | ||
1109 | | strd CARG12, [BASE, #8] | ||
1110 | | b ->fff_res | ||
1111 | | | ||
1112 | |//-- Base library: catch errors ---------------------------------------- | ||
1113 | | | ||
1114 | |.ffunc pcall | ||
1115 | | ldrb RA, [DISPATCH, #DISPATCH_GL(hookmask)] | ||
1116 | | cmp NARGS8:RC, #8 | ||
1117 | | blo ->fff_fallback | ||
1118 | | tst RA, #HOOK_ACTIVE // Remember active hook before pcall. | ||
1119 | | mov RB, BASE | ||
1120 | | add BASE, BASE, #8 | ||
1121 | | moveq PC, #8+FRAME_PCALL | ||
1122 | | movne PC, #8+FRAME_PCALLH | ||
1123 | | sub NARGS8:RC, NARGS8:RC, #8 | ||
1124 | | b ->vm_call_dispatch | ||
1125 | | | ||
1126 | |.ffunc_2 xpcall | ||
1127 | | ldrb RA, [DISPATCH, #DISPATCH_GL(hookmask)] | ||
1128 | | checkfunc CARG4, ->fff_fallback // Traceback must be a function. | ||
1129 | | mov RB, BASE | ||
1130 | | strd CARG12, [BASE, #8] // Swap function and traceback. | ||
1131 | | strd CARG34, [BASE] | ||
1132 | | tst RA, #HOOK_ACTIVE // Remember active hook before pcall. | ||
1133 | | add BASE, BASE, #16 | ||
1134 | | moveq PC, #16+FRAME_PCALL | ||
1135 | | movne PC, #16+FRAME_PCALLH | ||
1136 | | sub NARGS8:RC, NARGS8:RC, #16 | ||
1137 | | b ->vm_call_dispatch | ||
1138 | | | ||
1139 | |//-- Coroutine library -------------------------------------------------- | ||
1140 | | | ||
1141 | |.macro coroutine_resume_wrap, resume | ||
1142 | |.if resume | ||
1143 | |.ffunc_1 coroutine_resume | ||
1144 | | checktp CARG2, LJ_TTHREAD | ||
1145 | | bne ->fff_fallback | ||
1146 | |.else | ||
1147 | |.ffunc coroutine_wrap_aux | ||
1148 | | ldr L:CARG1, CFUNC:CARG3->upvalue[0].gcr | ||
1149 | |.endif | ||
1150 | | ldr PC, [BASE, FRAME_PC] | ||
1151 | | str BASE, L->base | ||
1152 | | ldr CARG2, L:CARG1->top | ||
1153 | | ldrb RA, L:CARG1->status | ||
1154 | | ldr RB, L:CARG1->base | ||
1155 | | add CARG3, CARG2, NARGS8:RC | ||
1156 | | add CARG4, CARG2, RA | ||
1157 | | str PC, SAVE_PC | ||
1158 | | cmp CARG4, RB | ||
1159 | | beq ->fff_fallback | ||
1160 | | ldr CARG4, L:CARG1->maxstack | ||
1161 | | ldr RB, L:CARG1->cframe | ||
1162 | | cmp RA, #LUA_YIELD | ||
1163 | | cmpls CARG3, CARG4 | ||
1164 | | cmpls RB, #0 | ||
1165 | | bhi ->fff_fallback | ||
1166 | |1: | ||
1167 | |.if resume | ||
1168 | | sub CARG3, CARG3, #8 // Keep resumed thread in stack for GC. | ||
1169 | | add BASE, BASE, #8 | ||
1170 | | sub NARGS8:RC, NARGS8:RC, #8 | ||
1171 | |.endif | ||
1172 | | str CARG3, L:CARG1->top | ||
1173 | | str BASE, L->top | ||
1174 | |2: // Move args to coroutine. | ||
1175 | | ldrd CARG34, [BASE, RB] | ||
1176 | | cmp RB, NARGS8:RC | ||
1177 | | strdne CARG34, [CARG2, RB] | ||
1178 | | add RB, RB, #8 | ||
1179 | | bne <2 | ||
1180 | | | ||
1181 | | mov CARG3, #0 | ||
1182 | | mov L:RA, L:CARG1 | ||
1183 | | mov CARG4, #0 | ||
1184 | | bl ->vm_resume // (lua_State *L, TValue *base, 0, 0) | ||
1185 | | // Returns thread status. | ||
1186 | |4: | ||
1187 | | ldr CARG3, L:RA->base | ||
1188 | | mv_vmstate CARG2, INTERP | ||
1189 | | ldr CARG4, L:RA->top | ||
1190 | | st_vmstate CARG2 | ||
1191 | | cmp CRET1, #LUA_YIELD | ||
1192 | | ldr BASE, L->base | ||
1193 | | bhi >8 | ||
1194 | | subs RC, CARG4, CARG3 | ||
1195 | | ldr CARG1, L->maxstack | ||
1196 | | add CARG2, BASE, RC | ||
1197 | | beq >6 // No results? | ||
1198 | | cmp CARG2, CARG1 | ||
1199 | | mov RB, #0 | ||
1200 | | bhi >9 // Need to grow stack? | ||
1201 | | | ||
1202 | | sub CARG4, RC, #8 | ||
1203 | | str CARG3, L:RA->top // Clear coroutine stack. | ||
1204 | |5: // Move results from coroutine. | ||
1205 | | ldrd CARG12, [CARG3, RB] | ||
1206 | | cmp RB, CARG4 | ||
1207 | | strd CARG12, [BASE, RB] | ||
1208 | | add RB, RB, #8 | ||
1209 | | bne <5 | ||
1210 | |6: | ||
1211 | |.if resume | ||
1212 | | mvn CARG3, #~LJ_TTRUE | ||
1213 | | add RC, RC, #16 | ||
1214 | |7: | ||
1215 | | str CARG3, [BASE, #-4] // Prepend true/false to results. | ||
1216 | | sub RA, BASE, #8 | ||
1217 | |.else | ||
1218 | | mov RA, BASE | ||
1219 | | add RC, RC, #8 | ||
1220 | |.endif | ||
1221 | | ands CARG1, PC, #FRAME_TYPE | ||
1222 | | str PC, SAVE_PC | ||
1223 | | str RC, SAVE_MULTRES | ||
1224 | | beq ->BC_RET_Z | ||
1225 | | b ->vm_return | ||
1226 | | | ||
1227 | |8: // Coroutine returned with error (at co->top-1). | ||
1228 | |.if resume | ||
1229 | | ldrd CARG12, [CARG4, #-8]! | ||
1230 | | mvn CARG3, #~LJ_TFALSE | ||
1231 | | mov RC, #(2+1)*8 | ||
1232 | | str CARG4, L:RA->top // Remove error from coroutine stack. | ||
1233 | | strd CARG12, [BASE] // Copy error message. | ||
1234 | | b <7 | ||
1235 | |.else | ||
1236 | | mov CARG1, L | ||
1237 | | mov CARG2, L:RA | ||
1238 | | bl extern lj_ffh_coroutine_wrap_err // (lua_State *L, lua_State *co) | ||
1239 | | // Never returns. | ||
1240 | |.endif | ||
1241 | | | ||
1242 | |9: // Handle stack expansion on return from yield. | ||
1243 | | mov CARG1, L | ||
1244 | | lsr CARG2, RC, #3 | ||
1245 | | bl extern lj_state_growstack // (lua_State *L, int n) | ||
1246 | | mov CRET1, #0 | ||
1247 | | b <4 | ||
1248 | |.endmacro | ||
1249 | | | ||
1250 | | coroutine_resume_wrap 1 // coroutine.resume | ||
1251 | | coroutine_resume_wrap 0 // coroutine.wrap | ||
1252 | | | ||
1253 | |.ffunc coroutine_yield | ||
1254 | | ldr CARG1, L->cframe | ||
1255 | | add CARG2, BASE, NARGS8:RC | ||
1256 | | str BASE, L->base | ||
1257 | | tst CARG1, #CFRAME_RESUME | ||
1258 | | str CARG2, L->top | ||
1259 | | mov CRET1, #LUA_YIELD | ||
1260 | | mov CARG3, #0 | ||
1261 | | beq ->fff_fallback | ||
1262 | | str CARG3, L->cframe | ||
1263 | | strb CRET1, L->status | ||
1264 | | b ->vm_leave_unw | ||
1265 | | | ||
1266 | |//-- Math library ------------------------------------------------------- | ||
1267 | | | ||
1268 | |.macro math_round, func | ||
1269 | | .ffunc_1 math_ .. func | ||
1270 | | checktp CARG2, LJ_TISNUM | ||
1271 | | beq ->fff_restv | ||
1272 | | bhi ->fff_fallback | ||
1273 | | // Round FP value and normalize result. | ||
1274 | | lsl CARG3, CARG2, #1 | ||
1275 | | adds RB, CARG3, #0x00200000 | ||
1276 | | bpl >2 // |x| < 1? | ||
1277 | | mvn CARG4, #0x3e0 | ||
1278 | | subs RB, CARG4, RB, asr #21 | ||
1279 | | lsl CARG4, CARG2, #11 | ||
1280 | | lsl CARG3, CARG1, #11 | ||
1281 | | orr CARG4, CARG4, #0x80000000 | ||
1282 | | rsb INS, RB, #32 | ||
1283 | | orr CARG4, CARG4, CARG1, lsr #21 | ||
1284 | | bls >3 // |x| >= 2^31? | ||
1285 | | orr CARG3, CARG3, CARG4, lsl INS | ||
1286 | | lsr CARG1, CARG4, RB | ||
1287 | |.if "func" == "floor" | ||
1288 | | tst CARG3, CARG2, asr #31 | ||
1289 | | addne CARG1, CARG1, #1 | ||
1290 | |.else | ||
1291 | | bics CARG3, CARG3, CARG2, asr #31 | ||
1292 | | addsne CARG1, CARG1, #1 | ||
1293 | | ldrdvs CARG12, >9 | ||
1294 | | bvs ->fff_restv | ||
1295 | |.endif | ||
1296 | | cmp CARG2, #0 | ||
1297 | | rsblt CARG1, CARG1, #0 | ||
1298 | |1: | ||
1299 | | mvn CARG2, #~LJ_TISNUM | ||
1300 | | b ->fff_restv | ||
1301 | | | ||
1302 | |2: // |x| < 1 | ||
1303 | | orr CARG3, CARG3, CARG1 // ztest = abs(hi) | lo | ||
1304 | |.if "func" == "floor" | ||
1305 | | tst CARG3, CARG2, asr #31 // return (ztest & sign) == 0 ? 0 : -1 | ||
1306 | | moveq CARG1, #0 | ||
1307 | | mvnne CARG1, #0 | ||
1308 | |.else | ||
1309 | | bics CARG3, CARG3, CARG2, asr #31 // return (ztest & ~sign) == 0 ? 0 : 1 | ||
1310 | | moveq CARG1, #0 | ||
1311 | | movne CARG1, #1 | ||
1312 | |.endif | ||
1313 | | mvn CARG2, #~LJ_TISNUM | ||
1314 | | b ->fff_restv | ||
1315 | | | ||
1316 | |3: // |x| >= 2^31. Check for x == -(2^31). | ||
1317 | | cmpeq CARG4, #0x80000000 | ||
1318 | |.if "func" == "floor" | ||
1319 | | cmpeq CARG3, #0 | ||
1320 | |.endif | ||
1321 | | bne >4 | ||
1322 | | cmp CARG2, #0 | ||
1323 | | movmi CARG1, #0x80000000 | ||
1324 | | bmi <1 | ||
1325 | |4: | ||
1326 | | bl ->vm_..func | ||
1327 | | b ->fff_restv | ||
1328 | |.endmacro | ||
1329 | | | ||
1330 | | math_round floor | ||
1331 | | math_round ceil | ||
1332 | | | ||
1333 | |.align 8 | ||
1334 | |9: | ||
1335 | | .long 0x00000000, 0x41e00000 // 2^31. | ||
1336 | | | ||
1337 | |.ffunc_1 math_abs | ||
1338 | | checktp CARG2, LJ_TISNUM | ||
1339 | | bhi ->fff_fallback | ||
1340 | | bicne CARG2, CARG2, #0x80000000 | ||
1341 | | bne ->fff_restv | ||
1342 | | cmp CARG1, #0 | ||
1343 | | rsbslt CARG1, CARG1, #0 | ||
1344 | | ldrdvs CARG12, <9 | ||
1345 | | // Fallthrough. | ||
1346 | | | ||
1347 | |->fff_restv: | ||
1348 | | // CARG12 = TValue result. | ||
1349 | | ldr PC, [BASE, FRAME_PC] | ||
1350 | | strd CARG12, [BASE, #-8] | ||
1351 | |->fff_res1: | ||
1352 | | // PC = return. | ||
1353 | | mov RC, #(1+1)*8 | ||
1354 | |->fff_res: | ||
1355 | | // RC = (nresults+1)*8, PC = return. | ||
1356 | | ands CARG1, PC, #FRAME_TYPE | ||
1357 | | ldreq INS, [PC, #-4] | ||
1358 | | str RC, SAVE_MULTRES | ||
1359 | | sub RA, BASE, #8 | ||
1360 | | bne ->vm_return | ||
1361 | | decode_RB8 RB, INS | ||
1362 | |5: | ||
1363 | | cmp RB, RC // More results expected? | ||
1364 | | bhi >6 | ||
1365 | | decode_RA8 CARG1, INS | ||
1366 | | ins_next1 | ||
1367 | | ins_next2 | ||
1368 | | // Adjust BASE. KBASE is assumed to be set for the calling frame. | ||
1369 | | sub BASE, RA, CARG1 | ||
1370 | | ins_next3 | ||
1371 | | | ||
1372 | |6: // Fill up results with nil. | ||
1373 | | add CARG2, RA, RC | ||
1374 | | mvn CARG1, #~LJ_TNIL | ||
1375 | | add RC, RC, #8 | ||
1376 | | str CARG1, [CARG2, #-4] | ||
1377 | | b <5 | ||
1378 | | | ||
1379 | |.macro math_extern, func | ||
1380 | | .ffunc_n math_ .. func | ||
1381 | | IOS mov RA, BASE | ||
1382 | | bl extern func | ||
1383 | | IOS mov BASE, RA | ||
1384 | | b ->fff_restv | ||
1385 | |.endmacro | ||
1386 | | | ||
1387 | |.macro math_extern2, func | ||
1388 | | .ffunc_nn math_ .. func | ||
1389 | | IOS mov RA, BASE | ||
1390 | | bl extern func | ||
1391 | | IOS mov BASE, RA | ||
1392 | | b ->fff_restv | ||
1393 | |.endmacro | ||
1394 | | | ||
1395 | | math_extern sqrt | ||
1396 | | math_extern log | ||
1397 | | math_extern log10 | ||
1398 | | math_extern exp | ||
1399 | | math_extern sin | ||
1400 | | math_extern cos | ||
1401 | | math_extern tan | ||
1402 | | math_extern asin | ||
1403 | | math_extern acos | ||
1404 | | math_extern atan | ||
1405 | | math_extern sinh | ||
1406 | | math_extern cosh | ||
1407 | | math_extern tanh | ||
1408 | | math_extern2 pow | ||
1409 | | math_extern2 atan2 | ||
1410 | | math_extern2 fmod | ||
1411 | | | ||
1412 | |->ff_math_deg: | ||
1413 | |.ffunc_n math_rad | ||
1414 | | ldrd CARG34, CFUNC:CARG3->upvalue[0] | ||
1415 | | bl extern __aeabi_dmul | ||
1416 | | b ->fff_restv | ||
1417 | | | ||
1418 | |.ffunc_2 math_ldexp | ||
1419 | | checktp CARG2, LJ_TISNUM | ||
1420 | | bhs ->fff_fallback | ||
1421 | | checktp CARG4, LJ_TISNUM | ||
1422 | | bne ->fff_fallback | ||
1423 | | IOS mov RA, BASE | ||
1424 | | bl extern ldexp // (double x, int exp) | ||
1425 | | IOS mov BASE, RA | ||
1426 | | b ->fff_restv | ||
1427 | | | ||
1428 | |.ffunc_n math_frexp | ||
1429 | | mov CARG3, sp | ||
1430 | | IOS mov RA, BASE | ||
1431 | | bl extern frexp | ||
1432 | | IOS mov BASE, RA | ||
1433 | | ldr CARG3, [sp] | ||
1434 | | mvn CARG4, #~LJ_TISNUM | ||
1435 | | ldr PC, [BASE, FRAME_PC] | ||
1436 | | strd CARG12, [BASE, #-8] | ||
1437 | | mov RC, #(2+1)*8 | ||
1438 | | strd CARG34, [BASE] | ||
1439 | | b ->fff_res | ||
1440 | | | ||
1441 | |.ffunc_n math_modf | ||
1442 | | sub CARG3, BASE, #8 | ||
1443 | | ldr PC, [BASE, FRAME_PC] | ||
1444 | | IOS mov RA, BASE | ||
1445 | | bl extern modf | ||
1446 | | IOS mov BASE, RA | ||
1447 | | mov RC, #(2+1)*8 | ||
1448 | | strd CARG12, [BASE] | ||
1449 | | b ->fff_res | ||
1450 | | | ||
1451 | |.macro math_minmax, name, cond, fcond | ||
1452 | | .ffunc_1 name | ||
1453 | | checktp CARG2, LJ_TISNUM | ||
1454 | | mov RA, #8 | ||
1455 | | bne >4 | ||
1456 | |1: // Handle integers. | ||
1457 | | ldrd CARG34, [BASE, RA] | ||
1458 | | cmp RA, RC | ||
1459 | | bhs ->fff_restv | ||
1460 | | checktp CARG4, LJ_TISNUM | ||
1461 | | bne >3 | ||
1462 | | cmp CARG1, CARG3 | ||
1463 | | add RA, RA, #8 | ||
1464 | | mov..cond CARG1, CARG3 | ||
1465 | | b <1 | ||
1466 | |3: | ||
1467 | | bhi ->fff_fallback | ||
1468 | | // Convert intermediate result to number and continue below. | ||
1469 | | bl extern __aeabi_i2d | ||
1470 | | ldrd CARG34, [BASE, RA] | ||
1471 | | b >6 | ||
1472 | | | ||
1473 | |4: | ||
1474 | | bhi ->fff_fallback | ||
1475 | |5: // Handle numbers. | ||
1476 | | ldrd CARG34, [BASE, RA] | ||
1477 | | cmp RA, RC | ||
1478 | | bhs ->fff_restv | ||
1479 | | checktp CARG4, LJ_TISNUM | ||
1480 | | bhs >7 | ||
1481 | |6: | ||
1482 | | bl extern __aeabi_cdcmple | ||
1483 | | add RA, RA, #8 | ||
1484 | | mov..fcond CARG1, CARG3 | ||
1485 | | mov..fcond CARG2, CARG4 | ||
1486 | | b <5 | ||
1487 | |7: // Convert integer to number and continue above. | ||
1488 | | bhi ->fff_fallback | ||
1489 | | strd CARG12, TMPD | ||
1490 | | mov CARG1, CARG3 | ||
1491 | | bl extern __aeabi_i2d | ||
1492 | | ldrd CARG34, TMPD | ||
1493 | | b <6 | ||
1494 | |.endmacro | ||
1495 | | | ||
1496 | | math_minmax math_min, gt, hi | ||
1497 | | math_minmax math_max, lt, lo | ||
1498 | | | ||
1499 | |//-- String library ----------------------------------------------------- | ||
1500 | | | ||
1501 | |.ffunc_1 string_len | ||
1502 | | checkstr CARG2, ->fff_fallback | ||
1503 | | ldr CARG1, STR:CARG1->len | ||
1504 | | mvn CARG2, #~LJ_TISNUM | ||
1505 | | b ->fff_restv | ||
1506 | | | ||
1507 | |.ffunc string_byte // Only handle the 1-arg case here. | ||
1508 | | ldrd CARG12, [BASE] | ||
1509 | | ldr PC, [BASE, FRAME_PC] | ||
1510 | | cmp NARGS8:RC, #8 | ||
1511 | | checktpeq CARG2, LJ_TSTR // Need exactly 1 argument. | ||
1512 | | bne ->fff_fallback | ||
1513 | | ldr CARG3, STR:CARG1->len | ||
1514 | | ldrb CARG1, STR:CARG1[1] // Access is always ok (NUL at end). | ||
1515 | | mvn CARG2, #~LJ_TISNUM | ||
1516 | | cmp CARG3, #0 | ||
1517 | | moveq RC, #(0+1)*8 | ||
1518 | | movne RC, #(1+1)*8 | ||
1519 | | strd CARG12, [BASE, #-8] | ||
1520 | | b ->fff_res | ||
1521 | | | ||
1522 | |.ffunc string_char // Only handle the 1-arg case here. | ||
1523 | | ffgccheck | ||
1524 | | ldrd CARG12, [BASE] | ||
1525 | | ldr PC, [BASE, FRAME_PC] | ||
1526 | | cmp NARGS8:RC, #8 // Need exactly 1 argument. | ||
1527 | | checktpeq CARG2, LJ_TISNUM | ||
1528 | | bicseq CARG4, CARG1, #255 | ||
1529 | | mov CARG3, #1 | ||
1530 | | bne ->fff_fallback | ||
1531 | | str CARG1, TMPD | ||
1532 | | mov CARG2, TMPDp // Points to stack. Little-endian. | ||
1533 | |->fff_newstr: | ||
1534 | | // CARG2 = str, CARG3 = len. | ||
1535 | | str BASE, L->base | ||
1536 | | mov CARG1, L | ||
1537 | | str PC, SAVE_PC | ||
1538 | | bl extern lj_str_new // (lua_State *L, char *str, size_t l) | ||
1539 | | // Returns GCstr *. | ||
1540 | | ldr BASE, L->base | ||
1541 | | mvn CARG2, #~LJ_TSTR | ||
1542 | | b ->fff_restv | ||
1543 | | | ||
1544 | |.ffunc string_sub | ||
1545 | | ffgccheck | ||
1546 | | ldrd CARG12, [BASE] | ||
1547 | | ldrd CARG34, [BASE, #16] | ||
1548 | | cmp NARGS8:RC, #16 | ||
1549 | | mvn RB, #0 | ||
1550 | | beq >1 | ||
1551 | | blo ->fff_fallback | ||
1552 | | checktp CARG4, LJ_TISNUM | ||
1553 | | mov RB, CARG3 | ||
1554 | | bne ->fff_fallback | ||
1555 | |1: | ||
1556 | | ldrd CARG34, [BASE, #8] | ||
1557 | | checktp CARG2, LJ_TSTR | ||
1558 | | ldreq CARG2, STR:CARG1->len | ||
1559 | | checktpeq CARG4, LJ_TISNUM | ||
1560 | | bne ->fff_fallback | ||
1561 | | // CARG1 = str, CARG2 = str->len, CARG3 = start, RB = end | ||
1562 | | add CARG4, CARG2, #1 | ||
1563 | | cmp CARG3, #0 // if (start < 0) start += len+1 | ||
1564 | | addlt CARG3, CARG3, CARG4 | ||
1565 | | cmp CARG3, #1 // if (start < 1) start = 1 | ||
1566 | | movlt CARG3, #1 | ||
1567 | | cmp RB, #0 // if (end < 0) end += len+1 | ||
1568 | | addlt RB, RB, CARG4 | ||
1569 | | bic RB, RB, RB, asr #31 // if (end < 0) end = 0 | ||
1570 | | cmp RB, CARG2 // if (end > len) end = len | ||
1571 | | add CARG1, STR:CARG1, #sizeof(GCstr)-1 | ||
1572 | | movgt RB, CARG2 | ||
1573 | | add CARG2, CARG1, CARG3 | ||
1574 | | subs CARG3, RB, CARG3 // len = start - end | ||
1575 | | add CARG3, CARG3, #1 // len += 1 | ||
1576 | | bge ->fff_newstr | ||
1577 | |->fff_emptystr: | ||
1578 | | sub STR:CARG1, DISPATCH, #-DISPATCH_GL(strempty) | ||
1579 | | mvn CARG2, #~LJ_TSTR | ||
1580 | | b ->fff_restv | ||
1581 | | | ||
1582 | |.ffunc string_rep // Only handle the 1-char case inline. | ||
1583 | | ffgccheck | ||
1584 | | ldrd CARG12, [BASE] | ||
1585 | | ldrd CARG34, [BASE, #8] | ||
1586 | | cmp NARGS8:RC, #16 | ||
1587 | | blo ->fff_fallback | ||
1588 | | checktp CARG2, LJ_TSTR | ||
1589 | | checktpeq CARG4, LJ_TISNUM | ||
1590 | | bne ->fff_fallback | ||
1591 | | subs CARG4, CARG3, #1 | ||
1592 | | ldr CARG2, STR:CARG1->len | ||
1593 | | blt ->fff_emptystr // Count <= 0? | ||
1594 | | cmp CARG2, #1 | ||
1595 | | blo ->fff_emptystr // Zero-length string? | ||
1596 | | bne ->fff_fallback // Fallback for > 1-char strings. | ||
1597 | | ldr RB, [DISPATCH, #DISPATCH_GL(tmpbuf.sz)] | ||
1598 | | ldr CARG2, [DISPATCH, #DISPATCH_GL(tmpbuf.buf)] | ||
1599 | | ldr CARG1, STR:CARG1[1] | ||
1600 | | cmp RB, CARG3 | ||
1601 | | blo ->fff_fallback | ||
1602 | |1: // Fill buffer with char. | ||
1603 | | strb CARG1, [CARG2, CARG4] | ||
1604 | | subs CARG4, CARG4, #1 | ||
1605 | | bge <1 | ||
1606 | | b ->fff_newstr | ||
1607 | | | ||
1608 | |.ffunc string_reverse | ||
1609 | | ffgccheck | ||
1610 | | ldrd CARG12, [BASE] | ||
1611 | | cmp NARGS8:RC, #8 | ||
1612 | | blo ->fff_fallback | ||
1613 | | checkstr CARG2, ->fff_fallback | ||
1614 | | ldr CARG3, STR:CARG1->len | ||
1615 | | ldr RB, [DISPATCH, #DISPATCH_GL(tmpbuf.sz)] | ||
1616 | | ldr CARG2, [DISPATCH, #DISPATCH_GL(tmpbuf.buf)] | ||
1617 | | mov CARG4, CARG3 | ||
1618 | | add CARG1, STR:CARG1, #sizeof(GCstr) | ||
1619 | | cmp RB, CARG3 | ||
1620 | | blo ->fff_fallback | ||
1621 | |1: // Reverse string copy. | ||
1622 | | ldrb RB, [CARG1], #1 | ||
1623 | | subs CARG4, CARG4, #1 | ||
1624 | | blt ->fff_newstr | ||
1625 | | strb RB, [CARG2, CARG4] | ||
1626 | | b <1 | ||
1627 | | | ||
1628 | |.macro ffstring_case, name, lo | ||
1629 | | .ffunc name | ||
1630 | | ffgccheck | ||
1631 | | ldrd CARG12, [BASE] | ||
1632 | | cmp NARGS8:RC, #8 | ||
1633 | | blo ->fff_fallback | ||
1634 | | checkstr CARG2, ->fff_fallback | ||
1635 | | ldr CARG3, STR:CARG1->len | ||
1636 | | ldr RB, [DISPATCH, #DISPATCH_GL(tmpbuf.sz)] | ||
1637 | | ldr CARG2, [DISPATCH, #DISPATCH_GL(tmpbuf.buf)] | ||
1638 | | mov CARG4, #0 | ||
1639 | | add CARG1, STR:CARG1, #sizeof(GCstr) | ||
1640 | | cmp RB, CARG3 | ||
1641 | | blo ->fff_fallback | ||
1642 | |1: // ASCII case conversion. | ||
1643 | | ldrb RB, [CARG1, CARG4] | ||
1644 | | cmp CARG4, CARG3 | ||
1645 | | bhs ->fff_newstr | ||
1646 | | sub RC, RB, #lo | ||
1647 | | cmp RC, #26 | ||
1648 | | eorlo RB, RB, #0x20 | ||
1649 | | strb RB, [CARG2, CARG4] | ||
1650 | | add CARG4, CARG4, #1 | ||
1651 | | b <1 | ||
1652 | |.endmacro | ||
1653 | | | ||
1654 | |ffstring_case string_lower, 65 | ||
1655 | |ffstring_case string_upper, 97 | ||
1656 | | | ||
1657 | |//-- Table library ------------------------------------------------------ | ||
1658 | | | ||
1659 | |.ffunc_1 table_getn | ||
1660 | | checktab CARG2, ->fff_fallback | ||
1661 | | IOS mov RA, BASE | ||
1662 | | bl extern lj_tab_len // (GCtab *t) | ||
1663 | | // Returns uint32_t (but less than 2^31). | ||
1664 | | IOS mov BASE, RA | ||
1665 | | mvn CARG2, #~LJ_TISNUM | ||
1666 | | b ->fff_restv | ||
1667 | | | ||
1668 | |//-- Bit library -------------------------------------------------------- | ||
1669 | | | ||
1670 | |// FP number to bit conversion for soft-float. Clobbers r0-r3. | ||
1671 | |->vm_tobit_fb: | ||
1672 | | bhi ->fff_fallback | ||
1673 | |->vm_tobit: | ||
1674 | | lsl RB, CARG2, #1 | ||
1675 | | adds RB, RB, #0x00200000 | ||
1676 | | movpl CARG1, #0 // |x| < 1? | ||
1677 | | bxpl lr | ||
1678 | | mvn CARG4, #0x3e0 | ||
1679 | | subs RB, CARG4, RB, asr #21 | ||
1680 | | bmi >1 // |x| >= 2^32? | ||
1681 | | lsl CARG4, CARG2, #11 | ||
1682 | | orr CARG4, CARG4, #0x80000000 | ||
1683 | | orr CARG4, CARG4, CARG1, lsr #21 | ||
1684 | | cmp CARG2, #0 | ||
1685 | | lsr CARG1, CARG4, RB | ||
1686 | | rsblt CARG1, CARG1, #0 | ||
1687 | | bx lr | ||
1688 | |1: | ||
1689 | | add RB, RB, #21 | ||
1690 | | lsr CARG4, CARG1, RB | ||
1691 | | rsb RB, RB, #20 | ||
1692 | | lsl CARG1, CARG2, #12 | ||
1693 | | cmp CARG2, #0 | ||
1694 | | orr CARG1, CARG4, CARG1, lsl RB | ||
1695 | | rsblt CARG1, CARG1, #0 | ||
1696 | | bx lr | ||
1697 | | | ||
1698 | |.macro .ffunc_bit, name | ||
1699 | | .ffunc_1 bit_..name | ||
1700 | | checktp CARG2, LJ_TISNUM | ||
1701 | | blne ->vm_tobit_fb | ||
1702 | |.endmacro | ||
1703 | | | ||
1704 | |.ffunc_bit tobit | ||
1705 | | mvn CARG2, #~LJ_TISNUM | ||
1706 | | b ->fff_restv | ||
1707 | | | ||
1708 | |.macro .ffunc_bit_op, name, ins | ||
1709 | | .ffunc_bit name | ||
1710 | | mov CARG3, CARG1 | ||
1711 | | mov RA, #8 | ||
1712 | |1: | ||
1713 | | ldrd CARG12, [BASE, RA] | ||
1714 | | cmp RA, NARGS8:RC | ||
1715 | | add RA, RA, #8 | ||
1716 | | bge >2 | ||
1717 | | checktp CARG2, LJ_TISNUM | ||
1718 | | blne ->vm_tobit_fb | ||
1719 | | ins CARG3, CARG3, CARG1 | ||
1720 | | b <1 | ||
1721 | |.endmacro | ||
1722 | | | ||
1723 | |.ffunc_bit_op band, and | ||
1724 | |.ffunc_bit_op bor, orr | ||
1725 | |.ffunc_bit_op bxor, eor | ||
1726 | | | ||
1727 | |2: | ||
1728 | | mvn CARG4, #~LJ_TISNUM | ||
1729 | | ldr PC, [BASE, FRAME_PC] | ||
1730 | | strd CARG34, [BASE, #-8] | ||
1731 | | b ->fff_res1 | ||
1732 | | | ||
1733 | |.ffunc_bit bswap | ||
1734 | | eor CARG3, CARG1, CARG1, ror #16 | ||
1735 | | bic CARG3, CARG3, #0x00ff0000 | ||
1736 | | ror CARG1, CARG1, #8 | ||
1737 | | mvn CARG2, #~LJ_TISNUM | ||
1738 | | eor CARG1, CARG1, CARG3, lsr #8 | ||
1739 | | b ->fff_restv | ||
1740 | | | ||
1741 | |.ffunc_bit bnot | ||
1742 | | mvn CARG1, CARG1 | ||
1743 | | mvn CARG2, #~LJ_TISNUM | ||
1744 | | b ->fff_restv | ||
1745 | | | ||
1746 | |.macro .ffunc_bit_sh, name, ins, shmod | ||
1747 | | .ffunc bit_..name | ||
1748 | | ldrd CARG12, [BASE, #8] | ||
1749 | | cmp NARGS8:RC, #16 | ||
1750 | | blo ->fff_fallback | ||
1751 | | checktp CARG2, LJ_TISNUM | ||
1752 | | blne ->vm_tobit_fb | ||
1753 | |.if shmod == 0 | ||
1754 | | and RA, CARG1, #31 | ||
1755 | |.else | ||
1756 | | rsb RA, CARG1, #0 | ||
1757 | |.endif | ||
1758 | | ldrd CARG12, [BASE] | ||
1759 | | checktp CARG2, LJ_TISNUM | ||
1760 | | blne ->vm_tobit_fb | ||
1761 | | ins CARG1, CARG1, RA | ||
1762 | | mvn CARG2, #~LJ_TISNUM | ||
1763 | | b ->fff_restv | ||
1764 | |.endmacro | ||
1765 | | | ||
1766 | |.ffunc_bit_sh lshift, lsl, 0 | ||
1767 | |.ffunc_bit_sh rshift, lsr, 0 | ||
1768 | |.ffunc_bit_sh arshift, asr, 0 | ||
1769 | |.ffunc_bit_sh rol, ror, 1 | ||
1770 | |.ffunc_bit_sh ror, ror, 0 | ||
1771 | | | ||
1772 | |//----------------------------------------------------------------------- | ||
1773 | | | ||
1774 | |->fff_fallback: // Call fast function fallback handler. | ||
1775 | | // BASE = new base, RC = nargs*8 | ||
1776 | | ldr CARG3, [BASE, FRAME_FUNC] | ||
1777 | | ldr CARG2, L->maxstack | ||
1778 | | add CARG1, BASE, NARGS8:RC | ||
1779 | | ldr PC, [BASE, FRAME_PC] // Fallback may overwrite PC. | ||
1780 | | str CARG1, L->top | ||
1781 | | ldr CARG3, CFUNC:CARG3->f | ||
1782 | | str BASE, L->base | ||
1783 | | add CARG1, CARG1, #8*LUA_MINSTACK | ||
1784 | | str PC, SAVE_PC // Redundant (but a defined value). | ||
1785 | | cmp CARG1, CARG2 | ||
1786 | | mov CARG1, L | ||
1787 | | bhi >5 // Need to grow stack. | ||
1788 | | blx CARG3 // (lua_State *L) | ||
1789 | | // Either throws an error, or recovers and returns -1, 0 or nresults+1. | ||
1790 | | ldr BASE, L->base | ||
1791 | | cmp CRET1, #0 | ||
1792 | | lsl RC, CRET1, #3 | ||
1793 | | sub RA, BASE, #8 | ||
1794 | | bgt ->fff_res // Returned nresults+1? | ||
1795 | |1: // Returned 0 or -1: retry fast path. | ||
1796 | | ldr CARG1, L->top | ||
1797 | | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] | ||
1798 | | sub NARGS8:RC, CARG1, BASE | ||
1799 | | bne ->vm_call_tail // Returned -1? | ||
1800 | | ins_callt // Returned 0: retry fast path. | ||
1801 | | | ||
1802 | |// Reconstruct previous base for vmeta_call during tailcall. | ||
1803 | |->vm_call_tail: | ||
1804 | | ands CARG1, PC, #FRAME_TYPE | ||
1805 | | bic CARG2, PC, #FRAME_TYPEP | ||
1806 | | ldreq INS, [PC, #-4] | ||
1807 | | andeq CARG2, MASKR8, INS, lsr #5 // Conditional decode_RA8. | ||
1808 | | sub RB, BASE, CARG2 | ||
1809 | | b ->vm_call_dispatch // Resolve again for tailcall. | ||
1810 | | | ||
1811 | |5: // Grow stack for fallback handler. | ||
1812 | | mov CARG2, #LUA_MINSTACK | ||
1813 | | bl extern lj_state_growstack // (lua_State *L, int n) | ||
1814 | | ldr BASE, L->base | ||
1815 | | cmp CARG1, CARG1 // Set zero-flag to force retry. | ||
1816 | | b <1 | ||
1817 | | | ||
1818 | |->fff_gcstep: // Call GC step function. | ||
1819 | | // BASE = new base, RC = nargs*8 | ||
1820 | | mov RA, lr | ||
1821 | | str BASE, L->base | ||
1822 | | add CARG2, BASE, NARGS8:RC | ||
1823 | | str PC, SAVE_PC // Redundant (but a defined value). | ||
1824 | | str CARG2, L->top | ||
1825 | | mov CARG1, L | ||
1826 | | bl extern lj_gc_step // (lua_State *L) | ||
1827 | | ldr BASE, L->base | ||
1828 | | mov lr, RA // Help return address predictor. | ||
1829 | | ldr CFUNC:CARG3, [BASE, FRAME_FUNC] | ||
1830 | | bx lr | ||
1831 | | | ||
1832 | |//----------------------------------------------------------------------- | ||
1833 | |//-- Special dispatch targets ------------------------------------------- | ||
1834 | |//----------------------------------------------------------------------- | ||
1835 | | | ||
1836 | |->vm_record: // Dispatch target for recording phase. | ||
1837 | #if LJ_HASJIT | ||
1838 | | ldrb CARG1, [DISPATCH, #DISPATCH_GL(hookmask)] | ||
1839 | | tst CARG1, #HOOK_VMEVENT // No recording while in vmevent. | ||
1840 | | bne >5 | ||
1841 | | // Decrement the hookcount for consistency, but always do the call. | ||
1842 | | ldr CARG2, [DISPATCH, #DISPATCH_GL(hookcount)] | ||
1843 | | tst CARG1, #HOOK_ACTIVE | ||
1844 | | bne >1 | ||
1845 | | sub CARG2, CARG2, #1 | ||
1846 | | tst CARG1, #LUA_MASKLINE|LUA_MASKCOUNT | ||
1847 | | strne CARG2, [DISPATCH, #DISPATCH_GL(hookcount)] | ||
1848 | | b >1 | ||
1849 | #endif | ||
1850 | | | ||
1851 | |->vm_rethook: // Dispatch target for return hooks. | ||
1852 | | ldrb CARG1, [DISPATCH, #DISPATCH_GL(hookmask)] | ||
1853 | | tst CARG1, #HOOK_ACTIVE // Hook already active? | ||
1854 | | beq >1 | ||
1855 | |5: // Re-dispatch to static ins. | ||
1856 | | decode_OP OP, INS | ||
1857 | | add OP, DISPATCH, OP, lsl #2 | ||
1858 | | ldr pc, [OP, #GG_DISP2STATIC] | ||
1859 | | | ||
1860 | |->vm_inshook: // Dispatch target for instr/line hooks. | ||
1861 | | ldrb CARG1, [DISPATCH, #DISPATCH_GL(hookmask)] | ||
1862 | | ldr CARG2, [DISPATCH, #DISPATCH_GL(hookcount)] | ||
1863 | | tst CARG1, #HOOK_ACTIVE // Hook already active? | ||
1864 | | bne <5 | ||
1865 | | tst CARG1, #LUA_MASKLINE|LUA_MASKCOUNT | ||
1866 | | beq <5 | ||
1867 | | subs CARG2, CARG2, #1 | ||
1868 | | str CARG2, [DISPATCH, #DISPATCH_GL(hookcount)] | ||
1869 | | beq >1 | ||
1870 | | tst CARG1, #LUA_MASKLINE | ||
1871 | | beq <5 | ||
1872 | |1: | ||
1873 | | mov CARG1, L | ||
1874 | | str BASE, L->base | ||
1875 | | mov CARG2, PC | ||
1876 | | // SAVE_PC must hold the _previous_ PC. The callee updates it with PC. | ||
1877 | | bl extern lj_dispatch_ins // (lua_State *L, const BCIns *pc) | ||
1878 | |3: | ||
1879 | | ldr BASE, L->base | ||
1880 | |4: // Re-dispatch to static ins. | ||
1881 | | ldrb OP, [PC, #-4] | ||
1882 | | ldr INS, [PC, #-4] | ||
1883 | | add OP, DISPATCH, OP, lsl #2 | ||
1884 | | ldr OP, [OP, #GG_DISP2STATIC] | ||
1885 | | decode_RA8 RA, INS | ||
1886 | | decode_RD RC, INS | ||
1887 | | bx OP | ||
1888 | | | ||
1889 | |->cont_hook: // Continue from hook yield. | ||
1890 | | ldr CARG1, [CARG4, #-24] | ||
1891 | | add PC, PC, #4 | ||
1892 | | str CARG1, SAVE_MULTRES // Restore MULTRES for *M ins. | ||
1893 | | b <4 | ||
1894 | | | ||
1895 | |->vm_hotloop: // Hot loop counter underflow. | ||
1896 | #if LJ_HASJIT | ||
1897 | | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Same as curr_topL(L). | ||
1898 | | sub CARG1, DISPATCH, #-GG_DISP2J | ||
1899 | | str PC, SAVE_PC | ||
1900 | | ldr CARG3, LFUNC:CARG3->field_pc | ||
1901 | | mov CARG2, PC | ||
1902 | | str L, [DISPATCH, #DISPATCH_J(L)] | ||
1903 | | ldrb CARG3, [CARG3, #PC2PROTO(framesize)] | ||
1904 | | str BASE, L->base | ||
1905 | | add CARG3, BASE, CARG3, lsl #3 | ||
1906 | | str CARG3, L->top | ||
1907 | | bl extern lj_trace_hot // (jit_State *J, const BCIns *pc) | ||
1908 | | b <3 | ||
1909 | #endif | ||
1910 | | | ||
1911 | |->vm_callhook: // Dispatch target for call hooks. | ||
1912 | | mov CARG2, PC | ||
1913 | #if LJ_HASJIT | ||
1914 | | b >1 | ||
1915 | #endif | ||
1916 | | | ||
1917 | |->vm_hotcall: // Hot call counter underflow. | ||
1918 | #if LJ_HASJIT | ||
1919 | | orr CARG2, PC, #1 | ||
1920 | |1: | ||
1921 | #endif | ||
1922 | | add CARG4, BASE, RC | ||
1923 | | str PC, SAVE_PC | ||
1924 | | mov CARG1, L | ||
1925 | | str BASE, L->base | ||
1926 | | sub RA, RA, BASE | ||
1927 | | str CARG4, L->top | ||
1928 | | bl extern lj_dispatch_call // (lua_State *L, const BCIns *pc) | ||
1929 | | // Returns ASMFunction. | ||
1930 | | ldr BASE, L->base | ||
1931 | | ldr CARG4, L->top | ||
1932 | | mov CARG2, #0 | ||
1933 | | add RA, BASE, RA | ||
1934 | | sub NARGS8:RC, CARG4, BASE | ||
1935 | | str CARG2, SAVE_PC // Invalidate for subsequent line hook. | ||
1936 | | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] | ||
1937 | | ldr INS, [PC, #-4] | ||
1938 | | bx CRET1 | ||
1939 | | | ||
1940 | |//----------------------------------------------------------------------- | ||
1941 | |//-- Trace exit handler ------------------------------------------------- | ||
1942 | |//----------------------------------------------------------------------- | ||
1943 | | | ||
1944 | |->vm_exit_handler: | ||
1945 | #if LJ_HASJIT | ||
1946 | | sub sp, sp, #12 | ||
1947 | | push {r0,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12} | ||
1948 | | ldr CARG1, [sp, #64] // Load original value of lr. | ||
1949 | | ldr DISPATCH, [lr] // Load DISPATCH. | ||
1950 | | add CARG3, sp, #64 // Recompute original value of sp. | ||
1951 | | mv_vmstate CARG4, EXIT | ||
1952 | | str CARG3, [sp, #52] // Store sp in RID_SP | ||
1953 | | st_vmstate CARG4 | ||
1954 | | ldr CARG2, [CARG1, #-4]! // Get exit instruction. | ||
1955 | | str CARG1, [sp, #56] // Store exit pc in RID_LR and RID_PC. | ||
1956 | | str CARG1, [sp, #60] | ||
1957 | | lsl CARG2, CARG2, #8 | ||
1958 | | add CARG1, CARG1, CARG2, asr #6 | ||
1959 | | ldr CARG2, [lr, #4] // Load exit stub group offset. | ||
1960 | | sub CARG1, CARG1, lr | ||
1961 | | ldr L, [DISPATCH, #DISPATCH_GL(jit_L)] | ||
1962 | | add CARG1, CARG2, CARG1, lsr #2 // Compute exit number. | ||
1963 | | ldr BASE, [DISPATCH, #DISPATCH_GL(jit_base)] | ||
1964 | | str CARG1, [DISPATCH, #DISPATCH_J(exitno)] | ||
1965 | | mov CARG4, #0 | ||
1966 | | str L, [DISPATCH, #DISPATCH_J(L)] | ||
1967 | | str BASE, L->base | ||
1968 | | str CARG4, [DISPATCH, #DISPATCH_GL(jit_L)] | ||
1969 | | sub CARG1, DISPATCH, #-GG_DISP2J | ||
1970 | | mov CARG2, sp | ||
1971 | | bl extern lj_trace_exit // (jit_State *J, ExitState *ex) | ||
1972 | | // Returns MULTRES (unscaled) or negated error code. | ||
1973 | | ldr CARG2, L->cframe | ||
1974 | | ldr BASE, L->base | ||
1975 | | bic CARG2, CARG2, #~CFRAME_RAWMASK // Use two steps: bic sp is deprecated. | ||
1976 | | mov sp, CARG2 | ||
1977 | | ldr PC, SAVE_PC // Get SAVE_PC. | ||
1978 | | str L, SAVE_L // Set SAVE_L (on-trace resume/yield). | ||
1979 | | b >1 | ||
1980 | #endif | ||
1981 | |->vm_exit_interp: | ||
1982 | | // CARG1 = MULTRES or negated error code, BASE, PC and DISPATCH set. | ||
1983 | #if LJ_HASJIT | ||
1984 | | ldr L, SAVE_L | ||
1985 | |1: | ||
1986 | | cmp CARG1, #0 | ||
1987 | | blt >3 // Check for error from exit. | ||
1988 | | lsl RC, CARG1, #3 | ||
1989 | | ldr LFUNC:CARG2, [BASE, FRAME_FUNC] | ||
1990 | | str RC, SAVE_MULTRES | ||
1991 | | mov CARG3, #0 | ||
1992 | | ldr CARG2, LFUNC:CARG2->field_pc | ||
1993 | | str CARG3, [DISPATCH, #DISPATCH_GL(jit_L)] | ||
1994 | | mv_vmstate CARG4, INTERP | ||
1995 | | ldr KBASE, [CARG2, #PC2PROTO(k)] | ||
1996 | | // Modified copy of ins_next which handles function header dispatch, too. | ||
1997 | | ldrb OP, [PC] | ||
1998 | | mov MASKR8, #255 | ||
1999 | | ldr INS, [PC], #4 | ||
2000 | | lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8. | ||
2001 | | st_vmstate CARG4 | ||
2002 | | cmp OP, #BC_FUNCF // Function header? | ||
2003 | | ldr OP, [DISPATCH, OP, lsl #2] | ||
2004 | | decode_RA8 RA, INS | ||
2005 | | lsrlo RC, INS, #16 // No: Decode operands A*8 and D. | ||
2006 | | subhs RC, RC, #8 | ||
2007 | | addhs RA, RA, BASE // Yes: RA = BASE+framesize*8, RC = nargs*8 | ||
2008 | | bx OP | ||
2009 | | | ||
2010 | |3: // Rethrow error from the right C frame. | ||
2011 | | rsb CARG2, CARG1, #0 | ||
2012 | | mov CARG1, L | ||
2013 | | bl extern lj_err_throw // (lua_State *L, int errcode) | ||
2014 | #endif | ||
2015 | | | ||
2016 | |//----------------------------------------------------------------------- | ||
2017 | |//-- Math helper functions ---------------------------------------------- | ||
2018 | |//----------------------------------------------------------------------- | ||
2019 | | | ||
2020 | |// FP value rounding. Called from JIT code. | ||
2021 | |// | ||
2022 | |// double lj_vm_floor/ceil/trunc(double x); | ||
2023 | |.macro vm_round, func | ||
2024 | |->vm_ .. func: | ||
2025 | | lsl CARG3, CARG2, #1 | ||
2026 | | adds RB, CARG3, #0x00200000 | ||
2027 | | bpl >2 // |x| < 1? | ||
2028 | | mvn CARG4, #0x3cc | ||
2029 | | subs RB, CARG4, RB, asr #21 // 2^0: RB = 51, 2^51: RB = 0. | ||
2030 | | bxlo lr // |x| >= 2^52: done. | ||
2031 | | mvn CARG4, #1 | ||
2032 | | bic CARG3, CARG1, CARG4, lsl RB // ztest = lo & ~lomask | ||
2033 | | and CARG1, CARG1, CARG4, lsl RB // lo &= lomask | ||
2034 | | subs RB, RB, #32 | ||
2035 | | bicpl CARG4, CARG2, CARG4, lsl RB // |x| <= 2^20: ztest |= hi & ~himask | ||
2036 | | orrpl CARG3, CARG3, CARG4 | ||
2037 | | mvnpl CARG4, #1 | ||
2038 | | andpl CARG2, CARG2, CARG4, lsl RB // |x| <= 2^20: hi &= himask | ||
2039 | |.if "func" == "floor" | ||
2040 | | tst CARG3, CARG2, asr #31 // iszero = ((ztest & signmask) == 0) | ||
2041 | |.else | ||
2042 | | bics CARG3, CARG3, CARG2, asr #31 // iszero = ((ztest & ~signmask) == 0) | ||
2043 | |.endif | ||
2044 | | bxeq lr // iszero: done. | ||
2045 | | mvn CARG4, #1 | ||
2046 | | cmp RB, #0 | ||
2047 | | lslpl CARG3, CARG4, RB | ||
2048 | | mvnmi CARG3, #0 | ||
2049 | | add RB, RB, #32 | ||
2050 | | subs CARG1, CARG1, CARG4, lsl RB // lo = lo-lomask | ||
2051 | | sbc CARG2, CARG2, CARG3 // hi = hi-himask+carry | ||
2052 | | bx lr | ||
2053 | | | ||
2054 | |2: // |x| < 1: | ||
2055 | | orr CARG3, CARG3, CARG1 // ztest = (2*hi) | lo | ||
2056 | |.if "func" == "floor" | ||
2057 | | tst CARG3, CARG2, asr #31 // iszero = ((ztest & signmask) == 0) | ||
2058 | |.else | ||
2059 | | bics CARG3, CARG3, CARG2, asr #31 // iszero = ((ztest & ~signmask) == 0) | ||
2060 | |.endif | ||
2061 | | mov CARG1, #0 // lo = 0 | ||
2062 | | and CARG2, CARG2, #0x80000000 | ||
2063 | | ldrne CARG4, <9 // hi = sign(x) | (iszero ? 0.0 : 1.0) | ||
2064 | | orrne CARG2, CARG2, CARG4 | ||
2065 | | bx lr | ||
2066 | |.endmacro | ||
2067 | | | ||
2068 | |9: | ||
2069 | | .long 0x3ff00000 // hiword(1.0) | ||
2070 | | vm_round floor | ||
2071 | | vm_round ceil | ||
2072 | | | ||
2073 | |->vm_trunc: | ||
2074 | #if LJ_HASJIT | ||
2075 | | lsl CARG3, CARG2, #1 | ||
2076 | | adds RB, CARG3, #0x00200000 | ||
2077 | | andpl CARG2, CARG2, #0x80000000 // |x| < 1? hi = sign(x), lo = 0. | ||
2078 | | movpl CARG1, #0 | ||
2079 | | bxpl lr | ||
2080 | | mvn CARG4, #0x3cc | ||
2081 | | subs RB, CARG4, RB, asr #21 // 2^0: RB = 51, 2^51: RB = 0. | ||
2082 | | bxlo lr // |x| >= 2^52: already done. | ||
2083 | | mvn CARG4, #1 | ||
2084 | | and CARG1, CARG1, CARG4, lsl RB // lo &= lomask | ||
2085 | | subs RB, RB, #32 | ||
2086 | | andpl CARG2, CARG2, CARG4, lsl RB // |x| <= 2^20: hi &= himask | ||
2087 | | bx lr | ||
2088 | #endif | ||
2089 | | | ||
2090 | | // double lj_vm_mod(double dividend, double divisor); | ||
2091 | |->vm_mod: | ||
2092 | | push {r0, r1, r2, r3, r4, lr} | ||
2093 | | bl extern __aeabi_ddiv | ||
2094 | | bl ->vm_floor | ||
2095 | | ldrd CARG34, [sp, #8] | ||
2096 | | bl extern __aeabi_dmul | ||
2097 | | ldrd CARG34, [sp] | ||
2098 | | eor CARG2, CARG2, #0x80000000 | ||
2099 | | bl extern __aeabi_dadd | ||
2100 | | add sp, sp, #20 | ||
2101 | | pop {pc} | ||
2102 | | | ||
2103 | | // int lj_vm_modi(int dividend, int divisor); | ||
2104 | |->vm_modi: | ||
2105 | | ands RB, CARG1, #0x80000000 | ||
2106 | | rsbmi CARG1, CARG1, #0 // a = |dividend| | ||
2107 | | eor RB, RB, CARG2, asr #1 // Keep signdiff and sign(divisor). | ||
2108 | | cmp CARG2, #0 | ||
2109 | | rsbmi CARG2, CARG2, #0 // b = |divisor| | ||
2110 | | subs CARG4, CARG2, #1 | ||
2111 | | cmpne CARG1, CARG2 | ||
2112 | | moveq CARG1, #0 // if (b == 1 || a == b) a = 0 | ||
2113 | | tsthi CARG2, CARG4 | ||
2114 | | andeq CARG1, CARG1, CARG4 // else if ((b & (b-1)) == 0) a &= b-1 | ||
2115 | | bls >1 | ||
2116 | | // Use repeated subtraction to get the remainder. | ||
2117 | | clz CARG3, CARG1 | ||
2118 | | clz CARG4, CARG2 | ||
2119 | | sub CARG4, CARG4, CARG3 | ||
2120 | | rsbs CARG3, CARG4, #31 // entry = (31-(clz(b)-clz(a)))*8 | ||
2121 | | addne pc, pc, CARG3, lsl #3 // Duff's device. | ||
2122 | | nop | ||
2123 | { | ||
2124 | int i; | ||
2125 | for (i = 31; i >= 0; i--) { | ||
2126 | | cmp CARG1, CARG2, lsl #i | ||
2127 | | subhs CARG1, CARG1, CARG2, lsl #i | ||
2128 | } | ||
2129 | } | ||
2130 | |1: | ||
2131 | | cmp CARG1, #0 | ||
2132 | | cmpne RB, #0 | ||
2133 | | submi CARG1, CARG1, CARG2 // if (y != 0 && signdiff) y = y - b | ||
2134 | | eors CARG2, CARG1, RB, lsl #1 | ||
2135 | | rsbmi CARG1, CARG1, #0 // if (sign(divisor) != sign(y)) y = -y | ||
2136 | | bx lr | ||
2137 | | | ||
2138 | |// Callable from C: double lj_vm_foldarith(double x, double y, int op) | ||
2139 | |// Compute x op y for basic arithmetic operators (+ - * / % ^ and unary -) | ||
2140 | |// and basic math functions. ORDER ARITH | ||
2141 | |->vm_foldarith: | ||
2142 | | ldr OP, [sp] | ||
2143 | | cmp OP, #1 | ||
2144 | | blo extern __aeabi_dadd | ||
2145 | | beq extern __aeabi_dsub | ||
2146 | | cmp OP, #3 | ||
2147 | | blo extern __aeabi_dmul | ||
2148 | | beq extern __aeabi_ddiv | ||
2149 | | cmp OP, #5 | ||
2150 | | blo ->vm_mod | ||
2151 | | beq extern pow | ||
2152 | | cmp OP, #7 | ||
2153 | | eorlo CARG2, CARG2, #0x80000000 | ||
2154 | | biceq CARG2, CARG2, #0x80000000 | ||
2155 | | bxls lr | ||
2156 | #if LJ_HASJIT | ||
2157 | | cmp OP, #9 | ||
2158 | | blo extern atan2 | ||
2159 | | beq >9 // No support needed for IR_LDEXP. | ||
2160 | | cmp OP, #11 | ||
2161 | | bhi >9 | ||
2162 | | push {r4, lr} | ||
2163 | | beq >1 | ||
2164 | | // IR_MIN | ||
2165 | | bl extern __aeabi_cdcmple | ||
2166 | | movhi CARG1, CARG3 | ||
2167 | | movhi CARG2, CARG4 | ||
2168 | | pop {r4, pc} | ||
2169 | |9: | ||
2170 | | NYI // Bad op. | ||
2171 | | | ||
2172 | |1: // IR_MAX | ||
2173 | | bl extern __aeabi_cdcmple | ||
2174 | | movlo CARG1, CARG3 | ||
2175 | | movlo CARG2, CARG4 | ||
2176 | | pop {r4, pc} | ||
2177 | #else | ||
2178 | | NYI // Other operations only needed by JIT compiler. | ||
2179 | #endif | ||
2180 | | | ||
2181 | |//----------------------------------------------------------------------- | ||
2182 | |//-- Miscellaneous functions -------------------------------------------- | ||
2183 | |//----------------------------------------------------------------------- | ||
2184 | | | ||
2185 | |//----------------------------------------------------------------------- | ||
2186 | |//-- FFI helper functions ----------------------------------------------- | ||
2187 | |//----------------------------------------------------------------------- | ||
2188 | | | ||
2189 | |// Handler for callback functions. | ||
2190 | |// Saveregs already performed. Callback slot number in [sp], g in r12. | ||
2191 | |->vm_ffi_callback: | ||
2192 | #if LJ_HASFFI | ||
2193 | |.type CTSTATE, CTState, PC | ||
2194 | | ldr CTSTATE, GL:r12->ctype_state | ||
2195 | | add DISPATCH, r12, #GG_G2DISP | ||
2196 | | strd CARG12, CTSTATE->cb.gpr[0] | ||
2197 | | strd CARG34, CTSTATE->cb.gpr[2] | ||
2198 | | ldr CARG4, [sp] | ||
2199 | | add CARG3, sp, #CFRAME_SIZE | ||
2200 | | mov CARG1, CTSTATE | ||
2201 | | lsr CARG4, CARG4, #3 | ||
2202 | | str CARG3, CTSTATE->cb.stack | ||
2203 | | mov CARG2, sp | ||
2204 | | str CARG4, CTSTATE->cb.slot | ||
2205 | | str CTSTATE, SAVE_PC // Any value outside of bytecode is ok. | ||
2206 | | bl extern lj_ccallback_enter // (CTState *cts, void *cf) | ||
2207 | | // Returns lua_State *. | ||
2208 | | ldr BASE, L:CRET1->base | ||
2209 | | mv_vmstate CARG2, INTERP | ||
2210 | | ldr RC, L:CRET1->top | ||
2211 | | mov MASKR8, #255 | ||
2212 | | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] | ||
2213 | | mov L, CRET1 | ||
2214 | | sub RC, RC, BASE | ||
2215 | | lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8. | ||
2216 | | st_vmstate CARG2 | ||
2217 | | ins_callt | ||
2218 | #endif | ||
2219 | | | ||
2220 | |->cont_ffi_callback: // Return from FFI callback. | ||
2221 | #if LJ_HASFFI | ||
2222 | | ldr CTSTATE, [DISPATCH, #DISPATCH_GL(ctype_state)] | ||
2223 | | str BASE, L->base | ||
2224 | | str CARG4, L->top | ||
2225 | | str L, CTSTATE->L | ||
2226 | | mov CARG1, CTSTATE | ||
2227 | | mov CARG2, RA | ||
2228 | | bl extern lj_ccallback_leave // (CTState *cts, TValue *o) | ||
2229 | | ldrd CARG12, CTSTATE->cb.gpr[0] | ||
2230 | | b ->vm_leave_unw | ||
2231 | #endif | ||
2232 | | | ||
2233 | |->vm_ffi_call: // Call C function via FFI. | ||
2234 | | // Caveat: needs special frame unwinding, see below. | ||
2235 | #if LJ_HASFFI | ||
2236 | | .type CCSTATE, CCallState, r4 | ||
2237 | | push {CCSTATE, r5, r11, lr} | ||
2238 | | mov CCSTATE, CARG1 | ||
2239 | | ldr CARG1, CCSTATE:CARG1->spadj | ||
2240 | | ldrb CARG2, CCSTATE->nsp | ||
2241 | | add CARG3, CCSTATE, #offsetof(CCallState, stack) | ||
2242 | | mov r11, sp | ||
2243 | | sub sp, sp, CARG1 // Readjust stack. | ||
2244 | | subs CARG2, CARG2, #1 | ||
2245 | | ldr RB, CCSTATE->func | ||
2246 | | bmi >2 | ||
2247 | |1: // Copy stack slots. | ||
2248 | | ldr CARG4, [CARG3, CARG2, lsl #2] | ||
2249 | | str CARG4, [sp, CARG2, lsl #2] | ||
2250 | | subs CARG2, CARG2, #1 | ||
2251 | | bpl <1 | ||
2252 | |2: | ||
2253 | | ldr CARG1, CCSTATE->gpr[0] | ||
2254 | | ldr CARG2, CCSTATE->gpr[1] | ||
2255 | | ldr CARG3, CCSTATE->gpr[2] | ||
2256 | | ldr CARG4, CCSTATE->gpr[3] | ||
2257 | | blx RB | ||
2258 | | mov sp, r11 | ||
2259 | | str CRET1, CCSTATE->gpr[0] | ||
2260 | | str CRET2, CCSTATE->gpr[1] | ||
2261 | | pop {CCSTATE, r5, r11, pc} | ||
2262 | #endif | ||
2263 | |// Note: vm_ffi_call must be the last function in this object file! | ||
2264 | | | ||
2265 | |//----------------------------------------------------------------------- | ||
2266 | } | ||
2267 | |||
2268 | /* Generate the code for a single instruction. */ | ||
2269 | static void build_ins(BuildCtx *ctx, BCOp op, int defop) | ||
2270 | { | ||
2271 | int vk = 0; | ||
2272 | |=>defop: | ||
2273 | |||
2274 | switch (op) { | ||
2275 | |||
2276 | /* -- Comparison ops ---------------------------------------------------- */ | ||
2277 | |||
2278 | /* Remember: all ops branch for a true comparison, fall through otherwise. */ | ||
2279 | |||
2280 | case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT: | ||
2281 | | // RA = src1*8, RC = src2, JMP with RC = target | ||
2282 | | lsl RC, RC, #3 | ||
2283 | | ldrd CARG12, [RA, BASE]! | ||
2284 | | ldrh RB, [PC, #2] | ||
2285 | | ldrd CARG34, [RC, BASE]! | ||
2286 | | add PC, PC, #4 | ||
2287 | | add RB, PC, RB, lsl #2 | ||
2288 | | checktp CARG2, LJ_TISNUM | ||
2289 | | bne >3 | ||
2290 | | checktp CARG4, LJ_TISNUM | ||
2291 | | bne >4 | ||
2292 | | cmp CARG1, CARG3 | ||
2293 | if (op == BC_ISLT) { | ||
2294 | | sublt PC, RB, #0x20000 | ||
2295 | } else if (op == BC_ISGE) { | ||
2296 | | subge PC, RB, #0x20000 | ||
2297 | } else if (op == BC_ISLE) { | ||
2298 | | suble PC, RB, #0x20000 | ||
2299 | } else { | ||
2300 | | subgt PC, RB, #0x20000 | ||
2301 | } | ||
2302 | |1: | ||
2303 | | ins_next | ||
2304 | | | ||
2305 | |3: // CARG12 is not an integer. | ||
2306 | | bhi ->vmeta_comp | ||
2307 | | // CARG12 is a number. | ||
2308 | | checktp CARG4, LJ_TISNUM | ||
2309 | | movlo RA, RB // Save RB. | ||
2310 | | blo >5 | ||
2311 | | // CARG12 is a number, CARG3 is an integer. | ||
2312 | | mov CARG1, CARG3 | ||
2313 | | mov RC, RA | ||
2314 | | mov RA, RB // Save RB. | ||
2315 | | bl extern __aeabi_i2d | ||
2316 | | mov CARG3, CARG1 | ||
2317 | | mov CARG4, CARG2 | ||
2318 | | ldrd CARG12, [RC] // Restore first operand. | ||
2319 | | b >5 | ||
2320 | |4: // CARG1 is an integer, CARG34 is not an integer. | ||
2321 | | bhi ->vmeta_comp | ||
2322 | | // CARG1 is an integer, CARG34 is a number | ||
2323 | | mov RA, RB // Save RB. | ||
2324 | | bl extern __aeabi_i2d | ||
2325 | | ldrd CARG34, [RC] // Restore second operand. | ||
2326 | |5: // CARG12 and CARG34 are numbers. | ||
2327 | | bl extern __aeabi_cdcmple | ||
2328 | | // To preserve NaN semantics GE/GT branch on unordered, but LT/LE don't. | ||
2329 | if (op == BC_ISLT) { | ||
2330 | | sublo PC, RA, #0x20000 | ||
2331 | } else if (op == BC_ISGE) { | ||
2332 | | subhs PC, RA, #0x20000 | ||
2333 | } else if (op == BC_ISLE) { | ||
2334 | | subls PC, RA, #0x20000 | ||
2335 | } else { | ||
2336 | | subhi PC, RA, #0x20000 | ||
2337 | } | ||
2338 | | b <1 | ||
2339 | break; | ||
2340 | |||
2341 | case BC_ISEQV: case BC_ISNEV: | ||
2342 | vk = op == BC_ISEQV; | ||
2343 | | // RA = src1*8, RC = src2, JMP with RC = target | ||
2344 | | lsl RC, RC, #3 | ||
2345 | | ldrd CARG12, [RA, BASE]! | ||
2346 | | ldrh RB, [PC, #2] | ||
2347 | | ldrd CARG34, [RC, BASE]! | ||
2348 | | add PC, PC, #4 | ||
2349 | | add RB, PC, RB, lsl #2 | ||
2350 | | checktp CARG2, LJ_TISNUM | ||
2351 | | cmnls CARG4, #-LJ_TISNUM | ||
2352 | if (vk) { | ||
2353 | | bls ->BC_ISEQN_Z | ||
2354 | } else { | ||
2355 | | bls ->BC_ISNEN_Z | ||
2356 | } | ||
2357 | | // Either or both types are not numbers. | ||
2358 | if (LJ_HASFFI) { | ||
2359 | | checktp CARG2, LJ_TCDATA | ||
2360 | | checktpne CARG4, LJ_TCDATA | ||
2361 | | beq ->vmeta_equal_cd | ||
2362 | } | ||
2363 | | cmp CARG2, CARG4 // Compare types. | ||
2364 | | bne >2 // Not the same type? | ||
2365 | | checktp CARG2, LJ_TISPRI | ||
2366 | | bhs >1 // Same type and primitive type? | ||
2367 | | | ||
2368 | | // Same types and not a primitive type. Compare GCobj or pvalue. | ||
2369 | | cmp CARG1, CARG3 | ||
2370 | if (vk) { | ||
2371 | | bne >3 // Different GCobjs or pvalues? | ||
2372 | |1: // Branch if same. | ||
2373 | | sub PC, RB, #0x20000 | ||
2374 | |2: // Different. | ||
2375 | | ins_next | ||
2376 | |3: | ||
2377 | | checktp CARG2, LJ_TISTABUD | ||
2378 | | bhi <2 // Different objects and not table/ud? | ||
2379 | } else { | ||
2380 | | beq >1 // Same GCobjs or pvalues? | ||
2381 | | checktp CARG2, LJ_TISTABUD | ||
2382 | | bhi >2 // Different objects and not table/ud? | ||
2383 | } | ||
2384 | | // Different tables or userdatas. Need to check __eq metamethod. | ||
2385 | | // Field metatable must be at same offset for GCtab and GCudata! | ||
2386 | | ldr TAB:RA, TAB:CARG1->metatable | ||
2387 | | cmp TAB:RA, #0 | ||
2388 | if (vk) { | ||
2389 | | beq <2 // No metatable? | ||
2390 | } else { | ||
2391 | | beq >2 // No metatable? | ||
2392 | } | ||
2393 | | ldrb RA, TAB:RA->nomm | ||
2394 | | mov CARG4, #1-vk // ne = 0 or 1. | ||
2395 | | mov CARG2, CARG1 | ||
2396 | | tst RA, #1<<MM_eq | ||
2397 | | beq ->vmeta_equal // 'no __eq' flag not set? | ||
2398 | if (vk) { | ||
2399 | | b <2 | ||
2400 | } else { | ||
2401 | |2: // Branch if different. | ||
2402 | | sub PC, RB, #0x20000 | ||
2403 | |1: // Same. | ||
2404 | | ins_next | ||
2405 | } | ||
2406 | break; | ||
2407 | |||
2408 | case BC_ISEQS: case BC_ISNES: | ||
2409 | vk = op == BC_ISEQS; | ||
2410 | | // RA = src*8, RC = str_const (~), JMP with RC = target | ||
2411 | | mvn RC, RC | ||
2412 | | ldrd CARG12, [BASE, RA] | ||
2413 | | ldrh RB, [PC, #2] | ||
2414 | | ldr STR:CARG3, [KBASE, RC, lsl #2] | ||
2415 | | add PC, PC, #4 | ||
2416 | | add RB, PC, RB, lsl #2 | ||
2417 | | checktp CARG2, LJ_TSTR | ||
2418 | if (LJ_HASFFI) { | ||
2419 | | bne >7 | ||
2420 | | cmp CARG1, CARG3 | ||
2421 | } else { | ||
2422 | | cmpeq CARG1, CARG3 | ||
2423 | } | ||
2424 | if (vk) { | ||
2425 | | subeq PC, RB, #0x20000 | ||
2426 | |1: | ||
2427 | } else { | ||
2428 | |1: | ||
2429 | | subne PC, RB, #0x20000 | ||
2430 | } | ||
2431 | | ins_next | ||
2432 | | | ||
2433 | if (LJ_HASFFI) { | ||
2434 | |7: | ||
2435 | | checktp CARG2, LJ_TCDATA | ||
2436 | | bne <1 | ||
2437 | | b ->vmeta_equal_cd | ||
2438 | } | ||
2439 | break; | ||
2440 | |||
2441 | case BC_ISEQN: case BC_ISNEN: | ||
2442 | vk = op == BC_ISEQN; | ||
2443 | | // RA = src*8, RC = num_const (~), JMP with RC = target | ||
2444 | | lsl RC, RC, #3 | ||
2445 | | ldrd CARG12, [RA, BASE]! | ||
2446 | | ldrh RB, [PC, #2] | ||
2447 | | ldrd CARG34, [RC, KBASE]! | ||
2448 | | add PC, PC, #4 | ||
2449 | | add RB, PC, RB, lsl #2 | ||
2450 | if (vk) { | ||
2451 | |->BC_ISEQN_Z: | ||
2452 | } else { | ||
2453 | |->BC_ISNEN_Z: | ||
2454 | } | ||
2455 | | checktp CARG2, LJ_TISNUM | ||
2456 | | bne >3 | ||
2457 | | checktp CARG4, LJ_TISNUM | ||
2458 | | bne >4 | ||
2459 | | cmp CARG1, CARG3 | ||
2460 | if (vk) { | ||
2461 | | subeq PC, RB, #0x20000 | ||
2462 | |1: | ||
2463 | } else { | ||
2464 | |1: | ||
2465 | | subne PC, RB, #0x20000 | ||
2466 | } | ||
2467 | |2: | ||
2468 | | ins_next | ||
2469 | | | ||
2470 | |3: // CARG12 is not an integer. | ||
2471 | if (LJ_HASFFI) { | ||
2472 | | bhi >7 | ||
2473 | } else { | ||
2474 | if (!vk) { | ||
2475 | | subhi PC, RB, #0x20000 | ||
2476 | } | ||
2477 | | bhi <2 | ||
2478 | } | ||
2479 | | // CARG12 is a number. | ||
2480 | | checktp CARG4, LJ_TISNUM | ||
2481 | | movlo RA, RB // Save RB. | ||
2482 | | blo >5 | ||
2483 | | // CARG12 is a number, CARG3 is an integer. | ||
2484 | | mov CARG1, CARG3 | ||
2485 | | mov RC, RA | ||
2486 | |4: // CARG1 is an integer, CARG34 is a number. | ||
2487 | | mov RA, RB // Save RB. | ||
2488 | | bl extern __aeabi_i2d | ||
2489 | | ldrd CARG34, [RC] // Restore other operand. | ||
2490 | |5: // CARG12 and CARG34 are numbers. | ||
2491 | | bl extern __aeabi_cdcmpeq | ||
2492 | if (vk) { | ||
2493 | | subeq PC, RA, #0x20000 | ||
2494 | } else { | ||
2495 | | subne PC, RA, #0x20000 | ||
2496 | } | ||
2497 | | b <2 | ||
2498 | | | ||
2499 | if (LJ_HASFFI) { | ||
2500 | |7: | ||
2501 | | checktp CARG2, LJ_TCDATA | ||
2502 | | bne <1 | ||
2503 | | b ->vmeta_equal_cd | ||
2504 | } | ||
2505 | break; | ||
2506 | |||
2507 | case BC_ISEQP: case BC_ISNEP: | ||
2508 | vk = op == BC_ISEQP; | ||
2509 | | // RA = src*8, RC = primitive_type (~), JMP with RC = target | ||
2510 | | ldrd CARG12, [BASE, RA] | ||
2511 | | ldrh RB, [PC, #2] | ||
2512 | | add PC, PC, #4 | ||
2513 | | mvn RC, RC | ||
2514 | | add RB, PC, RB, lsl #2 | ||
2515 | if (LJ_HASFFI) { | ||
2516 | | checktp CARG2, LJ_TCDATA | ||
2517 | | beq ->vmeta_equal_cd | ||
2518 | } | ||
2519 | | cmp CARG2, RC | ||
2520 | if (vk) { | ||
2521 | | subeq PC, RB, #0x20000 | ||
2522 | } else { | ||
2523 | | subne PC, RB, #0x20000 | ||
2524 | } | ||
2525 | | ins_next | ||
2526 | break; | ||
2527 | |||
2528 | /* -- Unary test and copy ops ------------------------------------------- */ | ||
2529 | |||
2530 | case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF: | ||
2531 | | // RA = dst*8 or unused, RC = src, JMP with RC = target | ||
2532 | | add RC, BASE, RC, lsl #3 | ||
2533 | | ldrh RB, [PC, #2] | ||
2534 | | ldrd CARG12, [RC] | ||
2535 | | add PC, PC, #4 | ||
2536 | | add RB, PC, RB, lsl #2 | ||
2537 | | checktp CARG2, LJ_TTRUE | ||
2538 | if (op == BC_ISTC || op == BC_IST) { | ||
2539 | | subls PC, RB, #0x20000 | ||
2540 | if (op == BC_ISTC) { | ||
2541 | | strdls CARG12, [BASE, RA] | ||
2542 | } | ||
2543 | } else { | ||
2544 | | subhi PC, RB, #0x20000 | ||
2545 | if (op == BC_ISFC) { | ||
2546 | | strdhi CARG12, [BASE, RA] | ||
2547 | } | ||
2548 | } | ||
2549 | | ins_next | ||
2550 | break; | ||
2551 | |||
2552 | /* -- Unary ops --------------------------------------------------------- */ | ||
2553 | |||
2554 | case BC_MOV: | ||
2555 | | // RA = dst*8, RC = src | ||
2556 | | lsl RC, RC, #3 | ||
2557 | | ins_next1 | ||
2558 | | ldrd CARG12, [BASE, RC] | ||
2559 | | ins_next2 | ||
2560 | | strd CARG12, [BASE, RA] | ||
2561 | | ins_next3 | ||
2562 | break; | ||
2563 | case BC_NOT: | ||
2564 | | // RA = dst*8, RC = src | ||
2565 | | add RC, BASE, RC, lsl #3 | ||
2566 | | ins_next1 | ||
2567 | | ldr CARG1, [RC, #4] | ||
2568 | | add RA, BASE, RA | ||
2569 | | ins_next2 | ||
2570 | | checktp CARG1, LJ_TTRUE | ||
2571 | | mvnls CARG2, #~LJ_TFALSE | ||
2572 | | mvnhi CARG2, #~LJ_TTRUE | ||
2573 | | str CARG2, [RA, #4] | ||
2574 | | ins_next3 | ||
2575 | break; | ||
2576 | case BC_UNM: | ||
2577 | | // RA = dst*8, RC = src | ||
2578 | | lsl RC, RC, #3 | ||
2579 | | ldrd CARG12, [BASE, RC] | ||
2580 | | ins_next1 | ||
2581 | | ins_next2 | ||
2582 | | checktp CARG2, LJ_TISNUM | ||
2583 | | bhi ->vmeta_unm | ||
2584 | | eorne CARG2, CARG2, #0x80000000 | ||
2585 | | bne >5 | ||
2586 | | rsbseq CARG1, CARG1, #0 | ||
2587 | | ldrdvs CARG12, >9 | ||
2588 | |5: | ||
2589 | | strd CARG12, [BASE, RA] | ||
2590 | | ins_next3 | ||
2591 | | | ||
2592 | |.align 8 | ||
2593 | |9: | ||
2594 | | .long 0x00000000, 0x41e00000 // 2^31. | ||
2595 | break; | ||
2596 | case BC_LEN: | ||
2597 | | // RA = dst*8, RC = src | ||
2598 | | lsl RC, RC, #3 | ||
2599 | | ldrd CARG12, [BASE, RC] | ||
2600 | | checkstr CARG2, >2 | ||
2601 | | ldr CARG1, STR:CARG1->len | ||
2602 | |1: | ||
2603 | | mvn CARG2, #~LJ_TISNUM | ||
2604 | | ins_next1 | ||
2605 | | ins_next2 | ||
2606 | | strd CARG12, [BASE, RA] | ||
2607 | | ins_next3 | ||
2608 | |2: | ||
2609 | | checktab CARG2, ->vmeta_len | ||
2610 | #ifdef LUAJIT_ENABLE_LUA52COMPAT | ||
2611 | | ldr TAB:CARG3, TAB:CARG1->metatable | ||
2612 | | cmp TAB:CARG3, #0 | ||
2613 | | bne >9 | ||
2614 | |3: | ||
2615 | #endif | ||
2616 | |->BC_LEN_Z: | ||
2617 | | IOS mov RC, BASE | ||
2618 | | bl extern lj_tab_len // (GCtab *t) | ||
2619 | | // Returns uint32_t (but less than 2^31). | ||
2620 | | IOS mov BASE, RC | ||
2621 | | b <1 | ||
2622 | #ifdef LUAJIT_ENABLE_LUA52COMPAT | ||
2623 | |9: | ||
2624 | | ldrb CARG4, TAB:CARG3->nomm | ||
2625 | | tst CARG4, #1<<MM_len | ||
2626 | | bne <3 // 'no __len' flag set: done. | ||
2627 | | b ->vmeta_len | ||
2628 | #endif | ||
2629 | break; | ||
2630 | |||
2631 | /* -- Binary ops -------------------------------------------------------- */ | ||
2632 | |||
2633 | |.macro ins_arithcheck, cond, ncond, target | ||
2634 | ||if (vk == 1) { | ||
2635 | | cmn CARG4, #-LJ_TISNUM | ||
2636 | | cmn..cond CARG2, #-LJ_TISNUM | ||
2637 | ||} else { | ||
2638 | | cmn CARG2, #-LJ_TISNUM | ||
2639 | | cmn..cond CARG4, #-LJ_TISNUM | ||
2640 | ||} | ||
2641 | | b..ncond target | ||
2642 | |.endmacro | ||
2643 | |.macro ins_arithcheck_int, target | ||
2644 | | ins_arithcheck eq, ne, target | ||
2645 | |.endmacro | ||
2646 | |.macro ins_arithcheck_num, target | ||
2647 | | ins_arithcheck lo, hs, target | ||
2648 | |.endmacro | ||
2649 | | | ||
2650 | |.macro ins_arithpre | ||
2651 | | decode_RB8 RB, INS | ||
2652 | | decode_RC8 RC, INS | ||
2653 | | // RA = dst*8, RB = src1*8, RC = src2*8 | num_const*8 | ||
2654 | ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN); | ||
2655 | ||switch (vk) { | ||
2656 | ||case 0: | ||
2657 | | ldrd CARG12, [BASE, RB] | ||
2658 | | ldrd CARG34, [KBASE, RC] | ||
2659 | || break; | ||
2660 | ||case 1: | ||
2661 | | ldrd CARG34, [BASE, RB] | ||
2662 | | ldrd CARG12, [KBASE, RC] | ||
2663 | || break; | ||
2664 | ||default: | ||
2665 | | ldrd CARG12, [BASE, RB] | ||
2666 | | ldrd CARG34, [BASE, RC] | ||
2667 | || break; | ||
2668 | ||} | ||
2669 | |.endmacro | ||
2670 | | | ||
2671 | |.macro ins_arithfallback, ins | ||
2672 | ||switch (vk) { | ||
2673 | ||case 0: | ||
2674 | | ins ->vmeta_arith_vn | ||
2675 | || break; | ||
2676 | ||case 1: | ||
2677 | | ins ->vmeta_arith_nv | ||
2678 | || break; | ||
2679 | ||default: | ||
2680 | | ins ->vmeta_arith_vv | ||
2681 | || break; | ||
2682 | ||} | ||
2683 | |.endmacro | ||
2684 | | | ||
2685 | |.macro ins_arithdn, intins, fpcall | ||
2686 | | ins_arithpre | ||
2687 | |.if "intins" ~= "vm_modi" | ||
2688 | | ins_next1 | ||
2689 | |.endif | ||
2690 | | ins_arithcheck_int >5 | ||
2691 | |.if "intins" == "smull" | ||
2692 | | smull CARG1, RC, CARG3, CARG1 | ||
2693 | | cmp RC, CARG1, asr #31 | ||
2694 | | ins_arithfallback bne | ||
2695 | |.elif "intins" == "vm_modi" | ||
2696 | | movs CARG2, CARG3 | ||
2697 | | ins_arithfallback beq | ||
2698 | | bl ->vm_modi | ||
2699 | | mvn CARG2, #~LJ_TISNUM | ||
2700 | |.else | ||
2701 | | intins CARG1, CARG1, CARG3 | ||
2702 | | ins_arithfallback bvs | ||
2703 | |.endif | ||
2704 | |4: | ||
2705 | |.if "intins" == "vm_modi" | ||
2706 | | ins_next1 | ||
2707 | |.endif | ||
2708 | | ins_next2 | ||
2709 | | strd CARG12, [BASE, RA] | ||
2710 | | ins_next3 | ||
2711 | |5: // FP variant. | ||
2712 | | ins_arithfallback ins_arithcheck_num | ||
2713 | |.if "intins" == "vm_modi" | ||
2714 | | bl fpcall | ||
2715 | |.else | ||
2716 | | bl fpcall | ||
2717 | | ins_next1 | ||
2718 | |.endif | ||
2719 | | b <4 | ||
2720 | |.endmacro | ||
2721 | | | ||
2722 | |.macro ins_arithfp, fpcall | ||
2723 | | ins_arithpre | ||
2724 | | ins_arithfallback ins_arithcheck_num | ||
2725 | |.if "fpcall" == "extern pow" | ||
2726 | | IOS mov RC, BASE | ||
2727 | | bl fpcall | ||
2728 | | IOS mov BASE, RC | ||
2729 | |.else | ||
2730 | | bl fpcall | ||
2731 | |.endif | ||
2732 | | ins_next1 | ||
2733 | | ins_next2 | ||
2734 | | strd CARG12, [BASE, RA] | ||
2735 | | ins_next3 | ||
2736 | |.endmacro | ||
2737 | |||
2738 | case BC_ADDVN: case BC_ADDNV: case BC_ADDVV: | ||
2739 | | ins_arithdn adds, extern __aeabi_dadd | ||
2740 | break; | ||
2741 | case BC_SUBVN: case BC_SUBNV: case BC_SUBVV: | ||
2742 | | ins_arithdn subs, extern __aeabi_dsub | ||
2743 | break; | ||
2744 | case BC_MULVN: case BC_MULNV: case BC_MULVV: | ||
2745 | | ins_arithdn smull, extern __aeabi_dmul | ||
2746 | break; | ||
2747 | case BC_DIVVN: case BC_DIVNV: case BC_DIVVV: | ||
2748 | | ins_arithfp extern __aeabi_ddiv | ||
2749 | break; | ||
2750 | case BC_MODVN: case BC_MODNV: case BC_MODVV: | ||
2751 | | ins_arithdn vm_modi, ->vm_mod | ||
2752 | break; | ||
2753 | case BC_POW: | ||
2754 | | // NYI: (partial) integer arithmetic. | ||
2755 | | ins_arithfp extern pow | ||
2756 | break; | ||
2757 | |||
2758 | case BC_CAT: | ||
2759 | | decode_RB8 RC, INS | ||
2760 | | decode_RC8 RB, INS | ||
2761 | | // RA = dst*8, RC = src_start*8, RB = src_end*8 (note: RB/RC swapped!) | ||
2762 | | sub CARG3, RB, RC | ||
2763 | | str BASE, L->base | ||
2764 | | add CARG2, BASE, RB | ||
2765 | |->BC_CAT_Z: | ||
2766 | | // RA = dst*8, RC = src_start*8, CARG2 = top-1 | ||
2767 | | mov CARG1, L | ||
2768 | | str PC, SAVE_PC | ||
2769 | | lsr CARG3, CARG3, #3 | ||
2770 | | bl extern lj_meta_cat // (lua_State *L, TValue *top, int left) | ||
2771 | | // Returns NULL (finished) or TValue * (metamethod). | ||
2772 | | ldr BASE, L->base | ||
2773 | | cmp CRET1, #0 | ||
2774 | | bne ->vmeta_binop | ||
2775 | | ldrd CARG34, [BASE, RC] | ||
2776 | | ins_next1 | ||
2777 | | ins_next2 | ||
2778 | | strd CARG34, [BASE, RA] // Copy result to RA. | ||
2779 | | ins_next3 | ||
2780 | break; | ||
2781 | |||
2782 | /* -- Constant ops ------------------------------------------------------ */ | ||
2783 | |||
2784 | case BC_KSTR: | ||
2785 | | // RA = dst*8, RC = str_const (~) | ||
2786 | | mvn RC, RC | ||
2787 | | ins_next1 | ||
2788 | | ldr CARG1, [KBASE, RC, lsl #2] | ||
2789 | | mvn CARG2, #~LJ_TSTR | ||
2790 | | ins_next2 | ||
2791 | | strd CARG12, [BASE, RA] | ||
2792 | | ins_next3 | ||
2793 | break; | ||
2794 | case BC_KCDATA: | ||
2795 | #if LJ_HASFFI | ||
2796 | | // RA = dst*8, RC = cdata_const (~) | ||
2797 | | mvn RC, RC | ||
2798 | | ins_next1 | ||
2799 | | ldr CARG1, [KBASE, RC, lsl #2] | ||
2800 | | mvn CARG2, #~LJ_TCDATA | ||
2801 | | ins_next2 | ||
2802 | | strd CARG12, [BASE, RA] | ||
2803 | | ins_next3 | ||
2804 | #endif | ||
2805 | break; | ||
2806 | case BC_KSHORT: | ||
2807 | | // RA = dst*8, (RC = int16_literal) | ||
2808 | | mov CARG1, INS, asr #16 // Refetch sign-extended reg. | ||
2809 | | mvn CARG2, #~LJ_TISNUM | ||
2810 | | ins_next1 | ||
2811 | | ins_next2 | ||
2812 | | strd CARG12, [BASE, RA] | ||
2813 | | ins_next3 | ||
2814 | break; | ||
2815 | case BC_KNUM: | ||
2816 | | // RA = dst*8, RC = num_const | ||
2817 | | lsl RC, RC, #3 | ||
2818 | | ins_next1 | ||
2819 | | ldrd CARG12, [KBASE, RC] | ||
2820 | | ins_next2 | ||
2821 | | strd CARG12, [BASE, RA] | ||
2822 | | ins_next3 | ||
2823 | break; | ||
2824 | case BC_KPRI: | ||
2825 | | // RA = dst*8, RC = primitive_type (~) | ||
2826 | | add RA, BASE, RA | ||
2827 | | mvn RC, RC | ||
2828 | | ins_next1 | ||
2829 | | ins_next2 | ||
2830 | | str RC, [RA, #4] | ||
2831 | | ins_next3 | ||
2832 | break; | ||
2833 | case BC_KNIL: | ||
2834 | | // RA = base*8, RC = end | ||
2835 | | add RA, BASE, RA | ||
2836 | | add RC, BASE, RC, lsl #3 | ||
2837 | | mvn CARG1, #~LJ_TNIL | ||
2838 | | str CARG1, [RA, #4] | ||
2839 | | add RA, RA, #8 | ||
2840 | |1: | ||
2841 | | str CARG1, [RA, #4] | ||
2842 | | cmp RA, RC | ||
2843 | | add RA, RA, #8 | ||
2844 | | blt <1 | ||
2845 | | ins_next_ | ||
2846 | break; | ||
2847 | |||
2848 | /* -- Upvalue and function ops ------------------------------------------ */ | ||
2849 | |||
2850 | case BC_UGET: | ||
2851 | | // RA = dst*8, RC = uvnum | ||
2852 | | ldr LFUNC:CARG2, [BASE, FRAME_FUNC] | ||
2853 | | lsl RC, RC, #2 | ||
2854 | | add RC, RC, #offsetof(GCfuncL, uvptr) | ||
2855 | | ldr UPVAL:CARG2, [LFUNC:CARG2, RC] | ||
2856 | | ldr CARG2, UPVAL:CARG2->v | ||
2857 | | ldrd CARG34, [CARG2] | ||
2858 | | ins_next1 | ||
2859 | | ins_next2 | ||
2860 | | strd CARG34, [BASE, RA] | ||
2861 | | ins_next3 | ||
2862 | break; | ||
2863 | case BC_USETV: | ||
2864 | | // RA = uvnum*8, RC = src | ||
2865 | | ldr LFUNC:CARG2, [BASE, FRAME_FUNC] | ||
2866 | | lsr RA, RA, #1 | ||
2867 | | add RA, RA, #offsetof(GCfuncL, uvptr) | ||
2868 | | lsl RC, RC, #3 | ||
2869 | | ldr UPVAL:CARG2, [LFUNC:CARG2, RA] | ||
2870 | | ldrd CARG34, [BASE, RC] | ||
2871 | | ldrb RB, UPVAL:CARG2->marked | ||
2872 | | ldrb RC, UPVAL:CARG2->closed | ||
2873 | | ldr CARG2, UPVAL:CARG2->v | ||
2874 | | tst RB, #LJ_GC_BLACK // isblack(uv) | ||
2875 | | add RB, CARG4, #-LJ_TISGCV | ||
2876 | | cmpne RC, #0 | ||
2877 | | strd CARG34, [CARG2] | ||
2878 | | bne >2 // Upvalue is closed and black? | ||
2879 | |1: | ||
2880 | | ins_next | ||
2881 | | | ||
2882 | |2: // Check if new value is collectable. | ||
2883 | | cmn RB, #-(LJ_TISNUM - LJ_TISGCV) | ||
2884 | | ldrbhi RC, GCOBJ:CARG3->gch.marked | ||
2885 | | bls <1 // tvisgcv(v) | ||
2886 | | sub CARG1, DISPATCH, #-GG_DISP2G | ||
2887 | | tst RC, #LJ_GC_WHITES | ||
2888 | | // Crossed a write barrier. Move the barrier forward. | ||
2889 | if (LJ_TARGET_OSX) { | ||
2890 | | beq <1 | ||
2891 | | mov RC, BASE | ||
2892 | | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv) | ||
2893 | | mov BASE, RC | ||
2894 | } else { | ||
2895 | | blne extern lj_gc_barrieruv // (global_State *g, TValue *tv) | ||
2896 | } | ||
2897 | | b <1 | ||
2898 | break; | ||
2899 | case BC_USETS: | ||
2900 | | // RA = uvnum*8, RC = str_const (~) | ||
2901 | | ldr LFUNC:CARG2, [BASE, FRAME_FUNC] | ||
2902 | | lsr RA, RA, #1 | ||
2903 | | add RA, RA, #offsetof(GCfuncL, uvptr) | ||
2904 | | mvn RC, RC | ||
2905 | | ldr UPVAL:CARG2, [LFUNC:CARG2, RA] | ||
2906 | | ldr STR:CARG3, [KBASE, RC, lsl #2] | ||
2907 | | mvn CARG4, #~LJ_TSTR | ||
2908 | | ldrb RB, UPVAL:CARG2->marked | ||
2909 | | ldr CARG2, UPVAL:CARG2->v | ||
2910 | | ldrb RC, UPVAL:CARG2->closed | ||
2911 | | tst RB, #LJ_GC_BLACK // isblack(uv) | ||
2912 | | ldrb RB, STR:CARG3->marked | ||
2913 | | strd CARG34, [CARG2] | ||
2914 | | bne >2 | ||
2915 | |1: | ||
2916 | | ins_next | ||
2917 | | | ||
2918 | |2: // Check if string is white and ensure upvalue is closed. | ||
2919 | | tst RB, #LJ_GC_WHITES // iswhite(str) | ||
2920 | | cmpne RC, #0 | ||
2921 | | sub CARG1, DISPATCH, #-GG_DISP2G | ||
2922 | | // Crossed a write barrier. Move the barrier forward. | ||
2923 | if (LJ_TARGET_OSX) { | ||
2924 | | beq <1 | ||
2925 | | mov RC, BASE | ||
2926 | | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv) | ||
2927 | | mov BASE, RC | ||
2928 | } else { | ||
2929 | | blne extern lj_gc_barrieruv // (global_State *g, TValue *tv) | ||
2930 | } | ||
2931 | | b <1 | ||
2932 | break; | ||
2933 | case BC_USETN: | ||
2934 | | // RA = uvnum*8, RC = num_const | ||
2935 | | ldr LFUNC:CARG2, [BASE, FRAME_FUNC] | ||
2936 | | lsr RA, RA, #1 | ||
2937 | | add RA, RA, #offsetof(GCfuncL, uvptr) | ||
2938 | | lsl RC, RC, #3 | ||
2939 | | ldr UPVAL:CARG2, [LFUNC:CARG2, RA] | ||
2940 | | ldrd CARG34, [KBASE, RC] | ||
2941 | | ldr CARG2, UPVAL:CARG2->v | ||
2942 | | ins_next1 | ||
2943 | | ins_next2 | ||
2944 | | strd CARG34, [CARG2] | ||
2945 | | ins_next3 | ||
2946 | break; | ||
2947 | case BC_USETP: | ||
2948 | | // RA = uvnum*8, RC = primitive_type (~) | ||
2949 | | ldr LFUNC:CARG2, [BASE, FRAME_FUNC] | ||
2950 | | lsr RA, RA, #1 | ||
2951 | | add RA, RA, #offsetof(GCfuncL, uvptr) | ||
2952 | | ldr UPVAL:CARG2, [LFUNC:CARG2, RA] | ||
2953 | | mvn RC, RC | ||
2954 | | ldr CARG2, UPVAL:CARG2->v | ||
2955 | | ins_next1 | ||
2956 | | ins_next2 | ||
2957 | | str RC, [CARG2, #4] | ||
2958 | | ins_next3 | ||
2959 | break; | ||
2960 | |||
2961 | case BC_UCLO: | ||
2962 | | // RA = level*8, RC = target | ||
2963 | | ldr CARG3, L->openupval | ||
2964 | | add RC, PC, RC, lsl #2 | ||
2965 | | str BASE, L->base | ||
2966 | | cmp CARG3, #0 | ||
2967 | | sub PC, RC, #0x20000 | ||
2968 | | beq >1 | ||
2969 | | mov CARG1, L | ||
2970 | | add CARG2, BASE, RA | ||
2971 | | bl extern lj_func_closeuv // (lua_State *L, TValue *level) | ||
2972 | | ldr BASE, L->base | ||
2973 | |1: | ||
2974 | | ins_next | ||
2975 | break; | ||
2976 | |||
2977 | case BC_FNEW: | ||
2978 | | // RA = dst*8, RC = proto_const (~) (holding function prototype) | ||
2979 | | mvn RC, RC | ||
2980 | | str BASE, L->base | ||
2981 | | ldr CARG2, [KBASE, RC, lsl #2] | ||
2982 | | str PC, SAVE_PC | ||
2983 | | ldr CARG3, [BASE, FRAME_FUNC] | ||
2984 | | mov CARG1, L | ||
2985 | | // (lua_State *L, GCproto *pt, GCfuncL *parent) | ||
2986 | | bl extern lj_func_newL_gc | ||
2987 | | // Returns GCfuncL *. | ||
2988 | | ldr BASE, L->base | ||
2989 | | mvn CARG2, #~LJ_TFUNC | ||
2990 | | ins_next1 | ||
2991 | | ins_next2 | ||
2992 | | strd CARG12, [BASE, RA] | ||
2993 | | ins_next3 | ||
2994 | break; | ||
2995 | |||
2996 | /* -- Table ops --------------------------------------------------------- */ | ||
2997 | |||
2998 | case BC_TNEW: | ||
2999 | case BC_TDUP: | ||
3000 | | // RA = dst*8, RC = (hbits|asize) | tab_const (~) | ||
3001 | if (op == BC_TDUP) { | ||
3002 | | mvn RC, RC | ||
3003 | } | ||
3004 | | ldr CARG3, [DISPATCH, #DISPATCH_GL(gc.total)] | ||
3005 | | ldr CARG4, [DISPATCH, #DISPATCH_GL(gc.threshold)] | ||
3006 | | str BASE, L->base | ||
3007 | | str PC, SAVE_PC | ||
3008 | | cmp CARG3, CARG4 | ||
3009 | | mov CARG1, L | ||
3010 | | bhs >5 | ||
3011 | |1: | ||
3012 | if (op == BC_TNEW) { | ||
3013 | | lsl CARG2, RC, #21 | ||
3014 | | lsr CARG3, RC, #11 | ||
3015 | | asr RC, CARG2, #21 | ||
3016 | | lsr CARG2, CARG2, #21 | ||
3017 | | cmn RC, #1 | ||
3018 | | addeq CARG2, CARG2, #2 | ||
3019 | | bl extern lj_tab_new // (lua_State *L, int32_t asize, uint32_t hbits) | ||
3020 | | // Returns GCtab *. | ||
3021 | } else { | ||
3022 | | ldr CARG2, [KBASE, RC, lsl #2] | ||
3023 | | bl extern lj_tab_dup // (lua_State *L, Table *kt) | ||
3024 | | // Returns GCtab *. | ||
3025 | } | ||
3026 | | ldr BASE, L->base | ||
3027 | | mvn CARG2, #~LJ_TTAB | ||
3028 | | ins_next1 | ||
3029 | | ins_next2 | ||
3030 | | strd CARG12, [BASE, RA] | ||
3031 | | ins_next3 | ||
3032 | |5: | ||
3033 | | bl extern lj_gc_step_fixtop // (lua_State *L) | ||
3034 | | mov CARG1, L | ||
3035 | | b <1 | ||
3036 | break; | ||
3037 | |||
3038 | case BC_GGET: | ||
3039 | | // RA = dst*8, RC = str_const (~) | ||
3040 | case BC_GSET: | ||
3041 | | // RA = dst*8, RC = str_const (~) | ||
3042 | | ldr LFUNC:CARG2, [BASE, FRAME_FUNC] | ||
3043 | | mvn RC, RC | ||
3044 | | ldr TAB:CARG1, LFUNC:CARG2->env | ||
3045 | | ldr STR:RC, [KBASE, RC, lsl #2] | ||
3046 | if (op == BC_GGET) { | ||
3047 | | b ->BC_TGETS_Z | ||
3048 | } else { | ||
3049 | | b ->BC_TSETS_Z | ||
3050 | } | ||
3051 | break; | ||
3052 | |||
3053 | case BC_TGETV: | ||
3054 | | decode_RB8 RB, INS | ||
3055 | | decode_RC8 RC, INS | ||
3056 | | // RA = dst*8, RB = table*8, RC = key*8 | ||
3057 | | ldrd TAB:CARG12, [BASE, RB] | ||
3058 | | ldrd CARG34, [BASE, RC] | ||
3059 | | checktab CARG2, ->vmeta_tgetv // STALL: load CARG12. | ||
3060 | | checktp CARG4, LJ_TISNUM // Integer key? | ||
3061 | | ldreq CARG4, TAB:CARG1->array | ||
3062 | | ldreq CARG2, TAB:CARG1->asize | ||
3063 | | bne >9 | ||
3064 | | | ||
3065 | | add CARG4, CARG4, CARG3, lsl #3 | ||
3066 | | cmp CARG3, CARG2 // In array part? | ||
3067 | | ldrdlo CARG34, [CARG4] | ||
3068 | | bhs ->vmeta_tgetv | ||
3069 | | ins_next1 // Overwrites RB! | ||
3070 | | checktp CARG4, LJ_TNIL | ||
3071 | | beq >5 | ||
3072 | |1: | ||
3073 | | ins_next2 | ||
3074 | | strd CARG34, [BASE, RA] | ||
3075 | | ins_next3 | ||
3076 | | | ||
3077 | |5: // Check for __index if table value is nil. | ||
3078 | | ldr TAB:CARG2, TAB:CARG1->metatable | ||
3079 | | cmp TAB:CARG2, #0 | ||
3080 | | beq <1 // No metatable: done. | ||
3081 | | ldrb CARG2, TAB:CARG2->nomm | ||
3082 | | tst CARG2, #1<<MM_index | ||
3083 | | bne <1 // 'no __index' flag set: done. | ||
3084 | | decode_RB8 RB, INS // Restore RB. | ||
3085 | | b ->vmeta_tgetv | ||
3086 | | | ||
3087 | |9: | ||
3088 | | checktp CARG4, LJ_TSTR // String key? | ||
3089 | | moveq STR:RC, CARG3 | ||
3090 | | beq ->BC_TGETS_Z | ||
3091 | | b ->vmeta_tgetv | ||
3092 | break; | ||
3093 | case BC_TGETS: | ||
3094 | | decode_RB8 RB, INS | ||
3095 | | and RC, RC, #255 | ||
3096 | | // RA = dst*8, RB = table*8, RC = str_const (~) | ||
3097 | | ldrd CARG12, [BASE, RB] | ||
3098 | | mvn RC, RC | ||
3099 | | ldr STR:RC, [KBASE, RC, lsl #2] // STALL: early RC. | ||
3100 | | checktab CARG2, ->vmeta_tgets1 | ||
3101 | |->BC_TGETS_Z: | ||
3102 | | // (TAB:RB =) TAB:CARG1 = GCtab *, STR:RC = GCstr *, RA = dst*8 | ||
3103 | | ldr CARG3, TAB:CARG1->hmask | ||
3104 | | ldr CARG4, STR:RC->hash | ||
3105 | | ldr NODE:INS, TAB:CARG1->node | ||
3106 | | mov TAB:RB, TAB:CARG1 | ||
3107 | | and CARG3, CARG3, CARG4 // idx = str->hash & tab->hmask | ||
3108 | | add CARG3, CARG3, CARG3, lsl #1 | ||
3109 | | add NODE:INS, NODE:INS, CARG3, lsl #3 // node = tab->node + idx*3*8 | ||
3110 | |1: | ||
3111 | | ldrd CARG12, NODE:INS->key // STALL: early NODE:INS. | ||
3112 | | ldrd CARG34, NODE:INS->val | ||
3113 | | ldr NODE:INS, NODE:INS->next | ||
3114 | | cmp CARG1, STR:RC | ||
3115 | | checktpeq CARG2, LJ_TSTR | ||
3116 | | bne >4 | ||
3117 | | checktp CARG4, LJ_TNIL | ||
3118 | | beq >5 | ||
3119 | |3: | ||
3120 | | ins_next1 | ||
3121 | | ins_next2 | ||
3122 | | strd CARG34, [BASE, RA] | ||
3123 | | ins_next3 | ||
3124 | | | ||
3125 | |4: // Follow hash chain. | ||
3126 | | cmp NODE:INS, #0 | ||
3127 | | bne <1 | ||
3128 | | // End of hash chain: key not found, nil result. | ||
3129 | | | ||
3130 | |5: // Check for __index if table value is nil. | ||
3131 | | ldr TAB:CARG1, TAB:RB->metatable | ||
3132 | | mov CARG3, #0 // Optional clear of undef. value (during load stall). | ||
3133 | | mvn CARG4, #~LJ_TNIL | ||
3134 | | cmp TAB:CARG1, #0 | ||
3135 | | beq <3 // No metatable: done. | ||
3136 | | ldrb CARG2, TAB:CARG1->nomm | ||
3137 | | tst CARG2, #1<<MM_index | ||
3138 | | bne <3 // 'no __index' flag set: done. | ||
3139 | | b ->vmeta_tgets | ||
3140 | break; | ||
3141 | case BC_TGETB: | ||
3142 | | decode_RB8 RB, INS | ||
3143 | | and RC, RC, #255 | ||
3144 | | // RA = dst*8, RB = table*8, RC = index | ||
3145 | | ldrd CARG12, [BASE, RB] | ||
3146 | | checktab CARG2, ->vmeta_tgetb // STALL: load CARG12. | ||
3147 | | ldr CARG3, TAB:CARG1->asize | ||
3148 | | ldr CARG4, TAB:CARG1->array | ||
3149 | | lsl CARG2, RC, #3 | ||
3150 | | cmp RC, CARG3 | ||
3151 | | ldrdlo CARG34, [CARG4, CARG2] | ||
3152 | | bhs ->vmeta_tgetb | ||
3153 | | ins_next1 // Overwrites RB! | ||
3154 | | checktp CARG4, LJ_TNIL | ||
3155 | | beq >5 | ||
3156 | |1: | ||
3157 | | ins_next2 | ||
3158 | | strd CARG34, [BASE, RA] | ||
3159 | | ins_next3 | ||
3160 | | | ||
3161 | |5: // Check for __index if table value is nil. | ||
3162 | | ldr TAB:CARG2, TAB:CARG1->metatable | ||
3163 | | cmp TAB:CARG2, #0 | ||
3164 | | beq <1 // No metatable: done. | ||
3165 | | ldrb CARG2, TAB:CARG2->nomm | ||
3166 | | tst CARG2, #1<<MM_index | ||
3167 | | bne <1 // 'no __index' flag set: done. | ||
3168 | | b ->vmeta_tgetb | ||
3169 | break; | ||
3170 | |||
3171 | case BC_TSETV: | ||
3172 | | decode_RB8 RB, INS | ||
3173 | | decode_RC8 RC, INS | ||
3174 | | // RA = src*8, RB = table*8, RC = key*8 | ||
3175 | | ldrd TAB:CARG12, [BASE, RB] | ||
3176 | | ldrd CARG34, [BASE, RC] | ||
3177 | | checktab CARG2, ->vmeta_tsetv // STALL: load CARG12. | ||
3178 | | checktp CARG4, LJ_TISNUM // Integer key? | ||
3179 | | ldreq CARG2, TAB:CARG1->array | ||
3180 | | ldreq CARG4, TAB:CARG1->asize | ||
3181 | | bne >9 | ||
3182 | | | ||
3183 | | add CARG2, CARG2, CARG3, lsl #3 | ||
3184 | | cmp CARG3, CARG4 // In array part? | ||
3185 | | ldrlo INS, [CARG2, #4] | ||
3186 | | bhs ->vmeta_tsetv | ||
3187 | | ins_next1 // Overwrites RB! | ||
3188 | | checktp INS, LJ_TNIL | ||
3189 | | ldrb INS, TAB:CARG1->marked | ||
3190 | | ldrd CARG34, [BASE, RA] | ||
3191 | | beq >5 | ||
3192 | |1: | ||
3193 | | tst INS, #LJ_GC_BLACK // isblack(table) | ||
3194 | | strd CARG34, [CARG2] | ||
3195 | | bne >7 | ||
3196 | |2: | ||
3197 | | ins_next2 | ||
3198 | | ins_next3 | ||
3199 | | | ||
3200 | |5: // Check for __newindex if previous value is nil. | ||
3201 | | ldr TAB:RA, TAB:CARG1->metatable | ||
3202 | | cmp TAB:RA, #0 | ||
3203 | | beq <1 // No metatable: done. | ||
3204 | | ldrb RA, TAB:RA->nomm | ||
3205 | | tst RA, #1<<MM_newindex | ||
3206 | | bne <1 // 'no __newindex' flag set: done. | ||
3207 | | ldr INS, [PC, #-4] // Restore RA and RB. | ||
3208 | | decode_RB8 RB, INS | ||
3209 | | decode_RA8 RA, INS | ||
3210 | | b ->vmeta_tsetv | ||
3211 | | | ||
3212 | |7: // Possible table write barrier for the value. Skip valiswhite check. | ||
3213 | | barrierback TAB:CARG1, INS, CARG3 | ||
3214 | | b <2 | ||
3215 | | | ||
3216 | |9: | ||
3217 | | checktp CARG4, LJ_TSTR // String key? | ||
3218 | | moveq STR:RC, CARG3 | ||
3219 | | beq ->BC_TSETS_Z | ||
3220 | | b ->vmeta_tsetv | ||
3221 | break; | ||
3222 | case BC_TSETS: | ||
3223 | | decode_RB8 RB, INS | ||
3224 | | and RC, RC, #255 | ||
3225 | | // RA = src*8, RB = table*8, RC = str_const (~) | ||
3226 | | ldrd CARG12, [BASE, RB] | ||
3227 | | mvn RC, RC | ||
3228 | | ldr STR:RC, [KBASE, RC, lsl #2] // STALL: early RC. | ||
3229 | | checktab CARG2, ->vmeta_tsets1 | ||
3230 | |->BC_TSETS_Z: | ||
3231 | | // (TAB:RB =) TAB:CARG1 = GCtab *, STR:RC = GCstr *, RA = dst*8 | ||
3232 | | ldr CARG3, TAB:CARG1->hmask | ||
3233 | | ldr CARG4, STR:RC->hash | ||
3234 | | ldr NODE:INS, TAB:CARG1->node | ||
3235 | | mov TAB:RB, TAB:CARG1 | ||
3236 | | and CARG3, CARG3, CARG4 // idx = str->hash & tab->hmask | ||
3237 | | add CARG3, CARG3, CARG3, lsl #1 | ||
3238 | | mov CARG4, #0 | ||
3239 | | add NODE:INS, NODE:INS, CARG3, lsl #3 // node = tab->node + idx*3*8 | ||
3240 | | strb CARG4, TAB:RB->nomm // Clear metamethod cache. | ||
3241 | |1: | ||
3242 | | ldrd CARG12, NODE:INS->key | ||
3243 | | ldr CARG4, NODE:INS->val.it | ||
3244 | | ldr NODE:CARG3, NODE:INS->next | ||
3245 | | cmp CARG1, STR:RC | ||
3246 | | checktpeq CARG2, LJ_TSTR | ||
3247 | | bne >5 | ||
3248 | | ldrb CARG2, TAB:RB->marked | ||
3249 | | checktp CARG4, LJ_TNIL // Key found, but nil value? | ||
3250 | | ldrd CARG34, [BASE, RA] | ||
3251 | | beq >4 | ||
3252 | |2: | ||
3253 | | tst CARG2, #LJ_GC_BLACK // isblack(table) | ||
3254 | | strd CARG34, NODE:INS->val | ||
3255 | | bne >7 | ||
3256 | |3: | ||
3257 | | ins_next | ||
3258 | | | ||
3259 | |4: // Check for __newindex if previous value is nil. | ||
3260 | | ldr TAB:CARG1, TAB:RB->metatable | ||
3261 | | cmp TAB:CARG1, #0 | ||
3262 | | beq <2 // No metatable: done. | ||
3263 | | ldrb CARG1, TAB:CARG1->nomm | ||
3264 | | tst CARG1, #1<<MM_newindex | ||
3265 | | bne <2 // 'no __newindex' flag set: done. | ||
3266 | | b ->vmeta_tsets | ||
3267 | | | ||
3268 | |5: // Follow hash chain. | ||
3269 | | movs NODE:INS, NODE:CARG3 | ||
3270 | | bne <1 | ||
3271 | | // End of hash chain: key not found, add a new one. | ||
3272 | | | ||
3273 | | // But check for __newindex first. | ||
3274 | | ldr TAB:CARG1, TAB:RB->metatable | ||
3275 | | mov CARG3, TMPDp | ||
3276 | | str PC, SAVE_PC | ||
3277 | | cmp TAB:CARG1, #0 // No metatable: continue. | ||
3278 | | str BASE, L->base | ||
3279 | | ldrbne CARG2, TAB:CARG1->nomm | ||
3280 | | mov CARG1, L | ||
3281 | | beq >6 | ||
3282 | | tst CARG2, #1<<MM_newindex | ||
3283 | | beq ->vmeta_tsets // 'no __newindex' flag NOT set: check. | ||
3284 | |6: | ||
3285 | | mvn CARG4, #~LJ_TSTR | ||
3286 | | str STR:RC, TMPDlo | ||
3287 | | mov CARG2, TAB:RB | ||
3288 | | str CARG4, TMPDhi | ||
3289 | | bl extern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k) | ||
3290 | | // Returns TValue *. | ||
3291 | | ldr BASE, L->base | ||
3292 | | ldrd CARG34, [BASE, RA] | ||
3293 | | strd CARG34, [CRET1] | ||
3294 | | b <3 // No 2nd write barrier needed. | ||
3295 | | | ||
3296 | |7: // Possible table write barrier for the value. Skip valiswhite check. | ||
3297 | | barrierback TAB:RB, CARG2, CARG3 | ||
3298 | | b <3 | ||
3299 | break; | ||
3300 | case BC_TSETB: | ||
3301 | | decode_RB8 RB, INS | ||
3302 | | and RC, RC, #255 | ||
3303 | | // RA = src*8, RB = table*8, RC = index | ||
3304 | | ldrd CARG12, [BASE, RB] | ||
3305 | | checktab CARG2, ->vmeta_tsetb // STALL: load CARG12. | ||
3306 | | ldr CARG3, TAB:CARG1->asize | ||
3307 | | ldr RB, TAB:CARG1->array | ||
3308 | | lsl CARG2, RC, #3 | ||
3309 | | cmp RC, CARG3 | ||
3310 | | ldrdlo CARG34, [CARG2, RB]! | ||
3311 | | bhs ->vmeta_tsetb | ||
3312 | | ins_next1 // Overwrites RB! | ||
3313 | | checktp CARG4, LJ_TNIL | ||
3314 | | ldrb INS, TAB:CARG1->marked | ||
3315 | | ldrd CARG34, [BASE, RA] | ||
3316 | | beq >5 | ||
3317 | |1: | ||
3318 | | tst INS, #LJ_GC_BLACK // isblack(table) | ||
3319 | | strd CARG34, [CARG2] | ||
3320 | | bne >7 | ||
3321 | |2: | ||
3322 | | ins_next2 | ||
3323 | | ins_next3 | ||
3324 | | | ||
3325 | |5: // Check for __newindex if previous value is nil. | ||
3326 | | ldr TAB:RA, TAB:CARG1->metatable | ||
3327 | | cmp TAB:RA, #0 | ||
3328 | | beq <1 // No metatable: done. | ||
3329 | | ldrb RA, TAB:RA->nomm | ||
3330 | | tst RA, #1<<MM_newindex | ||
3331 | | bne <1 // 'no __newindex' flag set: done. | ||
3332 | | ldr INS, [PC, #-4] // Restore INS. | ||
3333 | | decode_RA8 RA, INS | ||
3334 | | b ->vmeta_tsetb | ||
3335 | | | ||
3336 | |7: // Possible table write barrier for the value. Skip valiswhite check. | ||
3337 | | barrierback TAB:CARG1, INS, CARG3 | ||
3338 | | b <2 | ||
3339 | break; | ||
3340 | |||
3341 | case BC_TSETM: | ||
3342 | | // RA = base*8 (table at base-1), RC = num_const (start index) | ||
3343 | | add RA, BASE, RA | ||
3344 | |1: | ||
3345 | | ldr RB, SAVE_MULTRES | ||
3346 | | ldr TAB:CARG2, [RA, #-8] // Guaranteed to be a table. | ||
3347 | | ldr CARG1, [KBASE, RC, lsl #3] // Integer constant is in lo-word. | ||
3348 | | subs RB, RB, #8 | ||
3349 | | ldr CARG4, TAB:CARG2->asize | ||
3350 | | beq >4 // Nothing to copy? | ||
3351 | | add CARG3, CARG1, RB, lsr #3 | ||
3352 | | cmp CARG3, CARG4 | ||
3353 | | ldr CARG4, TAB:CARG2->array | ||
3354 | | add RB, RA, RB | ||
3355 | | bhi >5 | ||
3356 | | add INS, CARG4, CARG1, lsl #3 | ||
3357 | | ldrb CARG1, TAB:CARG2->marked | ||
3358 | |3: // Copy result slots to table. | ||
3359 | | ldrd CARG34, [RA], #8 | ||
3360 | | strd CARG34, [INS], #8 | ||
3361 | | cmp RA, RB | ||
3362 | | blo <3 | ||
3363 | | tst CARG1, #LJ_GC_BLACK // isblack(table) | ||
3364 | | bne >7 | ||
3365 | |4: | ||
3366 | | ins_next | ||
3367 | | | ||
3368 | |5: // Need to resize array part. | ||
3369 | | str BASE, L->base | ||
3370 | | mov CARG1, L | ||
3371 | | str PC, SAVE_PC | ||
3372 | | bl extern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize) | ||
3373 | | // Must not reallocate the stack. | ||
3374 | | IOS ldr BASE, L->base | ||
3375 | | b <1 | ||
3376 | | | ||
3377 | |7: // Possible table write barrier for any value. Skip valiswhite check. | ||
3378 | | barrierback TAB:CARG2, CARG1, CARG3 | ||
3379 | | b <4 | ||
3380 | break; | ||
3381 | |||
3382 | /* -- Calls and vararg handling ----------------------------------------- */ | ||
3383 | |||
3384 | case BC_CALLM: | ||
3385 | | // RA = base*8, (RB = nresults+1,) RC = extra_nargs | ||
3386 | | ldr CARG1, SAVE_MULTRES | ||
3387 | | decode_RC8 NARGS8:RC, INS | ||
3388 | | add NARGS8:RC, NARGS8:RC, CARG1 | ||
3389 | | b ->BC_CALL_Z | ||
3390 | break; | ||
3391 | case BC_CALL: | ||
3392 | | decode_RC8 NARGS8:RC, INS | ||
3393 | | // RA = base*8, (RB = nresults+1,) RC = (nargs+1)*8 | ||
3394 | |->BC_CALL_Z: | ||
3395 | | mov RB, BASE // Save old BASE for vmeta_call. | ||
3396 | | ldrd CARG34, [BASE, RA]! | ||
3397 | | sub NARGS8:RC, NARGS8:RC, #8 | ||
3398 | | add BASE, BASE, #8 | ||
3399 | | checkfunc CARG4, ->vmeta_call | ||
3400 | | ins_call | ||
3401 | break; | ||
3402 | |||
3403 | case BC_CALLMT: | ||
3404 | | // RA = base*8, (RB = 0,) RC = extra_nargs | ||
3405 | | ldr CARG1, SAVE_MULTRES | ||
3406 | | add NARGS8:RC, CARG1, RC, lsl #3 | ||
3407 | | b ->BC_CALLT1_Z | ||
3408 | break; | ||
3409 | case BC_CALLT: | ||
3410 | | lsl NARGS8:RC, RC, #3 | ||
3411 | | // RA = base*8, (RB = 0,) RC = (nargs+1)*8 | ||
3412 | |->BC_CALLT1_Z: | ||
3413 | | ldrd LFUNC:CARG34, [RA, BASE]! | ||
3414 | | sub NARGS8:RC, NARGS8:RC, #8 | ||
3415 | | add RA, RA, #8 | ||
3416 | | checkfunc CARG4, ->vmeta_callt | ||
3417 | | ldr PC, [BASE, FRAME_PC] | ||
3418 | |->BC_CALLT2_Z: | ||
3419 | | mov RB, #0 | ||
3420 | | ldrb CARG4, LFUNC:CARG3->ffid | ||
3421 | | tst PC, #FRAME_TYPE | ||
3422 | | bne >7 | ||
3423 | |1: | ||
3424 | | str LFUNC:CARG3, [BASE, FRAME_FUNC] // Copy function down, but keep PC. | ||
3425 | | cmp NARGS8:RC, #0 | ||
3426 | | beq >3 | ||
3427 | |2: | ||
3428 | | ldrd CARG12, [RA, RB] | ||
3429 | | add INS, RB, #8 | ||
3430 | | cmp INS, NARGS8:RC | ||
3431 | | strd CARG12, [BASE, RB] | ||
3432 | | mov RB, INS | ||
3433 | | bne <2 | ||
3434 | |3: | ||
3435 | | cmp CARG4, #1 // (> FF_C) Calling a fast function? | ||
3436 | | bhi >5 | ||
3437 | |4: | ||
3438 | | ins_callt | ||
3439 | | | ||
3440 | |5: // Tailcall to a fast function with a Lua frame below. | ||
3441 | | ldr INS, [PC, #-4] | ||
3442 | | decode_RA8 RA, INS | ||
3443 | | sub CARG1, BASE, RA | ||
3444 | | ldr LFUNC:CARG1, [CARG1, #-16] | ||
3445 | | ldr CARG1, LFUNC:CARG1->field_pc | ||
3446 | | ldr KBASE, [CARG1, #PC2PROTO(k)] | ||
3447 | | b <4 | ||
3448 | | | ||
3449 | |7: // Tailcall from a vararg function. | ||
3450 | | eor PC, PC, #FRAME_VARG | ||
3451 | | tst PC, #FRAME_TYPEP // Vararg frame below? | ||
3452 | | movne CARG4, #0 // Clear ffid if no Lua function below. | ||
3453 | | bne <1 | ||
3454 | | sub BASE, BASE, PC | ||
3455 | | ldr PC, [BASE, FRAME_PC] | ||
3456 | | tst PC, #FRAME_TYPE | ||
3457 | | movne CARG4, #0 // Clear ffid if no Lua function below. | ||
3458 | | b <1 | ||
3459 | break; | ||
3460 | |||
3461 | case BC_ITERC: | ||
3462 | | // RA = base*8, (RB = nresults+1, RC = nargs+1 (2+1)) | ||
3463 | | add RA, BASE, RA | ||
3464 | | mov RB, BASE // Save old BASE for vmeta_call. | ||
3465 | | ldrd CARG34, [RA, #-16] | ||
3466 | | ldrd CARG12, [RA, #-8] | ||
3467 | | add BASE, RA, #8 | ||
3468 | | strd CARG34, [RA, #8] // Copy state. | ||
3469 | | strd CARG12, [RA, #16] // Copy control var. | ||
3470 | | // STALL: locked CARG34. | ||
3471 | | ldrd LFUNC:CARG34, [RA, #-24] | ||
3472 | | mov NARGS8:RC, #16 // Iterators get 2 arguments. | ||
3473 | | // STALL: load CARG34. | ||
3474 | | strd LFUNC:CARG34, [RA] // Copy callable. | ||
3475 | | checkfunc CARG4, ->vmeta_call | ||
3476 | | ins_call | ||
3477 | break; | ||
3478 | |||
3479 | case BC_ITERN: | ||
3480 | | // RA = base*8, (RB = nresults+1, RC = nargs+1 (2+1)) | ||
3481 | #if LJ_HASJIT | ||
3482 | | // NYI: add hotloop, record BC_ITERN. | ||
3483 | #endif | ||
3484 | | add RA, BASE, RA | ||
3485 | | ldr TAB:RB, [RA, #-16] | ||
3486 | | ldr CARG1, [RA, #-8] // Get index from control var. | ||
3487 | | ldr INS, TAB:RB->asize | ||
3488 | | ldr CARG2, TAB:RB->array | ||
3489 | | add PC, PC, #4 | ||
3490 | |1: // Traverse array part. | ||
3491 | | subs RC, CARG1, INS | ||
3492 | | add CARG3, CARG2, CARG1, lsl #3 | ||
3493 | | bhs >5 // Index points after array part? | ||
3494 | | ldrd CARG34, [CARG3] | ||
3495 | | checktp CARG4, LJ_TNIL | ||
3496 | | addeq CARG1, CARG1, #1 // Skip holes in array part. | ||
3497 | | beq <1 | ||
3498 | | ldrh RC, [PC, #-2] | ||
3499 | | mvn CARG2, #~LJ_TISNUM | ||
3500 | | strd CARG34, [RA, #8] | ||
3501 | | add RC, PC, RC, lsl #2 | ||
3502 | | add RB, CARG1, #1 | ||
3503 | | strd CARG12, [RA] | ||
3504 | | sub PC, RC, #0x20000 | ||
3505 | | str RB, [RA, #-8] // Update control var. | ||
3506 | |3: | ||
3507 | | ins_next | ||
3508 | | | ||
3509 | |5: // Traverse hash part. | ||
3510 | | ldr CARG4, TAB:RB->hmask | ||
3511 | | ldr NODE:RB, TAB:RB->node | ||
3512 | |6: | ||
3513 | | add CARG1, RC, RC, lsl #1 | ||
3514 | | cmp RC, CARG4 // End of iteration? Branch to ITERL+1. | ||
3515 | | add NODE:CARG3, NODE:RB, CARG1, lsl #3 // node = tab->node + idx*3*8 | ||
3516 | | bhi <3 | ||
3517 | | ldrd CARG12, NODE:CARG3->val | ||
3518 | | checktp CARG2, LJ_TNIL | ||
3519 | | add RC, RC, #1 | ||
3520 | | beq <6 // Skip holes in hash part. | ||
3521 | | ldrh RB, [PC, #-2] | ||
3522 | | add RC, RC, INS | ||
3523 | | ldrd CARG34, NODE:CARG3->key | ||
3524 | | str RC, [RA, #-8] // Update control var. | ||
3525 | | strd CARG12, [RA, #8] | ||
3526 | | add RC, PC, RB, lsl #2 | ||
3527 | | sub PC, RC, #0x20000 | ||
3528 | | strd CARG34, [RA] | ||
3529 | | b <3 | ||
3530 | break; | ||
3531 | |||
3532 | case BC_ISNEXT: | ||
3533 | | // RA = base*8, RC = target (points to ITERN) | ||
3534 | | add RA, BASE, RA | ||
3535 | | add RC, PC, RC, lsl #2 | ||
3536 | | ldrd CFUNC:CARG12, [RA, #-24] | ||
3537 | | ldr CARG3, [RA, #-12] | ||
3538 | | ldr CARG4, [RA, #-4] | ||
3539 | | checktp CARG2, LJ_TFUNC | ||
3540 | | ldrbeq CARG1, CFUNC:CARG1->ffid | ||
3541 | | checktpeq CARG3, LJ_TTAB | ||
3542 | | checktpeq CARG4, LJ_TNIL | ||
3543 | | cmpeq CARG1, #FF_next_N | ||
3544 | | subeq PC, RC, #0x20000 | ||
3545 | | bne >5 | ||
3546 | | ins_next1 | ||
3547 | | ins_next2 | ||
3548 | | mov CARG1, #0 | ||
3549 | | str CARG1, [RA, #-8] // Initialize control var. | ||
3550 | |1: | ||
3551 | | ins_next3 | ||
3552 | |5: // Despecialize bytecode if any of the checks fail. | ||
3553 | | mov CARG1, #BC_JMP | ||
3554 | | mov OP, #BC_ITERC | ||
3555 | | strb CARG1, [PC, #-4] | ||
3556 | | sub PC, RC, #0x20000 | ||
3557 | | strb OP, [PC] // Subsumes ins_next1. | ||
3558 | | ins_next2 | ||
3559 | | b <1 | ||
3560 | break; | ||
3561 | |||
3562 | case BC_VARG: | ||
3563 | | decode_RB8 RB, INS | ||
3564 | | decode_RC8 RC, INS | ||
3565 | | // RA = base*8, RB = (nresults+1)*8, RC = numparams*8 | ||
3566 | | ldr CARG1, [BASE, FRAME_PC] | ||
3567 | | add RC, BASE, RC | ||
3568 | | add RA, BASE, RA | ||
3569 | | add RC, RC, #FRAME_VARG | ||
3570 | | add CARG4, RA, RB | ||
3571 | | sub CARG3, BASE, #8 // CARG3 = vtop | ||
3572 | | sub RC, RC, CARG1 // RC = vbase | ||
3573 | | // Note: RC may now be even _above_ BASE if nargs was < numparams. | ||
3574 | | cmp RB, #0 | ||
3575 | | sub CARG1, CARG3, RC | ||
3576 | | beq >5 // Copy all varargs? | ||
3577 | | sub CARG4, CARG4, #16 | ||
3578 | |1: // Copy vararg slots to destination slots. | ||
3579 | | cmp RC, CARG3 | ||
3580 | | ldrdlo CARG12, [RC], #8 | ||
3581 | | mvnhs CARG2, #~LJ_TNIL | ||
3582 | | cmp RA, CARG4 | ||
3583 | | strd CARG12, [RA], #8 | ||
3584 | | blo <1 | ||
3585 | |2: | ||
3586 | | ins_next | ||
3587 | | | ||
3588 | |5: // Copy all varargs. | ||
3589 | | ldr CARG4, L->maxstack | ||
3590 | | cmp CARG1, #0 | ||
3591 | | movle RB, #8 // MULTRES = (0+1)*8 | ||
3592 | | addgt RB, CARG1, #8 | ||
3593 | | add CARG2, RA, CARG1 | ||
3594 | | str RB, SAVE_MULTRES | ||
3595 | | ble <2 | ||
3596 | | cmp CARG2, CARG4 | ||
3597 | | bhi >7 | ||
3598 | |6: | ||
3599 | | ldrd CARG12, [RC], #8 | ||
3600 | | strd CARG12, [RA], #8 | ||
3601 | | cmp RC, CARG3 | ||
3602 | | blo <6 | ||
3603 | | b <2 | ||
3604 | | | ||
3605 | |7: // Grow stack for varargs. | ||
3606 | | lsr CARG2, CARG1, #3 | ||
3607 | | str RA, L->top | ||
3608 | | mov CARG1, L | ||
3609 | | str BASE, L->base | ||
3610 | | sub RC, RC, BASE // Need delta, because BASE may change. | ||
3611 | | str PC, SAVE_PC | ||
3612 | | sub RA, RA, BASE | ||
3613 | | bl extern lj_state_growstack // (lua_State *L, int n) | ||
3614 | | ldr BASE, L->base | ||
3615 | | add RA, BASE, RA | ||
3616 | | add RC, BASE, RC | ||
3617 | | sub CARG3, BASE, #8 | ||
3618 | | b <6 | ||
3619 | break; | ||
3620 | |||
3621 | /* -- Returns ----------------------------------------------------------- */ | ||
3622 | |||
3623 | case BC_RETM: | ||
3624 | | // RA = results*8, RC = extra results | ||
3625 | | ldr CARG1, SAVE_MULTRES | ||
3626 | | ldr PC, [BASE, FRAME_PC] | ||
3627 | | add RA, BASE, RA | ||
3628 | | add RC, CARG1, RC, lsl #3 | ||
3629 | | b ->BC_RETM_Z | ||
3630 | break; | ||
3631 | |||
3632 | case BC_RET: | ||
3633 | | // RA = results*8, RC = nresults+1 | ||
3634 | | ldr PC, [BASE, FRAME_PC] | ||
3635 | | lsl RC, RC, #3 | ||
3636 | | add RA, BASE, RA | ||
3637 | |->BC_RETM_Z: | ||
3638 | | str RC, SAVE_MULTRES | ||
3639 | |1: | ||
3640 | | ands CARG1, PC, #FRAME_TYPE | ||
3641 | | eor CARG2, PC, #FRAME_VARG | ||
3642 | | bne ->BC_RETV2_Z | ||
3643 | | | ||
3644 | |->BC_RET_Z: | ||
3645 | | // BASE = base, RA = resultptr, RC = (nresults+1)*8, PC = return | ||
3646 | | ldr INS, [PC, #-4] | ||
3647 | | subs CARG4, RC, #8 | ||
3648 | | sub CARG3, BASE, #8 | ||
3649 | | beq >3 | ||
3650 | |2: | ||
3651 | | ldrd CARG12, [RA], #8 | ||
3652 | | add BASE, BASE, #8 | ||
3653 | | subs CARG4, CARG4, #8 | ||
3654 | | strd CARG12, [BASE, #-16] | ||
3655 | | bne <2 | ||
3656 | |3: | ||
3657 | | decode_RA8 RA, INS | ||
3658 | | sub CARG4, CARG3, RA | ||
3659 | | decode_RB8 RB, INS | ||
3660 | | ldr LFUNC:CARG1, [CARG4, FRAME_FUNC] | ||
3661 | |5: | ||
3662 | | cmp RB, RC // More results expected? | ||
3663 | | bhi >6 | ||
3664 | | mov BASE, CARG4 | ||
3665 | | ldr CARG2, LFUNC:CARG1->field_pc | ||
3666 | | ins_next1 | ||
3667 | | ins_next2 | ||
3668 | | ldr KBASE, [CARG2, #PC2PROTO(k)] | ||
3669 | | ins_next3 | ||
3670 | | | ||
3671 | |6: // Fill up results with nil. | ||
3672 | | mvn CARG2, #~LJ_TNIL | ||
3673 | | add BASE, BASE, #8 | ||
3674 | | add RC, RC, #8 | ||
3675 | | str CARG2, [BASE, #-12] | ||
3676 | | b <5 | ||
3677 | | | ||
3678 | |->BC_RETV1_Z: // Non-standard return case. | ||
3679 | | add RA, BASE, RA | ||
3680 | |->BC_RETV2_Z: | ||
3681 | | tst CARG2, #FRAME_TYPEP | ||
3682 | | bne ->vm_return | ||
3683 | | // Return from vararg function: relocate BASE down. | ||
3684 | | sub BASE, BASE, CARG2 | ||
3685 | | ldr PC, [BASE, FRAME_PC] | ||
3686 | | b <1 | ||
3687 | break; | ||
3688 | |||
3689 | case BC_RET0: case BC_RET1: | ||
3690 | | // RA = results*8, RC = nresults+1 | ||
3691 | | ldr PC, [BASE, FRAME_PC] | ||
3692 | | lsl RC, RC, #3 | ||
3693 | | str RC, SAVE_MULTRES | ||
3694 | | ands CARG1, PC, #FRAME_TYPE | ||
3695 | | eor CARG2, PC, #FRAME_VARG | ||
3696 | | ldreq INS, [PC, #-4] | ||
3697 | | bne ->BC_RETV1_Z | ||
3698 | if (op == BC_RET1) { | ||
3699 | | ldrd CARG12, [BASE, RA] | ||
3700 | } | ||
3701 | | sub CARG4, BASE, #8 | ||
3702 | | decode_RA8 RA, INS | ||
3703 | if (op == BC_RET1) { | ||
3704 | | strd CARG12, [CARG4] | ||
3705 | } | ||
3706 | | sub BASE, CARG4, RA | ||
3707 | | decode_RB8 RB, INS | ||
3708 | | ldr LFUNC:CARG1, [BASE, FRAME_FUNC] | ||
3709 | |5: | ||
3710 | | cmp RB, RC | ||
3711 | | bhi >6 | ||
3712 | | ldr CARG2, LFUNC:CARG1->field_pc | ||
3713 | | ins_next1 | ||
3714 | | ins_next2 | ||
3715 | | ldr KBASE, [CARG2, #PC2PROTO(k)] | ||
3716 | | ins_next3 | ||
3717 | | | ||
3718 | |6: // Fill up results with nil. | ||
3719 | | sub CARG2, CARG4, #4 | ||
3720 | | mvn CARG3, #~LJ_TNIL | ||
3721 | | str CARG3, [CARG2, RC] | ||
3722 | | add RC, RC, #8 | ||
3723 | | b <5 | ||
3724 | break; | ||
3725 | |||
3726 | /* -- Loops and branches ------------------------------------------------ */ | ||
3727 | |||
3728 | |.define FOR_IDX, [RA]; .define FOR_TIDX, [RA, #4] | ||
3729 | |.define FOR_STOP, [RA, #8]; .define FOR_TSTOP, [RA, #12] | ||
3730 | |.define FOR_STEP, [RA, #16]; .define FOR_TSTEP, [RA, #20] | ||
3731 | |.define FOR_EXT, [RA, #24]; .define FOR_TEXT, [RA, #28] | ||
3732 | |||
3733 | case BC_FORL: | ||
3734 | #if LJ_HASJIT | ||
3735 | | hotloop | ||
3736 | #endif | ||
3737 | | // Fall through. Assumes BC_IFORL follows. | ||
3738 | break; | ||
3739 | |||
3740 | case BC_JFORI: | ||
3741 | case BC_JFORL: | ||
3742 | #if !LJ_HASJIT | ||
3743 | break; | ||
3744 | #endif | ||
3745 | case BC_FORI: | ||
3746 | case BC_IFORL: | ||
3747 | | // RA = base*8, RC = target (after end of loop or start of loop) | ||
3748 | vk = (op == BC_IFORL || op == BC_JFORL); | ||
3749 | | ldrd CARG12, [RA, BASE]! | ||
3750 | if (op != BC_JFORL) { | ||
3751 | | add RC, PC, RC, lsl #2 | ||
3752 | } | ||
3753 | if (!vk) { | ||
3754 | | ldrd CARG34, FOR_STOP | ||
3755 | | checktp CARG2, LJ_TISNUM | ||
3756 | | ldr RB, FOR_TSTEP | ||
3757 | | bne >5 | ||
3758 | | checktp CARG4, LJ_TISNUM | ||
3759 | | ldr CARG4, FOR_STEP | ||
3760 | | checktpeq RB, LJ_TISNUM | ||
3761 | | bne ->vmeta_for | ||
3762 | | cmp CARG4, #0 | ||
3763 | | blt >4 | ||
3764 | | cmp CARG1, CARG3 | ||
3765 | } else { | ||
3766 | | ldrd CARG34, FOR_STEP | ||
3767 | | checktp CARG2, LJ_TISNUM | ||
3768 | | bne >5 | ||
3769 | | adds CARG1, CARG1, CARG3 | ||
3770 | | ldr CARG4, FOR_STOP | ||
3771 | if (op == BC_IFORL) { | ||
3772 | | addvs RC, PC, #0x20000 // Overflow: prevent branch. | ||
3773 | } else { | ||
3774 | | bvs >2 // Overflow: do not enter mcode. | ||
3775 | } | ||
3776 | | cmp CARG3, #0 | ||
3777 | | blt >4 | ||
3778 | | cmp CARG1, CARG4 | ||
3779 | } | ||
3780 | |1: | ||
3781 | if (op == BC_FORI) { | ||
3782 | | subgt PC, RC, #0x20000 | ||
3783 | } else if (op == BC_JFORI) { | ||
3784 | | sub PC, RC, #0x20000 | ||
3785 | | ldrhle RC, [PC, #-2] | ||
3786 | } else if (op == BC_IFORL) { | ||
3787 | | suble PC, RC, #0x20000 | ||
3788 | } | ||
3789 | if (vk) { | ||
3790 | | strd CARG12, FOR_IDX | ||
3791 | } | ||
3792 | |2: | ||
3793 | | ins_next1 | ||
3794 | | ins_next2 | ||
3795 | | strd CARG12, FOR_EXT | ||
3796 | if (op == BC_JFORI || op == BC_JFORL) { | ||
3797 | | ble =>BC_JLOOP | ||
3798 | } | ||
3799 | |3: | ||
3800 | | ins_next3 | ||
3801 | | | ||
3802 | |4: // Invert check for negative step. | ||
3803 | if (!vk) { | ||
3804 | | cmp CARG3, CARG1 | ||
3805 | } else { | ||
3806 | | cmp CARG4, CARG1 | ||
3807 | } | ||
3808 | | b <1 | ||
3809 | | | ||
3810 | |5: // FP loop. | ||
3811 | if (!vk) { | ||
3812 | | cmnlo CARG4, #-LJ_TISNUM | ||
3813 | | cmnlo RB, #-LJ_TISNUM | ||
3814 | | bhs ->vmeta_for | ||
3815 | | cmp RB, #0 | ||
3816 | | strd CARG12, FOR_IDX | ||
3817 | | strd CARG12, FOR_EXT | ||
3818 | | blt >8 | ||
3819 | } else { | ||
3820 | | cmp CARG4, #0 | ||
3821 | | blt >8 | ||
3822 | | bl extern __aeabi_dadd | ||
3823 | | strd CARG12, FOR_IDX | ||
3824 | | ldrd CARG34, FOR_STOP | ||
3825 | | strd CARG12, FOR_EXT | ||
3826 | } | ||
3827 | |6: | ||
3828 | | bl extern __aeabi_cdcmple | ||
3829 | if (op == BC_FORI) { | ||
3830 | | subhi PC, RC, #0x20000 | ||
3831 | } else if (op == BC_JFORI) { | ||
3832 | | sub PC, RC, #0x20000 | ||
3833 | | ldrhls RC, [PC, #-2] | ||
3834 | | bls =>BC_JLOOP | ||
3835 | } else if (op == BC_IFORL) { | ||
3836 | | subls PC, RC, #0x20000 | ||
3837 | } else { | ||
3838 | | bls =>BC_JLOOP | ||
3839 | } | ||
3840 | | ins_next1 | ||
3841 | | ins_next2 | ||
3842 | | b <3 | ||
3843 | | | ||
3844 | |8: // Invert check for negative step. | ||
3845 | if (vk) { | ||
3846 | | bl extern __aeabi_dadd | ||
3847 | | strd CARG12, FOR_IDX | ||
3848 | | strd CARG12, FOR_EXT | ||
3849 | } | ||
3850 | | mov CARG3, CARG1 | ||
3851 | | mov CARG4, CARG2 | ||
3852 | | ldrd CARG12, FOR_STOP | ||
3853 | | b <6 | ||
3854 | break; | ||
3855 | |||
3856 | case BC_ITERL: | ||
3857 | #if LJ_HASJIT | ||
3858 | | hotloop | ||
3859 | #endif | ||
3860 | | // Fall through. Assumes BC_IITERL follows. | ||
3861 | break; | ||
3862 | |||
3863 | case BC_JITERL: | ||
3864 | #if !LJ_HASJIT | ||
3865 | break; | ||
3866 | #endif | ||
3867 | case BC_IITERL: | ||
3868 | | // RA = base*8, RC = target | ||
3869 | | ldrd CARG12, [RA, BASE]! | ||
3870 | if (op == BC_JITERL) { | ||
3871 | | cmn CARG2, #-LJ_TNIL // Stop if iterator returned nil. | ||
3872 | | strdne CARG12, [RA, #-8] | ||
3873 | | bne =>BC_JLOOP | ||
3874 | } else { | ||
3875 | | add RC, PC, RC, lsl #2 | ||
3876 | | // STALL: load CARG12. | ||
3877 | | cmn CARG2, #-LJ_TNIL // Stop if iterator returned nil. | ||
3878 | | subne PC, RC, #0x20000 // Otherwise save control var + branch. | ||
3879 | | strdne CARG12, [RA, #-8] | ||
3880 | } | ||
3881 | | ins_next | ||
3882 | break; | ||
3883 | |||
3884 | case BC_LOOP: | ||
3885 | | // RA = base*8, RC = target (loop extent) | ||
3886 | | // Note: RA/RC is only used by trace recorder to determine scope/extent | ||
3887 | | // This opcode does NOT jump, it's only purpose is to detect a hot loop. | ||
3888 | #if LJ_HASJIT | ||
3889 | | hotloop | ||
3890 | #endif | ||
3891 | | // Fall through. Assumes BC_ILOOP follows. | ||
3892 | break; | ||
3893 | |||
3894 | case BC_ILOOP: | ||
3895 | | // RA = base*8, RC = target (loop extent) | ||
3896 | | ins_next | ||
3897 | break; | ||
3898 | |||
3899 | case BC_JLOOP: | ||
3900 | #if LJ_HASJIT | ||
3901 | | // RA = base (ignored), RC = traceno | ||
3902 | | ldr CARG1, [DISPATCH, #DISPATCH_J(trace)] | ||
3903 | | mov CARG2, #0 // Traces on ARM don't store the trace number, so use 0. | ||
3904 | | ldr TRACE:RC, [CARG1, RC, lsl #2] | ||
3905 | | st_vmstate CARG2 | ||
3906 | | ldr RA, TRACE:RC->mcode | ||
3907 | | str BASE, [DISPATCH, #DISPATCH_GL(jit_base)] | ||
3908 | | str L, [DISPATCH, #DISPATCH_GL(jit_L)] | ||
3909 | | bx RA | ||
3910 | #endif | ||
3911 | break; | ||
3912 | |||
3913 | case BC_JMP: | ||
3914 | | // RA = base*8 (only used by trace recorder), RC = target | ||
3915 | | add RC, PC, RC, lsl #2 | ||
3916 | | sub PC, RC, #0x20000 | ||
3917 | | ins_next | ||
3918 | break; | ||
3919 | |||
3920 | /* -- Function headers -------------------------------------------------- */ | ||
3921 | |||
3922 | case BC_FUNCF: | ||
3923 | #if LJ_HASJIT | ||
3924 | | hotcall | ||
3925 | #endif | ||
3926 | case BC_FUNCV: /* NYI: compiled vararg functions. */ | ||
3927 | | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow. | ||
3928 | break; | ||
3929 | |||
3930 | case BC_JFUNCF: | ||
3931 | #if !LJ_HASJIT | ||
3932 | break; | ||
3933 | #endif | ||
3934 | case BC_IFUNCF: | ||
3935 | | // BASE = new base, RA = BASE+framesize*8, CARG3 = LFUNC, RC = nargs*8 | ||
3936 | | ldr CARG1, L->maxstack | ||
3937 | | ldrb CARG2, [PC, #-4+PC2PROTO(numparams)] | ||
3938 | | ldr KBASE, [PC, #-4+PC2PROTO(k)] | ||
3939 | | cmp RA, CARG1 | ||
3940 | | bhi ->vm_growstack_l | ||
3941 | if (op != BC_JFUNCF) { | ||
3942 | | ins_next1 | ||
3943 | | ins_next2 | ||
3944 | } | ||
3945 | |2: | ||
3946 | | cmp NARGS8:RC, CARG2, lsl #3 // Check for missing parameters. | ||
3947 | | mvn CARG4, #~LJ_TNIL | ||
3948 | | ble >3 | ||
3949 | if (op == BC_JFUNCF) { | ||
3950 | | decode_RD RC, INS | ||
3951 | | b =>BC_JLOOP | ||
3952 | } else { | ||
3953 | | ins_next3 | ||
3954 | } | ||
3955 | | | ||
3956 | |3: // Clear missing parameters. | ||
3957 | | strd CARG34, [BASE, NARGS8:RC] | ||
3958 | | add NARGS8:RC, NARGS8:RC, #8 | ||
3959 | | b <2 | ||
3960 | break; | ||
3961 | |||
3962 | case BC_JFUNCV: | ||
3963 | #if !LJ_HASJIT | ||
3964 | break; | ||
3965 | #endif | ||
3966 | | NYI // NYI: compiled vararg functions | ||
3967 | break; /* NYI: compiled vararg functions. */ | ||
3968 | |||
3969 | case BC_IFUNCV: | ||
3970 | | // BASE = new base, RA = BASE+framesize*8, CARG3 = LFUNC, RC = nargs*8 | ||
3971 | | ldr CARG1, L->maxstack | ||
3972 | | add CARG4, BASE, RC | ||
3973 | | add RA, RA, RC | ||
3974 | | str LFUNC:CARG3, [CARG4] // Store copy of LFUNC. | ||
3975 | | add CARG2, RC, #8+FRAME_VARG | ||
3976 | | ldr KBASE, [PC, #-4+PC2PROTO(k)] | ||
3977 | | cmp RA, CARG1 | ||
3978 | | str CARG2, [CARG4, #4] // Store delta + FRAME_VARG. | ||
3979 | | bhs ->vm_growstack_l | ||
3980 | | ldrb RB, [PC, #-4+PC2PROTO(numparams)] | ||
3981 | | mov RA, BASE | ||
3982 | | mov RC, CARG4 | ||
3983 | | cmp RB, #0 | ||
3984 | | add BASE, CARG4, #8 | ||
3985 | | beq >3 | ||
3986 | | mvn CARG3, #~LJ_TNIL | ||
3987 | |1: | ||
3988 | | cmp RA, RC // Less args than parameters? | ||
3989 | | ldrdlo CARG12, [RA], #8 | ||
3990 | | movhs CARG2, CARG3 | ||
3991 | | strlo CARG3, [RA, #-4] // Clear old fixarg slot (help the GC). | ||
3992 | |2: | ||
3993 | | subs RB, RB, #1 | ||
3994 | | strd CARG12, [CARG4, #8]! | ||
3995 | | bne <1 | ||
3996 | |3: | ||
3997 | | ins_next | ||
3998 | break; | ||
3999 | |||
4000 | case BC_FUNCC: | ||
4001 | case BC_FUNCCW: | ||
4002 | | // BASE = new base, RA = BASE+framesize*8, CARG3 = CFUNC, RC = nargs*8 | ||
4003 | if (op == BC_FUNCC) { | ||
4004 | | ldr CARG4, CFUNC:CARG3->f | ||
4005 | } else { | ||
4006 | | ldr CARG4, [DISPATCH, #DISPATCH_GL(wrapf)] | ||
4007 | } | ||
4008 | | add CARG2, RA, NARGS8:RC | ||
4009 | | ldr CARG1, L->maxstack | ||
4010 | | add RC, BASE, NARGS8:RC | ||
4011 | | str BASE, L->base | ||
4012 | | cmp CARG2, CARG1 | ||
4013 | | str RC, L->top | ||
4014 | if (op == BC_FUNCCW) { | ||
4015 | | ldr CARG2, CFUNC:CARG3->f | ||
4016 | } | ||
4017 | | mv_vmstate CARG3, C | ||
4018 | | mov CARG1, L | ||
4019 | | bhi ->vm_growstack_c // Need to grow stack. | ||
4020 | | st_vmstate CARG3 | ||
4021 | | blx CARG4 // (lua_State *L [, lua_CFunction f]) | ||
4022 | | // Returns nresults. | ||
4023 | | ldr BASE, L->base | ||
4024 | | mv_vmstate CARG3, INTERP | ||
4025 | | ldr CRET2, L->top | ||
4026 | | lsl RC, CRET1, #3 | ||
4027 | | st_vmstate CARG3 | ||
4028 | | ldr PC, [BASE, FRAME_PC] | ||
4029 | | sub RA, CRET2, RC // RA = L->top - nresults*8 | ||
4030 | | b ->vm_returnc | ||
4031 | break; | ||
4032 | |||
4033 | /* ---------------------------------------------------------------------- */ | ||
4034 | |||
4035 | default: | ||
4036 | fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]); | ||
4037 | exit(2); | ||
4038 | break; | ||
4039 | } | ||
4040 | } | ||
4041 | |||
4042 | static int build_backend(BuildCtx *ctx) | ||
4043 | { | ||
4044 | int op; | ||
4045 | |||
4046 | dasm_growpc(Dst, BC__MAX); | ||
4047 | |||
4048 | build_subroutines(ctx); | ||
4049 | |||
4050 | |.code_op | ||
4051 | for (op = 0; op < BC__MAX; op++) | ||
4052 | build_ins(ctx, (BCOp)op, op); | ||
4053 | |||
4054 | return BC__MAX; | ||
4055 | } | ||
4056 | |||
4057 | /* Emit pseudo frame-info for all assembler functions. */ | ||
4058 | static void emit_asm_debug(BuildCtx *ctx) | ||
4059 | { | ||
4060 | int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code); | ||
4061 | int i; | ||
4062 | switch (ctx->mode) { | ||
4063 | case BUILD_elfasm: | ||
4064 | fprintf(ctx->fp, "\t.section .debug_frame,\"\",%%progbits\n"); | ||
4065 | fprintf(ctx->fp, | ||
4066 | ".Lframe0:\n" | ||
4067 | "\t.long .LECIE0-.LSCIE0\n" | ||
4068 | ".LSCIE0:\n" | ||
4069 | "\t.long 0xffffffff\n" | ||
4070 | "\t.byte 0x1\n" | ||
4071 | "\t.string \"\"\n" | ||
4072 | "\t.uleb128 0x1\n" | ||
4073 | "\t.sleb128 -4\n" | ||
4074 | "\t.byte 0xe\n" /* Return address is in lr. */ | ||
4075 | "\t.byte 0xc\n\t.uleb128 0xd\n\t.uleb128 0\n" /* def_cfa sp */ | ||
4076 | "\t.align 2\n" | ||
4077 | ".LECIE0:\n\n"); | ||
4078 | fprintf(ctx->fp, | ||
4079 | ".LSFDE0:\n" | ||
4080 | "\t.long .LEFDE0-.LASFDE0\n" | ||
4081 | ".LASFDE0:\n" | ||
4082 | "\t.long .Lframe0\n" | ||
4083 | "\t.long .Lbegin\n" | ||
4084 | "\t.long %d\n" | ||
4085 | "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */ | ||
4086 | "\t.byte 0x8e\n\t.uleb128 1\n", /* offset lr */ | ||
4087 | fcofs, CFRAME_SIZE); | ||
4088 | for (i = 11; i >= 4; i--) /* offset r4-r11 */ | ||
4089 | fprintf(ctx->fp, "\t.byte %d\n\t.uleb128 %d\n", 0x80+i, 2+(11-i)); | ||
4090 | fprintf(ctx->fp, | ||
4091 | "\t.align 2\n" | ||
4092 | ".LEFDE0:\n\n"); | ||
4093 | #if LJ_HASFFI | ||
4094 | fprintf(ctx->fp, | ||
4095 | ".LSFDE1:\n" | ||
4096 | "\t.long .LEFDE1-.LASFDE1\n" | ||
4097 | ".LASFDE1:\n" | ||
4098 | "\t.long .Lframe0\n" | ||
4099 | "\t.long lj_vm_ffi_call\n" | ||
4100 | "\t.long %d\n" | ||
4101 | "\t.byte 0xe\n\t.uleb128 16\n" /* def_cfa_offset */ | ||
4102 | "\t.byte 0x8e\n\t.uleb128 1\n" /* offset lr */ | ||
4103 | "\t.byte 0x8b\n\t.uleb128 2\n" /* offset r11 */ | ||
4104 | "\t.byte 0x85\n\t.uleb128 3\n" /* offset r5 */ | ||
4105 | "\t.byte 0x84\n\t.uleb128 4\n" /* offset r4 */ | ||
4106 | "\t.byte 0xd\n\t.uleb128 0xb\n" /* def_cfa_register r11 */ | ||
4107 | "\t.align 2\n" | ||
4108 | ".LEFDE1:\n\n", (int)ctx->codesz - fcofs); | ||
4109 | #endif | ||
4110 | break; | ||
4111 | default: | ||
4112 | break; | ||
4113 | } | ||
4114 | } | ||
4115 | |||