Browse Source

+ implemented encoding of the most simple wasm instructions

Nikolay Nikolov 3 years ago
parent
commit
5b3efc4b38
1 changed files with 453 additions and 1 deletions
  1. 453 1
      compiler/wasm32/aasmcpu.pas

+ 453 - 1
compiler/wasm32/aasmcpu.pas

@@ -29,7 +29,7 @@ uses
   cclasses,
   cclasses,
   globtype,globals,verbose,
   globtype,globals,verbose,
   aasmbase,aasmtai,aasmdata,aasmsym,
   aasmbase,aasmtai,aasmdata,aasmsym,
-  cgbase,cgutils,cpubase,cpuinfo,
+  cgbase,cgutils,cpubase,cpuinfo,ogbase,
   widestr;
   widestr;
 
 
     { fake, there are no "mov reg,reg" instructions here }
     { fake, there are no "mov reg,reg" instructions here }
@@ -67,6 +67,9 @@ uses
 
 
          { register spilling code }
          { register spilling code }
          function spilling_get_operation_type(opnr: longint): topertype;override;
          function spilling_get_operation_type(opnr: longint): topertype;override;
+
+         function Pass1(objdata:TObjData):longint;override;
+         procedure Pass2(objdata:TObjData);override;
       end;
       end;
 
 
       tai_align = class(tai_align_abstract)
       tai_align = class(tai_align_abstract)
@@ -325,6 +328,455 @@ implementation
       end;
       end;
 
 
 
 
