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