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