This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
support for C<use vmsish 'hushed'>; move VMSISH_EXIT out of
[perl5.git] / scope.h
CommitLineData
b9d12d37
GS
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
79072805 35
3280af22
NIS
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))
c76ac1ee 42#define SSPUSHDXPTR(p) (PL_savestack[PL_savestack_ix++].any_dxptr = (p))
3280af22
NIS
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)
c76ac1ee 48#define SSPOPDXPTR (PL_savestack[--PL_savestack_ix].any_dxptr)
8990e307 49
954c1994
GS
50/*
51=for apidoc Ams||SAVETMPS
52Opening bracket for temporaries on a callback. See C<FREETMPS> and
53L<perlcall>.
54
55=for apidoc Ams||FREETMPS
56Closing bracket for temporaries on a callback. See C<SAVETMPS> and
57L<perlcall>.
58
59=for apidoc Ams||ENTER
60Opening bracket on a callback. See C<LEAVE> and L<perlcall>.
61
62=for apidoc Ams||LEAVE
63Closing bracket on a callback. See C<ENTER> and L<perlcall>.
64
65=cut
66*/
67
3280af22
NIS
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()
a0d0e21e 70
f46d017c
GS
71#ifdef DEBUGGING
72#define ENTER \
73 STMT_START { \
74 push_scope(); \
cea2e8a9 75 DEBUG_l(WITH_THR(Perl_deb(aTHX_ "ENTER scope %ld at %s:%d\n", \
3280af22 76 PL_scopestack_ix, __FILE__, __LINE__))); \
f46d017c
GS
77 } STMT_END
78#define LEAVE \
79 STMT_START { \
cea2e8a9 80 DEBUG_l(WITH_THR(Perl_deb(aTHX_ "LEAVE scope %ld at %s:%d\n", \
3280af22 81 PL_scopestack_ix, __FILE__, __LINE__))); \
f46d017c
GS
82 pop_scope(); \
83 } STMT_END
84#else
a0d0e21e
LW
85#define ENTER push_scope()
86#define LEAVE pop_scope()
f46d017c 87#endif
3280af22 88#define LEAVE_SCOPE(old) if (PL_savestack_ix > old) leave_scope(old)
a0d0e21e 89
55497cff 90/*
b9d12d37 91 * Not using SOFT_CAST on SAVESPTR, SAVEGENERICSV and SAVEFREESV
55497cff 92 * because these are used for several kinds of pointer values
93 */
e8347627 94#define SAVEI8(i) save_I8(SOFT_CAST(I8*)&(i))
4fdae800 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))
55497cff 100#define SAVESPTR(s) save_sptr((SV**)&(s))
101#define SAVEPPTR(s) save_pptr(SOFT_CAST(char**)&(s))
57b2e452 102#define SAVEVPTR(s) save_vptr((void*)&(s))
55497cff 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))
b9d12d37 107#define SAVEGENERICSV(s) save_generic_svref((SV**)&(s))
55497cff 108#define SAVEDELETE(h,k,l) \
109 save_delete(SOFT_CAST(HV*)(h), SOFT_CAST(char*)(k), (I32)(l))
110#define SAVEDESTRUCTOR(f,p) \
c76ac1ee
GS
111 save_destructor((DESTRUCTORFUNC_NOCONTEXT_t)(f), SOFT_CAST(void*)(p))
112
113#define SAVEDESTRUCTOR_X(f,p) \
114 save_destructor_x((DESTRUCTORFUNC_t)(f), SOFT_CAST(void*)(p))
25eaa213
GS
115
116#define SAVESTACK_POS() \
117 STMT_START { \
118 SSCHECK(2); \
3280af22 119 SSPUSHINT(PL_stack_sp - PL_stack_base); \
25eaa213
GS
120 SSPUSHINT(SAVEt_STACK_POS); \
121 } STMT_END
122
462e5cf6 123#define SAVEOP() save_op()
25eaa213
GS
124
125#define SAVEHINTS() \
126 STMT_START { \
3280af22 127 if (PL_hints & HINT_LOCALIZE_HH) \
25eaa213
GS
128 save_hints(); \
129 else { \
130 SSCHECK(2); \
3280af22 131 SSPUSHINT(PL_hints); \
25eaa213
GS
132 SSPUSHINT(SAVEt_HINTS); \
133 } \
134 } STMT_END
354992b1
GS
135
136#define SAVECOMPPAD() \
137 STMT_START { \
138 if (PL_comppad && PL_curpad == AvARRAY(PL_comppad)) { \
139 SSCHECK(2); \
140 SSPUSHPTR((SV*)PL_comppad); \
141 SSPUSHINT(SAVEt_COMPPAD); \
142 } \
143 else { \
144 SAVEVPTR(PL_curpad); \
145 SAVESPTR(PL_comppad); \
146 } \
147 } STMT_END
a9332b4a 148
57843af0 149#ifdef USE_ITHREADS
11faa288 150# define SAVECOPSTASH(cop) SAVEPPTR(CopSTASHPV(cop))
57843af0
GS
151# define SAVECOPFILE(cop) SAVEPPTR(CopFILE(cop))
152#else
11faa288 153# define SAVECOPSTASH(cop) SAVESPTR(CopSTASH(cop))
57843af0
GS
154# define SAVECOPFILE(cop) SAVESPTR(CopFILEGV(cop))
155#endif
156
157#define SAVECOPLINE(cop) SAVEI16(CopLINE(cop))
158
455ece5e
AD
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'!
166 *
167 * SSPTR() converts the index returned by SSNEW/SSNEWa() into a pointer.
168 */
169
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)
173
174#define SSPTR(off,type) ((type) ((char*)PL_savestack + off))
175
54310121 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.
179 *
180 * Existence of a non-null top_env->je_prev implies it is valid to call
6224f72b
GS
181 * longjmp() at that runlevel (we make sure start_env.je_prev is always
182 * null to ensure this).
54310121 183 *
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.
6224f72b 188 * GSAR 97-03-27
54310121 189 */
190
191struct jmpenv {
192 struct jmpenv * je_prev;
312caa8e
CS
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()? */
14dd3ad8 196#ifdef PERL_FLEXIBLE_EXCEPTIONS
312caa8e 197 void (*je_throw)(int v); /* last for bincompat */
db36c5a1 198 bool je_noset; /* no need for setjmp() */
14dd3ad8 199#endif
1163b5c4 200};
1163b5c4 201
6224f72b 202typedef struct jmpenv JMPENV;
1163b5c4 203
14dd3ad8
GS
204#ifdef OP_IN_REGISTER
205#define OP_REG_TO_MEM PL_opsave = op
206#define OP_MEM_TO_REG op = PL_opsave
207#else
208#define OP_REG_TO_MEM NOOP
209#define OP_MEM_TO_REG NOOP
210#endif
312caa8e
CS
211
212/*
213 * How to build the first jmpenv.
214 *
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 {}!
220 */
221
222#define JMPENV_BOOTSTRAP \
223 STMT_START { \
14dd3ad8 224 Zero(&PL_start_env, 1, JMPENV); \
312caa8e
CS
225 PL_start_env.je_ret = -1; \
226 PL_start_env.je_mustcatch = TRUE; \
227 PL_top_env = &PL_start_env; \
228 } STMT_END
229
14dd3ad8 230#ifdef PERL_FLEXIBLE_EXCEPTIONS
462e5cf6 231
312caa8e
CS
232/*
233 * These exception-handling macros are split up to
234 * ease integration with C++ exceptions.
235 *
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; } }
241 *
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
245 * report exceptions:
246 *
247 * dJMPENV;
248 * JMPENV_PUSH_INIT(thrower);
249 * try {
250 * ... stuff that may throw exceptions ...
251 * }
252 * catch (int why) { // or whatever matches thrower()
253 * JMPENV_POST_CATCH;
254 * EXCEPT_SET(why);
255 * switch (why) {
256 * ... // handle various Perl exception codes
257 * }
258 * }
259 * JMPENV_POP; // don't forget this!
260 */
261
14dd3ad8
GS
262/*
263 * Function that catches/throws, and its callback for the
264 * body of protected processing.
265 */
266typedef void *(CPERLscope(*protect_body_t)) (pTHX_ va_list);
267typedef void *(CPERLscope(*protect_proc_t)) (pTHX_ volatile JMPENV *pcur_env,
268 int *, protect_body_t, ...);
269
db36c5a1
GS
270#define dJMPENV JMPENV cur_env; \
271 volatile JMPENV *pcur_env = ((cur_env.je_noset = 0),&cur_env)
312caa8e 272
db36c5a1 273#define JMPENV_PUSH_INIT_ENV(ce,THROWFUNC) \
6224f72b 274 STMT_START { \
db36c5a1
GS
275 (ce).je_throw = (THROWFUNC); \
276 (ce).je_ret = -1; \
277 (ce).je_mustcatch = FALSE; \
278 (ce).je_prev = PL_top_env; \
279 PL_top_env = &(ce); \
6224f72b 280 OP_REG_TO_MEM; \
312caa8e 281 } STMT_END
1d651c14 282
db36c5a1 283#define JMPENV_PUSH_INIT(THROWFUNC) JMPENV_PUSH_INIT_ENV(*(JMPENV*)pcur_env,THROWFUNC)
1d651c14 284
db36c5a1 285#define JMPENV_POST_CATCH_ENV(ce) \
312caa8e 286 STMT_START { \
6224f72b 287 OP_MEM_TO_REG; \
db36c5a1 288 PL_top_env = &(ce); \
6224f72b 289 } STMT_END
312caa8e 290
db36c5a1 291#define JMPENV_POST_CATCH JMPENV_POST_CATCH_ENV(*(JMPENV*)pcur_env)
1d651c14 292
db36c5a1
GS
293#define JMPENV_PUSH_ENV(ce,v) \
294 STMT_START { \
295 if (!(ce).je_noset) { \
14dd3ad8
GS
296 DEBUG_l(Perl_deb(aTHX_ "Setting up jumplevel %p, was %p\n", \
297 ce, PL_top_env)); \
db36c5a1
GS
298 JMPENV_PUSH_INIT_ENV(ce,NULL); \
299 EXCEPT_SET_ENV(ce,PerlProc_setjmp((ce).je_buf, 1));\
300 (ce).je_noset = 1; \
301 } \
302 else \
303 EXCEPT_SET_ENV(ce,0); \
304 JMPENV_POST_CATCH_ENV(ce); \
305 (v) = EXCEPT_GET_ENV(ce); \
312caa8e
CS
306 } STMT_END
307
db36c5a1 308#define JMPENV_PUSH(v) JMPENV_PUSH_ENV(*(JMPENV*)pcur_env,v)
1d651c14 309
db36c5a1 310#define JMPENV_POP_ENV(ce) \
14dd3ad8
GS
311 STMT_START { \
312 if (PL_top_env == &(ce)) \
313 PL_top_env = (ce).je_prev; \
314 } STMT_END
312caa8e 315
db36c5a1 316#define JMPENV_POP JMPENV_POP_ENV(*(JMPENV*)pcur_env)
1d651c14 317
22921e25
CS
318#define JMPENV_JUMP(v) \
319 STMT_START { \
462e5cf6 320 OP_REG_TO_MEM; \
312caa8e
CS
321 if (PL_top_env->je_prev) { \
322 if (PL_top_env->je_throw) \
323 PL_top_env->je_throw(v); \
324 else \
325 PerlProc_longjmp(PL_top_env->je_buf, (v)); \
326 } \
6224f72b 327 if ((v) == 2) \
312caa8e 328 PerlProc_exit(STATUS_NATIVE_EXPORT); \
bf49b057 329 PerlIO_printf(Perl_error_log, "panic: top_env\n"); \
312caa8e 330 PerlProc_exit(1); \
22921e25 331 } STMT_END
312caa8e 332
db36c5a1
GS
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)
312caa8e 337
14dd3ad8
GS
338#else /* !PERL_FLEXIBLE_EXCEPTIONS */
339
340#define dJMPENV JMPENV cur_env
341
342#define JMPENV_PUSH(v) \
343 STMT_START { \
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; \
347 OP_REG_TO_MEM; \
348 cur_env.je_ret = PerlProc_setjmp(cur_env.je_buf, 1); \
349 OP_MEM_TO_REG; \
350 PL_top_env = &cur_env; \
351 cur_env.je_mustcatch = FALSE; \
352 (v) = cur_env.je_ret; \
353 } STMT_END
354
355#define JMPENV_POP \
356 STMT_START { PL_top_env = cur_env.je_prev; } STMT_END
357
358#define JMPENV_JUMP(v) \
359 STMT_START { \
360 OP_REG_TO_MEM; \
361 if (PL_top_env->je_prev) \
362 PerlProc_longjmp(PL_top_env->je_buf, (v)); \
363 if ((v) == 2) \
364 PerlProc_exit(STATUS_NATIVE_EXPORT); \
365 PerlIO_printf(PerlIO_stderr(), "panic: top_env\n"); \
366 PerlProc_exit(1); \
367 } STMT_END
368
369#endif /* PERL_FLEXIBLE_EXCEPTIONS */
370
db36c5a1
GS
371#define CATCH_GET (PL_top_env->je_mustcatch)
372#define CATCH_SET(v) (PL_top_env->je_mustcatch = (v))