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