aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMike Pall <mike>2015-01-03 15:28:33 +0100
committerMike Pall <mike>2015-01-03 15:28:33 +0100
commitf307d0adafc7e35d2dc1c461d50f6572c5e6bca8 (patch)
tree0ce471fc05dad49f3e5d60338034ff8a4a9904f8 /src
parentcb481ddc8f9d92913ba07d998f4274bbf9711077 (diff)
downloadluajit-f307d0adafc7e35d2dc1c461d50f6572c5e6bca8.tar.gz
luajit-f307d0adafc7e35d2dc1c461d50f6572c5e6bca8.tar.bz2
luajit-f307d0adafc7e35d2dc1c461d50f6572c5e6bca8.zip
ARM64: Add build infrastructure and initial port of interpreter.
Diffstat (limited to 'src')
-rw-r--r--src/Makefile9
-rw-r--r--src/host/buildvm.c2
-rw-r--r--src/host/buildvm_asm.c9
-rw-r--r--src/lib_jit.c2
-rw-r--r--src/lj_arch.h41
-rw-r--r--src/lj_frame.h9
-rw-r--r--src/lj_vm.h4
-rw-r--r--src/lj_vmmath.c2
-rw-r--r--src/vm_arm64.dasc3649
9 files changed, 3717 insertions, 10 deletions
diff --git a/src/Makefile b/src/Makefile
index 491aa75d..88dc8166 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -48,6 +48,7 @@ CCOPT= -O2 -fomit-frame-pointer
48CCOPT_x86= -march=i686 -msse -msse2 -mfpmath=sse 48CCOPT_x86= -march=i686 -msse -msse2 -mfpmath=sse
49CCOPT_x64= 49CCOPT_x64=
50CCOPT_arm= 50CCOPT_arm=
51CCOPT_arm64=
51CCOPT_ppc= 52CCOPT_ppc=
52CCOPT_ppcspe= 53CCOPT_ppcspe=
53CCOPT_mips= 54CCOPT_mips=
@@ -217,6 +218,9 @@ else
217ifneq (,$(findstring LJ_TARGET_ARM ,$(TARGET_TESTARCH))) 218ifneq (,$(findstring LJ_TARGET_ARM ,$(TARGET_TESTARCH)))
218 TARGET_LJARCH= arm 219 TARGET_LJARCH= arm
219else 220else
221ifneq (,$(findstring LJ_TARGET_ARM64 ,$(TARGET_TESTARCH)))
222 TARGET_LJARCH= arm64
223else
220ifneq (,$(findstring LJ_TARGET_PPC ,$(TARGET_TESTARCH))) 224ifneq (,$(findstring LJ_TARGET_PPC ,$(TARGET_TESTARCH)))
221 TARGET_LJARCH= ppc 225 TARGET_LJARCH= ppc
222else 226else
@@ -236,6 +240,7 @@ endif
236endif 240endif
237endif 241endif
238endif 242endif
243endif
239 244
240ifneq (,$(findstring LJ_TARGET_PS3 1,$(TARGET_TESTARCH))) 245ifneq (,$(findstring LJ_TARGET_PS3 1,$(TARGET_TESTARCH)))
241 TARGET_SYS= PS3 246 TARGET_SYS= PS3
@@ -402,7 +407,9 @@ ifeq (Windows,$(TARGET_SYS))
402 DASM_AFLAGS+= -D WIN 407 DASM_AFLAGS+= -D WIN
403endif 408endif
404ifeq (x64,$(TARGET_LJARCH)) 409ifeq (x64,$(TARGET_LJARCH))
405 DASM_ARCH= x86 410 ifeq (,$(findstring LJ_FR2 1,$(TARGET_TESTARCH)))
411 DASM_ARCH= x86
412 endif
406else 413else
407ifeq (arm,$(TARGET_LJARCH)) 414ifeq (arm,$(TARGET_LJARCH))
408 ifeq (iOS,$(TARGET_SYS)) 415 ifeq (iOS,$(TARGET_SYS))
diff --git a/src/host/buildvm.c b/src/host/buildvm.c
index d56c65ca..d162086e 100644
--- a/src/host/buildvm.c
+++ b/src/host/buildvm.c
@@ -59,6 +59,8 @@ static int collect_reloc(BuildCtx *ctx, uint8_t *addr, int idx, int type);
59#include "../dynasm/dasm_x86.h" 59#include "../dynasm/dasm_x86.h"
60#elif LJ_TARGET_ARM 60#elif LJ_TARGET_ARM
61#include "../dynasm/dasm_arm.h" 61#include "../dynasm/dasm_arm.h"
62#elif LJ_TARGET_ARM64
63#include "../dynasm/dasm_arm64.h"
62#elif LJ_TARGET_PPC 64#elif LJ_TARGET_PPC
63#include "../dynasm/dasm_ppc.h" 65#include "../dynasm/dasm_ppc.h"
64#elif LJ_TARGET_PPCSPE 66#elif LJ_TARGET_PPCSPE
diff --git a/src/host/buildvm_asm.c b/src/host/buildvm_asm.c
index c91f5bcd..7bea4d17 100644
--- a/src/host/buildvm_asm.c
+++ b/src/host/buildvm_asm.c
@@ -121,6 +121,15 @@ static void emit_asm_wordreloc(BuildCtx *ctx, uint8_t *p, int n,
121 ins, sym); 121 ins, sym);
122 exit(1); 122 exit(1);
123 } 123 }
124#elif LJ_TARGET_ARM64
125 if ((ins >> 26) == 0x25u) {
126 fprintf(ctx->fp, "\tbl %s\n", sym);
127 } else {
128 fprintf(stderr,
129 "Error: unsupported opcode %08x for %s symbol relocation.\n",
130 ins, sym);
131 exit(1);
132 }
124#elif LJ_TARGET_PPC || LJ_TARGET_PPCSPE 133#elif LJ_TARGET_PPC || LJ_TARGET_PPCSPE
125#if LJ_TARGET_PS3 134#if LJ_TARGET_PS3
126#define TOCPREFIX "." 135#define TOCPREFIX "."
diff --git a/src/lib_jit.c b/src/lib_jit.c
index 21a72a94..7a810ae0 100644
--- a/src/lib_jit.c
+++ b/src/lib_jit.c
@@ -695,6 +695,8 @@ static uint32_t jit_cpudetect(lua_State *L)
695 ver >= 60 ? JIT_F_ARMV6_ : 0; 695 ver >= 60 ? JIT_F_ARMV6_ : 0;
696 flags |= LJ_ARCH_HASFPU == 0 ? 0 : ver >= 70 ? JIT_F_VFPV3 : JIT_F_VFPV2; 696 flags |= LJ_ARCH_HASFPU == 0 ? 0 : ver >= 70 ? JIT_F_VFPV3 : JIT_F_VFPV2;
697#endif 697#endif
698#elif LJ_TARGET_ARM64
699 /* No optional CPU features to detect (for now). */
698#elif LJ_TARGET_PPC 700#elif LJ_TARGET_PPC
699#if LJ_HASJIT 701#if LJ_HASJIT
700#if LJ_ARCH_SQRT 702#if LJ_ARCH_SQRT
diff --git a/src/lj_arch.h b/src/lj_arch.h
index 18555b41..0d69d528 100644
--- a/src/lj_arch.h
+++ b/src/lj_arch.h
@@ -19,12 +19,14 @@
19#define LUAJIT_ARCH_x64 2 19#define LUAJIT_ARCH_x64 2
20#define LUAJIT_ARCH_ARM 3 20#define LUAJIT_ARCH_ARM 3
21#define LUAJIT_ARCH_arm 3 21#define LUAJIT_ARCH_arm 3
22#define LUAJIT_ARCH_PPC 4 22#define LUAJIT_ARCH_ARM64 4
23#define LUAJIT_ARCH_ppc 4 23#define LUAJIT_ARCH_arm64 4
24#define LUAJIT_ARCH_PPCSPE 5 24#define LUAJIT_ARCH_PPC 5
25#define LUAJIT_ARCH_ppcspe 5 25#define LUAJIT_ARCH_ppc 6
26#define LUAJIT_ARCH_MIPS 6 26#define LUAJIT_ARCH_PPCSPE 6
27#define LUAJIT_ARCH_mips 6 27#define LUAJIT_ARCH_ppcspe 6
28#define LUAJIT_ARCH_MIPS 7
29#define LUAJIT_ARCH_mips 7
28 30
29/* Target OS. */ 31/* Target OS. */
30#define LUAJIT_OS_OTHER 0 32#define LUAJIT_OS_OTHER 0
@@ -43,6 +45,8 @@
43#define LUAJIT_TARGET LUAJIT_ARCH_X64 45#define LUAJIT_TARGET LUAJIT_ARCH_X64
44#elif defined(__arm__) || defined(__arm) || defined(__ARM__) || defined(__ARM) 46#elif defined(__arm__) || defined(__arm) || defined(__ARM__) || defined(__ARM)
45#define LUAJIT_TARGET LUAJIT_ARCH_ARM 47#define LUAJIT_TARGET LUAJIT_ARCH_ARM
48#elif defined(__aarch64__)
49#define LUAJIT_TARGET LUAJIT_ARCH_ARM64
46#elif defined(__ppc__) || defined(__ppc) || defined(__PPC__) || defined(__PPC) || defined(__powerpc__) || defined(__powerpc) || defined(__POWERPC__) || defined(__POWERPC) || defined(_M_PPC) 50#elif defined(__ppc__) || defined(__ppc) || defined(__PPC__) || defined(__PPC) || defined(__powerpc__) || defined(__powerpc) || defined(__POWERPC__) || defined(__POWERPC) || defined(_M_PPC)
47#ifdef __NO_FPRS__ 51#ifdef __NO_FPRS__
48#define LUAJIT_TARGET LUAJIT_ARCH_PPCSPE 52#define LUAJIT_TARGET LUAJIT_ARCH_PPCSPE
@@ -191,6 +195,24 @@
191#define LJ_ARCH_VERSION 50 195#define LJ_ARCH_VERSION 50
192#endif 196#endif
193 197
198#elif LUAJIT_TARGET == LUAJIT_ARCH_ARM64
199
200#define LJ_ARCH_NAME "arm64"
201#define LJ_ARCH_BITS 64
202#define LJ_ARCH_ENDIAN LUAJIT_LE
203#define LJ_TARGET_ARM64 1
204#define LJ_TARGET_EHRETREG 0
205#define LJ_TARGET_JUMPRANGE 27 /* +-2^27 = +-128MB */
206#define LJ_TARGET_MASKSHIFT 1
207#define LJ_TARGET_MASKROT 1
208#define LJ_TARGET_UNIFYROT 2 /* Want only IR_BROR. */
209#define LJ_TARGET_GC64 1
210#define LJ_ARCH_NUMMODE LJ_NUMMODE_DUAL
211#define LJ_ARCH_NOFFI 1 /* NYI */
212#define LJ_ARCH_NOJIT 1 /* NYI */
213
214#define LJ_ARCH_VERSION 80
215
194#elif LUAJIT_TARGET == LUAJIT_ARCH_PPC 216#elif LUAJIT_TARGET == LUAJIT_ARCH_PPC
195 217
196#define LJ_ARCH_NAME "ppc" 218#define LJ_ARCH_NAME "ppc"
@@ -327,6 +349,13 @@
327#if !(__ARM_EABI__ || LJ_TARGET_IOS) 349#if !(__ARM_EABI__ || LJ_TARGET_IOS)
328#error "Only ARM EABI or iOS 3.0+ ABI is supported" 350#error "Only ARM EABI or iOS 3.0+ ABI is supported"
329#endif 351#endif
352#elif LJ_TARGET_ARM64
353#if defined(__AARCH64EB__)
354#error "No support for big-endian ARM64"
355#endif
356#if defined(_ILP32)
357#error "No support for ILP32 model on ARM64"
358#endif
330#elif LJ_TARGET_PPC || LJ_TARGET_PPCSPE 359#elif LJ_TARGET_PPC || LJ_TARGET_PPCSPE
331#if defined(_SOFT_FLOAT) || defined(_SOFT_DOUBLE) 360#if defined(_SOFT_FLOAT) || defined(_SOFT_DOUBLE)
332#error "No support for PowerPC CPUs without double-precision FPU" 361#error "No support for PowerPC CPUs without double-precision FPU"
diff --git a/src/lj_frame.h b/src/lj_frame.h
index 226d74f1..f024c43f 100644
--- a/src/lj_frame.h
+++ b/src/lj_frame.h
@@ -159,6 +159,15 @@ enum { LJ_CONT_TAILCALL, LJ_CONT_FFI_CALLBACK }; /* Special continuations. */
159#define CFRAME_SIZE 64 159#define CFRAME_SIZE 64
160#endif 160#endif
161#define CFRAME_SHIFT_MULTRES 3 161#define CFRAME_SHIFT_MULTRES 3
162#elif LJ_TARGET_ARM64
163#define CFRAME_OFS_ERRF 196
164#define CFRAME_OFS_NRES 200
165#define CFRAME_OFS_PREV 160
166#define CFRAME_OFS_L 176
167#define CFRAME_OFS_PC 168
168#define CFRAME_OFS_MULTRES 192
169#define CFRAME_SIZE 208
170#define CFRAME_SHIFT_MULTRES 3
162#elif LJ_TARGET_PPC 171#elif LJ_TARGET_PPC
163#if LJ_TARGET_XBOX360 172#if LJ_TARGET_XBOX360
164#define CFRAME_OFS_ERRF 424 173#define CFRAME_OFS_ERRF 424
diff --git a/src/lj_vm.h b/src/lj_vm.h
index 17b62d85..f488073c 100644
--- a/src/lj_vm.h
+++ b/src/lj_vm.h
@@ -50,7 +50,7 @@ LJ_ASMF void lj_vm_exit_handler(void);
50LJ_ASMF void lj_vm_exit_interp(void); 50LJ_ASMF void lj_vm_exit_interp(void);
51 51
52/* Internal math helper functions. */ 52/* Internal math helper functions. */
53#if LJ_TARGET_PPC 53#if LJ_TARGET_PPC || LJ_TARGET_ARM64
54#define lj_vm_floor floor 54#define lj_vm_floor floor
55#define lj_vm_ceil ceil 55#define lj_vm_ceil ceil
56#else 56#else
@@ -77,7 +77,7 @@ LJ_ASMF void lj_vm_powi_sse(void);
77#else 77#else
78LJ_ASMF double lj_vm_powi(double, int32_t); 78LJ_ASMF double lj_vm_powi(double, int32_t);
79#endif 79#endif
80#if LJ_TARGET_PPC 80#if LJ_TARGET_PPC || LJ_TARGET_ARM64
81#define lj_vm_trunc trunc 81#define lj_vm_trunc trunc
82#else 82#else
83LJ_ASMF double lj_vm_trunc(double); 83LJ_ASMF double lj_vm_trunc(double);
diff --git a/src/lj_vmmath.c b/src/lj_vmmath.c
index 6ea99d15..540193bc 100644
--- a/src/lj_vmmath.c
+++ b/src/lj_vmmath.c
@@ -73,7 +73,7 @@ double lj_vm_exp2(double a)
73} 73}
74#endif 74#endif
75 75
76#if !(LJ_TARGET_ARM || LJ_TARGET_PPC) 76#if !(LJ_TARGET_ARM || LJ_TARGET_ARM64 || LJ_TARGET_PPC)
77int32_t LJ_FASTCALL lj_vm_modi(int32_t a, int32_t b) 77int32_t LJ_FASTCALL lj_vm_modi(int32_t a, int32_t b)
78{ 78{
79 uint32_t y, ua, ub; 79 uint32_t y, ua, ub;
diff --git a/src/vm_arm64.dasc b/src/vm_arm64.dasc
new file mode 100644
index 00000000..d23cac20
--- /dev/null
+++ b/src/vm_arm64.dasc
@@ -0,0 +1,3649 @@
1|// Low-level VM code for ARM64 CPUs.
2|// Bytecode interpreter, fast functions and helper functions.
3|// Copyright (C) 2005-2014 Mike Pall. See Copyright Notice in luajit.h
4|
5|.arch arm64
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|// ARM64 registers and the AAPCS64 ABI 1.0 at a glance:
19|//
20|// x0-x17 temp, x19-x28 callee-saved, x29 fp, x30 lr
21|// x18 is reserved on most platforms. Don't use it, save it or restore it.
22|// x31 doesn't exist. Register number 31 either means xzr/wzr (zero) or sp,
23|// depending on the instruction.
24|// v0-v7 temp, v8-v15 callee-saved (only d8-d15 preserved), v16-v31 temp
25|//
26|// x0-x7/v0-v7 hold parameters and results.
27|
28|// Fixed register assignments for the interpreter.
29|
30|// The following must be C callee-save.
31|.define BASE, x19 // Base of current Lua stack frame.
32|.define KBASE, x20 // Constants of current Lua function.
33|.define PC, x21 // Next PC.
34|.define GLREG, x22 // Global state.
35|.define LREG, x23 // Register holding lua_State (also in SAVE_L).
36|.define TISNUM, x24 // Constant LJ_TISNUM << 47.
37|.define TISNUMhi, x25 // Constant LJ_TISNUM << 15.
38|.define TISNIL, x26 // Constant -1LL.
39|.define fp, x29 // Yes, we have to maintain a frame pointer.
40|
41|.define ST_INTERP, w26 // Constant -1.
42|
43|// The following temporaries are not saved across C calls, except for RA/RC.
44|.define RA, x27
45|.define RC, x28
46|.define RB, x17
47|.define RAw, w27
48|.define RCw, w28
49|.define RBw, w17
50|.define INS, x16
51|.define INSw, w16
52|.define ITYPE, x15
53|.define TMP0, x8
54|.define TMP1, x9
55|.define TMP2, x10
56|.define TMP3, x11
57|.define TMP0w, w8
58|.define TMP1w, w9
59|.define TMP2w, w10
60|.define TMP3w, w11
61|
62|// Calling conventions. Also used as temporaries.
63|.define CARG1, x0
64|.define CARG2, x1
65|.define CARG3, x2
66|.define CARG4, x3
67|.define CARG5, x4
68|.define CARG1w, w0
69|.define CARG2w, w1
70|.define CARG3w, w2
71|.define CARG4w, w3
72|.define CARG5w, w4
73|
74|.define FARG1, d0
75|.define FARG2, d1
76|
77|.define CRET1, x0
78|.define CRET1w, w0
79|
80|// Stack layout while in interpreter. Must match with lj_frame.h.
81|
82|.define CFRAME_SPACE, 208
83|//----- 16 byte aligned, <-- sp entering interpreter
84|// Unused [sp, #204] // 32 bit values
85|.define SAVE_NRES, [sp, #200]
86|.define SAVE_ERRF, [sp, #196]
87|.define SAVE_MULTRES, [sp, #192]
88|.define TMPD, [sp, #184] // 64 bit values
89|.define SAVE_L, [sp, #176]
90|.define SAVE_PC, [sp, #168]
91|.define SAVE_CFRAME, [sp, #160]
92|.define SAVE_FPR_, 96 // 96+8*8: 64 bit FPR saves
93|.define SAVE_GPR_, 16 // 16+10*8: 64 bit GPR saves
94|.define SAVE_LR, [sp, #8]
95|.define SAVE_FP, [sp]
96|//----- 16 byte aligned, <-- sp while in interpreter.
97|
98|.define TMPDofs, #184
99|
100|.macro save_, gpr1, gpr2, fpr1, fpr2
101| stp d..fpr1, d..fpr2, [sp, # SAVE_FPR_+(fpr1-8)*8]
102| stp x..gpr1, x..gpr2, [sp, # SAVE_GPR_+(gpr1-19)*8]
103|.endmacro
104|.macro rest_, gpr1, gpr2, fpr1, fpr2
105| ldp d..fpr1, d..fpr2, [sp, # SAVE_FPR_+(fpr1-8)*8]
106| ldp x..gpr1, x..gpr2, [sp, # SAVE_GPR_+(gpr1-19)*8]
107|.endmacro
108|
109|.macro saveregs
110| stp fp, lr, [sp, #-CFRAME_SPACE]!
111| add fp, sp, #0
112| stp x19, x20, [sp, # SAVE_GPR_]
113| save_ 21, 22, 8, 9
114| save_ 23, 24, 10, 11
115| save_ 25, 26, 12, 13
116| save_ 27, 28, 14, 15
117|.endmacro
118|.macro restoreregs
119| ldp x19, x20, [sp, # SAVE_GPR_]
120| rest_ 21, 22, 8, 9
121| rest_ 23, 24, 10, 11
122| rest_ 25, 26, 12, 13
123| rest_ 27, 28, 14, 15
124| ldp fp, lr, [sp], # CFRAME_SPACE
125|.endmacro
126|
127|// Type definitions. Some of these are only used for documentation.
128|.type L, lua_State, LREG
129|.type GL, global_State, GLREG
130|.type TVALUE, TValue
131|.type GCOBJ, GCobj
132|.type STR, GCstr
133|.type TAB, GCtab
134|.type LFUNC, GCfuncL
135|.type CFUNC, GCfuncC
136|.type PROTO, GCproto
137|.type UPVAL, GCupval
138|.type NODE, Node
139|.type NARGS8, int
140|.type TRACE, GCtrace
141|.type SBUF, SBuf
142|
143|//-----------------------------------------------------------------------
144|
145|// Trap for not-yet-implemented parts.
146|.macro NYI; brk; .endmacro
147|
148|//-----------------------------------------------------------------------
149|
150|// Access to frame relative to BASE.
151|.define FRAME_FUNC, #-16
152|.define FRAME_PC, #-8
153|
154|.macro decode_RA, dst, ins; ubfx dst, ins, #8, #8; .endmacro
155|.macro decode_RB, dst, ins; ubfx dst, ins, #24, #8; .endmacro
156|.macro decode_RC, dst, ins; ubfx dst, ins, #16, #8; .endmacro
157|.macro decode_RD, dst, ins; ubfx dst, ins, #16, #16; .endmacro
158|.macro decode_RC8RD, dst, src; ubfiz dst, src, #3, #8; .endmacro
159|
160|// Instruction decode+dispatch.
161|.macro ins_NEXT
162| ldr INSw, [PC], #4
163| add TMP1, GL, INS, uxtb #3
164| decode_RA RA, INS
165| ldr TMP0, [TMP1, #GG_G2DISP]
166| decode_RD RC, INS
167| br TMP0
168|.endmacro
169|
170|// Instruction footer.
171|.if 1
172| // Replicated dispatch. Less unpredictable branches, but higher I-Cache use.
173| .define ins_next, ins_NEXT
174| .define ins_next_, ins_NEXT
175|.else
176| // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch.
177| // Affects only certain kinds of benchmarks (and only with -j off).
178| .macro ins_next
179| b ->ins_next
180| .endmacro
181| .macro ins_next_
182| ->ins_next:
183| ins_NEXT
184| .endmacro
185|.endif
186|
187|// Call decode and dispatch.
188|.macro ins_callt
189| // BASE = new base, CARG3 = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC
190| ldr PC, LFUNC:CARG3->pc
191| ldr INSw, [PC], #4
192| add TMP1, GL, INS, uxtb #3
193| decode_RA RA, INS
194| ldr TMP0, [TMP1, #GG_G2DISP]
195| add RA, BASE, RA, lsl #3
196| br TMP0
197|.endmacro
198|
199|.macro ins_call
200| // BASE = new base, CARG3 = LFUNC/CFUNC, RC = nargs*8, PC = caller PC
201| str PC, [BASE, FRAME_PC]
202| ins_callt
203|.endmacro
204|
205|//-----------------------------------------------------------------------
206|
207|// Macros to check the TValue type and extract the GCobj. Branch on failure.
208|.macro checktp, reg, tp, target
209| asr ITYPE, reg, #47
210| cmn ITYPE, #-tp
211| and reg, reg, #LJ_GCVMASK
212| bne target
213|.endmacro
214|.macro checktp, dst, reg, tp, target
215| asr ITYPE, reg, #47
216| cmn ITYPE, #-tp
217| and dst, reg, #LJ_GCVMASK
218| bne target
219|.endmacro
220|.macro checkstr, reg, target; checktp reg, LJ_TSTR, target; .endmacro
221|.macro checktab, reg, target; checktp reg, LJ_TTAB, target; .endmacro
222|.macro checkfunc, reg, target; checktp reg, LJ_TFUNC, target; .endmacro
223|.macro checkint, reg, target
224| cmp TISNUMhi, reg, lsr #32
225| bne target
226|.endmacro
227|.macro checknum, reg, target
228| cmp TISNUMhi, reg, lsr #32
229| bls target
230|.endmacro
231|.macro checknumber, reg, target
232| cmp TISNUMhi, reg, lsr #32
233| blo target
234|.endmacro
235|
236|.macro mov_false, reg; movn reg, #0x8000, lsl #32; .endmacro
237|.macro mov_true, reg; movn reg, #0x0001, lsl #48; .endmacro
238|
239#define GL_J(field) (GG_OFS(J) + (int)offsetof(jit_State, field))
240|
241#define PC2PROTO(field) ((int)offsetof(GCproto, field)-(int)sizeof(GCproto))
242|
243|.macro hotcheck, delta
244| NYI
245|.endmacro
246|
247|.macro hotloop
248| hotcheck HOTCOUNT_LOOP
249| blo ->vm_hotloop
250|.endmacro
251|
252|.macro hotcall
253| hotcheck HOTCOUNT_CALL
254| blo ->vm_hotcall
255|.endmacro
256|
257|// Set current VM state.
258|.macro mv_vmstate, reg, st; movn reg, #LJ_VMST_..st; .endmacro
259|.macro st_vmstate, reg; str reg, GL->vmstate; .endmacro
260|
261|// Move table write barrier back. Overwrites mark and tmp.
262|.macro barrierback, tab, mark, tmp
263| ldr tmp, GL->gc.grayagain
264| and mark, mark, #~LJ_GC_BLACK // black2gray(tab)
265| str tab, GL->gc.grayagain
266| strb mark, tab->marked
267| str tmp, tab->gclist
268|.endmacro
269|
270|//-----------------------------------------------------------------------
271
272#if !LJ_DUALNUM
273#error "Only dual-number mode supported for ARM64 target"
274#endif
275
276/* Generate subroutines used by opcodes and other parts of the VM. */
277/* The .code_sub section should be last to help static branch prediction. */
278static void build_subroutines(BuildCtx *ctx)
279{
280 |.code_sub
281 |
282 |//-----------------------------------------------------------------------
283 |//-- Return handling ----------------------------------------------------
284 |//-----------------------------------------------------------------------
285 |
286 |->vm_returnp:
287 | // See vm_return. Also: RB = previous base.
288 | tbz PC, #2, ->cont_dispatch // (PC & FRAME_P) == 0?
289 |
290 | // Return from pcall or xpcall fast func.
291 | ldr PC, [RB, FRAME_PC] // Fetch PC of previous frame.
292 | mov_true TMP0
293 | mov BASE, RB
294 | // Prepending may overwrite the pcall frame, so do it at the end.
295 | str TMP0, [RA, #-8]! // Prepend true to results.
296 |
297 |->vm_returnc:
298 | adds RC, RC, #8 // RC = (nresults+1)*8.
299 | mov CRET1, #LUA_YIELD
300 | beq ->vm_unwind_c_eh
301 | str RCw, SAVE_MULTRES
302 | ands CARG1, PC, #FRAME_TYPE
303 | beq ->BC_RET_Z // Handle regular return to Lua.
304 |
305 |->vm_return:
306 | // BASE = base, RA = resultptr, RC/MULTRES = (nresults+1)*8, PC = return
307 | // CARG1 = PC & FRAME_TYPE
308 | and RB, PC, #~FRAME_TYPEP
309 | cmp CARG1, #FRAME_C
310 | sub RB, BASE, RB // RB = previous base.
311 | bne ->vm_returnp
312 |
313 | str RB, L->base
314 | ldrsw CARG2, SAVE_NRES // CARG2 = nresults+1.
315 | mv_vmstate TMP0w, C
316 | sub BASE, BASE, #16
317 | subs TMP2, RC, #8
318 | st_vmstate TMP0w
319 | beq >2
320 |1:
321 | subs TMP2, TMP2, #8
322 | ldr TMP0, [RA], #8
323 | str TMP0, [BASE], #8
324 | bne <1
325 |2:
326 | cmp RC, CARG2, lsl #3 // More/less results wanted?
327 | bne >6
328 |3:
329 | str BASE, L->top // Store new top.
330 |
331 |->vm_leave_cp:
332 | ldr RC, SAVE_CFRAME // Restore previous C frame.
333 | mov CRET1, #0 // Ok return status for vm_pcall.
334 | str RC, L->cframe
335 |
336 |->vm_leave_unw:
337 | restoreregs
338 | ret
339 |
340 |6:
341 | bgt >7 // Less results wanted?
342 | // More results wanted. Check stack size and fill up results with nil.
343 | ldr CARG3, L->maxstack
344 | cmp BASE, CARG3
345 | bhs >8
346 | str TISNIL, [BASE], #8
347 | add RC, RC, #8
348 | b <2
349 |
350 |7: // Less results wanted.
351 | cbz CARG2, <3 // LUA_MULTRET+1 case?
352 | sub CARG1, RC, CARG2, lsl #3
353 | sub BASE, BASE, CARG1 // Shrink top.
354 | b <3
355 |
356 |8: // Corner case: need to grow stack for filling up results.
357 | // This can happen if:
358 | // - A C function grows the stack (a lot).
359 | // - The GC shrinks the stack in between.
360 | // - A return back from a lua_call() with (high) nresults adjustment.
361 | str BASE, L->top // Save current top held in BASE (yes).
362 | mov CARG1, L
363 | bl extern lj_state_growstack // (lua_State *L, int n)
364 | ldr BASE, L->top // Need the (realloced) L->top in BASE.
365 | ldrsw CARG2, SAVE_NRES
366 | b <2
367 |
368 |->vm_unwind_c: // Unwind C stack, return from vm_pcall.
369 | // (void *cframe, int errcode)
370 | mov sp, CARG1
371 | mov CRET1, CARG2
372 |->vm_unwind_c_eh: // Landing pad for external unwinder.
373 | ldr L, SAVE_L
374 | mv_vmstate TMP0w, C
375 | ldr GL, L->glref
376 | st_vmstate TMP0w
377 | b ->vm_leave_unw
378 |
379 |->vm_unwind_ff: // Unwind C stack, return from ff pcall.
380 | // (void *cframe)
381 | and sp, CARG1, #CFRAME_RAWMASK
382 |->vm_unwind_ff_eh: // Landing pad for external unwinder.
383 | ldr L, SAVE_L
384 | movz TISNUM, #(LJ_TISNUM>>1)&0xffff, lsl #48
385 | movz TISNUMhi, #(LJ_TISNUM>>1)&0xffff, lsl #16
386 | movn TISNIL, #0
387 | mov RC, #16 // 2 results: false + error message.
388 | ldr BASE, L->base
389 | ldr GL, L->glref // Setup pointer to global state.
390 | mov_false TMP0
391 | sub RA, BASE, #8 // Results start at BASE-8.
392 | ldr PC, [BASE, FRAME_PC] // Fetch PC of previous frame.
393 | str TMP0, [BASE, #-8] // Prepend false to error message.
394 | st_vmstate ST_INTERP
395 | b ->vm_returnc
396 |
397 |//-----------------------------------------------------------------------
398 |//-- Grow stack for calls -----------------------------------------------
399 |//-----------------------------------------------------------------------
400 |
401 |->vm_growstack_c: // Grow stack for C function.
402 | // CARG1 = L
403 | mov CARG2, #LUA_MINSTACK
404 | b >2
405 |
406 |->vm_growstack_l: // Grow stack for Lua function.
407 | // BASE = new base, RA = BASE+framesize*8, RC = nargs*8, PC = first PC
408 | add RC, BASE, RC
409 | sub RA, RA, BASE
410 | mov CARG1, L
411 | stp BASE, RC, L->base
412 | add PC, PC, #4 // Must point after first instruction.
413 | lsr CARG2, RA, #3
414 |2:
415 | // L->base = new base, L->top = top
416 | str PC, SAVE_PC
417 | bl extern lj_state_growstack // (lua_State *L, int n)
418 | ldp BASE, RC, L->base
419 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC]
420 | sub NARGS8:RC, RC, BASE
421 | and LFUNC:CARG3, CARG3, #LJ_GCVMASK
422 | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC
423 | ins_callt // Just retry the call.
424 |
425 |//-----------------------------------------------------------------------
426 |//-- Entry points into the assembler VM ---------------------------------
427 |//-----------------------------------------------------------------------
428 |
429 |->vm_resume: // Setup C frame and resume thread.
430 | // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0)
431 | saveregs
432 | mov L, CARG1
433 | ldr GL, L->glref // Setup pointer to global state.
434 | mov BASE, CARG2
435 | str L, SAVE_L
436 | mov PC, #FRAME_CP
437 | str wzr, SAVE_NRES
438 | add TMP0, sp, #CFRAME_RESUME
439 | ldrb TMP1w, L->status
440 | str wzr, SAVE_ERRF
441 | str L, SAVE_PC // Any value outside of bytecode is ok.
442 | str xzr, SAVE_CFRAME
443 | str TMP0, L->cframe
444 | cbz TMP1w, >3
445 |
446 | // Resume after yield (like a return).
447 | str L, GL->cur_L
448 | mov RA, BASE
449 | ldp BASE, CARG1, L->base
450 | movz TISNUM, #(LJ_TISNUM>>1)&0xffff, lsl #48
451 | movz TISNUMhi, #(LJ_TISNUM>>1)&0xffff, lsl #16
452 | ldr PC, [BASE, FRAME_PC]
453 | strb wzr, L->status
454 | movn TISNIL, #0
455 | sub RC, CARG1, BASE
456 | ands CARG1, PC, #FRAME_TYPE
457 | add RC, RC, #8
458 | st_vmstate ST_INTERP
459 | str RCw, SAVE_MULTRES
460 | beq ->BC_RET_Z
461 | b ->vm_return
462 |
463 |->vm_pcall: // Setup protected C frame and enter VM.
464 | // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef)
465 | saveregs
466 | mov PC, #FRAME_CP
467 | str CARG4w, SAVE_ERRF
468 | b >1
469 |
470 |->vm_call: // Setup C frame and enter VM.
471 | // (lua_State *L, TValue *base, int nres1)
472 | saveregs
473 | mov PC, #FRAME_C
474 |
475 |1: // Entry point for vm_pcall above (PC = ftype).
476 | ldr RC, L:CARG1->cframe
477 | str CARG3w, SAVE_NRES
478 | mov L, CARG1
479 | str CARG1, SAVE_L
480 | ldr GL, L->glref // Setup pointer to global state.
481 | mov BASE, CARG2
482 | str CARG1, SAVE_PC // Any value outside of bytecode is ok.
483 | str RC, SAVE_CFRAME
484 | str fp, L->cframe // Add our C frame to cframe chain.
485 |
486 |3: // Entry point for vm_cpcall/vm_resume (BASE = base, PC = ftype).
487 | str L, GL->cur_L
488 | ldp RB, CARG1, L->base // RB = old base (for vmeta_call).
489 | movz TISNUM, #(LJ_TISNUM>>1)&0xffff, lsl #48
490 | movz TISNUMhi, #(LJ_TISNUM>>1)&0xffff, lsl #16
491 | add PC, PC, BASE
492 | movn TISNIL, #0
493 | sub PC, PC, RB // PC = frame delta + frame type
494 | sub NARGS8:RC, CARG1, BASE
495 | st_vmstate ST_INTERP
496 |
497 |->vm_call_dispatch:
498 | // RB = old base, BASE = new base, RC = nargs*8, PC = caller PC
499 | ldr CARG3, [BASE, FRAME_FUNC]
500 | checkfunc CARG3, ->vmeta_call
501 |
502 |->vm_call_dispatch_f:
503 | ins_call
504 | // BASE = new base, CARG3 = func, RC = nargs*8, PC = caller PC
505 |
506 |->vm_cpcall: // Setup protected C frame, call C.
507 | // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp)
508 | saveregs
509 | mov L, CARG1
510 | ldr RA, L:CARG1->stack
511 | str CARG1, SAVE_L
512 | ldr GL, L->glref // Setup pointer to global state.
513 | ldr RB, L->top
514 | str CARG1, SAVE_PC // Any value outside of bytecode is ok.
515 | ldr RC, L->cframe
516 | sub RA, RA, RB // Compute -savestack(L, L->top).
517 | str RAw, SAVE_NRES // Neg. delta means cframe w/o frame.
518 | str wzr, SAVE_ERRF // No error function.
519 | str RC, SAVE_CFRAME
520 | str fp, L->cframe // Add our C frame to cframe chain.
521 | str L, GL->cur_L
522 | blr CARG4 // (lua_State *L, lua_CFunction func, void *ud)
523 | mov BASE, CRET1
524 | mov PC, #FRAME_CP
525 | cbnz BASE, <3 // Else continue with the call.
526 | b ->vm_leave_cp // No base? Just remove C frame.
527 |
528 |//-----------------------------------------------------------------------
529 |//-- Metamethod handling ------------------------------------------------
530 |//-----------------------------------------------------------------------
531 |
532 |//-- Continuation dispatch ----------------------------------------------
533 |
534 |->cont_dispatch:
535 | // BASE = meta base, RA = resultptr, RC = (nresults+1)*8
536 | ldr LFUNC:CARG3, [RB, FRAME_FUNC]
537 | ldr CARG1, [BASE, #-32] // Get continuation.
538 | mov CARG4, BASE
539 | mov BASE, RB // Restore caller BASE.
540 | and LFUNC:CARG3, CARG3, #LJ_GCVMASK
541 |.if FFI
542 | cmp CARG1, #1
543 |.endif
544 | ldr PC, [CARG4, #-24] // Restore PC from [cont|PC].
545 | ldr CARG3, LFUNC:CARG3->pc
546 | add TMP0, RA, RC
547 | str TISNIL, [TMP0, #-8] // Ensure one valid arg.
548 |.if FFI
549 | bls >1
550 |.endif
551 | ldr KBASE, [CARG3, #PC2PROTO(k)]
552 | // BASE = base, RA = resultptr, CARG4 = meta base
553 | br CARG1
554 |
555 |.if FFI
556 |1:
557 | beq ->cont_ffi_callback // cont = 1: return from FFI callback.
558 | // cont = 0: tailcall from C function.
559 | sub CARG4, CARG4, #32
560 | sub RC, CARG4, BASE
561 | b ->vm_call_tail
562 |.endif
563 |
564 |->cont_cat: // RA = resultptr, CARG4 = meta base
565 | ldr INSw, [PC, #-4]
566 | sub CARG2, CARG4, #32
567 | ldr TMP0, [RA]
568 | str BASE, L->base
569 | decode_RB RB, INS
570 | decode_RA RA, INS
571 | add TMP1, BASE, RB, lsl #3
572 | subs TMP1, CARG2, TMP1
573 | beq >1
574 | str TMP0, [CARG2]
575 | lsr CARG3, TMP1, #3
576 | b ->BC_CAT_Z
577 |
578 |1:
579 | str TMP0, [BASE, RA, lsl #3]
580 | b ->cont_nop
581 |
582 |//-- Table indexing metamethods -----------------------------------------
583 |
584 |->vmeta_tgets1:
585 | movn CARG4, #~LJ_TSTR
586 | add CARG2, BASE, RB, lsl #3
587 | add CARG4, STR:RC, CARG4, lsl #47
588 | b >2
589 |
590 |->vmeta_tgets:
591 | movk CARG2, #(LJ_TTAB>>1)&0xffff, lsl #48
592 | str CARG2, GL->tmptv
593 | add CARG2, GL, #offsetof(global_State, tmptv)
594 |2:
595 | add CARG3, sp, TMPDofs
596 | str CARG4, TMPD
597 | b >1
598 |
599 |->vmeta_tgetb: // RB = table, RC = index
600 | add RC, RC, TISNUM
601 | add CARG2, BASE, RB, lsl #3
602 | add CARG3, sp, TMPDofs
603 | str RC, TMPD
604 | b >1
605 |
606 |->vmeta_tgetv: // RB = table, RC = key
607 | add CARG2, BASE, RB, lsl #3
608 | add CARG3, BASE, RC, lsl #3
609 |1:
610 | str BASE, L->base
611 | mov CARG1, L
612 | str PC, SAVE_PC
613 | bl extern lj_meta_tget // (lua_State *L, TValue *o, TValue *k)
614 | // Returns TValue * (finished) or NULL (metamethod).
615 | cbz CRET1, >3
616 | ldr TMP0, [CRET1]
617 | str TMP0, [BASE, RA, lsl #3]
618 | ins_next
619 |
620 |3: // Call __index metamethod.
621 | // BASE = base, L->top = new base, stack = cont/func/t/k
622 | sub TMP1, BASE, #FRAME_CONT
623 | ldr BASE, L->top
624 | mov NARGS8:RC, #16 // 2 args for func(t, k).
625 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Guaranteed to be a function here.
626 | str PC, [BASE, #-24] // [cont|PC]
627 | sub PC, BASE, TMP1
628 | and LFUNC:CARG3, CARG3, #LJ_GCVMASK
629 | b ->vm_call_dispatch_f
630 |
631 |->vmeta_tgetr:
632 | sxtw CARG2, TMP1w
633 | bl extern lj_tab_getinth // (GCtab *t, int32_t key)
634 | // Returns cTValue * or NULL.
635 | mov TMP0, TISNIL
636 | cbz CRET1, ->BC_TGETR_Z
637 | ldr TMP0, [CRET1]
638 | b ->BC_TGETR_Z
639 |
640 |//-----------------------------------------------------------------------
641 |
642 |->vmeta_tsets1:
643 | movn CARG4, #~LJ_TSTR
644 | add CARG2, BASE, RB, lsl #3
645 | add CARG4, STR:RC, CARG4, lsl #47
646 | b >2
647 |
648 |->vmeta_tsets:
649 | movk CARG2, #(LJ_TTAB>>1)&0xffff, lsl #48
650 | str CARG2, GL->tmptv
651 | add CARG2, GL, #offsetof(global_State, tmptv)
652 |2:
653 | add CARG3, sp, TMPDofs
654 | str CARG4, TMPD
655 | b >1
656 |
657 |->vmeta_tsetb: // RB = table, RC = index
658 | add RC, RC, TISNUM
659 | add CARG2, BASE, RB, lsl #3
660 | add CARG3, sp, TMPDofs
661 | str RC, TMPD
662 | b >1
663 |
664 |->vmeta_tsetv:
665 | add CARG2, BASE, RB, lsl #3
666 | add CARG3, BASE, RC, lsl #3
667 |1:
668 | str BASE, L->base
669 | mov CARG1, L
670 | str PC, SAVE_PC
671 | bl extern lj_meta_tset // (lua_State *L, TValue *o, TValue *k)
672 | // Returns TValue * (finished) or NULL (metamethod).
673 | ldr TMP0, [BASE, RA, lsl #3]
674 | cbz CRET1, >3
675 | // NOBARRIER: lj_meta_tset ensures the table is not black.
676 | str TMP0, [CRET1]
677 | ins_next
678 |
679 |3: // Call __newindex metamethod.
680 | // BASE = base, L->top = new base, stack = cont/func/t/k/(v)
681 | sub TMP1, BASE, #FRAME_CONT
682 | ldr BASE, L->top
683 | mov NARGS8:RC, #24 // 3 args for func(t, k, v).
684 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Guaranteed to be a function here.
685 | str TMP0, [BASE, #16] // Copy value to third argument.
686 | str PC, [BASE, #-24] // [cont|PC]
687 | sub PC, BASE, TMP1
688 | and LFUNC:CARG3, CARG3, #LJ_GCVMASK
689 | b ->vm_call_dispatch_f
690 |
691 |->vmeta_tsetr:
692 | sxtw CARG3, TMP1w
693 | str BASE, L->base
694 | str PC, SAVE_PC
695 | bl extern lj_tab_setinth // (lua_State *L, GCtab *t, int32_t key)
696 | // Returns TValue *.
697 | b ->BC_TSETR_Z
698 |
699 |//-- Comparison metamethods ---------------------------------------------
700 |
701 |->vmeta_comp:
702 | add CARG2, BASE, RA, lsl #3
703 | sub PC, PC, #4
704 | add CARG3, BASE, RC, lsl #3
705 | str BASE, L->base
706 | mov CARG1, L
707 | str PC, SAVE_PC
708 | uxtb CARG4w, INSw
709 | bl extern lj_meta_comp // (lua_State *L, TValue *o1, *o2, int op)
710 | // Returns 0/1 or TValue * (metamethod).
711 |3:
712 | cmp CRET1, #1
713 | bhi ->vmeta_binop
714 |4:
715 | ldrh RBw, [PC, #2]
716 | add PC, PC, #4
717 | add RB, PC, RB, lsl #2
718 | sub RB, RB, #0x20000
719 | csel PC, PC, RB, lo
720 |->cont_nop:
721 | ins_next
722 |
723 |->cont_ra: // RA = resultptr
724 | ldr INSw, [PC, #-4]
725 | ldr TMP0, [RA]
726 | decode_RA TMP1, INS
727 | str TMP0, [BASE, TMP1, lsl #3]
728 | b ->cont_nop
729 |
730 |->cont_condt: // RA = resultptr
731 | ldr TMP0, [RA]
732 | mov_true TMP1
733 | cmp TMP1, TMP0 // Branch if result is true.
734 | b <4
735 |
736 |->cont_condf: // RA = resultptr
737 | ldr TMP0, [RA]
738 | mov_false TMP1
739 | cmp TMP0, TMP1 // Branch if result is false.
740 | b <4
741 |
742 |->vmeta_equal:
743 | // CARG2, CARG3, CARG4 are already set by BC_ISEQV/BC_ISNEV.
744 | and TAB:CARG3, CARG3, #LJ_GCVMASK
745 | sub PC, PC, #4
746 | str BASE, L->base
747 | mov CARG1, L
748 | str PC, SAVE_PC
749 | bl extern lj_meta_equal // (lua_State *L, GCobj *o1, *o2, int ne)
750 | // Returns 0/1 or TValue * (metamethod).
751 | b <3
752 |
753 |->vmeta_equal_cd:
754 |.if FFI
755 | sub PC, PC, #4
756 | str BASE, L->base
757 | mov CARG1, L
758 | mov CARG2, INS
759 | str PC, SAVE_PC
760 | bl extern lj_meta_equal_cd // (lua_State *L, BCIns op)
761 | // Returns 0/1 or TValue * (metamethod).
762 | b <3
763 |.endif
764 |
765 |->vmeta_istype:
766 | sub PC, PC, #4
767 | str BASE, L->base
768 | mov CARG1, L
769 | mov CARG2, RA
770 | mov CARG3, RC
771 | str PC, SAVE_PC
772 | bl extern lj_meta_istype // (lua_State *L, BCReg ra, BCReg tp)
773 | b ->cont_nop
774 |
775 |//-- Arithmetic metamethods ---------------------------------------------
776 |
777 |->vmeta_arith_vn:
778 | add CARG3, BASE, RB, lsl #3
779 | add CARG4, KBASE, RC, lsl #3
780 | b >1
781 |
782 |->vmeta_arith_nv:
783 | add CARG4, BASE, RB, lsl #3
784 | add CARG3, KBASE, RC, lsl #3
785 | b >1
786 |
787 |->vmeta_unm:
788 | add CARG3, BASE, RC, lsl #3
789 | mov CARG4, CARG3
790 | b >1
791 |
792 |->vmeta_arith_vv:
793 | add CARG3, BASE, RB, lsl #3
794 | add CARG4, BASE, RC, lsl #3
795 |1:
796 | uxtb CARG5w, INSw
797 | add CARG2, BASE, RA, lsl #3
798 | str BASE, L->base
799 | mov CARG1, L
800 | str PC, SAVE_PC
801 | bl extern lj_meta_arith // (lua_State *L, TValue *ra,*rb,*rc, BCReg op)
802 | // Returns NULL (finished) or TValue * (metamethod).
803 | cbz CRET1, ->cont_nop
804 |
805 | // Call metamethod for binary op.
806 |->vmeta_binop:
807 | // BASE = old base, CRET1 = new base, stack = cont/func/o1/o2
808 | sub TMP1, CRET1, BASE
809 | str PC, [CRET1, #-24] // [cont|PC]
810 | add PC, TMP1, #FRAME_CONT
811 | mov BASE, CRET1
812 | mov NARGS8:RC, #16 // 2 args for func(o1, o2).
813 | b ->vm_call_dispatch
814 |
815 |->vmeta_len:
816 | add CARG2, BASE, RC, lsl #3
817#if LJ_52
818 | mov TAB:RC, TAB:CARG1 // Save table (ignored for other types).
819#endif
820 | str BASE, L->base
821 | mov CARG1, L
822 | str PC, SAVE_PC
823 | bl extern lj_meta_len // (lua_State *L, TValue *o)
824 | // Returns NULL (retry) or TValue * (metamethod base).
825#if LJ_52
826 | cbnz CRET1, ->vmeta_binop // Binop call for compatibility.
827 | mov TAB:CARG1, TAB:RC
828 | b ->BC_LEN_Z
829#else
830 | b ->vmeta_binop // Binop call for compatibility.
831#endif
832 |
833 |//-- Call metamethod ----------------------------------------------------
834 |
835 |->vmeta_call: // Resolve and call __call metamethod.
836 | // RB = old base, BASE = new base, RC = nargs*8
837 | mov CARG1, L
838 | str RB, L->base // This is the callers base!
839 | sub CARG2, BASE, #16
840 | str PC, SAVE_PC
841 | add CARG3, BASE, NARGS8:RC
842 | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
843 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Guaranteed to be a function here.
844 | add NARGS8:RC, NARGS8:RC, #8 // Got one more argument now.
845 | and LFUNC:CARG3, CARG3, #LJ_GCVMASK
846 | ins_call
847 |
848 |->vmeta_callt: // Resolve __call for BC_CALLT.
849 | // BASE = old base, RA = new base, RC = nargs*8
850 | mov CARG1, L
851 | str BASE, L->base
852 | sub CARG2, RA, #16
853 | str PC, SAVE_PC
854 | add CARG3, RA, NARGS8:RC
855 | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
856 | ldp LFUNC:CARG3, PC, [RA, FRAME_FUNC] // Guaranteed to be a function here.
857 | add NARGS8:RC, NARGS8:RC, #8 // Got one more argument now.
858 | and LFUNC:CARG3, CARG3, #LJ_GCVMASK
859 | b ->BC_CALLT2_Z
860 |
861 |//-- Argument coercion for 'for' statement ------------------------------
862 |
863 |->vmeta_for:
864 | mov CARG1, L
865 | str BASE, L->base
866 | mov CARG2, RA
867 | str PC, SAVE_PC
868 | bl extern lj_meta_for // (lua_State *L, TValue *base)
869 | ldr INSw, [PC, #-4]
870 |.if JIT
871 | uxtb TMP0, INS
872 |.endif
873 | decode_RA RA, INS
874 | decode_RD RC, INS
875 |.if JIT
876 | cmp TMP0, #BC_JFORI
877 | beq =>BC_JFORI
878 |.endif
879 | b =>BC_FORI
880 |
881 |//-----------------------------------------------------------------------
882 |//-- Fast functions -----------------------------------------------------
883 |//-----------------------------------------------------------------------
884 |
885 |.macro .ffunc, name
886 |->ff_ .. name:
887 |.endmacro
888 |
889 |.macro .ffunc_1, name
890 |->ff_ .. name:
891 | ldr CARG1, [BASE]
892 | cmp NARGS8:RC, #8
893 | blo ->fff_fallback
894 |.endmacro
895 |
896 |.macro .ffunc_2, name
897 |->ff_ .. name:
898 | ldp CARG1, CARG2, [BASE]
899 | cmp NARGS8:RC, #16
900 | blo ->fff_fallback
901 |.endmacro
902 |
903 |.macro .ffunc_n, name
904 | .ffunc name
905 | ldr CARG1, [BASE]
906 | cmp NARGS8:RC, #8
907 | ldr FARG1, [BASE]
908 | blo ->fff_fallback
909 | checknum CARG1, ->fff_fallback
910 |.endmacro
911 |
912 |.macro .ffunc_nn, name
913 | .ffunc name
914 | ldp CARG1, CARG2, [BASE]
915 | cmp NARGS8:RC, #16
916 | ldp FARG1, FARG2, [BASE]
917 | blo ->fff_fallback
918 | checknum CARG1, ->fff_fallback
919 | checknum CARG2, ->fff_fallback
920 |.endmacro
921 |
922 |// Inlined GC threshold check. Caveat: uses CARG1 and CARG2.
923 |.macro ffgccheck
924 | ldp CARG1, CARG2, GL->gc.total // Assumes threshold follows total.
925 | cmp CARG1, CARG2
926 | blt >1
927 | bl ->fff_gcstep
928 |1:
929 |.endmacro
930 |
931 |//-- Base library: checks -----------------------------------------------
932 |
933 |.ffunc_1 assert
934 | ldr PC, [BASE, FRAME_PC]
935 | mov_false TMP1
936 | cmp CARG1, TMP1
937 | bhs ->fff_fallback
938 | str CARG1, [BASE, #-16]
939 | sub RB, BASE, #8
940 | subs RA, NARGS8:RC, #8
941 | add RC, NARGS8:RC, #8 // Compute (nresults+1)*8.
942 | cbz RA, ->fff_res // Done if exactly 1 argument.
943 |1:
944 | ldr CARG1, [RB, #16]
945 | sub RA, RA, #8
946 | str CARG1, [RB], #8
947 | cbnz RA, <1
948 | b ->fff_res
949 |
950 |.ffunc_1 type
951 | mov TMP0, #~LJ_TISNUM
952 | asr ITYPE, CARG1, #47
953 | cmn ITYPE, #~LJ_TISNUM
954 | csinv TMP1, TMP0, ITYPE, lo
955 | add TMP1, TMP1, #offsetof(GCfuncC, upvalue)/8
956 | ldr CARG1, [CFUNC:CARG3, TMP1, lsl #3]
957 | b ->fff_restv
958 |
959 |//-- Base library: getters and setters ---------------------------------
960 |
961 |.ffunc_1 getmetatable
962 | asr ITYPE, CARG1, #47
963 | cmn ITYPE, #-LJ_TTAB
964 | ccmn ITYPE, #-LJ_TUDATA, #4, ne
965 | and TAB:CARG1, CARG1, #LJ_GCVMASK
966 | bne >6
967 |1: // Field metatable must be at same offset for GCtab and GCudata!
968 | ldr TAB:RB, TAB:CARG1->metatable
969 |2:
970 | mov CARG1, TISNIL
971 | ldr STR:RC, GL->gcroot[GCROOT_MMNAME+MM_metatable]
972 | cbz TAB:RB, ->fff_restv
973 | ldr TMP1w, TAB:RB->hmask
974 | ldr TMP2w, STR:RC->hash
975 | ldr NODE:CARG3, TAB:RB->node
976 | and TMP1w, TMP1w, TMP2w // idx = str->hash & tab->hmask
977 | add TMP1, TMP1, TMP1, lsl #1
978 | movn CARG4, #~LJ_TSTR
979 | add NODE:CARG3, NODE:CARG3, TMP1, lsl #3 // node = tab->node + idx*3*8
980 | add CARG4, STR:RC, CARG4, lsl #47 // Tagged key to look for.
981 |3: // Rearranged logic, because we expect _not_ to find the key.
982 | ldp CARG1, TMP0, NODE:CARG3->val
983 | ldr NODE:CARG3, NODE:CARG3->next
984 | cmp TMP0, CARG4
985 | beq >5
986 | cbnz NODE:CARG3, <3
987 |4:
988 | mov CARG1, RB // Use metatable as default result.
989 | movk CARG1, #(LJ_TTAB>>1)&0xffff, lsl #48
990 | b ->fff_restv
991 |5:
992 | cmp TMP0, TISNIL
993 | bne ->fff_restv
994 | b <4
995 |
996 |6:
997 | movn TMP0, #~LJ_TISNUM
998 | cmp ITYPE, TMP0
999 | csel ITYPE, ITYPE, TMP0, hs
1000 | sub TMP1, GL, ITYPE, lsl #3
1001 | ldr TAB:RB, [TMP1, #offsetof(global_State, gcroot[GCROOT_BASEMT])-8]
1002 | b <2
1003 |
1004 |.ffunc_2 setmetatable
1005 | // Fast path: no mt for table yet and not clearing the mt.
1006 | checktp TMP1, CARG1, LJ_TTAB, ->fff_fallback
1007 | ldr TAB:TMP0, TAB:TMP1->metatable
1008 | asr ITYPE, CARG2, #47
1009 | ldrb TMP2w, TAB:TMP1->marked
1010 | cmn ITYPE, #-LJ_TTAB
1011 | and TAB:CARG2, CARG2, #LJ_GCVMASK
1012 | ccmp TAB:TMP0, #0, #0, eq
1013 | bne ->fff_fallback
1014 | str TAB:CARG2, TAB:TMP1->metatable
1015 | tbz TMP2w, #2, ->fff_restv // isblack(table)
1016 | barrierback TAB:TMP1, TMP2w, TMP0
1017 | b ->fff_restv
1018 |
1019 |.ffunc rawget
1020 | ldr CARG2, [BASE]
1021 | cmp NARGS8:RC, #16
1022 | blo ->fff_fallback
1023 | checktab CARG2, ->fff_fallback
1024 | mov CARG1, L
1025 | add CARG3, BASE, #8
1026 | bl extern lj_tab_get // (lua_State *L, GCtab *t, cTValue *key)
1027 | // Returns cTValue *.
1028 | ldr CARG1, [CRET1]
1029 | b ->fff_restv
1030 |
1031 |//-- Base library: conversions ------------------------------------------
1032 |
1033 |.ffunc tonumber
1034 | // Only handles the number case inline (without a base argument).
1035 | ldr CARG1, [BASE]
1036 | cmp NARGS8:RC, #8
1037 | bne ->fff_fallback
1038 | checknumber CARG1, ->fff_fallback
1039 | b ->fff_restv
1040 |
1041 |.ffunc_1 tostring
1042 | // Only handles the string or number case inline.
1043 | asr ITYPE, CARG1, #47
1044 | cmn ITYPE, #-LJ_TSTR
1045 | // A __tostring method in the string base metatable is ignored.
1046 | beq ->fff_restv
1047 | // Handle numbers inline, unless a number base metatable is present.
1048 | ldr TMP1, GL->gcroot[GCROOT_BASEMT_NUM]
1049 | str BASE, L->base
1050 | cmn ITYPE, #-LJ_TISNUM
1051 | ccmp TMP1, #0, #0, ls
1052 | str PC, SAVE_PC // Redundant (but a defined value).
1053 | bne ->fff_fallback
1054 | ffgccheck
1055 | mov CARG1, L
1056 | mov CARG2, BASE
1057 | bl extern lj_strfmt_number // (lua_State *L, cTValue *o)
1058 | // Returns GCstr *.
1059 | movn TMP1, #~LJ_TSTR
1060 | ldr BASE, L->base
1061 | add CARG1, CARG1, TMP1, lsl #47
1062 | b ->fff_restv
1063 |
1064 |//-- Base library: iterators -------------------------------------------
1065 |
1066 |.ffunc_1 next
1067 | checktp CARG2, CARG1, LJ_TTAB, ->fff_fallback
1068 | str TISNIL, [BASE, NARGS8:RC] // Set missing 2nd arg to nil.
1069 | ldr PC, [BASE, FRAME_PC]
1070 | stp BASE, BASE, L->base // Add frame since C call can throw.
1071 | mov CARG1, L
1072 | add CARG3, BASE, #8
1073 | str PC, SAVE_PC
1074 | bl extern lj_tab_next // (lua_State *L, GCtab *t, TValue *key)
1075 | // Returns 0 at end of traversal.
1076 | str TISNIL, [BASE, #-16]
1077 | cbz CRET1, ->fff_res1 // End of traversal: return nil.
1078 | ldp CARG1, CARG2, [BASE, #8] // Copy key and value to results.
1079 | mov RC, #(2+1)*8
1080 | stp CARG1, CARG2, [BASE, #-16]
1081 | b ->fff_res
1082 |
1083 |.ffunc_1 pairs
1084 | checktp TMP1, CARG1, LJ_TTAB, ->fff_fallback
1085#if LJ_52
1086 | ldr TAB:CARG2, TAB:TMP1->metatable
1087#endif
1088 | ldr CFUNC:CARG4, CFUNC:CARG3->upvalue[0]
1089 | ldr PC, [BASE, FRAME_PC]
1090#if LJ_52
1091 | cbnz TAB:CARG2, ->fff_fallback
1092#endif
1093 | mov RC, #(3+1)*8
1094 | stp CARG1, TISNIL, [BASE, #-8]
1095 | str CFUNC:CARG4, [BASE, #-16]
1096 | b ->fff_res
1097 |
1098 |.ffunc_2 ipairs_aux
1099 | checktab CARG1, ->fff_fallback
1100 | checkint CARG2, ->fff_fallback
1101 | ldr TMP1w, TAB:CARG1->asize
1102 | ldr CARG3, TAB:CARG1->array
1103 | ldr TMP0w, TAB:CARG1->hmask
1104 | add CARG2w, CARG2w, #1
1105 | cmp CARG2w, TMP1w
1106 | ldr PC, [BASE, FRAME_PC]
1107 | add TMP2, CARG2, TISNUM
1108 | mov RC, #(0+1)*8
1109 | str TMP2, [BASE, #-16]
1110 | bhs >2 // Not in array part?
1111 | ldr TMP0, [CARG3, CARG2, lsl #3]
1112 |1:
1113 | mov TMP1, #(2+1)*8
1114 | cmp TMP0, TISNIL
1115 | str TMP0, [BASE, #-8]
1116 | csel RC, RC, TMP1, eq
1117 | b ->fff_res
1118 |2: // Check for empty hash part first. Otherwise call C function.
1119 | cbz TMP0w, ->fff_res
1120 | bl extern lj_tab_getinth // (GCtab *t, int32_t key)
1121 | // Returns cTValue * or NULL.
1122 | cbz CRET1, ->fff_res
1123 | ldr TMP0, [CRET1]
1124 | b <1
1125 |
1126 |.ffunc_1 ipairs
1127 | checktp TMP1, CARG1, LJ_TTAB, ->fff_fallback
1128#if LJ_52
1129 | ldr TAB:CARG2, TAB:TMP1->metatable
1130#endif
1131 | ldr CFUNC:CARG4, CFUNC:CARG3->upvalue[0]
1132 | ldr PC, [BASE, FRAME_PC]
1133#if LJ_52
1134 | cbnz TAB:CARG2, ->fff_fallback
1135#endif
1136 | mov RC, #(3+1)*8
1137 | stp CARG1, TISNUM, [BASE, #-8]
1138 | str CFUNC:CARG4, [BASE, #-16]
1139 | b ->fff_res
1140 |
1141 |//-- Base library: catch errors ----------------------------------------
1142 |
1143 |.ffunc pcall
1144 | ldrb TMP0w, GL->hookmask
1145 | subs NARGS8:RC, NARGS8:RC, #8
1146 | blo ->fff_fallback
1147 | mov RB, BASE
1148 | add BASE, BASE, #16
1149 | ubfx TMP0w, TMP0w, #HOOK_ACTIVE_SHIFT, #1
1150 | add PC, TMP0, #16+FRAME_PCALL
1151 | beq ->vm_call_dispatch
1152 |1:
1153 | add TMP2, BASE, NARGS8:RC
1154 |2:
1155 | ldr TMP0, [TMP2, #-16]
1156 | str TMP0, [TMP2, #-8]!
1157 | cmp TMP2, BASE
1158 | bne <2
1159 | b ->vm_call_dispatch
1160 |
1161 |.ffunc xpcall
1162 | ldp CARG1, CARG2, [BASE]
1163 | ldrb TMP0w, GL->hookmask
1164 | subs NARGS8:RC, NARGS8:RC, #16
1165 | blo ->fff_fallback
1166 | mov RB, BASE
1167 | add BASE, BASE, #24
1168 | asr ITYPE, CARG2, #47
1169 | ubfx TMP0w, TMP0w, #HOOK_ACTIVE_SHIFT, #1
1170 | cmn ITYPE, #-LJ_TFUNC
1171 | add PC, TMP0, #24+FRAME_PCALL
1172 | bne ->fff_fallback // Traceback must be a function.
1173 | stp CARG2, CARG1, [RB] // Swap function and traceback.
1174 | cbz NARGS8:RC, ->vm_call_dispatch
1175 | b <1
1176 |
1177 |//-- Coroutine library --------------------------------------------------
1178 |
1179 |.macro coroutine_resume_wrap, resume
1180 |.if resume
1181 |.ffunc_1 coroutine_resume
1182 | checktp CARG1, LJ_TTHREAD, ->fff_fallback
1183 |.else
1184 |.ffunc coroutine_wrap_aux
1185 | ldr L:CARG1, CFUNC:CARG3->upvalue[0].gcr
1186 | and L:CARG1, CARG1, #LJ_GCVMASK
1187 |.endif
1188 | ldr PC, [BASE, FRAME_PC]
1189 | str BASE, L->base
1190 | ldp RB, CARG2, L:CARG1->base
1191 | ldrb TMP1w, L:CARG1->status
1192 | add TMP0, CARG2, TMP1
1193 | str PC, SAVE_PC
1194 | cmp TMP0, RB
1195 | beq ->fff_fallback
1196 | cmp TMP1, #LUA_YIELD
1197 | add TMP0, CARG2, #8
1198 | csel CARG2, CARG2, TMP0, hs
1199 | ldr CARG4, L:CARG1->maxstack
1200 | add CARG3, CARG2, NARGS8:RC
1201 | ldr RB, L:CARG1->cframe
1202 | ccmp CARG3, CARG4, #2, ls
1203 | ccmp RB, #0, #2, ls
1204 | bhi ->fff_fallback
1205 |.if resume
1206 | sub CARG3, CARG3, #8 // Keep resumed thread in stack for GC.
1207 | add BASE, BASE, #8
1208 | sub NARGS8:RC, NARGS8:RC, #8
1209 |.endif
1210 | str CARG3, L:CARG1->top
1211 | str BASE, L->top
1212 | cbz NARGS8:RC, >3
1213 |2: // Move args to coroutine.
1214 | ldr TMP0, [BASE, RB]
1215 | cmp RB, NARGS8:RC
1216 | str TMP0, [CARG2, RB]
1217 | add RB, RB, #8
1218 | bne <2
1219 |3:
1220 | mov CARG3, #0
1221 | mov L:RA, L:CARG1
1222 | mov CARG4, #0
1223 | bl ->vm_resume // (lua_State *L, TValue *base, 0, 0)
1224 | // Returns thread status.
1225 |4:
1226 | ldp CARG3, CARG4, L:RA->base
1227 | cmp CRET1, #LUA_YIELD
1228 | ldr BASE, L->base
1229 | str L, GL->cur_L
1230 | st_vmstate ST_INTERP
1231 | bhi >8
1232 | sub RC, CARG4, CARG3
1233 | ldr CARG1, L->maxstack
1234 | add CARG2, BASE, RC
1235 | cbz RC, >6 // No results?
1236 | cmp CARG2, CARG1
1237 | mov RB, #0
1238 | bhi >9 // Need to grow stack?
1239 |
1240 | sub CARG4, RC, #8
1241 | str CARG3, L:RA->top // Clear coroutine stack.
1242 |5: // Move results from coroutine.
1243 | ldr TMP0, [CARG3, RB]
1244 | cmp RB, CARG4
1245 | str TMP0, [BASE, RB]
1246 | add RB, RB, #8
1247 | bne <5
1248 |6:
1249 |.if resume
1250 | mov_true TMP1
1251 | add RC, RC, #16
1252 |7:
1253 | str TMP1, [BASE, #-8] // Prepend true/false to results.
1254 | sub RA, BASE, #8
1255 |.else
1256 | mov RA, BASE
1257 | add RC, RC, #8
1258 |.endif
1259 | ands CARG1, PC, #FRAME_TYPE
1260 | str PC, SAVE_PC
1261 | str RCw, SAVE_MULTRES
1262 | beq ->BC_RET_Z
1263 | b ->vm_return
1264 |
1265 |8: // Coroutine returned with error (at co->top-1).
1266 |.if resume
1267 | ldr TMP0, [CARG4, #-8]!
1268 | mov_false TMP1
1269 | mov RC, #(2+1)*8
1270 | str CARG4, L:RA->top // Remove error from coroutine stack.
1271 | str TMP0, [BASE] // Copy error message.
1272 | b <7
1273 |.else
1274 | mov CARG1, L
1275 | mov CARG2, L:RA
1276 | bl extern lj_ffh_coroutine_wrap_err // (lua_State *L, lua_State *co)
1277 | // Never returns.
1278 |.endif
1279 |
1280 |9: // Handle stack expansion on return from yield.
1281 | mov CARG1, L
1282 | lsr CARG2, RC, #3
1283 | bl extern lj_state_growstack // (lua_State *L, int n)
1284 | mov CRET1, #0
1285 | b <4
1286 |.endmacro
1287 |
1288 | coroutine_resume_wrap 1 // coroutine.resume
1289 | coroutine_resume_wrap 0 // coroutine.wrap
1290 |
1291 |.ffunc coroutine_yield
1292 | ldr TMP0, L->cframe
1293 | add TMP1, BASE, NARGS8:RC
1294 | mov CRET1, #LUA_YIELD
1295 | stp BASE, TMP1, L->base
1296 | tbz TMP0, #0, ->fff_fallback
1297 | str xzr, L->cframe
1298 | strb CRET1w, L->status
1299 | b ->vm_leave_unw
1300 |
1301 |//-- Math library -------------------------------------------------------
1302 |
1303 |.macro math_round, func, round
1304 | .ffunc math_ .. func
1305 | ldr CARG1, [BASE]
1306 | cmp NARGS8:RC, #8
1307 | ldr d0, [BASE]
1308 | blo ->fff_fallback
1309 | cmp TISNUMhi, CARG1, lsr #32
1310 | beq ->fff_restv
1311 | round d0, d0
1312 | b ->fff_resn
1313 |.endmacro
1314 |
1315 | math_round floor, frintm
1316 | math_round ceil, frintp
1317 |
1318 |.ffunc_1 math_abs
1319 | checknumber CARG1, ->fff_fallback
1320 | and CARG1, CARG1, #U64x(7fffffff,ffffffff)
1321 | bne ->fff_restv
1322 | eor CARG2w, CARG1w, CARG1w, asr #31
1323 | movz CARG3, #0x41e0, lsl #48 // 2^31.
1324 | subs CARG1w, CARG2w, CARG1w, asr #31
1325 | add CARG1, CARG1, TISNUM
1326 | csel CARG1, CARG1, CARG3, pl
1327 | // Fallthrough.
1328 |
1329 |->fff_restv:
1330 | // CARG1 = TValue result.
1331 | ldr PC, [BASE, FRAME_PC]
1332 | str CARG1, [BASE, #-16]
1333 |->fff_res1:
1334 | // PC = return.
1335 | mov RC, #(1+1)*8
1336 |->fff_res:
1337 | // RC = (nresults+1)*8, PC = return.
1338 | ands CARG1, PC, #FRAME_TYPE
1339 | str RCw, SAVE_MULTRES
1340 | sub RA, BASE, #16
1341 | bne ->vm_return
1342 | ldr INSw, [PC, #-4]
1343 | decode_RB RB, INS
1344 |5:
1345 | cmp RC, RB, lsl #3 // More results expected?
1346 | blo >6
1347 | decode_RA TMP1, INS
1348 | // Adjust BASE. KBASE is assumed to be set for the calling frame.
1349 | sub BASE, RA, TMP1, lsl #3
1350 | ins_next
1351 |
1352 |6: // Fill up results with nil.
1353 | add TMP1, RA, RC
1354 | add RC, RC, #8
1355 | str TISNIL, [TMP1, #-8]
1356 | b <5
1357 |
1358 |.macro math_extern, func
1359 | .ffunc_n math_ .. func
1360 | bl extern func
1361 | b ->fff_resn
1362 |.endmacro
1363 |
1364 |.macro math_extern2, func
1365 | .ffunc_nn math_ .. func
1366 | bl extern func
1367 | b ->fff_resn
1368 |.endmacro
1369 |
1370 |.ffunc_n math_sqrt
1371 | fsqrt d0, d0
1372 |->fff_resn:
1373 | ldr PC, [BASE, FRAME_PC]
1374 | str d0, [BASE, #-16]
1375 | b ->fff_res1
1376 |
1377 |.ffunc math_log
1378 | ldr CARG1, [BASE]
1379 | cmp NARGS8:RC, #8
1380 | ldr FARG1, [BASE]
1381 | bne ->fff_fallback // Need exactly 1 argument.
1382 | checknum CARG1, ->fff_fallback
1383 | bl extern log
1384 | b ->fff_resn
1385 |
1386 | math_extern log10
1387 | math_extern exp
1388 | math_extern sin
1389 | math_extern cos
1390 | math_extern tan
1391 | math_extern asin
1392 | math_extern acos
1393 | math_extern atan
1394 | math_extern sinh
1395 | math_extern cosh
1396 | math_extern tanh
1397 | math_extern2 pow
1398 | math_extern2 atan2
1399 | math_extern2 fmod
1400 |
1401 |.ffunc_2 math_ldexp
1402 | ldr FARG1, [BASE]
1403 | checknum CARG1, ->fff_fallback
1404 | checkint CARG2, ->fff_fallback
1405 | sxtw CARG1, CARG2w
1406 | bl extern ldexp // (double x, int exp)
1407 | b ->fff_resn
1408 |
1409 |.ffunc_n math_frexp
1410 | add CARG1, sp, TMPDofs
1411 | bl extern frexp
1412 | ldr CARG2w, TMPD
1413 | ldr PC, [BASE, FRAME_PC]
1414 | str d0, [BASE, #-16]
1415 | mov RC, #(2+1)*8
1416 | add CARG2, CARG2, TISNUM
1417 | str CARG2, [BASE, #-8]
1418 | b ->fff_res
1419 |
1420 |.ffunc_n math_modf
1421 | sub CARG1, BASE, #16
1422 | ldr PC, [BASE, FRAME_PC]
1423 | bl extern modf
1424 | mov RC, #(2+1)*8
1425 | str d0, [BASE, #-8]
1426 | b ->fff_res
1427 |
1428 |.macro math_minmax, name, cond, fcond
1429 | .ffunc_1 name
1430 | add RB, BASE, RC
1431 | add RA, BASE, #8
1432 | checkint CARG1, >4
1433 |1: // Handle integers.
1434 | ldr CARG2, [RA]
1435 | cmp RA, RB
1436 | bhs ->fff_restv
1437 | checkint CARG2, >3
1438 | cmp CARG1w, CARG2w
1439 | add RA, RA, #8
1440 | csel CARG1, CARG2, CARG1, cond
1441 | b <1
1442 |3: // Convert intermediate result to number and continue below.
1443 | scvtf d0, CARG1w
1444 | blo ->fff_fallback
1445 | ldr d1, [RA]
1446 | b >6
1447 |
1448 |4:
1449 | ldr d0, [BASE]
1450 | blo ->fff_fallback
1451 |5: // Handle numbers.
1452 | ldr CARG2, [RA]
1453 | ldr d1, [RA]
1454 | cmp RA, RB
1455 | bhs ->fff_resn
1456 | checknum CARG2, >7
1457 |6:
1458 | fcmp d0, d1
1459 | add RA, RA, #8
1460 | fcsel d0, d1, d0, fcond
1461 | b <5
1462 |7: // Convert integer to number and continue above.
1463 | scvtf d1, CARG2w
1464 | blo ->fff_fallback
1465 | b <6
1466 |.endmacro
1467 |
1468 | math_minmax math_min, gt, hi
1469 | math_minmax math_max, lt, lo
1470 |
1471 |//-- String library -----------------------------------------------------
1472 |
1473 |.ffunc string_byte // Only handle the 1-arg case here.
1474 | ldp PC, CARG1, [BASE, FRAME_PC]
1475 | cmp NARGS8:RC, #8
1476 | asr ITYPE, CARG1, #47
1477 | ccmn ITYPE, #-LJ_TSTR, #0, eq
1478 | and STR:CARG1, CARG1, #LJ_GCVMASK
1479 | bne ->fff_fallback
1480 | ldrb TMP0w, STR:CARG1[1] // Access is always ok (NUL at end).
1481 | ldr CARG3w, STR:CARG1->len
1482 | add TMP0, TMP0, TISNUM
1483 | str TMP0, [BASE, #-16]
1484 | mov RC, #(0+1)*8
1485 | cbz CARG3, ->fff_res
1486 | b ->fff_res1
1487 |
1488 |.ffunc string_char // Only handle the 1-arg case here.
1489 | ffgccheck
1490 | ldp PC, CARG1, [BASE, FRAME_PC]
1491 | cmp CARG1w, #255
1492 | ccmp NARGS8:RC, #8, #0, ls // Need exactly 1 argument.
1493 | bne ->fff_fallback
1494 | checkint CARG1, ->fff_fallback
1495 | mov CARG3, #1
1496 | mov CARG2, BASE // Points to stack. Little-endian.
1497 |->fff_newstr:
1498 | // CARG2 = str, CARG3 = len.
1499 | str BASE, L->base
1500 | mov CARG1, L
1501 | str PC, SAVE_PC
1502 | bl extern lj_str_new // (lua_State *L, char *str, size_t l)
1503 |->fff_resstr:
1504 | // Returns GCstr *.
1505 | ldr BASE, L->base
1506 | movn TMP1, #~LJ_TSTR
1507 | add CARG1, CARG1, TMP1, lsl #47
1508 | b ->fff_restv
1509 |
1510 |.ffunc string_sub
1511 | ffgccheck
1512 | ldr CARG1, [BASE]
1513 | ldr CARG3, [BASE, #16]
1514 | cmp NARGS8:RC, #16
1515 | movn RB, #0
1516 | beq >1
1517 | blo ->fff_fallback
1518 | checkint CARG3, ->fff_fallback
1519 | sxtw RB, CARG3w
1520 |1:
1521 | ldr CARG2, [BASE, #8]
1522 | checkstr CARG1, ->fff_fallback
1523 | ldr TMP1w, STR:CARG1->len
1524 | checkint CARG2, ->fff_fallback
1525 | sxtw CARG2, CARG2w
1526 | // CARG1 = str, TMP1 = str->len, CARG2 = start, RB = end
1527 | add TMP2, RB, TMP1
1528 | cmp RB, #0
1529 | add TMP0, CARG2, TMP1
1530 | csinc RB, RB, TMP2, ge // if (end < 0) end += len+1
1531 | cmp CARG2, #0
1532 | csinc CARG2, CARG2, TMP0, ge // if (start < 0) start += len+1
1533 | cmp RB, #0
1534 | csel RB, RB, xzr, ge // if (end < 0) end = 0
1535 | cmp CARG2, #1
1536 | csinc CARG2, CARG2, xzr, ge // if (start < 1) start = 1
1537 | cmp RB, TMP1
1538 | csel RB, RB, TMP1, le // if (end > len) end = len
1539 | add CARG1, STR:CARG1, #sizeof(GCstr)-1
1540 | subs CARG3, RB, CARG2 // len = end - start
1541 | add CARG2, CARG1, CARG2
1542 | add CARG3, CARG3, #1 // len += 1
1543 | bge ->fff_newstr
1544 | add STR:CARG1, GL, #offsetof(global_State, strempty)
1545 | movn TMP1, #~LJ_TSTR
1546 | add CARG1, CARG1, TMP1, lsl #47
1547 | b ->fff_restv
1548 |
1549 |.macro ffstring_op, name
1550 | .ffunc string_ .. name
1551 | ffgccheck
1552 | ldr CARG2, [BASE]
1553 | cmp NARGS8:RC, #8
1554 | asr ITYPE, CARG2, #47
1555 | ccmn ITYPE, #-LJ_TSTR, #0, hs
1556 | and STR:CARG2, CARG2, #LJ_GCVMASK
1557 | bne ->fff_fallback
1558 | ldr TMP0, GL->tmpbuf.b
1559 | add SBUF:CARG1, GL, #offsetof(global_State, tmpbuf)
1560 | str BASE, L->base
1561 | str PC, SAVE_PC
1562 | str L, GL->tmpbuf.L
1563 | str TMP0, GL->tmpbuf.p
1564 | bl extern lj_buf_putstr_ .. name
1565 | bl extern lj_buf_tostr
1566 | b ->fff_resstr
1567 |.endmacro
1568 |
1569 |ffstring_op reverse
1570 |ffstring_op lower
1571 |ffstring_op upper
1572 |
1573 |//-- Bit library --------------------------------------------------------
1574 |
1575 |// FP number to bit conversion for soft-float. Clobbers CARG1-CARG3
1576 |->vm_tobit_fb:
1577 | bls ->fff_fallback
1578 | add CARG2, CARG1, CARG1
1579 | mov CARG3, #1076
1580 | sub CARG3, CARG3, CARG2, lsr #53
1581 | cmp CARG3, #53
1582 | bhi >1
1583 | and CARG2, CARG2, #U64x(001fffff,ffffffff)
1584 | orr CARG2, CARG2, #U64x(00200000,00000000)
1585 | cmp CARG1, #0
1586 | lsr CARG2, CARG2, CARG3
1587 | cneg CARG1w, CARG2w, mi
1588 | br lr
1589 |1:
1590 | mov CARG1w, #0
1591 | br lr
1592 |
1593 |.macro .ffunc_bit, name
1594 | .ffunc_1 bit_..name
1595 | adr lr, >1
1596 | checkint CARG1, ->vm_tobit_fb
1597 |1:
1598 |.endmacro
1599 |
1600 |.macro .ffunc_bit_op, name, ins
1601 | .ffunc_bit name
1602 | mov RA, #8
1603 | mov TMP0w, CARG1w
1604 | adr lr, >2
1605 |1:
1606 | ldr CARG1, [BASE, RA]
1607 | cmp RA, NARGS8:RC
1608 | add RA, RA, #8
1609 | bge >9
1610 | checkint CARG1, ->vm_tobit_fb
1611 |2:
1612 | ins TMP0w, TMP0w, CARG1w
1613 | b <1
1614 |.endmacro
1615 |
1616 |.ffunc_bit_op band, and
1617 |.ffunc_bit_op bor, orr
1618 |.ffunc_bit_op bxor, eor
1619 |
1620 |.ffunc_bit tobit
1621 | mov TMP0w, CARG1w
1622 |9: // Label reused by .ffunc_bit_op users.
1623 | add CARG1, TMP0, TISNUM
1624 | b ->fff_restv
1625 |
1626 |.ffunc_bit bswap
1627 | rev TMP0w, CARG1w
1628 | add CARG1, TMP0, TISNUM
1629 | b ->fff_restv
1630 |
1631 |.ffunc_bit bnot
1632 | mvn TMP0w, CARG1w
1633 | add CARG1, TMP0, TISNUM
1634 | b ->fff_restv
1635 |
1636 |.macro .ffunc_bit_sh, name, ins, shmod
1637 | .ffunc bit_..name
1638 | ldp TMP0, CARG1, [BASE]
1639 | cmp NARGS8:RC, #16
1640 | blo ->fff_fallback
1641 | adr lr, >1
1642 | checkint CARG1, ->vm_tobit_fb
1643 |1:
1644 |.if shmod == 0
1645 | mov TMP1, CARG1
1646 |.else
1647 | neg TMP1, CARG1
1648 |.endif
1649 | mov CARG1, TMP0
1650 | adr lr, >2
1651 | checkint CARG1, ->vm_tobit_fb
1652 |2:
1653 | ins TMP0w, CARG1w, TMP1w
1654 | add CARG1, TMP0, TISNUM
1655 | b ->fff_restv
1656 |.endmacro
1657 |
1658 |.ffunc_bit_sh lshift, lsl, 0
1659 |.ffunc_bit_sh rshift, lsr, 0
1660 |.ffunc_bit_sh arshift, asr, 0
1661 |.ffunc_bit_sh rol, ror, 1
1662 |.ffunc_bit_sh ror, ror, 0
1663 |
1664 |//-----------------------------------------------------------------------
1665 |
1666 |->fff_fallback: // Call fast function fallback handler.
1667 | // BASE = new base, RC = nargs*8
1668 | ldp CFUNC:CARG3, PC, [BASE, FRAME_FUNC] // Fallback may overwrite PC.
1669 | ldr TMP2, L->maxstack
1670 | add TMP1, BASE, NARGS8:RC
1671 | stp BASE, TMP1, L->base
1672 | and CFUNC:CARG3, CARG3, #LJ_GCVMASK
1673 | add TMP1, TMP1, #8*LUA_MINSTACK
1674 | ldr CARG3, CFUNC:CARG3->f
1675 | str PC, SAVE_PC // Redundant (but a defined value).
1676 | cmp TMP1, TMP2
1677 | mov CARG1, L
1678 | bhi >5 // Need to grow stack.
1679 | blr CARG3 // (lua_State *L)
1680 | // Either throws an error, or recovers and returns -1, 0 or nresults+1.
1681 | ldr BASE, L->base
1682 | cmp CRET1w, #0
1683 | lsl RC, CRET1, #3
1684 | sub RA, BASE, #16
1685 | bgt ->fff_res // Returned nresults+1?
1686 |1: // Returned 0 or -1: retry fast path.
1687 | ldr CARG1, L->top
1688 | ldr CFUNC:CARG3, [BASE, FRAME_FUNC]
1689 | sub NARGS8:RC, CARG1, BASE
1690 | bne ->vm_call_tail // Returned -1?
1691 | and CFUNC:CARG3, CARG3, #LJ_GCVMASK
1692 | ins_callt // Returned 0: retry fast path.
1693 |
1694 |// Reconstruct previous base for vmeta_call during tailcall.
1695 |->vm_call_tail:
1696 | ands TMP0, PC, #FRAME_TYPE
1697 | and TMP1, PC, #~FRAME_TYPEP
1698 | bne >3
1699 | ldrb RAw, [PC, #-3]
1700 | lsl RA, RA, #3
1701 | add TMP1, RA, #16
1702 |3:
1703 | sub RB, BASE, TMP1
1704 | b ->vm_call_dispatch // Resolve again for tailcall.
1705 |
1706 |5: // Grow stack for fallback handler.
1707 | mov CARG2, #LUA_MINSTACK
1708 | bl extern lj_state_growstack // (lua_State *L, int n)
1709 | ldr BASE, L->base
1710 | cmp CARG1, CARG1 // Set zero-flag to force retry.
1711 | b <1
1712 |
1713 |->fff_gcstep: // Call GC step function.
1714 | // BASE = new base, RC = nargs*8
1715 | add CARG2, BASE, NARGS8:RC // Calculate L->top.
1716 | mov RA, lr
1717 | stp BASE, CARG2, L->base
1718 | str PC, SAVE_PC // Redundant (but a defined value).
1719 | mov CARG1, L
1720 | bl extern lj_gc_step // (lua_State *L)
1721 | ldp BASE, CARG2, L->base
1722 | ldr CFUNC:CARG3, [BASE, FRAME_FUNC]
1723 | mov lr, RA // Help return address predictor.
1724 | sub NARGS8:RC, CARG2, BASE // Calculate nargs*8.
1725 | and CFUNC:CARG3, CARG3, #LJ_GCVMASK
1726 | ret
1727 |
1728 |//-----------------------------------------------------------------------
1729 |//-- Special dispatch targets -------------------------------------------
1730 |//-----------------------------------------------------------------------
1731 |
1732 |->vm_record: // Dispatch target for recording phase.
1733 | NYI
1734 |
1735 |->vm_rethook: // Dispatch target for return hooks.
1736 | ldrb TMP2w, GL->hookmask
1737 | tbz TMP2w, #HOOK_ACTIVE_SHIFT, >1 // Hook already active?
1738 |5: // Re-dispatch to static ins.
1739 | ldr TMP0, [TMP1, #GG_G2DISP+GG_DISP2STATIC]
1740 | br TMP0
1741 |
1742 |->vm_inshook: // Dispatch target for instr/line hooks.
1743 | ldrb TMP2w, GL->hookmask
1744 | ldr TMP3w, GL->hookcount
1745 | tbnz TMP2w, #HOOK_ACTIVE_SHIFT, <5 // Hook already active?
1746 | tst TMP2w, #LUA_MASKLINE|LUA_MASKCOUNT
1747 | beq <5
1748 | sub TMP3w, TMP3w, #1
1749 | str TMP3w, GL->hookcount
1750 | cbz TMP3w, >1
1751 | tbz TMP2w, #LUA_HOOKLINE, <5
1752 |1:
1753 | mov CARG1, L
1754 | str BASE, L->base
1755 | mov CARG2, PC
1756 | // SAVE_PC must hold the _previous_ PC. The callee updates it with PC.
1757 | bl extern lj_dispatch_ins // (lua_State *L, const BCIns *pc)
1758 |3:
1759 | ldr BASE, L->base
1760 |4: // Re-dispatch to static ins.
1761 | ldr INSw, [PC, #-4]
1762 | add TMP1, GL, INS, uxtb #3
1763 | decode_RA RA, INS
1764 | ldr TMP0, [TMP1, #GG_G2DISP+GG_DISP2STATIC]
1765 | decode_RD RC, INS
1766 | br TMP0
1767 |
1768 |->cont_hook: // Continue from hook yield.
1769 | ldr CARG1, [CARG4, #-40]
1770 | add PC, PC, #4
1771 | str CARG1w, SAVE_MULTRES // Restore MULTRES for *M ins.
1772 | b <4
1773 |
1774 |->vm_hotloop: // Hot loop counter underflow.
1775 | NYI
1776 |
1777 |->vm_callhook: // Dispatch target for call hooks.
1778 | mov CARG2, PC
1779 |.if JIT
1780 | b >1
1781 |.endif
1782 |
1783 |->vm_hotcall: // Hot call counter underflow.
1784 |.if JIT
1785 | orr CARG2, PC, #1
1786 |1:
1787 |.endif
1788 | add TMP1, BASE, NARGS8:RC
1789 | str PC, SAVE_PC
1790 | mov CARG1, L
1791 | sub RA, RA, BASE
1792 | stp BASE, TMP1, L->base
1793 | bl extern lj_dispatch_call // (lua_State *L, const BCIns *pc)
1794 | // Returns ASMFunction.
1795 | ldp BASE, TMP1, L->base
1796 | str xzr, SAVE_PC // Invalidate for subsequent line hook.
1797 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC]
1798 | add RA, BASE, RA
1799 | sub NARGS8:RC, TMP1, BASE
1800 | ldr INSw, [PC, #-4]
1801 | and LFUNC:CARG3, CARG3, #LJ_GCVMASK
1802 | br CRET1
1803 |
1804 |->cont_stitch: // Trace stitching.
1805 | NYI
1806 |
1807 |->vm_profhook: // Dispatch target for profiler hook.
1808#if LJ_HASPROFILE
1809 | mov CARG1, L
1810 | str BASE, L->base
1811 | mov CARG2, PC
1812 | bl extern lj_dispatch_profile // (lua_State *L, const BCIns *pc)
1813 | // HOOK_PROFILE is off again, so re-dispatch to dynamic instruction.
1814 | ldr BASE, L->base
1815 | sub PC, PC, #4
1816 | b ->cont_nop
1817#endif
1818 |
1819 |//-----------------------------------------------------------------------
1820 |//-- Trace exit handler -------------------------------------------------
1821 |//-----------------------------------------------------------------------
1822 |
1823 |->vm_exit_handler:
1824 | NYI
1825 |->vm_exit_interp:
1826 | NYI
1827 |
1828 |//-----------------------------------------------------------------------
1829 |//-- Math helper functions ----------------------------------------------
1830 |//-----------------------------------------------------------------------
1831 |
1832 | // int lj_vm_modi(int dividend, int divisor);
1833 |->vm_modi:
1834 | eor CARG4w, CARG1w, CARG2w
1835 | cmp CARG4w, #0
1836 | eor CARG3w, CARG1w, CARG1w, asr #31
1837 | eor CARG4w, CARG2w, CARG2w, asr #31
1838 | sub CARG3w, CARG3w, CARG1w, asr #31
1839 | sub CARG4w, CARG4w, CARG2w, asr #31
1840 | udiv CARG1w, CARG3w, CARG4w
1841 | msub CARG1w, CARG1w, CARG4w, CARG3w
1842 | ccmp CARG1w, #0, #4, mi
1843 | sub CARG3w, CARG1w, CARG4w
1844 | csel CARG1w, CARG1w, CARG3w, eq
1845 | eor CARG3w, CARG1w, CARG2w
1846 | cmp CARG3w, #0
1847 | cneg CARG1w, CARG1w, mi
1848 | ret
1849 |
1850 |//-----------------------------------------------------------------------
1851 |//-- Miscellaneous functions --------------------------------------------
1852 |//-----------------------------------------------------------------------
1853 |
1854 |//-----------------------------------------------------------------------
1855 |//-- FFI helper functions -----------------------------------------------
1856 |//-----------------------------------------------------------------------
1857 |
1858 |// Handler for callback functions.
1859 |// Saveregs already performed. Callback slot number in [sp], g in r12.
1860 |->vm_ffi_callback:
1861 |.if FFI
1862 | NYI
1863 |.endif
1864 |
1865 |->cont_ffi_callback: // Return from FFI callback.
1866 |.if FFI
1867 | NYI
1868 |.endif
1869 |
1870 |->vm_ffi_call: // Call C function via FFI.
1871 | // Caveat: needs special frame unwinding, see below.
1872 |.if FFI
1873 | NYI
1874 |.endif
1875 |// Note: vm_ffi_call must be the last function in this object file!
1876 |
1877 |//-----------------------------------------------------------------------
1878}
1879
1880/* Generate the code for a single instruction. */
1881static void build_ins(BuildCtx *ctx, BCOp op, int defop)
1882{
1883 int vk = 0;
1884 |=>defop:
1885
1886 switch (op) {
1887
1888 /* -- Comparison ops ---------------------------------------------------- */
1889
1890 /* Remember: all ops branch for a true comparison, fall through otherwise. */
1891
1892 case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT:
1893 | // RA = src1, RC = src2, JMP with RC = target
1894 | ldr CARG1, [BASE, RA, lsl #3]
1895 | ldrh RBw, [PC, #2]
1896 | ldr CARG2, [BASE, RC, lsl #3]
1897 | add PC, PC, #4
1898 | add RB, PC, RB, lsl #2
1899 | sub RB, RB, #0x20000
1900 | checkint CARG1, >3
1901 | checkint CARG2, >4
1902 | cmp CARG1w, CARG2w
1903 if (op == BC_ISLT) {
1904 | csel PC, RB, PC, lt
1905 } else if (op == BC_ISGE) {
1906 | csel PC, RB, PC, ge
1907 } else if (op == BC_ISLE) {
1908 | csel PC, RB, PC, le
1909 } else {
1910 | csel PC, RB, PC, gt
1911 }
1912 |1:
1913 | ins_next
1914 |
1915 |3: // RA not int.
1916 | ldr FARG1, [BASE, RA, lsl #3]
1917 | blo ->vmeta_comp
1918 | ldr FARG2, [BASE, RC, lsl #3]
1919 | cmp TISNUMhi, CARG2, lsr #32
1920 | bhi >5
1921 | bne ->vmeta_comp
1922 | // RA number, RC int.
1923 | scvtf FARG2, CARG2w
1924 | b >5
1925 |
1926 |4: // RA int, RC not int
1927 | ldr FARG2, [BASE, RC, lsl #3]
1928 | blo ->vmeta_comp
1929 | // RA int, RC number.
1930 | scvtf FARG1, CARG1w
1931 |
1932 |5: // RA number, RC number
1933 | fcmp FARG1, FARG2
1934 | // To preserve NaN semantics GE/GT branch on unordered, but LT/LE don't.
1935 if (op == BC_ISLT) {
1936 | csel PC, RB, PC, lo
1937 } else if (op == BC_ISGE) {
1938 | csel PC, RB, PC, hs
1939 } else if (op == BC_ISLE) {
1940 | csel PC, RB, PC, ls
1941 } else {
1942 | csel PC, RB, PC, hi
1943 }
1944 | b <1
1945 break;
1946
1947 case BC_ISEQV: case BC_ISNEV:
1948 vk = op == BC_ISEQV;
1949 | // RA = src1, RC = src2, JMP with RC = target
1950 | ldr CARG1, [BASE, RA, lsl #3]
1951 | add RC, BASE, RC, lsl #3
1952 | ldrh RBw, [PC, #2]
1953 | ldr CARG3, [RC]
1954 | add PC, PC, #4
1955 | add RB, PC, RB, lsl #2
1956 | sub RB, RB, #0x20000
1957 | asr ITYPE, CARG3, #47
1958 | cmn ITYPE, #-LJ_TISNUM
1959 if (vk) {
1960 | bls ->BC_ISEQN_Z
1961 } else {
1962 | bls ->BC_ISNEN_Z
1963 }
1964 | // RC is not a number.
1965 | asr TMP0, CARG1, #47
1966 |.if FFI
1967 | // Check if RC or RA is a cdata.
1968 | cmn ITYPE, #-LJ_TCDATA
1969 | ccmn TMP0, #-LJ_TCDATA, #4, ne
1970 | beq ->vmeta_equal_cd
1971 |.endif
1972 | cmp CARG1, CARG3
1973 | bne >2
1974 | // Tag and value are equal.
1975 if (vk) {
1976 |->BC_ISEQV_Z:
1977 | mov PC, RB // Perform branch.
1978 }
1979 |1:
1980 | ins_next
1981 |
1982 |2: // Check if the tags are the same and it's a table or userdata.
1983 | cmp ITYPE, TMP0
1984 | ccmn ITYPE, #-LJ_TISTABUD, #2, eq
1985 if (vk) {
1986 | bhi <1
1987 } else {
1988 | bhi ->BC_ISEQV_Z // Reuse code from opposite instruction.
1989 }
1990 | // Different tables or userdatas. Need to check __eq metamethod.
1991 | // Field metatable must be at same offset for GCtab and GCudata!
1992 | and TAB:CARG2, CARG1, #LJ_GCVMASK
1993 | ldr TAB:TMP2, TAB:CARG2->metatable
1994 if (vk) {
1995 | cbz TAB:TMP2, <1 // No metatable?
1996 | ldrb TMP1w, TAB:TMP2->nomm
1997 | mov CARG4, #0 // ne = 0
1998 | tbnz TMP1w, #MM_eq, <1 // 'no __eq' flag set: done.
1999 } else {
2000 | cbz TAB:TMP2, ->BC_ISEQV_Z // No metatable?
2001 | ldrb TMP1w, TAB:TMP2->nomm
2002 | mov CARG4, #1 // ne = 1.
2003 | tbnz TMP1w, #MM_eq, ->BC_ISEQV_Z // 'no __eq' flag set: done.
2004 }
2005 | b ->vmeta_equal
2006 break;
2007
2008 case BC_ISEQS: case BC_ISNES:
2009 vk = op == BC_ISEQS;
2010 | // RA = src, RC = str_const (~), JMP with RC = target
2011 | ldr CARG1, [BASE, RA, lsl #3]
2012 | mvn RC, RC
2013 | ldrh RBw, [PC, #2]
2014 | ldr CARG2, [KBASE, RC, lsl #3]
2015 | add PC, PC, #4
2016 | movn TMP0, #~LJ_TSTR
2017 |.if FFI
2018 | asr ITYPE, CARG1, #47
2019 |.endif
2020 | add RB, PC, RB, lsl #2
2021 | add CARG2, CARG2, TMP0, lsl #47
2022 | sub RB, RB, #0x20000
2023 |.if FFI
2024 | cmn ITYPE, #-LJ_TCDATA
2025 | beq ->vmeta_equal_cd
2026 |.endif
2027 | cmp CARG1, CARG2
2028 if (vk) {
2029 | csel PC, RB, PC, eq
2030 } else {
2031 | csel PC, RB, PC, ne
2032 }
2033 | ins_next
2034 break;
2035
2036 case BC_ISEQN: case BC_ISNEN:
2037 vk = op == BC_ISEQN;
2038 | // RA = src, RC = num_const (~), JMP with RC = target
2039 | ldr CARG1, [BASE, RA, lsl #3]
2040 | add RC, KBASE, RC, lsl #3
2041 | ldrh RBw, [PC, #2]
2042 | ldr CARG3, [RC]
2043 | add PC, PC, #4
2044 | add RB, PC, RB, lsl #2
2045 | sub RB, RB, #0x20000
2046 if (vk) {
2047 |->BC_ISEQN_Z:
2048 } else {
2049 |->BC_ISNEN_Z:
2050 }
2051 | checkint CARG1, >4
2052 | checkint CARG3, >6
2053 | cmp CARG1w, CARG3w
2054 |1:
2055 if (vk) {
2056 | csel PC, RB, PC, eq
2057 |2:
2058 } else {
2059 |2:
2060 | csel PC, RB, PC, ne
2061 }
2062 |3:
2063 | ins_next
2064 |
2065 |4: // RA not int.
2066 |.if FFI
2067 | blo >7
2068 |.else
2069 | blo <2
2070 |.endif
2071 | ldr FARG1, [BASE, RA, lsl #3]
2072 | ldr FARG2, [RC]
2073 | cmp TISNUMhi, CARG3, lsr #32
2074 | bne >5
2075 | // RA number, RC int.
2076 | scvtf FARG2, CARG3w
2077 |5:
2078 | // RA number, RC number.
2079 | fcmp FARG1, FARG2
2080 | b <1
2081 |
2082 |6: // RA int, RC number
2083 | ldr FARG2, [RC]
2084 | scvtf FARG1, CARG1w
2085 | fcmp FARG1, FARG2
2086 | b <1
2087 |
2088 |.if FFI
2089 |7:
2090 | asr ITYPE, TMP0, #47
2091 | cmn ITYPE, #-LJ_TCDATA
2092 | bne <2
2093 | b ->vmeta_equal_cd
2094 |.endif
2095 break;
2096
2097 case BC_ISEQP: case BC_ISNEP:
2098 vk = op == BC_ISEQP;
2099 | // RA = src, RC = primitive_type (~), JMP with RC = target
2100 | ldr TMP0, [BASE, RA, lsl #3]
2101 | ldrh RBw, [PC, #2]
2102 | add PC, PC, #4
2103 | add RC, RC, #1
2104 | add RB, PC, RB, lsl #2
2105 |.if FFI
2106 | asr ITYPE, TMP0, #47
2107 | cmn ITYPE, #-LJ_TCDATA
2108 | beq ->vmeta_equal_cd
2109 | cmn RC, ITYPE
2110 |.else
2111 | cmn RC, TMP0, asr #47
2112 |.endif
2113 | sub RB, RB, #0x20000
2114 if (vk) {
2115 | csel PC, RB, PC, eq
2116 } else {
2117 | csel PC, RB, PC, ne
2118 }
2119 | ins_next
2120 break;
2121
2122 /* -- Unary test and copy ops ------------------------------------------- */
2123
2124 case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF:
2125 | // RA = dst or unused, RC = src, JMP with RC = target
2126 | ldrh RBw, [PC, #2]
2127 | ldr TMP0, [BASE, RC, lsl #3]
2128 | add PC, PC, #4
2129 | mov_false TMP1
2130 | add RB, PC, RB, lsl #2
2131 | cmp TMP0, TMP1
2132 | sub RB, RB, #0x20000
2133 if (op == BC_ISTC || op == BC_IST) {
2134 if (op == BC_ISTC) {
2135 | csel RA, RA, RC, lo
2136 }
2137 | csel PC, RB, PC, lo
2138 } else {
2139 if (op == BC_ISFC) {
2140 | csel RA, RA, RC, hs
2141 }
2142 | csel PC, RB, PC, hs
2143 }
2144 if (op == BC_ISTC || op == BC_ISFC) {
2145 | str TMP0, [BASE, RA, lsl #3]
2146 }
2147 | ins_next
2148 break;
2149
2150 case BC_ISTYPE:
2151 | // RA = src, RC = -type
2152 | ldr TMP0, [BASE, RA, lsl #3]
2153 | cmn RC, TMP0, asr #47
2154 | bne ->vmeta_istype
2155 | ins_next
2156 break;
2157 case BC_ISNUM:
2158 | // RA = src, RC = -(TISNUM-1)
2159 | ldr TMP0, [BASE, RA]
2160 | checknum TMP0, ->vmeta_istype
2161 | ins_next
2162 break;
2163
2164 /* -- Unary ops --------------------------------------------------------- */
2165
2166 case BC_MOV:
2167 | // RA = dst, RC = src
2168 | ldr TMP0, [BASE, RC, lsl #3]
2169 | str TMP0, [BASE, RA, lsl #3]
2170 | ins_next
2171 break;
2172 case BC_NOT:
2173 | // RA = dst, RC = src
2174 | ldr TMP0, [BASE, RC, lsl #3]
2175 | mov_false TMP1
2176 | mov_true TMP2
2177 | cmp TMP0, TMP1
2178 | csel TMP0, TMP1, TMP2, lo
2179 | str TMP0, [BASE, RA, lsl #3]
2180 | ins_next
2181 break;
2182 case BC_UNM:
2183 | // RA = dst, RC = src
2184 | ldr TMP0, [BASE, RC, lsl #3]
2185 | asr ITYPE, TMP0, #47
2186 | cmn ITYPE, #-LJ_TISNUM
2187 | bhi ->vmeta_unm
2188 | eor TMP0, TMP0, #U64x(80000000,00000000)
2189 | bne >5
2190 | negs TMP0w, TMP0w
2191 | movz CARG3, #0x41e0, lsl #48 // 2^31.
2192 | add TMP0, TMP0, TISNUM
2193 | csel TMP0, TMP0, CARG3, vc
2194 |5:
2195 | str TMP0, [BASE, RA, lsl #3]
2196 | ins_next
2197 break;
2198 case BC_LEN:
2199 | // RA = dst, RC = src
2200 | ldr CARG1, [BASE, RC, lsl #3]
2201 | asr ITYPE, CARG1, #47
2202 | cmn ITYPE, #-LJ_TSTR
2203 | and CARG1, CARG1, #LJ_GCVMASK
2204 | bne >2
2205 | ldr CARG1w, STR:CARG1->len
2206 |1:
2207 | add CARG1, CARG1, TISNUM
2208 | str CARG1, [BASE, RA, lsl #3]
2209 | ins_next
2210 |
2211 |2:
2212 | cmn ITYPE, #-LJ_TTAB
2213 | bne ->vmeta_len
2214#if LJ_52
2215 | ldr TAB:CARG2, TAB:CARG1->metatable
2216 | cbnz TAB:CARG2, >9
2217 |3:
2218#endif
2219 |->BC_LEN_Z:
2220 | bl extern lj_tab_len // (GCtab *t)
2221 | // Returns uint32_t (but less than 2^31).
2222 | b <1
2223 |
2224#if LJ_52
2225 |9:
2226 | ldrb TMP1w, TAB:CARG2->nomm
2227 | tbnz TMP1w, #MM_len, <3 // 'no __len' flag set: done.
2228 | b ->vmeta_len
2229#endif
2230 break;
2231
2232 /* -- Binary ops -------------------------------------------------------- */
2233
2234 |.macro ins_arithcheck_int, target
2235 | checkint CARG1, target
2236 | checkint CARG2, target
2237 |.endmacro
2238 |
2239 |.macro ins_arithcheck_num, target
2240 | checknum CARG1, target
2241 | checknum CARG2, target
2242 |.endmacro
2243 |
2244 |.macro ins_arithcheck_nzdiv, target
2245 | cbz CARG2w, target
2246 |.endmacro
2247 |
2248 |.macro ins_arithhead
2249 ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
2250 ||if (vk == 1) {
2251 | and RC, RC, #255
2252 | decode_RB RB, INS
2253 ||} else {
2254 | decode_RB RB, INS
2255 | and RC, RC, #255
2256 ||}
2257 |.endmacro
2258 |
2259 |.macro ins_arithload, reg1, reg2
2260 | // RA = dst, RB = src1, RC = src2 | num_const
2261 ||switch (vk) {
2262 ||case 0:
2263 | ldr reg1, [BASE, RB, lsl #3]
2264 | ldr reg2, [KBASE, RC, lsl #3]
2265 || break;
2266 ||case 1:
2267 | ldr reg1, [KBASE, RC, lsl #3]
2268 | ldr reg2, [BASE, RB, lsl #3]
2269 || break;
2270 ||default:
2271 | ldr reg1, [BASE, RB, lsl #3]
2272 | ldr reg2, [BASE, RC, lsl #3]
2273 || break;
2274 ||}
2275 |.endmacro
2276 |
2277 |.macro ins_arithfallback, ins
2278 ||switch (vk) {
2279 ||case 0:
2280 | ins ->vmeta_arith_vn
2281 || break;
2282 ||case 1:
2283 | ins ->vmeta_arith_nv
2284 || break;
2285 ||default:
2286 | ins ->vmeta_arith_vv
2287 || break;
2288 ||}
2289 |.endmacro
2290 |
2291 |.macro ins_arithmod, res, reg1, reg2
2292 | fdiv d2, reg1, reg2
2293 | frintm d2, d2
2294 | fmsub res, d2, reg2, reg1
2295 |.endmacro
2296 |
2297 |.macro ins_arithdn, intins, fpins
2298 | ins_arithhead
2299 | ins_arithload CARG1, CARG2
2300 | ins_arithcheck_int >5
2301 |.if "intins" == "smull"
2302 | smull CARG1, CARG1w, CARG2w
2303 | cmp CARG1, CARG1, sxtw
2304 | mov CARG1w, CARG1w
2305 | ins_arithfallback bne
2306 |.elif "intins" == "ins_arithmodi"
2307 | ins_arithfallback ins_arithcheck_nzdiv
2308 | bl ->vm_modi
2309 |.else
2310 | intins CARG1w, CARG1w, CARG2w
2311 | ins_arithfallback bvs
2312 |.endif
2313 | add CARG1, CARG1, TISNUM
2314 | str CARG1, [BASE, RA, lsl #3]
2315 |4:
2316 | ins_next
2317 |
2318 |5: // FP variant.
2319 | ins_arithload FARG1, FARG2
2320 | ins_arithfallback ins_arithcheck_num
2321 | fpins FARG1, FARG1, FARG2
2322 | str FARG1, [BASE, RA, lsl #3]
2323 | b <4
2324 |.endmacro
2325 |
2326 |.macro ins_arithfp, fpins
2327 | ins_arithhead
2328 | ins_arithload CARG1, CARG2
2329 | ins_arithload FARG1, FARG2
2330 | ins_arithfallback ins_arithcheck_num
2331 |.if "fpins" == "fpow"
2332 | bl extern pow
2333 |.else
2334 | fpins FARG1, FARG1, FARG2
2335 |.endif
2336 | str FARG1, [BASE, RA, lsl #3]
2337 | ins_next
2338 |.endmacro
2339
2340 case BC_ADDVN: case BC_ADDNV: case BC_ADDVV:
2341 | ins_arithdn adds, fadd
2342 break;
2343 case BC_SUBVN: case BC_SUBNV: case BC_SUBVV:
2344 | ins_arithdn subs, fsub
2345 break;
2346 case BC_MULVN: case BC_MULNV: case BC_MULVV:
2347 | ins_arithdn smull, fmul
2348 break;
2349 case BC_DIVVN: case BC_DIVNV: case BC_DIVVV:
2350 | ins_arithfp fdiv
2351 break;
2352 case BC_MODVN: case BC_MODNV: case BC_MODVV:
2353 | ins_arithdn ins_arithmodi, ins_arithmod
2354 break;
2355 case BC_POW:
2356 | // NYI: (partial) integer arithmetic.
2357 | ins_arithfp fpow
2358 break;
2359
2360 case BC_CAT:
2361 | decode_RB RB, INS
2362 | and RC, RC, #255
2363 | // RA = dst, RB = src_start, RC = src_end
2364 | str BASE, L->base
2365 | sub CARG3, RC, RB
2366 | add CARG2, BASE, RC, lsl #3
2367 |->BC_CAT_Z:
2368 | // RA = dst, CARG2 = top-1, CARG3 = left
2369 | mov CARG1, L
2370 | str PC, SAVE_PC
2371 | bl extern lj_meta_cat // (lua_State *L, TValue *top, int left)
2372 | // Returns NULL (finished) or TValue * (metamethod).
2373 | ldrb RBw, [PC, #-1]
2374 | ldr BASE, L->base
2375 | cbnz CRET1, ->vmeta_binop
2376 | ldr TMP0, [BASE, RB, lsl #3]
2377 | str TMP0, [BASE, RA, lsl #3] // Copy result to RA.
2378 | ins_next
2379 break;
2380
2381 /* -- Constant ops ------------------------------------------------------ */
2382
2383 case BC_KSTR:
2384 | // RA = dst, RC = str_const (~)
2385 | mvn RC, RC
2386 | ldr TMP0, [KBASE, RC, lsl #3]
2387 | movn TMP1, #~LJ_TSTR
2388 | add TMP0, TMP0, TMP1, lsl #47
2389 | str TMP0, [BASE, RA, lsl #3]
2390 | ins_next
2391 break;
2392 case BC_KCDATA:
2393 |.if FFI
2394 | // RA = dst, RC = cdata_const (~)
2395 | mvn RC, RC
2396 | ldr TMP0, [KBASE, RC, lsl #3]
2397 | movn TMP1, #~LJ_TCDATA
2398 | add TMP0, TMP0, TMP1, lsl #47
2399 | str TMP0, [BASE, RA, lsl #3]
2400 | ins_next
2401 |.endif
2402 break;
2403 case BC_KSHORT:
2404 | // RA = dst, RC = int16_literal
2405 | sxth RCw, RCw
2406 | add TMP0, RC, TISNUM
2407 | str TMP0, [BASE, RA, lsl #3]
2408 | ins_next
2409 break;
2410 case BC_KNUM:
2411 | // RA = dst, RC = num_const
2412 | ldr TMP0, [KBASE, RC, lsl #3]
2413 | str TMP0, [BASE, RA, lsl #3]
2414 | ins_next
2415 break;
2416 case BC_KPRI:
2417 | // RA = dst, RC = primitive_type (~)
2418 | mvn TMP0, RC, lsl #47
2419 | str TMP0, [BASE, RA, lsl #3]
2420 | ins_next
2421 break;
2422 case BC_KNIL:
2423 | // RA = base, RC = end
2424 | add RA, BASE, RA, lsl #3
2425 | add RC, BASE, RC, lsl #3
2426 | str TISNIL, [RA], #8
2427 |1:
2428 | cmp RA, RC
2429 | str TISNIL, [RA], #8
2430 | blt <1
2431 | ins_next_
2432 break;
2433
2434 /* -- Upvalue and function ops ------------------------------------------ */
2435
2436 case BC_UGET:
2437 | // RA = dst, RC = uvnum
2438 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
2439 | add RC, RC, #offsetof(GCfuncL, uvptr)/8
2440 | and LFUNC:CARG2, CARG2, #LJ_GCVMASK
2441 | ldr UPVAL:CARG2, [LFUNC:CARG2, RC, lsl #3]
2442 | ldr CARG2, UPVAL:CARG2->v
2443 | ldr TMP0, [CARG2]
2444 | str TMP0, [BASE, RA, lsl #3]
2445 | ins_next
2446 break;
2447 case BC_USETV:
2448 | // RA = uvnum, RC = src
2449 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
2450 | add RA, RA, #offsetof(GCfuncL, uvptr)/8
2451 | and LFUNC:CARG2, CARG2, #LJ_GCVMASK
2452 | ldr UPVAL:CARG1, [LFUNC:CARG2, RA, lsl #3]
2453 | ldr CARG3, [BASE, RC, lsl #3]
2454 | ldr CARG2, UPVAL:CARG1->v
2455 | ldrb TMP2w, UPVAL:CARG1->marked
2456 | ldrb TMP0w, UPVAL:CARG1->closed
2457 | asr ITYPE, CARG3, #47
2458 | str CARG3, [CARG2]
2459 | add ITYPE, ITYPE, #-LJ_TISGCV
2460 | tst TMP2w, #LJ_GC_BLACK // isblack(uv)
2461 | ccmp TMP0w, #0, #4, ne // && uv->closed
2462 | ccmn ITYPE, #-(LJ_TNUMX - LJ_TISGCV), #0, ne // && tvisgcv(v)
2463 | bhi >2
2464 |1:
2465 | ins_next
2466 |
2467 |2: // Check if new value is white.
2468 | and GCOBJ:CARG3, CARG3, #LJ_GCVMASK
2469 | ldrb TMP1w, GCOBJ:CARG3->gch.marked
2470 | tst TMP1w, #LJ_GC_WHITES // iswhite(str)
2471 | beq <1
2472 | // Crossed a write barrier. Move the barrier forward.
2473 | mov CARG1, GL
2474 | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv)
2475 | b <1
2476 break;
2477 case BC_USETS:
2478 | // RA = uvnum, RC = str_const (~)
2479 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
2480 | add RA, RA, #offsetof(GCfuncL, uvptr)/8
2481 | mvn RC, RC
2482 | and LFUNC:CARG2, CARG2, #LJ_GCVMASK
2483 | ldr UPVAL:CARG1, [LFUNC:CARG2, RA, lsl #3]
2484 | ldr STR:CARG3, [KBASE, RC, lsl #3]
2485 | movn TMP0, #~LJ_TSTR
2486 | ldr CARG2, UPVAL:CARG1->v
2487 | ldrb TMP2w, UPVAL:CARG1->marked
2488 | add TMP0, STR:CARG3, TMP0, lsl #47
2489 | ldrb TMP1w, STR:CARG3->marked
2490 | str TMP0, [CARG2]
2491 | tbnz TMP2w, #2, >2 // isblack(uv)
2492 |1:
2493 | ins_next
2494 |
2495 |2: // Check if string is white and ensure upvalue is closed.
2496 | ldrb TMP0w, UPVAL:CARG1->closed
2497 | tst TMP1w, #LJ_GC_WHITES // iswhite(str)
2498 | ccmp TMP0w, #0, #0, ne
2499 | beq <1
2500 | // Crossed a write barrier. Move the barrier forward.
2501 | mov CARG1, GL
2502 | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv)
2503 | b <1
2504 break;
2505 case BC_USETN:
2506 | // RA = uvnum, RC = num_const
2507 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
2508 | add RA, RA, #offsetof(GCfuncL, uvptr)/8
2509 | and LFUNC:CARG2, CARG2, #LJ_GCVMASK
2510 | ldr UPVAL:CARG2, [LFUNC:CARG2, RA, lsl #3]
2511 | ldr TMP0, [KBASE, RC, lsl #3]
2512 | ldr CARG2, UPVAL:CARG2->v
2513 | str TMP0, [CARG2]
2514 | ins_next
2515 break;
2516 case BC_USETP:
2517 | // RA = uvnum, RC = primitive_type (~)
2518 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
2519 | add RA, RA, #offsetof(GCfuncL, uvptr)/8
2520 | and LFUNC:CARG2, CARG2, #LJ_GCVMASK
2521 | ldr UPVAL:CARG2, [LFUNC:CARG2, RA, lsl #3]
2522 | mvn TMP0, RC, lsl #47
2523 | ldr CARG2, UPVAL:CARG2->v
2524 | str TMP0, [CARG2]
2525 | ins_next
2526 break;
2527
2528 case BC_UCLO:
2529 | // RA = level, RC = target
2530 | ldr CARG3, L->openupval
2531 | add RC, PC, RC, lsl #2
2532 | str BASE, L->base
2533 | sub PC, RC, #0x20000
2534 | cbz CARG3, >1
2535 | mov CARG1, L
2536 | add CARG2, BASE, RA, lsl #3
2537 | bl extern lj_func_closeuv // (lua_State *L, TValue *level)
2538 | ldr BASE, L->base
2539 |1:
2540 | ins_next
2541 break;
2542
2543 case BC_FNEW:
2544 | // RA = dst, RC = proto_const (~) (holding function prototype)
2545 | mvn RC, RC
2546 | str BASE, L->base
2547 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC]
2548 | str PC, SAVE_PC
2549 | ldr CARG2, [KBASE, RC, lsl #3]
2550 | mov CARG1, L
2551 | and LFUNC:CARG3, CARG3, #LJ_GCVMASK
2552 | // (lua_State *L, GCproto *pt, GCfuncL *parent)
2553 | bl extern lj_func_newL_gc
2554 | // Returns GCfuncL *.
2555 | ldr BASE, L->base
2556 | movn TMP0, #~LJ_TFUNC
2557 | add CRET1, CRET1, TMP0, lsl #47
2558 | str CRET1, [BASE, RA, lsl #3]
2559 | ins_next
2560 break;
2561
2562 /* -- Table ops --------------------------------------------------------- */
2563
2564 case BC_TNEW:
2565 case BC_TDUP:
2566 | // RA = dst, RC = (hbits|asize) | tab_const (~)
2567 | ldp CARG3, CARG4, GL->gc.total // Assumes threshold follows total.
2568 | str BASE, L->base
2569 | str PC, SAVE_PC
2570 | mov CARG1, L
2571 | cmp CARG3, CARG4
2572 | bhs >5
2573 |1:
2574 if (op == BC_TNEW) {
2575 | and CARG2, RC, #0x7ff
2576 | lsr CARG3, RC, #11
2577 | cmp CARG2, #0x7ff
2578 | mov TMP0, #0x801
2579 | csel CARG2, CARG2, TMP0, ne
2580 | bl extern lj_tab_new // (lua_State *L, int32_t asize, uint32_t hbits)
2581 | // Returns GCtab *.
2582 } else {
2583 | mvn RC, RC
2584 | ldr CARG2, [KBASE, RC, lsl #3]
2585 | bl extern lj_tab_dup // (lua_State *L, Table *kt)
2586 | // Returns GCtab *.
2587 }
2588 | ldr BASE, L->base
2589 | movk CRET1, #(LJ_TTAB>>1)&0xffff, lsl #48
2590 | str CRET1, [BASE, RA, lsl #3]
2591 | ins_next
2592 |
2593 |5:
2594 | bl extern lj_gc_step_fixtop // (lua_State *L)
2595 | mov CARG1, L
2596 | b <1
2597 break;
2598
2599 case BC_GGET:
2600 | // RA = dst, RC = str_const (~)
2601 case BC_GSET:
2602 | // RA = dst, RC = str_const (~)
2603 | ldr LFUNC:CARG1, [BASE, FRAME_FUNC]
2604 | mvn RC, RC
2605 | and LFUNC:CARG1, CARG1, #LJ_GCVMASK
2606 | ldr TAB:CARG2, LFUNC:CARG1->env
2607 | ldr STR:RC, [KBASE, RC, lsl #3]
2608 if (op == BC_GGET) {
2609 | b ->BC_TGETS_Z
2610 } else {
2611 | b ->BC_TSETS_Z
2612 }
2613 break;
2614
2615 case BC_TGETV:
2616 | decode_RB RB, INS
2617 | and RC, RC, #255
2618 | // RA = dst, RB = table, RC = key
2619 | ldr CARG2, [BASE, RB, lsl #3]
2620 | ldr TMP1, [BASE, RC, lsl #3]
2621 | checktab CARG2, ->vmeta_tgetv
2622 | checkint TMP1, >9 // Integer key?
2623 | ldr CARG3, TAB:CARG2->array
2624 | ldr CARG1w, TAB:CARG2->asize
2625 | add CARG3, CARG3, TMP1, uxtw #3
2626 | cmp TMP1w, CARG1w // In array part?
2627 | bhs ->vmeta_tgetv
2628 | ldr TMP0, [CARG3]
2629 | cmp TMP0, TISNIL
2630 | beq >5
2631 |1:
2632 | str TMP0, [BASE, RA, lsl #3]
2633 | ins_next
2634 |
2635 |5: // Check for __index if table value is nil.
2636 | ldr TAB:CARG1, TAB:CARG2->metatable
2637 | cbz TAB:CARG1, <1 // No metatable: done.
2638 | ldrb TMP1w, TAB:CARG1->nomm
2639 | tbnz TMP1w, #MM_index, <1 // 'no __index' flag set: done.
2640 | b ->vmeta_tgetv
2641 |
2642 |9:
2643 | asr ITYPE, TMP1, #47
2644 | cmn ITYPE, #-LJ_TSTR // String key?
2645 | bne ->vmeta_tgetv
2646 | and STR:RC, TMP1, #LJ_GCVMASK
2647 | b ->BC_TGETS_Z
2648 break;
2649 case BC_TGETS:
2650 | decode_RB RB, INS
2651 | and RC, RC, #255
2652 | // RA = dst, RB = table, RC = str_const (~)
2653 | ldr CARG2, [BASE, RB, lsl #3]
2654 | mvn RC, RC
2655 | ldr STR:RC, [KBASE, RC, lsl #3]
2656 | checktab CARG2, ->vmeta_tgets1
2657 |->BC_TGETS_Z:
2658 | // TAB:CARG2 = GCtab *, STR:RC = GCstr *, RA = dst
2659 | ldr TMP1w, TAB:CARG2->hmask
2660 | ldr TMP2w, STR:RC->hash
2661 | ldr NODE:CARG3, TAB:CARG2->node
2662 | and TMP1w, TMP1w, TMP2w // idx = str->hash & tab->hmask
2663 | add TMP1, TMP1, TMP1, lsl #1
2664 | movn CARG4, #~LJ_TSTR
2665 | add NODE:CARG3, NODE:CARG3, TMP1, lsl #3 // node = tab->node + idx*3*8
2666 | add CARG4, STR:RC, CARG4, lsl #47 // Tagged key to look for.
2667 |1:
2668 | ldp TMP0, CARG1, NODE:CARG3->val
2669 | ldr NODE:CARG3, NODE:CARG3->next
2670 | cmp CARG1, CARG4
2671 | bne >4
2672 | cmp TMP0, TISNIL
2673 | beq >5
2674 |3:
2675 | str TMP0, [BASE, RA, lsl #3]
2676 | ins_next
2677 |
2678 |4: // Follow hash chain.
2679 | cbnz NODE:CARG3, <1
2680 | // End of hash chain: key not found, nil result.
2681 | mov TMP0, TISNIL
2682 |
2683 |5: // Check for __index if table value is nil.
2684 | ldr TAB:CARG1, TAB:CARG2->metatable
2685 | cbz TAB:CARG1, <3 // No metatable: done.
2686 | ldrb TMP1w, TAB:CARG1->nomm
2687 | tbnz TMP1w, #MM_index, <3 // 'no __index' flag set: done.
2688 | b ->vmeta_tgets
2689 break;
2690 case BC_TGETB:
2691 | decode_RB RB, INS
2692 | and RC, RC, #255
2693 | // RA = dst, RB = table, RC = index
2694 | ldr CARG2, [BASE, RB, lsl #3]
2695 | checktab CARG2, ->vmeta_tgetb
2696 | ldr CARG3, TAB:CARG2->array
2697 | ldr CARG1w, TAB:CARG2->asize
2698 | add CARG3, CARG3, RC, lsl #3
2699 | cmp RCw, CARG1w // In array part?
2700 | bhs ->vmeta_tgetb
2701 | ldr TMP0, [CARG3]
2702 | cmp TMP0, TISNIL
2703 | beq >5
2704 |1:
2705 | str TMP0, [BASE, RA, lsl #3]
2706 | ins_next
2707 |
2708 |5: // Check for __index if table value is nil.
2709 | ldr TAB:CARG1, TAB:CARG2->metatable
2710 | cbz TAB:CARG1, <1 // No metatable: done.
2711 | ldrb TMP1w, TAB:CARG1->nomm
2712 | tbnz TMP1w, #MM_index, <1 // 'no __index' flag set: done.
2713 | b ->vmeta_tgetb
2714 break;
2715 case BC_TGETR:
2716 | decode_RB RB, INS
2717 | and RC, RC, #255
2718 | // RA = dst, RB = table, RC = key
2719 | ldr CARG1, [BASE, RB, lsl #3]
2720 | ldr TMP1, [BASE, RC, lsl #3]
2721 | and TAB:CARG1, CARG1, #LJ_GCVMASK
2722 | ldr CARG3, TAB:CARG1->array
2723 | ldr TMP2w, TAB:CARG1->asize
2724 | add CARG3, CARG3, TMP1w, uxtw #3
2725 | cmp TMP1w, TMP2w // In array part?
2726 | bhs ->vmeta_tgetr
2727 | ldr TMP0, [CARG3]
2728 |->BC_TGETR_Z:
2729 | str TMP0, [BASE, RA, lsl #3]
2730 | ins_next
2731 break;
2732
2733 case BC_TSETV:
2734 | decode_RB RB, INS
2735 | and RC, RC, #255
2736 | // RA = src, RB = table, RC = key
2737 | ldr CARG2, [BASE, RB, lsl #3]
2738 | ldr TMP1, [BASE, RC, lsl #3]
2739 | checktab CARG2, ->vmeta_tsetv
2740 | checkint TMP1, >9 // Integer key?
2741 | ldr CARG3, TAB:CARG2->array
2742 | ldr CARG1w, TAB:CARG2->asize
2743 | add CARG3, CARG3, TMP1, uxtw #3
2744 | cmp TMP1w, CARG1w // In array part?
2745 | bhs ->vmeta_tsetv
2746 | ldr TMP1, [CARG3]
2747 | ldr TMP0, [BASE, RA, lsl #3]
2748 | ldrb TMP2w, TAB:CARG2->marked
2749 | cmp TMP1, TISNIL // Previous value is nil?
2750 | beq >5
2751 |1:
2752 | str TMP0, [CARG3]
2753 | tbnz TMP2w, #2, >7 // isblack(table)
2754 |2:
2755 | ins_next
2756 |
2757 |5: // Check for __newindex if previous value is nil.
2758 | ldr TAB:CARG1, TAB:CARG2->metatable
2759 | cbz TAB:CARG1, <1 // No metatable: done.
2760 | ldrb TMP1w, TAB:CARG1->nomm
2761 | tbnz TMP1w, #MM_newindex, <1 // 'no __newindex' flag set: done.
2762 | b ->vmeta_tsetv
2763 |
2764 |7: // Possible table write barrier for the value. Skip valiswhite check.
2765 | barrierback TAB:CARG2, TMP2w, TMP1
2766 | b <2
2767 |
2768 |9:
2769 | asr ITYPE, TMP1, #47
2770 | cmn ITYPE, #-LJ_TSTR // String key?
2771 | bne ->vmeta_tsetv
2772 | and STR:RC, TMP1, #LJ_GCVMASK
2773 | b ->BC_TSETS_Z
2774 break;
2775 case BC_TSETS:
2776 | decode_RB RB, INS
2777 | and RC, RC, #255
2778 | // RA = dst, RB = table, RC = str_const (~)
2779 | ldr CARG2, [BASE, RB, lsl #3]
2780 | mvn RC, RC
2781 | ldr STR:RC, [KBASE, RC, lsl #3]
2782 | checktab CARG2, ->vmeta_tsets1
2783 |->BC_TSETS_Z:
2784 | // TAB:CARG2 = GCtab *, STR:RC = GCstr *, RA = src
2785 | ldr TMP1w, TAB:CARG2->hmask
2786 | ldr TMP2w, STR:RC->hash
2787 | ldr NODE:CARG3, TAB:CARG2->node
2788 | and TMP1w, TMP1w, TMP2w // idx = str->hash & tab->hmask
2789 | add TMP1, TMP1, TMP1, lsl #1
2790 | movn CARG4, #~LJ_TSTR
2791 | add NODE:CARG3, NODE:CARG3, TMP1, lsl #3 // node = tab->node + idx*3*8
2792 | add CARG4, STR:RC, CARG4, lsl #47 // Tagged key to look for.
2793 | strb wzr, TAB:CARG2->nomm // Clear metamethod cache.
2794 |1:
2795 | ldp TMP1, CARG1, NODE:CARG3->val
2796 | ldr NODE:TMP3, NODE:CARG3->next
2797 | ldrb TMP2w, TAB:CARG2->marked
2798 | cmp CARG1, CARG4
2799 | bne >5
2800 | ldr TMP0, [BASE, RA, lsl #3]
2801 | cmp TMP1, TISNIL // Previous value is nil?
2802 | beq >4
2803 |2:
2804 | str TMP0, NODE:CARG3->val
2805 | tbnz TMP2w, #2, >7 // isblack(table)
2806 |3:
2807 | ins_next
2808 |
2809 |4: // Check for __newindex if previous value is nil.
2810 | ldr TAB:CARG1, TAB:CARG2->metatable
2811 | cbz TAB:CARG1, <2 // No metatable: done.
2812 | ldrb TMP1w, TAB:CARG1->nomm
2813 | tbnz TMP1w, #MM_newindex, <2 // 'no __newindex' flag set: done.
2814 | b ->vmeta_tsets
2815 |
2816 |5: // Follow hash chain.
2817 | mov NODE:CARG3, NODE:TMP3
2818 | cbnz NODE:TMP3, <1
2819 | // End of hash chain: key not found, add a new one.
2820 |
2821 | // But check for __newindex first.
2822 | ldr TAB:CARG1, TAB:CARG2->metatable
2823 | cbz TAB:CARG1, >6 // No metatable: continue.
2824 | ldrb TMP1w, TAB:CARG1->nomm
2825 | // 'no __newindex' flag NOT set: check.
2826 | tbz TMP1w, #MM_newindex, ->vmeta_tsets
2827 |6:
2828 | movn TMP1, #~LJ_TSTR
2829 | str PC, SAVE_PC
2830 | add TMP0, STR:RC, TMP1, lsl #47
2831 | str BASE, L->base
2832 | mov CARG1, L
2833 | str TMP0, TMPD
2834 | add CARG3, sp, TMPDofs
2835 | bl extern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k)
2836 | // Returns TValue *.
2837 | ldr BASE, L->base
2838 | ldr TMP0, [BASE, RA, lsl #3]
2839 | str TMP0, [CRET1]
2840 | b <3 // No 2nd write barrier needed.
2841 |
2842 |7: // Possible table write barrier for the value. Skip valiswhite check.
2843 | barrierback TAB:CARG2, TMP2w, TMP1
2844 | b <3
2845 break;
2846 case BC_TSETB:
2847 | decode_RB RB, INS
2848 | and RC, RC, #255
2849 | // RA = src, RB = table, RC = index
2850 | ldr CARG2, [BASE, RB, lsl #3]
2851 | checktab CARG2, ->vmeta_tsetb
2852 | ldr CARG3, TAB:CARG2->array
2853 | ldr CARG1w, TAB:CARG2->asize
2854 | add CARG3, CARG3, RC, lsl #3
2855 | cmp RCw, CARG1w // In array part?
2856 | bhs ->vmeta_tsetb
2857 | ldr TMP1, [CARG3]
2858 | ldr TMP0, [BASE, RA, lsl #3]
2859 | ldrb TMP2w, TAB:CARG2->marked
2860 | cmp TMP1, TISNIL // Previous value is nil?
2861 | beq >5
2862 |1:
2863 | str TMP0, [CARG3]
2864 | tbnz TMP2w, #2, >7 // isblack(table)
2865 |2:
2866 | ins_next
2867 |
2868 |5: // Check for __newindex if previous value is nil.
2869 | ldr TAB:CARG1, TAB:CARG2->metatable
2870 | cbz TAB:CARG1, <1 // No metatable: done.
2871 | ldrb TMP1w, TAB:CARG1->nomm
2872 | tbnz TMP1w, #MM_newindex, <1 // 'no __newindex' flag set: done.
2873 | b ->vmeta_tsetb
2874 |
2875 |7: // Possible table write barrier for the value. Skip valiswhite check.
2876 | barrierback TAB:CARG2, TMP2w, TMP1
2877 | b <2
2878 break;
2879 case BC_TSETR:
2880 | decode_RB RB, INS
2881 | and RC, RC, #255
2882 | // RA = src, RB = table, RC = key
2883 | ldr CARG2, [BASE, RB, lsl #3]
2884 | ldr TMP1, [BASE, RC, lsl #3]
2885 | and TAB:CARG2, CARG2, #LJ_GCVMASK
2886 | ldr CARG1, TAB:CARG2->array
2887 | ldrb TMP2w, TAB:CARG2->marked
2888 | ldr CARG4w, TAB:CARG2->asize
2889 | add CARG1, CARG1, TMP1, uxtw #3
2890 | tbnz TMP2w, #2, >7 // isblack(table)
2891 |2:
2892 | cmp TMP1w, CARG4w // In array part?
2893 | bhs ->vmeta_tsetr
2894 |->BC_TSETR_Z:
2895 | ldr TMP0, [BASE, RA, lsl #3]
2896 | str TMP0, [CARG1]
2897 | ins_next
2898 |
2899 |7: // Possible table write barrier for the value. Skip valiswhite check.
2900 | barrierback TAB:CARG2, TMP2w, TMP0
2901 | b <2
2902 break;
2903
2904 case BC_TSETM:
2905 | // RA = base (table at base-1), RC = num_const (start index)
2906 | add RA, BASE, RA, lsl #3
2907 |1:
2908 | ldr RBw, SAVE_MULTRES
2909 | ldr TAB:CARG2, [RA, #-8] // Guaranteed to be a table.
2910 | ldr TMP1, [KBASE, RC, lsl #3] // Integer constant is in lo-word.
2911 | sub RB, RB, #8
2912 | cbz RB, >4 // Nothing to copy?
2913 | and TAB:CARG2, CARG2, #LJ_GCVMASK
2914 | ldr CARG1w, TAB:CARG2->asize
2915 | add CARG3w, TMP1w, RBw, lsr #3
2916 | ldr CARG4, TAB:CARG2->array
2917 | cmp CARG3, CARG1
2918 | add RB, RA, RB
2919 | bhi >5
2920 | add TMP1, CARG4, TMP1w, uxtw #3
2921 | ldrb TMP2w, TAB:CARG2->marked
2922 |3: // Copy result slots to table.
2923 | ldr TMP0, [RA], #8
2924 | str TMP0, [TMP1], #8
2925 | cmp RA, RB
2926 | blo <3
2927 | tbnz TMP2w, #2, >7 // isblack(table)
2928 |4:
2929 | ins_next
2930 |
2931 |5: // Need to resize array part.
2932 | str BASE, L->base
2933 | mov CARG1, L
2934 | str PC, SAVE_PC
2935 | bl extern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize)
2936 | // Must not reallocate the stack.
2937 | b <1
2938 |
2939 |7: // Possible table write barrier for any value. Skip valiswhite check.
2940 | barrierback TAB:CARG2, TMP2w, TMP1
2941 | b <4
2942 break;
2943
2944 /* -- Calls and vararg handling ----------------------------------------- */
2945
2946 case BC_CALLM:
2947 | // RA = base, (RB = nresults+1,) RC = extra_nargs
2948 | ldr TMP0w, SAVE_MULTRES
2949 | decode_RC8RD NARGS8:RC, RC
2950 | add NARGS8:RC, NARGS8:RC, TMP0
2951 | b ->BC_CALL_Z
2952 break;
2953 case BC_CALL:
2954 | decode_RC8RD NARGS8:RC, RC
2955 | // RA = base, (RB = nresults+1,) RC = (nargs+1)*8
2956 |->BC_CALL_Z:
2957 | mov RB, BASE // Save old BASE for vmeta_call.
2958 | add BASE, BASE, RA, lsl #3
2959 | ldr CARG3, [BASE]
2960 | sub NARGS8:RC, NARGS8:RC, #8
2961 | add BASE, BASE, #16
2962 | checkfunc CARG3, ->vmeta_call
2963 | ins_call
2964 break;
2965
2966 case BC_CALLMT:
2967 | // RA = base, (RB = 0,) RC = extra_nargs
2968 | ldr TMP0w, SAVE_MULTRES
2969 | add NARGS8:RC, TMP0, RC, lsl #3
2970 | b ->BC_CALLT1_Z
2971 break;
2972 case BC_CALLT:
2973 | lsl NARGS8:RC, RC, #3
2974 | // RA = base, (RB = 0,) RC = (nargs+1)*8
2975 |->BC_CALLT1_Z:
2976 | add RA, BASE, RA, lsl #3
2977 | ldr TMP1, [RA]
2978 | sub NARGS8:RC, NARGS8:RC, #8
2979 | add RA, RA, #16
2980 | checktp CARG3, TMP1, LJ_TFUNC, ->vmeta_callt
2981 | ldr PC, [BASE, FRAME_PC]
2982 |->BC_CALLT2_Z:
2983 | mov RB, #0
2984 | ldrb TMP2w, LFUNC:CARG3->ffid
2985 | tst PC, #FRAME_TYPE
2986 | bne >7
2987 |1:
2988 | str TMP1, [BASE, FRAME_FUNC] // Copy function down, but keep PC.
2989 | cbz NARGS8:RC, >3
2990 |2:
2991 | ldr TMP0, [RA, RB]
2992 | add TMP1, RB, #8
2993 | cmp TMP1, NARGS8:RC
2994 | str TMP0, [BASE, RB]
2995 | mov RB, TMP1
2996 | bne <2
2997 |3:
2998 | cmp TMP2, #1 // (> FF_C) Calling a fast function?
2999 | bhi >5
3000 |4:
3001 | ins_callt
3002 |
3003 |5: // Tailcall to a fast function with a Lua frame below.
3004 | ldrb RAw, [PC, #-3]
3005 | sub CARG1, BASE, RA, lsl #3
3006 | ldr LFUNC:CARG1, [CARG1, #-32]
3007 | and LFUNC:CARG1, CARG1, #LJ_GCVMASK
3008 | ldr CARG1, LFUNC:CARG1->pc
3009 | ldr KBASE, [CARG1, #PC2PROTO(k)]
3010 | b <4
3011 |
3012 |7: // Tailcall from a vararg function.
3013 | eor PC, PC, #FRAME_VARG
3014 | tst PC, #FRAME_TYPEP // Vararg frame below?
3015 | csel TMP2, RB, TMP2, ne // Clear ffid if no Lua function below.
3016 | bne <1
3017 | sub BASE, BASE, PC
3018 | ldr PC, [BASE, FRAME_PC]
3019 | tst PC, #FRAME_TYPE
3020 | csel TMP2, RB, TMP2, ne // Clear ffid if no Lua function below.
3021 | b <1
3022 break;
3023
3024 case BC_ITERC:
3025 | // RA = base, (RB = nresults+1, RC = nargs+1 (2+1))
3026 | add RA, BASE, RA, lsl #3
3027 | ldr CARG3, [RA, #-24]
3028 | mov RB, BASE // Save old BASE for vmeta_call.
3029 | ldp CARG1, CARG2, [RA, #-16]
3030 | add BASE, RA, #16
3031 | mov NARGS8:RC, #16 // Iterators get 2 arguments.
3032 | str CARG3, [RA] // Copy callable.
3033 | stp CARG1, CARG2, [RA, #16] // Copy state and control var.
3034 | checkfunc CARG3, ->vmeta_call
3035 | ins_call
3036 break;
3037
3038 case BC_ITERN:
3039 | // RA = base, (RB = nresults+1, RC = nargs+1 (2+1))
3040 |.if JIT
3041 | // NYI: add hotloop, record BC_ITERN.
3042 |.endif
3043 | add RA, BASE, RA, lsl #3
3044 | ldr TAB:RB, [RA, #-16]
3045 | ldrh TMP3w, [PC, #2]
3046 | ldr CARG1w, [RA, #-8] // Get index from control var.
3047 | add PC, PC, #4
3048 | add TMP3, PC, TMP3, lsl #2
3049 | and TAB:RB, RB, #LJ_GCVMASK
3050 | sub TMP3, TMP3, #0x20000
3051 | ldr TMP1w, TAB:RB->asize
3052 | ldr CARG2, TAB:RB->array
3053 |1: // Traverse array part.
3054 | subs RC, CARG1, TMP1
3055 | add CARG3, CARG2, CARG1, lsl #3
3056 | bhs >5 // Index points after array part?
3057 | ldr TMP0, [CARG3]
3058 | cmp TMP0, TISNIL
3059 | cinc CARG1, CARG1, eq // Skip holes in array part.
3060 | beq <1
3061 | add CARG1, CARG1, TISNUM
3062 | stp CARG1, TMP0, [RA]
3063 | add CARG1, CARG1, #1
3064 |3:
3065 | str CARG1w, [RA, #-8] // Update control var.
3066 | mov PC, TMP3
3067 |4:
3068 | ins_next
3069 |
3070 |5: // Traverse hash part.
3071 | ldr TMP2w, TAB:RB->hmask
3072 | ldr NODE:RB, TAB:RB->node
3073 |6:
3074 | add CARG1, RC, RC, lsl #1
3075 | cmp RC, TMP2 // End of iteration? Branch to ITERN+1.
3076 | add NODE:CARG3, NODE:RB, CARG1, lsl #3 // node = tab->node + idx*3*8
3077 | bhi <4
3078 | ldp TMP0, CARG1, NODE:CARG3->val
3079 | cmp TMP0, TISNIL
3080 | add RC, RC, #1
3081 | beq <6 // Skip holes in hash part.
3082 | stp CARG1, TMP0, [RA]
3083 | add CARG1, RC, TMP1
3084 | b <3
3085 break;
3086
3087 case BC_ISNEXT:
3088 | // RA = base, RC = target (points to ITERN)
3089 | add RA, BASE, RA, lsl #3
3090 | ldr CFUNC:CARG1, [RA, #-24]
3091 | add RC, PC, RC, lsl #2
3092 | ldp TAB:CARG3, CARG4, [RA, #-16]
3093 | sub RC, RC, #0x20000
3094 | checkfunc CFUNC:CARG1, >5
3095 | asr TMP0, TAB:CARG3, #47
3096 | ldrb TMP1w, CFUNC:CARG1->ffid
3097 | cmn TMP0, #-LJ_TTAB
3098 | ccmp CARG4, TISNIL, #0, eq
3099 | ccmp TMP1w, #FF_next_N, #0, eq
3100 | bne >5
3101 | mov TMP0w, #0xfffe7fff
3102 | lsl TMP0, TMP0, #32
3103 | str TMP0, [RA, #-8] // Initialize control var.
3104 |1:
3105 | mov PC, RC
3106 | ins_next
3107 |
3108 |5: // Despecialize bytecode if any of the checks fail.
3109 | mov TMP0, #BC_JMP
3110 | mov TMP1, #BC_ITERC
3111 | strb TMP0w, [PC, #-4]
3112 | strb TMP1w, [RC]
3113 | b <1
3114 break;
3115
3116 case BC_VARG:
3117 | decode_RB RB, INS
3118 | and RC, RC, #255
3119 | // RA = base, RB = (nresults+1), RC = numparams
3120 | ldr TMP1, [BASE, FRAME_PC]
3121 | add RC, BASE, RC, lsl #3
3122 | add RA, BASE, RA, lsl #3
3123 | add RC, RC, #FRAME_VARG
3124 | add TMP2, RA, RB, lsl #3
3125 | sub RC, RC, TMP1 // RC = vbase
3126 | // Note: RC may now be even _above_ BASE if nargs was < numparams.
3127 | sub TMP3, BASE, #16 // TMP3 = vtop
3128 | cbz RB, >5
3129 | sub TMP2, TMP2, #16
3130 |1: // Copy vararg slots to destination slots.
3131 | cmp RC, TMP3
3132 | ldr TMP0, [RC], #8
3133 | csel TMP0, TMP0, TISNIL, lo
3134 | cmp RA, TMP2
3135 | str TMP0, [RA], #8
3136 | blo <1
3137 |2:
3138 | ins_next
3139 |
3140 |5: // Copy all varargs.
3141 | ldr TMP0, L->maxstack
3142 | subs TMP2, TMP3, RC
3143 | csel RB, xzr, TMP2, le // MULTRES = (max(vtop-vbase,0)+1)*8
3144 | add RB, RB, #8
3145 | add TMP1, RA, TMP2
3146 | str RBw, SAVE_MULTRES
3147 | ble <2 // Nothing to copy.
3148 | cmp TMP1, TMP0
3149 | bhi >7
3150 |6:
3151 | ldr TMP0, [RC], #8
3152 | str TMP0, [RA], #8
3153 | cmp RC, TMP3
3154 | blo <6
3155 | b <2
3156 |
3157 |7: // Grow stack for varargs.
3158 | lsr CARG2, TMP2, #3
3159 | stp BASE, RA, L->base
3160 | mov CARG1, L
3161 | sub RC, RC, BASE // Need delta, because BASE may change.
3162 | str PC, SAVE_PC
3163 | bl extern lj_state_growstack // (lua_State *L, int n)
3164 | ldp BASE, RA, L->base
3165 | add RC, BASE, RC
3166 | sub TMP3, BASE, #16
3167 | b <6
3168 break;
3169
3170 /* -- Returns ----------------------------------------------------------- */
3171
3172 case BC_RETM:
3173 | // RA = results, RC = extra results
3174 | ldr TMP0w, SAVE_MULTRES
3175 | ldr PC, [BASE, FRAME_PC]
3176 | add RA, BASE, RA, lsl #3
3177 | add RC, TMP0, RC, lsl #3
3178 | b ->BC_RETM_Z
3179 break;
3180
3181 case BC_RET:
3182 | // RA = results, RC = nresults+1
3183 | ldr PC, [BASE, FRAME_PC]
3184 | lsl RC, RC, #3
3185 | add RA, BASE, RA, lsl #3
3186 |->BC_RETM_Z:
3187 | str RCw, SAVE_MULTRES
3188 |1:
3189 | ands CARG1, PC, #FRAME_TYPE
3190 | eor CARG2, PC, #FRAME_VARG
3191 | bne ->BC_RETV2_Z
3192 |
3193 |->BC_RET_Z:
3194 | // BASE = base, RA = resultptr, RC = (nresults+1)*8, PC = return
3195 | ldr INSw, [PC, #-4]
3196 | subs TMP1, RC, #8
3197 | sub CARG3, BASE, #16
3198 | beq >3
3199 |2:
3200 | ldr TMP0, [RA], #8
3201 | add BASE, BASE, #8
3202 | sub TMP1, TMP1, #8
3203 | str TMP0, [BASE, #-24]
3204 | cbnz TMP1, <2
3205 |3:
3206 | decode_RA RA, INS
3207 | sub CARG4, CARG3, RA, lsl #3
3208 | decode_RB RB, INS
3209 | ldr LFUNC:CARG1, [CARG4, FRAME_FUNC]
3210 |5:
3211 | cmp RC, RB, lsl #3 // More results expected?
3212 | blo >6
3213 | and LFUNC:CARG1, CARG1, #LJ_GCVMASK
3214 | mov BASE, CARG4
3215 | ldr CARG2, LFUNC:CARG1->pc
3216 | ldr KBASE, [CARG2, #PC2PROTO(k)]
3217 | ins_next
3218 |
3219 |6: // Fill up results with nil.
3220 | add BASE, BASE, #8
3221 | add RC, RC, #8
3222 | str TISNIL, [BASE, #-24]
3223 | b <5
3224 |
3225 |->BC_RETV1_Z: // Non-standard return case.
3226 | add RA, BASE, RA, lsl #3
3227 |->BC_RETV2_Z:
3228 | tst CARG2, #FRAME_TYPEP
3229 | bne ->vm_return
3230 | // Return from vararg function: relocate BASE down.
3231 | sub BASE, BASE, CARG2
3232 | ldr PC, [BASE, FRAME_PC]
3233 | b <1
3234 break;
3235
3236 case BC_RET0: case BC_RET1:
3237 | // RA = results, RC = nresults+1
3238 | ldr PC, [BASE, FRAME_PC]
3239 | lsl RC, RC, #3
3240 | str RCw, SAVE_MULTRES
3241 | ands CARG1, PC, #FRAME_TYPE
3242 | eor CARG2, PC, #FRAME_VARG
3243 | bne ->BC_RETV1_Z
3244 | ldr INSw, [PC, #-4]
3245 if (op == BC_RET1) {
3246 | ldr TMP0, [BASE, RA, lsl #3]
3247 }
3248 | sub CARG4, BASE, #16
3249 | decode_RA RA, INS
3250 | sub BASE, CARG4, RA, lsl #3
3251 if (op == BC_RET1) {
3252 | str TMP0, [CARG4], #8
3253 }
3254 | decode_RB RB, INS
3255 | ldr LFUNC:CARG1, [BASE, FRAME_FUNC]
3256 |5:
3257 | cmp RC, RB, lsl #3
3258 | blo >6
3259 | and LFUNC:CARG1, CARG1, #LJ_GCVMASK
3260 | ldr CARG2, LFUNC:CARG1->pc
3261 | ldr KBASE, [CARG2, #PC2PROTO(k)]
3262 | ins_next
3263 |
3264 |6: // Fill up results with nil.
3265 | add RC, RC, #8
3266 | str TISNIL, [CARG4], #8
3267 | b <5
3268 break;
3269
3270 /* -- Loops and branches ------------------------------------------------ */
3271
3272 |.define FOR_IDX, [RA]; .define FOR_TIDX, [RA, #4]
3273 |.define FOR_STOP, [RA, #8]; .define FOR_TSTOP, [RA, #12]
3274 |.define FOR_STEP, [RA, #16]; .define FOR_TSTEP, [RA, #20]
3275 |.define FOR_EXT, [RA, #24]; .define FOR_TEXT, [RA, #28]
3276
3277 case BC_FORL:
3278 |.if JIT
3279 | hotloop
3280 |.endif
3281 | // Fall through. Assumes BC_IFORL follows.
3282 break;
3283
3284 case BC_JFORI:
3285 case BC_JFORL:
3286#if !LJ_HASJIT
3287 break;
3288#endif
3289 case BC_FORI:
3290 case BC_IFORL:
3291 | // RA = base, RC = target (after end of loop or start of loop)
3292 vk = (op == BC_IFORL || op == BC_JFORL);
3293 | add RA, BASE, RA, lsl #3
3294 | ldp CARG1, CARG2, FOR_IDX // CARG1 = IDX, CARG2 = STOP
3295 | ldr CARG3, FOR_STEP // CARG3 = STEP
3296 if (op != BC_JFORL) {
3297 | add RC, PC, RC, lsl #2
3298 | sub RC, RC, #0x20000
3299 }
3300 | checkint CARG1, >5
3301 if (!vk) {
3302 | checkint CARG2, ->vmeta_for
3303 | checkint CARG3, ->vmeta_for
3304 | tbnz CARG3w, #31, >4
3305 | cmp CARG1w, CARG2w
3306 } else {
3307 | adds CARG1w, CARG1w, CARG3w
3308 | bvs >2
3309 | add TMP0, CARG1, TISNUM
3310 | tbnz CARG3w, #31, >4
3311 | cmp CARG1w, CARG2w
3312 }
3313 |1:
3314 if (op == BC_FORI) {
3315 | csel PC, RC, PC, gt
3316 } else if (op == BC_JFORI) {
3317 | ldrh RCw, [RC, #-2]
3318 } else if (op == BC_IFORL) {
3319 | csel PC, RC, PC, le
3320 }
3321 if (vk) {
3322 | str TMP0, FOR_IDX
3323 | str TMP0, FOR_EXT
3324 } else {
3325 | str CARG1, FOR_EXT
3326 }
3327 if (op == BC_JFORI || op == BC_JFORL) {
3328 | ble =>BC_JLOOP
3329 }
3330 |2:
3331 | ins_next
3332 |
3333 |4: // Invert check for negative step.
3334 | cmp CARG2w, CARG1w
3335 | b <1
3336 |
3337 |5: // FP loop.
3338 | ldp d0, d1, FOR_IDX
3339 | blo ->vmeta_for
3340 if (!vk) {
3341 | checknum CARG2, ->vmeta_for
3342 | checknum CARG3, ->vmeta_for
3343 | str d0, FOR_EXT
3344 } else {
3345 | ldr d2, FOR_STEP
3346 | fadd d0, d0, d2
3347 }
3348 | tbnz CARG3, #63, >7
3349 | fcmp d0, d1
3350 |6:
3351 if (vk) {
3352 | str d0, FOR_IDX
3353 | str d0, FOR_EXT
3354 }
3355 if (op == BC_FORI) {
3356 | csel PC, RC, PC, hi
3357 } else if (op == BC_JFORI) {
3358 | ldrh RCw, [RC, #-2]
3359 | bls =>BC_JLOOP
3360 } else if (op == BC_IFORL) {
3361 | csel PC, RC, PC, ls
3362 } else {
3363 | bls =>BC_JLOOP
3364 }
3365 | b <2
3366 |
3367 |7: // Invert check for negative step.
3368 | fcmp d1, d0
3369 | b <6
3370 break;
3371
3372 case BC_ITERL:
3373 |.if JIT
3374 | hotloop
3375 |.endif
3376 | // Fall through. Assumes BC_IITERL follows.
3377 break;
3378
3379 case BC_JITERL:
3380#if !LJ_HASJIT
3381 break;
3382#endif
3383 case BC_IITERL:
3384 | // RA = base, RC = target
3385 | ldr CARG1, [BASE, RA, lsl #3]
3386 | add TMP1, BASE, RA, lsl #3
3387 | cmp CARG1, TISNIL
3388 | beq >1 // Stop if iterator returned nil.
3389 if (op == BC_JITERL) {
3390 | str CARG1, [TMP1, #-8]
3391 | b =>BC_JLOOP
3392 } else {
3393 | add TMP0, PC, RC, lsl #2 // Otherwise save control var + branch.
3394 | sub PC, TMP0, #0x20000
3395 | str CARG1, [TMP1, #-8]
3396 }
3397 |1:
3398 | ins_next
3399 break;
3400
3401 case BC_LOOP:
3402 | // RA = base, RC = target (loop extent)
3403 | // Note: RA/RC is only used by trace recorder to determine scope/extent
3404 | // This opcode does NOT jump, it's only purpose is to detect a hot loop.
3405 |.if JIT
3406 | hotloop
3407 |.endif
3408 | // Fall through. Assumes BC_ILOOP follows.
3409 break;
3410
3411 case BC_ILOOP:
3412 | // RA = base, RC = target (loop extent)
3413 | ins_next
3414 break;
3415
3416 case BC_JLOOP:
3417 |.if JIT
3418 | NYI
3419 |.endif
3420 break;
3421
3422 case BC_JMP:
3423 | // RA = base (only used by trace recorder), RC = target
3424 | add RC, PC, RC, lsl #2
3425 | sub PC, RC, #0x20000
3426 | ins_next
3427 break;
3428
3429 /* -- Function headers -------------------------------------------------- */
3430
3431 case BC_FUNCF:
3432 |.if JIT
3433 | hotcall
3434 |.endif
3435 case BC_FUNCV: /* NYI: compiled vararg functions. */
3436 | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow.
3437 break;
3438
3439 case BC_JFUNCF:
3440#if !LJ_HASJIT
3441 break;
3442#endif
3443 case BC_IFUNCF:
3444 | // BASE = new base, RA = BASE+framesize*8, CARG3 = LFUNC, RC = nargs*8
3445 | ldr CARG1, L->maxstack
3446 | ldrb TMP1w, [PC, #-4+PC2PROTO(numparams)]
3447 | ldr KBASE, [PC, #-4+PC2PROTO(k)]
3448 | cmp RA, CARG1
3449 | bhi ->vm_growstack_l
3450 |2:
3451 | cmp NARGS8:RC, TMP1, lsl #3 // Check for missing parameters.
3452 | blo >3
3453 if (op == BC_JFUNCF) {
3454 | decode_RD RC, INS
3455 | b =>BC_JLOOP
3456 } else {
3457 | ins_next
3458 }
3459 |
3460 |3: // Clear missing parameters.
3461 | str TISNIL, [BASE, NARGS8:RC]
3462 | add NARGS8:RC, NARGS8:RC, #8
3463 | b <2
3464 break;
3465
3466 case BC_JFUNCV:
3467#if !LJ_HASJIT
3468 break;
3469#endif
3470 | NYI // NYI: compiled vararg functions
3471 break; /* NYI: compiled vararg functions. */
3472
3473 case BC_IFUNCV:
3474 | // BASE = new base, RA = BASE+framesize*8, CARG3 = LFUNC, RC = nargs*8
3475 | ldr CARG1, L->maxstack
3476 | add TMP2, BASE, RC
3477 | add RA, RA, RC
3478 | add TMP0, RC, #16+FRAME_VARG
3479 | str LFUNC:CARG3, [TMP2], #8 // Store (untagged) copy of LFUNC.
3480 | ldr KBASE, [PC, #-4+PC2PROTO(k)]
3481 | cmp RA, CARG1
3482 | str TMP0, [TMP2], #8 // Store delta + FRAME_VARG.
3483 | bhs ->vm_growstack_l
3484 | sub RC, TMP2, #16
3485 | ldrb TMP1w, [PC, #-4+PC2PROTO(numparams)]
3486 | mov RA, BASE
3487 | mov BASE, TMP2
3488 | cbz TMP1, >2
3489 |1:
3490 | cmp RA, RC // Less args than parameters?
3491 | bhs >3
3492 | ldr TMP0, [RA]
3493 | sub TMP1, TMP1, #1
3494 | str TISNIL, [RA], #8 // Clear old fixarg slot (help the GC).
3495 | str TMP0, [TMP2], #8
3496 | cbnz TMP1, <1
3497 |2:
3498 | ins_next
3499 |
3500 |3:
3501 | sub TMP1, TMP1, #1
3502 | str TISNIL, [TMP2], #8
3503 | cbz TMP1, <2
3504 | b <3
3505 break;
3506
3507 case BC_FUNCC:
3508 case BC_FUNCCW:
3509 | // BASE = new base, RA = BASE+framesize*8, CARG3 = CFUNC, RC = nargs*8
3510 if (op == BC_FUNCC) {
3511 | ldr CARG4, CFUNC:CARG3->f
3512 } else {
3513 | ldr CARG4, GL->wrapf
3514 }
3515 | add CARG2, RA, NARGS8:RC
3516 | ldr CARG1, L->maxstack
3517 | add RC, BASE, NARGS8:RC
3518 | cmp CARG2, CARG1
3519 | stp BASE, RC, L->base
3520 if (op == BC_FUNCCW) {
3521 | ldr CARG2, CFUNC:CARG3->f
3522 }
3523 | mv_vmstate TMP0w, C
3524 | mov CARG1, L
3525 | bhi ->vm_growstack_c // Need to grow stack.
3526 | st_vmstate TMP0w
3527 | blr CARG4 // (lua_State *L [, lua_CFunction f])
3528 | // Returns nresults.
3529 | ldp BASE, TMP1, L->base
3530 | str L, GL->cur_L
3531 | sbfiz RC, CRET1, #3, #32
3532 | st_vmstate ST_INTERP
3533 | ldr PC, [BASE, FRAME_PC]
3534 | sub RA, TMP1, RC // RA = L->top - nresults*8
3535 | b ->vm_returnc
3536 break;
3537
3538 /* ---------------------------------------------------------------------- */
3539
3540 default:
3541 fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]);
3542 exit(2);
3543 break;
3544 }
3545}
3546
3547static int build_backend(BuildCtx *ctx)
3548{
3549 int op;
3550
3551 dasm_growpc(Dst, BC__MAX);
3552
3553 build_subroutines(ctx);
3554
3555 |.code_op
3556 for (op = 0; op < BC__MAX; op++)
3557 build_ins(ctx, (BCOp)op, op);
3558
3559 return BC__MAX;
3560}
3561
3562/* Emit pseudo frame-info for all assembler functions. */
3563static void emit_asm_debug(BuildCtx *ctx)
3564{
3565 int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code);
3566 int i, cf = CFRAME_SIZE >> 3;
3567 switch (ctx->mode) {
3568 case BUILD_elfasm:
3569 fprintf(ctx->fp, "\t.section .debug_frame,\"\",%%progbits\n");
3570 fprintf(ctx->fp,
3571 ".Lframe0:\n"
3572 "\t.long .LECIE0-.LSCIE0\n"
3573 ".LSCIE0:\n"
3574 "\t.long 0xffffffff\n"
3575 "\t.byte 0x1\n"
3576 "\t.string \"\"\n"
3577 "\t.uleb128 0x1\n"
3578 "\t.sleb128 -8\n"
3579 "\t.byte 30\n" /* Return address is in lr. */
3580 "\t.byte 0xc\n\t.uleb128 31\n\t.uleb128 0\n" /* def_cfa sp */
3581 "\t.align 3\n"
3582 ".LECIE0:\n\n");
3583 fprintf(ctx->fp,
3584 ".LSFDE0:\n"
3585 "\t.long .LEFDE0-.LASFDE0\n"
3586 ".LASFDE0:\n"
3587 "\t.long .Lframe0\n"
3588 "\t.quad .Lbegin\n"
3589 "\t.quad %d\n"
3590 "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */
3591 "\t.byte 0x9d\n\t.uleb128 %d\n" /* offset fp */
3592 "\t.byte 0x9e\n\t.uleb128 %d\n", /* offset lr */
3593 fcofs, CFRAME_SIZE, cf, cf-1);
3594 for (i = 19; i <= 28; i++) /* offset x19-x28 */
3595 fprintf(ctx->fp, "\t.byte 0x%x\n\t.uleb128 %d\n", 0x80+i, cf-i+17);
3596 for (i = 8; i <= 15; i++) /* offset d8-d15 */
3597 fprintf(ctx->fp, "\t.byte 5\n\t.uleb128 0x%x, %d\n", 64+i, cf-i-4);
3598 fprintf(ctx->fp,
3599 "\t.align 3\n"
3600 ".LEFDE0:\n\n");
3601#if LJ_HASFFI
3602#error "NYI"
3603#endif
3604 fprintf(ctx->fp, "\t.section .eh_frame,\"a\",%%progbits\n");
3605 fprintf(ctx->fp,
3606 ".Lframe1:\n"
3607 "\t.long .LECIE1-.LSCIE1\n"
3608 ".LSCIE1:\n"
3609 "\t.long 0\n"
3610 "\t.byte 0x1\n"
3611 "\t.string \"zPR\"\n"
3612 "\t.uleb128 0x1\n"
3613 "\t.sleb128 -8\n"
3614 "\t.byte 30\n" /* Return address is in lr. */
3615 "\t.uleb128 6\n" /* augmentation length */
3616 "\t.byte 0x1b\n" /* pcrel|sdata4 */
3617 "\t.long lj_err_unwind_dwarf-.\n"
3618 "\t.byte 0x1b\n" /* pcrel|sdata4 */
3619 "\t.byte 0xc\n\t.uleb128 31\n\t.uleb128 0\n" /* def_cfa sp */
3620 "\t.align 3\n"
3621 ".LECIE1:\n\n");
3622 fprintf(ctx->fp,
3623 ".LSFDE2:\n"
3624 "\t.long .LEFDE2-.LASFDE2\n"
3625 ".LASFDE2:\n"
3626 "\t.long .LASFDE2-.Lframe1\n"
3627 "\t.long .Lbegin-.\n"
3628 "\t.long %d\n"
3629 "\t.uleb128 0\n" /* augmentation length */
3630 "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */
3631 "\t.byte 0x9d\n\t.uleb128 %d\n" /* offset fp */
3632 "\t.byte 0x9e\n\t.uleb128 %d\n", /* offset lr */
3633 fcofs, CFRAME_SIZE, cf, cf-1);
3634 for (i = 19; i <= 28; i++) /* offset x19-x28 */
3635 fprintf(ctx->fp, "\t.byte 0x%x\n\t.uleb128 %d\n", 0x80+i, cf-i+17);
3636 for (i = 8; i <= 15; i++) /* offset d8-d15 */
3637 fprintf(ctx->fp, "\t.byte 5\n\t.uleb128 0x%x, %d\n", 64+i, cf-i-4);
3638 fprintf(ctx->fp,
3639 "\t.align 3\n"
3640 ".LEFDE2:\n\n");
3641#if LJ_HASFFI
3642#error "NYI"
3643#endif
3644 break;
3645 default:
3646 break;
3647 }
3648}
3649