Browse Source

+ WebAssembly threads internal linker: partial implementation of TWasmExeOutput.GenerateCode_InitSharedMemory

Nikolay Nikolov 1 year ago
parent
commit
8648c0353a
1 changed files with 59 additions and 3 deletions
  1. 59 3
      compiler/ogwasm.pas

+ 59 - 3
compiler/ogwasm.pas

@@ -5559,6 +5559,8 @@ implementation
       end;
       end;
 
 
     procedure TWasmExeOutput.GenerateCode_InitSharedMemory;
     procedure TWasmExeOutput.GenerateCode_InitSharedMemory;
+      const
+        InitFlagOfs=256;
       var
       var
         Sec: TObjSection;
         Sec: TObjSection;
       begin
       begin
@@ -5570,9 +5572,63 @@ implementation
         { locals }
         { locals }
         Sec.writeUInt8($00);
         Sec.writeUInt8($00);
 
 
-        { TODO: implement }
-
-        Sec.writeUInt8($0B);  { end }
+        { block }
+        Sec.writeUInt16BE($0240);
+        { block }
+        Sec.writeUInt16BE($0240);
+        { block }
+        Sec.writeUInt16BE($0240);
+
+        { i32.const $InitFlag }
+        Sec.writeUInt8($41);
+        WriteUleb5(sec,InitFlagOfs);
+        { i32.const 0 }
+        Sec.writeUInt16BE($4100);
+        { i32.const 1 }
+        Sec.writeUInt16BE($4101);
+        { i32.atomic.rmw.cmpxchg 2 0 }
+        Sec.writeUInt32BE($fe480200);
+        { br_table 0 1 2 }
+        Sec.writebytes(#$0e#$02#$00#$01#$02);
+        { end }
+        Sec.writeUInt8($0B);
+
+        { TODO: memory.init and memory.fill }
+
+        { i32.const $InitFlag }
+        Sec.writeUInt8($41);
+        WriteUleb5(sec,InitFlagOfs);
+        { i32.const 2 }
+        Sec.writeUInt16BE($4102);
+        { i32.atomic.store 2 0 }
+        Sec.writeUInt32BE($fe170200);
+        { i32.const $InitFlag }
+        Sec.writeUInt8($41);
+        WriteUleb5(sec,InitFlagOfs);
+        { i32.const 4294967295 }
+        Sec.writeUInt16BE($417f);
+        { memory.atomic.notify 2 0 }
+        Sec.writeUInt32BE($fe000200);
+        { drop }
+        Sec.writeUInt8($1A);
+        { end }
+        Sec.writeUInt8($0B);
+        { i32.const $InitFlag }
+        Sec.writeUInt8($41);
+        WriteUleb5(sec,InitFlagOfs);
+        { i32.const 1 }
+        Sec.writeUInt16BE($4101);
+        { i64.const -1 }
+        Sec.writeUInt16BE($427f);
+        { memory.atomic.wait32 2 0 }
+        Sec.writeUInt32BE($fe010200);
+        { drop }
+        Sec.writeUInt8($1A);
+        { end }
+        Sec.writeUInt8($0B);
+        { TODO: data.drop }
+        { end }
+        Sec.writeUInt8($0B);
       end;
       end;
 
 
     procedure TWasmExeOutput.WriteExeSectionToDynArray(exesec: TExeSection; dynarr: tdynamicarray);
     procedure TWasmExeOutput.WriteExeSectionToDynArray(exesec: TExeSection; dynarr: tdynamicarray);