+ insert exception flag check and branch after each function call, when

compiling in WebAssembly branchful exceptions mode
This commit is contained in:
Nikolay Nikolov 2021-10-05 02:26:13 +03:00
parent 5be1bfeeca
commit 7afb665c92
4 changed files with 39 additions and 1 deletions

View File

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

View File

@ -49,13 +49,15 @@ interface
implementation
uses
globtype, aasmdata, defutil, tgobj, hlcgcpu, symconst, symcpu;
globals, globtype, aasmdata, defutil, tgobj, hlcgcpu, symconst, symcpu;
{ twasmcallnode }
procedure twasmcallnode.extra_post_call_code;
begin
thlcgwasm(hlcg).g_adjust_stack_after_call(current_asmdata.CurrAsmList,procdefinition);
if ts_wasm_bf_exceptions in current_settings.targetswitches then
thlcgwasm(hlcg).g_checkexceptions(current_asmdata.CurrAsmList);
end;
procedure twasmcallnode.do_release_unused_return_value;

View File

@ -715,6 +715,10 @@ procedure fpc_dispatch_by_id(Result: Pointer; const Dispatch: pointer;DispDesc:
Function fpc_PushExceptAddr (Ft: {$ifdef CPU16}SmallInt{$else}Longint{$endif};_buf,_newaddr : pointer): PJmp_buf ; compilerproc;
Procedure fpc_PopAddrStack; compilerproc;
{$endif}
{$ifdef FPC_WASM_BRANCHFUL_EXCEPTIONS}
function fpc_raised_exception_flag: Boolean;compilerproc;
procedure fpc_clear_exception_flag;compilerproc;
{$endif FPC_WASM_BRANCHFUL_EXCEPTIONS}
procedure fpc_Raiseexception (Obj : TObject; AnAddr : CodePointer; AFrame : Pointer); compilerproc;
function fpc_PopObjectStack : TObject; compilerproc;
function fpc_PopSecondObjectStack : TObject; compilerproc;

View File

@ -30,6 +30,16 @@ Var
{$i psabieh.inc}
{$endif}
function fpc_raised_exception_flag: Boolean;[public,alias:'FPC_RAISED_EXCEPTION_FLAG']compilerproc;
begin
result:=RaisedException;
end;
procedure fpc_clear_exception_flag;[public,alias:'FPC_CLEAR_EXCEPTION_FLAG']compilerproc;
begin
RaisedException:=false;
end;
Function RaiseList : PExceptObject;
begin
RaiseList:=ExceptObjectStack;