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