Implement multi-value feature and import binarydump tool (#308)

This commit is contained in:
Weining
2020-07-10 16:29:15 +08:00
committed by GitHub
parent 16a284a67c
commit 1a85051415
25 changed files with 2660 additions and 812 deletions

View File

@ -64,9 +64,9 @@ check_exception_thrown(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
/* Load the first byte of aot_module_inst->cur_exception, and check
whether it is '\0'. If yes, no exception was thrown. */
if (!(value = LLVMBuildLoad(comp_ctx->builder, func_ctx->cur_exception,
"exce_value"))
"exce_value"))
|| !(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ,
value, I8_ZERO, "cmp"))) {
value, I8_ZERO, "cmp"))) {
aot_set_last_error("llvm build icmp failed.");
return false;
}
@ -183,7 +183,7 @@ call_aot_invoke_native_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
}
}
if (param_count > 64) {
if (param_cell_num > 64) {
aot_set_last_error("prepare native arguments failed: "
"maximum 64 parameter cell number supported.");
return false;
@ -309,17 +309,22 @@ aot_compile_op_call(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
uint32 import_func_count = comp_ctx->comp_data->import_func_count;
AOTImportFunc *import_funcs = comp_ctx->comp_data->import_funcs;
uint32 func_count = comp_ctx->func_ctx_count, param_cell_num = 0;
uint32 ext_ret_cell_num = 0, cell_num = 0;
AOTFuncContext **func_ctxes = comp_ctx->func_ctxes;
AOTFuncType *func_type;
AOTFunc *aot_func;
LLVMTypeRef *param_types = NULL, ret_type;
LLVMTypeRef ext_ret_ptr_type;
LLVMValueRef *param_values = NULL, value_ret = NULL, func;
LLVMValueRef import_func_idx, res;
int32 i, j = 0, param_count;
LLVMValueRef ext_ret, ext_ret_ptr, ext_ret_idx;
int32 i, j = 0, param_count, result_count, ext_ret_count;
uint64 total_size;
uint32 callee_cell_num;
uint8 wasm_ret_type;
uint8 *ext_ret_types = NULL;
bool ret = false;
char buf[32];
/* Check function index */
if (func_idx >= import_func_count + func_count) {
@ -335,11 +340,19 @@ aot_compile_op_call(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
aot_func->func_type;
/* Get param cell number */
param_cell_num = wasm_type_param_cell_num(func_type);
param_cell_num = func_type->param_cell_num;
/* Allocate memory for parameters */
/* Allocate memory for parameters.
* Parameters layout:
* - exec env
* - wasm function's parameters
* - extra results'(except the first one) addresses
*/
param_count = (int32)func_type->param_count;
total_size = sizeof(LLVMValueRef) * (uint64)(param_count + 1);
result_count = (int32)func_type->result_count;
ext_ret_count = result_count > 1 ? result_count - 1 : 0;
total_size = sizeof(LLVMValueRef) * (uint64)(param_count + 1
+ ext_ret_count);
if (total_size >= UINT32_MAX
|| !(param_values = wasm_runtime_malloc((uint32)total_size))) {
aot_set_last_error("Allocate memory failed.");
@ -402,9 +415,50 @@ aot_compile_op_call(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
&& !check_stack_boundary(comp_ctx, func_ctx, callee_cell_num))
goto fail;
/* Prepare parameters for extra results */
if (ext_ret_count > 0) {
ext_ret_types = func_type->types + param_count + 1;
ext_ret_cell_num =
wasm_get_cell_num(ext_ret_types, ext_ret_count);
if (ext_ret_cell_num > 64) {
aot_set_last_error("prepare extra results's return "
"address arguments failed: "
"maximum 64 parameter cell number supported.");
goto fail;
}
for (i = 0; i < ext_ret_count; i++) {
if (!(ext_ret_idx = I32_CONST(cell_num))
|| !(ext_ret_ptr_type =
LLVMPointerType(TO_LLVM_TYPE(ext_ret_types[i]), 0))) {
aot_set_last_error("llvm add const or pointer type failed.");
goto fail;
}
snprintf(buf, sizeof(buf), "func%d_ext_ret%d_ptr", func_idx, i);
if (!(ext_ret_ptr = LLVMBuildInBoundsGEP(comp_ctx->builder,
func_ctx->argv_buf,
&ext_ret_idx, 1, buf))) {
aot_set_last_error("llvm build GEP failed.");
goto fail;
}
snprintf(buf, sizeof(buf), "func%d_ext_ret%d_ptr_cast", func_idx, i);
if (!(ext_ret_ptr = LLVMBuildBitCast(comp_ctx->builder,
ext_ret_ptr,
ext_ret_ptr_type,
buf))) {
aot_set_last_error("llvm build bit cast failed.");
goto fail;
}
param_values[1 + param_count + i] = ext_ret_ptr;
cell_num += wasm_value_type_cell_num(ext_ret_types[i]);
}
}
/* Call the function */
if (!(value_ret = LLVMBuildCall(comp_ctx->builder, func,
param_values, (uint32)param_count + 1,
param_values,
(uint32)param_count + 1 + ext_ret_count,
(func_type->result_count > 0
? "call" : "")))) {
aot_set_last_error("LLVM build call failed.");
@ -419,8 +473,21 @@ aot_compile_op_call(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
goto fail;
}
if (func_type->result_count > 0)
if (func_type->result_count > 0) {
/* Push the first result to stack */
PUSH(value_ret, func_type->types[func_type->param_count]);
/* Load extra result from its address and push to stack */
for (i = 0; i < ext_ret_count; i++) {
snprintf(buf, sizeof(buf), "func%d_ext_ret%d", func_idx, i);
if (!(ext_ret = LLVMBuildLoad(comp_ctx->builder,
param_values[1 + param_count + i],
buf))) {
aot_set_last_error("llvm build load failed.");
goto fail;
}
PUSH(ext_ret, ext_ret_types[i]);
}
}
ret = true;
fail:
@ -437,15 +504,15 @@ call_aot_call_indirect_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
LLVMValueRef func_type_idx, LLVMValueRef table_elem_idx,
LLVMTypeRef *param_types, LLVMValueRef *param_values,
uint32 param_count, uint32 param_cell_num,
LLVMTypeRef ret_type, uint8 wasm_ret_type,
LLVMValueRef *p_value_ret, LLVMValueRef *p_res)
uint32 result_count, uint8 *wasm_ret_types,
LLVMValueRef *value_rets, LLVMValueRef *p_res)
{
LLVMTypeRef func_type, func_ptr_type, func_param_types[6];
LLVMTypeRef ret_ptr_type, elem_ptr_type;
LLVMValueRef func, elem_idx, elem_ptr;
LLVMValueRef func_param_values[6], value_ret = NULL, res = NULL;
LLVMTypeRef ret_type, ret_ptr_type, elem_ptr_type;
LLVMValueRef func, ret_idx, ret_ptr, elem_idx, elem_ptr;
LLVMValueRef func_param_values[6], res = NULL;
char buf[32], *func_name = "aot_call_indirect";
uint32 i, cell_num = 0;
uint32 i, cell_num = 0, ret_cell_num, argv_cell_num;
/* prepare function type of aot_call_indirect */
func_param_types[0] = comp_ctx->exec_env_type; /* exec_env */
@ -482,7 +549,9 @@ call_aot_call_indirect_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
}
}
if (param_count > 64) {
ret_cell_num = wasm_get_cell_num(wasm_ret_types, result_count);
argv_cell_num = param_cell_num > ret_cell_num ? param_cell_num : ret_cell_num;
if (argv_cell_num > 64) {
aot_set_last_error("prepare native arguments failed: "
"maximum 64 parameter cell number supported.");
return false;
@ -533,24 +602,31 @@ call_aot_call_indirect_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
return false;
}
/* get function return value */
if (wasm_ret_type != VALUE_TYPE_VOID) {
if (!(ret_ptr_type = LLVMPointerType(ret_type, 0))) {
aot_set_last_error("llvm add pointer type failed.");
/* get function result values */
cell_num = 0;
for (i = 0; i < result_count; i++) {
ret_type = TO_LLVM_TYPE(wasm_ret_types[i]);
if (!(ret_idx = I32_CONST(cell_num))
|| !(ret_ptr_type = LLVMPointerType(ret_type, 0))) {
aot_set_last_error("llvm add const or pointer type failed.");
return false;
}
if (!(value_ret = LLVMBuildBitCast(comp_ctx->builder, func_ctx->argv_buf,
ret_ptr_type, "argv_ret"))) {
aot_set_last_error("llvm build bit cast failed.");
snprintf(buf, sizeof(buf), "argv_ret%d", i);
if (!(ret_ptr = LLVMBuildInBoundsGEP(comp_ctx->builder,
func_ctx->argv_buf, &ret_idx, 1, buf))
|| !(ret_ptr = LLVMBuildBitCast(comp_ctx->builder, ret_ptr,
ret_ptr_type, buf))) {
aot_set_last_error("llvm build GEP or bit cast failed.");
return false;
}
if (!(*p_value_ret = LLVMBuildLoad(comp_ctx->builder, value_ret,
"value_ret"))) {
snprintf(buf, sizeof(buf), "ret%d", i);
if (!(value_rets[i] = LLVMBuildLoad(comp_ctx->builder, ret_ptr, buf))) {
aot_set_last_error("llvm build load failed.");
return false;
}
cell_num += wasm_value_type_cell_num(wasm_ret_types[i]);
}
*p_res = res;
@ -563,12 +639,12 @@ aot_compile_op_call_indirect(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
{
AOTFuncType *func_type;
LLVMValueRef elem_idx, ftype_idx;
LLVMValueRef *param_values = NULL, value_ret = NULL, res = NULL;
LLVMTypeRef *param_types = NULL, ret_type;
int32 i, param_count;
LLVMValueRef *param_values = NULL, *value_rets = NULL, res = NULL;
LLVMTypeRef *param_types = NULL;
int32 i, param_count, result_count;
uint32 param_cell_num;
uint64 total_size;
uint8 wasm_ret_type;
uint8 *wasm_ret_types = NULL;
bool ret;
/* Check function type index */
@ -582,7 +658,9 @@ aot_compile_op_call_indirect(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
func_type = comp_ctx->comp_data->func_types[type_idx];
param_cell_num = wasm_type_param_cell_num(func_type);
param_cell_num = func_type->param_cell_num;
result_count = func_type->result_count;
wasm_ret_types = func_type->types + func_type->param_count;
POP_I32(elem_idx);
@ -598,16 +676,6 @@ aot_compile_op_call_indirect(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
for (i = 0; i < param_count; i++)
param_types[i] = TO_LLVM_TYPE(func_type->types[i]);
/* Resolve return type of the LLVM function */
if (func_type->result_count) {
wasm_ret_type = func_type->types[func_type->param_count];
ret_type = TO_LLVM_TYPE(wasm_ret_type);
}
else {
wasm_ret_type = VALUE_TYPE_VOID;
ret_type = VOID_TYPE;
}
/* Allocate memory for parameters */
total_size = sizeof(LLVMValueRef) * (uint64)param_count;
if (total_size >= UINT32_MAX
@ -620,16 +688,25 @@ aot_compile_op_call_indirect(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
for (i = param_count - 1; i >= 0; i--)
POP(param_values[i], func_type->types[i]);
/* Allocate memory for result values */
total_size = sizeof(LLVMValueRef) * (uint64)result_count;
if (total_size >= UINT32_MAX
|| !(value_rets = wasm_runtime_malloc((uint32)total_size))) {
aot_set_last_error("Allocate memory failed.");
goto fail;
}
memset(value_rets, 0, total_size);
if (!call_aot_call_indirect_func(comp_ctx, func_ctx,
func_type, ftype_idx, elem_idx,
param_types, param_values,
param_count, param_cell_num,
ret_type, wasm_ret_type,
&value_ret, &res))
result_count, wasm_ret_types,
value_rets, &res))
goto fail;
if (func_type->result_count > 0)
PUSH(value_ret, func_type->types[func_type->param_count]);
for (i = 0; i < func_type->result_count; i++)
PUSH(value_rets[i], func_type->types[func_type->param_count + i]);
/* Check whether there was exception thrown when executing the function */
if (!check_call_return(comp_ctx, func_ctx, res))
@ -638,6 +715,8 @@ aot_compile_op_call_indirect(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
ret = true;
fail:
if (value_rets)
wasm_runtime_free(value_rets);
if (param_values)
wasm_runtime_free(param_values);
if (param_types)