Skip to content

Commit

Permalink
Merge pull request #4 from no1wudi/dev
Browse files Browse the repository at this point in the history
Prepare essential utils for wasm compiler
  • Loading branch information
wenyongh authored Jan 15, 2021
2 parents cae61bf + 146e0a7 commit dca19db
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 51 deletions.
3 changes: 2 additions & 1 deletion core/iwasm/aot/aot_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ load_native_symbol_section(const uint8 *buf,
{
const uint8 *p = buf, *p_end = buf_end;
uint32 cnt;
int32 i;
const char *symbol;

read_uint32(p, p_end, cnt);
Expand All @@ -405,7 +406,7 @@ load_native_symbol_section(const uint8 *buf,
goto fail;
}

for (uint32 i = 0; i < cnt; i++) {
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) {
Expand Down
1 change: 1 addition & 0 deletions core/iwasm/compilation/aot_compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ typedef enum FloatArithmetic {
#define I32_TWO (comp_ctx->llvm_consts.i32_two)
#define I32_THREE (comp_ctx->llvm_consts.i32_three)
#define I32_FOUR (comp_ctx->llvm_consts.i32_four)
#define I32_FIVE (comp_ctx->llvm_consts.i32_five)
#define I32_EIGHT (comp_ctx->llvm_consts.i32_eight)
#define I32_NEG_ONE (comp_ctx->llvm_consts.i32_neg_one)
#define I64_NEG_ONE (comp_ctx->llvm_consts.i64_neg_one)
Expand Down
2 changes: 2 additions & 0 deletions core/iwasm/compilation/aot_emit_aot_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,7 @@ get_native_symbol_list_size(AOTCompContext *comp_ctx)
sym = bh_list_first_elem(&comp_ctx->native_symbols);

while (sym) {
len = align_uint(len, 2);
len += get_string_size(sym->symbol);
sym = bh_list_elem_next(sym);
}
Expand Down Expand Up @@ -1474,6 +1475,7 @@ aot_emit_native_symbol(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
sym = bh_list_first_elem(&comp_ctx->native_symbols);

while (sym) {
offset = align_uint(offset, 2);
EMIT_STR(sym->symbol);
sym = bh_list_elem_next(sym);
}
Expand Down
18 changes: 18 additions & 0 deletions core/iwasm/compilation/aot_emit_exception.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@ aot_emit_exception(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
return false;
}
}
else if (comp_ctx->is_indirect_mode) {
int32 func_index;
if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
aot_set_last_error("create LLVM function type failed.");
return false;
}

func_index = aot_get_native_symbol_index(
comp_ctx, "aot_set_exception_with_id");
if (func_index < 0) {
return false;
}
if (!(func =
aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
func_ptr_type, func_index))) {
return false;
}
}
else {
/* Create LLVM function with external function pointer */
if (!(func = LLVMGetNamedFunction(comp_ctx->module,
Expand Down
36 changes: 34 additions & 2 deletions core/iwasm/compilation/aot_emit_function.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,26 @@ call_aot_invoke_native_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
return false;
}
}
else if (comp_ctx->is_indirect_mode) {
int32 func_index;
if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
aot_set_last_error("create LLVM function type failed.");
return false;
}
func_index =
aot_get_native_symbol_index(comp_ctx, func_name);
if (func_index < 0) {
return false;
}
if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
func_ptr_type, func_index))) {
return false;
}
}
else {
if (!(func = LLVMGetNamedFunction(comp_ctx->module, func_name))
&& !(func = LLVMAddFunction(comp_ctx->module,
func_name, func_type))) {
&& !(func =
LLVMAddFunction(comp_ctx->module, func_name, func_type))) {
aot_set_last_error("add LLVM function failed.");
return false;
}
Expand Down Expand Up @@ -534,6 +550,22 @@ call_aot_call_indirect_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
return false;
}
}
else if (comp_ctx->is_indirect_mode) {
int32 func_index;
if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
aot_set_last_error("create LLVM function type failed.");
return false;
}
func_index =
aot_get_native_symbol_index(comp_ctx, func_name);
if (func_index < 0) {
return false;
}
if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
func_ptr_type, func_index))) {
return false;
}
}
else {
if (!(func = LLVMGetNamedFunction(comp_ctx->module, func_name))
&& !(func = LLVMAddFunction(comp_ctx->module,
Expand Down
91 changes: 62 additions & 29 deletions core/iwasm/compilation/aot_emit_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,7 @@ aot_compile_op_memory_grow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
LLVMValueRef mem_size = get_memory_size(comp_ctx, func_ctx);
LLVMValueRef delta, param_values[2], ret_value, func, value;
LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
int32 func_index;

if (!mem_size)
return false;
Expand Down Expand Up @@ -679,6 +680,21 @@ aot_compile_op_memory_grow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
return false;
}
}
else if (comp_ctx->is_indirect_mode) {
if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
aot_set_last_error("create LLVM function type failed.");
return false;
}
func_index =
aot_get_native_symbol_index(comp_ctx, "wasm_runtime_enlarge_memory");
if (func_index < 0) {
return false;
}
if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
func_ptr_type, func_index))) {
return false;
}
}
else {
char *func_name = "wasm_runtime_enlarge_memory";
/* AOT mode, delcare the function */
Expand Down Expand Up @@ -715,35 +731,52 @@ aot_compile_op_memory_grow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
return false;
}

