This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
make die/warn and other diagnostics go to wherever STDERR happens
[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
32 #define SSCHECK(need) if (PL_savestack_ix + need > PL_savestack_max) savestack_grow()
33 #define SSPUSHINT(i) (PL_savestack[PL_savestack_ix++].any_i32 = (I32)(i))
34 #define SSPUSHLONG(i) (PL_savestack[PL_savestack_ix++].any_long = (long)(i))
35 #define SSPUSHIV(i) (PL_savestack[PL_savestack_ix++].any_iv = (IV)(i))
36 #define SSPUSHPTR(p) (PL_savestack[PL_savestack_ix++].any_ptr = (void*)(p))
37 #define SSPUSHDPTR(p) (PL_savestack[PL_savestack_ix++].any_dptr = (p))
38 #define SSPOPINT (PL_savestack[--PL_savestack_ix].any_i32)
39 #define SSPOPLONG (PL_savestack[--PL_savestack_ix].any_long)
40 #define SSPOPIV (PL_savestack[--PL_savestack_ix].any_iv)
41 #define SSPOPPTR (PL_savestack[--PL_savestack_ix].any_ptr)
42 #define SSPOPDPTR (PL_savestack[--PL_savestack_ix].any_dptr)
43
44 #define SAVETMPS save_int((int*)&PL_tmps_floor), PL_tmps_floor = PL_tmps_ix
45 #define FREETMPS if (PL_tmps_ix > PL_tmps_floor) free_tmps()
46
47 #ifdef DEBUGGING
48 #define ENTER                                                   \
49     STMT_START {                                                \
50         push_scope();                                           \
51         DEBUG_l(WITH_THR(Perl_deb(aTHX_ "ENTER scope %ld at %s:%d\n",   \
52                     PL_scopestack_ix, __FILE__, __LINE__)));    \
53     } STMT_END
54 #define LEAVE                                                   \
55     STMT_START {                                                \
56         DEBUG_l(WITH_THR(Perl_deb(aTHX_ "LEAVE scope %ld at %s:%d\n",   \
57                     PL_scopestack_ix, __FILE__, __LINE__)));    \
58         pop_scope();                                            \
59     } STMT_END
60 #else
61 #define ENTER push_scope()
62 #define LEAVE pop_scope()
63 #endif
64 #define LEAVE_SCOPE(old) if (PL_savestack_ix > old) leave_scope(old)
65
66 /*
67  * Not using SOFT_CAST on SAVESPTR, SAVEGENERICSV and SAVEFREESV
68  * because these are used for several kinds of pointer values
69  */
70 #define SAVEI16(i)      save_I16(SOFT_CAST(I16*)&(i))
71 #define SAVEI32(i)      save_I32(SOFT_CAST(I32*)&(i))
72 #define SAVEINT(i)      save_int(SOFT_CAST(int*)&(i))
73 #define SAVEIV(i)       save_iv(SOFT_CAST(IV*)&(i))
74 #define SAVELONG(l)     save_long(SOFT_CAST(long*)&(l))
75 #define SAVESPTR(s)     save_sptr((SV**)&(s))
76 #define SAVEPPTR(s)     save_pptr(SOFT_CAST(char**)&(s))
77 #define SAVEFREESV(s)   save_freesv((SV*)(s))
78 #define SAVEFREEOP(o)   save_freeop(SOFT_CAST(OP*)(o))
79 #define SAVEFREEPV(p)   save_freepv(SOFT_CAST(char*)(p))
80 #define SAVECLEARSV(sv) save_clearsv(SOFT_CAST(SV**)&(sv))
81 #define SAVEGENERICSV(s)        save_generic_svref((SV**)&(s))
82 #define SAVEDELETE(h,k,l) \
83           save_delete(SOFT_CAST(HV*)(h), SOFT_CAST(char*)(k), (I32)(l))
84 #define CALLDESTRUCTOR (*SSPOPDPTR)
85 #define SAVEDESTRUCTOR(f,p) \
86           save_destructor((DESTRUCTORFUNC_t)(f), SOFT_CAST(void*)(p))
87
88 #define SAVESTACK_POS() \
89     STMT_START {                                \
90         SSCHECK(2);                             \
91         SSPUSHINT(PL_stack_sp - PL_stack_base); \
92         SSPUSHINT(SAVEt_STACK_POS);             \
93     } STMT_END
94
95 #define SAVEOP()        save_op()
96
97 #define SAVEHINTS() \
98     STMT_START {                                \
99         if (PL_hints & HINT_LOCALIZE_HH)        \
100             save_hints();                       \
101         else {                                  \
102             SSCHECK(2);                         \
103             SSPUSHINT(PL_hints);                \
104             SSPUSHINT(SAVEt_HINTS);             \
105         }                                       \
106     } STMT_END
107
108 /* SSNEW() temporarily allocates a specified number of bytes of data on the
109  * savestack.  It returns an integer index into the savestack, because a
110  * pointer would get broken if the savestack is moved on reallocation.
111  * SSNEWa() works like SSNEW(), but also aligns the data to the specified
112  * number of bytes.  MEM_ALIGNBYTES is perhaps the most useful.  The
113  * alignment will be preserved therough savestack reallocation *only* if
114  * realloc returns data aligned to a size divisible by `align'!
115  *
116  * SSPTR() converts the index returned by SSNEW/SSNEWa() into a pointer.
117  */
118
119 #define SSNEW(size)             save_alloc(size, 0)
120 #define SSNEWa(size,align)      save_alloc(size, \
121     (align - ((int)((caddr_t)&PL_savestack[PL_savestack_ix]) % align)) % align)
122
123 #define SSPTR(off,type)         ((type) ((char*)PL_savestack + off))
124
125 /* A jmpenv packages the state required to perform a proper non-local jump.
126  * Note that there is a start_env initialized when perl starts, and top_env
127  * points to this initially, so top_env should always be non-null.
128  *
129  * Existence of a non-null top_env->je_prev implies it is valid to call
130  * longjmp() at that runlevel (we make sure start_env.je_prev is always
131  * null to ensure this).
132  *
133  * je_mustcatch, when set at any runlevel to TRUE, means eval ops must
134  * establish a local jmpenv to handle exception traps.  Care must be taken
135  * to restore the previous value of je_mustcatch before exiting the
136  * stack frame iff JMPENV_PUSH was not called in that stack frame.
137  * GSAR 97-03-27
138  */
139
140 struct jmpenv {
141     struct jmpenv *     je_prev;
142     Sigjmp_buf          je_buf;         /* only for use if !je_throw */
143     int                 je_ret;         /* last exception thrown */
144     bool                je_mustcatch;   /* need to call longjmp()? */
145     void                (*je_throw)(int v); /* last for bincompat */
146 };
147
148 typedef struct jmpenv JMPENV;
149
150 /*
151  * Function that catches/throws, and its callback for the
152  *  body of protected processing.
153  */
154 typedef void *(CPERLscope(*protect_body_t)) (pTHX_ va_list);
155 typedef void *(CPERLscope(*protect_proc_t)) (pTHX_ int *, protect_body_t, ...);
156
157 /*
158  * How to build the first jmpenv.
159  *
160  * top_env needs to be non-zero. It points to an area
161  * in which longjmp() stuff is stored, as C callstack
162  * info there at least is thread specific this has to
163  * be per-thread. Otherwise a 'die' in a thread gives
164  * that thread the C stack of last thread to do an eval {}!
165  */
166
167 #define JMPENV_BOOTSTRAP \
168     STMT_START {                                \
169         PL_start_env.je_prev = NULL;            \
170         PL_start_env.je_throw = NULL;           \
171         PL_start_env.je_ret = -1;               \
172         PL_start_env.je_mustcatch = TRUE;       \
173         PL_top_env = &PL_start_env;             \
174     } STMT_END
175
176 #ifdef OP_IN_REGISTER
177 #define OP_REG_TO_MEM   PL_opsave = op
178 #define OP_MEM_TO_REG   op = PL_opsave
179 #else
180 #define OP_REG_TO_MEM   NOOP
181 #define OP_MEM_TO_REG   NOOP
182 #endif
183
184 /*
185  * These exception-handling macros are split up to
186  * ease integration with C++ exceptions.
187  *
188  * To use C++ try+catch to catch Perl exceptions, an extension author
189  * needs to first write an extern "C" function to throw an appropriate
190  * exception object; typically it will be or contain an integer,
191  * because Perl's internals use integers to track exception types:
192  *    extern "C" { static void thrower(int i) { throw i; } }
193  *
194  * Then (as shown below) the author needs to use, not the simple
195  * JMPENV_PUSH, but several of its constitutent macros, to arrange for
196  * the Perl internals to call thrower() rather than longjmp() to
197  * report exceptions:
198  *
199  *    dJMPENV;
200  *    JMPENV_PUSH_INIT(thrower);
201  *    try {
202  *        ... stuff that may throw exceptions ...
203  *    }
204  *    catch (int why) {  // or whatever matches thrower()
205  *        JMPENV_POST_CATCH;
206  *        EXCEPT_SET(why);
207  *        switch (why) {
208  *          ... // handle various Perl exception codes
209  *        }
210  *    }
211  *    JMPENV_POP;  // don't forget this!
212  */
213
214 #define dJMPENV         JMPENV cur_env
215
216 #define JMPENV_PUSH_INIT_ENV(cur_env,THROWFUNC) \
217     STMT_START {                                        \
218         cur_env.je_throw = (THROWFUNC);                 \
219         cur_env.je_ret = -1;                            \
220         cur_env.je_mustcatch = FALSE;                   \
221         cur_env.je_prev = PL_top_env;                   \
222         PL_top_env = &cur_env;                          \
223         OP_REG_TO_MEM;                                  \
224     } STMT_END
225
226 #define JMPENV_PUSH_INIT(THROWFUNC) JMPENV_PUSH_INIT_ENV(cur_env,THROWFUNC) 
227
228 #define JMPENV_POST_CATCH_ENV(cur_env) \
229     STMT_START {                                        \
230         OP_MEM_TO_REG;                                  \
231         PL_top_env = &cur_env;                          \
232     } STMT_END
233
234 #define JMPENV_POST_CATCH JMPENV_POST_CATCH_ENV(cur_env)
235
236
237 #define JMPENV_PUSH_ENV(cur_env,v) \
238     STMT_START {                                        \
239         JMPENV_PUSH_INIT_ENV(cur_env,NULL);                             \
240         EXCEPT_SET_ENV(cur_env,PerlProc_setjmp(cur_env.je_buf, 1));     \
241         JMPENV_POST_CATCH_ENV(cur_env);                         \
242         (v) = EXCEPT_GET_ENV(cur_env);                          \
243     } STMT_END
244
245 #define JMPENV_PUSH(v) JMPENV_PUSH_ENV(cur_env,v) 
246
247 #define JMPENV_POP_ENV(cur_env) \
248     STMT_START { PL_top_env = cur_env.je_prev; } STMT_END
249
250 #define JMPENV_POP  JMPENV_POP_ENV(cur_env) 
251
252 #define JMPENV_JUMP(v) \
253     STMT_START {                                                \
254         OP_REG_TO_MEM;                                          \
255         if (PL_top_env->je_prev) {                              \
256             if (PL_top_env->je_throw)                           \
257                 PL_top_env->je_throw(v);                        \
258             else                                                \
259                 PerlProc_longjmp(PL_top_env->je_buf, (v));      \
260         }                                                       \
261         if ((v) == 2)                                           \
262             PerlProc_exit(STATUS_NATIVE_EXPORT);                \
263         PerlIO_printf(Perl_error_log, "panic: top_env\n");      \
264         PerlProc_exit(1);                                       \
265     } STMT_END
266
267 #define EXCEPT_GET_ENV(cur_env) (cur_env.je_ret)
268 #define EXCEPT_GET EXCEPT_GET_ENV(cur_env)
269 #define EXCEPT_SET_ENV(cur_env,v)       (cur_env.je_ret = (v))
270 #define EXCEPT_SET(v) EXCEPT_SET_ENV(cur_env,v)
271
272 #define CATCH_GET       (PL_top_env->je_mustcatch)
273 #define CATCH_SET(v)    (PL_top_env->je_mustcatch = (v))
274