3 * Copyright (C) 1993, 1994, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2004, 2005 by Larry Wall and others
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.
23 #define SAVEt_NSTAB 12
24 #define SAVEt_SVREF 13
25 #define SAVEt_GP_OLD 14 /* Old style save, kept for bincompat. */
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
35 #define SAVEt_AELEM 24
36 #define SAVEt_HELEM 25
38 #define SAVEt_HINTS 27
39 #define SAVEt_ALLOC 28
40 #define SAVEt_GENERIC_SVREF 29
41 #define SAVEt_DESTRUCTOR_X 30
44 #define SAVEt_COMPPAD 33
45 #define SAVEt_GENERIC_PVREF 34
46 #define SAVEt_PADSV 35
47 #define SAVEt_MORTALIZESV 36
48 #define SAVEt_SHARED_PVREF 37
50 #define SAVEt_SAVESWITCHSTACK 40
51 #define SAVEt_COP_ARYBASE 41
52 #define SAVEt_RE_STATE 42
53 #define SAVEt_GP_NEW 43
56 /* There may just be something out there hand building save stacks. */
57 # define SAVEt_GP SAVEt_GP_OLD
59 #define SAVEt_STACK_CXPOS 44
61 #ifndef SCOPE_SAVES_SIGNAL_MASK
62 #define SCOPE_SAVES_SIGNAL_MASK 0
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)
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))
69 #define SSPUSHBOOL(p) (PL_savestack[PL_savestack_ix++].any_bool = (p))
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))
73 #define SSPUSHDXPTR(p) (PL_savestack[PL_savestack_ix++].any_dxptr = (p))
74 #define SSPOPINT (PL_savestack[--PL_savestack_ix].any_i32)
75 #define SSPOPLONG (PL_savestack[--PL_savestack_ix].any_long)
76 #define SSPOPBOOL (PL_savestack[--PL_savestack_ix].any_bool)
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)
80 #define SSPOPDXPTR (PL_savestack[--PL_savestack_ix].any_dxptr)
83 =head1 Callback Functions
85 =for apidoc Ams||SAVETMPS
86 Opening bracket for temporaries on a callback. See C<FREETMPS> and
89 =for apidoc Ams||FREETMPS
90 Closing bracket for temporaries on a callback. See C<SAVETMPS> and
93 =for apidoc Ams||ENTER
94 Opening bracket on a callback. See C<LEAVE> and L<perlcall>.
96 =for apidoc Ams||LEAVE
97 Closing bracket on a callback. See C<ENTER> and L<perlcall>.
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()
109 DEBUG_SCOPE("ENTER") \
113 DEBUG_SCOPE("LEAVE") \
117 #define ENTER push_scope()
118 #define LEAVE pop_scope()
120 #define LEAVE_SCOPE(old) if (PL_savestack_ix > old) leave_scope(old)
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))
129 #define SAVESPTR(s) save_sptr((SV**)&(s))
130 #define SAVEPPTR(s) save_pptr((char**)&(s))
131 #define SAVEVPTR(s) save_vptr((void*)&(s))
132 #define SAVEPADSV(s) save_padsv(s)
133 #define SAVEFREESV(s) save_freesv((SV*)(s))
134 #define SAVEMORTALIZESV(s) save_mortalizesv((SV*)(s))
135 #define SAVEFREEOP(o) save_freeop((OP*)(o))
136 #define SAVEFREEPV(p) save_freepv((char*)(p))
137 #define SAVECLEARSV(sv) save_clearsv((SV**)&(sv))
138 #define SAVEGENERICSV(s) save_generic_svref((SV**)&(s))
139 #define SAVEGENERICPV(s) save_generic_pvref((char**)&(s))
140 #define SAVESHAREDPV(s) save_shared_pvref((char**)&(s))
141 #define SAVEDELETE(h,k,l) \
142 save_delete((HV*)(h), (char*)(k), (I32)(l))
143 #define SAVEDESTRUCTOR(f,p) \
144 save_destructor((DESTRUCTORFUNC_NOCONTEXT_t)(f), (void*)(p))
146 #define SAVEDESTRUCTOR_X(f,p) \
147 save_destructor_x((DESTRUCTORFUNC_t)(f), (void*)(p))
149 #define SAVESTACK_POS() \
152 SSPUSHINT(PL_stack_sp - PL_stack_base); \
153 SSPUSHINT(SAVEt_STACK_POS); \
156 #define SAVEOP() save_op()
158 #define SAVEHINTS() \
161 if (PL_hints & HINT_LOCALIZE_HH) { \
162 SSPUSHPTR(GvHV(PL_hintgv)); \
163 GvHV(PL_hintgv) = newHVhv(GvHV(PL_hintgv)); \
165 SSPUSHINT(PL_hints); \
166 SSPUSHINT(SAVEt_HINTS); \
169 #define SAVECOMPPAD() \
172 SSPUSHPTR((SV*)PL_comppad); \
173 SSPUSHINT(SAVEt_COMPPAD); \
176 #define SAVESWITCHSTACK(f,t) \
179 SSPUSHPTR((SV*)(f)); \
180 SSPUSHPTR((SV*)(t)); \
181 SSPUSHINT(SAVEt_SAVESWITCHSTACK); \
182 SWITCHSTACK((f),(t)); \
183 PL_curstackinfo->si_stack = (t); \
186 #define SAVECOPARYBASE(c) \
189 SSPUSHINT(CopARYBASE_get(c)); \
191 SSPUSHINT(SAVEt_COP_ARYBASE); \
195 #define SAVESTACK_CXPOS() \
198 SSPUSHINT(cxstack[cxstack_ix].blk_oldsp); \
199 SSPUSHINT(cxstack_ix); \
200 SSPUSHINT(SAVEt_STACK_CXPOS); \
204 # define SAVECOPSTASH(c) SAVEPPTR(CopSTASHPV(c))
205 # define SAVECOPSTASH_FREE(c) SAVESHAREDPV(CopSTASHPV(c))
206 # define SAVECOPFILE(c) SAVEPPTR(CopFILE(c))
207 # define SAVECOPFILE_FREE(c) SAVESHAREDPV(CopFILE(c))
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))
215 #define SAVECOPLINE(c) SAVEI32(CopLINE(c))
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
223 * realloc returns data aligned to a size divisible by "align"!
225 * SSPTR() converts the index returned by SSNEW/SSNEWa() into a pointer.
228 #define SSNEW(size) Perl_save_alloc(aTHX_ (size), 0)
229 #define SSNEWt(n,t) SSNEW((n)*sizeof(t))
230 #define SSNEWa(size,align) Perl_save_alloc(aTHX_ (size), \
231 (align - ((int)((caddr_t)&PL_savestack[PL_savestack_ix]) % align)) % align)
232 #define SSNEWat(n,t,align) SSNEWa((n)*sizeof(t), align)
234 #define SSPTR(off,type) ((type) ((char*)PL_savestack + off))
235 #define SSPTRt(off,type) ((type*) ((char*)PL_savestack + off))
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.
241 * Existence of a non-null top_env->je_prev implies it is valid to call
242 * longjmp() at that runlevel (we make sure start_env.je_prev is always
243 * null to ensure this).
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.
253 struct jmpenv * je_prev;
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()? */
257 #ifdef PERL_FLEXIBLE_EXCEPTIONS
258 void (*je_throw)(int v); /* last for bincompat */
259 bool je_noset; /* no need for setjmp() */
263 typedef struct jmpenv JMPENV;
265 #ifdef OP_IN_REGISTER
266 #define OP_REG_TO_MEM PL_opsave = op
267 #define OP_MEM_TO_REG op = PL_opsave
269 #define OP_REG_TO_MEM NOOP
270 #define OP_MEM_TO_REG NOOP
274 * How to build the first jmpenv.
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 {}!
283 #define JMPENV_BOOTSTRAP \
285 Zero(&PL_start_env, 1, JMPENV); \
286 PL_start_env.je_ret = -1; \
287 PL_start_env.je_mustcatch = TRUE; \
288 PL_top_env = &PL_start_env; \
291 #ifdef PERL_FLEXIBLE_EXCEPTIONS
294 * These exception-handling macros are split up to
295 * ease integration with C++ exceptions.
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; } }
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
309 * JMPENV_PUSH_INIT(thrower);
311 * ... stuff that may throw exceptions ...
313 * catch (int why) { // or whatever matches thrower()
317 * ... // handle various Perl exception codes
320 * JMPENV_POP; // don't forget this!
324 * Function that catches/throws, and its callback for the
325 * body of protected processing.
327 typedef void *(CPERLscope(*protect_body_t)) (pTHX_ va_list);
328 typedef void *(CPERLscope(*protect_proc_t)) (pTHX_ volatile JMPENV *pcur_env,
329 int *, protect_body_t, ...);
331 #define dJMPENV JMPENV cur_env; \
332 volatile JMPENV *pcur_env = ((cur_env.je_noset = 0),&cur_env)
334 #define JMPENV_PUSH_INIT_ENV(ce,THROWFUNC) \
336 (ce).je_throw = (THROWFUNC); \
338 (ce).je_mustcatch = FALSE; \
339 (ce).je_prev = PL_top_env; \
340 PL_top_env = &(ce); \
344 #define JMPENV_PUSH_INIT(THROWFUNC) JMPENV_PUSH_INIT_ENV(*(JMPENV*)pcur_env,THROWFUNC)
346 #define JMPENV_POST_CATCH_ENV(ce) \
349 PL_top_env = &(ce); \
352 #define JMPENV_POST_CATCH JMPENV_POST_CATCH_ENV(*(JMPENV*)pcur_env)
354 #define JMPENV_PUSH_ENV(ce,v) \
356 if (!(ce).je_noset) { \
357 DEBUG_l(Perl_deb(aTHX_ "Setting up jumplevel %p, was %p\n", \
358 (void*)ce, (void*)PL_top_env)); \
359 JMPENV_PUSH_INIT_ENV(ce,NULL); \
360 EXCEPT_SET_ENV(ce,PerlProc_setjmp((ce).je_buf, SCOPE_SAVES_SIGNAL_MASK));\
364 EXCEPT_SET_ENV(ce,0); \
365 JMPENV_POST_CATCH_ENV(ce); \
366 (v) = EXCEPT_GET_ENV(ce); \
369 #define JMPENV_PUSH(v) JMPENV_PUSH_ENV(*(JMPENV*)pcur_env,v)
371 #define JMPENV_POP_ENV(ce) \
373 if (PL_top_env == &(ce)) \
374 PL_top_env = (ce).je_prev; \
377 #define JMPENV_POP JMPENV_POP_ENV(*(JMPENV*)pcur_env)
379 #define JMPENV_JUMP(v) \
382 if (PL_top_env->je_prev) { \
383 if (PL_top_env->je_throw) \
384 PL_top_env->je_throw(v); \
386 PerlProc_longjmp(PL_top_env->je_buf, (v)); \
389 PerlProc_exit(STATUS_EXIT); \
390 PerlIO_printf(Perl_error_log, "panic: top_env\n"); \
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)
399 #else /* !PERL_FLEXIBLE_EXCEPTIONS */
401 #define dJMPENV JMPENV cur_env
403 #define JMPENV_PUSH(v) \
405 DEBUG_l(Perl_deb(aTHX_ "Setting up jumplevel %p, was %p\n", \
406 (void*)&cur_env, (void*)PL_top_env)); \
407 cur_env.je_prev = PL_top_env; \
409 cur_env.je_ret = PerlProc_setjmp(cur_env.je_buf, SCOPE_SAVES_SIGNAL_MASK); \
411 PL_top_env = &cur_env; \
412 cur_env.je_mustcatch = FALSE; \
413 (v) = cur_env.je_ret; \
417 STMT_START { PL_top_env = cur_env.je_prev; } STMT_END
419 #define JMPENV_JUMP(v) \
422 if (PL_top_env->je_prev) \
423 PerlProc_longjmp(PL_top_env->je_buf, (v)); \
425 PerlProc_exit(STATUS_EXIT); \
426 PerlIO_printf(PerlIO_stderr(), "panic: top_env\n"); \
430 #endif /* PERL_FLEXIBLE_EXCEPTIONS */
432 #define CATCH_GET (PL_top_env->je_mustcatch)
433 #define CATCH_SET(v) (PL_top_env->je_mustcatch = (v))