Skip to content

Commit

Permalink
Enhance wasm loader and update build app document (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
wenyongh authored Nov 27, 2019
1 parent ab15747 commit 1c81ad6
Show file tree
Hide file tree
Showing 7 changed files with 276 additions and 129 deletions.
37 changes: 19 additions & 18 deletions core/iwasm/lib/native/libc/libc_builtin_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,17 @@ _printf_hex_uint(out_func_t out, void *ctx,
enum pad_type padding,
int min_width)
{
int size = sizeof(num) * (is_u64 ? 2 : 1);
int shift = sizeof(num) * 8;
int found_largest_digit = 0;
int remaining = 8; /* 8 digits max */
int remaining = 16; /* 16 digits max */
int digits = 0;
char nibble;

for (; size; size--) {
char nibble = (num >> ((size - 1) << 2) & 0xf);
while (shift >= 4) {
shift -= 4;
nibble = (num >> shift) & 0xf;

if (nibble || found_largest_digit || size == 1) {
if (nibble || found_largest_digit || shift == 0) {
found_largest_digit = 1;
nibble = (char)(nibble + (nibble > 9 ? 87 : 48));
out((int) nibble, ctx);
Expand Down Expand Up @@ -1153,17 +1155,13 @@ __cxa_throw_wrapper(wasm_module_inst_t module_inst,
wasm_runtime_set_exception(module_inst, buf);
}

/*#define ENABLE_SPEC_TEST 1*/

#ifdef ENABLE_SPEC_TEST
static void
print_i32_wrapper(wasm_module_inst_t module_inst, int i32)
{
bh_printf("%d\n", i32);
}
#ifndef ENABLE_SPEC_TEST
#define ENABLE_SPEC_TEST 0
#endif

#if ENABLE_SPEC_TEST != 0
static void
print_wrapper(wasm_module_inst_t module_inst, int i32)
print_i32_wrapper(wasm_module_inst_t module_inst, int32 i32)
{
bh_printf("%d\n", i32);
}
Expand All @@ -1180,9 +1178,8 @@ typedef struct WASMNativeFuncDef {
} WASMNativeFuncDef;

static WASMNativeFuncDef native_func_defs[] = {
#ifdef ENABLE_SPEC_TEST
#if ENABLE_SPEC_TEST != 0
REG_NATIVE_FUNC(spectest, print_i32),
REG_NATIVE_FUNC(spectest, print),
#endif
REG_NATIVE_FUNC(env, _printf),
REG_NATIVE_FUNC(env, _sprintf),
Expand Down Expand Up @@ -1284,8 +1281,12 @@ typedef struct WASMNativeGlobalDef {
} WASMNativeGlobalDef;

static WASMNativeGlobalDef native_global_defs[] = {
#ifdef ENABLE_SPEC_TEST
{ "spectest", "global_i32", .global_data.u32 = 0 },
#if ENABLE_SPEC_TEST != 0
{ "spectest", "global_i32", .global_data.i32 = 666 },
{ "spectest", "global_f32", .global_data.f32 = 0 },
{ "spectest", "global_f64", .global_data.f64 = 0 },
{ "test", "global-i32", .global_data.i32 = 0 },
{ "test", "global-f32", .global_data.f32 = 0 },
#endif
{ "env", "STACKTOP", .global_data.u32 = 0 },
{ "env", "STACK_MAX", .global_data.u32 = 0 },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ __wasi_errno_t wasmtime_ssp_fd_pread(

struct fd_object *fo;
__wasi_errno_t error = fd_object_get(curfds,
&fo, fd, __WASI_RIGHT_FD_READ | __WASI_RIGHT_FD_SEEK, 0);
&fo, fd, __WASI_RIGHT_FD_READ, 0);
if (error != 0)
return error;

Expand Down Expand Up @@ -918,7 +918,7 @@ __wasi_errno_t wasmtime_ssp_fd_pwrite(

struct fd_object *fo;
__wasi_errno_t error = fd_object_get(curfds,
&fo, fd, __WASI_RIGHT_FD_WRITE | __WASI_RIGHT_FD_SEEK, 0);
&fo, fd, __WASI_RIGHT_FD_WRITE, 0);
if (error != 0)
return error;

Expand Down
5 changes: 0 additions & 5 deletions core/iwasm/runtime/vmcore-wasm/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ extern "C" {
#define BLOCK_TYPE_IF 2
#define BLOCK_TYPE_FUNCTION 3

#define CALL_TYPE_WRAPPER 0
#define CALL_TYPE_C_INTRINSIC 1

typedef union WASMValue {
int32 i32;
uint32 u32;
Expand Down Expand Up @@ -140,8 +137,6 @@ typedef struct WASMFunctionImport {
char *field_name;
/* function type */
WASMType *func_type;
/* c intrinsic function or wrapper function */
uint32 call_type;
/* function pointer after linked */
void *func_ptr_linked;
} WASMFunctionImport;
Expand Down
19 changes: 16 additions & 3 deletions core/iwasm/runtime/vmcore-wasm/wasm_interp.c
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,16 @@ wasm_interp_call_func_native(WASMThread *self,

wasm_thread_set_cur_frame (self, frame);

if (!cur_func->u.func_import->func_ptr_linked) {
char buf[128];
snprintf(buf,
sizeof(buf), "fail to call unlinked import function (%s, %s)",
cur_func->u.func_import->module_name,
cur_func->u.func_import->field_name);
wasm_runtime_set_exception(self->module_inst, buf);
return;
}

ret = wasm_runtime_invoke_native(cur_func->u.func_import->func_ptr_linked,
cur_func->u.func_import->func_type,
self->module_inst,
Expand Down Expand Up @@ -840,7 +850,7 @@ wasm_interp_call_func_bytecode(WASMThread *self,
HANDLE_OP (WASM_OP_CALL_INDIRECT):
{
WASMType *cur_type, *cur_func_type;
/* TODO: test */

read_leb_uint32(frame_ip, frame_ip_end, tidx);
if (tidx >= module->module->type_count) {
wasm_runtime_set_exception(module, "type index is overflow");
Expand All @@ -858,8 +868,11 @@ wasm_interp_call_func_bytecode(WASMThread *self,
}

fidx = ((uint32*)table->base_addr)[val];
/* Skip function index check, it has been checked
in wasm module instantiate */
if (fidx == (uint32)-1) {
wasm_runtime_set_exception(module, "uninitialized element");
goto got_exception;
}

cur_func = module->functions + fidx;

if (cur_func->is_import_func)
Expand Down
Loading

0 comments on commit 1c81ad6

Please sign in to comment.