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