authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-17 13:49:22-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-17 13:49:22-04:00
logb0968abccbfb4072528c3b5e039bc03b27af89a1
tree28c0025b0d03c245c476897c7d826c7fd1c832e2
parent88c8ff6e374334e538a686781d608c1b204aeb45

update ZIR compare output test to test incremental updates


2 files changed, 204 insertions(+), 118 deletions(-)

src-self-hosted/test.zig+61-49
...@@ -21,8 +21,8 @@ pub const TestContext = struct {...@@ -21,8 +21,8 @@ pub const TestContext = struct {
2121
22 pub const ZIRCompareOutputCase = struct {22 pub const ZIRCompareOutputCase = struct {
23 name: []const u8,23 name: []const u8,
24 src: [:0]const u8,24 src_list: []const []const u8,
25 expected_stdout: []const u8,25 expected_stdout_list: []const []const u8,
26 };26 };
2727
28 pub const ZIRTransformCase = struct {28 pub const ZIRTransformCase = struct {
...@@ -35,13 +35,13 @@ pub const TestContext = struct {...@@ -35,13 +35,13 @@ pub const TestContext = struct {
35 pub fn addZIRCompareOutput(35 pub fn addZIRCompareOutput(
36 ctx: *TestContext,36 ctx: *TestContext,
37 name: []const u8,37 name: []const u8,
38 src: [:0]const u8,38 src_list: []const []const u8,
39 expected_stdout: []const u8,39 expected_stdout_list: []const []const u8,
40 ) void {40 ) void {
41 ctx.zir_cmp_output_cases.append(.{41 ctx.zir_cmp_output_cases.append(.{
42 .name = name,42 .name = name,
43 .src = src,43 .src_list = src_list,
44 .expected_stdout = expected_stdout,44 .expected_stdout_list = expected_stdout_list,
45 }) catch unreachable;45 }) catch unreachable;
46 }46 }
4747
...@@ -104,56 +104,68 @@ pub const TestContext = struct {...@@ -104,56 +104,68 @@ pub const TestContext = struct {
104 var tmp = std.testing.tmpDir(.{});104 var tmp = std.testing.tmpDir(.{});
105 defer tmp.cleanup();105 defer tmp.cleanup();
106106
107 var prg_node = root_node.start(case.name, 2);
108 prg_node.activate();
109 defer prg_node.end();
110
111 const tmp_src_path = "test-case.zir";107 const tmp_src_path = "test-case.zir";
112 try tmp.dir.writeFile(tmp_src_path, case.src);
113
114 const root_pkg = try Package.create(allocator, tmp.dir, ".", tmp_src_path);108 const root_pkg = try Package.create(allocator, tmp.dir, ".", tmp_src_path);
115 defer root_pkg.destroy();109 defer root_pkg.destroy();
116110
117 {111 var prg_node = root_node.start(case.name, case.src_list.len);
118 var module = try Module.init(allocator, .{112 prg_node.activate();
119 .target = target,113 defer prg_node.end();
120 .output_mode = .Exe,
121 .optimize_mode = .Debug,
122 .bin_file_dir = tmp.dir,
123 .bin_file_path = "a.out",
124 .root_pkg = root_pkg,
125 });
126 defer module.deinit();
127
128 var module_node = prg_node.start("parse,analysis,codegen", null);
129 module_node.activate();
130 try module.update();
131 module_node.end();
132 }
133114
134 var exec_result = x: {115 var module = try Module.init(allocator, .{
135 var exec_node = prg_node.start("execute", null);116 .target = target,
136 exec_node.activate();117 .output_mode = .Exe,
137 defer exec_node.end();118 .optimize_mode = .Debug,
119 .bin_file_dir = tmp.dir,
120 .bin_file_path = "a.out",
121 .root_pkg = root_pkg,
122 });
123 defer module.deinit();
138124
139 break :x try std.ChildProcess.exec(.{125 for (case.src_list) |source, i| {
140 .allocator = allocator,126 var src_node = prg_node.start("update", 2);
141 .argv = &[_][]const u8{"./a.out"},127 src_node.activate();
142 .cwd_dir = tmp.dir,128 defer src_node.end();
143 });129
144 };130 try tmp.dir.writeFile(tmp_src_path, source);
145 defer allocator.free(exec_result.stdout);131
146 defer allocator.free(exec_result.stderr);132 var update_node = src_node.start("parse,analysis,codegen", null);
147 switch (exec_result.term) {133 update_node.activate();
148 .Exited => |code| {134 try module.makeBinFileWritable();
149 if (code != 0) {135 try module.update();
150 std.debug.warn("elf file exited with code {}\n", .{code});136 update_node.end();
151 return error.BinaryBadExitCode;137
152 }138 var exec_result = x: {
153 },139 var exec_node = src_node.start("execute", null);
154 else => return error.BinaryCrashed,140 exec_node.activate();
141 defer exec_node.end();
142
143 try module.makeBinFileExecutable();
144 break :x try std.ChildProcess.exec(.{
145 .allocator = allocator,
146 .argv = &[_][]const u8{"./a.out"},
147 .cwd_dir = tmp.dir,
148 });
149 };
150 defer allocator.free(exec_result.stdout);
151 defer allocator.free(exec_result.stderr);
152 switch (exec_result.term) {
153 .Exited => |code| {
154 if (code != 0) {
155 std.debug.warn("elf file exited with code {}\n", .{code});
156 return error.BinaryBadExitCode;
157 }
158 },
159 else => return error.BinaryCrashed,
160 }
161 const expected_stdout = case.expected_stdout_list[i];
162 if (!std.mem.eql(u8, expected_stdout, exec_result.stdout)) {
163 std.debug.panic(
164 "update index {}, mismatched stdout\n====Expected (len={}):====\n{}\n====Actual (len={}):====\n{}\n========\n",
165 .{ i, expected_stdout.len, expected_stdout, exec_result.stdout.len, exec_result.stdout },
166 );
167 }
155 }168 }
156 std.testing.expectEqualSlices(u8, case.expected_stdout, exec_result.stdout);
157 }169 }
158170
159 fn runOneZIRTransformCase(171 fn runOneZIRTransformCase(
test/stage2/zir.zig+143-69
...@@ -65,74 +65,148 @@ pub fn addCases(ctx: *TestContext) void {...@@ -65,74 +65,148 @@ pub fn addCases(ctx: *TestContext) void {
65 return;65 return;
66 }66 }
6767
68 ctx.addZIRCompareOutput("hello world ZIR",68 ctx.addZIRCompareOutput(
69 \\@noreturn = primitive(noreturn)69 "hello world ZIR, update msg",
70 \\@void = primitive(void)70 &[_][]const u8{
71 \\@usize = primitive(usize)71 \\@noreturn = primitive(noreturn)
72 \\@0 = int(0)72 \\@void = primitive(void)
73 \\@1 = int(1)73 \\@usize = primitive(usize)
74 \\@2 = int(2)74 \\@0 = int(0)
75 \\@3 = int(3)75 \\@1 = int(1)
76 \\76 \\@2 = int(2)
77 \\@syscall_array = str("syscall")77 \\@3 = int(3)
78 \\@sysoutreg_array = str("={rax}")78 \\
79 \\@rax_array = str("{rax}")79 \\@syscall_array = str("syscall")
80 \\@rdi_array = str("{rdi}")80 \\@sysoutreg_array = str("={rax}")
81 \\@rcx_array = str("rcx")81 \\@rax_array = str("{rax}")
82 \\@r11_array = str("r11")82 \\@rdi_array = str("{rdi}")
83 \\@rdx_array = str("{rdx}")83 \\@rcx_array = str("rcx")
84 \\@rsi_array = str("{rsi}")84 \\@r11_array = str("r11")
85 \\@memory_array = str("memory")85 \\@rdx_array = str("{rdx}")
86 \\@len_array = str("len")86 \\@rsi_array = str("{rsi}")
87 \\87 \\@memory_array = str("memory")
88 \\@msg = str("Hello, world!\n")88 \\@len_array = str("len")
89 \\89 \\
90 \\@start_fnty = fntype([], @noreturn, cc=Naked)90 \\@msg = str("Hello, world!\n")
91 \\@start = fn(@start_fnty, {91 \\
92 \\ %SYS_exit_group = int(231)92 \\@start_fnty = fntype([], @noreturn, cc=Naked)
93 \\ %exit_code = as(@usize, @0)93 \\@start = fn(@start_fnty, {
94 \\94 \\ %SYS_exit_group = int(231)
95 \\ %syscall = ref(@syscall_array)95 \\ %exit_code = as(@usize, @0)
96 \\ %sysoutreg = ref(@sysoutreg_array)96 \\
97 \\ %rax = ref(@rax_array)97 \\ %syscall = ref(@syscall_array)
98 \\ %rdi = ref(@rdi_array)98 \\ %sysoutreg = ref(@sysoutreg_array)
99 \\ %rcx = ref(@rcx_array)99 \\ %rax = ref(@rax_array)
100 \\ %rdx = ref(@rdx_array)100 \\ %rdi = ref(@rdi_array)
101 \\ %rsi = ref(@rsi_array)101 \\ %rcx = ref(@rcx_array)
102 \\ %r11 = ref(@r11_array)102 \\ %rdx = ref(@rdx_array)
103 \\ %memory = ref(@memory_array)103 \\ %rsi = ref(@rsi_array)
104 \\104 \\ %r11 = ref(@r11_array)
105 \\ %SYS_write = as(@usize, @1)105 \\ %memory = ref(@memory_array)
106 \\ %STDOUT_FILENO = as(@usize, @1)106 \\
107 \\107 \\ %SYS_write = as(@usize, @1)
108 \\ %msg_ptr = ref(@msg)108 \\ %STDOUT_FILENO = as(@usize, @1)
109 \\ %msg_addr = ptrtoint(%msg_ptr)109 \\
110 \\110 \\ %msg_ptr = ref(@msg)
111 \\ %len_name = ref(@len_array)111 \\ %msg_addr = ptrtoint(%msg_ptr)
112 \\ %msg_len_ptr = fieldptr(%msg_ptr, %len_name)112 \\
113 \\ %msg_len = deref(%msg_len_ptr)113 \\ %len_name = ref(@len_array)
114 \\ %rc_write = asm(%syscall, @usize,114 \\ %msg_len_ptr = fieldptr(%msg_ptr, %len_name)
115 \\ volatile=1,115 \\ %msg_len = deref(%msg_len_ptr)
116 \\ output=%sysoutreg,116 \\ %rc_write = asm(%syscall, @usize,
117 \\ inputs=[%rax, %rdi, %rsi, %rdx],117 \\ volatile=1,
118 \\ clobbers=[%rcx, %r11, %memory],118 \\ output=%sysoutreg,
119 \\ args=[%SYS_write, %STDOUT_FILENO, %msg_addr, %msg_len])119 \\ inputs=[%rax, %rdi, %rsi, %rdx],
120 \\120 \\ clobbers=[%rcx, %r11, %memory],
121 \\ %rc_exit = asm(%syscall, @usize,121 \\ args=[%SYS_write, %STDOUT_FILENO, %msg_addr, %msg_len])
122 \\ volatile=1,122 \\
123 \\ output=%sysoutreg,123 \\ %rc_exit = asm(%syscall, @usize,
124 \\ inputs=[%rax, %rdi],124 \\ volatile=1,
125 \\ clobbers=[%rcx, %r11, %memory],125 \\ output=%sysoutreg,
126 \\ args=[%SYS_exit_group, %exit_code])126 \\ inputs=[%rax, %rdi],
127 \\127 \\ clobbers=[%rcx, %r11, %memory],
128 \\ %99 = unreachable()128 \\ args=[%SYS_exit_group, %exit_code])
129 \\});129 \\
130 \\130 \\ %99 = unreachable()
131 \\@9 = str("_start")131 \\});
132 \\@10 = ref(@9)132 \\
133 \\@11 = export(@10, @start)133 \\@9 = str("_start")
134 ,134 \\@10 = ref(@9)
135 \\Hello, world!135 \\@11 = export(@10, @start)
136 \\136 ,
137 \\@noreturn = primitive(noreturn)
138 \\@void = primitive(void)
139 \\@usize = primitive(usize)
140 \\@0 = int(0)
141 \\@1 = int(1)
142 \\@2 = int(2)
143 \\@3 = int(3)
144 \\
145 \\@syscall_array = str("syscall")
146 \\@sysoutreg_array = str("={rax}")
147 \\@rax_array = str("{rax}")
148 \\@rdi_array = str("{rdi}")
149 \\@rcx_array = str("rcx")
150 \\@r11_array = str("r11")
151 \\@rdx_array = str("{rdx}")
152 \\@rsi_array = str("{rsi}")
153 \\@memory_array = str("memory")
154 \\@len_array = str("len")
155 \\
156 \\@msg = str("Hello, world!\n")
157 \\@msg2 = str("HELL WORLD\n")
158 \\
159 \\@start_fnty = fntype([], @noreturn, cc=Naked)
160 \\@start = fn(@start_fnty, {
161 \\ %SYS_exit_group = int(231)
162 \\ %exit_code = as(@usize, @0)
163 \\
164 \\ %syscall = ref(@syscall_array)
165 \\ %sysoutreg = ref(@sysoutreg_array)
166 \\ %rax = ref(@rax_array)
167 \\ %rdi = ref(@rdi_array)
168 \\ %rcx = ref(@rcx_array)
169 \\ %rdx = ref(@rdx_array)
170 \\ %rsi = ref(@rsi_array)
171 \\ %r11 = ref(@r11_array)
172 \\ %memory = ref(@memory_array)
173 \\
174 \\ %SYS_write = as(@usize, @1)
175 \\ %STDOUT_FILENO = as(@usize, @1)
176 \\
177 \\ %msg_ptr = ref(@msg2)
178 \\ %msg_addr = ptrtoint(%msg_ptr)
179 \\
180 \\ %len_name = ref(@len_array)
181 \\ %msg_len_ptr = fieldptr(%msg_ptr, %len_name)
182 \\ %msg_len = deref(%msg_len_ptr)
183 \\ %rc_write = asm(%syscall, @usize,
184 \\ volatile=1,
185 \\ output=%sysoutreg,
186 \\ inputs=[%rax, %rdi, %rsi, %rdx],
187 \\ clobbers=[%rcx, %r11, %memory],
188 \\ args=[%SYS_write, %STDOUT_FILENO, %msg_addr, %msg_len])
189 \\
190 \\ %rc_exit = asm(%syscall, @usize,
191 \\ volatile=1,
192 \\ output=%sysoutreg,
193 \\ inputs=[%rax, %rdi],
194 \\ clobbers=[%rcx, %r11, %memory],
195 \\ args=[%SYS_exit_group, %exit_code])
196 \\
197 \\ %99 = unreachable()
198 \\});
199 \\
200 \\@9 = str("_start")
201 \\@10 = ref(@9)
202 \\@11 = export(@10, @start)
203 },
204 &[_][]const u8{
205 \\Hello, world!
206 \\
207 ,
208 \\HELL WORLD
209 \\
210 },
137 );211 );
138}212}