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) {
126 case OA_PVOP_OR_SVOP:
128 * Character translations (tr///) are usually a PVOP, keeping a
129 * pointer to a table of shorts used to look up translations.
130 * Under utf8, however, a simple table isn't practical; instead,
131 * the OP is an SVOP, and the SV is a reference to a swash
132 * (i.e., an RV pointing to an HV).
134 return (o->op_private & (OPpTRANS_TO_UTF|OPpTRANS_FROM_UTF))
135 ? OPc_SVOP : OPc_PVOP;
143 case OA_BASEOP_OR_UNOP:
145 * UNI(OP_foo) in toke.c returns token UNI or FUNC1 depending on
146 * whether parens were seen. perly.y uses OPf_SPECIAL to
147 * signal whether a BASEOP had empty parens or none.
148 * Some other UNOPs are created later, though, so the best
149 * test is OPf_KIDS, which is set in newUNOP.
151 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
155 * The file stat OPs are created via UNI(OP_foo) in toke.c but use
156 * the OPf_REF flag to distinguish between OP types instead of the
157 * usual OPf_SPECIAL flag. As usual, if OPf_KIDS is set, then we
158 * return OPc_UNOP so that walkoptree can find our children. If
159 * OPf_KIDS is not set then we check OPf_REF. Without OPf_REF set
160 * (no argument to the operator) it's an OP; with OPf_REF set it's
161 * a GVOP (and op_gv is the GV for the filehandle argument).
163 return ((o->op_flags & OPf_KIDS) ? OPc_UNOP :
164 (o->op_flags & OPf_REF) ? OPc_GVOP : OPc_BASEOP);
168 * next, last, redo, dump and goto use OPf_SPECIAL to indicate that a
169 * label was omitted (in which case it's a BASEOP) or else a term was
170 * seen. In this last case, all except goto are definitely PVOP but
171 * goto is either a PVOP (with an ordinary constant label), an UNOP
172 * with OPf_STACKED (with a non-constant non-sub) or an UNOP for
173 * OP_REFGEN (with goto &sub) in which case OPf_STACKED also seems to
176 if (o->op_flags & OPf_STACKED)
178 else if (o->op_flags & OPf_SPECIAL)
183 warn("can't determine class of operator %s, assuming BASEOP\n",
184 PL_op_name[o->op_type]);
189 cc_opclassname(OP *o)
191 return opclassnames[cc_opclass(o)];
195 make_sv_object(SV *arg, SV *sv)
200 for (iv = 0; iv < sizeof(PL_specialsv_list)/sizeof(SV*); iv++) {
201 if (sv == PL_specialsv_list[iv]) {
207 type = svclassnames[SvTYPE(sv)];
210 sv_setiv(newSVrv(arg, type), iv);
215 make_mg_object(SV *arg, MAGIC *mg)
217 sv_setiv(newSVrv(arg, "B::MAGIC"), (IV)mg);
224 SV *sstr = newSVpvn("", 0);
229 sv_setpvn(sstr, "0", 1);
234 sv_catpv(sstr, "\"");
235 for (; len; len--, s++)
237 /* At least try a little for readability */
239 sv_catpv(sstr, "\\\"");
241 sv_catpv(sstr, "\\\\");
242 else if (*s >= ' ' && *s < 127) /* XXX not portable */
243 sv_catpvn(sstr, s, 1);
245 sv_catpv(sstr, "\\n");
247 sv_catpv(sstr, "\\r");
249 sv_catpv(sstr, "\\t");
251 sv_catpv(sstr, "\\a");
253 sv_catpv(sstr, "\\b");
255 sv_catpv(sstr, "\\f");
257 sv_catpv(sstr, "\\v");
260 /* no trigraph support */
261 char escbuff[5]; /* to fit backslash, 3 octals + trailing \0 */
262 /* Don't want promotion of a signed -1 char in sprintf args */
263 unsigned char c = (unsigned char) *s;
264 sprintf(escbuff, "\\%03o", c);
265 sv_catpv(sstr, escbuff);
267 /* XXX Add line breaks if string is long */
269 sv_catpv(sstr, "\"");
277 SV *sstr = newSVpvn("'", 1);
279 char *s = SvPV(sv, n_a);
282 sv_catpv(sstr, "\\'");
284 sv_catpv(sstr, "\\\\");
285 else if (*s >= ' ' && *s < 127) /* XXX not portable */
286 sv_catpvn(sstr, s, 1);
288 sv_catpv(sstr, "\\n");
290 sv_catpv(sstr, "\\r");
292 sv_catpv(sstr, "\\t");
294 sv_catpv(sstr, "\\a");
296 sv_catpv(sstr, "\\b");
298 sv_catpv(sstr, "\\f");
300 sv_catpv(sstr, "\\v");
303 /* no trigraph support */
304 char escbuff[5]; /* to fit backslash, 3 octals + trailing \0 */
305 /* Don't want promotion of a signed -1 char in sprintf args */
306 unsigned char c = (unsigned char) *s;
307 sprintf(escbuff, "\\%03o", c);
308 sv_catpv(sstr, escbuff);
314 #ifdef INDIRECT_BGET_MACROS
315 void freadpv(U32 len, void *data)
317 New(666, pv.xpv_pv, len, char);
318 fread(pv.xpv_pv, 1, len, (FILE*)data);
320 pv.xpv_cur = len - 1;
323 void byteload_fh(InputStream fp)
325 struct bytestream bs;
327 bs.fgetc = (int(*) _((void*)))fgetc;
328 bs.fread = (int(*) _((char*,size_t,size_t,void*)))fread;
329 bs.freadpv = freadpv;
333 static int fgetc_fromstring(void *data)
335 char **strp = (char **)data;
339 static int fread_fromstring(char *argp, size_t elemsize, size_t nelem,
342 char **strp = (char **)data;
343 size_t len = elemsize * nelem;
345 memcpy(argp, *strp, len);
350 static void freadpv_fromstring(U32 len, void *data)
352 char **strp = (char **)data;
354 New(666, pv.xpv_pv, len, char);
355 memcpy(pv.xpv_pv, *strp, len);
357 pv.xpv_cur = len - 1;
361 void byteload_string(char *str)
363 struct bytestream bs;
365 bs.fgetc = fgetc_fromstring;
366 bs.fread = fread_fromstring;
367 bs.freadpv = freadpv_fromstring;
371 void byteload_fh(InputStream fp)
376 void byteload_string(char *str)
378 croak("Must compile with -DINDIRECT_BGET_MACROS for byteload_string");
380 #endif /* INDIRECT_BGET_MACROS */
383 walkoptree(SV *opsv, char *method)
389 croak("opsv is not a reference");
390 opsv = sv_mortalcopy(opsv);
391 o = (OP*)SvIV((SV*)SvRV(opsv));
392 if (walkoptree_debug) {
396 perl_call_method("walkoptree_debug", G_DISCARD);
401 perl_call_method(method, G_DISCARD);
402 if (o && (o->op_flags & OPf_KIDS)) {
404 for (kid = ((UNOP*)o)->op_first; kid; kid = kid->op_sibling) {
405 /* Use the same opsv. Rely on methods not to mess it up. */
406 sv_setiv(newSVrv(opsv, cc_opclassname(kid)), (IV)kid);
407 walkoptree(opsv, method);
413 typedef UNOP *B__UNOP;
414 typedef BINOP *B__BINOP;
415 typedef LOGOP *B__LOGOP;
416 typedef CONDOP *B__CONDOP;
417 typedef LISTOP *B__LISTOP;
418 typedef PMOP *B__PMOP;
419 typedef SVOP *B__SVOP;
420 typedef GVOP *B__GVOP;
421 typedef PVOP *B__PVOP;
422 typedef LOOP *B__LOOP;
439 typedef MAGIC *B__MAGIC;
441 MODULE = B PACKAGE = B PREFIX = B_
447 HV *stash = gv_stashpvn("B", 1, TRUE);
448 AV *export_ok = perl_get_av("B::EXPORT_OK",TRUE);
453 #define B_main_cv() PL_main_cv
454 #define B_init_av() PL_initav
455 #define B_main_root() PL_main_root
456 #define B_main_start() PL_main_start
457 #define B_comppadlist() (PL_main_cv ? CvPADLIST(PL_main_cv) : CvPADLIST(PL_compcv))
458 #define B_sv_undef() &PL_sv_undef
459 #define B_sv_yes() &PL_sv_yes
460 #define B_sv_no() &PL_sv_no
486 MODULE = B PACKAGE = B
490 walkoptree(opsv, method)
495 walkoptree_debug(...)
497 RETVAL = walkoptree_debug;
498 if (items > 0 && SvTRUE(ST(1)))
499 walkoptree_debug = 1;
516 #define address(sv) (IV)sv
527 croak("argument is not a reference");
528 RETVAL = (SV*)SvRV(sv);
539 ST(0) = sv_newmortal();
540 if (strncmp(name,"pp_",3) == 0)
542 for (i = 0; i < PL_maxo; i++)
544 if (strcmp(name, PL_op_name[i]) == 0)
550 sv_setiv(ST(0),result);
557 ST(0) = sv_newmortal();
558 if (opnum >= 0 && opnum < PL_maxo) {
559 sv_setpvn(ST(0), "pp_", 3);
560 sv_catpv(ST(0), PL_op_name[opnum]);
570 char hexhash[19]; /* must fit "0xffffffff" plus trailing \0 */
572 PERL_HASH(hash, s, len);
573 sprintf(hexhash, "0x%x", hash);
574 ST(0) = sv_2mortal(newSVpv(hexhash, 0));
576 #define cast_I32(foo) (I32)foo
599 STRLEN len = strlen(PL_threadsv_names);
602 for (i = 0; i < len; i++)
603 PUSHs(sv_2mortal(newSVpvn(&PL_threadsv_names[i], 1)));
607 #define OP_next(o) o->op_next
608 #define OP_sibling(o) o->op_sibling
609 #define OP_desc(o) PL_op_desc[o->op_type]
610 #define OP_targ(o) o->op_targ
611 #define OP_type(o) o->op_type
612 #define OP_seq(o) o->op_seq
613 #define OP_flags(o) o->op_flags
614 #define OP_private(o) o->op_private
616 MODULE = B PACKAGE = B::OP PREFIX = OP_
630 ST(0) = sv_newmortal();
631 sv_setpvn(ST(0), "pp_", 3);
632 sv_catpv(ST(0), PL_op_name[o->op_type]);
658 #define UNOP_first(o) o->op_first
660 MODULE = B PACKAGE = B::UNOP PREFIX = UNOP_
666 #define BINOP_last(o) o->op_last
668 MODULE = B PACKAGE = B::BINOP PREFIX = BINOP_
674 #define LOGOP_other(o) o->op_other
676 MODULE = B PACKAGE = B::LOGOP PREFIX = LOGOP_
682 #define CONDOP_true(o) o->op_true
683 #define CONDOP_false(o) o->op_false
685 MODULE = B PACKAGE = B::CONDOP PREFIX = CONDOP_
695 #define LISTOP_children(o) o->op_children
697 MODULE = B PACKAGE = B::LISTOP PREFIX = LISTOP_
703 #define PMOP_pmreplroot(o) o->op_pmreplroot
704 #define PMOP_pmreplstart(o) o->op_pmreplstart
705 #define PMOP_pmnext(o) o->op_pmnext
706 #define PMOP_pmregexp(o) o->op_pmregexp
707 #define PMOP_pmflags(o) o->op_pmflags
708 #define PMOP_pmpermflags(o) o->op_pmpermflags
710 MODULE = B PACKAGE = B::PMOP PREFIX = PMOP_
717 ST(0) = sv_newmortal();
718 root = o->op_pmreplroot;
719 /* OP_PUSHRE stores an SV* instead of an OP* in op_pmreplroot */
720 if (o->op_type == OP_PUSHRE) {
721 sv_setiv(newSVrv(ST(0), root ?
722 svclassnames[SvTYPE((SV*)root)] : "B::SV"),
726 sv_setiv(newSVrv(ST(0), cc_opclassname(root)), (IV)root);
748 REGEXP * rx = NO_INIT
750 ST(0) = sv_newmortal();
753 sv_setpvn(ST(0), rx->precomp, rx->prelen);
755 #define SVOP_sv(o) o->op_sv
757 MODULE = B PACKAGE = B::SVOP PREFIX = SVOP_
764 #define GVOP_gv(o) o->op_gv
766 MODULE = B PACKAGE = B::GVOP PREFIX = GVOP_
773 MODULE = B PACKAGE = B::PVOP PREFIX = PVOP_
780 * OP_TRANS uses op_pv to point to a table of 256 shorts
781 * whereas other PVOPs point to a null terminated string.
783 ST(0) = sv_2mortal(newSVpv(o->op_pv, (o->op_type == OP_TRANS) ?
784 256 * sizeof(short) : 0));
786 #define LOOP_redoop(o) o->op_redoop
787 #define LOOP_nextop(o) o->op_nextop
788 #define LOOP_lastop(o) o->op_lastop
790 MODULE = B PACKAGE = B::LOOP PREFIX = LOOP_
805 #define COP_label(o) o->cop_label
806 #define COP_stash(o) o->cop_stash
807 #define COP_filegv(o) o->cop_filegv
808 #define COP_cop_seq(o) o->cop_seq
809 #define COP_arybase(o) o->cop_arybase
810 #define COP_line(o) o->cop_line
811 #define COP_warnings(o) o->cop_warnings
813 MODULE = B PACKAGE = B::COP PREFIX = COP_
843 MODULE = B PACKAGE = B::SV PREFIX = Sv
853 MODULE = B PACKAGE = B::IV PREFIX = Sv
863 MODULE = B PACKAGE = B::IV
865 #define needs64bits(sv) ((I32)SvIVX(sv) != SvIVX(sv))
875 if (sizeof(IV) == 8) {
879 * The following way of spelling 32 is to stop compilers on
880 * 32-bit architectures from moaning about the shift count
881 * being >= the width of the type. Such architectures don't
882 * reach this code anyway (unless sizeof(IV) > 8 but then
883 * everything else breaks too so I'm not fussed at the moment).
885 wp[0] = htonl(((U32)iv) >> (sizeof(IV)*4));
886 wp[1] = htonl(iv & 0xffffffff);
887 ST(0) = sv_2mortal(newSVpvn((char *)wp, 8));
889 U32 w = htonl((U32)SvIVX(sv));
890 ST(0) = sv_2mortal(newSVpvn((char *)&w, 4));
893 MODULE = B PACKAGE = B::NV PREFIX = Sv
903 MODULE = B PACKAGE = B::RV PREFIX = Sv
909 MODULE = B PACKAGE = B::PV PREFIX = Sv
915 ST(0) = sv_newmortal();
916 sv_setpvn(ST(0), SvPVX(sv), SvCUR(sv));
918 MODULE = B PACKAGE = B::PVMG PREFIX = Sv
925 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic)
926 XPUSHs(make_mg_object(sv_newmortal(), mg));
928 MODULE = B PACKAGE = B::PVMG
934 #define MgMOREMAGIC(mg) mg->mg_moremagic
935 #define MgPRIVATE(mg) mg->mg_private
936 #define MgTYPE(mg) mg->mg_type
937 #define MgFLAGS(mg) mg->mg_flags
938 #define MgOBJ(mg) mg->mg_obj
939 #define MgLENGTH(mg) mg->mg_len
941 MODULE = B PACKAGE = B::MAGIC PREFIX = Mg
971 ST(0) = sv_newmortal();
973 if (mg->mg_len >= 0){
974 sv_setpvn(ST(0), mg->mg_ptr, mg->mg_len);
976 if (mg->mg_len == HEf_SVKEY)
977 sv_setsv(ST(0),newRV((SV*)mg->mg_ptr));
981 MODULE = B PACKAGE = B::PVLV PREFIX = Lv
999 MODULE = B PACKAGE = B::BM PREFIX = Bm
1016 STRLEN len = NO_INIT
1017 char * str = NO_INIT
1019 str = SvPV(sv, len);
1020 /* Boyer-Moore table is just after string and its safety-margin \0 */
1021 ST(0) = sv_2mortal(newSVpvn(str + len + 1, 256));
1023 MODULE = B PACKAGE = B::GV PREFIX = Gv
1029 ST(0) = sv_2mortal(newSVpvn(GvNAME(gv), GvNAMELEN(gv)));
1075 MODULE = B PACKAGE = B::GV
1085 MODULE = B PACKAGE = B::IO PREFIX = Io
1131 MODULE = B PACKAGE = B::IO
1141 MODULE = B PACKAGE = B::AV PREFIX = Av
1151 #define AvOFF(av) ((XPVAV*)SvANY(av))->xof_off
1161 if (AvFILL(av) >= 0) {
1162 SV **svp = AvARRAY(av);
1164 for (i = 0; i <= AvFILL(av); i++)
1165 XPUSHs(make_sv_object(sv_newmortal(), svp[i]));
1168 MODULE = B PACKAGE = B::AV
1174 MODULE = B PACKAGE = B::CV PREFIX = Cv
1212 ST(0) = sv_2mortal(newSViv((IV)CvXSUB(cv)));
1219 ST(0) = sv_2mortal(newSViv(CvXSUBANY(cv).any_iv));
1221 MODULE = B PACKAGE = B::CV
1228 MODULE = B PACKAGE = B::HV PREFIX = Hv
1258 if (HvKEYS(hv) > 0) {
1262 (void)hv_iterinit(hv);
1263 EXTEND(sp, HvKEYS(hv) * 2);
1264 while (sv = hv_iternextsv(hv, &key, &len)) {
1265 PUSHs(newSVpvn(key, len));
1266 PUSHs(make_sv_object(sv_newmortal(), sv));