Răsfoiți Sursa

* 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.

Nikolay Nikolov 3 ani în urmă
părinte
comite
3c58f26e83

+ 14 - 0
compiler/hlcgobj.pas

@@ -437,6 +437,14 @@ unit hlcgobj;
           }
           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
           }
@@ -3346,6 +3354,12 @@ implementation
     end;
 
 
+  procedure thlcgobj.g_maybe_checkforexceptions(list : TAsmList);
+    begin
+      { do nothing by default }
+    end;
+
+
   procedure thlcgobj.g_unreachable(list: TAsmList);
     begin
       { nothing }

+ 9 - 9
compiler/wasm32/hlcgcpu.pas

@@ -148,7 +148,7 @@ uses
       { Wasm-specific routines }
 
       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);
       { 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));
     end;
 
-  procedure thlcgwasm.g_checkexceptions(list: TasmList);
+  procedure thlcgwasm.g_maybe_checkforexceptions(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;
+      if ts_wasm_bf_exceptions in current_settings.targetswitches then
+        begin
+          pd:=search_system_proc('fpc_raised_exception_flag');
+          g_call_system_proc(list,pd,[],nil).resetiftemp;
 
-      decstack(current_asmdata.CurrAsmList,1);
+          decstack(current_asmdata.CurrAsmList,1);
 
-      list.concat(taicpu.op_const(a_br_if,br_blocks-raiseBr));
+          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);

+ 1 - 2
compiler/wasm32/nwasmcal.pas

@@ -56,8 +56,7 @@ implementation
     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);
+        hlcg.g_maybe_checkforexceptions(current_asmdata.CurrAsmList);
       end;
 
     procedure twasmcallnode.do_release_unused_return_value;

+ 4 - 4
compiler/wasm32/nwasmflw.pas

@@ -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_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));
                 thlcgwasm(hlcg).decblock;
@@ -838,7 +838,7 @@ implementation
         else
           begin
             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;
           end;
 
@@ -1311,7 +1311,7 @@ implementation
           thlcgwasm(hlcg).incblock;
           thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
           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));
           thlcgwasm(hlcg).decblock;
         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_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));
         thlcgwasm(hlcg).decblock;