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.
19 #define PL_op_name (pPerl->Perl_get_op_names())
20 #define PL_opargs (pPerl->Perl_get_opargs())
21 #define PL_op_desc (pPerl->Perl_get_op_descs())
25 typedef PerlIO * InputStream;
27 typedef FILE * InputStream;
31 static char *svclassnames[] = {
67 static char *opclassnames[] = {
84 static int walkoptree_debug = 0; /* Flag for walkoptree debug hook */
86 static SV *specialsv_list[4];
95 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
97 if (o->op_type == OP_SASSIGN)
98 return ((o->op_private & OPpASSIGN_BACKWARDS) ? OPc_UNOP : OPc_BINOP);
100 switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
128 case OA_PVOP_OR_SVOP:
130 * Character translations (tr///) are usually a PVOP, keeping a
131 * pointer to a table of shorts used to look up translations.
132 * Under utf8, however, a simple table isn't practical; instead,
133 * the OP is an SVOP, and the SV is a reference to a swash
134 * (i.e., an RV pointing to an HV).
136 return (o->op_private & (OPpTRANS_TO_UTF|OPpTRANS_FROM_UTF))
137 ? OPc_SVOP : OPc_PVOP;
145 case OA_BASEOP_OR_UNOP:
147 * UNI(OP_foo) in toke.c returns token UNI or FUNC1 depending on
148 * whether parens were seen. perly.y uses OPf_SPECIAL to
149 * signal whether a BASEOP had empty parens or none.
150 * Some other UNOPs are created later, though, so the best
151 * test is OPf_KIDS, which is set in newUNOP.
153 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
157 * The file stat OPs are created via UNI(OP_foo) in toke.c but use
158 * the OPf_REF flag to distinguish between OP types instead of the
159 * usual OPf_SPECIAL flag. As usual, if OPf_KIDS is set, then we
160 * return OPc_UNOP so that walkoptree can find our children. If
161 * OPf_KIDS is not set then we check OPf_REF. Without OPf_REF set
162 * (no argument to the operator) it's an OP; with OPf_REF set it's
163 * a GVOP (and op_gv is the GV for the filehandle argument).
165 return ((o->op_flags & OPf_KIDS) ? OPc_UNOP :
166 (o->op_flags & OPf_REF) ? OPc_GVOP : OPc_BASEOP);
170 * next, last, redo, dump and goto use OPf_SPECIAL to indicate that a
171 * label was omitted (in which case it's a BASEOP) or else a term was
172 * seen. In this last case, all except goto are definitely PVOP but
173 * goto is either a PVOP (with an ordinary constant label), an UNOP
174 * with OPf_STACKED (with a non-constant non-sub) or an UNOP for
175 * OP_REFGEN (with goto &sub) in which case OPf_STACKED also seems to
178 if (o->op_flags & OPf_STACKED)
180 else if (o->op_flags & OPf_SPECIAL)
185 warn("can't determine class of operator %s, assuming BASEOP\n",
186 PL_op_name[o->op_type]);
191 cc_opclassname(OP *o)
193 return opclassnames[cc_opclass(o)];
197 make_sv_object(SV *arg, SV *sv)
202 for (iv = 0; iv < sizeof(specialsv_list)/sizeof(SV*); iv++) {
203 if (sv == specialsv_list[iv]) {
209 type = svclassnames[SvTYPE(sv)];
212 sv_setiv(newSVrv(arg, type), iv);
217 make_mg_object(SV *arg, MAGIC *mg)
219 sv_setiv(newSVrv(arg, "B::MAGIC"), (IV)mg);
226 SV *sstr = newSVpvn("", 0);
231 sv_setpvn(sstr, "0", 1);
236 sv_catpv(sstr, "\"");
237 for (; len; len--, s++)
239 /* At least try a little for readability */
241 sv_catpv(sstr, "\\\"");
243 sv_catpv(sstr, "\\\\");
244 else if (*s >= ' ' && *s < 127) /* XXX not portable */
245 sv_catpvn(sstr, s, 1);
247 sv_catpv(sstr, "\\n");
249 sv_catpv(sstr, "\\r");
251 sv_catpv(sstr, "\\t");
253 sv_catpv(sstr, "\\a");
255 sv_catpv(sstr, "\\b");
257 sv_catpv(sstr, "\\f");
259 sv_catpv(sstr, "\\v");
262 /* no trigraph support */
263 char escbuff[5]; /* to fit backslash, 3 octals + trailing \0 */
264 /* Don't want promotion of a signed -1 char in sprintf args */
265 unsigned char c = (unsigned char) *s;
266 sprintf(escbuff, "\\%03o", c);
267 sv_catpv(sstr, escbuff);
269 /* XXX Add line breaks if string is long */
271 sv_catpv(sstr, "\"");
279 SV *sstr = newSVpvn("'", 1);
281 char *s = SvPV(sv, n_a);
284 sv_catpv(sstr, "\\'");
286 sv_catpv(sstr, "\\\\");
287 else if (*s >= ' ' && *s < 127) /* XXX not portable */
288 sv_catpvn(sstr, s, 1);
290 sv_catpv(sstr, "\\n");
292 sv_catpv(sstr, "\\r");
294 sv_catpv(sstr, "\\t");
296 sv_catpv(sstr, "\\a");
298 sv_catpv(sstr, "\\b");
300 sv_catpv(sstr, "\\f");
302 sv_catpv(sstr, "\\v");
305 /* no trigraph support */
306 char escbuff[5]; /* to fit backslash, 3 octals + trailing \0 */
307 /* Don't want promotion of a signed -1 char in sprintf args */
308 unsigned char c = (unsigned char) *s;
309 sprintf(escbuff, "\\%03o", c);
310 sv_catpv(sstr, escbuff);
317 walkoptree(SV *opsv, char *method)
323 croak("opsv is not a reference");
324 opsv = sv_mortalcopy(opsv);
325 o = (OP*)SvIV((SV*)SvRV(opsv));
326 if (walkoptree_debug) {
330 perl_call_method("walkoptree_debug", G_DISCARD);
335 perl_call_method(method, G_DISCARD);
336 if (o && (o->op_flags & OPf_KIDS)) {
338 for (kid = ((UNOP*)o)->op_first; kid; kid = kid->op_sibling) {
339 /* Use the same opsv. Rely on methods not to mess it up. */
340 sv_setiv(newSVrv(opsv, cc_opclassname(kid)), (IV)kid);
341 walkoptree(opsv, method);
347 typedef UNOP *B__UNOP;
348 typedef BINOP *B__BINOP;
349 typedef LOGOP *B__LOGOP;
350 typedef CONDOP *B__CONDOP;
351 typedef LISTOP *B__LISTOP;
352 typedef PMOP *B__PMOP;
353 typedef SVOP *B__SVOP;
354 typedef GVOP *B__GVOP;
355 typedef PVOP *B__PVOP;
356 typedef LOOP *B__LOOP;
373 typedef MAGIC *B__MAGIC;
375 MODULE = B PACKAGE = B PREFIX = B_
381 HV *stash = gv_stashpvn("B", 1, TRUE);
382 AV *export_ok = perl_get_av("B::EXPORT_OK",TRUE);
383 specialsv_list[0] = Nullsv;
384 specialsv_list[1] = &PL_sv_undef;
385 specialsv_list[2] = &PL_sv_yes;
386 specialsv_list[3] = &PL_sv_no;
390 #define B_main_cv() PL_main_cv
391 #define B_init_av() PL_initav
392 #define B_main_root() PL_main_root
393 #define B_main_start() PL_main_start
394 #define B_amagic_generation() PL_amagic_generation
395 #define B_comppadlist() (PL_main_cv ? CvPADLIST(PL_main_cv) : CvPADLIST(PL_compcv))
396 #define B_sv_undef() &PL_sv_undef
397 #define B_sv_yes() &PL_sv_yes
398 #define B_sv_no() &PL_sv_no
413 B_amagic_generation()
427 MODULE = B PACKAGE = B
431 walkoptree(opsv, method)
436 walkoptree_debug(...)
438 RETVAL = walkoptree_debug;
439 if (items > 0 && SvTRUE(ST(1)))
440 walkoptree_debug = 1;
457 #define address(sv) (IV)sv
468 croak("argument is not a reference");
469 RETVAL = (SV*)SvRV(sv);
480 ST(0) = sv_newmortal();
481 if (strncmp(name,"pp_",3) == 0)
483 for (i = 0; i < PL_maxo; i++)
485 if (strcmp(name, PL_op_name[i]) == 0)
491 sv_setiv(ST(0),result);
498 ST(0) = sv_newmortal();
499 if (opnum >= 0 && opnum < PL_maxo) {
500 sv_setpvn(ST(0), "pp_", 3);
501 sv_catpv(ST(0), PL_op_name[opnum]);
511 char hexhash[19]; /* must fit "0xffffffff" plus trailing \0 */
513 PERL_HASH(hash, s, len);
514 sprintf(hexhash, "0x%x", hash);
515 ST(0) = sv_2mortal(newSVpv(hexhash, 0));
517 #define cast_I32(foo) (I32)foo
540 STRLEN len = strlen(PL_threadsv_names);
543 for (i = 0; i < len; i++)
544 PUSHs(sv_2mortal(newSVpvn(&PL_threadsv_names[i], 1)));
548 #define OP_next(o) o->op_next
549 #define OP_sibling(o) o->op_sibling
550 #define OP_desc(o) PL_op_desc[o->op_type]
551 #define OP_targ(o) o->op_targ
552 #define OP_type(o) o->op_type
553 #define OP_seq(o) o->op_seq
554 #define OP_flags(o) o->op_flags
555 #define OP_private(o) o->op_private
557 MODULE = B PACKAGE = B::OP PREFIX = OP_
571 ST(0) = sv_newmortal();
572 sv_setpvn(ST(0), "pp_", 3);
573 sv_catpv(ST(0), PL_op_name[o->op_type]);
599 #define UNOP_first(o) o->op_first
601 MODULE = B PACKAGE = B::UNOP PREFIX = UNOP_
607 #define BINOP_last(o) o->op_last
609 MODULE = B PACKAGE = B::BINOP PREFIX = BINOP_
615 #define LOGOP_other(o) o->op_other
617 MODULE = B PACKAGE = B::LOGOP PREFIX = LOGOP_
623 #define CONDOP_true(o) o->op_true
624 #define CONDOP_false(o) o->op_false
626 MODULE = B PACKAGE = B::CONDOP PREFIX = CONDOP_
636 #define LISTOP_children(o) o->op_children
638 MODULE = B PACKAGE = B::LISTOP PREFIX = LISTOP_
644 #define PMOP_pmreplroot(o) o->op_pmreplroot
645 #define PMOP_pmreplstart(o) o->op_pmreplstart
646 #define PMOP_pmnext(o) o->op_pmnext
647 #define PMOP_pmregexp(o) o->op_pmregexp
648 #define PMOP_pmflags(o) o->op_pmflags
649 #define PMOP_pmpermflags(o) o->op_pmpermflags
651 MODULE = B PACKAGE = B::PMOP PREFIX = PMOP_
658 ST(0) = sv_newmortal();
659 root = o->op_pmreplroot;
660 /* OP_PUSHRE stores an SV* instead of an OP* in op_pmreplroot */
661 if (o->op_type == OP_PUSHRE) {
662 sv_setiv(newSVrv(ST(0), root ?
663 svclassnames[SvTYPE((SV*)root)] : "B::SV"),
667 sv_setiv(newSVrv(ST(0), cc_opclassname(root)), (IV)root);
689 REGEXP * rx = NO_INIT
691 ST(0) = sv_newmortal();
694 sv_setpvn(ST(0), rx->precomp, rx->prelen);
696 #define SVOP_sv(o) o->op_sv
698 MODULE = B PACKAGE = B::SVOP PREFIX = SVOP_
705 #define GVOP_gv(o) o->op_gv
707 MODULE = B PACKAGE = B::GVOP PREFIX = GVOP_
714 MODULE = B PACKAGE = B::PVOP PREFIX = PVOP_
721 * OP_TRANS uses op_pv to point to a table of 256 shorts
722 * whereas other PVOPs point to a null terminated string.
724 ST(0) = sv_2mortal(newSVpv(o->op_pv, (o->op_type == OP_TRANS) ?
725 256 * sizeof(short) : 0));
727 #define LOOP_redoop(o) o->op_redoop
728 #define LOOP_nextop(o) o->op_nextop
729 #define LOOP_lastop(o) o->op_lastop
731 MODULE = B PACKAGE = B::LOOP PREFIX = LOOP_
746 #define COP_label(o) o->cop_label
747 #define COP_stash(o) o->cop_stash
748 #define COP_filegv(o) o->cop_filegv
749 #define COP_cop_seq(o) o->cop_seq
750 #define COP_arybase(o) o->cop_arybase
751 #define COP_line(o) o->cop_line
752 #define COP_warnings(o) o->cop_warnings
754 MODULE = B PACKAGE = B::COP PREFIX = COP_
784 MODULE = B PACKAGE = B::SV PREFIX = Sv
794 MODULE = B PACKAGE = B::IV PREFIX = Sv
804 MODULE = B PACKAGE = B::IV
806 #define needs64bits(sv) ((I32)SvIVX(sv) != SvIVX(sv))
816 if (sizeof(IV) == 8) {
820 * The following way of spelling 32 is to stop compilers on
821 * 32-bit architectures from moaning about the shift count
822 * being >= the width of the type. Such architectures don't
823 * reach this code anyway (unless sizeof(IV) > 8 but then
824 * everything else breaks too so I'm not fussed at the moment).
826 wp[0] = htonl(((U32)iv) >> (sizeof(IV)*4));
827 wp[1] = htonl(iv & 0xffffffff);
828 ST(0) = sv_2mortal(newSVpvn((char *)wp, 8));
830 U32 w = htonl((U32)SvIVX(sv));
831 ST(0) = sv_2mortal(newSVpvn((char *)&w, 4));
834 MODULE = B PACKAGE = B::NV PREFIX = Sv
844 MODULE = B PACKAGE = B::RV PREFIX = Sv
850 MODULE = B PACKAGE = B::PV PREFIX = Sv
856 ST(0) = sv_newmortal();
857 sv_setpvn(ST(0), SvPVX(sv), SvCUR(sv));
859 MODULE = B PACKAGE = B::PVMG PREFIX = Sv
866 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic)
867 XPUSHs(make_mg_object(sv_newmortal(), mg));
869 MODULE = B PACKAGE = B::PVMG
875 #define MgMOREMAGIC(mg) mg->mg_moremagic
876 #define MgPRIVATE(mg) mg->mg_private
877 #define MgTYPE(mg) mg->mg_type
878 #define MgFLAGS(mg) mg->mg_flags
879 #define MgOBJ(mg) mg->mg_obj
880 #define MgLENGTH(mg) mg->mg_len
882 MODULE = B PACKAGE = B::MAGIC PREFIX = Mg
912 ST(0) = sv_newmortal();
914 if (mg->mg_len >= 0){
915 sv_setpvn(ST(0), mg->mg_ptr, mg->mg_len);
917 if (mg->mg_len == HEf_SVKEY)
918 sv_setsv(ST(0),newRV((SV*)mg->mg_ptr));
922 MODULE = B PACKAGE = B::PVLV PREFIX = Lv
940 MODULE = B PACKAGE = B::BM PREFIX = Bm
961 /* Boyer-Moore table is just after string and its safety-margin \0 */
962 ST(0) = sv_2mortal(newSVpvn(str + len + 1, 256));
964 MODULE = B PACKAGE = B::GV PREFIX = Gv
970 ST(0) = sv_2mortal(newSVpvn(GvNAME(gv), GvNAMELEN(gv)));
1016 MODULE = B PACKAGE = B::GV
1026 MODULE = B PACKAGE = B::IO PREFIX = Io
1072 MODULE = B PACKAGE = B::IO
1082 MODULE = B PACKAGE = B::AV PREFIX = Av
1092 #define AvOFF(av) ((XPVAV*)SvANY(av))->xof_off
1102 if (AvFILL(av) >= 0) {
1103 SV **svp = AvARRAY(av);
1105 for (i = 0; i <= AvFILL(av); i++)
1106 XPUSHs(make_sv_object(sv_newmortal(), svp[i]));
1109 MODULE = B PACKAGE = B::AV
1115 MODULE = B PACKAGE = B::CV PREFIX = Cv
1153 ST(0) = sv_2mortal(newSViv((IV)CvXSUB(cv)));
1160 ST(0) = sv_2mortal(newSViv(CvXSUBANY(cv).any_iv));
1162 MODULE = B PACKAGE = B::CV
1169 MODULE = B PACKAGE = B::HV PREFIX = Hv
1199 if (HvKEYS(hv) > 0) {
1203 (void)hv_iterinit(hv);
1204 EXTEND(sp, HvKEYS(hv) * 2);
1205 while (sv = hv_iternextsv(hv, &key, &len)) {
1206 PUSHs(newSVpvn(key, len));
1207 PUSHs(make_sv_object(sv_newmortal(), sv));