This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
.github - switch to v3 actions
[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
DM
30#define SSCHECK(need) if (UNLIKELY(PL_savestack_ix + (I32)(need) > PL_savestack_max)) savestack_grow()
31#define SSGROW(need) if (UNLIKELY(PL_savestack_ix + (I32)(need) > PL_savestack_max)) savestack_grow_cnt(need)
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
KW
49 * ensure there are SS_MAXPUSH free slots on the savestack. This is ensured by
50 * savestack_grow() and savestack_grow_cnt always allocating SS_MAXPUSH slots
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
DM
63 assert(ix <= PL_savestack_max + SS_MAXPUSH); \
64 if (UNLIKELY(ix > PL_savestack_max)) savestack_grow(); \
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))
6760f691 180#define SAVERCPVFREE(s) save_rcpv_free((char**)&(s))
58541fd0
PE
181#define SAVESHAREDPV(s) save_shared_pvref((char**)&(s))
182#define SAVESETSVFLAGS(sv,mask,val) save_set_svflags(sv,mask,val)
183#define SAVEFREECOPHH(h) save_pushptr((void *)(h), SAVEt_FREECOPHH)
184
55497cff 185#define SAVEDELETE(h,k,l) \
1604cfb0 186 save_delete(MUTABLE_HV(h), (char*)(k), (I32)(l))
af097752 187#define SAVEHDELETE(h,s) \
1604cfb0 188 save_hdelete(MUTABLE_HV(h), (s))
c68ec7a9 189#define SAVEADELETE(a,k) \
1604cfb0 190 save_adelete(MUTABLE_AV(a), (SSize_t)(k))
55497cff 191#define SAVEDESTRUCTOR(f,p) \
1604cfb0 192 save_destructor((DESTRUCTORFUNC_NOCONTEXT_t)(f), (void*)(p))
c76ac1ee
GS
193
194#define SAVEDESTRUCTOR_X(f,p) \
1604cfb0 195 save_destructor_x((DESTRUCTORFUNC_t)(f), (void*)(p))
25eaa213
GS
196
197#define SAVESTACK_POS() \
a3444cc5
DM
198 STMT_START { \
199 dSS_ADD; \
200 SS_ADD_INT(PL_stack_sp - PL_stack_base); \
201 SS_ADD_UV(SAVEt_STACK_POS); \
202 SS_ADD_END(2); \
25eaa213
GS
203 } STMT_END
204
462e5cf6 205#define SAVEOP() save_op()
25eaa213 206
da8315f8 207#define SAVEHINTS() save_hints()
354992b1 208
9c47725a 209#define SAVECOMPPAD() save_pushptr(MUTABLE_SV(PL_comppad), SAVEt_COMPPAD)
a9332b4a 210
8b7059b1
DM
211#define SAVESWITCHSTACK(f,t) \
212 STMT_START { \
1604cfb0
MS
213 save_pushptrptr(MUTABLE_SV(f), MUTABLE_SV(t), SAVEt_SAVESWITCHSTACK); \
214 SWITCHSTACK((f),(t)); \
215 PL_curstackinfo->si_stack = (t); \
8b7059b1
DM
216 } STMT_END
217
fdec6615
YO
218/* Note these are special, we can't just use a save_pushptrptr() on them
219 * as the target might change after a fork or thread start. */
220#define SAVECOMPILEWARNINGS() save_pushptr(PL_compiling.cop_warnings, SAVEt_COMPILE_WARNINGS)
221#define SAVECURCOPWARNINGS() save_pushptr(PL_curcop->cop_warnings, SAVEt_CURCOP_WARNINGS)
222
fc15ae8f 223
747e2fae 224#define SAVEPARSER(p) save_pushptr((p), SAVEt_PARSER)
7c197c94 225
57843af0 226#ifdef USE_ITHREADS
d4d03940 227# define SAVECOPSTASH_FREE(c) SAVEIV((c)->cop_stashoff)
6760f691
YO
228# define SAVECOPFILE_x(c) SAVEPPTR((c)->cop_file)
229# define SAVECOPFILE(c) \
230 STMT_START { \
231 SAVECOPFILE_x(c); \
232 CopFILE_debug((c),"SAVECOPFILE",0); \
233 } STMT_END
234# define SAVECOPFILE_FREE_x(c) SAVERCPVFREE((c)->cop_file)
235# define SAVECOPFILE_FREE(c) \
236 STMT_START { \
237 SAVECOPFILE_FREE_x(c); \
238 CopFILE_debug((c),"SAVECOPFILE_FREE",0); \
239 } STMT_END
57843af0 240#else
61b3e50d
FC
241# /* XXX not refcounted */
242# define SAVECOPSTASH_FREE(c) SAVESPTR(CopSTASH(c))
f4dd75d9
GS
243# define SAVECOPFILE(c) SAVESPTR(CopFILEGV(c))
244# define SAVECOPFILE_FREE(c) SAVEGENERICSV(CopFILEGV(c))
57843af0
GS
245#endif
246
dea28490 247#define SAVECOPLINE(c) SAVEI32(CopLINE(c))
57843af0 248
bf546166
KW
249/*
250=for apidoc_section $stack
1a2b24d8
TC
251=for apidoc Am|SSize_t|SSNEW |Size_t size
252=for apidoc_item | |SSNEWa |Size_t_size|Size_t align
253=for apidoc_item | |SSNEWat|Size_t_size|type|Size_t align
254=for apidoc_item | |SSNEWt |Size_t size|type
bf546166 255
1a2b24d8 256These temporarily allocates data on the savestack, returning an SSize_t index into
bf546166
KW
257the savestack, because a pointer would get broken if the savestack is moved on
258reallocation. Use L</C<SSPTR>> to convert the returned index into a pointer.
259
260The forms differ in that plain C<SSNEW> allocates C<size> bytes;
261C<SSNEWt> and C<SSNEWat> allocate C<size> objects, each of which is type
262C<type>;
263and <SSNEWa> and C<SSNEWat> make sure to align the new data to an C<align>
264boundary. The most useful value for the alignment is likely to be
265L</C<MEM_ALIGNBYTES>>. The alignment will be preserved through savestack
266reallocation B<only> if realloc returns data aligned to a size divisible by
267"align"!
268
1a2b24d8
TC
269=for apidoc Am|type |SSPTR |SSize_t index|type
270=for apidoc_item|type *|SSPTRt|SSize_t index|type
bf546166
KW
271
272These convert the C<index> returned by L/<C<SSNEW>> and kin into actual pointers.
273
274The difference is that C<SSPTR> casts the result to C<type>, and C<SSPTRt>
275casts it to a pointer of that C<type>.
276
277=cut
455ece5e
AD
278 */
279
f2b2c1a7 280#define SSNEW(size) Perl_save_alloc(aTHX_ (size), 0)
02db2b7b 281#define SSNEWt(n,t) SSNEW((n)*sizeof(t))
f2b2c1a7 282#define SSNEWa(size,align) Perl_save_alloc(aTHX_ (size), \
6b99f28a 283 (I32)(align - ((size_t)((caddr_t)&PL_savestack[PL_savestack_ix]) % align)) % align)
02db2b7b 284#define SSNEWat(n,t,align) SSNEWa((n)*sizeof(t), align)
455ece5e 285
0507844a
TC
286#define SSPTR(off,type) (assert(sizeof(off) >= sizeof(SSize_t)), (type) ((char*)PL_savestack + off))
287#define SSPTRt(off,type) (assert(sizeof(off) >= sizeof(SSize_t)), (type*) ((char*)PL_savestack + off))
455ece5e 288
2fd8beea
NC
289#define save_freesv(op) save_pushptr((void *)(op), SAVEt_FREESV)
290#define save_mortalizesv(op) save_pushptr((void *)(op), SAVEt_MORTALIZESV)
6702ba93 291
bb38a9e0 292# define save_freeop(op) \
6702ba93 293STMT_START { \
bb38a9e0 294 OP * const _o = (OP *)(op); \
6c7ae946 295 assert(!_o->op_savefree); \
bb38a9e0
FC
296 _o->op_savefree = 1; \
297 save_pushptr((void *)(_o), SAVEt_FREEOP); \
6702ba93 298 } STMT_END
2fd8beea 299#define save_freepv(pv) save_pushptr((void *)(pv), SAVEt_FREEPV)
e446bf93
KW
300
301/*
302=for apidoc_section $callback
303=for apidoc save_op
304
305Implements C<SAVEOP>.
306
307=cut
308 */
309
2fd8beea
NC
310#define save_op() save_pushptr((void *)(PL_op), SAVEt_OP)
311
e9a8c099 312/*
14d04a33 313 * ex: set ts=8 sts=4 sw=4 et:
e9a8c099 314 */