3 * Copyright (c) 1991-1999, 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 #define CHECKCALL this->*PL_check
24 #define CHECKCALL *PL_check
28 * In the following definition, the ", Nullop" is just to make the compiler
29 * think the expression is of the right type: croak actually does a Siglongjmp.
31 #define CHECKOP(type,o) \
32 ((PL_op_mask && PL_op_mask[type]) \
33 ? ( op_free((OP*)o), \
34 croak("%s trapped by operation mask", PL_op_desc[type]), \
36 : (CHECKCALL[type])((OP*)o))
38 #define PAD_MAX 999999999
40 static bool scalar_mod_type _((OP *o, I32 type));
42 static I32 list_assignment _((OP *o));
43 static void bad_type _((I32 n, char *t, char *name, OP *kid));
44 static OP *modkids _((OP *o, I32 type));
45 static OP *no_fh_allowed _((OP *o));
46 static OP *scalarboolean _((OP *o));
47 static OP *too_few_arguments _((OP *o, char* name));
48 static OP *too_many_arguments _((OP *o, char* name));
49 static void null _((OP* o));
50 static PADOFFSET pad_findlex _((char* name, PADOFFSET newoff, U32 seq,
51 CV* startcv, I32 cx_ix, I32 saweval, U32 flags));
52 static OP *newDEFSVOP _((void));
53 static OP *new_logop _((I32 type, I32 flags, OP **firstp, OP **otherp));
54 static void simplify_sort _((OP *o));
55 static bool is_handle_constructor _((OP *o, I32 argnum));
62 SV* tmpsv = sv_newmortal();
63 gv_efullname3(tmpsv, gv, Nullch);
64 return SvPV(tmpsv,n_a);
70 yyerror(form("Missing comma after first argument to %s function",
71 PL_op_desc[o->op_type]));
76 too_few_arguments(OP *o, char *name)
78 yyerror(form("Not enough arguments for %s", name));
83 too_many_arguments(OP *o, char *name)
85 yyerror(form("Too many arguments for %s", name));
90 bad_type(I32 n, char *t, char *name, OP *kid)
92 yyerror(form("Type of arg %d to %s must be %s (not %s)",
93 (int)n, name, t, PL_op_desc[kid->op_type]));
99 int type = o->op_type;
100 if (type != OP_AELEM && type != OP_HELEM && type != OP_GELEM) {
101 yyerror(form("Can't use subscript on %s", PL_op_desc[type]));
102 if (type == OP_ENTERSUB || type == OP_RV2HV || type == OP_PADHV) {
104 SV *msg = sv_2mortal(
105 newSVpvf("(Did you mean $ or @ instead of %c?)\n",
106 type == OP_ENTERSUB ? '&' : '%'));
110 sv_catsv(GvSV(PL_errgv), msg);
112 PerlIO_write(PerlIO_stderr(), SvPVX(msg), SvCUR(msg));
117 /* "register" allocation */
120 pad_allocmy(char *name)
128 (PL_hints & HINT_UTF8 && (name[1] & 0xc0) == 0xc0) ||
129 name[1] == '_' && (int)strlen(name) > 2))
131 if (!isPRINT(name[1])) {
132 /* 1999-02-27 mjd@plover.com */
134 p = strchr(name, '\0');
135 /* The next block assumes the buffer is at least 205 chars
136 long. At present, it's always at least 256 chars. */
138 strcpy(name+200, "...");
144 /* Move everything else down one character */
145 for (; p-name > 2; p--)
147 name[2] = toCTRL(name[1]);
150 yyerror(form("Can't use global %s in \"my\"",name));
152 if (ckWARN(WARN_UNSAFE) && AvFILLp(PL_comppad_name) >= 0) {
153 SV **svp = AvARRAY(PL_comppad_name);
154 for (off = AvFILLp(PL_comppad_name); off > PL_comppad_name_floor; off--) {
156 && sv != &PL_sv_undef
157 && (SvIVX(sv) == PAD_MAX || SvIVX(sv) == 0)
158 && strEQ(name, SvPVX(sv)))
161 "\"my\" variable %s masks earlier declaration in same %s",
162 name, (SvIVX(sv) == PAD_MAX ? "scope" : "statement"));
167 off = pad_alloc(OP_PADSV, SVs_PADMY);
169 sv_upgrade(sv, SVt_PVNV);
171 if (PL_in_my_stash) {
173 yyerror(form("Can't declare class for non-scalar %s in \"my\"",
176 (void)SvUPGRADE(sv, SVt_PVMG);
177 SvSTASH(sv) = (HV*)SvREFCNT_inc(PL_in_my_stash);
180 av_store(PL_comppad_name, off, sv);
181 SvNVX(sv) = (double)PAD_MAX;
182 SvIVX(sv) = 0; /* Not yet introduced--see newSTATEOP */
183 if (!PL_min_intro_pending)
184 PL_min_intro_pending = off;
185 PL_max_intro_pending = off;
187 av_store(PL_comppad, off, (SV*)newAV());
188 else if (*name == '%')
189 av_store(PL_comppad, off, (SV*)newHV());
190 SvPADMY_on(PL_curpad[off]);
194 #define FINDLEX_NOSEARCH 1 /* don't search outer contexts */
197 pad_findlex(char *name, PADOFFSET newoff, U32 seq, CV* startcv, I32 cx_ix, I32 saweval,
205 register PERL_CONTEXT *cx;
207 for (cv = startcv; cv; cv = CvOUTSIDE(cv)) {
208 AV *curlist = CvPADLIST(cv);
209 SV **svp = av_fetch(curlist, 0, FALSE);
212 if (!svp || *svp == &PL_sv_undef)
215 svp = AvARRAY(curname);
216 for (off = AvFILLp(curname); off > 0; off--) {
217 if ((sv = svp[off]) &&
218 sv != &PL_sv_undef &&
220 seq > I_32(SvNVX(sv)) &&
221 strEQ(SvPVX(sv), name))
232 return 0; /* don't clone from inactive stack frame */
236 oldpad = (AV*)*av_fetch(curlist, depth, FALSE);
237 oldsv = *av_fetch(oldpad, off, TRUE);
238 if (!newoff) { /* Not a mere clone operation. */
239 SV *namesv = NEWSV(1103,0);
240 newoff = pad_alloc(OP_PADSV, SVs_PADMY);
241 sv_upgrade(namesv, SVt_PVNV);
242 sv_setpv(namesv, name);
243 av_store(PL_comppad_name, newoff, namesv);
244 SvNVX(namesv) = (double)PL_curcop->cop_seq;
245 SvIVX(namesv) = PAD_MAX; /* A ref, intro immediately */
246 SvFAKE_on(namesv); /* A ref, not a real var */
247 if (SvOBJECT(sv)) { /* A typed var */
249 (void)SvUPGRADE(namesv, SVt_PVMG);
250 SvSTASH(namesv) = (HV*)SvREFCNT_inc((SV*)SvSTASH(sv));
253 if (CvANON(PL_compcv) || SvTYPE(PL_compcv) == SVt_PVFM) {
254 /* "It's closures all the way down." */
255 CvCLONE_on(PL_compcv);
257 if (CvANON(PL_compcv))
258 oldsv = Nullsv; /* no need to keep ref */
263 bcv && bcv != cv && !CvCLONE(bcv);
264 bcv = CvOUTSIDE(bcv))
269 if (ckWARN(WARN_CLOSURE)
270 && !CvUNIQUE(bcv) && !CvUNIQUE(cv))
273 "Variable \"%s\" may be unavailable",
281 else if (!CvUNIQUE(PL_compcv)) {
282 if (ckWARN(WARN_CLOSURE) && !SvFAKE(sv) && !CvUNIQUE(cv))
284 "Variable \"%s\" will not stay shared", name);
287 av_store(PL_comppad, newoff, SvREFCNT_inc(oldsv));
293 if (flags & FINDLEX_NOSEARCH)
296 /* Nothing in current lexical context--try eval's context, if any.
297 * This is necessary to let the perldb get at lexically scoped variables.
298 * XXX This will also probably interact badly with eval tree caching.
301 for (i = cx_ix; i >= 0; i--) {
303 switch (CxTYPE(cx)) {
305 if (i == 0 && saweval) {
306 seq = cxstack[saweval].blk_oldcop->cop_seq;
307 return pad_findlex(name, newoff, seq, PL_main_cv, -1, saweval, 0);
311 switch (cx->blk_eval.old_op_type) {
317 /* require must have its own scope */
325 if (PL_debstash && CvSTASH(cv) == PL_debstash) { /* ignore DB'* scope */
326 saweval = i; /* so we know where we were called from */
329 seq = cxstack[saweval].blk_oldcop->cop_seq;
330 return pad_findlex(name, newoff, seq, cv, i-1, saweval,FINDLEX_NOSEARCH);
338 pad_findmy(char *name)
344 SV **svp = AvARRAY(PL_comppad_name);
345 U32 seq = PL_cop_seqmax;
351 * Special case to get lexical (and hence per-thread) @_.
352 * XXX I need to find out how to tell at parse-time whether use
353 * of @_ should refer to a lexical (from a sub) or defgv (global
354 * scope and maybe weird sub-ish things like formats). See
355 * startsub in perly.y. It's possible that @_ could be lexical
356 * (at least from subs) even in non-threaded perl.
358 if (strEQ(name, "@_"))
359 return 0; /* success. (NOT_IN_PAD indicates failure) */
360 #endif /* USE_THREADS */
362 /* The one we're looking for is probably just before comppad_name_fill. */
363 for (off = AvFILLp(PL_comppad_name); off > 0; off--) {
364 if ((sv = svp[off]) &&
365 sv != &PL_sv_undef &&
368 seq > I_32(SvNVX(sv)))) &&
369 strEQ(SvPVX(sv), name))
372 return (PADOFFSET)off;
373 pendoff = off; /* this pending def. will override import */
377 outside = CvOUTSIDE(PL_compcv);
379 /* Check if if we're compiling an eval'', and adjust seq to be the
380 * eval's seq number. This depends on eval'' having a non-null
381 * CvOUTSIDE() while it is being compiled. The eval'' itself is
382 * identified by CvEVAL being true and CvGV being null. */
383 if (outside && CvEVAL(PL_compcv) && !CvGV(PL_compcv) && cxstack_ix >= 0) {
384 cx = &cxstack[cxstack_ix];
386 seq = cx->blk_oldcop->cop_seq;
389 /* See if it's in a nested scope */
390 off = pad_findlex(name, 0, seq, outside, cxstack_ix, 0, 0);
392 /* If there is a pending local definition, this new alias must die */
394 SvIVX(AvARRAY(PL_comppad_name)[off]) = seq;
395 return off; /* pad_findlex returns 0 for failure...*/
397 return NOT_IN_PAD; /* ...but we return NOT_IN_PAD for failure */
401 pad_leavemy(I32 fill)
404 SV **svp = AvARRAY(PL_comppad_name);
406 if (PL_min_intro_pending && fill < PL_min_intro_pending) {
407 for (off = PL_max_intro_pending; off >= PL_min_intro_pending; off--) {
408 if ((sv = svp[off]) && sv != &PL_sv_undef)
409 warn("%s never introduced", SvPVX(sv));
412 /* "Deintroduce" my variables that are leaving with this scope. */
413 for (off = AvFILLp(PL_comppad_name); off > fill; off--) {
414 if ((sv = svp[off]) && sv != &PL_sv_undef && SvIVX(sv) == PAD_MAX)
415 SvIVX(sv) = PL_cop_seqmax;
420 pad_alloc(I32 optype, U32 tmptype)
426 if (AvARRAY(PL_comppad) != PL_curpad)
427 croak("panic: pad_alloc");
428 if (PL_pad_reset_pending)
430 if (tmptype & SVs_PADMY) {
432 sv = *av_fetch(PL_comppad, AvFILLp(PL_comppad) + 1, TRUE);
433 } while (SvPADBUSY(sv)); /* need a fresh one */
434 retval = AvFILLp(PL_comppad);
437 SV **names = AvARRAY(PL_comppad_name);
438 SSize_t names_fill = AvFILLp(PL_comppad_name);
441 * "foreach" index vars temporarily become aliases to non-"my"
442 * values. Thus we must skip, not just pad values that are
443 * marked as current pad values, but also those with names.
445 if (++PL_padix <= names_fill &&
446 (sv = names[PL_padix]) && sv != &PL_sv_undef)
448 sv = *av_fetch(PL_comppad, PL_padix, TRUE);
449 if (!(SvFLAGS(sv) & (SVs_PADTMP|SVs_PADMY)))
454 SvFLAGS(sv) |= tmptype;
455 PL_curpad = AvARRAY(PL_comppad);
457 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx alloc %ld for %s\n",
458 (unsigned long) thr, (unsigned long) PL_curpad,
459 (long) retval, PL_op_name[optype]));
461 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx alloc %ld for %s\n",
462 (unsigned long) PL_curpad,
463 (long) retval, PL_op_name[optype]));
464 #endif /* USE_THREADS */
465 return (PADOFFSET)retval;
473 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx sv %d\n",
474 (unsigned long) thr, (unsigned long) PL_curpad, po));
477 croak("panic: pad_sv po");
478 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx sv %d\n",
479 (unsigned long) PL_curpad, po));
480 #endif /* USE_THREADS */
481 return PL_curpad[po]; /* eventually we'll turn this into a macro */
485 pad_free(PADOFFSET po)
490 if (AvARRAY(PL_comppad) != PL_curpad)
491 croak("panic: pad_free curpad");
493 croak("panic: pad_free po");
495 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx free %d\n",
496 (unsigned long) thr, (unsigned long) PL_curpad, po));
498 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx free %d\n",
499 (unsigned long) PL_curpad, po));
500 #endif /* USE_THREADS */
501 if (PL_curpad[po] && PL_curpad[po] != &PL_sv_undef)
502 SvPADTMP_off(PL_curpad[po]);
503 if ((I32)po < PL_padix)
508 pad_swipe(PADOFFSET po)
511 if (AvARRAY(PL_comppad) != PL_curpad)
512 croak("panic: pad_swipe curpad");
514 croak("panic: pad_swipe po");
516 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx swipe %d\n",
517 (unsigned long) thr, (unsigned long) PL_curpad, po));
519 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx swipe %d\n",
520 (unsigned long) PL_curpad, po));
521 #endif /* USE_THREADS */
522 SvPADTMP_off(PL_curpad[po]);
523 PL_curpad[po] = NEWSV(1107,0);
524 SvPADTMP_on(PL_curpad[po]);
525 if ((I32)po < PL_padix)
529 /* XXX pad_reset() is currently disabled because it results in serious bugs.
530 * It causes pad temp TARGs to be shared between OPs. Since TARGs are pushed
531 * on the stack by OPs that use them, there are several ways to get an alias
532 * to a shared TARG. Such an alias will change randomly and unpredictably.
533 * We avoid doing this until we can think of a Better Way.
538 #ifdef USE_BROKEN_PAD_RESET
542 if (AvARRAY(PL_comppad) != PL_curpad)
543 croak("panic: pad_reset curpad");
545 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx reset\n",
546 (unsigned long) thr, (unsigned long) PL_curpad));
548 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx reset\n",
549 (unsigned long) PL_curpad));
550 #endif /* USE_THREADS */
551 if (!PL_tainting) { /* Can't mix tainted and non-tainted temporaries. */
552 for (po = AvMAX(PL_comppad); po > PL_padix_floor; po--) {
553 if (PL_curpad[po] && !SvIMMORTAL(PL_curpad[po]))
554 SvPADTMP_off(PL_curpad[po]);
556 PL_padix = PL_padix_floor;
559 PL_pad_reset_pending = FALSE;
563 /* find_threadsv is not reentrant */
565 find_threadsv(const char *name)
571 /* We currently only handle names of a single character */
572 p = strchr(PL_threadsv_names, *name);
575 key = p - PL_threadsv_names;
576 MUTEX_LOCK(&thr->mutex);
577 svp = av_fetch(thr->threadsv, key, FALSE);
579 MUTEX_UNLOCK(&thr->mutex);
581 SV *sv = NEWSV(0, 0);
582 av_store(thr->threadsv, key, sv);
583 thr->threadsvp = AvARRAY(thr->threadsv);
584 MUTEX_UNLOCK(&thr->mutex);
586 * Some magic variables used to be automagically initialised
587 * in gv_fetchpv. Those which are now per-thread magicals get
588 * initialised here instead.
594 sv_setpv(sv, "\034");
595 sv_magic(sv, 0, 0, name, 1);
600 PL_sawampersand = TRUE;
614 /* XXX %! tied to Errno.pm needs to be added here.
615 * See gv_fetchpv(). */
619 sv_magic(sv, 0, 0, name, 1);
621 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
622 "find_threadsv: new SV %p for $%s%c\n",
623 sv, (*name < 32) ? "^" : "",
624 (*name < 32) ? toCTRL(*name) : *name));
628 #endif /* USE_THREADS */
635 register OP *kid, *nextkid;
637 if (!o || o->op_seq == (U16)-1)
640 if (o->op_flags & OPf_KIDS) {
641 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
642 nextkid = kid->op_sibling; /* Get before next freeing kid */
647 switch (o->op_type) {
649 o->op_targ = 0; /* Was holding old type, if any. */
652 o->op_targ = 0; /* Was holding hints. */
656 if (!(o->op_flags & OPf_SPECIAL))
660 o->op_targ = 0; /* Was holding index into thr->threadsv AV. */
662 #endif /* USE_THREADS */
664 if (!(o->op_flags & OPf_REF)
665 || (PL_check[o->op_type] != FUNC_NAME_TO_PTR(ck_ftst)))
671 SvREFCNT_dec(cGVOPo->op_gv);
675 Safefree(cCOPo->cop_label);
676 SvREFCNT_dec(cCOPo->cop_filegv);
677 if (cCOPo->cop_warnings != WARN_NONE && cCOPo->cop_warnings != WARN_ALL)
678 SvREFCNT_dec(cCOPo->cop_warnings);
681 SvREFCNT_dec(cSVOPo->op_sv);
687 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
691 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF))
692 SvREFCNT_dec(cSVOPo->op_sv);
694 Safefree(cPVOPo->op_pv);
697 op_free(cPMOPo->op_pmreplroot);
702 ReREFCNT_dec(cPMOPo->op_pmregexp);
707 pad_free(o->op_targ);
715 if (o->op_type != OP_NULL && o->op_type != OP_THREADSV && o->op_targ > 0)
716 pad_free(o->op_targ);
717 o->op_targ = o->op_type;
718 o->op_type = OP_NULL;
719 o->op_ppaddr = PL_ppaddr[OP_NULL];
722 /* Contextualizers */
724 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
734 /* establish postfix order */
735 if (cUNOPo->op_first) {
736 o->op_next = LINKLIST(cUNOPo->op_first);
737 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
739 kid->op_next = LINKLIST(kid->op_sibling);
754 if (o && o->op_flags & OPf_KIDS) {
755 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
764 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
766 if (ckWARN(WARN_SYNTAX)) {
767 line_t oldline = PL_curcop->cop_line;
769 if (PL_copline != NOLINE)
770 PL_curcop->cop_line = PL_copline;
771 warner(WARN_SYNTAX, "Found = in conditional, should be ==");
772 PL_curcop->cop_line = oldline;
783 /* assumes no premature commitment */
784 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
785 || o->op_type == OP_RETURN)
788 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
790 switch (o->op_type) {
792 if (o->op_private & OPpREPEAT_DOLIST)
793 null(((LISTOP*)cBINOPo->op_first)->op_first);
794 scalar(cBINOPo->op_first);
799 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
803 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
804 if (!kPMOP->op_pmreplroot)
805 deprecate("implicit split to @_");
813 if (o->op_flags & OPf_KIDS) {
814 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
820 kid = cLISTOPo->op_first;
822 while (kid = kid->op_sibling) {
828 WITH_THR(PL_curcop = &PL_compiling);
833 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
839 WITH_THR(PL_curcop = &PL_compiling);
852 /* assumes no premature commitment */
853 U8 want = o->op_flags & OPf_WANT;
854 if (!o || (want && want != OPf_WANT_SCALAR) || PL_error_count
855 || o->op_type == OP_RETURN)
858 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
860 switch (o->op_type) {
862 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
866 if (o->op_flags & OPf_STACKED)
870 if (o->op_private == 4)
941 if (!(o->op_private & OPpLVAL_INTRO))
942 useless = PL_op_desc[o->op_type];
949 if (!(o->op_private & OPpLVAL_INTRO) &&
950 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
951 useless = "a variable";
956 WITH_THR(PL_curcop = ((COP*)o)); /* for warning below */
963 if (ckWARN(WARN_VOID)) {
964 useless = "a constant";
965 if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
967 else if (SvPOK(sv)) {
968 if (strnEQ(SvPVX(sv), "di", 2) ||
969 strnEQ(SvPVX(sv), "ds", 2) ||
970 strnEQ(SvPVX(sv), "ig", 2))
975 null(o); /* don't execute a constant */
976 SvREFCNT_dec(sv); /* don't even remember it */
980 o->op_type = OP_PREINC; /* pre-increment is faster */
981 o->op_ppaddr = PL_ppaddr[OP_PREINC];
985 o->op_type = OP_PREDEC; /* pre-decrement is faster */
986 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
992 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
997 if (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE)
998 WITH_THR(PL_curcop = ((COP*)o)); /* for warning below */
999 if (o->op_flags & OPf_STACKED)
1005 if (!(o->op_flags & OPf_KIDS))
1014 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1021 /* all requires must return a boolean value */
1022 o->op_flags &= ~OPf_WANT;
1025 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
1026 if (!kPMOP->op_pmreplroot)
1027 deprecate("implicit split to @_");
1033 if (ckWARN(WARN_VOID))
1034 warner(WARN_VOID, "Useless use of %s in void context", useless);
1043 if (o && o->op_flags & OPf_KIDS) {
1044 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1055 /* assumes no premature commitment */
1056 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
1057 || o->op_type == OP_RETURN)
1060 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
1062 switch (o->op_type) {
1065 list(cBINOPo->op_first);
1070 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1078 if (!(o->op_flags & OPf_KIDS))
1080 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1081 list(cBINOPo->op_first);
1082 return gen_constant_list(o);
1089 kid = cLISTOPo->op_first;
1091 while (kid = kid->op_sibling) {
1092 if (kid->op_sibling)
1097 WITH_THR(PL_curcop = &PL_compiling);
1101 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1102 if (kid->op_sibling)
1107 WITH_THR(PL_curcop = &PL_compiling);
1110 /* all requires must return a boolean value */
1111 o->op_flags &= ~OPf_WANT;
1123 if (o->op_type == OP_LINESEQ ||
1124 o->op_type == OP_SCOPE ||
1125 o->op_type == OP_LEAVE ||
1126 o->op_type == OP_LEAVETRY)
1129 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1130 if (kid->op_sibling) {
1134 PL_curcop = &PL_compiling;
1136 o->op_flags &= ~OPf_PARENS;
1137 if (PL_hints & HINT_BLOCK_SCOPE)
1138 o->op_flags |= OPf_PARENS;
1141 o = newOP(OP_STUB, 0);
1146 modkids(OP *o, I32 type)
1149 if (o && o->op_flags & OPf_KIDS) {
1150 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1157 mod(OP *o, I32 type)
1164 if (!o || PL_error_count)
1167 switch (o->op_type) {
1172 if (!(o->op_private & (OPpCONST_ARYBASE)))
1174 if (PL_eval_start && PL_eval_start->op_type == OP_CONST) {
1175 PL_compiling.cop_arybase = (I32)SvIV(((SVOP*)PL_eval_start)->op_sv);
1179 SAVEI32(PL_compiling.cop_arybase);
1180 PL_compiling.cop_arybase = 0;
1182 else if (type == OP_REFGEN)
1185 croak("That use of $[ is unsupported");
1188 if (o->op_flags & OPf_PARENS)
1192 if ((type == OP_UNDEF || type == OP_REFGEN) &&
1193 !(o->op_flags & OPf_STACKED)) {
1194 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1195 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1196 assert(cUNOPo->op_first->op_type == OP_NULL);
1197 null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1203 /* grep, foreach, subcalls, refgen */
1204 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1206 yyerror(form("Can't modify %s in %s",
1207 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
1208 ? "do block" : PL_op_desc[o->op_type]),
1209 type ? PL_op_desc[type] : "local"));
1223 case OP_RIGHT_SHIFT:
1232 if (!(o->op_flags & OPf_STACKED))
1238 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1244 if (!type && cUNOPo->op_first->op_type != OP_GV)
1245 croak("Can't localize through a reference");
1246 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
1247 PL_modcount = 10000;
1248 return o; /* Treat \(@foo) like ordinary list. */
1252 if (scalar_mod_type(o, type))
1254 ref(cUNOPo->op_first, o->op_type);
1263 PL_modcount = 10000;
1266 if (!type && cUNOPo->op_first->op_type != OP_GV)
1267 croak("Can't localize through a reference");
1268 ref(cUNOPo->op_first, o->op_type);
1272 PL_hints |= HINT_BLOCK_SCOPE;
1282 PL_modcount = 10000;
1283 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1284 return o; /* Treat \(@foo) like ordinary list. */
1285 if (scalar_mod_type(o, type))
1291 croak("Can't localize lexical variable %s",
1292 SvPV(*av_fetch(PL_comppad_name, o->op_targ, 4), n_a));
1297 PL_modcount++; /* XXX ??? */
1299 #endif /* USE_THREADS */
1305 if (type != OP_SASSIGN)
1309 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
1315 pad_free(o->op_targ);
1316 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
1317 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
1318 if (o->op_flags & OPf_KIDS)
1319 mod(cBINOPo->op_first->op_sibling, type);
1324 ref(cBINOPo->op_first, o->op_type);
1325 if (type == OP_ENTERSUB &&
1326 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1327 o->op_private |= OPpLVAL_DEFER;
1334 if (o->op_flags & OPf_KIDS)
1335 mod(cLISTOPo->op_last, type);
1339 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
1341 else if (!(o->op_flags & OPf_KIDS))
1343 if (o->op_targ != OP_LIST) {
1344 mod(cBINOPo->op_first, type);
1349 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1353 o->op_flags |= OPf_MOD;
1355 if (type == OP_AASSIGN || type == OP_SASSIGN)
1356 o->op_flags |= OPf_SPECIAL|OPf_REF;
1358 o->op_private |= OPpLVAL_INTRO;
1359 o->op_flags &= ~OPf_SPECIAL;
1360 PL_hints |= HINT_BLOCK_SCOPE;
1362 else if (type != OP_GREPSTART && type != OP_ENTERSUB)
1363 o->op_flags |= OPf_REF;
1368 scalar_mod_type(OP *o, I32 type)
1372 if (o->op_type == OP_RV2GV)
1396 case OP_RIGHT_SHIFT:
1415 is_handle_constructor(OP *o, I32 argnum)
1417 switch (o->op_type) {
1425 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
1438 refkids(OP *o, I32 type)
1441 if (o && o->op_flags & OPf_KIDS) {
1442 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1449 ref(OP *o, I32 type)
1453 if (!o || PL_error_count)
1456 switch (o->op_type) {
1458 if ((type == OP_DEFINED || type == OP_LOCK) &&
1459 !(o->op_flags & OPf_STACKED)) {
1460 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1461 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1462 assert(cUNOPo->op_first->op_type == OP_NULL);
1463 null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1464 o->op_flags |= OPf_SPECIAL;
1469 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1473 if (type == OP_DEFINED)
1474 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1475 ref(cUNOPo->op_first, o->op_type);
1478 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1479 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1480 : type == OP_RV2HV ? OPpDEREF_HV
1482 o->op_flags |= OPf_MOD;
1487 o->op_flags |= OPf_MOD; /* XXX ??? */
1492 o->op_flags |= OPf_REF;
1495 if (type == OP_DEFINED)
1496 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1497 ref(cUNOPo->op_first, o->op_type);
1502 o->op_flags |= OPf_REF;
1507 if (!(o->op_flags & OPf_KIDS))
1509 ref(cBINOPo->op_first, type);
1513 ref(cBINOPo->op_first, o->op_type);
1514 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1515 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1516 : type == OP_RV2HV ? OPpDEREF_HV
1518 o->op_flags |= OPf_MOD;
1526 if (!(o->op_flags & OPf_KIDS))
1528 ref(cLISTOPo->op_last, type);
1543 if (!o || PL_error_count)
1547 if (type == OP_LIST) {
1548 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1550 } else if (type == OP_UNDEF) {
1552 } else if (type != OP_PADSV &&
1555 type != OP_PUSHMARK)
1557 yyerror(form("Can't declare %s in my", PL_op_desc[o->op_type]));
1560 o->op_flags |= OPf_MOD;
1561 o->op_private |= OPpLVAL_INTRO;
1569 o->op_flags |= OPf_PARENS;
1574 bind_match(I32 type, OP *left, OP *right)
1579 if (ckWARN(WARN_UNSAFE) &&
1580 (left->op_type == OP_RV2AV ||
1581 left->op_type == OP_RV2HV ||
1582 left->op_type == OP_PADAV ||
1583 left->op_type == OP_PADHV)) {
1584 char *desc = PL_op_desc[(right->op_type == OP_SUBST ||
1585 right->op_type == OP_TRANS)
1586 ? right->op_type : OP_MATCH];
1587 char *sample = ((left->op_type == OP_RV2AV ||
1588 left->op_type == OP_PADAV)
1589 ? "@array" : "%hash");
1591 "Applying %s to %s will act on scalar(%s)",
1592 desc, sample, sample);
1595 if (right->op_type == OP_MATCH ||
1596 right->op_type == OP_SUBST ||
1597 right->op_type == OP_TRANS) {
1598 right->op_flags |= OPf_STACKED;
1599 if (right->op_type != OP_MATCH)
1600 left = mod(left, right->op_type);
1601 if (right->op_type == OP_TRANS)
1602 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
1604 o = prepend_elem(right->op_type, scalar(left), right);
1606 return newUNOP(OP_NOT, 0, scalar(o));
1610 return bind_match(type, left,
1611 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
1619 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
1620 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
1627 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
1628 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1629 o->op_type = OP_LEAVE;
1630 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
1633 if (o->op_type == OP_LINESEQ) {
1635 o->op_type = OP_SCOPE;
1636 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
1637 kid = ((LISTOP*)o)->op_first;
1638 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE){
1639 SvREFCNT_dec(((COP*)kid)->cop_filegv);
1644 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
1654 SAVESPTR(GvHV(PL_hintgv));
1655 GvHV(PL_hintgv) = newHVhv(GvHV(PL_hintgv));
1656 SAVEFREESV(GvHV(PL_hintgv));
1660 block_start(int full)
1663 int retval = PL_savestack_ix;
1665 SAVEI32(PL_comppad_name_floor);
1667 if ((PL_comppad_name_fill = AvFILLp(PL_comppad_name)) > 0)
1668 PL_comppad_name_floor = PL_comppad_name_fill;
1670 PL_comppad_name_floor = 0;
1672 SAVEI32(PL_min_intro_pending);
1673 SAVEI32(PL_max_intro_pending);
1674 PL_min_intro_pending = 0;
1675 SAVEI32(PL_comppad_name_fill);
1676 SAVEI32(PL_padix_floor);
1677 PL_padix_floor = PL_padix;
1678 PL_pad_reset_pending = FALSE;
1680 PL_hints &= ~HINT_BLOCK_SCOPE;
1681 SAVEPPTR(PL_compiling.cop_warnings);
1682 if (PL_compiling.cop_warnings != WARN_ALL &&
1683 PL_compiling.cop_warnings != WARN_NONE) {
1684 PL_compiling.cop_warnings = newSVsv(PL_compiling.cop_warnings) ;
1685 SAVEFREESV(PL_compiling.cop_warnings) ;
1693 block_end(I32 floor, OP *seq)
1696 int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
1697 OP* retval = scalarseq(seq);
1699 PL_pad_reset_pending = FALSE;
1700 PL_compiling.op_private = PL_hints;
1702 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
1703 pad_leavemy(PL_comppad_name_fill);
1712 OP *o = newOP(OP_THREADSV, 0);
1713 o->op_targ = find_threadsv("_");
1716 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
1717 #endif /* USE_THREADS */
1727 PL_eval_root = newUNOP(OP_LEAVEEVAL, ((PL_in_eval & 4) ? OPf_SPECIAL : 0), o);
1728 PL_eval_start = linklist(PL_eval_root);
1729 PL_eval_root->op_next = 0;
1730 peep(PL_eval_start);
1735 PL_main_root = scope(sawparens(scalarvoid(o)));
1736 PL_curcop = &PL_compiling;
1737 PL_main_start = LINKLIST(PL_main_root);
1738 PL_main_root->op_next = 0;
1739 peep(PL_main_start);
1742 /* Register with debugger */
1744 CV *cv = perl_get_cv("DB::postponed", FALSE);
1748 XPUSHs((SV*)PL_compiling.cop_filegv);
1750 perl_call_sv((SV*)cv, G_DISCARD);
1757 localize(OP *o, I32 lex)
1759 if (o->op_flags & OPf_PARENS)
1763 if (ckWARN(WARN_PARENTHESIS) && PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == ',') {
1765 for (s = PL_bufptr; *s && (isALNUM(*s) || (*s & 0x80) || strchr("@$%, ",*s)); s++) ;
1766 if (*s == ';' || *s == '=')
1767 warner(WARN_PARENTHESIS, "Parentheses missing around \"%s\" list",
1768 lex ? "my" : "local");
1772 PL_in_my_stash = Nullhv;
1776 return mod(o, OP_NULL); /* a bit kludgey */
1782 if (o->op_type == OP_LIST) {
1785 o2 = newOP(OP_THREADSV, 0);
1786 o2->op_targ = find_threadsv(";");
1788 o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
1789 #endif /* USE_THREADS */
1790 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
1796 fold_constants(register OP *o)
1800 I32 type = o->op_type;
1803 if (PL_opargs[type] & OA_RETSCALAR)
1805 if (PL_opargs[type] & OA_TARGET)
1806 o->op_targ = pad_alloc(type, SVs_PADTMP);
1808 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER))
1809 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
1811 if (!(PL_opargs[type] & OA_FOLDCONST))
1826 if (o->op_private & OPpLOCALE)
1831 goto nope; /* Don't try to run w/ errors */
1833 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
1834 if (curop->op_type != OP_CONST &&
1835 curop->op_type != OP_LIST &&
1836 curop->op_type != OP_SCALAR &&
1837 curop->op_type != OP_NULL &&
1838 curop->op_type != OP_PUSHMARK) {
1843 curop = LINKLIST(o);
1847 sv = *(PL_stack_sp--);
1848 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
1849 pad_swipe(o->op_targ);
1850 else if (SvTEMP(sv)) { /* grab mortal temp? */
1851 (void)SvREFCNT_inc(sv);
1855 if (type == OP_RV2GV)
1856 return newGVOP(OP_GV, 0, (GV*)sv);
1858 /* try to smush double to int, but don't smush -2.0 to -2 */
1859 if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK &&
1863 if ((double)iv == SvNV(sv)) {
1868 SvIOK_off(sv); /* undo SvIV() damage */
1870 return newSVOP(OP_CONST, 0, sv);
1874 if (!(PL_opargs[type] & OA_OTHERINT))
1877 if (!(PL_hints & HINT_INTEGER)) {
1878 if (type == OP_DIVIDE || !(o->op_flags & OPf_KIDS))
1881 for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
1882 if (curop->op_type == OP_CONST) {
1883 if (SvIOK(((SVOP*)curop)->op_sv))
1887 if (PL_opargs[curop->op_type] & OA_RETINTEGER)
1891 o->op_ppaddr = PL_ppaddr[++(o->op_type)];
1898 gen_constant_list(register OP *o)
1902 I32 oldtmps_floor = PL_tmps_floor;
1906 return o; /* Don't attempt to run with errors */
1908 PL_op = curop = LINKLIST(o);
1914 PL_tmps_floor = oldtmps_floor;
1916 o->op_type = OP_RV2AV;
1917 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
1918 curop = ((UNOP*)o)->op_first;
1919 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*PL_stack_sp--));
1926 convert(I32 type, I32 flags, OP *o)
1931 if (!o || o->op_type != OP_LIST)
1932 o = newLISTOP(OP_LIST, 0, o, Nullop);
1934 o->op_flags &= ~OPf_WANT;
1936 if (!(PL_opargs[type] & OA_MARK))
1937 null(cLISTOPo->op_first);
1940 o->op_ppaddr = PL_ppaddr[type];
1941 o->op_flags |= flags;
1943 o = CHECKOP(type, o);
1944 if (o->op_type != type)
1947 if (cLISTOPo->op_children < 7) {
1948 /* XXX do we really need to do this if we're done appending?? */
1949 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1951 cLISTOPo->op_last = last; /* in case check substituted last arg */
1954 return fold_constants(o);
1957 /* List constructors */
1960 append_elem(I32 type, OP *first, OP *last)
1968 if (first->op_type != type || type==OP_LIST && first->op_flags & OPf_PARENS)
1969 return newLISTOP(type, 0, first, last);
1971 if (first->op_flags & OPf_KIDS)
1972 ((LISTOP*)first)->op_last->op_sibling = last;
1974 first->op_flags |= OPf_KIDS;
1975 ((LISTOP*)first)->op_first = last;
1977 ((LISTOP*)first)->op_last = last;
1978 ((LISTOP*)first)->op_children++;
1983 append_list(I32 type, LISTOP *first, LISTOP *last)
1991 if (first->op_type != type)
1992 return prepend_elem(type, (OP*)first, (OP*)last);
1994 if (last->op_type != type)
1995 return append_elem(type, (OP*)first, (OP*)last);
1997 first->op_last->op_sibling = last->op_first;
1998 first->op_last = last->op_last;
1999 first->op_children += last->op_children;
2000 if (first->op_children)
2001 first->op_flags |= OPf_KIDS;
2008 prepend_elem(I32 type, OP *first, OP *last)
2016 if (last->op_type == type) {
2017 if (type == OP_LIST) { /* already a PUSHMARK there */
2018 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
2019 ((LISTOP*)last)->op_first->op_sibling = first;
2022 if (!(last->op_flags & OPf_KIDS)) {
2023 ((LISTOP*)last)->op_last = first;
2024 last->op_flags |= OPf_KIDS;
2026 first->op_sibling = ((LISTOP*)last)->op_first;
2027 ((LISTOP*)last)->op_first = first;
2029 ((LISTOP*)last)->op_children++;
2033 return newLISTOP(type, 0, first, last);
2041 return newOP(OP_STUB, 0);
2047 if (!o || o->op_type != OP_LIST)
2048 o = newLISTOP(OP_LIST, 0, o, Nullop);
2054 newLISTOP(I32 type, I32 flags, OP *first, OP *last)
2058 Newz(1101, listop, 1, LISTOP);
2060 listop->op_type = type;
2061 listop->op_ppaddr = PL_ppaddr[type];
2062 listop->op_children = (first != 0) + (last != 0);
2063 listop->op_flags = flags;
2067 else if (!first && last)
2070 first->op_sibling = last;
2071 listop->op_first = first;
2072 listop->op_last = last;
2073 if (type == OP_LIST) {
2075 pushop = newOP(OP_PUSHMARK, 0);
2076 pushop->op_sibling = first;
2077 listop->op_first = pushop;
2078 listop->op_flags |= OPf_KIDS;
2080 listop->op_last = pushop;
2082 else if (listop->op_children)
2083 listop->op_flags |= OPf_KIDS;
2089 newOP(I32 type, I32 flags)
2092 Newz(1101, o, 1, OP);
2094 o->op_ppaddr = PL_ppaddr[type];
2095 o->op_flags = flags;
2098 o->op_private = 0 + (flags >> 8);
2099 if (PL_opargs[type] & OA_RETSCALAR)
2101 if (PL_opargs[type] & OA_TARGET)
2102 o->op_targ = pad_alloc(type, SVs_PADTMP);
2103 return CHECKOP(type, o);
2107 newUNOP(I32 type, I32 flags, OP *first)
2112 first = newOP(OP_STUB, 0);
2113 if (PL_opargs[type] & OA_MARK)
2114 first = force_list(first);
2116 Newz(1101, unop, 1, UNOP);
2117 unop->op_type = type;
2118 unop->op_ppaddr = PL_ppaddr[type];
2119 unop->op_first = first;
2120 unop->op_flags = flags | OPf_KIDS;
2121 unop->op_private = 1 | (flags >> 8);
2122 unop = (UNOP*) CHECKOP(type, unop);
2126 return fold_constants((OP *) unop);
2130 newBINOP(I32 type, I32 flags, OP *first, OP *last)
2133 Newz(1101, binop, 1, BINOP);
2136 first = newOP(OP_NULL, 0);
2138 binop->op_type = type;
2139 binop->op_ppaddr = PL_ppaddr[type];
2140 binop->op_first = first;
2141 binop->op_flags = flags | OPf_KIDS;
2144 binop->op_private = 1 | (flags >> 8);
2147 binop->op_private = 2 | (flags >> 8);
2148 first->op_sibling = last;
2151 binop = (BINOP*)CHECKOP(type, binop);
2155 binop->op_last = binop->op_first->op_sibling;
2157 return fold_constants((OP *)binop);
2161 utf8compare(const void *a, const void *b)
2164 for (i = 0; i < 10; i++) {
2165 if ((*(U8**)a)[i] < (*(U8**)b)[i])
2167 if ((*(U8**)a)[i] > (*(U8**)b)[i])
2174 pmtrans(OP *o, OP *expr, OP *repl)
2176 SV *tstr = ((SVOP*)expr)->op_sv;
2177 SV *rstr = ((SVOP*)repl)->op_sv;
2180 register U8 *t = (U8*)SvPV(tstr, tlen);
2181 register U8 *r = (U8*)SvPV(rstr, rlen);
2187 register short *tbl;
2189 complement = o->op_private & OPpTRANS_COMPLEMENT;
2190 del = o->op_private & OPpTRANS_DELETE;
2191 squash = o->op_private & OPpTRANS_SQUASH;
2193 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
2194 SV* listsv = newSVpvn("# comment\n",10);
2196 U8* tend = t + tlen;
2197 U8* rend = r + rlen;
2213 I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
2214 I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
2220 New(1109, cp, tlen, U8*);
2222 transv = newSVpvn("",0);
2231 qsort(cp, i, sizeof(U8*), utf8compare);
2232 for (j = 0; j < i; j++) {
2234 UV val = utf8_to_uv(s, &ulen);
2236 diff = val - nextmin;
2238 t = uv_to_utf8(tmpbuf,nextmin);
2239 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2241 t = uv_to_utf8(tmpbuf, val - 1);
2242 sv_catpvn(transv, "\377", 1);
2243 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2247 val = utf8_to_uv(s+1, &ulen);
2251 t = uv_to_utf8(tmpbuf,nextmin);
2252 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2253 t = uv_to_utf8(tmpbuf, 0x7fffffff);
2254 sv_catpvn(transv, "\377", 1);
2255 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2256 t = (U8*)SvPVX(transv);
2257 tlen = SvCUR(transv);
2260 else if (!rlen && !del) {
2261 r = t; rlen = tlen; rend = tend;
2264 if (to_utf && from_utf) { /* only counting characters */
2265 if (t == r || (tlen == rlen && memEQ(t, r, tlen)))
2266 o->op_private |= OPpTRANS_IDENTICAL;
2268 else { /* straight latin-1 translation */
2269 if (tlen == 4 && memEQ(t, "\0\377\303\277", 4) &&
2270 rlen == 4 && memEQ(r, "\0\377\303\277", 4))
2271 o->op_private |= OPpTRANS_IDENTICAL;
2275 while (t < tend || tfirst <= tlast) {
2276 /* see if we need more "t" chars */
2277 if (tfirst > tlast) {
2278 tfirst = (I32)utf8_to_uv(t, &ulen);
2280 if (t < tend && *t == 0xff) { /* illegal utf8 val indicates range */
2281 tlast = (I32)utf8_to_uv(++t, &ulen);
2288 /* now see if we need more "r" chars */
2289 if (rfirst > rlast) {
2291 rfirst = (I32)utf8_to_uv(r, &ulen);
2293 if (r < rend && *r == 0xff) { /* illegal utf8 val indicates range */
2294 rlast = (I32)utf8_to_uv(++r, &ulen);
2303 rfirst = rlast = 0xffffffff;
2307 /* now see which range will peter our first, if either. */
2308 tdiff = tlast - tfirst;
2309 rdiff = rlast - rfirst;
2316 if (rfirst == 0xffffffff) {
2317 diff = tdiff; /* oops, pretend rdiff is infinite */
2319 sv_catpvf(listsv, "%04x\t%04x\tXXXX\n", tfirst, tlast);
2321 sv_catpvf(listsv, "%04x\t\tXXXX\n", tfirst);
2325 sv_catpvf(listsv, "%04x\t%04x\t%04x\n", tfirst, tfirst + diff, rfirst);
2327 sv_catpvf(listsv, "%04x\t\t%04x\n", tfirst, rfirst);
2329 if (rfirst + diff > max)
2330 max = rfirst + diff;
2335 else if (rfirst <= 0x800)
2336 grows |= (tfirst < 0x80);
2337 else if (rfirst <= 0x10000)
2338 grows |= (tfirst < 0x800);
2339 else if (rfirst <= 0x200000)
2340 grows |= (tfirst < 0x10000);
2341 else if (rfirst <= 0x4000000)
2342 grows |= (tfirst < 0x200000);
2343 else if (rfirst <= 0x80000000)
2344 grows |= (tfirst < 0x4000000);
2356 else if (max > 0xff)
2361 cSVOPo->op_sv = (SV*)swash_init("utf8", "", listsv, bits, none);
2362 SvREFCNT_dec(listsv);
2364 SvREFCNT_dec(transv);
2366 if (!del && havefinal)
2367 (void)hv_store((HV*)SvRV((cSVOPo->op_sv)), "FINAL", 5, newSViv((IV)final), 0);
2369 if (grows && to_utf)
2370 o->op_private |= OPpTRANS_GROWS;
2377 tbl = (short*)cPVOPo->op_pv;
2379 Zero(tbl, 256, short);
2380 for (i = 0; i < tlen; i++)
2382 for (i = 0, j = 0; i < 256; i++) {
2398 if (!rlen && !del) {
2401 o->op_private |= OPpTRANS_IDENTICAL;
2403 for (i = 0; i < 256; i++)
2405 for (i = 0, j = 0; i < tlen; i++,j++) {
2408 if (tbl[t[i]] == -1)
2414 if (tbl[t[i]] == -1)
2425 newPMOP(I32 type, I32 flags)
2430 Newz(1101, pmop, 1, PMOP);
2431 pmop->op_type = type;
2432 pmop->op_ppaddr = PL_ppaddr[type];
2433 pmop->op_flags = flags;
2434 pmop->op_private = 0 | (flags >> 8);
2436 if (PL_hints & HINT_RE_TAINT)
2437 pmop->op_pmpermflags |= PMf_RETAINT;
2438 if (PL_hints & HINT_LOCALE)
2439 pmop->op_pmpermflags |= PMf_LOCALE;
2440 pmop->op_pmflags = pmop->op_pmpermflags;
2442 /* link into pm list */
2443 if (type != OP_TRANS && PL_curstash) {
2444 pmop->op_pmnext = HvPMROOT(PL_curstash);
2445 HvPMROOT(PL_curstash) = pmop;
2452 pmruntime(OP *o, OP *expr, OP *repl)
2457 I32 repl_has_vars = 0;
2459 if (o->op_type == OP_TRANS)
2460 return pmtrans(o, expr, repl);
2462 PL_hints |= HINT_BLOCK_SCOPE;
2465 if (expr->op_type == OP_CONST) {
2467 SV *pat = ((SVOP*)expr)->op_sv;
2468 char *p = SvPV(pat, plen);
2469 if ((o->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
2470 sv_setpvn(pat, "\\s+", 3);
2471 p = SvPV(pat, plen);
2472 pm->op_pmflags |= PMf_SKIPWHITE;
2474 pm->op_pmregexp = CALLREGCOMP(p, p + plen, pm);
2475 if (strEQ("\\s+", pm->op_pmregexp->precomp))
2476 pm->op_pmflags |= PMf_WHITE;
2480 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
2481 expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
2483 : OP_REGCMAYBE),0,expr);
2485 Newz(1101, rcop, 1, LOGOP);
2486 rcop->op_type = OP_REGCOMP;
2487 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
2488 rcop->op_first = scalar(expr);
2489 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL)
2490 ? (OPf_SPECIAL | OPf_KIDS)
2492 rcop->op_private = 1;
2495 /* establish postfix order */
2496 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
2498 rcop->op_next = expr;
2499 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2502 rcop->op_next = LINKLIST(expr);
2503 expr->op_next = (OP*)rcop;
2506 prepend_elem(o->op_type, scalar((OP*)rcop), o);
2511 if (pm->op_pmflags & PMf_EVAL) {
2513 if (PL_curcop->cop_line < PL_multi_end)
2514 PL_curcop->cop_line = PL_multi_end;
2517 else if (repl->op_type == OP_THREADSV
2518 && strchr("&`'123456789+",
2519 PL_threadsv_names[repl->op_targ]))
2523 #endif /* USE_THREADS */
2524 else if (repl->op_type == OP_CONST)
2528 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
2529 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
2531 if (curop->op_type == OP_THREADSV) {
2533 if (strchr("&`'123456789+", curop->op_private))
2537 if (curop->op_type == OP_GV) {
2538 GV *gv = ((GVOP*)curop)->op_gv;
2540 if (strchr("&`'123456789+", *GvENAME(gv)))
2543 #endif /* USE_THREADS */
2544 else if (curop->op_type == OP_RV2CV)
2546 else if (curop->op_type == OP_RV2SV ||
2547 curop->op_type == OP_RV2AV ||
2548 curop->op_type == OP_RV2HV ||
2549 curop->op_type == OP_RV2GV) {
2550 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2553 else if (curop->op_type == OP_PADSV ||
2554 curop->op_type == OP_PADAV ||
2555 curop->op_type == OP_PADHV ||
2556 curop->op_type == OP_PADANY) {
2559 else if (curop->op_type == OP_PUSHRE)
2560 ; /* Okay here, dangerous in newASSIGNOP */
2569 && (!pm->op_pmregexp
2570 || pm->op_pmregexp->reganch & ROPT_EVAL_SEEN))) {
2571 pm->op_pmflags |= PMf_CONST; /* const for long enough */
2572 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
2573 prepend_elem(o->op_type, scalar(repl), o);
2576 if (curop == repl && !pm->op_pmregexp) { /* Has variables. */
2577 pm->op_pmflags |= PMf_MAYBE_CONST;
2578 pm->op_pmpermflags |= PMf_MAYBE_CONST;
2580 Newz(1101, rcop, 1, LOGOP);
2581 rcop->op_type = OP_SUBSTCONT;
2582 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
2583 rcop->op_first = scalar(repl);
2584 rcop->op_flags |= OPf_KIDS;
2585 rcop->op_private = 1;
2588 /* establish postfix order */
2589 rcop->op_next = LINKLIST(repl);
2590 repl->op_next = (OP*)rcop;
2592 pm->op_pmreplroot = scalar((OP*)rcop);
2593 pm->op_pmreplstart = LINKLIST(rcop);
2602 newSVOP(I32 type, I32 flags, SV *sv)
2605 Newz(1101, svop, 1, SVOP);
2606 svop->op_type = type;
2607 svop->op_ppaddr = PL_ppaddr[type];
2609 svop->op_next = (OP*)svop;
2610 svop->op_flags = flags;
2611 if (PL_opargs[type] & OA_RETSCALAR)
2613 if (PL_opargs[type] & OA_TARGET)
2614 svop->op_targ = pad_alloc(type, SVs_PADTMP);
2615 return CHECKOP(type, svop);
2619 newGVOP(I32 type, I32 flags, GV *gv)
2623 Newz(1101, gvop, 1, GVOP);
2624 gvop->op_type = type;
2625 gvop->op_ppaddr = PL_ppaddr[type];
2626 gvop->op_gv = (GV*)SvREFCNT_inc(gv);
2627 gvop->op_next = (OP*)gvop;
2628 gvop->op_flags = flags;
2629 if (PL_opargs[type] & OA_RETSCALAR)
2631 if (PL_opargs[type] & OA_TARGET)
2632 gvop->op_targ = pad_alloc(type, SVs_PADTMP);
2633 return CHECKOP(type, gvop);
2637 newPVOP(I32 type, I32 flags, char *pv)
2640 Newz(1101, pvop, 1, PVOP);
2641 pvop->op_type = type;
2642 pvop->op_ppaddr = PL_ppaddr[type];
2644 pvop->op_next = (OP*)pvop;
2645 pvop->op_flags = flags;
2646 if (PL_opargs[type] & OA_RETSCALAR)
2648 if (PL_opargs[type] & OA_TARGET)
2649 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
2650 return CHECKOP(type, pvop);
2659 save_hptr(&PL_curstash);
2660 save_item(PL_curstname);
2665 name = SvPV(sv, len);
2666 PL_curstash = gv_stashpvn(name,len,TRUE);
2667 sv_setpvn(PL_curstname, name, len);
2671 sv_setpv(PL_curstname,"<none>");
2672 PL_curstash = Nullhv;
2674 PL_hints |= HINT_BLOCK_SCOPE;
2675 PL_copline = NOLINE;
2680 utilize(int aver, I32 floor, OP *version, OP *id, OP *arg)
2689 if (id->op_type != OP_CONST)
2690 croak("Module name must be constant");
2694 if(version != Nullop) {
2695 SV *vesv = ((SVOP*)version)->op_sv;
2697 if (arg == Nullop && !SvNIOK(vesv)) {
2704 if (version->op_type != OP_CONST || !SvNIOK(vesv))
2705 croak("Version number must be constant number");
2707 /* Make copy of id so we don't free it twice */
2708 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2710 /* Fake up a method call to VERSION */
2711 meth = newSVOP(OP_CONST, 0, newSVpvn("VERSION", 7));
2712 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2713 append_elem(OP_LIST,
2714 prepend_elem(OP_LIST, pack, list(version)),
2715 newUNOP(OP_METHOD, 0, meth)));
2719 /* Fake up an import/unimport */
2720 if (arg && arg->op_type == OP_STUB)
2721 imop = arg; /* no import on explicit () */
2722 else if(SvNIOK(((SVOP*)id)->op_sv)) {
2723 imop = Nullop; /* use 5.0; */
2726 /* Make copy of id so we don't free it twice */
2727 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2728 meth = newSVOP(OP_CONST, 0,
2730 ? newSVpvn("import", 6)
2731 : newSVpvn("unimport", 8)
2733 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2734 append_elem(OP_LIST,
2735 prepend_elem(OP_LIST, pack, list(arg)),
2736 newUNOP(OP_METHOD, 0, meth)));
2739 /* Fake up a require, handle override, if any */
2740 gv = gv_fetchpv("require", FALSE, SVt_PVCV);
2741 if (!(gv && GvIMPORTED_CV(gv)))
2742 gv = gv_fetchpv("CORE::GLOBAL::require", FALSE, SVt_PVCV);
2744 if (gv && GvIMPORTED_CV(gv)) {
2745 rqop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
2746 append_elem(OP_LIST, id,
2747 scalar(newUNOP(OP_RV2CV, 0,
2752 rqop = newUNOP(OP_REQUIRE, 0, id);
2755 /* Fake up the BEGIN {}, which does its thing immediately. */
2757 newSVOP(OP_CONST, 0, newSVpvn("BEGIN", 5)),
2759 append_elem(OP_LINESEQ,
2760 append_elem(OP_LINESEQ,
2761 newSTATEOP(0, Nullch, rqop),
2762 newSTATEOP(0, Nullch, veop)),
2763 newSTATEOP(0, Nullch, imop) ));
2765 PL_copline = NOLINE;
2775 gv = gv_fetchpv("do", FALSE, SVt_PVCV);
2776 if (!(gv && GvIMPORTED_CV(gv)))
2777 gv = gv_fetchpv("CORE::GLOBAL::do", FALSE, SVt_PVCV);
2779 if (gv && GvIMPORTED_CV(gv)) {
2780 doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
2781 append_elem(OP_LIST, term,
2782 scalar(newUNOP(OP_RV2CV, 0,
2787 doop = newUNOP(OP_DOFILE, 0, scalar(term));
2793 newSLICEOP(I32 flags, OP *subscript, OP *listval)
2795 return newBINOP(OP_LSLICE, flags,
2796 list(force_list(subscript)),
2797 list(force_list(listval)) );
2801 list_assignment(register OP *o)
2806 if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
2807 o = cUNOPo->op_first;
2809 if (o->op_type == OP_COND_EXPR) {
2810 I32 t = list_assignment(cCONDOPo->op_first->op_sibling);
2811 I32 f = list_assignment(cCONDOPo->op_first->op_sibling->op_sibling);
2816 yyerror("Assignment to both a list and a scalar");
2820 if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
2821 o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
2822 o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
2825 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
2828 if (o->op_type == OP_RV2SV)
2835 newASSIGNOP(I32 flags, OP *left, I32 optype, OP *right)
2840 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
2841 return newLOGOP(optype, 0,
2842 mod(scalar(left), optype),
2843 newUNOP(OP_SASSIGN, 0, scalar(right)));
2846 return newBINOP(optype, OPf_STACKED,
2847 mod(scalar(left), optype), scalar(right));
2851 if (list_assignment(left)) {
2854 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
2855 left = mod(left, OP_AASSIGN);
2863 o = newBINOP(OP_AASSIGN, flags,
2864 list(force_list(right)),
2865 list(force_list(left)) );
2866 o->op_private = 0 | (flags >> 8);
2867 if (!(left->op_private & OPpLVAL_INTRO)) {
2871 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
2872 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
2873 if (curop->op_type == OP_GV) {
2874 GV *gv = ((GVOP*)curop)->op_gv;
2875 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
2877 SvCUR(gv) = PL_generation;
2879 else if (curop->op_type == OP_PADSV ||
2880 curop->op_type == OP_PADAV ||
2881 curop->op_type == OP_PADHV ||
2882 curop->op_type == OP_PADANY) {
2883 SV **svp = AvARRAY(PL_comppad_name);
2884 SV *sv = svp[curop->op_targ];
2885 if (SvCUR(sv) == PL_generation)
2887 SvCUR(sv) = PL_generation; /* (SvCUR not used any more) */
2889 else if (curop->op_type == OP_RV2CV)
2891 else if (curop->op_type == OP_RV2SV ||
2892 curop->op_type == OP_RV2AV ||
2893 curop->op_type == OP_RV2HV ||
2894 curop->op_type == OP_RV2GV) {
2895 if (lastop->op_type != OP_GV) /* funny deref? */
2898 else if (curop->op_type == OP_PUSHRE) {
2899 if (((PMOP*)curop)->op_pmreplroot) {
2900 GV *gv = (GV*)((PMOP*)curop)->op_pmreplroot;
2901 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
2903 SvCUR(gv) = PL_generation;
2912 o->op_private = OPpASSIGN_COMMON;
2914 if (right && right->op_type == OP_SPLIT) {
2916 if ((tmpop = ((LISTOP*)right)->op_first) &&
2917 tmpop->op_type == OP_PUSHRE)
2919 PMOP *pm = (PMOP*)tmpop;
2920 if (left->op_type == OP_RV2AV &&
2921 !(left->op_private & OPpLVAL_INTRO) &&
2922 !(o->op_private & OPpASSIGN_COMMON) )
2924 tmpop = ((UNOP*)left)->op_first;
2925 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
2926 pm->op_pmreplroot = (OP*)((GVOP*)tmpop)->op_gv;
2927 pm->op_pmflags |= PMf_ONCE;
2928 tmpop = cUNOPo->op_first; /* to list (nulled) */
2929 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
2930 tmpop->op_sibling = Nullop; /* don't free split */
2931 right->op_next = tmpop->op_next; /* fix starting loc */
2932 op_free(o); /* blow off assign */
2933 right->op_flags &= ~OPf_WANT;
2934 /* "I don't know and I don't care." */
2939 if (PL_modcount < 10000 &&
2940 ((LISTOP*)right)->op_last->op_type == OP_CONST)
2942 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
2944 sv_setiv(sv, PL_modcount+1);
2952 right = newOP(OP_UNDEF, 0);
2953 if (right->op_type == OP_READLINE) {
2954 right->op_flags |= OPf_STACKED;
2955 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
2958 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
2959 o = newBINOP(OP_SASSIGN, flags,
2960 scalar(right), mod(scalar(left), OP_SASSIGN) );
2972 newSTATEOP(I32 flags, char *label, OP *o)
2975 U32 seq = intro_my();
2978 Newz(1101, cop, 1, COP);
2979 if (PERLDB_LINE && PL_curcop->cop_line && PL_curstash != PL_debstash) {
2980 cop->op_type = OP_DBSTATE;
2981 cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
2984 cop->op_type = OP_NEXTSTATE;
2985 cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
2987 cop->op_flags = flags;
2988 cop->op_private = (PL_hints & HINT_UTF8);
2990 cop->op_private |= NATIVE_HINTS;
2992 PL_compiling.op_private = cop->op_private;
2993 cop->op_next = (OP*)cop;
2996 cop->cop_label = label;
2997 PL_hints |= HINT_BLOCK_SCOPE;
3000 cop->cop_arybase = PL_curcop->cop_arybase;
3001 if (PL_curcop->cop_warnings == WARN_NONE
3002 || PL_curcop->cop_warnings == WARN_ALL)
3003 cop->cop_warnings = PL_curcop->cop_warnings ;
3005 cop->cop_warnings = newSVsv(PL_curcop->cop_warnings) ;
3008 if (PL_copline == NOLINE)
3009 cop->cop_line = PL_curcop->cop_line;
3011 cop->cop_line = PL_copline;
3012 PL_copline = NOLINE;
3014 cop->cop_filegv = (GV*)SvREFCNT_inc(PL_curcop->cop_filegv);
3015 cop->cop_stash = PL_curstash;
3017 if (PERLDB_LINE && PL_curstash != PL_debstash) {
3018 SV **svp = av_fetch(GvAV(PL_curcop->cop_filegv),(I32)cop->cop_line, FALSE);
3019 if (svp && *svp != &PL_sv_undef && !SvIOK(*svp)) {
3020 (void)SvIOK_on(*svp);
3022 SvSTASH(*svp) = (HV*)cop;
3026 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
3029 /* "Introduce" my variables to visible status. */
3037 if (! PL_min_intro_pending)
3038 return PL_cop_seqmax;
3040 svp = AvARRAY(PL_comppad_name);
3041 for (i = PL_min_intro_pending; i <= PL_max_intro_pending; i++) {
3042 if ((sv = svp[i]) && sv != &PL_sv_undef && !SvIVX(sv)) {
3043 SvIVX(sv) = PAD_MAX; /* Don't know scope end yet. */
3044 SvNVX(sv) = (double)PL_cop_seqmax;
3047 PL_min_intro_pending = 0;
3048 PL_comppad_name_fill = PL_max_intro_pending; /* Needn't search higher */
3049 return PL_cop_seqmax++;
3053 newLOGOP(I32 type, I32 flags, OP *first, OP *other)
3055 return new_logop(type, flags, &first, &other);
3059 new_logop(I32 type, I32 flags, OP** firstp, OP** otherp)
3064 OP *first = *firstp;
3065 OP *other = *otherp;
3067 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
3068 return newBINOP(type, flags, scalar(first), scalar(other));
3070 scalarboolean(first);
3071 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
3072 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
3073 if (type == OP_AND || type == OP_OR) {
3079 first = *firstp = cUNOPo->op_first;
3081 first->op_next = o->op_next;
3082 cUNOPo->op_first = Nullop;
3086 if (first->op_type == OP_CONST) {
3087 if (ckWARN(WARN_PRECEDENCE) && (first->op_private & OPpCONST_BARE))
3088 warner(WARN_PRECEDENCE, "Probable precedence problem on %s",
3090 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
3101 else if (first->op_type == OP_WANTARRAY) {
3107 else if (ckWARN(WARN_UNSAFE) && (first->op_flags & OPf_KIDS)) {
3108 OP *k1 = ((UNOP*)first)->op_first;
3109 OP *k2 = k1->op_sibling;
3111 switch (first->op_type)
3114 if (k2 && k2->op_type == OP_READLINE
3115 && (k2->op_flags & OPf_STACKED)
3116 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3117 warnop = k2->op_type;
3121 if (k1->op_type == OP_READDIR
3122 || k1->op_type == OP_GLOB
3123 || k1->op_type == OP_EACH)
3124 warnop = k1->op_type;
3128 line_t oldline = PL_curcop->cop_line;
3129 PL_curcop->cop_line = PL_copline;
3131 "Value of %s%s can be \"0\"; test with defined()",
3133 ((warnop == OP_READLINE || warnop == OP_GLOB)
3134 ? " construct" : "() operator"));
3135 PL_curcop->cop_line = oldline;
3142 if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
3143 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
3145 Newz(1101, logop, 1, LOGOP);
3147 logop->op_type = type;
3148 logop->op_ppaddr = PL_ppaddr[type];
3149 logop->op_first = first;
3150 logop->op_flags = flags | OPf_KIDS;
3151 logop->op_other = LINKLIST(other);
3152 logop->op_private = 1 | (flags >> 8);
3154 /* establish postfix order */
3155 logop->op_next = LINKLIST(first);
3156 first->op_next = (OP*)logop;
3157 first->op_sibling = other;
3159 o = newUNOP(OP_NULL, 0, (OP*)logop);
3166 newCONDOP(I32 flags, OP *first, OP *trueop, OP *falseop)
3173 return newLOGOP(OP_AND, 0, first, trueop);
3175 return newLOGOP(OP_OR, 0, first, falseop);
3177 scalarboolean(first);
3178 if (first->op_type == OP_CONST) {
3179 if (SvTRUE(((SVOP*)first)->op_sv)) {
3190 else if (first->op_type == OP_WANTARRAY) {
3194 Newz(1101, condop, 1, CONDOP);
3196 condop->op_type = OP_COND_EXPR;
3197 condop->op_ppaddr = PL_ppaddr[OP_COND_EXPR];
3198 condop->op_first = first;
3199 condop->op_flags = flags | OPf_KIDS;
3200 condop->op_true = LINKLIST(trueop);
3201 condop->op_false = LINKLIST(falseop);
3202 condop->op_private = 1 | (flags >> 8);
3204 /* establish postfix order */
3205 condop->op_next = LINKLIST(first);
3206 first->op_next = (OP*)condop;
3208 first->op_sibling = trueop;
3209 trueop->op_sibling = falseop;
3210 o = newUNOP(OP_NULL, 0, (OP*)condop);
3212 trueop->op_next = o;
3213 falseop->op_next = o;
3219 newRANGE(I32 flags, OP *left, OP *right)
3227 Newz(1101, condop, 1, CONDOP);
3229 condop->op_type = OP_RANGE;
3230 condop->op_ppaddr = PL_ppaddr[OP_RANGE];
3231 condop->op_first = left;
3232 condop->op_flags = OPf_KIDS;
3233 condop->op_true = LINKLIST(left);
3234 condop->op_false = LINKLIST(right);
3235 condop->op_private = 1 | (flags >> 8);
3237 left->op_sibling = right;
3239 condop->op_next = (OP*)condop;
3240 flip = newUNOP(OP_FLIP, flags, (OP*)condop);
3241 flop = newUNOP(OP_FLOP, 0, flip);
3242 o = newUNOP(OP_NULL, 0, flop);
3245 left->op_next = flip;
3246 right->op_next = flop;
3248 condop->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3249 sv_upgrade(PAD_SV(condop->op_targ), SVt_PVNV);
3250 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3251 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
3253 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3254 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3257 if (!flip->op_private || !flop->op_private)
3258 linklist(o); /* blow off optimizer unless constant */
3264 newLOOPOP(I32 flags, I32 debuggable, OP *expr, OP *block)
3269 int once = block && block->op_flags & OPf_SPECIAL &&
3270 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
3273 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
3274 return block; /* do {} while 0 does once */
3275 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3276 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
3277 expr = newUNOP(OP_DEFINED, 0,
3278 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3279 } else if (expr->op_flags & OPf_KIDS) {
3280 OP *k1 = ((UNOP*)expr)->op_first;
3281 OP *k2 = (k1) ? k1->op_sibling : NULL;
3282 switch (expr->op_type) {
3284 if (k2 && k2->op_type == OP_READLINE
3285 && (k2->op_flags & OPf_STACKED)
3286 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3287 expr = newUNOP(OP_DEFINED, 0, expr);
3291 if (k1->op_type == OP_READDIR
3292 || k1->op_type == OP_GLOB
3293 || k1->op_type == OP_EACH)
3294 expr = newUNOP(OP_DEFINED, 0, expr);
3300 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
3301 o = new_logop(OP_AND, 0, &expr, &listop);
3304 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
3306 if (once && o != listop)
3307 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
3310 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
3312 o->op_flags |= flags;
3314 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
3319 newWHILEOP(I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *block, OP *cont)
3328 if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3329 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB))) {
3330 expr = newUNOP(OP_DEFINED, 0,
3331 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3332 } else if (expr && (expr->op_flags & OPf_KIDS)) {
3333 OP *k1 = ((UNOP*)expr)->op_first;
3334 OP *k2 = (k1) ? k1->op_sibling : NULL;
3335 switch (expr->op_type) {
3337 if (k2 && k2->op_type == OP_READLINE
3338 && (k2->op_flags & OPf_STACKED)
3339 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3340 expr = newUNOP(OP_DEFINED, 0, expr);
3344 if (k1->op_type == OP_READDIR
3345 || k1->op_type == OP_GLOB
3346 || k1->op_type == OP_EACH)
3347 expr = newUNOP(OP_DEFINED, 0, expr);
3353 block = newOP(OP_NULL, 0);
3356 next = LINKLIST(cont);
3358 cont = append_elem(OP_LINESEQ, cont, newOP(OP_UNSTACK, 0));
3359 if ((line_t)whileline != NOLINE) {
3360 PL_copline = whileline;
3361 cont = append_elem(OP_LINESEQ, cont,
3362 newSTATEOP(0, Nullch, Nullop));
3366 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
3367 redo = LINKLIST(listop);
3370 PL_copline = whileline;
3372 o = new_logop(OP_AND, 0, &expr, &listop);
3373 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
3374 op_free(expr); /* oops, it's a while (0) */
3376 return Nullop; /* listop already freed by new_logop */
3379 ((LISTOP*)listop)->op_last->op_next = condop =
3380 (o == listop ? redo : LINKLIST(o));
3388 Newz(1101,loop,1,LOOP);
3389 loop->op_type = OP_ENTERLOOP;
3390 loop->op_ppaddr = PL_ppaddr[OP_ENTERLOOP];
3391 loop->op_private = 0;
3392 loop->op_next = (OP*)loop;
3395 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
3397 loop->op_redoop = redo;
3398 loop->op_lastop = o;
3401 loop->op_nextop = next;
3403 loop->op_nextop = o;
3405 o->op_flags |= flags;
3406 o->op_private |= (flags >> 8);
3411 newFOROP(I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
3419 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
3420 sv->op_type = OP_RV2GV;
3421 sv->op_ppaddr = PL_ppaddr[OP_RV2GV];
3423 else if (sv->op_type == OP_PADSV) { /* private variable */
3424 padoff = sv->op_targ;
3428 else if (sv->op_type == OP_THREADSV) { /* per-thread variable */
3429 padoff = sv->op_targ;
3430 iterflags |= OPf_SPECIAL;
3435 croak("Can't use %s for loop variable", PL_op_desc[sv->op_type]);
3439 padoff = find_threadsv("_");
3440 iterflags |= OPf_SPECIAL;
3442 sv = newGVOP(OP_GV, 0, PL_defgv);
3445 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
3446 expr = mod(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART);
3447 iterflags |= OPf_STACKED;
3449 else if (expr->op_type == OP_NULL &&
3450 (expr->op_flags & OPf_KIDS) &&
3451 ((BINOP*)expr)->op_first->op_type == OP_FLOP)
3453 /* Basically turn for($x..$y) into the same as for($x,$y), but we
3454 * set the STACKED flag to indicate that these values are to be
3455 * treated as min/max values by 'pp_iterinit'.
3457 UNOP* flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
3458 CONDOP* range = (CONDOP*) flip->op_first;
3459 OP* left = range->op_first;
3460 OP* right = left->op_sibling;
3463 range->op_flags &= ~OPf_KIDS;
3464 range->op_first = Nullop;
3466 listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
3467 listop->op_first->op_next = range->op_true;
3468 left->op_next = range->op_false;
3469 right->op_next = (OP*)listop;
3470 listop->op_next = listop->op_first;
3473 expr = (OP*)(listop);
3475 iterflags |= OPf_STACKED;
3478 expr = mod(force_list(expr), OP_GREPSTART);
3482 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
3483 append_elem(OP_LIST, expr, scalar(sv))));
3484 assert(!loop->op_next);
3485 Renew(loop, 1, LOOP);
3486 loop->op_targ = padoff;
3487 wop = newWHILEOP(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont);
3488 PL_copline = forline;
3489 return newSTATEOP(0, label, wop);
3493 newLOOPEX(I32 type, OP *label)
3499 if (type != OP_GOTO || label->op_type == OP_CONST) {
3500 /* "last()" means "last" */
3501 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS))
3502 o = newOP(type, OPf_SPECIAL);
3504 o = newPVOP(type, 0, savepv(label->op_type == OP_CONST
3505 ? SvPVx(((SVOP*)label)->op_sv, n_a)
3511 if (label->op_type == OP_ENTERSUB)
3512 label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
3513 o = newUNOP(type, OPf_STACKED, label);
3515 PL_hints |= HINT_BLOCK_SCOPE;
3525 MUTEX_DESTROY(CvMUTEXP(cv));
3526 Safefree(CvMUTEXP(cv));
3529 #endif /* USE_THREADS */
3531 if (!CvXSUB(cv) && CvROOT(cv)) {
3533 if (CvDEPTH(cv) || (CvOWNER(cv) && CvOWNER(cv) != thr))
3534 croak("Can't undef active subroutine");
3537 croak("Can't undef active subroutine");
3538 #endif /* USE_THREADS */
3541 SAVESPTR(PL_curpad);
3545 op_free(CvROOT(cv));
3546 CvROOT(cv) = Nullop;
3549 SvPOK_off((SV*)cv); /* forget prototype */
3551 SvREFCNT_dec(CvGV(cv));
3553 SvREFCNT_dec(CvOUTSIDE(cv));
3554 CvOUTSIDE(cv) = Nullcv;
3555 if (CvPADLIST(cv)) {
3556 /* may be during global destruction */
3557 if (SvREFCNT(CvPADLIST(cv))) {
3558 I32 i = AvFILLp(CvPADLIST(cv));
3560 SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE);
3561 SV* sv = svp ? *svp : Nullsv;
3564 if (sv == (SV*)PL_comppad_name)
3565 PL_comppad_name = Nullav;
3566 else if (sv == (SV*)PL_comppad) {
3567 PL_comppad = Nullav;
3568 PL_curpad = Null(SV**);
3572 SvREFCNT_dec((SV*)CvPADLIST(cv));
3574 CvPADLIST(cv) = Nullav;
3578 #ifdef DEBUG_CLOSURES
3583 CV *outside = CvOUTSIDE(cv);
3584 AV* padlist = CvPADLIST(cv);
3591 PerlIO_printf(Perl_debug_log, "\tCV=0x%lx (%s), OUTSIDE=0x%lx (%s)\n",
3593 (CvANON(cv) ? "ANON"
3594 : (cv == PL_main_cv) ? "MAIN"
3595 : CvUNIQUE(cv) ? "UNIQUE"
3596 : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"),
3599 : CvANON(outside) ? "ANON"
3600 : (outside == PL_main_cv) ? "MAIN"
3601 : CvUNIQUE(outside) ? "UNIQUE"
3602 : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED"));
3607 pad_name = (AV*)*av_fetch(padlist, 0, FALSE);
3608 pad = (AV*)*av_fetch(padlist, 1, FALSE);
3609 pname = AvARRAY(pad_name);
3610 ppad = AvARRAY(pad);
3612 for (ix = 1; ix <= AvFILLp(pad_name); ix++) {
3613 if (SvPOK(pname[ix]))
3614 PerlIO_printf(Perl_debug_log, "\t%4d. 0x%lx (%s\"%s\" %ld-%ld)\n",
3616 SvFAKE(pname[ix]) ? "FAKE " : "",
3618 (long)I_32(SvNVX(pname[ix])),
3619 (long)SvIVX(pname[ix]));
3622 #endif /* DEBUG_CLOSURES */
3625 cv_clone2(CV *proto, CV *outside)
3630 AV* protopadlist = CvPADLIST(proto);
3631 AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE);
3632 AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE);
3633 SV** pname = AvARRAY(protopad_name);
3634 SV** ppad = AvARRAY(protopad);
3635 I32 fname = AvFILLp(protopad_name);
3636 I32 fpad = AvFILLp(protopad);
3640 assert(!CvUNIQUE(proto));
3643 SAVESPTR(PL_curpad);
3644 SAVESPTR(PL_comppad);
3645 SAVESPTR(PL_comppad_name);
3646 SAVESPTR(PL_compcv);
3648 cv = PL_compcv = (CV*)NEWSV(1104,0);
3649 sv_upgrade((SV *)cv, SvTYPE(proto));
3655 New(666, CvMUTEXP(cv), 1, perl_mutex);
3656 MUTEX_INIT(CvMUTEXP(cv));
3658 #endif /* USE_THREADS */
3659 CvFILEGV(cv) = CvFILEGV(proto);
3660 CvGV(cv) = (GV*)SvREFCNT_inc(CvGV(proto));
3661 CvSTASH(cv) = CvSTASH(proto);
3662 CvROOT(cv) = CvROOT(proto);
3663 CvSTART(cv) = CvSTART(proto);
3665 CvOUTSIDE(cv) = (CV*)SvREFCNT_inc(outside);
3668 sv_setpvn((SV*)cv, SvPVX(proto), SvCUR(proto));
3670 PL_comppad_name = newAV();
3671 for (ix = fname; ix >= 0; ix--)
3672 av_store(PL_comppad_name, ix, SvREFCNT_inc(pname[ix]));
3674 PL_comppad = newAV();
3676 comppadlist = newAV();
3677 AvREAL_off(comppadlist);
3678 av_store(comppadlist, 0, (SV*)PL_comppad_name);
3679 av_store(comppadlist, 1, (SV*)PL_comppad);
3680 CvPADLIST(cv) = comppadlist;
3681 av_fill(PL_comppad, AvFILLp(protopad));
3682 PL_curpad = AvARRAY(PL_comppad);
3684 av = newAV(); /* will be @_ */
3686 av_store(PL_comppad, 0, (SV*)av);
3687 AvFLAGS(av) = AVf_REIFY;
3689 for (ix = fpad; ix > 0; ix--) {
3690 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
3691 if (namesv && namesv != &PL_sv_undef) {
3692 char *name = SvPVX(namesv); /* XXX */
3693 if (SvFLAGS(namesv) & SVf_FAKE) { /* lexical from outside? */
3694 I32 off = pad_findlex(name, ix, SvIVX(namesv),
3695 CvOUTSIDE(cv), cxstack_ix, 0, 0);
3697 PL_curpad[ix] = SvREFCNT_inc(ppad[ix]);
3699 croak("panic: cv_clone: %s", name);
3701 else { /* our own lexical */
3704 /* anon code -- we'll come back for it */
3705 sv = SvREFCNT_inc(ppad[ix]);
3707 else if (*name == '@')
3709 else if (*name == '%')
3719 SV* sv = NEWSV(0,0);
3725 /* Now that vars are all in place, clone nested closures. */
3727 for (ix = fpad; ix > 0; ix--) {
3728 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
3730 && namesv != &PL_sv_undef
3731 && !(SvFLAGS(namesv) & SVf_FAKE)
3732 && *SvPVX(namesv) == '&'
3733 && CvCLONE(ppad[ix]))
3735 CV *kid = cv_clone2((CV*)ppad[ix], cv);
3736 SvREFCNT_dec(ppad[ix]);
3739 PL_curpad[ix] = (SV*)kid;
3743 #ifdef DEBUG_CLOSURES
3744 PerlIO_printf(Perl_debug_log, "Cloned inside:\n");
3746 PerlIO_printf(Perl_debug_log, " from:\n");
3748 PerlIO_printf(Perl_debug_log, " to:\n");
3760 MUTEX_LOCK(&PL_cred_mutex); /* XXX create separate mutex */
3761 cv = cv_clone2(proto, CvOUTSIDE(proto));
3762 MUTEX_UNLOCK(&PL_cred_mutex); /* XXX create separate mutex */
3767 cv_ckproto(CV *cv, GV *gv, char *p)
3769 if ((!p != !SvPOK(cv)) || (p && strNE(p, SvPVX(cv)))) {
3770 SV* msg = sv_newmortal();
3774 gv_efullname3(name = sv_newmortal(), gv, Nullch);
3775 sv_setpv(msg, "Prototype mismatch:");
3777 sv_catpvf(msg, " sub %_", name);
3779 sv_catpvf(msg, " (%s)", SvPVX(cv));
3780 sv_catpv(msg, " vs ");
3782 sv_catpvf(msg, "(%s)", p);
3784 sv_catpv(msg, "none");
3792 if (!cv || !SvPOK(cv) || SvCUR(cv))
3794 return op_const_sv(CvSTART(cv), cv);
3798 op_const_sv(OP *o, CV *cv)
3805 if(o->op_type == OP_LINESEQ && cLISTOPo->op_first)
3806 o = cLISTOPo->op_first->op_sibling;
3808 for (; o; o = o->op_next) {
3809 OPCODE type = o->op_type;
3811 if(sv && o->op_next == o)
3813 if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
3815 if (type == OP_LEAVESUB || type == OP_RETURN)
3819 if (type == OP_CONST)
3821 else if (type == OP_PADSV && cv) {
3822 AV* padav = (AV*)(AvARRAY(CvPADLIST(cv))[1]);
3823 sv = padav ? AvARRAY(padav)[o->op_targ] : Nullsv;
3824 if (!sv || (!SvREADONLY(sv) && SvREFCNT(sv) > 1))
3836 newSUB(I32 floor, OP *o, OP *proto, OP *block)
3840 char *name = o ? SvPVx(cSVOPo->op_sv, n_a) : Nullch;
3841 GV *gv = gv_fetchpv(name ? name : "__ANON__",
3842 GV_ADDMULTI | (block ? 0 : GV_NOINIT), SVt_PVCV);
3843 char *ps = proto ? SvPVx(((SVOP*)proto)->op_sv, n_a) : Nullch;
3852 if (SvTYPE(gv) != SVt_PVGV) { /* Prototype now, and had
3853 maximum a prototype before. */
3854 if (SvTYPE(gv) > SVt_NULL) {
3855 if (!SvPOK((SV*)gv) && !(SvIOK((SV*)gv) && SvIVX((SV*)gv) == -1))
3856 warn("Runaway prototype");
3857 cv_ckproto((CV*)gv, NULL, ps);
3860 sv_setpv((SV*)gv, ps);
3862 sv_setiv((SV*)gv, -1);
3863 SvREFCNT_dec(PL_compcv);
3864 cv = PL_compcv = NULL;
3865 PL_sub_generation++;
3869 if (!name || GvCVGEN(gv))
3871 else if (cv = GvCV(gv)) {
3872 cv_ckproto(cv, gv, ps);
3873 /* already defined (or promised)? */
3874 if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
3876 bool const_changed = TRUE;
3878 /* just a "sub foo;" when &foo is already defined */
3879 SAVEFREESV(PL_compcv);
3882 /* ahem, death to those who redefine active sort subs */
3883 if (PL_curstackinfo->si_type == PERLSI_SORT && PL_sortcop == CvSTART(cv))
3884 croak("Can't redefine active sort subroutine %s", name);
3885 if(const_sv = cv_const_sv(cv))
3886 const_changed = sv_cmp(const_sv, op_const_sv(block, Nullcv));
3887 if ((const_sv && const_changed) || ckWARN(WARN_REDEFINE)
3888 && !(CvGV(cv) && GvSTASH(CvGV(cv))
3889 && HvNAME(GvSTASH(CvGV(cv)))
3890 && strEQ(HvNAME(GvSTASH(CvGV(cv))),
3892 line_t oldline = PL_curcop->cop_line;
3893 PL_curcop->cop_line = PL_copline;
3894 warner(WARN_REDEFINE,
3895 const_sv ? "Constant subroutine %s redefined"
3896 : "Subroutine %s redefined", name);
3897 PL_curcop->cop_line = oldline;
3903 if (cv) { /* must reuse cv if autoloaded */
3905 CvFLAGS(cv) = CvFLAGS(PL_compcv);
3906 CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv);
3907 CvOUTSIDE(PL_compcv) = 0;
3908 CvPADLIST(cv) = CvPADLIST(PL_compcv);
3909 CvPADLIST(PL_compcv) = 0;
3910 if (SvREFCNT(PL_compcv) > 1) /* XXX Make closures transit through stub. */
3911 CvOUTSIDE(PL_compcv) = (CV*)SvREFCNT_inc((SV*)cv);
3912 SvREFCNT_dec(PL_compcv);
3919 PL_sub_generation++;
3922 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
3923 CvFILEGV(cv) = PL_curcop->cop_filegv;
3924 CvSTASH(cv) = PL_curstash;
3927 if (!CvMUTEXP(cv)) {
3928 New(666, CvMUTEXP(cv), 1, perl_mutex);
3929 MUTEX_INIT(CvMUTEXP(cv));
3931 #endif /* USE_THREADS */
3934 sv_setpv((SV*)cv, ps);
3936 if (PL_error_count) {
3940 char *s = strrchr(name, ':');
3942 if (strEQ(s, "BEGIN")) {
3944 "BEGIN not safe after errors--compilation aborted";
3948 /* force display of errors found but not reported */
3949 sv_catpv(ERRSV, not_safe);
3950 croak("%s", SvPVx(ERRSV, n_a));
3957 PL_copline = NOLINE;
3962 if (AvFILLp(PL_comppad_name) < AvFILLp(PL_comppad))
3963 av_store(PL_comppad_name, AvFILLp(PL_comppad), Nullsv);
3966 SV **namep = AvARRAY(PL_comppad_name);
3967 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
3970 if (SvIMMORTAL(PL_curpad[ix]))
3973 * The only things that a clonable function needs in its
3974 * pad are references to outer lexicals and anonymous subs.
3975 * The rest are created anew during cloning.
3977 if (!((namesv = namep[ix]) != Nullsv &&
3978 namesv != &PL_sv_undef &&
3980 *SvPVX(namesv) == '&')))
3982 SvREFCNT_dec(PL_curpad[ix]);
3983 PL_curpad[ix] = Nullsv;
3988 AV *av = newAV(); /* Will be @_ */
3990 av_store(PL_comppad, 0, (SV*)av);
3991 AvFLAGS(av) = AVf_REIFY;
3993 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
3994 if (SvIMMORTAL(PL_curpad[ix]))
3996 if (!SvPADMY(PL_curpad[ix]))
3997 SvPADTMP_on(PL_curpad[ix]);
4001 CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
4002 CvSTART(cv) = LINKLIST(CvROOT(cv));
4003 CvROOT(cv)->op_next = 0;
4009 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
4010 SV *sv = NEWSV(0,0);
4011 SV *tmpstr = sv_newmortal();
4012 GV *db_postponed = gv_fetchpv("DB::postponed", GV_ADDMULTI, SVt_PVHV);
4016 sv_setpvf(sv, "%_:%ld-%ld",
4017 GvSV(PL_curcop->cop_filegv),
4018 (long)PL_subline, (long)PL_curcop->cop_line);
4019 gv_efullname3(tmpstr, gv, Nullch);
4020 hv_store(GvHV(PL_DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0);
4021 hv = GvHVn(db_postponed);
4022 if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX(tmpstr), SvCUR(tmpstr))
4023 && (cv = GvCV(db_postponed))) {
4028 perl_call_sv((SV*)cv, G_DISCARD);
4032 if ((s = strrchr(name,':')))
4036 if (strEQ(s, "BEGIN")) {
4037 I32 oldscope = PL_scopestack_ix;
4039 SAVESPTR(PL_compiling.cop_filegv);
4040 SAVEI16(PL_compiling.cop_line);
4042 sv_setsv(PL_rs, PL_nrs);
4045 PL_beginav = newAV();
4046 DEBUG_x( dump_sub(gv) );
4047 av_push(PL_beginav, (SV *)cv);
4049 call_list(oldscope, PL_beginav);
4051 PL_curcop = &PL_compiling;
4052 PL_compiling.op_private = PL_hints;
4055 else if (strEQ(s, "END") && !PL_error_count) {
4058 av_unshift(PL_endav, 1);
4059 av_store(PL_endav, 0, (SV *)cv);
4062 else if (strEQ(s, "INIT") && !PL_error_count) {
4064 PL_initav = newAV();
4065 av_push(PL_initav, SvREFCNT_inc(cv));
4071 PL_copline = NOLINE;
4076 /* XXX unsafe for threads if eval_owner isn't held */
4078 newCONSTSUB(HV *stash, char *name, SV *sv)
4081 U32 oldhints = PL_hints;
4082 HV *old_cop_stash = PL_curcop->cop_stash;
4083 HV *old_curstash = PL_curstash;
4084 line_t oldline = PL_curcop->cop_line;
4085 PL_curcop->cop_line = PL_copline;
4087 PL_hints &= ~HINT_BLOCK_SCOPE;
4089 PL_curstash = PL_curcop->cop_stash = stash;
4092 start_subparse(FALSE, 0),
4093 newSVOP(OP_CONST, 0, newSVpv(name,0)),
4094 newSVOP(OP_CONST, 0, &PL_sv_no), /* SvPV(&PL_sv_no) == "" -- GMB */
4095 newSTATEOP(0, Nullch, newSVOP(OP_CONST, 0, sv))
4098 PL_hints = oldhints;
4099 PL_curcop->cop_stash = old_cop_stash;
4100 PL_curstash = old_curstash;
4101 PL_curcop->cop_line = oldline;
4105 newXS(char *name, void (*subaddr) (CV * _CPERLproto), char *filename)
4108 GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV);
4111 if (cv = (name ? GvCV(gv) : Nullcv)) {
4113 /* just a cached method */
4117 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
4118 /* already defined (or promised) */
4119 if (ckWARN(WARN_REDEFINE) && !(CvGV(cv) && GvSTASH(CvGV(cv))
4120 && HvNAME(GvSTASH(CvGV(cv)))
4121 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "autouse"))) {
4122 line_t oldline = PL_curcop->cop_line;
4123 if (PL_copline != NOLINE)
4124 PL_curcop->cop_line = PL_copline;
4125 warner(WARN_REDEFINE, "Subroutine %s redefined",name);
4126 PL_curcop->cop_line = oldline;
4133 if (cv) /* must reuse cv if autoloaded */
4136 cv = (CV*)NEWSV(1105,0);
4137 sv_upgrade((SV *)cv, SVt_PVCV);
4141 PL_sub_generation++;
4144 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4146 New(666, CvMUTEXP(cv), 1, perl_mutex);
4147 MUTEX_INIT(CvMUTEXP(cv));
4149 #endif /* USE_THREADS */
4150 CvFILEGV(cv) = gv_fetchfile(filename);
4151 CvXSUB(cv) = subaddr;
4154 char *s = strrchr(name,':');
4159 if (strEQ(s, "BEGIN")) {
4161 PL_beginav = newAV();
4162 av_push(PL_beginav, (SV *)cv);
4165 else if (strEQ(s, "END")) {
4168 av_unshift(PL_endav, 1);
4169 av_store(PL_endav, 0, (SV *)cv);
4172 else if (strEQ(s, "INIT")) {
4174 PL_initav = newAV();
4175 av_push(PL_initav, (SV *)cv);
4186 newFORM(I32 floor, OP *o, OP *block)
4196 name = SvPVx(cSVOPo->op_sv, n_a);
4199 gv = gv_fetchpv(name,TRUE, SVt_PVFM);
4201 if (cv = GvFORM(gv)) {
4202 if (ckWARN(WARN_REDEFINE)) {
4203 line_t oldline = PL_curcop->cop_line;
4205 PL_curcop->cop_line = PL_copline;
4206 warner(WARN_REDEFINE, "Format %s redefined",name);
4207 PL_curcop->cop_line = oldline;
4213 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4214 CvFILEGV(cv) = PL_curcop->cop_filegv;
4216 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4217 if (!SvPADMY(PL_curpad[ix]) && !SvIMMORTAL(PL_curpad[ix]))
4218 SvPADTMP_on(PL_curpad[ix]);
4221 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
4222 CvSTART(cv) = LINKLIST(CvROOT(cv));
4223 CvROOT(cv)->op_next = 0;
4226 PL_copline = NOLINE;
4233 return newUNOP(OP_REFGEN, 0,
4234 mod(list(convert(OP_ANONLIST, 0, o)), OP_REFGEN));
4240 return newUNOP(OP_REFGEN, 0,
4241 mod(list(convert(OP_ANONHASH, 0, o)), OP_REFGEN));
4245 newANONSUB(I32 floor, OP *proto, OP *block)
4247 return newUNOP(OP_REFGEN, 0,
4248 newSVOP(OP_ANONCODE, 0, (SV*)newSUB(floor, 0, proto, block)));
4254 switch (o->op_type) {
4256 o->op_type = OP_PADAV;
4257 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4258 return ref(o, OP_RV2AV);
4261 o->op_type = OP_RV2AV;
4262 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
4267 warn("oops: oopsAV");
4276 switch (o->op_type) {
4279 o->op_type = OP_PADHV;
4280 o->op_ppaddr = PL_ppaddr[OP_PADHV];
4281 return ref(o, OP_RV2HV);
4285 o->op_type = OP_RV2HV;
4286 o->op_ppaddr = PL_ppaddr[OP_RV2HV];
4291 warn("oops: oopsHV");
4300 if (o->op_type == OP_PADANY) {
4301 o->op_type = OP_PADAV;
4302 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4305 return newUNOP(OP_RV2AV, 0, scalar(o));
4309 newGVREF(I32 type, OP *o)
4311 if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
4312 return newUNOP(OP_NULL, 0, o);
4313 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
4319 if (o->op_type == OP_PADANY) {
4320 o->op_type = OP_PADHV;
4321 o->op_ppaddr = PL_ppaddr[OP_PADHV];
4324 return newUNOP(OP_RV2HV, 0, scalar(o));
4330 croak("NOT IMPL LINE %d",__LINE__);
4336 newCVREF(I32 flags, OP *o)
4338 return newUNOP(OP_RV2CV, flags, scalar(o));
4344 if (o->op_type == OP_PADANY) {
4345 o->op_type = OP_PADSV;
4346 o->op_ppaddr = PL_ppaddr[OP_PADSV];
4349 else if (o->op_type == OP_THREADSV && !(o->op_flags & OPpDONE_SVREF)) {
4350 o->op_flags |= OPpDONE_SVREF;
4353 return newUNOP(OP_RV2SV, 0, scalar(o));
4356 /* Check routines. */
4364 name = NEWSV(1106,0);
4365 sv_upgrade(name, SVt_PVNV);
4366 sv_setpvn(name, "&", 1);
4369 ix = pad_alloc(o->op_type, SVs_PADMY);
4370 av_store(PL_comppad_name, ix, name);
4371 av_store(PL_comppad, ix, cSVOPo->op_sv);
4372 SvPADMY_on(cSVOPo->op_sv);
4373 cSVOPo->op_sv = Nullsv;
4374 cSVOPo->op_targ = ix;
4381 o->op_private = PL_hints;
4388 if (cUNOPo->op_first->op_type == OP_CONCAT)
4389 o->op_flags |= OPf_STACKED;
4396 if (o->op_flags & OPf_KIDS) {
4399 OPCODE type = o->op_type;
4400 o = modkids(ck_fun(o), type);
4401 kid = cUNOPo->op_first;
4402 newop = kUNOP->op_first->op_sibling;
4404 (newop->op_sibling ||
4405 !(PL_opargs[newop->op_type] & OA_RETSCALAR) ||
4406 newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
4407 newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
4411 op_free(kUNOP->op_first);
4412 kUNOP->op_first = newop;
4414 o->op_ppaddr = PL_ppaddr[++o->op_type];
4423 if (o->op_flags & OPf_KIDS) {
4424 OP *kid = cUNOPo->op_first;
4425 if (kid->op_type == OP_HSLICE)
4426 o->op_private |= OPpSLICE;
4427 else if (kid->op_type != OP_HELEM)
4428 croak("%s argument is not a HASH element or slice",
4429 PL_op_desc[o->op_type]);
4438 I32 type = o->op_type;
4440 if (o->op_flags & OPf_KIDS) {
4441 if (cLISTOPo->op_first->op_type == OP_STUB) {
4443 o = newUNOP(type, OPf_SPECIAL,
4444 newGVOP(OP_GV, 0, gv_fetchpv("main::ARGV", TRUE, SVt_PVAV)));
4454 PL_hints |= HINT_BLOCK_SCOPE;
4455 if (o->op_flags & OPf_KIDS) {
4456 SVOP *kid = (SVOP*)cUNOPo->op_first;
4459 o->op_flags &= ~OPf_KIDS;
4462 else if (kid->op_type == OP_LINESEQ) {
4465 kid->op_next = o->op_next;
4466 cUNOPo->op_first = 0;
4469 Newz(1101, enter, 1, LOGOP);
4470 enter->op_type = OP_ENTERTRY;
4471 enter->op_ppaddr = PL_ppaddr[OP_ENTERTRY];
4472 enter->op_private = 0;
4474 /* establish postfix order */
4475 enter->op_next = (OP*)enter;
4477 o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
4478 o->op_type = OP_LEAVETRY;
4479 o->op_ppaddr = PL_ppaddr[OP_LEAVETRY];
4480 enter->op_other = o;
4488 o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP());
4490 o->op_targ = (PADOFFSET)PL_hints;
4498 if (o->op_flags & OPf_STACKED) {
4500 kid = cUNOPo->op_first->op_sibling;
4501 if (kid->op_type == OP_RV2GV)
4513 if (o->op_flags & OPf_KIDS) {
4514 OP *kid = cUNOPo->op_first;
4515 if (kid->op_type != OP_HELEM)
4516 croak("%s argument is not a HASH element", PL_op_desc[o->op_type]);
4524 ck_gvconst(register OP *o)
4526 o = fold_constants(o);
4527 if (o->op_type == OP_CONST)
4534 ck_rvconst(register OP *o)
4537 SVOP *kid = (SVOP*)cUNOPo->op_first;
4539 o->op_private |= (PL_hints & HINT_STRICT_REFS);
4540 if (kid->op_type == OP_CONST) {
4544 SV *kidsv = kid->op_sv;
4547 /* Is it a constant from cv_const_sv()? */
4548 if (SvROK(kidsv) && SvREADONLY(kidsv)) {
4549 SV *rsv = SvRV(kidsv);
4550 int svtype = SvTYPE(rsv);
4551 char *badtype = Nullch;
4553 switch (o->op_type) {
4555 if (svtype > SVt_PVMG)
4556 badtype = "a SCALAR";
4559 if (svtype != SVt_PVAV)
4560 badtype = "an ARRAY";
4563 if (svtype != SVt_PVHV) {
4564 if (svtype == SVt_PVAV) { /* pseudohash? */
4565 SV **ksv = av_fetch((AV*)rsv, 0, FALSE);
4566 if (ksv && SvROK(*ksv)
4567 && SvTYPE(SvRV(*ksv)) == SVt_PVHV)
4576 if (svtype != SVt_PVCV)
4581 croak("Constant is not %s reference", badtype);
4584 name = SvPV(kidsv, n_a);
4585 if ((PL_hints & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
4586 char *badthing = Nullch;
4587 switch (o->op_type) {
4589 badthing = "a SCALAR";
4592 badthing = "an ARRAY";
4595 badthing = "a HASH";
4600 "Can't use bareword (\"%s\") as %s ref while \"strict refs\" in use",
4604 * This is a little tricky. We only want to add the symbol if we
4605 * didn't add it in the lexer. Otherwise we get duplicate strict
4606 * warnings. But if we didn't add it in the lexer, we must at
4607 * least pretend like we wanted to add it even if it existed before,
4608 * or we get possible typo warnings. OPpCONST_ENTERED says
4609 * whether the lexer already added THIS instance of this symbol.
4611 iscv = (o->op_type == OP_RV2CV) * 2;
4613 gv = gv_fetchpv(name,
4614 iscv | !(kid->op_private & OPpCONST_ENTERED),
4617 : o->op_type == OP_RV2SV
4619 : o->op_type == OP_RV2AV
4621 : o->op_type == OP_RV2HV
4624 } while (!gv && !(kid->op_private & OPpCONST_ENTERED) && !iscv++);
4626 kid->op_type = OP_GV;
4627 SvREFCNT_dec(kid->op_sv);
4628 kid->op_sv = SvREFCNT_inc(gv);
4638 I32 type = o->op_type;
4640 if (o->op_flags & OPf_REF)
4643 if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
4644 SVOP *kid = (SVOP*)cUNOPo->op_first;
4646 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
4648 OP *newop = newGVOP(type, OPf_REF,
4649 gv_fetchpv(SvPVx(kid->op_sv, n_a), TRUE, SVt_PVIO));
4656 if (type == OP_FTTTY)
4657 return newGVOP(type, OPf_REF, gv_fetchpv("main::STDIN", TRUE,
4660 return newUNOP(type, 0, newDEFSVOP());
4673 int type = o->op_type;
4674 register I32 oa = PL_opargs[type] >> OASHIFT;
4676 if (o->op_flags & OPf_STACKED) {
4677 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
4680 return no_fh_allowed(o);
4683 if (o->op_flags & OPf_KIDS) {
4685 tokid = &cLISTOPo->op_first;
4686 kid = cLISTOPo->op_first;
4687 if (kid->op_type == OP_PUSHMARK ||
4688 kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK)
4690 tokid = &kid->op_sibling;
4691 kid = kid->op_sibling;
4693 if (!kid && PL_opargs[type] & OA_DEFGV)
4694 *tokid = kid = newDEFSVOP();
4698 sibl = kid->op_sibling;
4701 /* list seen where single (scalar) arg expected? */
4702 if (numargs == 1 && !(oa >> 4)
4703 && kid->op_type == OP_LIST && type != OP_SCALAR)
4705 return too_many_arguments(o,PL_op_desc[type]);
4718 if (kid->op_type == OP_CONST &&
4719 (kid->op_private & OPpCONST_BARE))
4721 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
4722 OP *newop = newAVREF(newGVOP(OP_GV, 0,
4723 gv_fetchpv(name, TRUE, SVt_PVAV) ));
4724 if (ckWARN(WARN_SYNTAX))
4726 "Array @%s missing the @ in argument %ld of %s()",
4727 name, (long)numargs, PL_op_desc[type]);
4730 kid->op_sibling = sibl;
4733 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
4734 bad_type(numargs, "array", PL_op_desc[type], kid);
4738 if (kid->op_type == OP_CONST &&
4739 (kid->op_private & OPpCONST_BARE))
4741 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
4742 OP *newop = newHVREF(newGVOP(OP_GV, 0,
4743 gv_fetchpv(name, TRUE, SVt_PVHV) ));
4744 if (ckWARN(WARN_SYNTAX))
4746 "Hash %%%s missing the %% in argument %ld of %s()",
4747 name, (long)numargs, PL_op_desc[type]);
4750 kid->op_sibling = sibl;
4753 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
4754 bad_type(numargs, "hash", PL_op_desc[type], kid);
4759 OP *newop = newUNOP(OP_NULL, 0, kid);
4760 kid->op_sibling = 0;
4762 newop->op_next = newop;
4764 kid->op_sibling = sibl;
4769 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) {
4770 if (kid->op_type == OP_CONST &&
4771 (kid->op_private & OPpCONST_BARE))
4773 OP *newop = newGVOP(OP_GV, 0,
4774 gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, n_a), TRUE,
4779 else if (kid->op_type == OP_READLINE) {
4780 /* neophyte patrol: open(<FH>), close(<FH>) etc. */
4781 bad_type(numargs, "HANDLE", PL_op_desc[o->op_type], kid);
4784 I32 flags = OPf_SPECIAL;
4786 /* is this op a FH constructor? */
4787 if (is_handle_constructor(o,numargs)) {
4789 /* Set a flag to tell rv2gv to vivify
4790 * need to "prove" flag does not mean something
4791 * else already - NI-S 1999/05/07
4795 /* Helps with open($array[$n],...)
4796 but is too simplistic - need to do selectively
4801 kid->op_sibling = 0;
4802 kid = newUNOP(OP_RV2GV, flags, scalar(kid));
4804 kid->op_private |= private;
4807 kid->op_sibling = sibl;
4813 mod(scalar(kid), type);
4817 tokid = &kid->op_sibling;
4818 kid = kid->op_sibling;
4820 o->op_private |= numargs;
4822 return too_many_arguments(o,PL_op_desc[o->op_type]);
4825 else if (PL_opargs[type] & OA_DEFGV) {
4827 return newUNOP(type, 0, newDEFSVOP());
4831 while (oa & OA_OPTIONAL)
4833 if (oa && oa != OA_LIST)
4834 return too_few_arguments(o,PL_op_desc[o->op_type]);
4844 if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
4845 append_elem(OP_GLOB, o, newDEFSVOP());
4847 if (!((gv = gv_fetchpv("glob", FALSE, SVt_PVCV)) && GvIMPORTED_CV(gv)))
4848 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
4850 if (gv && GvIMPORTED_CV(gv)) {
4851 append_elem(OP_GLOB, o,
4852 newSVOP(OP_CONST, 0, newSViv(PL_glob_index++)));
4853 o->op_type = OP_LIST;
4854 o->op_ppaddr = PL_ppaddr[OP_LIST];
4855 cLISTOPo->op_first->op_type = OP_PUSHMARK;
4856 cLISTOPo->op_first->op_ppaddr = PL_ppaddr[OP_PUSHMARK];
4857 o = newUNOP(OP_ENTERSUB, OPf_STACKED,
4858 append_elem(OP_LIST, o,
4859 scalar(newUNOP(OP_RV2CV, 0,
4860 newGVOP(OP_GV, 0, gv)))));
4861 o = newUNOP(OP_NULL, 0, ck_subr(o));
4862 o->op_targ = OP_GLOB; /* hint at what it used to be */
4865 gv = newGVgen("main");
4867 append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
4877 OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
4879 o->op_ppaddr = PL_ppaddr[OP_GREPSTART];
4880 Newz(1101, gwop, 1, LOGOP);
4882 if (o->op_flags & OPf_STACKED) {
4885 kid = cLISTOPo->op_first->op_sibling;
4886 for (k = cLISTOPo->op_first->op_sibling->op_next; k; k = k->op_next) {
4889 kid->op_next = (OP*)gwop;
4890 o->op_flags &= ~OPf_STACKED;
4892 kid = cLISTOPo->op_first->op_sibling;
4893 if (type == OP_MAPWHILE)
4900 kid = cLISTOPo->op_first->op_sibling;
4901 if (kid->op_type != OP_NULL)
4902 croak("panic: ck_grep");
4903 kid = kUNOP->op_first;
4905 gwop->op_type = type;
4906 gwop->op_ppaddr = PL_ppaddr[type];
4907 gwop->op_first = listkids(o);
4908 gwop->op_flags |= OPf_KIDS;
4909 gwop->op_private = 1;
4910 gwop->op_other = LINKLIST(kid);
4911 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
4912 kid->op_next = (OP*)gwop;
4914 kid = cLISTOPo->op_first->op_sibling;
4915 if (!kid || !kid->op_sibling)
4916 return too_few_arguments(o,PL_op_desc[o->op_type]);
4917 for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
4918 mod(kid, OP_GREPSTART);
4926 if (o->op_flags & OPf_KIDS) {
4927 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
4929 kid = kid->op_sibling; /* get past "big" */
4930 if (kid && kid->op_type == OP_CONST)
4931 fbm_compile(((SVOP*)kid)->op_sv, 0);
4937 ck_lengthconst(OP *o)
4939 /* XXX length optimization goes here */
4946 OPCODE type = o->op_type;
4947 return modkids(ck_fun(o), type);
4953 OPCODE type = o->op_type;
4954 return refkids(ck_fun(o), type);
4962 kid = cLISTOPo->op_first;
4965 kid = cLISTOPo->op_first;
4967 if (kid->op_type == OP_PUSHMARK)
4968 kid = kid->op_sibling;
4969 if (kid && o->op_flags & OPf_STACKED)
4970 kid = kid->op_sibling;
4971 else if (kid && !kid->op_sibling) { /* print HANDLE; */
4972 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
4973 o->op_flags |= OPf_STACKED; /* make it a filehandle */
4974 kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
4975 cLISTOPo->op_first->op_sibling = kid;
4976 cLISTOPo->op_last = kid;
4977 kid = kid->op_sibling;
4982 append_elem(o->op_type, o, newDEFSVOP());
4988 if (PL_hints & HINT_LOCALE)
4989 o->op_private |= OPpLOCALE;
4996 ck_fun_locale(OP *o)
5002 if (PL_hints & HINT_LOCALE)
5003 o->op_private |= OPpLOCALE;
5014 if (PL_hints & HINT_LOCALE)
5015 o->op_private |= OPpLOCALE;
5024 o->op_private |= OPpRUNTIME;
5037 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
5038 o->op_private |= OPpREPEAT_DOLIST;
5039 cBINOPo->op_first = force_list(cBINOPo->op_first);
5049 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
5050 SVOP *kid = (SVOP*)cUNOPo->op_first;
5052 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
5054 for (s = SvPVX(kid->op_sv); *s; s++) {
5055 if (*s == ':' && s[1] == ':') {
5057 Move(s+2, s+1, strlen(s+2)+1, char);
5058 --SvCUR(kid->op_sv);
5061 sv_catpvn(kid->op_sv, ".pm", 3);
5071 croak("NOT IMPL LINE %d",__LINE__);
5081 if (o->op_flags & OPf_KIDS) {
5082 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5083 if (kid && kid->op_sibling) {
5084 o->op_type = OP_SSELECT;
5085 o->op_ppaddr = PL_ppaddr[OP_SSELECT];
5087 return fold_constants(o);
5091 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5092 if (kid && kid->op_type == OP_RV2GV)
5093 kid->op_private &= ~HINT_STRICT_REFS;
5100 I32 type = o->op_type;
5102 if (!(o->op_flags & OPf_KIDS)) {
5107 if (!CvUNIQUE(PL_compcv)) {
5108 argop = newOP(OP_PADAV, OPf_REF);
5109 argop->op_targ = 0; /* PL_curpad[0] is @_ */
5112 argop = newUNOP(OP_RV2AV, 0,
5113 scalar(newGVOP(OP_GV, 0,
5114 gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
5117 argop = newUNOP(OP_RV2AV, 0,
5118 scalar(newGVOP(OP_GV, 0, !CvUNIQUE(PL_compcv) ?
5119 PL_defgv : gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
5120 #endif /* USE_THREADS */
5121 return newUNOP(type, 0, scalar(argop));
5123 return scalar(modkids(ck_fun(o), type));
5131 if (PL_hints & HINT_LOCALE)
5132 o->op_private |= OPpLOCALE;
5135 if (o->op_type == OP_SORT && o->op_flags & OPf_STACKED)
5137 if (o->op_flags & OPf_STACKED) { /* may have been cleared */
5138 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5140 kid = kUNOP->op_first; /* get past null */
5142 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
5144 if (kid->op_type == OP_SCOPE) {
5148 else if (kid->op_type == OP_LEAVE) {
5149 if (o->op_type == OP_SORT) {
5150 null(kid); /* wipe out leave */
5153 for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
5154 if (k->op_next == kid)
5159 kid->op_next = 0; /* just disconnect the leave */
5160 k = kLISTOP->op_first;
5164 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5165 if (o->op_type == OP_SORT)
5169 o->op_flags |= OPf_SPECIAL;
5171 else if (kid->op_type == OP_RV2SV || kid->op_type == OP_PADSV)
5172 null(cLISTOPo->op_first->op_sibling);
5179 simplify_sort(OP *o)
5182 register OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5185 if (!(o->op_flags & OPf_STACKED))
5187 GvMULTI_on(gv_fetchpv("a", TRUE, SVt_PV));
5188 GvMULTI_on(gv_fetchpv("b", TRUE, SVt_PV));
5189 kid = kUNOP->op_first; /* get past null */
5190 if (kid->op_type != OP_SCOPE)
5192 kid = kLISTOP->op_last; /* get past scope */
5193 switch(kid->op_type) {
5201 k = kid; /* remember this node*/
5202 if (kBINOP->op_first->op_type != OP_RV2SV)
5204 kid = kBINOP->op_first; /* get past cmp */
5205 if (kUNOP->op_first->op_type != OP_GV)
5207 kid = kUNOP->op_first; /* get past rv2sv */
5208 if (GvSTASH(kGVOP->op_gv) != PL_curstash)
5210 if (strEQ(GvNAME(kGVOP->op_gv), "a"))
5212 else if(strEQ(GvNAME(kGVOP->op_gv), "b"))
5216 kid = k; /* back to cmp */
5217 if (kBINOP->op_last->op_type != OP_RV2SV)
5219 kid = kBINOP->op_last; /* down to 2nd arg */
5220 if (kUNOP->op_first->op_type != OP_GV)
5222 kid = kUNOP->op_first; /* get past rv2sv */
5223 if (GvSTASH(kGVOP->op_gv) != PL_curstash
5225 ? strNE(GvNAME(kGVOP->op_gv), "a")
5226 : strNE(GvNAME(kGVOP->op_gv), "b")))
5228 o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL);
5230 o->op_private |= OPpSORT_REVERSE;
5231 if (k->op_type == OP_NCMP)
5232 o->op_private |= OPpSORT_NUMERIC;
5233 if (k->op_type == OP_I_NCMP)
5234 o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER;
5235 op_free(cLISTOPo->op_first->op_sibling); /* delete comparison block */
5236 cLISTOPo->op_first->op_sibling = cLISTOPo->op_last;
5237 cLISTOPo->op_children = 1;
5245 if (o->op_flags & OPf_STACKED)
5246 return no_fh_allowed(o);
5248 kid = cLISTOPo->op_first;
5249 if (kid->op_type != OP_NULL)
5250 croak("panic: ck_split");
5251 kid = kid->op_sibling;
5252 op_free(cLISTOPo->op_first);
5253 cLISTOPo->op_first = kid;
5255 cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpvn(" ", 1));
5256 cLISTOPo->op_last = kid; /* There was only one element previously */
5259 if (kid->op_type != OP_MATCH) {
5260 OP *sibl = kid->op_sibling;
5261 kid->op_sibling = 0;
5262 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
5263 if (cLISTOPo->op_first == cLISTOPo->op_last)
5264 cLISTOPo->op_last = kid;
5265 cLISTOPo->op_first = kid;
5266 kid->op_sibling = sibl;
5269 kid->op_type = OP_PUSHRE;
5270 kid->op_ppaddr = PL_ppaddr[OP_PUSHRE];
5273 if (!kid->op_sibling)
5274 append_elem(OP_SPLIT, o, newDEFSVOP());
5276 kid = kid->op_sibling;
5279 if (!kid->op_sibling)
5280 append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
5282 kid = kid->op_sibling;
5285 if (kid->op_sibling)
5286 return too_many_arguments(o,PL_op_desc[o->op_type]);
5295 OP *prev = ((cUNOPo->op_first->op_sibling)
5296 ? cUNOPo : ((UNOP*)cUNOPo->op_first))->op_first;
5297 OP *o2 = prev->op_sibling;
5306 for (cvop = o2; cvop->op_sibling; cvop = cvop->op_sibling) ;
5307 if (cvop->op_type == OP_RV2CV) {
5309 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
5310 null(cvop); /* disable rv2cv */
5311 tmpop = (SVOP*)((UNOP*)cvop)->op_first;
5312 if (tmpop->op_type == OP_GV) {
5313 cv = GvCVu(tmpop->op_sv);
5314 if (cv && SvPOK(cv) && !(o->op_private & OPpENTERSUB_AMPER)) {
5315 namegv = CvANON(cv) ? (GV*)tmpop->op_sv : CvGV(cv);
5316 proto = SvPV((SV*)cv, n_a);
5320 o->op_private |= (PL_hints & HINT_STRICT_REFS);
5321 if (PERLDB_SUB && PL_curstash != PL_debstash)
5322 o->op_private |= OPpENTERSUB_DB;
5323 while (o2 != cvop) {
5327 return too_many_arguments(o, gv_ename(namegv));
5345 if (o2->op_type != OP_REFGEN && o2->op_type != OP_UNDEF)
5346 bad_type(arg, "block", gv_ename(namegv), o2);
5349 /* '*' allows any scalar type, including bareword */
5352 if (o2->op_type == OP_RV2GV)
5353 goto wrapref; /* autoconvert GLOB -> GLOBref */
5361 if (o2->op_type != OP_RV2GV)
5362 bad_type(arg, "symbol", gv_ename(namegv), o2);
5365 if (o2->op_type != OP_RV2CV)
5366 bad_type(arg, "sub", gv_ename(namegv), o2);
5369 if (o2->op_type != OP_RV2SV
5370 && o2->op_type != OP_PADSV
5371 && o2->op_type != OP_THREADSV)
5373 bad_type(arg, "scalar", gv_ename(namegv), o2);
5377 if (o2->op_type != OP_RV2AV && o2->op_type != OP_PADAV)
5378 bad_type(arg, "array", gv_ename(namegv), o2);
5381 if (o2->op_type != OP_RV2HV && o2->op_type != OP_PADHV)
5382 bad_type(arg, "hash", gv_ename(namegv), o2);
5386 OP* sib = kid->op_sibling;
5387 kid->op_sibling = 0;
5388 o2 = newUNOP(OP_REFGEN, 0, kid);
5389 o2->op_sibling = sib;
5390 prev->op_sibling = o2;
5401 croak("Malformed prototype for %s: %s",
5402 gv_ename(namegv), SvPV((SV*)cv, n_a));
5407 mod(o2, OP_ENTERSUB);
5409 o2 = o2->op_sibling;
5411 if (proto && !optional &&
5412 (*proto && *proto != '@' && *proto != '%' && *proto != ';'))
5413 return too_few_arguments(o, gv_ename(namegv));
5420 SvREADONLY_on(cSVOPo->op_sv);
5427 if (o->op_flags & OPf_KIDS) {
5428 SVOP *kid = (SVOP*)cUNOPo->op_first;
5430 if (kid->op_type == OP_NULL)
5431 kid = (SVOP*)kid->op_sibling;
5433 kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE))
5434 o->op_flags |= OPf_SPECIAL;
5439 /* A peephole optimizer. We visit the ops in the order they're to execute. */
5442 peep(register OP *o)
5445 register OP* oldop = 0;
5448 if (!o || o->op_seq)
5452 SAVESPTR(PL_curcop);
5453 for (; o; o = o->op_next) {
5459 switch (o->op_type) {
5462 PL_curcop = ((COP*)o); /* for warnings */
5463 o->op_seq = PL_op_seqmax++;
5474 if (o->op_next && o->op_next->op_type == OP_STRINGIFY)
5476 o->op_seq = PL_op_seqmax++;
5479 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
5480 o->op_seq = PL_op_seqmax++;
5481 break; /* Scalar stub must produce undef. List stub is noop */
5485 if (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE)
5486 PL_curcop = ((COP*)o);
5492 if (oldop && o->op_next) {
5493 oldop->op_next = o->op_next;
5496 o->op_seq = PL_op_seqmax++;
5500 if (o->op_next->op_type == OP_RV2SV) {
5501 if (!(o->op_next->op_private & OPpDEREF)) {
5503 o->op_private |= o->op_next->op_private & OPpLVAL_INTRO;
5504 o->op_next = o->op_next->op_next;
5505 o->op_type = OP_GVSV;
5506 o->op_ppaddr = PL_ppaddr[OP_GVSV];
5509 else if (o->op_next->op_type == OP_RV2AV) {
5510 OP* pop = o->op_next->op_next;
5512 if (pop->op_type == OP_CONST &&
5513 (PL_op = pop->op_next) &&
5514 pop->op_next->op_type == OP_AELEM &&
5515 !(pop->op_next->op_private &
5516 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF)) &&
5517 (i = SvIV(((SVOP*)pop)->op_sv) - PL_compiling.cop_arybase)
5521 SvREFCNT_dec(((SVOP*)pop)->op_sv);
5525 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
5526 o->op_next = pop->op_next->op_next;
5527 o->op_type = OP_AELEMFAST;
5528 o->op_ppaddr = PL_ppaddr[OP_AELEMFAST];
5529 o->op_private = (U8)i;
5530 GvAVn(((GVOP*)o)->op_gv);
5533 o->op_seq = PL_op_seqmax++;
5540 o->op_seq = PL_op_seqmax++;
5541 while (cLOGOP->op_other->op_type == OP_NULL)
5542 cLOGOP->op_other = cLOGOP->op_other->op_next;
5543 peep(cLOGOP->op_other);
5547 o->op_seq = PL_op_seqmax++;
5548 peep(cCONDOP->op_true);
5549 peep(cCONDOP->op_false);
5553 o->op_seq = PL_op_seqmax++;
5554 peep(cLOOP->op_redoop);
5555 peep(cLOOP->op_nextop);
5556 peep(cLOOP->op_lastop);
5562 o->op_seq = PL_op_seqmax++;
5563 peep(cPMOP->op_pmreplstart);
5567 o->op_seq = PL_op_seqmax++;
5568 if (ckWARN(WARN_SYNTAX) && o->op_next
5569 && o->op_next->op_type == OP_NEXTSTATE) {
5570 if (o->op_next->op_sibling &&
5571 o->op_next->op_sibling->op_type != OP_EXIT &&
5572 o->op_next->op_sibling->op_type != OP_WARN &&
5573 o->op_next->op_sibling->op_type != OP_DIE) {
5574 line_t oldline = PL_curcop->cop_line;
5576 PL_curcop->cop_line = ((COP*)o->op_next)->cop_line;
5577 warner(WARN_SYNTAX, "Statement unlikely to be reached");
5578 warner(WARN_SYNTAX, "(Maybe you meant system() when you said exec()?)\n");
5579 PL_curcop->cop_line = oldline;
5593 if ((o->op_private & (OPpLVAL_INTRO))
5594 || ((BINOP*)o)->op_last->op_type != OP_CONST)
5596 rop = (UNOP*)((BINOP*)o)->op_first;
5597 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
5599 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
5600 if (!SvOBJECT(lexname))
5602 fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
5603 if (!fields || !GvHV(*fields))
5605 svp = &((SVOP*)((BINOP*)o)->op_last)->op_sv;
5606 key = SvPV(*svp, keylen);
5607 indsvp = hv_fetch(GvHV(*fields), key, keylen, FALSE);
5609 croak("No such field \"%s\" in variable %s of type %s",
5610 key, SvPV(lexname, n_a), HvNAME(SvSTASH(lexname)));
5612 ind = SvIV(*indsvp);
5614 croak("Bad index while coercing array into hash");
5615 rop->op_type = OP_RV2AV;
5616 rop->op_ppaddr = PL_ppaddr[OP_RV2AV];
5617 o->op_type = OP_AELEM;
5618 o->op_ppaddr = PL_ppaddr[OP_AELEM];
5620 *svp = newSViv(ind);
5625 o->op_seq = PL_op_seqmax++;