From c116dcb92b2ee36ef5c4541a88e73540837f1057 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 14 May 2003 09:09:12 -0300 Subject: [PATCH] better information about instruction behavior (use of arguments) --- lcode.c | 10 ++++--- ldebug.c | 47 +++++++++++++++++------------- lopcodes.c | 85 ++++++++++++++++++++++++++---------------------------- lopcodes.h | 30 +++++++++++-------- lvm.c | 14 +++++---- 5 files changed, 100 insertions(+), 86 deletions(-) diff --git a/lcode.c b/lcode.c index 96b01e37..bb15a1cd 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 1.116 2003/02/27 12:33:07 roberto Exp roberto $ +** $Id: lcode.c,v 1.117 2003/04/03 13:35:34 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -88,7 +88,7 @@ static int luaK_getjump (FuncState *fs, int pc) { static Instruction *getjumpcontrol (FuncState *fs, int pc) { Instruction *pi = &fs->f->code[pc]; - if (pc >= 1 && testOpMode(GET_OPCODE(*(pi-1)), OpModeT)) + if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1)))) return pi-1; else return pi; @@ -462,8 +462,7 @@ void luaK_self (FuncState *fs, expdesc *e, expdesc *key) { static void invertjump (FuncState *fs, expdesc *e) { Instruction *pc = getjumpcontrol(fs, e->info); - lua_assert(testOpMode(GET_OPCODE(*pc), OpModeT) && - GET_OPCODE(*pc) != OP_TEST); + lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TEST); SETARG_A(*pc, !(GETARG_A(*pc))); } @@ -703,12 +702,15 @@ int luaK_code (FuncState *fs, Instruction i, int line) { int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) { lua_assert(getOpMode(o) == iABC); + lua_assert(getBMode(o) != OpArgN || b == 0); + lua_assert(getCMode(o) != OpArgN || c == 0); return luaK_code(fs, CREATE_ABC(o, a, b, c), fs->ls->lastline); } int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) { lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx); + lua_assert(getCMode(o) == OpArgN); return luaK_code(fs, CREATE_ABx(o, a, bc), fs->ls->lastline); } diff --git a/ldebug.c b/ldebug.c index d42b161a..cae7e8d7 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 1.151 2003/04/28 13:31:06 roberto Exp roberto $ +** $Id: ldebug.c,v 1.152 2003/05/13 20:15:59 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -296,8 +296,16 @@ static int checkopenop (const Proto *pt, int pc) { } -static int checkRK (const Proto *pt, int r) { - return (r < pt->maxstacksize || (r >= MAXSTACK && r-MAXSTACK < pt->sizek)); +static int checkArgMode (const Proto *pt, int r, enum OpArgMask mode) { + switch (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)); + break; + } + return 1; } @@ -317,29 +325,28 @@ static Instruction luaG_symbexec (const Proto *pt, int lastpc, int reg) { case iABC: { b = GETARG_B(i); c = GETARG_C(i); - if (testOpMode(op, OpModeBreg)) { - checkreg(pt, b); - } - else if (testOpMode(op, OpModeBrk)) - check(checkRK(pt, b)); - if (testOpMode(op, OpModeCrk)) - check(checkRK(pt, c)); + check(checkArgMode(pt, b, getBMode(op))); + check(checkArgMode(pt, c, getCMode(op))); break; } case iABx: { b = GETARG_Bx(i); - if (testOpMode(op, OpModeK)) check(b < pt->sizek); + if (getBMode(op) == OpArgK) check(b < pt->sizek); break; } case iAsBx: { b = GETARG_sBx(i); + if (getBMode(op) == OpArgR) { + int dest = pc+1+b; + check(0 <= dest && dest < pt->sizecode); + } break; } } - if (testOpMode(op, OpModesetA)) { + if (testAMode(op)) { if (a == reg) last = pc; /* change register `a' */ } - if (testOpMode(op, OpModeT)) { + if (testTMode(op)) { check(pc+2 < pt->sizecode); /* check skip */ check(GET_OPCODE(pt->code[pc+1]) == OP_JMP); } @@ -369,21 +376,21 @@ static Instruction luaG_symbexec (const Proto *pt, int lastpc, int reg) { break; } case OP_CONCAT: { - /* `c' is a register, and at least two operands */ - check(c < MAXSTACK && b < c); + check(b < c); /* at least two operands */ break; } - case OP_TFORLOOP: - checkreg(pt, a+5); + case OP_TFORLOOP: { + checkreg(pt, a+5); /* space for control variables */ if (reg >= a) last = pc; /* affect all registers above base */ - /* go through */ + break; + } + case OP_TFORPREP: case OP_FORLOOP: case OP_FORPREP: - checkreg(pt, a+2); + checkreg(pt, a+3); /* go through */ case OP_JMP: { int dest = pc+1+b; - check(0 <= dest && dest < pt->sizecode); /* not full check and jump is forward and do not skip `lastpc'? */ if (reg != NO_REG && pc < dest && dest <= lastpc) pc += b; /* do the jump */ diff --git a/lopcodes.c b/lopcodes.c index b10e5bd1..e6a592d9 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -1,7 +1,5 @@ /* -** $Id: lopcodes.c,v 1.22 2002/12/04 17:38:31 roberto Exp roberto $ -** extracted automatically from lopcodes.h by mkprint.lua -** DO NOT EDIT +** $Id: lopcodes.c,v 1.23 2003/05/13 20:15:59 roberto Exp roberto $ ** See Copyright Notice in lua.h */ @@ -14,6 +12,8 @@ #include "lopcodes.h" +/* ORDER OP */ + #ifdef LUA_OPNAMES const char *const luaP_opnames[] = { @@ -57,48 +57,45 @@ const char *const luaP_opnames[] = { #endif -#define opmode(t,b,bk,ck,sa,k,m) (((t)<> 4) & 3)) +#define getCMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 2) & 3)) +#define testAMode(m) (luaP_opmodes[m] & (1 << 6)) +#define testTMode(m) (luaP_opmodes[m] & (1 << 7)) #ifdef LUA_OPNAMES diff --git a/lvm.c b/lvm.c index f5348881..1905b8d1 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 1.285 2003/05/05 18:39:57 roberto Exp roberto $ +** $Id: lvm.c,v 1.286 2003/05/13 20:15:59 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -361,11 +361,13 @@ static void Arith (lua_State *L, StkId ra, #define RA(i) (base+GETARG_A(i)) /* to be used after possible stack reallocation */ #define XRA(i) (L->base+GETARG_A(i)) -#define RB(i) (base+GETARG_B(i)) -#define RKB(i) ((GETARG_B(i) < MAXSTACK) ? RB(i) : k+GETARG_B(i)-MAXSTACK) -#define RC(i) (base+GETARG_C(i)) -#define RKC(i) ((GETARG_C(i) < MAXSTACK) ? RC(i) : k+GETARG_C(i)-MAXSTACK) -#define KBx(i) (k+GETARG_Bx(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 RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \ + (GETARG_B(i) < MAXSTACK) ? base+GETARG_B(i) : k+GETARG_B(i)-MAXSTACK) +#define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \ + (GETARG_C(i) < MAXSTACK) ? base+GETARG_C(i) : k+GETARG_C(i)-MAXSTACK) +#define KBx(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_Bx(i)) #define dojump(pc, i) ((pc) += (i))