--- a/src/lvm.c +++ b/src/lvm.c @@ -31,6 +31,9 @@ /* limit for table tag-method chains (to avoid loops) */ #define MAXTAGLOOP 100 +#ifdef __GNUC__ +#define COMPUTED_GOTO 1 +#endif /* * If 'obj' is a string, it is tried to be interpreted as a number. @@ -566,12 +569,63 @@ static inline int arith_mode( const TVal ARITH_OP1_END #endif +#ifdef COMPUTED_GOTO +#define OPCODE_TARGET(op) DO_OP_##op: +#define CALL_OPCODE(op) goto *opcodes[op]; +#define OPCODE_PTR(op) [OP_##op] = &&DO_OP_##op +#else +#define OPCODE_TARGET(op) case OP_##op: +#define CALL_OPCODE(op) switch (op) +#endif + void luaV_execute (lua_State *L, int nexeccalls) { LClosure *cl; StkId base; TValue *k; const Instruction *pc; +#ifdef COMPUTED_GOTO + static const void *opcodes[] = { + OPCODE_PTR(MOVE), + OPCODE_PTR(LOADK), + OPCODE_PTR(LOADBOOL), + OPCODE_PTR(LOADNIL), + OPCODE_PTR(GETUPVAL), + OPCODE_PTR(GETGLOBAL), + OPCODE_PTR(GETTABLE), + OPCODE_PTR(SETGLOBAL), + OPCODE_PTR(SETUPVAL), + OPCODE_PTR(SETTABLE), + OPCODE_PTR(NEWTABLE), + OPCODE_PTR(SELF), + OPCODE_PTR(ADD), + OPCODE_PTR(SUB), + OPCODE_PTR(MUL), + OPCODE_PTR(DIV), + OPCODE_PTR(MOD), + OPCODE_PTR(POW), + OPCODE_PTR(UNM), + OPCODE_PTR(NOT), + OPCODE_PTR(LEN), + OPCODE_PTR(CONCAT), + OPCODE_PTR(JMP), + OPCODE_PTR(EQ), + OPCODE_PTR(LT), + OPCODE_PTR(LE), + OPCODE_PTR(TEST), + OPCODE_PTR(TESTSET), + OPCODE_PTR(CALL), + OPCODE_PTR(TAILCALL), + OPCODE_PTR(RETURN), + OPCODE_PTR(FORLOOP), + OPCODE_PTR(FORPREP), + OPCODE_PTR(TFORLOOP), + OPCODE_PTR(SETLIST), + OPCODE_PTR(CLOSE), + OPCODE_PTR(CLOSURE), + OPCODE_PTR(VARARG) + }; +#endif reentry: /* entry point */ lua_assert(isLua(L->ci)); pc = L->savedpc; @@ -596,33 +650,33 @@ void luaV_execute (lua_State *L, int nex lua_assert(base == L->base && L->base == L->ci->base); lua_assert(base <= L->top && L->top <= L->stack + L->stacksize); lua_assert(L->top == L->ci->top || luaG_checkopenop(i)); - switch (GET_OPCODE(i)) { - case OP_MOVE: { + CALL_OPCODE(GET_OPCODE(i)) { + OPCODE_TARGET(MOVE) { setobjs2s(L, ra, RB(i)); continue; } - case OP_LOADK: { + OPCODE_TARGET(LOADK) { setobj2s(L, ra, KBx(i)); continue; } - case OP_LOADBOOL: { + OPCODE_TARGET(LOADBOOL) { setbvalue(ra, GETARG_B(i)); if (GETARG_C(i)) pc++; /* skip next instruction (if C) */ continue; } - case OP_LOADNIL: { + OPCODE_TARGET(LOADNIL) { TValue *rb = RB(i); do { setnilvalue(rb--); } while (rb >= ra); continue; } - case OP_GETUPVAL: { + OPCODE_TARGET(GETUPVAL) { int b = GETARG_B(i); setobj2s(L, ra, cl->upvals[b]->v); continue; } - case OP_GETGLOBAL: { + OPCODE_TARGET(GETGLOBAL) { TValue g; TValue *rb = KBx(i); sethvalue(L, &g, cl->env); @@ -630,88 +684,88 @@ void luaV_execute (lua_State *L, int nex Protect(luaV_gettable(L, &g, rb, ra)); continue; } - case OP_GETTABLE: { + OPCODE_TARGET(GETTABLE) { Protect(luaV_gettable(L, RB(i), RKC(i), ra)); continue; } - case OP_SETGLOBAL: { + OPCODE_TARGET(SETGLOBAL) { TValue g; sethvalue(L, &g, cl->env); lua_assert(ttisstring(KBx(i))); Protect(luaV_settable(L, &g, KBx(i), ra)); continue; } - case OP_SETUPVAL: { + OPCODE_TARGET(SETUPVAL) { UpVal *uv = cl->upvals[GETARG_B(i)]; setobj(L, uv->v, ra); luaC_barrier(L, uv, ra); continue; } - case OP_SETTABLE: { + OPCODE_TARGET(SETTABLE) { Protect(luaV_settable(L, ra, RKB(i), RKC(i))); continue; } - case OP_NEWTABLE: { + OPCODE_TARGET(NEWTABLE) { int b = GETARG_B(i); int c = GETARG_C(i); sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c))); Protect(luaC_checkGC(L)); continue; } - case OP_SELF: { + OPCODE_TARGET(SELF) { StkId rb = RB(i); setobjs2s(L, ra+1, rb); Protect(luaV_gettable(L, rb, RKC(i), ra)); continue; } - case OP_ADD: { + OPCODE_TARGET(ADD) { TValue *rb = RKB(i), *rc= RKC(i); arith_op_continue( luai_numadd, try_addint, luai_vectadd ); Protect(Arith(L, ra, rb, rc, TM_ADD)); \ continue; } - case OP_SUB: { + OPCODE_TARGET(SUB) { TValue *rb = RKB(i), *rc= RKC(i); arith_op_continue( luai_numsub, try_subint, luai_vectsub ); Protect(Arith(L, ra, rb, rc, TM_SUB)); continue; } - case OP_MUL: { + OPCODE_TARGET(MUL) { TValue *rb = RKB(i), *rc= RKC(i); arith_op_continue(luai_nummul, try_mulint, luai_vectmul); Protect(Arith(L, ra, rb, rc, TM_MUL)); continue; } - case OP_DIV: { + OPCODE_TARGET(DIV) { TValue *rb = RKB(i), *rc= RKC(i); arith_op_continue(luai_numdiv, try_divint, luai_vectdiv); Protect(Arith(L, ra, rb, rc, TM_DIV)); continue; } - case OP_MOD: { + OPCODE_TARGET(MOD) { TValue *rb = RKB(i), *rc= RKC(i); arith_op_continue_scalar(luai_nummod, try_modint); /* scalars only */ Protect(Arith(L, ra, rb, rc, TM_MOD)); continue; } - case OP_POW: { + OPCODE_TARGET(POW) { TValue *rb = RKB(i), *rc= RKC(i); arith_op_continue(luai_numpow, try_powint, luai_vectpow); Protect(Arith(L, ra, rb, rc, TM_POW)); continue; } - case OP_UNM: { + OPCODE_TARGET(UNM) { TValue *rb = RB(i); arith_op1_continue(luai_numunm, try_unmint, luai_vectunm); Protect(Arith(L, ra, rb, rb, TM_UNM)); continue; } - case OP_NOT: { + OPCODE_TARGET(NOT) { int res = l_isfalse(RB(i)); /* next assignment may change this value */ setbvalue(ra, res); continue; } - case OP_LEN: { + OPCODE_TARGET(LEN) { const TValue *rb = RB(i); switch (ttype(rb)) { case LUA_TTABLE: { @@ -731,18 +785,18 @@ void luaV_execute (lua_State *L, int nex } continue; } - case OP_CONCAT: { + OPCODE_TARGET(CONCAT) { int b = GETARG_B(i); int c = GETARG_C(i); Protect(luaV_concat(L, c-b+1, c); luaC_checkGC(L)); setobjs2s(L, RA(i), base+b); continue; } - case OP_JMP: { + OPCODE_TARGET(JMP) { dojump(L, pc, GETARG_sBx(i)); continue; } - case OP_EQ: { + OPCODE_TARGET(EQ) { TValue *rb = RKB(i); TValue *rc = RKC(i); Protect( @@ -752,7 +806,7 @@ void luaV_execute (lua_State *L, int nex pc++; continue; } - case OP_LT: { + OPCODE_TARGET(LT) { Protect( if (luaV_lessthan(L, RKB(i), RKC(i)) == GETARG_A(i)) dojump(L, pc, GETARG_sBx(*pc)); @@ -760,7 +814,7 @@ void luaV_execute (lua_State *L, int nex pc++; continue; } - case OP_LE: { + OPCODE_TARGET(LE) { Protect( if (lessequal(L, RKB(i), RKC(i)) == GETARG_A(i)) dojump(L, pc, GETARG_sBx(*pc)); @@ -768,13 +822,13 @@ void luaV_execute (lua_State *L, int nex pc++; continue; } - case OP_TEST: { + OPCODE_TARGET(TEST) { if (l_isfalse(ra) != GETARG_C(i)) dojump(L, pc, GETARG_sBx(*pc)); pc++; continue; } - case OP_TESTSET: { + OPCODE_TARGET(TESTSET) { TValue *rb = RB(i); if (l_isfalse(rb) != GETARG_C(i)) { setobjs2s(L, ra, rb); @@ -783,7 +837,7 @@ void luaV_execute (lua_State *L, int nex pc++; continue; } - case OP_CALL: { + OPCODE_TARGET(CALL) { int b = GETARG_B(i); int nresults = GETARG_C(i) - 1; if (b != 0) L->top = ra+b; /* else previous instruction set top */ @@ -804,7 +858,7 @@ void luaV_execute (lua_State *L, int nex } } } - case OP_TAILCALL: { + OPCODE_TARGET(TAILCALL) { int b = GETARG_B(i); if (b != 0) L->top = ra+b; /* else previous instruction set top */ L->savedpc = pc; @@ -836,7 +890,7 @@ void luaV_execute (lua_State *L, int nex } } } - case OP_RETURN: { + OPCODE_TARGET(RETURN) { int b = GETARG_B(i); if (b != 0) L->top = ra+b-1; if (L->openupval) luaF_close(L, base); @@ -851,7 +905,7 @@ void luaV_execute (lua_State *L, int nex goto reentry; } } - case OP_FORLOOP: { + OPCODE_TARGET(FORLOOP) { /* If start,step and limit are all integers, we don't need to check * against overflow in the looping. */ @@ -879,7 +933,7 @@ void luaV_execute (lua_State *L, int nex } continue; } - case OP_FORPREP: { + OPCODE_TARGET(FORPREP) { const TValue *init = ra; const TValue *plimit = ra+1; const TValue *pstep = ra+2; @@ -902,7 +956,7 @@ void luaV_execute (lua_State *L, int nex dojump(L, pc, GETARG_sBx(i)); continue; } - case OP_TFORLOOP: { + OPCODE_TARGET(TFORLOOP) { StkId cb = ra + 3; /* call base */ setobjs2s(L, cb+2, ra+2); setobjs2s(L, cb+1, ra+1); @@ -918,7 +972,7 @@ void luaV_execute (lua_State *L, int nex pc++; continue; } - case OP_SETLIST: { + OPCODE_TARGET(SETLIST) { int n = GETARG_B(i); int c = GETARG_C(i); int last; @@ -940,11 +994,11 @@ void luaV_execute (lua_State *L, int nex } continue; } - case OP_CLOSE: { + OPCODE_TARGET(CLOSE) { luaF_close(L, ra); continue; } - case OP_CLOSURE: { + OPCODE_TARGET(CLOSURE) { Proto *p; Closure *ncl; int nup, j; @@ -964,7 +1018,7 @@ void luaV_execute (lua_State *L, int nex Protect(luaC_checkGC(L)); continue; } - case OP_VARARG: { + OPCODE_TARGET(VARARG) { int b = GETARG_B(i) - 1; int j; CallInfo *ci = L->ci; 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 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621