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