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));
60 SV* tmpsv = sv_newmortal();
61 gv_efullname3(tmpsv, gv, Nullch);
62 return SvPV(tmpsv,n_a);
68 yyerror(form("Missing comma after first argument to %s function",
69 PL_op_desc[o->op_type]));
74 too_few_arguments(OP *o, char *name)
76 yyerror(form("Not enough arguments for %s", name));
81 too_many_arguments(OP *o, char *name)
83 yyerror(form("Too many arguments for %s", name));
88 bad_type(I32 n, char *t, char *name, OP *kid)
90 yyerror(form("Type of arg %d to %s must be %s (not %s)",
91 (int)n, name, t, PL_op_desc[kid->op_type]));
97 int type = o->op_type;
98 if (type != OP_AELEM && type != OP_HELEM && type != OP_GELEM) {
99 yyerror(form("Can't use subscript on %s", PL_op_desc[type]));
100 if (type == OP_ENTERSUB || type == OP_RV2HV || type == OP_PADHV) {
102 SV *msg = sv_2mortal(
103 newSVpvf("(Did you mean $ or @ instead of %c?)\n",
104 type == OP_ENTERSUB ? '&' : '%'));
108 sv_catsv(GvSV(PL_errgv), msg);
110 PerlIO_write(PerlIO_stderr(), SvPVX(msg), SvCUR(msg));
115 /* "register" allocation */
118 pad_allocmy(char *name)
126 (PL_hints & HINT_UTF8 && (name[1] & 0xc0) == 0xc0) ||
127 name[1] == '_' && (int)strlen(name) > 2))
129 if (!isPRINT(name[1])) {
131 name[2] = toCTRL(name[1]);
134 croak("Can't use global %s in \"my\"",name);
136 if (ckWARN(WARN_UNSAFE) && AvFILLp(PL_comppad_name) >= 0) {
137 SV **svp = AvARRAY(PL_comppad_name);
138 for (off = AvFILLp(PL_comppad_name); off > PL_comppad_name_floor; off--) {
140 && sv != &PL_sv_undef
141 && (SvIVX(sv) == PAD_MAX || SvIVX(sv) == 0)
142 && strEQ(name, SvPVX(sv)))
145 "\"my\" variable %s masks earlier declaration in same %s",
146 name, (SvIVX(sv) == PAD_MAX ? "scope" : "statement"));
151 off = pad_alloc(OP_PADSV, SVs_PADMY);
153 sv_upgrade(sv, SVt_PVNV);
155 if (PL_in_my_stash) {
157 croak("Can't declare class for non-scalar %s in \"my\"",name);
159 (void)SvUPGRADE(sv, SVt_PVMG);
160 SvSTASH(sv) = (HV*)SvREFCNT_inc(PL_in_my_stash);
163 av_store(PL_comppad_name, off, sv);
164 SvNVX(sv) = (double)PAD_MAX;
165 SvIVX(sv) = 0; /* Not yet introduced--see newSTATEOP */
166 if (!PL_min_intro_pending)
167 PL_min_intro_pending = off;
168 PL_max_intro_pending = off;
170 av_store(PL_comppad, off, (SV*)newAV());
171 else if (*name == '%')
172 av_store(PL_comppad, off, (SV*)newHV());
173 SvPADMY_on(PL_curpad[off]);
178 pad_findlex(char *name, PADOFFSET newoff, U32 seq, CV* startcv, I32 cx_ix, I32 saweval)
185 register PERL_CONTEXT *cx;
187 for (cv = startcv; cv; cv = CvOUTSIDE(cv)) {
188 AV *curlist = CvPADLIST(cv);
189 SV **svp = av_fetch(curlist, 0, FALSE);
192 if (!svp || *svp == &PL_sv_undef)
195 svp = AvARRAY(curname);
196 for (off = AvFILLp(curname); off > 0; off--) {
197 if ((sv = svp[off]) &&
198 sv != &PL_sv_undef &&
200 seq > I_32(SvNVX(sv)) &&
201 strEQ(SvPVX(sv), name))
212 return 0; /* don't clone from inactive stack frame */
216 oldpad = (AV*)*av_fetch(curlist, depth, FALSE);
217 oldsv = *av_fetch(oldpad, off, TRUE);
218 if (!newoff) { /* Not a mere clone operation. */
219 SV *namesv = NEWSV(1103,0);
220 newoff = pad_alloc(OP_PADSV, SVs_PADMY);
221 sv_upgrade(namesv, SVt_PVNV);
222 sv_setpv(namesv, name);
223 av_store(PL_comppad_name, newoff, namesv);
224 SvNVX(namesv) = (double)PL_curcop->cop_seq;
225 SvIVX(namesv) = PAD_MAX; /* A ref, intro immediately */
226 SvFAKE_on(namesv); /* A ref, not a real var */
227 if (SvOBJECT(sv)) { /* A typed var */
229 (void)SvUPGRADE(namesv, SVt_PVMG);
230 SvSTASH(namesv) = (HV*)SvREFCNT_inc((SV*)SvSTASH(sv));
233 if (CvANON(PL_compcv) || SvTYPE(PL_compcv) == SVt_PVFM) {
234 /* "It's closures all the way down." */
235 CvCLONE_on(PL_compcv);
237 if (CvANON(PL_compcv))
238 oldsv = Nullsv; /* no need to keep ref */
243 bcv && bcv != cv && !CvCLONE(bcv);
244 bcv = CvOUTSIDE(bcv))
249 if (ckWARN(WARN_CLOSURE)
250 && !CvUNIQUE(bcv) && !CvUNIQUE(cv))
253 "Variable \"%s\" may be unavailable",
261 else if (!CvUNIQUE(PL_compcv)) {
262 if (ckWARN(WARN_CLOSURE) && !SvFAKE(sv) && !CvUNIQUE(cv))
264 "Variable \"%s\" will not stay shared", name);
267 av_store(PL_comppad, newoff, SvREFCNT_inc(oldsv));
273 /* Nothing in current lexical context--try eval's context, if any.
274 * This is necessary to let the perldb get at lexically scoped variables.
275 * XXX This will also probably interact badly with eval tree caching.
278 for (i = cx_ix; i >= 0; i--) {
280 switch (CxTYPE(cx)) {
282 if (i == 0 && saweval) {
283 seq = cxstack[saweval].blk_oldcop->cop_seq;
284 return pad_findlex(name, newoff, seq, PL_main_cv, -1, saweval);
288 switch (cx->blk_eval.old_op_type) {
294 /* require must have its own scope */
302 if (PL_debstash && CvSTASH(cv) == PL_debstash) { /* ignore DB'* scope */
303 saweval = i; /* so we know where we were called from */
306 seq = cxstack[saweval].blk_oldcop->cop_seq;
307 return pad_findlex(name, newoff, seq, cv, i-1, saweval);
315 pad_findmy(char *name)
321 SV **svp = AvARRAY(PL_comppad_name);
322 U32 seq = PL_cop_seqmax;
328 * Special case to get lexical (and hence per-thread) @_.
329 * XXX I need to find out how to tell at parse-time whether use
330 * of @_ should refer to a lexical (from a sub) or defgv (global
331 * scope and maybe weird sub-ish things like formats). See
332 * startsub in perly.y. It's possible that @_ could be lexical
333 * (at least from subs) even in non-threaded perl.
335 if (strEQ(name, "@_"))
336 return 0; /* success. (NOT_IN_PAD indicates failure) */
337 #endif /* USE_THREADS */
339 /* The one we're looking for is probably just before comppad_name_fill. */
340 for (off = AvFILLp(PL_comppad_name); off > 0; off--) {
341 if ((sv = svp[off]) &&
342 sv != &PL_sv_undef &&
345 seq > I_32(SvNVX(sv)))) &&
346 strEQ(SvPVX(sv), name))
349 return (PADOFFSET)off;
350 pendoff = off; /* this pending def. will override import */
354 outside = CvOUTSIDE(PL_compcv);
356 /* Check if if we're compiling an eval'', and adjust seq to be the
357 * eval's seq number. This depends on eval'' having a non-null
358 * CvOUTSIDE() while it is being compiled. The eval'' itself is
359 * identified by CvUNIQUE being set and CvGV being null. */
360 if (outside && CvUNIQUE(PL_compcv) && !CvGV(PL_compcv) && cxstack_ix >= 0) {
361 cx = &cxstack[cxstack_ix];
363 seq = cx->blk_oldcop->cop_seq;
366 /* See if it's in a nested scope */
367 off = pad_findlex(name, 0, seq, outside, cxstack_ix, 0);
369 /* If there is a pending local definition, this new alias must die */
371 SvIVX(AvARRAY(PL_comppad_name)[off]) = seq;
372 return off; /* pad_findlex returns 0 for failure...*/
374 return NOT_IN_PAD; /* ...but we return NOT_IN_PAD for failure */
378 pad_leavemy(I32 fill)
381 SV **svp = AvARRAY(PL_comppad_name);
383 if (PL_min_intro_pending && fill < PL_min_intro_pending) {
384 for (off = PL_max_intro_pending; off >= PL_min_intro_pending; off--) {
385 if ((sv = svp[off]) && sv != &PL_sv_undef)
386 warn("%s never introduced", SvPVX(sv));
389 /* "Deintroduce" my variables that are leaving with this scope. */
390 for (off = AvFILLp(PL_comppad_name); off > fill; off--) {
391 if ((sv = svp[off]) && sv != &PL_sv_undef && SvIVX(sv) == PAD_MAX)
392 SvIVX(sv) = PL_cop_seqmax;
397 pad_alloc(I32 optype, U32 tmptype)
403 if (AvARRAY(PL_comppad) != PL_curpad)
404 croak("panic: pad_alloc");
405 if (PL_pad_reset_pending)
407 if (tmptype & SVs_PADMY) {
409 sv = *av_fetch(PL_comppad, AvFILLp(PL_comppad) + 1, TRUE);
410 } while (SvPADBUSY(sv)); /* need a fresh one */
411 retval = AvFILLp(PL_comppad);
414 SV **names = AvARRAY(PL_comppad_name);
415 SSize_t names_fill = AvFILLp(PL_comppad_name);
418 * "foreach" index vars temporarily become aliases to non-"my"
419 * values. Thus we must skip, not just pad values that are
420 * marked as current pad values, but also those with names.
422 if (++PL_padix <= names_fill &&
423 (sv = names[PL_padix]) && sv != &PL_sv_undef)
425 sv = *av_fetch(PL_comppad, PL_padix, TRUE);
426 if (!(SvFLAGS(sv) & (SVs_PADTMP|SVs_PADMY)))
431 SvFLAGS(sv) |= tmptype;
432 PL_curpad = AvARRAY(PL_comppad);
434 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx alloc %ld for %s\n",
435 (unsigned long) thr, (unsigned long) PL_curpad,
436 (long) retval, PL_op_name[optype]));
438 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx alloc %ld for %s\n",
439 (unsigned long) PL_curpad,
440 (long) retval, PL_op_name[optype]));
441 #endif /* USE_THREADS */
442 return (PADOFFSET)retval;
450 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx sv %d\n",
451 (unsigned long) thr, (unsigned long) PL_curpad, po));
454 croak("panic: pad_sv po");
455 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx sv %d\n",
456 (unsigned long) PL_curpad, po));
457 #endif /* USE_THREADS */
458 return PL_curpad[po]; /* eventually we'll turn this into a macro */
462 pad_free(PADOFFSET po)
467 if (AvARRAY(PL_comppad) != PL_curpad)
468 croak("panic: pad_free curpad");
470 croak("panic: pad_free po");
472 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx free %d\n",
473 (unsigned long) thr, (unsigned long) PL_curpad, po));
475 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx free %d\n",
476 (unsigned long) PL_curpad, po));
477 #endif /* USE_THREADS */
478 if (PL_curpad[po] && PL_curpad[po] != &PL_sv_undef)
479 SvPADTMP_off(PL_curpad[po]);
480 if ((I32)po < PL_padix)
485 pad_swipe(PADOFFSET po)
488 if (AvARRAY(PL_comppad) != PL_curpad)
489 croak("panic: pad_swipe curpad");
491 croak("panic: pad_swipe po");
493 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx swipe %d\n",
494 (unsigned long) thr, (unsigned long) PL_curpad, po));
496 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx swipe %d\n",
497 (unsigned long) PL_curpad, po));
498 #endif /* USE_THREADS */
499 SvPADTMP_off(PL_curpad[po]);
500 PL_curpad[po] = NEWSV(1107,0);
501 SvPADTMP_on(PL_curpad[po]);
502 if ((I32)po < PL_padix)
506 /* XXX pad_reset() is currently disabled because it results in serious bugs.
507 * It causes pad temp TARGs to be shared between OPs. Since TARGs are pushed
508 * on the stack by OPs that use them, there are several ways to get an alias
509 * to a shared TARG. Such an alias will change randomly and unpredictably.
510 * We avoid doing this until we can think of a Better Way.
515 #ifdef USE_BROKEN_PAD_RESET
519 if (AvARRAY(PL_comppad) != PL_curpad)
520 croak("panic: pad_reset curpad");
522 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx reset\n",
523 (unsigned long) thr, (unsigned long) PL_curpad));
525 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx reset\n",
526 (unsigned long) PL_curpad));
527 #endif /* USE_THREADS */
528 if (!PL_tainting) { /* Can't mix tainted and non-tainted temporaries. */
529 for (po = AvMAX(PL_comppad); po > PL_padix_floor; po--) {
530 if (PL_curpad[po] && !SvIMMORTAL(PL_curpad[po]))
531 SvPADTMP_off(PL_curpad[po]);
533 PL_padix = PL_padix_floor;
536 PL_pad_reset_pending = FALSE;
540 /* find_threadsv is not reentrant */
542 find_threadsv(char *name)
548 /* We currently only handle names of a single character */
549 p = strchr(PL_threadsv_names, *name);
552 key = p - PL_threadsv_names;
553 MUTEX_LOCK(&thr->mutex);
554 svp = av_fetch(thr->threadsv, key, FALSE);
556 MUTEX_UNLOCK(&thr->mutex);
558 SV *sv = NEWSV(0, 0);
559 av_store(thr->threadsv, key, sv);
560 thr->threadsvp = AvARRAY(thr->threadsv);
561 MUTEX_UNLOCK(&thr->mutex);
563 * Some magic variables used to be automagically initialised
564 * in gv_fetchpv. Those which are now per-thread magicals get
565 * initialised here instead.
571 sv_setpv(sv, "\034");
572 sv_magic(sv, 0, 0, name, 1);
577 PL_sawampersand = TRUE;
591 /* XXX %! tied to Errno.pm needs to be added here.
592 * See gv_fetchpv(). */
596 sv_magic(sv, 0, 0, name, 1);
598 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
599 "find_threadsv: new SV %p for $%s%c\n",
600 sv, (*name < 32) ? "^" : "",
601 (*name < 32) ? toCTRL(*name) : *name));
605 #endif /* USE_THREADS */
612 register OP *kid, *nextkid;
614 if (!o || o->op_seq == (U16)-1)
617 if (o->op_flags & OPf_KIDS) {
618 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
619 nextkid = kid->op_sibling; /* Get before next freeing kid */
624 switch (o->op_type) {
626 o->op_targ = 0; /* Was holding old type, if any. */
629 o->op_targ = 0; /* Was holding hints. */
633 if (!(o->op_flags & OPf_SPECIAL))
637 o->op_targ = 0; /* Was holding index into thr->threadsv AV. */
639 #endif /* USE_THREADS */
641 if (!(o->op_flags & OPf_REF)
642 || (PL_check[o->op_type] != FUNC_NAME_TO_PTR(ck_ftst)))
648 SvREFCNT_dec(cGVOPo->op_gv);
652 Safefree(cCOPo->cop_label);
653 SvREFCNT_dec(cCOPo->cop_filegv);
654 if (cCOPo->cop_warnings != WARN_NONE && cCOPo->cop_warnings != WARN_ALL)
655 SvREFCNT_dec(cCOPo->cop_warnings);
658 SvREFCNT_dec(cSVOPo->op_sv);
664 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
668 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF))
669 SvREFCNT_dec(cSVOPo->op_sv);
671 Safefree(cPVOPo->op_pv);
674 op_free(cPMOPo->op_pmreplroot);
679 ReREFCNT_dec(cPMOPo->op_pmregexp);
684 pad_free(o->op_targ);
692 if (o->op_type != OP_NULL && o->op_type != OP_THREADSV && o->op_targ > 0)
693 pad_free(o->op_targ);
694 o->op_targ = o->op_type;
695 o->op_type = OP_NULL;
696 o->op_ppaddr = PL_ppaddr[OP_NULL];
699 /* Contextualizers */
701 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
711 /* establish postfix order */
712 if (cUNOPo->op_first) {
713 o->op_next = LINKLIST(cUNOPo->op_first);
714 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
716 kid->op_next = LINKLIST(kid->op_sibling);
731 if (o && o->op_flags & OPf_KIDS) {
732 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
741 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
743 if (ckWARN(WARN_SYNTAX)) {
744 line_t oldline = PL_curcop->cop_line;
746 if (PL_copline != NOLINE)
747 PL_curcop->cop_line = PL_copline;
748 warner(WARN_SYNTAX, "Found = in conditional, should be ==");
749 PL_curcop->cop_line = oldline;
760 /* assumes no premature commitment */
761 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
762 || o->op_type == OP_RETURN)
765 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
767 switch (o->op_type) {
769 if (o->op_private & OPpREPEAT_DOLIST)
770 null(((LISTOP*)cBINOPo->op_first)->op_first);
771 scalar(cBINOPo->op_first);
776 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
780 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
781 if (!kPMOP->op_pmreplroot)
782 deprecate("implicit split to @_");
790 if (o->op_flags & OPf_KIDS) {
791 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
797 kid = cLISTOPo->op_first;
799 while (kid = kid->op_sibling) {
805 WITH_THR(PL_curcop = &PL_compiling);
810 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
816 WITH_THR(PL_curcop = &PL_compiling);
829 /* assumes no premature commitment */
830 if (!o || (o->op_flags & OPf_WANT) == OPf_WANT_LIST || PL_error_count
831 || o->op_type == OP_RETURN)
834 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
836 switch (o->op_type) {
838 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
842 if (o->op_flags & OPf_STACKED)
846 if (o->op_private == 4)
917 if (!(o->op_private & OPpLVAL_INTRO))
918 useless = PL_op_desc[o->op_type];
925 if (!(o->op_private & OPpLVAL_INTRO) &&
926 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
927 useless = "a variable";
932 WITH_THR(PL_curcop = ((COP*)o)); /* for warning below */
939 if (ckWARN(WARN_VOID)) {
940 useless = "a constant";
941 if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
943 else if (SvPOK(sv)) {
944 if (strnEQ(SvPVX(sv), "di", 2) ||
945 strnEQ(SvPVX(sv), "ds", 2) ||
946 strnEQ(SvPVX(sv), "ig", 2))
951 null(o); /* don't execute a constant */
952 SvREFCNT_dec(sv); /* don't even remember it */
956 o->op_type = OP_PREINC; /* pre-increment is faster */
957 o->op_ppaddr = PL_ppaddr[OP_PREINC];
961 o->op_type = OP_PREDEC; /* pre-decrement is faster */
962 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
968 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
973 if (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE)
974 WITH_THR(PL_curcop = ((COP*)o)); /* for warning below */
975 if (o->op_flags & OPf_STACKED)
981 if (!(o->op_flags & OPf_KIDS))
990 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
997 /* all requires must return a boolean value */
998 o->op_flags &= ~OPf_WANT;
1001 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
1002 if (!kPMOP->op_pmreplroot)
1003 deprecate("implicit split to @_");
1009 if (ckWARN(WARN_VOID))
1010 warner(WARN_VOID, "Useless use of %s in void context", useless);
1019 if (o && o->op_flags & OPf_KIDS) {
1020 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1031 /* assumes no premature commitment */
1032 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
1033 || o->op_type == OP_RETURN)
1036 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
1038 switch (o->op_type) {
1041 list(cBINOPo->op_first);
1046 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1054 if (!(o->op_flags & OPf_KIDS))
1056 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1057 list(cBINOPo->op_first);
1058 return gen_constant_list(o);
1065 kid = cLISTOPo->op_first;
1067 while (kid = kid->op_sibling) {
1068 if (kid->op_sibling)
1073 WITH_THR(PL_curcop = &PL_compiling);
1077 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1078 if (kid->op_sibling)
1083 WITH_THR(PL_curcop = &PL_compiling);
1086 /* all requires must return a boolean value */
1087 o->op_flags &= ~OPf_WANT;
1099 if (o->op_type == OP_LINESEQ ||
1100 o->op_type == OP_SCOPE ||
1101 o->op_type == OP_LEAVE ||
1102 o->op_type == OP_LEAVETRY)
1105 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1106 if (kid->op_sibling) {
1110 PL_curcop = &PL_compiling;
1112 o->op_flags &= ~OPf_PARENS;
1113 if (PL_hints & HINT_BLOCK_SCOPE)
1114 o->op_flags |= OPf_PARENS;
1117 o = newOP(OP_STUB, 0);
1122 modkids(OP *o, I32 type)
1125 if (o && o->op_flags & OPf_KIDS) {
1126 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1133 mod(OP *o, I32 type)
1140 if (!o || PL_error_count)
1143 switch (o->op_type) {
1148 if (!(o->op_private & (OPpCONST_ARYBASE)))
1150 if (PL_eval_start && PL_eval_start->op_type == OP_CONST) {
1151 PL_compiling.cop_arybase = (I32)SvIV(((SVOP*)PL_eval_start)->op_sv);
1155 SAVEI32(PL_compiling.cop_arybase);
1156 PL_compiling.cop_arybase = 0;
1158 else if (type == OP_REFGEN)
1161 croak("That use of $[ is unsupported");
1164 if (o->op_flags & OPf_PARENS)
1168 if ((type == OP_UNDEF || type == OP_REFGEN) &&
1169 !(o->op_flags & OPf_STACKED)) {
1170 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1171 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1172 assert(cUNOPo->op_first->op_type == OP_NULL);
1173 null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1179 /* grep, foreach, subcalls, refgen */
1180 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1182 yyerror(form("Can't modify %s in %s",
1183 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
1184 ? "do block" : PL_op_desc[o->op_type]),
1185 type ? PL_op_desc[type] : "local"));
1199 case OP_RIGHT_SHIFT:
1208 if (!(o->op_flags & OPf_STACKED))
1214 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1220 if (!type && cUNOPo->op_first->op_type != OP_GV)
1221 croak("Can't localize through a reference");
1222 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
1223 PL_modcount = 10000;
1224 return o; /* Treat \(@foo) like ordinary list. */
1228 if (scalar_mod_type(o, type))
1230 ref(cUNOPo->op_first, o->op_type);
1239 PL_modcount = 10000;
1242 if (!type && cUNOPo->op_first->op_type != OP_GV)
1243 croak("Can't localize through a reference");
1244 ref(cUNOPo->op_first, o->op_type);
1248 PL_hints |= HINT_BLOCK_SCOPE;
1256 PL_modcount = 10000;
1257 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1258 return o; /* Treat \(@foo) like ordinary list. */
1259 if (scalar_mod_type(o, type))
1265 croak("Can't localize lexical variable %s",
1266 SvPV(*av_fetch(PL_comppad_name, o->op_targ, 4), n_a));
1271 PL_modcount++; /* XXX ??? */
1273 #endif /* USE_THREADS */
1279 if (type != OP_SASSIGN)
1283 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
1289 pad_free(o->op_targ);
1290 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
1291 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
1292 if (o->op_flags & OPf_KIDS)
1293 mod(cBINOPo->op_first->op_sibling, type);
1298 ref(cBINOPo->op_first, o->op_type);
1299 if (type == OP_ENTERSUB &&
1300 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1301 o->op_private |= OPpLVAL_DEFER;
1308 if (o->op_flags & OPf_KIDS)
1309 mod(cLISTOPo->op_last, type);
1313 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
1315 else if (!(o->op_flags & OPf_KIDS))
1317 if (o->op_targ != OP_LIST) {
1318 mod(cBINOPo->op_first, type);
1323 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1327 o->op_flags |= OPf_MOD;
1329 if (type == OP_AASSIGN || type == OP_SASSIGN)
1330 o->op_flags |= OPf_SPECIAL|OPf_REF;
1332 o->op_private |= OPpLVAL_INTRO;
1333 o->op_flags &= ~OPf_SPECIAL;
1334 PL_hints |= HINT_BLOCK_SCOPE;
1336 else if (type != OP_GREPSTART && type != OP_ENTERSUB)
1337 o->op_flags |= OPf_REF;
1342 scalar_mod_type(OP *o, I32 type)
1346 if (o->op_type == OP_RV2GV)
1370 case OP_RIGHT_SHIFT:
1380 case OP_ANDASSIGN: /* may work later */
1381 case OP_ORASSIGN: /* may work later */
1389 refkids(OP *o, I32 type)
1392 if (o && o->op_flags & OPf_KIDS) {
1393 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1400 ref(OP *o, I32 type)
1404 if (!o || PL_error_count)
1407 switch (o->op_type) {
1409 if ((type == OP_DEFINED || type == OP_LOCK) &&
1410 !(o->op_flags & OPf_STACKED)) {
1411 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1412 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1413 assert(cUNOPo->op_first->op_type == OP_NULL);
1414 null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1415 o->op_flags |= OPf_SPECIAL;
1420 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1424 ref(cUNOPo->op_first, o->op_type);
1427 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1428 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1429 : type == OP_RV2HV ? OPpDEREF_HV
1431 o->op_flags |= OPf_MOD;
1436 o->op_flags |= OPf_MOD; /* XXX ??? */
1441 o->op_flags |= OPf_REF;
1444 ref(cUNOPo->op_first, o->op_type);
1449 o->op_flags |= OPf_REF;
1454 if (!(o->op_flags & OPf_KIDS))
1456 ref(cBINOPo->op_first, type);
1460 ref(cBINOPo->op_first, o->op_type);
1461 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1462 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1463 : type == OP_RV2HV ? OPpDEREF_HV
1465 o->op_flags |= OPf_MOD;
1473 if (!(o->op_flags & OPf_KIDS))
1475 ref(cLISTOPo->op_last, type);
1490 if (!o || PL_error_count)
1494 if (type == OP_LIST) {
1495 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1497 } else if (type == OP_UNDEF) {
1499 } else if (type != OP_PADSV &&
1502 type != OP_PUSHMARK)
1504 yyerror(form("Can't declare %s in my", PL_op_desc[o->op_type]));
1507 o->op_flags |= OPf_MOD;
1508 o->op_private |= OPpLVAL_INTRO;
1516 o->op_flags |= OPf_PARENS;
1521 bind_match(I32 type, OP *left, OP *right)
1526 if (ckWARN(WARN_UNSAFE) &&
1527 (left->op_type == OP_RV2AV ||
1528 left->op_type == OP_RV2HV ||
1529 left->op_type == OP_PADAV ||
1530 left->op_type == OP_PADHV)) {
1531 char *desc = PL_op_desc[(right->op_type == OP_SUBST ||
1532 right->op_type == OP_TRANS)
1533 ? right->op_type : OP_MATCH];
1534 char *sample = ((left->op_type == OP_RV2AV ||
1535 left->op_type == OP_PADAV)
1536 ? "@array" : "%hash");
1538 "Applying %s to %s will act on scalar(%s)",
1539 desc, sample, sample);
1542 if (right->op_type == OP_MATCH ||
1543 right->op_type == OP_SUBST ||
1544 right->op_type == OP_TRANS) {
1545 right->op_flags |= OPf_STACKED;
1546 if (right->op_type != OP_MATCH)
1547 left = mod(left, right->op_type);
1548 if (right->op_type == OP_TRANS)
1549 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
1551 o = prepend_elem(right->op_type, scalar(left), right);
1553 return newUNOP(OP_NOT, 0, scalar(o));
1557 return bind_match(type, left,
1558 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
1566 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
1567 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
1574 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
1575 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1576 o->op_type = OP_LEAVE;
1577 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
1580 if (o->op_type == OP_LINESEQ) {
1582 o->op_type = OP_SCOPE;
1583 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
1584 kid = ((LISTOP*)o)->op_first;
1585 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE){
1586 SvREFCNT_dec(((COP*)kid)->cop_filegv);
1591 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
1601 SAVESPTR(GvHV(PL_hintgv));
1602 GvHV(PL_hintgv) = newHVhv(GvHV(PL_hintgv));
1603 SAVEFREESV(GvHV(PL_hintgv));
1607 block_start(int full)
1610 int retval = PL_savestack_ix;
1612 SAVEI32(PL_comppad_name_floor);
1614 if ((PL_comppad_name_fill = AvFILLp(PL_comppad_name)) > 0)
1615 PL_comppad_name_floor = PL_comppad_name_fill;
1617 PL_comppad_name_floor = 0;
1619 SAVEI32(PL_min_intro_pending);
1620 SAVEI32(PL_max_intro_pending);
1621 PL_min_intro_pending = 0;
1622 SAVEI32(PL_comppad_name_fill);
1623 SAVEI32(PL_padix_floor);
1624 PL_padix_floor = PL_padix;
1625 PL_pad_reset_pending = FALSE;
1627 PL_hints &= ~HINT_BLOCK_SCOPE;
1628 SAVEPPTR(PL_compiling.cop_warnings);
1629 if (PL_compiling.cop_warnings != WARN_ALL &&
1630 PL_compiling.cop_warnings != WARN_NONE) {
1631 PL_compiling.cop_warnings = newSVsv(PL_compiling.cop_warnings) ;
1632 SAVEFREESV(PL_compiling.cop_warnings) ;
1640 block_end(I32 floor, OP *seq)
1643 int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
1644 OP* retval = scalarseq(seq);
1646 PL_pad_reset_pending = FALSE;
1647 PL_compiling.op_private = PL_hints;
1649 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
1650 pad_leavemy(PL_comppad_name_fill);
1659 OP *o = newOP(OP_THREADSV, 0);
1660 o->op_targ = find_threadsv("_");
1663 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
1664 #endif /* USE_THREADS */
1672 PL_eval_root = newUNOP(OP_LEAVEEVAL, ((PL_in_eval & 4) ? OPf_SPECIAL : 0), o);
1673 PL_eval_start = linklist(PL_eval_root);
1674 PL_eval_root->op_next = 0;
1675 peep(PL_eval_start);
1680 PL_main_root = scope(sawparens(scalarvoid(o)));
1681 PL_curcop = &PL_compiling;
1682 PL_main_start = LINKLIST(PL_main_root);
1683 PL_main_root->op_next = 0;
1684 peep(PL_main_start);
1687 /* Register with debugger */
1689 CV *cv = perl_get_cv("DB::postponed", FALSE);
1693 XPUSHs((SV*)PL_compiling.cop_filegv);
1695 perl_call_sv((SV*)cv, G_DISCARD);
1702 localize(OP *o, I32 lex)
1704 if (o->op_flags & OPf_PARENS)
1708 if (ckWARN(WARN_PARENTHESIS) && PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == ',') {
1710 for (s = PL_bufptr; *s && (isALNUM(*s) || (*s & 0x80) || strchr("@$%, ",*s)); s++) ;
1711 if (*s == ';' || *s == '=')
1712 warner(WARN_PARENTHESIS, "Parens missing around \"%s\" list",
1713 lex ? "my" : "local");
1717 PL_in_my_stash = Nullhv;
1721 return mod(o, OP_NULL); /* a bit kludgey */
1727 if (o->op_type == OP_LIST) {
1730 o2 = newOP(OP_THREADSV, 0);
1731 o2->op_targ = find_threadsv(";");
1733 o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
1734 #endif /* USE_THREADS */
1735 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
1741 fold_constants(register OP *o)
1745 I32 type = o->op_type;
1748 if (PL_opargs[type] & OA_RETSCALAR)
1750 if (PL_opargs[type] & OA_TARGET)
1751 o->op_targ = pad_alloc(type, SVs_PADTMP);
1753 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER))
1754 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
1756 if (!(PL_opargs[type] & OA_FOLDCONST))
1771 if (o->op_private & OPpLOCALE)
1776 goto nope; /* Don't try to run w/ errors */
1778 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
1779 if (curop->op_type != OP_CONST &&
1780 curop->op_type != OP_LIST &&
1781 curop->op_type != OP_SCALAR &&
1782 curop->op_type != OP_NULL &&
1783 curop->op_type != OP_PUSHMARK) {
1788 curop = LINKLIST(o);
1792 sv = *(PL_stack_sp--);
1793 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
1794 pad_swipe(o->op_targ);
1795 else if (SvTEMP(sv)) { /* grab mortal temp? */
1796 (void)SvREFCNT_inc(sv);
1800 if (type == OP_RV2GV)
1801 return newGVOP(OP_GV, 0, (GV*)sv);
1803 /* try to smush double to int, but don't smush -2.0 to -2 */
1804 if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK &&
1808 if ((double)iv == SvNV(sv)) {
1813 SvIOK_off(sv); /* undo SvIV() damage */
1815 return newSVOP(OP_CONST, 0, sv);
1819 if (!(PL_opargs[type] & OA_OTHERINT))
1822 if (!(PL_hints & HINT_INTEGER)) {
1823 if (type == OP_DIVIDE || !(o->op_flags & OPf_KIDS))
1826 for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
1827 if (curop->op_type == OP_CONST) {
1828 if (SvIOK(((SVOP*)curop)->op_sv))
1832 if (PL_opargs[curop->op_type] & OA_RETINTEGER)
1836 o->op_ppaddr = PL_ppaddr[++(o->op_type)];
1843 gen_constant_list(register OP *o)
1847 I32 oldtmps_floor = PL_tmps_floor;
1851 return o; /* Don't attempt to run with errors */
1853 PL_op = curop = LINKLIST(o);
1859 PL_tmps_floor = oldtmps_floor;
1861 o->op_type = OP_RV2AV;
1862 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
1863 curop = ((UNOP*)o)->op_first;
1864 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*PL_stack_sp--));
1871 convert(I32 type, I32 flags, OP *o)
1876 if (!o || o->op_type != OP_LIST)
1877 o = newLISTOP(OP_LIST, 0, o, Nullop);
1879 o->op_flags &= ~OPf_WANT;
1881 if (!(PL_opargs[type] & OA_MARK))
1882 null(cLISTOPo->op_first);
1885 o->op_ppaddr = PL_ppaddr[type];
1886 o->op_flags |= flags;
1888 o = CHECKOP(type, o);
1889 if (o->op_type != type)
1892 if (cLISTOPo->op_children < 7) {
1893 /* XXX do we really need to do this if we're done appending?? */
1894 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1896 cLISTOPo->op_last = last; /* in case check substituted last arg */
1899 return fold_constants(o);
1902 /* List constructors */
1905 append_elem(I32 type, OP *first, OP *last)
1913 if (first->op_type != type || type==OP_LIST && first->op_flags & OPf_PARENS)
1914 return newLISTOP(type, 0, first, last);
1916 if (first->op_flags & OPf_KIDS)
1917 ((LISTOP*)first)->op_last->op_sibling = last;
1919 first->op_flags |= OPf_KIDS;
1920 ((LISTOP*)first)->op_first = last;
1922 ((LISTOP*)first)->op_last = last;
1923 ((LISTOP*)first)->op_children++;
1928 append_list(I32 type, LISTOP *first, LISTOP *last)
1936 if (first->op_type != type)
1937 return prepend_elem(type, (OP*)first, (OP*)last);
1939 if (last->op_type != type)
1940 return append_elem(type, (OP*)first, (OP*)last);
1942 first->op_last->op_sibling = last->op_first;
1943 first->op_last = last->op_last;
1944 first->op_children += last->op_children;
1945 if (first->op_children)
1946 first->op_flags |= OPf_KIDS;
1953 prepend_elem(I32 type, OP *first, OP *last)
1961 if (last->op_type == type) {
1962 if (type == OP_LIST) { /* already a PUSHMARK there */
1963 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
1964 ((LISTOP*)last)->op_first->op_sibling = first;
1967 if (!(last->op_flags & OPf_KIDS)) {
1968 ((LISTOP*)last)->op_last = first;
1969 last->op_flags |= OPf_KIDS;
1971 first->op_sibling = ((LISTOP*)last)->op_first;
1972 ((LISTOP*)last)->op_first = first;
1974 ((LISTOP*)last)->op_children++;
1978 return newLISTOP(type, 0, first, last);
1986 return newOP(OP_STUB, 0);
1992 if (!o || o->op_type != OP_LIST)
1993 o = newLISTOP(OP_LIST, 0, o, Nullop);
1999 newLISTOP(I32 type, I32 flags, OP *first, OP *last)
2003 Newz(1101, listop, 1, LISTOP);
2005 listop->op_type = type;
2006 listop->op_ppaddr = PL_ppaddr[type];
2007 listop->op_children = (first != 0) + (last != 0);
2008 listop->op_flags = flags;
2012 else if (!first && last)
2015 first->op_sibling = last;
2016 listop->op_first = first;
2017 listop->op_last = last;
2018 if (type == OP_LIST) {
2020 pushop = newOP(OP_PUSHMARK, 0);
2021 pushop->op_sibling = first;
2022 listop->op_first = pushop;
2023 listop->op_flags |= OPf_KIDS;
2025 listop->op_last = pushop;
2027 else if (listop->op_children)
2028 listop->op_flags |= OPf_KIDS;
2034 newOP(I32 type, I32 flags)
2037 Newz(1101, o, 1, OP);
2039 o->op_ppaddr = PL_ppaddr[type];
2040 o->op_flags = flags;
2043 o->op_private = 0 + (flags >> 8);
2044 if (PL_opargs[type] & OA_RETSCALAR)
2046 if (PL_opargs[type] & OA_TARGET)
2047 o->op_targ = pad_alloc(type, SVs_PADTMP);
2048 return CHECKOP(type, o);
2052 newUNOP(I32 type, I32 flags, OP *first)
2057 first = newOP(OP_STUB, 0);
2058 if (PL_opargs[type] & OA_MARK)
2059 first = force_list(first);
2061 Newz(1101, unop, 1, UNOP);
2062 unop->op_type = type;
2063 unop->op_ppaddr = PL_ppaddr[type];
2064 unop->op_first = first;
2065 unop->op_flags = flags | OPf_KIDS;
2066 unop->op_private = 1 | (flags >> 8);
2067 unop = (UNOP*) CHECKOP(type, unop);
2071 return fold_constants((OP *) unop);
2075 newBINOP(I32 type, I32 flags, OP *first, OP *last)
2078 Newz(1101, binop, 1, BINOP);
2081 first = newOP(OP_NULL, 0);
2083 binop->op_type = type;
2084 binop->op_ppaddr = PL_ppaddr[type];
2085 binop->op_first = first;
2086 binop->op_flags = flags | OPf_KIDS;
2089 binop->op_private = 1 | (flags >> 8);
2092 binop->op_private = 2 | (flags >> 8);
2093 first->op_sibling = last;
2096 binop = (BINOP*)CHECKOP(type, binop);
2100 binop->op_last = binop->op_first->op_sibling;
2102 return fold_constants((OP *)binop);
2106 utf8compare(const void *a, const void *b)
2109 for (i = 0; i < 10; i++) {
2110 if ((*(U8**)a)[i] < (*(U8**)b)[i])
2112 if ((*(U8**)a)[i] > (*(U8**)b)[i])
2119 pmtrans(OP *o, OP *expr, OP *repl)
2121 SV *tstr = ((SVOP*)expr)->op_sv;
2122 SV *rstr = ((SVOP*)repl)->op_sv;
2125 register U8 *t = (U8*)SvPV(tstr, tlen);
2126 register U8 *r = (U8*)SvPV(rstr, rlen);
2132 register short *tbl;
2134 complement = o->op_private & OPpTRANS_COMPLEMENT;
2135 del = o->op_private & OPpTRANS_DELETE;
2136 squash = o->op_private & OPpTRANS_SQUASH;
2138 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
2139 SV* listsv = newSVpv("# comment\n",0);
2141 U8* tend = t + tlen;
2142 U8* rend = r + rlen;
2158 I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
2159 I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
2165 New(1109, cp, tlen, U8*);
2167 transv = newSVpv("",0);
2176 qsort(cp, i, sizeof(U8*), utf8compare);
2177 for (j = 0; j < i; j++) {
2179 UV val = utf8_to_uv(s, &ulen);
2181 diff = val - nextmin;
2183 t = uv_to_utf8(tmpbuf,nextmin);
2184 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2186 t = uv_to_utf8(tmpbuf, val - 1);
2187 sv_catpvn(transv, "\377", 1);
2188 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2192 val = utf8_to_uv(s+1, &ulen);
2196 t = uv_to_utf8(tmpbuf,nextmin);
2197 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2198 t = uv_to_utf8(tmpbuf, 0x7fffffff);
2199 sv_catpvn(transv, "\377", 1);
2200 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2201 t = (U8*)SvPVX(transv);
2202 tlen = SvCUR(transv);
2205 else if (!rlen && !del) {
2206 r = t; rlen = tlen; rend = tend;
2209 if (to_utf && from_utf) { /* only counting characters */
2210 if (t == r || (tlen == rlen && memEQ(t, r, tlen)))
2211 o->op_private |= OPpTRANS_IDENTICAL;
2213 else { /* straight latin-1 translation */
2214 if (tlen == 4 && memEQ(t, "\0\377\303\277", 4) &&
2215 rlen == 4 && memEQ(r, "\0\377\303\277", 4))
2216 o->op_private |= OPpTRANS_IDENTICAL;
2220 while (t < tend || tfirst <= tlast) {
2221 /* see if we need more "t" chars */
2222 if (tfirst > tlast) {
2223 tfirst = (I32)utf8_to_uv(t, &ulen);
2225 if (t < tend && *t == 0xff) { /* illegal utf8 val indicates range */
2226 tlast = (I32)utf8_to_uv(++t, &ulen);
2233 /* now see if we need more "r" chars */
2234 if (rfirst > rlast) {
2236 rfirst = (I32)utf8_to_uv(r, &ulen);
2238 if (r < rend && *r == 0xff) { /* illegal utf8 val indicates range */
2239 rlast = (I32)utf8_to_uv(++r, &ulen);
2248 rfirst = rlast = 0xffffffff;
2252 /* now see which range will peter our first, if either. */
2253 tdiff = tlast - tfirst;
2254 rdiff = rlast - rfirst;
2261 if (rfirst == 0xffffffff) {
2262 diff = tdiff; /* oops, pretend rdiff is infinite */
2264 sv_catpvf(listsv, "%04x\t%04x\tXXXX\n", tfirst, tlast);
2266 sv_catpvf(listsv, "%04x\t\tXXXX\n", tfirst);
2270 sv_catpvf(listsv, "%04x\t%04x\t%04x\n", tfirst, tfirst + diff, rfirst);
2272 sv_catpvf(listsv, "%04x\t\t%04x\n", tfirst, rfirst);
2274 if (rfirst + diff > max)
2275 max = rfirst + diff;
2280 else if (rfirst <= 0x800)
2281 grows |= (tfirst < 0x80);
2282 else if (rfirst <= 0x10000)
2283 grows |= (tfirst < 0x800);
2284 else if (rfirst <= 0x200000)
2285 grows |= (tfirst < 0x10000);
2286 else if (rfirst <= 0x4000000)
2287 grows |= (tfirst < 0x200000);
2288 else if (rfirst <= 0x80000000)
2289 grows |= (tfirst < 0x4000000);
2301 else if (max > 0xff)
2306 cSVOPo->op_sv = (SV*)swash_init("utf8", "", listsv, bits, none);
2307 SvREFCNT_dec(listsv);
2309 SvREFCNT_dec(transv);
2311 if (!del && havefinal)
2312 (void)hv_store((HV*)SvRV((cSVOPo->op_sv)), "FINAL", 5, newSViv((IV)final), 0);
2314 if (grows && to_utf)
2315 o->op_private |= OPpTRANS_GROWS;
2322 tbl = (short*)cPVOPo->op_pv;
2324 Zero(tbl, 256, short);
2325 for (i = 0; i < tlen; i++)
2327 for (i = 0, j = 0; i < 256; i++) {
2343 if (!rlen && !del) {
2346 o->op_private |= OPpTRANS_IDENTICAL;
2348 for (i = 0; i < 256; i++)
2350 for (i = 0, j = 0; i < tlen; i++,j++) {
2353 if (tbl[t[i]] == -1)
2359 if (tbl[t[i]] == -1)
2370 newPMOP(I32 type, I32 flags)
2375 Newz(1101, pmop, 1, PMOP);
2376 pmop->op_type = type;
2377 pmop->op_ppaddr = PL_ppaddr[type];
2378 pmop->op_flags = flags;
2379 pmop->op_private = 0 | (flags >> 8);
2381 if (PL_hints & HINT_RE_TAINT)
2382 pmop->op_pmpermflags |= PMf_RETAINT;
2383 if (PL_hints & HINT_LOCALE)
2384 pmop->op_pmpermflags |= PMf_LOCALE;
2385 pmop->op_pmflags = pmop->op_pmpermflags;
2387 /* link into pm list */
2388 if (type != OP_TRANS && PL_curstash) {
2389 pmop->op_pmnext = HvPMROOT(PL_curstash);
2390 HvPMROOT(PL_curstash) = pmop;
2397 pmruntime(OP *o, OP *expr, OP *repl)
2402 I32 repl_has_vars = 0;
2404 if (o->op_type == OP_TRANS)
2405 return pmtrans(o, expr, repl);
2407 PL_hints |= HINT_BLOCK_SCOPE;
2410 if (expr->op_type == OP_CONST) {
2412 SV *pat = ((SVOP*)expr)->op_sv;
2413 char *p = SvPV(pat, plen);
2414 if ((o->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
2415 sv_setpvn(pat, "\\s+", 3);
2416 p = SvPV(pat, plen);
2417 pm->op_pmflags |= PMf_SKIPWHITE;
2419 pm->op_pmregexp = CALLREGCOMP(p, p + plen, pm);
2420 if (strEQ("\\s+", pm->op_pmregexp->precomp))
2421 pm->op_pmflags |= PMf_WHITE;
2425 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
2426 expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
2428 : OP_REGCMAYBE),0,expr);
2430 Newz(1101, rcop, 1, LOGOP);
2431 rcop->op_type = OP_REGCOMP;
2432 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
2433 rcop->op_first = scalar(expr);
2434 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL)
2435 ? (OPf_SPECIAL | OPf_KIDS)
2437 rcop->op_private = 1;
2440 /* establish postfix order */
2441 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
2443 rcop->op_next = expr;
2444 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2447 rcop->op_next = LINKLIST(expr);
2448 expr->op_next = (OP*)rcop;
2451 prepend_elem(o->op_type, scalar((OP*)rcop), o);
2456 if (pm->op_pmflags & PMf_EVAL)
2459 else if (repl->op_type == OP_THREADSV
2460 && strchr("&`'123456789+",
2461 PL_threadsv_names[repl->op_targ]))
2465 #endif /* USE_THREADS */
2466 else if (repl->op_type == OP_CONST)
2470 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
2471 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
2473 if (curop->op_type == OP_THREADSV) {
2475 if (strchr("&`'123456789+", curop->op_private))
2479 if (curop->op_type == OP_GV) {
2480 GV *gv = ((GVOP*)curop)->op_gv;
2482 if (strchr("&`'123456789+", *GvENAME(gv)))
2485 #endif /* USE_THREADS */
2486 else if (curop->op_type == OP_RV2CV)
2488 else if (curop->op_type == OP_RV2SV ||
2489 curop->op_type == OP_RV2AV ||
2490 curop->op_type == OP_RV2HV ||
2491 curop->op_type == OP_RV2GV) {
2492 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2495 else if (curop->op_type == OP_PADSV ||
2496 curop->op_type == OP_PADAV ||
2497 curop->op_type == OP_PADHV ||
2498 curop->op_type == OP_PADANY) {
2501 else if (curop->op_type == OP_PUSHRE)
2502 ; /* Okay here, dangerous in newASSIGNOP */
2511 && (!pm->op_pmregexp
2512 || pm->op_pmregexp->reganch & ROPT_EVAL_SEEN))) {
2513 pm->op_pmflags |= PMf_CONST; /* const for long enough */
2514 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
2515 prepend_elem(o->op_type, scalar(repl), o);
2518 if (curop == repl && !pm->op_pmregexp) { /* Has variables. */
2519 pm->op_pmflags |= PMf_MAYBE_CONST;
2520 pm->op_pmpermflags |= PMf_MAYBE_CONST;
2522 Newz(1101, rcop, 1, LOGOP);
2523 rcop->op_type = OP_SUBSTCONT;
2524 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
2525 rcop->op_first = scalar(repl);
2526 rcop->op_flags |= OPf_KIDS;
2527 rcop->op_private = 1;
2530 /* establish postfix order */
2531 rcop->op_next = LINKLIST(repl);
2532 repl->op_next = (OP*)rcop;
2534 pm->op_pmreplroot = scalar((OP*)rcop);
2535 pm->op_pmreplstart = LINKLIST(rcop);
2544 newSVOP(I32 type, I32 flags, SV *sv)
2547 Newz(1101, svop, 1, SVOP);
2548 svop->op_type = type;
2549 svop->op_ppaddr = PL_ppaddr[type];
2551 svop->op_next = (OP*)svop;
2552 svop->op_flags = flags;
2553 if (PL_opargs[type] & OA_RETSCALAR)
2555 if (PL_opargs[type] & OA_TARGET)
2556 svop->op_targ = pad_alloc(type, SVs_PADTMP);
2557 return CHECKOP(type, svop);
2561 newGVOP(I32 type, I32 flags, GV *gv)
2565 Newz(1101, gvop, 1, GVOP);
2566 gvop->op_type = type;
2567 gvop->op_ppaddr = PL_ppaddr[type];
2568 gvop->op_gv = (GV*)SvREFCNT_inc(gv);
2569 gvop->op_next = (OP*)gvop;
2570 gvop->op_flags = flags;
2571 if (PL_opargs[type] & OA_RETSCALAR)
2573 if (PL_opargs[type] & OA_TARGET)
2574 gvop->op_targ = pad_alloc(type, SVs_PADTMP);
2575 return CHECKOP(type, gvop);
2579 newPVOP(I32 type, I32 flags, char *pv)
2582 Newz(1101, pvop, 1, PVOP);
2583 pvop->op_type = type;
2584 pvop->op_ppaddr = PL_ppaddr[type];
2586 pvop->op_next = (OP*)pvop;
2587 pvop->op_flags = flags;
2588 if (PL_opargs[type] & OA_RETSCALAR)
2590 if (PL_opargs[type] & OA_TARGET)
2591 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
2592 return CHECKOP(type, pvop);
2601 save_hptr(&PL_curstash);
2602 save_item(PL_curstname);
2607 name = SvPV(sv, len);
2608 PL_curstash = gv_stashpvn(name,len,TRUE);
2609 sv_setpvn(PL_curstname, name, len);
2613 sv_setpv(PL_curstname,"<none>");
2614 PL_curstash = Nullhv;
2616 PL_hints |= HINT_BLOCK_SCOPE;
2617 PL_copline = NOLINE;
2622 utilize(int aver, I32 floor, OP *version, OP *id, OP *arg)
2631 if (id->op_type != OP_CONST)
2632 croak("Module name must be constant");
2636 if(version != Nullop) {
2637 SV *vesv = ((SVOP*)version)->op_sv;
2639 if (arg == Nullop && !SvNIOK(vesv)) {
2646 if (version->op_type != OP_CONST || !SvNIOK(vesv))
2647 croak("Version number must be constant number");
2649 /* Make copy of id so we don't free it twice */
2650 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2652 /* Fake up a method call to VERSION */
2653 meth = newSVOP(OP_CONST, 0, newSVpv("VERSION", 7));
2654 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2655 append_elem(OP_LIST,
2656 prepend_elem(OP_LIST, pack, list(version)),
2657 newUNOP(OP_METHOD, 0, meth)));
2661 /* Fake up an import/unimport */
2662 if (arg && arg->op_type == OP_STUB)
2663 imop = arg; /* no import on explicit () */
2664 else if(SvNIOK(((SVOP*)id)->op_sv)) {
2665 imop = Nullop; /* use 5.0; */
2668 /* Make copy of id so we don't free it twice */
2669 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2670 meth = newSVOP(OP_CONST, 0,
2672 ? newSVpv("import", 6)
2673 : newSVpv("unimport", 8)
2675 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2676 append_elem(OP_LIST,
2677 prepend_elem(OP_LIST, pack, list(arg)),
2678 newUNOP(OP_METHOD, 0, meth)));
2681 /* Fake up a require, handle override, if any */
2682 gv = gv_fetchpv("require", FALSE, SVt_PVCV);
2683 if (!(gv && GvIMPORTED_CV(gv)))
2684 gv = gv_fetchpv("CORE::GLOBAL::require", FALSE, SVt_PVCV);
2686 if (gv && GvIMPORTED_CV(gv)) {
2687 rqop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
2688 append_elem(OP_LIST, id,
2689 scalar(newUNOP(OP_RV2CV, 0,
2694 rqop = newUNOP(OP_REQUIRE, 0, id);
2697 /* Fake up the BEGIN {}, which does its thing immediately. */
2699 newSVOP(OP_CONST, 0, newSVpv("BEGIN", 5)),
2701 append_elem(OP_LINESEQ,
2702 append_elem(OP_LINESEQ,
2703 newSTATEOP(0, Nullch, rqop),
2704 newSTATEOP(0, Nullch, veop)),
2705 newSTATEOP(0, Nullch, imop) ));
2707 PL_copline = NOLINE;
2717 gv = gv_fetchpv("do", FALSE, SVt_PVCV);
2718 if (!(gv && GvIMPORTED_CV(gv)))
2719 gv = gv_fetchpv("CORE::GLOBAL::do", FALSE, SVt_PVCV);
2721 if (gv && GvIMPORTED_CV(gv)) {
2722 doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
2723 append_elem(OP_LIST, term,
2724 scalar(newUNOP(OP_RV2CV, 0,
2729 doop = newUNOP(OP_DOFILE, 0, scalar(term));
2735 newSLICEOP(I32 flags, OP *subscript, OP *listval)
2737 return newBINOP(OP_LSLICE, flags,
2738 list(force_list(subscript)),
2739 list(force_list(listval)) );
2743 list_assignment(register OP *o)
2748 if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
2749 o = cUNOPo->op_first;
2751 if (o->op_type == OP_COND_EXPR) {
2752 I32 t = list_assignment(cCONDOPo->op_first->op_sibling);
2753 I32 f = list_assignment(cCONDOPo->op_first->op_sibling->op_sibling);
2758 yyerror("Assignment to both a list and a scalar");
2762 if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
2763 o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
2764 o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
2767 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
2770 if (o->op_type == OP_RV2SV)
2777 newASSIGNOP(I32 flags, OP *left, I32 optype, OP *right)
2782 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
2783 return newLOGOP(optype, 0,
2784 mod(scalar(left), optype),
2785 newUNOP(OP_SASSIGN, 0, scalar(right)));
2788 return newBINOP(optype, OPf_STACKED,
2789 mod(scalar(left), optype), scalar(right));
2793 if (list_assignment(left)) {
2796 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
2797 left = mod(left, OP_AASSIGN);
2805 o = newBINOP(OP_AASSIGN, flags,
2806 list(force_list(right)),
2807 list(force_list(left)) );
2808 o->op_private = 0 | (flags >> 8);
2809 if (!(left->op_private & OPpLVAL_INTRO)) {
2813 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
2814 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
2815 if (curop->op_type == OP_GV) {
2816 GV *gv = ((GVOP*)curop)->op_gv;
2817 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
2819 SvCUR(gv) = PL_generation;
2821 else if (curop->op_type == OP_PADSV ||
2822 curop->op_type == OP_PADAV ||
2823 curop->op_type == OP_PADHV ||
2824 curop->op_type == OP_PADANY) {
2825 SV **svp = AvARRAY(PL_comppad_name);
2826 SV *sv = svp[curop->op_targ];
2827 if (SvCUR(sv) == PL_generation)
2829 SvCUR(sv) = PL_generation; /* (SvCUR not used any more) */
2831 else if (curop->op_type == OP_RV2CV)
2833 else if (curop->op_type == OP_RV2SV ||
2834 curop->op_type == OP_RV2AV ||
2835 curop->op_type == OP_RV2HV ||
2836 curop->op_type == OP_RV2GV) {
2837 if (lastop->op_type != OP_GV) /* funny deref? */
2840 else if (curop->op_type == OP_PUSHRE) {
2841 if (((PMOP*)curop)->op_pmreplroot) {
2842 GV *gv = (GV*)((PMOP*)curop)->op_pmreplroot;
2843 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
2845 SvCUR(gv) = PL_generation;
2854 o->op_private = OPpASSIGN_COMMON;
2856 if (right && right->op_type == OP_SPLIT) {
2858 if ((tmpop = ((LISTOP*)right)->op_first) &&
2859 tmpop->op_type == OP_PUSHRE)
2861 PMOP *pm = (PMOP*)tmpop;
2862 if (left->op_type == OP_RV2AV &&
2863 !(left->op_private & OPpLVAL_INTRO) &&
2864 !(o->op_private & OPpASSIGN_COMMON) )
2866 tmpop = ((UNOP*)left)->op_first;
2867 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
2868 pm->op_pmreplroot = (OP*)((GVOP*)tmpop)->op_gv;
2869 pm->op_pmflags |= PMf_ONCE;
2870 tmpop = cUNOPo->op_first; /* to list (nulled) */
2871 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
2872 tmpop->op_sibling = Nullop; /* don't free split */
2873 right->op_next = tmpop->op_next; /* fix starting loc */
2874 op_free(o); /* blow off assign */
2875 right->op_flags &= ~OPf_WANT;
2876 /* "I don't know and I don't care." */
2881 if (PL_modcount < 10000 &&
2882 ((LISTOP*)right)->op_last->op_type == OP_CONST)
2884 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
2886 sv_setiv(sv, PL_modcount+1);
2894 right = newOP(OP_UNDEF, 0);
2895 if (right->op_type == OP_READLINE) {
2896 right->op_flags |= OPf_STACKED;
2897 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
2900 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
2901 o = newBINOP(OP_SASSIGN, flags,
2902 scalar(right), mod(scalar(left), OP_SASSIGN) );
2914 newSTATEOP(I32 flags, char *label, OP *o)
2917 U32 seq = intro_my();
2920 Newz(1101, cop, 1, COP);
2921 if (PERLDB_LINE && PL_curcop->cop_line && PL_curstash != PL_debstash) {
2922 cop->op_type = OP_DBSTATE;
2923 cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
2926 cop->op_type = OP_NEXTSTATE;
2927 cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
2929 cop->op_flags = flags;
2930 cop->op_private = (PL_hints & HINT_UTF8);
2932 cop->op_private |= NATIVE_HINTS;
2934 PL_compiling.op_private = cop->op_private;
2935 cop->op_next = (OP*)cop;
2938 cop->cop_label = label;
2939 PL_hints |= HINT_BLOCK_SCOPE;
2942 cop->cop_arybase = PL_curcop->cop_arybase;
2943 if (PL_curcop->cop_warnings == WARN_NONE
2944 || PL_curcop->cop_warnings == WARN_ALL)
2945 cop->cop_warnings = PL_curcop->cop_warnings ;
2947 cop->cop_warnings = newSVsv(PL_curcop->cop_warnings) ;
2950 if (PL_copline == NOLINE)
2951 cop->cop_line = PL_curcop->cop_line;
2953 cop->cop_line = PL_copline;
2954 PL_copline = NOLINE;
2956 cop->cop_filegv = (GV*)SvREFCNT_inc(PL_curcop->cop_filegv);
2957 cop->cop_stash = PL_curstash;
2959 if (PERLDB_LINE && PL_curstash != PL_debstash) {
2960 SV **svp = av_fetch(GvAV(PL_curcop->cop_filegv),(I32)cop->cop_line, FALSE);
2961 if (svp && *svp != &PL_sv_undef && !SvIOK(*svp)) {
2962 (void)SvIOK_on(*svp);
2964 SvSTASH(*svp) = (HV*)cop;
2968 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
2971 /* "Introduce" my variables to visible status. */
2979 if (! PL_min_intro_pending)
2980 return PL_cop_seqmax;
2982 svp = AvARRAY(PL_comppad_name);
2983 for (i = PL_min_intro_pending; i <= PL_max_intro_pending; i++) {
2984 if ((sv = svp[i]) && sv != &PL_sv_undef && !SvIVX(sv)) {
2985 SvIVX(sv) = PAD_MAX; /* Don't know scope end yet. */
2986 SvNVX(sv) = (double)PL_cop_seqmax;
2989 PL_min_intro_pending = 0;
2990 PL_comppad_name_fill = PL_max_intro_pending; /* Needn't search higher */
2991 return PL_cop_seqmax++;
2995 newLOGOP(I32 type, I32 flags, OP *first, OP *other)
2997 return new_logop(type, flags, &first, &other);
3001 new_logop(I32 type, I32 flags, OP** firstp, OP** otherp)
3006 OP *first = *firstp;
3007 OP *other = *otherp;
3009 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
3010 return newBINOP(type, flags, scalar(first), scalar(other));
3012 scalarboolean(first);
3013 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
3014 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
3015 if (type == OP_AND || type == OP_OR) {
3021 first = *firstp = cUNOPo->op_first;
3023 first->op_next = o->op_next;
3024 cUNOPo->op_first = Nullop;
3028 if (first->op_type == OP_CONST) {
3029 if (ckWARN(WARN_PRECEDENCE) && (first->op_private & OPpCONST_BARE))
3030 warner(WARN_PRECEDENCE, "Probable precedence problem on %s",
3032 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
3043 else if (first->op_type == OP_WANTARRAY) {
3049 else if (ckWARN(WARN_UNSAFE) && (first->op_flags & OPf_KIDS)) {
3050 OP *k1 = ((UNOP*)first)->op_first;
3051 OP *k2 = k1->op_sibling;
3053 switch (first->op_type)
3056 if (k2 && k2->op_type == OP_READLINE
3057 && (k2->op_flags & OPf_STACKED)
3058 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3059 warnop = k2->op_type;
3063 if (k1->op_type == OP_READDIR
3064 || k1->op_type == OP_GLOB
3065 || k1->op_type == OP_EACH)
3066 warnop = k1->op_type;
3070 line_t oldline = PL_curcop->cop_line;
3071 PL_curcop->cop_line = PL_copline;
3073 "Value of %s%s can be \"0\"; test with defined()",
3075 ((warnop == OP_READLINE || warnop == OP_GLOB)
3076 ? " construct" : "() operator"));
3077 PL_curcop->cop_line = oldline;
3084 if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
3085 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
3087 Newz(1101, logop, 1, LOGOP);
3089 logop->op_type = type;
3090 logop->op_ppaddr = PL_ppaddr[type];
3091 logop->op_first = first;
3092 logop->op_flags = flags | OPf_KIDS;
3093 logop->op_other = LINKLIST(other);
3094 logop->op_private = 1 | (flags >> 8);
3096 /* establish postfix order */
3097 logop->op_next = LINKLIST(first);
3098 first->op_next = (OP*)logop;
3099 first->op_sibling = other;
3101 o = newUNOP(OP_NULL, 0, (OP*)logop);
3108 newCONDOP(I32 flags, OP *first, OP *trueop, OP *falseop)
3115 return newLOGOP(OP_AND, 0, first, trueop);
3117 return newLOGOP(OP_OR, 0, first, falseop);
3119 scalarboolean(first);
3120 if (first->op_type == OP_CONST) {
3121 if (SvTRUE(((SVOP*)first)->op_sv)) {
3132 else if (first->op_type == OP_WANTARRAY) {
3136 Newz(1101, condop, 1, CONDOP);
3138 condop->op_type = OP_COND_EXPR;
3139 condop->op_ppaddr = PL_ppaddr[OP_COND_EXPR];
3140 condop->op_first = first;
3141 condop->op_flags = flags | OPf_KIDS;
3142 condop->op_true = LINKLIST(trueop);
3143 condop->op_false = LINKLIST(falseop);
3144 condop->op_private = 1 | (flags >> 8);
3146 /* establish postfix order */
3147 condop->op_next = LINKLIST(first);
3148 first->op_next = (OP*)condop;
3150 first->op_sibling = trueop;
3151 trueop->op_sibling = falseop;
3152 o = newUNOP(OP_NULL, 0, (OP*)condop);
3154 trueop->op_next = o;
3155 falseop->op_next = o;
3161 newRANGE(I32 flags, OP *left, OP *right)
3169 Newz(1101, condop, 1, CONDOP);
3171 condop->op_type = OP_RANGE;
3172 condop->op_ppaddr = PL_ppaddr[OP_RANGE];
3173 condop->op_first = left;
3174 condop->op_flags = OPf_KIDS;
3175 condop->op_true = LINKLIST(left);
3176 condop->op_false = LINKLIST(right);
3177 condop->op_private = 1 | (flags >> 8);
3179 left->op_sibling = right;
3181 condop->op_next = (OP*)condop;
3182 flip = newUNOP(OP_FLIP, flags, (OP*)condop);
3183 flop = newUNOP(OP_FLOP, 0, flip);
3184 o = newUNOP(OP_NULL, 0, flop);
3187 left->op_next = flip;
3188 right->op_next = flop;
3190 condop->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3191 sv_upgrade(PAD_SV(condop->op_targ), SVt_PVNV);
3192 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3193 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
3195 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3196 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3199 if (!flip->op_private || !flop->op_private)
3200 linklist(o); /* blow off optimizer unless constant */
3206 newLOOPOP(I32 flags, I32 debuggable, OP *expr, OP *block)
3211 int once = block && block->op_flags & OPf_SPECIAL &&
3212 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
3215 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
3216 return block; /* do {} while 0 does once */
3217 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3218 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
3219 expr = newUNOP(OP_DEFINED, 0,
3220 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3221 } else if (expr->op_flags & OPf_KIDS) {
3222 OP *k1 = ((UNOP*)expr)->op_first;
3223 OP *k2 = (k1) ? k1->op_sibling : NULL;
3224 switch (expr->op_type) {
3226 if (k2 && k2->op_type == OP_READLINE
3227 && (k2->op_flags & OPf_STACKED)
3228 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3229 expr = newUNOP(OP_DEFINED, 0, expr);
3233 if (k1->op_type == OP_READDIR
3234 || k1->op_type == OP_GLOB
3235 || k1->op_type == OP_EACH)
3236 expr = newUNOP(OP_DEFINED, 0, expr);
3242 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
3243 o = new_logop(OP_AND, 0, &expr, &listop);
3246 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
3248 if (once && o != listop)
3249 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
3252 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
3254 o->op_flags |= flags;
3256 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
3261 newWHILEOP(I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *block, OP *cont)
3270 if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3271 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB))) {
3272 expr = newUNOP(OP_DEFINED, 0,
3273 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3274 } else if (expr && (expr->op_flags & OPf_KIDS)) {
3275 OP *k1 = ((UNOP*)expr)->op_first;
3276 OP *k2 = (k1) ? k1->op_sibling : NULL;
3277 switch (expr->op_type) {
3279 if (k2 && k2->op_type == OP_READLINE
3280 && (k2->op_flags & OPf_STACKED)
3281 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3282 expr = newUNOP(OP_DEFINED, 0, expr);
3286 if (k1->op_type == OP_READDIR
3287 || k1->op_type == OP_GLOB
3288 || k1->op_type == OP_EACH)
3289 expr = newUNOP(OP_DEFINED, 0, expr);
3295 block = newOP(OP_NULL, 0);
3298 next = LINKLIST(cont);
3300 cont = append_elem(OP_LINESEQ, cont, newOP(OP_UNSTACK, 0));
3301 if ((line_t)whileline != NOLINE) {
3302 PL_copline = whileline;
3303 cont = append_elem(OP_LINESEQ, cont,
3304 newSTATEOP(0, Nullch, Nullop));
3308 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
3309 redo = LINKLIST(listop);
3312 PL_copline = whileline;
3314 o = new_logop(OP_AND, 0, &expr, &listop);
3315 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
3316 op_free(expr); /* oops, it's a while (0) */
3318 return Nullop; /* listop already freed by new_logop */
3321 ((LISTOP*)listop)->op_last->op_next = condop =
3322 (o == listop ? redo : LINKLIST(o));
3330 Newz(1101,loop,1,LOOP);
3331 loop->op_type = OP_ENTERLOOP;
3332 loop->op_ppaddr = PL_ppaddr[OP_ENTERLOOP];
3333 loop->op_private = 0;
3334 loop->op_next = (OP*)loop;
3337 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
3339 loop->op_redoop = redo;
3340 loop->op_lastop = o;
3343 loop->op_nextop = next;
3345 loop->op_nextop = o;
3347 o->op_flags |= flags;
3348 o->op_private |= (flags >> 8);
3353 newFOROP(I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
3361 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
3362 sv->op_type = OP_RV2GV;
3363 sv->op_ppaddr = PL_ppaddr[OP_RV2GV];
3365 else if (sv->op_type == OP_PADSV) { /* private variable */
3366 padoff = sv->op_targ;
3370 else if (sv->op_type == OP_THREADSV) { /* per-thread variable */
3371 padoff = sv->op_targ;
3372 iterflags |= OPf_SPECIAL;
3377 croak("Can't use %s for loop variable", PL_op_desc[sv->op_type]);
3381 padoff = find_threadsv("_");
3382 iterflags |= OPf_SPECIAL;
3384 sv = newGVOP(OP_GV, 0, PL_defgv);
3387 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
3388 expr = mod(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART);
3389 iterflags |= OPf_STACKED;
3391 else if (expr->op_type == OP_NULL &&
3392 (expr->op_flags & OPf_KIDS) &&
3393 ((BINOP*)expr)->op_first->op_type == OP_FLOP)
3395 /* Basically turn for($x..$y) into the same as for($x,$y), but we
3396 * set the STACKED flag to indicate that these values are to be
3397 * treated as min/max values by 'pp_iterinit'.
3399 UNOP* flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
3400 CONDOP* range = (CONDOP*) flip->op_first;
3401 OP* left = range->op_first;
3402 OP* right = left->op_sibling;
3405 range->op_flags &= ~OPf_KIDS;
3406 range->op_first = Nullop;
3408 listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
3409 listop->op_first->op_next = range->op_true;
3410 left->op_next = range->op_false;
3411 right->op_next = (OP*)listop;
3412 listop->op_next = listop->op_first;
3415 expr = (OP*)(listop);
3417 iterflags |= OPf_STACKED;
3420 expr = mod(force_list(expr), OP_GREPSTART);
3424 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
3425 append_elem(OP_LIST, expr, scalar(sv))));
3426 assert(!loop->op_next);
3427 Renew(loop, 1, LOOP);
3428 loop->op_targ = padoff;
3429 wop = newWHILEOP(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont);
3430 PL_copline = forline;
3431 return newSTATEOP(0, label, wop);
3435 newLOOPEX(I32 type, OP *label)
3441 if (type != OP_GOTO || label->op_type == OP_CONST) {
3442 /* "last()" means "last" */
3443 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS))
3444 o = newOP(type, OPf_SPECIAL);
3446 o = newPVOP(type, 0, savepv(label->op_type == OP_CONST
3447 ? SvPVx(((SVOP*)label)->op_sv, n_a)
3453 if (label->op_type == OP_ENTERSUB)
3454 label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
3455 o = newUNOP(type, OPf_STACKED, label);
3457 PL_hints |= HINT_BLOCK_SCOPE;
3467 MUTEX_DESTROY(CvMUTEXP(cv));
3468 Safefree(CvMUTEXP(cv));
3471 #endif /* USE_THREADS */
3473 if (!CvXSUB(cv) && CvROOT(cv)) {
3475 if (CvDEPTH(cv) || (CvOWNER(cv) && CvOWNER(cv) != thr))
3476 croak("Can't undef active subroutine");
3479 croak("Can't undef active subroutine");
3480 #endif /* USE_THREADS */
3483 SAVESPTR(PL_curpad);
3487 op_free(CvROOT(cv));
3488 CvROOT(cv) = Nullop;
3491 SvPOK_off((SV*)cv); /* forget prototype */
3493 SvREFCNT_dec(CvGV(cv));
3495 SvREFCNT_dec(CvOUTSIDE(cv));
3496 CvOUTSIDE(cv) = Nullcv;
3497 if (CvPADLIST(cv)) {
3498 /* may be during global destruction */
3499 if (SvREFCNT(CvPADLIST(cv))) {
3500 I32 i = AvFILLp(CvPADLIST(cv));
3502 SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE);
3503 SV* sv = svp ? *svp : Nullsv;
3506 if (sv == (SV*)PL_comppad_name)
3507 PL_comppad_name = Nullav;
3508 else if (sv == (SV*)PL_comppad) {
3509 PL_comppad = Nullav;
3510 PL_curpad = Null(SV**);
3514 SvREFCNT_dec((SV*)CvPADLIST(cv));
3516 CvPADLIST(cv) = Nullav;
3520 #ifdef DEBUG_CLOSURES
3525 CV *outside = CvOUTSIDE(cv);
3526 AV* padlist = CvPADLIST(cv);
3533 PerlIO_printf(Perl_debug_log, "\tCV=0x%lx (%s), OUTSIDE=0x%lx (%s)\n",
3535 (CvANON(cv) ? "ANON"
3536 : (cv == PL_main_cv) ? "MAIN"
3537 : CvUNIQUE(cv) ? "UNIQUE"
3538 : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"),
3541 : CvANON(outside) ? "ANON"
3542 : (outside == PL_main_cv) ? "MAIN"
3543 : CvUNIQUE(outside) ? "UNIQUE"
3544 : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED"));
3549 pad_name = (AV*)*av_fetch(padlist, 0, FALSE);
3550 pad = (AV*)*av_fetch(padlist, 1, FALSE);
3551 pname = AvARRAY(pad_name);
3552 ppad = AvARRAY(pad);
3554 for (ix = 1; ix <= AvFILLp(pad_name); ix++) {
3555 if (SvPOK(pname[ix]))
3556 PerlIO_printf(Perl_debug_log, "\t%4d. 0x%lx (%s\"%s\" %ld-%ld)\n",
3558 SvFAKE(pname[ix]) ? "FAKE " : "",
3560 (long)I_32(SvNVX(pname[ix])),
3561 (long)SvIVX(pname[ix]));
3564 #endif /* DEBUG_CLOSURES */
3567 cv_clone2(CV *proto, CV *outside)
3572 AV* protopadlist = CvPADLIST(proto);
3573 AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE);
3574 AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE);
3575 SV** pname = AvARRAY(protopad_name);
3576 SV** ppad = AvARRAY(protopad);
3577 I32 fname = AvFILLp(protopad_name);
3578 I32 fpad = AvFILLp(protopad);
3582 assert(!CvUNIQUE(proto));
3585 SAVESPTR(PL_curpad);
3586 SAVESPTR(PL_comppad);
3587 SAVESPTR(PL_comppad_name);
3588 SAVESPTR(PL_compcv);
3590 cv = PL_compcv = (CV*)NEWSV(1104,0);
3591 sv_upgrade((SV *)cv, SvTYPE(proto));
3597 New(666, CvMUTEXP(cv), 1, perl_mutex);
3598 MUTEX_INIT(CvMUTEXP(cv));
3600 #endif /* USE_THREADS */
3601 CvFILEGV(cv) = CvFILEGV(proto);
3602 CvGV(cv) = (GV*)SvREFCNT_inc(CvGV(proto));
3603 CvSTASH(cv) = CvSTASH(proto);
3604 CvROOT(cv) = CvROOT(proto);
3605 CvSTART(cv) = CvSTART(proto);
3607 CvOUTSIDE(cv) = (CV*)SvREFCNT_inc(outside);
3610 sv_setpvn((SV*)cv, SvPVX(proto), SvCUR(proto));
3612 PL_comppad_name = newAV();
3613 for (ix = fname; ix >= 0; ix--)
3614 av_store(PL_comppad_name, ix, SvREFCNT_inc(pname[ix]));
3616 PL_comppad = newAV();
3618 comppadlist = newAV();
3619 AvREAL_off(comppadlist);
3620 av_store(comppadlist, 0, (SV*)PL_comppad_name);
3621 av_store(comppadlist, 1, (SV*)PL_comppad);
3622 CvPADLIST(cv) = comppadlist;
3623 av_fill(PL_comppad, AvFILLp(protopad));
3624 PL_curpad = AvARRAY(PL_comppad);
3626 av = newAV(); /* will be @_ */
3628 av_store(PL_comppad, 0, (SV*)av);
3629 AvFLAGS(av) = AVf_REIFY;
3631 for (ix = fpad; ix > 0; ix--) {
3632 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
3633 if (namesv && namesv != &PL_sv_undef) {
3634 char *name = SvPVX(namesv); /* XXX */
3635 if (SvFLAGS(namesv) & SVf_FAKE) { /* lexical from outside? */
3636 I32 off = pad_findlex(name, ix, SvIVX(namesv),
3637 CvOUTSIDE(cv), cxstack_ix, 0);
3639 PL_curpad[ix] = SvREFCNT_inc(ppad[ix]);
3641 croak("panic: cv_clone: %s", name);
3643 else { /* our own lexical */
3646 /* anon code -- we'll come back for it */
3647 sv = SvREFCNT_inc(ppad[ix]);
3649 else if (*name == '@')
3651 else if (*name == '%')
3661 SV* sv = NEWSV(0,0);
3667 /* Now that vars are all in place, clone nested closures. */
3669 for (ix = fpad; ix > 0; ix--) {
3670 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
3672 && namesv != &PL_sv_undef
3673 && !(SvFLAGS(namesv) & SVf_FAKE)
3674 && *SvPVX(namesv) == '&'
3675 && CvCLONE(ppad[ix]))
3677 CV *kid = cv_clone2((CV*)ppad[ix], cv);
3678 SvREFCNT_dec(ppad[ix]);
3681 PL_curpad[ix] = (SV*)kid;
3685 #ifdef DEBUG_CLOSURES
3686 PerlIO_printf(Perl_debug_log, "Cloned inside:\n");
3688 PerlIO_printf(Perl_debug_log, " from:\n");
3690 PerlIO_printf(Perl_debug_log, " to:\n");
3702 MUTEX_LOCK(&PL_cred_mutex); /* XXX create separate mutex */
3703 cv = cv_clone2(proto, CvOUTSIDE(proto));
3704 MUTEX_UNLOCK(&PL_cred_mutex); /* XXX create separate mutex */
3709 cv_ckproto(CV *cv, GV *gv, char *p)
3711 if ((!p != !SvPOK(cv)) || (p && strNE(p, SvPVX(cv)))) {
3712 SV* msg = sv_newmortal();
3716 gv_efullname3(name = sv_newmortal(), gv, Nullch);
3717 sv_setpv(msg, "Prototype mismatch:");
3719 sv_catpvf(msg, " sub %_", name);
3721 sv_catpvf(msg, " (%s)", SvPVX(cv));
3722 sv_catpv(msg, " vs ");
3724 sv_catpvf(msg, "(%s)", p);
3726 sv_catpv(msg, "none");
3734 if (!cv || !SvPOK(cv) || SvCUR(cv))
3736 return op_const_sv(CvSTART(cv), cv);
3740 op_const_sv(OP *o, CV *cv)
3747 if(o->op_type == OP_LINESEQ && cLISTOPo->op_first)
3748 o = cLISTOPo->op_first->op_sibling;
3750 for (; o; o = o->op_next) {
3751 OPCODE type = o->op_type;
3753 if(sv && o->op_next == o)
3755 if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
3757 if (type == OP_LEAVESUB || type == OP_RETURN)
3761 if (type == OP_CONST)
3763 else if (type == OP_PADSV && cv) {
3764 AV* padav = (AV*)(AvARRAY(CvPADLIST(cv))[1]);
3765 sv = padav ? AvARRAY(padav)[o->op_targ] : Nullsv;
3766 if (!sv || (!SvREADONLY(sv) && SvREFCNT(sv) > 1))
3778 newSUB(I32 floor, OP *o, OP *proto, OP *block)
3782 char *name = o ? SvPVx(cSVOPo->op_sv, n_a) : Nullch;
3783 GV *gv = gv_fetchpv(name ? name : "__ANON__",
3784 GV_ADDMULTI | (block ? 0 : GV_NOINIT), SVt_PVCV);
3785 char *ps = proto ? SvPVx(((SVOP*)proto)->op_sv, n_a) : Nullch;
3794 if (SvTYPE(gv) != SVt_PVGV) { /* Prototype now, and had
3795 maximum a prototype before. */
3796 if (SvTYPE(gv) > SVt_NULL) {
3797 if (!SvPOK((SV*)gv) && !(SvIOK((SV*)gv) && SvIVX((SV*)gv) == -1))
3798 warn("Runaway prototype");
3799 cv_ckproto((CV*)gv, NULL, ps);
3802 sv_setpv((SV*)gv, ps);
3804 sv_setiv((SV*)gv, -1);
3805 SvREFCNT_dec(PL_compcv);
3806 cv = PL_compcv = NULL;
3807 PL_sub_generation++;
3811 if (!name || GvCVGEN(gv))
3813 else if (cv = GvCV(gv)) {
3814 cv_ckproto(cv, gv, ps);
3815 /* already defined (or promised)? */
3816 if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
3818 bool const_changed = TRUE;
3820 /* just a "sub foo;" when &foo is already defined */
3821 SAVEFREESV(PL_compcv);
3824 /* ahem, death to those who redefine active sort subs */
3825 if (PL_curstackinfo->si_type == PERLSI_SORT && PL_sortcop == CvSTART(cv))
3826 croak("Can't redefine active sort subroutine %s", name);
3827 if(const_sv = cv_const_sv(cv))
3828 const_changed = sv_cmp(const_sv, op_const_sv(block, Nullcv));
3829 if ((const_sv && const_changed) || ckWARN(WARN_REDEFINE)
3830 && !(CvGV(cv) && GvSTASH(CvGV(cv))
3831 && HvNAME(GvSTASH(CvGV(cv)))
3832 && strEQ(HvNAME(GvSTASH(CvGV(cv))),
3834 line_t oldline = PL_curcop->cop_line;
3835 PL_curcop->cop_line = PL_copline;
3836 warner(WARN_REDEFINE,
3837 const_sv ? "Constant subroutine %s redefined"
3838 : "Subroutine %s redefined", name);
3839 PL_curcop->cop_line = oldline;
3845 if (cv) { /* must reuse cv if autoloaded */
3847 CvFLAGS(cv) = CvFLAGS(PL_compcv);
3848 CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv);
3849 CvOUTSIDE(PL_compcv) = 0;
3850 CvPADLIST(cv) = CvPADLIST(PL_compcv);
3851 CvPADLIST(PL_compcv) = 0;
3852 if (SvREFCNT(PL_compcv) > 1) /* XXX Make closures transit through stub. */
3853 CvOUTSIDE(PL_compcv) = (CV*)SvREFCNT_inc((SV*)cv);
3854 SvREFCNT_dec(PL_compcv);
3861 PL_sub_generation++;
3864 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
3865 CvFILEGV(cv) = PL_curcop->cop_filegv;
3866 CvSTASH(cv) = PL_curstash;
3869 if (!CvMUTEXP(cv)) {
3870 New(666, CvMUTEXP(cv), 1, perl_mutex);
3871 MUTEX_INIT(CvMUTEXP(cv));
3873 #endif /* USE_THREADS */
3876 sv_setpv((SV*)cv, ps);
3878 if (PL_error_count) {
3882 char *s = strrchr(name, ':');
3884 if (strEQ(s, "BEGIN")) {
3886 "BEGIN not safe after errors--compilation aborted";
3890 /* force display of errors found but not reported */
3891 sv_catpv(ERRSV, not_safe);
3892 croak("%s", SvPVx(ERRSV, n_a));
3899 PL_copline = NOLINE;
3904 if (AvFILLp(PL_comppad_name) < AvFILLp(PL_comppad))
3905 av_store(PL_comppad_name, AvFILLp(PL_comppad), Nullsv);
3908 SV **namep = AvARRAY(PL_comppad_name);
3909 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
3912 if (SvIMMORTAL(PL_curpad[ix]))
3915 * The only things that a clonable function needs in its
3916 * pad are references to outer lexicals and anonymous subs.
3917 * The rest are created anew during cloning.
3919 if (!((namesv = namep[ix]) != Nullsv &&
3920 namesv != &PL_sv_undef &&
3922 *SvPVX(namesv) == '&')))
3924 SvREFCNT_dec(PL_curpad[ix]);
3925 PL_curpad[ix] = Nullsv;
3930 AV *av = newAV(); /* Will be @_ */
3932 av_store(PL_comppad, 0, (SV*)av);
3933 AvFLAGS(av) = AVf_REIFY;
3935 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
3936 if (SvIMMORTAL(PL_curpad[ix]))
3938 if (!SvPADMY(PL_curpad[ix]))
3939 SvPADTMP_on(PL_curpad[ix]);
3943 CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
3944 CvSTART(cv) = LINKLIST(CvROOT(cv));
3945 CvROOT(cv)->op_next = 0;
3951 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
3952 SV *sv = NEWSV(0,0);
3953 SV *tmpstr = sv_newmortal();
3954 GV *db_postponed = gv_fetchpv("DB::postponed", GV_ADDMULTI, SVt_PVHV);
3958 sv_setpvf(sv, "%_:%ld-%ld",
3959 GvSV(PL_curcop->cop_filegv),
3960 (long)PL_subline, (long)PL_curcop->cop_line);
3961 gv_efullname3(tmpstr, gv, Nullch);
3962 hv_store(GvHV(PL_DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0);
3963 hv = GvHVn(db_postponed);
3964 if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX(tmpstr), SvCUR(tmpstr))
3965 && (cv = GvCV(db_postponed))) {
3970 perl_call_sv((SV*)cv, G_DISCARD);
3974 if ((s = strrchr(name,':')))
3978 if (strEQ(s, "BEGIN")) {
3979 I32 oldscope = PL_scopestack_ix;
3981 SAVESPTR(PL_compiling.cop_filegv);
3982 SAVEI16(PL_compiling.cop_line);
3984 sv_setsv(PL_rs, PL_nrs);
3987 PL_beginav = newAV();
3988 DEBUG_x( dump_sub(gv) );
3989 av_push(PL_beginav, (SV *)cv);
3991 call_list(oldscope, PL_beginav);
3993 PL_curcop = &PL_compiling;
3994 PL_compiling.op_private = PL_hints;
3997 else if (strEQ(s, "END") && !PL_error_count) {
4000 av_unshift(PL_endav, 1);
4001 av_store(PL_endav, 0, (SV *)cv);
4004 else if (strEQ(s, "INIT") && !PL_error_count) {
4006 PL_initav = newAV();
4007 av_push(PL_initav, SvREFCNT_inc(cv));
4013 PL_copline = NOLINE;
4018 /* XXX unsafe for threads if eval_owner isn't held */
4020 newCONSTSUB(HV *stash, char *name, SV *sv)
4023 U32 oldhints = PL_hints;
4024 HV *old_cop_stash = PL_curcop->cop_stash;
4025 HV *old_curstash = PL_curstash;
4026 line_t oldline = PL_curcop->cop_line;
4027 PL_curcop->cop_line = PL_copline;
4029 PL_hints &= ~HINT_BLOCK_SCOPE;
4031 PL_curstash = PL_curcop->cop_stash = stash;
4034 start_subparse(FALSE, 0),
4035 newSVOP(OP_CONST, 0, newSVpv(name,0)),
4036 newSVOP(OP_CONST, 0, &PL_sv_no), /* SvPV(&PL_sv_no) == "" -- GMB */
4037 newSTATEOP(0, Nullch, newSVOP(OP_CONST, 0, sv))
4040 PL_hints = oldhints;
4041 PL_curcop->cop_stash = old_cop_stash;
4042 PL_curstash = old_curstash;
4043 PL_curcop->cop_line = oldline;
4047 newXS(char *name, void (*subaddr) (CV * _CPERLproto), char *filename)
4050 GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV);
4053 if (cv = (name ? GvCV(gv) : Nullcv)) {
4055 /* just a cached method */
4059 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
4060 /* already defined (or promised) */
4061 if (ckWARN(WARN_REDEFINE) && !(CvGV(cv) && GvSTASH(CvGV(cv))
4062 && HvNAME(GvSTASH(CvGV(cv)))
4063 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "autouse"))) {
4064 line_t oldline = PL_curcop->cop_line;
4065 if (PL_copline != NOLINE)
4066 PL_curcop->cop_line = PL_copline;
4067 warner(WARN_REDEFINE, "Subroutine %s redefined",name);
4068 PL_curcop->cop_line = oldline;
4075 if (cv) /* must reuse cv if autoloaded */
4078 cv = (CV*)NEWSV(1105,0);
4079 sv_upgrade((SV *)cv, SVt_PVCV);
4083 PL_sub_generation++;
4086 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4088 New(666, CvMUTEXP(cv), 1, perl_mutex);
4089 MUTEX_INIT(CvMUTEXP(cv));
4091 #endif /* USE_THREADS */
4092 CvFILEGV(cv) = gv_fetchfile(filename);
4093 CvXSUB(cv) = subaddr;
4096 char *s = strrchr(name,':');
4101 if (strEQ(s, "BEGIN")) {
4103 PL_beginav = newAV();
4104 av_push(PL_beginav, (SV *)cv);
4107 else if (strEQ(s, "END")) {
4110 av_unshift(PL_endav, 1);
4111 av_store(PL_endav, 0, (SV *)cv);
4114 else if (strEQ(s, "INIT")) {
4116 PL_initav = newAV();
4117 av_push(PL_initav, (SV *)cv);
4128 newFORM(I32 floor, OP *o, OP *block)
4138 name = SvPVx(cSVOPo->op_sv, n_a);
4141 gv = gv_fetchpv(name,TRUE, SVt_PVFM);
4143 if (cv = GvFORM(gv)) {
4144 if (ckWARN(WARN_REDEFINE)) {
4145 line_t oldline = PL_curcop->cop_line;
4147 PL_curcop->cop_line = PL_copline;
4148 warner(WARN_REDEFINE, "Format %s redefined",name);
4149 PL_curcop->cop_line = oldline;
4155 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4156 CvFILEGV(cv) = PL_curcop->cop_filegv;
4158 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4159 if (!SvPADMY(PL_curpad[ix]) && !SvIMMORTAL(PL_curpad[ix]))
4160 SvPADTMP_on(PL_curpad[ix]);
4163 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
4164 CvSTART(cv) = LINKLIST(CvROOT(cv));
4165 CvROOT(cv)->op_next = 0;
4168 PL_copline = NOLINE;
4175 return newUNOP(OP_REFGEN, 0,
4176 mod(list(convert(OP_ANONLIST, 0, o)), OP_REFGEN));
4182 return newUNOP(OP_REFGEN, 0,
4183 mod(list(convert(OP_ANONHASH, 0, o)), OP_REFGEN));
4187 newANONSUB(I32 floor, OP *proto, OP *block)
4189 return newUNOP(OP_REFGEN, 0,
4190 newSVOP(OP_ANONCODE, 0, (SV*)newSUB(floor, 0, proto, block)));
4196 switch (o->op_type) {
4198 o->op_type = OP_PADAV;
4199 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4200 return ref(o, OP_RV2AV);
4203 o->op_type = OP_RV2AV;
4204 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
4209 warn("oops: oopsAV");
4218 switch (o->op_type) {
4221 o->op_type = OP_PADHV;
4222 o->op_ppaddr = PL_ppaddr[OP_PADHV];
4223 return ref(o, OP_RV2HV);
4227 o->op_type = OP_RV2HV;
4228 o->op_ppaddr = PL_ppaddr[OP_RV2HV];
4233 warn("oops: oopsHV");
4242 if (o->op_type == OP_PADANY) {
4243 o->op_type = OP_PADAV;
4244 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4247 return newUNOP(OP_RV2AV, 0, scalar(o));
4251 newGVREF(I32 type, OP *o)
4253 if (type == OP_MAPSTART)
4254 return newUNOP(OP_NULL, 0, o);
4255 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
4261 if (o->op_type == OP_PADANY) {
4262 o->op_type = OP_PADHV;
4263 o->op_ppaddr = PL_ppaddr[OP_PADHV];
4266 return newUNOP(OP_RV2HV, 0, scalar(o));
4272 croak("NOT IMPL LINE %d",__LINE__);
4278 newCVREF(I32 flags, OP *o)
4280 return newUNOP(OP_RV2CV, flags, scalar(o));
4286 if (o->op_type == OP_PADANY) {
4287 o->op_type = OP_PADSV;
4288 o->op_ppaddr = PL_ppaddr[OP_PADSV];
4291 else if (o->op_type == OP_THREADSV && !(o->op_flags & OPpDONE_SVREF)) {
4292 o->op_flags |= OPpDONE_SVREF;
4295 return newUNOP(OP_RV2SV, 0, scalar(o));
4298 /* Check routines. */
4306 name = NEWSV(1106,0);
4307 sv_upgrade(name, SVt_PVNV);
4308 sv_setpvn(name, "&", 1);
4311 ix = pad_alloc(o->op_type, SVs_PADMY);
4312 av_store(PL_comppad_name, ix, name);
4313 av_store(PL_comppad, ix, cSVOPo->op_sv);
4314 SvPADMY_on(cSVOPo->op_sv);
4315 cSVOPo->op_sv = Nullsv;
4316 cSVOPo->op_targ = ix;
4323 o->op_private = PL_hints;
4330 if (cUNOPo->op_first->op_type == OP_CONCAT)
4331 o->op_flags |= OPf_STACKED;
4338 if (o->op_flags & OPf_KIDS) {
4341 OPCODE type = o->op_type;
4342 o = modkids(ck_fun(o), type);
4343 kid = cUNOPo->op_first;
4344 newop = kUNOP->op_first->op_sibling;
4346 (newop->op_sibling ||
4347 !(PL_opargs[newop->op_type] & OA_RETSCALAR) ||
4348 newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
4349 newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
4353 op_free(kUNOP->op_first);
4354 kUNOP->op_first = newop;
4356 o->op_ppaddr = PL_ppaddr[++o->op_type];
4365 if (o->op_flags & OPf_KIDS) {
4366 OP *kid = cUNOPo->op_first;
4367 if (kid->op_type == OP_HSLICE)
4368 o->op_private |= OPpSLICE;
4369 else if (kid->op_type != OP_HELEM)
4370 croak("%s argument is not a HASH element or slice",
4371 PL_op_desc[o->op_type]);
4380 I32 type = o->op_type;
4382 if (o->op_flags & OPf_KIDS) {
4383 if (cLISTOPo->op_first->op_type == OP_STUB) {
4385 o = newUNOP(type, OPf_SPECIAL,
4386 newGVOP(OP_GV, 0, gv_fetchpv("main::ARGV", TRUE, SVt_PVAV)));
4396 PL_hints |= HINT_BLOCK_SCOPE;
4397 if (o->op_flags & OPf_KIDS) {
4398 SVOP *kid = (SVOP*)cUNOPo->op_first;
4401 o->op_flags &= ~OPf_KIDS;
4404 else if (kid->op_type == OP_LINESEQ) {
4407 kid->op_next = o->op_next;
4408 cUNOPo->op_first = 0;
4411 Newz(1101, enter, 1, LOGOP);
4412 enter->op_type = OP_ENTERTRY;
4413 enter->op_ppaddr = PL_ppaddr[OP_ENTERTRY];
4414 enter->op_private = 0;
4416 /* establish postfix order */
4417 enter->op_next = (OP*)enter;
4419 o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
4420 o->op_type = OP_LEAVETRY;
4421 o->op_ppaddr = PL_ppaddr[OP_LEAVETRY];
4422 enter->op_other = o;
4430 o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP());
4432 o->op_targ = (PADOFFSET)PL_hints;
4440 if (o->op_flags & OPf_STACKED) {
4442 kid = cUNOPo->op_first->op_sibling;
4443 if (kid->op_type == OP_RV2GV)
4455 if (o->op_flags & OPf_KIDS) {
4456 OP *kid = cUNOPo->op_first;
4457 if (kid->op_type != OP_HELEM)
4458 croak("%s argument is not a HASH element", PL_op_desc[o->op_type]);
4466 ck_gvconst(register OP *o)
4468 o = fold_constants(o);
4469 if (o->op_type == OP_CONST)
4476 ck_rvconst(register OP *o)
4479 SVOP *kid = (SVOP*)cUNOPo->op_first;
4481 o->op_private |= (PL_hints & HINT_STRICT_REFS);
4482 if (kid->op_type == OP_CONST) {
4486 SV *kidsv = kid->op_sv;
4489 /* Is it a constant from cv_const_sv()? */
4490 if (SvROK(kidsv) && SvREADONLY(kidsv)) {
4491 SV *rsv = SvRV(kidsv);
4492 int svtype = SvTYPE(rsv);
4493 char *badtype = Nullch;
4495 switch (o->op_type) {
4497 if (svtype > SVt_PVMG)
4498 badtype = "a SCALAR";
4501 if (svtype != SVt_PVAV)
4502 badtype = "an ARRAY";
4505 if (svtype != SVt_PVHV) {
4506 if (svtype == SVt_PVAV) { /* pseudohash? */
4507 SV **ksv = av_fetch((AV*)rsv, 0, FALSE);
4508 if (ksv && SvROK(*ksv)
4509 && SvTYPE(SvRV(*ksv)) == SVt_PVHV)
4518 if (svtype != SVt_PVCV)
4523 croak("Constant is not %s reference", badtype);
4526 name = SvPV(kidsv, n_a);
4527 if ((PL_hints & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
4528 char *badthing = Nullch;
4529 switch (o->op_type) {
4531 badthing = "a SCALAR";
4534 badthing = "an ARRAY";
4537 badthing = "a HASH";
4542 "Can't use bareword (\"%s\") as %s ref while \"strict refs\" in use",
4546 * This is a little tricky. We only want to add the symbol if we
4547 * didn't add it in the lexer. Otherwise we get duplicate strict
4548 * warnings. But if we didn't add it in the lexer, we must at
4549 * least pretend like we wanted to add it even if it existed before,
4550 * or we get possible typo warnings. OPpCONST_ENTERED says
4551 * whether the lexer already added THIS instance of this symbol.
4553 iscv = (o->op_type == OP_RV2CV) * 2;
4555 gv = gv_fetchpv(name,
4556 iscv | !(kid->op_private & OPpCONST_ENTERED),
4559 : o->op_type == OP_RV2SV
4561 : o->op_type == OP_RV2AV
4563 : o->op_type == OP_RV2HV
4566 } while (!gv && !(kid->op_private & OPpCONST_ENTERED) && !iscv++);
4568 kid->op_type = OP_GV;
4569 SvREFCNT_dec(kid->op_sv);
4570 kid->op_sv = SvREFCNT_inc(gv);
4580 I32 type = o->op_type;
4582 if (o->op_flags & OPf_REF)
4585 if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
4586 SVOP *kid = (SVOP*)cUNOPo->op_first;
4588 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
4590 OP *newop = newGVOP(type, OPf_REF,
4591 gv_fetchpv(SvPVx(kid->op_sv, n_a), TRUE, SVt_PVIO));
4598 if (type == OP_FTTTY)
4599 return newGVOP(type, OPf_REF, gv_fetchpv("main::STDIN", TRUE,
4602 return newUNOP(type, 0, newDEFSVOP());
4615 int type = o->op_type;
4616 register I32 oa = PL_opargs[type] >> OASHIFT;
4618 if (o->op_flags & OPf_STACKED) {
4619 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
4622 return no_fh_allowed(o);
4625 if (o->op_flags & OPf_KIDS) {
4627 tokid = &cLISTOPo->op_first;
4628 kid = cLISTOPo->op_first;
4629 if (kid->op_type == OP_PUSHMARK ||
4630 kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK)
4632 tokid = &kid->op_sibling;
4633 kid = kid->op_sibling;
4635 if (!kid && PL_opargs[type] & OA_DEFGV)
4636 *tokid = kid = newDEFSVOP();
4640 sibl = kid->op_sibling;
4654 if (kid->op_type == OP_CONST &&
4655 (kid->op_private & OPpCONST_BARE)) {
4656 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
4657 OP *newop = newAVREF(newGVOP(OP_GV, 0,
4658 gv_fetchpv(name, TRUE, SVt_PVAV) ));
4659 if (ckWARN(WARN_SYNTAX))
4661 "Array @%s missing the @ in argument %ld of %s()",
4662 name, (long)numargs, PL_op_desc[type]);
4665 kid->op_sibling = sibl;
4668 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
4669 bad_type(numargs, "array", PL_op_desc[o->op_type], kid);
4673 if (kid->op_type == OP_CONST &&
4674 (kid->op_private & OPpCONST_BARE)) {
4675 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
4676 OP *newop = newHVREF(newGVOP(OP_GV, 0,
4677 gv_fetchpv(name, TRUE, SVt_PVHV) ));
4678 if (ckWARN(WARN_SYNTAX))
4680 "Hash %%%s missing the %% in argument %ld of %s()",
4681 name, (long)numargs, PL_op_desc[type]);
4684 kid->op_sibling = sibl;
4687 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
4688 bad_type(numargs, "hash", PL_op_desc[o->op_type], kid);
4693 OP *newop = newUNOP(OP_NULL, 0, kid);
4694 kid->op_sibling = 0;
4696 newop->op_next = newop;
4698 kid->op_sibling = sibl;
4703 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) {
4704 if (kid->op_type == OP_CONST &&
4705 (kid->op_private & OPpCONST_BARE)) {
4706 OP *newop = newGVOP(OP_GV, 0,
4707 gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, n_a), TRUE,
4712 else if (kid->op_type == OP_READLINE) {
4713 /* neophyte patrol: open(<FH>), close(<FH>) etc. */
4714 bad_type(numargs, "HANDLE", PL_op_desc[o->op_type], kid);
4717 kid->op_sibling = 0;
4718 kid = newUNOP(OP_RV2GV, 0, scalar(kid));
4720 kid->op_sibling = sibl;
4726 mod(scalar(kid), type);
4730 tokid = &kid->op_sibling;
4731 kid = kid->op_sibling;
4733 o->op_private |= numargs;
4735 return too_many_arguments(o,PL_op_desc[o->op_type]);
4738 else if (PL_opargs[type] & OA_DEFGV) {
4740 return newUNOP(type, 0, newDEFSVOP());
4744 while (oa & OA_OPTIONAL)
4746 if (oa && oa != OA_LIST)
4747 return too_few_arguments(o,PL_op_desc[o->op_type]);
4757 if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
4758 append_elem(OP_GLOB, o, newDEFSVOP());
4760 if (!((gv = gv_fetchpv("glob", FALSE, SVt_PVCV)) && GvIMPORTED_CV(gv)))
4761 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
4763 if (gv && GvIMPORTED_CV(gv)) {
4764 static int glob_index;
4766 append_elem(OP_GLOB, o,
4767 newSVOP(OP_CONST, 0, newSViv(glob_index++)));
4768 o->op_type = OP_LIST;
4769 o->op_ppaddr = PL_ppaddr[OP_LIST];
4770 cLISTOPo->op_first->op_type = OP_PUSHMARK;
4771 cLISTOPo->op_first->op_ppaddr = PL_ppaddr[OP_PUSHMARK];
4772 o = newUNOP(OP_ENTERSUB, OPf_STACKED,
4773 append_elem(OP_LIST, o,
4774 scalar(newUNOP(OP_RV2CV, 0,
4775 newGVOP(OP_GV, 0, gv)))));
4776 o = newUNOP(OP_NULL, 0, ck_subr(o));
4777 o->op_targ = OP_GLOB; /* hint at what it used to be */
4780 gv = newGVgen("main");
4782 append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
4792 OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
4794 o->op_ppaddr = PL_ppaddr[OP_GREPSTART];
4795 Newz(1101, gwop, 1, LOGOP);
4797 if (o->op_flags & OPf_STACKED) {
4800 kid = cLISTOPo->op_first->op_sibling;
4801 for (k = cLISTOPo->op_first->op_sibling->op_next; k; k = k->op_next) {
4804 kid->op_next = (OP*)gwop;
4805 o->op_flags &= ~OPf_STACKED;
4807 kid = cLISTOPo->op_first->op_sibling;
4808 if (type == OP_MAPWHILE)
4815 kid = cLISTOPo->op_first->op_sibling;
4816 if (kid->op_type != OP_NULL)
4817 croak("panic: ck_grep");
4818 kid = kUNOP->op_first;
4820 gwop->op_type = type;
4821 gwop->op_ppaddr = PL_ppaddr[type];
4822 gwop->op_first = listkids(o);
4823 gwop->op_flags |= OPf_KIDS;
4824 gwop->op_private = 1;
4825 gwop->op_other = LINKLIST(kid);
4826 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
4827 kid->op_next = (OP*)gwop;
4829 kid = cLISTOPo->op_first->op_sibling;
4830 if (!kid || !kid->op_sibling)
4831 return too_few_arguments(o,PL_op_desc[o->op_type]);
4832 for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
4833 mod(kid, OP_GREPSTART);
4841 if (o->op_flags & OPf_KIDS) {
4842 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
4844 kid = kid->op_sibling; /* get past "big" */
4845 if (kid && kid->op_type == OP_CONST)
4846 fbm_compile(((SVOP*)kid)->op_sv, 0);
4852 ck_lengthconst(OP *o)
4854 /* XXX length optimization goes here */
4861 OPCODE type = o->op_type;
4862 return modkids(ck_fun(o), type);
4868 OPCODE type = o->op_type;
4869 return refkids(ck_fun(o), type);
4877 kid = cLISTOPo->op_first;
4880 kid = cLISTOPo->op_first;
4882 if (kid->op_type == OP_PUSHMARK)
4883 kid = kid->op_sibling;
4884 if (kid && o->op_flags & OPf_STACKED)
4885 kid = kid->op_sibling;
4886 else if (kid && !kid->op_sibling) { /* print HANDLE; */
4887 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
4888 o->op_flags |= OPf_STACKED; /* make it a filehandle */
4889 kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
4890 cLISTOPo->op_first->op_sibling = kid;
4891 cLISTOPo->op_last = kid;
4892 kid = kid->op_sibling;
4897 append_elem(o->op_type, o, newDEFSVOP());
4903 if (PL_hints & HINT_LOCALE)
4904 o->op_private |= OPpLOCALE;
4911 ck_fun_locale(OP *o)
4917 if (PL_hints & HINT_LOCALE)
4918 o->op_private |= OPpLOCALE;
4929 if (PL_hints & HINT_LOCALE)
4930 o->op_private |= OPpLOCALE;
4939 o->op_private |= OPpRUNTIME;
4952 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
4953 o->op_private |= OPpREPEAT_DOLIST;
4954 cBINOPo->op_first = force_list(cBINOPo->op_first);
4964 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
4965 SVOP *kid = (SVOP*)cUNOPo->op_first;
4967 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
4969 for (s = SvPVX(kid->op_sv); *s; s++) {
4970 if (*s == ':' && s[1] == ':') {
4972 Move(s+2, s+1, strlen(s+2)+1, char);
4973 --SvCUR(kid->op_sv);
4976 sv_catpvn(kid->op_sv, ".pm", 3);
4986 croak("NOT IMPL LINE %d",__LINE__);
4996 if (o->op_flags & OPf_KIDS) {
4997 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
4998 if (kid && kid->op_sibling) {
4999 o->op_type = OP_SSELECT;
5000 o->op_ppaddr = PL_ppaddr[OP_SSELECT];
5002 return fold_constants(o);
5006 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5007 if (kid && kid->op_type == OP_RV2GV)
5008 kid->op_private &= ~HINT_STRICT_REFS;
5015 I32 type = o->op_type;
5017 if (!(o->op_flags & OPf_KIDS)) {
5022 if (!CvUNIQUE(PL_compcv)) {
5023 argop = newOP(OP_PADAV, OPf_REF);
5024 argop->op_targ = 0; /* PL_curpad[0] is @_ */
5027 argop = newUNOP(OP_RV2AV, 0,
5028 scalar(newGVOP(OP_GV, 0,
5029 gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
5032 argop = newUNOP(OP_RV2AV, 0,
5033 scalar(newGVOP(OP_GV, 0, !CvUNIQUE(PL_compcv) ?
5034 PL_defgv : gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
5035 #endif /* USE_THREADS */
5036 return newUNOP(type, 0, scalar(argop));
5038 return scalar(modkids(ck_fun(o), type));
5046 if (PL_hints & HINT_LOCALE)
5047 o->op_private |= OPpLOCALE;
5050 if (o->op_flags & OPf_STACKED) {
5051 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5053 kid = kUNOP->op_first; /* get past rv2gv */
5055 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
5057 if (kid->op_type == OP_SCOPE) {
5061 else if (kid->op_type == OP_LEAVE) {
5062 if (o->op_type == OP_SORT) {
5063 null(kid); /* wipe out leave */
5066 for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
5067 if (k->op_next == kid)
5072 kid->op_next = 0; /* just disconnect the leave */
5073 k = kLISTOP->op_first;
5077 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5078 null(kid); /* wipe out rv2gv */
5079 if (o->op_type == OP_SORT)
5083 o->op_flags |= OPf_SPECIAL;
5085 else if (kid->op_type == OP_RV2SV || kid->op_type == OP_PADSV)
5086 null(cLISTOPo->op_first->op_sibling);
5097 if (o->op_flags & OPf_STACKED)
5098 return no_fh_allowed(o);
5100 kid = cLISTOPo->op_first;
5101 if (kid->op_type != OP_NULL)
5102 croak("panic: ck_split");
5103 kid = kid->op_sibling;
5104 op_free(cLISTOPo->op_first);
5105 cLISTOPo->op_first = kid;
5107 cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpv(" ", 1));
5108 cLISTOPo->op_last = kid; /* There was only one element previously */
5111 if (kid->op_type != OP_MATCH) {
5112 OP *sibl = kid->op_sibling;
5113 kid->op_sibling = 0;
5114 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
5115 if (cLISTOPo->op_first == cLISTOPo->op_last)
5116 cLISTOPo->op_last = kid;
5117 cLISTOPo->op_first = kid;
5118 kid->op_sibling = sibl;
5121 kid->op_type = OP_PUSHRE;
5122 kid->op_ppaddr = PL_ppaddr[OP_PUSHRE];
5125 if (!kid->op_sibling)
5126 append_elem(OP_SPLIT, o, newDEFSVOP());
5128 kid = kid->op_sibling;
5131 if (!kid->op_sibling)
5132 append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
5134 kid = kid->op_sibling;
5137 if (kid->op_sibling)
5138 return too_many_arguments(o,PL_op_desc[o->op_type]);
5147 OP *prev = ((cUNOPo->op_first->op_sibling)
5148 ? cUNOPo : ((UNOP*)cUNOPo->op_first))->op_first;
5149 OP *o2 = prev->op_sibling;
5158 for (cvop = o2; cvop->op_sibling; cvop = cvop->op_sibling) ;
5159 if (cvop->op_type == OP_RV2CV) {
5161 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
5162 null(cvop); /* disable rv2cv */
5163 tmpop = (SVOP*)((UNOP*)cvop)->op_first;
5164 if (tmpop->op_type == OP_GV) {
5165 cv = GvCVu(tmpop->op_sv);
5166 if (cv && SvPOK(cv) && !(o->op_private & OPpENTERSUB_AMPER)) {
5167 namegv = CvANON(cv) ? (GV*)tmpop->op_sv : CvGV(cv);
5168 proto = SvPV((SV*)cv, n_a);
5172 o->op_private |= (PL_hints & HINT_STRICT_REFS);
5173 if (PERLDB_SUB && PL_curstash != PL_debstash)
5174 o->op_private |= OPpENTERSUB_DB;
5175 while (o2 != cvop) {
5179 return too_many_arguments(o, gv_ename(namegv));
5197 if (o2->op_type != OP_REFGEN && o2->op_type != OP_UNDEF)
5198 bad_type(arg, "block", gv_ename(namegv), o2);
5203 if (o2->op_type == OP_RV2GV)
5207 OP* sib = kid->op_sibling;
5208 kid->op_sibling = 0;
5209 o2 = newUNOP(OP_RV2GV, 0, kid);
5210 o2->op_sibling = sib;
5211 prev->op_sibling = o2;
5219 if (o2->op_type != OP_RV2GV)
5220 bad_type(arg, "symbol", gv_ename(namegv), o2);
5223 if (o2->op_type != OP_RV2CV)
5224 bad_type(arg, "sub", gv_ename(namegv), o2);
5227 if (o2->op_type != OP_RV2SV
5228 && o2->op_type != OP_PADSV
5229 && o2->op_type != OP_THREADSV)
5231 bad_type(arg, "scalar", gv_ename(namegv), o2);
5235 if (o2->op_type != OP_RV2AV && o2->op_type != OP_PADAV)
5236 bad_type(arg, "array", gv_ename(namegv), o2);
5239 if (o2->op_type != OP_RV2HV && o2->op_type != OP_PADHV)
5240 bad_type(arg, "hash", gv_ename(namegv), o2);
5244 OP* sib = kid->op_sibling;
5245 kid->op_sibling = 0;
5246 o2 = newUNOP(OP_REFGEN, 0, kid);
5247 o2->op_sibling = sib;
5248 prev->op_sibling = o2;
5259 croak("Malformed prototype for %s: %s",
5260 gv_ename(namegv), SvPV((SV*)cv, n_a));
5265 mod(o2, OP_ENTERSUB);
5267 o2 = o2->op_sibling;
5269 if (proto && !optional &&
5270 (*proto && *proto != '@' && *proto != '%' && *proto != ';'))
5271 return too_few_arguments(o, gv_ename(namegv));
5278 SvREADONLY_on(cSVOPo->op_sv);
5285 if (o->op_flags & OPf_KIDS) {
5286 SVOP *kid = (SVOP*)cUNOPo->op_first;
5288 if (kid->op_type == OP_NULL)
5289 kid = (SVOP*)kid->op_sibling;
5291 kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE))
5292 o->op_flags |= OPf_SPECIAL;
5297 /* A peephole optimizer. We visit the ops in the order they're to execute. */
5300 peep(register OP *o)
5303 register OP* oldop = 0;
5306 if (!o || o->op_seq)
5310 SAVESPTR(PL_curcop);
5311 for (; o; o = o->op_next) {
5317 switch (o->op_type) {
5320 PL_curcop = ((COP*)o); /* for warnings */
5321 o->op_seq = PL_op_seqmax++;
5332 if (o->op_next && o->op_next->op_type == OP_STRINGIFY)
5334 o->op_seq = PL_op_seqmax++;
5337 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
5338 o->op_seq = PL_op_seqmax++;
5339 break; /* Scalar stub must produce undef. List stub is noop */
5343 if (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE)
5344 PL_curcop = ((COP*)o);
5350 if (oldop && o->op_next) {
5351 oldop->op_next = o->op_next;
5354 o->op_seq = PL_op_seqmax++;
5358 if (o->op_next->op_type == OP_RV2SV) {
5359 if (!(o->op_next->op_private & OPpDEREF)) {
5361 o->op_private |= o->op_next->op_private & OPpLVAL_INTRO;
5362 o->op_next = o->op_next->op_next;
5363 o->op_type = OP_GVSV;
5364 o->op_ppaddr = PL_ppaddr[OP_GVSV];
5367 else if (o->op_next->op_type == OP_RV2AV) {
5368 OP* pop = o->op_next->op_next;
5370 if (pop->op_type == OP_CONST &&
5371 (PL_op = pop->op_next) &&
5372 pop->op_next->op_type == OP_AELEM &&
5373 !(pop->op_next->op_private &
5374 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF)) &&
5375 (i = SvIV(((SVOP*)pop)->op_sv) - PL_compiling.cop_arybase)
5379 SvREFCNT_dec(((SVOP*)pop)->op_sv);
5383 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
5384 o->op_next = pop->op_next->op_next;
5385 o->op_type = OP_AELEMFAST;
5386 o->op_ppaddr = PL_ppaddr[OP_AELEMFAST];
5387 o->op_private = (U8)i;
5388 GvAVn(((GVOP*)o)->op_gv);
5391 o->op_seq = PL_op_seqmax++;
5398 o->op_seq = PL_op_seqmax++;
5399 while (cLOGOP->op_other->op_type == OP_NULL)
5400 cLOGOP->op_other = cLOGOP->op_other->op_next;
5401 peep(cLOGOP->op_other);
5405 o->op_seq = PL_op_seqmax++;
5406 peep(cCONDOP->op_true);
5407 peep(cCONDOP->op_false);
5411 o->op_seq = PL_op_seqmax++;
5412 peep(cLOOP->op_redoop);
5413 peep(cLOOP->op_nextop);
5414 peep(cLOOP->op_lastop);
5420 o->op_seq = PL_op_seqmax++;
5421 peep(cPMOP->op_pmreplstart);
5425 o->op_seq = PL_op_seqmax++;
5426 if (ckWARN(WARN_SYNTAX) && o->op_next
5427 && o->op_next->op_type == OP_NEXTSTATE) {
5428 if (o->op_next->op_sibling &&
5429 o->op_next->op_sibling->op_type != OP_EXIT &&
5430 o->op_next->op_sibling->op_type != OP_WARN &&
5431 o->op_next->op_sibling->op_type != OP_DIE) {
5432 line_t oldline = PL_curcop->cop_line;
5434 PL_curcop->cop_line = ((COP*)o->op_next)->cop_line;
5435 warner(WARN_SYNTAX, "Statement unlikely to be reached");
5436 warner(WARN_SYNTAX, "(Maybe you meant system() when you said exec()?)\n");
5437 PL_curcop->cop_line = oldline;
5451 if (o->op_private & (OPpDEREF_HV|OPpDEREF_AV|OPpLVAL_INTRO)
5452 || ((BINOP*)o)->op_last->op_type != OP_CONST)
5454 rop = (UNOP*)((BINOP*)o)->op_first;
5455 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
5457 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
5458 if (!SvOBJECT(lexname))
5460 fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
5461 if (!fields || !GvHV(*fields))
5463 svp = &((SVOP*)((BINOP*)o)->op_last)->op_sv;
5464 key = SvPV(*svp, keylen);
5465 indsvp = hv_fetch(GvHV(*fields), key, keylen, FALSE);
5467 croak("No such field \"%s\" in variable %s of type %s",
5468 key, SvPV(lexname, n_a), HvNAME(SvSTASH(lexname)));
5470 ind = SvIV(*indsvp);
5472 croak("Bad index while coercing array into hash");
5473 rop->op_type = OP_RV2AV;
5474 rop->op_ppaddr = PL_ppaddr[OP_RV2AV];
5475 o->op_type = OP_AELEM;
5476 o->op_ppaddr = PL_ppaddr[OP_AELEM];
5478 *svp = newSViv(ind);
5483 o->op_seq = PL_op_seqmax++;