This repository has been archived on 2023-11-05. You can view files and clone it, but cannot push or open issues or pull requests.
wasm-micro-runtime/core/iwasm/compilation/aot_emit_conversion.c

433 lines
11 KiB
C
Raw Normal View History

Enable AoT and wamr-sdk, and change arguments of call wasm API (#157) * Implement memory profiler, optimize memory usage, modify code indent * Implement memory.grow and limit heap space base offset to 1G; modify iwasm build type to Release and 64 bit by default * Add a new extension library: connection * Fix bug of reading magic number and version in big endian platform * Re-org platform APIs: move most platform APIs from iwasm to shared-lib * Enhance wasm loader to fix some security issues * Fix issue about illegal load of EXC_RETURN into PC on stm32 board * Updates that let a restricted version of the interpreter run in SGX * Enable native/app address validation and conversion for wasm app * Remove wasm_application_exectue_* APIs from wasm_export.h which makes confused * Refine binary size and fix several minor issues Optimize interpreter LOAD/STORE opcodes to decrease the binary size Fix issues when using iwasm library: _bh_log undefined, bh_memory.h not found Remove unused _stdin/_stdout/_stderr global variables resolve in libc wrapper Add macros of global heap size, stack size, heap size for Zephyr main.c Clear compile warning of wasm_application.c * Add more strict security checks for libc wrapper API's * Use one libc wrapper copy for sgx and other platforms; remove bh_printf macro for other platform header files * Enhance security of libc strcpy/sprintf wrapper function * Fix issue of call native for x86_64/arm/mips, add module inst parameter for native wrapper functions * Remove get_module_inst() and fix issue of call native * Refine wgl lib: remove module_inst parameter from widget functions; move function index check to runtime instantiate * Refine interpreter call native process, refine memory boudary check * Fix issues of invokeNative function of arm/mips/general version * Add a switch to build simple sample without gui support * Add BUILD_TARGET setting in makefile to replace cpu compiler flags in source code * Re-org shared lib header files, remove unused info; fix compile issues of vxworks * Add build target general * Remove unused files * Update license header * test push * Restore file * Sync up with internal/feature * Sync up with internal/feature * Rename build_wamr_app to build_wasm_app * Fix small issues of README * Enhance malformed wasm file checking Fix issue of print hex int and implement utf8 string check Fix wasi file read/write right issue Fix minor issue of build wasm app doc * Sync up with internal/feature * Sync up with internal/feature: fix interpreter arm issue, fix read leb issue * Sync up with internal/feature * Fix bug of config.h and rename wasi config.h to ssp_config.h * Sync up with internal/feature * Import wamr aot * update document * update document * Update document, disable WASI in 32bit * update document * remove files * update document * Update document * update document * update document * update samples * Sync up with internal repo
2020-01-21 13:26:14 +08:00
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "aot_emit_conversion.h"
#include "aot_emit_exception.h"
#include "aot_emit_numberic.h"
#include "../aot/aot_runtime.h"
static bool
trunc_float_to_int(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
LLVMValueRef operand, LLVMTypeRef dest_type,
LLVMValueRef min_value, LLVMValueRef max_value,
char *name, bool sign)
{
LLVMBasicBlockRef check_nan_succ, check_overflow_succ;
LLVMValueRef is_less, is_greater, res;
if (!(res = LLVMBuildFCmp(comp_ctx->builder, LLVMRealUNO,
operand, operand, "fcmp_is_nan"))) {
aot_set_last_error("llvm build fcmp failed.");
goto fail;
}
if (!(check_nan_succ =
LLVMAppendBasicBlockInContext(comp_ctx->context,
func_ctx->func,
"check_nan_succ"))) {
aot_set_last_error("llvm add basic block failed.");
goto fail;
}
LLVMMoveBasicBlockAfter(check_nan_succ,
LLVMGetInsertBlock(comp_ctx->builder));
if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_INVALID_CONVERSION_TO_INTEGER,
true, res, check_nan_succ)))
goto fail;
if (!(is_less = LLVMBuildFCmp(comp_ctx->builder, LLVMRealOLE, operand,
min_value, "fcmp_min_value"))) {
aot_set_last_error("llvm build fcmp failed.");
goto fail;
}
if (!(is_greater = LLVMBuildFCmp(comp_ctx->builder, LLVMRealOGE, operand,
max_value, "fcmp_max_value"))) {
aot_set_last_error("llvm build fcmp failed.");
goto fail;
}
if (!(res = LLVMBuildOr(comp_ctx->builder, is_less, is_greater, "is_overflow"))) {
aot_set_last_error("llvm build logic and failed.");
goto fail;
}
/* Check if float value out of range */
if (!(check_overflow_succ =
LLVMAppendBasicBlockInContext(comp_ctx->context,
func_ctx->func,
"check_overflow_succ"))) {
aot_set_last_error("llvm add basic block failed.");
goto fail;
}
LLVMMoveBasicBlockAfter(check_overflow_succ,
LLVMGetInsertBlock(comp_ctx->builder));
if (!(aot_emit_exception(comp_ctx, func_ctx, EXCE_INTEGER_OVERFLOW,
true, res, check_overflow_succ)))
goto fail;
if (sign)
res = LLVMBuildFPToSI(comp_ctx->builder, operand, dest_type, name);
else
res = LLVMBuildFPToUI(comp_ctx->builder, operand, dest_type, name);
if (!res) {
aot_set_last_error("llvm build conversion failed.");
return false;
}
if (dest_type == I32_TYPE)
PUSH_I32(res);
else if (dest_type == I64_TYPE)
PUSH_I64(res);
return true;
fail:
return false;
}
bool
aot_compile_op_i32_wrap_i64(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
{
LLVMValueRef value, res;
POP_I64(value);
if (!(res = LLVMBuildTrunc(comp_ctx->builder, value, I32_TYPE, "i32_wrap_i64"))) {
aot_set_last_error("llvm build conversion failed.");
return false;
}
PUSH_I32(res);
return true;
fail:
return false;
}
bool
aot_compile_op_i32_trunc_f32(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
bool sign)
{
LLVMValueRef value;
LLVMValueRef min_value, max_value;
POP_F32(value);
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);
}
return trunc_float_to_int(comp_ctx, func_ctx, value,
I32_TYPE, min_value, max_value,
sign ? "i32_trunc_f32_s" : "i32_trunc_f32_u", sign);
fail:
return false;
}
bool
aot_compile_op_i32_trunc_f64(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
bool sign)
{
LLVMValueRef value;
LLVMValueRef min_value, max_value;
POP_F64(value);
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);
}
return trunc_float_to_int(comp_ctx, func_ctx, value,
I32_TYPE, min_value, max_value,
sign ? "i32_trunc_f64_s" : "i32_trunc_f64_u", sign);
fail:
return false;
}
bool
aot_compile_op_i64_extend_i32(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
bool sign)
{
LLVMValueRef value, res;
POP_I32(value);
if (sign)
res = LLVMBuildSExt(comp_ctx->builder, value, I64_TYPE, "i64_extend_i32_s");
else
res = LLVMBuildZExt(comp_ctx->builder, value, I64_TYPE, "i64_extend_i32_u");
if (!res) {
aot_set_last_error("llvm build conversion failed.");
return false;
}
PUSH_I64(res);
return true;
fail:
return false;
}
bool
aot_compile_op_i64_trunc_f32(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
bool sign)
{
LLVMValueRef value;
LLVMValueRef min_value, max_value;
POP_F32(value);
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);
}
return trunc_float_to_int(comp_ctx, func_ctx, value,
I64_TYPE, min_value, max_value,
sign ? "i64_trunc_f32_s" : "i64_trunc_f32_u", sign);
fail:
return false;
}
bool
aot_compile_op_i64_trunc_f64(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
bool sign)
{
LLVMValueRef value;
LLVMValueRef min_value, max_value;
POP_F64(value);
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);
}
return trunc_float_to_int(comp_ctx, func_ctx, value,
I64_TYPE, min_value, max_value,
sign ? "i64_trunc_f64_s" : "i64_trunc_f64_u", sign);
fail:
return false;
}
bool
aot_compile_op_f32_convert_i32(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
bool sign)
{
LLVMValueRef value, res;
POP_I32(value);
if (sign)
res = LLVMBuildSIToFP(comp_ctx->builder, value, F32_TYPE, "f32_convert_i32_s");
else
res = LLVMBuildUIToFP(comp_ctx->builder, value, F32_TYPE, "f32_convert_i32_u");
if (!res) {
aot_set_last_error("llvm build conversion failed.");
return false;
}
PUSH_F32(res);
return true;
fail:
return false;
}
bool
aot_compile_op_f32_convert_i64(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
bool sign)
{
LLVMValueRef value, res;
POP_I64(value);
if (sign)
res = LLVMBuildSIToFP(comp_ctx->builder, value, F32_TYPE, "f32_convert_i64_s");
else
res = LLVMBuildUIToFP(comp_ctx->builder, value, F32_TYPE, "f32_convert_i64_u");
if (!res) {
aot_set_last_error("llvm build conversion failed.");
return false;
}
PUSH_F32(res);
return true;
fail:
return false;
}
bool
aot_compile_op_f32_demote_f64(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
{
LLVMValueRef value, res;
POP_F64(value);
if (!(res = LLVMBuildFPTrunc(comp_ctx->builder, value, F32_TYPE, "f32_demote_f64"))) {
aot_set_last_error("llvm build conversion failed.");
return false;
}
PUSH_F32(res);
return true;
fail:
return false;
}
bool
aot_compile_op_f64_convert_i32(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
bool sign)
{
LLVMValueRef value, res;
POP_I32(value);
if (sign)
res = LLVMBuildSIToFP(comp_ctx->builder, value, F64_TYPE, "f64_convert_i32_s");
else
res = LLVMBuildUIToFP(comp_ctx->builder, value, F64_TYPE, "f64_convert_i32_u");
if (!res) {
aot_set_last_error("llvm build conversion failed.");
return false;
}
PUSH_F64(res);
return true;
fail:
return false;
}
bool
aot_compile_op_f64_convert_i64(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
bool sign)
{
LLVMValueRef value, res;
POP_I64(value);
if (sign)
res = LLVMBuildSIToFP(comp_ctx->builder, value, F64_TYPE, "f64_convert_i64_s");
else
res = LLVMBuildUIToFP(comp_ctx->builder, value, F64_TYPE, "f64_convert_i64_u");
if (!res) {
aot_set_last_error("llvm build conversion failed.");
return false;
}
PUSH_F64(res);
return true;
fail:
return false;
}
bool
aot_compile_op_f64_promote_f32(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
{
LLVMValueRef value, res;
POP_F32(value);
if (!(res = LLVMBuildFPExt(comp_ctx->builder, value, F64_TYPE, "f64_promote_f32"))) {
aot_set_last_error("llvm build conversion failed.");
return false;
}
PUSH_F64(res);
/* Avoid the promote being optimized away */
PUSH_F64(F64_CONST(1.0));
return aot_compile_op_f64_arithmetic(comp_ctx, func_ctx, FLOAT_MUL);
fail:
return false;
}
bool
aot_compile_op_i64_reinterpret_f64(AOTCompContext *comp_ctx,
AOTFuncContext *func_ctx)
{
LLVMValueRef value;
POP_F64(value);
if (!(value = LLVMBuildBitCast(comp_ctx->builder, value,
I64_TYPE, "i64"))) {
aot_set_last_error("llvm build fp to si failed.");
return false;
}
PUSH_I64(value);
return true;
fail:
return false;
}
bool
aot_compile_op_i32_reinterpret_f32(AOTCompContext *comp_ctx,
AOTFuncContext *func_ctx)
{
LLVMValueRef value;
POP_F32(value);
if (!(value = LLVMBuildBitCast(comp_ctx->builder, value,
I32_TYPE, "i32"))) {
aot_set_last_error("llvm build fp to si failed.");
return false;
}
PUSH_I32(value);
return true;
fail:
return false;
}
bool
aot_compile_op_f64_reinterpret_i64(AOTCompContext *comp_ctx,
AOTFuncContext *func_ctx)
{
LLVMValueRef value;
POP_I64(value);
if (!(value = LLVMBuildBitCast(comp_ctx->builder, value,
F64_TYPE, "f64"))) {
aot_set_last_error("llvm build si to fp failed.");
return false;
}
PUSH_F64(value);
return true;
fail:
return false;
}
bool
aot_compile_op_f32_reinterpret_i32(AOTCompContext *comp_ctx,
AOTFuncContext *func_ctx)
{
LLVMValueRef value;
POP_I32(value);
if (!(value = LLVMBuildBitCast(comp_ctx->builder, value,
F32_TYPE, "f32"))) {
aot_set_last_error("llvm build si to fp failed.");
return false;
}
PUSH_F32(value);
return true;
fail:
return false;
}