This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Comment why hexfp is set here, not earlier.
[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 /* *** 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
19 /*** SPARE                      4 ***/
20
21 #define SAVEt_ARG0_MAX          4
22
23 /* one arg */
24
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
41 #define SAVEt_READONLY_OFF      21
42
43 #define SAVEt_ARG1_MAX          21
44
45 /* two args */
46
47 #define SAVEt_APTR              22
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
67 #define SAVEt_STRLEN            42
68 #define SAVEt_SV                43
69 #define SAVEt_SVREF             44
70 #define SAVEt_VPTR              45
71 #define SAVEt_ADELETE           46
72
73 #define SAVEt_ARG2_MAX          46
74
75 /* three args */
76
77 #define SAVEt_DELETE            47
78 #define SAVEt_HELEM             48
79 #define SAVEt_PADSV_AND_MORTALIZE 49
80 #define SAVEt_SET_SVFLAGS       50
81 #define SAVEt_GVSLOT            51
82 #define SAVEt_AELEM             52
83
84 #define SAVEf_SETMAGIC          1
85 #define SAVEf_KEEPOLDELEM       2
86
87 #define SAVE_TIGHT_SHIFT        6
88 #define SAVE_MASK               0x3F
89
90 #define save_aelem(av,idx,sptr) save_aelem_flags(av,idx,sptr,SAVEf_SETMAGIC)
91 #define save_helem(hv,key,sptr) save_helem_flags(hv,key,sptr,SAVEf_SETMAGIC)
92
93 #ifndef SCOPE_SAVES_SIGNAL_MASK
94 #define SCOPE_SAVES_SIGNAL_MASK 0
95 #endif
96
97 /* the maximum number of entries that might be pushed using the SS_ADD*
98  * macros */
99 #define SS_MAXPUSH 4
100
101 #define SSCHECK(need) if (UNLIKELY(PL_savestack_ix + (I32)(need) + SS_MAXPUSH > PL_savestack_max)) savestack_grow()
102 #define SSGROW(need) if (UNLIKELY(PL_savestack_ix + (I32)(need) + SS_MAXPUSH > PL_savestack_max)) savestack_grow_cnt(need + SS_MAXPUSH)
103 #define SSPUSHINT(i) (PL_savestack[PL_savestack_ix++].any_i32 = (I32)(i))
104 #define SSPUSHLONG(i) (PL_savestack[PL_savestack_ix++].any_long = (long)(i))
105 #define SSPUSHBOOL(p) (PL_savestack[PL_savestack_ix++].any_bool = (p))
106 #define SSPUSHIV(i) (PL_savestack[PL_savestack_ix++].any_iv = (IV)(i))
107 #define SSPUSHUV(u) (PL_savestack[PL_savestack_ix++].any_uv = (UV)(u))
108 #define SSPUSHPTR(p) (PL_savestack[PL_savestack_ix++].any_ptr = (void*)(p))
109 #define SSPUSHDPTR(p) (PL_savestack[PL_savestack_ix++].any_dptr = (p))
110 #define SSPUSHDXPTR(p) (PL_savestack[PL_savestack_ix++].any_dxptr = (p))
111
112 /* SS_ADD*: newer, faster versions of the above. Don't mix the two sets of
113  * macros. These are fast because they save reduce accesses to the PL_
114  * vars and move the size check to the end. Doing the check last means
115  * that values in registers will have been pushed and no longer needed, so
116  * don't need saving around the call to grow. Also, tail-call elimination
117  * of the grow() can be done. These changes reduce the code of something
118  * like save_pushptrptr() to half its former size.
119  * Of course, doing the size check *after* pushing means we must always
120  * ensure there are SS_MAXPUSH free slots on the savestack
121  *
122  * These are for internal core use only and are subject to change */
123
124 #define dSS_ADD \
125     I32 ix = PL_savestack_ix;     \
126     ANY *ssp = &PL_savestack[ix]
127
128 #define SS_ADD_END(need) \
129     assert((need) <= SS_MAXPUSH);                               \
130     ix += (need);                                               \
131     PL_savestack_ix = ix;                                       \
132     assert(ix <= PL_savestack_max);                             \
133     if (UNLIKELY((ix + SS_MAXPUSH) > PL_savestack_max)) savestack_grow(); \
134     assert(PL_savestack_ix + SS_MAXPUSH <= PL_savestack_max);
135
136 #define SS_ADD_INT(i)   ((ssp++)->any_i32 = (I32)(i))
137 #define SS_ADD_LONG(i)  ((ssp++)->any_long = (long)(i))
138 #define SS_ADD_BOOL(p)  ((ssp++)->any_bool = (p))
139 #define SS_ADD_IV(i)    ((ssp++)->any_iv = (IV)(i))
140 #define SS_ADD_UV(u)    ((ssp++)->any_uv = (UV)(u))
141 #define SS_ADD_PTR(p)   ((ssp++)->any_ptr = (void*)(p))
142 #define SS_ADD_DPTR(p)  ((ssp++)->any_dptr = (p))
143 #define SS_ADD_DXPTR(p) ((ssp++)->any_dxptr = (p))
144
145 #define SSPOPINT (PL_savestack[--PL_savestack_ix].any_i32)
146 #define SSPOPLONG (PL_savestack[--PL_savestack_ix].any_long)
147 #define SSPOPBOOL (PL_savestack[--PL_savestack_ix].any_bool)
148 #define SSPOPIV (PL_savestack[--PL_savestack_ix].any_iv)
149 #define SSPOPUV (PL_savestack[--PL_savestack_ix].any_uv)
150 #define SSPOPPTR (PL_savestack[--PL_savestack_ix].any_ptr)
151 #define SSPOPDPTR (PL_savestack[--PL_savestack_ix].any_dptr)
152 #define SSPOPDXPTR (PL_savestack[--PL_savestack_ix].any_dxptr)
153
154
155 /*
156 =head1 Callback Functions
157
158 =for apidoc Ams||SAVETMPS
159 Opening bracket for temporaries on a callback.  See C<FREETMPS> and
160 L<perlcall>.
161
162 =for apidoc Ams||FREETMPS
163 Closing bracket for temporaries on a callback.  See C<SAVETMPS> and
164 L<perlcall>.
165
166 =for apidoc Ams||ENTER
167 Opening bracket on a callback.  See C<LEAVE> and L<perlcall>.
168
169 =for apidoc Ams||LEAVE
170 Closing bracket on a callback.  See C<ENTER> and L<perlcall>.
171
172 =over
173
174 =item ENTER_with_name(name)
175
176 Same as C<ENTER>, but when debugging is enabled it also associates the
177 given literal string with the new scope.
178
179 =item LEAVE_with_name(name)
180
181 Same as C<LEAVE>, but when debugging is enabled it first checks that the
182 scope has the given name. Name must be a literal string.
183
184 =back
185
186 =cut
187 */
188
189 #define SAVETMPS Perl_save_strlen(aTHX_ (STRLEN *)&PL_tmps_floor), \
190                  PL_tmps_floor = PL_tmps_ix
191 #define FREETMPS if (PL_tmps_ix > PL_tmps_floor) free_tmps()
192
193 #ifdef DEBUGGING
194 #define ENTER                                                   \
195     STMT_START {                                                \
196         push_scope();                                           \
197         DEBUG_SCOPE("ENTER")                                    \
198     } STMT_END
199 #define LEAVE                                                   \
200     STMT_START {                                                \
201         DEBUG_SCOPE("LEAVE")                                    \
202         pop_scope();                                            \
203     } STMT_END
204 #define ENTER_with_name(name)                                           \
205     STMT_START {                                                        \
206         push_scope();                                                   \
207         if (PL_scopestack_name)                                         \
208             PL_scopestack_name[PL_scopestack_ix-1] = name;              \
209         DEBUG_SCOPE("ENTER \"" name "\"")                               \
210     } STMT_END
211 #define LEAVE_with_name(name)                                           \
212     STMT_START {                                                        \
213         DEBUG_SCOPE("LEAVE \"" name "\"")                               \
214         if (PL_scopestack_name) {                                       \
215             assert(((char*)PL_scopestack_name[PL_scopestack_ix-1]       \
216                         == (char*)name)                                 \
217                     || strEQ(PL_scopestack_name[PL_scopestack_ix-1], name));        \
218         }                                                               \
219         pop_scope();                                                    \
220     } STMT_END
221 #else
222 #define ENTER push_scope()
223 #define LEAVE pop_scope()
224 #define ENTER_with_name(name) ENTER
225 #define LEAVE_with_name(name) LEAVE
226 #endif
227 #define LEAVE_SCOPE(old) STMT_START { \
228         if (PL_savestack_ix > old) leave_scope(old); \
229     } STMT_END
230
231 #define SAVEI8(i)       save_I8((I8*)&(i))
232 #define SAVEI16(i)      save_I16((I16*)&(i))
233 #define SAVEI32(i)      save_I32((I32*)&(i))
234 #define SAVEINT(i)      save_int((int*)&(i))
235 #define SAVEIV(i)       save_iv((IV*)&(i))
236 #define SAVELONG(l)     save_long((long*)&(l))
237 #define SAVEBOOL(b)     save_bool(&(b))
238 #define SAVESPTR(s)     save_sptr((SV**)&(s))
239 #define SAVEPPTR(s)     save_pptr((char**)&(s))
240 #define SAVEVPTR(s)     save_vptr((void*)&(s))
241 #define SAVEPADSVANDMORTALIZE(s)        save_padsv_and_mortalize(s)
242 #define SAVEFREESV(s)   save_freesv(MUTABLE_SV(s))
243 #define SAVEMORTALIZESV(s)      save_mortalizesv(MUTABLE_SV(s))
244 #define SAVEFREEOP(o)   save_freeop((OP*)(o))
245 #define SAVEFREEPV(p)   save_freepv((char*)(p))
246 #define SAVECLEARSV(sv) save_clearsv((SV**)&(sv))
247 #define SAVEGENERICSV(s)        save_generic_svref((SV**)&(s))
248 #define SAVEGENERICPV(s)        save_generic_pvref((char**)&(s))
249 #define SAVESHAREDPV(s)         save_shared_pvref((char**)&(s))
250 #define SAVESETSVFLAGS(sv,mask,val)     save_set_svflags(sv,mask,val)
251 #define SAVEFREECOPHH(h)        save_pushptr((void *)(h), SAVEt_FREECOPHH)
252 #define SAVEDELETE(h,k,l) \
253           save_delete(MUTABLE_HV(h), (char*)(k), (I32)(l))
254 #define SAVEHDELETE(h,s) \
255           save_hdelete(MUTABLE_HV(h), (s))
256 #define SAVEADELETE(a,k) \
257           save_adelete(MUTABLE_AV(a), (SSize_t)(k))
258 #define SAVEDESTRUCTOR(f,p) \
259           save_destructor((DESTRUCTORFUNC_NOCONTEXT_t)(f), (void*)(p))
260
261 #define SAVEDESTRUCTOR_X(f,p) \
262           save_destructor_x((DESTRUCTORFUNC_t)(f), (void*)(p))
263
264 #define SAVESTACK_POS() \
265     STMT_START {                                   \
266         dSS_ADD;                                   \
267         SS_ADD_INT(PL_stack_sp - PL_stack_base);   \
268         SS_ADD_UV(SAVEt_STACK_POS);                \
269         SS_ADD_END(2);                             \
270     } STMT_END
271
272 #define SAVEOP()        save_op()
273
274 #define SAVEHINTS()     save_hints()
275
276 #define SAVECOMPPAD() save_pushptr(MUTABLE_SV(PL_comppad), SAVEt_COMPPAD)
277
278 #define SAVESWITCHSTACK(f,t) \
279     STMT_START {                                        \
280         save_pushptrptr(MUTABLE_SV(f), MUTABLE_SV(t), SAVEt_SAVESWITCHSTACK); \
281         SWITCHSTACK((f),(t));                           \
282         PL_curstackinfo->si_stack = (t);                \
283     } STMT_END
284
285 /* Need to do the cop warnings like this, rather than a "SAVEFREESHAREDPV",
286    because realloc() means that the value can actually change. Possibly
287    could have done savefreesharedpvREF, but this way actually seems cleaner,
288    as it simplifies the code that does the saves, and reduces the load on the
289    save stack.  */
290 #define SAVECOMPILEWARNINGS() save_pushptr(PL_compiling.cop_warnings, SAVEt_COMPILE_WARNINGS)
291
292 #define SAVEPARSER(p) save_pushptr((p), SAVEt_PARSER)
293
294 #ifdef USE_ITHREADS
295 #  define SAVECOPSTASH_FREE(c)  SAVEIV((c)->cop_stashoff)
296 #  define SAVECOPFILE(c)        SAVEPPTR(CopFILE(c))
297 #  define SAVECOPFILE_FREE(c)   SAVESHAREDPV(CopFILE(c))
298 #else
299 #  /* XXX not refcounted */
300 #  define SAVECOPSTASH_FREE(c)  SAVESPTR(CopSTASH(c))
301 #  define SAVECOPFILE(c)        SAVESPTR(CopFILEGV(c))
302 #  define SAVECOPFILE_FREE(c)   SAVEGENERICSV(CopFILEGV(c))
303 #endif
304
305 #define SAVECOPLINE(c)          SAVEI32(CopLINE(c))
306
307 /* SSNEW() temporarily allocates a specified number of bytes of data on the
308  * savestack.  It returns an integer index into the savestack, because a
309  * pointer would get broken if the savestack is moved on reallocation.
310  * SSNEWa() works like SSNEW(), but also aligns the data to the specified
311  * number of bytes.  MEM_ALIGNBYTES is perhaps the most useful.  The
312  * alignment will be preserved through savestack reallocation *only* if
313  * realloc returns data aligned to a size divisible by "align"!
314  *
315  * SSPTR() converts the index returned by SSNEW/SSNEWa() into a pointer.
316  */
317
318 #define SSNEW(size)             Perl_save_alloc(aTHX_ (size), 0)
319 #define SSNEWt(n,t)             SSNEW((n)*sizeof(t))
320 #define SSNEWa(size,align)      Perl_save_alloc(aTHX_ (size), \
321     (I32)(align - ((size_t)((caddr_t)&PL_savestack[PL_savestack_ix]) % align)) % align)
322 #define SSNEWat(n,t,align)      SSNEWa((n)*sizeof(t), align)
323
324 #define SSPTR(off,type)         ((type)  ((char*)PL_savestack + off))
325 #define SSPTRt(off,type)        ((type*) ((char*)PL_savestack + off))
326
327 #define save_freesv(op)         save_pushptr((void *)(op), SAVEt_FREESV)
328 #define save_mortalizesv(op)    save_pushptr((void *)(op), SAVEt_MORTALIZESV)
329
330 # define save_freeop(op)                    \
331 STMT_START {                                 \
332       OP * const _o = (OP *)(op);             \
333       assert(!_o->op_savefree);               \
334       _o->op_savefree = 1;                     \
335       save_pushptr((void *)(_o), SAVEt_FREEOP); \
336     } STMT_END
337 #define save_freepv(pv)         save_pushptr((void *)(pv), SAVEt_FREEPV)
338 #define save_op()               save_pushptr((void *)(PL_op), SAVEt_OP)
339
340 /*
341  * Local variables:
342  * c-indentation-style: bsd
343  * c-basic-offset: 4
344  * indent-tabs-mode: nil
345  * End:
346  *
347  * ex: set ts=8 sts=4 sw=4 et:
348  */