This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
(Retracted by #11223.)
[perl5.git] / ext / Opcode / Opcode.xs
CommitLineData
c5be433b 1#define PERL_NO_GET_CONTEXT
6badd1a5
PP
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
PP
7#define OP_MASK_BUF_SIZE (MAXO + 100)
8
4d8e9581 9/* XXX op_named_bits and opset_all are never freed */
6badd1a5
PP
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
GS
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
PP
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
GS
26 * XXX leak-alert: data allocated here is never freed, call this
27 * at most once
6badd1a5
PP
28 */
29
30static void
cea2e8a9 31op_names_init(pTHX)
6badd1a5
PP
32{
33 int i;
34 STRLEN len;
31fb1209 35 char **op_names;
6badd1a5
PP
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
MB
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
PP
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
PP
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
PP
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
PP
67{
68 SV **svp;
cea2e8a9 69 verify_opset(aTHX_ mask,1);
6badd1a5
PP
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
PP
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
PP
111{
112 SV *opset;
113 if (old_opset) {
cea2e8a9 114 verify_opset(aTHX_ old_opset,1);
6badd1a5
PP
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
PP
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
PP
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
PP
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
PP
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
PP
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
PP
171 croak("panic: invalid bitspec for \"%s\" (type %u)",
172 opname, (unsigned)SvTYPE(bitspec));
6badd1a5
PP
173}
174
175
176static void
cea2e8a9 177opmask_add(pTHX_ SV *opset) /* THE ONLY FUNCTION TO EDIT PL_op_mask ITSELF */
6badd1a5
PP
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
NIS
186 if (!PL_op_mask) /* caller must ensure PL_op_mask exists */
187 croak("Can't add to uninitialised PL_op_mask");
6badd1a5
PP
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
NIS
198 for (j=0; j < 8 && myopcode < PL_maxo; )
199 PL_op_mask[myopcode++] |= bits & (1 << j++);
6badd1a5
PP
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
GS
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
PP
221}
222
223
224
225MODULE = Opcode PACKAGE = Opcode
226
227PROTOTYPES: ENABLE
228
229BOOT:
3280af22
NIS
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
8c7314f9
JH
236void
237_safe_pkg_prep(Package)
238 char * Package
239PPCODE:
240 HV *hv;
241 ENTER;
242
243 hv = gv_stashpv(Package, GV_ADDWARN); /* should exist already */
244
245 if (strNE(HvNAME(hv),"main")) {
246 Safefree(HvNAME(hv));
247 HvNAME(hv) = savepv("main"); /* make it think it's in main:: */
248 hv_store(hv,"_",1,(SV *)PL_defgv,0); /* connect _ to global */
249 SvREFCNT_inc((SV *)PL_defgv); /* want to keep _ around! */
250 }
251 LEAVE;
252
253
254
255
6badd1a5
PP
256
257void
9d8a25dc
DL
258_safe_call_sv(Package, mask, codesv)
259 char * Package
6badd1a5
PP
260 SV * mask
261 SV * codesv
4d8e9581 262PPCODE:
6badd1a5
PP
263 char op_mask_buf[OP_MASK_BUF_SIZE];
264 GV *gv;
265
266 ENTER;
267
cea2e8a9 268 opmask_addlocal(aTHX_ mask, op_mask_buf);
6badd1a5 269
3280af22
NIS
270 save_aptr(&PL_endav);
271 PL_endav = (AV*)sv_2mortal((SV*)newAV()); /* ignore END blocks for now */
6badd1a5 272
e5125a24 273 save_hptr(&PL_defstash); /* save current default stash */
6badd1a5 274 /* the assignment to global defstash changes our sense of 'main' */
3280af22 275 PL_defstash = gv_stashpv(Package, GV_ADDWARN); /* should exist already */
8c7314f9 276
ed094faf
GS
277 save_hptr(&PL_curstash);
278 PL_curstash = PL_defstash;
6badd1a5
PP
279
280 /* defstash must itself contain a main:: so we'll add that now */
281 /* take care with the ref counts (was cause of long standing bug) */
282 /* XXX I'm still not sure if this is right, GV_ADDWARN should warn! */
283 gv = gv_fetchpv("main::", GV_ADDWARN, SVt_PVHV);
284 sv_free((SV*)GvHV(gv));
3280af22 285 GvHV(gv) = (HV*)SvREFCNT_inc(PL_defstash);
6badd1a5 286
e5125a24
GS
287 /* %INC must be clean for use/require in compartment */
288 save_hash(PL_incgv);
7ec3aa30 289 sv_free((SV*)GvHV(PL_incgv)); /* get rid of what save_hash gave us*/
e5125a24
GS
290 GvHV(PL_incgv) = (HV*)SvREFCNT_inc(GvHV(gv_HVadd(gv_fetchpv("INC",TRUE,SVt_PVHV))));
291
924508f0 292 PUSHMARK(SP);
246699ca 293 CvDEFSTASH(SvRV(codesv)) = PL_defstash;
6badd1a5
PP
294 perl_call_sv(codesv, GIMME|G_EVAL|G_KEEPERR); /* use callers context */
295 SPAGAIN; /* for the PUTBACK added by xsubpp */
296 LEAVE;
297
298
299int
300verify_opset(opset, fatal = 0)
301 SV *opset
302 int fatal
cea2e8a9
GS
303CODE:
304 RETVAL = verify_opset(aTHX_ opset,fatal);
305OUTPUT:
306 RETVAL
6badd1a5
PP
307
308void
309invert_opset(opset)
310 SV *opset
4d8e9581 311CODE:
6badd1a5
PP
312 {
313 char *bitmap;
314 STRLEN len = opset_len;
cea2e8a9 315 opset = sv_2mortal(new_opset(aTHX_ opset)); /* verify and clone opset */
6badd1a5
PP
316 bitmap = SvPVX(opset);
317 while(len-- > 0)
318 bitmap[len] = ~bitmap[len];
3280af22
NIS
319 /* take care of extra bits beyond PL_maxo in last byte */
320 if (PL_maxo & 07)
321 bitmap[opset_len-1] &= ~(0xFF << (PL_maxo & 0x07));
6badd1a5
PP
322 }
323 ST(0) = opset;
324
325
326void
327opset_to_ops(opset, desc = 0)
328 SV *opset
329 int desc
4d8e9581 330PPCODE:
6badd1a5
PP
331 {
332 STRLEN len;
333 int i, j, myopcode;
334 char *bitmap = SvPV(opset, len);
31fb1209 335 char **names = (desc) ? get_op_descs() : get_op_names();
cea2e8a9 336 verify_opset(aTHX_ opset,1);
6badd1a5
PP
337 for (myopcode=0, i=0; i < opset_len; i++) {
338 U16 bits = bitmap[i];
3280af22 339 for (j=0; j < 8 && myopcode < PL_maxo; j++, myopcode++) {
6badd1a5
PP
340 if ( bits & (1 << j) )
341 XPUSHs(sv_2mortal(newSVpv(names[myopcode], 0)));
342 }
343 }
344 }
345
346
347void
348opset(...)
4d8e9581 349CODE:
8063af02 350 int i;
6badd1a5
PP
351 SV *bitspec, *opset;
352 char *bitmap;
353 STRLEN len, on;
cea2e8a9 354 opset = sv_2mortal(new_opset(aTHX_ Nullsv));
6badd1a5
PP
355 bitmap = SvPVX(opset);
356 for (i = 0; i < items; i++) {
357 char *opname;
358 on = 1;
cea2e8a9 359 if (verify_opset(aTHX_ ST(i),0)) {
6badd1a5
PP
360 opname = "(opset)";
361 bitspec = ST(i);
362 }
363 else {
364 opname = SvPV(ST(i), len);
365 if (*opname == '!') { on=0; ++opname;--len; }
cea2e8a9 366 bitspec = get_op_bitspec(aTHX_ opname, len, 1);
6badd1a5 367 }
cea2e8a9 368 set_opset_bits(aTHX_ bitmap, bitspec, on, opname);
6badd1a5
PP
369 }
370 ST(0) = opset;
371
372
373#define PERMITING (ix == 0 || ix == 1)
374#define ONLY_THESE (ix == 0 || ix == 2)
375
376void
377permit_only(safe, ...)
378 SV *safe
4d8e9581 379ALIAS:
6badd1a5
PP
380 permit = 1
381 deny_only = 2
382 deny = 3
4d8e9581 383CODE:
6badd1a5
PP
384 int i, on;
385 SV *bitspec, *mask;
386 char *bitmap, *opname;
387 STRLEN len;
388
389 if (!SvROK(safe) || !SvOBJECT(SvRV(safe)) || SvTYPE(SvRV(safe))!=SVt_PVHV)
390 croak("Not a Safe object");
391 mask = *hv_fetch((HV*)SvRV(safe), "Mask",4, 1);
392 if (ONLY_THESE) /* *_only = new mask, else edit current */
cea2e8a9 393 sv_setsv(mask, sv_2mortal(new_opset(aTHX_ PERMITING ? opset_all : Nullsv)));
4d8e9581 394 else
cea2e8a9 395 verify_opset(aTHX_ mask,1); /* croaks */
6badd1a5
PP
396 bitmap = SvPVX(mask);
397 for (i = 1; i < items; i++) {
398 on = PERMITING ? 0 : 1; /* deny = mask bit on */
cea2e8a9 399 if (verify_opset(aTHX_ ST(i),0)) { /* it's a valid mask */
6badd1a5
PP
400 opname = "(opset)";
401 bitspec = ST(i);
402 }
403 else { /* it's an opname/optag */
404 opname = SvPV(ST(i), len);
405 /* invert if op has ! prefix (only one allowed) */
406 if (*opname == '!') { on = !on; ++opname; --len; }
cea2e8a9 407 bitspec = get_op_bitspec(aTHX_ opname, len, 1); /* croaks */
6badd1a5 408 }
cea2e8a9 409 set_opset_bits(aTHX_ bitmap, bitspec, on, opname);
6badd1a5 410 }
6b88bc9c 411 ST(0) = &PL_sv_yes;
6badd1a5
PP
412
413
414
415void
416opdesc(...)
4d8e9581 417PPCODE:
6badd1a5
PP
418 int i, myopcode;
419 STRLEN len;
420 SV **args;
31fb1209 421 char **op_desc = get_op_descs();
6badd1a5
PP
422 /* copy args to a scratch area since we may push output values onto */
423 /* the stack faster than we read values off it if masks are used. */
79cb57f6 424 args = (SV**)SvPVX(sv_2mortal(newSVpvn((char*)&ST(0), items*sizeof(SV*))));
6badd1a5
PP
425 for (i = 0; i < items; i++) {
426 char *opname = SvPV(args[i], len);
cea2e8a9 427 SV *bitspec = get_op_bitspec(aTHX_ opname, len, 1);
6badd1a5
PP
428 if (SvIOK(bitspec)) {
429 myopcode = SvIV(bitspec);
3280af22 430 if (myopcode < 0 || myopcode >= PL_maxo)
6badd1a5
PP
431 croak("panic: opcode %d (%s) out of range",myopcode,opname);
432 XPUSHs(sv_2mortal(newSVpv(op_desc[myopcode], 0)));
433 }
434 else if (SvPOK(bitspec) && SvCUR(bitspec) == opset_len) {
435 int b, j;
2d8e6c8d
GS
436 STRLEN n_a;
437 char *bitmap = SvPV(bitspec,n_a);
6badd1a5
PP
438 myopcode = 0;
439 for (b=0; b < opset_len; b++) {
440 U16 bits = bitmap[b];
3280af22 441 for (j=0; j < 8 && myopcode < PL_maxo; j++, myopcode++)
6badd1a5
PP
442 if (bits & (1 << j))
443 XPUSHs(sv_2mortal(newSVpv(op_desc[myopcode], 0)));
444 }
445 }
446 else
ff0cee69
PP
447 croak("panic: invalid bitspec for \"%s\" (type %u)",
448 opname, (unsigned)SvTYPE(bitspec));
6badd1a5
PP
449 }
450
451
452void
453define_optag(optagsv, mask)
454 SV *optagsv
455 SV *mask
4d8e9581 456CODE:
6badd1a5
PP
457 STRLEN len;
458 char *optag = SvPV(optagsv, len);
cea2e8a9 459 put_op_bitspec(aTHX_ optag, len, mask); /* croaks */
6b88bc9c 460 ST(0) = &PL_sv_yes;
6badd1a5
PP
461
462
463void
464empty_opset()
4d8e9581 465CODE:
cea2e8a9 466 ST(0) = sv_2mortal(new_opset(aTHX_ Nullsv));
6badd1a5
PP
467
468void
469full_opset()
4d8e9581 470CODE:
cea2e8a9 471 ST(0) = sv_2mortal(new_opset(aTHX_ opset_all));
6badd1a5
PP
472
473void
474opmask_add(opset)
475 SV *opset
4d8e9581 476PREINIT:
3280af22
NIS
477 if (!PL_op_mask)
478 Newz(0, PL_op_mask, PL_maxo, char);
cea2e8a9
GS
479CODE:
480 opmask_add(aTHX_ opset);
6badd1a5
PP
481
482void
483opcodes()
4d8e9581 484PPCODE:
6badd1a5
PP
485 if (GIMME == G_ARRAY) {
486 croak("opcodes in list context not yet implemented"); /* XXX */
487 }
488 else {
3280af22 489 XPUSHs(sv_2mortal(newSViv(PL_maxo)));
6badd1a5
PP
490 }
491
492void
493opmask()
4d8e9581 494CODE:
cea2e8a9 495 ST(0) = sv_2mortal(new_opset(aTHX_ Nullsv));
3280af22 496 if (PL_op_mask) {
6badd1a5
PP
497 char *bitmap = SvPVX(ST(0));
498 int myopcode;
3280af22
NIS
499 for(myopcode=0; myopcode < PL_maxo; ++myopcode) {
500 if (PL_op_mask[myopcode])
6badd1a5
PP
501 bitmap[myopcode >> 3] |= 1 << (myopcode & 0x07);
502 }
503 }
504