| author | |
| committer | |
| log | 84039a57e4684e8df10e657bb76c6acb3fb89238 |
| tree | 67d0ca6c151b7ab77311bc2a4e156dffa88b6d24 |
| parent | 7aaea20e7e5e98bc5b2c4c8ddcf8f6aaa4a9c55d |
2 files changed, 45 insertions(+), 0 deletions(-)
src/codegen.zig+23| ... | ... | @@ -4740,6 +4740,29 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type { |
| 4740 | 4740 | } |
| 4741 | 4741 | return self.fail("TODO non pointer optionals", .{}); |
| 4742 | 4742 | }, |
| 4743 | .Enum => { | |
| 4744 | if (typed_value.val.castTag(.enum_field_index)) |field_index| { | |
| 4745 | switch (typed_value.ty.tag()) { | |
| 4746 | .enum_simple => { | |
| 4747 | return MCValue{ .immediate = field_index.data }; | |
| 4748 | }, | |
| 4749 | .enum_full, .enum_nonexhaustive => { | |
| 4750 | const enum_full = typed_value.ty.cast(Type.Payload.EnumFull).?.data; | |
| 4751 | if (enum_full.values.count() != 0) { | |
| 4752 | const tag_val = enum_full.values.keys()[field_index.data]; | |
| 4753 | return self.genTypedValue(.{ .ty = enum_full.tag_ty, .val = tag_val }); | |
| 4754 | } else { | |
| 4755 | return MCValue{ .immediate = field_index.data }; | |
| 4756 | } | |
| 4757 | }, | |
| 4758 | else => unreachable, | |
| 4759 | } | |
| 4760 | } else { | |
| 4761 | var int_tag_buffer: Type.Payload.Bits = undefined; | |
| 4762 | const int_tag_ty = typed_value.ty.intTagType(&int_tag_buffer); | |
| 4763 | return self.genTypedValue(.{ .ty = int_tag_ty, .val = typed_value.val }); | |
| 4764 | } | |
| 4765 | }, | |
| 4743 | 4766 | .ErrorSet => { |
| 4744 | 4767 | switch (typed_value.val.tag()) { |
| 4745 | 4768 | .@"error" => { |
test/stage2/arm.zig+22| ... | ... | @@ -299,6 +299,28 @@ pub fn addCases(ctx: *TestContext) !void { |
| 299 | 299 | ); |
| 300 | 300 | } |
| 301 | 301 | |
| 302 | { | |
| 303 | var case = ctx.exe("enums", linux_arm); | |
| 304 | case.addCompareOutput( | |
| 305 | \\const Number = enum { one, two, three }; | |
| 306 | \\ | |
| 307 | \\pub fn main() void { | |
| 308 | \\ var x: Number = .one; | |
| 309 | \\ var y = Number.two; | |
| 310 | \\ var z = @intToEnum(Number, 2); | |
| 311 | \\ assert(@enumToInt(x) == 0); | |
| 312 | \\ assert(@enumToInt(y) == 1); | |
| 313 | \\ assert(@enumToInt(z) == 2); | |
| 314 | \\} | |
| 315 | \\ | |
| 316 | \\fn assert(ok: bool) void { | |
| 317 | \\ if (!ok) unreachable; // assertion failure | |
| 318 | \\} | |
| 319 | , | |
| 320 | "", | |
| 321 | ); | |
| 322 | } | |
| 323 | ||
| 302 | 324 | { |
| 303 | 325 | var case = ctx.exe("recursive fibonacci", linux_arm); |
| 304 | 326 | case.addCompareOutput( |