glob-basic.t, runenv.t fix-ups
[p5sagit/p5-mst-13.2.git] / scope.h
CommitLineData
b9d12d37 1#define SAVEt_ITEM 0
2#define SAVEt_SV 1
3#define SAVEt_AV 2
4#define SAVEt_HV 3
5#define SAVEt_INT 4
6#define SAVEt_LONG 5
7#define SAVEt_I32 6
8#define SAVEt_IV 7
9#define SAVEt_SPTR 8
10#define SAVEt_APTR 9
11#define SAVEt_HPTR 10
12#define SAVEt_PPTR 11
13#define SAVEt_NSTAB 12
14#define SAVEt_SVREF 13
15#define SAVEt_GP 14
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
24#define SAVEt_I16 23
25#define SAVEt_AELEM 24
26#define SAVEt_HELEM 25
27#define SAVEt_OP 26
28#define SAVEt_HINTS 27
29#define SAVEt_ALLOC 28
30#define SAVEt_GENERIC_SVREF 29
c76ac1ee 31#define SAVEt_DESTRUCTOR_X 30
7766f137 32#define SAVEt_VPTR 31
e8347627 33#define SAVEt_I8 32
354992b1 34#define SAVEt_COMPPAD 33
f4dd75d9 35#define SAVEt_GENERIC_PVREF 34
c3564e5c 36#define SAVEt_PADSV 35
79072805 37
b03c0a3a 38#ifndef SCOPE_SAVES_SIGNAL_MASK
1b266415 39#define SCOPE_SAVES_SIGNAL_MASK 0
b03c0a3a 40#endif
41
3280af22 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))
c76ac1ee 48#define SSPUSHDXPTR(p) (PL_savestack[PL_savestack_ix++].any_dxptr = (p))
3280af22 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)
c76ac1ee 54#define SSPOPDXPTR (PL_savestack[--PL_savestack_ix].any_dxptr)
8990e307 55
954c1994 56/*
57=for apidoc Ams||SAVETMPS
58Opening bracket for temporaries on a callback. See C<FREETMPS> and
59L<perlcall>.
60
61=for apidoc Ams||FREETMPS
62Closing bracket for temporaries on a callback. See C<SAVETMPS> and
63L<perlcall>.
64
65=for apidoc Ams||ENTER
66Opening bracket on a callback. See C<LEAVE> and L<perlcall>.
67
68=for apidoc Ams||LEAVE
69Closing bracket on a callback. See C<ENTER> and L<perlcall>.
70
71=cut
72*/
73
3280af22 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()
a0d0e21e 76
f46d017c 77#ifdef DEBUGGING
78#define ENTER \
79 STMT_START { \
80 push_scope(); \
cea2e8a9 81 DEBUG_l(WITH_THR(Perl_deb(aTHX_ "ENTER scope %ld at %s:%d\n", \
3280af22 82 PL_scopestack_ix, __FILE__, __LINE__))); \
f46d017c 83 } STMT_END
84#define LEAVE \
85 STMT_START { \
cea2e8a9 86 DEBUG_l(WITH_THR(Perl_deb(aTHX_ "LEAVE scope %ld at %s:%d\n", \
3280af22 87 PL_scopestack_ix, __FILE__, __LINE__))); \
f46d017c 88 pop_scope(); \
89 } STMT_END
90#else
a0d0e21e 91#define ENTER push_scope()
92#define LEAVE pop_scope()
f46d017c 93#endif
3280af22 94#define LEAVE_SCOPE(old) if (PL_savestack_ix > old) leave_scope(old)
a0d0e21e 95
55497cff 96/*
b9d12d37 97 * Not using SOFT_CAST on SAVESPTR, SAVEGENERICSV and SAVEFREESV
55497cff 98 * because these are used for several kinds of pointer values
99 */
e8347627 100#define SAVEI8(i) save_I8(SOFT_CAST(I8*)&(i))
4fdae800 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))
55497cff 106#define SAVESPTR(s) save_sptr((SV**)&(s))
107#define SAVEPPTR(s) save_pptr(SOFT_CAST(char**)&(s))
57b2e452 108#define SAVEVPTR(s) save_vptr((void*)&(s))
c3564e5c 109#define SAVEPADSV(s) save_padsv(s)
55497cff 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))
b9d12d37 114#define SAVEGENERICSV(s) save_generic_svref((SV**)&(s))
f4dd75d9 115#define SAVEGENERICPV(s) save_generic_pvref((char**)&(s))
55497cff 116#define SAVEDELETE(h,k,l) \
117 save_delete(SOFT_CAST(HV*)(h), SOFT_CAST(char*)(k), (I32)(l))
118#define SAVEDESTRUCTOR(f,p) \
c76ac1ee 119 save_destructor((DESTRUCTORFUNC_NOCONTEXT_t)(f), SOFT_CAST(void*)(p))
120
121#define SAVEDESTRUCTOR_X(f,p) \
122 save_destructor_x((DESTRUCTORFUNC_t)(f), SOFT_CAST(void*)(p))
25eaa213 123
124#define SAVESTACK_POS() \
125 STMT_START { \
126 SSCHECK(2); \
3280af22 127 SSPUSHINT(PL_stack_sp - PL_stack_base); \
25eaa213 128 SSPUSHINT(SAVEt_STACK_POS); \
129 } STMT_END
130
462e5cf6 131#define SAVEOP() save_op()
25eaa213 132
133#define SAVEHINTS() \
134 STMT_START { \
3280af22 135 if (PL_hints & HINT_LOCALIZE_HH) \
25eaa213 136 save_hints(); \
137 else { \
138 SSCHECK(2); \
3280af22 139 SSPUSHINT(PL_hints); \
25eaa213 140 SSPUSHINT(SAVEt_HINTS); \
141 } \
142 } STMT_END
354992b1 143
144#define SAVECOMPPAD() \
145 STMT_START { \
146 if (PL_comppad && PL_curpad == AvARRAY(PL_comppad)) { \
147 SSCHECK(2); \
148 SSPUSHPTR((SV*)PL_comppad); \
149 SSPUSHINT(SAVEt_COMPPAD); \
150 } \
151 else { \
152 SAVEVPTR(PL_curpad); \
153 SAVESPTR(PL_comppad); \
154 } \
155 } STMT_END
a9332b4a 156
57843af0 157#ifdef USE_ITHREADS
f4dd75d9 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))
57843af0 162#else
f4dd75d9 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))
57843af0 167#endif
168
f4dd75d9 169#define SAVECOPLINE(c) SAVEI16(CopLINE(c))
57843af0 170
455ece5e 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'!
178 *
179 * SSPTR() converts the index returned by SSNEW/SSNEWa() into a pointer.
180 */
181
f2b2c1a7 182#define SSNEW(size) Perl_save_alloc(aTHX_ (size), 0)
02db2b7b 183#define SSNEWt(n,t) SSNEW((n)*sizeof(t))
f2b2c1a7 184#define SSNEWa(size,align) Perl_save_alloc(aTHX_ (size), \
455ece5e 185 (align - ((int)((caddr_t)&PL_savestack[PL_savestack_ix]) % align)) % align)
02db2b7b 186#define SSNEWat(n,t,align) SSNEWa((n)*sizeof(t), align)
455ece5e 187
02db2b7b 188#define SSPTR(off,type) ((type) ((char*)PL_savestack + off))
189#define SSPTRt(off,type) ((type*) ((char*)PL_savestack + off))
455ece5e 190
54310121 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.
194 *
195 * Existence of a non-null top_env->je_prev implies it is valid to call
6224f72b 196 * longjmp() at that runlevel (we make sure start_env.je_prev is always
197 * null to ensure this).
54310121 198 *
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.
6224f72b 203 * GSAR 97-03-27
54310121 204 */
205
206struct jmpenv {
207 struct jmpenv * je_prev;
312caa8e 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()? */
14dd3ad8 211#ifdef PERL_FLEXIBLE_EXCEPTIONS
312caa8e 212 void (*je_throw)(int v); /* last for bincompat */
db36c5a1 213 bool je_noset; /* no need for setjmp() */
14dd3ad8 214#endif
1163b5c4 215};
1163b5c4 216
6224f72b 217typedef struct jmpenv JMPENV;
1163b5c4 218
14dd3ad8 219#ifdef OP_IN_REGISTER
220#define OP_REG_TO_MEM PL_opsave = op
221#define OP_MEM_TO_REG op = PL_opsave
222#else
223#define OP_REG_TO_MEM NOOP
224#define OP_MEM_TO_REG NOOP
225#endif
312caa8e 226
227/*
228 * How to build the first jmpenv.
229 *
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 {}!
235 */
236
237#define JMPENV_BOOTSTRAP \
238 STMT_START { \
14dd3ad8 239 Zero(&PL_start_env, 1, JMPENV); \
312caa8e 240 PL_start_env.je_ret = -1; \
241 PL_start_env.je_mustcatch = TRUE; \
242 PL_top_env = &PL_start_env; \
243 } STMT_END
244
14dd3ad8 245#ifdef PERL_FLEXIBLE_EXCEPTIONS
462e5cf6 246
312caa8e 247/*
248 * These exception-handling macros are split up to
249 * ease integration with C++ exceptions.
250 *
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; } }
256 *
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
260 * report exceptions:
261 *
262 * dJMPENV;
263 * JMPENV_PUSH_INIT(thrower);
264 * try {
265 * ... stuff that may throw exceptions ...
266 * }
267 * catch (int why) { // or whatever matches thrower()
268 * JMPENV_POST_CATCH;
269 * EXCEPT_SET(why);
270 * switch (why) {
271 * ... // handle various Perl exception codes
272 * }
273 * }
274 * JMPENV_POP; // don't forget this!
275 */
276
14dd3ad8 277/*
278 * Function that catches/throws, and its callback for the
279 * body of protected processing.
280 */
281typedef void *(CPERLscope(*protect_body_t)) (pTHX_ va_list);
282typedef void *(CPERLscope(*protect_proc_t)) (pTHX_ volatile JMPENV *pcur_env,
283 int *, protect_body_t, ...);
284
db36c5a1 285#define dJMPENV JMPENV cur_env; \
286 volatile JMPENV *pcur_env = ((cur_env.je_noset = 0),&cur_env)
312caa8e 287
db36c5a1 288#define JMPENV_PUSH_INIT_ENV(ce,THROWFUNC) \
6224f72b 289 STMT_START { \
db36c5a1 290 (ce).je_throw = (THROWFUNC); \
291 (ce).je_ret = -1; \
292 (ce).je_mustcatch = FALSE; \
293 (ce).je_prev = PL_top_env; \
294 PL_top_env = &(ce); \
6224f72b 295 OP_REG_TO_MEM; \
312caa8e 296 } STMT_END
1d651c14 297
f2b2c1a7 298#define JMPENV_PUSH_INIT(THROWFUNC) JMPENV_PUSH_INIT_ENV(*(JMPENV*)pcur_env,THROWFUNC)
1d651c14 299
db36c5a1 300#define JMPENV_POST_CATCH_ENV(ce) \
312caa8e 301 STMT_START { \
6224f72b 302 OP_MEM_TO_REG; \
db36c5a1 303 PL_top_env = &(ce); \
6224f72b 304 } STMT_END
312caa8e 305
db36c5a1 306#define JMPENV_POST_CATCH JMPENV_POST_CATCH_ENV(*(JMPENV*)pcur_env)
1d651c14 307
db36c5a1 308#define JMPENV_PUSH_ENV(ce,v) \
309 STMT_START { \
310 if (!(ce).je_noset) { \
14dd3ad8 311 DEBUG_l(Perl_deb(aTHX_ "Setting up jumplevel %p, was %p\n", \
312 ce, PL_top_env)); \
db36c5a1 313 JMPENV_PUSH_INIT_ENV(ce,NULL); \
b03c0a3a 314 EXCEPT_SET_ENV(ce,PerlProc_setjmp((ce).je_buf, SCOPE_SAVES_SIGNAL_MASK));\
db36c5a1 315 (ce).je_noset = 1; \
316 } \
317 else \
318 EXCEPT_SET_ENV(ce,0); \
319 JMPENV_POST_CATCH_ENV(ce); \
320 (v) = EXCEPT_GET_ENV(ce); \
312caa8e 321 } STMT_END
322
f2b2c1a7 323#define JMPENV_PUSH(v) JMPENV_PUSH_ENV(*(JMPENV*)pcur_env,v)
1d651c14 324
db36c5a1 325#define JMPENV_POP_ENV(ce) \
14dd3ad8 326 STMT_START { \
327 if (PL_top_env == &(ce)) \
328 PL_top_env = (ce).je_prev; \
329 } STMT_END
312caa8e 330
f2b2c1a7 331#define JMPENV_POP JMPENV_POP_ENV(*(JMPENV*)pcur_env)
1d651c14 332
22921e25 333#define JMPENV_JUMP(v) \
334 STMT_START { \
462e5cf6 335 OP_REG_TO_MEM; \
312caa8e 336 if (PL_top_env->je_prev) { \
337 if (PL_top_env->je_throw) \
338 PL_top_env->je_throw(v); \
339 else \
340 PerlProc_longjmp(PL_top_env->je_buf, (v)); \
341 } \
6224f72b 342 if ((v) == 2) \
312caa8e 343 PerlProc_exit(STATUS_NATIVE_EXPORT); \
bf49b057 344 PerlIO_printf(Perl_error_log, "panic: top_env\n"); \
312caa8e 345 PerlProc_exit(1); \
22921e25 346 } STMT_END
312caa8e 347
db36c5a1 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)
312caa8e 352
14dd3ad8 353#else /* !PERL_FLEXIBLE_EXCEPTIONS */
354
355#define dJMPENV JMPENV cur_env
356
357#define JMPENV_PUSH(v) \
358 STMT_START { \
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; \
362 OP_REG_TO_MEM; \
b03c0a3a 363 cur_env.je_ret = PerlProc_setjmp(cur_env.je_buf, SCOPE_SAVES_SIGNAL_MASK); \
14dd3ad8 364 OP_MEM_TO_REG; \
365 PL_top_env = &cur_env; \
366 cur_env.je_mustcatch = FALSE; \
367 (v) = cur_env.je_ret; \
368 } STMT_END
369
370#define JMPENV_POP \
371 STMT_START { PL_top_env = cur_env.je_prev; } STMT_END
372
373#define JMPENV_JUMP(v) \
374 STMT_START { \
375 OP_REG_TO_MEM; \
376 if (PL_top_env->je_prev) \
377 PerlProc_longjmp(PL_top_env->je_buf, (v)); \
378 if ((v) == 2) \
379 PerlProc_exit(STATUS_NATIVE_EXPORT); \
380 PerlIO_printf(PerlIO_stderr(), "panic: top_env\n"); \
381 PerlProc_exit(1); \
382 } STMT_END
383
384#endif /* PERL_FLEXIBLE_EXCEPTIONS */
385
db36c5a1 386#define CATCH_GET (PL_top_env->je_mustcatch)
387#define CATCH_SET(v) (PL_top_env->je_mustcatch = (v))