Abstract all the accesses to cop_arybase (apart from ByteLoader)
[p5sagit/p5-mst-13.2.git] / ext / B / B.xs
index ed1af11..d1a3d7a 100644 (file)
@@ -19,7 +19,7 @@ typedef FILE * InputStream;
 #endif
 
 
-static char *svclassnames[] = {
+static const char* const svclassnames[] = {
     "B::NULL",
     "B::IV",
     "B::NV",
@@ -29,11 +29,16 @@ static char *svclassnames[] = {
     "B::PVNV",
     "B::PVMG",
     "B::BM",
+#if PERL_VERSION >= 9
     "B::GV",
+#endif
     "B::PVLV",
     "B::AV",
     "B::HV",
     "B::CV",
+#if PERL_VERSION <= 8
+    "B::GV",
+#endif
     "B::FM",
     "B::IO",
 };
@@ -53,7 +58,7 @@ typedef enum {
     OPc_COP    /* 11 */
 } opclass;
 
-static char *opclassnames[] = {
+static const char* const opclassnames[] = {
     "B::NULL",
     "B::OP",
     "B::UNOP",
@@ -68,7 +73,7 @@ static char *opclassnames[] = {
     "B::COP"   
 };
 
-static size_t opsizes[] = {
+static const size_t opsizes[] = {
     0, 
     sizeof(OP),
     sizeof(UNOP),
@@ -96,7 +101,7 @@ START_MY_CXT
 #define specialsv_list         (MY_CXT.x_specialsv_list)
 
 static opclass
-cc_opclass(pTHX_ OP *o)
+cc_opclass(pTHX_ const OP *o)
 {
     if (!o)
        return OPc_NULL;
@@ -107,9 +112,20 @@ cc_opclass(pTHX_ OP *o)
     if (o->op_type == OP_SASSIGN)
        return ((o->op_private & OPpASSIGN_BACKWARDS) ? OPc_UNOP : OPc_BINOP);
 
+    if (o->op_type == OP_AELEMFAST) {
+       if (o->op_flags & OPf_SPECIAL)
+           return OPc_BASEOP;
+       else
+#ifdef USE_ITHREADS
+           return OPc_PADOP;
+#else
+           return OPc_SVOP;
+#endif
+    }
+    
 #ifdef USE_ITHREADS
     if (o->op_type == OP_GV || o->op_type == OP_GVSV ||
-       o->op_type == OP_AELEMFAST || o->op_type == OP_RCATLINE)
+       o->op_type == OP_RCATLINE)
        return OPc_PADOP;
 #endif
 
@@ -204,15 +220,15 @@ cc_opclass(pTHX_ OP *o)
 }
 
 static char *
-cc_opclassname(pTHX_ OP *o)
+cc_opclassname(pTHX_ const OP *o)
 {
-    return opclassnames[cc_opclass(aTHX_ o)];
+    return (char *)opclassnames[cc_opclass(aTHX_ o)];
 }
 
 static SV *
 make_sv_object(pTHX_ SV *arg, SV *sv)
 {
-    char *type = 0;
+    const char *type = 0;
     IV iv;
     dMY_CXT;
     
@@ -241,32 +257,28 @@ static SV *
 cstring(pTHX_ SV *sv, bool perlstyle)
 {
     SV *sstr = newSVpvn("", 0);
-    STRLEN len;
-    char *s;
-    char escbuff[5]; /* to fit backslash, 3 octals + trailing \0 */
 
     if (!SvOK(sv))
        sv_setpvn(sstr, "0", 1);
-    else if (perlstyle && SvUTF8(sv))
-    {
+    else if (perlstyle && SvUTF8(sv)) {
        SV *tmpsv = sv_newmortal(); /* Temporary SV to feed sv_uni_display */
-       len = SvCUR(sv);
-       s = sv_uni_display(tmpsv, sv, 8*len, UNI_DISPLAY_QQ);
-       sv_setpv(sstr,"\"");
+       const STRLEN len = SvCUR(sv);
+       const char *s = sv_uni_display(tmpsv, sv, 8*len, UNI_DISPLAY_QQ);
+       sv_setpvn(sstr,"\"",1);
        while (*s)
        {
            if (*s == '"')
-               sv_catpv(sstr, "\\\"");
+               sv_catpvn(sstr, "\\\"", 2);
            else if (*s == '$')
-               sv_catpv(sstr, "\\$");
+               sv_catpvn(sstr, "\\$", 2);
            else if (*s == '@')
-               sv_catpv(sstr, "\\@");
+               sv_catpvn(sstr, "\\@", 2);
            else if (*s == '\\')
            {
                if (strchr("nrftax\\",*(s+1)))
                    sv_catpvn(sstr, s++, 2);
                else
-                   sv_catpv(sstr, "\\\\");
+                   sv_catpvn(sstr, "\\\\", 2);
            }
            else /* should always be printable */
                sv_catpvn(sstr, s, 1);
@@ -278,7 +290,8 @@ cstring(pTHX_ SV *sv, bool perlstyle)
     else
     {
        /* XXX Optimise? */
-       s = SvPV(sv, len);
+       STRLEN len;
+       const char *s = SvPV(sv, len);
        sv_catpv(sstr, "\"");
        for (; len; len--, s++)
        {
@@ -288,8 +301,8 @@ cstring(pTHX_ SV *sv, bool perlstyle)
            else if (*s == '\\')
                sv_catpv(sstr, "\\\\");
             /* trigraphs - bleagh */
-            else if (!perlstyle && *s == '?' && len>=3 && s[1] == '?')
-            {
+            else if (!perlstyle && *s == '?' && len>=3 && s[1] == '?') {
+               char escbuff[5]; /* to fit backslash, 3 octals + trailing \0 */
                 sprintf(escbuff, "\\%03o", '?');
                 sv_catpv(sstr, escbuff);
             }
@@ -320,7 +333,8 @@ cstring(pTHX_ SV *sv, bool perlstyle)
            else
            {
                /* Don't want promotion of a signed -1 char in sprintf args */
-               unsigned char c = (unsigned char) *s;
+               char escbuff[5]; /* to fit backslash, 3 octals + trailing \0 */
+               const unsigned char c = (unsigned char) *s;
                sprintf(escbuff, "\\%03o", c);
                sv_catpv(sstr, escbuff);
            }
@@ -335,13 +349,12 @@ static SV *
 cchar(pTHX_ SV *sv)
 {
     SV *sstr = newSVpvn("'", 1);
-    STRLEN n_a;
-    char *s = SvPV(sv, n_a);
+    const char *s = SvPV_nolen(sv);
 
     if (*s == '\'')
-       sv_catpv(sstr, "\\'");
+       sv_catpvn(sstr, "\\'", 2);
     else if (*s == '\\')
-       sv_catpv(sstr, "\\\\");
+       sv_catpvn(sstr, "\\\\", 2);
 #ifdef EBCDIC
     else if (isPRINT(*s))
 #else
@@ -349,19 +362,19 @@ cchar(pTHX_ SV *sv)
 #endif /* EBCDIC */
        sv_catpvn(sstr, s, 1);
     else if (*s == '\n')
-       sv_catpv(sstr, "\\n");
+       sv_catpvn(sstr, "\\n", 2);
     else if (*s == '\r')
-       sv_catpv(sstr, "\\r");
+       sv_catpvn(sstr, "\\r", 2);
     else if (*s == '\t')
-       sv_catpv(sstr, "\\t");
+       sv_catpvn(sstr, "\\t", 2);
     else if (*s == '\a')
-       sv_catpv(sstr, "\\a");
+       sv_catpvn(sstr, "\\a", 2);
     else if (*s == '\b')
-       sv_catpv(sstr, "\\b");
+       sv_catpvn(sstr, "\\b", 2);
     else if (*s == '\f')
-       sv_catpv(sstr, "\\f");
+       sv_catpvn(sstr, "\\f", 2);
     else if (*s == '\v')
-       sv_catpv(sstr, "\\v");
+       sv_catpvn(sstr, "\\v", 2);
     else
     {
        /* no trigraph support */
@@ -371,12 +384,12 @@ cchar(pTHX_ SV *sv)
        sprintf(escbuff, "\\%03o", c);
        sv_catpv(sstr, escbuff);
     }
-    sv_catpv(sstr, "'");
+    sv_catpvn(sstr, "'", 1);
     return sstr;
 }
 
-void
-walkoptree(pTHX_ SV *opsv, char *method)
+static void
+walkoptree(pTHX_ SV *opsv, const char *method)
 {
     dSP;
     OP *o, *kid;
@@ -403,22 +416,28 @@ walkoptree(pTHX_ SV *opsv, char *method)
            walkoptree(aTHX_ opsv, method);
        }
     }
-    if (o && (cc_opclass(aTHX_ o) == OPc_PMOP)
+    if (o && (cc_opclass(aTHX_ o) == OPc_PMOP) && o->op_type != OP_PUSHRE
            && (kid = cPMOPo->op_pmreplroot))
     {
-       sv_setiv(newSVrv(opsv, opclassnames[OPc_PMOP]), PTR2IV(kid));
+       sv_setiv(newSVrv(opsv, cc_opclassname(aTHX_ kid)), PTR2IV(kid));
        walkoptree(aTHX_ opsv, method);
     }
 }
 
-SV **
+static SV **
 oplist(pTHX_ OP *o, SV **SP)
 {
     for(; o; o = o->op_next) {
        SV *opsv;
-       if (o->op_opt == 0) 
+#if PERL_VERSION >= 9
+       if (o->op_opt == 0)
            break;
        o->op_opt = 0;
+#else
+       if (o->op_seq == 0)
+           break;
+       o->op_seq = 0;
+#endif
        opsv = sv_newmortal();
        sv_setiv(newSVrv(opsv, cc_opclassname(aTHX_ (OP*)o)), PTR2IV(o));
        XPUSHs(opsv);
@@ -494,6 +513,9 @@ BOOT:
     specialsv_list[4] = pWARN_ALL;
     specialsv_list[5] = pWARN_NONE;
     specialsv_list[6] = pWARN_STD;
+#if PERL_VERSION <= 8
+#  define CVf_ASSERTION        0
+#endif
 #include "defsubs.h"
 }
 
@@ -506,6 +528,7 @@ BOOT:
 #define B_main_root()  PL_main_root
 #define B_main_start() PL_main_start
 #define B_amagic_generation()  PL_amagic_generation
+#define B_sub_generation()     PL_sub_generation
 #define B_defstash()   PL_defstash
 #define B_curstash()   PL_curstash
 #define B_dowarn()     PL_dowarn
@@ -552,6 +575,9 @@ B_main_start()
 long 
 B_amagic_generation()
 
+long
+B_sub_generation()
+
 B::AV
 B_comppadlist()
 
@@ -591,7 +617,7 @@ MODULE = B  PACKAGE = B
 void
 walkoptree(opsv, method)
        SV *    opsv
-       char *  method
+       const char *    method
     CODE:
        walkoptree(aTHX_ opsv, method);
 
@@ -623,7 +649,7 @@ svref_2object(sv)
 
 void
 opnumber(name)
-char * name
+const char *   name
 CODE:
 {
  int i; 
@@ -656,11 +682,10 @@ void
 hash(sv)
        SV *    sv
     CODE:
-       char *s;
        STRLEN len;
        U32 hash = 0;
        char hexhash[19]; /* must fit "0xffffffffffffffff" plus trailing \0 */
-       s = SvPV(sv, len);
+       const char *s = SvPV(sv, len);
        PERL_HASH(hash, s, len);
        sprintf(hexhash, "0x%"UVxf, (UV)hash);
        ST(0) = sv_2mortal(newSVpv(hexhash, 0));
@@ -707,15 +732,28 @@ cchar(sv)
 void
 threadsv_names()
     PPCODE:
+#if PERL_VERSION <= 8
+# ifdef USE_5005THREADS
+       int i;
+       const STRLEN len = strlen(PL_threadsv_names);
 
+       EXTEND(sp, len);
+       for (i = 0; i < len; i++)
+           PUSHs(sv_2mortal(newSVpvn(&PL_threadsv_names[i], 1)));
+# endif
+#endif
 
 #define OP_next(o)     o->op_next
 #define OP_sibling(o)  o->op_sibling
-#define OP_desc(o)     PL_op_desc[o->op_type]
+#define OP_desc(o)     (char *)PL_op_desc[o->op_type]
 #define OP_targ(o)     o->op_targ
 #define OP_type(o)     o->op_type
-#define OP_opt(o)      o->op_opt
-#define OP_static(o)   o->op_static
+#if PERL_VERSION >= 9
+#  define OP_opt(o)    o->op_opt
+#  define OP_static(o) o->op_static
+#else
+#  define OP_seq(o)    o->op_seq
+#endif
 #define OP_flags(o)    o->op_flags
 #define OP_private(o)  o->op_private
 #define OP_spare(o)    o->op_spare
@@ -742,7 +780,7 @@ char *
 OP_name(o)
        B::OP           o
     CODE:
-       RETVAL = PL_op_name[o->op_type];
+       RETVAL = (char *)PL_op_name[o->op_type];
     OUTPUT:
        RETVAL
 
@@ -773,6 +811,8 @@ U16
 OP_type(o)
        B::OP           o
 
+#if PERL_VERSION >= 9
+
 U8
 OP_opt(o)
        B::OP           o
@@ -781,6 +821,14 @@ U8
 OP_static(o)
        B::OP           o
 
+#else
+
+U16
+OP_seq(o)
+       B::OP           o
+
+#endif
+
 U8
 OP_flags(o)
        B::OP           o
@@ -789,10 +837,14 @@ U8
 OP_private(o)
        B::OP           o
 
+#if PERL_VERSION >= 9
+
 U8
 OP_spare(o)
        B::OP           o
 
+#endif
+
 void
 OP_oplist(o)
        B::OP           o
@@ -970,8 +1022,8 @@ PVOP_pv(o)
                (o->op_private & OPpTRANS_COMPLEMENT) &&
                !(o->op_private & OPpTRANS_DELETE))
        {
-           short* tbl = (short*)o->op_pv;
-           short entries = 257 + tbl[256];
+           const short* const tbl = (short*)o->op_pv;
+           const short entries = 257 + tbl[256];
            ST(0) = sv_2mortal(newSVpv(o->op_pv, entries * sizeof(short)));
        }
        else if (o->op_type == OP_TRANS) {
@@ -1005,7 +1057,7 @@ LOOP_lastop(o)
 #define COP_file(o)    CopFILE(o)
 #define COP_filegv(o)  CopFILEGV(o)
 #define COP_cop_seq(o) o->cop_seq
-#define COP_arybase(o) o->cop_arybase
+#define COP_arybase(o) CopARYBASE_get(o)
 #define COP_line(o)    CopLINE(o)
 #define COP_warnings(o)        o->cop_warnings
 #define COP_io(o)      o->cop_io
@@ -1117,7 +1169,7 @@ packiv(sv)
     CODE:
        if (sizeof(IV) == 8) {
            U32 wp[2];
-           IV iv = SvIVX(sv);
+           const IV iv = SvIVX(sv);
            /*
             * The following way of spelling 32 is to stop compilers on
             * 32-bit architectures from moaning about the shift count
@@ -1177,8 +1229,16 @@ SvPV(sv)
        B::PV   sv
     CODE:
         ST(0) = sv_newmortal();
-        if( SvPOK(sv) ) { 
-            sv_setpvn(ST(0), SvPVX(sv), SvCUR(sv));
+        if( SvPOK(sv) ) {
+           /* FIXME - we need a better way for B to identify PVs that are
+              in the pads as variable names.  */
+           if((SvLEN(sv) && SvCUR(sv) >= SvLEN(sv))) {
+               /* It claims to be longer than the space allocated for it -
+                  presuambly it's a variable name in the pad  */
+               sv_setpv(ST(0), SvPV_nolen_const(sv));
+           } else {
+               sv_setpvn(ST(0), SvPVX_const(sv), SvCUR(sv));
+           }
             SvFLAGS(ST(0)) |= SvUTF8(sv);
         }
         else {
@@ -1192,7 +1252,7 @@ SvPVBM(sv)
        B::PV   sv
     CODE:
         ST(0) = sv_newmortal();
-       sv_setpvn(ST(0), SvPVX(sv),
+       sv_setpvn(ST(0), SvPVX_const(sv),
            SvCUR(sv) + (SvTYPE(sv) == SVt_PVBM ? 257 : 0));
 
 
@@ -1263,7 +1323,7 @@ IV
 MgREGEX(mg)
        B::MAGIC        mg
     CODE:
-        if( mg->mg_type == 'r' ) {
+        if(mg->mg_type == PERL_MAGIC_qr) {
             RETVAL = MgREGEX(mg);
         }
         else {
@@ -1276,8 +1336,9 @@ SV*
 precomp(mg)
         B::MAGIC        mg
     CODE:
-        if (mg->mg_type == 'r') {
+        if (mg->mg_type == PERL_MAGIC_qr) {
             REGEXP* rx = (REGEXP*)mg->mg_obj;
+            RETVAL = Nullsv;
             if( rx )
                 RETVAL = newSVpvn( rx->precomp, rx->prelen );
         }
@@ -1478,7 +1539,7 @@ IoSUBPROCESS(io)
 bool
 IsSTD(io,name)
        B::IO   io
-       char*   name
+       const char*     name
     PREINIT:
        PerlIO* handle = 0;
     CODE:
@@ -1518,12 +1579,17 @@ SSize_t
 AvMAX(av)
        B::AV   av
 
+#if PERL_VERSION < 9
+                          
+
 #define AvOFF(av) ((XPVAV*)SvANY(av))->xof_off
 
 IV
 AvOFF(av)
        B::AV   av
 
+#endif
+
 void
 AvARRAY(av)
        B::AV   av
@@ -1545,6 +1611,7 @@ AvARRAYelt(av, idx)
        else
            XPUSHs(make_sv_object(aTHX_ sv_newmortal(), NULL));
 
+#if PERL_VERSION < 9
                                   
 MODULE = B     PACKAGE = B::AV
 
@@ -1552,6 +1619,8 @@ U8
 AvFLAGS(av)
        B::AV   av
 
+#endif
+
 MODULE = B     PACKAGE = B::FM         PREFIX = Fm
 
 IV
@@ -1571,10 +1640,18 @@ CvSTASH(cv)
 B::OP
 CvSTART(cv)
        B::CV   cv
+    CODE:
+       RETVAL = CvISXSUB(cv) ? NULL : CvSTART(cv);
+    OUTPUT:
+       RETVAL
 
 B::OP
 CvROOT(cv)
        B::CV   cv
+    CODE:
+       RETVAL = CvISXSUB(cv) ? NULL : CvROOT(cv);
+    OUTPUT:
+       RETVAL
 
 B::GV
 CvGV(cv)
@@ -1604,7 +1681,7 @@ void
 CvXSUB(cv)
        B::CV   cv
     CODE:
-       ST(0) = sv_2mortal(newSViv(PTR2IV(CvXSUB(cv))));
+       ST(0) = sv_2mortal(newSViv(CvISXSUB(cv) ? PTR2IV(CvXSUB(cv)) : 0));
 
 
 void
@@ -1612,8 +1689,8 @@ CvXSUBANY(cv)
        B::CV   cv
     CODE:
        ST(0) = CvCONST(cv) ?
-           make_sv_object(aTHX_ sv_newmortal(),CvXSUBANY(cv).any_ptr) :
-           sv_2mortal(newSViv(CvXSUBANY(cv).any_iv));
+           make_sv_object(aTHX_ sv_newmortal(),(SV *)CvXSUBANY(cv).any_ptr) :
+           sv_2mortal(newSViv(CvISXSUB(cv) ? CvXSUBANY(cv).any_iv : 0));
 
 MODULE = B    PACKAGE = B::CV
 
@@ -1650,10 +1727,14 @@ char *
 HvNAME(hv)
        B::HV   hv
 
+#if PERL_VERSION < 9
+
 B::PMOP
 HvPMROOT(hv)
        B::HV   hv
 
+#endif
+
 void
 HvARRAY(hv)
        B::HV   hv