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