5 /* maxo shouldn't differ from MAXO but leave room anyway (see BOOT:) */
6 #define OP_MASK_BUF_SIZE (MAXO + 100)
8 static HV *op_named_bits; /* cache shared for whole process */
9 static SV *opset_all; /* mask with all bits set */
10 static IV opset_len; /* length of opmasks in bytes */
11 static int opcode_debug = 0;
13 static SV *new_opset _((SV *old_opset));
14 static int verify_opset _((SV *opset, int fatal));
15 static void set_opset_bits _((char *bitmap, SV *bitspec, int on, char *opname));
16 static void put_op_bitspec _((char *optag, STRLEN len, SV *opset));
17 static SV *get_op_bitspec _((char *opname, STRLEN len, int fatal));
20 /* Initialise our private op_named_bits HV.
21 * It is first loaded with the name and number of each perl operator.
22 * Then the builtin tags :none and :all are added.
23 * Opcode.pm loads the standard optags from __DATA__
34 op_named_bits = newHV();
35 op_names = get_op_names();
36 for(i=0; i < maxo; ++i) {
40 hv_store(op_named_bits, op_names[i], strlen(op_names[i]), sv, 0);
43 put_op_bitspec(":none",0, sv_2mortal(new_opset(Nullsv)));
45 opset_all = new_opset(Nullsv);
46 bitmap = SvPV(opset_all, len);
47 i = len-1; /* deal with last byte specially, see below */
50 /* Take care to set the right number of bits in the last byte */
51 bitmap[len-1] = (maxo & 0x07) ? ~(0xFF << (maxo & 0x07)) : 0xFF;
52 put_op_bitspec(":all",0, opset_all); /* don't mortalise */
56 /* Store a new tag definition. Always a mask.
57 * The tag must not already be defined.
58 * SV *mask is copied not referenced.
62 put_op_bitspec(char *optag, STRLEN len, SV *mask)
68 svp = hv_fetch(op_named_bits, optag, len, 1);
70 croak("Opcode tag \"%s\" already defined", optag);
77 /* Fetch a 'bits' entry for an opname or optag (IV/PV).
78 * Note that we return the actual entry for speed.
79 * Always sv_mortalcopy() if returing it to user code.
83 get_op_bitspec(char *opname, STRLEN len, int fatal)
88 svp = hv_fetch(op_named_bits, opname, len, 0);
89 if (!svp || !SvOK(*svp)) {
93 croak("Unknown operator tag \"%s\"", opname);
94 if (*opname == '!') /* XXX here later, or elsewhere? */
95 croak("Can't negate operators here (\"%s\")", opname);
97 croak("Unknown operator name \"%s\"", opname);
98 croak("Unknown operator prefix \"%s\"", opname);
106 new_opset(SV *old_opset)
110 verify_opset(old_opset,1);
111 opset = newSVsv(old_opset);
114 opset = NEWSV(1156, opset_len);
115 Zero(SvPVX(opset), opset_len + 1, char);
116 SvCUR_set(opset, opset_len);
117 (void)SvPOK_only(opset);
119 /* not mortalised here */
125 verify_opset(SV *opset, int fatal)
128 if (!SvOK(opset)) err = "undefined";
129 else if (!SvPOK(opset)) err = "wrong type";
130 else if (SvCUR(opset) != opset_len) err = "wrong size";
132 croak("Invalid opset: %s", err);
139 set_opset_bits(char *bitmap, SV *bitspec, int on, char *opname)
141 if (SvIOK(bitspec)) {
142 int myopcode = SvIV(bitspec);
143 int offset = myopcode >> 3;
144 int bit = myopcode & 0x07;
145 if (myopcode >= maxo || myopcode < 0)
146 croak("panic: opcode \"%s\" value %d is invalid", opname, myopcode);
147 if (opcode_debug >= 2)
148 warn("set_opset_bits bit %2d (off=%d, bit=%d) %s %s\n",
149 myopcode, offset, bit, opname, (on)?"on":"off");
151 bitmap[offset] |= 1 << bit;
153 bitmap[offset] &= ~(1 << bit);
155 else if (SvPOK(bitspec) && SvCUR(bitspec) == opset_len) {
158 char *specbits = SvPV(bitspec, len);
159 if (opcode_debug >= 2)
160 warn("set_opset_bits opset %s %s\n", opname, (on)?"on":"off");
162 while(len-- > 0) bitmap[len] |= specbits[len];
164 while(len-- > 0) bitmap[len] &= ~specbits[len];
167 croak("panic: invalid bitspec for \"%s\" (type %u)",
168 opname, (unsigned)SvTYPE(bitspec));
173 opmask_add(SV *opset) /* THE ONLY FUNCTION TO EDIT op_mask ITSELF */
180 verify_opset(opset,1); /* croaks on bad opset */
182 if (!op_mask) /* caller must ensure op_mask exists */
183 croak("Can't add to uninitialised op_mask");
185 /* OPCODES ALREADY MASKED ARE NEVER UNMASKED. See opmask_addlocal() */
187 bitmask = SvPV(opset, len);
188 for (i=0; i < opset_len; i++) {
189 U16 bits = bitmask[i];
190 if (!bits) { /* optimise for sparse masks */
194 for (j=0; j < 8 && myopcode < maxo; )
195 op_mask[myopcode++] |= bits & (1 << j++);
200 opmask_addlocal(SV *opset, char *op_mask_buf) /* Localise op_mask then opmask_add() */
202 char *orig_op_mask = op_mask;
204 if (opcode_debug >= 2)
205 SAVEDESTRUCTOR((void(*)_((void*)))warn,"op_mask restored");
206 op_mask = &op_mask_buf[0];
208 Copy(orig_op_mask, op_mask, maxo, char);
210 Zero(op_mask, maxo, char);
216 MODULE = Opcode PACKAGE = Opcode
221 assert(maxo < OP_MASK_BUF_SIZE);
222 opset_len = (maxo + 7) / 8;
223 if (opcode_debug >= 1)
224 warn("opset_len %ld\n", (long)opset_len);
229 _safe_call_sv(package, mask, codesv)
234 char op_mask_buf[OP_MASK_BUF_SIZE];
239 opmask_addlocal(mask, op_mask_buf);
242 endav = (AV*)sv_2mortal((SV*)newAV()); /* ignore END blocks for now */
244 save_hptr(&defstash); /* save current default stack */
245 /* the assignment to global defstash changes our sense of 'main' */
246 defstash = gv_stashpv(package, GV_ADDWARN); /* should exist already */
248 /* defstash must itself contain a main:: so we'll add that now */
249 /* take care with the ref counts (was cause of long standing bug) */
250 /* XXX I'm still not sure if this is right, GV_ADDWARN should warn! */
251 gv = gv_fetchpv("main::", GV_ADDWARN, SVt_PVHV);
252 sv_free((SV*)GvHV(gv));
253 GvHV(gv) = (HV*)SvREFCNT_inc(defstash);
256 perl_call_sv(codesv, GIMME|G_EVAL|G_KEEPERR); /* use callers context */
257 SPAGAIN; /* for the PUTBACK added by xsubpp */
262 verify_opset(opset, fatal = 0)
273 STRLEN len = opset_len;
274 opset = new_opset(opset); /* verify and clone opset */
275 bitmap = SvPVX(opset);
277 bitmap[len] = ~bitmap[len];
278 /* take care of extra bits beyond maxo in last byte */
280 bitmap[opset_len-1] &= ~(0xFF << (maxo & 0x07));
286 opset_to_ops(opset, desc = 0)
293 char *bitmap = SvPV(opset, len);
294 char **names = (desc) ? get_op_descs() : get_op_names();
295 verify_opset(opset,1);
296 for (myopcode=0, i=0; i < opset_len; i++) {
297 U16 bits = bitmap[i];
298 for (j=0; j < 8 && myopcode < maxo; j++, myopcode++) {
299 if ( bits & (1 << j) )
300 XPUSHs(sv_2mortal(newSVpv(names[myopcode], 0)));
313 opset = new_opset(Nullsv);
314 bitmap = SvPVX(opset);
315 for (i = 0; i < items; i++) {
318 if (verify_opset(ST(i),0)) {
323 opname = SvPV(ST(i), len);
324 if (*opname == '!') { on=0; ++opname;--len; }
325 bitspec = get_op_bitspec(opname, len, 1);
327 set_opset_bits(bitmap, bitspec, on, opname);
332 #define PERMITING (ix == 0 || ix == 1)
333 #define ONLY_THESE (ix == 0 || ix == 2)
336 permit_only(safe, ...)
345 char *bitmap, *opname;
348 if (!SvROK(safe) || !SvOBJECT(SvRV(safe)) || SvTYPE(SvRV(safe))!=SVt_PVHV)
349 croak("Not a Safe object");
350 mask = *hv_fetch((HV*)SvRV(safe), "Mask",4, 1);
351 if (ONLY_THESE) /* *_only = new mask, else edit current */
352 sv_setsv(mask, new_opset(PERMITING ? opset_all : Nullsv));
353 else verify_opset(mask,1); /* croaks */
354 bitmap = SvPVX(mask);
355 for (i = 1; i < items; i++) {
356 on = PERMITING ? 0 : 1; /* deny = mask bit on */
357 if (verify_opset(ST(i),0)) { /* it's a valid mask */
361 else { /* it's an opname/optag */
362 opname = SvPV(ST(i), len);
363 /* invert if op has ! prefix (only one allowed) */
364 if (*opname == '!') { on = !on; ++opname; --len; }
365 bitspec = get_op_bitspec(opname, len, 1); /* croaks */
367 set_opset_bits(bitmap, bitspec, on, opname);
379 char **op_desc = get_op_descs();
380 /* copy args to a scratch area since we may push output values onto */
381 /* the stack faster than we read values off it if masks are used. */
382 args = (SV**)SvPVX(sv_2mortal(newSVpv((char*)&ST(0), items*sizeof(SV*))));
383 for (i = 0; i < items; i++) {
384 char *opname = SvPV(args[i], len);
385 SV *bitspec = get_op_bitspec(opname, len, 1);
386 if (SvIOK(bitspec)) {
387 myopcode = SvIV(bitspec);
388 if (myopcode < 0 || myopcode >= maxo)
389 croak("panic: opcode %d (%s) out of range",myopcode,opname);
390 XPUSHs(sv_2mortal(newSVpv(op_desc[myopcode], 0)));
392 else if (SvPOK(bitspec) && SvCUR(bitspec) == opset_len) {
394 char *bitmap = SvPV(bitspec,na);
396 for (b=0; b < opset_len; b++) {
397 U16 bits = bitmap[b];
398 for (j=0; j < 8 && myopcode < maxo; j++, myopcode++)
400 XPUSHs(sv_2mortal(newSVpv(op_desc[myopcode], 0)));
404 croak("panic: invalid bitspec for \"%s\" (type %u)",
405 opname, (unsigned)SvTYPE(bitspec));
410 define_optag(optagsv, mask)
415 char *optag = SvPV(optagsv, len);
416 put_op_bitspec(optag, len, mask); /* croaks */
423 ST(0) = sv_2mortal(new_opset(Nullsv));
428 ST(0) = sv_2mortal(new_opset(opset_all));
435 Newz(0, op_mask, maxo, char);
440 if (GIMME == G_ARRAY) {
441 croak("opcodes in list context not yet implemented"); /* XXX */
444 XPUSHs(sv_2mortal(newSViv(maxo)));
450 ST(0) = sv_2mortal(new_opset(Nullsv));
452 char *bitmap = SvPVX(ST(0));
454 for(myopcode=0; myopcode < maxo; ++myopcode) {
455 if (op_mask[myopcode])
456 bitmap[myopcode >> 3] |= 1 << (myopcode & 0x07);