bf389810911aa2f7ad5d88be65de5e9a39b0b82c
[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 #define PERL_NO_GET_CONTEXT
11 #include "EXTERN.h"
12 #include "perl.h"
13 #include "XSUB.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 (get_op_names())
20 #define PL_opargs (get_opargs())
21 #define PL_op_desc (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_LISTOP, /* 5 */
57     OPc_PMOP,   /* 6 */
58     OPc_SVOP,   /* 7 */
59     OPc_PADOP,  /* 8 */
60     OPc_PVOP,   /* 9 */
61     OPc_CVOP,   /* 10 */
62     OPc_LOOP,   /* 11 */
63     OPc_COP     /* 12 */
64 } opclass;
65
66 static char *opclassnames[] = {
67     "B::NULL",
68     "B::OP",
69     "B::UNOP",
70     "B::BINOP",
71     "B::LOGOP",
72     "B::LISTOP",
73     "B::PMOP",
74     "B::SVOP",
75     "B::PADOP",
76     "B::PVOP",
77     "B::CVOP",
78     "B::LOOP",
79     "B::COP"    
80 };
81
82 static int walkoptree_debug = 0;        /* Flag for walkoptree debug hook */
83
84 static SV *specialsv_list[4];
85
86 static opclass
87 cc_opclass(pTHX_ 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
98 #ifdef USE_ITHREADS
99     if (o->op_type == OP_GV || o->op_type == OP_GVSV || o->op_type == OP_AELEMFAST)
100         return OPc_PADOP;
101 #endif
102
103     switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
104     case OA_BASEOP:
105         return OPc_BASEOP;
106
107     case OA_UNOP:
108         return OPc_UNOP;
109
110     case OA_BINOP:
111         return OPc_BINOP;
112
113     case OA_LOGOP:
114         return OPc_LOGOP;
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_PADOP:
126         return OPc_PADOP;
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          * an SVOP (and op_sv is the GV for the filehandle argument).
164          */
165         return ((o->op_flags & OPf_KIDS) ? OPc_UNOP :
166                 (o->op_flags & OPf_REF) ? OPc_SVOP : 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(pTHX_ OP *o)
192 {
193     return opclassnames[cc_opclass(aTHX_ o)];
194 }
195
196 static SV *
197 make_sv_object(pTHX_ 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 = PTR2IV(sv);
211     }
212     sv_setiv(newSVrv(arg, type), iv);
213     return arg;
214 }
215
216 static SV *
217 make_mg_object(pTHX_ SV *arg, MAGIC *mg)
218 {
219     sv_setiv(newSVrv(arg, "B::MAGIC"), PTR2IV(mg));
220     return arg;
221 }
222
223 static SV *
224 cstring(pTHX_ 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(pTHX_ 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(pTHX_ 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 = INT2PTR(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(aTHX_ kid)), PTR2IV(kid));
341             walkoptree(aTHX_ 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 LISTOP  *B__LISTOP;
351 typedef PMOP    *B__PMOP;
352 typedef SVOP    *B__SVOP;
353 typedef PADOP   *B__PADOP;
354 typedef PVOP    *B__PVOP;
355 typedef LOOP    *B__LOOP;
356 typedef COP     *B__COP;
357
358 typedef SV      *B__SV;
359 typedef SV      *B__IV;
360 typedef SV      *B__PV;
361 typedef SV      *B__NV;
362 typedef SV      *B__PVMG;
363 typedef SV      *B__PVLV;
364 typedef SV      *B__BM;
365 typedef SV      *B__RV;
366 typedef AV      *B__AV;
367 typedef HV      *B__HV;
368 typedef CV      *B__CV;
369 typedef GV      *B__GV;
370 typedef IO      *B__IO;
371
372 typedef MAGIC   *B__MAGIC;
373
374 MODULE = B      PACKAGE = B     PREFIX = B_
375
376 PROTOTYPES: DISABLE
377
378 BOOT:
379 {
380     HV *stash = gv_stashpvn("B", 1, TRUE);
381     AV *export_ok = perl_get_av("B::EXPORT_OK",TRUE);
382     specialsv_list[0] = Nullsv;
383     specialsv_list[1] = &PL_sv_undef;
384     specialsv_list[2] = &PL_sv_yes;
385     specialsv_list[3] = &PL_sv_no;
386 #include "defsubs.h"
387 }
388
389 #define B_main_cv()     PL_main_cv
390 #define B_init_av()     PL_initav
391 #define B_main_root()   PL_main_root
392 #define B_main_start()  PL_main_start
393 #define B_amagic_generation()   PL_amagic_generation
394 #define B_comppadlist() (PL_main_cv ? CvPADLIST(PL_main_cv) : CvPADLIST(PL_compcv))
395 #define B_sv_undef()    &PL_sv_undef
396 #define B_sv_yes()      &PL_sv_yes
397 #define B_sv_no()       &PL_sv_no
398
399 B::AV
400 B_init_av()
401
402 B::CV
403 B_main_cv()
404
405 B::OP
406 B_main_root()
407
408 B::OP
409 B_main_start()
410
411 long 
412 B_amagic_generation()
413
414 B::AV
415 B_comppadlist()
416
417 B::SV
418 B_sv_undef()
419
420 B::SV
421 B_sv_yes()
422
423 B::SV
424 B_sv_no()
425
426 MODULE = B      PACKAGE = B
427
428
429 void
430 walkoptree(opsv, method)
431         SV *    opsv
432         char *  method
433     CODE:
434         walkoptree(aTHX_ opsv, method);
435
436 int
437 walkoptree_debug(...)
438     CODE:
439         RETVAL = walkoptree_debug;
440         if (items > 0 && SvTRUE(ST(1)))
441             walkoptree_debug = 1;
442     OUTPUT:
443         RETVAL
444
445 #define address(sv) PTR2IV(sv)
446
447 IV
448 address(sv)
449         SV *    sv
450
451 B::SV
452 svref_2object(sv)
453         SV *    sv
454     CODE:
455         if (!SvROK(sv))
456             croak("argument is not a reference");
457         RETVAL = (SV*)SvRV(sv);
458     OUTPUT:
459         RETVAL              
460
461 void
462 opnumber(name)
463 char *  name
464 CODE:
465 {
466  int i; 
467  IV  result = -1;
468  ST(0) = sv_newmortal();
469  if (strncmp(name,"pp_",3) == 0)
470    name += 3;
471  for (i = 0; i < PL_maxo; i++)
472   {
473    if (strcmp(name, PL_op_name[i]) == 0)
474     {
475      result = i;
476      break;
477     }
478   }
479  sv_setiv(ST(0),result);
480 }
481
482 void
483 ppname(opnum)
484         int     opnum
485     CODE:
486         ST(0) = sv_newmortal();
487         if (opnum >= 0 && opnum < PL_maxo) {
488             sv_setpvn(ST(0), "pp_", 3);
489             sv_catpv(ST(0), PL_op_name[opnum]);
490         }
491
492 void
493 hash(sv)
494         SV *    sv
495     CODE:
496         char *s;
497         STRLEN len;
498         U32 hash = 0;
499         char hexhash[19]; /* must fit "0xffffffffffffffff" plus trailing \0 */
500         s = SvPV(sv, len);
501         PERL_HASH(hash, s, len);
502         sprintf(hexhash, "0x%"UVxf, (UV)hash);
503         ST(0) = sv_2mortal(newSVpv(hexhash, 0));
504
505 #define cast_I32(foo) (I32)foo
506 IV
507 cast_I32(i)
508         IV      i
509
510 void
511 minus_c()
512     CODE:
513         PL_minus_c = TRUE;
514
515 SV *
516 cstring(sv)
517         SV *    sv
518     CODE:
519         RETVAL = cstring(aTHX_ sv);
520     OUTPUT:
521         RETVAL
522
523 SV *
524 cchar(sv)
525         SV *    sv
526     CODE:
527         RETVAL = cchar(aTHX_ sv);
528     OUTPUT:
529         RETVAL
530
531 void
532 threadsv_names()
533     PPCODE:
534 #ifdef USE_THREADS
535         int i;
536         STRLEN len = strlen(PL_threadsv_names);
537
538         EXTEND(sp, len);
539         for (i = 0; i < len; i++)
540             PUSHs(sv_2mortal(newSVpvn(&PL_threadsv_names[i], 1)));
541 #endif
542
543
544 #define OP_next(o)      o->op_next
545 #define OP_sibling(o)   o->op_sibling
546 #define OP_desc(o)      PL_op_desc[o->op_type]
547 #define OP_targ(o)      o->op_targ
548 #define OP_type(o)      o->op_type
549 #define OP_seq(o)       o->op_seq
550 #define OP_flags(o)     o->op_flags
551 #define OP_private(o)   o->op_private
552
553 MODULE = B      PACKAGE = B::OP         PREFIX = OP_
554
555 B::OP
556 OP_next(o)
557         B::OP           o
558
559 B::OP
560 OP_sibling(o)
561         B::OP           o
562
563 char *
564 OP_name(o)
565         B::OP           o
566     CODE:
567         ST(0) = sv_newmortal();
568         sv_setpv(ST(0), PL_op_name[o->op_type]);
569
570
571 char *
572 OP_ppaddr(o)
573         B::OP           o
574     PREINIT:
575         int i;
576         SV *sv = sv_newmortal();
577     CODE:
578         sv_setpvn(sv, "PL_ppaddr[OP_", 13);
579         sv_catpv(sv, PL_op_name[o->op_type]);
580         for (i=13; i<SvCUR(sv); ++i)
581             SvPVX(sv)[i] = toUPPER(SvPVX(sv)[i]);
582         sv_catpv(sv, "]");
583         ST(0) = sv;
584
585 char *
586 OP_desc(o)
587         B::OP           o
588
589 PADOFFSET
590 OP_targ(o)
591         B::OP           o
592
593 U16
594 OP_type(o)
595         B::OP           o
596
597 U16
598 OP_seq(o)
599         B::OP           o
600
601 U8
602 OP_flags(o)
603         B::OP           o
604
605 U8
606 OP_private(o)
607         B::OP           o
608
609 #define UNOP_first(o)   o->op_first
610
611 MODULE = B      PACKAGE = B::UNOP               PREFIX = UNOP_
612
613 B::OP 
614 UNOP_first(o)
615         B::UNOP o
616
617 #define BINOP_last(o)   o->op_last
618
619 MODULE = B      PACKAGE = B::BINOP              PREFIX = BINOP_
620
621 B::OP
622 BINOP_last(o)
623         B::BINOP        o
624
625 #define LOGOP_other(o)  o->op_other
626
627 MODULE = B      PACKAGE = B::LOGOP              PREFIX = LOGOP_
628
629 B::OP
630 LOGOP_other(o)
631         B::LOGOP        o
632
633 #define LISTOP_children(o)      o->op_children
634
635 MODULE = B      PACKAGE = B::LISTOP             PREFIX = LISTOP_
636
637 U32
638 LISTOP_children(o)
639         B::LISTOP       o
640
641 #define PMOP_pmreplroot(o)      o->op_pmreplroot
642 #define PMOP_pmreplstart(o)     o->op_pmreplstart
643 #define PMOP_pmnext(o)          o->op_pmnext
644 #define PMOP_pmregexp(o)        o->op_pmregexp
645 #define PMOP_pmflags(o)         o->op_pmflags
646 #define PMOP_pmpermflags(o)     o->op_pmpermflags
647
648 MODULE = B      PACKAGE = B::PMOP               PREFIX = PMOP_
649
650 void
651 PMOP_pmreplroot(o)
652         B::PMOP         o
653         OP *            root = NO_INIT
654     CODE:
655         ST(0) = sv_newmortal();
656         root = o->op_pmreplroot;
657         /* OP_PUSHRE stores an SV* instead of an OP* in op_pmreplroot */
658         if (o->op_type == OP_PUSHRE) {
659             sv_setiv(newSVrv(ST(0), root ?
660                              svclassnames[SvTYPE((SV*)root)] : "B::SV"),
661                      PTR2IV(root));
662         }
663         else {
664             sv_setiv(newSVrv(ST(0), cc_opclassname(aTHX_ root)), PTR2IV(root));
665         }
666
667 B::OP
668 PMOP_pmreplstart(o)
669         B::PMOP         o
670
671 B::PMOP
672 PMOP_pmnext(o)
673         B::PMOP         o
674
675 U16
676 PMOP_pmflags(o)
677         B::PMOP         o
678
679 U16
680 PMOP_pmpermflags(o)
681         B::PMOP         o
682
683 void
684 PMOP_precomp(o)
685         B::PMOP         o
686         REGEXP *        rx = NO_INIT
687     CODE:
688         ST(0) = sv_newmortal();
689         rx = o->op_pmregexp;
690         if (rx)
691             sv_setpvn(ST(0), rx->precomp, rx->prelen);
692
693 #define SVOP_sv(o)      cSVOPo->op_sv
694 #define SVOP_gv(o)      ((GV*)cSVOPo->op_sv)
695
696 MODULE = B      PACKAGE = B::SVOP               PREFIX = SVOP_
697
698 B::SV
699 SVOP_sv(o)
700         B::SVOP o
701
702 B::SV
703 SVOP_gv(o)
704         B::SVOP o
705
706 #define PADOP_padix(o)  o->op_padix
707 #define PADOP_sv(o)     (o->op_padix ? PL_curpad[o->op_padix] : Nullsv)
708 #define PADOP_gv(o)     ((o->op_padix \
709                           && SvTYPE(PL_curpad[o->op_padix]) == SVt_PVGV) \
710                          ? (GV*)PL_curpad[o->op_padix] : Nullgv)
711
712 MODULE = B      PACKAGE = B::PADOP              PREFIX = PADOP_
713
714 PADOFFSET
715 PADOP_padix(o)
716         B::PADOP o
717
718 B::SV
719 PADOP_sv(o)
720         B::PADOP o
721
722 B::GV
723 PADOP_gv(o)
724         B::PADOP o
725
726 MODULE = B      PACKAGE = B::PVOP               PREFIX = PVOP_
727
728 void
729 PVOP_pv(o)
730         B::PVOP o
731     CODE:
732         /*
733          * OP_TRANS uses op_pv to point to a table of 256 shorts
734          * whereas other PVOPs point to a null terminated string.
735          */
736         ST(0) = sv_2mortal(newSVpv(o->op_pv, (o->op_type == OP_TRANS) ?
737                                    256 * sizeof(short) : 0));
738
739 #define LOOP_redoop(o)  o->op_redoop
740 #define LOOP_nextop(o)  o->op_nextop
741 #define LOOP_lastop(o)  o->op_lastop
742
743 MODULE = B      PACKAGE = B::LOOP               PREFIX = LOOP_
744
745
746 B::OP
747 LOOP_redoop(o)
748         B::LOOP o
749
750 B::OP
751 LOOP_nextop(o)
752         B::LOOP o
753
754 B::OP
755 LOOP_lastop(o)
756         B::LOOP o
757
758 #define COP_label(o)    o->cop_label
759 #define COP_stashpv(o)  CopSTASHPV(o)
760 #define COP_stash(o)    CopSTASH(o)
761 #define COP_file(o)     CopFILE(o)
762 #define COP_cop_seq(o)  o->cop_seq
763 #define COP_arybase(o)  o->cop_arybase
764 #define COP_line(o)     CopLINE(o)
765 #define COP_warnings(o) o->cop_warnings
766
767 MODULE = B      PACKAGE = B::COP                PREFIX = COP_
768
769 char *
770 COP_label(o)
771         B::COP  o
772
773 char *
774 COP_stashpv(o)
775         B::COP  o
776
777 B::HV
778 COP_stash(o)
779         B::COP  o
780
781 char *
782 COP_file(o)
783         B::COP  o
784
785 U32
786 COP_cop_seq(o)
787         B::COP  o
788
789 I32
790 COP_arybase(o)
791         B::COP  o
792
793 U16
794 COP_line(o)
795         B::COP  o
796
797 B::SV
798 COP_warnings(o)
799         B::COP  o
800
801 MODULE = B      PACKAGE = B::SV         PREFIX = Sv
802
803 U32
804 SvREFCNT(sv)
805         B::SV   sv
806
807 U32
808 SvFLAGS(sv)
809         B::SV   sv
810
811 MODULE = B      PACKAGE = B::IV         PREFIX = Sv
812
813 IV
814 SvIV(sv)
815         B::IV   sv
816
817 IV
818 SvIVX(sv)
819         B::IV   sv
820
821 UV 
822 SvUVX(sv) 
823         B::IV   sv
824                       
825
826 MODULE = B      PACKAGE = B::IV
827
828 #define needs64bits(sv) ((I32)SvIVX(sv) != SvIVX(sv))
829
830 int
831 needs64bits(sv)
832         B::IV   sv
833
834 void
835 packiv(sv)
836         B::IV   sv
837     CODE:
838         if (sizeof(IV) == 8) {
839             U32 wp[2];
840             IV iv = SvIVX(sv);
841             /*
842              * The following way of spelling 32 is to stop compilers on
843              * 32-bit architectures from moaning about the shift count
844              * being >= the width of the type. Such architectures don't
845              * reach this code anyway (unless sizeof(IV) > 8 but then
846              * everything else breaks too so I'm not fussed at the moment).
847              */
848 #ifdef UV_IS_QUAD
849             wp[0] = htonl(((UV)iv) >> (sizeof(UV)*4));
850 #else
851             wp[0] = htonl(((U32)iv) >> (sizeof(UV)*4));
852 #endif
853             wp[1] = htonl(iv & 0xffffffff);
854             ST(0) = sv_2mortal(newSVpvn((char *)wp, 8));
855         } else {
856             U32 w = htonl((U32)SvIVX(sv));
857             ST(0) = sv_2mortal(newSVpvn((char *)&w, 4));
858         }
859
860 MODULE = B      PACKAGE = B::NV         PREFIX = Sv
861
862 double
863 SvNV(sv)
864         B::NV   sv
865
866 double
867 SvNVX(sv)
868         B::NV   sv
869
870 MODULE = B      PACKAGE = B::RV         PREFIX = Sv
871
872 B::SV
873 SvRV(sv)
874         B::RV   sv
875
876 MODULE = B      PACKAGE = B::PV         PREFIX = Sv
877
878 void
879 SvPV(sv)
880         B::PV   sv
881     CODE:
882         ST(0) = sv_newmortal();
883         sv_setpvn(ST(0), SvPVX(sv), SvCUR(sv));
884
885 STRLEN
886 SvLEN(sv)
887         B::PV   sv
888
889 STRLEN
890 SvCUR(sv)
891         B::PV   sv
892
893 MODULE = B      PACKAGE = B::PVMG       PREFIX = Sv
894
895 void
896 SvMAGIC(sv)
897         B::PVMG sv
898         MAGIC * mg = NO_INIT
899     PPCODE:
900         for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic)
901             XPUSHs(make_mg_object(aTHX_ sv_newmortal(), mg));
902
903 MODULE = B      PACKAGE = B::PVMG
904
905 B::HV
906 SvSTASH(sv)
907         B::PVMG sv
908
909 #define MgMOREMAGIC(mg) mg->mg_moremagic
910 #define MgPRIVATE(mg) mg->mg_private
911 #define MgTYPE(mg) mg->mg_type
912 #define MgFLAGS(mg) mg->mg_flags
913 #define MgOBJ(mg) mg->mg_obj
914 #define MgLENGTH(mg) mg->mg_len
915
916 MODULE = B      PACKAGE = B::MAGIC      PREFIX = Mg     
917
918 B::MAGIC
919 MgMOREMAGIC(mg)
920         B::MAGIC        mg
921
922 U16
923 MgPRIVATE(mg)
924         B::MAGIC        mg
925
926 char
927 MgTYPE(mg)
928         B::MAGIC        mg
929
930 U8
931 MgFLAGS(mg)
932         B::MAGIC        mg
933
934 B::SV
935 MgOBJ(mg)
936         B::MAGIC        mg
937
938 I32 
939 MgLENGTH(mg)
940         B::MAGIC        mg
941  
942 void
943 MgPTR(mg)
944         B::MAGIC        mg
945     CODE:
946         ST(0) = sv_newmortal();
947         if (mg->mg_ptr){
948                 if (mg->mg_len >= 0){
949                         sv_setpvn(ST(0), mg->mg_ptr, mg->mg_len);
950                 } else {
951                         if (mg->mg_len == HEf_SVKEY)    
952                                 sv_setsv(ST(0),newRV((SV*)mg->mg_ptr));
953                 }
954         }
955
956 MODULE = B      PACKAGE = B::PVLV       PREFIX = Lv
957
958 U32
959 LvTARGOFF(sv)
960         B::PVLV sv
961
962 U32
963 LvTARGLEN(sv)
964         B::PVLV sv
965
966 char
967 LvTYPE(sv)
968         B::PVLV sv
969
970 B::SV
971 LvTARG(sv)
972         B::PVLV sv
973
974 MODULE = B      PACKAGE = B::BM         PREFIX = Bm
975
976 I32
977 BmUSEFUL(sv)
978         B::BM   sv
979
980 U16
981 BmPREVIOUS(sv)
982         B::BM   sv
983
984 U8
985 BmRARE(sv)
986         B::BM   sv
987
988 void
989 BmTABLE(sv)
990         B::BM   sv
991         STRLEN  len = NO_INIT
992         char *  str = NO_INIT
993     CODE:
994         str = SvPV(sv, len);
995         /* Boyer-Moore table is just after string and its safety-margin \0 */
996         ST(0) = sv_2mortal(newSVpvn(str + len + 1, 256));
997
998 MODULE = B      PACKAGE = B::GV         PREFIX = Gv
999
1000 void
1001 GvNAME(gv)
1002         B::GV   gv
1003     CODE:
1004         ST(0) = sv_2mortal(newSVpvn(GvNAME(gv), GvNAMELEN(gv)));
1005
1006 bool
1007 is_empty(gv)
1008         B::GV   gv
1009     CODE:
1010         RETVAL = GvGP(gv) == Null(GP*);
1011     OUTPUT:
1012         RETVAL
1013
1014 B::HV
1015 GvSTASH(gv)
1016         B::GV   gv
1017
1018 B::SV
1019 GvSV(gv)
1020         B::GV   gv
1021
1022 B::IO
1023 GvIO(gv)
1024         B::GV   gv
1025
1026 B::CV
1027 GvFORM(gv)
1028         B::GV   gv
1029
1030 B::AV
1031 GvAV(gv)
1032         B::GV   gv
1033
1034 B::HV
1035 GvHV(gv)
1036         B::GV   gv
1037
1038 B::GV
1039 GvEGV(gv)
1040         B::GV   gv
1041
1042 B::CV
1043 GvCV(gv)
1044         B::GV   gv
1045
1046 U32
1047 GvCVGEN(gv)
1048         B::GV   gv
1049
1050 U16
1051 GvLINE(gv)
1052         B::GV   gv
1053
1054 char *
1055 GvFILE(gv)
1056         B::GV   gv
1057
1058 B::GV
1059 GvFILEGV(gv)
1060         B::GV   gv
1061
1062 MODULE = B      PACKAGE = B::GV
1063
1064 U32
1065 GvREFCNT(gv)
1066         B::GV   gv
1067
1068 U8
1069 GvFLAGS(gv)
1070         B::GV   gv
1071
1072 MODULE = B      PACKAGE = B::IO         PREFIX = Io
1073
1074 long
1075 IoLINES(io)
1076         B::IO   io
1077
1078 long
1079 IoPAGE(io)
1080         B::IO   io
1081
1082 long
1083 IoPAGE_LEN(io)
1084         B::IO   io
1085
1086 long
1087 IoLINES_LEFT(io)
1088         B::IO   io
1089
1090 char *
1091 IoTOP_NAME(io)
1092         B::IO   io
1093
1094 B::GV
1095 IoTOP_GV(io)
1096         B::IO   io
1097
1098 char *
1099 IoFMT_NAME(io)
1100         B::IO   io
1101
1102 B::GV
1103 IoFMT_GV(io)
1104         B::IO   io
1105
1106 char *
1107 IoBOTTOM_NAME(io)
1108         B::IO   io
1109
1110 B::GV
1111 IoBOTTOM_GV(io)
1112         B::IO   io
1113
1114 short
1115 IoSUBPROCESS(io)
1116         B::IO   io
1117
1118 MODULE = B      PACKAGE = B::IO
1119
1120 char
1121 IoTYPE(io)
1122         B::IO   io
1123
1124 U8
1125 IoFLAGS(io)
1126         B::IO   io
1127
1128 MODULE = B      PACKAGE = B::AV         PREFIX = Av
1129
1130 SSize_t
1131 AvFILL(av)
1132         B::AV   av
1133
1134 SSize_t
1135 AvMAX(av)
1136         B::AV   av
1137
1138 #define AvOFF(av) ((XPVAV*)SvANY(av))->xof_off
1139
1140 IV
1141 AvOFF(av)
1142         B::AV   av
1143
1144 void
1145 AvARRAY(av)
1146         B::AV   av
1147     PPCODE:
1148         if (AvFILL(av) >= 0) {
1149             SV **svp = AvARRAY(av);
1150             I32 i;
1151             for (i = 0; i <= AvFILL(av); i++)
1152                 XPUSHs(make_sv_object(aTHX_ sv_newmortal(), svp[i]));
1153         }
1154
1155 MODULE = B      PACKAGE = B::AV
1156
1157 U8
1158 AvFLAGS(av)
1159         B::AV   av
1160
1161 MODULE = B      PACKAGE = B::CV         PREFIX = Cv
1162
1163 B::HV
1164 CvSTASH(cv)
1165         B::CV   cv
1166
1167 B::OP
1168 CvSTART(cv)
1169         B::CV   cv
1170
1171 B::OP
1172 CvROOT(cv)
1173         B::CV   cv
1174
1175 B::GV
1176 CvGV(cv)
1177         B::CV   cv
1178
1179 char *
1180 CvFILE(cv)
1181         B::CV   cv
1182
1183 long
1184 CvDEPTH(cv)
1185         B::CV   cv
1186
1187 B::AV
1188 CvPADLIST(cv)
1189         B::CV   cv
1190
1191 B::CV
1192 CvOUTSIDE(cv)
1193         B::CV   cv
1194
1195 void
1196 CvXSUB(cv)
1197         B::CV   cv
1198     CODE:
1199         ST(0) = sv_2mortal(newSViv(PTR2IV(CvXSUB(cv))));
1200
1201
1202 void
1203 CvXSUBANY(cv)
1204         B::CV   cv
1205     CODE:
1206         ST(0) = sv_2mortal(newSViv(CvXSUBANY(cv).any_iv));
1207
1208 MODULE = B    PACKAGE = B::CV
1209
1210 U8
1211 CvFLAGS(cv)
1212       B::CV   cv
1213
1214
1215 MODULE = B      PACKAGE = B::HV         PREFIX = Hv
1216
1217 STRLEN
1218 HvFILL(hv)
1219         B::HV   hv
1220
1221 STRLEN
1222 HvMAX(hv)
1223         B::HV   hv
1224
1225 I32
1226 HvKEYS(hv)
1227         B::HV   hv
1228
1229 I32
1230 HvRITER(hv)
1231         B::HV   hv
1232
1233 char *
1234 HvNAME(hv)
1235         B::HV   hv
1236
1237 B::PMOP
1238 HvPMROOT(hv)
1239         B::HV   hv
1240
1241 void
1242 HvARRAY(hv)
1243         B::HV   hv
1244     PPCODE:
1245         if (HvKEYS(hv) > 0) {
1246             SV *sv;
1247             char *key;
1248             I32 len;
1249             (void)hv_iterinit(hv);
1250             EXTEND(sp, HvKEYS(hv) * 2);
1251             while (sv = hv_iternextsv(hv, &key, &len)) {
1252                 PUSHs(newSVpvn(key, len));
1253                 PUSHs(make_sv_object(aTHX_ sv_newmortal(), sv));
1254             }
1255         }