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);
236 else if (perlstyle && SvUTF8(sv))
238 SV *tmpsv = sv_newmortal(); /* Temporary SV to feed sv_uni_display */
240 s = sv_uni_display(tmpsv, sv, 8*len, UNI_DISPLAY_QQ);
245 sv_catpv(sstr, "\\\"");
247 sv_catpv(sstr, "\\$");
249 sv_catpv(sstr, "\\@");
252 if (strchr("nrftax\\",*(s+1)))
253 sv_catpvn(sstr, s++, 2);
255 sv_catpv(sstr, "\\\\");
257 else /* should always be printable */
258 sv_catpvn(sstr, s, 1);
261 sv_catpv(sstr, "\"");
268 sv_catpv(sstr, "\"");
269 for (; len; len--, s++)
271 /* At least try a little for readability */
273 sv_catpv(sstr, "\\\"");
275 sv_catpv(sstr, "\\\\");
276 /* trigraphs - bleagh */
277 else if (!perlstyle && *s == '?' && len>=3 && s[1] == '?')
279 sprintf(escbuff, "\\%03o", '?');
280 sv_catpv(sstr, escbuff);
282 else if (perlstyle && *s == '$')
283 sv_catpv(sstr, "\\$");
284 else if (perlstyle && *s == '@')
285 sv_catpv(sstr, "\\@");
287 else if (isPRINT(*s))
289 else if (*s >= ' ' && *s < 127)
291 sv_catpvn(sstr, s, 1);
293 sv_catpv(sstr, "\\n");
295 sv_catpv(sstr, "\\r");
297 sv_catpv(sstr, "\\t");
299 sv_catpv(sstr, "\\a");
301 sv_catpv(sstr, "\\b");
303 sv_catpv(sstr, "\\f");
304 else if (!perlstyle && *s == '\v')
305 sv_catpv(sstr, "\\v");
308 /* Don't want promotion of a signed -1 char in sprintf args */
309 unsigned char c = (unsigned char) *s;
310 sprintf(escbuff, "\\%03o", c);
311 sv_catpv(sstr, escbuff);
313 /* XXX Add line breaks if string is long */
315 sv_catpv(sstr, "\"");
323 SV *sstr = newSVpvn("'", 1);
325 char *s = SvPV(sv, n_a);
328 sv_catpv(sstr, "\\'");
330 sv_catpv(sstr, "\\\\");
332 else if (isPRINT(*s))
334 else if (*s >= ' ' && *s < 127)
336 sv_catpvn(sstr, s, 1);
338 sv_catpv(sstr, "\\n");
340 sv_catpv(sstr, "\\r");
342 sv_catpv(sstr, "\\t");
344 sv_catpv(sstr, "\\a");
346 sv_catpv(sstr, "\\b");
348 sv_catpv(sstr, "\\f");
350 sv_catpv(sstr, "\\v");
353 /* no trigraph support */
354 char escbuff[5]; /* to fit backslash, 3 octals + trailing \0 */
355 /* Don't want promotion of a signed -1 char in sprintf args */
356 unsigned char c = (unsigned char) *s;
357 sprintf(escbuff, "\\%03o", c);
358 sv_catpv(sstr, escbuff);
365 walkoptree(pTHX_ SV *opsv, char *method)
372 croak("opsv is not a reference");
373 opsv = sv_mortalcopy(opsv);
374 o = INT2PTR(OP*,SvIV((SV*)SvRV(opsv)));
375 if (walkoptree_debug) {
379 perl_call_method("walkoptree_debug", G_DISCARD);
384 perl_call_method(method, G_DISCARD);
385 if (o && (o->op_flags & OPf_KIDS)) {
387 for (kid = ((UNOP*)o)->op_first; kid; kid = kid->op_sibling) {
388 /* Use the same opsv. Rely on methods not to mess it up. */
389 sv_setiv(newSVrv(opsv, cc_opclassname(aTHX_ kid)), PTR2IV(kid));
390 walkoptree(aTHX_ opsv, method);
396 typedef UNOP *B__UNOP;
397 typedef BINOP *B__BINOP;
398 typedef LOGOP *B__LOGOP;
399 typedef LISTOP *B__LISTOP;
400 typedef PMOP *B__PMOP;
401 typedef SVOP *B__SVOP;
402 typedef PADOP *B__PADOP;
403 typedef PVOP *B__PVOP;
404 typedef LOOP *B__LOOP;
421 typedef MAGIC *B__MAGIC;
423 MODULE = B PACKAGE = B PREFIX = B_
429 HV *stash = gv_stashpvn("B", 1, TRUE);
430 AV *export_ok = perl_get_av("B::EXPORT_OK",TRUE);
432 specialsv_list[0] = Nullsv;
433 specialsv_list[1] = &PL_sv_undef;
434 specialsv_list[2] = &PL_sv_yes;
435 specialsv_list[3] = &PL_sv_no;
436 specialsv_list[4] = pWARN_ALL;
437 specialsv_list[5] = pWARN_NONE;
438 specialsv_list[6] = pWARN_STD;
442 #define B_main_cv() PL_main_cv
443 #define B_init_av() PL_initav
444 #define B_begin_av() PL_beginav_save
445 #define B_end_av() PL_endav
446 #define B_main_root() PL_main_root
447 #define B_main_start() PL_main_start
448 #define B_amagic_generation() PL_amagic_generation
449 #define B_comppadlist() (PL_main_cv ? CvPADLIST(PL_main_cv) : CvPADLIST(PL_compcv))
450 #define B_sv_undef() &PL_sv_undef
451 #define B_sv_yes() &PL_sv_yes
452 #define B_sv_no() &PL_sv_no
454 #define B_regex_padav() PL_regex_padav
483 B_amagic_generation()
497 MODULE = B PACKAGE = B
501 walkoptree(opsv, method)
505 walkoptree(aTHX_ opsv, method);
508 walkoptree_debug(...)
511 RETVAL = walkoptree_debug;
512 if (items > 0 && SvTRUE(ST(1)))
513 walkoptree_debug = 1;
517 #define address(sv) PTR2IV(sv)
528 croak("argument is not a reference");
529 RETVAL = (SV*)SvRV(sv);
540 ST(0) = sv_newmortal();
541 if (strncmp(name,"pp_",3) == 0)
543 for (i = 0; i < PL_maxo; i++)
545 if (strcmp(name, PL_op_name[i]) == 0)
551 sv_setiv(ST(0),result);
558 ST(0) = sv_newmortal();
559 if (opnum >= 0 && opnum < PL_maxo) {
560 sv_setpvn(ST(0), "pp_", 3);
561 sv_catpv(ST(0), PL_op_name[opnum]);
571 char hexhash[19]; /* must fit "0xffffffffffffffff" plus trailing \0 */
573 PERL_HASH(hash, s, len);
574 sprintf(hexhash, "0x%"UVxf, (UV)hash);
575 ST(0) = sv_2mortal(newSVpv(hexhash, 0));
577 #define cast_I32(foo) (I32)foo
596 RETVAL = cstring(aTHX_ sv, 0);
604 RETVAL = cstring(aTHX_ sv, 1);
612 RETVAL = cchar(aTHX_ sv);
619 #ifdef USE_5005THREADS
621 STRLEN len = strlen(PL_threadsv_names);
624 for (i = 0; i < len; i++)
625 PUSHs(sv_2mortal(newSVpvn(&PL_threadsv_names[i], 1)));
629 #define OP_next(o) o->op_next
630 #define OP_sibling(o) o->op_sibling
631 #define OP_desc(o) PL_op_desc[o->op_type]
632 #define OP_targ(o) o->op_targ
633 #define OP_type(o) o->op_type
634 #define OP_seq(o) o->op_seq
635 #define OP_flags(o) o->op_flags
636 #define OP_private(o) o->op_private
638 MODULE = B PACKAGE = B::OP PREFIX = OP_
652 RETVAL = PL_op_name[o->op_type];
662 SV *sv = sv_newmortal();
664 sv_setpvn(sv, "PL_ppaddr[OP_", 13);
665 sv_catpv(sv, PL_op_name[o->op_type]);
666 for (i=13; i<SvCUR(sv); ++i)
667 SvPVX(sv)[i] = toUPPER(SvPVX(sv)[i]);
695 #define UNOP_first(o) o->op_first
697 MODULE = B PACKAGE = B::UNOP PREFIX = UNOP_
703 #define BINOP_last(o) o->op_last
705 MODULE = B PACKAGE = B::BINOP PREFIX = BINOP_
711 #define LOGOP_other(o) o->op_other
713 MODULE = B PACKAGE = B::LOGOP PREFIX = LOGOP_
719 MODULE = B PACKAGE = B::LISTOP PREFIX = LISTOP_
728 for (kid = o->op_first; kid; kid = kid->op_sibling)
734 #define PMOP_pmreplroot(o) o->op_pmreplroot
735 #define PMOP_pmreplstart(o) o->op_pmreplstart
736 #define PMOP_pmnext(o) o->op_pmnext
737 #define PMOP_pmregexp(o) PM_GETRE(o)
739 #define PMOP_pmoffset(o) o->op_pmoffset
741 #define PMOP_pmflags(o) o->op_pmflags
742 #define PMOP_pmpermflags(o) o->op_pmpermflags
743 #define PMOP_pmdynflags(o) o->op_pmdynflags
745 MODULE = B PACKAGE = B::PMOP PREFIX = PMOP_
752 ST(0) = sv_newmortal();
753 root = o->op_pmreplroot;
754 /* OP_PUSHRE stores an SV* instead of an OP* in op_pmreplroot */
755 if (o->op_type == OP_PUSHRE) {
757 sv_setiv(ST(0), INT2PTR(PADOFFSET,root) );
759 sv_setiv(newSVrv(ST(0), root ?
760 svclassnames[SvTYPE((SV*)root)] : "B::SV"),
765 sv_setiv(newSVrv(ST(0), cc_opclassname(aTHX_ root)), PTR2IV(root));
799 REGEXP * rx = NO_INIT
801 ST(0) = sv_newmortal();
804 sv_setpvn(ST(0), rx->precomp, rx->prelen);
806 #define SVOP_sv(o) cSVOPo->op_sv
807 #define SVOP_gv(o) ((GV*)cSVOPo->op_sv)
809 MODULE = B PACKAGE = B::SVOP PREFIX = SVOP_
819 #define PADOP_padix(o) o->op_padix
820 #define PADOP_sv(o) (o->op_padix ? PL_curpad[o->op_padix] : Nullsv)
821 #define PADOP_gv(o) ((o->op_padix \
822 && SvTYPE(PL_curpad[o->op_padix]) == SVt_PVGV) \
823 ? (GV*)PL_curpad[o->op_padix] : Nullgv)
825 MODULE = B PACKAGE = B::PADOP PREFIX = PADOP_
839 MODULE = B PACKAGE = B::PVOP PREFIX = PVOP_
846 * OP_TRANS uses op_pv to point to a table of 256 or >=258 shorts
847 * whereas other PVOPs point to a null terminated string.
849 if (o->op_type == OP_TRANS &&
850 (o->op_private & OPpTRANS_COMPLEMENT) &&
851 !(o->op_private & OPpTRANS_DELETE))
853 short* tbl = (short*)o->op_pv;
854 short entries = 257 + tbl[256];
855 ST(0) = sv_2mortal(newSVpv(o->op_pv, entries * sizeof(short)));
857 else if (o->op_type == OP_TRANS) {
858 ST(0) = sv_2mortal(newSVpv(o->op_pv, 256 * sizeof(short)));
861 ST(0) = sv_2mortal(newSVpv(o->op_pv, 0));
863 #define LOOP_redoop(o) o->op_redoop
864 #define LOOP_nextop(o) o->op_nextop
865 #define LOOP_lastop(o) o->op_lastop
867 MODULE = B PACKAGE = B::LOOP PREFIX = LOOP_
882 #define COP_label(o) o->cop_label
883 #define COP_stashpv(o) CopSTASHPV(o)
884 #define COP_stash(o) CopSTASH(o)
885 #define COP_file(o) CopFILE(o)
886 #define COP_cop_seq(o) o->cop_seq
887 #define COP_arybase(o) o->cop_arybase
888 #define COP_line(o) CopLINE(o)
889 #define COP_warnings(o) o->cop_warnings
891 MODULE = B PACKAGE = B::COP PREFIX = COP_
925 MODULE = B PACKAGE = B::SV PREFIX = Sv
935 MODULE = B PACKAGE = B::IV PREFIX = Sv
950 MODULE = B PACKAGE = B::IV
952 #define needs64bits(sv) ((I32)SvIVX(sv) != SvIVX(sv))
962 if (sizeof(IV) == 8) {
966 * The following way of spelling 32 is to stop compilers on
967 * 32-bit architectures from moaning about the shift count
968 * being >= the width of the type. Such architectures don't
969 * reach this code anyway (unless sizeof(IV) > 8 but then
970 * everything else breaks too so I'm not fussed at the moment).
973 wp[0] = htonl(((UV)iv) >> (sizeof(UV)*4));
975 wp[0] = htonl(((U32)iv) >> (sizeof(UV)*4));
977 wp[1] = htonl(iv & 0xffffffff);
978 ST(0) = sv_2mortal(newSVpvn((char *)wp, 8));
980 U32 w = htonl((U32)SvIVX(sv));
981 ST(0) = sv_2mortal(newSVpvn((char *)&w, 4));
984 MODULE = B PACKAGE = B::NV PREFIX = Sv
994 MODULE = B PACKAGE = B::RV PREFIX = Sv
1000 MODULE = B PACKAGE = B::PV PREFIX = Sv
1014 croak( "argument is not SvROK" );
1023 ST(0) = sv_newmortal();
1025 sv_setpvn(ST(0), SvPVX(sv), SvCUR(sv));
1026 SvFLAGS(ST(0)) |= SvUTF8(sv);
1029 /* XXX for backward compatibility, but should fail */
1030 /* croak( "argument is not SvPOK" ); */
1031 sv_setpvn(ST(0), NULL, 0);
1042 MODULE = B PACKAGE = B::PVMG PREFIX = Sv
1047 MAGIC * mg = NO_INIT
1049 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic)
1050 XPUSHs(make_mg_object(aTHX_ sv_newmortal(), mg));
1052 MODULE = B PACKAGE = B::PVMG
1058 #define MgMOREMAGIC(mg) mg->mg_moremagic
1059 #define MgPRIVATE(mg) mg->mg_private
1060 #define MgTYPE(mg) mg->mg_type
1061 #define MgFLAGS(mg) mg->mg_flags
1062 #define MgOBJ(mg) mg->mg_obj
1063 #define MgLENGTH(mg) mg->mg_len
1064 #define MgREGEX(mg) PTR2IV(mg->mg_obj)
1066 MODULE = B PACKAGE = B::MAGIC PREFIX = Mg
1088 if( mg->mg_type != 'r' ) {
1092 croak( "OBJ is not meaningful on r-magic" );
1101 if( mg->mg_type == 'r' ) {
1102 RETVAL = MgREGEX(mg);
1105 croak( "REGEX is only meaningful on r-magic" );
1114 if (mg->mg_type == 'r') {
1115 REGEXP* rx = (REGEXP*)mg->mg_obj;
1117 RETVAL = newSVpvn( rx->precomp, rx->prelen );
1120 croak( "precomp is only meaningful on r-magic" );
1133 ST(0) = sv_newmortal();
1135 if (mg->mg_len >= 0){
1136 sv_setpvn(ST(0), mg->mg_ptr, mg->mg_len);
1138 if (mg->mg_len == HEf_SVKEY)
1139 sv_setsv(ST(0),newRV((SV*)mg->mg_ptr));
1143 MODULE = B PACKAGE = B::PVLV PREFIX = Lv
1161 MODULE = B PACKAGE = B::BM PREFIX = Bm
1178 STRLEN len = NO_INIT
1179 char * str = NO_INIT
1181 str = SvPV(sv, len);
1182 /* Boyer-Moore table is just after string and its safety-margin \0 */
1183 ST(0) = sv_2mortal(newSVpvn(str + len + 1, 256));
1185 MODULE = B PACKAGE = B::GV PREFIX = Gv
1191 ST(0) = sv_2mortal(newSVpvn(GvNAME(gv), GvNAMELEN(gv)));
1197 RETVAL = GvGP(gv) == Null(GP*);
1249 MODULE = B PACKAGE = B::GV
1259 MODULE = B PACKAGE = B::IO PREFIX = Io
1312 if( strEQ( name, "stdin" ) ) {
1313 handle = PerlIO_stdin();
1315 else if( strEQ( name, "stdout" ) ) {
1316 handle = PerlIO_stdout();
1318 else if( strEQ( name, "stderr" ) ) {
1319 handle = PerlIO_stderr();
1322 croak( "Invalid value '%s'", name );
1324 RETVAL = handle == IoIFP(io);
1328 MODULE = B PACKAGE = B::IO
1338 MODULE = B PACKAGE = B::AV PREFIX = Av
1348 #define AvOFF(av) ((XPVAV*)SvANY(av))->xof_off
1358 if (AvFILL(av) >= 0) {
1359 SV **svp = AvARRAY(av);
1361 for (i = 0; i <= AvFILL(av); i++)
1362 XPUSHs(make_sv_object(aTHX_ sv_newmortal(), svp[i]));
1365 MODULE = B PACKAGE = B::AV
1371 MODULE = B PACKAGE = B::CV PREFIX = Cv
1409 ST(0) = sv_2mortal(newSViv(PTR2IV(CvXSUB(cv))));
1416 ST(0) = CvCONST(cv) ?
1417 make_sv_object(aTHX_ sv_newmortal(),CvXSUBANY(cv).any_ptr) :
1418 sv_2mortal(newSViv(CvXSUBANY(cv).any_iv));
1420 MODULE = B PACKAGE = B::CV
1426 MODULE = B PACKAGE = B::CV PREFIX = cv_
1433 MODULE = B PACKAGE = B::HV PREFIX = Hv
1463 if (HvKEYS(hv) > 0) {
1467 (void)hv_iterinit(hv);
1468 EXTEND(sp, HvKEYS(hv) * 2);
1469 while ((sv = hv_iternextsv(hv, &key, &len))) {
1470 PUSHs(newSVpvn(key, len));
1471 PUSHs(make_sv_object(aTHX_ sv_newmortal(), sv));