[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Adding an opcode
- From: Dibyendu Majumdar <mobile@...>
- Date: Fri, 2 Jan 2015 22:47:08 +0000
Hi,
I am interested in adding some opcodes to the VM. As I want to
increase the range of opcodes I have made following changes to
lopcodes.h:
#define SIZE_C 8
#define SIZE_B 8
#define SIZE_Bx (SIZE_C + SIZE_B)
#define SIZE_A 8
#define SIZE_Ax (SIZE_C + SIZE_B + SIZE_A)
#define SIZE_OP 8
I can't see any negative impact - but wanted to confirm if this is okay to do?
Also for writing some tests I am directly calling luaV_execute() as
follows. Seems to work fine. My aim here is to directly test certain
opcodes, and more importantly be able to step through the VM code in
the debugger without having to go through a lot of noise.
Of course any opcodes that require the rest of Lua infrastructure
won't work so I will extend this when necessary.
typedef struct {
lua_State *L;
CallInfo *ci;
Closure *cl;
TValue *stack;
Instruction *instructions;
Proto *p;
} LuaHandle;
static LuaHandle *alloc_handle()
{
LuaHandle *h = (LuaHandle *)calloc(1, sizeof(LuaHandle));
h->L = (lua_State *)calloc(1, sizeof(lua_State));
h->ci = (CallInfo *)calloc(10, sizeof(CallInfo));
h->cl = (Closure *)calloc(1, sizeof(Closure));
h->stack = (TValue *)calloc(10, sizeof(TValue));
h->instructions = (Instruction *)calloc(10, sizeof(Instruction));
h->p = (Proto *)calloc(1, sizeof(Proto));
h->L->stack = h->stack;
h->L->stacksize = 10;
h->L->stack_last = h->L->stack + 10 - 1;
h->p->code = &h->instructions[0];
h->cl->l.p = h->p; /* Dummy proto */
h->stack[0].value_.gc = (GCObject *)h->cl; /* Dummy function at
stack position 0 */
h->ci->u.l.base = &h->stack[1]; /* base for this function */
h->ci->func = &h->stack[0]; /* function index in the stack */
h->ci->top = &h->stack[1]; /* top for this function, 1 past the function */
h->ci->u.l.savedpc = h->cl->l.p->code;
h->L->ci = h->ci;
h->L->top = &h->stack[1];
return h;
}
static void free_handle(LuaHandle *h)
{
free(h->p);
free(h->instructions);
free(h->stack);
free(h->cl);
free(h->ci);
free(h->L);
free(h);
}
static int test_return1()
{
LuaHandle *h = alloc_handle();
LuaHandle copyh = *h; /* save contents */
int rc = 0;
/* just a return instruction - B = 1 means no return values */
h->instructions[0] = CREATE_ABC(OP_RETURN, 0, 2, 0);
h->stack[1].tt_ = LUA_TNUMFLT;
h->stack[1].value_.n = 56.65;
h->L->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 (h->stack[0].tt_ != LUA_TNUMFLT ||
h->stack[0].value_.n != 56.65)
rc = 1;
*h = copyh; /* restore contents */
free_handle(h);
return 0;
}