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 #define MY_CXT_KEY "B::_guts" XS_VERSION
76 int x_walkoptree_debug; /* Flag for walkoptree debug hook */
77 SV * x_specialsv_list[7];
82 #define walkoptree_debug (MY_CXT.x_walkoptree_debug)
83 #define specialsv_list (MY_CXT.x_specialsv_list)
86 cc_opclass(pTHX_ OP *o)
92 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
94 if (o->op_type == OP_SASSIGN)
95 return ((o->op_private & OPpASSIGN_BACKWARDS) ? OPc_UNOP : OPc_BINOP);
98 if (o->op_type == OP_GV || o->op_type == OP_GVSV || o->op_type == OP_AELEMFAST)
102 switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
127 case OA_PVOP_OR_SVOP:
129 * Character translations (tr///) are usually a PVOP, keeping a
130 * pointer to a table of shorts used to look up translations.
131 * Under utf8, however, a simple table isn't practical; instead,
132 * the OP is an SVOP, and the SV is a reference to a swash
133 * (i.e., an RV pointing to an HV).
135 return (o->op_private & (OPpTRANS_TO_UTF|OPpTRANS_FROM_UTF))
136 ? OPc_SVOP : OPc_PVOP;
144 case OA_BASEOP_OR_UNOP:
146 * UNI(OP_foo) in toke.c returns token UNI or FUNC1 depending on
147 * whether parens were seen. perly.y uses OPf_SPECIAL to
148 * signal whether a BASEOP had empty parens or none.
149 * Some other UNOPs are created later, though, so the best
150 * test is OPf_KIDS, which is set in newUNOP.
152 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
156 * The file stat OPs are created via UNI(OP_foo) in toke.c but use
157 * the OPf_REF flag to distinguish between OP types instead of the
158 * usual OPf_SPECIAL flag. As usual, if OPf_KIDS is set, then we
159 * return OPc_UNOP so that walkoptree can find our children. If
160 * OPf_KIDS is not set then we check OPf_REF. Without OPf_REF set
161 * (no argument to the operator) it's an OP; with OPf_REF set it's
162 * an SVOP (and op_sv is the GV for the filehandle argument).
164 return ((o->op_flags & OPf_KIDS) ? OPc_UNOP :
166 (o->op_flags & OPf_REF) ? OPc_PADOP : OPc_BASEOP);
168 (o->op_flags & OPf_REF) ? OPc_SVOP : OPc_BASEOP);
172 * next, last, redo, dump and goto use OPf_SPECIAL to indicate that a
173 * label was omitted (in which case it's a BASEOP) or else a term was
174 * seen. In this last case, all except goto are definitely PVOP but
175 * goto is either a PVOP (with an ordinary constant label), an UNOP
176 * with OPf_STACKED (with a non-constant non-sub) or an UNOP for
177 * OP_REFGEN (with goto &sub) in which case OPf_STACKED also seems to
180 if (o->op_flags & OPf_STACKED)
182 else if (o->op_flags & OPf_SPECIAL)
187 warn("can't determine class of operator %s, assuming BASEOP\n",
188 PL_op_name[o->op_type]);
193 cc_opclassname(pTHX_ OP *o)
195 return opclassnames[cc_opclass(aTHX_ o)];
199 make_sv_object(pTHX_ SV *arg, SV *sv)
205 for (iv = 0; iv < sizeof(specialsv_list)/sizeof(SV*); iv++) {
206 if (sv == specialsv_list[iv]) {
212 type = svclassnames[SvTYPE(sv)];
215 sv_setiv(newSVrv(arg, type), iv);
220 make_mg_object(pTHX_ SV *arg, MAGIC *mg)
222 sv_setiv(newSVrv(arg, "B::MAGIC"), PTR2IV(mg));
227 cstring(pTHX_ SV *sv)
229 SV *sstr = newSVpvn("", 0);
232 char escbuff[5]; /* to fit backslash, 3 octals + trailing \0 */
235 sv_setpvn(sstr, "0", 1);
240 sv_catpv(sstr, "\"");
241 for (; len; len--, s++)
243 /* At least try a little for readability */
245 sv_catpv(sstr, "\\\"");
247 sv_catpv(sstr, "\\\\");
248 /* trigraphs - bleagh */
249 else if (*s == '?' && len>=3 && s[1] == '?')
251 sprintf(escbuff, "\\%03o", '?');
252 sv_catpv(sstr, escbuff);
254 else if (*s >= ' ' && *s < 127) /* XXX not portable */
255 sv_catpvn(sstr, s, 1);
257 sv_catpv(sstr, "\\n");
259 sv_catpv(sstr, "\\r");
261 sv_catpv(sstr, "\\t");
263 sv_catpv(sstr, "\\a");
265 sv_catpv(sstr, "\\b");
267 sv_catpv(sstr, "\\f");
269 sv_catpv(sstr, "\\v");
272 /* Don't want promotion of a signed -1 char in sprintf args */
273 unsigned char c = (unsigned char) *s;
274 sprintf(escbuff, "\\%03o", c);
275 sv_catpv(sstr, escbuff);
277 /* XXX Add line breaks if string is long */
279 sv_catpv(sstr, "\"");
287 SV *sstr = newSVpvn("'", 1);
289 char *s = SvPV(sv, n_a);
292 sv_catpv(sstr, "\\'");
294 sv_catpv(sstr, "\\\\");
295 else if (*s >= ' ' && *s < 127) /* XXX not portable */
296 sv_catpvn(sstr, s, 1);
298 sv_catpv(sstr, "\\n");
300 sv_catpv(sstr, "\\r");
302 sv_catpv(sstr, "\\t");
304 sv_catpv(sstr, "\\a");
306 sv_catpv(sstr, "\\b");
308 sv_catpv(sstr, "\\f");
310 sv_catpv(sstr, "\\v");
313 /* no trigraph support */
314 char escbuff[5]; /* to fit backslash, 3 octals + trailing \0 */
315 /* Don't want promotion of a signed -1 char in sprintf args */
316 unsigned char c = (unsigned char) *s;
317 sprintf(escbuff, "\\%03o", c);
318 sv_catpv(sstr, escbuff);
325 walkoptree(pTHX_ SV *opsv, char *method)
332 croak("opsv is not a reference");
333 opsv = sv_mortalcopy(opsv);
334 o = INT2PTR(OP*,SvIV((SV*)SvRV(opsv)));
335 if (walkoptree_debug) {
339 perl_call_method("walkoptree_debug", G_DISCARD);
344 perl_call_method(method, G_DISCARD);
345 if (o && (o->op_flags & OPf_KIDS)) {
347 for (kid = ((UNOP*)o)->op_first; kid; kid = kid->op_sibling) {
348 /* Use the same opsv. Rely on methods not to mess it up. */
349 sv_setiv(newSVrv(opsv, cc_opclassname(aTHX_ kid)), PTR2IV(kid));
350 walkoptree(aTHX_ opsv, method);
356 typedef UNOP *B__UNOP;
357 typedef BINOP *B__BINOP;
358 typedef LOGOP *B__LOGOP;
359 typedef LISTOP *B__LISTOP;
360 typedef PMOP *B__PMOP;
361 typedef SVOP *B__SVOP;
362 typedef PADOP *B__PADOP;
363 typedef PVOP *B__PVOP;
364 typedef LOOP *B__LOOP;
381 typedef MAGIC *B__MAGIC;
383 MODULE = B PACKAGE = B PREFIX = B_
389 HV *stash = gv_stashpvn("B", 1, TRUE);
390 AV *export_ok = perl_get_av("B::EXPORT_OK",TRUE);
392 specialsv_list[0] = Nullsv;
393 specialsv_list[1] = &PL_sv_undef;
394 specialsv_list[2] = &PL_sv_yes;
395 specialsv_list[3] = &PL_sv_no;
396 specialsv_list[4] = pWARN_ALL;
397 specialsv_list[5] = pWARN_NONE;
398 specialsv_list[6] = pWARN_STD;
402 #define B_main_cv() PL_main_cv
403 #define B_init_av() PL_initav
404 #define B_begin_av() PL_beginav_save
405 #define B_end_av() PL_endav
406 #define B_main_root() PL_main_root
407 #define B_main_start() PL_main_start
408 #define B_amagic_generation() PL_amagic_generation
409 #define B_comppadlist() (PL_main_cv ? CvPADLIST(PL_main_cv) : CvPADLIST(PL_compcv))
410 #define B_sv_undef() &PL_sv_undef
411 #define B_sv_yes() &PL_sv_yes
412 #define B_sv_no() &PL_sv_no
433 B_amagic_generation()
447 MODULE = B PACKAGE = B
451 walkoptree(opsv, method)
455 walkoptree(aTHX_ opsv, method);
458 walkoptree_debug(...)
461 RETVAL = walkoptree_debug;
462 if (items > 0 && SvTRUE(ST(1)))
463 walkoptree_debug = 1;
467 #define address(sv) PTR2IV(sv)
478 croak("argument is not a reference");
479 RETVAL = (SV*)SvRV(sv);
490 ST(0) = sv_newmortal();
491 if (strncmp(name,"pp_",3) == 0)
493 for (i = 0; i < PL_maxo; i++)
495 if (strcmp(name, PL_op_name[i]) == 0)
501 sv_setiv(ST(0),result);
508 ST(0) = sv_newmortal();
509 if (opnum >= 0 && opnum < PL_maxo) {
510 sv_setpvn(ST(0), "pp_", 3);
511 sv_catpv(ST(0), PL_op_name[opnum]);
521 char hexhash[19]; /* must fit "0xffffffffffffffff" plus trailing \0 */
523 PERL_HASH(hash, s, len);
524 sprintf(hexhash, "0x%"UVxf, (UV)hash);
525 ST(0) = sv_2mortal(newSVpv(hexhash, 0));
527 #define cast_I32(foo) (I32)foo
546 RETVAL = cstring(aTHX_ sv);
554 RETVAL = cchar(aTHX_ sv);
561 #ifdef USE_5005THREADS
563 STRLEN len = strlen(PL_threadsv_names);
566 for (i = 0; i < len; i++)
567 PUSHs(sv_2mortal(newSVpvn(&PL_threadsv_names[i], 1)));
571 #define OP_next(o) o->op_next
572 #define OP_sibling(o) o->op_sibling
573 #define OP_desc(o) PL_op_desc[o->op_type]
574 #define OP_targ(o) o->op_targ
575 #define OP_type(o) o->op_type
576 #define OP_seq(o) o->op_seq
577 #define OP_flags(o) o->op_flags
578 #define OP_private(o) o->op_private
580 MODULE = B PACKAGE = B::OP PREFIX = OP_
594 RETVAL = PL_op_name[o->op_type];
604 SV *sv = sv_newmortal();
606 sv_setpvn(sv, "PL_ppaddr[OP_", 13);
607 sv_catpv(sv, PL_op_name[o->op_type]);
608 for (i=13; i<SvCUR(sv); ++i)
609 SvPVX(sv)[i] = toUPPER(SvPVX(sv)[i]);
637 #define UNOP_first(o) o->op_first
639 MODULE = B PACKAGE = B::UNOP PREFIX = UNOP_
645 #define BINOP_last(o) o->op_last
647 MODULE = B PACKAGE = B::BINOP PREFIX = BINOP_
653 #define LOGOP_other(o) o->op_other
655 MODULE = B PACKAGE = B::LOGOP PREFIX = LOGOP_
661 MODULE = B PACKAGE = B::LISTOP PREFIX = LISTOP_
670 for (kid = o->op_first; kid; kid = kid->op_sibling)
676 #define PMOP_pmreplroot(o) o->op_pmreplroot
677 #define PMOP_pmreplstart(o) o->op_pmreplstart
678 #define PMOP_pmnext(o) o->op_pmnext
679 #define PMOP_pmregexp(o) PM_GETRE(o)
680 #define PMOP_pmflags(o) o->op_pmflags
681 #define PMOP_pmpermflags(o) o->op_pmpermflags
683 MODULE = B PACKAGE = B::PMOP PREFIX = PMOP_
690 ST(0) = sv_newmortal();
691 root = o->op_pmreplroot;
692 /* OP_PUSHRE stores an SV* instead of an OP* in op_pmreplroot */
693 if (o->op_type == OP_PUSHRE) {
694 sv_setiv(newSVrv(ST(0), root ?
695 svclassnames[SvTYPE((SV*)root)] : "B::SV"),
699 sv_setiv(newSVrv(ST(0), cc_opclassname(aTHX_ root)), PTR2IV(root));
721 REGEXP * rx = NO_INIT
723 ST(0) = sv_newmortal();
726 sv_setpvn(ST(0), rx->precomp, rx->prelen);
728 #define SVOP_sv(o) cSVOPo->op_sv
729 #define SVOP_gv(o) ((GV*)cSVOPo->op_sv)
731 MODULE = B PACKAGE = B::SVOP PREFIX = SVOP_
741 #define PADOP_padix(o) o->op_padix
742 #define PADOP_sv(o) (o->op_padix ? PL_curpad[o->op_padix] : Nullsv)
743 #define PADOP_gv(o) ((o->op_padix \
744 && SvTYPE(PL_curpad[o->op_padix]) == SVt_PVGV) \
745 ? (GV*)PL_curpad[o->op_padix] : Nullgv)
747 MODULE = B PACKAGE = B::PADOP PREFIX = PADOP_
761 MODULE = B PACKAGE = B::PVOP PREFIX = PVOP_
768 * OP_TRANS uses op_pv to point to a table of 256 or >=258 shorts
769 * whereas other PVOPs point to a null terminated string.
771 if (o->op_type == OP_TRANS &&
772 (o->op_private & OPpTRANS_COMPLEMENT) &&
773 !(o->op_private & OPpTRANS_DELETE))
775 short* tbl = (short*)o->op_pv;
776 short entries = 257 + tbl[256];
777 ST(0) = sv_2mortal(newSVpv(o->op_pv, entries * sizeof(short)));
779 else if (o->op_type == OP_TRANS) {
780 ST(0) = sv_2mortal(newSVpv(o->op_pv, 256 * sizeof(short)));
783 ST(0) = sv_2mortal(newSVpv(o->op_pv, 0));
785 #define LOOP_redoop(o) o->op_redoop
786 #define LOOP_nextop(o) o->op_nextop
787 #define LOOP_lastop(o) o->op_lastop
789 MODULE = B PACKAGE = B::LOOP PREFIX = LOOP_
804 #define COP_label(o) o->cop_label
805 #define COP_stashpv(o) CopSTASHPV(o)
806 #define COP_stash(o) CopSTASH(o)
807 #define COP_file(o) CopFILE(o)
808 #define COP_cop_seq(o) o->cop_seq
809 #define COP_arybase(o) o->cop_arybase
810 #define COP_line(o) CopLINE(o)
811 #define COP_warnings(o) o->cop_warnings
813 MODULE = B PACKAGE = B::COP PREFIX = COP_
847 MODULE = B PACKAGE = B::SV PREFIX = Sv
857 MODULE = B PACKAGE = B::IV PREFIX = Sv
872 MODULE = B PACKAGE = B::IV
874 #define needs64bits(sv) ((I32)SvIVX(sv) != SvIVX(sv))
884 if (sizeof(IV) == 8) {
888 * The following way of spelling 32 is to stop compilers on
889 * 32-bit architectures from moaning about the shift count
890 * being >= the width of the type. Such architectures don't
891 * reach this code anyway (unless sizeof(IV) > 8 but then
892 * everything else breaks too so I'm not fussed at the moment).
895 wp[0] = htonl(((UV)iv) >> (sizeof(UV)*4));
897 wp[0] = htonl(((U32)iv) >> (sizeof(UV)*4));
899 wp[1] = htonl(iv & 0xffffffff);
900 ST(0) = sv_2mortal(newSVpvn((char *)wp, 8));
902 U32 w = htonl((U32)SvIVX(sv));
903 ST(0) = sv_2mortal(newSVpvn((char *)&w, 4));
906 MODULE = B PACKAGE = B::NV PREFIX = Sv
916 MODULE = B PACKAGE = B::RV PREFIX = Sv
922 MODULE = B PACKAGE = B::PV PREFIX = Sv
936 croak( "argument is not SvROK" );
945 ST(0) = sv_newmortal();
947 sv_setpvn(ST(0), SvPVX(sv), SvCUR(sv));
948 SvFLAGS(ST(0)) |= SvUTF8(sv);
951 /* XXX for backward compatibility, but should fail */
952 /* croak( "argument is not SvPOK" ); */
953 sv_setpvn(ST(0), NULL, 0);
964 MODULE = B PACKAGE = B::PVMG PREFIX = Sv
971 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic)
972 XPUSHs(make_mg_object(aTHX_ sv_newmortal(), mg));
974 MODULE = B PACKAGE = B::PVMG
980 #define MgMOREMAGIC(mg) mg->mg_moremagic
981 #define MgPRIVATE(mg) mg->mg_private
982 #define MgTYPE(mg) mg->mg_type
983 #define MgFLAGS(mg) mg->mg_flags
984 #define MgOBJ(mg) mg->mg_obj
985 #define MgLENGTH(mg) mg->mg_len
987 MODULE = B PACKAGE = B::MAGIC PREFIX = Mg
1009 if( mg->mg_type != 'r' ) {
1013 croak( "OBJ is not meaningful on r-magic" );
1022 if (mg->mg_type == 'r') {
1023 REGEXP* rx = (REGEXP*)mg->mg_obj;
1025 RETVAL = newSVpvn( rx->precomp, rx->prelen );
1028 croak( "precomp is only meaningful on r-magic" );
1041 ST(0) = sv_newmortal();
1043 if (mg->mg_len >= 0){
1044 sv_setpvn(ST(0), mg->mg_ptr, mg->mg_len);
1046 if (mg->mg_len == HEf_SVKEY)
1047 sv_setsv(ST(0),newRV((SV*)mg->mg_ptr));
1051 MODULE = B PACKAGE = B::PVLV PREFIX = Lv
1069 MODULE = B PACKAGE = B::BM PREFIX = Bm
1086 STRLEN len = NO_INIT
1087 char * str = NO_INIT
1089 str = SvPV(sv, len);
1090 /* Boyer-Moore table is just after string and its safety-margin \0 */
1091 ST(0) = sv_2mortal(newSVpvn(str + len + 1, 256));
1093 MODULE = B PACKAGE = B::GV PREFIX = Gv
1099 ST(0) = sv_2mortal(newSVpvn(GvNAME(gv), GvNAMELEN(gv)));
1105 RETVAL = GvGP(gv) == Null(GP*);
1157 MODULE = B PACKAGE = B::GV
1167 MODULE = B PACKAGE = B::IO PREFIX = Io
1220 if( strEQ( name, "stdin" ) ) {
1221 handle = PerlIO_stdin();
1223 else if( strEQ( name, "stdout" ) ) {
1224 handle = PerlIO_stdout();
1226 else if( strEQ( name, "stderr" ) ) {
1227 handle = PerlIO_stderr();
1230 croak( "Invalid value '%s'", name );
1232 RETVAL = handle == IoIFP(io);
1236 MODULE = B PACKAGE = B::IO
1246 MODULE = B PACKAGE = B::AV PREFIX = Av
1256 #define AvOFF(av) ((XPVAV*)SvANY(av))->xof_off
1266 if (AvFILL(av) >= 0) {
1267 SV **svp = AvARRAY(av);
1269 for (i = 0; i <= AvFILL(av); i++)
1270 XPUSHs(make_sv_object(aTHX_ sv_newmortal(), svp[i]));
1273 MODULE = B PACKAGE = B::AV
1279 MODULE = B PACKAGE = B::CV PREFIX = Cv
1317 ST(0) = sv_2mortal(newSViv(PTR2IV(CvXSUB(cv))));
1324 ST(0) = CvCONST(cv) ?
1325 make_sv_object(aTHX_ sv_newmortal(),CvXSUBANY(cv).any_ptr) :
1326 sv_2mortal(newSViv(CvXSUBANY(cv).any_iv));
1328 MODULE = B PACKAGE = B::CV
1334 MODULE = B PACKAGE = B::CV PREFIX = cv_
1341 MODULE = B PACKAGE = B::HV PREFIX = Hv
1371 if (HvKEYS(hv) > 0) {
1375 (void)hv_iterinit(hv);
1376 EXTEND(sp, HvKEYS(hv) * 2);
1377 while ((sv = hv_iternextsv(hv, &key, &len))) {
1378 PUSHs(newSVpvn(key, len));
1379 PUSHs(make_sv_object(aTHX_ sv_newmortal(), sv));