Skip to content

Commit b4380fb

Browse files
authored
refactoring: Re-use commit IP functionality between exception handling and other cases (bytecodealliance#3768)
1 parent 65521b1 commit b4380fb

File tree

3 files changed

+54
-66
lines changed

3 files changed

+54
-66
lines changed

core/iwasm/compilation/aot_compiler.c

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,46 @@ aot_gen_commit_values(AOTCompFrame *frame)
569569
return true;
570570
}
571571

572+
bool
573+
aot_gen_commit_ip(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
574+
LLVMValueRef ip_value, bool is_64bit)
575+
{
576+
LLVMValueRef cur_frame = func_ctx->cur_frame;
577+
LLVMValueRef value_offset, value_addr, value_ptr;
578+
uint32 offset_ip;
579+
580+
if (!comp_ctx->is_jit_mode)
581+
offset_ip = comp_ctx->pointer_size * 4;
582+
else
583+
offset_ip = offsetof(WASMInterpFrame, ip);
584+
585+
if (!(value_offset = I32_CONST(offset_ip))) {
586+
aot_set_last_error("llvm build const failed");
587+
return false;
588+
}
589+
590+
if (!(value_addr =
591+
LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, cur_frame,
592+
&value_offset, 1, "ip_addr"))) {
593+
aot_set_last_error("llvm build in bounds gep failed");
594+
return false;
595+
}
596+
597+
if (!(value_ptr = LLVMBuildBitCast(
598+
comp_ctx->builder, value_addr,
599+
is_64bit ? INT64_PTR_TYPE : INT32_PTR_TYPE, "ip_ptr"))) {
600+
aot_set_last_error("llvm build bit cast failed");
601+
return false;
602+
}
603+
604+
if (!LLVMBuildStore(comp_ctx->builder, ip_value, value_ptr)) {
605+
aot_set_last_error("llvm build store failed");
606+
return false;
607+
}
608+
609+
return true;
610+
}
611+
572612
bool
573613
aot_gen_commit_sp_ip(AOTCompFrame *frame, bool commit_sp, bool commit_ip)
574614
{
@@ -577,40 +617,19 @@ aot_gen_commit_sp_ip(AOTCompFrame *frame, bool commit_sp, bool commit_ip)
577617
LLVMValueRef cur_frame = func_ctx->cur_frame;
578618
LLVMValueRef value_offset, value_addr, value_ptr, value;
579619
LLVMTypeRef int8_ptr_ptr_type;
580-
uint32 offset_ip, offset_sp, n;
620+
uint32 offset_sp, n;
581621
bool is_64bit = (comp_ctx->pointer_size == sizeof(uint64)) ? true : false;
582622
const AOTValueSlot *sp = frame->sp;
583623
const uint8 *ip = frame->frame_ip;
584624

585625
if (!comp_ctx->is_jit_mode) {
586-
offset_ip = frame->comp_ctx->pointer_size * 4;
587626
offset_sp = frame->comp_ctx->pointer_size * 5;
588627
}
589628
else {
590-
offset_ip = offsetof(WASMInterpFrame, ip);
591629
offset_sp = offsetof(WASMInterpFrame, sp);
592630
}
593631

594632
if (commit_ip) {
595-
if (!(value_offset = I32_CONST(offset_ip))) {
596-
aot_set_last_error("llvm build const failed");
597-
return false;
598-
}
599-
600-
if (!(value_addr =
601-
LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, cur_frame,
602-
&value_offset, 1, "ip_addr"))) {
603-
aot_set_last_error("llvm build in bounds gep failed");
604-
return false;
605-
}
606-
607-
if (!(value_ptr = LLVMBuildBitCast(
608-
comp_ctx->builder, value_addr,
609-
is_64bit ? INT64_PTR_TYPE : INT32_PTR_TYPE, "ip_ptr"))) {
610-
aot_set_last_error("llvm build bit cast failed");
611-
return false;
612-
}
613-
614633
if (!comp_ctx->is_jit_mode) {
615634
WASMModule *module = comp_ctx->comp_data->wasm_module;
616635
if (is_64bit)
@@ -630,8 +649,7 @@ aot_gen_commit_sp_ip(AOTCompFrame *frame, bool commit_sp, bool commit_ip)
630649
return false;
631650
}
632651

633-
if (!LLVMBuildStore(comp_ctx->builder, value, value_ptr)) {
634-
aot_set_last_error("llvm build store failed");
652+
if (!aot_gen_commit_ip(comp_ctx, func_ctx, value, is_64bit)) {
635653
return false;
636654
}
637655
}

core/iwasm/compilation/aot_compiler.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,15 @@ aot_gen_commit_values(AOTCompFrame *frame);
195195
bool
196196
aot_gen_commit_sp_ip(AOTCompFrame *frame, bool commit_sp, bool commit_ip);
197197

198+
/**
199+
* Generate instructions to commit IP pointer to the frame.
200+
*
201+
* @param frame the frame information
202+
*/
203+
bool
204+
aot_gen_commit_ip(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
205+
LLVMValueRef ip_value, bool is_64bit);
206+
198207
bool
199208
aot_frame_store_value(AOTCompContext *comp_ctx, LLVMValueRef value,
200209
uint8 value_type, LLVMValueRef cur_frame, uint32 offset);

core/iwasm/compilation/aot_emit_exception.c

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,10 @@
44
*/
55

66
#include "aot_emit_exception.h"
7+
#include "aot_compiler.h"
78
#include "../interpreter/wasm_runtime.h"
89
#include "../aot/aot_runtime.h"
910

10-
static bool
11-
commit_ip(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
12-
LLVMValueRef exce_ip, bool is_64bit)
13-
{
14-
LLVMValueRef cur_frame = func_ctx->cur_frame;
15-
LLVMValueRef value_offset, value_addr, value_ptr;
16-
uint32 offset_ip;
17-
18-
if (!comp_ctx->is_jit_mode)
19-
offset_ip = comp_ctx->pointer_size * 4;
20-
else
21-
offset_ip = offsetof(WASMInterpFrame, ip);
22-
23-
if (!(value_offset = I32_CONST(offset_ip))) {
24-
aot_set_last_error("llvm build const failed");
25-
return false;
26-
}
27-
28-
if (!(value_addr =
29-
LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, cur_frame,
30-
&value_offset, 1, "ip_addr"))) {
31-
aot_set_last_error("llvm build in bounds gep failed");
32-
return false;
33-
}
34-
35-
if (!(value_ptr = LLVMBuildBitCast(
36-
comp_ctx->builder, value_addr,
37-
is_64bit ? INT64_PTR_TYPE : INT32_PTR_TYPE, "ip_ptr"))) {
38-
aot_set_last_error("llvm build bit cast failed");
39-
return false;
40-
}
41-
42-
if (!LLVMBuildStore(comp_ctx->builder, exce_ip, value_ptr)) {
43-
aot_set_last_error("llvm build store failed");
44-
return false;
45-
}
46-
47-
return true;
48-
}
49-
5011
bool
5112
aot_emit_exception(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
5213
int32 exception_id, bool is_cond_br, LLVMValueRef cond_br_if,
@@ -90,8 +51,8 @@ aot_emit_exception(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
9051
}
9152

9253
/* Commit ip to current frame */
93-
if (!commit_ip(comp_ctx, func_ctx, func_ctx->exception_ip_phi,
94-
is_64bit)) {
54+
if (!aot_gen_commit_ip(comp_ctx, func_ctx,
55+
func_ctx->exception_ip_phi, is_64bit)) {
9556
return false;
9657
}
9758
}

0 commit comments

Comments
 (0)