This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Failed patch hunk reapplied.
[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
c5be433b 10#define PERL_NO_GET_CONTEXT
a8a597b2
MB
11#include "EXTERN.h"
12#include "perl.h"
13#include "XSUB.h"
a8a597b2 14
51aa15f3
GS
15#ifdef PerlIO
16typedef PerlIO * InputStream;
17#else
18typedef FILE * InputStream;
19#endif
20
21
a8a597b2
MB
22static char *svclassnames[] = {
23 "B::NULL",
24 "B::IV",
25 "B::NV",
26 "B::RV",
27 "B::PV",
28 "B::PVIV",
29 "B::PVNV",
30 "B::PVMG",
31 "B::BM",
32 "B::PVLV",
33 "B::AV",
34 "B::HV",
35 "B::CV",
36 "B::GV",
37 "B::FM",
38 "B::IO",
39};
40
41typedef enum {
42 OPc_NULL, /* 0 */
43 OPc_BASEOP, /* 1 */
44 OPc_UNOP, /* 2 */
45 OPc_BINOP, /* 3 */
46 OPc_LOGOP, /* 4 */
1a67a97c
SM
47 OPc_LISTOP, /* 5 */
48 OPc_PMOP, /* 6 */
49 OPc_SVOP, /* 7 */
7934575e 50 OPc_PADOP, /* 8 */
1a67a97c
SM
51 OPc_PVOP, /* 9 */
52 OPc_CVOP, /* 10 */
53 OPc_LOOP, /* 11 */
54 OPc_COP /* 12 */
a8a597b2
MB
55} opclass;
56
57static char *opclassnames[] = {
58 "B::NULL",
59 "B::OP",
60 "B::UNOP",
61 "B::BINOP",
62 "B::LOGOP",
a8a597b2
MB
63 "B::LISTOP",
64 "B::PMOP",
65 "B::SVOP",
7934575e 66 "B::PADOP",
a8a597b2
MB
67 "B::PVOP",
68 "B::CVOP",
69 "B::LOOP",
70 "B::COP"
71};
72
df3728a2 73#define MY_CXT_KEY "B::_guts" XS_VERSION
a8a597b2 74
89ca4ac7
JH
75typedef struct {
76 int x_walkoptree_debug; /* Flag for walkoptree debug hook */
b326da91 77 SV * x_specialsv_list[7];
89ca4ac7
JH
78} my_cxt_t;
79
80START_MY_CXT
81
82#define walkoptree_debug (MY_CXT.x_walkoptree_debug)
83#define specialsv_list (MY_CXT.x_specialsv_list)
e8edd1e6 84
a8a597b2 85static opclass
cea2e8a9 86cc_opclass(pTHX_ OP *o)
a8a597b2
MB
87{
88 if (!o)
89 return OPc_NULL;
90
91 if (o->op_type == 0)
92 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
93
94 if (o->op_type == OP_SASSIGN)
95 return ((o->op_private & OPpASSIGN_BACKWARDS) ? OPc_UNOP : OPc_BINOP);
96
18228111
GS
97#ifdef USE_ITHREADS
98 if (o->op_type == OP_GV || o->op_type == OP_GVSV || o->op_type == OP_AELEMFAST)
99 return OPc_PADOP;
100#endif
101
22c35a8c 102 switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
a8a597b2
MB
103 case OA_BASEOP:
104 return OPc_BASEOP;
105
106 case OA_UNOP:
107 return OPc_UNOP;
108
109 case OA_BINOP:
110 return OPc_BINOP;
111
112 case OA_LOGOP:
113 return OPc_LOGOP;
114
a8a597b2
MB
115 case OA_LISTOP:
116 return OPc_LISTOP;
117
118 case OA_PMOP:
119 return OPc_PMOP;
120
121 case OA_SVOP:
122 return OPc_SVOP;
123
7934575e
GS
124 case OA_PADOP:
125 return OPc_PADOP;
a8a597b2 126
293d3ffa
SM
127 case OA_PVOP_OR_SVOP:
128 /*
129 * Character translations (tr///) are usually a PVOP, keeping a
130 * pointer to a table of shorts used to look up translations.
131 * Under utf8, however, a simple table isn't practical; instead,
132 * the OP is an SVOP, and the SV is a reference to a swash
133 * (i.e., an RV pointing to an HV).
134 */
135 return (o->op_private & (OPpTRANS_TO_UTF|OPpTRANS_FROM_UTF))
136 ? OPc_SVOP : OPc_PVOP;
a8a597b2
MB
137
138 case OA_LOOP:
139 return OPc_LOOP;
140
141 case OA_COP:
142 return OPc_COP;
143
144 case OA_BASEOP_OR_UNOP:
145 /*
146 * UNI(OP_foo) in toke.c returns token UNI or FUNC1 depending on
45f6cd40
SM
147 * whether parens were seen. perly.y uses OPf_SPECIAL to
148 * signal whether a BASEOP had empty parens or none.
149 * Some other UNOPs are created later, though, so the best
150 * test is OPf_KIDS, which is set in newUNOP.
a8a597b2 151 */
45f6cd40 152 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
a8a597b2
MB
153
154 case OA_FILESTATOP:
155 /*
156 * The file stat OPs are created via UNI(OP_foo) in toke.c but use
157 * the OPf_REF flag to distinguish between OP types instead of the
158 * usual OPf_SPECIAL flag. As usual, if OPf_KIDS is set, then we
159 * return OPc_UNOP so that walkoptree can find our children. If
160 * OPf_KIDS is not set then we check OPf_REF. Without OPf_REF set
161 * (no argument to the operator) it's an OP; with OPf_REF set it's
7934575e 162 * an SVOP (and op_sv is the GV for the filehandle argument).
a8a597b2
MB
163 */
164 return ((o->op_flags & OPf_KIDS) ? OPc_UNOP :
93865851
GS
165#ifdef USE_ITHREADS
166 (o->op_flags & OPf_REF) ? OPc_PADOP : OPc_BASEOP);
167#else
7934575e 168 (o->op_flags & OPf_REF) ? OPc_SVOP : OPc_BASEOP);
93865851 169#endif
a8a597b2
MB
170 case OA_LOOPEXOP:
171 /*
172 * next, last, redo, dump and goto use OPf_SPECIAL to indicate that a
173 * label was omitted (in which case it's a BASEOP) or else a term was
174 * seen. In this last case, all except goto are definitely PVOP but
175 * goto is either a PVOP (with an ordinary constant label), an UNOP
176 * with OPf_STACKED (with a non-constant non-sub) or an UNOP for
177 * OP_REFGEN (with goto &sub) in which case OPf_STACKED also seems to
178 * get set.
179 */
180 if (o->op_flags & OPf_STACKED)
181 return OPc_UNOP;
182 else if (o->op_flags & OPf_SPECIAL)
183 return OPc_BASEOP;
184 else
185 return OPc_PVOP;
186 }
187 warn("can't determine class of operator %s, assuming BASEOP\n",
22c35a8c 188 PL_op_name[o->op_type]);
a8a597b2
MB
189 return OPc_BASEOP;
190}
191
192static char *
cea2e8a9 193cc_opclassname(pTHX_ OP *o)
a8a597b2 194{
cea2e8a9 195 return opclassnames[cc_opclass(aTHX_ o)];
a8a597b2
MB
196}
197
198static SV *
cea2e8a9 199make_sv_object(pTHX_ SV *arg, SV *sv)
a8a597b2
MB
200{
201 char *type = 0;
202 IV iv;
89ca4ac7 203 dMY_CXT;
a8a597b2 204
e8edd1e6
TH
205 for (iv = 0; iv < sizeof(specialsv_list)/sizeof(SV*); iv++) {
206 if (sv == specialsv_list[iv]) {
a8a597b2
MB
207 type = "B::SPECIAL";
208 break;
209 }
210 }
211 if (!type) {
212 type = svclassnames[SvTYPE(sv)];
56431972 213 iv = PTR2IV(sv);
a8a597b2
MB
214 }
215 sv_setiv(newSVrv(arg, type), iv);
216 return arg;
217}
218
219static SV *
cea2e8a9 220make_mg_object(pTHX_ SV *arg, MAGIC *mg)
a8a597b2 221{
56431972 222 sv_setiv(newSVrv(arg, "B::MAGIC"), PTR2IV(mg));
a8a597b2
MB
223 return arg;
224}
225
226static SV *
52ad86de 227cstring(pTHX_ SV *sv, bool perlstyle)
a8a597b2 228{
79cb57f6 229 SV *sstr = newSVpvn("", 0);
a8a597b2
MB
230 STRLEN len;
231 char *s;
b326da91 232 char escbuff[5]; /* to fit backslash, 3 octals + trailing \0 */
a8a597b2
MB
233
234 if (!SvOK(sv))
235 sv_setpvn(sstr, "0", 1);
236 else
237 {
238 /* XXX Optimise? */
239 s = SvPV(sv, len);
240 sv_catpv(sstr, "\"");
241 for (; len; len--, s++)
242 {
243 /* At least try a little for readability */
244 if (*s == '"')
245 sv_catpv(sstr, "\\\"");
246 else if (*s == '\\')
247 sv_catpv(sstr, "\\\\");
b326da91 248 /* trigraphs - bleagh */
52ad86de 249 else if (!perlstyle && *s == '?' && len>=3 && s[1] == '?')
b326da91
MB
250 {
251 sprintf(escbuff, "\\%03o", '?');
252 sv_catpv(sstr, escbuff);
253 }
52ad86de
JH
254 else if (perlstyle && *s == '$')
255 sv_catpv(sstr, "\\$");
256 else if (perlstyle && *s == '@')
257 sv_catpv(sstr, "\\@");
ce561ef2
JH
258#ifdef EBCDIC
259 else if (isPRINT(*s))
260#else
261 else if (*s >= ' ' && *s < 127)
262#endif /* EBCDIC */
a8a597b2
MB
263 sv_catpvn(sstr, s, 1);
264 else if (*s == '\n')
265 sv_catpv(sstr, "\\n");
266 else if (*s == '\r')
267 sv_catpv(sstr, "\\r");
268 else if (*s == '\t')
269 sv_catpv(sstr, "\\t");
270 else if (*s == '\a')
271 sv_catpv(sstr, "\\a");
272 else if (*s == '\b')
273 sv_catpv(sstr, "\\b");
274 else if (*s == '\f')
275 sv_catpv(sstr, "\\f");
52ad86de 276 else if (!perlstyle && *s == '\v')
a8a597b2
MB
277 sv_catpv(sstr, "\\v");
278 else
279 {
a8a597b2
MB
280 /* Don't want promotion of a signed -1 char in sprintf args */
281 unsigned char c = (unsigned char) *s;
282 sprintf(escbuff, "\\%03o", c);
283 sv_catpv(sstr, escbuff);
284 }
285 /* XXX Add line breaks if string is long */
286 }
287 sv_catpv(sstr, "\"");
288 }
289 return sstr;
290}
291
292static SV *
cea2e8a9 293cchar(pTHX_ SV *sv)
a8a597b2 294{
79cb57f6 295 SV *sstr = newSVpvn("'", 1);
2d8e6c8d
GS
296 STRLEN n_a;
297 char *s = SvPV(sv, n_a);
a8a597b2
MB
298
299 if (*s == '\'')
300 sv_catpv(sstr, "\\'");
301 else if (*s == '\\')
302 sv_catpv(sstr, "\\\\");
ce561ef2 303#ifdef EBCDIC
133b4094 304 else if (isPRINT(*s))
ce561ef2
JH
305#else
306 else if (*s >= ' ' && *s < 127)
307#endif /* EBCDIC */
a8a597b2
MB
308 sv_catpvn(sstr, s, 1);
309 else if (*s == '\n')
310 sv_catpv(sstr, "\\n");
311 else if (*s == '\r')
312 sv_catpv(sstr, "\\r");
313 else if (*s == '\t')
314 sv_catpv(sstr, "\\t");
315 else if (*s == '\a')
316 sv_catpv(sstr, "\\a");
317 else if (*s == '\b')
318 sv_catpv(sstr, "\\b");
319 else if (*s == '\f')
320 sv_catpv(sstr, "\\f");
321 else if (*s == '\v')
322 sv_catpv(sstr, "\\v");
323 else
324 {
325 /* no trigraph support */
326 char escbuff[5]; /* to fit backslash, 3 octals + trailing \0 */
327 /* Don't want promotion of a signed -1 char in sprintf args */
328 unsigned char c = (unsigned char) *s;
329 sprintf(escbuff, "\\%03o", c);
330 sv_catpv(sstr, escbuff);
331 }
332 sv_catpv(sstr, "'");
333 return sstr;
334}
335
a8a597b2 336void
cea2e8a9 337walkoptree(pTHX_ SV *opsv, char *method)
a8a597b2
MB
338{
339 dSP;
340 OP *o;
89ca4ac7
JH
341 dMY_CXT;
342
a8a597b2
MB
343 if (!SvROK(opsv))
344 croak("opsv is not a reference");
345 opsv = sv_mortalcopy(opsv);
56431972 346 o = INT2PTR(OP*,SvIV((SV*)SvRV(opsv)));
a8a597b2
MB
347 if (walkoptree_debug) {
348 PUSHMARK(sp);
349 XPUSHs(opsv);
350 PUTBACK;
351 perl_call_method("walkoptree_debug", G_DISCARD);
352 }
353 PUSHMARK(sp);
354 XPUSHs(opsv);
355 PUTBACK;
356 perl_call_method(method, G_DISCARD);
357 if (o && (o->op_flags & OPf_KIDS)) {
358 OP *kid;
359 for (kid = ((UNOP*)o)->op_first; kid; kid = kid->op_sibling) {
360 /* Use the same opsv. Rely on methods not to mess it up. */
56431972 361 sv_setiv(newSVrv(opsv, cc_opclassname(aTHX_ kid)), PTR2IV(kid));
cea2e8a9 362 walkoptree(aTHX_ opsv, method);
a8a597b2
MB
363 }
364 }
365}
366
367typedef OP *B__OP;
368typedef UNOP *B__UNOP;
369typedef BINOP *B__BINOP;
370typedef LOGOP *B__LOGOP;
a8a597b2
MB
371typedef LISTOP *B__LISTOP;
372typedef PMOP *B__PMOP;
373typedef SVOP *B__SVOP;
7934575e 374typedef PADOP *B__PADOP;
a8a597b2
MB
375typedef PVOP *B__PVOP;
376typedef LOOP *B__LOOP;
377typedef COP *B__COP;
378
379typedef SV *B__SV;
380typedef SV *B__IV;
381typedef SV *B__PV;
382typedef SV *B__NV;
383typedef SV *B__PVMG;
384typedef SV *B__PVLV;
385typedef SV *B__BM;
386typedef SV *B__RV;
387typedef AV *B__AV;
388typedef HV *B__HV;
389typedef CV *B__CV;
390typedef GV *B__GV;
391typedef IO *B__IO;
392
393typedef MAGIC *B__MAGIC;
394
395MODULE = B PACKAGE = B PREFIX = B_
396
397PROTOTYPES: DISABLE
398
399BOOT:
4c1f658f
NIS
400{
401 HV *stash = gv_stashpvn("B", 1, TRUE);
402 AV *export_ok = perl_get_av("B::EXPORT_OK",TRUE);
89ca4ac7 403 MY_CXT_INIT;
e8edd1e6
TH
404 specialsv_list[0] = Nullsv;
405 specialsv_list[1] = &PL_sv_undef;
406 specialsv_list[2] = &PL_sv_yes;
407 specialsv_list[3] = &PL_sv_no;
059a8bb7
JH
408 specialsv_list[4] = pWARN_ALL;
409 specialsv_list[5] = pWARN_NONE;
b326da91 410 specialsv_list[6] = pWARN_STD;
4c1f658f
NIS
411#include "defsubs.h"
412}
a8a597b2 413
3280af22 414#define B_main_cv() PL_main_cv
31d7d75a 415#define B_init_av() PL_initav
059a8bb7
JH
416#define B_begin_av() PL_beginav_save
417#define B_end_av() PL_endav
3280af22
NIS
418#define B_main_root() PL_main_root
419#define B_main_start() PL_main_start
56eca212 420#define B_amagic_generation() PL_amagic_generation
3280af22
NIS
421#define B_comppadlist() (PL_main_cv ? CvPADLIST(PL_main_cv) : CvPADLIST(PL_compcv))
422#define B_sv_undef() &PL_sv_undef
423#define B_sv_yes() &PL_sv_yes
424#define B_sv_no() &PL_sv_no
9d2bbe64
MB
425#ifdef USE_ITHREADS
426#define B_regex_padav() PL_regex_padav
427#endif
a8a597b2 428
31d7d75a
NIS
429B::AV
430B_init_av()
431
059a8bb7
JH
432B::AV
433B_begin_av()
434
435B::AV
436B_end_av()
437
9d2bbe64
MB
438#ifdef USE_ITHREADS
439
440B::AV
441B_regex_padav()
442
443#endif
444
a8a597b2
MB
445B::CV
446B_main_cv()
447
448B::OP
449B_main_root()
450
451B::OP
452B_main_start()
453
56eca212
GS
454long
455B_amagic_generation()
456
a8a597b2
MB
457B::AV
458B_comppadlist()
459
460B::SV
461B_sv_undef()
462
463B::SV
464B_sv_yes()
465
466B::SV
467B_sv_no()
468
469MODULE = B PACKAGE = B
470
471
472void
473walkoptree(opsv, method)
474 SV * opsv
475 char * method
cea2e8a9
GS
476 CODE:
477 walkoptree(aTHX_ opsv, method);
a8a597b2
MB
478
479int
480walkoptree_debug(...)
481 CODE:
89ca4ac7 482 dMY_CXT;
a8a597b2
MB
483 RETVAL = walkoptree_debug;
484 if (items > 0 && SvTRUE(ST(1)))
485 walkoptree_debug = 1;
486 OUTPUT:
487 RETVAL
488
56431972 489#define address(sv) PTR2IV(sv)
a8a597b2
MB
490
491IV
492address(sv)
493 SV * sv
494
495B::SV
496svref_2object(sv)
497 SV * sv
498 CODE:
499 if (!SvROK(sv))
500 croak("argument is not a reference");
501 RETVAL = (SV*)SvRV(sv);
502 OUTPUT:
0cc1d052
NIS
503 RETVAL
504
505void
506opnumber(name)
507char * name
508CODE:
509{
510 int i;
511 IV result = -1;
512 ST(0) = sv_newmortal();
513 if (strncmp(name,"pp_",3) == 0)
514 name += 3;
515 for (i = 0; i < PL_maxo; i++)
516 {
517 if (strcmp(name, PL_op_name[i]) == 0)
518 {
519 result = i;
520 break;
521 }
522 }
523 sv_setiv(ST(0),result);
524}
a8a597b2
MB
525
526void
527ppname(opnum)
528 int opnum
529 CODE:
530 ST(0) = sv_newmortal();
3280af22 531 if (opnum >= 0 && opnum < PL_maxo) {
a8a597b2 532 sv_setpvn(ST(0), "pp_", 3);
22c35a8c 533 sv_catpv(ST(0), PL_op_name[opnum]);
a8a597b2
MB
534 }
535
536void
537hash(sv)
538 SV * sv
539 CODE:
540 char *s;
541 STRLEN len;
542 U32 hash = 0;
faccc32b 543 char hexhash[19]; /* must fit "0xffffffffffffffff" plus trailing \0 */
a8a597b2 544 s = SvPV(sv, len);
cf86991c 545 PERL_HASH(hash, s, len);
faccc32b 546 sprintf(hexhash, "0x%"UVxf, (UV)hash);
a8a597b2
MB
547 ST(0) = sv_2mortal(newSVpv(hexhash, 0));
548
549#define cast_I32(foo) (I32)foo
550IV
551cast_I32(i)
552 IV i
553
554void
555minus_c()
556 CODE:
3280af22 557 PL_minus_c = TRUE;
a8a597b2 558
059a8bb7
JH
559void
560save_BEGINs()
561 CODE:
aefff11f 562 PL_savebegin = TRUE;
059a8bb7 563
a8a597b2
MB
564SV *
565cstring(sv)
566 SV * sv
cea2e8a9 567 CODE:
52ad86de
JH
568 RETVAL = cstring(aTHX_ sv, 0);
569 OUTPUT:
570 RETVAL
571
572SV *
573perlstring(sv)
574 SV * sv
575 CODE:
576 RETVAL = cstring(aTHX_ sv, 1);
cea2e8a9
GS
577 OUTPUT:
578 RETVAL
a8a597b2
MB
579
580SV *
581cchar(sv)
582 SV * sv
cea2e8a9
GS
583 CODE:
584 RETVAL = cchar(aTHX_ sv);
585 OUTPUT:
586 RETVAL
a8a597b2
MB
587
588void
589threadsv_names()
590 PPCODE:
4d1ff10f 591#ifdef USE_5005THREADS
a8a597b2 592 int i;
533c011a 593 STRLEN len = strlen(PL_threadsv_names);
a8a597b2
MB
594
595 EXTEND(sp, len);
596 for (i = 0; i < len; i++)
79cb57f6 597 PUSHs(sv_2mortal(newSVpvn(&PL_threadsv_names[i], 1)));
a8a597b2
MB
598#endif
599
600
601#define OP_next(o) o->op_next
602#define OP_sibling(o) o->op_sibling
22c35a8c 603#define OP_desc(o) PL_op_desc[o->op_type]
a8a597b2
MB
604#define OP_targ(o) o->op_targ
605#define OP_type(o) o->op_type
606#define OP_seq(o) o->op_seq
607#define OP_flags(o) o->op_flags
608#define OP_private(o) o->op_private
609
610MODULE = B PACKAGE = B::OP PREFIX = OP_
611
612B::OP
613OP_next(o)
614 B::OP o
615
616B::OP
617OP_sibling(o)
618 B::OP o
619
620char *
3f872cb9
GS
621OP_name(o)
622 B::OP o
623 CODE:
8063af02
DM
624 RETVAL = PL_op_name[o->op_type];
625 OUTPUT:
626 RETVAL
3f872cb9
GS
627
628
8063af02 629void
a8a597b2
MB
630OP_ppaddr(o)
631 B::OP o
dc333d64
GS
632 PREINIT:
633 int i;
634 SV *sv = sv_newmortal();
a8a597b2 635 CODE:
dc333d64
GS
636 sv_setpvn(sv, "PL_ppaddr[OP_", 13);
637 sv_catpv(sv, PL_op_name[o->op_type]);
638 for (i=13; i<SvCUR(sv); ++i)
639 SvPVX(sv)[i] = toUPPER(SvPVX(sv)[i]);
640 sv_catpv(sv, "]");
641 ST(0) = sv;
a8a597b2
MB
642
643char *
644OP_desc(o)
645 B::OP o
646
7934575e 647PADOFFSET
a8a597b2
MB
648OP_targ(o)
649 B::OP o
650
651U16
652OP_type(o)
653 B::OP o
654
655U16
656OP_seq(o)
657 B::OP o
658
659U8
660OP_flags(o)
661 B::OP o
662
663U8
664OP_private(o)
665 B::OP o
666
667#define UNOP_first(o) o->op_first
668
669MODULE = B PACKAGE = B::UNOP PREFIX = UNOP_
670
671B::OP
672UNOP_first(o)
673 B::UNOP o
674
675#define BINOP_last(o) o->op_last
676
677MODULE = B PACKAGE = B::BINOP PREFIX = BINOP_
678
679B::OP
680BINOP_last(o)
681 B::BINOP o
682
683#define LOGOP_other(o) o->op_other
684
685MODULE = B PACKAGE = B::LOGOP PREFIX = LOGOP_
686
687B::OP
688LOGOP_other(o)
689 B::LOGOP o
690
a8a597b2
MB
691MODULE = B PACKAGE = B::LISTOP PREFIX = LISTOP_
692
c03c2844
SM
693U32
694LISTOP_children(o)
695 B::LISTOP o
696 OP * kid = NO_INIT
697 int i = NO_INIT
698 CODE:
c03c2844
SM
699 i = 0;
700 for (kid = o->op_first; kid; kid = kid->op_sibling)
701 i++;
8063af02
DM
702 RETVAL = i;
703 OUTPUT:
704 RETVAL
c03c2844 705
a8a597b2
MB
706#define PMOP_pmreplroot(o) o->op_pmreplroot
707#define PMOP_pmreplstart(o) o->op_pmreplstart
708#define PMOP_pmnext(o) o->op_pmnext
aaa362c4 709#define PMOP_pmregexp(o) PM_GETRE(o)
9d2bbe64
MB
710#ifdef USE_ITHREADS
711#define PMOP_pmoffset(o) o->op_pmoffset
712#endif
a8a597b2
MB
713#define PMOP_pmflags(o) o->op_pmflags
714#define PMOP_pmpermflags(o) o->op_pmpermflags
9d2bbe64 715#define PMOP_pmdynflags(o) o->op_pmdynflags
a8a597b2
MB
716
717MODULE = B PACKAGE = B::PMOP PREFIX = PMOP_
718
719void
720PMOP_pmreplroot(o)
721 B::PMOP o
722 OP * root = NO_INIT
723 CODE:
724 ST(0) = sv_newmortal();
725 root = o->op_pmreplroot;
726 /* OP_PUSHRE stores an SV* instead of an OP* in op_pmreplroot */
727 if (o->op_type == OP_PUSHRE) {
9d2bbe64
MB
728#ifdef USE_ITHREADS
729 sv_setiv(ST(0), INT2PTR(PADOFFSET,root) );
730#else
a8a597b2
MB
731 sv_setiv(newSVrv(ST(0), root ?
732 svclassnames[SvTYPE((SV*)root)] : "B::SV"),
56431972 733 PTR2IV(root));
9d2bbe64 734#endif
a8a597b2
MB
735 }
736 else {
56431972 737 sv_setiv(newSVrv(ST(0), cc_opclassname(aTHX_ root)), PTR2IV(root));
a8a597b2
MB
738 }
739
740B::OP
741PMOP_pmreplstart(o)
742 B::PMOP o
743
744B::PMOP
745PMOP_pmnext(o)
746 B::PMOP o
747
9d2bbe64
MB
748#ifdef USE_ITHREADS
749
750IV
751PMOP_pmoffset(o)
752 B::PMOP o
753
754#endif
755
a8a597b2
MB
756U16
757PMOP_pmflags(o)
758 B::PMOP o
759
760U16
761PMOP_pmpermflags(o)
762 B::PMOP o
763
9d2bbe64
MB
764U8
765PMOP_pmdynflags(o)
766 B::PMOP o
767
a8a597b2
MB
768void
769PMOP_precomp(o)
770 B::PMOP o
771 REGEXP * rx = NO_INIT
772 CODE:
773 ST(0) = sv_newmortal();
aaa362c4 774 rx = PM_GETRE(o);
a8a597b2
MB
775 if (rx)
776 sv_setpvn(ST(0), rx->precomp, rx->prelen);
777
ac33dcd1
JH
778#define SVOP_sv(o) cSVOPo->op_sv
779#define SVOP_gv(o) ((GV*)cSVOPo->op_sv)
a8a597b2
MB
780
781MODULE = B PACKAGE = B::SVOP PREFIX = SVOP_
782
a8a597b2
MB
783B::SV
784SVOP_sv(o)
785 B::SVOP o
786
f22444f5 787B::GV
065a1863
GS
788SVOP_gv(o)
789 B::SVOP o
790
7934575e
GS
791#define PADOP_padix(o) o->op_padix
792#define PADOP_sv(o) (o->op_padix ? PL_curpad[o->op_padix] : Nullsv)
793#define PADOP_gv(o) ((o->op_padix \
794 && SvTYPE(PL_curpad[o->op_padix]) == SVt_PVGV) \
795 ? (GV*)PL_curpad[o->op_padix] : Nullgv)
a8a597b2 796
7934575e
GS
797MODULE = B PACKAGE = B::PADOP PREFIX = PADOP_
798
799PADOFFSET
800PADOP_padix(o)
801 B::PADOP o
802
803B::SV
804PADOP_sv(o)
805 B::PADOP o
a8a597b2
MB
806
807B::GV
7934575e
GS
808PADOP_gv(o)
809 B::PADOP o
a8a597b2
MB
810
811MODULE = B PACKAGE = B::PVOP PREFIX = PVOP_
812
813void
814PVOP_pv(o)
815 B::PVOP o
816 CODE:
817 /*
bec89253 818 * OP_TRANS uses op_pv to point to a table of 256 or >=258 shorts
a8a597b2
MB
819 * whereas other PVOPs point to a null terminated string.
820 */
bec89253
RH
821 if (o->op_type == OP_TRANS &&
822 (o->op_private & OPpTRANS_COMPLEMENT) &&
823 !(o->op_private & OPpTRANS_DELETE))
824 {
825 short* tbl = (short*)o->op_pv;
826 short entries = 257 + tbl[256];
827 ST(0) = sv_2mortal(newSVpv(o->op_pv, entries * sizeof(short)));
828 }
829 else if (o->op_type == OP_TRANS) {
830 ST(0) = sv_2mortal(newSVpv(o->op_pv, 256 * sizeof(short)));
831 }
832 else
833 ST(0) = sv_2mortal(newSVpv(o->op_pv, 0));
a8a597b2
MB
834
835#define LOOP_redoop(o) o->op_redoop
836#define LOOP_nextop(o) o->op_nextop
837#define LOOP_lastop(o) o->op_lastop
838
839MODULE = B PACKAGE = B::LOOP PREFIX = LOOP_
840
841
842B::OP
843LOOP_redoop(o)
844 B::LOOP o
845
846B::OP
847LOOP_nextop(o)
848 B::LOOP o
849
850B::OP
851LOOP_lastop(o)
852 B::LOOP o
853
854#define COP_label(o) o->cop_label
11faa288
GS
855#define COP_stashpv(o) CopSTASHPV(o)
856#define COP_stash(o) CopSTASH(o)
57843af0 857#define COP_file(o) CopFILE(o)
a8a597b2
MB
858#define COP_cop_seq(o) o->cop_seq
859#define COP_arybase(o) o->cop_arybase
57843af0 860#define COP_line(o) CopLINE(o)
b295d113 861#define COP_warnings(o) o->cop_warnings
a8a597b2
MB
862
863MODULE = B PACKAGE = B::COP PREFIX = COP_
864
865char *
866COP_label(o)
867 B::COP o
868
11faa288
GS
869char *
870COP_stashpv(o)
871 B::COP o
872
a8a597b2
MB
873B::HV
874COP_stash(o)
875 B::COP o
876
57843af0
GS
877char *
878COP_file(o)
a8a597b2
MB
879 B::COP o
880
881U32
882COP_cop_seq(o)
883 B::COP o
884
885I32
886COP_arybase(o)
887 B::COP o
888
889U16
890COP_line(o)
891 B::COP o
892
b295d113
TH
893B::SV
894COP_warnings(o)
895 B::COP o
896
a8a597b2
MB
897MODULE = B PACKAGE = B::SV PREFIX = Sv
898
899U32
900SvREFCNT(sv)
901 B::SV sv
902
903U32
904SvFLAGS(sv)
905 B::SV sv
906
907MODULE = B PACKAGE = B::IV PREFIX = Sv
908
909IV
910SvIV(sv)
911 B::IV sv
912
913IV
914SvIVX(sv)
915 B::IV sv
916
0ca04487
VB
917UV
918SvUVX(sv)
919 B::IV sv
920
921
a8a597b2
MB
922MODULE = B PACKAGE = B::IV
923
924#define needs64bits(sv) ((I32)SvIVX(sv) != SvIVX(sv))
925
926int
927needs64bits(sv)
928 B::IV sv
929
930void
931packiv(sv)
932 B::IV sv
933 CODE:
934 if (sizeof(IV) == 8) {
935 U32 wp[2];
936 IV iv = SvIVX(sv);
937 /*
938 * The following way of spelling 32 is to stop compilers on
939 * 32-bit architectures from moaning about the shift count
940 * being >= the width of the type. Such architectures don't
941 * reach this code anyway (unless sizeof(IV) > 8 but then
942 * everything else breaks too so I'm not fussed at the moment).
943 */
42718184
RB
944#ifdef UV_IS_QUAD
945 wp[0] = htonl(((UV)iv) >> (sizeof(UV)*4));
946#else
947 wp[0] = htonl(((U32)iv) >> (sizeof(UV)*4));
948#endif
a8a597b2 949 wp[1] = htonl(iv & 0xffffffff);
79cb57f6 950 ST(0) = sv_2mortal(newSVpvn((char *)wp, 8));
a8a597b2
MB
951 } else {
952 U32 w = htonl((U32)SvIVX(sv));
79cb57f6 953 ST(0) = sv_2mortal(newSVpvn((char *)&w, 4));
a8a597b2
MB
954 }
955
956MODULE = B PACKAGE = B::NV PREFIX = Sv
957
76ef7183 958NV
a8a597b2
MB
959SvNV(sv)
960 B::NV sv
961
76ef7183 962NV
a8a597b2
MB
963SvNVX(sv)
964 B::NV sv
965
966MODULE = B PACKAGE = B::RV PREFIX = Sv
967
968B::SV
969SvRV(sv)
970 B::RV sv
971
972MODULE = B PACKAGE = B::PV PREFIX = Sv
973
0b40bd6d
RH
974char*
975SvPVX(sv)
976 B::PV sv
977
b326da91
MB
978B::SV
979SvRV(sv)
980 B::PV sv
981 CODE:
982 if( SvROK(sv) ) {
983 RETVAL = SvRV(sv);
984 }
985 else {
986 croak( "argument is not SvROK" );
987 }
988 OUTPUT:
989 RETVAL
990
a8a597b2
MB
991void
992SvPV(sv)
993 B::PV sv
994 CODE:
b326da91 995 ST(0) = sv_newmortal();
9d2bbe64 996 if( SvPOK(sv) ) {
b326da91
MB
997 sv_setpvn(ST(0), SvPVX(sv), SvCUR(sv));
998 SvFLAGS(ST(0)) |= SvUTF8(sv);
999 }
1000 else {
1001 /* XXX for backward compatibility, but should fail */
1002 /* croak( "argument is not SvPOK" ); */
1003 sv_setpvn(ST(0), NULL, 0);
1004 }
a8a597b2 1005
445a12f6
DM
1006STRLEN
1007SvLEN(sv)
1008 B::PV sv
1009
1010STRLEN
1011SvCUR(sv)
1012 B::PV sv
1013
a8a597b2
MB
1014MODULE = B PACKAGE = B::PVMG PREFIX = Sv
1015
1016void
1017SvMAGIC(sv)
1018 B::PVMG sv
1019 MAGIC * mg = NO_INIT
1020 PPCODE:
1021 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic)
cea2e8a9 1022 XPUSHs(make_mg_object(aTHX_ sv_newmortal(), mg));
a8a597b2
MB
1023
1024MODULE = B PACKAGE = B::PVMG
1025
1026B::HV
1027SvSTASH(sv)
1028 B::PVMG sv
1029
1030#define MgMOREMAGIC(mg) mg->mg_moremagic
1031#define MgPRIVATE(mg) mg->mg_private
1032#define MgTYPE(mg) mg->mg_type
1033#define MgFLAGS(mg) mg->mg_flags
1034#define MgOBJ(mg) mg->mg_obj
88b39979 1035#define MgLENGTH(mg) mg->mg_len
bde7177d 1036#define MgREGEX(mg) PTR2IV(mg->mg_obj)
a8a597b2
MB
1037
1038MODULE = B PACKAGE = B::MAGIC PREFIX = Mg
1039
1040B::MAGIC
1041MgMOREMAGIC(mg)
1042 B::MAGIC mg
1043
1044U16
1045MgPRIVATE(mg)
1046 B::MAGIC mg
1047
1048char
1049MgTYPE(mg)
1050 B::MAGIC mg
1051
1052U8
1053MgFLAGS(mg)
1054 B::MAGIC mg
1055
1056B::SV
1057MgOBJ(mg)
1058 B::MAGIC mg
b326da91
MB
1059 CODE:
1060 if( mg->mg_type != 'r' ) {
1061 RETVAL = MgOBJ(mg);
1062 }
1063 else {
1064 croak( "OBJ is not meaningful on r-magic" );
1065 }
1066 OUTPUT:
1067 RETVAL
1068
9d2bbe64
MB
1069IV
1070MgREGEX(mg)
1071 B::MAGIC mg
1072 CODE:
1073 if( mg->mg_type == 'r' ) {
1074 RETVAL = MgREGEX(mg);
1075 }
1076 else {
1077 croak( "REGEX is only meaningful on r-magic" );
1078 }
1079 OUTPUT:
1080 RETVAL
1081
b326da91
MB
1082SV*
1083precomp(mg)
1084 B::MAGIC mg
1085 CODE:
1086 if (mg->mg_type == 'r') {
1087 REGEXP* rx = (REGEXP*)mg->mg_obj;
1088 if( rx )
1089 RETVAL = newSVpvn( rx->precomp, rx->prelen );
1090 }
1091 else {
1092 croak( "precomp is only meaningful on r-magic" );
1093 }
1094 OUTPUT:
1095 RETVAL
a8a597b2 1096
88b39979
VB
1097I32
1098MgLENGTH(mg)
1099 B::MAGIC mg
1100
a8a597b2
MB
1101void
1102MgPTR(mg)
1103 B::MAGIC mg
1104 CODE:
1105 ST(0) = sv_newmortal();
88b39979
VB
1106 if (mg->mg_ptr){
1107 if (mg->mg_len >= 0){
1108 sv_setpvn(ST(0), mg->mg_ptr, mg->mg_len);
1109 } else {
1110 if (mg->mg_len == HEf_SVKEY)
1111 sv_setsv(ST(0),newRV((SV*)mg->mg_ptr));
1112 }
1113 }
a8a597b2
MB
1114
1115MODULE = B PACKAGE = B::PVLV PREFIX = Lv
1116
1117U32
1118LvTARGOFF(sv)
1119 B::PVLV sv
1120
1121U32
1122LvTARGLEN(sv)
1123 B::PVLV sv
1124
1125char
1126LvTYPE(sv)
1127 B::PVLV sv
1128
1129B::SV
1130LvTARG(sv)
1131 B::PVLV sv
1132
1133MODULE = B PACKAGE = B::BM PREFIX = Bm
1134
1135I32
1136BmUSEFUL(sv)
1137 B::BM sv
1138
1139U16
1140BmPREVIOUS(sv)
1141 B::BM sv
1142
1143U8
1144BmRARE(sv)
1145 B::BM sv
1146
1147void
1148BmTABLE(sv)
1149 B::BM sv
1150 STRLEN len = NO_INIT
1151 char * str = NO_INIT
1152 CODE:
1153 str = SvPV(sv, len);
1154 /* Boyer-Moore table is just after string and its safety-margin \0 */
79cb57f6 1155 ST(0) = sv_2mortal(newSVpvn(str + len + 1, 256));
a8a597b2
MB
1156
1157MODULE = B PACKAGE = B::GV PREFIX = Gv
1158
1159void
1160GvNAME(gv)
1161 B::GV gv
1162 CODE:
79cb57f6 1163 ST(0) = sv_2mortal(newSVpvn(GvNAME(gv), GvNAMELEN(gv)));
a8a597b2 1164
87d7fd28
GS
1165bool
1166is_empty(gv)
1167 B::GV gv
1168 CODE:
1169 RETVAL = GvGP(gv) == Null(GP*);
1170 OUTPUT:
1171 RETVAL
1172
a8a597b2
MB
1173B::HV
1174GvSTASH(gv)
1175 B::GV gv
1176
1177B::SV
1178GvSV(gv)
1179 B::GV gv
1180
1181B::IO
1182GvIO(gv)
1183 B::GV gv
1184
1185B::CV
1186GvFORM(gv)
1187 B::GV gv
1188
1189B::AV
1190GvAV(gv)
1191 B::GV gv
1192
1193B::HV
1194GvHV(gv)
1195 B::GV gv
1196
1197B::GV
1198GvEGV(gv)
1199 B::GV gv
1200
1201B::CV
1202GvCV(gv)
1203 B::GV gv
1204
1205U32
1206GvCVGEN(gv)
1207 B::GV gv
1208
1209U16
1210GvLINE(gv)
1211 B::GV gv
1212
b195d487
GS
1213char *
1214GvFILE(gv)
1215 B::GV gv
1216
a8a597b2
MB
1217B::GV
1218GvFILEGV(gv)
1219 B::GV gv
1220
1221MODULE = B PACKAGE = B::GV
1222
1223U32
1224GvREFCNT(gv)
1225 B::GV gv
1226
1227U8
1228GvFLAGS(gv)
1229 B::GV gv
1230
1231MODULE = B PACKAGE = B::IO PREFIX = Io
1232
1233long
1234IoLINES(io)
1235 B::IO io
1236
1237long
1238IoPAGE(io)
1239 B::IO io
1240
1241long
1242IoPAGE_LEN(io)
1243 B::IO io
1244
1245long
1246IoLINES_LEFT(io)
1247 B::IO io
1248
1249char *
1250IoTOP_NAME(io)
1251 B::IO io
1252
1253B::GV
1254IoTOP_GV(io)
1255 B::IO io
1256
1257char *
1258IoFMT_NAME(io)
1259 B::IO io
1260
1261B::GV
1262IoFMT_GV(io)
1263 B::IO io
1264
1265char *
1266IoBOTTOM_NAME(io)
1267 B::IO io
1268
1269B::GV
1270IoBOTTOM_GV(io)
1271 B::IO io
1272
1273short
1274IoSUBPROCESS(io)
1275 B::IO io
1276
b326da91
MB
1277bool
1278IsSTD(io,name)
1279 B::IO io
1280 char* name
1281 PREINIT:
1282 PerlIO* handle = 0;
1283 CODE:
1284 if( strEQ( name, "stdin" ) ) {
1285 handle = PerlIO_stdin();
1286 }
1287 else if( strEQ( name, "stdout" ) ) {
1288 handle = PerlIO_stdout();
1289 }
1290 else if( strEQ( name, "stderr" ) ) {
1291 handle = PerlIO_stderr();
1292 }
1293 else {
1294 croak( "Invalid value '%s'", name );
1295 }
1296 RETVAL = handle == IoIFP(io);
1297 OUTPUT:
1298 RETVAL
1299
a8a597b2
MB
1300MODULE = B PACKAGE = B::IO
1301
1302char
1303IoTYPE(io)
1304 B::IO io
1305
1306U8
1307IoFLAGS(io)
1308 B::IO io
1309
1310MODULE = B PACKAGE = B::AV PREFIX = Av
1311
1312SSize_t
1313AvFILL(av)
1314 B::AV av
1315
1316SSize_t
1317AvMAX(av)
1318 B::AV av
1319
1320#define AvOFF(av) ((XPVAV*)SvANY(av))->xof_off
1321
1322IV
1323AvOFF(av)
1324 B::AV av
1325
1326void
1327AvARRAY(av)
1328 B::AV av
1329 PPCODE:
1330 if (AvFILL(av) >= 0) {
1331 SV **svp = AvARRAY(av);
1332 I32 i;
1333 for (i = 0; i <= AvFILL(av); i++)
cea2e8a9 1334 XPUSHs(make_sv_object(aTHX_ sv_newmortal(), svp[i]));
a8a597b2
MB
1335 }
1336
1337MODULE = B PACKAGE = B::AV
1338
1339U8
1340AvFLAGS(av)
1341 B::AV av
1342
1343MODULE = B PACKAGE = B::CV PREFIX = Cv
1344
1345B::HV
1346CvSTASH(cv)
1347 B::CV cv
1348
1349B::OP
1350CvSTART(cv)
1351 B::CV cv
1352
1353B::OP
1354CvROOT(cv)
1355 B::CV cv
1356
1357B::GV
1358CvGV(cv)
1359 B::CV cv
1360
57843af0
GS
1361char *
1362CvFILE(cv)
1363 B::CV cv
1364
a8a597b2
MB
1365long
1366CvDEPTH(cv)
1367 B::CV cv
1368
1369B::AV
1370CvPADLIST(cv)
1371 B::CV cv
1372
1373B::CV
1374CvOUTSIDE(cv)
1375 B::CV cv
1376
1377void
1378CvXSUB(cv)
1379 B::CV cv
1380 CODE:
56431972 1381 ST(0) = sv_2mortal(newSViv(PTR2IV(CvXSUB(cv))));
a8a597b2
MB
1382
1383
1384void
1385CvXSUBANY(cv)
1386 B::CV cv
1387 CODE:
b326da91
MB
1388 ST(0) = CvCONST(cv) ?
1389 make_sv_object(aTHX_ sv_newmortal(),CvXSUBANY(cv).any_ptr) :
1390 sv_2mortal(newSViv(CvXSUBANY(cv).any_iv));
a8a597b2 1391
5cfd8ad4
VB
1392MODULE = B PACKAGE = B::CV
1393
6aaf4108 1394U16
5cfd8ad4
VB
1395CvFLAGS(cv)
1396 B::CV cv
1397
de3f1649
JT
1398MODULE = B PACKAGE = B::CV PREFIX = cv_
1399
1400B::SV
1401cv_const_sv(cv)
1402 B::CV cv
1403
5cfd8ad4 1404
a8a597b2
MB
1405MODULE = B PACKAGE = B::HV PREFIX = Hv
1406
1407STRLEN
1408HvFILL(hv)
1409 B::HV hv
1410
1411STRLEN
1412HvMAX(hv)
1413 B::HV hv
1414
1415I32
1416HvKEYS(hv)
1417 B::HV hv
1418
1419I32
1420HvRITER(hv)
1421 B::HV hv
1422
1423char *
1424HvNAME(hv)
1425 B::HV hv
1426
1427B::PMOP
1428HvPMROOT(hv)
1429 B::HV hv
1430
1431void
1432HvARRAY(hv)
1433 B::HV hv
1434 PPCODE:
1435 if (HvKEYS(hv) > 0) {
1436 SV *sv;
1437 char *key;
1438 I32 len;
1439 (void)hv_iterinit(hv);
1440 EXTEND(sp, HvKEYS(hv) * 2);
8063af02 1441 while ((sv = hv_iternextsv(hv, &key, &len))) {
79cb57f6 1442 PUSHs(newSVpvn(key, len));
cea2e8a9 1443 PUSHs(make_sv_object(aTHX_ sv_newmortal(), sv));
a8a597b2
MB
1444 }
1445 }