Commit | Line | Data |
---|---|---|
b9d12d37 GS |
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 | |
c76ac1ee | 31 | #define SAVEt_DESTRUCTOR_X 30 |
7766f137 | 32 | #define SAVEt_VPTR 31 |
e8347627 | 33 | #define SAVEt_I8 32 |
354992b1 | 34 | #define SAVEt_COMPPAD 33 |
f4dd75d9 | 35 | #define SAVEt_GENERIC_PVREF 34 |
ccaecade | 36 | #define SAVEt_PADSV 35 |
79072805 | 37 | |
3280af22 NIS |
38 | #define SSCHECK(need) if (PL_savestack_ix + need > PL_savestack_max) savestack_grow() |
39 | #define SSPUSHINT(i) (PL_savestack[PL_savestack_ix++].any_i32 = (I32)(i)) | |
40 | #define SSPUSHLONG(i) (PL_savestack[PL_savestack_ix++].any_long = (long)(i)) | |
41 | #define SSPUSHIV(i) (PL_savestack[PL_savestack_ix++].any_iv = (IV)(i)) | |
42 | #define SSPUSHPTR(p) (PL_savestack[PL_savestack_ix++].any_ptr = (void*)(p)) | |
43 | #define SSPUSHDPTR(p) (PL_savestack[PL_savestack_ix++].any_dptr = (p)) | |
c76ac1ee | 44 | #define SSPUSHDXPTR(p) (PL_savestack[PL_savestack_ix++].any_dxptr = (p)) |
3280af22 NIS |
45 | #define SSPOPINT (PL_savestack[--PL_savestack_ix].any_i32) |
46 | #define SSPOPLONG (PL_savestack[--PL_savestack_ix].any_long) | |
47 | #define SSPOPIV (PL_savestack[--PL_savestack_ix].any_iv) | |
48 | #define SSPOPPTR (PL_savestack[--PL_savestack_ix].any_ptr) | |
49 | #define SSPOPDPTR (PL_savestack[--PL_savestack_ix].any_dptr) | |
c76ac1ee | 50 | #define SSPOPDXPTR (PL_savestack[--PL_savestack_ix].any_dxptr) |
8990e307 | 51 | |
954c1994 GS |
52 | /* |
53 | =for apidoc Ams||SAVETMPS | |
54 | Opening bracket for temporaries on a callback. See C<FREETMPS> and | |
55 | L<perlcall>. | |
56 | ||
57 | =for apidoc Ams||FREETMPS | |
58 | Closing bracket for temporaries on a callback. See C<SAVETMPS> and | |
59 | L<perlcall>. | |
60 | ||
61 | =for apidoc Ams||ENTER | |
62 | Opening bracket on a callback. See C<LEAVE> and L<perlcall>. | |
63 | ||
64 | =for apidoc Ams||LEAVE | |
65 | Closing bracket on a callback. See C<ENTER> and L<perlcall>. | |
66 | ||
67 | =cut | |
68 | */ | |
69 | ||
3280af22 NIS |
70 | #define SAVETMPS save_int((int*)&PL_tmps_floor), PL_tmps_floor = PL_tmps_ix |
71 | #define FREETMPS if (PL_tmps_ix > PL_tmps_floor) free_tmps() | |
a0d0e21e | 72 | |
f46d017c GS |
73 | #ifdef DEBUGGING |
74 | #define ENTER \ | |
75 | STMT_START { \ | |
76 | push_scope(); \ | |
cea2e8a9 | 77 | DEBUG_l(WITH_THR(Perl_deb(aTHX_ "ENTER scope %ld at %s:%d\n", \ |
3280af22 | 78 | PL_scopestack_ix, __FILE__, __LINE__))); \ |
f46d017c GS |
79 | } STMT_END |
80 | #define LEAVE \ | |
81 | STMT_START { \ | |
cea2e8a9 | 82 | DEBUG_l(WITH_THR(Perl_deb(aTHX_ "LEAVE scope %ld at %s:%d\n", \ |
3280af22 | 83 | PL_scopestack_ix, __FILE__, __LINE__))); \ |
f46d017c GS |
84 | pop_scope(); \ |
85 | } STMT_END | |
86 | #else | |
a0d0e21e LW |
87 | #define ENTER push_scope() |
88 | #define LEAVE pop_scope() | |
f46d017c | 89 | #endif |
3280af22 | 90 | #define LEAVE_SCOPE(old) if (PL_savestack_ix > old) leave_scope(old) |
a0d0e21e | 91 | |
55497cff | 92 | /* |
b9d12d37 | 93 | * Not using SOFT_CAST on SAVESPTR, SAVEGENERICSV and SAVEFREESV |
55497cff | 94 | * because these are used for several kinds of pointer values |
95 | */ | |
e8347627 | 96 | #define SAVEI8(i) save_I8(SOFT_CAST(I8*)&(i)) |
4fdae800 | 97 | #define SAVEI16(i) save_I16(SOFT_CAST(I16*)&(i)) |
98 | #define SAVEI32(i) save_I32(SOFT_CAST(I32*)&(i)) | |
99 | #define SAVEINT(i) save_int(SOFT_CAST(int*)&(i)) | |
100 | #define SAVEIV(i) save_iv(SOFT_CAST(IV*)&(i)) | |
101 | #define SAVELONG(l) save_long(SOFT_CAST(long*)&(l)) | |
55497cff | 102 | #define SAVESPTR(s) save_sptr((SV**)&(s)) |
103 | #define SAVEPPTR(s) save_pptr(SOFT_CAST(char**)&(s)) | |
57b2e452 | 104 | #define SAVEVPTR(s) save_vptr((void*)&(s)) |
ccaecade | 105 | #define SAVEPADSV(s) save_padsv(s) |
55497cff | 106 | #define SAVEFREESV(s) save_freesv((SV*)(s)) |
107 | #define SAVEFREEOP(o) save_freeop(SOFT_CAST(OP*)(o)) | |
108 | #define SAVEFREEPV(p) save_freepv(SOFT_CAST(char*)(p)) | |
109 | #define SAVECLEARSV(sv) save_clearsv(SOFT_CAST(SV**)&(sv)) | |
b9d12d37 | 110 | #define SAVEGENERICSV(s) save_generic_svref((SV**)&(s)) |
f4dd75d9 | 111 | #define SAVEGENERICPV(s) save_generic_pvref((char**)&(s)) |
55497cff | 112 | #define SAVEDELETE(h,k,l) \ |
113 | save_delete(SOFT_CAST(HV*)(h), SOFT_CAST(char*)(k), (I32)(l)) | |
114 | #define SAVEDESTRUCTOR(f,p) \ | |
c76ac1ee GS |
115 | save_destructor((DESTRUCTORFUNC_NOCONTEXT_t)(f), SOFT_CAST(void*)(p)) |
116 | ||
117 | #define SAVEDESTRUCTOR_X(f,p) \ | |
118 | save_destructor_x((DESTRUCTORFUNC_t)(f), SOFT_CAST(void*)(p)) | |
25eaa213 GS |
119 | |
120 | #define SAVESTACK_POS() \ | |
121 | STMT_START { \ | |
122 | SSCHECK(2); \ | |
3280af22 | 123 | SSPUSHINT(PL_stack_sp - PL_stack_base); \ |
25eaa213 GS |
124 | SSPUSHINT(SAVEt_STACK_POS); \ |
125 | } STMT_END | |
126 | ||
462e5cf6 | 127 | #define SAVEOP() save_op() |
25eaa213 GS |
128 | |
129 | #define SAVEHINTS() \ | |
130 | STMT_START { \ | |
3280af22 | 131 | if (PL_hints & HINT_LOCALIZE_HH) \ |
25eaa213 GS |
132 | save_hints(); \ |
133 | else { \ | |
134 | SSCHECK(2); \ | |
3280af22 | 135 | SSPUSHINT(PL_hints); \ |
25eaa213 GS |
136 | SSPUSHINT(SAVEt_HINTS); \ |
137 | } \ | |
138 | } STMT_END | |
354992b1 GS |
139 | |
140 | #define SAVECOMPPAD() \ | |
141 | STMT_START { \ | |
142 | if (PL_comppad && PL_curpad == AvARRAY(PL_comppad)) { \ | |
143 | SSCHECK(2); \ | |
144 | SSPUSHPTR((SV*)PL_comppad); \ | |
145 | SSPUSHINT(SAVEt_COMPPAD); \ | |
146 | } \ | |
147 | else { \ | |
148 | SAVEVPTR(PL_curpad); \ | |
149 | SAVESPTR(PL_comppad); \ | |
150 | } \ | |
151 | } STMT_END | |
a9332b4a | 152 | |
57843af0 | 153 | #ifdef USE_ITHREADS |
f4dd75d9 GS |
154 | # define SAVECOPSTASH(c) SAVEPPTR(CopSTASHPV(c)) |
155 | # define SAVECOPSTASH_FREE(c) SAVEGENERICPV(CopSTASHPV(c)) | |
156 | # define SAVECOPFILE(c) SAVEPPTR(CopFILE(c)) | |
157 | # define SAVECOPFILE_FREE(c) SAVEGENERICPV(CopFILE(c)) | |
57843af0 | 158 | #else |
f4dd75d9 GS |
159 | # define SAVECOPSTASH(c) SAVESPTR(CopSTASH(c)) |
160 | # define SAVECOPSTASH_FREE(c) SAVECOPSTASH(c) /* XXX not refcounted */ | |
161 | # define SAVECOPFILE(c) SAVESPTR(CopFILEGV(c)) | |
162 | # define SAVECOPFILE_FREE(c) SAVEGENERICSV(CopFILEGV(c)) | |
57843af0 GS |
163 | #endif |
164 | ||
f4dd75d9 | 165 | #define SAVECOPLINE(c) SAVEI16(CopLINE(c)) |
57843af0 | 166 | |
455ece5e AD |
167 | /* SSNEW() temporarily allocates a specified number of bytes of data on the |
168 | * savestack. It returns an integer index into the savestack, because a | |
169 | * pointer would get broken if the savestack is moved on reallocation. | |
170 | * SSNEWa() works like SSNEW(), but also aligns the data to the specified | |
171 | * number of bytes. MEM_ALIGNBYTES is perhaps the most useful. The | |
172 | * alignment will be preserved therough savestack reallocation *only* if | |
173 | * realloc returns data aligned to a size divisible by `align'! | |
174 | * | |
175 | * SSPTR() converts the index returned by SSNEW/SSNEWa() into a pointer. | |
176 | */ | |
177 | ||
178 | #define SSNEW(size) save_alloc(size, 0) | |
179 | #define SSNEWa(size,align) save_alloc(size, \ | |
180 | (align - ((int)((caddr_t)&PL_savestack[PL_savestack_ix]) % align)) % align) | |
181 | ||
182 | #define SSPTR(off,type) ((type) ((char*)PL_savestack + off)) | |
183 | ||
54310121 | 184 | /* A jmpenv packages the state required to perform a proper non-local jump. |
185 | * Note that there is a start_env initialized when perl starts, and top_env | |
186 | * points to this initially, so top_env should always be non-null. | |
187 | * | |
188 | * Existence of a non-null top_env->je_prev implies it is valid to call | |
6224f72b GS |
189 | * longjmp() at that runlevel (we make sure start_env.je_prev is always |
190 | * null to ensure this). | |
54310121 | 191 | * |
192 | * je_mustcatch, when set at any runlevel to TRUE, means eval ops must | |
193 | * establish a local jmpenv to handle exception traps. Care must be taken | |
194 | * to restore the previous value of je_mustcatch before exiting the | |
195 | * stack frame iff JMPENV_PUSH was not called in that stack frame. | |
6224f72b | 196 | * GSAR 97-03-27 |
54310121 | 197 | */ |
198 | ||
199 | struct jmpenv { | |
200 | struct jmpenv * je_prev; | |
312caa8e CS |
201 | Sigjmp_buf je_buf; /* only for use if !je_throw */ |
202 | int je_ret; /* last exception thrown */ | |
203 | bool je_mustcatch; /* need to call longjmp()? */ | |
14dd3ad8 | 204 | #ifdef PERL_FLEXIBLE_EXCEPTIONS |
312caa8e | 205 | void (*je_throw)(int v); /* last for bincompat */ |
db36c5a1 | 206 | bool je_noset; /* no need for setjmp() */ |
14dd3ad8 | 207 | #endif |
1163b5c4 | 208 | }; |
1163b5c4 | 209 | |
6224f72b | 210 | typedef struct jmpenv JMPENV; |
1163b5c4 | 211 | |
14dd3ad8 GS |
212 | #ifdef OP_IN_REGISTER |
213 | #define OP_REG_TO_MEM PL_opsave = op | |
214 | #define OP_MEM_TO_REG op = PL_opsave | |
215 | #else | |
216 | #define OP_REG_TO_MEM NOOP | |
217 | #define OP_MEM_TO_REG NOOP | |
218 | #endif | |
312caa8e CS |
219 | |
220 | /* | |
221 | * How to build the first jmpenv. | |
222 | * | |
223 | * top_env needs to be non-zero. It points to an area | |
224 | * in which longjmp() stuff is stored, as C callstack | |
225 | * info there at least is thread specific this has to | |
226 | * be per-thread. Otherwise a 'die' in a thread gives | |
227 | * that thread the C stack of last thread to do an eval {}! | |
228 | */ | |
229 | ||
230 | #define JMPENV_BOOTSTRAP \ | |
231 | STMT_START { \ | |
14dd3ad8 | 232 | Zero(&PL_start_env, 1, JMPENV); \ |
312caa8e CS |
233 | PL_start_env.je_ret = -1; \ |
234 | PL_start_env.je_mustcatch = TRUE; \ | |
235 | PL_top_env = &PL_start_env; \ | |
236 | } STMT_END | |
237 | ||
14dd3ad8 | 238 | #ifdef PERL_FLEXIBLE_EXCEPTIONS |
462e5cf6 | 239 | |
312caa8e CS |
240 | /* |
241 | * These exception-handling macros are split up to | |
242 | * ease integration with C++ exceptions. | |
243 | * | |
244 | * To use C++ try+catch to catch Perl exceptions, an extension author | |
245 | * needs to first write an extern "C" function to throw an appropriate | |
246 | * exception object; typically it will be or contain an integer, | |
247 | * because Perl's internals use integers to track exception types: | |
248 | * extern "C" { static void thrower(int i) { throw i; } } | |
249 | * | |
250 | * Then (as shown below) the author needs to use, not the simple | |
251 | * JMPENV_PUSH, but several of its constitutent macros, to arrange for | |
252 | * the Perl internals to call thrower() rather than longjmp() to | |
253 | * report exceptions: | |
254 | * | |
255 | * dJMPENV; | |
256 | * JMPENV_PUSH_INIT(thrower); | |
257 | * try { | |
258 | * ... stuff that may throw exceptions ... | |
259 | * } | |
260 | * catch (int why) { // or whatever matches thrower() | |
261 | * JMPENV_POST_CATCH; | |
262 | * EXCEPT_SET(why); | |
263 | * switch (why) { | |
264 | * ... // handle various Perl exception codes | |
265 | * } | |
266 | * } | |
267 | * JMPENV_POP; // don't forget this! | |
268 | */ | |
269 | ||
14dd3ad8 GS |
270 | /* |
271 | * Function that catches/throws, and its callback for the | |
272 | * body of protected processing. | |
273 | */ | |
274 | typedef void *(CPERLscope(*protect_body_t)) (pTHX_ va_list); | |
275 | typedef void *(CPERLscope(*protect_proc_t)) (pTHX_ volatile JMPENV *pcur_env, | |
276 | int *, protect_body_t, ...); | |
277 | ||
db36c5a1 GS |
278 | #define dJMPENV JMPENV cur_env; \ |
279 | volatile JMPENV *pcur_env = ((cur_env.je_noset = 0),&cur_env) | |
312caa8e | 280 | |
db36c5a1 | 281 | #define JMPENV_PUSH_INIT_ENV(ce,THROWFUNC) \ |
6224f72b | 282 | STMT_START { \ |
db36c5a1 GS |
283 | (ce).je_throw = (THROWFUNC); \ |
284 | (ce).je_ret = -1; \ | |
285 | (ce).je_mustcatch = FALSE; \ | |
286 | (ce).je_prev = PL_top_env; \ | |
287 | PL_top_env = &(ce); \ | |
6224f72b | 288 | OP_REG_TO_MEM; \ |
312caa8e | 289 | } STMT_END |
1d651c14 | 290 | |
db36c5a1 | 291 | #define JMPENV_PUSH_INIT(THROWFUNC) JMPENV_PUSH_INIT_ENV(*(JMPENV*)pcur_env,THROWFUNC) |
1d651c14 | 292 | |
db36c5a1 | 293 | #define JMPENV_POST_CATCH_ENV(ce) \ |
312caa8e | 294 | STMT_START { \ |
6224f72b | 295 | OP_MEM_TO_REG; \ |
db36c5a1 | 296 | PL_top_env = &(ce); \ |
6224f72b | 297 | } STMT_END |
312caa8e | 298 | |
db36c5a1 | 299 | #define JMPENV_POST_CATCH JMPENV_POST_CATCH_ENV(*(JMPENV*)pcur_env) |
1d651c14 | 300 | |
db36c5a1 GS |
301 | #define JMPENV_PUSH_ENV(ce,v) \ |
302 | STMT_START { \ | |
303 | if (!(ce).je_noset) { \ | |
14dd3ad8 GS |
304 | DEBUG_l(Perl_deb(aTHX_ "Setting up jumplevel %p, was %p\n", \ |
305 | ce, PL_top_env)); \ | |
db36c5a1 GS |
306 | JMPENV_PUSH_INIT_ENV(ce,NULL); \ |
307 | EXCEPT_SET_ENV(ce,PerlProc_setjmp((ce).je_buf, 1));\ | |
308 | (ce).je_noset = 1; \ | |
309 | } \ | |
310 | else \ | |
311 | EXCEPT_SET_ENV(ce,0); \ | |
312 | JMPENV_POST_CATCH_ENV(ce); \ | |
313 | (v) = EXCEPT_GET_ENV(ce); \ | |
312caa8e CS |
314 | } STMT_END |
315 | ||
db36c5a1 | 316 | #define JMPENV_PUSH(v) JMPENV_PUSH_ENV(*(JMPENV*)pcur_env,v) |
1d651c14 | 317 | |
db36c5a1 | 318 | #define JMPENV_POP_ENV(ce) \ |
14dd3ad8 GS |
319 | STMT_START { \ |
320 | if (PL_top_env == &(ce)) \ | |
321 | PL_top_env = (ce).je_prev; \ | |
322 | } STMT_END | |
312caa8e | 323 | |
db36c5a1 | 324 | #define JMPENV_POP JMPENV_POP_ENV(*(JMPENV*)pcur_env) |
1d651c14 | 325 | |
22921e25 CS |
326 | #define JMPENV_JUMP(v) \ |
327 | STMT_START { \ | |
462e5cf6 | 328 | OP_REG_TO_MEM; \ |
312caa8e CS |
329 | if (PL_top_env->je_prev) { \ |
330 | if (PL_top_env->je_throw) \ | |
331 | PL_top_env->je_throw(v); \ | |
332 | else \ | |
333 | PerlProc_longjmp(PL_top_env->je_buf, (v)); \ | |
334 | } \ | |
6224f72b | 335 | if ((v) == 2) \ |
312caa8e | 336 | PerlProc_exit(STATUS_NATIVE_EXPORT); \ |
bf49b057 | 337 | PerlIO_printf(Perl_error_log, "panic: top_env\n"); \ |
312caa8e | 338 | PerlProc_exit(1); \ |
22921e25 | 339 | } STMT_END |
312caa8e | 340 | |
db36c5a1 GS |
341 | #define EXCEPT_GET_ENV(ce) ((ce).je_ret) |
342 | #define EXCEPT_GET EXCEPT_GET_ENV(*(JMPENV*)pcur_env) | |
343 | #define EXCEPT_SET_ENV(ce,v) ((ce).je_ret = (v)) | |
344 | #define EXCEPT_SET(v) EXCEPT_SET_ENV(*(JMPENV*)pcur_env,v) | |
312caa8e | 345 | |
14dd3ad8 GS |
346 | #else /* !PERL_FLEXIBLE_EXCEPTIONS */ |
347 | ||
348 | #define dJMPENV JMPENV cur_env | |
349 | ||
350 | #define JMPENV_PUSH(v) \ | |
351 | STMT_START { \ | |
352 | DEBUG_l(Perl_deb(aTHX_ "Setting up jumplevel %p, was %p\n", \ | |
353 | &cur_env, PL_top_env)); \ | |
354 | cur_env.je_prev = PL_top_env; \ | |
355 | OP_REG_TO_MEM; \ | |
356 | cur_env.je_ret = PerlProc_setjmp(cur_env.je_buf, 1); \ | |
357 | OP_MEM_TO_REG; \ | |
358 | PL_top_env = &cur_env; \ | |
359 | cur_env.je_mustcatch = FALSE; \ | |
360 | (v) = cur_env.je_ret; \ | |
361 | } STMT_END | |
362 | ||
363 | #define JMPENV_POP \ | |
364 | STMT_START { PL_top_env = cur_env.je_prev; } STMT_END | |
365 | ||
366 | #define JMPENV_JUMP(v) \ | |
367 | STMT_START { \ | |
368 | OP_REG_TO_MEM; \ | |
369 | if (PL_top_env->je_prev) \ | |
370 | PerlProc_longjmp(PL_top_env->je_buf, (v)); \ | |
371 | if ((v) == 2) \ | |
372 | PerlProc_exit(STATUS_NATIVE_EXPORT); \ | |
373 | PerlIO_printf(PerlIO_stderr(), "panic: top_env\n"); \ | |
374 | PerlProc_exit(1); \ | |
375 | } STMT_END | |
376 | ||
377 | #endif /* PERL_FLEXIBLE_EXCEPTIONS */ | |
378 | ||
db36c5a1 GS |
379 | #define CATCH_GET (PL_top_env->je_mustcatch) |
380 | #define CATCH_SET(v) (PL_top_env->je_mustcatch = (v)) |