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_
439 HV *stash = gv_stashpvn("B", 1, TRUE);
440 AV *export_ok = perl_get_av("B::EXPORT_OK",TRUE);
445 #define B_main_cv() PL_main_cv
446 #define B_init_av() PL_initav
447 #define B_main_root() PL_main_root
448 #define B_main_start() PL_main_start
449 #define B_comppadlist() (PL_main_cv ? CvPADLIST(PL_main_cv) : CvPADLIST(PL_compcv))
450 #define B_sv_undef() &PL_sv_undef
451 #define B_sv_yes() &PL_sv_yes
452 #define B_sv_no() &PL_sv_no
478 MODULE = B PACKAGE = B
482 walkoptree(opsv, method)
487 walkoptree_debug(...)
489 RETVAL = walkoptree_debug;
490 if (items > 0 && SvTRUE(ST(1)))
491 walkoptree_debug = 1;
508 #define address(sv) (IV)sv
519 croak("argument is not a reference");
520 RETVAL = (SV*)SvRV(sv);
531 ST(0) = sv_newmortal();
532 if (strncmp(name,"pp_",3) == 0)
534 for (i = 0; i < PL_maxo; i++)
536 if (strcmp(name, PL_op_name[i]) == 0)
542 sv_setiv(ST(0),result);
549 ST(0) = sv_newmortal();
550 if (opnum >= 0 && opnum < PL_maxo) {
551 sv_setpvn(ST(0), "pp_", 3);
552 sv_catpv(ST(0), PL_op_name[opnum]);
562 char hexhash[19]; /* must fit "0xffffffff" plus trailing \0 */
564 PERL_HASH(hash, s, len);
565 sprintf(hexhash, "0x%x", hash);
566 ST(0) = sv_2mortal(newSVpv(hexhash, 0));
568 #define cast_I32(foo) (I32)foo
591 STRLEN len = strlen(PL_threadsv_names);
594 for (i = 0; i < len; i++)
595 PUSHs(sv_2mortal(newSVpv(&PL_threadsv_names[i], 1)));
599 #define OP_next(o) o->op_next
600 #define OP_sibling(o) o->op_sibling
601 #define OP_desc(o) PL_op_desc[o->op_type]
602 #define OP_targ(o) o->op_targ
603 #define OP_type(o) o->op_type
604 #define OP_seq(o) o->op_seq
605 #define OP_flags(o) o->op_flags
606 #define OP_private(o) o->op_private
608 MODULE = B PACKAGE = B::OP PREFIX = OP_
622 ST(0) = sv_newmortal();
623 sv_setpvn(ST(0), "pp_", 3);
624 sv_catpv(ST(0), PL_op_name[o->op_type]);
650 #define UNOP_first(o) o->op_first
652 MODULE = B PACKAGE = B::UNOP PREFIX = UNOP_
658 #define BINOP_last(o) o->op_last
660 MODULE = B PACKAGE = B::BINOP PREFIX = BINOP_
666 #define LOGOP_other(o) o->op_other
668 MODULE = B PACKAGE = B::LOGOP PREFIX = LOGOP_
674 #define CONDOP_true(o) o->op_true
675 #define CONDOP_false(o) o->op_false
677 MODULE = B PACKAGE = B::CONDOP PREFIX = CONDOP_
687 #define LISTOP_children(o) o->op_children
689 MODULE = B PACKAGE = B::LISTOP PREFIX = LISTOP_
695 #define PMOP_pmreplroot(o) o->op_pmreplroot
696 #define PMOP_pmreplstart(o) o->op_pmreplstart
697 #define PMOP_pmnext(o) o->op_pmnext
698 #define PMOP_pmregexp(o) o->op_pmregexp
699 #define PMOP_pmflags(o) o->op_pmflags
700 #define PMOP_pmpermflags(o) o->op_pmpermflags
702 MODULE = B PACKAGE = B::PMOP PREFIX = PMOP_
709 ST(0) = sv_newmortal();
710 root = o->op_pmreplroot;
711 /* OP_PUSHRE stores an SV* instead of an OP* in op_pmreplroot */
712 if (o->op_type == OP_PUSHRE) {
713 sv_setiv(newSVrv(ST(0), root ?
714 svclassnames[SvTYPE((SV*)root)] : "B::SV"),
718 sv_setiv(newSVrv(ST(0), cc_opclassname(root)), (IV)root);
740 REGEXP * rx = NO_INIT
742 ST(0) = sv_newmortal();
745 sv_setpvn(ST(0), rx->precomp, rx->prelen);
747 #define SVOP_sv(o) o->op_sv
749 MODULE = B PACKAGE = B::SVOP PREFIX = SVOP_
756 #define GVOP_gv(o) o->op_gv
758 MODULE = B PACKAGE = B::GVOP PREFIX = GVOP_
765 MODULE = B PACKAGE = B::PVOP PREFIX = PVOP_
772 * OP_TRANS uses op_pv to point to a table of 256 shorts
773 * whereas other PVOPs point to a null terminated string.
775 ST(0) = sv_2mortal(newSVpv(o->op_pv, (o->op_type == OP_TRANS) ?
776 256 * sizeof(short) : 0));
778 #define LOOP_redoop(o) o->op_redoop
779 #define LOOP_nextop(o) o->op_nextop
780 #define LOOP_lastop(o) o->op_lastop
782 MODULE = B PACKAGE = B::LOOP PREFIX = LOOP_
797 #define COP_label(o) o->cop_label
798 #define COP_stash(o) o->cop_stash
799 #define COP_filegv(o) o->cop_filegv
800 #define COP_cop_seq(o) o->cop_seq
801 #define COP_arybase(o) o->cop_arybase
802 #define COP_line(o) o->cop_line
804 MODULE = B PACKAGE = B::COP PREFIX = COP_
830 MODULE = B PACKAGE = B::SV PREFIX = Sv
840 MODULE = B PACKAGE = B::IV PREFIX = Sv
850 MODULE = B PACKAGE = B::IV
852 #define needs64bits(sv) ((I32)SvIVX(sv) != SvIVX(sv))
862 if (sizeof(IV) == 8) {
866 * The following way of spelling 32 is to stop compilers on
867 * 32-bit architectures from moaning about the shift count
868 * being >= the width of the type. Such architectures don't
869 * reach this code anyway (unless sizeof(IV) > 8 but then
870 * everything else breaks too so I'm not fussed at the moment).
872 wp[0] = htonl(((U32)iv) >> (sizeof(IV)*4));
873 wp[1] = htonl(iv & 0xffffffff);
874 ST(0) = sv_2mortal(newSVpv((char *)wp, 8));
876 U32 w = htonl((U32)SvIVX(sv));
877 ST(0) = sv_2mortal(newSVpv((char *)&w, 4));
880 MODULE = B PACKAGE = B::NV PREFIX = Sv
890 MODULE = B PACKAGE = B::RV PREFIX = Sv
896 MODULE = B PACKAGE = B::PV PREFIX = Sv
902 ST(0) = sv_newmortal();
903 sv_setpvn(ST(0), SvPVX(sv), SvCUR(sv));
905 MODULE = B PACKAGE = B::PVMG PREFIX = Sv
912 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic)
913 XPUSHs(make_mg_object(sv_newmortal(), mg));
915 MODULE = B PACKAGE = B::PVMG
921 #define MgMOREMAGIC(mg) mg->mg_moremagic
922 #define MgPRIVATE(mg) mg->mg_private
923 #define MgTYPE(mg) mg->mg_type
924 #define MgFLAGS(mg) mg->mg_flags
925 #define MgOBJ(mg) mg->mg_obj
926 #define MgLENGTH(mg) mg->mg_len
928 MODULE = B PACKAGE = B::MAGIC PREFIX = Mg
958 ST(0) = sv_newmortal();
960 if (mg->mg_len >= 0){
961 sv_setpvn(ST(0), mg->mg_ptr, mg->mg_len);
963 if (mg->mg_len == HEf_SVKEY)
964 sv_setsv(ST(0),newRV((SV*)mg->mg_ptr));
968 MODULE = B PACKAGE = B::PVLV PREFIX = Lv
986 MODULE = B PACKAGE = B::BM PREFIX = Bm
1003 STRLEN len = NO_INIT
1004 char * str = NO_INIT
1006 str = SvPV(sv, len);
1007 /* Boyer-Moore table is just after string and its safety-margin \0 */
1008 ST(0) = sv_2mortal(newSVpv(str + len + 1, 256));
1010 MODULE = B PACKAGE = B::GV PREFIX = Gv
1016 ST(0) = sv_2mortal(newSVpv(GvNAME(gv), GvNAMELEN(gv)));
1062 MODULE = B PACKAGE = B::GV
1072 MODULE = B PACKAGE = B::IO PREFIX = Io
1118 MODULE = B PACKAGE = B::IO
1128 MODULE = B PACKAGE = B::AV PREFIX = Av
1138 #define AvOFF(av) ((XPVAV*)SvANY(av))->xof_off
1148 if (AvFILL(av) >= 0) {
1149 SV **svp = AvARRAY(av);
1151 for (i = 0; i <= AvFILL(av); i++)
1152 XPUSHs(make_sv_object(sv_newmortal(), svp[i]));
1155 MODULE = B PACKAGE = B::AV
1161 MODULE = B PACKAGE = B::CV PREFIX = Cv
1199 ST(0) = sv_2mortal(newSViv((IV)CvXSUB(cv)));
1206 ST(0) = sv_2mortal(newSViv(CvXSUBANY(cv).any_iv));
1208 MODULE = B PACKAGE = B::CV
1215 MODULE = B PACKAGE = B::HV PREFIX = Hv
1245 if (HvKEYS(hv) > 0) {
1249 (void)hv_iterinit(hv);
1250 EXTEND(sp, HvKEYS(hv) * 2);
1251 while (sv = hv_iternextsv(hv, &key, &len)) {
1252 PUSHs(newSVpv(key, len));
1253 PUSHs(make_sv_object(sv_newmortal(), sv));