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