Browse Source

+ added the WasmBasicType to TWasmGlobalAsmSymbol. This will help WebAssembly
validation and stack tracking.

Nikolay Nikolov 1 year ago
parent
commit
ab65c9889b
3 changed files with 40 additions and 10 deletions
  1. 23 4
      compiler/wasm32/aasmcpu.pas
  2. 12 5
      compiler/wasm32/hlcgcpu.pas
  3. 5 1
      compiler/wasm32/nwasminl.pas

+ 23 - 4
compiler/wasm32/aasmcpu.pas

@@ -46,6 +46,11 @@ uses
       { TWasmGlobalAsmSymbol }
 
       TWasmGlobalAsmSymbol = class(TAsmSymbol)
+      private
+        FWasmGlobalType: TWasmBasicType;
+        procedure SetWasmGlobalType(AValue: TWasmBasicType);
+      public
+        property WasmGlobalType: TWasmBasicType read FWasmGlobalType write SetWasmGlobalType;
       end;
 
       { TWasmValueStack }
@@ -328,7 +333,7 @@ uses
         immutable: boolean;
         is_external: boolean;
         is_global: boolean;
-        sym       : tasmsymbol;
+        sym       : TWasmGlobalAsmSymbol;
         constructor create(const aglobalname:string; atype: TWasmBasicType; aimmutable: boolean);
         constructor create_local(const aglobalname:string; atype: TWasmBasicType; aimmutable: boolean; def: tdef);
         constructor create_global(const aglobalname:string; atype: TWasmBasicType; aimmutable: boolean; def: tdef);
@@ -385,6 +390,17 @@ uses
     function wasm_convert_first_item_to_structured(srclist: TAsmList): tai; forward;
     procedure map_structured_asmlist_inner(l: TAsmList; f: TAsmMapFunc; blockstack: twasmstruc_stack); forward;
 
+    { TWasmGlobalAsmSymbol }
+
+    procedure TWasmGlobalAsmSymbol.SetWasmGlobalType(AValue: TWasmBasicType);
+      begin
+        if FWasmGlobalType=AValue then
+          Exit;
+        if FWasmGlobalType<>wbt_Unknown then
+          Internalerror(2024022503);
+        FWasmGlobalType:=AValue;
+      end;
+
     { TWasmValueStack }
 
     function TWasmValueStack.GetItems(AIndex: Integer): TWasmBasicType;
@@ -1657,7 +1673,8 @@ uses
     constructor tai_globaltype.create(const aglobalname: string; atype: TWasmBasicType; aimmutable: boolean);
       begin
         inherited Create;
-        sym:=current_asmdata.RefAsmSymbolByClass(TWasmGlobalAsmSymbol,aglobalname,AT_WASM_GLOBAL);
+        sym:=TWasmGlobalAsmSymbol(current_asmdata.RefAsmSymbolByClass(TWasmGlobalAsmSymbol,aglobalname,AT_WASM_GLOBAL));
+        sym.WasmGlobalType:=atype;
         typ:=ait_globaltype;
         globalname:=aglobalname;
         gtype:=atype;
@@ -1669,7 +1686,8 @@ uses
     constructor tai_globaltype.create_local(const aglobalname: string; atype: TWasmBasicType; aimmutable: boolean; def: tdef);
       begin
         inherited Create;
-        sym:=current_asmdata.DefineAsmSymbolByClass(TWasmGlobalAsmSymbol,aglobalname,AB_LOCAL,AT_WASM_GLOBAL,def);
+        sym:=TWasmGlobalAsmSymbol(current_asmdata.DefineAsmSymbolByClass(TWasmGlobalAsmSymbol,aglobalname,AB_LOCAL,AT_WASM_GLOBAL,def));
+        sym.WasmGlobalType:=atype;
         typ:=ait_globaltype;
         globalname:=aglobalname;
         gtype:=atype;
@@ -1681,7 +1699,8 @@ uses
     constructor tai_globaltype.create_global(const aglobalname: string; atype: TWasmBasicType; aimmutable: boolean; def: tdef);
       begin
         inherited Create;
-        sym:=current_asmdata.DefineAsmSymbolByClass(TWasmGlobalAsmSymbol,aglobalname,AB_GLOBAL,AT_WASM_GLOBAL,def);
+        sym:=TWasmGlobalAsmSymbol(current_asmdata.DefineAsmSymbolByClass(TWasmGlobalAsmSymbol,aglobalname,AB_GLOBAL,AT_WASM_GLOBAL,def));
+        sym.WasmGlobalType:=atype;
         typ:=ait_globaltype;
         globalname:=aglobalname;
         gtype:=atype;

