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