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
37 #define SAVEt_MORTALIZESV 36
39 #define SSCHECK(need) if (PL_savestack_ix + need > PL_savestack_max) savestack_grow()
40 #define SSPUSHINT(i) (PL_savestack[PL_savestack_ix++].any_i32 = (I32)(i))
41 #define SSPUSHLONG(i) (PL_savestack[PL_savestack_ix++].any_long = (long)(i))
42 #define SSPUSHIV(i) (PL_savestack[PL_savestack_ix++].any_iv = (IV)(i))
43 #define SSPUSHPTR(p) (PL_savestack[PL_savestack_ix++].any_ptr = (void*)(p))
44 #define SSPUSHDPTR(p) (PL_savestack[PL_savestack_ix++].any_dptr = (p))
45 #define SSPUSHDXPTR(p) (PL_savestack[PL_savestack_ix++].any_dxptr = (p))
46 #define SSPOPINT (PL_savestack[--PL_savestack_ix].any_i32)
47 #define SSPOPLONG (PL_savestack[--PL_savestack_ix].any_long)
48 #define SSPOPIV (PL_savestack[--PL_savestack_ix].any_iv)
49 #define SSPOPPTR (PL_savestack[--PL_savestack_ix].any_ptr)
50 #define SSPOPDPTR (PL_savestack[--PL_savestack_ix].any_dptr)
51 #define SSPOPDXPTR (PL_savestack[--PL_savestack_ix].any_dxptr)
54 =for apidoc Ams||SAVETMPS
55 Opening bracket for temporaries on a callback. See C<FREETMPS> and
58 =for apidoc Ams||FREETMPS
59 Closing bracket for temporaries on a callback. See C<SAVETMPS> and
62 =for apidoc Ams||ENTER
63 Opening bracket on a callback. See C<LEAVE> and L<perlcall>.
65 =for apidoc Ams||LEAVE
66 Closing bracket on a callback. See C<ENTER> and L<perlcall>.
71 #define SAVETMPS save_int((int*)&PL_tmps_floor), PL_tmps_floor = PL_tmps_ix
72 #define FREETMPS if (PL_tmps_ix > PL_tmps_floor) free_tmps()
78 DEBUG_l(WITH_THR(Perl_deb(aTHX_ "ENTER scope %ld at %s:%d\n", \
79 PL_scopestack_ix, __FILE__, __LINE__))); \
83 DEBUG_l(WITH_THR(Perl_deb(aTHX_ "LEAVE scope %ld at %s:%d\n", \
84 PL_scopestack_ix, __FILE__, __LINE__))); \
88 #define ENTER push_scope()
89 #define LEAVE pop_scope()
91 #define LEAVE_SCOPE(old) if (PL_savestack_ix > old) leave_scope(old)
94 * Not using SOFT_CAST on SAVESPTR, SAVEGENERICSV and SAVEFREESV
95 * because these are used for several kinds of pointer values
97 #define SAVEI8(i) save_I8(SOFT_CAST(I8*)&(i))
98 #define SAVEI16(i) save_I16(SOFT_CAST(I16*)&(i))
99 #define SAVEI32(i) save_I32(SOFT_CAST(I32*)&(i))
100 #define SAVEINT(i) save_int(SOFT_CAST(int*)&(i))
101 #define SAVEIV(i) save_iv(SOFT_CAST(IV*)&(i))
102 #define SAVELONG(l) save_long(SOFT_CAST(long*)&(l))
103 #define SAVESPTR(s) save_sptr((SV**)&(s))
104 #define SAVEPPTR(s) save_pptr(SOFT_CAST(char**)&(s))
105 #define SAVEVPTR(s) save_vptr((void*)&(s))
106 #define SAVEPADSV(s) save_padsv(s)
107 #define SAVEFREESV(s) save_freesv((SV*)(s))
108 #define SAVEMORTALIZESV(s) save_mortalizesv((SV*)(s))
109 #define SAVEFREEOP(o) save_freeop(SOFT_CAST(OP*)(o))
110 #define SAVEFREEPV(p) save_freepv(SOFT_CAST(char*)(p))
111 #define SAVECLEARSV(sv) save_clearsv(SOFT_CAST(SV**)&(sv))
112 #define SAVEGENERICSV(s) save_generic_svref((SV**)&(s))
113 #define SAVEGENERICPV(s) save_generic_pvref((char**)&(s))
114 #define SAVEDELETE(h,k,l) \
115 save_delete(SOFT_CAST(HV*)(h), SOFT_CAST(char*)(k), (I32)(l))
116 #define SAVEDESTRUCTOR(f,p) \
117 save_destructor((DESTRUCTORFUNC_NOCONTEXT_t)(f), SOFT_CAST(void*)(p))
119 #define SAVEDESTRUCTOR_X(f,p) \
120 save_destructor_x((DESTRUCTORFUNC_t)(f), SOFT_CAST(void*)(p))
122 #define SAVESTACK_POS() \
125 SSPUSHINT(PL_stack_sp - PL_stack_base); \
126 SSPUSHINT(SAVEt_STACK_POS); \
129 #define SAVEOP() save_op()
131 #define SAVEHINTS() \
133 if (PL_hints & HINT_LOCALIZE_HH) \
137 SSPUSHINT(PL_hints); \
138 SSPUSHINT(SAVEt_HINTS); \
142 #define SAVECOMPPAD() \
144 if (PL_comppad && PL_curpad == AvARRAY(PL_comppad)) { \
146 SSPUSHPTR((SV*)PL_comppad); \
147 SSPUSHINT(SAVEt_COMPPAD); \
150 SAVEVPTR(PL_curpad); \
151 SAVESPTR(PL_comppad); \
156 # define SAVECOPSTASH(c) SAVEPPTR(CopSTASHPV(c))
157 # define SAVECOPSTASH_FREE(c) SAVEGENERICPV(CopSTASHPV(c))
158 # define SAVECOPFILE(c) SAVEPPTR(CopFILE(c))
159 # define SAVECOPFILE_FREE(c) SAVEGENERICPV(CopFILE(c))
161 # define SAVECOPSTASH(c) SAVESPTR(CopSTASH(c))
162 # define SAVECOPSTASH_FREE(c) SAVECOPSTASH(c) /* XXX not refcounted */
163 # define SAVECOPFILE(c) SAVESPTR(CopFILEGV(c))
164 # define SAVECOPFILE_FREE(c) SAVEGENERICSV(CopFILEGV(c))
167 #define SAVECOPLINE(c) SAVEI16(CopLINE(c))
169 /* SSNEW() temporarily allocates a specified number of bytes of data on the
170 * savestack. It returns an integer index into the savestack, because a
171 * pointer would get broken if the savestack is moved on reallocation.
172 * SSNEWa() works like SSNEW(), but also aligns the data to the specified
173 * number of bytes. MEM_ALIGNBYTES is perhaps the most useful. The
174 * alignment will be preserved therough savestack reallocation *only* if
175 * realloc returns data aligned to a size divisible by `align'!
177 * SSPTR() converts the index returned by SSNEW/SSNEWa() into a pointer.
180 #define SSNEW(size) Perl_save_alloc(aTHX_ (size), 0)
181 #define SSNEWt(n,t) SSNEW((n)*sizeof(t))
182 #define SSNEWa(size,align) Perl_save_alloc(aTHX_ (size), \
183 (align - ((int)((caddr_t)&PL_savestack[PL_savestack_ix]) % align)) % align)
184 #define SSNEWat(n,t,align) SSNEWa((n)*sizeof(t), align)
186 #define SSPTR(off,type) ((type) ((char*)PL_savestack + off))
187 #define SSPTRt(off,type) ((type*) ((char*)PL_savestack + off))
189 /* A jmpenv packages the state required to perform a proper non-local jump.
190 * Note that there is a start_env initialized when perl starts, and top_env
191 * points to this initially, so top_env should always be non-null.
193 * Existence of a non-null top_env->je_prev implies it is valid to call
194 * longjmp() at that runlevel (we make sure start_env.je_prev is always
195 * null to ensure this).
197 * je_mustcatch, when set at any runlevel to TRUE, means eval ops must
198 * establish a local jmpenv to handle exception traps. Care must be taken
199 * to restore the previous value of je_mustcatch before exiting the
200 * stack frame iff JMPENV_PUSH was not called in that stack frame.
205 struct jmpenv
* je_prev
;
206 Sigjmp_buf je_buf
; /* only for use if !je_throw */
207 int je_ret
; /* last exception thrown */
208 bool je_mustcatch
; /* need to call longjmp()? */
209 #ifdef PERL_FLEXIBLE_EXCEPTIONS
210 void (*je_throw
)(int v
); /* last for bincompat */
211 bool je_noset
; /* no need for setjmp() */
215 typedef struct jmpenv JMPENV
;
217 #ifdef OP_IN_REGISTER
218 #define OP_REG_TO_MEM PL_opsave = op
219 #define OP_MEM_TO_REG op = PL_opsave
221 #define OP_REG_TO_MEM NOOP
222 #define OP_MEM_TO_REG NOOP
226 * How to build the first jmpenv.
228 * top_env needs to be non-zero. It points to an area
229 * in which longjmp() stuff is stored, as C callstack
230 * info there at least is thread specific this has to
231 * be per-thread. Otherwise a 'die' in a thread gives
232 * that thread the C stack of last thread to do an eval {}!
235 #define JMPENV_BOOTSTRAP \
237 Zero(&PL_start_env, 1, JMPENV); \
238 PL_start_env.je_ret = -1; \
239 PL_start_env.je_mustcatch = TRUE; \
240 PL_top_env = &PL_start_env; \
243 #ifdef PERL_FLEXIBLE_EXCEPTIONS
246 * These exception-handling macros are split up to
247 * ease integration with C++ exceptions.
249 * To use C++ try+catch to catch Perl exceptions, an extension author
250 * needs to first write an extern "C" function to throw an appropriate
251 * exception object; typically it will be or contain an integer,
252 * because Perl's internals use integers to track exception types:
253 * extern "C" { static void thrower(int i) { throw i; } }
255 * Then (as shown below) the author needs to use, not the simple
256 * JMPENV_PUSH, but several of its constitutent macros, to arrange for
257 * the Perl internals to call thrower() rather than longjmp() to
261 * JMPENV_PUSH_INIT(thrower);
263 * ... stuff that may throw exceptions ...
265 * catch (int why) { // or whatever matches thrower()
269 * ... // handle various Perl exception codes
272 * JMPENV_POP; // don't forget this!
276 * Function that catches/throws, and its callback for the
277 * body of protected processing.
279 typedef void *(CPERLscope(*protect_body_t
)) (pTHX_
va_list);
280 typedef void *(CPERLscope(*protect_proc_t
)) (pTHX_
volatile JMPENV
*pcur_env
,
281 int *, protect_body_t
, ...);
283 #define dJMPENV JMPENV cur_env; \
284 volatile JMPENV *pcur_env = ((cur_env.je_noset = 0),&cur_env)
286 #define JMPENV_PUSH_INIT_ENV(ce,THROWFUNC) \
288 (ce).je_throw = (THROWFUNC); \
290 (ce).je_mustcatch = FALSE; \
291 (ce).je_prev = PL_top_env; \
292 PL_top_env = &(ce); \
296 #define JMPENV_PUSH_INIT(THROWFUNC) JMPENV_PUSH_INIT_ENV(*(JMPENV*)pcur_env,THROWFUNC)
298 #define JMPENV_POST_CATCH_ENV(ce) \
301 PL_top_env = &(ce); \
304 #define JMPENV_POST_CATCH JMPENV_POST_CATCH_ENV(*(JMPENV*)pcur_env)
306 #define JMPENV_PUSH_ENV(ce,v) \
308 if (!(ce).je_noset) { \
309 DEBUG_l(Perl_deb(aTHX_ "Setting up jumplevel %p, was %p\n", \
311 JMPENV_PUSH_INIT_ENV(ce,NULL); \
312 EXCEPT_SET_ENV(ce,PerlProc_setjmp((ce).je_buf, 1));\
316 EXCEPT_SET_ENV(ce,0); \
317 JMPENV_POST_CATCH_ENV(ce); \
318 (v) = EXCEPT_GET_ENV(ce); \
321 #define JMPENV_PUSH(v) JMPENV_PUSH_ENV(*(JMPENV*)pcur_env,v)
323 #define JMPENV_POP_ENV(ce) \
325 if (PL_top_env == &(ce)) \
326 PL_top_env = (ce).je_prev; \
329 #define JMPENV_POP JMPENV_POP_ENV(*(JMPENV*)pcur_env)
331 #define JMPENV_JUMP(v) \
334 if (PL_top_env->je_prev) { \
335 if (PL_top_env->je_throw) \
336 PL_top_env->je_throw(v); \
338 PerlProc_longjmp(PL_top_env->je_buf, (v)); \
341 PerlProc_exit(STATUS_NATIVE_EXPORT); \
342 PerlIO_printf(Perl_error_log, "panic: top_env\n"); \
346 #define EXCEPT_GET_ENV(ce) ((ce).je_ret)
347 #define EXCEPT_GET EXCEPT_GET_ENV(*(JMPENV*)pcur_env)
348 #define EXCEPT_SET_ENV(ce,v) ((ce).je_ret = (v))
349 #define EXCEPT_SET(v) EXCEPT_SET_ENV(*(JMPENV*)pcur_env,v)
351 #else /* !PERL_FLEXIBLE_EXCEPTIONS */
353 #define dJMPENV JMPENV cur_env
355 #define JMPENV_PUSH(v) \
357 DEBUG_l(Perl_deb(aTHX_ "Setting up jumplevel %p, was %p\n", \
358 &cur_env, PL_top_env)); \
359 cur_env.je_prev = PL_top_env; \
361 cur_env.je_ret = PerlProc_setjmp(cur_env.je_buf, 1); \
363 PL_top_env = &cur_env; \
364 cur_env.je_mustcatch = FALSE; \
365 (v) = cur_env.je_ret; \
369 STMT_START { PL_top_env = cur_env.je_prev; } STMT_END
371 #define JMPENV_JUMP(v) \
374 if (PL_top_env->je_prev) \
375 PerlProc_longjmp(PL_top_env->je_buf, (v)); \
377 PerlProc_exit(STATUS_NATIVE_EXPORT); \
378 PerlIO_printf(PerlIO_stderr(), "panic: top_env\n"); \
382 #endif /* PERL_FLEXIBLE_EXCEPTIONS */
384 #define CATCH_GET (PL_top_env->je_mustcatch)
385 #define CATCH_SET(v) (PL_top_env->je_mustcatch = (v))