/src/testdir/build/lua-master/source/lcode.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | ** $Id: lcode.c $ |
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 <float.h> |
14 | | #include <limits.h> |
15 | | #include <math.h> |
16 | | #include <stdlib.h> |
17 | | |
18 | | #include "lua.h" |
19 | | |
20 | | #include "lcode.h" |
21 | | #include "ldebug.h" |
22 | | #include "ldo.h" |
23 | | #include "lgc.h" |
24 | | #include "llex.h" |
25 | | #include "lmem.h" |
26 | | #include "lobject.h" |
27 | | #include "lopcodes.h" |
28 | | #include "lparser.h" |
29 | | #include "lstring.h" |
30 | | #include "ltable.h" |
31 | | #include "lvm.h" |
32 | | |
33 | | |
34 | | /* Maximum number of registers in a Lua function (must fit in 8 bits) */ |
35 | 35.5k | #define MAXREGS 255 |
36 | | |
37 | | |
38 | 108M | #define hasjumps(e) ((e)->t != (e)->f) |
39 | | |
40 | | |
41 | | static int codesJ (FuncState *fs, OpCode o, int sj, int k); |
42 | | |
43 | | |
44 | | |
45 | | /* semantic error */ |
46 | 8 | l_noret luaK_semerror (LexState *ls, const char *msg) { |
47 | 8 | ls->t.token = 0; /* remove "near <token>" from final message */ |
48 | 8 | luaX_syntaxerror(ls, msg); |
49 | 8 | } |
50 | | |
51 | | |
52 | | /* |
53 | | ** If expression is a numeric constant, fills 'v' with its value |
54 | | ** and returns 1. Otherwise, returns 0. |
55 | | */ |
56 | 18.9M | static int tonumeral (const expdesc *e, TValue *v) { |
57 | 18.9M | if (hasjumps(e)) |
58 | 8.41k | return 0; /* not a numeral */ |
59 | 18.9M | switch (e->k) { |
60 | 534k | case VKINT: |
61 | 534k | if (v) setivalue(v, e->u.ival); |
62 | 534k | return 1; |
63 | 162k | case VKFLT: |
64 | 162k | if (v) setfltvalue(v, e->u.nval); |
65 | 162k | return 1; |
66 | 18.2M | default: return 0; |
67 | 18.9M | } |
68 | 18.9M | } |
69 | | |
70 | | |
71 | | /* |
72 | | ** Get the constant value from a constant expression |
73 | | */ |
74 | 531k | static TValue *const2val (FuncState *fs, const expdesc *e) { |
75 | 531k | lua_assert(e->k == VCONST); |
76 | 531k | return &fs->ls->dyd->actvar.arr[e->u.info].k; |
77 | 531k | } |
78 | | |
79 | | |
80 | | /* |
81 | | ** If expression is a constant, fills 'v' with its value |
82 | | ** and returns 1. Otherwise, returns 0. |
83 | | */ |
84 | 2.32k | int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v) { |
85 | 2.32k | if (hasjumps(e)) |
86 | 14 | return 0; /* not a constant */ |
87 | 2.31k | switch (e->k) { |
88 | 269 | case VFALSE: |
89 | 269 | setbfvalue(v); |
90 | 269 | return 1; |
91 | 210 | case VTRUE: |
92 | 210 | setbtvalue(v); |
93 | 210 | return 1; |
94 | 145 | case VNIL: |
95 | 145 | setnilvalue(v); |
96 | 145 | return 1; |
97 | 72 | case VKSTR: { |
98 | 72 | setsvalue(fs->ls->L, v, e->u.strval); |
99 | 72 | return 1; |
100 | 0 | } |
101 | 21 | case VCONST: { |
102 | 21 | setobj(fs->ls->L, v, const2val(fs, e)); |
103 | 21 | return 1; |
104 | 0 | } |
105 | 1.59k | default: return tonumeral(e, v); |
106 | 2.31k | } |
107 | 2.31k | } |
108 | | |
109 | | |
110 | | /* |
111 | | ** Return the previous instruction of the current code. If there |
112 | | ** may be a jump target between the current instruction and the |
113 | | ** previous one, return an invalid instruction (to avoid wrong |
114 | | ** optimizations). |
115 | | */ |
116 | 197k | static Instruction *previousinstruction (FuncState *fs) { |
117 | 197k | static const Instruction invalidinstruction = ~(Instruction)0; |
118 | 197k | if (fs->pc > fs->lasttarget) |
119 | 59.0k | return &fs->f->code[fs->pc - 1]; /* previous instruction */ |
120 | 138k | else |
121 | 138k | return cast(Instruction*, &invalidinstruction); |
122 | 197k | } |
123 | | |
124 | | |
125 | | /* |
126 | | ** Create a OP_LOADNIL instruction, but try to optimize: if the previous |
127 | | ** instruction is also OP_LOADNIL and ranges are compatible, adjust |
128 | | ** range of previous instruction instead of emitting a new one. (For |
129 | | ** instance, 'local a; local b' will generate a single opcode.) |
130 | | */ |
131 | 179k | void luaK_nil (FuncState *fs, int from, int n) { |
132 | 179k | int l = from + n - 1; /* last register to set nil */ |
133 | 179k | Instruction *previous = previousinstruction(fs); |
134 | 179k | if (GET_OPCODE(*previous) == OP_LOADNIL) { /* previous is LOADNIL? */ |
135 | 578 | int pfrom = GETARG_A(*previous); /* get previous range */ |
136 | 578 | int pl = pfrom + GETARG_B(*previous); |
137 | 578 | if ((pfrom <= from && from <= pl + 1) || |
138 | 578 | (from <= pfrom && pfrom <= l + 1)) { /* can connect both? */ |
139 | 512 | if (pfrom < from) from = pfrom; /* from = min(from, pfrom) */ |
140 | 512 | if (pl > l) l = pl; /* l = max(l, pl) */ |
141 | 512 | SETARG_A(*previous, from); |
142 | 512 | SETARG_B(*previous, l - from); |
143 | 512 | return; |
144 | 512 | } /* else go through */ |
145 | 578 | } |
146 | 178k | luaK_codeABC(fs, OP_LOADNIL, from, n - 1, 0); /* else no optimization */ |
147 | 178k | } |
148 | | |
149 | | |
150 | | /* |
151 | | ** Gets the destination address of a jump instruction. Used to traverse |
152 | | ** a list of jumps. |
153 | | */ |
154 | 64.2M | static int getjump (FuncState *fs, int pc) { |
155 | 64.2M | int offset = GETARG_sJ(fs->f->code[pc]); |
156 | 64.2M | if (offset == NO_JUMP) /* point to itself represents end of list */ |
157 | 6.55M | return NO_JUMP; /* end of list */ |
158 | 57.7M | else |
159 | 57.7M | return (pc+1)+offset; /* turn offset into absolute position */ |
160 | 64.2M | } |
161 | | |
162 | | |
163 | | /* |
164 | | ** Fix jump instruction at position 'pc' to jump to 'dest'. |
165 | | ** (Jump addresses are relative in Lua) |
166 | | */ |
167 | 6.91M | static void fixjump (FuncState *fs, int pc, int dest) { |
168 | 6.91M | Instruction *jmp = &fs->f->code[pc]; |
169 | 6.91M | int offset = dest - (pc + 1); |
170 | 6.91M | lua_assert(dest != NO_JUMP); |
171 | 6.91M | if (!(-OFFSET_sJ <= offset && offset <= MAXARG_sJ - OFFSET_sJ)) |
172 | 0 | luaX_syntaxerror(fs->ls, "control structure too long"); |
173 | 6.91M | lua_assert(GET_OPCODE(*jmp) == OP_JMP); |
174 | 6.91M | SETARG_sJ(*jmp, offset); |
175 | 6.91M | } |
176 | | |
177 | | |
178 | | /* |
179 | | ** Concatenate jump-list 'l2' into jump-list 'l1' |
180 | | */ |
181 | 6.73M | void luaK_concat (FuncState *fs, int *l1, int l2) { |
182 | 6.73M | if (l2 == NO_JUMP) return; /* nothing to concatenate? */ |
183 | 6.73M | else if (*l1 == NO_JUMP) /* no original list? */ |
184 | 6.55M | *l1 = l2; /* 'l1' points to 'l2' */ |
185 | 174k | else { |
186 | 174k | int list = *l1; |
187 | 174k | int next; |
188 | 57.5M | while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */ |
189 | 57.4M | list = next; |
190 | 174k | fixjump(fs, list, l2); /* last element links to 'l2' */ |
191 | 174k | } |
192 | 6.73M | } |
193 | | |
194 | | |
195 | | /* |
196 | | ** Create a jump instruction and return its position, so its destination |
197 | | ** can be fixed later (with 'fixjump'). |
198 | | */ |
199 | 6.54M | int luaK_jump (FuncState *fs) { |
200 | 6.54M | return codesJ(fs, OP_JMP, NO_JUMP, 0); |
201 | 6.54M | } |
202 | | |
203 | | |
204 | | /* |
205 | | ** Code a 'return' instruction |
206 | | */ |
207 | 5.73k | void luaK_ret (FuncState *fs, int first, int nret) { |
208 | 5.73k | OpCode op; |
209 | 5.73k | switch (nret) { |
210 | 2.82k | case 0: op = OP_RETURN0; break; |
211 | 864 | case 1: op = OP_RETURN1; break; |
212 | 2.04k | default: op = OP_RETURN; break; |
213 | 5.73k | } |
214 | 5.73k | luaK_codeABC(fs, op, first, nret + 1, 0); |
215 | 5.73k | } |
216 | | |
217 | | |
218 | | /* |
219 | | ** Code a "conditional jump", that is, a test or comparison opcode |
220 | | ** followed by a jump. Return jump position. |
221 | | */ |
222 | 6.51M | static int condjump (FuncState *fs, OpCode op, int A, int B, int C, int k) { |
223 | 6.51M | luaK_codeABCk(fs, op, A, B, C, k); |
224 | 6.51M | return luaK_jump(fs); |
225 | 6.51M | } |
226 | | |
227 | | |
228 | | /* |
229 | | ** returns current 'pc' and marks it as a jump target (to avoid wrong |
230 | | ** optimizations with consecutive instructions not in the same basic block). |
231 | | */ |
232 | 25.4M | int luaK_getlabel (FuncState *fs) { |
233 | 25.4M | fs->lasttarget = fs->pc; |
234 | 25.4M | return fs->pc; |
235 | 25.4M | } |
236 | | |
237 | | |
238 | | /* |
239 | | ** Returns the position of the instruction "controlling" a given |
240 | | ** jump (that is, its condition), or the jump itself if it is |
241 | | ** unconditional. |
242 | | */ |
243 | 12.9M | static Instruction *getjumpcontrol (FuncState *fs, int pc) { |
244 | 12.9M | Instruction *pi = &fs->f->code[pc]; |
245 | 12.9M | if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1)))) |
246 | 12.9M | return pi-1; |
247 | 25.5k | else |
248 | 25.5k | return pi; |
249 | 12.9M | } |
250 | | |
251 | | |
252 | | /* |
253 | | ** Patch destination register for a TESTSET instruction. |
254 | | ** If instruction in position 'node' is not a TESTSET, return 0 ("fails"). |
255 | | ** Otherwise, if 'reg' is not 'NO_REG', set it as the destination |
256 | | ** register. Otherwise, change instruction to a simple 'TEST' (produces |
257 | | ** no register value) |
258 | | */ |
259 | 6.63M | static int patchtestreg (FuncState *fs, int node, int reg) { |
260 | 6.63M | Instruction *i = getjumpcontrol(fs, node); |
261 | 6.63M | if (GET_OPCODE(*i) != OP_TESTSET) |
262 | 6.59M | return 0; /* cannot patch other instructions */ |
263 | 42.1k | if (reg != NO_REG && reg != GETARG_B(*i)) |
264 | 42.1k | SETARG_A(*i, reg); |
265 | 40.6k | else { |
266 | | /* no register to put value or register already has the value; |
267 | | change instruction to simple test */ |
268 | 40.6k | *i = CREATE_ABCk(OP_TEST, GETARG_B(*i), 0, 0, GETARG_k(*i)); |
269 | 40.6k | } |
270 | 42.1k | return 1; |
271 | 6.63M | } |
272 | | |
273 | | |
274 | | /* |
275 | | ** Traverse a list of tests ensuring no one produces a value |
276 | | */ |
277 | 12.6k | static void removevalues (FuncState *fs, int list) { |
278 | 137k | for (; list != NO_JUMP; list = getjump(fs, list)) |
279 | 125k | patchtestreg(fs, list, NO_REG); |
280 | 12.6k | } |
281 | | |
282 | | |
283 | | /* |
284 | | ** Traverse a list of tests, patching their destination address and |
285 | | ** registers: tests producing values jump to 'vtarget' (and put their |
286 | | ** values in 'reg'), other tests jump to 'dtarget'. |
287 | | */ |
288 | | static void patchlistaux (FuncState *fs, int list, int vtarget, int reg, |
289 | 19.1M | int dtarget) { |
290 | 25.6M | while (list != NO_JUMP) { |
291 | 6.51M | int next = getjump(fs, list); |
292 | 6.51M | if (patchtestreg(fs, list, reg)) |
293 | 41.3k | fixjump(fs, list, vtarget); |
294 | 6.47M | else |
295 | 6.47M | fixjump(fs, list, dtarget); /* jump to default target */ |
296 | 6.51M | list = next; |
297 | 6.51M | } |
298 | 19.1M | } |
299 | | |
300 | | |
301 | | /* |
302 | | ** Path all jumps in 'list' to jump to 'target'. |
303 | | ** (The assert means that we cannot fix a jump to a forward address |
304 | | ** because we only know addresses once code is generated.) |
305 | | */ |
306 | 6.54M | void luaK_patchlist (FuncState *fs, int list, int target) { |
307 | 6.54M | lua_assert(target <= fs->pc); |
308 | 6.54M | patchlistaux(fs, list, target, NO_REG, target); |
309 | 6.54M | } |
310 | | |
311 | | |
312 | 6.53M | void luaK_patchtohere (FuncState *fs, int list) { |
313 | 6.53M | int hr = luaK_getlabel(fs); /* mark "here" as a jump target */ |
314 | 6.53M | luaK_patchlist(fs, list, hr); |
315 | 6.53M | } |
316 | | |
317 | | |
318 | | /* limit for difference between lines in relative line info. */ |
319 | 143M | #define LIMLINEDIFF 0x80 |
320 | | |
321 | | |
322 | | /* |
323 | | ** Save line info for a new instruction. If difference from last line |
324 | | ** does not fit in a byte, of after that many instructions, save a new |
325 | | ** absolute line info; (in that case, the special value 'ABSLINEINFO' |
326 | | ** in 'lineinfo' signals the existence of this absolute information.) |
327 | | ** Otherwise, store the difference from last line in 'lineinfo'. |
328 | | */ |
329 | 71.7M | static void savelineinfo (FuncState *fs, Proto *f, int line) { |
330 | 71.7M | int linedif = line - fs->previousline; |
331 | 71.7M | int pc = fs->pc - 1; /* last instruction coded */ |
332 | 71.7M | if (abs(linedif) >= LIMLINEDIFF || fs->iwthabs++ >= MAXIWTHABS) { |
333 | 560k | luaM_growvector(fs->ls->L, f->abslineinfo, fs->nabslineinfo, |
334 | 560k | f->sizeabslineinfo, AbsLineInfo, MAX_INT, "lines"); |
335 | 560k | f->abslineinfo[fs->nabslineinfo].pc = pc; |
336 | 560k | f->abslineinfo[fs->nabslineinfo++].line = line; |
337 | 560k | linedif = ABSLINEINFO; /* signal that there is absolute information */ |
338 | 560k | fs->iwthabs = 1; /* restart counter */ |
339 | 560k | } |
340 | 71.7M | luaM_growvector(fs->ls->L, f->lineinfo, pc, f->sizelineinfo, ls_byte, |
341 | 71.7M | MAX_INT, "opcodes"); |
342 | 71.7M | f->lineinfo[pc] = linedif; |
343 | 71.7M | fs->previousline = line; /* last line saved */ |
344 | 71.7M | } |
345 | | |
346 | | |
347 | | /* |
348 | | ** Remove line information from the last instruction. |
349 | | ** If line information for that instruction is absolute, set 'iwthabs' |
350 | | ** above its max to force the new (replacing) instruction to have |
351 | | ** absolute line info, too. |
352 | | */ |
353 | 13.1M | static void removelastlineinfo (FuncState *fs) { |
354 | 13.1M | Proto *f = fs->f; |
355 | 13.1M | int pc = fs->pc - 1; /* last instruction coded */ |
356 | 13.1M | if (f->lineinfo[pc] != ABSLINEINFO) { /* relative line info? */ |
357 | 13.0M | fs->previousline -= f->lineinfo[pc]; /* correct last line saved */ |
358 | 13.0M | fs->iwthabs--; /* undo previous increment */ |
359 | 13.0M | } |
360 | 102k | else { /* absolute line information */ |
361 | 102k | lua_assert(f->abslineinfo[fs->nabslineinfo - 1].pc == pc); |
362 | 102k | fs->nabslineinfo--; /* remove it */ |
363 | 102k | fs->iwthabs = MAXIWTHABS + 1; /* force next line info to be absolute */ |
364 | 102k | } |
365 | 13.1M | } |
366 | | |
367 | | |
368 | | /* |
369 | | ** Remove the last instruction created, correcting line information |
370 | | ** accordingly. |
371 | | */ |
372 | 1.44k | static void removelastinstruction (FuncState *fs) { |
373 | 1.44k | removelastlineinfo(fs); |
374 | 1.44k | fs->pc--; |
375 | 1.44k | } |
376 | | |
377 | | |
378 | | /* |
379 | | ** Emit instruction 'i', checking for array sizes and saving also its |
380 | | ** line information. Return 'i' position. |
381 | | */ |
382 | 58.6M | int luaK_code (FuncState *fs, Instruction i) { |
383 | 58.6M | Proto *f = fs->f; |
384 | | /* put new instruction in code array */ |
385 | 58.6M | luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction, |
386 | 58.6M | MAX_INT, "opcodes"); |
387 | 58.6M | f->code[fs->pc++] = i; |
388 | 58.6M | savelineinfo(fs, f, fs->ls->lastline); |
389 | 58.6M | return fs->pc - 1; /* index of new instruction */ |
390 | 58.6M | } |
391 | | |
392 | | |
393 | | /* |
394 | | ** Format and emit an 'iABC' instruction. (Assertions check consistency |
395 | | ** of parameters versus opcode.) |
396 | | */ |
397 | 48.4M | int luaK_codeABCk (FuncState *fs, OpCode o, int a, int b, int c, int k) { |
398 | 48.4M | lua_assert(getOpMode(o) == iABC); |
399 | 48.4M | lua_assert(a <= MAXARG_A && b <= MAXARG_B && |
400 | 48.4M | c <= MAXARG_C && (k & ~1) == 0); |
401 | 48.4M | return luaK_code(fs, CREATE_ABCk(o, a, b, c, k)); |
402 | 48.4M | } |
403 | | |
404 | | |
405 | | /* |
406 | | ** Format and emit an 'iABx' instruction. |
407 | | */ |
408 | 3.56M | int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) { |
409 | 3.56M | lua_assert(getOpMode(o) == iABx); |
410 | 3.56M | lua_assert(a <= MAXARG_A && bc <= MAXARG_Bx); |
411 | 3.56M | return luaK_code(fs, CREATE_ABx(o, a, bc)); |
412 | 3.56M | } |
413 | | |
414 | | |
415 | | /* |
416 | | ** Format and emit an 'iAsBx' instruction. |
417 | | */ |
418 | 72.4k | static int codeAsBx (FuncState *fs, OpCode o, int a, int bc) { |
419 | 72.4k | unsigned int b = bc + OFFSET_sBx; |
420 | 72.4k | lua_assert(getOpMode(o) == iAsBx); |
421 | 72.4k | lua_assert(a <= MAXARG_A && b <= MAXARG_Bx); |
422 | 72.4k | return luaK_code(fs, CREATE_ABx(o, a, b)); |
423 | 72.4k | } |
424 | | |
425 | | |
426 | | /* |
427 | | ** Format and emit an 'isJ' instruction. |
428 | | */ |
429 | 6.54M | static int codesJ (FuncState *fs, OpCode o, int sj, int k) { |
430 | 6.54M | unsigned int j = sj + OFFSET_sJ; |
431 | 6.54M | lua_assert(getOpMode(o) == isJ); |
432 | 6.54M | lua_assert(j <= MAXARG_sJ && (k & ~1) == 0); |
433 | 6.54M | return luaK_code(fs, CREATE_sJ(o, j, k)); |
434 | 6.54M | } |
435 | | |
436 | | |
437 | | /* |
438 | | ** Emit an "extra argument" instruction (format 'iAx') |
439 | | */ |
440 | 0 | static int codeextraarg (FuncState *fs, int a) { |
441 | 0 | lua_assert(a <= MAXARG_Ax); |
442 | 0 | return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, a)); |
443 | 0 | } |
444 | | |
445 | | |
446 | | /* |
447 | | ** Emit a "load constant" instruction, using either 'OP_LOADK' |
448 | | ** (if constant index 'k' fits in 18 bits) or an 'OP_LOADKX' |
449 | | ** instruction with "extra argument". |
450 | | */ |
451 | 3.55M | static int luaK_codek (FuncState *fs, int reg, int k) { |
452 | 3.55M | if (k <= MAXARG_Bx) |
453 | 3.55M | return luaK_codeABx(fs, OP_LOADK, reg, k); |
454 | 0 | else { |
455 | 0 | int p = luaK_codeABx(fs, OP_LOADKX, reg, 0); |
456 | 0 | codeextraarg(fs, k); |
457 | 0 | return p; |
458 | 0 | } |
459 | 3.55M | } |
460 | | |
461 | | |
462 | | /* |
463 | | ** Check register-stack level, keeping track of its maximum size |
464 | | ** in field 'maxstacksize' |
465 | | */ |
466 | 32.8M | void luaK_checkstack (FuncState *fs, int n) { |
467 | 32.8M | int newstack = fs->freereg + n; |
468 | 32.8M | if (newstack > fs->f->maxstacksize) { |
469 | 35.5k | if (newstack >= MAXREGS) |
470 | 0 | luaX_syntaxerror(fs->ls, |
471 | 0 | "function or expression needs too many registers"); |
472 | 35.5k | fs->f->maxstacksize = cast_byte(newstack); |
473 | 35.5k | } |
474 | 32.8M | } |
475 | | |
476 | | |
477 | | /* |
478 | | ** Reserve 'n' registers in register stack |
479 | | */ |
480 | 32.8M | void luaK_reserveregs (FuncState *fs, int n) { |
481 | 32.8M | luaK_checkstack(fs, n); |
482 | 32.8M | fs->freereg += n; |
483 | 32.8M | } |
484 | | |
485 | | |
486 | | /* |
487 | | ** Free register 'reg', if it is neither a constant index nor |
488 | | ** a local variable. |
489 | | ) |
490 | | */ |
491 | 32.5M | static void freereg (FuncState *fs, int reg) { |
492 | 32.5M | if (reg >= luaY_nvarstack(fs)) { |
493 | 32.3M | fs->freereg--; |
494 | 32.3M | lua_assert(reg == fs->freereg); |
495 | 32.3M | } |
496 | 32.5M | } |
497 | | |
498 | | |
499 | | /* |
500 | | ** Free two registers in proper order |
501 | | */ |
502 | 15.6M | static void freeregs (FuncState *fs, int r1, int r2) { |
503 | 15.6M | if (r1 > r2) { |
504 | 182k | freereg(fs, r1); |
505 | 182k | freereg(fs, r2); |
506 | 182k | } |
507 | 15.4M | else { |
508 | 15.4M | freereg(fs, r2); |
509 | 15.4M | freereg(fs, r1); |
510 | 15.4M | } |
511 | 15.6M | } |
512 | | |
513 | | |
514 | | /* |
515 | | ** Free register used by expression 'e' (if any) |
516 | | */ |
517 | 33.1M | static void freeexp (FuncState *fs, expdesc *e) { |
518 | 33.1M | if (e->k == VNONRELOC) |
519 | 623k | freereg(fs, e->u.info); |
520 | 33.1M | } |
521 | | |
522 | | |
523 | | /* |
524 | | ** Free registers used by expressions 'e1' and 'e2' (if any) in proper |
525 | | ** order. |
526 | | */ |
527 | 12.6M | static void freeexps (FuncState *fs, expdesc *e1, expdesc *e2) { |
528 | 12.6M | int r1 = (e1->k == VNONRELOC) ? e1->u.info : -1; |
529 | 12.6M | int r2 = (e2->k == VNONRELOC) ? e2->u.info : -1; |
530 | 12.6M | freeregs(fs, r1, r2); |
531 | 12.6M | } |
532 | | |
533 | | |
534 | | /* |
535 | | ** Add constant 'v' to prototype's list of constants (field 'k'). |
536 | | ** Use scanner's table to cache position of constants in constant list |
537 | | ** and try to reuse constants. Because some values should not be used |
538 | | ** as keys (nil cannot be a key, integer keys can collapse with float |
539 | | ** keys), the caller must provide a useful 'key' for indexing the cache. |
540 | | ** Note that all functions share the same table, so entering or exiting |
541 | | ** a function can make some indices wrong. |
542 | | */ |
543 | 13.3M | static int addk (FuncState *fs, TValue *key, TValue *v) { |
544 | 13.3M | TValue val; |
545 | 13.3M | lua_State *L = fs->ls->L; |
546 | 13.3M | Proto *f = fs->f; |
547 | 13.3M | const TValue *idx = luaH_get(fs->ls->h, key); /* query scanner table */ |
548 | 13.3M | int k, oldsize; |
549 | 13.3M | if (ttisinteger(idx)) { /* is there an index there? */ |
550 | 13.3M | k = cast_int(ivalue(idx)); |
551 | | /* correct value? (warning: must distinguish floats from integers!) */ |
552 | 13.3M | if (k < fs->nk && ttypetag(&f->k[k]) == ttypetag(v) && |
553 | 13.3M | luaV_rawequalobj(&f->k[k], v)) |
554 | 13.2M | return k; /* reuse index */ |
555 | 13.3M | } |
556 | | /* constant not found; create a new entry */ |
557 | 89.8k | oldsize = f->sizek; |
558 | 89.8k | k = fs->nk; |
559 | | /* numerical value does not need GC barrier; |
560 | | table has no metatable, so it does not need to invalidate cache */ |
561 | 89.8k | setivalue(&val, k); |
562 | 89.8k | luaH_finishset(L, fs->ls->h, key, idx, &val); |
563 | 89.8k | luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants"); |
564 | 218k | while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]); |
565 | 89.8k | setobj(L, &f->k[k], v); |
566 | 89.8k | fs->nk++; |
567 | 89.8k | luaC_barrier(L, f, v); |
568 | 89.8k | return k; |
569 | 13.3M | } |
570 | | |
571 | | |
572 | | /* |
573 | | ** Add a string to list of constants and return its index. |
574 | | */ |
575 | 13.2M | static int stringK (FuncState *fs, TString *s) { |
576 | 13.2M | TValue o; |
577 | 13.2M | setsvalue(fs->ls->L, &o, s); |
578 | 13.2M | return addk(fs, &o, &o); /* use string itself as key */ |
579 | 13.2M | } |
580 | | |
581 | | |
582 | | /* |
583 | | ** Add an integer to list of constants and return its index. |
584 | | */ |
585 | 61.4k | static int luaK_intK (FuncState *fs, lua_Integer n) { |
586 | 61.4k | TValue o; |
587 | 61.4k | setivalue(&o, n); |
588 | 61.4k | return addk(fs, &o, &o); /* use integer itself as key */ |
589 | 61.4k | } |
590 | | |
591 | | /* |
592 | | ** Add a float to list of constants and return its index. Floats |
593 | | ** with integral values need a different key, to avoid collision |
594 | | ** with actual integers. To that, we add to the number its smaller |
595 | | ** power-of-two fraction that is still significant in its scale. |
596 | | ** For doubles, that would be 1/2^52. |
597 | | ** (This method is not bulletproof: there may be another float |
598 | | ** with that value, and for floats larger than 2^53 the result is |
599 | | ** still an integer. At worst, this only wastes an entry with |
600 | | ** a duplicate.) |
601 | | */ |
602 | 76.4k | static int luaK_numberK (FuncState *fs, lua_Number r) { |
603 | 76.4k | TValue o; |
604 | 76.4k | lua_Integer ik; |
605 | 76.4k | setfltvalue(&o, r); |
606 | 76.4k | if (!luaV_flttointeger(r, &ik, F2Ieq)) /* not an integral value? */ |
607 | 36.6k | return addk(fs, &o, &o); /* use number itself as key */ |
608 | 39.7k | else { /* must build an alternative key */ |
609 | 39.7k | const int nbm = l_floatatt(MANT_DIG); |
610 | 39.7k | const lua_Number q = l_mathop(ldexp)(l_mathop(1.0), -nbm + 1); |
611 | 39.7k | const lua_Number k = (ik == 0) ? q : r + r*q; /* new key */ |
612 | 39.7k | TValue kv; |
613 | 39.7k | setfltvalue(&kv, k); |
614 | | /* result is not an integral value, unless value is too large */ |
615 | 39.7k | lua_assert(!luaV_flttointeger(k, &ik, F2Ieq) || |
616 | 39.7k | l_mathop(fabs)(r) >= l_mathop(1e6)); |
617 | 39.7k | return addk(fs, &kv, &o); |
618 | 39.7k | } |
619 | 76.4k | } |
620 | | |
621 | | |
622 | | /* |
623 | | ** Add a false to list of constants and return its index. |
624 | | */ |
625 | 12 | static int boolF (FuncState *fs) { |
626 | 12 | TValue o; |
627 | 12 | setbfvalue(&o); |
628 | 12 | return addk(fs, &o, &o); /* use boolean itself as key */ |
629 | 12 | } |
630 | | |
631 | | |
632 | | /* |
633 | | ** Add a true to list of constants and return its index. |
634 | | */ |
635 | 932 | static int boolT (FuncState *fs) { |
636 | 932 | TValue o; |
637 | 932 | setbtvalue(&o); |
638 | 932 | return addk(fs, &o, &o); /* use boolean itself as key */ |
639 | 932 | } |
640 | | |
641 | | |
642 | | /* |
643 | | ** Add nil to list of constants and return its index. |
644 | | */ |
645 | 1.14k | static int nilK (FuncState *fs) { |
646 | 1.14k | TValue k, v; |
647 | 1.14k | setnilvalue(&v); |
648 | | /* cannot use nil as key; instead use table itself to represent nil */ |
649 | 1.14k | sethvalue(fs->ls->L, &k, fs->ls->h); |
650 | 1.14k | return addk(fs, &k, &v); |
651 | 1.14k | } |
652 | | |
653 | | |
654 | | /* |
655 | | ** Check whether 'i' can be stored in an 'sC' operand. Equivalent to |
656 | | ** (0 <= int2sC(i) && int2sC(i) <= MAXARG_C) but without risk of |
657 | | ** overflows in the hidden addition inside 'int2sC'. |
658 | | */ |
659 | 81.0k | static int fitsC (lua_Integer i) { |
660 | 81.0k | return (l_castS2U(i) + OFFSET_sC <= cast_uint(MAXARG_C)); |
661 | 81.0k | } |
662 | | |
663 | | |
664 | | /* |
665 | | ** Check whether 'i' can be stored in an 'sBx' operand. |
666 | | */ |
667 | 81.3k | static int fitsBx (lua_Integer i) { |
668 | 81.3k | return (-OFFSET_sBx <= i && i <= MAXARG_Bx - OFFSET_sBx); |
669 | 81.3k | } |
670 | | |
671 | | |
672 | 46.5k | void luaK_int (FuncState *fs, int reg, lua_Integer i) { |
673 | 46.5k | if (fitsBx(i)) |
674 | 42.7k | codeAsBx(fs, OP_LOADI, reg, cast_int(i)); |
675 | 3.80k | else |
676 | 3.80k | luaK_codek(fs, reg, luaK_intK(fs, i)); |
677 | 46.5k | } |
678 | | |
679 | | |
680 | 60.0k | static void luaK_float (FuncState *fs, int reg, lua_Number f) { |
681 | 60.0k | lua_Integer fi; |
682 | 60.0k | if (luaV_flttointeger(f, &fi, F2Ieq) && fitsBx(fi)) |
683 | 29.6k | codeAsBx(fs, OP_LOADF, reg, cast_int(fi)); |
684 | 30.4k | else |
685 | 30.4k | luaK_codek(fs, reg, luaK_numberK(fs, f)); |
686 | 60.0k | } |
687 | | |
688 | | |
689 | | /* |
690 | | ** Convert a constant in 'v' into an expression description 'e' |
691 | | */ |
692 | 531k | static void const2exp (TValue *v, expdesc *e) { |
693 | 531k | switch (ttypetag(v)) { |
694 | 2 | case LUA_VNUMINT: |
695 | 2 | e->k = VKINT; e->u.ival = ivalue(v); |
696 | 2 | break; |
697 | 0 | case LUA_VNUMFLT: |
698 | 0 | e->k = VKFLT; e->u.nval = fltvalue(v); |
699 | 0 | break; |
700 | 303k | case LUA_VFALSE: |
701 | 303k | e->k = VFALSE; |
702 | 303k | break; |
703 | 201 | case LUA_VTRUE: |
704 | 201 | e->k = VTRUE; |
705 | 201 | break; |
706 | 88.6k | case LUA_VNIL: |
707 | 88.6k | e->k = VNIL; |
708 | 88.6k | break; |
709 | 139k | case LUA_VSHRSTR: case LUA_VLNGSTR: |
710 | 139k | e->k = VKSTR; e->u.strval = tsvalue(v); |
711 | 139k | break; |
712 | 0 | default: lua_assert(0); |
713 | 531k | } |
714 | 531k | } |
715 | | |
716 | | |
717 | | /* |
718 | | ** Fix an expression to return the number of results 'nresults'. |
719 | | ** 'e' must be a multi-ret expression (function call or vararg). |
720 | | */ |
721 | 2.63k | void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) { |
722 | 2.63k | Instruction *pc = &getinstruction(fs, e); |
723 | 2.63k | if (e->k == VCALL) /* expression is an open function call? */ |
724 | 2.63k | SETARG_C(*pc, nresults + 1); |
725 | 477 | else { |
726 | 477 | lua_assert(e->k == VVARARG); |
727 | 477 | SETARG_C(*pc, nresults + 1); |
728 | 477 | SETARG_A(*pc, fs->freereg); |
729 | 477 | luaK_reserveregs(fs, 1); |
730 | 477 | } |
731 | 2.63k | } |
732 | | |
733 | | |
734 | | /* |
735 | | ** Convert a VKSTR to a VK |
736 | | */ |
737 | 13.2M | static void str2K (FuncState *fs, expdesc *e) { |
738 | 13.2M | lua_assert(e->k == VKSTR); |
739 | 13.2M | e->u.info = stringK(fs, e->u.strval); |
740 | 13.2M | e->k = VK; |
741 | 13.2M | } |
742 | | |
743 | | |
744 | | /* |
745 | | ** Fix an expression to return one result. |
746 | | ** If expression is not a multi-ret expression (function call or |
747 | | ** vararg), it already returns one result, so nothing needs to be done. |
748 | | ** Function calls become VNONRELOC expressions (as its result comes |
749 | | ** fixed in the base register of the call), while vararg expressions |
750 | | ** become VRELOC (as OP_VARARG puts its results where it wants). |
751 | | ** (Calls are created returning one result, so that does not need |
752 | | ** to be fixed.) |
753 | | */ |
754 | 451k | void luaK_setoneret (FuncState *fs, expdesc *e) { |
755 | 451k | if (e->k == VCALL) { /* expression is an open function call? */ |
756 | | /* already returns 1 value */ |
757 | 432k | lua_assert(GETARG_C(getinstruction(fs, e)) == 2); |
758 | 432k | e->k = VNONRELOC; /* result has fixed position */ |
759 | 432k | e->u.info = GETARG_A(getinstruction(fs, e)); |
760 | 432k | } |
761 | 18.2k | else if (e->k == VVARARG) { |
762 | 693 | SETARG_C(getinstruction(fs, e), 2); |
763 | 693 | e->k = VRELOC; /* can relocate its simple result */ |
764 | 693 | } |
765 | 451k | } |
766 | | |
767 | | |
768 | | /* |
769 | | ** Ensure that expression 'e' is not a variable (nor a <const>). |
770 | | ** (Expression still may have jump lists.) |
771 | | */ |
772 | 137M | void luaK_dischargevars (FuncState *fs, expdesc *e) { |
773 | 137M | switch (e->k) { |
774 | 531k | case VCONST: { |
775 | 531k | const2exp(const2val(fs, e), e); |
776 | 531k | break; |
777 | 0 | } |
778 | 155k | case VLOCAL: { /* already in a register */ |
779 | 155k | e->u.info = e->u.var.ridx; |
780 | 155k | e->k = VNONRELOC; /* becomes a non-relocatable value */ |
781 | 155k | break; |
782 | 0 | } |
783 | 2.98M | case VUPVAL: { /* move value to some (pending) register */ |
784 | 2.98M | e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0); |
785 | 2.98M | e->k = VRELOC; |
786 | 2.98M | break; |
787 | 0 | } |
788 | 9.04M | case VINDEXUP: { |
789 | 9.04M | e->u.info = luaK_codeABC(fs, OP_GETTABUP, 0, e->u.ind.t, e->u.ind.idx); |
790 | 9.04M | e->k = VRELOC; |
791 | 9.04M | break; |
792 | 0 | } |
793 | 335 | case VINDEXI: { |
794 | 335 | freereg(fs, e->u.ind.t); |
795 | 335 | e->u.info = luaK_codeABC(fs, OP_GETI, 0, e->u.ind.t, e->u.ind.idx); |
796 | 335 | e->k = VRELOC; |
797 | 335 | break; |
798 | 0 | } |
799 | 628k | case VINDEXSTR: { |
800 | 628k | freereg(fs, e->u.ind.t); |
801 | 628k | e->u.info = luaK_codeABC(fs, OP_GETFIELD, 0, e->u.ind.t, e->u.ind.idx); |
802 | 628k | e->k = VRELOC; |
803 | 628k | break; |
804 | 0 | } |
805 | 3.00M | case VINDEXED: { |
806 | 3.00M | freeregs(fs, e->u.ind.t, e->u.ind.idx); |
807 | 3.00M | e->u.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.ind.t, e->u.ind.idx); |
808 | 3.00M | e->k = VRELOC; |
809 | 3.00M | break; |
810 | 0 | } |
811 | 433k | case VVARARG: case VCALL: { |
812 | 433k | luaK_setoneret(fs, e); |
813 | 433k | break; |
814 | 491 | } |
815 | 120M | default: break; /* there is one value available (somewhere) */ |
816 | 137M | } |
817 | 137M | } |
818 | | |
819 | | |
820 | | /* |
821 | | ** Ensure expression value is in register 'reg', making 'e' a |
822 | | ** non-relocatable expression. |
823 | | ** (Expression still may have jump lists.) |
824 | | */ |
825 | 32.8M | static void discharge2reg (FuncState *fs, expdesc *e, int reg) { |
826 | 32.8M | luaK_dischargevars(fs, e); |
827 | 32.8M | switch (e->k) { |
828 | 177k | case VNIL: { |
829 | 177k | luaK_nil(fs, reg, 1); |
830 | 177k | break; |
831 | 0 | } |
832 | 305k | case VFALSE: { |
833 | 305k | luaK_codeABC(fs, OP_LOADFALSE, reg, 0, 0); |
834 | 305k | break; |
835 | 0 | } |
836 | 1.51k | case VTRUE: { |
837 | 1.51k | luaK_codeABC(fs, OP_LOADTRUE, reg, 0, 0); |
838 | 1.51k | break; |
839 | 0 | } |
840 | 542k | case VKSTR: { |
841 | 542k | str2K(fs, e); |
842 | 542k | } /* FALLTHROUGH */ |
843 | 3.52M | case VK: { |
844 | 3.52M | luaK_codek(fs, reg, e->u.info); |
845 | 3.52M | break; |
846 | 542k | } |
847 | 60.0k | case VKFLT: { |
848 | 60.0k | luaK_float(fs, reg, e->u.nval); |
849 | 60.0k | break; |
850 | 542k | } |
851 | 46.0k | case VKINT: { |
852 | 46.0k | luaK_int(fs, reg, e->u.ival); |
853 | 46.0k | break; |
854 | 542k | } |
855 | 22.1M | case VRELOC: { |
856 | 22.1M | Instruction *pc = &getinstruction(fs, e); |
857 | 22.1M | SETARG_A(*pc, reg); /* instruction will put result in 'reg' */ |
858 | 22.1M | break; |
859 | 542k | } |
860 | 287k | case VNONRELOC: { |
861 | 287k | if (reg != e->u.info) |
862 | 31.2k | luaK_codeABC(fs, OP_MOVE, reg, e->u.info, 0); |
863 | 287k | break; |
864 | 542k | } |
865 | 6.28M | default: { |
866 | 6.28M | lua_assert(e->k == VJMP); |
867 | 6.28M | return; /* nothing to do... */ |
868 | 542k | } |
869 | 32.8M | } |
870 | 26.5M | e->u.info = reg; |
871 | 26.5M | e->k = VNONRELOC; |
872 | 26.5M | } |
873 | | |
874 | | |
875 | | /* |
876 | | ** Ensure expression value is in a register, making 'e' a |
877 | | ** non-relocatable expression. |
878 | | ** (Expression still may have jump lists.) |
879 | | */ |
880 | 50.0k | static void discharge2anyreg (FuncState *fs, expdesc *e) { |
881 | 50.0k | if (e->k != VNONRELOC) { /* no fixed register yet? */ |
882 | 36.2k | luaK_reserveregs(fs, 1); /* get a register */ |
883 | 36.2k | discharge2reg(fs, e, fs->freereg-1); /* put value there */ |
884 | 36.2k | } |
885 | 50.0k | } |
886 | | |
887 | | |
888 | 12.6M | static int code_loadbool (FuncState *fs, int A, OpCode op) { |
889 | 12.6M | luaK_getlabel(fs); /* those instructions may be jump targets */ |
890 | 12.6M | return luaK_codeABC(fs, op, A, 0, 0); |
891 | 12.6M | } |
892 | | |
893 | | |
894 | | /* |
895 | | ** check whether list has any jump that do not produce a value |
896 | | ** or produce an inverted value |
897 | | */ |
898 | 6.34M | static int need_value (FuncState *fs, int list) { |
899 | 6.36M | for (; list != NO_JUMP; list = getjump(fs, list)) { |
900 | 6.32M | Instruction i = *getjumpcontrol(fs, list); |
901 | 6.32M | if (GET_OPCODE(i) != OP_TESTSET) return 1; |
902 | 6.32M | } |
903 | 40.8k | return 0; /* not found */ |
904 | 6.34M | } |
905 | | |
906 | | |
907 | | /* |
908 | | ** Ensures final expression result (which includes results from its |
909 | | ** jump lists) is in register 'reg'. |
910 | | ** If expression has jumps, need to patch these jumps either to |
911 | | ** its final position or to "load" instructions (for those tests |
912 | | ** that do not produce values). |
913 | | */ |
914 | 32.7M | static void exp2reg (FuncState *fs, expdesc *e, int reg) { |
915 | 32.7M | discharge2reg(fs, e, reg); |
916 | 32.7M | if (e->k == VJMP) /* expression itself is a test? */ |
917 | 6.28M | luaK_concat(fs, &e->t, e->u.info); /* put this jump in 't' list */ |
918 | 32.7M | if (hasjumps(e)) { |
919 | 6.31M | int final; /* position after whole expression */ |
920 | 6.31M | int p_f = NO_JUMP; /* position of an eventual LOAD false */ |
921 | 6.31M | int p_t = NO_JUMP; /* position of an eventual LOAD true */ |
922 | 6.31M | if (need_value(fs, e->t) || need_value(fs, e->f)) { |
923 | 6.30M | int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs); |
924 | 6.30M | p_f = code_loadbool(fs, reg, OP_LFALSESKIP); /* skip next inst. */ |
925 | 6.30M | p_t = code_loadbool(fs, reg, OP_LOADTRUE); |
926 | | /* jump around these booleans if 'e' is not a test */ |
927 | 6.30M | luaK_patchtohere(fs, fj); |
928 | 6.30M | } |
929 | 6.31M | final = luaK_getlabel(fs); |
930 | 6.31M | patchlistaux(fs, e->f, final, reg, p_f); |
931 | 6.31M | patchlistaux(fs, e->t, final, reg, p_t); |
932 | 6.31M | } |
933 | 32.7M | e->f = e->t = NO_JUMP; |
934 | 32.7M | e->u.info = reg; |
935 | 32.7M | e->k = VNONRELOC; |
936 | 32.7M | } |
937 | | |
938 | | |
939 | | /* |
940 | | ** Ensures final expression result is in next available register. |
941 | | */ |
942 | 32.7M | void luaK_exp2nextreg (FuncState *fs, expdesc *e) { |
943 | 32.7M | luaK_dischargevars(fs, e); |
944 | 32.7M | freeexp(fs, e); |
945 | 32.7M | luaK_reserveregs(fs, 1); |
946 | 32.7M | exp2reg(fs, e, fs->freereg - 1); |
947 | 32.7M | } |
948 | | |
949 | | |
950 | | /* |
951 | | ** Ensures final expression result is in some (any) register |
952 | | ** and return that register. |
953 | | */ |
954 | 44.7M | int luaK_exp2anyreg (FuncState *fs, expdesc *e) { |
955 | 44.7M | luaK_dischargevars(fs, e); |
956 | 44.7M | if (e->k == VNONRELOC) { /* expression already has a register? */ |
957 | 12.9M | if (!hasjumps(e)) /* no jumps? */ |
958 | 12.9M | return e->u.info; /* result is already in a register */ |
959 | 19.6k | if (e->u.info >= luaY_nvarstack(fs)) { /* reg. is not a local? */ |
960 | 16.7k | exp2reg(fs, e, e->u.info); /* put final result in it */ |
961 | 16.7k | return e->u.info; |
962 | 16.7k | } |
963 | | /* else expression has jumps and cannot change its register |
964 | | to hold the jump values, because it is a local variable. |
965 | | Go through to the default case. */ |
966 | 19.6k | } |
967 | 31.8M | luaK_exp2nextreg(fs, e); /* default: use next available register */ |
968 | 31.8M | return e->u.info; |
969 | 44.7M | } |
970 | | |
971 | | |
972 | | /* |
973 | | ** Ensures final expression result is either in a register |
974 | | ** or in an upvalue. |
975 | | */ |
976 | 12.7M | void luaK_exp2anyregup (FuncState *fs, expdesc *e) { |
977 | 12.7M | if (e->k != VUPVAL || hasjumps(e)) |
978 | 655k | luaK_exp2anyreg(fs, e); |
979 | 12.7M | } |
980 | | |
981 | | |
982 | | /* |
983 | | ** Ensures final expression result is either in a register |
984 | | ** or it is a constant. |
985 | | */ |
986 | 23.2k | void luaK_exp2val (FuncState *fs, expdesc *e) { |
987 | 23.2k | if (hasjumps(e)) |
988 | 336 | luaK_exp2anyreg(fs, e); |
989 | 22.8k | else |
990 | 22.8k | luaK_dischargevars(fs, e); |
991 | 23.2k | } |
992 | | |
993 | | |
994 | | /* |
995 | | ** Try to make 'e' a K expression with an index in the range of R/K |
996 | | ** indices. Return true iff succeeded. |
997 | | */ |
998 | 137k | static int luaK_exp2K (FuncState *fs, expdesc *e) { |
999 | 137k | if (!hasjumps(e)) { |
1000 | 135k | int info; |
1001 | 135k | switch (e->k) { /* move constants to 'k' */ |
1002 | 932 | case VTRUE: info = boolT(fs); break; |
1003 | 12 | case VFALSE: info = boolF(fs); break; |
1004 | 1.14k | case VNIL: info = nilK(fs); break; |
1005 | 57.6k | case VKINT: info = luaK_intK(fs, e->u.ival); break; |
1006 | 45.9k | case VKFLT: info = luaK_numberK(fs, e->u.nval); break; |
1007 | 1.92k | case VKSTR: info = stringK(fs, e->u.strval); break; |
1008 | 1.16k | case VK: info = e->u.info; break; |
1009 | 26.2k | default: return 0; /* not a constant */ |
1010 | 135k | } |
1011 | 108k | if (info <= MAXINDEXRK) { /* does constant fit in 'argC'? */ |
1012 | 79.0k | e->k = VK; /* make expression a 'K' expression */ |
1013 | 79.0k | e->u.info = info; |
1014 | 79.0k | return 1; |
1015 | 79.0k | } |
1016 | 108k | } |
1017 | | /* else, expression doesn't fit; leave it unchanged */ |
1018 | 32.6k | return 0; |
1019 | 137k | } |
1020 | | |
1021 | | |
1022 | | /* |
1023 | | ** Ensures final expression result is in a valid R/K index |
1024 | | ** (that is, it is either in a register or in 'k' with an index |
1025 | | ** in the range of R/K indices). |
1026 | | ** Returns 1 iff expression is K. |
1027 | | */ |
1028 | 38.6k | static int exp2RK (FuncState *fs, expdesc *e) { |
1029 | 38.6k | if (luaK_exp2K(fs, e)) |
1030 | 9.27k | return 1; |
1031 | 29.3k | else { /* not a constant in the right range: put it in a register */ |
1032 | 29.3k | luaK_exp2anyreg(fs, e); |
1033 | 29.3k | return 0; |
1034 | 29.3k | } |
1035 | 38.6k | } |
1036 | | |
1037 | | |
1038 | | static void codeABRK (FuncState *fs, OpCode o, int a, int b, |
1039 | 25.5k | expdesc *ec) { |
1040 | 25.5k | int k = exp2RK(fs, ec); |
1041 | 25.5k | luaK_codeABCk(fs, o, a, b, ec->u.info, k); |
1042 | 25.5k | } |
1043 | | |
1044 | | |
1045 | | /* |
1046 | | ** Generate code to store result of expression 'ex' into variable 'var'. |
1047 | | */ |
1048 | 28.8k | void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { |
1049 | 28.8k | switch (var->k) { |
1050 | 3.30k | case VLOCAL: { |
1051 | 3.30k | freeexp(fs, ex); |
1052 | 3.30k | exp2reg(fs, ex, var->u.var.ridx); /* compute 'ex' into proper place */ |
1053 | 3.30k | return; |
1054 | 0 | } |
1055 | 1.22k | case VUPVAL: { |
1056 | 1.22k | int e = luaK_exp2anyreg(fs, ex); |
1057 | 1.22k | luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0); |
1058 | 1.22k | break; |
1059 | 0 | } |
1060 | 16.6k | case VINDEXUP: { |
1061 | 16.6k | codeABRK(fs, OP_SETTABUP, var->u.ind.t, var->u.ind.idx, ex); |
1062 | 16.6k | break; |
1063 | 0 | } |
1064 | 604 | case VINDEXI: { |
1065 | 604 | codeABRK(fs, OP_SETI, var->u.ind.t, var->u.ind.idx, ex); |
1066 | 604 | break; |
1067 | 0 | } |
1068 | 4.80k | case VINDEXSTR: { |
1069 | 4.80k | codeABRK(fs, OP_SETFIELD, var->u.ind.t, var->u.ind.idx, ex); |
1070 | 4.80k | break; |
1071 | 0 | } |
1072 | 2.33k | case VINDEXED: { |
1073 | 2.33k | codeABRK(fs, OP_SETTABLE, var->u.ind.t, var->u.ind.idx, ex); |
1074 | 2.33k | break; |
1075 | 0 | } |
1076 | 0 | default: lua_assert(0); /* invalid var kind to store */ |
1077 | 28.8k | } |
1078 | 25.5k | freeexp(fs, ex); |
1079 | 25.5k | } |
1080 | | |
1081 | | |
1082 | | /* |
1083 | | ** Emit SELF instruction (convert expression 'e' into 'e:key(e,'). |
1084 | | */ |
1085 | 1.17k | void luaK_self (FuncState *fs, expdesc *e, expdesc *key) { |
1086 | 1.17k | int ereg; |
1087 | 1.17k | luaK_exp2anyreg(fs, e); |
1088 | 1.17k | ereg = e->u.info; /* register where 'e' was placed */ |
1089 | 1.17k | freeexp(fs, e); |
1090 | 1.17k | e->u.info = fs->freereg; /* base register for op_self */ |
1091 | 1.17k | e->k = VNONRELOC; /* self expression has a fixed register */ |
1092 | 1.17k | luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */ |
1093 | 1.17k | codeABRK(fs, OP_SELF, e->u.info, ereg, key); |
1094 | 1.17k | freeexp(fs, key); |
1095 | 1.17k | } |
1096 | | |
1097 | | |
1098 | | /* |
1099 | | ** Negate condition 'e' (where 'e' is a comparison). |
1100 | | */ |
1101 | 7.84k | static void negatecondition (FuncState *fs, expdesc *e) { |
1102 | 7.84k | Instruction *pc = getjumpcontrol(fs, e->u.info); |
1103 | 7.84k | lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET && |
1104 | 7.84k | GET_OPCODE(*pc) != OP_TEST); |
1105 | 7.84k | SETARG_k(*pc, (GETARG_k(*pc) ^ 1)); |
1106 | 7.84k | } |
1107 | | |
1108 | | |
1109 | | /* |
1110 | | ** Emit instruction to jump if 'e' is 'cond' (that is, if 'cond' |
1111 | | ** is true, code will jump if 'e' is true.) Return jump position. |
1112 | | ** Optimize when 'e' is 'not' something, inverting the condition |
1113 | | ** and removing the 'not'. |
1114 | | */ |
1115 | 46.8k | static int jumponcond (FuncState *fs, expdesc *e, int cond) { |
1116 | 46.8k | if (e->k == VRELOC) { |
1117 | 32.7k | Instruction ie = getinstruction(fs, e); |
1118 | 32.7k | if (GET_OPCODE(ie) == OP_NOT) { |
1119 | 1.44k | removelastinstruction(fs); /* remove previous OP_NOT */ |
1120 | 1.44k | return condjump(fs, OP_TEST, GETARG_B(ie), 0, 0, !cond); |
1121 | 1.44k | } |
1122 | | /* else go through */ |
1123 | 32.7k | } |
1124 | 45.3k | discharge2anyreg(fs, e); |
1125 | 45.3k | freeexp(fs, e); |
1126 | 45.3k | return condjump(fs, OP_TESTSET, NO_REG, e->u.info, 0, cond); |
1127 | 46.8k | } |
1128 | | |
1129 | | |
1130 | | /* |
1131 | | ** Emit code to go through if 'e' is true, jump otherwise. |
1132 | | */ |
1133 | 34.0k | void luaK_goiftrue (FuncState *fs, expdesc *e) { |
1134 | 34.0k | int pc; /* pc of new jump */ |
1135 | 34.0k | luaK_dischargevars(fs, e); |
1136 | 34.0k | switch (e->k) { |
1137 | 7.14k | case VJMP: { /* condition? */ |
1138 | 7.14k | negatecondition(fs, e); /* jump when it is false */ |
1139 | 7.14k | pc = e->u.info; /* save jump position */ |
1140 | 7.14k | break; |
1141 | 0 | } |
1142 | 2.47k | case VK: case VKFLT: case VKINT: case VKSTR: case VTRUE: { |
1143 | 2.47k | pc = NO_JUMP; /* always true; do nothing */ |
1144 | 2.47k | break; |
1145 | 1.38k | } |
1146 | 24.4k | default: { |
1147 | 24.4k | pc = jumponcond(fs, e, 0); /* jump when false */ |
1148 | 24.4k | break; |
1149 | 1.38k | } |
1150 | 34.0k | } |
1151 | 34.0k | luaK_concat(fs, &e->f, pc); /* insert new jump in false list */ |
1152 | 34.0k | luaK_patchtohere(fs, e->t); /* true list jumps to here (to go through) */ |
1153 | 34.0k | e->t = NO_JUMP; |
1154 | 34.0k | } |
1155 | | |
1156 | | |
1157 | | /* |
1158 | | ** Emit code to go through if 'e' is false, jump otherwise. |
1159 | | */ |
1160 | 193k | void luaK_goiffalse (FuncState *fs, expdesc *e) { |
1161 | 193k | int pc; /* pc of new jump */ |
1162 | 193k | luaK_dischargevars(fs, e); |
1163 | 193k | switch (e->k) { |
1164 | 171k | case VJMP: { |
1165 | 171k | pc = e->u.info; /* already jump if true */ |
1166 | 171k | break; |
1167 | 0 | } |
1168 | 270 | case VNIL: case VFALSE: { |
1169 | 270 | pc = NO_JUMP; /* always false; do nothing */ |
1170 | 270 | break; |
1171 | 270 | } |
1172 | 22.3k | default: { |
1173 | 22.3k | pc = jumponcond(fs, e, 1); /* jump if true */ |
1174 | 22.3k | break; |
1175 | 270 | } |
1176 | 193k | } |
1177 | 193k | luaK_concat(fs, &e->t, pc); /* insert new jump in 't' list */ |
1178 | 193k | luaK_patchtohere(fs, e->f); /* false list jumps to here (to go through) */ |
1179 | 193k | e->f = NO_JUMP; |
1180 | 193k | } |
1181 | | |
1182 | | |
1183 | | /* |
1184 | | ** Code 'not e', doing constant folding. |
1185 | | */ |
1186 | 6.33k | static void codenot (FuncState *fs, expdesc *e) { |
1187 | 6.33k | switch (e->k) { |
1188 | 219 | case VNIL: case VFALSE: { |
1189 | 219 | e->k = VTRUE; /* true == not nil == not false */ |
1190 | 219 | break; |
1191 | 0 | } |
1192 | 777 | case VK: case VKFLT: case VKINT: case VKSTR: case VTRUE: { |
1193 | 777 | e->k = VFALSE; /* false == not "x" == not 0.5 == not 1 == not true */ |
1194 | 777 | break; |
1195 | 537 | } |
1196 | 700 | case VJMP: { |
1197 | 700 | negatecondition(fs, e); |
1198 | 700 | break; |
1199 | 537 | } |
1200 | 3.06k | case VRELOC: |
1201 | 4.63k | case VNONRELOC: { |
1202 | 4.63k | discharge2anyreg(fs, e); |
1203 | 4.63k | freeexp(fs, e); |
1204 | 4.63k | e->u.info = luaK_codeABC(fs, OP_NOT, 0, e->u.info, 0); |
1205 | 4.63k | e->k = VRELOC; |
1206 | 4.63k | break; |
1207 | 3.06k | } |
1208 | 0 | default: lua_assert(0); /* cannot happen */ |
1209 | 6.33k | } |
1210 | | /* interchange true and false lists */ |
1211 | 6.33k | { int temp = e->f; e->f = e->t; e->t = temp; } |
1212 | 6.33k | removevalues(fs, e->f); /* values are useless when negated */ |
1213 | 6.33k | removevalues(fs, e->t); |
1214 | 6.33k | } |
1215 | | |
1216 | | |
1217 | | /* |
1218 | | ** Check whether expression 'e' is a short literal string |
1219 | | */ |
1220 | 15.6M | static int isKstr (FuncState *fs, expdesc *e) { |
1221 | 15.6M | return (e->k == VK && !hasjumps(e) && e->u.info <= MAXARG_B && |
1222 | 15.6M | ttisshrstring(&fs->f->k[e->u.info])); |
1223 | 15.6M | } |
1224 | | |
1225 | | /* |
1226 | | ** Check whether expression 'e' is a literal integer. |
1227 | | */ |
1228 | 3.74M | static int isKint (expdesc *e) { |
1229 | 3.74M | return (e->k == VKINT && !hasjumps(e)); |
1230 | 3.74M | } |
1231 | | |
1232 | | |
1233 | | /* |
1234 | | ** Check whether expression 'e' is a literal integer in |
1235 | | ** proper range to fit in register C |
1236 | | */ |
1237 | 3.00M | static int isCint (expdesc *e) { |
1238 | 3.00M | return isKint(e) && (l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C)); |
1239 | 3.00M | } |
1240 | | |
1241 | | |
1242 | | /* |
1243 | | ** Check whether expression 'e' is a literal integer in |
1244 | | ** proper range to fit in register sC |
1245 | | */ |
1246 | 363k | static int isSCint (expdesc *e) { |
1247 | 363k | return isKint(e) && fitsC(e->u.ival); |
1248 | 363k | } |
1249 | | |
1250 | | |
1251 | | /* |
1252 | | ** Check whether expression 'e' is a literal integer or float in |
1253 | | ** proper range to fit in a register (sB or sC). |
1254 | | */ |
1255 | 19.3M | static int isSCnumber (expdesc *e, int *pi, int *isfloat) { |
1256 | 19.3M | lua_Integer i; |
1257 | 19.3M | if (e->k == VKINT) |
1258 | 31.0k | i = e->u.ival; |
1259 | 19.3M | else if (e->k == VKFLT && luaV_flttointeger(e->u.nval, &i, F2Ieq)) |
1260 | 5.51k | *isfloat = 1; |
1261 | 19.3M | else |
1262 | 19.3M | return 0; /* not a number */ |
1263 | 36.5k | if (!hasjumps(e) && fitsC(i)) { |
1264 | 29.8k | *pi = int2sC(cast_int(i)); |
1265 | 29.8k | return 1; |
1266 | 29.8k | } |
1267 | 6.70k | else |
1268 | 6.70k | return 0; |
1269 | 36.5k | } |
1270 | | |
1271 | | |
1272 | | /* |
1273 | | ** Create expression 't[k]'. 't' must have its final result already in a |
1274 | | ** register or upvalue. Upvalues can only be indexed by literal strings. |
1275 | | ** Keys can be literal strings in the constant table or arbitrary |
1276 | | ** values in registers. |
1277 | | */ |
1278 | 12.7M | void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { |
1279 | 12.7M | if (k->k == VKSTR) |
1280 | 12.6M | str2K(fs, k); |
1281 | 12.7M | lua_assert(!hasjumps(t) && |
1282 | 12.7M | (t->k == VLOCAL || t->k == VNONRELOC || t->k == VUPVAL)); |
1283 | 12.7M | if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non 'Kstr'? */ |
1284 | 2.98M | luaK_exp2anyreg(fs, t); /* put it in a register */ |
1285 | 12.7M | if (t->k == VUPVAL) { |
1286 | 9.06M | lua_assert(isKstr(fs, k)); |
1287 | 9.06M | t->u.ind.t = t->u.info; /* upvalue index */ |
1288 | 9.06M | t->u.ind.idx = k->u.info; /* literal short string */ |
1289 | 9.06M | t->k = VINDEXUP; |
1290 | 9.06M | } |
1291 | 3.63M | else { |
1292 | | /* register index of the table */ |
1293 | 3.63M | t->u.ind.t = (t->k == VLOCAL) ? t->u.var.ridx: t->u.info; |
1294 | 3.63M | if (isKstr(fs, k)) { |
1295 | 633k | t->u.ind.idx = k->u.info; /* literal short string */ |
1296 | 633k | t->k = VINDEXSTR; |
1297 | 633k | } |
1298 | 3.00M | else if (isCint(k)) { |
1299 | 941 | t->u.ind.idx = cast_int(k->u.ival); /* int. constant in proper range */ |
1300 | 941 | t->k = VINDEXI; |
1301 | 941 | } |
1302 | 3.00M | else { |
1303 | 3.00M | t->u.ind.idx = luaK_exp2anyreg(fs, k); /* register */ |
1304 | 3.00M | t->k = VINDEXED; |
1305 | 3.00M | } |
1306 | 3.63M | } |
1307 | 12.7M | } |
1308 | | |
1309 | | |
1310 | | /* |
1311 | | ** Return false if folding can raise an error. |
1312 | | ** Bitwise operations need operands convertible to integers; division |
1313 | | ** operations cannot have 0 as divisor. |
1314 | | */ |
1315 | 210k | static int validop (int op, TValue *v1, TValue *v2) { |
1316 | 210k | switch (op) { |
1317 | 2.66k | case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR: |
1318 | 68.4k | case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: { /* conversion errors */ |
1319 | 68.4k | lua_Integer i; |
1320 | 68.4k | return (luaV_tointegerns(v1, &i, LUA_FLOORN2I) && |
1321 | 68.4k | luaV_tointegerns(v2, &i, LUA_FLOORN2I)); |
1322 | 4.79k | } |
1323 | 12.1k | case LUA_OPDIV: case LUA_OPIDIV: case LUA_OPMOD: /* division by 0 */ |
1324 | 12.1k | return (nvalue(v2) != 0); |
1325 | 129k | default: return 1; /* everything else is valid */ |
1326 | 210k | } |
1327 | 210k | } |
1328 | | |
1329 | | |
1330 | | /* |
1331 | | ** Try to "constant-fold" an operation; return 1 iff successful. |
1332 | | ** (In this case, 'e1' has the final result.) |
1333 | | */ |
1334 | | static int constfolding (FuncState *fs, int op, expdesc *e1, |
1335 | 6.61M | const expdesc *e2) { |
1336 | 6.61M | TValue v1, v2, res; |
1337 | 6.61M | if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2)) |
1338 | 6.41M | return 0; /* non-numeric operands or not safe to fold */ |
1339 | 193k | luaO_rawarith(fs->ls->L, op, &v1, &v2, &res); /* does operation */ |
1340 | 193k | if (ttisinteger(&res)) { |
1341 | 73.9k | e1->k = VKINT; |
1342 | 73.9k | e1->u.ival = ivalue(&res); |
1343 | 73.9k | } |
1344 | 119k | else { /* folds neither NaN nor 0.0 (to avoid problems with -0.0) */ |
1345 | 119k | lua_Number n = fltvalue(&res); |
1346 | 119k | if (luai_numisnan(n) || n == 0) |
1347 | 13.2k | return 0; |
1348 | 106k | e1->k = VKFLT; |
1349 | 106k | e1->u.nval = n; |
1350 | 106k | } |
1351 | 180k | return 1; |
1352 | 193k | } |
1353 | | |
1354 | | |
1355 | | /* |
1356 | | ** Convert a BinOpr to an OpCode (ORDER OPR - ORDER OP) |
1357 | | */ |
1358 | 12.6M | l_sinline OpCode binopr2op (BinOpr opr, BinOpr baser, OpCode base) { |
1359 | 12.6M | lua_assert(baser <= opr && |
1360 | 12.6M | ((baser == OPR_ADD && opr <= OPR_SHR) || |
1361 | 12.6M | (baser == OPR_LT && opr <= OPR_LE))); |
1362 | 12.6M | return cast(OpCode, (cast_int(opr) - cast_int(baser)) + cast_int(base)); |
1363 | 12.6M | } |
1364 | | |
1365 | | |
1366 | | /* |
1367 | | ** Convert a UnOpr to an OpCode (ORDER OPR - ORDER OP) |
1368 | | */ |
1369 | 260k | l_sinline OpCode unopr2op (UnOpr opr) { |
1370 | 260k | return cast(OpCode, (cast_int(opr) - cast_int(OPR_MINUS)) + |
1371 | 260k | cast_int(OP_UNM)); |
1372 | 260k | } |
1373 | | |
1374 | | |
1375 | | /* |
1376 | | ** Convert a BinOpr to a tag method (ORDER OPR - ORDER TM) |
1377 | | */ |
1378 | 6.18M | l_sinline TMS binopr2TM (BinOpr opr) { |
1379 | 6.18M | lua_assert(OPR_ADD <= opr && opr <= OPR_SHR); |
1380 | 6.18M | return cast(TMS, (cast_int(opr) - cast_int(OPR_ADD)) + cast_int(TM_ADD)); |
1381 | 6.18M | } |
1382 | | |
1383 | | |
1384 | | /* |
1385 | | ** Emit code for unary expressions that "produce values" |
1386 | | ** (everything but 'not'). |
1387 | | ** Expression to produce final result will be encoded in 'e'. |
1388 | | */ |
1389 | 260k | static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) { |
1390 | 260k | int r = luaK_exp2anyreg(fs, e); /* opcodes operate only on registers */ |
1391 | 260k | freeexp(fs, e); |
1392 | 260k | e->u.info = luaK_codeABC(fs, op, 0, r, 0); /* generate opcode */ |
1393 | 260k | e->k = VRELOC; /* all those operations are relocatable */ |
1394 | 260k | luaK_fixline(fs, line); |
1395 | 260k | } |
1396 | | |
1397 | | |
1398 | | /* |
1399 | | ** Emit code for binary expressions that "produce values" |
1400 | | ** (everything but logical operators 'and'/'or' and comparison |
1401 | | ** operators). |
1402 | | ** Expression to produce final result will be encoded in 'e1'. |
1403 | | */ |
1404 | | static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2, |
1405 | | OpCode op, int v2, int flip, int line, |
1406 | 6.20M | OpCode mmop, TMS event) { |
1407 | 6.20M | int v1 = luaK_exp2anyreg(fs, e1); |
1408 | 6.20M | int pc = luaK_codeABCk(fs, op, 0, v1, v2, 0); |
1409 | 6.20M | freeexps(fs, e1, e2); |
1410 | 6.20M | e1->u.info = pc; |
1411 | 6.20M | e1->k = VRELOC; /* all those operations are relocatable */ |
1412 | 6.20M | luaK_fixline(fs, line); |
1413 | 6.20M | luaK_codeABCk(fs, mmop, v1, v2, event, flip); /* to call metamethod */ |
1414 | 6.20M | luaK_fixline(fs, line); |
1415 | 6.20M | } |
1416 | | |
1417 | | |
1418 | | /* |
1419 | | ** Emit code for binary expressions that "produce values" over |
1420 | | ** two registers. |
1421 | | */ |
1422 | | static void codebinexpval (FuncState *fs, BinOpr opr, |
1423 | 6.11M | expdesc *e1, expdesc *e2, int line) { |
1424 | 6.11M | OpCode op = binopr2op(opr, OPR_ADD, OP_ADD); |
1425 | 6.11M | int v2 = luaK_exp2anyreg(fs, e2); /* make sure 'e2' is in a register */ |
1426 | | /* 'e1' must be already in a register or it is a constant */ |
1427 | 6.11M | lua_assert((VNIL <= e1->k && e1->k <= VKSTR) || |
1428 | 6.11M | e1->k == VNONRELOC || e1->k == VRELOC); |
1429 | 6.11M | lua_assert(OP_ADD <= op && op <= OP_SHR); |
1430 | 6.11M | finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN, binopr2TM(opr)); |
1431 | 6.11M | } |
1432 | | |
1433 | | |
1434 | | /* |
1435 | | ** Code binary operators with immediate operands. |
1436 | | */ |
1437 | | static void codebini (FuncState *fs, OpCode op, |
1438 | | expdesc *e1, expdesc *e2, int flip, int line, |
1439 | 6.32k | TMS event) { |
1440 | 6.32k | int v2 = int2sC(cast_int(e2->u.ival)); /* immediate operand */ |
1441 | 6.32k | lua_assert(e2->k == VKINT); |
1442 | 6.32k | finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINI, event); |
1443 | 6.32k | } |
1444 | | |
1445 | | |
1446 | | /* |
1447 | | ** Code binary operators with K operand. |
1448 | | */ |
1449 | | static void codebinK (FuncState *fs, BinOpr opr, |
1450 | 69.7k | expdesc *e1, expdesc *e2, int flip, int line) { |
1451 | 69.7k | TMS event = binopr2TM(opr); |
1452 | 69.7k | int v2 = e2->u.info; /* K index */ |
1453 | 69.7k | OpCode op = binopr2op(opr, OPR_ADD, OP_ADDK); |
1454 | 69.7k | finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, event); |
1455 | 69.7k | } |
1456 | | |
1457 | | |
1458 | | /* Try to code a binary operator negating its second operand. |
1459 | | ** For the metamethod, 2nd operand must keep its original value. |
1460 | | */ |
1461 | | static int finishbinexpneg (FuncState *fs, expdesc *e1, expdesc *e2, |
1462 | 381k | OpCode op, int line, TMS event) { |
1463 | 381k | if (!isKint(e2)) |
1464 | 361k | return 0; /* not an integer constant */ |
1465 | 19.9k | else { |
1466 | 19.9k | lua_Integer i2 = e2->u.ival; |
1467 | 19.9k | if (!(fitsC(i2) && fitsC(-i2))) |
1468 | 722 | return 0; /* not in the proper range */ |
1469 | 19.2k | else { /* operating a small integer constant */ |
1470 | 19.2k | int v2 = cast_int(i2); |
1471 | 19.2k | finishbinexpval(fs, e1, e2, op, int2sC(-v2), 0, line, OP_MMBINI, event); |
1472 | | /* correct metamethod argument */ |
1473 | 19.2k | SETARG_B(fs->f->code[fs->pc - 1], int2sC(v2)); |
1474 | 19.2k | return 1; /* successfully coded */ |
1475 | 19.2k | } |
1476 | 19.9k | } |
1477 | 381k | } |
1478 | | |
1479 | | |
1480 | 70.6k | static void swapexps (expdesc *e1, expdesc *e2) { |
1481 | 70.6k | expdesc temp = *e1; *e1 = *e2; *e2 = temp; /* swap 'e1' and 'e2' */ |
1482 | 70.6k | } |
1483 | | |
1484 | | |
1485 | | /* |
1486 | | ** Code binary operators with no constant operand. |
1487 | | */ |
1488 | | static void codebinNoK (FuncState *fs, BinOpr opr, |
1489 | 5.75M | expdesc *e1, expdesc *e2, int flip, int line) { |
1490 | 5.75M | if (flip) |
1491 | 1.34k | swapexps(e1, e2); /* back to original order */ |
1492 | 5.75M | codebinexpval(fs, opr, e1, e2, line); /* use standard operators */ |
1493 | 5.75M | } |
1494 | | |
1495 | | |
1496 | | /* |
1497 | | ** Code arithmetic operators ('+', '-', ...). If second operand is a |
1498 | | ** constant in the proper range, use variant opcodes with K operands. |
1499 | | */ |
1500 | | static void codearith (FuncState *fs, BinOpr opr, |
1501 | 5.75M | expdesc *e1, expdesc *e2, int flip, int line) { |
1502 | 5.75M | if (tonumeral(e2, NULL) && luaK_exp2K(fs, e2)) /* K operand? */ |
1503 | 44.7k | codebinK(fs, opr, e1, e2, flip, line); |
1504 | 5.71M | else /* 'e2' is neither an immediate nor a K operand */ |
1505 | 5.71M | codebinNoK(fs, opr, e1, e2, flip, line); |
1506 | 5.75M | } |
1507 | | |
1508 | | |
1509 | | /* |
1510 | | ** Code commutative operators ('+', '*'). If first operand is a |
1511 | | ** numeric constant, change order of operands to try to use an |
1512 | | ** immediate or K operator. |
1513 | | */ |
1514 | | static void codecommutative (FuncState *fs, BinOpr op, |
1515 | 34.4k | expdesc *e1, expdesc *e2, int line) { |
1516 | 34.4k | int flip = 0; |
1517 | 34.4k | if (tonumeral(e1, NULL)) { /* is first operand a numeric constant? */ |
1518 | 4.65k | swapexps(e1, e2); /* change order */ |
1519 | 4.65k | flip = 1; |
1520 | 4.65k | } |
1521 | 34.4k | if (op == OPR_ADD && isSCint(e2)) /* immediate operand? */ |
1522 | 1.05k | codebini(fs, OP_ADDI, e1, e2, flip, line, TM_ADD); |
1523 | 33.4k | else |
1524 | 33.4k | codearith(fs, op, e1, e2, flip, line); |
1525 | 34.4k | } |
1526 | | |
1527 | | |
1528 | | /* |
1529 | | ** Code bitwise operations; they are all commutative, so the function |
1530 | | ** tries to put an integer constant as the 2nd operand (a K operand). |
1531 | | */ |
1532 | | static void codebitwise (FuncState *fs, BinOpr opr, |
1533 | 69.9k | expdesc *e1, expdesc *e2, int line) { |
1534 | 69.9k | int flip = 0; |
1535 | 69.9k | if (e1->k == VKINT) { |
1536 | 5.22k | swapexps(e1, e2); /* 'e2' will be the constant operand */ |
1537 | 5.22k | flip = 1; |
1538 | 5.22k | } |
1539 | 69.9k | if (e2->k == VKINT && luaK_exp2K(fs, e2)) /* K operand? */ |
1540 | 25.0k | codebinK(fs, opr, e1, e2, flip, line); |
1541 | 44.9k | else /* no constants */ |
1542 | 44.9k | codebinNoK(fs, opr, e1, e2, flip, line); |
1543 | 69.9k | } |
1544 | | |
1545 | | |
1546 | | /* |
1547 | | ** Emit code for order comparisons. When using an immediate operand, |
1548 | | ** 'isfloat' tells whether the original value was a float. |
1549 | | */ |
1550 | 6.45M | static void codeorder (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { |
1551 | 6.45M | int r1, r2; |
1552 | 6.45M | int im; |
1553 | 6.45M | int isfloat = 0; |
1554 | 6.45M | OpCode op; |
1555 | 6.45M | if (isSCnumber(e2, &im, &isfloat)) { |
1556 | | /* use immediate operand */ |
1557 | 5.91k | r1 = luaK_exp2anyreg(fs, e1); |
1558 | 5.91k | r2 = im; |
1559 | 5.91k | op = binopr2op(opr, OPR_LT, OP_LTI); |
1560 | 5.91k | } |
1561 | 6.44M | else if (isSCnumber(e1, &im, &isfloat)) { |
1562 | | /* transform (A < B) to (B > A) and (A <= B) to (B >= A) */ |
1563 | 7.98k | r1 = luaK_exp2anyreg(fs, e2); |
1564 | 7.98k | r2 = im; |
1565 | 7.98k | op = binopr2op(opr, OPR_LT, OP_GTI); |
1566 | 7.98k | } |
1567 | 6.43M | else { /* regular case, compare two registers */ |
1568 | 6.43M | r1 = luaK_exp2anyreg(fs, e1); |
1569 | 6.43M | r2 = luaK_exp2anyreg(fs, e2); |
1570 | 6.43M | op = binopr2op(opr, OPR_LT, OP_LT); |
1571 | 6.43M | } |
1572 | 6.45M | freeexps(fs, e1, e2); |
1573 | 6.45M | e1->u.info = condjump(fs, op, r1, r2, isfloat, 1); |
1574 | 6.45M | e1->k = VJMP; |
1575 | 6.45M | } |
1576 | | |
1577 | | |
1578 | | /* |
1579 | | ** Emit code for equality comparisons ('==', '~='). |
1580 | | ** 'e1' was already put as RK by 'luaK_infix'. |
1581 | | */ |
1582 | 10.2k | static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { |
1583 | 10.2k | int r1, r2; |
1584 | 10.2k | int im; |
1585 | 10.2k | int isfloat = 0; /* not needed here, but kept for symmetry */ |
1586 | 10.2k | OpCode op; |
1587 | 10.2k | if (e1->k != VNONRELOC) { |
1588 | 5.51k | lua_assert(e1->k == VK || e1->k == VKINT || e1->k == VKFLT); |
1589 | 5.51k | swapexps(e1, e2); |
1590 | 5.51k | } |
1591 | 10.2k | r1 = luaK_exp2anyreg(fs, e1); /* 1st expression must be in register */ |
1592 | 10.2k | if (isSCnumber(e2, &im, &isfloat)) { |
1593 | 3.30k | op = OP_EQI; |
1594 | 3.30k | r2 = im; /* immediate operand */ |
1595 | 3.30k | } |
1596 | 6.96k | else if (exp2RK(fs, e2)) { /* 2nd expression is constant? */ |
1597 | 3.10k | op = OP_EQK; |
1598 | 3.10k | r2 = e2->u.info; /* constant index */ |
1599 | 3.10k | } |
1600 | 3.86k | else { |
1601 | 3.86k | op = OP_EQ; /* will compare two registers */ |
1602 | 3.86k | r2 = luaK_exp2anyreg(fs, e2); |
1603 | 3.86k | } |
1604 | 10.2k | freeexps(fs, e1, e2); |
1605 | 10.2k | e1->u.info = condjump(fs, op, r1, r2, isfloat, (opr == OPR_EQ)); |
1606 | 10.2k | e1->k = VJMP; |
1607 | 10.2k | } |
1608 | | |
1609 | | |
1610 | | /* |
1611 | | ** Apply prefix operation 'op' to expression 'e'. |
1612 | | */ |
1613 | 325k | void luaK_prefix (FuncState *fs, UnOpr opr, expdesc *e, int line) { |
1614 | 325k | static const expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP}; |
1615 | 325k | luaK_dischargevars(fs, e); |
1616 | 325k | switch (opr) { |
1617 | 282k | case OPR_MINUS: case OPR_BNOT: /* use 'ef' as fake 2nd operand */ |
1618 | 282k | if (constfolding(fs, opr + LUA_OPUNM, e, &ef)) |
1619 | 58.3k | break; |
1620 | | /* else */ /* FALLTHROUGH */ |
1621 | 260k | case OPR_LEN: |
1622 | 260k | codeunexpval(fs, unopr2op(opr), e, line); |
1623 | 260k | break; |
1624 | 6.33k | case OPR_NOT: codenot(fs, e); break; |
1625 | 0 | default: lua_assert(0); |
1626 | 325k | } |
1627 | 325k | } |
1628 | | |
1629 | | |
1630 | | /* |
1631 | | ** Process 1st operand 'v' of binary operation 'op' before reading |
1632 | | ** 2nd operand. |
1633 | | */ |
1634 | 13.0M | void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { |
1635 | 13.0M | luaK_dischargevars(fs, v); |
1636 | 13.0M | switch (op) { |
1637 | 32.0k | case OPR_AND: { |
1638 | 32.0k | luaK_goiftrue(fs, v); /* go ahead only if 'v' is true */ |
1639 | 32.0k | break; |
1640 | 0 | } |
1641 | 191k | case OPR_OR: { |
1642 | 191k | luaK_goiffalse(fs, v); /* go ahead only if 'v' is false */ |
1643 | 191k | break; |
1644 | 0 | } |
1645 | 18.7k | case OPR_CONCAT: { |
1646 | 18.7k | luaK_exp2nextreg(fs, v); /* operand must be on the stack */ |
1647 | 18.7k | break; |
1648 | 0 | } |
1649 | 42.5k | case OPR_ADD: case OPR_SUB: |
1650 | 5.33M | case OPR_MUL: case OPR_DIV: case OPR_IDIV: |
1651 | 5.89M | case OPR_MOD: case OPR_POW: |
1652 | 5.96M | case OPR_BAND: case OPR_BOR: case OPR_BXOR: |
1653 | 6.33M | case OPR_SHL: case OPR_SHR: { |
1654 | 6.33M | if (!tonumeral(v, NULL)) |
1655 | 6.16M | luaK_exp2anyreg(fs, v); |
1656 | | /* else keep numeral, which may be folded or used as an immediate |
1657 | | operand */ |
1658 | 6.33M | break; |
1659 | 6.32M | } |
1660 | 10.4k | case OPR_EQ: case OPR_NE: { |
1661 | 10.4k | if (!tonumeral(v, NULL)) |
1662 | 6.09k | exp2RK(fs, v); |
1663 | | /* else keep numeral, which may be an immediate operand */ |
1664 | 10.4k | break; |
1665 | 5.25k | } |
1666 | 6.40M | case OPR_LT: case OPR_LE: |
1667 | 6.45M | case OPR_GT: case OPR_GE: { |
1668 | 6.45M | int dummy, dummy2; |
1669 | 6.45M | if (!isSCnumber(v, &dummy, &dummy2)) |
1670 | 6.44M | luaK_exp2anyreg(fs, v); |
1671 | | /* else keep numeral, which may be an immediate operand */ |
1672 | 6.45M | break; |
1673 | 6.45M | } |
1674 | 0 | default: lua_assert(0); |
1675 | 13.0M | } |
1676 | 13.0M | } |
1677 | | |
1678 | | /* |
1679 | | ** Create code for '(e1 .. e2)'. |
1680 | | ** For '(e1 .. e2.1 .. e2.2)' (which is '(e1 .. (e2.1 .. e2.2))', |
1681 | | ** because concatenation is right associative), merge both CONCATs. |
1682 | | */ |
1683 | 18.6k | static void codeconcat (FuncState *fs, expdesc *e1, expdesc *e2, int line) { |
1684 | 18.6k | Instruction *ie2 = previousinstruction(fs); |
1685 | 18.6k | if (GET_OPCODE(*ie2) == OP_CONCAT) { /* is 'e2' a concatenation? */ |
1686 | 8.76k | int n = GETARG_B(*ie2); /* # of elements concatenated in 'e2' */ |
1687 | 8.76k | lua_assert(e1->u.info + 1 == GETARG_A(*ie2)); |
1688 | 8.76k | freeexp(fs, e2); |
1689 | 8.76k | SETARG_A(*ie2, e1->u.info); /* correct first element ('e1') */ |
1690 | 8.76k | SETARG_B(*ie2, n + 1); /* will concatenate one more element */ |
1691 | 8.76k | } |
1692 | 9.87k | else { /* 'e2' is not a concatenation */ |
1693 | 9.87k | luaK_codeABC(fs, OP_CONCAT, e1->u.info, 2, 0); /* new concat opcode */ |
1694 | 9.87k | freeexp(fs, e2); |
1695 | 9.87k | luaK_fixline(fs, line); |
1696 | 9.87k | } |
1697 | 18.6k | } |
1698 | | |
1699 | | |
1700 | | /* |
1701 | | ** Finalize code for binary operation, after reading 2nd operand. |
1702 | | */ |
1703 | | void luaK_posfix (FuncState *fs, BinOpr opr, |
1704 | 13.0M | expdesc *e1, expdesc *e2, int line) { |
1705 | 13.0M | luaK_dischargevars(fs, e2); |
1706 | 13.0M | if (foldbinop(opr) && constfolding(fs, opr + LUA_OPADD, e1, e2)) |
1707 | 121k | return; /* done by folding */ |
1708 | 12.9M | switch (opr) { |
1709 | 30.4k | case OPR_AND: { |
1710 | 30.4k | lua_assert(e1->t == NO_JUMP); /* list closed by 'luaK_infix' */ |
1711 | 30.4k | luaK_concat(fs, &e2->f, e1->f); |
1712 | 30.4k | *e1 = *e2; |
1713 | 30.4k | break; |
1714 | 0 | } |
1715 | 191k | case OPR_OR: { |
1716 | 191k | lua_assert(e1->f == NO_JUMP); /* list closed by 'luaK_infix' */ |
1717 | 191k | luaK_concat(fs, &e2->t, e1->t); |
1718 | 191k | *e1 = *e2; |
1719 | 191k | break; |
1720 | 0 | } |
1721 | 18.6k | case OPR_CONCAT: { /* e1 .. e2 */ |
1722 | 18.6k | luaK_exp2nextreg(fs, e2); |
1723 | 18.6k | codeconcat(fs, e1, e2, line); |
1724 | 18.6k | break; |
1725 | 0 | } |
1726 | 34.4k | case OPR_ADD: case OPR_MUL: { |
1727 | 34.4k | codecommutative(fs, opr, e1, e2, line); |
1728 | 34.4k | break; |
1729 | 3.88k | } |
1730 | 31.4k | case OPR_SUB: { |
1731 | 31.4k | if (finishbinexpneg(fs, e1, e2, OP_ADDI, line, TM_SUB)) |
1732 | 17.9k | break; /* coded as (r1 + -I) */ |
1733 | | /* ELSE */ |
1734 | 31.4k | } /* FALLTHROUGH */ |
1735 | 5.72M | case OPR_DIV: case OPR_IDIV: case OPR_MOD: case OPR_POW: { |
1736 | 5.72M | codearith(fs, opr, e1, e2, 0, line); |
1737 | 5.72M | break; |
1738 | 5.27M | } |
1739 | 69.9k | case OPR_BAND: case OPR_BOR: case OPR_BXOR: { |
1740 | 69.9k | codebitwise(fs, opr, e1, e2, line); |
1741 | 69.9k | break; |
1742 | 22.3k | } |
1743 | 354k | case OPR_SHL: { |
1744 | 354k | if (isSCint(e1)) { |
1745 | 4.08k | swapexps(e1, e2); |
1746 | 4.08k | codebini(fs, OP_SHLI, e1, e2, 1, line, TM_SHL); /* I << r2 */ |
1747 | 4.08k | } |
1748 | 350k | else if (finishbinexpneg(fs, e1, e2, OP_SHRI, line, TM_SHL)) { |
1749 | 1.24k | /* coded as (r1 >> -I) */; |
1750 | 1.24k | } |
1751 | 349k | else /* regular case (two registers) */ |
1752 | 349k | codebinexpval(fs, opr, e1, e2, line); |
1753 | 354k | break; |
1754 | 22.3k | } |
1755 | 4.80k | case OPR_SHR: { |
1756 | 4.80k | if (isSCint(e2)) |
1757 | 1.19k | codebini(fs, OP_SHRI, e1, e2, 0, line, TM_SHR); /* r1 >> I */ |
1758 | 3.60k | else /* regular case (two registers) */ |
1759 | 3.60k | codebinexpval(fs, opr, e1, e2, line); |
1760 | 4.80k | break; |
1761 | 22.3k | } |
1762 | 10.2k | case OPR_EQ: case OPR_NE: { |
1763 | 10.2k | codeeq(fs, opr, e1, e2); |
1764 | 10.2k | break; |
1765 | 5.17k | } |
1766 | 49.8k | case OPR_GT: case OPR_GE: { |
1767 | | /* '(a > b)' <=> '(b < a)'; '(a >= b)' <=> '(b <= a)' */ |
1768 | 49.8k | swapexps(e1, e2); |
1769 | 49.8k | opr = cast(BinOpr, (opr - OPR_GT) + OPR_LT); |
1770 | 49.8k | } /* FALLTHROUGH */ |
1771 | 6.45M | case OPR_LT: case OPR_LE: { |
1772 | 6.45M | codeorder(fs, opr, e1, e2); |
1773 | 6.45M | break; |
1774 | 6.44M | } |
1775 | 0 | default: lua_assert(0); |
1776 | 12.9M | } |
1777 | 12.9M | } |
1778 | | |
1779 | | |
1780 | | /* |
1781 | | ** Change line information associated with current position, by removing |
1782 | | ** previous info and adding it again with new line. |
1783 | | */ |
1784 | 13.1M | void luaK_fixline (FuncState *fs, int line) { |
1785 | 13.1M | removelastlineinfo(fs); |
1786 | 13.1M | savelineinfo(fs, fs->f, line); |
1787 | 13.1M | } |
1788 | | |
1789 | | |
1790 | 228 | void luaK_settablesize (FuncState *fs, int pc, int ra, int asize, int hsize) { |
1791 | 228 | Instruction *inst = &fs->f->code[pc]; |
1792 | 228 | int rb = (hsize != 0) ? luaO_ceillog2(hsize) + 1 : 0; /* hash size */ |
1793 | 228 | int extra = asize / (MAXARG_C + 1); /* higher bits of array size */ |
1794 | 228 | int rc = asize % (MAXARG_C + 1); /* lower bits of array size */ |
1795 | 228 | int k = (extra > 0); /* true iff needs extra argument */ |
1796 | 228 | *inst = CREATE_ABCk(OP_NEWTABLE, ra, rb, rc, k); |
1797 | 228 | *(inst + 1) = CREATE_Ax(OP_EXTRAARG, extra); |
1798 | 228 | } |
1799 | | |
1800 | | |
1801 | | /* |
1802 | | ** Emit a SETLIST instruction. |
1803 | | ** 'base' is register that keeps table; |
1804 | | ** 'nelems' is #table plus those to be stored now; |
1805 | | ** 'tostore' is number of values (in registers 'base + 1',...) to add to |
1806 | | ** table (or LUA_MULTRET to add up to stack top). |
1807 | | */ |
1808 | 46 | void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) { |
1809 | 46 | lua_assert(tostore != 0 && tostore <= LFIELDS_PER_FLUSH); |
1810 | 46 | if (tostore == LUA_MULTRET) |
1811 | 4 | tostore = 0; |
1812 | 46 | if (nelems <= MAXARG_C) |
1813 | 46 | luaK_codeABC(fs, OP_SETLIST, base, tostore, nelems); |
1814 | 0 | else { |
1815 | 0 | int extra = nelems / (MAXARG_C + 1); |
1816 | 0 | nelems %= (MAXARG_C + 1); |
1817 | 0 | luaK_codeABCk(fs, OP_SETLIST, base, tostore, nelems, 1); |
1818 | 0 | codeextraarg(fs, extra); |
1819 | 0 | } |
1820 | 46 | fs->freereg = base + 1; /* free registers with list values */ |
1821 | 46 | } |
1822 | | |
1823 | | |
1824 | | /* |
1825 | | ** return the final target of a jump (skipping jumps to jumps) |
1826 | | */ |
1827 | 225k | static int finaltarget (Instruction *code, int i) { |
1828 | 225k | int count; |
1829 | 453k | for (count = 0; count < 100; count++) { /* avoid infinite loops */ |
1830 | 453k | Instruction pc = code[i]; |
1831 | 453k | if (GET_OPCODE(pc) != OP_JMP) |
1832 | 225k | break; |
1833 | 227k | else |
1834 | 227k | i += GETARG_sJ(pc) + 1; |
1835 | 453k | } |
1836 | 225k | return i; |
1837 | 225k | } |
1838 | | |
1839 | | |
1840 | | /* |
1841 | | ** Do a final pass over the code of a function, doing small peephole |
1842 | | ** optimizations and adjustments. |
1843 | | */ |
1844 | 2.78k | void luaK_finish (FuncState *fs) { |
1845 | 2.78k | int i; |
1846 | 2.78k | Proto *p = fs->f; |
1847 | 1.71M | for (i = 0; i < fs->pc; i++) { |
1848 | 1.71M | Instruction *pc = &p->code[i]; |
1849 | 1.71M | lua_assert(i == 0 || isOT(*(pc - 1)) == isIT(*pc)); |
1850 | 1.71M | switch (GET_OPCODE(*pc)) { |
1851 | 3.25k | case OP_RETURN0: case OP_RETURN1: { |
1852 | 3.25k | if (!(fs->needclose || p->is_vararg)) |
1853 | 2.73k | break; /* no extra work */ |
1854 | | /* else use OP_RETURN to do the extra work */ |
1855 | 518 | SET_OPCODE(*pc, OP_RETURN); |
1856 | 518 | } /* FALLTHROUGH */ |
1857 | 1.31k | case OP_RETURN: case OP_TAILCALL: { |
1858 | 1.31k | if (fs->needclose) |
1859 | 1.31k | SETARG_k(*pc, 1); /* signal that it needs to close */ |
1860 | 1.31k | if (p->is_vararg) |
1861 | 1.31k | SETARG_C(*pc, p->numparams + 1); /* signal that it is vararg */ |
1862 | 1.31k | break; |
1863 | 1.01k | } |
1864 | 225k | case OP_JMP: { |
1865 | 225k | int target = finaltarget(p->code, i); |
1866 | 225k | fixjump(fs, i, target); |
1867 | 225k | break; |
1868 | 1.01k | } |
1869 | 1.48M | default: break; |
1870 | 1.71M | } |
1871 | 1.71M | } |
1872 | 2.78k | } |