authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2021-05-15 16:52:01+02:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2021-05-19 08:59:08+02:00
logb22e22ef55327055e88817ae2d950da4739e488d
treeb9ea437dfe6fb64eb07d09c2e64084ecf27cb8f1
parent1d6c804b29fed146aafda0f56cae7a4853b6abf9
signaturelock-open Commit is signed but in an unrecognized format.

wasm backend - Initial enum support

- This adds support for enum values using field indexes - EmitConstant's signature was changed so it's easier to recursively call it using a different type (enum -> int type). - Implemented initial support for bitcast which for now just returns the `WValue` of the operand.

1 files changed, 66 insertions(+), 25 deletions(-)

src/codegen/wasm.zig+66-25
...@@ -557,7 +557,14 @@ pub const Context = struct {...@@ -557,7 +557,14 @@ pub const Context = struct {
557 return self.fail(src, "Integer bit size not supported by wasm: '{d}'", .{info.bits});557 return self.fail(src, "Integer bit size not supported by wasm: '{d}'", .{info.bits});
558 },558 },
559 .Bool, .Pointer => wasm.Valtype.i32,559 .Bool, .Pointer => wasm.Valtype.i32,
560 else => self.fail(src, "TODO - Wasm valtype for type '{s}'", .{ty.tag()}),560 .Enum => switch (ty.tag()) {
561 .enum_simple => wasm.Valtype.i32,
562 else => self.typeToValtype(
563 src,
564 ty.cast(Type.Payload.EnumFull).?.data.tag_ty,
565 ),
566 },
567 else => self.fail(src, "TODO - Wasm valtype for type '{s}'", .{ty.zigTypeTag()}),
561 };568 };
562 }569 }
563570
...@@ -586,7 +593,7 @@ pub const Context = struct {...@@ -586,7 +593,7 @@ pub const Context = struct {
586 try writer.writeByte(wasm.opcode(.local_get));593 try writer.writeByte(wasm.opcode(.local_get));
587 try leb.writeULEB128(writer, idx);594 try leb.writeULEB128(writer, idx);
588 },595 },
589 .constant => |inst| try self.emitConstant(inst.castTag(.constant).?), // creates a new constant onto the stack596 .constant => |inst| try self.emitConstant(inst.src, inst.value().?, inst.ty), // creates a new constant onto the stack
590 }597 }
591 }598 }
592599
...@@ -707,14 +714,15 @@ pub const Context = struct {...@@ -707,14 +714,15 @@ pub const Context = struct {
707 .add => self.genBinOp(inst.castTag(.add).?, .add),714 .add => self.genBinOp(inst.castTag(.add).?, .add),
708 .alloc => self.genAlloc(inst.castTag(.alloc).?),715 .alloc => self.genAlloc(inst.castTag(.alloc).?),
709 .arg => self.genArg(inst.castTag(.arg).?),716 .arg => self.genArg(inst.castTag(.arg).?),
717 .bitcast => self.genBitcast(inst.castTag(.bitcast).?),
718 .bit_and => self.genBinOp(inst.castTag(.bit_and).?, .@"and"),
719 .bit_or => self.genBinOp(inst.castTag(.bit_or).?, .@"or"),
710 .block => self.genBlock(inst.castTag(.block).?),720 .block => self.genBlock(inst.castTag(.block).?),
721 .bool_and => self.genBinOp(inst.castTag(.bool_and).?, .@"and"),
722 .bool_or => self.genBinOp(inst.castTag(.bool_or).?, .@"or"),
711 .breakpoint => self.genBreakpoint(inst.castTag(.breakpoint).?),723 .breakpoint => self.genBreakpoint(inst.castTag(.breakpoint).?),
712 .br => self.genBr(inst.castTag(.br).?),724 .br => self.genBr(inst.castTag(.br).?),
713 .call => self.genCall(inst.castTag(.call).?),725 .call => self.genCall(inst.castTag(.call).?),
714 .bit_or => self.genBinOp(inst.castTag(.bit_or).?, .@"or"),
715 .bit_and => self.genBinOp(inst.castTag(.bit_and).?, .@"and"),
716 .bool_or => self.genBinOp(inst.castTag(.bool_or).?, .@"or"),
717 .bool_and => self.genBinOp(inst.castTag(.bool_and).?, .@"and"),
718 .cmp_eq => self.genCmp(inst.castTag(.cmp_eq).?, .eq),726 .cmp_eq => self.genCmp(inst.castTag(.cmp_eq).?, .eq),
719 .cmp_gte => self.genCmp(inst.castTag(.cmp_gte).?, .gte),727 .cmp_gte => self.genCmp(inst.castTag(.cmp_gte).?, .gte),
720 .cmp_gt => self.genCmp(inst.castTag(.cmp_gt).?, .gt),728 .cmp_gt => self.genCmp(inst.castTag(.cmp_gt).?, .gt),
...@@ -724,18 +732,18 @@ pub const Context = struct {...@@ -724,18 +732,18 @@ pub const Context = struct {
724 .condbr => self.genCondBr(inst.castTag(.condbr).?),732 .condbr => self.genCondBr(inst.castTag(.condbr).?),
725 .constant => unreachable,733 .constant => unreachable,
726 .dbg_stmt => WValue.none,734 .dbg_stmt => WValue.none,
735 .div => self.genBinOp(inst.castTag(.div).?, .div),
727 .load => self.genLoad(inst.castTag(.load).?),736 .load => self.genLoad(inst.castTag(.load).?),
728 .loop => self.genLoop(inst.castTag(.loop).?),737 .loop => self.genLoop(inst.castTag(.loop).?),
729 .mul => self.genBinOp(inst.castTag(.mul).?, .mul),738 .mul => self.genBinOp(inst.castTag(.mul).?, .mul),
730 .div => self.genBinOp(inst.castTag(.div).?, .div),
731 .xor => self.genBinOp(inst.castTag(.xor).?, .xor),
732 .not => self.genNot(inst.castTag(.not).?),739 .not => self.genNot(inst.castTag(.not).?),
733 .ret => self.genRet(inst.castTag(.ret).?),740 .ret => self.genRet(inst.castTag(.ret).?),
734 .retvoid => WValue.none,741 .retvoid => WValue.none,
735 .store => self.genStore(inst.castTag(.store).?),742 .store => self.genStore(inst.castTag(.store).?),
736 .sub => self.genBinOp(inst.castTag(.sub).?, .sub),743 .sub => self.genBinOp(inst.castTag(.sub).?, .sub),
737 .unreach => self.genUnreachable(inst.castTag(.unreach).?),744 .unreach => self.genUnreachable(inst.castTag(.unreach).?),
738 else => self.fail(inst.src, "TODO: Implement wasm inst: {s}", .{inst.tag}),745 .xor => self.genBinOp(inst.castTag(.xor).?, .xor),
746 else => self.fail(.{ .node_offset = 0 }, "TODO: Implement wasm inst: {s}", .{inst.tag}),
739 };747 };
740 }748 }
741749
...@@ -830,44 +838,44 @@ pub const Context = struct {...@@ -830,44 +838,44 @@ pub const Context = struct {
830 return .none;838 return .none;
831 }839 }
832840
833 fn emitConstant(self: *Context, inst: *Inst.Constant) InnerError!void {841 fn emitConstant(self: *Context, src: LazySrcLoc, value: Value, ty: Type) InnerError!void {
834 const writer = self.code.writer();842 const writer = self.code.writer();
835 switch (inst.base.ty.zigTypeTag()) {843 switch (ty.zigTypeTag()) {
836 .Int => {844 .Int => {
837 // write opcode845 // write opcode
838 const opcode: wasm.Opcode = buildOpcode(.{846 const opcode: wasm.Opcode = buildOpcode(.{
839 .op = .@"const",847 .op = .@"const",
840 .valtype1 = try self.typeToValtype(inst.base.src, inst.base.ty),848 .valtype1 = try self.typeToValtype(src, ty),
841 });849 });
842 try writer.writeByte(wasm.opcode(opcode));850 try writer.writeByte(wasm.opcode(opcode));
843 // write constant851 // write constant
844 switch (inst.base.ty.intInfo(self.target).signedness) {852 switch (ty.intInfo(self.target).signedness) {
845 .signed => try leb.writeILEB128(writer, inst.val.toSignedInt()),853 .signed => try leb.writeILEB128(writer, value.toSignedInt()),
846 .unsigned => try leb.writeILEB128(writer, inst.val.toUnsignedInt()),854 .unsigned => try leb.writeILEB128(writer, value.toUnsignedInt()),
847 }855 }
848 },856 },
849 .Bool => {857 .Bool => {
850 // write opcode858 // write opcode
851 try writer.writeByte(wasm.opcode(.i32_const));859 try writer.writeByte(wasm.opcode(.i32_const));
852 // write constant860 // write constant
853 try leb.writeILEB128(writer, inst.val.toSignedInt());861 try leb.writeILEB128(writer, value.toSignedInt());
854 },862 },
855 .Float => {863 .Float => {
856 // write opcode864 // write opcode
857 const opcode: wasm.Opcode = buildOpcode(.{865 const opcode: wasm.Opcode = buildOpcode(.{
858 .op = .@"const",866 .op = .@"const",
859 .valtype1 = try self.typeToValtype(inst.base.src, inst.base.ty),867 .valtype1 = try self.typeToValtype(src, ty),
860 });868 });
861 try writer.writeByte(wasm.opcode(opcode));869 try writer.writeByte(wasm.opcode(opcode));
862 // write constant870 // write constant
863 switch (inst.base.ty.floatBits(self.target)) {871 switch (ty.floatBits(self.target)) {
864 0...32 => try writer.writeIntLittle(u32, @bitCast(u32, inst.val.toFloat(f32))),872 0...32 => try writer.writeIntLittle(u32, @bitCast(u32, value.toFloat(f32))),
865 64 => try writer.writeIntLittle(u64, @bitCast(u64, inst.val.toFloat(f64))),873 64 => try writer.writeIntLittle(u64, @bitCast(u64, value.toFloat(f64))),
866 else => |bits| return self.fail(inst.base.src, "Wasm TODO: emitConstant for float with {d} bits", .{bits}),874 else => |bits| return self.fail(src, "Wasm TODO: emitConstant for float with {d} bits", .{bits}),
867 }875 }
868 },876 },
869 .Pointer => {877 .Pointer => {
870 if (inst.val.castTag(.decl_ref)) |payload| {878 if (value.castTag(.decl_ref)) |payload| {
871 const decl = payload.data;879 const decl = payload.data;
872880
873 // offset into the offset table within the 'data' section881 // offset into the offset table within the 'data' section
...@@ -880,10 +888,32 @@ pub const Context = struct {...@@ -880,10 +888,32 @@ pub const Context = struct {
880 try writer.writeByte(wasm.opcode(.i32_load));888 try writer.writeByte(wasm.opcode(.i32_load));
881 try leb.writeULEB128(writer, @as(u32, 0));889 try leb.writeULEB128(writer, @as(u32, 0));
882 try leb.writeULEB128(writer, @as(u32, 0));890 try leb.writeULEB128(writer, @as(u32, 0));
883 } else return self.fail(inst.base.src, "Wasm TODO: emitConstant for other const pointer tag {s}", .{inst.val.tag()});891 } else return self.fail(src, "Wasm TODO: emitConstant for other const pointer tag {s}", .{value.tag()});
884 },892 },
885 .Void => {},893 .Void => {},
886 else => |ty| return self.fail(inst.base.src, "Wasm TODO: emitConstant for zigTypeTag {s}", .{ty}),894 .Enum => {
895 if (value.castTag(.enum_field_index)) |field_index| {
896 switch (ty.tag()) {
897 .enum_simple => {
898 try writer.writeByte(wasm.opcode(.i32_const));
899 try leb.writeULEB128(writer, field_index.data);
900 },
901 .enum_full, .enum_nonexhaustive => {
902 const enum_full = ty.cast(Type.Payload.EnumFull).?.data;
903 if (enum_full.values.count() != 0) {
904 const tag_val = enum_full.values.entries.items[field_index.data].key;
905 try self.emitConstant(src, tag_val, enum_full.tag_ty);
906 }
907 },
908 else => unreachable,
909 }
910 } else {
911 var int_tag_buffer: Type.Payload.Bits = undefined;
912 const int_tag_ty = ty.intTagType(&int_tag_buffer);
913 try self.emitConstant(src, value, int_tag_ty);
914 }
915 },
916 else => |zig_type| return self.fail(src, "Wasm TODO: emitConstant for zigTypeTag {s}", .{zig_type}),
887 }917 }
888 }918 }
889919
...@@ -984,6 +1014,13 @@ pub const Context = struct {...@@ -984,6 +1014,13 @@ pub const Context = struct {
984 try self.emitWValue(lhs);1014 try self.emitWValue(lhs);
985 try self.emitWValue(rhs);1015 try self.emitWValue(rhs);
9861016
1017 const signedness: std.builtin.Signedness = blk: {
1018 // by default we tell the operand type is unsigned (i.e. bools and enum values)
1019 if (inst.lhs.ty.zigTypeTag() != .Int) break :blk .unsigned;
1020
1021 // incase of an actual integer, we emit the correct signedness
1022 break :blk inst.lhs.ty.intInfo(self.target).signedness;
1023 };
987 const opcode: wasm.Opcode = buildOpcode(.{1024 const opcode: wasm.Opcode = buildOpcode(.{
988 .valtype1 = try self.typeToValtype(inst.base.src, inst.lhs.ty),1025 .valtype1 = try self.typeToValtype(inst.base.src, inst.lhs.ty),
989 .op = switch (op) {1026 .op = switch (op) {
...@@ -994,7 +1031,7 @@ pub const Context = struct {...@@ -994,7 +1031,7 @@ pub const Context = struct {
994 .gte => .ge,1031 .gte => .ge,
995 .gt => .gt,1032 .gt => .gt,
996 },1033 },
997 .signedness = inst.lhs.ty.intInfo(self.target).signedness,1034 .signedness = signedness,
998 });1035 });
999 try self.code.append(wasm.opcode(opcode));1036 try self.code.append(wasm.opcode(opcode));
1000 return WValue{ .code_offset = offset };1037 return WValue{ .code_offset = offset };
...@@ -1045,4 +1082,8 @@ pub const Context = struct {...@@ -1045,4 +1082,8 @@ pub const Context = struct {
1045 try self.code.append(wasm.opcode(.@"unreachable"));1082 try self.code.append(wasm.opcode(.@"unreachable"));
1046 return .none;1083 return .none;
1047 }1084 }
1085
1086 fn genBitcast(self: *Context, bitcast: *Inst.UnOp) InnerError!WValue {
1087 return self.resolveInst(bitcast.operand);
1088 }
1048};1089};