Browse Source

* WASI: preserve the initial value of the stack pointer on startup, so that we
can later use the area after that, until the end of memory as an initial heap

Nikolay Nikolov 1 year ago
parent
commit
7d1999eedb
3 changed files with 44 additions and 4 deletions
  1. 18 2
      rtl/wasi/si_dll.pp
  2. 18 2
      rtl/wasi/si_prc.pp
  3. 8 0
      rtl/wasi/sysheap.inc

+ 18 - 2
rtl/wasi/si_dll.pp

@@ -32,7 +32,7 @@ procedure PASCALMAIN; external 'PASCALMAIN';
 {$if defined(FPC_WASM_BRANCHFUL_EXCEPTIONS) or defined(FPC_WASM_NATIVE_EXCEPTIONS)}
 {$if defined(FPC_WASM_BRANCHFUL_EXCEPTIONS) or defined(FPC_WASM_NATIVE_EXCEPTIONS)}
 Procedure DoUnHandledException; external name 'FPC_DOUNHANDLEDEXCEPTION';
 Procedure DoUnHandledException; external name 'FPC_DOUNHANDLEDEXCEPTION';
 
 
-procedure _initialize;
+procedure _initialize_pascal;
 begin
 begin
   try
   try
     PASCALMAIN;
     PASCALMAIN;
@@ -41,12 +41,28 @@ begin
   end;
   end;
 end;
 end;
 {$else}
 {$else}
-procedure _initialize;
+procedure _initialize_pascal;
 begin
 begin
   PASCALMAIN;
   PASCALMAIN;
 end;
 end;
 {$endif}
 {$endif}
 
 
+procedure SetInitialHeapBlockStart(p: Pointer); external name 'FPC_WASM_SETINITIALHEAPBLOCKSTART';
+
+{ TODO: remove this, when calling SetInitialHeapBlockStart works directly from within inline asm }
+procedure SetInitialHeapBlockStart2(p: Pointer);
+begin
+  SetInitialHeapBlockStart(p);
+end;
+
+procedure _initialize; assembler; nostackframe;
+asm
+  global.get $__stack_pointer
+  call $SetInitialHeapBlockStart2
+
+  call $_initialize_pascal
+end;
+
 exports
 exports
   _initialize,
   _initialize,
   _initialize name '_initialize_promising' promising;
   _initialize name '_initialize_promising' promising;

+ 18 - 2
rtl/wasi/si_prc.pp

@@ -32,7 +32,7 @@ procedure PASCALMAIN; external 'PASCALMAIN';
 {$if defined(FPC_WASM_BRANCHFUL_EXCEPTIONS) or defined(FPC_WASM_NATIVE_EXCEPTIONS)}
 {$if defined(FPC_WASM_BRANCHFUL_EXCEPTIONS) or defined(FPC_WASM_NATIVE_EXCEPTIONS)}
 Procedure DoUnHandledException; external name 'FPC_DOUNHANDLEDEXCEPTION';
 Procedure DoUnHandledException; external name 'FPC_DOUNHANDLEDEXCEPTION';
 
 
-procedure _start;
+procedure _start_pascal;
 begin
 begin
   try
   try
     PASCALMAIN;
     PASCALMAIN;
@@ -41,12 +41,28 @@ begin
   end;
   end;
 end;
 end;
 {$else}
 {$else}
-procedure _start;
+procedure _start_pascal;
 begin
 begin
   PASCALMAIN;
   PASCALMAIN;
 end;
 end;
 {$endif}
 {$endif}
 
 
+procedure SetInitialHeapBlockStart(p: Pointer); external name 'FPC_WASM_SETINITIALHEAPBLOCKSTART';
+
+{ TODO: remove this, when calling SetInitialHeapBlockStart works directly from within inline asm }
+procedure SetInitialHeapBlockStart2(p: Pointer);
+begin
+  SetInitialHeapBlockStart(p);
+end;
+
+procedure _start; assembler; nostackframe;
+asm
+  global.get $__stack_pointer
+  call $SetInitialHeapBlockStart2
+
+  call $_start_pascal
+end;
+
 exports
 exports
   _start,
   _start,
   _start name '_start_promising' promising;
   _start name '_start_promising' promising;

+ 8 - 0
rtl/wasi/sysheap.inc

@@ -20,6 +20,14 @@
                               Heap Management
                               Heap Management
 *****************************************************************************}
 *****************************************************************************}
 
 
+var
+  InitialHeapBlockStart: Pointer;
+
+procedure SetInitialHeapBlockStart(p: Pointer);[Public, Alias : 'FPC_WASM_SETINITIALHEAPBLOCKSTART'];
+begin
+  InitialHeapBlockStart:=p;
+end;
+
 function SysOSAlloc(size: ptruint): pointer;
 function SysOSAlloc(size: ptruint): pointer;
 const
 const
   page_size = 65536;
   page_size = 65536;