[asperl] added AS patch#5 (patch #4 was intentionally skipped after
[p5sagit/p5-mst-13.2.git] / ext / Opcode / Opcode.xs
CommitLineData
6badd1a5 1#include "EXTERN.h"
2#include "perl.h"
3#include "XSUB.h"
4
5/* maxo shouldn't differ from MAXO but leave room anyway (see BOOT:) */
6#define OP_MASK_BUF_SIZE (MAXO + 100)
7
8static HV *op_named_bits; /* cache shared for whole process */
9static SV *opset_all; /* mask with all bits set */
10static IV opset_len; /* length of opmasks in bytes */
11static int opcode_debug = 0;
12
13static SV *new_opset _((SV *old_opset));
14static int verify_opset _((SV *opset, int fatal));
15static void set_opset_bits _((char *bitmap, SV *bitspec, int on, char *opname));
16static void put_op_bitspec _((char *optag, STRLEN len, SV *opset));
17static SV *get_op_bitspec _((char *opname, STRLEN len, int fatal));
18
19
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__
24 */
25
26static void
f0f333f4 27op_names_init(void)
6badd1a5 28{
29 int i;
30 STRLEN len;
31fb1209 31 char **op_names;
6badd1a5 32 char *bitmap;
33
34 op_named_bits = newHV();
31fb1209 35 op_names = get_op_names();
6badd1a5 36 for(i=0; i < maxo; ++i) {
e858de61 37 SV *sv;
38 sv = newSViv(i);
39 SvREADONLY_on(sv);
31fb1209 40 hv_store(op_named_bits, op_names[i], strlen(op_names[i]), sv, 0);
6badd1a5 41 }
42
43 put_op_bitspec(":none",0, sv_2mortal(new_opset(Nullsv)));
44
45 opset_all = new_opset(Nullsv);
46 bitmap = SvPV(opset_all, len);
47 i = len-1; /* deal with last byte specially, see below */
48 while(i-- > 0)
49 bitmap[i] = 0xFF;
50 /* Take care to set the right number of bits in the last byte */
8903cb82 51 bitmap[len-1] = (maxo & 0x07) ? ~(0xFF << (maxo & 0x07)) : 0xFF;
6badd1a5 52 put_op_bitspec(":all",0, opset_all); /* don't mortalise */
53}
54
55
56/* Store a new tag definition. Always a mask.
57 * The tag must not already be defined.
58 * SV *mask is copied not referenced.
59 */
60
61static void
f0f333f4 62put_op_bitspec(char *optag, STRLEN len, SV *mask)
6badd1a5 63{
64 SV **svp;
65 verify_opset(mask,1);
66 if (!len)
67 len = strlen(optag);
68 svp = hv_fetch(op_named_bits, optag, len, 1);
69 if (SvOK(*svp))
70 croak("Opcode tag \"%s\" already defined", optag);
71 sv_setsv(*svp, mask);
72 SvREADONLY_on(*svp);
73}
74
75
76
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.
80 */
81
82static SV *
f0f333f4 83get_op_bitspec(char *opname, STRLEN len, int fatal)
6badd1a5 84{
85 SV **svp;
86 if (!len)
87 len = strlen(opname);
88 svp = hv_fetch(op_named_bits, opname, len, 0);
89 if (!svp || !SvOK(*svp)) {
90 if (!fatal)
91 return Nullsv;
92 if (*opname == ':')
93 croak("Unknown operator tag \"%s\"", opname);
94 if (*opname == '!') /* XXX here later, or elsewhere? */
95 croak("Can't negate operators here (\"%s\")", opname);
96 if (isALPHA(*opname))
97 croak("Unknown operator name \"%s\"", opname);
98 croak("Unknown operator prefix \"%s\"", opname);
99 }
100 return *svp;
101}
102
103
104
105static SV *
f0f333f4 106new_opset(SV *old_opset)
6badd1a5 107{
108 SV *opset;
109 if (old_opset) {
110 verify_opset(old_opset,1);
111 opset = newSVsv(old_opset);
112 }
113 else {
8c52afec 114 opset = NEWSV(1156, opset_len);
67a5ea69 115 Zero(SvPVX(opset), opset_len + 1, char);
6badd1a5 116 SvCUR_set(opset, opset_len);
117 (void)SvPOK_only(opset);
118 }
119 /* not mortalised here */
120 return opset;
121}
122
123
124static int
f0f333f4 125verify_opset(SV *opset, int fatal)
6badd1a5 126{
127 char *err = Nullch;
128 if (!SvOK(opset)) err = "undefined";
129 else if (!SvPOK(opset)) err = "wrong type";
130 else if (SvCUR(opset) != opset_len) err = "wrong size";
131 if (err && fatal) {
132 croak("Invalid opset: %s", err);
133 }
134 return !err;
135}
136
137
138static void
f0f333f4 139set_opset_bits(char *bitmap, SV *bitspec, int on, char *opname)
6badd1a5 140{
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)
ff0cee69 148 warn("set_opset_bits bit %2d (off=%d, bit=%d) %s %s\n",
6badd1a5 149 myopcode, offset, bit, opname, (on)?"on":"off");
150 if (on)
151 bitmap[offset] |= 1 << bit;
152 else
153 bitmap[offset] &= ~(1 << bit);
154 }
155 else if (SvPOK(bitspec) && SvCUR(bitspec) == opset_len) {
156
157 STRLEN 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");
161 if (on)
162 while(len-- > 0) bitmap[len] |= specbits[len];
163 else
164 while(len-- > 0) bitmap[len] &= ~specbits[len];
165 }
166 else
ff0cee69 167 croak("panic: invalid bitspec for \"%s\" (type %u)",
168 opname, (unsigned)SvTYPE(bitspec));
6badd1a5 169}
170
171
172static void
f0f333f4 173opmask_add(SV *opset) /* THE ONLY FUNCTION TO EDIT op_mask ITSELF */
6badd1a5 174{
175 int i,j;
176 char *bitmask;
177 STRLEN len;
178 int myopcode = 0;
179
180 verify_opset(opset,1); /* croaks on bad opset */
181
182 if (!op_mask) /* caller must ensure op_mask exists */
183 croak("Can't add to uninitialised op_mask");
184
185 /* OPCODES ALREADY MASKED ARE NEVER UNMASKED. See opmask_addlocal() */
186
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 */
191 myopcode += 8;
192 continue;
193 }
194 for (j=0; j < 8 && myopcode < maxo; )
195 op_mask[myopcode++] |= bits & (1 << j++);
196 }
197}
198
199static void
f0f333f4 200opmask_addlocal(SV *opset, char *op_mask_buf) /* Localise op_mask then opmask_add() */
6badd1a5 201{
202 char *orig_op_mask = op_mask;
203 SAVEPPTR(op_mask);
204 if (opcode_debug >= 2)
205 SAVEDESTRUCTOR((void(*)_((void*)))warn,"op_mask restored");
206 op_mask = &op_mask_buf[0];
207 if (orig_op_mask)
208 Copy(orig_op_mask, op_mask, maxo, char);
209 else
210 Zero(op_mask, maxo, char);
211 opmask_add(opset);
212}
213
214
215
216MODULE = Opcode PACKAGE = Opcode
217
218PROTOTYPES: ENABLE
219
220BOOT:
221 assert(maxo < OP_MASK_BUF_SIZE);
760ac839 222 opset_len = (maxo + 7) / 8;
6badd1a5 223 if (opcode_debug >= 1)
ff0cee69 224 warn("opset_len %ld\n", (long)opset_len);
6badd1a5 225 op_names_init();
226
227
228void
229_safe_call_sv(package, mask, codesv)
230 char * package
231 SV * mask
232 SV * codesv
233 PPCODE:
234 char op_mask_buf[OP_MASK_BUF_SIZE];
235 GV *gv;
236
237 ENTER;
238
239 opmask_addlocal(mask, op_mask_buf);
240
241 save_aptr(&endav);
242 endav = (AV*)sv_2mortal((SV*)newAV()); /* ignore END blocks for now */
243
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 */
247
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);
254
255 PUSHMARK(sp);
256 perl_call_sv(codesv, GIMME|G_EVAL|G_KEEPERR); /* use callers context */
257 SPAGAIN; /* for the PUTBACK added by xsubpp */
258 LEAVE;
259
260
261int
262verify_opset(opset, fatal = 0)
263 SV *opset
264 int fatal
265
266
267void
268invert_opset(opset)
269 SV *opset
270 CODE:
271 {
272 char *bitmap;
273 STRLEN len = opset_len;
274 opset = new_opset(opset); /* verify and clone opset */
275 bitmap = SvPVX(opset);
276 while(len-- > 0)
277 bitmap[len] = ~bitmap[len];
278 /* take care of extra bits beyond maxo in last byte */
8903cb82 279 if (maxo & 07)
280 bitmap[opset_len-1] &= ~(0xFF << (maxo & 0x07));
6badd1a5 281 }
282 ST(0) = opset;
283
284
285void
286opset_to_ops(opset, desc = 0)
287 SV *opset
288 int desc
289 PPCODE:
290 {
291 STRLEN len;
292 int i, j, myopcode;
293 char *bitmap = SvPV(opset, len);
31fb1209 294 char **names = (desc) ? get_op_descs() : get_op_names();
6badd1a5 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)));
301 }
302 }
303 }
304
305
306void
307opset(...)
308 CODE:
309 int i, j;
310 SV *bitspec, *opset;
311 char *bitmap;
312 STRLEN len, on;
313 opset = new_opset(Nullsv);
314 bitmap = SvPVX(opset);
315 for (i = 0; i < items; i++) {
316 char *opname;
317 on = 1;
318 if (verify_opset(ST(i),0)) {
319 opname = "(opset)";
320 bitspec = ST(i);
321 }
322 else {
323 opname = SvPV(ST(i), len);
324 if (*opname == '!') { on=0; ++opname;--len; }
325 bitspec = get_op_bitspec(opname, len, 1);
326 }
327 set_opset_bits(bitmap, bitspec, on, opname);
328 }
329 ST(0) = opset;
330
331
332#define PERMITING (ix == 0 || ix == 1)
333#define ONLY_THESE (ix == 0 || ix == 2)
334
335void
336permit_only(safe, ...)
337 SV *safe
338 ALIAS:
339 permit = 1
340 deny_only = 2
341 deny = 3
342 CODE:
343 int i, on;
344 SV *bitspec, *mask;
345 char *bitmap, *opname;
346 STRLEN len;
347
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 */
358 opname = "(opset)";
359 bitspec = ST(i);
360 }
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 */
366 }
367 set_opset_bits(bitmap, bitspec, on, opname);
368 }
369 ST(0) = &sv_yes;
370
371
372
373void
374opdesc(...)
375 PPCODE:
376 int i, myopcode;
377 STRLEN len;
378 SV **args;
31fb1209 379 char **op_desc = get_op_descs();
6badd1a5 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)));
391 }
392 else if (SvPOK(bitspec) && SvCUR(bitspec) == opset_len) {
393 int b, j;
394 char *bitmap = SvPV(bitspec,na);
395 myopcode = 0;
396 for (b=0; b < opset_len; b++) {
397 U16 bits = bitmap[b];
398 for (j=0; j < 8 && myopcode < maxo; j++, myopcode++)
399 if (bits & (1 << j))
400 XPUSHs(sv_2mortal(newSVpv(op_desc[myopcode], 0)));
401 }
402 }
403 else
ff0cee69 404 croak("panic: invalid bitspec for \"%s\" (type %u)",
405 opname, (unsigned)SvTYPE(bitspec));
6badd1a5 406 }
407
408
409void
410define_optag(optagsv, mask)
411 SV *optagsv
412 SV *mask
413 CODE:
414 STRLEN len;
415 char *optag = SvPV(optagsv, len);
416 put_op_bitspec(optag, len, mask); /* croaks */
417 ST(0) = &sv_yes;
418
419
420void
421empty_opset()
422 CODE:
423 ST(0) = sv_2mortal(new_opset(Nullsv));
424
425void
426full_opset()
427 CODE:
428 ST(0) = sv_2mortal(new_opset(opset_all));
429
430void
431opmask_add(opset)
432 SV *opset
433 PREINIT:
434 if (!op_mask)
435 Newz(0, op_mask, maxo, char);
436
437void
438opcodes()
439 PPCODE:
440 if (GIMME == G_ARRAY) {
441 croak("opcodes in list context not yet implemented"); /* XXX */
442 }
443 else {
444 XPUSHs(sv_2mortal(newSViv(maxo)));
445 }
446
447void
448opmask()
449 CODE:
450 ST(0) = sv_2mortal(new_opset(Nullsv));
451 if (op_mask) {
452 char *bitmap = SvPVX(ST(0));
453 int myopcode;
454 for(myopcode=0; myopcode < maxo; ++myopcode) {
455 if (op_mask[myopcode])
456 bitmap[myopcode >> 3] |= 1 << (myopcode & 0x07);
457 }
458 }
459