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