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.
18 #define PL_op_name (get_op_names())
19 #define PL_opargs (get_opargs())
20 #define PL_op_desc (get_op_descs())
24 typedef PerlIO * InputStream;
26 typedef FILE * InputStream;
30 static char *svclassnames[] = {
66 static char *opclassnames[] = {
83 static int walkoptree_debug = 0; /* Flag for walkoptree debug hook */
85 static SV *specialsv_list[4];
88 cc_opclass(pTHX_ OP *o)
94 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
96 if (o->op_type == OP_SASSIGN)
97 return ((o->op_private & OPpASSIGN_BACKWARDS) ? OPc_UNOP : OPc_BINOP);
99 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 * a GVOP (and op_gv is the GV for the filehandle argument).
164 return ((o->op_flags & OPf_KIDS) ? OPc_UNOP :
165 (o->op_flags & OPf_REF) ? OPc_GVOP : OPc_BASEOP);
169 * next, last, redo, dump and goto use OPf_SPECIAL to indicate that a
170 * label was omitted (in which case it's a BASEOP) or else a term was
171 * seen. In this last case, all except goto are definitely PVOP but
172 * goto is either a PVOP (with an ordinary constant label), an UNOP
173 * with OPf_STACKED (with a non-constant non-sub) or an UNOP for
174 * OP_REFGEN (with goto &sub) in which case OPf_STACKED also seems to
177 if (o->op_flags & OPf_STACKED)
179 else if (o->op_flags & OPf_SPECIAL)
184 warn("can't determine class of operator %s, assuming BASEOP\n",
185 PL_op_name[o->op_type]);
190 cc_opclassname(pTHX_ OP *o)
192 return opclassnames[cc_opclass(aTHX_ o)];
196 make_sv_object(pTHX_ SV *arg, SV *sv)
201 for (iv = 0; iv < sizeof(specialsv_list)/sizeof(SV*); iv++) {
202 if (sv == specialsv_list[iv]) {
208 type = svclassnames[SvTYPE(sv)];
211 sv_setiv(newSVrv(arg, type), iv);
216 make_mg_object(pTHX_ SV *arg, MAGIC *mg)
218 sv_setiv(newSVrv(arg, "B::MAGIC"), (IV)mg);
223 cstring(pTHX_ SV *sv)
225 SV *sstr = newSVpvn("", 0);
230 sv_setpvn(sstr, "0", 1);
235 sv_catpv(sstr, "\"");
236 for (; len; len--, s++)
238 /* At least try a little for readability */
240 sv_catpv(sstr, "\\\"");
242 sv_catpv(sstr, "\\\\");
243 else if (*s >= ' ' && *s < 127) /* XXX not portable */
244 sv_catpvn(sstr, s, 1);
246 sv_catpv(sstr, "\\n");
248 sv_catpv(sstr, "\\r");
250 sv_catpv(sstr, "\\t");
252 sv_catpv(sstr, "\\a");
254 sv_catpv(sstr, "\\b");
256 sv_catpv(sstr, "\\f");
258 sv_catpv(sstr, "\\v");
261 /* no trigraph support */
262 char escbuff[5]; /* to fit backslash, 3 octals + trailing \0 */
263 /* Don't want promotion of a signed -1 char in sprintf args */
264 unsigned char c = (unsigned char) *s;
265 sprintf(escbuff, "\\%03o", c);
266 sv_catpv(sstr, escbuff);
268 /* XXX Add line breaks if string is long */
270 sv_catpv(sstr, "\"");
278 SV *sstr = newSVpvn("'", 1);
280 char *s = SvPV(sv, n_a);
283 sv_catpv(sstr, "\\'");
285 sv_catpv(sstr, "\\\\");
286 else if (*s >= ' ' && *s < 127) /* XXX not portable */
287 sv_catpvn(sstr, s, 1);
289 sv_catpv(sstr, "\\n");
291 sv_catpv(sstr, "\\r");
293 sv_catpv(sstr, "\\t");
295 sv_catpv(sstr, "\\a");
297 sv_catpv(sstr, "\\b");
299 sv_catpv(sstr, "\\f");
301 sv_catpv(sstr, "\\v");
304 /* no trigraph support */
305 char escbuff[5]; /* to fit backslash, 3 octals + trailing \0 */
306 /* Don't want promotion of a signed -1 char in sprintf args */
307 unsigned char c = (unsigned char) *s;
308 sprintf(escbuff, "\\%03o", c);
309 sv_catpv(sstr, escbuff);
316 walkoptree(pTHX_ SV *opsv, char *method)
322 croak("opsv is not a reference");
323 opsv = sv_mortalcopy(opsv);
324 o = (OP*)SvIV((SV*)SvRV(opsv));
325 if (walkoptree_debug) {
329 perl_call_method("walkoptree_debug", G_DISCARD);
334 perl_call_method(method, G_DISCARD);
335 if (o && (o->op_flags & OPf_KIDS)) {
337 for (kid = ((UNOP*)o)->op_first; kid; kid = kid->op_sibling) {
338 /* Use the same opsv. Rely on methods not to mess it up. */
339 sv_setiv(newSVrv(opsv, cc_opclassname(aTHX_ kid)), (IV)kid);
340 walkoptree(aTHX_ opsv, method);
346 typedef UNOP *B__UNOP;
347 typedef BINOP *B__BINOP;
348 typedef LOGOP *B__LOGOP;
349 typedef CONDOP *B__CONDOP;
350 typedef LISTOP *B__LISTOP;
351 typedef PMOP *B__PMOP;
352 typedef SVOP *B__SVOP;
353 typedef GVOP *B__GVOP;
354 typedef PVOP *B__PVOP;
355 typedef LOOP *B__LOOP;
372 typedef MAGIC *B__MAGIC;
374 MODULE = B PACKAGE = B PREFIX = B_
380 HV *stash = gv_stashpvn("B", 1, TRUE);
381 AV *export_ok = perl_get_av("B::EXPORT_OK",TRUE);
382 specialsv_list[0] = Nullsv;
383 specialsv_list[1] = &PL_sv_undef;
384 specialsv_list[2] = &PL_sv_yes;
385 specialsv_list[3] = &PL_sv_no;
389 #define B_main_cv() PL_main_cv
390 #define B_init_av() PL_initav
391 #define B_main_root() PL_main_root
392 #define B_main_start() PL_main_start
393 #define B_amagic_generation() PL_amagic_generation
394 #define B_comppadlist() (PL_main_cv ? CvPADLIST(PL_main_cv) : CvPADLIST(PL_compcv))
395 #define B_sv_undef() &PL_sv_undef
396 #define B_sv_yes() &PL_sv_yes
397 #define B_sv_no() &PL_sv_no
412 B_amagic_generation()
426 MODULE = B PACKAGE = B
430 walkoptree(opsv, method)
434 walkoptree(aTHX_ opsv, method);
437 walkoptree_debug(...)
439 RETVAL = walkoptree_debug;
440 if (items > 0 && SvTRUE(ST(1)))
441 walkoptree_debug = 1;
445 #define address(sv) (IV)sv
456 croak("argument is not a reference");
457 RETVAL = (SV*)SvRV(sv);
468 ST(0) = sv_newmortal();
469 if (strncmp(name,"pp_",3) == 0)
471 for (i = 0; i < PL_maxo; i++)
473 if (strcmp(name, PL_op_name[i]) == 0)
479 sv_setiv(ST(0),result);
486 ST(0) = sv_newmortal();
487 if (opnum >= 0 && opnum < PL_maxo) {
488 sv_setpvn(ST(0), "pp_", 3);
489 sv_catpv(ST(0), PL_op_name[opnum]);
499 char hexhash[19]; /* must fit "0xffffffff" plus trailing \0 */
501 PERL_HASH(hash, s, len);
502 sprintf(hexhash, "0x%x", hash);
503 ST(0) = sv_2mortal(newSVpv(hexhash, 0));
505 #define cast_I32(foo) (I32)foo
519 RETVAL = cstring(aTHX_ sv);
527 RETVAL = cchar(aTHX_ sv);
536 STRLEN len = strlen(PL_threadsv_names);
539 for (i = 0; i < len; i++)
540 PUSHs(sv_2mortal(newSVpvn(&PL_threadsv_names[i], 1)));
544 #define OP_next(o) o->op_next
545 #define OP_sibling(o) o->op_sibling
546 #define OP_desc(o) PL_op_desc[o->op_type]
547 #define OP_targ(o) o->op_targ
548 #define OP_type(o) o->op_type
549 #define OP_seq(o) o->op_seq
550 #define OP_flags(o) o->op_flags
551 #define OP_private(o) o->op_private
553 MODULE = B PACKAGE = B::OP PREFIX = OP_
567 ST(0) = sv_newmortal();
568 sv_setpvn(ST(0), "pp_", 3);
569 sv_catpv(ST(0), PL_op_name[o->op_type]);
595 #define UNOP_first(o) o->op_first
597 MODULE = B PACKAGE = B::UNOP PREFIX = UNOP_
603 #define BINOP_last(o) o->op_last
605 MODULE = B PACKAGE = B::BINOP PREFIX = BINOP_
611 #define LOGOP_other(o) o->op_other
613 MODULE = B PACKAGE = B::LOGOP PREFIX = LOGOP_
619 #define CONDOP_true(o) o->op_true
620 #define CONDOP_false(o) o->op_false
622 MODULE = B PACKAGE = B::CONDOP PREFIX = CONDOP_
632 #define LISTOP_children(o) o->op_children
634 MODULE = B PACKAGE = B::LISTOP PREFIX = LISTOP_
640 #define PMOP_pmreplroot(o) o->op_pmreplroot
641 #define PMOP_pmreplstart(o) o->op_pmreplstart
642 #define PMOP_pmnext(o) o->op_pmnext
643 #define PMOP_pmregexp(o) o->op_pmregexp
644 #define PMOP_pmflags(o) o->op_pmflags
645 #define PMOP_pmpermflags(o) o->op_pmpermflags
647 MODULE = B PACKAGE = B::PMOP PREFIX = PMOP_
654 ST(0) = sv_newmortal();
655 root = o->op_pmreplroot;
656 /* OP_PUSHRE stores an SV* instead of an OP* in op_pmreplroot */
657 if (o->op_type == OP_PUSHRE) {
658 sv_setiv(newSVrv(ST(0), root ?
659 svclassnames[SvTYPE((SV*)root)] : "B::SV"),
663 sv_setiv(newSVrv(ST(0), cc_opclassname(aTHX_ root)), (IV)root);
685 REGEXP * rx = NO_INIT
687 ST(0) = sv_newmortal();
690 sv_setpvn(ST(0), rx->precomp, rx->prelen);
692 #define SVOP_sv(o) o->op_sv
694 MODULE = B PACKAGE = B::SVOP PREFIX = SVOP_
701 #define GVOP_gv(o) o->op_gv
703 MODULE = B PACKAGE = B::GVOP PREFIX = GVOP_
710 MODULE = B PACKAGE = B::PVOP PREFIX = PVOP_
717 * OP_TRANS uses op_pv to point to a table of 256 shorts
718 * whereas other PVOPs point to a null terminated string.
720 ST(0) = sv_2mortal(newSVpv(o->op_pv, (o->op_type == OP_TRANS) ?
721 256 * sizeof(short) : 0));
723 #define LOOP_redoop(o) o->op_redoop
724 #define LOOP_nextop(o) o->op_nextop
725 #define LOOP_lastop(o) o->op_lastop
727 MODULE = B PACKAGE = B::LOOP PREFIX = LOOP_
742 #define COP_label(o) o->cop_label
743 #define COP_stash(o) o->cop_stash
744 #define COP_filegv(o) o->cop_filegv
745 #define COP_cop_seq(o) o->cop_seq
746 #define COP_arybase(o) o->cop_arybase
747 #define COP_line(o) o->cop_line
748 #define COP_warnings(o) o->cop_warnings
750 MODULE = B PACKAGE = B::COP PREFIX = COP_
780 MODULE = B PACKAGE = B::SV PREFIX = Sv
790 MODULE = B PACKAGE = B::IV PREFIX = Sv
805 MODULE = B PACKAGE = B::IV
807 #define needs64bits(sv) ((I32)SvIVX(sv) != SvIVX(sv))
817 if (sizeof(IV) == 8) {
821 * The following way of spelling 32 is to stop compilers on
822 * 32-bit architectures from moaning about the shift count
823 * being >= the width of the type. Such architectures don't
824 * reach this code anyway (unless sizeof(IV) > 8 but then
825 * everything else breaks too so I'm not fussed at the moment).
827 wp[0] = htonl(((U32)iv) >> (sizeof(IV)*4));
828 wp[1] = htonl(iv & 0xffffffff);
829 ST(0) = sv_2mortal(newSVpvn((char *)wp, 8));
831 U32 w = htonl((U32)SvIVX(sv));
832 ST(0) = sv_2mortal(newSVpvn((char *)&w, 4));
835 MODULE = B PACKAGE = B::NV PREFIX = Sv
845 MODULE = B PACKAGE = B::RV PREFIX = Sv
851 MODULE = B PACKAGE = B::PV PREFIX = Sv
857 ST(0) = sv_newmortal();
858 sv_setpvn(ST(0), SvPVX(sv), SvCUR(sv));
860 MODULE = B PACKAGE = B::PVMG PREFIX = Sv
867 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic)
868 XPUSHs(make_mg_object(aTHX_ sv_newmortal(), mg));
870 MODULE = B PACKAGE = B::PVMG
876 #define MgMOREMAGIC(mg) mg->mg_moremagic
877 #define MgPRIVATE(mg) mg->mg_private
878 #define MgTYPE(mg) mg->mg_type
879 #define MgFLAGS(mg) mg->mg_flags
880 #define MgOBJ(mg) mg->mg_obj
881 #define MgLENGTH(mg) mg->mg_len
883 MODULE = B PACKAGE = B::MAGIC PREFIX = Mg
913 ST(0) = sv_newmortal();
915 if (mg->mg_len >= 0){
916 sv_setpvn(ST(0), mg->mg_ptr, mg->mg_len);
918 if (mg->mg_len == HEf_SVKEY)
919 sv_setsv(ST(0),newRV((SV*)mg->mg_ptr));
923 MODULE = B PACKAGE = B::PVLV PREFIX = Lv
941 MODULE = B PACKAGE = B::BM PREFIX = Bm
962 /* Boyer-Moore table is just after string and its safety-margin \0 */
963 ST(0) = sv_2mortal(newSVpvn(str + len + 1, 256));
965 MODULE = B PACKAGE = B::GV PREFIX = Gv
971 ST(0) = sv_2mortal(newSVpvn(GvNAME(gv), GvNAMELEN(gv)));
1017 MODULE = B PACKAGE = B::GV
1027 MODULE = B PACKAGE = B::IO PREFIX = Io
1073 MODULE = B PACKAGE = B::IO
1083 MODULE = B PACKAGE = B::AV PREFIX = Av
1093 #define AvOFF(av) ((XPVAV*)SvANY(av))->xof_off
1103 if (AvFILL(av) >= 0) {
1104 SV **svp = AvARRAY(av);
1106 for (i = 0; i <= AvFILL(av); i++)
1107 XPUSHs(make_sv_object(aTHX_ sv_newmortal(), svp[i]));
1110 MODULE = B PACKAGE = B::AV
1116 MODULE = B PACKAGE = B::CV PREFIX = Cv
1154 ST(0) = sv_2mortal(newSViv((IV)CvXSUB(cv)));
1161 ST(0) = sv_2mortal(newSViv(CvXSUBANY(cv).any_iv));
1163 MODULE = B PACKAGE = B::CV
1170 MODULE = B PACKAGE = B::HV PREFIX = Hv
1200 if (HvKEYS(hv) > 0) {
1204 (void)hv_iterinit(hv);
1205 EXTEND(sp, HvKEYS(hv) * 2);
1206 while (sv = hv_iternextsv(hv, &key, &len)) {
1207 PUSHs(newSVpvn(key, len));
1208 PUSHs(make_sv_object(aTHX_ sv_newmortal(), sv));