History

Lua 5.1-work3

Lua Team committed 21 years ago

Commit ec3e02b1 parent cdf85b646 files · +1107 −738Browse files

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

Unified · Split

src/lcode.c+50−27
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lcode.c,v 2.6 2004/08/24 20:09:11 roberto Exp $
2+** $Id: lcode.c,v 2.8 2004/12/03 20:35:33 roberto Exp $
33 ** Code generator for Lua
44 ** See Copyright Notice in lua.h
55 */
@@ -53,13 +53,13 @@ int luaK_jump (FuncState *fs) { · 47 unmodified lines
5353 }
5454
5555
56−static int luaK_condjump (FuncState *fs, OpCode op, int A, int B, int C) {
56+static int condjump (FuncState *fs, OpCode op, int A, int B, int C) {
5757 luaK_codeABC(fs, op, A, B, C);
5858 return luaK_jump(fs);
5959 }
6060
6161
62−static void luaK_fixjump (FuncState *fs, int pc, int dest) {
62+static void fixjump (FuncState *fs, int pc, int dest) {
6363 Instruction *jmp = &fs->f->code[pc];
6464 int offset = dest-(pc+1);
6565 lua_assert(dest != NO_JUMP);
@@ -79,7 +79,7 @@ int luaK_getlabel (FuncState *fs) { · 13 unmodified lines
7979 }
8080
8181
82−static int luaK_getjump (FuncState *fs, int pc) {
82+static int getjump (FuncState *fs, int pc) {
8383 int offset = GETARG_sBx(fs->f->code[pc]);
8484 if (offset == NO_JUMP) /* point to itself represents end of list */
8585 return NO_JUMP; /* end of list */
@@ -102,7 +102,7 @@ static Instruction *getjumpcontrol (FuncState *fs, int pc) { · 16 unmodified lines
102102 ** (or produce an inverted value)
103103 */
104104 static int need_value (FuncState *fs, int list, int cond) {
105− for (; list != NO_JUMP; list = luaK_getjump(fs, list)) {
105+ for (; list != NO_JUMP; list = getjump(fs, list)) {
106106 Instruction i = *getjumpcontrol(fs, list);
107107 if (GET_OPCODE(i) != OP_TEST || GETARG_C(i) != cond) return 1;
108108 }
@@ -116,25 +116,25 @@ static void patchtestreg (Instruction *i, int reg) { · 7 unmodified lines
116116 }
117117
118118
119−static void luaK_patchlistaux (FuncState *fs, int list,
119+static void patchlistaux (FuncState *fs, int list,
120120 int ttarget, int treg, int ftarget, int freg, int dtarget) {
121121 while (list != NO_JUMP) {
122− int next = luaK_getjump(fs, list);
122+ int next = getjump(fs, list);
123123 Instruction *i = getjumpcontrol(fs, list);
124124 if (GET_OPCODE(*i) != OP_TEST) {
125125 lua_assert(dtarget != NO_JUMP);
126− luaK_fixjump(fs, list, dtarget); /* jump to default target */
126+ fixjump(fs, list, dtarget); /* jump to default target */
127127 }
128128 else {
129129 if (GETARG_C(*i)) {
130130 lua_assert(ttarget != NO_JUMP);
131131 patchtestreg(i, treg);
132− luaK_fixjump(fs, list, ttarget);
132+ fixjump(fs, list, ttarget);
133133 }
134134 else {
135135 lua_assert(ftarget != NO_JUMP);
136136 patchtestreg(i, freg);
137− luaK_fixjump(fs, list, ftarget);
137+ fixjump(fs, list, ftarget);
138138 }
139139 }
140140 list = next;
@@ -142,8 +142,8 @@ static void luaK_patchlistaux (FuncState *fs, int list, · 1 unmodified line
142142 }
143143
144144
145−static void luaK_dischargejpc (FuncState *fs) {
146− luaK_patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc, NO_REG, fs->pc);
145+static void dischargejpc (FuncState *fs) {
146+ patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc, NO_REG, fs->pc);
147147 fs->jpc = NO_JUMP;
148148 }
149149
@@ -153,7 +153,7 @@ void luaK_patchlist (FuncState *fs, int list, int target) { · 3 unmodified lines
153153 luaK_patchtohere(fs, list);
154154 else {
155155 lua_assert(target < fs->pc);
156− luaK_patchlistaux(fs, list, target, NO_REG, target, NO_REG, target);
156+ patchlistaux(fs, list, target, NO_REG, target, NO_REG, target);
157157 }
158158 }
159159
@@ -171,9 +171,9 @@ void luaK_concat (FuncState *fs, int *l1, int l2) { · 11 unmodified lines
171171 else {
172172 int list = *l1;
173173 int next;
174− while ((next = luaK_getjump(fs, list)) != NO_JUMP) /* find last element */
174+ while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */
175175 list = next;
176− luaK_fixjump(fs, list, l2);
176+ fixjump(fs, list, l2);
177177 }
178178 }
179179
@@ -243,7 +243,14 @@ int luaK_numberK (FuncState *fs, lua_Number r) { · 63 unmodified lines
243243 }
244244
245245
246−static int nil_constant (FuncState *fs) {
246+static int boolK (FuncState *fs, int b) {
247+ TValue o;
248+ setbvalue(&o, b);
249+ return addk(fs, &o, &o);
250+}
251+
252+
253+static int nilK (FuncState *fs) {
247254 TValue k, v;
248255 setnilvalue(&v);
249256 /* cannot use nil as key; instead use table itself to represent nil */
@@ -358,7 +365,7 @@ static void discharge2anyreg (FuncState *fs, expdesc *e) { · 108 unmodified lines
358365 }
359366
360367
361−static void luaK_exp2reg (FuncState *fs, expdesc *e, int reg) {
368+static void exp2reg (FuncState *fs, expdesc *e, int reg) {
362369 discharge2reg(fs, e, reg);
363370 if (e->k == VJMP)
364371 luaK_concat(fs, &e->t, e->info); /* put this jump in `t' list */
@@ -375,8 +382,8 @@ static void luaK_exp2reg (FuncState *fs, expdesc *e, int reg) { · 10 unmodified lines
375382 luaK_patchtohere(fs, fj);
376383 }
377384 final = luaK_getlabel(fs);
378− luaK_patchlistaux(fs, e->f, p_f, NO_REG, final, reg, p_f);
379− luaK_patchlistaux(fs, e->t, final, reg, p_t, NO_REG, p_t);
385+ patchlistaux(fs, e->f, p_f, NO_REG, final, reg, p_f);
386+ patchlistaux(fs, e->t, final, reg, p_t, NO_REG, p_t);
380387 }
381388 e->f = e->t = NO_JUMP;
382389 e->info = reg;
@@ -388,7 +395,7 @@ void luaK_exp2nextreg (FuncState *fs, expdesc *e) { · 5 unmodified lines
388395 luaK_dischargevars(fs, e);
389396 freeexp(fs, e);
390397 luaK_reserveregs(fs, 1);
391− luaK_exp2reg(fs, e, fs->freereg - 1);
398+ exp2reg(fs, e, fs->freereg - 1);
392399 }
393400
394401
@@ -397,7 +404,7 @@ int luaK_exp2anyreg (FuncState *fs, expdesc *e) { · 2 unmodified lines
397404 if (e->k == VNONRELOC) {
398405 if (!hasjumps(e)) return e->info; /* exp is already in a register */
399406 if (e->info >= fs->nactvar) { /* reg. is not a local? */
400− luaK_exp2reg(fs, e, e->info); /* put value on it */
407+ exp2reg(fs, e, e->info); /* put value on it */
401408 return e->info;
402409 }
403410 }
@@ -417,9 +424,11 @@ void luaK_exp2val (FuncState *fs, expdesc *e) { · 13 unmodified lines
417424 int luaK_exp2RK (FuncState *fs, expdesc *e) {
418425 luaK_exp2val(fs, e);
419426 switch (e->k) {
427+ case VTRUE:
428+ case VFALSE:
420429 case VNIL: {
421430 if (fs->nk <= MAXINDEXRK) { /* constant fit in RK operand? */
422− e->info = nil_constant(fs);
431+ e->info = (e->k == VNIL) ? nilK(fs) : boolK(fs, (e->k == VTRUE));
423432 e->k = VK;
424433 return RKASK(e->info);
425434 }
@@ -441,7 +450,7 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { · 15 unmodified lines
441450 switch (var->k) {
442451 case VLOCAL: {
443452 freeexp(fs, ex);
444− luaK_exp2reg(fs, ex, var->info);
453+ exp2reg(fs, ex, var->info);
445454 return;
446455 }
447456 case VUPVAL: {
@@ -493,13 +502,13 @@ static int jumponcond (FuncState *fs, expdesc *e, int cond) { · 45 unmodified lines
493502 Instruction ie = getcode(fs, e);
494503 if (GET_OPCODE(ie) == OP_NOT) {
495504 fs->pc--; /* remove previous OP_NOT */
496− return luaK_condjump(fs, OP_TEST, NO_REG, GETARG_B(ie), !cond);
505+ return condjump(fs, OP_TEST, NO_REG, GETARG_B(ie), !cond);
497506 }
498507 /* else go through */
499508 }
500509 discharge2anyreg(fs, e);
501510 freeexp(fs, e);
502− return luaK_condjump(fs, OP_TEST, NO_REG, e->info, cond);
511+ return condjump(fs, OP_TEST, NO_REG, e->info, cond);
503512 }
504513
505514
@@ -651,7 +660,7 @@ static void codebinop (FuncState *fs, expdesc *res, BinOpr op, · 145 unmodified lines
651660 temp = o1; o1 = o2; o2 = temp; /* o1 <==> o2 */
652661 }
653662 else if (op == OPR_NE) cond = 0;
654− res->info = luaK_condjump(fs, ops[op - OPR_NE], cond, o1, o2);
663+ res->info = condjump(fs, ops[op - OPR_NE], cond, o1, o2);
655664 res->k = VJMP;
656665 }
657666 }
@@ -708,7 +717,7 @@ void luaK_fixline (FuncState *fs, int line) { · 50 unmodified lines
708717
709718 int luaK_code (FuncState *fs, Instruction i, int line) {
710719 Proto *f = fs->f;
711− luaK_dischargejpc(fs); /* `pc' will change */
720+ dischargejpc(fs); /* `pc' will change */
712721 /* put new instruction in code array */
713722 luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction,
714723 MAX_INT, "code size overflow");
@@ -735,3 +744,17 @@ int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) { · 20 unmodified lines
735744 return luaK_code(fs, CREATE_ABx(o, a, bc), fs->ls->lastline);
736745 }
737746
747+
748+void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {
749+ int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1;
750+ int b = (tostore == LUA_MULTRET) ? 0 : tostore;
751+ lua_assert(tostore != 0);
752+ if (c <= MAXARG_C)
753+ luaK_codeABC(fs, OP_SETLIST, base, b, c);
754+ else {
755+ luaK_codeABC(fs, OP_SETLIST, base, b, 0);
756+ luaK_code(fs, cast(Instruction, c), fs->ls->lastline);
757+ }
758+ fs->freereg = base + 1; /* free registers with list values */
759+}
760+