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