This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
fix various compiler warnings from XS code
[perl5.git] / ext / Opcode / Opcode.xs
CommitLineData
c5be433b 1#define PERL_NO_GET_CONTEXT
6badd1a5
PP
2#include "EXTERN.h"
3#include "perl.h"
4#include "XSUB.h"
5
3280af22 6/* PL_maxo shouldn't differ from MAXO but leave room anyway (see BOOT:) */
6badd1a5
PP
7#define OP_MASK_BUF_SIZE (MAXO + 100)
8
4d8e9581 9/* XXX op_named_bits and opset_all are never freed */
df3728a2 10#define MY_CXT_KEY "Opcode::_guts" XS_VERSION
89ca4ac7
JH
11
12typedef struct {
13 HV * x_op_named_bits; /* cache shared for whole process */
14 SV * x_opset_all; /* mask with all bits set */
15 IV x_opset_len; /* length of opmasks in bytes */
16 int x_opcode_debug;
17} my_cxt_t;
18
19START_MY_CXT
20
21#define op_named_bits (MY_CXT.x_op_named_bits)
22#define opset_all (MY_CXT.x_opset_all)
23#define opset_len (MY_CXT.x_opset_len)
24#define opcode_debug (MY_CXT.x_opcode_debug)
6badd1a5 25
cea2e8a9
GS
26static SV *new_opset (pTHX_ SV *old_opset);
27static int verify_opset (pTHX_ SV *opset, int fatal);
5d7488b2
AL
28static void set_opset_bits (pTHX_ char *bitmap, SV *bitspec, int on, const char *opname);
29static void put_op_bitspec (pTHX_ const char *optag, STRLEN len, SV *opset);
30static SV *get_op_bitspec (pTHX_ const char *opname, STRLEN len, int fatal);
6badd1a5
PP
31
32
33/* Initialise our private op_named_bits HV.
34 * It is first loaded with the name and number of each perl operator.
35 * Then the builtin tags :none and :all are added.
36 * Opcode.pm loads the standard optags from __DATA__
4d8e9581
GS
37 * XXX leak-alert: data allocated here is never freed, call this
38 * at most once
6badd1a5
PP
39 */
40
41static void
cea2e8a9 42op_names_init(pTHX)
6badd1a5
PP
43{
44 int i;
45 STRLEN len;
31fb1209 46 char **op_names;
6badd1a5 47 char *bitmap;
89ca4ac7 48 dMY_CXT;
6badd1a5
PP
49
50 op_named_bits = newHV();
31fb1209 51 op_names = get_op_names();
3280af22 52 for(i=0; i < PL_maxo; ++i) {
5d7488b2 53 SV * const sv = newSViv(i);
e858de61 54 SvREADONLY_on(sv);
c33e8be1 55 (void) hv_store(op_named_bits, op_names[i], strlen(op_names[i]), sv, 0);
6badd1a5
PP
56 }
57
e7c4ace0 58 put_op_bitspec(aTHX_ STR_WITH_LEN(":none"), sv_2mortal(new_opset(aTHX_ Nullsv)));
6badd1a5 59
cea2e8a9 60 opset_all = new_opset(aTHX_ Nullsv);
6badd1a5 61 bitmap = SvPV(opset_all, len);
e7c4ace0 62 memset(bitmap, 0xFF, len-1); /* deal with last byte specially, see below */
6badd1a5 63 /* Take care to set the right number of bits in the last byte */
3280af22 64 bitmap[len-1] = (PL_maxo & 0x07) ? ~(0xFF << (PL_maxo & 0x07)) : 0xFF;
e7c4ace0 65 put_op_bitspec(aTHX_ STR_WITH_LEN(":all"), opset_all); /* don't mortalise */
6badd1a5
PP
66}
67
68
69/* Store a new tag definition. Always a mask.
70 * The tag must not already be defined.
71 * SV *mask is copied not referenced.
72 */
73
74static void
5d7488b2 75put_op_bitspec(pTHX_ const char *optag, STRLEN len, SV *mask)
6badd1a5
PP
76{
77 SV **svp;
89ca4ac7
JH
78 dMY_CXT;
79
cea2e8a9 80 verify_opset(aTHX_ mask,1);
6badd1a5
PP
81 svp = hv_fetch(op_named_bits, optag, len, 1);
82 if (SvOK(*svp))
83 croak("Opcode tag \"%s\" already defined", optag);
84 sv_setsv(*svp, mask);
85 SvREADONLY_on(*svp);
86}
87
88
89
90/* Fetch a 'bits' entry for an opname or optag (IV/PV).
91 * Note that we return the actual entry for speed.
92 * Always sv_mortalcopy() if returing it to user code.
93 */
94
95static SV *
5d7488b2 96get_op_bitspec(pTHX_ const char *opname, STRLEN len, int fatal)
6badd1a5
PP
97{
98 SV **svp;
89ca4ac7
JH
99 dMY_CXT;
100
6badd1a5
PP
101 svp = hv_fetch(op_named_bits, opname, len, 0);
102 if (!svp || !SvOK(*svp)) {
103 if (!fatal)
104 return Nullsv;
105 if (*opname == ':')
106 croak("Unknown operator tag \"%s\"", opname);
107 if (*opname == '!') /* XXX here later, or elsewhere? */
108 croak("Can't negate operators here (\"%s\")", opname);
109 if (isALPHA(*opname))
110 croak("Unknown operator name \"%s\"", opname);
111 croak("Unknown operator prefix \"%s\"", opname);
112 }
113 return *svp;
114}
115
116
117
118static SV *
cea2e8a9 119new_opset(pTHX_ SV *old_opset)
6badd1a5
PP
120{
121 SV *opset;
89ca4ac7
JH
122 dMY_CXT;
123
6badd1a5 124 if (old_opset) {
cea2e8a9 125 verify_opset(aTHX_ old_opset,1);
6badd1a5
PP
126 opset = newSVsv(old_opset);
127 }
128 else {
561b68a9 129 opset = newSV(opset_len);
aa07b2f6 130 Zero(SvPVX_const(opset), opset_len + 1, char);
6badd1a5
PP
131 SvCUR_set(opset, opset_len);
132 (void)SvPOK_only(opset);
133 }
134 /* not mortalised here */
135 return opset;
136}
137
138
139static int
cea2e8a9 140verify_opset(pTHX_ SV *opset, int fatal)
6badd1a5 141{
9849c14c 142 const char *err = NULL;
89ca4ac7
JH
143 dMY_CXT;
144
6badd1a5
PP
145 if (!SvOK(opset)) err = "undefined";
146 else if (!SvPOK(opset)) err = "wrong type";
7c436af3 147 else if (SvCUR(opset) != (STRLEN)opset_len) err = "wrong size";
6badd1a5
PP
148 if (err && fatal) {
149 croak("Invalid opset: %s", err);
150 }
151 return !err;
152}
153
154
155static void
5d7488b2 156set_opset_bits(pTHX_ char *bitmap, SV *bitspec, int on, const char *opname)
6badd1a5 157{
89ca4ac7
JH
158 dMY_CXT;
159
6badd1a5 160 if (SvIOK(bitspec)) {
5d7488b2
AL
161 const int myopcode = SvIV(bitspec);
162 const int offset = myopcode >> 3;
163 const int bit = myopcode & 0x07;
3280af22 164 if (myopcode >= PL_maxo || myopcode < 0)
6badd1a5
PP
165 croak("panic: opcode \"%s\" value %d is invalid", opname, myopcode);
166 if (opcode_debug >= 2)
ff0cee69 167 warn("set_opset_bits bit %2d (off=%d, bit=%d) %s %s\n",
6badd1a5
PP
168 myopcode, offset, bit, opname, (on)?"on":"off");
169 if (on)
170 bitmap[offset] |= 1 << bit;
171 else
172 bitmap[offset] &= ~(1 << bit);
173 }
7c436af3 174 else if (SvPOK(bitspec) && SvCUR(bitspec) == (STRLEN)opset_len) {
6badd1a5
PP
175
176 STRLEN len;
5d7488b2 177 const char * const specbits = SvPV(bitspec, len);
6badd1a5
PP
178 if (opcode_debug >= 2)
179 warn("set_opset_bits opset %s %s\n", opname, (on)?"on":"off");
180 if (on)
181 while(len-- > 0) bitmap[len] |= specbits[len];
182 else
183 while(len-- > 0) bitmap[len] &= ~specbits[len];
184 }
185 else
ff0cee69
PP
186 croak("panic: invalid bitspec for \"%s\" (type %u)",
187 opname, (unsigned)SvTYPE(bitspec));
6badd1a5
PP
188}
189
190
191static void
cea2e8a9 192opmask_add(pTHX_ SV *opset) /* THE ONLY FUNCTION TO EDIT PL_op_mask ITSELF */
6badd1a5
PP
193{
194 int i,j;
195 char *bitmask;
196 STRLEN len;
197 int myopcode = 0;
89ca4ac7 198 dMY_CXT;
6badd1a5 199
cea2e8a9 200 verify_opset(aTHX_ opset,1); /* croaks on bad opset */
6badd1a5 201
3280af22
NIS
202 if (!PL_op_mask) /* caller must ensure PL_op_mask exists */
203 croak("Can't add to uninitialised PL_op_mask");
6badd1a5
PP
204
205 /* OPCODES ALREADY MASKED ARE NEVER UNMASKED. See opmask_addlocal() */
206
207 bitmask = SvPV(opset, len);
208 for (i=0; i < opset_len; i++) {
5d7488b2 209 const U16 bits = bitmask[i];
6badd1a5
PP
210 if (!bits) { /* optimise for sparse masks */
211 myopcode += 8;
212 continue;
213 }
3280af22
NIS
214 for (j=0; j < 8 && myopcode < PL_maxo; )
215 PL_op_mask[myopcode++] |= bits & (1 << j++);
6badd1a5
PP
216 }
217}
218
219static void
cea2e8a9 220opmask_addlocal(pTHX_ SV *opset, char *op_mask_buf) /* Localise PL_op_mask then opmask_add() */
6badd1a5 221{
3280af22 222 char *orig_op_mask = PL_op_mask;
89ca4ac7
JH
223 dMY_CXT;
224
7766f137 225 SAVEVPTR(PL_op_mask);
ac4c12e7
GS
226 /* XXX casting to an ordinary function ptr from a member function ptr
227 * is disallowed by Borland
228 */
6badd1a5 229 if (opcode_debug >= 2)
51371543 230 SAVEDESTRUCTOR((void(*)(void*))Perl_warn,"PL_op_mask restored");
3280af22 231 PL_op_mask = &op_mask_buf[0];
6badd1a5 232 if (orig_op_mask)
3280af22 233 Copy(orig_op_mask, PL_op_mask, PL_maxo, char);
6badd1a5 234 else
3280af22 235 Zero(PL_op_mask, PL_maxo, char);
cea2e8a9 236 opmask_add(aTHX_ opset);
6badd1a5
PP
237}
238
239
240
241MODULE = Opcode PACKAGE = Opcode
242
243PROTOTYPES: ENABLE
244
245BOOT:
89ca4ac7
JH
246{
247 MY_CXT_INIT;
3280af22
NIS
248 assert(PL_maxo < OP_MASK_BUF_SIZE);
249 opset_len = (PL_maxo + 7) / 8;
6badd1a5 250 if (opcode_debug >= 1)
ff0cee69 251 warn("opset_len %ld\n", (long)opset_len);
cea2e8a9 252 op_names_init(aTHX);
89ca4ac7 253}
6badd1a5 254
8c7314f9
JH
255void
256_safe_pkg_prep(Package)
352e1c09 257 SV *Package
8c7314f9
JH
258PPCODE:
259 HV *hv;
260 ENTER;
261
352e1c09 262 hv = gv_stashsv(Package, GV_ADDWARN); /* should exist already */
8c7314f9 263
bfcb3514
NC
264 if (strNE(HvNAME_get(hv),"main")) {
265 /* make it think it's in main:: */
51a37f80 266 hv_name_set(hv, "main", 4, 0);
c33e8be1 267 (void) hv_store(hv,"_",1,(SV *)PL_defgv,0); /* connect _ to global */
8c7314f9
JH
268 SvREFCNT_inc((SV *)PL_defgv); /* want to keep _ around! */
269 }
270 LEAVE;
271
272
273
274
6badd1a5
PP
275
276void
9d8a25dc 277_safe_call_sv(Package, mask, codesv)
352e1c09 278 SV * Package
6badd1a5
PP
279 SV * mask
280 SV * codesv
4d8e9581 281PPCODE:
6badd1a5
PP
282 char op_mask_buf[OP_MASK_BUF_SIZE];
283 GV *gv;
4adfac04 284 HV *dummy_hv;
6badd1a5
PP
285
286 ENTER;
287
cea2e8a9 288 opmask_addlocal(aTHX_ mask, op_mask_buf);
6badd1a5 289
3280af22
NIS
290 save_aptr(&PL_endav);
291 PL_endav = (AV*)sv_2mortal((SV*)newAV()); /* ignore END blocks for now */
6badd1a5 292
e5125a24 293 save_hptr(&PL_defstash); /* save current default stash */
6badd1a5 294 /* the assignment to global defstash changes our sense of 'main' */
352e1c09 295 PL_defstash = gv_stashsv(Package, GV_ADDWARN); /* should exist already */
8c7314f9 296
ed094faf
GS
297 save_hptr(&PL_curstash);
298 PL_curstash = PL_defstash;
6badd1a5
PP
299
300 /* defstash must itself contain a main:: so we'll add that now */
301 /* take care with the ref counts (was cause of long standing bug) */
302 /* XXX I'm still not sure if this is right, GV_ADDWARN should warn! */
352e1c09 303 gv = gv_fetchpvs("main::", GV_ADDWARN, SVt_PVHV);
6badd1a5 304 sv_free((SV*)GvHV(gv));
3280af22 305 GvHV(gv) = (HV*)SvREFCNT_inc(PL_defstash);
6badd1a5 306
e5125a24 307 /* %INC must be clean for use/require in compartment */
4adfac04 308 dummy_hv = save_hash(PL_incgv);
352e1c09 309 GvHV(PL_incgv) = (HV*)SvREFCNT_inc(GvHV(gv_HVadd(gv_fetchpvs("INC",GV_ADD,SVt_PVHV))));
e5125a24 310
4eb3f1b8
RGS
311 /* Invalidate ISA and method caches */
312 ++PL_sub_generation;
313 hv_clear(PL_stashcache);
314
924508f0 315 PUSHMARK(SP);
6badd1a5 316 perl_call_sv(codesv, GIMME|G_EVAL|G_KEEPERR); /* use callers context */
57481f5a 317 sv_free( (SV *) dummy_hv); /* get rid of what save_hash gave us*/
6badd1a5
PP
318 SPAGAIN; /* for the PUTBACK added by xsubpp */
319 LEAVE;
320
321
322int
323verify_opset(opset, fatal = 0)
324 SV *opset
325 int fatal
cea2e8a9
GS
326CODE:
327 RETVAL = verify_opset(aTHX_ opset,fatal);
328OUTPUT:
329 RETVAL
6badd1a5
PP
330
331void
332invert_opset(opset)
333 SV *opset
4d8e9581 334CODE:
6badd1a5
PP
335 {
336 char *bitmap;
89ca4ac7 337 dMY_CXT;
6badd1a5 338 STRLEN len = opset_len;
89ca4ac7 339
cea2e8a9 340 opset = sv_2mortal(new_opset(aTHX_ opset)); /* verify and clone opset */
6badd1a5
PP
341 bitmap = SvPVX(opset);
342 while(len-- > 0)
343 bitmap[len] = ~bitmap[len];
3280af22
NIS
344 /* take care of extra bits beyond PL_maxo in last byte */
345 if (PL_maxo & 07)
346 bitmap[opset_len-1] &= ~(0xFF << (PL_maxo & 0x07));
6badd1a5
PP
347 }
348 ST(0) = opset;
349
350
351void
352opset_to_ops(opset, desc = 0)
353 SV *opset
354 int desc
4d8e9581 355PPCODE:
6badd1a5
PP
356 {
357 STRLEN len;
358 int i, j, myopcode;
5d7488b2 359 const char * const bitmap = SvPV(opset, len);
31fb1209 360 char **names = (desc) ? get_op_descs() : get_op_names();
89ca4ac7
JH
361 dMY_CXT;
362
cea2e8a9 363 verify_opset(aTHX_ opset,1);
6badd1a5 364 for (myopcode=0, i=0; i < opset_len; i++) {
5d7488b2 365 const U16 bits = bitmap[i];
3280af22 366 for (j=0; j < 8 && myopcode < PL_maxo; j++, myopcode++) {
6badd1a5 367 if ( bits & (1 << j) )
d3d34884
NC
368 XPUSHs(newSVpvn_flags(names[myopcode], strlen(names[myopcode]),
369 SVs_TEMP));
6badd1a5
PP
370 }
371 }
372 }
373
374
375void
376opset(...)
4d8e9581 377CODE:
8063af02 378 int i;
5d7488b2 379 SV *bitspec;
6badd1a5 380 STRLEN len, on;
89ca4ac7 381
5d7488b2
AL
382 SV * const opset = sv_2mortal(new_opset(aTHX_ Nullsv));
383 char * const bitmap = SvPVX(opset);
6badd1a5 384 for (i = 0; i < items; i++) {
5d7488b2 385 const char *opname;
6badd1a5 386 on = 1;
cea2e8a9 387 if (verify_opset(aTHX_ ST(i),0)) {
6badd1a5
PP
388 opname = "(opset)";
389 bitspec = ST(i);
390 }
391 else {
392 opname = SvPV(ST(i), len);
393 if (*opname == '!') { on=0; ++opname;--len; }
cea2e8a9 394 bitspec = get_op_bitspec(aTHX_ opname, len, 1);
6badd1a5 395 }
cea2e8a9 396 set_opset_bits(aTHX_ bitmap, bitspec, on, opname);
6badd1a5
PP
397 }
398 ST(0) = opset;
399
400
401#define PERMITING (ix == 0 || ix == 1)
402#define ONLY_THESE (ix == 0 || ix == 2)
403
404void
405permit_only(safe, ...)
406 SV *safe
4d8e9581 407ALIAS:
6badd1a5
PP
408 permit = 1
409 deny_only = 2
410 deny = 3
4d8e9581 411CODE:
5d7488b2 412 int i;
6badd1a5 413 SV *bitspec, *mask;
5d7488b2 414 char *bitmap;
6badd1a5 415 STRLEN len;
89ca4ac7 416 dMY_CXT;
6badd1a5
PP
417
418 if (!SvROK(safe) || !SvOBJECT(SvRV(safe)) || SvTYPE(SvRV(safe))!=SVt_PVHV)
419 croak("Not a Safe object");
420 mask = *hv_fetch((HV*)SvRV(safe), "Mask",4, 1);
421 if (ONLY_THESE) /* *_only = new mask, else edit current */
cea2e8a9 422 sv_setsv(mask, sv_2mortal(new_opset(aTHX_ PERMITING ? opset_all : Nullsv)));
4d8e9581 423 else
cea2e8a9 424 verify_opset(aTHX_ mask,1); /* croaks */
6badd1a5
PP
425 bitmap = SvPVX(mask);
426 for (i = 1; i < items; i++) {
5d7488b2
AL
427 const char *opname;
428 int on = PERMITING ? 0 : 1; /* deny = mask bit on */
cea2e8a9 429 if (verify_opset(aTHX_ ST(i),0)) { /* it's a valid mask */
6badd1a5
PP
430 opname = "(opset)";
431 bitspec = ST(i);
432 }
433 else { /* it's an opname/optag */
434 opname = SvPV(ST(i), len);
435 /* invert if op has ! prefix (only one allowed) */
436 if (*opname == '!') { on = !on; ++opname; --len; }
cea2e8a9 437 bitspec = get_op_bitspec(aTHX_ opname, len, 1); /* croaks */
6badd1a5 438 }
cea2e8a9 439 set_opset_bits(aTHX_ bitmap, bitspec, on, opname);
6badd1a5 440 }
6b88bc9c 441 ST(0) = &PL_sv_yes;
6badd1a5
PP
442
443
444
445void
446opdesc(...)
4d8e9581 447PPCODE:
5d7488b2 448 int i;
6badd1a5
PP
449 STRLEN len;
450 SV **args;
31fb1209 451 char **op_desc = get_op_descs();
89ca4ac7
JH
452 dMY_CXT;
453
6badd1a5
PP
454 /* copy args to a scratch area since we may push output values onto */
455 /* the stack faster than we read values off it if masks are used. */
d3d34884 456 args = (SV**)SvPVX(newSVpvn_flags((char*)&ST(0), items*sizeof(SV*), SVs_TEMP));
6badd1a5 457 for (i = 0; i < items; i++) {
5d7488b2 458 const char * const opname = SvPV(args[i], len);
cea2e8a9 459 SV *bitspec = get_op_bitspec(aTHX_ opname, len, 1);
6badd1a5 460 if (SvIOK(bitspec)) {
5d7488b2 461 const int myopcode = SvIV(bitspec);
3280af22 462 if (myopcode < 0 || myopcode >= PL_maxo)
6badd1a5 463 croak("panic: opcode %d (%s) out of range",myopcode,opname);
d3d34884
NC
464 XPUSHs(newSVpvn_flags(op_desc[myopcode], strlen(op_desc[myopcode]),
465 SVs_TEMP));
6badd1a5 466 }
7c436af3 467 else if (SvPOK(bitspec) && SvCUR(bitspec) == (STRLEN)opset_len) {
6badd1a5 468 int b, j;
2596d9fe 469 const char * const bitmap = SvPV_nolen_const(bitspec);
5d7488b2 470 int myopcode = 0;
6badd1a5 471 for (b=0; b < opset_len; b++) {
5d7488b2 472 const U16 bits = bitmap[b];
3280af22 473 for (j=0; j < 8 && myopcode < PL_maxo; j++, myopcode++)
6badd1a5 474 if (bits & (1 << j))
d3d34884
NC
475 XPUSHs(newSVpvn_flags(op_desc[myopcode],
476 strlen(op_desc[myopcode]),
477 SVs_TEMP));
6badd1a5
PP
478 }
479 }
480 else
ff0cee69
PP
481 croak("panic: invalid bitspec for \"%s\" (type %u)",
482 opname, (unsigned)SvTYPE(bitspec));
6badd1a5
PP
483 }
484
485
486void
487define_optag(optagsv, mask)
488 SV *optagsv
489 SV *mask
4d8e9581 490CODE:
6badd1a5 491 STRLEN len;
5d7488b2 492 const char *optag = SvPV(optagsv, len);
89ca4ac7 493
cea2e8a9 494 put_op_bitspec(aTHX_ optag, len, mask); /* croaks */
6b88bc9c 495 ST(0) = &PL_sv_yes;
6badd1a5
PP
496
497
498void
499empty_opset()
4d8e9581 500CODE:
cea2e8a9 501 ST(0) = sv_2mortal(new_opset(aTHX_ Nullsv));
6badd1a5
PP
502
503void
504full_opset()
4d8e9581 505CODE:
89ca4ac7 506 dMY_CXT;
cea2e8a9 507 ST(0) = sv_2mortal(new_opset(aTHX_ opset_all));
6badd1a5
PP
508
509void
510opmask_add(opset)
511 SV *opset
4d8e9581 512PREINIT:
3280af22 513 if (!PL_op_mask)
a02a5408 514 Newxz(PL_op_mask, PL_maxo, char);
cea2e8a9
GS
515CODE:
516 opmask_add(aTHX_ opset);
6badd1a5
PP
517
518void
519opcodes()
4d8e9581 520PPCODE:
6badd1a5
PP
521 if (GIMME == G_ARRAY) {
522 croak("opcodes in list context not yet implemented"); /* XXX */
523 }
524 else {
3280af22 525 XPUSHs(sv_2mortal(newSViv(PL_maxo)));
6badd1a5
PP
526 }
527
528void
529opmask()
4d8e9581 530CODE:
cea2e8a9 531 ST(0) = sv_2mortal(new_opset(aTHX_ Nullsv));
3280af22 532 if (PL_op_mask) {
5d7488b2 533 char * const bitmap = SvPVX(ST(0));
6badd1a5 534 int myopcode;
3280af22
NIS
535 for(myopcode=0; myopcode < PL_maxo; ++myopcode) {
536 if (PL_op_mask[myopcode])
6badd1a5
PP
537 bitmap[myopcode >> 3] |= 1 << (myopcode & 0x07);
538 }
539 }
540