* thlcgwasm.g_checkexceptions renamed .g_maybe_checkforexceptions, added also as

an empty virtual method in thlcgobj and modified the WebAssembly
  implementation, so that it can be called from any exceptions mode (so it
  emits no code in exception modes that don't require it, instead of generating
  an internal error). This will allow .g_maybe_checkforexceptions to be called
  from the generic parts of the code generator, after calls to systemprocs that
  could raise an exceptions.
This commit is contained in:
Nikolay Nikolov 2021-10-05 17:54:35 +03:00
parent 387c80cc57
commit 3c58f26e83
4 changed files with 28 additions and 15 deletions

View File

@ -437,6 +437,14 @@ unit hlcgobj;
} }
procedure g_exception_reason_discard(list : TAsmList; size: tdef; href: treference); virtual; procedure g_exception_reason_discard(list : TAsmList; size: tdef; href: treference); virtual;
{#
This routine is called after g_call_system_proc to a system proc,
that might raise an exception. It is used on platforms, that need
to manually check an 'exception raised' flag, like WebAssembly in
branchful exceptions mode.
}
procedure g_maybe_checkforexceptions(list : TAsmList); virtual;
{# {#
Call when the current location should never be reached Call when the current location should never be reached
} }
@ -3346,6 +3354,12 @@ implementation
end; end;
procedure thlcgobj.g_maybe_checkforexceptions(list : TAsmList);
begin
{ do nothing by default }
end;
procedure thlcgobj.g_unreachable(list: TAsmList); procedure thlcgobj.g_unreachable(list: TAsmList);
begin begin
{ nothing } { nothing }

View File

@ -148,7 +148,7 @@ uses
{ Wasm-specific routines } { Wasm-specific routines }
procedure g_procdef(list:TAsmList;pd: tprocdef); procedure g_procdef(list:TAsmList;pd: tprocdef);
procedure g_checkexceptions(list:TasmList); procedure g_maybe_checkforexceptions(list:TasmList); override;
procedure a_load_stack_reg(list : TAsmList;size: tdef;reg: tregister); procedure a_load_stack_reg(list : TAsmList;size: tdef;reg: tregister);
{ extra_slots are the slots that are used by the reference, and that { extra_slots are the slots that are used by the reference, and that
@ -2267,19 +2267,19 @@ implementation
list.Concat(tai_functype.create(pd.mangledname,tcpuprocdef(pd).create_functype)); list.Concat(tai_functype.create(pd.mangledname,tcpuprocdef(pd).create_functype));
end; end;
procedure thlcgwasm.g_checkexceptions(list: TasmList); procedure thlcgwasm.g_maybe_checkforexceptions(list: TasmList);
var var
pd: tprocdef; pd: tprocdef;
begin begin
if not (ts_wasm_bf_exceptions in current_settings.targetswitches) then if ts_wasm_bf_exceptions in current_settings.targetswitches then
internalerror(2021100501); begin
pd:=search_system_proc('fpc_raised_exception_flag');
g_call_system_proc(list,pd,[],nil).resetiftemp;
pd:=search_system_proc('fpc_raised_exception_flag'); decstack(current_asmdata.CurrAsmList,1);
g_call_system_proc(list,pd,[],nil).resetiftemp;
decstack(current_asmdata.CurrAsmList,1); list.concat(taicpu.op_const(a_br_if,br_blocks-raiseBr));
end;
list.concat(taicpu.op_const(a_br_if,br_blocks-raiseBr));
end; end;
procedure thlcgwasm.a_load_stack_reg(list: TAsmList; size: tdef; reg: tregister); procedure thlcgwasm.a_load_stack_reg(list: TAsmList; size: tdef; reg: tregister);

View File

@ -56,8 +56,7 @@ implementation
procedure twasmcallnode.extra_post_call_code; procedure twasmcallnode.extra_post_call_code;
begin begin
thlcgwasm(hlcg).g_adjust_stack_after_call(current_asmdata.CurrAsmList,procdefinition); thlcgwasm(hlcg).g_adjust_stack_after_call(current_asmdata.CurrAsmList,procdefinition);
if ts_wasm_bf_exceptions in current_settings.targetswitches then hlcg.g_maybe_checkforexceptions(current_asmdata.CurrAsmList);
thlcgwasm(hlcg).g_checkexceptions(current_asmdata.CurrAsmList);
end; end;
procedure twasmcallnode.do_release_unused_return_value; procedure twasmcallnode.do_release_unused_return_value;

View File

@ -824,7 +824,7 @@ implementation
hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_clear_exception_flag',[],nil).resetiftemp; hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_clear_exception_flag',[],nil).resetiftemp;
hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_raise_nested',[],nil).resetiftemp; hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_raise_nested',[],nil).resetiftemp;
thlcgwasm(hlcg).g_checkexceptions(current_asmdata.CurrAsmList); hlcg.g_maybe_checkforexceptions(current_asmdata.CurrAsmList);
current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_block)); current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_block));
thlcgwasm(hlcg).decblock; thlcgwasm(hlcg).decblock;
@ -838,7 +838,7 @@ implementation
else else
begin begin
hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_reraise',[],nil).resetiftemp; hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_reraise',[],nil).resetiftemp;
thlcgwasm(hlcg).g_checkexceptions(current_asmdata.CurrAsmList); hlcg.g_maybe_checkforexceptions(current_asmdata.CurrAsmList);
doobjectdestroyandreraisestate.newflowcontrol:=afteronflowcontrol; doobjectdestroyandreraisestate.newflowcontrol:=afteronflowcontrol;
end; end;
@ -1311,7 +1311,7 @@ implementation
thlcgwasm(hlcg).incblock; thlcgwasm(hlcg).incblock;
thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1); thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_reraise',[],nil).resetiftemp; hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_reraise',[],nil).resetiftemp;
thlcgwasm(hlcg).g_checkexceptions(current_asmdata.CurrAsmList); hlcg.g_maybe_checkforexceptions(current_asmdata.CurrAsmList);
current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_if)); current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_if));
thlcgwasm(hlcg).decblock; thlcgwasm(hlcg).decblock;
end; end;
@ -1817,7 +1817,7 @@ implementation
hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_clear_exception_flag',[],nil).resetiftemp; hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_clear_exception_flag',[],nil).resetiftemp;
hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_raise_nested',[],nil).resetiftemp; hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_raise_nested',[],nil).resetiftemp;
thlcgwasm(hlcg).g_checkexceptions(current_asmdata.CurrAsmList); hlcg.g_maybe_checkforexceptions(current_asmdata.CurrAsmList);
current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_block)); current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_block));
thlcgwasm(hlcg).decblock; thlcgwasm(hlcg).decblock;