aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2015-12-18 11:53:36 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2015-12-18 11:53:36 -0200
commit384d1b47b0542cfb5df38eaac18a2f87726a3ef3 (patch)
tree28413a4da45203076a818460550532a9f4b48147
parent19770b03a9faa8e2e0bf573a9417db5bfba1eac9 (diff)
downloadlua-384d1b47b0542cfb5df38eaac18a2f87726a3ef3.tar.gz
lua-384d1b47b0542cfb5df38eaac18a2f87726a3ef3.tar.bz2
lua-384d1b47b0542cfb5df38eaac18a2f87726a3ef3.zip
comments (*lots* of them) + asserts
-rw-r--r--lcode.c408
1 files changed, 324 insertions, 84 deletions
diff --git a/lcode.c b/lcode.c
index 13d12a13..a15e8dcc 100644
--- a/lcode.c
+++ b/lcode.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lcode.c,v 2.104 2015/12/17 14:52:53 roberto Exp roberto $ 2** $Id: lcode.c,v 2.103 2015/11/19 19:16:22 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*/
@@ -36,6 +36,10 @@
36#define hasjumps(e) ((e)->t != (e)->f) 36#define hasjumps(e) ((e)->t != (e)->f)
37 37
38 38
39/*
40** If expression is a numeric constant, fills 'v' with its value
41** and returns 1. Otherwise, returns 0.
42*/
39static int tonumeral(expdesc *e, TValue *v) { 43static int tonumeral(expdesc *e, TValue *v) {
40 if (hasjumps(e)) 44 if (hasjumps(e))
41 return 0; /* not a numeral */ 45 return 0; /* not a numeral */
@@ -51,13 +55,19 @@ static int tonumeral(expdesc *e, TValue *v) {
51} 55}
52 56
53 57
58/*
59** Create a OP_LOADNIL instruction, but try to optimize: if the previous
60** instruction is also OP_LOADNIL and ranges are compatible, adjust
61** range of previous instruction instead of emitting a new one. (For
62** instance, 'local a; local b' will generate a single opcode.)
63*/
54void luaK_nil (FuncState *fs, int from, int n) { 64void luaK_nil (FuncState *fs, int from, int n) {
55 Instruction *previous; 65 Instruction *previous;
56 int l = from + n - 1; /* last register to set nil */ 66 int l = from + n - 1; /* last register to set nil */
57 if (fs->pc > fs->lasttarget) { /* no jumps to current position? */ 67 if (fs->pc > fs->lasttarget) { /* no jumps to current position? */
58 previous = &fs->f->code[fs->pc-1]; 68 previous = &fs->f->code[fs->pc-1];
59 if (GET_OPCODE(*previous) == OP_LOADNIL) { 69 if (GET_OPCODE(*previous) == OP_LOADNIL) { /* previous is LOADNIL? */
60 int pfrom = GETARG_A(*previous); 70 int pfrom = GETARG_A(*previous); /* get previous range */
61 int pl = pfrom + GETARG_B(*previous); 71 int pl = pfrom + GETARG_B(*previous);
62 if ((pfrom <= from && from <= pl + 1) || 72 if ((pfrom <= from && from <= pl + 1) ||
63 (from <= pfrom && pfrom <= l + 1)) { /* can connect both? */ 73 (from <= pfrom && pfrom <= l + 1)) { /* can connect both? */
@@ -73,6 +83,10 @@ void luaK_nil (FuncState *fs, int from, int n) {
73} 83}
74 84
75 85
86/*
87** Gets the destination address of a jump instruction. Used to traverse
88** a list of jumps.
89*/
76static int getjump (FuncState *fs, int pc) { 90static int getjump (FuncState *fs, int pc) {
77 int offset = GETARG_sBx(fs->f->code[pc]); 91 int offset = GETARG_sBx(fs->f->code[pc]);
78 if (offset == NO_JUMP) /* point to itself represents end of list */ 92 if (offset == NO_JUMP) /* point to itself represents end of list */
@@ -82,9 +96,13 @@ static int getjump (FuncState *fs, int pc) {
82} 96}
83 97
84 98
99/*
100** Fix jump instruction at position 'pc' to jump to 'dest'.
101** (Jump addresses are relative in Lua)
102*/
85static void fixjump (FuncState *fs, int pc, int dest) { 103static void fixjump (FuncState *fs, int pc, int dest) {
86 Instruction *jmp = &fs->f->code[pc]; 104 Instruction *jmp = &fs->f->code[pc];
87 int offset = dest-(pc+1); 105 int offset = dest - (pc + 1);
88 lua_assert(dest != NO_JUMP); 106 lua_assert(dest != NO_JUMP);
89 if (abs(offset) > MAXARG_sBx) 107 if (abs(offset) > MAXARG_sBx)
90 luaX_syntaxerror(fs->ls, "control structure too long"); 108 luaX_syntaxerror(fs->ls, "control structure too long");
@@ -92,35 +110,51 @@ static void fixjump (FuncState *fs, int pc, int dest) {
92} 110}
93 111
94 112
113/*
114** Concatenate jump-list 'l2' into jump-list 'l1'
115*/
95void luaK_concat (FuncState *fs, int *l1, int l2) { 116void luaK_concat (FuncState *fs, int *l1, int l2) {
96 if (l2 == NO_JUMP) return; 117 if (l2 == NO_JUMP) return; /* nothing to concatenate? */
97 else if (*l1 == NO_JUMP) 118 else if (*l1 == NO_JUMP) /* no original list? */
98 *l1 = l2; 119 *l1 = l2; /* 'l1' points to 'l2' */
99 else { 120 else {
100 int list = *l1; 121 int list = *l1;
101 int next; 122 int next;
102 while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */ 123 while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */
103 list = next; 124 list = next;
104 fixjump(fs, list, l2); 125 fixjump(fs, list, l2); /* last element links to 'l2' */
105 } 126 }
106} 127}
107 128
108 129
130/*
131** Create a jump instruction and return its position, so its destination
132** can be fixed later (with 'fixjump'). If there are jumps to
133** this position (kept in 'jpc'), link them all together so that
134** 'patchlistaux' will fix all them directly to the final destination.
135*/
109int luaK_jump (FuncState *fs) { 136int luaK_jump (FuncState *fs) {
110 int jpc = fs->jpc; /* save list of jumps to here */ 137 int jpc = fs->jpc; /* save list of jumps to here */
111 int j; 138 int j;
112 fs->jpc = NO_JUMP; 139 fs->jpc = NO_JUMP; /* no more jumps to here */
113 j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP); 140 j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP);
114 luaK_concat(fs, &j, jpc); /* keep them on hold */ 141 luaK_concat(fs, &j, jpc); /* keep them on hold */
115 return j; 142 return j;
116} 143}
117 144
118 145
146/*
147** Code a 'return' instruction
148*/
119void luaK_ret (FuncState *fs, int first, int nret) { 149void luaK_ret (FuncState *fs, int first, int nret) {
120 luaK_codeABC(fs, OP_RETURN, first, nret+1, 0); 150 luaK_codeABC(fs, OP_RETURN, first, nret+1, 0);
121} 151}
122 152
123 153
154/*
155** Code a "conditional jump", that is, a test or comparison opcode
156** followed by a jump. Return jump position.
157*/
124static int condjump (FuncState *fs, OpCode op, int A, int B, int C) { 158static int condjump (FuncState *fs, OpCode op, int A, int B, int C) {
125 luaK_codeABC(fs, op, A, B, C); 159 luaK_codeABC(fs, op, A, B, C);
126 return luaK_jump(fs); 160 return luaK_jump(fs);
@@ -137,6 +171,11 @@ int luaK_getlabel (FuncState *fs) {
137} 171}
138 172
139 173
174/*
175** Returns the position of the instruction "controlling" a given
176** jump (that is, its condition), or the jump itself if it is
177** unconditional.
178*/
140static Instruction *getjumpcontrol (FuncState *fs, int pc) { 179static Instruction *getjumpcontrol (FuncState *fs, int pc) {
141 Instruction *pi = &fs->f->code[pc]; 180 Instruction *pi = &fs->f->code[pc];
142 if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1)))) 181 if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1))))
@@ -146,25 +185,42 @@ static Instruction *getjumpcontrol (FuncState *fs, int pc) {
146} 185}
147 186
148 187
188/*
189** Patch destination register for a TESTSET instruction.
190** If instruction in position 'node' is not a TESTSET, return 0 ("fails").
191** Otherwise, if 'reg' is not 'NO_REG', set it as the destination
192** register. Otherwise, change instruction to a simple 'TEST' (produces
193** no register value)
194*/
149static int patchtestreg (FuncState *fs, int node, int reg) { 195static int patchtestreg (FuncState *fs, int node, int reg) {
150 Instruction *i = getjumpcontrol(fs, node); 196 Instruction *i = getjumpcontrol(fs, node);
151 if (GET_OPCODE(*i) != OP_TESTSET) 197 if (GET_OPCODE(*i) != OP_TESTSET)
152 return 0; /* cannot patch other instructions */ 198 return 0; /* cannot patch other instructions */
153 if (reg != NO_REG && reg != GETARG_B(*i)) 199 if (reg != NO_REG && reg != GETARG_B(*i))
154 SETARG_A(*i, reg); 200 SETARG_A(*i, reg);
155 else /* no register to put value or register already has the value */ 201 else {
202 /* no register to put value or register already has the value;
203 change instruction to simple test */
156 *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i)); 204 *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i));
157 205 }
158 return 1; 206 return 1;
159} 207}
160 208
161 209
210/*
211** Traverse a list of tests ensuring no one produces a value
212*/
162static void removevalues (FuncState *fs, int list) { 213static void removevalues (FuncState *fs, int list) {
163 for (; list != NO_JUMP; list = getjump(fs, list)) 214 for (; list != NO_JUMP; list = getjump(fs, list))
164 patchtestreg(fs, list, NO_REG); 215 patchtestreg(fs, list, NO_REG);
165} 216}
166 217
167 218
219/*
220** Traverse a list of tests, patching their destination address and
221** registers: tests producing values jump to 'vtarget' (and put their
222** values in 'reg'), other tests jump to 'dtarget'.
223*/
168static void patchlistaux (FuncState *fs, int list, int vtarget, int reg, 224static void patchlistaux (FuncState *fs, int list, int vtarget, int reg,
169 int dtarget) { 225 int dtarget) {
170 while (list != NO_JUMP) { 226 while (list != NO_JUMP) {
@@ -178,21 +234,35 @@ static void patchlistaux (FuncState *fs, int list, int vtarget, int reg,
178} 234}
179 235
180 236
237/*
238** Ensure all pending jumps to current position are fixed (jumping
239** to current position with no values) and reset list of pending
240** jumps
241*/
181static void dischargejpc (FuncState *fs) { 242static void dischargejpc (FuncState *fs) {
182 patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc); 243 patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc);
183 fs->jpc = NO_JUMP; 244 fs->jpc = NO_JUMP;
184} 245}
185 246
186 247
248/*
249** Add elements in 'list' to list of pending jumps to "here"
250** (current position)
251*/
187void luaK_patchtohere (FuncState *fs, int list) { 252void luaK_patchtohere (FuncState *fs, int list) {
188 luaK_getlabel(fs); 253 luaK_getlabel(fs); /* mark "here" as a jump target */
189 luaK_concat(fs, &fs->jpc, list); 254 luaK_concat(fs, &fs->jpc, list);
190} 255}
191 256
192 257
258/*
259** Path all jumps in 'list' to jump to 'target'.
260** (The assert means that we cannot fix a jump to a forward address
261** because we only know addresses once code is generated.)
262*/
193void luaK_patchlist (FuncState *fs, int list, int target) { 263void luaK_patchlist (FuncState *fs, int list, int target) {
194 if (target == fs->pc) 264 if (target == fs->pc) /* 'target' is current position? */
195 luaK_patchtohere(fs, list); 265 luaK_patchtohere(fs, list); /* add list to pending jumps */
196 else { 266 else {
197 lua_assert(target < fs->pc); 267 lua_assert(target < fs->pc);
198 patchlistaux(fs, list, target, NO_REG, target); 268 patchlistaux(fs, list, target, NO_REG, target);
@@ -200,6 +270,11 @@ void luaK_patchlist (FuncState *fs, int list, int target) {
200} 270}
201 271
202 272
273/*
274** Path all jumps in 'list' to close upvalues up to given 'level'
275** (The assertion checks that jumps either were closing nothing
276** or were closing higher levels, from inner blocks.)
277*/
203void luaK_patchclose (FuncState *fs, int list, int level) { 278void luaK_patchclose (FuncState *fs, int list, int level) {
204 level++; /* argument is +1 to reserve 0 as non-op */ 279 level++; /* argument is +1 to reserve 0 as non-op */
205 for (; list != NO_JUMP; list = getjump(fs, list)) { 280 for (; list != NO_JUMP; list = getjump(fs, list)) {
@@ -211,6 +286,10 @@ void luaK_patchclose (FuncState *fs, int list, int level) {
211} 286}
212 287
213 288
289/*
290** Emit instruction 'i', checking for array sizes and saving also its
291** line information. Return 'i' position.
292*/
214static int luaK_code (FuncState *fs, Instruction i) { 293static int luaK_code (FuncState *fs, Instruction i) {
215 Proto *f = fs->f; 294 Proto *f = fs->f;
216 dischargejpc(fs); /* 'pc' will change */ 295 dischargejpc(fs); /* 'pc' will change */
@@ -226,6 +305,10 @@ static int luaK_code (FuncState *fs, Instruction i) {
226} 305}
227 306
228 307
308/*
309** Format and emit an 'iABC' instruction. (Assertions check consistency
310** of parameters versus opcode.)
311*/
229int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) { 312int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {
230 lua_assert(getOpMode(o) == iABC); 313 lua_assert(getOpMode(o) == iABC);
231 lua_assert(getBMode(o) != OpArgN || b == 0); 314 lua_assert(getBMode(o) != OpArgN || b == 0);
@@ -235,6 +318,9 @@ int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {
235} 318}
236 319
237 320
321/*
322** Format and emit an 'iABx' instruction.
323*/
238int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) { 324int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {
239 lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx); 325 lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx);
240 lua_assert(getCMode(o) == OpArgN); 326 lua_assert(getCMode(o) == OpArgN);
@@ -243,12 +329,20 @@ int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {
243} 329}
244 330
245 331
332/*
333** Emit an "extra argument" instruction (format 'iAx')
334*/
246static int codeextraarg (FuncState *fs, int a) { 335static int codeextraarg (FuncState *fs, int a) {
247 lua_assert(a <= MAXARG_Ax); 336 lua_assert(a <= MAXARG_Ax);
248 return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, a)); 337 return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, a));
249} 338}
250 339
251 340
341/*
342** Emit a "load constant" instruction, using either 'OP_LOADK'
343** (if constant index 'k' fits in 18 bits) or an 'OP_LOADKX'
344** instruction with "extra argument".
345*/
252int luaK_codek (FuncState *fs, int reg, int k) { 346int luaK_codek (FuncState *fs, int reg, int k) {
253 if (k <= MAXARG_Bx) 347 if (k <= MAXARG_Bx)
254 return luaK_codeABx(fs, OP_LOADK, reg, k); 348 return luaK_codeABx(fs, OP_LOADK, reg, k);
@@ -260,6 +354,10 @@ int luaK_codek (FuncState *fs, int reg, int k) {
260} 354}
261 355
262 356
357/*
358** Check register-stack level, keeping track of its maximum size
359** in field 'maxstacksize'
360*/
263void luaK_checkstack (FuncState *fs, int n) { 361void luaK_checkstack (FuncState *fs, int n) {
264 int newstack = fs->freereg + n; 362 int newstack = fs->freereg + n;
265 if (newstack > fs->f->maxstacksize) { 363 if (newstack > fs->f->maxstacksize) {
@@ -271,12 +369,20 @@ void luaK_checkstack (FuncState *fs, int n) {
271} 369}
272 370
273 371
372/*
373** Reserve 'n' registers in register stack
374*/
274void luaK_reserveregs (FuncState *fs, int n) { 375void luaK_reserveregs (FuncState *fs, int n) {
275 luaK_checkstack(fs, n); 376 luaK_checkstack(fs, n);
276 fs->freereg += n; 377 fs->freereg += n;
277} 378}
278 379
279 380
381/*
382** Free register 'reg', if it is neither a constant index nor
383** a local variable.
384)
385*/
280static void freereg (FuncState *fs, int reg) { 386static void freereg (FuncState *fs, int reg) {
281 if (!ISK(reg) && reg >= fs->nactvar) { 387 if (!ISK(reg) && reg >= fs->nactvar) {
282 fs->freereg--; 388 fs->freereg--;
@@ -285,6 +391,9 @@ static void freereg (FuncState *fs, int reg) {
285} 391}
286 392
287 393
394/*
395** Free register used by expression 'e' (if any)
396*/
288static void freeexp (FuncState *fs, expdesc *e) { 397static void freeexp (FuncState *fs, expdesc *e) {
289 if (e->k == VNONRELOC) 398 if (e->k == VNONRELOC)
290 freereg(fs, e->u.info); 399 freereg(fs, e->u.info);
@@ -292,8 +401,11 @@ static void freeexp (FuncState *fs, expdesc *e) {
292 401
293 402
294/* 403/*
404** Add constant 'v' to prototype's list of constants (field 'k').
295** Use scanner's table to cache position of constants in constant list 405** Use scanner's table to cache position of constants in constant list
296** and try to reuse constants 406** and try to reuse constants. Because some values should not be used
407** as keys (nil cannot be a key, integer keys can collapse with float
408** keys), the caller must provide a useful 'key' for indexing the cache.
297*/ 409*/
298static int addk (FuncState *fs, TValue *key, TValue *v) { 410static int addk (FuncState *fs, TValue *key, TValue *v) {
299 lua_State *L = fs->ls->L; 411 lua_State *L = fs->ls->L;
@@ -322,17 +434,21 @@ static int addk (FuncState *fs, TValue *key, TValue *v) {
322} 434}
323 435
324 436
437/*
438** Add a string to list of constants and return its index.
439*/
325int luaK_stringK (FuncState *fs, TString *s) { 440int luaK_stringK (FuncState *fs, TString *s) {
326 TValue o; 441 TValue o;
327 setsvalue(fs->ls->L, &o, s); 442 setsvalue(fs->ls->L, &o, s);
328 return addk(fs, &o, &o); 443 return addk(fs, &o, &o); /* use string itself as key */
329} 444}
330 445
331 446
332/* 447/*
333** Integers use userdata as keys to avoid collision with floats with same 448** Add an integer to list of constants and return its index.
334** value; conversion to 'void*' used only for hashing, no "precision" 449** Integers use userdata as keys to avoid collision with floats with
335** problems 450** same value; conversion to 'void*' is used only for hashing, so there
451** are no "precision" problems.
336*/ 452*/
337int luaK_intK (FuncState *fs, lua_Integer n) { 453int luaK_intK (FuncState *fs, lua_Integer n) {
338 TValue k, o; 454 TValue k, o;
@@ -341,21 +457,29 @@ int luaK_intK (FuncState *fs, lua_Integer n) {
341 return addk(fs, &k, &o); 457 return addk(fs, &k, &o);
342} 458}
343 459
344 460/*
461** Add a float to list of constants and return its index.
462*/
345static int luaK_numberK (FuncState *fs, lua_Number r) { 463static int luaK_numberK (FuncState *fs, lua_Number r) {
346 TValue o; 464 TValue o;
347 setfltvalue(&o, r); 465 setfltvalue(&o, r);
348 return addk(fs, &o, &o); 466 return addk(fs, &o, &o); /* use number itself as key */
349} 467}
350 468
351 469
470/*
471** Add a boolean to list of constants and return its index.
472*/
352static int boolK (FuncState *fs, int b) { 473static int boolK (FuncState *fs, int b) {
353 TValue o; 474 TValue o;
354 setbvalue(&o, b); 475 setbvalue(&o, b);
355 return addk(fs, &o, &o); 476 return addk(fs, &o, &o); /* use boolean itself as key */
356} 477}
357 478
358 479
480/*
481** Add nil to list of constants and return its index.
482*/
359static int nilK (FuncState *fs) { 483static int nilK (FuncState *fs) {
360 TValue k, v; 484 TValue k, v;
361 setnilvalue(&v); 485 setnilvalue(&v);
@@ -365,6 +489,11 @@ static int nilK (FuncState *fs) {
365} 489}
366 490
367 491
492/*
493** Fix an expression to return the number of results 'nresults'.
494** Either 'e' is a multi-ret expression (function call or vararg)
495** or 'nresults' is LUA_MULTRET (as any expression can satisfy that).
496*/
368void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) { 497void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {
369 if (e->k == VCALL) { /* expression is an open function call? */ 498 if (e->k == VCALL) { /* expression is an open function call? */
370 SETARG_C(getcode(fs, e), nresults+1); 499 SETARG_C(getcode(fs, e), nresults+1);
@@ -374,12 +503,24 @@ void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {
374 SETARG_A(getcode(fs, e), fs->freereg); 503 SETARG_A(getcode(fs, e), fs->freereg);
375 luaK_reserveregs(fs, 1); 504 luaK_reserveregs(fs, 1);
376 } 505 }
506 else lua_assert(nresults == LUA_MULTRET);
377} 507}
378 508
379 509
510/*
511** Fix an expression to return one result.
512** If expression is not a multi-ret expression (function call or
513** vararg), it already returns one result, so nothing needs to be done.
514** Function calls become VNONRELOC expressions (as its result comes
515** fixed in the base register of the call), while vararg expressions
516** become VRELOCABLE (as OP_VARARG puts its results where it wants).
517** (Calls are created returning one result, so that does not need
518** to be fixed.)
519*/
380void luaK_setoneret (FuncState *fs, expdesc *e) { 520void luaK_setoneret (FuncState *fs, expdesc *e) {
381 if (e->k == VCALL) { /* expression is an open function call? */ 521 if (e->k == VCALL) { /* expression is an open function call? */
382 e->k = VNONRELOC; 522 lua_assert(GETARG_C(getcode(fs, e)) == 2); /* already returns 1 value */
523 e->k = VNONRELOC; /* result has fixed position */
383 e->u.info = GETARG_A(getcode(fs, e)); 524 e->u.info = GETARG_A(getcode(fs, e));
384 } 525 }
385 else if (e->k == VVARARG) { 526 else if (e->k == VVARARG) {
@@ -389,10 +530,14 @@ void luaK_setoneret (FuncState *fs, expdesc *e) {
389} 530}
390 531
391 532
533/*
534** Ensure that expression 'e' has a value somewhere (either it
535** is a constant or result is in a register).
536*/
392void luaK_dischargevars (FuncState *fs, expdesc *e) { 537void luaK_dischargevars (FuncState *fs, expdesc *e) {
393 switch (e->k) { 538 switch (e->k) {
394 case VLOCAL: { 539 case VLOCAL: {
395 e->k = VNONRELOC; 540 e->k = VNONRELOC; /* becomes a non-relocatable value */
396 break; 541 break;
397 } 542 }
398 case VUPVAL: { 543 case VUPVAL: {
@@ -421,6 +566,10 @@ void luaK_dischargevars (FuncState *fs, expdesc *e) {
421} 566}
422 567
423 568
569/*
570** Ensures expression value is in register 'reg' (and therefore
571** 'e' will become a non-relocatable expression).
572*/
424static void discharge2reg (FuncState *fs, expdesc *e, int reg) { 573static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
425 luaK_dischargevars(fs, e); 574 luaK_dischargevars(fs, e);
426 switch (e->k) { 575 switch (e->k) {
@@ -446,7 +595,7 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
446 } 595 }
447 case VRELOCABLE: { 596 case VRELOCABLE: {
448 Instruction *pc = &getcode(fs, e); 597 Instruction *pc = &getcode(fs, e);
449 SETARG_A(*pc, reg); 598 SETARG_A(*pc, reg); /* instruction will put result in 'reg' */
450 break; 599 break;
451 } 600 }
452 case VNONRELOC: { 601 case VNONRELOC: {
@@ -455,7 +604,7 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
455 break; 604 break;
456 } 605 }
457 default: { 606 default: {
458 lua_assert(e->k == VVOID || e->k == VJMP); 607 lua_assert(e->k == VJMP);
459 return; /* nothing to do... */ 608 return; /* nothing to do... */
460 } 609 }
461 } 610 }
@@ -464,10 +613,13 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
464} 613}
465 614
466 615
616/*
617** Ensures expression value is in any register.
618*/
467static void discharge2anyreg (FuncState *fs, expdesc *e) { 619static void discharge2anyreg (FuncState *fs, expdesc *e) {
468 if (e->k != VNONRELOC) { 620 if (e->k != VNONRELOC) { /* no fixed register yet? */
469 luaK_reserveregs(fs, 1); 621 luaK_reserveregs(fs, 1); /* get a register */
470 discharge2reg(fs, e, fs->freereg-1); 622 discharge2reg(fs, e, fs->freereg-1); /* put value there */
471 } 623 }
472} 624}
473 625
@@ -480,7 +632,7 @@ static int code_loadbool (FuncState *fs, int A, int b, int jump) {
480 632
481/* 633/*
482** check whether list has any jump that do not produce a value 634** check whether list has any jump that do not produce a value
483** (or produce an inverted value) 635** or produce an inverted value
484*/ 636*/
485static int need_value (FuncState *fs, int list) { 637static int need_value (FuncState *fs, int list) {
486 for (; list != NO_JUMP; list = getjump(fs, list)) { 638 for (; list != NO_JUMP; list = getjump(fs, list)) {
@@ -491,9 +643,16 @@ static int need_value (FuncState *fs, int list) {
491} 643}
492 644
493 645
646/*
647** Ensures final expression result (including results from its jump
648** lists) is in register 'reg'.
649** If expression has jumps, need to patch these jumps either to
650** its final position or to "load" instructions (for those tests
651** that do not produce values).
652*/
494static void exp2reg (FuncState *fs, expdesc *e, int reg) { 653static void exp2reg (FuncState *fs, expdesc *e, int reg) {
495 discharge2reg(fs, e, reg); 654 discharge2reg(fs, e, reg);
496 if (e->k == VJMP) 655 if (e->k == VJMP) /* expression itself is a test? */
497 luaK_concat(fs, &e->t, e->u.info); /* put this jump in 't' list */ 656 luaK_concat(fs, &e->t, e->u.info); /* put this jump in 't' list */
498 if (hasjumps(e)) { 657 if (hasjumps(e)) {
499 int final; /* position after whole expression */ 658 int final; /* position after whole expression */
@@ -515,6 +674,10 @@ static void exp2reg (FuncState *fs, expdesc *e, int reg) {
515} 674}
516 675
517 676
677/*
678** Ensures final expression result (including results from its jump
679** lists) is in next available register.
680*/
518void luaK_exp2nextreg (FuncState *fs, expdesc *e) { 681void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
519 luaK_dischargevars(fs, e); 682 luaK_dischargevars(fs, e);
520 freeexp(fs, e); 683 freeexp(fs, e);
@@ -523,26 +686,39 @@ void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
523} 686}
524 687
525 688
689/*
690** Ensures final expression result (including results from its jump
691** lists) is in some (any) register and return that register.
692*/
526int luaK_exp2anyreg (FuncState *fs, expdesc *e) { 693int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
527 luaK_dischargevars(fs, e); 694 luaK_dischargevars(fs, e);
528 if (e->k == VNONRELOC) { 695 if (e->k == VNONRELOC) { /* expression already has a register? */
529 if (!hasjumps(e)) return e->u.info; /* exp is already in a register */ 696 if (!hasjumps(e)) /* no jumps? */
697 return e->u.info; /* result is already in a register */
530 if (e->u.info >= fs->nactvar) { /* reg. is not a local? */ 698 if (e->u.info >= fs->nactvar) { /* reg. is not a local? */
531 exp2reg(fs, e, e->u.info); /* put value on it */ 699 exp2reg(fs, e, e->u.info); /* put final result in it */
532 return e->u.info; 700 return e->u.info;
533 } 701 }
534 } 702 }
535 luaK_exp2nextreg(fs, e); /* default */ 703 luaK_exp2nextreg(fs, e); /* otherwise, use next available register */
536 return e->u.info; 704 return e->u.info;
537} 705}
538 706
539 707
708/*
709** Ensures final expression result is either in a register or in an
710** upvalue.
711*/
540void luaK_exp2anyregup (FuncState *fs, expdesc *e) { 712void luaK_exp2anyregup (FuncState *fs, expdesc *e) {
541 if (e->k != VUPVAL || hasjumps(e)) 713 if (e->k != VUPVAL || hasjumps(e))
542 luaK_exp2anyreg(fs, e); 714 luaK_exp2anyreg(fs, e);
543} 715}
544 716
545 717
718/*
719** Ensures final expression result is either in a register or it is
720** a constant.
721*/
546void luaK_exp2val (FuncState *fs, expdesc *e) { 722void luaK_exp2val (FuncState *fs, expdesc *e) {
547 if (hasjumps(e)) 723 if (hasjumps(e))
548 luaK_exp2anyreg(fs, e); 724 luaK_exp2anyreg(fs, e);
@@ -551,9 +727,14 @@ void luaK_exp2val (FuncState *fs, expdesc *e) {
551} 727}
552 728
553 729
730/*
731** Ensures final expression result is in a valid R/K index
732** (that is, it is either in a register or in 'k' with an index
733** in the range of R/K indices).
734*/
554int luaK_exp2RK (FuncState *fs, expdesc *e) { 735int luaK_exp2RK (FuncState *fs, expdesc *e) {
555 luaK_exp2val(fs, e); 736 luaK_exp2val(fs, e);
556 switch (e->k) { 737 switch (e->k) { /* handle constants */
557 case VTRUE: 738 case VTRUE:
558 case VFALSE: 739 case VFALSE:
559 case VNIL: { 740 case VNIL: {
@@ -587,11 +768,14 @@ int luaK_exp2RK (FuncState *fs, expdesc *e) {
587} 768}
588 769
589 770
771/*
772** Generate code to store result of expression 'ex' into variable 'var'.
773*/
590void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { 774void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
591 switch (var->k) { 775 switch (var->k) {
592 case VLOCAL: { 776 case VLOCAL: {
593 freeexp(fs, ex); 777 freeexp(fs, ex);
594 exp2reg(fs, ex, var->u.info); 778 exp2reg(fs, ex, var->u.info); /* compute 'ex' into proper place */
595 return; 779 return;
596 } 780 }
597 case VUPVAL: { 781 case VUPVAL: {
@@ -605,29 +789,32 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
605 luaK_codeABC(fs, op, var->u.ind.t, var->u.ind.idx, e); 789 luaK_codeABC(fs, op, var->u.ind.t, var->u.ind.idx, e);
606 break; 790 break;
607 } 791 }
608 default: { 792 default: lua_assert(0); /* invalid var kind to store */
609 lua_assert(0); /* invalid var kind to store */
610 break;
611 }
612 } 793 }
613 freeexp(fs, ex); 794 freeexp(fs, ex);
614} 795}
615 796
616 797
798/*
799** Emit SELF instruction (convert expression 'e' into 'e:key(e,').
800*/
617void luaK_self (FuncState *fs, expdesc *e, expdesc *key) { 801void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
618 int ereg; 802 int ereg;
619 luaK_exp2anyreg(fs, e); 803 luaK_exp2anyreg(fs, e);
620 ereg = e->u.info; /* register where 'e' was placed */ 804 ereg = e->u.info; /* register where 'e' was placed */
621 freeexp(fs, e); 805 freeexp(fs, e);
622 e->u.info = fs->freereg; /* base register for op_self */ 806 e->u.info = fs->freereg; /* base register for op_self */
623 e->k = VNONRELOC; 807 e->k = VNONRELOC; /* self expression has a fixed register */
624 luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */ 808 luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */
625 luaK_codeABC(fs, OP_SELF, e->u.info, ereg, luaK_exp2RK(fs, key)); 809 luaK_codeABC(fs, OP_SELF, e->u.info, ereg, luaK_exp2RK(fs, key));
626 freeexp(fs, key); 810 freeexp(fs, key);
627} 811}
628 812
629 813
630static void invertjump (FuncState *fs, expdesc *e) { 814/*
815** Negate condition 'e' (where 'e' is a comparison).
816*/
817static void negatecondition (FuncState *fs, expdesc *e) {
631 Instruction *pc = getjumpcontrol(fs, e->u.info); 818 Instruction *pc = getjumpcontrol(fs, e->u.info);
632 lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET && 819 lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET &&
633 GET_OPCODE(*pc) != OP_TEST); 820 GET_OPCODE(*pc) != OP_TEST);
@@ -635,6 +822,12 @@ static void invertjump (FuncState *fs, expdesc *e) {
635} 822}
636 823
637 824
825/*
826** Emit instruction to jump if 'e' is 'cond' (that is, if 'cond'
827** is true, code will jump if 'e' is true.) Return jump position.
828** Optimize when 'e' is 'not' something, inverting the condition
829** and removing the 'not'.
830*/
638static int jumponcond (FuncState *fs, expdesc *e, int cond) { 831static int jumponcond (FuncState *fs, expdesc *e, int cond) {
639 if (e->k == VRELOCABLE) { 832 if (e->k == VRELOCABLE) {
640 Instruction ie = getcode(fs, e); 833 Instruction ie = getcode(fs, e);
@@ -650,13 +843,16 @@ static int jumponcond (FuncState *fs, expdesc *e, int cond) {
650} 843}
651 844
652 845
846/*
847** Emit code to go through if 'e' is true, jump otherwise.
848*/
653void luaK_goiftrue (FuncState *fs, expdesc *e) { 849void luaK_goiftrue (FuncState *fs, expdesc *e) {
654 int pc; /* pc of last jump */ 850 int pc; /* pc of new jump */
655 luaK_dischargevars(fs, e); 851 luaK_dischargevars(fs, e);
656 switch (e->k) { 852 switch (e->k) {
657 case VJMP: { 853 case VJMP: { /* condition? */
658 invertjump(fs, e); 854 negatecondition(fs, e); /* jump when it is false */
659 pc = e->u.info; 855 pc = e->u.info; /* save jump position */
660 break; 856 break;
661 } 857 }
662 case VK: case VKFLT: case VKINT: case VTRUE: { 858 case VK: case VKFLT: case VKINT: case VTRUE: {
@@ -664,22 +860,25 @@ void luaK_goiftrue (FuncState *fs, expdesc *e) {
664 break; 860 break;
665 } 861 }
666 default: { 862 default: {
667 pc = jumponcond(fs, e, 0); 863 pc = jumponcond(fs, e, 0); /* jump when false */
668 break; 864 break;
669 } 865 }
670 } 866 }
671 luaK_concat(fs, &e->f, pc); /* insert last jump in 'f' list */ 867 luaK_concat(fs, &e->f, pc); /* insert new jump in false list */
672 luaK_patchtohere(fs, e->t); 868 luaK_patchtohere(fs, e->t); /* true list jumps to here (to go through) */
673 e->t = NO_JUMP; 869 e->t = NO_JUMP;
674} 870}
675 871
676 872
873/*
874** Emit code to go through if 'e' is false, jump otherwise.
875*/
677void luaK_goiffalse (FuncState *fs, expdesc *e) { 876void luaK_goiffalse (FuncState *fs, expdesc *e) {
678 int pc; /* pc of last jump */ 877 int pc; /* pc of new jump */
679 luaK_dischargevars(fs, e); 878 luaK_dischargevars(fs, e);
680 switch (e->k) { 879 switch (e->k) {
681 case VJMP: { 880 case VJMP: {
682 pc = e->u.info; 881 pc = e->u.info; /* already jump if true */
683 break; 882 break;
684 } 883 }
685 case VNIL: case VFALSE: { 884 case VNIL: case VFALSE: {
@@ -687,29 +886,32 @@ void luaK_goiffalse (FuncState *fs, expdesc *e) {
687 break; 886 break;
688 } 887 }
689 default: { 888 default: {
690 pc = jumponcond(fs, e, 1); 889 pc = jumponcond(fs, e, 1); /* jump if true */
691 break; 890 break;
692 } 891 }
693 } 892 }
694 luaK_concat(fs, &e->t, pc); /* insert last jump in 't' list */ 893 luaK_concat(fs, &e->t, pc); /* insert new jump in 't' list */
695 luaK_patchtohere(fs, e->f); 894 luaK_patchtohere(fs, e->f); /* false list jumps to here (to go through) */
696 e->f = NO_JUMP; 895 e->f = NO_JUMP;
697} 896}
698 897
699 898
899/*
900** Code 'not e', doing constant folding.
901*/
700static void codenot (FuncState *fs, expdesc *e) { 902static void codenot (FuncState *fs, expdesc *e) {
701 luaK_dischargevars(fs, e); 903 luaK_dischargevars(fs, e);
702 switch (e->k) { 904 switch (e->k) {
703 case VNIL: case VFALSE: { 905 case VNIL: case VFALSE: {
704 e->k = VTRUE; 906 e->k = VTRUE; /* true == not nil == not false */
705 break; 907 break;
706 } 908 }
707 case VK: case VKFLT: case VKINT: case VTRUE: { 909 case VK: case VKFLT: case VKINT: case VTRUE: {
708 e->k = VFALSE; 910 e->k = VFALSE; /* false == not "x" == not 0.5 == not 1 == not true */
709 break; 911 break;
710 } 912 }
711 case VJMP: { 913 case VJMP: {
712 invertjump(fs, e); 914 negatecondition(fs, e);
713 break; 915 break;
714 } 916 }
715 case VRELOCABLE: 917 case VRELOCABLE:
@@ -720,30 +922,32 @@ static void codenot (FuncState *fs, expdesc *e) {
720 e->k = VRELOCABLE; 922 e->k = VRELOCABLE;
721 break; 923 break;
722 } 924 }
723 default: { 925 default: lua_assert(0); /* cannot happen */
724 lua_assert(0); /* cannot happen */
725 break;
726 }
727 } 926 }
728 /* interchange true and false lists */ 927 /* interchange true and false lists */
729 { int temp = e->f; e->f = e->t; e->t = temp; } 928 { int temp = e->f; e->f = e->t; e->t = temp; }
730 removevalues(fs, e->f); 929 removevalues(fs, e->f); /* values are useless when negated */
731 removevalues(fs, e->t); 930 removevalues(fs, e->t);
732} 931}
733 932
734 933
934/*
935** Create expression 't[k]'. 't' must have its final result already in a
936** register or upvalue.
937*/
735void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { 938void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
736 lua_assert(!hasjumps(t)); 939 lua_assert(!hasjumps(t) && (vkisinreg(t->k) || t->k == VUPVAL));
737 t->u.ind.t = t->u.info; 940 t->u.ind.t = t->u.info; /* register or upvalue index */
738 t->u.ind.idx = luaK_exp2RK(fs, k); 941 t->u.ind.idx = luaK_exp2RK(fs, k); /* R/K index for key */
739 t->u.ind.vt = (t->k == VUPVAL) ? VUPVAL 942 t->u.ind.vt = (t->k == VUPVAL) ? VUPVAL : VLOCAL;
740 : check_exp(vkisinreg(t->k), VLOCAL);
741 t->k = VINDEXED; 943 t->k = VINDEXED;
742} 944}
743 945
744 946
745/* 947/*
746** return false if folding can raise an error 948** Return false if folding can raise an error.
949** Bitwise operations need operands convertible to integers; division
950** operations cannot have 0 as divisor.
747*/ 951*/
748static int validop (int op, TValue *v1, TValue *v2) { 952static int validop (int op, TValue *v1, TValue *v2) {
749 switch (op) { 953 switch (op) {
@@ -760,7 +964,8 @@ static int validop (int op, TValue *v1, TValue *v2) {
760 964
761 965
762/* 966/*
763** Try to "constant-fold" an operation; return 1 iff successful 967** Try to "constant-fold" an operation; return 1 iff successful.
968** (In this case, 'e1' has the final result.)
764*/ 969*/
765static int constfolding (FuncState *fs, int op, expdesc *e1, expdesc *e2) { 970static int constfolding (FuncState *fs, int op, expdesc *e1, expdesc *e2) {
766 TValue v1, v2, res; 971 TValue v1, v2, res;
@@ -771,7 +976,7 @@ static int constfolding (FuncState *fs, int op, expdesc *e1, expdesc *e2) {
771 e1->k = VKINT; 976 e1->k = VKINT;
772 e1->u.ival = ivalue(&res); 977 e1->u.ival = ivalue(&res);
773 } 978 }
774 else { /* folds neither NaN nor 0.0 (to avoid collapsing with -0.0) */ 979 else { /* folds neither NaN nor 0.0 (to avoid problems with -0.0) */
775 lua_Number n = fltvalue(&res); 980 lua_Number n = fltvalue(&res);
776 if (luai_numisnan(n) || n == 0) 981 if (luai_numisnan(n) || n == 0)
777 return 0; 982 return 0;
@@ -783,15 +988,19 @@ static int constfolding (FuncState *fs, int op, expdesc *e1, expdesc *e2) {
783 988
784 989
785/* 990/*
786** Code for binary and unary expressions that "produce values" 991** Emit code for binary and unary expressions that "produce values"
787** (arithmetic operations, bitwise operations, concat, length). First 992** (everything but logical operators 'and', 'or' and comparison
788** try to do constant folding (only for numeric [arithmetic and 993** operators).
789** bitwise] operations, which is what 'lua_arith' accepts). 994** First try to do constant folding (only for numeric [arithmetic and
790** Expression to produce final result will be encoded in 'e1'. 995** bitwise] operations, which is what 'lua_arith' accepts). Expression
996** to produce final result will be encoded in 'e1'.
997** (The "free registers in proper order" reason is tricky: because
998** expression evaluation can be delayed, the final numbering for
999** registers in 'e1' and 'e2' depends on how each one was delayed.)
791*/ 1000*/
792static void codeexpval (FuncState *fs, OpCode op, 1001static void codeexpval (FuncState *fs, OpCode op,
793 expdesc *e1, expdesc *e2, int line) { 1002 expdesc *e1, expdesc *e2, int line) {
794 lua_assert(op >= OP_ADD); 1003 lua_assert(OP_ADD <= op && op <= OP_CONCAT);
795 if (op <= OP_BNOT && constfolding(fs, (op - OP_ADD) + LUA_OPADD, e1, e2)) 1004 if (op <= OP_BNOT && constfolding(fs, (op - OP_ADD) + LUA_OPADD, e1, e2))
796 return; /* result has been folded */ 1005 return; /* result has been folded */
797 else { 1006 else {
@@ -820,10 +1029,16 @@ static void codeexpval (FuncState *fs, OpCode op,
820} 1029}
821 1030
822 1031
1032/*
1033** Emit code for comparisons.
1034** Code will jump if result equals 'cond' ('cond' true <=> code will
1035** jump if result is true).
1036*/
823static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1, 1037static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1,
824 expdesc *e2) { 1038 expdesc *e2) {
825 int o1 = luaK_exp2RK(fs, e1); 1039 int o1 = luaK_exp2RK(fs, e1);
826 int o2 = luaK_exp2RK(fs, e2); 1040 int o2 = luaK_exp2RK(fs, e2);
1041 lua_assert(OP_EQ <= op && op <= OP_LE); /* comparison operation */
827 freeexp(fs, e2); 1042 freeexp(fs, e2);
828 freeexp(fs, e1); 1043 freeexp(fs, e1);
829 if (cond == 0 && op != OP_EQ) { 1044 if (cond == 0 && op != OP_EQ) {
@@ -836,8 +1051,11 @@ static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1,
836} 1051}
837 1052
838 1053
1054/*
1055** Aplly prefix operation 'op' to expression 'e'.
1056*/
839void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) { 1057void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) {
840 expdesc e2; 1058 expdesc e2; /* fake 2nd operand */
841 e2.t = e2.f = NO_JUMP; e2.k = VKINT; e2.u.ival = 0; 1059 e2.t = e2.f = NO_JUMP; e2.k = VKINT; e2.u.ival = 0;
842 switch (op) { 1060 switch (op) {
843 case OPR_MINUS: case OPR_BNOT: case OPR_LEN: { 1061 case OPR_MINUS: case OPR_BNOT: case OPR_LEN: {
@@ -850,14 +1068,18 @@ void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) {
850} 1068}
851 1069
852 1070
1071/*
1072** Process 1st operand 'v' of binary operation 'op' before reading
1073** 2nd operand.
1074*/
853void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { 1075void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
854 switch (op) { 1076 switch (op) {
855 case OPR_AND: { 1077 case OPR_AND: {
856 luaK_goiftrue(fs, v); 1078 luaK_goiftrue(fs, v); /* go ahead only if 'v' is true */
857 break; 1079 break;
858 } 1080 }
859 case OPR_OR: { 1081 case OPR_OR: {
860 luaK_goiffalse(fs, v); 1082 luaK_goiffalse(fs, v); /* go ahead only if 'v' is false */
861 break; 1083 break;
862 } 1084 }
863 case OPR_CONCAT: { 1085 case OPR_CONCAT: {
@@ -869,7 +1091,9 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
869 case OPR_MOD: case OPR_POW: 1091 case OPR_MOD: case OPR_POW:
870 case OPR_BAND: case OPR_BOR: case OPR_BXOR: 1092 case OPR_BAND: case OPR_BOR: case OPR_BXOR:
871 case OPR_SHL: case OPR_SHR: { 1093 case OPR_SHL: case OPR_SHR: {
872 if (!tonumeral(v, NULL)) luaK_exp2RK(fs, v); 1094 if (!tonumeral(v, NULL))
1095 luaK_exp2RK(fs, v);
1096 /* else keep numeral, which may be folded with 2nd operand */
873 break; 1097 break;
874 } 1098 }
875 default: { 1099 default: {
@@ -880,18 +1104,24 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
880} 1104}
881 1105
882 1106
1107/*
1108** Finalize code for binary operation, after reading 2nd operand.
1109** For '(a .. b .. c)' (which is '(a .. (b .. c))', because
1110** concatenation is right associative), merge second CONCAT into first
1111** one.
1112*/
883void luaK_posfix (FuncState *fs, BinOpr op, 1113void luaK_posfix (FuncState *fs, BinOpr op,
884 expdesc *e1, expdesc *e2, int line) { 1114 expdesc *e1, expdesc *e2, int line) {
885 switch (op) { 1115 switch (op) {
886 case OPR_AND: { 1116 case OPR_AND: {
887 lua_assert(e1->t == NO_JUMP); /* list must be closed */ 1117 lua_assert(e1->t == NO_JUMP); /* list closed by 'luK_infix' */
888 luaK_dischargevars(fs, e2); 1118 luaK_dischargevars(fs, e2);
889 luaK_concat(fs, &e2->f, e1->f); 1119 luaK_concat(fs, &e2->f, e1->f);
890 *e1 = *e2; 1120 *e1 = *e2;
891 break; 1121 break;
892 } 1122 }
893 case OPR_OR: { 1123 case OPR_OR: {
894 lua_assert(e1->f == NO_JUMP); /* list must be closed */ 1124 lua_assert(e1->f == NO_JUMP); /* list closed by 'luK_infix' */
895 luaK_dischargevars(fs, e2); 1125 luaK_dischargevars(fs, e2);
896 luaK_concat(fs, &e2->t, e1->t); 1126 luaK_concat(fs, &e2->t, e1->t);
897 *e1 = *e2; 1127 *e1 = *e2;
@@ -931,15 +1161,25 @@ void luaK_posfix (FuncState *fs, BinOpr op,
931} 1161}
932 1162
933 1163
1164/*
1165** Change line information associated with current position.
1166*/
934void luaK_fixline (FuncState *fs, int line) { 1167void luaK_fixline (FuncState *fs, int line) {
935 fs->f->lineinfo[fs->pc - 1] = line; 1168 fs->f->lineinfo[fs->pc - 1] = line;
936} 1169}
937 1170
938 1171
1172/*
1173** Emit a SETLIST instruction.
1174** 'base' is register that keeps table;
1175** 'nelems' is #table plus those to be stored now;
1176** 'tostore' is number of values (in registers 'base + 1',...) to add to
1177** table (or LUA_MULTRET to add up to stack top).
1178*/
939void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) { 1179void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {
940 int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1; 1180 int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1;
941 int b = (tostore == LUA_MULTRET) ? 0 : tostore; 1181 int b = (tostore == LUA_MULTRET) ? 0 : tostore;
942 lua_assert(tostore != 0); 1182 lua_assert(tostore != 0 && tostore <= LFIELDS_PER_FLUSH);
943 if (c <= MAXARG_C) 1183 if (c <= MAXARG_C)
944 luaK_codeABC(fs, OP_SETLIST, base, b, c); 1184 luaK_codeABC(fs, OP_SETLIST, base, b, c);
945 else if (c <= MAXARG_Ax) { 1185 else if (c <= MAXARG_Ax) {