beta-0.89.2
[luatex.git] / source / libs / luajit / LuaJIT-src / src / lj_frame.h
bloba86c36be7e5901f53517d3a1acd4babd6f32419a
1 /*
2 ** Stack frames.
3 ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h
4 */
6 #ifndef _LJ_FRAME_H
7 #define _LJ_FRAME_H
9 #include "lj_obj.h"
10 #include "lj_bc.h"
12 /* -- Lua stack frame ----------------------------------------------------- */
14 /* Frame type markers in LSB of PC (4-byte aligned) or delta (8-byte aligned:
16 ** PC 00 Lua frame
17 ** delta 001 C frame
18 ** delta 010 Continuation frame
19 ** delta 011 Lua vararg frame
20 ** delta 101 cpcall() frame
21 ** delta 110 ff pcall() frame
22 ** delta 111 ff pcall() frame with active hook
24 enum {
25 FRAME_LUA, FRAME_C, FRAME_CONT, FRAME_VARG,
26 FRAME_LUAP, FRAME_CP, FRAME_PCALL, FRAME_PCALLH
28 #define FRAME_TYPE 3
29 #define FRAME_P 4
30 #define FRAME_TYPEP (FRAME_TYPE|FRAME_P)
32 /* Macros to access and modify Lua frames. */
33 #if LJ_FR2
34 /* Two-slot frame info, required for 64 bit PC/GCRef:
36 ** base-2 base-1 | base base+1 ...
37 ** [func PC/delta/ft] | [slots ...]
38 ** ^-- frame | ^-- base ^-- top
40 ** Continuation frames:
42 ** base-4 base-3 base-2 base-1 | base base+1 ...
43 ** [cont PC ] [func PC/delta/ft] | [slots ...]
44 ** ^-- frame | ^-- base ^-- top
46 #define frame_gc(f) (gcval((f)-1))
47 #define frame_ftsz(f) ((ptrdiff_t)(f)->ftsz)
48 #define frame_pc(f) ((const BCIns *)frame_ftsz(f))
49 #define setframe_gc(f, p, tp) (setgcVraw((f)-1, (p), (tp)))
50 #define setframe_ftsz(f, sz) ((f)->ftsz = (sz))
51 #define setframe_pc(f, pc) ((f)->ftsz = (int64_t)(intptr_t)(pc))
52 #else
53 /* One-slot frame info, sufficient for 32 bit PC/GCRef:
55 ** base-1 | base base+1 ...
56 ** lo hi |
57 ** [func | PC/delta/ft] | [slots ...]
58 ** ^-- frame | ^-- base ^-- top
60 ** Continuation frames:
62 ** base-2 base-1 | base base+1 ...
63 ** lo hi lo hi |
64 ** [cont | PC] [func | PC/delta/ft] | [slots ...]
65 ** ^-- frame | ^-- base ^-- top
67 #define frame_gc(f) (gcref((f)->fr.func))
68 #define frame_ftsz(f) ((ptrdiff_t)(f)->fr.tp.ftsz)
69 #define frame_pc(f) (mref((f)->fr.tp.pcr, const BCIns))
70 #define setframe_gc(f, p, tp) (setgcref((f)->fr.func, (p)), UNUSED(tp))
71 #define setframe_ftsz(f, sz) ((f)->fr.tp.ftsz = (int32_t)(sz))
72 #define setframe_pc(f, pc) (setmref((f)->fr.tp.pcr, (pc)))
73 #endif
75 #define frame_type(f) (frame_ftsz(f) & FRAME_TYPE)
76 #define frame_typep(f) (frame_ftsz(f) & FRAME_TYPEP)
77 #define frame_islua(f) (frame_type(f) == FRAME_LUA)
78 #define frame_isc(f) (frame_type(f) == FRAME_C)
79 #define frame_iscont(f) (frame_typep(f) == FRAME_CONT)
80 #define frame_isvarg(f) (frame_typep(f) == FRAME_VARG)
81 #define frame_ispcall(f) ((frame_ftsz(f) & 6) == FRAME_PCALL)
83 #define frame_func(f) (&frame_gc(f)->fn)
84 #define frame_delta(f) (frame_ftsz(f) >> 3)
85 #define frame_sized(f) (frame_ftsz(f) & ~FRAME_TYPEP)
87 enum { LJ_CONT_TAILCALL, LJ_CONT_FFI_CALLBACK }; /* Special continuations. */
89 #if LJ_FR2
90 #define frame_contpc(f) (frame_pc((f)-2))
91 #define frame_contv(f) (((f)-3)->u64)
92 #else
93 #define frame_contpc(f) (frame_pc((f)-1))
94 #define frame_contv(f) (((f)-1)->u32.lo)
95 #endif
96 #if LJ_FR2
97 #define frame_contf(f) ((ASMFunction)(uintptr_t)((f)-3)->u64)
98 #elif LJ_64
99 #define frame_contf(f) \
100 ((ASMFunction)(void *)((intptr_t)lj_vm_asm_begin + \
101 (intptr_t)(int32_t)((f)-1)->u32.lo))
102 #else
103 #define frame_contf(f) ((ASMFunction)gcrefp(((f)-1)->gcr, void))
104 #endif
105 #define frame_iscont_fficb(f) \
106 (LJ_HASFFI && frame_contv(f) == LJ_CONT_FFI_CALLBACK)
108 #define frame_prevl(f) ((f) - (1+LJ_FR2+bc_a(frame_pc(f)[-1])))
109 #define frame_prevd(f) ((TValue *)((char *)(f) - frame_sized(f)))
110 #define frame_prev(f) (frame_islua(f)?frame_prevl(f):frame_prevd(f))
111 /* Note: this macro does not skip over FRAME_VARG. */
113 /* -- C stack frame ------------------------------------------------------- */
115 /* Macros to access and modify the C stack frame chain. */
117 /* These definitions must match with the arch-specific *.dasc files. */
118 #if LJ_TARGET_X86
119 #define CFRAME_OFS_ERRF (15*4)
120 #define CFRAME_OFS_NRES (14*4)
121 #define CFRAME_OFS_PREV (13*4)
122 #define CFRAME_OFS_L (12*4)
123 #define CFRAME_OFS_PC (6*4)
124 #define CFRAME_OFS_MULTRES (5*4)
125 #define CFRAME_SIZE (12*4)
126 #define CFRAME_SHIFT_MULTRES 0
127 #elif LJ_TARGET_X64
128 #if LJ_ABI_WIN
129 #define CFRAME_OFS_PREV (13*8)
130 #if LJ_GC64
131 #define CFRAME_OFS_PC (12*8)
132 #define CFRAME_OFS_L (11*8)
133 #define CFRAME_OFS_ERRF (21*4)
134 #define CFRAME_OFS_NRES (20*4)
135 #define CFRAME_OFS_MULTRES (8*4)
136 #else
137 #define CFRAME_OFS_PC (25*4)
138 #define CFRAME_OFS_L (24*4)
139 #define CFRAME_OFS_ERRF (23*4)
140 #define CFRAME_OFS_NRES (22*4)
141 #define CFRAME_OFS_MULTRES (21*4)
142 #endif
143 #define CFRAME_SIZE (10*8)
144 #define CFRAME_SIZE_JIT (CFRAME_SIZE + 9*16 + 4*8)
145 #define CFRAME_SHIFT_MULTRES 0
146 #else
147 #define CFRAME_OFS_PREV (4*8)
148 #if LJ_GC64
149 #define CFRAME_OFS_PC (3*8)
150 #define CFRAME_OFS_L (2*8)
151 #define CFRAME_OFS_ERRF (3*4)
152 #define CFRAME_OFS_NRES (2*4)
153 #define CFRAME_OFS_MULTRES (0*4)
154 #else
155 #define CFRAME_OFS_PC (7*4)
156 #define CFRAME_OFS_L (6*4)
157 #define CFRAME_OFS_ERRF (5*4)
158 #define CFRAME_OFS_NRES (4*4)
159 #define CFRAME_OFS_MULTRES (1*4)
160 #endif
161 #if LJ_NO_UNWIND
162 #define CFRAME_SIZE (12*8)
163 #else
164 #define CFRAME_SIZE (10*8)
165 #endif
166 #define CFRAME_SIZE_JIT (CFRAME_SIZE + 16)
167 #define CFRAME_SHIFT_MULTRES 0
168 #endif
169 #elif LJ_TARGET_ARM
170 #define CFRAME_OFS_ERRF 24
171 #define CFRAME_OFS_NRES 20
172 #define CFRAME_OFS_PREV 16
173 #define CFRAME_OFS_L 12
174 #define CFRAME_OFS_PC 8
175 #define CFRAME_OFS_MULTRES 4
176 #if LJ_ARCH_HASFPU
177 #define CFRAME_SIZE 128
178 #else
179 #define CFRAME_SIZE 64
180 #endif
181 #define CFRAME_SHIFT_MULTRES 3
182 #elif LJ_TARGET_ARM64
183 #define CFRAME_OFS_ERRF 196
184 #define CFRAME_OFS_NRES 200
185 #define CFRAME_OFS_PREV 160
186 #define CFRAME_OFS_L 176
187 #define CFRAME_OFS_PC 168
188 #define CFRAME_OFS_MULTRES 192
189 #define CFRAME_SIZE 208
190 #define CFRAME_SHIFT_MULTRES 3
191 #elif LJ_TARGET_PPC
192 #if LJ_TARGET_XBOX360
193 #define CFRAME_OFS_ERRF 424
194 #define CFRAME_OFS_NRES 420
195 #define CFRAME_OFS_PREV 400
196 #define CFRAME_OFS_L 416
197 #define CFRAME_OFS_PC 412
198 #define CFRAME_OFS_MULTRES 408
199 #define CFRAME_SIZE 384
200 #define CFRAME_SHIFT_MULTRES 3
201 #elif LJ_ARCH_PPC32ON64
202 #define CFRAME_OFS_ERRF 472
203 #define CFRAME_OFS_NRES 468
204 #define CFRAME_OFS_PREV 448
205 #define CFRAME_OFS_L 464
206 #define CFRAME_OFS_PC 460
207 #define CFRAME_OFS_MULTRES 456
208 #define CFRAME_SIZE 400
209 #define CFRAME_SHIFT_MULTRES 3
210 #else
211 #define CFRAME_OFS_ERRF 48
212 #define CFRAME_OFS_NRES 44
213 #define CFRAME_OFS_PREV 40
214 #define CFRAME_OFS_L 36
215 #define CFRAME_OFS_PC 32
216 #define CFRAME_OFS_MULTRES 28
217 #define CFRAME_SIZE 272
218 #define CFRAME_SHIFT_MULTRES 3
219 #endif
220 #elif LJ_TARGET_MIPS
221 #define CFRAME_OFS_ERRF 124
222 #define CFRAME_OFS_NRES 120
223 #define CFRAME_OFS_PREV 116
224 #define CFRAME_OFS_L 112
225 #define CFRAME_OFS_PC 20
226 #define CFRAME_OFS_MULTRES 16
227 #define CFRAME_SIZE 112
228 #define CFRAME_SHIFT_MULTRES 3
229 #else
230 #error "Missing CFRAME_* definitions for this architecture"
231 #endif
233 #ifndef CFRAME_SIZE_JIT
234 #define CFRAME_SIZE_JIT CFRAME_SIZE
235 #endif
237 #define CFRAME_RESUME 1
238 #define CFRAME_UNWIND_FF 2 /* Only used in unwinder. */
239 #define CFRAME_RAWMASK (~(intptr_t)(CFRAME_RESUME|CFRAME_UNWIND_FF))
241 #define cframe_errfunc(cf) (*(int32_t *)(((char *)(cf))+CFRAME_OFS_ERRF))
242 #define cframe_nres(cf) (*(int32_t *)(((char *)(cf))+CFRAME_OFS_NRES))
243 #define cframe_prev(cf) (*(void **)(((char *)(cf))+CFRAME_OFS_PREV))
244 #define cframe_multres(cf) (*(uint32_t *)(((char *)(cf))+CFRAME_OFS_MULTRES))
245 #define cframe_multres_n(cf) (cframe_multres((cf)) >> CFRAME_SHIFT_MULTRES)
246 #define cframe_L(cf) \
247 (&gcref(*(GCRef *)(((char *)(cf))+CFRAME_OFS_L))->th)
248 #define cframe_pc(cf) \
249 (mref(*(MRef *)(((char *)(cf))+CFRAME_OFS_PC), const BCIns))
250 #define setcframe_L(cf, L) \
251 (setmref(*(MRef *)(((char *)(cf))+CFRAME_OFS_L), (L)))
252 #define setcframe_pc(cf, pc) \
253 (setmref(*(MRef *)(((char *)(cf))+CFRAME_OFS_PC), (pc)))
254 #define cframe_canyield(cf) ((intptr_t)(cf) & CFRAME_RESUME)
255 #define cframe_unwind_ff(cf) ((intptr_t)(cf) & CFRAME_UNWIND_FF)
256 #define cframe_raw(cf) ((void *)((intptr_t)(cf) & CFRAME_RAWMASK))
257 #define cframe_Lpc(L) cframe_pc(cframe_raw(L->cframe))
259 #endif