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 !(defined(PERL_OBJECT) && defined(__BORLANDC__))
205 /* XXX casting to an ordinary function ptr from a member function ptr
206 * is disallowed by Borland
208 if (opcode_debug >= 2)
209 SAVEDESTRUCTOR((void(CPERLscope(*))_((void*)))warn,"op_mask restored");
211 op_mask = &op_mask_buf[0];
213 Copy(orig_op_mask, op_mask, maxo, char);
215 Zero(op_mask, maxo, char);
221 MODULE = Opcode PACKAGE = Opcode
226 assert(maxo < OP_MASK_BUF_SIZE);
227 opset_len = (maxo + 7) / 8;
228 if (opcode_debug >= 1)
229 warn("opset_len %ld\n", (long)opset_len);
234 _safe_call_sv(Package, mask, codesv)
239 char op_mask_buf[OP_MASK_BUF_SIZE];
244 opmask_addlocal(mask, op_mask_buf);
247 endav = (AV*)sv_2mortal((SV*)newAV()); /* ignore END blocks for now */
249 save_hptr(&defstash); /* save current default stack */
250 /* the assignment to global defstash changes our sense of 'main' */
251 defstash = gv_stashpv(Package, GV_ADDWARN); /* should exist already */
253 /* defstash must itself contain a main:: so we'll add that now */
254 /* take care with the ref counts (was cause of long standing bug) */
255 /* XXX I'm still not sure if this is right, GV_ADDWARN should warn! */
256 gv = gv_fetchpv("main::", GV_ADDWARN, SVt_PVHV);
257 sv_free((SV*)GvHV(gv));
258 GvHV(gv) = (HV*)SvREFCNT_inc(defstash);
261 perl_call_sv(codesv, GIMME|G_EVAL|G_KEEPERR); /* use callers context */
262 SPAGAIN; /* for the PUTBACK added by xsubpp */
267 verify_opset(opset, fatal = 0)
278 STRLEN len = opset_len;
279 opset = new_opset(opset); /* verify and clone opset */
280 bitmap = SvPVX(opset);
282 bitmap[len] = ~bitmap[len];
283 /* take care of extra bits beyond maxo in last byte */
285 bitmap[opset_len-1] &= ~(0xFF << (maxo & 0x07));
291 opset_to_ops(opset, desc = 0)
298 char *bitmap = SvPV(opset, len);
299 char **names = (desc) ? get_op_descs() : get_op_names();
300 verify_opset(opset,1);
301 for (myopcode=0, i=0; i < opset_len; i++) {
302 U16 bits = bitmap[i];
303 for (j=0; j < 8 && myopcode < maxo; j++, myopcode++) {
304 if ( bits & (1 << j) )
305 XPUSHs(sv_2mortal(newSVpv(names[myopcode], 0)));
318 opset = new_opset(Nullsv);
319 bitmap = SvPVX(opset);
320 for (i = 0; i < items; i++) {
323 if (verify_opset(ST(i),0)) {
328 opname = SvPV(ST(i), len);
329 if (*opname == '!') { on=0; ++opname;--len; }
330 bitspec = get_op_bitspec(opname, len, 1);
332 set_opset_bits(bitmap, bitspec, on, opname);
337 #define PERMITING (ix == 0 || ix == 1)
338 #define ONLY_THESE (ix == 0 || ix == 2)
341 permit_only(safe, ...)
350 char *bitmap, *opname;
353 if (!SvROK(safe) || !SvOBJECT(SvRV(safe)) || SvTYPE(SvRV(safe))!=SVt_PVHV)
354 croak("Not a Safe object");
355 mask = *hv_fetch((HV*)SvRV(safe), "Mask",4, 1);
356 if (ONLY_THESE) /* *_only = new mask, else edit current */
357 sv_setsv(mask, new_opset(PERMITING ? opset_all : Nullsv));
358 else verify_opset(mask,1); /* croaks */
359 bitmap = SvPVX(mask);
360 for (i = 1; i < items; i++) {
361 on = PERMITING ? 0 : 1; /* deny = mask bit on */
362 if (verify_opset(ST(i),0)) { /* it's a valid mask */
366 else { /* it's an opname/optag */
367 opname = SvPV(ST(i), len);
368 /* invert if op has ! prefix (only one allowed) */
369 if (*opname == '!') { on = !on; ++opname; --len; }
370 bitspec = get_op_bitspec(opname, len, 1); /* croaks */
372 set_opset_bits(bitmap, bitspec, on, opname);
384 char **op_desc = get_op_descs();
385 /* copy args to a scratch area since we may push output values onto */
386 /* the stack faster than we read values off it if masks are used. */
387 args = (SV**)SvPVX(sv_2mortal(newSVpv((char*)&ST(0), items*sizeof(SV*))));
388 for (i = 0; i < items; i++) {
389 char *opname = SvPV(args[i], len);
390 SV *bitspec = get_op_bitspec(opname, len, 1);
391 if (SvIOK(bitspec)) {
392 myopcode = SvIV(bitspec);
393 if (myopcode < 0 || myopcode >= maxo)
394 croak("panic: opcode %d (%s) out of range",myopcode,opname);
395 XPUSHs(sv_2mortal(newSVpv(op_desc[myopcode], 0)));
397 else if (SvPOK(bitspec) && SvCUR(bitspec) == opset_len) {
399 char *bitmap = SvPV(bitspec,na);
401 for (b=0; b < opset_len; b++) {
402 U16 bits = bitmap[b];
403 for (j=0; j < 8 && myopcode < maxo; j++, myopcode++)
405 XPUSHs(sv_2mortal(newSVpv(op_desc[myopcode], 0)));
409 croak("panic: invalid bitspec for \"%s\" (type %u)",
410 opname, (unsigned)SvTYPE(bitspec));
415 define_optag(optagsv, mask)
420 char *optag = SvPV(optagsv, len);
421 put_op_bitspec(optag, len, mask); /* croaks */
428 ST(0) = sv_2mortal(new_opset(Nullsv));
433 ST(0) = sv_2mortal(new_opset(opset_all));
440 Newz(0, op_mask, maxo, char);
445 if (GIMME == G_ARRAY) {
446 croak("opcodes in list context not yet implemented"); /* XXX */
449 XPUSHs(sv_2mortal(newSViv(maxo)));
455 ST(0) = sv_2mortal(new_opset(Nullsv));
457 char *bitmap = SvPVX(ST(0));
459 for(myopcode=0; myopcode < maxo; ++myopcode) {
460 if (op_mask[myopcode])
461 bitmap[myopcode >> 3] |= 1 << (myopcode & 0x07);