FFI: Fix pragma push stack limit check and throw on overflow.
[luajit-2.0.git] / src / lj_frame.h
blob35cbb4ead974a04ff2666e8d92117c1e43c31eb3
1 /*
2 ** Stack frames.
3 ** Copyright (C) 2005-2023 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 callee function slot (callee base-1). */
15 enum {
16 FRAME_LUA, FRAME_C, FRAME_CONT, FRAME_VARG,
17 FRAME_LUAP, FRAME_CP, FRAME_PCALL, FRAME_PCALLH
19 #define FRAME_TYPE 3
20 #define FRAME_P 4
21 #define FRAME_TYPEP (FRAME_TYPE|FRAME_P)
23 /* Macros to access and modify Lua frames. */
24 #define frame_gc(f) (gcref((f)->fr.func))
25 #define frame_func(f) (&frame_gc(f)->fn)
26 #define frame_ftsz(f) ((f)->fr.tp.ftsz)
28 #define frame_type(f) (frame_ftsz(f) & FRAME_TYPE)
29 #define frame_typep(f) (frame_ftsz(f) & FRAME_TYPEP)
30 #define frame_islua(f) (frame_type(f) == FRAME_LUA)
31 #define frame_isc(f) (frame_type(f) == FRAME_C)
32 #define frame_iscont(f) (frame_typep(f) == FRAME_CONT)
33 #define frame_isvarg(f) (frame_typep(f) == FRAME_VARG)
34 #define frame_ispcall(f) ((frame_ftsz(f) & 6) == FRAME_PCALL)
36 #define frame_pc(f) (mref((f)->fr.tp.pcr, const BCIns))
37 #define frame_contpc(f) (frame_pc((f)-1))
38 #if LJ_64
39 #define frame_contf(f) \
40 ((ASMFunction)(void *)((intptr_t)lj_vm_asm_begin + \
41 (intptr_t)(int32_t)((f)-1)->u32.lo))
42 #else
43 #define frame_contf(f) ((ASMFunction)gcrefp(((f)-1)->gcr, void))
44 #endif
45 #define frame_delta(f) (frame_ftsz(f) >> 3)
46 #define frame_sized(f) (frame_ftsz(f) & ~FRAME_TYPEP)
48 #define frame_prevl(f) ((f) - (1+bc_a(frame_pc(f)[-1])))
49 #define frame_prevd(f) ((TValue *)((char *)(f) - frame_sized(f)))
50 #define frame_prev(f) (frame_islua(f)?frame_prevl(f):frame_prevd(f))
51 /* Note: this macro does not skip over FRAME_VARG. */
53 #define setframe_pc(f, pc) (setmref((f)->fr.tp.pcr, (pc)))
54 #define setframe_ftsz(f, sz) ((f)->fr.tp.ftsz = (sz))
55 #define setframe_gc(f, p) (setgcref((f)->fr.func, (p)))
57 /* -- C stack frame ------------------------------------------------------- */
59 /* Macros to access and modify the C stack frame chain. */
61 /* These definitions must match with the arch-specific *.dasc files. */
62 #if LJ_TARGET_X86
63 #define CFRAME_OFS_ERRF (15*4)
64 #define CFRAME_OFS_NRES (14*4)
65 #define CFRAME_OFS_PREV (13*4)
66 #define CFRAME_OFS_L (12*4)
67 #define CFRAME_OFS_PC (6*4)
68 #define CFRAME_OFS_MULTRES (5*4)
69 #define CFRAME_SIZE (12*4)
70 #define CFRAME_SHIFT_MULTRES 0
71 #elif LJ_TARGET_X64
72 #if LJ_ABI_WIN
73 #define CFRAME_OFS_PREV (13*8)
74 #define CFRAME_OFS_PC (25*4)
75 #define CFRAME_OFS_L (24*4)
76 #define CFRAME_OFS_ERRF (23*4)
77 #define CFRAME_OFS_NRES (22*4)
78 #define CFRAME_OFS_MULTRES (21*4)
79 #define CFRAME_SIZE (10*8)
80 #define CFRAME_SIZE_JIT (CFRAME_SIZE + 9*16 + 4*8)
81 #define CFRAME_SHIFT_MULTRES 0
82 #else
83 #define CFRAME_OFS_PREV (4*8)
84 #define CFRAME_OFS_PC (7*4)
85 #define CFRAME_OFS_L (6*4)
86 #define CFRAME_OFS_ERRF (5*4)
87 #define CFRAME_OFS_NRES (4*4)
88 #define CFRAME_OFS_MULTRES (1*4)
89 #if LJ_NO_UNWIND
90 #define CFRAME_SIZE (12*8)
91 #else
92 #define CFRAME_SIZE (10*8)
93 #endif
94 #define CFRAME_SIZE_JIT (CFRAME_SIZE + 16)
95 #define CFRAME_SHIFT_MULTRES 0
96 #endif
97 #elif LJ_TARGET_ARM
98 #define CFRAME_OFS_ERRF 24
99 #define CFRAME_OFS_NRES 20
100 #define CFRAME_OFS_PREV 16
101 #define CFRAME_OFS_L 12
102 #define CFRAME_OFS_PC 8
103 #define CFRAME_OFS_MULTRES 4
104 #if LJ_ARCH_HASFPU
105 #define CFRAME_SIZE 128
106 #else
107 #define CFRAME_SIZE 64
108 #endif
109 #define CFRAME_SHIFT_MULTRES 3
110 #elif LJ_TARGET_PPC
111 #if LJ_TARGET_XBOX360
112 #define CFRAME_OFS_ERRF 424
113 #define CFRAME_OFS_NRES 420
114 #define CFRAME_OFS_PREV 400
115 #define CFRAME_OFS_L 416
116 #define CFRAME_OFS_PC 412
117 #define CFRAME_OFS_MULTRES 408
118 #define CFRAME_SIZE 384
119 #define CFRAME_SHIFT_MULTRES 3
120 #elif LJ_ARCH_PPC64
121 #define CFRAME_OFS_ERRF 472
122 #define CFRAME_OFS_NRES 468
123 #define CFRAME_OFS_PREV 448
124 #define CFRAME_OFS_L 464
125 #define CFRAME_OFS_PC 460
126 #define CFRAME_OFS_MULTRES 456
127 #define CFRAME_SIZE 400
128 #define CFRAME_SHIFT_MULTRES 3
129 #else
130 #define CFRAME_OFS_ERRF 48
131 #define CFRAME_OFS_NRES 44
132 #define CFRAME_OFS_PREV 40
133 #define CFRAME_OFS_L 36
134 #define CFRAME_OFS_PC 32
135 #define CFRAME_OFS_MULTRES 28
136 #define CFRAME_SIZE 272
137 #define CFRAME_SHIFT_MULTRES 3
138 #endif
139 #elif LJ_TARGET_PPCSPE
140 #define CFRAME_OFS_ERRF 28
141 #define CFRAME_OFS_NRES 24
142 #define CFRAME_OFS_PREV 20
143 #define CFRAME_OFS_L 16
144 #define CFRAME_OFS_PC 12
145 #define CFRAME_OFS_MULTRES 8
146 #define CFRAME_SIZE 184
147 #define CFRAME_SHIFT_MULTRES 3
148 #elif LJ_TARGET_MIPS
149 #define CFRAME_OFS_ERRF 124
150 #define CFRAME_OFS_NRES 120
151 #define CFRAME_OFS_PREV 116
152 #define CFRAME_OFS_L 112
153 #define CFRAME_OFS_PC 20
154 #define CFRAME_OFS_MULTRES 16
155 #define CFRAME_SIZE 112
156 #define CFRAME_SHIFT_MULTRES 3
157 #else
158 #error "Missing CFRAME_* definitions for this architecture"
159 #endif
161 #ifndef CFRAME_SIZE_JIT
162 #define CFRAME_SIZE_JIT CFRAME_SIZE
163 #endif
165 #define CFRAME_RESUME 1
166 #define CFRAME_UNWIND_FF 2 /* Only used in unwinder. */
167 #define CFRAME_RAWMASK (~(intptr_t)(CFRAME_RESUME|CFRAME_UNWIND_FF))
169 #define cframe_errfunc(cf) (*(int32_t *)(((char *)(cf))+CFRAME_OFS_ERRF))
170 #define cframe_nres(cf) (*(int32_t *)(((char *)(cf))+CFRAME_OFS_NRES))
171 #define cframe_prev(cf) (*(void **)(((char *)(cf))+CFRAME_OFS_PREV))
172 #define cframe_multres(cf) (*(uint32_t *)(((char *)(cf))+CFRAME_OFS_MULTRES))
173 #define cframe_multres_n(cf) (cframe_multres((cf)) >> CFRAME_SHIFT_MULTRES)
174 #define cframe_L(cf) \
175 (&gcref(*(GCRef *)(((char *)(cf))+CFRAME_OFS_L))->th)
176 #define cframe_pc(cf) \
177 (mref(*(MRef *)(((char *)(cf))+CFRAME_OFS_PC), const BCIns))
178 #define setcframe_L(cf, L) \
179 (setmref(*(MRef *)(((char *)(cf))+CFRAME_OFS_L), (L)))
180 #define setcframe_pc(cf, pc) \
181 (setmref(*(MRef *)(((char *)(cf))+CFRAME_OFS_PC), (pc)))
182 #define cframe_canyield(cf) ((intptr_t)(cf) & CFRAME_RESUME)
183 #define cframe_unwind_ff(cf) ((intptr_t)(cf) & CFRAME_UNWIND_FF)
184 #define cframe_raw(cf) ((void *)((intptr_t)(cf) & CFRAME_RAWMASK))
185 #define cframe_Lpc(L) cframe_pc(cframe_raw(L->cframe))
187 #endif