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