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