This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add socklen_t probe; Configure maintenance.
[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
PP
235
236
237void
9d8a25dc
DL
238_safe_call_sv(Package, mask, codesv)
239 char * Package
6badd1a5
PP
240 SV * mask
241 SV * codesv
4d8e9581 242PPCODE:
6badd1a5
PP
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
NIS
250 save_aptr(&PL_endav);
251 PL_endav = (AV*)sv_2mortal((SV*)newAV()); /* ignore END blocks for now */
6badd1a5 252
3280af22 253 save_hptr(&PL_defstash); /* save current default stack */
6badd1a5 254 /* the assignment to global defstash changes our sense of 'main' */
3280af22 255 PL_defstash = gv_stashpv(Package, GV_ADDWARN); /* should exist already */
ed094faf
GS
256 save_hptr(&PL_curstash);
257 PL_curstash = PL_defstash;
6badd1a5
PP
258
259 /* defstash must itself contain a main:: so we'll add that now */
260 /* take care with the ref counts (was cause of long standing bug) */
261 /* XXX I'm still not sure if this is right, GV_ADDWARN should warn! */
262 gv = gv_fetchpv("main::", GV_ADDWARN, SVt_PVHV);
263 sv_free((SV*)GvHV(gv));
3280af22 264 GvHV(gv) = (HV*)SvREFCNT_inc(PL_defstash);
6badd1a5 265
924508f0 266 PUSHMARK(SP);
6badd1a5
PP
267 perl_call_sv(codesv, GIMME|G_EVAL|G_KEEPERR); /* use callers context */
268 SPAGAIN; /* for the PUTBACK added by xsubpp */
269 LEAVE;
270
271
272int
273verify_opset(opset, fatal = 0)
274 SV *opset
275 int fatal
cea2e8a9
GS
276CODE:
277 RETVAL = verify_opset(aTHX_ opset,fatal);
278OUTPUT:
279 RETVAL
6badd1a5
PP
280
281void
282invert_opset(opset)
283 SV *opset
4d8e9581 284CODE:
6badd1a5
PP
285 {
286 char *bitmap;
287 STRLEN len = opset_len;
cea2e8a9 288 opset = sv_2mortal(new_opset(aTHX_ opset)); /* verify and clone opset */
6badd1a5
PP
289 bitmap = SvPVX(opset);
290 while(len-- > 0)
291 bitmap[len] = ~bitmap[len];
3280af22
NIS
292 /* take care of extra bits beyond PL_maxo in last byte */
293 if (PL_maxo & 07)
294 bitmap[opset_len-1] &= ~(0xFF << (PL_maxo & 0x07));
6badd1a5
PP
295 }
296 ST(0) = opset;
297
298
299void
300opset_to_ops(opset, desc = 0)
301 SV *opset
302 int desc
4d8e9581 303PPCODE:
6badd1a5
PP
304 {
305 STRLEN len;
306 int i, j, myopcode;
307 char *bitmap = SvPV(opset, len);
31fb1209 308 char **names = (desc) ? get_op_descs() : get_op_names();
cea2e8a9 309 verify_opset(aTHX_ opset,1);
6badd1a5
PP
310 for (myopcode=0, i=0; i < opset_len; i++) {
311 U16 bits = bitmap[i];
3280af22 312 for (j=0; j < 8 && myopcode < PL_maxo; j++, myopcode++) {
6badd1a5
PP
313 if ( bits & (1 << j) )
314 XPUSHs(sv_2mortal(newSVpv(names[myopcode], 0)));
315 }
316 }
317 }
318
319
320void
321opset(...)
4d8e9581 322CODE:
6badd1a5
PP
323 int i, j;
324 SV *bitspec, *opset;
325 char *bitmap;
326 STRLEN len, on;
cea2e8a9 327 opset = sv_2mortal(new_opset(aTHX_ Nullsv));
6badd1a5
PP
328 bitmap = SvPVX(opset);
329 for (i = 0; i < items; i++) {
330 char *opname;
331 on = 1;
cea2e8a9 332 if (verify_opset(aTHX_ ST(i),0)) {
6badd1a5
PP
333 opname = "(opset)";
334 bitspec = ST(i);
335 }
336 else {
337 opname = SvPV(ST(i), len);
338 if (*opname == '!') { on=0; ++opname;--len; }
cea2e8a9 339 bitspec = get_op_bitspec(aTHX_ opname, len, 1);
6badd1a5 340 }
cea2e8a9 341 set_opset_bits(aTHX_ bitmap, bitspec, on, opname);
6badd1a5
PP
342 }
343 ST(0) = opset;
344
345
346#define PERMITING (ix == 0 || ix == 1)
347#define ONLY_THESE (ix == 0 || ix == 2)
348
349void
350permit_only(safe, ...)
351 SV *safe
4d8e9581 352ALIAS:
6badd1a5
PP
353 permit = 1
354 deny_only = 2
355 deny = 3
4d8e9581 356CODE:
6badd1a5
PP
357 int i, on;
358 SV *bitspec, *mask;
359 char *bitmap, *opname;
360 STRLEN len;
361
362 if (!SvROK(safe) || !SvOBJECT(SvRV(safe)) || SvTYPE(SvRV(safe))!=SVt_PVHV)
363 croak("Not a Safe object");
364 mask = *hv_fetch((HV*)SvRV(safe), "Mask",4, 1);
365 if (ONLY_THESE) /* *_only = new mask, else edit current */
cea2e8a9 366 sv_setsv(mask, sv_2mortal(new_opset(aTHX_ PERMITING ? opset_all : Nullsv)));
4d8e9581 367 else
cea2e8a9 368 verify_opset(aTHX_ mask,1); /* croaks */
6badd1a5
PP
369 bitmap = SvPVX(mask);
370 for (i = 1; i < items; i++) {
371 on = PERMITING ? 0 : 1; /* deny = mask bit on */
cea2e8a9 372 if (verify_opset(aTHX_ ST(i),0)) { /* it's a valid mask */
6badd1a5
PP
373 opname = "(opset)";
374 bitspec = ST(i);
375 }
376 else { /* it's an opname/optag */
377 opname = SvPV(ST(i), len);
378 /* invert if op has ! prefix (only one allowed) */
379 if (*opname == '!') { on = !on; ++opname; --len; }
cea2e8a9 380 bitspec = get_op_bitspec(aTHX_ opname, len, 1); /* croaks */
6badd1a5 381 }
cea2e8a9 382 set_opset_bits(aTHX_ bitmap, bitspec, on, opname);
6badd1a5 383 }
6b88bc9c 384 ST(0) = &PL_sv_yes;
6badd1a5
PP
385
386
387
388void
389opdesc(...)
4d8e9581 390PPCODE:
6badd1a5
PP
391 int i, myopcode;
392 STRLEN len;
393 SV **args;
31fb1209 394 char **op_desc = get_op_descs();
6badd1a5
PP
395 /* copy args to a scratch area since we may push output values onto */
396 /* the stack faster than we read values off it if masks are used. */
79cb57f6 397 args = (SV**)SvPVX(sv_2mortal(newSVpvn((char*)&ST(0), items*sizeof(SV*))));
6badd1a5
PP
398 for (i = 0; i < items; i++) {
399 char *opname = SvPV(args[i], len);
cea2e8a9 400 SV *bitspec = get_op_bitspec(aTHX_ opname, len, 1);
6badd1a5
PP
401 if (SvIOK(bitspec)) {
402 myopcode = SvIV(bitspec);
3280af22 403 if (myopcode < 0 || myopcode >= PL_maxo)
6badd1a5
PP
404 croak("panic: opcode %d (%s) out of range",myopcode,opname);
405 XPUSHs(sv_2mortal(newSVpv(op_desc[myopcode], 0)));
406 }
407 else if (SvPOK(bitspec) && SvCUR(bitspec) == opset_len) {
408 int b, j;
2d8e6c8d
GS
409 STRLEN n_a;
410 char *bitmap = SvPV(bitspec,n_a);
6badd1a5
PP
411 myopcode = 0;
412 for (b=0; b < opset_len; b++) {
413 U16 bits = bitmap[b];
3280af22 414 for (j=0; j < 8 && myopcode < PL_maxo; j++, myopcode++)
6badd1a5
PP
415 if (bits & (1 << j))
416 XPUSHs(sv_2mortal(newSVpv(op_desc[myopcode], 0)));
417 }
418 }
419 else
ff0cee69
PP
420 croak("panic: invalid bitspec for \"%s\" (type %u)",
421 opname, (unsigned)SvTYPE(bitspec));
6badd1a5
PP
422 }
423
424
425void
426define_optag(optagsv, mask)
427 SV *optagsv
428 SV *mask
4d8e9581 429CODE:
6badd1a5
PP
430 STRLEN len;
431 char *optag = SvPV(optagsv, len);
cea2e8a9 432 put_op_bitspec(aTHX_ optag, len, mask); /* croaks */
6b88bc9c 433 ST(0) = &PL_sv_yes;
6badd1a5
PP
434
435
436void
437empty_opset()
4d8e9581 438CODE:
cea2e8a9 439 ST(0) = sv_2mortal(new_opset(aTHX_ Nullsv));
6badd1a5
PP
440
441void
442full_opset()
4d8e9581 443CODE:
cea2e8a9 444 ST(0) = sv_2mortal(new_opset(aTHX_ opset_all));
6badd1a5
PP
445
446void
447opmask_add(opset)
448 SV *opset
4d8e9581 449PREINIT:
3280af22
NIS
450 if (!PL_op_mask)
451 Newz(0, PL_op_mask, PL_maxo, char);
cea2e8a9
GS
452CODE:
453 opmask_add(aTHX_ opset);
6badd1a5
PP
454
455void
456opcodes()
4d8e9581 457PPCODE:
6badd1a5
PP
458 if (GIMME == G_ARRAY) {
459 croak("opcodes in list context not yet implemented"); /* XXX */
460 }
461 else {
3280af22 462 XPUSHs(sv_2mortal(newSViv(PL_maxo)));
6badd1a5
PP
463 }
464
465void
466opmask()
4d8e9581 467CODE:
cea2e8a9 468 ST(0) = sv_2mortal(new_opset(aTHX_ Nullsv));
3280af22 469 if (PL_op_mask) {
6badd1a5
PP
470 char *bitmap = SvPVX(ST(0));
471 int myopcode;
3280af22
NIS
472 for(myopcode=0; myopcode < PL_maxo; ++myopcode) {
473 if (PL_op_mask[myopcode])
6badd1a5
PP
474 bitmap[myopcode >> 3] |= 1 << (myopcode & 0x07);
475 }
476 }
477