3 * Copyright (c) 1991-1997, Larry Wall
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.
11 * "You see: Mr. Drogo, he married poor Miss Primula Brandybuck. She was
12 * our Mr. Bilbo's first cousin on the mother's side (her mother being the
13 * youngest of the Old Took's daughters); and Mr. Drogo was his second
14 * cousin. So Mr. Frodo is his first *and* second cousin, once removed
15 * either way, as the saying is, if you follow me." --the Gaffer
22 * In the following definition, the ", Nullop" is just to make the compiler
23 * think the expression is of the right type: croak actually does a Siglongjmp.
25 #define CHECKOP(type,o) \
26 ((op_mask && op_mask[type]) \
27 ? ( op_free((OP*)o), \
28 croak("%s trapped by operation mask", op_desc[type]), \
30 : (*check[type])((OP*)o))
32 static I32 list_assignment _((OP *o));
33 static void bad_type _((I32 n, char *t, char *name, OP *kid));
34 static OP *modkids _((OP *o, I32 type));
35 static OP *no_fh_allowed _((OP *o));
36 static bool scalar_mod_type _((OP *o, I32 type));
37 static OP *scalarboolean _((OP *o));
38 static OP *too_few_arguments _((OP *o, char* name));
39 static OP *too_many_arguments _((OP *o, char* name));
40 static void null _((OP* o));
41 static PADOFFSET pad_findlex _((char* name, PADOFFSET newoff, U32 seq,
42 CV* startcv, I32 cx_ix));
43 static OP *newDEFSVOP _((void));
44 static OP *new_logop _((I32 type, I32 flags, OP **firstp, OP **otherp));
49 SV* tmpsv = sv_newmortal();
50 gv_efullname3(tmpsv, gv, Nullch);
51 return SvPV(tmpsv,na);
57 yyerror(form("Missing comma after first argument to %s function",
58 op_desc[o->op_type]));
63 too_few_arguments(OP *o, char *name)
65 yyerror(form("Not enough arguments for %s", name));
70 too_many_arguments(OP *o, char *name)
72 yyerror(form("Too many arguments for %s", name));
77 bad_type(I32 n, char *t, char *name, OP *kid)
79 yyerror(form("Type of arg %d to %s must be %s (not %s)",
80 (int)n, name, t, op_desc[kid->op_type]));
86 int type = o->op_type;
87 if (type != OP_AELEM && type != OP_HELEM) {
88 yyerror(form("Can't use subscript on %s", op_desc[type]));
89 if (type == OP_ENTERSUB || type == OP_RV2HV || type == OP_PADHV) {
91 newSVpvf("(Did you mean $ or @ instead of %c?)\n",
92 type == OP_ENTERSUB ? '&' : '%'));
96 sv_catsv(GvSV(errgv), msg);
98 PerlIO_write(PerlIO_stderr(), SvPVX(msg), SvCUR(msg));
103 /* "register" allocation */
106 pad_allocmy(char *name)
112 if (!(isALPHA(name[1]) || name[1] == '_' && (int)strlen(name) > 2)) {
113 if (!isPRINT(name[1])) {
115 name[2] = toCTRL(name[1]);
118 croak("Can't use global %s in \"my\"",name);
120 if (dowarn && AvFILLp(comppad_name) >= 0) {
121 SV **svp = AvARRAY(comppad_name);
122 for (off = AvFILLp(comppad_name); off > comppad_name_floor; off--) {
125 && SvIVX(sv) == 999999999 /* var is in open scope */
126 && strEQ(name, SvPVX(sv)))
128 warn("\"my\" variable %s masks earlier declaration in same scope", name);
133 off = pad_alloc(OP_PADSV, SVs_PADMY);
135 sv_upgrade(sv, SVt_PVNV);
139 croak("Can't declare class for non-scalar %s in \"my\"",name);
141 (void)SvUPGRADE(sv, SVt_PVMG);
142 SvSTASH(sv) = (HV*)SvREFCNT_inc(in_my_stash);
145 av_store(comppad_name, off, sv);
146 SvNVX(sv) = (double)999999999;
147 SvIVX(sv) = 0; /* Not yet introduced--see newSTATEOP */
148 if (!min_intro_pending)
149 min_intro_pending = off;
150 max_intro_pending = off;
152 av_store(comppad, off, (SV*)newAV());
153 else if (*name == '%')
154 av_store(comppad, off, (SV*)newHV());
155 SvPADMY_on(curpad[off]);
160 pad_findlex(char *name, PADOFFSET newoff, U32 seq, CV* startcv, I32 cx_ix)
167 register PERL_CONTEXT *cx;
170 for (cv = startcv; cv; cv = CvOUTSIDE(cv)) {
171 AV *curlist = CvPADLIST(cv);
172 SV **svp = av_fetch(curlist, 0, FALSE);
175 if (!svp || *svp == &sv_undef)
178 svp = AvARRAY(curname);
179 for (off = AvFILLp(curname); off > 0; off--) {
180 if ((sv = svp[off]) &&
183 seq > I_32(SvNVX(sv)) &&
184 strEQ(SvPVX(sv), name))
195 return 0; /* don't clone from inactive stack frame */
199 oldpad = (AV*)*av_fetch(curlist, depth, FALSE);
200 oldsv = *av_fetch(oldpad, off, TRUE);
201 if (!newoff) { /* Not a mere clone operation. */
202 SV *namesv = NEWSV(1103,0);
203 newoff = pad_alloc(OP_PADSV, SVs_PADMY);
204 sv_upgrade(namesv, SVt_PVNV);
205 sv_setpv(namesv, name);
206 av_store(comppad_name, newoff, namesv);
207 SvNVX(namesv) = (double)curcop->cop_seq;
208 SvIVX(namesv) = 999999999; /* A ref, intro immediately */
209 SvFAKE_on(namesv); /* A ref, not a real var */
210 if (CvANON(compcv) || SvTYPE(compcv) == SVt_PVFM) {
211 /* "It's closures all the way down." */
215 oldsv = Nullsv; /* no need to keep ref */
220 bcv && bcv != cv && !CvCLONE(bcv);
221 bcv = CvOUTSIDE(bcv)) {
225 if (dowarn && !CvUNIQUE(cv))
227 "Variable \"%s\" may be unavailable",
234 else if (!CvUNIQUE(compcv)) {
235 if (dowarn && !SvFAKE(sv) && !CvUNIQUE(cv))
236 warn("Variable \"%s\" will not stay shared", name);
239 av_store(comppad, newoff, SvREFCNT_inc(oldsv));
245 /* Nothing in current lexical context--try eval's context, if any.
246 * This is necessary to let the perldb get at lexically scoped variables.
247 * XXX This will also probably interact badly with eval tree caching.
251 for (i = cx_ix; i >= 0; i--) {
253 switch (cx->cx_type) {
255 if (i == 0 && saweval) {
256 seq = cxstack[saweval].blk_oldcop->cop_seq;
257 return pad_findlex(name, newoff, seq, main_cv, 0);
261 switch (cx->blk_eval.old_op_type) {
266 /* require must have its own scope */
274 if (debstash && CvSTASH(cv) == debstash) { /* ignore DB'* scope */
275 saweval = i; /* so we know where we were called from */
278 seq = cxstack[saweval].blk_oldcop->cop_seq;
279 return pad_findlex(name, newoff, seq, cv, i-1);
287 pad_findmy(char *name)
293 SV **svp = AvARRAY(comppad_name);
294 U32 seq = cop_seqmax;
298 * Special case to get lexical (and hence per-thread) @_.
299 * XXX I need to find out how to tell at parse-time whether use
300 * of @_ should refer to a lexical (from a sub) or defgv (global
301 * scope and maybe weird sub-ish things like formats). See
302 * startsub in perly.y. It's possible that @_ could be lexical
303 * (at least from subs) even in non-threaded perl.
305 if (strEQ(name, "@_"))
306 return 0; /* success. (NOT_IN_PAD indicates failure) */
307 #endif /* USE_THREADS */
309 /* The one we're looking for is probably just before comppad_name_fill. */
310 for (off = AvFILLp(comppad_name); off > 0; off--) {
311 if ((sv = svp[off]) &&
315 seq > I_32(SvNVX(sv)))) &&
316 strEQ(SvPVX(sv), name))
319 return (PADOFFSET)off;
320 pendoff = off; /* this pending def. will override import */
324 /* See if it's in a nested scope */
325 off = pad_findlex(name, 0, seq, CvOUTSIDE(compcv), cxstack_ix);
327 /* If there is a pending local definition, this new alias must die */
329 SvIVX(AvARRAY(comppad_name)[off]) = seq;
330 return off; /* pad_findlex returns 0 for failure...*/
332 return NOT_IN_PAD; /* ...but we return NOT_IN_PAD for failure */
336 pad_leavemy(I32 fill)
339 SV **svp = AvARRAY(comppad_name);
341 if (min_intro_pending && fill < min_intro_pending) {
342 for (off = max_intro_pending; off >= min_intro_pending; off--) {
343 if ((sv = svp[off]) && sv != &sv_undef)
344 warn("%s never introduced", SvPVX(sv));
347 /* "Deintroduce" my variables that are leaving with this scope. */
348 for (off = AvFILLp(comppad_name); off > fill; off--) {
349 if ((sv = svp[off]) && sv != &sv_undef && SvIVX(sv) == 999999999)
350 SvIVX(sv) = cop_seqmax;
355 pad_alloc(I32 optype, U32 tmptype)
361 if (AvARRAY(comppad) != curpad)
362 croak("panic: pad_alloc");
363 if (pad_reset_pending)
365 if (tmptype & SVs_PADMY) {
367 sv = *av_fetch(comppad, AvFILLp(comppad) + 1, TRUE);
368 } while (SvPADBUSY(sv)); /* need a fresh one */
369 retval = AvFILLp(comppad);
372 SV **names = AvARRAY(comppad_name);
373 SSize_t names_fill = AvFILLp(comppad_name);
376 * "foreach" index vars temporarily become aliases to non-"my"
377 * values. Thus we must skip, not just pad values that are
378 * marked as current pad values, but also those with names.
380 if (++padix <= names_fill &&
381 (sv = names[padix]) && sv != &sv_undef)
383 sv = *av_fetch(comppad, padix, TRUE);
384 if (!(SvFLAGS(sv) & (SVs_PADTMP|SVs_PADMY)))
389 SvFLAGS(sv) |= tmptype;
390 curpad = AvARRAY(comppad);
392 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx alloc %ld for %s\n",
393 (unsigned long) thr, (unsigned long) curpad,
394 (long) retval, op_name[optype]));
396 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx alloc %ld for %s\n",
397 (unsigned long) curpad,
398 (long) retval, op_name[optype]));
399 #endif /* USE_THREADS */
400 return (PADOFFSET)retval;
408 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx sv %d\n",
409 (unsigned long) thr, (unsigned long) curpad, po));
412 croak("panic: pad_sv po");
413 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx sv %d\n",
414 (unsigned long) curpad, po));
415 #endif /* USE_THREADS */
416 return curpad[po]; /* eventually we'll turn this into a macro */
420 pad_free(PADOFFSET po)
425 if (AvARRAY(comppad) != curpad)
426 croak("panic: pad_free curpad");
428 croak("panic: pad_free po");
430 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx free %d\n",
431 (unsigned long) thr, (unsigned long) curpad, po));
433 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx free %d\n",
434 (unsigned long) curpad, po));
435 #endif /* USE_THREADS */
436 if (curpad[po] && curpad[po] != &sv_undef)
437 SvPADTMP_off(curpad[po]);
443 pad_swipe(PADOFFSET po)
446 if (AvARRAY(comppad) != curpad)
447 croak("panic: pad_swipe curpad");
449 croak("panic: pad_swipe po");
451 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx swipe %d\n",
452 (unsigned long) thr, (unsigned long) curpad, po));
454 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx swipe %d\n",
455 (unsigned long) curpad, po));
456 #endif /* USE_THREADS */
457 SvPADTMP_off(curpad[po]);
458 curpad[po] = NEWSV(1107,0);
459 SvPADTMP_on(curpad[po]);
464 /* XXX pad_reset() is currently disabled because it results in serious bugs.
465 * It causes pad temp TARGs to be shared between OPs. Since TARGs are pushed
466 * on the stack by OPs that use them, there are several ways to get an alias
467 * to a shared TARG. Such an alias will change randomly and unpredictably.
468 * We avoid doing this until we can think of a Better Way.
473 #ifdef USE_BROKEN_PAD_RESET
477 if (AvARRAY(comppad) != curpad)
478 croak("panic: pad_reset curpad");
480 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx reset\n",
481 (unsigned long) thr, (unsigned long) curpad));
483 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx reset\n",
484 (unsigned long) curpad));
485 #endif /* USE_THREADS */
486 if (!tainting) { /* Can't mix tainted and non-tainted temporaries. */
487 for (po = AvMAX(comppad); po > padix_floor; po--) {
488 if (curpad[po] && !SvIMMORTAL(curpad[po]))
489 SvPADTMP_off(curpad[po]);
494 pad_reset_pending = FALSE;
498 /* find_threadsv is not reentrant */
500 find_threadsv(char *name)
506 /* We currently only handle names of a single character */
507 p = strchr(threadsv_names, *name);
510 key = p - threadsv_names;
511 svp = av_fetch(thr->threadsv, key, FALSE);
513 SV *sv = NEWSV(0, 0);
514 av_store(thr->threadsv, key, sv);
515 thr->threadsvp = AvARRAY(thr->threadsv);
517 * Some magic variables used to be automagically initialised
518 * in gv_fetchpv. Those which are now per-thread magicals get
519 * initialised here instead.
525 sv_setpv(sv, "\034");
526 sv_magic(sv, 0, 0, name, 1);
535 sv_magic(sv, 0, 0, name, 1);
537 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
538 "find_threadsv: new SV %p for $%s%c\n",
539 sv, (*name < 32) ? "^" : "",
540 (*name < 32) ? toCTRL(*name) : *name));
544 #endif /* USE_THREADS */
551 register OP *kid, *nextkid;
553 if (!o || o->op_seq == (U16)-1)
556 if (o->op_flags & OPf_KIDS) {
557 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
558 nextkid = kid->op_sibling; /* Get before next freeing kid */
563 switch (o->op_type) {
565 o->op_targ = 0; /* Was holding old type, if any. */
568 o->op_targ = 0; /* Was holding hints. */
572 o->op_targ = 0; /* Was holding index into thr->threadsv AV. */
574 #endif /* USE_THREADS */
576 if (!(o->op_flags & OPf_REF) || (check[o->op_type] != ck_ftst))
582 SvREFCNT_dec(cGVOPo->op_gv);
586 Safefree(cCOPo->cop_label);
587 SvREFCNT_dec(cCOPo->cop_filegv);
590 SvREFCNT_dec(cSVOPo->op_sv);
596 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
600 Safefree(cPVOPo->op_pv);
603 op_free(cPMOPo->op_pmreplroot);
607 ReREFCNT_dec(cPMOPo->op_pmregexp);
612 pad_free(o->op_targ);
620 if (o->op_type != OP_NULL && o->op_type != OP_THREADSV && o->op_targ > 0)
621 pad_free(o->op_targ);
622 o->op_targ = o->op_type;
623 o->op_type = OP_NULL;
624 o->op_ppaddr = ppaddr[OP_NULL];
627 /* Contextualizers */
629 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
639 /* establish postfix order */
640 if (cUNOPo->op_first) {
641 o->op_next = LINKLIST(cUNOPo->op_first);
642 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
644 kid->op_next = LINKLIST(kid->op_sibling);
659 if (o && o->op_flags & OPf_KIDS) {
660 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
670 o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
672 line_t oldline = curcop->cop_line;
674 if (copline != NOLINE)
675 curcop->cop_line = copline;
676 warn("Found = in conditional, should be ==");
677 curcop->cop_line = oldline;
687 /* assumes no premature commitment */
688 if (!o || (o->op_flags & OPf_WANT) || error_count
689 || o->op_type == OP_RETURN)
692 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
694 switch (o->op_type) {
696 if (o->op_private & OPpREPEAT_DOLIST)
697 null(((LISTOP*)cBINOPo->op_first)->op_first);
698 scalar(cBINOPo->op_first);
703 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
707 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
708 if (!kPMOP->op_pmreplroot)
709 deprecate("implicit split to @_");
716 if (o->op_flags & OPf_KIDS) {
717 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
723 kid = cLISTOPo->op_first;
725 while (kid = kid->op_sibling) {
731 WITH_THR(curcop = &compiling);
736 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
742 WITH_THR(curcop = &compiling);
755 /* assumes no premature commitment */
756 if (!o || (o->op_flags & OPf_WANT) == OPf_WANT_LIST || error_count
757 || o->op_type == OP_RETURN)
760 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
762 switch (o->op_type) {
764 if (!(opargs[o->op_type] & OA_FOLDCONST))
768 if (o->op_flags & OPf_STACKED)
839 if (!(o->op_private & OPpLVAL_INTRO))
840 useless = op_desc[o->op_type];
847 if (!(o->op_private & OPpLVAL_INTRO) &&
848 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
849 useless = "a variable";
854 WITH_THR(curcop = ((COP*)o)); /* for warning below */
860 useless = "a constant";
861 if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
863 else if (SvPOK(sv)) {
864 if (strnEQ(SvPVX(sv), "di", 2) ||
865 strnEQ(SvPVX(sv), "ds", 2) ||
866 strnEQ(SvPVX(sv), "ig", 2))
870 null(o); /* don't execute a constant */
871 SvREFCNT_dec(sv); /* don't even remember it */
875 o->op_type = OP_PREINC; /* pre-increment is faster */
876 o->op_ppaddr = ppaddr[OP_PREINC];
880 o->op_type = OP_PREDEC; /* pre-decrement is faster */
881 o->op_ppaddr = ppaddr[OP_PREDEC];
887 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
892 if (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE)
893 WITH_THR(curcop = ((COP*)o)); /* for warning below */
894 if (o->op_flags & OPf_STACKED)
900 if (!(o->op_flags & OPf_KIDS))
909 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
916 /* all requires must return a boolean value */
917 o->op_flags &= ~OPf_WANT;
920 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
921 if (!kPMOP->op_pmreplroot)
922 deprecate("implicit split to @_");
926 if (useless && dowarn)
927 warn("Useless use of %s in void context", useless);
935 if (o && o->op_flags & OPf_KIDS) {
936 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
947 /* assumes no premature commitment */
948 if (!o || (o->op_flags & OPf_WANT) || error_count
949 || o->op_type == OP_RETURN)
952 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
954 switch (o->op_type) {
957 list(cBINOPo->op_first);
962 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
969 if (!(o->op_flags & OPf_KIDS))
971 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
972 list(cBINOPo->op_first);
973 return gen_constant_list(o);
980 kid = cLISTOPo->op_first;
982 while (kid = kid->op_sibling) {
988 WITH_THR(curcop = &compiling);
992 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
998 WITH_THR(curcop = &compiling);
1001 /* all requires must return a boolean value */
1002 o->op_flags &= ~OPf_WANT;
1014 if (o->op_type == OP_LINESEQ ||
1015 o->op_type == OP_SCOPE ||
1016 o->op_type == OP_LEAVE ||
1017 o->op_type == OP_LEAVETRY)
1020 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1021 if (kid->op_sibling) {
1025 curcop = &compiling;
1027 o->op_flags &= ~OPf_PARENS;
1028 if (hints & HINT_BLOCK_SCOPE)
1029 o->op_flags |= OPf_PARENS;
1032 o = newOP(OP_STUB, 0);
1037 modkids(OP *o, I32 type)
1040 if (o && o->op_flags & OPf_KIDS) {
1041 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1047 static I32 modcount;
1050 mod(OP *o, I32 type)
1056 if (!o || error_count)
1059 switch (o->op_type) {
1064 if (!(o->op_private & (OPpCONST_ARYBASE)))
1066 if (eval_start && eval_start->op_type == OP_CONST) {
1067 compiling.cop_arybase = (I32)SvIV(((SVOP*)eval_start)->op_sv);
1071 SAVEI32(compiling.cop_arybase);
1072 compiling.cop_arybase = 0;
1074 else if (type == OP_REFGEN)
1077 croak("That use of $[ is unsupported");
1080 if (o->op_flags & OPf_PARENS)
1084 if ((type == OP_UNDEF || type == OP_REFGEN) &&
1085 !(o->op_flags & OPf_STACKED)) {
1086 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1087 o->op_ppaddr = ppaddr[OP_RV2CV];
1088 assert(cUNOPo->op_first->op_type == OP_NULL);
1089 null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1095 /* grep, foreach, subcalls, refgen */
1096 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1098 yyerror(form("Can't modify %s in %s",
1099 op_desc[o->op_type],
1100 type ? op_desc[type] : "local"));
1114 case OP_RIGHT_SHIFT:
1123 if (!(o->op_flags & OPf_STACKED))
1129 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1135 if (!type && cUNOPo->op_first->op_type != OP_GV)
1136 croak("Can't localize through a reference");
1137 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
1139 return o; /* Treat \(@foo) like ordinary list. */
1143 if (scalar_mod_type(o, type))
1145 ref(cUNOPo->op_first, o->op_type);
1157 if (!type && cUNOPo->op_first->op_type != OP_GV)
1158 croak("Can't localize through a reference");
1159 ref(cUNOPo->op_first, o->op_type);
1163 hints |= HINT_BLOCK_SCOPE;
1172 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1173 return o; /* Treat \(@foo) like ordinary list. */
1174 if (scalar_mod_type(o, type))
1180 croak("Can't localize lexical variable %s",
1181 SvPV(*av_fetch(comppad_name, o->op_targ, 4), na));
1186 modcount++; /* XXX ??? */
1188 #endif /* USE_THREADS */
1194 if (type != OP_SASSIGN)
1200 pad_free(o->op_targ);
1201 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
1202 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
1203 if (o->op_flags & OPf_KIDS)
1204 mod(cBINOPo->op_first->op_sibling, type);
1209 ref(cBINOPo->op_first, o->op_type);
1210 if (type == OP_ENTERSUB &&
1211 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1212 o->op_private |= OPpLVAL_DEFER;
1219 if (o->op_flags & OPf_KIDS)
1220 mod(cLISTOPo->op_last, type);
1224 if (!(o->op_flags & OPf_KIDS))
1226 if (o->op_targ != OP_LIST) {
1227 mod(cBINOPo->op_first, type);
1232 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1236 o->op_flags |= OPf_MOD;
1238 if (type == OP_AASSIGN || type == OP_SASSIGN)
1239 o->op_flags |= OPf_SPECIAL|OPf_REF;
1241 o->op_private |= OPpLVAL_INTRO;
1242 o->op_flags &= ~OPf_SPECIAL;
1244 else if (type != OP_GREPSTART && type != OP_ENTERSUB)
1245 o->op_flags |= OPf_REF;
1250 scalar_mod_type(OP *o, I32 type)
1254 if (o->op_type == OP_RV2GV)
1278 case OP_RIGHT_SHIFT:
1285 case OP_ANDASSIGN: /* may work later */
1286 case OP_ORASSIGN: /* may work later */
1294 refkids(OP *o, I32 type)
1297 if (o && o->op_flags & OPf_KIDS) {
1298 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1305 ref(OP *o, I32 type)
1309 if (!o || error_count)
1312 switch (o->op_type) {
1314 if ((type == OP_DEFINED || type == OP_LOCK) &&
1315 !(o->op_flags & OPf_STACKED)) {
1316 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1317 o->op_ppaddr = ppaddr[OP_RV2CV];
1318 assert(cUNOPo->op_first->op_type == OP_NULL);
1319 null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1320 o->op_flags |= OPf_SPECIAL;
1325 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1329 ref(cUNOPo->op_first, o->op_type);
1332 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1333 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1334 : type == OP_RV2HV ? OPpDEREF_HV
1336 o->op_flags |= OPf_MOD;
1341 o->op_flags |= OPf_MOD; /* XXX ??? */
1346 o->op_flags |= OPf_REF;
1349 ref(cUNOPo->op_first, o->op_type);
1354 o->op_flags |= OPf_REF;
1359 if (!(o->op_flags & OPf_KIDS))
1361 ref(cBINOPo->op_first, type);
1365 ref(cBINOPo->op_first, o->op_type);
1366 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1367 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1368 : type == OP_RV2HV ? OPpDEREF_HV
1370 o->op_flags |= OPf_MOD;
1378 if (!(o->op_flags & OPf_KIDS))
1380 ref(cLISTOPo->op_last, type);
1395 if (!o || error_count)
1399 if (type == OP_LIST) {
1400 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1402 } else if (type == OP_UNDEF) {
1404 } else if (type != OP_PADSV &&
1407 type != OP_PUSHMARK)
1409 yyerror(form("Can't declare %s in my", op_desc[o->op_type]));
1412 o->op_flags |= OPf_MOD;
1413 o->op_private |= OPpLVAL_INTRO;
1421 o->op_flags |= OPf_PARENS;
1426 bind_match(I32 type, OP *left, OP *right)
1431 (left->op_type == OP_RV2AV ||
1432 left->op_type == OP_RV2HV ||
1433 left->op_type == OP_PADAV ||
1434 left->op_type == OP_PADHV)) {
1435 char *desc = op_desc[(right->op_type == OP_SUBST ||
1436 right->op_type == OP_TRANS)
1437 ? right->op_type : OP_MATCH];
1438 char *sample = ((left->op_type == OP_RV2AV ||
1439 left->op_type == OP_PADAV)
1440 ? "@array" : "%hash");
1441 warn("Applying %s to %s will act on scalar(%s)", desc, sample, sample);
1444 if (right->op_type == OP_MATCH ||
1445 right->op_type == OP_SUBST ||
1446 right->op_type == OP_TRANS) {
1447 right->op_flags |= OPf_STACKED;
1448 if (right->op_type != OP_MATCH)
1449 left = mod(left, right->op_type);
1450 if (right->op_type == OP_TRANS)
1451 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
1453 o = prepend_elem(right->op_type, scalar(left), right);
1455 return newUNOP(OP_NOT, 0, scalar(o));
1459 return bind_match(type, left,
1460 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
1468 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
1469 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
1476 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || tainting) {
1477 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1478 o->op_type = OP_LEAVE;
1479 o->op_ppaddr = ppaddr[OP_LEAVE];
1482 if (o->op_type == OP_LINESEQ) {
1484 o->op_type = OP_SCOPE;
1485 o->op_ppaddr = ppaddr[OP_SCOPE];
1486 kid = ((LISTOP*)o)->op_first;
1487 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE){
1488 SvREFCNT_dec(((COP*)kid)->cop_filegv);
1493 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
1500 block_start(int full)
1503 int retval = savestack_ix;
1504 SAVEI32(comppad_name_floor);
1506 if ((comppad_name_fill = AvFILLp(comppad_name)) > 0)
1507 comppad_name_floor = comppad_name_fill;
1509 comppad_name_floor = 0;
1511 SAVEI32(min_intro_pending);
1512 SAVEI32(max_intro_pending);
1513 min_intro_pending = 0;
1514 SAVEI32(comppad_name_fill);
1515 SAVEI32(padix_floor);
1516 padix_floor = padix;
1517 pad_reset_pending = FALSE;
1519 hints &= ~HINT_BLOCK_SCOPE;
1524 block_end(I32 floor, OP *seq)
1527 int needblockscope = hints & HINT_BLOCK_SCOPE;
1528 OP* retval = scalarseq(seq);
1530 pad_reset_pending = FALSE;
1532 hints |= HINT_BLOCK_SCOPE; /* propagate out */
1533 pad_leavemy(comppad_name_fill);
1542 OP *o = newOP(OP_THREADSV, 0);
1543 o->op_targ = find_threadsv("_");
1546 return newSVREF(newGVOP(OP_GV, 0, defgv));
1547 #endif /* USE_THREADS */
1555 eval_root = newUNOP(OP_LEAVEEVAL, ((in_eval & 4) ? OPf_SPECIAL : 0), o);
1556 eval_start = linklist(eval_root);
1557 eval_root->op_next = 0;
1563 main_root = scope(sawparens(scalarvoid(o)));
1564 curcop = &compiling;
1565 main_start = LINKLIST(main_root);
1566 main_root->op_next = 0;
1570 /* Register with debugger */
1572 CV *cv = perl_get_cv("DB::postponed", FALSE);
1576 XPUSHs((SV*)compiling.cop_filegv);
1578 perl_call_sv((SV*)cv, G_DISCARD);
1585 localize(OP *o, I32 lex)
1587 if (o->op_flags & OPf_PARENS)
1590 if (dowarn && bufptr > oldbufptr && bufptr[-1] == ',') {
1592 for (s = bufptr; *s && (isALNUM(*s) || strchr("@$%, ",*s)); s++) ;
1593 if (*s == ';' || *s == '=')
1594 warn("Parens missing around \"%s\" list", lex ? "my" : "local");
1598 in_my_stash = Nullhv;
1602 return mod(o, OP_NULL); /* a bit kludgey */
1608 if (o->op_type == OP_LIST) {
1611 o2 = newOP(OP_THREADSV, 0);
1612 o2->op_targ = find_threadsv(";");
1614 o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
1615 #endif /* USE_THREADS */
1616 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
1622 fold_constants(register OP *o)
1626 I32 type = o->op_type;
1629 if (opargs[type] & OA_RETSCALAR)
1631 if (opargs[type] & OA_TARGET)
1632 o->op_targ = pad_alloc(type, SVs_PADTMP);
1634 if ((opargs[type] & OA_OTHERINT) && (hints & HINT_INTEGER))
1635 o->op_ppaddr = ppaddr[type = ++(o->op_type)];
1637 if (!(opargs[type] & OA_FOLDCONST))
1652 if (o->op_private & OPpLOCALE)
1657 goto nope; /* Don't try to run w/ errors */
1659 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
1660 if (curop->op_type != OP_CONST &&
1661 curop->op_type != OP_LIST &&
1662 curop->op_type != OP_SCALAR &&
1663 curop->op_type != OP_NULL &&
1664 curop->op_type != OP_PUSHMARK) {
1669 curop = LINKLIST(o);
1674 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
1675 pad_swipe(o->op_targ);
1676 else if (SvTEMP(sv)) { /* grab mortal temp? */
1677 (void)SvREFCNT_inc(sv);
1681 if (type == OP_RV2GV)
1682 return newGVOP(OP_GV, 0, (GV*)sv);
1684 if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK) {
1686 if ((double)iv == SvNV(sv)) { /* can we smush double to int */
1691 SvIOK_off(sv); /* undo SvIV() damage */
1693 return newSVOP(OP_CONST, 0, sv);
1697 if (!(opargs[type] & OA_OTHERINT))
1700 if (!(hints & HINT_INTEGER)) {
1701 if (type == OP_DIVIDE || !(o->op_flags & OPf_KIDS))
1704 for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
1705 if (curop->op_type == OP_CONST) {
1706 if (SvIOK(((SVOP*)curop)->op_sv))
1710 if (opargs[curop->op_type] & OA_RETINTEGER)
1714 o->op_ppaddr = ppaddr[++(o->op_type)];
1721 gen_constant_list(register OP *o)
1725 I32 oldtmps_floor = tmps_floor;
1729 return o; /* Don't attempt to run with errors */
1731 op = curop = LINKLIST(o);
1737 tmps_floor = oldtmps_floor;
1739 o->op_type = OP_RV2AV;
1740 o->op_ppaddr = ppaddr[OP_RV2AV];
1741 curop = ((UNOP*)o)->op_first;
1742 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*stack_sp--));
1749 convert(I32 type, I32 flags, OP *o)
1754 if (!o || o->op_type != OP_LIST)
1755 o = newLISTOP(OP_LIST, 0, o, Nullop);
1757 o->op_flags &= ~OPf_WANT;
1759 if (!(opargs[type] & OA_MARK))
1760 null(cLISTOPo->op_first);
1763 o->op_ppaddr = ppaddr[type];
1764 o->op_flags |= flags;
1766 o = CHECKOP(type, o);
1767 if (o->op_type != type)
1770 if (cLISTOPo->op_children < 7) {
1771 /* XXX do we really need to do this if we're done appending?? */
1772 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1774 cLISTOPo->op_last = last; /* in case check substituted last arg */
1777 return fold_constants(o);
1780 /* List constructors */
1783 append_elem(I32 type, OP *first, OP *last)
1791 if (first->op_type != type || type==OP_LIST && first->op_flags & OPf_PARENS)
1792 return newLISTOP(type, 0, first, last);
1794 if (first->op_flags & OPf_KIDS)
1795 ((LISTOP*)first)->op_last->op_sibling = last;
1797 first->op_flags |= OPf_KIDS;
1798 ((LISTOP*)first)->op_first = last;
1800 ((LISTOP*)first)->op_last = last;
1801 ((LISTOP*)first)->op_children++;
1806 append_list(I32 type, LISTOP *first, LISTOP *last)
1814 if (first->op_type != type)
1815 return prepend_elem(type, (OP*)first, (OP*)last);
1817 if (last->op_type != type)
1818 return append_elem(type, (OP*)first, (OP*)last);
1820 first->op_last->op_sibling = last->op_first;
1821 first->op_last = last->op_last;
1822 first->op_children += last->op_children;
1823 if (first->op_children)
1824 last->op_flags |= OPf_KIDS;
1831 prepend_elem(I32 type, OP *first, OP *last)
1839 if (last->op_type == type) {
1840 if (type == OP_LIST) { /* already a PUSHMARK there */
1841 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
1842 ((LISTOP*)last)->op_first->op_sibling = first;
1845 if (!(last->op_flags & OPf_KIDS)) {
1846 ((LISTOP*)last)->op_last = first;
1847 last->op_flags |= OPf_KIDS;
1849 first->op_sibling = ((LISTOP*)last)->op_first;
1850 ((LISTOP*)last)->op_first = first;
1852 ((LISTOP*)last)->op_children++;
1856 return newLISTOP(type, 0, first, last);
1864 return newOP(OP_STUB, 0);
1870 if (!o || o->op_type != OP_LIST)
1871 o = newLISTOP(OP_LIST, 0, o, Nullop);
1877 newLISTOP(I32 type, I32 flags, OP *first, OP *last)
1881 Newz(1101, listop, 1, LISTOP);
1883 listop->op_type = type;
1884 listop->op_ppaddr = ppaddr[type];
1885 listop->op_children = (first != 0) + (last != 0);
1886 listop->op_flags = flags;
1890 else if (!first && last)
1893 first->op_sibling = last;
1894 listop->op_first = first;
1895 listop->op_last = last;
1896 if (type == OP_LIST) {
1898 pushop = newOP(OP_PUSHMARK, 0);
1899 pushop->op_sibling = first;
1900 listop->op_first = pushop;
1901 listop->op_flags |= OPf_KIDS;
1903 listop->op_last = pushop;
1905 else if (listop->op_children)
1906 listop->op_flags |= OPf_KIDS;
1912 newOP(I32 type, I32 flags)
1915 Newz(1101, o, 1, OP);
1917 o->op_ppaddr = ppaddr[type];
1918 o->op_flags = flags;
1921 o->op_private = 0 + (flags >> 8);
1922 if (opargs[type] & OA_RETSCALAR)
1924 if (opargs[type] & OA_TARGET)
1925 o->op_targ = pad_alloc(type, SVs_PADTMP);
1926 return CHECKOP(type, o);
1930 newUNOP(I32 type, I32 flags, OP *first)
1935 first = newOP(OP_STUB, 0);
1936 if (opargs[type] & OA_MARK)
1937 first = force_list(first);
1939 Newz(1101, unop, 1, UNOP);
1940 unop->op_type = type;
1941 unop->op_ppaddr = ppaddr[type];
1942 unop->op_first = first;
1943 unop->op_flags = flags | OPf_KIDS;
1944 unop->op_private = 1 | (flags >> 8);
1946 if(type == OP_STUDY && first->op_type == OP_MATCH) {
1947 first->op_type = OP_PUSHRE;
1948 first->op_ppaddr = ppaddr[OP_PUSHRE];
1951 unop = (UNOP*) CHECKOP(type, unop);
1955 return fold_constants((OP *) unop);
1959 newBINOP(I32 type, I32 flags, OP *first, OP *last)
1962 Newz(1101, binop, 1, BINOP);
1965 first = newOP(OP_NULL, 0);
1967 binop->op_type = type;
1968 binop->op_ppaddr = ppaddr[type];
1969 binop->op_first = first;
1970 binop->op_flags = flags | OPf_KIDS;
1973 binop->op_private = 1 | (flags >> 8);
1976 binop->op_private = 2 | (flags >> 8);
1977 first->op_sibling = last;
1980 binop = (BINOP*)CHECKOP(type, binop);
1984 binop->op_last = last = binop->op_first->op_sibling;
1986 return fold_constants((OP *)binop);
1990 pmtrans(OP *o, OP *expr, OP *repl)
1992 SV *tstr = ((SVOP*)expr)->op_sv;
1993 SV *rstr = ((SVOP*)repl)->op_sv;
1996 register U8 *t = (U8*)SvPV(tstr, tlen);
1997 register U8 *r = (U8*)SvPV(rstr, rlen);
2003 register short *tbl;
2005 tbl = (short*)cPVOPo->op_pv;
2006 complement = o->op_private & OPpTRANS_COMPLEMENT;
2007 Delete = o->op_private & OPpTRANS_DELETE;
2008 squash = o->op_private & OPpTRANS_SQUASH;
2011 Zero(tbl, 256, short);
2012 for (i = 0; i < tlen; i++)
2014 for (i = 0, j = 0; i < 256; i++) {
2030 if (!rlen && !Delete) {
2033 o->op_private |= OPpTRANS_COUNTONLY;
2035 for (i = 0; i < 256; i++)
2037 for (i = 0, j = 0; i < tlen; i++,j++) {
2040 if (tbl[t[i]] == -1)
2046 if (tbl[t[i]] == -1)
2057 newPMOP(I32 type, I32 flags)
2062 Newz(1101, pmop, 1, PMOP);
2063 pmop->op_type = type;
2064 pmop->op_ppaddr = ppaddr[type];
2065 pmop->op_flags = flags;
2066 pmop->op_private = 0 | (flags >> 8);
2068 if (hints & HINT_LOCALE)
2069 pmop->op_pmpermflags = (pmop->op_pmflags |= PMf_LOCALE);
2071 /* link into pm list */
2072 if (type != OP_TRANS && curstash) {
2073 pmop->op_pmnext = HvPMROOT(curstash);
2074 HvPMROOT(curstash) = pmop;
2081 pmruntime(OP *o, OP *expr, OP *repl)
2086 if (o->op_type == OP_TRANS)
2087 return pmtrans(o, expr, repl);
2089 hints |= HINT_BLOCK_SCOPE;
2092 if (expr->op_type == OP_CONST) {
2094 SV *pat = ((SVOP*)expr)->op_sv;
2095 char *p = SvPV(pat, plen);
2096 if ((o->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
2097 sv_setpvn(pat, "\\s+", 3);
2098 p = SvPV(pat, plen);
2099 pm->op_pmflags |= PMf_SKIPWHITE;
2101 pm->op_pmregexp = pregcomp(p, p + plen, pm);
2102 if (strEQ("\\s+", pm->op_pmregexp->precomp))
2103 pm->op_pmflags |= PMf_WHITE;
2107 if (pm->op_pmflags & PMf_KEEP)
2108 expr = newUNOP(OP_REGCMAYBE,0,expr);
2110 Newz(1101, rcop, 1, LOGOP);
2111 rcop->op_type = OP_REGCOMP;
2112 rcop->op_ppaddr = ppaddr[OP_REGCOMP];
2113 rcop->op_first = scalar(expr);
2114 rcop->op_flags |= OPf_KIDS;
2115 rcop->op_private = 1;
2118 /* establish postfix order */
2119 if (pm->op_pmflags & PMf_KEEP) {
2121 rcop->op_next = expr;
2122 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2125 rcop->op_next = LINKLIST(expr);
2126 expr->op_next = (OP*)rcop;
2129 prepend_elem(o->op_type, scalar((OP*)rcop), o);
2134 if (pm->op_pmflags & PMf_EVAL)
2137 else if (repl->op_type == OP_THREADSV
2138 && strchr("&`'123456789+",
2139 threadsv_names[repl->op_targ]))
2143 #endif /* USE_THREADS */
2144 else if (repl->op_type == OP_CONST)
2148 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
2149 if (opargs[curop->op_type] & OA_DANGEROUS) {
2151 if (curop->op_type == OP_THREADSV
2152 && strchr("&`'123456789+", curop->op_private)) {
2156 if (curop->op_type == OP_GV) {
2157 GV *gv = ((GVOP*)curop)->op_gv;
2158 if (strchr("&`'123456789+", *GvENAME(gv)))
2161 #endif /* USE_THREADS */
2162 else if (curop->op_type == OP_RV2CV)
2164 else if (curop->op_type == OP_RV2SV ||
2165 curop->op_type == OP_RV2AV ||
2166 curop->op_type == OP_RV2HV ||
2167 curop->op_type == OP_RV2GV) {
2168 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2171 else if (curop->op_type == OP_PADSV ||
2172 curop->op_type == OP_PADAV ||
2173 curop->op_type == OP_PADHV ||
2174 curop->op_type == OP_PADANY) {
2183 if (curop == repl) {
2184 pm->op_pmflags |= PMf_CONST; /* const for long enough */
2185 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
2186 prepend_elem(o->op_type, scalar(repl), o);
2189 Newz(1101, rcop, 1, LOGOP);
2190 rcop->op_type = OP_SUBSTCONT;
2191 rcop->op_ppaddr = ppaddr[OP_SUBSTCONT];
2192 rcop->op_first = scalar(repl);
2193 rcop->op_flags |= OPf_KIDS;
2194 rcop->op_private = 1;
2197 /* establish postfix order */
2198 rcop->op_next = LINKLIST(repl);
2199 repl->op_next = (OP*)rcop;
2201 pm->op_pmreplroot = scalar((OP*)rcop);
2202 pm->op_pmreplstart = LINKLIST(rcop);
2211 newSVOP(I32 type, I32 flags, SV *sv)
2214 Newz(1101, svop, 1, SVOP);
2215 svop->op_type = type;
2216 svop->op_ppaddr = ppaddr[type];
2218 svop->op_next = (OP*)svop;
2219 svop->op_flags = flags;
2220 if (opargs[type] & OA_RETSCALAR)
2222 if (opargs[type] & OA_TARGET)
2223 svop->op_targ = pad_alloc(type, SVs_PADTMP);
2224 return CHECKOP(type, svop);
2228 newGVOP(I32 type, I32 flags, GV *gv)
2232 Newz(1101, gvop, 1, GVOP);
2233 gvop->op_type = type;
2234 gvop->op_ppaddr = ppaddr[type];
2235 gvop->op_gv = (GV*)SvREFCNT_inc(gv);
2236 gvop->op_next = (OP*)gvop;
2237 gvop->op_flags = flags;
2238 if (opargs[type] & OA_RETSCALAR)
2240 if (opargs[type] & OA_TARGET)
2241 gvop->op_targ = pad_alloc(type, SVs_PADTMP);
2242 return CHECKOP(type, gvop);
2246 newPVOP(I32 type, I32 flags, char *pv)
2249 Newz(1101, pvop, 1, PVOP);
2250 pvop->op_type = type;
2251 pvop->op_ppaddr = ppaddr[type];
2253 pvop->op_next = (OP*)pvop;
2254 pvop->op_flags = flags;
2255 if (opargs[type] & OA_RETSCALAR)
2257 if (opargs[type] & OA_TARGET)
2258 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
2259 return CHECKOP(type, pvop);
2268 save_hptr(&curstash);
2269 save_item(curstname);
2274 name = SvPV(sv, len);
2275 curstash = gv_stashpvn(name,len,TRUE);
2276 sv_setpvn(curstname, name, len);
2280 sv_setpv(curstname,"<none>");
2288 utilize(int aver, I32 floor, OP *version, OP *id, OP *arg)
2296 if (id->op_type != OP_CONST)
2297 croak("Module name must be constant");
2301 if(version != Nullop) {
2302 SV *vesv = ((SVOP*)version)->op_sv;
2304 if (arg == Nullop && !SvNIOK(vesv)) {
2311 if (version->op_type != OP_CONST || !SvNIOK(vesv))
2312 croak("Version number must be constant number");
2314 /* Make copy of id so we don't free it twice */
2315 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2317 /* Fake up a method call to VERSION */
2318 meth = newSVOP(OP_CONST, 0, newSVpv("VERSION", 7));
2319 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2320 append_elem(OP_LIST,
2321 prepend_elem(OP_LIST, pack, list(version)),
2322 newUNOP(OP_METHOD, 0, meth)));
2326 /* Fake up an import/unimport */
2327 if (arg && arg->op_type == OP_STUB)
2328 imop = arg; /* no import on explicit () */
2329 else if(SvNIOK(((SVOP*)id)->op_sv)) {
2330 imop = Nullop; /* use 5.0; */
2333 /* Make copy of id so we don't free it twice */
2334 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2335 meth = newSVOP(OP_CONST, 0,
2337 ? newSVpv("import", 6)
2338 : newSVpv("unimport", 8)
2340 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2341 append_elem(OP_LIST,
2342 prepend_elem(OP_LIST, pack, list(arg)),
2343 newUNOP(OP_METHOD, 0, meth)));
2346 /* Fake up a require */
2347 rqop = newUNOP(OP_REQUIRE, 0, id);
2349 /* Fake up the BEGIN {}, which does its thing immediately. */
2351 newSVOP(OP_CONST, 0, newSVpv("BEGIN", 5)),
2353 append_elem(OP_LINESEQ,
2354 append_elem(OP_LINESEQ,
2355 newSTATEOP(0, Nullch, rqop),
2356 newSTATEOP(0, Nullch, veop)),
2357 newSTATEOP(0, Nullch, imop) ));
2364 newSLICEOP(I32 flags, OP *subscript, OP *listval)
2366 return newBINOP(OP_LSLICE, flags,
2367 list(force_list(subscript)),
2368 list(force_list(listval)) );
2372 list_assignment(register OP *o)
2377 if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
2378 o = cUNOPo->op_first;
2380 if (o->op_type == OP_COND_EXPR) {
2381 I32 t = list_assignment(cCONDOPo->op_first->op_sibling);
2382 I32 f = list_assignment(cCONDOPo->op_first->op_sibling->op_sibling);
2387 yyerror("Assignment to both a list and a scalar");
2391 if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
2392 o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
2393 o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
2396 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
2399 if (o->op_type == OP_RV2SV)
2406 newASSIGNOP(I32 flags, OP *left, I32 optype, OP *right)
2411 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
2412 return newLOGOP(optype, 0,
2413 mod(scalar(left), optype),
2414 newUNOP(OP_SASSIGN, 0, scalar(right)));
2417 return newBINOP(optype, OPf_STACKED,
2418 mod(scalar(left), optype), scalar(right));
2422 if (list_assignment(left)) {
2424 eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
2425 left = mod(left, OP_AASSIGN);
2433 o = newBINOP(OP_AASSIGN, flags,
2434 list(force_list(right)),
2435 list(force_list(left)) );
2436 o->op_private = 0 | (flags >> 8);
2437 if (!(left->op_private & OPpLVAL_INTRO)) {
2438 static int generation = 100;
2442 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
2443 if (opargs[curop->op_type] & OA_DANGEROUS) {
2444 if (curop->op_type == OP_GV) {
2445 GV *gv = ((GVOP*)curop)->op_gv;
2446 if (gv == defgv || SvCUR(gv) == generation)
2448 SvCUR(gv) = generation;
2450 else if (curop->op_type == OP_PADSV ||
2451 curop->op_type == OP_PADAV ||
2452 curop->op_type == OP_PADHV ||
2453 curop->op_type == OP_PADANY) {
2454 SV **svp = AvARRAY(comppad_name);
2455 SV *sv = svp[curop->op_targ];
2456 if (SvCUR(sv) == generation)
2458 SvCUR(sv) = generation; /* (SvCUR not used any more) */
2460 else if (curop->op_type == OP_RV2CV)
2462 else if (curop->op_type == OP_RV2SV ||
2463 curop->op_type == OP_RV2AV ||
2464 curop->op_type == OP_RV2HV ||
2465 curop->op_type == OP_RV2GV) {
2466 if (lastop->op_type != OP_GV) /* funny deref? */
2475 o->op_private = OPpASSIGN_COMMON;
2477 if (right && right->op_type == OP_SPLIT) {
2479 if ((tmpop = ((LISTOP*)right)->op_first) &&
2480 tmpop->op_type == OP_PUSHRE)
2482 PMOP *pm = (PMOP*)tmpop;
2483 if (left->op_type == OP_RV2AV &&
2484 !(left->op_private & OPpLVAL_INTRO) &&
2485 !(o->op_private & OPpASSIGN_COMMON) )
2487 tmpop = ((UNOP*)left)->op_first;
2488 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
2489 pm->op_pmreplroot = (OP*)((GVOP*)tmpop)->op_gv;
2490 pm->op_pmflags |= PMf_ONCE;
2491 tmpop = cUNOPo->op_first; /* to list (nulled) */
2492 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
2493 tmpop->op_sibling = Nullop; /* don't free split */
2494 right->op_next = tmpop->op_next; /* fix starting loc */
2495 op_free(o); /* blow off assign */
2496 right->op_flags &= ~OPf_WANT;
2497 /* "I don't know and I don't care." */
2502 if (modcount < 10000 &&
2503 ((LISTOP*)right)->op_last->op_type == OP_CONST)
2505 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
2507 sv_setiv(sv, modcount+1);
2515 right = newOP(OP_UNDEF, 0);
2516 if (right->op_type == OP_READLINE) {
2517 right->op_flags |= OPf_STACKED;
2518 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
2521 eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
2522 o = newBINOP(OP_SASSIGN, flags,
2523 scalar(right), mod(scalar(left), OP_SASSIGN) );
2535 newSTATEOP(I32 flags, char *label, OP *o)
2538 U32 seq = intro_my();
2541 Newz(1101, cop, 1, COP);
2542 if (PERLDB_LINE && curcop->cop_line && curstash != debstash) {
2543 cop->op_type = OP_DBSTATE;
2544 cop->op_ppaddr = ppaddr[ OP_DBSTATE ];
2547 cop->op_type = OP_NEXTSTATE;
2548 cop->op_ppaddr = ppaddr[ OP_NEXTSTATE ];
2550 cop->op_flags = flags;
2551 cop->op_private = 0 | (flags >> 8);
2553 cop->op_private |= NATIVE_HINTS;
2555 cop->op_next = (OP*)cop;
2558 cop->cop_label = label;
2559 hints |= HINT_BLOCK_SCOPE;
2562 cop->cop_arybase = curcop->cop_arybase;
2564 if (copline == NOLINE)
2565 cop->cop_line = curcop->cop_line;
2567 cop->cop_line = copline;
2570 cop->cop_filegv = (GV*)SvREFCNT_inc(curcop->cop_filegv);
2571 cop->cop_stash = curstash;
2573 if (PERLDB_LINE && curstash != debstash) {
2574 SV **svp = av_fetch(GvAV(curcop->cop_filegv),(I32)cop->cop_line, FALSE);
2575 if (svp && *svp != &sv_undef && !SvIOK(*svp)) {
2576 (void)SvIOK_on(*svp);
2578 SvSTASH(*svp) = (HV*)cop;
2582 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
2585 /* "Introduce" my variables to visible status. */
2593 if (! min_intro_pending)
2596 svp = AvARRAY(comppad_name);
2597 for (i = min_intro_pending; i <= max_intro_pending; i++) {
2598 if ((sv = svp[i]) && sv != &sv_undef && !SvIVX(sv)) {
2599 SvIVX(sv) = 999999999; /* Don't know scope end yet. */
2600 SvNVX(sv) = (double)cop_seqmax;
2603 min_intro_pending = 0;
2604 comppad_name_fill = max_intro_pending; /* Needn't search higher */
2605 return cop_seqmax++;
2609 newLOGOP(I32 type, I32 flags, OP *first, OP *other)
2611 return new_logop(type, flags, &first, &other);
2615 new_logop(I32 type, I32 flags, OP** firstp, OP** otherp)
2620 OP *first = *firstp;
2621 OP *other = *otherp;
2623 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
2624 return newBINOP(type, flags, scalar(first), scalar(other));
2626 scalarboolean(first);
2627 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
2628 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
2629 if (type == OP_AND || type == OP_OR) {
2635 first = *firstp = cUNOPo->op_first;
2637 first->op_next = o->op_next;
2638 cUNOPo->op_first = Nullop;
2642 if (first->op_type == OP_CONST) {
2643 if (dowarn && (first->op_private & OPpCONST_BARE))
2644 warn("Probable precedence problem on %s", op_desc[type]);
2645 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
2656 else if (first->op_type == OP_WANTARRAY) {
2662 else if (dowarn && (first->op_flags & OPf_KIDS)) {
2663 OP *k1 = ((UNOP*)first)->op_first;
2664 OP *k2 = k1->op_sibling;
2666 switch (first->op_type)
2669 if (k2 && k2->op_type == OP_READLINE
2670 && (k2->op_flags & OPf_STACKED)
2671 && (k1->op_type == OP_RV2SV || k1->op_type == OP_PADSV))
2672 warnop = k2->op_type;
2676 if (k1->op_type == OP_READDIR
2677 || k1->op_type == OP_GLOB
2678 || k1->op_type == OP_EACH)
2679 warnop = k1->op_type;
2683 line_t oldline = curcop->cop_line;
2684 curcop->cop_line = copline;
2685 warn("Value of %s%s can be \"0\"; test with defined()",
2687 ((warnop == OP_READLINE || warnop == OP_GLOB)
2688 ? " construct" : "() operator"));
2689 curcop->cop_line = oldline;
2696 if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
2697 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
2699 Newz(1101, logop, 1, LOGOP);
2701 logop->op_type = type;
2702 logop->op_ppaddr = ppaddr[type];
2703 logop->op_first = first;
2704 logop->op_flags = flags | OPf_KIDS;
2705 logop->op_other = LINKLIST(other);
2706 logop->op_private = 1 | (flags >> 8);
2708 /* establish postfix order */
2709 logop->op_next = LINKLIST(first);
2710 first->op_next = (OP*)logop;
2711 first->op_sibling = other;
2713 o = newUNOP(OP_NULL, 0, (OP*)logop);
2720 newCONDOP(I32 flags, OP *first, OP *trueop, OP *falseop)
2727 return newLOGOP(OP_AND, 0, first, trueop);
2729 return newLOGOP(OP_OR, 0, first, falseop);
2731 scalarboolean(first);
2732 if (first->op_type == OP_CONST) {
2733 if (SvTRUE(((SVOP*)first)->op_sv)) {
2744 else if (first->op_type == OP_WANTARRAY) {
2748 Newz(1101, condop, 1, CONDOP);
2750 condop->op_type = OP_COND_EXPR;
2751 condop->op_ppaddr = ppaddr[OP_COND_EXPR];
2752 condop->op_first = first;
2753 condop->op_flags = flags | OPf_KIDS;
2754 condop->op_true = LINKLIST(trueop);
2755 condop->op_false = LINKLIST(falseop);
2756 condop->op_private = 1 | (flags >> 8);
2758 /* establish postfix order */
2759 condop->op_next = LINKLIST(first);
2760 first->op_next = (OP*)condop;
2762 first->op_sibling = trueop;
2763 trueop->op_sibling = falseop;
2764 o = newUNOP(OP_NULL, 0, (OP*)condop);
2766 trueop->op_next = o;
2767 falseop->op_next = o;
2773 newRANGE(I32 flags, OP *left, OP *right)
2781 Newz(1101, condop, 1, CONDOP);
2783 condop->op_type = OP_RANGE;
2784 condop->op_ppaddr = ppaddr[OP_RANGE];
2785 condop->op_first = left;
2786 condop->op_flags = OPf_KIDS;
2787 condop->op_true = LINKLIST(left);
2788 condop->op_false = LINKLIST(right);
2789 condop->op_private = 1 | (flags >> 8);
2791 left->op_sibling = right;
2793 condop->op_next = (OP*)condop;
2794 flip = newUNOP(OP_FLIP, flags, (OP*)condop);
2795 flop = newUNOP(OP_FLOP, 0, flip);
2796 o = newUNOP(OP_NULL, 0, flop);
2799 left->op_next = flip;
2800 right->op_next = flop;
2802 condop->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
2803 sv_upgrade(PAD_SV(condop->op_targ), SVt_PVNV);
2804 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
2805 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
2807 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
2808 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
2811 if (!flip->op_private || !flop->op_private)
2812 linklist(o); /* blow off optimizer unless constant */
2818 newLOOPOP(I32 flags, I32 debuggable, OP *expr, OP *block)
2823 int once = block && block->op_flags & OPf_SPECIAL &&
2824 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
2827 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
2828 return block; /* do {} while 0 does once */
2829 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
2830 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
2831 expr = newUNOP(OP_DEFINED, 0,
2832 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
2836 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
2837 o = new_logop(OP_AND, 0, &expr, &listop);
2840 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
2842 if (once && o != listop)
2843 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
2846 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
2848 o->op_flags |= flags;
2850 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
2855 newWHILEOP(I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *block, OP *cont)
2864 if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
2865 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB))) {
2866 expr = newUNOP(OP_DEFINED, 0,
2867 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
2871 block = newOP(OP_NULL, 0);
2874 next = LINKLIST(cont);
2876 cont = append_elem(OP_LINESEQ, cont, newOP(OP_UNSTACK, 0));
2877 if ((line_t)whileline != NOLINE) {
2878 copline = whileline;
2879 cont = append_elem(OP_LINESEQ, cont,
2880 newSTATEOP(0, Nullch, Nullop));
2884 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
2885 redo = LINKLIST(listop);
2888 copline = whileline;
2890 o = new_logop(OP_AND, 0, &expr, &listop);
2891 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
2892 op_free(expr); /* oops, it's a while (0) */
2894 return Nullop; /* listop already freed by new_logop */
2897 ((LISTOP*)listop)->op_last->op_next = condop =
2898 (o == listop ? redo : LINKLIST(o));
2906 Newz(1101,loop,1,LOOP);
2907 loop->op_type = OP_ENTERLOOP;
2908 loop->op_ppaddr = ppaddr[OP_ENTERLOOP];
2909 loop->op_private = 0;
2910 loop->op_next = (OP*)loop;
2913 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
2915 loop->op_redoop = redo;
2916 loop->op_lastop = o;
2919 loop->op_nextop = next;
2921 loop->op_nextop = o;
2923 o->op_flags |= flags;
2924 o->op_private |= (flags >> 8);
2929 newFOROP(I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
2937 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
2938 sv->op_type = OP_RV2GV;
2939 sv->op_ppaddr = ppaddr[OP_RV2GV];
2941 else if (sv->op_type == OP_PADSV) { /* private variable */
2942 padoff = sv->op_targ;
2946 else if (sv->op_type == OP_THREADSV) { /* per-thread variable */
2947 padoff = sv->op_targ;
2948 iterflags |= OPf_SPECIAL;
2953 croak("Can't use %s for loop variable", op_desc[sv->op_type]);
2957 padoff = find_threadsv("_");
2958 iterflags |= OPf_SPECIAL;
2960 sv = newGVOP(OP_GV, 0, defgv);
2963 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
2964 expr = scalar(ref(expr, OP_ITER));
2965 iterflags |= OPf_STACKED;
2967 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
2968 append_elem(OP_LIST, mod(force_list(expr), OP_GREPSTART),
2970 assert(!loop->op_next);
2971 Renew(loop, 1, LOOP);
2972 loop->op_targ = padoff;
2973 wop = newWHILEOP(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont);
2975 return newSTATEOP(0, label, wop);
2979 newLOOPEX(I32 type, OP *label)
2983 if (type != OP_GOTO || label->op_type == OP_CONST) {
2984 /* "last()" means "last" */
2985 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS))
2986 o = newOP(type, OPf_SPECIAL);
2988 o = newPVOP(type, 0, savepv(label->op_type == OP_CONST
2989 ? SvPVx(((SVOP*)label)->op_sv, na)
2995 if (label->op_type == OP_ENTERSUB)
2996 label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
2997 o = newUNOP(type, OPf_STACKED, label);
2999 hints |= HINT_BLOCK_SCOPE;
3009 MUTEX_DESTROY(CvMUTEXP(cv));
3010 Safefree(CvMUTEXP(cv));
3013 #endif /* USE_THREADS */
3015 if (!CvXSUB(cv) && CvROOT(cv)) {
3017 if (CvDEPTH(cv) || (CvOWNER(cv) && CvOWNER(cv) != thr))
3018 croak("Can't undef active subroutine");
3021 croak("Can't undef active subroutine");
3022 #endif /* USE_THREADS */
3029 op_free(CvROOT(cv));
3030 CvROOT(cv) = Nullop;
3033 SvPOK_off((SV*)cv); /* forget prototype */
3035 SvREFCNT_dec(CvGV(cv));
3037 SvREFCNT_dec(CvOUTSIDE(cv));
3038 CvOUTSIDE(cv) = Nullcv;
3039 if (CvPADLIST(cv)) {
3040 /* may be during global destruction */
3041 if (SvREFCNT(CvPADLIST(cv))) {
3042 I32 i = AvFILLp(CvPADLIST(cv));
3044 SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE);
3045 SV* sv = svp ? *svp : Nullsv;
3048 if (sv == (SV*)comppad_name)
3049 comppad_name = Nullav;
3050 else if (sv == (SV*)comppad) {
3052 curpad = Null(SV**);
3056 SvREFCNT_dec((SV*)CvPADLIST(cv));
3058 CvPADLIST(cv) = Nullav;
3062 #ifdef DEBUG_CLOSURES
3067 CV *outside = CvOUTSIDE(cv);
3068 AV* padlist = CvPADLIST(cv);
3075 PerlIO_printf(Perl_debug_log, "\tCV=0x%lx (%s), OUTSIDE=0x%lx (%s)\n",
3077 (CvANON(cv) ? "ANON"
3078 : (cv == main_cv) ? "MAIN"
3079 : CvUNIQUE(outside) ? "UNIQUE"
3080 : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"),
3083 : CvANON(outside) ? "ANON"
3084 : (outside == main_cv) ? "MAIN"
3085 : CvUNIQUE(outside) ? "UNIQUE"
3086 : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED"));
3091 pad_name = (AV*)*av_fetch(padlist, 0, FALSE);
3092 pad = (AV*)*av_fetch(padlist, 1, FALSE);
3093 pname = AvARRAY(pad_name);
3094 ppad = AvARRAY(pad);
3096 for (ix = 1; ix <= AvFILLp(pad_name); ix++) {
3097 if (SvPOK(pname[ix]))
3098 PerlIO_printf(Perl_debug_log, "\t%4d. 0x%lx (%s\"%s\" %ld-%ld)\n",
3100 SvFAKE(pname[ix]) ? "FAKE " : "",
3102 (long)I_32(SvNVX(pname[ix])),
3103 (long)SvIVX(pname[ix]));
3106 #endif /* DEBUG_CLOSURES */
3109 cv_clone2(CV *proto, CV *outside)
3114 AV* protopadlist = CvPADLIST(proto);
3115 AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE);
3116 AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE);
3117 SV** pname = AvARRAY(protopad_name);
3118 SV** ppad = AvARRAY(protopad);
3119 I32 fname = AvFILLp(protopad_name);
3120 I32 fpad = AvFILLp(protopad);
3124 assert(!CvUNIQUE(proto));
3129 SAVESPTR(comppad_name);
3132 cv = compcv = (CV*)NEWSV(1104,0);
3133 sv_upgrade((SV *)cv, SvTYPE(proto));
3139 New(666, CvMUTEXP(cv), 1, perl_mutex);
3140 MUTEX_INIT(CvMUTEXP(cv));
3142 #endif /* USE_THREADS */
3143 CvFILEGV(cv) = CvFILEGV(proto);
3144 CvGV(cv) = (GV*)SvREFCNT_inc(CvGV(proto));
3145 CvSTASH(cv) = CvSTASH(proto);
3146 CvROOT(cv) = CvROOT(proto);
3147 CvSTART(cv) = CvSTART(proto);
3149 CvOUTSIDE(cv) = (CV*)SvREFCNT_inc(outside);
3152 sv_setpvn((SV*)cv, SvPVX(proto), SvCUR(proto));
3154 comppad_name = newAV();
3155 for (ix = fname; ix >= 0; ix--)
3156 av_store(comppad_name, ix, SvREFCNT_inc(pname[ix]));
3160 comppadlist = newAV();
3161 AvREAL_off(comppadlist);
3162 av_store(comppadlist, 0, (SV*)comppad_name);
3163 av_store(comppadlist, 1, (SV*)comppad);
3164 CvPADLIST(cv) = comppadlist;
3165 av_fill(comppad, AvFILLp(protopad));
3166 curpad = AvARRAY(comppad);
3168 av = newAV(); /* will be @_ */
3170 av_store(comppad, 0, (SV*)av);
3171 AvFLAGS(av) = AVf_REIFY;
3173 for (ix = fpad; ix > 0; ix--) {
3174 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
3175 if (namesv && namesv != &sv_undef) {
3176 char *name = SvPVX(namesv); /* XXX */
3177 if (SvFLAGS(namesv) & SVf_FAKE) { /* lexical from outside? */
3178 I32 off = pad_findlex(name, ix, SvIVX(namesv),
3179 CvOUTSIDE(cv), cxstack_ix);
3181 curpad[ix] = SvREFCNT_inc(ppad[ix]);
3183 croak("panic: cv_clone: %s", name);
3185 else { /* our own lexical */
3188 /* anon code -- we'll come back for it */
3189 sv = SvREFCNT_inc(ppad[ix]);
3191 else if (*name == '@')
3193 else if (*name == '%')
3203 SV* sv = NEWSV(0,0);
3209 /* Now that vars are all in place, clone nested closures. */
3211 for (ix = fpad; ix > 0; ix--) {
3212 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
3214 && namesv != &sv_undef
3215 && !(SvFLAGS(namesv) & SVf_FAKE)
3216 && *SvPVX(namesv) == '&'
3217 && CvCLONE(ppad[ix]))
3219 CV *kid = cv_clone2((CV*)ppad[ix], cv);
3220 SvREFCNT_dec(ppad[ix]);
3223 curpad[ix] = (SV*)kid;
3227 #ifdef DEBUG_CLOSURES
3228 PerlIO_printf(Perl_debug_log, "Cloned inside:\n");
3230 PerlIO_printf(Perl_debug_log, " from:\n");
3232 PerlIO_printf(Perl_debug_log, " to:\n");
3243 return cv_clone2(proto, CvOUTSIDE(proto));
3247 cv_ckproto(CV *cv, GV *gv, char *p)
3249 if ((!p != !SvPOK(cv)) || (p && strNE(p, SvPVX(cv)))) {
3250 SV* msg = sv_newmortal();
3254 gv_efullname3(name = sv_newmortal(), gv, Nullch);
3255 sv_setpv(msg, "Prototype mismatch:");
3257 sv_catpvf(msg, " sub %_", name);
3259 sv_catpvf(msg, " (%s)", SvPVX(cv));
3260 sv_catpv(msg, " vs ");
3262 sv_catpvf(msg, "(%s)", p);
3264 sv_catpv(msg, "none");
3275 if (!cv || !SvPOK(cv) || SvCUR(cv))
3279 for (o = CvSTART(cv); o; o = o->op_next) {
3280 OPCODE type = o->op_type;
3282 if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
3284 if (type == OP_LEAVESUB || type == OP_RETURN)
3288 if (type == OP_CONST)
3290 else if (type == OP_PADSV) {
3291 AV* padav = (AV*)(AvARRAY(CvPADLIST(cv))[1]);
3292 sv = padav ? AvARRAY(padav)[o->op_targ] : Nullsv;
3293 if (!sv || (!SvREADONLY(sv) && SvREFCNT(sv) > 1))
3305 newSUB(I32 floor, OP *o, OP *proto, OP *block)
3308 char *name = o ? SvPVx(cSVOPo->op_sv, na) : Nullch;
3309 GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV);
3310 char *ps = proto ? SvPVx(((SVOP*)proto)->op_sv, na) : Nullch;
3319 if (!name || GvCVGEN(gv))
3321 else if (cv = GvCV(gv)) {
3322 cv_ckproto(cv, gv, ps);
3323 /* already defined (or promised)? */
3324 if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
3327 /* just a "sub foo;" when &foo is already defined */
3331 /* ahem, death to those who redefine active sort subs */
3332 if (curstack == sortstack && sortcop == CvSTART(cv))
3333 croak("Can't redefine active sort subroutine %s", name);
3334 const_sv = cv_const_sv(cv);
3335 if (const_sv || dowarn && !(CvGV(cv) && GvSTASH(CvGV(cv))
3336 && HvNAME(GvSTASH(CvGV(cv)))
3337 && strEQ(HvNAME(GvSTASH(CvGV(cv))),
3339 line_t oldline = curcop->cop_line;
3340 curcop->cop_line = copline;
3341 warn(const_sv ? "Constant subroutine %s redefined"
3342 : "Subroutine %s redefined", name);
3343 curcop->cop_line = oldline;
3349 if (cv) { /* must reuse cv if autoloaded */
3351 CvFLAGS(cv) = CvFLAGS(compcv);
3352 CvOUTSIDE(cv) = CvOUTSIDE(compcv);
3353 CvOUTSIDE(compcv) = 0;
3354 CvPADLIST(cv) = CvPADLIST(compcv);
3355 CvPADLIST(compcv) = 0;
3356 if (SvREFCNT(compcv) > 1) /* XXX Make closures transit through stub. */
3357 CvOUTSIDE(compcv) = (CV*)SvREFCNT_inc((SV*)cv);
3358 SvREFCNT_dec(compcv);
3368 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
3369 CvFILEGV(cv) = curcop->cop_filegv;
3370 CvSTASH(cv) = curstash;
3373 New(666, CvMUTEXP(cv), 1, perl_mutex);
3374 MUTEX_INIT(CvMUTEXP(cv));
3375 #endif /* USE_THREADS */
3378 sv_setpv((SV*)cv, ps);
3384 char *s = strrchr(name, ':');
3386 if (strEQ(s, "BEGIN")) {
3388 "BEGIN not safe after errors--compilation aborted";
3392 /* force display of errors found but not reported */
3393 sv_catpv(ERRSV, not_safe);
3394 croak("%s", SvPVx(ERRSV, na));
3405 if (AvFILLp(comppad_name) < AvFILLp(comppad))
3406 av_store(comppad_name, AvFILLp(comppad), Nullsv);
3409 SV **namep = AvARRAY(comppad_name);
3410 for (ix = AvFILLp(comppad); ix > 0; ix--) {
3413 if (SvIMMORTAL(curpad[ix]))
3416 * The only things that a clonable function needs in its
3417 * pad are references to outer lexicals and anonymous subs.
3418 * The rest are created anew during cloning.
3420 if (!((namesv = namep[ix]) != Nullsv &&
3421 namesv != &sv_undef &&
3423 *SvPVX(namesv) == '&')))
3425 SvREFCNT_dec(curpad[ix]);
3426 curpad[ix] = Nullsv;
3431 AV *av = newAV(); /* Will be @_ */
3433 av_store(comppad, 0, (SV*)av);
3434 AvFLAGS(av) = AVf_REIFY;
3436 for (ix = AvFILLp(comppad); ix > 0; ix--) {
3437 if (SvIMMORTAL(curpad[ix]))
3439 if (!SvPADMY(curpad[ix]))
3440 SvPADTMP_on(curpad[ix]);
3444 CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
3445 CvSTART(cv) = LINKLIST(CvROOT(cv));
3446 CvROOT(cv)->op_next = 0;
3452 if (PERLDB_SUBLINE && curstash != debstash) {
3453 SV *sv = NEWSV(0,0);
3454 SV *tmpstr = sv_newmortal();
3455 GV *db_postponed = gv_fetchpv("DB::postponed", GV_ADDMULTI, SVt_PVHV);
3459 sv_setpvf(sv, "%_:%ld-%ld",
3460 GvSV(curcop->cop_filegv),
3461 (long)subline, (long)curcop->cop_line);
3462 gv_efullname3(tmpstr, gv, Nullch);
3463 hv_store(GvHV(DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0);
3464 hv = GvHVn(db_postponed);
3465 if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX(tmpstr), SvCUR(tmpstr))
3466 && (cv = GvCV(db_postponed))) {
3471 perl_call_sv((SV*)cv, G_DISCARD);
3475 if ((s = strrchr(name,':')))
3479 if (strEQ(s, "BEGIN")) {
3480 I32 oldscope = scopestack_ix;
3482 SAVESPTR(compiling.cop_filegv);
3483 SAVEI16(compiling.cop_line);
3490 DEBUG_x( dump_sub(gv) );
3491 av_push(beginav, (SV *)cv);
3493 call_list(oldscope, beginav);
3495 curcop = &compiling;
3498 else if (strEQ(s, "END") && !error_count) {
3501 av_unshift(endav, 1);
3502 av_store(endav, 0, (SV *)cv);
3505 else if (strEQ(s, "INIT") && !error_count) {
3508 av_push(initav, SvREFCNT_inc(cv));
3519 newXS(char *name, void (*subaddr) (CV *), char *filename)
3522 GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV);
3525 if (cv = (name ? GvCV(gv) : Nullcv)) {
3527 /* just a cached method */
3531 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
3532 /* already defined (or promised) */
3533 if (dowarn && !(CvGV(cv) && GvSTASH(CvGV(cv))
3534 && HvNAME(GvSTASH(CvGV(cv)))
3535 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "autouse"))) {
3536 line_t oldline = curcop->cop_line;
3537 curcop->cop_line = copline;
3538 warn("Subroutine %s redefined",name);
3539 curcop->cop_line = oldline;
3546 if (cv) /* must reuse cv if autoloaded */
3549 cv = (CV*)NEWSV(1105,0);
3550 sv_upgrade((SV *)cv, SVt_PVCV);
3557 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
3559 New(666, CvMUTEXP(cv), 1, perl_mutex);
3560 MUTEX_INIT(CvMUTEXP(cv));
3562 #endif /* USE_THREADS */
3563 CvFILEGV(cv) = gv_fetchfile(filename);
3564 CvXSUB(cv) = subaddr;
3567 char *s = strrchr(name,':');
3572 if (strEQ(s, "BEGIN")) {
3575 av_push(beginav, (SV *)cv);
3578 else if (strEQ(s, "END")) {
3581 av_unshift(endav, 1);
3582 av_store(endav, 0, (SV *)cv);
3585 else if (strEQ(s, "INIT")) {
3588 av_push(initav, (SV *)cv);
3598 newFORM(I32 floor, OP *o, OP *block)
3607 name = SvPVx(cSVOPo->op_sv, na);
3610 gv = gv_fetchpv(name,TRUE, SVt_PVFM);
3612 if (cv = GvFORM(gv)) {
3614 line_t oldline = curcop->cop_line;
3616 curcop->cop_line = copline;
3617 warn("Format %s redefined",name);
3618 curcop->cop_line = oldline;
3624 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
3625 CvFILEGV(cv) = curcop->cop_filegv;
3627 for (ix = AvFILLp(comppad); ix > 0; ix--) {
3628 if (!SvPADMY(curpad[ix]) && !SvIMMORTAL(curpad[ix]))
3629 SvPADTMP_on(curpad[ix]);
3632 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
3633 CvSTART(cv) = LINKLIST(CvROOT(cv));
3634 CvROOT(cv)->op_next = 0;
3644 return newUNOP(OP_REFGEN, 0,
3645 mod(list(convert(OP_ANONLIST, 0, o)), OP_REFGEN));
3651 return newUNOP(OP_REFGEN, 0,
3652 mod(list(convert(OP_ANONHASH, 0, o)), OP_REFGEN));
3656 newANONSUB(I32 floor, OP *proto, OP *block)
3658 return newUNOP(OP_REFGEN, 0,
3659 newSVOP(OP_ANONCODE, 0, (SV*)newSUB(floor, 0, proto, block)));
3665 switch (o->op_type) {
3667 o->op_type = OP_PADAV;
3668 o->op_ppaddr = ppaddr[OP_PADAV];
3669 return ref(newUNOP(OP_RV2AV, 0, scalar(o)), OP_RV2AV);
3672 o->op_type = OP_RV2AV;
3673 o->op_ppaddr = ppaddr[OP_RV2AV];
3678 warn("oops: oopsAV");
3687 switch (o->op_type) {
3690 o->op_type = OP_PADHV;
3691 o->op_ppaddr = ppaddr[OP_PADHV];
3692 return ref(newUNOP(OP_RV2HV, 0, scalar(o)), OP_RV2HV);
3696 o->op_type = OP_RV2HV;
3697 o->op_ppaddr = ppaddr[OP_RV2HV];
3702 warn("oops: oopsHV");
3711 if (o->op_type == OP_PADANY) {
3712 o->op_type = OP_PADAV;
3713 o->op_ppaddr = ppaddr[OP_PADAV];
3716 return newUNOP(OP_RV2AV, 0, scalar(o));
3720 newGVREF(I32 type, OP *o)
3722 if (type == OP_MAPSTART)
3723 return newUNOP(OP_NULL, 0, o);
3724 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
3730 if (o->op_type == OP_PADANY) {
3731 o->op_type = OP_PADHV;
3732 o->op_ppaddr = ppaddr[OP_PADHV];
3735 return newUNOP(OP_RV2HV, 0, scalar(o));
3741 croak("NOT IMPL LINE %d",__LINE__);
3747 newCVREF(I32 flags, OP *o)
3749 return newUNOP(OP_RV2CV, flags, scalar(o));
3755 if (o->op_type == OP_PADANY) {
3756 o->op_type = OP_PADSV;
3757 o->op_ppaddr = ppaddr[OP_PADSV];
3760 else if (o->op_type == OP_THREADSV && !(o->op_flags & OPpDONE_SVREF)) {
3761 o->op_flags |= OPpDONE_SVREF;
3764 return newUNOP(OP_RV2SV, 0, scalar(o));
3767 /* Check routines. */
3775 name = NEWSV(1106,0);
3776 sv_upgrade(name, SVt_PVNV);
3777 sv_setpvn(name, "&", 1);
3780 ix = pad_alloc(o->op_type, SVs_PADMY);
3781 av_store(comppad_name, ix, name);
3782 av_store(comppad, ix, cSVOPo->op_sv);
3783 SvPADMY_on(cSVOPo->op_sv);
3784 cSVOPo->op_sv = Nullsv;
3785 cSVOPo->op_targ = ix;
3792 o->op_private = hints;
3799 if (cUNOPo->op_first->op_type == OP_CONCAT)
3800 o->op_flags |= OPf_STACKED;
3807 if (o->op_flags & OPf_KIDS) {
3810 OPCODE type = o->op_type;
3811 o = modkids(ck_fun(o), type);
3812 kid = cUNOPo->op_first;
3813 newop = kUNOP->op_first->op_sibling;
3815 (newop->op_sibling ||
3816 !(opargs[newop->op_type] & OA_RETSCALAR) ||
3817 newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
3818 newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
3822 op_free(kUNOP->op_first);
3823 kUNOP->op_first = newop;
3825 o->op_ppaddr = ppaddr[++o->op_type];
3834 if (o->op_flags & OPf_KIDS) {
3835 OP *kid = cUNOPo->op_first;
3836 if (kid->op_type == OP_HSLICE)
3837 o->op_private |= OPpSLICE;
3838 else if (kid->op_type != OP_HELEM)
3839 croak("%s argument is not a HASH element or slice",
3840 op_desc[o->op_type]);
3849 I32 type = o->op_type;
3851 if (o->op_flags & OPf_KIDS) {
3852 if (cLISTOPo->op_first->op_type == OP_STUB) {
3854 o = newUNOP(type, OPf_SPECIAL,
3855 newGVOP(OP_GV, 0, gv_fetchpv("main::ARGV", TRUE, SVt_PVAV)));
3865 hints |= HINT_BLOCK_SCOPE;
3866 if (o->op_flags & OPf_KIDS) {
3867 SVOP *kid = (SVOP*)cUNOPo->op_first;
3870 o->op_flags &= ~OPf_KIDS;
3873 else if (kid->op_type == OP_LINESEQ) {
3876 kid->op_next = o->op_next;
3877 cUNOPo->op_first = 0;
3880 Newz(1101, enter, 1, LOGOP);
3881 enter->op_type = OP_ENTERTRY;
3882 enter->op_ppaddr = ppaddr[OP_ENTERTRY];
3883 enter->op_private = 0;
3885 /* establish postfix order */
3886 enter->op_next = (OP*)enter;
3888 o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
3889 o->op_type = OP_LEAVETRY;
3890 o->op_ppaddr = ppaddr[OP_LEAVETRY];
3891 enter->op_other = o;
3899 o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP());
3901 o->op_targ = (PADOFFSET)hints;
3909 if (o->op_flags & OPf_STACKED) {
3911 kid = cUNOPo->op_first->op_sibling;
3912 if (kid->op_type == OP_RV2GV)
3924 if (o->op_flags & OPf_KIDS) {
3925 OP *kid = cUNOPo->op_first;
3926 if (kid->op_type != OP_HELEM)
3927 croak("%s argument is not a HASH element", op_desc[o->op_type]);
3934 ck_gvconst(register OP *o)
3936 o = fold_constants(o);
3937 if (o->op_type == OP_CONST)
3943 ck_rvconst(register OP *o)
3946 SVOP *kid = (SVOP*)cUNOPo->op_first;
3948 o->op_private |= (hints & HINT_STRICT_REFS);
3949 if (kid->op_type == OP_CONST) {
3954 name = SvPV(kid->op_sv, na);
3955 if ((hints & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
3956 char *badthing = Nullch;
3957 switch (o->op_type) {
3959 badthing = "a SCALAR";
3962 badthing = "an ARRAY";
3965 badthing = "a HASH";
3970 "Can't use bareword (\"%s\") as %s ref while \"strict refs\" in use",
3974 * This is a little tricky. We only want to add the symbol if we
3975 * didn't add it in the lexer. Otherwise we get duplicate strict
3976 * warnings. But if we didn't add it in the lexer, we must at
3977 * least pretend like we wanted to add it even if it existed before,
3978 * or we get possible typo warnings. OPpCONST_ENTERED says
3979 * whether the lexer already added THIS instance of this symbol.
3981 iscv = (o->op_type == OP_RV2CV) * 2;
3983 gv = gv_fetchpv(name,
3984 iscv | !(kid->op_private & OPpCONST_ENTERED),
3987 : o->op_type == OP_RV2SV
3989 : o->op_type == OP_RV2AV
3991 : o->op_type == OP_RV2HV
3994 } while (!gv && !(kid->op_private & OPpCONST_ENTERED) && !iscv++);
3996 kid->op_type = OP_GV;
3997 SvREFCNT_dec(kid->op_sv);
3998 kid->op_sv = SvREFCNT_inc(gv);
4008 I32 type = o->op_type;
4010 if (o->op_flags & OPf_REF)
4013 if (o->op_flags & OPf_KIDS) {
4014 SVOP *kid = (SVOP*)cUNOPo->op_first;
4016 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
4017 OP *newop = newGVOP(type, OPf_REF,
4018 gv_fetchpv(SvPVx(kid->op_sv, na), TRUE, SVt_PVIO));
4025 if (type == OP_FTTTY)
4026 return newGVOP(type, OPf_REF, gv_fetchpv("main::STDIN", TRUE,
4029 return newUNOP(type, 0, newDEFSVOP());
4042 int type = o->op_type;
4043 register I32 oa = opargs[type] >> OASHIFT;
4045 if (o->op_flags & OPf_STACKED) {
4046 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
4049 return no_fh_allowed(o);
4052 if (o->op_flags & OPf_KIDS) {
4053 tokid = &cLISTOPo->op_first;
4054 kid = cLISTOPo->op_first;
4055 if (kid->op_type == OP_PUSHMARK ||
4056 kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK)
4058 tokid = &kid->op_sibling;
4059 kid = kid->op_sibling;
4061 if (!kid && opargs[type] & OA_DEFGV)
4062 *tokid = kid = newDEFSVOP();
4066 sibl = kid->op_sibling;
4080 if (kid->op_type == OP_CONST &&
4081 (kid->op_private & OPpCONST_BARE)) {
4082 char *name = SvPVx(((SVOP*)kid)->op_sv, na);
4083 OP *newop = newAVREF(newGVOP(OP_GV, 0,
4084 gv_fetchpv(name, TRUE, SVt_PVAV) ));
4086 warn("Array @%s missing the @ in argument %ld of %s()",
4087 name, (long)numargs, op_desc[type]);
4090 kid->op_sibling = sibl;
4093 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
4094 bad_type(numargs, "array", op_desc[o->op_type], kid);
4098 if (kid->op_type == OP_CONST &&
4099 (kid->op_private & OPpCONST_BARE)) {
4100 char *name = SvPVx(((SVOP*)kid)->op_sv, na);
4101 OP *newop = newHVREF(newGVOP(OP_GV, 0,
4102 gv_fetchpv(name, TRUE, SVt_PVHV) ));
4104 warn("Hash %%%s missing the %% in argument %ld of %s()",
4105 name, (long)numargs, op_desc[type]);
4108 kid->op_sibling = sibl;
4111 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
4112 bad_type(numargs, "hash", op_desc[o->op_type], kid);
4117 OP *newop = newUNOP(OP_NULL, 0, kid);
4118 kid->op_sibling = 0;
4120 newop->op_next = newop;
4122 kid->op_sibling = sibl;
4127 if (kid->op_type != OP_GV) {
4128 if (kid->op_type == OP_CONST &&
4129 (kid->op_private & OPpCONST_BARE)) {
4130 OP *newop = newGVOP(OP_GV, 0,
4131 gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, na), TRUE,
4137 kid->op_sibling = 0;
4138 kid = newUNOP(OP_RV2GV, 0, scalar(kid));
4140 kid->op_sibling = sibl;
4146 mod(scalar(kid), type);
4150 tokid = &kid->op_sibling;
4151 kid = kid->op_sibling;
4153 o->op_private |= numargs;
4155 return too_many_arguments(o,op_desc[o->op_type]);
4158 else if (opargs[type] & OA_DEFGV) {
4160 return newUNOP(type, 0, newDEFSVOP());
4164 while (oa & OA_OPTIONAL)
4166 if (oa && oa != OA_LIST)
4167 return too_few_arguments(o,op_desc[o->op_type]);
4177 if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
4178 append_elem(OP_GLOB, o, newDEFSVOP());
4180 if (!((gv = gv_fetchpv("glob", FALSE, SVt_PVCV)) && GvIMPORTED_CV(gv)))
4181 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
4183 if (gv && GvIMPORTED_CV(gv)) {
4184 static int glob_index;
4186 append_elem(OP_GLOB, o,
4187 newSVOP(OP_CONST, 0, newSViv(glob_index++)));
4188 o->op_type = OP_LIST;
4189 o->op_ppaddr = ppaddr[OP_LIST];
4190 cLISTOPo->op_first->op_type = OP_PUSHMARK;
4191 cLISTOPo->op_first->op_ppaddr = ppaddr[OP_PUSHMARK];
4192 o = newUNOP(OP_ENTERSUB, OPf_STACKED,
4193 append_elem(OP_LIST, o,
4194 scalar(newUNOP(OP_RV2CV, 0,
4195 newGVOP(OP_GV, 0, gv)))));
4196 o = newUNOP(OP_NULL, 0, ck_subr(o));
4197 o->op_targ = OP_GLOB; /* hint at what it used to be */
4200 gv = newGVgen("main");
4202 append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
4212 OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
4214 o->op_ppaddr = ppaddr[OP_GREPSTART];
4215 Newz(1101, gwop, 1, LOGOP);
4217 if (o->op_flags & OPf_STACKED) {
4220 kid = cLISTOPo->op_first->op_sibling;
4221 for (k = cLISTOPo->op_first->op_sibling->op_next; k; k = k->op_next) {
4224 kid->op_next = (OP*)gwop;
4225 o->op_flags &= ~OPf_STACKED;
4227 kid = cLISTOPo->op_first->op_sibling;
4228 if (type == OP_MAPWHILE)
4235 kid = cLISTOPo->op_first->op_sibling;
4236 if (kid->op_type != OP_NULL)
4237 croak("panic: ck_grep");
4238 kid = kUNOP->op_first;
4240 gwop->op_type = type;
4241 gwop->op_ppaddr = ppaddr[type];
4242 gwop->op_first = listkids(o);
4243 gwop->op_flags |= OPf_KIDS;
4244 gwop->op_private = 1;
4245 gwop->op_other = LINKLIST(kid);
4246 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
4247 kid->op_next = (OP*)gwop;
4249 kid = cLISTOPo->op_first->op_sibling;
4250 if (!kid || !kid->op_sibling)
4251 return too_few_arguments(o,op_desc[o->op_type]);
4252 for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
4253 mod(kid, OP_GREPSTART);
4261 if (o->op_flags & OPf_KIDS) {
4262 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
4263 if (kid && kid->op_type == OP_CONST)
4264 fbm_compile(((SVOP*)kid)->op_sv);
4270 ck_lengthconst(OP *o)
4272 /* XXX length optimization goes here */
4279 OPCODE type = o->op_type;
4280 return modkids(ck_fun(o), type);
4286 OPCODE type = o->op_type;
4287 return refkids(ck_fun(o), type);
4295 kid = cLISTOPo->op_first;
4298 kid = cLISTOPo->op_first;
4300 if (kid->op_type == OP_PUSHMARK)
4301 kid = kid->op_sibling;
4302 if (kid && o->op_flags & OPf_STACKED)
4303 kid = kid->op_sibling;
4304 else if (kid && !kid->op_sibling) { /* print HANDLE; */
4305 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
4306 o->op_flags |= OPf_STACKED; /* make it a filehandle */
4307 kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
4308 cLISTOPo->op_first->op_sibling = kid;
4309 cLISTOPo->op_last = kid;
4310 kid = kid->op_sibling;
4315 append_elem(o->op_type, o, newDEFSVOP());
4321 if (hints & HINT_LOCALE)
4322 o->op_private |= OPpLOCALE;
4329 ck_fun_locale(OP *o)
4335 if (hints & HINT_LOCALE)
4336 o->op_private |= OPpLOCALE;
4347 if (hints & HINT_LOCALE)
4348 o->op_private |= OPpLOCALE;
4357 o->op_private |= OPpRUNTIME;
4370 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
4371 o->op_private |= OPpREPEAT_DOLIST;
4372 cBINOPo->op_first = force_list(cBINOPo->op_first);
4382 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
4383 SVOP *kid = (SVOP*)cUNOPo->op_first;
4385 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
4387 for (s = SvPVX(kid->op_sv); *s; s++) {
4388 if (*s == ':' && s[1] == ':') {
4390 Move(s+2, s+1, strlen(s+2)+1, char);
4391 --SvCUR(kid->op_sv);
4394 sv_catpvn(kid->op_sv, ".pm", 3);
4403 croak("NOT IMPL LINE %d",__LINE__);
4412 if (o->op_flags & OPf_KIDS) {
4413 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
4414 if (kid && kid->op_sibling) {
4415 o->op_type = OP_SSELECT;
4416 o->op_ppaddr = ppaddr[OP_SSELECT];
4418 return fold_constants(o);
4422 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
4423 if (kid && kid->op_type == OP_RV2GV)
4424 kid->op_private &= ~HINT_STRICT_REFS;
4431 I32 type = o->op_type;
4433 if (!(o->op_flags & OPf_KIDS)) {
4438 if (!CvUNIQUE(compcv)) {
4439 argop = newOP(OP_PADAV, OPf_REF);
4440 argop->op_targ = 0; /* curpad[0] is @_ */
4443 argop = newUNOP(OP_RV2AV, 0,
4444 scalar(newGVOP(OP_GV, 0,
4445 gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
4448 argop = newUNOP(OP_RV2AV, 0,
4449 scalar(newGVOP(OP_GV, 0, !CvUNIQUE(compcv) ?
4450 defgv : gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
4451 #endif /* USE_THREADS */
4452 return newUNOP(type, 0, scalar(argop));
4454 return scalar(modkids(ck_fun(o), type));
4462 if (hints & HINT_LOCALE)
4463 o->op_private |= OPpLOCALE;
4466 if (o->op_flags & OPf_STACKED) {
4467 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
4469 kid = kUNOP->op_first; /* get past rv2gv */
4471 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
4473 if (kid->op_type == OP_SCOPE) {
4477 else if (kid->op_type == OP_LEAVE) {
4478 if (o->op_type == OP_SORT) {
4479 null(kid); /* wipe out leave */
4482 for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
4483 if (k->op_next == kid)
4488 kid->op_next = 0; /* just disconnect the leave */
4489 k = kLISTOP->op_first;
4493 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
4494 null(kid); /* wipe out rv2gv */
4495 if (o->op_type == OP_SORT)
4499 o->op_flags |= OPf_SPECIAL;
4511 if (o->op_flags & OPf_STACKED)
4512 return no_fh_allowed(o);
4514 kid = cLISTOPo->op_first;
4515 if (kid->op_type != OP_NULL)
4516 croak("panic: ck_split");
4517 kid = kid->op_sibling;
4518 op_free(cLISTOPo->op_first);
4519 cLISTOPo->op_first = kid;
4521 cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpv(" ", 1));
4522 cLISTOPo->op_last = kid; /* There was only one element previously */
4525 if (kid->op_type != OP_MATCH) {
4526 OP *sibl = kid->op_sibling;
4527 kid->op_sibling = 0;
4528 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
4529 if (cLISTOPo->op_first == cLISTOPo->op_last)
4530 cLISTOPo->op_last = kid;
4531 cLISTOPo->op_first = kid;
4532 kid->op_sibling = sibl;
4535 kid->op_type = OP_PUSHRE;
4536 kid->op_ppaddr = ppaddr[OP_PUSHRE];
4539 if (!kid->op_sibling)
4540 append_elem(OP_SPLIT, o, newDEFSVOP());
4542 kid = kid->op_sibling;
4545 if (!kid->op_sibling)
4546 append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
4548 kid = kid->op_sibling;
4551 if (kid->op_sibling)
4552 return too_many_arguments(o,op_desc[o->op_type]);
4561 OP *prev = ((cUNOPo->op_first->op_sibling)
4562 ? cUNOPo : ((UNOP*)cUNOPo->op_first))->op_first;
4563 OP *o2 = prev->op_sibling;
4571 for (cvop = o2; cvop->op_sibling; cvop = cvop->op_sibling) ;
4572 if (cvop->op_type == OP_RV2CV) {
4574 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
4575 null(cvop); /* disable rv2cv */
4576 tmpop = (SVOP*)((UNOP*)cvop)->op_first;
4577 if (tmpop->op_type == OP_GV) {
4578 cv = GvCVu(tmpop->op_sv);
4579 if (cv && SvPOK(cv) && !(o->op_private & OPpENTERSUB_AMPER)) {
4580 namegv = CvANON(cv) ? (GV*)tmpop->op_sv : CvGV(cv);
4581 proto = SvPV((SV*)cv, na);
4585 o->op_private |= (hints & HINT_STRICT_REFS);
4586 if (PERLDB_SUB && curstash != debstash)
4587 o->op_private |= OPpENTERSUB_DB;
4588 while (o2 != cvop) {
4592 return too_many_arguments(o, gv_ename(namegv));
4610 if (o2->op_type != OP_REFGEN && o2->op_type != OP_UNDEF)
4611 bad_type(arg, "block", gv_ename(namegv), o2);
4616 if (o2->op_type == OP_RV2GV)
4620 OP* sib = kid->op_sibling;
4621 kid->op_sibling = 0;
4622 o2 = newUNOP(OP_RV2GV, 0, kid);
4623 o2->op_sibling = sib;
4624 prev->op_sibling = o2;
4632 if (o2->op_type != OP_RV2GV)
4633 bad_type(arg, "symbol", gv_ename(namegv), o2);
4636 if (o2->op_type != OP_RV2CV)
4637 bad_type(arg, "sub", gv_ename(namegv), o2);
4640 if (o2->op_type != OP_RV2SV && o2->op_type != OP_PADSV)
4641 bad_type(arg, "scalar", gv_ename(namegv), o2);
4644 if (o2->op_type != OP_RV2AV && o2->op_type != OP_PADAV)
4645 bad_type(arg, "array", gv_ename(namegv), o2);
4648 if (o2->op_type != OP_RV2HV && o2->op_type != OP_PADHV)
4649 bad_type(arg, "hash", gv_ename(namegv), o2);
4653 OP* sib = kid->op_sibling;
4654 kid->op_sibling = 0;
4655 o2 = newUNOP(OP_REFGEN, 0, kid);
4656 o2->op_sibling = sib;
4657 prev->op_sibling = o2;
4668 croak("Malformed prototype for %s: %s",
4669 gv_ename(namegv), SvPV((SV*)cv, na));
4674 mod(o2, OP_ENTERSUB);
4676 o2 = o2->op_sibling;
4678 if (proto && !optional &&
4679 (*proto && *proto != '@' && *proto != '%' && *proto != ';'))
4680 return too_few_arguments(o, gv_ename(namegv));
4687 SvREADONLY_on(cSVOPo->op_sv);
4694 if (o->op_flags & OPf_KIDS) {
4695 SVOP *kid = (SVOP*)cUNOPo->op_first;
4697 if (kid->op_type == OP_NULL)
4698 kid = (SVOP*)kid->op_sibling;
4700 kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE))
4701 o->op_flags |= OPf_SPECIAL;
4706 /* A peephole optimizer. We visit the ops in the order they're to execute. */
4709 peep(register OP *o)
4712 register OP* oldop = 0;
4713 if (!o || o->op_seq)
4718 for (; o; o = o->op_next) {
4724 switch (o->op_type) {
4727 curcop = ((COP*)o); /* for warnings */
4728 o->op_seq = op_seqmax++;
4739 if (o->op_next && o->op_next->op_type == OP_STRINGIFY)
4741 o->op_seq = op_seqmax++;
4744 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
4745 o->op_seq = op_seqmax++;
4746 break; /* Scalar stub must produce undef. List stub is noop */
4750 if (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE)
4757 if (oldop && o->op_next) {
4758 oldop->op_next = o->op_next;
4761 o->op_seq = op_seqmax++;
4765 if (o->op_next->op_type == OP_RV2SV) {
4766 if (!(o->op_next->op_private & OPpDEREF)) {
4768 o->op_private |= o->op_next->op_private & OPpLVAL_INTRO;
4769 o->op_next = o->op_next->op_next;
4770 o->op_type = OP_GVSV;
4771 o->op_ppaddr = ppaddr[OP_GVSV];
4774 else if (o->op_next->op_type == OP_RV2AV) {
4775 OP* pop = o->op_next->op_next;
4777 if (pop->op_type == OP_CONST &&
4778 (op = pop->op_next) &&
4779 pop->op_next->op_type == OP_AELEM &&
4780 !(pop->op_next->op_private &
4781 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF)) &&
4782 (i = SvIV(((SVOP*)pop)->op_sv) - compiling.cop_arybase)
4786 SvREFCNT_dec(((SVOP*)pop)->op_sv);
4790 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
4791 o->op_next = pop->op_next->op_next;
4792 o->op_type = OP_AELEMFAST;
4793 o->op_ppaddr = ppaddr[OP_AELEMFAST];
4794 o->op_private = (U8)i;
4795 GvAVn(((GVOP*)o)->op_gv);
4798 o->op_seq = op_seqmax++;
4802 if (o->op_next->op_type == OP_RV2AV
4803 && (o->op_next->op_flags && OPf_REF))
4806 o->op_next = o->op_next->op_next;
4811 if (o->op_next->op_type == OP_RV2HV
4812 && (o->op_next->op_flags && OPf_REF))
4815 o->op_next = o->op_next->op_next;
4823 o->op_seq = op_seqmax++;
4824 while (cLOGOP->op_other->op_type == OP_NULL)
4825 cLOGOP->op_other = cLOGOP->op_other->op_next;
4826 peep(cLOGOP->op_other);
4830 o->op_seq = op_seqmax++;
4831 peep(cCONDOP->op_true);
4832 peep(cCONDOP->op_false);
4836 o->op_seq = op_seqmax++;
4837 peep(cLOOP->op_redoop);
4838 peep(cLOOP->op_nextop);
4839 peep(cLOOP->op_lastop);
4844 o->op_seq = op_seqmax++;
4845 peep(cPMOP->op_pmreplstart);
4849 o->op_seq = op_seqmax++;
4850 if (dowarn && o->op_next && o->op_next->op_type == OP_NEXTSTATE) {
4851 if (o->op_next->op_sibling &&
4852 o->op_next->op_sibling->op_type != OP_DIE) {
4853 line_t oldline = curcop->cop_line;
4855 curcop->cop_line = ((COP*)o->op_next)->cop_line;
4856 warn("Statement unlikely to be reached");
4857 warn("(Maybe you meant system() when you said exec()?)\n");
4858 curcop->cop_line = oldline;
4872 if (o->op_private & (OPpDEREF_HV|OPpDEREF_AV|OPpLVAL_INTRO)
4873 || ((BINOP*)o)->op_last->op_type != OP_CONST)
4875 rop = (UNOP*)((BINOP*)o)->op_first;
4876 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
4878 lexname = *av_fetch(comppad_name, rop->op_first->op_targ, TRUE);
4879 if (!SvOBJECT(lexname))
4881 fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
4882 if (!fields || !GvHV(*fields))
4884 svp = &((SVOP*)((BINOP*)o)->op_last)->op_sv;
4885 key = SvPV(*svp, keylen);
4886 indsvp = hv_fetch(GvHV(*fields), key, keylen, FALSE);
4888 croak("No such field \"%s\" in variable %s of type %s",
4889 key, SvPV(lexname, na), HvNAME(SvSTASH(lexname)));
4891 ind = SvIV(*indsvp);
4893 croak("Bad index while coercing array into hash");
4894 rop->op_type = OP_RV2AV;
4895 rop->op_ppaddr = ppaddr[OP_RV2AV];
4896 o->op_type = OP_AELEM;
4897 o->op_ppaddr = ppaddr[OP_AELEM];
4899 *svp = newSViv(ind);
4904 o->op_seq = op_seqmax++;