aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lvm.c175
1 files changed, 96 insertions, 79 deletions
diff --git a/lvm.c b/lvm.c
index 3f6e82bb..69cb38f4 100644
--- a/lvm.c
+++ b/lvm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.c,v 2.38 2005/04/11 18:01:29 roberto Exp roberto $ 2** $Id: lvm.c,v 2.39 2005/05/02 17:49:43 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*/
@@ -83,10 +83,10 @@ static void traceexec (lua_State *L, const Instruction *pc) {
83static void callTMres (lua_State *L, StkId res, const TValue *f, 83static void callTMres (lua_State *L, StkId res, const TValue *f,
84 const TValue *p1, const TValue *p2) { 84 const TValue *p1, const TValue *p2) {
85 ptrdiff_t result = savestack(L, res); 85 ptrdiff_t result = savestack(L, res);
86 luaD_checkstack(L, 3);
87 setobj2s(L, L->top, f); /* push function */ 86 setobj2s(L, L->top, f); /* push function */
88 setobj2s(L, L->top+1, p1); /* 1st argument */ 87 setobj2s(L, L->top+1, p1); /* 1st argument */
89 setobj2s(L, L->top+2, p2); /* 2nd argument */ 88 setobj2s(L, L->top+2, p2); /* 2nd argument */
89 luaD_checkstack(L, 3);
90 L->top += 3; 90 L->top += 3;
91 luaD_call(L, L->top - 3, 1); 91 luaD_call(L, L->top - 3, 1);
92 res = restorestack(L, result); 92 res = restorestack(L, result);
@@ -97,11 +97,11 @@ static void callTMres (lua_State *L, StkId res, const TValue *f,
97 97
98 98
99static void callTM (lua_State *L, const TValue *f, const TValue *p1, const TValue *p2, const TValue *p3) { 99static void callTM (lua_State *L, const TValue *f, const TValue *p1, const TValue *p2, const TValue *p3) {
100 luaD_checkstack(L, 4);
101 setobj2s(L, L->top, f); /* push function */ 100 setobj2s(L, L->top, f); /* push function */
102 setobj2s(L, L->top+1, p1); /* 1st argument */ 101 setobj2s(L, L->top+1, p1); /* 1st argument */
103 setobj2s(L, L->top+2, p2); /* 2nd argument */ 102 setobj2s(L, L->top+2, p2); /* 2nd argument */
104 setobj2s(L, L->top+3, p3); /* 3th argument */ 103 setobj2s(L, L->top+3, p3); /* 3th argument */
104 luaD_checkstack(L, 4);
105 L->top += 4; 105 L->top += 4;
106 luaD_call(L, L->top - 4, 0); 106 luaD_call(L, L->top - 4, 0);
107} 107}
@@ -354,8 +354,12 @@ static StkId Arith (lua_State *L, StkId ra, const TValue *rb,
354#define dojump(L,pc,i) {(pc) += (i); luai_threadyield(L);} 354#define dojump(L,pc,i) {(pc) += (i); luai_threadyield(L);}
355 355
356 356
357#define Protect(x) { L->savedpc = pc; {x;}; base = L->base; }
358
359
357StkId luaV_execute (lua_State *L, int nexeccalls) { 360StkId luaV_execute (lua_State *L, int nexeccalls) {
358 LClosure *cl; 361 LClosure *cl;
362 StkId base;
359 TValue *k; 363 TValue *k;
360 const Instruction *pc; 364 const Instruction *pc;
361 callentry: /* entry point when calling new functions */ 365 callentry: /* entry point when calling new functions */
@@ -364,25 +368,24 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
364 retentry: /* entry point when returning to old functions */ 368 retentry: /* entry point when returning to old functions */
365 pc = L->savedpc; 369 pc = L->savedpc;
366 cl = &clvalue(L->ci->func)->l; 370 cl = &clvalue(L->ci->func)->l;
371 base = L->base;
367 k = cl->p->k; 372 k = cl->p->k;
368 /* main loop of interpreter */ 373 /* main loop of interpreter */
369 for (;;) { 374 for (;;) {
370 StkId base;
371 const Instruction i = *pc++; 375 const Instruction i = *pc++;
372 StkId ra; 376 StkId ra;
373 if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) && 377 if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
374 (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) { 378 (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
375 traceexec(L, pc); /***/ 379 traceexec(L, pc);
376 if (L->status == LUA_YIELD) { /* did hook yield? */ 380 if (L->status == LUA_YIELD) { /* did hook yield? */
377 L->savedpc = pc - 1; 381 L->savedpc = pc - 1;
378 return NULL; 382 return NULL;
379 } 383 }
384 base = L->base;
380 } 385 }
381 /* warning!! several calls may realloc the stack and invalidate `ra' */ 386 /* warning!! several calls may realloc the stack and invalidate `ra' */
382 base = L->base;
383 ra = RA(i); 387 ra = RA(i);
384 L->savedpc = pc; 388 lua_assert(base == L->base && L->base == L->ci->base);
385 lua_assert(base == L->ci->base);
386 lua_assert(base <= L->top && L->top <= L->stack + L->stacksize); 389 lua_assert(base <= L->top && L->top <= L->stack + L->stacksize);
387 lua_assert(L->top == L->ci->top || luaG_checkopenop(i)); 390 lua_assert(L->top == L->ci->top || luaG_checkopenop(i));
388 switch (GET_OPCODE(i)) { 391 switch (GET_OPCODE(i)) {
@@ -416,18 +419,18 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
416 TValue *rb = KBx(i); 419 TValue *rb = KBx(i);
417 sethvalue(L, &g, cl->env); 420 sethvalue(L, &g, cl->env);
418 lua_assert(ttisstring(rb)); 421 lua_assert(ttisstring(rb));
419 luaV_gettable(L, &g, rb, ra); /***/ 422 Protect(luaV_gettable(L, &g, rb, ra));
420 continue; 423 continue;
421 } 424 }
422 case OP_GETTABLE: { 425 case OP_GETTABLE: {
423 luaV_gettable(L, RB(i), RKC(i), ra); /***/ 426 Protect(luaV_gettable(L, RB(i), RKC(i), ra));
424 continue; 427 continue;
425 } 428 }
426 case OP_SETGLOBAL: { 429 case OP_SETGLOBAL: {
427 TValue g; 430 TValue g;
428 sethvalue(L, &g, cl->env); 431 sethvalue(L, &g, cl->env);
429 lua_assert(ttisstring(KBx(i))); 432 lua_assert(ttisstring(KBx(i)));
430 luaV_settable(L, &g, KBx(i), ra); /***/ 433 Protect(luaV_settable(L, &g, KBx(i), ra));
431 continue; 434 continue;
432 } 435 }
433 case OP_SETUPVAL: { 436 case OP_SETUPVAL: {
@@ -437,20 +440,20 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
437 continue; 440 continue;
438 } 441 }
439 case OP_SETTABLE: { 442 case OP_SETTABLE: {
440 luaV_settable(L, ra, RKB(i), RKC(i)); /***/ 443 Protect(luaV_settable(L, ra, RKB(i), RKC(i)));
441 continue; 444 continue;
442 } 445 }
443 case OP_NEWTABLE: { 446 case OP_NEWTABLE: {
444 int b = GETARG_B(i); 447 int b = GETARG_B(i);
445 int c = GETARG_C(i); 448 int c = GETARG_C(i);
446 sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c))); 449 sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c)));
447 luaC_checkGC(L); /***/ 450 Protect(luaC_checkGC(L));
448 continue; 451 continue;
449 } 452 }
450 case OP_SELF: { 453 case OP_SELF: {
451 StkId rb = RB(i); 454 StkId rb = RB(i);
452 setobjs2s(L, ra+1, rb); 455 setobjs2s(L, ra+1, rb);
453 luaV_gettable(L, rb, RKC(i), ra); /***/ 456 Protect(luaV_gettable(L, rb, RKC(i), ra));
454 continue; 457 continue;
455 } 458 }
456 case OP_ADD: { 459 case OP_ADD: {
@@ -461,7 +464,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
461 setnvalue(ra, luai_numadd(nb, nc)); 464 setnvalue(ra, luai_numadd(nb, nc));
462 } 465 }
463 else 466 else
464 Arith(L, ra, rb, rc, TM_ADD); /***/ 467 Protect(Arith(L, ra, rb, rc, TM_ADD));
465 continue; 468 continue;
466 } 469 }
467 case OP_SUB: { 470 case OP_SUB: {
@@ -472,7 +475,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
472 setnvalue(ra, luai_numsub(nb, nc)); 475 setnvalue(ra, luai_numsub(nb, nc));
473 } 476 }
474 else 477 else
475 Arith(L, ra, rb, rc, TM_SUB); /***/ 478 Protect(Arith(L, ra, rb, rc, TM_SUB));
476 continue; 479 continue;
477 } 480 }
478 case OP_MUL: { 481 case OP_MUL: {
@@ -483,7 +486,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
483 setnvalue(ra, luai_nummul(nb, nc)); 486 setnvalue(ra, luai_nummul(nb, nc));
484 } 487 }
485 else 488 else
486 Arith(L, ra, rb, rc, TM_MUL); /***/ 489 Protect(Arith(L, ra, rb, rc, TM_MUL));
487 continue; 490 continue;
488 } 491 }
489 case OP_DIV: { 492 case OP_DIV: {
@@ -494,7 +497,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
494 setnvalue(ra, luai_numdiv(nb, nc)); 497 setnvalue(ra, luai_numdiv(nb, nc));
495 } 498 }
496 else 499 else
497 Arith(L, ra, rb, rc, TM_DIV); /***/ 500 Protect(Arith(L, ra, rb, rc, TM_DIV));
498 continue; 501 continue;
499 } 502 }
500 case OP_MOD: { 503 case OP_MOD: {
@@ -505,7 +508,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
505 setnvalue(ra, luai_nummod(nb, nc)); 508 setnvalue(ra, luai_nummod(nb, nc));
506 } 509 }
507 else 510 else
508 Arith(L, ra, rb, rc, TM_MOD); /***/ 511 Protect(Arith(L, ra, rb, rc, TM_MOD));
509 continue; 512 continue;
510 } 513 }
511 case OP_POW: { 514 case OP_POW: {
@@ -516,7 +519,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
516 setnvalue(ra, luai_numpow(nb, nc)); 519 setnvalue(ra, luai_numpow(nb, nc));
517 } 520 }
518 else 521 else
519 Arith(L, ra, rb, rc, TM_POW); /***/ 522 Protect(Arith(L, ra, rb, rc, TM_POW));
520 continue; 523 continue;
521 } 524 }
522 case OP_UNM: { 525 case OP_UNM: {
@@ -528,8 +531,10 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
528 } 531 }
529 else { 532 else {
530 rb = RB(i); /* `tonumber' erased `rb' */ 533 rb = RB(i); /* `tonumber' erased `rb' */
531 if (!call_binTM(L, rb, &luaO_nilobject, ra, TM_UNM)) /***/ 534 Protect(
532 luaG_aritherror(L, rb, &luaO_nilobject); 535 if (!call_binTM(L, rb, &luaO_nilobject, ra, TM_UNM))
536 luaG_aritherror(L, rb, &luaO_nilobject);
537 )
533 } 538 }
534 continue; 539 continue;
535 } 540 }
@@ -544,17 +549,17 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
544 setnvalue(ra, cast(lua_Number, luaH_getn(hvalue(rb)))); 549 setnvalue(ra, cast(lua_Number, luaH_getn(hvalue(rb))));
545 } 550 }
546 else { /* try metamethod */ 551 else { /* try metamethod */
547 if (!call_binTM(L, rb, &luaO_nilobject, ra, TM_SIZ)) /***/ 552 Protect(
548 luaG_typeerror(L, rb, "get size of"); 553 if (!call_binTM(L, rb, &luaO_nilobject, ra, TM_SIZ))
554 luaG_typeerror(L, rb, "get size of");
555 )
549 } 556 }
550 continue; 557 continue;
551 } 558 }
552 case OP_CONCAT: { 559 case OP_CONCAT: {
553 int b = GETARG_B(i); 560 int b = GETARG_B(i);
554 int c = GETARG_C(i); 561 int c = GETARG_C(i);
555 luaV_concat(L, c-b+1, c); /* may change `base' (and `ra') */ /***/ 562 Protect(luaV_concat(L, c-b+1, c); luaC_checkGC(L));
556 luaC_checkGC(L); /***/
557 base = L->base;
558 setobjs2s(L, RA(i), base+b); 563 setobjs2s(L, RA(i), base+b);
559 continue; 564 continue;
560 } 565 }
@@ -563,18 +568,27 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
563 continue; 568 continue;
564 } 569 }
565 case OP_EQ: { 570 case OP_EQ: {
566 if (equalobj(L, RKB(i), RKC(i)) != GETARG_A(i)) pc++; /***/ 571 Protect(
567 else dojump(L, pc, GETARG_sBx(*pc) + 1); 572 if (equalobj(L, RKB(i), RKC(i)) == GETARG_A(i))
573 dojump(L, pc, GETARG_sBx(*pc));
574 )
575 pc++;
568 continue; 576 continue;
569 } 577 }
570 case OP_LT: { 578 case OP_LT: {
571 if (luaV_lessthan(L, RKB(i), RKC(i)) != GETARG_A(i)) pc++; /***/ 579 Protect(
572 else dojump(L, pc, GETARG_sBx(*pc) + 1); 580 if (luaV_lessthan(L, RKB(i), RKC(i)) == GETARG_A(i))
581 dojump(L, pc, GETARG_sBx(*pc));
582 )
583 pc++;
573 continue; 584 continue;
574 } 585 }
575 case OP_LE: { 586 case OP_LE: {
576 if (lessequal(L, RKB(i), RKC(i)) != GETARG_A(i)) pc++; /***/ 587 Protect(
577 else dojump(L, pc, GETARG_sBx(*pc) + 1); 588 if (lessequal(L, RKB(i), RKC(i)) == GETARG_A(i))
589 dojump(L, pc, GETARG_sBx(*pc));
590 )
591 pc++;
578 continue; 592 continue;
579 } 593 }
580 case OP_TEST: { 594 case OP_TEST: {
@@ -586,62 +600,65 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
586 } 600 }
587 continue; 601 continue;
588 } 602 }
589 case OP_CALL: { /***/ 603 case OP_CALL: {
590 int pcr;
591 int b = GETARG_B(i); 604 int b = GETARG_B(i);
592 int nresults = GETARG_C(i) - 1; 605 int nresults = GETARG_C(i) - 1;
593 if (b != 0) L->top = ra+b; /* else previous instruction set top */ 606 if (b != 0) L->top = ra+b; /* else previous instruction set top */
594 pcr = luaD_precall(L, ra, nresults); 607 L->savedpc = pc;
595 if (pcr == PCRLUA) { 608 switch (luaD_precall(L, ra, nresults)) {
596 nexeccalls++; 609 case PCRLUA: {
597 goto callentry; /* restart luaV_execute over new Lua function */ 610 nexeccalls++;
598 } 611 goto callentry; /* restart luaV_execute over new Lua function */
599 else if (pcr == PCRC) { 612 }
600 /* it was a C function (`precall' called it); adjust results */ 613 case PCRC: {
601 if (nresults >= 0) L->top = L->ci->top; 614 /* it was a C function (`precall' called it); adjust results */
602 continue; 615 if (nresults >= 0) L->top = L->ci->top;
603 } 616 base = L->base;
604 else { 617 continue;
605 lua_assert(pcr == PCRYIELD); 618 }
606 return NULL; 619 default: {
620 return NULL;
621 }
607 } 622 }
608 } 623 }
609 case OP_TAILCALL: { /***/ 624 case OP_TAILCALL: {
610 int pcr;
611 int b = GETARG_B(i); 625 int b = GETARG_B(i);
612 if (b != 0) L->top = ra+b; /* else previous instruction set top */ 626 if (b != 0) L->top = ra+b; /* else previous instruction set top */
627 L->savedpc = pc;
613 lua_assert(GETARG_C(i) - 1 == LUA_MULTRET); 628 lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
614 pcr = luaD_precall(L, ra, LUA_MULTRET); 629 switch (luaD_precall(L, ra, LUA_MULTRET)) {
615 if (pcr == PCRLUA) { 630 case PCRLUA: {
616 /* tail call: put new frame in place of previous one */ 631 /* tail call: put new frame in place of previous one */
617 CallInfo *ci = L->ci - 1; /* previous frame */ 632 CallInfo *ci = L->ci - 1; /* previous frame */
618 int aux; 633 int aux;
619 StkId func = ci->func; 634 StkId func = ci->func;
620 StkId pfunc = (ci+1)->func; /* previous function index */ 635 StkId pfunc = (ci+1)->func; /* previous function index */
621 if (L->openupval) luaF_close(L, ci->base); 636 if (L->openupval) luaF_close(L, ci->base);
622 L->base = ci->base = ci->func + ((ci+1)->base - pfunc); 637 L->base = ci->base = ci->func + ((ci+1)->base - pfunc);
623 for (aux = 0; pfunc+aux < L->top; aux++) /* move frame down */ 638 for (aux = 0; pfunc+aux < L->top; aux++) /* move frame down */
624 setobjs2s(L, func+aux, pfunc+aux); 639 setobjs2s(L, func+aux, pfunc+aux);
625 ci->top = L->top = func+aux; /* correct top */ 640 ci->top = L->top = func+aux; /* correct top */
626 lua_assert(L->top == L->base + clvalue(func)->l.p->maxstacksize); 641 lua_assert(L->top == L->base + clvalue(func)->l.p->maxstacksize);
627 ci->savedpc = L->savedpc; 642 ci->savedpc = L->savedpc;
628 ci->tailcalls++; /* one more call lost */ 643 ci->tailcalls++; /* one more call lost */
629 L->ci--; /* remove new frame */ 644 L->ci--; /* remove new frame */
630 goto callentry; 645 goto callentry;
631 } 646 }
632 else if (pcr == PCRC) { 647 case PCRC: {
633 /* it was a C function (`precall' called it) */ 648 /* it was a C function (`precall' called it) */
634 continue; 649 base = L->base;
635 } 650 continue;
636 else { 651 }
637 lua_assert(pcr == PCRYIELD); 652 default: {
638 return NULL; 653 return NULL;
654 }
639 } 655 }
640 } 656 }
641 case OP_RETURN: { 657 case OP_RETURN: {
642 int b = GETARG_B(i); 658 int b = GETARG_B(i);
643 if (b != 0) L->top = ra+b-1; 659 if (b != 0) L->top = ra+b-1;
644 if (L->openupval) luaF_close(L, base); 660 if (L->openupval) luaF_close(L, base);
661 L->savedpc = pc;
645 if (--nexeccalls == 0) /* was previous function running `here'? */ 662 if (--nexeccalls == 0) /* was previous function running `here'? */
646 return ra; /* no: return */ 663 return ra; /* no: return */
647 else { /* yes: continue its execution */ 664 else { /* yes: continue its execution */
@@ -664,10 +681,11 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
664 } 681 }
665 continue; 682 continue;
666 } 683 }
667 case OP_FORPREP: { /***/ 684 case OP_FORPREP: {
668 const TValue *init = ra; 685 const TValue *init = ra;
669 const TValue *plimit = ra+1; 686 const TValue *plimit = ra+1;
670 const TValue *pstep = ra+2; 687 const TValue *pstep = ra+2;
688 L->savedpc = pc; /* next steps may throw errors */
671 if (!tonumber(init, ra)) 689 if (!tonumber(init, ra))
672 luaG_runerror(L, "`for' initial value must be a number"); 690 luaG_runerror(L, "`for' initial value must be a number");
673 else if (!tonumber(plimit, ra+1)) 691 else if (!tonumber(plimit, ra+1))
@@ -684,9 +702,8 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
684 setobjs2s(L, cb+1, ra+1); 702 setobjs2s(L, cb+1, ra+1);
685 setobjs2s(L, cb, ra); 703 setobjs2s(L, cb, ra);
686 L->top = cb+3; /* func. + 2 args (state and index) */ 704 L->top = cb+3; /* func. + 2 args (state and index) */
687 luaD_call(L, cb, GETARG_C(i)); /***/ 705 Protect(luaD_call(L, cb, GETARG_C(i)));
688 L->top = L->ci->top; 706 L->top = L->ci->top;
689 base = L->base;
690 cb = RA(i) + 3; /* previous call may change the stack */ 707 cb = RA(i) + 3; /* previous call may change the stack */
691 if (ttisnil(cb)) /* break loop? */ 708 if (ttisnil(cb)) /* break loop? */
692 pc++; /* skip jump (break loop) */ 709 pc++; /* skip jump (break loop) */
@@ -747,7 +764,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
747 } 764 }
748 } 765 }
749 setclvalue(L, ra, ncl); 766 setclvalue(L, ra, ncl);
750 luaC_checkGC(L); /***/ 767 Protect(luaC_checkGC(L));
751 continue; 768 continue;
752 } 769 }
753 case OP_VARARG: { 770 case OP_VARARG: {