Przeglądaj źródła

+ implemented all the load/store instructions in the wasm internal asm
writer (previously, only i32.load and i32.store were implemented)

Nikolay Nikolov 3 lat temu
rodzic
commit
81b8574bfd
2 zmienionych plików z 132 dodań i 8 usunięć
  1. 92 8
      compiler/wasm32/aasmcpu.pas
  2. 40 0
      compiler/wasm32/cpubase.pas

+ 92 - 8
compiler/wasm32/aasmcpu.pas

@@ -611,8 +611,29 @@ uses
                   Writeln('Warning! Not implemented opcode, pass2: ', opcode, ' ', typ);
                   Writeln('Warning! Not implemented opcode, pass2: ', opcode, ' ', typ);
                 end;
                 end;
             end;
             end;
+          a_i32_load,
+          a_i64_load,
+          a_f32_load,
+          a_f64_load,
+          a_i32_load8_s,
+          a_i32_load8_u,
+          a_i32_load16_s,
+          a_i32_load16_u,
+          a_i64_load8_s,
+          a_i64_load8_u,
+          a_i64_load16_s,
+          a_i64_load16_u,
+          a_i64_load32_s,
+          a_i64_load32_u,
           a_i32_store,
           a_i32_store,
-          a_i32_load:
+          a_i64_store,
+          a_f32_store,
+          a_f64_store,
+          a_i32_store8,
+          a_i32_store16,
+          a_i64_store8,
+          a_i64_store16,
+          a_i64_store32:
             begin
             begin
               if ops<>1 then
               if ops<>1 then
                 internalerror(2021092016);
                 internalerror(2021092016);
@@ -623,7 +644,7 @@ uses
                       if assigned(ref^.symbol) then
                       if assigned(ref^.symbol) then
                         begin
                         begin
                           Result:=1+
                           Result:=1+
-                            UlebSize(2)+  { alignment: 1 shl 2 }
+                            UlebSize(natural_alignment_for_load_store(opcode))+
                             5;  { relocation, fixed size = 5 bytes }
                             5;  { relocation, fixed size = 5 bytes }
                         end
                         end
                       else
                       else
@@ -631,7 +652,7 @@ uses
                           if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
                           if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
                             internalerror(2021092018);
                             internalerror(2021092018);
                           Result:=1+
                           Result:=1+
-                            UlebSize(2)+  { alignment: 1 shl 2 }
+                            UlebSize(natural_alignment_for_load_store(opcode))+
                             UlebSize(ref^.offset);
                             UlebSize(ref^.offset);
                         end;
                         end;
                     end;
                     end;
@@ -1103,14 +1124,77 @@ uses
                   Writeln('Warning! Not implemented opcode, pass2: ', opcode, ' ', typ);
                   Writeln('Warning! Not implemented opcode, pass2: ', opcode, ' ', typ);
                 end;
                 end;
             end;
             end;
+          a_i32_load,
+          a_i64_load,
+          a_f32_load,
+          a_f64_load,
+          a_i32_load8_s,
+          a_i32_load8_u,
+          a_i32_load16_s,
+          a_i32_load16_u,
+          a_i64_load8_s,
+          a_i64_load8_u,
+          a_i64_load16_s,
+          a_i64_load16_u,
+          a_i64_load32_s,
+          a_i64_load32_u,
           a_i32_store,
           a_i32_store,
-          a_i32_load:
+          a_i64_store,
+          a_f32_store,
+          a_f64_store,
+          a_i32_store8,
+          a_i32_store16,
+          a_i64_store8,
+          a_i64_store16,
+          a_i64_store32:
             begin
             begin
               case opcode of
               case opcode of
-                a_i32_store:
-                  WriteByte($36);
                 a_i32_load:
                 a_i32_load:
                   WriteByte($28);
                   WriteByte($28);
