Browse Source

OP_LOADINT can be done by OP_LOADK

v5-2
Roberto Ierusalimschy 23 years ago
parent
commit
74907fb71e
  1. 113
      lcode.c
  2. 5
      lcode.h
  3. 2
      lopcodes.c
  4. 6
      lopcodes.h
  5. 37
      lparser.c
  6. 8
      lparser.h

113
lcode.c

@ -22,7 +22,7 @@
#define hasjumps(e) ((e)->t != (e)->f)
#define getcode(fs,e) ((fs)->f->code[(e)->u.i.info])
#define getcode(fs,e) ((fs)->f->code[(e)->info])
@ -202,7 +202,7 @@ static void freereg (FuncState *fs, int reg) {
static void freeexp (FuncState *fs, expdesc *e) {
if (e->k == VNONRELOC)
freereg(fs, e->u.i.info);
freereg(fs, e->info);
}
@ -225,14 +225,14 @@ static int addk (FuncState *fs, TObject *k, TObject *v) {
}
int luaK_stringk (FuncState *fs, TString *s) {
int luaK_stringK (FuncState *fs, TString *s) {
TObject o;
setsvalue(&o, s);
return addk(fs, &o, &o);
}
static int number_constant (FuncState *fs, lua_Number r) {
int luaK_numberK (FuncState *fs, lua_Number r) {
TObject o;
setnvalue(&o, r);
return addk(fs, &o, &o);
@ -252,7 +252,7 @@ void luaK_setcallreturns (FuncState *fs, expdesc *e, int nresults) {
SETARG_C(getcode(fs, e), nresults+1);
if (nresults == 1) { /* `regular' expression? */
e->k = VNONRELOC;
e->u.i.info = GETARG_A(getcode(fs, e));
e->info = GETARG_A(getcode(fs, e));
}
}
}
@ -265,19 +265,19 @@ void luaK_dischargevars (FuncState *fs, expdesc *e) {
break;
}
case VUPVAL: {
e->u.i.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.i.info, 0);
e->info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->info, 0);
e->k = VRELOCABLE;
break;
}
case VGLOBAL: {
e->u.i.info = luaK_codeABc(fs, OP_GETGLOBAL, 0, e->u.i.info);
e->info = luaK_codeABc(fs, OP_GETGLOBAL, 0, e->info);
e->k = VRELOCABLE;
break;
}
case VINDEXED: {
freereg(fs, e->u.i.aux);
freereg(fs, e->u.i.info);
e->u.i.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.i.info, e->u.i.aux);
freereg(fs, e->aux);
freereg(fs, e->info);
e->info = luaK_codeABC(fs, OP_GETTABLE, 0, e->info, e->aux);
e->k = VRELOCABLE;
break;
}
@ -334,17 +334,8 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
break;
}
case VNUMBER: {
lua_Number f = e->u.n;
int i = cast(int, f);
if ((lua_Number)i == f && -MAXARG_sBc <= i && i <= MAXARG_sBc)
luaK_codeAsBc(fs, OP_LOADINT, reg, i); /* f has a small int value */
else
luaK_codeABc(fs, OP_LOADK, reg, number_constant(fs, f));
break;
}
case VK: {
luaK_codeABc(fs, OP_LOADK, reg, e->u.i.info);
luaK_codeABc(fs, OP_LOADK, reg, e->info);
break;
}
case VRELOCABLE: {
@ -354,7 +345,7 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
}
default: return;
}
e->u.i.info = reg;
e->info = reg;
e->k = VNONRELOC;
}
@ -374,8 +365,8 @@ static void luaK_exp2reg (FuncState *fs, expdesc *e, int reg) {
return; /* nothing to do... */
}
case VNONRELOC: {
if (reg != e->u.i.info)
luaK_codeABC(fs, OP_MOVE, reg, e->u.i.info, 0);
if (reg != e->info)
luaK_codeABC(fs, OP_MOVE, reg, e->info, 0);
break;
}
case VJMP: {
@ -387,7 +378,7 @@ static void luaK_exp2reg (FuncState *fs, expdesc *e, int reg) {
}
}
dischargejumps(fs, e, reg);
e->u.i.info = reg;
e->info = reg;
e->k = VNONRELOC;
}
@ -405,14 +396,14 @@ void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
luaK_dischargevars(fs, e);
if (e->k == VNONRELOC) {
if (!hasjumps(e)) return e->u.i.info; /* exp is already in a register */
if (e->u.i.info >= fs->nactloc) { /* reg. is not a local? */
dischargejumps(fs, e, e->u.i.info); /* put value on it */
return e->u.i.info;
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;
}
}
luaK_exp2nextreg(fs, e); /* default */
return e->u.i.info;
return e->info;
}
@ -427,18 +418,17 @@ void luaK_exp2val (FuncState *fs, expdesc *e) {
int luaK_exp2RK (FuncState *fs, expdesc *e) {
luaK_exp2val(fs, e);
switch (e->k) {
case VNUMBER: case VNIL: {
case VNIL: {
if (fs->nk + MAXSTACK <= MAXARG_C) { /* constant fit in argC? */
e->u.i.info = (e->k == VNIL) ? nil_constant(fs) :
number_constant(fs, e->u.n);
e->info = nil_constant(fs);
e->k = VK;
return e->u.i.info + MAXSTACK;
return e->info + MAXSTACK;
}
else break;
}
case VK: {
if (e->u.i.info + MAXSTACK <= MAXARG_C) /* constant fit in argC? */
return e->u.i.info + MAXSTACK;
if (e->info + MAXSTACK <= MAXARG_C) /* constant fit in argC? */
return e->info + MAXSTACK;
else break;
}
default: break;
@ -452,25 +442,25 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *exp) {
switch (var->k) {
case VLOCAL: {
freeexp(fs, exp);
luaK_exp2reg(fs, exp, var->u.i.info);
luaK_exp2reg(fs, exp, var->info);
break;
}
case VUPVAL: {
int e = luaK_exp2anyreg(fs, exp);
freereg(fs, e);
luaK_codeABC(fs, OP_SETUPVAL, e, var->u.i.info, 0);
luaK_codeABC(fs, OP_SETUPVAL, e, var->info, 0);
break;
}
case VGLOBAL: {
int e = luaK_exp2anyreg(fs, exp);
freereg(fs, e);
luaK_codeABc(fs, OP_SETGLOBAL, e, var->u.i.info);
luaK_codeABc(fs, OP_SETGLOBAL, e, var->info);
break;
}
case VINDEXED: {
int e = luaK_exp2anyreg(fs, exp);
freereg(fs, e);
luaK_codeABC(fs, OP_SETTABLE, e, var->u.i.info, var->u.i.aux);
luaK_codeABC(fs, OP_SETTABLE, e, var->info, var->aux);
break;
}
default: {
@ -487,9 +477,9 @@ void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
freeexp(fs, e);
func = fs->freereg;
luaK_reserveregs(fs, 2);
luaK_codeABC(fs, OP_SELF, func, e->u.i.info, luaK_exp2RK(fs, key));
luaK_codeABC(fs, OP_SELF, func, e->info, luaK_exp2RK(fs, key));
freeexp(fs, key);
e->u.i.info = func;
e->info = func;
e->k = VNONRELOC;
}
@ -510,7 +500,7 @@ static OpCode invertoperator (OpCode op) {
static void invertjump (FuncState *fs, expdesc *e) {
Instruction *pc = getjumpcontrol(fs, e->u.i.info);
Instruction *pc = getjumpcontrol(fs, e->info);
*pc = SET_OPCODE(*pc, invertoperator(GET_OPCODE(*pc)));
}
@ -527,7 +517,7 @@ static int jumponcond (FuncState *fs, expdesc *e, OpCode op) {
}
discharge2anyreg(fs, e);
freeexp(fs, e);
return luaK_condjump(fs, op, NO_REG, e->u.i.info, 0);
return luaK_condjump(fs, op, NO_REG, e->info, 0);
}
@ -535,7 +525,7 @@ void luaK_goiftrue (FuncState *fs, expdesc *e) {
int pc; /* pc of last jump */
luaK_dischargevars(fs, e);
switch (e->k) {
case VK: case VNUMBER: case VTRUE: {
case VK: case VTRUE: {
pc = NO_JUMP; /* always true; do nothing */
break;
}
@ -545,7 +535,7 @@ void luaK_goiftrue (FuncState *fs, expdesc *e) {
}
case VJMP: {
invertjump(fs, e);
pc = e->u.i.info;
pc = e->info;
break;
}
default: {
@ -572,7 +562,7 @@ static void luaK_goiffalse (FuncState *fs, expdesc *e) {
break;
}
case VJMP: {
pc = e->u.i.info;
pc = e->info;
break;
}
default: {
@ -593,7 +583,7 @@ static void codenot (FuncState *fs, expdesc *e) {
e->k = VTRUE;
break;
}
case VK: case VNUMBER: case VTRUE: {
case VK: case VTRUE: {
e->k = VFALSE;
break;
}
@ -605,7 +595,7 @@ static void codenot (FuncState *fs, expdesc *e) {
case VNONRELOC: {
discharge2anyreg(fs, e);
freeexp(fs, e);
e->u.i.info = luaK_codeABC(fs, OP_NOT, 0, e->u.i.info, 0);
e->info = luaK_codeABC(fs, OP_NOT, 0, e->info, 0);
e->k = VRELOCABLE;
break;
}
@ -620,7 +610,7 @@ static void codenot (FuncState *fs, expdesc *e) {
void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
t->u.i.aux = luaK_exp2RK(fs, k);
t->aux = luaK_exp2RK(fs, k);
t->k = VINDEXED;
}
@ -628,12 +618,12 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) {
if (op == OPR_MINUS) {
luaK_exp2val(fs, e);
if (e->k == VNUMBER)
e->u.n = -e->u.n;
if (e->k == VK && ttype(&fs->f->k[e->info]) == LUA_TNUMBER)
e->info = luaK_numberK(fs, -nvalue(&fs->f->k[e->info]));
else {
luaK_exp2anyreg(fs, e);
freeexp(fs, e);
e->u.i.info = luaK_codeABC(fs, OP_UNM, 0, e->u.i.info, 0);
e->info = luaK_codeABC(fs, OP_UNM, 0, e->info, 0);
e->k = VRELOCABLE;
}
}
@ -695,30 +685,29 @@ void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) {
lua_assert(e1->t == NO_JUMP); /* list must be closed */
luaK_dischargevars(fs, e2);
luaK_concat(fs, &e1->f, e2->f);
e1->k = e2->k; e1->u = e2->u; e1->t = e2->t;
e1->k = e2->k; e1->info = e2->info; e1->aux = e2->aux; e1->t = e2->t;
break;
}
case OPR_OR: {
lua_assert(e1->f == NO_JUMP); /* list must be closed */
luaK_dischargevars(fs, e2);
luaK_concat(fs, &e1->t, e2->t);
e1->k = e2->k; e1->u = e2->u; e1->f = e2->f;
e1->k = e2->k; e1->info = e2->info; e1->aux = e2->aux; e1->f = e2->f;
break;
}
case OPR_CONCAT: {
luaK_exp2val(fs, e2);
if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) {
lua_assert(e1->u.i.info == GETARG_B(getcode(fs, e2))-1);
lua_assert(e1->info == GETARG_B(getcode(fs, e2))-1);
freeexp(fs, e1);
SETARG_B(getcode(fs, e2), e1->u.i.info);
e1->k = e2->k; e1->u.i.info = e2->u.i.info;
SETARG_B(getcode(fs, e2), e1->info);
e1->k = e2->k; e1->info = e2->info;
}
else {
luaK_exp2nextreg(fs, e2);
freeexp(fs, e2);
freeexp(fs, e1);
e1->u.i.info = luaK_codeABC(fs, codes[op], 0, e1->u.i.info,
e2->u.i.info);
e1->info = luaK_codeABC(fs, codes[op], 0, e1->info, e2->info);
e1->k = VRELOCABLE;
}
break;
@ -727,7 +716,7 @@ void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) {
int o1, o2;
OpCode opc;
if (e1->k != VK) { /* not a constant operator? */
o1 = e1->u.i.info;
o1 = e1->info;
o2 = luaK_exp2RK(fs, e2); /* maybe other operator is constant... */
opc = codes[op];
}
@ -739,11 +728,11 @@ void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) {
freeexp(fs, e2);
freeexp(fs, e1);
if (op < OPR_NE) { /* ORDER OPR */
e1->u.i.info = luaK_codeABC(fs, opc, 0, o1, o2);
e1->info = luaK_codeABC(fs, opc, 0, o1, o2);
e1->k = VRELOCABLE;
}
else { /* jump */
e1->u.i.info = luaK_condjump(fs, opc, o1, 0, o2);
e1->info = luaK_condjump(fs, opc, o1, 0, o2);
e1->k = VJMP;
}
}

5
lcode.h

@ -1,5 +1,5 @@
/*
** $Id: lcode.h,v 1.24 2001/07/24 17:19:07 roberto Exp $
** $Id: lcode.h,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $
** Code generator for Lua
** See Copyright Notice in lua.h
*/
@ -43,7 +43,8 @@ int luaK_codeABc (FuncState *fs, OpCode o, int A, unsigned int Bc);
int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C);
void luaK_nil (FuncState *fs, int from, int n);
void luaK_reserveregs (FuncState *fs, int n);
int luaK_stringk (FuncState *fs, TString *s);
int luaK_stringK (FuncState *fs, TString *s);
int luaK_numberK (FuncState *fs, lua_Number r);
void luaK_dischargevars (FuncState *fs, expdesc *e);
int luaK_exp2anyreg (FuncState *fs, expdesc *e);
void luaK_exp2nextreg (FuncState *fs, expdesc *e);

2
lopcodes.c

@ -17,7 +17,6 @@
const char *const luaP_opnames[] = {
"MOVE",
"LOADK",
"LOADINT",
"LOADBOOL",
"LOADNIL",
"GETUPVAL",
@ -67,7 +66,6 @@ const lu_byte luaP_opmodes[NUM_OPCODES] = {
/* T _ B C sA K mode opcode */
opmode(0,0,1,0, 1,0,iABC) /* OP_MOVE */
,opmode(0,0,0,0, 1,1,iABc) /* OP_LOADK */
,opmode(0,0,0,0, 1,0,iAsBc) /* OP_LOADINT */
,opmode(0,0,0,0, 1,0,iABC) /* OP_LOADBOOL */
,opmode(0,0,1,0, 1,0,iABC) /* OP_LOADNIL */
,opmode(0,0,0,0, 1,0,iABC) /* OP_GETUPVAL */

6
lopcodes.h

@ -129,7 +129,6 @@ name args description
------------------------------------------------------------------------*/
OP_MOVE,/* A B R(A) := R(B) */
OP_LOADK,/* A Bc R(A) := Kst(Bc) */
OP_LOADINT,/* A sBc R(A) := (Number)sBc */
OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) PC++ */
OP_LOADNIL,/* A B R(A) := ... := R(B) := nil */
OP_GETUPVAL,/* A B R(A) := UpValue[B] */
@ -180,10 +179,7 @@ OP_SETLIST,/* A Bc R(A)[Bc-Bc%FPF+i] := R(A+i), 1 <= i <= Bc%FPF+1 */
OP_SETLISTO,/* A Bc */
OP_CLOSE,/* A close all variables in the stack up to (>=) R(A)*/
OP_CLOSURE /* A Bc R(A) := closure(KPROTO[Bc], R(A), ... ,R(A+n)) */
/*----------------------------------------------------------------------
pseudo-instructions (interruptions): cannot occur in regular code
------------------------------------------------------------------------*/
OP_CLOSURE/* A Bc R(A) := closure(KPROTO[Bc], R(A), ... ,R(A+n)) */
} OpCode;

37
lparser.c

@ -130,12 +130,12 @@ static TString *str_checkname (LexState *ls) {
static void init_exp (expdesc *e, expkind k, int i) {
e->f = e->t = NO_JUMP;
e->k = k;
e->u.i.info = i;
e->info = i;
}
static void codestring (LexState *ls, expdesc *e, TString *s) {
init_exp(e, VK, luaK_stringk(ls->fs, s));
init_exp(e, VK, luaK_stringK(ls->fs, s));
}
@ -201,7 +201,7 @@ static void new_localvarstr (LexState *ls, const char *name, int n) {
static int indexupvalue (FuncState *fs, expdesc *v) {
int i;
for (i=0; i<fs->f->nupvalues; i++) {
if (fs->upvalues[i].k == v->k && fs->upvalues[i].u.i.info == v->u.i.info)
if (fs->upvalues[i].k == v->k && fs->upvalues[i].info == v->info)
return i;
}
/* new one */
@ -228,10 +228,10 @@ static void singlevar (FuncState *fs, TString *n, expdesc *var, int baselevel) {
singlevar(fs->prev, n, var, 0);
if (var->k == VGLOBAL) {
if (baselevel)
var->u.i.info = luaK_stringk(fs, n); /* info points to global name */
var->info = luaK_stringK(fs, n); /* info points to global name */
}
else { /* local variable in some upper level? */
var->u.i.info = indexupvalue(fs, var);
var->info = indexupvalue(fs, var);
var->k = VUPVAL; /* upvalue in this level */
}
}
@ -447,7 +447,7 @@ static void funcargs (LexState *ls, expdesc *f) {
}
}
lua_assert(f->k == VNONRELOC);
base = f->u.i.info; /* base register for call */
base = f->info; /* base register for call */
if (args.k == VCALL)
nparams = LUA_MULTRET; /* open call */
else {
@ -489,7 +489,7 @@ static void recfield (LexState *ls, expdesc *t) {
luaK_exp2RK(fs, &key);
expr(ls, &val);
luaK_exp2anyreg(fs, &val);
luaK_codeABC(fs, OP_SETTABLE, val.u.i.info, t->u.i.info,
luaK_codeABC(fs, OP_SETTABLE, val.info, t->info,
luaK_exp2RK(fs, &key));
fs->freereg = reg; /* free registers */
}
@ -526,7 +526,7 @@ static int listfields (LexState *ls, expdesc *t) {
luaK_exp2nextreg(fs, &v);
luaX_checklimit(ls, n, MAXARG_Bc, "items in a constructor");
if (n%LFIELDS_PER_FLUSH == 0) {
luaK_codeABc(fs, OP_SETLIST, t->u.i.info, n-1); /* flush */
luaK_codeABc(fs, OP_SETLIST, t->info, n-1); /* flush */
fs->freereg = reg; /* free registers */
}
expr(ls, &v);
@ -534,11 +534,11 @@ static int listfields (LexState *ls, expdesc *t) {
}
if (v.k == VCALL) {
luaK_setcallreturns(fs, &v, LUA_MULTRET);
luaK_codeABc(fs, OP_SETLISTO, t->u.i.info, n-1);
luaK_codeABc(fs, OP_SETLISTO, t->info, n-1);
}
else {
luaK_exp2nextreg(fs, &v);
luaK_codeABc(fs, OP_SETLIST, t->u.i.info, n-1);
luaK_codeABc(fs, OP_SETLIST, t->info, n-1);
}
fs->freereg = reg; /* free registers */
return n;
@ -686,8 +686,7 @@ static void simpleexp (LexState *ls, expdesc *v) {
| primaryexp */
switch (ls->t.token) {
case TK_NUMBER: {
init_exp(v, VNUMBER, 0);
v->u.n = ls->t.seminfo.r;
init_exp(v, VK, luaK_numberK(ls->fs, ls->t.seminfo.r));
next(ls); /* must use `seminfo' before `next' */
break;
}
@ -859,18 +858,18 @@ static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) {
int conflict = 0;
for (; lh; lh = lh->prev) {
if (lh->v.k == VINDEXED) {
if (lh->v.u.i.info == v->u.i.info) { /* conflict? */
if (lh->v.info == v->info) { /* conflict? */
conflict = 1;
lh->v.u.i.info = extra; /* previous assignment will use safe copy */
lh->v.info = extra; /* previous assignment will use safe copy */
}
if (lh->v.u.i.aux == v->u.i.info) { /* conflict? */
if (lh->v.aux == v->info) { /* conflict? */
conflict = 1;
lh->v.u.i.aux = extra; /* previous assignment will use safe copy */
lh->v.aux = extra; /* previous assignment will use safe copy */
}
}
}
if (conflict) {
luaK_codeABC(fs, OP_MOVE, fs->freereg, v->u.i.info, 0); /* make copy */
luaK_codeABC(fs, OP_MOVE, fs->freereg, v->info, 0); /* make copy */
luaK_reserveregs(fs, 1);
}
}
@ -980,8 +979,8 @@ static void fornum (LexState *ls, TString *varname) {
exp1(ls); /* limit */
if (optional(ls, ','))
exp1(ls); /* optional step */
else {
luaK_codeAsBc(fs, OP_LOADINT, fs->freereg, 1); /* default step */
else { /* default step = 1 */
luaK_codeABc(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1));
luaK_reserveregs(fs, 1);
}
new_localvar(ls, varname, 0);

8
lparser.h

@ -34,7 +34,6 @@ typedef enum {
VNIL,
VTRUE,
VFALSE,
VNUMBER, /* n = value */
VK, /* info = index of constant in `k' */
VLOCAL, /* info = local register */
VUPVAL, /* info = index of upvalue in `upvalues' */
@ -48,12 +47,7 @@ typedef enum {
typedef struct expdesc {
expkind k;
union {
struct {
int info, aux;
} i;
lua_Number n;
} u;
int info, aux;
int t; /* patch list of `exit when true' */
int f; /* patch list of `exit when false' */
} expdesc;

Loading…
Cancel
Save