1 /*
2 ** Stack frames.
3 ** Copyright (C) 2005-2014 Mike Pall. See Copyright Notice in luajit.h
4 */
5 
6 #ifndef _LJ_FRAME_H
7 #define _LJ_FRAME_H
8 
9 #include "lj_obj.h"
10 #include "lj_bc.h"
11 
12 /* -- Lua stack frame ----------------------------------------------------- */
13 
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
18 };
19 #define FRAME_TYPE		3
20 #define FRAME_P			4
21 #define FRAME_TYPEP		(FRAME_TYPE|FRAME_P)
22 
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)
27 
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)
35 
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)
47 
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. */
52 
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)))
56 
57 /* -- C stack frame ------------------------------------------------------- */
58 
59 /* Macros to access and modify the C stack frame chain. */
60 
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 #define CFRAME_SIZE		(10*8)
90 #define CFRAME_SIZE_JIT		(CFRAME_SIZE + 16)
91 #define CFRAME_SHIFT_MULTRES	0
92 #endif
93 #elif LJ_TARGET_ARM
94 #define CFRAME_OFS_ERRF		24
95 #define CFRAME_OFS_NRES		20
96 #define CFRAME_OFS_PREV		16
97 #define CFRAME_OFS_L		12
98 #define CFRAME_OFS_PC		8
99 #define CFRAME_OFS_MULTRES	4
100 #if LJ_ARCH_HASFPU
101 #define CFRAME_SIZE		128
102 #else
103 #define CFRAME_SIZE		64
104 #endif
105 #define CFRAME_SHIFT_MULTRES	3
106 #elif LJ_TARGET_PPC
107 #if LJ_TARGET_XBOX360
108 #define CFRAME_OFS_ERRF		424
109 #define CFRAME_OFS_NRES		420
110 #define CFRAME_OFS_PREV		400
111 #define CFRAME_OFS_L		416
112 #define CFRAME_OFS_PC		412
113 #define CFRAME_OFS_MULTRES	408
114 #define CFRAME_SIZE		384
115 #define CFRAME_SHIFT_MULTRES	3
116 #elif LJ_ARCH_PPC64
117 #define CFRAME_OFS_ERRF		472
118 #define CFRAME_OFS_NRES		468
119 #define CFRAME_OFS_PREV		448
120 #define CFRAME_OFS_L		464
121 #define CFRAME_OFS_PC		460
122 #define CFRAME_OFS_MULTRES	456
123 #define CFRAME_SIZE		400
124 #define CFRAME_SHIFT_MULTRES	3
125 #else
126 #define CFRAME_OFS_ERRF		48
127 #define CFRAME_OFS_NRES		44
128 #define CFRAME_OFS_PREV		40
129 #define CFRAME_OFS_L		36
130 #define CFRAME_OFS_PC		32
131 #define CFRAME_OFS_MULTRES	28
132 #define CFRAME_SIZE		272
133 #define CFRAME_SHIFT_MULTRES	3
134 #endif
135 #elif LJ_TARGET_PPCSPE
136 #define CFRAME_OFS_ERRF		28
137 #define CFRAME_OFS_NRES		24
138 #define CFRAME_OFS_PREV		20
139 #define CFRAME_OFS_L		16
140 #define CFRAME_OFS_PC		12
141 #define CFRAME_OFS_MULTRES	8
142 #define CFRAME_SIZE		184
143 #define CFRAME_SHIFT_MULTRES	3
144 #elif LJ_TARGET_MIPS
145 #define CFRAME_OFS_ERRF		124
146 #define CFRAME_OFS_NRES		120
147 #define CFRAME_OFS_PREV		116
148 #define CFRAME_OFS_L		112
149 #define CFRAME_OFS_PC		20
150 #define CFRAME_OFS_MULTRES	16
151 #define CFRAME_SIZE		112
152 #define CFRAME_SHIFT_MULTRES	3
153 #else
154 #error "Missing CFRAME_* definitions for this architecture"
155 #endif
156 
157 #ifndef CFRAME_SIZE_JIT
158 #define CFRAME_SIZE_JIT		CFRAME_SIZE
159 #endif
160 
161 #define CFRAME_RESUME		1
162 #define CFRAME_UNWIND_FF	2  /* Only used in unwinder. */
163 #define CFRAME_RAWMASK		(~(intptr_t)(CFRAME_RESUME|CFRAME_UNWIND_FF))
164 
165 #define cframe_errfunc(cf)	(*(int32_t *)(((char *)(cf))+CFRAME_OFS_ERRF))
166 #define cframe_nres(cf)		(*(int32_t *)(((char *)(cf))+CFRAME_OFS_NRES))
167 #define cframe_prev(cf)		(*(void **)(((char *)(cf))+CFRAME_OFS_PREV))
168 #define cframe_multres(cf)  (*(uint32_t *)(((char *)(cf))+CFRAME_OFS_MULTRES))
169 #define cframe_multres_n(cf)	(cframe_multres((cf)) >> CFRAME_SHIFT_MULTRES)
170 #define cframe_L(cf) \
171   (&gcref(*(GCRef *)(((char *)(cf))+CFRAME_OFS_L))->th)
172 #define cframe_pc(cf) \
173   (mref(*(MRef *)(((char *)(cf))+CFRAME_OFS_PC), const BCIns))
174 #define setcframe_L(cf, L) \
175   (setmref(*(MRef *)(((char *)(cf))+CFRAME_OFS_L), (L)))
176 #define setcframe_pc(cf, pc) \
177   (setmref(*(MRef *)(((char *)(cf))+CFRAME_OFS_PC), (pc)))
178 #define cframe_canyield(cf)	((intptr_t)(cf) & CFRAME_RESUME)
179 #define cframe_unwind_ff(cf)	((intptr_t)(cf) & CFRAME_UNWIND_FF)
180 #define cframe_raw(cf)		((void *)((intptr_t)(cf) & CFRAME_RAWMASK))
181 #define cframe_Lpc(L)		cframe_pc(cframe_raw(L->cframe))
182 
183 #endif
184