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