Commit | Line | Data |
6badd1a5 |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
3 | #include "XSUB.h" |
4 | |
3280af22 |
5 | /* PL_maxo shouldn't differ from MAXO but leave room anyway (see BOOT:) */ |
6badd1a5 |
6 | #define OP_MASK_BUF_SIZE (MAXO + 100) |
7 | |
4d8e9581 |
8 | /* XXX op_named_bits and opset_all are never freed */ |
6badd1a5 |
9 | static HV *op_named_bits; /* cache shared for whole process */ |
10 | static SV *opset_all; /* mask with all bits set */ |
11 | static IV opset_len; /* length of opmasks in bytes */ |
12 | static int opcode_debug = 0; |
13 | |
cea2e8a9 |
14 | static SV *new_opset (pTHX_ SV *old_opset); |
15 | static int verify_opset (pTHX_ SV *opset, int fatal); |
16 | static void set_opset_bits (pTHX_ char *bitmap, SV *bitspec, int on, char *opname); |
17 | static void put_op_bitspec (pTHX_ char *optag, STRLEN len, SV *opset); |
18 | static SV *get_op_bitspec (pTHX_ char *opname, STRLEN len, int fatal); |
6badd1a5 |
19 | |
20 | |
21 | /* Initialise our private op_named_bits HV. |
22 | * It is first loaded with the name and number of each perl operator. |
23 | * Then the builtin tags :none and :all are added. |
24 | * Opcode.pm loads the standard optags from __DATA__ |
4d8e9581 |
25 | * XXX leak-alert: data allocated here is never freed, call this |
26 | * at most once |
6badd1a5 |
27 | */ |
28 | |
29 | static void |
cea2e8a9 |
30 | op_names_init(pTHX) |
6badd1a5 |
31 | { |
32 | int i; |
33 | STRLEN len; |
31fb1209 |
34 | char **op_names; |
6badd1a5 |
35 | char *bitmap; |
36 | |
37 | op_named_bits = newHV(); |
31fb1209 |
38 | op_names = get_op_names(); |
3280af22 |
39 | for(i=0; i < PL_maxo; ++i) { |
e858de61 |
40 | SV *sv; |
41 | sv = newSViv(i); |
42 | SvREADONLY_on(sv); |
31fb1209 |
43 | hv_store(op_named_bits, op_names[i], strlen(op_names[i]), sv, 0); |
6badd1a5 |
44 | } |
45 | |
cea2e8a9 |
46 | put_op_bitspec(aTHX_ ":none",0, sv_2mortal(new_opset(aTHX_ Nullsv))); |
6badd1a5 |
47 | |
cea2e8a9 |
48 | opset_all = new_opset(aTHX_ Nullsv); |
6badd1a5 |
49 | bitmap = SvPV(opset_all, len); |
50 | i = len-1; /* deal with last byte specially, see below */ |
51 | while(i-- > 0) |
52 | bitmap[i] = 0xFF; |
53 | /* Take care to set the right number of bits in the last byte */ |
3280af22 |
54 | bitmap[len-1] = (PL_maxo & 0x07) ? ~(0xFF << (PL_maxo & 0x07)) : 0xFF; |
cea2e8a9 |
55 | put_op_bitspec(aTHX_ ":all",0, opset_all); /* don't mortalise */ |
6badd1a5 |
56 | } |
57 | |
58 | |
59 | /* Store a new tag definition. Always a mask. |
60 | * The tag must not already be defined. |
61 | * SV *mask is copied not referenced. |
62 | */ |
63 | |
64 | static void |
cea2e8a9 |
65 | put_op_bitspec(pTHX_ char *optag, STRLEN len, SV *mask) |
6badd1a5 |
66 | { |
67 | SV **svp; |
cea2e8a9 |
68 | verify_opset(aTHX_ mask,1); |
6badd1a5 |
69 | if (!len) |
70 | len = strlen(optag); |
71 | svp = hv_fetch(op_named_bits, optag, len, 1); |
72 | if (SvOK(*svp)) |
73 | croak("Opcode tag \"%s\" already defined", optag); |
74 | sv_setsv(*svp, mask); |
75 | SvREADONLY_on(*svp); |
76 | } |
77 | |
78 | |
79 | |
80 | /* Fetch a 'bits' entry for an opname or optag (IV/PV). |
81 | * Note that we return the actual entry for speed. |
82 | * Always sv_mortalcopy() if returing it to user code. |
83 | */ |
84 | |
85 | static SV * |
cea2e8a9 |
86 | get_op_bitspec(pTHX_ char *opname, STRLEN len, int fatal) |
6badd1a5 |
87 | { |
88 | SV **svp; |
89 | if (!len) |
90 | len = strlen(opname); |
91 | svp = hv_fetch(op_named_bits, opname, len, 0); |
92 | if (!svp || !SvOK(*svp)) { |
93 | if (!fatal) |
94 | return Nullsv; |
95 | if (*opname == ':') |
96 | croak("Unknown operator tag \"%s\"", opname); |
97 | if (*opname == '!') /* XXX here later, or elsewhere? */ |
98 | croak("Can't negate operators here (\"%s\")", opname); |
99 | if (isALPHA(*opname)) |
100 | croak("Unknown operator name \"%s\"", opname); |
101 | croak("Unknown operator prefix \"%s\"", opname); |
102 | } |
103 | return *svp; |
104 | } |
105 | |
106 | |
107 | |
108 | static SV * |
cea2e8a9 |
109 | new_opset(pTHX_ SV *old_opset) |
6badd1a5 |
110 | { |
111 | SV *opset; |
112 | if (old_opset) { |
cea2e8a9 |
113 | verify_opset(aTHX_ old_opset,1); |
6badd1a5 |
114 | opset = newSVsv(old_opset); |
115 | } |
116 | else { |
8c52afec |
117 | opset = NEWSV(1156, opset_len); |
67a5ea69 |
118 | Zero(SvPVX(opset), opset_len + 1, char); |
6badd1a5 |
119 | SvCUR_set(opset, opset_len); |
120 | (void)SvPOK_only(opset); |
121 | } |
122 | /* not mortalised here */ |
123 | return opset; |
124 | } |
125 | |
126 | |
127 | static int |
cea2e8a9 |
128 | verify_opset(pTHX_ SV *opset, int fatal) |
6badd1a5 |
129 | { |
130 | char *err = Nullch; |
131 | if (!SvOK(opset)) err = "undefined"; |
132 | else if (!SvPOK(opset)) err = "wrong type"; |
133 | else if (SvCUR(opset) != opset_len) err = "wrong size"; |
134 | if (err && fatal) { |
135 | croak("Invalid opset: %s", err); |
136 | } |
137 | return !err; |
138 | } |
139 | |
140 | |
141 | static void |
cea2e8a9 |
142 | set_opset_bits(pTHX_ char *bitmap, SV *bitspec, int on, char *opname) |
6badd1a5 |
143 | { |
144 | if (SvIOK(bitspec)) { |
145 | int myopcode = SvIV(bitspec); |
146 | int offset = myopcode >> 3; |
147 | int bit = myopcode & 0x07; |
3280af22 |
148 | if (myopcode >= PL_maxo || myopcode < 0) |
6badd1a5 |
149 | croak("panic: opcode \"%s\" value %d is invalid", opname, myopcode); |
150 | if (opcode_debug >= 2) |
ff0cee69 |
151 | warn("set_opset_bits bit %2d (off=%d, bit=%d) %s %s\n", |
6badd1a5 |
152 | myopcode, offset, bit, opname, (on)?"on":"off"); |
153 | if (on) |
154 | bitmap[offset] |= 1 << bit; |
155 | else |
156 | bitmap[offset] &= ~(1 << bit); |
157 | } |
158 | else if (SvPOK(bitspec) && SvCUR(bitspec) == opset_len) { |
159 | |
160 | STRLEN len; |
161 | char *specbits = SvPV(bitspec, len); |
162 | if (opcode_debug >= 2) |
163 | warn("set_opset_bits opset %s %s\n", opname, (on)?"on":"off"); |
164 | if (on) |
165 | while(len-- > 0) bitmap[len] |= specbits[len]; |
166 | else |
167 | while(len-- > 0) bitmap[len] &= ~specbits[len]; |
168 | } |
169 | else |
ff0cee69 |
170 | croak("panic: invalid bitspec for \"%s\" (type %u)", |
171 | opname, (unsigned)SvTYPE(bitspec)); |
6badd1a5 |
172 | } |
173 | |
174 | |
175 | static void |
cea2e8a9 |
176 | opmask_add(pTHX_ SV *opset) /* THE ONLY FUNCTION TO EDIT PL_op_mask ITSELF */ |
6badd1a5 |
177 | { |
178 | int i,j; |
179 | char *bitmask; |
180 | STRLEN len; |
181 | int myopcode = 0; |
182 | |
cea2e8a9 |
183 | verify_opset(aTHX_ opset,1); /* croaks on bad opset */ |
6badd1a5 |
184 | |
3280af22 |
185 | if (!PL_op_mask) /* caller must ensure PL_op_mask exists */ |
186 | croak("Can't add to uninitialised PL_op_mask"); |
6badd1a5 |
187 | |
188 | /* OPCODES ALREADY MASKED ARE NEVER UNMASKED. See opmask_addlocal() */ |
189 | |
190 | bitmask = SvPV(opset, len); |
191 | for (i=0; i < opset_len; i++) { |
192 | U16 bits = bitmask[i]; |
193 | if (!bits) { /* optimise for sparse masks */ |
194 | myopcode += 8; |
195 | continue; |
196 | } |
3280af22 |
197 | for (j=0; j < 8 && myopcode < PL_maxo; ) |
198 | PL_op_mask[myopcode++] |= bits & (1 << j++); |
6badd1a5 |
199 | } |
200 | } |
201 | |
202 | static void |
cea2e8a9 |
203 | opmask_addlocal(pTHX_ SV *opset, char *op_mask_buf) /* Localise PL_op_mask then opmask_add() */ |
6badd1a5 |
204 | { |
3280af22 |
205 | char *orig_op_mask = PL_op_mask; |
206 | SAVEPPTR(PL_op_mask); |
db15561c |
207 | #if !defined(PERL_OBJECT) |
ac4c12e7 |
208 | /* XXX casting to an ordinary function ptr from a member function ptr |
209 | * is disallowed by Borland |
210 | */ |
6badd1a5 |
211 | if (opcode_debug >= 2) |
20ce7b12 |
212 | SAVEDESTRUCTOR((void(CPERLscope(*))(void*))warn,"PL_op_mask restored"); |
ac4c12e7 |
213 | #endif |
3280af22 |
214 | PL_op_mask = &op_mask_buf[0]; |
6badd1a5 |
215 | if (orig_op_mask) |
3280af22 |
216 | Copy(orig_op_mask, PL_op_mask, PL_maxo, char); |
6badd1a5 |
217 | else |
3280af22 |
218 | Zero(PL_op_mask, PL_maxo, char); |
cea2e8a9 |
219 | opmask_add(aTHX_ opset); |
6badd1a5 |
220 | } |
221 | |
222 | |
223 | |
224 | MODULE = Opcode PACKAGE = Opcode |
225 | |
226 | PROTOTYPES: ENABLE |
227 | |
228 | BOOT: |
3280af22 |
229 | assert(PL_maxo < OP_MASK_BUF_SIZE); |
230 | opset_len = (PL_maxo + 7) / 8; |
6badd1a5 |
231 | if (opcode_debug >= 1) |
ff0cee69 |
232 | warn("opset_len %ld\n", (long)opset_len); |
cea2e8a9 |
233 | op_names_init(aTHX); |
6badd1a5 |
234 | |
235 | |
236 | void |
9d8a25dc |
237 | _safe_call_sv(Package, mask, codesv) |
238 | char * Package |
6badd1a5 |
239 | SV * mask |
240 | SV * codesv |
4d8e9581 |
241 | PPCODE: |
6badd1a5 |
242 | char op_mask_buf[OP_MASK_BUF_SIZE]; |
243 | GV *gv; |
244 | |
245 | ENTER; |
246 | |
cea2e8a9 |
247 | opmask_addlocal(aTHX_ mask, op_mask_buf); |
6badd1a5 |
248 | |
3280af22 |
249 | save_aptr(&PL_endav); |
250 | PL_endav = (AV*)sv_2mortal((SV*)newAV()); /* ignore END blocks for now */ |
6badd1a5 |
251 | |
3280af22 |
252 | save_hptr(&PL_defstash); /* save current default stack */ |
6badd1a5 |
253 | /* the assignment to global defstash changes our sense of 'main' */ |
3280af22 |
254 | PL_defstash = gv_stashpv(Package, GV_ADDWARN); /* should exist already */ |
6badd1a5 |
255 | |
256 | /* defstash must itself contain a main:: so we'll add that now */ |
257 | /* take care with the ref counts (was cause of long standing bug) */ |
258 | /* XXX I'm still not sure if this is right, GV_ADDWARN should warn! */ |
259 | gv = gv_fetchpv("main::", GV_ADDWARN, SVt_PVHV); |
260 | sv_free((SV*)GvHV(gv)); |
3280af22 |
261 | GvHV(gv) = (HV*)SvREFCNT_inc(PL_defstash); |
6badd1a5 |
262 | |
924508f0 |
263 | PUSHMARK(SP); |
6badd1a5 |
264 | perl_call_sv(codesv, GIMME|G_EVAL|G_KEEPERR); /* use callers context */ |
265 | SPAGAIN; /* for the PUTBACK added by xsubpp */ |
266 | LEAVE; |
267 | |
268 | |
269 | int |
270 | verify_opset(opset, fatal = 0) |
271 | SV *opset |
272 | int fatal |
cea2e8a9 |
273 | CODE: |
274 | RETVAL = verify_opset(aTHX_ opset,fatal); |
275 | OUTPUT: |
276 | RETVAL |
6badd1a5 |
277 | |
278 | void |
279 | invert_opset(opset) |
280 | SV *opset |
4d8e9581 |
281 | CODE: |
6badd1a5 |
282 | { |
283 | char *bitmap; |
284 | STRLEN len = opset_len; |
cea2e8a9 |
285 | opset = sv_2mortal(new_opset(aTHX_ opset)); /* verify and clone opset */ |
6badd1a5 |
286 | bitmap = SvPVX(opset); |
287 | while(len-- > 0) |
288 | bitmap[len] = ~bitmap[len]; |
3280af22 |
289 | /* take care of extra bits beyond PL_maxo in last byte */ |
290 | if (PL_maxo & 07) |
291 | bitmap[opset_len-1] &= ~(0xFF << (PL_maxo & 0x07)); |
6badd1a5 |
292 | } |
293 | ST(0) = opset; |
294 | |
295 | |
296 | void |
297 | opset_to_ops(opset, desc = 0) |
298 | SV *opset |
299 | int desc |
4d8e9581 |
300 | PPCODE: |
6badd1a5 |
301 | { |
302 | STRLEN len; |
303 | int i, j, myopcode; |
304 | char *bitmap = SvPV(opset, len); |
31fb1209 |
305 | char **names = (desc) ? get_op_descs() : get_op_names(); |
cea2e8a9 |
306 | verify_opset(aTHX_ opset,1); |
6badd1a5 |
307 | for (myopcode=0, i=0; i < opset_len; i++) { |
308 | U16 bits = bitmap[i]; |
3280af22 |
309 | for (j=0; j < 8 && myopcode < PL_maxo; j++, myopcode++) { |
6badd1a5 |
310 | if ( bits & (1 << j) ) |
311 | XPUSHs(sv_2mortal(newSVpv(names[myopcode], 0))); |
312 | } |
313 | } |
314 | } |
315 | |
316 | |
317 | void |
318 | opset(...) |
4d8e9581 |
319 | CODE: |
6badd1a5 |
320 | int i, j; |
321 | SV *bitspec, *opset; |
322 | char *bitmap; |
323 | STRLEN len, on; |
cea2e8a9 |
324 | opset = sv_2mortal(new_opset(aTHX_ Nullsv)); |
6badd1a5 |
325 | bitmap = SvPVX(opset); |
326 | for (i = 0; i < items; i++) { |
327 | char *opname; |
328 | on = 1; |
cea2e8a9 |
329 | if (verify_opset(aTHX_ ST(i),0)) { |
6badd1a5 |
330 | opname = "(opset)"; |
331 | bitspec = ST(i); |
332 | } |
333 | else { |
334 | opname = SvPV(ST(i), len); |
335 | if (*opname == '!') { on=0; ++opname;--len; } |
cea2e8a9 |
336 | bitspec = get_op_bitspec(aTHX_ opname, len, 1); |
6badd1a5 |
337 | } |
cea2e8a9 |
338 | set_opset_bits(aTHX_ bitmap, bitspec, on, opname); |
6badd1a5 |
339 | } |
340 | ST(0) = opset; |
341 | |
342 | |
343 | #define PERMITING (ix == 0 || ix == 1) |
344 | #define ONLY_THESE (ix == 0 || ix == 2) |
345 | |
346 | void |
347 | permit_only(safe, ...) |
348 | SV *safe |
4d8e9581 |
349 | ALIAS: |
6badd1a5 |
350 | permit = 1 |
351 | deny_only = 2 |
352 | deny = 3 |
4d8e9581 |
353 | CODE: |
6badd1a5 |
354 | int i, on; |
355 | SV *bitspec, *mask; |
356 | char *bitmap, *opname; |
357 | STRLEN len; |
358 | |
359 | if (!SvROK(safe) || !SvOBJECT(SvRV(safe)) || SvTYPE(SvRV(safe))!=SVt_PVHV) |
360 | croak("Not a Safe object"); |
361 | mask = *hv_fetch((HV*)SvRV(safe), "Mask",4, 1); |
362 | if (ONLY_THESE) /* *_only = new mask, else edit current */ |
cea2e8a9 |
363 | sv_setsv(mask, sv_2mortal(new_opset(aTHX_ PERMITING ? opset_all : Nullsv))); |
4d8e9581 |
364 | else |
cea2e8a9 |
365 | verify_opset(aTHX_ mask,1); /* croaks */ |
6badd1a5 |
366 | bitmap = SvPVX(mask); |
367 | for (i = 1; i < items; i++) { |
368 | on = PERMITING ? 0 : 1; /* deny = mask bit on */ |
cea2e8a9 |
369 | if (verify_opset(aTHX_ ST(i),0)) { /* it's a valid mask */ |
6badd1a5 |
370 | opname = "(opset)"; |
371 | bitspec = ST(i); |
372 | } |
373 | else { /* it's an opname/optag */ |
374 | opname = SvPV(ST(i), len); |
375 | /* invert if op has ! prefix (only one allowed) */ |
376 | if (*opname == '!') { on = !on; ++opname; --len; } |
cea2e8a9 |
377 | bitspec = get_op_bitspec(aTHX_ opname, len, 1); /* croaks */ |
6badd1a5 |
378 | } |
cea2e8a9 |
379 | set_opset_bits(aTHX_ bitmap, bitspec, on, opname); |
6badd1a5 |
380 | } |
6b88bc9c |
381 | ST(0) = &PL_sv_yes; |
6badd1a5 |
382 | |
383 | |
384 | |
385 | void |
386 | opdesc(...) |
4d8e9581 |
387 | PPCODE: |
6badd1a5 |
388 | int i, myopcode; |
389 | STRLEN len; |
390 | SV **args; |
31fb1209 |
391 | char **op_desc = get_op_descs(); |
6badd1a5 |
392 | /* copy args to a scratch area since we may push output values onto */ |
393 | /* the stack faster than we read values off it if masks are used. */ |
79cb57f6 |
394 | args = (SV**)SvPVX(sv_2mortal(newSVpvn((char*)&ST(0), items*sizeof(SV*)))); |
6badd1a5 |
395 | for (i = 0; i < items; i++) { |
396 | char *opname = SvPV(args[i], len); |
cea2e8a9 |
397 | SV *bitspec = get_op_bitspec(aTHX_ opname, len, 1); |
6badd1a5 |
398 | if (SvIOK(bitspec)) { |
399 | myopcode = SvIV(bitspec); |
3280af22 |
400 | if (myopcode < 0 || myopcode >= PL_maxo) |
6badd1a5 |
401 | croak("panic: opcode %d (%s) out of range",myopcode,opname); |
402 | XPUSHs(sv_2mortal(newSVpv(op_desc[myopcode], 0))); |
403 | } |
404 | else if (SvPOK(bitspec) && SvCUR(bitspec) == opset_len) { |
405 | int b, j; |
2d8e6c8d |
406 | STRLEN n_a; |
407 | char *bitmap = SvPV(bitspec,n_a); |
6badd1a5 |
408 | myopcode = 0; |
409 | for (b=0; b < opset_len; b++) { |
410 | U16 bits = bitmap[b]; |
3280af22 |
411 | for (j=0; j < 8 && myopcode < PL_maxo; j++, myopcode++) |
6badd1a5 |
412 | if (bits & (1 << j)) |
413 | XPUSHs(sv_2mortal(newSVpv(op_desc[myopcode], 0))); |
414 | } |
415 | } |
416 | else |
ff0cee69 |
417 | croak("panic: invalid bitspec for \"%s\" (type %u)", |
418 | opname, (unsigned)SvTYPE(bitspec)); |
6badd1a5 |
419 | } |
420 | |
421 | |
422 | void |
423 | define_optag(optagsv, mask) |
424 | SV *optagsv |
425 | SV *mask |
4d8e9581 |
426 | CODE: |
6badd1a5 |
427 | STRLEN len; |
428 | char *optag = SvPV(optagsv, len); |
cea2e8a9 |
429 | put_op_bitspec(aTHX_ optag, len, mask); /* croaks */ |
6b88bc9c |
430 | ST(0) = &PL_sv_yes; |
6badd1a5 |
431 | |
432 | |
433 | void |
434 | empty_opset() |
4d8e9581 |
435 | CODE: |
cea2e8a9 |
436 | ST(0) = sv_2mortal(new_opset(aTHX_ Nullsv)); |
6badd1a5 |
437 | |
438 | void |
439 | full_opset() |
4d8e9581 |
440 | CODE: |
cea2e8a9 |
441 | ST(0) = sv_2mortal(new_opset(aTHX_ opset_all)); |
6badd1a5 |
442 | |
443 | void |
444 | opmask_add(opset) |
445 | SV *opset |
4d8e9581 |
446 | PREINIT: |
3280af22 |
447 | if (!PL_op_mask) |
448 | Newz(0, PL_op_mask, PL_maxo, char); |
cea2e8a9 |
449 | CODE: |
450 | opmask_add(aTHX_ opset); |
6badd1a5 |
451 | |
452 | void |
453 | opcodes() |
4d8e9581 |
454 | PPCODE: |
6badd1a5 |
455 | if (GIMME == G_ARRAY) { |
456 | croak("opcodes in list context not yet implemented"); /* XXX */ |
457 | } |
458 | else { |
3280af22 |
459 | XPUSHs(sv_2mortal(newSViv(PL_maxo))); |
6badd1a5 |
460 | } |
461 | |
462 | void |
463 | opmask() |
4d8e9581 |
464 | CODE: |
cea2e8a9 |
465 | ST(0) = sv_2mortal(new_opset(aTHX_ Nullsv)); |
3280af22 |
466 | if (PL_op_mask) { |
6badd1a5 |
467 | char *bitmap = SvPVX(ST(0)); |
468 | int myopcode; |
3280af22 |
469 | for(myopcode=0; myopcode < PL_maxo; ++myopcode) { |
470 | if (PL_op_mask[myopcode]) |
6badd1a5 |
471 | bitmap[myopcode >> 3] |= 1 << (myopcode & 0x07); |
472 | } |
473 | } |
474 | |