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 op_name (pPerl->Perl_get_op_names())
20 #define opargs (pPerl->Perl_get_opargs())
21 #define 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 */
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 (opargs[o->op_type] & OA_CLASS_MASK) {
135 case OA_BASEOP_OR_UNOP:
137 * UNI(OP_foo) in toke.c returns token UNI or FUNC1 depending on
138 * whether parens were seen. perly.y uses OPf_SPECIAL to
139 * signal whether a BASEOP had empty parens or none.
140 * Some other UNOPs are created later, though, so the best
141 * test is OPf_KIDS, which is set in newUNOP.
143 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
147 * The file stat OPs are created via UNI(OP_foo) in toke.c but use
148 * the OPf_REF flag to distinguish between OP types instead of the
149 * usual OPf_SPECIAL flag. As usual, if OPf_KIDS is set, then we
150 * return OPc_UNOP so that walkoptree can find our children. If
151 * OPf_KIDS is not set then we check OPf_REF. Without OPf_REF set
152 * (no argument to the operator) it's an OP; with OPf_REF set it's
153 * a GVOP (and op_gv is the GV for the filehandle argument).
155 return ((o->op_flags & OPf_KIDS) ? OPc_UNOP :
156 (o->op_flags & OPf_REF) ? OPc_GVOP : OPc_BASEOP);
160 * next, last, redo, dump and goto use OPf_SPECIAL to indicate that a
161 * label was omitted (in which case it's a BASEOP) or else a term was
162 * seen. In this last case, all except goto are definitely PVOP but
163 * goto is either a PVOP (with an ordinary constant label), an UNOP
164 * with OPf_STACKED (with a non-constant non-sub) or an UNOP for
165 * OP_REFGEN (with goto &sub) in which case OPf_STACKED also seems to
168 if (o->op_flags & OPf_STACKED)
170 else if (o->op_flags & OPf_SPECIAL)
175 warn("can't determine class of operator %s, assuming BASEOP\n",
176 op_name[o->op_type]);
181 cc_opclassname(OP *o)
183 return opclassnames[cc_opclass(o)];
187 make_sv_object(SV *arg, SV *sv)
192 for (iv = 0; iv < sizeof(PL_specialsv_list)/sizeof(SV*); iv++) {
193 if (sv == PL_specialsv_list[iv]) {
199 type = svclassnames[SvTYPE(sv)];
202 sv_setiv(newSVrv(arg, type), iv);
207 make_mg_object(SV *arg, MAGIC *mg)
209 sv_setiv(newSVrv(arg, "B::MAGIC"), (IV)mg);
216 SV *sstr = newSVpv("", 0);
221 sv_setpvn(sstr, "0", 1);
226 sv_catpv(sstr, "\"");
227 for (; len; len--, s++)
229 /* At least try a little for readability */
231 sv_catpv(sstr, "\\\"");
233 sv_catpv(sstr, "\\\\");
234 else if (*s >= ' ' && *s < 127) /* XXX not portable */
235 sv_catpvn(sstr, s, 1);
237 sv_catpv(sstr, "\\n");
239 sv_catpv(sstr, "\\r");
241 sv_catpv(sstr, "\\t");
243 sv_catpv(sstr, "\\a");
245 sv_catpv(sstr, "\\b");
247 sv_catpv(sstr, "\\f");
249 sv_catpv(sstr, "\\v");
252 /* no trigraph support */
253 char escbuff[5]; /* to fit backslash, 3 octals + trailing \0 */
254 /* Don't want promotion of a signed -1 char in sprintf args */
255 unsigned char c = (unsigned char) *s;
256 sprintf(escbuff, "\\%03o", c);
257 sv_catpv(sstr, escbuff);
259 /* XXX Add line breaks if string is long */
261 sv_catpv(sstr, "\"");
269 SV *sstr = newSVpv("'", 0);
271 char *s = SvPV(sv, n_a);
274 sv_catpv(sstr, "\\'");
276 sv_catpv(sstr, "\\\\");
277 else if (*s >= ' ' && *s < 127) /* XXX not portable */
278 sv_catpvn(sstr, s, 1);
280 sv_catpv(sstr, "\\n");
282 sv_catpv(sstr, "\\r");
284 sv_catpv(sstr, "\\t");
286 sv_catpv(sstr, "\\a");
288 sv_catpv(sstr, "\\b");
290 sv_catpv(sstr, "\\f");
292 sv_catpv(sstr, "\\v");
295 /* no trigraph support */
296 char escbuff[5]; /* to fit backslash, 3 octals + trailing \0 */
297 /* Don't want promotion of a signed -1 char in sprintf args */
298 unsigned char c = (unsigned char) *s;
299 sprintf(escbuff, "\\%03o", c);
300 sv_catpv(sstr, escbuff);
306 #ifdef INDIRECT_BGET_MACROS
307 void freadpv(U32 len, void *data)
309 New(666, pv.xpv_pv, len, char);
310 fread(pv.xpv_pv, 1, len, (FILE*)data);
312 pv.xpv_cur = len - 1;
315 void byteload_fh(InputStream fp)
317 struct bytestream bs;
319 bs.fgetc = (int(*) _((void*)))fgetc;
320 bs.fread = (int(*) _((char*,size_t,size_t,void*)))fread;
321 bs.freadpv = freadpv;
325 static int fgetc_fromstring(void *data)
327 char **strp = (char **)data;
331 static int fread_fromstring(char *argp, size_t elemsize, size_t nelem,
334 char **strp = (char **)data;
335 size_t len = elemsize * nelem;
337 memcpy(argp, *strp, len);
342 static void freadpv_fromstring(U32 len, void *data)
344 char **strp = (char **)data;
346 New(666, pv.xpv_pv, len, char);
347 memcpy(pv.xpv_pv, *strp, len);
349 pv.xpv_cur = len - 1;
353 void byteload_string(char *str)
355 struct bytestream bs;
357 bs.fgetc = fgetc_fromstring;
358 bs.fread = fread_fromstring;
359 bs.freadpv = freadpv_fromstring;
363 void byteload_fh(InputStream fp)
368 void byteload_string(char *str)
370 croak("Must compile with -DINDIRECT_BGET_MACROS for byteload_string");
372 #endif /* INDIRECT_BGET_MACROS */
375 walkoptree(SV *opsv, char *method)
381 croak("opsv is not a reference");
382 opsv = sv_mortalcopy(opsv);
383 o = (OP*)SvIV((SV*)SvRV(opsv));
384 if (walkoptree_debug) {
388 perl_call_method("walkoptree_debug", G_DISCARD);
393 perl_call_method(method, G_DISCARD);
394 if (o && (o->op_flags & OPf_KIDS)) {
396 for (kid = ((UNOP*)o)->op_first; kid; kid = kid->op_sibling) {
397 /* Use the same opsv. Rely on methods not to mess it up. */
398 sv_setiv(newSVrv(opsv, cc_opclassname(kid)), (IV)kid);
399 walkoptree(opsv, method);
405 typedef UNOP *B__UNOP;
406 typedef BINOP *B__BINOP;
407 typedef LOGOP *B__LOGOP;
408 typedef CONDOP *B__CONDOP;
409 typedef LISTOP *B__LISTOP;
410 typedef PMOP *B__PMOP;
411 typedef SVOP *B__SVOP;
412 typedef GVOP *B__GVOP;
413 typedef PVOP *B__PVOP;
414 typedef LOOP *B__LOOP;
431 typedef MAGIC *B__MAGIC;
433 MODULE = B PACKAGE = B PREFIX = B_
440 #define B_main_cv() PL_main_cv
441 #define B_init_av() PL_initav
442 #define B_main_root() PL_main_root
443 #define B_main_start() PL_main_start
444 #define B_comppadlist() (PL_main_cv ? CvPADLIST(PL_main_cv) : CvPADLIST(PL_compcv))
445 #define B_sv_undef() &PL_sv_undef
446 #define B_sv_yes() &PL_sv_yes
447 #define B_sv_no() &PL_sv_no
473 MODULE = B PACKAGE = B
477 walkoptree(opsv, method)
482 walkoptree_debug(...)
484 RETVAL = walkoptree_debug;
485 if (items > 0 && SvTRUE(ST(1)))
486 walkoptree_debug = 1;
503 #define address(sv) (IV)sv
514 croak("argument is not a reference");
515 RETVAL = (SV*)SvRV(sv);
523 ST(0) = sv_newmortal();
524 if (opnum >= 0 && opnum < PL_maxo) {
525 sv_setpvn(ST(0), "pp_", 3);
526 sv_catpv(ST(0), op_name[opnum]);
536 char hexhash[11]; /* must fit "0xffffffff" plus trailing \0 */
539 hash = hash * 33 + *s++;
540 sprintf(hexhash, "0x%x", hash);
541 ST(0) = sv_2mortal(newSVpv(hexhash, 0));
543 #define cast_I32(foo) (I32)foo
566 STRLEN len = strlen(PL_threadsv_names);
569 for (i = 0; i < len; i++)
570 PUSHs(sv_2mortal(newSVpv(&PL_threadsv_names[i], 1)));
574 #define OP_next(o) o->op_next
575 #define OP_sibling(o) o->op_sibling
576 #define OP_desc(o) op_desc[o->op_type]
577 #define OP_targ(o) o->op_targ
578 #define OP_type(o) o->op_type
579 #define OP_seq(o) o->op_seq
580 #define OP_flags(o) o->op_flags
581 #define OP_private(o) o->op_private
583 MODULE = B PACKAGE = B::OP PREFIX = OP_
597 ST(0) = sv_newmortal();
598 sv_setpvn(ST(0), "pp_", 3);
599 sv_catpv(ST(0), op_name[o->op_type]);
625 #define UNOP_first(o) o->op_first
627 MODULE = B PACKAGE = B::UNOP PREFIX = UNOP_
633 #define BINOP_last(o) o->op_last
635 MODULE = B PACKAGE = B::BINOP PREFIX = BINOP_
641 #define LOGOP_other(o) o->op_other
643 MODULE = B PACKAGE = B::LOGOP PREFIX = LOGOP_
649 #define CONDOP_true(o) o->op_true
650 #define CONDOP_false(o) o->op_false
652 MODULE = B PACKAGE = B::CONDOP PREFIX = CONDOP_
662 #define LISTOP_children(o) o->op_children
664 MODULE = B PACKAGE = B::LISTOP PREFIX = LISTOP_
670 #define PMOP_pmreplroot(o) o->op_pmreplroot
671 #define PMOP_pmreplstart(o) o->op_pmreplstart
672 #define PMOP_pmnext(o) o->op_pmnext
673 #define PMOP_pmregexp(o) o->op_pmregexp
674 #define PMOP_pmflags(o) o->op_pmflags
675 #define PMOP_pmpermflags(o) o->op_pmpermflags
677 MODULE = B PACKAGE = B::PMOP PREFIX = PMOP_
684 ST(0) = sv_newmortal();
685 root = o->op_pmreplroot;
686 /* OP_PUSHRE stores an SV* instead of an OP* in op_pmreplroot */
687 if (o->op_type == OP_PUSHRE) {
688 sv_setiv(newSVrv(ST(0), root ?
689 svclassnames[SvTYPE((SV*)root)] : "B::SV"),
693 sv_setiv(newSVrv(ST(0), cc_opclassname(root)), (IV)root);
715 REGEXP * rx = NO_INIT
717 ST(0) = sv_newmortal();
720 sv_setpvn(ST(0), rx->precomp, rx->prelen);
722 #define SVOP_sv(o) o->op_sv
724 MODULE = B PACKAGE = B::SVOP PREFIX = SVOP_
731 #define GVOP_gv(o) o->op_gv
733 MODULE = B PACKAGE = B::GVOP PREFIX = GVOP_
740 MODULE = B PACKAGE = B::PVOP PREFIX = PVOP_
747 * OP_TRANS uses op_pv to point to a table of 256 shorts
748 * whereas other PVOPs point to a null terminated string.
750 ST(0) = sv_2mortal(newSVpv(o->op_pv, (o->op_type == OP_TRANS) ?
751 256 * sizeof(short) : 0));
753 #define LOOP_redoop(o) o->op_redoop
754 #define LOOP_nextop(o) o->op_nextop
755 #define LOOP_lastop(o) o->op_lastop
757 MODULE = B PACKAGE = B::LOOP PREFIX = LOOP_
772 #define COP_label(o) o->cop_label
773 #define COP_stash(o) o->cop_stash
774 #define COP_filegv(o) o->cop_filegv
775 #define COP_cop_seq(o) o->cop_seq
776 #define COP_arybase(o) o->cop_arybase
777 #define COP_line(o) o->cop_line
779 MODULE = B PACKAGE = B::COP PREFIX = COP_
805 MODULE = B PACKAGE = B::SV PREFIX = Sv
815 MODULE = B PACKAGE = B::IV PREFIX = Sv
825 MODULE = B PACKAGE = B::IV
827 #define needs64bits(sv) ((I32)SvIVX(sv) != SvIVX(sv))
837 if (sizeof(IV) == 8) {
841 * The following way of spelling 32 is to stop compilers on
842 * 32-bit architectures from moaning about the shift count
843 * being >= the width of the type. Such architectures don't
844 * reach this code anyway (unless sizeof(IV) > 8 but then
845 * everything else breaks too so I'm not fussed at the moment).
847 wp[0] = htonl(((U32)iv) >> (sizeof(IV)*4));
848 wp[1] = htonl(iv & 0xffffffff);
849 ST(0) = sv_2mortal(newSVpv((char *)wp, 8));
851 U32 w = htonl((U32)SvIVX(sv));
852 ST(0) = sv_2mortal(newSVpv((char *)&w, 4));
855 MODULE = B PACKAGE = B::NV PREFIX = Sv
865 MODULE = B PACKAGE = B::RV PREFIX = Sv
871 MODULE = B PACKAGE = B::PV PREFIX = Sv
877 ST(0) = sv_newmortal();
878 sv_setpvn(ST(0), SvPVX(sv), SvCUR(sv));
880 MODULE = B PACKAGE = B::PVMG PREFIX = Sv
887 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic)
888 XPUSHs(make_mg_object(sv_newmortal(), mg));
890 MODULE = B PACKAGE = B::PVMG
896 #define MgMOREMAGIC(mg) mg->mg_moremagic
897 #define MgPRIVATE(mg) mg->mg_private
898 #define MgTYPE(mg) mg->mg_type
899 #define MgFLAGS(mg) mg->mg_flags
900 #define MgOBJ(mg) mg->mg_obj
902 MODULE = B PACKAGE = B::MAGIC PREFIX = Mg
928 ST(0) = sv_newmortal();
930 sv_setpvn(ST(0), mg->mg_ptr, mg->mg_len);
932 MODULE = B PACKAGE = B::PVLV PREFIX = Lv
950 MODULE = B PACKAGE = B::BM PREFIX = Bm
971 /* Boyer-Moore table is just after string and its safety-margin \0 */
972 ST(0) = sv_2mortal(newSVpv(str + len + 1, 256));
974 MODULE = B PACKAGE = B::GV PREFIX = Gv
980 ST(0) = sv_2mortal(newSVpv(GvNAME(gv), GvNAMELEN(gv)));
1026 MODULE = B PACKAGE = B::GV
1036 MODULE = B PACKAGE = B::IO PREFIX = Io
1082 MODULE = B PACKAGE = B::IO
1092 MODULE = B PACKAGE = B::AV PREFIX = Av
1102 #define AvOFF(av) ((XPVAV*)SvANY(av))->xof_off
1112 if (AvFILL(av) >= 0) {
1113 SV **svp = AvARRAY(av);
1115 for (i = 0; i <= AvFILL(av); i++)
1116 XPUSHs(make_sv_object(sv_newmortal(), svp[i]));
1119 MODULE = B PACKAGE = B::AV
1125 MODULE = B PACKAGE = B::CV PREFIX = Cv
1163 ST(0) = sv_2mortal(newSViv((IV)CvXSUB(cv)));
1170 ST(0) = sv_2mortal(newSViv(CvXSUBANY(cv).any_iv));
1172 MODULE = B PACKAGE = B::CV
1179 MODULE = B PACKAGE = B::HV PREFIX = Hv
1209 if (HvKEYS(hv) > 0) {
1213 (void)hv_iterinit(hv);
1214 EXTEND(sp, HvKEYS(hv) * 2);
1215 while (sv = hv_iternextsv(hv, &key, &len)) {
1216 PUSHs(newSVpv(key, len));
1217 PUSHs(make_sv_object(sv_newmortal(), sv));