authorgravatar for noam@pixelhero.devNoam Preil <noam@pixelhero.dev> 2020-06-15 17:59:38-04:00
committergravatar for noam@pixelhero.devNoam Preil <noam@pixelhero.dev> 2020-06-15 20:33:39-04:00
log7ee0462f5f8b4ec5ff824b38821d8f1b79e5ba63
treef14ccce94569e7f427ec13878a20aba1ddf9976e
parent1e5945d0a926b74517b1bed3cdab00f244aa592c
signaturelock-open Commit is signed but in an unrecognized format.

Stage2/Testing: Fix transformation tests


2 files changed, 114 insertions(+), 64 deletions(-)

src-self-hosted/test.zig+32-10
...@@ -38,7 +38,6 @@ pub const TestContext = struct {...@@ -38,7 +38,6 @@ pub const TestContext = struct {
38 };38 };
3939
40 // TODO: remove40 // TODO: remove
41
42 pub const ZIRErrorCase = struct {41 pub const ZIRErrorCase = struct {
43 name: []const u8,42 name: []const u8,
44 src: [:0]const u8,43 src: [:0]const u8,
...@@ -71,7 +70,7 @@ pub const TestContext = struct {...@@ -71,7 +70,7 @@ pub const TestContext = struct {
71 src: [:0]const u8,70 src: [:0]const u8,
72 case: union(ZIRUpdateType) {71 case: union(ZIRUpdateType) {
73 /// The expected output ZIR72 /// The expected output ZIR
74 Transformation: []const u8,73 Transformation: [:0]const u8,
75 /// A slice containing the expected errors *in sequential order*.74 /// A slice containing the expected errors *in sequential order*.
76 Error: []const ErrorMsg,75 Error: []const ErrorMsg,
7776
...@@ -106,21 +105,30 @@ pub const TestContext = struct {...@@ -106,21 +105,30 @@ pub const TestContext = struct {
106 /// such as QEMU is required for tests to complete.105 /// such as QEMU is required for tests to complete.
107 ///106 ///
108 target: std.zig.CrossTarget,107 target: std.zig.CrossTarget,
109 updates: []const ZIRUpdate,108 updates: std.ArrayList(ZIRUpdate),
109
110 pub fn addTransform(self: *ZIRCase, src: [:0]const u8, result: [:0]const u8) void {
111 self.updates.append(.{
112 .src = src,
113 .case = .{ .Transformation = result },
114 }) catch unreachable;
115 }
116
117 pub fn addError(self: *ZIRCase, src: [:0]const u8, errors: []const []const u8) void {}
110 };118 };
111119
112 pub fn addZIRCase(120 pub fn addZIRMulti(
113 ctx: *TestContext,121 ctx: *TestContext,
114 name: []const u8,122 name: []const u8,
115 target: std.zig.CrossTarget,123 target: std.zig.CrossTarget,
116 updates: []const ZIRUpdate,124 ) *ZIRCase {
117 ) void {
118 const case = ZIRCase{125 const case = ZIRCase{
119 .name = name,126 .name = name,
120 .target = target,127 .target = target,
121 .updates = updates,128 .updates = std.ArrayList(ZIRUpdate).init(ctx.zir_cases.allocator),
122 };129 };
123 ctx.zir_cases.append(case) catch |err| std.debug.panic("Error: {}", .{err});130 ctx.zir_cases.append(case) catch unreachable;
131 return &ctx.zir_cases.items[ctx.zir_cases.items.len - 1];
124 }132 }
125133
126 pub fn addZIRCompareOutput(134 pub fn addZIRCompareOutput(
...@@ -136,6 +144,17 @@ pub const TestContext = struct {...@@ -136,6 +144,17 @@ pub const TestContext = struct {
136 }) catch unreachable;144 }) catch unreachable;
137 }145 }
138146
147 pub fn addZIRTransform(
148 ctx: *TestContext,
149 name: []const u8,
150 target: std.zig.CrossTarget,
151 src: [:0]const u8,
152 result: [:0]const u8,
153 ) void {
154 var c = ctx.addZIRMulti(name, target);
155 c.addTransform(src, result);
156 }
157
139 pub fn addZIRError(158 pub fn addZIRError(
140 ctx: *TestContext,159 ctx: *TestContext,
141 name: []const u8,160 name: []const u8,
...@@ -194,6 +213,9 @@ pub const TestContext = struct {...@@ -194,6 +213,9 @@ pub const TestContext = struct {
194 self.zir_error_cases.allocator.free(e.expected_errors);213 self.zir_error_cases.allocator.free(e.expected_errors);
195 }214 }
196 self.zir_error_cases.deinit();215 self.zir_error_cases.deinit();
216 for (self.zir_cases.items) |c| {
217 c.updates.deinit();
218 }
197 self.zir_cases.deinit();219 self.zir_cases.deinit();
198 self.* = undefined;220 self.* = undefined;
199 }221 }
...@@ -234,7 +256,7 @@ pub const TestContext = struct {...@@ -234,7 +256,7 @@ pub const TestContext = struct {
234 const root_pkg = try Package.create(allocator, tmp.dir, ".", tmp_src_path);256 const root_pkg = try Package.create(allocator, tmp.dir, ".", tmp_src_path);
235 defer root_pkg.destroy();257 defer root_pkg.destroy();
236258
237 var prg_node = root_node.start(case.name, case.updates.len);259 var prg_node = root_node.start(case.name, case.updates.items.len);
238 prg_node.activate();260 prg_node.activate();
239 defer prg_node.end();261 defer prg_node.end();
240262
...@@ -255,7 +277,7 @@ pub const TestContext = struct {...@@ -255,7 +277,7 @@ pub const TestContext = struct {
255 });277 });
256 defer module.deinit();278 defer module.deinit();
257279
258 for (case.updates) |s| {280 for (case.updates.items) |s| {
259 // TODO: remove before committing. This is for ZLS ;)281 // TODO: remove before committing. This is for ZLS ;)
260 const update: ZIRUpdate = s;282 const update: ZIRUpdate = s;
261283
test/stage2/zir.zig+82-54
...@@ -1,6 +1,5 @@...@@ -1,6 +1,5 @@
1const std = @import("std");1const std = @import("std");
2const TestContext = @import("../../src-self-hosted/test.zig").TestContext;2const TestContext = @import("../../src-self-hosted/test.zig").TestContext;
3const ZIRUpdate = TestContext.ZIRUpdate;
4// self-hosted does not yet support PE executable files / COFF object files3// self-hosted does not yet support PE executable files / COFF object files
5// or mach-o files. So we do the ZIR transform test cases cross compiling for4// or mach-o files. So we do the ZIR transform test cases cross compiling for
6// x86_64-linux.5// x86_64-linux.
...@@ -10,61 +9,90 @@ const linux_x64 = std.zig.CrossTarget{...@@ -10,61 +9,90 @@ const linux_x64 = std.zig.CrossTarget{
10};9};
1110
12pub fn addCases(ctx: *TestContext) void {11pub fn addCases(ctx: *TestContext) void {
13 ctx.addZIRCase("elemptr, add, cmp, condbr, return, breakpoint", linux_x64, &[_]ZIRUpdate{ZIRUpdate{12 ctx.addZIRTransform("referencing decls which appear later in the file", linux_x64,
14 .src =13 \\@void = primitive(void)
15 \\@void = primitive(void)14 \\@fnty = fntype([], @void, cc=C)
16 \\@usize = primitive(usize)15 \\
17 \\@fnty = fntype([], @void, cc=C)16 \\@9 = str("entry")
18 \\@0 = int(0)17 \\@10 = ref(@9)
19 \\@1 = int(1)18 \\@11 = export(@10, @entry)
20 \\@2 = int(2)19 \\
21 \\@3 = int(3)20 \\@entry = fn(@fnty, {
22 \\21 \\ %11 = return()
23 \\@entry = fn(@fnty, {22 \\})
24 \\ %a = str("\x32\x08\x01\x0a")23 ,
25 \\ %aref = ref(%a)24 \\@void = primitive(void)
26 \\ %eptr0 = elemptr(%aref, @0)25 \\@fnty = fntype([], @void, cc=C)
27 \\ %eptr1 = elemptr(%aref, @1)26 \\@9 = str("entry")
28 \\ %eptr2 = elemptr(%aref, @2)27 \\@10 = ref(@9)
29 \\ %eptr3 = elemptr(%aref, @3)28 \\@unnamed$6 = str("entry")
30 \\ %v0 = deref(%eptr0)29 \\@unnamed$7 = ref(@unnamed$6)
31 \\ %v1 = deref(%eptr1)30 \\@unnamed$8 = export(@unnamed$7, @entry)
32 \\ %v2 = deref(%eptr2)31 \\@unnamed$10 = fntype([], @void, cc=C)
33 \\ %v3 = deref(%eptr3)32 \\@entry = fn(@unnamed$10, {
34 \\ %x0 = add(%v0, %v1)33 \\ %0 = return()
35 \\ %x1 = add(%v2, %v3)34 \\})
36 \\ %result = add(%x0, %x1)35 \\
37 \\36 );
38 \\ %expected = int(69)37 ctx.addZIRTransform("elemptr, add, cmp, condbr, return, breakpoint", linux_x64,
39 \\ %ok = cmp(%result, eq, %expected)38 \\@void = primitive(void)
40 \\ %10 = condbr(%ok, {39 \\@usize = primitive(usize)
41 \\ %11 = return()40 \\@fnty = fntype([], @void, cc=C)
42 \\ }, {41 \\@0 = int(0)
43 \\ %12 = breakpoint()42 \\@1 = int(1)
44 \\ })43 \\@2 = int(2)
45 \\})44 \\@3 = int(3)
46 \\45 \\
47 \\@9 = str("entry")46 \\@entry = fn(@fnty, {
48 \\@10 = ref(@9)47 \\ %a = str("\x32\x08\x01\x0a")
49 \\@11 = export(@10, @entry)48 \\ %aref = ref(%a)
50 ,49 \\ %eptr0 = elemptr(%aref, @0)
51 .case = .{50 \\ %eptr1 = elemptr(%aref, @1)
52 .Transformation =51 \\ %eptr2 = elemptr(%aref, @2)
53 \\@0 = primitive(void)52 \\ %eptr3 = elemptr(%aref, @3)
54 \\@1 = fntype([], @0, cc=C)53 \\ %v0 = deref(%eptr0)
55 \\@2 = fn(@1, {54 \\ %v1 = deref(%eptr1)
56 \\ %0 = return()55 \\ %v2 = deref(%eptr2)
57 \\})56 \\ %v3 = deref(%eptr3)
58 \\@3 = str("entry")57 \\ %x0 = add(%v0, %v1)
59 \\@4 = ref(@3)58 \\ %x1 = add(%v2, %v3)
60 \\@5 = export(@4, @2)59 \\ %result = add(%x0, %x1)
61 \\60 \\
62 },61 \\ %expected = int(69)
63 }});62 \\ %ok = cmp(%result, eq, %expected)
63 \\ %10 = condbr(%ok, {
64 \\ %11 = return()
65 \\ }, {
66 \\ %12 = breakpoint()
67 \\ })
68 \\})
69 \\
70 \\@9 = str("entry")
71 \\@10 = ref(@9)
72 \\@11 = export(@10, @entry)
73 ,
74 \\@void = primitive(void)
75 \\@fnty = fntype([], @void, cc=C)
76 \\@0 = int(0)
77 \\@1 = int(1)
78 \\@2 = int(2)
79 \\@3 = int(3)
80 \\@unnamed$7 = fntype([], @void, cc=C)
81 \\@entry = fn(@unnamed$7, {
82 \\ %0 = return()
83 \\})
84 \\@a = str("2\x08\x01\n")
85 \\@9 = str("entry")
86 \\@10 = ref(@9)
87 \\@unnamed$14 = str("entry")
88 \\@unnamed$15 = ref(@unnamed$14)
89 \\@unnamed$16 = export(@unnamed$15, @entry)
90 \\
91 );
6492
65 {93 {
66 var case = ctx.addZIRMulti("reference cycle with compile error in the cycle", linux_x64);94 var case = ctx.addZIRMulti("reference cycle with compile error in the cycle", linux_x64);
67 case.addZIR(95 case.addTransform(
68 \\@void = primitive(void)96 \\@void = primitive(void)
69 \\@fnty = fntype([], @void, cc=C)97 \\@fnty = fntype([], @void, cc=C)
70 \\98 \\
...@@ -143,7 +171,7 @@ pub fn addCases(ctx: *TestContext) void {...@@ -143,7 +171,7 @@ pub fn addCases(ctx: *TestContext) void {
143 // Now we remove the call to `a`. `a` and `b` form a cycle, but no entry points are171 // Now we remove the call to `a`. `a` and `b` form a cycle, but no entry points are
144 // referencing either of them. This tests that the cycle is detected, and the error172 // referencing either of them. This tests that the cycle is detected, and the error
145 // goes away.173 // goes away.
146 case.addZIR(174 case.addTransform(
147 \\@void = primitive(void)175 \\@void = primitive(void)
148 \\@fnty = fntype([], @void, cc=C)176 \\@fnty = fntype([], @void, cc=C)
149 \\177 \\