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
36 #define SAVEt_PADSV 35
38 #ifndef SCOPE_SAVES_SIGNAL_MASK
39 #define SCOPE_SAVES_SIGNAL_MASK 0
42 #define SSCHECK(need) if (PL_savestack_ix + need > PL_savestack_max) savestack_grow()
43 #define SSPUSHINT(i) (PL_savestack[PL_savestack_ix++].any_i32 = (I32)(i))
44 #define SSPUSHLONG(i) (PL_savestack[PL_savestack_ix++].any_long = (long)(i))
45 #define SSPUSHIV(i) (PL_savestack[PL_savestack_ix++].any_iv = (IV)(i))
46 #define SSPUSHPTR(p) (PL_savestack[PL_savestack_ix++].any_ptr = (void*)(p))
47 #define SSPUSHDPTR(p) (PL_savestack[PL_savestack_ix++].any_dptr = (p))
48 #define SSPUSHDXPTR(p) (PL_savestack[PL_savestack_ix++].any_dxptr = (p))
49 #define SSPOPINT (PL_savestack[--PL_savestack_ix].any_i32)
50 #define SSPOPLONG (PL_savestack[--PL_savestack_ix].any_long)
51 #define SSPOPIV (PL_savestack[--PL_savestack_ix].any_iv)
52 #define SSPOPPTR (PL_savestack[--PL_savestack_ix].any_ptr)
53 #define SSPOPDPTR (PL_savestack[--PL_savestack_ix].any_dptr)
54 #define SSPOPDXPTR (PL_savestack[--PL_savestack_ix].any_dxptr)
57 =for apidoc Ams||SAVETMPS
58 Opening bracket for temporaries on a callback. See C<FREETMPS> and
61 =for apidoc Ams||FREETMPS
62 Closing bracket for temporaries on a callback. See C<SAVETMPS> and
65 =for apidoc Ams||ENTER
66 Opening bracket on a callback. See C<LEAVE> and L<perlcall>.
68 =for apidoc Ams||LEAVE
69 Closing bracket on a callback. See C<ENTER> and L<perlcall>.
74 #define SAVETMPS save_int((int*)&PL_tmps_floor), PL_tmps_floor = PL_tmps_ix
75 #define FREETMPS if (PL_tmps_ix > PL_tmps_floor) free_tmps()
81 DEBUG_l(WITH_THR(Perl_deb(aTHX_ "ENTER scope %ld at %s:%d\n", \
82 PL_scopestack_ix, __FILE__, __LINE__))); \
86 DEBUG_l(WITH_THR(Perl_deb(aTHX_ "LEAVE scope %ld at %s:%d\n", \
87 PL_scopestack_ix, __FILE__, __LINE__))); \
91 #define ENTER push_scope()
92 #define LEAVE pop_scope()
94 #define LEAVE_SCOPE(old) if (PL_savestack_ix > old) leave_scope(old)
97 * Not using SOFT_CAST on SAVESPTR, SAVEGENERICSV and SAVEFREESV
98 * because these are used for several kinds of pointer values
100 #define SAVEI8(i) save_I8(SOFT_CAST(I8*)&(i))
101 #define SAVEI16(i) save_I16(SOFT_CAST(I16*)&(i))
102 #define SAVEI32(i) save_I32(SOFT_CAST(I32*)&(i))
103 #define SAVEINT(i) save_int(SOFT_CAST(int*)&(i))
104 #define SAVEIV(i) save_iv(SOFT_CAST(IV*)&(i))
105 #define SAVELONG(l) save_long(SOFT_CAST(long*)&(l))
106 #define SAVESPTR(s) save_sptr((SV**)&(s))
107 #define SAVEPPTR(s) save_pptr(SOFT_CAST(char**)&(s))
108 #define SAVEVPTR(s) save_vptr((void*)&(s))
109 #define SAVEPADSV(s) save_padsv(s)
110 #define SAVEFREESV(s) save_freesv((SV*)(s))
111 #define SAVEFREEOP(o) save_freeop(SOFT_CAST(OP*)(o))
112 #define SAVEFREEPV(p) save_freepv(SOFT_CAST(char*)(p))
113 #define SAVECLEARSV(sv) save_clearsv(SOFT_CAST(SV**)&(sv))
114 #define SAVEGENERICSV(s) save_generic_svref((SV**)&(s))
115 #define SAVEGENERICPV(s) save_generic_pvref((char**)&(s))
116 #define SAVEDELETE(h,k,l) \
117 save_delete(SOFT_CAST(HV*)(h), SOFT_CAST(char*)(k), (I32)(l))
118 #define SAVEDESTRUCTOR(f,p) \
119 save_destructor((DESTRUCTORFUNC_NOCONTEXT_t)(f), SOFT_CAST(void*)(p))
121 #define SAVEDESTRUCTOR_X(f,p) \
122 save_destructor_x((DESTRUCTORFUNC_t)(f), SOFT_CAST(void*)(p))
124 #define SAVESTACK_POS() \
127 SSPUSHINT(PL_stack_sp - PL_stack_base); \
128 SSPUSHINT(SAVEt_STACK_POS); \
131 #define SAVEOP() save_op()
133 #define SAVEHINTS() \
135 if (PL_hints & HINT_LOCALIZE_HH) \
139 SSPUSHINT(PL_hints); \
140 SSPUSHINT(SAVEt_HINTS); \
144 #define SAVECOMPPAD() \
146 if (PL_comppad && PL_curpad == AvARRAY(PL_comppad)) { \
148 SSPUSHPTR((SV*)PL_comppad); \
149 SSPUSHINT(SAVEt_COMPPAD); \
152 SAVEVPTR(PL_curpad); \
153 SAVESPTR(PL_comppad); \
158 # define SAVECOPSTASH(c) SAVEPPTR(CopSTASHPV(c))
159 # define SAVECOPSTASH_FREE(c) SAVEGENERICPV(CopSTASHPV(c))
160 # define SAVECOPFILE(c) SAVEPPTR(CopFILE(c))
161 # define SAVECOPFILE_FREE(c) SAVEGENERICPV(CopFILE(c))
163 # define SAVECOPSTASH(c) SAVESPTR(CopSTASH(c))
164 # define SAVECOPSTASH_FREE(c) SAVECOPSTASH(c) /* XXX not refcounted */
165 # define SAVECOPFILE(c) SAVESPTR(CopFILEGV(c))
166 # define SAVECOPFILE_FREE(c) SAVEGENERICSV(CopFILEGV(c))
169 #define SAVECOPLINE(c) SAVEI16(CopLINE(c))
171 /* SSNEW() temporarily allocates a specified number of bytes of data on the
172 * savestack. It returns an integer index into the savestack, because a
173 * pointer would get broken if the savestack is moved on reallocation.
174 * SSNEWa() works like SSNEW(), but also aligns the data to the specified
175 * number of bytes. MEM_ALIGNBYTES is perhaps the most useful. The
176 * alignment will be preserved therough savestack reallocation *only* if
177 * realloc returns data aligned to a size divisible by `align'!
179 * SSPTR() converts the index returned by SSNEW/SSNEWa() into a pointer.
182 #define SSNEW(size) Perl_save_alloc(aTHX_ (size), 0)
183 #define SSNEWt(n,t) SSNEW((n)*sizeof(t))
184 #define SSNEWa(size,align) Perl_save_alloc(aTHX_ (size), \
185 (align - ((int)((caddr_t)&PL_savestack[PL_savestack_ix]) % align)) % align)
186 #define SSNEWat(n,t,align) SSNEWa((n)*sizeof(t), align)
188 #define SSPTR(off,type) ((type) ((char*)PL_savestack + off))
189 #define SSPTRt(off,type) ((type*) ((char*)PL_savestack + off))
191 /* A jmpenv packages the state required to perform a proper non-local jump.
192 * Note that there is a start_env initialized when perl starts, and top_env
193 * points to this initially, so top_env should always be non-null.
195 * Existence of a non-null top_env->je_prev implies it is valid to call
196 * longjmp() at that runlevel (we make sure start_env.je_prev is always
197 * null to ensure this).
199 * je_mustcatch, when set at any runlevel to TRUE, means eval ops must
200 * establish a local jmpenv to handle exception traps. Care must be taken
201 * to restore the previous value of je_mustcatch before exiting the
202 * stack frame iff JMPENV_PUSH was not called in that stack frame.
207 struct jmpenv * je_prev;
208 Sigjmp_buf je_buf; /* only for use if !je_throw */
209 int je_ret; /* last exception thrown */
210 bool je_mustcatch; /* need to call longjmp()? */
211 #ifdef PERL_FLEXIBLE_EXCEPTIONS
212 void (*je_throw)(int v); /* last for bincompat */
213 bool je_noset; /* no need for setjmp() */
217 typedef struct jmpenv JMPENV;
219 #ifdef OP_IN_REGISTER
220 #define OP_REG_TO_MEM PL_opsave = op
221 #define OP_MEM_TO_REG op = PL_opsave
223 #define OP_REG_TO_MEM NOOP
224 #define OP_MEM_TO_REG NOOP
228 * How to build the first jmpenv.
230 * top_env needs to be non-zero. It points to an area
231 * in which longjmp() stuff is stored, as C callstack
232 * info there at least is thread specific this has to
233 * be per-thread. Otherwise a 'die' in a thread gives
234 * that thread the C stack of last thread to do an eval {}!
237 #define JMPENV_BOOTSTRAP \
239 Zero(&PL_start_env, 1, JMPENV); \
240 PL_start_env.je_ret = -1; \
241 PL_start_env.je_mustcatch = TRUE; \
242 PL_top_env = &PL_start_env; \
245 #ifdef PERL_FLEXIBLE_EXCEPTIONS
248 * These exception-handling macros are split up to
249 * ease integration with C++ exceptions.
251 * To use C++ try+catch to catch Perl exceptions, an extension author
252 * needs to first write an extern "C" function to throw an appropriate
253 * exception object; typically it will be or contain an integer,
254 * because Perl's internals use integers to track exception types:
255 * extern "C" { static void thrower(int i) { throw i; } }
257 * Then (as shown below) the author needs to use, not the simple
258 * JMPENV_PUSH, but several of its constitutent macros, to arrange for
259 * the Perl internals to call thrower() rather than longjmp() to
263 * JMPENV_PUSH_INIT(thrower);
265 * ... stuff that may throw exceptions ...
267 * catch (int why) { // or whatever matches thrower()
271 * ... // handle various Perl exception codes
274 * JMPENV_POP; // don't forget this!
278 * Function that catches/throws, and its callback for the
279 * body of protected processing.
281 typedef void *(CPERLscope(*protect_body_t)) (pTHX_ va_list);
282 typedef void *(CPERLscope(*protect_proc_t)) (pTHX_ volatile JMPENV *pcur_env,
283 int *, protect_body_t, ...);
285 #define dJMPENV JMPENV cur_env; \
286 volatile JMPENV *pcur_env = ((cur_env.je_noset = 0),&cur_env)
288 #define JMPENV_PUSH_INIT_ENV(ce,THROWFUNC) \
290 (ce).je_throw = (THROWFUNC); \
292 (ce).je_mustcatch = FALSE; \
293 (ce).je_prev = PL_top_env; \
294 PL_top_env = &(ce); \
298 #define JMPENV_PUSH_INIT(THROWFUNC) JMPENV_PUSH_INIT_ENV(*(JMPENV*)pcur_env,THROWFUNC)
300 #define JMPENV_POST_CATCH_ENV(ce) \
303 PL_top_env = &(ce); \
306 #define JMPENV_POST_CATCH JMPENV_POST_CATCH_ENV(*(JMPENV*)pcur_env)
308 #define JMPENV_PUSH_ENV(ce,v) \
310 if (!(ce).je_noset) { \
311 DEBUG_l(Perl_deb(aTHX_ "Setting up jumplevel %p, was %p\n", \
313 JMPENV_PUSH_INIT_ENV(ce,NULL); \
314 EXCEPT_SET_ENV(ce,PerlProc_setjmp((ce).je_buf, SCOPE_SAVES_SIGNAL_MASK));\
318 EXCEPT_SET_ENV(ce,0); \
319 JMPENV_POST_CATCH_ENV(ce); \
320 (v) = EXCEPT_GET_ENV(ce); \
323 #define JMPENV_PUSH(v) JMPENV_PUSH_ENV(*(JMPENV*)pcur_env,v)
325 #define JMPENV_POP_ENV(ce) \
327 if (PL_top_env == &(ce)) \
328 PL_top_env = (ce).je_prev; \
331 #define JMPENV_POP JMPENV_POP_ENV(*(JMPENV*)pcur_env)
333 #define JMPENV_JUMP(v) \
336 if (PL_top_env->je_prev) { \
337 if (PL_top_env->je_throw) \
338 PL_top_env->je_throw(v); \
340 PerlProc_longjmp(PL_top_env->je_buf, (v)); \
343 PerlProc_exit(STATUS_NATIVE_EXPORT); \
344 PerlIO_printf(Perl_error_log, "panic: top_env\n"); \
348 #define EXCEPT_GET_ENV(ce) ((ce).je_ret)
349 #define EXCEPT_GET EXCEPT_GET_ENV(*(JMPENV*)pcur_env)
350 #define EXCEPT_SET_ENV(ce,v) ((ce).je_ret = (v))
351 #define EXCEPT_SET(v) EXCEPT_SET_ENV(*(JMPENV*)pcur_env,v)
353 #else /* !PERL_FLEXIBLE_EXCEPTIONS */
355 #define dJMPENV JMPENV cur_env
357 #define JMPENV_PUSH(v) \
359 DEBUG_l(Perl_deb(aTHX_ "Setting up jumplevel %p, was %p\n", \
360 &cur_env, PL_top_env)); \
361 cur_env.je_prev = PL_top_env; \
363 cur_env.je_ret = PerlProc_setjmp(cur_env.je_buf, SCOPE_SAVES_SIGNAL_MASK); \
365 PL_top_env = &cur_env; \
366 cur_env.je_mustcatch = FALSE; \
367 (v) = cur_env.je_ret; \
371 STMT_START { PL_top_env = cur_env.je_prev; } STMT_END
373 #define JMPENV_JUMP(v) \
376 if (PL_top_env->je_prev) \
377 PerlProc_longjmp(PL_top_env->je_buf, (v)); \
379 PerlProc_exit(STATUS_NATIVE_EXPORT); \
380 PerlIO_printf(PerlIO_stderr(), "panic: top_env\n"); \
384 #endif /* PERL_FLEXIBLE_EXCEPTIONS */
386 #define CATCH_GET (PL_top_env->je_mustcatch)
387 #define CATCH_SET(v) (PL_top_env->je_mustcatch = (v))