rlm@1: /* rlm@1: ** $Id: lcode.c,v 2.25.1.3 2007/12/28 15:32:23 roberto Exp $ rlm@1: ** Code generator for Lua rlm@1: ** See Copyright Notice in lua.h rlm@1: */ rlm@1: rlm@1: rlm@1: #include rlm@1: rlm@1: #define lcode_c rlm@1: #define LUA_CORE rlm@1: rlm@1: #include "lua.h" rlm@1: rlm@1: #include "lcode.h" rlm@1: #include "ldebug.h" rlm@1: #include "ldo.h" rlm@1: #include "lgc.h" rlm@1: #include "llex.h" rlm@1: #include "lmem.h" rlm@1: #include "lobject.h" rlm@1: #include "lopcodes.h" rlm@1: #include "lparser.h" rlm@1: #include "ltable.h" rlm@1: rlm@1: rlm@1: #define hasjumps(e) ((e)->t != (e)->f) rlm@1: rlm@1: rlm@1: static int isnumeral(expdesc *e) { rlm@1: return (e->k == VKNUM && e->t == NO_JUMP && e->f == NO_JUMP); rlm@1: } rlm@1: rlm@1: rlm@1: void luaK_nil (FuncState *fs, int from, int n) { rlm@1: Instruction *previous; rlm@1: if (fs->pc > fs->lasttarget) { /* no jumps to current position? */ rlm@1: if (fs->pc == 0) { /* function start? */ rlm@1: if (from >= fs->nactvar) rlm@1: return; /* positions are already clean */ rlm@1: } rlm@1: else { rlm@1: previous = &fs->f->code[fs->pc-1]; rlm@1: if (GET_OPCODE(*previous) == OP_LOADNIL) { rlm@1: int pfrom = GETARG_A(*previous); rlm@1: int pto = GETARG_B(*previous); rlm@1: if (pfrom <= from && from <= pto+1) { /* can connect both? */ rlm@1: if (from+n-1 > pto) rlm@1: SETARG_B(*previous, from+n-1); rlm@1: return; rlm@1: } rlm@1: } rlm@1: } rlm@1: } rlm@1: luaK_codeABC(fs, OP_LOADNIL, from, from+n-1, 0); /* else no optimization */ rlm@1: } rlm@1: rlm@1: rlm@1: int luaK_jump (FuncState *fs) { rlm@1: int jpc = fs->jpc; /* save list of jumps to here */ rlm@1: int j; rlm@1: fs->jpc = NO_JUMP; rlm@1: j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP); rlm@1: luaK_concat(fs, &j, jpc); /* keep them on hold */ rlm@1: return j; rlm@1: } rlm@1: rlm@1: rlm@1: void luaK_ret (FuncState *fs, int first, int nret) { rlm@1: luaK_codeABC(fs, OP_RETURN, first, nret+1, 0); rlm@1: } rlm@1: rlm@1: rlm@1: static int condjump (FuncState *fs, OpCode op, int A, int B, int C) { rlm@1: luaK_codeABC(fs, op, A, B, C); rlm@1: return luaK_jump(fs); rlm@1: } rlm@1: rlm@1: rlm@1: static void fixjump (FuncState *fs, int pc, int dest) { rlm@1: Instruction *jmp = &fs->f->code[pc]; rlm@1: int offset = dest-(pc+1); rlm@1: lua_assert(dest != NO_JUMP); rlm@1: if (abs(offset) > MAXARG_sBx) rlm@1: luaX_syntaxerror(fs->ls, "control structure too long"); rlm@1: SETARG_sBx(*jmp, offset); rlm@1: } rlm@1: rlm@1: rlm@1: /* rlm@1: ** returns current `pc' and marks it as a jump target (to avoid wrong rlm@1: ** optimizations with consecutive instructions not in the same basic block). rlm@1: */ rlm@1: int luaK_getlabel (FuncState *fs) { rlm@1: fs->lasttarget = fs->pc; rlm@1: return fs->pc; rlm@1: } rlm@1: rlm@1: rlm@1: static int getjump (FuncState *fs, int pc) { rlm@1: int offset = GETARG_sBx(fs->f->code[pc]); rlm@1: if (offset == NO_JUMP) /* point to itself represents end of list */ rlm@1: return NO_JUMP; /* end of list */ rlm@1: else rlm@1: return (pc+1)+offset; /* turn offset into absolute position */ rlm@1: } rlm@1: rlm@1: rlm@1: static Instruction *getjumpcontrol (FuncState *fs, int pc) { rlm@1: Instruction *pi = &fs->f->code[pc]; rlm@1: if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1)))) rlm@1: return pi-1; rlm@1: else rlm@1: return pi; rlm@1: } rlm@1: rlm@1: rlm@1: /* rlm@1: ** check whether list has any jump that do not produce a value rlm@1: ** (or produce an inverted value) rlm@1: */ rlm@1: static int need_value (FuncState *fs, int list) { rlm@1: for (; list != NO_JUMP; list = getjump(fs, list)) { rlm@1: Instruction i = *getjumpcontrol(fs, list); rlm@1: if (GET_OPCODE(i) != OP_TESTSET) return 1; rlm@1: } rlm@1: return 0; /* not found */ rlm@1: } rlm@1: rlm@1: rlm@1: static int patchtestreg (FuncState *fs, int node, int reg) { rlm@1: Instruction *i = getjumpcontrol(fs, node); rlm@1: if (GET_OPCODE(*i) != OP_TESTSET) rlm@1: return 0; /* cannot patch other instructions */ rlm@1: if (reg != NO_REG && reg != GETARG_B(*i)) rlm@1: SETARG_A(*i, reg); rlm@1: else /* no register to put value or register already has the value */ rlm@1: *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i)); rlm@1: rlm@1: return 1; rlm@1: } rlm@1: rlm@1: rlm@1: static void removevalues (FuncState *fs, int list) { rlm@1: for (; list != NO_JUMP; list = getjump(fs, list)) rlm@1: patchtestreg(fs, list, NO_REG); rlm@1: } rlm@1: rlm@1: rlm@1: static void patchlistaux (FuncState *fs, int list, int vtarget, int reg, rlm@1: int dtarget) { rlm@1: while (list != NO_JUMP) { rlm@1: int next = getjump(fs, list); rlm@1: if (patchtestreg(fs, list, reg)) rlm@1: fixjump(fs, list, vtarget); rlm@1: else rlm@1: fixjump(fs, list, dtarget); /* jump to default target */ rlm@1: list = next; rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: static void dischargejpc (FuncState *fs) { rlm@1: patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc); rlm@1: fs->jpc = NO_JUMP; rlm@1: } rlm@1: rlm@1: rlm@1: void luaK_patchlist (FuncState *fs, int list, int target) { rlm@1: if (target == fs->pc) rlm@1: luaK_patchtohere(fs, list); rlm@1: else { rlm@1: lua_assert(target < fs->pc); rlm@1: patchlistaux(fs, list, target, NO_REG, target); rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: void luaK_patchtohere (FuncState *fs, int list) { rlm@1: luaK_getlabel(fs); rlm@1: luaK_concat(fs, &fs->jpc, list); rlm@1: } rlm@1: rlm@1: rlm@1: void luaK_concat (FuncState *fs, int *l1, int l2) { rlm@1: if (l2 == NO_JUMP) return; rlm@1: else if (*l1 == NO_JUMP) rlm@1: *l1 = l2; rlm@1: else { rlm@1: int list = *l1; rlm@1: int next; rlm@1: while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */ rlm@1: list = next; rlm@1: fixjump(fs, list, l2); rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: void luaK_checkstack (FuncState *fs, int n) { rlm@1: int newstack = fs->freereg + n; rlm@1: if (newstack > fs->f->maxstacksize) { rlm@1: if (newstack >= MAXSTACK) rlm@1: luaX_syntaxerror(fs->ls, "function or expression too complex"); rlm@1: fs->f->maxstacksize = cast_byte(newstack); rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: void luaK_reserveregs (FuncState *fs, int n) { rlm@1: luaK_checkstack(fs, n); rlm@1: fs->freereg += n; rlm@1: } rlm@1: rlm@1: rlm@1: static void freereg (FuncState *fs, int reg) { rlm@1: if (!ISK(reg) && reg >= fs->nactvar) { rlm@1: fs->freereg--; rlm@1: lua_assert(reg == fs->freereg); rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: static void freeexp (FuncState *fs, expdesc *e) { rlm@1: if (e->k == VNONRELOC) rlm@1: freereg(fs, e->u.s.info); rlm@1: } rlm@1: rlm@1: rlm@1: static int addk (FuncState *fs, TValue *k, TValue *v) { rlm@1: lua_State *L = fs->L; rlm@1: TValue *idx = luaH_set(L, fs->h, k); rlm@1: Proto *f = fs->f; rlm@1: int oldsize = f->sizek; rlm@1: if (ttisnumber(idx)) { rlm@1: lua_assert(luaO_rawequalObj(&fs->f->k[cast_int(nvalue(idx))], v)); rlm@1: return cast_int(nvalue(idx)); rlm@1: } rlm@1: else { /* constant not found; create a new entry */ rlm@1: setnvalue(idx, cast_num(fs->nk)); rlm@1: luaM_growvector(L, f->k, fs->nk, f->sizek, TValue, rlm@1: MAXARG_Bx, "constant table overflow"); rlm@1: while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]); rlm@1: setobj(L, &f->k[fs->nk], v); rlm@1: luaC_barrier(L, f, v); rlm@1: return fs->nk++; rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: int luaK_stringK (FuncState *fs, TString *s) { rlm@1: TValue o; rlm@1: setsvalue(fs->L, &o, s); rlm@1: return addk(fs, &o, &o); rlm@1: } rlm@1: rlm@1: rlm@1: int luaK_numberK (FuncState *fs, lua_Number r) { rlm@1: TValue o; rlm@1: setnvalue(&o, r); rlm@1: return addk(fs, &o, &o); rlm@1: } rlm@1: rlm@1: rlm@1: static int boolK (FuncState *fs, int b) { rlm@1: TValue o; rlm@1: setbvalue(&o, b); rlm@1: return addk(fs, &o, &o); rlm@1: } rlm@1: rlm@1: rlm@1: static int nilK (FuncState *fs) { rlm@1: TValue k, v; rlm@1: setnilvalue(&v); rlm@1: /* cannot use nil as key; instead use table itself to represent nil */ rlm@1: sethvalue(fs->L, &k, fs->h); rlm@1: return addk(fs, &k, &v); rlm@1: } rlm@1: rlm@1: rlm@1: void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) { rlm@1: if (e->k == VCALL) { /* expression is an open function call? */ rlm@1: SETARG_C(getcode(fs, e), nresults+1); rlm@1: } rlm@1: else if (e->k == VVARARG) { rlm@1: SETARG_B(getcode(fs, e), nresults+1); rlm@1: SETARG_A(getcode(fs, e), fs->freereg); rlm@1: luaK_reserveregs(fs, 1); rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: void luaK_setoneret (FuncState *fs, expdesc *e) { rlm@1: if (e->k == VCALL) { /* expression is an open function call? */ rlm@1: e->k = VNONRELOC; rlm@1: e->u.s.info = GETARG_A(getcode(fs, e)); rlm@1: } rlm@1: else if (e->k == VVARARG) { rlm@1: SETARG_B(getcode(fs, e), 2); rlm@1: e->k = VRELOCABLE; /* can relocate its simple result */ rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: void luaK_dischargevars (FuncState *fs, expdesc *e) { rlm@1: switch (e->k) { rlm@1: case VLOCAL: { rlm@1: e->k = VNONRELOC; rlm@1: break; rlm@1: } rlm@1: case VUPVAL: { rlm@1: e->u.s.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.s.info, 0); rlm@1: e->k = VRELOCABLE; rlm@1: break; rlm@1: } rlm@1: case VGLOBAL: { rlm@1: e->u.s.info = luaK_codeABx(fs, OP_GETGLOBAL, 0, e->u.s.info); rlm@1: e->k = VRELOCABLE; rlm@1: break; rlm@1: } rlm@1: case VINDEXED: { rlm@1: freereg(fs, e->u.s.aux); rlm@1: freereg(fs, e->u.s.info); rlm@1: e->u.s.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.s.info, e->u.s.aux); rlm@1: e->k = VRELOCABLE; rlm@1: break; rlm@1: } rlm@1: case VVARARG: rlm@1: case VCALL: { rlm@1: luaK_setoneret(fs, e); rlm@1: break; rlm@1: } rlm@1: default: break; /* there is one value available (somewhere) */ rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: static int code_label (FuncState *fs, int A, int b, int jump) { rlm@1: luaK_getlabel(fs); /* those instructions may be jump targets */ rlm@1: return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump); rlm@1: } rlm@1: rlm@1: rlm@1: static void discharge2reg (FuncState *fs, expdesc *e, int reg) { rlm@1: luaK_dischargevars(fs, e); rlm@1: switch (e->k) { rlm@1: case VNIL: { rlm@1: luaK_nil(fs, reg, 1); rlm@1: break; rlm@1: } rlm@1: case VFALSE: case VTRUE: { rlm@1: luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0); rlm@1: break; rlm@1: } rlm@1: case VK: { rlm@1: luaK_codeABx(fs, OP_LOADK, reg, e->u.s.info); rlm@1: break; rlm@1: } rlm@1: case VKNUM: { rlm@1: luaK_codeABx(fs, OP_LOADK, reg, luaK_numberK(fs, e->u.nval)); rlm@1: break; rlm@1: } rlm@1: case VRELOCABLE: { rlm@1: Instruction *pc = &getcode(fs, e); rlm@1: SETARG_A(*pc, reg); rlm@1: break; rlm@1: } rlm@1: case VNONRELOC: { rlm@1: if (reg != e->u.s.info) rlm@1: luaK_codeABC(fs, OP_MOVE, reg, e->u.s.info, 0); rlm@1: break; rlm@1: } rlm@1: default: { rlm@1: lua_assert(e->k == VVOID || e->k == VJMP); rlm@1: return; /* nothing to do... */ rlm@1: } rlm@1: } rlm@1: e->u.s.info = reg; rlm@1: e->k = VNONRELOC; rlm@1: } rlm@1: rlm@1: rlm@1: static void discharge2anyreg (FuncState *fs, expdesc *e) { rlm@1: if (e->k != VNONRELOC) { rlm@1: luaK_reserveregs(fs, 1); rlm@1: discharge2reg(fs, e, fs->freereg-1); rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: static void exp2reg (FuncState *fs, expdesc *e, int reg) { rlm@1: discharge2reg(fs, e, reg); rlm@1: if (e->k == VJMP) rlm@1: luaK_concat(fs, &e->t, e->u.s.info); /* put this jump in `t' list */ rlm@1: if (hasjumps(e)) { rlm@1: int final; /* position after whole expression */ rlm@1: int p_f = NO_JUMP; /* position of an eventual LOAD false */ rlm@1: int p_t = NO_JUMP; /* position of an eventual LOAD true */ rlm@1: if (need_value(fs, e->t) || need_value(fs, e->f)) { rlm@1: int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs); rlm@1: p_f = code_label(fs, reg, 0, 1); rlm@1: p_t = code_label(fs, reg, 1, 0); rlm@1: luaK_patchtohere(fs, fj); rlm@1: } rlm@1: final = luaK_getlabel(fs); rlm@1: patchlistaux(fs, e->f, final, reg, p_f); rlm@1: patchlistaux(fs, e->t, final, reg, p_t); rlm@1: } rlm@1: e->f = e->t = NO_JUMP; rlm@1: e->u.s.info = reg; rlm@1: e->k = VNONRELOC; rlm@1: } rlm@1: rlm@1: rlm@1: void luaK_exp2nextreg (FuncState *fs, expdesc *e) { rlm@1: luaK_dischargevars(fs, e); rlm@1: freeexp(fs, e); rlm@1: luaK_reserveregs(fs, 1); rlm@1: exp2reg(fs, e, fs->freereg - 1); rlm@1: } rlm@1: rlm@1: rlm@1: int luaK_exp2anyreg (FuncState *fs, expdesc *e) { rlm@1: luaK_dischargevars(fs, e); rlm@1: if (e->k == VNONRELOC) { rlm@1: if (!hasjumps(e)) return e->u.s.info; /* exp is already in a register */ rlm@1: if (e->u.s.info >= fs->nactvar) { /* reg. is not a local? */ rlm@1: exp2reg(fs, e, e->u.s.info); /* put value on it */ rlm@1: return e->u.s.info; rlm@1: } rlm@1: } rlm@1: luaK_exp2nextreg(fs, e); /* default */ rlm@1: return e->u.s.info; rlm@1: } rlm@1: rlm@1: rlm@1: void luaK_exp2val (FuncState *fs, expdesc *e) { rlm@1: if (hasjumps(e)) rlm@1: luaK_exp2anyreg(fs, e); rlm@1: else rlm@1: luaK_dischargevars(fs, e); rlm@1: } rlm@1: rlm@1: rlm@1: int luaK_exp2RK (FuncState *fs, expdesc *e) { rlm@1: luaK_exp2val(fs, e); rlm@1: switch (e->k) { rlm@1: case VKNUM: rlm@1: case VTRUE: rlm@1: case VFALSE: rlm@1: case VNIL: { rlm@1: if (fs->nk <= MAXINDEXRK) { /* constant fit in RK operand? */ rlm@1: e->u.s.info = (e->k == VNIL) ? nilK(fs) : rlm@1: (e->k == VKNUM) ? luaK_numberK(fs, e->u.nval) : rlm@1: boolK(fs, (e->k == VTRUE)); rlm@1: e->k = VK; rlm@1: return RKASK(e->u.s.info); rlm@1: } rlm@1: else break; rlm@1: } rlm@1: case VK: { rlm@1: if (e->u.s.info <= MAXINDEXRK) /* constant fit in argC? */ rlm@1: return RKASK(e->u.s.info); rlm@1: else break; rlm@1: } rlm@1: default: break; rlm@1: } rlm@1: /* not a constant in the right range: put it in a register */ rlm@1: return luaK_exp2anyreg(fs, e); rlm@1: } rlm@1: rlm@1: rlm@1: void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { rlm@1: switch (var->k) { rlm@1: case VLOCAL: { rlm@1: freeexp(fs, ex); rlm@1: exp2reg(fs, ex, var->u.s.info); rlm@1: return; rlm@1: } rlm@1: case VUPVAL: { rlm@1: int e = luaK_exp2anyreg(fs, ex); rlm@1: luaK_codeABC(fs, OP_SETUPVAL, e, var->u.s.info, 0); rlm@1: break; rlm@1: } rlm@1: case VGLOBAL: { rlm@1: int e = luaK_exp2anyreg(fs, ex); rlm@1: luaK_codeABx(fs, OP_SETGLOBAL, e, var->u.s.info); rlm@1: break; rlm@1: } rlm@1: case VINDEXED: { rlm@1: int e = luaK_exp2RK(fs, ex); rlm@1: luaK_codeABC(fs, OP_SETTABLE, var->u.s.info, var->u.s.aux, e); rlm@1: break; rlm@1: } rlm@1: default: { rlm@1: lua_assert(0); /* invalid var kind to store */ rlm@1: break; rlm@1: } rlm@1: } rlm@1: freeexp(fs, ex); rlm@1: } rlm@1: rlm@1: rlm@1: void luaK_self (FuncState *fs, expdesc *e, expdesc *key) { rlm@1: int func; rlm@1: luaK_exp2anyreg(fs, e); rlm@1: freeexp(fs, e); rlm@1: func = fs->freereg; rlm@1: luaK_reserveregs(fs, 2); rlm@1: luaK_codeABC(fs, OP_SELF, func, e->u.s.info, luaK_exp2RK(fs, key)); rlm@1: freeexp(fs, key); rlm@1: e->u.s.info = func; rlm@1: e->k = VNONRELOC; rlm@1: } rlm@1: rlm@1: rlm@1: static void invertjump (FuncState *fs, expdesc *e) { rlm@1: Instruction *pc = getjumpcontrol(fs, e->u.s.info); rlm@1: lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET && rlm@1: GET_OPCODE(*pc) != OP_TEST); rlm@1: SETARG_A(*pc, !(GETARG_A(*pc))); rlm@1: } rlm@1: rlm@1: rlm@1: static int jumponcond (FuncState *fs, expdesc *e, int cond) { rlm@1: if (e->k == VRELOCABLE) { rlm@1: Instruction ie = getcode(fs, e); rlm@1: if (GET_OPCODE(ie) == OP_NOT) { rlm@1: fs->pc--; /* remove previous OP_NOT */ rlm@1: return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond); rlm@1: } rlm@1: /* else go through */ rlm@1: } rlm@1: discharge2anyreg(fs, e); rlm@1: freeexp(fs, e); rlm@1: return condjump(fs, OP_TESTSET, NO_REG, e->u.s.info, cond); rlm@1: } rlm@1: rlm@1: rlm@1: void luaK_goiftrue (FuncState *fs, expdesc *e) { rlm@1: int pc; /* pc of last jump */ rlm@1: luaK_dischargevars(fs, e); rlm@1: switch (e->k) { rlm@1: case VK: case VKNUM: case VTRUE: { rlm@1: pc = NO_JUMP; /* always true; do nothing */ rlm@1: break; rlm@1: } rlm@1: case VFALSE: { rlm@1: pc = luaK_jump(fs); /* always jump */ rlm@1: break; rlm@1: } rlm@1: case VJMP: { rlm@1: invertjump(fs, e); rlm@1: pc = e->u.s.info; rlm@1: break; rlm@1: } rlm@1: default: { rlm@1: pc = jumponcond(fs, e, 0); rlm@1: break; rlm@1: } rlm@1: } rlm@1: luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */ rlm@1: luaK_patchtohere(fs, e->t); rlm@1: e->t = NO_JUMP; rlm@1: } rlm@1: rlm@1: rlm@1: static void luaK_goiffalse (FuncState *fs, expdesc *e) { rlm@1: int pc; /* pc of last jump */ rlm@1: luaK_dischargevars(fs, e); rlm@1: switch (e->k) { rlm@1: case VNIL: case VFALSE: { rlm@1: pc = NO_JUMP; /* always false; do nothing */ rlm@1: break; rlm@1: } rlm@1: case VTRUE: { rlm@1: pc = luaK_jump(fs); /* always jump */ rlm@1: break; rlm@1: } rlm@1: case VJMP: { rlm@1: pc = e->u.s.info; rlm@1: break; rlm@1: } rlm@1: default: { rlm@1: pc = jumponcond(fs, e, 1); rlm@1: break; rlm@1: } rlm@1: } rlm@1: luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */ rlm@1: luaK_patchtohere(fs, e->f); rlm@1: e->f = NO_JUMP; rlm@1: } rlm@1: rlm@1: rlm@1: static void codenot (FuncState *fs, expdesc *e) { rlm@1: luaK_dischargevars(fs, e); rlm@1: switch (e->k) { rlm@1: case VNIL: case VFALSE: { rlm@1: e->k = VTRUE; rlm@1: break; rlm@1: } rlm@1: case VK: case VKNUM: case VTRUE: { rlm@1: e->k = VFALSE; rlm@1: break; rlm@1: } rlm@1: case VJMP: { rlm@1: invertjump(fs, e); rlm@1: break; rlm@1: } rlm@1: case VRELOCABLE: rlm@1: case VNONRELOC: { rlm@1: discharge2anyreg(fs, e); rlm@1: freeexp(fs, e); rlm@1: e->u.s.info = luaK_codeABC(fs, OP_NOT, 0, e->u.s.info, 0); rlm@1: e->k = VRELOCABLE; rlm@1: break; rlm@1: } rlm@1: default: { rlm@1: lua_assert(0); /* cannot happen */ rlm@1: break; rlm@1: } rlm@1: } rlm@1: /* interchange true and false lists */ rlm@1: { int temp = e->f; e->f = e->t; e->t = temp; } rlm@1: removevalues(fs, e->f); rlm@1: removevalues(fs, e->t); rlm@1: } rlm@1: rlm@1: rlm@1: void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { rlm@1: t->u.s.aux = luaK_exp2RK(fs, k); rlm@1: t->k = VINDEXED; rlm@1: } rlm@1: rlm@1: rlm@1: static int constfolding (OpCode op, expdesc *e1, expdesc *e2) { rlm@1: lua_Number v1, v2, r; rlm@1: if (!isnumeral(e1) || !isnumeral(e2)) return 0; rlm@1: v1 = e1->u.nval; rlm@1: v2 = e2->u.nval; rlm@1: switch (op) { rlm@1: case OP_ADD: r = luai_numadd(v1, v2); break; rlm@1: case OP_SUB: r = luai_numsub(v1, v2); break; rlm@1: case OP_MUL: r = luai_nummul(v1, v2); break; rlm@1: case OP_DIV: rlm@1: if (v2 == 0) return 0; /* do not attempt to divide by 0 */ rlm@1: r = luai_numdiv(v1, v2); break; rlm@1: case OP_MOD: rlm@1: if (v2 == 0) return 0; /* do not attempt to divide by 0 */ rlm@1: r = luai_nummod(v1, v2); break; rlm@1: case OP_POW: r = luai_numpow(v1, v2); break; rlm@1: case OP_UNM: r = luai_numunm(v1); break; rlm@1: case OP_LEN: return 0; /* no constant folding for 'len' */ rlm@1: default: lua_assert(0); r = 0; break; rlm@1: } rlm@1: if (luai_numisnan(r)) return 0; /* do not attempt to produce NaN */ rlm@1: e1->u.nval = r; rlm@1: return 1; rlm@1: } rlm@1: rlm@1: rlm@1: static void codearith (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) { rlm@1: if (constfolding(op, e1, e2)) rlm@1: return; rlm@1: else { rlm@1: int o2 = (op != OP_UNM && op != OP_LEN) ? luaK_exp2RK(fs, e2) : 0; rlm@1: int o1 = luaK_exp2RK(fs, e1); rlm@1: if (o1 > o2) { rlm@1: freeexp(fs, e1); rlm@1: freeexp(fs, e2); rlm@1: } rlm@1: else { rlm@1: freeexp(fs, e2); rlm@1: freeexp(fs, e1); rlm@1: } rlm@1: e1->u.s.info = luaK_codeABC(fs, op, 0, o1, o2); rlm@1: e1->k = VRELOCABLE; rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1, rlm@1: expdesc *e2) { rlm@1: int o1 = luaK_exp2RK(fs, e1); rlm@1: int o2 = luaK_exp2RK(fs, e2); rlm@1: freeexp(fs, e2); rlm@1: freeexp(fs, e1); rlm@1: if (cond == 0 && op != OP_EQ) { rlm@1: int temp; /* exchange args to replace by `<' or `<=' */ rlm@1: temp = o1; o1 = o2; o2 = temp; /* o1 <==> o2 */ rlm@1: cond = 1; rlm@1: } rlm@1: e1->u.s.info = condjump(fs, op, cond, o1, o2); rlm@1: e1->k = VJMP; rlm@1: } rlm@1: rlm@1: rlm@1: void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) { rlm@1: expdesc e2; rlm@1: e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0; rlm@1: switch (op) { rlm@1: case OPR_MINUS: { rlm@1: if (!isnumeral(e)) rlm@1: luaK_exp2anyreg(fs, e); /* cannot operate on non-numeric constants */ rlm@1: codearith(fs, OP_UNM, e, &e2); rlm@1: break; rlm@1: } rlm@1: case OPR_NOT: codenot(fs, e); break; rlm@1: case OPR_LEN: { rlm@1: luaK_exp2anyreg(fs, e); /* cannot operate on constants */ rlm@1: codearith(fs, OP_LEN, e, &e2); rlm@1: break; rlm@1: } rlm@1: default: lua_assert(0); rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { rlm@1: switch (op) { rlm@1: case OPR_AND: { rlm@1: luaK_goiftrue(fs, v); rlm@1: break; rlm@1: } rlm@1: case OPR_OR: { rlm@1: luaK_goiffalse(fs, v); rlm@1: break; rlm@1: } rlm@1: case OPR_CONCAT: { rlm@1: luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */ rlm@1: break; rlm@1: } rlm@1: case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV: rlm@1: case OPR_MOD: case OPR_POW: { rlm@1: if (!isnumeral(v)) luaK_exp2RK(fs, v); rlm@1: break; rlm@1: } rlm@1: default: { rlm@1: luaK_exp2RK(fs, v); rlm@1: break; rlm@1: } rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) { rlm@1: switch (op) { rlm@1: case OPR_AND: { rlm@1: lua_assert(e1->t == NO_JUMP); /* list must be closed */ rlm@1: luaK_dischargevars(fs, e2); rlm@1: luaK_concat(fs, &e2->f, e1->f); rlm@1: *e1 = *e2; rlm@1: break; rlm@1: } rlm@1: case OPR_OR: { rlm@1: lua_assert(e1->f == NO_JUMP); /* list must be closed */ rlm@1: luaK_dischargevars(fs, e2); rlm@1: luaK_concat(fs, &e2->t, e1->t); rlm@1: *e1 = *e2; rlm@1: break; rlm@1: } rlm@1: case OPR_CONCAT: { rlm@1: luaK_exp2val(fs, e2); rlm@1: if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) { rlm@1: lua_assert(e1->u.s.info == GETARG_B(getcode(fs, e2))-1); rlm@1: freeexp(fs, e1); rlm@1: SETARG_B(getcode(fs, e2), e1->u.s.info); rlm@1: e1->k = VRELOCABLE; e1->u.s.info = e2->u.s.info; rlm@1: } rlm@1: else { rlm@1: luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */ rlm@1: codearith(fs, OP_CONCAT, e1, e2); rlm@1: } rlm@1: break; rlm@1: } rlm@1: case OPR_ADD: codearith(fs, OP_ADD, e1, e2); break; rlm@1: case OPR_SUB: codearith(fs, OP_SUB, e1, e2); break; rlm@1: case OPR_MUL: codearith(fs, OP_MUL, e1, e2); break; rlm@1: case OPR_DIV: codearith(fs, OP_DIV, e1, e2); break; rlm@1: case OPR_MOD: codearith(fs, OP_MOD, e1, e2); break; rlm@1: case OPR_POW: codearith(fs, OP_POW, e1, e2); break; rlm@1: case OPR_EQ: codecomp(fs, OP_EQ, 1, e1, e2); break; rlm@1: case OPR_NE: codecomp(fs, OP_EQ, 0, e1, e2); break; rlm@1: case OPR_LT: codecomp(fs, OP_LT, 1, e1, e2); break; rlm@1: case OPR_LE: codecomp(fs, OP_LE, 1, e1, e2); break; rlm@1: case OPR_GT: codecomp(fs, OP_LT, 0, e1, e2); break; rlm@1: case OPR_GE: codecomp(fs, OP_LE, 0, e1, e2); break; rlm@1: default: lua_assert(0); rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: void luaK_fixline (FuncState *fs, int line) { rlm@1: fs->f->lineinfo[fs->pc - 1] = line; rlm@1: } rlm@1: rlm@1: rlm@1: static int luaK_code (FuncState *fs, Instruction i, int line) { rlm@1: Proto *f = fs->f; rlm@1: dischargejpc(fs); /* `pc' will change */ rlm@1: /* put new instruction in code array */ rlm@1: luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction, rlm@1: MAX_INT, "code size overflow"); rlm@1: f->code[fs->pc] = i; rlm@1: /* save corresponding line information */ rlm@1: luaM_growvector(fs->L, f->lineinfo, fs->pc, f->sizelineinfo, int, rlm@1: MAX_INT, "code size overflow"); rlm@1: f->lineinfo[fs->pc] = line; rlm@1: return fs->pc++; rlm@1: } rlm@1: rlm@1: rlm@1: int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) { rlm@1: lua_assert(getOpMode(o) == iABC); rlm@1: lua_assert(getBMode(o) != OpArgN || b == 0); rlm@1: lua_assert(getCMode(o) != OpArgN || c == 0); rlm@1: return luaK_code(fs, CREATE_ABC(o, a, b, c), fs->ls->lastline); rlm@1: } rlm@1: rlm@1: rlm@1: int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) { rlm@1: lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx); rlm@1: lua_assert(getCMode(o) == OpArgN); rlm@1: return luaK_code(fs, CREATE_ABx(o, a, bc), fs->ls->lastline); rlm@1: } rlm@1: rlm@1: rlm@1: void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) { rlm@1: int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1; rlm@1: int b = (tostore == LUA_MULTRET) ? 0 : tostore; rlm@1: lua_assert(tostore != 0); rlm@1: if (c <= MAXARG_C) rlm@1: luaK_codeABC(fs, OP_SETLIST, base, b, c); rlm@1: else { rlm@1: luaK_codeABC(fs, OP_SETLIST, base, b, 0); rlm@1: luaK_code(fs, cast(Instruction, c), fs->ls->lastline); rlm@1: } rlm@1: fs->freereg = base + 1; /* free registers with list values */ rlm@1: } rlm@1: