Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 39 additions & 7 deletions core/iwasm/aot/aot_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,12 @@ get_native_symbol_by_name(const char *name)
return func;
}

static bool
str2uint32(const char *buf, uint32 *p_res);

static bool
str2uint64(const char *buf, uint64 *p_res);

static bool
load_native_symbol_section(const uint8 *buf, const uint8 *buf_end,
AOTModule *module, bool is_load_from_file_buf,
Expand All @@ -487,11 +493,39 @@ load_native_symbol_section(const uint8 *buf, const uint8 *buf_end,

for (i = cnt - 1; i >= 0; i--) {
read_string(p, p_end, symbol);
module->native_symbol_list[i] = get_native_symbol_by_name(symbol);
if (module->native_symbol_list[i] == NULL) {
set_error_buf_v(error_buf, error_buf_size,
"missing native symbol: %s", symbol);
goto fail;
if (!strncmp(symbol, "f32#", 4)) {
uint32 u32;
/* Resolve the raw int bits of f32 const */
if (!str2uint32(symbol + 4, &u32)) {
set_error_buf_v(error_buf, error_buf_size,
"resolve symbol %s failed", symbol);
goto fail;
}
*(uint32 *)(&module->native_symbol_list[i]) = u32;
}
else if (!strncmp(symbol, "f64#", 4)) {
uint64 u64;
/* Resolve the raw int bits of f64 const */
if (!str2uint64(symbol + 4, &u64)) {
set_error_buf_v(error_buf, error_buf_size,
"resolve symbol %s failed", symbol);
goto fail;
}
*(uint64 *)(&module->native_symbol_list[i]) = u64;
}
else if (!strncmp(symbol, "__ignore", 8)) {
/* Padding bytes to make f64 on 8-byte aligned address,
or it is the second 32-bit slot in 32-bit system */
continue;
}
else {
module->native_symbol_list[i] =
get_native_symbol_by_name(symbol);
if (module->native_symbol_list[i] == NULL) {
set_error_buf_v(error_buf, error_buf_size,
"missing native symbol: %s", symbol);
goto fail;
}
}
}
}
Expand Down Expand Up @@ -1711,7 +1745,6 @@ is_literal_relocation(const char *reloc_sec_name)
return !strcmp(reloc_sec_name, ".rela.literal");
}

#if defined(BH_PLATFORM_WINDOWS)
static bool
str2uint32(const char *buf, uint32 *p_res)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the difference between str2uint32 to the strtol from libc ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lum1n0us Seems the result of str2uint32 is a raw hex rather than an integer

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is raw int bits of f32 and f64.

{
Expand Down Expand Up @@ -1757,7 +1790,6 @@ str2uint64(const char *buf, uint64 *p_res)
*p_res = res;
return true;
}
#endif

static bool
do_text_relocation(AOTModule *module, AOTRelocationGroup *group,
Expand Down
2 changes: 1 addition & 1 deletion core/iwasm/compilation/aot.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ typedef struct AOTCompData {

typedef struct AOTNativeSymbol {
bh_list_link link;
const char *symbol;
char symbol[32];
int32 index;
} AOTNativeSymbol;

Expand Down
36 changes: 30 additions & 6 deletions core/iwasm/compilation/aot_emit_const.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,21 @@ aot_compile_op_f32_const(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
LLVMValueRef alloca, value;

if (!isnan(f32_const)) {
value = F32_CONST(f32_const);
CHECK_LLVM_CONST(value);
PUSH_F32(value);
if (!comp_ctx->is_indirect_mode) {
value = F32_CONST(f32_const);
CHECK_LLVM_CONST(value);
PUSH_F32(value);
}
else {
WASMValue wasm_value;
memcpy(&wasm_value.f32, &f32_const, sizeof(float32));
value = aot_load_const_from_table(comp_ctx, func_ctx->native_symbol,
&wasm_value, VALUE_TYPE_F32);
if (!value) {
return false;
}
PUSH_F32(value);
}
}
else {
int32 i32_const;
Expand Down Expand Up @@ -77,9 +89,21 @@ aot_compile_op_f64_const(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
LLVMValueRef alloca, value;

if (!isnan(f64_const)) {
value = F64_CONST(f64_const);
CHECK_LLVM_CONST(value);
PUSH_F64(value);
if (!comp_ctx->is_indirect_mode) {
value = F64_CONST(f64_const);
CHECK_LLVM_CONST(value);
PUSH_F64(value);
}
else {
WASMValue wasm_value;
memcpy(&wasm_value.f64, &f64_const, sizeof(float64));
value = aot_load_const_from_table(comp_ctx, func_ctx->native_symbol,
&wasm_value, VALUE_TYPE_F64);
if (!value) {
return false;
}
PUSH_F64(value);
}
}
else {
int64 i64_const;
Expand Down
132 changes: 112 additions & 20 deletions core/iwasm/compilation/aot_emit_conversion.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,14 +348,37 @@ aot_compile_op_i32_trunc_f32(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,

POP_F32(value);

if (sign) {
min_value = F32_CONST(-2147483904.0f);
max_value = F32_CONST(2147483648.0f);
if (!comp_ctx->is_indirect_mode) {
if (sign) {
min_value = F32_CONST(-2147483904.0f);
max_value = F32_CONST(2147483648.0f);
}
else {
min_value = F32_CONST(-1.0f);
max_value = F32_CONST(4294967296.0f);
}
}
else {
min_value = F32_CONST(-1.0f);
max_value = F32_CONST(4294967296.0f);
WASMValue wasm_value;
if (sign) {
wasm_value.f32 = -2147483904.0f;
min_value = aot_load_const_from_table(
comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F32);
wasm_value.f32 = 2147483648.0f;
max_value = aot_load_const_from_table(
comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F32);
}
else {
wasm_value.f32 = -1.0f;
min_value = aot_load_const_from_table(
comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F32);
wasm_value.f32 = 4294967296.0f;
max_value = aot_load_const_from_table(
comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F32);
}
}
CHECK_LLVM_CONST(min_value);
CHECK_LLVM_CONST(max_value);

if (!saturating)
return trunc_float_to_int(
Expand All @@ -378,14 +401,37 @@ aot_compile_op_i32_trunc_f64(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,

POP_F64(value);

if (sign) {
min_value = F64_CONST(-2147483649.0);
max_value = F64_CONST(2147483648.0);
if (!comp_ctx->is_indirect_mode) {
if (sign) {
min_value = F64_CONST(-2147483649.0);
max_value = F64_CONST(2147483648.0);
}
else {
min_value = F64_CONST(-1.0);
max_value = F64_CONST(4294967296.0);
}
}
else {
min_value = F64_CONST(-1.0);
max_value = F64_CONST(4294967296.0);
WASMValue wasm_value;
if (sign) {
wasm_value.f64 = -2147483649.0;
min_value = aot_load_const_from_table(
comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F64);
wasm_value.f64 = 2147483648.0;
max_value = aot_load_const_from_table(
comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F64);
}
else {
wasm_value.f64 = -1.0;
min_value = aot_load_const_from_table(
comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F64);
wasm_value.f64 = 4294967296.0;
max_value = aot_load_const_from_table(
comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F64);
}
}
CHECK_LLVM_CONST(min_value);
CHECK_LLVM_CONST(max_value);

if (!saturating)
return trunc_float_to_int(
Expand Down Expand Up @@ -509,14 +555,37 @@ aot_compile_op_i64_trunc_f32(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,

POP_F32(value);

if (sign) {
min_value = F32_CONST(-9223373136366403584.0f);
max_value = F32_CONST(9223372036854775808.0f);
if (!comp_ctx->is_indirect_mode) {
if (sign) {
min_value = F32_CONST(-9223373136366403584.0f);
max_value = F32_CONST(9223372036854775808.0f);
}
else {
min_value = F32_CONST(-1.0f);
max_value = F32_CONST(18446744073709551616.0f);
}
}
else {
min_value = F32_CONST(-1.0f);
max_value = F32_CONST(18446744073709551616.0f);
WASMValue wasm_value;
if (sign) {
wasm_value.f32 = -9223373136366403584.0f;
min_value = aot_load_const_from_table(
comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F32);
wasm_value.f32 = 9223372036854775808.0f;
max_value = aot_load_const_from_table(
comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F32);
}
else {
wasm_value.f32 = -1.0f;
min_value = aot_load_const_from_table(
comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F32);
wasm_value.f32 = 18446744073709551616.0f;
max_value = aot_load_const_from_table(
comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F32);
}
}
CHECK_LLVM_CONST(min_value);
CHECK_LLVM_CONST(max_value);

if (!saturating)
return trunc_float_to_int(
Expand All @@ -539,14 +608,37 @@ aot_compile_op_i64_trunc_f64(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,

POP_F64(value);

if (sign) {
min_value = F64_CONST(-9223372036854777856.0);
max_value = F64_CONST(9223372036854775808.0);
if (!comp_ctx->is_indirect_mode) {
if (sign) {
min_value = F64_CONST(-9223372036854777856.0);
max_value = F64_CONST(9223372036854775808.0);
}
else {
min_value = F64_CONST(-1.0);
max_value = F64_CONST(18446744073709551616.0);
}
}
else {
min_value = F64_CONST(-1.0);
max_value = F64_CONST(18446744073709551616.0);
WASMValue wasm_value;
if (sign) {
wasm_value.f64 = -9223372036854777856.0;
min_value = aot_load_const_from_table(
comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F64);
wasm_value.f64 = 9223372036854775808.0;
max_value = aot_load_const_from_table(
comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F64);
}
else {
wasm_value.f64 = -1.0;
min_value = aot_load_const_from_table(
comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F64);
wasm_value.f64 = 18446744073709551616.0;
max_value = aot_load_const_from_table(
comp_ctx, func_ctx->native_symbol, &wasm_value, VALUE_TYPE_F64);
}
}
CHECK_LLVM_CONST(min_value);
CHECK_LLVM_CONST(max_value);

if (!saturating)
return trunc_float_to_int(
Expand Down
Loading