This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
s/croak/Perl_croak/
[perl5.git] / scope.h
CommitLineData
b9d12d37
GS
1#define SAVEt_ITEM 0
2#define SAVEt_SV 1
3#define SAVEt_AV 2
4#define SAVEt_HV 3
5#define SAVEt_INT 4
6#define SAVEt_LONG 5
7#define SAVEt_I32 6
8#define SAVEt_IV 7
9#define SAVEt_SPTR 8
10#define SAVEt_APTR 9
11#define SAVEt_HPTR 10
12#define SAVEt_PPTR 11
13#define SAVEt_NSTAB 12
14#define SAVEt_SVREF 13
15#define SAVEt_GP 14
16#define SAVEt_FREESV 15
17#define SAVEt_FREEOP 16
18#define SAVEt_FREEPV 17
19#define SAVEt_CLEARSV 18
20#define SAVEt_DELETE 19
21#define SAVEt_DESTRUCTOR 20
22#define SAVEt_REGCONTEXT 21
23#define SAVEt_STACK_POS 22
24#define SAVEt_I16 23
25#define SAVEt_AELEM 24
26#define SAVEt_HELEM 25
27#define SAVEt_OP 26
28#define SAVEt_HINTS 27
29#define SAVEt_ALLOC 28
30#define SAVEt_GENERIC_SVREF 29
c76ac1ee 31#define SAVEt_DESTRUCTOR_X 30
7766f137 32#define SAVEt_VPTR 31
e8347627 33#define SAVEt_I8 32
354992b1 34#define SAVEt_COMPPAD 33
79072805 35
3280af22
NIS
36#define SSCHECK(need) if (PL_savestack_ix + need > PL_savestack_max) savestack_grow()
37#define SSPUSHINT(i) (PL_savestack[PL_savestack_ix++].any_i32 = (I32)(i))
38#define SSPUSHLONG(i) (PL_savestack[PL_savestack_ix++].any_long = (long)(i))
39#define SSPUSHIV(i) (PL_savestack[PL_savestack_ix++].any_iv = (IV)(i))
40#define SSPUSHPTR(p) (PL_savestack[PL_savestack_ix++].any_ptr = (void*)(p))
41#define SSPUSHDPTR(p) (PL_savestack[PL_savestack_ix++].any_dptr = (p))
c76ac1ee 42#define SSPUSHDXPTR(p) (PL_savestack[PL_savestack_ix++].any_dxptr = (p))
3280af22
NIS
43#define SSPOPINT (PL_savestack[--PL_savestack_ix].any_i32)
44#define SSPOPLONG (PL_savestack[--PL_savestack_ix].any_long)
45#define SSPOPIV (PL_savestack[--PL_savestack_ix].any_iv)
46#define SSPOPPTR (PL_savestack[--PL_savestack_ix].any_ptr)
47#define SSPOPDPTR (PL_savestack[--PL_savestack_ix].any_dptr)
c76ac1ee 48#define SSPOPDXPTR (PL_savestack[--PL_savestack_ix].any_dxptr)
8990e307 49
954c1994
GS
50/*
51=for apidoc Ams||SAVETMPS
52Opening bracket for temporaries on a callback. See C<FREETMPS> and
53L<perlcall>.
54
55=for apidoc Ams||FREETMPS
56Closing bracket for temporaries on a callback. See C<SAVETMPS> and
57L<perlcall>.
58
59=for apidoc Ams||ENTER
60Opening bracket on a callback. See C<LEAVE> and L<perlcall>.
61
62=for apidoc Ams||LEAVE
63Closing bracket on a callback. See C<ENTER> and L<perlcall>.
64
65=cut
66*/
67
3280af22
NIS
68#define SAVETMPS save_int((int*)&PL_tmps_floor), PL_tmps_floor = PL_tmps_ix
69#define FREETMPS if (PL_tmps_ix > PL_tmps_floor) free_tmps()
a0d0e21e 70
f46d017c
GS
71#ifdef DEBUGGING
72#define ENTER \
73 STMT_START { \
74 push_scope(); \
cea2e8a9 75 DEBUG_l(WITH_THR(Perl_deb(aTHX_ "ENTER scope %ld at %s:%d\n", \
3280af22 76 PL_scopestack_ix, __FILE__, __LINE__))); \
f46d017c
GS
77 } STMT_END
78#define LEAVE \
79 STMT_START { \
cea2e8a9 80 DEBUG_l(WITH_THR(Perl_deb(aTHX_ "LEAVE scope %ld at %s:%d\n", \
3280af22 81 PL_scopestack_ix, __FILE__, __LINE__))); \
f46d017c
GS
82 pop_scope(); \
83 } STMT_END
84#else
a0d0e21e
LW
85#define ENTER push_scope()
86#define LEAVE pop_scope()
f46d017c 87#endif
3280af22 88#define LEAVE_SCOPE(old) if (PL_savestack_ix > old) leave_scope(old)
a0d0e21e 89
55497cff 90/*
b9d12d37 91 * Not using SOFT_CAST on SAVESPTR, SAVEGENERICSV and SAVEFREESV
55497cff 92 * because these are used for several kinds of pointer values
93 */
e8347627 94#define SAVEI8(i) save_I8(SOFT_CAST(I8*)&(i))
4fdae800 95#define SAVEI16(i) save_I16(SOFT_CAST(I16*)&(i))
96#define SAVEI32(i) save_I32(SOFT_CAST(I32*)&(i))
97#define SAVEINT(i) save_int(SOFT_CAST(int*)&(i))
98#define SAVEIV(i) save_iv(SOFT_CAST(IV*)&(i))
99#define SAVELONG(l) save_long(SOFT_CAST(long*)&(l))
55497cff 100#define SAVESPTR(s) save_sptr((SV**)&(s))
101#define SAVEPPTR(s) save_pptr(SOFT_CAST(char**)&(s))
57b2e452 102#define SAVEVPTR(s) save_vptr((void*)&(s))
55497cff 103#define SAVEFREESV(s) save_freesv((SV*)(s))
104#define SAVEFREEOP(o) save_freeop(SOFT_CAST(OP*)(o))
105#define SAVEFREEPV(p) save_freepv(SOFT_CAST(char*)(p))
106#define SAVECLEARSV(sv) save_clearsv(SOFT_CAST(SV**)&(sv))
b9d12d37 107#define SAVEGENERICSV(s) save_generic_svref((SV**)&(s))
55497cff 108#define SAVEDELETE(h,k,l) \
109 save_delete(SOFT_CAST(HV*)(h), SOFT_CAST(char*)(k), (I32)(l))
110#define SAVEDESTRUCTOR(f,p) \
c76ac1ee
GS
111 save_destructor((DESTRUCTORFUNC_NOCONTEXT_t)(f), SOFT_CAST(void*)(p))
112
113#define SAVEDESTRUCTOR_X(f,p) \
114 save_destructor_x((DESTRUCTORFUNC_t)(f), SOFT_CAST(void*)(p))
25eaa213
GS
115
116#define SAVESTACK_POS() \
117 STMT_START { \
118 SSCHECK(2); \
3280af22 119 SSPUSHINT(PL_stack_sp - PL_stack_base); \
25eaa213
GS
120 SSPUSHINT(SAVEt_STACK_POS); \
121 } STMT_END
122
462e5cf6 123#define SAVEOP() save_op()
25eaa213
GS
124
125#define SAVEHINTS() \
126 STMT_START { \
3280af22 127 if (PL_hints & HINT_LOCALIZE_HH) \
25eaa213
GS
128 save_hints(); \
129 else { \
130 SSCHECK(2); \
3280af22 131 SSPUSHINT(PL_hints); \
25eaa213
GS
132 SSPUSHINT(SAVEt_HINTS); \
133 } \
134 } STMT_END
354992b1
GS
135
136#define SAVECOMPPAD() \
137 STMT_START { \
138 if (PL_comppad && PL_curpad == AvARRAY(PL_comppad)) { \
139 SSCHECK(2); \
140 SSPUSHPTR((SV*)PL_comppad); \
141 SSPUSHINT(SAVEt_COMPPAD); \
142 } \
143 else { \
144 SAVEVPTR(PL_curpad); \
145 SAVESPTR(PL_comppad); \
146 } \
147 } STMT_END
a9332b4a 148
57843af0 149#ifdef USE_ITHREADS
11faa288 150# define SAVECOPSTASH(cop) SAVEPPTR(CopSTASHPV(cop))
57843af0
GS
151# define SAVECOPFILE(cop) SAVEPPTR(CopFILE(cop))
152#else
11faa288 153# define SAVECOPSTASH(cop) SAVESPTR(CopSTASH(cop))
57843af0
GS
154# define SAVECOPFILE(cop) SAVESPTR(CopFILEGV(cop))
155#endif
156
157#define SAVECOPLINE(cop) SAVEI16(CopLINE(cop))
158
455ece5e
AD
159/* SSNEW() temporarily allocates a specified number of bytes of data on the
160 * savestack. It returns an integer index into the savestack, because a
161 * pointer would get broken if the savestack is moved on reallocation.
162 * SSNEWa() works like SSNEW(), but also aligns the data to the specified
163 * number of bytes. MEM_ALIGNBYTES is perhaps the most useful. The
164 * alignment will be preserved therough savestack reallocation *only* if
165 * realloc returns data aligned to a size divisible by `align'!
166 *
167 * SSPTR() converts the index returned by SSNEW/SSNEWa() into a pointer.
168 */
169
170#define SSNEW(size) save_alloc(size, 0)
171#define SSNEWa(size,align) save_alloc(size, \
172 (align - ((int)((caddr_t)&PL_savestack[PL_savestack_ix]) % align)) % align)
173
174#define SSPTR(off,type) ((type) ((char*)PL_savestack + off))
175
54310121 176/* A jmpenv packages the state required to perform a proper non-local jump.
177 * Note that there is a start_env initialized when perl starts, and top_env
178 * points to this initially, so top_env should always be non-null.
179 *
180 * Existence of a non-null top_env->je_prev implies it is valid to call
6224f72b
GS
181 * longjmp() at that runlevel (we make sure start_env.je_prev is always
182 * null to ensure this).
54310121 183 *
184 * je_mustcatch, when set at any runlevel to TRUE, means eval ops must
185 * establish a local jmpenv to handle exception traps. Care must be taken
186 * to restore the previous value of je_mustcatch before exiting the
187 * stack frame iff JMPENV_PUSH was not called in that stack frame.
6224f72b 188 * GSAR 97-03-27
54310121 189 */
190
191struct jmpenv {
192 struct jmpenv * je_prev;
312caa8e
CS
193 Sigjmp_buf je_buf; /* only for use if !je_throw */
194 int je_ret; /* last exception thrown */
195 bool je_mustcatch; /* need to call longjmp()? */
196 void (*je_throw)(int v); /* last for bincompat */
db36c5a1 197 bool je_noset; /* no need for setjmp() */
1163b5c4 198};
1163b5c4 199
6224f72b 200typedef struct jmpenv JMPENV;
1163b5c4 201
312caa8e
CS
202/*
203 * Function that catches/throws, and its callback for the
204 * body of protected processing.
205 */
cea2e8a9 206typedef void *(CPERLscope(*protect_body_t)) (pTHX_ va_list);
db36c5a1
GS
207typedef void *(CPERLscope(*protect_proc_t)) (pTHX_ volatile JMPENV *pcur_env,
208 int *, protect_body_t, ...);
312caa8e
CS
209
210/*
211 * How to build the first jmpenv.
212 *
213 * top_env needs to be non-zero. It points to an area
214 * in which longjmp() stuff is stored, as C callstack
215 * info there at least is thread specific this has to
216 * be per-thread. Otherwise a 'die' in a thread gives
217 * that thread the C stack of last thread to do an eval {}!
218 */
219
220#define JMPENV_BOOTSTRAP \
221 STMT_START { \
222 PL_start_env.je_prev = NULL; \
223 PL_start_env.je_throw = NULL; \
224 PL_start_env.je_ret = -1; \
225 PL_start_env.je_mustcatch = TRUE; \
db36c5a1 226 PL_start_env.je_noset = 0; \
312caa8e
CS
227 PL_top_env = &PL_start_env; \
228 } STMT_END
229
462e5cf6 230#ifdef OP_IN_REGISTER
6b88bc9c
GS
231#define OP_REG_TO_MEM PL_opsave = op
232#define OP_MEM_TO_REG op = PL_opsave
462e5cf6
MB
233#else
234#define OP_REG_TO_MEM NOOP
235#define OP_MEM_TO_REG NOOP
236#endif
237
312caa8e
CS
238/*
239 * These exception-handling macros are split up to
240 * ease integration with C++ exceptions.
241 *
242 * To use C++ try+catch to catch Perl exceptions, an extension author
243 * needs to first write an extern "C" function to throw an appropriate
244 * exception object; typically it will be or contain an integer,
245 * because Perl's internals use integers to track exception types:
246 * extern "C" { static void thrower(int i) { throw i; } }
247 *
248 * Then (as shown below) the author needs to use, not the simple
249 * JMPENV_PUSH, but several of its constitutent macros, to arrange for
250 * the Perl internals to call thrower() rather than longjmp() to
251 * report exceptions:
252 *
253 * dJMPENV;
254 * JMPENV_PUSH_INIT(thrower);
255 * try {
256 * ... stuff that may throw exceptions ...
257 * }
258 * catch (int why) { // or whatever matches thrower()
259 * JMPENV_POST_CATCH;
260 * EXCEPT_SET(why);
261 * switch (why) {
262 * ... // handle various Perl exception codes
263 * }
264 * }
265 * JMPENV_POP; // don't forget this!
266 */
267
db36c5a1
GS
268#define dJMPENV JMPENV cur_env; \
269 volatile JMPENV *pcur_env = ((cur_env.je_noset = 0),&cur_env)
312caa8e 270
db36c5a1 271#define JMPENV_PUSH_INIT_ENV(ce,THROWFUNC) \
6224f72b 272 STMT_START { \
db36c5a1
GS
273 (ce).je_throw = (THROWFUNC); \
274 (ce).je_ret = -1; \
275 (ce).je_mustcatch = FALSE; \
276 (ce).je_prev = PL_top_env; \
277 PL_top_env = &(ce); \
6224f72b 278 OP_REG_TO_MEM; \
312caa8e 279 } STMT_END
1d651c14 280
db36c5a1 281#define JMPENV_PUSH_INIT(THROWFUNC) JMPENV_PUSH_INIT_ENV(*(JMPENV*)pcur_env,THROWFUNC)
1d651c14 282
db36c5a1 283#define JMPENV_POST_CATCH_ENV(ce) \
312caa8e 284 STMT_START { \
6224f72b 285 OP_MEM_TO_REG; \
db36c5a1 286 PL_top_env = &(ce); \
6224f72b 287 } STMT_END
312caa8e 288
db36c5a1 289#define JMPENV_POST_CATCH JMPENV_POST_CATCH_ENV(*(JMPENV*)pcur_env)
1d651c14
VB
290
291
db36c5a1
GS
292#define JMPENV_PUSH_ENV(ce,v) \
293 STMT_START { \
294 if (!(ce).je_noset) { \
295 JMPENV_PUSH_INIT_ENV(ce,NULL); \
296 EXCEPT_SET_ENV(ce,PerlProc_setjmp((ce).je_buf, 1));\
297 (ce).je_noset = 1; \
298 } \
299 else \
300 EXCEPT_SET_ENV(ce,0); \
301 JMPENV_POST_CATCH_ENV(ce); \
302 (v) = EXCEPT_GET_ENV(ce); \
312caa8e
CS
303 } STMT_END
304
db36c5a1 305#define JMPENV_PUSH(v) JMPENV_PUSH_ENV(*(JMPENV*)pcur_env,v)
1d651c14 306
db36c5a1
GS
307#define JMPENV_POP_ENV(ce) \
308 STMT_START { PL_top_env = (ce).je_prev; } STMT_END
312caa8e 309
db36c5a1 310#define JMPENV_POP JMPENV_POP_ENV(*(JMPENV*)pcur_env)
1d651c14 311
22921e25
CS
312#define JMPENV_JUMP(v) \
313 STMT_START { \
462e5cf6 314 OP_REG_TO_MEM; \
312caa8e
CS
315 if (PL_top_env->je_prev) { \
316 if (PL_top_env->je_throw) \
317 PL_top_env->je_throw(v); \
318 else \
319 PerlProc_longjmp(PL_top_env->je_buf, (v)); \
320 } \
6224f72b 321 if ((v) == 2) \
312caa8e 322 PerlProc_exit(STATUS_NATIVE_EXPORT); \
bf49b057 323 PerlIO_printf(Perl_error_log, "panic: top_env\n"); \
312caa8e 324 PerlProc_exit(1); \
22921e25 325 } STMT_END
312caa8e 326
db36c5a1
GS
327#define EXCEPT_GET_ENV(ce) ((ce).je_ret)
328#define EXCEPT_GET EXCEPT_GET_ENV(*(JMPENV*)pcur_env)
329#define EXCEPT_SET_ENV(ce,v) ((ce).je_ret = (v))
330#define EXCEPT_SET(v) EXCEPT_SET_ENV(*(JMPENV*)pcur_env,v)
312caa8e 331
db36c5a1
GS
332#define CATCH_GET (PL_top_env->je_mustcatch)
333#define CATCH_SET(v) (PL_top_env->je_mustcatch = (v))