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);
99 if (o->op_type == OP_GV || o->op_type == OP_GVSV || o->op_type == OP_AELEMFAST)
103 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 * an SVOP (and op_sv is the GV for the filehandle argument).
165 return ((o->op_flags & OPf_KIDS) ? OPc_UNOP :
167 (o->op_flags & OPf_REF) ? OPc_PADOP : OPc_BASEOP);
169 (o->op_flags & OPf_REF) ? OPc_SVOP : OPc_BASEOP);
173 * next, last, redo, dump and goto use OPf_SPECIAL to indicate that a
174 * label was omitted (in which case it's a BASEOP) or else a term was
175 * seen. In this last case, all except goto are definitely PVOP but
176 * goto is either a PVOP (with an ordinary constant label), an UNOP
177 * with OPf_STACKED (with a non-constant non-sub) or an UNOP for
178 * OP_REFGEN (with goto &sub) in which case OPf_STACKED also seems to
181 if (o->op_flags & OPf_STACKED)
183 else if (o->op_flags & OPf_SPECIAL)
188 warn("can't determine class of operator %s, assuming BASEOP\n",
189 PL_op_name[o->op_type]);
194 cc_opclassname(pTHX_ OP *o)
196 return opclassnames[cc_opclass(aTHX_ o)];
200 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)
229 SV *sstr = newSVpvn("", 0);
234 sv_setpvn(sstr, "0", 1);
239 sv_catpv(sstr, "\"");
240 for (; len; len--, s++)
242 /* At least try a little for readability */
244 sv_catpv(sstr, "\\\"");
246 sv_catpv(sstr, "\\\\");
247 else if (*s >= ' ' && *s < 127) /* XXX not portable */
248 sv_catpvn(sstr, s, 1);
250 sv_catpv(sstr, "\\n");
252 sv_catpv(sstr, "\\r");
254 sv_catpv(sstr, "\\t");
256 sv_catpv(sstr, "\\a");
258 sv_catpv(sstr, "\\b");
260 sv_catpv(sstr, "\\f");
262 sv_catpv(sstr, "\\v");
265 /* no trigraph support */
266 char escbuff[5]; /* to fit backslash, 3 octals + trailing \0 */
267 /* Don't want promotion of a signed -1 char in sprintf args */
268 unsigned char c = (unsigned char) *s;
269 sprintf(escbuff, "\\%03o", c);
270 sv_catpv(sstr, escbuff);
272 /* XXX Add line breaks if string is long */
274 sv_catpv(sstr, "\"");
282 SV *sstr = newSVpvn("'", 1);
284 char *s = SvPV(sv, n_a);
287 sv_catpv(sstr, "\\'");
289 sv_catpv(sstr, "\\\\");
290 else if (*s >= ' ' && *s < 127) /* XXX not portable */
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");
305 sv_catpv(sstr, "\\v");
308 /* no trigraph support */
309 char escbuff[5]; /* to fit backslash, 3 octals + trailing \0 */
310 /* Don't want promotion of a signed -1 char in sprintf args */
311 unsigned char c = (unsigned char) *s;
312 sprintf(escbuff, "\\%03o", c);
313 sv_catpv(sstr, escbuff);
320 walkoptree(pTHX_ SV *opsv, char *method)
326 croak("opsv is not a reference");
327 opsv = sv_mortalcopy(opsv);
328 o = INT2PTR(OP*,SvIV((SV*)SvRV(opsv)));
329 if (walkoptree_debug) {
333 perl_call_method("walkoptree_debug", G_DISCARD);
338 perl_call_method(method, G_DISCARD);
339 if (o && (o->op_flags & OPf_KIDS)) {
341 for (kid = ((UNOP*)o)->op_first; kid; kid = kid->op_sibling) {
342 /* Use the same opsv. Rely on methods not to mess it up. */
343 sv_setiv(newSVrv(opsv, cc_opclassname(aTHX_ kid)), PTR2IV(kid));
344 walkoptree(aTHX_ opsv, method);
350 typedef UNOP *B__UNOP;
351 typedef BINOP *B__BINOP;
352 typedef LOGOP *B__LOGOP;
353 typedef LISTOP *B__LISTOP;
354 typedef PMOP *B__PMOP;
355 typedef SVOP *B__SVOP;
356 typedef PADOP *B__PADOP;
357 typedef PVOP *B__PVOP;
358 typedef LOOP *B__LOOP;
375 typedef MAGIC *B__MAGIC;
377 MODULE = B PACKAGE = B PREFIX = B_
383 HV *stash = gv_stashpvn("B", 1, TRUE);
384 AV *export_ok = perl_get_av("B::EXPORT_OK",TRUE);
385 specialsv_list[0] = Nullsv;
386 specialsv_list[1] = &PL_sv_undef;
387 specialsv_list[2] = &PL_sv_yes;
388 specialsv_list[3] = &PL_sv_no;
392 #define B_main_cv() PL_main_cv
393 #define B_init_av() PL_initav
394 #define B_main_root() PL_main_root
395 #define B_main_start() PL_main_start
396 #define B_amagic_generation() PL_amagic_generation
397 #define B_comppadlist() (PL_main_cv ? CvPADLIST(PL_main_cv) : CvPADLIST(PL_compcv))
398 #define B_sv_undef() &PL_sv_undef
399 #define B_sv_yes() &PL_sv_yes
400 #define B_sv_no() &PL_sv_no
415 B_amagic_generation()
429 MODULE = B PACKAGE = B
433 walkoptree(opsv, method)
437 walkoptree(aTHX_ opsv, method);
440 walkoptree_debug(...)
442 RETVAL = walkoptree_debug;
443 if (items > 0 && SvTRUE(ST(1)))
444 walkoptree_debug = 1;
448 #define address(sv) PTR2IV(sv)
459 croak("argument is not a reference");
460 RETVAL = (SV*)SvRV(sv);
471 ST(0) = sv_newmortal();
472 if (strncmp(name,"pp_",3) == 0)
474 for (i = 0; i < PL_maxo; i++)
476 if (strcmp(name, PL_op_name[i]) == 0)
482 sv_setiv(ST(0),result);
489 ST(0) = sv_newmortal();
490 if (opnum >= 0 && opnum < PL_maxo) {
491 sv_setpvn(ST(0), "pp_", 3);
492 sv_catpv(ST(0), PL_op_name[opnum]);
502 char hexhash[19]; /* must fit "0xffffffffffffffff" plus trailing \0 */
504 PERL_HASH(hash, s, len);
505 sprintf(hexhash, "0x%"UVxf, (UV)hash);
506 ST(0) = sv_2mortal(newSVpv(hexhash, 0));
508 #define cast_I32(foo) (I32)foo
522 RETVAL = cstring(aTHX_ sv);
530 RETVAL = cchar(aTHX_ sv);
539 STRLEN len = strlen(PL_threadsv_names);
542 for (i = 0; i < len; i++)
543 PUSHs(sv_2mortal(newSVpvn(&PL_threadsv_names[i], 1)));
547 #define OP_next(o) o->op_next
548 #define OP_sibling(o) o->op_sibling
549 #define OP_desc(o) PL_op_desc[o->op_type]
550 #define OP_targ(o) o->op_targ
551 #define OP_type(o) o->op_type
552 #define OP_seq(o) o->op_seq
553 #define OP_flags(o) o->op_flags
554 #define OP_private(o) o->op_private
556 MODULE = B PACKAGE = B::OP PREFIX = OP_
570 ST(0) = sv_newmortal();
571 sv_setpv(ST(0), PL_op_name[o->op_type]);
579 SV *sv = sv_newmortal();
581 sv_setpvn(sv, "PL_ppaddr[OP_", 13);
582 sv_catpv(sv, PL_op_name[o->op_type]);
583 for (i=13; i<SvCUR(sv); ++i)
584 SvPVX(sv)[i] = toUPPER(SvPVX(sv)[i]);
612 #define UNOP_first(o) o->op_first
614 MODULE = B PACKAGE = B::UNOP PREFIX = UNOP_
620 #define BINOP_last(o) o->op_last
622 MODULE = B PACKAGE = B::BINOP PREFIX = BINOP_
628 #define LOGOP_other(o) o->op_other
630 MODULE = B PACKAGE = B::LOGOP PREFIX = LOGOP_
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(aTHX_ root)), PTR2IV(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) cSVOPo->op_sv
697 #define SVOP_gv(o) ((GV*)cSVOPo->op_sv)
699 MODULE = B PACKAGE = B::SVOP PREFIX = SVOP_
709 #define PADOP_padix(o) o->op_padix
710 #define PADOP_sv(o) (o->op_padix ? PL_curpad[o->op_padix] : Nullsv)
711 #define PADOP_gv(o) ((o->op_padix \
712 && SvTYPE(PL_curpad[o->op_padix]) == SVt_PVGV) \
713 ? (GV*)PL_curpad[o->op_padix] : Nullgv)
715 MODULE = B PACKAGE = B::PADOP PREFIX = PADOP_
729 MODULE = B PACKAGE = B::PVOP PREFIX = PVOP_
736 * OP_TRANS uses op_pv to point to a table of 256 shorts
737 * whereas other PVOPs point to a null terminated string.
739 ST(0) = sv_2mortal(newSVpv(o->op_pv, (o->op_type == OP_TRANS) ?
740 256 * sizeof(short) : 0));
742 #define LOOP_redoop(o) o->op_redoop
743 #define LOOP_nextop(o) o->op_nextop
744 #define LOOP_lastop(o) o->op_lastop
746 MODULE = B PACKAGE = B::LOOP PREFIX = LOOP_
761 #define COP_label(o) o->cop_label
762 #define COP_stashpv(o) CopSTASHPV(o)
763 #define COP_stash(o) CopSTASH(o)
764 #define COP_file(o) CopFILE(o)
765 #define COP_cop_seq(o) o->cop_seq
766 #define COP_arybase(o) o->cop_arybase
767 #define COP_line(o) CopLINE(o)
768 #define COP_warnings(o) o->cop_warnings
770 MODULE = B PACKAGE = B::COP PREFIX = COP_
804 MODULE = B PACKAGE = B::SV PREFIX = Sv
814 MODULE = B PACKAGE = B::IV PREFIX = Sv
829 MODULE = B PACKAGE = B::IV
831 #define needs64bits(sv) ((I32)SvIVX(sv) != SvIVX(sv))
841 if (sizeof(IV) == 8) {
845 * The following way of spelling 32 is to stop compilers on
846 * 32-bit architectures from moaning about the shift count
847 * being >= the width of the type. Such architectures don't
848 * reach this code anyway (unless sizeof(IV) > 8 but then
849 * everything else breaks too so I'm not fussed at the moment).
852 wp[0] = htonl(((UV)iv) >> (sizeof(UV)*4));
854 wp[0] = htonl(((U32)iv) >> (sizeof(UV)*4));
856 wp[1] = htonl(iv & 0xffffffff);
857 ST(0) = sv_2mortal(newSVpvn((char *)wp, 8));
859 U32 w = htonl((U32)SvIVX(sv));
860 ST(0) = sv_2mortal(newSVpvn((char *)&w, 4));
863 MODULE = B PACKAGE = B::NV PREFIX = Sv
873 MODULE = B PACKAGE = B::RV PREFIX = Sv
879 MODULE = B PACKAGE = B::PV PREFIX = Sv
885 ST(0) = sv_newmortal();
886 sv_setpvn(ST(0), SvPVX(sv), SvCUR(sv));
896 MODULE = B PACKAGE = B::PVMG PREFIX = Sv
903 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic)
904 XPUSHs(make_mg_object(aTHX_ sv_newmortal(), mg));
906 MODULE = B PACKAGE = B::PVMG
912 #define MgMOREMAGIC(mg) mg->mg_moremagic
913 #define MgPRIVATE(mg) mg->mg_private
914 #define MgTYPE(mg) mg->mg_type
915 #define MgFLAGS(mg) mg->mg_flags
916 #define MgOBJ(mg) mg->mg_obj
917 #define MgLENGTH(mg) mg->mg_len
919 MODULE = B PACKAGE = B::MAGIC PREFIX = Mg
949 ST(0) = sv_newmortal();
951 if (mg->mg_len >= 0){
952 sv_setpvn(ST(0), mg->mg_ptr, mg->mg_len);
954 if (mg->mg_len == HEf_SVKEY)
955 sv_setsv(ST(0),newRV((SV*)mg->mg_ptr));
959 MODULE = B PACKAGE = B::PVLV PREFIX = Lv
977 MODULE = B PACKAGE = B::BM PREFIX = Bm
998 /* Boyer-Moore table is just after string and its safety-margin \0 */
999 ST(0) = sv_2mortal(newSVpvn(str + len + 1, 256));
1001 MODULE = B PACKAGE = B::GV PREFIX = Gv
1007 ST(0) = sv_2mortal(newSVpvn(GvNAME(gv), GvNAMELEN(gv)));
1013 RETVAL = GvGP(gv) == Null(GP*);
1065 MODULE = B PACKAGE = B::GV
1075 MODULE = B PACKAGE = B::IO PREFIX = Io
1121 MODULE = B PACKAGE = B::IO
1131 MODULE = B PACKAGE = B::AV PREFIX = Av
1141 #define AvOFF(av) ((XPVAV*)SvANY(av))->xof_off
1151 if (AvFILL(av) >= 0) {
1152 SV **svp = AvARRAY(av);
1154 for (i = 0; i <= AvFILL(av); i++)
1155 XPUSHs(make_sv_object(aTHX_ sv_newmortal(), svp[i]));
1158 MODULE = B PACKAGE = B::AV
1164 MODULE = B PACKAGE = B::CV PREFIX = Cv
1202 ST(0) = sv_2mortal(newSViv(PTR2IV(CvXSUB(cv))));
1209 ST(0) = sv_2mortal(newSViv(CvXSUBANY(cv).any_iv));
1211 MODULE = B PACKAGE = B::CV
1218 MODULE = B PACKAGE = B::HV PREFIX = Hv
1248 if (HvKEYS(hv) > 0) {
1252 (void)hv_iterinit(hv);
1253 EXTEND(sp, HvKEYS(hv) * 2);
1254 while (sv = hv_iternextsv(hv, &key, &len)) {
1255 PUSHs(newSVpvn(key, len));
1256 PUSHs(make_sv_object(aTHX_ sv_newmortal(), sv));