3 * Copyright (c) 1996 Malcolm Beattie
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.
10 #define PERL_NO_GET_CONTEXT
16 typedef PerlIO * InputStream;
18 typedef FILE * InputStream;
22 static char *svclassnames[] = {
57 static char *opclassnames[] = {
73 static int walkoptree_debug = 0; /* Flag for walkoptree debug hook */
75 static SV *specialsv_list[6];
78 cc_opclass(pTHX_ OP *o)
84 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
86 if (o->op_type == OP_SASSIGN)
87 return ((o->op_private & OPpASSIGN_BACKWARDS) ? OPc_UNOP : OPc_BINOP);
90 if (o->op_type == OP_GV || o->op_type == OP_GVSV || o->op_type == OP_AELEMFAST)
94 switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
119 case OA_PVOP_OR_SVOP:
121 * Character translations (tr///) are usually a PVOP, keeping a
122 * pointer to a table of shorts used to look up translations.
123 * Under utf8, however, a simple table isn't practical; instead,
124 * the OP is an SVOP, and the SV is a reference to a swash
125 * (i.e., an RV pointing to an HV).
127 return (o->op_private & (OPpTRANS_TO_UTF|OPpTRANS_FROM_UTF))
128 ? OPc_SVOP : OPc_PVOP;
136 case OA_BASEOP_OR_UNOP:
138 * UNI(OP_foo) in toke.c returns token UNI or FUNC1 depending on
139 * whether parens were seen. perly.y uses OPf_SPECIAL to
140 * signal whether a BASEOP had empty parens or none.
141 * Some other UNOPs are created later, though, so the best
142 * test is OPf_KIDS, which is set in newUNOP.
144 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
148 * The file stat OPs are created via UNI(OP_foo) in toke.c but use
149 * the OPf_REF flag to distinguish between OP types instead of the
150 * usual OPf_SPECIAL flag. As usual, if OPf_KIDS is set, then we
151 * return OPc_UNOP so that walkoptree can find our children. If
152 * OPf_KIDS is not set then we check OPf_REF. Without OPf_REF set
153 * (no argument to the operator) it's an OP; with OPf_REF set it's
154 * an SVOP (and op_sv is the GV for the filehandle argument).
156 return ((o->op_flags & OPf_KIDS) ? OPc_UNOP :
158 (o->op_flags & OPf_REF) ? OPc_PADOP : OPc_BASEOP);
160 (o->op_flags & OPf_REF) ? OPc_SVOP : OPc_BASEOP);
164 * next, last, redo, dump and goto use OPf_SPECIAL to indicate that a
165 * label was omitted (in which case it's a BASEOP) or else a term was
166 * seen. In this last case, all except goto are definitely PVOP but
167 * goto is either a PVOP (with an ordinary constant label), an UNOP
168 * with OPf_STACKED (with a non-constant non-sub) or an UNOP for
169 * OP_REFGEN (with goto &sub) in which case OPf_STACKED also seems to
172 if (o->op_flags & OPf_STACKED)
174 else if (o->op_flags & OPf_SPECIAL)
179 warn("can't determine class of operator %s, assuming BASEOP\n",
180 PL_op_name[o->op_type]);
185 cc_opclassname(pTHX_ OP *o)
187 return opclassnames[cc_opclass(aTHX_ o)];
191 make_sv_object(pTHX_ SV *arg, SV *sv)
196 for (iv = 0; iv < sizeof(specialsv_list)/sizeof(SV*); iv++) {
197 if (sv == specialsv_list[iv]) {
203 type = svclassnames[SvTYPE(sv)];
206 sv_setiv(newSVrv(arg, type), iv);
211 make_mg_object(pTHX_ SV *arg, MAGIC *mg)
213 sv_setiv(newSVrv(arg, "B::MAGIC"), PTR2IV(mg));
218 cstring(pTHX_ SV *sv)
220 SV *sstr = newSVpvn("", 0);
225 sv_setpvn(sstr, "0", 1);
230 sv_catpv(sstr, "\"");
231 for (; len; len--, s++)
233 /* At least try a little for readability */
235 sv_catpv(sstr, "\\\"");
237 sv_catpv(sstr, "\\\\");
238 else if (*s >= ' ' && *s < 127) /* XXX not portable */
239 sv_catpvn(sstr, s, 1);
241 sv_catpv(sstr, "\\n");
243 sv_catpv(sstr, "\\r");
245 sv_catpv(sstr, "\\t");
247 sv_catpv(sstr, "\\a");
249 sv_catpv(sstr, "\\b");
251 sv_catpv(sstr, "\\f");
253 sv_catpv(sstr, "\\v");
256 /* no trigraph support */
257 char escbuff[5]; /* to fit backslash, 3 octals + trailing \0 */
258 /* Don't want promotion of a signed -1 char in sprintf args */
259 unsigned char c = (unsigned char) *s;
260 sprintf(escbuff, "\\%03o", c);
261 sv_catpv(sstr, escbuff);
263 /* XXX Add line breaks if string is long */
265 sv_catpv(sstr, "\"");
273 SV *sstr = newSVpvn("'", 1);
275 char *s = SvPV(sv, n_a);
278 sv_catpv(sstr, "\\'");
280 sv_catpv(sstr, "\\\\");
281 else if (*s >= ' ' && *s < 127) /* XXX not portable */
282 sv_catpvn(sstr, s, 1);
284 sv_catpv(sstr, "\\n");
286 sv_catpv(sstr, "\\r");
288 sv_catpv(sstr, "\\t");
290 sv_catpv(sstr, "\\a");
292 sv_catpv(sstr, "\\b");
294 sv_catpv(sstr, "\\f");
296 sv_catpv(sstr, "\\v");
299 /* no trigraph support */
300 char escbuff[5]; /* to fit backslash, 3 octals + trailing \0 */
301 /* Don't want promotion of a signed -1 char in sprintf args */
302 unsigned char c = (unsigned char) *s;
303 sprintf(escbuff, "\\%03o", c);
304 sv_catpv(sstr, escbuff);
311 walkoptree(pTHX_ SV *opsv, char *method)
317 croak("opsv is not a reference");
318 opsv = sv_mortalcopy(opsv);
319 o = INT2PTR(OP*,SvIV((SV*)SvRV(opsv)));
320 if (walkoptree_debug) {
324 perl_call_method("walkoptree_debug", G_DISCARD);
329 perl_call_method(method, G_DISCARD);
330 if (o && (o->op_flags & OPf_KIDS)) {
332 for (kid = ((UNOP*)o)->op_first; kid; kid = kid->op_sibling) {
333 /* Use the same opsv. Rely on methods not to mess it up. */
334 sv_setiv(newSVrv(opsv, cc_opclassname(aTHX_ kid)), PTR2IV(kid));
335 walkoptree(aTHX_ opsv, method);
341 typedef UNOP *B__UNOP;
342 typedef BINOP *B__BINOP;
343 typedef LOGOP *B__LOGOP;
344 typedef LISTOP *B__LISTOP;
345 typedef PMOP *B__PMOP;
346 typedef SVOP *B__SVOP;
347 typedef PADOP *B__PADOP;
348 typedef PVOP *B__PVOP;
349 typedef LOOP *B__LOOP;
366 typedef MAGIC *B__MAGIC;
368 MODULE = B PACKAGE = B PREFIX = B_
374 HV *stash = gv_stashpvn("B", 1, TRUE);
375 AV *export_ok = perl_get_av("B::EXPORT_OK",TRUE);
376 specialsv_list[0] = Nullsv;
377 specialsv_list[1] = &PL_sv_undef;
378 specialsv_list[2] = &PL_sv_yes;
379 specialsv_list[3] = &PL_sv_no;
380 specialsv_list[4] = pWARN_ALL;
381 specialsv_list[5] = pWARN_NONE;
385 #define B_main_cv() PL_main_cv
386 #define B_init_av() PL_initav
387 #define B_begin_av() PL_beginav_save
388 #define B_end_av() PL_endav
389 #define B_main_root() PL_main_root
390 #define B_main_start() PL_main_start
391 #define B_amagic_generation() PL_amagic_generation
392 #define B_comppadlist() (PL_main_cv ? CvPADLIST(PL_main_cv) : CvPADLIST(PL_compcv))
393 #define B_sv_undef() &PL_sv_undef
394 #define B_sv_yes() &PL_sv_yes
395 #define B_sv_no() &PL_sv_no
416 B_amagic_generation()
430 MODULE = B PACKAGE = B
434 walkoptree(opsv, method)
438 walkoptree(aTHX_ opsv, method);
441 walkoptree_debug(...)
443 RETVAL = walkoptree_debug;
444 if (items > 0 && SvTRUE(ST(1)))
445 walkoptree_debug = 1;
449 #define address(sv) PTR2IV(sv)
460 croak("argument is not a reference");
461 RETVAL = (SV*)SvRV(sv);
472 ST(0) = sv_newmortal();
473 if (strncmp(name,"pp_",3) == 0)
475 for (i = 0; i < PL_maxo; i++)
477 if (strcmp(name, PL_op_name[i]) == 0)
483 sv_setiv(ST(0),result);
490 ST(0) = sv_newmortal();
491 if (opnum >= 0 && opnum < PL_maxo) {
492 sv_setpvn(ST(0), "pp_", 3);
493 sv_catpv(ST(0), PL_op_name[opnum]);
503 char hexhash[19]; /* must fit "0xffffffffffffffff" plus trailing \0 */
505 PERL_HASH(hash, s, len);
506 sprintf(hexhash, "0x%"UVxf, (UV)hash);
507 ST(0) = sv_2mortal(newSVpv(hexhash, 0));
509 #define cast_I32(foo) (I32)foo
528 RETVAL = cstring(aTHX_ sv);
536 RETVAL = cchar(aTHX_ sv);
543 #ifdef USE_5005THREADS
545 STRLEN len = strlen(PL_threadsv_names);
548 for (i = 0; i < len; i++)
549 PUSHs(sv_2mortal(newSVpvn(&PL_threadsv_names[i], 1)));
553 #define OP_next(o) o->op_next
554 #define OP_sibling(o) o->op_sibling
555 #define OP_desc(o) PL_op_desc[o->op_type]
556 #define OP_targ(o) o->op_targ
557 #define OP_type(o) o->op_type
558 #define OP_seq(o) o->op_seq
559 #define OP_flags(o) o->op_flags
560 #define OP_private(o) o->op_private
562 MODULE = B PACKAGE = B::OP PREFIX = OP_
576 RETVAL = PL_op_name[o->op_type];
586 SV *sv = sv_newmortal();
588 sv_setpvn(sv, "PL_ppaddr[OP_", 13);
589 sv_catpv(sv, PL_op_name[o->op_type]);
590 for (i=13; i<SvCUR(sv); ++i)
591 SvPVX(sv)[i] = toUPPER(SvPVX(sv)[i]);
619 #define UNOP_first(o) o->op_first
621 MODULE = B PACKAGE = B::UNOP PREFIX = UNOP_
627 #define BINOP_last(o) o->op_last
629 MODULE = B PACKAGE = B::BINOP PREFIX = BINOP_
635 #define LOGOP_other(o) o->op_other
637 MODULE = B PACKAGE = B::LOGOP PREFIX = LOGOP_
643 MODULE = B PACKAGE = B::LISTOP PREFIX = LISTOP_
652 for (kid = o->op_first; kid; kid = kid->op_sibling)
658 #define PMOP_pmreplroot(o) o->op_pmreplroot
659 #define PMOP_pmreplstart(o) o->op_pmreplstart
660 #define PMOP_pmnext(o) o->op_pmnext
661 #define PMOP_pmregexp(o) PM_GETRE(o)
662 #define PMOP_pmflags(o) o->op_pmflags
663 #define PMOP_pmpermflags(o) o->op_pmpermflags
665 MODULE = B PACKAGE = B::PMOP PREFIX = PMOP_
672 ST(0) = sv_newmortal();
673 root = o->op_pmreplroot;
674 /* OP_PUSHRE stores an SV* instead of an OP* in op_pmreplroot */
675 if (o->op_type == OP_PUSHRE) {
676 sv_setiv(newSVrv(ST(0), root ?
677 svclassnames[SvTYPE((SV*)root)] : "B::SV"),
681 sv_setiv(newSVrv(ST(0), cc_opclassname(aTHX_ root)), PTR2IV(root));
703 REGEXP * rx = NO_INIT
705 ST(0) = sv_newmortal();
708 sv_setpvn(ST(0), rx->precomp, rx->prelen);
710 #define SVOP_sv(o) cSVOPo->op_sv
711 #define SVOP_gv(o) ((GV*)cSVOPo->op_sv)
713 MODULE = B PACKAGE = B::SVOP PREFIX = SVOP_
723 #define PADOP_padix(o) o->op_padix
724 #define PADOP_sv(o) (o->op_padix ? PL_curpad[o->op_padix] : Nullsv)
725 #define PADOP_gv(o) ((o->op_padix \
726 && SvTYPE(PL_curpad[o->op_padix]) == SVt_PVGV) \
727 ? (GV*)PL_curpad[o->op_padix] : Nullgv)
729 MODULE = B PACKAGE = B::PADOP PREFIX = PADOP_
743 MODULE = B PACKAGE = B::PVOP PREFIX = PVOP_
750 * OP_TRANS uses op_pv to point to a table of 256 or >=258 shorts
751 * whereas other PVOPs point to a null terminated string.
753 if (o->op_type == OP_TRANS &&
754 (o->op_private & OPpTRANS_COMPLEMENT) &&
755 !(o->op_private & OPpTRANS_DELETE))
757 short* tbl = (short*)o->op_pv;
758 short entries = 257 + tbl[256];
759 ST(0) = sv_2mortal(newSVpv(o->op_pv, entries * sizeof(short)));
761 else if (o->op_type == OP_TRANS) {
762 ST(0) = sv_2mortal(newSVpv(o->op_pv, 256 * sizeof(short)));
765 ST(0) = sv_2mortal(newSVpv(o->op_pv, 0));
767 #define LOOP_redoop(o) o->op_redoop
768 #define LOOP_nextop(o) o->op_nextop
769 #define LOOP_lastop(o) o->op_lastop
771 MODULE = B PACKAGE = B::LOOP PREFIX = LOOP_
786 #define COP_label(o) o->cop_label
787 #define COP_stashpv(o) CopSTASHPV(o)
788 #define COP_stash(o) CopSTASH(o)
789 #define COP_file(o) CopFILE(o)
790 #define COP_cop_seq(o) o->cop_seq
791 #define COP_arybase(o) o->cop_arybase
792 #define COP_line(o) CopLINE(o)
793 #define COP_warnings(o) o->cop_warnings
795 MODULE = B PACKAGE = B::COP PREFIX = COP_
829 MODULE = B PACKAGE = B::SV PREFIX = Sv
839 MODULE = B PACKAGE = B::IV PREFIX = Sv
854 MODULE = B PACKAGE = B::IV
856 #define needs64bits(sv) ((I32)SvIVX(sv) != SvIVX(sv))
866 if (sizeof(IV) == 8) {
870 * The following way of spelling 32 is to stop compilers on
871 * 32-bit architectures from moaning about the shift count
872 * being >= the width of the type. Such architectures don't
873 * reach this code anyway (unless sizeof(IV) > 8 but then
874 * everything else breaks too so I'm not fussed at the moment).
877 wp[0] = htonl(((UV)iv) >> (sizeof(UV)*4));
879 wp[0] = htonl(((U32)iv) >> (sizeof(UV)*4));
881 wp[1] = htonl(iv & 0xffffffff);
882 ST(0) = sv_2mortal(newSVpvn((char *)wp, 8));
884 U32 w = htonl((U32)SvIVX(sv));
885 ST(0) = sv_2mortal(newSVpvn((char *)&w, 4));
888 MODULE = B PACKAGE = B::NV PREFIX = Sv
898 MODULE = B PACKAGE = B::RV PREFIX = Sv
904 MODULE = B PACKAGE = B::PV PREFIX = Sv
914 ST(0) = sv_newmortal();
915 sv_setpvn(ST(0), SvPVX(sv), SvCUR(sv));
916 SvFLAGS(ST(0)) |= SvUTF8(sv);
926 MODULE = B PACKAGE = B::PVMG PREFIX = Sv
933 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic)
934 XPUSHs(make_mg_object(aTHX_ sv_newmortal(), mg));
936 MODULE = B PACKAGE = B::PVMG
942 #define MgMOREMAGIC(mg) mg->mg_moremagic
943 #define MgPRIVATE(mg) mg->mg_private
944 #define MgTYPE(mg) mg->mg_type
945 #define MgFLAGS(mg) mg->mg_flags
946 #define MgOBJ(mg) mg->mg_obj
947 #define MgLENGTH(mg) mg->mg_len
949 MODULE = B PACKAGE = B::MAGIC PREFIX = Mg
979 ST(0) = sv_newmortal();
981 if (mg->mg_len >= 0){
982 sv_setpvn(ST(0), mg->mg_ptr, mg->mg_len);
984 if (mg->mg_len == HEf_SVKEY)
985 sv_setsv(ST(0),newRV((SV*)mg->mg_ptr));
989 MODULE = B PACKAGE = B::PVLV PREFIX = Lv
1007 MODULE = B PACKAGE = B::BM PREFIX = Bm
1024 STRLEN len = NO_INIT
1025 char * str = NO_INIT
1027 str = SvPV(sv, len);
1028 /* Boyer-Moore table is just after string and its safety-margin \0 */
1029 ST(0) = sv_2mortal(newSVpvn(str + len + 1, 256));
1031 MODULE = B PACKAGE = B::GV PREFIX = Gv
1037 ST(0) = sv_2mortal(newSVpvn(GvNAME(gv), GvNAMELEN(gv)));
1043 RETVAL = GvGP(gv) == Null(GP*);
1095 MODULE = B PACKAGE = B::GV
1105 MODULE = B PACKAGE = B::IO PREFIX = Io
1151 MODULE = B PACKAGE = B::IO
1161 MODULE = B PACKAGE = B::AV PREFIX = Av
1171 #define AvOFF(av) ((XPVAV*)SvANY(av))->xof_off
1181 if (AvFILL(av) >= 0) {
1182 SV **svp = AvARRAY(av);
1184 for (i = 0; i <= AvFILL(av); i++)
1185 XPUSHs(make_sv_object(aTHX_ sv_newmortal(), svp[i]));
1188 MODULE = B PACKAGE = B::AV
1194 MODULE = B PACKAGE = B::CV PREFIX = Cv
1232 ST(0) = sv_2mortal(newSViv(PTR2IV(CvXSUB(cv))));
1239 ST(0) = sv_2mortal(newSViv(CvXSUBANY(cv).any_iv));
1241 MODULE = B PACKAGE = B::CV
1247 MODULE = B PACKAGE = B::CV PREFIX = cv_
1254 MODULE = B PACKAGE = B::HV PREFIX = Hv
1284 if (HvKEYS(hv) > 0) {
1288 (void)hv_iterinit(hv);
1289 EXTEND(sp, HvKEYS(hv) * 2);
1290 while ((sv = hv_iternextsv(hv, &key, &len))) {
1291 PUSHs(newSVpvn(key, len));
1292 PUSHs(make_sv_object(aTHX_ sv_newmortal(), sv));