1087
|
1 /*
|
|
2 ** $Id: lcode.c,v 2.60 2011/08/30 16:26:41 roberto Exp $
|
|
3 ** Code generator for Lua
|
|
4 ** See Copyright Notice in lua.h
|
|
5 */
|
|
6
|
|
7
|
|
8 #include <stdlib.h>
|
|
9
|
|
10 #define lcode_c
|
|
11 #define LUA_CORE
|
|
12
|
|
13 #include "lua.h"
|
|
14
|
|
15 #include "lcode.h"
|
|
16 #include "ldebug.h"
|
|
17 #include "ldo.h"
|
|
18 #include "lgc.h"
|
|
19 #include "llex.h"
|
|
20 #include "lmem.h"
|
|
21 #include "lobject.h"
|
|
22 #include "lopcodes.h"
|
|
23 #include "lparser.h"
|
|
24 #include "lstring.h"
|
|
25 #include "ltable.h"
|
|
26 #include "lvm.h"
|
|
27
|
|
28
|
|
29 #define hasjumps(e) ((e)->t != (e)->f)
|
|
30
|
|
31
|
|
32 static int isnumeral(expdesc *e) {
|
|
33 return (e->k == VKNUM && e->t == NO_JUMP && e->f == NO_JUMP);
|
|
34 }
|
|
35
|
|
36
|
|
37 void luaK_nil (FuncState *fs, int from, int n) {
|
|
38 Instruction *previous;
|
|
39 int l = from + n - 1; /* last register to set nil */
|
|
40 if (fs->pc > fs->lasttarget) { /* no jumps to current position? */
|
|
41 previous = &fs->f->code[fs->pc-1];
|
|
42 if (GET_OPCODE(*previous) == OP_LOADNIL) {
|
|
43 int pfrom = GETARG_A(*previous);
|
|
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);
|
|
51 return;
|
|
52 }
|
|
53 } /* else go through */
|
|
54 }
|
|
55 luaK_codeABC(fs, OP_LOADNIL, from, n - 1, 0); /* else no optimization */
|
|
56 }
|
|
57
|
|
58
|
|
59 int luaK_jump (FuncState *fs) {
|
|
60 int jpc = fs->jpc; /* save list of jumps to here */
|
|
61 int j;
|
|
62 fs->jpc = NO_JUMP;
|
|
63 j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP);
|
|
64 luaK_concat(fs, &j, jpc); /* keep them on hold */
|
|
65 return j;
|
|
66 }
|
|
67
|
|
68
|
|
69 void luaK_ret (FuncState *fs, int first, int nret) {
|
|
70 luaK_codeABC(fs, OP_RETURN, first, nret+1, 0);
|
|
71 }
|
|
72
|
|
73
|
|
74 static int condjump (FuncState *fs, OpCode op, int A, int B, int C) {
|
|
75 luaK_codeABC(fs, op, A, B, C);
|
|
76 return luaK_jump(fs);
|
|
77 }
|
|
78
|
|
79
|
|
80 static void fixjump (FuncState *fs, int pc, int dest) {
|
|
81 Instruction *jmp = &fs->f->code[pc];
|
|
82 int offset = dest-(pc+1);
|
|
83 lua_assert(dest != NO_JUMP);
|
|
84 if (abs(offset) > MAXARG_sBx)
|
|
85 luaX_syntaxerror(fs->ls, "control structure too long");
|
|
86 SETARG_sBx(*jmp, offset);
|
|
87 }
|
|
88
|
|
89
|
|
90 /*
|
|
91 ** returns current `pc' and marks it as a jump target (to avoid wrong
|
|
92 ** optimizations with consecutive instructions not in the same basic block).
|
|
93 */
|
|
94 int luaK_getlabel (FuncState *fs) {
|
|
95 fs->lasttarget = fs->pc;
|
|
96 return fs->pc;
|
|
97 }
|
|
98
|
|
99
|
|
100 static int getjump (FuncState *fs, int pc) {
|
|
101 int offset = GETARG_sBx(fs->f->code[pc]);
|
|
102 if (offset == NO_JUMP) /* point to itself represents end of list */
|
|
103 return NO_JUMP; /* end of list */
|
|
104 else
|
|
105 return (pc+1)+offset; /* turn offset into absolute position */
|
|
106 }
|
|
107
|
|
108
|
|
109 static Instruction *getjumpcontrol (FuncState *fs, int pc) {
|
|
110 Instruction *pi = &fs->f->code[pc];
|
|
111 if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1))))
|
|
112 return pi-1;
|
|
113 else
|
|
114 return pi;
|
|
115 }
|
|
116
|
|
117
|
|
118 /*
|
|
119 ** check whether list has any jump that do not produce a value
|
|
120 ** (or produce an inverted value)
|
|
121 */
|
|
122 static int need_value (FuncState *fs, int list) {
|
|
123 for (; list != NO_JUMP; list = getjump(fs, list)) {
|
|
124 Instruction i = *getjumpcontrol(fs, list);
|
|
125 if (GET_OPCODE(i) != OP_TESTSET) return 1;
|
|
126 }
|
|
127 return 0; /* not found */
|
|
128 }
|
|
129
|
|
130
|
|
131 static int patchtestreg (FuncState *fs, int node, int reg) {
|
|
132 Instruction *i = getjumpcontrol(fs, node);
|
|
133 if (GET_OPCODE(*i) != OP_TESTSET)
|
|
134 return 0; /* cannot patch other instructions */
|
|
135 if (reg != NO_REG && reg != GETARG_B(*i))
|
|
136 SETARG_A(*i, reg);
|
|
137 else /* no register to put value or register already has the value */
|
|
138 *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i));
|
|
139
|
|
140 return 1;
|
|
141 }
|
|
142
|
|
143
|
|
144 static void removevalues (FuncState *fs, int list) {
|
|
145 for (; list != NO_JUMP; list = getjump(fs, list))
|
|
146 patchtestreg(fs, list, NO_REG);
|
|
147 }
|
|
148
|
|
149
|
|
150 static void patchlistaux (FuncState *fs, int list, int vtarget, int reg,
|
|
151 int dtarget) {
|
|
152 while (list != NO_JUMP) {
|
|
153 int next = getjump(fs, list);
|
|
154 if (patchtestreg(fs, list, reg))
|
|
155 fixjump(fs, list, vtarget);
|
|
156 else
|
|
157 fixjump(fs, list, dtarget); /* jump to default target */
|
|
158 list = next;
|
|
159 }
|
|
160 }
|
|
161
|
|
162
|
|
163 static void dischargejpc (FuncState *fs) {
|
|
164 patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc);
|
|
165 fs->jpc = NO_JUMP;
|
|
166 }
|
|
167
|
|
168
|
|
169 void luaK_patchlist (FuncState *fs, int list, int target) {
|
|
170 if (target == fs->pc)
|
|
171 luaK_patchtohere(fs, list);
|
|
172 else {
|
|
173 lua_assert(target < fs->pc);
|
|
174 patchlistaux(fs, list, target, NO_REG, target);
|
|
175 }
|
|
176 }
|
|
177
|
|
178
|
|
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
|
|
192 void luaK_patchtohere (FuncState *fs, int list) {
|
|
193 luaK_getlabel(fs);
|
|
194 luaK_concat(fs, &fs->jpc, list);
|
|
195 }
|
|
196
|
|
197
|
|
198 void luaK_concat (FuncState *fs, int *l1, int l2) {
|
|
199 if (l2 == NO_JUMP) return;
|
|
200 else if (*l1 == NO_JUMP)
|
|
201 *l1 = l2;
|
|
202 else {
|
|
203 int list = *l1;
|
|
204 int next;
|
|
205 while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */
|
|
206 list = next;
|
|
207 fixjump(fs, list, l2);
|
|
208 }
|
|
209 }
|
|
210
|
|
211
|
|
212 static int luaK_code (FuncState *fs, Instruction i) {
|
|
213 Proto *f = fs->f;
|
|
214 dischargejpc(fs); /* `pc' will change */
|
|
215 /* put new instruction in code array */
|
|
216 luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction,
|
|
217 MAX_INT, "opcodes");
|
|
218 f->code[fs->pc] = i;
|
|
219 /* save corresponding line information */
|
|
220 luaM_growvector(fs->ls->L, f->lineinfo, fs->pc, f->sizelineinfo, int,
|
|
221 MAX_INT, "opcodes");
|
|
222 f->lineinfo[fs->pc] = fs->ls->lastline;
|
|
223 return fs->pc++;
|
|
224 }
|
|
225
|
|
226
|
|
227 int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {
|
|
228 lua_assert(getOpMode(o) == iABC);
|
|
229 lua_assert(getBMode(o) != OpArgN || b == 0);
|
|
230 lua_assert(getCMode(o) != OpArgN || c == 0);
|
|
231 lua_assert(a <= MAXARG_A && b <= MAXARG_B && c <= MAXARG_C);
|
|
232 return luaK_code(fs, CREATE_ABC(o, a, b, c));
|
|
233 }
|
|
234
|
|
235
|
|
236 int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {
|
|
237 lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx);
|
|
238 lua_assert(getCMode(o) == OpArgN);
|
|
239 lua_assert(a <= MAXARG_A && bc <= MAXARG_Bx);
|
|
240 return luaK_code(fs, CREATE_ABx(o, a, bc));
|
|
241 }
|
|
242
|
|
243
|
|
244 static int codeextraarg (FuncState *fs, int a) {
|
|
245 lua_assert(a <= MAXARG_Ax);
|
|
246 return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, a));
|
|
247 }
|
|
248
|
|
249
|
|
250 int luaK_codek (FuncState *fs, int reg, int k) {
|
|
251 if (k <= MAXARG_Bx)
|
|
252 return luaK_codeABx(fs, OP_LOADK, reg, k);
|
|
253 else {
|
|
254 int p = luaK_codeABx(fs, OP_LOADKX, reg, 0);
|
|
255 codeextraarg(fs, k);
|
|
256 return p;
|
|
257 }
|
|
258 }
|
|
259
|
|
260
|
|
261 void luaK_checkstack (FuncState *fs, int n) {
|
|
262 int newstack = fs->freereg + n;
|
|
263 if (newstack > fs->f->maxstacksize) {
|
|
264 if (newstack >= MAXSTACK)
|
|
265 luaX_syntaxerror(fs->ls, "function or expression too complex");
|
|
266 fs->f->maxstacksize = cast_byte(newstack);
|
|
267 }
|
|
268 }
|
|
269
|
|
270
|
|
271 void luaK_reserveregs (FuncState *fs, int n) {
|
|
272 luaK_checkstack(fs, n);
|
|
273 fs->freereg += n;
|
|
274 }
|
|
275
|
|
276
|
|
277 static void freereg (FuncState *fs, int reg) {
|
|
278 if (!ISK(reg) && reg >= fs->nactvar) {
|
|
279 fs->freereg--;
|
|
280 lua_assert(reg == fs->freereg);
|
|
281 }
|
|
282 }
|
|
283
|
|
284
|
|
285 static void freeexp (FuncState *fs, expdesc *e) {
|
|
286 if (e->k == VNONRELOC)
|
|
287 freereg(fs, e->u.info);
|
|
288 }
|
|
289
|
|
290
|
|
291 static int addk (FuncState *fs, TValue *key, TValue *v) {
|
|
292 lua_State *L = fs->ls->L;
|
|
293 TValue *idx = luaH_set(L, fs->h, key);
|
|
294 Proto *f = fs->f;
|
|
295 int k, oldsize;
|
|
296 if (ttisnumber(idx)) {
|
|
297 lua_Number n = nvalue(idx);
|
|
298 lua_number2int(k, n);
|
|
299 if (luaV_rawequalobj(&f->k[k], v))
|
|
300 return k;
|
|
301 /* else may be a collision (e.g., between 0.0 and "\0\0\0\0\0\0\0\0");
|
|
302 go through and create a new entry for this value */
|
|
303 }
|
|
304 /* constant not found; create a new entry */
|
|
305 oldsize = f->sizek;
|
|
306 k = fs->nk;
|
|
307 /* numerical value does not need GC barrier;
|
|
308 table has no metatable, so it does not need to invalidate cache */
|
|
309 setnvalue(idx, cast_num(k));
|
|
310 luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants");
|
|
311 while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
|
|
312 setobj(L, &f->k[k], v);
|
|
313 fs->nk++;
|
|
314 luaC_barrier(L, f, v);
|
|
315 return k;
|
|
316 }
|
|
317
|
|
318
|
|
319 int luaK_stringK (FuncState *fs, TString *s) {
|
|
320 TValue o;
|
|
321 setsvalue(fs->ls->L, &o, s);
|
|
322 return addk(fs, &o, &o);
|
|
323 }
|
|
324
|
|
325
|
|
326 int luaK_numberK (FuncState *fs, lua_Number r) {
|
|
327 int n;
|
|
328 lua_State *L = fs->ls->L;
|
|
329 TValue o;
|
|
330 setnvalue(&o, r);
|
|
331 if (r == 0 || luai_numisnan(NULL, r)) { /* handle -0 and NaN */
|
|
332 /* use raw representation as key to avoid numeric problems */
|
|
333 setsvalue(L, L->top, luaS_newlstr(L, (char *)&r, sizeof(r)));
|
|
334 incr_top(L);
|
|
335 n = addk(fs, L->top - 1, &o);
|
|
336 L->top--;
|
|
337 }
|
|
338 else
|
|
339 n = addk(fs, &o, &o); /* regular case */
|
|
340 return n;
|
|
341 }
|
|
342
|
|
343
|
|
344 static int boolK (FuncState *fs, int b) {
|
|
345 TValue o;
|
|
346 setbvalue(&o, b);
|
|
347 return addk(fs, &o, &o);
|
|
348 }
|
|
349
|
|
350
|
|
351 static int nilK (FuncState *fs) {
|
|
352 TValue k, v;
|
|
353 setnilvalue(&v);
|
|
354 /* cannot use nil as key; instead use table itself to represent nil */
|
|
355 sethvalue(fs->ls->L, &k, fs->h);
|
|
356 return addk(fs, &k, &v);
|
|
357 }
|
|
358
|
|
359
|
|
360 void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {
|
|
361 if (e->k == VCALL) { /* expression is an open function call? */
|
|
362 SETARG_C(getcode(fs, e), nresults+1);
|
|
363 }
|
|
364 else if (e->k == VVARARG) {
|
|
365 SETARG_B(getcode(fs, e), nresults+1);
|
|
366 SETARG_A(getcode(fs, e), fs->freereg);
|
|
367 luaK_reserveregs(fs, 1);
|
|
368 }
|
|
369 }
|
|
370
|
|
371
|
|
372 void luaK_setoneret (FuncState *fs, expdesc *e) {
|
|
373 if (e->k == VCALL) { /* expression is an open function call? */
|
|
374 e->k = VNONRELOC;
|
|
375 e->u.info = GETARG_A(getcode(fs, e));
|
|
376 }
|
|
377 else if (e->k == VVARARG) {
|
|
378 SETARG_B(getcode(fs, e), 2);
|
|
379 e->k = VRELOCABLE; /* can relocate its simple result */
|
|
380 }
|
|
381 }
|
|
382
|
|
383
|
|
384 void luaK_dischargevars (FuncState *fs, expdesc *e) {
|
|
385 switch (e->k) {
|
|
386 case VLOCAL: {
|
|
387 e->k = VNONRELOC;
|
|
388 break;
|
|
389 }
|
|
390 case VUPVAL: {
|
|
391 e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0);
|
|
392 e->k = VRELOCABLE;
|
|
393 break;
|
|
394 }
|
|
395 case VINDEXED: {
|
|
396 OpCode op = OP_GETTABUP; /* assume 't' is in an upvalue */
|
|
397 freereg(fs, e->u.ind.idx);
|
|
398 if (e->u.ind.vt == VLOCAL) { /* 't' is in a register? */
|
|
399 freereg(fs, e->u.ind.t);
|
|
400 op = OP_GETTABLE;
|
|
401 }
|
|
402 e->u.info = luaK_codeABC(fs, op, 0, e->u.ind.t, e->u.ind.idx);
|
|
403 e->k = VRELOCABLE;
|
|
404 break;
|
|
405 }
|
|
406 case VVARARG:
|
|
407 case VCALL: {
|
|
408 luaK_setoneret(fs, e);
|
|
409 break;
|
|
410 }
|
|
411 default: break; /* there is one value available (somewhere) */
|
|
412 }
|
|
413 }
|
|
414
|
|
415
|
|
416 static int code_label (FuncState *fs, int A, int b, int jump) {
|
|
417 luaK_getlabel(fs); /* those instructions may be jump targets */
|
|
418 return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);
|
|
419 }
|
|
420
|
|
421
|
|
422 static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
|
|
423 luaK_dischargevars(fs, e);
|
|
424 switch (e->k) {
|
|
425 case VNIL: {
|
|
426 luaK_nil(fs, reg, 1);
|
|
427 break;
|
|
428 }
|
|
429 case VFALSE: case VTRUE: {
|
|
430 luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
|
|
431 break;
|
|
432 }
|
|
433 case VK: {
|
|
434 luaK_codek(fs, reg, e->u.info);
|
|
435 break;
|
|
436 }
|
|
437 case VKNUM: {
|
|
438 luaK_codek(fs, reg, luaK_numberK(fs, e->u.nval));
|
|
439 break;
|
|
440 }
|
|
441 case VRELOCABLE: {
|
|
442 Instruction *pc = &getcode(fs, e);
|
|
443 SETARG_A(*pc, reg);
|
|
444 break;
|
|
445 }
|
|
446 case VNONRELOC: {
|
|
447 if (reg != e->u.info)
|
|
448 luaK_codeABC(fs, OP_MOVE, reg, e->u.info, 0);
|
|
449 break;
|
|
450 }
|
|
451 default: {
|
|
452 lua_assert(e->k == VVOID || e->k == VJMP);
|
|
453 return; /* nothing to do... */
|
|
454 }
|
|
455 }
|
|
456 e->u.info = reg;
|
|
457 e->k = VNONRELOC;
|
|
458 }
|
|
459
|
|
460
|
|
461 static void discharge2anyreg (FuncState *fs, expdesc *e) {
|
|
462 if (e->k != VNONRELOC) {
|
|
463 luaK_reserveregs(fs, 1);
|
|
464 discharge2reg(fs, e, fs->freereg-1);
|
|
465 }
|
|
466 }
|
|
467
|
|
468
|
|
469 static void exp2reg (FuncState *fs, expdesc *e, int reg) {
|
|
470 discharge2reg(fs, e, reg);
|
|
471 if (e->k == VJMP)
|
|
472 luaK_concat(fs, &e->t, e->u.info); /* put this jump in `t' list */
|
|
473 if (hasjumps(e)) {
|
|
474 int final; /* position after whole expression */
|
|
475 int p_f = NO_JUMP; /* position of an eventual LOAD false */
|
|
476 int p_t = NO_JUMP; /* position of an eventual LOAD true */
|
|
477 if (need_value(fs, e->t) || need_value(fs, e->f)) {
|
|
478 int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs);
|
|
479 p_f = code_label(fs, reg, 0, 1);
|
|
480 p_t = code_label(fs, reg, 1, 0);
|
|
481 luaK_patchtohere(fs, fj);
|
|
482 }
|
|
483 final = luaK_getlabel(fs);
|
|
484 patchlistaux(fs, e->f, final, reg, p_f);
|
|
485 patchlistaux(fs, e->t, final, reg, p_t);
|
|
486 }
|
|
487 e->f = e->t = NO_JUMP;
|
|
488 e->u.info = reg;
|
|
489 e->k = VNONRELOC;
|
|
490 }
|
|
491
|
|
492
|
|
493 void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
|
|
494 luaK_dischargevars(fs, e);
|
|
495 freeexp(fs, e);
|
|
496 luaK_reserveregs(fs, 1);
|
|
497 exp2reg(fs, e, fs->freereg - 1);
|
|
498 }
|
|
499
|
|
500
|
|
501 int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
|
|
502 luaK_dischargevars(fs, e);
|
|
503 if (e->k == VNONRELOC) {
|
|
504 if (!hasjumps(e)) return e->u.info; /* exp is already in a register */
|
|
505 if (e->u.info >= fs->nactvar) { /* reg. is not a local? */
|
|
506 exp2reg(fs, e, e->u.info); /* put value on it */
|
|
507 return e->u.info;
|
|
508 }
|
|
509 }
|
|
510 luaK_exp2nextreg(fs, e); /* default */
|
|
511 return e->u.info;
|
|
512 }
|
|
513
|
|
514
|
|
515 void luaK_exp2anyregup (FuncState *fs, expdesc *e) {
|
|
516 if (e->k != VUPVAL || hasjumps(e))
|
|
517 luaK_exp2anyreg(fs, e);
|
|
518 }
|
|
519
|
|
520
|
|
521 void luaK_exp2val (FuncState *fs, expdesc *e) {
|
|
522 if (hasjumps(e))
|
|
523 luaK_exp2anyreg(fs, e);
|
|
524 else
|
|
525 luaK_dischargevars(fs, e);
|
|
526 }
|
|
527
|
|
528
|
|
529 int luaK_exp2RK (FuncState *fs, expdesc *e) {
|
|
530 luaK_exp2val(fs, e);
|
|
531 switch (e->k) {
|
|
532 case VTRUE:
|
|
533 case VFALSE:
|
|
534 case VNIL: {
|
|
535 if (fs->nk <= MAXINDEXRK) { /* constant fits in RK operand? */
|
|
536 e->u.info = (e->k == VNIL) ? nilK(fs) : boolK(fs, (e->k == VTRUE));
|
|
537 e->k = VK;
|
|
538 return RKASK(e->u.info);
|
|
539 }
|
|
540 else break;
|
|
541 }
|
|
542 case VKNUM: {
|
|
543 e->u.info = luaK_numberK(fs, e->u.nval);
|
|
544 e->k = VK;
|
|
545 /* go through */
|
|
546 }
|
|
547 case VK: {
|
|
548 if (e->u.info <= MAXINDEXRK) /* constant fits in argC? */
|
|
549 return RKASK(e->u.info);
|
|
550 else break;
|
|
551 }
|
|
552 default: break;
|
|
553 }
|
|
554 /* not a constant in the right range: put it in a register */
|
|
555 return luaK_exp2anyreg(fs, e);
|
|
556 }
|
|
557
|
|
558
|
|
559 void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
|
|
560 switch (var->k) {
|
|
561 case VLOCAL: {
|
|
562 freeexp(fs, ex);
|
|
563 exp2reg(fs, ex, var->u.info);
|
|
564 return;
|
|
565 }
|
|
566 case VUPVAL: {
|
|
567 int e = luaK_exp2anyreg(fs, ex);
|
|
568 luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0);
|
|
569 break;
|
|
570 }
|
|
571 case VINDEXED: {
|
|
572 OpCode op = (var->u.ind.vt == VLOCAL) ? OP_SETTABLE : OP_SETTABUP;
|
|
573 int e = luaK_exp2RK(fs, ex);
|
|
574 luaK_codeABC(fs, op, var->u.ind.t, var->u.ind.idx, e);
|
|
575 break;
|
|
576 }
|
|
577 default: {
|
|
578 lua_assert(0); /* invalid var kind to store */
|
|
579 break;
|
|
580 }
|
|
581 }
|
|
582 freeexp(fs, ex);
|
|
583 }
|
|
584
|
|
585
|
|
586 void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
|
|
587 int ereg;
|
|
588 luaK_exp2anyreg(fs, e);
|
|
589 ereg = e->u.info; /* register where 'e' was placed */
|
|
590 freeexp(fs, e);
|
|
591 e->u.info = fs->freereg; /* base register for op_self */
|
|
592 e->k = VNONRELOC;
|
|
593 luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */
|
|
594 luaK_codeABC(fs, OP_SELF, e->u.info, ereg, luaK_exp2RK(fs, key));
|
|
595 freeexp(fs, key);
|
|
596 }
|
|
597
|
|
598
|
|
599 static void invertjump (FuncState *fs, expdesc *e) {
|
|
600 Instruction *pc = getjumpcontrol(fs, e->u.info);
|
|
601 lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET &&
|
|
602 GET_OPCODE(*pc) != OP_TEST);
|
|
603 SETARG_A(*pc, !(GETARG_A(*pc)));
|
|
604 }
|
|
605
|
|
606
|
|
607 static int jumponcond (FuncState *fs, expdesc *e, int cond) {
|
|
608 if (e->k == VRELOCABLE) {
|
|
609 Instruction ie = getcode(fs, e);
|
|
610 if (GET_OPCODE(ie) == OP_NOT) {
|
|
611 fs->pc--; /* remove previous OP_NOT */
|
|
612 return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond);
|
|
613 }
|
|
614 /* else go through */
|
|
615 }
|
|
616 discharge2anyreg(fs, e);
|
|
617 freeexp(fs, e);
|
|
618 return condjump(fs, OP_TESTSET, NO_REG, e->u.info, cond);
|
|
619 }
|
|
620
|
|
621
|
|
622 void luaK_goiftrue (FuncState *fs, expdesc *e) {
|
|
623 int pc; /* pc of last jump */
|
|
624 luaK_dischargevars(fs, e);
|
|
625 switch (e->k) {
|
|
626 case VJMP: {
|
|
627 invertjump(fs, e);
|
|
628 pc = e->u.info;
|
|
629 break;
|
|
630 }
|
|
631 case VK: case VKNUM: case VTRUE: {
|
|
632 pc = NO_JUMP; /* always true; do nothing */
|
|
633 break;
|
|
634 }
|
|
635 default: {
|
|
636 pc = jumponcond(fs, e, 0);
|
|
637 break;
|
|
638 }
|
|
639 }
|
|
640 luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */
|
|
641 luaK_patchtohere(fs, e->t);
|
|
642 e->t = NO_JUMP;
|
|
643 }
|
|
644
|
|
645
|
|
646 void luaK_goiffalse (FuncState *fs, expdesc *e) {
|
|
647 int pc; /* pc of last jump */
|
|
648 luaK_dischargevars(fs, e);
|
|
649 switch (e->k) {
|
|
650 case VJMP: {
|
|
651 pc = e->u.info;
|
|
652 break;
|
|
653 }
|
|
654 case VNIL: case VFALSE: {
|
|
655 pc = NO_JUMP; /* always false; do nothing */
|
|
656 break;
|
|
657 }
|
|
658 default: {
|
|
659 pc = jumponcond(fs, e, 1);
|
|
660 break;
|
|
661 }
|
|
662 }
|
|
663 luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */
|
|
664 luaK_patchtohere(fs, e->f);
|
|
665 e->f = NO_JUMP;
|
|
666 }
|
|
667
|
|
668
|
|
669 static void codenot (FuncState *fs, expdesc *e) {
|
|
670 luaK_dischargevars(fs, e);
|
|
671 switch (e->k) {
|
|
672 case VNIL: case VFALSE: {
|
|
673 e->k = VTRUE;
|
|
674 break;
|
|
675 }
|
|
676 case VK: case VKNUM: case VTRUE: {
|
|
677 e->k = VFALSE;
|
|
678 break;
|
|
679 }
|
|
680 case VJMP: {
|
|
681 invertjump(fs, e);
|
|
682 break;
|
|
683 }
|
|
684 case VRELOCABLE:
|
|
685 case VNONRELOC: {
|
|
686 discharge2anyreg(fs, e);
|
|
687 freeexp(fs, e);
|
|
688 e->u.info = luaK_codeABC(fs, OP_NOT, 0, e->u.info, 0);
|
|
689 e->k = VRELOCABLE;
|
|
690 break;
|
|
691 }
|
|
692 default: {
|
|
693 lua_assert(0); /* cannot happen */
|
|
694 break;
|
|
695 }
|
|
696 }
|
|
697 /* interchange true and false lists */
|
|
698 { int temp = e->f; e->f = e->t; e->t = temp; }
|
|
699 removevalues(fs, e->f);
|
|
700 removevalues(fs, e->t);
|
|
701 }
|
|
702
|
|
703
|
|
704 void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
|
|
705 lua_assert(!hasjumps(t));
|
|
706 t->u.ind.t = t->u.info;
|
|
707 t->u.ind.idx = luaK_exp2RK(fs, k);
|
|
708 t->u.ind.vt = (t->k == VUPVAL) ? VUPVAL
|
|
709 : check_exp(vkisinreg(t->k), VLOCAL);
|
|
710 t->k = VINDEXED;
|
|
711 }
|
|
712
|
|
713
|
|
714 static int constfolding (OpCode op, expdesc *e1, expdesc *e2) {
|
|
715 lua_Number r;
|
|
716 if (!isnumeral(e1) || !isnumeral(e2)) return 0;
|
|
717 if ((op == OP_DIV || op == OP_MOD) && e2->u.nval == 0)
|
|
718 return 0; /* do not attempt to divide by 0 */
|
|
719 r = luaO_arith(op - OP_ADD + LUA_OPADD, e1->u.nval, e2->u.nval);
|
|
720 e1->u.nval = r;
|
|
721 return 1;
|
|
722 }
|
|
723
|
|
724
|
|
725 static void codearith (FuncState *fs, OpCode op,
|
|
726 expdesc *e1, expdesc *e2, int line) {
|
|
727 if (constfolding(op, e1, e2))
|
|
728 return;
|
|
729 else {
|
|
730 int o2 = (op != OP_UNM && op != OP_LEN) ? luaK_exp2RK(fs, e2) : 0;
|
|
731 int o1 = luaK_exp2RK(fs, e1);
|
|
732 if (o1 > o2) {
|
|
733 freeexp(fs, e1);
|
|
734 freeexp(fs, e2);
|
|
735 }
|
|
736 else {
|
|
737 freeexp(fs, e2);
|
|
738 freeexp(fs, e1);
|
|
739 }
|
|
740 e1->u.info = luaK_codeABC(fs, op, 0, o1, o2);
|
|
741 e1->k = VRELOCABLE;
|
|
742 luaK_fixline(fs, line);
|
|
743 }
|
|
744 }
|
|
745
|
|
746
|
|
747 static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1,
|
|
748 expdesc *e2) {
|
|
749 int o1 = luaK_exp2RK(fs, e1);
|
|
750 int o2 = luaK_exp2RK(fs, e2);
|
|
751 freeexp(fs, e2);
|
|
752 freeexp(fs, e1);
|
|
753 if (cond == 0 && op != OP_EQ) {
|
|
754 int temp; /* exchange args to replace by `<' or `<=' */
|
|
755 temp = o1; o1 = o2; o2 = temp; /* o1 <==> o2 */
|
|
756 cond = 1;
|
|
757 }
|
|
758 e1->u.info = condjump(fs, op, cond, o1, o2);
|
|
759 e1->k = VJMP;
|
|
760 }
|
|
761
|
|
762
|
|
763 void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) {
|
|
764 expdesc e2;
|
|
765 e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0;
|
|
766 switch (op) {
|
|
767 case OPR_MINUS: {
|
|
768 if (isnumeral(e)) /* minus constant? */
|
|
769 e->u.nval = luai_numunm(NULL, e->u.nval); /* fold it */
|
|
770 else {
|
|
771 luaK_exp2anyreg(fs, e);
|
|
772 codearith(fs, OP_UNM, e, &e2, line);
|
|
773 }
|
|
774 break;
|
|
775 }
|
|
776 case OPR_NOT: codenot(fs, e); break;
|
|
777 case OPR_LEN: {
|
|
778 luaK_exp2anyreg(fs, e); /* cannot operate on constants */
|
|
779 codearith(fs, OP_LEN, e, &e2, line);
|
|
780 break;
|
|
781 }
|
|
782 default: lua_assert(0);
|
|
783 }
|
|
784 }
|
|
785
|
|
786
|
|
787 void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
|
|
788 switch (op) {
|
|
789 case OPR_AND: {
|
|
790 luaK_goiftrue(fs, v);
|
|
791 break;
|
|
792 }
|
|
793 case OPR_OR: {
|
|
794 luaK_goiffalse(fs, v);
|
|
795 break;
|
|
796 }
|
|
797 case OPR_CONCAT: {
|
|
798 luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */
|
|
799 break;
|
|
800 }
|
|
801 case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV:
|
|
802 case OPR_MOD: case OPR_POW: {
|
|
803 if (!isnumeral(v)) luaK_exp2RK(fs, v);
|
|
804 break;
|
|
805 }
|
|
806 default: {
|
|
807 luaK_exp2RK(fs, v);
|
|
808 break;
|
|
809 }
|
|
810 }
|
|
811 }
|
|
812
|
|
813
|
|
814 void luaK_posfix (FuncState *fs, BinOpr op,
|
|
815 expdesc *e1, expdesc *e2, int line) {
|
|
816 switch (op) {
|
|
817 case OPR_AND: {
|
|
818 lua_assert(e1->t == NO_JUMP); /* list must be closed */
|
|
819 luaK_dischargevars(fs, e2);
|
|
820 luaK_concat(fs, &e2->f, e1->f);
|
|
821 *e1 = *e2;
|
|
822 break;
|
|
823 }
|
|
824 case OPR_OR: {
|
|
825 lua_assert(e1->f == NO_JUMP); /* list must be closed */
|
|
826 luaK_dischargevars(fs, e2);
|
|
827 luaK_concat(fs, &e2->t, e1->t);
|
|
828 *e1 = *e2;
|
|
829 break;
|
|
830 }
|
|
831 case OPR_CONCAT: {
|
|
832 luaK_exp2val(fs, e2);
|
|
833 if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) {
|
|
834 lua_assert(e1->u.info == GETARG_B(getcode(fs, e2))-1);
|
|
835 freeexp(fs, e1);
|
|
836 SETARG_B(getcode(fs, e2), e1->u.info);
|
|
837 e1->k = VRELOCABLE; e1->u.info = e2->u.info;
|
|
838 }
|
|
839 else {
|
|
840 luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */
|
|
841 codearith(fs, OP_CONCAT, e1, e2, line);
|
|
842 }
|
|
843 break;
|
|
844 }
|
|
845 case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV:
|
|
846 case OPR_MOD: case OPR_POW: {
|
|
847 codearith(fs, cast(OpCode, op - OPR_ADD + OP_ADD), e1, e2, line);
|
|
848 break;
|
|
849 }
|
|
850 case OPR_EQ: case OPR_LT: case OPR_LE: {
|
|
851 codecomp(fs, cast(OpCode, op - OPR_EQ + OP_EQ), 1, e1, e2);
|
|
852 break;
|
|
853 }
|
|
854 case OPR_NE: case OPR_GT: case OPR_GE: {
|
|
855 codecomp(fs, cast(OpCode, op - OPR_NE + OP_EQ), 0, e1, e2);
|
|
856 break;
|
|
857 }
|
|
858 default: lua_assert(0);
|
|
859 }
|
|
860 }
|
|
861
|
|
862
|
|
863 void luaK_fixline (FuncState *fs, int line) {
|
|
864 fs->f->lineinfo[fs->pc - 1] = line;
|
|
865 }
|
|
866
|
|
867
|
|
868 void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {
|
|
869 int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1;
|
|
870 int b = (tostore == LUA_MULTRET) ? 0 : tostore;
|
|
871 lua_assert(tostore != 0);
|
|
872 if (c <= MAXARG_C)
|
|
873 luaK_codeABC(fs, OP_SETLIST, base, b, c);
|
|
874 else if (c <= MAXARG_Ax) {
|
|
875 luaK_codeABC(fs, OP_SETLIST, base, b, 0);
|
|
876 codeextraarg(fs, c);
|
|
877 }
|
|
878 else
|
|
879 luaX_syntaxerror(fs->ls, "constructor too long");
|
|
880 fs->freereg = base + 1; /* free registers with list values */
|
|
881 }
|
|
882
|