Implement GC (Garbage Collection) feature for interpreter, AOT and LLVM-JIT (#3125)

Implement the GC (Garbage Collection) feature for interpreter mode,
AOT mode and LLVM-JIT mode, and support most features of the latest
spec proposal, and also enable the stringref feature.

Use `cmake -DWAMR_BUILD_GC=1/0` to enable/disable the feature,
and `wamrc --enable-gc` to generate the AOT file with GC supported.

And update the AOT file version from 2 to 3 since there are many AOT
ABI breaks, including the changes of AOT file format, the changes of
AOT module/memory instance layouts, the AOT runtime APIs for the
AOT code to invoke and so on.
This commit is contained in:
Wenyong Huang
2024-02-06 20:47:11 +08:00
committed by GitHub
parent 5931aaacbe
commit 16a4d71b34
98 changed files with 33469 additions and 3159 deletions

View File

@ -9,12 +9,15 @@
#include "bh_platform.h"
#include "bh_hashmap.h"
#include "bh_assert.h"
#if WASM_ENABLE_GC != 0
#include "gc_export.h"
#endif
#ifdef __cplusplus
extern "C" {
#endif
/** Value Type */
/* Value Type */
#define VALUE_TYPE_I32 0x7F
#define VALUE_TYPE_I64 0X7E
#define VALUE_TYPE_F32 0x7D
@ -23,29 +26,99 @@ extern "C" {
#define VALUE_TYPE_FUNCREF 0x70
#define VALUE_TYPE_EXTERNREF 0x6F
#define VALUE_TYPE_VOID 0x40
/* Packed Types */
#define PACKED_TYPE_I8 0x78
#define PACKED_TYPE_I16 0x77
/* Reference Types */
#define REF_TYPE_NULLFUNCREF 0x73
#define REF_TYPE_NULLEXTERNREF 0x72
#define REF_TYPE_NULLREF 0x71
#define REF_TYPE_FUNCREF VALUE_TYPE_FUNCREF /* 0x70 */
#define REF_TYPE_EXTERNREF VALUE_TYPE_EXTERNREF /* 0x6F */
#define REF_TYPE_ANYREF 0x6E
#define REF_TYPE_EQREF 0x6D
#define REF_TYPE_I31REF 0x6C
#define REF_TYPE_STRUCTREF 0x6B
#define REF_TYPE_ARRAYREF 0x6A
#define REF_TYPE_HT_NON_NULLABLE 0x64
#define REF_TYPE_HT_NULLABLE 0x63
#define REF_TYPE_STRINGREF VALUE_TYPE_STRINGREF /* 0x67 */
#define REF_TYPE_STRINGVIEWWTF8 VALUE_TYPE_STRINGVIEWWTF8 /* 0x66 */
#define REF_TYPE_STRINGVIEWWTF16 VALUE_TYPE_STRINGVIEWWTF16 /* 0x62 */
#define REF_TYPE_STRINGVIEWITER VALUE_TYPE_STRINGVIEWITER /* 0x61 */
/* Heap Types */
#define HEAP_TYPE_NOFUNC (-0x0D)
#define HEAP_TYPE_NOEXTERN (-0x0E)
#define HEAP_TYPE_NONE (-0x0F)
#define HEAP_TYPE_FUNC (-0x10)
#define HEAP_TYPE_EXTERN (-0x11)
#define HEAP_TYPE_ANY (-0x12)
#define HEAP_TYPE_EQ (-0x13)
#define HEAP_TYPE_I31 (-0x14)
#define HEAP_TYPE_STRUCT (-0x15)
#define HEAP_TYPE_ARRAY (-0x16)
#define HEAP_TYPE_STRINGREF (-0x19)
#define HEAP_TYPE_STRINGVIEWWTF8 (-0x1A)
#define HEAP_TYPE_STRINGVIEWWTF16 (-0x1E)
#define HEAP_TYPE_STRINGVIEWITER (-0x1F)
/* Defined Types */
#define DEFINED_TYPE_FUNC 0x60
#define DEFINED_TYPE_STRUCT 0x5F
#define DEFINED_TYPE_ARRAY 0x5E
#define DEFINED_TYPE_SUB 0x50
#define DEFINED_TYPE_SUB_FINAL 0x4F
#define DEFINED_TYPE_REC 0x4E
/* Used by AOT */
#define VALUE_TYPE_I1 0x41
/* Used by loader to represent any type of i32/i64/f32/f64 */
/**
* Used by loader to represent any type of i32/i64/f32/f64/v128
* and ref types, including funcref, externref, anyref, eqref,
* (ref null $ht), (ref $ht), i31ref, structref, arrayref,
* nullfuncref, nullexternref, nullref and stringref
*/
#define VALUE_TYPE_ANY 0x42
/**
* Used by wamr compiler to represent object ref types,
* including func object ref, externref object ref,
* internal object ref, eq obect ref, i31 object ref,
* struct object ref, array obect ref
*/
#define VALUE_TYPE_GC_REF 0x43
#define DEFAULT_NUM_BYTES_PER_PAGE 65536
#define DEFAULT_MAX_PAGES 65536
#if WASM_ENABLE_GC == 0
typedef uintptr_t table_elem_type_t;
#define NULL_REF (0xFFFFFFFF)
#else
typedef void *table_elem_type_t;
#define NULL_REF (NULL)
#define REF_CELL_NUM ((uint32)sizeof(uintptr_t) / sizeof(uint32))
#endif
#define TABLE_MAX_SIZE (1024)
#define INIT_EXPR_NONE 0x00
#define INIT_EXPR_TYPE_I32_CONST 0x41
#define INIT_EXPR_TYPE_I64_CONST 0x42
#define INIT_EXPR_TYPE_F32_CONST 0x43
#define INIT_EXPR_TYPE_F64_CONST 0x44
#define INIT_EXPR_TYPE_V128_CONST 0xFD
/* = WASM_OP_REF_FUNC */
#define INIT_EXPR_TYPE_FUNCREF_CONST 0xD2
/* = WASM_OP_REF_NULL */
#define INIT_EXPR_TYPE_REFNULL_CONST 0xD0
#define INIT_EXPR_TYPE_GET_GLOBAL 0x23
#define INIT_EXPR_TYPE_ERROR 0xff
#define INIT_EXPR_TYPE_REFNULL_CONST 0xD0
#define INIT_EXPR_TYPE_FUNCREF_CONST 0xD2
#define INIT_EXPR_TYPE_STRUCT_NEW 0xD3
#define INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT 0xD4
#define INIT_EXPR_TYPE_ARRAY_NEW 0xD5
#define INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT 0xD6
#define INIT_EXPR_TYPE_ARRAY_NEW_FIXED 0xD7
#define INIT_EXPR_TYPE_I31_NEW 0xD8
#define INIT_EXPR_TYPE_ANY_CONVERT_EXTERN 0xD9
#define INIT_EXPR_TYPE_EXTERN_CONVERT_ANY 0xDA
#define WASM_MAGIC_NUMBER 0x6d736100
#define WASM_CURRENT_VERSION 1
@ -68,6 +141,9 @@ extern "C" {
#if WASM_ENABLE_TAGS != 0
#define SECTION_TYPE_TAG 13
#endif
#if WASM_ENABLE_STRINGREF != 0
#define SECTION_TYPE_STRINGREF 14
#endif
#define SUB_SECTION_TYPE_MODULE 0
#define SUB_SECTION_TYPE_FUNC 1
@ -99,6 +175,21 @@ extern "C" {
#define LABEL_TYPE_CATCH_ALL 6
#endif
#define WASM_TYPE_FUNC 0
#define WASM_TYPE_STRUCT 1
#define WASM_TYPE_ARRAY 2
#if WASM_ENABLE_STRINGREF != 0
#define WASM_TYPE_STRINGREF 3
#define WASM_TYPE_STRINGVIEWWTF8 4
#define WASM_TYPE_STRINGVIEWWTF16 5
#define WASM_TYPE_STRINGVIEWITER 6
#endif
/* In WasmGC, a table can start with [0x40 0x00] to indicate it has an
* initializer */
#define TABLE_INIT_EXPR_FLAG 0x40
typedef struct WASMModule WASMModule;
typedef struct WASMFunction WASMFunction;
typedef struct WASMGlobal WASMGlobal;
@ -106,6 +197,8 @@ typedef struct WASMGlobal WASMGlobal;
typedef struct WASMTag WASMTag;
#endif
#ifndef WASM_VALUE_DEFINED
#define WASM_VALUE_DEFINED
typedef union V128 {
int8 i8x16[16];
int16 i16x8[8];
@ -124,44 +217,268 @@ typedef union WASMValue {
uint64 u64;
float32 f32;
float64 f64;
uintptr_t addr;
V128 v128;
#if WASM_ENABLE_GC != 0
wasm_obj_t gc_obj;
uint32 type_index;
struct {
uint32 type_index;
uint32 length;
} array_new_default;
/* pointer to a memory space holding more data, current usage:
* struct.new init value: WASMStructNewInitValues *
* array.new init value: WASMArrayNewInitValues *
*/
void *data;
#endif
} WASMValue;
#endif /* end of WASM_VALUE_DEFINED */
typedef struct WASMStructNewInitValues {
uint8 type_idx;
uint32 count;
WASMValue fields[1];
} WASMStructNewInitValues;
typedef struct WASMArrayNewInitValues {
uint8 type_idx;
uint32 length;
WASMValue elem_data[1];
} WASMArrayNewInitValues;
typedef struct InitializerExpression {
/* type of INIT_EXPR_TYPE_XXX */
/* it actually is instr, in some places, requires constant only */
/* type of INIT_EXPR_TYPE_XXX, which is an instruction of
constant expression */
uint8 init_expr_type;
WASMValue u;
} InitializerExpression;
#if WASM_ENABLE_GC != 0
/**
* Reference type of (ref null ht) or (ref ht),
* and heap type is defined type (type i), i >= 0
*/
typedef struct RefHeapType_TypeIdx {
/* ref_type is REF_TYPE_HT_NULLABLE or
REF_TYPE_HT_NON_NULLABLE, (0x6C or 0x6B) */
uint8 ref_type;
/* true if ref_type is REF_TYPE_HT_NULLABLE */
bool nullable;
/* heap type is defined type: type_index >= 0 */
int32 type_idx;
} RefHeapType_TypeIdx;
/**
* Reference type of (ref null ht) or (ref ht),
* and heap type is non-defined type
*/
typedef struct RefHeapType_Common {
/* ref_type is REF_TYPE_HT_NULLABLE or
REF_TYPE_HT_NON_NULLABLE (0x6C or 0x6B) */
uint8 ref_type;
/* true if ref_type is REF_TYPE_HT_NULLABLE */
bool nullable;
/* Common heap type (not defined type):
-0x10 (func), -0x11 (extern), -0x12 (any), -0x13 (eq),
-0x16 (i31), -0x17 (nofunc), -0x18 (noextern),
-0x19 (struct), -0x20 (array), -0x21 (none) */
int32 heap_type;
} RefHeapType_Common;
/**
* Reference type
*/
typedef union WASMRefType {
uint8 ref_type;
RefHeapType_TypeIdx ref_ht_typeidx;
RefHeapType_Common ref_ht_common;
} WASMRefType;
typedef struct WASMRefTypeMap {
/**
* The type index of a type array, which only stores
* the first byte of the type, e.g. WASMFuncType.types,
* WASMStructType.fields
*/
uint16 index;
/* The full type info if the type cannot be described
with one byte */
WASMRefType *ref_type;
} WASMRefTypeMap;
#endif /* end of WASM_ENABLE_GC */
#if WASM_ENABLE_GC == 0
typedef struct WASMFuncType WASMType;
typedef WASMType *WASMTypePtr;
#else
/**
* Common type, store the same fields of
* WASMFuncType, WASMStructType and WASMArrayType
*/
typedef struct WASMType {
/**
* type_flag must be WASM_TYPE_FUNC/STRUCT/ARRAY to
* denote that it is a WASMFuncType, WASMStructType or
* WASMArrayType
*/
uint16 type_flag;
bool is_sub_final;
/* The inheritance depth */
uint32 inherit_depth;
/* The root type */
struct WASMType *root_type;
/* The parent type */
struct WASMType *parent_type;
uint32 parent_type_idx;
/* number of internal types in the current rec group, if the type is not in
* a recursive group, rec_count = 0 */
uint16 rec_count;
uint16 rec_idx;
} WASMType, *WASMTypePtr;
#endif /* end of WASM_ENABLE_GC */
/* Function type */
typedef struct WASMFuncType {
#if WASM_ENABLE_GC != 0
WASMType base_type;
#endif
uint16 param_count;
uint16 result_count;
uint16 param_cell_num;
uint16 ret_cell_num;
uint16 ref_count;
#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
&& WASM_ENABLE_LAZY_JIT != 0
/* Code block to call llvm jit functions of this
kind of function type from fast jit jitted code */
void *call_to_llvm_jit_from_fast_jit;
#endif
#if WASM_ENABLE_GC != 0
uint16 ref_type_map_count;
WASMRefTypeMap *ref_type_maps;
WASMRefTypeMap *result_ref_type_maps;
/* minimal type index of the type equal to this type,
used in type equal check in call_indirect opcode */
uint32 min_type_idx_normalized;
#else
uint16 ref_count;
#endif
#if WASM_ENABLE_QUICK_AOT_ENTRY != 0
/* Quick AOT/JIT entry of this func type */
void *quick_aot_entry;
#endif
/* types of params and results */
/* types of params and results, only store the first byte
* of the type, if it cannot be described with one byte,
* then the full type info is stored in ref_type_maps */
uint8 types[1];
} WASMType;
} WASMFuncType;
#if WASM_ENABLE_GC != 0
typedef struct WASMStructFieldType {
uint16 field_flags;
uint8 field_type;
uint8 field_size;
uint32 field_offset;
#if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0
/*
* The field size and field offset of a wasm struct may vary
* in 32-bit target and 64-bit target, e.g., the size of a
* GC reference is 4 bytes in the former and 8 bytes in the
* latter, the AOT compiler needs to use the correct field
* offset according to the target info.
*/
uint8 field_size_64bit;
uint8 field_size_32bit;
uint32 field_offset_64bit;
uint32 field_offset_32bit;
#endif
} WASMStructFieldType;
typedef struct WASMStructType {
WASMType base_type;
/* total size of this struct object */
uint32 total_size;
uint16 field_count;
uint16 ref_type_map_count;
WASMRefTypeMap *ref_type_maps;
/* Offsets of reference fields that need to be traced during GC.
The first element of the table is the number of such offsets. */
uint16 *reference_table;
/* Field info, note that fields[i]->field_type only stores
* the first byte of the field type, if it cannot be described
* with one byte, then the full field type info is stored in
* ref_type_maps */
WASMStructFieldType fields[1];
} WASMStructType;
typedef struct WASMArrayType {
WASMType base_type;
uint16 elem_flags;
uint8 elem_type;
/* The full elem type info if the elem type cannot be
described with one byte */
WASMRefType *elem_ref_type;
} WASMArrayType;
#if WASM_ENABLE_STRINGREF != 0
/* stringref representation, we define it as a void * pointer here, the
* stringref implementation can use any structure */
/*
WasmGC heap
+-----------------------+
| |
| stringref |
| +----------+ | external string representation
| | host_ptr |--------o------+----->+------------+
| +----------+ | | | |
| | | +------------+
| stringview_wtf8/16 | |
| +----------+ | |
| | host_ptr |--------o------+
| +----------+ | |
| | |
| stringview_iter | |
| +----------+ | |
| | host_ptr |--------o------+
| +----------+ |
| | pos | |
| +----------+ |
| |
+-----------------------+
*/
typedef void *WASMString;
#endif /* end of WASM_ENABLE_STRINGREF != 0 */
#endif /* end of WASM_ENABLE_GC != 0 */
typedef struct WASMTable {
uint8 elem_type;
uint32 flags;
/**
* 0: no max size and not shared
* 1: hax max size
* 2: shared
*/
uint8 flags;
bool possible_grow;
uint32 init_size;
/* specified if (flags & 1), else it is 0x10000 */
uint32 max_size;
bool possible_grow;
#if WASM_ENABLE_GC != 0
WASMRefType *elem_ref_type;
/* init expr for the whole table */
InitializerExpression init_expr;
#endif
} WASMTable;
typedef struct WASMMemory {
@ -174,12 +491,16 @@ typedef struct WASMMemory {
typedef struct WASMTableImport {
char *module_name;
char *field_name;
/* 0: no max size, 1: has max size */
uint8 elem_type;
uint32 flags;
uint8 flags;
bool possible_grow;
uint32 init_size;
/* specified if (flags & 1), else it is 0x10000 */
uint32 max_size;
bool possible_grow;
#if WASM_ENABLE_GC != 0
WASMRefType *elem_ref_type;
#endif
#if WASM_ENABLE_MULTI_MODULE != 0
WASMModule *import_module;
WASMTable *import_table_linked;
@ -203,19 +524,23 @@ typedef struct WASMFunctionImport {
char *module_name;
char *field_name;
/* function type */
WASMType *func_type;
WASMFuncType *func_type;
/* native function pointer after linked */
void *func_ptr_linked;
/* signature from registered native symbols */
const char *signature;
/* attachment */
void *attachment;
#if WASM_ENABLE_GC != 0
/* the type index of this function's func_type */
uint32 type_idx;
#endif
bool call_conv_raw;
bool call_conv_wasm_c_api;
#if WASM_ENABLE_MULTI_MODULE != 0
WASMModule *import_module;
WASMFunction *import_func_linked;
#endif
bool call_conv_wasm_c_api;
} WASMFunctionImport;
#if WASM_ENABLE_TAGS != 0
@ -241,9 +566,12 @@ typedef struct WASMGlobalImport {
char *field_name;
uint8 type;
bool is_mutable;
bool is_linked;
/* global data after linked */
WASMValue global_data_linked;
bool is_linked;
#if WASM_ENABLE_GC != 0
WASMRefType *ref_type;
#endif
#if WASM_ENABLE_MULTI_MODULE != 0
/* imported function pointer after linked */
/* TODO: remove if not needed */
@ -278,9 +606,13 @@ struct WASMFunction {
char *field_name;
#endif
/* the type of function */
WASMType *func_type;
WASMFuncType *func_type;
uint32 local_count;
uint8 *local_types;
#if WASM_ENABLE_GC != 0
uint16 local_ref_type_map_count;
WASMRefTypeMap *local_ref_type_maps;
#endif
/* cell num of parameters */
uint16 param_cell_num;
@ -303,6 +635,11 @@ struct WASMFunction {
uint32 const_cell_num;
#endif
#if WASM_ENABLE_GC != 0
/* the type index of this function's func_type */
uint32 type_idx;
#endif
#if WASM_ENABLE_EXCE_HANDLING != 0
uint32 exception_handler_count;
#endif
@ -347,6 +684,9 @@ struct WASMTag {
struct WASMGlobal {
uint8 type;
bool is_mutable;
#if WASM_ENABLE_GC != 0
WASMRefType *ref_type;
#endif
InitializerExpression init_expr;
#if WASM_ENABLE_FAST_JIT != 0
/* The data offset of current global in global data */
@ -365,11 +705,14 @@ typedef struct WASMTableSeg {
uint32 mode;
/* funcref or externref, elemkind will be considered as funcref */
uint32 elem_type;
#if WASM_ENABLE_GC != 0
WASMRefType *elem_ref_type;
#endif
/* optional, only for active */
uint32 table_index;
InitializerExpression base_offset;
uint32 function_count;
uint32 *func_indexes;
uint32 value_count;
InitializerExpression *init_values;
} WASMTableSeg;
typedef struct WASMDataSeg {
@ -481,6 +824,13 @@ struct WASMModule {
#if WASM_ENABLE_BULK_MEMORY != 0
/* data count read from datacount section */
uint32 data_seg_count1;
#endif
#if WASM_ENABLE_GC != 0
#if WASM_ENABLE_STRINGREF != 0
uint32 string_literal_count;
uint32 *string_literal_lengths;
const uint8 **string_literal_ptrs;
#endif
#endif
uint32 import_function_count;
@ -564,13 +914,31 @@ struct WASMModule {
bh_list import_module_list_head;
bh_list *import_module_list;
#endif
#if WASM_ENABLE_GC != 0
/* Ref types hash set */
HashMap *ref_type_set;
struct WASMRttType **rtt_types;
korp_mutex rtt_type_lock;
#if WASM_ENABLE_STRINGREF != 0
/* special rtts for stringref types
- stringref
- stringview_wtf8
- stringview_wtf16
- stringview_iter
*/
struct WASMRttType *stringref_rtts[4];
#endif
#endif
#if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
bh_list fast_opcode_list;
uint8 *buf_code;
uint64 buf_code_size;
#endif
#if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0 \
|| WASM_ENABLE_FAST_JIT != 0
#if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0 \
|| WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_JIT != 0
uint8 *load_addr;
uint64 load_size;
#endif
@ -671,6 +1039,12 @@ struct WASMModule {
functions in that group */
uint32 fast_jit_ready_groups;
#endif
#if WASM_ENABLE_WAMR_COMPILER != 0
bool is_simd_used;
bool is_ref_types_used;
bool is_bulk_memory_used;
#endif
};
typedef struct BlockType {
@ -679,8 +1053,13 @@ typedef struct BlockType {
* by a type index of module.
*/
union {
uint8 value_type;
WASMType *type;
struct {
uint8 type;
#if WASM_ENABLE_GC != 0
WASMRefTypeMap ref_type_map;
#endif
} value_type;
WASMFuncType *type;
} u;
bool is_value_type;
} BlockType;
@ -755,33 +1134,72 @@ wasm_string_equal(const char *s1, const char *s2)
}
/**
* Return the byte size of value type.
* Return the byte size of value type with specific pointer size.
*
* Note: Please use wasm_value_type_size for interpreter, only aot compiler
* can use this API directly to calculate type size for different target
*/
inline static uint32
wasm_value_type_size_internal(uint8 value_type, uint8 pointer_size)
{
if (value_type == VALUE_TYPE_VOID)
return 0;
else if (value_type == VALUE_TYPE_I32 || value_type == VALUE_TYPE_F32
|| value_type == VALUE_TYPE_ANY)
return sizeof(int32);
else if (value_type == VALUE_TYPE_I64 || value_type == VALUE_TYPE_F64)
return sizeof(int64);
#if WASM_ENABLE_SIMD != 0
else if (value_type == VALUE_TYPE_V128)
return sizeof(int64) * 2;
#endif
#if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
else if (value_type == VALUE_TYPE_FUNCREF
|| value_type == VALUE_TYPE_EXTERNREF)
return sizeof(uint32);
#elif WASM_ENABLE_GC != 0
else if ((value_type >= (uint8)REF_TYPE_ARRAYREF /* 0x6A */
&& value_type <= (uint8)REF_TYPE_NULLFUNCREF) /* 0x73 */
|| (value_type >= (uint8)REF_TYPE_HT_NULLABLE /* 0x63 */
&& value_type <= (uint8)REF_TYPE_HT_NON_NULLABLE) /* 0x64 */
#if WASM_ENABLE_STRINGREF != 0
|| (value_type >= (uint8)REF_TYPE_STRINGVIEWWTF8 /* 0x66 */
&& value_type <= (uint8)REF_TYPE_STRINGREF) /* 0x67 */
|| (value_type >= (uint8)REF_TYPE_STRINGVIEWITER /* 0x61 */
&& value_type <= (uint8)REF_TYPE_STRINGVIEWWTF16) /* 0x62 */
#endif
)
return pointer_size;
else if (value_type == PACKED_TYPE_I8)
return sizeof(int8);
else if (value_type == PACKED_TYPE_I16)
return sizeof(int16);
#endif
else {
bh_assert(0);
}
return 0;
}
/**
* Return the cell num of value type with specific pointer size.
*
* Note: Please use wasm_value_type_cell_num for interpreter, only aot compiler
* can use this API directly to calculate type cell num for different target
*/
inline static uint16
wasm_value_type_cell_num_internal(uint8 value_type, uint8 pointer_size)
{
return wasm_value_type_size_internal(value_type, pointer_size) / 4;
}
/**
* Return the byte size of value type.
*/
inline static uint32
wasm_value_type_size(uint8 value_type)
{
switch (value_type) {
case VALUE_TYPE_I32:
case VALUE_TYPE_F32:
#if WASM_ENABLE_REF_TYPES != 0
case VALUE_TYPE_FUNCREF:
case VALUE_TYPE_EXTERNREF:
#endif
return sizeof(int32);
case VALUE_TYPE_I64:
case VALUE_TYPE_F64:
return sizeof(int64);
#if WASM_ENABLE_SIMD != 0
case VALUE_TYPE_V128:
return sizeof(int64) * 2;
#endif
case VALUE_TYPE_VOID:
return 0;
default:
bh_assert(0);
}
return 0;
return wasm_value_type_size_internal(value_type, sizeof(uintptr_t));
}
inline static uint16
@ -813,70 +1231,152 @@ wasm_value_type_cell_num_outside(uint8 value_type)
}
#endif
#if WASM_ENABLE_GC == 0
inline static bool
wasm_type_equal(const WASMType *type1, const WASMType *type2)
wasm_type_equal(const WASMType *type1, const WASMType *type2,
const WASMTypePtr *types, uint32 type_count)
{
const WASMFuncType *func_type1 = (const WASMFuncType *)type1;
const WASMFuncType *func_type2 = (const WASMFuncType *)type2;
if (type1 == type2) {
return true;
}
return (type1->param_count == type2->param_count
&& type1->result_count == type2->result_count
&& memcmp(type1->types, type2->types,
(uint32)(type1->param_count + type1->result_count))
return (func_type1->param_count == func_type2->param_count
&& func_type1->result_count == func_type2->result_count
&& memcmp(
func_type1->types, func_type2->types,
(uint32)(func_type1->param_count + func_type1->result_count))
== 0)
? true
: false;
(void)types;
(void)type_count;
}
#else
/* implemented in gc_type.c */
bool
wasm_type_equal(const WASMType *type1, const WASMType *type2,
const WASMTypePtr *types, uint32 type_count);
#endif
inline static uint32
wasm_get_smallest_type_idx(WASMType **types, uint32 type_count,
wasm_get_smallest_type_idx(const WASMTypePtr *types, uint32 type_count,
uint32 cur_type_idx)
{
uint32 i;
for (i = 0; i < cur_type_idx; i++) {
if (wasm_type_equal(types[cur_type_idx], types[i]))
if (wasm_type_equal(types[cur_type_idx], types[i], types, type_count))
return i;
}
(void)type_count;
return cur_type_idx;
}
#if WASM_ENABLE_GC == 0
static inline uint32
block_type_get_param_types(BlockType *block_type, uint8 **p_param_types)
#else
static inline uint32
block_type_get_param_types(BlockType *block_type, uint8 **p_param_types,
WASMRefTypeMap **p_param_reftype_maps,
uint32 *p_param_reftype_map_count)
#endif
{
uint32 param_count = 0;
if (!block_type->is_value_type) {
WASMType *wasm_type = block_type->u.type;
*p_param_types = wasm_type->types;
param_count = wasm_type->param_count;
WASMFuncType *func_type = block_type->u.type;
*p_param_types = func_type->types;
param_count = func_type->param_count;
#if WASM_ENABLE_GC != 0
*p_param_reftype_maps = func_type->ref_type_maps;
*p_param_reftype_map_count =
func_type->result_ref_type_maps - func_type->ref_type_maps;
#endif
}
else {
*p_param_types = NULL;
param_count = 0;
#if WASM_ENABLE_GC != 0
*p_param_reftype_maps = NULL;
*p_param_reftype_map_count = 0;
#endif
}
return param_count;
}
#if WASM_ENABLE_GC == 0
static inline uint32
block_type_get_result_types(BlockType *block_type, uint8 **p_result_types)
#else
static inline uint32
block_type_get_result_types(BlockType *block_type, uint8 **p_result_types,
WASMRefTypeMap **p_result_reftype_maps,
uint32 *p_result_reftype_map_count)
#endif
{
uint32 result_count = 0;
uint8 *result_types = NULL;
#if WASM_ENABLE_GC != 0
uint8 type;
uint32 result_reftype_map_count = 0;
WASMRefTypeMap *result_reftype_maps = NULL;
#endif
if (block_type->is_value_type) {
if (block_type->u.value_type != VALUE_TYPE_VOID) {
*p_result_types = &block_type->u.value_type;
if (block_type->u.value_type.type != VALUE_TYPE_VOID) {
result_types = &block_type->u.value_type.type;
result_count = 1;
#if WASM_ENABLE_GC != 0
type = block_type->u.value_type.type;
if (type == (uint8)REF_TYPE_HT_NULLABLE
|| type == (uint8)REF_TYPE_HT_NON_NULLABLE) {
result_reftype_maps = &block_type->u.value_type.ref_type_map;
result_reftype_map_count = 1;
}
#endif
}
}
else {
WASMType *wasm_type = block_type->u.type;
*p_result_types = wasm_type->types + wasm_type->param_count;
result_count = wasm_type->result_count;
WASMFuncType *func_type = block_type->u.type;
result_types = func_type->types + func_type->param_count;
result_count = func_type->result_count;
#if WASM_ENABLE_GC != 0
result_reftype_maps = func_type->result_ref_type_maps;
result_reftype_map_count = (uint32)(func_type->ref_type_map_count
- (func_type->result_ref_type_maps
- func_type->ref_type_maps));
#endif
}
*p_result_types = result_types;
#if WASM_ENABLE_GC != 0
*p_result_reftype_maps = result_reftype_maps;
*p_result_reftype_map_count = result_reftype_map_count;
#endif
return result_count;
}
static inline uint32
block_type_get_arity(const BlockType *block_type, uint8 label_type)
{
if (label_type == LABEL_TYPE_LOOP) {
if (block_type->is_value_type)
return 0;
else
return block_type->u.type->param_count;
}
else {
if (block_type->is_value_type) {
return block_type->u.value_type.type != VALUE_TYPE_VOID ? 1 : 0;
}
else
return block_type->u.type->result_count;
}
return 0;
}
#ifdef __cplusplus
} /* end of extern "C" */
#endif