#define GET_AOT_FUNCTION(name, argc) do { \
if (!(func_type = LLVMFunctionType(ret_type, param_types, \
argc, false))) { \
aot_set_last_error("llvm add function type failed."); \
return false; \
} \
if (comp_ctx->is_jit_mode) { \
/* JIT mode, call the function directly */ \
if (!(func_ptr_type = LLVMPointerType(func_type, 0))) { \
aot_set_last_error("llvm add pointer type failed."); \
return false; \
} \
if (!(value = I64_CONST((uint64)(uintptr_t)name)) \
|| !(func = LLVMConstIntToPtr(value, func_ptr_type))) { \
aot_set_last_error("create LLVM value failed."); \
return false; \
} \
} \
else { \
char *func_name = #name; \
/* AOT mode, delcare the function */ \
if (!(func = LLVMGetNamedFunction(comp_ctx->module, func_name)) \
&& !(func = LLVMAddFunction(comp_ctx->module, \
func_name, func_type))) { \
aot_set_last_error("llvm add function failed."); \
return false; \
} \
} \
} while (0)
#define GET_AOT_FUNCTION(name, argc) \
do { \
if (!(func_type = \
LLVMFunctionType(ret_type, param_types, argc, false))) { \
aot_set_last_error("llvm add function type failed."); \
return false; \
} \
if (comp_ctx->is_jit_mode) { \
/* JIT mode, call the function directly */ \
if (!(func_ptr_type = LLVMPointerType(func_type, 0))) { \
aot_set_last_error("llvm add pointer type failed."); \
return false; \
} \
if (!(value = I64_CONST((uint64)(uintptr_t)name)) \
|| !(func = LLVMConstIntToPtr(value, func_ptr_type))) { \
aot_set_last_error("create LLVM value failed."); \
return false; \
} \
} \
else if (comp_ctx->is_indirect_mode) { \
int32 func_index; \
if (!(func_ptr_type = LLVMPointerType(func_type, 0))) { \
aot_set_last_error("create LLVM function type failed."); \
return false; \
} \
func_index = aot_get_native_symbol_index(comp_ctx, #name); \
if (func_index < 0) { \
return false; \
} \
if (!(func = aot_get_func_from_table( \
comp_ctx, func_ctx->native_symbol, func_ptr_type, \
func_index))) { \
return false; \
} \
} \
else { \
char *func_name = #name; \
/* AOT mode, delcare the function */ \
if (!(func = LLVMGetNamedFunction(comp_ctx->module, func_name)) \
&& !(func = LLVMAddFunction(comp_ctx->module, func_name, \
func_type))) { \
aot_set_last_error("llvm add function failed."); \
return false; \
} \
} \
} while (0)

#if WASM_ENABLE_BULK_MEMORY != 0

Expand Down
105 changes: 87 additions & 18 deletions core/iwasm/compilation/aot_llvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -596,9 +596,11 @@ aot_create_func_context(AOTCompData *comp_data, AOTCompContext *comp_ctx,
AOTFuncType *aot_func_type = comp_data->func_types[func->func_type_index];
AOTBlock *aot_block;
LLVMTypeRef int8_ptr_type, int32_ptr_type;
LLVMTypeRef void_ptr_type;
LLVMValueRef aot_inst_offset = I32_TWO, aot_inst_addr;
LLVMValueRef argv_buf_offset = I32_THREE, argv_buf_addr;
LLVMValueRef stack_bound_offset = I32_FOUR, stack_bound_addr;
LLVMValueRef native_symbol_offset = I32_FIVE, native_symbol_addr;
char local_name[32];
uint64 size;
uint32 i, j = 0;
Expand Down Expand Up @@ -689,6 +691,29 @@ aot_create_func_context(AOTCompData *comp_data, AOTCompContext *comp_ctx,
goto fail;
}

if (!(native_symbol_addr =
LLVMBuildInBoundsGEP(comp_ctx->builder, func_ctx->exec_env,
&native_symbol_offset, 1, "native_symbol_addr"))) {
aot_set_last_error("llvm build in bounds gep failed");
goto fail;
}

/* Type void ** */
if (!(void_ptr_type = LLVMPointerType(LLVMVoidType(), 0))
|| !(void_ptr_type = LLVMPointerType(void_ptr_type, 0))) {
aot_set_last_error("construct void** ptr failed.");
goto fail;
}

func_ctx->native_symbol =
LLVMBuildLoad2(comp_ctx->builder, void_ptr_type, native_symbol_addr,
"load_native_symbol");

if (func_ctx->native_symbol == NULL) {
aot_set_last_error("llvm build load failed");
goto fail;
}

for (i = 0; i < aot_func_type->param_count; i++, j++) {
snprintf(local_name, sizeof(local_name), "l%d", i);
func_ctx->locals[i] =
Expand Down Expand Up @@ -924,6 +949,7 @@ aot_create_llvm_consts(AOTLLVMConsts *consts, AOTCompContext *comp_ctx)
consts->i32_two = I32_CONST(2);
consts->i32_three = I32_CONST(3);
consts->i32_four = I32_CONST(4);
consts->i32_five = I32_CONST(5);
consts->i32_eight = I32_CONST(8);
consts->i32_neg_one = I32_CONST((uint32)-1);
consts->i64_neg_one = I64_CONST((uint64)-1);
Expand All @@ -949,6 +975,7 @@ aot_create_llvm_consts(AOTLLVMConsts *consts, AOTCompContext *comp_ctx)
&& consts->i32_two
&& consts->i32_three
&& consts->i32_four
&& consts->i32_five
&& consts->i32_eight
&& consts->i32_neg_one
&& consts->i64_neg_one
Expand Down Expand Up @@ -1542,45 +1569,42 @@ aot_destroy_comp_context(AOTCompContext *comp_ctx)
}

int32
aot_comp_get_native_symbol_index(AOTCompContext *comp_ctx, const char *symbol)
aot_get_native_symbol_index(AOTCompContext *comp_ctx, const char *symbol)
{
int32 idx = -1;
int32 cnt = 0;
AOTNativeSymbol *sym = NULL;

if (!comp_ctx)
return idx;

cnt = bh_list_length(&comp_ctx->native_symbols);

sym = bh_list_first_elem(&comp_ctx->native_symbols);

/* Lookup an existing symobl record */

if (sym) {
for (; cnt > 0; cnt--) {
if (sym->symbol == symbol) {
idx = sym->index;
break;
}
sym = bh_list_elem_next(sym);
while (sym) {
if (strcmp(sym->symbol, symbol) == 0) {
idx = sym->index;
break;
}
sym = bh_list_elem_next(sym);
}

/* Given symbol is not exist in list, then we alloc a new index for it */

if (idx < 0) {
sym = wasm_runtime_malloc(sizeof(AOTNativeSymbol));

if (!sym)
return idx;

if (BH_LIST_ERROR == bh_list_insert(&comp_ctx->native_symbols, sym))
if (!sym) {
aot_set_last_error("alloc native symbol failed.");
return idx;
}

idx = bh_list_length(&comp_ctx->native_symbols);
sym->symbol = symbol;
sym->index = idx;

if (BH_LIST_ERROR == bh_list_insert(&comp_ctx->native_symbols, sym)) {
wasm_runtime_free(sym);
aot_set_last_error("alloc index for native symbol failed.");
return -1;
}
}

return idx;
Expand Down Expand Up @@ -1905,3 +1929,48 @@ aot_call_llvm_intrinsic_v(const AOTCompContext *comp_ctx,

return ret;
}

LLVMValueRef
aot_get_func_from_table(AOTCompContext *comp_ctx,
LLVMValueRef base,
LLVMTypeRef func_type,
int32 index)
{
LLVMValueRef func;
LLVMValueRef func_addr;
LLVMTypeRef void_ptr = LLVMPointerType(LLVMVoidType(), 0);

if (void_ptr == NULL) {
aot_set_last_error("construct void_ptr failed.");
goto fail;
}

if (!(func_addr = LLVMConstInt(LLVMInt32Type(), index, true))) {
aot_set_last_error("construct function index failed.");
goto fail;
}

if (!(func_addr = LLVMBuildInBoundsGEP(comp_ctx->builder, base, &func_addr,
1, "get_func_addr_by_idx"))) {
aot_set_last_error("get function addr by index failed.");
goto fail;
}

func =
LLVMBuildLoad2(comp_ctx->builder, void_ptr, func_addr, "get_func_ptr");

if (func == NULL) {
aot_set_last_error("get function pointer failed.");
goto fail;
}

if (!(func = LLVMBuildBitCast(comp_ctx->builder, func, func_type,
"cast_func_type"))) {
aot_set_last_error("cast function fialed.");
goto fail;
}

return func;
fail:
return NULL;
}
Loading

0 comments on commit dca19db

Please sign in to comment.