annotate src/lua/lcode.c @ 574:be6f46094ad0

implemented video-memory pointer logic.
author Robert McIntyre <rlm@mit.edu>
date Sat, 01 Sep 2012 04:05:51 -0500
parents 27763b933818
children
rev   line source
rlm@1 1 /*
rlm@1 2 ** $Id: lcode.c,v 2.25.1.3 2007/12/28 15:32:23 roberto Exp $
rlm@1 3 ** Code generator for Lua
rlm@1 4 ** See Copyright Notice in lua.h
rlm@1 5 */
rlm@1 6
rlm@1 7
rlm@1 8 #include <stdlib.h>
rlm@1 9
rlm@1 10 #define lcode_c
rlm@1 11 #define LUA_CORE
rlm@1 12
rlm@1 13 #include "lua.h"
rlm@1 14
rlm@1 15 #include "lcode.h"
rlm@1 16 #include "ldebug.h"
rlm@1 17 #include "ldo.h"
rlm@1 18 #include "lgc.h"
rlm@1 19 #include "llex.h"
rlm@1 20 #include "lmem.h"
rlm@1 21 #include "lobject.h"
rlm@1 22 #include "lopcodes.h"
rlm@1 23 #include "lparser.h"
rlm@1 24 #include "ltable.h"
rlm@1 25
rlm@1 26
rlm@1 27 #define hasjumps(e) ((e)->t != (e)->f)
rlm@1 28
rlm@1 29
rlm@1 30 static int isnumeral(expdesc *e) {
rlm@1 31 return (e->k == VKNUM && e->t == NO_JUMP && e->f == NO_JUMP);
rlm@1 32 }
rlm@1 33
rlm@1 34
rlm@1 35 void luaK_nil (FuncState *fs, int from, int n) {
rlm@1 36 Instruction *previous;
rlm@1 37 if (fs->pc > fs->lasttarget) { /* no jumps to current position? */
rlm@1 38 if (fs->pc == 0) { /* function start? */
rlm@1 39 if (from >= fs->nactvar)
rlm@1 40 return; /* positions are already clean */
rlm@1 41 }
rlm@1 42 else {
rlm@1 43 previous = &fs->f->code[fs->pc-1];
rlm@1 44 if (GET_OPCODE(*previous) == OP_LOADNIL) {
rlm@1 45 int pfrom = GETARG_A(*previous);
rlm@1 46 int pto = GETARG_B(*previous);
rlm@1 47 if (pfrom <= from && from <= pto+1) { /* can connect both? */
rlm@1 48 if (from+n-1 > pto)
rlm@1 49 SETARG_B(*previous, from+n-1);
rlm@1 50 return;
rlm@1 51 }
rlm@1 52 }
rlm@1 53 }
rlm@1 54 }
rlm@1 55 luaK_codeABC(fs, OP_LOADNIL, from, from+n-1, 0); /* else no optimization */
rlm@1 56 }
rlm@1 57
rlm@1 58
rlm@1 59 int luaK_jump (FuncState *fs) {
rlm@1 60 int jpc = fs->jpc; /* save list of jumps to here */
rlm@1 61 int j;
rlm@1 62 fs->jpc = NO_JUMP;
rlm@1 63 j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP);
rlm@1 64 luaK_concat(fs, &j, jpc); /* keep them on hold */
rlm@1 65 return j;
rlm@1 66 }
rlm@1 67
rlm@1 68
rlm@1 69 void luaK_ret (FuncState *fs, int first, int nret) {
rlm@1 70 luaK_codeABC(fs, OP_RETURN, first, nret+1, 0);
rlm@1 71 }
rlm@1 72
rlm@1 73
rlm@1 74 static int condjump (FuncState *fs, OpCode op, int A, int B, int C) {
rlm@1 75 luaK_codeABC(fs, op, A, B, C);
rlm@1 76 return luaK_jump(fs);
rlm@1 77 }
rlm@1 78
rlm@1 79
rlm@1 80 static void fixjump (FuncState *fs, int pc, int dest) {
rlm@1 81 Instruction *jmp = &fs->f->code[pc];
rlm@1 82 int offset = dest-(pc+1);
rlm@1 83 lua_assert(dest != NO_JUMP);
rlm@1 84 if (abs(offset) > MAXARG_sBx)
rlm@1 85 luaX_syntaxerror(fs->ls, "control structure too long");
rlm@1 86 SETARG_sBx(*jmp, offset);
rlm@1 87 }
rlm@1 88
rlm@1 89
rlm@1 90 /*
rlm@1 91 ** returns current `pc' and marks it as a jump target (to avoid wrong
rlm@1 92 ** optimizations with consecutive instructions not in the same basic block).
rlm@1 93 */
rlm@1 94 int luaK_getlabel (FuncState *fs) {
rlm@1 95 fs->lasttarget = fs->pc;
rlm@1 96 return fs->pc;
rlm@1 97 }
rlm@1 98
rlm@1 99
rlm@1 100 static int getjump (FuncState *fs, int pc) {
rlm@1 101 int offset = GETARG_sBx(fs->f->code[pc]);
rlm@1 102 if (offset == NO_JUMP) /* point to itself represents end of list */
rlm@1 103 return NO_JUMP; /* end of list */
rlm@1 104 else
rlm@1 105 return (pc+1)+offset; /* turn offset into absolute position */
rlm@1 106 }
rlm@1 107
rlm@1 108
rlm@1 109 static Instruction *getjumpcontrol (FuncState *fs, int pc) {
rlm@1 110 Instruction *pi = &fs->f->code[pc];
rlm@1 111 if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1))))
rlm@1 112 return pi-1;
rlm@1 113 else
rlm@1 114 return pi;
rlm@1 115 }
rlm@1 116
rlm@1 117
rlm@1 118 /*
rlm@1 119 ** check whether list has any jump that do not produce a value
rlm@1 120 ** (or produce an inverted value)
rlm@1 121 */
rlm@1 122 static int need_value (FuncState *fs, int list) {
rlm@1 123 for (; list != NO_JUMP; list = getjump(fs, list)) {
rlm@1 124 Instruction i = *getjumpcontrol(fs, list);
rlm@1 125 if (GET_OPCODE(i) != OP_TESTSET) return 1;
rlm@1 126 }
rlm@1 127 return 0; /* not found */
rlm@1 128 }
rlm@1 129
rlm@1 130
rlm@1 131 static int patchtestreg (FuncState *fs, int node, int reg) {
rlm@1 132 Instruction *i = getjumpcontrol(fs, node);
rlm@1 133 if (GET_OPCODE(*i) != OP_TESTSET)
rlm@1 134 return 0; /* cannot patch other instructions */
rlm@1 135 if (reg != NO_REG && reg != GETARG_B(*i))
rlm@1 136 SETARG_A(*i, reg);
rlm@1 137 else /* no register to put value or register already has the value */
rlm@1 138 *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i));
rlm@1 139
rlm@1 140 return 1;
rlm@1 141 }
rlm@1 142
rlm@1 143
rlm@1 144 static void removevalues (FuncState *fs, int list) {
rlm@1 145 for (; list != NO_JUMP; list = getjump(fs, list))
rlm@1 146 patchtestreg(fs, list, NO_REG);
rlm@1 147 }
rlm@1 148
rlm@1 149
rlm@1 150 static void patchlistaux (FuncState *fs, int list, int vtarget, int reg,
rlm@1 151 int dtarget) {
rlm@1 152 while (list != NO_JUMP) {
rlm@1 153 int next = getjump(fs, list);
rlm@1 154 if (patchtestreg(fs, list, reg))
rlm@1 155 fixjump(fs, list, vtarget);
rlm@1 156 else
rlm@1 157 fixjump(fs, list, dtarget); /* jump to default target */
rlm@1 158 list = next;
rlm@1 159 }
rlm@1 160 }
rlm@1 161
rlm@1 162
rlm@1 163 static void dischargejpc (FuncState *fs) {
rlm@1 164 patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc);
rlm@1 165 fs->jpc = NO_JUMP;
rlm@1 166 }
rlm@1 167
rlm@1 168
rlm@1 169 void luaK_patchlist (FuncState *fs, int list, int target) {
rlm@1 170 if (target == fs->pc)
rlm@1 171 luaK_patchtohere(fs, list);
rlm@1 172 else {
rlm@1 173 lua_assert(target < fs->pc);
rlm@1 174 patchlistaux(fs, list, target, NO_REG, target);
rlm@1 175 }
rlm@1 176 }
rlm@1 177
rlm@1 178
rlm@1 179 void luaK_patchtohere (FuncState *fs, int list) {
rlm@1 180 luaK_getlabel(fs);
rlm@1 181 luaK_concat(fs, &fs->jpc, list);
rlm@1 182 }
rlm@1 183
rlm@1 184
rlm@1 185 void luaK_concat (FuncState *fs, int *l1, int l2) {
rlm@1 186 if (l2 == NO_JUMP) return;
rlm@1 187 else if (*l1 == NO_JUMP)
rlm@1 188 *l1 = l2;
rlm@1 189 else {
rlm@1 190 int list = *l1;
rlm@1 191 int next;
rlm@1 192 while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */
rlm@1 193 list = next;
rlm@1 194 fixjump(fs, list, l2);
rlm@1 195 }
rlm@1 196 }
rlm@1 197
rlm@1 198
rlm@1 199 void luaK_checkstack (FuncState *fs, int n) {
rlm@1 200 int newstack = fs->freereg + n;
rlm@1 201 if (newstack > fs->f->maxstacksize) {
rlm@1 202 if (newstack >= MAXSTACK)
rlm@1 203 luaX_syntaxerror(fs->ls, "function or expression too complex");
rlm@1 204 fs->f->maxstacksize = cast_byte(newstack);
rlm@1 205 }
rlm@1 206 }
rlm@1 207
rlm@1 208
rlm@1 209 void luaK_reserveregs (FuncState *fs, int n) {
rlm@1 210 luaK_checkstack(fs, n);
rlm@1 211 fs->freereg += n;
rlm@1 212 }
rlm@1 213
rlm@1 214
rlm@1 215 static void freereg (FuncState *fs, int reg) {
rlm@1 216 if (!ISK(reg) && reg >= fs->nactvar) {
rlm@1 217 fs->freereg--;
rlm@1 218 lua_assert(reg == fs->freereg);
rlm@1 219 }
rlm@1 220 }
rlm@1 221
rlm@1 222
rlm@1 223 static void freeexp (FuncState *fs, expdesc *e) {
rlm@1 224 if (e->k == VNONRELOC)
rlm@1 225 freereg(fs, e->u.s.info);
rlm@1 226 }
rlm@1 227
rlm@1 228
rlm@1 229 static int addk (FuncState *fs, TValue *k, TValue *v) {
rlm@1 230 lua_State *L = fs->L;
rlm@1 231 TValue *idx = luaH_set(L, fs->h, k);
rlm@1 232 Proto *f = fs->f;
rlm@1 233 int oldsize = f->sizek;
rlm@1 234 if (ttisnumber(idx)) {
rlm@1 235 lua_assert(luaO_rawequalObj(&fs->f->k[cast_int(nvalue(idx))], v));
rlm@1 236 return cast_int(nvalue(idx));
rlm@1 237 }
rlm@1 238 else { /* constant not found; create a new entry */
rlm@1 239 setnvalue(idx, cast_num(fs->nk));
rlm@1 240 luaM_growvector(L, f->k, fs->nk, f->sizek, TValue,
rlm@1 241 MAXARG_Bx, "constant table overflow");
rlm@1 242 while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
rlm@1 243 setobj(L, &f->k[fs->nk], v);
rlm@1 244 luaC_barrier(L, f, v);
rlm@1 245 return fs->nk++;
rlm@1 246 }
rlm@1 247 }
rlm@1 248
rlm@1 249
rlm@1 250 int luaK_stringK (FuncState *fs, TString *s) {
rlm@1 251 TValue o;
rlm@1 252 setsvalue(fs->L, &o, s);
rlm@1 253 return addk(fs, &o, &o);
rlm@1 254 }
rlm@1 255
rlm@1 256
rlm@1 257 int luaK_numberK (FuncState *fs, lua_Number r) {
rlm@1 258 TValue o;
rlm@1 259 setnvalue(&o, r);
rlm@1 260 return addk(fs, &o, &o);
rlm@1 261 }
rlm@1 262
rlm@1 263
rlm@1 264 static int boolK (FuncState *fs, int b) {
rlm@1 265 TValue o;
rlm@1 266 setbvalue(&o, b);
rlm@1 267 return addk(fs, &o, &o);
rlm@1 268 }
rlm@1 269
rlm@1 270
rlm@1 271 static int nilK (FuncState *fs) {
rlm@1 272 TValue k, v;
rlm@1 273 setnilvalue(&v);
rlm@1 274 /* cannot use nil as key; instead use table itself to represent nil */
rlm@1 275 sethvalue(fs->L, &k, fs->h);
rlm@1 276 return addk(fs, &k, &v);
rlm@1 277 }
rlm@1 278
rlm@1 279
rlm@1 280 void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {
rlm@1 281 if (e->k == VCALL) { /* expression is an open function call? */
rlm@1 282 SETARG_C(getcode(fs, e), nresults+1);
rlm@1 283 }
rlm@1 284 else if (e->k == VVARARG) {
rlm@1 285 SETARG_B(getcode(fs, e), nresults+1);
rlm@1 286 SETARG_A(getcode(fs, e), fs->freereg);
rlm@1 287 luaK_reserveregs(fs, 1);
rlm@1 288 }
rlm@1 289 }
rlm@1 290
rlm@1 291
rlm@1 292 void luaK_setoneret (FuncState *fs, expdesc *e) {
rlm@1 293 if (e->k == VCALL) { /* expression is an open function call? */
rlm@1 294 e->k = VNONRELOC;
rlm@1 295 e->u.s.info = GETARG_A(getcode(fs, e));
rlm@1 296 }
rlm@1 297 else if (e->k == VVARARG) {
rlm@1 298 SETARG_B(getcode(fs, e), 2);
rlm@1 299 e->k = VRELOCABLE; /* can relocate its simple result */
rlm@1 300 }
rlm@1 301 }
rlm@1 302
rlm@1 303
rlm@1 304 void luaK_dischargevars (FuncState *fs, expdesc *e) {
rlm@1 305 switch (e->k) {
rlm@1 306 case VLOCAL: {
rlm@1 307 e->k = VNONRELOC;
rlm@1 308 break;
rlm@1 309 }
rlm@1 310 case VUPVAL: {
rlm@1 311 e->u.s.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.s.info, 0);
rlm@1 312 e->k = VRELOCABLE;
rlm@1 313 break;
rlm@1 314 }
rlm@1 315 case VGLOBAL: {
rlm@1 316 e->u.s.info = luaK_codeABx(fs, OP_GETGLOBAL, 0, e->u.s.info);
rlm@1 317 e->k = VRELOCABLE;
rlm@1 318 break;
rlm@1 319 }
rlm@1 320 case VINDEXED: {
rlm@1 321 freereg(fs, e->u.s.aux);
rlm@1 322 freereg(fs, e->u.s.info);
rlm@1 323 e->u.s.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.s.info, e->u.s.aux);
rlm@1 324 e->k = VRELOCABLE;
rlm@1 325 break;
rlm@1 326 }
rlm@1 327 case VVARARG:
rlm@1 328 case VCALL: {
rlm@1 329 luaK_setoneret(fs, e);
rlm@1 330 break;
rlm@1 331 }
rlm@1 332 default: break; /* there is one value available (somewhere) */
rlm@1 333 }
rlm@1 334 }
rlm@1 335
rlm@1 336
rlm@1 337 static int code_label (FuncState *fs, int A, int b, int jump) {
rlm@1 338 luaK_getlabel(fs); /* those instructions may be jump targets */
rlm@1 339 return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);
rlm@1 340 }
rlm@1 341
rlm@1 342
rlm@1 343 static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
rlm@1 344 luaK_dischargevars(fs, e);
rlm@1 345 switch (e->k) {
rlm@1 346 case VNIL: {
rlm@1 347 luaK_nil(fs, reg, 1);
rlm@1 348 break;
rlm@1 349 }
rlm@1 350 case VFALSE: case VTRUE: {
rlm@1 351 luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
rlm@1 352 break;
rlm@1 353 }
rlm@1 354 case VK: {
rlm@1 355 luaK_codeABx(fs, OP_LOADK, reg, e->u.s.info);
rlm@1 356 break;
rlm@1 357 }
rlm@1 358 case VKNUM: {
rlm@1 359 luaK_codeABx(fs, OP_LOADK, reg, luaK_numberK(fs, e->u.nval));
rlm@1 360 break;
rlm@1 361 }
rlm@1 362 case VRELOCABLE: {
rlm@1 363 Instruction *pc = &getcode(fs, e);
rlm@1 364 SETARG_A(*pc, reg);
rlm@1 365 break;
rlm@1 366 }
rlm@1 367 case VNONRELOC: {
rlm@1 368 if (reg != e->u.s.info)
rlm@1 369 luaK_codeABC(fs, OP_MOVE, reg, e->u.s.info, 0);
rlm@1 370 break;
rlm@1 371 }
rlm@1 372 default: {
rlm@1 373 lua_assert(e->k == VVOID || e->k == VJMP);
rlm@1 374 return; /* nothing to do... */
rlm@1 375 }
rlm@1 376 }
rlm@1 377 e->u.s.info = reg;
rlm@1 378 e->k = VNONRELOC;
rlm@1 379 }
rlm@1 380
rlm@1 381
rlm@1 382 static void discharge2anyreg (FuncState *fs, expdesc *e) {
rlm@1 383 if (e->k != VNONRELOC) {
rlm@1 384 luaK_reserveregs(fs, 1);
rlm@1 385 discharge2reg(fs, e, fs->freereg-1);
rlm@1 386 }
rlm@1 387 }
rlm@1 388
rlm@1 389
rlm@1 390 static void exp2reg (FuncState *fs, expdesc *e, int reg) {
rlm@1 391 discharge2reg(fs, e, reg);
rlm@1 392 if (e->k == VJMP)
rlm@1 393 luaK_concat(fs, &e->t, e->u.s.info); /* put this jump in `t' list */
rlm@1 394 if (hasjumps(e)) {
rlm@1 395 int final; /* position after whole expression */
rlm@1 396 int p_f = NO_JUMP; /* position of an eventual LOAD false */
rlm@1 397 int p_t = NO_JUMP; /* position of an eventual LOAD true */
rlm@1 398 if (need_value(fs, e->t) || need_value(fs, e->f)) {
rlm@1 399 int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs);
rlm@1 400 p_f = code_label(fs, reg, 0, 1);
rlm@1 401 p_t = code_label(fs, reg, 1, 0);
rlm@1 402 luaK_patchtohere(fs, fj);
rlm@1 403 }
rlm@1 404 final = luaK_getlabel(fs);
rlm@1 405 patchlistaux(fs, e->f, final, reg, p_f);
rlm@1 406 patchlistaux(fs, e->t, final, reg, p_t);
rlm@1 407 }
rlm@1 408 e->f = e->t = NO_JUMP;
rlm@1 409 e->u.s.info = reg;
rlm@1 410 e->k = VNONRELOC;
rlm@1 411 }
rlm@1 412
rlm@1 413
rlm@1 414 void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
rlm@1 415 luaK_dischargevars(fs, e);
rlm@1 416 freeexp(fs, e);
rlm@1 417 luaK_reserveregs(fs, 1);
rlm@1 418 exp2reg(fs, e, fs->freereg - 1);
rlm@1 419 }
rlm@1 420
rlm@1 421
rlm@1 422 int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
rlm@1 423 luaK_dischargevars(fs, e);
rlm@1 424 if (e->k == VNONRELOC) {
rlm@1 425 if (!hasjumps(e)) return e->u.s.info; /* exp is already in a register */
rlm@1 426 if (e->u.s.info >= fs->nactvar) { /* reg. is not a local? */
rlm@1 427 exp2reg(fs, e, e->u.s.info); /* put value on it */
rlm@1 428 return e->u.s.info;
rlm@1 429 }
rlm@1 430 }
rlm@1 431 luaK_exp2nextreg(fs, e); /* default */
rlm@1 432 return e->u.s.info;
rlm@1 433 }
rlm@1 434
rlm@1 435
rlm@1 436 void luaK_exp2val (FuncState *fs, expdesc *e) {
rlm@1 437 if (hasjumps(e))
rlm@1 438 luaK_exp2anyreg(fs, e);
rlm@1 439 else
rlm@1 440 luaK_dischargevars(fs, e);
rlm@1 441 }
rlm@1 442
rlm@1 443
rlm@1 444 int luaK_exp2RK (FuncState *fs, expdesc *e) {
rlm@1 445 luaK_exp2val(fs, e);
rlm@1 446 switch (e->k) {
rlm@1 447 case VKNUM:
rlm@1 448 case VTRUE:
rlm@1 449 case VFALSE:
rlm@1 450 case VNIL: {
rlm@1 451 if (fs->nk <= MAXINDEXRK) { /* constant fit in RK operand? */
rlm@1 452 e->u.s.info = (e->k == VNIL) ? nilK(fs) :
rlm@1 453 (e->k == VKNUM) ? luaK_numberK(fs, e->u.nval) :
rlm@1 454 boolK(fs, (e->k == VTRUE));
rlm@1 455 e->k = VK;
rlm@1 456 return RKASK(e->u.s.info);
rlm@1 457 }
rlm@1 458 else break;
rlm@1 459 }
rlm@1 460 case VK: {
rlm@1 461 if (e->u.s.info <= MAXINDEXRK) /* constant fit in argC? */
rlm@1 462 return RKASK(e->u.s.info);
rlm@1 463 else break;
rlm@1 464 }
rlm@1 465 default: break;
rlm@1 466 }
rlm@1 467 /* not a constant in the right range: put it in a register */
rlm@1 468 return luaK_exp2anyreg(fs, e);
rlm@1 469 }
rlm@1 470
rlm@1 471
rlm@1 472 void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
rlm@1 473 switch (var->k) {
rlm@1 474 case VLOCAL: {
rlm@1 475 freeexp(fs, ex);
rlm@1 476 exp2reg(fs, ex, var->u.s.info);
rlm@1 477 return;
rlm@1 478 }
rlm@1 479 case VUPVAL: {
rlm@1 480 int e = luaK_exp2anyreg(fs, ex);
rlm@1 481 luaK_codeABC(fs, OP_SETUPVAL, e, var->u.s.info, 0);
rlm@1 482 break;
rlm@1 483 }
rlm@1 484 case VGLOBAL: {
rlm@1 485 int e = luaK_exp2anyreg(fs, ex);
rlm@1 486 luaK_codeABx(fs, OP_SETGLOBAL, e, var->u.s.info);
rlm@1 487 break;
rlm@1 488 }
rlm@1 489 case VINDEXED: {
rlm@1 490 int e = luaK_exp2RK(fs, ex);
rlm@1 491 luaK_codeABC(fs, OP_SETTABLE, var->u.s.info, var->u.s.aux, e);
rlm@1 492 break;
rlm@1 493 }
rlm@1 494 default: {
rlm@1 495 lua_assert(0); /* invalid var kind to store */
rlm@1 496 break;
rlm@1 497 }
rlm@1 498 }
rlm@1 499 freeexp(fs, ex);
rlm@1 500 }
rlm@1 501
rlm@1 502
rlm@1 503 void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
rlm@1 504 int func;
rlm@1 505 luaK_exp2anyreg(fs, e);
rlm@1 506 freeexp(fs, e);
rlm@1 507 func = fs->freereg;
rlm@1 508 luaK_reserveregs(fs, 2);
rlm@1 509 luaK_codeABC(fs, OP_SELF, func, e->u.s.info, luaK_exp2RK(fs, key));
rlm@1 510 freeexp(fs, key);
rlm@1 511 e->u.s.info = func;
rlm@1 512 e->k = VNONRELOC;
rlm@1 513 }
rlm@1 514
rlm@1 515
rlm@1 516 static void invertjump (FuncState *fs, expdesc *e) {
rlm@1 517 Instruction *pc = getjumpcontrol(fs, e->u.s.info);
rlm@1 518 lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET &&
rlm@1 519 GET_OPCODE(*pc) != OP_TEST);
rlm@1 520 SETARG_A(*pc, !(GETARG_A(*pc)));
rlm@1 521 }
rlm@1 522
rlm@1 523
rlm@1 524 static int jumponcond (FuncState *fs, expdesc *e, int cond) {
rlm@1 525 if (e->k == VRELOCABLE) {
rlm@1 526 Instruction ie = getcode(fs, e);
rlm@1 527 if (GET_OPCODE(ie) == OP_NOT) {
rlm@1 528 fs->pc--; /* remove previous OP_NOT */
rlm@1 529 return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond);
rlm@1 530 }
rlm@1 531 /* else go through */
rlm@1 532 }
rlm@1 533 discharge2anyreg(fs, e);
rlm@1 534 freeexp(fs, e);
rlm@1 535 return condjump(fs, OP_TESTSET, NO_REG, e->u.s.info, cond);
rlm@1 536 }
rlm@1 537
rlm@1 538
rlm@1 539 void luaK_goiftrue (FuncState *fs, expdesc *e) {
rlm@1 540 int pc; /* pc of last jump */
rlm@1 541 luaK_dischargevars(fs, e);
rlm@1 542 switch (e->k) {
rlm@1 543 case VK: case VKNUM: case VTRUE: {
rlm@1 544 pc = NO_JUMP; /* always true; do nothing */
rlm@1 545 break;
rlm@1 546 }
rlm@1 547 case VFALSE: {
rlm@1 548 pc = luaK_jump(fs); /* always jump */
rlm@1 549 break;
rlm@1 550 }
rlm@1 551 case VJMP: {
rlm@1 552 invertjump(fs, e);
rlm@1 553 pc = e->u.s.info;
rlm@1 554 break;
rlm@1 555 }
rlm@1 556 default: {
rlm@1 557 pc = jumponcond(fs, e, 0);
rlm@1 558 break;
rlm@1 559 }
rlm@1 560 }
rlm@1 561 luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */
rlm@1 562 luaK_patchtohere(fs, e->t);
rlm@1 563 e->t = NO_JUMP;
rlm@1 564 }
rlm@1 565
rlm@1 566
rlm@1 567 static void luaK_goiffalse (FuncState *fs, expdesc *e) {
rlm@1 568 int pc; /* pc of last jump */
rlm@1 569 luaK_dischargevars(fs, e);
rlm@1 570 switch (e->k) {
rlm@1 571 case VNIL: case VFALSE: {
rlm@1 572 pc = NO_JUMP; /* always false; do nothing */
rlm@1 573 break;
rlm@1 574 }
rlm@1 575 case VTRUE: {
rlm@1 576 pc = luaK_jump(fs); /* always jump */
rlm@1 577 break;
rlm@1 578 }
rlm@1 579 case VJMP: {
rlm@1 580 pc = e->u.s.info;
rlm@1 581 break;
rlm@1 582 }
rlm@1 583 default: {
rlm@1 584 pc = jumponcond(fs, e, 1);
rlm@1 585 break;
rlm@1 586 }
rlm@1 587 }
rlm@1 588 luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */
rlm@1 589 luaK_patchtohere(fs, e->f);
rlm@1 590 e->f = NO_JUMP;
rlm@1 591 }
rlm@1 592
rlm@1 593
rlm@1 594 static void codenot (FuncState *fs, expdesc *e) {
rlm@1 595 luaK_dischargevars(fs, e);
rlm@1 596 switch (e->k) {
rlm@1 597 case VNIL: case VFALSE: {
rlm@1 598 e->k = VTRUE;
rlm@1 599 break;
rlm@1 600 }
rlm@1 601 case VK: case VKNUM: case VTRUE: {
rlm@1 602 e->k = VFALSE;
rlm@1 603 break;
rlm@1 604 }
rlm@1 605 case VJMP: {
rlm@1 606 invertjump(fs, e);
rlm@1 607 break;
rlm@1 608 }
rlm@1 609 case VRELOCABLE:
rlm@1 610 case VNONRELOC: {
rlm@1 611 discharge2anyreg(fs, e);
rlm@1 612 freeexp(fs, e);
rlm@1 613 e->u.s.info = luaK_codeABC(fs, OP_NOT, 0, e->u.s.info, 0);
rlm@1 614 e->k = VRELOCABLE;
rlm@1 615 break;
rlm@1 616 }
rlm@1 617 default: {
rlm@1 618 lua_assert(0); /* cannot happen */
rlm@1 619 break;
rlm@1 620 }
rlm@1 621 }
rlm@1 622 /* interchange true and false lists */
rlm@1 623 { int temp = e->f; e->f = e->t; e->t = temp; }
rlm@1 624 removevalues(fs, e->f);
rlm@1 625 removevalues(fs, e->t);
rlm@1 626 }
rlm@1 627
rlm@1 628
rlm@1 629 void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
rlm@1 630 t->u.s.aux = luaK_exp2RK(fs, k);
rlm@1 631 t->k = VINDEXED;
rlm@1 632 }
rlm@1 633
rlm@1 634
rlm@1 635 static int constfolding (OpCode op, expdesc *e1, expdesc *e2) {
rlm@1 636 lua_Number v1, v2, r;
rlm@1 637 if (!isnumeral(e1) || !isnumeral(e2)) return 0;
rlm@1 638 v1 = e1->u.nval;
rlm@1 639 v2 = e2->u.nval;
rlm@1 640 switch (op) {
rlm@1 641 case OP_ADD: r = luai_numadd(v1, v2); break;
rlm@1 642 case OP_SUB: r = luai_numsub(v1, v2); break;
rlm@1 643 case OP_MUL: r = luai_nummul(v1, v2); break;
rlm@1 644 case OP_DIV:
rlm@1 645 if (v2 == 0) return 0; /* do not attempt to divide by 0 */
rlm@1 646 r = luai_numdiv(v1, v2); break;
rlm@1 647 case OP_MOD:
rlm@1 648 if (v2 == 0) return 0; /* do not attempt to divide by 0 */
rlm@1 649 r = luai_nummod(v1, v2); break;
rlm@1 650 case OP_POW: r = luai_numpow(v1, v2); break;
rlm@1 651 case OP_UNM: r = luai_numunm(v1); break;
rlm@1 652 case OP_LEN: return 0; /* no constant folding for 'len' */
rlm@1 653 default: lua_assert(0); r = 0; break;
rlm@1 654 }
rlm@1 655 if (luai_numisnan(r)) return 0; /* do not attempt to produce NaN */
rlm@1 656 e1->u.nval = r;
rlm@1 657 return 1;
rlm@1 658 }
rlm@1 659
rlm@1 660
rlm@1 661 static void codearith (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) {
rlm@1 662 if (constfolding(op, e1, e2))
rlm@1 663 return;
rlm@1 664 else {
rlm@1 665 int o2 = (op != OP_UNM && op != OP_LEN) ? luaK_exp2RK(fs, e2) : 0;
rlm@1 666 int o1 = luaK_exp2RK(fs, e1);
rlm@1 667 if (o1 > o2) {
rlm@1 668 freeexp(fs, e1);
rlm@1 669 freeexp(fs, e2);
rlm@1 670 }
rlm@1 671 else {
rlm@1 672 freeexp(fs, e2);
rlm@1 673 freeexp(fs, e1);
rlm@1 674 }
rlm@1 675 e1->u.s.info = luaK_codeABC(fs, op, 0, o1, o2);
rlm@1 676 e1->k = VRELOCABLE;
rlm@1 677 }
rlm@1 678 }
rlm@1 679
rlm@1 680
rlm@1 681 static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1,
rlm@1 682 expdesc *e2) {
rlm@1 683 int o1 = luaK_exp2RK(fs, e1);
rlm@1 684 int o2 = luaK_exp2RK(fs, e2);
rlm@1 685 freeexp(fs, e2);
rlm@1 686 freeexp(fs, e1);
rlm@1 687 if (cond == 0 && op != OP_EQ) {
rlm@1 688 int temp; /* exchange args to replace by `<' or `<=' */
rlm@1 689 temp = o1; o1 = o2; o2 = temp; /* o1 <==> o2 */
rlm@1 690 cond = 1;
rlm@1 691 }
rlm@1 692 e1->u.s.info = condjump(fs, op, cond, o1, o2);
rlm@1 693 e1->k = VJMP;
rlm@1 694 }
rlm@1 695
rlm@1 696
rlm@1 697 void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) {
rlm@1 698 expdesc e2;
rlm@1 699 e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0;
rlm@1 700 switch (op) {
rlm@1 701 case OPR_MINUS: {
rlm@1 702 if (!isnumeral(e))
rlm@1 703 luaK_exp2anyreg(fs, e); /* cannot operate on non-numeric constants */
rlm@1 704 codearith(fs, OP_UNM, e, &e2);
rlm@1 705 break;
rlm@1 706 }
rlm@1 707 case OPR_NOT: codenot(fs, e); break;
rlm@1 708 case OPR_LEN: {
rlm@1 709 luaK_exp2anyreg(fs, e); /* cannot operate on constants */
rlm@1 710 codearith(fs, OP_LEN, e, &e2);
rlm@1 711 break;
rlm@1 712 }
rlm@1 713 default: lua_assert(0);
rlm@1 714 }
rlm@1 715 }
rlm@1 716
rlm@1 717
rlm@1 718 void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
rlm@1 719 switch (op) {
rlm@1 720 case OPR_AND: {
rlm@1 721 luaK_goiftrue(fs, v);
rlm@1 722 break;
rlm@1 723 }
rlm@1 724 case OPR_OR: {
rlm@1 725 luaK_goiffalse(fs, v);
rlm@1 726 break;
rlm@1 727 }
rlm@1 728 case OPR_CONCAT: {
rlm@1 729 luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */
rlm@1 730 break;
rlm@1 731 }
rlm@1 732 case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV:
rlm@1 733 case OPR_MOD: case OPR_POW: {
rlm@1 734 if (!isnumeral(v)) luaK_exp2RK(fs, v);
rlm@1 735 break;
rlm@1 736 }
rlm@1 737 default: {
rlm@1 738 luaK_exp2RK(fs, v);
rlm@1 739 break;
rlm@1 740 }
rlm@1 741 }
rlm@1 742 }
rlm@1 743
rlm@1 744
rlm@1 745 void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) {
rlm@1 746 switch (op) {
rlm@1 747 case OPR_AND: {
rlm@1 748 lua_assert(e1->t == NO_JUMP); /* list must be closed */
rlm@1 749 luaK_dischargevars(fs, e2);
rlm@1 750 luaK_concat(fs, &e2->f, e1->f);
rlm@1 751 *e1 = *e2;
rlm@1 752 break;
rlm@1 753 }
rlm@1 754 case OPR_OR: {
rlm@1 755 lua_assert(e1->f == NO_JUMP); /* list must be closed */
rlm@1 756 luaK_dischargevars(fs, e2);
rlm@1 757 luaK_concat(fs, &e2->t, e1->t);
rlm@1 758 *e1 = *e2;
rlm@1 759 break;
rlm@1 760 }
rlm@1 761 case OPR_CONCAT: {
rlm@1 762 luaK_exp2val(fs, e2);
rlm@1 763 if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) {
rlm@1 764 lua_assert(e1->u.s.info == GETARG_B(getcode(fs, e2))-1);
rlm@1 765 freeexp(fs, e1);
rlm@1 766 SETARG_B(getcode(fs, e2), e1->u.s.info);
rlm@1 767 e1->k = VRELOCABLE; e1->u.s.info = e2->u.s.info;
rlm@1 768 }
rlm@1 769 else {
rlm@1 770 luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */
rlm@1 771 codearith(fs, OP_CONCAT, e1, e2);
rlm@1 772 }
rlm@1 773 break;
rlm@1 774 }
rlm@1 775 case OPR_ADD: codearith(fs, OP_ADD, e1, e2); break;
rlm@1 776 case OPR_SUB: codearith(fs, OP_SUB, e1, e2); break;
rlm@1 777 case OPR_MUL: codearith(fs, OP_MUL, e1, e2); break;
rlm@1 778 case OPR_DIV: codearith(fs, OP_DIV, e1, e2); break;
rlm@1 779 case OPR_MOD: codearith(fs, OP_MOD, e1, e2); break;
rlm@1 780 case OPR_POW: codearith(fs, OP_POW, e1, e2); break;
rlm@1 781 case OPR_EQ: codecomp(fs, OP_EQ, 1, e1, e2); break;
rlm@1 782 case OPR_NE: codecomp(fs, OP_EQ, 0, e1, e2); break;
rlm@1 783 case OPR_LT: codecomp(fs, OP_LT, 1, e1, e2); break;
rlm@1 784 case OPR_LE: codecomp(fs, OP_LE, 1, e1, e2); break;
rlm@1 785 case OPR_GT: codecomp(fs, OP_LT, 0, e1, e2); break;
rlm@1 786 case OPR_GE: codecomp(fs, OP_LE, 0, e1, e2); break;
rlm@1 787 default: lua_assert(0);
rlm@1 788 }
rlm@1 789 }
rlm@1 790
rlm@1 791
rlm@1 792 void luaK_fixline (FuncState *fs, int line) {
rlm@1 793 fs->f->lineinfo[fs->pc - 1] = line;
rlm@1 794 }
rlm@1 795
rlm@1 796
rlm@1 797 static int luaK_code (FuncState *fs, Instruction i, int line) {
rlm@1 798 Proto *f = fs->f;
rlm@1 799 dischargejpc(fs); /* `pc' will change */
rlm@1 800 /* put new instruction in code array */
rlm@1 801 luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction,
rlm@1 802 MAX_INT, "code size overflow");
rlm@1 803 f->code[fs->pc] = i;
rlm@1 804 /* save corresponding line information */
rlm@1 805 luaM_growvector(fs->L, f->lineinfo, fs->pc, f->sizelineinfo, int,
rlm@1 806 MAX_INT, "code size overflow");
rlm@1 807 f->lineinfo[fs->pc] = line;
rlm@1 808 return fs->pc++;
rlm@1 809 }
rlm@1 810
rlm@1 811
rlm@1 812 int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {
rlm@1 813 lua_assert(getOpMode(o) == iABC);
rlm@1 814 lua_assert(getBMode(o) != OpArgN || b == 0);
rlm@1 815 lua_assert(getCMode(o) != OpArgN || c == 0);
rlm@1 816 return luaK_code(fs, CREATE_ABC(o, a, b, c), fs->ls->lastline);
rlm@1 817 }
rlm@1 818
rlm@1 819
rlm@1 820 int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {
rlm@1 821 lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx);
rlm@1 822 lua_assert(getCMode(o) == OpArgN);
rlm@1 823 return luaK_code(fs, CREATE_ABx(o, a, bc), fs->ls->lastline);
rlm@1 824 }
rlm@1 825
rlm@1 826
rlm@1 827 void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {
rlm@1 828 int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1;
rlm@1 829 int b = (tostore == LUA_MULTRET) ? 0 : tostore;
rlm@1 830 lua_assert(tostore != 0);
rlm@1 831 if (c <= MAXARG_C)
rlm@1 832 luaK_codeABC(fs, OP_SETLIST, base, b, c);
rlm@1 833 else {
rlm@1 834 luaK_codeABC(fs, OP_SETLIST, base, b, 0);
rlm@1 835 luaK_code(fs, cast(Instruction, c), fs->ls->lastline);
rlm@1 836 }
rlm@1 837 fs->freereg = base + 1; /* free registers with list values */
rlm@1 838 }
rlm@1 839