]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | /* |
11fdf7f2 | 2 | ** $Id: lcode.c,v 2.109 2016/05/13 19:09:21 roberto Exp $ |
7c673cae FG |
3 | ** Code generator for Lua |
4 | ** See Copyright Notice in lua.h | |
5 | */ | |
6 | ||
7 | #define lcode_c | |
8 | #define LUA_CORE | |
9 | ||
10 | #include "lprefix.h" | |
11 | ||
12 | ||
13 | #include <math.h> | |
14 | #include <stdlib.h> | |
15 | ||
16 | #include "lua.h" | |
17 | ||
18 | #include "lcode.h" | |
19 | #include "ldebug.h" | |
20 | #include "ldo.h" | |
21 | #include "lgc.h" | |
22 | #include "llex.h" | |
23 | #include "lmem.h" | |
24 | #include "lobject.h" | |
25 | #include "lopcodes.h" | |
26 | #include "lparser.h" | |
27 | #include "lstring.h" | |
28 | #include "ltable.h" | |
29 | #include "lvm.h" | |
30 | ||
31 | ||
32 | /* Maximum number of registers in a Lua function (must fit in 8 bits) */ | |
33 | #define MAXREGS 255 | |
34 | ||
35 | ||
36 | #define hasjumps(e) ((e)->t != (e)->f) | |
37 | ||
38 | ||
11fdf7f2 TL |
39 | /* |
40 | ** If expression is a numeric constant, fills 'v' with its value | |
41 | ** and returns 1. Otherwise, returns 0. | |
42 | */ | |
7c673cae | 43 | static int tonumeral(expdesc *e, TValue *v) { |
11fdf7f2 | 44 | if (hasjumps(e)) |
7c673cae FG |
45 | return 0; /* not a numeral */ |
46 | switch (e->k) { | |
47 | case VKINT: | |
48 | if (v) setivalue(v, e->u.ival); | |
49 | return 1; | |
50 | case VKFLT: | |
51 | if (v) setfltvalue(v, e->u.nval); | |
52 | return 1; | |
53 | default: return 0; | |
54 | } | |
55 | } | |
56 | ||
57 | ||
11fdf7f2 TL |
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 | */ | |
7c673cae FG |
64 | void luaK_nil (FuncState *fs, int from, int n) { |
65 | Instruction *previous; | |
66 | int l = from + n - 1; /* last register to set nil */ | |
67 | if (fs->pc > fs->lasttarget) { /* no jumps to current position? */ | |
68 | previous = &fs->f->code[fs->pc-1]; | |
11fdf7f2 TL |
69 | if (GET_OPCODE(*previous) == OP_LOADNIL) { /* previous is LOADNIL? */ |
70 | int pfrom = GETARG_A(*previous); /* get previous range */ | |
7c673cae FG |
71 | int pl = pfrom + GETARG_B(*previous); |
72 | if ((pfrom <= from && from <= pl + 1) || | |
73 | (from <= pfrom && pfrom <= l + 1)) { /* can connect both? */ | |
74 | if (pfrom < from) from = pfrom; /* from = min(from, pfrom) */ | |
75 | if (pl > l) l = pl; /* l = max(l, pl) */ | |
76 | SETARG_A(*previous, from); | |
77 | SETARG_B(*previous, l - from); | |
78 | return; | |
79 | } | |
80 | } /* else go through */ | |
81 | } | |
82 | luaK_codeABC(fs, OP_LOADNIL, from, n - 1, 0); /* else no optimization */ | |
83 | } | |
84 | ||
85 | ||
11fdf7f2 TL |
86 | /* |
87 | ** Gets the destination address of a jump instruction. Used to traverse | |
88 | ** a list of jumps. | |
89 | */ | |
90 | static int getjump (FuncState *fs, int pc) { | |
91 | int offset = GETARG_sBx(fs->f->code[pc]); | |
92 | if (offset == NO_JUMP) /* point to itself represents end of list */ | |
93 | return NO_JUMP; /* end of list */ | |
94 | else | |
95 | return (pc+1)+offset; /* turn offset into absolute position */ | |
96 | } | |
97 | ||
98 | ||
99 | /* | |
100 | ** Fix jump instruction at position 'pc' to jump to 'dest'. | |
101 | ** (Jump addresses are relative in Lua) | |
102 | */ | |
103 | static void fixjump (FuncState *fs, int pc, int dest) { | |
104 | Instruction *jmp = &fs->f->code[pc]; | |
105 | int offset = dest - (pc + 1); | |
106 | lua_assert(dest != NO_JUMP); | |
107 | if (abs(offset) > MAXARG_sBx) | |
108 | luaX_syntaxerror(fs->ls, "control structure too long"); | |
109 | SETARG_sBx(*jmp, offset); | |
110 | } | |
111 | ||
112 | ||
113 | /* | |
114 | ** Concatenate jump-list 'l2' into jump-list 'l1' | |
115 | */ | |
116 | void luaK_concat (FuncState *fs, int *l1, int l2) { | |
117 | if (l2 == NO_JUMP) return; /* nothing to concatenate? */ | |
118 | else if (*l1 == NO_JUMP) /* no original list? */ | |
119 | *l1 = l2; /* 'l1' points to 'l2' */ | |
120 | else { | |
121 | int list = *l1; | |
122 | int next; | |
123 | while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */ | |
124 | list = next; | |
125 | fixjump(fs, list, l2); /* last element links to 'l2' */ | |
126 | } | |
127 | } | |
128 | ||
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 | */ | |
7c673cae FG |
136 | int luaK_jump (FuncState *fs) { |
137 | int jpc = fs->jpc; /* save list of jumps to here */ | |
138 | int j; | |
11fdf7f2 | 139 | fs->jpc = NO_JUMP; /* no more jumps to here */ |
7c673cae FG |
140 | j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP); |
141 | luaK_concat(fs, &j, jpc); /* keep them on hold */ | |
142 | return j; | |
143 | } | |
144 | ||
145 | ||
11fdf7f2 TL |
146 | /* |
147 | ** Code a 'return' instruction | |
148 | */ | |
7c673cae FG |
149 | void luaK_ret (FuncState *fs, int first, int nret) { |
150 | luaK_codeABC(fs, OP_RETURN, first, nret+1, 0); | |
151 | } | |
152 | ||
153 | ||
11fdf7f2 TL |
154 | /* |
155 | ** Code a "conditional jump", that is, a test or comparison opcode | |
156 | ** followed by a jump. Return jump position. | |
157 | */ | |
7c673cae FG |
158 | static int condjump (FuncState *fs, OpCode op, int A, int B, int C) { |
159 | luaK_codeABC(fs, op, A, B, C); | |
160 | return luaK_jump(fs); | |
161 | } | |
162 | ||
163 | ||
7c673cae FG |
164 | /* |
165 | ** returns current 'pc' and marks it as a jump target (to avoid wrong | |
166 | ** optimizations with consecutive instructions not in the same basic block). | |
167 | */ | |
168 | int luaK_getlabel (FuncState *fs) { | |
169 | fs->lasttarget = fs->pc; | |
170 | return fs->pc; | |
171 | } | |
172 | ||
173 | ||
11fdf7f2 TL |
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 | */ | |
7c673cae FG |
179 | static Instruction *getjumpcontrol (FuncState *fs, int pc) { |
180 | Instruction *pi = &fs->f->code[pc]; | |
181 | if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1)))) | |
182 | return pi-1; | |
183 | else | |
184 | return pi; | |
185 | } | |
186 | ||
187 | ||
188 | /* | |
11fdf7f2 TL |
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) | |
7c673cae | 194 | */ |
7c673cae FG |
195 | static int patchtestreg (FuncState *fs, int node, int reg) { |
196 | Instruction *i = getjumpcontrol(fs, node); | |
197 | if (GET_OPCODE(*i) != OP_TESTSET) | |
198 | return 0; /* cannot patch other instructions */ | |
199 | if (reg != NO_REG && reg != GETARG_B(*i)) | |
200 | SETARG_A(*i, reg); | |
11fdf7f2 TL |
201 | else { |
202 | /* no register to put value or register already has the value; | |
203 | change instruction to simple test */ | |
7c673cae | 204 | *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i)); |
11fdf7f2 | 205 | } |
7c673cae FG |
206 | return 1; |
207 | } | |
208 | ||
209 | ||
11fdf7f2 TL |
210 | /* |
211 | ** Traverse a list of tests ensuring no one produces a value | |
212 | */ | |
7c673cae FG |
213 | static void removevalues (FuncState *fs, int list) { |
214 | for (; list != NO_JUMP; list = getjump(fs, list)) | |
215 | patchtestreg(fs, list, NO_REG); | |
216 | } | |
217 | ||
218 | ||
11fdf7f2 TL |
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 | */ | |
7c673cae FG |
224 | static void patchlistaux (FuncState *fs, int list, int vtarget, int reg, |
225 | int dtarget) { | |
226 | while (list != NO_JUMP) { | |
227 | int next = getjump(fs, list); | |
228 | if (patchtestreg(fs, list, reg)) | |
229 | fixjump(fs, list, vtarget); | |
230 | else | |
231 | fixjump(fs, list, dtarget); /* jump to default target */ | |
232 | list = next; | |
233 | } | |
234 | } | |
235 | ||
236 | ||
11fdf7f2 TL |
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 | */ | |
7c673cae FG |
242 | static void dischargejpc (FuncState *fs) { |
243 | patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc); | |
244 | fs->jpc = NO_JUMP; | |
245 | } | |
246 | ||
247 | ||
11fdf7f2 TL |
248 | /* |
249 | ** Add elements in 'list' to list of pending jumps to "here" | |
250 | ** (current position) | |
251 | */ | |
252 | void luaK_patchtohere (FuncState *fs, int list) { | |
253 | luaK_getlabel(fs); /* mark "here" as a jump target */ | |
254 | luaK_concat(fs, &fs->jpc, list); | |
255 | } | |
256 | ||
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 | */ | |
7c673cae | 263 | void luaK_patchlist (FuncState *fs, int list, int target) { |
11fdf7f2 TL |
264 | if (target == fs->pc) /* 'target' is current position? */ |
265 | luaK_patchtohere(fs, list); /* add list to pending jumps */ | |
7c673cae FG |
266 | else { |
267 | lua_assert(target < fs->pc); | |
268 | patchlistaux(fs, list, target, NO_REG, target); | |
269 | } | |
270 | } | |
271 | ||
272 | ||
11fdf7f2 TL |
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 | */ | |
7c673cae FG |
278 | void luaK_patchclose (FuncState *fs, int list, int level) { |
279 | level++; /* argument is +1 to reserve 0 as non-op */ | |
11fdf7f2 | 280 | for (; list != NO_JUMP; list = getjump(fs, list)) { |
7c673cae FG |
281 | lua_assert(GET_OPCODE(fs->f->code[list]) == OP_JMP && |
282 | (GETARG_A(fs->f->code[list]) == 0 || | |
283 | GETARG_A(fs->f->code[list]) >= level)); | |
284 | SETARG_A(fs->f->code[list], level); | |
7c673cae FG |
285 | } |
286 | } | |
287 | ||
288 | ||
11fdf7f2 TL |
289 | /* |
290 | ** Emit instruction 'i', checking for array sizes and saving also its | |
291 | ** line information. Return 'i' position. | |
292 | */ | |
7c673cae FG |
293 | static int luaK_code (FuncState *fs, Instruction i) { |
294 | Proto *f = fs->f; | |
295 | dischargejpc(fs); /* 'pc' will change */ | |
296 | /* put new instruction in code array */ | |
297 | luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction, | |
298 | MAX_INT, "opcodes"); | |
299 | f->code[fs->pc] = i; | |
300 | /* save corresponding line information */ | |
301 | luaM_growvector(fs->ls->L, f->lineinfo, fs->pc, f->sizelineinfo, int, | |
302 | MAX_INT, "opcodes"); | |
303 | f->lineinfo[fs->pc] = fs->ls->lastline; | |
304 | return fs->pc++; | |
305 | } | |
306 | ||
307 | ||
11fdf7f2 TL |
308 | /* |
309 | ** Format and emit an 'iABC' instruction. (Assertions check consistency | |
310 | ** of parameters versus opcode.) | |
311 | */ | |
7c673cae FG |
312 | int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) { |
313 | lua_assert(getOpMode(o) == iABC); | |
314 | lua_assert(getBMode(o) != OpArgN || b == 0); | |
315 | lua_assert(getCMode(o) != OpArgN || c == 0); | |
316 | lua_assert(a <= MAXARG_A && b <= MAXARG_B && c <= MAXARG_C); | |
317 | return luaK_code(fs, CREATE_ABC(o, a, b, c)); | |
318 | } | |
319 | ||
320 | ||
11fdf7f2 TL |
321 | /* |
322 | ** Format and emit an 'iABx' instruction. | |
323 | */ | |
7c673cae FG |
324 | int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) { |
325 | lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx); | |
326 | lua_assert(getCMode(o) == OpArgN); | |
327 | lua_assert(a <= MAXARG_A && bc <= MAXARG_Bx); | |
328 | return luaK_code(fs, CREATE_ABx(o, a, bc)); | |
329 | } | |
330 | ||
331 | ||
11fdf7f2 TL |
332 | /* |
333 | ** Emit an "extra argument" instruction (format 'iAx') | |
334 | */ | |
7c673cae FG |
335 | static int codeextraarg (FuncState *fs, int a) { |
336 | lua_assert(a <= MAXARG_Ax); | |
337 | return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, a)); | |
338 | } | |
339 | ||
340 | ||
11fdf7f2 TL |
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 | */ | |
7c673cae FG |
346 | int luaK_codek (FuncState *fs, int reg, int k) { |
347 | if (k <= MAXARG_Bx) | |
348 | return luaK_codeABx(fs, OP_LOADK, reg, k); | |
349 | else { | |
350 | int p = luaK_codeABx(fs, OP_LOADKX, reg, 0); | |
351 | codeextraarg(fs, k); | |
352 | return p; | |
353 | } | |
354 | } | |
355 | ||
356 | ||
11fdf7f2 TL |
357 | /* |
358 | ** Check register-stack level, keeping track of its maximum size | |
359 | ** in field 'maxstacksize' | |
360 | */ | |
7c673cae FG |
361 | void luaK_checkstack (FuncState *fs, int n) { |
362 | int newstack = fs->freereg + n; | |
363 | if (newstack > fs->f->maxstacksize) { | |
364 | if (newstack >= MAXREGS) | |
365 | luaX_syntaxerror(fs->ls, | |
366 | "function or expression needs too many registers"); | |
367 | fs->f->maxstacksize = cast_byte(newstack); | |
368 | } | |
369 | } | |
370 | ||
371 | ||
11fdf7f2 TL |
372 | /* |
373 | ** Reserve 'n' registers in register stack | |
374 | */ | |
7c673cae FG |
375 | void luaK_reserveregs (FuncState *fs, int n) { |
376 | luaK_checkstack(fs, n); | |
377 | fs->freereg += n; | |
378 | } | |
379 | ||
380 | ||
11fdf7f2 TL |
381 | /* |
382 | ** Free register 'reg', if it is neither a constant index nor | |
383 | ** a local variable. | |
384 | ) | |
385 | */ | |
7c673cae FG |
386 | static void freereg (FuncState *fs, int reg) { |
387 | if (!ISK(reg) && reg >= fs->nactvar) { | |
388 | fs->freereg--; | |
389 | lua_assert(reg == fs->freereg); | |
390 | } | |
391 | } | |
392 | ||
393 | ||
11fdf7f2 TL |
394 | /* |
395 | ** Free register used by expression 'e' (if any) | |
396 | */ | |
7c673cae FG |
397 | static void freeexp (FuncState *fs, expdesc *e) { |
398 | if (e->k == VNONRELOC) | |
399 | freereg(fs, e->u.info); | |
400 | } | |
401 | ||
402 | ||
403 | /* | |
11fdf7f2 TL |
404 | ** Free registers used by expressions 'e1' and 'e2' (if any) in proper |
405 | ** order. | |
406 | */ | |
407 | static void freeexps (FuncState *fs, expdesc *e1, expdesc *e2) { | |
408 | int r1 = (e1->k == VNONRELOC) ? e1->u.info : -1; | |
409 | int r2 = (e2->k == VNONRELOC) ? e2->u.info : -1; | |
410 | if (r1 > r2) { | |
411 | freereg(fs, r1); | |
412 | freereg(fs, r2); | |
413 | } | |
414 | else { | |
415 | freereg(fs, r2); | |
416 | freereg(fs, r1); | |
417 | } | |
418 | } | |
419 | ||
420 | ||
421 | /* | |
422 | ** Add constant 'v' to prototype's list of constants (field 'k'). | |
7c673cae | 423 | ** Use scanner's table to cache position of constants in constant list |
11fdf7f2 TL |
424 | ** and try to reuse constants. Because some values should not be used |
425 | ** as keys (nil cannot be a key, integer keys can collapse with float | |
426 | ** keys), the caller must provide a useful 'key' for indexing the cache. | |
7c673cae FG |
427 | */ |
428 | static int addk (FuncState *fs, TValue *key, TValue *v) { | |
429 | lua_State *L = fs->ls->L; | |
430 | Proto *f = fs->f; | |
431 | TValue *idx = luaH_set(L, fs->ls->h, key); /* index scanner table */ | |
432 | int k, oldsize; | |
433 | if (ttisinteger(idx)) { /* is there an index there? */ | |
434 | k = cast_int(ivalue(idx)); | |
435 | /* correct value? (warning: must distinguish floats from integers!) */ | |
436 | if (k < fs->nk && ttype(&f->k[k]) == ttype(v) && | |
437 | luaV_rawequalobj(&f->k[k], v)) | |
438 | return k; /* reuse index */ | |
439 | } | |
440 | /* constant not found; create a new entry */ | |
441 | oldsize = f->sizek; | |
442 | k = fs->nk; | |
443 | /* numerical value does not need GC barrier; | |
444 | table has no metatable, so it does not need to invalidate cache */ | |
445 | setivalue(idx, k); | |
446 | luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants"); | |
447 | while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]); | |
448 | setobj(L, &f->k[k], v); | |
449 | fs->nk++; | |
450 | luaC_barrier(L, f, v); | |
451 | return k; | |
452 | } | |
453 | ||
454 | ||
11fdf7f2 TL |
455 | /* |
456 | ** Add a string to list of constants and return its index. | |
457 | */ | |
7c673cae FG |
458 | int luaK_stringK (FuncState *fs, TString *s) { |
459 | TValue o; | |
460 | setsvalue(fs->ls->L, &o, s); | |
11fdf7f2 | 461 | return addk(fs, &o, &o); /* use string itself as key */ |
7c673cae FG |
462 | } |
463 | ||
464 | ||
465 | /* | |
11fdf7f2 TL |
466 | ** Add an integer to list of constants and return its index. |
467 | ** Integers use userdata as keys to avoid collision with floats with | |
468 | ** same value; conversion to 'void*' is used only for hashing, so there | |
469 | ** are no "precision" problems. | |
7c673cae FG |
470 | */ |
471 | int luaK_intK (FuncState *fs, lua_Integer n) { | |
472 | TValue k, o; | |
473 | setpvalue(&k, cast(void*, cast(size_t, n))); | |
474 | setivalue(&o, n); | |
475 | return addk(fs, &k, &o); | |
476 | } | |
477 | ||
11fdf7f2 TL |
478 | /* |
479 | ** Add a float to list of constants and return its index. | |
480 | */ | |
7c673cae FG |
481 | static int luaK_numberK (FuncState *fs, lua_Number r) { |
482 | TValue o; | |
483 | setfltvalue(&o, r); | |
11fdf7f2 | 484 | return addk(fs, &o, &o); /* use number itself as key */ |
7c673cae FG |
485 | } |
486 | ||
487 | ||
11fdf7f2 TL |
488 | /* |
489 | ** Add a boolean to list of constants and return its index. | |
490 | */ | |
7c673cae FG |
491 | static int boolK (FuncState *fs, int b) { |
492 | TValue o; | |
493 | setbvalue(&o, b); | |
11fdf7f2 | 494 | return addk(fs, &o, &o); /* use boolean itself as key */ |
7c673cae FG |
495 | } |
496 | ||
497 | ||
11fdf7f2 TL |
498 | /* |
499 | ** Add nil to list of constants and return its index. | |
500 | */ | |
7c673cae FG |
501 | static int nilK (FuncState *fs) { |
502 | TValue k, v; | |
503 | setnilvalue(&v); | |
504 | /* cannot use nil as key; instead use table itself to represent nil */ | |
505 | sethvalue(fs->ls->L, &k, fs->ls->h); | |
506 | return addk(fs, &k, &v); | |
507 | } | |
508 | ||
509 | ||
11fdf7f2 TL |
510 | /* |
511 | ** Fix an expression to return the number of results 'nresults'. | |
512 | ** Either 'e' is a multi-ret expression (function call or vararg) | |
513 | ** or 'nresults' is LUA_MULTRET (as any expression can satisfy that). | |
514 | */ | |
7c673cae FG |
515 | void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) { |
516 | if (e->k == VCALL) { /* expression is an open function call? */ | |
11fdf7f2 | 517 | SETARG_C(getinstruction(fs, e), nresults + 1); |
7c673cae FG |
518 | } |
519 | else if (e->k == VVARARG) { | |
11fdf7f2 TL |
520 | Instruction *pc = &getinstruction(fs, e); |
521 | SETARG_B(*pc, nresults + 1); | |
522 | SETARG_A(*pc, fs->freereg); | |
7c673cae FG |
523 | luaK_reserveregs(fs, 1); |
524 | } | |
11fdf7f2 | 525 | else lua_assert(nresults == LUA_MULTRET); |
7c673cae FG |
526 | } |
527 | ||
528 | ||
11fdf7f2 TL |
529 | /* |
530 | ** Fix an expression to return one result. | |
531 | ** If expression is not a multi-ret expression (function call or | |
532 | ** vararg), it already returns one result, so nothing needs to be done. | |
533 | ** Function calls become VNONRELOC expressions (as its result comes | |
534 | ** fixed in the base register of the call), while vararg expressions | |
535 | ** become VRELOCABLE (as OP_VARARG puts its results where it wants). | |
536 | ** (Calls are created returning one result, so that does not need | |
537 | ** to be fixed.) | |
538 | */ | |
7c673cae FG |
539 | void luaK_setoneret (FuncState *fs, expdesc *e) { |
540 | if (e->k == VCALL) { /* expression is an open function call? */ | |
11fdf7f2 TL |
541 | /* already returns 1 value */ |
542 | lua_assert(GETARG_C(getinstruction(fs, e)) == 2); | |
543 | e->k = VNONRELOC; /* result has fixed position */ | |
544 | e->u.info = GETARG_A(getinstruction(fs, e)); | |
7c673cae FG |
545 | } |
546 | else if (e->k == VVARARG) { | |
11fdf7f2 | 547 | SETARG_B(getinstruction(fs, e), 2); |
7c673cae FG |
548 | e->k = VRELOCABLE; /* can relocate its simple result */ |
549 | } | |
550 | } | |
551 | ||
552 | ||
11fdf7f2 TL |
553 | /* |
554 | ** Ensure that expression 'e' is not a variable. | |
555 | */ | |
7c673cae FG |
556 | void luaK_dischargevars (FuncState *fs, expdesc *e) { |
557 | switch (e->k) { | |
11fdf7f2 TL |
558 | case VLOCAL: { /* already in a register */ |
559 | e->k = VNONRELOC; /* becomes a non-relocatable value */ | |
7c673cae FG |
560 | break; |
561 | } | |
11fdf7f2 | 562 | case VUPVAL: { /* move value to some (pending) register */ |
7c673cae FG |
563 | e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0); |
564 | e->k = VRELOCABLE; | |
565 | break; | |
566 | } | |
567 | case VINDEXED: { | |
11fdf7f2 | 568 | OpCode op; |
7c673cae | 569 | freereg(fs, e->u.ind.idx); |
11fdf7f2 | 570 | if (e->u.ind.vt == VLOCAL) { /* is 't' in a register? */ |
7c673cae FG |
571 | freereg(fs, e->u.ind.t); |
572 | op = OP_GETTABLE; | |
573 | } | |
11fdf7f2 TL |
574 | else { |
575 | lua_assert(e->u.ind.vt == VUPVAL); | |
576 | op = OP_GETTABUP; /* 't' is in an upvalue */ | |
577 | } | |
7c673cae FG |
578 | e->u.info = luaK_codeABC(fs, op, 0, e->u.ind.t, e->u.ind.idx); |
579 | e->k = VRELOCABLE; | |
580 | break; | |
581 | } | |
11fdf7f2 | 582 | case VVARARG: case VCALL: { |
7c673cae FG |
583 | luaK_setoneret(fs, e); |
584 | break; | |
585 | } | |
586 | default: break; /* there is one value available (somewhere) */ | |
587 | } | |
588 | } | |
589 | ||
590 | ||
11fdf7f2 TL |
591 | /* |
592 | ** Ensures expression value is in register 'reg' (and therefore | |
593 | ** 'e' will become a non-relocatable expression). | |
594 | */ | |
7c673cae FG |
595 | static void discharge2reg (FuncState *fs, expdesc *e, int reg) { |
596 | luaK_dischargevars(fs, e); | |
597 | switch (e->k) { | |
598 | case VNIL: { | |
599 | luaK_nil(fs, reg, 1); | |
600 | break; | |
601 | } | |
602 | case VFALSE: case VTRUE: { | |
603 | luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0); | |
604 | break; | |
605 | } | |
606 | case VK: { | |
607 | luaK_codek(fs, reg, e->u.info); | |
608 | break; | |
609 | } | |
610 | case VKFLT: { | |
611 | luaK_codek(fs, reg, luaK_numberK(fs, e->u.nval)); | |
612 | break; | |
613 | } | |
614 | case VKINT: { | |
615 | luaK_codek(fs, reg, luaK_intK(fs, e->u.ival)); | |
616 | break; | |
617 | } | |
618 | case VRELOCABLE: { | |
11fdf7f2 TL |
619 | Instruction *pc = &getinstruction(fs, e); |
620 | SETARG_A(*pc, reg); /* instruction will put result in 'reg' */ | |
7c673cae FG |
621 | break; |
622 | } | |
623 | case VNONRELOC: { | |
624 | if (reg != e->u.info) | |
625 | luaK_codeABC(fs, OP_MOVE, reg, e->u.info, 0); | |
626 | break; | |
627 | } | |
628 | default: { | |
11fdf7f2 | 629 | lua_assert(e->k == VJMP); |
7c673cae FG |
630 | return; /* nothing to do... */ |
631 | } | |
632 | } | |
633 | e->u.info = reg; | |
634 | e->k = VNONRELOC; | |
635 | } | |
636 | ||
637 | ||
11fdf7f2 TL |
638 | /* |
639 | ** Ensures expression value is in any register. | |
640 | */ | |
7c673cae | 641 | static void discharge2anyreg (FuncState *fs, expdesc *e) { |
11fdf7f2 TL |
642 | if (e->k != VNONRELOC) { /* no fixed register yet? */ |
643 | luaK_reserveregs(fs, 1); /* get a register */ | |
644 | discharge2reg(fs, e, fs->freereg-1); /* put value there */ | |
7c673cae FG |
645 | } |
646 | } | |
647 | ||
648 | ||
11fdf7f2 TL |
649 | static int code_loadbool (FuncState *fs, int A, int b, int jump) { |
650 | luaK_getlabel(fs); /* those instructions may be jump targets */ | |
651 | return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump); | |
652 | } | |
653 | ||
654 | ||
655 | /* | |
656 | ** check whether list has any jump that do not produce a value | |
657 | ** or produce an inverted value | |
658 | */ | |
659 | static int need_value (FuncState *fs, int list) { | |
660 | for (; list != NO_JUMP; list = getjump(fs, list)) { | |
661 | Instruction i = *getjumpcontrol(fs, list); | |
662 | if (GET_OPCODE(i) != OP_TESTSET) return 1; | |
663 | } | |
664 | return 0; /* not found */ | |
665 | } | |
666 | ||
667 | ||
668 | /* | |
669 | ** Ensures final expression result (including results from its jump | |
670 | ** lists) is in register 'reg'. | |
671 | ** If expression has jumps, need to patch these jumps either to | |
672 | ** its final position or to "load" instructions (for those tests | |
673 | ** that do not produce values). | |
674 | */ | |
7c673cae FG |
675 | static void exp2reg (FuncState *fs, expdesc *e, int reg) { |
676 | discharge2reg(fs, e, reg); | |
11fdf7f2 | 677 | if (e->k == VJMP) /* expression itself is a test? */ |
7c673cae FG |
678 | luaK_concat(fs, &e->t, e->u.info); /* put this jump in 't' list */ |
679 | if (hasjumps(e)) { | |
680 | int final; /* position after whole expression */ | |
681 | int p_f = NO_JUMP; /* position of an eventual LOAD false */ | |
682 | int p_t = NO_JUMP; /* position of an eventual LOAD true */ | |
683 | if (need_value(fs, e->t) || need_value(fs, e->f)) { | |
684 | int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs); | |
11fdf7f2 TL |
685 | p_f = code_loadbool(fs, reg, 0, 1); |
686 | p_t = code_loadbool(fs, reg, 1, 0); | |
7c673cae FG |
687 | luaK_patchtohere(fs, fj); |
688 | } | |
689 | final = luaK_getlabel(fs); | |
690 | patchlistaux(fs, e->f, final, reg, p_f); | |
691 | patchlistaux(fs, e->t, final, reg, p_t); | |
692 | } | |
693 | e->f = e->t = NO_JUMP; | |
694 | e->u.info = reg; | |
695 | e->k = VNONRELOC; | |
696 | } | |
697 | ||
698 | ||
11fdf7f2 TL |
699 | /* |
700 | ** Ensures final expression result (including results from its jump | |
701 | ** lists) is in next available register. | |
702 | */ | |
7c673cae FG |
703 | void luaK_exp2nextreg (FuncState *fs, expdesc *e) { |
704 | luaK_dischargevars(fs, e); | |
705 | freeexp(fs, e); | |
706 | luaK_reserveregs(fs, 1); | |
707 | exp2reg(fs, e, fs->freereg - 1); | |
708 | } | |
709 | ||
710 | ||
11fdf7f2 TL |
711 | /* |
712 | ** Ensures final expression result (including results from its jump | |
713 | ** lists) is in some (any) register and return that register. | |
714 | */ | |
7c673cae FG |
715 | int luaK_exp2anyreg (FuncState *fs, expdesc *e) { |
716 | luaK_dischargevars(fs, e); | |
11fdf7f2 TL |
717 | if (e->k == VNONRELOC) { /* expression already has a register? */ |
718 | if (!hasjumps(e)) /* no jumps? */ | |
719 | return e->u.info; /* result is already in a register */ | |
7c673cae | 720 | if (e->u.info >= fs->nactvar) { /* reg. is not a local? */ |
11fdf7f2 | 721 | exp2reg(fs, e, e->u.info); /* put final result in it */ |
7c673cae FG |
722 | return e->u.info; |
723 | } | |
724 | } | |
11fdf7f2 | 725 | luaK_exp2nextreg(fs, e); /* otherwise, use next available register */ |
7c673cae FG |
726 | return e->u.info; |
727 | } | |
728 | ||
729 | ||
11fdf7f2 TL |
730 | /* |
731 | ** Ensures final expression result is either in a register or in an | |
732 | ** upvalue. | |
733 | */ | |
7c673cae FG |
734 | void luaK_exp2anyregup (FuncState *fs, expdesc *e) { |
735 | if (e->k != VUPVAL || hasjumps(e)) | |
736 | luaK_exp2anyreg(fs, e); | |
737 | } | |
738 | ||
739 | ||
11fdf7f2 TL |
740 | /* |
741 | ** Ensures final expression result is either in a register or it is | |
742 | ** a constant. | |
743 | */ | |
7c673cae FG |
744 | void luaK_exp2val (FuncState *fs, expdesc *e) { |
745 | if (hasjumps(e)) | |
746 | luaK_exp2anyreg(fs, e); | |
747 | else | |
748 | luaK_dischargevars(fs, e); | |
749 | } | |
750 | ||
751 | ||
11fdf7f2 TL |
752 | /* |
753 | ** Ensures final expression result is in a valid R/K index | |
754 | ** (that is, it is either in a register or in 'k' with an index | |
755 | ** in the range of R/K indices). | |
756 | ** Returns R/K index. | |
757 | */ | |
7c673cae FG |
758 | int luaK_exp2RK (FuncState *fs, expdesc *e) { |
759 | luaK_exp2val(fs, e); | |
11fdf7f2 TL |
760 | switch (e->k) { /* move constants to 'k' */ |
761 | case VTRUE: e->u.info = boolK(fs, 1); goto vk; | |
762 | case VFALSE: e->u.info = boolK(fs, 0); goto vk; | |
763 | case VNIL: e->u.info = nilK(fs); goto vk; | |
764 | case VKINT: e->u.info = luaK_intK(fs, e->u.ival); goto vk; | |
765 | case VKFLT: e->u.info = luaK_numberK(fs, e->u.nval); goto vk; | |
766 | case VK: | |
7c673cae | 767 | vk: |
11fdf7f2 | 768 | e->k = VK; |
7c673cae FG |
769 | if (e->u.info <= MAXINDEXRK) /* constant fits in 'argC'? */ |
770 | return RKASK(e->u.info); | |
771 | else break; | |
7c673cae FG |
772 | default: break; |
773 | } | |
774 | /* not a constant in the right range: put it in a register */ | |
775 | return luaK_exp2anyreg(fs, e); | |
776 | } | |
777 | ||
778 | ||
11fdf7f2 TL |
779 | /* |
780 | ** Generate code to store result of expression 'ex' into variable 'var'. | |
781 | */ | |
7c673cae FG |
782 | void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { |
783 | switch (var->k) { | |
784 | case VLOCAL: { | |
785 | freeexp(fs, ex); | |
11fdf7f2 | 786 | exp2reg(fs, ex, var->u.info); /* compute 'ex' into proper place */ |
7c673cae FG |
787 | return; |
788 | } | |
789 | case VUPVAL: { | |
790 | int e = luaK_exp2anyreg(fs, ex); | |
791 | luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0); | |
792 | break; | |
793 | } | |
794 | case VINDEXED: { | |
795 | OpCode op = (var->u.ind.vt == VLOCAL) ? OP_SETTABLE : OP_SETTABUP; | |
796 | int e = luaK_exp2RK(fs, ex); | |
797 | luaK_codeABC(fs, op, var->u.ind.t, var->u.ind.idx, e); | |
798 | break; | |
799 | } | |
11fdf7f2 | 800 | default: lua_assert(0); /* invalid var kind to store */ |
7c673cae FG |
801 | } |
802 | freeexp(fs, ex); | |
803 | } | |
804 | ||
805 | ||
11fdf7f2 TL |
806 | /* |
807 | ** Emit SELF instruction (convert expression 'e' into 'e:key(e,'). | |
808 | */ | |
7c673cae FG |
809 | void luaK_self (FuncState *fs, expdesc *e, expdesc *key) { |
810 | int ereg; | |
811 | luaK_exp2anyreg(fs, e); | |
812 | ereg = e->u.info; /* register where 'e' was placed */ | |
813 | freeexp(fs, e); | |
814 | e->u.info = fs->freereg; /* base register for op_self */ | |
11fdf7f2 | 815 | e->k = VNONRELOC; /* self expression has a fixed register */ |
7c673cae FG |
816 | luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */ |
817 | luaK_codeABC(fs, OP_SELF, e->u.info, ereg, luaK_exp2RK(fs, key)); | |
818 | freeexp(fs, key); | |
819 | } | |
820 | ||
821 | ||
11fdf7f2 TL |
822 | /* |
823 | ** Negate condition 'e' (where 'e' is a comparison). | |
824 | */ | |
825 | static void negatecondition (FuncState *fs, expdesc *e) { | |
7c673cae FG |
826 | Instruction *pc = getjumpcontrol(fs, e->u.info); |
827 | lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET && | |
828 | GET_OPCODE(*pc) != OP_TEST); | |
829 | SETARG_A(*pc, !(GETARG_A(*pc))); | |
830 | } | |
831 | ||
832 | ||
11fdf7f2 TL |
833 | /* |
834 | ** Emit instruction to jump if 'e' is 'cond' (that is, if 'cond' | |
835 | ** is true, code will jump if 'e' is true.) Return jump position. | |
836 | ** Optimize when 'e' is 'not' something, inverting the condition | |
837 | ** and removing the 'not'. | |
838 | */ | |
7c673cae FG |
839 | static int jumponcond (FuncState *fs, expdesc *e, int cond) { |
840 | if (e->k == VRELOCABLE) { | |
11fdf7f2 | 841 | Instruction ie = getinstruction(fs, e); |
7c673cae FG |
842 | if (GET_OPCODE(ie) == OP_NOT) { |
843 | fs->pc--; /* remove previous OP_NOT */ | |
844 | return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond); | |
845 | } | |
846 | /* else go through */ | |
847 | } | |
848 | discharge2anyreg(fs, e); | |
849 | freeexp(fs, e); | |
850 | return condjump(fs, OP_TESTSET, NO_REG, e->u.info, cond); | |
851 | } | |
852 | ||
853 | ||
11fdf7f2 TL |
854 | /* |
855 | ** Emit code to go through if 'e' is true, jump otherwise. | |
856 | */ | |
7c673cae | 857 | void luaK_goiftrue (FuncState *fs, expdesc *e) { |
11fdf7f2 | 858 | int pc; /* pc of new jump */ |
7c673cae FG |
859 | luaK_dischargevars(fs, e); |
860 | switch (e->k) { | |
11fdf7f2 TL |
861 | case VJMP: { /* condition? */ |
862 | negatecondition(fs, e); /* jump when it is false */ | |
863 | pc = e->u.info; /* save jump position */ | |
7c673cae FG |
864 | break; |
865 | } | |
866 | case VK: case VKFLT: case VKINT: case VTRUE: { | |
867 | pc = NO_JUMP; /* always true; do nothing */ | |
868 | break; | |
869 | } | |
870 | default: { | |
11fdf7f2 | 871 | pc = jumponcond(fs, e, 0); /* jump when false */ |
7c673cae FG |
872 | break; |
873 | } | |
874 | } | |
11fdf7f2 TL |
875 | luaK_concat(fs, &e->f, pc); /* insert new jump in false list */ |
876 | luaK_patchtohere(fs, e->t); /* true list jumps to here (to go through) */ | |
7c673cae FG |
877 | e->t = NO_JUMP; |
878 | } | |
879 | ||
880 | ||
11fdf7f2 TL |
881 | /* |
882 | ** Emit code to go through if 'e' is false, jump otherwise. | |
883 | */ | |
7c673cae | 884 | void luaK_goiffalse (FuncState *fs, expdesc *e) { |
11fdf7f2 | 885 | int pc; /* pc of new jump */ |
7c673cae FG |
886 | luaK_dischargevars(fs, e); |
887 | switch (e->k) { | |
888 | case VJMP: { | |
11fdf7f2 | 889 | pc = e->u.info; /* already jump if true */ |
7c673cae FG |
890 | break; |
891 | } | |
892 | case VNIL: case VFALSE: { | |
893 | pc = NO_JUMP; /* always false; do nothing */ | |
894 | break; | |
895 | } | |
896 | default: { | |
11fdf7f2 | 897 | pc = jumponcond(fs, e, 1); /* jump if true */ |
7c673cae FG |
898 | break; |
899 | } | |
900 | } | |
11fdf7f2 TL |
901 | luaK_concat(fs, &e->t, pc); /* insert new jump in 't' list */ |
902 | luaK_patchtohere(fs, e->f); /* false list jumps to here (to go through) */ | |
7c673cae FG |
903 | e->f = NO_JUMP; |
904 | } | |
905 | ||
906 | ||
11fdf7f2 TL |
907 | /* |
908 | ** Code 'not e', doing constant folding. | |
909 | */ | |
7c673cae FG |
910 | static void codenot (FuncState *fs, expdesc *e) { |
911 | luaK_dischargevars(fs, e); | |
912 | switch (e->k) { | |
913 | case VNIL: case VFALSE: { | |
11fdf7f2 | 914 | e->k = VTRUE; /* true == not nil == not false */ |
7c673cae FG |
915 | break; |
916 | } | |
917 | case VK: case VKFLT: case VKINT: case VTRUE: { | |
11fdf7f2 | 918 | e->k = VFALSE; /* false == not "x" == not 0.5 == not 1 == not true */ |
7c673cae FG |
919 | break; |
920 | } | |
921 | case VJMP: { | |
11fdf7f2 | 922 | negatecondition(fs, e); |
7c673cae FG |
923 | break; |
924 | } | |
925 | case VRELOCABLE: | |
926 | case VNONRELOC: { | |
927 | discharge2anyreg(fs, e); | |
928 | freeexp(fs, e); | |
929 | e->u.info = luaK_codeABC(fs, OP_NOT, 0, e->u.info, 0); | |
930 | e->k = VRELOCABLE; | |
931 | break; | |
932 | } | |
11fdf7f2 | 933 | default: lua_assert(0); /* cannot happen */ |
7c673cae FG |
934 | } |
935 | /* interchange true and false lists */ | |
936 | { int temp = e->f; e->f = e->t; e->t = temp; } | |
11fdf7f2 | 937 | removevalues(fs, e->f); /* values are useless when negated */ |
7c673cae FG |
938 | removevalues(fs, e->t); |
939 | } | |
940 | ||
941 | ||
11fdf7f2 TL |
942 | /* |
943 | ** Create expression 't[k]'. 't' must have its final result already in a | |
944 | ** register or upvalue. | |
945 | */ | |
7c673cae | 946 | void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { |
11fdf7f2 TL |
947 | lua_assert(!hasjumps(t) && (vkisinreg(t->k) || t->k == VUPVAL)); |
948 | t->u.ind.t = t->u.info; /* register or upvalue index */ | |
949 | t->u.ind.idx = luaK_exp2RK(fs, k); /* R/K index for key */ | |
950 | t->u.ind.vt = (t->k == VUPVAL) ? VUPVAL : VLOCAL; | |
7c673cae FG |
951 | t->k = VINDEXED; |
952 | } | |
953 | ||
954 | ||
955 | /* | |
11fdf7f2 TL |
956 | ** Return false if folding can raise an error. |
957 | ** Bitwise operations need operands convertible to integers; division | |
958 | ** operations cannot have 0 as divisor. | |
7c673cae FG |
959 | */ |
960 | static int validop (int op, TValue *v1, TValue *v2) { | |
961 | switch (op) { | |
962 | case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR: | |
963 | case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: { /* conversion errors */ | |
964 | lua_Integer i; | |
965 | return (tointeger(v1, &i) && tointeger(v2, &i)); | |
966 | } | |
967 | case LUA_OPDIV: case LUA_OPIDIV: case LUA_OPMOD: /* division by 0 */ | |
968 | return (nvalue(v2) != 0); | |
969 | default: return 1; /* everything else is valid */ | |
970 | } | |
971 | } | |
972 | ||
973 | ||
974 | /* | |
11fdf7f2 TL |
975 | ** Try to "constant-fold" an operation; return 1 iff successful. |
976 | ** (In this case, 'e1' has the final result.) | |
7c673cae FG |
977 | */ |
978 | static int constfolding (FuncState *fs, int op, expdesc *e1, expdesc *e2) { | |
979 | TValue v1, v2, res; | |
980 | if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2)) | |
981 | return 0; /* non-numeric operands or not safe to fold */ | |
982 | luaO_arith(fs->ls->L, op, &v1, &v2, &res); /* does operation */ | |
983 | if (ttisinteger(&res)) { | |
984 | e1->k = VKINT; | |
985 | e1->u.ival = ivalue(&res); | |
986 | } | |
11fdf7f2 | 987 | else { /* folds neither NaN nor 0.0 (to avoid problems with -0.0) */ |
7c673cae FG |
988 | lua_Number n = fltvalue(&res); |
989 | if (luai_numisnan(n) || n == 0) | |
990 | return 0; | |
991 | e1->k = VKFLT; | |
992 | e1->u.nval = n; | |
993 | } | |
994 | return 1; | |
995 | } | |
996 | ||
997 | ||
998 | /* | |
11fdf7f2 TL |
999 | ** Emit code for unary expressions that "produce values" |
1000 | ** (everything but 'not'). | |
1001 | ** Expression to produce final result will be encoded in 'e'. | |
1002 | */ | |
1003 | static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) { | |
1004 | int r = luaK_exp2anyreg(fs, e); /* opcodes operate only on registers */ | |
1005 | freeexp(fs, e); | |
1006 | e->u.info = luaK_codeABC(fs, op, 0, r, 0); /* generate opcode */ | |
1007 | e->k = VRELOCABLE; /* all those operations are relocatable */ | |
1008 | luaK_fixline(fs, line); | |
1009 | } | |
1010 | ||
1011 | ||
1012 | /* | |
1013 | ** Emit code for binary expressions that "produce values" | |
1014 | ** (everything but logical operators 'and'/'or' and comparison | |
1015 | ** operators). | |
7c673cae FG |
1016 | ** Expression to produce final result will be encoded in 'e1'. |
1017 | */ | |
11fdf7f2 TL |
1018 | static void codebinexpval (FuncState *fs, OpCode op, |
1019 | expdesc *e1, expdesc *e2, int line) { | |
1020 | int rk1 = luaK_exp2RK(fs, e1); /* both operands are "RK" */ | |
1021 | int rk2 = luaK_exp2RK(fs, e2); | |
1022 | freeexps(fs, e1, e2); | |
1023 | e1->u.info = luaK_codeABC(fs, op, 0, rk1, rk2); /* generate opcode */ | |
1024 | e1->k = VRELOCABLE; /* all those operations are relocatable */ | |
1025 | luaK_fixline(fs, line); | |
7c673cae FG |
1026 | } |
1027 | ||
1028 | ||
11fdf7f2 TL |
1029 | /* |
1030 | ** Emit code for comparisons. | |
1031 | ** 'e1' was already put in R/K form by 'luaK_infix'. | |
1032 | */ | |
1033 | static void codecomp (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { | |
1034 | int rk1 = (e1->k == VK) ? RKASK(e1->u.info) | |
1035 | : check_exp(e1->k == VNONRELOC, e1->u.info); | |
1036 | int rk2 = luaK_exp2RK(fs, e2); | |
1037 | freeexps(fs, e1, e2); | |
1038 | switch (opr) { | |
1039 | case OPR_NE: { /* '(a ~= b)' ==> 'not (a == b)' */ | |
1040 | e1->u.info = condjump(fs, OP_EQ, 0, rk1, rk2); | |
1041 | break; | |
1042 | } | |
1043 | case OPR_GT: case OPR_GE: { | |
1044 | /* '(a > b)' ==> '(b < a)'; '(a >= b)' ==> '(b <= a)' */ | |
1045 | OpCode op = cast(OpCode, (opr - OPR_NE) + OP_EQ); | |
1046 | e1->u.info = condjump(fs, op, 1, rk2, rk1); /* invert operands */ | |
1047 | break; | |
1048 | } | |
1049 | default: { /* '==', '<', '<=' use their own opcodes */ | |
1050 | OpCode op = cast(OpCode, (opr - OPR_EQ) + OP_EQ); | |
1051 | e1->u.info = condjump(fs, op, 1, rk1, rk2); | |
1052 | break; | |
1053 | } | |
7c673cae | 1054 | } |
7c673cae FG |
1055 | e1->k = VJMP; |
1056 | } | |
1057 | ||
1058 | ||
11fdf7f2 TL |
1059 | /* |
1060 | ** Aplly prefix operation 'op' to expression 'e'. | |
1061 | */ | |
7c673cae | 1062 | void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) { |
11fdf7f2 | 1063 | static expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP}; /* fake 2nd operand */ |
7c673cae | 1064 | switch (op) { |
11fdf7f2 TL |
1065 | case OPR_MINUS: case OPR_BNOT: |
1066 | if (constfolding(fs, op + LUA_OPUNM, e, &ef)) | |
1067 | break; | |
1068 | /* FALLTHROUGH */ | |
1069 | case OPR_LEN: | |
1070 | codeunexpval(fs, cast(OpCode, op + OP_UNM), e, line); | |
7c673cae | 1071 | break; |
7c673cae FG |
1072 | case OPR_NOT: codenot(fs, e); break; |
1073 | default: lua_assert(0); | |
1074 | } | |
1075 | } | |
1076 | ||
1077 | ||
11fdf7f2 TL |
1078 | /* |
1079 | ** Process 1st operand 'v' of binary operation 'op' before reading | |
1080 | ** 2nd operand. | |
1081 | */ | |
7c673cae FG |
1082 | void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { |
1083 | switch (op) { | |
1084 | case OPR_AND: { | |
11fdf7f2 | 1085 | luaK_goiftrue(fs, v); /* go ahead only if 'v' is true */ |
7c673cae FG |
1086 | break; |
1087 | } | |
1088 | case OPR_OR: { | |
11fdf7f2 | 1089 | luaK_goiffalse(fs, v); /* go ahead only if 'v' is false */ |
7c673cae FG |
1090 | break; |
1091 | } | |
1092 | case OPR_CONCAT: { | |
1093 | luaK_exp2nextreg(fs, v); /* operand must be on the 'stack' */ | |
1094 | break; | |
1095 | } | |
1096 | case OPR_ADD: case OPR_SUB: | |
1097 | case OPR_MUL: case OPR_DIV: case OPR_IDIV: | |
1098 | case OPR_MOD: case OPR_POW: | |
1099 | case OPR_BAND: case OPR_BOR: case OPR_BXOR: | |
1100 | case OPR_SHL: case OPR_SHR: { | |
11fdf7f2 TL |
1101 | if (!tonumeral(v, NULL)) |
1102 | luaK_exp2RK(fs, v); | |
1103 | /* else keep numeral, which may be folded with 2nd operand */ | |
7c673cae FG |
1104 | break; |
1105 | } | |
1106 | default: { | |
1107 | luaK_exp2RK(fs, v); | |
1108 | break; | |
1109 | } | |
1110 | } | |
1111 | } | |
1112 | ||
1113 | ||
11fdf7f2 TL |
1114 | /* |
1115 | ** Finalize code for binary operation, after reading 2nd operand. | |
1116 | ** For '(a .. b .. c)' (which is '(a .. (b .. c))', because | |
1117 | ** concatenation is right associative), merge second CONCAT into first | |
1118 | ** one. | |
1119 | */ | |
7c673cae FG |
1120 | void luaK_posfix (FuncState *fs, BinOpr op, |
1121 | expdesc *e1, expdesc *e2, int line) { | |
1122 | switch (op) { | |
1123 | case OPR_AND: { | |
11fdf7f2 | 1124 | lua_assert(e1->t == NO_JUMP); /* list closed by 'luK_infix' */ |
7c673cae FG |
1125 | luaK_dischargevars(fs, e2); |
1126 | luaK_concat(fs, &e2->f, e1->f); | |
1127 | *e1 = *e2; | |
1128 | break; | |
1129 | } | |
1130 | case OPR_OR: { | |
11fdf7f2 | 1131 | lua_assert(e1->f == NO_JUMP); /* list closed by 'luK_infix' */ |
7c673cae FG |
1132 | luaK_dischargevars(fs, e2); |
1133 | luaK_concat(fs, &e2->t, e1->t); | |
1134 | *e1 = *e2; | |
1135 | break; | |
1136 | } | |
1137 | case OPR_CONCAT: { | |
1138 | luaK_exp2val(fs, e2); | |
11fdf7f2 TL |
1139 | if (e2->k == VRELOCABLE && |
1140 | GET_OPCODE(getinstruction(fs, e2)) == OP_CONCAT) { | |
1141 | lua_assert(e1->u.info == GETARG_B(getinstruction(fs, e2))-1); | |
7c673cae | 1142 | freeexp(fs, e1); |
11fdf7f2 | 1143 | SETARG_B(getinstruction(fs, e2), e1->u.info); |
7c673cae FG |
1144 | e1->k = VRELOCABLE; e1->u.info = e2->u.info; |
1145 | } | |
1146 | else { | |
1147 | luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */ | |
11fdf7f2 | 1148 | codebinexpval(fs, OP_CONCAT, e1, e2, line); |
7c673cae FG |
1149 | } |
1150 | break; | |
1151 | } | |
1152 | case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV: | |
1153 | case OPR_IDIV: case OPR_MOD: case OPR_POW: | |
1154 | case OPR_BAND: case OPR_BOR: case OPR_BXOR: | |
1155 | case OPR_SHL: case OPR_SHR: { | |
11fdf7f2 TL |
1156 | if (!constfolding(fs, op + LUA_OPADD, e1, e2)) |
1157 | codebinexpval(fs, cast(OpCode, op + OP_ADD), e1, e2, line); | |
7c673cae FG |
1158 | break; |
1159 | } | |
11fdf7f2 | 1160 | case OPR_EQ: case OPR_LT: case OPR_LE: |
7c673cae | 1161 | case OPR_NE: case OPR_GT: case OPR_GE: { |
11fdf7f2 | 1162 | codecomp(fs, op, e1, e2); |
7c673cae FG |
1163 | break; |
1164 | } | |
1165 | default: lua_assert(0); | |
1166 | } | |
1167 | } | |
1168 | ||
1169 | ||
11fdf7f2 TL |
1170 | /* |
1171 | ** Change line information associated with current position. | |
1172 | */ | |
7c673cae FG |
1173 | void luaK_fixline (FuncState *fs, int line) { |
1174 | fs->f->lineinfo[fs->pc - 1] = line; | |
1175 | } | |
1176 | ||
1177 | ||
11fdf7f2 TL |
1178 | /* |
1179 | ** Emit a SETLIST instruction. | |
1180 | ** 'base' is register that keeps table; | |
1181 | ** 'nelems' is #table plus those to be stored now; | |
1182 | ** 'tostore' is number of values (in registers 'base + 1',...) to add to | |
1183 | ** table (or LUA_MULTRET to add up to stack top). | |
1184 | */ | |
7c673cae FG |
1185 | void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) { |
1186 | int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1; | |
1187 | int b = (tostore == LUA_MULTRET) ? 0 : tostore; | |
11fdf7f2 | 1188 | lua_assert(tostore != 0 && tostore <= LFIELDS_PER_FLUSH); |
7c673cae FG |
1189 | if (c <= MAXARG_C) |
1190 | luaK_codeABC(fs, OP_SETLIST, base, b, c); | |
1191 | else if (c <= MAXARG_Ax) { | |
1192 | luaK_codeABC(fs, OP_SETLIST, base, b, 0); | |
1193 | codeextraarg(fs, c); | |
1194 | } | |
1195 | else | |
1196 | luaX_syntaxerror(fs->ls, "constructor too long"); | |
1197 | fs->freereg = base + 1; /* free registers with list values */ | |
1198 | } | |
1199 |