This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
document that it is the operator that determines the operation
[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
41
42#define SAVEt_ARG1_MAX 20
43
44/* two args */
45
46#define SAVEt_ADELETE 21
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_STACK_CXPOS 42
68#define SAVEt_SV 43
69#define SAVEt_SVREF 44
70#define SAVEt_VPTR 45
71
72#define SAVEt_ARG2_MAX 45
73
74/* three args */
75
76#define SAVEt_AELEM 46
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
79072805 82
af7df257 83#define SAVEf_SETMAGIC 1
75d34a09 84#define SAVEf_KEEPOLDELEM 2
af7df257 85
c6bf6a65
NC
86#define SAVE_TIGHT_SHIFT 6
87#define SAVE_MASK 0x3F
88
91d1c79f 89#define save_aelem(av,idx,sptr) save_aelem_flags(av,idx,sptr,SAVEf_SETMAGIC)
af7df257
CS
90#define save_helem(hv,key,sptr) save_helem_flags(hv,key,sptr,SAVEf_SETMAGIC)
91
b03c0a3a 92#ifndef SCOPE_SAVES_SIGNAL_MASK
1b266415 93#define SCOPE_SAVES_SIGNAL_MASK 0
b03c0a3a
NIS
94#endif
95
a3444cc5
DM
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 (PL_savestack_ix + (I32)(need) + SS_MAXPUSH > PL_savestack_max) savestack_grow()
101#define SSGROW(need) if (PL_savestack_ix + (I32)(need) + SS_MAXPUSH > PL_savestack_max) savestack_grow_cnt(need + SS_MAXPUSH)
3280af22
NIS
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))
9febdf04 104#define SSPUSHBOOL(p) (PL_savestack[PL_savestack_ix++].any_bool = (p))
3280af22 105#define SSPUSHIV(i) (PL_savestack[PL_savestack_ix++].any_iv = (IV)(i))
c6bf6a65 106#define SSPUSHUV(u) (PL_savestack[PL_savestack_ix++].any_uv = (UV)(u))
3280af22
NIS
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))
c76ac1ee 109#define SSPUSHDXPTR(p) (PL_savestack[PL_savestack_ix++].any_dxptr = (p))
a3444cc5
DM
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
120 *
121 * These are for internal core use only and are subject to change */
122
123#define dSS_ADD \
124 I32 ix = PL_savestack_ix; \
125 ANY *ssp = &PL_savestack[ix];
126
127#define SS_ADD_END(need) \
128 assert((need) <= SS_MAXPUSH); \
129 ix += (need); \
130 PL_savestack_ix = ix; \
131 assert(ix <= PL_savestack_max); \
132 if ((ix + SS_MAXPUSH) > PL_savestack_max) savestack_grow(); \
133 assert(PL_savestack_ix + SS_MAXPUSH <= PL_savestack_max);
134
135#define SS_ADD_INT(i) ((ssp++)->any_i32 = (I32)(i))
136#define SS_ADD_LONG(i) ((ssp++)->any_long = (long)(i))
137#define SS_ADD_BOOL(p) ((ssp++)->any_bool = (p))
138#define SS_ADD_IV(i) ((ssp++)->any_iv = (IV)(i))
139#define SS_ADD_UV(u) ((ssp++)->any_uv = (UV)(u))
140#define SS_ADD_PTR(p) ((ssp++)->any_ptr = (void*)(p))
141#define SS_ADD_DPTR(p) ((ssp++)->any_dptr = (p))
142#define SS_ADD_DXPTR(p) ((ssp++)->any_dxptr = (p))
143
3280af22
NIS
144#define SSPOPINT (PL_savestack[--PL_savestack_ix].any_i32)
145#define SSPOPLONG (PL_savestack[--PL_savestack_ix].any_long)
9febdf04 146#define SSPOPBOOL (PL_savestack[--PL_savestack_ix].any_bool)
3280af22 147#define SSPOPIV (PL_savestack[--PL_savestack_ix].any_iv)
c6bf6a65 148#define SSPOPUV (PL_savestack[--PL_savestack_ix].any_uv)
3280af22
NIS
149#define SSPOPPTR (PL_savestack[--PL_savestack_ix].any_ptr)
150#define SSPOPDPTR (PL_savestack[--PL_savestack_ix].any_dptr)
c76ac1ee 151#define SSPOPDXPTR (PL_savestack[--PL_savestack_ix].any_dxptr)
8990e307 152
a3444cc5 153
954c1994 154/*
ccfc67b7
JH
155=head1 Callback Functions
156
954c1994
GS
157=for apidoc Ams||SAVETMPS
158Opening bracket for temporaries on a callback. See C<FREETMPS> and
159L<perlcall>.
160
161=for apidoc Ams||FREETMPS
162Closing bracket for temporaries on a callback. See C<SAVETMPS> and
163L<perlcall>.
164
165=for apidoc Ams||ENTER
166Opening bracket on a callback. See C<LEAVE> and L<perlcall>.
167
168=for apidoc Ams||LEAVE
169Closing bracket on a callback. See C<ENTER> and L<perlcall>.
170
d343c3ef
GG
171=over
172
173=item ENTER_with_name(name)
174
175Same as C<ENTER>, but when debugging is enabled it also associates the
176given literal string with the new scope.
177
178=item LEAVE_with_name(name)
179
180Same as C<LEAVE>, but when debugging is enabled it first checks that the
181scope has the given name. Name must be a literal string.
182
183=back
184
954c1994
GS
185=cut
186*/
187
3280af22
NIS
188#define SAVETMPS save_int((int*)&PL_tmps_floor), PL_tmps_floor = PL_tmps_ix
189#define FREETMPS if (PL_tmps_ix > PL_tmps_floor) free_tmps()
a0d0e21e 190
f46d017c
GS
191#ifdef DEBUGGING
192#define ENTER \
193 STMT_START { \
194 push_scope(); \
b4ab917c 195 DEBUG_SCOPE("ENTER") \
f46d017c
GS
196 } STMT_END
197#define LEAVE \
198 STMT_START { \
b4ab917c 199 DEBUG_SCOPE("LEAVE") \
f46d017c
GS
200 pop_scope(); \
201 } STMT_END
d343c3ef
GG
202#define ENTER_with_name(name) \
203 STMT_START { \
204 push_scope(); \
a09dc31d
NC
205 if (PL_scopestack_name) \
206 PL_scopestack_name[PL_scopestack_ix-1] = name; \
d343c3ef
GG
207 DEBUG_SCOPE("ENTER \"" name "\"") \
208 } STMT_END
209#define LEAVE_with_name(name) \
210 STMT_START { \
211 DEBUG_SCOPE("LEAVE \"" name "\"") \
a09dc31d
NC
212 if (PL_scopestack_name) { \
213 assert(((char*)PL_scopestack_name[PL_scopestack_ix-1] \
214 == (char*)name) \
215 || strEQ(PL_scopestack_name[PL_scopestack_ix-1], name)); \
216 } \
d343c3ef
GG
217 pop_scope(); \
218 } STMT_END
f46d017c 219#else
a0d0e21e
LW
220#define ENTER push_scope()
221#define LEAVE pop_scope()
d343c3ef
GG
222#define ENTER_with_name(name) ENTER
223#define LEAVE_with_name(name) LEAVE
f46d017c 224#endif
d699ecb7
FC
225#define LEAVE_SCOPE(old) STMT_START { \
226 if (PL_savestack_ix > old) leave_scope(old); \
227 } STMT_END
a0d0e21e 228
3f26ef39
NC
229#define SAVEI8(i) save_I8((I8*)&(i))
230#define SAVEI16(i) save_I16((I16*)&(i))
231#define SAVEI32(i) save_I32((I32*)&(i))
232#define SAVEINT(i) save_int((int*)&(i))
233#define SAVEIV(i) save_iv((IV*)&(i))
234#define SAVELONG(l) save_long((long*)&(l))
02644217 235#define SAVEBOOL(b) save_bool(&(b))
55497cff 236#define SAVESPTR(s) save_sptr((SV**)&(s))
3f26ef39 237#define SAVEPPTR(s) save_pptr((char**)&(s))
57b2e452 238#define SAVEVPTR(s) save_vptr((void*)&(s))
09edbca0 239#define SAVEPADSVANDMORTALIZE(s) save_padsv_and_mortalize(s)
b1bc3f34
NC
240#define SAVEFREESV(s) save_freesv(MUTABLE_SV(s))
241#define SAVEMORTALIZESV(s) save_mortalizesv(MUTABLE_SV(s))
3f26ef39
NC
242#define SAVEFREEOP(o) save_freeop((OP*)(o))
243#define SAVEFREEPV(p) save_freepv((char*)(p))
244#define SAVECLEARSV(sv) save_clearsv((SV**)&(sv))
b9d12d37 245#define SAVEGENERICSV(s) save_generic_svref((SV**)&(s))
f4dd75d9 246#define SAVEGENERICPV(s) save_generic_pvref((char**)&(s))
05ec9bb3 247#define SAVESHAREDPV(s) save_shared_pvref((char**)&(s))
14f338dc 248#define SAVESETSVFLAGS(sv,mask,val) save_set_svflags(sv,mask,val)
3987a177 249#define SAVEFREECOPHH(h) save_pushptr((void *)(h), SAVEt_FREECOPHH)
55497cff 250#define SAVEDELETE(h,k,l) \
dbebbdb4 251 save_delete(MUTABLE_HV(h), (char*)(k), (I32)(l))
af097752
VP
252#define SAVEHDELETE(h,s) \
253 save_hdelete(MUTABLE_HV(h), (s))
c68ec7a9
VP
254#define SAVEADELETE(a,k) \
255 save_adelete(MUTABLE_AV(a), (I32)(k))
55497cff 256#define SAVEDESTRUCTOR(f,p) \
3f26ef39 257 save_destructor((DESTRUCTORFUNC_NOCONTEXT_t)(f), (void*)(p))
c76ac1ee
GS
258
259#define SAVEDESTRUCTOR_X(f,p) \
3f26ef39 260 save_destructor_x((DESTRUCTORFUNC_t)(f), (void*)(p))
25eaa213
GS
261
262#define SAVESTACK_POS() \
a3444cc5
DM
263 STMT_START { \
264 dSS_ADD; \
265 SS_ADD_INT(PL_stack_sp - PL_stack_base); \
266 SS_ADD_UV(SAVEt_STACK_POS); \
267 SS_ADD_END(2); \
25eaa213
GS
268 } STMT_END
269
462e5cf6 270#define SAVEOP() save_op()
25eaa213 271
da8315f8 272#define SAVEHINTS() save_hints()
354992b1 273
9c47725a 274#define SAVECOMPPAD() save_pushptr(MUTABLE_SV(PL_comppad), SAVEt_COMPPAD)
a9332b4a 275
8b7059b1
DM
276#define SAVESWITCHSTACK(f,t) \
277 STMT_START { \
dfcd3de5 278 save_pushptrptr(MUTABLE_SV(f), MUTABLE_SV(t), SAVEt_SAVESWITCHSTACK); \
8b7059b1
DM
279 SWITCHSTACK((f),(t)); \
280 PL_curstackinfo->si_stack = (t); \
281 } STMT_END
282
c88c823b 283/* Need to do the cop warnings like this, rather than a "SAVEFREESHAREDPV",
72dc9ed5
NC
284 because realloc() means that the value can actually change. Possibly
285 could have done savefreesharedpvREF, but this way actually seems cleaner,
286 as it simplifies the code that does the saves, and reduces the load on the
287 save stack. */
747e2fae 288#define SAVECOMPILEWARNINGS() save_pushptr(PL_compiling.cop_warnings, SAVEt_COMPILE_WARNINGS)
fc15ae8f 289
ea8d6ae1 290#define SAVESTACK_CXPOS() \
a3444cc5
DM
291 STMT_START { \
292 dSS_ADD; \
293 SS_ADD_INT(cxstack[cxstack_ix].blk_oldsp); \
294 SS_ADD_INT(cxstack_ix); \
295 SS_ADD_UV(SAVEt_STACK_CXPOS); \
296 SS_ADD_END(3); \
ea8d6ae1
DB
297 } STMT_END
298
747e2fae 299#define SAVEPARSER(p) save_pushptr((p), SAVEt_PARSER)
7c197c94 300
57843af0 301#ifdef USE_ITHREADS
d4d03940 302# define SAVECOPSTASH_FREE(c) SAVEIV((c)->cop_stashoff)
f4dd75d9 303# define SAVECOPFILE(c) SAVEPPTR(CopFILE(c))
05ec9bb3 304# define SAVECOPFILE_FREE(c) SAVESHAREDPV(CopFILE(c))
57843af0 305#else
61b3e50d
FC
306# /* XXX not refcounted */
307# define SAVECOPSTASH_FREE(c) SAVESPTR(CopSTASH(c))
f4dd75d9
GS
308# define SAVECOPFILE(c) SAVESPTR(CopFILEGV(c))
309# define SAVECOPFILE_FREE(c) SAVEGENERICSV(CopFILEGV(c))
57843af0
GS
310#endif
311
dea28490 312#define SAVECOPLINE(c) SAVEI32(CopLINE(c))
57843af0 313
455ece5e
AD
314/* SSNEW() temporarily allocates a specified number of bytes of data on the
315 * savestack. It returns an integer index into the savestack, because a
316 * pointer would get broken if the savestack is moved on reallocation.
317 * SSNEWa() works like SSNEW(), but also aligns the data to the specified
318 * number of bytes. MEM_ALIGNBYTES is perhaps the most useful. The
486ec47a 319 * alignment will be preserved through savestack reallocation *only* if
a0288114 320 * realloc returns data aligned to a size divisible by "align"!
455ece5e
AD
321 *
322 * SSPTR() converts the index returned by SSNEW/SSNEWa() into a pointer.
323 */
324
f2b2c1a7 325#define SSNEW(size) Perl_save_alloc(aTHX_ (size), 0)
02db2b7b 326#define SSNEWt(n,t) SSNEW((n)*sizeof(t))
f2b2c1a7 327#define SSNEWa(size,align) Perl_save_alloc(aTHX_ (size), \
6b99f28a 328 (I32)(align - ((size_t)((caddr_t)&PL_savestack[PL_savestack_ix]) % align)) % align)
02db2b7b 329#define SSNEWat(n,t,align) SSNEWa((n)*sizeof(t), align)
455ece5e 330
02db2b7b
IZ
331#define SSPTR(off,type) ((type) ((char*)PL_savestack + off))
332#define SSPTRt(off,type) ((type*) ((char*)PL_savestack + off))
455ece5e 333
2fd8beea
NC
334#define save_freesv(op) save_pushptr((void *)(op), SAVEt_FREESV)
335#define save_mortalizesv(op) save_pushptr((void *)(op), SAVEt_MORTALIZESV)
6702ba93 336
bb38a9e0 337# define save_freeop(op) \
6702ba93 338STMT_START { \
bb38a9e0 339 OP * const _o = (OP *)(op); \
6c7ae946 340 assert(!_o->op_savefree); \
bb38a9e0
FC
341 _o->op_savefree = 1; \
342 save_pushptr((void *)(_o), SAVEt_FREEOP); \
6702ba93 343 } STMT_END
2fd8beea
NC
344#define save_freepv(pv) save_pushptr((void *)(pv), SAVEt_FREEPV)
345#define save_op() save_pushptr((void *)(PL_op), SAVEt_OP)
346
e9a8c099
MHM
347/*
348 * Local variables:
349 * c-indentation-style: bsd
350 * c-basic-offset: 4
14d04a33 351 * indent-tabs-mode: nil
e9a8c099
MHM
352 * End:
353 *
14d04a33 354 * ex: set ts=8 sts=4 sw=4 et:
e9a8c099 355 */