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