This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
NULL-terminate PERL_INC_VERSION_LIST
[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
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))
42 #define SSPUSHDXPTR(p) (PL_savestack[PL_savestack_ix++].any_dxptr = (p))
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)
48 #define SSPOPDXPTR (PL_savestack[--PL_savestack_ix].any_dxptr)
49
50 /*
51 =for apidoc Ams||SAVETMPS
52 Opening bracket for temporaries on a callback.  See C<FREETMPS> and
53 L<perlcall>.
54
55 =for apidoc Ams||FREETMPS
56 Closing bracket for temporaries on a callback.  See C<SAVETMPS> and
57 L<perlcall>.
58
59 =for apidoc Ams||ENTER
60 Opening bracket on a callback.  See C<LEAVE> and L<perlcall>.
61
62 =for apidoc Ams||LEAVE
63 Closing bracket on a callback.  See C<ENTER> and L<perlcall>.
64
65 =cut
66 */
67
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()
70
71 #ifdef DEBUGGING
72 #define ENTER                                                   \
73     STMT_START {                                                \
74         push_scope();                                           \
75         DEBUG_l(WITH_THR(Perl_deb(aTHX_ "ENTER scope %ld at %s:%d\n",   \
76                     PL_scopestack_ix, __FILE__, __LINE__)));    \
77     } STMT_END
78 #define LEAVE                                                   \
79     STMT_START {                                                \
80         DEBUG_l(WITH_THR(Perl_deb(aTHX_ "LEAVE scope %ld at %s:%d\n",   \
81                     PL_scopestack_ix, __FILE__, __LINE__)));    \
82         pop_scope();                                            \
83     } STMT_END
84 #else
85 #define ENTER push_scope()
86 #define LEAVE pop_scope()
87 #endif
88 #define LEAVE_SCOPE(old) if (PL_savestack_ix > old) leave_scope(old)
89
90 /*
91  * Not using SOFT_CAST on SAVESPTR, SAVEGENERICSV and SAVEFREESV
92  * because these are used for several kinds of pointer values
93  */
94 #define SAVEI8(i)       save_I8(SOFT_CAST(I8*)&(i))
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))
100 #define SAVESPTR(s)     save_sptr((SV**)&(s))
101 #define SAVEPPTR(s)     save_pptr(SOFT_CAST(char**)&(s))
102 #define SAVEVPTR(s)     save_vptr((void*)&(s))
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))
107 #define SAVEGENERICSV(s)        save_generic_svref((SV**)&(s))
108 #define SAVEDELETE(h,k,l) \
109           save_delete(SOFT_CAST(HV*)(h), SOFT_CAST(char*)(k), (I32)(l))
110 #define SAVEDESTRUCTOR(f,p) \
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))
115
116 #define SAVESTACK_POS() \
117     STMT_START {                                \
118         SSCHECK(2);                             \
119         SSPUSHINT(PL_stack_sp - PL_stack_base); \
120         SSPUSHINT(SAVEt_STACK_POS);             \
121     } STMT_END
122
123 #define SAVEOP()        save_op()
124
125 #define SAVEHINTS() \
126     STMT_START {                                \
127         if (PL_hints & HINT_LOCALIZE_HH)        \
128             save_hints();                       \
129         else {                                  \
130             SSCHECK(2);                         \
131             SSPUSHINT(PL_hints);                \
132             SSPUSHINT(SAVEt_HINTS);             \
133         }                                       \
134     } STMT_END
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
148
149 #ifdef USE_ITHREADS
150 #  define SAVECOPSTASH(cop)     SAVEPPTR(CopSTASHPV(cop))
151 #  define SAVECOPFILE(cop)      SAVEPPTR(CopFILE(cop))
152 #else
153 #  define SAVECOPSTASH(cop)     SAVESPTR(CopSTASH(cop))
154 #  define SAVECOPFILE(cop)      SAVESPTR(CopFILEGV(cop))
155 #endif
156
157 #define SAVECOPLINE(cop)        SAVEI16(CopLINE(cop))
158
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
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
181  * longjmp() at that runlevel (we make sure start_env.je_prev is always
182  * null to ensure this).
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.
188  * GSAR 97-03-27
189  */
190
191 struct jmpenv {
192     struct jmpenv *     je_prev;
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()? */
196     void                (*je_throw)(int v); /* last for bincompat */
197     bool                je_noset;       /* no need for setjmp() */
198 };
199
200 typedef struct jmpenv JMPENV;
201
202 /*
203  * Function that catches/throws, and its callback for the
204  *  body of protected processing.
205  */
206 typedef void *(CPERLscope(*protect_body_t)) (pTHX_ va_list);
207 typedef void *(CPERLscope(*protect_proc_t)) (pTHX_ volatile JMPENV *pcur_env,
208                                              int *, protect_body_t, ...);
209
210 /*
211  * How to build the first jmpenv.
212  *
213  * top_env needs to be non-zero. It points to an area
214  * in which longjmp() stuff is stored, as C callstack
215  * info there at least is thread specific this has to
216  * be per-thread. Otherwise a 'die' in a thread gives
217  * that thread the C stack of last thread to do an eval {}!
218  */
219
220 #define JMPENV_BOOTSTRAP \
221     STMT_START {                                \
222         PL_start_env.je_prev = NULL;            \
223         PL_start_env.je_throw = NULL;           \
224         PL_start_env.je_ret = -1;               \
225         PL_start_env.je_mustcatch = TRUE;       \
226         PL_start_env.je_noset = 0;              \
227         PL_top_env = &PL_start_env;             \
228     } STMT_END
229
230 #ifdef OP_IN_REGISTER
231 #define OP_REG_TO_MEM   PL_opsave = op
232 #define OP_MEM_TO_REG   op = PL_opsave
233 #else
234 #define OP_REG_TO_MEM   NOOP
235 #define OP_MEM_TO_REG   NOOP
236 #endif
237
238 /*
239  * These exception-handling macros are split up to
240  * ease integration with C++ exceptions.
241  *
242  * To use C++ try+catch to catch Perl exceptions, an extension author
243  * needs to first write an extern "C" function to throw an appropriate
244  * exception object; typically it will be or contain an integer,
245  * because Perl's internals use integers to track exception types:
246  *    extern "C" { static void thrower(int i) { throw i; } }
247  *
248  * Then (as shown below) the author needs to use, not the simple
249  * JMPENV_PUSH, but several of its constitutent macros, to arrange for
250  * the Perl internals to call thrower() rather than longjmp() to
251  * report exceptions:
252  *
253  *    dJMPENV;
254  *    JMPENV_PUSH_INIT(thrower);
255  *    try {
256  *        ... stuff that may throw exceptions ...
257  *    }
258  *    catch (int why) {  // or whatever matches thrower()
259  *        JMPENV_POST_CATCH;
260  *        EXCEPT_SET(why);
261  *        switch (why) {
262  *          ... // handle various Perl exception codes
263  *        }
264  *    }
265  *    JMPENV_POP;  // don't forget this!
266  */
267
268 #define dJMPENV JMPENV cur_env; \
269                 volatile JMPENV *pcur_env = ((cur_env.je_noset = 0),&cur_env)
270
271 #define JMPENV_PUSH_INIT_ENV(ce,THROWFUNC) \
272     STMT_START {                                        \
273         (ce).je_throw = (THROWFUNC);                    \
274         (ce).je_ret = -1;                               \
275         (ce).je_mustcatch = FALSE;                      \
276         (ce).je_prev = PL_top_env;                      \
277         PL_top_env = &(ce);                             \
278         OP_REG_TO_MEM;                                  \
279     } STMT_END
280
281 #define JMPENV_PUSH_INIT(THROWFUNC) JMPENV_PUSH_INIT_ENV(*(JMPENV*)pcur_env,THROWFUNC) 
282
283 #define JMPENV_POST_CATCH_ENV(ce) \
284     STMT_START {                                        \
285         OP_MEM_TO_REG;                                  \
286         PL_top_env = &(ce);                             \
287     } STMT_END
288
289 #define JMPENV_POST_CATCH JMPENV_POST_CATCH_ENV(*(JMPENV*)pcur_env)
290
291
292 #define JMPENV_PUSH_ENV(ce,v) \
293     STMT_START {                                                \
294         if (!(ce).je_noset) {                                   \
295             JMPENV_PUSH_INIT_ENV(ce,NULL);                      \
296             EXCEPT_SET_ENV(ce,PerlProc_setjmp((ce).je_buf, 1));\
297             (ce).je_noset = 1;                                  \
298         }                                                       \
299         else                                                    \
300             EXCEPT_SET_ENV(ce,0);                               \
301         JMPENV_POST_CATCH_ENV(ce);                              \
302         (v) = EXCEPT_GET_ENV(ce);                               \
303     } STMT_END
304
305 #define JMPENV_PUSH(v) JMPENV_PUSH_ENV(*(JMPENV*)pcur_env,v) 
306
307 #define JMPENV_POP_ENV(ce) \
308     STMT_START { PL_top_env = (ce).je_prev; } STMT_END
309
310 #define JMPENV_POP  JMPENV_POP_ENV(*(JMPENV*)pcur_env) 
311
312 #define JMPENV_JUMP(v) \
313     STMT_START {                                                \
314         OP_REG_TO_MEM;                                          \
315         if (PL_top_env->je_prev) {                              \
316             if (PL_top_env->je_throw)                           \
317                 PL_top_env->je_throw(v);                        \
318             else                                                \
319                 PerlProc_longjmp(PL_top_env->je_buf, (v));      \
320         }                                                       \
321         if ((v) == 2)                                           \
322             PerlProc_exit(STATUS_NATIVE_EXPORT);                \
323         PerlIO_printf(Perl_error_log, "panic: top_env\n");      \
324         PerlProc_exit(1);                                       \
325     } STMT_END
326
327 #define EXCEPT_GET_ENV(ce)      ((ce).je_ret)
328 #define EXCEPT_GET              EXCEPT_GET_ENV(*(JMPENV*)pcur_env)
329 #define EXCEPT_SET_ENV(ce,v)    ((ce).je_ret = (v))
330 #define EXCEPT_SET(v)           EXCEPT_SET_ENV(*(JMPENV*)pcur_env,v)
331
332 #define CATCH_GET               (PL_top_env->je_mustcatch)
333 #define CATCH_SET(v)            (PL_top_env->je_mustcatch = (v))