浏览代码

new way to code RK values

Roberto Ierusalimschy 21 年之前
父节点
当前提交
02a2c01ccd
共有 4 个文件被更改,包括 32 次插入16 次删除
  1. 5 5
      lcode.c
  2. 5 6
      ldebug.c
  3. 19 2
      lopcodes.h
  4. 3 3
      lvm.c

+ 5 - 5
lcode.c

@@ -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
 ** Code generator for Lua
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -418,16 +418,16 @@ int luaK_exp2RK (FuncState *fs, expdesc *e) {
   luaK_exp2val(fs, e);
   luaK_exp2val(fs, e);
   switch (e->k) {
   switch (e->k) {
     case VNIL: {
     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->info = nil_constant(fs);
         e->k = VK;
         e->k = VK;
-        return e->info + MAXSTACK;
+        return RKASK(e->info);
       }
       }
       else break;
       else break;
     }
     }
     case VK: {
     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;
       else break;
     }
     }
     default: break;
     default: break;

+ 5 - 6
ldebug.c

@@ -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
 ** Debug Interface
 ** See Copyright Notice in lua.h
 ** 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 OpArgN: check(r == 0); break;
     case OpArgU: break;
     case OpArgU: break;
     case OpArgR: checkreg(pt, r); 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;
       break;
   }
   }
   return 1;
   return 1;
@@ -432,9 +432,8 @@ int luaG_checkcode (const Proto *pt) {
 
 
 
 
 static const char *kname (Proto *p, int c) {
 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
   else
     return "?";
     return "?";
 }
 }

+ 19 - 2
lopcodes.h

@@ -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
 ** Opcodes for Lua virtual machine
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -109,6 +109,23 @@ enum OpMode {iABC, iABx, iAsBx};  /* basic instruction format */
 			| (cast(Instruction, bc)<<POS_Bx))
 			| (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
 ** R(x) - register
 ** Kst(x) - constant (in constant table)
 ** 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)
 */
 */
 
 
 
 

+ 3 - 3
lvm.c

@@ -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
 ** Lua virtual machine
 ** See Copyright Notice in lua.h
 ** 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 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 RC(i)	check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
 #define RKB(i)	check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
 #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, \
 #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))
 #define KBx(i)	check_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_Bx(i))