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 = 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;
72 put_op_bitspec(aTHX_ STR_WITH_LEN(":all"), opset_all); /* don't mortalise */
76 /* Store a new tag definition. Always a mask.
77 * The tag must not already be defined.
78 * SV *mask is copied not referenced.
82 put_op_bitspec(pTHX_ const char *optag, STRLEN len, SV *mask)
87 verify_opset(aTHX_ mask,1);
88 svp = hv_fetch(op_named_bits, optag, len, 1);
90 croak("Opcode tag \"%s\" already defined", optag);
97 /* Fetch a 'bits' entry for an opname or optag (IV/PV).
98 * Note that we return the actual entry for speed.
99 * Always sv_mortalcopy() if returning it to user code.
103 get_op_bitspec(pTHX_ const char *opname, STRLEN len, int fatal)
108 svp = hv_fetch(op_named_bits, opname, len, 0);
109 if (!svp || !SvOK(*svp)) {
113 croak("Unknown operator tag \"%s\"", opname);
114 if (*opname == '!') /* XXX here later, or elsewhere? */
115 croak("Can't negate operators here (\"%s\")", opname);
116 if (isALPHA(*opname))
117 croak("Unknown operator name \"%s\"", opname);
118 croak("Unknown operator prefix \"%s\"", opname);
126 new_opset(pTHX_ SV *old_opset)
132 verify_opset(aTHX_ old_opset,1);
133 opset = newSVsv(old_opset);
136 opset = newSV(opset_len);
137 Zero(SvPVX_const(opset), opset_len + 1, char);
138 SvCUR_set(opset, opset_len);
139 (void)SvPOK_only(opset);
141 /* not mortalised here */
147 verify_opset(pTHX_ SV *opset, int fatal)
149 const char *err = NULL;
152 if (!SvOK(opset)) err = "undefined";
153 else if (!SvPOK(opset)) err = "wrong type";
154 else if (SvCUR(opset) != (STRLEN)opset_len) err = "wrong size";
156 croak("Invalid opset: %s", err);
163 set_opset_bits(pTHX_ char *bitmap, SV *bitspec, int on, const char *opname)
167 if (SvIOK(bitspec)) {
168 const int myopcode = SvIV(bitspec);
169 const int offset = myopcode >> 3;
170 const int bit = myopcode & 0x07;
171 if (myopcode >= PL_maxo || myopcode < 0)
172 croak("panic: opcode \"%s\" value %d is invalid", opname, myopcode);
173 if (opcode_debug >= 2)
174 warn("set_opset_bits bit %2d (off=%d, bit=%d) %s %s\n",
175 myopcode, offset, bit, opname, (on)?"on":"off");
177 bitmap[offset] |= 1 << bit;
179 bitmap[offset] &= ~(1 << bit);
181 else if (SvPOK(bitspec) && SvCUR(bitspec) == (STRLEN)opset_len) {
184 const char * const specbits = SvPV(bitspec, len);
185 if (opcode_debug >= 2)
186 warn("set_opset_bits opset %s %s\n", opname, (on)?"on":"off");
188 while(len-- > 0) bitmap[len] |= specbits[len];
190 while(len-- > 0) bitmap[len] &= ~specbits[len];
193 croak("panic: invalid bitspec for \"%s\" (type %u)",
194 opname, (unsigned)SvTYPE(bitspec));
199 opmask_add(pTHX_ SV *opset) /* THE ONLY FUNCTION TO EDIT PL_op_mask ITSELF */
207 verify_opset(aTHX_ opset,1); /* croaks on bad opset */
209 if (!PL_op_mask) /* caller must ensure PL_op_mask exists */
210 croak("Can't add to uninitialised PL_op_mask");
212 /* OPCODES ALREADY MASKED ARE NEVER UNMASKED. See opmask_addlocal() */
214 bitmask = SvPV(opset, len);
215 for (i=0; i < opset_len; i++) {
216 const U16 bits = bitmask[i];
217 if (!bits) { /* optimise for sparse masks */
221 for (j=0; j < 8 && myopcode < PL_maxo; )
222 PL_op_mask[myopcode++] |= bits & (1 << j++);
227 opmask_addlocal(pTHX_ SV *opset, char *op_mask_buf) /* Localise PL_op_mask then opmask_add() */
229 char *orig_op_mask = PL_op_mask;
232 SAVEVPTR(PL_op_mask);
233 /* XXX casting to an ordinary function ptr from a member function ptr
234 * is disallowed by Borland
236 if (opcode_debug >= 2)
237 SAVEDESTRUCTOR((void(*)(void*))Perl_warn,"PL_op_mask restored");
238 PL_op_mask = &op_mask_buf[0];
240 Copy(orig_op_mask, PL_op_mask, PL_maxo, char);
242 Zero(PL_op_mask, PL_maxo, char);
243 opmask_add(aTHX_ opset);
248 MODULE = Opcode PACKAGE = Opcode
255 assert(PL_maxo < OP_MASK_BUF_SIZE);
256 opset_len = (PL_maxo + 7) / 8;
257 if (opcode_debug >= 1)
258 warn("opset_len %ld\n", (long)opset_len);
263 _safe_pkg_prep(Package)
270 hv = gv_stashsv(Package, GV_ADDWARN); /* should exist already */
272 hvname = HvNAME_get(hv);
273 if (!hvname || strNE(hvname, "main")) {
274 /* make it think it's in main:: */
275 hv_name_set(hv, "main", 4, 0);
276 (void) hv_store(hv,"_",1,(SV *)PL_defgv,0); /* connect _ to global */
277 SvREFCNT_inc((SV *)PL_defgv); /* want to keep _ around! */
286 _safe_call_sv(Package, mask, codesv)
291 char op_mask_buf[OP_MASK_BUF_SIZE];
297 opmask_addlocal(aTHX_ mask, op_mask_buf);
299 save_aptr(&PL_endav);
300 PL_endav = (AV*)sv_2mortal((SV*)newAV()); /* ignore END blocks for now */
302 save_hptr(&PL_defstash); /* save current default stash */
303 /* the assignment to global defstash changes our sense of 'main' */
304 PL_defstash = gv_stashsv(Package, GV_ADDWARN); /* should exist already */
306 SAVEGENERICSV(PL_curstash);
307 PL_curstash = (HV *)SvREFCNT_inc_simple(PL_defstash);
309 /* defstash must itself contain a main:: so we'll add that now */
310 /* take care with the ref counts (was cause of long standing bug) */
311 /* XXX I'm still not sure if this is right, GV_ADDWARN should warn! */
312 gv = gv_fetchpvs("main::", GV_ADDWARN, SVt_PVHV);
313 sv_free((SV*)GvHV(gv));
314 GvHV(gv) = (HV*)SvREFCNT_inc(PL_defstash);
316 /* %INC must be clean for use/require in compartment */
317 dummy_hv = save_hash(PL_incgv);
318 GvHV(PL_incgv) = (HV*)SvREFCNT_inc(GvHV(gv_HVadd(gv_fetchpvs("INC",GV_ADD,SVt_PVHV))));
320 /* Invalidate class and method caches */
322 hv_clear(PL_stashcache);
325 /* use caller’s context */
326 perl_call_sv(codesv, GIMME_V|G_EVAL|G_KEEPERR);
327 sv_free( (SV *) dummy_hv); /* get rid of what save_hash gave us*/
328 SPAGAIN; /* for the PUTBACK added by xsubpp */
331 /* Invalidate again */
333 hv_clear(PL_stashcache);
337 verify_opset(opset, fatal = 0)
341 RETVAL = verify_opset(aTHX_ opset,fatal);
352 STRLEN len = opset_len;
354 opset = sv_2mortal(new_opset(aTHX_ opset)); /* verify and clone opset */
355 bitmap = SvPVX(opset);
357 bitmap[len] = ~bitmap[len];
358 /* take care of extra bits beyond PL_maxo in last byte */
360 bitmap[opset_len-1] &= ~(0xFF << (PL_maxo & 0x07));
366 opset_to_ops(opset, desc = 0)
373 const char * const bitmap = SvPV(opset, len);
374 char **names = (desc) ? get_op_descs() : get_op_names();
377 verify_opset(aTHX_ opset,1);
378 for (myopcode=0, i=0; i < opset_len; i++) {
379 const U16 bits = bitmap[i];
380 for (j=0; j < 8 && myopcode < PL_maxo; j++, myopcode++) {
381 if ( bits & (1 << j) )
382 XPUSHs(newSVpvn_flags(names[myopcode], strlen(names[myopcode]),
396 SV * const opset = sv_2mortal(new_opset(aTHX_ Nullsv));
397 char * const bitmap = SvPVX(opset);
398 for (i = 0; i < items; i++) {
401 if (verify_opset(aTHX_ ST(i),0)) {
406 opname = SvPV(ST(i), len);
407 if (*opname == '!') { on=0; ++opname;--len; }
408 bitspec = get_op_bitspec(aTHX_ opname, len, 1);
410 set_opset_bits(aTHX_ bitmap, bitspec, on, opname);
415 #define PERMITING (ix == 0 || ix == 1)
416 #define ONLY_THESE (ix == 0 || ix == 2)
419 permit_only(safe, ...)
432 if (!SvROK(safe) || !SvOBJECT(SvRV(safe)) || SvTYPE(SvRV(safe))!=SVt_PVHV)
433 croak("Not a Safe object");
434 mask = *hv_fetch((HV*)SvRV(safe), "Mask",4, 1);
435 if (ONLY_THESE) /* *_only = new mask, else edit current */
436 sv_setsv(mask, sv_2mortal(new_opset(aTHX_ PERMITING ? opset_all : Nullsv)));
438 verify_opset(aTHX_ mask,1); /* croaks */
439 bitmap = SvPVX(mask);
440 for (i = 1; i < items; i++) {
442 int on = PERMITING ? 0 : 1; /* deny = mask bit on */
443 if (verify_opset(aTHX_ ST(i),0)) { /* it's a valid mask */
447 else { /* it's an opname/optag */
448 opname = SvPV(ST(i), len);
449 /* invert if op has ! prefix (only one allowed) */
450 if (*opname == '!') { on = !on; ++opname; --len; }
451 bitspec = get_op_bitspec(aTHX_ opname, len, 1); /* croaks */
453 set_opset_bits(aTHX_ bitmap, bitspec, on, opname);
465 char **op_desc = get_op_descs();
468 /* copy args to a scratch area since we may push output values onto */
469 /* the stack faster than we read values off it if masks are used. */
470 args = (SV**)SvPVX(newSVpvn_flags((char*)&ST(0), items*sizeof(SV*), SVs_TEMP));
471 for (i = 0; i < items; i++) {
472 const char * const opname = SvPV(args[i], len);
473 SV *bitspec = get_op_bitspec(aTHX_ opname, len, 1);
474 if (SvIOK(bitspec)) {
475 const int myopcode = SvIV(bitspec);
476 if (myopcode < 0 || myopcode >= PL_maxo)
477 croak("panic: opcode %d (%s) out of range",myopcode,opname);
478 XPUSHs(newSVpvn_flags(op_desc[myopcode], strlen(op_desc[myopcode]),
481 else if (SvPOK(bitspec) && SvCUR(bitspec) == (STRLEN)opset_len) {
483 const char * const bitmap = SvPV_nolen_const(bitspec);
485 for (b=0; b < opset_len; b++) {
486 const U16 bits = bitmap[b];
487 for (j=0; j < 8 && myopcode < PL_maxo; j++, myopcode++)
489 XPUSHs(newSVpvn_flags(op_desc[myopcode],
490 strlen(op_desc[myopcode]),
495 croak("panic: invalid bitspec for \"%s\" (type %u)",
496 opname, (unsigned)SvTYPE(bitspec));
501 define_optag(optagsv, mask)
506 const char *optag = SvPV(optagsv, len);
508 put_op_bitspec(aTHX_ optag, len, mask); /* croaks */
515 ST(0) = sv_2mortal(new_opset(aTHX_ Nullsv));
521 ST(0) = sv_2mortal(new_opset(aTHX_ opset_all));
528 Newxz(PL_op_mask, PL_maxo, char);
530 opmask_add(aTHX_ opset);
535 if (GIMME_V == G_ARRAY) {
536 croak("opcodes in list context not yet implemented"); /* XXX */
539 XPUSHs(sv_2mortal(newSViv(PL_maxo)));
545 ST(0) = sv_2mortal(new_opset(aTHX_ Nullsv));
547 char * const bitmap = SvPVX(ST(0));
549 for(myopcode=0; myopcode < PL_maxo; ++myopcode) {
550 if (PL_op_mask[myopcode])
551 bitmap[myopcode >> 3] |= 1 << (myopcode & 0x07);