3 * Copyright (C) 2002, 2003, 2004, 2005, 2006, by Larry Wall and others
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.
8 * "Anyway: there was this Mr Frodo left an orphan and stranded, as you
9 * might say, among those queer Bucklanders, being brought up anyhow in
10 * Brandy Hall. A regular warren, by all accounts. Old Master Gorbadoc
11 * never had fewer than a couple of hundred relations in the place. Mr
12 * Bilbo never did a kinder deed than when he brought the lad back to
13 * live among decent folk." --the Gaffer
17 * As of Sept 2002, this file is new and may be in a state of flux for
18 * a while. I've marked things I intent to come back and look at further
19 * with an 'XXX DAPM' comment.
23 =head1 Pad Data Structures
25 This file contains the functions that create and manipulate scratchpads,
26 which are array-of-array data structures attached to a CV (ie a sub)
27 and which store lexical variables and opcode temporary and per-thread
30 =for apidoc m|AV *|CvPADLIST|CV *cv
31 CV's can have CvPADLIST(cv) set to point to an AV.
33 For these purposes "forms" are a kind-of CV, eval""s are too (except they're
34 not callable at will and are always thrown away after the eval"" is done
37 XSUBs don't have CvPADLIST set - dXSTARG fetches values from PL_curpad,
38 but that is really the callers pad (a slot of which is allocated by
41 The CvPADLIST AV has does not have AvREAL set, so REFCNT of component items
42 is managed "manual" (mostly in pad.c) rather than normal av.c rules.
43 The items in the AV are not SVs as for a normal AV, but other AVs:
45 0'th Entry of the CvPADLIST is an AV which represents the "names" or rather
46 the "static type information" for lexicals.
48 The CvDEPTH'th entry of CvPADLIST AV is an AV which is the stack frame at that
49 depth of recursion into the CV.
50 The 0'th slot of a frame AV is an AV which is @_.
51 other entries are storage for variables and op targets.
54 C<PL_comppad_name> is set to the names AV.
55 C<PL_comppad> is set to the frame AV for the frame CvDEPTH == 1.
56 C<PL_curpad> is set to the body of the frame AV (i.e. AvARRAY(PL_comppad)).
58 During execution, C<PL_comppad> and C<PL_curpad> refer to the live
59 frame of the currently executing sub.
61 Iterating over the names AV iterates over all possible pad
62 items. Pad slots that are SVs_PADTMP (targets/GVs/constants) end up having
63 &PL_sv_undef "names" (see pad_alloc()).
65 Only my/our variable (SVs_PADMY/SVs_PADOUR) slots get valid names.
66 The rest are op targets/GVs/constants which are statically allocated
67 or resolved at compile time. These don't have names by which they
68 can be looked up from Perl code at run time through eval"" like
69 my/our variables can be. Since they can't be looked up by "name"
70 but only by their index allocated at compile time (which is usually
71 in PL_op->op_targ), wasting a name SV for them doesn't make sense.
73 The SVs in the names AV have their PV being the name of the variable.
74 NV+1..IV inclusive is a range of cop_seq numbers for which the name is
75 valid. For typed lexicals name SV is SVt_PVMG and SvSTASH points at the
76 type. For C<our> lexicals, the type is SVt_PVGV, and GvSTASH points at the
77 stash of the associated global (so that duplicate C<our> declarations in the
78 same package can be detected). SvCUR is sometimes hijacked to
79 store the generation number during compilation.
81 If SvFAKE is set on the name SV then slot in the frame AVs are
82 a REFCNT'ed references to a lexical from "outside". In this case,
83 the name SV does not have a cop_seq range, since it is in scope
86 If the 'name' is '&' the corresponding entry in frame AV
87 is a CV representing a possible closure.
88 (SvFAKE and name of '&' is not a meaningful combination currently but could
89 become so if C<my sub foo {}> is implemented.)
91 The flag SVf_PADSTALE is cleared on lexicals each time the my() is executed,
92 and set on scope exit. This allows the 'Variable $x is not available' warning
93 to be generated in evals, such as
95 { my $x = 1; sub f { eval '$x'} } f();
102 #define PERL_IN_PAD_C
106 #define PAD_MAX 999999999
113 Create a new compiling padlist, saving and updating the various global
114 vars at the same time as creating the pad itself. The following flags
115 can be OR'ed together:
117 padnew_CLONE this pad is for a cloned CV
118 padnew_SAVE save old globals
119 padnew_SAVESUB also save extra stuff for start of sub
125 Perl_pad_new(pTHX_ int flags)
127 AV *padlist, *padname, *pad;
129 ASSERT_CURPAD_LEGAL("pad_new");
131 /* XXX DAPM really need a new SAVEt_PAD which restores all or most
132 * vars (based on flags) rather than storing vals + addresses for
133 * each individually. Also see pad_block_start.
134 * XXX DAPM Try to see whether all these conditionals are required
137 /* save existing state, ... */
139 if (flags & padnew_SAVE) {
141 SAVESPTR(PL_comppad_name);
142 if (! (flags & padnew_CLONE)) {
144 SAVEI32(PL_comppad_name_fill);
145 SAVEI32(PL_min_intro_pending);
146 SAVEI32(PL_max_intro_pending);
147 if (flags & padnew_SAVESUB) {
148 SAVEI32(PL_pad_reset_pending);
152 /* XXX DAPM interestingly, PL_comppad_name_floor never seems to be
153 * saved - check at some pt that this is okay */
155 /* ... create new pad ... */
161 if (flags & padnew_CLONE) {
162 /* XXX DAPM I dont know why cv_clone needs it
163 * doing differently yet - perhaps this separate branch can be
164 * dispensed with eventually ???
167 AV * const a0 = newAV(); /* will be @_ */
169 av_store(pad, 0, (SV*)a0);
170 AvFLAGS(a0) = AVf_REIFY;
173 #ifdef USE_5005THREADS
174 AV * const a0 = newAV(); /* will be @_ */
175 av_store(padname, 0, newSVpvn("@_", 2));
176 SvPADMY_on((SV*)a0); /* XXX Needed? */
177 av_store(pad, 0, (SV*)a0);
179 av_store(pad, 0, Nullsv);
180 #endif /* USE_THREADS */
184 av_store(padlist, 0, (SV*)padname);
185 av_store(padlist, 1, (SV*)pad);
187 /* ... then update state variables */
189 PL_comppad_name = (AV*)(*av_fetch(padlist, 0, FALSE));
190 PL_comppad = (AV*)(*av_fetch(padlist, 1, FALSE));
191 PL_curpad = AvARRAY(PL_comppad);
193 if (! (flags & padnew_CLONE)) {
194 PL_comppad_name_fill = 0;
195 PL_min_intro_pending = 0;
199 DEBUG_X(PerlIO_printf(Perl_debug_log,
200 "Pad 0x%"UVxf"[0x%"UVxf"] new: padlist=0x%"UVxf
201 " name=0x%"UVxf" flags=0x%"UVxf"\n",
202 PTR2UV(PL_comppad), PTR2UV(PL_curpad), PTR2UV(padlist),
203 PTR2UV(padname), (UV)flags
207 return (PADLIST*)padlist;
211 =for apidoc pad_undef
213 Free the padlist associated with a CV.
214 If parts of it happen to be current, we null the relevant
215 PL_*pad* global vars so that we don't have any dangling references left.
216 We also repoint the CvOUTSIDE of any about-to-be-orphaned
217 inner subs to the outer of this cv.
219 (This function should really be called pad_free, but the name was already
226 Perl_pad_undef(pTHX_ CV* cv)
229 const PADLIST * const padlist = CvPADLIST(cv);
233 if (!SvREFCNT(CvPADLIST(cv))) /* may be during global destruction */
236 DEBUG_X(PerlIO_printf(Perl_debug_log,
237 "Pad undef: padlist=0x%"UVxf"\n" , PTR2UV(padlist))
240 /* detach any '&' anon children in the pad; if afterwards they
241 * are still live, fix up their CvOUTSIDEs to point to our outside,
243 /* XXX DAPM for efficiency, we should only do this if we know we have
244 * children, or integrate this loop with general cleanup */
246 if (!PL_dirty) { /* don't bother during global destruction */
247 CV * const outercv = CvOUTSIDE(cv);
248 const U32 seq = CvOUTSIDE_SEQ(cv);
249 AV * const comppad_name = (AV*)AvARRAY(padlist)[0];
250 SV ** const namepad = AvARRAY(comppad_name);
251 AV * const comppad = (AV*)AvARRAY(padlist)[1];
252 SV ** const curpad = AvARRAY(comppad);
253 for (ix = AvFILLp(comppad_name); ix > 0; ix--) {
254 SV * const namesv = namepad[ix];
255 if (namesv && namesv != &PL_sv_undef
256 && *SvPVX_const(namesv) == '&')
258 CV * const innercv = (CV*)curpad[ix];
259 U32 inner_rc = SvREFCNT(innercv);
261 namepad[ix] = Nullsv;
262 SvREFCNT_dec(namesv);
264 if (SvREFCNT(comppad) < 2) { /* allow for /(?{ sub{} })/ */
266 SvREFCNT_dec(innercv);
269 if (inner_rc /* in use, not just a prototype */
270 && CvOUTSIDE(innercv) == cv)
272 assert(CvWEAKOUTSIDE(innercv));
273 /* don't relink to grandfather if he's being freed */
274 if (outercv && SvREFCNT(outercv)) {
275 CvWEAKOUTSIDE_off(innercv);
276 CvOUTSIDE(innercv) = outercv;
277 CvOUTSIDE_SEQ(innercv) = seq;
278 (void)SvREFCNT_inc(outercv);
281 CvOUTSIDE(innercv) = Nullcv;
290 ix = AvFILLp(padlist);
292 SV* const sv = AvARRAY(padlist)[ix--];
295 if (sv == (SV*)PL_comppad_name)
296 PL_comppad_name = Nullav;
297 else if (sv == (SV*)PL_comppad) {
298 PL_comppad = Null(PAD*);
299 PL_curpad = Null(SV**);
303 SvREFCNT_dec((SV*)CvPADLIST(cv));
304 CvPADLIST(cv) = Null(PADLIST*);
311 =for apidoc pad_add_name
313 Create a new name in the current pad at the specified offset.
314 If C<typestash> is valid, the name is for a typed lexical; set the
315 name's stash to that value.
316 If C<ourstash> is valid, it's an our lexical, set the name's
317 GvSTASH to that value
319 Also, if the name is @.. or %.., create a new array or hash for that slot
321 If fake, it means we're cloning an existing entry
327 * XXX DAPM this doesn't seem the right place to create a new array/hash.
328 * Whatever we do, we should be consistent - create scalars too, and
329 * create even if fake. Really need to integrate better the whole entry
330 * creation business - when + where does the name and value get created?
334 Perl_pad_add_name(pTHX_ char *name, HV* typestash, HV* ourstash, bool fake)
336 const PADOFFSET offset = pad_alloc(OP_PADSV, SVs_PADMY);
337 SV* const namesv = NEWSV(1102, 0);
339 ASSERT_CURPAD_ACTIVE("pad_add_name");
342 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
343 "Pad addname: %ld \"%s\"%s\n",
344 (long)offset, name, (fake ? " FAKE" : "")
348 sv_upgrade(namesv, ourstash ? SVt_PVGV : typestash ? SVt_PVMG : SVt_PVNV);
349 sv_setpv(namesv, name);
352 SvFLAGS(namesv) |= SVpad_TYPED;
353 SvSTASH_set(namesv, (HV*)SvREFCNT_inc((SV*) typestash));
356 SvFLAGS(namesv) |= SVpad_OUR;
357 GvSTASH(namesv) = (HV*)SvREFCNT_inc((SV*) ourstash);
360 av_store(PL_comppad_name, offset, namesv);
364 /* not yet introduced */
365 SvNV_set(namesv, (NV)PAD_MAX); /* min */
366 SvIV_set(namesv, 0); /* max */
368 if (!PL_min_intro_pending)
369 PL_min_intro_pending = offset;
370 PL_max_intro_pending = offset;
371 /* XXX DAPM since slot has been allocated, replace
372 * av_store with PL_curpad[offset] ? */
374 av_store(PL_comppad, offset, (SV*)newAV());
375 else if (*name == '%')
376 av_store(PL_comppad, offset, (SV*)newHV());
377 SvPADMY_on(PL_curpad[offset]);
387 =for apidoc pad_alloc
389 Allocate a new my or tmp pad entry. For a my, simply push a null SV onto
390 the end of PL_comppad, but for a tmp, scan the pad from PL_padix upwards
391 for a slot which has no name and no active value.
396 /* XXX DAPM integrate alloc(), add_name() and add_anon(),
397 * or at least rationalise ??? */
401 Perl_pad_alloc(pTHX_ I32 optype, U32 tmptype)
406 ASSERT_CURPAD_ACTIVE("pad_alloc");
408 if (AvARRAY(PL_comppad) != PL_curpad)
409 Perl_croak(aTHX_ "panic: pad_alloc");
410 if (PL_pad_reset_pending)
412 if (tmptype & SVs_PADMY) {
414 sv = *av_fetch(PL_comppad, AvFILLp(PL_comppad) + 1, TRUE);
415 } while (SvPADBUSY(sv)); /* need a fresh one */
416 retval = AvFILLp(PL_comppad);
419 SV * const * const names = AvARRAY(PL_comppad_name);
420 const SSize_t names_fill = AvFILLp(PL_comppad_name);
423 * "foreach" index vars temporarily become aliases to non-"my"
424 * values. Thus we must skip, not just pad values that are
425 * marked as current pad values, but also those with names.
427 /* HVDS why copy to sv here? we don't seem to use it */
428 if (++PL_padix <= names_fill &&
429 (sv = names[PL_padix]) && sv != &PL_sv_undef)
431 sv = *av_fetch(PL_comppad, PL_padix, TRUE);
432 if (!(SvFLAGS(sv) & (SVs_PADTMP | SVs_PADMY)) &&
433 !IS_PADGV(sv) && !IS_PADCONST(sv))
438 SvFLAGS(sv) |= tmptype;
439 PL_curpad = AvARRAY(PL_comppad);
441 DEBUG_X(PerlIO_printf(Perl_debug_log,
442 "Pad 0x%"UVxf"[0x%"UVxf"] alloc: %ld for %s\n",
443 PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long) retval,
444 PL_op_name[optype]));
445 return (PADOFFSET)retval;
449 =for apidoc pad_add_anon
451 Add an anon code entry to the current compiling pad
457 Perl_pad_add_anon(pTHX_ SV* sv, OPCODE op_type)
460 SV* const name = NEWSV(1106, 0);
461 sv_upgrade(name, SVt_PVNV);
462 sv_setpvn(name, "&", 1);
465 ix = pad_alloc(op_type, SVs_PADMY);
466 av_store(PL_comppad_name, ix, name);
467 /* XXX DAPM use PL_curpad[] ? */
468 av_store(PL_comppad, ix, sv);
471 /* to avoid ref loops, we never have parent + child referencing each
472 * other simultaneously */
473 if (CvOUTSIDE((CV*)sv)) {
474 assert(!CvWEAKOUTSIDE((CV*)sv));
475 CvWEAKOUTSIDE_on((CV*)sv);
476 SvREFCNT_dec(CvOUTSIDE((CV*)sv));
484 =for apidoc pad_check_dup
486 Check for duplicate declarations: report any of:
487 * a my in the current scope with the same name;
488 * an our (anywhere in the pad) with the same name and the same stash
490 C<is_our> indicates that the name to check is an 'our' declaration
495 /* XXX DAPM integrate this into pad_add_name ??? */
498 Perl_pad_check_dup(pTHX_ char *name, bool is_our, HV *ourstash)
503 ASSERT_CURPAD_ACTIVE("pad_check_dup");
504 if (AvFILLp(PL_comppad_name) < 0 || !ckWARN(WARN_MISC))
505 return; /* nothing to check */
507 svp = AvARRAY(PL_comppad_name);
508 top = AvFILLp(PL_comppad_name);
509 /* check the current scope */
510 /* XXX DAPM - why the (I32) cast - shouldn't we ensure they're the same
512 for (off = top; (I32)off > PL_comppad_name_floor; off--) {
513 SV * const sv = svp[off];
515 && sv != &PL_sv_undef
517 && (SvIVX(sv) == PAD_MAX || SvIVX(sv) == 0)
519 || ((SvFLAGS(sv) & SVpad_OUR) && GvSTASH(sv) == ourstash))
520 && strEQ(name, SvPVX_const(sv)))
522 Perl_warner(aTHX_ packWARN(WARN_MISC),
523 "\"%s\" variable %s masks earlier declaration in same %s",
524 (is_our ? "our" : "my"),
526 (SvIVX(sv) == PAD_MAX ? "scope" : "statement"));
531 /* check the rest of the pad */
534 SV * const sv = svp[off];
536 && sv != &PL_sv_undef
538 && (SvIVX(sv) == PAD_MAX || SvIVX(sv) == 0)
539 && ((SvFLAGS(sv) & SVpad_OUR) && GvSTASH(sv) == ourstash)
540 && strEQ(name, SvPVX_const(sv)))
542 Perl_warner(aTHX_ packWARN(WARN_MISC),
543 "\"our\" variable %s redeclared", name);
544 Perl_warner(aTHX_ packWARN(WARN_MISC),
545 "\t(Did you mean \"local\" instead of \"our\"?)\n");
548 } while ( off-- > 0 );
555 =for apidoc pad_findmy
557 Given a lexical name, try to find its offset, first in the current pad,
558 or failing that, in the pads of any lexically enclosing subs (including
559 the complications introduced by eval). If the name is found in an outer pad,
560 then a fake entry is added to the current pad.
561 Returns the offset in the current pad, or NOT_IN_PAD on failure.
567 Perl_pad_findmy(pTHX_ char *name)
573 SV **svp = AvARRAY(PL_comppad_name);
574 U32 seq = PL_cop_seqmax;
576 ASSERT_CURPAD_ACTIVE("pad_findmy");
577 DEBUG_Xv(PerlIO_printf(Perl_debug_log, "Pad findmy: \"%s\"\n", name));
579 #ifdef USE_5005THREADS
581 * Special case to get lexical (and hence per-thread) @_.
582 * XXX I need to find out how to tell at parse-time whether use
583 * of @_ should refer to a lexical (from a sub) or defgv (global
584 * scope and maybe weird sub-ish things like formats). See
585 * startsub in perly.y. It's possible that @_ could be lexical
586 * (at least from subs) even in non-threaded perl.
588 if (strEQ(name, "@_"))
589 return 0; /* success. (NOT_IN_PAD indicates failure) */
590 #endif /* USE_5005THREADS */
592 /* The one we're looking for is probably just before comppad_name_fill. */
593 for (off = AvFILLp(PL_comppad_name); off > 0; off--) {
595 if (!sv || sv == &PL_sv_undef || !strEQ(SvPVX_const(sv), name))
598 /* we'll use this later if we don't find a real entry */
603 if ( seq > U_32(SvNVX(sv)) /* min */
604 && seq <= (U32)SvIVX(sv)) /* max */
606 else if ((SvFLAGS(sv) & SVpad_OUR)
607 && U_32(SvNVX(sv)) == PAD_MAX) /* min */
609 /* look for an our that's being introduced; this allows
610 * our $foo = 0 unless defined $foo;
611 * to not give a warning. (Yes, this is a hack) */
619 /* See if it's in a nested scope */
620 off = pad_findlex(name, 0, PL_compcv);
621 if (off) /* pad_findlex returns 0 for failure...*/
625 return NOT_IN_PAD; /* ...but we return NOT_IN_PAD for failure */
632 =for apidoc pad_findlex
634 Find a named lexical anywhere in a chain of nested pads. Add fake entries
635 in the inner pads if it's found in an outer one. innercv is the CV *inside*
636 the chain of outer CVs to be searched. If newoff is non-null, this is a
637 run-time cloning: don't add fake entries, just find the lexical and add a
638 ref to it at newoff in the current pad.
644 S_pad_findlex(pTHX_ const char *name, PADOFFSET newoff, const CV* innercv)
656 ASSERT_CURPAD_ACTIVE("pad_findlex");
657 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
658 "Pad findlex: \"%s\" off=%ld startcv=0x%"UVxf"\n",
659 name, (long)newoff, PTR2UV(innercv))
662 seq = CvOUTSIDE_SEQ(innercv);
663 startcv = CvOUTSIDE(innercv);
665 for (cv = startcv; cv; seq = CvOUTSIDE_SEQ(cv), cv = CvOUTSIDE(cv)) {
670 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
671 " searching: cv=0x%"UVxf" seq=%d\n",
672 PTR2UV(cv), (int) seq )
675 curlist = CvPADLIST(cv);
677 continue; /* an undef CV */
678 svp = av_fetch(curlist, 0, FALSE);
679 if (!svp || *svp == &PL_sv_undef)
682 svp = AvARRAY(curname);
685 for (off = AvFILLp(curname); off > 0; off--) {
687 if (!sv || sv == &PL_sv_undef || !strEQ(SvPVX_const(sv), name))
690 /* we'll use this later if we don't find a real entry */
695 if ( seq > U_32(SvNVX(sv)) /* min */
696 && seq <= (U32)SvIVX(sv) /* max */
697 && !(newoff && !depth) /* ignore inactive when cloning */
703 /* no real entry - but did we find a fake one? */
705 if (newoff && !depth)
706 return 0; /* don't clone from inactive stack frame */
719 oldpad = (AV*)AvARRAY(curlist)[depth];
720 oldsv = *av_fetch(oldpad, off, TRUE);
724 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
725 " matched: offset %ld"
726 " FAKE, sv=0x%"UVxf"\n",
732 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
733 " matched: offset %ld"
734 " (%lu,%lu), sv=0x%"UVxf"\n",
736 (unsigned long)U_32(SvNVX(sv)),
737 (unsigned long)SvIVX(sv),
743 if (!newoff) { /* Not a mere clone operation. */
744 newoff = pad_add_name(
746 (SvFLAGS(sv) & SVpad_TYPED) ? SvSTASH(sv) : Nullhv,
747 (SvFLAGS(sv) & SVpad_OUR) ? GvSTASH(sv) : Nullhv,
751 if (CvANON(PL_compcv) || SvTYPE(PL_compcv) == SVt_PVFM) {
752 /* "It's closures all the way down." */
753 CvCLONE_on(PL_compcv);
755 if (CvANON(PL_compcv))
756 oldsv = Nullsv; /* no need to keep ref */
761 bcv && bcv != cv && !CvCLONE(bcv);
762 bcv = CvOUTSIDE(bcv))
765 /* install the missing pad entry in intervening
766 * nested subs and mark them cloneable. */
767 AV *ocomppad_name = PL_comppad_name;
768 PAD *ocomppad = PL_comppad;
769 AV *padlist = CvPADLIST(bcv);
770 PL_comppad_name = (AV*)AvARRAY(padlist)[0];
771 PL_comppad = (AV*)AvARRAY(padlist)[1];
772 PL_curpad = AvARRAY(PL_comppad);
775 (SvFLAGS(sv) & SVpad_TYPED)
776 ? SvSTASH(sv) : Nullhv,
777 (SvFLAGS(sv) & SVpad_OUR)
778 ? GvSTASH(sv) : Nullhv,
782 PL_comppad_name = ocomppad_name;
783 PL_comppad = ocomppad;
784 PL_curpad = ocomppad ?
785 AvARRAY(ocomppad) : Null(SV **);
789 if (ckWARN(WARN_CLOSURE)
790 && !CvUNIQUE(bcv) && !CvUNIQUE(cv))
792 Perl_warner(aTHX_ packWARN(WARN_CLOSURE),
793 "Variable \"%s\" may be unavailable",
801 else if (!CvUNIQUE(PL_compcv)) {
802 if (ckWARN(WARN_CLOSURE) && !SvFAKE(sv) && !CvUNIQUE(cv)
803 && !(SvFLAGS(sv) & SVpad_OUR))
805 Perl_warner(aTHX_ packWARN(WARN_CLOSURE),
806 "Variable \"%s\" will not stay shared", name);
810 av_store(PL_comppad, newoff, SvREFCNT_inc(oldsv));
811 ASSERT_CURPAD_ACTIVE("pad_findlex 2");
812 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
813 "Pad findlex: set offset %ld to sv 0x%"UVxf"\n",
814 (long)newoff, PTR2UV(oldsv)
824 Get the value at offset po in the current pad.
825 Use macro PAD_SV instead of calling this function directly.
832 Perl_pad_sv(pTHX_ PADOFFSET po)
834 ASSERT_CURPAD_ACTIVE("pad_sv");
836 #ifndef USE_5005THREADS
838 Perl_croak(aTHX_ "panic: pad_sv po");
840 DEBUG_X(PerlIO_printf(Perl_debug_log,
841 "Pad 0x%"UVxf"[0x%"UVxf"] sv: %ld sv=0x%"UVxf"\n",
842 PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long)po, PTR2UV(PL_curpad[po]))
844 return PL_curpad[po];
849 =for apidoc pad_setsv
851 Set the entry at offset po in the current pad to sv.
852 Use the macro PAD_SETSV() rather than calling this function directly.
859 Perl_pad_setsv(pTHX_ PADOFFSET po, SV* sv)
861 ASSERT_CURPAD_ACTIVE("pad_setsv");
863 DEBUG_X(PerlIO_printf(Perl_debug_log,
864 "Pad 0x%"UVxf"[0x%"UVxf"] setsv: %ld sv=0x%"UVxf"\n",
865 PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long)po, PTR2UV(sv))
874 =for apidoc pad_block_start
876 Update the pad compilation state variables on entry to a new block
882 * - integrate this in general state-saving routine ???
883 * - combine with the state-saving going on in pad_new ???
884 * - introduce a new SAVE type that does all this in one go ?
888 Perl_pad_block_start(pTHX_ int full)
890 ASSERT_CURPAD_ACTIVE("pad_block_start");
891 SAVEI32(PL_comppad_name_floor);
892 PL_comppad_name_floor = AvFILLp(PL_comppad_name);
894 PL_comppad_name_fill = PL_comppad_name_floor;
895 if (PL_comppad_name_floor < 0)
896 PL_comppad_name_floor = 0;
897 SAVEI32(PL_min_intro_pending);
898 SAVEI32(PL_max_intro_pending);
899 PL_min_intro_pending = 0;
900 SAVEI32(PL_comppad_name_fill);
901 SAVEI32(PL_padix_floor);
902 PL_padix_floor = PL_padix;
903 PL_pad_reset_pending = FALSE;
910 "Introduce" my variables to visible status.
921 ASSERT_CURPAD_ACTIVE("intro_my");
922 if (! PL_min_intro_pending)
923 return PL_cop_seqmax;
925 svp = AvARRAY(PL_comppad_name);
926 for (i = PL_min_intro_pending; i <= PL_max_intro_pending; i++) {
927 SV * const sv = svp[i];
929 if (sv && sv != &PL_sv_undef && !SvFAKE(sv) && !SvIVX(sv)) {
930 SvIV_set(sv, PAD_MAX); /* Don't know scope end yet. */
931 SvNV_set(sv, (NV)PL_cop_seqmax);
932 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
933 "Pad intromy: %ld \"%s\", (%lu,%lu)\n",
934 (long)i, SvPVX_const(sv),
935 (unsigned long)U_32(SvNVX(sv)), (unsigned long)SvIVX(sv))
939 PL_min_intro_pending = 0;
940 PL_comppad_name_fill = PL_max_intro_pending; /* Needn't search higher */
941 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
942 "Pad intromy: seq -> %ld\n", (long)(PL_cop_seqmax+1)));
944 return PL_cop_seqmax++;
948 =for apidoc pad_leavemy
950 Cleanup at end of scope during compilation: set the max seq number for
951 lexicals in this scope and warn of any lexicals that never got introduced.
957 Perl_pad_leavemy(pTHX)
960 SV * const * const svp = AvARRAY(PL_comppad_name);
962 PL_pad_reset_pending = FALSE;
964 ASSERT_CURPAD_ACTIVE("pad_leavemy");
965 if (PL_min_intro_pending && PL_comppad_name_fill < PL_min_intro_pending) {
966 for (off = PL_max_intro_pending; off >= PL_min_intro_pending; off--) {
967 const SV * const sv = svp[off];
968 if (sv && sv != &PL_sv_undef
969 && !SvFAKE(sv) && ckWARN_d(WARN_INTERNAL))
970 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
971 "%"SVf" never introduced", sv);
974 /* "Deintroduce" my variables that are leaving with this scope. */
975 for (off = AvFILLp(PL_comppad_name); off > PL_comppad_name_fill; off--) {
976 const SV * const sv = svp[off];
977 if (sv && sv != &PL_sv_undef && !SvFAKE(sv) && SvIVX(sv) == PAD_MAX) {
978 SvIV_set(sv, PL_cop_seqmax);
979 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
980 "Pad leavemy: %ld \"%s\", (%lu,%lu)\n",
981 (long)off, SvPVX_const(sv),
982 (unsigned long)U_32(SvNVX(sv)), (unsigned long)SvIVX(sv))
987 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
988 "Pad leavemy: seq = %ld\n", (long)PL_cop_seqmax));
993 =for apidoc pad_swipe
995 Abandon the tmp in the current pad at offset po and replace with a
1002 Perl_pad_swipe(pTHX_ PADOFFSET po, bool refadjust)
1004 ASSERT_CURPAD_LEGAL("pad_swipe");
1007 if (AvARRAY(PL_comppad) != PL_curpad)
1008 Perl_croak(aTHX_ "panic: pad_swipe curpad");
1010 Perl_croak(aTHX_ "panic: pad_swipe po");
1012 DEBUG_X(PerlIO_printf(Perl_debug_log,
1013 "Pad 0x%"UVxf"[0x%"UVxf"] swipe: %ld\n",
1014 PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long)po));
1017 SvPADTMP_off(PL_curpad[po]);
1019 SvREFCNT_dec(PL_curpad[po]);
1022 /* if pad tmps aren't shared between ops, then there's no need to
1023 * create a new tmp when an existing op is freed */
1024 #ifdef USE_BROKEN_PAD_RESET
1025 PL_curpad[po] = NEWSV(1107,0);
1026 SvPADTMP_on(PL_curpad[po]);
1028 PL_curpad[po] = &PL_sv_undef;
1030 if ((I32)po < PL_padix)
1036 =for apidoc pad_reset
1038 Mark all the current temporaries for reuse
1043 /* XXX pad_reset() is currently disabled because it results in serious bugs.
1044 * It causes pad temp TARGs to be shared between OPs. Since TARGs are pushed
1045 * on the stack by OPs that use them, there are several ways to get an alias
1046 * to a shared TARG. Such an alias will change randomly and unpredictably.
1047 * We avoid doing this until we can think of a Better Way.
1050 Perl_pad_reset(pTHX)
1052 #ifdef USE_BROKEN_PAD_RESET
1053 if (AvARRAY(PL_comppad) != PL_curpad)
1054 Perl_croak(aTHX_ "panic: pad_reset curpad");
1056 DEBUG_X(PerlIO_printf(Perl_debug_log,
1057 "Pad 0x%"UVxf"[0x%"UVxf"] reset: padix %ld -> %ld",
1058 PTR2UV(PL_comppad), PTR2UV(PL_curpad),
1059 (long)PL_padix, (long)PL_padix_floor
1063 if (!PL_tainting) { /* Can't mix tainted and non-tainted temporaries. */
1065 for (po = AvMAX(PL_comppad); po > PL_padix_floor; po--) {
1066 if (PL_curpad[po] && !SvIMMORTAL(PL_curpad[po]))
1067 SvPADTMP_off(PL_curpad[po]);
1069 PL_padix = PL_padix_floor;
1072 PL_pad_reset_pending = FALSE;
1077 =for apidoc pad_tidy
1079 Tidy up a pad after we've finished compiling it:
1080 * remove most stuff from the pads of anonsub prototypes;
1082 * mark tmps as such.
1087 /* XXX DAPM surely most of this stuff should be done properly
1088 * at the right time beforehand, rather than going around afterwards
1089 * cleaning up our mistakes ???
1093 Perl_pad_tidy(pTHX_ padtidy_type type)
1096 ASSERT_CURPAD_ACTIVE("pad_tidy");
1097 /* extend curpad to match namepad */
1098 if (AvFILLp(PL_comppad_name) < AvFILLp(PL_comppad))
1099 av_store(PL_comppad_name, AvFILLp(PL_comppad), Nullsv);
1101 if (type == padtidy_SUBCLONE) {
1102 SV * const * const namep = AvARRAY(PL_comppad_name);
1104 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
1107 if (SvIMMORTAL(PL_curpad[ix]) || IS_PADGV(PL_curpad[ix]) || IS_PADCONST(PL_curpad[ix]))
1110 * The only things that a clonable function needs in its
1111 * pad are references to outer lexicals and anonymous subs.
1112 * The rest are created anew during cloning.
1114 if (!((namesv = namep[ix]) != Nullsv &&
1115 namesv != &PL_sv_undef &&
1117 *SvPVX_const(namesv) == '&')))
1119 SvREFCNT_dec(PL_curpad[ix]);
1120 PL_curpad[ix] = Nullsv;
1124 else if (type == padtidy_SUB) {
1125 /* XXX DAPM this same bit of code keeps appearing !!! Rationalise? */
1126 AV * const av = newAV(); /* Will be @_ */
1128 av_store(PL_comppad, 0, (SV*)av);
1129 AvFLAGS(av) = AVf_REIFY;
1132 /* XXX DAPM rationalise these two similar branches */
1134 if (type == padtidy_SUB) {
1136 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
1137 if (SvIMMORTAL(PL_curpad[ix]) || IS_PADGV(PL_curpad[ix]) || IS_PADCONST(PL_curpad[ix]))
1139 if (!SvPADMY(PL_curpad[ix]))
1140 SvPADTMP_on(PL_curpad[ix]);
1143 else if (type == padtidy_FORMAT) {
1145 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
1146 if (!SvPADMY(PL_curpad[ix]) && !SvIMMORTAL(PL_curpad[ix]))
1147 SvPADTMP_on(PL_curpad[ix]);
1150 PL_curpad = AvARRAY(PL_comppad);
1155 =for apidoc pad_free
1157 Free the SV at offset po in the current pad.
1162 /* XXX DAPM integrate with pad_swipe ???? */
1164 Perl_pad_free(pTHX_ PADOFFSET po)
1166 ASSERT_CURPAD_LEGAL("pad_free");
1169 if (AvARRAY(PL_comppad) != PL_curpad)
1170 Perl_croak(aTHX_ "panic: pad_free curpad");
1172 Perl_croak(aTHX_ "panic: pad_free po");
1174 DEBUG_X(PerlIO_printf(Perl_debug_log,
1175 "Pad 0x%"UVxf"[0x%"UVxf"] free: %ld\n",
1176 PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long)po)
1179 if (PL_curpad[po] && PL_curpad[po] != &PL_sv_undef) {
1180 SvPADTMP_off(PL_curpad[po]);
1182 /* SV could be a shared hash key (eg bugid #19022) */
1183 if (!SvFAKE(PL_curpad[po]))
1184 SvREADONLY_off(PL_curpad[po]); /* could be a freed constant */
1188 if ((I32)po < PL_padix)
1195 =for apidoc do_dump_pad
1197 Dump the contents of a padlist
1203 Perl_do_dump_pad(pTHX_ I32 level, PerlIO *file, PADLIST *padlist, int full)
1214 pad_name = (AV*)*av_fetch((AV*)padlist, 0, FALSE);
1215 pad = (AV*)*av_fetch((AV*)padlist, 1, FALSE);
1216 pname = AvARRAY(pad_name);
1217 ppad = AvARRAY(pad);
1218 Perl_dump_indent(aTHX_ level, file,
1219 "PADNAME = 0x%"UVxf"(0x%"UVxf") PAD = 0x%"UVxf"(0x%"UVxf")\n",
1220 PTR2UV(pad_name), PTR2UV(pname), PTR2UV(pad), PTR2UV(ppad)
1223 for (ix = 1; ix <= AvFILLp(pad_name); ix++) {
1224 const SV *namesv = pname[ix];
1225 if (namesv && namesv == &PL_sv_undef) {
1230 Perl_dump_indent(aTHX_ level+1, file,
1231 "%2d. 0x%"UVxf"<%lu> FAKE \"%s\"\n",
1234 (unsigned long) (ppad[ix] ? SvREFCNT(ppad[ix]) : 0),
1238 Perl_dump_indent(aTHX_ level+1, file,
1239 "%2d. 0x%"UVxf"<%lu> (%lu,%lu) \"%s\"\n",
1242 (unsigned long) (ppad[ix] ? SvREFCNT(ppad[ix]) : 0),
1243 (unsigned long)U_32(SvNVX(namesv)),
1244 (unsigned long)SvIVX(namesv),
1249 Perl_dump_indent(aTHX_ level+1, file,
1250 "%2d. 0x%"UVxf"<%lu>\n",
1253 (unsigned long) (ppad[ix] ? SvREFCNT(ppad[ix]) : 0)
1264 dump the contents of a CV
1271 S_cv_dump(pTHX_ const CV *cv, const char *title)
1273 const CV * const outside = CvOUTSIDE(cv);
1274 AV* const padlist = CvPADLIST(cv);
1276 PerlIO_printf(Perl_debug_log,
1277 " %s: CV=0x%"UVxf" (%s), OUTSIDE=0x%"UVxf" (%s)\n",
1280 (CvANON(cv) ? "ANON"
1281 : (cv == PL_main_cv) ? "MAIN"
1282 : CvUNIQUE(cv) ? "UNIQUE"
1283 : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"),
1286 : CvANON(outside) ? "ANON"
1287 : (outside == PL_main_cv) ? "MAIN"
1288 : CvUNIQUE(outside) ? "UNIQUE"
1289 : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED"));
1291 PerlIO_printf(Perl_debug_log,
1292 " PADLIST = 0x%"UVxf"\n", PTR2UV(padlist));
1293 do_dump_pad(1, Perl_debug_log, padlist, 1);
1295 #endif /* DEBUGGING */
1302 =for apidoc cv_clone
1304 Clone a CV: make a new CV which points to the same code etc, but which
1305 has a newly-created pad built by copying the prototype pad and capturing
1312 Perl_cv_clone(pTHX_ CV *proto)
1316 LOCK_CRED_MUTEX; /* XXX create separate mutex */
1317 cv = cv_clone2(proto, CvOUTSIDE(proto));
1318 UNLOCK_CRED_MUTEX; /* XXX create separate mutex */
1323 /* XXX DAPM separate out cv and paddish bits ???
1324 * ideally the CV-related stuff shouldn't be in pad.c - how about
1328 S_cv_clone2(pTHX_ CV *proto, CV *outside)
1331 AV* const protopadlist = CvPADLIST(proto);
1332 const AV* const protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE);
1333 const AV* const protopad = (AV*)*av_fetch(protopadlist, 1, FALSE);
1334 SV** const pname = AvARRAY(protopad_name);
1335 SV** const ppad = AvARRAY(protopad);
1336 const I32 fname = AvFILLp(protopad_name);
1337 const I32 fpad = AvFILLp(protopad);
1340 assert(!CvUNIQUE(proto));
1343 SAVESPTR(PL_compcv);
1345 cv = PL_compcv = (CV*)NEWSV(1104, 0);
1346 sv_upgrade((SV *)cv, SvTYPE(proto));
1347 CvFLAGS(cv) = CvFLAGS(proto) & ~(CVf_CLONE|CVf_WEAKOUTSIDE);
1350 #ifdef USE_5005THREADS
1351 New(666, CvMUTEXP(cv), 1, perl_mutex);
1352 MUTEX_INIT(CvMUTEXP(cv));
1354 #endif /* USE_5005THREADS */
1356 CvFILE(cv) = CvXSUB(proto) ? CvFILE(proto)
1357 : savepv(CvFILE(proto));
1359 CvFILE(cv) = CvFILE(proto);
1361 CvGV(cv) = CvGV(proto);
1362 CvSTASH(cv) = CvSTASH(proto);
1364 CvROOT(cv) = OpREFCNT_inc(CvROOT(proto));
1366 CvSTART(cv) = CvSTART(proto);
1368 CvOUTSIDE(cv) = (CV*)SvREFCNT_inc(outside);
1369 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(proto);
1373 sv_setpvn((SV*)cv, SvPVX_const(proto), SvCUR(proto));
1375 CvPADLIST(cv) = pad_new(padnew_CLONE|padnew_SAVE);
1377 for (ix = fname; ix >= 0; ix--)
1378 av_store(PL_comppad_name, ix, SvREFCNT_inc(pname[ix]));
1380 av_fill(PL_comppad, fpad);
1381 PL_curpad = AvARRAY(PL_comppad);
1383 for (ix = fpad; ix > 0; ix--) {
1384 SV* const namesv = (ix <= fname) ? pname[ix] : Nullsv;
1385 if (namesv && namesv != &PL_sv_undef) {
1386 const char *name = SvPVX_const(namesv); /* XXX */
1387 if (SvFLAGS(namesv) & SVf_FAKE) { /* lexical from outside? */
1388 I32 off = pad_findlex(name, ix, cv);
1390 PL_curpad[ix] = SvREFCNT_inc(ppad[ix]);
1392 Perl_croak(aTHX_ "panic: cv_clone: %s", name);
1394 else { /* our own lexical */
1397 /* anon code -- we'll come back for it */
1398 sv = SvREFCNT_inc(ppad[ix]);
1400 else if (*name == '@')
1402 else if (*name == '%')
1411 else if (IS_PADGV(ppad[ix]) || IS_PADCONST(ppad[ix])) {
1412 PL_curpad[ix] = SvREFCNT_inc(ppad[ix]);
1415 SV* sv = NEWSV(0, 0);
1421 /* Now that vars are all in place, clone nested closures. */
1423 for (ix = fpad; ix > 0; ix--) {
1424 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
1426 && namesv != &PL_sv_undef
1427 && !(SvFLAGS(namesv) & SVf_FAKE)
1428 && *SvPVX(namesv) == '&'
1429 && CvCLONE(ppad[ix]))
1431 CV *kid = cv_clone2((CV*)ppad[ix], cv);
1432 SvREFCNT_dec(ppad[ix]);
1435 PL_curpad[ix] = (SV*)kid;
1436 /* '&' entry points to child, so child mustn't refcnt parent */
1437 CvWEAKOUTSIDE_on(kid);
1443 PerlIO_printf(Perl_debug_log, "\nPad CV clone\n");
1444 cv_dump(outside, "Outside");
1445 cv_dump(proto, "Proto");
1452 SV* const const_sv = op_const_sv(CvSTART(cv), cv);
1454 /* constant sub () { $x } closing over $x - see lib/constant.pm */
1456 cv = newCONSTSUB(CvSTASH(proto), Nullch, const_sv);
1464 =for apidoc pad_fixup_inner_anons
1466 For any anon CVs in the pad, change CvOUTSIDE of that CV from
1467 old_cv to new_cv if necessary. Needed when a newly-compiled CV has to be
1468 moved to a pre-existing CV struct.
1474 Perl_pad_fixup_inner_anons(pTHX_ PADLIST *padlist, CV *old_cv, CV *new_cv)
1477 AV * const comppad_name = (AV*)AvARRAY(padlist)[0];
1478 AV * const comppad = (AV*)AvARRAY(padlist)[1];
1479 SV ** const namepad = AvARRAY(comppad_name);
1480 SV ** const curpad = AvARRAY(comppad);
1481 for (ix = AvFILLp(comppad_name); ix > 0; ix--) {
1482 const SV * const namesv = namepad[ix];
1483 if (namesv && namesv != &PL_sv_undef
1484 && *SvPVX_const(namesv) == '&')
1486 CV * const innercv = (CV*)curpad[ix];
1487 assert(CvWEAKOUTSIDE(innercv));
1488 assert(CvOUTSIDE(innercv) == old_cv);
1489 CvOUTSIDE(innercv) = new_cv;
1496 =for apidoc pad_push
1498 Push a new pad frame onto the padlist, unless there's already a pad at
1499 this depth, in which case don't bother creating a new one.
1500 If has_args is true, give the new pad an @_ in slot zero.
1505 /* XXX pad_push is now always called with has_args == 1. Get rid of
1506 * this arg at some point */
1509 Perl_pad_push(pTHX_ PADLIST *padlist, int depth, int has_args)
1511 if (depth <= AvFILLp(padlist))
1515 SV** const svp = AvARRAY(padlist);
1516 AV* const newpad = newAV();
1517 SV** const oldpad = AvARRAY(svp[depth-1]);
1518 I32 ix = AvFILLp((AV*)svp[1]);
1519 I32 names_fill = AvFILLp((AV*)svp[0]);
1520 SV** const names = AvARRAY(svp[0]);
1522 for ( ;ix > 0; ix--) {
1523 if (names_fill >= ix && names[ix] != &PL_sv_undef) {
1524 const char *name = SvPVX_const(names[ix]);
1525 if ((SvFLAGS(names[ix]) & SVf_FAKE) || *name == '&') {
1526 /* outer lexical or anon code */
1527 av_store(newpad, ix, SvREFCNT_inc(oldpad[ix]));
1529 else { /* our own lexical */
1531 av_store(newpad, ix, sv = (SV*)newAV());
1532 else if (*name == '%')
1533 av_store(newpad, ix, sv = (SV*)newHV());
1535 av_store(newpad, ix, sv = NEWSV(0, 0));
1539 else if (IS_PADGV(oldpad[ix]) || IS_PADCONST(oldpad[ix])) {
1540 av_store(newpad, ix, sv = SvREFCNT_inc(oldpad[ix]));
1543 /* save temporaries on recursion? */
1544 av_store(newpad, ix, sv = NEWSV(0, 0));
1551 av_store(newpad, 0, (SV*)av);
1552 AvFLAGS(av) = AVf_REIFY;
1554 av_store(padlist, depth, (SV*)newpad);
1555 AvFILLp(padlist) = depth;
1561 Perl_pad_compname_type(pTHX_ const PADOFFSET po)
1563 SV* const * const av = av_fetch(PL_comppad_name, po, FALSE);
1564 if ( SvFLAGS(*av) & SVpad_TYPED ) {
1565 return SvSTASH(*av);
1572 * c-indentation-style: bsd
1574 * indent-tabs-mode: t
1577 * ex: set ts=8 sts=4 sw=4 noet: