Hash lookup of constant strings optimization:
[p5sagit/p5-mst-13.2.git] / scope.h
CommitLineData
b9d12d37 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 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 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 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 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 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 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 83 pop_scope(); \
84 } STMT_END
85#else
a0d0e21e 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 93 * because these are used for several kinds of pointer values
94 */
e8347627 95#define SAVEI8(i) save_I8(SOFT_CAST(I8*)&(i))
4fdae800 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 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 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 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 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 117
118#define SAVESTACK_POS() \
119 STMT_START { \
120 SSCHECK(2); \
3280af22 121 SSPUSHINT(PL_stack_sp - PL_stack_base); \
25eaa213 122 SSPUSHINT(SAVEt_STACK_POS); \
123 } STMT_END
124
462e5cf6 125#define SAVEOP() save_op()
25eaa213 126
127#define SAVEHINTS() \
128 STMT_START { \
3280af22 129 if (PL_hints & HINT_LOCALIZE_HH) \
25eaa213 130 save_hints(); \
131 else { \
132 SSCHECK(2); \
3280af22 133 SSPUSHINT(PL_hints); \
25eaa213 134 SSPUSHINT(SAVEt_HINTS); \
135 } \
136 } STMT_END
354992b1 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 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 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 161#endif
162
f4dd75d9 163#define SAVECOPLINE(c) SAVEI16(CopLINE(c))
57843af0 164
455ece5e 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
54310121 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
6224f72b 187 * longjmp() at that runlevel (we make sure start_env.je_prev is always
188 * null to ensure this).
54310121 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.
6224f72b 194 * GSAR 97-03-27
54310121 195 */
196
197struct jmpenv {
198 struct jmpenv * je_prev;
312caa8e 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()? */
14dd3ad8 202#ifdef PERL_FLEXIBLE_EXCEPTIONS
312caa8e 203 void (*je_throw)(int v); /* last for bincompat */
db36c5a1 204 bool je_noset; /* no need for setjmp() */
14dd3ad8 205#endif
1163b5c4 206};
1163b5c4 207
6224f72b 208typedef struct jmpenv JMPENV;
1163b5c4 209
14dd3ad8 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
312caa8e 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 { \
14dd3ad8 230 Zero(&PL_start_env, 1, JMPENV); \
312caa8e 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
14dd3ad8 236#ifdef PERL_FLEXIBLE_EXCEPTIONS
462e5cf6 237
312caa8e 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
14dd3ad8 268/*
269 * Function that catches/throws, and its callback for the
270 * body of protected processing.
271 */
272typedef void *(CPERLscope(*protect_body_t)) (pTHX_ va_list);
273typedef void *(CPERLscope(*protect_proc_t)) (pTHX_ volatile JMPENV *pcur_env,
274 int *, protect_body_t, ...);
275
db36c5a1 276#define dJMPENV JMPENV cur_env; \
277 volatile JMPENV *pcur_env = ((cur_env.je_noset = 0),&cur_env)
312caa8e 278
db36c5a1 279#define JMPENV_PUSH_INIT_ENV(ce,THROWFUNC) \
6224f72b 280 STMT_START { \
db36c5a1 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); \
6224f72b 286 OP_REG_TO_MEM; \
312caa8e 287 } STMT_END
1d651c14 288
db36c5a1 289#define JMPENV_PUSH_INIT(THROWFUNC) JMPENV_PUSH_INIT_ENV(*(JMPENV*)pcur_env,THROWFUNC)
1d651c14 290
db36c5a1 291#define JMPENV_POST_CATCH_ENV(ce) \
312caa8e 292 STMT_START { \
6224f72b 293 OP_MEM_TO_REG; \
db36c5a1 294 PL_top_env = &(ce); \
6224f72b 295 } STMT_END
312caa8e 296
db36c5a1 297#define JMPENV_POST_CATCH JMPENV_POST_CATCH_ENV(*(JMPENV*)pcur_env)
1d651c14 298
db36c5a1 299#define JMPENV_PUSH_ENV(ce,v) \
300 STMT_START { \
301 if (!(ce).je_noset) { \
14dd3ad8 302 DEBUG_l(Perl_deb(aTHX_ "Setting up jumplevel %p, was %p\n", \
303 ce, PL_top_env)); \
db36c5a1 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); \
312caa8e 312 } STMT_END
313
db36c5a1 314#define JMPENV_PUSH(v) JMPENV_PUSH_ENV(*(JMPENV*)pcur_env,v)
1d651c14 315
db36c5a1 316#define JMPENV_POP_ENV(ce) \
14dd3ad8 317 STMT_START { \
318 if (PL_top_env == &(ce)) \
319 PL_top_env = (ce).je_prev; \
320 } STMT_END
312caa8e 321
db36c5a1 322#define JMPENV_POP JMPENV_POP_ENV(*(JMPENV*)pcur_env)
1d651c14 323
22921e25 324#define JMPENV_JUMP(v) \
325 STMT_START { \
462e5cf6 326 OP_REG_TO_MEM; \
312caa8e 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 } \
6224f72b 333 if ((v) == 2) \
312caa8e 334 PerlProc_exit(STATUS_NATIVE_EXPORT); \
bf49b057 335 PerlIO_printf(Perl_error_log, "panic: top_env\n"); \
312caa8e 336 PerlProc_exit(1); \
22921e25 337 } STMT_END
312caa8e 338
db36c5a1 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)
312caa8e 343
14dd3ad8 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
db36c5a1 377#define CATCH_GET (PL_top_env->je_mustcatch)
378#define CATCH_SET(v) (PL_top_env->je_mustcatch = (v))