History

Lua 5.2.0-beta-rc1

Lua Team committed 15 years ago

Commit 30837cc1 parent 9bce00553 files · +2418 −1456Browse files

Showing the changes to src/lcode.c (1 of 53 files). Show all

Unified · Split

src/lcode.c+42−38
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lcode.c,v 2.49 2010/07/07 16:27:29 roberto Exp $
2+** $Id: lcode.c,v 2.56 2011/05/31 18:27:56 roberto Exp $
33 ** Code generator for Lua
44 ** See Copyright Notice in lua.h
55 */
@@ -23,6 +23,7 @@ · 17 unmodified lines
2323 #include "lparser.h"
2424 #include "lstring.h"
2525 #include "ltable.h"
26+#include "lvm.h"
2627
2728
2829 #define hasjumps(e) ((e)->t != (e)->f)
@@ -35,19 +36,23 @@ static int isnumeral(expdesc *e) { · 6 unmodified lines
3536
3637 void luaK_nil (FuncState *fs, int from, int n) {
3738 Instruction *previous;
39+ int l = from + n - 1; /* last register to set nil */
3840 if (fs->pc > fs->lasttarget) { /* no jumps to current position? */
3941 previous = &fs->f->code[fs->pc-1];
4042 if (GET_OPCODE(*previous) == OP_LOADNIL) {
4143 int pfrom = GETARG_A(*previous);
42− int pto = GETARG_B(*previous);
43− if (pfrom <= from && from <= pto+1) { /* can connect both? */
44− if (from+n-1 > pto)
45− SETARG_B(*previous, from+n-1);
44+ int pl = pfrom + GETARG_B(*previous);
45+ if ((pfrom <= from && from <= pl + 1) ||
46+ (from <= pfrom && pfrom <= l + 1)) { /* can connect both? */
47+ if (pfrom < from) from = pfrom; /* from = min(from, pfrom) */
48+ if (pl > l) l = pl; /* l = max(l, pl) */
49+ SETARG_A(*previous, from);
50+ SETARG_B(*previous, l - from);
4651 return;
4752 }
48− }
53+ } /* else go through */
4954 }
50− luaK_codeABC(fs, OP_LOADNIL, from, from+n-1, 0); /* else no optimization */
55+ luaK_codeABC(fs, OP_LOADNIL, from, n - 1, 0); /* else no optimization */
5156 }
5257
5358
@@ -171,6 +176,19 @@ void luaK_patchlist (FuncState *fs, int list, int target) { · 117 unmodified lines
171176 }
172177
173178
179+LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level) {
180+ level++; /* argument is +1 to reserve 0 as non-op */
181+ while (list != NO_JUMP) {
182+ int next = getjump(fs, list);
183+ lua_assert(GET_OPCODE(fs->f->code[list]) == OP_JMP &&
184+ (GETARG_A(fs->f->code[list]) == 0 ||
185+ GETARG_A(fs->f->code[list]) >= level));
186+ SETARG_A(fs->f->code[list], level);
187+ list = next;
188+ }
189+}
190+
191+
174192 void luaK_patchtohere (FuncState *fs, int list) {
175193 luaK_getlabel(fs);
176194 luaK_concat(fs, &fs->jpc, list);
@@ -229,11 +247,11 @@ static int codeextraarg (FuncState *fs, int a) { · 52 unmodified lines
229247 }
230248
231249
232−int luaK_codeABxX (FuncState *fs, OpCode o, int reg, int k) {
233− if (k < MAXARG_Bx)
234− return luaK_codeABx(fs, o, reg, k + 1);
250+int luaK_codek (FuncState *fs, int reg, int k) {
251+ if (k <= MAXARG_Bx)
252+ return luaK_codeABx(fs, OP_LOADK, reg, k);
235253 else {
236− int p = luaK_codeABx(fs, o, reg, 0);
254+ int p = luaK_codeABx(fs, OP_LOADKX, reg, 0);
237255 codeextraarg(fs, k);
238256 return p;
239257 }
@@ -278,7 +296,7 @@ static int addk (FuncState *fs, TValue *key, TValue *v) { · 38 unmodified lines
278296 if (ttisnumber(idx)) {
279297 lua_Number n = nvalue(idx);
280298 lua_number2int(k, n);
281− if (luaO_rawequalObj(&f->k[k], v))
299+ if (luaV_rawequalobj(&f->k[k], v))
282300 return k;
283301 /* else may be a collision (e.g., between 0.0 and "\0\0\0\0\0\0\0\0");
284302 go through and create a new entry for this value */
@@ -564,15 +582,15 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { · 279 unmodified lines
564582
565583
566584 void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
567− int func;
585+ int ereg;
568586 luaK_exp2anyreg(fs, e);
587+ ereg = e->u.info; /* register where 'e' was placed */
569588 freeexp(fs, e);
570− func = fs->freereg;
571− luaK_codeABC(fs, OP_SELF, func, e->u.info, luaK_exp2RK(fs, key));
572− freeexp(fs, key);
573− luaK_reserveregs(fs, 2);
574− e->u.info = func;
589+ e->u.info = fs->freereg; /* base register for op_self */
575590 e->k = VNONRELOC;
591+ luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */
592+ luaK_codeABC(fs, OP_SELF, e->u.info, ereg, luaK_exp2RK(fs, key));
593+ freeexp(fs, key);
576594 }
577595
578596
@@ -603,21 +621,14 @@ void luaK_goiftrue (FuncState *fs, expdesc *e) { · 24 unmodified lines
603621 int pc; /* pc of last jump */
604622 luaK_dischargevars(fs, e);
605623 switch (e->k) {
606− case VK: case VKNUM: case VTRUE: {
607− pc = NO_JUMP; /* always true; do nothing */
608− break;
609− }
610624 case VJMP: {
611625 invertjump(fs, e);
612626 pc = e->u.info;
613627 break;
614628 }
615− case VFALSE: {
616− if (!hasjumps(e)) {
617− pc = luaK_jump(fs); /* always jump */
618− break;
619− }
620− /* else go through */
629+ case VK: case VKNUM: case VTRUE: {
630+ pc = NO_JUMP; /* always true; do nothing */
631+ break;
621632 }
622633 default: {
623634 pc = jumponcond(fs, e, 0);
@@ -634,20 +645,13 @@ static void luaK_goiffalse (FuncState *fs, expdesc *e) { · 10 unmodified lines
634645 int pc; /* pc of last jump */
635646 luaK_dischargevars(fs, e);
636647 switch (e->k) {
637− case VNIL: case VFALSE: {
638− pc = NO_JUMP; /* always false; do nothing */
639− break;
640− }
641648 case VJMP: {
642649 pc = e->u.info;
643650 break;
644651 }
645− case VTRUE: {
646− if (!hasjumps(e)) {
647− pc = luaK_jump(fs); /* always jump */
648− break;
649− }
650− /* else go through */
652+ case VNIL: case VFALSE: {
653+ pc = NO_JUMP; /* always false; do nothing */
654+ break;
651655 }
652656 default: {
653657 pc = jumponcond(fs, e, 1);