13 #define SAVEt_NSTAB 12
14 #define SAVEt_SVREF 13
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
25 #define SAVEt_AELEM 24
26 #define SAVEt_HELEM 25
28 #define SAVEt_HINTS 27
29 #define SAVEt_ALLOC 28
30 #define SAVEt_GENERIC_SVREF 29
31 #define SAVEt_DESTRUCTOR_X 30
34 #define SAVEt_COMPPAD 33
35 #define SAVEt_GENERIC_PVREF 34
37 #define SSCHECK(need) if (PL_savestack_ix + need > PL_savestack_max) savestack_grow()
38 #define SSPUSHINT(i) (PL_savestack[PL_savestack_ix++].any_i32 = (I32)(i))
39 #define SSPUSHLONG(i) (PL_savestack[PL_savestack_ix++].any_long = (long)(i))
40 #define SSPUSHIV(i) (PL_savestack[PL_savestack_ix++].any_iv = (IV)(i))
41 #define SSPUSHPTR(p) (PL_savestack[PL_savestack_ix++].any_ptr = (void*)(p))
42 #define SSPUSHDPTR(p) (PL_savestack[PL_savestack_ix++].any_dptr = (p))
43 #define SSPUSHDXPTR(p) (PL_savestack[PL_savestack_ix++].any_dxptr = (p))
44 #define SSPOPINT (PL_savestack[--PL_savestack_ix].any_i32)
45 #define SSPOPLONG (PL_savestack[--PL_savestack_ix].any_long)
46 #define SSPOPIV (PL_savestack[--PL_savestack_ix].any_iv)
47 #define SSPOPPTR (PL_savestack[--PL_savestack_ix].any_ptr)
48 #define SSPOPDPTR (PL_savestack[--PL_savestack_ix].any_dptr)
49 #define SSPOPDXPTR (PL_savestack[--PL_savestack_ix].any_dxptr)
52 =for apidoc Ams||SAVETMPS
53 Opening bracket for temporaries on a callback. See C<FREETMPS> and
56 =for apidoc Ams||FREETMPS
57 Closing bracket for temporaries on a callback. See C<SAVETMPS> and
60 =for apidoc Ams||ENTER
61 Opening bracket on a callback. See C<LEAVE> and L<perlcall>.
63 =for apidoc Ams||LEAVE
64 Closing bracket on a callback. See C<ENTER> and L<perlcall>.
69 #define SAVETMPS save_int((int*)&PL_tmps_floor), PL_tmps_floor = PL_tmps_ix
70 #define FREETMPS if (PL_tmps_ix > PL_tmps_floor) free_tmps()
76 DEBUG_l(WITH_THR(Perl_deb(aTHX_ "ENTER scope %ld at %s:%d\n", \
77 PL_scopestack_ix, __FILE__, __LINE__))); \
81 DEBUG_l(WITH_THR(Perl_deb(aTHX_ "LEAVE scope %ld at %s:%d\n", \
82 PL_scopestack_ix, __FILE__, __LINE__))); \
86 #define ENTER push_scope()
87 #define LEAVE pop_scope()
89 #define LEAVE_SCOPE(old) if (PL_savestack_ix > old) leave_scope(old)
92 * Not using SOFT_CAST on SAVESPTR, SAVEGENERICSV and SAVEFREESV
93 * because these are used for several kinds of pointer values
95 #define SAVEI8(i) save_I8(SOFT_CAST(I8*)&(i))
96 #define SAVEI16(i) save_I16(SOFT_CAST(I16*)&(i))
97 #define SAVEI32(i) save_I32(SOFT_CAST(I32*)&(i))
98 #define SAVEINT(i) save_int(SOFT_CAST(int*)&(i))
99 #define SAVEIV(i) save_iv(SOFT_CAST(IV*)&(i))
100 #define SAVELONG(l) save_long(SOFT_CAST(long*)&(l))
101 #define SAVESPTR(s) save_sptr((SV**)&(s))
102 #define SAVEPPTR(s) save_pptr(SOFT_CAST(char**)&(s))
103 #define SAVEVPTR(s) save_vptr((void*)&(s))
104 #define SAVEFREESV(s) save_freesv((SV*)(s))
105 #define SAVEFREEOP(o) save_freeop(SOFT_CAST(OP*)(o))
106 #define SAVEFREEPV(p) save_freepv(SOFT_CAST(char*)(p))
107 #define SAVECLEARSV(sv) save_clearsv(SOFT_CAST(SV**)&(sv))
108 #define SAVEGENERICSV(s) save_generic_svref((SV**)&(s))
109 #define SAVEGENERICPV(s) save_generic_pvref((char**)&(s))
110 #define SAVEDELETE(h,k,l) \
111 save_delete(SOFT_CAST(HV*)(h), SOFT_CAST(char*)(k), (I32)(l))
112 #define SAVEDESTRUCTOR(f,p) \
113 save_destructor((DESTRUCTORFUNC_NOCONTEXT_t)(f), SOFT_CAST(void*)(p))
115 #define SAVEDESTRUCTOR_X(f,p) \
116 save_destructor_x((DESTRUCTORFUNC_t)(f), SOFT_CAST(void*)(p))
118 #define SAVESTACK_POS() \
121 SSPUSHINT(PL_stack_sp - PL_stack_base); \
122 SSPUSHINT(SAVEt_STACK_POS); \
125 #define SAVEOP() save_op()
127 #define SAVEHINTS() \
129 if (PL_hints & HINT_LOCALIZE_HH) \
133 SSPUSHINT(PL_hints); \
134 SSPUSHINT(SAVEt_HINTS); \
138 #define SAVECOMPPAD() \
140 if (PL_comppad && PL_curpad == AvARRAY(PL_comppad)) { \
142 SSPUSHPTR((SV*)PL_comppad); \
143 SSPUSHINT(SAVEt_COMPPAD); \
146 SAVEVPTR(PL_curpad); \
147 SAVESPTR(PL_comppad); \
152 # define SAVECOPSTASH(c) SAVEPPTR(CopSTASHPV(c))
153 # define SAVECOPSTASH_FREE(c) SAVEGENERICPV(CopSTASHPV(c))
154 # define SAVECOPFILE(c) SAVEPPTR(CopFILE(c))
155 # define SAVECOPFILE_FREE(c) SAVEGENERICPV(CopFILE(c))
157 # define SAVECOPSTASH(c) SAVESPTR(CopSTASH(c))
158 # define SAVECOPSTASH_FREE(c) SAVECOPSTASH(c) /* XXX not refcounted */
159 # define SAVECOPFILE(c) SAVESPTR(CopFILEGV(c))
160 # define SAVECOPFILE_FREE(c) SAVEGENERICSV(CopFILEGV(c))
163 #define SAVECOPLINE(c) SAVEI16(CopLINE(c))
165 /* SSNEW() temporarily allocates a specified number of bytes of data on the
166 * savestack. It returns an integer index into the savestack, because a
167 * pointer would get broken if the savestack is moved on reallocation.
168 * SSNEWa() works like SSNEW(), but also aligns the data to the specified
169 * number of bytes. MEM_ALIGNBYTES is perhaps the most useful. The
170 * alignment will be preserved therough savestack reallocation *only* if
171 * realloc returns data aligned to a size divisible by `align'!
173 * SSPTR() converts the index returned by SSNEW/SSNEWa() into a pointer.
176 #define SSNEW(size) save_alloc(size, 0)
177 #define SSNEWa(size,align) save_alloc(size, \
178 (align - ((int)((caddr_t)&PL_savestack[PL_savestack_ix]) % align)) % align)
180 #define SSPTR(off,type) ((type) ((char*)PL_savestack + off))
182 /* A jmpenv packages the state required to perform a proper non-local jump.
183 * Note that there is a start_env initialized when perl starts, and top_env
184 * points to this initially, so top_env should always be non-null.
186 * Existence of a non-null top_env->je_prev implies it is valid to call
187 * longjmp() at that runlevel (we make sure start_env.je_prev is always
188 * null to ensure this).
190 * je_mustcatch, when set at any runlevel to TRUE, means eval ops must
191 * establish a local jmpenv to handle exception traps. Care must be taken
192 * to restore the previous value of je_mustcatch before exiting the
193 * stack frame iff JMPENV_PUSH was not called in that stack frame.
198 struct jmpenv * je_prev;
199 Sigjmp_buf je_buf; /* only for use if !je_throw */
200 int je_ret; /* last exception thrown */
201 bool je_mustcatch; /* need to call longjmp()? */
202 #ifdef PERL_FLEXIBLE_EXCEPTIONS
203 void (*je_throw)(int v); /* last for bincompat */
204 bool je_noset; /* no need for setjmp() */
208 typedef struct jmpenv JMPENV;
210 #ifdef OP_IN_REGISTER
211 #define OP_REG_TO_MEM PL_opsave = op
212 #define OP_MEM_TO_REG op = PL_opsave
214 #define OP_REG_TO_MEM NOOP
215 #define OP_MEM_TO_REG NOOP
219 * How to build the first jmpenv.
221 * top_env needs to be non-zero. It points to an area
222 * in which longjmp() stuff is stored, as C callstack
223 * info there at least is thread specific this has to
224 * be per-thread. Otherwise a 'die' in a thread gives
225 * that thread the C stack of last thread to do an eval {}!
228 #define JMPENV_BOOTSTRAP \
230 Zero(&PL_start_env, 1, JMPENV); \
231 PL_start_env.je_ret = -1; \
232 PL_start_env.je_mustcatch = TRUE; \
233 PL_top_env = &PL_start_env; \
236 #ifdef PERL_FLEXIBLE_EXCEPTIONS
239 * These exception-handling macros are split up to
240 * ease integration with C++ exceptions.
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; } }
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
254 * JMPENV_PUSH_INIT(thrower);
256 * ... stuff that may throw exceptions ...
258 * catch (int why) { // or whatever matches thrower()
262 * ... // handle various Perl exception codes
265 * JMPENV_POP; // don't forget this!
269 * Function that catches/throws, and its callback for the
270 * body of protected processing.
272 typedef void *(CPERLscope(*protect_body_t)) (pTHX_ va_list);
273 typedef void *(CPERLscope(*protect_proc_t)) (pTHX_ volatile JMPENV *pcur_env,
274 int *, protect_body_t, ...);
276 #define dJMPENV JMPENV cur_env; \
277 volatile JMPENV *pcur_env = ((cur_env.je_noset = 0),&cur_env)
279 #define JMPENV_PUSH_INIT_ENV(ce,THROWFUNC) \
281 (ce).je_throw = (THROWFUNC); \
283 (ce).je_mustcatch = FALSE; \
284 (ce).je_prev = PL_top_env; \
285 PL_top_env = &(ce); \
289 #define JMPENV_PUSH_INIT(THROWFUNC) JMPENV_PUSH_INIT_ENV(*(JMPENV*)pcur_env,THROWFUNC)
291 #define JMPENV_POST_CATCH_ENV(ce) \
294 PL_top_env = &(ce); \
297 #define JMPENV_POST_CATCH JMPENV_POST_CATCH_ENV(*(JMPENV*)pcur_env)
299 #define JMPENV_PUSH_ENV(ce,v) \
301 if (!(ce).je_noset) { \
302 DEBUG_l(Perl_deb(aTHX_ "Setting up jumplevel %p, was %p\n", \
304 JMPENV_PUSH_INIT_ENV(ce,NULL); \
305 EXCEPT_SET_ENV(ce,PerlProc_setjmp((ce).je_buf, 1));\
309 EXCEPT_SET_ENV(ce,0); \
310 JMPENV_POST_CATCH_ENV(ce); \
311 (v) = EXCEPT_GET_ENV(ce); \
314 #define JMPENV_PUSH(v) JMPENV_PUSH_ENV(*(JMPENV*)pcur_env,v)
316 #define JMPENV_POP_ENV(ce) \
318 if (PL_top_env == &(ce)) \
319 PL_top_env = (ce).je_prev; \
322 #define JMPENV_POP JMPENV_POP_ENV(*(JMPENV*)pcur_env)
324 #define JMPENV_JUMP(v) \
327 if (PL_top_env->je_prev) { \
328 if (PL_top_env->je_throw) \
329 PL_top_env->je_throw(v); \
331 PerlProc_longjmp(PL_top_env->je_buf, (v)); \
334 PerlProc_exit(STATUS_NATIVE_EXPORT); \
335 PerlIO_printf(Perl_error_log, "panic: top_env\n"); \
339 #define EXCEPT_GET_ENV(ce) ((ce).je_ret)
340 #define EXCEPT_GET EXCEPT_GET_ENV(*(JMPENV*)pcur_env)
341 #define EXCEPT_SET_ENV(ce,v) ((ce).je_ret = (v))
342 #define EXCEPT_SET(v) EXCEPT_SET_ENV(*(JMPENV*)pcur_env,v)
344 #else /* !PERL_FLEXIBLE_EXCEPTIONS */
346 #define dJMPENV JMPENV cur_env
348 #define JMPENV_PUSH(v) \
350 DEBUG_l(Perl_deb(aTHX_ "Setting up jumplevel %p, was %p\n", \
351 &cur_env, PL_top_env)); \
352 cur_env.je_prev = PL_top_env; \
354 cur_env.je_ret = PerlProc_setjmp(cur_env.je_buf, 1); \
356 PL_top_env = &cur_env; \
357 cur_env.je_mustcatch = FALSE; \
358 (v) = cur_env.je_ret; \
362 STMT_START { PL_top_env = cur_env.je_prev; } STMT_END
364 #define JMPENV_JUMP(v) \
367 if (PL_top_env->je_prev) \
368 PerlProc_longjmp(PL_top_env->je_buf, (v)); \
370 PerlProc_exit(STATUS_NATIVE_EXPORT); \
371 PerlIO_printf(PerlIO_stderr(), "panic: top_env\n"); \
375 #endif /* PERL_FLEXIBLE_EXCEPTIONS */
377 #define CATCH_GET (PL_top_env->je_mustcatch)
378 #define CATCH_SET(v) (PL_top_env->je_mustcatch = (v))