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
16 typedef PerlIO * InputStream;
18 typedef FILE * InputStream;
22 static const char* const svclassnames[] = {
29 #if PERL_VERSION <= 10
39 #if PERL_VERSION >= 11
71 static const char* const opclassnames[] = {
86 static const size_t opsizes[] = {
101 #define MY_CXT_KEY "B::_guts" XS_VERSION
104 int x_walkoptree_debug; /* Flag for walkoptree debug hook */
105 SV * x_specialsv_list[7];
110 #define walkoptree_debug (MY_CXT.x_walkoptree_debug)
111 #define specialsv_list (MY_CXT.x_specialsv_list)
114 cc_opclass(pTHX_ const OP *o)
120 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
122 if (o->op_type == OP_SASSIGN)
123 return ((o->op_private & OPpASSIGN_BACKWARDS) ? OPc_UNOP : OPc_BINOP);
125 if (o->op_type == OP_AELEMFAST) {
126 if (o->op_flags & OPf_SPECIAL)
137 if (o->op_type == OP_GV || o->op_type == OP_GVSV ||
138 o->op_type == OP_RCATLINE)
142 switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
167 case OA_PVOP_OR_SVOP:
169 * Character translations (tr///) are usually a PVOP, keeping a
170 * pointer to a table of shorts used to look up translations.
171 * Under utf8, however, a simple table isn't practical; instead,
172 * the OP is an SVOP, and the SV is a reference to a swash
173 * (i.e., an RV pointing to an HV).
175 return (o->op_private & (OPpTRANS_TO_UTF|OPpTRANS_FROM_UTF))
176 ? OPc_SVOP : OPc_PVOP;
184 case OA_BASEOP_OR_UNOP:
186 * UNI(OP_foo) in toke.c returns token UNI or FUNC1 depending on
187 * whether parens were seen. perly.y uses OPf_SPECIAL to
188 * signal whether a BASEOP had empty parens or none.
189 * Some other UNOPs are created later, though, so the best
190 * test is OPf_KIDS, which is set in newUNOP.
192 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
196 * The file stat OPs are created via UNI(OP_foo) in toke.c but use
197 * the OPf_REF flag to distinguish between OP types instead of the
198 * usual OPf_SPECIAL flag. As usual, if OPf_KIDS is set, then we
199 * return OPc_UNOP so that walkoptree can find our children. If
200 * OPf_KIDS is not set then we check OPf_REF. Without OPf_REF set
201 * (no argument to the operator) it's an OP; with OPf_REF set it's
202 * an SVOP (and op_sv is the GV for the filehandle argument).
204 return ((o->op_flags & OPf_KIDS) ? OPc_UNOP :
206 (o->op_flags & OPf_REF) ? OPc_PADOP : OPc_BASEOP);
208 (o->op_flags & OPf_REF) ? OPc_SVOP : OPc_BASEOP);
212 * next, last, redo, dump and goto use OPf_SPECIAL to indicate that a
213 * label was omitted (in which case it's a BASEOP) or else a term was
214 * seen. In this last case, all except goto are definitely PVOP but
215 * goto is either a PVOP (with an ordinary constant label), an UNOP
216 * with OPf_STACKED (with a non-constant non-sub) or an UNOP for
217 * OP_REFGEN (with goto &sub) in which case OPf_STACKED also seems to
220 if (o->op_flags & OPf_STACKED)
222 else if (o->op_flags & OPf_SPECIAL)
227 warn("can't determine class of operator %s, assuming BASEOP\n",
228 PL_op_name[o->op_type]);
233 cc_opclassname(pTHX_ const OP *o)
235 return (char *)opclassnames[cc_opclass(aTHX_ o)];
238 /* FIXME - figure out how to get the typemap to assign this to ST(0), rather
239 than creating a new mortal for ST(0) then passing it in as the first
242 make_sv_object(pTHX_ SV *arg, SV *sv)
244 const char *type = 0;
249 arg = sv_newmortal();
251 for (iv = 0; iv < sizeof(specialsv_list)/sizeof(SV*); iv++) {
252 if (sv == specialsv_list[iv]) {
258 type = svclassnames[SvTYPE(sv)];
261 sv_setiv(newSVrv(arg, type), iv);
265 #if PERL_VERSION >= 9
267 make_temp_object(pTHX_ SV *temp)
270 SV *arg = sv_newmortal();
271 const char *const type = svclassnames[SvTYPE(temp)];
272 const IV iv = PTR2IV(temp);
274 target = newSVrv(arg, type);
275 sv_setiv(target, iv);
277 /* Need to keep our "temp" around as long as the target exists.
278 Simplest way seems to be to hang it from magic, and let that clear
279 it up. No vtable, so won't actually get in the way of anything. */
280 sv_magicext(target, temp, PERL_MAGIC_sv, NULL, NULL, 0);
281 /* magic object has had its reference count increased, so we must drop
288 make_warnings_object(pTHX_ STRLEN *warnings)
290 const char *type = 0;
292 IV iv = sizeof(specialsv_list)/sizeof(SV*);
294 /* Counting down is deliberate. Before the split between make_sv_object
295 and make_warnings_obj there appeared to be a bug - Nullsv and pWARN_STD
296 were both 0, so you could never get a B::SPECIAL for pWARN_STD */
299 if ((SV*)warnings == specialsv_list[iv]) {
305 SV *arg = sv_newmortal();
306 sv_setiv(newSVrv(arg, type), iv);
309 /* B assumes that warnings are a regular SV. Seems easier to keep it
310 happy by making them into a regular SV. */
311 return make_temp_object(aTHX_ newSVpvn((char *)(warnings + 1), *warnings));
316 make_cop_io_object(pTHX_ COP *cop)
318 SV *const value = newSV(0);
320 Perl_emulate_cop_io(aTHX_ cop, value);
323 return make_sv_object(aTHX_ NULL, value);
326 return make_sv_object(aTHX_ NULL, NULL);
332 make_mg_object(pTHX_ MAGIC *mg)
334 SV *arg = sv_newmortal();
335 sv_setiv(newSVrv(arg, "B::MAGIC"), PTR2IV(mg));
340 cstring(pTHX_ SV *sv, bool perlstyle)
345 return newSVpvs_flags("0", SVs_TEMP);
347 sstr = newSVpvs_flags("\"", SVs_TEMP);
349 if (perlstyle && SvUTF8(sv)) {
350 SV *tmpsv = sv_newmortal(); /* Temporary SV to feed sv_uni_display */
351 const STRLEN len = SvCUR(sv);
352 const char *s = sv_uni_display(tmpsv, sv, 8*len, UNI_DISPLAY_QQ);
356 sv_catpvs(sstr, "\\\"");
358 sv_catpvs(sstr, "\\$");
360 sv_catpvs(sstr, "\\@");
363 if (strchr("nrftax\\",*(s+1)))
364 sv_catpvn(sstr, s++, 2);
366 sv_catpvs(sstr, "\\\\");
368 else /* should always be printable */
369 sv_catpvn(sstr, s, 1);
377 const char *s = SvPV(sv, len);
378 for (; len; len--, s++)
380 /* At least try a little for readability */
382 sv_catpvs(sstr, "\\\"");
384 sv_catpvs(sstr, "\\\\");
385 /* trigraphs - bleagh */
386 else if (!perlstyle && *s == '?' && len>=3 && s[1] == '?') {
387 Perl_sv_catpvf(aTHX_ sstr, "\\%03o", '?');
389 else if (perlstyle && *s == '$')
390 sv_catpvs(sstr, "\\$");
391 else if (perlstyle && *s == '@')
392 sv_catpvs(sstr, "\\@");
394 else if (isPRINT(*s))
396 else if (*s >= ' ' && *s < 127)
398 sv_catpvn(sstr, s, 1);
400 sv_catpvs(sstr, "\\n");
402 sv_catpvs(sstr, "\\r");
404 sv_catpvs(sstr, "\\t");
406 sv_catpvs(sstr, "\\a");
408 sv_catpvs(sstr, "\\b");
410 sv_catpvs(sstr, "\\f");
411 else if (!perlstyle && *s == '\v')
412 sv_catpvs(sstr, "\\v");
415 /* Don't want promotion of a signed -1 char in sprintf args */
416 const unsigned char c = (unsigned char) *s;
417 Perl_sv_catpvf(aTHX_ sstr, "\\%03o", c);
419 /* XXX Add line breaks if string is long */
422 sv_catpvs(sstr, "\"");
429 SV *sstr = newSVpvs_flags("'", SVs_TEMP);
430 const char *s = SvPV_nolen(sv);
431 /* Don't want promotion of a signed -1 char in sprintf args */
432 const unsigned char c = (unsigned char) *s;
435 sv_catpvs(sstr, "\\'");
437 sv_catpvs(sstr, "\\\\");
441 else if (c >= ' ' && c < 127)
443 sv_catpvn(sstr, s, 1);
445 sv_catpvs(sstr, "\\n");
447 sv_catpvs(sstr, "\\r");
449 sv_catpvs(sstr, "\\t");
451 sv_catpvs(sstr, "\\a");
453 sv_catpvs(sstr, "\\b");
455 sv_catpvs(sstr, "\\f");
457 sv_catpvs(sstr, "\\v");
459 Perl_sv_catpvf(aTHX_ sstr, "\\%03o", c);
460 sv_catpvs(sstr, "'");
464 #if PERL_VERSION >= 9
465 # define PMOP_pmreplstart(o) o->op_pmstashstartu.op_pmreplstart
466 # define PMOP_pmreplroot(o) o->op_pmreplrootu.op_pmreplroot
468 # define PMOP_pmreplstart(o) o->op_pmreplstart
469 # define PMOP_pmreplroot(o) o->op_pmreplroot
470 # define PMOP_pmpermflags(o) o->op_pmpermflags
471 # define PMOP_pmdynflags(o) o->op_pmdynflags
475 walkoptree(pTHX_ SV *opsv, const char *method)
482 croak("opsv is not a reference");
483 opsv = sv_mortalcopy(opsv);
484 o = INT2PTR(OP*,SvIV((SV*)SvRV(opsv)));
485 if (walkoptree_debug) {
489 perl_call_method("walkoptree_debug", G_DISCARD);
494 perl_call_method(method, G_DISCARD);
495 if (o && (o->op_flags & OPf_KIDS)) {
496 for (kid = ((UNOP*)o)->op_first; kid; kid = kid->op_sibling) {
497 /* Use the same opsv. Rely on methods not to mess it up. */
498 sv_setiv(newSVrv(opsv, cc_opclassname(aTHX_ kid)), PTR2IV(kid));
499 walkoptree(aTHX_ opsv, method);
502 if (o && (cc_opclass(aTHX_ o) == OPc_PMOP) && o->op_type != OP_PUSHRE
503 && (kid = PMOP_pmreplroot(cPMOPo)))
505 sv_setiv(newSVrv(opsv, cc_opclassname(aTHX_ kid)), PTR2IV(kid));
506 walkoptree(aTHX_ opsv, method);
511 oplist(pTHX_ OP *o, SV **SP)
513 for(; o; o = o->op_next) {
515 #if PERL_VERSION >= 9
524 opsv = sv_newmortal();
525 sv_setiv(newSVrv(opsv, cc_opclassname(aTHX_ (OP*)o)), PTR2IV(o));
527 switch (o->op_type) {
529 SP = oplist(aTHX_ PMOP_pmreplstart(cPMOPo), SP);
532 if (o->op_flags & OPf_STACKED && o->op_flags & OPf_SPECIAL) {
533 OP *kid = cLISTOPo->op_first->op_sibling; /* pass pushmark */
534 kid = kUNOP->op_first; /* pass rv2gv */
535 kid = kUNOP->op_first; /* pass leave */
536 SP = oplist(aTHX_ kid->op_next, SP);
540 switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
542 SP = oplist(aTHX_ cLOGOPo->op_other, SP);
545 SP = oplist(aTHX_ cLOOPo->op_lastop, SP);
546 SP = oplist(aTHX_ cLOOPo->op_nextop, SP);
547 SP = oplist(aTHX_ cLOOPo->op_redoop, SP);
555 typedef UNOP *B__UNOP;
556 typedef BINOP *B__BINOP;
557 typedef LOGOP *B__LOGOP;
558 typedef LISTOP *B__LISTOP;
559 typedef PMOP *B__PMOP;
560 typedef SVOP *B__SVOP;
561 typedef PADOP *B__PADOP;
562 typedef PVOP *B__PVOP;
563 typedef LOOP *B__LOOP;
571 #if PERL_VERSION >= 11
572 typedef SV *B__REGEXP;
584 typedef MAGIC *B__MAGIC;
586 #if PERL_VERSION >= 9
587 typedef struct refcounted_he *B__RHE;
590 #include "const-c.inc"
592 MODULE = B PACKAGE = B PREFIX = B_
594 INCLUDE: const-xs.inc
600 HV *stash = gv_stashpvs("B", GV_ADD);
601 AV *export_ok = perl_get_av("B::EXPORT_OK", GV_ADD);
603 specialsv_list[0] = Nullsv;
604 specialsv_list[1] = &PL_sv_undef;
605 specialsv_list[2] = &PL_sv_yes;
606 specialsv_list[3] = &PL_sv_no;
607 specialsv_list[4] = (SV *) pWARN_ALL;
608 specialsv_list[5] = (SV *) pWARN_NONE;
609 specialsv_list[6] = (SV *) pWARN_STD;
610 #if PERL_VERSION <= 8
611 # define OPpPAD_STATE 0
615 #define B_main_cv() PL_main_cv
616 #define B_init_av() PL_initav
617 #define B_inc_gv() PL_incgv
618 #define B_check_av() PL_checkav_save
620 # define B_unitcheck_av() PL_unitcheckav_save
622 # define B_unitcheck_av() NULL
624 #define B_begin_av() PL_beginav_save
625 #define B_end_av() PL_endav
626 #define B_main_root() PL_main_root
627 #define B_main_start() PL_main_start
628 #define B_amagic_generation() PL_amagic_generation
629 #define B_sub_generation() PL_sub_generation
630 #define B_defstash() PL_defstash
631 #define B_curstash() PL_curstash
632 #define B_dowarn() PL_dowarn
633 #define B_comppadlist() (PL_main_cv ? CvPADLIST(PL_main_cv) : CvPADLIST(PL_compcv))
634 #define B_sv_undef() &PL_sv_undef
635 #define B_sv_yes() &PL_sv_yes
636 #define B_sv_no() &PL_sv_no
637 #define B_formfeed() PL_formfeed
639 #define B_regex_padav() PL_regex_padav
648 #if PERL_VERSION >= 9
681 B_amagic_generation()
713 ST(0) = make_sv_object(aTHX_ NULL, PL_warnhook);
718 ST(0) = make_sv_object(aTHX_ NULL, PL_diehook);
720 MODULE = B PACKAGE = B
723 walkoptree(opsv, method)
727 walkoptree(aTHX_ opsv, method);
730 walkoptree_debug(...)
733 RETVAL = walkoptree_debug;
734 if (items > 0 && SvTRUE(ST(1)))
735 walkoptree_debug = 1;
739 #define address(sv) PTR2IV(sv)
750 croak("argument is not a reference");
751 RETVAL = (SV*)SvRV(sv);
762 ST(0) = sv_newmortal();
763 if (strncmp(name,"pp_",3) == 0)
765 for (i = 0; i < PL_maxo; i++)
767 if (strcmp(name, PL_op_name[i]) == 0)
773 sv_setiv(ST(0),result);
780 ST(0) = sv_newmortal();
781 if (opnum >= 0 && opnum < PL_maxo) {
782 sv_setpvs(ST(0), "pp_");
783 sv_catpv(ST(0), PL_op_name[opnum]);
792 const char *s = SvPVbyte(sv, len);
793 PERL_HASH(hash, s, len);
794 ST(0) = sv_2mortal(Perl_newSVpvf(aTHX_ "0x%"UVxf, (UV)hash));
796 #define cast_I32(foo) (I32)foo
818 PUSHs(ix == 2 ? cchar(aTHX_ sv) : cstring(aTHX_ sv, ix));
823 #if PERL_VERSION <= 8
824 # ifdef USE_5005THREADS
826 const STRLEN len = strlen(PL_threadsv_names);
829 for (i = 0; i < len; i++)
830 PUSHs(newSVpvn_flags(&PL_threadsv_names[i], 1, SVs_TEMP));
836 #define line_tp 0x20000
838 #define PADOFFSETp 0x40000
841 #define char_pp 0x70000
843 #define OP_next_ix OPp | offsetof(struct op, op_next)
844 #define OP_sibling_ix OPp | offsetof(struct op, op_sibling)
845 #define UNOP_first_ix OPp | offsetof(struct unop, op_first)
846 #define BINOP_last_ix OPp | offsetof(struct binop, op_last)
847 #define LOGOP_other_ix OPp | offsetof(struct logop, op_other)
848 #if PERL_VERSION >= 9
849 # define PMOP_pmreplstart_ix \
850 OPp | offsetof(struct pmop, op_pmstashstartu.op_pmreplstart)
852 # define PMOP_pmreplstart_ix OPp | offsetof(struct pmop, op_pmreplstart)
854 #define LOOP_redoop_ix OPp | offsetof(struct loop, op_redoop)
855 #define LOOP_nextop_ix OPp | offsetof(struct loop, op_nextop)
856 #define LOOP_lastop_ix OPp | offsetof(struct loop, op_lastop)
858 #define OP_targ_ix PADOFFSETp | offsetof(struct op, op_targ)
859 #define OP_flags_ix U8p | offsetof(struct op, op_flags)
860 #define OP_private_ix U8p | offsetof(struct op, op_private)
862 #define PMOP_pmflags_ix U32p | offsetof(struct pmop, op_pmflags)
865 #define PMOP_pmoffset_ix IVp | offsetof(struct pmop, op_pmoffset)
868 # Yes, B::SV::sv and B::SV::gv really do end up generating identical code.
869 #define SVOP_sv_ix SVp | offsetof(struct svop, op_sv)
870 #define SVOP_gv_ix SVp | offsetof(struct svop, op_sv)
872 #define PADOP_padix_ix PADOFFSETp | offsetof(struct padop, op_padix)
874 #define COP_seq_ix U32p | offsetof(struct cop, cop_seq)
875 #define COP_line_ix line_tp | offsetof(struct cop, cop_line)
876 #if PERL_VERSION >= 9
877 #define COP_hints_ix U32p | offsetof(struct cop, cop_hints)
879 #define COP_hints_ix U8p | offsetof(struct cop, op_private)
883 #define COP_stashpv_ix char_pp | offsetof(struct cop, cop_stashpv)
884 #define COP_file_ix char_pp | offsetof(struct cop, cop_file)
886 #define COP_stash_ix SVp | offsetof(struct cop, cop_stash)
887 #define COP_filegv_ix SVp | offsetof(struct cop, cop_filegv)
890 MODULE = B PACKAGE = B::OP PREFIX = OP_
896 RETVAL = opsizes[cc_opclass(aTHX_ o)];
900 # The type checking code in B has always been identical for all OP types,
901 # irrespective of whether the action is actually defined on that OP.
907 B::OP::next = OP_next_ix
908 B::OP::sibling = OP_sibling_ix
909 B::OP::targ = OP_targ_ix
910 B::OP::flags = OP_flags_ix
911 B::OP::private = OP_private_ix
912 B::UNOP::first = UNOP_first_ix
913 B::BINOP::last = BINOP_last_ix
914 B::LOGOP::other = LOGOP_other_ix
915 B::PMOP::pmreplstart = PMOP_pmreplstart_ix
916 B::LOOP::redoop = LOOP_redoop_ix
917 B::LOOP::nextop = LOOP_nextop_ix
918 B::LOOP::lastop = LOOP_lastop_ix
919 B::PMOP::pmflags = PMOP_pmflags_ix
920 B::SVOP::sv = SVOP_sv_ix
921 B::SVOP::gv = SVOP_gv_ix
922 B::PADOP::padix = PADOP_padix_ix
923 B::COP::cop_seq = COP_seq_ix
924 B::COP::line = COP_line_ix
925 B::COP::hints = COP_hints_ix
930 ptr = (ix & 0xFFFF) + (char *)o;
931 switch ((U8)(ix >> 16)) {
932 case (U8)(OPp >> 16):
934 OP *const o2 = *((OP **)ptr);
935 ret = sv_newmortal();
936 sv_setiv(newSVrv(ret, cc_opclassname(aTHX_ o2)), PTR2IV(o2));
939 case (U8)(PADOFFSETp >> 16):
940 ret = sv_2mortal(newSVuv(*((PADOFFSET*)ptr)));
942 case (U8)(U8p >> 16):
943 ret = sv_2mortal(newSVuv(*((U8*)ptr)));
945 case (U8)(U32p >> 16):
946 ret = sv_2mortal(newSVuv(*((U32*)ptr)));
948 case (U8)(SVp >> 16):
949 ret = make_sv_object(aTHX_ NULL, *((SV **)ptr));
951 case (U8)(line_tp >> 16):
952 ret = sv_2mortal(newSVuv(*((line_t *)ptr)));
955 case (U8)(IVp >> 16):
956 ret = sv_2mortal(newSViv(*((IV*)ptr)));
958 case (U8)(char_pp >> 16):
959 ret = sv_2mortal(newSVpv(*((char **)ptr), 0));
972 RETVAL = (char *)(ix ? PL_op_desc : PL_op_name)[o->op_type];
981 SV *sv = newSVpvs_flags("PL_ppaddr[OP_", SVs_TEMP);
983 sv_catpv(sv, PL_op_name[o->op_type]);
984 for (i=13; (STRLEN)i < SvCUR(sv); ++i)
985 SvPVX(sv)[i] = toUPPER(SvPVX(sv)[i]);
989 #if PERL_VERSION >= 9
990 # These 3 are all bitfields, so we can't take their addresses.
1003 RETVAL = o->op_spare;
1006 RETVAL = o->op_type;
1024 RETVAL = o->op_type;
1035 SP = oplist(aTHX_ o, SP);
1037 MODULE = B PACKAGE = B::LISTOP PREFIX = LISTOP_
1046 for (kid = o->op_first; kid; kid = kid->op_sibling)
1052 MODULE = B PACKAGE = B::PMOP PREFIX = PMOP_
1054 #if PERL_VERSION <= 8
1061 ST(0) = sv_newmortal();
1062 root = o->op_pmreplroot;
1063 /* OP_PUSHRE stores an SV* instead of an OP* in op_pmreplroot */
1064 if (o->op_type == OP_PUSHRE) {
1065 # ifdef USE_ITHREADS
1066 sv_setiv(ST(0), INT2PTR(PADOFFSET,root) );
1068 sv_setiv(newSVrv(ST(0), root ?
1069 svclassnames[SvTYPE((SV*)root)] : "B::SV"),
1074 sv_setiv(newSVrv(ST(0), cc_opclassname(aTHX_ root)), PTR2IV(root));
1083 ST(0) = sv_newmortal();
1084 if (o->op_type == OP_PUSHRE) {
1085 # ifdef USE_ITHREADS
1086 sv_setiv(ST(0), o->op_pmreplrootu.op_pmtargetoff);
1088 GV *const target = o->op_pmreplrootu.op_pmtargetgv;
1089 sv_setiv(newSVrv(ST(0), target ?
1090 svclassnames[SvTYPE((SV*)target)] : "B::SV"),
1095 OP *const root = o->op_pmreplrootu.op_pmreplroot;
1096 sv_setiv(newSVrv(ST(0), cc_opclassname(aTHX_ root)),
1103 #define PMOP_pmstashpv(o) PmopSTASHPV(o);
1110 #define PMOP_pmstash(o) PmopSTASH(o);
1118 #if PERL_VERSION < 9
1119 #define PMOP_pmnext(o) o->op_pmnext
1143 ST(0) = sv_newmortal();
1145 #if PERL_VERSION >= 9
1147 sv_setuv(ST(0), RX_EXTFLAGS(rx));
1151 sv_setpvn(ST(0), RX_PRECOMP(rx), RX_PRELEN(rx));
1159 cv = newXS("B::PMOP::pmoffset", XS_B__OP_next, __FILE__);
1160 XSANY.any_i32 = PMOP_pmoffset_ix;
1161 cv = newXS("B::COP::stashpv", XS_B__OP_next, __FILE__);
1162 XSANY.any_i32 = COP_stashpv_ix;
1163 cv = newXS("B::COP::file", XS_B__OP_next, __FILE__);
1164 XSANY.any_i32 = COP_file_ix;
1166 cv = newXS("B::COP::stash", XS_B__OP_next, __FILE__);
1167 XSANY.any_i32 = COP_stash_ix;
1168 cv = newXS("B::COP::filegv", XS_B__OP_next, __FILE__);
1169 XSANY.any_i32 = COP_filegv_ix;
1171 #if PERL_VERSION >= 9
1172 cv = newXS("B::PMOP::reflags", XS_B__PMOP_precomp, __FILE__);
1177 MODULE = B PACKAGE = B::PADOP
1185 /* It happens that the output typemaps for B::SV and B::GV are
1186 identical. The "smarts" are in make_sv_object(), which determines
1187 which class to use based on SvTYPE(), rather than anything baked in
1190 RETVAL = PAD_SVl(o->op_padix);
1191 if (ix && SvTYPE(RETVAL) != SVt_PVGV)
1199 MODULE = B PACKAGE = B::PVOP PREFIX = PVOP_
1206 * OP_TRANS uses op_pv to point to a table of 256 or >=258 shorts
1207 * whereas other PVOPs point to a null terminated string.
1209 if (o->op_type == OP_TRANS &&
1210 (o->op_private & OPpTRANS_COMPLEMENT) &&
1211 !(o->op_private & OPpTRANS_DELETE))
1213 const short* const tbl = (short*)o->op_pv;
1214 const short entries = 257 + tbl[256];
1215 ST(0) = newSVpvn_flags(o->op_pv, entries * sizeof(short), SVs_TEMP);
1217 else if (o->op_type == OP_TRANS) {
1218 ST(0) = newSVpvn_flags(o->op_pv, 256 * sizeof(short), SVs_TEMP);
1221 ST(0) = newSVpvn_flags(o->op_pv, strlen(o->op_pv), SVs_TEMP);
1223 #define COP_label(o) CopLABEL(o)
1224 #define COP_arybase(o) CopARYBASE_get(o)
1226 MODULE = B PACKAGE = B::COP PREFIX = COP_
1232 # Both pairs of accessors are provided for both ithreads and not, but for each,
1233 # one pair is direct structure access, and 1 pair "faked up" with a more complex
1234 # macro. We implement the direct structure access pair using the common code
1235 # above (B::OP::next)
1238 #define COP_stash(o) CopSTASH(o)
1239 #define COP_filegv(o) CopFILEGV(o)
1250 #define COP_stashpv(o) CopSTASHPV(o)
1251 #define COP_file(o) CopFILE(o)
1271 #if PERL_VERSION >= 9
1272 ST(0) = make_warnings_object(aTHX_ o->cop_warnings);
1274 ST(0) = make_sv_object(aTHX_ NULL, o->cop_warnings);
1282 #if PERL_VERSION >= 9
1283 ST(0) = make_cop_io_object(aTHX_ o);
1285 ST(0) = make_sv_object(aTHX_ NULL, o->cop_io);
1289 #if PERL_VERSION >= 9
1295 RETVAL = CopHINTHASH_get(o);
1301 MODULE = B PACKAGE = B::SV
1303 #define MAGICAL_FLAG_BITS (SVs_GMG|SVs_SMG|SVs_RMG)
1313 MAGICAL = MAGICAL_FLAG_BITS
1315 RETVAL = ix ? (SvFLAGS(sv) & (U32)ix) : SvREFCNT(sv);
1323 ST(0) = sv_2mortal(newRV(sv));
1326 MODULE = B PACKAGE = B::IV PREFIX = Sv
1332 MODULE = B PACKAGE = B::IV
1334 #define sv_SVp 0x00000
1335 #define sv_IVp 0x10000
1336 #define sv_UVp 0x20000
1337 #define sv_STRLENp 0x30000
1338 #define sv_U32p 0x40000
1339 #define sv_U8p 0x50000
1340 #define sv_char_pp 0x60000
1341 #define sv_NVp 0x70000
1342 #define sv_char_p 0x80000
1344 #define IV_ivx_ix sv_IVp | offsetof(struct xpviv, xiv_iv)
1345 #define IV_uvx_ix sv_UVp | offsetof(struct xpvuv, xuv_uv)
1346 #define NV_nvx_ix sv_NVp | offsetof(struct xpvnv, xnv_u.xnv_nv)
1348 #if PERL_VERSION >= 10
1349 #define NV_cop_seq_range_low_ix \
1350 sv_U32p | offsetof(struct xpvnv, xnv_u.xpad_cop_seq.xlow)
1351 #define NV_cop_seq_range_high_ix \
1352 sv_U32p | offsetof(struct xpvnv, xnv_u.xpad_cop_seq.xhigh)
1353 #define NV_parent_pad_index_ix \
1354 sv_U32p | offsetof(struct xpvnv, xnv_u.xpad_cop_seq.xlow)
1355 #define NV_parent_fakelex_flags_ix \
1356 sv_U32p | offsetof(struct xpvnv, xnv_u.xpad_cop_seq.xhigh)
1358 #define NV_cop_seq_range_low_ix \
1359 sv_NVp | offsetof(struct xpvnv, xnv_nv)
1360 #define NV_cop_seq_range_high_ix \
1361 sv_UVp | offsetof(struct xpvnv, xuv_uv)
1362 #define NV_parent_pad_index_ix \
1363 sv_NVp | offsetof(struct xpvnv, xnv_nv)
1364 #define NV_parent_fakelex_flags_ix \
1365 sv_UVp | offsetof(struct xpvnv, xuv_uv)
1368 #define PV_cur_ix sv_STRLENp | offsetof(struct xpv, xpv_cur)
1369 #define PV_len_ix sv_STRLENp | offsetof(struct xpv, xpv_len)
1371 #define PVMG_stash_ix sv_SVp | offsetof(struct xpvmg, xmg_stash)
1373 #define PVLV_targoff_ix sv_U32p | offsetof(struct xpvlv, xlv_targoff)
1374 #define PVLV_targlen_ix sv_U32p | offsetof(struct xpvlv, xlv_targlen)
1375 #define PVLV_targ_ix sv_SVp | offsetof(struct xpvlv, xlv_targ)
1376 #define PVLV_type_ix sv_char_p | offsetof(struct xpvlv, xlv_type)
1378 #if PERL_VERSION >= 10
1379 #define PVGV_stash_ix sv_SVp | offsetof(struct xpvgv, xnv_u.xgv_stash)
1380 #define PVGV_flags_ix sv_STRLENp | offsetof(struct xpvgv, xpv_cur)
1381 #define PVIO_lines_ix sv_IVp | offsetof(struct xpvio, xiv_iv)
1383 #define PVGV_stash_ix sv_SVp | offsetof(struct xpvgv, xgv_stash)
1384 #define PVGV_flags_ix sv_U8p | offsetof(struct xpvgv, xgv_flags)
1385 #define PVIO_lines_ix sv_IVp | offsetof(struct xpvio, xio_lines)
1388 #define PVIO_page_ix sv_IVp | offsetof(struct xpvio, xio_page)
1389 #define PVIO_page_len_ix sv_IVp | offsetof(struct xpvio, xio_page_len)
1390 #define PVIO_lines_left_ix sv_IVp | offsetof(struct xpvio, xio_lines_left)
1391 #define PVIO_top_name_ix sv_char_pp | offsetof(struct xpvio, xio_top_name)
1392 #define PVIO_top_gv_ix sv_SVp | offsetof(struct xpvio, xio_top_gv)
1393 #define PVIO_fmt_name_ix sv_char_pp | offsetof(struct xpvio, xio_fmt_name)
1394 #define PVIO_fmt_gv_ix sv_SVp | offsetof(struct xpvio, xio_fmt_gv)
1395 #define PVIO_bottom_name_ix sv_char_pp | offsetof(struct xpvio, xio_bottom_name)
1396 #define PVIO_bottom_gv_ix sv_SVp | offsetof(struct xpvio, xio_bottom_gv)
1397 #define PVIO_type_ix sv_char_p | offsetof(struct xpvio, xio_type)
1398 #define PVIO_flags_ix sv_U8p | offsetof(struct xpvio, xio_flags)
1400 # The type checking code in B has always been identical for all SV types,
1401 # irrespective of whether the action is actually defined on that SV.
1402 # We should fix this
1407 B::IV::IVX = IV_ivx_ix
1408 B::IV::UVX = IV_uvx_ix
1409 B::NV::NVX = NV_nvx_ix
1410 B::NV::COP_SEQ_RANGE_LOW = NV_cop_seq_range_low_ix
1411 B::NV::COP_SEQ_RANGE_HIGH = NV_cop_seq_range_high_ix
1412 B::NV::PARENT_PAD_INDEX = NV_parent_pad_index_ix
1413 B::NV::PARENT_FAKELEX_FLAGS = NV_parent_fakelex_flags_ix
1414 B::PV::CUR = PV_cur_ix
1415 B::PV::LEN = PV_len_ix
1416 B::PVMG::SvSTASH = PVMG_stash_ix
1417 B::PVLV::TARGOFF = PVLV_targoff_ix
1418 B::PVLV::TARGLEN = PVLV_targlen_ix
1419 B::PVLV::TARG = PVLV_targ_ix
1420 B::PVLV::TYPE = PVLV_type_ix
1421 B::GV::STASH = PVGV_stash_ix
1422 B::GV::GvFLAGS = PVGV_flags_ix
1423 B::IO::LINES = PVIO_lines_ix
1424 B::IO::PAGE = PVIO_page_ix
1425 B::IO::PAGE_LEN = PVIO_page_len_ix
1426 B::IO::LINES_LEFT = PVIO_lines_left_ix
1427 B::IO::TOP_NAME = PVIO_top_name_ix
1428 B::IO::TOP_GV = PVIO_top_gv_ix
1429 B::IO::FMT_NAME = PVIO_fmt_name_ix
1430 B::IO::FMT_GV = PVIO_fmt_gv_ix
1431 B::IO::BOTTOM_NAME = PVIO_bottom_name_ix
1432 B::IO::BOTTOM_GV = PVIO_bottom_gv_ix
1433 B::IO::IoTYPE = PVIO_type_ix
1434 B::IO::IoFLAGS = PVIO_flags_ix
1439 ptr = (ix & 0xFFFF) + (char *)SvANY(sv);
1440 switch ((U8)(ix >> 16)) {
1441 case (U8)(sv_SVp >> 16):
1442 ret = make_sv_object(aTHX_ NULL, *((SV **)ptr));
1444 case (U8)(sv_IVp >> 16):
1445 ret = sv_2mortal(newSViv(*((IV *)ptr)));
1447 case (U8)(sv_UVp >> 16):
1448 ret = sv_2mortal(newSVuv(*((UV *)ptr)));
1450 case (U8)(sv_STRLENp >> 16):
1451 ret = sv_2mortal(newSVuv(*((STRLEN *)ptr)));
1453 case (U8)(sv_U32p >> 16):
1454 ret = sv_2mortal(newSVuv(*((U32 *)ptr)));
1456 case (U8)(sv_U8p >> 16):
1457 ret = sv_2mortal(newSVuv(*((U8 *)ptr)));
1459 case (U8)(sv_char_pp >> 16):
1460 ret = sv_2mortal(newSVpv(*((char **)ptr), 0));
1462 case (U8)(sv_NVp >> 16):
1463 ret = sv_2mortal(newSVnv(*((NV *)ptr)));
1465 case (U8)(sv_char_p >> 16):
1466 ret = newSVpvn_flags((char *)ptr, 1, SVs_TEMP);
1479 ST(0) = boolSV((I32)SvIVX(sv) != SvIVX(sv));
1480 } else if (sizeof(IV) == 8) {
1482 const IV iv = SvIVX(sv);
1484 * The following way of spelling 32 is to stop compilers on
1485 * 32-bit architectures from moaning about the shift count
1486 * being >= the width of the type. Such architectures don't
1487 * reach this code anyway (unless sizeof(IV) > 8 but then
1488 * everything else breaks too so I'm not fussed at the moment).
1491 wp[0] = htonl(((UV)iv) >> (sizeof(UV)*4));
1493 wp[0] = htonl(((U32)iv) >> (sizeof(UV)*4));
1495 wp[1] = htonl(iv & 0xffffffff);
1496 ST(0) = newSVpvn_flags((char *)wp, 8, SVs_TEMP);
1498 U32 w = htonl((U32)SvIVX(sv));
1499 ST(0) = newSVpvn_flags((char *)&w, 4, SVs_TEMP);
1502 #if PERL_VERSION >= 11
1503 # The input typemap checking makes no distinction between different SV types,
1504 # so the XS body will generate the same C code, despite the different XS
1505 # "types". So there is no change in behaviour from doing newXS like this,
1506 # compared with the old approach of having a (near) duplicate XS body.
1507 # We should fix the typemap checking.
1510 newXS("B::IV::RV", XS_B__PV_RV, __FILE__);
1514 MODULE = B PACKAGE = B::NV PREFIX = Sv
1520 #if PERL_VERSION < 11
1522 MODULE = B PACKAGE = B::RV PREFIX = Sv
1530 MODULE = B PACKAGE = B::PV PREFIX = Sv
1544 croak( "argument is not SvROK" );
1554 STRLEN len = SvCUR(sv);
1555 const char *p = SvPVX_const(sv);
1556 #if PERL_VERSION < 10
1557 /* Before 5.10 (well 931b58fb28fa5ca7), PAD_COMPNAME_GEN was stored
1558 in SvCUR(), which meant we had to attempt this special casing
1559 to avoid tripping up over variable names in the pads. */
1560 if((SvLEN(sv) && len >= SvLEN(sv))) {
1561 /* It claims to be longer than the space allocated for it -
1562 presuambly it's a variable name in the pad */
1566 ST(0) = newSVpvn_flags(p, len, SVs_TEMP | SvUTF8(sv));
1569 /* XXX for backward compatibility, but should fail */
1570 /* croak( "argument is not SvPOK" ); */
1571 ST(0) = sv_newmortal();
1574 # This used to read 257. I think that that was buggy - should have been 258.
1575 # (The "\0", the flags byte, and 256 for the table. Not that anything
1576 # anywhere calls this method. NWC.
1581 ST(0) = newSVpvn_flags(SvPVX_const(sv),
1582 SvCUR(sv) + (SvVALID(sv) ? 256 + PERL_FBM_TABLE_OFFSET : 0),
1585 MODULE = B PACKAGE = B::PVMG PREFIX = Sv
1590 MAGIC * mg = NO_INIT
1592 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic)
1593 XPUSHs(make_mg_object(aTHX_ mg));
1595 MODULE = B PACKAGE = B::REGEXP
1597 #if PERL_VERSION >= 11
1603 /* FIXME - can we code this method more efficiently? */
1604 RETVAL = PTR2IV(sv);
1612 RETVAL = newSVpvn( RX_PRECOMP(sv), RX_PRELEN(sv) );
1618 #define MgMOREMAGIC(mg) mg->mg_moremagic
1619 #define MgPRIVATE(mg) mg->mg_private
1620 #define MgTYPE(mg) mg->mg_type
1621 #define MgFLAGS(mg) mg->mg_flags
1622 #define MgOBJ(mg) mg->mg_obj
1623 #define MgLENGTH(mg) mg->mg_len
1624 #define MgREGEX(mg) PTR2IV(mg->mg_obj)
1626 MODULE = B PACKAGE = B::MAGIC PREFIX = Mg
1632 if( MgMOREMAGIC(mg) ) {
1633 RETVAL = MgMOREMAGIC(mg);
1661 if(mg->mg_type == PERL_MAGIC_qr) {
1662 RETVAL = MgREGEX(mg);
1665 croak( "REGEX is only meaningful on r-magic" );
1674 if (mg->mg_type == PERL_MAGIC_qr) {
1675 REGEXP* rx = (REGEXP*)mg->mg_obj;
1678 RETVAL = newSVpvn( RX_PRECOMP(rx), RX_PRELEN(rx) );
1681 croak( "precomp is only meaningful on r-magic" );
1695 if (mg->mg_len >= 0){
1696 ST(0) = newSVpvn_flags(mg->mg_ptr, mg->mg_len, SVs_TEMP);
1697 } else if (mg->mg_len == HEf_SVKEY) {
1698 ST(0) = make_sv_object(aTHX_ NULL, (SV*)mg->mg_ptr);
1700 ST(0) = sv_newmortal();
1702 ST(0) = sv_newmortal();
1704 MODULE = B PACKAGE = B::BM PREFIX = Bm
1721 STRLEN len = NO_INIT
1722 char * str = NO_INIT
1724 str = SvPV(sv, len);
1725 /* Boyer-Moore table is just after string and its safety-margin \0 */
1726 ST(0) = newSVpvn_flags(str + len + PERL_FBM_TABLE_OFFSET, 256, SVs_TEMP);
1728 MODULE = B PACKAGE = B::GV PREFIX = Gv
1736 #if PERL_VERSION >= 10
1737 ST(0) = sv_2mortal(newSVhek(ix ? GvFILE_HEK(gv) : GvNAME_HEK(gv)));
1739 ST(0) = ix ? sv_2mortal(newSVpv(GvFILE(gv), 0))
1740 : newSVpvn_flags(GvNAME(gv), GvNAMELEN(gv), SVs_TEMP);
1750 #if PERL_VERSION >= 9
1751 RETVAL = isGV_with_GP(gv) ? TRUE : FALSE;
1753 RETVAL = TRUE; /* In 5.8 and earlier they all are. */
1756 RETVAL = GvGP(gv) == Null(GP*);
1765 #define GP_sv_ix SVp | offsetof(struct gp, gp_sv)
1766 #define GP_io_ix SVp | offsetof(struct gp, gp_io)
1767 #define GP_cv_ix SVp | offsetof(struct gp, gp_cv)
1768 #define GP_cvgen_ix U32p | offsetof(struct gp, gp_cvgen)
1769 #define GP_refcnt_ix U32p | offsetof(struct gp, gp_refcnt)
1770 #define GP_hv_ix SVp | offsetof(struct gp, gp_hv)
1771 #define GP_av_ix SVp | offsetof(struct gp, gp_av)
1772 #define GP_form_ix SVp | offsetof(struct gp, gp_form)
1773 #define GP_egv_ix SVp | offsetof(struct gp, gp_egv)
1774 #define GP_line_ix line_tp | offsetof(struct gp, gp_line)
1784 GvREFCNT = GP_refcnt_ix
1797 const GV *const gv = CvGV(cv);
1798 Perl_croak(aTHX_ "NULL gp in B::GV::%s", gv ? GvNAME(gv) : "???");
1800 ptr = (ix & 0xFFFF) + (char *)gp;
1801 switch ((U8)(ix >> 16)) {
1802 case (U8)(SVp >> 16):
1803 ret = make_sv_object(aTHX_ NULL, *((SV **)ptr));
1805 case (U8)(U32p >> 16):
1806 ret = sv_2mortal(newSVuv(*((U32*)ptr)));
1808 case (U8)(line_tp >> 16):
1809 ret = sv_2mortal(newSVuv(*((line_t *)ptr)));
1819 MODULE = B PACKAGE = B::IO PREFIX = Io
1821 #if PERL_VERSION <= 8
1836 if( strEQ( name, "stdin" ) ) {
1837 handle = PerlIO_stdin();
1839 else if( strEQ( name, "stdout" ) ) {
1840 handle = PerlIO_stdout();
1842 else if( strEQ( name, "stderr" ) ) {
1843 handle = PerlIO_stderr();
1846 croak( "Invalid value '%s'", name );
1848 RETVAL = handle == IoIFP(io);
1852 MODULE = B PACKAGE = B::AV PREFIX = Av
1866 if (AvFILL(av) >= 0) {
1867 SV **svp = AvARRAY(av);
1869 for (i = 0; i <= AvFILL(av); i++)
1870 XPUSHs(make_sv_object(aTHX_ NULL, svp[i]));
1878 if (idx >= 0 && AvFILL(av) >= 0 && idx <= AvFILL(av))
1879 XPUSHs(make_sv_object(aTHX_ NULL, (AvARRAY(av)[idx])));
1881 XPUSHs(make_sv_object(aTHX_ NULL, NULL));
1883 #if PERL_VERSION < 9
1885 #define AvOFF(av) ((XPVAV*)SvANY(av))->xof_off
1891 MODULE = B PACKAGE = B::AV
1899 MODULE = B PACKAGE = B::FM PREFIX = Fm
1905 MODULE = B PACKAGE = B::CV PREFIX = Cv
1921 RETVAL = CvISXSUB(cv) ? NULL : ix ? CvROOT(cv) : CvSTART(cv);
1953 ST(0) = sv_2mortal(newSViv(CvISXSUB(cv) ? PTR2IV(CvXSUB(cv)) : 0));
1961 ? make_sv_object(aTHX_ NULL, (SV *)CvXSUBANY(cv).any_ptr)
1962 : sv_2mortal(newSViv(CvISXSUB(cv) ? CvXSUBANY(cv).any_iv : 0));
1964 MODULE = B PACKAGE = B::CV
1970 MODULE = B PACKAGE = B::CV PREFIX = cv_
1977 MODULE = B PACKAGE = B::HV PREFIX = Hv
1999 #if PERL_VERSION < 9
2011 if (HvKEYS(hv) > 0) {
2015 (void)hv_iterinit(hv);
2016 EXTEND(sp, HvKEYS(hv) * 2);
2017 while ((sv = hv_iternextsv(hv, &key, &len))) {
2019 PUSHs(make_sv_object(aTHX_ NULL, sv));
2023 MODULE = B PACKAGE = B::HE PREFIX = He
2037 MODULE = B PACKAGE = B::RHE PREFIX = RHE_
2039 #if PERL_VERSION >= 9
2045 RETVAL = newRV( (SV*)cophh_2hv(h, 0) );