This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Remove full stop in the 'try' feature heading
[perl5.git] / scope.h
1 /*    scope.h
2  *
3  *    Copyright (C) 1993, 1994, 1996, 1997, 1998, 1999, 2000, 2001,
4  *    2002, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
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
11 #include "scope_types.h"
12
13 #define SAVEf_SETMAGIC          1
14 #define SAVEf_KEEPOLDELEM       2
15
16 #define SAVE_TIGHT_SHIFT        6
17 #define SAVE_MASK               0x3F
18
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)
21
22 #ifndef SCOPE_SAVES_SIGNAL_MASK
23 #define SCOPE_SAVES_SIGNAL_MASK 0
24 #endif
25
26 /* the maximum number of entries that might be pushed using the SS_ADD*
27  * macros */
28 #define SS_MAXPUSH 4
29
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)
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))
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
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
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;     \
57     ANY *ssp = &PL_savestack[ix]
58
59 #define SS_ADD_END(need) \
60     assert((need) <= SS_MAXPUSH);                               \
61     ix += (need);                                               \
62     PL_savestack_ix = ix;                                       \
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);
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
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)
84
85
86 /*
87 =for apidoc_section $callback
88
89 =for apidoc Amn;||SAVETMPS
90 Opening bracket for temporaries on a callback.  See C<L</FREETMPS>> and
91 L<perlcall>.
92
93 =for apidoc Amn;||FREETMPS
94 Closing bracket for temporaries on a callback.  See C<L</SAVETMPS>> and
95 L<perlcall>.
96
97 =for apidoc Amn;||ENTER
98 Opening bracket on a callback.  See C<L</LEAVE>> and L<perlcall>.
99
100 =for apidoc Amn;||LEAVE
101 Closing bracket on a callback.  See C<L</ENTER>> and L<perlcall>.
102
103 =for apidoc Am;||ENTER_with_name|"name"
104
105 Same as C<L</ENTER>>, but when debugging is enabled it also associates the
106 given literal string with the new scope.
107
108 =for apidoc Am;||LEAVE_with_name|"name"
109
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.
112
113 =cut
114 */
115
116 #define SAVETMPS Perl_savetmps(aTHX)
117
118 #define FREETMPS if (PL_tmps_ix > PL_tmps_floor) free_tmps()
119
120 #ifdef DEBUGGING
121 #define ENTER                                                   \
122     STMT_START {                                                \
123         push_scope();                                           \
124         DEBUG_SCOPE("ENTER")                                    \
125     } STMT_END
126 #define LEAVE                                                   \
127     STMT_START {                                                \
128         DEBUG_SCOPE("LEAVE")                                    \
129         pop_scope();                                            \
130     } STMT_END
131 #define ENTER_with_name(name)                                           \
132     STMT_START {                                                        \
133         push_scope();                                                   \
134         if (PL_scopestack_name)                                         \
135             PL_scopestack_name[PL_scopestack_ix-1] = ASSERT_IS_LITERAL(name);\
136         DEBUG_SCOPE("ENTER \"" name "\"")                               \
137     } STMT_END
138 #define LEAVE_with_name(name)                                           \
139     STMT_START {                                                        \
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;                                    \
147         }                                                               \
148         pop_scope();                                                    \
149     } STMT_END
150 #else
151 #define ENTER push_scope()
152 #define LEAVE pop_scope()
153 #define ENTER_with_name(name) ENTER
154 #define LEAVE_with_name(name) LEAVE
155 #endif
156 #define LEAVE_SCOPE(old) STMT_START { \
157         if (PL_savestack_ix > old) leave_scope(old); \
158     } STMT_END
159
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 SAVERCPVFREE(s)             save_rcpv_free((char**)&(s))
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
185 #define SAVEDELETE(h,k,l) \
186           save_delete(MUTABLE_HV(h), (char*)(k), (I32)(l))
187 #define SAVEHDELETE(h,s) \
188           save_hdelete(MUTABLE_HV(h), (s))
189 #define SAVEADELETE(a,k) \
190           save_adelete(MUTABLE_AV(a), (SSize_t)(k))
191 #define SAVEDESTRUCTOR(f,p) \
192           save_destructor((DESTRUCTORFUNC_NOCONTEXT_t)(f), (void*)(p))
193
194 #define SAVEDESTRUCTOR_X(f,p) \
195           save_destructor_x((DESTRUCTORFUNC_t)(f), (void*)(p))
196
197 #define SAVESTACK_POS() \
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);                             \
203     } STMT_END
204
205 #define SAVEOP()        save_op()
206
207 #define SAVEHINTS()     save_hints()
208
209 #define SAVECOMPPAD() save_pushptr(MUTABLE_SV(PL_comppad), SAVEt_COMPPAD)
210
211 #define SAVESWITCHSTACK(f,t) \
212     STMT_START {                                        \
213         save_pushptrptr(MUTABLE_SV(f), MUTABLE_SV(t), SAVEt_SAVESWITCHSTACK); \
214         SWITCHSTACK((f),(t));                           \
215         PL_curstackinfo->si_stack = (t);                \
216     } STMT_END
217
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
223
224 #define SAVEPARSER(p) save_pushptr((p), SAVEt_PARSER)
225
226 #ifdef USE_ITHREADS
227 #  define SAVECOPSTASH_FREE(c)  SAVEIV((c)->cop_stashoff)
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
240 #else
241 #  /* XXX not refcounted */
242 #  define SAVECOPSTASH_FREE(c)  SAVESPTR(CopSTASH(c))
243 #  define SAVECOPFILE(c)        SAVESPTR(CopFILEGV(c))
244 #  define SAVECOPFILE_FREE(c)   SAVEGENERICSV(CopFILEGV(c))
245 #endif
246
247 #define SAVECOPLINE(c)          SAVEI32(CopLINE(c))
248
249 /*
250 =for apidoc_section $stack
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
255
256 These temporarily allocates data on the savestack, returning an SSize_t index into
257 the savestack, because a pointer would get broken if the savestack is moved on
258 reallocation.  Use L</C<SSPTR>> to convert the returned index into a pointer.
259
260 The forms differ in that plain C<SSNEW> allocates C<size> bytes;
261 C<SSNEWt> and C<SSNEWat> allocate C<size> objects, each of which is type
262 C<type>;
263 and <SSNEWa> and C<SSNEWat> make sure to align the new data to an C<align>
264 boundary.  The most useful value for the alignment is likely to be
265 L</C<MEM_ALIGNBYTES>>.  The alignment will be preserved through savestack
266 reallocation B<only> if realloc returns data aligned to a size divisible by
267 "align"!
268
269 =for apidoc   Am|type  |SSPTR |SSize_t index|type
270 =for apidoc_item|type *|SSPTRt|SSize_t index|type
271
272 These convert the C<index> returned by L/<C<SSNEW>> and kin into actual pointers.
273
274 The difference is that C<SSPTR> casts the result to C<type>, and C<SSPTRt>
275 casts it to a pointer of that C<type>.
276
277 =cut
278  */
279
280 #define SSNEW(size)             Perl_save_alloc(aTHX_ (size), 0)
281 #define SSNEWt(n,t)             SSNEW((n)*sizeof(t))
282 #define SSNEWa(size,align)      Perl_save_alloc(aTHX_ (size), \
283     (I32)(align - ((size_t)((caddr_t)&PL_savestack[PL_savestack_ix]) % align)) % align)
284 #define SSNEWat(n,t,align)      SSNEWa((n)*sizeof(t), align)
285
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))
288
289 #define save_freesv(op)         save_pushptr((void *)(op), SAVEt_FREESV)
290 #define save_mortalizesv(op)    save_pushptr((void *)(op), SAVEt_MORTALIZESV)
291
292 # define save_freeop(op)                    \
293 STMT_START {                                 \
294       OP * const _o = (OP *)(op);             \
295       assert(!_o->op_savefree);               \
296       _o->op_savefree = 1;                     \
297       save_pushptr((void *)(_o), SAVEt_FREEOP); \
298     } STMT_END
299 #define save_freepv(pv)         save_pushptr((void *)(pv), SAVEt_FREEPV)
300
301 /*
302 =for apidoc_section $callback
303 =for apidoc save_op
304
305 Implements C<SAVEOP>.
306
307 =cut
308  */
309
310 #define save_op()               save_pushptr((void *)(PL_op), SAVEt_OP)
311
312 /*
313  * ex: set ts=8 sts=4 sw=4 et:
314  */