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