Browse Source

+ also support immutable wasm globals in the wasm assembler writer (both internal and external)

Nikolay Nikolov 3 years ago
parent
commit
b46c010f82
4 changed files with 19 additions and 6 deletions
  1. 4 1
      compiler/aggas.pas
  2. 10 2
      compiler/ogwasm.pas
  3. 4 2
      compiler/wasm32/aasmcpu.pas
  4. 1 1
      compiler/wasm32/nwasmutil.pas

+ 4 - 1
compiler/aggas.pas

@@ -1640,7 +1640,10 @@ implementation
                writer.AsmWrite(#9'.globaltype'#9);
                writer.AsmWrite(tai_globaltype(hp).globalname);
                writer.AsmWrite(', ');
-               writer.AsmWriteLn(gas_wasm_basic_type_str[tai_globaltype(hp).gtype]);
+               writer.AsmWrite(gas_wasm_basic_type_str[tai_globaltype(hp).gtype]);
+               if tai_globaltype(hp).immutable then
+                 writer.AsmWrite(', immutable');
+               writer.AsmLn;
              end;
            ait_functype:
              WriteFuncTypeDirective(tai_functype(hp));

+ 10 - 2
compiler/ogwasm.pas

@@ -70,6 +70,7 @@ interface
         ImportName: string;
         ExportName: string;
         GlobalType: TWasmBasicType;
+        GlobalIsImmutable: Boolean;
         Locals: array of TWasmBasicType;
         constructor Create(HashObjectList: TFPHashObjectList; const s: TSymStr);
         procedure AddLocal(bastyp: TWasmBasicType);
@@ -660,6 +661,7 @@ implementation
       begin
         ObjSymExtraData:=AddOrCreateObjSymbolExtraData(gt.globalname);
         ObjSymExtraData.GlobalType:=gt.gtype;
+        ObjSymExtraData.GlobalIsImmutable:=gt.immutable;
       end;
 
     procedure TWasmObjData.DeclareFuncType(ft: tai_functype);
@@ -1259,7 +1261,10 @@ implementation
                 WriteName(FWasmSections[wsiImport],objsym.Name);
                 WriteByte(FWasmSections[wsiImport],$03);  { global }
                 WriteWasmBasicType(FWasmSections[wsiImport],objsym.ExtraData.GlobalType);
-                WriteByte(FWasmSections[wsiImport],$01);  { var }
+                if objsym.ExtraData.GlobalIsImmutable then
+                  WriteByte(FWasmSections[wsiImport],$00)   { const }
+                else
+                  WriteByte(FWasmSections[wsiImport],$01);  { var }
               end;
           end;
         { import functions }
@@ -1314,7 +1319,10 @@ implementation
                     Inc(NextGlobalIndex);
                     objsym.ExtraData:=TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name));
                     WriteWasmBasicType(FWasmSections[wsiGlobal],objsym.ExtraData.GlobalType);
-                    WriteByte(FWasmSections[wsiGlobal],$01);  { 0=const, 1=var }
+                    if objsym.ExtraData.GlobalIsImmutable then
+                      WriteByte(FWasmSections[wsiGlobal],$00)   { const }
+                    else
+                      WriteByte(FWasmSections[wsiGlobal],$01);  { var }
                     { init expr }
                     case objsym.ExtraData.GlobalType of
                       wbt_i32:

+ 4 - 2
compiler/wasm32/aasmcpu.pas

@@ -109,7 +109,8 @@ uses
       tai_globaltype = class(tai)
         globalname: string;
         gtype: TWasmBasicType;
-        constructor create(const aglobalname:string; atype: TWasmBasicType);
+        immutable: boolean;
+        constructor create(const aglobalname:string; atype: TWasmBasicType; aimmutable: boolean);
       end;
 
       { tai_functype }
@@ -158,12 +159,13 @@ uses
 
     { tai_globaltype }
 
-    constructor tai_globaltype.create(const aglobalname: string; atype: TWasmBasicType);
+    constructor tai_globaltype.create(const aglobalname: string; atype: TWasmBasicType; aimmutable: boolean);
       begin
         inherited Create;
         typ:=ait_globaltype;
         globalname:=aglobalname;
         gtype:=atype;
+        immutable:=aimmutable;
       end;
 
     { tai_import_name }

+ 1 - 1
compiler/wasm32/nwasmutil.pas

@@ -69,7 +69,7 @@ implementation
 
       list:=current_asmdata.asmlists[al_start];
 
-      list.Concat(tai_globaltype.create(STACK_POINTER_SYM,wbt_i32));
+      list.Concat(tai_globaltype.create(STACK_POINTER_SYM,wbt_i32,false));
 
       if ts_wasm_native_exceptions in current_settings.targetswitches then
         list.Concat(tai_tagtype.create('__FPC_exception', []));