Browse Source

+ parse the instructions with integer parameter i32.const and i64.const in twasmreader.HandlePlainInstruction

Nikolay Nikolov 1 year ago
parent
commit
c1c1d46c15
1 changed files with 63 additions and 2 deletions
  1. 63 2
      compiler/wasm32/rawasmtext.pas

+ 63 - 2
compiler/wasm32/rawasmtext.pas

@@ -54,6 +54,7 @@ Unit rawasmtext;
         actasmpattern_origcase: string;
         actasmpattern_origcase: string;
         actasmtoken   : tasmtoken;
         actasmtoken   : tasmtoken;
         prevasmtoken  : tasmtoken;
         prevasmtoken  : tasmtoken;
+        actinttoken   : aint;
         procedure SetupTables;
         procedure SetupTables;
         procedure GetToken;
         procedure GetToken;
         function consume(t : tasmtoken):boolean;
         function consume(t : tasmtoken):boolean;
@@ -106,9 +107,47 @@ Unit rawasmtext;
 
 
 
 
     procedure twasmreader.GetToken;
     procedure twasmreader.GetToken;
+
       var
       var
-        len: Integer;
         has_sign, is_hex, is_float: Boolean;
         has_sign, is_hex, is_float: Boolean;
+
+      function GetIntToken: aint;
+        var
+          s: string;
+          u64: UInt64;
+        begin
+          s:=actasmpattern;
+          if has_sign and (s[1]='-') then
+            begin
+              delete(s,1,1);
+              if is_hex then
+                begin
+                  delete(s,1,2);
+                  Val('$'+s,u64);
+                end
+              else
+                Val(s,u64);
+{$push} {$R-}{$Q-}
+              result:=aint(-u64);
+{$pop}
+            end
+          else
+            begin
+              if has_sign then
+                delete(s,1,1);
+              if is_hex then
+                begin
+                  delete(s,1,2);
+                  Val('$'+s,u64);
+                end
+              else
+                Val(s,u64);
+              result:=aint(u64);
+            end;
+        end;
+
+      var
+        len: Integer;
         tmpS: string;
         tmpS: string;
         tmpI, tmpCode: Integer;
         tmpI, tmpCode: Integer;
       begin
       begin
@@ -300,7 +339,10 @@ Unit rawasmtext;
               if is_float then
               if is_float then
                 actasmtoken:=AS_REALNUM
                 actasmtoken:=AS_REALNUM
               else
               else
-                actasmtoken:=AS_INTNUM;
+                begin
+                  actasmtoken:=AS_INTNUM;
+                  actinttoken:=GetIntToken;
+                end;
             end;
             end;
           '"':
           '"':
             begin
             begin
@@ -650,6 +692,7 @@ Unit rawasmtext;
             begin
             begin
               instr:=TWasmInstruction.create(TWasmOperand);
               instr:=TWasmInstruction.create(TWasmOperand);
               instr.opcode:=actopcode;
               instr.opcode:=actopcode;
+              Consume(AS_OPCODE);
               case actopcode of
               case actopcode of
                 { instructions, which require 0 operands }
                 { instructions, which require 0 operands }
                 a_nop,
                 a_nop,
@@ -710,6 +753,24 @@ Unit rawasmtext;
                 a_i64_extend16_s,
                 a_i64_extend16_s,
                 a_i64_extend32_s:
                 a_i64_extend32_s:
                   ;
                   ;
+                { instructions with an integer const operand }
+                a_i32_const,
+                a_i64_const:
+                  begin
+                    if actasmtoken=AS_INTNUM then
+                      begin
+                        instr.operands[1].opr.typ:=OPR_CONSTANT;
+                        instr.operands[1].opr.val:=actinttoken;
+                        Consume(AS_INTNUM);
+                      end
+                    else
+                      begin
+                        { error: expected integer }
+                        instr.Free;
+                        instr:=nil;
+                        Consume(AS_INTNUM);
+                      end;
+                  end;
                 else
                 else
                   internalerror(2024071401);
                   internalerror(2024071401);
               end;
               end;