Commit | Line | Data |
---|---|---|
dd2155a4 DM |
1 | /* pad.h |
2 | * | |
45ca242c | 3 | * Copyright (C) 2002, 2003, 2005, by Larry Wall and others |
dd2155a4 DM |
4 | * |
5 | * You may distribute under the terms of either the GNU General Public | |
6 | * License or the Artistic License, as specified in the README file. | |
7 | * | |
8 | * This file defines the types and macros associated with the API for | |
9 | * manipulating scratchpads, which are used by perl to store lexical | |
10 | * variables, op targets and constants. | |
11 | */ | |
12 | ||
13 | ||
14 | ||
15 | ||
16 | /* a padlist is currently just an AV; but that might change, | |
17 | * so hide the type. Ditto a pad. */ | |
18 | ||
19 | typedef AV PADLIST; | |
f3548bdc | 20 | typedef AV PAD; |
dd2155a4 DM |
21 | |
22 | ||
23 | /* offsets within a pad */ | |
24 | ||
25 | #if PTRSIZE == 4 | |
26 | typedef U32TYPE PADOFFSET; | |
27 | #else | |
28 | # if PTRSIZE == 8 | |
29 | typedef U64TYPE PADOFFSET; | |
30 | # endif | |
31 | #endif | |
32 | #define NOT_IN_PAD ((PADOFFSET) -1) | |
809abb02 NC |
33 | |
34 | /* B.xs needs these for the benefit of B::Deparse */ | |
35 | /* Low range end is exclusive (valid from the cop seq after this one) */ | |
809abb02 | 36 | /* High range end is inclusive (valid up to this cop seq) */ |
809abb02 | 37 | |
3441fb63 NC |
38 | #if defined (DEBUGGING) && defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN) |
39 | # define COP_SEQ_RANGE_LOW(sv) \ | |
40 | (({ SV *const _svi = (SV *) (sv); \ | |
41 | assert(SvTYPE(_svi) == SVt_NV || SvTYPE(_svi) >= SVt_PVNV); \ | |
42 | assert(SvTYPE(_svi) != SVt_PVAV); \ | |
43 | assert(SvTYPE(_svi) != SVt_PVHV); \ | |
44 | assert(SvTYPE(_svi) != SVt_PVCV); \ | |
45 | assert(SvTYPE(_svi) != SVt_PVFM); \ | |
46 | assert(!isGV_with_GP(_svi)); \ | |
47 | ((XPVNV*) SvANY(_svi))->xnv_u.xpad_cop_seq.xlow; \ | |
48 | })) | |
49 | # define COP_SEQ_RANGE_HIGH(sv) \ | |
50 | (({ SV *const _svi = (SV *) (sv); \ | |
51 | assert(SvTYPE(_svi) == SVt_NV || SvTYPE(_svi) >= SVt_PVNV); \ | |
52 | assert(SvTYPE(_svi) != SVt_PVAV); \ | |
53 | assert(SvTYPE(_svi) != SVt_PVHV); \ | |
54 | assert(SvTYPE(_svi) != SVt_PVCV); \ | |
55 | assert(SvTYPE(_svi) != SVt_PVFM); \ | |
56 | assert(!isGV_with_GP(_svi)); \ | |
57 | ((XPVNV*) SvANY(_svi))->xnv_u.xpad_cop_seq.xhigh; \ | |
58 | })) | |
59 | # define PARENT_PAD_INDEX(sv) \ | |
60 | (({ SV *const _svi = (SV *) (sv); \ | |
61 | assert(SvTYPE(_svi) == SVt_NV || SvTYPE(_svi) >= SVt_PVNV); \ | |
62 | assert(SvTYPE(_svi) != SVt_PVAV); \ | |
63 | assert(SvTYPE(_svi) != SVt_PVHV); \ | |
64 | assert(SvTYPE(_svi) != SVt_PVCV); \ | |
65 | assert(SvTYPE(_svi) != SVt_PVFM); \ | |
66 | assert(!isGV_with_GP(_svi)); \ | |
67 | ((XPVNV*) SvANY(_svi))->xnv_u.xpad_cop_seq.xlow; \ | |
68 | })) | |
69 | # define PARENT_FAKELEX_FLAGS(sv) \ | |
70 | (({ SV *const _svi = (SV *) (sv); \ | |
71 | assert(SvTYPE(_svi) == SVt_NV || SvTYPE(_svi) >= SVt_PVNV); \ | |
72 | assert(SvTYPE(_svi) != SVt_PVAV); \ | |
73 | assert(SvTYPE(_svi) != SVt_PVHV); \ | |
74 | assert(SvTYPE(_svi) != SVt_PVCV); \ | |
75 | assert(SvTYPE(_svi) != SVt_PVFM); \ | |
76 | assert(!isGV_with_GP(_svi)); \ | |
77 | ((XPVNV*) SvANY(_svi))->xnv_u.xpad_cop_seq.xhigh; \ | |
78 | })) | |
79 | #else | |
80 | # define COP_SEQ_RANGE_LOW(sv) \ | |
81 | (0 + (((XPVNV*) SvANY(sv))->xnv_u.xpad_cop_seq.xlow)) | |
82 | # define COP_SEQ_RANGE_HIGH(sv) \ | |
83 | (0 + (((XPVNV*) SvANY(sv))->xnv_u.xpad_cop_seq.xhigh)) | |
84 | ||
85 | ||
86 | # define PARENT_PAD_INDEX(sv) \ | |
87 | (0 + (((XPVNV*) SvANY(sv))->xnv_u.xpad_cop_seq.xlow)) | |
88 | # define PARENT_FAKELEX_FLAGS(sv) \ | |
89 | (0 + (((XPVNV*) SvANY(sv))->xnv_u.xpad_cop_seq.xhigh)) | |
90 | #endif | |
dd2155a4 DM |
91 | |
92 | /* flags for the pad_new() function */ | |
93 | ||
c7c737cb DM |
94 | #define padnew_CLONE 1 /* this pad is for a cloned CV */ |
95 | #define padnew_SAVE 2 /* save old globals */ | |
96 | #define padnew_SAVESUB 4 /* also save extra stuff for start of sub */ | |
dd2155a4 DM |
97 | |
98 | /* values for the pad_tidy() function */ | |
99 | ||
100 | typedef enum { | |
101 | padtidy_SUB, /* tidy up a pad for a sub, */ | |
102 | padtidy_SUBCLONE, /* a cloned sub, */ | |
103 | padtidy_FORMAT /* or a format */ | |
104 | } padtidy_type; | |
105 | ||
f3548bdc DM |
106 | /* ASSERT_CURPAD_LEGAL and ASSERT_CURPAD_ACTIVE respectively determine |
107 | * whether PL_comppad and PL_curpad are consistent and whether they have | |
108 | * active values */ | |
dd2155a4 | 109 | |
1dba731d NC |
110 | #ifndef PERL_MAD |
111 | # define pad_peg(label) | |
112 | #endif | |
113 | ||
f3548bdc DM |
114 | #ifdef DEBUGGING |
115 | # define ASSERT_CURPAD_LEGAL(label) \ | |
1dba731d | 116 | pad_peg(label); \ |
f3548bdc DM |
117 | if (PL_comppad ? (AvARRAY(PL_comppad) != PL_curpad) : (PL_curpad != 0)) \ |
118 | Perl_croak(aTHX_ "panic: illegal pad in %s: 0x%"UVxf"[0x%"UVxf"]",\ | |
119 | label, PTR2UV(PL_comppad), PTR2UV(PL_curpad)); | |
120 | ||
121 | ||
122 | # define ASSERT_CURPAD_ACTIVE(label) \ | |
1dba731d | 123 | pad_peg(label); \ |
f3548bdc DM |
124 | if (!PL_comppad || (AvARRAY(PL_comppad) != PL_curpad)) \ |
125 | Perl_croak(aTHX_ "panic: invalid pad in %s: 0x%"UVxf"[0x%"UVxf"]",\ | |
126 | label, PTR2UV(PL_comppad), PTR2UV(PL_curpad)); | |
127 | #else | |
128 | # define ASSERT_CURPAD_LEGAL(label) | |
129 | # define ASSERT_CURPAD_ACTIVE(label) | |
130 | #endif | |
131 | ||
132 | ||
133 | ||
134 | /* Note: the following three macros are actually defined in scope.h, but | |
dd2155a4 DM |
135 | * they are documented here for completeness, since they directly or |
136 | * indirectly affect pads. | |
137 | ||
138 | =for apidoc m|void|SAVEPADSV |PADOFFSET po | |
139 | Save a pad slot (used to restore after an iteration) | |
140 | ||
f3548bdc | 141 | XXX DAPM it would make more sense to make the arg a PADOFFSET |
dd2155a4 | 142 | =for apidoc m|void|SAVECLEARSV |SV **svp |
eeb8d49e | 143 | Clear the pointed to pad value on scope exit. (i.e. the runtime action of 'my') |
dd2155a4 DM |
144 | |
145 | =for apidoc m|void|SAVECOMPPAD | |
146 | save PL_comppad and PL_curpad | |
147 | ||
dd2155a4 DM |
148 | |
149 | ||
150 | ||
151 | ||
152 | =for apidoc m|SV *|PAD_SETSV |PADOFFSET po|SV* sv | |
153 | Set the slot at offset C<po> in the current pad to C<sv> | |
154 | ||
155 | =for apidoc m|void|PAD_SV |PADOFFSET po | |
156 | Get the value at offset C<po> in the current pad | |
157 | ||
158 | =for apidoc m|SV *|PAD_SVl |PADOFFSET po | |
159 | Lightweight and lvalue version of C<PAD_SV>. | |
160 | Get or set the value at offset C<po> in the current pad. | |
161 | Unlike C<PAD_SV>, does not print diagnostics with -DX. | |
162 | For internal use only. | |
163 | ||
164 | =for apidoc m|SV *|PAD_BASE_SV |PADLIST padlist|PADOFFSET po | |
165 | Get the value from slot C<po> in the base (DEPTH=1) pad of a padlist | |
166 | ||
167 | =for apidoc m|void|PAD_SET_CUR |PADLIST padlist|I32 n | |
168 | Set the current pad to be pad C<n> in the padlist, saving | |
fd617465 DM |
169 | the previous current pad. NB currently this macro expands to a string too |
170 | long for some compilers, so it's best to replace it with | |
171 | ||
172 | SAVECOMPPAD(); | |
173 | PAD_SET_CUR_NOSAVE(padlist,n); | |
174 | ||
dd2155a4 | 175 | |
4e380990 DM |
176 | =for apidoc m|void|PAD_SET_CUR_NOSAVE |PADLIST padlist|I32 n |
177 | like PAD_SET_CUR, but without the save | |
178 | ||
dd2155a4 DM |
179 | =for apidoc m|void|PAD_SAVE_SETNULLPAD |
180 | Save the current pad then set it to null. | |
181 | ||
f3548bdc DM |
182 | =for apidoc m|void|PAD_SAVE_LOCAL|PAD *opad|PAD *npad |
183 | Save the current pad to the local variable opad, then make the | |
184 | current pad equal to npad | |
185 | ||
186 | =for apidoc m|void|PAD_RESTORE_LOCAL|PAD *opad | |
187 | Restore the old pad saved into the local variable opad by PAD_SAVE_LOCAL() | |
dd2155a4 DM |
188 | |
189 | =cut | |
190 | */ | |
191 | ||
192 | #ifdef DEBUGGING | |
193 | # define PAD_SV(po) pad_sv(po) | |
194 | # define PAD_SETSV(po,sv) pad_setsv(po,sv) | |
195 | #else | |
196 | # define PAD_SV(po) (PL_curpad[po]) | |
197 | # define PAD_SETSV(po,sv) PL_curpad[po] = (sv) | |
198 | #endif | |
199 | ||
200 | #define PAD_SVl(po) (PL_curpad[po]) | |
201 | ||
202 | #define PAD_BASE_SV(padlist, po) \ | |
203 | (AvARRAY(padlist)[1]) \ | |
a0714e2c | 204 | ? AvARRAY((AV*)(AvARRAY(padlist)[1]))[po] : NULL; |
dd2155a4 DM |
205 | |
206 | ||
de5e01c2 JH |
207 | #define PAD_SET_CUR_NOSAVE(padlist,nth) \ |
208 | PL_comppad = (PAD*) (AvARRAY(padlist)[nth]); \ | |
f3548bdc DM |
209 | PL_curpad = AvARRAY(PL_comppad); \ |
210 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, \ | |
211 | "Pad 0x%"UVxf"[0x%"UVxf"] set_cur depth=%d\n", \ | |
de5e01c2 | 212 | PTR2UV(PL_comppad), PTR2UV(PL_curpad), (int)(nth))); |
f3548bdc DM |
213 | |
214 | ||
de5e01c2 | 215 | #define PAD_SET_CUR(padlist,nth) \ |
4e380990 | 216 | SAVECOMPPAD(); \ |
de5e01c2 | 217 | PAD_SET_CUR_NOSAVE(padlist,nth); |
4e380990 DM |
218 | |
219 | ||
f3548bdc | 220 | #define PAD_SAVE_SETNULLPAD() SAVECOMPPAD(); \ |
4608196e | 221 | PL_comppad = NULL; PL_curpad = NULL; \ |
f3548bdc DM |
222 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, "Pad set_null\n")); |
223 | ||
224 | #define PAD_SAVE_LOCAL(opad,npad) \ | |
225 | opad = PL_comppad; \ | |
226 | PL_comppad = (npad); \ | |
4608196e | 227 | PL_curpad = PL_comppad ? AvARRAY(PL_comppad) : NULL; \ |
f3548bdc DM |
228 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, \ |
229 | "Pad 0x%"UVxf"[0x%"UVxf"] save_local\n", \ | |
230 | PTR2UV(PL_comppad), PTR2UV(PL_curpad))); | |
231 | ||
232 | #define PAD_RESTORE_LOCAL(opad) \ | |
233 | PL_comppad = opad; \ | |
4608196e | 234 | PL_curpad = PL_comppad ? AvARRAY(PL_comppad) : NULL; \ |
f3548bdc DM |
235 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, \ |
236 | "Pad 0x%"UVxf"[0x%"UVxf"] restore_local\n", \ | |
237 | PTR2UV(PL_comppad), PTR2UV(PL_curpad))); | |
dd2155a4 DM |
238 | |
239 | ||
240 | /* | |
241 | =for apidoc m|void|CX_CURPAD_SAVE|struct context | |
242 | Save the current pad in the given context block structure. | |
243 | ||
f3548bdc | 244 | =for apidoc m|SV *|CX_CURPAD_SV|struct context|PADOFFSET po |
dd2155a4 DM |
245 | Access the SV at offset po in the saved current pad in the given |
246 | context block structure (can be used as an lvalue). | |
247 | ||
248 | =cut | |
249 | */ | |
250 | ||
f3548bdc DM |
251 | #define CX_CURPAD_SAVE(block) (block).oldcomppad = PL_comppad |
252 | #define CX_CURPAD_SV(block,po) (AvARRAY((AV*)((block).oldcomppad))[po]) | |
dd2155a4 DM |
253 | |
254 | ||
255 | /* | |
256 | =for apidoc m|U32|PAD_COMPNAME_FLAGS|PADOFFSET po | |
257 | Return the flags for the current compiling pad name | |
258 | at offset C<po>. Assumes a valid slot entry. | |
259 | ||
260 | =for apidoc m|char *|PAD_COMPNAME_PV|PADOFFSET po | |
261 | Return the name of the current compiling pad name | |
262 | at offset C<po>. Assumes a valid slot entry. | |
263 | ||
264 | =for apidoc m|HV *|PAD_COMPNAME_TYPE|PADOFFSET po | |
265 | Return the type (stash) of the current compiling pad name at offset | |
266 | C<po>. Must be a valid name. Returns null if not typed. | |
267 | ||
268 | =for apidoc m|HV *|PAD_COMPNAME_OURSTASH|PADOFFSET po | |
269 | Return the stash associated with an C<our> variable. | |
270 | Assumes the slot entry is a valid C<our> lexical. | |
271 | ||
272 | =for apidoc m|STRLEN|PAD_COMPNAME_GEN|PADOFFSET po | |
273 | The generation number of the name at offset C<po> in the current | |
274 | compiling pad (lvalue). Note that C<SvCUR> is hijacked for this purpose. | |
275 | ||
b162af07 SP |
276 | =for apidoc m|STRLEN|PAD_COMPNAME_GEN_set|PADOFFSET po|int gen |
277 | Sets the generation number of the name at offset C<po> in the current | |
278 | ling pad (lvalue) to C<gen>. Note that C<SvCUR_set> is hijacked for this purpose. | |
279 | ||
dd2155a4 | 280 | =cut |
b162af07 | 281 | |
dd2155a4 DM |
282 | */ |
283 | ||
284 | #define PAD_COMPNAME_FLAGS(po) SvFLAGS(*av_fetch(PL_comppad_name, (po), FALSE)) | |
1e7f542f NC |
285 | #define PAD_COMPNAME_FLAGS_isOUR(po) \ |
286 | ((PAD_COMPNAME_FLAGS(po) & (SVpad_NAME|SVpad_OUR)) == (SVpad_NAME|SVpad_OUR)) | |
dd2155a4 DM |
287 | #define PAD_COMPNAME_PV(po) SvPV_nolen(*av_fetch(PL_comppad_name, (po), FALSE)) |
288 | ||
b21dc031 | 289 | #define PAD_COMPNAME_TYPE(po) pad_compname_type(po) |
dd2155a4 DM |
290 | |
291 | #define PAD_COMPNAME_OURSTASH(po) \ | |
035dab74 | 292 | (OURSTASH(*av_fetch(PL_comppad_name, (po), FALSE))) |
dd2155a4 DM |
293 | |
294 | #define PAD_COMPNAME_GEN(po) SvCUR(AvARRAY(PL_comppad_name)[po]) | |
295 | ||
b162af07 | 296 | #define PAD_COMPNAME_GEN_set(po, gen) SvCUR_set(AvARRAY(PL_comppad_name)[po], gen) |
dd2155a4 DM |
297 | |
298 | ||
299 | /* | |
300 | =for apidoc m|void|PAD_DUP|PADLIST dstpad|PADLIST srcpad|CLONE_PARAMS* param | |
301 | Clone a padlist. | |
302 | ||
303 | =for apidoc m|void|PAD_CLONE_VARS|PerlInterpreter *proto_perl \ | |
304 | |CLONE_PARAMS* param | |
305 | Clone the state variables associated with running and compiling pads. | |
306 | ||
307 | =cut | |
308 | */ | |
309 | ||
310 | ||
311 | #define PAD_DUP(dstpad, srcpad, param) \ | |
312 | if ((srcpad) && !AvREAL(srcpad)) { \ | |
313 | /* XXX padlists are real, but pretend to be not */ \ | |
314 | AvREAL_on(srcpad); \ | |
315 | (dstpad) = av_dup_inc((srcpad), param); \ | |
316 | AvREAL_off(srcpad); \ | |
317 | AvREAL_off(dstpad); \ | |
318 | } \ | |
319 | else \ | |
320 | (dstpad) = av_dup_inc((srcpad), param); | |
321 | ||
c5ab0850 DM |
322 | /* NB - we set PL_comppad to null unless it points at a value that |
323 | * has already been dup'ed, ie it points to part of an active padlist. | |
324 | * Otherwise PL_comppad ends up being a leaked scalar in code like | |
325 | * the following: | |
326 | * threads->create(sub { threads->create(sub {...} ) } ); | |
327 | * where the second thread dups the outer sub's comppad but not the | |
328 | * sub's CV or padlist. */ | |
f3548bdc | 329 | |
dd2155a4 | 330 | #define PAD_CLONE_VARS(proto_perl, param) \ |
8f77bfdb | 331 | PL_comppad = (AV *) ptr_table_fetch(PL_ptr_table, proto_perl->Icomppad); \ |
4608196e | 332 | PL_curpad = PL_comppad ? AvARRAY(PL_comppad) : NULL; \ |
dd2155a4 DM |
333 | PL_comppad_name = av_dup(proto_perl->Icomppad_name, param); \ |
334 | PL_comppad_name_fill = proto_perl->Icomppad_name_fill; \ | |
335 | PL_comppad_name_floor = proto_perl->Icomppad_name_floor; \ | |
dd2155a4 DM |
336 | PL_min_intro_pending = proto_perl->Imin_intro_pending; \ |
337 | PL_max_intro_pending = proto_perl->Imax_intro_pending; \ | |
338 | PL_padix = proto_perl->Ipadix; \ | |
339 | PL_padix_floor = proto_perl->Ipadix_floor; \ | |
340 | PL_pad_reset_pending = proto_perl->Ipad_reset_pending; \ | |
341 | PL_cop_seqmax = proto_perl->Icop_seqmax; |