Prechádzať zdrojové kódy

+ implemented memory.grow and memory.size intrinsics and implemented SysOSAlloc

git-svn-id: branches/wasm@48290 -
nickysn 4 rokov pred
rodič
commit
54299874d3

+ 2 - 0
.gitattributes

@@ -975,6 +975,7 @@ compiler/wasm32/aasmcpu.pas svneol=native#text/plain
 compiler/wasm32/agbinaryen.pas svneol=native#text/plain
 compiler/wasm32/agbinaryen.pas svneol=native#text/plain
 compiler/wasm32/agllvmmc.pas svneol=native#text/plain
 compiler/wasm32/agllvmmc.pas svneol=native#text/plain
 compiler/wasm32/agwat.pas svneol=native#text/plain
 compiler/wasm32/agwat.pas svneol=native#text/plain
+compiler/wasm32/ccpuinnr.inc svneol=native#text/plain
 compiler/wasm32/cgcpu.pas svneol=native#text/plain
 compiler/wasm32/cgcpu.pas svneol=native#text/plain
 compiler/wasm32/cpubase.pas svneol=native#text/plain
 compiler/wasm32/cpubase.pas svneol=native#text/plain
 compiler/wasm32/cpuinfo.pas svneol=native#text/plain
 compiler/wasm32/cpuinfo.pas svneol=native#text/plain
@@ -12199,6 +12200,7 @@ rtl/wasi/sysos.inc svneol=native#text/plain
 rtl/wasi/sysosh.inc svneol=native#text/plain
 rtl/wasi/sysosh.inc svneol=native#text/plain
 rtl/wasi/system.pp svneol=native#text/plain
 rtl/wasi/system.pp svneol=native#text/plain
 rtl/wasm32/cpuh.inc svneol=native#text/plain
 rtl/wasm32/cpuh.inc svneol=native#text/plain
+rtl/wasm32/cpuinnr.inc svneol=native#text/plain
 rtl/wasm32/int64p.inc svneol=native#text/plain
 rtl/wasm32/int64p.inc svneol=native#text/plain
 rtl/wasm32/makefile.cpu svneol=native#text/plain
 rtl/wasm32/makefile.cpu svneol=native#text/plain
 rtl/wasm32/math.inc svneol=native#text/plain
 rtl/wasm32/math.inc svneol=native#text/plain

+ 4 - 0
compiler/compinnr.pas

@@ -202,6 +202,10 @@ type
 {$if defined(Z80)}
 {$if defined(Z80)}
      ,
      ,
      {$i ccpuinnr.inc}
      {$i ccpuinnr.inc}
+{$endif}
+{$if defined(WASM32)}
+     ,
+     {$i ccpuinnr.inc}
 {$endif}
 {$endif}
    );
    );
 
 

+ 17 - 0
compiler/wasm32/ccpuinnr.inc

@@ -0,0 +1,17 @@
+{
+
+    This file is part of the Free Pascal run time library.
+    Copyright (c) 2021 by the Free Pascal development team.
+
+    See the file COPYING.FPC, included in this distribution,
+    for details about the copyright.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+ **********************************************************************}
+
+  in_wasm32_memory_size = in_cpu_first,
+  in_wasm32_memory_grow = in_cpu_first+1
+

+ 1 - 1
compiler/wasm32/itcpugas.pas

@@ -82,7 +82,7 @@ interface
       'i32.load8_u', 'i32.load16_u', 'i64.load8_u', 'i64.load16_u', 'i64.load32_u',
       'i32.load8_u', 'i32.load16_u', 'i64.load8_u', 'i64.load16_u', 'i64.load32_u',
       'i32.store8', 'i32.store16', 'i64.store8', 'i64.store16', 'i64.store32',
       'i32.store8', 'i32.store16', 'i64.store8', 'i64.store16', 'i64.store32',
       // additional memory
       // additional memory
-      'grow_memory', 'current_memory'
+      'memory.grow 0', 'memory.size 0'
     );
     );
 
 
     gas_wasm_basic_type_str : array [TWasmBasicType] of string = ('i32','i64','f32','f64');
     gas_wasm_basic_type_str : array [TWasmBasicType] of string = ('i32','i64','f32','f64');

+ 80 - 2
compiler/wasm32/nwasminl.pas

@@ -26,21 +26,27 @@ unit nwasminl;
 interface
 interface
 
 
     uses
     uses
-      ncginl;
+      node,ncginl;
 
 
     type
     type
 
 
       { twasminlinenode }
       { twasminlinenode }
 
 
       twasminlinenode = class(tcginlinenode)
       twasminlinenode = class(tcginlinenode)
+      private
+        procedure second_memory_size;
+        procedure second_memory_grow;
       public
       public
+        function pass_typecheck_cpu: tnode; override;
+        function first_cpu: tnode; override;
+        procedure pass_generate_code_cpu; override;
         procedure second_length;override;
         procedure second_length;override;
       end;
       end;
 
 
 implementation
 implementation
 
 
     uses
     uses
