2000-02-22 21:31:19 +08:00
|
|
|
/*
|
2002-03-09 03:10:32 +08:00
|
|
|
** $Id: lcode.c,v 1.90 2002/03/05 12:42:47 roberto Exp roberto $
|
2000-02-22 21:31:19 +08:00
|
|
|
** Code generator for Lua
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2001-02-24 04:31:37 +08:00
|
|
|
#include <stdlib.h>
|
2000-04-05 04:48:44 +08:00
|
|
|
|
2000-06-12 21:52:05 +08:00
|
|
|
#include "lua.h"
|
|
|
|
|
2000-02-22 21:31:19 +08:00
|
|
|
#include "lcode.h"
|
2001-06-06 02:17:01 +08:00
|
|
|
#include "ldebug.h"
|
2000-03-03 20:33:59 +08:00
|
|
|
#include "ldo.h"
|
2000-02-22 21:31:19 +08:00
|
|
|
#include "llex.h"
|
|
|
|
#include "lmem.h"
|
|
|
|
#include "lobject.h"
|
|
|
|
#include "lopcodes.h"
|
|
|
|
#include "lparser.h"
|
2001-08-27 23:16:28 +08:00
|
|
|
#include "ltable.h"
|
2000-03-03 20:33:59 +08:00
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
#define hasjumps(e) ((e)->t != (e)->f)
|
|
|
|
|
2002-01-26 05:50:39 +08:00
|
|
|
#define getcode(fs,e) ((fs)->f->code[(e)->info])
|
2001-06-06 02:17:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2001-11-29 04:13:13 +08:00
|
|
|
void luaK_error (LexState *ls, const char *msg) {
|
2000-05-25 02:04:17 +08:00
|
|
|
luaX_error(ls, msg, ls->t.token);
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
2000-02-22 21:31:19 +08:00
|
|
|
|
2000-04-08 03:35:20 +08:00
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
void luaK_nil (FuncState *fs, int from, int n) {
|
2002-01-19 01:39:40 +08:00
|
|
|
Instruction *previous;
|
|
|
|
if (fs->pc > fs->lasttarget && /* no jumps to current position? */
|
|
|
|
GET_OPCODE(*(previous = &fs->f->code[fs->pc-1])) == OP_LOADNIL) {
|
|
|
|
int pfrom = GETARG_A(*previous);
|
|
|
|
int pto = GETARG_B(*previous);
|
2001-06-06 02:17:01 +08:00
|
|
|
if (pfrom <= from && from <= pto+1) { /* can connect both? */
|
|
|
|
if (from+n-1 > pto)
|
2002-01-19 01:39:40 +08:00
|
|
|
SETARG_B(*previous, from+n-1);
|
2001-06-06 02:17:01 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
luaK_codeABC(fs, OP_LOADNIL, from, from+n-1, 0); /* else no optimization */
|
2000-04-06 01:51:58 +08:00
|
|
|
}
|
|
|
|
|
2000-02-22 21:31:19 +08:00
|
|
|
|
2000-04-06 01:51:58 +08:00
|
|
|
int luaK_jump (FuncState *fs) {
|
2001-06-06 02:17:01 +08:00
|
|
|
int j = luaK_codeAsBc(fs, OP_JMP, 0, NO_JUMP);
|
2000-04-07 01:36:52 +08:00
|
|
|
if (j == fs->lasttarget) { /* possible jumps to this jump? */
|
|
|
|
luaK_concat(fs, &j, fs->jlt); /* keep them on hold */
|
|
|
|
fs->jlt = NO_JUMP;
|
|
|
|
}
|
|
|
|
return j;
|
2000-04-06 01:51:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-11 22:56:42 +08:00
|
|
|
static int luaK_condjump (FuncState *fs, OpCode op, int A, int B, int C) {
|
|
|
|
luaK_codeABC(fs, op, A, B, C);
|
2002-01-17 06:03:57 +08:00
|
|
|
return luaK_codeAsBc(fs, OP_JMP, 0, NO_JUMP);
|
2001-06-06 02:17:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-04-06 01:51:58 +08:00
|
|
|
static void luaK_fixjump (FuncState *fs, int pc, int dest) {
|
2000-02-22 21:31:19 +08:00
|
|
|
Instruction *jmp = &fs->f->code[pc];
|
2000-03-17 02:03:09 +08:00
|
|
|
if (dest == NO_JUMP)
|
2001-06-06 02:17:01 +08:00
|
|
|
SETARG_sBc(*jmp, NO_JUMP); /* point to itself to represent end of list */
|
2000-03-17 02:03:09 +08:00
|
|
|
else { /* jump is relative to position following jump instruction */
|
|
|
|
int offset = dest-(pc+1);
|
2001-06-06 02:17:01 +08:00
|
|
|
if (abs(offset) > MAXARG_sBc)
|
2001-11-29 04:13:13 +08:00
|
|
|
luaK_error(fs->ls, "control structure too long");
|
2001-06-06 02:17:01 +08:00
|
|
|
SETARG_sBc(*jmp, offset);
|
2000-03-17 02:03:09 +08:00
|
|
|
}
|
2000-03-14 04:37:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-05 04:18:15 +08:00
|
|
|
/*
|
|
|
|
** returns current `pc' and marks it as a jump target (to avoid wrong
|
|
|
|
** optimizations with consecutive instructions not in the same basic block).
|
2000-06-05 22:56:18 +08:00
|
|
|
** discharge list of jumps to last target.
|
2000-03-05 04:18:15 +08:00
|
|
|
*/
|
2000-03-14 04:37:16 +08:00
|
|
|
int luaK_getlabel (FuncState *fs) {
|
2000-04-07 01:36:52 +08:00
|
|
|
if (fs->pc != fs->lasttarget) {
|
|
|
|
int lasttarget = fs->lasttarget;
|
|
|
|
fs->lasttarget = fs->pc;
|
|
|
|
luaK_patchlist(fs, fs->jlt, lasttarget); /* discharge old list `jlt' */
|
2000-06-05 22:56:18 +08:00
|
|
|
fs->jlt = NO_JUMP; /* nobody jumps to this new label (yet) */
|
2000-04-07 01:36:52 +08:00
|
|
|
}
|
2000-03-05 04:18:15 +08:00
|
|
|
return fs->pc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
static int luaK_getjump (FuncState *fs, int pc) {
|
|
|
|
int offset = GETARG_sBc(fs->f->code[pc]);
|
|
|
|
if (offset == NO_JUMP) /* point to itself represents end of list */
|
|
|
|
return NO_JUMP; /* end of list */
|
|
|
|
else
|
|
|
|
return (pc+1)+offset; /* turn offset into absolute position */
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
static Instruction *getjumpcontrol (FuncState *fs, int pc) {
|
|
|
|
Instruction *pi = &fs->f->code[pc];
|
2002-01-17 06:03:57 +08:00
|
|
|
if (pc >= 1 && testOpMode(GET_OPCODE(*(pi-1)), OpModeT))
|
2001-06-06 02:17:01 +08:00
|
|
|
return pi-1;
|
2002-01-17 06:03:57 +08:00
|
|
|
else
|
2001-06-06 02:17:01 +08:00
|
|
|
return pi;
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
static int need_value (FuncState *fs, int list, OpCode op) {
|
|
|
|
/* check whether list has any jump different from `op' */
|
|
|
|
for (; list != NO_JUMP; list = luaK_getjump(fs, list))
|
|
|
|
if (GET_OPCODE(*getjumpcontrol(fs, list)) != op) return 1;
|
|
|
|
return 0; /* not found */
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-08 20:29:27 +08:00
|
|
|
static void patchtestreg (Instruction *i, int reg) {
|
|
|
|
if (reg == NO_REG) reg = GETARG_B(*i);
|
|
|
|
SETARG_A(*i, reg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
static void luaK_patchlistaux (FuncState *fs, int list,
|
|
|
|
int ttarget, int treg, int ftarget, int freg, int dtarget) {
|
|
|
|
while (list != NO_JUMP) {
|
|
|
|
int next = luaK_getjump(fs, list);
|
|
|
|
Instruction *i = getjumpcontrol(fs, list);
|
|
|
|
switch (GET_OPCODE(*i)) {
|
|
|
|
case OP_TESTT: {
|
2001-06-08 20:29:27 +08:00
|
|
|
patchtestreg(i, treg);
|
2001-06-06 02:17:01 +08:00
|
|
|
luaK_fixjump(fs, list, ttarget);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OP_TESTF: {
|
2001-06-08 20:29:27 +08:00
|
|
|
patchtestreg(i, freg);
|
2001-06-06 02:17:01 +08:00
|
|
|
luaK_fixjump(fs, list, ftarget);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
luaK_fixjump(fs, list, dtarget); /* jump to default target */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
list = next;
|
|
|
|
}
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
void luaK_patchlist (FuncState *fs, int list, int target) {
|
|
|
|
if (target == fs->lasttarget) /* same target that list `jlt'? */
|
|
|
|
luaK_concat(fs, &fs->jlt, list); /* delay fixing */
|
2000-06-05 22:56:18 +08:00
|
|
|
else
|
2001-06-06 02:17:01 +08:00
|
|
|
luaK_patchlistaux(fs, list, target, NO_REG, target, NO_REG, target);
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-03-09 03:10:32 +08:00
|
|
|
void luaK_patchtohere (FuncState *fs, int list) {
|
|
|
|
luaK_patchlist(fs, list, luaK_getlabel(fs));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
void luaK_concat (FuncState *fs, int *l1, int l2) {
|
|
|
|
if (*l1 == NO_JUMP)
|
|
|
|
*l1 = l2;
|
|
|
|
else {
|
|
|
|
int list = *l1;
|
|
|
|
int next;
|
|
|
|
while ((next = luaK_getjump(fs, list)) != NO_JUMP) /* find last element */
|
|
|
|
list = next;
|
|
|
|
luaK_fixjump(fs, list, l2);
|
|
|
|
}
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-03-09 03:10:32 +08:00
|
|
|
void luaK_checkstack (FuncState *fs, int n) {
|
|
|
|
int newstack = fs->freereg + n;
|
|
|
|
if (newstack > fs->f->maxstacksize) {
|
|
|
|
if (newstack >= MAXSTACK)
|
2001-11-29 04:13:13 +08:00
|
|
|
luaK_error(fs->ls, "function or expression too complex");
|
2002-03-09 03:10:32 +08:00
|
|
|
fs->f->maxstacksize = cast(lu_byte, newstack);
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-03-09 03:10:32 +08:00
|
|
|
void luaK_reserveregs (FuncState *fs, int n) {
|
|
|
|
luaK_checkstack(fs, n);
|
|
|
|
fs->freereg += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
static void freereg (FuncState *fs, int reg) {
|
|
|
|
if (reg >= fs->nactloc && reg < MAXSTACK) {
|
|
|
|
fs->freereg--;
|
|
|
|
lua_assert(reg == fs->freereg);
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
2000-03-05 04:18:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
static void freeexp (FuncState *fs, expdesc *e) {
|
|
|
|
if (e->k == VNONRELOC)
|
2002-01-26 05:50:39 +08:00
|
|
|
freereg(fs, e->info);
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
2000-04-08 03:35:20 +08:00
|
|
|
|
2000-03-03 20:33:59 +08:00
|
|
|
|
2001-12-12 06:48:44 +08:00
|
|
|
static int addk (FuncState *fs, TObject *k, TObject *v) {
|
2001-08-27 23:16:28 +08:00
|
|
|
const TObject *index = luaH_get(fs->h, k);
|
|
|
|
if (ttype(index) == LUA_TNUMBER) {
|
2001-12-12 06:48:44 +08:00
|
|
|
lua_assert(luaO_equalObj(&fs->f->k[cast(int, nvalue(index))], v));
|
2001-09-01 03:46:07 +08:00
|
|
|
return cast(int, nvalue(index));
|
2001-08-27 23:16:28 +08:00
|
|
|
}
|
|
|
|
else { /* constant not found; create a new entry */
|
|
|
|
TObject o;
|
|
|
|
Proto *f = fs->f;
|
|
|
|
luaM_growvector(fs->L, f->k, fs->nk, f->sizek, TObject,
|
2001-11-29 04:13:13 +08:00
|
|
|
MAXARG_Bc, "constant table overflow");
|
2001-12-12 06:48:44 +08:00
|
|
|
setobj(&f->k[fs->nk], v);
|
2001-08-27 23:16:28 +08:00
|
|
|
setnvalue(&o, fs->nk);
|
2001-08-31 04:56:43 +08:00
|
|
|
luaH_set(fs->L, fs->h, k, &o);
|
2001-08-27 23:16:28 +08:00
|
|
|
return fs->nk++;
|
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-01-26 05:50:39 +08:00
|
|
|
int luaK_stringK (FuncState *fs, TString *s) {
|
2001-08-27 23:16:28 +08:00
|
|
|
TObject o;
|
|
|
|
setsvalue(&o, s);
|
2001-12-12 06:48:44 +08:00
|
|
|
return addk(fs, &o, &o);
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-01-26 05:50:39 +08:00
|
|
|
int luaK_numberK (FuncState *fs, lua_Number r) {
|
2001-06-06 02:17:01 +08:00
|
|
|
TObject o;
|
|
|
|
setnvalue(&o, r);
|
2001-12-12 06:48:44 +08:00
|
|
|
return addk(fs, &o, &o);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int nil_constant (FuncState *fs) {
|
|
|
|
TObject k, v;
|
|
|
|
setnilvalue(&v);
|
|
|
|
sethvalue(&k, fs->h); /* cannot use nil as key; instead use table itself */
|
|
|
|
return addk(fs, &k, &v);
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
void luaK_setcallreturns (FuncState *fs, expdesc *e, int nresults) {
|
|
|
|
if (e->k == VCALL) { /* expression is an open function call? */
|
2002-01-10 06:02:47 +08:00
|
|
|
SETARG_C(getcode(fs, e), nresults+1);
|
2001-06-06 02:17:01 +08:00
|
|
|
if (nresults == 1) { /* `regular' expression? */
|
|
|
|
e->k = VNONRELOC;
|
2002-01-26 05:50:39 +08:00
|
|
|
e->info = GETARG_A(getcode(fs, e));
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-12 22:36:48 +08:00
|
|
|
void luaK_dischargevars (FuncState *fs, expdesc *e) {
|
2001-06-06 02:17:01 +08:00
|
|
|
switch (e->k) {
|
|
|
|
case VLOCAL: {
|
|
|
|
e->k = VNONRELOC;
|
|
|
|
break;
|
|
|
|
}
|
2001-09-08 01:39:10 +08:00
|
|
|
case VUPVAL: {
|
2002-01-26 05:50:39 +08:00
|
|
|
e->info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->info, 0);
|
2001-09-08 01:39:10 +08:00
|
|
|
e->k = VRELOCABLE;
|
|
|
|
break;
|
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
case VGLOBAL: {
|
2002-01-26 05:50:39 +08:00
|
|
|
e->info = luaK_codeABc(fs, OP_GETGLOBAL, 0, e->info);
|
2001-06-06 02:17:01 +08:00
|
|
|
e->k = VRELOCABLE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case VINDEXED: {
|
2002-01-26 05:50:39 +08:00
|
|
|
freereg(fs, e->aux);
|
|
|
|
freereg(fs, e->info);
|
|
|
|
e->info = luaK_codeABC(fs, OP_GETTABLE, 0, e->info, e->aux);
|
2001-06-06 02:17:01 +08:00
|
|
|
e->k = VRELOCABLE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case VCALL: {
|
|
|
|
luaK_setcallreturns(fs, e, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: break; /* there is one value available (somewhere) */
|
|
|
|
}
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-12 06:48:44 +08:00
|
|
|
static int code_label (FuncState *fs, int A, int b, int jump) {
|
2001-06-06 02:17:01 +08:00
|
|
|
luaK_getlabel(fs); /* those instructions may be jump targets */
|
2001-12-12 06:48:44 +08:00
|
|
|
return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
static void dischargejumps (FuncState *fs, expdesc *e, int reg) {
|
2002-01-17 06:03:57 +08:00
|
|
|
if (e->k == VJMP || hasjumps(e)) {
|
2001-06-06 02:17:01 +08:00
|
|
|
int final; /* position after whole expression */
|
2001-12-12 06:48:44 +08:00
|
|
|
int p_f = NO_JUMP; /* position of an eventual PUSH false */
|
|
|
|
int p_t = NO_JUMP; /* position of an eventual PUSH true */
|
2002-01-17 06:03:57 +08:00
|
|
|
if (e->k == VJMP || need_value(fs, e->f, OP_TESTF) ||
|
|
|
|
need_value(fs, e->t, OP_TESTT)) {
|
2001-06-06 02:17:01 +08:00
|
|
|
/* expression needs values */
|
2001-12-12 06:48:44 +08:00
|
|
|
if (e->k != VJMP) {
|
|
|
|
luaK_getlabel(fs); /* these instruction may be jump target */
|
|
|
|
luaK_codeAsBc(fs, OP_JMP, 0, 2); /* to jump over both pushes */
|
|
|
|
}
|
2002-01-17 06:03:57 +08:00
|
|
|
else { /* last expression is a conditional (test + jump) */
|
|
|
|
fs->pc--; /* remove its jump */
|
|
|
|
lua_assert(testOpMode(GET_OPCODE(fs->f->code[fs->pc - 1]), OpModeT));
|
|
|
|
}
|
|
|
|
p_t = code_label(fs, reg, 1, 1);
|
|
|
|
p_f = code_label(fs, reg, 0, 0);
|
2001-06-06 02:17:01 +08:00
|
|
|
}
|
|
|
|
final = luaK_getlabel(fs);
|
2001-12-12 06:48:44 +08:00
|
|
|
luaK_patchlistaux(fs, e->f, p_f, NO_REG, final, reg, p_f);
|
|
|
|
luaK_patchlistaux(fs, e->t, final, reg, p_t, NO_REG, p_t);
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
e->f = e->t = NO_JUMP;
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
|
2001-06-12 22:36:48 +08:00
|
|
|
luaK_dischargevars(fs, e);
|
2001-06-06 02:17:01 +08:00
|
|
|
switch (e->k) {
|
|
|
|
case VNIL: {
|
|
|
|
luaK_nil(fs, reg, 1);
|
|
|
|
break;
|
|
|
|
}
|
2001-12-12 06:48:44 +08:00
|
|
|
case VFALSE: case VTRUE: {
|
|
|
|
luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
|
|
|
|
break;
|
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
case VK: {
|
2002-01-26 05:50:39 +08:00
|
|
|
luaK_codeABc(fs, OP_LOADK, reg, e->info);
|
2001-06-06 02:17:01 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case VRELOCABLE: {
|
|
|
|
Instruction *pc = &getcode(fs, e);
|
|
|
|
SETARG_A(*pc, reg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: return;
|
2000-03-24 20:18:30 +08:00
|
|
|
}
|
2002-01-26 05:50:39 +08:00
|
|
|
e->info = reg;
|
2001-06-06 02:17:01 +08:00
|
|
|
e->k = VNONRELOC;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void discharge2anyreg (FuncState *fs, expdesc *e) {
|
|
|
|
if (e->k != VNONRELOC) {
|
|
|
|
luaK_reserveregs(fs, 1);
|
|
|
|
discharge2reg(fs, e, fs->freereg-1);
|
2000-06-17 01:51:40 +08:00
|
|
|
}
|
2000-03-24 20:18:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
static void luaK_exp2reg (FuncState *fs, expdesc *e, int reg) {
|
|
|
|
discharge2reg(fs, e, reg);
|
|
|
|
switch (e->k) {
|
|
|
|
case VVOID: {
|
|
|
|
return; /* nothing to do... */
|
|
|
|
}
|
|
|
|
case VNONRELOC: {
|
2002-01-26 05:50:39 +08:00
|
|
|
if (reg != e->info)
|
|
|
|
luaK_codeABC(fs, OP_MOVE, reg, e->info, 0);
|
2001-06-06 02:17:01 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case VJMP: {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
lua_assert(0); /* cannot happen */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dischargejumps(fs, e, reg);
|
2002-01-26 05:50:39 +08:00
|
|
|
e->info = reg;
|
2001-06-06 02:17:01 +08:00
|
|
|
e->k = VNONRELOC;
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
|
|
|
|
int reg;
|
2001-06-12 22:36:48 +08:00
|
|
|
luaK_dischargevars(fs, e);
|
2001-06-06 02:17:01 +08:00
|
|
|
freeexp(fs, e);
|
|
|
|
reg = fs->freereg;
|
|
|
|
luaK_reserveregs(fs, 1);
|
|
|
|
luaK_exp2reg(fs, e, reg);
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
|
2001-06-12 22:36:48 +08:00
|
|
|
luaK_dischargevars(fs, e);
|
2001-06-06 02:17:01 +08:00
|
|
|
if (e->k == VNONRELOC) {
|
2002-01-26 05:50:39 +08:00
|
|
|
if (!hasjumps(e)) return e->info; /* exp is already in a register */
|
|
|
|
if (e->info >= fs->nactloc) { /* reg. is not a local? */
|
|
|
|
dischargejumps(fs, e, e->info); /* put value on it */
|
|
|
|
return e->info;
|
2000-04-17 22:05:34 +08:00
|
|
|
}
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
luaK_exp2nextreg(fs, e); /* default */
|
2002-01-26 05:50:39 +08:00
|
|
|
return e->info;
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
void luaK_exp2val (FuncState *fs, expdesc *e) {
|
|
|
|
if (hasjumps(e))
|
|
|
|
luaK_exp2anyreg(fs, e);
|
|
|
|
else
|
2001-06-12 22:36:48 +08:00
|
|
|
luaK_dischargevars(fs, e);
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
int luaK_exp2RK (FuncState *fs, expdesc *e) {
|
|
|
|
luaK_exp2val(fs, e);
|
2001-12-12 06:48:44 +08:00
|
|
|
switch (e->k) {
|
2002-01-26 05:50:39 +08:00
|
|
|
case VNIL: {
|
2001-12-12 06:48:44 +08:00
|
|
|
if (fs->nk + MAXSTACK <= MAXARG_C) { /* constant fit in argC? */
|
2002-01-26 05:50:39 +08:00
|
|
|
e->info = nil_constant(fs);
|
2001-12-12 06:48:44 +08:00
|
|
|
e->k = VK;
|
2002-01-26 05:50:39 +08:00
|
|
|
return e->info + MAXSTACK;
|
2001-12-12 06:48:44 +08:00
|
|
|
}
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
case VK: {
|
2002-01-26 05:50:39 +08:00
|
|
|
if (e->info + MAXSTACK <= MAXARG_C) /* constant fit in argC? */
|
|
|
|
return e->info + MAXSTACK;
|
2001-12-12 06:48:44 +08:00
|
|
|
else break;
|
|
|
|
}
|
|
|
|
default: break;
|
2000-08-09 22:49:13 +08:00
|
|
|
}
|
2001-12-12 06:48:44 +08:00
|
|
|
/* not a constant in the right range: put in a register */
|
|
|
|
return luaK_exp2anyreg(fs, e);
|
2000-04-08 03:35:20 +08:00
|
|
|
}
|
2000-03-03 20:33:59 +08:00
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
void luaK_storevar (FuncState *fs, expdesc *var, expdesc *exp) {
|
|
|
|
switch (var->k) {
|
|
|
|
case VLOCAL: {
|
|
|
|
freeexp(fs, exp);
|
2002-01-26 05:50:39 +08:00
|
|
|
luaK_exp2reg(fs, exp, var->info);
|
2000-08-09 22:49:13 +08:00
|
|
|
break;
|
|
|
|
}
|
2001-09-08 01:39:10 +08:00
|
|
|
case VUPVAL: {
|
|
|
|
int e = luaK_exp2anyreg(fs, exp);
|
|
|
|
freereg(fs, e);
|
2002-01-26 05:50:39 +08:00
|
|
|
luaK_codeABC(fs, OP_SETUPVAL, e, var->info, 0);
|
2001-09-08 01:39:10 +08:00
|
|
|
break;
|
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
case VGLOBAL: {
|
|
|
|
int e = luaK_exp2anyreg(fs, exp);
|
|
|
|
freereg(fs, e);
|
2002-01-26 05:50:39 +08:00
|
|
|
luaK_codeABc(fs, OP_SETGLOBAL, e, var->info);
|
2001-06-06 02:17:01 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case VINDEXED: {
|
|
|
|
int e = luaK_exp2anyreg(fs, exp);
|
|
|
|
freereg(fs, e);
|
2002-01-26 05:50:39 +08:00
|
|
|
luaK_codeABC(fs, OP_SETTABLE, e, var->info, var->aux);
|
2000-08-09 22:49:13 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
2001-06-06 02:17:01 +08:00
|
|
|
lua_assert(0); /* invalid var kind to store */
|
|
|
|
break;
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-04-08 03:35:20 +08:00
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
|
2001-07-17 22:30:44 +08:00
|
|
|
int func;
|
2001-06-06 02:17:01 +08:00
|
|
|
luaK_exp2anyreg(fs, e);
|
|
|
|
freeexp(fs, e);
|
2001-07-17 22:30:44 +08:00
|
|
|
func = fs->freereg;
|
2001-06-06 02:17:01 +08:00
|
|
|
luaK_reserveregs(fs, 2);
|
2002-01-26 05:50:39 +08:00
|
|
|
luaK_codeABC(fs, OP_SELF, func, e->info, luaK_exp2RK(fs, key));
|
2001-07-17 22:30:44 +08:00
|
|
|
freeexp(fs, key);
|
2002-01-26 05:50:39 +08:00
|
|
|
e->info = func;
|
2001-06-06 02:17:01 +08:00
|
|
|
e->k = VNONRELOC;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static OpCode invertoperator (OpCode op) {
|
|
|
|
switch (op) {
|
|
|
|
case OP_TESTNE: return OP_TESTEQ;
|
|
|
|
case OP_TESTEQ: return OP_TESTNE;
|
|
|
|
case OP_TESTLT: return OP_TESTGE;
|
|
|
|
case OP_TESTLE: return OP_TESTGT;
|
|
|
|
case OP_TESTGT: return OP_TESTLE;
|
|
|
|
case OP_TESTGE: return OP_TESTLT;
|
|
|
|
case OP_TESTT: return OP_TESTF;
|
|
|
|
case OP_TESTF: return OP_TESTT;
|
|
|
|
default: lua_assert(0); return op; /* invalid jump instruction */
|
2000-08-09 02:26:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
static void invertjump (FuncState *fs, expdesc *e) {
|
2002-01-26 05:50:39 +08:00
|
|
|
Instruction *pc = getjumpcontrol(fs, e->info);
|
2001-06-06 02:17:01 +08:00
|
|
|
*pc = SET_OPCODE(*pc, invertoperator(GET_OPCODE(*pc)));
|
2000-04-08 03:35:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
static int jumponcond (FuncState *fs, expdesc *e, OpCode op) {
|
|
|
|
if (e->k == VRELOCABLE) {
|
|
|
|
Instruction ie = getcode(fs, e);
|
|
|
|
if (GET_OPCODE(ie) == OP_NOT) {
|
|
|
|
op = invertoperator(op);
|
|
|
|
fs->pc--; /* remove previous OP_NOT */
|
2001-06-11 22:56:42 +08:00
|
|
|
return luaK_condjump(fs, op, NO_REG, GETARG_B(ie), 0);
|
2001-06-06 02:17:01 +08:00
|
|
|
}
|
|
|
|
/* else go through */
|
|
|
|
}
|
|
|
|
discharge2anyreg(fs, e);
|
|
|
|
freeexp(fs, e);
|
2002-01-26 05:50:39 +08:00
|
|
|
return luaK_condjump(fs, op, NO_REG, e->info, 0);
|
2000-04-08 03:35:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
void luaK_goiftrue (FuncState *fs, expdesc *e) {
|
|
|
|
int pc; /* pc of last jump */
|
2001-06-12 22:36:48 +08:00
|
|
|
luaK_dischargevars(fs, e);
|
2001-06-06 02:17:01 +08:00
|
|
|
switch (e->k) {
|
2002-01-26 05:50:39 +08:00
|
|
|
case VK: case VTRUE: {
|
2001-06-06 02:17:01 +08:00
|
|
|
pc = NO_JUMP; /* always true; do nothing */
|
2000-04-15 01:47:24 +08:00
|
|
|
break;
|
2000-06-17 01:51:40 +08:00
|
|
|
}
|
2001-12-12 06:48:44 +08:00
|
|
|
case VFALSE: {
|
2001-06-06 02:17:01 +08:00
|
|
|
pc = luaK_codeAsBc(fs, OP_JMP, 0, NO_JUMP); /* always jump */
|
2000-06-05 22:56:18 +08:00
|
|
|
break;
|
2000-06-17 01:51:40 +08:00
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
case VJMP: {
|
|
|
|
invertjump(fs, e);
|
2002-01-26 05:50:39 +08:00
|
|
|
pc = e->info;
|
2000-04-15 01:47:24 +08:00
|
|
|
break;
|
2000-06-17 01:51:40 +08:00
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
default: {
|
2001-12-12 06:48:44 +08:00
|
|
|
pc = jumponcond(fs, e, OP_TESTF);
|
2000-04-08 03:35:20 +08:00
|
|
|
break;
|
2000-06-17 01:51:40 +08:00
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
}
|
|
|
|
luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */
|
2002-03-09 03:10:32 +08:00
|
|
|
luaK_patchtohere(fs, e->t);
|
2001-06-06 02:17:01 +08:00
|
|
|
e->t = NO_JUMP;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void luaK_goiffalse (FuncState *fs, expdesc *e) {
|
|
|
|
int pc; /* pc of last jump */
|
2001-06-12 22:36:48 +08:00
|
|
|
luaK_dischargevars(fs, e);
|
2001-06-06 02:17:01 +08:00
|
|
|
switch (e->k) {
|
2001-12-12 06:48:44 +08:00
|
|
|
case VNIL: case VFALSE: {
|
2001-06-06 02:17:01 +08:00
|
|
|
pc = NO_JUMP; /* always false; do nothing */
|
2000-04-08 03:35:20 +08:00
|
|
|
break;
|
2000-06-17 01:51:40 +08:00
|
|
|
}
|
2001-12-12 06:48:44 +08:00
|
|
|
case VTRUE: {
|
|
|
|
pc = luaK_codeAsBc(fs, OP_JMP, 0, NO_JUMP); /* always jump */
|
2000-04-08 03:35:20 +08:00
|
|
|
break;
|
2000-06-17 01:51:40 +08:00
|
|
|
}
|
2001-12-12 06:48:44 +08:00
|
|
|
case VJMP: {
|
2002-01-26 05:50:39 +08:00
|
|
|
pc = e->info;
|
2000-04-08 03:35:20 +08:00
|
|
|
break;
|
2000-06-17 01:51:40 +08:00
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
default: {
|
2001-12-12 06:48:44 +08:00
|
|
|
pc = jumponcond(fs, e, OP_TESTT);
|
2000-04-08 03:35:20 +08:00
|
|
|
break;
|
2000-06-17 01:51:40 +08:00
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
}
|
|
|
|
luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */
|
2002-03-09 03:10:32 +08:00
|
|
|
luaK_patchtohere(fs, e->f);
|
2001-06-06 02:17:01 +08:00
|
|
|
e->f = NO_JUMP;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void codenot (FuncState *fs, expdesc *e) {
|
2001-06-12 22:36:48 +08:00
|
|
|
luaK_dischargevars(fs, e);
|
2001-06-06 02:17:01 +08:00
|
|
|
switch (e->k) {
|
2001-12-12 06:48:44 +08:00
|
|
|
case VNIL: case VFALSE: {
|
|
|
|
e->k = VTRUE;
|
2000-04-08 03:35:20 +08:00
|
|
|
break;
|
2000-06-17 01:51:40 +08:00
|
|
|
}
|
2002-01-26 05:50:39 +08:00
|
|
|
case VK: case VTRUE: {
|
2001-12-12 06:48:44 +08:00
|
|
|
e->k = VFALSE;
|
2000-04-08 03:35:20 +08:00
|
|
|
break;
|
2000-06-17 01:51:40 +08:00
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
case VJMP: {
|
|
|
|
invertjump(fs, e);
|
2000-04-08 03:35:20 +08:00
|
|
|
break;
|
2000-06-17 01:51:40 +08:00
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
case VRELOCABLE:
|
|
|
|
case VNONRELOC: {
|
|
|
|
discharge2anyreg(fs, e);
|
|
|
|
freeexp(fs, e);
|
2002-01-26 05:50:39 +08:00
|
|
|
e->info = luaK_codeABC(fs, OP_NOT, 0, e->info, 0);
|
2001-06-06 02:17:01 +08:00
|
|
|
e->k = VRELOCABLE;
|
2000-04-08 03:35:20 +08:00
|
|
|
break;
|
2000-06-17 01:51:40 +08:00
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
default: {
|
|
|
|
lua_assert(0); /* cannot happen */
|
2000-06-17 01:51:40 +08:00
|
|
|
break;
|
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
}
|
|
|
|
/* interchange true and false lists */
|
|
|
|
{ int temp = e->f; e->f = e->t; e->t = temp; }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
|
2002-01-26 05:50:39 +08:00
|
|
|
t->aux = luaK_exp2RK(fs, k);
|
2001-06-06 02:17:01 +08:00
|
|
|
t->k = VINDEXED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) {
|
|
|
|
if (op == OPR_MINUS) {
|
|
|
|
luaK_exp2val(fs, e);
|
2002-01-26 05:50:39 +08:00
|
|
|
if (e->k == VK && ttype(&fs->f->k[e->info]) == LUA_TNUMBER)
|
|
|
|
e->info = luaK_numberK(fs, -nvalue(&fs->f->k[e->info]));
|
2001-06-06 02:17:01 +08:00
|
|
|
else {
|
|
|
|
luaK_exp2anyreg(fs, e);
|
|
|
|
freeexp(fs, e);
|
2002-01-26 05:50:39 +08:00
|
|
|
e->info = luaK_codeABC(fs, OP_UNM, 0, e->info, 0);
|
2001-06-06 02:17:01 +08:00
|
|
|
e->k = VRELOCABLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else /* op == NOT */
|
|
|
|
codenot(fs, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
|
|
|
|
switch (op) {
|
|
|
|
case OPR_AND: {
|
|
|
|
luaK_goiftrue(fs, v);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OPR_OR: {
|
|
|
|
luaK_goiffalse(fs, v);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OPR_CONCAT: {
|
|
|
|
luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */
|
2000-04-08 03:35:20 +08:00
|
|
|
break;
|
2000-06-17 01:51:40 +08:00
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
case OPR_SUB: case OPR_DIV: case OPR_POW: {
|
|
|
|
/* non-comutative operators */
|
|
|
|
luaK_exp2anyreg(fs, v); /* first operand must be a register */
|
2000-05-23 02:44:46 +08:00
|
|
|
break;
|
2000-06-17 01:51:40 +08:00
|
|
|
}
|
|
|
|
default: {
|
2001-06-06 02:17:01 +08:00
|
|
|
luaK_exp2RK(fs, v);
|
2000-05-23 02:44:46 +08:00
|
|
|
break;
|
2000-06-17 01:51:40 +08:00
|
|
|
}
|
2000-04-08 03:35:20 +08:00
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* opcode for each binary operator */
|
|
|
|
static const OpCode codes[] = { /* ORDER OPR */
|
|
|
|
OP_ADD, OP_SUB, OP_MUL, OP_DIV,
|
|
|
|
OP_POW, OP_CONCAT,
|
|
|
|
OP_TESTNE, OP_TESTEQ,
|
|
|
|
OP_TESTLT, OP_TESTLE, OP_TESTGT, OP_TESTGE
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* `inverted' opcode for each binary operator */
|
|
|
|
/* ( -1 means operator has no inverse) */
|
|
|
|
static const OpCode invcodes[] = { /* ORDER OPR */
|
|
|
|
OP_ADD, (OpCode)-1, OP_MUL, (OpCode)-1,
|
|
|
|
(OpCode)-1, (OpCode)-1,
|
|
|
|
OP_TESTNE, OP_TESTEQ,
|
|
|
|
OP_TESTGT, OP_TESTGE, OP_TESTLT, OP_TESTLE
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) {
|
|
|
|
switch (op) {
|
|
|
|
case OPR_AND: {
|
|
|
|
lua_assert(e1->t == NO_JUMP); /* list must be closed */
|
2001-06-12 22:36:48 +08:00
|
|
|
luaK_dischargevars(fs, e2);
|
2001-06-06 02:17:01 +08:00
|
|
|
luaK_concat(fs, &e1->f, e2->f);
|
2002-01-26 05:50:39 +08:00
|
|
|
e1->k = e2->k; e1->info = e2->info; e1->aux = e2->aux; e1->t = e2->t;
|
2001-06-06 02:17:01 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OPR_OR: {
|
|
|
|
lua_assert(e1->f == NO_JUMP); /* list must be closed */
|
2001-06-12 22:36:48 +08:00
|
|
|
luaK_dischargevars(fs, e2);
|
2001-06-06 02:17:01 +08:00
|
|
|
luaK_concat(fs, &e1->t, e2->t);
|
2002-01-26 05:50:39 +08:00
|
|
|
e1->k = e2->k; e1->info = e2->info; e1->aux = e2->aux; e1->f = e2->f;
|
2001-06-06 02:17:01 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OPR_CONCAT: {
|
|
|
|
luaK_exp2val(fs, e2);
|
|
|
|
if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) {
|
2002-01-26 05:50:39 +08:00
|
|
|
lua_assert(e1->info == GETARG_B(getcode(fs, e2))-1);
|
2001-06-06 02:17:01 +08:00
|
|
|
freeexp(fs, e1);
|
2002-01-26 05:50:39 +08:00
|
|
|
SETARG_B(getcode(fs, e2), e1->info);
|
|
|
|
e1->k = e2->k; e1->info = e2->info;
|
2001-06-06 02:17:01 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
luaK_exp2nextreg(fs, e2);
|
|
|
|
freeexp(fs, e2);
|
|
|
|
freeexp(fs, e1);
|
2002-01-26 05:50:39 +08:00
|
|
|
e1->info = luaK_codeABC(fs, codes[op], 0, e1->info, e2->info);
|
2001-06-06 02:17:01 +08:00
|
|
|
e1->k = VRELOCABLE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
int o1, o2;
|
|
|
|
OpCode opc;
|
|
|
|
if (e1->k != VK) { /* not a constant operator? */
|
2002-01-26 05:50:39 +08:00
|
|
|
o1 = e1->info;
|
2001-06-06 02:17:01 +08:00
|
|
|
o2 = luaK_exp2RK(fs, e2); /* maybe other operator is constant... */
|
|
|
|
opc = codes[op];
|
|
|
|
}
|
|
|
|
else { /* invert operands */
|
|
|
|
o2 = luaK_exp2RK(fs, e1); /* constant must be 2nd operand */
|
|
|
|
o1 = luaK_exp2anyreg(fs, e2); /* other operator must be in register */
|
|
|
|
opc = invcodes[op]; /* use inverted operator */
|
|
|
|
}
|
|
|
|
freeexp(fs, e2);
|
|
|
|
freeexp(fs, e1);
|
|
|
|
if (op < OPR_NE) { /* ORDER OPR */
|
2002-01-26 05:50:39 +08:00
|
|
|
e1->info = luaK_codeABC(fs, opc, 0, o1, o2);
|
2001-06-06 02:17:01 +08:00
|
|
|
e1->k = VRELOCABLE;
|
|
|
|
}
|
|
|
|
else { /* jump */
|
2002-01-26 05:50:39 +08:00
|
|
|
e1->info = luaK_condjump(fs, opc, o1, 0, o2);
|
2001-06-06 02:17:01 +08:00
|
|
|
e1->k = VJMP;
|
|
|
|
}
|
|
|
|
}
|
2000-05-23 02:44:46 +08:00
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void codelineinfo (FuncState *fs) {
|
|
|
|
Proto *f = fs->f;
|
|
|
|
LexState *ls = fs->ls;
|
|
|
|
if (ls->lastline > fs->lastline) {
|
|
|
|
if (ls->lastline > fs->lastline+1) {
|
|
|
|
luaM_growvector(fs->L, f->lineinfo, fs->nlineinfo, f->sizelineinfo, int,
|
2001-11-29 04:13:13 +08:00
|
|
|
MAX_INT, "line info overflow");
|
2001-06-06 02:17:01 +08:00
|
|
|
f->lineinfo[fs->nlineinfo++] = -(ls->lastline - (fs->lastline+1));
|
|
|
|
}
|
|
|
|
luaM_growvector(fs->L, f->lineinfo, fs->nlineinfo, f->sizelineinfo, int,
|
2001-11-29 04:13:13 +08:00
|
|
|
MAX_INT, "line info overflow");
|
2001-06-06 02:17:01 +08:00
|
|
|
f->lineinfo[fs->nlineinfo++] = fs->pc;
|
|
|
|
fs->lastline = ls->lastline;
|
2000-04-08 03:35:20 +08:00
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int luaK_code (FuncState *fs, Instruction i) {
|
|
|
|
Proto *f;
|
2000-08-09 04:42:07 +08:00
|
|
|
codelineinfo(fs);
|
2001-06-06 02:17:01 +08:00
|
|
|
f = fs->f;
|
2000-06-22 02:13:56 +08:00
|
|
|
/* put new instruction in code array */
|
2000-12-28 20:55:41 +08:00
|
|
|
luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction,
|
2001-11-29 04:13:13 +08:00
|
|
|
MAX_INT, "code size overflow");
|
2000-12-28 20:55:41 +08:00
|
|
|
f->code[fs->pc] = i;
|
2000-04-08 03:35:20 +08:00
|
|
|
return fs->pc++;
|
|
|
|
}
|
|
|
|
|
2000-05-23 02:44:46 +08:00
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {
|
|
|
|
lua_assert(getOpMode(o) == iABC);
|
|
|
|
return luaK_code(fs, CREATE_ABC(o, a, b, c));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-25 01:19:07 +08:00
|
|
|
int luaK_codeABc (FuncState *fs, OpCode o, int a, unsigned int bc) {
|
2001-06-06 02:17:01 +08:00
|
|
|
lua_assert(getOpMode(o) == iABc || getOpMode(o) == iAsBc);
|
|
|
|
return luaK_code(fs, CREATE_ABc(o, a, bc));
|
|
|
|
}
|
2000-06-29 04:21:06 +08:00
|
|
|
|