+                a_i64_load:
+                  WriteByte($29);
+                a_f32_load:
+                  WriteByte($2A);
+                a_f64_load:
+                  WriteByte($2B);
+                a_i32_load8_s:
+                  WriteByte($2C);
+                a_i32_load8_u:
+                  WriteByte($2D);
+                a_i32_load16_s:
+                  WriteByte($2E);
+                a_i32_load16_u:
+                  WriteByte($2F);
+                a_i64_load8_s:
+                  WriteByte($30);
+                a_i64_load8_u:
+                  WriteByte($31);
+                a_i64_load16_s:
+                  WriteByte($32);
+                a_i64_load16_u:
+                  WriteByte($33);
+                a_i64_load32_s:
+                  WriteByte($34);
+                a_i64_load32_u:
+                  WriteByte($35);
+                a_i32_store:
+                  WriteByte($36);
+                a_i64_store:
+                  WriteByte($37);
+                a_f32_store:
+                  WriteByte($38);
+                a_f64_store:
+                  WriteByte($39);
+                a_i32_store8:
+                  WriteByte($3A);
+                a_i32_store16:
+                  WriteByte($3B);
+                a_i64_store8:
+                  WriteByte($3C);
+                a_i64_store16:
+                  WriteByte($3D);
+                a_i64_store32:
+                  WriteByte($3E);
                 else
                 else
                   internalerror(2021092019);
                   internalerror(2021092019);
               end;
               end;
@@ -1122,14 +1206,14 @@ uses
                     begin
                     begin
                       if assigned(ref^.symbol) then
                       if assigned(ref^.symbol) then
                         begin
                         begin
-                          WriteUleb(2);  { alignment: 1 shl 2 }
+                          WriteUleb(natural_alignment_for_load_store(opcode));
                           objdata.writeReloc(ref^.offset,5,ObjData.symbolref(ref^.symbol),RELOC_MEMORY_ADDR_LEB);
                           objdata.writeReloc(ref^.offset,5,ObjData.symbolref(ref^.symbol),RELOC_MEMORY_ADDR_LEB);
                         end
                         end
                       else
                       else
                         begin
                         begin
                           if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
                           if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
                             internalerror(2021092018);
                             internalerror(2021092018);
-                          WriteUleb(2);  { alignment: 1 shl 2 }
+                          WriteUleb(natural_alignment_for_load_store(opcode));
                           WriteUleb(ref^.offset);
                           WriteUleb(ref^.offset);
                         end;
                         end;
                     end;
                     end;

+ 40 - 0
compiler/wasm32/cpubase.pas

@@ -317,6 +317,8 @@ uses
     }
     }
     function inverse_cond(const c: TAsmCond): Tasmcond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
     function inverse_cond(const c: TAsmCond): Tasmcond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
 
 
+    function natural_alignment_for_load_store(op: TAsmOp): shortint;
+
 implementation
 implementation
 
 
 uses
 uses
@@ -396,6 +398,44 @@ uses
         internalerror(2015082701);
         internalerror(2015082701);
       end;
       end;
 
 
+    function natural_alignment_for_load_store(op: TAsmOp): shortint;
+      begin
+        case op of
+          a_i32_load8_s,
+          a_i32_load8_u,
+          a_i64_load8_s,
+          a_i64_load8_u,
+          a_i32_store8,
+          a_i64_store8:
+            result:=0;
+
+          a_i32_load16_s,
+          a_i32_load16_u,
+          a_i64_load16_s,
+          a_i64_load16_u,
+          a_i32_store16,
+          a_i64_store16:
+            result:=1;
+
+          a_i32_load,
+          a_f32_load,
+          a_i64_load32_s,
+          a_i64_load32_u,
+          a_i32_store,
+          a_f32_store,
+          a_i64_store32:
+            result:=2;
+
+          a_i64_load,
+          a_f64_load,
+          a_i64_store,
+          a_f64_store:
+            result:=3;
+          else
+            internalerror(2021092614);
+        end;
+      end;
+
 {*****************************************************************************
 {*****************************************************************************
                                   TWasmFuncType
                                   TWasmFuncType
 *****************************************************************************}
 *****************************************************************************}