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