3 * Copyright (C) 2002, 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 =for apidoc m|AV *|CvPADLIST|CV *cv
26 CV's can have CvPADLIST(cv) set to point to an AV.
28 For these purposes "forms" are a kind-of CV, eval""s are too (except they're
29 not callable at will and are always thrown away after the eval"" is done
32 XSUBs don't have CvPADLIST set - dXSTARG fetches values from PL_curpad,
33 but that is really the callers pad (a slot of which is allocated by
36 The CvPADLIST AV has does not have AvREAL set, so REFCNT of component items
37 is managed "manual" (mostly in pad.c) rather than normal av.c rules.
38 The items in the AV are not SVs as for a normal AV, but other AVs:
40 0'th Entry of the CvPADLIST is an AV which represents the "names" or rather
41 the "static type information" for lexicals.
43 The CvDEPTH'th entry of CvPADLIST AV is an AV which is the stack frame at that
44 depth of recursion into the CV.
45 The 0'th slot of a frame AV is an AV which is @_.
46 other entries are storage for variables and op targets.
49 C<PL_comppad_name> is set to the names AV.
50 C<PL_comppad> is set to the frame AV for the frame CvDEPTH == 1.
51 C<PL_curpad> is set to the body of the frame AV (i.e. AvARRAY(PL_comppad)).
53 During execution, C<PL_comppad> and C<PL_curpad> refer to the live
54 frame of the currently executing sub.
56 Iterating over the names AV iterates over all possible pad
57 items. Pad slots that are SVs_PADTMP (targets/GVs/constants) end up having
58 &PL_sv_undef "names" (see pad_alloc()).
60 Only my/our variable (SVs_PADMY/SVs_PADOUR) slots get valid names.
61 The rest are op targets/GVs/constants which are statically allocated
62 or resolved at compile time. These don't have names by which they
63 can be looked up from Perl code at run time through eval"" like
64 my/our variables can be. Since they can't be looked up by "name"
65 but only by their index allocated at compile time (which is usually
66 in PL_op->op_targ), wasting a name SV for them doesn't make sense.
68 The SVs in the names AV have their PV being the name of the variable.
69 NV+1..IV inclusive is a range of cop_seq numbers for which the name is
70 valid. For typed lexicals name SV is SVt_PVMG and SvSTASH points at the
71 type. For C<our> lexicals, the type is SVt_PVGV, and GvSTASH points at the
72 stash of the associated global (so that duplicate C<our> delarations in the
73 same package can be detected). SvCUR is sometimes hijacked to
74 store the generation number during compilation.
76 If SvFAKE is set on the name SV then slot in the frame AVs are
77 a REFCNT'ed references to a lexical from "outside". In this case,
78 the name SV does not have a cop_seq range, since it is in scope
81 If the 'name' is '&' the corresponding entry in frame AV
82 is a CV representing a possible closure.
83 (SvFAKE and name of '&' is not a meaningful combination currently but could
84 become so if C<my sub foo {}> is implemented.)
95 #define PAD_MAX 999999999
102 Create a new compiling padlist, saving and updating the various global
103 vars at the same time as creating the pad itself. The following flags
104 can be OR'ed together:
106 padnew_CLONE this pad is for a cloned CV
107 padnew_SAVE save old globals
108 padnew_SAVESUB also save extra stuff for start of sub
114 Perl_pad_new(pTHX_ int flags)
116 AV *padlist, *padname, *pad, *a0;
118 ASSERT_CURPAD_LEGAL("pad_new");
120 /* XXX DAPM really need a new SAVEt_PAD which restores all or most
121 * vars (based on flags) rather than storing vals + addresses for
122 * each individually. Also see pad_block_start.
123 * XXX DAPM Try to see whether all these conditionals are required
126 /* save existing state, ... */
128 if (flags & padnew_SAVE) {
130 SAVESPTR(PL_comppad_name);
131 if (! (flags & padnew_CLONE)) {
133 SAVEI32(PL_comppad_name_fill);
134 SAVEI32(PL_min_intro_pending);
135 SAVEI32(PL_max_intro_pending);
136 if (flags & padnew_SAVESUB) {
137 SAVEI32(PL_pad_reset_pending);
141 /* XXX DAPM interestingly, PL_comppad_name_floor never seems to be
142 * saved - check at some pt that this is okay */
144 /* ... create new pad ... */
150 if (flags & padnew_CLONE) {
151 /* XXX DAPM I dont know why cv_clone needs it
152 * doing differently yet - perhaps this separate branch can be
153 * dispensed with eventually ???
156 a0 = newAV(); /* will be @_ */
158 av_store(pad, 0, (SV*)a0);
159 AvFLAGS(a0) = AVf_REIFY;
162 #ifdef USE_5005THREADS
163 av_store(padname, 0, newSVpvn("@_", 2));
165 SvPADMY_on((SV*)a0); /* XXX Needed? */
166 av_store(pad, 0, (SV*)a0);
168 av_store(pad, 0, Nullsv);
169 #endif /* USE_THREADS */
173 av_store(padlist, 0, (SV*)padname);
174 av_store(padlist, 1, (SV*)pad);
176 /* ... then update state variables */
178 PL_comppad_name = (AV*)(*av_fetch(padlist, 0, FALSE));
179 PL_comppad = (AV*)(*av_fetch(padlist, 1, FALSE));
180 PL_curpad = AvARRAY(PL_comppad);
182 if (! (flags & padnew_CLONE)) {
183 PL_comppad_name_fill = 0;
184 PL_min_intro_pending = 0;
188 DEBUG_X(PerlIO_printf(Perl_debug_log,
189 "Pad 0x%"UVxf"[0x%"UVxf"] new: padlist=0x%"UVxf
190 " name=0x%"UVxf" flags=0x%"UVxf"\n",
191 PTR2UV(PL_comppad), PTR2UV(PL_curpad), PTR2UV(padlist),
192 PTR2UV(padname), (UV)flags
196 return (PADLIST*)padlist;
200 =for apidoc pad_undef
202 Free the padlist associated with a CV.
203 If parts of it happen to be current, we null the relevant
204 PL_*pad* global vars so that we don't have any dangling references left.
205 We also repoint the CvOUTSIDE of any about-to-be-orphaned
206 inner subs to the outer of this cv.
208 (This function should really be called pad_free, but the name was already
215 Perl_pad_undef(pTHX_ CV* cv)
218 PADLIST *padlist = CvPADLIST(cv);
222 if (!SvREFCNT(CvPADLIST(cv))) /* may be during global destruction */
225 DEBUG_X(PerlIO_printf(Perl_debug_log,
226 "Pad undef: padlist=0x%"UVxf"\n" , PTR2UV(padlist))
229 /* detach any '&' anon children in the pad; if afterwards they
230 * are still live, fix up their CvOUTSIDEs to point to our outside,
232 /* XXX DAPM for efficiency, we should only do this if we know we have
233 * children, or integrate this loop with general cleanup */
235 if (!PL_dirty) { /* don't bother during global destruction */
236 CV *outercv = CvOUTSIDE(cv);
237 U32 seq = CvOUTSIDE_SEQ(cv);
238 AV *comppad_name = (AV*)AvARRAY(padlist)[0];
239 SV **namepad = AvARRAY(comppad_name);
240 AV *comppad = (AV*)AvARRAY(padlist)[1];
241 SV **curpad = AvARRAY(comppad);
242 for (ix = AvFILLp(comppad_name); ix > 0; ix--) {
243 SV *namesv = namepad[ix];
244 if (namesv && namesv != &PL_sv_undef
245 && *SvPVX(namesv) == '&')
247 CV *innercv = (CV*)curpad[ix];
248 namepad[ix] = Nullsv;
249 SvREFCNT_dec(namesv);
251 SvREFCNT_dec(innercv);
252 if (SvREFCNT(innercv) /* in use, not just a prototype */
253 && CvOUTSIDE(innercv) == cv)
255 assert(CvWEAKOUTSIDE(innercv));
256 CvWEAKOUTSIDE_off(innercv);
257 CvOUTSIDE(innercv) = outercv;
258 CvOUTSIDE_SEQ(innercv) = seq;
259 SvREFCNT_inc(outercv);
265 ix = AvFILLp(padlist);
267 SV* sv = AvARRAY(padlist)[ix--];
270 if (sv == (SV*)PL_comppad_name)
271 PL_comppad_name = Nullav;
272 else if (sv == (SV*)PL_comppad) {
273 PL_comppad = Null(PAD*);
274 PL_curpad = Null(SV**);
278 SvREFCNT_dec((SV*)CvPADLIST(cv));
279 CvPADLIST(cv) = Null(PADLIST*);
286 =for apidoc pad_add_name
288 Create a new name in the current pad at the specified offset.
289 If C<typestash> is valid, the name is for a typed lexical; set the
290 name's stash to that value.
291 If C<ourstash> is valid, it's an our lexical, set the name's
292 GvSTASH to that value
294 Also, if the name is @.. or %.., create a new array or hash for that slot
296 If fake, it means we're cloning an existing entry
302 * XXX DAPM this doesn't seem the right place to create a new array/hash.
303 * Whatever we do, we should be consistent - create scalars too, and
304 * create even if fake. Really need to integrate better the whole entry
305 * creation business - when + where does the name and value get created?
309 Perl_pad_add_name(pTHX_ char *name, HV* typestash, HV* ourstash, bool fake)
311 PADOFFSET offset = pad_alloc(OP_PADSV, SVs_PADMY);
312 SV* namesv = NEWSV(1102, 0);
314 ASSERT_CURPAD_ACTIVE("pad_add_name");
317 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
318 "Pad addname: %ld \"%s\"%s\n",
319 (long)offset, name, (fake ? " FAKE" : "")
323 sv_upgrade(namesv, ourstash ? SVt_PVGV : typestash ? SVt_PVMG : SVt_PVNV);
324 sv_setpv(namesv, name);
327 SvFLAGS(namesv) |= SVpad_TYPED;
328 SvSTASH(namesv) = (HV*)SvREFCNT_inc((SV*) typestash);
331 SvFLAGS(namesv) |= SVpad_OUR;
332 GvSTASH(namesv) = (HV*)SvREFCNT_inc((SV*) ourstash);
335 av_store(PL_comppad_name, offset, namesv);
339 /* not yet introduced */
340 SvNVX(namesv) = (NV)PAD_MAX; /* min */
341 SvIVX(namesv) = 0; /* max */
343 if (!PL_min_intro_pending)
344 PL_min_intro_pending = offset;
345 PL_max_intro_pending = offset;
346 /* XXX DAPM since slot has been allocated, replace
347 * av_store with PL_curpad[offset] ? */
349 av_store(PL_comppad, offset, (SV*)newAV());
350 else if (*name == '%')
351 av_store(PL_comppad, offset, (SV*)newHV());
352 SvPADMY_on(PL_curpad[offset]);
362 =for apidoc pad_alloc
364 Allocate a new my or tmp pad entry. For a my, simply push a null SV onto
365 the end of PL_comppad, but for a tmp, scan the pad from PL_padix upwards
366 for a slot which has no name and and no active value.
371 /* XXX DAPM integrate alloc(), add_name() and add_anon(),
372 * or at least rationalise ??? */
376 Perl_pad_alloc(pTHX_ I32 optype, U32 tmptype)
381 ASSERT_CURPAD_ACTIVE("pad_alloc");
383 if (AvARRAY(PL_comppad) != PL_curpad)
384 Perl_croak(aTHX_ "panic: pad_alloc");
385 if (PL_pad_reset_pending)
387 if (tmptype & SVs_PADMY) {
389 sv = *av_fetch(PL_comppad, AvFILLp(PL_comppad) + 1, TRUE);
390 } while (SvPADBUSY(sv)); /* need a fresh one */
391 retval = AvFILLp(PL_comppad);
394 SV **names = AvARRAY(PL_comppad_name);
395 SSize_t names_fill = AvFILLp(PL_comppad_name);
398 * "foreach" index vars temporarily become aliases to non-"my"
399 * values. Thus we must skip, not just pad values that are
400 * marked as current pad values, but also those with names.
402 /* HVDS why copy to sv here? we don't seem to use it */
403 if (++PL_padix <= names_fill &&
404 (sv = names[PL_padix]) && sv != &PL_sv_undef)
406 sv = *av_fetch(PL_comppad, PL_padix, TRUE);
407 if (!(SvFLAGS(sv) & (SVs_PADTMP | SVs_PADMY)) &&
408 !IS_PADGV(sv) && !IS_PADCONST(sv))
413 SvFLAGS(sv) |= tmptype;
414 PL_curpad = AvARRAY(PL_comppad);
416 DEBUG_X(PerlIO_printf(Perl_debug_log,
417 "Pad 0x%"UVxf"[0x%"UVxf"] alloc: %ld for %s\n",
418 PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long) retval,
419 PL_op_name[optype]));
420 return (PADOFFSET)retval;
424 =for apidoc pad_add_anon
426 Add an anon code entry to the current compiling pad
432 Perl_pad_add_anon(pTHX_ SV* sv, OPCODE op_type)
437 name = NEWSV(1106, 0);
438 sv_upgrade(name, SVt_PVNV);
439 sv_setpvn(name, "&", 1);
442 ix = pad_alloc(op_type, SVs_PADMY);
443 av_store(PL_comppad_name, ix, name);
444 /* XXX DAPM use PL_curpad[] ? */
445 av_store(PL_comppad, ix, sv);
448 /* to avoid ref loops, we never have parent + child referencing each
449 * other simultaneously */
450 if (CvOUTSIDE((CV*)sv)) {
451 assert(!CvWEAKOUTSIDE((CV*)sv));
452 CvWEAKOUTSIDE_on((CV*)sv);
453 SvREFCNT_dec(CvOUTSIDE((CV*)sv));
461 =for apidoc pad_check_dup
463 Check for duplicate declarations: report any of:
464 * a my in the current scope with the same name;
465 * an our (anywhere in the pad) with the same name and the same stash
467 C<is_our> indicates that the name to check is an 'our' declaration
472 /* XXX DAPM integrate this into pad_add_name ??? */
475 Perl_pad_check_dup(pTHX_ char *name, bool is_our, HV *ourstash)
480 ASSERT_CURPAD_ACTIVE("pad_check_dup");
481 if (!ckWARN(WARN_MISC) || AvFILLp(PL_comppad_name) < 0)
482 return; /* nothing to check */
484 svp = AvARRAY(PL_comppad_name);
485 top = AvFILLp(PL_comppad_name);
486 /* check the current scope */
487 /* XXX DAPM - why the (I32) cast - shouldn't we ensure they're the same
489 for (off = top; (I32)off > PL_comppad_name_floor; off--) {
491 && sv != &PL_sv_undef
493 && (SvIVX(sv) == PAD_MAX || SvIVX(sv) == 0)
495 || ((SvFLAGS(sv) & SVpad_OUR) && GvSTASH(sv) == ourstash))
496 && strEQ(name, SvPVX(sv)))
498 Perl_warner(aTHX_ packWARN(WARN_MISC),
499 "\"%s\" variable %s masks earlier declaration in same %s",
500 (is_our ? "our" : "my"),
502 (SvIVX(sv) == PAD_MAX ? "scope" : "statement"));
507 /* check the rest of the pad */
511 && sv != &PL_sv_undef
513 && (SvIVX(sv) == PAD_MAX || SvIVX(sv) == 0)
514 && ((SvFLAGS(sv) & SVpad_OUR) && GvSTASH(sv) == ourstash)
515 && strEQ(name, SvPVX(sv)))
517 Perl_warner(aTHX_ packWARN(WARN_MISC),
518 "\"our\" variable %s redeclared", name);
519 Perl_warner(aTHX_ packWARN(WARN_MISC),
520 "\t(Did you mean \"local\" instead of \"our\"?)\n");
523 } while ( off-- > 0 );
530 =for apidoc pad_findmy
532 Given a lexical name, try to find its offset, first in the current pad,
533 or failing that, in the pads of any lexically enclosing subs (including
534 the complications introduced by eval). If the name is found in an outer pad,
535 then a fake entry is added to the current pad.
536 Returns the offset in the current pad, or NOT_IN_PAD on failure.
542 Perl_pad_findmy(pTHX_ char *name)
548 SV **svp = AvARRAY(PL_comppad_name);
549 U32 seq = PL_cop_seqmax;
551 ASSERT_CURPAD_ACTIVE("pad_findmy");
552 DEBUG_Xv(PerlIO_printf(Perl_debug_log, "Pad findmy: \"%s\"\n", name));
554 #ifdef USE_5005THREADS
556 * Special case to get lexical (and hence per-thread) @_.
557 * XXX I need to find out how to tell at parse-time whether use
558 * of @_ should refer to a lexical (from a sub) or defgv (global
559 * scope and maybe weird sub-ish things like formats). See
560 * startsub in perly.y. It's possible that @_ could be lexical
561 * (at least from subs) even in non-threaded perl.
563 if (strEQ(name, "@_"))
564 return 0; /* success. (NOT_IN_PAD indicates failure) */
565 #endif /* USE_5005THREADS */
567 /* The one we're looking for is probably just before comppad_name_fill. */
568 for (off = AvFILLp(PL_comppad_name); off > 0; off--) {
570 if (!sv || sv == &PL_sv_undef || !strEQ(SvPVX(sv), name))
573 /* we'll use this later if we don't find a real entry */
578 if ( seq > U_32(SvNVX(sv)) /* min */
579 && seq <= (U32)SvIVX(sv)) /* max */
581 else if ((SvFLAGS(sv) & SVpad_OUR)
582 && U_32(SvNVX(sv)) == PAD_MAX) /* min */
584 /* look for an our that's being introduced; this allows
585 * our $foo = 0 unless defined $foo;
586 * to not give a warning. (Yes, this is a hack) */
594 /* See if it's in a nested scope */
595 off = pad_findlex(name, 0, PL_compcv);
596 if (off) /* pad_findlex returns 0 for failure...*/
600 return NOT_IN_PAD; /* ...but we return NOT_IN_PAD for failure */
607 =for apidoc pad_findlex
609 Find a named lexical anywhere in a chain of nested pads. Add fake entries
610 in the inner pads if it's found in an outer one. innercv is the CV *inside*
611 the chain of outer CVs to be searched. If newoff is non-null, this is a
612 run-time cloning: don't add fake entries, just find the lexical and add a
613 ref to it at newoff in the current pad.
619 S_pad_findlex(pTHX_ char *name, PADOFFSET newoff, CV* innercv)
631 ASSERT_CURPAD_ACTIVE("pad_findlex");
632 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
633 "Pad findlex: \"%s\" off=%ld startcv=0x%"UVxf"\n",
634 name, (long)newoff, PTR2UV(innercv))
637 seq = CvOUTSIDE_SEQ(innercv);
638 startcv = CvOUTSIDE(innercv);
640 for (cv = startcv; cv; seq = CvOUTSIDE_SEQ(cv), cv = CvOUTSIDE(cv)) {
645 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
646 " searching: cv=0x%"UVxf" seq=%d\n",
647 PTR2UV(cv), (int) seq )
650 curlist = CvPADLIST(cv);
652 continue; /* an undef CV */
653 svp = av_fetch(curlist, 0, FALSE);
654 if (!svp || *svp == &PL_sv_undef)
657 svp = AvARRAY(curname);
660 for (off = AvFILLp(curname); off > 0; off--) {
662 if (!sv || sv == &PL_sv_undef || !strEQ(SvPVX(sv), name))
665 /* we'll use this later if we don't find a real entry */
670 if ( seq > U_32(SvNVX(sv)) /* min */
671 && seq <= (U32)SvIVX(sv) /* max */
672 && !(newoff && !depth) /* ignore inactive when cloning */
678 /* no real entry - but did we find a fake one? */
680 if (newoff && !depth)
681 return 0; /* don't clone from inactive stack frame */
694 oldpad = (AV*)AvARRAY(curlist)[depth];
695 oldsv = *av_fetch(oldpad, off, TRUE);
699 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
700 " matched: offset %ld"
701 " FAKE, sv=0x%"UVxf"\n",
707 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
708 " matched: offset %ld"
709 " (%lu,%lu), sv=0x%"UVxf"\n",
711 (unsigned long)U_32(SvNVX(sv)),
712 (unsigned long)SvIVX(sv),
718 if (!newoff) { /* Not a mere clone operation. */
719 newoff = pad_add_name(
721 (SvFLAGS(sv) & SVpad_TYPED) ? SvSTASH(sv) : Nullhv,
722 (SvFLAGS(sv) & SVpad_OUR) ? GvSTASH(sv) : Nullhv,
726 if (CvANON(PL_compcv) || SvTYPE(PL_compcv) == SVt_PVFM) {
727 /* "It's closures all the way down." */
728 CvCLONE_on(PL_compcv);
730 if (CvANON(PL_compcv))
731 oldsv = Nullsv; /* no need to keep ref */
736 bcv && bcv != cv && !CvCLONE(bcv);
737 bcv = CvOUTSIDE(bcv))
740 /* install the missing pad entry in intervening
741 * nested subs and mark them cloneable. */
742 AV *ocomppad_name = PL_comppad_name;
743 PAD *ocomppad = PL_comppad;
744 AV *padlist = CvPADLIST(bcv);
745 PL_comppad_name = (AV*)AvARRAY(padlist)[0];
746 PL_comppad = (AV*)AvARRAY(padlist)[1];
747 PL_curpad = AvARRAY(PL_comppad);
750 (SvFLAGS(sv) & SVpad_TYPED)
751 ? SvSTASH(sv) : Nullhv,
752 (SvFLAGS(sv) & SVpad_OUR)
753 ? GvSTASH(sv) : Nullhv,
757 PL_comppad_name = ocomppad_name;
758 PL_comppad = ocomppad;
759 PL_curpad = ocomppad ?
760 AvARRAY(ocomppad) : Null(SV **);
764 if (ckWARN(WARN_CLOSURE)
765 && !CvUNIQUE(bcv) && !CvUNIQUE(cv))
767 Perl_warner(aTHX_ packWARN(WARN_CLOSURE),
768 "Variable \"%s\" may be unavailable",
776 else if (!CvUNIQUE(PL_compcv)) {
777 if (ckWARN(WARN_CLOSURE) && !SvFAKE(sv) && !CvUNIQUE(cv)
778 && !(SvFLAGS(sv) & SVpad_OUR))
780 Perl_warner(aTHX_ packWARN(WARN_CLOSURE),
781 "Variable \"%s\" will not stay shared", name);
785 av_store(PL_comppad, newoff, SvREFCNT_inc(oldsv));
786 ASSERT_CURPAD_ACTIVE("pad_findlex 2");
787 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
788 "Pad findlex: set offset %ld to sv 0x%"UVxf"\n",
789 (long)newoff, PTR2UV(oldsv)
799 Get the value at offset po in the current pad.
800 Use macro PAD_SV instead of calling this function directly.
807 Perl_pad_sv(pTHX_ PADOFFSET po)
809 ASSERT_CURPAD_ACTIVE("pad_sv");
811 #ifndef USE_5005THREADS
813 Perl_croak(aTHX_ "panic: pad_sv po");
815 DEBUG_X(PerlIO_printf(Perl_debug_log,
816 "Pad 0x%"UVxf"[0x%"UVxf"] sv: %ld sv=0x%"UVxf"\n",
817 PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long)po, PTR2UV(PL_curpad[po]))
819 return PL_curpad[po];
824 =for apidoc pad_setsv
826 Set the entry at offset po in the current pad to sv.
827 Use the macro PAD_SETSV() rather than calling this function directly.
834 Perl_pad_setsv(pTHX_ PADOFFSET po, SV* sv)
836 ASSERT_CURPAD_ACTIVE("pad_setsv");
838 DEBUG_X(PerlIO_printf(Perl_debug_log,
839 "Pad 0x%"UVxf"[0x%"UVxf"] setsv: %ld sv=0x%"UVxf"\n",
840 PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long)po, PTR2UV(sv))
849 =for apidoc pad_block_start
851 Update the pad compilation state variables on entry to a new block
857 * - integrate this in general state-saving routine ???
858 * - combine with the state-saving going on in pad_new ???
859 * - introduce a new SAVE type that does all this in one go ?
863 Perl_pad_block_start(pTHX_ int full)
865 ASSERT_CURPAD_ACTIVE("pad_block_start");
866 SAVEI32(PL_comppad_name_floor);
867 PL_comppad_name_floor = AvFILLp(PL_comppad_name);
869 PL_comppad_name_fill = PL_comppad_name_floor;
870 if (PL_comppad_name_floor < 0)
871 PL_comppad_name_floor = 0;
872 SAVEI32(PL_min_intro_pending);
873 SAVEI32(PL_max_intro_pending);
874 PL_min_intro_pending = 0;
875 SAVEI32(PL_comppad_name_fill);
876 SAVEI32(PL_padix_floor);
877 PL_padix_floor = PL_padix;
878 PL_pad_reset_pending = FALSE;
885 "Introduce" my variables to visible status.
897 ASSERT_CURPAD_ACTIVE("intro_my");
898 if (! PL_min_intro_pending)
899 return PL_cop_seqmax;
901 svp = AvARRAY(PL_comppad_name);
902 for (i = PL_min_intro_pending; i <= PL_max_intro_pending; i++) {
903 if ((sv = svp[i]) && sv != &PL_sv_undef
904 && !SvFAKE(sv) && !SvIVX(sv))
906 SvIVX(sv) = PAD_MAX; /* Don't know scope end yet. */
907 SvNVX(sv) = (NV)PL_cop_seqmax;
908 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
909 "Pad intromy: %ld \"%s\", (%lu,%lu)\n",
911 (unsigned long)U_32(SvNVX(sv)), (unsigned long)SvIVX(sv))
915 PL_min_intro_pending = 0;
916 PL_comppad_name_fill = PL_max_intro_pending; /* Needn't search higher */
917 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
918 "Pad intromy: seq -> %ld\n", (long)(PL_cop_seqmax+1)));
920 return PL_cop_seqmax++;
924 =for apidoc pad_leavemy
926 Cleanup at end of scope during compilation: set the max seq number for
927 lexicals in this scope and warn of any lexicals that never got introduced.
933 Perl_pad_leavemy(pTHX)
936 SV **svp = AvARRAY(PL_comppad_name);
939 PL_pad_reset_pending = FALSE;
941 ASSERT_CURPAD_ACTIVE("pad_leavemy");
942 if (PL_min_intro_pending && PL_comppad_name_fill < PL_min_intro_pending) {
943 for (off = PL_max_intro_pending; off >= PL_min_intro_pending; off--) {
944 if ((sv = svp[off]) && sv != &PL_sv_undef
945 && !SvFAKE(sv) && ckWARN_d(WARN_INTERNAL))
946 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
947 "%"SVf" never introduced", sv);
950 /* "Deintroduce" my variables that are leaving with this scope. */
951 for (off = AvFILLp(PL_comppad_name); off > PL_comppad_name_fill; off--) {
952 if ((sv = svp[off]) && sv != &PL_sv_undef
953 && !SvFAKE(sv) && SvIVX(sv) == PAD_MAX)
955 SvIVX(sv) = PL_cop_seqmax;
956 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
957 "Pad leavemy: %ld \"%s\", (%lu,%lu)\n",
958 (long)off, SvPVX(sv),
959 (unsigned long)U_32(SvNVX(sv)), (unsigned long)SvIVX(sv))
964 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
965 "Pad leavemy: seq = %ld\n", (long)PL_cop_seqmax));
970 =for apidoc pad_swipe
972 Abandon the tmp in the current pad at offset po and replace with a
979 Perl_pad_swipe(pTHX_ PADOFFSET po, bool refadjust)
981 ASSERT_CURPAD_LEGAL("pad_swipe");
984 if (AvARRAY(PL_comppad) != PL_curpad)
985 Perl_croak(aTHX_ "panic: pad_swipe curpad");
987 Perl_croak(aTHX_ "panic: pad_swipe po");
989 DEBUG_X(PerlIO_printf(Perl_debug_log,
990 "Pad 0x%"UVxf"[0x%"UVxf"] swipe: %ld\n",
991 PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long)po));
994 SvPADTMP_off(PL_curpad[po]);
996 SvREFCNT_dec(PL_curpad[po]);
998 PL_curpad[po] = NEWSV(1107,0);
999 SvPADTMP_on(PL_curpad[po]);
1000 if ((I32)po < PL_padix)
1006 =for apidoc pad_reset
1008 Mark all the current temporaries for reuse
1013 /* XXX pad_reset() is currently disabled because it results in serious bugs.
1014 * It causes pad temp TARGs to be shared between OPs. Since TARGs are pushed
1015 * on the stack by OPs that use them, there are several ways to get an alias
1016 * to a shared TARG. Such an alias will change randomly and unpredictably.
1017 * We avoid doing this until we can think of a Better Way.
1020 Perl_pad_reset(pTHX)
1022 #ifdef USE_BROKEN_PAD_RESET
1025 if (AvARRAY(PL_comppad) != PL_curpad)
1026 Perl_croak(aTHX_ "panic: pad_reset curpad");
1028 DEBUG_X(PerlIO_printf(Perl_debug_log,
1029 "Pad 0x%"UVxf"[0x%"UVxf"] reset: padix %ld -> %ld",
1030 PTR2UV(PL_comppad), PTR2UV(PL_curpad),
1031 (long)PL_padix, (long)PL_padix_floor
1035 if (!PL_tainting) { /* Can't mix tainted and non-tainted temporaries. */
1036 for (po = AvMAX(PL_comppad); po > PL_padix_floor; po--) {
1037 if (PL_curpad[po] && !SvIMMORTAL(PL_curpad[po]))
1038 SvPADTMP_off(PL_curpad[po]);
1040 PL_padix = PL_padix_floor;
1043 PL_pad_reset_pending = FALSE;
1048 =for apidoc pad_tidy
1050 Tidy up a pad after we've finished compiling it:
1051 * remove most stuff from the pads of anonsub prototypes;
1053 * mark tmps as such.
1058 /* XXX DAPM surely most of this stuff should be done properly
1059 * at the right time beforehand, rather than going around afterwards
1060 * cleaning up our mistakes ???
1064 Perl_pad_tidy(pTHX_ padtidy_type type)
1068 ASSERT_CURPAD_ACTIVE("pad_tidy");
1069 /* extend curpad to match namepad */
1070 if (AvFILLp(PL_comppad_name) < AvFILLp(PL_comppad))
1071 av_store(PL_comppad_name, AvFILLp(PL_comppad), Nullsv);
1073 if (type == padtidy_SUBCLONE) {
1074 SV **namep = AvARRAY(PL_comppad_name);
1075 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
1078 if (SvIMMORTAL(PL_curpad[ix]) || IS_PADGV(PL_curpad[ix]) || IS_PADCONST(PL_curpad[ix]))
1081 * The only things that a clonable function needs in its
1082 * pad are references to outer lexicals and anonymous subs.
1083 * The rest are created anew during cloning.
1085 if (!((namesv = namep[ix]) != Nullsv &&
1086 namesv != &PL_sv_undef &&
1088 *SvPVX(namesv) == '&')))
1090 SvREFCNT_dec(PL_curpad[ix]);
1091 PL_curpad[ix] = Nullsv;
1095 else if (type == padtidy_SUB) {
1096 /* XXX DAPM this same bit of code keeps appearing !!! Rationalise? */
1097 AV *av = newAV(); /* Will be @_ */
1099 av_store(PL_comppad, 0, (SV*)av);
1100 AvFLAGS(av) = AVf_REIFY;
1103 /* XXX DAPM rationalise these two similar branches */
1105 if (type == padtidy_SUB) {
1106 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
1107 if (SvIMMORTAL(PL_curpad[ix]) || IS_PADGV(PL_curpad[ix]) || IS_PADCONST(PL_curpad[ix]))
1109 if (!SvPADMY(PL_curpad[ix]))
1110 SvPADTMP_on(PL_curpad[ix]);
1113 else if (type == padtidy_FORMAT) {
1114 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
1115 if (!SvPADMY(PL_curpad[ix]) && !SvIMMORTAL(PL_curpad[ix]))
1116 SvPADTMP_on(PL_curpad[ix]);
1119 PL_curpad = AvARRAY(PL_comppad);
1124 =for apidoc pad_free
1126 Free the SV at offet po in the current pad.
1131 /* XXX DAPM integrate with pad_swipe ???? */
1133 Perl_pad_free(pTHX_ PADOFFSET po)
1135 ASSERT_CURPAD_LEGAL("pad_free");
1138 if (AvARRAY(PL_comppad) != PL_curpad)
1139 Perl_croak(aTHX_ "panic: pad_free curpad");
1141 Perl_croak(aTHX_ "panic: pad_free po");
1143 DEBUG_X(PerlIO_printf(Perl_debug_log,
1144 "Pad 0x%"UVxf"[0x%"UVxf"] free: %ld\n",
1145 PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long)po)
1148 if (PL_curpad[po] && PL_curpad[po] != &PL_sv_undef) {
1149 SvPADTMP_off(PL_curpad[po]);
1151 /* SV could be a shared hash key (eg bugid #19022) */
1152 if (!SvFAKE(PL_curpad[po]))
1153 SvREADONLY_off(PL_curpad[po]); /* could be a freed constant */
1157 if ((I32)po < PL_padix)
1164 =for apidoc do_dump_pad
1166 Dump the contents of a padlist
1172 Perl_do_dump_pad(pTHX_ I32 level, PerlIO *file, PADLIST *padlist, int full)
1184 pad_name = (AV*)*av_fetch((AV*)padlist, 0, FALSE);
1185 pad = (AV*)*av_fetch((AV*)padlist, 1, FALSE);
1186 pname = AvARRAY(pad_name);
1187 ppad = AvARRAY(pad);
1188 Perl_dump_indent(aTHX_ level, file,
1189 "PADNAME = 0x%"UVxf"(0x%"UVxf") PAD = 0x%"UVxf"(0x%"UVxf")\n",
1190 PTR2UV(pad_name), PTR2UV(pname), PTR2UV(pad), PTR2UV(ppad)
1193 for (ix = 1; ix <= AvFILLp(pad_name); ix++) {
1195 if (namesv && namesv == &PL_sv_undef) {
1200 Perl_dump_indent(aTHX_ level+1, file,
1201 "%2d. 0x%"UVxf"<%lu> FAKE \"%s\"\n",
1204 (unsigned long) (ppad[ix] ? SvREFCNT(ppad[ix]) : 0),
1208 Perl_dump_indent(aTHX_ level+1, file,
1209 "%2d. 0x%"UVxf"<%lu> (%lu,%lu) \"%s\"\n",
1212 (unsigned long) (ppad[ix] ? SvREFCNT(ppad[ix]) : 0),
1213 (unsigned long)U_32(SvNVX(namesv)),
1214 (unsigned long)SvIVX(namesv),
1219 Perl_dump_indent(aTHX_ level+1, file,
1220 "%2d. 0x%"UVxf"<%lu>\n",
1223 (unsigned long) (ppad[ix] ? SvREFCNT(ppad[ix]) : 0)
1234 dump the contents of a CV
1241 S_cv_dump(pTHX_ CV *cv, char *title)
1243 CV *outside = CvOUTSIDE(cv);
1244 AV* padlist = CvPADLIST(cv);
1246 PerlIO_printf(Perl_debug_log,
1247 " %s: CV=0x%"UVxf" (%s), OUTSIDE=0x%"UVxf" (%s)\n",
1250 (CvANON(cv) ? "ANON"
1251 : (cv == PL_main_cv) ? "MAIN"
1252 : CvUNIQUE(cv) ? "UNIQUE"
1253 : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"),
1256 : CvANON(outside) ? "ANON"
1257 : (outside == PL_main_cv) ? "MAIN"
1258 : CvUNIQUE(outside) ? "UNIQUE"
1259 : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED"));
1261 PerlIO_printf(Perl_debug_log,
1262 " PADLIST = 0x%"UVxf"\n", PTR2UV(padlist));
1263 do_dump_pad(1, Perl_debug_log, padlist, 1);
1265 #endif /* DEBUGGING */
1272 =for apidoc cv_clone
1274 Clone a CV: make a new CV which points to the same code etc, but which
1275 has a newly-created pad built by copying the prototype pad and capturing
1282 Perl_cv_clone(pTHX_ CV *proto)
1286 LOCK_CRED_MUTEX; /* XXX create separate mutex */
1287 cv = cv_clone2(proto, CvOUTSIDE(proto));
1288 UNLOCK_CRED_MUTEX; /* XXX create separate mutex */
1293 /* XXX DAPM separate out cv and paddish bits ???
1294 * ideally the CV-related stuff shouldn't be in pad.c - how about
1298 S_cv_clone2(pTHX_ CV *proto, CV *outside)
1301 AV* protopadlist = CvPADLIST(proto);
1302 AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE);
1303 AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE);
1304 SV** pname = AvARRAY(protopad_name);
1305 SV** ppad = AvARRAY(protopad);
1306 I32 fname = AvFILLp(protopad_name);
1307 I32 fpad = AvFILLp(protopad);
1311 assert(!CvUNIQUE(proto));
1314 SAVESPTR(PL_compcv);
1316 cv = PL_compcv = (CV*)NEWSV(1104, 0);
1317 sv_upgrade((SV *)cv, SvTYPE(proto));
1318 CvFLAGS(cv) = CvFLAGS(proto) & ~(CVf_CLONE|CVf_WEAKOUTSIDE);
1321 #ifdef USE_5005THREADS
1322 New(666, CvMUTEXP(cv), 1, perl_mutex);
1323 MUTEX_INIT(CvMUTEXP(cv));
1325 #endif /* USE_5005THREADS */
1327 CvFILE(cv) = CvXSUB(proto) ? CvFILE(proto)
1328 : savepv(CvFILE(proto));
1330 CvFILE(cv) = CvFILE(proto);
1332 CvGV(cv) = CvGV(proto);
1333 CvSTASH(cv) = CvSTASH(proto);
1334 CvROOT(cv) = OpREFCNT_inc(CvROOT(proto));
1335 CvSTART(cv) = CvSTART(proto);
1337 CvOUTSIDE(cv) = (CV*)SvREFCNT_inc(outside);
1338 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(proto);
1342 sv_setpvn((SV*)cv, SvPVX(proto), SvCUR(proto));
1344 CvPADLIST(cv) = comppadlist = pad_new(padnew_CLONE|padnew_SAVE);
1346 for (ix = fname; ix >= 0; ix--)
1347 av_store(PL_comppad_name, ix, SvREFCNT_inc(pname[ix]));
1349 av_fill(PL_comppad, fpad);
1350 PL_curpad = AvARRAY(PL_comppad);
1352 for (ix = fpad; ix > 0; ix--) {
1353 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
1354 if (namesv && namesv != &PL_sv_undef) {
1355 char *name = SvPVX(namesv); /* XXX */
1356 if (SvFLAGS(namesv) & SVf_FAKE) { /* lexical from outside? */
1357 I32 off = pad_findlex(name, ix, cv);
1359 PL_curpad[ix] = SvREFCNT_inc(ppad[ix]);
1361 Perl_croak(aTHX_ "panic: cv_clone: %s", name);
1363 else { /* our own lexical */
1366 /* anon code -- we'll come back for it */
1367 sv = SvREFCNT_inc(ppad[ix]);
1369 else if (*name == '@')
1371 else if (*name == '%')
1380 else if (IS_PADGV(ppad[ix]) || IS_PADCONST(ppad[ix])) {
1381 PL_curpad[ix] = SvREFCNT_inc(ppad[ix]);
1384 SV* sv = NEWSV(0, 0);
1390 /* Now that vars are all in place, clone nested closures. */
1392 for (ix = fpad; ix > 0; ix--) {
1393 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
1395 && namesv != &PL_sv_undef
1396 && !(SvFLAGS(namesv) & SVf_FAKE)
1397 && *SvPVX(namesv) == '&'
1398 && CvCLONE(ppad[ix]))
1400 CV *kid = cv_clone2((CV*)ppad[ix], cv);
1401 SvREFCNT_dec(ppad[ix]);
1404 PL_curpad[ix] = (SV*)kid;
1405 /* '&' entry points to child, so child mustn't refcnt parent */
1406 CvWEAKOUTSIDE_on(kid);
1412 PerlIO_printf(Perl_debug_log, "\nPad CV clone\n");
1413 cv_dump(outside, "Outside");
1414 cv_dump(proto, "Proto");
1421 SV* const_sv = op_const_sv(CvSTART(cv), cv);
1423 /* constant sub () { $x } closing over $x - see lib/constant.pm */
1425 cv = newCONSTSUB(CvSTASH(proto), 0, const_sv);
1433 =for apidoc pad_fixup_inner_anons
1435 For any anon CVs in the pad, change CvOUTSIDE of that CV from
1436 old_cv to new_cv if necessary. Needed when a newly-compiled CV has to be
1437 moved to a pre-existing CV struct.
1443 Perl_pad_fixup_inner_anons(pTHX_ PADLIST *padlist, CV *old_cv, CV *new_cv)
1446 AV *comppad_name = (AV*)AvARRAY(padlist)[0];
1447 AV *comppad = (AV*)AvARRAY(padlist)[1];
1448 SV **namepad = AvARRAY(comppad_name);
1449 SV **curpad = AvARRAY(comppad);
1450 for (ix = AvFILLp(comppad_name); ix > 0; ix--) {
1451 SV *namesv = namepad[ix];
1452 if (namesv && namesv != &PL_sv_undef
1453 && *SvPVX(namesv) == '&')
1455 CV *innercv = (CV*)curpad[ix];
1456 assert(CvWEAKOUTSIDE(innercv));
1457 assert(CvOUTSIDE(innercv) == old_cv);
1458 CvOUTSIDE(innercv) = new_cv;
1465 =for apidoc pad_push
1467 Push a new pad frame onto the padlist, unless there's already a pad at
1468 this depth, in which case don't bother creating a new one.
1469 If has_args is true, give the new pad an @_ in slot zero.
1475 Perl_pad_push(pTHX_ PADLIST *padlist, int depth, int has_args)
1477 if (depth <= AvFILLp(padlist))
1481 SV** svp = AvARRAY(padlist);
1482 AV *newpad = newAV();
1483 SV **oldpad = AvARRAY(svp[depth-1]);
1484 I32 ix = AvFILLp((AV*)svp[1]);
1485 I32 names_fill = AvFILLp((AV*)svp[0]);
1486 SV** names = AvARRAY(svp[0]);
1488 for ( ;ix > 0; ix--) {
1489 if (names_fill >= ix && names[ix] != &PL_sv_undef) {
1490 char *name = SvPVX(names[ix]);
1491 if ((SvFLAGS(names[ix]) & SVf_FAKE) || *name == '&') {
1492 /* outer lexical or anon code */
1493 av_store(newpad, ix, SvREFCNT_inc(oldpad[ix]));
1495 else { /* our own lexical */
1497 av_store(newpad, ix, sv = (SV*)newAV());
1498 else if (*name == '%')
1499 av_store(newpad, ix, sv = (SV*)newHV());
1501 av_store(newpad, ix, sv = NEWSV(0, 0));
1505 else if (IS_PADGV(oldpad[ix]) || IS_PADCONST(oldpad[ix])) {
1506 av_store(newpad, ix, sv = SvREFCNT_inc(oldpad[ix]));
1509 /* save temporaries on recursion? */
1510 av_store(newpad, ix, sv = NEWSV(0, 0));
1517 av_store(newpad, 0, (SV*)av);
1518 AvFLAGS(av) = AVf_REIFY;
1520 av_store(padlist, depth, (SV*)newpad);
1521 AvFILLp(padlist) = depth;