3 * Copyright (c) 1991-1997, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * "You see: Mr. Drogo, he married poor Miss Primula Brandybuck. She was
12 * our Mr. Bilbo's first cousin on the mother's side (her mother being the
13 * youngest of the Old Took's daughters); and Mr. Drogo was his second
14 * cousin. So Mr. Frodo is his first *and* second cousin, once removed
15 * either way, as the saying is, if you follow me." --the Gaffer
22 #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));
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])) {
133 name[2] = toCTRL(name[1]);
136 croak("Can't use global %s in \"my\"",name);
138 if (ckWARN(WARN_UNSAFE) && AvFILLp(PL_comppad_name) >= 0) {
139 SV **svp = AvARRAY(PL_comppad_name);
140 for (off = AvFILLp(PL_comppad_name); off > PL_comppad_name_floor; off--) {
142 && sv != &PL_sv_undef
143 && (SvIVX(sv) == PAD_MAX || SvIVX(sv) == 0)
144 && strEQ(name, SvPVX(sv)))
147 "\"my\" variable %s masks earlier declaration in same %s",
148 name, (SvIVX(sv) == PAD_MAX ? "scope" : "statement"));
153 off = pad_alloc(OP_PADSV, SVs_PADMY);
155 sv_upgrade(sv, SVt_PVNV);
157 if (PL_in_my_stash) {
159 croak("Can't declare class for non-scalar %s in \"my\"",name);
161 (void)SvUPGRADE(sv, SVt_PVMG);
162 SvSTASH(sv) = (HV*)SvREFCNT_inc(PL_in_my_stash);
165 av_store(PL_comppad_name, off, sv);
166 SvNVX(sv) = (double)PAD_MAX;
167 SvIVX(sv) = 0; /* Not yet introduced--see newSTATEOP */
168 if (!PL_min_intro_pending)
169 PL_min_intro_pending = off;
170 PL_max_intro_pending = off;
172 av_store(PL_comppad, off, (SV*)newAV());
173 else if (*name == '%')
174 av_store(PL_comppad, off, (SV*)newHV());
175 SvPADMY_on(PL_curpad[off]);
180 pad_findlex(char *name, PADOFFSET newoff, U32 seq, CV* startcv, I32 cx_ix, I32 saweval)
187 register PERL_CONTEXT *cx;
189 for (cv = startcv; cv; cv = CvOUTSIDE(cv)) {
190 AV *curlist = CvPADLIST(cv);
191 SV **svp = av_fetch(curlist, 0, FALSE);
194 if (!svp || *svp == &PL_sv_undef)
197 svp = AvARRAY(curname);
198 for (off = AvFILLp(curname); off > 0; off--) {
199 if ((sv = svp[off]) &&
200 sv != &PL_sv_undef &&
202 seq > I_32(SvNVX(sv)) &&
203 strEQ(SvPVX(sv), name))
214 return 0; /* don't clone from inactive stack frame */
218 oldpad = (AV*)*av_fetch(curlist, depth, FALSE);
219 oldsv = *av_fetch(oldpad, off, TRUE);
220 if (!newoff) { /* Not a mere clone operation. */
221 SV *namesv = NEWSV(1103,0);
222 newoff = pad_alloc(OP_PADSV, SVs_PADMY);
223 sv_upgrade(namesv, SVt_PVNV);
224 sv_setpv(namesv, name);
225 av_store(PL_comppad_name, newoff, namesv);
226 SvNVX(namesv) = (double)PL_curcop->cop_seq;
227 SvIVX(namesv) = PAD_MAX; /* A ref, intro immediately */
228 SvFAKE_on(namesv); /* A ref, not a real var */
229 if (SvOBJECT(sv)) { /* A typed var */
231 (void)SvUPGRADE(namesv, SVt_PVMG);
232 SvSTASH(namesv) = (HV*)SvREFCNT_inc((SV*)SvSTASH(sv));
235 if (CvANON(PL_compcv) || SvTYPE(PL_compcv) == SVt_PVFM) {
236 /* "It's closures all the way down." */
237 CvCLONE_on(PL_compcv);
239 if (CvANON(PL_compcv))
240 oldsv = Nullsv; /* no need to keep ref */
245 bcv && bcv != cv && !CvCLONE(bcv);
246 bcv = CvOUTSIDE(bcv))
251 if (ckWARN(WARN_CLOSURE)
252 && !CvUNIQUE(bcv) && !CvUNIQUE(cv))
255 "Variable \"%s\" may be unavailable",
263 else if (!CvUNIQUE(PL_compcv)) {
264 if (ckWARN(WARN_CLOSURE) && !SvFAKE(sv) && !CvUNIQUE(cv))
266 "Variable \"%s\" will not stay shared", name);
269 av_store(PL_comppad, newoff, SvREFCNT_inc(oldsv));
275 /* Nothing in current lexical context--try eval's context, if any.
276 * This is necessary to let the perldb get at lexically scoped variables.
277 * XXX This will also probably interact badly with eval tree caching.
280 for (i = cx_ix; i >= 0; i--) {
282 switch (CxTYPE(cx)) {
284 if (i == 0 && saweval) {
285 seq = cxstack[saweval].blk_oldcop->cop_seq;
286 return pad_findlex(name, newoff, seq, PL_main_cv, -1, saweval);
290 switch (cx->blk_eval.old_op_type) {
296 /* require must have its own scope */
304 if (PL_debstash && CvSTASH(cv) == PL_debstash) { /* ignore DB'* scope */
305 saweval = i; /* so we know where we were called from */
308 seq = cxstack[saweval].blk_oldcop->cop_seq;
309 return pad_findlex(name, newoff, seq, cv, i-1, saweval);
317 pad_findmy(char *name)
323 SV **svp = AvARRAY(PL_comppad_name);
324 U32 seq = PL_cop_seqmax;
330 * Special case to get lexical (and hence per-thread) @_.
331 * XXX I need to find out how to tell at parse-time whether use
332 * of @_ should refer to a lexical (from a sub) or defgv (global
333 * scope and maybe weird sub-ish things like formats). See
334 * startsub in perly.y. It's possible that @_ could be lexical
335 * (at least from subs) even in non-threaded perl.
337 if (strEQ(name, "@_"))
338 return 0; /* success. (NOT_IN_PAD indicates failure) */
339 #endif /* USE_THREADS */
341 /* The one we're looking for is probably just before comppad_name_fill. */
342 for (off = AvFILLp(PL_comppad_name); off > 0; off--) {
343 if ((sv = svp[off]) &&
344 sv != &PL_sv_undef &&
347 seq > I_32(SvNVX(sv)))) &&
348 strEQ(SvPVX(sv), name))
351 return (PADOFFSET)off;
352 pendoff = off; /* this pending def. will override import */
356 outside = CvOUTSIDE(PL_compcv);
358 /* Check if if we're compiling an eval'', and adjust seq to be the
359 * eval's seq number. This depends on eval'' having a non-null
360 * CvOUTSIDE() while it is being compiled. The eval'' itself is
361 * identified by CvUNIQUE being set and CvGV being null. */
362 if (outside && CvUNIQUE(PL_compcv) && !CvGV(PL_compcv) && cxstack_ix >= 0) {
363 cx = &cxstack[cxstack_ix];
365 seq = cx->blk_oldcop->cop_seq;
368 /* See if it's in a nested scope */
369 off = pad_findlex(name, 0, seq, outside, cxstack_ix, 0);
371 /* If there is a pending local definition, this new alias must die */
373 SvIVX(AvARRAY(PL_comppad_name)[off]) = seq;
374 return off; /* pad_findlex returns 0 for failure...*/
376 return NOT_IN_PAD; /* ...but we return NOT_IN_PAD for failure */
380 pad_leavemy(I32 fill)
383 SV **svp = AvARRAY(PL_comppad_name);
385 if (PL_min_intro_pending && fill < PL_min_intro_pending) {
386 for (off = PL_max_intro_pending; off >= PL_min_intro_pending; off--) {
387 if ((sv = svp[off]) && sv != &PL_sv_undef)
388 warn("%s never introduced", SvPVX(sv));
391 /* "Deintroduce" my variables that are leaving with this scope. */
392 for (off = AvFILLp(PL_comppad_name); off > fill; off--) {
393 if ((sv = svp[off]) && sv != &PL_sv_undef && SvIVX(sv) == PAD_MAX)
394 SvIVX(sv) = PL_cop_seqmax;
399 pad_alloc(I32 optype, U32 tmptype)
405 if (AvARRAY(PL_comppad) != PL_curpad)
406 croak("panic: pad_alloc");
407 if (PL_pad_reset_pending)
409 if (tmptype & SVs_PADMY) {
411 sv = *av_fetch(PL_comppad, AvFILLp(PL_comppad) + 1, TRUE);
412 } while (SvPADBUSY(sv)); /* need a fresh one */
413 retval = AvFILLp(PL_comppad);
416 SV **names = AvARRAY(PL_comppad_name);
417 SSize_t names_fill = AvFILLp(PL_comppad_name);
420 * "foreach" index vars temporarily become aliases to non-"my"
421 * values. Thus we must skip, not just pad values that are
422 * marked as current pad values, but also those with names.
424 if (++PL_padix <= names_fill &&
425 (sv = names[PL_padix]) && sv != &PL_sv_undef)
427 sv = *av_fetch(PL_comppad, PL_padix, TRUE);
428 if (!(SvFLAGS(sv) & (SVs_PADTMP|SVs_PADMY)))
433 SvFLAGS(sv) |= tmptype;
434 PL_curpad = AvARRAY(PL_comppad);
436 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx alloc %ld for %s\n",
437 (unsigned long) thr, (unsigned long) PL_curpad,
438 (long) retval, PL_op_name[optype]));
440 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx alloc %ld for %s\n",
441 (unsigned long) PL_curpad,
442 (long) retval, PL_op_name[optype]));
443 #endif /* USE_THREADS */
444 return (PADOFFSET)retval;
452 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx sv %d\n",
453 (unsigned long) thr, (unsigned long) PL_curpad, po));
456 croak("panic: pad_sv po");
457 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx sv %d\n",
458 (unsigned long) PL_curpad, po));
459 #endif /* USE_THREADS */
460 return PL_curpad[po]; /* eventually we'll turn this into a macro */
464 pad_free(PADOFFSET po)
469 if (AvARRAY(PL_comppad) != PL_curpad)
470 croak("panic: pad_free curpad");
472 croak("panic: pad_free po");
474 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx free %d\n",
475 (unsigned long) thr, (unsigned long) PL_curpad, po));
477 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx free %d\n",
478 (unsigned long) PL_curpad, po));
479 #endif /* USE_THREADS */
480 if (PL_curpad[po] && PL_curpad[po] != &PL_sv_undef)
481 SvPADTMP_off(PL_curpad[po]);
482 if ((I32)po < PL_padix)
487 pad_swipe(PADOFFSET po)
490 if (AvARRAY(PL_comppad) != PL_curpad)
491 croak("panic: pad_swipe curpad");
493 croak("panic: pad_swipe po");
495 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx swipe %d\n",
496 (unsigned long) thr, (unsigned long) PL_curpad, po));
498 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx swipe %d\n",
499 (unsigned long) PL_curpad, po));
500 #endif /* USE_THREADS */
501 SvPADTMP_off(PL_curpad[po]);
502 PL_curpad[po] = NEWSV(1107,0);
503 SvPADTMP_on(PL_curpad[po]);
504 if ((I32)po < PL_padix)
508 /* XXX pad_reset() is currently disabled because it results in serious bugs.
509 * It causes pad temp TARGs to be shared between OPs. Since TARGs are pushed
510 * on the stack by OPs that use them, there are several ways to get an alias
511 * to a shared TARG. Such an alias will change randomly and unpredictably.
512 * We avoid doing this until we can think of a Better Way.
517 #ifdef USE_BROKEN_PAD_RESET
521 if (AvARRAY(PL_comppad) != PL_curpad)
522 croak("panic: pad_reset curpad");
524 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx reset\n",
525 (unsigned long) thr, (unsigned long) PL_curpad));
527 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx reset\n",
528 (unsigned long) PL_curpad));
529 #endif /* USE_THREADS */
530 if (!PL_tainting) { /* Can't mix tainted and non-tainted temporaries. */
531 for (po = AvMAX(PL_comppad); po > PL_padix_floor; po--) {
532 if (PL_curpad[po] && !SvIMMORTAL(PL_curpad[po]))
533 SvPADTMP_off(PL_curpad[po]);
535 PL_padix = PL_padix_floor;
538 PL_pad_reset_pending = FALSE;
542 /* find_threadsv is not reentrant */
544 find_threadsv(const char *name)
550 /* We currently only handle names of a single character */
551 p = strchr(PL_threadsv_names, *name);
554 key = p - PL_threadsv_names;
555 MUTEX_LOCK(&thr->mutex);
556 svp = av_fetch(thr->threadsv, key, FALSE);
558 MUTEX_UNLOCK(&thr->mutex);
560 SV *sv = NEWSV(0, 0);
561 av_store(thr->threadsv, key, sv);
562 thr->threadsvp = AvARRAY(thr->threadsv);
563 MUTEX_UNLOCK(&thr->mutex);
565 * Some magic variables used to be automagically initialised
566 * in gv_fetchpv. Those which are now per-thread magicals get
567 * initialised here instead.
573 sv_setpv(sv, "\034");
574 sv_magic(sv, 0, 0, name, 1);
579 PL_sawampersand = TRUE;
593 /* XXX %! tied to Errno.pm needs to be added here.
594 * See gv_fetchpv(). */
598 sv_magic(sv, 0, 0, name, 1);
600 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
601 "find_threadsv: new SV %p for $%s%c\n",
602 sv, (*name < 32) ? "^" : "",
603 (*name < 32) ? toCTRL(*name) : *name));
607 #endif /* USE_THREADS */
614 register OP *kid, *nextkid;
616 if (!o || o->op_seq == (U16)-1)
619 if (o->op_flags & OPf_KIDS) {
620 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
621 nextkid = kid->op_sibling; /* Get before next freeing kid */
626 switch (o->op_type) {
628 o->op_targ = 0; /* Was holding old type, if any. */
631 o->op_targ = 0; /* Was holding hints. */
635 if (!(o->op_flags & OPf_SPECIAL))
639 o->op_targ = 0; /* Was holding index into thr->threadsv AV. */
641 #endif /* USE_THREADS */
643 if (!(o->op_flags & OPf_REF)
644 || (PL_check[o->op_type] != FUNC_NAME_TO_PTR(ck_ftst)))
650 SvREFCNT_dec(cGVOPo->op_gv);
654 Safefree(cCOPo->cop_label);
655 SvREFCNT_dec(cCOPo->cop_filegv);
656 if (cCOPo->cop_warnings != WARN_NONE && cCOPo->cop_warnings != WARN_ALL)
657 SvREFCNT_dec(cCOPo->cop_warnings);
660 SvREFCNT_dec(cSVOPo->op_sv);
666 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
670 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF))
671 SvREFCNT_dec(cSVOPo->op_sv);
673 Safefree(cPVOPo->op_pv);
676 op_free(cPMOPo->op_pmreplroot);
681 ReREFCNT_dec(cPMOPo->op_pmregexp);
686 pad_free(o->op_targ);
694 if (o->op_type != OP_NULL && o->op_type != OP_THREADSV && o->op_targ > 0)
695 pad_free(o->op_targ);
696 o->op_targ = o->op_type;
697 o->op_type = OP_NULL;
698 o->op_ppaddr = PL_ppaddr[OP_NULL];
701 /* Contextualizers */
703 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
713 /* establish postfix order */
714 if (cUNOPo->op_first) {
715 o->op_next = LINKLIST(cUNOPo->op_first);
716 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
718 kid->op_next = LINKLIST(kid->op_sibling);
733 if (o && o->op_flags & OPf_KIDS) {
734 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
743 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
745 if (ckWARN(WARN_SYNTAX)) {
746 line_t oldline = PL_curcop->cop_line;
748 if (PL_copline != NOLINE)
749 PL_curcop->cop_line = PL_copline;
750 warner(WARN_SYNTAX, "Found = in conditional, should be ==");
751 PL_curcop->cop_line = oldline;
762 /* assumes no premature commitment */
763 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
764 || o->op_type == OP_RETURN)
767 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
769 switch (o->op_type) {
771 if (o->op_private & OPpREPEAT_DOLIST)
772 null(((LISTOP*)cBINOPo->op_first)->op_first);
773 scalar(cBINOPo->op_first);
778 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
782 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
783 if (!kPMOP->op_pmreplroot)
784 deprecate("implicit split to @_");
792 if (o->op_flags & OPf_KIDS) {
793 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
799 kid = cLISTOPo->op_first;
801 while (kid = kid->op_sibling) {
807 WITH_THR(PL_curcop = &PL_compiling);
812 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
818 WITH_THR(PL_curcop = &PL_compiling);
831 /* assumes no premature commitment */
832 U8 want = o->op_flags & OPf_WANT;
833 if (!o || (want && want != OPf_WANT_SCALAR) || PL_error_count
834 || o->op_type == OP_RETURN)
837 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
839 switch (o->op_type) {
841 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
845 if (o->op_flags & OPf_STACKED)
849 if (o->op_private == 4)
920 if (!(o->op_private & OPpLVAL_INTRO))
921 useless = PL_op_desc[o->op_type];
928 if (!(o->op_private & OPpLVAL_INTRO) &&
929 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
930 useless = "a variable";
935 WITH_THR(PL_curcop = ((COP*)o)); /* for warning below */
942 if (ckWARN(WARN_VOID)) {
943 useless = "a constant";
944 if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
946 else if (SvPOK(sv)) {
947 if (strnEQ(SvPVX(sv), "di", 2) ||
948 strnEQ(SvPVX(sv), "ds", 2) ||
949 strnEQ(SvPVX(sv), "ig", 2))
954 null(o); /* don't execute a constant */
955 SvREFCNT_dec(sv); /* don't even remember it */
959 o->op_type = OP_PREINC; /* pre-increment is faster */
960 o->op_ppaddr = PL_ppaddr[OP_PREINC];
964 o->op_type = OP_PREDEC; /* pre-decrement is faster */
965 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
971 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
976 if (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE)
977 WITH_THR(PL_curcop = ((COP*)o)); /* for warning below */
978 if (o->op_flags & OPf_STACKED)
984 if (!(o->op_flags & OPf_KIDS))
993 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1000 /* all requires must return a boolean value */
1001 o->op_flags &= ~OPf_WANT;
1004 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
1005 if (!kPMOP->op_pmreplroot)
1006 deprecate("implicit split to @_");
1012 if (ckWARN(WARN_VOID))
1013 warner(WARN_VOID, "Useless use of %s in void context", useless);
1022 if (o && o->op_flags & OPf_KIDS) {
1023 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1034 /* assumes no premature commitment */
1035 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
1036 || o->op_type == OP_RETURN)
1039 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
1041 switch (o->op_type) {
1044 list(cBINOPo->op_first);
1049 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1057 if (!(o->op_flags & OPf_KIDS))
1059 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1060 list(cBINOPo->op_first);
1061 return gen_constant_list(o);
1068 kid = cLISTOPo->op_first;
1070 while (kid = kid->op_sibling) {
1071 if (kid->op_sibling)
1076 WITH_THR(PL_curcop = &PL_compiling);
1080 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1081 if (kid->op_sibling)
1086 WITH_THR(PL_curcop = &PL_compiling);
1089 /* all requires must return a boolean value */
1090 o->op_flags &= ~OPf_WANT;
1102 if (o->op_type == OP_LINESEQ ||
1103 o->op_type == OP_SCOPE ||
1104 o->op_type == OP_LEAVE ||
1105 o->op_type == OP_LEAVETRY)
1108 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1109 if (kid->op_sibling) {
1113 PL_curcop = &PL_compiling;
1115 o->op_flags &= ~OPf_PARENS;
1116 if (PL_hints & HINT_BLOCK_SCOPE)
1117 o->op_flags |= OPf_PARENS;
1120 o = newOP(OP_STUB, 0);
1125 modkids(OP *o, I32 type)
1128 if (o && o->op_flags & OPf_KIDS) {
1129 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1136 mod(OP *o, I32 type)
1143 if (!o || PL_error_count)
1146 switch (o->op_type) {
1151 if (!(o->op_private & (OPpCONST_ARYBASE)))
1153 if (PL_eval_start && PL_eval_start->op_type == OP_CONST) {
1154 PL_compiling.cop_arybase = (I32)SvIV(((SVOP*)PL_eval_start)->op_sv);
1158 SAVEI32(PL_compiling.cop_arybase);
1159 PL_compiling.cop_arybase = 0;
1161 else if (type == OP_REFGEN)
1164 croak("That use of $[ is unsupported");
1167 if (o->op_flags & OPf_PARENS)
1171 if ((type == OP_UNDEF || type == OP_REFGEN) &&
1172 !(o->op_flags & OPf_STACKED)) {
1173 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1174 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1175 assert(cUNOPo->op_first->op_type == OP_NULL);
1176 null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1182 /* grep, foreach, subcalls, refgen */
1183 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1185 yyerror(form("Can't modify %s in %s",
1186 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
1187 ? "do block" : PL_op_desc[o->op_type]),
1188 type ? PL_op_desc[type] : "local"));
1202 case OP_RIGHT_SHIFT:
1211 if (!(o->op_flags & OPf_STACKED))
1217 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1223 if (!type && cUNOPo->op_first->op_type != OP_GV)
1224 croak("Can't localize through a reference");
1225 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
1226 PL_modcount = 10000;
1227 return o; /* Treat \(@foo) like ordinary list. */
1231 if (scalar_mod_type(o, type))
1233 ref(cUNOPo->op_first, o->op_type);
1242 PL_modcount = 10000;
1245 if (!type && cUNOPo->op_first->op_type != OP_GV)
1246 croak("Can't localize through a reference");
1247 ref(cUNOPo->op_first, o->op_type);
1251 PL_hints |= HINT_BLOCK_SCOPE;
1261 PL_modcount = 10000;
1262 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1263 return o; /* Treat \(@foo) like ordinary list. */
1264 if (scalar_mod_type(o, type))
1270 croak("Can't localize lexical variable %s",
1271 SvPV(*av_fetch(PL_comppad_name, o->op_targ, 4), n_a));
1276 PL_modcount++; /* XXX ??? */
1278 #endif /* USE_THREADS */
1284 if (type != OP_SASSIGN)
1288 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
1294 pad_free(o->op_targ);
1295 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
1296 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
1297 if (o->op_flags & OPf_KIDS)
1298 mod(cBINOPo->op_first->op_sibling, type);
1303 ref(cBINOPo->op_first, o->op_type);
1304 if (type == OP_ENTERSUB &&
1305 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1306 o->op_private |= OPpLVAL_DEFER;
1313 if (o->op_flags & OPf_KIDS)
1314 mod(cLISTOPo->op_last, type);
1318 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
1320 else if (!(o->op_flags & OPf_KIDS))
1322 if (o->op_targ != OP_LIST) {
1323 mod(cBINOPo->op_first, type);
1328 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1332 o->op_flags |= OPf_MOD;
1334 if (type == OP_AASSIGN || type == OP_SASSIGN)
1335 o->op_flags |= OPf_SPECIAL|OPf_REF;
1337 o->op_private |= OPpLVAL_INTRO;
1338 o->op_flags &= ~OPf_SPECIAL;
1339 PL_hints |= HINT_BLOCK_SCOPE;
1341 else if (type != OP_GREPSTART && type != OP_ENTERSUB)
1342 o->op_flags |= OPf_REF;
1347 scalar_mod_type(OP *o, I32 type)
1351 if (o->op_type == OP_RV2GV)
1375 case OP_RIGHT_SHIFT:
1394 is_handle_constructor(OP *o, I32 argnum)
1396 switch (o->op_type) {
1404 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
1417 refkids(OP *o, I32 type)
1420 if (o && o->op_flags & OPf_KIDS) {
1421 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1428 ref(OP *o, I32 type)
1432 if (!o || PL_error_count)
1435 switch (o->op_type) {
1437 if ((type == OP_DEFINED || type == OP_LOCK) &&
1438 !(o->op_flags & OPf_STACKED)) {
1439 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1440 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1441 assert(cUNOPo->op_first->op_type == OP_NULL);
1442 null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1443 o->op_flags |= OPf_SPECIAL;
1448 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1452 if (type == OP_DEFINED)
1453 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1454 ref(cUNOPo->op_first, o->op_type);
1457 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1458 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1459 : type == OP_RV2HV ? OPpDEREF_HV
1461 o->op_flags |= OPf_MOD;
1466 o->op_flags |= OPf_MOD; /* XXX ??? */
1471 o->op_flags |= OPf_REF;
1474 if (type == OP_DEFINED)
1475 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1476 ref(cUNOPo->op_first, o->op_type);
1481 o->op_flags |= OPf_REF;
1486 if (!(o->op_flags & OPf_KIDS))
1488 ref(cBINOPo->op_first, type);
1492 ref(cBINOPo->op_first, o->op_type);
1493 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1494 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1495 : type == OP_RV2HV ? OPpDEREF_HV
1497 o->op_flags |= OPf_MOD;
1505 if (!(o->op_flags & OPf_KIDS))
1507 ref(cLISTOPo->op_last, type);
1522 if (!o || PL_error_count)
1526 if (type == OP_LIST) {
1527 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1529 } else if (type == OP_UNDEF) {
1531 } else if (type != OP_PADSV &&
1534 type != OP_PUSHMARK)
1536 yyerror(form("Can't declare %s in my", PL_op_desc[o->op_type]));
1539 o->op_flags |= OPf_MOD;
1540 o->op_private |= OPpLVAL_INTRO;
1548 o->op_flags |= OPf_PARENS;
1553 bind_match(I32 type, OP *left, OP *right)
1558 if (ckWARN(WARN_UNSAFE) &&
1559 (left->op_type == OP_RV2AV ||
1560 left->op_type == OP_RV2HV ||
1561 left->op_type == OP_PADAV ||
1562 left->op_type == OP_PADHV)) {
1563 char *desc = PL_op_desc[(right->op_type == OP_SUBST ||
1564 right->op_type == OP_TRANS)
1565 ? right->op_type : OP_MATCH];
1566 char *sample = ((left->op_type == OP_RV2AV ||
1567 left->op_type == OP_PADAV)
1568 ? "@array" : "%hash");
1570 "Applying %s to %s will act on scalar(%s)",
1571 desc, sample, sample);
1574 if (right->op_type == OP_MATCH ||
1575 right->op_type == OP_SUBST ||
1576 right->op_type == OP_TRANS) {
1577 right->op_flags |= OPf_STACKED;
1578 if (right->op_type != OP_MATCH)
1579 left = mod(left, right->op_type);
1580 if (right->op_type == OP_TRANS)
1581 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
1583 o = prepend_elem(right->op_type, scalar(left), right);
1585 return newUNOP(OP_NOT, 0, scalar(o));
1589 return bind_match(type, left,
1590 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
1598 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
1599 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
1606 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
1607 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1608 o->op_type = OP_LEAVE;
1609 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
1612 if (o->op_type == OP_LINESEQ) {
1614 o->op_type = OP_SCOPE;
1615 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
1616 kid = ((LISTOP*)o)->op_first;
1617 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE){
1618 SvREFCNT_dec(((COP*)kid)->cop_filegv);
1623 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
1633 SAVESPTR(GvHV(PL_hintgv));
1634 GvHV(PL_hintgv) = newHVhv(GvHV(PL_hintgv));
1635 SAVEFREESV(GvHV(PL_hintgv));
1639 block_start(int full)
1642 int retval = PL_savestack_ix;
1644 SAVEI32(PL_comppad_name_floor);
1646 if ((PL_comppad_name_fill = AvFILLp(PL_comppad_name)) > 0)
1647 PL_comppad_name_floor = PL_comppad_name_fill;
1649 PL_comppad_name_floor = 0;
1651 SAVEI32(PL_min_intro_pending);
1652 SAVEI32(PL_max_intro_pending);
1653 PL_min_intro_pending = 0;
1654 SAVEI32(PL_comppad_name_fill);
1655 SAVEI32(PL_padix_floor);
1656 PL_padix_floor = PL_padix;
1657 PL_pad_reset_pending = FALSE;
1659 PL_hints &= ~HINT_BLOCK_SCOPE;
1660 SAVEPPTR(PL_compiling.cop_warnings);
1661 if (PL_compiling.cop_warnings != WARN_ALL &&
1662 PL_compiling.cop_warnings != WARN_NONE) {
1663 PL_compiling.cop_warnings = newSVsv(PL_compiling.cop_warnings) ;
1664 SAVEFREESV(PL_compiling.cop_warnings) ;
1672 block_end(I32 floor, OP *seq)
1675 int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
1676 OP* retval = scalarseq(seq);
1678 PL_pad_reset_pending = FALSE;
1679 PL_compiling.op_private = PL_hints;
1681 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
1682 pad_leavemy(PL_comppad_name_fill);
1691 OP *o = newOP(OP_THREADSV, 0);
1692 o->op_targ = find_threadsv("_");
1695 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
1696 #endif /* USE_THREADS */
1704 PL_eval_root = newUNOP(OP_LEAVEEVAL, ((PL_in_eval & 4) ? OPf_SPECIAL : 0), o);
1705 PL_eval_start = linklist(PL_eval_root);
1706 PL_eval_root->op_next = 0;
1707 peep(PL_eval_start);
1712 PL_main_root = scope(sawparens(scalarvoid(o)));
1713 PL_curcop = &PL_compiling;
1714 PL_main_start = LINKLIST(PL_main_root);
1715 PL_main_root->op_next = 0;
1716 peep(PL_main_start);
1719 /* Register with debugger */
1721 CV *cv = perl_get_cv("DB::postponed", FALSE);
1725 XPUSHs((SV*)PL_compiling.cop_filegv);
1727 perl_call_sv((SV*)cv, G_DISCARD);
1734 localize(OP *o, I32 lex)
1736 if (o->op_flags & OPf_PARENS)
1740 if (ckWARN(WARN_PARENTHESIS) && PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == ',') {
1742 for (s = PL_bufptr; *s && (isALNUM(*s) || (*s & 0x80) || strchr("@$%, ",*s)); s++) ;
1743 if (*s == ';' || *s == '=')
1744 warner(WARN_PARENTHESIS, "Parentheses missing around \"%s\" list",
1745 lex ? "my" : "local");
1749 PL_in_my_stash = Nullhv;
1753 return mod(o, OP_NULL); /* a bit kludgey */
1759 if (o->op_type == OP_LIST) {
1762 o2 = newOP(OP_THREADSV, 0);
1763 o2->op_targ = find_threadsv(";");
1765 o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
1766 #endif /* USE_THREADS */
1767 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
1773 fold_constants(register OP *o)
1777 I32 type = o->op_type;
1780 if (PL_opargs[type] & OA_RETSCALAR)
1782 if (PL_opargs[type] & OA_TARGET)
1783 o->op_targ = pad_alloc(type, SVs_PADTMP);
1785 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER))
1786 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
1788 if (!(PL_opargs[type] & OA_FOLDCONST))
1803 if (o->op_private & OPpLOCALE)
1808 goto nope; /* Don't try to run w/ errors */
1810 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
1811 if (curop->op_type != OP_CONST &&
1812 curop->op_type != OP_LIST &&
1813 curop->op_type != OP_SCALAR &&
1814 curop->op_type != OP_NULL &&
1815 curop->op_type != OP_PUSHMARK) {
1820 curop = LINKLIST(o);
1824 sv = *(PL_stack_sp--);
1825 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
1826 pad_swipe(o->op_targ);
1827 else if (SvTEMP(sv)) { /* grab mortal temp? */
1828 (void)SvREFCNT_inc(sv);
1832 if (type == OP_RV2GV)
1833 return newGVOP(OP_GV, 0, (GV*)sv);
1835 /* try to smush double to int, but don't smush -2.0 to -2 */
1836 if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK &&
1840 if ((double)iv == SvNV(sv)) {
1845 SvIOK_off(sv); /* undo SvIV() damage */
1847 return newSVOP(OP_CONST, 0, sv);
1851 if (!(PL_opargs[type] & OA_OTHERINT))
1854 if (!(PL_hints & HINT_INTEGER)) {
1855 if (type == OP_DIVIDE || !(o->op_flags & OPf_KIDS))
1858 for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
1859 if (curop->op_type == OP_CONST) {
1860 if (SvIOK(((SVOP*)curop)->op_sv))
1864 if (PL_opargs[curop->op_type] & OA_RETINTEGER)
1868 o->op_ppaddr = PL_ppaddr[++(o->op_type)];
1875 gen_constant_list(register OP *o)
1879 I32 oldtmps_floor = PL_tmps_floor;
1883 return o; /* Don't attempt to run with errors */
1885 PL_op = curop = LINKLIST(o);
1891 PL_tmps_floor = oldtmps_floor;
1893 o->op_type = OP_RV2AV;
1894 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
1895 curop = ((UNOP*)o)->op_first;
1896 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*PL_stack_sp--));
1903 convert(I32 type, I32 flags, OP *o)
1908 if (!o || o->op_type != OP_LIST)
1909 o = newLISTOP(OP_LIST, 0, o, Nullop);
1911 o->op_flags &= ~OPf_WANT;
1913 if (!(PL_opargs[type] & OA_MARK))
1914 null(cLISTOPo->op_first);
1917 o->op_ppaddr = PL_ppaddr[type];
1918 o->op_flags |= flags;
1920 o = CHECKOP(type, o);
1921 if (o->op_type != type)
1924 if (cLISTOPo->op_children < 7) {
1925 /* XXX do we really need to do this if we're done appending?? */
1926 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1928 cLISTOPo->op_last = last; /* in case check substituted last arg */
1931 return fold_constants(o);
1934 /* List constructors */
1937 append_elem(I32 type, OP *first, OP *last)
1945 if (first->op_type != type || type==OP_LIST && first->op_flags & OPf_PARENS)
1946 return newLISTOP(type, 0, first, last);
1948 if (first->op_flags & OPf_KIDS)
1949 ((LISTOP*)first)->op_last->op_sibling = last;
1951 first->op_flags |= OPf_KIDS;
1952 ((LISTOP*)first)->op_first = last;
1954 ((LISTOP*)first)->op_last = last;
1955 ((LISTOP*)first)->op_children++;
1960 append_list(I32 type, LISTOP *first, LISTOP *last)
1968 if (first->op_type != type)
1969 return prepend_elem(type, (OP*)first, (OP*)last);
1971 if (last->op_type != type)
1972 return append_elem(type, (OP*)first, (OP*)last);
1974 first->op_last->op_sibling = last->op_first;
1975 first->op_last = last->op_last;
1976 first->op_children += last->op_children;
1977 if (first->op_children)
1978 first->op_flags |= OPf_KIDS;
1985 prepend_elem(I32 type, OP *first, OP *last)
1993 if (last->op_type == type) {
1994 if (type == OP_LIST) { /* already a PUSHMARK there */
1995 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
1996 ((LISTOP*)last)->op_first->op_sibling = first;
1999 if (!(last->op_flags & OPf_KIDS)) {
2000 ((LISTOP*)last)->op_last = first;
2001 last->op_flags |= OPf_KIDS;
2003 first->op_sibling = ((LISTOP*)last)->op_first;
2004 ((LISTOP*)last)->op_first = first;
2006 ((LISTOP*)last)->op_children++;
2010 return newLISTOP(type, 0, first, last);
2018 return newOP(OP_STUB, 0);
2024 if (!o || o->op_type != OP_LIST)
2025 o = newLISTOP(OP_LIST, 0, o, Nullop);
2031 newLISTOP(I32 type, I32 flags, OP *first, OP *last)
2035 Newz(1101, listop, 1, LISTOP);
2037 listop->op_type = type;
2038 listop->op_ppaddr = PL_ppaddr[type];
2039 listop->op_children = (first != 0) + (last != 0);
2040 listop->op_flags = flags;
2044 else if (!first && last)
2047 first->op_sibling = last;
2048 listop->op_first = first;
2049 listop->op_last = last;
2050 if (type == OP_LIST) {
2052 pushop = newOP(OP_PUSHMARK, 0);
2053 pushop->op_sibling = first;
2054 listop->op_first = pushop;
2055 listop->op_flags |= OPf_KIDS;
2057 listop->op_last = pushop;
2059 else if (listop->op_children)
2060 listop->op_flags |= OPf_KIDS;
2066 newOP(I32 type, I32 flags)
2069 Newz(1101, o, 1, OP);
2071 o->op_ppaddr = PL_ppaddr[type];
2072 o->op_flags = flags;
2075 o->op_private = 0 + (flags >> 8);
2076 if (PL_opargs[type] & OA_RETSCALAR)
2078 if (PL_opargs[type] & OA_TARGET)
2079 o->op_targ = pad_alloc(type, SVs_PADTMP);
2080 return CHECKOP(type, o);
2084 newUNOP(I32 type, I32 flags, OP *first)
2089 first = newOP(OP_STUB, 0);
2090 if (PL_opargs[type] & OA_MARK)
2091 first = force_list(first);
2093 Newz(1101, unop, 1, UNOP);
2094 unop->op_type = type;
2095 unop->op_ppaddr = PL_ppaddr[type];
2096 unop->op_first = first;
2097 unop->op_flags = flags | OPf_KIDS;
2098 unop->op_private = 1 | (flags >> 8);
2099 unop = (UNOP*) CHECKOP(type, unop);
2103 return fold_constants((OP *) unop);
2107 newBINOP(I32 type, I32 flags, OP *first, OP *last)
2110 Newz(1101, binop, 1, BINOP);
2113 first = newOP(OP_NULL, 0);
2115 binop->op_type = type;
2116 binop->op_ppaddr = PL_ppaddr[type];
2117 binop->op_first = first;
2118 binop->op_flags = flags | OPf_KIDS;
2121 binop->op_private = 1 | (flags >> 8);
2124 binop->op_private = 2 | (flags >> 8);
2125 first->op_sibling = last;
2128 binop = (BINOP*)CHECKOP(type, binop);
2132 binop->op_last = binop->op_first->op_sibling;
2134 return fold_constants((OP *)binop);
2138 utf8compare(const void *a, const void *b)
2141 for (i = 0; i < 10; i++) {
2142 if ((*(U8**)a)[i] < (*(U8**)b)[i])
2144 if ((*(U8**)a)[i] > (*(U8**)b)[i])
2151 pmtrans(OP *o, OP *expr, OP *repl)
2153 SV *tstr = ((SVOP*)expr)->op_sv;
2154 SV *rstr = ((SVOP*)repl)->op_sv;
2157 register U8 *t = (U8*)SvPV(tstr, tlen);
2158 register U8 *r = (U8*)SvPV(rstr, rlen);
2164 register short *tbl;
2166 complement = o->op_private & OPpTRANS_COMPLEMENT;
2167 del = o->op_private & OPpTRANS_DELETE;
2168 squash = o->op_private & OPpTRANS_SQUASH;
2170 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
2171 SV* listsv = newSVpv("# comment\n",0);
2173 U8* tend = t + tlen;
2174 U8* rend = r + rlen;
2190 I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
2191 I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
2197 New(1109, cp, tlen, U8*);
2199 transv = newSVpv("",0);
2208 qsort(cp, i, sizeof(U8*), utf8compare);
2209 for (j = 0; j < i; j++) {
2211 UV val = utf8_to_uv(s, &ulen);
2213 diff = val - nextmin;
2215 t = uv_to_utf8(tmpbuf,nextmin);
2216 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2218 t = uv_to_utf8(tmpbuf, val - 1);
2219 sv_catpvn(transv, "\377", 1);
2220 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2224 val = utf8_to_uv(s+1, &ulen);
2228 t = uv_to_utf8(tmpbuf,nextmin);
2229 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2230 t = uv_to_utf8(tmpbuf, 0x7fffffff);
2231 sv_catpvn(transv, "\377", 1);
2232 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2233 t = (U8*)SvPVX(transv);
2234 tlen = SvCUR(transv);
2237 else if (!rlen && !del) {
2238 r = t; rlen = tlen; rend = tend;
2241 if (to_utf && from_utf) { /* only counting characters */
2242 if (t == r || (tlen == rlen && memEQ(t, r, tlen)))
2243 o->op_private |= OPpTRANS_IDENTICAL;
2245 else { /* straight latin-1 translation */
2246 if (tlen == 4 && memEQ(t, "\0\377\303\277", 4) &&
2247 rlen == 4 && memEQ(r, "\0\377\303\277", 4))
2248 o->op_private |= OPpTRANS_IDENTICAL;
2252 while (t < tend || tfirst <= tlast) {
2253 /* see if we need more "t" chars */
2254 if (tfirst > tlast) {
2255 tfirst = (I32)utf8_to_uv(t, &ulen);
2257 if (t < tend && *t == 0xff) { /* illegal utf8 val indicates range */
2258 tlast = (I32)utf8_to_uv(++t, &ulen);
2265 /* now see if we need more "r" chars */
2266 if (rfirst > rlast) {
2268 rfirst = (I32)utf8_to_uv(r, &ulen);
2270 if (r < rend && *r == 0xff) { /* illegal utf8 val indicates range */
2271 rlast = (I32)utf8_to_uv(++r, &ulen);
2280 rfirst = rlast = 0xffffffff;
2284 /* now see which range will peter our first, if either. */
2285 tdiff = tlast - tfirst;
2286 rdiff = rlast - rfirst;
2293 if (rfirst == 0xffffffff) {
2294 diff = tdiff; /* oops, pretend rdiff is infinite */
2296 sv_catpvf(listsv, "%04x\t%04x\tXXXX\n", tfirst, tlast);
2298 sv_catpvf(listsv, "%04x\t\tXXXX\n", tfirst);
2302 sv_catpvf(listsv, "%04x\t%04x\t%04x\n", tfirst, tfirst + diff, rfirst);
2304 sv_catpvf(listsv, "%04x\t\t%04x\n", tfirst, rfirst);
2306 if (rfirst + diff > max)
2307 max = rfirst + diff;
2312 else if (rfirst <= 0x800)
2313 grows |= (tfirst < 0x80);
2314 else if (rfirst <= 0x10000)
2315 grows |= (tfirst < 0x800);
2316 else if (rfirst <= 0x200000)
2317 grows |= (tfirst < 0x10000);
2318 else if (rfirst <= 0x4000000)
2319 grows |= (tfirst < 0x200000);
2320 else if (rfirst <= 0x80000000)
2321 grows |= (tfirst < 0x4000000);
2333 else if (max > 0xff)
2338 cSVOPo->op_sv = (SV*)swash_init("utf8", "", listsv, bits, none);
2339 SvREFCNT_dec(listsv);
2341 SvREFCNT_dec(transv);
2343 if (!del && havefinal)
2344 (void)hv_store((HV*)SvRV((cSVOPo->op_sv)), "FINAL", 5, newSViv((IV)final), 0);
2346 if (grows && to_utf)
2347 o->op_private |= OPpTRANS_GROWS;
2354 tbl = (short*)cPVOPo->op_pv;
2356 Zero(tbl, 256, short);
2357 for (i = 0; i < tlen; i++)
2359 for (i = 0, j = 0; i < 256; i++) {
2375 if (!rlen && !del) {
2378 o->op_private |= OPpTRANS_IDENTICAL;
2380 for (i = 0; i < 256; i++)
2382 for (i = 0, j = 0; i < tlen; i++,j++) {
2385 if (tbl[t[i]] == -1)
2391 if (tbl[t[i]] == -1)
2402 newPMOP(I32 type, I32 flags)
2407 Newz(1101, pmop, 1, PMOP);
2408 pmop->op_type = type;
2409 pmop->op_ppaddr = PL_ppaddr[type];
2410 pmop->op_flags = flags;
2411 pmop->op_private = 0 | (flags >> 8);
2413 if (PL_hints & HINT_RE_TAINT)
2414 pmop->op_pmpermflags |= PMf_RETAINT;
2415 if (PL_hints & HINT_LOCALE)
2416 pmop->op_pmpermflags |= PMf_LOCALE;
2417 pmop->op_pmflags = pmop->op_pmpermflags;
2419 /* link into pm list */
2420 if (type != OP_TRANS && PL_curstash) {
2421 pmop->op_pmnext = HvPMROOT(PL_curstash);
2422 HvPMROOT(PL_curstash) = pmop;
2429 pmruntime(OP *o, OP *expr, OP *repl)
2434 I32 repl_has_vars = 0;
2436 if (o->op_type == OP_TRANS)
2437 return pmtrans(o, expr, repl);
2439 PL_hints |= HINT_BLOCK_SCOPE;
2442 if (expr->op_type == OP_CONST) {
2444 SV *pat = ((SVOP*)expr)->op_sv;
2445 char *p = SvPV(pat, plen);
2446 if ((o->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
2447 sv_setpvn(pat, "\\s+", 3);
2448 p = SvPV(pat, plen);
2449 pm->op_pmflags |= PMf_SKIPWHITE;
2451 pm->op_pmregexp = CALLREGCOMP(p, p + plen, pm);
2452 if (strEQ("\\s+", pm->op_pmregexp->precomp))
2453 pm->op_pmflags |= PMf_WHITE;
2457 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
2458 expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
2460 : OP_REGCMAYBE),0,expr);
2462 Newz(1101, rcop, 1, LOGOP);
2463 rcop->op_type = OP_REGCOMP;
2464 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
2465 rcop->op_first = scalar(expr);
2466 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL)
2467 ? (OPf_SPECIAL | OPf_KIDS)
2469 rcop->op_private = 1;
2472 /* establish postfix order */
2473 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
2475 rcop->op_next = expr;
2476 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2479 rcop->op_next = LINKLIST(expr);
2480 expr->op_next = (OP*)rcop;
2483 prepend_elem(o->op_type, scalar((OP*)rcop), o);
2488 if (pm->op_pmflags & PMf_EVAL)
2491 else if (repl->op_type == OP_THREADSV
2492 && strchr("&`'123456789+",
2493 PL_threadsv_names[repl->op_targ]))
2497 #endif /* USE_THREADS */
2498 else if (repl->op_type == OP_CONST)
2502 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
2503 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
2505 if (curop->op_type == OP_THREADSV) {
2507 if (strchr("&`'123456789+", curop->op_private))
2511 if (curop->op_type == OP_GV) {
2512 GV *gv = ((GVOP*)curop)->op_gv;
2514 if (strchr("&`'123456789+", *GvENAME(gv)))
2517 #endif /* USE_THREADS */
2518 else if (curop->op_type == OP_RV2CV)
2520 else if (curop->op_type == OP_RV2SV ||
2521 curop->op_type == OP_RV2AV ||
2522 curop->op_type == OP_RV2HV ||
2523 curop->op_type == OP_RV2GV) {
2524 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2527 else if (curop->op_type == OP_PADSV ||
2528 curop->op_type == OP_PADAV ||
2529 curop->op_type == OP_PADHV ||
2530 curop->op_type == OP_PADANY) {
2533 else if (curop->op_type == OP_PUSHRE)
2534 ; /* Okay here, dangerous in newASSIGNOP */
2543 && (!pm->op_pmregexp
2544 || pm->op_pmregexp->reganch & ROPT_EVAL_SEEN))) {
2545 pm->op_pmflags |= PMf_CONST; /* const for long enough */
2546 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
2547 prepend_elem(o->op_type, scalar(repl), o);
2550 if (curop == repl && !pm->op_pmregexp) { /* Has variables. */
2551 pm->op_pmflags |= PMf_MAYBE_CONST;
2552 pm->op_pmpermflags |= PMf_MAYBE_CONST;
2554 Newz(1101, rcop, 1, LOGOP);
2555 rcop->op_type = OP_SUBSTCONT;
2556 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
2557 rcop->op_first = scalar(repl);
2558 rcop->op_flags |= OPf_KIDS;
2559 rcop->op_private = 1;
2562 /* establish postfix order */
2563 rcop->op_next = LINKLIST(repl);
2564 repl->op_next = (OP*)rcop;
2566 pm->op_pmreplroot = scalar((OP*)rcop);
2567 pm->op_pmreplstart = LINKLIST(rcop);
2576 newSVOP(I32 type, I32 flags, SV *sv)
2579 Newz(1101, svop, 1, SVOP);
2580 svop->op_type = type;
2581 svop->op_ppaddr = PL_ppaddr[type];
2583 svop->op_next = (OP*)svop;
2584 svop->op_flags = flags;
2585 if (PL_opargs[type] & OA_RETSCALAR)
2587 if (PL_opargs[type] & OA_TARGET)
2588 svop->op_targ = pad_alloc(type, SVs_PADTMP);
2589 return CHECKOP(type, svop);
2593 newGVOP(I32 type, I32 flags, GV *gv)
2597 Newz(1101, gvop, 1, GVOP);
2598 gvop->op_type = type;
2599 gvop->op_ppaddr = PL_ppaddr[type];
2600 gvop->op_gv = (GV*)SvREFCNT_inc(gv);
2601 gvop->op_next = (OP*)gvop;
2602 gvop->op_flags = flags;
2603 if (PL_opargs[type] & OA_RETSCALAR)
2605 if (PL_opargs[type] & OA_TARGET)
2606 gvop->op_targ = pad_alloc(type, SVs_PADTMP);
2607 return CHECKOP(type, gvop);
2611 newPVOP(I32 type, I32 flags, char *pv)
2614 Newz(1101, pvop, 1, PVOP);
2615 pvop->op_type = type;
2616 pvop->op_ppaddr = PL_ppaddr[type];
2618 pvop->op_next = (OP*)pvop;
2619 pvop->op_flags = flags;
2620 if (PL_opargs[type] & OA_RETSCALAR)
2622 if (PL_opargs[type] & OA_TARGET)
2623 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
2624 return CHECKOP(type, pvop);
2633 save_hptr(&PL_curstash);
2634 save_item(PL_curstname);
2639 name = SvPV(sv, len);
2640 PL_curstash = gv_stashpvn(name,len,TRUE);
2641 sv_setpvn(PL_curstname, name, len);
2645 sv_setpv(PL_curstname,"<none>");
2646 PL_curstash = Nullhv;
2648 PL_hints |= HINT_BLOCK_SCOPE;
2649 PL_copline = NOLINE;
2654 utilize(int aver, I32 floor, OP *version, OP *id, OP *arg)
2663 if (id->op_type != OP_CONST)
2664 croak("Module name must be constant");
2668 if(version != Nullop) {
2669 SV *vesv = ((SVOP*)version)->op_sv;
2671 if (arg == Nullop && !SvNIOK(vesv)) {
2678 if (version->op_type != OP_CONST || !SvNIOK(vesv))
2679 croak("Version number must be constant number");
2681 /* Make copy of id so we don't free it twice */
2682 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2684 /* Fake up a method call to VERSION */
2685 meth = newSVOP(OP_CONST, 0, newSVpv("VERSION", 7));
2686 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2687 append_elem(OP_LIST,
2688 prepend_elem(OP_LIST, pack, list(version)),
2689 newUNOP(OP_METHOD, 0, meth)));
2693 /* Fake up an import/unimport */
2694 if (arg && arg->op_type == OP_STUB)
2695 imop = arg; /* no import on explicit () */
2696 else if(SvNIOK(((SVOP*)id)->op_sv)) {
2697 imop = Nullop; /* use 5.0; */
2700 /* Make copy of id so we don't free it twice */
2701 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2702 meth = newSVOP(OP_CONST, 0,
2704 ? newSVpv("import", 6)
2705 : newSVpv("unimport", 8)
2707 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2708 append_elem(OP_LIST,
2709 prepend_elem(OP_LIST, pack, list(arg)),
2710 newUNOP(OP_METHOD, 0, meth)));
2713 /* Fake up a require, handle override, if any */
2714 gv = gv_fetchpv("require", FALSE, SVt_PVCV);
2715 if (!(gv && GvIMPORTED_CV(gv)))
2716 gv = gv_fetchpv("CORE::GLOBAL::require", FALSE, SVt_PVCV);
2718 if (gv && GvIMPORTED_CV(gv)) {
2719 rqop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
2720 append_elem(OP_LIST, id,
2721 scalar(newUNOP(OP_RV2CV, 0,
2726 rqop = newUNOP(OP_REQUIRE, 0, id);
2729 /* Fake up the BEGIN {}, which does its thing immediately. */
2731 newSVOP(OP_CONST, 0, newSVpv("BEGIN", 5)),
2733 append_elem(OP_LINESEQ,
2734 append_elem(OP_LINESEQ,
2735 newSTATEOP(0, Nullch, rqop),
2736 newSTATEOP(0, Nullch, veop)),
2737 newSTATEOP(0, Nullch, imop) ));
2739 PL_copline = NOLINE;
2749 gv = gv_fetchpv("do", FALSE, SVt_PVCV);
2750 if (!(gv && GvIMPORTED_CV(gv)))
2751 gv = gv_fetchpv("CORE::GLOBAL::do", FALSE, SVt_PVCV);
2753 if (gv && GvIMPORTED_CV(gv)) {
2754 doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
2755 append_elem(OP_LIST, term,
2756 scalar(newUNOP(OP_RV2CV, 0,
2761 doop = newUNOP(OP_DOFILE, 0, scalar(term));
2767 newSLICEOP(I32 flags, OP *subscript, OP *listval)
2769 return newBINOP(OP_LSLICE, flags,
2770 list(force_list(subscript)),
2771 list(force_list(listval)) );
2775 list_assignment(register OP *o)
2780 if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
2781 o = cUNOPo->op_first;
2783 if (o->op_type == OP_COND_EXPR) {
2784 I32 t = list_assignment(cCONDOPo->op_first->op_sibling);
2785 I32 f = list_assignment(cCONDOPo->op_first->op_sibling->op_sibling);
2790 yyerror("Assignment to both a list and a scalar");
2794 if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
2795 o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
2796 o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
2799 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
2802 if (o->op_type == OP_RV2SV)
2809 newASSIGNOP(I32 flags, OP *left, I32 optype, OP *right)
2814 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
2815 return newLOGOP(optype, 0,
2816 mod(scalar(left), optype),
2817 newUNOP(OP_SASSIGN, 0, scalar(right)));
2820 return newBINOP(optype, OPf_STACKED,
2821 mod(scalar(left), optype), scalar(right));
2825 if (list_assignment(left)) {
2828 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
2829 left = mod(left, OP_AASSIGN);
2837 o = newBINOP(OP_AASSIGN, flags,
2838 list(force_list(right)),
2839 list(force_list(left)) );
2840 o->op_private = 0 | (flags >> 8);
2841 if (!(left->op_private & OPpLVAL_INTRO)) {
2845 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
2846 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
2847 if (curop->op_type == OP_GV) {
2848 GV *gv = ((GVOP*)curop)->op_gv;
2849 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
2851 SvCUR(gv) = PL_generation;
2853 else if (curop->op_type == OP_PADSV ||
2854 curop->op_type == OP_PADAV ||
2855 curop->op_type == OP_PADHV ||
2856 curop->op_type == OP_PADANY) {
2857 SV **svp = AvARRAY(PL_comppad_name);
2858 SV *sv = svp[curop->op_targ];
2859 if (SvCUR(sv) == PL_generation)
2861 SvCUR(sv) = PL_generation; /* (SvCUR not used any more) */
2863 else if (curop->op_type == OP_RV2CV)
2865 else if (curop->op_type == OP_RV2SV ||
2866 curop->op_type == OP_RV2AV ||
2867 curop->op_type == OP_RV2HV ||
2868 curop->op_type == OP_RV2GV) {
2869 if (lastop->op_type != OP_GV) /* funny deref? */
2872 else if (curop->op_type == OP_PUSHRE) {
2873 if (((PMOP*)curop)->op_pmreplroot) {
2874 GV *gv = (GV*)((PMOP*)curop)->op_pmreplroot;
2875 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
2877 SvCUR(gv) = PL_generation;
2886 o->op_private = OPpASSIGN_COMMON;
2888 if (right && right->op_type == OP_SPLIT) {
2890 if ((tmpop = ((LISTOP*)right)->op_first) &&
2891 tmpop->op_type == OP_PUSHRE)
2893 PMOP *pm = (PMOP*)tmpop;
2894 if (left->op_type == OP_RV2AV &&
2895 !(left->op_private & OPpLVAL_INTRO) &&
2896 !(o->op_private & OPpASSIGN_COMMON) )
2898 tmpop = ((UNOP*)left)->op_first;
2899 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
2900 pm->op_pmreplroot = (OP*)((GVOP*)tmpop)->op_gv;
2901 pm->op_pmflags |= PMf_ONCE;
2902 tmpop = cUNOPo->op_first; /* to list (nulled) */
2903 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
2904 tmpop->op_sibling = Nullop; /* don't free split */
2905 right->op_next = tmpop->op_next; /* fix starting loc */
2906 op_free(o); /* blow off assign */
2907 right->op_flags &= ~OPf_WANT;
2908 /* "I don't know and I don't care." */
2913 if (PL_modcount < 10000 &&
2914 ((LISTOP*)right)->op_last->op_type == OP_CONST)
2916 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
2918 sv_setiv(sv, PL_modcount+1);
2926 right = newOP(OP_UNDEF, 0);
2927 if (right->op_type == OP_READLINE) {
2928 right->op_flags |= OPf_STACKED;
2929 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
2932 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
2933 o = newBINOP(OP_SASSIGN, flags,
2934 scalar(right), mod(scalar(left), OP_SASSIGN) );
2946 newSTATEOP(I32 flags, char *label, OP *o)
2949 U32 seq = intro_my();
2952 Newz(1101, cop, 1, COP);
2953 if (PERLDB_LINE && PL_curcop->cop_line && PL_curstash != PL_debstash) {
2954 cop->op_type = OP_DBSTATE;
2955 cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
2958 cop->op_type = OP_NEXTSTATE;
2959 cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
2961 cop->op_flags = flags;
2962 cop->op_private = (PL_hints & HINT_UTF8);
2964 cop->op_private |= NATIVE_HINTS;
2966 PL_compiling.op_private = cop->op_private;
2967 cop->op_next = (OP*)cop;
2970 cop->cop_label = label;
2971 PL_hints |= HINT_BLOCK_SCOPE;
2974 cop->cop_arybase = PL_curcop->cop_arybase;
2975 if (PL_curcop->cop_warnings == WARN_NONE
2976 || PL_curcop->cop_warnings == WARN_ALL)
2977 cop->cop_warnings = PL_curcop->cop_warnings ;
2979 cop->cop_warnings = newSVsv(PL_curcop->cop_warnings) ;
2982 if (PL_copline == NOLINE)
2983 cop->cop_line = PL_curcop->cop_line;
2985 cop->cop_line = PL_copline;
2986 PL_copline = NOLINE;
2988 cop->cop_filegv = (GV*)SvREFCNT_inc(PL_curcop->cop_filegv);
2989 cop->cop_stash = PL_curstash;
2991 if (PERLDB_LINE && PL_curstash != PL_debstash) {
2992 SV **svp = av_fetch(GvAV(PL_curcop->cop_filegv),(I32)cop->cop_line, FALSE);
2993 if (svp && *svp != &PL_sv_undef && !SvIOK(*svp)) {
2994 (void)SvIOK_on(*svp);
2996 SvSTASH(*svp) = (HV*)cop;
3000 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
3003 /* "Introduce" my variables to visible status. */
3011 if (! PL_min_intro_pending)
3012 return PL_cop_seqmax;
3014 svp = AvARRAY(PL_comppad_name);
3015 for (i = PL_min_intro_pending; i <= PL_max_intro_pending; i++) {
3016 if ((sv = svp[i]) && sv != &PL_sv_undef && !SvIVX(sv)) {
3017 SvIVX(sv) = PAD_MAX; /* Don't know scope end yet. */
3018 SvNVX(sv) = (double)PL_cop_seqmax;
3021 PL_min_intro_pending = 0;
3022 PL_comppad_name_fill = PL_max_intro_pending; /* Needn't search higher */
3023 return PL_cop_seqmax++;
3027 newLOGOP(I32 type, I32 flags, OP *first, OP *other)
3029 return new_logop(type, flags, &first, &other);
3033 new_logop(I32 type, I32 flags, OP** firstp, OP** otherp)
3038 OP *first = *firstp;
3039 OP *other = *otherp;
3041 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
3042 return newBINOP(type, flags, scalar(first), scalar(other));
3044 scalarboolean(first);
3045 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
3046 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
3047 if (type == OP_AND || type == OP_OR) {
3053 first = *firstp = cUNOPo->op_first;
3055 first->op_next = o->op_next;
3056 cUNOPo->op_first = Nullop;
3060 if (first->op_type == OP_CONST) {
3061 if (ckWARN(WARN_PRECEDENCE) && (first->op_private & OPpCONST_BARE))
3062 warner(WARN_PRECEDENCE, "Probable precedence problem on %s",
3064 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
3075 else if (first->op_type == OP_WANTARRAY) {
3081 else if (ckWARN(WARN_UNSAFE) && (first->op_flags & OPf_KIDS)) {
3082 OP *k1 = ((UNOP*)first)->op_first;
3083 OP *k2 = k1->op_sibling;
3085 switch (first->op_type)
3088 if (k2 && k2->op_type == OP_READLINE
3089 && (k2->op_flags & OPf_STACKED)
3090 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3091 warnop = k2->op_type;
3095 if (k1->op_type == OP_READDIR
3096 || k1->op_type == OP_GLOB
3097 || k1->op_type == OP_EACH)
3098 warnop = k1->op_type;
3102 line_t oldline = PL_curcop->cop_line;
3103 PL_curcop->cop_line = PL_copline;
3105 "Value of %s%s can be \"0\"; test with defined()",
3107 ((warnop == OP_READLINE || warnop == OP_GLOB)
3108 ? " construct" : "() operator"));
3109 PL_curcop->cop_line = oldline;
3116 if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
3117 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
3119 Newz(1101, logop, 1, LOGOP);
3121 logop->op_type = type;
3122 logop->op_ppaddr = PL_ppaddr[type];
3123 logop->op_first = first;
3124 logop->op_flags = flags | OPf_KIDS;
3125 logop->op_other = LINKLIST(other);
3126 logop->op_private = 1 | (flags >> 8);
3128 /* establish postfix order */
3129 logop->op_next = LINKLIST(first);
3130 first->op_next = (OP*)logop;
3131 first->op_sibling = other;
3133 o = newUNOP(OP_NULL, 0, (OP*)logop);
3140 newCONDOP(I32 flags, OP *first, OP *trueop, OP *falseop)
3147 return newLOGOP(OP_AND, 0, first, trueop);
3149 return newLOGOP(OP_OR, 0, first, falseop);
3151 scalarboolean(first);
3152 if (first->op_type == OP_CONST) {
3153 if (SvTRUE(((SVOP*)first)->op_sv)) {
3164 else if (first->op_type == OP_WANTARRAY) {
3168 Newz(1101, condop, 1, CONDOP);
3170 condop->op_type = OP_COND_EXPR;
3171 condop->op_ppaddr = PL_ppaddr[OP_COND_EXPR];
3172 condop->op_first = first;
3173 condop->op_flags = flags | OPf_KIDS;
3174 condop->op_true = LINKLIST(trueop);
3175 condop->op_false = LINKLIST(falseop);
3176 condop->op_private = 1 | (flags >> 8);
3178 /* establish postfix order */
3179 condop->op_next = LINKLIST(first);
3180 first->op_next = (OP*)condop;
3182 first->op_sibling = trueop;
3183 trueop->op_sibling = falseop;
3184 o = newUNOP(OP_NULL, 0, (OP*)condop);
3186 trueop->op_next = o;
3187 falseop->op_next = o;
3193 newRANGE(I32 flags, OP *left, OP *right)
3201 Newz(1101, condop, 1, CONDOP);
3203 condop->op_type = OP_RANGE;
3204 condop->op_ppaddr = PL_ppaddr[OP_RANGE];
3205 condop->op_first = left;
3206 condop->op_flags = OPf_KIDS;
3207 condop->op_true = LINKLIST(left);
3208 condop->op_false = LINKLIST(right);
3209 condop->op_private = 1 | (flags >> 8);
3211 left->op_sibling = right;
3213 condop->op_next = (OP*)condop;
3214 flip = newUNOP(OP_FLIP, flags, (OP*)condop);
3215 flop = newUNOP(OP_FLOP, 0, flip);
3216 o = newUNOP(OP_NULL, 0, flop);
3219 left->op_next = flip;
3220 right->op_next = flop;
3222 condop->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3223 sv_upgrade(PAD_SV(condop->op_targ), SVt_PVNV);
3224 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3225 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
3227 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3228 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3231 if (!flip->op_private || !flop->op_private)
3232 linklist(o); /* blow off optimizer unless constant */
3238 newLOOPOP(I32 flags, I32 debuggable, OP *expr, OP *block)
3243 int once = block && block->op_flags & OPf_SPECIAL &&
3244 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
3247 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
3248 return block; /* do {} while 0 does once */
3249 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3250 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
3251 expr = newUNOP(OP_DEFINED, 0,
3252 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3253 } else if (expr->op_flags & OPf_KIDS) {
3254 OP *k1 = ((UNOP*)expr)->op_first;
3255 OP *k2 = (k1) ? k1->op_sibling : NULL;
3256 switch (expr->op_type) {
3258 if (k2 && k2->op_type == OP_READLINE
3259 && (k2->op_flags & OPf_STACKED)
3260 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3261 expr = newUNOP(OP_DEFINED, 0, expr);
3265 if (k1->op_type == OP_READDIR
3266 || k1->op_type == OP_GLOB
3267 || k1->op_type == OP_EACH)
3268 expr = newUNOP(OP_DEFINED, 0, expr);
3274 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
3275 o = new_logop(OP_AND, 0, &expr, &listop);
3278 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
3280 if (once && o != listop)
3281 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
3284 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
3286 o->op_flags |= flags;
3288 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
3293 newWHILEOP(I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *block, OP *cont)
3302 if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3303 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB))) {
3304 expr = newUNOP(OP_DEFINED, 0,
3305 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3306 } else if (expr && (expr->op_flags & OPf_KIDS)) {
3307 OP *k1 = ((UNOP*)expr)->op_first;
3308 OP *k2 = (k1) ? k1->op_sibling : NULL;
3309 switch (expr->op_type) {
3311 if (k2 && k2->op_type == OP_READLINE
3312 && (k2->op_flags & OPf_STACKED)
3313 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3314 expr = newUNOP(OP_DEFINED, 0, expr);
3318 if (k1->op_type == OP_READDIR
3319 || k1->op_type == OP_GLOB
3320 || k1->op_type == OP_EACH)
3321 expr = newUNOP(OP_DEFINED, 0, expr);
3327 block = newOP(OP_NULL, 0);
3330 next = LINKLIST(cont);
3332 cont = append_elem(OP_LINESEQ, cont, newOP(OP_UNSTACK, 0));
3333 if ((line_t)whileline != NOLINE) {
3334 PL_copline = whileline;
3335 cont = append_elem(OP_LINESEQ, cont,
3336 newSTATEOP(0, Nullch, Nullop));
3340 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
3341 redo = LINKLIST(listop);
3344 PL_copline = whileline;
3346 o = new_logop(OP_AND, 0, &expr, &listop);
3347 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
3348 op_free(expr); /* oops, it's a while (0) */
3350 return Nullop; /* listop already freed by new_logop */
3353 ((LISTOP*)listop)->op_last->op_next = condop =
3354 (o == listop ? redo : LINKLIST(o));
3362 Newz(1101,loop,1,LOOP);
3363 loop->op_type = OP_ENTERLOOP;
3364 loop->op_ppaddr = PL_ppaddr[OP_ENTERLOOP];
3365 loop->op_private = 0;
3366 loop->op_next = (OP*)loop;
3369 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
3371 loop->op_redoop = redo;
3372 loop->op_lastop = o;
3375 loop->op_nextop = next;
3377 loop->op_nextop = o;
3379 o->op_flags |= flags;
3380 o->op_private |= (flags >> 8);
3385 newFOROP(I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
3393 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
3394 sv->op_type = OP_RV2GV;
3395 sv->op_ppaddr = PL_ppaddr[OP_RV2GV];
3397 else if (sv->op_type == OP_PADSV) { /* private variable */
3398 padoff = sv->op_targ;
3402 else if (sv->op_type == OP_THREADSV) { /* per-thread variable */
3403 padoff = sv->op_targ;
3404 iterflags |= OPf_SPECIAL;
3409 croak("Can't use %s for loop variable", PL_op_desc[sv->op_type]);
3413 padoff = find_threadsv("_");
3414 iterflags |= OPf_SPECIAL;
3416 sv = newGVOP(OP_GV, 0, PL_defgv);
3419 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
3420 expr = mod(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART);
3421 iterflags |= OPf_STACKED;
3423 else if (expr->op_type == OP_NULL &&
3424 (expr->op_flags & OPf_KIDS) &&
3425 ((BINOP*)expr)->op_first->op_type == OP_FLOP)
3427 /* Basically turn for($x..$y) into the same as for($x,$y), but we
3428 * set the STACKED flag to indicate that these values are to be
3429 * treated as min/max values by 'pp_iterinit'.
3431 UNOP* flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
3432 CONDOP* range = (CONDOP*) flip->op_first;
3433 OP* left = range->op_first;
3434 OP* right = left->op_sibling;
3437 range->op_flags &= ~OPf_KIDS;
3438 range->op_first = Nullop;
3440 listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
3441 listop->op_first->op_next = range->op_true;
3442 left->op_next = range->op_false;
3443 right->op_next = (OP*)listop;
3444 listop->op_next = listop->op_first;
3447 expr = (OP*)(listop);
3449 iterflags |= OPf_STACKED;
3452 expr = mod(force_list(expr), OP_GREPSTART);
3456 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
3457 append_elem(OP_LIST, expr, scalar(sv))));
3458 assert(!loop->op_next);
3459 Renew(loop, 1, LOOP);
3460 loop->op_targ = padoff;
3461 wop = newWHILEOP(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont);
3462 PL_copline = forline;
3463 return newSTATEOP(0, label, wop);
3467 newLOOPEX(I32 type, OP *label)
3473 if (type != OP_GOTO || label->op_type == OP_CONST) {
3474 /* "last()" means "last" */
3475 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS))
3476 o = newOP(type, OPf_SPECIAL);
3478 o = newPVOP(type, 0, savepv(label->op_type == OP_CONST
3479 ? SvPVx(((SVOP*)label)->op_sv, n_a)
3485 if (label->op_type == OP_ENTERSUB)
3486 label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
3487 o = newUNOP(type, OPf_STACKED, label);
3489 PL_hints |= HINT_BLOCK_SCOPE;
3499 MUTEX_DESTROY(CvMUTEXP(cv));
3500 Safefree(CvMUTEXP(cv));
3503 #endif /* USE_THREADS */
3505 if (!CvXSUB(cv) && CvROOT(cv)) {
3507 if (CvDEPTH(cv) || (CvOWNER(cv) && CvOWNER(cv) != thr))
3508 croak("Can't undef active subroutine");
3511 croak("Can't undef active subroutine");
3512 #endif /* USE_THREADS */
3515 SAVESPTR(PL_curpad);
3519 op_free(CvROOT(cv));
3520 CvROOT(cv) = Nullop;
3523 SvPOK_off((SV*)cv); /* forget prototype */
3525 SvREFCNT_dec(CvGV(cv));
3527 SvREFCNT_dec(CvOUTSIDE(cv));
3528 CvOUTSIDE(cv) = Nullcv;
3529 if (CvPADLIST(cv)) {
3530 /* may be during global destruction */
3531 if (SvREFCNT(CvPADLIST(cv))) {
3532 I32 i = AvFILLp(CvPADLIST(cv));
3534 SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE);
3535 SV* sv = svp ? *svp : Nullsv;
3538 if (sv == (SV*)PL_comppad_name)
3539 PL_comppad_name = Nullav;
3540 else if (sv == (SV*)PL_comppad) {
3541 PL_comppad = Nullav;
3542 PL_curpad = Null(SV**);
3546 SvREFCNT_dec((SV*)CvPADLIST(cv));
3548 CvPADLIST(cv) = Nullav;
3552 #ifdef DEBUG_CLOSURES
3557 CV *outside = CvOUTSIDE(cv);
3558 AV* padlist = CvPADLIST(cv);
3565 PerlIO_printf(Perl_debug_log, "\tCV=0x%lx (%s), OUTSIDE=0x%lx (%s)\n",
3567 (CvANON(cv) ? "ANON"
3568 : (cv == PL_main_cv) ? "MAIN"
3569 : CvUNIQUE(cv) ? "UNIQUE"
3570 : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"),
3573 : CvANON(outside) ? "ANON"
3574 : (outside == PL_main_cv) ? "MAIN"
3575 : CvUNIQUE(outside) ? "UNIQUE"
3576 : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED"));
3581 pad_name = (AV*)*av_fetch(padlist, 0, FALSE);
3582 pad = (AV*)*av_fetch(padlist, 1, FALSE);
3583 pname = AvARRAY(pad_name);
3584 ppad = AvARRAY(pad);
3586 for (ix = 1; ix <= AvFILLp(pad_name); ix++) {
3587 if (SvPOK(pname[ix]))
3588 PerlIO_printf(Perl_debug_log, "\t%4d. 0x%lx (%s\"%s\" %ld-%ld)\n",
3590 SvFAKE(pname[ix]) ? "FAKE " : "",
3592 (long)I_32(SvNVX(pname[ix])),
3593 (long)SvIVX(pname[ix]));
3596 #endif /* DEBUG_CLOSURES */
3599 cv_clone2(CV *proto, CV *outside)
3604 AV* protopadlist = CvPADLIST(proto);
3605 AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE);
3606 AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE);
3607 SV** pname = AvARRAY(protopad_name);
3608 SV** ppad = AvARRAY(protopad);
3609 I32 fname = AvFILLp(protopad_name);
3610 I32 fpad = AvFILLp(protopad);
3614 assert(!CvUNIQUE(proto));
3617 SAVESPTR(PL_curpad);
3618 SAVESPTR(PL_comppad);
3619 SAVESPTR(PL_comppad_name);
3620 SAVESPTR(PL_compcv);
3622 cv = PL_compcv = (CV*)NEWSV(1104,0);
3623 sv_upgrade((SV *)cv, SvTYPE(proto));
3629 New(666, CvMUTEXP(cv), 1, perl_mutex);
3630 MUTEX_INIT(CvMUTEXP(cv));
3632 #endif /* USE_THREADS */
3633 CvFILEGV(cv) = CvFILEGV(proto);
3634 CvGV(cv) = (GV*)SvREFCNT_inc(CvGV(proto));
3635 CvSTASH(cv) = CvSTASH(proto);
3636 CvROOT(cv) = CvROOT(proto);
3637 CvSTART(cv) = CvSTART(proto);
3639 CvOUTSIDE(cv) = (CV*)SvREFCNT_inc(outside);
3642 sv_setpvn((SV*)cv, SvPVX(proto), SvCUR(proto));
3644 PL_comppad_name = newAV();
3645 for (ix = fname; ix >= 0; ix--)
3646 av_store(PL_comppad_name, ix, SvREFCNT_inc(pname[ix]));
3648 PL_comppad = newAV();
3650 comppadlist = newAV();
3651 AvREAL_off(comppadlist);
3652 av_store(comppadlist, 0, (SV*)PL_comppad_name);
3653 av_store(comppadlist, 1, (SV*)PL_comppad);
3654 CvPADLIST(cv) = comppadlist;
3655 av_fill(PL_comppad, AvFILLp(protopad));
3656 PL_curpad = AvARRAY(PL_comppad);
3658 av = newAV(); /* will be @_ */
3660 av_store(PL_comppad, 0, (SV*)av);
3661 AvFLAGS(av) = AVf_REIFY;
3663 for (ix = fpad; ix > 0; ix--) {
3664 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
3665 if (namesv && namesv != &PL_sv_undef) {
3666 char *name = SvPVX(namesv); /* XXX */
3667 if (SvFLAGS(namesv) & SVf_FAKE) { /* lexical from outside? */
3668 I32 off = pad_findlex(name, ix, SvIVX(namesv),
3669 CvOUTSIDE(cv), cxstack_ix, 0);
3671 PL_curpad[ix] = SvREFCNT_inc(ppad[ix]);
3673 croak("panic: cv_clone: %s", name);
3675 else { /* our own lexical */
3678 /* anon code -- we'll come back for it */
3679 sv = SvREFCNT_inc(ppad[ix]);
3681 else if (*name == '@')
3683 else if (*name == '%')
3693 SV* sv = NEWSV(0,0);
3699 /* Now that vars are all in place, clone nested closures. */
3701 for (ix = fpad; ix > 0; ix--) {
3702 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
3704 && namesv != &PL_sv_undef
3705 && !(SvFLAGS(namesv) & SVf_FAKE)
3706 && *SvPVX(namesv) == '&'
3707 && CvCLONE(ppad[ix]))
3709 CV *kid = cv_clone2((CV*)ppad[ix], cv);
3710 SvREFCNT_dec(ppad[ix]);
3713 PL_curpad[ix] = (SV*)kid;
3717 #ifdef DEBUG_CLOSURES
3718 PerlIO_printf(Perl_debug_log, "Cloned inside:\n");
3720 PerlIO_printf(Perl_debug_log, " from:\n");
3722 PerlIO_printf(Perl_debug_log, " to:\n");
3734 MUTEX_LOCK(&PL_cred_mutex); /* XXX create separate mutex */
3735 cv = cv_clone2(proto, CvOUTSIDE(proto));
3736 MUTEX_UNLOCK(&PL_cred_mutex); /* XXX create separate mutex */
3741 cv_ckproto(CV *cv, GV *gv, char *p)
3743 if ((!p != !SvPOK(cv)) || (p && strNE(p, SvPVX(cv)))) {
3744 SV* msg = sv_newmortal();
3748 gv_efullname3(name = sv_newmortal(), gv, Nullch);
3749 sv_setpv(msg, "Prototype mismatch:");
3751 sv_catpvf(msg, " sub %_", name);
3753 sv_catpvf(msg, " (%s)", SvPVX(cv));
3754 sv_catpv(msg, " vs ");
3756 sv_catpvf(msg, "(%s)", p);
3758 sv_catpv(msg, "none");
3766 if (!cv || !SvPOK(cv) || SvCUR(cv))
3768 return op_const_sv(CvSTART(cv), cv);
3772 op_const_sv(OP *o, CV *cv)
3779 if(o->op_type == OP_LINESEQ && cLISTOPo->op_first)
3780 o = cLISTOPo->op_first->op_sibling;
3782 for (; o; o = o->op_next) {
3783 OPCODE type = o->op_type;
3785 if(sv && o->op_next == o)
3787 if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
3789 if (type == OP_LEAVESUB || type == OP_RETURN)
3793 if (type == OP_CONST)
3795 else if (type == OP_PADSV && cv) {
3796 AV* padav = (AV*)(AvARRAY(CvPADLIST(cv))[1]);
3797 sv = padav ? AvARRAY(padav)[o->op_targ] : Nullsv;
3798 if (!sv || (!SvREADONLY(sv) && SvREFCNT(sv) > 1))
3810 newSUB(I32 floor, OP *o, OP *proto, OP *block)
3814 char *name = o ? SvPVx(cSVOPo->op_sv, n_a) : Nullch;
3815 GV *gv = gv_fetchpv(name ? name : "__ANON__",
3816 GV_ADDMULTI | (block ? 0 : GV_NOINIT), SVt_PVCV);
3817 char *ps = proto ? SvPVx(((SVOP*)proto)->op_sv, n_a) : Nullch;
3826 if (SvTYPE(gv) != SVt_PVGV) { /* Prototype now, and had
3827 maximum a prototype before. */
3828 if (SvTYPE(gv) > SVt_NULL) {
3829 if (!SvPOK((SV*)gv) && !(SvIOK((SV*)gv) && SvIVX((SV*)gv) == -1))
3830 warn("Runaway prototype");
3831 cv_ckproto((CV*)gv, NULL, ps);
3834 sv_setpv((SV*)gv, ps);
3836 sv_setiv((SV*)gv, -1);
3837 SvREFCNT_dec(PL_compcv);
3838 cv = PL_compcv = NULL;
3839 PL_sub_generation++;
3843 if (!name || GvCVGEN(gv))
3845 else if (cv = GvCV(gv)) {
3846 cv_ckproto(cv, gv, ps);
3847 /* already defined (or promised)? */
3848 if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
3850 bool const_changed = TRUE;
3852 /* just a "sub foo;" when &foo is already defined */
3853 SAVEFREESV(PL_compcv);
3856 /* ahem, death to those who redefine active sort subs */
3857 if (PL_curstackinfo->si_type == PERLSI_SORT && PL_sortcop == CvSTART(cv))
3858 croak("Can't redefine active sort subroutine %s", name);
3859 if(const_sv = cv_const_sv(cv))
3860 const_changed = sv_cmp(const_sv, op_const_sv(block, Nullcv));
3861 if ((const_sv && const_changed) || ckWARN(WARN_REDEFINE)
3862 && !(CvGV(cv) && GvSTASH(CvGV(cv))
3863 && HvNAME(GvSTASH(CvGV(cv)))
3864 && strEQ(HvNAME(GvSTASH(CvGV(cv))),
3866 line_t oldline = PL_curcop->cop_line;
3867 PL_curcop->cop_line = PL_copline;
3868 warner(WARN_REDEFINE,
3869 const_sv ? "Constant subroutine %s redefined"
3870 : "Subroutine %s redefined", name);
3871 PL_curcop->cop_line = oldline;
3877 if (cv) { /* must reuse cv if autoloaded */
3879 CvFLAGS(cv) = CvFLAGS(PL_compcv);
3880 CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv);
3881 CvOUTSIDE(PL_compcv) = 0;
3882 CvPADLIST(cv) = CvPADLIST(PL_compcv);
3883 CvPADLIST(PL_compcv) = 0;
3884 if (SvREFCNT(PL_compcv) > 1) /* XXX Make closures transit through stub. */
3885 CvOUTSIDE(PL_compcv) = (CV*)SvREFCNT_inc((SV*)cv);
3886 SvREFCNT_dec(PL_compcv);
3893 PL_sub_generation++;
3896 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
3897 CvFILEGV(cv) = PL_curcop->cop_filegv;
3898 CvSTASH(cv) = PL_curstash;
3901 if (!CvMUTEXP(cv)) {
3902 New(666, CvMUTEXP(cv), 1, perl_mutex);
3903 MUTEX_INIT(CvMUTEXP(cv));
3905 #endif /* USE_THREADS */
3908 sv_setpv((SV*)cv, ps);
3910 if (PL_error_count) {
3914 char *s = strrchr(name, ':');
3916 if (strEQ(s, "BEGIN")) {
3918 "BEGIN not safe after errors--compilation aborted";
3922 /* force display of errors found but not reported */
3923 sv_catpv(ERRSV, not_safe);
3924 croak("%s", SvPVx(ERRSV, n_a));
3931 PL_copline = NOLINE;
3936 if (AvFILLp(PL_comppad_name) < AvFILLp(PL_comppad))
3937 av_store(PL_comppad_name, AvFILLp(PL_comppad), Nullsv);
3940 SV **namep = AvARRAY(PL_comppad_name);
3941 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
3944 if (SvIMMORTAL(PL_curpad[ix]))
3947 * The only things that a clonable function needs in its
3948 * pad are references to outer lexicals and anonymous subs.
3949 * The rest are created anew during cloning.
3951 if (!((namesv = namep[ix]) != Nullsv &&
3952 namesv != &PL_sv_undef &&
3954 *SvPVX(namesv) == '&')))
3956 SvREFCNT_dec(PL_curpad[ix]);
3957 PL_curpad[ix] = Nullsv;
3962 AV *av = newAV(); /* Will be @_ */
3964 av_store(PL_comppad, 0, (SV*)av);
3965 AvFLAGS(av) = AVf_REIFY;
3967 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
3968 if (SvIMMORTAL(PL_curpad[ix]))
3970 if (!SvPADMY(PL_curpad[ix]))
3971 SvPADTMP_on(PL_curpad[ix]);
3975 CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
3976 CvSTART(cv) = LINKLIST(CvROOT(cv));
3977 CvROOT(cv)->op_next = 0;
3983 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
3984 SV *sv = NEWSV(0,0);
3985 SV *tmpstr = sv_newmortal();
3986 GV *db_postponed = gv_fetchpv("DB::postponed", GV_ADDMULTI, SVt_PVHV);
3990 sv_setpvf(sv, "%_:%ld-%ld",
3991 GvSV(PL_curcop->cop_filegv),
3992 (long)PL_subline, (long)PL_curcop->cop_line);
3993 gv_efullname3(tmpstr, gv, Nullch);
3994 hv_store(GvHV(PL_DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0);
3995 hv = GvHVn(db_postponed);
3996 if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX(tmpstr), SvCUR(tmpstr))
3997 && (cv = GvCV(db_postponed))) {
4002 perl_call_sv((SV*)cv, G_DISCARD);
4006 if ((s = strrchr(name,':')))
4010 if (strEQ(s, "BEGIN")) {
4011 I32 oldscope = PL_scopestack_ix;
4013 SAVESPTR(PL_compiling.cop_filegv);
4014 SAVEI16(PL_compiling.cop_line);
4016 sv_setsv(PL_rs, PL_nrs);
4019 PL_beginav = newAV();
4020 DEBUG_x( dump_sub(gv) );
4021 av_push(PL_beginav, (SV *)cv);
4023 call_list(oldscope, PL_beginav);
4025 PL_curcop = &PL_compiling;
4026 PL_compiling.op_private = PL_hints;
4029 else if (strEQ(s, "END") && !PL_error_count) {
4032 av_unshift(PL_endav, 1);
4033 av_store(PL_endav, 0, (SV *)cv);
4036 else if (strEQ(s, "INIT") && !PL_error_count) {
4038 PL_initav = newAV();
4039 av_push(PL_initav, SvREFCNT_inc(cv));
4045 PL_copline = NOLINE;
4050 /* XXX unsafe for threads if eval_owner isn't held */
4052 newCONSTSUB(HV *stash, char *name, SV *sv)
4055 U32 oldhints = PL_hints;
4056 HV *old_cop_stash = PL_curcop->cop_stash;
4057 HV *old_curstash = PL_curstash;
4058 line_t oldline = PL_curcop->cop_line;
4059 PL_curcop->cop_line = PL_copline;
4061 PL_hints &= ~HINT_BLOCK_SCOPE;
4063 PL_curstash = PL_curcop->cop_stash = stash;
4066 start_subparse(FALSE, 0),
4067 newSVOP(OP_CONST, 0, newSVpv(name,0)),
4068 newSVOP(OP_CONST, 0, &PL_sv_no), /* SvPV(&PL_sv_no) == "" -- GMB */
4069 newSTATEOP(0, Nullch, newSVOP(OP_CONST, 0, sv))
4072 PL_hints = oldhints;
4073 PL_curcop->cop_stash = old_cop_stash;
4074 PL_curstash = old_curstash;
4075 PL_curcop->cop_line = oldline;
4079 newXS(char *name, void (*subaddr) (CV * _CPERLproto), char *filename)
4082 GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV);
4085 if (cv = (name ? GvCV(gv) : Nullcv)) {
4087 /* just a cached method */
4091 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
4092 /* already defined (or promised) */
4093 if (ckWARN(WARN_REDEFINE) && !(CvGV(cv) && GvSTASH(CvGV(cv))
4094 && HvNAME(GvSTASH(CvGV(cv)))
4095 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "autouse"))) {
4096 line_t oldline = PL_curcop->cop_line;
4097 if (PL_copline != NOLINE)
4098 PL_curcop->cop_line = PL_copline;
4099 warner(WARN_REDEFINE, "Subroutine %s redefined",name);
4100 PL_curcop->cop_line = oldline;
4107 if (cv) /* must reuse cv if autoloaded */
4110 cv = (CV*)NEWSV(1105,0);
4111 sv_upgrade((SV *)cv, SVt_PVCV);
4115 PL_sub_generation++;
4118 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4120 New(666, CvMUTEXP(cv), 1, perl_mutex);
4121 MUTEX_INIT(CvMUTEXP(cv));
4123 #endif /* USE_THREADS */
4124 CvFILEGV(cv) = gv_fetchfile(filename);
4125 CvXSUB(cv) = subaddr;
4128 char *s = strrchr(name,':');
4133 if (strEQ(s, "BEGIN")) {
4135 PL_beginav = newAV();
4136 av_push(PL_beginav, (SV *)cv);
4139 else if (strEQ(s, "END")) {
4142 av_unshift(PL_endav, 1);
4143 av_store(PL_endav, 0, (SV *)cv);
4146 else if (strEQ(s, "INIT")) {
4148 PL_initav = newAV();
4149 av_push(PL_initav, (SV *)cv);
4160 newFORM(I32 floor, OP *o, OP *block)
4170 name = SvPVx(cSVOPo->op_sv, n_a);
4173 gv = gv_fetchpv(name,TRUE, SVt_PVFM);
4175 if (cv = GvFORM(gv)) {
4176 if (ckWARN(WARN_REDEFINE)) {
4177 line_t oldline = PL_curcop->cop_line;
4179 PL_curcop->cop_line = PL_copline;
4180 warner(WARN_REDEFINE, "Format %s redefined",name);
4181 PL_curcop->cop_line = oldline;
4187 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4188 CvFILEGV(cv) = PL_curcop->cop_filegv;
4190 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4191 if (!SvPADMY(PL_curpad[ix]) && !SvIMMORTAL(PL_curpad[ix]))
4192 SvPADTMP_on(PL_curpad[ix]);
4195 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
4196 CvSTART(cv) = LINKLIST(CvROOT(cv));
4197 CvROOT(cv)->op_next = 0;
4200 PL_copline = NOLINE;
4207 return newUNOP(OP_REFGEN, 0,
4208 mod(list(convert(OP_ANONLIST, 0, o)), OP_REFGEN));
4214 return newUNOP(OP_REFGEN, 0,
4215 mod(list(convert(OP_ANONHASH, 0, o)), OP_REFGEN));
4219 newANONSUB(I32 floor, OP *proto, OP *block)
4221 return newUNOP(OP_REFGEN, 0,
4222 newSVOP(OP_ANONCODE, 0, (SV*)newSUB(floor, 0, proto, block)));
4228 switch (o->op_type) {
4230 o->op_type = OP_PADAV;
4231 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4232 return ref(o, OP_RV2AV);
4235 o->op_type = OP_RV2AV;
4236 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
4241 warn("oops: oopsAV");
4250 switch (o->op_type) {
4253 o->op_type = OP_PADHV;
4254 o->op_ppaddr = PL_ppaddr[OP_PADHV];
4255 return ref(o, OP_RV2HV);
4259 o->op_type = OP_RV2HV;
4260 o->op_ppaddr = PL_ppaddr[OP_RV2HV];
4265 warn("oops: oopsHV");
4274 if (o->op_type == OP_PADANY) {
4275 o->op_type = OP_PADAV;
4276 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4279 return newUNOP(OP_RV2AV, 0, scalar(o));
4283 newGVREF(I32 type, OP *o)
4285 if (type == OP_MAPSTART)
4286 return newUNOP(OP_NULL, 0, o);
4287 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
4293 if (o->op_type == OP_PADANY) {
4294 o->op_type = OP_PADHV;
4295 o->op_ppaddr = PL_ppaddr[OP_PADHV];
4298 return newUNOP(OP_RV2HV, 0, scalar(o));
4304 croak("NOT IMPL LINE %d",__LINE__);
4310 newCVREF(I32 flags, OP *o)
4312 return newUNOP(OP_RV2CV, flags, scalar(o));
4318 if (o->op_type == OP_PADANY) {
4319 o->op_type = OP_PADSV;
4320 o->op_ppaddr = PL_ppaddr[OP_PADSV];
4323 else if (o->op_type == OP_THREADSV && !(o->op_flags & OPpDONE_SVREF)) {
4324 o->op_flags |= OPpDONE_SVREF;
4327 return newUNOP(OP_RV2SV, 0, scalar(o));
4330 /* Check routines. */
4338 name = NEWSV(1106,0);
4339 sv_upgrade(name, SVt_PVNV);
4340 sv_setpvn(name, "&", 1);
4343 ix = pad_alloc(o->op_type, SVs_PADMY);
4344 av_store(PL_comppad_name, ix, name);
4345 av_store(PL_comppad, ix, cSVOPo->op_sv);
4346 SvPADMY_on(cSVOPo->op_sv);
4347 cSVOPo->op_sv = Nullsv;
4348 cSVOPo->op_targ = ix;
4355 o->op_private = PL_hints;
4362 if (cUNOPo->op_first->op_type == OP_CONCAT)
4363 o->op_flags |= OPf_STACKED;
4370 if (o->op_flags & OPf_KIDS) {
4373 OPCODE type = o->op_type;
4374 o = modkids(ck_fun(o), type);
4375 kid = cUNOPo->op_first;
4376 newop = kUNOP->op_first->op_sibling;
4378 (newop->op_sibling ||
4379 !(PL_opargs[newop->op_type] & OA_RETSCALAR) ||
4380 newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
4381 newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
4385 op_free(kUNOP->op_first);
4386 kUNOP->op_first = newop;
4388 o->op_ppaddr = PL_ppaddr[++o->op_type];
4397 if (o->op_flags & OPf_KIDS) {
4398 OP *kid = cUNOPo->op_first;
4399 if (kid->op_type == OP_HSLICE)
4400 o->op_private |= OPpSLICE;
4401 else if (kid->op_type != OP_HELEM)
4402 croak("%s argument is not a HASH element or slice",
4403 PL_op_desc[o->op_type]);
4412 I32 type = o->op_type;
4414 if (o->op_flags & OPf_KIDS) {
4415 if (cLISTOPo->op_first->op_type == OP_STUB) {
4417 o = newUNOP(type, OPf_SPECIAL,
4418 newGVOP(OP_GV, 0, gv_fetchpv("main::ARGV", TRUE, SVt_PVAV)));
4428 PL_hints |= HINT_BLOCK_SCOPE;
4429 if (o->op_flags & OPf_KIDS) {
4430 SVOP *kid = (SVOP*)cUNOPo->op_first;
4433 o->op_flags &= ~OPf_KIDS;
4436 else if (kid->op_type == OP_LINESEQ) {
4439 kid->op_next = o->op_next;
4440 cUNOPo->op_first = 0;
4443 Newz(1101, enter, 1, LOGOP);
4444 enter->op_type = OP_ENTERTRY;
4445 enter->op_ppaddr = PL_ppaddr[OP_ENTERTRY];
4446 enter->op_private = 0;
4448 /* establish postfix order */
4449 enter->op_next = (OP*)enter;
4451 o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
4452 o->op_type = OP_LEAVETRY;
4453 o->op_ppaddr = PL_ppaddr[OP_LEAVETRY];
4454 enter->op_other = o;
4462 o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP());
4464 o->op_targ = (PADOFFSET)PL_hints;
4472 if (o->op_flags & OPf_STACKED) {
4474 kid = cUNOPo->op_first->op_sibling;
4475 if (kid->op_type == OP_RV2GV)
4487 if (o->op_flags & OPf_KIDS) {
4488 OP *kid = cUNOPo->op_first;
4489 if (kid->op_type != OP_HELEM)
4490 croak("%s argument is not a HASH element", PL_op_desc[o->op_type]);
4498 ck_gvconst(register OP *o)
4500 o = fold_constants(o);
4501 if (o->op_type == OP_CONST)
4508 ck_rvconst(register OP *o)
4511 SVOP *kid = (SVOP*)cUNOPo->op_first;
4513 o->op_private |= (PL_hints & HINT_STRICT_REFS);
4514 if (kid->op_type == OP_CONST) {
4518 SV *kidsv = kid->op_sv;
4521 /* Is it a constant from cv_const_sv()? */
4522 if (SvROK(kidsv) && SvREADONLY(kidsv)) {
4523 SV *rsv = SvRV(kidsv);
4524 int svtype = SvTYPE(rsv);
4525 char *badtype = Nullch;
4527 switch (o->op_type) {
4529 if (svtype > SVt_PVMG)
4530 badtype = "a SCALAR";
4533 if (svtype != SVt_PVAV)
4534 badtype = "an ARRAY";
4537 if (svtype != SVt_PVHV) {
4538 if (svtype == SVt_PVAV) { /* pseudohash? */
4539 SV **ksv = av_fetch((AV*)rsv, 0, FALSE);
4540 if (ksv && SvROK(*ksv)
4541 && SvTYPE(SvRV(*ksv)) == SVt_PVHV)
4550 if (svtype != SVt_PVCV)
4555 croak("Constant is not %s reference", badtype);
4558 name = SvPV(kidsv, n_a);
4559 if ((PL_hints & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
4560 char *badthing = Nullch;
4561 switch (o->op_type) {
4563 badthing = "a SCALAR";
4566 badthing = "an ARRAY";
4569 badthing = "a HASH";
4574 "Can't use bareword (\"%s\") as %s ref while \"strict refs\" in use",
4578 * This is a little tricky. We only want to add the symbol if we
4579 * didn't add it in the lexer. Otherwise we get duplicate strict
4580 * warnings. But if we didn't add it in the lexer, we must at
4581 * least pretend like we wanted to add it even if it existed before,
4582 * or we get possible typo warnings. OPpCONST_ENTERED says
4583 * whether the lexer already added THIS instance of this symbol.
4585 iscv = (o->op_type == OP_RV2CV) * 2;
4587 gv = gv_fetchpv(name,
4588 iscv | !(kid->op_private & OPpCONST_ENTERED),
4591 : o->op_type == OP_RV2SV
4593 : o->op_type == OP_RV2AV
4595 : o->op_type == OP_RV2HV
4598 } while (!gv && !(kid->op_private & OPpCONST_ENTERED) && !iscv++);
4600 kid->op_type = OP_GV;
4601 SvREFCNT_dec(kid->op_sv);
4602 kid->op_sv = SvREFCNT_inc(gv);
4612 I32 type = o->op_type;
4614 if (o->op_flags & OPf_REF)
4617 if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
4618 SVOP *kid = (SVOP*)cUNOPo->op_first;
4620 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
4622 OP *newop = newGVOP(type, OPf_REF,
4623 gv_fetchpv(SvPVx(kid->op_sv, n_a), TRUE, SVt_PVIO));
4630 if (type == OP_FTTTY)
4631 return newGVOP(type, OPf_REF, gv_fetchpv("main::STDIN", TRUE,
4634 return newUNOP(type, 0, newDEFSVOP());
4647 int type = o->op_type;
4648 register I32 oa = PL_opargs[type] >> OASHIFT;
4650 if (o->op_flags & OPf_STACKED) {
4651 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
4654 return no_fh_allowed(o);
4657 if (o->op_flags & OPf_KIDS) {
4659 tokid = &cLISTOPo->op_first;
4660 kid = cLISTOPo->op_first;
4661 if (kid->op_type == OP_PUSHMARK ||
4662 kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK)
4664 tokid = &kid->op_sibling;
4665 kid = kid->op_sibling;
4667 if (!kid && PL_opargs[type] & OA_DEFGV)
4668 *tokid = kid = newDEFSVOP();
4672 sibl = kid->op_sibling;
4675 /* list seen where single (scalar) arg expected? */
4676 if (numargs == 1 && !(oa >> 4)
4677 && kid->op_type == OP_LIST && type != OP_SCALAR)
4679 return too_many_arguments(o,PL_op_desc[type]);
4692 if (kid->op_type == OP_CONST &&
4693 (kid->op_private & OPpCONST_BARE))
4695 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
4696 OP *newop = newAVREF(newGVOP(OP_GV, 0,
4697 gv_fetchpv(name, TRUE, SVt_PVAV) ));
4698 if (ckWARN(WARN_SYNTAX))
4700 "Array @%s missing the @ in argument %ld of %s()",
4701 name, (long)numargs, PL_op_desc[type]);
4704 kid->op_sibling = sibl;
4707 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
4708 bad_type(numargs, "array", PL_op_desc[type], kid);
4712 if (kid->op_type == OP_CONST &&
4713 (kid->op_private & OPpCONST_BARE))
4715 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
4716 OP *newop = newHVREF(newGVOP(OP_GV, 0,
4717 gv_fetchpv(name, TRUE, SVt_PVHV) ));
4718 if (ckWARN(WARN_SYNTAX))
4720 "Hash %%%s missing the %% in argument %ld of %s()",
4721 name, (long)numargs, PL_op_desc[type]);
4724 kid->op_sibling = sibl;
4727 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
4728 bad_type(numargs, "hash", PL_op_desc[type], kid);
4733 OP *newop = newUNOP(OP_NULL, 0, kid);
4734 kid->op_sibling = 0;
4736 newop->op_next = newop;
4738 kid->op_sibling = sibl;
4743 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) {
4744 if (kid->op_type == OP_CONST &&
4745 (kid->op_private & OPpCONST_BARE))
4747 OP *newop = newGVOP(OP_GV, 0,
4748 gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, n_a), TRUE,
4753 else if (kid->op_type == OP_READLINE) {
4754 /* neophyte patrol: open(<FH>), close(<FH>) etc. */
4755 bad_type(numargs, "HANDLE", PL_op_desc[o->op_type], kid);
4758 I32 flags = OPf_SPECIAL;
4759 /* is this op a FH constructor? */
4760 if (is_handle_constructor(o,numargs))
4762 kid->op_sibling = 0;
4763 kid = newUNOP(OP_RV2GV, flags, scalar(kid));
4765 kid->op_sibling = sibl;
4771 mod(scalar(kid), type);
4775 tokid = &kid->op_sibling;
4776 kid = kid->op_sibling;
4778 o->op_private |= numargs;
4780 return too_many_arguments(o,PL_op_desc[o->op_type]);
4783 else if (PL_opargs[type] & OA_DEFGV) {
4785 return newUNOP(type, 0, newDEFSVOP());
4789 while (oa & OA_OPTIONAL)
4791 if (oa && oa != OA_LIST)
4792 return too_few_arguments(o,PL_op_desc[o->op_type]);
4802 if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
4803 append_elem(OP_GLOB, o, newDEFSVOP());
4805 if (!((gv = gv_fetchpv("glob", FALSE, SVt_PVCV)) && GvIMPORTED_CV(gv)))
4806 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
4808 if (gv && GvIMPORTED_CV(gv)) {
4809 append_elem(OP_GLOB, o,
4810 newSVOP(OP_CONST, 0, newSViv(PL_glob_index++)));
4811 o->op_type = OP_LIST;
4812 o->op_ppaddr = PL_ppaddr[OP_LIST];
4813 cLISTOPo->op_first->op_type = OP_PUSHMARK;
4814 cLISTOPo->op_first->op_ppaddr = PL_ppaddr[OP_PUSHMARK];
4815 o = newUNOP(OP_ENTERSUB, OPf_STACKED,
4816 append_elem(OP_LIST, o,
4817 scalar(newUNOP(OP_RV2CV, 0,
4818 newGVOP(OP_GV, 0, gv)))));
4819 o = newUNOP(OP_NULL, 0, ck_subr(o));
4820 o->op_targ = OP_GLOB; /* hint at what it used to be */
4823 gv = newGVgen("main");
4825 append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
4835 OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
4837 o->op_ppaddr = PL_ppaddr[OP_GREPSTART];
4838 Newz(1101, gwop, 1, LOGOP);
4840 if (o->op_flags & OPf_STACKED) {
4843 kid = cLISTOPo->op_first->op_sibling;
4844 for (k = cLISTOPo->op_first->op_sibling->op_next; k; k = k->op_next) {
4847 kid->op_next = (OP*)gwop;
4848 o->op_flags &= ~OPf_STACKED;
4850 kid = cLISTOPo->op_first->op_sibling;
4851 if (type == OP_MAPWHILE)
4858 kid = cLISTOPo->op_first->op_sibling;
4859 if (kid->op_type != OP_NULL)
4860 croak("panic: ck_grep");
4861 kid = kUNOP->op_first;
4863 gwop->op_type = type;
4864 gwop->op_ppaddr = PL_ppaddr[type];
4865 gwop->op_first = listkids(o);
4866 gwop->op_flags |= OPf_KIDS;
4867 gwop->op_private = 1;
4868 gwop->op_other = LINKLIST(kid);
4869 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
4870 kid->op_next = (OP*)gwop;
4872 kid = cLISTOPo->op_first->op_sibling;
4873 if (!kid || !kid->op_sibling)
4874 return too_few_arguments(o,PL_op_desc[o->op_type]);
4875 for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
4876 mod(kid, OP_GREPSTART);
4884 if (o->op_flags & OPf_KIDS) {
4885 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
4887 kid = kid->op_sibling; /* get past "big" */
4888 if (kid && kid->op_type == OP_CONST)
4889 fbm_compile(((SVOP*)kid)->op_sv, 0);
4895 ck_lengthconst(OP *o)
4897 /* XXX length optimization goes here */
4904 OPCODE type = o->op_type;
4905 return modkids(ck_fun(o), type);
4911 OPCODE type = o->op_type;
4912 return refkids(ck_fun(o), type);
4920 kid = cLISTOPo->op_first;
4923 kid = cLISTOPo->op_first;
4925 if (kid->op_type == OP_PUSHMARK)
4926 kid = kid->op_sibling;
4927 if (kid && o->op_flags & OPf_STACKED)
4928 kid = kid->op_sibling;
4929 else if (kid && !kid->op_sibling) { /* print HANDLE; */
4930 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
4931 o->op_flags |= OPf_STACKED; /* make it a filehandle */
4932 kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
4933 cLISTOPo->op_first->op_sibling = kid;
4934 cLISTOPo->op_last = kid;
4935 kid = kid->op_sibling;
4940 append_elem(o->op_type, o, newDEFSVOP());
4946 if (PL_hints & HINT_LOCALE)
4947 o->op_private |= OPpLOCALE;
4954 ck_fun_locale(OP *o)
4960 if (PL_hints & HINT_LOCALE)
4961 o->op_private |= OPpLOCALE;
4972 if (PL_hints & HINT_LOCALE)
4973 o->op_private |= OPpLOCALE;
4982 o->op_private |= OPpRUNTIME;
4995 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
4996 o->op_private |= OPpREPEAT_DOLIST;
4997 cBINOPo->op_first = force_list(cBINOPo->op_first);
5007 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
5008 SVOP *kid = (SVOP*)cUNOPo->op_first;
5010 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
5012 for (s = SvPVX(kid->op_sv); *s; s++) {
5013 if (*s == ':' && s[1] == ':') {
5015 Move(s+2, s+1, strlen(s+2)+1, char);
5016 --SvCUR(kid->op_sv);
5019 sv_catpvn(kid->op_sv, ".pm", 3);
5029 croak("NOT IMPL LINE %d",__LINE__);
5039 if (o->op_flags & OPf_KIDS) {
5040 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5041 if (kid && kid->op_sibling) {
5042 o->op_type = OP_SSELECT;
5043 o->op_ppaddr = PL_ppaddr[OP_SSELECT];
5045 return fold_constants(o);
5049 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5050 if (kid && kid->op_type == OP_RV2GV)
5051 kid->op_private &= ~HINT_STRICT_REFS;
5058 I32 type = o->op_type;
5060 if (!(o->op_flags & OPf_KIDS)) {
5065 if (!CvUNIQUE(PL_compcv)) {
5066 argop = newOP(OP_PADAV, OPf_REF);
5067 argop->op_targ = 0; /* PL_curpad[0] is @_ */
5070 argop = newUNOP(OP_RV2AV, 0,
5071 scalar(newGVOP(OP_GV, 0,
5072 gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
5075 argop = newUNOP(OP_RV2AV, 0,
5076 scalar(newGVOP(OP_GV, 0, !CvUNIQUE(PL_compcv) ?
5077 PL_defgv : gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
5078 #endif /* USE_THREADS */
5079 return newUNOP(type, 0, scalar(argop));
5081 return scalar(modkids(ck_fun(o), type));
5089 if (PL_hints & HINT_LOCALE)
5090 o->op_private |= OPpLOCALE;
5093 if (o->op_flags & OPf_STACKED)
5095 if (o->op_flags & OPf_STACKED) { /* may have been cleared */
5096 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5098 kid = kUNOP->op_first; /* get past rv2gv */
5100 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
5102 if (kid->op_type == OP_SCOPE) {
5106 else if (kid->op_type == OP_LEAVE) {
5107 if (o->op_type == OP_SORT) {
5108 null(kid); /* wipe out leave */
5111 for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
5112 if (k->op_next == kid)
5117 kid->op_next = 0; /* just disconnect the leave */
5118 k = kLISTOP->op_first;
5122 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5123 null(kid); /* wipe out rv2gv */
5124 if (o->op_type == OP_SORT)
5128 o->op_flags |= OPf_SPECIAL;
5130 else if (kid->op_type == OP_RV2SV || kid->op_type == OP_PADSV)
5131 null(cLISTOPo->op_first->op_sibling);
5138 simplify_sort(OP *o)
5141 register OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5144 if (!(o->op_flags & OPf_STACKED))
5146 kid = kUNOP->op_first; /* get past rv2gv */
5147 if (kid->op_type != OP_SCOPE)
5149 kid = kLISTOP->op_last; /* get past scope */
5150 switch(kid->op_type) {
5158 k = kid; /* remember this node*/
5159 if (kBINOP->op_first->op_type != OP_RV2SV)
5161 kid = kBINOP->op_first; /* get past cmp */
5162 if (kUNOP->op_first->op_type != OP_GV)
5164 kid = kUNOP->op_first; /* get past rv2sv */
5165 if (GvSTASH(kGVOP->op_gv) != PL_curstash)
5167 if (strEQ(GvNAME(kGVOP->op_gv), "a"))
5169 else if(strEQ(GvNAME(kGVOP->op_gv), "b"))
5173 kid = k; /* back to cmp */
5174 if (kBINOP->op_last->op_type != OP_RV2SV)
5176 kid = kBINOP->op_last; /* down to 2nd arg */
5177 if (kUNOP->op_first->op_type != OP_GV)
5179 kid = kUNOP->op_first; /* get past rv2sv */
5180 if (GvSTASH(kGVOP->op_gv) != PL_curstash
5182 ? strNE(GvNAME(kGVOP->op_gv), "a")
5183 : strNE(GvNAME(kGVOP->op_gv), "b")))
5185 o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL);
5187 o->op_private |= OPpSORT_REVERSE;
5188 if (k->op_type == OP_NCMP)
5189 o->op_private |= OPpSORT_NUMERIC;
5190 if (k->op_type == OP_I_NCMP)
5191 o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER;
5192 op_free(cLISTOPo->op_first->op_sibling); /* delete comparison block */
5193 cLISTOPo->op_first->op_sibling = cLISTOPo->op_last;
5194 cLISTOPo->op_children = 1;
5202 if (o->op_flags & OPf_STACKED)
5203 return no_fh_allowed(o);
5205 kid = cLISTOPo->op_first;
5206 if (kid->op_type != OP_NULL)
5207 croak("panic: ck_split");
5208 kid = kid->op_sibling;
5209 op_free(cLISTOPo->op_first);
5210 cLISTOPo->op_first = kid;
5212 cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpv(" ", 1));
5213 cLISTOPo->op_last = kid; /* There was only one element previously */
5216 if (kid->op_type != OP_MATCH) {
5217 OP *sibl = kid->op_sibling;
5218 kid->op_sibling = 0;
5219 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
5220 if (cLISTOPo->op_first == cLISTOPo->op_last)
5221 cLISTOPo->op_last = kid;
5222 cLISTOPo->op_first = kid;
5223 kid->op_sibling = sibl;
5226 kid->op_type = OP_PUSHRE;
5227 kid->op_ppaddr = PL_ppaddr[OP_PUSHRE];
5230 if (!kid->op_sibling)
5231 append_elem(OP_SPLIT, o, newDEFSVOP());
5233 kid = kid->op_sibling;
5236 if (!kid->op_sibling)
5237 append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
5239 kid = kid->op_sibling;
5242 if (kid->op_sibling)
5243 return too_many_arguments(o,PL_op_desc[o->op_type]);
5252 OP *prev = ((cUNOPo->op_first->op_sibling)
5253 ? cUNOPo : ((UNOP*)cUNOPo->op_first))->op_first;
5254 OP *o2 = prev->op_sibling;
5263 for (cvop = o2; cvop->op_sibling; cvop = cvop->op_sibling) ;
5264 if (cvop->op_type == OP_RV2CV) {
5266 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
5267 null(cvop); /* disable rv2cv */
5268 tmpop = (SVOP*)((UNOP*)cvop)->op_first;
5269 if (tmpop->op_type == OP_GV) {
5270 cv = GvCVu(tmpop->op_sv);
5271 if (cv && SvPOK(cv) && !(o->op_private & OPpENTERSUB_AMPER)) {
5272 namegv = CvANON(cv) ? (GV*)tmpop->op_sv : CvGV(cv);
5273 proto = SvPV((SV*)cv, n_a);
5277 o->op_private |= (PL_hints & HINT_STRICT_REFS);
5278 if (PERLDB_SUB && PL_curstash != PL_debstash)
5279 o->op_private |= OPpENTERSUB_DB;
5280 while (o2 != cvop) {
5284 return too_many_arguments(o, gv_ename(namegv));
5302 if (o2->op_type != OP_REFGEN && o2->op_type != OP_UNDEF)
5303 bad_type(arg, "block", gv_ename(namegv), o2);
5308 if (o2->op_type == OP_RV2GV)
5312 OP* sib = kid->op_sibling;
5313 kid->op_sibling = 0;
5314 o2 = newUNOP(OP_RV2GV, 0, kid);
5315 o2->op_sibling = sib;
5316 prev->op_sibling = o2;
5324 if (o2->op_type != OP_RV2GV)
5325 bad_type(arg, "symbol", gv_ename(namegv), o2);
5328 if (o2->op_type != OP_RV2CV)
5329 bad_type(arg, "sub", gv_ename(namegv), o2);
5332 if (o2->op_type != OP_RV2SV
5333 && o2->op_type != OP_PADSV
5334 && o2->op_type != OP_THREADSV)
5336 bad_type(arg, "scalar", gv_ename(namegv), o2);
5340 if (o2->op_type != OP_RV2AV && o2->op_type != OP_PADAV)
5341 bad_type(arg, "array", gv_ename(namegv), o2);
5344 if (o2->op_type != OP_RV2HV && o2->op_type != OP_PADHV)
5345 bad_type(arg, "hash", gv_ename(namegv), o2);
5349 OP* sib = kid->op_sibling;
5350 kid->op_sibling = 0;
5351 o2 = newUNOP(OP_REFGEN, 0, kid);
5352 o2->op_sibling = sib;
5353 prev->op_sibling = o2;
5364 croak("Malformed prototype for %s: %s",
5365 gv_ename(namegv), SvPV((SV*)cv, n_a));
5370 mod(o2, OP_ENTERSUB);
5372 o2 = o2->op_sibling;
5374 if (proto && !optional &&
5375 (*proto && *proto != '@' && *proto != '%' && *proto != ';'))
5376 return too_few_arguments(o, gv_ename(namegv));
5383 SvREADONLY_on(cSVOPo->op_sv);
5390 if (o->op_flags & OPf_KIDS) {
5391 SVOP *kid = (SVOP*)cUNOPo->op_first;
5393 if (kid->op_type == OP_NULL)
5394 kid = (SVOP*)kid->op_sibling;
5396 kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE))
5397 o->op_flags |= OPf_SPECIAL;
5402 /* A peephole optimizer. We visit the ops in the order they're to execute. */
5405 peep(register OP *o)
5408 register OP* oldop = 0;
5411 if (!o || o->op_seq)
5415 SAVESPTR(PL_curcop);
5416 for (; o; o = o->op_next) {
5422 switch (o->op_type) {
5425 PL_curcop = ((COP*)o); /* for warnings */
5426 o->op_seq = PL_op_seqmax++;
5437 if (o->op_next && o->op_next->op_type == OP_STRINGIFY)
5439 o->op_seq = PL_op_seqmax++;
5442 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
5443 o->op_seq = PL_op_seqmax++;
5444 break; /* Scalar stub must produce undef. List stub is noop */
5448 if (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE)
5449 PL_curcop = ((COP*)o);
5455 if (oldop && o->op_next) {
5456 oldop->op_next = o->op_next;
5459 o->op_seq = PL_op_seqmax++;
5463 if (o->op_next->op_type == OP_RV2SV) {
5464 if (!(o->op_next->op_private & OPpDEREF)) {
5466 o->op_private |= o->op_next->op_private & OPpLVAL_INTRO;
5467 o->op_next = o->op_next->op_next;
5468 o->op_type = OP_GVSV;
5469 o->op_ppaddr = PL_ppaddr[OP_GVSV];
5472 else if (o->op_next->op_type == OP_RV2AV) {
5473 OP* pop = o->op_next->op_next;
5475 if (pop->op_type == OP_CONST &&
5476 (PL_op = pop->op_next) &&
5477 pop->op_next->op_type == OP_AELEM &&
5478 !(pop->op_next->op_private &
5479 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF)) &&
5480 (i = SvIV(((SVOP*)pop)->op_sv) - PL_compiling.cop_arybase)
5484 SvREFCNT_dec(((SVOP*)pop)->op_sv);
5488 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
5489 o->op_next = pop->op_next->op_next;
5490 o->op_type = OP_AELEMFAST;
5491 o->op_ppaddr = PL_ppaddr[OP_AELEMFAST];
5492 o->op_private = (U8)i;
5493 GvAVn(((GVOP*)o)->op_gv);
5496 o->op_seq = PL_op_seqmax++;
5503 o->op_seq = PL_op_seqmax++;
5504 while (cLOGOP->op_other->op_type == OP_NULL)
5505 cLOGOP->op_other = cLOGOP->op_other->op_next;
5506 peep(cLOGOP->op_other);
5510 o->op_seq = PL_op_seqmax++;
5511 peep(cCONDOP->op_true);
5512 peep(cCONDOP->op_false);
5516 o->op_seq = PL_op_seqmax++;
5517 peep(cLOOP->op_redoop);
5518 peep(cLOOP->op_nextop);
5519 peep(cLOOP->op_lastop);
5525 o->op_seq = PL_op_seqmax++;
5526 peep(cPMOP->op_pmreplstart);
5530 o->op_seq = PL_op_seqmax++;
5531 if (ckWARN(WARN_SYNTAX) && o->op_next
5532 && o->op_next->op_type == OP_NEXTSTATE) {
5533 if (o->op_next->op_sibling &&
5534 o->op_next->op_sibling->op_type != OP_EXIT &&
5535 o->op_next->op_sibling->op_type != OP_WARN &&
5536 o->op_next->op_sibling->op_type != OP_DIE) {
5537 line_t oldline = PL_curcop->cop_line;
5539 PL_curcop->cop_line = ((COP*)o->op_next)->cop_line;
5540 warner(WARN_SYNTAX, "Statement unlikely to be reached");
5541 warner(WARN_SYNTAX, "(Maybe you meant system() when you said exec()?)\n");
5542 PL_curcop->cop_line = oldline;
5556 if (o->op_private & (OPpDEREF_HV|OPpDEREF_AV|OPpLVAL_INTRO)
5557 || ((BINOP*)o)->op_last->op_type != OP_CONST)
5559 rop = (UNOP*)((BINOP*)o)->op_first;
5560 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
5562 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
5563 if (!SvOBJECT(lexname))
5565 fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
5566 if (!fields || !GvHV(*fields))
5568 svp = &((SVOP*)((BINOP*)o)->op_last)->op_sv;
5569 key = SvPV(*svp, keylen);
5570 indsvp = hv_fetch(GvHV(*fields), key, keylen, FALSE);
5572 croak("No such field \"%s\" in variable %s of type %s",
5573 key, SvPV(lexname, n_a), HvNAME(SvSTASH(lexname)));
5575 ind = SvIV(*indsvp);
5577 croak("Bad index while coercing array into hash");
5578 rop->op_type = OP_RV2AV;
5579 rop->op_ppaddr = PL_ppaddr[OP_RV2AV];
5580 o->op_type = OP_AELEM;
5581 o->op_ppaddr = PL_ppaddr[OP_AELEM];
5583 *svp = newSViv(ind);
5588 o->op_seq = PL_op_seqmax++;