+ 12 - 5
compiler/wasm32/hlcgcpu.pas

@@ -28,7 +28,7 @@ interface
 
 uses
   globtype,
-  aasmbase,aasmdata,
+  aasmbase,aasmdata,aasmcpu,
   symbase,symconst,symtype,symdef,symsym,
   node,
   cpubase, hlcgobj, cgbase, cgutils, parabase, wasmdef;
@@ -55,6 +55,7 @@ uses
                               (check d.size to determine which one of the two)
         }
       function is_methodptr_like_type(d:tdef): boolean;
+      function RefStackPointerSym: TWasmGlobalAsmSymbol;
      public
       fntypelookup : TWasmProcTypeLookup;
 
@@ -257,7 +258,7 @@ implementation
   uses
     verbose,cutils,globals,fmodule,constexp,
     defutil,cpupi,
-    aasmtai,aasmcpu,
+    aasmtai,
     symtable,symcpu,
     procinfo,cpuinfo,cgobj,cgcpu,tgobj,tgcpu,paramgr;
 
@@ -317,6 +318,12 @@ implementation
       result:=is_8byterecord or is_methodptr or is_nestedprocptr;
     end;
 
+  function thlcgwasm.RefStackPointerSym: TWasmGlobalAsmSymbol;
+    begin
+      result:=TWasmGlobalAsmSymbol(current_asmdata.RefAsmSymbolByClass(TWasmGlobalAsmSymbol,STACK_POINTER_SYM,AT_WASM_GLOBAL));
+      result.WasmGlobalType:=wbt_i32;
+    end;
+
   constructor thlcgwasm.create;
     begin
       fevalstackheight:=0;
@@ -2049,7 +2056,7 @@ implementation
 
       g_fingerprint(list);
 
-      list.Concat(taicpu.op_sym(a_global_get,current_asmdata.RefAsmSymbolByClass(TWasmGlobalAsmSymbol,STACK_POINTER_SYM,AT_WASM_GLOBAL)));
+      list.Concat(taicpu.op_sym(a_global_get,RefStackPointerSym));
       incstack(list,1);
       list.Concat(taicpu.op_ref(a_local_set,pd.base_pointer_ref));
       decstack(list,1);
@@ -2065,7 +2072,7 @@ implementation
         decstack(list,1);
         list.Concat(taicpu.op_ref(a_local_get,pd.frame_pointer_ref));
         incstack(list,1);
-        list.Concat(taicpu.op_sym(a_global_set,current_asmdata.RefAsmSymbolByClass(TWasmGlobalAsmSymbol,STACK_POINTER_SYM,AT_WASM_GLOBAL)));
+        list.Concat(taicpu.op_sym(a_global_set,RefStackPointerSym));
         decstack(list,1);
       end;
     end;
@@ -2077,7 +2084,7 @@ implementation
       pd:=tcpuprocdef(current_procinfo.procdef);
       list.Concat(taicpu.op_ref(a_local_get,pd.base_pointer_ref));
       incstack(list,1);
-      list.Concat(taicpu.op_sym(a_global_set,current_asmdata.RefAsmSymbolByClass(TWasmGlobalAsmSymbol,STACK_POINTER_SYM,AT_WASM_GLOBAL)));
+      list.Concat(taicpu.op_sym(a_global_set,RefStackPointerSym));
       decstack(list,1);
 
       list.concat(taicpu.op_none(a_return));

+ 5 - 1
compiler/wasm32/nwasminl.pas

@@ -543,8 +543,12 @@ implementation
 
 
     procedure twasminlinenode.second_tls_get(const SymStr: string);
+      var
+        sym: TWasmGlobalAsmSymbol;
       begin
-        current_asmdata.CurrAsmList.Concat(taicpu.op_sym(a_global_get,current_asmdata.RefAsmSymbolByClass(TWasmGlobalAsmSymbol,SymStr,AT_WASM_GLOBAL)));
+        sym:=TWasmGlobalAsmSymbol(current_asmdata.RefAsmSymbolByClass(TWasmGlobalAsmSymbol,SymStr,AT_WASM_GLOBAL));
+        sym.WasmGlobalType:=wbt_i32;
+        current_asmdata.CurrAsmList.Concat(taicpu.op_sym(a_global_get,sym));
         thlcgwasm(hlcg).incstack(current_asmdata.CurrAsmList,1);
 
         location_reset(location,LOC_REGISTER,def_cgsize(resultdef));