new way to code RK values

This commit is contained in:
Roberto Ierusalimschy 2004-06-29 15:49:02 -03:00
parent 23e08a68f5
commit 02a2c01ccd
4 changed files with 32 additions and 16 deletions

10
lcode.c
View File

@ -1,5 +1,5 @@
/*
** $Id: lcode.c,v 2.2 2004/04/30 20:13:38 roberto Exp roberto $
** $Id: lcode.c,v 2.3 2004/05/31 18:51:50 roberto Exp roberto $
** Code generator for Lua
** See Copyright Notice in lua.h
*/
@ -418,16 +418,16 @@ int luaK_exp2RK (FuncState *fs, expdesc *e) {
luaK_exp2val(fs, e);
switch (e->k) {
case VNIL: {
if (fs->nk + MAXSTACK <= MAXARG_C) { /* constant fit in argC? */
if (fs->nk <= MAXINDEXRK) { /* constant fit in RK operand? */
e->info = nil_constant(fs);
e->k = VK;
return e->info + MAXSTACK;
return RKASK(e->info);
}
else break;
}
case VK: {
if (e->info + MAXSTACK <= MAXARG_C) /* constant fit in argC? */
return e->info + MAXSTACK;
if (e->info <= MAXINDEXRK) /* constant fit in argC? */
return RKASK(e->info);
else break;
}
default: break;

View File

@ -1,5 +1,5 @@
/*
** $Id: ldebug.c,v 2.5 2004/05/31 18:51:50 roberto Exp roberto $
** $Id: ldebug.c,v 2.6 2004/06/02 19:07:55 roberto Exp roberto $
** Debug Interface
** See Copyright Notice in lua.h
*/
@ -276,8 +276,8 @@ static int checkArgMode (const Proto *pt, int r, enum OpArgMask mode) {
case OpArgN: check(r == 0); break;
case OpArgU: break;
case OpArgR: checkreg(pt, r); break;
case OpArgK:
check(r < pt->maxstacksize || (r >= MAXSTACK && r-MAXSTACK < pt->sizek));
case OpArgK:
check(ISK(r) ? INDEXK(r) < pt->sizek : r < pt->maxstacksize);
break;
}
return 1;
@ -432,9 +432,8 @@ int luaG_checkcode (const Proto *pt) {
static const char *kname (Proto *p, int c) {
c = c - MAXSTACK;
if (c >= 0 && ttisstring(&p->k[c]))
return svalue(&p->k[c]);
if (ISK(c) && ttisstring(&p->k[INDEXK(c)]))
return svalue(&p->k[INDEXK(c)]);
else
return "?";
}

View File

@ -1,5 +1,5 @@
/*
** $Id: lopcodes.h,v 1.108 2004/05/17 12:34:00 roberto Exp roberto $
** $Id: lopcodes.h,v 1.109 2004/05/31 18:51:50 roberto Exp roberto $
** Opcodes for Lua virtual machine
** See Copyright Notice in lua.h
*/
@ -109,6 +109,23 @@ enum OpMode {iABC, iABx, iAsBx}; /* basic instruction format */
| (cast(Instruction, bc)<<POS_Bx))
/*
** Macros to operate RK indices
*/
/* this bit 1 means constant (0 means register) */
#define BITRK (1 << (SIZE_B - 1))
/* test whether value is a constant */
#define ISK(x) ((x) & BITRK)
/* gets the index of the constant */
#define INDEXK(r) ((int)(r) & ~BITRK)
#define MAXINDEXRK (BITRK - 1)
/* code a constant index as a RK value */
#define RKASK(x) ((x) | BITRK)
/*
@ -120,7 +137,7 @@ enum OpMode {iABC, iABx, iAsBx}; /* basic instruction format */
/*
** R(x) - register
** Kst(x) - constant (in constant table)
** RK(x) == if x < MAXSTACK then R(x) else Kst(x-MAXSTACK)
** RK(x) == if ISK(x) then Kst(INDEXK(x)) else R(x)
*/

6
lvm.c
View File

@ -1,5 +1,5 @@
/*
** $Id: lvm.c,v 2.9 2004/06/08 16:23:58 roberto Exp roberto $
** $Id: lvm.c,v 2.10 2004/06/29 17:05:00 roberto Exp roberto $
** Lua virtual machine
** See Copyright Notice in lua.h
*/
@ -369,9 +369,9 @@ static StkId Arith (lua_State *L, StkId ra, const TValue *rb,
#define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
#define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
#define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
(GETARG_B(i) < MAXSTACK) ? base+GETARG_B(i) : k+GETARG_B(i)-MAXSTACK)
ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
#define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
(GETARG_C(i) < MAXSTACK) ? base+GETARG_C(i) : k+GETARG_C(i)-MAXSTACK)
ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
#define KBx(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_Bx(i))