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) */ | |
36 | #define COP_SEQ_RANGE_LOW(sv) U_32(SvNVX(sv)) | |
37 | /* High range end is inclusive (valid up to this cop seq) */ | |
38 | #define COP_SEQ_RANGE_HIGH(sv) U_32(SvUVX(sv)) | |
39 | ||
40 | #define PARENT_PAD_INDEX(sv) U_32(SvNVX(sv)) | |
41 | #define PARENT_FAKELEX_FLAGS(sv) U_32(SvUVX(sv)) | |
dd2155a4 DM |
42 | |
43 | /* flags for the pad_new() function */ | |
44 | ||
c7c737cb DM |
45 | #define padnew_CLONE 1 /* this pad is for a cloned CV */ |
46 | #define padnew_SAVE 2 /* save old globals */ | |
47 | #define padnew_SAVESUB 4 /* also save extra stuff for start of sub */ | |
dd2155a4 DM |
48 | |
49 | /* values for the pad_tidy() function */ | |
50 | ||
51 | typedef enum { | |
52 | padtidy_SUB, /* tidy up a pad for a sub, */ | |
53 | padtidy_SUBCLONE, /* a cloned sub, */ | |
54 | padtidy_FORMAT /* or a format */ | |
55 | } padtidy_type; | |
56 | ||
f3548bdc DM |
57 | /* ASSERT_CURPAD_LEGAL and ASSERT_CURPAD_ACTIVE respectively determine |
58 | * whether PL_comppad and PL_curpad are consistent and whether they have | |
59 | * active values */ | |
dd2155a4 | 60 | |
1dba731d NC |
61 | #ifndef PERL_MAD |
62 | # define pad_peg(label) | |
63 | #endif | |
64 | ||
f3548bdc DM |
65 | #ifdef DEBUGGING |
66 | # define ASSERT_CURPAD_LEGAL(label) \ | |
1dba731d | 67 | pad_peg(label); \ |
f3548bdc DM |
68 | if (PL_comppad ? (AvARRAY(PL_comppad) != PL_curpad) : (PL_curpad != 0)) \ |
69 | Perl_croak(aTHX_ "panic: illegal pad in %s: 0x%"UVxf"[0x%"UVxf"]",\ | |
70 | label, PTR2UV(PL_comppad), PTR2UV(PL_curpad)); | |
71 | ||
72 | ||
73 | # define ASSERT_CURPAD_ACTIVE(label) \ | |
1dba731d | 74 | pad_peg(label); \ |
f3548bdc DM |
75 | if (!PL_comppad || (AvARRAY(PL_comppad) != PL_curpad)) \ |
76 | Perl_croak(aTHX_ "panic: invalid pad in %s: 0x%"UVxf"[0x%"UVxf"]",\ | |
77 | label, PTR2UV(PL_comppad), PTR2UV(PL_curpad)); | |
78 | #else | |
79 | # define ASSERT_CURPAD_LEGAL(label) | |
80 | # define ASSERT_CURPAD_ACTIVE(label) | |
81 | #endif | |
82 | ||
83 | ||
84 | ||
85 | /* Note: the following three macros are actually defined in scope.h, but | |
dd2155a4 DM |
86 | * they are documented here for completeness, since they directly or |
87 | * indirectly affect pads. | |
88 | ||
89 | =for apidoc m|void|SAVEPADSV |PADOFFSET po | |
90 | Save a pad slot (used to restore after an iteration) | |
91 | ||
f3548bdc | 92 | XXX DAPM it would make more sense to make the arg a PADOFFSET |
dd2155a4 | 93 | =for apidoc m|void|SAVECLEARSV |SV **svp |
eeb8d49e | 94 | Clear the pointed to pad value on scope exit. (i.e. the runtime action of 'my') |
dd2155a4 DM |
95 | |
96 | =for apidoc m|void|SAVECOMPPAD | |
97 | save PL_comppad and PL_curpad | |
98 | ||
dd2155a4 DM |
99 | |
100 | ||
101 | ||
102 | ||
103 | =for apidoc m|SV *|PAD_SETSV |PADOFFSET po|SV* sv | |
104 | Set the slot at offset C<po> in the current pad to C<sv> | |
105 | ||
106 | =for apidoc m|void|PAD_SV |PADOFFSET po | |
107 | Get the value at offset C<po> in the current pad | |
108 | ||
109 | =for apidoc m|SV *|PAD_SVl |PADOFFSET po | |
110 | Lightweight and lvalue version of C<PAD_SV>. | |
111 | Get or set the value at offset C<po> in the current pad. | |
112 | Unlike C<PAD_SV>, does not print diagnostics with -DX. | |
113 | For internal use only. | |
114 | ||
115 | =for apidoc m|SV *|PAD_BASE_SV |PADLIST padlist|PADOFFSET po | |
116 | Get the value from slot C<po> in the base (DEPTH=1) pad of a padlist | |
117 | ||
118 | =for apidoc m|void|PAD_SET_CUR |PADLIST padlist|I32 n | |
119 | Set the current pad to be pad C<n> in the padlist, saving | |
fd617465 DM |
120 | the previous current pad. NB currently this macro expands to a string too |
121 | long for some compilers, so it's best to replace it with | |
122 | ||
123 | SAVECOMPPAD(); | |
124 | PAD_SET_CUR_NOSAVE(padlist,n); | |
125 | ||
dd2155a4 | 126 | |
4e380990 DM |
127 | =for apidoc m|void|PAD_SET_CUR_NOSAVE |PADLIST padlist|I32 n |
128 | like PAD_SET_CUR, but without the save | |
129 | ||
dd2155a4 DM |
130 | =for apidoc m|void|PAD_SAVE_SETNULLPAD |
131 | Save the current pad then set it to null. | |
132 | ||
f3548bdc DM |
133 | =for apidoc m|void|PAD_SAVE_LOCAL|PAD *opad|PAD *npad |
134 | Save the current pad to the local variable opad, then make the | |
135 | current pad equal to npad | |
136 | ||
137 | =for apidoc m|void|PAD_RESTORE_LOCAL|PAD *opad | |
138 | Restore the old pad saved into the local variable opad by PAD_SAVE_LOCAL() | |
dd2155a4 DM |
139 | |
140 | =cut | |
141 | */ | |
142 | ||
143 | #ifdef DEBUGGING | |
144 | # define PAD_SV(po) pad_sv(po) | |
145 | # define PAD_SETSV(po,sv) pad_setsv(po,sv) | |
146 | #else | |
147 | # define PAD_SV(po) (PL_curpad[po]) | |
148 | # define PAD_SETSV(po,sv) PL_curpad[po] = (sv) | |
149 | #endif | |
150 | ||
151 | #define PAD_SVl(po) (PL_curpad[po]) | |
152 | ||
153 | #define PAD_BASE_SV(padlist, po) \ | |
154 | (AvARRAY(padlist)[1]) \ | |
a0714e2c | 155 | ? AvARRAY((AV*)(AvARRAY(padlist)[1]))[po] : NULL; |
dd2155a4 DM |
156 | |
157 | ||
de5e01c2 JH |
158 | #define PAD_SET_CUR_NOSAVE(padlist,nth) \ |
159 | PL_comppad = (PAD*) (AvARRAY(padlist)[nth]); \ | |
f3548bdc DM |
160 | PL_curpad = AvARRAY(PL_comppad); \ |
161 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, \ | |
162 | "Pad 0x%"UVxf"[0x%"UVxf"] set_cur depth=%d\n", \ | |
de5e01c2 | 163 | PTR2UV(PL_comppad), PTR2UV(PL_curpad), (int)(nth))); |
f3548bdc DM |
164 | |
165 | ||
de5e01c2 | 166 | #define PAD_SET_CUR(padlist,nth) \ |
4e380990 | 167 | SAVECOMPPAD(); \ |
de5e01c2 | 168 | PAD_SET_CUR_NOSAVE(padlist,nth); |
4e380990 DM |
169 | |
170 | ||
f3548bdc | 171 | #define PAD_SAVE_SETNULLPAD() SAVECOMPPAD(); \ |
4608196e | 172 | PL_comppad = NULL; PL_curpad = NULL; \ |
f3548bdc DM |
173 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, "Pad set_null\n")); |
174 | ||
175 | #define PAD_SAVE_LOCAL(opad,npad) \ | |
176 | opad = PL_comppad; \ | |
177 | PL_comppad = (npad); \ | |
4608196e | 178 | PL_curpad = PL_comppad ? AvARRAY(PL_comppad) : NULL; \ |
f3548bdc DM |
179 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, \ |
180 | "Pad 0x%"UVxf"[0x%"UVxf"] save_local\n", \ | |
181 | PTR2UV(PL_comppad), PTR2UV(PL_curpad))); | |
182 | ||
183 | #define PAD_RESTORE_LOCAL(opad) \ | |
184 | PL_comppad = opad; \ | |
4608196e | 185 | PL_curpad = PL_comppad ? AvARRAY(PL_comppad) : NULL; \ |
f3548bdc DM |
186 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, \ |
187 | "Pad 0x%"UVxf"[0x%"UVxf"] restore_local\n", \ | |
188 | PTR2UV(PL_comppad), PTR2UV(PL_curpad))); | |
dd2155a4 DM |
189 | |
190 | ||
191 | /* | |
192 | =for apidoc m|void|CX_CURPAD_SAVE|struct context | |
193 | Save the current pad in the given context block structure. | |
194 | ||
f3548bdc | 195 | =for apidoc m|SV *|CX_CURPAD_SV|struct context|PADOFFSET po |
dd2155a4 DM |
196 | Access the SV at offset po in the saved current pad in the given |
197 | context block structure (can be used as an lvalue). | |
198 | ||
199 | =cut | |
200 | */ | |
201 | ||
f3548bdc DM |
202 | #define CX_CURPAD_SAVE(block) (block).oldcomppad = PL_comppad |
203 | #define CX_CURPAD_SV(block,po) (AvARRAY((AV*)((block).oldcomppad))[po]) | |
dd2155a4 DM |
204 | |
205 | ||
206 | /* | |
207 | =for apidoc m|U32|PAD_COMPNAME_FLAGS|PADOFFSET po | |
208 | Return the flags for the current compiling pad name | |
209 | at offset C<po>. Assumes a valid slot entry. | |
210 | ||
211 | =for apidoc m|char *|PAD_COMPNAME_PV|PADOFFSET po | |
212 | Return the name of the current compiling pad name | |
213 | at offset C<po>. Assumes a valid slot entry. | |
214 | ||
215 | =for apidoc m|HV *|PAD_COMPNAME_TYPE|PADOFFSET po | |
216 | Return the type (stash) of the current compiling pad name at offset | |
217 | C<po>. Must be a valid name. Returns null if not typed. | |
218 | ||
219 | =for apidoc m|HV *|PAD_COMPNAME_OURSTASH|PADOFFSET po | |
220 | Return the stash associated with an C<our> variable. | |
221 | Assumes the slot entry is a valid C<our> lexical. | |
222 | ||
223 | =for apidoc m|STRLEN|PAD_COMPNAME_GEN|PADOFFSET po | |
224 | The generation number of the name at offset C<po> in the current | |
225 | compiling pad (lvalue). Note that C<SvCUR> is hijacked for this purpose. | |
226 | ||
b162af07 SP |
227 | =for apidoc m|STRLEN|PAD_COMPNAME_GEN_set|PADOFFSET po|int gen |
228 | Sets the generation number of the name at offset C<po> in the current | |
229 | ling pad (lvalue) to C<gen>. Note that C<SvCUR_set> is hijacked for this purpose. | |
230 | ||
dd2155a4 | 231 | =cut |
b162af07 | 232 | |
dd2155a4 DM |
233 | */ |
234 | ||
235 | #define PAD_COMPNAME_FLAGS(po) SvFLAGS(*av_fetch(PL_comppad_name, (po), FALSE)) | |
1e7f542f NC |
236 | #define PAD_COMPNAME_FLAGS_isOUR(po) \ |
237 | ((PAD_COMPNAME_FLAGS(po) & (SVpad_NAME|SVpad_OUR)) == (SVpad_NAME|SVpad_OUR)) | |
dd2155a4 DM |
238 | #define PAD_COMPNAME_PV(po) SvPV_nolen(*av_fetch(PL_comppad_name, (po), FALSE)) |
239 | ||
b21dc031 | 240 | #define PAD_COMPNAME_TYPE(po) pad_compname_type(po) |
dd2155a4 DM |
241 | |
242 | #define PAD_COMPNAME_OURSTASH(po) \ | |
035dab74 | 243 | (OURSTASH(*av_fetch(PL_comppad_name, (po), FALSE))) |
dd2155a4 DM |
244 | |
245 | #define PAD_COMPNAME_GEN(po) SvCUR(AvARRAY(PL_comppad_name)[po]) | |
246 | ||
b162af07 | 247 | #define PAD_COMPNAME_GEN_set(po, gen) SvCUR_set(AvARRAY(PL_comppad_name)[po], gen) |
dd2155a4 DM |
248 | |
249 | ||
250 | /* | |
251 | =for apidoc m|void|PAD_DUP|PADLIST dstpad|PADLIST srcpad|CLONE_PARAMS* param | |
252 | Clone a padlist. | |
253 | ||
254 | =for apidoc m|void|PAD_CLONE_VARS|PerlInterpreter *proto_perl \ | |
255 | |CLONE_PARAMS* param | |
256 | Clone the state variables associated with running and compiling pads. | |
257 | ||
258 | =cut | |
259 | */ | |
260 | ||
261 | ||
262 | #define PAD_DUP(dstpad, srcpad, param) \ | |
263 | if ((srcpad) && !AvREAL(srcpad)) { \ | |
264 | /* XXX padlists are real, but pretend to be not */ \ | |
265 | AvREAL_on(srcpad); \ | |
266 | (dstpad) = av_dup_inc((srcpad), param); \ | |
267 | AvREAL_off(srcpad); \ | |
268 | AvREAL_off(dstpad); \ | |
269 | } \ | |
270 | else \ | |
271 | (dstpad) = av_dup_inc((srcpad), param); | |
272 | ||
c5ab0850 DM |
273 | /* NB - we set PL_comppad to null unless it points at a value that |
274 | * has already been dup'ed, ie it points to part of an active padlist. | |
275 | * Otherwise PL_comppad ends up being a leaked scalar in code like | |
276 | * the following: | |
277 | * threads->create(sub { threads->create(sub {...} ) } ); | |
278 | * where the second thread dups the outer sub's comppad but not the | |
279 | * sub's CV or padlist. */ | |
f3548bdc | 280 | |
dd2155a4 | 281 | #define PAD_CLONE_VARS(proto_perl, param) \ |
8f77bfdb | 282 | PL_comppad = (AV *) ptr_table_fetch(PL_ptr_table, proto_perl->Icomppad); \ |
4608196e | 283 | PL_curpad = PL_comppad ? AvARRAY(PL_comppad) : NULL; \ |
dd2155a4 DM |
284 | PL_comppad_name = av_dup(proto_perl->Icomppad_name, param); \ |
285 | PL_comppad_name_fill = proto_perl->Icomppad_name_fill; \ | |
286 | PL_comppad_name_floor = proto_perl->Icomppad_name_floor; \ | |
dd2155a4 DM |
287 | PL_min_intro_pending = proto_perl->Imin_intro_pending; \ |
288 | PL_max_intro_pending = proto_perl->Imax_intro_pending; \ | |
289 | PL_padix = proto_perl->Ipadix; \ | |
290 | PL_padix_floor = proto_perl->Ipadix_floor; \ | |
291 | PL_pad_reset_pending = proto_perl->Ipad_reset_pending; \ | |
292 | PL_cop_seqmax = proto_perl->Icop_seqmax; |