This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update CPANPLUS-Dist-Build to CPAN version 0.50
[perl5.git] / ext / B / B.xs
CommitLineData
a8a597b2
MB
1/* B.xs
2 *
3 * Copyright (c) 1996 Malcolm Beattie
4 *
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.
7 *
8 */
9
c5be433b 10#define PERL_NO_GET_CONTEXT
a8a597b2
MB
11#include "EXTERN.h"
12#include "perl.h"
13#include "XSUB.h"
a8a597b2 14
51aa15f3
GS
15#ifdef PerlIO
16typedef PerlIO * InputStream;
17#else
18typedef FILE * InputStream;
19#endif
20
21
27da23d5 22static const char* const svclassnames[] = {
a8a597b2 23 "B::NULL",
cecf5685
NC
24#if PERL_VERSION >= 9
25 "B::BIND",
26#endif
1cb9cd50 27 "B::IV",
b53eecb4 28 "B::NV",
4df7f6af
NC
29#if PERL_VERSION <= 10
30 "B::RV",
31#endif
a8a597b2
MB
32 "B::PV",
33 "B::PVIV",
34 "B::PVNV",
35 "B::PVMG",
cecf5685 36#if PERL_VERSION <= 8
a8a597b2 37 "B::BM",
cecf5685 38#endif
4df7f6af 39#if PERL_VERSION >= 11
5c35adbb 40 "B::REGEXP",
4df7f6af 41#endif
7252851f 42#if PERL_VERSION >= 9
4ce457a6 43 "B::GV",
7252851f 44#endif
a8a597b2
MB
45 "B::PVLV",
46 "B::AV",
47 "B::HV",
48 "B::CV",
7252851f
NC
49#if PERL_VERSION <= 8
50 "B::GV",
51#endif
a8a597b2
MB
52 "B::FM",
53 "B::IO",
54};
55
56typedef enum {
57 OPc_NULL, /* 0 */
58 OPc_BASEOP, /* 1 */
59 OPc_UNOP, /* 2 */
60 OPc_BINOP, /* 3 */
61 OPc_LOGOP, /* 4 */
1a67a97c
SM
62 OPc_LISTOP, /* 5 */
63 OPc_PMOP, /* 6 */
64 OPc_SVOP, /* 7 */
7934575e 65 OPc_PADOP, /* 8 */
1a67a97c 66 OPc_PVOP, /* 9 */
651aa52e
AE
67 OPc_LOOP, /* 10 */
68 OPc_COP /* 11 */
a8a597b2
MB
69} opclass;
70
27da23d5 71static const char* const opclassnames[] = {
a8a597b2
MB
72 "B::NULL",
73 "B::OP",
74 "B::UNOP",
75 "B::BINOP",
76 "B::LOGOP",
a8a597b2
MB
77 "B::LISTOP",
78 "B::PMOP",
79 "B::SVOP",
7934575e 80 "B::PADOP",
a8a597b2 81 "B::PVOP",
a8a597b2
MB
82 "B::LOOP",
83 "B::COP"
84};
85
27da23d5 86static const size_t opsizes[] = {
651aa52e
AE
87 0,
88 sizeof(OP),
89 sizeof(UNOP),
90 sizeof(BINOP),
91 sizeof(LOGOP),
92 sizeof(LISTOP),
93 sizeof(PMOP),
94 sizeof(SVOP),
95 sizeof(PADOP),
96 sizeof(PVOP),
97 sizeof(LOOP),
98 sizeof(COP)
99};
100
df3728a2 101#define MY_CXT_KEY "B::_guts" XS_VERSION
a8a597b2 102
89ca4ac7
JH
103typedef struct {
104 int x_walkoptree_debug; /* Flag for walkoptree debug hook */
b326da91 105 SV * x_specialsv_list[7];
89ca4ac7
JH
106} my_cxt_t;
107
108START_MY_CXT
109
110#define walkoptree_debug (MY_CXT.x_walkoptree_debug)
111#define specialsv_list (MY_CXT.x_specialsv_list)
e8edd1e6 112
a8a597b2 113static opclass
5d7488b2 114cc_opclass(pTHX_ const OP *o)
a8a597b2
MB
115{
116 if (!o)
117 return OPc_NULL;
118
119 if (o->op_type == 0)
120 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
121
122 if (o->op_type == OP_SASSIGN)
123 return ((o->op_private & OPpASSIGN_BACKWARDS) ? OPc_UNOP : OPc_BINOP);
124
c60fdceb
SM
125 if (o->op_type == OP_AELEMFAST) {
126 if (o->op_flags & OPf_SPECIAL)
127 return OPc_BASEOP;
128 else
129#ifdef USE_ITHREADS
130 return OPc_PADOP;
131#else
132 return OPc_SVOP;
133#endif
134 }
135
18228111 136#ifdef USE_ITHREADS
31b49ad4 137 if (o->op_type == OP_GV || o->op_type == OP_GVSV ||
c60fdceb 138 o->op_type == OP_RCATLINE)
18228111
GS
139 return OPc_PADOP;
140#endif
141
22c35a8c 142 switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
a8a597b2
MB
143 case OA_BASEOP:
144 return OPc_BASEOP;
145
146 case OA_UNOP:
147 return OPc_UNOP;
148
149 case OA_BINOP:
150 return OPc_BINOP;
151
152 case OA_LOGOP:
153 return OPc_LOGOP;
154
a8a597b2
MB
155 case OA_LISTOP:
156 return OPc_LISTOP;
157
158 case OA_PMOP:
159 return OPc_PMOP;
160
161 case OA_SVOP:
162 return OPc_SVOP;
163
7934575e
GS
164 case OA_PADOP:
165 return OPc_PADOP;
a8a597b2 166
293d3ffa
SM
167 case OA_PVOP_OR_SVOP:
168 /*
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).
174 */
175 return (o->op_private & (OPpTRANS_TO_UTF|OPpTRANS_FROM_UTF))
176 ? OPc_SVOP : OPc_PVOP;
a8a597b2
MB
177
178 case OA_LOOP:
179 return OPc_LOOP;
180
181 case OA_COP:
182 return OPc_COP;
183
184 case OA_BASEOP_OR_UNOP:
185 /*
186 * UNI(OP_foo) in toke.c returns token UNI or FUNC1 depending on
45f6cd40
SM
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.
a8a597b2 191 */
45f6cd40 192 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
a8a597b2
MB
193
194 case OA_FILESTATOP:
195 /*
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
7934575e 202 * an SVOP (and op_sv is the GV for the filehandle argument).
a8a597b2
MB
203 */
204 return ((o->op_flags & OPf_KIDS) ? OPc_UNOP :
93865851
GS
205#ifdef USE_ITHREADS
206 (o->op_flags & OPf_REF) ? OPc_PADOP : OPc_BASEOP);
207#else
7934575e 208 (o->op_flags & OPf_REF) ? OPc_SVOP : OPc_BASEOP);
93865851 209#endif
a8a597b2
MB
210 case OA_LOOPEXOP:
211 /*
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
218 * get set.
219 */
220 if (o->op_flags & OPf_STACKED)
221 return OPc_UNOP;
222 else if (o->op_flags & OPf_SPECIAL)
223 return OPc_BASEOP;
224 else
225 return OPc_PVOP;
226 }
227 warn("can't determine class of operator %s, assuming BASEOP\n",
22c35a8c 228 PL_op_name[o->op_type]);
a8a597b2
MB
229 return OPc_BASEOP;
230}
231
232static char *
5d7488b2 233cc_opclassname(pTHX_ const OP *o)
a8a597b2 234{
27da23d5 235 return (char *)opclassnames[cc_opclass(aTHX_ o)];
a8a597b2
MB
236}
237
9496d2e5
NC
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
240 argument. */
a8a597b2 241static SV *
cea2e8a9 242make_sv_object(pTHX_ SV *arg, SV *sv)
a8a597b2 243{
27da23d5 244 const char *type = 0;
a8a597b2 245 IV iv;
89ca4ac7 246 dMY_CXT;
9496d2e5
NC
247
248 if (!arg)
249 arg = sv_newmortal();
250
e8edd1e6
TH
251 for (iv = 0; iv < sizeof(specialsv_list)/sizeof(SV*); iv++) {
252 if (sv == specialsv_list[iv]) {
a8a597b2
MB
253 type = "B::SPECIAL";
254 break;
255 }
256 }
257 if (!type) {
258 type = svclassnames[SvTYPE(sv)];
56431972 259 iv = PTR2IV(sv);
a8a597b2
MB
260 }
261 sv_setiv(newSVrv(arg, type), iv);
262 return arg;
263}
264
e412117e 265#if PERL_VERSION >= 9
a8a597b2 266static SV *
9496d2e5 267make_temp_object(pTHX_ SV *temp)
8e01d9a6
NC
268{
269 SV *target;
9496d2e5 270 SV *arg = sv_newmortal();
8e01d9a6
NC
271 const char *const type = svclassnames[SvTYPE(temp)];
272 const IV iv = PTR2IV(temp);
273
274 target = newSVrv(arg, type);
275 sv_setiv(target, iv);
276
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
282 our reference. */
283 SvREFCNT_dec(temp);
284 return arg;
285}
286
287static SV *
9496d2e5 288make_warnings_object(pTHX_ STRLEN *warnings)
5c3c3f81
NC
289{
290 const char *type = 0;
291 dMY_CXT;
292 IV iv = sizeof(specialsv_list)/sizeof(SV*);
293
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 */
297
298 while (iv--) {
299 if ((SV*)warnings == specialsv_list[iv]) {
300 type = "B::SPECIAL";
301 break;
302 }
303 }
304 if (type) {
9496d2e5 305 SV *arg = sv_newmortal();
5c3c3f81 306 sv_setiv(newSVrv(arg, type), iv);
8e01d9a6 307 return arg;
5c3c3f81
NC
308 } else {
309 /* B assumes that warnings are a regular SV. Seems easier to keep it
310 happy by making them into a regular SV. */
9496d2e5 311 return make_temp_object(aTHX_ newSVpvn((char *)(warnings + 1), *warnings));
8e01d9a6
NC
312 }
313}
314
315static SV *
9496d2e5 316make_cop_io_object(pTHX_ COP *cop)
8e01d9a6 317{
8b850bd5
NC
318 SV *const value = newSV(0);
319
33972ad6 320 Perl_emulate_cop_io(aTHX_ cop, value);
8b850bd5
NC
321
322 if(SvOK(value)) {
23098a26 323 return make_sv_object(aTHX_ NULL, value);
8e01d9a6 324 } else {
8b850bd5 325 SvREFCNT_dec(value);
9496d2e5 326 return make_sv_object(aTHX_ NULL, NULL);
5c3c3f81 327 }
5c3c3f81 328}
e412117e 329#endif
5c3c3f81
NC
330
331static SV *
9496d2e5 332make_mg_object(pTHX_ MAGIC *mg)
a8a597b2 333{
9496d2e5 334 SV *arg = sv_newmortal();
56431972 335 sv_setiv(newSVrv(arg, "B::MAGIC"), PTR2IV(mg));
a8a597b2
MB
336 return arg;
337}
338
339static SV *
52ad86de 340cstring(pTHX_ SV *sv, bool perlstyle)
a8a597b2 341{
09e97b95 342 SV *sstr;
a8a597b2
MB
343
344 if (!SvOK(sv))
09e97b95
NC
345 return newSVpvs_flags("0", SVs_TEMP);
346
347 sstr = newSVpvs_flags("\"", SVs_TEMP);
348
349 if (perlstyle && SvUTF8(sv)) {
d79a7a3d 350 SV *tmpsv = sv_newmortal(); /* Temporary SV to feed sv_uni_display */
5d7488b2
AL
351 const STRLEN len = SvCUR(sv);
352 const char *s = sv_uni_display(tmpsv, sv, 8*len, UNI_DISPLAY_QQ);
d79a7a3d
RGS
353 while (*s)
354 {
355 if (*s == '"')
6beb30a6 356 sv_catpvs(sstr, "\\\"");
d79a7a3d 357 else if (*s == '$')
6beb30a6 358 sv_catpvs(sstr, "\\$");
d79a7a3d 359 else if (*s == '@')
6beb30a6 360 sv_catpvs(sstr, "\\@");
d79a7a3d
RGS
361 else if (*s == '\\')
362 {
363 if (strchr("nrftax\\",*(s+1)))
364 sv_catpvn(sstr, s++, 2);
365 else
6beb30a6 366 sv_catpvs(sstr, "\\\\");
d79a7a3d
RGS
367 }
368 else /* should always be printable */
369 sv_catpvn(sstr, s, 1);
370 ++s;
371 }
d79a7a3d 372 }
a8a597b2
MB
373 else
374 {
375 /* XXX Optimise? */
5d7488b2
AL
376 STRLEN len;
377 const char *s = SvPV(sv, len);
a8a597b2
MB
378 for (; len; len--, s++)
379 {
380 /* At least try a little for readability */
381 if (*s == '"')
6beb30a6 382 sv_catpvs(sstr, "\\\"");
a8a597b2 383 else if (*s == '\\')
6beb30a6 384 sv_catpvs(sstr, "\\\\");
b326da91 385 /* trigraphs - bleagh */
5d7488b2 386 else if (!perlstyle && *s == '?' && len>=3 && s[1] == '?') {
47bf35fa 387 Perl_sv_catpvf(aTHX_ sstr, "\\%03o", '?');
b326da91 388 }
52ad86de 389 else if (perlstyle && *s == '$')
6beb30a6 390 sv_catpvs(sstr, "\\$");
52ad86de 391 else if (perlstyle && *s == '@')
6beb30a6 392 sv_catpvs(sstr, "\\@");
ce561ef2
JH
393#ifdef EBCDIC
394 else if (isPRINT(*s))
395#else
396 else if (*s >= ' ' && *s < 127)
397#endif /* EBCDIC */
a8a597b2
MB
398 sv_catpvn(sstr, s, 1);
399 else if (*s == '\n')
6beb30a6 400 sv_catpvs(sstr, "\\n");
a8a597b2 401 else if (*s == '\r')
6beb30a6 402 sv_catpvs(sstr, "\\r");
a8a597b2 403 else if (*s == '\t')
6beb30a6 404 sv_catpvs(sstr, "\\t");
a8a597b2 405 else if (*s == '\a')
6beb30a6 406 sv_catpvs(sstr, "\\a");
a8a597b2 407 else if (*s == '\b')
6beb30a6 408 sv_catpvs(sstr, "\\b");
a8a597b2 409 else if (*s == '\f')
6beb30a6 410 sv_catpvs(sstr, "\\f");
52ad86de 411 else if (!perlstyle && *s == '\v')
6beb30a6 412 sv_catpvs(sstr, "\\v");
a8a597b2
MB
413 else
414 {
a8a597b2 415 /* Don't want promotion of a signed -1 char in sprintf args */
5d7488b2 416 const unsigned char c = (unsigned char) *s;
47bf35fa 417 Perl_sv_catpvf(aTHX_ sstr, "\\%03o", c);
a8a597b2
MB
418 }
419 /* XXX Add line breaks if string is long */
420 }
a8a597b2 421 }
09e97b95 422 sv_catpvs(sstr, "\"");
a8a597b2
MB
423 return sstr;
424}
425
426static SV *
cea2e8a9 427cchar(pTHX_ SV *sv)
a8a597b2 428{
422d053b 429 SV *sstr = newSVpvs_flags("'", SVs_TEMP);
5d7488b2 430 const char *s = SvPV_nolen(sv);
422d053b
NC
431 /* Don't want promotion of a signed -1 char in sprintf args */
432 const unsigned char c = (unsigned char) *s;
a8a597b2 433
422d053b 434 if (c == '\'')
6beb30a6 435 sv_catpvs(sstr, "\\'");
422d053b 436 else if (c == '\\')
6beb30a6 437 sv_catpvs(sstr, "\\\\");
ce561ef2 438#ifdef EBCDIC
422d053b 439 else if (isPRINT(c))
ce561ef2 440#else
422d053b 441 else if (c >= ' ' && c < 127)
ce561ef2 442#endif /* EBCDIC */
a8a597b2 443 sv_catpvn(sstr, s, 1);
422d053b 444 else if (c == '\n')
6beb30a6 445 sv_catpvs(sstr, "\\n");
422d053b 446 else if (c == '\r')
6beb30a6 447 sv_catpvs(sstr, "\\r");
422d053b 448 else if (c == '\t')
6beb30a6 449 sv_catpvs(sstr, "\\t");
422d053b 450 else if (c == '\a')
6beb30a6 451 sv_catpvs(sstr, "\\a");
422d053b 452 else if (c == '\b')
6beb30a6 453 sv_catpvs(sstr, "\\b");
422d053b 454 else if (c == '\f')
6beb30a6 455 sv_catpvs(sstr, "\\f");
422d053b 456 else if (c == '\v')
6beb30a6 457 sv_catpvs(sstr, "\\v");
a8a597b2 458 else
422d053b 459 Perl_sv_catpvf(aTHX_ sstr, "\\%03o", c);
6beb30a6 460 sv_catpvs(sstr, "'");
a8a597b2
MB
461 return sstr;
462}
463
8f3d514b
JC
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
467#else
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
472#endif
473
5d7488b2
AL
474static void
475walkoptree(pTHX_ SV *opsv, const char *method)
a8a597b2
MB
476{
477 dSP;
f3be9b72 478 OP *o, *kid;
89ca4ac7
JH
479 dMY_CXT;
480
a8a597b2
MB
481 if (!SvROK(opsv))
482 croak("opsv is not a reference");
483 opsv = sv_mortalcopy(opsv);
56431972 484 o = INT2PTR(OP*,SvIV((SV*)SvRV(opsv)));
a8a597b2
MB
485 if (walkoptree_debug) {
486 PUSHMARK(sp);
487 XPUSHs(opsv);
488 PUTBACK;
489 perl_call_method("walkoptree_debug", G_DISCARD);
490 }
491 PUSHMARK(sp);
492 XPUSHs(opsv);
493 PUTBACK;
494 perl_call_method(method, G_DISCARD);
495 if (o && (o->op_flags & OPf_KIDS)) {
a8a597b2
MB
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. */
56431972 498 sv_setiv(newSVrv(opsv, cc_opclassname(aTHX_ kid)), PTR2IV(kid));
cea2e8a9 499 walkoptree(aTHX_ opsv, method);
a8a597b2
MB
500 }
501 }
5464c149 502 if (o && (cc_opclass(aTHX_ o) == OPc_PMOP) && o->op_type != OP_PUSHRE
8f3d514b 503 && (kid = PMOP_pmreplroot(cPMOPo)))
f3be9b72 504 {
5464c149 505 sv_setiv(newSVrv(opsv, cc_opclassname(aTHX_ kid)), PTR2IV(kid));
f3be9b72
RGS
506 walkoptree(aTHX_ opsv, method);
507 }
a8a597b2
MB
508}
509
5d7488b2 510static SV **
1df34986
AE
511oplist(pTHX_ OP *o, SV **SP)
512{
513 for(; o; o = o->op_next) {
514 SV *opsv;
7252851f
NC
515#if PERL_VERSION >= 9
516 if (o->op_opt == 0)
1df34986 517 break;
2814eb74 518 o->op_opt = 0;
7252851f
NC
519#else
520 if (o->op_seq == 0)
521 break;
522 o->op_seq = 0;
523#endif
1df34986
AE
524 opsv = sv_newmortal();
525 sv_setiv(newSVrv(opsv, cc_opclassname(aTHX_ (OP*)o)), PTR2IV(o));
526 XPUSHs(opsv);
527 switch (o->op_type) {
528 case OP_SUBST:
8f3d514b 529 SP = oplist(aTHX_ PMOP_pmreplstart(cPMOPo), SP);
1df34986
AE
530 continue;
531 case OP_SORT:
f66c782a 532 if (o->op_flags & OPf_STACKED && o->op_flags & OPf_SPECIAL) {
1df34986
AE
533 OP *kid = cLISTOPo->op_first->op_sibling; /* pass pushmark */
534 kid = kUNOP->op_first; /* pass rv2gv */
535 kid = kUNOP->op_first; /* pass leave */
f66c782a 536 SP = oplist(aTHX_ kid->op_next, SP);
1df34986
AE
537 }
538 continue;
539 }
540 switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
541 case OA_LOGOP:
542 SP = oplist(aTHX_ cLOGOPo->op_other, SP);
543 break;
544 case OA_LOOP:
545 SP = oplist(aTHX_ cLOOPo->op_lastop, SP);
546 SP = oplist(aTHX_ cLOOPo->op_nextop, SP);
547 SP = oplist(aTHX_ cLOOPo->op_redoop, SP);
548 break;
549 }
550 }
551 return SP;
552}
553
a8a597b2
MB
554typedef OP *B__OP;
555typedef UNOP *B__UNOP;
556typedef BINOP *B__BINOP;
557typedef LOGOP *B__LOGOP;
a8a597b2
MB
558typedef LISTOP *B__LISTOP;
559typedef PMOP *B__PMOP;
560typedef SVOP *B__SVOP;
7934575e 561typedef PADOP *B__PADOP;
a8a597b2
MB
562typedef PVOP *B__PVOP;
563typedef LOOP *B__LOOP;
564typedef COP *B__COP;
565
566typedef SV *B__SV;
567typedef SV *B__IV;
568typedef SV *B__PV;
569typedef SV *B__NV;
570typedef SV *B__PVMG;
5c35adbb
NC
571#if PERL_VERSION >= 11
572typedef SV *B__REGEXP;
573#endif
a8a597b2
MB
574typedef SV *B__PVLV;
575typedef SV *B__BM;
576typedef SV *B__RV;
1df34986 577typedef SV *B__FM;
a8a597b2
MB
578typedef AV *B__AV;
579typedef HV *B__HV;
580typedef CV *B__CV;
581typedef GV *B__GV;
582typedef IO *B__IO;
583
584typedef MAGIC *B__MAGIC;
fd9f6265 585typedef HE *B__HE;
e412117e 586#if PERL_VERSION >= 9
fd9f6265 587typedef struct refcounted_he *B__RHE;
e412117e 588#endif
a8a597b2 589
b1826b71
NC
590#include "const-c.inc"
591
a8a597b2
MB
592MODULE = B PACKAGE = B PREFIX = B_
593
b1826b71
NC
594INCLUDE: const-xs.inc
595
a8a597b2
MB
596PROTOTYPES: DISABLE
597
598BOOT:
4c1f658f 599{
6beb30a6 600 HV *stash = gv_stashpvs("B", GV_ADD);
cbfd0a87 601 AV *export_ok = perl_get_av("B::EXPORT_OK", GV_ADD);
89ca4ac7 602 MY_CXT_INIT;
e8edd1e6
TH
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;
5c3c3f81
NC
607 specialsv_list[4] = (SV *) pWARN_ALL;
608 specialsv_list[5] = (SV *) pWARN_NONE;
609 specialsv_list[6] = (SV *) pWARN_STD;
f5ba1307 610#if PERL_VERSION <= 8
e6663653 611# define OPpPAD_STATE 0
7252851f 612#endif
4c1f658f 613}
a8a597b2 614
3280af22 615#define B_main_cv() PL_main_cv
31d7d75a 616#define B_init_av() PL_initav
651aa52e 617#define B_inc_gv() PL_incgv
ece599bd 618#define B_check_av() PL_checkav_save
e6663653
NC
619#if PERL_VERSION > 8
620# define B_unitcheck_av() PL_unitcheckav_save
621#else
622# define B_unitcheck_av() NULL
623#endif
059a8bb7
JH
624#define B_begin_av() PL_beginav_save
625#define B_end_av() PL_endav
3280af22
NIS
626#define B_main_root() PL_main_root
627#define B_main_start() PL_main_start
56eca212 628#define B_amagic_generation() PL_amagic_generation
5ce57cc0 629#define B_sub_generation() PL_sub_generation
651aa52e
AE
630#define B_defstash() PL_defstash
631#define B_curstash() PL_curstash
632#define B_dowarn() PL_dowarn
3280af22
NIS
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
1df34986 637#define B_formfeed() PL_formfeed
9d2bbe64
MB
638#ifdef USE_ITHREADS
639#define B_regex_padav() PL_regex_padav
640#endif
a8a597b2 641
31d7d75a
NIS
642B::AV
643B_init_av()
644
059a8bb7 645B::AV
ece599bd
RGS
646B_check_av()
647
e412117e
NC
648#if PERL_VERSION >= 9
649
ece599bd 650B::AV
676456c2
AG
651B_unitcheck_av()
652
e412117e
NC
653#endif
654
676456c2 655B::AV
059a8bb7
JH
656B_begin_av()
657
658B::AV
659B_end_av()
660
651aa52e
AE
661B::GV
662B_inc_gv()
663
9d2bbe64
MB
664#ifdef USE_ITHREADS
665
666B::AV
667B_regex_padav()
668
669#endif
670
a8a597b2
MB
671B::CV
672B_main_cv()
673
674B::OP
675B_main_root()
676
677B::OP
678B_main_start()
679
56eca212
GS
680long
681B_amagic_generation()
682
5ce57cc0
JJ
683long
684B_sub_generation()
685
a8a597b2
MB
686B::AV
687B_comppadlist()
688
689B::SV
690B_sv_undef()
691
692B::SV
693B_sv_yes()
694
695B::SV
696B_sv_no()
697
651aa52e
AE
698B::HV
699B_curstash()
700
701B::HV
702B_defstash()
a8a597b2 703
651aa52e
AE
704U8
705B_dowarn()
706
1df34986
AE
707B::SV
708B_formfeed()
709
651aa52e
AE
710void
711B_warnhook()
712 CODE:
9496d2e5 713 ST(0) = make_sv_object(aTHX_ NULL, PL_warnhook);
651aa52e
AE
714
715void
716B_diehook()
717 CODE:
9496d2e5 718 ST(0) = make_sv_object(aTHX_ NULL, PL_diehook);
651aa52e
AE
719
720MODULE = B PACKAGE = B
a8a597b2
MB
721
722void
723walkoptree(opsv, method)
724 SV * opsv
5d7488b2 725 const char * method
cea2e8a9
GS
726 CODE:
727 walkoptree(aTHX_ opsv, method);
a8a597b2
MB
728
729int
730walkoptree_debug(...)
731 CODE:
89ca4ac7 732 dMY_CXT;
a8a597b2
MB
733 RETVAL = walkoptree_debug;
734 if (items > 0 && SvTRUE(ST(1)))
735 walkoptree_debug = 1;
736 OUTPUT:
737 RETVAL
738
56431972 739#define address(sv) PTR2IV(sv)
a8a597b2
MB
740
741IV
742address(sv)
743 SV * sv
744
745B::SV
746svref_2object(sv)
747 SV * sv
748 CODE:
749 if (!SvROK(sv))
750 croak("argument is not a reference");
751 RETVAL = (SV*)SvRV(sv);
752 OUTPUT:
0cc1d052
NIS
753 RETVAL
754
755void
756opnumber(name)
5d7488b2 757const char * name
0cc1d052
NIS
758CODE:
759{
760 int i;
761 IV result = -1;
762 ST(0) = sv_newmortal();
763 if (strncmp(name,"pp_",3) == 0)
764 name += 3;
765 for (i = 0; i < PL_maxo; i++)
766 {
767 if (strcmp(name, PL_op_name[i]) == 0)
768 {
769 result = i;
770 break;
771 }
772 }
773 sv_setiv(ST(0),result);
774}
a8a597b2
MB
775
776void
777ppname(opnum)
778 int opnum
779 CODE:
780 ST(0) = sv_newmortal();
3280af22 781 if (opnum >= 0 && opnum < PL_maxo) {
6beb30a6 782 sv_setpvs(ST(0), "pp_");
22c35a8c 783 sv_catpv(ST(0), PL_op_name[opnum]);
a8a597b2
MB
784 }
785
786void
787hash(sv)
788 SV * sv
789 CODE:
a8a597b2
MB
790 STRLEN len;
791 U32 hash = 0;
8c5b7c71 792 const char *s = SvPVbyte(sv, len);
c32d3395 793 PERL_HASH(hash, s, len);
90b16320 794 ST(0) = sv_2mortal(Perl_newSVpvf(aTHX_ "0x%"UVxf, (UV)hash));
a8a597b2
MB
795
796#define cast_I32(foo) (I32)foo
797IV
798cast_I32(i)
799 IV i
800
801void
802minus_c()
803 CODE:
3280af22 804 PL_minus_c = TRUE;
a8a597b2 805
059a8bb7
JH
806void
807save_BEGINs()
808 CODE:
aefff11f 809 PL_savebegin = TRUE;
059a8bb7 810
a8a597b2
MB
811SV *
812cstring(sv)
813 SV * sv
84556172
NC
814 ALIAS:
815 perlstring = 1
9e380ad4 816 cchar = 2
09e97b95 817 PPCODE:
9e380ad4 818 PUSHs(ix == 2 ? cchar(aTHX_ sv) : cstring(aTHX_ sv, ix));
a8a597b2
MB
819
820void
821threadsv_names()
822 PPCODE:
f5ba1307
NC
823#if PERL_VERSION <= 8
824# ifdef USE_5005THREADS
825 int i;
5d7488b2 826 const STRLEN len = strlen(PL_threadsv_names);
f5ba1307
NC
827
828 EXTEND(sp, len);
829 for (i = 0; i < len; i++)
d3d34884 830 PUSHs(newSVpvn_flags(&PL_threadsv_names[i], 1, SVs_TEMP));
f5ba1307
NC
831# endif
832#endif
a8a597b2
MB
833
834#define OP_next(o) o->op_next
835#define OP_sibling(o) o->op_sibling
a8a597b2 836#define OP_targ(o) o->op_targ
a8a597b2
MB
837#define OP_flags(o) o->op_flags
838#define OP_private(o) o->op_private
839
840MODULE = B PACKAGE = B::OP PREFIX = OP_
841
651aa52e
AE
842size_t
843OP_size(o)
844 B::OP o
845 CODE:
846 RETVAL = opsizes[cc_opclass(aTHX_ o)];
847 OUTPUT:
848 RETVAL
849
a8a597b2
MB
850B::OP
851OP_next(o)
852 B::OP o
853
854B::OP
855OP_sibling(o)
856 B::OP o
857
858char *
3f872cb9
GS
859OP_name(o)
860 B::OP o
d2b33dc1
NC
861 ALIAS:
862 desc = 1
3f872cb9 863 CODE:
d2b33dc1 864 RETVAL = (char *)(ix ? PL_op_desc : PL_op_name)[o->op_type];
8063af02
DM
865 OUTPUT:
866 RETVAL
3f872cb9 867
8063af02 868void
a8a597b2
MB
869OP_ppaddr(o)
870 B::OP o
dc333d64
GS
871 PREINIT:
872 int i;
fdbd1d64 873 SV *sv = newSVpvs_flags("PL_ppaddr[OP_", SVs_TEMP);
a8a597b2 874 CODE:
dc333d64 875 sv_catpv(sv, PL_op_name[o->op_type]);
7c436af3 876 for (i=13; (STRLEN)i < SvCUR(sv); ++i)
dc333d64 877 SvPVX(sv)[i] = toUPPER(SvPVX(sv)[i]);
6beb30a6 878 sv_catpvs(sv, "]");
dc333d64 879 ST(0) = sv;
a8a597b2 880
7934575e 881PADOFFSET
a8a597b2
MB
882OP_targ(o)
883 B::OP o
884
7252851f 885#if PERL_VERSION >= 9
dd8be0e4
NC
886# These 3 are all bitfields, so we can't take their addresses.
887UV
888OP_type(o)
2814eb74 889 B::OP o
dd8be0e4
NC
890 ALIAS:
891 opt = 1
892 spare = 2
893 CODE:
894 switch(ix) {
895 case 1:
896 RETVAL = o->op_opt;
897 break;
898 case 2:
899 RETVAL = o->op_spare;
900 break;
901 default:
902 RETVAL = o->op_type;
903 }
904 OUTPUT:
905 RETVAL
2814eb74 906
7252851f
NC
907#else
908
dd8be0e4
NC
909UV
910OP_type(o)
7252851f 911 B::OP o
dd8be0e4
NC
912 ALIAS:
913 seq = 1
914 CODE:
915 switch(ix) {
916 case 1:
917 RETVAL = o->op_seq;
918 break;
919 default:
920 RETVAL = o->op_type;
921 }
922 OUTPUT:
923 RETVAL
7252851f
NC
924
925#endif
926
a8a597b2
MB
927U8
928OP_flags(o)
929 B::OP o
930
931U8
932OP_private(o)
933 B::OP o
934
1df34986
AE
935void
936OP_oplist(o)
937 B::OP o
938 PPCODE:
939 SP = oplist(aTHX_ o, SP);
940
a8a597b2
MB
941#define UNOP_first(o) o->op_first
942
943MODULE = B PACKAGE = B::UNOP PREFIX = UNOP_
944
945B::OP
946UNOP_first(o)
947 B::UNOP o
948
949#define BINOP_last(o) o->op_last
950
951MODULE = B PACKAGE = B::BINOP PREFIX = BINOP_
952
953B::OP
954BINOP_last(o)
955 B::BINOP o
956
957#define LOGOP_other(o) o->op_other
958
959MODULE = B PACKAGE = B::LOGOP PREFIX = LOGOP_
960
961B::OP
962LOGOP_other(o)
963 B::LOGOP o
964
a8a597b2
MB
965MODULE = B PACKAGE = B::LISTOP PREFIX = LISTOP_
966
c03c2844
SM
967U32
968LISTOP_children(o)
969 B::LISTOP o
970 OP * kid = NO_INIT
971 int i = NO_INIT
972 CODE:
c03c2844
SM
973 i = 0;
974 for (kid = o->op_first; kid; kid = kid->op_sibling)
975 i++;
8063af02
DM
976 RETVAL = i;
977 OUTPUT:
978 RETVAL
c03c2844 979
9d2bbe64
MB
980#ifdef USE_ITHREADS
981#define PMOP_pmoffset(o) o->op_pmoffset
29f2e912 982#define PMOP_pmstashpv(o) PmopSTASHPV(o);
651aa52e 983#else
29f2e912 984#define PMOP_pmstash(o) PmopSTASH(o);
9d2bbe64 985#endif
a8a597b2 986#define PMOP_pmflags(o) o->op_pmflags
a8a597b2
MB
987
988MODULE = B PACKAGE = B::PMOP PREFIX = PMOP_
989
20e98b0f
NC
990#if PERL_VERSION <= 8
991
a8a597b2
MB
992void
993PMOP_pmreplroot(o)
994 B::PMOP o
995 OP * root = NO_INIT
996 CODE:
997 ST(0) = sv_newmortal();
998 root = o->op_pmreplroot;
999 /* OP_PUSHRE stores an SV* instead of an OP* in op_pmreplroot */
1000 if (o->op_type == OP_PUSHRE) {
20e98b0f 1001# ifdef USE_ITHREADS
9d2bbe64 1002 sv_setiv(ST(0), INT2PTR(PADOFFSET,root) );
20e98b0f 1003# else
a8a597b2
MB
1004 sv_setiv(newSVrv(ST(0), root ?
1005 svclassnames[SvTYPE((SV*)root)] : "B::SV"),
56431972 1006 PTR2IV(root));
20e98b0f 1007# endif
a8a597b2
MB
1008 }
1009 else {
56431972 1010 sv_setiv(newSVrv(ST(0), cc_opclassname(aTHX_ root)), PTR2IV(root));
a8a597b2
MB
1011 }
1012
20e98b0f
NC
1013#else
1014
1015void
1016PMOP_pmreplroot(o)
1017 B::PMOP o
1018 CODE:
1019 ST(0) = sv_newmortal();
1020 if (o->op_type == OP_PUSHRE) {
1021# ifdef USE_ITHREADS
1022 sv_setiv(ST(0), o->op_pmreplrootu.op_pmtargetoff);
1023# else
1024 GV *const target = o->op_pmreplrootu.op_pmtargetgv;
1025 sv_setiv(newSVrv(ST(0), target ?
1026 svclassnames[SvTYPE((SV*)target)] : "B::SV"),
1027 PTR2IV(target));
1028# endif
1029 }
1030 else {
1031 OP *const root = o->op_pmreplrootu.op_pmreplroot;
1032 sv_setiv(newSVrv(ST(0), cc_opclassname(aTHX_ root)),
1033 PTR2IV(root));
1034 }
1035
1036#endif
1037
a8a597b2
MB
1038B::OP
1039PMOP_pmreplstart(o)
1040 B::PMOP o
1041
c2b1997a 1042#if PERL_VERSION < 9
e6070cd6 1043#define PMOP_pmnext(o) o->op_pmnext
c2b1997a 1044
a8a597b2
MB
1045B::PMOP
1046PMOP_pmnext(o)
1047 B::PMOP o
1048
c2b1997a
NC
1049#endif
1050
9d2bbe64
MB
1051#ifdef USE_ITHREADS
1052
1053IV
1054PMOP_pmoffset(o)
1055 B::PMOP o
1056
651aa52e
AE
1057char*
1058PMOP_pmstashpv(o)
1059 B::PMOP o
1060
1061#else
1062
1063B::HV
1064PMOP_pmstash(o)
1065 B::PMOP o
1066
9d2bbe64
MB
1067#endif
1068
6e21dc91 1069U32
a8a597b2
MB
1070PMOP_pmflags(o)
1071 B::PMOP o
1072
7c1f70cb
NC
1073#if PERL_VERSION < 9
1074
1075U32
1076PMOP_pmpermflags(o)
1077 B::PMOP o
1078
1079U8
1080PMOP_pmdynflags(o)
1081 B::PMOP o
1082
1083#endif
1084
a8a597b2
MB
1085void
1086PMOP_precomp(o)
1087 B::PMOP o
1088 REGEXP * rx = NO_INIT
1089 CODE:
1090 ST(0) = sv_newmortal();
aaa362c4 1091 rx = PM_GETRE(o);
a8a597b2 1092 if (rx)
220fc49f 1093 sv_setpvn(ST(0), RX_PRECOMP(rx), RX_PRELEN(rx));
a8a597b2 1094
7c1f70cb
NC
1095#if PERL_VERSION >= 9
1096
c737faaf
YO
1097void
1098PMOP_reflags(o)
1099 B::PMOP o
1100 REGEXP * rx = NO_INIT
1101 CODE:
1102 ST(0) = sv_newmortal();
1103 rx = PM_GETRE(o);
1104 if (rx)
07bc277f 1105 sv_setuv(ST(0), RX_EXTFLAGS(rx));
c737faaf 1106
7c1f70cb
NC
1107#endif
1108
ac33dcd1
JH
1109#define SVOP_sv(o) cSVOPo->op_sv
1110#define SVOP_gv(o) ((GV*)cSVOPo->op_sv)
a8a597b2
MB
1111
1112MODULE = B PACKAGE = B::SVOP PREFIX = SVOP_
1113
a8a597b2
MB
1114B::SV
1115SVOP_sv(o)
1116 B::SVOP o
1117
f22444f5 1118B::GV
065a1863
GS
1119SVOP_gv(o)
1120 B::SVOP o
1121
7934575e 1122#define PADOP_padix(o) o->op_padix
dd2155a4 1123#define PADOP_sv(o) (o->op_padix ? PAD_SVl(o->op_padix) : Nullsv)
7934575e 1124#define PADOP_gv(o) ((o->op_padix \
dd2155a4 1125 && SvTYPE(PAD_SVl(o->op_padix)) == SVt_PVGV) \
3ae1b226 1126 ? (GV*)PAD_SVl(o->op_padix) : (GV *)NULL)
a8a597b2 1127
7934575e
GS
1128MODULE = B PACKAGE = B::PADOP PREFIX = PADOP_
1129
1130PADOFFSET
1131PADOP_padix(o)
1132 B::PADOP o
1133
1134B::SV
1135PADOP_sv(o)
1136 B::PADOP o
a8a597b2
MB
1137
1138B::GV
7934575e
GS
1139PADOP_gv(o)
1140 B::PADOP o
a8a597b2
MB
1141
1142MODULE = B PACKAGE = B::PVOP PREFIX = PVOP_
1143
1144void
1145PVOP_pv(o)
1146 B::PVOP o
1147 CODE:
1148 /*
bec89253 1149 * OP_TRANS uses op_pv to point to a table of 256 or >=258 shorts
a8a597b2
MB
1150 * whereas other PVOPs point to a null terminated string.
1151 */
bec89253
RH
1152 if (o->op_type == OP_TRANS &&
1153 (o->op_private & OPpTRANS_COMPLEMENT) &&
1154 !(o->op_private & OPpTRANS_DELETE))
1155 {
5d7488b2
AL
1156 const short* const tbl = (short*)o->op_pv;
1157 const short entries = 257 + tbl[256];
d3d34884 1158 ST(0) = newSVpvn_flags(o->op_pv, entries * sizeof(short), SVs_TEMP);
bec89253
RH
1159 }
1160 else if (o->op_type == OP_TRANS) {
d3d34884 1161 ST(0) = newSVpvn_flags(o->op_pv, 256 * sizeof(short), SVs_TEMP);
bec89253
RH
1162 }
1163 else
d3d34884 1164 ST(0) = newSVpvn_flags(o->op_pv, strlen(o->op_pv), SVs_TEMP);
a8a597b2
MB
1165
1166#define LOOP_redoop(o) o->op_redoop
1167#define LOOP_nextop(o) o->op_nextop
1168#define LOOP_lastop(o) o->op_lastop
1169
1170MODULE = B PACKAGE = B::LOOP PREFIX = LOOP_
1171
1172
1173B::OP
1174LOOP_redoop(o)
1175 B::LOOP o
1176
1177B::OP
1178LOOP_nextop(o)
1179 B::LOOP o
1180
1181B::OP
1182LOOP_lastop(o)
1183 B::LOOP o
1184
4b65a919 1185#define COP_label(o) CopLABEL(o)
11faa288
GS
1186#define COP_stashpv(o) CopSTASHPV(o)
1187#define COP_stash(o) CopSTASH(o)
57843af0 1188#define COP_file(o) CopFILE(o)
1df34986 1189#define COP_filegv(o) CopFILEGV(o)
a8a597b2 1190#define COP_cop_seq(o) o->cop_seq
fc15ae8f 1191#define COP_arybase(o) CopARYBASE_get(o)
57843af0 1192#define COP_line(o) CopLINE(o)
d5ec2987 1193#define COP_hints(o) CopHINTS_get(o)
a8a597b2
MB
1194
1195MODULE = B PACKAGE = B::COP PREFIX = COP_
1196
d5b8ed54
NC
1197#if PERL_VERSION >= 11
1198
1199const char *
1200COP_label(o)
1201 B::COP o
1202
1203#else
1204
a8a597b2
MB
1205char *
1206COP_label(o)
1207 B::COP o
1208
d5b8ed54
NC
1209#endif
1210
11faa288
GS
1211char *
1212COP_stashpv(o)
1213 B::COP o
1214
a8a597b2
MB
1215B::HV
1216COP_stash(o)
1217 B::COP o
1218
57843af0
GS
1219char *
1220COP_file(o)
a8a597b2
MB
1221 B::COP o
1222
1df34986
AE
1223B::GV
1224COP_filegv(o)
1225 B::COP o
1226
1227
a8a597b2
MB
1228U32
1229COP_cop_seq(o)
1230 B::COP o
1231
1232I32
1233COP_arybase(o)
1234 B::COP o
1235
8bafa735 1236U32
a8a597b2
MB
1237COP_line(o)
1238 B::COP o
1239
5c3c3f81 1240void
b295d113
TH
1241COP_warnings(o)
1242 B::COP o
5c3c3f81 1243 PPCODE:
13d356f3 1244#if PERL_VERSION >= 9
9496d2e5 1245 ST(0) = make_warnings_object(aTHX_ o->cop_warnings);
13d356f3
NC
1246#else
1247 ST(0) = make_sv_object(aTHX_ NULL, o->cop_warnings);
1248#endif
5c3c3f81 1249 XSRETURN(1);
b295d113 1250
670f1322 1251void
6e6a1aef
RGS
1252COP_io(o)
1253 B::COP o
11bcd5da 1254 PPCODE:
13d356f3 1255#if PERL_VERSION >= 9
9496d2e5 1256 ST(0) = make_cop_io_object(aTHX_ o);
13d356f3
NC
1257#else
1258 ST(0) = make_sv_object(aTHX_ NULL, o->cop_io);
1259#endif
11bcd5da 1260 XSRETURN(1);
6e6a1aef 1261
13d356f3
NC
1262#if PERL_VERSION >= 9
1263
fd9f6265
JJ
1264B::RHE
1265COP_hints_hash(o)
1266 B::COP o
1267 CODE:
20439bc7 1268 RETVAL = CopHINTHASH_get(o);
fd9f6265
JJ
1269 OUTPUT:
1270 RETVAL
1271
e412117e
NC
1272#endif
1273
1274U32
1275COP_hints(o)
1276 B::COP o
1277
651aa52e
AE
1278MODULE = B PACKAGE = B::SV
1279
1280U32
1281SvTYPE(sv)
1282 B::SV sv
1283
429a5ce7
SM
1284#define object_2svref(sv) sv
1285#define SVREF SV *
1286
1287SVREF
1288object_2svref(sv)
1289 B::SV sv
1290
a8a597b2
MB
1291MODULE = B PACKAGE = B::SV PREFIX = Sv
1292
1293U32
1294SvREFCNT(sv)
1295 B::SV sv
1296
1297U32
1298SvFLAGS(sv)
1299 B::SV sv
1300
651aa52e
AE
1301U32
1302SvPOK(sv)
1303 B::SV sv
1304
1305U32
1306SvROK(sv)
1307 B::SV sv
1308
1309U32
1310SvMAGICAL(sv)
1311 B::SV sv
1312
a8a597b2
MB
1313MODULE = B PACKAGE = B::IV PREFIX = Sv
1314
1315IV
1316SvIV(sv)
1317 B::IV sv
1318
1319IV
1320SvIVX(sv)
1321 B::IV sv
1322
0ca04487
VB
1323UV
1324SvUVX(sv)
1325 B::IV sv
1326
1327
a8a597b2
MB
1328MODULE = B PACKAGE = B::IV
1329
1330#define needs64bits(sv) ((I32)SvIVX(sv) != SvIVX(sv))
1331
1332int
1333needs64bits(sv)
1334 B::IV sv
1335
1336void
1337packiv(sv)
1338 B::IV sv
1339 CODE:
1340 if (sizeof(IV) == 8) {
1341 U32 wp[2];
5d7488b2 1342 const IV iv = SvIVX(sv);
a8a597b2
MB
1343 /*
1344 * The following way of spelling 32 is to stop compilers on
1345 * 32-bit architectures from moaning about the shift count
1346 * being >= the width of the type. Such architectures don't
1347 * reach this code anyway (unless sizeof(IV) > 8 but then
1348 * everything else breaks too so I'm not fussed at the moment).
1349 */
42718184
RB
1350#ifdef UV_IS_QUAD
1351 wp[0] = htonl(((UV)iv) >> (sizeof(UV)*4));
1352#else
1353 wp[0] = htonl(((U32)iv) >> (sizeof(UV)*4));
1354#endif
a8a597b2 1355 wp[1] = htonl(iv & 0xffffffff);
d3d34884 1356 ST(0) = newSVpvn_flags((char *)wp, 8, SVs_TEMP);
a8a597b2
MB
1357 } else {
1358 U32 w = htonl((U32)SvIVX(sv));
d3d34884 1359 ST(0) = newSVpvn_flags((char *)&w, 4, SVs_TEMP);
a8a597b2
MB
1360 }
1361
4df7f6af
NC
1362
1363#if PERL_VERSION >= 11
1364
1365B::SV
1366RV(sv)
1367 B::IV sv
1368 CODE:
1369 if( SvROK(sv) ) {
1370 RETVAL = SvRV(sv);
1371 }
1372 else {
1373 croak( "argument is not SvROK" );
1374 }
1375 OUTPUT:
1376 RETVAL
1377
1378#endif
1379
a8a597b2
MB
1380MODULE = B PACKAGE = B::NV PREFIX = Sv
1381
76ef7183 1382NV
a8a597b2
MB
1383SvNV(sv)
1384 B::NV sv
1385
76ef7183 1386NV
a8a597b2
MB
1387SvNVX(sv)
1388 B::NV sv
1389
809abb02
NC
1390U32
1391COP_SEQ_RANGE_LOW(sv)
1392 B::NV sv
1393
1394U32
1395COP_SEQ_RANGE_HIGH(sv)
1396 B::NV sv
1397
1398U32
1399PARENT_PAD_INDEX(sv)
1400 B::NV sv
1401
1402U32
1403PARENT_FAKELEX_FLAGS(sv)
1404 B::NV sv
1405
4df7f6af
NC
1406#if PERL_VERSION < 11
1407
a8a597b2
MB
1408MODULE = B PACKAGE = B::RV PREFIX = Sv
1409
1410B::SV
1411SvRV(sv)
1412 B::RV sv
1413
4df7f6af
NC
1414#endif
1415
a8a597b2
MB
1416MODULE = B PACKAGE = B::PV PREFIX = Sv
1417
0b40bd6d
RH
1418char*
1419SvPVX(sv)
1420 B::PV sv
1421
b326da91
MB
1422B::SV
1423SvRV(sv)
1424 B::PV sv
1425 CODE:
1426 if( SvROK(sv) ) {
1427 RETVAL = SvRV(sv);
1428 }
1429 else {
1430 croak( "argument is not SvROK" );
1431 }
1432 OUTPUT:
1433 RETVAL
1434
a8a597b2
MB
1435void
1436SvPV(sv)
1437 B::PV sv
1438 CODE:
c0b20461 1439 if( SvPOK(sv) ) {
fdbd1d64
NC
1440 STRLEN len = SvCUR(sv);
1441 const char *p = SvPVX_const(sv);
b55685ae
NC
1442 /* FIXME - we need a better way for B to identify PVs that are
1443 in the pads as variable names. */
fdbd1d64 1444 if((SvLEN(sv) && len >= SvLEN(sv))) {
b55685ae
NC
1445 /* It claims to be longer than the space allocated for it -
1446 presuambly it's a variable name in the pad */
fdbd1d64 1447 len = strlen(p);
b55685ae 1448 }
fdbd1d64 1449 ST(0) = newSVpvn_flags(p, len, SVs_TEMP | SvUTF8(sv));
b326da91
MB
1450 }
1451 else {
1452 /* XXX for backward compatibility, but should fail */
1453 /* croak( "argument is not SvPOK" ); */
fdbd1d64 1454 ST(0) = sv_newmortal();
b326da91 1455 }
a8a597b2 1456
5a44e503
NC
1457# This used to read 257. I think that that was buggy - should have been 258.
1458# (The "\0", the flags byte, and 256 for the table. Not that anything
1459# anywhere calls this method. NWC.
651aa52e
AE
1460void
1461SvPVBM(sv)
1462 B::PV sv
1463 CODE:
fdbd1d64
NC
1464 ST(0) = newSVpvn_flags(SvPVX_const(sv),
1465 SvCUR(sv) + (SvVALID(sv) ? 256 + PERL_FBM_TABLE_OFFSET : 0),
1466 SVs_TEMP);
651aa52e
AE
1467
1468
445a12f6
DM
1469STRLEN
1470SvLEN(sv)
1471 B::PV sv
1472
1473STRLEN
1474SvCUR(sv)
1475 B::PV sv
1476
a8a597b2
MB
1477MODULE = B PACKAGE = B::PVMG PREFIX = Sv
1478
1479void
1480SvMAGIC(sv)
1481 B::PVMG sv
1482 MAGIC * mg = NO_INIT
1483 PPCODE:
1484 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic)
9496d2e5 1485 XPUSHs(make_mg_object(aTHX_ mg));
a8a597b2
MB
1486
1487MODULE = B PACKAGE = B::PVMG
1488
1489B::HV
1490SvSTASH(sv)
1491 B::PVMG sv
1492
5c35adbb
NC
1493MODULE = B PACKAGE = B::REGEXP
1494
1495#if PERL_VERSION >= 11
1496
1497IV
1498REGEX(sv)
07bc277f 1499 B::REGEXP sv
5c35adbb 1500 CODE:
288b8c02
NC
1501 /* FIXME - can we code this method more efficiently? */
1502 RETVAL = PTR2IV(sv);
5c35adbb
NC
1503 OUTPUT:
1504 RETVAL
1505
1506SV*
1507precomp(sv)
07bc277f 1508 B::REGEXP sv
5c35adbb 1509 CODE:
288b8c02 1510 RETVAL = newSVpvn( RX_PRECOMP(sv), RX_PRELEN(sv) );
5c35adbb
NC
1511 OUTPUT:
1512 RETVAL
1513
1514#endif
1515
a8a597b2
MB
1516#define MgMOREMAGIC(mg) mg->mg_moremagic
1517#define MgPRIVATE(mg) mg->mg_private
1518#define MgTYPE(mg) mg->mg_type
1519#define MgFLAGS(mg) mg->mg_flags
1520#define MgOBJ(mg) mg->mg_obj
88b39979 1521#define MgLENGTH(mg) mg->mg_len
bde7177d 1522#define MgREGEX(mg) PTR2IV(mg->mg_obj)
a8a597b2
MB
1523
1524MODULE = B PACKAGE = B::MAGIC PREFIX = Mg
1525
1526B::MAGIC
1527MgMOREMAGIC(mg)
1528 B::MAGIC mg
c5f0f3aa
RGS
1529 CODE:
1530 if( MgMOREMAGIC(mg) ) {
1531 RETVAL = MgMOREMAGIC(mg);
1532 }
1533 else {
1534 XSRETURN_UNDEF;
1535 }
1536 OUTPUT:
1537 RETVAL
a8a597b2
MB
1538
1539U16
1540MgPRIVATE(mg)
1541 B::MAGIC mg
1542
1543char
1544MgTYPE(mg)
1545 B::MAGIC mg
1546
1547U8
1548MgFLAGS(mg)
1549 B::MAGIC mg
1550
1551B::SV
1552MgOBJ(mg)
1553 B::MAGIC mg
b326da91 1554
9d2bbe64
MB
1555IV
1556MgREGEX(mg)
1557 B::MAGIC mg
1558 CODE:
a8248b05 1559 if(mg->mg_type == PERL_MAGIC_qr) {
9d2bbe64
MB
1560 RETVAL = MgREGEX(mg);
1561 }
1562 else {
1563 croak( "REGEX is only meaningful on r-magic" );
1564 }
1565 OUTPUT:
1566 RETVAL
1567
b326da91
MB
1568SV*
1569precomp(mg)
1570 B::MAGIC mg
1571 CODE:
a8248b05 1572 if (mg->mg_type == PERL_MAGIC_qr) {
b326da91 1573 REGEXP* rx = (REGEXP*)mg->mg_obj;
ef35129c 1574 RETVAL = Nullsv;
b326da91 1575 if( rx )
220fc49f 1576 RETVAL = newSVpvn( RX_PRECOMP(rx), RX_PRELEN(rx) );
b326da91
MB
1577 }
1578 else {
1579 croak( "precomp is only meaningful on r-magic" );
1580 }
1581 OUTPUT:
1582 RETVAL
a8a597b2 1583
88b39979
VB
1584I32
1585MgLENGTH(mg)
1586 B::MAGIC mg
1587
a8a597b2
MB
1588void
1589MgPTR(mg)
1590 B::MAGIC mg
1591 CODE:
88b39979
VB
1592 if (mg->mg_ptr){
1593 if (mg->mg_len >= 0){
fdbd1d64 1594 ST(0) = newSVpvn_flags(mg->mg_ptr, mg->mg_len, SVs_TEMP);
651aa52e 1595 } else if (mg->mg_len == HEf_SVKEY) {
9496d2e5 1596 ST(0) = make_sv_object(aTHX_ NULL, (SV*)mg->mg_ptr);
fdbd1d64
NC
1597 } else
1598 ST(0) = sv_newmortal();
1599 } else
1600 ST(0) = sv_newmortal();
a8a597b2
MB
1601
1602MODULE = B PACKAGE = B::PVLV PREFIX = Lv
1603
1604U32
1605LvTARGOFF(sv)
1606 B::PVLV sv
1607
1608U32
1609LvTARGLEN(sv)
1610 B::PVLV sv
1611
1612char
1613LvTYPE(sv)
1614 B::PVLV sv
1615
1616B::SV
1617LvTARG(sv)
1618 B::PVLV sv
1619
1620MODULE = B PACKAGE = B::BM PREFIX = Bm
1621
1622I32
1623BmUSEFUL(sv)
1624 B::BM sv
1625
85c508c3 1626U32
a8a597b2
MB
1627BmPREVIOUS(sv)
1628 B::BM sv
1629
1630U8
1631BmRARE(sv)
1632 B::BM sv
1633
1634void
1635BmTABLE(sv)
1636 B::BM sv
1637 STRLEN len = NO_INIT
1638 char * str = NO_INIT
1639 CODE:
1640 str = SvPV(sv, len);
1641 /* Boyer-Moore table is just after string and its safety-margin \0 */
d3d34884 1642 ST(0) = newSVpvn_flags(str + len + PERL_FBM_TABLE_OFFSET, 256, SVs_TEMP);
a8a597b2
MB
1643
1644MODULE = B PACKAGE = B::GV PREFIX = Gv
1645
1646void
1647GvNAME(gv)
1648 B::GV gv
1649 CODE:
6beb30a6
NC
1650#if PERL_VERSION >= 10
1651 ST(0) = sv_2mortal(newSVhek(GvNAME_HEK(gv)));
1652#else
d3d34884 1653 ST(0) = newSVpvn_flags(GvNAME(gv), GvNAMELEN(gv), SVs_TEMP);
6beb30a6 1654#endif
a8a597b2 1655
87d7fd28
GS
1656bool
1657is_empty(gv)
1658 B::GV gv
1659 CODE:
1660 RETVAL = GvGP(gv) == Null(GP*);
1661 OUTPUT:
1662 RETVAL
1663
50786ba8
NC
1664bool
1665isGV_with_GP(gv)
1666 B::GV gv
1667 CODE:
1668#if PERL_VERSION >= 9
1669 RETVAL = isGV_with_GP(gv) ? TRUE : FALSE;
1670#else
1671 RETVAL = TRUE; /* In 5.8 and earlier they all are. */
1672#endif
1673 OUTPUT:
1674 RETVAL
1675
651aa52e
AE
1676void*
1677GvGP(gv)
1678 B::GV gv
1679
a8a597b2
MB
1680B::HV
1681GvSTASH(gv)
1682 B::GV gv
1683
1684B::SV
1685GvSV(gv)
1686 B::GV gv
1687
1688B::IO
1689GvIO(gv)
1690 B::GV gv
1691
1df34986 1692B::FM
a8a597b2
MB
1693GvFORM(gv)
1694 B::GV gv
1df34986
AE
1695 CODE:
1696 RETVAL = (SV*)GvFORM(gv);
1697 OUTPUT:
1698 RETVAL
a8a597b2
MB
1699
1700B::AV
1701GvAV(gv)
1702 B::GV gv
1703
1704B::HV
1705GvHV(gv)
1706 B::GV gv
1707
1708B::GV
1709GvEGV(gv)
1710 B::GV gv
1711
1712B::CV
1713GvCV(gv)
1714 B::GV gv
1715
1716U32
1717GvCVGEN(gv)
1718 B::GV gv
1719
8bafa735 1720U32
a8a597b2
MB
1721GvLINE(gv)
1722 B::GV gv
1723
b195d487
GS
1724char *
1725GvFILE(gv)
1726 B::GV gv
1727
a8a597b2
MB
1728B::GV
1729GvFILEGV(gv)
1730 B::GV gv
1731
1732MODULE = B PACKAGE = B::GV
1733
1734U32
1735GvREFCNT(gv)
1736 B::GV gv
1737
1738U8
1739GvFLAGS(gv)
1740 B::GV gv
1741
1742MODULE = B PACKAGE = B::IO PREFIX = Io
1743
1744long
1745IoLINES(io)
1746 B::IO io
1747
1748long
1749IoPAGE(io)
1750 B::IO io
1751
1752long
1753IoPAGE_LEN(io)
1754 B::IO io
1755
1756long
1757IoLINES_LEFT(io)
1758 B::IO io
1759
1760char *
1761IoTOP_NAME(io)
1762 B::IO io
1763
1764B::GV
1765IoTOP_GV(io)
1766 B::IO io
1767
1768char *
1769IoFMT_NAME(io)
1770 B::IO io
1771
1772B::GV
1773IoFMT_GV(io)
1774 B::IO io
1775
1776char *
1777IoBOTTOM_NAME(io)
1778 B::IO io
1779
1780B::GV
1781IoBOTTOM_GV(io)
1782 B::IO io
1783
04071355
NC
1784#if PERL_VERSION <= 8
1785
a8a597b2
MB
1786short
1787IoSUBPROCESS(io)
1788 B::IO io
1789
04071355
NC
1790#endif
1791
b326da91
MB
1792bool
1793IsSTD(io,name)
1794 B::IO io
5d7488b2 1795 const char* name
b326da91
MB
1796 PREINIT:
1797 PerlIO* handle = 0;
1798 CODE:
1799 if( strEQ( name, "stdin" ) ) {
1800 handle = PerlIO_stdin();
1801 }
1802 else if( strEQ( name, "stdout" ) ) {
1803 handle = PerlIO_stdout();
1804 }
1805 else if( strEQ( name, "stderr" ) ) {
1806 handle = PerlIO_stderr();
1807 }
1808 else {
1809 croak( "Invalid value '%s'", name );
1810 }
1811 RETVAL = handle == IoIFP(io);
1812 OUTPUT:
1813 RETVAL
1814
a8a597b2
MB
1815MODULE = B PACKAGE = B::IO
1816
1817char
1818IoTYPE(io)
1819 B::IO io
1820
1821U8
1822IoFLAGS(io)
1823 B::IO io
1824
1825MODULE = B PACKAGE = B::AV PREFIX = Av
1826
1827SSize_t
1828AvFILL(av)
1829 B::AV av
1830
1831SSize_t
1832AvMAX(av)
1833 B::AV av
1834
edcc7c74
NC
1835#if PERL_VERSION < 9
1836
1837
1838#define AvOFF(av) ((XPVAV*)SvANY(av))->xof_off
1839
1840IV
1841AvOFF(av)
1842 B::AV av
1843
1844#endif
1845
a8a597b2
MB
1846void
1847AvARRAY(av)
1848 B::AV av
1849 PPCODE:
1850 if (AvFILL(av) >= 0) {
1851 SV **svp = AvARRAY(av);
1852 I32 i;
1853 for (i = 0; i <= AvFILL(av); i++)
9496d2e5 1854 XPUSHs(make_sv_object(aTHX_ NULL, svp[i]));
a8a597b2
MB
1855 }
1856
429a5ce7
SM
1857void
1858AvARRAYelt(av, idx)
1859 B::AV av
1860 int idx
1861 PPCODE:
1862 if (idx >= 0 && AvFILL(av) >= 0 && idx <= AvFILL(av))
9496d2e5 1863 XPUSHs(make_sv_object(aTHX_ NULL, (AvARRAY(av)[idx])));
429a5ce7 1864 else
9496d2e5 1865 XPUSHs(make_sv_object(aTHX_ NULL, NULL));
429a5ce7 1866
edcc7c74
NC
1867#if PERL_VERSION < 9
1868
1869MODULE = B PACKAGE = B::AV
1870
1871U8
1872AvFLAGS(av)
1873 B::AV av
1874
1875#endif
1876
1df34986
AE
1877MODULE = B PACKAGE = B::FM PREFIX = Fm
1878
1879IV
1880FmLINES(form)
1881 B::FM form
1882
a8a597b2
MB
1883MODULE = B PACKAGE = B::CV PREFIX = Cv
1884
651aa52e
AE
1885U32
1886CvCONST(cv)
1887 B::CV cv
1888
a8a597b2
MB
1889B::HV
1890CvSTASH(cv)
1891 B::CV cv
1892
1893B::OP
1894CvSTART(cv)
1895 B::CV cv
a0da4400
NC
1896 ALIAS:
1897 ROOT = 1
bf53b3a5 1898 CODE:
a0da4400 1899 RETVAL = CvISXSUB(cv) ? NULL : ix ? CvROOT(cv) : CvSTART(cv);
d04ba589
NC
1900 OUTPUT:
1901 RETVAL
a8a597b2
MB
1902
1903B::GV
1904CvGV(cv)
1905 B::CV cv
1906
57843af0
GS
1907char *
1908CvFILE(cv)
1909 B::CV cv
1910
a8a597b2
MB
1911long
1912CvDEPTH(cv)
1913 B::CV cv
1914
1915B::AV
1916CvPADLIST(cv)
1917 B::CV cv
1918
1919B::CV
1920CvOUTSIDE(cv)
1921 B::CV cv
1922
a3985cdc
DM
1923U32
1924CvOUTSIDE_SEQ(cv)
1925 B::CV cv
1926
a8a597b2
MB
1927void
1928CvXSUB(cv)
1929 B::CV cv
1930 CODE:
d04ba589 1931 ST(0) = sv_2mortal(newSViv(CvISXSUB(cv) ? PTR2IV(CvXSUB(cv)) : 0));
a8a597b2
MB
1932
1933
1934void
1935CvXSUBANY(cv)
1936 B::CV cv
1937 CODE:
9496d2e5
NC
1938 ST(0) = CvCONST(cv)
1939 ? make_sv_object(aTHX_ NULL, (SV *)CvXSUBANY(cv).any_ptr)
1940 : sv_2mortal(newSViv(CvISXSUB(cv) ? CvXSUBANY(cv).any_iv : 0));
a8a597b2 1941
5cfd8ad4
VB
1942MODULE = B PACKAGE = B::CV
1943
6aaf4108 1944U16
5cfd8ad4
VB
1945CvFLAGS(cv)
1946 B::CV cv
1947
de3f1649
JT
1948MODULE = B PACKAGE = B::CV PREFIX = cv_
1949
1950B::SV
1951cv_const_sv(cv)
1952 B::CV cv
1953
5cfd8ad4 1954
a8a597b2
MB
1955MODULE = B PACKAGE = B::HV PREFIX = Hv
1956
1957STRLEN
1958HvFILL(hv)
1959 B::HV hv
1960
1961STRLEN
1962HvMAX(hv)
1963 B::HV hv
1964
1965I32
1966HvKEYS(hv)
1967 B::HV hv
1968
1969I32
1970HvRITER(hv)
1971 B::HV hv
1972
1973char *
1974HvNAME(hv)
1975 B::HV hv
1976
edcc7c74
NC
1977#if PERL_VERSION < 9
1978
1979B::PMOP
1980HvPMROOT(hv)
1981 B::HV hv
1982
1983#endif
1984
a8a597b2
MB
1985void
1986HvARRAY(hv)
1987 B::HV hv
1988 PPCODE:
1989 if (HvKEYS(hv) > 0) {
1990 SV *sv;
1991 char *key;
1992 I32 len;
1993 (void)hv_iterinit(hv);
1994 EXTEND(sp, HvKEYS(hv) * 2);
8063af02 1995 while ((sv = hv_iternextsv(hv, &key, &len))) {
22f1178f 1996 mPUSHp(key, len);
9496d2e5 1997 PUSHs(make_sv_object(aTHX_ NULL, sv));
a8a597b2
MB
1998 }
1999 }
fd9f6265
JJ
2000
2001MODULE = B PACKAGE = B::HE PREFIX = He
2002
2003B::SV
2004HeVAL(he)
2005 B::HE he
2006
2007U32
2008HeHASH(he)
2009 B::HE he
2010
2011B::SV
2012HeSVKEY_force(he)
2013 B::HE he
2014
2015MODULE = B PACKAGE = B::RHE PREFIX = RHE_
2016
e412117e
NC
2017#if PERL_VERSION >= 9
2018
fd9f6265
JJ
2019SV*
2020RHE_HASH(h)
2021 B::RHE h
2022 CODE:
20439bc7 2023 RETVAL = newRV( (SV*)cophh_2hv(h, 0) );
fd9f6265
JJ
2024 OUTPUT:
2025 RETVAL
e412117e
NC
2026
2027#endif