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
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))
42 #define SSPUSHDXPTR(p) (PL_savestack[PL_savestack_ix++].any_dxptr = (p))
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)
48 #define SSPOPDXPTR (PL_savestack[--PL_savestack_ix].any_dxptr)
51 =for apidoc Ams||SAVETMPS
52 Opening bracket for temporaries on a callback. See C<FREETMPS> and
55 =for apidoc Ams||FREETMPS
56 Closing bracket for temporaries on a callback. See C<SAVETMPS> and
59 =for apidoc Ams||ENTER
60 Opening bracket on a callback. See C<LEAVE> and L<perlcall>.
62 =for apidoc Ams||LEAVE
63 Closing bracket on a callback. See C<ENTER> and L<perlcall>.
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()
75 DEBUG_l(WITH_THR(Perl_deb(aTHX_ "ENTER scope %ld at %s:%d\n", \
76 PL_scopestack_ix, __FILE__, __LINE__))); \
80 DEBUG_l(WITH_THR(Perl_deb(aTHX_ "LEAVE scope %ld at %s:%d\n", \
81 PL_scopestack_ix, __FILE__, __LINE__))); \
85 #define ENTER push_scope()
86 #define LEAVE pop_scope()
88 #define LEAVE_SCOPE(old) if (PL_savestack_ix > old) leave_scope(old)
91 * Not using SOFT_CAST on SAVESPTR, SAVEGENERICSV and SAVEFREESV
92 * because these are used for several kinds of pointer values
94 #define SAVEI8(i) save_I8(SOFT_CAST(I8*)&(i))
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))
100 #define SAVESPTR(s) save_sptr((SV**)&(s))
101 #define SAVEPPTR(s) save_pptr(SOFT_CAST(char**)&(s))
102 #define SAVEVPTR(s) save_vptr((void*)&(s))
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))
107 #define SAVEGENERICSV(s) save_generic_svref((SV**)&(s))
108 #define SAVEDELETE(h,k,l) \
109 save_delete(SOFT_CAST(HV*)(h), SOFT_CAST(char*)(k), (I32)(l))
110 #define SAVEDESTRUCTOR(f,p) \
111 save_destructor((DESTRUCTORFUNC_NOCONTEXT_t)(f), SOFT_CAST(void*)(p))
113 #define SAVEDESTRUCTOR_X(f,p) \
114 save_destructor_x((DESTRUCTORFUNC_t)(f), SOFT_CAST(void*)(p))
116 #define SAVESTACK_POS() \
119 SSPUSHINT(PL_stack_sp - PL_stack_base); \
120 SSPUSHINT(SAVEt_STACK_POS); \
123 #define SAVEOP() save_op()
125 #define SAVEHINTS() \
127 if (PL_hints & HINT_LOCALIZE_HH) \
131 SSPUSHINT(PL_hints); \
132 SSPUSHINT(SAVEt_HINTS); \
136 #define SAVECOMPPAD() \
138 if (PL_comppad && PL_curpad == AvARRAY(PL_comppad)) { \
140 SSPUSHPTR((SV*)PL_comppad); \
141 SSPUSHINT(SAVEt_COMPPAD); \
144 SAVEVPTR(PL_curpad); \
145 SAVESPTR(PL_comppad); \
150 # define SAVECOPSTASH(cop) SAVEPPTR(CopSTASHPV(cop))
151 # define SAVECOPFILE(cop) SAVEPPTR(CopFILE(cop))
153 # define SAVECOPSTASH(cop) SAVESPTR(CopSTASH(cop))
154 # define SAVECOPFILE(cop) SAVESPTR(CopFILEGV(cop))
157 #define SAVECOPLINE(cop) SAVEI16(CopLINE(cop))
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'!
167 * SSPTR() converts the index returned by SSNEW/SSNEWa() into a pointer.
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)
174 #define SSPTR(off,type) ((type) ((char*)PL_savestack + off))
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.
180 * Existence of a non-null top_env->je_prev implies it is valid to call
181 * longjmp() at that runlevel (we make sure start_env.je_prev is always
182 * null to ensure this).
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.
192 struct jmpenv * je_prev;
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 #ifdef PERL_FLEXIBLE_EXCEPTIONS
197 void (*je_throw)(int v); /* last for bincompat */
198 bool je_noset; /* no need for setjmp() */
202 typedef struct jmpenv JMPENV;
204 #ifdef OP_IN_REGISTER
205 #define OP_REG_TO_MEM PL_opsave = op
206 #define OP_MEM_TO_REG op = PL_opsave
208 #define OP_REG_TO_MEM NOOP
209 #define OP_MEM_TO_REG NOOP
213 * How to build the first jmpenv.
215 * top_env needs to be non-zero. It points to an area
216 * in which longjmp() stuff is stored, as C callstack
217 * info there at least is thread specific this has to
218 * be per-thread. Otherwise a 'die' in a thread gives
219 * that thread the C stack of last thread to do an eval {}!
222 #define JMPENV_BOOTSTRAP \
224 Zero(&PL_start_env, 1, JMPENV); \
225 PL_start_env.je_ret = -1; \
226 PL_start_env.je_mustcatch = TRUE; \
227 PL_top_env = &PL_start_env; \
230 #ifdef PERL_FLEXIBLE_EXCEPTIONS
233 * These exception-handling macros are split up to
234 * ease integration with C++ exceptions.
236 * To use C++ try+catch to catch Perl exceptions, an extension author
237 * needs to first write an extern "C" function to throw an appropriate
238 * exception object; typically it will be or contain an integer,
239 * because Perl's internals use integers to track exception types:
240 * extern "C" { static void thrower(int i) { throw i; } }
242 * Then (as shown below) the author needs to use, not the simple
243 * JMPENV_PUSH, but several of its constitutent macros, to arrange for
244 * the Perl internals to call thrower() rather than longjmp() to
248 * JMPENV_PUSH_INIT(thrower);
250 * ... stuff that may throw exceptions ...
252 * catch (int why) { // or whatever matches thrower()
256 * ... // handle various Perl exception codes
259 * JMPENV_POP; // don't forget this!
263 * Function that catches/throws, and its callback for the
264 * body of protected processing.
266 typedef void *(CPERLscope(*protect_body_t)) (pTHX_ va_list);
267 typedef void *(CPERLscope(*protect_proc_t)) (pTHX_ volatile JMPENV *pcur_env,
268 int *, protect_body_t, ...);
270 #define dJMPENV JMPENV cur_env; \
271 volatile JMPENV *pcur_env = ((cur_env.je_noset = 0),&cur_env)
273 #define JMPENV_PUSH_INIT_ENV(ce,THROWFUNC) \
275 (ce).je_throw = (THROWFUNC); \
277 (ce).je_mustcatch = FALSE; \
278 (ce).je_prev = PL_top_env; \
279 PL_top_env = &(ce); \
283 #define JMPENV_PUSH_INIT(THROWFUNC) JMPENV_PUSH_INIT_ENV(*(JMPENV*)pcur_env,THROWFUNC)
285 #define JMPENV_POST_CATCH_ENV(ce) \
288 PL_top_env = &(ce); \
291 #define JMPENV_POST_CATCH JMPENV_POST_CATCH_ENV(*(JMPENV*)pcur_env)
293 #define JMPENV_PUSH_ENV(ce,v) \
295 if (!(ce).je_noset) { \
296 DEBUG_l(Perl_deb(aTHX_ "Setting up jumplevel %p, was %p\n", \
298 JMPENV_PUSH_INIT_ENV(ce,NULL); \
299 EXCEPT_SET_ENV(ce,PerlProc_setjmp((ce).je_buf, 1));\
303 EXCEPT_SET_ENV(ce,0); \
304 JMPENV_POST_CATCH_ENV(ce); \
305 (v) = EXCEPT_GET_ENV(ce); \
308 #define JMPENV_PUSH(v) JMPENV_PUSH_ENV(*(JMPENV*)pcur_env,v)
310 #define JMPENV_POP_ENV(ce) \
312 if (PL_top_env == &(ce)) \
313 PL_top_env = (ce).je_prev; \
316 #define JMPENV_POP JMPENV_POP_ENV(*(JMPENV*)pcur_env)
318 #define JMPENV_JUMP(v) \
321 if (PL_top_env->je_prev) { \
322 if (PL_top_env->je_throw) \
323 PL_top_env->je_throw(v); \
325 PerlProc_longjmp(PL_top_env->je_buf, (v)); \
328 PerlProc_exit(STATUS_NATIVE_EXPORT); \
329 PerlIO_printf(Perl_error_log, "panic: top_env\n"); \
333 #define EXCEPT_GET_ENV(ce) ((ce).je_ret)
334 #define EXCEPT_GET EXCEPT_GET_ENV(*(JMPENV*)pcur_env)
335 #define EXCEPT_SET_ENV(ce,v) ((ce).je_ret = (v))
336 #define EXCEPT_SET(v) EXCEPT_SET_ENV(*(JMPENV*)pcur_env,v)
338 #else /* !PERL_FLEXIBLE_EXCEPTIONS */
340 #define dJMPENV JMPENV cur_env
342 #define JMPENV_PUSH(v) \
344 DEBUG_l(Perl_deb(aTHX_ "Setting up jumplevel %p, was %p\n", \
345 &cur_env, PL_top_env)); \
346 cur_env.je_prev = PL_top_env; \
348 cur_env.je_ret = PerlProc_setjmp(cur_env.je_buf, 1); \
350 PL_top_env = &cur_env; \
351 cur_env.je_mustcatch = FALSE; \
352 (v) = cur_env.je_ret; \
356 STMT_START { PL_top_env = cur_env.je_prev; } STMT_END
358 #define JMPENV_JUMP(v) \
361 if (PL_top_env->je_prev) \
362 PerlProc_longjmp(PL_top_env->je_buf, (v)); \
364 PerlProc_exit(STATUS_NATIVE_EXPORT); \
365 PerlIO_printf(PerlIO_stderr(), "panic: top_env\n"); \
369 #endif /* PERL_FLEXIBLE_EXCEPTIONS */
371 #define CATCH_GET (PL_top_env->je_mustcatch)
372 #define CATCH_SET(v) (PL_top_env->je_mustcatch = (v))