diff options
Diffstat (limited to '')
| -rw-r--r-- | lvm.c | 235 |
1 files changed, 51 insertions, 184 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lvm.c,v 1.40 1999/01/20 20:22:06 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 1.41 1999/01/25 17:39:28 roberto Exp roberto $ |
| 3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -31,9 +31,7 @@ | |||
| 31 | #endif | 31 | #endif |
| 32 | 32 | ||
| 33 | 33 | ||
| 34 | #define skip_word(pc) (pc+=2) | 34 | #define highbyte(x) ((x)<<8) |
| 35 | #define get_word(pc) ((*(pc)<<8)+(*((pc)+1))) | ||
| 36 | #define next_word(pc) (pc+=2, get_word(pc-2)) | ||
| 37 | 35 | ||
| 38 | 36 | ||
| 39 | /* Extra stack size to run a function: LUA_T_LINE(1), TM calls(2), ... */ | 37 | /* Extra stack size to run a function: LUA_T_LINE(1), TM calls(2), ... */ |
| @@ -315,10 +313,9 @@ static void adjust_varargs (StkId first_extra_arg) | |||
| 315 | ** [stack+base,top). Returns n such that the the results are between | 313 | ** [stack+base,top). Returns n such that the the results are between |
| 316 | ** [stack+n,top). | 314 | ** [stack+n,top). |
| 317 | */ | 315 | */ |
| 318 | StkId luaV_execute (Closure *cl, TProtoFunc *tf, StkId base) | 316 | StkId luaV_execute (Closure *cl, TProtoFunc *tf, StkId base) { |
| 319 | { | ||
| 320 | struct Stack *S = &L->stack; /* to optimize */ | 317 | struct Stack *S = &L->stack; /* to optimize */ |
| 321 | Byte *pc = tf->code; | 318 | register Byte *pc = tf->code; |
| 322 | TObject *consts = tf->consts; | 319 | TObject *consts = tf->consts; |
| 323 | if (lua_callhook) | 320 | if (lua_callhook) |
| 324 | luaD_callHook(base, tf, 0); | 321 | luaD_callHook(base, tf, 0); |
| @@ -330,54 +327,28 @@ StkId luaV_execute (Closure *cl, TProtoFunc *tf, StkId base) | |||
| 330 | adjust_varargs(base+(*pc++)-ZEROVARARG); | 327 | adjust_varargs(base+(*pc++)-ZEROVARARG); |
| 331 | } | 328 | } |
| 332 | for (;;) { | 329 | for (;;) { |
| 333 | int aux; | 330 | register int aux = 0; |
| 334 | switch ((OpCode)(aux = *pc++)) { | 331 | switch ((OpCode)*pc++) { |
| 335 | |||
| 336 | case PUSHNIL0: | ||
| 337 | ttype(S->top++) = LUA_T_NIL; | ||
| 338 | break; | ||
| 339 | 332 | ||
| 340 | case PUSHNIL: | 333 | case PUSHNIL: aux = *pc++; |
| 341 | aux = *pc++; | ||
| 342 | do { | 334 | do { |
| 343 | ttype(S->top++) = LUA_T_NIL; | 335 | ttype(S->top++) = LUA_T_NIL; |
| 344 | } while (aux--); | 336 | } while (aux--); |
| 345 | break; | 337 | break; |
| 346 | 338 | ||
| 347 | case PUSHNUMBER: | 339 | case PUSHNUMBERW: aux = highbyte(*pc++); |
| 348 | aux = *pc++; goto pushnumber; | 340 | case PUSHNUMBER: aux += *pc++; |
| 349 | |||
| 350 | case PUSHNUMBERW: | ||
| 351 | aux = next_word(pc); goto pushnumber; | ||
| 352 | |||
| 353 | case PUSHNUMBER0: case PUSHNUMBER1: case PUSHNUMBER2: | ||
| 354 | aux -= PUSHNUMBER0; | ||
| 355 | pushnumber: | ||
| 356 | ttype(S->top) = LUA_T_NUMBER; | 341 | ttype(S->top) = LUA_T_NUMBER; |
| 357 | nvalue(S->top) = aux; | 342 | nvalue(S->top) = aux-NUMOFFSET; |
| 358 | S->top++; | 343 | S->top++; |
| 359 | break; | 344 | break; |
| 360 | 345 | ||
| 361 | case PUSHLOCAL: | 346 | case PUSHLOCAL: aux = *pc++; |
| 362 | aux = *pc++; goto pushlocal; | ||
| 363 | |||
| 364 | case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2: case PUSHLOCAL3: | ||
| 365 | case PUSHLOCAL4: case PUSHLOCAL5: case PUSHLOCAL6: case PUSHLOCAL7: | ||
| 366 | aux -= PUSHLOCAL0; | ||
| 367 | pushlocal: | ||
| 368 | *S->top++ = *((S->stack+base) + aux); | 347 | *S->top++ = *((S->stack+base) + aux); |
| 369 | break; | 348 | break; |
| 370 | 349 | ||
| 371 | case GETGLOBALW: | 350 | case GETGLOBALW: aux = highbyte(*pc++); |
| 372 | aux = next_word(pc); goto getglobal; | 351 | case GETGLOBAL: aux += *pc++; |
| 373 | |||
| 374 | case GETGLOBAL: | ||
| 375 | aux = *pc++; goto getglobal; | ||
| 376 | |||
| 377 | case GETGLOBAL0: case GETGLOBAL1: case GETGLOBAL2: case GETGLOBAL3: | ||
| 378 | case GETGLOBAL4: case GETGLOBAL5: case GETGLOBAL6: case GETGLOBAL7: | ||
| 379 | aux -= GETGLOBAL0; | ||
| 380 | getglobal: | ||
| 381 | luaV_getglobal(tsvalue(&consts[aux])); | 352 | luaV_getglobal(tsvalue(&consts[aux])); |
| 382 | break; | 353 | break; |
| 383 | 354 | ||
| @@ -385,30 +356,14 @@ StkId luaV_execute (Closure *cl, TProtoFunc *tf, StkId base) | |||
| 385 | luaV_gettable(); | 356 | luaV_gettable(); |
| 386 | break; | 357 | break; |
| 387 | 358 | ||
| 388 | case GETDOTTEDW: | 359 | case GETDOTTEDW: aux = highbyte(*pc++); |
| 389 | aux = next_word(pc); goto getdotted; | 360 | case GETDOTTED: aux += *pc++; |
| 390 | |||
| 391 | case GETDOTTED: | ||
| 392 | aux = *pc++; goto getdotted; | ||
| 393 | |||
| 394 | case GETDOTTED0: case GETDOTTED1: case GETDOTTED2: case GETDOTTED3: | ||
| 395 | case GETDOTTED4: case GETDOTTED5: case GETDOTTED6: case GETDOTTED7: | ||
| 396 | aux -= GETDOTTED0; | ||
| 397 | getdotted: | ||
| 398 | *S->top++ = consts[aux]; | 361 | *S->top++ = consts[aux]; |
| 399 | luaV_gettable(); | 362 | luaV_gettable(); |
| 400 | break; | 363 | break; |
| 401 | 364 | ||
| 402 | case PUSHSELFW: | 365 | case PUSHSELFW: aux = highbyte(*pc++); |
| 403 | aux = next_word(pc); goto pushself; | 366 | case PUSHSELF: aux += *pc++; { |
| 404 | |||
| 405 | case PUSHSELF: | ||
| 406 | aux = *pc++; goto pushself; | ||
| 407 | |||
| 408 | case PUSHSELF0: case PUSHSELF1: case PUSHSELF2: case PUSHSELF3: | ||
| 409 | case PUSHSELF4: case PUSHSELF5: case PUSHSELF6: case PUSHSELF7: | ||
| 410 | aux -= PUSHSELF0; | ||
| 411 | pushself: { | ||
| 412 | TObject receiver = *(S->top-1); | 367 | TObject receiver = *(S->top-1); |
| 413 | *S->top++ = consts[aux]; | 368 | *S->top++ = consts[aux]; |
| 414 | luaV_gettable(); | 369 | luaV_gettable(); |
| @@ -416,49 +371,21 @@ StkId luaV_execute (Closure *cl, TProtoFunc *tf, StkId base) | |||
| 416 | break; | 371 | break; |
| 417 | } | 372 | } |
| 418 | 373 | ||
| 419 | case PUSHCONSTANTW: | 374 | case PUSHCONSTANTW: aux = highbyte(*pc++); |
| 420 | aux = next_word(pc); goto pushconstant; | 375 | case PUSHCONSTANT: aux += *pc++; |
| 421 | |||
| 422 | case PUSHCONSTANT: | ||
| 423 | aux = *pc++; goto pushconstant; | ||
| 424 | |||
| 425 | case PUSHCONSTANT0: case PUSHCONSTANT1: case PUSHCONSTANT2: | ||
| 426 | case PUSHCONSTANT3: case PUSHCONSTANT4: case PUSHCONSTANT5: | ||
| 427 | case PUSHCONSTANT6: case PUSHCONSTANT7: | ||
| 428 | aux -= PUSHCONSTANT0; | ||
| 429 | pushconstant: | ||
| 430 | *S->top++ = consts[aux]; | 376 | *S->top++ = consts[aux]; |
| 431 | break; | 377 | break; |
| 432 | 378 | ||
| 433 | case PUSHUPVALUE: | 379 | case PUSHUPVALUE: aux = *pc++; |
| 434 | aux = *pc++; goto pushupvalue; | ||
| 435 | |||
| 436 | case PUSHUPVALUE0: case PUSHUPVALUE1: | ||
| 437 | aux -= PUSHUPVALUE0; | ||
| 438 | pushupvalue: | ||
| 439 | *S->top++ = cl->consts[aux+1]; | 380 | *S->top++ = cl->consts[aux+1]; |
| 440 | break; | 381 | break; |
| 441 | 382 | ||
| 442 | case SETLOCAL: | 383 | case SETLOCAL: aux = *pc++; |
| 443 | aux = *pc++; goto setlocal; | ||
| 444 | |||
| 445 | case SETLOCAL0: case SETLOCAL1: case SETLOCAL2: case SETLOCAL3: | ||
| 446 | case SETLOCAL4: case SETLOCAL5: case SETLOCAL6: case SETLOCAL7: | ||
| 447 | aux -= SETLOCAL0; | ||
| 448 | setlocal: | ||
| 449 | *((S->stack+base) + aux) = *(--S->top); | 384 | *((S->stack+base) + aux) = *(--S->top); |
| 450 | break; | 385 | break; |
| 451 | 386 | ||
| 452 | case SETGLOBALW: | 387 | case SETGLOBALW: aux = highbyte(*pc++); |
| 453 | aux = next_word(pc); goto setglobal; | 388 | case SETGLOBAL: aux += *pc++; |
| 454 | |||
| 455 | case SETGLOBAL: | ||
| 456 | aux = *pc++; goto setglobal; | ||
| 457 | |||
| 458 | case SETGLOBAL0: case SETGLOBAL1: case SETGLOBAL2: case SETGLOBAL3: | ||
| 459 | case SETGLOBAL4: case SETGLOBAL5: case SETGLOBAL6: case SETGLOBAL7: | ||
| 460 | aux -= SETGLOBAL0; | ||
| 461 | setglobal: | ||
| 462 | luaV_setglobal(tsvalue(&consts[aux])); | 389 | luaV_setglobal(tsvalue(&consts[aux])); |
| 463 | break; | 390 | break; |
| 464 | 391 | ||
| @@ -470,28 +397,17 @@ StkId luaV_execute (Closure *cl, TProtoFunc *tf, StkId base) | |||
| 470 | luaV_settable(S->top-3-(*pc++), 1); | 397 | luaV_settable(S->top-3-(*pc++), 1); |
| 471 | break; | 398 | break; |
| 472 | 399 | ||
| 473 | case SETLISTW: | 400 | case SETLISTW: aux = highbyte(*pc++); |
| 474 | aux = next_word(pc); aux *= LFIELDS_PER_FLUSH; goto setlist; | 401 | case SETLIST: aux += *pc++; { |
| 475 | |||
| 476 | case SETLIST: | ||
| 477 | aux = *(pc++) * LFIELDS_PER_FLUSH; goto setlist; | ||
| 478 | |||
| 479 | case SETLIST0: | ||
| 480 | aux = 0; | ||
| 481 | setlist: { | ||
| 482 | int n = *(pc++); | 402 | int n = *(pc++); |
| 483 | TObject *arr = S->top-n-1; | 403 | TObject *arr = S->top-n-1; |
| 404 | aux *= LFIELDS_PER_FLUSH; | ||
| 484 | for (; n; n--) | 405 | for (; n; n--) |
| 485 | luaH_setint(avalue(arr), n+aux, --S->top); | 406 | luaH_setint(avalue(arr), n+aux, --S->top); |
| 486 | break; | 407 | break; |
| 487 | } | 408 | } |
| 488 | 409 | ||
| 489 | case SETMAP0: | 410 | case SETMAP: aux = *pc++; { |
| 490 | aux = 0; goto setmap; | ||
| 491 | |||
| 492 | case SETMAP: | ||
| 493 | aux = *pc++; | ||
| 494 | setmap: { | ||
| 495 | TObject *arr = S->top-(2*aux)-3; | 411 | TObject *arr = S->top-(2*aux)-3; |
| 496 | do { | 412 | do { |
| 497 | luaH_set(avalue(arr), S->top-2, S->top-1); | 413 | luaH_set(avalue(arr), S->top-2, S->top-1); |
| @@ -500,34 +416,23 @@ StkId luaV_execute (Closure *cl, TProtoFunc *tf, StkId base) | |||
| 500 | break; | 416 | break; |
| 501 | } | 417 | } |
| 502 | 418 | ||
| 503 | case POP: | 419 | case POP: aux = *pc++; |
| 504 | aux = *pc++; goto pop; | ||
| 505 | |||
| 506 | case POP0: case POP1: | ||
| 507 | aux -= POP0; | ||
| 508 | pop: | ||
| 509 | S->top -= (aux+1); | 420 | S->top -= (aux+1); |
| 510 | break; | 421 | break; |
| 511 | 422 | ||
| 512 | case CREATEARRAYW: | 423 | case CREATEARRAYW: aux = highbyte(*pc++); |
| 513 | aux = next_word(pc); goto createarray; | 424 | case CREATEARRAY: aux += *pc++; |
| 514 | |||
| 515 | case CREATEARRAY0: case CREATEARRAY1: | ||
| 516 | aux -= CREATEARRAY0; goto createarray; | ||
| 517 | |||
| 518 | case CREATEARRAY: | ||
| 519 | aux = *pc++; | ||
| 520 | createarray: | ||
| 521 | luaC_checkGC(); | 425 | luaC_checkGC(); |
| 522 | avalue(S->top) = luaH_new(aux); | 426 | avalue(S->top) = luaH_new(aux); |
| 523 | ttype(S->top) = LUA_T_ARRAY; | 427 | ttype(S->top) = LUA_T_ARRAY; |
| 524 | S->top++; | 428 | S->top++; |
| 525 | break; | 429 | break; |
| 526 | 430 | ||
| 527 | case EQOP: case NEQOP: { | 431 | case NEQOP: aux = 1; |
| 432 | case EQOP: { | ||
| 528 | int res = luaO_equalObj(S->top-2, S->top-1); | 433 | int res = luaO_equalObj(S->top-2, S->top-1); |
| 434 | if (aux) res = !res; | ||
| 529 | S->top--; | 435 | S->top--; |
| 530 | if (aux == NEQOP) res = !res; | ||
| 531 | ttype(S->top-1) = res ? LUA_T_NUMBER : LUA_T_NIL; | 436 | ttype(S->top-1) = res ? LUA_T_NUMBER : LUA_T_NIL; |
| 532 | nvalue(S->top-1) = 1; | 437 | nvalue(S->top-1) = 1; |
| 533 | break; | 438 | break; |
| @@ -630,98 +535,60 @@ StkId luaV_execute (Closure *cl, TProtoFunc *tf, StkId base) | |||
| 630 | nvalue(S->top-1) = 1; | 535 | nvalue(S->top-1) = 1; |
| 631 | break; | 536 | break; |
| 632 | 537 | ||
| 633 | case ONTJMPW: | 538 | case ONTJMPW: aux = highbyte(*pc++); |
| 634 | aux = next_word(pc); goto ontjmp; | 539 | case ONTJMP: aux += *pc++; |
| 635 | |||
| 636 | case ONTJMP: | ||
| 637 | aux = *pc++; | ||
| 638 | ontjmp: | ||
| 639 | if (ttype(S->top-1) != LUA_T_NIL) pc += aux; | 540 | if (ttype(S->top-1) != LUA_T_NIL) pc += aux; |
| 640 | else S->top--; | 541 | else S->top--; |
| 641 | break; | 542 | break; |
| 642 | 543 | ||
| 643 | case ONFJMPW: | 544 | case ONFJMPW: aux = highbyte(*pc++); |
| 644 | aux = next_word(pc); goto onfjmp; | 545 | case ONFJMP: aux += *pc++; |
| 645 | |||
| 646 | case ONFJMP: | ||
| 647 | aux = *pc++; | ||
| 648 | onfjmp: | ||
| 649 | if (ttype(S->top-1) == LUA_T_NIL) pc += aux; | 546 | if (ttype(S->top-1) == LUA_T_NIL) pc += aux; |
| 650 | else S->top--; | 547 | else S->top--; |
| 651 | break; | 548 | break; |
| 652 | 549 | ||
| 653 | case JMPW: | 550 | case JMPW: aux = highbyte(*pc++); |
| 654 | aux = next_word(pc); goto jmp; | 551 | case JMP: aux += *pc++; |
| 655 | |||
| 656 | case JMP: | ||
| 657 | aux = *pc++; | ||
| 658 | jmp: | ||
| 659 | pc += aux; | 552 | pc += aux; |
| 660 | break; | 553 | break; |
| 661 | 554 | ||
| 662 | case IFFJMPW: | 555 | case IFFJMPW: aux = highbyte(*pc++); |
| 663 | aux = next_word(pc); goto iffjmp; | 556 | case IFFJMP: aux += *pc++; |
| 664 | |||
| 665 | case IFFJMP: | ||
| 666 | aux = *pc++; | ||
| 667 | iffjmp: | ||
| 668 | if (ttype(--S->top) == LUA_T_NIL) pc += aux; | 557 | if (ttype(--S->top) == LUA_T_NIL) pc += aux; |
| 669 | break; | 558 | break; |
| 670 | 559 | ||
| 671 | case IFTUPJMPW: | 560 | case IFTUPJMPW: aux = highbyte(*pc++); |
| 672 | aux = next_word(pc); goto iftupjmp; | 561 | case IFTUPJMP: aux += *pc++; |
| 673 | |||
| 674 | case IFTUPJMP: | ||
| 675 | aux = *pc++; | ||
| 676 | iftupjmp: | ||
| 677 | if (ttype(--S->top) != LUA_T_NIL) pc -= aux; | 562 | if (ttype(--S->top) != LUA_T_NIL) pc -= aux; |
| 678 | break; | 563 | break; |
| 679 | 564 | ||
| 680 | case IFFUPJMPW: | 565 | case IFFUPJMPW: aux = highbyte(*pc++); |
| 681 | aux = next_word(pc); goto iffupjmp; | 566 | case IFFUPJMP: aux += *pc++; |
| 682 | |||
| 683 | case IFFUPJMP: | ||
| 684 | aux = *pc++; | ||
| 685 | iffupjmp: | ||
| 686 | if (ttype(--S->top) == LUA_T_NIL) pc -= aux; | 567 | if (ttype(--S->top) == LUA_T_NIL) pc -= aux; |
| 687 | break; | 568 | break; |
| 688 | 569 | ||
| 689 | case CLOSUREW: | 570 | case CLOSURE: aux = *pc++; |
| 690 | aux = next_word(pc); goto closure; | ||
| 691 | |||
| 692 | case CLOSURE: | ||
| 693 | aux = *pc++; | ||
| 694 | closure: | ||
| 695 | *S->top++ = consts[aux]; | 571 | *S->top++ = consts[aux]; |
| 696 | luaV_closure(*pc++); | 572 | luaV_closure(*pc++); |
| 697 | luaC_checkGC(); | 573 | luaC_checkGC(); |
| 698 | break; | 574 | break; |
| 699 | 575 | ||
| 700 | case CALLFUNC: | 576 | case CALLFUNC: aux = *pc++; { |
| 701 | aux = *pc++; goto callfunc; | ||
| 702 | |||
| 703 | case CALLFUNC0: case CALLFUNC1: | ||
| 704 | aux -= CALLFUNC0; | ||
| 705 | callfunc: { | ||
| 706 | StkId newBase = (S->top-S->stack)-(*pc++); | 577 | StkId newBase = (S->top-S->stack)-(*pc++); |
| 707 | luaD_call(newBase, aux); | 578 | luaD_call(newBase, aux); |
| 708 | break; | 579 | break; |
| 709 | } | 580 | } |
| 710 | 581 | ||
| 711 | case ENDCODE: | 582 | case ENDCODE: aux = 1; |
| 712 | S->top = S->stack + base; | 583 | S->top = S->stack + base; |
| 713 | /* goes through */ | 584 | /* goes through */ |
| 714 | case RETCODE: | 585 | case RETCODE: |
| 715 | if (lua_callhook) | 586 | if (lua_callhook) |
| 716 | luaD_callHook(base, NULL, 1); | 587 | luaD_callHook(base, NULL, 1); |
| 717 | return (base + ((aux==RETCODE) ? *pc : 0)); | 588 | return base + (aux ? 0 : *pc); |
| 718 | |||
| 719 | case SETLINEW: | ||
| 720 | aux = next_word(pc); goto setline; | ||
| 721 | 589 | ||
| 722 | case SETLINE: | 590 | case SETLINEW: aux = highbyte(*pc++); |
| 723 | aux = *pc++; | 591 | case SETLINE: aux += *pc++; |
| 724 | setline: | ||
| 725 | if ((S->stack+base-1)->ttype != LUA_T_LINE) { | 592 | if ((S->stack+base-1)->ttype != LUA_T_LINE) { |
| 726 | /* open space for LINE value */ | 593 | /* open space for LINE value */ |
| 727 | luaD_openstack((S->top-S->stack)-base); | 594 | luaD_openstack((S->top-S->stack)-base); |