+    function taicpu.Pass1(objdata: TObjData): longint;
+      begin
+        case opcode of
+          a_unreachable,
+          a_nop,
+          a_return,
+          a_drop,
+          a_i32_eqz,
+          a_i32_eq,
+          a_i32_ne,
+          a_i32_lt_s,
+          a_i32_lt_u,
+          a_i32_gt_s,
+          a_i32_gt_u,
+          a_i32_le_s,
+          a_i32_le_u,
+          a_i32_ge_s,
+          a_i32_ge_u,
+          a_i64_eqz,
+          a_i64_eq,
+          a_i64_ne,
+          a_i64_lt_s,
+          a_i64_lt_u,
+          a_i64_gt_s,
+          a_i64_gt_u,
+          a_i64_le_s,
+          a_i64_le_u,
+          a_i64_ge_s,
+          a_i64_ge_u,
+          a_f32_eq,
+          a_f32_ne,
+          a_f32_lt,
+          a_f32_gt,
+          a_f32_le,
+          a_f32_ge,
+          a_f64_eq,
+          a_f64_ne,
+          a_f64_lt,
+          a_f64_gt,
+          a_f64_le,
+          a_f64_ge,
+          a_i32_clz,
+          a_i32_ctz,
+          a_i32_popcnt,
+          a_i32_add,
+          a_i32_sub,
+          a_i32_mul,
+          a_i32_div_s,
+          a_i32_div_u,
+          a_i32_rem_s,
+          a_i32_rem_u,
+          a_i32_and,
+          a_i32_or,
+          a_i32_xor,
+          a_i32_shl,
+          a_i32_shr_s,
+          a_i32_shr_u,
+          a_i32_rotl,
+          a_i32_rotr,
+          a_i64_clz,
+          a_i64_ctz,
+          a_i64_popcnt,
+          a_i64_add,
+          a_i64_sub,
+          a_i64_mul,
+          a_i64_div_s,
+          a_i64_div_u,
+          a_i64_rem_s,
+          a_i64_rem_u,
+          a_i64_and,
+          a_i64_or,
+          a_i64_xor,
+          a_i64_shl,
+          a_i64_shr_s,
+          a_i64_shr_u,
+          a_i64_rotl,
+          a_i64_rotr,
+          a_f32_abs,
+          a_f32_neg,
+          a_f32_ceil,
+          a_f32_floor,
+          a_f32_trunc,
+          a_f32_nearest,
+          a_f32_sqrt,
+          a_f32_add,
+          a_f32_sub,
+          a_f32_mul,
+          a_f32_div,
+          a_f32_min,
+          a_f32_max,
+          a_f32_copysign,
+          a_f64_abs,
+          a_f64_neg,
+          a_f64_ceil,
+          a_f64_floor,
+          a_f64_trunc,
+          a_f64_nearest,
+          a_f64_sqrt,
+          a_f64_add,
+          a_f64_sub,
+          a_f64_mul,
+          a_f64_div,
+          a_f64_min,
+          a_f64_max,
+          a_f64_copysign,
+          a_i32_wrap_i64,
+          a_i32_trunc_f32_s,
+          a_i32_trunc_f32_u,
+          a_i32_trunc_f64_s,
+          a_i32_trunc_f64_u,
+          a_i64_extend_i32_s,
+          a_i64_extend_i32_u,
+          a_i64_trunc_f32_s,
+          a_i64_trunc_f32_u,
+          a_i64_trunc_f64_s,
+          a_i64_trunc_f64_u,
+          a_f32_convert_i32_s,
+          a_f32_convert_i32_u,
+          a_f32_convert_i64_s,
+          a_f32_convert_i64_u,
+          a_f32_demote_f64,
+          a_f64_convert_i32_s,
+          a_f64_convert_i32_u,
+          a_f64_convert_i64_s,
+          a_f64_convert_i64_u,
+          a_f64_promote_f32,
+          a_i32_reinterpret_f32,
+          a_i64_reinterpret_f64,
+          a_f32_reinterpret_i32,
+          a_f64_reinterpret_i64,
+          a_i32_extend8_s,
+          a_i32_extend16_s,
+          a_i64_extend8_s,
+          a_i64_extend16_s,
+          a_i64_extend32_s,
+          a_end_block,
+          a_end_if,
+          a_end_loop,
+          a_end_try,
+          a_catch_all:
+            result:=1;
+          a_memory_size,
+          a_memory_grow:
+            result:=2;
+          else
+            begin
+              Writeln('Warning! Not implemented opcode, pass1: ', opcode);
+              result:=0;
+            end;
+        end;
+      end;
+
+
+    procedure taicpu.Pass2(objdata: TObjData);
+
+        procedure WriteByte(b: byte);
+          begin
+            objdata.writebytes(b,1);
+          end;
+
+      begin
+        case opcode of
+          a_unreachable:
+            WriteByte($00);
+          a_nop:
+            WriteByte($01);
+          a_return:
+            WriteByte($0F);
+          a_drop:
+            WriteByte($1A);
+          a_memory_size:
+            begin
+              WriteByte($3F);
+              WriteByte($00);
+            end;
+          a_memory_grow:
+            begin
+              WriteByte($40);
+              WriteByte($00);
+            end;
+          a_i32_eqz:
+            WriteByte($45);
+          a_i32_eq:
+            WriteByte($46);
+          a_i32_ne:
+            WriteByte($47);
+          a_i32_lt_s:
+            WriteByte($48);
+          a_i32_lt_u:
+            WriteByte($49);
+          a_i32_gt_s:
+            WriteByte($4A);
+          a_i32_gt_u:
+            WriteByte($4B);
+          a_i32_le_s:
+            WriteByte($4C);
+          a_i32_le_u:
+            WriteByte($4D);
+          a_i32_ge_s:
+            WriteByte($4E);
+          a_i32_ge_u:
+            WriteByte($4F);
+          a_i64_eqz:
+            WriteByte($50);
+          a_i64_eq:
+            WriteByte($51);
+          a_i64_ne:
+            WriteByte($52);
+          a_i64_lt_s:
+            WriteByte($53);
+          a_i64_lt_u:
+            WriteByte($54);
+          a_i64_gt_s:
+            WriteByte($55);
+          a_i64_gt_u:
+            WriteByte($56);
+          a_i64_le_s:
+            WriteByte($57);
+          a_i64_le_u:
+            WriteByte($58);
+          a_i64_ge_s:
+            WriteByte($59);
+          a_i64_ge_u:
+            WriteByte($5A);
+          a_f32_eq:
+            WriteByte($5B);
+          a_f32_ne:
+            WriteByte($5C);
+          a_f32_lt:
+            WriteByte($5D);
+          a_f32_gt:
+            WriteByte($5E);
+          a_f32_le:
+            WriteByte($5F);
+          a_f32_ge:
+            WriteByte($60);
+          a_f64_eq:
+            WriteByte($61);
+          a_f64_ne:
+            WriteByte($62);
+          a_f64_lt:
+            WriteByte($63);
+          a_f64_gt:
+            WriteByte($64);
+          a_f64_le:
+            WriteByte($65);
+          a_f64_ge:
+            WriteByte($66);
+          a_i32_clz:
+            WriteByte($67);
+          a_i32_ctz:
+            WriteByte($68);
+          a_i32_popcnt:
+            WriteByte($69);
+          a_i32_add:
+            WriteByte($6A);
+          a_i32_sub:
+            WriteByte($6B);
+          a_i32_mul:
+            WriteByte($6C);
+          a_i32_div_s:
+            WriteByte($6D);
+          a_i32_div_u:
+            WriteByte($6E);
+          a_i32_rem_s:
+            WriteByte($6F);
+          a_i32_rem_u:
+            WriteByte($70);
+          a_i32_and:
+            WriteByte($71);
+          a_i32_or:
+            WriteByte($72);
+          a_i32_xor:
+            WriteByte($73);
+          a_i32_shl:
+            WriteByte($74);
+          a_i32_shr_s:
+            WriteByte($75);
+          a_i32_shr_u:
+            WriteByte($76);
+          a_i32_rotl:
+            WriteByte($77);
+          a_i32_rotr:
+            WriteByte($78);
+          a_i64_clz:
+            WriteByte($79);
+          a_i64_ctz:
+            WriteByte($7A);
+          a_i64_popcnt:
+            WriteByte($7B);
+          a_i64_add:
+            WriteByte($7C);
+          a_i64_sub:
+            WriteByte($7D);
+          a_i64_mul:
+            WriteByte($7E);
+          a_i64_div_s:
+            WriteByte($7F);
+          a_i64_div_u:
+            WriteByte($80);
+          a_i64_rem_s:
+            WriteByte($81);
+          a_i64_rem_u:
+            WriteByte($82);
+          a_i64_and:
+            WriteByte($83);
+          a_i64_or:
+            WriteByte($84);
+          a_i64_xor:
+            WriteByte($85);
+          a_i64_shl:
+            WriteByte($86);
+          a_i64_shr_s:
+            WriteByte($87);
+          a_i64_shr_u:
+            WriteByte($88);
+          a_i64_rotl:
+            WriteByte($89);
+          a_i64_rotr:
+            WriteByte($8A);
+          a_f32_abs:
+            WriteByte($8B);
+          a_f32_neg:
+            WriteByte($8C);
+          a_f32_ceil:
+            WriteByte($8D);
+          a_f32_floor:
+            WriteByte($8E);
+          a_f32_trunc:
+            WriteByte($8F);
+          a_f32_nearest:
+            WriteByte($90);
+          a_f32_sqrt:
+            WriteByte($91);
+          a_f32_add:
+            WriteByte($92);
+          a_f32_sub:
+            WriteByte($93);
+          a_f32_mul:
+            WriteByte($94);
+          a_f32_div:
+            WriteByte($95);
+          a_f32_min:
+            WriteByte($96);
+          a_f32_max:
+            WriteByte($97);
+          a_f32_copysign:
+            WriteByte($98);
+          a_f64_abs:
+            WriteByte($99);
+          a_f64_neg:
+            WriteByte($9A);
+          a_f64_ceil:
+            WriteByte($9B);
+          a_f64_floor:
+            WriteByte($9C);
+          a_f64_trunc:
+            WriteByte($9D);
+          a_f64_nearest:
+            WriteByte($9E);
+          a_f64_sqrt:
+            WriteByte($9F);
+          a_f64_add:
+            WriteByte($A0);
+          a_f64_sub:
+            WriteByte($A1);
+          a_f64_mul:
+            WriteByte($A2);
+          a_f64_div:
+            WriteByte($A3);
+          a_f64_min:
+            WriteByte($A4);
+          a_f64_max:
+            WriteByte($A5);
+          a_f64_copysign:
+            WriteByte($A6);
+          a_i32_wrap_i64:
+            WriteByte($A7);
+          a_i32_trunc_f32_s:
+            WriteByte($A8);
+          a_i32_trunc_f32_u:
+            WriteByte($A9);
+          a_i32_trunc_f64_s:
+            WriteByte($AA);
+          a_i32_trunc_f64_u:
+            WriteByte($AB);
+          a_i64_extend_i32_s:
+            WriteByte($AC);
+          a_i64_extend_i32_u:
+            WriteByte($AD);
+          a_i64_trunc_f32_s:
+            WriteByte($AE);
+          a_i64_trunc_f32_u:
+            WriteByte($AF);
+          a_i64_trunc_f64_s:
+            WriteByte($B0);
+          a_i64_trunc_f64_u:
+            WriteByte($B1);
+          a_f32_convert_i32_s:
+            WriteByte($B2);
+          a_f32_convert_i32_u:
+            WriteByte($B3);
+          a_f32_convert_i64_s:
+            WriteByte($B4);
+          a_f32_convert_i64_u:
+            WriteByte($B5);
+          a_f32_demote_f64:
+            WriteByte($B6);
+          a_f64_convert_i32_s:
+            WriteByte($B7);
+          a_f64_convert_i32_u:
+            WriteByte($B8);
+          a_f64_convert_i64_s:
+            WriteByte($B9);
+          a_f64_convert_i64_u:
+            WriteByte($BA);
+          a_f64_promote_f32:
+            WriteByte($BB);
+          a_i32_reinterpret_f32:
+            WriteByte($BC);
+          a_i64_reinterpret_f64:
+            WriteByte($BD);
+          a_f32_reinterpret_i32:
+            WriteByte($BE);
+          a_f64_reinterpret_i64:
+            WriteByte($BF);
+          a_i32_extend8_s:
+            WriteByte($C0);
+          a_i32_extend16_s:
+            WriteByte($C1);
+          a_i64_extend8_s:
+            WriteByte($C2);
+          a_i64_extend16_s:
+            WriteByte($C3);
+          a_i64_extend32_s:
+            WriteByte($C4);
+          a_end_block,
+          a_end_if,
+          a_end_loop,
+          a_end_try:
+            WriteByte($0B);
+          a_catch_all:
+            WriteByte($19);
+          else
+            Writeln('Warning! Not implemented opcode, pass2: ', opcode);
+        end;
+      end;
+
+
     function spilling_create_load(const ref:treference;r:tregister):Taicpu;
     function spilling_create_load(const ref:treference;r:tregister):Taicpu;
       begin
       begin
        internalerror(2010122614);
        internalerror(2010122614);