3 * Copyright (C) 1993, 1994, 1996, 1997, 1998, 1999, 2000, 2001,
4 * 2002, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
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.
11 #include "scope_types.h"
13 #define SAVEf_SETMAGIC 1
14 #define SAVEf_KEEPOLDELEM 2
16 #define SAVE_TIGHT_SHIFT 6
17 #define SAVE_MASK 0x3F
19 #define save_aelem(av,idx,sptr) save_aelem_flags(av,idx,sptr,SAVEf_SETMAGIC)
20 #define save_helem(hv,key,sptr) save_helem_flags(hv,key,sptr,SAVEf_SETMAGIC)
22 #ifndef SCOPE_SAVES_SIGNAL_MASK
23 #define SCOPE_SAVES_SIGNAL_MASK 0
26 /* the maximum number of entries that might be pushed using the SS_ADD*
30 #define SSGROW(need) if (UNLIKELY(PL_savestack_ix + (I32)(need) > PL_savestack_max)) savestack_grow_cnt(need)
31 #define SSCHECK(need) SSGROW(need) /* legacy */
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))
34 #define SSPUSHBOOL(p) (PL_savestack[PL_savestack_ix++].any_bool = (p))
35 #define SSPUSHIV(i) (PL_savestack[PL_savestack_ix++].any_iv = (IV)(i))
36 #define SSPUSHUV(u) (PL_savestack[PL_savestack_ix++].any_uv = (UV)(u))
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))
39 #define SSPUSHDXPTR(p) (PL_savestack[PL_savestack_ix++].any_dxptr = (p))
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
49 * ensure there are SS_MAXPUSH free slots on the savestack. This is ensured by
50 * savestack_grow_cnt always allocating SS_MAXPUSH slots
51 * more than asked for, or that it sets PL_savestack_max to
53 * These are for internal core use only and are subject to change */
56 I32 ix = PL_savestack_ix; \
57 ANY *ssp = &PL_savestack[ix]
59 #define SS_ADD_END(need) \
60 assert((need) <= SS_MAXPUSH); \
62 PL_savestack_ix = ix; \
63 assert(ix <= PL_savestack_max + SS_MAXPUSH); \
64 if (UNLIKELY(ix > PL_savestack_max)) savestack_grow_cnt(ix - PL_savestack_max); \
65 assert(PL_savestack_ix <= PL_savestack_max);
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))
76 #define SSPOPINT (PL_savestack[--PL_savestack_ix].any_i32)
77 #define SSPOPLONG (PL_savestack[--PL_savestack_ix].any_long)
78 #define SSPOPBOOL (PL_savestack[--PL_savestack_ix].any_bool)
79 #define SSPOPIV (PL_savestack[--PL_savestack_ix].any_iv)
80 #define SSPOPUV (PL_savestack[--PL_savestack_ix].any_uv)
81 #define SSPOPPTR (PL_savestack[--PL_savestack_ix].any_ptr)
82 #define SSPOPDPTR (PL_savestack[--PL_savestack_ix].any_dptr)
83 #define SSPOPDXPTR (PL_savestack[--PL_savestack_ix].any_dxptr)
87 =for apidoc_section $callback
89 =for apidoc Amn;||SAVETMPS
90 Opening bracket for temporaries on a callback. See C<L</FREETMPS>> and
93 =for apidoc Amn;||FREETMPS
94 Closing bracket for temporaries on a callback. See C<L</SAVETMPS>> and
97 =for apidoc Amn;||ENTER
98 Opening bracket on a callback. See C<L</LEAVE>> and L<perlcall>.
100 =for apidoc Amn;||LEAVE
101 Closing bracket on a callback. See C<L</ENTER>> and L<perlcall>.
103 =for apidoc Am;||ENTER_with_name|"name"
105 Same as C<L</ENTER>>, but when debugging is enabled it also associates the
106 given literal string with the new scope.
108 =for apidoc Am;||LEAVE_with_name|"name"
110 Same as C<L</LEAVE>>, but when debugging is enabled it first checks that the
111 scope has the given name. C<name> must be a literal string.
116 #define SAVETMPS Perl_savetmps(aTHX)
118 #define FREETMPS if (PL_tmps_ix > PL_tmps_floor) free_tmps()
124 DEBUG_SCOPE("ENTER") \
128 DEBUG_SCOPE("LEAVE") \
131 #define ENTER_with_name(name) \
134 if (PL_scopestack_name) \
135 PL_scopestack_name[PL_scopestack_ix-1] = ASSERT_IS_LITERAL(name);\
136 DEBUG_SCOPE("ENTER \"" name "\"") \
138 #define LEAVE_with_name(name) \
140 DEBUG_SCOPE("LEAVE \"" name "\"") \
141 if (PL_scopestack_name) { \
142 CLANG_DIAG_IGNORE_STMT(-Wstring-compare); \
143 assert(((char*)PL_scopestack_name[PL_scopestack_ix-1] \
144 == (char*)ASSERT_IS_LITERAL(name)) \
145 || strEQ(PL_scopestack_name[PL_scopestack_ix-1], name)); \
146 CLANG_DIAG_RESTORE_STMT; \
151 #define ENTER push_scope()
152 #define LEAVE pop_scope()
153 #define ENTER_with_name(name) ENTER
154 #define LEAVE_with_name(name) LEAVE
156 #define LEAVE_SCOPE(old) STMT_START { \
157 if (PL_savestack_ix > old) leave_scope(old); \
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))
180 #define SAVERCPV(s) save_rcpv((char**)&(s))
181 #define SAVEFREERCPV(s) save_freercpv(s)
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)
186 #if defined(PERL_CORE) || defined(PERL_EXT)
187 # define SAVE_FREE_REXC_STATE(p) \
188 save_pushptr((void *)(p), SAVEt_FREE_REXC_STATE)
191 #define SAVEDELETE(h,k,l) \
192 save_delete(MUTABLE_HV(h), (char*)(k), (I32)(l))
193 #define SAVEHDELETE(h,s) \
194 save_hdelete(MUTABLE_HV(h), (s))
195 #define SAVEADELETE(a,k) \
196 save_adelete(MUTABLE_AV(a), (SSize_t)(k))
197 #define SAVEDESTRUCTOR(f,p) \
198 save_destructor((DESTRUCTORFUNC_NOCONTEXT_t)(f), (void*)(p))
200 #define SAVEDESTRUCTOR_X(f,p) \
201 save_destructor_x((DESTRUCTORFUNC_t)(f), (void*)(p))
203 #define MORTALSVFUNC_X(f,sv) \
204 mortal_svfunc_x((SVFUNC_t)(f), sv)
206 #define MORTALDESTRUCTOR_SV(coderef,args) \
207 mortal_destructor_sv(coderef,args)
209 #define SAVESTACK_POS() \
212 SS_ADD_INT(PL_stack_sp - PL_stack_base); \
213 SS_ADD_UV(SAVEt_STACK_POS); \
217 #define SAVEOP() save_op()
219 #define SAVEHINTS() save_hints()
221 #define SAVECOMPPAD() save_pushptr(MUTABLE_SV(PL_comppad), SAVEt_COMPPAD)
223 #define SAVESWITCHSTACK(f,t) \
225 save_pushptrptr(MUTABLE_SV(f), MUTABLE_SV(t), SAVEt_SAVESWITCHSTACK); \
226 SWITCHSTACK((f),(t)); \
227 PL_curstackinfo->si_stack = (t); \
230 /* Note these are special, we can't just use a save_pushptrptr() on them
231 * as the target might change after a fork or thread start. */
232 #define SAVECOMPILEWARNINGS() save_pushptr(PL_compiling.cop_warnings, SAVEt_COMPILE_WARNINGS)
233 #define SAVECURCOPWARNINGS() save_pushptr(PL_curcop->cop_warnings, SAVEt_CURCOP_WARNINGS)
236 #define SAVEPARSER(p) save_pushptr((p), SAVEt_PARSER)
239 # define SAVECOPSTASH_FREE(c) SAVEIV((c)->cop_stashoff)
240 # define SAVECOPFILE_x(c) SAVEPPTR((c)->cop_file)
241 # define SAVECOPFILE(c) \
244 CopFILE_debug((c),"SAVECOPFILE",0); \
246 # define SAVECOPFILE_FREE_x(c) SAVERCPV((c)->cop_file)
247 # define SAVECOPFILE_FREE(c) \
249 SAVECOPFILE_FREE_x(c); \
250 CopFILE_debug((c),"SAVECOPFILE_FREE",0); \
253 # /* XXX not refcounted */
254 # define SAVECOPSTASH_FREE(c) SAVESPTR(CopSTASH(c))
255 # define SAVECOPFILE(c) SAVESPTR(CopFILEGV(c))
256 # define SAVECOPFILE_FREE(c) SAVEGENERICSV(CopFILEGV(c))
259 #define SAVECOPLINE(c) SAVEI32(CopLINE(c))
262 =for apidoc_section $stack
263 =for apidoc Am|SSize_t|SSNEW |Size_t size
264 =for apidoc_item | |SSNEWa |Size_t size|Size_t align
265 =for apidoc_item | |SSNEWat|Size_t size|type|Size_t align
266 =for apidoc_item | |SSNEWt |Size_t size|type
268 These each temporarily allocate data on the savestack, returning an SSize_t
269 index into the savestack, because a pointer would get broken if the savestack
270 is moved on reallocation. Use L</C<SSPTR>> to convert the returned index into
273 The forms differ in that plain C<SSNEW> allocates C<size> bytes;
274 C<SSNEWt> and C<SSNEWat> allocate C<size> objects, each of which is type
276 and <SSNEWa> and C<SSNEWat> make sure to align the new data to an C<align>
277 boundary. The most useful value for the alignment is likely to be
278 L</C<MEM_ALIGNBYTES>>. The alignment will be preserved through savestack
279 reallocation B<only> if realloc returns data aligned to a size divisible by
282 =for apidoc Am|type |SSPTR |SSize_t index|type
283 =for apidoc_item|type *|SSPTRt|SSize_t index|type
285 These convert the C<index> returned by L/<C<SSNEW>> and kin into actual pointers.
287 The difference is that C<SSPTR> casts the result to C<type>, and C<SSPTRt>
288 casts it to a pointer of that C<type>.
293 #define SSNEW(size) Perl_save_alloc(aTHX_ (size), 0)
294 #define SSNEWt(n,t) SSNEW((n)*sizeof(t))
295 #define SSNEWa(size,align) Perl_save_alloc(aTHX_ (size), \
296 (I32)(align - ((size_t)((caddr_t)&PL_savestack[PL_savestack_ix]) % align)) % align)
297 #define SSNEWat(n,t,align) SSNEWa((n)*sizeof(t), align)
299 #define SSPTR(off,type) (assert(sizeof(off) >= sizeof(SSize_t)), (type) ((char*)PL_savestack + off))
300 #define SSPTRt(off,type) (assert(sizeof(off) >= sizeof(SSize_t)), (type*) ((char*)PL_savestack + off))
302 #define save_freesv(op) save_pushptr((void *)(op), SAVEt_FREESV)
303 #define save_mortalizesv(op) save_pushptr((void *)(op), SAVEt_MORTALIZESV)
305 # define save_freeop(op) \
307 OP * const _o = (OP *)(op); \
308 assert(!_o->op_savefree); \
309 _o->op_savefree = 1; \
310 save_pushptr((void *)(_o), SAVEt_FREEOP); \
312 #define save_freepv(pv) save_pushptr((void *)(pv), SAVEt_FREEPV)
315 =for apidoc_section $callback
318 Implements C<SAVEOP>.
323 #define save_op() save_pushptr((void *)(PL_op), SAVEt_OP)
326 * ex: set ts=8 sts=4 sw=4 et: