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