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 */
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) {
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 PL_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);
526 ST(0) = sv_newmortal();
527 if (strncmp(name,"pp_",3) == 0)
529 for (i = 0; i < PL_maxo; i++)
531 if (strcmp(name, PL_op_name[i]) == 0)
537 sv_setiv(ST(0),result);
544 ST(0) = sv_newmortal();
545 if (opnum >= 0 && opnum < PL_maxo) {
546 sv_setpvn(ST(0), "pp_", 3);
547 sv_catpv(ST(0), PL_op_name[opnum]);
557 char hexhash[19]; /* must fit "0xffffffff" plus trailing \0 */
559 PERL_HASH(hash, s, len);
560 sprintf(hexhash, "0x%x", hash);
561 ST(0) = sv_2mortal(newSVpv(hexhash, 0));
563 #define cast_I32(foo) (I32)foo
586 STRLEN len = strlen(PL_threadsv_names);
589 for (i = 0; i < len; i++)
590 PUSHs(sv_2mortal(newSVpv(&PL_threadsv_names[i], 1)));
594 #define OP_next(o) o->op_next
595 #define OP_sibling(o) o->op_sibling
596 #define OP_desc(o) PL_op_desc[o->op_type]
597 #define OP_targ(o) o->op_targ
598 #define OP_type(o) o->op_type
599 #define OP_seq(o) o->op_seq
600 #define OP_flags(o) o->op_flags
601 #define OP_private(o) o->op_private
603 MODULE = B PACKAGE = B::OP PREFIX = OP_
617 ST(0) = sv_newmortal();
618 sv_setpvn(ST(0), "pp_", 3);
619 sv_catpv(ST(0), PL_op_name[o->op_type]);
645 #define UNOP_first(o) o->op_first
647 MODULE = B PACKAGE = B::UNOP PREFIX = UNOP_
653 #define BINOP_last(o) o->op_last
655 MODULE = B PACKAGE = B::BINOP PREFIX = BINOP_
661 #define LOGOP_other(o) o->op_other
663 MODULE = B PACKAGE = B::LOGOP PREFIX = LOGOP_
669 #define CONDOP_true(o) o->op_true
670 #define CONDOP_false(o) o->op_false
672 MODULE = B PACKAGE = B::CONDOP PREFIX = CONDOP_
682 #define LISTOP_children(o) o->op_children
684 MODULE = B PACKAGE = B::LISTOP PREFIX = LISTOP_
690 #define PMOP_pmreplroot(o) o->op_pmreplroot
691 #define PMOP_pmreplstart(o) o->op_pmreplstart
692 #define PMOP_pmnext(o) o->op_pmnext
693 #define PMOP_pmregexp(o) o->op_pmregexp
694 #define PMOP_pmflags(o) o->op_pmflags
695 #define PMOP_pmpermflags(o) o->op_pmpermflags
697 MODULE = B PACKAGE = B::PMOP PREFIX = PMOP_
704 ST(0) = sv_newmortal();
705 root = o->op_pmreplroot;
706 /* OP_PUSHRE stores an SV* instead of an OP* in op_pmreplroot */
707 if (o->op_type == OP_PUSHRE) {
708 sv_setiv(newSVrv(ST(0), root ?
709 svclassnames[SvTYPE((SV*)root)] : "B::SV"),
713 sv_setiv(newSVrv(ST(0), cc_opclassname(root)), (IV)root);
735 REGEXP * rx = NO_INIT
737 ST(0) = sv_newmortal();
740 sv_setpvn(ST(0), rx->precomp, rx->prelen);
742 #define SVOP_sv(o) o->op_sv
744 MODULE = B PACKAGE = B::SVOP PREFIX = SVOP_
751 #define GVOP_gv(o) o->op_gv
753 MODULE = B PACKAGE = B::GVOP PREFIX = GVOP_
760 MODULE = B PACKAGE = B::PVOP PREFIX = PVOP_
767 * OP_TRANS uses op_pv to point to a table of 256 shorts
768 * whereas other PVOPs point to a null terminated string.
770 ST(0) = sv_2mortal(newSVpv(o->op_pv, (o->op_type == OP_TRANS) ?
771 256 * sizeof(short) : 0));
773 #define LOOP_redoop(o) o->op_redoop
774 #define LOOP_nextop(o) o->op_nextop
775 #define LOOP_lastop(o) o->op_lastop
777 MODULE = B PACKAGE = B::LOOP PREFIX = LOOP_
792 #define COP_label(o) o->cop_label
793 #define COP_stash(o) o->cop_stash
794 #define COP_filegv(o) o->cop_filegv
795 #define COP_cop_seq(o) o->cop_seq
796 #define COP_arybase(o) o->cop_arybase
797 #define COP_line(o) o->cop_line
799 MODULE = B PACKAGE = B::COP PREFIX = COP_
825 MODULE = B PACKAGE = B::SV PREFIX = Sv
835 MODULE = B PACKAGE = B::IV PREFIX = Sv
845 MODULE = B PACKAGE = B::IV
847 #define needs64bits(sv) ((I32)SvIVX(sv) != SvIVX(sv))
857 if (sizeof(IV) == 8) {
861 * The following way of spelling 32 is to stop compilers on
862 * 32-bit architectures from moaning about the shift count
863 * being >= the width of the type. Such architectures don't
864 * reach this code anyway (unless sizeof(IV) > 8 but then
865 * everything else breaks too so I'm not fussed at the moment).
867 wp[0] = htonl(((U32)iv) >> (sizeof(IV)*4));
868 wp[1] = htonl(iv & 0xffffffff);
869 ST(0) = sv_2mortal(newSVpv((char *)wp, 8));
871 U32 w = htonl((U32)SvIVX(sv));
872 ST(0) = sv_2mortal(newSVpv((char *)&w, 4));
875 MODULE = B PACKAGE = B::NV PREFIX = Sv
885 MODULE = B PACKAGE = B::RV PREFIX = Sv
891 MODULE = B PACKAGE = B::PV PREFIX = Sv
897 ST(0) = sv_newmortal();
898 sv_setpvn(ST(0), SvPVX(sv), SvCUR(sv));
900 MODULE = B PACKAGE = B::PVMG PREFIX = Sv
907 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic)
908 XPUSHs(make_mg_object(sv_newmortal(), mg));
910 MODULE = B PACKAGE = B::PVMG
916 #define MgMOREMAGIC(mg) mg->mg_moremagic
917 #define MgPRIVATE(mg) mg->mg_private
918 #define MgTYPE(mg) mg->mg_type
919 #define MgFLAGS(mg) mg->mg_flags
920 #define MgOBJ(mg) mg->mg_obj
921 #define MgLENGTH(mg) mg->mg_len
923 MODULE = B PACKAGE = B::MAGIC PREFIX = Mg
953 ST(0) = sv_newmortal();
955 if (mg->mg_len >= 0){
956 sv_setpvn(ST(0), mg->mg_ptr, mg->mg_len);
958 if (mg->mg_len == HEf_SVKEY)
959 sv_setsv(ST(0),newRV((SV*)mg->mg_ptr));
963 MODULE = B PACKAGE = B::PVLV PREFIX = Lv
981 MODULE = B PACKAGE = B::BM PREFIX = Bm
1001 str = SvPV(sv, len);
1002 /* Boyer-Moore table is just after string and its safety-margin \0 */
1003 ST(0) = sv_2mortal(newSVpv(str + len + 1, 256));
1005 MODULE = B PACKAGE = B::GV PREFIX = Gv
1011 ST(0) = sv_2mortal(newSVpv(GvNAME(gv), GvNAMELEN(gv)));
1057 MODULE = B PACKAGE = B::GV
1067 MODULE = B PACKAGE = B::IO PREFIX = Io
1113 MODULE = B PACKAGE = B::IO
1123 MODULE = B PACKAGE = B::AV PREFIX = Av
1133 #define AvOFF(av) ((XPVAV*)SvANY(av))->xof_off
1143 if (AvFILL(av) >= 0) {
1144 SV **svp = AvARRAY(av);
1146 for (i = 0; i <= AvFILL(av); i++)
1147 XPUSHs(make_sv_object(sv_newmortal(), svp[i]));
1150 MODULE = B PACKAGE = B::AV
1156 MODULE = B PACKAGE = B::CV PREFIX = Cv
1194 ST(0) = sv_2mortal(newSViv((IV)CvXSUB(cv)));
1201 ST(0) = sv_2mortal(newSViv(CvXSUBANY(cv).any_iv));
1203 MODULE = B PACKAGE = B::CV
1210 MODULE = B PACKAGE = B::HV PREFIX = Hv
1240 if (HvKEYS(hv) > 0) {
1244 (void)hv_iterinit(hv);
1245 EXTEND(sp, HvKEYS(hv) * 2);
1246 while (sv = hv_iternextsv(hv, &key, &len)) {
1247 PUSHs(newSVpv(key, len));
1248 PUSHs(make_sv_object(sv_newmortal(), sv));