aboutsummaryrefslogtreecommitdiff
path: root/opcode.c
diff options
context:
space:
mode:
authorWaldemar Celes <celes@tecgraf.puc-rio.br>1995-04-11 14:56:30 -0300
committerWaldemar Celes <celes@tecgraf.puc-rio.br>1995-04-11 14:56:30 -0300
commit8156604823aa487f4436d33fe89302598faab3db (patch)
tree6ed6e5ec52c397e8e3ce11fec01440fd4287b1ab /opcode.c
parent36b6fdda833d4dd4a866d2c96ac42062b2b29af9 (diff)
downloadlua-8156604823aa487f4436d33fe89302598faab3db.tar.gz
lua-8156604823aa487f4436d33fe89302598faab3db.tar.bz2
lua-8156604823aa487f4436d33fe89302598faab3db.zip
run-time stack now is controled at run time, instead of
compilation time.
Diffstat (limited to 'opcode.c')
-rw-r--r--opcode.c136
1 files changed, 79 insertions, 57 deletions
diff --git a/opcode.c b/opcode.c
index f46f6a88..40147af1 100644
--- a/opcode.c
+++ b/opcode.c
@@ -3,7 +3,7 @@
3** TecCGraf - PUC-Rio 3** TecCGraf - PUC-Rio
4*/ 4*/
5 5
6char *rcs_opcode="$Id: opcode.c,v 3.34 1995/02/06 19:35:09 roberto Exp roberto $"; 6char *rcs_opcode="$Id: opcode.c,v 3.35 1995/02/10 12:51:29 roberto Exp celes $";
7 7
8#include <setjmp.h> 8#include <setjmp.h>
9#include <stdlib.h> 9#include <stdlib.h>
@@ -23,13 +23,15 @@ char *rcs_opcode="$Id: opcode.c,v 3.34 1995/02/06 19:35:09 roberto Exp roberto $
23#define tostring(o) ((tag(o) != LUA_T_STRING) && (lua_tostring(o) != 0)) 23#define tostring(o) ((tag(o) != LUA_T_STRING) && (lua_tostring(o) != 0))
24 24
25 25
26#define STACK_BUFFER (STACKGAP+128) 26#define STACK_SIZE 128
27 27
28typedef int StkId; /* index to stack elements */ 28typedef int StkId; /* index to stack elements */
29 29
30static Long maxstack = 0L; 30static Object initial_stack;
31static Object *stack = NULL; 31
32static Object *top = NULL; 32static Object *stackLimit = &initial_stack+1;
33static Object *stack = &initial_stack;
34static Object *top = &initial_stack;
33 35
34 36
35/* macros to convert from lua_Object to (Object *) and back */ 37/* macros to convert from lua_Object to (Object *) and back */
@@ -38,6 +40,11 @@ static Object *top = NULL;
38#define Ref(st) ((st)-stack+1) 40#define Ref(st) ((st)-stack+1)
39 41
40 42
43/* macro to increment stack top. There must be always an empty slot in
44* the stack
45*/
46#define incr_top if (++top >= stackLimit) growstack()
47
41static StkId CBase = 0; /* when Lua calls C or C calls Lua, points to */ 48static StkId CBase = 0; /* when Lua calls C or C calls Lua, points to */
42 /* the first slot after the last parameter. */ 49 /* the first slot after the last parameter. */
43static int CnResults = 0; /* when Lua calls C, has the number of parameters; */ 50static int CnResults = 0; /* when Lua calls C, has the number of parameters; */
@@ -88,29 +95,35 @@ void lua_error (char *s)
88*/ 95*/
89static void lua_initstack (void) 96static void lua_initstack (void)
90{ 97{
91 maxstack = STACK_BUFFER; 98 Long maxstack = STACK_SIZE;
92 stack = newvector(maxstack, Object); 99 stack = newvector(maxstack, Object);
100 stackLimit = stack+maxstack;
93 top = stack; 101 top = stack;
102 *top = initial_stack;
94} 103}
95 104
96 105
97/* 106/*
98** Check stack overflow and, if necessary, realloc vector 107** Check stack overflow and, if necessary, realloc vector
99*/ 108*/
100#define lua_checkstack(n) if ((Long)(n) > maxstack) checkstack(n) 109#define lua_checkstack(nt) if ((nt) >= stackLimit) growstack()
101 110
102static void checkstack (StkId n) 111static void growstack (void)
103{ 112{
104 StkId t; 113 StkId t = top-stack;
105 if (stack == NULL) 114 if (stack == &initial_stack)
106 lua_initstack(); 115 lua_initstack();
107 if (maxstack >= MAX_INT) 116 else
108 lua_error("stack size overflow"); 117 {
109 t = top-stack; 118 Long maxstack = stackLimit - stack;
110 maxstack *= 2; 119 if (maxstack >= MAX_INT)
111 if (maxstack >= MAX_INT) 120 lua_error("stack size overflow");
112 maxstack = MAX_INT; 121 maxstack *= 2;
113 stack = growvector(stack, maxstack, Object); 122 if (maxstack >= MAX_INT)
123 maxstack = MAX_INT;
124 stack = growvector(stack, maxstack, Object);
125 stackLimit = stack+maxstack;
126 }
114 top = stack + t; 127 top = stack + t;
115} 128}
116 129
@@ -185,8 +198,8 @@ static int lua_tostring (Object *obj)
185static void adjust_top (StkId newtop) 198static void adjust_top (StkId newtop)
186{ 199{
187 Object *nt; 200 Object *nt;
188 lua_checkstack(newtop); 201 lua_checkstack(stack+newtop);
189 nt = stack+newtop; 202 nt = stack+newtop; /* warning: previous call may change stack */
190 while (top < nt) tag(top++) = LUA_T_NIL; 203 while (top < nt) tag(top++) = LUA_T_NIL;
191 top = nt; /* top could be bigger than newtop */ 204 top = nt; /* top could be bigger than newtop */
192} 205}
@@ -227,7 +240,7 @@ static void call_funcFB (Object *func, StkId base, int nResults, StkId whereRes)
227 /* open space for first parameter (func) */ 240 /* open space for first parameter (func) */
228 for (i=top-stack; i>base; i--) 241 for (i=top-stack; i>base; i--)
229 stack[i] = stack[i-1]; 242 stack[i] = stack[i-1];
230 top++; 243 incr_top;
231 stack[base] = *func; 244 stack[base] = *func;
232 do_call(&luaI_fallBacks[FB_FUNCTION].function, base, nResults, whereRes); 245 do_call(&luaI_fallBacks[FB_FUNCTION].function, base, nResults, whereRes);
233} 246}
@@ -499,7 +512,7 @@ lua_Object lua_createtable (void)
499 adjustC(0); 512 adjustC(0);
500 avalue(top) = lua_createarray(0); 513 avalue(top) = lua_createarray(0);
501 tag(top) = LUA_T_ARRAY; 514 tag(top) = LUA_T_ARRAY;
502 top++; 515 incr_top;
503 CBase++; /* incorporate object in the stack */ 516 CBase++; /* incorporate object in the stack */
504 return Ref(top-1); 517 return Ref(top-1);
505} 518}
@@ -561,7 +574,7 @@ lua_Object lua_getlocked (int ref)
561{ 574{
562 adjustC(0); 575 adjustC(0);
563 *top = *luaI_getlocked(ref); 576 *top = *luaI_getlocked(ref);
564 top++; 577 incr_top;
565 CBase++; /* incorporate object in the stack */ 578 CBase++; /* incorporate object in the stack */
566 return Ref(top-1); 579 return Ref(top-1);
567} 580}
@@ -569,9 +582,8 @@ lua_Object lua_getlocked (int ref)
569 582
570void lua_pushlocked (int ref) 583void lua_pushlocked (int ref)
571{ 584{
572 lua_checkstack(top-stack+1);
573 *top = *luaI_getlocked(ref); 585 *top = *luaI_getlocked(ref);
574 top++; 586 incr_top;
575} 587}
576 588
577 589
@@ -590,7 +602,7 @@ lua_Object lua_getglobal (char *name)
590 Word n = luaI_findsymbolbyname(name); 602 Word n = luaI_findsymbolbyname(name);
591 adjustC(0); 603 adjustC(0);
592 *top = s_object(n); 604 *top = s_object(n);
593 top++; 605 incr_top;
594 CBase++; /* incorporate object in the stack */ 606 CBase++; /* incorporate object in the stack */
595 return Ref(top-1); 607 return Ref(top-1);
596} 608}
@@ -610,8 +622,8 @@ void lua_storeglobal (char *name)
610*/ 622*/
611void lua_pushnil (void) 623void lua_pushnil (void)
612{ 624{
613 lua_checkstack(top-stack+1); 625 tag(top) = LUA_T_NIL;
614 tag(top++) = LUA_T_NIL; 626 incr_top;
615} 627}
616 628
617/* 629/*
@@ -619,8 +631,8 @@ void lua_pushnil (void)
619*/ 631*/
620void lua_pushnumber (real n) 632void lua_pushnumber (real n)
621{ 633{
622 lua_checkstack(top-stack+1); 634 tag(top) = LUA_T_NUMBER; nvalue(top) = n;
623 tag(top) = LUA_T_NUMBER; nvalue(top++) = n; 635 incr_top;
624} 636}
625 637
626/* 638/*
@@ -628,10 +640,9 @@ void lua_pushnumber (real n)
628*/ 640*/
629void lua_pushstring (char *s) 641void lua_pushstring (char *s)
630{ 642{
631 lua_checkstack(top-stack+1);
632 tsvalue(top) = lua_createstring(s); 643 tsvalue(top) = lua_createstring(s);
633 tag(top) = LUA_T_STRING; 644 tag(top) = LUA_T_STRING;
634 top++; 645 incr_top;
635} 646}
636 647
637/* 648/*
@@ -639,10 +650,9 @@ void lua_pushstring (char *s)
639*/ 650*/
640void lua_pushliteral (char *s) 651void lua_pushliteral (char *s)
641{ 652{
642 lua_checkstack(top-stack+1);
643 tsvalue(top) = lua_constant[luaI_findconstant(lua_constcreate(s))]; 653 tsvalue(top) = lua_constant[luaI_findconstant(lua_constcreate(s))];
644 tag(top) = LUA_T_STRING; 654 tag(top) = LUA_T_STRING;
645 top++; 655 incr_top;
646} 656}
647 657
648/* 658/*
@@ -650,8 +660,8 @@ void lua_pushliteral (char *s)
650*/ 660*/
651void lua_pushcfunction (lua_CFunction fn) 661void lua_pushcfunction (lua_CFunction fn)
652{ 662{
653 lua_checkstack(top-stack+1); 663 tag(top) = LUA_T_CFUNCTION; fvalue(top) = fn;
654 tag(top) = LUA_T_CFUNCTION; fvalue(top++) = fn; 664 incr_top;
655} 665}
656 666
657/* 667/*
@@ -660,8 +670,8 @@ void lua_pushcfunction (lua_CFunction fn)
660void lua_pushusertag (void *u, int tag) 670void lua_pushusertag (void *u, int tag)
661{ 671{
662 if (tag < LUA_T_USERDATA) return; 672 if (tag < LUA_T_USERDATA) return;
663 lua_checkstack(top-stack+1); 673 tag(top) = tag; uvalue(top) = u;
664 tag(top) = tag; uvalue(top++) = u; 674 incr_top;
665} 675}
666 676
667/* 677/*
@@ -669,8 +679,8 @@ void lua_pushusertag (void *u, int tag)
669*/ 679*/
670void lua_pushobject (lua_Object o) 680void lua_pushobject (lua_Object o)
671{ 681{
672 lua_checkstack(top-stack+1); 682 *top = *Address(o);
673 *top++ = *Address(o); 683 incr_top;
674} 684}
675 685
676/* 686/*
@@ -678,8 +688,8 @@ void lua_pushobject (lua_Object o)
678*/ 688*/
679void luaI_pushobject (Object *o) 689void luaI_pushobject (Object *o)
680{ 690{
681 lua_checkstack(top-stack+1); 691 *top = *o;
682 *top++ = *o; 692 incr_top;
683} 693}
684 694
685int lua_type (lua_Object o) 695int lua_type (lua_Object o)
@@ -693,7 +703,8 @@ int lua_type (lua_Object o)
693 703
694void luaI_gcFB (Object *o) 704void luaI_gcFB (Object *o)
695{ 705{
696 *(top++) = *o; 706 *top = *o;
707 incr_top;
697 do_call(&luaI_fallBacks[FB_GC].function, (top-stack)-1, 0, (top-stack)-1); 708 do_call(&luaI_fallBacks[FB_GC].function, (top-stack)-1, 0, (top-stack)-1);
698} 709}
699 710
@@ -734,26 +745,28 @@ static void comparison (lua_Type tag_less, lua_Type tag_equal,
734*/ 745*/
735static StkId lua_execute (Byte *pc, StkId base) 746static StkId lua_execute (Byte *pc, StkId base)
736{ 747{
737 lua_checkstack(STACKGAP+MAX_TEMPS+base);
738 while (1) 748 while (1)
739 { 749 {
740 OpCode opcode; 750 OpCode opcode;
741 switch (opcode = (OpCode)*pc++) 751 switch (opcode = (OpCode)*pc++)
742 { 752 {
743 case PUSHNIL: tag(top++) = LUA_T_NIL; break; 753 case PUSHNIL: tag(top) = LUA_T_NIL; incr_top; break;
744 754
745 case PUSH0: case PUSH1: case PUSH2: 755 case PUSH0: case PUSH1: case PUSH2:
746 tag(top) = LUA_T_NUMBER; 756 tag(top) = LUA_T_NUMBER;
747 nvalue(top++) = opcode-PUSH0; 757 nvalue(top) = opcode-PUSH0;
758 incr_top;
748 break; 759 break;
749 760
750 case PUSHBYTE: tag(top) = LUA_T_NUMBER; nvalue(top++) = *pc++; break; 761 case PUSHBYTE:
762 tag(top) = LUA_T_NUMBER; nvalue(top) = *pc++; incr_top; break;
751 763
752 case PUSHWORD: 764 case PUSHWORD:
753 { 765 {
754 CodeWord code; 766 CodeWord code;
755 get_word(code,pc); 767 get_word(code,pc);
756 tag(top) = LUA_T_NUMBER; nvalue(top++) = code.w; 768 tag(top) = LUA_T_NUMBER; nvalue(top) = code.w;
769 incr_top;
757 } 770 }
758 break; 771 break;
759 772
@@ -761,7 +774,8 @@ static StkId lua_execute (Byte *pc, StkId base)
761 { 774 {
762 CodeFloat code; 775 CodeFloat code;
763 get_float(code,pc); 776 get_float(code,pc);
764 tag(top) = LUA_T_NUMBER; nvalue(top++) = code.f; 777 tag(top) = LUA_T_NUMBER; nvalue(top) = code.f;
778 incr_top;
765 } 779 }
766 break; 780 break;
767 781
@@ -769,7 +783,8 @@ static StkId lua_execute (Byte *pc, StkId base)
769 { 783 {
770 CodeWord code; 784 CodeWord code;
771 get_word(code,pc); 785 get_word(code,pc);
772 tag(top) = LUA_T_STRING; tsvalue(top++) = lua_constant[code.w]; 786 tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[code.w];
787 incr_top;
773 } 788 }
774 break; 789 break;
775 790
@@ -777,22 +792,25 @@ static StkId lua_execute (Byte *pc, StkId base)
777 { 792 {
778 CodeCode code; 793 CodeCode code;
779 get_code(code,pc); 794 get_code(code,pc);
780 tag(top) = LUA_T_FUNCTION; bvalue(top++) = code.b; 795 tag(top) = LUA_T_FUNCTION; bvalue(top) = code.b;
796 incr_top;
781 } 797 }
782 break; 798 break;
783 799
784 case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2: 800 case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2:
785 case PUSHLOCAL3: case PUSHLOCAL4: case PUSHLOCAL5: 801 case PUSHLOCAL3: case PUSHLOCAL4: case PUSHLOCAL5:
786 case PUSHLOCAL6: case PUSHLOCAL7: case PUSHLOCAL8: 802 case PUSHLOCAL6: case PUSHLOCAL7: case PUSHLOCAL8:
787 case PUSHLOCAL9: *top++ = *((stack+base) + (int)(opcode-PUSHLOCAL0)); break; 803 case PUSHLOCAL9:
804 *top = *((stack+base) + (int)(opcode-PUSHLOCAL0)); incr_top; break;
788 805
789 case PUSHLOCAL: *top++ = *((stack+base) + (*pc++)); break; 806 case PUSHLOCAL: *top = *((stack+base) + (*pc++)); incr_top; break;
790 807
791 case PUSHGLOBAL: 808 case PUSHGLOBAL:
792 { 809 {
793 CodeWord code; 810 CodeWord code;
794 get_word(code,pc); 811 get_word(code,pc);
795 *top++ = s_object(code.w); 812 *top = s_object(code.w);
813 incr_top;
796 } 814 }
797 break; 815 break;
798 816
@@ -805,9 +823,11 @@ static StkId lua_execute (Byte *pc, StkId base)
805 Object receiver = *(top-1); 823 Object receiver = *(top-1);
806 CodeWord code; 824 CodeWord code;
807 get_word(code,pc); 825 get_word(code,pc);
808 tag(top) = LUA_T_STRING; tsvalue(top++) = lua_constant[code.w]; 826 tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[code.w];
827 incr_top;
809 pushsubscript(); 828 pushsubscript();
810 *(top++) = receiver; 829 *top = receiver;
830 incr_top;
811 break; 831 break;
812 } 832 }
813 833
@@ -837,6 +857,7 @@ static StkId lua_execute (Byte *pc, StkId base)
837 int n = *pc++; 857 int n = *pc++;
838 if (tag(top-3-n) != LUA_T_ARRAY) 858 if (tag(top-3-n) != LUA_T_ARRAY)
839 { 859 {
860 lua_checkstack(top+2);
840 *(top+1) = *(top-1); 861 *(top+1) = *(top-1);
841 *(top) = *(top-2-n); 862 *(top) = *(top-2-n);
842 *(top-1) = *(top-3-n); 863 *(top-1) = *(top-3-n);
@@ -901,7 +922,7 @@ static StkId lua_execute (Byte *pc, StkId base)
901 get_word(size,pc); 922 get_word(size,pc);
902 avalue(top) = lua_createarray(size.w); 923 avalue(top) = lua_createarray(size.w);
903 tag(top) = LUA_T_ARRAY; 924 tag(top) = LUA_T_ARRAY;
904 top++; 925 incr_top;
905 } 926 }
906 break; 927 break;
907 928
@@ -1007,7 +1028,8 @@ static StkId lua_execute (Byte *pc, StkId base)
1007 case MINUSOP: 1028 case MINUSOP:
1008 if (tonumber(top-1)) 1029 if (tonumber(top-1))
1009 { 1030 {
1010 tag(top++) = LUA_T_NIL; 1031 tag(top) = LUA_T_NIL;
1032 incr_top;
1011 call_arith("unm"); 1033 call_arith("unm");
1012 } 1034 }
1013 else 1035 else