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