aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lcode.c14
-rw-r--r--lopcodes.c152
-rw-r--r--lopcodes.h20
-rw-r--r--lparser.c4
-rw-r--r--lvm.c8
5 files changed, 104 insertions, 94 deletions
diff --git a/lcode.c b/lcode.c
index 04b534e2..521f597a 100644
--- a/lcode.c
+++ b/lcode.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lcode.c,v 2.145 2017/12/15 18:53:48 roberto Exp roberto $ 2** $Id: lcode.c,v 2.146 2017/12/18 15:44:44 roberto Exp roberto $
3** Code generator for Lua 3** Code generator for Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -618,12 +618,11 @@ static void luaK_float (FuncState *fs, int reg, lua_Number f) {
618** or 'nresults' is LUA_MULTRET (as any expression can satisfy that). 618** or 'nresults' is LUA_MULTRET (as any expression can satisfy that).
619*/ 619*/
620void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) { 620void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {
621 if (e->k == VCALL) { /* expression is an open function call? */ 621 Instruction *pc = &getinstruction(fs, e);
622 SETARG_C(getinstruction(fs, e), nresults + 1); 622 if (e->k == VCALL) /* expression is an open function call? */
623 } 623 SETARG_C(*pc, nresults + 1);
624 else if (e->k == VVARARG) { 624 else if (e->k == VVARARG) {
625 Instruction *pc = &getinstruction(fs, e); 625 SETARG_C(*pc, nresults + 1);
626 SETARG_B(*pc, nresults + 1);
627 SETARG_A(*pc, fs->freereg); 626 SETARG_A(*pc, fs->freereg);
628 luaK_reserveregs(fs, 1); 627 luaK_reserveregs(fs, 1);
629 } 628 }
@@ -649,7 +648,7 @@ void luaK_setoneret (FuncState *fs, expdesc *e) {
649 e->u.info = GETARG_A(getinstruction(fs, e)); 648 e->u.info = GETARG_A(getinstruction(fs, e));
650 } 649 }
651 else if (e->k == VVARARG) { 650 else if (e->k == VVARARG) {
652 SETARG_B(getinstruction(fs, e), 2); 651 SETARG_C(getinstruction(fs, e), 2);
653 e->k = VRELOC; /* can relocate its simple result */ 652 e->k = VRELOC; /* can relocate its simple result */
654 } 653 }
655} 654}
@@ -1623,6 +1622,7 @@ void luaK_finish (FuncState *fs) {
1623 Proto *p = fs->f; 1622 Proto *p = fs->f;
1624 for (i = 0; i < fs->pc; i++) { 1623 for (i = 0; i < fs->pc; i++) {
1625 Instruction *pc = &p->code[i]; 1624 Instruction *pc = &p->code[i];
1625 lua_assert(isOT(*pc) == isIT(*(pc + 1)));
1626 switch (GET_OPCODE(*pc)) { 1626 switch (GET_OPCODE(*pc)) {
1627 case OP_RETURN: case OP_RETURN0: case OP_RETURN1: 1627 case OP_RETURN: case OP_RETURN0: case OP_RETURN1:
1628 case OP_TAILCALL: { 1628 case OP_TAILCALL: {
diff --git a/lopcodes.c b/lopcodes.c
index f5896ba0..a5867d23 100644
--- a/lopcodes.c
+++ b/lopcodes.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lopcodes.c,v 1.73 2017/12/13 18:32:09 roberto Exp roberto $ 2** $Id: lopcodes.c,v 1.74 2017/12/18 17:49:31 roberto Exp $
3** Opcodes for Lua virtual machine 3** Opcodes for Lua virtual machine
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -97,80 +97,80 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = {
97 97
98 98
99LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { 99LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = {
100/* T A mode opcode */ 100/* OT IT T A mode opcode */
101 opmode(0, 1, iABC) /* OP_MOVE */ 101 opmode(0, 0, 0, 1, iABC) /* OP_MOVE */
102 ,opmode(0, 1, iAsBx) /* OP_LOADI */ 102 ,opmode(0, 0, 0, 1, iAsBx) /* OP_LOADI */
103 ,opmode(0, 1, iAsBx) /* OP_LOADF */ 103 ,opmode(0, 0, 0, 1, iAsBx) /* OP_LOADF */
104 ,opmode(0, 1, iABx) /* OP_LOADK */ 104 ,opmode(0, 0, 0, 1, iABx) /* OP_LOADK */
105 ,opmode(0, 1, iABx) /* OP_LOADKX */ 105 ,opmode(0, 0, 0, 1, iABx) /* OP_LOADKX */
106 ,opmode(0, 1, iABC) /* OP_LOADBOOL */ 106 ,opmode(0, 0, 0, 1, iABC) /* OP_LOADBOOL */
107 ,opmode(0, 1, iABC) /* OP_LOADNIL */ 107 ,opmode(0, 0, 0, 1, iABC) /* OP_LOADNIL */
108 ,opmode(0, 1, iABC) /* OP_GETUPVAL */ 108 ,opmode(0, 0, 0, 1, iABC) /* OP_GETUPVAL */
109 ,opmode(0, 0, iABC) /* OP_SETUPVAL */ 109 ,opmode(0, 0, 0, 0, iABC) /* OP_SETUPVAL */
110 ,opmode(0, 1, iABC) /* OP_GETTABUP */ 110 ,opmode(0, 0, 0, 1, iABC) /* OP_GETTABUP */
111 ,opmode(0, 1, iABC) /* OP_GETTABLE */ 111 ,opmode(0, 0, 0, 1, iABC) /* OP_GETTABLE */
112 ,opmode(0, 1, iABC) /* OP_GETI */ 112 ,opmode(0, 0, 0, 1, iABC) /* OP_GETI */
113 ,opmode(0, 1, iABC) /* OP_GETFIELD */ 113 ,opmode(0, 0, 0, 1, iABC) /* OP_GETFIELD */
114 ,opmode(0, 0, iABC) /* OP_SETTABUP */ 114 ,opmode(0, 0, 0, 0, iABC) /* OP_SETTABUP */
115 ,opmode(0, 0, iABC) /* OP_SETTABLE */ 115 ,opmode(0, 0, 0, 0, iABC) /* OP_SETTABLE */
116 ,opmode(0, 0, iABC) /* OP_SETI */ 116 ,opmode(0, 0, 0, 0, iABC) /* OP_SETI */
117 ,opmode(0, 0, iABC) /* OP_SETFIELD */ 117 ,opmode(0, 0, 0, 0, iABC) /* OP_SETFIELD */
118 ,opmode(0, 1, iABC) /* OP_NEWTABLE */ 118 ,opmode(0, 0, 0, 1, iABC) /* OP_NEWTABLE */
119 ,opmode(0, 1, iABC) /* OP_SELF */ 119 ,opmode(0, 0, 0, 1, iABC) /* OP_SELF */
120 ,opmode(0, 1, iABC) /* OP_ADDI */ 120 ,opmode(0, 0, 0, 1, iABC) /* OP_ADDI */
121 ,opmode(0, 1, iABC) /* OP_SUBI */ 121 ,opmode(0, 0, 0, 1, iABC) /* OP_SUBI */
122 ,opmode(0, 1, iABC) /* OP_MULI */ 122 ,opmode(0, 0, 0, 1, iABC) /* OP_MULI */
123 ,opmode(0, 1, iABC) /* OP_MODI */ 123 ,opmode(0, 0, 0, 1, iABC) /* OP_MODI */
124 ,opmode(0, 1, iABC) /* OP_POWI */ 124 ,opmode(0, 0, 0, 1, iABC) /* OP_POWI */
125 ,opmode(0, 1, iABC) /* OP_DIVI */ 125 ,opmode(0, 0, 0, 1, iABC) /* OP_DIVI */
126 ,opmode(0, 1, iABC) /* OP_IDIVI */ 126 ,opmode(0, 0, 0, 1, iABC) /* OP_IDIVI */
127 ,opmode(0, 1, iABC) /* OP_BANDK */ 127 ,opmode(0, 0, 0, 1, iABC) /* OP_BANDK */
128 ,opmode(0, 1, iABC) /* OP_BORK */ 128 ,opmode(0, 0, 0, 1, iABC) /* OP_BORK */
129 ,opmode(0, 1, iABC) /* OP_BXORK */ 129 ,opmode(0, 0, 0, 1, iABC) /* OP_BXORK */
130 ,opmode(0, 1, iABC) /* OP_SHRI */ 130 ,opmode(0, 0, 0, 1, iABC) /* OP_SHRI */
131 ,opmode(0, 1, iABC) /* OP_SHLI */ 131 ,opmode(0, 0, 0, 1, iABC) /* OP_SHLI */
132 ,opmode(0, 1, iABC) /* OP_ADD */ 132 ,opmode(0, 0, 0, 1, iABC) /* OP_ADD */
133 ,opmode(0, 1, iABC) /* OP_SUB */ 133 ,opmode(0, 0, 0, 1, iABC) /* OP_SUB */
134 ,opmode(0, 1, iABC) /* OP_MUL */ 134 ,opmode(0, 0, 0, 1, iABC) /* OP_MUL */
135 ,opmode(0, 1, iABC) /* OP_MOD */ 135 ,opmode(0, 0, 0, 1, iABC) /* OP_MOD */
136 ,opmode(0, 1, iABC) /* OP_POW */ 136 ,opmode(0, 0, 0, 1, iABC) /* OP_POW */
137 ,opmode(0, 1, iABC) /* OP_DIV */ 137 ,opmode(0, 0, 0, 1, iABC) /* OP_DIV */
138 ,opmode(0, 1, iABC) /* OP_IDIV */ 138 ,opmode(0, 0, 0, 1, iABC) /* OP_IDIV */
139 ,opmode(0, 1, iABC) /* OP_BAND */ 139 ,opmode(0, 0, 0, 1, iABC) /* OP_BAND */
140 ,opmode(0, 1, iABC) /* OP_BOR */ 140 ,opmode(0, 0, 0, 1, iABC) /* OP_BOR */
141 ,opmode(0, 1, iABC) /* OP_BXOR */ 141 ,opmode(0, 0, 0, 1, iABC) /* OP_BXOR */
142 ,opmode(0, 1, iABC) /* OP_SHL */ 142 ,opmode(0, 0, 0, 1, iABC) /* OP_SHL */
143 ,opmode(0, 1, iABC) /* OP_SHR */ 143 ,opmode(0, 0, 0, 1, iABC) /* OP_SHR */
144 ,opmode(0, 1, iABC) /* OP_UNM */ 144 ,opmode(0, 0, 0, 1, iABC) /* OP_UNM */
145 ,opmode(0, 1, iABC) /* OP_BNOT */ 145 ,opmode(0, 0, 0, 1, iABC) /* OP_BNOT */
146 ,opmode(0, 1, iABC) /* OP_NOT */ 146 ,opmode(0, 0, 0, 1, iABC) /* OP_NOT */
147 ,opmode(0, 1, iABC) /* OP_LEN */ 147 ,opmode(0, 0, 0, 1, iABC) /* OP_LEN */
148 ,opmode(0, 1, iABC) /* OP_CONCAT */ 148 ,opmode(0, 0, 0, 1, iABC) /* OP_CONCAT */
149 ,opmode(0, 0, iABC) /* OP_CLOSE */ 149 ,opmode(0, 0, 0, 0, iABC) /* OP_CLOSE */
150 ,opmode(0, 0, isJ) /* OP_JMP */ 150 ,opmode(0, 0, 0, 0, isJ) /* OP_JMP */
151 ,opmode(1, 0, iABC) /* OP_EQ */ 151 ,opmode(0, 0, 1, 0, iABC) /* OP_EQ */
152 ,opmode(1, 0, iABC) /* OP_LT */ 152 ,opmode(0, 0, 1, 0, iABC) /* OP_LT */
153 ,opmode(1, 0, iABC) /* OP_LE */ 153 ,opmode(0, 0, 1, 0, iABC) /* OP_LE */
154 ,opmode(1, 0, iABC) /* OP_EQK */ 154 ,opmode(0, 0, 1, 0, iABC) /* OP_EQK */
155 ,opmode(1, 0, iABC) /* OP_EQI */ 155 ,opmode(0, 0, 1, 0, iABC) /* OP_EQI */
156 ,opmode(1, 0, iABC) /* OP_LTI */ 156 ,opmode(0, 0, 1, 0, iABC) /* OP_LTI */
157 ,opmode(1, 0, iABC) /* OP_LEI */ 157 ,opmode(0, 0, 1, 0, iABC) /* OP_LEI */
158 ,opmode(1, 0, iABC) /* OP_TEST */ 158 ,opmode(0, 0, 1, 0, iABC) /* OP_TEST */
159 ,opmode(1, 1, iABC) /* OP_TESTSET */ 159 ,opmode(0, 0, 1, 1, iABC) /* OP_TESTSET */
160 ,opmode(0, 1, iABC) /* OP_CALL */ 160 ,opmode(1, 1, 0, 1, iABC) /* OP_CALL */
161 ,opmode(0, 1, iABC) /* OP_TAILCALL */ 161 ,opmode(1, 1, 0, 1, iABC) /* OP_TAILCALL */
162 ,opmode(0, 0, iABC) /* OP_RETURN */ 162 ,opmode(0, 1, 0, 0, iABC) /* OP_RETURN */
163 ,opmode(0, 0, iABC) /* OP_RETURN0 */ 163 ,opmode(0, 0, 0, 0, iABC) /* OP_RETURN0 */
164 ,opmode(0, 0, iABC) /* OP_RETURN1 */ 164 ,opmode(0, 0, 0, 0, iABC) /* OP_RETURN1 */
165 ,opmode(0, 1, iABx) /* OP_FORLOOP1 */ 165 ,opmode(0, 0, 0, 1, iABx) /* OP_FORLOOP1 */
166 ,opmode(0, 1, iABx) /* OP_FORPREP1 */ 166 ,opmode(0, 0, 0, 1, iABx) /* OP_FORPREP1 */
167 ,opmode(0, 1, iABx) /* OP_FORLOOP */ 167 ,opmode(0, 0, 0, 1, iABx) /* OP_FORLOOP */
168 ,opmode(0, 1, iABx) /* OP_FORPREP */ 168 ,opmode(0, 0, 0, 1, iABx) /* OP_FORPREP */
169 ,opmode(0, 0, iABC) /* OP_TFORCALL */ 169 ,opmode(0, 0, 0, 0, iABC) /* OP_TFORCALL */
170 ,opmode(0, 1, iABx) /* OP_TFORLOOP */ 170 ,opmode(0, 0, 0, 1, iABx) /* OP_TFORLOOP */
171 ,opmode(0, 0, iABC) /* OP_SETLIST */ 171 ,opmode(0, 1, 0, 0, iABC) /* OP_SETLIST */
172 ,opmode(0, 1, iABx) /* OP_CLOSURE */ 172 ,opmode(0, 0, 0, 1, iABx) /* OP_CLOSURE */
173 ,opmode(0, 1, iABC) /* OP_VARARG */ 173 ,opmode(1, 0, 0, 1, iABC) /* OP_VARARG */
174 ,opmode(0, 0, iAx) /* OP_EXTRAARG */ 174 ,opmode(0, 0, 0, 0, iAx) /* OP_EXTRAARG */
175}; 175};
176 176
diff --git a/lopcodes.h b/lopcodes.h
index ec880703..eae0dfaf 100644
--- a/lopcodes.h
+++ b/lopcodes.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lopcodes.h,v 1.179 2017/12/15 18:53:48 roberto Exp roberto $ 2** $Id: lopcodes.h,v 1.180 2017/12/18 17:49:31 roberto Exp roberto $
3** Opcodes for Lua virtual machine 3** Opcodes for Lua virtual machine
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -282,7 +282,7 @@ OP_SETLIST,/* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */
282 282
283OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx]) */ 283OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx]) */
284 284
285OP_VARARG,/* A B C R(A), R(A+1), ..., R(A+B-2) = vararg(C) */ 285OP_VARARG,/* A B C R(A), R(A+1), ..., R(A+C-2) = vararg(B) */
286 286
287OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ 287OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
288} OpCode; 288} OpCode;
@@ -298,8 +298,8 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
298 set to last_result+1, so next open instruction (OP_CALL, OP_RETURN, 298 set to last_result+1, so next open instruction (OP_CALL, OP_RETURN,
299 OP_SETLIST) may use 'top'. 299 OP_SETLIST) may use 'top'.
300 300
301 (*) In OP_VARARG, if (B == 0) then use actual number of varargs and 301 (*) In OP_VARARG, if (C == 0) then use actual number of varargs and
302 set top (like in OP_CALL with C == 0). C is the vararg parameter. 302 set top (like in OP_CALL with C == 0). B is the vararg parameter.
303 303
304 (*) In OP_RETURN, if (B == 0) then return up to 'top'. 304 (*) In OP_RETURN, if (B == 0) then return up to 'top'.
305 305
@@ -330,6 +330,8 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
330** bits 0-2: op mode 330** bits 0-2: op mode
331** bit 3: instruction set register A 331** bit 3: instruction set register A
332** bit 4: operator is a test (next instruction must be a jump) 332** bit 4: operator is a test (next instruction must be a jump)
333** bit 5: instruction sets 'L->top' for next instruction (when C == 0)
334** bit 6: instruction uses 'L->top' set by previous instruction (when B == 0)
333*/ 335*/
334 336
335LUAI_DDEC const lu_byte luaP_opmodes[NUM_OPCODES]; 337LUAI_DDEC const lu_byte luaP_opmodes[NUM_OPCODES];
@@ -337,8 +339,16 @@ LUAI_DDEC const lu_byte luaP_opmodes[NUM_OPCODES];
337#define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 7)) 339#define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 7))
338#define testAMode(m) (luaP_opmodes[m] & (1 << 3)) 340#define testAMode(m) (luaP_opmodes[m] & (1 << 3))
339#define testTMode(m) (luaP_opmodes[m] & (1 << 4)) 341#define testTMode(m) (luaP_opmodes[m] & (1 << 4))
342#define testOTMode(m) (luaP_opmodes[m] & (1 << 5))
343#define testITMode(m) (luaP_opmodes[m] & (1 << 6))
340 344
341#define opmode(t,a,m) (((t)<<4) | ((a)<<3) | (m)) 345/* "out top" (set top for next instruction) */
346#define isOT(i) (testOTMode(GET_OPCODE(i)) && GETARG_C(i) == 0)
347
348/* "in top" (uses top from previous instruction) */
349#define isIT(i) (testITMode(GET_OPCODE(i)) && GETARG_B(i) == 0)
350
351#define opmode(ot,it,t,a,m) (((ot)<<5) | ((it)<<6) | ((t)<<4) | ((a)<<3) | (m))
342 352
343 353
344LUAI_DDEC const char *const luaP_opnames[NUM_OPCODES+1]; /* opcode names */ 354LUAI_DDEC const char *const luaP_opnames[NUM_OPCODES+1]; /* opcode names */
diff --git a/lparser.c b/lparser.c
index 6317a788..e99b33ff 100644
--- a/lparser.c
+++ b/lparser.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lparser.c,v 2.173 2017/12/18 12:33:54 roberto Exp roberto $ 2** $Id: lparser.c,v 2.174 2017/12/18 17:49:31 roberto Exp roberto $
3** Lua Parser 3** Lua Parser
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -977,7 +977,7 @@ static void simpleexp (LexState *ls, expdesc *v) {
977 int lastparam = fs->f->numparams; 977 int lastparam = fs->f->numparams;
978 check_condition(ls, fs->f->is_vararg, 978 check_condition(ls, fs->f->is_vararg,
979 "cannot use '...' outside a vararg function"); 979 "cannot use '...' outside a vararg function");
980 init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, lastparam)); 980 init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, lastparam, 1));
981 break; 981 break;
982 } 982 }
983 case '{': { /* constructor */ 983 case '{': { /* constructor */
diff --git a/lvm.c b/lvm.c
index 8039770e..89646d24 100644
--- a/lvm.c
+++ b/lvm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.c,v 2.327 2017/12/19 16:18:04 roberto Exp roberto $ 2** $Id: lvm.c,v 2.328 2017/12/20 14:58:05 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*/
@@ -1688,9 +1688,9 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1688 vmbreak; 1688 vmbreak;
1689 } 1689 }
1690 vmcase(OP_VARARG) { 1690 vmcase(OP_VARARG) {
1691 int b = GETARG_B(i) - 1; /* required results */ 1691 int n = GETARG_C(i) - 1; /* required results */
1692 TValue *vtab = vRC(i); /* vararg table */ 1692 TValue *vtab = vRB(i); /* vararg table */
1693 Protect(luaT_getvarargs(L, vtab, ra, b)); 1693 Protect(luaT_getvarargs(L, vtab, ra, n));
1694 vmbreak; 1694 vmbreak;
1695 } 1695 }
1696 vmcase(OP_EXTRAARG) { 1696 vmcase(OP_EXTRAARG) {