This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Revert "tweak NOT_REACHED in DEBUGGING builds"
[perl5.git] / scope.h
CommitLineData
d6376244
JH
1/* scope.h
2 *
1129b882
NC
3 * Copyright (C) 1993, 1994, 1996, 1997, 1998, 1999, 2000, 2001,
4 * 2002, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
d6376244
JH
5 *
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8 *
9 */
10
03dba561
DM
11/* *** these are ordered by number of of auto-popped args */
12
13/* zero args */
14
15#define SAVEt_ALLOC 0
16#define SAVEt_CLEARPADRANGE 1
17#define SAVEt_CLEARSV 2
18#define SAVEt_REGCONTEXT 3
03dba561 19
ff2a62e0 20#define SAVEt_ARG0_MAX 3
03dba561
DM
21
22/* one arg */
23
2ef9a108 24#define SAVEt_TMPSFLOOR 4
03dba561
DM
25#define SAVEt_BOOL 5
26#define SAVEt_COMPILE_WARNINGS 6
27#define SAVEt_COMPPAD 7
28#define SAVEt_FREECOPHH 8
29#define SAVEt_FREEOP 9
30#define SAVEt_FREEPV 10
31#define SAVEt_FREESV 11
32#define SAVEt_I16 12
33#define SAVEt_I32_SMALL 13
34#define SAVEt_I8 14
35#define SAVEt_INT_SMALL 15
36#define SAVEt_MORTALIZESV 16
37#define SAVEt_NSTAB 17
38#define SAVEt_OP 18
39#define SAVEt_PARSER 19
40#define SAVEt_STACK_POS 20
20d5dc23 41#define SAVEt_READONLY_OFF 21
0f94cb1f 42#define SAVEt_FREEPADNAME 22
03dba561 43
0f94cb1f 44#define SAVEt_ARG1_MAX 22
03dba561
DM
45
46/* two args */
47
03dba561
DM
48#define SAVEt_AV 23
49#define SAVEt_DESTRUCTOR 24
50#define SAVEt_DESTRUCTOR_X 25
51#define SAVEt_GENERIC_PVREF 26
52#define SAVEt_GENERIC_SVREF 27
53#define SAVEt_GP 28
54#define SAVEt_GVSV 29
55#define SAVEt_HINTS 30
56#define SAVEt_HPTR 31
57#define SAVEt_HV 32
58#define SAVEt_I32 33
59#define SAVEt_INT 34
60#define SAVEt_ITEM 35
61#define SAVEt_IV 36
62#define SAVEt_LONG 37
63#define SAVEt_PPTR 38
64#define SAVEt_SAVESWITCHSTACK 39
65#define SAVEt_SHARED_PVREF 40
66#define SAVEt_SPTR 41
e8eb279c 67#define SAVEt_STRLEN 42
03dba561
DM
68#define SAVEt_SV 43
69#define SAVEt_SVREF 44
70#define SAVEt_VPTR 45
20d5dc23 71#define SAVEt_ADELETE 46
0f94cb1f 72#define SAVEt_APTR 47
03dba561 73
0f94cb1f 74#define SAVEt_ARG2_MAX 47
03dba561
DM
75
76/* three args */
77
03dba561
DM
78#define SAVEt_HELEM 48
79#define SAVEt_PADSV_AND_MORTALIZE 49
80#define SAVEt_SET_SVFLAGS 50
81#define SAVEt_GVSLOT 51
20d5dc23 82#define SAVEt_AELEM 52
0f94cb1f
FC
83#define SAVEt_DELETE 53
84
79072805 85
af7df257 86#define SAVEf_SETMAGIC 1
75d34a09 87#define SAVEf_KEEPOLDELEM 2
af7df257 88
c6bf6a65
NC
89#define SAVE_TIGHT_SHIFT 6
90#define SAVE_MASK 0x3F
91
91d1c79f 92#define save_aelem(av,idx,sptr) save_aelem_flags(av,idx,sptr,SAVEf_SETMAGIC)
af7df257
CS
93#define save_helem(hv,key,sptr) save_helem_flags(hv,key,sptr,SAVEf_SETMAGIC)
94
b03c0a3a 95#ifndef SCOPE_SAVES_SIGNAL_MASK
1b266415 96#define SCOPE_SAVES_SIGNAL_MASK 0
b03c0a3a
NIS
97#endif
98
a3444cc5
DM
99/* the maximum number of entries that might be pushed using the SS_ADD*
100 * macros */
101#define SS_MAXPUSH 4
102
3caf0269
DM
103#define SSCHECK(need) if (UNLIKELY(PL_savestack_ix + (I32)(need) > PL_savestack_max)) savestack_grow()
104#define SSGROW(need) if (UNLIKELY(PL_savestack_ix + (I32)(need) > PL_savestack_max)) savestack_grow_cnt(need)
3280af22
NIS
105#define SSPUSHINT(i) (PL_savestack[PL_savestack_ix++].any_i32 = (I32)(i))
106#define SSPUSHLONG(i) (PL_savestack[PL_savestack_ix++].any_long = (long)(i))
9febdf04 107#define SSPUSHBOOL(p) (PL_savestack[PL_savestack_ix++].any_bool = (p))
3280af22 108#define SSPUSHIV(i) (PL_savestack[PL_savestack_ix++].any_iv = (IV)(i))
c6bf6a65 109#define SSPUSHUV(u) (PL_savestack[PL_savestack_ix++].any_uv = (UV)(u))
3280af22
NIS
110#define SSPUSHPTR(p) (PL_savestack[PL_savestack_ix++].any_ptr = (void*)(p))
111#define SSPUSHDPTR(p) (PL_savestack[PL_savestack_ix++].any_dptr = (p))
c76ac1ee 112#define SSPUSHDXPTR(p) (PL_savestack[PL_savestack_ix++].any_dxptr = (p))
a3444cc5
DM
113
114/* SS_ADD*: newer, faster versions of the above. Don't mix the two sets of
115 * macros. These are fast because they save reduce accesses to the PL_
116 * vars and move the size check to the end. Doing the check last means
117 * that values in registers will have been pushed and no longer needed, so
118 * don't need saving around the call to grow. Also, tail-call elimination
119 * of the grow() can be done. These changes reduce the code of something
120 * like save_pushptrptr() to half its former size.
121 * Of course, doing the size check *after* pushing means we must always
3caf0269
DM
122 * ensure there are SS_MAXPUSH free slots on the savestack. This ensured
123 * bt savestack_grow() and savestack_grow_cnt always allocating SS_MAXPUSH
124 * slots more than asked for, or that it sets PL_savestack_max to
a3444cc5
DM
125 *
126 * These are for internal core use only and are subject to change */
127
128#define dSS_ADD \
129 I32 ix = PL_savestack_ix; \
c70927a6 130 ANY *ssp = &PL_savestack[ix]
a3444cc5
DM
131
132#define SS_ADD_END(need) \
133 assert((need) <= SS_MAXPUSH); \
134 ix += (need); \
135 PL_savestack_ix = ix; \
3caf0269
DM
136 assert(ix <= PL_savestack_max + SS_MAXPUSH); \
137 if (UNLIKELY(ix > PL_savestack_max)) savestack_grow(); \
138 assert(PL_savestack_ix <= PL_savestack_max);
a3444cc5
DM
139
140#define SS_ADD_INT(i) ((ssp++)->any_i32 = (I32)(i))
141#define SS_ADD_LONG(i) ((ssp++)->any_long = (long)(i))
142#define SS_ADD_BOOL(p) ((ssp++)->any_bool = (p))
143#define SS_ADD_IV(i) ((ssp++)->any_iv = (IV)(i))
144#define SS_ADD_UV(u) ((ssp++)->any_uv = (UV)(u))
145#define SS_ADD_PTR(p) ((ssp++)->any_ptr = (void*)(p))
146#define SS_ADD_DPTR(p) ((ssp++)->any_dptr = (p))
147#define SS_ADD_DXPTR(p) ((ssp++)->any_dxptr = (p))
148
3280af22
NIS
149#define SSPOPINT (PL_savestack[--PL_savestack_ix].any_i32)
150#define SSPOPLONG (PL_savestack[--PL_savestack_ix].any_long)
9febdf04 151#define SSPOPBOOL (PL_savestack[--PL_savestack_ix].any_bool)
3280af22 152#define SSPOPIV (PL_savestack[--PL_savestack_ix].any_iv)
c6bf6a65 153#define SSPOPUV (PL_savestack[--PL_savestack_ix].any_uv)
3280af22
NIS
154#define SSPOPPTR (PL_savestack[--PL_savestack_ix].any_ptr)
155#define SSPOPDPTR (PL_savestack[--PL_savestack_ix].any_dptr)
c76ac1ee 156#define SSPOPDXPTR (PL_savestack[--PL_savestack_ix].any_dxptr)
8990e307 157
a3444cc5 158
954c1994 159/*
ccfc67b7
JH
160=head1 Callback Functions
161
954c1994 162=for apidoc Ams||SAVETMPS
fbe13c60 163Opening bracket for temporaries on a callback. See C<L</FREETMPS>> and
954c1994
GS
164L<perlcall>.
165
166=for apidoc Ams||FREETMPS
fbe13c60 167Closing bracket for temporaries on a callback. See C<L</SAVETMPS>> and
954c1994
GS
168L<perlcall>.
169
170=for apidoc Ams||ENTER
fbe13c60 171Opening bracket on a callback. See C<L</LEAVE>> and L<perlcall>.
954c1994
GS
172
173=for apidoc Ams||LEAVE
fbe13c60 174Closing bracket on a callback. See C<L</ENTER>> and L<perlcall>.
954c1994 175
d343c3ef
GG
176=over
177
178=item ENTER_with_name(name)
179
180Same as C<ENTER>, but when debugging is enabled it also associates the
181given literal string with the new scope.
182
183=item LEAVE_with_name(name)
184
185Same as C<LEAVE>, but when debugging is enabled it first checks that the
0c395ea5 186scope has the given name. C<name> must be a C<NUL>-terminated literal string.
d343c3ef
GG
187
188=back
189
954c1994
GS
190=cut
191*/
192
2ef9a108
DM
193#define SAVETMPS Perl_savetmps(aTHX)
194
3280af22 195#define FREETMPS if (PL_tmps_ix > PL_tmps_floor) free_tmps()
a0d0e21e 196
f46d017c
GS
197#ifdef DEBUGGING
198#define ENTER \
199 STMT_START { \
200 push_scope(); \
b4ab917c 201 DEBUG_SCOPE("ENTER") \
f46d017c
GS
202 } STMT_END
203#define LEAVE \
204 STMT_START { \
b4ab917c 205 DEBUG_SCOPE("LEAVE") \
f46d017c
GS
206 pop_scope(); \
207 } STMT_END
d343c3ef
GG
208#define ENTER_with_name(name) \
209 STMT_START { \
210 push_scope(); \
a09dc31d
NC
211 if (PL_scopestack_name) \
212 PL_scopestack_name[PL_scopestack_ix-1] = name; \
d343c3ef
GG
213 DEBUG_SCOPE("ENTER \"" name "\"") \
214 } STMT_END
215#define LEAVE_with_name(name) \
216 STMT_START { \
217 DEBUG_SCOPE("LEAVE \"" name "\"") \
a09dc31d
NC
218 if (PL_scopestack_name) { \
219 assert(((char*)PL_scopestack_name[PL_scopestack_ix-1] \
220 == (char*)name) \
221 || strEQ(PL_scopestack_name[PL_scopestack_ix-1], name)); \
222 } \
d343c3ef
GG
223 pop_scope(); \
224 } STMT_END
f46d017c 225#else
a0d0e21e
LW
226#define ENTER push_scope()
227#define LEAVE pop_scope()
d343c3ef
GG
228#define ENTER_with_name(name) ENTER
229#define LEAVE_with_name(name) LEAVE
f46d017c 230#endif
d699ecb7
FC
231#define LEAVE_SCOPE(old) STMT_START { \
232 if (PL_savestack_ix > old) leave_scope(old); \
233 } STMT_END
a0d0e21e 234
3f26ef39
NC
235#define SAVEI8(i) save_I8((I8*)&(i))
236#define SAVEI16(i) save_I16((I16*)&(i))
237#define SAVEI32(i) save_I32((I32*)&(i))
238#define SAVEINT(i) save_int((int*)&(i))
239#define SAVEIV(i) save_iv((IV*)&(i))
240#define SAVELONG(l) save_long((long*)&(l))
02644217 241#define SAVEBOOL(b) save_bool(&(b))
55497cff 242#define SAVESPTR(s) save_sptr((SV**)&(s))
3f26ef39 243#define SAVEPPTR(s) save_pptr((char**)&(s))
57b2e452 244#define SAVEVPTR(s) save_vptr((void*)&(s))
09edbca0 245#define SAVEPADSVANDMORTALIZE(s) save_padsv_and_mortalize(s)
b1bc3f34 246#define SAVEFREESV(s) save_freesv(MUTABLE_SV(s))
0f94cb1f 247#define SAVEFREEPADNAME(s) save_pushptr((void *)(s), SAVEt_FREEPADNAME)
b1bc3f34 248#define SAVEMORTALIZESV(s) save_mortalizesv(MUTABLE_SV(s))
3f26ef39
NC
249#define SAVEFREEOP(o) save_freeop((OP*)(o))
250#define SAVEFREEPV(p) save_freepv((char*)(p))
251#define SAVECLEARSV(sv) save_clearsv((SV**)&(sv))
b9d12d37 252#define SAVEGENERICSV(s) save_generic_svref((SV**)&(s))
f4dd75d9 253#define SAVEGENERICPV(s) save_generic_pvref((char**)&(s))
05ec9bb3 254#define SAVESHAREDPV(s) save_shared_pvref((char**)&(s))
14f338dc 255#define SAVESETSVFLAGS(sv,mask,val) save_set_svflags(sv,mask,val)
3987a177 256#define SAVEFREECOPHH(h) save_pushptr((void *)(h), SAVEt_FREECOPHH)
55497cff 257#define SAVEDELETE(h,k,l) \
dbebbdb4 258 save_delete(MUTABLE_HV(h), (char*)(k), (I32)(l))
af097752
VP
259#define SAVEHDELETE(h,s) \
260 save_hdelete(MUTABLE_HV(h), (s))
c68ec7a9 261#define SAVEADELETE(a,k) \
c70927a6 262 save_adelete(MUTABLE_AV(a), (SSize_t)(k))
55497cff 263#define SAVEDESTRUCTOR(f,p) \
3f26ef39 264 save_destructor((DESTRUCTORFUNC_NOCONTEXT_t)(f), (void*)(p))
c76ac1ee
GS
265
266#define SAVEDESTRUCTOR_X(f,p) \
3f26ef39 267 save_destructor_x((DESTRUCTORFUNC_t)(f), (void*)(p))
25eaa213
GS
268
269#define SAVESTACK_POS() \
a3444cc5
DM
270 STMT_START { \
271 dSS_ADD; \
272 SS_ADD_INT(PL_stack_sp - PL_stack_base); \
273 SS_ADD_UV(SAVEt_STACK_POS); \
274 SS_ADD_END(2); \
25eaa213
GS
275 } STMT_END
276
462e5cf6 277#define SAVEOP() save_op()
25eaa213 278
da8315f8 279#define SAVEHINTS() save_hints()
354992b1 280
9c47725a 281#define SAVECOMPPAD() save_pushptr(MUTABLE_SV(PL_comppad), SAVEt_COMPPAD)
a9332b4a 282
8b7059b1
DM
283#define SAVESWITCHSTACK(f,t) \
284 STMT_START { \
dfcd3de5 285 save_pushptrptr(MUTABLE_SV(f), MUTABLE_SV(t), SAVEt_SAVESWITCHSTACK); \
8b7059b1
DM
286 SWITCHSTACK((f),(t)); \
287 PL_curstackinfo->si_stack = (t); \
288 } STMT_END
289
c88c823b 290/* Need to do the cop warnings like this, rather than a "SAVEFREESHAREDPV",
72dc9ed5
NC
291 because realloc() means that the value can actually change. Possibly
292 could have done savefreesharedpvREF, but this way actually seems cleaner,
293 as it simplifies the code that does the saves, and reduces the load on the
294 save stack. */
747e2fae 295#define SAVECOMPILEWARNINGS() save_pushptr(PL_compiling.cop_warnings, SAVEt_COMPILE_WARNINGS)
fc15ae8f 296
747e2fae 297#define SAVEPARSER(p) save_pushptr((p), SAVEt_PARSER)
7c197c94 298
57843af0 299#ifdef USE_ITHREADS
d4d03940 300# define SAVECOPSTASH_FREE(c) SAVEIV((c)->cop_stashoff)
1dc74fdb
FC
301# define SAVECOPFILE(c) SAVEPPTR(CopFILE(c))
302# define SAVECOPFILE_FREE(c) SAVESHAREDPV(CopFILE(c))
57843af0 303#else
61b3e50d
FC
304# /* XXX not refcounted */
305# define SAVECOPSTASH_FREE(c) SAVESPTR(CopSTASH(c))
f4dd75d9
GS
306# define SAVECOPFILE(c) SAVESPTR(CopFILEGV(c))
307# define SAVECOPFILE_FREE(c) SAVEGENERICSV(CopFILEGV(c))
57843af0
GS
308#endif
309
dea28490 310#define SAVECOPLINE(c) SAVEI32(CopLINE(c))
57843af0 311
455ece5e
AD
312/* SSNEW() temporarily allocates a specified number of bytes of data on the
313 * savestack. It returns an integer index into the savestack, because a
314 * pointer would get broken if the savestack is moved on reallocation.
315 * SSNEWa() works like SSNEW(), but also aligns the data to the specified
316 * number of bytes. MEM_ALIGNBYTES is perhaps the most useful. The
486ec47a 317 * alignment will be preserved through savestack reallocation *only* if
a0288114 318 * realloc returns data aligned to a size divisible by "align"!
455ece5e
AD
319 *
320 * SSPTR() converts the index returned by SSNEW/SSNEWa() into a pointer.
321 */
322
f2b2c1a7 323#define SSNEW(size) Perl_save_alloc(aTHX_ (size), 0)
02db2b7b 324#define SSNEWt(n,t) SSNEW((n)*sizeof(t))
f2b2c1a7 325#define SSNEWa(size,align) Perl_save_alloc(aTHX_ (size), \
6b99f28a 326 (I32)(align - ((size_t)((caddr_t)&PL_savestack[PL_savestack_ix]) % align)) % align)
02db2b7b 327#define SSNEWat(n,t,align) SSNEWa((n)*sizeof(t), align)
455ece5e 328
02db2b7b
IZ
329#define SSPTR(off,type) ((type) ((char*)PL_savestack + off))
330#define SSPTRt(off,type) ((type*) ((char*)PL_savestack + off))
455ece5e 331
2fd8beea
NC
332#define save_freesv(op) save_pushptr((void *)(op), SAVEt_FREESV)
333#define save_mortalizesv(op) save_pushptr((void *)(op), SAVEt_MORTALIZESV)
6702ba93 334
bb38a9e0 335# define save_freeop(op) \
6702ba93 336STMT_START { \
bb38a9e0 337 OP * const _o = (OP *)(op); \
6c7ae946 338 assert(!_o->op_savefree); \
bb38a9e0
FC
339 _o->op_savefree = 1; \
340 save_pushptr((void *)(_o), SAVEt_FREEOP); \
6702ba93 341 } STMT_END
2fd8beea
NC
342#define save_freepv(pv) save_pushptr((void *)(pv), SAVEt_FREEPV)
343#define save_op() save_pushptr((void *)(PL_op), SAVEt_OP)
344
e9a8c099 345/*
14d04a33 346 * ex: set ts=8 sts=4 sw=4 et:
e9a8c099 347 */