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