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