Browse Source

* 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 years ago
parent
commit
3c58f26e83
4 changed files with 28 additions and 15 deletions
  1. 14 0
      compiler/hlcgobj.pas
  2. 9 9
      compiler/wasm32/hlcgcpu.pas
  3. 1 2
      compiler/wasm32/nwasmcal.pas
  4. 4 4
      compiler/wasm32/nwasmflw.pas

+ 14 - 0
compiler/hlcgobj.pas

@@ -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 }

+ 9 - 9
compiler/wasm32/hlcgcpu.pas

@@ -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
-        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;
     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);

+ 1 - 2
compiler/wasm32/nwasmcal.pas

@@ -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
-          thlcgwasm(hlcg).g_checkexceptions(current_asmdata.CurrAsmList);
+        hlcg.g_maybe_checkforexceptions(current_asmdata.CurrAsmList);
       end;
       end;
 
 
     procedure twasmcallnode.do_release_unused_return_value;
     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_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;