issue #89 add support for 32-bit numbers

gccjit-ravi534
Dibyendu Majumdar 8 years ago
parent efca456fd6
commit e91538b54b

@ -33,7 +33,7 @@
** ensure that all software connected to Lua will be compiled with the
** same configuration.
*/
/* #define LUA_32BITS */
#define LUA_32BITS
/*

@ -105,6 +105,7 @@ struct LuaLLVMTypes {
// Must ensure that these types match
// between JIT and the C compiler
llvm::Type *C_voidT;
llvm::Type *C_floatT;
llvm::Type *C_doubleT;
llvm::Type *C_intptr_t;
llvm::Type *C_size_t;

@ -332,7 +332,7 @@ print("test 2 OK")
-- test 3
x = function ()
local j:number
for i=1,1000000000 do
for i=1,10000000 do
j = j+1
end
return j
@ -341,7 +341,7 @@ check(x, 'LOADNIL', 'LOADFZ', 'LOADK', 'LOADK',
'LOADK', 'FORPREP_I1', 'ADDFI', 'FORLOOP_I1',
'RETURN', 'RETURN')
compile(x)
assert(x() == 1000000000.0)
assert(x() == 10000000.0)
print("test 3 OK")
-- test 4
@ -513,12 +513,12 @@ function x(f)
local i:number, j:number = f()
return i+j
end
assert(x(y) == 11.3)
assert(math.abs(x(y) - 11.3) < 0.0001)
check(x, 'MOVE', 'CALL', 'TOFLT',
'TOFLT', 'ADDFF', 'RETURN', 'RETURN')
assert(compile(y))
assert(compile(x))
assert(x(y) == 11.3)
assert(math.abs(x(y) - 11.3) < 0.0001)
print("test 13 OK")
-- test 16
@ -848,9 +848,9 @@ function testsub(a,b)
end
assert(compile(testsub))
assert(testsub(1,1) == 0)
assert(math.abs(testsub(1.5,1.6)-(-0.1)) < 1e-12)
assert(math.abs(testsub("1.5",1.6)-(-0.1)) < 1e-12)
assert(math.abs(testsub("1.5","1.6")-(-0.1)) < 1e-12)
assert(math.abs(testsub(1.5,1.6)-(-0.1)) < 1e-6)
assert(math.abs(testsub("1.5",1.6)-(-0.1)) < 1e-6)
assert(math.abs(testsub("1.5","1.6")-(-0.1)) < 1e-6)
print("test 33 OK")
-- test 25
@ -976,8 +976,10 @@ x=function(a,b)
return a%b
end
assert(compile(x))
-- When using 32-bit numbers we don't jit mod
-- if ravi.jit() then ravi.compile(x) end
assert(x(5,2) == 1)
assert(math.abs(x(5.1,2.1)-0.9) < 1e-12)
assert(math.abs(x(5.1,2.1)-0.9) < 1e-6)
print("test 41 OK")
-- test 33
@ -985,8 +987,10 @@ x=function(a,b)
return a//b
end
assert(compile(x))
-- When using 32-bit numbers we don't jit idiv
-- if ravi.jit() then ravi.compile(x) end
assert(x(5,2) == 2)
assert(math.abs(x(5.5,2.1)-2.0) < 1e-12)
assert(math.abs(x(5.5,2.1)-2.0) < 1e-6)
print("test 42 OK")
-- test 6

@ -201,7 +201,7 @@ function x()
return a,b
end
y,z = x()
assert(y == 61 and math.abs(z - 19.9) < 1e-10)
assert(y == 61 and math.abs(z - 19.9) < 1e-5)
compile(x)
y,z = x()
assert(y == 61 and math.abs(z - 19.9) < 1e-10)

@ -853,7 +853,7 @@ LUA_API int ravi_is_integer_array(lua_State *L, int idx) {
LUA_API lua_Number* ravi_get_number_array_rawdata(lua_State *L, int idx) {
StkId o = index2addr(L, idx);
lua_assert(ttistable(o) && hvalue(o)->ravi_array.array_type == RAVI_TARRAYFLT);
double *startp, *endp;
lua_Number *startp, *endp;
raviH_get_number_array_rawdata(L, hvalue(o), &startp, &endp);
return startp;
}

@ -117,7 +117,7 @@ static void print_expdesc(FILE *fp, FuncState *fs, const expdesc *e) {
raviY_typename(e->ravi_type));
break;
case VKINT:
fprintf(fp, "{p=%p, k=VKINT, n=%lld, type=%s}", e, e->u.ival,
fprintf(fp, "{p=%p, k=VKINT, n=%lld, type=%s}", e, (long long)e->u.ival,
raviY_typename(e->ravi_type));
break;
case VNONRELOC:

@ -1948,7 +1948,7 @@ void ravi_dump_value(lua_State *L, const TValue *stack_ptr) {
else if (ttisfloat(stack_ptr))
printf("%.6f\n", fltvalue(stack_ptr));
else if (ttisinteger(stack_ptr))
printf("%lld\n", ivalue(stack_ptr));
printf("%lld\n", (long long)ivalue(stack_ptr));
else if (ttislightuserdata(stack_ptr))
printf("light user data\n");
else if (ttisfulluserdata(stack_ptr))
@ -2006,7 +2006,7 @@ static void ravi_dump_ci(lua_State *L, CallInfo *ci) {
else if (ttisfloat(stack_ptr))
printf("%.6f\n", fltvalue(stack_ptr));
else if (ttisinteger(stack_ptr))
printf("%lld\n", ivalue(stack_ptr));
printf("%lld\n", (long long)ivalue(stack_ptr));
else if (ttislightuserdata(stack_ptr))
printf("light user data\n");
else if (ttisfulluserdata(stack_ptr))

@ -29,6 +29,10 @@ bool ravi_setup_lua_types(ravi_gcc_context_t *ravi) {
if (!ravi->types)
return false;
#ifdef LUA_32BITS
#error 32-bit number is not supported for gcc jit
#endif
ravi_gcc_types_t *t = ravi->types;
t->C_boolT = gcc_jit_context_get_type(ravi->context, GCC_JIT_TYPE_BOOL);

@ -376,7 +376,7 @@ void RaviCodeGenerator::emit_MOD(RaviFunctionDef *def, int A, int B, int C,
// If m*b < 0
cmp1 = def->builder->CreateFCmpOLT(
mb, llvm::ConstantFP::get(def->types->C_doubleT, 0.0));
mb, llvm::ConstantFP::get(def->types->lua_NumberT, 0.0));
llvm::BasicBlock *mb_lt0_then =
llvm::BasicBlock::Create(def->jitState->context(), "mb.lt.zero", def->f);
llvm::BasicBlock *mb_lt0_else =

@ -868,8 +868,8 @@ bool RaviCodeGenerator::canCompile(Proto *p) {
case OP_DIV:
case OP_MOD:
case OP_IDIV:
case OP_UNM:
case OP_POW:
case OP_UNM:
case OP_LEN:
case OP_VARARG:
case OP_CONCAT:
@ -1186,22 +1186,34 @@ void RaviCodeGenerator::emit_extern_declarations(RaviFunctionDef *def) {
// stdc fmod declaration
args.clear();
args.push_back(def->types->C_doubleT);
args.push_back(def->types->C_doubleT);
args.push_back(def->types->lua_NumberT);
args.push_back(def->types->lua_NumberT);
llvm::FunctionType *fmodType =
llvm::FunctionType::get(def->types->C_doubleT, args, false);
llvm::FunctionType::get(def->types->lua_NumberT, args, false);
#ifdef LUA_32BITS
def->fmodFunc = def->raviF->module()->getOrInsertFunction("fmodf", fmodType);
#else
def->fmodFunc = def->raviF->module()->getOrInsertFunction("fmod", fmodType);
#endif
llvm::FunctionType *powType =
llvm::FunctionType::get(def->types->C_doubleT, args, false);
llvm::FunctionType::get(def->types->lua_NumberT, args, false);
#ifdef LUA_32BITS
def->powFunc = def->raviF->module()->getOrInsertFunction("powf", powType);
#else
def->powFunc = def->raviF->module()->getOrInsertFunction("pow", powType);
#endif
// stdc floor declaration
args.clear();
args.push_back(def->types->C_doubleT);
args.push_back(def->types->lua_NumberT);
llvm::FunctionType *floorType =
llvm::FunctionType::get(def->types->C_doubleT, args, false);
llvm::FunctionType::get(def->types->lua_NumberT, args, false);
#ifdef LUA_32BITS
def->floorFunc =
def->raviF->module()->getOrInsertFunction("floorf", floorType);
#else
def->floorFunc =
def->raviF->module()->getOrInsertFunction("floor", floorType);
#endif
}
#define RA(i) (base + GETARG_A(i))

@ -28,17 +28,31 @@ LuaLLVMTypes::LuaLLVMTypes(llvm::LLVMContext &context) : mdbuilder(context) {
C_voidT = llvm::Type::getVoidTy(context);
static_assert(std::is_floating_point<lua_Number>::value &&
sizeof(lua_Number) == sizeof(double),
"lua_Number is not a double");
C_floatT = llvm::Type::getFloatTy(context);
C_doubleT = llvm::Type::getDoubleTy(context);
#ifdef LUA_32BITS
// issue #93 add support for 32-bit Lua
lua_NumberT = C_floatT;
static_assert(std::is_floating_point<lua_Number>::value &&
sizeof(lua_Number) == sizeof(float),
"lua_Number is not a float");
static_assert(sizeof(lua_Integer) == sizeof(lua_Number) &&
sizeof(lua_Integer) == sizeof(int32_t),
"lua_Integer size (32-bit) does not match lua_Number");
#else
lua_NumberT = C_doubleT;
static_assert(std::is_floating_point<lua_Number>::value &&
sizeof(lua_Number) == sizeof(double),
"lua_Number is not a double");
static_assert(sizeof(lua_Integer) == sizeof(lua_Number) &&
sizeof(lua_Integer) == sizeof(int64_t),
"lua_Integer size (64-bit) does not match lua_Number");
#endif
plua_NumberT = llvm::PointerType::get(lua_NumberT, 0);
pplua_NumberT = llvm::PointerType::get(plua_NumberT, 0);
static_assert(sizeof(lua_Integer) == sizeof(lua_Number) &&
sizeof(lua_Integer) == sizeof(int64_t),
"Only 64-bit int supported");
static_assert(std::is_integral<lua_Integer>::value,
"lua_Integer is not an integer type");
lua_IntegerT = llvm::Type::getIntNTy(context, sizeof(lua_Integer) * 8);
@ -69,7 +83,7 @@ LuaLLVMTypes::LuaLLVMTypes(llvm::LLVMContext &context) : mdbuilder(context) {
tmsT = C_intT;
static_assert(sizeof(L_Umaxalign) == sizeof(double),
"L_Umaxalign is not same size as double");
"L_Umaxalign is not same size as double");
L_UmaxalignT = C_doubleT;
static_assert(sizeof(lu_byte) == sizeof(char),

Loading…
Cancel
Save