implement C<goto &func> and other fixes (via private mail)
[p5sagit/p5-mst-13.2.git] / pp_hot.c
index d7f8f52..24bb4da 100644 (file)
--- a/pp_hot.c
+++ b/pp_hot.c
 #ifdef I_UNISTD
 #include <unistd.h>
 #endif
+#ifdef I_FCNTL
+#include <fcntl.h>
+#endif
+#ifdef I_SYS_FILE
+#include <sys/file.h>
+#endif
+
+#define HOP(pos,off) (IN_UTF8 ? utf8_hop(pos, off) : (pos + off))
 
 /* Hot code. */
 
@@ -33,10 +41,10 @@ unset_cvowner(void *cvarg)
     dTHR;
 #endif /* DEBUGGING */
 
-    DEBUG_L((PerlIO_printf(PerlIO_stderr(), "%p unsetting CvOWNER of %p:%s\n",
+    DEBUG_S((PerlIO_printf(PerlIO_stderr(), "%p unsetting CvOWNER of %p:%s\n",
                           thr, cv, SvPEEK((SV*)cv))));
     MUTEX_LOCK(CvMUTEXP(cv));
-    DEBUG_L(if (CvDEPTH(cv) != 0)
+    DEBUG_S(if (CvDEPTH(cv) != 0)
                PerlIO_printf(PerlIO_stderr(), "depth %ld != 0\n",
                              CvDEPTH(cv)););
     assert(thr == CvOWNER(cv));
@@ -284,7 +292,7 @@ PP(pp_pushre)
     Copy(&PL_op, &LvTARGOFF(sv), 1, OP*);
     XPUSHs(sv);
 #else
-    XPUSHs((SV*)op);
+    XPUSHs((SV*)PL_op);
 #endif
     RETURN;
 }
@@ -303,7 +311,7 @@ PP(pp_print)
        gv = (GV*)*++MARK;
     else
        gv = PL_defoutgv;
-    if (SvRMAGICAL(gv) && (mg = mg_find((SV*)gv, 'q'))) {
+    if (mg = SvTIED_mg((SV*)gv, 'q')) {
        if (MARK == ORIGMARK) {
            /* If using default handle then we need to make space to 
             * pass object as 1st arg, so move other args up ...
@@ -314,7 +322,7 @@ PP(pp_print)
            ++SP;
        }
        PUSHMARK(MARK - 1);
-       *MARK = mg->mg_obj;
+       *MARK = SvTIED_obj((SV*)gv, mg);
        PUTBACK;
        ENTER;
        perl_call_method("PRINT", G_SCALAR);
@@ -326,23 +334,25 @@ PP(pp_print)
        RETURN;
     }
     if (!(io = GvIO(gv))) {
-       if (PL_dowarn) {
+       if (ckWARN(WARN_UNOPENED)) {
            SV* sv = sv_newmortal();
             gv_fullname3(sv, gv, Nullch);
-            warn("Filehandle %s never opened", SvPV(sv,PL_na));
+            warner(WARN_UNOPENED, "Filehandle %s never opened", SvPV(sv,PL_na));
         }
 
        SETERRNO(EBADF,RMS$_IFI);
        goto just_say_no;
     }
     else if (!(fp = IoOFP(io))) {
-       if (PL_dowarn)  {
+       if (ckWARN2(WARN_CLOSED, WARN_IO))  {
            SV* sv = sv_newmortal();
             gv_fullname3(sv, gv, Nullch);
            if (IoIFP(io))
-               warn("Filehandle %s opened only for input", SvPV(sv,PL_na));
-           else
-               warn("print on closed filehandle %s", SvPV(sv,PL_na));
+               warner(WARN_IO, "Filehandle %s opened only for input", 
+                               SvPV(sv,PL_na));
+           else if (ckWARN(WARN_CLOSED))
+               warner(WARN_CLOSED, "print on closed filehandle %s", 
+                               SvPV(sv,PL_na));
        }
        SETERRNO(EBADF,IoIFP(io)?RMS$_FAC:RMS$_IFI);
        goto just_say_no;
@@ -429,8 +439,8 @@ PP(pp_rv2av)
                    if (PL_op->op_flags & OPf_REF ||
                      PL_op->op_private & HINT_STRICT_REFS)
                        DIE(no_usym, "an ARRAY");
-                   if (PL_dowarn)
-                       warn(warn_uninit);
+                   if (ckWARN(WARN_UNINITIALIZED))
+                       warner(WARN_UNINITIALIZED, warn_uninit);
                    if (GIMME == G_ARRAY)
                        RETURN;
                    RETPUSHUNDEF;
@@ -513,8 +523,8 @@ PP(pp_rv2hv)
                    if (PL_op->op_flags & OPf_REF ||
                      PL_op->op_private & HINT_STRICT_REFS)
                        DIE(no_usym, "a HASH");
-                   if (PL_dowarn)
-                       warn(warn_uninit);
+                   if (ckWARN(WARN_UNINITIALIZED))
+                       warner(WARN_UNINITIALIZED, warn_uninit);
                    if (GIMME == G_ARRAY) {
                        SP--;
                        RETURN;
@@ -583,6 +593,7 @@ PP(pp_aassign)
      * clobber a value on the right that's used later in the list.
      */
     if (PL_op->op_private & OPpASSIGN_COMMON) {
+       EXTEND_MORTAL(lastrelem - firstrelem + 1);
         for (relem = firstrelem; relem <= lastrelem; relem++) {
             /*SUPPRESS 560*/
             if (sv = *relem) {
@@ -652,14 +663,14 @@ PP(pp_aassign)
                if (relem == lastrelem) {
                    if (*relem) {
                        HE *didstore;
-                       if (PL_dowarn) {
+                       if (ckWARN(WARN_UNSAFE)) {
                            if (relem == firstrelem &&
                                SvROK(*relem) &&
                                ( SvTYPE(SvRV(*relem)) == SVt_PVAV ||
                                  SvTYPE(SvRV(*relem)) == SVt_PVHV ) )
-                               warn("Reference found where even-sized list expected");
+                               warner(WARN_UNSAFE, "Reference found where even-sized list expected");
                            else
-                               warn("Odd number of elements in hash assignment");
+                               warner(WARN_UNSAFE, "Odd number of elements in hash assignment");
                        }
                        tmpstr = NEWSV(29,0);
                        didstore = hv_store_ent(hash,*relem,tmpstr,0);
@@ -853,9 +864,9 @@ PP(pp_match)
            }
        }
     }
-    safebase = (((gimme == G_ARRAY) || global || !rx->nparens)
-               && !PL_sawampersand);
-    safebase = safebase ? 0  : REXEC_COPY_STR ;
+    safebase = ((gimme != G_ARRAY && !global && rx->nparens)
+               || SvTEMP(TARG) || PL_sawampersand)
+               ? REXEC_COPY_STR : 0;
     if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
        SAVEINT(PL_multiline);
        PL_multiline = pm->op_pmflags & PMf_MULTILINE;
@@ -873,17 +884,20 @@ play_it_again:
        if (!(rx->reganch & ROPT_NOSCAN)) { /* Floating checkstring. */
            if ( screamer ) {
                I32 p = -1;
+               char *b;
                
                if (PL_screamfirst[BmRARE(rx->check_substr)] < 0)
                    goto nope;
-               else if (!(s = screaminstr(TARG, rx->check_substr, 
-                                          rx->check_offset_min, 0, &p, 0)))
+
+               b = (char*)HOP((U8*)s, rx->check_offset_min);
+               if (!(s = screaminstr(TARG, rx->check_substr, b - s, 0, &p, 0)))
                    goto nope;
-               else if ((rx->reganch & ROPT_CHECK_ALL)
+
+               if ((rx->reganch & ROPT_CHECK_ALL)
                         && !PL_sawampersand && !SvTAIL(rx->check_substr))
                    goto yup;
            }
-           else if (!(s = fbm_instr((unsigned char*)s + rx->check_offset_min,
+           else if (!(s = fbm_instr((unsigned char*)HOP((U8*)s, rx->check_offset_min),
                                     (unsigned char*)strend, 
                                     rx->check_substr, 0)))
                goto nope;
@@ -891,7 +905,7 @@ play_it_again:
                goto yup;
            if (s && rx->check_offset_max < s - t) {
                ++BmUSEFUL(rx->check_substr);
-               s -= rx->check_offset_max;
+               s = (char*)HOP((U8*)s, -rx->check_offset_max);
            }
            else
                s = t;
@@ -900,13 +914,13 @@ play_it_again:
           beginning of match, and the match is anchored at s. */
        else if (!PL_multiline) {       /* Anchored near beginning of string. */
            I32 slen;
-           if (*SvPVX(rx->check_substr) != s[rx->check_offset_min]
+           char *b = (char*)HOP((U8*)s, rx->check_offset_min);
+           if (*SvPVX(rx->check_substr) != *b
                || ((slen = SvCUR(rx->check_substr)) > 1
-                   && memNE(SvPVX(rx->check_substr), 
-                            s + rx->check_offset_min, slen)))
+                   && memNE(SvPVX(rx->check_substr), b, slen)))
                goto nope;
        }
-       if (!rx->naughty && --BmUSEFUL(rx->check_substr) < 0
+       if (!(rx->reganch & ROPT_NAUGHTY) && --BmUSEFUL(rx->check_substr) < 0
            && rx->check_substr == rx->float_substr) {
            SvREFCNT_dec(rx->check_substr);
            rx->check_substr = Nullsv;  /* opt is being useless */
@@ -1042,9 +1056,9 @@ do_readline(void)
     I32 gimme = GIMME_V;
     MAGIC *mg;
 
-    if (SvRMAGICAL(PL_last_in_gv) && (mg = mg_find((SV*)PL_last_in_gv, 'q'))) {
+    if (mg = SvTIED_mg((SV*)PL_last_in_gv, 'q')) {
        PUSHMARK(SP);
-       XPUSHs(mg->mg_obj);
+       XPUSHs(SvTIED_obj((SV*)PL_last_in_gv, mg));
        PUTBACK;
        ENTER;
        perl_call_method("READLINE", gimme);
@@ -1063,7 +1077,7 @@ do_readline(void)
                    IoFLAGS(io) &= ~IOf_START;
                    IoLINES(io) = 0;
                    if (av_len(GvAVn(PL_last_in_gv)) < 0) {
-                       do_open(PL_last_in_gv,"-",1,FALSE,0,0,Nullfp);
+                       do_open(PL_last_in_gv,"-",1,FALSE,O_RDONLY,0,Nullfp);
                        sv_setpvn(GvSV(PL_last_in_gv), "-", 1);
                        SvSETMAGIC(GvSV(PL_last_in_gv));
                        fp = IoIFP(io);
@@ -1197,7 +1211,7 @@ do_readline(void)
 #endif /* !CSH */
 #endif /* !DOSISH */
                (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
-                             FALSE, 0, 0, Nullfp);
+                             FALSE, O_RDONLY, 0, Nullfp);
                fp = IoIFP(io);
 #endif /* !VMS */
                LEAVE;
@@ -1207,8 +1221,9 @@ do_readline(void)
            SP--;
     }
     if (!fp) {
-       if (PL_dowarn && io && !(IoFLAGS(io) & IOf_START))
-           warn("Read on closed filehandle <%s>", GvENAME(PL_last_in_gv));
+       if (ckWARN(WARN_CLOSED) && io && !(IoFLAGS(io) & IOf_START))
+           warner(WARN_CLOSED,
+                  "Read on closed filehandle <%s>", GvENAME(PL_last_in_gv));
        if (gimme == G_SCALAR) {
            (void)SvOK_off(TARG);
            PUSHTARG;
@@ -1244,8 +1259,12 @@ do_readline(void)
                IoFLAGS(io) |= IOf_START;
            }
            else if (type == OP_GLOB) {
-               if (do_close(PL_last_in_gv, FALSE) & ~0xFF)
-                   warn("internal error: glob failed");
+               if (!do_close(PL_last_in_gv, FALSE) && ckWARN(WARN_CLOSED)) {
+                   warner(WARN_CLOSED,
+                          "glob failed (child exited with status %d%s)",
+                          STATUS_CURRENT >> 8,
+                          (STATUS_CURRENT & 0x80) ? ", core dumped" : "");
+               }
            }
            if (gimme == G_SCALAR) {
                (void)SvOK_off(TARG);
@@ -1337,6 +1356,8 @@ PP(pp_helem)
        svp = he ? &HeVAL(he) : 0;
     }
     else if (SvTYPE(hv) == SVt_PVAV) {
+       if (PL_op->op_private & OPpLVAL_INTRO)
+           DIE("Can't localize pseudo-hash element");
        svp = avhv_fetch_ent((AV*)hv, keysv, lval && !defer, 0);
     }
     else {
@@ -1445,7 +1466,7 @@ PP(pp_iter)
 
     EXTEND(SP, 1);
     cx = &cxstack[cxstack_ix];
-    if (cx->cx_type != CXt_LOOP)
+    if (CxTYPE(cx) != CXt_LOOP)
        DIE("panic: pp_iter");
 
     av = cx->blk_loop.iterary;
@@ -1458,7 +1479,9 @@ PP(pp_iter)
            char *max = SvPV((SV*)av, maxlen);
            if (!SvNIOK(cur) && SvCUR(cur) <= maxlen) {
 #ifndef USE_THREADS                      /* don't risk potential race */
-               if (SvREFCNT(*cx->blk_loop.itervar) == 1) {
+               if (SvREFCNT(*cx->blk_loop.itervar) == 1
+                   && !SvMAGICAL(*cx->blk_loop.itervar))
+               {
                    /* safe to reuse old SV */
                    sv_setsv(*cx->blk_loop.itervar, cur);
                }
@@ -1484,7 +1507,9 @@ PP(pp_iter)
            RETPUSHNO;
 
 #ifndef USE_THREADS                      /* don't risk potential race */
-       if (SvREFCNT(*cx->blk_loop.itervar) == 1) {
+       if (SvREFCNT(*cx->blk_loop.itervar) == 1
+           && !SvMAGICAL(*cx->blk_loop.itervar))
+       {
            /* safe to reuse old SV */
            sv_setiv(*cx->blk_loop.itervar, cx->blk_loop.iterix++);
        }
@@ -1602,7 +1627,8 @@ PP(pp_subst)
                  && SvTYPE(rx->check_substr) == SVt_PVBM
                  && SvVALID(rx->check_substr)) 
                ? TARG : Nullsv);
-    safebase = (!rx->nparens && !PL_sawampersand) ? 0 : REXEC_COPY_STR;
+    safebase = (rx->nparens || SvTEMP(TARG) || PL_sawampersand)
+               ? REXEC_COPY_STR : 0;
     if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
        SAVEINT(PL_multiline);
        PL_multiline = pm->op_pmflags & PMf_MULTILINE;
@@ -1612,19 +1638,22 @@ PP(pp_subst)
        if (!(rx->reganch & ROPT_NOSCAN)) { /* It floats. */
            if (screamer) {
                I32 p = -1;
+               char *b;
                
                if (PL_screamfirst[BmRARE(rx->check_substr)] < 0)
                    goto nope;
-               else if (!(s = screaminstr(TARG, rx->check_substr, rx->check_offset_min, 0, &p, 0)))
+
+               b = (char*)HOP((U8*)s, rx->check_offset_min);
+               if (!(s = screaminstr(TARG, rx->check_substr, b - s, 0, &p, 0)))
                    goto nope;
            }
-           else if (!(s = fbm_instr((unsigned char*)s + rx->check_offset_min, 
+           else if (!(s = fbm_instr((unsigned char*)HOP((U8*)s, rx->check_offset_min), 
                                     (unsigned char*)strend,
                                     rx->check_substr, 0)))
                goto nope;
            if (s && rx->check_offset_max < s - m) {
                ++BmUSEFUL(rx->check_substr);
-               s -= rx->check_offset_max;
+               s = (char*)HOP((U8*)s, -rx->check_offset_max);
            }
            else
                s = m;
@@ -1633,13 +1662,13 @@ PP(pp_subst)
           beginning of match, and the match is anchored at s. */
        else if (!PL_multiline) { /* Anchored at beginning of string. */
            I32 slen;
-           if (*SvPVX(rx->check_substr) != s[rx->check_offset_min]
+           char *b = (char*)HOP((U8*)s, rx->check_offset_min);
+           if (*SvPVX(rx->check_substr) != *b
                || ((slen = SvCUR(rx->check_substr)) > 1
-                   && memNE(SvPVX(rx->check_substr), 
-                            s + rx->check_offset_min, slen)))
+                   && memNE(SvPVX(rx->check_substr), b, slen)))
                goto nope;
        }
-       if (!rx->naughty && --BmUSEFUL(rx->check_substr) < 0
+       if (!(rx->reganch & ROPT_NAUGHTY) && --BmUSEFUL(rx->check_substr) < 0
            && rx->check_substr == rx->float_substr) {
            SvREFCNT_dec(rx->check_substr);
            rx->check_substr = Nullsv;  /* opt is being useless */
@@ -2079,10 +2108,9 @@ PP(pp_entersub)
            while (MgOWNER(mg))
                COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
            MgOWNER(mg) = thr;
-           DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: pp_entersub lock %p\n",
+           DEBUG_S(PerlIO_printf(PerlIO_stderr(), "%p: pp_entersub lock %p\n",
                                  thr, sv);)
            MUTEX_UNLOCK(MgMUTEXP(mg));
-           SvREFCNT_inc(sv);   /* Keep alive until magic_mutexfree */
            save_destructor(unlock_condpair, sv);
        }
        MUTEX_LOCK(CvMUTEXP(cv));
@@ -2123,7 +2151,7 @@ PP(pp_entersub)
            /* We already have a clone to use */
            MUTEX_UNLOCK(CvMUTEXP(cv));
            cv = *(CV**)svp;
-           DEBUG_L(PerlIO_printf(PerlIO_stderr(),
+           DEBUG_S(PerlIO_printf(PerlIO_stderr(),
                                  "entersub: %p already has clone %p:%s\n",
                                  thr, cv, SvPEEK((SV*)cv)));
            CvOWNER(cv) = thr;
@@ -2137,7 +2165,7 @@ PP(pp_entersub)
                CvOWNER(cv) = thr;
                SvREFCNT_inc(cv);
                MUTEX_UNLOCK(CvMUTEXP(cv));
-               DEBUG_L(PerlIO_printf(PerlIO_stderr(),
+               DEBUG_S(PerlIO_printf(PerlIO_stderr(),
                            "entersub: %p grabbing %p:%s in stash %s\n",
                            thr, cv, SvPEEK((SV*)cv), CvSTASH(cv) ?
                                HvNAME(CvSTASH(cv)) : "(none)"));
@@ -2146,7 +2174,7 @@ PP(pp_entersub)
                CV *clonecv;
                SvREFCNT_inc(cv); /* don't let it vanish from under us */
                MUTEX_UNLOCK(CvMUTEXP(cv));
-               DEBUG_L((PerlIO_printf(PerlIO_stderr(),
+               DEBUG_S((PerlIO_printf(PerlIO_stderr(),
                                       "entersub: %p cloning %p:%s\n",
                                       thr, cv, SvPEEK((SV*)cv))));
                /*
@@ -2163,7 +2191,7 @@ PP(pp_entersub)
                cv = clonecv;
                SvREFCNT_inc(cv);
            }
-           DEBUG_L(if (CvDEPTH(cv) != 0)
+           DEBUG_S(if (CvDEPTH(cv) != 0)
                        PerlIO_printf(PerlIO_stderr(), "depth %ld != 0\n",
                                      CvDEPTH(cv)););
            SAVEDESTRUCTOR(unset_cvowner, (void*) cv);
@@ -2245,12 +2273,14 @@ PP(pp_entersub)
        PUSHBLOCK(cx, CXt_SUB, MARK);
        PUSHSUB(cx);
        CvDEPTH(cv)++;
+       /* XXX This would be a natural place to set C<PL_compcv = cv> so
+        * that eval'' ops within this sub know the correct lexical space.
+        * Owing the speed considerations, we choose to search for the cv
+        * in doeval() instead.
+        */
        if (CvDEPTH(cv) < 2)
            (void)SvREFCNT_inc(cv);
        else {  /* save temporaries on recursion? */
-           if (CvDEPTH(cv) == 100 && PL_dowarn 
-                 && !(PERLDB_SUB && cv == GvCV(PL_DBsub)))
-               sub_crush_depth(cv);
            if (CvDEPTH(cv) > AvFILLp(padlist)) {
                AV *av;
                AV *newpad = newAV();
@@ -2313,7 +2343,7 @@ PP(pp_entersub)
            SV** ary;
 
 #if 0
-           DEBUG_L(PerlIO_printf(PerlIO_stderr(),
+           DEBUG_S(PerlIO_printf(PerlIO_stderr(),
                                  "%p entersub preparing @_\n", thr));
 #endif
            av = (AV*)PL_curpad[0];
@@ -2350,8 +2380,15 @@ PP(pp_entersub)
                MARK++;
            }
        }
+       /* warning must come *after* we fully set up the context
+        * stuff so that __WARN__ handlers can safely dounwind()
+        * if they want to
+        */
+       if (CvDEPTH(cv) == 100 && ckWARN(WARN_RECURSION)
+           && !(PERLDB_SUB && cv == GvCV(PL_DBsub)))
+           sub_crush_depth(cv);
 #if 0
-       DEBUG_L(PerlIO_printf(PerlIO_stderr(),
+       DEBUG_S(PerlIO_printf(PerlIO_stderr(),
                              "%p entersub returning %p\n", thr, CvSTART(cv)));
 #endif
        RETURNOP(CvSTART(cv));
@@ -2362,11 +2399,12 @@ void
 sub_crush_depth(CV *cv)
 {
     if (CvANON(cv))
-       warn("Deep recursion on anonymous subroutine");
+       warner(WARN_RECURSION, "Deep recursion on anonymous subroutine");
     else {
        SV* tmpstr = sv_newmortal();
        gv_efullname3(tmpstr, CvGV(cv), Nullch);
-       warn("Deep recursion on subroutine \"%s\"", SvPVX(tmpstr));
+       warner(WARN_RECURSION, "Deep recursion on subroutine \"%s\"", 
+               SvPVX(tmpstr));
     }
 }
 
@@ -2478,10 +2516,16 @@ PP(pp_method)
            !(iogv = gv_fetchpv(packname, FALSE, SVt_PVIO)) ||
            !(ob=(SV*)GvIO(iogv)))
        {
-           if (!packname || !isIDFIRST(*packname))
+           if (!packname || 
+               ((*(U8*)packname >= 0xc0 && IN_UTF8)
+                   ? !isIDFIRST_utf8(packname)
+                   : !isIDFIRST(*packname)
+               ))
+           {
                DIE("Can't call method \"%s\" %s", name,
                    SvOK(sv)? "without a package or object reference"
                            : "on an undefined value");
+           }
            stash = gv_stashpvn(packname, packlen, TRUE);
            goto fetch;
        }