Retract the mergesort code, way too incompatible licensing
[perl.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)             save_alloc(size, 0)
177 #define SSNEWa(size,align)      save_alloc(size, \
178     (align - ((int)((caddr_t)&PL_savestack[PL_savestack_ix]) % align)) % align)
179
180 #define SSPTR(off,type)         ((type) ((char*)PL_savestack + off))
181
182 /* A jmpenv packages the state required to perform a proper non-local jump.
183  * Note that there is a start_env initialized when perl starts, and top_env
184  * points to this initially, so top_env should always be non-null.
185  *
186  * Existence of a non-null top_env->je_prev implies it is valid to call
187  * longjmp() at that runlevel (we make sure start_env.je_prev is always
188  * null to ensure this).
189  *
190  * je_mustcatch, when set at any runlevel to TRUE, means eval ops must
191  * establish a local jmpenv to handle exception traps.  Care must be taken
192  * to restore the previous value of je_mustcatch before exiting the
193  * stack frame iff JMPENV_PUSH was not called in that stack frame.
194  * GSAR 97-03-27
195  */
196
197 struct jmpenv {
198     struct jmpenv *     je_prev;
199     Sigjmp_buf          je_buf;         /* only for use if !je_throw */
200     int                 je_ret;         /* last exception thrown */
201     bool                je_mustcatch;   /* need to call longjmp()? */
202 #ifdef PERL_FLEXIBLE_EXCEPTIONS
203     void                (*je_throw)(int v); /* last for bincompat */
204     bool                je_noset;       /* no need for setjmp() */
205 #endif
206 };
207
208 typedef struct jmpenv JMPENV;
209
210 #ifdef OP_IN_REGISTER
211 #define OP_REG_TO_MEM   PL_opsave = op
212 #define OP_MEM_TO_REG   op = PL_opsave
213 #else
214 #define OP_REG_TO_MEM   NOOP
215 #define OP_MEM_TO_REG   NOOP
216 #endif
217
218 /*
219  * How to build the first jmpenv.
220  *
221  * top_env needs to be non-zero. It points to an area
222  * in which longjmp() stuff is stored, as C callstack
223  * info there at least is thread specific this has to
224  * be per-thread. Otherwise a 'die' in a thread gives
225  * that thread the C stack of last thread to do an eval {}!
226  */
227
228 #define JMPENV_BOOTSTRAP \
229     STMT_START {                                \
230         Zero(&PL_start_env, 1, JMPENV);         \
231         PL_start_env.je_ret = -1;               \
232         PL_start_env.je_mustcatch = TRUE;       \
233         PL_top_env = &PL_start_env;             \
234     } STMT_END
235
236 #ifdef PERL_FLEXIBLE_EXCEPTIONS
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 /*
269  * Function that catches/throws, and its callback for the
270  *  body of protected processing.
271  */
272 typedef void *(CPERLscope(*protect_body_t)) (pTHX_ va_list);
273 typedef void *(CPERLscope(*protect_proc_t)) (pTHX_ volatile JMPENV *pcur_env,
274                                              int *, protect_body_t, ...);
275
276 #define dJMPENV JMPENV cur_env; \
277                 volatile JMPENV *pcur_env = ((cur_env.je_noset = 0),&cur_env)
278
279 #define JMPENV_PUSH_INIT_ENV(ce,THROWFUNC) \
280     STMT_START {                                        \
281         (ce).je_throw = (THROWFUNC);                    \
282         (ce).je_ret = -1;                               \
283         (ce).je_mustcatch = FALSE;                      \
284         (ce).je_prev = PL_top_env;                      \
285         PL_top_env = &(ce);                             \
286         OP_REG_TO_MEM;                                  \
287     } STMT_END
288
289 #define JMPENV_PUSH_INIT(THROWFUNC) JMPENV_PUSH_INIT_ENV(*(JMPENV*)pcur_env,THROWFUNC) 
290
291 #define JMPENV_POST_CATCH_ENV(ce) \
292     STMT_START {                                        \
293         OP_MEM_TO_REG;                                  \
294         PL_top_env = &(ce);                             \
295     } STMT_END
296
297 #define JMPENV_POST_CATCH JMPENV_POST_CATCH_ENV(*(JMPENV*)pcur_env)
298
299 #define JMPENV_PUSH_ENV(ce,v) \
300     STMT_START {                                                \
301         if (!(ce).je_noset) {                                   \
302             DEBUG_l(Perl_deb(aTHX_ "Setting up jumplevel %p, was %p\n", \
303                              ce, PL_top_env));                  \
304             JMPENV_PUSH_INIT_ENV(ce,NULL);                      \
305             EXCEPT_SET_ENV(ce,PerlProc_setjmp((ce).je_buf, 1));\
306             (ce).je_noset = 1;                                  \
307         }                                                       \
308         else                                                    \
309             EXCEPT_SET_ENV(ce,0);                               \
310         JMPENV_POST_CATCH_ENV(ce);                              \
311         (v) = EXCEPT_GET_ENV(ce);                               \
312     } STMT_END
313
314 #define JMPENV_PUSH(v) JMPENV_PUSH_ENV(*(JMPENV*)pcur_env,v) 
315
316 #define JMPENV_POP_ENV(ce) \
317     STMT_START {                                                \
318         if (PL_top_env == &(ce))                                \
319             PL_top_env = (ce).je_prev;                          \
320     } STMT_END
321
322 #define JMPENV_POP  JMPENV_POP_ENV(*(JMPENV*)pcur_env) 
323
324 #define JMPENV_JUMP(v) \
325     STMT_START {                                                \
326         OP_REG_TO_MEM;                                          \
327         if (PL_top_env->je_prev) {                              \
328             if (PL_top_env->je_throw)                           \
329                 PL_top_env->je_throw(v);                        \
330             else                                                \
331                 PerlProc_longjmp(PL_top_env->je_buf, (v));      \
332         }                                                       \
333         if ((v) == 2)                                           \
334             PerlProc_exit(STATUS_NATIVE_EXPORT);                \
335         PerlIO_printf(Perl_error_log, "panic: top_env\n");      \
336         PerlProc_exit(1);                                       \
337     } STMT_END
338
339 #define EXCEPT_GET_ENV(ce)      ((ce).je_ret)
340 #define EXCEPT_GET              EXCEPT_GET_ENV(*(JMPENV*)pcur_env)
341 #define EXCEPT_SET_ENV(ce,v)    ((ce).je_ret = (v))
342 #define EXCEPT_SET(v)           EXCEPT_SET_ENV(*(JMPENV*)pcur_env,v)
343
344 #else /* !PERL_FLEXIBLE_EXCEPTIONS */
345
346 #define dJMPENV         JMPENV cur_env
347
348 #define JMPENV_PUSH(v) \
349     STMT_START {                                                        \
350         DEBUG_l(Perl_deb(aTHX_ "Setting up jumplevel %p, was %p\n",     \
351                          &cur_env, PL_top_env));                        \
352         cur_env.je_prev = PL_top_env;                                   \
353         OP_REG_TO_MEM;                                                  \
354         cur_env.je_ret = PerlProc_setjmp(cur_env.je_buf, 1);            \
355         OP_MEM_TO_REG;                                                  \
356         PL_top_env = &cur_env;                                          \
357         cur_env.je_mustcatch = FALSE;                                   \
358         (v) = cur_env.je_ret;                                           \
359     } STMT_END
360
361 #define JMPENV_POP \
362     STMT_START { PL_top_env = cur_env.je_prev; } STMT_END
363
364 #define JMPENV_JUMP(v) \
365     STMT_START {                                                \
366         OP_REG_TO_MEM;                                          \
367         if (PL_top_env->je_prev)                                \
368             PerlProc_longjmp(PL_top_env->je_buf, (v));          \
369         if ((v) == 2)                                           \
370             PerlProc_exit(STATUS_NATIVE_EXPORT);                \
371         PerlIO_printf(PerlIO_stderr(), "panic: top_env\n");     \
372         PerlProc_exit(1);                                       \
373     } STMT_END
374
375 #endif /* PERL_FLEXIBLE_EXCEPTIONS */
376
377 #define CATCH_GET               (PL_top_env->je_mustcatch)
378 #define CATCH_SET(v)            (PL_top_env->je_mustcatch = (v))