This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate:
[perl5.git] / scope.h
CommitLineData
d6376244
JH
1/* scope.h
2 *
e6906430 3 * Copyright (C) 1993, 1994, 1996, 1997, 1998, 1999,
31ab2e0d 4 * 2000, 2001, 2002, 2004, 2005 by Larry Wall and others
d6376244
JH
5 *
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8 *
9 */
10
b9d12d37
GS
11#define SAVEt_ITEM 0
12#define SAVEt_SV 1
13#define SAVEt_AV 2
14#define SAVEt_HV 3
15#define SAVEt_INT 4
16#define SAVEt_LONG 5
17#define SAVEt_I32 6
18#define SAVEt_IV 7
19#define SAVEt_SPTR 8
20#define SAVEt_APTR 9
21#define SAVEt_HPTR 10
22#define SAVEt_PPTR 11
23#define SAVEt_NSTAB 12
24#define SAVEt_SVREF 13
f0e060fc 25#define SAVEt_GP_OLD 14 /* Old style save, kept for bincompat. */
b9d12d37
GS
26#define SAVEt_FREESV 15
27#define SAVEt_FREEOP 16
28#define SAVEt_FREEPV 17
29#define SAVEt_CLEARSV 18
30#define SAVEt_DELETE 19
31#define SAVEt_DESTRUCTOR 20
32#define SAVEt_REGCONTEXT 21
33#define SAVEt_STACK_POS 22
34#define SAVEt_I16 23
35#define SAVEt_AELEM 24
36#define SAVEt_HELEM 25
37#define SAVEt_OP 26
38#define SAVEt_HINTS 27
39#define SAVEt_ALLOC 28
40#define SAVEt_GENERIC_SVREF 29
c76ac1ee 41#define SAVEt_DESTRUCTOR_X 30
7766f137 42#define SAVEt_VPTR 31
e8347627 43#define SAVEt_I8 32
354992b1 44#define SAVEt_COMPPAD 33
f4dd75d9 45#define SAVEt_GENERIC_PVREF 34
c3564e5c 46#define SAVEt_PADSV 35
26d9b02f 47#define SAVEt_MORTALIZESV 36
05ec9bb3 48#define SAVEt_SHARED_PVREF 37
4f4e7967 49#define SAVEt_BOOL 38
240fcc4a 50#define SAVEt_SAVESWITCHSTACK 40
2cb86c03 51#define SAVEt_COP_ARYBASE 41
4c49a5e0 52#define SAVEt_RE_STATE 42
f0e060fc
NC
53#define SAVEt_GP_NEW 43
54
55#ifndef PERL_CORE
56/* There may just be something out there hand building save stacks. */
57# define SAVEt_GP SAVEt_GP_OLD
58#endif
bf9e0bfe 59#define SAVEt_STACK_CXPOS 44
79072805 60
b03c0a3a 61#ifndef SCOPE_SAVES_SIGNAL_MASK
1b266415 62#define SCOPE_SAVES_SIGNAL_MASK 0
b03c0a3a
NIS
63#endif
64
9f01e09a
MHM
65#define SSCHECK(need) if (PL_savestack_ix + (I32)(need) > PL_savestack_max) savestack_grow()
66#define SSGROW(need) if (PL_savestack_ix + (I32)(need) > PL_savestack_max) savestack_grow_cnt(need)
3280af22
NIS
67#define SSPUSHINT(i) (PL_savestack[PL_savestack_ix++].any_i32 = (I32)(i))
68#define SSPUSHLONG(i) (PL_savestack[PL_savestack_ix++].any_long = (long)(i))
4f4e7967 69#define SSPUSHBOOL(p) (PL_savestack[PL_savestack_ix++].any_bool = (p))
3280af22
NIS
70#define SSPUSHIV(i) (PL_savestack[PL_savestack_ix++].any_iv = (IV)(i))
71#define SSPUSHPTR(p) (PL_savestack[PL_savestack_ix++].any_ptr = (void*)(p))
72#define SSPUSHDPTR(p) (PL_savestack[PL_savestack_ix++].any_dptr = (p))
c76ac1ee 73#define SSPUSHDXPTR(p) (PL_savestack[PL_savestack_ix++].any_dxptr = (p))
3280af22
NIS
74#define SSPOPINT (PL_savestack[--PL_savestack_ix].any_i32)
75#define SSPOPLONG (PL_savestack[--PL_savestack_ix].any_long)
4f4e7967 76#define SSPOPBOOL (PL_savestack[--PL_savestack_ix].any_bool)
3280af22
NIS
77#define SSPOPIV (PL_savestack[--PL_savestack_ix].any_iv)
78#define SSPOPPTR (PL_savestack[--PL_savestack_ix].any_ptr)
79#define SSPOPDPTR (PL_savestack[--PL_savestack_ix].any_dptr)
c76ac1ee 80#define SSPOPDXPTR (PL_savestack[--PL_savestack_ix].any_dxptr)
8990e307 81
954c1994 82/*
ccfc67b7
JH
83=head1 Callback Functions
84
954c1994
GS
85=for apidoc Ams||SAVETMPS
86Opening bracket for temporaries on a callback. See C<FREETMPS> and
87L<perlcall>.
88
89=for apidoc Ams||FREETMPS
90Closing bracket for temporaries on a callback. See C<SAVETMPS> and
91L<perlcall>.
92
93=for apidoc Ams||ENTER
94Opening bracket on a callback. See C<LEAVE> and L<perlcall>.
95
96=for apidoc Ams||LEAVE
97Closing bracket on a callback. See C<ENTER> and L<perlcall>.
98
99=cut
100*/
101
3280af22
NIS
102#define SAVETMPS save_int((int*)&PL_tmps_floor), PL_tmps_floor = PL_tmps_ix
103#define FREETMPS if (PL_tmps_ix > PL_tmps_floor) free_tmps()
a0d0e21e 104
f46d017c
GS
105#ifdef DEBUGGING
106#define ENTER \
107 STMT_START { \
108 push_scope(); \
1aa6899f 109 DEBUG_SCOPE("ENTER") \
f46d017c
GS
110 } STMT_END
111#define LEAVE \
112 STMT_START { \
1aa6899f 113 DEBUG_SCOPE("LEAVE") \
f46d017c
GS
114 pop_scope(); \
115 } STMT_END
116#else
a0d0e21e
LW
117#define ENTER push_scope()
118#define LEAVE pop_scope()
f46d017c 119#endif
3280af22 120#define LEAVE_SCOPE(old) if (PL_savestack_ix > old) leave_scope(old)
a0d0e21e 121
b21e3693
NC
122#define SAVEI8(i) save_I8((I8*)&(i))
123#define SAVEI16(i) save_I16((I16*)&(i))
124#define SAVEI32(i) save_I32((I32*)&(i))
125#define SAVEINT(i) save_int((int*)&(i))
126#define SAVEIV(i) save_iv((IV*)&(i))
127#define SAVELONG(l) save_long((long*)&(l))
128#define SAVEBOOL(b) save_bool((bool*)&(b))
55497cff 129#define SAVESPTR(s) save_sptr((SV**)&(s))
b21e3693 130#define SAVEPPTR(s) save_pptr((char**)&(s))
57b2e452 131#define SAVEVPTR(s) save_vptr((void*)&(s))
c3564e5c 132#define SAVEPADSV(s) save_padsv(s)
55497cff 133#define SAVEFREESV(s) save_freesv((SV*)(s))
26d9b02f 134#define SAVEMORTALIZESV(s) save_mortalizesv((SV*)(s))
b21e3693
NC
135#define SAVEFREEOP(o) save_freeop((OP*)(o))
136#define SAVEFREEPV(p) save_freepv((char*)(p))
137#define SAVECLEARSV(sv) save_clearsv((SV**)&(sv))
b9d12d37 138#define SAVEGENERICSV(s) save_generic_svref((SV**)&(s))
f4dd75d9 139#define SAVEGENERICPV(s) save_generic_pvref((char**)&(s))
05ec9bb3 140#define SAVESHAREDPV(s) save_shared_pvref((char**)&(s))
55497cff 141#define SAVEDELETE(h,k,l) \
b21e3693 142 save_delete((HV*)(h), (char*)(k), (I32)(l))
55497cff 143#define SAVEDESTRUCTOR(f,p) \
b21e3693 144 save_destructor((DESTRUCTORFUNC_NOCONTEXT_t)(f), (void*)(p))
c76ac1ee
GS
145
146#define SAVEDESTRUCTOR_X(f,p) \
b21e3693 147 save_destructor_x((DESTRUCTORFUNC_t)(f), (void*)(p))
25eaa213
GS
148
149#define SAVESTACK_POS() \
150 STMT_START { \
151 SSCHECK(2); \
3280af22 152 SSPUSHINT(PL_stack_sp - PL_stack_base); \
25eaa213
GS
153 SSPUSHINT(SAVEt_STACK_POS); \
154 } STMT_END
155
462e5cf6 156#define SAVEOP() save_op()
25eaa213
GS
157
158#define SAVEHINTS() \
1f26b251
NC
159 STMT_START { \
160 SSCHECK(3); \
161 if (PL_hints & HINT_LOCALIZE_HH) { \
162 SSPUSHPTR(GvHV(PL_hintgv)); \
163 GvHV(PL_hintgv) = newHVhv(GvHV(PL_hintgv)); \
164 } \
165 SSPUSHINT(PL_hints); \
166 SSPUSHINT(SAVEt_HINTS); \
25eaa213 167 } STMT_END
354992b1
GS
168
169#define SAVECOMPPAD() \
170 STMT_START { \
d7afa7f5
JH
171 SSCHECK(2); \
172 SSPUSHPTR((SV*)PL_comppad); \
173 SSPUSHINT(SAVEt_COMPPAD); \
354992b1 174 } STMT_END
a9332b4a 175
240fcc4a
JC
176#define SAVESWITCHSTACK(f,t) \
177 STMT_START { \
178 SSCHECK(3); \
179 SSPUSHPTR((SV*)(f)); \
180 SSPUSHPTR((SV*)(t)); \
181 SSPUSHINT(SAVEt_SAVESWITCHSTACK); \
182 SWITCHSTACK((f),(t)); \
183 PL_curstackinfo->si_stack = (t); \
184 } STMT_END
185
2cb86c03
NC
186#define SAVECOPARYBASE(c) \
187 STMT_START { \
188 SSCHECK(3); \
189 SSPUSHINT(CopARYBASE_get(c)); \
190 SSPUSHPTR(c); \
191 SSPUSHINT(SAVEt_COP_ARYBASE); \
192 } STMT_END
193
194
bf9e0bfe
NC
195#define SAVESTACK_CXPOS() \
196 STMT_START { \
197 SSCHECK(3); \
198 SSPUSHINT(cxstack[cxstack_ix].blk_oldsp); \
199 SSPUSHINT(cxstack_ix); \
200 SSPUSHINT(SAVEt_STACK_CXPOS); \
201 } STMT_END
202
57843af0 203#ifdef USE_ITHREADS
f4dd75d9 204# define SAVECOPSTASH(c) SAVEPPTR(CopSTASHPV(c))
05ec9bb3 205# define SAVECOPSTASH_FREE(c) SAVESHAREDPV(CopSTASHPV(c))
f4dd75d9 206# define SAVECOPFILE(c) SAVEPPTR(CopFILE(c))
05ec9bb3 207# define SAVECOPFILE_FREE(c) SAVESHAREDPV(CopFILE(c))
57843af0 208#else
f4dd75d9
GS
209# define SAVECOPSTASH(c) SAVESPTR(CopSTASH(c))
210# define SAVECOPSTASH_FREE(c) SAVECOPSTASH(c) /* XXX not refcounted */
211# define SAVECOPFILE(c) SAVESPTR(CopFILEGV(c))
212# define SAVECOPFILE_FREE(c) SAVEGENERICSV(CopFILEGV(c))
57843af0
GS
213#endif
214
219ef941 215#define SAVECOPLINE(c) SAVEI32(CopLINE(c))
57843af0 216
455ece5e
AD
217/* SSNEW() temporarily allocates a specified number of bytes of data on the
218 * savestack. It returns an integer index into the savestack, because a
219 * pointer would get broken if the savestack is moved on reallocation.
220 * SSNEWa() works like SSNEW(), but also aligns the data to the specified
221 * number of bytes. MEM_ALIGNBYTES is perhaps the most useful. The
222 * alignment will be preserved therough savestack reallocation *only* if
5332c881 223 * realloc returns data aligned to a size divisible by "align"!
455ece5e
AD
224 *
225 * SSPTR() converts the index returned by SSNEW/SSNEWa() into a pointer.
226 */
227
f2b2c1a7 228#define SSNEW(size) Perl_save_alloc(aTHX_ (size), 0)
02db2b7b 229#define SSNEWt(n,t) SSNEW((n)*sizeof(t))
f2b2c1a7 230#define SSNEWa(size,align) Perl_save_alloc(aTHX_ (size), \
455ece5e 231 (align - ((int)((caddr_t)&PL_savestack[PL_savestack_ix]) % align)) % align)
02db2b7b 232#define SSNEWat(n,t,align) SSNEWa((n)*sizeof(t), align)
455ece5e 233
02db2b7b
IZ
234#define SSPTR(off,type) ((type) ((char*)PL_savestack + off))
235#define SSPTRt(off,type) ((type*) ((char*)PL_savestack + off))
455ece5e 236
54310121 237/* A jmpenv packages the state required to perform a proper non-local jump.
238 * Note that there is a start_env initialized when perl starts, and top_env
239 * points to this initially, so top_env should always be non-null.
240 *
241 * Existence of a non-null top_env->je_prev implies it is valid to call
6224f72b
GS
242 * longjmp() at that runlevel (we make sure start_env.je_prev is always
243 * null to ensure this).
54310121 244 *
245 * je_mustcatch, when set at any runlevel to TRUE, means eval ops must
246 * establish a local jmpenv to handle exception traps. Care must be taken
247 * to restore the previous value of je_mustcatch before exiting the
248 * stack frame iff JMPENV_PUSH was not called in that stack frame.
6224f72b 249 * GSAR 97-03-27
54310121 250 */
251
252struct jmpenv {
253 struct jmpenv * je_prev;
312caa8e
CS
254 Sigjmp_buf je_buf; /* only for use if !je_throw */
255 int je_ret; /* last exception thrown */
256 bool je_mustcatch; /* need to call longjmp()? */
14dd3ad8 257#ifdef PERL_FLEXIBLE_EXCEPTIONS
312caa8e 258 void (*je_throw)(int v); /* last for bincompat */
db36c5a1 259 bool je_noset; /* no need for setjmp() */
14dd3ad8 260#endif
1163b5c4 261};
1163b5c4 262
6224f72b 263typedef struct jmpenv JMPENV;
1163b5c4 264
14dd3ad8
GS
265#ifdef OP_IN_REGISTER
266#define OP_REG_TO_MEM PL_opsave = op
267#define OP_MEM_TO_REG op = PL_opsave
268#else
269#define OP_REG_TO_MEM NOOP
270#define OP_MEM_TO_REG NOOP
271#endif
312caa8e
CS
272
273/*
274 * How to build the first jmpenv.
275 *
276 * top_env needs to be non-zero. It points to an area
277 * in which longjmp() stuff is stored, as C callstack
278 * info there at least is thread specific this has to
279 * be per-thread. Otherwise a 'die' in a thread gives
280 * that thread the C stack of last thread to do an eval {}!
281 */
282
283#define JMPENV_BOOTSTRAP \
284 STMT_START { \
14dd3ad8 285 Zero(&PL_start_env, 1, JMPENV); \
312caa8e
CS
286 PL_start_env.je_ret = -1; \
287 PL_start_env.je_mustcatch = TRUE; \
288 PL_top_env = &PL_start_env; \
289 } STMT_END
290
14dd3ad8 291#ifdef PERL_FLEXIBLE_EXCEPTIONS
462e5cf6 292
312caa8e
CS
293/*
294 * These exception-handling macros are split up to
295 * ease integration with C++ exceptions.
296 *
297 * To use C++ try+catch to catch Perl exceptions, an extension author
298 * needs to first write an extern "C" function to throw an appropriate
299 * exception object; typically it will be or contain an integer,
300 * because Perl's internals use integers to track exception types:
301 * extern "C" { static void thrower(int i) { throw i; } }
302 *
303 * Then (as shown below) the author needs to use, not the simple
304 * JMPENV_PUSH, but several of its constitutent macros, to arrange for
305 * the Perl internals to call thrower() rather than longjmp() to
306 * report exceptions:
307 *
308 * dJMPENV;
309 * JMPENV_PUSH_INIT(thrower);
310 * try {
311 * ... stuff that may throw exceptions ...
312 * }
313 * catch (int why) { // or whatever matches thrower()
314 * JMPENV_POST_CATCH;
315 * EXCEPT_SET(why);
316 * switch (why) {
317 * ... // handle various Perl exception codes
318 * }
319 * }
320 * JMPENV_POP; // don't forget this!
321 */
322
14dd3ad8
GS
323/*
324 * Function that catches/throws, and its callback for the
325 * body of protected processing.
326 */
327typedef void *(CPERLscope(*protect_body_t)) (pTHX_ va_list);
328typedef void *(CPERLscope(*protect_proc_t)) (pTHX_ volatile JMPENV *pcur_env,
329 int *, protect_body_t, ...);
330
db36c5a1
GS
331#define dJMPENV JMPENV cur_env; \
332 volatile JMPENV *pcur_env = ((cur_env.je_noset = 0),&cur_env)
312caa8e 333
db36c5a1 334#define JMPENV_PUSH_INIT_ENV(ce,THROWFUNC) \
6224f72b 335 STMT_START { \
db36c5a1
GS
336 (ce).je_throw = (THROWFUNC); \
337 (ce).je_ret = -1; \
338 (ce).je_mustcatch = FALSE; \
339 (ce).je_prev = PL_top_env; \
340 PL_top_env = &(ce); \
6224f72b 341 OP_REG_TO_MEM; \
312caa8e 342 } STMT_END
1d651c14 343
f2b2c1a7 344#define JMPENV_PUSH_INIT(THROWFUNC) JMPENV_PUSH_INIT_ENV(*(JMPENV*)pcur_env,THROWFUNC)
1d651c14 345
db36c5a1 346#define JMPENV_POST_CATCH_ENV(ce) \
312caa8e 347 STMT_START { \
6224f72b 348 OP_MEM_TO_REG; \
db36c5a1 349 PL_top_env = &(ce); \
6224f72b 350 } STMT_END
312caa8e 351
db36c5a1 352#define JMPENV_POST_CATCH JMPENV_POST_CATCH_ENV(*(JMPENV*)pcur_env)
1d651c14 353
db36c5a1
GS
354#define JMPENV_PUSH_ENV(ce,v) \
355 STMT_START { \
356 if (!(ce).je_noset) { \
14dd3ad8 357 DEBUG_l(Perl_deb(aTHX_ "Setting up jumplevel %p, was %p\n", \
f2139bd3 358 (void*)ce, (void*)PL_top_env)); \
db36c5a1 359 JMPENV_PUSH_INIT_ENV(ce,NULL); \
b03c0a3a 360 EXCEPT_SET_ENV(ce,PerlProc_setjmp((ce).je_buf, SCOPE_SAVES_SIGNAL_MASK));\
db36c5a1
GS
361 (ce).je_noset = 1; \
362 } \
363 else \
364 EXCEPT_SET_ENV(ce,0); \
365 JMPENV_POST_CATCH_ENV(ce); \
366 (v) = EXCEPT_GET_ENV(ce); \
312caa8e
CS
367 } STMT_END
368
f2b2c1a7 369#define JMPENV_PUSH(v) JMPENV_PUSH_ENV(*(JMPENV*)pcur_env,v)
1d651c14 370
db36c5a1 371#define JMPENV_POP_ENV(ce) \
14dd3ad8
GS
372 STMT_START { \
373 if (PL_top_env == &(ce)) \
374 PL_top_env = (ce).je_prev; \
375 } STMT_END
312caa8e 376
f2b2c1a7 377#define JMPENV_POP JMPENV_POP_ENV(*(JMPENV*)pcur_env)
1d651c14 378
22921e25
CS
379#define JMPENV_JUMP(v) \
380 STMT_START { \
462e5cf6 381 OP_REG_TO_MEM; \
312caa8e
CS
382 if (PL_top_env->je_prev) { \
383 if (PL_top_env->je_throw) \
384 PL_top_env->je_throw(v); \
385 else \
386 PerlProc_longjmp(PL_top_env->je_buf, (v)); \
387 } \
6224f72b 388 if ((v) == 2) \
b14528dd 389 PerlProc_exit(STATUS_EXIT); \
bf49b057 390 PerlIO_printf(Perl_error_log, "panic: top_env\n"); \
312caa8e 391 PerlProc_exit(1); \
22921e25 392 } STMT_END
312caa8e 393
db36c5a1
GS
394#define EXCEPT_GET_ENV(ce) ((ce).je_ret)
395#define EXCEPT_GET EXCEPT_GET_ENV(*(JMPENV*)pcur_env)
396#define EXCEPT_SET_ENV(ce,v) ((ce).je_ret = (v))
397#define EXCEPT_SET(v) EXCEPT_SET_ENV(*(JMPENV*)pcur_env,v)
312caa8e 398
14dd3ad8
GS
399#else /* !PERL_FLEXIBLE_EXCEPTIONS */
400
401#define dJMPENV JMPENV cur_env
402
403#define JMPENV_PUSH(v) \
404 STMT_START { \
405 DEBUG_l(Perl_deb(aTHX_ "Setting up jumplevel %p, was %p\n", \
f2139bd3 406 (void*)&cur_env, (void*)PL_top_env)); \
14dd3ad8
GS
407 cur_env.je_prev = PL_top_env; \
408 OP_REG_TO_MEM; \
b03c0a3a 409 cur_env.je_ret = PerlProc_setjmp(cur_env.je_buf, SCOPE_SAVES_SIGNAL_MASK); \
14dd3ad8
GS
410 OP_MEM_TO_REG; \
411 PL_top_env = &cur_env; \
412 cur_env.je_mustcatch = FALSE; \
413 (v) = cur_env.je_ret; \
414 } STMT_END
415
416#define JMPENV_POP \
417 STMT_START { PL_top_env = cur_env.je_prev; } STMT_END
418
419#define JMPENV_JUMP(v) \
420 STMT_START { \
421 OP_REG_TO_MEM; \
422 if (PL_top_env->je_prev) \
423 PerlProc_longjmp(PL_top_env->je_buf, (v)); \
424 if ((v) == 2) \
b14528dd 425 PerlProc_exit(STATUS_EXIT); \
14dd3ad8
GS
426 PerlIO_printf(PerlIO_stderr(), "panic: top_env\n"); \
427 PerlProc_exit(1); \
428 } STMT_END
429
430#endif /* PERL_FLEXIBLE_EXCEPTIONS */
431
db36c5a1
GS
432#define CATCH_GET (PL_top_env->je_mustcatch)
433#define CATCH_SET(v) (PL_top_env->je_mustcatch = (v))