-      ninl,
+      ninl,compinnr,
       cpubase,
       cpubase,
       aasmbase,aasmdata,aasmcpu,
       aasmbase,aasmdata,aasmcpu,
       cgbase,cgutils,
       cgbase,cgutils,
@@ -52,6 +58,78 @@ implementation
                                twasminlinenode
                                twasminlinenode
 *****************************************************************************}
 *****************************************************************************}
 
 
+    procedure twasminlinenode.second_memory_size;
+      begin
+        current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_current_memory));
+        thlcgwasm(hlcg).incstack(current_asmdata.CurrAsmList,1);
+
+        location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
+        location.register:=hlcg.getregisterfordef(current_asmdata.CurrAsmList,resultdef);
+        thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
+      end;
+
+
+    procedure twasminlinenode.second_memory_grow;
+      begin
+        secondpass(left);
+
+        hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
+        thlcgwasm(hlcg).a_load_reg_stack(current_asmdata.CurrAsmList,left.resultdef,left.location.register);
+
+        current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_grow_memory));
+
+        location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
+        location.register:=hlcg.getregisterfordef(current_asmdata.CurrAsmList,resultdef);
+        thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
+      end;
+
+
+    function twasminlinenode.pass_typecheck_cpu: tnode;
+      begin
+        Result:=nil;
+        case inlinenumber of
+          in_wasm32_memory_size:
+            begin
+              CheckParameters(0);
+              resultdef:=u32inttype;
+            end;
+          in_wasm32_memory_grow:
+            begin
+              CheckParameters(1);
+              resultdef:=u32inttype;
+            end;
+          else
+            Result:=inherited pass_typecheck_cpu;
+        end;
+      end;
+
+
+    function twasminlinenode.first_cpu: tnode;
+      begin
+        Result:=nil;
+        case inlinenumber of
+          in_wasm32_memory_size,
+          in_wasm32_memory_grow:
+            expectloc:=LOC_REGISTER;
+          else
+            Result:=inherited first_cpu;
+        end;
+      end;
+
+
+    procedure twasminlinenode.pass_generate_code_cpu;
+      begin
+        case inlinenumber of
+          in_wasm32_memory_size:
+            second_memory_size;
+          in_wasm32_memory_grow:
+            second_memory_grow;
+          else
+            inherited pass_generate_code_cpu;
+        end;
+      end;
+
+
     procedure twasminlinenode.second_length;
     procedure twasminlinenode.second_length;
       var
       var
         lendef : tdef;
         lendef : tdef;

+ 7 - 1
rtl/wasi/sysheap.inc

@@ -21,9 +21,15 @@
 *****************************************************************************}
 *****************************************************************************}
 
 
 function SysOSAlloc(size: ptruint): pointer;
 function SysOSAlloc(size: ptruint): pointer;
+const
+  page_size = 65536;
+  err = high(longword);
 begin
 begin
   DebugWriteLn('SysOSAlloc');
   DebugWriteLn('SysOSAlloc');
-  SysOSAlloc:=nil;
+  SysOSAlloc:=pointer(fpc_wasm32_memory_size*page_size);
+  if fpc_wasm32_memory_grow((size + page_size - 1) div page_size) = err then
+    SysOSAlloc:=nil;
+  DebugWriteLn('SysOSAlloc done');
 end;
 end;
 
 
 procedure SysOSFree(p: pointer; size: ptruint);
 procedure SysOSFree(p: pointer; size: ptruint);

+ 7 - 1
rtl/wasm32/cpuh.inc

@@ -1,7 +1,7 @@
 {
 {
 
 
     This file is part of the Free Pascal run time library.
     This file is part of the Free Pascal run time library.
-    Copyright (c) 2016 by the Free Pascal development team.
+    Copyright (c) 2021 by the Free Pascal development team.
 
 
     CPU specific system unit header file
     CPU specific system unit header file
 
 
@@ -13,3 +13,9 @@
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
 
  **********************************************************************}
  **********************************************************************}
+
+const
+  {$I cpuinnr.inc}
+
+function fpc_wasm32_memory_size: longword;[internproc:fpc_in_wasm32_memory_size];
+function fpc_wasm32_memory_grow(n: longword): longword;[internproc:fpc_in_wasm32_memory_grow];

+ 17 - 0
rtl/wasm32/cpuinnr.inc

@@ -0,0 +1,17 @@
+{
+
+    This file is part of the Free Pascal run time library.
+    Copyright (c) 2021 by the Free Pascal development team.
+
+    See the file COPYING.FPC, included in this distribution,
+    for details about the copyright.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+ **********************************************************************}
+
+  fpc_in_wasm32_memory_size = fpc_in_cpu_first;
+  fpc_in_wasm32_memory_grow = fpc_in_cpu_first+1;
+