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