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