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, bool perlstyle)
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 (!perlstyle && *s == '?' && len>=3 && s[1] == '?')
251 sprintf(escbuff, "\\%03o", '?');
252 sv_catpv(sstr, escbuff);
254 else if (perlstyle && *s == '$')
255 sv_catpv(sstr, "\\$");
256 else if (perlstyle && *s == '@')
257 sv_catpv(sstr, "\\@");
259 else if (isPRINT(*s))
261 else if (*s >= ' ' && *s < 127)
263 sv_catpvn(sstr, s, 1);
265 sv_catpv(sstr, "\\n");
267 sv_catpv(sstr, "\\r");
269 sv_catpv(sstr, "\\t");
271 sv_catpv(sstr, "\\a");
273 sv_catpv(sstr, "\\b");
275 sv_catpv(sstr, "\\f");
276 else if (!perlstyle && *s == '\v')
277 sv_catpv(sstr, "\\v");
280 /* Don't want promotion of a signed -1 char in sprintf args */
281 unsigned char c = (unsigned char) *s;
282 sprintf(escbuff, "\\%03o", c);
283 sv_catpv(sstr, escbuff);
285 /* XXX Add line breaks if string is long */
287 sv_catpv(sstr, "\"");
295 SV *sstr = newSVpvn("'", 1);
297 char *s = SvPV(sv, n_a);
300 sv_catpv(sstr, "\\'");
302 sv_catpv(sstr, "\\\\");
304 else if (isPRINT(*s))
306 else if (*s >= ' ' && *s < 127)
308 sv_catpvn(sstr, s, 1);
310 sv_catpv(sstr, "\\n");
312 sv_catpv(sstr, "\\r");
314 sv_catpv(sstr, "\\t");
316 sv_catpv(sstr, "\\a");
318 sv_catpv(sstr, "\\b");
320 sv_catpv(sstr, "\\f");
322 sv_catpv(sstr, "\\v");
325 /* no trigraph support */
326 char escbuff[5]; /* to fit backslash, 3 octals + trailing \0 */
327 /* Don't want promotion of a signed -1 char in sprintf args */
328 unsigned char c = (unsigned char) *s;
329 sprintf(escbuff, "\\%03o", c);
330 sv_catpv(sstr, escbuff);
337 walkoptree(pTHX_ SV *opsv, char *method)
344 croak("opsv is not a reference");
345 opsv = sv_mortalcopy(opsv);
346 o = INT2PTR(OP*,SvIV((SV*)SvRV(opsv)));
347 if (walkoptree_debug) {
351 perl_call_method("walkoptree_debug", G_DISCARD);
356 perl_call_method(method, G_DISCARD);
357 if (o && (o->op_flags & OPf_KIDS)) {
359 for (kid = ((UNOP*)o)->op_first; kid; kid = kid->op_sibling) {
360 /* Use the same opsv. Rely on methods not to mess it up. */
361 sv_setiv(newSVrv(opsv, cc_opclassname(aTHX_ kid)), PTR2IV(kid));
362 walkoptree(aTHX_ opsv, method);
368 typedef UNOP *B__UNOP;
369 typedef BINOP *B__BINOP;
370 typedef LOGOP *B__LOGOP;
371 typedef LISTOP *B__LISTOP;
372 typedef PMOP *B__PMOP;
373 typedef SVOP *B__SVOP;
374 typedef PADOP *B__PADOP;
375 typedef PVOP *B__PVOP;
376 typedef LOOP *B__LOOP;
393 typedef MAGIC *B__MAGIC;
395 MODULE = B PACKAGE = B PREFIX = B_
401 HV *stash = gv_stashpvn("B", 1, TRUE);
402 AV *export_ok = perl_get_av("B::EXPORT_OK",TRUE);
404 specialsv_list[0] = Nullsv;
405 specialsv_list[1] = &PL_sv_undef;
406 specialsv_list[2] = &PL_sv_yes;
407 specialsv_list[3] = &PL_sv_no;
408 specialsv_list[4] = pWARN_ALL;
409 specialsv_list[5] = pWARN_NONE;
410 specialsv_list[6] = pWARN_STD;
414 #define B_main_cv() PL_main_cv
415 #define B_init_av() PL_initav
416 #define B_begin_av() PL_beginav_save
417 #define B_end_av() PL_endav
418 #define B_main_root() PL_main_root
419 #define B_main_start() PL_main_start
420 #define B_amagic_generation() PL_amagic_generation
421 #define B_comppadlist() (PL_main_cv ? CvPADLIST(PL_main_cv) : CvPADLIST(PL_compcv))
422 #define B_sv_undef() &PL_sv_undef
423 #define B_sv_yes() &PL_sv_yes
424 #define B_sv_no() &PL_sv_no
426 #define B_regex_padav() PL_regex_padav
455 B_amagic_generation()
469 MODULE = B PACKAGE = B
473 walkoptree(opsv, method)
477 walkoptree(aTHX_ opsv, method);
480 walkoptree_debug(...)
483 RETVAL = walkoptree_debug;
484 if (items > 0 && SvTRUE(ST(1)))
485 walkoptree_debug = 1;
489 #define address(sv) PTR2IV(sv)
500 croak("argument is not a reference");
501 RETVAL = (SV*)SvRV(sv);
512 ST(0) = sv_newmortal();
513 if (strncmp(name,"pp_",3) == 0)
515 for (i = 0; i < PL_maxo; i++)
517 if (strcmp(name, PL_op_name[i]) == 0)
523 sv_setiv(ST(0),result);
530 ST(0) = sv_newmortal();
531 if (opnum >= 0 && opnum < PL_maxo) {
532 sv_setpvn(ST(0), "pp_", 3);
533 sv_catpv(ST(0), PL_op_name[opnum]);
543 char hexhash[19]; /* must fit "0xffffffffffffffff" plus trailing \0 */
545 PERL_HASH(hash, s, len);
546 sprintf(hexhash, "0x%"UVxf, (UV)hash);
547 ST(0) = sv_2mortal(newSVpv(hexhash, 0));
549 #define cast_I32(foo) (I32)foo
568 RETVAL = cstring(aTHX_ sv, 0);
576 RETVAL = cstring(aTHX_ sv, 1);
584 RETVAL = cchar(aTHX_ sv);
591 #ifdef USE_5005THREADS
593 STRLEN len = strlen(PL_threadsv_names);
596 for (i = 0; i < len; i++)
597 PUSHs(sv_2mortal(newSVpvn(&PL_threadsv_names[i], 1)));
601 #define OP_next(o) o->op_next
602 #define OP_sibling(o) o->op_sibling
603 #define OP_desc(o) PL_op_desc[o->op_type]
604 #define OP_targ(o) o->op_targ
605 #define OP_type(o) o->op_type
606 #define OP_seq(o) o->op_seq
607 #define OP_flags(o) o->op_flags
608 #define OP_private(o) o->op_private
610 MODULE = B PACKAGE = B::OP PREFIX = OP_
624 RETVAL = PL_op_name[o->op_type];
634 SV *sv = sv_newmortal();
636 sv_setpvn(sv, "PL_ppaddr[OP_", 13);
637 sv_catpv(sv, PL_op_name[o->op_type]);
638 for (i=13; i<SvCUR(sv); ++i)
639 SvPVX(sv)[i] = toUPPER(SvPVX(sv)[i]);
667 #define UNOP_first(o) o->op_first
669 MODULE = B PACKAGE = B::UNOP PREFIX = UNOP_
675 #define BINOP_last(o) o->op_last
677 MODULE = B PACKAGE = B::BINOP PREFIX = BINOP_
683 #define LOGOP_other(o) o->op_other
685 MODULE = B PACKAGE = B::LOGOP PREFIX = LOGOP_
691 MODULE = B PACKAGE = B::LISTOP PREFIX = LISTOP_
700 for (kid = o->op_first; kid; kid = kid->op_sibling)
706 #define PMOP_pmreplroot(o) o->op_pmreplroot
707 #define PMOP_pmreplstart(o) o->op_pmreplstart
708 #define PMOP_pmnext(o) o->op_pmnext
709 #define PMOP_pmregexp(o) PM_GETRE(o)
711 #define PMOP_pmoffset(o) o->op_pmoffset
713 #define PMOP_pmflags(o) o->op_pmflags
714 #define PMOP_pmpermflags(o) o->op_pmpermflags
715 #define PMOP_pmdynflags(o) o->op_pmdynflags
717 MODULE = B PACKAGE = B::PMOP PREFIX = PMOP_
724 ST(0) = sv_newmortal();
725 root = o->op_pmreplroot;
726 /* OP_PUSHRE stores an SV* instead of an OP* in op_pmreplroot */
727 if (o->op_type == OP_PUSHRE) {
729 sv_setiv(ST(0), INT2PTR(PADOFFSET,root) );
731 sv_setiv(newSVrv(ST(0), root ?
732 svclassnames[SvTYPE((SV*)root)] : "B::SV"),
737 sv_setiv(newSVrv(ST(0), cc_opclassname(aTHX_ root)), PTR2IV(root));
771 REGEXP * rx = NO_INIT
773 ST(0) = sv_newmortal();
776 sv_setpvn(ST(0), rx->precomp, rx->prelen);
778 #define SVOP_sv(o) cSVOPo->op_sv
779 #define SVOP_gv(o) ((GV*)cSVOPo->op_sv)
781 MODULE = B PACKAGE = B::SVOP PREFIX = SVOP_
791 #define PADOP_padix(o) o->op_padix
792 #define PADOP_sv(o) (o->op_padix ? PL_curpad[o->op_padix] : Nullsv)
793 #define PADOP_gv(o) ((o->op_padix \
794 && SvTYPE(PL_curpad[o->op_padix]) == SVt_PVGV) \
795 ? (GV*)PL_curpad[o->op_padix] : Nullgv)
797 MODULE = B PACKAGE = B::PADOP PREFIX = PADOP_
811 MODULE = B PACKAGE = B::PVOP PREFIX = PVOP_
818 * OP_TRANS uses op_pv to point to a table of 256 or >=258 shorts
819 * whereas other PVOPs point to a null terminated string.
821 if (o->op_type == OP_TRANS &&
822 (o->op_private & OPpTRANS_COMPLEMENT) &&
823 !(o->op_private & OPpTRANS_DELETE))
825 short* tbl = (short*)o->op_pv;
826 short entries = 257 + tbl[256];
827 ST(0) = sv_2mortal(newSVpv(o->op_pv, entries * sizeof(short)));
829 else if (o->op_type == OP_TRANS) {
830 ST(0) = sv_2mortal(newSVpv(o->op_pv, 256 * sizeof(short)));
833 ST(0) = sv_2mortal(newSVpv(o->op_pv, 0));
835 #define LOOP_redoop(o) o->op_redoop
836 #define LOOP_nextop(o) o->op_nextop
837 #define LOOP_lastop(o) o->op_lastop
839 MODULE = B PACKAGE = B::LOOP PREFIX = LOOP_
854 #define COP_label(o) o->cop_label
855 #define COP_stashpv(o) CopSTASHPV(o)
856 #define COP_stash(o) CopSTASH(o)
857 #define COP_file(o) CopFILE(o)
858 #define COP_cop_seq(o) o->cop_seq
859 #define COP_arybase(o) o->cop_arybase
860 #define COP_line(o) CopLINE(o)
861 #define COP_warnings(o) o->cop_warnings
863 MODULE = B PACKAGE = B::COP PREFIX = COP_
897 MODULE = B PACKAGE = B::SV PREFIX = Sv
907 MODULE = B PACKAGE = B::IV PREFIX = Sv
922 MODULE = B PACKAGE = B::IV
924 #define needs64bits(sv) ((I32)SvIVX(sv) != SvIVX(sv))
934 if (sizeof(IV) == 8) {
938 * The following way of spelling 32 is to stop compilers on
939 * 32-bit architectures from moaning about the shift count
940 * being >= the width of the type. Such architectures don't
941 * reach this code anyway (unless sizeof(IV) > 8 but then
942 * everything else breaks too so I'm not fussed at the moment).
945 wp[0] = htonl(((UV)iv) >> (sizeof(UV)*4));
947 wp[0] = htonl(((U32)iv) >> (sizeof(UV)*4));
949 wp[1] = htonl(iv & 0xffffffff);
950 ST(0) = sv_2mortal(newSVpvn((char *)wp, 8));
952 U32 w = htonl((U32)SvIVX(sv));
953 ST(0) = sv_2mortal(newSVpvn((char *)&w, 4));
956 MODULE = B PACKAGE = B::NV PREFIX = Sv
966 MODULE = B PACKAGE = B::RV PREFIX = Sv
972 MODULE = B PACKAGE = B::PV PREFIX = Sv
986 croak( "argument is not SvROK" );
995 ST(0) = sv_newmortal();
997 sv_setpvn(ST(0), SvPVX(sv), SvCUR(sv));
998 SvFLAGS(ST(0)) |= SvUTF8(sv);
1001 /* XXX for backward compatibility, but should fail */
1002 /* croak( "argument is not SvPOK" ); */
1003 sv_setpvn(ST(0), NULL, 0);
1014 MODULE = B PACKAGE = B::PVMG PREFIX = Sv
1019 MAGIC * mg = NO_INIT
1021 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic)
1022 XPUSHs(make_mg_object(aTHX_ sv_newmortal(), mg));
1024 MODULE = B PACKAGE = B::PVMG
1030 #define MgMOREMAGIC(mg) mg->mg_moremagic
1031 #define MgPRIVATE(mg) mg->mg_private
1032 #define MgTYPE(mg) mg->mg_type
1033 #define MgFLAGS(mg) mg->mg_flags
1034 #define MgOBJ(mg) mg->mg_obj
1035 #define MgLENGTH(mg) mg->mg_len
1036 #define MgREGEX(mg) PTR2IV(mg->mg_obj)
1038 MODULE = B PACKAGE = B::MAGIC PREFIX = Mg
1060 if( mg->mg_type != 'r' ) {
1064 croak( "OBJ is not meaningful on r-magic" );
1073 if( mg->mg_type == 'r' ) {
1074 RETVAL = MgREGEX(mg);
1077 croak( "REGEX is only meaningful on r-magic" );
1086 if (mg->mg_type == 'r') {
1087 REGEXP* rx = (REGEXP*)mg->mg_obj;
1089 RETVAL = newSVpvn( rx->precomp, rx->prelen );
1092 croak( "precomp is only meaningful on r-magic" );
1105 ST(0) = sv_newmortal();
1107 if (mg->mg_len >= 0){
1108 sv_setpvn(ST(0), mg->mg_ptr, mg->mg_len);
1110 if (mg->mg_len == HEf_SVKEY)
1111 sv_setsv(ST(0),newRV((SV*)mg->mg_ptr));
1115 MODULE = B PACKAGE = B::PVLV PREFIX = Lv
1133 MODULE = B PACKAGE = B::BM PREFIX = Bm
1150 STRLEN len = NO_INIT
1151 char * str = NO_INIT
1153 str = SvPV(sv, len);
1154 /* Boyer-Moore table is just after string and its safety-margin \0 */
1155 ST(0) = sv_2mortal(newSVpvn(str + len + 1, 256));
1157 MODULE = B PACKAGE = B::GV PREFIX = Gv
1163 ST(0) = sv_2mortal(newSVpvn(GvNAME(gv), GvNAMELEN(gv)));
1169 RETVAL = GvGP(gv) == Null(GP*);
1221 MODULE = B PACKAGE = B::GV
1231 MODULE = B PACKAGE = B::IO PREFIX = Io
1284 if( strEQ( name, "stdin" ) ) {
1285 handle = PerlIO_stdin();
1287 else if( strEQ( name, "stdout" ) ) {
1288 handle = PerlIO_stdout();
1290 else if( strEQ( name, "stderr" ) ) {
1291 handle = PerlIO_stderr();
1294 croak( "Invalid value '%s'", name );
1296 RETVAL = handle == IoIFP(io);
1300 MODULE = B PACKAGE = B::IO
1310 MODULE = B PACKAGE = B::AV PREFIX = Av
1320 #define AvOFF(av) ((XPVAV*)SvANY(av))->xof_off
1330 if (AvFILL(av) >= 0) {
1331 SV **svp = AvARRAY(av);
1333 for (i = 0; i <= AvFILL(av); i++)
1334 XPUSHs(make_sv_object(aTHX_ sv_newmortal(), svp[i]));
1337 MODULE = B PACKAGE = B::AV
1343 MODULE = B PACKAGE = B::CV PREFIX = Cv
1381 ST(0) = sv_2mortal(newSViv(PTR2IV(CvXSUB(cv))));
1388 ST(0) = CvCONST(cv) ?
1389 make_sv_object(aTHX_ sv_newmortal(),CvXSUBANY(cv).any_ptr) :
1390 sv_2mortal(newSViv(CvXSUBANY(cv).any_iv));
1392 MODULE = B PACKAGE = B::CV
1398 MODULE = B PACKAGE = B::CV PREFIX = cv_
1405 MODULE = B PACKAGE = B::HV PREFIX = Hv
1435 if (HvKEYS(hv) > 0) {
1439 (void)hv_iterinit(hv);
1440 EXTEND(sp, HvKEYS(hv) * 2);
1441 while ((sv = hv_iternextsv(hv, &key, &len))) {
1442 PUSHs(newSVpvn(key, len));
1443 PUSHs(make_sv_object(aTHX_ sv_newmortal(), sv));