Skip to content

Commit

Permalink
start loader refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Zzzabiyaka committed Jan 28, 2025
1 parent 36833e4 commit 5450cae
Showing 1 changed file with 23 additions and 43 deletions.
66 changes: 23 additions & 43 deletions core/iwasm/interpreter/wasm_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,19 @@ is_64bit_type(uint8 type)
return false;
}

static int
get_type_size_bytes(uint8 type)
{
if (is_32bit_type(type))
return 4;
else if (is_64bit_type(type))
return 8;
else if (type == VALUE_TYPE_V128)
return 16;
else
return 0;
}

#if WASM_ENABLE_GC != 0
static bool
is_packed_type(uint8 type)
Expand Down Expand Up @@ -9110,22 +9123,19 @@ preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
if (loader_ctx->p_code_compiled) {
bh_assert(preserved_offset != (int16)local_index);
}
if (loader_ctx->p_code_compiled)
loader_ctx->preserved_local_offset +=
get_type_size_bytes(local_type) / 4;
if (is_32bit_type(local_type)) {
/* Only increase preserve offset in the second traversal */
if (loader_ctx->p_code_compiled)
loader_ctx->preserved_local_offset++;
emit_label(EXT_OP_COPY_STACK_TOP);
}
#if WASM_ENABLE_SIMDE != 0
else if (local_type == VALUE_TYPE_V128) {
if (loader_ctx->p_code_compiled)
loader_ctx->preserved_local_offset += 4;
emit_label(EXT_OP_COPY_STACK_TOP_V128);
}
#endif
else {
if (loader_ctx->p_code_compiled)
loader_ctx->preserved_local_offset += 2;
emit_label(EXT_OP_COPY_STACK_TOP_I64);
}
emit_operand(loader_ctx, local_index);
Expand All @@ -9135,15 +9145,7 @@ preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
loader_ctx->frame_offset_bottom[i] = preserved_offset;
}

if (cur_type == VALUE_TYPE_V128) {
i += 4;
}
else if (is_32bit_type(cur_type)) {
i++;
}
else {
i += 2;
}
i += get_type_size_bytes(cur_type) / 4;
}

(void)error_buf;
Expand Down Expand Up @@ -9172,15 +9174,7 @@ preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
return false;
}

if (cur_type == VALUE_TYPE_V128) {
i += 4;
}
else if (is_32bit_type(cur_type)) {
i++;
}
else {
i += 2;
}
i += get_type_size_bytes(cur_type) / 4;
}

return true;
Expand Down Expand Up @@ -9528,23 +9522,13 @@ wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
operand_offset = c->slot_index;
break;
}
if (is_32bit_type(c->value_type))
operand_offset += 1;
else if (c->value_type == VALUE_TYPE_V128) {
operand_offset += 4;
}
else
operand_offset += 2;

operand_offset = get_type_size_bytes(c->value_type) / 4;
}

if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
/* New constant, append to the const buffer */
if ((type == VALUE_TYPE_F64) || (type == VALUE_TYPE_I64)) {
bytes_to_increase = 2;
}
else {
bytes_to_increase = 1;
}
bytes_to_increase = get_type_size_bytes(type) / 4;

/* The max cell num of const buffer is 32768 since the valid index range
* is -32768 ~ -1. Return an invalid index 0 to indicate the buffer is
Expand Down Expand Up @@ -13034,23 +13018,21 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
else {
if (is_32bit_type(local_type)) {
emit_label(EXT_OP_SET_LOCAL_FAST);
emit_byte(loader_ctx, (uint8)local_offset);
}
else if (is_64bit_type(local_type)) {
emit_label(EXT_OP_SET_LOCAL_FAST_I64);
emit_byte(loader_ctx, (uint8)local_offset);
}
#if WASM_ENABLE_SIMDE != 0
else if (local_type == VALUE_TYPE_V128) {
emit_label(EXT_OP_SET_LOCAL_FAST_V128);
emit_byte(loader_ctx, (uint8)local_offset);
}
#endif
else {
set_error_buf(error_buf, error_buf_size,
"unknown local type");
goto fail;
}
emit_byte(loader_ctx, (uint8)local_offset);
POP_OFFSET_TYPE(local_type);
}
}
Expand Down Expand Up @@ -13121,18 +13103,16 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
skip_label();
if (is_32bit_type(local_type)) {
emit_label(EXT_OP_TEE_LOCAL_FAST);
emit_byte(loader_ctx, (uint8)local_offset);
}
#if WASM_ENABLE_SIMDE != 0
else if (local_type == VALUE_TYPE_V128) {
emit_label(EXT_OP_TEE_LOCAL_FAST_V128);
emit_byte(loader_ctx, (uint8)local_offset);
}
#endif
else {
emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
emit_byte(loader_ctx, (uint8)local_offset);
}
emit_byte(loader_ctx, (uint8)local_offset);
}
else { /* local index larger than 255, reserve leb */
emit_uint32(loader_ctx, local_idx);
Expand Down

0 comments on commit 5450cae

Please sign in to comment.