implement compilation of type specific add

Dibyendu Majumdar 9 years ago
parent 559e955a14
commit c3a689e478

@ -55,4 +55,6 @@ LUAI_FUNC lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y);
LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y);
LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb);
LUAI_FUNC void stackDump(lua_State *L, const char *s);
#endif

@ -846,7 +846,37 @@ static void codeexpval (FuncState *fs, OpCode op,
freeexp(fs, e2);
freeexp(fs, e1);
}
e1->u.info = luaK_codeABC(fs, op, 0, o1, o2); /* generate opcode */
if (op == OP_ADD && e1->ravi_tt == LUA_TNUMFLT && e2->ravi_tt == LUA_TNUMFLT) {
if (ISK(o1) && ISK(o2)) {
e1->u.info = luaK_codeABC(fs, OP_RAVI_ADDFFKK, 0, o1, o2); /* generate opcode */
}
else if (ISK(o1)) {
e1->u.info = luaK_codeABC(fs, OP_RAVI_ADDFFKR, 0, o1, o2); /* generate opcode */
}
else if (ISK(o2)) {
e1->u.info = luaK_codeABC(fs, OP_RAVI_ADDFFRK, 0, o1, o2); /* generate opcode */
}
else {
e1->u.info = luaK_codeABC(fs, OP_RAVI_ADDFFRR, 0, o1, o2); /* generate opcode */
}
}
else if (op == OP_ADD && e1->ravi_tt == LUA_TNUMINT && e2->ravi_tt == LUA_TNUMINT) {
if (ISK(o1) && ISK(o2)) {
e1->u.info = luaK_codeABC(fs, OP_RAVI_ADDIIKK, 0, o1, o2); /* generate opcode */
}
else if (ISK(o1)) {
e1->u.info = luaK_codeABC(fs, OP_RAVI_ADDIIKR, 0, o1, o2); /* generate opcode */
}
else if (ISK(o2)) {
e1->u.info = luaK_codeABC(fs, OP_RAVI_ADDIIRK, 0, o1, o2); /* generate opcode */
}
else {
e1->u.info = luaK_codeABC(fs, OP_RAVI_ADDIIRR, 0, o1, o2); /* generate opcode */
}
}
else {
e1->u.info = luaK_codeABC(fs, op, 0, o1, o2); /* generate opcode */
}
e1->k = VRELOCABLE; /* all those operations are relocable */
if (isbinary) {
if (e1->ravi_tt == LUA_TNUMFLT && e2->ravi_tt == LUA_TNUMFLT)

@ -647,7 +647,7 @@ void luaV_finishOp (lua_State *L) {
#define vmcase(l,b) case l: {b} break;
#define vmcasenb(l,b) case l: {b} /* nb = no break */
static void stackDump(lua_State *L, const char *s) {
void stackDump(lua_State *L, const char *s) {
if (!s)
return;
StkId p = L->stack;
@ -1177,8 +1177,8 @@ newframe: /* reentry point when frame changes (call/return) */
#define OP(i) (i - OP_RAVI_UNMF)
default: {
int b = GETARG_B(i);
int c = GETARG_C(i);
int b = INDEXK(GETARG_B(i));
int c = INDEXK(GETARG_C(i));
switch (OP(op)) {
case OP(OP_RAVI_UNMF): {

@ -502,6 +502,35 @@ static int test_luacomp1(const char *code)
return rc;
}
/* test supplied lua code compiles */
static int test_luacompexec1(const char *code, int expected)
{
int rc = 0;
lua_State *L;
L = luaL_newstate();
if (luaL_loadbuffer(L, code, strlen(code), "testfunc") != 0) {
rc = 1;
fprintf(stderr, "%s\n", lua_tostring(L, -1));
lua_pop(L, 1); /* pop error message from the stack */
}
else {
DumpFunction(L);
if (lua_pcall(L, 0, 1, 0) != 0) {
rc = 1;
fprintf(stderr, "%s\n", lua_tostring(L, -1));
}
else {
stackDump(L, "after executing function");
if (!(lua_isinteger(L, -1) && lua_tointeger(L, -1) == expected)) {
rc = 1;
}
}
}
lua_close(L);
return rc;
}
int main(const char *argv[])
{
@ -515,6 +544,7 @@ int main(const char *argv[])
failures += test_luacomp1("local max, min = 0x7fffffff, -0x80000000; assert(string.format(\"%d\", min) == \"-2147483648\"); max, min = 0x7fffffffffffffff, -0x8000000000000000; if max > 2.0 ^ 53 then; end;");
failures += test_luacomp1("local function F (m); local function round(m); m = m + 0.04999; return format(\"%.1f\", m);end; end");
failures += test_luacomp1("local b:int = 6; local i:int = 5+b; return i");
failures += test_luacompexec1("local b:int = 6; local i:int = 5+b; return i", 11);
failures += test_luacomp1("local f = function(); end");
failures += test_luacomp1("local b:int = 6; b = nil; return i") == 1 ? 0 : 1; /* should fail */
failures += test_luacomp1("local f = function(); local function y() ; end; end");

Loading…
Cancel
Save