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