This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
integrate changes#7858,7986 from mainline
[perl5.git] / scope.h
... / ...
CommitLineData
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
31#define SAVEt_DESTRUCTOR_X 30
32#define SAVEt_VPTR 31
33#define SAVEt_I8 32
34#define SAVEt_COMPPAD 33
35#define SAVEt_GENERIC_PVREF 34
36#define SAVEt_PADSV 35
37
38#define SSCHECK(need) if (PL_savestack_ix + need > PL_savestack_max) savestack_grow()
39#define SSPUSHINT(i) (PL_savestack[PL_savestack_ix++].any_i32 = (I32)(i))
40#define SSPUSHLONG(i) (PL_savestack[PL_savestack_ix++].any_long = (long)(i))
41#define SSPUSHIV(i) (PL_savestack[PL_savestack_ix++].any_iv = (IV)(i))
42#define SSPUSHPTR(p) (PL_savestack[PL_savestack_ix++].any_ptr = (void*)(p))
43#define SSPUSHDPTR(p) (PL_savestack[PL_savestack_ix++].any_dptr = (p))
44#define SSPUSHDXPTR(p) (PL_savestack[PL_savestack_ix++].any_dxptr = (p))
45#define SSPOPINT (PL_savestack[--PL_savestack_ix].any_i32)
46#define SSPOPLONG (PL_savestack[--PL_savestack_ix].any_long)
47#define SSPOPIV (PL_savestack[--PL_savestack_ix].any_iv)
48#define SSPOPPTR (PL_savestack[--PL_savestack_ix].any_ptr)
49#define SSPOPDPTR (PL_savestack[--PL_savestack_ix].any_dptr)
50#define SSPOPDXPTR (PL_savestack[--PL_savestack_ix].any_dxptr)
51
52/*
53=for apidoc Ams||SAVETMPS
54Opening bracket for temporaries on a callback. See C<FREETMPS> and
55L<perlcall>.
56
57=for apidoc Ams||FREETMPS
58Closing bracket for temporaries on a callback. See C<SAVETMPS> and
59L<perlcall>.
60
61=for apidoc Ams||ENTER
62Opening bracket on a callback. See C<LEAVE> and L<perlcall>.
63
64=for apidoc Ams||LEAVE
65Closing bracket on a callback. See C<ENTER> and L<perlcall>.
66
67=cut
68*/
69
70#define SAVETMPS save_int((int*)&PL_tmps_floor), PL_tmps_floor = PL_tmps_ix
71#define FREETMPS if (PL_tmps_ix > PL_tmps_floor) free_tmps()
72
73#ifdef DEBUGGING
74#define ENTER \
75 STMT_START { \
76 push_scope(); \
77 DEBUG_l(WITH_THR(Perl_deb(aTHX_ "ENTER scope %ld at %s:%d\n", \
78 PL_scopestack_ix, __FILE__, __LINE__))); \
79 } STMT_END
80#define LEAVE \
81 STMT_START { \
82 DEBUG_l(WITH_THR(Perl_deb(aTHX_ "LEAVE scope %ld at %s:%d\n", \
83 PL_scopestack_ix, __FILE__, __LINE__))); \
84 pop_scope(); \
85 } STMT_END
86#else
87#define ENTER push_scope()
88#define LEAVE pop_scope()
89#endif
90#define LEAVE_SCOPE(old) if (PL_savestack_ix > old) leave_scope(old)
91
92/*
93 * Not using SOFT_CAST on SAVESPTR, SAVEGENERICSV and SAVEFREESV
94 * because these are used for several kinds of pointer values
95 */
96#define SAVEI8(i) save_I8(SOFT_CAST(I8*)&(i))
97#define SAVEI16(i) save_I16(SOFT_CAST(I16*)&(i))
98#define SAVEI32(i) save_I32(SOFT_CAST(I32*)&(i))
99#define SAVEINT(i) save_int(SOFT_CAST(int*)&(i))
100#define SAVEIV(i) save_iv(SOFT_CAST(IV*)&(i))
101#define SAVELONG(l) save_long(SOFT_CAST(long*)&(l))
102#define SAVESPTR(s) save_sptr((SV**)&(s))
103#define SAVEPPTR(s) save_pptr(SOFT_CAST(char**)&(s))
104#define SAVEVPTR(s) save_vptr((void*)&(s))
105#define SAVEPADSV(s) save_padsv(s)
106#define SAVEFREESV(s) save_freesv((SV*)(s))
107#define SAVEFREEOP(o) save_freeop(SOFT_CAST(OP*)(o))
108#define SAVEFREEPV(p) save_freepv(SOFT_CAST(char*)(p))
109#define SAVECLEARSV(sv) save_clearsv(SOFT_CAST(SV**)&(sv))
110#define SAVEGENERICSV(s) save_generic_svref((SV**)&(s))
111#define SAVEGENERICPV(s) save_generic_pvref((char**)&(s))
112#define SAVEDELETE(h,k,l) \
113 save_delete(SOFT_CAST(HV*)(h), SOFT_CAST(char*)(k), (I32)(l))
114#define SAVEDESTRUCTOR(f,p) \
115 save_destructor((DESTRUCTORFUNC_NOCONTEXT_t)(f), SOFT_CAST(void*)(p))
116
117#define SAVEDESTRUCTOR_X(f,p) \
118 save_destructor_x((DESTRUCTORFUNC_t)(f), SOFT_CAST(void*)(p))
119
120#define SAVESTACK_POS() \
121 STMT_START { \
122 SSCHECK(2); \
123 SSPUSHINT(PL_stack_sp - PL_stack_base); \
124 SSPUSHINT(SAVEt_STACK_POS); \
125 } STMT_END
126
127#define SAVEOP() save_op()
128
129#define SAVEHINTS() \
130 STMT_START { \
131 if (PL_hints & HINT_LOCALIZE_HH) \
132 save_hints(); \
133 else { \
134 SSCHECK(2); \
135 SSPUSHINT(PL_hints); \
136 SSPUSHINT(SAVEt_HINTS); \
137 } \
138 } STMT_END
139
140#define SAVECOMPPAD() \
141 STMT_START { \
142 if (PL_comppad && PL_curpad == AvARRAY(PL_comppad)) { \
143 SSCHECK(2); \
144 SSPUSHPTR((SV*)PL_comppad); \
145 SSPUSHINT(SAVEt_COMPPAD); \
146 } \
147 else { \
148 SAVEVPTR(PL_curpad); \
149 SAVESPTR(PL_comppad); \
150 } \
151 } STMT_END
152
153#ifdef USE_ITHREADS
154# define SAVECOPSTASH(c) SAVEPPTR(CopSTASHPV(c))
155# define SAVECOPSTASH_FREE(c) SAVEGENERICPV(CopSTASHPV(c))
156# define SAVECOPFILE(c) SAVEPPTR(CopFILE(c))
157# define SAVECOPFILE_FREE(c) SAVEGENERICPV(CopFILE(c))
158#else
159# define SAVECOPSTASH(c) SAVESPTR(CopSTASH(c))
160# define SAVECOPSTASH_FREE(c) SAVECOPSTASH(c) /* XXX not refcounted */
161# define SAVECOPFILE(c) SAVESPTR(CopFILEGV(c))
162# define SAVECOPFILE_FREE(c) SAVEGENERICSV(CopFILEGV(c))
163#endif
164
165#define SAVECOPLINE(c) SAVEI16(CopLINE(c))
166
167/* SSNEW() temporarily allocates a specified number of bytes of data on the
168 * savestack. It returns an integer index into the savestack, because a
169 * pointer would get broken if the savestack is moved on reallocation.
170 * SSNEWa() works like SSNEW(), but also aligns the data to the specified
171 * number of bytes. MEM_ALIGNBYTES is perhaps the most useful. The
172 * alignment will be preserved therough savestack reallocation *only* if
173 * realloc returns data aligned to a size divisible by `align'!
174 *
175 * SSPTR() converts the index returned by SSNEW/SSNEWa() into a pointer.
176 */
177
178#define SSNEW(size) save_alloc(size, 0)
179#define SSNEWa(size,align) save_alloc(size, \
180 (align - ((int)((caddr_t)&PL_savestack[PL_savestack_ix]) % align)) % align)
181
182#define SSPTR(off,type) ((type) ((char*)PL_savestack + off))
183
184/* A jmpenv packages the state required to perform a proper non-local jump.
185 * Note that there is a start_env initialized when perl starts, and top_env
186 * points to this initially, so top_env should always be non-null.
187 *
188 * Existence of a non-null top_env->je_prev implies it is valid to call
189 * longjmp() at that runlevel (we make sure start_env.je_prev is always
190 * null to ensure this).
191 *
192 * je_mustcatch, when set at any runlevel to TRUE, means eval ops must
193 * establish a local jmpenv to handle exception traps. Care must be taken
194 * to restore the previous value of je_mustcatch before exiting the
195 * stack frame iff JMPENV_PUSH was not called in that stack frame.
196 * GSAR 97-03-27
197 */
198
199struct jmpenv {
200 struct jmpenv * je_prev;
201 Sigjmp_buf je_buf; /* only for use if !je_throw */
202 int je_ret; /* last exception thrown */
203 bool je_mustcatch; /* need to call longjmp()? */
204#ifdef PERL_FLEXIBLE_EXCEPTIONS
205 void (*je_throw)(int v); /* last for bincompat */
206 bool je_noset; /* no need for setjmp() */
207#endif
208};
209
210typedef struct jmpenv JMPENV;
211
212#ifdef OP_IN_REGISTER
213#define OP_REG_TO_MEM PL_opsave = op
214#define OP_MEM_TO_REG op = PL_opsave
215#else
216#define OP_REG_TO_MEM NOOP
217#define OP_MEM_TO_REG NOOP
218#endif
219
220/*
221 * How to build the first jmpenv.
222 *
223 * top_env needs to be non-zero. It points to an area
224 * in which longjmp() stuff is stored, as C callstack
225 * info there at least is thread specific this has to
226 * be per-thread. Otherwise a 'die' in a thread gives
227 * that thread the C stack of last thread to do an eval {}!
228 */
229
230#define JMPENV_BOOTSTRAP \
231 STMT_START { \
232 Zero(&PL_start_env, 1, JMPENV); \
233 PL_start_env.je_ret = -1; \
234 PL_start_env.je_mustcatch = TRUE; \
235 PL_top_env = &PL_start_env; \
236 } STMT_END
237
238#ifdef PERL_FLEXIBLE_EXCEPTIONS
239
240/*
241 * These exception-handling macros are split up to
242 * ease integration with C++ exceptions.
243 *
244 * To use C++ try+catch to catch Perl exceptions, an extension author
245 * needs to first write an extern "C" function to throw an appropriate
246 * exception object; typically it will be or contain an integer,
247 * because Perl's internals use integers to track exception types:
248 * extern "C" { static void thrower(int i) { throw i; } }
249 *
250 * Then (as shown below) the author needs to use, not the simple
251 * JMPENV_PUSH, but several of its constitutent macros, to arrange for
252 * the Perl internals to call thrower() rather than longjmp() to
253 * report exceptions:
254 *
255 * dJMPENV;
256 * JMPENV_PUSH_INIT(thrower);
257 * try {
258 * ... stuff that may throw exceptions ...
259 * }
260 * catch (int why) { // or whatever matches thrower()
261 * JMPENV_POST_CATCH;
262 * EXCEPT_SET(why);
263 * switch (why) {
264 * ... // handle various Perl exception codes
265 * }
266 * }
267 * JMPENV_POP; // don't forget this!
268 */
269
270/*
271 * Function that catches/throws, and its callback for the
272 * body of protected processing.
273 */
274typedef void *(CPERLscope(*protect_body_t)) (pTHX_ va_list);
275typedef void *(CPERLscope(*protect_proc_t)) (pTHX_ volatile JMPENV *pcur_env,
276 int *, protect_body_t, ...);
277
278#define dJMPENV JMPENV cur_env; \
279 volatile JMPENV *pcur_env = ((cur_env.je_noset = 0),&cur_env)
280
281#define JMPENV_PUSH_INIT_ENV(ce,THROWFUNC) \
282 STMT_START { \
283 (ce).je_throw = (THROWFUNC); \
284 (ce).je_ret = -1; \
285 (ce).je_mustcatch = FALSE; \
286 (ce).je_prev = PL_top_env; \
287 PL_top_env = &(ce); \
288 OP_REG_TO_MEM; \
289 } STMT_END
290
291#define JMPENV_PUSH_INIT(THROWFUNC) JMPENV_PUSH_INIT_ENV(*(JMPENV*)pcur_env,THROWFUNC)
292
293#define JMPENV_POST_CATCH_ENV(ce) \
294 STMT_START { \
295 OP_MEM_TO_REG; \
296 PL_top_env = &(ce); \
297 } STMT_END
298
299#define JMPENV_POST_CATCH JMPENV_POST_CATCH_ENV(*(JMPENV*)pcur_env)
300
301#define JMPENV_PUSH_ENV(ce,v) \
302 STMT_START { \
303 if (!(ce).je_noset) { \
304 DEBUG_l(Perl_deb(aTHX_ "Setting up jumplevel %p, was %p\n", \
305 ce, PL_top_env)); \
306 JMPENV_PUSH_INIT_ENV(ce,NULL); \
307 EXCEPT_SET_ENV(ce,PerlProc_setjmp((ce).je_buf, 1));\
308 (ce).je_noset = 1; \
309 } \
310 else \
311 EXCEPT_SET_ENV(ce,0); \
312 JMPENV_POST_CATCH_ENV(ce); \
313 (v) = EXCEPT_GET_ENV(ce); \
314 } STMT_END
315
316#define JMPENV_PUSH(v) JMPENV_PUSH_ENV(*(JMPENV*)pcur_env,v)
317
318#define JMPENV_POP_ENV(ce) \
319 STMT_START { \
320 if (PL_top_env == &(ce)) \
321 PL_top_env = (ce).je_prev; \
322 } STMT_END
323
324#define JMPENV_POP JMPENV_POP_ENV(*(JMPENV*)pcur_env)
325
326#define JMPENV_JUMP(v) \
327 STMT_START { \
328 OP_REG_TO_MEM; \
329 if (PL_top_env->je_prev) { \
330 if (PL_top_env->je_throw) \
331 PL_top_env->je_throw(v); \
332 else \
333 PerlProc_longjmp(PL_top_env->je_buf, (v)); \
334 } \
335 if ((v) == 2) \
336 PerlProc_exit(STATUS_NATIVE_EXPORT); \
337 PerlIO_printf(Perl_error_log, "panic: top_env\n"); \
338 PerlProc_exit(1); \
339 } STMT_END
340
341#define EXCEPT_GET_ENV(ce) ((ce).je_ret)
342#define EXCEPT_GET EXCEPT_GET_ENV(*(JMPENV*)pcur_env)
343#define EXCEPT_SET_ENV(ce,v) ((ce).je_ret = (v))
344#define EXCEPT_SET(v) EXCEPT_SET_ENV(*(JMPENV*)pcur_env,v)
345
346#else /* !PERL_FLEXIBLE_EXCEPTIONS */
347
348#define dJMPENV JMPENV cur_env
349
350#define JMPENV_PUSH(v) \
351 STMT_START { \
352 DEBUG_l(Perl_deb(aTHX_ "Setting up jumplevel %p, was %p\n", \
353 &cur_env, PL_top_env)); \
354 cur_env.je_prev = PL_top_env; \
355 OP_REG_TO_MEM; \
356 cur_env.je_ret = PerlProc_setjmp(cur_env.je_buf, 1); \
357 OP_MEM_TO_REG; \
358 PL_top_env = &cur_env; \
359 cur_env.je_mustcatch = FALSE; \
360 (v) = cur_env.je_ret; \
361 } STMT_END
362
363#define JMPENV_POP \
364 STMT_START { PL_top_env = cur_env.je_prev; } STMT_END
365
366#define JMPENV_JUMP(v) \
367 STMT_START { \
368 OP_REG_TO_MEM; \
369 if (PL_top_env->je_prev) \
370 PerlProc_longjmp(PL_top_env->je_buf, (v)); \
371 if ((v) == 2) \
372 PerlProc_exit(STATUS_NATIVE_EXPORT); \
373 PerlIO_printf(PerlIO_stderr(), "panic: top_env\n"); \
374 PerlProc_exit(1); \
375 } STMT_END
376
377#endif /* PERL_FLEXIBLE_EXCEPTIONS */
378
379#define CATCH_GET (PL_top_env->je_mustcatch)
380#define CATCH_SET(v) (PL_top_env->je_mustcatch = (v))