authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-20 14:03:54-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-05-20 14:03:54-04:00
logdf56bf94a2a3d1ddef891a90a5b1ee077b5dd6c9
tree169f07aa58fd905264dea3a20770ea24085932e2
parentb09936d72822e8bb7732f68c6ead369b83c39742
parent141a0cbb5a1ffd9e6c47bf859064139143ed8c51
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #8789 from Luukdegram/wasm-enum

stage2: wasm backend - Enums

2 files changed, 105 insertions(+), 25 deletions(-)

src/codegen/wasm.zig+70-25
......@@ -557,7 +557,14 @@ pub const Context = struct {
557557 return self.fail(src, "Integer bit size not supported by wasm: '{d}'", .{info.bits});
558558 },
559559 .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()}),
561568 };
562569 }
563570
......@@ -586,7 +593,7 @@ pub const Context = struct {
586593 try writer.writeByte(wasm.opcode(.local_get));
587594 try leb.writeULEB128(writer, idx);
588595 },
589 .constant => |inst| try self.emitConstant(inst.castTag(.constant).?), // creates a new constant onto the stack
596 .constant => |inst| try self.emitConstant(inst.src, inst.value().?, inst.ty), // creates a new constant onto the stack
590597 }
591598 }
592599
......@@ -707,14 +714,15 @@ pub const Context = struct {
707714 .add => self.genBinOp(inst.castTag(.add).?, .add),
708715 .alloc => self.genAlloc(inst.castTag(.alloc).?),
709716 .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"),
710720 .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"),
711723 .breakpoint => self.genBreakpoint(inst.castTag(.breakpoint).?),
712724 .br => self.genBr(inst.castTag(.br).?),
713725 .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"),
718726 .cmp_eq => self.genCmp(inst.castTag(.cmp_eq).?, .eq),
719727 .cmp_gte => self.genCmp(inst.castTag(.cmp_gte).?, .gte),
720728 .cmp_gt => self.genCmp(inst.castTag(.cmp_gt).?, .gt),
......@@ -724,18 +732,18 @@ pub const Context = struct {
724732 .condbr => self.genCondBr(inst.castTag(.condbr).?),
725733 .constant => unreachable,
726734 .dbg_stmt => WValue.none,
735 .div => self.genBinOp(inst.castTag(.div).?, .div),
727736 .load => self.genLoad(inst.castTag(.load).?),
728737 .loop => self.genLoop(inst.castTag(.loop).?),
729738 .mul => self.genBinOp(inst.castTag(.mul).?, .mul),
730 .div => self.genBinOp(inst.castTag(.div).?, .div),
731 .xor => self.genBinOp(inst.castTag(.xor).?, .xor),
732739 .not => self.genNot(inst.castTag(.not).?),
733740 .ret => self.genRet(inst.castTag(.ret).?),
734741 .retvoid => WValue.none,
735742 .store => self.genStore(inst.castTag(.store).?),
736743 .sub => self.genBinOp(inst.castTag(.sub).?, .sub),
737744 .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}),
739747 };
740748 }
741749
......@@ -750,6 +758,7 @@ pub const Context = struct {
750758 // TODO: Implement tail calls
751759 const operand = self.resolveInst(inst.operand);
752760 try self.emitWValue(operand);
761 try self.code.append(wasm.opcode(.@"return"));
753762 return .none;
754763 }
755764
......@@ -830,44 +839,44 @@ pub const Context = struct {
830839 return .none;
831840 }
832841
833 fn emitConstant(self: *Context, inst: *Inst.Constant) InnerError!void {
842 fn emitConstant(self: *Context, src: LazySrcLoc, value: Value, ty: Type) InnerError!void {
834843 const writer = self.code.writer();
835 switch (inst.base.ty.zigTypeTag()) {
844 switch (ty.zigTypeTag()) {
836845 .Int => {
837846 // write opcode
838847 const opcode: wasm.Opcode = buildOpcode(.{
839848 .op = .@"const",
840 .valtype1 = try self.typeToValtype(inst.base.src, inst.base.ty),
849 .valtype1 = try self.typeToValtype(src, ty),
841850 });
842851 try writer.writeByte(wasm.opcode(opcode));
843852 // write constant
844 switch (inst.base.ty.intInfo(self.target).signedness) {
845 .signed => try leb.writeILEB128(writer, inst.val.toSignedInt()),
846 .unsigned => try leb.writeILEB128(writer, inst.val.toUnsignedInt()),
853 switch (ty.intInfo(self.target).signedness) {
854 .signed => try leb.writeILEB128(writer, value.toSignedInt()),
855 .unsigned => try leb.writeILEB128(writer, value.toUnsignedInt()),
847856 }
848857 },
849858 .Bool => {
850859 // write opcode
851860 try writer.writeByte(wasm.opcode(.i32_const));
852861 // write constant
853 try leb.writeILEB128(writer, inst.val.toSignedInt());
862 try leb.writeILEB128(writer, value.toSignedInt());
854863 },
855864 .Float => {
856865 // write opcode
857866 const opcode: wasm.Opcode = buildOpcode(.{
858867 .op = .@"const",
859 .valtype1 = try self.typeToValtype(inst.base.src, inst.base.ty),
868 .valtype1 = try self.typeToValtype(src, ty),
860869 });
861870 try writer.writeByte(wasm.opcode(opcode));
862871 // write constant
863 switch (inst.base.ty.floatBits(self.target)) {
864 0...32 => try writer.writeIntLittle(u32, @bitCast(u32, inst.val.toFloat(f32))),
865 64 => try writer.writeIntLittle(u64, @bitCast(u64, inst.val.toFloat(f64))),
866 else => |bits| return self.fail(inst.base.src, "Wasm TODO: emitConstant for float with {d} bits", .{bits}),
872 switch (ty.floatBits(self.target)) {
873 0...32 => try writer.writeIntLittle(u32, @bitCast(u32, value.toFloat(f32))),
874 64 => try writer.writeIntLittle(u64, @bitCast(u64, value.toFloat(f64))),
875 else => |bits| return self.fail(src, "Wasm TODO: emitConstant for float with {d} bits", .{bits}),
867876 }
868877 },
869878 .Pointer => {
870 if (inst.val.castTag(.decl_ref)) |payload| {
879 if (value.castTag(.decl_ref)) |payload| {
871880 const decl = payload.data;
872881
873882 // offset into the offset table within the 'data' section
......@@ -880,10 +889,35 @@ pub const Context = struct {
880889 try writer.writeByte(wasm.opcode(.i32_load));
881890 try leb.writeULEB128(writer, @as(u32, 0));
882891 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()});
892 } else return self.fail(src, "Wasm TODO: emitConstant for other const pointer tag {s}", .{value.tag()});
884893 },
885894 .Void => {},
886 else => |ty| return self.fail(inst.base.src, "Wasm TODO: emitConstant for zigTypeTag {s}", .{ty}),
895 .Enum => {
896 if (value.castTag(.enum_field_index)) |field_index| {
897 switch (ty.tag()) {
898 .enum_simple => {
899 try writer.writeByte(wasm.opcode(.i32_const));
900 try leb.writeULEB128(writer, field_index.data);
901 },
902 .enum_full, .enum_nonexhaustive => {
903 const enum_full = ty.cast(Type.Payload.EnumFull).?.data;
904 if (enum_full.values.count() != 0) {
905 const tag_val = enum_full.values.entries.items[field_index.data].key;
906 try self.emitConstant(src, tag_val, enum_full.tag_ty);
907 } else {
908 try writer.writeByte(wasm.opcode(.i32_const));
909 try leb.writeULEB128(writer, field_index.data);
910 }
911 },
912 else => unreachable,
913 }
914 } else {
915 var int_tag_buffer: Type.Payload.Bits = undefined;
916 const int_tag_ty = ty.intTagType(&int_tag_buffer);
917 try self.emitConstant(src, value, int_tag_ty);
918 }
919 },
920 else => |zig_type| return self.fail(src, "Wasm TODO: emitConstant for zigTypeTag {s}", .{zig_type}),
887921 }
888922 }
889923
......@@ -984,6 +1018,13 @@ pub const Context = struct {
9841018 try self.emitWValue(lhs);
9851019 try self.emitWValue(rhs);
9861020
1021 const signedness: std.builtin.Signedness = blk: {
1022 // by default we tell the operand type is unsigned (i.e. bools and enum values)
1023 if (inst.lhs.ty.zigTypeTag() != .Int) break :blk .unsigned;
1024
1025 // incase of an actual integer, we emit the correct signedness
1026 break :blk inst.lhs.ty.intInfo(self.target).signedness;
1027 };
9871028 const opcode: wasm.Opcode = buildOpcode(.{
9881029 .valtype1 = try self.typeToValtype(inst.base.src, inst.lhs.ty),
9891030 .op = switch (op) {
......@@ -994,7 +1035,7 @@ pub const Context = struct {
9941035 .gte => .ge,
9951036 .gt => .gt,
9961037 },
997 .signedness = inst.lhs.ty.intInfo(self.target).signedness,
1038 .signedness = signedness,
9981039 });
9991040 try self.code.append(wasm.opcode(opcode));
10001041 return WValue{ .code_offset = offset };
......@@ -1045,4 +1086,8 @@ pub const Context = struct {
10451086 try self.code.append(wasm.opcode(.@"unreachable"));
10461087 return .none;
10471088 }
1089
1090 fn genBitcast(self: *Context, bitcast: *Inst.UnOp) InnerError!WValue {
1091 return self.resolveInst(bitcast.operand);
1092 }
10481093};
test/stage2/wasm.zig+35
......@@ -384,4 +384,39 @@ pub fn addCases(ctx: *TestContext) !void {
384384 \\}
385385 , "5\n");
386386 }
387
388 {
389 var case = ctx.exe("wasm enum values", wasi);
390
391 case.addCompareOutput(
392 \\const Number = enum { One, Two, Three };
393 \\
394 \\pub export fn _start() i32 {
395 \\ var number1 = Number.One;
396 \\ var number2: Number = .Two;
397 \\ const number3 = @intToEnum(Number, 2);
398 \\
399 \\ return @enumToInt(number3);
400 \\}
401 , "2\n");
402
403 case.addCompareOutput(
404 \\const Number = enum { One, Two, Three };
405 \\
406 \\pub export fn _start() i32 {
407 \\ var number1 = Number.One;
408 \\ var number2: Number = .Two;
409 \\ const number3 = @intToEnum(Number, 2);
410 \\ if (number1 == number2) return 1;
411 \\ if (number2 == number3) return 1;
412 \\ if (@enumToInt(number1) != 0) return 1;
413 \\ if (@enumToInt(number2) != 1) return 1;
414 \\ if (@enumToInt(number3) != 2) return 1;
415 \\ var x: Number = .Two;
416 \\ if (number2 != x) return 1;
417 \\
418 \\ return @enumToInt(number3);
419 \\}
420 , "2\n");
421 }
387422}