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