瀏覽代碼

+ declare user-defined wasm globals in the llvm-mc assembly output

Nikolay Nikolov 3 年之前
父節點
當前提交
c6af4df9b9
共有 3 個文件被更改,包括 49 次插入1 次删除
  1. 10 0
      compiler/aggas.pas
  2. 33 0
      compiler/wasm32/aasmcpu.pas
  3. 6 1
      compiler/wasm32/nwasmutil.pas

+ 10 - 0
compiler/aggas.pas

@@ -1650,6 +1650,16 @@ implementation
                if tai_globaltype(hp).immutable then
                  writer.AsmWrite(', immutable');
                writer.AsmLn;
+               if tai_globaltype(hp).is_global then
+                 begin
+                   writer.AsmWrite(#9'.globl ');
+                   writer.AsmWriteLn(tai_globaltype(hp).globalname);
+                 end;
+               if not tai_globaltype(hp).is_external then
+                 begin
+                   writer.AsmWrite(tai_globaltype(hp).globalname);
+                   writer.AsmWriteLn(':');
+                 end;
              end;
            ait_functype:
              WriteFuncTypeDirective(tai_functype(hp));

+ 33 - 0
compiler/wasm32/aasmcpu.pas

@@ -30,6 +30,7 @@ uses
   globtype,globals,verbose,
   aasmbase,aasmtai,aasmdata,aasmsym,
   cgbase,cgutils,cpubase,cpuinfo,ogbase,
+  symtype,
   widestr;
 
     { fake, there are no "mov reg,reg" instructions here }
@@ -110,7 +111,12 @@ uses
         globalname: string;
         gtype: TWasmBasicType;
         immutable: boolean;
+        is_external: boolean;
+        is_global: boolean;
+        sym       : tasmsymbol;
         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);
       end;
 
       { tai_functype }
@@ -162,10 +168,37 @@ uses
     constructor tai_globaltype.create(const aglobalname: string; atype: TWasmBasicType; aimmutable: boolean);
       begin
         inherited Create;
+        sym:=current_asmdata.RefAsmSymbol(aglobalname,AT_WASM_GLOBAL);
         typ:=ait_globaltype;
         globalname:=aglobalname;
         gtype:=atype;
         immutable:=aimmutable;
+        is_external:=true;
+        is_global:=false;
+      end;
+
+    constructor tai_globaltype.create_local(const aglobalname: string; atype: TWasmBasicType; aimmutable: boolean; def: tdef);
+      begin
+        inherited Create;
+        sym:=current_asmdata.DefineAsmSymbol(aglobalname,AB_LOCAL,AT_WASM_GLOBAL,def);
+        typ:=ait_globaltype;
+        globalname:=aglobalname;
+        gtype:=atype;
+        immutable:=aimmutable;
+        is_external:=false;
+        is_global:=false;
+      end;
+
+    constructor tai_globaltype.create_global(const aglobalname: string; atype: TWasmBasicType; aimmutable: boolean; def: tdef);
+      begin
+        inherited Create;
+        sym:=current_asmdata.DefineAsmSymbol(aglobalname,AB_GLOBAL,AT_WASM_GLOBAL,def);
+        typ:=ait_globaltype;
+        globalname:=aglobalname;
+        gtype:=atype;
+        immutable:=aimmutable;
+        is_external:=false;
+        is_global:=true;
       end;
 
     { tai_import_name }

+ 6 - 1
compiler/wasm32/nwasmutil.pas

@@ -57,7 +57,12 @@ implementation
     begin
       symcpu:=tcpustaticvarsym(sym);
       if symcpu.is_wasm_global then
-        // don't reserve bss data for wasm global vars
+        begin
+          if sym.globalasmsym then
+            current_asmdata.asmlists[al_start].Concat(tai_globaltype.create_global(symcpu.mangledname,symcpu.get_wasm_global_vardef_type,false,symcpu.vardef))
+          else
+            current_asmdata.asmlists[al_start].Concat(tai_globaltype.create_local(symcpu.mangledname,symcpu.get_wasm_global_vardef_type,false,symcpu.vardef));
+        end
       else
         inherited;
     end;