add ravi operators for add, sub, mul and div

Dibyendu Majumdar 9 years ago
parent c9e3d67ca6
commit b84ed2a540

@ -112,6 +112,17 @@ enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */
#define GETARG_sBx(i) (GETARG_Bx(i)-MAXARG_sBx)
#define SETARG_sBx(i,b) SETARG_Bx((i),cast(unsigned int, (b)+MAXARG_sBx))
#define CREATE_ABC(o,a,b,c) ((cast(Instruction, o)<<POS_OP) \
| (cast(Instruction, a)<<POS_A) \
| (cast(Instruction, b)<<POS_B) \
| (cast(Instruction, c)<<POS_C))
#define CREATE_ABx(o,a,bc) ((cast(Instruction, o)<<POS_OP) \
| (cast(Instruction, a)<<POS_A) \
| (cast(Instruction, bc)<<POS_Bx))
#define CREATE_Ax(o,a) ((cast(Instruction, o)<<POS_OP) \
| (cast(Instruction, a)<<POS_Ax))
#define RAVI_GET_OPCODE(i) (cast(OpCode, ((i)>>SIZE_OP) & MASK1(10,0)))
#define RAVI_SET_OPCODE(i,o) ((i) = (((i)&MASK0(16,0)) | \
@ -126,25 +137,14 @@ enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */
#define RAVI_GETARG_C(i) getarg(i, 16, 16)
#define RAVI_SETARG_C(i,v) setarg(i, v, 16, 16)
/* A (16 bits) Opcode (10 bits) OP_RAVI (6 bits) */
#define RAVI_CREATE_A(o,a) ((cast(Instruction, a)<<16) \
| (cast(Instruction, o)<<6) \
| cast(Instruction, OP_RAVI))
#define CREATE_ABC(o,a,b,c) ((cast(Instruction, o)<<POS_OP) \
| (cast(Instruction, a)<<POS_A) \
| (cast(Instruction, b)<<POS_B) \
| (cast(Instruction, c)<<POS_C))
#define CREATE_ABx(o,a,bc) ((cast(Instruction, o)<<POS_OP) \
| (cast(Instruction, a)<<POS_A) \
| (cast(Instruction, bc)<<POS_Bx))
#define CREATE_Ax(o,a) ((cast(Instruction, o)<<POS_OP) \
| (cast(Instruction, a)<<POS_Ax))
#define RAVI_CREATE_A(o,a) ((cast(Instruction, a)<<16) \
| (cast(Instruction, o)))
#define RAVI_CREATE_BC(b,c) ((cast(Instruction, c)<<16) \
| (cast(Instruction, b)))
/* B (16 bits) C (16 bits) */
#define RAVI_CREATE_BC(b,c) ((cast(Instruction, c)<<16) \
| (cast(Instruction, b)))
/*

@ -1173,7 +1173,10 @@ newframe: /* reentry point when frame changes (call/return) */
case OP_EXTRAARG: {
lua_assert(0);
} break;
case OP_RAVI: {
#define OP(i) (i - OP_RAVI)
default: {
Instruction j = *(ci->u.l.savedpc++);
OpCode ravi_opcode = RAVI_GET_OPCODE(i);
@ -1185,13 +1188,344 @@ newframe: /* reentry point when frame changes (call/return) */
printf("Instruction %s\n", luaP_opnames[ravi_opcode]);
switch (ravi_opcode-OP_RAVI) {
case OP_RAVI_UNMF - OP_RAVI: {
switch (OP(ravi_opcode)) {
case OP(OP_RAVI_UNMF): {
/* R(A) = -R(B), R(B) must be a float */
TValue *rb = base+b;
lua_assert(ttisfloat(rb));
setfltvalue(ra, -fltvalue(rb));
} break;
case OP(OP_RAVI_UNMI): {
/* R(A) = -R(B), R(B) must be a float */
TValue *rb = base + b;
lua_assert(ttisinteger(rb));
setivalue(ra, -ivalue(rb));
} break;
case OP(OP_RAVI_ADDFFKK): {
TValue *rb = k + b;
TValue *rc = k + c;
setfltvalue(ra, fltvalue(rb) + fltvalue(rc));
} break;
case OP(OP_RAVI_ADDFFKR): {
TValue *rb = k + b;
TValue *rc = base + c;
setfltvalue(ra, fltvalue(rb) + fltvalue(rc));
} break;
case OP(OP_RAVI_ADDFFRK): {
TValue *rb = base + b;
TValue *rc = k + c;
setfltvalue(ra, fltvalue(rb) + fltvalue(rc));
} break;
case OP(OP_RAVI_ADDFFRR): {
TValue *rb = base + b;
TValue *rc = base + c;
setfltvalue(ra, fltvalue(rb) + fltvalue(rc));
} break;
case OP(OP_RAVI_ADDFIKK): {
TValue *rb = k + b;
TValue *rc = k + c;
setfltvalue(ra, fltvalue(rb) + ivalue(rc));
} break;
case OP(OP_RAVI_ADDFIKR): {
TValue *rb = k + b;
TValue *rc = base + c;
setfltvalue(ra, fltvalue(rb) + ivalue(rc));
} break;
case OP(OP_RAVI_ADDFIRK): {
TValue *rb = base + b;
TValue *rc = k + c;
setfltvalue(ra, fltvalue(rb) + ivalue(rc));
} break;
case OP(OP_RAVI_ADDFIRR): {
TValue *rb = base + b;
TValue *rc = base + c;
setfltvalue(ra, fltvalue(rb) + ivalue(rc));
} break;
case OP(OP_RAVI_ADDIFKK): {
TValue *rb = k + b;
TValue *rc = k + c;
setfltvalue(ra, ivalue(rb) + fltvalue(rc));
} break;
case OP(OP_RAVI_ADDIFKR): {
TValue *rb = k + b;
TValue *rc = base + c;
setfltvalue(ra, ivalue(rb) + fltvalue(rc));
} break;
case OP(OP_RAVI_ADDIFRK): {
TValue *rb = base + b;
TValue *rc = k + c;
setfltvalue(ra, ivalue(rb) + fltvalue(rc));
} break;
case OP(OP_RAVI_ADDIFRR): {
TValue *rb = base + b;
TValue *rc = base + c;
setfltvalue(ra, ivalue(rb) + fltvalue(rc));
} break;
case OP(OP_RAVI_ADDIIKK): {
TValue *rb = k + b;
TValue *rc = k + c;
setivalue(ra, ivalue(rb) + ivalue(rc));
} break;
case OP(OP_RAVI_ADDIIKR): {
TValue *rb = k + b;
TValue *rc = base + c;
setivalue(ra, ivalue(rb) + ivalue(rc));
} break;
case OP(OP_RAVI_ADDIIRK): {
TValue *rb = base + b;
TValue *rc = k + c;
setivalue(ra, ivalue(rb) + ivalue(rc));
} break;
case OP(OP_RAVI_ADDIIRR): {
TValue *rb = base + b;
TValue *rc = base + c;
setivalue(ra, ivalue(rb) + ivalue(rc));
} break;
case OP(OP_RAVI_SUBFFKK): {
TValue *rb = k + b;
TValue *rc = k + c;
setfltvalue(ra, fltvalue(rb) - fltvalue(rc));
} break;
case OP(OP_RAVI_SUBFFKR): {
TValue *rb = k + b;
TValue *rc = base + c;
setfltvalue(ra, fltvalue(rb) - fltvalue(rc));
} break;
case OP(OP_RAVI_SUBFFRK): {
TValue *rb = base + b;
TValue *rc = k + c;
setfltvalue(ra, fltvalue(rb) - fltvalue(rc));
} break;
case OP(OP_RAVI_SUBFFRR): {
TValue *rb = base + b;
TValue *rc = base + c;
setfltvalue(ra, fltvalue(rb) - fltvalue(rc));
} break;
case OP(OP_RAVI_SUBFIKK): {
TValue *rb = k + b;
TValue *rc = k + c;
setfltvalue(ra, fltvalue(rb) - ivalue(rc));
} break;
case OP(OP_RAVI_SUBFIKR): {
TValue *rb = k + b;
TValue *rc = base + c;
setfltvalue(ra, fltvalue(rb) - ivalue(rc));
} break;
case OP(OP_RAVI_SUBFIRK): {
TValue *rb = base + b;
TValue *rc = k + c;
setfltvalue(ra, fltvalue(rb) - ivalue(rc));
} break;
case OP(OP_RAVI_SUBFIRR): {
TValue *rb = base + b;
TValue *rc = base + c;
setfltvalue(ra, fltvalue(rb) - ivalue(rc));
} break;
case OP(OP_RAVI_SUBIFKK): {
TValue *rb = k + b;
TValue *rc = k + c;
setfltvalue(ra, ivalue(rb) - fltvalue(rc));
} break;
case OP(OP_RAVI_SUBIFKR): {
TValue *rb = k + b;
TValue *rc = base + c;
setfltvalue(ra, ivalue(rb) - fltvalue(rc));
} break;
case OP(OP_RAVI_SUBIFRK): {
TValue *rb = base + b;
TValue *rc = k + c;
setfltvalue(ra, ivalue(rb) - fltvalue(rc));
} break;
case OP(OP_RAVI_SUBIFRR): {
TValue *rb = base + b;
TValue *rc = base + c;
setfltvalue(ra, ivalue(rb) - fltvalue(rc));
} break;
case OP(OP_RAVI_SUBIIKK): {
TValue *rb = k + b;
TValue *rc = k + c;
setivalue(ra, ivalue(rb) - ivalue(rc));
} break;
case OP(OP_RAVI_SUBIIKR): {
TValue *rb = k + b;
TValue *rc = base + c;
setivalue(ra, ivalue(rb) - ivalue(rc));
} break;
case OP(OP_RAVI_SUBIIRK): {
TValue *rb = base + b;
TValue *rc = k + c;
setivalue(ra, ivalue(rb) - ivalue(rc));
} break;
case OP(OP_RAVI_SUBIIRR): {
TValue *rb = base + b;
TValue *rc = base + c;
setivalue(ra, ivalue(rb) - ivalue(rc));
} break;
case OP(OP_RAVI_MULFFKK): {
TValue *rb = k + b;
TValue *rc = k + c;
setfltvalue(ra, fltvalue(rb) * fltvalue(rc));
} break;
case OP(OP_RAVI_MULFFKR): {
TValue *rb = k + b;
TValue *rc = base + c;
setfltvalue(ra, fltvalue(rb) * fltvalue(rc));
} break;
case OP(OP_RAVI_MULFFRK): {
TValue *rb = base + b;
TValue *rc = k + c;
setfltvalue(ra, fltvalue(rb) * fltvalue(rc));
} break;
case OP(OP_RAVI_MULFFRR): {
TValue *rb = base + b;
TValue *rc = base + c;
setfltvalue(ra, fltvalue(rb) * fltvalue(rc));
} break;
case OP(OP_RAVI_MULFIKK): {
TValue *rb = k + b;
TValue *rc = k + c;
setfltvalue(ra, fltvalue(rb) * ivalue(rc));
} break;
case OP(OP_RAVI_MULFIKR): {
TValue *rb = k + b;
TValue *rc = base + c;
setfltvalue(ra, fltvalue(rb) * ivalue(rc));
} break;
case OP(OP_RAVI_MULFIRK): {
TValue *rb = base + b;
TValue *rc = k + c;
setfltvalue(ra, fltvalue(rb) * ivalue(rc));
} break;
case OP(OP_RAVI_MULFIRR): {
TValue *rb = base + b;
TValue *rc = base + c;
setfltvalue(ra, fltvalue(rb) * ivalue(rc));
} break;
case OP(OP_RAVI_MULIFKK): {
TValue *rb = k + b;
TValue *rc = k + c;
setfltvalue(ra, ivalue(rb) * fltvalue(rc));
} break;
case OP(OP_RAVI_MULIFKR): {
TValue *rb = k + b;
TValue *rc = base + c;
setfltvalue(ra, ivalue(rb) * fltvalue(rc));
} break;
case OP(OP_RAVI_MULIFRK): {
TValue *rb = base + b;
TValue *rc = k + c;
setfltvalue(ra, ivalue(rb) * fltvalue(rc));
} break;
case OP(OP_RAVI_MULIFRR): {
TValue *rb = base + b;
TValue *rc = base + c;
setfltvalue(ra, ivalue(rb) * fltvalue(rc));
} break;
case OP(OP_RAVI_MULIIKK): {
TValue *rb = k + b;
TValue *rc = k + c;
setivalue(ra, ivalue(rb) * ivalue(rc));
} break;
case OP(OP_RAVI_MULIIKR): {
TValue *rb = k + b;
TValue *rc = base + c;
setivalue(ra, ivalue(rb) * ivalue(rc));
} break;
case OP(OP_RAVI_MULIIRK): {
TValue *rb = base + b;
TValue *rc = k + c;
setivalue(ra, ivalue(rb) * ivalue(rc));
} break;
case OP(OP_RAVI_MULIIRR): {
TValue *rb = base + b;
TValue *rc = base + c;
setivalue(ra, ivalue(rb) * ivalue(rc));
} break;
case OP(OP_RAVI_DIVFFKK): {
TValue *rb = k + b;
TValue *rc = k + c;
setfltvalue(ra, fltvalue(rb) / fltvalue(rc));
} break;
case OP(OP_RAVI_DIVFFKR): {
TValue *rb = k + b;
TValue *rc = base + c;
setfltvalue(ra, fltvalue(rb) / fltvalue(rc));
} break;
case OP(OP_RAVI_DIVFFRK): {
TValue *rb = base + b;
TValue *rc = k + c;
setfltvalue(ra, fltvalue(rb) / fltvalue(rc));
} break;
case OP(OP_RAVI_DIVFFRR): {
TValue *rb = base + b;
TValue *rc = base + c;
setfltvalue(ra, fltvalue(rb) / fltvalue(rc));
} break;
case OP(OP_RAVI_DIVFIKK): {
TValue *rb = k + b;
TValue *rc = k + c;
setfltvalue(ra, fltvalue(rb) / ivalue(rc));
} break;
case OP(OP_RAVI_DIVFIKR): {
TValue *rb = k + b;
TValue *rc = base + c;
setfltvalue(ra, fltvalue(rb) / ivalue(rc));
} break;
case OP(OP_RAVI_DIVFIRK): {
TValue *rb = base + b;
TValue *rc = k + c;
setfltvalue(ra, fltvalue(rb) / ivalue(rc));
} break;
case OP(OP_RAVI_DIVFIRR): {
TValue *rb = base + b;
TValue *rc = base + c;
setfltvalue(ra, fltvalue(rb) / ivalue(rc));
} break;
case OP(OP_RAVI_DIVIFKK): {
TValue *rb = k + b;
TValue *rc = k + c;
setfltvalue(ra, ivalue(rb) / fltvalue(rc));
} break;
case OP(OP_RAVI_DIVIFKR): {
TValue *rb = k + b;
TValue *rc = base + c;
setfltvalue(ra, ivalue(rb) / fltvalue(rc));
} break;
case OP(OP_RAVI_DIVIFRK): {
TValue *rb = base + b;
TValue *rc = k + c;
setfltvalue(ra, ivalue(rb) / fltvalue(rc));
} break;
case OP(OP_RAVI_DIVIFRR): {
TValue *rb = base + b;
TValue *rc = base + c;
setfltvalue(ra, ivalue(rb) / fltvalue(rc));
} break;
case OP(OP_RAVI_DIVIIKK): {
TValue *rb = k + b;
TValue *rc = k + c;
setivalue(ra, ivalue(rb) / ivalue(rc));
} break;
case OP(OP_RAVI_DIVIIKR): {
TValue *rb = k + b;
TValue *rc = base + c;
setivalue(ra, ivalue(rb) / ivalue(rc));
} break;
case OP(OP_RAVI_DIVIIRK): {
TValue *rb = base + b;
TValue *rc = k + c;
setivalue(ra, ivalue(rb) / ivalue(rc));
} break;
case OP(OP_RAVI_DIVIIRR): {
TValue *rb = base + b;
TValue *rc = base + c;
setivalue(ra, ivalue(rb) / ivalue(rc));
} break;
}
} break;

@ -70,12 +70,67 @@ static void free_handle(LuaHandle *h)
free(h);
}
static int test_binintop(OpCode o, int p, int q, int expected)
{
LuaHandle *h = alloc_handle();
LuaHandle copyh = *h; /* save contents */
int rc = 0;
/* Here we simulate following
* local p = x
* local q = y
* return x+y
*
* So we need three registers
* p in register 0
* q in register 1
* return value must be register 2
*/
int A = 2;
int B = 1;
int C = 0;
int RETURN_ITEMS = 1;
/* register A (2) will hold return value */
h->instructions[0] = RAVI_CREATE_A(o, A);
/* register B (1) will hold q */
/* register C (0) will hold p */
h->instructions[1] = RAVI_CREATE_BC(B, C);
/* return register 2 (A) */
h->instructions[2] = CREATE_ABC(OP_RETURN, A, RETURN_ITEMS+1, 0);
/* set register 0 (p) */
setivalue(&h->stack[1], p);
/* set register 1 (q) */
setivalue(&h->stack[2], q);
h->p->maxstacksize = 3; /* need three registers */
h->ci->top = h->ci->u.l.base + h->p->maxstacksize;
h->L->top = h->ci->top;
h->ci->nresults = 1;
luaV_execute(h->L);
if (h->L->top != &h->L->stack[1])
rc = 1;
if (h->L->ci != NULL)
rc = 1;
if (!ttisinteger(&h->stack[0]) ||
ivalue(&h->stack[0]) != expected)
rc = 1;
*h = copyh; /* restore contents */
free_handle(h);
return rc;
}
static int test_unmf()
{
LuaHandle *h = alloc_handle();
LuaHandle copyh = *h; /* save contents */
int rc = 0;
printf("Number of opcodes %d\n", NUM_OPCODES);
/* Here we simulate following
* local p = 56.65
* local q = -p
@ -87,10 +142,9 @@ static int test_unmf()
* return value must be register 1
*/
h->instructions[0] = cast(Instruction, (1 << 16)) | (cast(Instruction,OP_RAVI_UNMF)<<6) | cast(Instruction, OP_RAVI);
/* R(1) = -R(0) */
/* register 1 holds q */
/* register 0 holds p */
/* register 1 holds q, so A = 1 */
/* register 0 holds p, so B = 0 */
h->instructions[0] = RAVI_CREATE_A(OP_RAVI_UNMF, 1);
h->instructions[1] = RAVI_CREATE_BC(0, 0);
/* return register 1 (q) */
h->instructions[2] = CREATE_ABC(OP_RETURN, 1, 2, 0);
@ -170,9 +224,12 @@ static int test_return0()
int main(const char *argv[])
{
int failures = 0;
// failures += test_return0();
// failures += test_return1();
failures += test_return0();
failures += test_return1();
failures += test_unmf();
failures += test_binintop(OP_RAVI_ADDIIRR, 6, 7, 13);
failures += test_binintop(OP_RAVI_MULIIRR, 6, 7, 42);
return failures ? 1 : 0;
}
Loading…
Cancel
Save