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 #define MY_CXT_KEY "Opcode::_guts" XS_VERSION
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 */
17 int x_opcode_debug; /* unused warn() emitting debugging code */
23 #define op_named_bits (MY_CXT.x_op_named_bits)
24 #define opset_all (MY_CXT.x_opset_all)
25 #define opset_len (MY_CXT.x_opset_len)
27 # define opcode_debug (MY_CXT.x_opcode_debug)
29 /* no API to turn this on at runtime, so constant fold the code away */
30 # define opcode_debug 0
33 static SV *new_opset (pTHX_ SV *old_opset);
34 static int verify_opset (pTHX_ SV *opset, int fatal);
35 static void set_opset_bits (pTHX_ char *bitmap, SV *bitspec, int on, const char *opname);
36 static void put_op_bitspec (pTHX_ const char *optag, STRLEN len, SV *opset);
37 static SV *get_op_bitspec (pTHX_ const char *opname, STRLEN len, int fatal);
40 /* Initialise our private op_named_bits HV.
41 * It is first loaded with the name and number of each perl operator.
42 * Then the builtin tags :none and :all are added.
43 * Opcode.pm loads the standard optags from __DATA__
44 * XXX leak-alert: data allocated here is never freed, call this
57 op_named_bits = newHV();
58 op_names = get_op_names();
59 for(i=0; i < PL_maxo; ++i) {
60 SV * const sv = newSViv(i);
62 (void) hv_store(op_named_bits, op_names[i], strlen(op_names[i]), sv, 0);
65 put_op_bitspec(aTHX_ STR_WITH_LEN(":none"), sv_2mortal(new_opset(aTHX_ Nullsv)));
67 opset_all = new_opset(aTHX_ Nullsv);
68 bitmap = (U8*)SvPV(opset_all, len);
69 memset(bitmap, 0xFF, len-1); /* deal with last byte specially, see below */
70 /* Take care to set the right number of bits in the last byte */
71 bitmap[len-1] = (PL_maxo & 0x07) ? ((~(0xFF << (PL_maxo & 0x07))) & 0xFF)
73 put_op_bitspec(aTHX_ STR_WITH_LEN(":all"), opset_all); /* don't mortalise */
77 /* Store a new tag definition. Always a mask.
78 * The tag must not already be defined.
79 * SV *mask is copied not referenced.
83 put_op_bitspec(pTHX_ const char *optag, STRLEN len, SV *mask)
88 verify_opset(aTHX_ mask,1);
89 svp = hv_fetch(op_named_bits, optag, len, 1);
91 croak("Opcode tag \"%s\" already defined", optag);
98 /* Fetch a 'bits' entry for an opname or optag (IV/PV).
99 * Note that we return the actual entry for speed.
100 * Always sv_mortalcopy() if returning it to user code.
104 get_op_bitspec(pTHX_ const char *opname, STRLEN len, int fatal)
109 svp = hv_fetch(op_named_bits, opname, len, 0);
110 if (!svp || !SvOK(*svp)) {
114 croak("Unknown operator tag \"%s\"", opname);
115 if (*opname == '!') /* XXX here later, or elsewhere? */
116 croak("Can't negate operators here (\"%s\")", opname);
117 if (isALPHA(*opname))
118 croak("Unknown operator name \"%s\"", opname);
119 croak("Unknown operator prefix \"%s\"", opname);
127 new_opset(pTHX_ SV *old_opset)
133 verify_opset(aTHX_ old_opset,1);
134 opset = newSVsv(old_opset);
137 opset = newSV(opset_len);
138 Zero(SvPVX_const(opset), opset_len + 1, char);
139 SvCUR_set(opset, opset_len);
140 (void)SvPOK_only(opset);
142 /* not mortalised here */
148 verify_opset(pTHX_ SV *opset, int fatal)
150 const char *err = NULL;
153 if (!SvOK(opset)) err = "undefined";
154 else if (!SvPOK(opset)) err = "wrong type";
155 else if (SvCUR(opset) != (STRLEN)opset_len) err = "wrong size";
157 croak("Invalid opset: %s", err);
164 set_opset_bits(pTHX_ char *bitmap, SV *bitspec, int on, const char *opname)
168 if (SvIOK(bitspec)) {
169 const int myopcode = SvIV(bitspec);
170 const int offset = myopcode >> 3;
171 const int bit = myopcode & 0x07;
172 if (myopcode >= PL_maxo || myopcode < 0)
173 croak("panic: opcode \"%s\" value %d is invalid", opname, myopcode);
174 if (opcode_debug >= 2)
175 warn("set_opset_bits bit %2d (off=%d, bit=%d) %s %s\n",
176 myopcode, offset, bit, opname, (on)?"on":"off");
178 bitmap[offset] |= 1 << bit;
180 bitmap[offset] &= ~(1 << bit);
182 else if (SvPOK(bitspec) && SvCUR(bitspec) == (STRLEN)opset_len) {
185 const char * const specbits = SvPV(bitspec, len);
186 if (opcode_debug >= 2)
187 warn("set_opset_bits opset %s %s\n", opname, (on)?"on":"off");
189 while(len-- > 0) bitmap[len] |= specbits[len];
191 while(len-- > 0) bitmap[len] &= ~specbits[len];
194 croak("panic: invalid bitspec for \"%s\" (type %u)",
195 opname, (unsigned)SvTYPE(bitspec));
200 opmask_add(pTHX_ SV *opset) /* THE ONLY FUNCTION TO EDIT PL_op_mask ITSELF */
208 verify_opset(aTHX_ opset,1); /* croaks on bad opset */
210 if (!PL_op_mask) /* caller must ensure PL_op_mask exists */
211 croak("Can't add to uninitialised PL_op_mask");
213 /* OPCODES ALREADY MASKED ARE NEVER UNMASKED. See opmask_addlocal() */
215 bitmask = SvPV(opset, len);
216 for (i=0; i < opset_len; i++) {
217 const U16 bits = bitmask[i];
218 if (!bits) { /* optimise for sparse masks */
222 for (j=0; j < 8 && myopcode < PL_maxo; )
223 PL_op_mask[myopcode++] |= bits & (1 << j++);
228 opmask_addlocal(pTHX_ SV *opset, char *op_mask_buf) /* Localise PL_op_mask then opmask_add() */
230 char *orig_op_mask = PL_op_mask;
235 SAVEVPTR(PL_op_mask);
236 /* XXX casting to an ordinary function ptr from a member function ptr
237 * is disallowed by Borland
239 if (opcode_debug >= 2)
240 SAVEDESTRUCTOR((void(*)(void*))Perl_warn,"PL_op_mask restored");
241 PL_op_mask = &op_mask_buf[0];
243 Copy(orig_op_mask, PL_op_mask, PL_maxo, char);
245 Zero(PL_op_mask, PL_maxo, char);
246 opmask_add(aTHX_ opset);
251 MODULE = Opcode PACKAGE = Opcode
258 STATIC_ASSERT_STMT(PL_maxo < OP_MASK_BUF_SIZE);
259 opset_len = (PL_maxo + 7) / 8;
260 if (opcode_debug >= 1)
261 warn("opset_len %ld\n", (long)opset_len);
266 _safe_pkg_prep(Package)
273 hv = gv_stashsv(Package, GV_ADDWARN); /* should exist already */
275 hvname = HvNAME_get(hv);
276 if (!hvname || strNE(hvname, "main")) {
277 /* make it think it's in main:: */
278 hv_name_set(hv, "main", 4, 0);
279 (void) hv_store(hv,"_",1,(SV *)PL_defgv,0); /* connect _ to global */
280 SvREFCNT_inc((SV *)PL_defgv); /* want to keep _ around! */
289 _safe_call_sv(Package, mask, codesv)
294 char op_mask_buf[OP_MASK_BUF_SIZE];
300 opmask_addlocal(aTHX_ mask, op_mask_buf);
302 save_aptr(&PL_endav);
303 PL_endav = (AV*)sv_2mortal((SV*)newAV()); /* ignore END blocks for now */
305 save_hptr(&PL_defstash); /* save current default stash */
306 /* the assignment to global defstash changes our sense of 'main' */
307 PL_defstash = gv_stashsv(Package, GV_ADDWARN); /* should exist already */
309 SAVEGENERICSV(PL_curstash);
310 PL_curstash = (HV *)SvREFCNT_inc_simple(PL_defstash);
312 /* defstash must itself contain a main:: so we'll add that now */
313 /* take care with the ref counts (was cause of long standing bug) */
314 /* XXX I'm still not sure if this is right, GV_ADDWARN should warn! */
315 gv = gv_fetchpvs("main::", GV_ADDWARN, SVt_PVHV);
316 sv_free((SV*)GvHV(gv));
317 GvHV(gv) = (HV*)SvREFCNT_inc(PL_defstash);
319 /* %INC must be clean for use/require in compartment */
320 dummy_hv = save_hash(PL_incgv);
321 GvHV(PL_incgv) = (HV*)SvREFCNT_inc(GvHV(gv_HVadd(gv_fetchpvs("INC",GV_ADD,SVt_PVHV))));
323 /* Invalidate class and method caches */
325 hv_clear(PL_stashcache);
328 /* use caller’s context */
329 perl_call_sv(codesv, GIMME_V|G_EVAL|G_KEEPERR);
330 sv_free( (SV *) dummy_hv); /* get rid of what save_hash gave us*/
331 SPAGAIN; /* for the PUTBACK added by xsubpp */
334 /* Invalidate again */
336 hv_clear(PL_stashcache);
340 verify_opset(opset, fatal = 0)
344 RETVAL = verify_opset(aTHX_ opset,fatal);
355 STRLEN len = opset_len;
357 opset = sv_2mortal(new_opset(aTHX_ opset)); /* verify and clone opset */
358 bitmap = SvPVX(opset);
360 bitmap[len] = ~bitmap[len];
361 /* take care of extra bits beyond PL_maxo in last byte */
363 bitmap[opset_len-1] &= ~(char)(0xFF << (PL_maxo & 0x07));
369 opset_to_ops(opset, desc = 0)
376 const char * const bitmap = SvPV(opset, len);
377 char **names = (desc) ? get_op_descs() : get_op_names();
380 verify_opset(aTHX_ opset,1);
381 for (myopcode=0, i=0; i < opset_len; i++) {
382 const U16 bits = bitmap[i];
383 for (j=0; j < 8 && myopcode < PL_maxo; j++, myopcode++) {
384 if ( bits & (1 << j) )
385 XPUSHs(newSVpvn_flags(names[myopcode], strlen(names[myopcode]),
399 SV * const opset = sv_2mortal(new_opset(aTHX_ Nullsv));
400 char * const bitmap = SvPVX(opset);
401 for (i = 0; i < items; i++) {
404 if (verify_opset(aTHX_ ST(i),0)) {
409 opname = SvPV(ST(i), len);
410 if (*opname == '!') { on=0; ++opname;--len; }
411 bitspec = get_op_bitspec(aTHX_ opname, len, 1);
413 set_opset_bits(aTHX_ bitmap, bitspec, on, opname);
418 #define PERMITING (ix == 0 || ix == 1)
419 #define ONLY_THESE (ix == 0 || ix == 2)
422 permit_only(safe, ...)
435 if (!SvROK(safe) || !SvOBJECT(SvRV(safe)) || SvTYPE(SvRV(safe))!=SVt_PVHV)
436 croak("Not a Safe object");
437 mask = *hv_fetchs((HV*)SvRV(safe), "Mask", 1);
438 if (ONLY_THESE) /* *_only = new mask, else edit current */
439 sv_setsv(mask, sv_2mortal(new_opset(aTHX_ PERMITING ? opset_all : Nullsv)));
441 verify_opset(aTHX_ mask,1); /* croaks */
442 bitmap = SvPVX(mask);
443 for (i = 1; i < items; i++) {
445 int on = PERMITING ? 0 : 1; /* deny = mask bit on */
446 if (verify_opset(aTHX_ ST(i),0)) { /* it's a valid mask */
450 else { /* it's an opname/optag */
451 opname = SvPV(ST(i), len);
452 /* invert if op has ! prefix (only one allowed) */
453 if (*opname == '!') { on = !on; ++opname; --len; }
454 bitspec = get_op_bitspec(aTHX_ opname, len, 1); /* croaks */
456 set_opset_bits(aTHX_ bitmap, bitspec, on, opname);
468 char **op_desc = get_op_descs();
471 /* copy args to a scratch area since we may push output values onto */
472 /* the stack faster than we read values off it if masks are used. */
473 args = (SV**)SvPVX(newSVpvn_flags((char*)&ST(0), items*sizeof(SV*), SVs_TEMP));
474 for (i = 0; i < items; i++) {
475 const char * const opname = SvPV(args[i], len);
476 SV *bitspec = get_op_bitspec(aTHX_ opname, len, 1);
477 if (SvIOK(bitspec)) {
478 const int myopcode = SvIV(bitspec);
479 if (myopcode < 0 || myopcode >= PL_maxo)
480 croak("panic: opcode %d (%s) out of range",myopcode,opname);
481 XPUSHs(newSVpvn_flags(op_desc[myopcode], strlen(op_desc[myopcode]),
484 else if (SvPOK(bitspec) && SvCUR(bitspec) == (STRLEN)opset_len) {
486 const char * const bitmap = SvPV_nolen_const(bitspec);
488 for (b=0; b < opset_len; b++) {
489 const U16 bits = bitmap[b];
490 for (j=0; j < 8 && myopcode < PL_maxo; j++, myopcode++)
492 XPUSHs(newSVpvn_flags(op_desc[myopcode],
493 strlen(op_desc[myopcode]),
498 croak("panic: invalid bitspec for \"%s\" (type %u)",
499 opname, (unsigned)SvTYPE(bitspec));
504 define_optag(optagsv, mask)
509 const char *optag = SvPV(optagsv, len);
511 put_op_bitspec(aTHX_ optag, len, mask); /* croaks */
518 ST(0) = sv_2mortal(new_opset(aTHX_ Nullsv));
524 ST(0) = sv_2mortal(new_opset(aTHX_ opset_all));
531 Newxz(PL_op_mask, PL_maxo, char);
533 opmask_add(aTHX_ opset);
538 if (GIMME_V == G_ARRAY) {
539 croak("opcodes in list context not yet implemented"); /* XXX */
542 XPUSHs(sv_2mortal(newSViv(PL_maxo)));
548 ST(0) = sv_2mortal(new_opset(aTHX_ Nullsv));
550 char * const bitmap = SvPVX(ST(0));
552 for(myopcode=0; myopcode < PL_maxo; ++myopcode) {
553 if (PL_op_mask[myopcode])
554 bitmap[myopcode >> 3] |= 1 << (myopcode & 0x07);