This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perl5380delta: a messy commit to get things started
[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
721bab59 11#include "scope_types.h"
79072805 12
af7df257 13#define SAVEf_SETMAGIC 1
75d34a09 14#define SAVEf_KEEPOLDELEM 2
af7df257 15
c6bf6a65
NC
16#define SAVE_TIGHT_SHIFT 6
17#define SAVE_MASK 0x3F
18
91d1c79f 19#define save_aelem(av,idx,sptr) save_aelem_flags(av,idx,sptr,SAVEf_SETMAGIC)
af7df257
CS
20#define save_helem(hv,key,sptr) save_helem_flags(hv,key,sptr,SAVEf_SETMAGIC)
21
b03c0a3a 22#ifndef SCOPE_SAVES_SIGNAL_MASK
1b266415 23#define SCOPE_SAVES_SIGNAL_MASK 0
b03c0a3a
NIS
24#endif
25
a3444cc5
DM
26/* the maximum number of entries that might be pushed using the SS_ADD*
27 * macros */
28#define SS_MAXPUSH 4
29
3caf0269 30#define SSGROW(need) if (UNLIKELY(PL_savestack_ix + (I32)(need) > PL_savestack_max)) savestack_grow_cnt(need)
fc11df3e 31#define SSCHECK(need) SSGROW(need) /* legacy */
3280af22
NIS
32#define SSPUSHINT(i) (PL_savestack[PL_savestack_ix++].any_i32 = (I32)(i))
33#define SSPUSHLONG(i) (PL_savestack[PL_savestack_ix++].any_long = (long)(i))
9febdf04 34#define SSPUSHBOOL(p) (PL_savestack[PL_savestack_ix++].any_bool = (p))
3280af22 35#define SSPUSHIV(i) (PL_savestack[PL_savestack_ix++].any_iv = (IV)(i))
c6bf6a65 36#define SSPUSHUV(u) (PL_savestack[PL_savestack_ix++].any_uv = (UV)(u))
3280af22
NIS
37#define SSPUSHPTR(p) (PL_savestack[PL_savestack_ix++].any_ptr = (void*)(p))
38#define SSPUSHDPTR(p) (PL_savestack[PL_savestack_ix++].any_dptr = (p))
c76ac1ee 39#define SSPUSHDXPTR(p) (PL_savestack[PL_savestack_ix++].any_dxptr = (p))
a3444cc5
DM
40
41/* SS_ADD*: newer, faster versions of the above. Don't mix the two sets of
42 * macros. These are fast because they save reduce accesses to the PL_
43 * vars and move the size check to the end. Doing the check last means
44 * that values in registers will have been pushed and no longer needed, so
45 * don't need saving around the call to grow. Also, tail-call elimination
46 * of the grow() can be done. These changes reduce the code of something
47 * like save_pushptrptr() to half its former size.
48 * Of course, doing the size check *after* pushing means we must always
f2e74355 49 * ensure there are SS_MAXPUSH free slots on the savestack. This is ensured by
fc11df3e 50 * savestack_grow_cnt always allocating SS_MAXPUSH slots
f2e74355 51 * more than asked for, or that it sets PL_savestack_max to
a3444cc5
DM
52 *
53 * These are for internal core use only and are subject to change */
54
55#define dSS_ADD \
56 I32 ix = PL_savestack_ix; \
c70927a6 57 ANY *ssp = &PL_savestack[ix]
a3444cc5
DM
58
59#define SS_ADD_END(need) \
60 assert((need) <= SS_MAXPUSH); \
61 ix += (need); \
62 PL_savestack_ix = ix; \
3caf0269 63 assert(ix <= PL_savestack_max + SS_MAXPUSH); \
fc11df3e 64 if (UNLIKELY(ix > PL_savestack_max)) savestack_grow_cnt(ix - PL_savestack_max); \
3caf0269 65 assert(PL_savestack_ix <= PL_savestack_max);
a3444cc5
DM
66
67#define SS_ADD_INT(i) ((ssp++)->any_i32 = (I32)(i))
68#define SS_ADD_LONG(i) ((ssp++)->any_long = (long)(i))
69#define SS_ADD_BOOL(p) ((ssp++)->any_bool = (p))
70#define SS_ADD_IV(i) ((ssp++)->any_iv = (IV)(i))
71#define SS_ADD_UV(u) ((ssp++)->any_uv = (UV)(u))
72#define SS_ADD_PTR(p) ((ssp++)->any_ptr = (void*)(p))
73#define SS_ADD_DPTR(p) ((ssp++)->any_dptr = (p))
74#define SS_ADD_DXPTR(p) ((ssp++)->any_dxptr = (p))
75
3280af22
NIS
76#define SSPOPINT (PL_savestack[--PL_savestack_ix].any_i32)
77#define SSPOPLONG (PL_savestack[--PL_savestack_ix].any_long)
9febdf04 78#define SSPOPBOOL (PL_savestack[--PL_savestack_ix].any_bool)
3280af22 79#define SSPOPIV (PL_savestack[--PL_savestack_ix].any_iv)
c6bf6a65 80#define SSPOPUV (PL_savestack[--PL_savestack_ix].any_uv)
3280af22
NIS
81#define SSPOPPTR (PL_savestack[--PL_savestack_ix].any_ptr)
82#define SSPOPDPTR (PL_savestack[--PL_savestack_ix].any_dptr)
c76ac1ee 83#define SSPOPDXPTR (PL_savestack[--PL_savestack_ix].any_dxptr)
8990e307 84
a3444cc5 85
954c1994 86/*
3f620621 87=for apidoc_section $callback
ccfc67b7 88
3734d2f5 89=for apidoc Amn;||SAVETMPS
fbe13c60 90Opening bracket for temporaries on a callback. See C<L</FREETMPS>> and
954c1994
GS
91L<perlcall>.
92
3734d2f5 93=for apidoc Amn;||FREETMPS
fbe13c60 94Closing bracket for temporaries on a callback. See C<L</SAVETMPS>> and
954c1994
GS
95L<perlcall>.
96
3734d2f5 97=for apidoc Amn;||ENTER
fbe13c60 98Opening bracket on a callback. See C<L</LEAVE>> and L<perlcall>.
954c1994 99
3734d2f5 100=for apidoc Amn;||LEAVE
fbe13c60 101Closing bracket on a callback. See C<L</ENTER>> and L<perlcall>.
954c1994 102
3734d2f5 103=for apidoc Am;||ENTER_with_name|"name"
d343c3ef 104
ef7ea7ad 105Same as C<L</ENTER>>, but when debugging is enabled it also associates the
d343c3ef
GG
106given literal string with the new scope.
107
3734d2f5 108=for apidoc Am;||LEAVE_with_name|"name"
d343c3ef 109
ef7ea7ad 110Same as C<L</LEAVE>>, but when debugging is enabled it first checks that the
1568d13a 111scope has the given name. C<name> must be a literal string.
d343c3ef 112
954c1994
GS
113=cut
114*/
115
2ef9a108
DM
116#define SAVETMPS Perl_savetmps(aTHX)
117
3280af22 118#define FREETMPS if (PL_tmps_ix > PL_tmps_floor) free_tmps()
a0d0e21e 119
f46d017c
GS
120#ifdef DEBUGGING
121#define ENTER \
122 STMT_START { \
1604cfb0
MS
123 push_scope(); \
124 DEBUG_SCOPE("ENTER") \
f46d017c
GS
125 } STMT_END
126#define LEAVE \
127 STMT_START { \
1604cfb0
MS
128 DEBUG_SCOPE("LEAVE") \
129 pop_scope(); \
f46d017c 130 } STMT_END
d343c3ef
GG
131#define ENTER_with_name(name) \
132 STMT_START { \
1604cfb0
MS
133 push_scope(); \
134 if (PL_scopestack_name) \
ca0572d7 135 PL_scopestack_name[PL_scopestack_ix-1] = ASSERT_IS_LITERAL(name);\
1604cfb0 136 DEBUG_SCOPE("ENTER \"" name "\"") \
d343c3ef
GG
137 } STMT_END
138#define LEAVE_with_name(name) \
139 STMT_START { \
1604cfb0
MS
140 DEBUG_SCOPE("LEAVE \"" name "\"") \
141 if (PL_scopestack_name) { \
5f356391 142 CLANG_DIAG_IGNORE_STMT(-Wstring-compare); \
1604cfb0 143 assert(((char*)PL_scopestack_name[PL_scopestack_ix-1] \
ca0572d7 144 == (char*)ASSERT_IS_LITERAL(name)) \
1604cfb0 145 || strEQ(PL_scopestack_name[PL_scopestack_ix-1], name)); \
5f356391 146 CLANG_DIAG_RESTORE_STMT; \
1604cfb0
MS
147 } \
148 pop_scope(); \
d343c3ef 149 } STMT_END
f46d017c 150#else
a0d0e21e
LW
151#define ENTER push_scope()
152#define LEAVE pop_scope()
d343c3ef
GG
153#define ENTER_with_name(name) ENTER
154#define LEAVE_with_name(name) LEAVE
f46d017c 155#endif
d699ecb7 156#define LEAVE_SCOPE(old) STMT_START { \
1604cfb0 157 if (PL_savestack_ix > old) leave_scope(old); \
d699ecb7 158 } STMT_END
a0d0e21e 159
58541fd0
PE
160#define SAVEI8(i) save_I8((I8*)&(i))
161#define SAVEI16(i) save_I16((I16*)&(i))
162#define SAVEI32(i) save_I32((I32*)&(i))
163#define SAVEINT(i) save_int((int*)&(i))
164#define SAVEIV(i) save_iv((IV*)&(i))
165#define SAVELONG(l) save_long((long*)&(l))
166#define SAVESTRLEN(l) Perl_save_strlen(aTHX_ (STRLEN*)&(l))
167#define SAVEBOOL(b) save_bool(&(b))
168#define SAVESPTR(s) save_sptr((SV**)&(s))
169#define SAVEPPTR(s) save_pptr((char**)&(s))
170#define SAVEVPTR(s) save_vptr((void*)&(s))
171#define SAVEPADSVANDMORTALIZE(s) save_padsv_and_mortalize(s)
172#define SAVEFREESV(s) save_freesv(MUTABLE_SV(s))
173#define SAVEFREEPADNAME(s) save_pushptr((void *)(s), SAVEt_FREEPADNAME)
174#define SAVEMORTALIZESV(s) save_mortalizesv(MUTABLE_SV(s))
175#define SAVEFREEOP(o) save_freeop((OP*)(o))
176#define SAVEFREEPV(p) save_freepv((char*)(p))
177#define SAVECLEARSV(sv) save_clearsv((SV**)&(sv))
178#define SAVEGENERICSV(s) save_generic_svref((SV**)&(s))
179#define SAVEGENERICPV(s) save_generic_pvref((char**)&(s))
624f6f53
YO
180#define SAVERCPV(s) save_rcpv((char**)&(s))
181#define SAVEFREERCPV(s) save_freercpv(s)
58541fd0
PE
182#define SAVESHAREDPV(s) save_shared_pvref((char**)&(s))
183#define SAVESETSVFLAGS(sv,mask,val) save_set_svflags(sv,mask,val)
184#define SAVEFREECOPHH(h) save_pushptr((void *)(h), SAVEt_FREECOPHH)
185
55497cff 186#define SAVEDELETE(h,k,l) \
1604cfb0 187 save_delete(MUTABLE_HV(h), (char*)(k), (I32)(l))
af097752 188#define SAVEHDELETE(h,s) \
1604cfb0 189 save_hdelete(MUTABLE_HV(h), (s))
c68ec7a9 190#define SAVEADELETE(a,k) \
1604cfb0 191 save_adelete(MUTABLE_AV(a), (SSize_t)(k))
55497cff 192#define SAVEDESTRUCTOR(f,p) \
1604cfb0 193 save_destructor((DESTRUCTORFUNC_NOCONTEXT_t)(f), (void*)(p))
c76ac1ee
GS
194
195#define SAVEDESTRUCTOR_X(f,p) \
1604cfb0 196 save_destructor_x((DESTRUCTORFUNC_t)(f), (void*)(p))
25eaa213 197
2f920c2f
YO
198#define MORTALSVFUNC_X(f,sv) \
199 mortal_svfunc_x((SVFUNC_t)(f), sv)
200
201#define MORTALDESTRUCTOR_SV(coderef,args) \
202 mortal_destructor_sv(coderef,args)
203
25eaa213 204#define SAVESTACK_POS() \
a3444cc5
DM
205 STMT_START { \
206 dSS_ADD; \
207 SS_ADD_INT(PL_stack_sp - PL_stack_base); \
208 SS_ADD_UV(SAVEt_STACK_POS); \
209 SS_ADD_END(2); \
25eaa213
GS
210 } STMT_END
211
462e5cf6 212#define SAVEOP() save_op()
25eaa213 213
da8315f8 214#define SAVEHINTS() save_hints()
354992b1 215
9c47725a 216#define SAVECOMPPAD() save_pushptr(MUTABLE_SV(PL_comppad), SAVEt_COMPPAD)
a9332b4a 217
8b7059b1
DM
218#define SAVESWITCHSTACK(f,t) \
219 STMT_START { \
1604cfb0
MS
220 save_pushptrptr(MUTABLE_SV(f), MUTABLE_SV(t), SAVEt_SAVESWITCHSTACK); \
221 SWITCHSTACK((f),(t)); \
222 PL_curstackinfo->si_stack = (t); \
8b7059b1
DM
223 } STMT_END
224
fdec6615
YO
225/* Note these are special, we can't just use a save_pushptrptr() on them
226 * as the target might change after a fork or thread start. */
227#define SAVECOMPILEWARNINGS() save_pushptr(PL_compiling.cop_warnings, SAVEt_COMPILE_WARNINGS)
228#define SAVECURCOPWARNINGS() save_pushptr(PL_curcop->cop_warnings, SAVEt_CURCOP_WARNINGS)
229
fc15ae8f 230
747e2fae 231#define SAVEPARSER(p) save_pushptr((p), SAVEt_PARSER)
7c197c94 232
57843af0 233#ifdef USE_ITHREADS
d4d03940 234# define SAVECOPSTASH_FREE(c) SAVEIV((c)->cop_stashoff)
6760f691
YO
235# define SAVECOPFILE_x(c) SAVEPPTR((c)->cop_file)
236# define SAVECOPFILE(c) \
237 STMT_START { \
238 SAVECOPFILE_x(c); \
239 CopFILE_debug((c),"SAVECOPFILE",0); \
240 } STMT_END
624f6f53 241# define SAVECOPFILE_FREE_x(c) SAVERCPV((c)->cop_file)
6760f691
YO
242# define SAVECOPFILE_FREE(c) \
243 STMT_START { \
244 SAVECOPFILE_FREE_x(c); \
245 CopFILE_debug((c),"SAVECOPFILE_FREE",0); \
246 } STMT_END
57843af0 247#else
61b3e50d
FC
248# /* XXX not refcounted */
249# define SAVECOPSTASH_FREE(c) SAVESPTR(CopSTASH(c))
f4dd75d9
GS
250# define SAVECOPFILE(c) SAVESPTR(CopFILEGV(c))
251# define SAVECOPFILE_FREE(c) SAVEGENERICSV(CopFILEGV(c))
57843af0
GS
252#endif
253
dea28490 254#define SAVECOPLINE(c) SAVEI32(CopLINE(c))
57843af0 255
bf546166
KW
256/*
257=for apidoc_section $stack
1a2b24d8
TC
258=for apidoc Am|SSize_t|SSNEW |Size_t size
259=for apidoc_item | |SSNEWa |Size_t_size|Size_t align
260=for apidoc_item | |SSNEWat|Size_t_size|type|Size_t align
261=for apidoc_item | |SSNEWt |Size_t size|type
bf546166 262
1a2b24d8 263These temporarily allocates data on the savestack, returning an SSize_t index into
bf546166
KW
264the savestack, because a pointer would get broken if the savestack is moved on
265reallocation. Use L</C<SSPTR>> to convert the returned index into a pointer.
266
267The forms differ in that plain C<SSNEW> allocates C<size> bytes;
268C<SSNEWt> and C<SSNEWat> allocate C<size> objects, each of which is type
269C<type>;
270and <SSNEWa> and C<SSNEWat> make sure to align the new data to an C<align>
271boundary. The most useful value for the alignment is likely to be
272L</C<MEM_ALIGNBYTES>>. The alignment will be preserved through savestack
273reallocation B<only> if realloc returns data aligned to a size divisible by
274"align"!
275
1a2b24d8
TC
276=for apidoc Am|type |SSPTR |SSize_t index|type
277=for apidoc_item|type *|SSPTRt|SSize_t index|type
bf546166
KW
278
279These convert the C<index> returned by L/<C<SSNEW>> and kin into actual pointers.
280
281The difference is that C<SSPTR> casts the result to C<type>, and C<SSPTRt>
282casts it to a pointer of that C<type>.
283
284=cut
455ece5e
AD
285 */
286
f2b2c1a7 287#define SSNEW(size) Perl_save_alloc(aTHX_ (size), 0)
02db2b7b 288#define SSNEWt(n,t) SSNEW((n)*sizeof(t))
f2b2c1a7 289#define SSNEWa(size,align) Perl_save_alloc(aTHX_ (size), \
6b99f28a 290 (I32)(align - ((size_t)((caddr_t)&PL_savestack[PL_savestack_ix]) % align)) % align)
02db2b7b 291#define SSNEWat(n,t,align) SSNEWa((n)*sizeof(t), align)
455ece5e 292
0507844a
TC
293#define SSPTR(off,type) (assert(sizeof(off) >= sizeof(SSize_t)), (type) ((char*)PL_savestack + off))
294#define SSPTRt(off,type) (assert(sizeof(off) >= sizeof(SSize_t)), (type*) ((char*)PL_savestack + off))
455ece5e 295
2fd8beea
NC
296#define save_freesv(op) save_pushptr((void *)(op), SAVEt_FREESV)
297#define save_mortalizesv(op) save_pushptr((void *)(op), SAVEt_MORTALIZESV)
6702ba93 298
bb38a9e0 299# define save_freeop(op) \
6702ba93 300STMT_START { \
bb38a9e0 301 OP * const _o = (OP *)(op); \
6c7ae946 302 assert(!_o->op_savefree); \
bb38a9e0
FC
303 _o->op_savefree = 1; \
304 save_pushptr((void *)(_o), SAVEt_FREEOP); \
6702ba93 305 } STMT_END
2fd8beea 306#define save_freepv(pv) save_pushptr((void *)(pv), SAVEt_FREEPV)
e446bf93
KW
307
308/*
309=for apidoc_section $callback
310=for apidoc save_op
311
312Implements C<SAVEOP>.
313
314=cut
315 */
316
2fd8beea
NC
317#define save_op() save_pushptr((void *)(PL_op), SAVEt_OP)
318
e9a8c099 319/*
14d04a33 320 * ex: set ts=8 sts=4 sw=4 et:
e9a8c099 321 */