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
1341 MODULE = B PACKAGE = B::IV
1350 ST(0) = boolSV((I32)SvIVX(sv) != SvIVX(sv));
1351 } else if (sizeof(IV) == 8) {
1353 const IV iv = SvIVX(sv);
1355 * The following way of spelling 32 is to stop compilers on
1356 * 32-bit architectures from moaning about the shift count
1357 * being >= the width of the type. Such architectures don't
1358 * reach this code anyway (unless sizeof(IV) > 8 but then
1359 * everything else breaks too so I'm not fussed at the moment).
1362 wp[0] = htonl(((UV)iv) >> (sizeof(UV)*4));
1364 wp[0] = htonl(((U32)iv) >> (sizeof(UV)*4));
1366 wp[1] = htonl(iv & 0xffffffff);
1367 ST(0) = newSVpvn_flags((char *)wp, 8, SVs_TEMP);
1369 U32 w = htonl((U32)SvIVX(sv));
1370 ST(0) = newSVpvn_flags((char *)&w, 4, SVs_TEMP);
1373 #if PERL_VERSION >= 11
1374 # The input typemap checking makes no distinction between different SV types,
1375 # so the XS body will generate the same C code, despite the different XS
1376 # "types". So there is no change in behaviour from doing newXS like this,
1377 # compared with the old approach of having a (near) duplicate XS body.
1378 # We should fix the typemap checking.
1381 newXS("B::IV::RV", XS_B__PV_RV, __FILE__);
1385 MODULE = B PACKAGE = B::NV PREFIX = Sv
1396 COP_SEQ_RANGE_LOW(sv)
1400 COP_SEQ_RANGE_HIGH(sv)
1404 PARENT_PAD_INDEX(sv)
1408 PARENT_FAKELEX_FLAGS(sv)
1411 #if PERL_VERSION < 11
1413 MODULE = B PACKAGE = B::RV PREFIX = Sv
1421 MODULE = B PACKAGE = B::PV PREFIX = Sv
1435 croak( "argument is not SvROK" );
1445 STRLEN len = SvCUR(sv);
1446 const char *p = SvPVX_const(sv);
1447 #if PERL_VERSION < 10
1448 /* Before 5.10 (well 931b58fb28fa5ca7), PAD_COMPNAME_GEN was stored
1449 in SvCUR(), which meant we had to attempt this special casing
1450 to avoid tripping up over variable names in the pads. */
1451 if((SvLEN(sv) && len >= SvLEN(sv))) {
1452 /* It claims to be longer than the space allocated for it -
1453 presuambly it's a variable name in the pad */
1457 ST(0) = newSVpvn_flags(p, len, SVs_TEMP | SvUTF8(sv));
1460 /* XXX for backward compatibility, but should fail */
1461 /* croak( "argument is not SvPOK" ); */
1462 ST(0) = sv_newmortal();
1465 # This used to read 257. I think that that was buggy - should have been 258.
1466 # (The "\0", the flags byte, and 256 for the table. Not that anything
1467 # anywhere calls this method. NWC.
1472 ST(0) = newSVpvn_flags(SvPVX_const(sv),
1473 SvCUR(sv) + (SvVALID(sv) ? 256 + PERL_FBM_TABLE_OFFSET : 0),
1485 MODULE = B PACKAGE = B::PVMG PREFIX = Sv
1490 MAGIC * mg = NO_INIT
1492 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic)
1493 XPUSHs(make_mg_object(aTHX_ mg));
1495 MODULE = B PACKAGE = B::PVMG
1501 MODULE = B PACKAGE = B::REGEXP
1503 #if PERL_VERSION >= 11
1509 /* FIXME - can we code this method more efficiently? */
1510 RETVAL = PTR2IV(sv);
1518 RETVAL = newSVpvn( RX_PRECOMP(sv), RX_PRELEN(sv) );
1524 #define MgMOREMAGIC(mg) mg->mg_moremagic
1525 #define MgPRIVATE(mg) mg->mg_private
1526 #define MgTYPE(mg) mg->mg_type
1527 #define MgFLAGS(mg) mg->mg_flags
1528 #define MgOBJ(mg) mg->mg_obj
1529 #define MgLENGTH(mg) mg->mg_len
1530 #define MgREGEX(mg) PTR2IV(mg->mg_obj)
1532 MODULE = B PACKAGE = B::MAGIC PREFIX = Mg
1538 if( MgMOREMAGIC(mg) ) {
1539 RETVAL = MgMOREMAGIC(mg);
1567 if(mg->mg_type == PERL_MAGIC_qr) {
1568 RETVAL = MgREGEX(mg);
1571 croak( "REGEX is only meaningful on r-magic" );
1580 if (mg->mg_type == PERL_MAGIC_qr) {
1581 REGEXP* rx = (REGEXP*)mg->mg_obj;
1584 RETVAL = newSVpvn( RX_PRECOMP(rx), RX_PRELEN(rx) );
1587 croak( "precomp is only meaningful on r-magic" );
1601 if (mg->mg_len >= 0){
1602 ST(0) = newSVpvn_flags(mg->mg_ptr, mg->mg_len, SVs_TEMP);
1603 } else if (mg->mg_len == HEf_SVKEY) {
1604 ST(0) = make_sv_object(aTHX_ NULL, (SV*)mg->mg_ptr);
1606 ST(0) = sv_newmortal();
1608 ST(0) = sv_newmortal();
1610 MODULE = B PACKAGE = B::PVLV PREFIX = Lv
1628 MODULE = B PACKAGE = B::BM PREFIX = Bm
1645 STRLEN len = NO_INIT
1646 char * str = NO_INIT
1648 str = SvPV(sv, len);
1649 /* Boyer-Moore table is just after string and its safety-margin \0 */
1650 ST(0) = newSVpvn_flags(str + len + PERL_FBM_TABLE_OFFSET, 256, SVs_TEMP);
1652 MODULE = B PACKAGE = B::GV PREFIX = Gv
1660 #if PERL_VERSION >= 10
1661 ST(0) = sv_2mortal(newSVhek(ix ? GvFILE_HEK(gv) : GvNAME_HEK(gv)));
1663 ST(0) = ix ? sv_2mortal(newSVpv(GvFILE(gv), 0))
1664 : newSVpvn_flags(GvNAME(gv), GvNAMELEN(gv), SVs_TEMP);
1674 #if PERL_VERSION >= 9
1675 RETVAL = isGV_with_GP(gv) ? TRUE : FALSE;
1677 RETVAL = TRUE; /* In 5.8 and earlier they all are. */
1680 RETVAL = GvGP(gv) == Null(GP*);
1693 #define GP_sv_ix SVp | offsetof(struct gp, gp_sv)
1694 #define GP_io_ix SVp | offsetof(struct gp, gp_io)
1695 #define GP_cv_ix SVp | offsetof(struct gp, gp_cv)
1696 #define GP_cvgen_ix U32p | offsetof(struct gp, gp_cvgen)
1697 #define GP_refcnt_ix U32p | offsetof(struct gp, gp_refcnt)
1698 #define GP_hv_ix SVp | offsetof(struct gp, gp_hv)
1699 #define GP_av_ix SVp | offsetof(struct gp, gp_av)
1700 #define GP_form_ix SVp | offsetof(struct gp, gp_form)
1701 #define GP_egv_ix SVp | offsetof(struct gp, gp_egv)
1702 #define GP_line_ix line_tp | offsetof(struct gp, gp_line)
1712 GvREFCNT = GP_refcnt_ix
1725 const GV *const gv = CvGV(cv);
1726 Perl_croak(aTHX_ "NULL gp in B::GV::%s", gv ? GvNAME(gv) : "???");
1728 ptr = (ix & 0xFFFF) + (char *)gp;
1729 switch ((U8)(ix >> 16)) {
1730 case (U8)(SVp >> 16):
1731 ret = make_sv_object(aTHX_ NULL, *((SV **)ptr));
1733 case (U8)(U32p >> 16):
1734 ret = sv_2mortal(newSVuv(*((U32*)ptr)));
1736 case (U8)(line_tp >> 16):
1737 ret = sv_2mortal(newSVuv(*((line_t *)ptr)));
1747 MODULE = B PACKAGE = B::GV
1753 MODULE = B PACKAGE = B::IO PREFIX = Io
1795 #if PERL_VERSION <= 8
1810 if( strEQ( name, "stdin" ) ) {
1811 handle = PerlIO_stdin();
1813 else if( strEQ( name, "stdout" ) ) {
1814 handle = PerlIO_stdout();
1816 else if( strEQ( name, "stderr" ) ) {
1817 handle = PerlIO_stderr();
1820 croak( "Invalid value '%s'", name );
1822 RETVAL = handle == IoIFP(io);
1826 MODULE = B PACKAGE = B::IO
1836 MODULE = B PACKAGE = B::AV PREFIX = Av
1850 if (AvFILL(av) >= 0) {
1851 SV **svp = AvARRAY(av);
1853 for (i = 0; i <= AvFILL(av); i++)
1854 XPUSHs(make_sv_object(aTHX_ NULL, svp[i]));
1862 if (idx >= 0 && AvFILL(av) >= 0 && idx <= AvFILL(av))
1863 XPUSHs(make_sv_object(aTHX_ NULL, (AvARRAY(av)[idx])));
1865 XPUSHs(make_sv_object(aTHX_ NULL, NULL));
1867 #if PERL_VERSION < 9
1869 #define AvOFF(av) ((XPVAV*)SvANY(av))->xof_off
1875 MODULE = B PACKAGE = B::AV
1883 MODULE = B PACKAGE = B::FM PREFIX = Fm
1889 MODULE = B PACKAGE = B::CV PREFIX = Cv
1905 RETVAL = CvISXSUB(cv) ? NULL : ix ? CvROOT(cv) : CvSTART(cv);
1937 ST(0) = sv_2mortal(newSViv(CvISXSUB(cv) ? PTR2IV(CvXSUB(cv)) : 0));
1945 ? make_sv_object(aTHX_ NULL, (SV *)CvXSUBANY(cv).any_ptr)
1946 : sv_2mortal(newSViv(CvISXSUB(cv) ? CvXSUBANY(cv).any_iv : 0));
1948 MODULE = B PACKAGE = B::CV
1954 MODULE = B PACKAGE = B::CV PREFIX = cv_
1961 MODULE = B PACKAGE = B::HV PREFIX = Hv
1983 #if PERL_VERSION < 9
1995 if (HvKEYS(hv) > 0) {
1999 (void)hv_iterinit(hv);
2000 EXTEND(sp, HvKEYS(hv) * 2);
2001 while ((sv = hv_iternextsv(hv, &key, &len))) {
2003 PUSHs(make_sv_object(aTHX_ NULL, sv));
2007 MODULE = B PACKAGE = B::HE PREFIX = He
2021 MODULE = B PACKAGE = B::RHE PREFIX = RHE_
2023 #if PERL_VERSION >= 9
2029 RETVAL = newRV( (SV*)cophh_2hv(h, 0) );