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