authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-12 22:00:14-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-12 22:00:14-07:00
loge043396b242d727cd72fecda265bd4b78a86958a
tree6634cf1996d5e9d406c20bb29aa16fc2fbdc6deb
parentde4f3f11f735708cf9ffe4bbdbbfa693b6b07916
parenteec53d67abb3a3d894de945f549994a16cb92474

Merge branch 'pixelherodev-cbe'

closes #6007

6 files changed, 341 insertions(+), 84 deletions(-)

src-self-hosted/cbe.h+11-4
...@@ -1,8 +1,15 @@...@@ -1,8 +1,15 @@
1#if __STDC_VERSION__ >= 201112L1#if __STDC_VERSION__ >= 201112L
2#define noreturn _Noreturn2#define zig_noreturn _Noreturn
3#elif __GNUC__ && !__STRICT_ANSI__3#elif __GNUC__
4#define noreturn __attribute__ ((noreturn))4#define zig_noreturn __attribute__ ((noreturn))
5#elif _MSC_VER
6#define zig_noreturn __declspec(noreturn)
5#else7#else
6#define noreturn8#define zig_noreturn
7#endif9#endif
810
11#if __GNUC__
12#define zig_unreachable() __builtin_unreachable()
13#else
14#define zig_unreachable()
15#endif
src-self-hosted/codegen/c.zig+170-69
...@@ -17,40 +17,58 @@ fn map(allocator: *std.mem.Allocator, name: []const u8) ![]const u8 {...@@ -17,40 +17,58 @@ fn map(allocator: *std.mem.Allocator, name: []const u8) ![]const u8 {
17 return allocator.dupe(u8, name);17 return allocator.dupe(u8, name);
18}18}
1919
20fn renderType(file: *C, writer: std.ArrayList(u8).Writer, T: Type, src: usize) !void {20fn renderType(ctx: *Context, writer: std.ArrayList(u8).Writer, T: Type) !void {
21 if (T.tag() == .usize) {21 switch (T.zigTypeTag()) {
22 file.need_stddef = true;22 .NoReturn => {
23 try writer.writeAll("size_t");23 try writer.writeAll("zig_noreturn void");
24 } else {24 },
25 switch (T.zigTypeTag()) {25 .Void => try writer.writeAll("void"),
26 .NoReturn => {26 .Int => {
27 file.need_noreturn = true;27 if (T.tag() == .u8) {
28 try writer.writeAll("noreturn void");28 ctx.file.need_stdint = true;
29 },29 try writer.writeAll("uint8_t");
30 .Void => try writer.writeAll("void"),30 } else if (T.tag() == .usize) {
31 .Int => {31 ctx.file.need_stddef = true;
32 if (T.tag() == .u8) {32 try writer.writeAll("size_t");
33 file.need_stdint = true;33 } else {
34 try writer.writeAll("uint8_t");34 return ctx.file.fail(ctx.decl.src(), "TODO implement int types", .{});
35 } else {35 }
36 return file.fail(src, "TODO implement int types", .{});36 },
37 }37 else => |e| return ctx.file.fail(ctx.decl.src(), "TODO implement type {}", .{e}),
38 },
39 else => |e| return file.fail(src, "TODO implement type {}", .{e}),
40 }
41 }38 }
42}39}
4340
44fn renderFunctionSignature(file: *C, writer: std.ArrayList(u8).Writer, decl: *Decl) !void {41fn renderValue(ctx: *Context, writer: std.ArrayList(u8).Writer, T: Type, val: Value) !void {
42 switch (T.zigTypeTag()) {
43 .Int => {
44 if (T.isSignedInt())
45 return writer.print("{}", .{val.toSignedInt()});
46 return writer.print("{}", .{val.toUnsignedInt()});
47 },
48 else => |e| return ctx.file.fail(ctx.decl.src(), "TODO implement value {}", .{e}),
49 }
50}
51
52fn renderFunctionSignature(ctx: *Context, writer: std.ArrayList(u8).Writer, decl: *Decl) !void {
45 const tv = decl.typed_value.most_recent.typed_value;53 const tv = decl.typed_value.most_recent.typed_value;
46 try renderType(file, writer, tv.ty.fnReturnType(), decl.src());54 try renderType(ctx, writer, tv.ty.fnReturnType());
47 const name = try map(file.base.allocator, mem.spanZ(decl.name));55 const name = try map(ctx.file.base.allocator, mem.spanZ(decl.name));
48 defer file.base.allocator.free(name);56 defer ctx.file.base.allocator.free(name);
49 try writer.print(" {}(", .{name});57 try writer.print(" {}(", .{name});
50 if (tv.ty.fnParamLen() == 0)58 var param_len = tv.ty.fnParamLen();
51 try writer.writeAll("void)")59 if (param_len == 0)
52 else60 try writer.writeAll("void")
53 return file.fail(decl.src(), "TODO implement parameters", .{});61 else {
62 var index: usize = 0;
63 while (index < param_len) : (index += 1) {
64 if (index > 0) {
65 try writer.writeAll(", ");
66 }
67 try renderType(ctx, writer, tv.ty.fnParamType(index));
68 try writer.print(" arg{}", .{index});
69 }
70 }
71 try writer.writeByte(')');
54}72}
5573
56pub fn generate(file: *C, decl: *Decl) !void {74pub fn generate(file: *C, decl: *Decl) !void {
...@@ -78,11 +96,40 @@ fn genArray(file: *C, decl: *Decl) !void {...@@ -78,11 +96,40 @@ fn genArray(file: *C, decl: *Decl) !void {
78 return file.fail(decl.src(), "TODO non-byte arrays", .{});96 return file.fail(decl.src(), "TODO non-byte arrays", .{});
79}97}
8098
99const Context = struct {
100 file: *C,
101 decl: *Decl,
102 inst_map: std.AutoHashMap(*Inst, []u8),
103 argdex: usize = 0,
104 unnamed_index: usize = 0,
105
106 fn name(self: *Context) ![]u8 {
107 const val = try std.fmt.allocPrint(self.file.base.allocator, "__temp_{}", .{self.unnamed_index});
108 self.unnamed_index += 1;
109 return val;
110 }
111
112 fn deinit(self: *Context) void {
113 for (self.inst_map.items()) |kv| {
114 self.file.base.allocator.free(kv.value);
115 }
116 self.inst_map.deinit();
117 self.* = undefined;
118 }
119};
120
81fn genFn(file: *C, decl: *Decl) !void {121fn genFn(file: *C, decl: *Decl) !void {
82 const writer = file.main.writer();122 const writer = file.main.writer();
83 const tv = decl.typed_value.most_recent.typed_value;123 const tv = decl.typed_value.most_recent.typed_value;
84124
85 try renderFunctionSignature(file, writer, decl);125 var ctx = Context{
126 .file = file,
127 .decl = decl,
128 .inst_map = std.AutoHashMap(*Inst, []u8).init(file.base.allocator),
129 };
130 defer ctx.deinit();
131
132 try renderFunctionSignature(&ctx, writer, decl);
86133
87 try writer.writeAll(" {");134 try writer.writeAll(" {");
88135
...@@ -91,13 +138,19 @@ fn genFn(file: *C, decl: *Decl) !void {...@@ -91,13 +138,19 @@ fn genFn(file: *C, decl: *Decl) !void {
91 if (instructions.len > 0) {138 if (instructions.len > 0) {
92 try writer.writeAll("\n");139 try writer.writeAll("\n");
93 for (instructions) |inst| {140 for (instructions) |inst| {
94 switch (inst.tag) {141 if (switch (inst.tag) {
95 .assembly => try genAsm(file, inst.castTag(.assembly).?, decl),142 .assembly => try genAsm(&ctx, inst.castTag(.assembly).?),
96 .call => try genCall(file, inst.castTag(.call).?, decl),143 .call => try genCall(&ctx, inst.castTag(.call).?),
97 .ret => try genRet(file, inst.castTag(.ret).?, decl, tv.ty.fnReturnType()),144 .ret => try genRet(&ctx, inst.castTag(.ret).?),
98 .retvoid => try file.main.writer().print(" return;\n", .{}),145 .retvoid => try genRetVoid(&ctx),
99 .dbg_stmt => try genDbgStmt(file, inst.castTag(.dbg_stmt).?, decl),146 .arg => try genArg(&ctx),
147 .dbg_stmt => try genDbgStmt(&ctx, inst.castTag(.dbg_stmt).?),
148 .breakpoint => try genBreak(&ctx, inst.castTag(.breakpoint).?),
149 .unreach => try genUnreach(&ctx, inst.castTag(.unreach).?),
150 .intcast => try genIntCast(&ctx, inst.castTag(.intcast).?),
100 else => |e| return file.fail(decl.src(), "TODO implement C codegen for {}", .{e}),151 else => |e| return file.fail(decl.src(), "TODO implement C codegen for {}", .{e}),
152 }) |name| {
153 try ctx.inst_map.putNoClobber(inst, name);
101 }154 }
102 }155 }
103 }156 }
...@@ -105,13 +158,40 @@ fn genFn(file: *C, decl: *Decl) !void {...@@ -105,13 +158,40 @@ fn genFn(file: *C, decl: *Decl) !void {
105 try writer.writeAll("}\n\n");158 try writer.writeAll("}\n\n");
106}159}
107160
108fn genRet(file: *C, inst: *Inst.UnOp, decl: *Decl, expected_return_type: Type) !void {161fn genArg(ctx: *Context) !?[]u8 {
109 return file.fail(decl.src(), "TODO return {}", .{expected_return_type});162 const name = try std.fmt.allocPrint(ctx.file.base.allocator, "arg{}", .{ctx.argdex});
163 ctx.argdex += 1;
164 return name;
110}165}
111166
112fn genCall(file: *C, inst: *Inst.Call, decl: *Decl) !void {167fn genRetVoid(ctx: *Context) !?[]u8 {
113 const writer = file.main.writer();168 try ctx.file.main.writer().print(" return;\n", .{});
114 const header = file.header.writer();169 return null;
170}
171
172fn genRet(ctx: *Context, inst: *Inst.UnOp) !?[]u8 {
173 return ctx.file.fail(ctx.decl.src(), "TODO return", .{});
174}
175
176fn genIntCast(ctx: *Context, inst: *Inst.UnOp) !?[]u8 {
177 if (inst.base.isUnused())
178 return null;
179 const op = inst.operand;
180 const writer = ctx.file.main.writer();
181 const name = try ctx.name();
182 const from = ctx.inst_map.get(op) orelse
183 return ctx.file.fail(ctx.decl.src(), "Internal error in C backend: intCast argument not found in inst_map", .{});
184 try writer.writeAll(" const ");
185 try renderType(ctx, writer, inst.base.ty);
186 try writer.print(" {} = (", .{name});
187 try renderType(ctx, writer, inst.base.ty);
188 try writer.print("){};\n", .{from});
189 return name;
190}
191
192fn genCall(ctx: *Context, inst: *Inst.Call) !?[]u8 {
193 const writer = ctx.file.main.writer();
194 const header = ctx.file.header.writer();
115 try writer.writeAll(" ");195 try writer.writeAll(" ");
116 if (inst.func.castTag(.constant)) |func_inst| {196 if (inst.func.castTag(.constant)) |func_inst| {
117 if (func_inst.val.cast(Value.Payload.Function)) |func_val| {197 if (func_inst.val.cast(Value.Payload.Function)) |func_val| {
...@@ -122,52 +202,77 @@ fn genCall(file: *C, inst: *Inst.Call, decl: *Decl) !void {...@@ -122,52 +202,77 @@ fn genCall(file: *C, inst: *Inst.Call, decl: *Decl) !void {
122 try writer.print("(void)", .{});202 try writer.print("(void)", .{});
123 }203 }
124 const tname = mem.spanZ(target.name);204 const tname = mem.spanZ(target.name);
125 if (file.called.get(tname) == null) {205 if (ctx.file.called.get(tname) == null) {
126 try file.called.put(tname, void{});206 try ctx.file.called.put(tname, void{});
127 try renderFunctionSignature(file, header, target);207 try renderFunctionSignature(ctx, header, target);
128 try header.writeAll(";\n");208 try header.writeAll(";\n");
129 }209 }
130 try writer.print("{}();\n", .{tname});210 try writer.print("{}(", .{tname});
211 if (inst.args.len != 0) {
212 for (inst.args) |arg, i| {
213 if (i > 0) {
214 try writer.writeAll(", ");
215 }
216 if (arg.cast(Inst.Constant)) |con| {
217 try renderValue(ctx, writer, arg.ty, con.val);
218 } else {
219 return ctx.file.fail(ctx.decl.src(), "TODO call pass arg {}", .{arg});
220 }
221 }
222 }
223 try writer.writeAll(");\n");
131 } else {224 } else {
132 return file.fail(decl.src(), "TODO non-function call target?", .{});225 return ctx.file.fail(ctx.decl.src(), "TODO non-function call target?", .{});
133 }
134 if (inst.args.len != 0) {
135 return file.fail(decl.src(), "TODO function arguments", .{});
136 }226 }
137 } else {227 } else {
138 return file.fail(decl.src(), "TODO non-constant call inst?", .{});228 return ctx.file.fail(ctx.decl.src(), "TODO non-constant call inst?", .{});
139 }229 }
230 return null;
140}231}
141232
142fn genDbgStmt(file: *C, inst: *Inst.NoOp, decl: *Decl) !void {233fn genDbgStmt(ctx: *Context, inst: *Inst.NoOp) !?[]u8 {
143 // TODO emit #line directive here with line number and filename234 // TODO emit #line directive here with line number and filename
235 return null;
144}236}
145237
146fn genAsm(file: *C, as: *Inst.Assembly, decl: *Decl) !void {238fn genBreak(ctx: *Context, inst: *Inst.NoOp) !?[]u8 {
147 const writer = file.main.writer();239 // TODO ??
240 return null;
241}
242
243fn genUnreach(ctx: *Context, inst: *Inst.NoOp) !?[]u8 {
244 try ctx.file.main.writer().writeAll(" zig_unreachable();\n");
245 return null;
246}
247
248fn genAsm(ctx: *Context, as: *Inst.Assembly) !?[]u8 {
249 const writer = ctx.file.main.writer();
148 try writer.writeAll(" ");250 try writer.writeAll(" ");
149 for (as.inputs) |i, index| {251 for (as.inputs) |i, index| {
150 if (i[0] == '{' and i[i.len - 1] == '}') {252 if (i[0] == '{' and i[i.len - 1] == '}') {
151 const reg = i[1 .. i.len - 1];253 const reg = i[1 .. i.len - 1];
152 const arg = as.args[index];254 const arg = as.args[index];
255 try writer.writeAll("register ");
256 try renderType(ctx, writer, arg.ty);
257 try writer.print(" {}_constant __asm__(\"{}\") = ", .{ reg, reg });
258 // TODO merge constant handling into inst_map as well
153 if (arg.castTag(.constant)) |c| {259 if (arg.castTag(.constant)) |c| {
154 if (c.val.tag() == .int_u64) {260 try renderValue(ctx, writer, arg.ty, c.val);
155 try writer.writeAll("register ");261 try writer.writeAll(";\n ");
156 try renderType(file, writer, arg.ty, decl.src());
157 try writer.print(" {}_constant __asm__(\"{}\") = {};\n ", .{ reg, reg, c.val.toUnsignedInt() });
158 } else {
159 return file.fail(decl.src(), "TODO inline asm {} args", .{c.val.tag()});
160 }
161 } else {262 } else {
162 return file.fail(decl.src(), "TODO non-constant inline asm args", .{});263 const gop = try ctx.inst_map.getOrPut(arg);
264 if (!gop.found_existing) {
265 return ctx.file.fail(ctx.decl.src(), "Internal error in C backend: asm argument not found in inst_map", .{});
266 }
267 try writer.print("{};\n ", .{gop.entry.value});
163 }268 }
164 } else {269 } else {
165 return file.fail(decl.src(), "TODO non-explicit inline asm regs", .{});270 return ctx.file.fail(ctx.decl.src(), "TODO non-explicit inline asm regs", .{});
166 }271 }
167 }272 }
168 try writer.print("__asm {} (\"{}\"", .{ if (as.is_volatile) @as([]const u8, "volatile") else "", as.asm_source });273 try writer.print("__asm {} (\"{}\"", .{ if (as.is_volatile) @as([]const u8, "volatile") else "", as.asm_source });
169 if (as.output) |o| {274 if (as.output) |o| {
170 return file.fail(decl.src(), "TODO inline asm output", .{});275 return ctx.file.fail(ctx.decl.src(), "TODO inline asm output", .{});
171 }276 }
172 if (as.inputs.len > 0) {277 if (as.inputs.len > 0) {
173 if (as.output == null) {278 if (as.output == null) {
...@@ -181,12 +286,7 @@ fn genAsm(file: *C, as: *Inst.Assembly, decl: *Decl) !void {...@@ -181,12 +286,7 @@ fn genAsm(file: *C, as: *Inst.Assembly, decl: *Decl) !void {
181 if (index > 0) {286 if (index > 0) {
182 try writer.writeAll(", ");287 try writer.writeAll(", ");
183 }288 }
184 if (arg.castTag(.constant)) |c| {289 try writer.print("\"\"({}_constant)", .{reg});
185 try writer.print("\"\"({}_constant)", .{reg});
186 } else {
187 // This is blocked by the earlier test
188 unreachable;
189 }
190 } else {290 } else {
191 // This is blocked by the earlier test291 // This is blocked by the earlier test
192 unreachable;292 unreachable;
...@@ -194,4 +294,5 @@ fn genAsm(file: *C, as: *Inst.Assembly, decl: *Decl) !void {...@@ -194,4 +294,5 @@ fn genAsm(file: *C, as: *Inst.Assembly, decl: *Decl) !void {
194 }294 }
195 }295 }
196 try writer.writeAll(");\n");296 try writer.writeAll(");\n");
297 return null;
197}298}
src-self-hosted/link.zig+1-2
...@@ -202,7 +202,6 @@ pub const File = struct {...@@ -202,7 +202,6 @@ pub const File = struct {
202 called: std.StringHashMap(void),202 called: std.StringHashMap(void),
203 need_stddef: bool = false,203 need_stddef: bool = false,
204 need_stdint: bool = false,204 need_stdint: bool = false,
205 need_noreturn: bool = false,
206 error_msg: *Module.ErrorMsg = undefined,205 error_msg: *Module.ErrorMsg = undefined,
207206
208 pub fn openPath(allocator: *Allocator, dir: fs.Dir, sub_path: []const u8, options: Options) !*File {207 pub fn openPath(allocator: *Allocator, dir: fs.Dir, sub_path: []const u8, options: Options) !*File {
...@@ -230,7 +229,7 @@ pub const File = struct {...@@ -230,7 +229,7 @@ pub const File = struct {
230 return &c_file.base;229 return &c_file.base;
231 }230 }
232231
233 pub fn fail(self: *C, src: usize, comptime format: []const u8, args: anytype) !void {232 pub fn fail(self: *C, src: usize, comptime format: []const u8, args: anytype) error{AnalysisFail, OutOfMemory} {
234 self.error_msg = try Module.ErrorMsg.create(self.base.allocator, src, format, args);233 self.error_msg = try Module.ErrorMsg.create(self.base.allocator, src, format, args);
235 return error.AnalysisFail;234 return error.AnalysisFail;
236 }235 }
src-self-hosted/test.zig+4
...@@ -478,6 +478,10 @@ pub const TestContext = struct {...@@ -478,6 +478,10 @@ pub const TestContext = struct {
478 for (all_errors.list) |err| {478 for (all_errors.list) |err| {
479 std.debug.warn(":{}:{}: error: {}\n================\n", .{ err.line + 1, err.column + 1, err.msg });479 std.debug.warn(":{}:{}: error: {}\n================\n", .{ err.line + 1, err.column + 1, err.msg });
480 }480 }
481 if (case.cbe) {
482 const C = module.bin_file.cast(link.File.C).?;
483 std.debug.warn("Generated C: \n===============\n{}\n\n===========\n\n", .{C.main.items});
484 }
481 std.debug.warn("Test failed.\n", .{});485 std.debug.warn("Test failed.\n", .{});
482 std.process.exit(1);486 std.process.exit(1);
483 }487 }
src-self-hosted/value.zig+75
...@@ -568,6 +568,81 @@ pub const Value = extern union {...@@ -568,6 +568,81 @@ pub const Value = extern union {
568 }568 }
569 }569 }
570570
571 /// Asserts the value is an integer and it fits in a i64
572 pub fn toSignedInt(self: Value) i64 {
573 switch (self.tag()) {
574 .ty,
575 .int_type,
576 .u8_type,
577 .i8_type,
578 .u16_type,
579 .i16_type,
580 .u32_type,
581 .i32_type,
582 .u64_type,
583 .i64_type,
584 .usize_type,
585 .isize_type,
586 .c_short_type,
587 .c_ushort_type,
588 .c_int_type,
589 .c_uint_type,
590 .c_long_type,
591 .c_ulong_type,
592 .c_longlong_type,
593 .c_ulonglong_type,
594 .c_longdouble_type,
595 .f16_type,
596 .f32_type,
597 .f64_type,
598 .f128_type,
599 .c_void_type,
600 .bool_type,
601 .void_type,
602 .type_type,
603 .anyerror_type,
604 .comptime_int_type,
605 .comptime_float_type,
606 .noreturn_type,
607 .null_type,
608 .undefined_type,
609 .fn_noreturn_no_args_type,
610 .fn_void_no_args_type,
611 .fn_naked_noreturn_no_args_type,
612 .fn_ccc_void_no_args_type,
613 .single_const_pointer_to_comptime_int_type,
614 .const_slice_u8_type,
615 .null_value,
616 .function,
617 .ref_val,
618 .decl_ref,
619 .elem_ptr,
620 .bytes,
621 .repeated,
622 .float_16,
623 .float_32,
624 .float_64,
625 .float_128,
626 .void_value,
627 .unreachable_value,
628 .empty_array,
629 => unreachable,
630
631 .undef => unreachable,
632
633 .zero,
634 .bool_false,
635 => return 0,
636
637 .bool_true => return 1,
638
639 .int_u64 => return @intCast(i64, self.cast(Payload.Int_i64).?.int),
640 .int_i64 => return self.cast(Payload.Int_i64).?.int,
641 .int_big_positive => return self.cast(Payload.IntBigPositive).?.asBigInt().to(i64) catch unreachable,
642 .int_big_negative => return self.cast(Payload.IntBigNegative).?.asBigInt().to(i64) catch unreachable,
643 }
644 }
645
571 pub fn toBool(self: Value) bool {646 pub fn toBool(self: Value) bool {
572 return switch (self.tag()) {647 return switch (self.tag()) {
573 .bool_true => true,648 .bool_true => true,
test/stage2/cbe.zig+80-9
...@@ -12,7 +12,7 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -12,7 +12,7 @@ pub fn addCases(ctx: *TestContext) !void {
12 ctx.c("empty start function", linux_x64,12 ctx.c("empty start function", linux_x64,
13 \\export fn _start() noreturn {}13 \\export fn _start() noreturn {}
14 ,14 ,
15 \\noreturn void _start(void) {}15 \\zig_noreturn void _start(void) {}
16 \\16 \\
17 );17 );
18 ctx.c("less empty start function", linux_x64,18 ctx.c("less empty start function", linux_x64,
...@@ -22,19 +22,19 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -22,19 +22,19 @@ pub fn addCases(ctx: *TestContext) !void {
22 \\ main();22 \\ main();
23 \\}23 \\}
24 ,24 ,
25 \\noreturn void main(void);25 \\zig_noreturn void main(void);
26 \\26 \\
27 \\noreturn void _start(void) {27 \\zig_noreturn void _start(void) {
28 \\ main();28 \\ main();
29 \\}29 \\}
30 \\30 \\
31 \\noreturn void main(void) {}31 \\zig_noreturn void main(void) {}
32 \\32 \\
33 );33 );
34 // TODO: implement return values34 // TODO: implement return values
35 // TODO: figure out a way to prevent asm constants from being generated35 // TODO: figure out a way to prevent asm constants from being generated
36 ctx.c("inline asm", linux_x64,36 ctx.c("inline asm", linux_x64,
37 \\fn exitGood() void {37 \\fn exitGood() noreturn {
38 \\ asm volatile ("syscall"38 \\ asm volatile ("syscall"
39 \\ :39 \\ :
40 \\ : [number] "{rax}" (231),40 \\ : [number] "{rax}" (231),
...@@ -48,21 +48,92 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -48,21 +48,92 @@ pub fn addCases(ctx: *TestContext) !void {
48 ,48 ,
49 \\#include <stddef.h>49 \\#include <stddef.h>
50 \\50 \\
51 \\void exitGood(void);51 \\zig_noreturn void exitGood(void);
52 \\52 \\
53 \\const char *const exitGood__anon_0 = "{rax}";53 \\const char *const exitGood__anon_0 = "{rax}";
54 \\const char *const exitGood__anon_1 = "{rdi}";54 \\const char *const exitGood__anon_1 = "{rdi}";
55 \\const char *const exitGood__anon_2 = "syscall";55 \\const char *const exitGood__anon_2 = "syscall";
56 \\56 \\
57 \\noreturn void _start(void) {57 \\zig_noreturn void _start(void) {
58 \\ exitGood();58 \\ exitGood();
59 \\}59 \\}
60 \\60 \\
61 \\void exitGood(void) {61 \\zig_noreturn void exitGood(void) {
62 \\ register size_t rax_constant __asm__("rax") = 231;62 \\ register size_t rax_constant __asm__("rax") = 231;
63 \\ register size_t rdi_constant __asm__("rdi") = 0;63 \\ register size_t rdi_constant __asm__("rdi") = 0;
64 \\ __asm volatile ("syscall" :: ""(rax_constant), ""(rdi_constant));64 \\ __asm volatile ("syscall" :: ""(rax_constant), ""(rdi_constant));
65 \\ return;65 \\}
66 \\
67 );
68 ctx.c("exit with parameter", linux_x64,
69 \\export fn _start() noreturn {
70 \\ exit(0);
71 \\}
72 \\
73 \\fn exit(code: usize) noreturn {
74 \\ asm volatile ("syscall"
75 \\ :
76 \\ : [number] "{rax}" (231),
77 \\ [arg1] "{rdi}" (code)
78 \\ );
79 \\ unreachable;
80 \\}
81 \\
82 ,
83 \\#include <stddef.h>
84 \\
85 \\zig_noreturn void exit(size_t arg0);
86 \\
87 \\const char *const exit__anon_0 = "{rax}";
88 \\const char *const exit__anon_1 = "{rdi}";
89 \\const char *const exit__anon_2 = "syscall";
90 \\
91 \\zig_noreturn void _start(void) {
92 \\ exit(0);
93 \\}
94 \\
95 \\zig_noreturn void exit(size_t arg0) {
96 \\ register size_t rax_constant __asm__("rax") = 231;
97 \\ register size_t rdi_constant __asm__("rdi") = arg0;
98 \\ __asm volatile ("syscall" :: ""(rax_constant), ""(rdi_constant));
99 \\ zig_unreachable();
100 \\}
101 \\
102 );
103 ctx.c("exit with u8 parameter", linux_x64,
104 \\export fn _start() noreturn {
105 \\ exit(0);
106 \\}
107 \\
108 \\fn exit(code: u8) noreturn {
109 \\ asm volatile ("syscall"
110 \\ :
111 \\ : [number] "{rax}" (231),
112 \\ [arg1] "{rdi}" (code)
113 \\ );
114 \\ unreachable;
115 \\}
116 \\
117 ,
118 \\#include <stddef.h>
119 \\#include <stdint.h>
120 \\
121 \\zig_noreturn void exit(uint8_t arg0);
122 \\
123 \\const char *const exit__anon_0 = "{rax}";
124 \\const char *const exit__anon_1 = "{rdi}";
125 \\const char *const exit__anon_2 = "syscall";
126 \\
127 \\zig_noreturn void _start(void) {
128 \\ exit(0);
129 \\}
130 \\
131 \\zig_noreturn void exit(uint8_t arg0) {
132 \\ const size_t __temp_0 = (size_t)arg0;
133 \\ register size_t rax_constant __asm__("rax") = 231;
134 \\ register size_t rdi_constant __asm__("rdi") = __temp_0;
135 \\ __asm volatile ("syscall" :: ""(rax_constant), ""(rdi_constant));
136 \\ zig_unreachable();
66 \\}137 \\}
67 \\138 \\
68 );139 );