This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
fix test that depends on op_dump() output
[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 15#ifdef PERL_OBJECT
22c35a8c
GS
16#undef PL_op_name
17#undef PL_opargs
18#undef PL_op_desc
0cb96387
GS
19#define PL_op_name (get_op_names())
20#define PL_opargs (get_opargs())
21#define PL_op_desc (get_op_descs())
51aa15f3
GS
22#endif
23
24#ifdef PerlIO
25typedef PerlIO * InputStream;
26#else
27typedef FILE * InputStream;
28#endif
29
30
a8a597b2
MB
31static char *svclassnames[] = {
32 "B::NULL",
33 "B::IV",
34 "B::NV",
35 "B::RV",
36 "B::PV",
37 "B::PVIV",
38 "B::PVNV",
39 "B::PVMG",
40 "B::BM",
41 "B::PVLV",
42 "B::AV",
43 "B::HV",
44 "B::CV",
45 "B::GV",
46 "B::FM",
47 "B::IO",
48};
49
50typedef enum {
51 OPc_NULL, /* 0 */
52 OPc_BASEOP, /* 1 */
53 OPc_UNOP, /* 2 */
54 OPc_BINOP, /* 3 */
55 OPc_LOGOP, /* 4 */
1a67a97c
SM
56 OPc_LISTOP, /* 5 */
57 OPc_PMOP, /* 6 */
58 OPc_SVOP, /* 7 */
7934575e 59 OPc_PADOP, /* 8 */
1a67a97c
SM
60 OPc_PVOP, /* 9 */
61 OPc_CVOP, /* 10 */
62 OPc_LOOP, /* 11 */
63 OPc_COP /* 12 */
a8a597b2
MB
64} opclass;
65
66static char *opclassnames[] = {
67 "B::NULL",
68 "B::OP",
69 "B::UNOP",
70 "B::BINOP",
71 "B::LOGOP",
a8a597b2
MB
72 "B::LISTOP",
73 "B::PMOP",
74 "B::SVOP",
7934575e 75 "B::PADOP",
a8a597b2
MB
76 "B::PVOP",
77 "B::CVOP",
78 "B::LOOP",
79 "B::COP"
80};
81
82static int walkoptree_debug = 0; /* Flag for walkoptree debug hook */
83
e8edd1e6
TH
84static SV *specialsv_list[4];
85
a8a597b2 86static opclass
cea2e8a9 87cc_opclass(pTHX_ OP *o)
a8a597b2
MB
88{
89 if (!o)
90 return OPc_NULL;
91
92 if (o->op_type == 0)
93 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
94
95 if (o->op_type == OP_SASSIGN)
96 return ((o->op_private & OPpASSIGN_BACKWARDS) ? OPc_UNOP : OPc_BINOP);
97
22c35a8c 98 switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
a8a597b2
MB
99 case OA_BASEOP:
100 return OPc_BASEOP;
101
102 case OA_UNOP:
103 return OPc_UNOP;
104
105 case OA_BINOP:
106 return OPc_BINOP;
107
108 case OA_LOGOP:
109 return OPc_LOGOP;
110
a8a597b2
MB
111 case OA_LISTOP:
112 return OPc_LISTOP;
113
114 case OA_PMOP:
115 return OPc_PMOP;
116
117 case OA_SVOP:
118 return OPc_SVOP;
119
7934575e
GS
120 case OA_PADOP:
121 return OPc_PADOP;
a8a597b2 122
293d3ffa
SM
123 case OA_PVOP_OR_SVOP:
124 /*
125 * Character translations (tr///) are usually a PVOP, keeping a
126 * pointer to a table of shorts used to look up translations.
127 * Under utf8, however, a simple table isn't practical; instead,
128 * the OP is an SVOP, and the SV is a reference to a swash
129 * (i.e., an RV pointing to an HV).
130 */
131 return (o->op_private & (OPpTRANS_TO_UTF|OPpTRANS_FROM_UTF))
132 ? OPc_SVOP : OPc_PVOP;
a8a597b2
MB
133
134 case OA_LOOP:
135 return OPc_LOOP;
136
137 case OA_COP:
138 return OPc_COP;
139
140 case OA_BASEOP_OR_UNOP:
141 /*
142 * UNI(OP_foo) in toke.c returns token UNI or FUNC1 depending on
45f6cd40
SM
143 * whether parens were seen. perly.y uses OPf_SPECIAL to
144 * signal whether a BASEOP had empty parens or none.
145 * Some other UNOPs are created later, though, so the best
146 * test is OPf_KIDS, which is set in newUNOP.
a8a597b2 147 */
45f6cd40 148 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
a8a597b2
MB
149
150 case OA_FILESTATOP:
151 /*
152 * The file stat OPs are created via UNI(OP_foo) in toke.c but use
153 * the OPf_REF flag to distinguish between OP types instead of the
154 * usual OPf_SPECIAL flag. As usual, if OPf_KIDS is set, then we
155 * return OPc_UNOP so that walkoptree can find our children. If
156 * OPf_KIDS is not set then we check OPf_REF. Without OPf_REF set
157 * (no argument to the operator) it's an OP; with OPf_REF set it's
7934575e 158 * an SVOP (and op_sv is the GV for the filehandle argument).
a8a597b2
MB
159 */
160 return ((o->op_flags & OPf_KIDS) ? OPc_UNOP :
7934575e 161 (o->op_flags & OPf_REF) ? OPc_SVOP : OPc_BASEOP);
a8a597b2
MB
162
163 case OA_LOOPEXOP:
164 /*
165 * next, last, redo, dump and goto use OPf_SPECIAL to indicate that a
166 * label was omitted (in which case it's a BASEOP) or else a term was
167 * seen. In this last case, all except goto are definitely PVOP but
168 * goto is either a PVOP (with an ordinary constant label), an UNOP
169 * with OPf_STACKED (with a non-constant non-sub) or an UNOP for
170 * OP_REFGEN (with goto &sub) in which case OPf_STACKED also seems to
171 * get set.
172 */
173 if (o->op_flags & OPf_STACKED)
174 return OPc_UNOP;
175 else if (o->op_flags & OPf_SPECIAL)
176 return OPc_BASEOP;
177 else
178 return OPc_PVOP;
179 }
180 warn("can't determine class of operator %s, assuming BASEOP\n",
22c35a8c 181 PL_op_name[o->op_type]);
a8a597b2
MB
182 return OPc_BASEOP;
183}
184
185static char *
cea2e8a9 186cc_opclassname(pTHX_ OP *o)
a8a597b2 187{
cea2e8a9 188 return opclassnames[cc_opclass(aTHX_ o)];
a8a597b2
MB
189}
190
191static SV *
cea2e8a9 192make_sv_object(pTHX_ SV *arg, SV *sv)
a8a597b2
MB
193{
194 char *type = 0;
195 IV iv;
196
e8edd1e6
TH
197 for (iv = 0; iv < sizeof(specialsv_list)/sizeof(SV*); iv++) {
198 if (sv == specialsv_list[iv]) {
a8a597b2
MB
199 type = "B::SPECIAL";
200 break;
201 }
202 }
203 if (!type) {
204 type = svclassnames[SvTYPE(sv)];
56431972 205 iv = PTR2IV(sv);
a8a597b2
MB
206 }
207 sv_setiv(newSVrv(arg, type), iv);
208 return arg;
209}
210
211static SV *
cea2e8a9 212make_mg_object(pTHX_ SV *arg, MAGIC *mg)
a8a597b2 213{
56431972 214 sv_setiv(newSVrv(arg, "B::MAGIC"), PTR2IV(mg));
a8a597b2
MB
215 return arg;
216}
217
218static SV *
cea2e8a9 219cstring(pTHX_ SV *sv)
a8a597b2 220{
79cb57f6 221 SV *sstr = newSVpvn("", 0);
a8a597b2
MB
222 STRLEN len;
223 char *s;
224
225 if (!SvOK(sv))
226 sv_setpvn(sstr, "0", 1);
227 else
228 {
229 /* XXX Optimise? */
230 s = SvPV(sv, len);
231 sv_catpv(sstr, "\"");
232 for (; len; len--, s++)
233 {
234 /* At least try a little for readability */
235 if (*s == '"')
236 sv_catpv(sstr, "\\\"");
237 else if (*s == '\\')
238 sv_catpv(sstr, "\\\\");
239 else if (*s >= ' ' && *s < 127) /* XXX not portable */
240 sv_catpvn(sstr, s, 1);
241 else if (*s == '\n')
242 sv_catpv(sstr, "\\n");
243 else if (*s == '\r')
244 sv_catpv(sstr, "\\r");
245 else if (*s == '\t')
246 sv_catpv(sstr, "\\t");
247 else if (*s == '\a')
248 sv_catpv(sstr, "\\a");
249 else if (*s == '\b')
250 sv_catpv(sstr, "\\b");
251 else if (*s == '\f')
252 sv_catpv(sstr, "\\f");
253 else if (*s == '\v')
254 sv_catpv(sstr, "\\v");
255 else
256 {
257 /* no trigraph support */
258 char escbuff[5]; /* to fit backslash, 3 octals + trailing \0 */
259 /* Don't want promotion of a signed -1 char in sprintf args */
260 unsigned char c = (unsigned char) *s;
261 sprintf(escbuff, "\\%03o", c);
262 sv_catpv(sstr, escbuff);
263 }
264 /* XXX Add line breaks if string is long */
265 }
266 sv_catpv(sstr, "\"");
267 }
268 return sstr;
269}
270
271static SV *
cea2e8a9 272cchar(pTHX_ SV *sv)
a8a597b2 273{
79cb57f6 274 SV *sstr = newSVpvn("'", 1);
2d8e6c8d
GS
275 STRLEN n_a;
276 char *s = SvPV(sv, n_a);
a8a597b2
MB
277
278 if (*s == '\'')
279 sv_catpv(sstr, "\\'");
280 else if (*s == '\\')
281 sv_catpv(sstr, "\\\\");
282 else if (*s >= ' ' && *s < 127) /* XXX not portable */
283 sv_catpvn(sstr, s, 1);
284 else if (*s == '\n')
285 sv_catpv(sstr, "\\n");
286 else if (*s == '\r')
287 sv_catpv(sstr, "\\r");
288 else if (*s == '\t')
289 sv_catpv(sstr, "\\t");
290 else if (*s == '\a')
291 sv_catpv(sstr, "\\a");
292 else if (*s == '\b')
293 sv_catpv(sstr, "\\b");
294 else if (*s == '\f')
295 sv_catpv(sstr, "\\f");
296 else if (*s == '\v')
297 sv_catpv(sstr, "\\v");
298 else
299 {
300 /* no trigraph support */
301 char escbuff[5]; /* to fit backslash, 3 octals + trailing \0 */
302 /* Don't want promotion of a signed -1 char in sprintf args */
303 unsigned char c = (unsigned char) *s;
304 sprintf(escbuff, "\\%03o", c);
305 sv_catpv(sstr, escbuff);
306 }
307 sv_catpv(sstr, "'");
308 return sstr;
309}
310
a8a597b2 311void
cea2e8a9 312walkoptree(pTHX_ SV *opsv, char *method)
a8a597b2
MB
313{
314 dSP;
315 OP *o;
316
317 if (!SvROK(opsv))
318 croak("opsv is not a reference");
319 opsv = sv_mortalcopy(opsv);
56431972 320 o = INT2PTR(OP*,SvIV((SV*)SvRV(opsv)));
a8a597b2
MB
321 if (walkoptree_debug) {
322 PUSHMARK(sp);
323 XPUSHs(opsv);
324 PUTBACK;
325 perl_call_method("walkoptree_debug", G_DISCARD);
326 }
327 PUSHMARK(sp);
328 XPUSHs(opsv);
329 PUTBACK;
330 perl_call_method(method, G_DISCARD);
331 if (o && (o->op_flags & OPf_KIDS)) {
332 OP *kid;
333 for (kid = ((UNOP*)o)->op_first; kid; kid = kid->op_sibling) {
334 /* Use the same opsv. Rely on methods not to mess it up. */
56431972 335 sv_setiv(newSVrv(opsv, cc_opclassname(aTHX_ kid)), PTR2IV(kid));
cea2e8a9 336 walkoptree(aTHX_ opsv, method);
a8a597b2
MB
337 }
338 }
339}
340
341typedef OP *B__OP;
342typedef UNOP *B__UNOP;
343typedef BINOP *B__BINOP;
344typedef LOGOP *B__LOGOP;
a8a597b2
MB
345typedef LISTOP *B__LISTOP;
346typedef PMOP *B__PMOP;
347typedef SVOP *B__SVOP;
7934575e 348typedef PADOP *B__PADOP;
a8a597b2
MB
349typedef PVOP *B__PVOP;
350typedef LOOP *B__LOOP;
351typedef COP *B__COP;
352
353typedef SV *B__SV;
354typedef SV *B__IV;
355typedef SV *B__PV;
356typedef SV *B__NV;
357typedef SV *B__PVMG;
358typedef SV *B__PVLV;
359typedef SV *B__BM;
360typedef SV *B__RV;
361typedef AV *B__AV;
362typedef HV *B__HV;
363typedef CV *B__CV;
364typedef GV *B__GV;
365typedef IO *B__IO;
366
367typedef MAGIC *B__MAGIC;
368
369MODULE = B PACKAGE = B PREFIX = B_
370
371PROTOTYPES: DISABLE
372
373BOOT:
4c1f658f
NIS
374{
375 HV *stash = gv_stashpvn("B", 1, TRUE);
376 AV *export_ok = perl_get_av("B::EXPORT_OK",TRUE);
e8edd1e6
TH
377 specialsv_list[0] = Nullsv;
378 specialsv_list[1] = &PL_sv_undef;
379 specialsv_list[2] = &PL_sv_yes;
380 specialsv_list[3] = &PL_sv_no;
4c1f658f
NIS
381#include "defsubs.h"
382}
a8a597b2 383
3280af22 384#define B_main_cv() PL_main_cv
31d7d75a 385#define B_init_av() PL_initav
3280af22
NIS
386#define B_main_root() PL_main_root
387#define B_main_start() PL_main_start
56eca212 388#define B_amagic_generation() PL_amagic_generation
3280af22
NIS
389#define B_comppadlist() (PL_main_cv ? CvPADLIST(PL_main_cv) : CvPADLIST(PL_compcv))
390#define B_sv_undef() &PL_sv_undef
391#define B_sv_yes() &PL_sv_yes
392#define B_sv_no() &PL_sv_no
a8a597b2 393
31d7d75a
NIS
394B::AV
395B_init_av()
396
a8a597b2
MB
397B::CV
398B_main_cv()
399
400B::OP
401B_main_root()
402
403B::OP
404B_main_start()
405
56eca212
GS
406long
407B_amagic_generation()
408
a8a597b2
MB
409B::AV
410B_comppadlist()
411
412B::SV
413B_sv_undef()
414
415B::SV
416B_sv_yes()
417
418B::SV
419B_sv_no()
420
421MODULE = B PACKAGE = B
422
423
424void
425walkoptree(opsv, method)
426 SV * opsv
427 char * method
cea2e8a9
GS
428 CODE:
429 walkoptree(aTHX_ opsv, method);
a8a597b2
MB
430
431int
432walkoptree_debug(...)
433 CODE:
434 RETVAL = walkoptree_debug;
435 if (items > 0 && SvTRUE(ST(1)))
436 walkoptree_debug = 1;
437 OUTPUT:
438 RETVAL
439
56431972 440#define address(sv) PTR2IV(sv)
a8a597b2
MB
441
442IV
443address(sv)
444 SV * sv
445
446B::SV
447svref_2object(sv)
448 SV * sv
449 CODE:
450 if (!SvROK(sv))
451 croak("argument is not a reference");
452 RETVAL = (SV*)SvRV(sv);
453 OUTPUT:
0cc1d052
NIS
454 RETVAL
455
456void
457opnumber(name)
458char * name
459CODE:
460{
461 int i;
462 IV result = -1;
463 ST(0) = sv_newmortal();
464 if (strncmp(name,"pp_",3) == 0)
465 name += 3;
466 for (i = 0; i < PL_maxo; i++)
467 {
468 if (strcmp(name, PL_op_name[i]) == 0)
469 {
470 result = i;
471 break;
472 }
473 }
474 sv_setiv(ST(0),result);
475}
a8a597b2
MB
476
477void
478ppname(opnum)
479 int opnum
480 CODE:
481 ST(0) = sv_newmortal();
3280af22 482 if (opnum >= 0 && opnum < PL_maxo) {
a8a597b2 483 sv_setpvn(ST(0), "pp_", 3);
22c35a8c 484 sv_catpv(ST(0), PL_op_name[opnum]);
a8a597b2
MB
485 }
486
487void
488hash(sv)
489 SV * sv
490 CODE:
491 char *s;
492 STRLEN len;
493 U32 hash = 0;
faccc32b 494 char hexhash[19]; /* must fit "0xffffffffffffffff" plus trailing \0 */
a8a597b2 495 s = SvPV(sv, len);
cf86991c 496 PERL_HASH(hash, s, len);
faccc32b 497 sprintf(hexhash, "0x%"UVxf, (UV)hash);
a8a597b2
MB
498 ST(0) = sv_2mortal(newSVpv(hexhash, 0));
499
500#define cast_I32(foo) (I32)foo
501IV
502cast_I32(i)
503 IV i
504
505void
506minus_c()
507 CODE:
3280af22 508 PL_minus_c = TRUE;
a8a597b2
MB
509
510SV *
511cstring(sv)
512 SV * sv
cea2e8a9
GS
513 CODE:
514 RETVAL = cstring(aTHX_ sv);
515 OUTPUT:
516 RETVAL
a8a597b2
MB
517
518SV *
519cchar(sv)
520 SV * sv
cea2e8a9
GS
521 CODE:
522 RETVAL = cchar(aTHX_ sv);
523 OUTPUT:
524 RETVAL
a8a597b2
MB
525
526void
527threadsv_names()
528 PPCODE:
529#ifdef USE_THREADS
530 int i;
533c011a 531 STRLEN len = strlen(PL_threadsv_names);
a8a597b2
MB
532
533 EXTEND(sp, len);
534 for (i = 0; i < len; i++)
79cb57f6 535 PUSHs(sv_2mortal(newSVpvn(&PL_threadsv_names[i], 1)));
a8a597b2
MB
536#endif
537
538
539#define OP_next(o) o->op_next
540#define OP_sibling(o) o->op_sibling
22c35a8c 541#define OP_desc(o) PL_op_desc[o->op_type]
a8a597b2
MB
542#define OP_targ(o) o->op_targ
543#define OP_type(o) o->op_type
544#define OP_seq(o) o->op_seq
545#define OP_flags(o) o->op_flags
546#define OP_private(o) o->op_private
547
548MODULE = B PACKAGE = B::OP PREFIX = OP_
549
550B::OP
551OP_next(o)
552 B::OP o
553
554B::OP
555OP_sibling(o)
556 B::OP o
557
558char *
3f872cb9
GS
559OP_name(o)
560 B::OP o
561 CODE:
562 ST(0) = sv_newmortal();
563 sv_setpv(ST(0), PL_op_name[o->op_type]);
564
565
566char *
a8a597b2
MB
567OP_ppaddr(o)
568 B::OP o
569 CODE:
570 ST(0) = sv_newmortal();
3f872cb9 571 sv_setpvn(ST(0), "Perl_pp_", 8);
22c35a8c 572 sv_catpv(ST(0), PL_op_name[o->op_type]);
a8a597b2
MB
573
574char *
575OP_desc(o)
576 B::OP o
577
7934575e 578PADOFFSET
a8a597b2
MB
579OP_targ(o)
580 B::OP o
581
582U16
583OP_type(o)
584 B::OP o
585
586U16
587OP_seq(o)
588 B::OP o
589
590U8
591OP_flags(o)
592 B::OP o
593
594U8
595OP_private(o)
596 B::OP o
597
598#define UNOP_first(o) o->op_first
599
600MODULE = B PACKAGE = B::UNOP PREFIX = UNOP_
601
602B::OP
603UNOP_first(o)
604 B::UNOP o
605
606#define BINOP_last(o) o->op_last
607
608MODULE = B PACKAGE = B::BINOP PREFIX = BINOP_
609
610B::OP
611BINOP_last(o)
612 B::BINOP o
613
614#define LOGOP_other(o) o->op_other
615
616MODULE = B PACKAGE = B::LOGOP PREFIX = LOGOP_
617
618B::OP
619LOGOP_other(o)
620 B::LOGOP o
621
a8a597b2
MB
622#define LISTOP_children(o) o->op_children
623
624MODULE = B PACKAGE = B::LISTOP PREFIX = LISTOP_
625
626U32
627LISTOP_children(o)
628 B::LISTOP o
629
630#define PMOP_pmreplroot(o) o->op_pmreplroot
631#define PMOP_pmreplstart(o) o->op_pmreplstart
632#define PMOP_pmnext(o) o->op_pmnext
633#define PMOP_pmregexp(o) o->op_pmregexp
634#define PMOP_pmflags(o) o->op_pmflags
635#define PMOP_pmpermflags(o) o->op_pmpermflags
636
637MODULE = B PACKAGE = B::PMOP PREFIX = PMOP_
638
639void
640PMOP_pmreplroot(o)
641 B::PMOP o
642 OP * root = NO_INIT
643 CODE:
644 ST(0) = sv_newmortal();
645 root = o->op_pmreplroot;
646 /* OP_PUSHRE stores an SV* instead of an OP* in op_pmreplroot */
647 if (o->op_type == OP_PUSHRE) {
648 sv_setiv(newSVrv(ST(0), root ?
649 svclassnames[SvTYPE((SV*)root)] : "B::SV"),
56431972 650 PTR2IV(root));
a8a597b2
MB
651 }
652 else {
56431972 653 sv_setiv(newSVrv(ST(0), cc_opclassname(aTHX_ root)), PTR2IV(root));
a8a597b2
MB
654 }
655
656B::OP
657PMOP_pmreplstart(o)
658 B::PMOP o
659
660B::PMOP
661PMOP_pmnext(o)
662 B::PMOP o
663
664U16
665PMOP_pmflags(o)
666 B::PMOP o
667
668U16
669PMOP_pmpermflags(o)
670 B::PMOP o
671
672void
673PMOP_precomp(o)
674 B::PMOP o
675 REGEXP * rx = NO_INIT
676 CODE:
677 ST(0) = sv_newmortal();
678 rx = o->op_pmregexp;
679 if (rx)
680 sv_setpvn(ST(0), rx->precomp, rx->prelen);
681
562d31b8
GS
682#define SVOP_sv(o) cSVOPx_sv(o)
683#define SVOP_gv(o) cGVOPx_gv(o)
a8a597b2
MB
684
685MODULE = B PACKAGE = B::SVOP PREFIX = SVOP_
686
a8a597b2
MB
687B::SV
688SVOP_sv(o)
689 B::SVOP o
690
7934575e
GS
691B::GV
692SVOP_gv(o)
693 B::SVOP o
a8a597b2 694
7934575e
GS
695#define PADOP_padix(o) o->op_padix
696#define PADOP_sv(o) (o->op_padix ? PL_curpad[o->op_padix] : Nullsv)
697#define PADOP_gv(o) ((o->op_padix \
698 && SvTYPE(PL_curpad[o->op_padix]) == SVt_PVGV) \
699 ? (GV*)PL_curpad[o->op_padix] : Nullgv)
a8a597b2 700
7934575e
GS
701MODULE = B PACKAGE = B::PADOP PREFIX = PADOP_
702
703PADOFFSET
704PADOP_padix(o)
705 B::PADOP o
706
707B::SV
708PADOP_sv(o)
709 B::PADOP o
a8a597b2
MB
710
711B::GV
7934575e
GS
712PADOP_gv(o)
713 B::PADOP o
a8a597b2
MB
714
715MODULE = B PACKAGE = B::PVOP PREFIX = PVOP_
716
717void
718PVOP_pv(o)
719 B::PVOP o
720 CODE:
721 /*
722 * OP_TRANS uses op_pv to point to a table of 256 shorts
723 * whereas other PVOPs point to a null terminated string.
724 */
725 ST(0) = sv_2mortal(newSVpv(o->op_pv, (o->op_type == OP_TRANS) ?
726 256 * sizeof(short) : 0));
727
728#define LOOP_redoop(o) o->op_redoop
729#define LOOP_nextop(o) o->op_nextop
730#define LOOP_lastop(o) o->op_lastop
731
732MODULE = B PACKAGE = B::LOOP PREFIX = LOOP_
733
734
735B::OP
736LOOP_redoop(o)
737 B::LOOP o
738
739B::OP
740LOOP_nextop(o)
741 B::LOOP o
742
743B::OP
744LOOP_lastop(o)
745 B::LOOP o
746
747#define COP_label(o) o->cop_label
11faa288
GS
748#define COP_stashpv(o) CopSTASHPV(o)
749#define COP_stash(o) CopSTASH(o)
57843af0 750#define COP_file(o) CopFILE(o)
a8a597b2
MB
751#define COP_cop_seq(o) o->cop_seq
752#define COP_arybase(o) o->cop_arybase
57843af0 753#define COP_line(o) CopLINE(o)
b295d113 754#define COP_warnings(o) o->cop_warnings
a8a597b2
MB
755
756MODULE = B PACKAGE = B::COP PREFIX = COP_
757
758char *
759COP_label(o)
760 B::COP o
761
11faa288
GS
762char *
763COP_stashpv(o)
764 B::COP o
765
a8a597b2
MB
766B::HV
767COP_stash(o)
768 B::COP o
769
57843af0
GS
770char *
771COP_file(o)
a8a597b2
MB
772 B::COP o
773
774U32
775COP_cop_seq(o)
776 B::COP o
777
778I32
779COP_arybase(o)
780 B::COP o
781
782U16
783COP_line(o)
784 B::COP o
785
b295d113
TH
786B::SV
787COP_warnings(o)
788 B::COP o
789
a8a597b2
MB
790MODULE = B PACKAGE = B::SV PREFIX = Sv
791
792U32
793SvREFCNT(sv)
794 B::SV sv
795
796U32
797SvFLAGS(sv)
798 B::SV sv
799
800MODULE = B PACKAGE = B::IV PREFIX = Sv
801
802IV
803SvIV(sv)
804 B::IV sv
805
806IV
807SvIVX(sv)
808 B::IV sv
809
0ca04487
VB
810UV
811SvUVX(sv)
812 B::IV sv
813
814
a8a597b2
MB
815MODULE = B PACKAGE = B::IV
816
817#define needs64bits(sv) ((I32)SvIVX(sv) != SvIVX(sv))
818
819int
820needs64bits(sv)
821 B::IV sv
822
823void
824packiv(sv)
825 B::IV sv
826 CODE:
827 if (sizeof(IV) == 8) {
828 U32 wp[2];
829 IV iv = SvIVX(sv);
830 /*
831 * The following way of spelling 32 is to stop compilers on
832 * 32-bit architectures from moaning about the shift count
833 * being >= the width of the type. Such architectures don't
834 * reach this code anyway (unless sizeof(IV) > 8 but then
835 * everything else breaks too so I'm not fussed at the moment).
836 */
42718184
RB
837#ifdef UV_IS_QUAD
838 wp[0] = htonl(((UV)iv) >> (sizeof(UV)*4));
839#else
840 wp[0] = htonl(((U32)iv) >> (sizeof(UV)*4));
841#endif
a8a597b2 842 wp[1] = htonl(iv & 0xffffffff);
79cb57f6 843 ST(0) = sv_2mortal(newSVpvn((char *)wp, 8));
a8a597b2
MB
844 } else {
845 U32 w = htonl((U32)SvIVX(sv));
79cb57f6 846 ST(0) = sv_2mortal(newSVpvn((char *)&w, 4));
a8a597b2
MB
847 }
848
849MODULE = B PACKAGE = B::NV PREFIX = Sv
850
851double
852SvNV(sv)
853 B::NV sv
854
855double
856SvNVX(sv)
857 B::NV sv
858
859MODULE = B PACKAGE = B::RV PREFIX = Sv
860
861B::SV
862SvRV(sv)
863 B::RV sv
864
865MODULE = B PACKAGE = B::PV PREFIX = Sv
866
867void
868SvPV(sv)
869 B::PV sv
870 CODE:
871 ST(0) = sv_newmortal();
872 sv_setpvn(ST(0), SvPVX(sv), SvCUR(sv));
873
445a12f6
DM
874STRLEN
875SvLEN(sv)
876 B::PV sv
877
878STRLEN
879SvCUR(sv)
880 B::PV sv
881
a8a597b2
MB
882MODULE = B PACKAGE = B::PVMG PREFIX = Sv
883
884void
885SvMAGIC(sv)
886 B::PVMG sv
887 MAGIC * mg = NO_INIT
888 PPCODE:
889 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic)
cea2e8a9 890 XPUSHs(make_mg_object(aTHX_ sv_newmortal(), mg));
a8a597b2
MB
891
892MODULE = B PACKAGE = B::PVMG
893
894B::HV
895SvSTASH(sv)
896 B::PVMG sv
897
898#define MgMOREMAGIC(mg) mg->mg_moremagic
899#define MgPRIVATE(mg) mg->mg_private
900#define MgTYPE(mg) mg->mg_type
901#define MgFLAGS(mg) mg->mg_flags
902#define MgOBJ(mg) mg->mg_obj
88b39979 903#define MgLENGTH(mg) mg->mg_len
a8a597b2
MB
904
905MODULE = B PACKAGE = B::MAGIC PREFIX = Mg
906
907B::MAGIC
908MgMOREMAGIC(mg)
909 B::MAGIC mg
910
911U16
912MgPRIVATE(mg)
913 B::MAGIC mg
914
915char
916MgTYPE(mg)
917 B::MAGIC mg
918
919U8
920MgFLAGS(mg)
921 B::MAGIC mg
922
923B::SV
924MgOBJ(mg)
925 B::MAGIC mg
926
88b39979
VB
927I32
928MgLENGTH(mg)
929 B::MAGIC mg
930
a8a597b2
MB
931void
932MgPTR(mg)
933 B::MAGIC mg
934 CODE:
935 ST(0) = sv_newmortal();
88b39979
VB
936 if (mg->mg_ptr){
937 if (mg->mg_len >= 0){
938 sv_setpvn(ST(0), mg->mg_ptr, mg->mg_len);
939 } else {
940 if (mg->mg_len == HEf_SVKEY)
941 sv_setsv(ST(0),newRV((SV*)mg->mg_ptr));
942 }
943 }
a8a597b2
MB
944
945MODULE = B PACKAGE = B::PVLV PREFIX = Lv
946
947U32
948LvTARGOFF(sv)
949 B::PVLV sv
950
951U32
952LvTARGLEN(sv)
953 B::PVLV sv
954
955char
956LvTYPE(sv)
957 B::PVLV sv
958
959B::SV
960LvTARG(sv)
961 B::PVLV sv
962
963MODULE = B PACKAGE = B::BM PREFIX = Bm
964
965I32
966BmUSEFUL(sv)
967 B::BM sv
968
969U16
970BmPREVIOUS(sv)
971 B::BM sv
972
973U8
974BmRARE(sv)
975 B::BM sv
976
977void
978BmTABLE(sv)
979 B::BM sv
980 STRLEN len = NO_INIT
981 char * str = NO_INIT
982 CODE:
983 str = SvPV(sv, len);
984 /* Boyer-Moore table is just after string and its safety-margin \0 */
79cb57f6 985 ST(0) = sv_2mortal(newSVpvn(str + len + 1, 256));
a8a597b2
MB
986
987MODULE = B PACKAGE = B::GV PREFIX = Gv
988
989void
990GvNAME(gv)
991 B::GV gv
992 CODE:
79cb57f6 993 ST(0) = sv_2mortal(newSVpvn(GvNAME(gv), GvNAMELEN(gv)));
a8a597b2
MB
994
995B::HV
996GvSTASH(gv)
997 B::GV gv
998
999B::SV
1000GvSV(gv)
1001 B::GV gv
1002
1003B::IO
1004GvIO(gv)
1005 B::GV gv
1006
1007B::CV
1008GvFORM(gv)
1009 B::GV gv
1010
1011B::AV
1012GvAV(gv)
1013 B::GV gv
1014
1015B::HV
1016GvHV(gv)
1017 B::GV gv
1018
1019B::GV
1020GvEGV(gv)
1021 B::GV gv
1022
1023B::CV
1024GvCV(gv)
1025 B::GV gv
1026
1027U32
1028GvCVGEN(gv)
1029 B::GV gv
1030
1031U16
1032GvLINE(gv)
1033 B::GV gv
1034
b195d487
GS
1035char *
1036GvFILE(gv)
1037 B::GV gv
1038
a8a597b2
MB
1039B::GV
1040GvFILEGV(gv)
1041 B::GV gv
1042
1043MODULE = B PACKAGE = B::GV
1044
1045U32
1046GvREFCNT(gv)
1047 B::GV gv
1048
1049U8
1050GvFLAGS(gv)
1051 B::GV gv
1052
1053MODULE = B PACKAGE = B::IO PREFIX = Io
1054
1055long
1056IoLINES(io)
1057 B::IO io
1058
1059long
1060IoPAGE(io)
1061 B::IO io
1062
1063long
1064IoPAGE_LEN(io)
1065 B::IO io
1066
1067long
1068IoLINES_LEFT(io)
1069 B::IO io
1070
1071char *
1072IoTOP_NAME(io)
1073 B::IO io
1074
1075B::GV
1076IoTOP_GV(io)
1077 B::IO io
1078
1079char *
1080IoFMT_NAME(io)
1081 B::IO io
1082
1083B::GV
1084IoFMT_GV(io)
1085 B::IO io
1086
1087char *
1088IoBOTTOM_NAME(io)
1089 B::IO io
1090
1091B::GV
1092IoBOTTOM_GV(io)
1093 B::IO io
1094
1095short
1096IoSUBPROCESS(io)
1097 B::IO io
1098
1099MODULE = B PACKAGE = B::IO
1100
1101char
1102IoTYPE(io)
1103 B::IO io
1104
1105U8
1106IoFLAGS(io)
1107 B::IO io
1108
1109MODULE = B PACKAGE = B::AV PREFIX = Av
1110
1111SSize_t
1112AvFILL(av)
1113 B::AV av
1114
1115SSize_t
1116AvMAX(av)
1117 B::AV av
1118
1119#define AvOFF(av) ((XPVAV*)SvANY(av))->xof_off
1120
1121IV
1122AvOFF(av)
1123 B::AV av
1124
1125void
1126AvARRAY(av)
1127 B::AV av
1128 PPCODE:
1129 if (AvFILL(av) >= 0) {
1130 SV **svp = AvARRAY(av);
1131 I32 i;
1132 for (i = 0; i <= AvFILL(av); i++)
cea2e8a9 1133 XPUSHs(make_sv_object(aTHX_ sv_newmortal(), svp[i]));
a8a597b2
MB
1134 }
1135
1136MODULE = B PACKAGE = B::AV
1137
1138U8
1139AvFLAGS(av)
1140 B::AV av
1141
1142MODULE = B PACKAGE = B::CV PREFIX = Cv
1143
1144B::HV
1145CvSTASH(cv)
1146 B::CV cv
1147
1148B::OP
1149CvSTART(cv)
1150 B::CV cv
1151
1152B::OP
1153CvROOT(cv)
1154 B::CV cv
1155
1156B::GV
1157CvGV(cv)
1158 B::CV cv
1159
57843af0
GS
1160char *
1161CvFILE(cv)
1162 B::CV cv
1163
a8a597b2
MB
1164long
1165CvDEPTH(cv)
1166 B::CV cv
1167
1168B::AV
1169CvPADLIST(cv)
1170 B::CV cv
1171
1172B::CV
1173CvOUTSIDE(cv)
1174 B::CV cv
1175
1176void
1177CvXSUB(cv)
1178 B::CV cv
1179 CODE:
56431972 1180 ST(0) = sv_2mortal(newSViv(PTR2IV(CvXSUB(cv))));
a8a597b2
MB
1181
1182
1183void
1184CvXSUBANY(cv)
1185 B::CV cv
1186 CODE:
1187 ST(0) = sv_2mortal(newSViv(CvXSUBANY(cv).any_iv));
1188
5cfd8ad4
VB
1189MODULE = B PACKAGE = B::CV
1190
1191U8
1192CvFLAGS(cv)
1193 B::CV cv
1194
1195
a8a597b2
MB
1196MODULE = B PACKAGE = B::HV PREFIX = Hv
1197
1198STRLEN
1199HvFILL(hv)
1200 B::HV hv
1201
1202STRLEN
1203HvMAX(hv)
1204 B::HV hv
1205
1206I32
1207HvKEYS(hv)
1208 B::HV hv
1209
1210I32
1211HvRITER(hv)
1212 B::HV hv
1213
1214char *
1215HvNAME(hv)
1216 B::HV hv
1217
1218B::PMOP
1219HvPMROOT(hv)
1220 B::HV hv
1221
1222void
1223HvARRAY(hv)
1224 B::HV hv
1225 PPCODE:
1226 if (HvKEYS(hv) > 0) {
1227 SV *sv;
1228 char *key;
1229 I32 len;
1230 (void)hv_iterinit(hv);
1231 EXTEND(sp, HvKEYS(hv) * 2);
1232 while (sv = hv_iternextsv(hv, &key, &len)) {
79cb57f6 1233 PUSHs(newSVpvn(key, len));
cea2e8a9 1234 PUSHs(make_sv_object(aTHX_ sv_newmortal(), sv));
a8a597b2
MB
1235 }
1236 }