1 #define PERL_NO_GET_CONTEXT
6 /* PL_maxo shouldn't differ from MAXO but leave room anyway (see BOOT:) */
7 #define OP_MASK_BUF_SIZE (MAXO + 100)
9 /* XXX op_named_bits and opset_all are never freed */
10 static HV *op_named_bits; /* cache shared for whole process */
11 static SV *opset_all; /* mask with all bits set */
12 static IV opset_len; /* length of opmasks in bytes */
13 static int opcode_debug = 0;
15 static SV *new_opset (pTHX_ SV *old_opset);
16 static int verify_opset (pTHX_ SV *opset, int fatal);
17 static void set_opset_bits (pTHX_ char *bitmap, SV *bitspec, int on, char *opname);
18 static void put_op_bitspec (pTHX_ char *optag, STRLEN len, SV *opset);
19 static SV *get_op_bitspec (pTHX_ char *opname, STRLEN len, int fatal);
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__
26 * XXX leak-alert: data allocated here is never freed, call this
38 op_named_bits = newHV();
39 op_names = get_op_names();
40 for(i=0; i < PL_maxo; ++i) {
44 hv_store(op_named_bits, op_names[i], strlen(op_names[i]), sv, 0);
47 put_op_bitspec(aTHX_ ":none",0, sv_2mortal(new_opset(aTHX_ Nullsv)));
49 opset_all = new_opset(aTHX_ Nullsv);
50 bitmap = SvPV(opset_all, len);
51 i = len-1; /* deal with last byte specially, see below */
54 /* Take care to set the right number of bits in the last byte */
55 bitmap[len-1] = (PL_maxo & 0x07) ? ~(0xFF << (PL_maxo & 0x07)) : 0xFF;
56 put_op_bitspec(aTHX_ ":all",0, opset_all); /* don't mortalise */
60 /* Store a new tag definition. Always a mask.
61 * The tag must not already be defined.
62 * SV *mask is copied not referenced.
66 put_op_bitspec(pTHX_ char *optag, STRLEN len, SV *mask)
69 verify_opset(aTHX_ mask,1);
72 svp = hv_fetch(op_named_bits, optag, len, 1);
74 croak("Opcode tag \"%s\" already defined", optag);
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.
87 get_op_bitspec(pTHX_ char *opname, STRLEN len, int fatal)
92 svp = hv_fetch(op_named_bits, opname, len, 0);
93 if (!svp || !SvOK(*svp)) {
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);
110 new_opset(pTHX_ SV *old_opset)
114 verify_opset(aTHX_ old_opset,1);
115 opset = newSVsv(old_opset);
118 opset = NEWSV(1156, opset_len);
119 Zero(SvPVX(opset), opset_len + 1, char);
120 SvCUR_set(opset, opset_len);
121 (void)SvPOK_only(opset);
123 /* not mortalised here */
129 verify_opset(pTHX_ SV *opset, int fatal)
132 if (!SvOK(opset)) err = "undefined";
133 else if (!SvPOK(opset)) err = "wrong type";
134 else if (SvCUR(opset) != opset_len) err = "wrong size";
136 croak("Invalid opset: %s", err);
143 set_opset_bits(pTHX_ char *bitmap, SV *bitspec, int on, char *opname)
145 if (SvIOK(bitspec)) {
146 int myopcode = SvIV(bitspec);
147 int offset = myopcode >> 3;
148 int bit = myopcode & 0x07;
149 if (myopcode >= PL_maxo || myopcode < 0)
150 croak("panic: opcode \"%s\" value %d is invalid", opname, myopcode);
151 if (opcode_debug >= 2)
152 warn("set_opset_bits bit %2d (off=%d, bit=%d) %s %s\n",
153 myopcode, offset, bit, opname, (on)?"on":"off");
155 bitmap[offset] |= 1 << bit;
157 bitmap[offset] &= ~(1 << bit);
159 else if (SvPOK(bitspec) && SvCUR(bitspec) == opset_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");
166 while(len-- > 0) bitmap[len] |= specbits[len];
168 while(len-- > 0) bitmap[len] &= ~specbits[len];
171 croak("panic: invalid bitspec for \"%s\" (type %u)",
172 opname, (unsigned)SvTYPE(bitspec));
177 opmask_add(pTHX_ SV *opset) /* THE ONLY FUNCTION TO EDIT PL_op_mask ITSELF */
184 verify_opset(aTHX_ opset,1); /* croaks on bad opset */
186 if (!PL_op_mask) /* caller must ensure PL_op_mask exists */
187 croak("Can't add to uninitialised PL_op_mask");
189 /* OPCODES ALREADY MASKED ARE NEVER UNMASKED. See opmask_addlocal() */
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 */
198 for (j=0; j < 8 && myopcode < PL_maxo; )
199 PL_op_mask[myopcode++] |= bits & (1 << j++);
204 opmask_addlocal(pTHX_ SV *opset, char *op_mask_buf) /* Localise PL_op_mask then opmask_add() */
206 char *orig_op_mask = PL_op_mask;
207 SAVEPPTR(PL_op_mask);
208 #if !defined(PERL_OBJECT)
209 /* XXX casting to an ordinary function ptr from a member function ptr
210 * is disallowed by Borland
212 if (opcode_debug >= 2)
213 SAVEDESTRUCTOR((void(*)(void*))Perl_warn,"PL_op_mask restored");
215 PL_op_mask = &op_mask_buf[0];
217 Copy(orig_op_mask, PL_op_mask, PL_maxo, char);
219 Zero(PL_op_mask, PL_maxo, char);
220 opmask_add(aTHX_ opset);
225 MODULE = Opcode PACKAGE = Opcode
230 assert(PL_maxo < OP_MASK_BUF_SIZE);
231 opset_len = (PL_maxo + 7) / 8;
232 if (opcode_debug >= 1)
233 warn("opset_len %ld\n", (long)opset_len);
238 _safe_call_sv(Package, mask, codesv)
243 char op_mask_buf[OP_MASK_BUF_SIZE];
248 opmask_addlocal(aTHX_ mask, op_mask_buf);
250 save_aptr(&PL_endav);
251 PL_endav = (AV*)sv_2mortal((SV*)newAV()); /* ignore END blocks for now */
253 save_hptr(&PL_defstash); /* save current default stack */
254 /* the assignment to global defstash changes our sense of 'main' */
255 PL_defstash = gv_stashpv(Package, GV_ADDWARN); /* should exist already */
256 save_hptr(&PL_curstash);
257 PL_curstash = PL_defstash;
259 /* defstash must itself contain a main:: so we'll add that now */
260 /* take care with the ref counts (was cause of long standing bug) */
261 /* XXX I'm still not sure if this is right, GV_ADDWARN should warn! */
262 gv = gv_fetchpv("main::", GV_ADDWARN, SVt_PVHV);
263 sv_free((SV*)GvHV(gv));
264 GvHV(gv) = (HV*)SvREFCNT_inc(PL_defstash);
267 perl_call_sv(codesv, GIMME|G_EVAL|G_KEEPERR); /* use callers context */
268 SPAGAIN; /* for the PUTBACK added by xsubpp */
273 verify_opset(opset, fatal = 0)
277 RETVAL = verify_opset(aTHX_ opset,fatal);
287 STRLEN len = opset_len;
288 opset = sv_2mortal(new_opset(aTHX_ opset)); /* verify and clone opset */
289 bitmap = SvPVX(opset);
291 bitmap[len] = ~bitmap[len];
292 /* take care of extra bits beyond PL_maxo in last byte */
294 bitmap[opset_len-1] &= ~(0xFF << (PL_maxo & 0x07));
300 opset_to_ops(opset, desc = 0)
307 char *bitmap = SvPV(opset, len);
308 char **names = (desc) ? get_op_descs() : get_op_names();
309 verify_opset(aTHX_ opset,1);
310 for (myopcode=0, i=0; i < opset_len; i++) {
311 U16 bits = bitmap[i];
312 for (j=0; j < 8 && myopcode < PL_maxo; j++, myopcode++) {
313 if ( bits & (1 << j) )
314 XPUSHs(sv_2mortal(newSVpv(names[myopcode], 0)));
327 opset = sv_2mortal(new_opset(aTHX_ Nullsv));
328 bitmap = SvPVX(opset);
329 for (i = 0; i < items; i++) {
332 if (verify_opset(aTHX_ ST(i),0)) {
337 opname = SvPV(ST(i), len);
338 if (*opname == '!') { on=0; ++opname;--len; }
339 bitspec = get_op_bitspec(aTHX_ opname, len, 1);
341 set_opset_bits(aTHX_ bitmap, bitspec, on, opname);
346 #define PERMITING (ix == 0 || ix == 1)
347 #define ONLY_THESE (ix == 0 || ix == 2)
350 permit_only(safe, ...)
359 char *bitmap, *opname;
362 if (!SvROK(safe) || !SvOBJECT(SvRV(safe)) || SvTYPE(SvRV(safe))!=SVt_PVHV)
363 croak("Not a Safe object");
364 mask = *hv_fetch((HV*)SvRV(safe), "Mask",4, 1);
365 if (ONLY_THESE) /* *_only = new mask, else edit current */
366 sv_setsv(mask, sv_2mortal(new_opset(aTHX_ PERMITING ? opset_all : Nullsv)));
368 verify_opset(aTHX_ mask,1); /* croaks */
369 bitmap = SvPVX(mask);
370 for (i = 1; i < items; i++) {
371 on = PERMITING ? 0 : 1; /* deny = mask bit on */
372 if (verify_opset(aTHX_ ST(i),0)) { /* it's a valid mask */
376 else { /* it's an opname/optag */
377 opname = SvPV(ST(i), len);
378 /* invert if op has ! prefix (only one allowed) */
379 if (*opname == '!') { on = !on; ++opname; --len; }
380 bitspec = get_op_bitspec(aTHX_ opname, len, 1); /* croaks */
382 set_opset_bits(aTHX_ bitmap, bitspec, on, opname);
394 char **op_desc = get_op_descs();
395 /* copy args to a scratch area since we may push output values onto */
396 /* the stack faster than we read values off it if masks are used. */
397 args = (SV**)SvPVX(sv_2mortal(newSVpvn((char*)&ST(0), items*sizeof(SV*))));
398 for (i = 0; i < items; i++) {
399 char *opname = SvPV(args[i], len);
400 SV *bitspec = get_op_bitspec(aTHX_ opname, len, 1);
401 if (SvIOK(bitspec)) {
402 myopcode = SvIV(bitspec);
403 if (myopcode < 0 || myopcode >= PL_maxo)
404 croak("panic: opcode %d (%s) out of range",myopcode,opname);
405 XPUSHs(sv_2mortal(newSVpv(op_desc[myopcode], 0)));
407 else if (SvPOK(bitspec) && SvCUR(bitspec) == opset_len) {
410 char *bitmap = SvPV(bitspec,n_a);
412 for (b=0; b < opset_len; b++) {
413 U16 bits = bitmap[b];
414 for (j=0; j < 8 && myopcode < PL_maxo; j++, myopcode++)
416 XPUSHs(sv_2mortal(newSVpv(op_desc[myopcode], 0)));
420 croak("panic: invalid bitspec for \"%s\" (type %u)",
421 opname, (unsigned)SvTYPE(bitspec));
426 define_optag(optagsv, mask)
431 char *optag = SvPV(optagsv, len);
432 put_op_bitspec(aTHX_ optag, len, mask); /* croaks */
439 ST(0) = sv_2mortal(new_opset(aTHX_ Nullsv));
444 ST(0) = sv_2mortal(new_opset(aTHX_ opset_all));
451 Newz(0, PL_op_mask, PL_maxo, char);
453 opmask_add(aTHX_ opset);
458 if (GIMME == G_ARRAY) {
459 croak("opcodes in list context not yet implemented"); /* XXX */
462 XPUSHs(sv_2mortal(newSViv(PL_maxo)));
468 ST(0) = sv_2mortal(new_opset(aTHX_ Nullsv));
470 char *bitmap = SvPVX(ST(0));
472 for(myopcode=0; myopcode < PL_maxo; ++myopcode) {
473 if (PL_op_mask[myopcode])
474 bitmap[myopcode >> 3] |= 1 << (myopcode & 0x07);