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
19 #define PL_op_name (get_op_names())
20 #define PL_opargs (get_opargs())
21 #define PL_op_desc (get_op_descs())
25 typedef PerlIO * InputStream;
27 typedef FILE * InputStream;
31 static char *svclassnames[] = {
66 static char *opclassnames[] = {
82 static int walkoptree_debug = 0; /* Flag for walkoptree debug hook */
84 static SV *specialsv_list[4];
87 cc_opclass(pTHX_ OP *o)
93 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
95 if (o->op_type == OP_SASSIGN)
96 return ((o->op_private & OPpASSIGN_BACKWARDS) ? OPc_UNOP : OPc_BINOP);
98 switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
123 case OA_PVOP_OR_SVOP:
125 * Character translations (tr///) are usually a PVOP, keeping a
126 * pointer to a table of shorts used to look up translations.
127 * Under utf8, however, a simple table isn't practical; instead,
128 * the OP is an SVOP, and the SV is a reference to a swash
129 * (i.e., an RV pointing to an HV).
131 return (o->op_private & (OPpTRANS_TO_UTF|OPpTRANS_FROM_UTF))
132 ? OPc_SVOP : OPc_PVOP;
140 case OA_BASEOP_OR_UNOP:
142 * UNI(OP_foo) in toke.c returns token UNI or FUNC1 depending on
143 * whether parens were seen. perly.y uses OPf_SPECIAL to
144 * signal whether a BASEOP had empty parens or none.
145 * Some other UNOPs are created later, though, so the best
146 * test is OPf_KIDS, which is set in newUNOP.
148 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
152 * The file stat OPs are created via UNI(OP_foo) in toke.c but use
153 * the OPf_REF flag to distinguish between OP types instead of the
154 * usual OPf_SPECIAL flag. As usual, if OPf_KIDS is set, then we
155 * return OPc_UNOP so that walkoptree can find our children. If
156 * OPf_KIDS is not set then we check OPf_REF. Without OPf_REF set
157 * (no argument to the operator) it's an OP; with OPf_REF set it's
158 * a GVOP (and op_gv is the GV for the filehandle argument).
160 return ((o->op_flags & OPf_KIDS) ? OPc_UNOP :
161 (o->op_flags & OPf_REF) ? OPc_GVOP : OPc_BASEOP);
165 * next, last, redo, dump and goto use OPf_SPECIAL to indicate that a
166 * label was omitted (in which case it's a BASEOP) or else a term was
167 * seen. In this last case, all except goto are definitely PVOP but
168 * goto is either a PVOP (with an ordinary constant label), an UNOP
169 * with OPf_STACKED (with a non-constant non-sub) or an UNOP for
170 * OP_REFGEN (with goto &sub) in which case OPf_STACKED also seems to
173 if (o->op_flags & OPf_STACKED)
175 else if (o->op_flags & OPf_SPECIAL)
180 warn("can't determine class of operator %s, assuming BASEOP\n",
181 PL_op_name[o->op_type]);
186 cc_opclassname(pTHX_ OP *o)
188 return opclassnames[cc_opclass(aTHX_ o)];
192 make_sv_object(pTHX_ SV *arg, SV *sv)
197 for (iv = 0; iv < sizeof(specialsv_list)/sizeof(SV*); iv++) {
198 if (sv == specialsv_list[iv]) {
204 type = svclassnames[SvTYPE(sv)];
207 sv_setiv(newSVrv(arg, type), iv);
212 make_mg_object(pTHX_ SV *arg, MAGIC *mg)
214 sv_setiv(newSVrv(arg, "B::MAGIC"), PTR2IV(mg));
219 cstring(pTHX_ SV *sv)
221 SV *sstr = newSVpvn("", 0);
226 sv_setpvn(sstr, "0", 1);
231 sv_catpv(sstr, "\"");
232 for (; len; len--, s++)
234 /* At least try a little for readability */
236 sv_catpv(sstr, "\\\"");
238 sv_catpv(sstr, "\\\\");
239 else if (*s >= ' ' && *s < 127) /* XXX not portable */
240 sv_catpvn(sstr, s, 1);
242 sv_catpv(sstr, "\\n");
244 sv_catpv(sstr, "\\r");
246 sv_catpv(sstr, "\\t");
248 sv_catpv(sstr, "\\a");
250 sv_catpv(sstr, "\\b");
252 sv_catpv(sstr, "\\f");
254 sv_catpv(sstr, "\\v");
257 /* no trigraph support */
258 char escbuff[5]; /* to fit backslash, 3 octals + trailing \0 */
259 /* Don't want promotion of a signed -1 char in sprintf args */
260 unsigned char c = (unsigned char) *s;
261 sprintf(escbuff, "\\%03o", c);
262 sv_catpv(sstr, escbuff);
264 /* XXX Add line breaks if string is long */
266 sv_catpv(sstr, "\"");
274 SV *sstr = newSVpvn("'", 1);
276 char *s = SvPV(sv, n_a);
279 sv_catpv(sstr, "\\'");
281 sv_catpv(sstr, "\\\\");
282 else if (*s >= ' ' && *s < 127) /* XXX not portable */
283 sv_catpvn(sstr, s, 1);
285 sv_catpv(sstr, "\\n");
287 sv_catpv(sstr, "\\r");
289 sv_catpv(sstr, "\\t");
291 sv_catpv(sstr, "\\a");
293 sv_catpv(sstr, "\\b");
295 sv_catpv(sstr, "\\f");
297 sv_catpv(sstr, "\\v");
300 /* no trigraph support */
301 char escbuff[5]; /* to fit backslash, 3 octals + trailing \0 */
302 /* Don't want promotion of a signed -1 char in sprintf args */
303 unsigned char c = (unsigned char) *s;
304 sprintf(escbuff, "\\%03o", c);
305 sv_catpv(sstr, escbuff);
312 walkoptree(pTHX_ SV *opsv, char *method)
318 croak("opsv is not a reference");
319 opsv = sv_mortalcopy(opsv);
320 o = INT2PTR(OP*,SvIV((SV*)SvRV(opsv)));
321 if (walkoptree_debug) {
325 perl_call_method("walkoptree_debug", G_DISCARD);
330 perl_call_method(method, G_DISCARD);
331 if (o && (o->op_flags & OPf_KIDS)) {
333 for (kid = ((UNOP*)o)->op_first; kid; kid = kid->op_sibling) {
334 /* Use the same opsv. Rely on methods not to mess it up. */
335 sv_setiv(newSVrv(opsv, cc_opclassname(aTHX_ kid)), PTR2IV(kid));
336 walkoptree(aTHX_ opsv, method);
342 typedef UNOP *B__UNOP;
343 typedef BINOP *B__BINOP;
344 typedef LOGOP *B__LOGOP;
345 typedef LISTOP *B__LISTOP;
346 typedef PMOP *B__PMOP;
347 typedef SVOP *B__SVOP;
348 typedef GVOP *B__GVOP;
349 typedef PVOP *B__PVOP;
350 typedef LOOP *B__LOOP;
367 typedef MAGIC *B__MAGIC;
369 MODULE = B PACKAGE = B PREFIX = B_
375 HV *stash = gv_stashpvn("B", 1, TRUE);
376 AV *export_ok = perl_get_av("B::EXPORT_OK",TRUE);
377 specialsv_list[0] = Nullsv;
378 specialsv_list[1] = &PL_sv_undef;
379 specialsv_list[2] = &PL_sv_yes;
380 specialsv_list[3] = &PL_sv_no;
384 #define B_main_cv() PL_main_cv
385 #define B_init_av() PL_initav
386 #define B_main_root() PL_main_root
387 #define B_main_start() PL_main_start
388 #define B_amagic_generation() PL_amagic_generation
389 #define B_comppadlist() (PL_main_cv ? CvPADLIST(PL_main_cv) : CvPADLIST(PL_compcv))
390 #define B_sv_undef() &PL_sv_undef
391 #define B_sv_yes() &PL_sv_yes
392 #define B_sv_no() &PL_sv_no
407 B_amagic_generation()
421 MODULE = B PACKAGE = B
425 walkoptree(opsv, method)
429 walkoptree(aTHX_ opsv, method);
432 walkoptree_debug(...)
434 RETVAL = walkoptree_debug;
435 if (items > 0 && SvTRUE(ST(1)))
436 walkoptree_debug = 1;
440 #define address(sv) PTR2IV(sv)
451 croak("argument is not a reference");
452 RETVAL = (SV*)SvRV(sv);
463 ST(0) = sv_newmortal();
464 if (strncmp(name,"pp_",3) == 0)
466 for (i = 0; i < PL_maxo; i++)
468 if (strcmp(name, PL_op_name[i]) == 0)
474 sv_setiv(ST(0),result);
481 ST(0) = sv_newmortal();
482 if (opnum >= 0 && opnum < PL_maxo) {
483 sv_setpvn(ST(0), "pp_", 3);
484 sv_catpv(ST(0), PL_op_name[opnum]);
494 char hexhash[19]; /* must fit "0xffffffff" plus trailing \0 */
496 PERL_HASH(hash, s, len);
497 sprintf(hexhash, "0x%x", hash);
498 ST(0) = sv_2mortal(newSVpv(hexhash, 0));
500 #define cast_I32(foo) (I32)foo
514 RETVAL = cstring(aTHX_ sv);
522 RETVAL = cchar(aTHX_ sv);
531 STRLEN len = strlen(PL_threadsv_names);
534 for (i = 0; i < len; i++)
535 PUSHs(sv_2mortal(newSVpvn(&PL_threadsv_names[i], 1)));
539 #define OP_next(o) o->op_next
540 #define OP_sibling(o) o->op_sibling
541 #define OP_desc(o) PL_op_desc[o->op_type]
542 #define OP_targ(o) o->op_targ
543 #define OP_type(o) o->op_type
544 #define OP_seq(o) o->op_seq
545 #define OP_flags(o) o->op_flags
546 #define OP_private(o) o->op_private
548 MODULE = B PACKAGE = B::OP PREFIX = OP_
562 ST(0) = sv_newmortal();
563 sv_setpv(ST(0), PL_op_name[o->op_type]);
570 ST(0) = sv_newmortal();
571 sv_setpvn(ST(0), "Perl_pp_", 8);
572 sv_catpv(ST(0), PL_op_name[o->op_type]);
598 #define UNOP_first(o) o->op_first
600 MODULE = B PACKAGE = B::UNOP PREFIX = UNOP_
606 #define BINOP_last(o) o->op_last
608 MODULE = B PACKAGE = B::BINOP PREFIX = BINOP_
614 #define LOGOP_other(o) o->op_other
616 MODULE = B PACKAGE = B::LOGOP PREFIX = LOGOP_
622 #define LISTOP_children(o) o->op_children
624 MODULE = B PACKAGE = B::LISTOP PREFIX = LISTOP_
630 #define PMOP_pmreplroot(o) o->op_pmreplroot
631 #define PMOP_pmreplstart(o) o->op_pmreplstart
632 #define PMOP_pmnext(o) o->op_pmnext
633 #define PMOP_pmregexp(o) o->op_pmregexp
634 #define PMOP_pmflags(o) o->op_pmflags
635 #define PMOP_pmpermflags(o) o->op_pmpermflags
637 MODULE = B PACKAGE = B::PMOP PREFIX = PMOP_
644 ST(0) = sv_newmortal();
645 root = o->op_pmreplroot;
646 /* OP_PUSHRE stores an SV* instead of an OP* in op_pmreplroot */
647 if (o->op_type == OP_PUSHRE) {
648 sv_setiv(newSVrv(ST(0), root ?
649 svclassnames[SvTYPE((SV*)root)] : "B::SV"),
653 sv_setiv(newSVrv(ST(0), cc_opclassname(aTHX_ root)), PTR2IV(root));
675 REGEXP * rx = NO_INIT
677 ST(0) = sv_newmortal();
680 sv_setpvn(ST(0), rx->precomp, rx->prelen);
682 #define SVOP_sv(o) o->op_sv
684 MODULE = B PACKAGE = B::SVOP PREFIX = SVOP_
691 #define GVOP_gv(o) o->op_gv
693 MODULE = B PACKAGE = B::GVOP PREFIX = GVOP_
700 MODULE = B PACKAGE = B::PVOP PREFIX = PVOP_
707 * OP_TRANS uses op_pv to point to a table of 256 shorts
708 * whereas other PVOPs point to a null terminated string.
710 ST(0) = sv_2mortal(newSVpv(o->op_pv, (o->op_type == OP_TRANS) ?
711 256 * sizeof(short) : 0));
713 #define LOOP_redoop(o) o->op_redoop
714 #define LOOP_nextop(o) o->op_nextop
715 #define LOOP_lastop(o) o->op_lastop
717 MODULE = B PACKAGE = B::LOOP PREFIX = LOOP_
732 #define COP_label(o) o->cop_label
733 #define COP_stash(o) o->cop_stash
734 #define COP_filegv(o) o->cop_filegv
735 #define COP_cop_seq(o) o->cop_seq
736 #define COP_arybase(o) o->cop_arybase
737 #define COP_line(o) o->cop_line
738 #define COP_warnings(o) o->cop_warnings
740 MODULE = B PACKAGE = B::COP PREFIX = COP_
770 MODULE = B PACKAGE = B::SV PREFIX = Sv
780 MODULE = B PACKAGE = B::IV PREFIX = Sv
795 MODULE = B PACKAGE = B::IV
797 #define needs64bits(sv) ((I32)SvIVX(sv) != SvIVX(sv))
807 if (sizeof(IV) == 8) {
811 * The following way of spelling 32 is to stop compilers on
812 * 32-bit architectures from moaning about the shift count
813 * being >= the width of the type. Such architectures don't
814 * reach this code anyway (unless sizeof(IV) > 8 but then
815 * everything else breaks too so I'm not fussed at the moment).
818 wp[0] = htonl(((UV)iv) >> (sizeof(UV)*4));
820 wp[0] = htonl(((U32)iv) >> (sizeof(UV)*4));
822 wp[1] = htonl(iv & 0xffffffff);
823 ST(0) = sv_2mortal(newSVpvn((char *)wp, 8));
825 U32 w = htonl((U32)SvIVX(sv));
826 ST(0) = sv_2mortal(newSVpvn((char *)&w, 4));
829 MODULE = B PACKAGE = B::NV PREFIX = Sv
839 MODULE = B PACKAGE = B::RV PREFIX = Sv
845 MODULE = B PACKAGE = B::PV PREFIX = Sv
851 ST(0) = sv_newmortal();
852 sv_setpvn(ST(0), SvPVX(sv), SvCUR(sv));
862 MODULE = B PACKAGE = B::PVMG PREFIX = Sv
869 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic)
870 XPUSHs(make_mg_object(aTHX_ sv_newmortal(), mg));
872 MODULE = B PACKAGE = B::PVMG
878 #define MgMOREMAGIC(mg) mg->mg_moremagic
879 #define MgPRIVATE(mg) mg->mg_private
880 #define MgTYPE(mg) mg->mg_type
881 #define MgFLAGS(mg) mg->mg_flags
882 #define MgOBJ(mg) mg->mg_obj
883 #define MgLENGTH(mg) mg->mg_len
885 MODULE = B PACKAGE = B::MAGIC PREFIX = Mg
915 ST(0) = sv_newmortal();
917 if (mg->mg_len >= 0){
918 sv_setpvn(ST(0), mg->mg_ptr, mg->mg_len);
920 if (mg->mg_len == HEf_SVKEY)
921 sv_setsv(ST(0),newRV((SV*)mg->mg_ptr));
925 MODULE = B PACKAGE = B::PVLV PREFIX = Lv
943 MODULE = B PACKAGE = B::BM PREFIX = Bm
964 /* Boyer-Moore table is just after string and its safety-margin \0 */
965 ST(0) = sv_2mortal(newSVpvn(str + len + 1, 256));
967 MODULE = B PACKAGE = B::GV PREFIX = Gv
973 ST(0) = sv_2mortal(newSVpvn(GvNAME(gv), GvNAMELEN(gv)));
1019 MODULE = B PACKAGE = B::GV
1029 MODULE = B PACKAGE = B::IO PREFIX = Io
1075 MODULE = B PACKAGE = B::IO
1085 MODULE = B PACKAGE = B::AV PREFIX = Av
1095 #define AvOFF(av) ((XPVAV*)SvANY(av))->xof_off
1105 if (AvFILL(av) >= 0) {
1106 SV **svp = AvARRAY(av);
1108 for (i = 0; i <= AvFILL(av); i++)
1109 XPUSHs(make_sv_object(aTHX_ sv_newmortal(), svp[i]));
1112 MODULE = B PACKAGE = B::AV
1118 MODULE = B PACKAGE = B::CV PREFIX = Cv
1156 ST(0) = sv_2mortal(newSViv(PTR2IV(CvXSUB(cv))));
1163 ST(0) = sv_2mortal(newSViv(CvXSUBANY(cv).any_iv));
1165 MODULE = B PACKAGE = B::CV
1172 MODULE = B PACKAGE = B::HV PREFIX = Hv
1202 if (HvKEYS(hv) > 0) {
1206 (void)hv_iterinit(hv);
1207 EXTEND(sp, HvKEYS(hv) * 2);
1208 while (sv = hv_iternextsv(hv, &key, &len)) {
1209 PUSHs(newSVpvn(key, len));
1210 PUSHs(make_sv_object(aTHX_ sv_newmortal(), sv));