This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Export the SOCKS workaround symbols only if necessary.
[perl5.git] / scope.h
CommitLineData
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
79072805 36
3280af22
NIS
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))
c76ac1ee 43#define SSPUSHDXPTR(p) (PL_savestack[PL_savestack_ix++].any_dxptr = (p))
3280af22
NIS
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)
c76ac1ee 49#define SSPOPDXPTR (PL_savestack[--PL_savestack_ix].any_dxptr)
8990e307 50
954c1994
GS
51/*
52=for apidoc Ams||SAVETMPS
53Opening bracket for temporaries on a callback. See C<FREETMPS> and
54L<perlcall>.
55
56=for apidoc Ams||FREETMPS
57Closing bracket for temporaries on a callback. See C<SAVETMPS> and
58L<perlcall>.
59
60=for apidoc Ams||ENTER
61Opening bracket on a callback. See C<LEAVE> and L<perlcall>.
62
63=for apidoc Ams||LEAVE
64Closing bracket on a callback. See C<ENTER> and L<perlcall>.
65
66=cut
67*/
68
3280af22
NIS
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()
a0d0e21e 71
f46d017c
GS
72#ifdef DEBUGGING
73#define ENTER \
74 STMT_START { \
75 push_scope(); \
cea2e8a9 76 DEBUG_l(WITH_THR(Perl_deb(aTHX_ "ENTER scope %ld at %s:%d\n", \
3280af22 77 PL_scopestack_ix, __FILE__, __LINE__))); \
f46d017c
GS
78 } STMT_END
79#define LEAVE \
80 STMT_START { \
cea2e8a9 81 DEBUG_l(WITH_THR(Perl_deb(aTHX_ "LEAVE scope %ld at %s:%d\n", \
3280af22 82 PL_scopestack_ix, __FILE__, __LINE__))); \
f46d017c
GS
83 pop_scope(); \
84 } STMT_END
85#else
a0d0e21e
LW
86#define ENTER push_scope()
87#define LEAVE pop_scope()
f46d017c 88#endif
3280af22 89#define LEAVE_SCOPE(old) if (PL_savestack_ix > old) leave_scope(old)
a0d0e21e 90
55497cff 91/*
b9d12d37 92 * Not using SOFT_CAST on SAVESPTR, SAVEGENERICSV and SAVEFREESV
55497cff
PP
93 * because these are used for several kinds of pointer values
94 */
e8347627 95#define SAVEI8(i) save_I8(SOFT_CAST(I8*)&(i))
4fdae800
PP
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))
55497cff
PP
101#define SAVESPTR(s) save_sptr((SV**)&(s))
102#define SAVEPPTR(s) save_pptr(SOFT_CAST(char**)&(s))
57b2e452 103#define SAVEVPTR(s) save_vptr((void*)&(s))
55497cff
PP
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))
b9d12d37 108#define SAVEGENERICSV(s) save_generic_svref((SV**)&(s))
f4dd75d9 109#define SAVEGENERICPV(s) save_generic_pvref((char**)&(s))
55497cff
PP
110#define SAVEDELETE(h,k,l) \
111 save_delete(SOFT_CAST(HV*)(h), SOFT_CAST(char*)(k), (I32)(l))
112#define SAVEDESTRUCTOR(f,p) \
c76ac1ee
GS
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))
25eaa213
GS
117
118#define SAVESTACK_POS() \
119 STMT_START { \
120 SSCHECK(2); \
3280af22 121 SSPUSHINT(PL_stack_sp - PL_stack_base); \
25eaa213
GS
122 SSPUSHINT(SAVEt_STACK_POS); \
123 } STMT_END
124
462e5cf6 125#define SAVEOP() save_op()
25eaa213
GS
126
127#define SAVEHINTS() \
128 STMT_START { \
3280af22 129 if (PL_hints & HINT_LOCALIZE_HH) \
25eaa213
GS
130 save_hints(); \
131 else { \
132 SSCHECK(2); \
3280af22 133 SSPUSHINT(PL_hints); \
25eaa213
GS
134 SSPUSHINT(SAVEt_HINTS); \
135 } \
136 } STMT_END
354992b1
GS
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
a9332b4a 150
57843af0 151#ifdef USE_ITHREADS
f4dd75d9
GS
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))
57843af0 156#else
f4dd75d9
GS
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))
57843af0
GS
161#endif
162
f4dd75d9 163#define SAVECOPLINE(c) SAVEI16(CopLINE(c))
57843af0 164
455ece5e
AD
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
02db2b7b
IZ
176#define SSNEW(size) save_alloc(aTHX_ (size), 0)
177#define SSNEWt(n,t) SSNEW((n)*sizeof(t))
178#define SSNEWa(size,align) save_alloc(aTHX_ (size), \
455ece5e 179 (align - ((int)((caddr_t)&PL_savestack[PL_savestack_ix]) % align)) % align)
02db2b7b 180#define SSNEWat(n,t,align) SSNEWa((n)*sizeof(t), align)
455ece5e 181
02db2b7b
IZ
182#define SSPTR(off,type) ((type) ((char*)PL_savestack + off))
183#define SSPTRt(off,type) ((type*) ((char*)PL_savestack + off))
455ece5e 184
54310121
PP
185/* A jmpenv packages the state required to perform a proper non-local jump.
186 * Note that there is a start_env initialized when perl starts, and top_env
187 * points to this initially, so top_env should always be non-null.
188 *
189 * Existence of a non-null top_env->je_prev implies it is valid to call
6224f72b
GS
190 * longjmp() at that runlevel (we make sure start_env.je_prev is always
191 * null to ensure this).
54310121
PP
192 *
193 * je_mustcatch, when set at any runlevel to TRUE, means eval ops must
194 * establish a local jmpenv to handle exception traps. Care must be taken
195 * to restore the previous value of je_mustcatch before exiting the
196 * stack frame iff JMPENV_PUSH was not called in that stack frame.
6224f72b 197 * GSAR 97-03-27
54310121
PP
198 */
199
200struct jmpenv {
201 struct jmpenv * je_prev;
312caa8e
CS
202 Sigjmp_buf je_buf; /* only for use if !je_throw */
203 int je_ret; /* last exception thrown */
204 bool je_mustcatch; /* need to call longjmp()? */
14dd3ad8 205#ifdef PERL_FLEXIBLE_EXCEPTIONS
312caa8e 206 void (*je_throw)(int v); /* last for bincompat */
db36c5a1 207 bool je_noset; /* no need for setjmp() */
14dd3ad8 208#endif
1163b5c4 209};
1163b5c4 210
6224f72b 211typedef struct jmpenv JMPENV;
1163b5c4 212
14dd3ad8
GS
213#ifdef OP_IN_REGISTER
214#define OP_REG_TO_MEM PL_opsave = op
215#define OP_MEM_TO_REG op = PL_opsave
216#else
217#define OP_REG_TO_MEM NOOP
218#define OP_MEM_TO_REG NOOP
219#endif
312caa8e
CS
220
221/*
222 * How to build the first jmpenv.
223 *
224 * top_env needs to be non-zero. It points to an area
225 * in which longjmp() stuff is stored, as C callstack
226 * info there at least is thread specific this has to
227 * be per-thread. Otherwise a 'die' in a thread gives
228 * that thread the C stack of last thread to do an eval {}!
229 */
230
231#define JMPENV_BOOTSTRAP \
232 STMT_START { \
14dd3ad8 233 Zero(&PL_start_env, 1, JMPENV); \
312caa8e
CS
234 PL_start_env.je_ret = -1; \
235 PL_start_env.je_mustcatch = TRUE; \
236 PL_top_env = &PL_start_env; \
237 } STMT_END
238
14dd3ad8 239#ifdef PERL_FLEXIBLE_EXCEPTIONS
462e5cf6 240
312caa8e
CS
241/*
242 * These exception-handling macros are split up to
243 * ease integration with C++ exceptions.
244 *
245 * To use C++ try+catch to catch Perl exceptions, an extension author
246 * needs to first write an extern "C" function to throw an appropriate
247 * exception object; typically it will be or contain an integer,
248 * because Perl's internals use integers to track exception types:
249 * extern "C" { static void thrower(int i) { throw i; } }
250 *
251 * Then (as shown below) the author needs to use, not the simple
252 * JMPENV_PUSH, but several of its constitutent macros, to arrange for
253 * the Perl internals to call thrower() rather than longjmp() to
254 * report exceptions:
255 *
256 * dJMPENV;
257 * JMPENV_PUSH_INIT(thrower);
258 * try {
259 * ... stuff that may throw exceptions ...
260 * }
261 * catch (int why) { // or whatever matches thrower()
262 * JMPENV_POST_CATCH;
263 * EXCEPT_SET(why);
264 * switch (why) {
265 * ... // handle various Perl exception codes
266 * }
267 * }
268 * JMPENV_POP; // don't forget this!
269 */
270
14dd3ad8
GS
271/*
272 * Function that catches/throws, and its callback for the
273 * body of protected processing.
274 */
275typedef void *(CPERLscope(*protect_body_t)) (pTHX_ va_list);
276typedef void *(CPERLscope(*protect_proc_t)) (pTHX_ volatile JMPENV *pcur_env,
277 int *, protect_body_t, ...);
278
db36c5a1
GS
279#define dJMPENV JMPENV cur_env; \
280 volatile JMPENV *pcur_env = ((cur_env.je_noset = 0),&cur_env)
312caa8e 281
db36c5a1 282#define JMPENV_PUSH_INIT_ENV(ce,THROWFUNC) \
6224f72b 283 STMT_START { \
db36c5a1
GS
284 (ce).je_throw = (THROWFUNC); \
285 (ce).je_ret = -1; \
286 (ce).je_mustcatch = FALSE; \
287 (ce).je_prev = PL_top_env; \
288 PL_top_env = &(ce); \
6224f72b 289 OP_REG_TO_MEM; \
312caa8e 290 } STMT_END
1d651c14 291
db36c5a1 292#define JMPENV_PUSH_INIT(THROWFUNC) JMPENV_PUSH_INIT_ENV(*(JMPENV*)pcur_env,THROWFUNC)
1d651c14 293
db36c5a1 294#define JMPENV_POST_CATCH_ENV(ce) \
312caa8e 295 STMT_START { \
6224f72b 296 OP_MEM_TO_REG; \
db36c5a1 297 PL_top_env = &(ce); \
6224f72b 298 } STMT_END
312caa8e 299
db36c5a1 300#define JMPENV_POST_CATCH JMPENV_POST_CATCH_ENV(*(JMPENV*)pcur_env)
1d651c14 301
db36c5a1
GS
302#define JMPENV_PUSH_ENV(ce,v) \
303 STMT_START { \
304 if (!(ce).je_noset) { \
14dd3ad8
GS
305 DEBUG_l(Perl_deb(aTHX_ "Setting up jumplevel %p, was %p\n", \
306 ce, PL_top_env)); \
db36c5a1
GS
307 JMPENV_PUSH_INIT_ENV(ce,NULL); \
308 EXCEPT_SET_ENV(ce,PerlProc_setjmp((ce).je_buf, 1));\
309 (ce).je_noset = 1; \
310 } \
311 else \
312 EXCEPT_SET_ENV(ce,0); \
313 JMPENV_POST_CATCH_ENV(ce); \
314 (v) = EXCEPT_GET_ENV(ce); \
312caa8e
CS
315 } STMT_END
316
db36c5a1 317#define JMPENV_PUSH(v) JMPENV_PUSH_ENV(*(JMPENV*)pcur_env,v)
1d651c14 318
db36c5a1 319#define JMPENV_POP_ENV(ce) \
14dd3ad8
GS
320 STMT_START { \
321 if (PL_top_env == &(ce)) \
322 PL_top_env = (ce).je_prev; \
323 } STMT_END
312caa8e 324
db36c5a1 325#define JMPENV_POP JMPENV_POP_ENV(*(JMPENV*)pcur_env)
1d651c14 326
22921e25
CS
327#define JMPENV_JUMP(v) \
328 STMT_START { \
462e5cf6 329 OP_REG_TO_MEM; \
312caa8e
CS
330 if (PL_top_env->je_prev) { \
331 if (PL_top_env->je_throw) \
332 PL_top_env->je_throw(v); \
333 else \
334 PerlProc_longjmp(PL_top_env->je_buf, (v)); \
335 } \
6224f72b 336 if ((v) == 2) \
312caa8e 337 PerlProc_exit(STATUS_NATIVE_EXPORT); \
bf49b057 338 PerlIO_printf(Perl_error_log, "panic: top_env\n"); \
312caa8e 339 PerlProc_exit(1); \
22921e25 340 } STMT_END
312caa8e 341
db36c5a1
GS
342#define EXCEPT_GET_ENV(ce) ((ce).je_ret)
343#define EXCEPT_GET EXCEPT_GET_ENV(*(JMPENV*)pcur_env)
344#define EXCEPT_SET_ENV(ce,v) ((ce).je_ret = (v))
345#define EXCEPT_SET(v) EXCEPT_SET_ENV(*(JMPENV*)pcur_env,v)
312caa8e 346
14dd3ad8
GS
347#else /* !PERL_FLEXIBLE_EXCEPTIONS */
348
349#define dJMPENV JMPENV cur_env
350
351#define JMPENV_PUSH(v) \
352 STMT_START { \
353 DEBUG_l(Perl_deb(aTHX_ "Setting up jumplevel %p, was %p\n", \
354 &cur_env, PL_top_env)); \
355 cur_env.je_prev = PL_top_env; \
356 OP_REG_TO_MEM; \
357 cur_env.je_ret = PerlProc_setjmp(cur_env.je_buf, 1); \
358 OP_MEM_TO_REG; \
359 PL_top_env = &cur_env; \
360 cur_env.je_mustcatch = FALSE; \
361 (v) = cur_env.je_ret; \
362 } STMT_END
363
364#define JMPENV_POP \
365 STMT_START { PL_top_env = cur_env.je_prev; } STMT_END
366
367#define JMPENV_JUMP(v) \
368 STMT_START { \
369 OP_REG_TO_MEM; \
370 if (PL_top_env->je_prev) \
371 PerlProc_longjmp(PL_top_env->je_buf, (v)); \
372 if ((v) == 2) \
373 PerlProc_exit(STATUS_NATIVE_EXPORT); \
374 PerlIO_printf(PerlIO_stderr(), "panic: top_env\n"); \
375 PerlProc_exit(1); \
376 } STMT_END
377
378#endif /* PERL_FLEXIBLE_EXCEPTIONS */
379
db36c5a1
GS
380#define CATCH_GET (PL_top_env->je_mustcatch)
381#define CATCH_SET(v) (PL_top_env->je_mustcatch = (v))