From 87c6341b61aa54301aa98fea1a449fff40ba25af Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 28 Dec 2020 17:15:29 -0700 Subject: stage2: add extern functions and improve the C backend enough to support Hello World (almost) --- src/codegen/c.zig | 377 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 245 insertions(+), 132 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 589e2f17e0..8d706f8735 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -12,7 +12,7 @@ const C = link.File.C; const Decl = Module.Decl; const mem = std.mem; -const indentation = " "; +const Writer = std.ArrayList(u8).Writer; /// Maps a name from Zig source to C. Currently, this will always give the same /// output for any given input, sometimes resulting in broken identifiers. @@ -20,43 +20,145 @@ fn map(allocator: *std.mem.Allocator, name: []const u8) ![]const u8 { return allocator.dupe(u8, name); } -fn renderType(ctx: *Context, header: *C.Header, writer: std.ArrayList(u8).Writer, T: Type) !void { - switch (T.zigTypeTag()) { +fn renderType( + ctx: *Context, + header: *C.Header, + writer: Writer, + t: Type, +) error{ OutOfMemory, AnalysisFail }!void { + switch (t.zigTypeTag()) { .NoReturn => { try writer.writeAll("zig_noreturn void"); }, .Void => try writer.writeAll("void"), .Bool => try writer.writeAll("bool"), .Int => { - if (T.tag() == .u8) { - header.need_stdint = true; - try writer.writeAll("uint8_t"); - } else if (T.tag() == .u32) { - header.need_stdint = true; - try writer.writeAll("uint32_t"); - } else if (T.tag() == .usize) { - header.need_stddef = true; - try writer.writeAll("size_t"); + switch (t.tag()) { + .u8 => try writer.writeAll("uint8_t"), + .i8 => try writer.writeAll("int8_t"), + .u16 => try writer.writeAll("uint16_t"), + .i16 => try writer.writeAll("int16_t"), + .u32 => try writer.writeAll("uint32_t"), + .i32 => try writer.writeAll("int32_t"), + .u64 => try writer.writeAll("uint64_t"), + .i64 => try writer.writeAll("int64_t"), + .usize => try writer.writeAll("uintptr_t"), + .isize => try writer.writeAll("intptr_t"), + .c_short => try writer.writeAll("short"), + .c_ushort => try writer.writeAll("unsigned short"), + .c_int => try writer.writeAll("int"), + .c_uint => try writer.writeAll("unsigned int"), + .c_long => try writer.writeAll("long"), + .c_ulong => try writer.writeAll("unsigned long"), + .c_longlong => try writer.writeAll("long long"), + .c_ulonglong => try writer.writeAll("unsigned long long"), + .int_signed, .int_unsigned => { + const info = t.intInfo(ctx.target); + const sign_prefix = switch (info.signedness) { + .signed => "i", + .unsigned => "", + }; + inline for (.{ 8, 16, 32, 64, 128 }) |nbits| { + if (info.bits <= nbits) { + try writer.print("{s}int{d}_t", .{ sign_prefix, nbits }); + break; + } + } else { + return ctx.fail(ctx.decl.src(), "TODO: C backend: implement integer types larger than 128 bits", .{}); + } + }, + else => unreachable, + } + }, + .Pointer => { + if (t.isSlice()) { + return ctx.fail(ctx.decl.src(), "TODO: C backend: implement slices", .{}); } else { - return ctx.fail(ctx.decl.src(), "TODO implement int type {}", .{T}); + if (t.isConstPtr()) { + try writer.writeAll("const "); + } + if (t.isVolatilePtr()) { + try writer.writeAll("volatile "); + } + try renderType(ctx, header, writer, t.elemType()); + try writer.writeAll(" *"); } }, - else => |e| return ctx.fail(ctx.decl.src(), "TODO implement type {}", .{e}), + .Array => { + try renderType(ctx, header, writer, t.elemType()); + const sentinel_bit = @boolToInt(t.sentinel() != null); + const c_len = t.arrayLen() + sentinel_bit; + try writer.print("[{d}]", .{c_len}); + }, + else => |e| return ctx.fail(ctx.decl.src(), "TODO: C backend: implement type {s}", .{ + @tagName(e), + }), } } -fn renderValue(ctx: *Context, writer: std.ArrayList(u8).Writer, T: Type, val: Value) !void { - switch (T.zigTypeTag()) { +fn renderValue( + ctx: *Context, + writer: Writer, + t: Type, + val: Value, +) error{ OutOfMemory, AnalysisFail }!void { + switch (t.zigTypeTag()) { .Int => { - if (T.isSignedInt()) - return writer.print("{}", .{val.toSignedInt()}); - return writer.print("{}", .{val.toUnsignedInt()}); + if (t.isSignedInt()) + return writer.print("{d}", .{val.toSignedInt()}); + return writer.print("{d}", .{val.toUnsignedInt()}); + }, + .Pointer => switch (val.tag()) { + .undef, .zero => try writer.writeAll("0"), + .one => try writer.writeAll("1"), + .decl_ref => { + const decl_ref_payload = val.cast(Value.Payload.DeclRef).?; + try writer.print("&{s}", .{decl_ref_payload.decl.name}); + }, + .function => { + const payload = val.cast(Value.Payload.Function).?; + try writer.print("{s}", .{payload.func.owner_decl.name}); + }, + .extern_fn => { + const payload = val.cast(Value.Payload.ExternFn).?; + try writer.print("{s}", .{payload.decl.name}); + }, + else => |e| return ctx.fail( + ctx.decl.src(), + "TODO: C backend: implement Pointer value {s}", + .{@tagName(e)}, + ), + }, + .Array => { + // TODO first try specific tag representations for more efficiency + // Fall back to inefficient generic implementation. + try writer.writeAll("{"); + var index: usize = 0; + const len = t.arrayLen(); + const elem_ty = t.elemType(); + while (index < len) : (index += 1) { + if (index != 0) try writer.writeAll(","); + const elem_val = try val.elemValue(&ctx.arena.allocator, index); + try renderValue(ctx, writer, elem_ty, elem_val); + } + if (t.sentinel()) |sentinel_val| { + if (index != 0) try writer.writeAll(","); + try renderValue(ctx, writer, elem_ty, sentinel_val); + } + try writer.writeAll("}"); }, - else => |e| return ctx.fail(ctx.decl.src(), "TODO implement value {}", .{e}), + else => |e| return ctx.fail(ctx.decl.src(), "TODO: C backend: implement value {s}", .{ + @tagName(e), + }), } } -fn renderFunctionSignature(ctx: *Context, header: *C.Header, writer: std.ArrayList(u8).Writer, decl: *Decl) !void { +fn renderFunctionSignature( + ctx: *Context, + header: *C.Header, + writer: Writer, + decl: *Decl, +) !void { const tv = decl.typed_value.most_recent.typed_value; try renderType(ctx, header, writer, tv.ty.fnReturnType()); // Use the child allocator directly, as we know the name can be freed before @@ -81,27 +183,92 @@ fn renderFunctionSignature(ctx: *Context, header: *C.Header, writer: std.ArrayLi } pub fn generate(file: *C, decl: *Decl) !void { - switch (decl.typed_value.most_recent.typed_value.ty.zigTypeTag()) { - .Fn => try genFn(file, decl), - .Array => try genArray(file, decl), - else => |e| return file.fail(decl.src(), "TODO {}", .{e}), + const tv = decl.typed_value.most_recent.typed_value; + + var arena = std.heap.ArenaAllocator.init(file.base.allocator); + defer arena.deinit(); + var inst_map = std.AutoHashMap(*Inst, []u8).init(&arena.allocator); + defer inst_map.deinit(); + var ctx = Context{ + .decl = decl, + .arena = &arena, + .inst_map = &inst_map, + .target = file.base.options.target, + }; + defer { + file.error_msg = ctx.error_msg; + ctx.deinit(); + } + + if (tv.val.cast(Value.Payload.Function)) |func_payload| { + const writer = file.main.writer(); + try renderFunctionSignature(&ctx, &file.header, writer, decl); + + try writer.writeAll(" {"); + + const func: *Module.Fn = func_payload.func; + const instructions = func.analysis.success.instructions; + if (instructions.len > 0) { + try writer.writeAll("\n"); + for (instructions) |inst| { + const indent_size = 4; + const indent_level = 1; + try writer.writeByteNTimes(' ', indent_size * indent_level); + if (switch (inst.tag) { + .assembly => try genAsm(&ctx, file, inst.castTag(.assembly).?), + .call => try genCall(&ctx, file, inst.castTag(.call).?), + .add => try genBinOp(&ctx, file, inst.cast(Inst.BinOp).?, "+"), + .sub => try genBinOp(&ctx, file, inst.cast(Inst.BinOp).?, "-"), + .ret => try genRet(&ctx, file, inst.castTag(.ret).?), + .retvoid => try genRetVoid(file), + .arg => try genArg(&ctx), + .dbg_stmt => try genDbgStmt(&ctx, inst.castTag(.dbg_stmt).?), + .breakpoint => try genBreak(&ctx, inst.castTag(.breakpoint).?), + .unreach => try genUnreach(file, inst.castTag(.unreach).?), + .intcast => try genIntCast(&ctx, file, inst.castTag(.intcast).?), + else => |e| return ctx.fail(decl.src(), "TODO implement C codegen for {}", .{e}), + }) |name| { + try ctx.inst_map.putNoClobber(inst, name); + } + } + } + + try writer.writeAll("}\n\n"); + } else if (tv.val.tag() == .extern_fn) { + return; // handled when referenced + } else { + const writer = file.constants.writer(); + try writer.writeAll("static "); + + // TODO ask the Decl if it is const + // https://github.com/ziglang/zig/issues/7582 + + try renderType(&ctx, &file.header, writer, tv.ty); + try writer.print(" {s} = ", .{decl.name}); + try renderValue(&ctx, writer, tv.ty, tv.val); + try writer.writeAll(";\n"); } } pub fn generateHeader( - arena: *std.heap.ArenaAllocator, + comp: *Compilation, module: *Module, header: *C.Header, decl: *Decl, ) error{ AnalysisFail, OutOfMemory }!void { switch (decl.typed_value.most_recent.typed_value.ty.zigTypeTag()) { .Fn => { - var inst_map = std.AutoHashMap(*Inst, []u8).init(&arena.allocator); + var inst_map = std.AutoHashMap(*Inst, []u8).init(comp.gpa); defer inst_map.deinit(); + + var arena = std.heap.ArenaAllocator.init(comp.gpa); + defer arena.deinit(); + var ctx = Context{ .decl = decl, - .arena = arena, + .arena = &arena, .inst_map = &inst_map, + .target = comp.getTarget(), }; const writer = header.buf.writer(); renderFunctionSignature(&ctx, header, writer, decl) catch |err| { @@ -116,24 +283,6 @@ pub fn generateHeader( } } -fn genArray(file: *C, decl: *Decl) !void { - const tv = decl.typed_value.most_recent.typed_value; - // TODO: prevent inline asm constants from being emitted - const name = try map(file.base.allocator, mem.span(decl.name)); - defer file.base.allocator.free(name); - if (tv.val.cast(Value.Payload.Bytes)) |payload| - if (tv.ty.sentinel()) |sentinel| - if (sentinel.toUnsignedInt() == 0) - // TODO: static by default - try file.constants.writer().print("const char *const {} = \"{}\";\n", .{ name, payload.data }) - else - return file.fail(decl.src(), "TODO byte arrays with non-zero sentinels", .{}) - else - return file.fail(decl.src(), "TODO byte arrays without sentinels", .{}) - else - return file.fail(decl.src(), "TODO non-byte arrays", .{}); -} - const Context = struct { decl: *Decl, inst_map: *std.AutoHashMap(*Inst, []u8), @@ -141,6 +290,7 @@ const Context = struct { argdex: usize = 0, unnamed_index: usize = 0, error_msg: *Compilation.ErrorMsg = undefined, + target: std.Target, fn resolveInst(self: *Context, inst: *Inst) ![]u8 { if (inst.cast(Inst.Constant)) |const_inst| { @@ -170,55 +320,6 @@ const Context = struct { } }; -fn genFn(file: *C, decl: *Decl) !void { - const writer = file.main.writer(); - const tv = decl.typed_value.most_recent.typed_value; - - var arena = std.heap.ArenaAllocator.init(file.base.allocator); - defer arena.deinit(); - var inst_map = std.AutoHashMap(*Inst, []u8).init(&arena.allocator); - defer inst_map.deinit(); - var ctx = Context{ - .decl = decl, - .arena = &arena, - .inst_map = &inst_map, - }; - defer { - file.error_msg = ctx.error_msg; - ctx.deinit(); - } - - try renderFunctionSignature(&ctx, &file.header, writer, decl); - - try writer.writeAll(" {"); - - const func: *Module.Fn = tv.val.cast(Value.Payload.Function).?.func; - const instructions = func.analysis.success.instructions; - if (instructions.len > 0) { - try writer.writeAll("\n"); - for (instructions) |inst| { - if (switch (inst.tag) { - .assembly => try genAsm(&ctx, file, inst.castTag(.assembly).?), - .call => try genCall(&ctx, file, inst.castTag(.call).?), - .add => try genBinOp(&ctx, file, inst.cast(Inst.BinOp).?, "+"), - .sub => try genBinOp(&ctx, file, inst.cast(Inst.BinOp).?, "-"), - .ret => try genRet(&ctx, inst.castTag(.ret).?), - .retvoid => try genRetVoid(file), - .arg => try genArg(&ctx), - .dbg_stmt => try genDbgStmt(&ctx, inst.castTag(.dbg_stmt).?), - .breakpoint => try genBreak(&ctx, inst.castTag(.breakpoint).?), - .unreach => try genUnreach(file, inst.castTag(.unreach).?), - .intcast => try genIntCast(&ctx, file, inst.castTag(.intcast).?), - else => |e| return ctx.fail(decl.src(), "TODO implement C codegen for {}", .{e}), - }) |name| { - try ctx.inst_map.putNoClobber(inst, name); - } - } - } - - try writer.writeAll("}\n\n"); -} - fn genArg(ctx: *Context) !?[]u8 { const name = try std.fmt.allocPrint(&ctx.arena.allocator, "arg{}", .{ctx.argdex}); ctx.argdex += 1; @@ -226,12 +327,24 @@ fn genArg(ctx: *Context) !?[]u8 { } fn genRetVoid(file: *C) !?[]u8 { - try file.main.writer().print(indentation ++ "return;\n", .{}); + try file.main.writer().print("return;\n", .{}); return null; } -fn genRet(ctx: *Context, inst: *Inst.UnOp) !?[]u8 { - return ctx.fail(ctx.decl.src(), "TODO return", .{}); +fn genRet(ctx: *Context, file: *C, inst: *Inst.UnOp) !?[]u8 { + const writer = file.main.writer(); + try writer.writeAll("return "); + try genValue(ctx, writer, inst.operand); + try writer.writeAll(";\n"); + return null; +} + +fn genValue(ctx: *Context, writer: Writer, inst: *Inst) !void { + if (inst.value()) |val| { + try renderValue(ctx, writer, inst.ty, val); + return; + } + return ctx.fail(ctx.decl.src(), "TODO: C backend: genValue for non-constant value", .{}); } fn genIntCast(ctx: *Context, file: *C, inst: *Inst.UnOp) !?[]u8 { @@ -241,7 +354,7 @@ fn genIntCast(ctx: *Context, file: *C, inst: *Inst.UnOp) !?[]u8 { const writer = file.main.writer(); const name = try ctx.name(); const from = try ctx.resolveInst(inst.operand); - try writer.writeAll(indentation ++ "const "); + try writer.writeAll("const "); try renderType(ctx, &file.header, writer, inst.base.ty); try writer.print(" {} = (", .{name}); try renderType(ctx, &file.header, writer, inst.base.ty); @@ -256,7 +369,7 @@ fn genBinOp(ctx: *Context, file: *C, inst: *Inst.BinOp, comptime operator: []con const rhs = ctx.resolveInst(inst.rhs); const writer = file.main.writer(); const name = try ctx.name(); - try writer.writeAll(indentation ++ "const "); + try writer.writeAll("const "); try renderType(ctx, &file.header, writer, inst.base.ty); try writer.print(" {} = {} " ++ operator ++ " {};\n", .{ name, lhs, rhs }); return name; @@ -265,41 +378,42 @@ fn genBinOp(ctx: *Context, file: *C, inst: *Inst.BinOp, comptime operator: []con fn genCall(ctx: *Context, file: *C, inst: *Inst.Call) !?[]u8 { const writer = file.main.writer(); const header = file.header.buf.writer(); - try writer.writeAll(indentation); if (inst.func.castTag(.constant)) |func_inst| { - if (func_inst.val.cast(Value.Payload.Function)) |func_val| { - const target = func_val.func.owner_decl; - const target_ty = target.typed_value.most_recent.typed_value.ty; - const ret_ty = target_ty.fnReturnType().tag(); - if (target_ty.fnReturnType().hasCodeGenBits() and inst.base.isUnused()) { - try writer.print("(void)", .{}); - } - const tname = mem.spanZ(target.name); - if (file.called.get(tname) == null) { - try file.called.put(tname, void{}); - try renderFunctionSignature(ctx, &file.header, header, target); - try header.writeAll(";\n"); - } - try writer.print("{}(", .{tname}); - if (inst.args.len != 0) { - for (inst.args) |arg, i| { - if (i > 0) { - try writer.writeAll(", "); - } - if (arg.cast(Inst.Constant)) |con| { - try renderValue(ctx, writer, arg.ty, con.val); - } else { - const val = try ctx.resolveInst(arg); - try writer.print("{}", .{val}); - } + const fn_decl = if (func_inst.val.cast(Value.Payload.ExternFn)) |extern_fn| + extern_fn.decl + else if (func_inst.val.cast(Value.Payload.Function)) |func_val| + func_val.func.owner_decl + else + unreachable; + + const fn_ty = fn_decl.typed_value.most_recent.typed_value.ty; + const ret_ty = fn_ty.fnReturnType().tag(); + if (fn_ty.fnReturnType().hasCodeGenBits() and inst.base.isUnused()) { + try writer.print("(void)", .{}); + } + const fn_name = mem.spanZ(fn_decl.name); + if (file.called.get(fn_name) == null) { + try file.called.put(fn_name, void{}); + try renderFunctionSignature(ctx, &file.header, header, fn_decl); + try header.writeAll(";\n"); + } + try writer.print("{s}(", .{fn_name}); + if (inst.args.len != 0) { + for (inst.args) |arg, i| { + if (i > 0) { + try writer.writeAll(", "); + } + if (arg.cast(Inst.Constant)) |con| { + try renderValue(ctx, writer, arg.ty, con.val); + } else { + const val = try ctx.resolveInst(arg); + try writer.print("{}", .{val}); } } - try writer.writeAll(");\n"); - } else { - return ctx.fail(ctx.decl.src(), "TODO non-function call target?", .{}); } + try writer.writeAll(");\n"); } else { - return ctx.fail(ctx.decl.src(), "TODO non-constant call inst?", .{}); + return ctx.fail(ctx.decl.src(), "TODO: C backend: implement function pointers", .{}); } return null; } @@ -315,13 +429,12 @@ fn genBreak(ctx: *Context, inst: *Inst.NoOp) !?[]u8 { } fn genUnreach(file: *C, inst: *Inst.NoOp) !?[]u8 { - try file.main.writer().writeAll(indentation ++ "zig_unreachable();\n"); + try file.main.writer().writeAll("zig_unreachable();\n"); return null; } fn genAsm(ctx: *Context, file: *C, as: *Inst.Assembly) !?[]u8 { const writer = file.main.writer(); - try writer.writeAll(indentation); for (as.inputs) |i, index| { if (i[0] == '{' and i[i.len - 1] == '}') { const reg = i[1 .. i.len - 1]; -- cgit v1.2.3 From 52056b156b8e8ed848c909e527f0c89435e24deb Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 28 Dec 2020 17:46:50 -0700 Subject: stage2: C backend: pointer cast decl refs if necessary --- src/codegen/c.zig | 51 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 19 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 8d706f8735..a568e4df45 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -11,6 +11,7 @@ const Type = @import("../type.zig").Type; const C = link.File.C; const Decl = Module.Decl; const mem = std.mem; +const log = std.log.scoped(.c); const Writer = std.ArrayList(u8).Writer; @@ -22,7 +23,6 @@ fn map(allocator: *std.mem.Allocator, name: []const u8) ![]const u8 { fn renderType( ctx: *Context, - header: *C.Header, writer: Writer, t: Type, ) error{ OutOfMemory, AnalysisFail }!void { @@ -80,12 +80,12 @@ fn renderType( if (t.isVolatilePtr()) { try writer.writeAll("volatile "); } - try renderType(ctx, header, writer, t.elemType()); + try renderType(ctx, writer, t.elemType()); try writer.writeAll(" *"); } }, .Array => { - try renderType(ctx, header, writer, t.elemType()); + try renderType(ctx, writer, t.elemType()); const sentinel_bit = @boolToInt(t.sentinel() != null); const c_len = t.arrayLen() + sentinel_bit; try writer.print("[{d}]", .{c_len}); @@ -113,7 +113,16 @@ fn renderValue( .one => try writer.writeAll("1"), .decl_ref => { const decl_ref_payload = val.cast(Value.Payload.DeclRef).?; - try writer.print("&{s}", .{decl_ref_payload.decl.name}); + + // Determine if we must pointer cast. + const decl_tv = decl_ref_payload.decl.typed_value.most_recent.typed_value; + if (t.eql(decl_tv.ty)) { + try writer.print("&{s}", .{decl_ref_payload.decl.name}); + } else { + try writer.writeAll("("); + try renderType(ctx, writer, t); + try writer.print(")&{s}", .{decl_ref_payload.decl.name}); + } }, .function => { const payload = val.cast(Value.Payload.Function).?; @@ -155,12 +164,11 @@ fn renderValue( fn renderFunctionSignature( ctx: *Context, - header: *C.Header, writer: Writer, decl: *Decl, ) !void { const tv = decl.typed_value.most_recent.typed_value; - try renderType(ctx, header, writer, tv.ty.fnReturnType()); + try renderType(ctx, writer, tv.ty.fnReturnType()); // Use the child allocator directly, as we know the name can be freed before // the rest of the arena. const name = try map(ctx.arena.child_allocator, mem.spanZ(decl.name)); @@ -175,7 +183,7 @@ fn renderFunctionSignature( if (index > 0) { try writer.writeAll(", "); } - try renderType(ctx, header, writer, tv.ty.fnParamType(index)); + try renderType(ctx, writer, tv.ty.fnParamType(index)); try writer.print(" arg{}", .{index}); } } @@ -194,6 +202,7 @@ pub fn generate(file: *C, decl: *Decl) !void { .arena = &arena, .inst_map = &inst_map, .target = file.base.options.target, + .header = &file.header, }; defer { file.error_msg = ctx.error_msg; @@ -202,7 +211,7 @@ pub fn generate(file: *C, decl: *Decl) !void { if (tv.val.cast(Value.Payload.Function)) |func_payload| { const writer = file.main.writer(); - try renderFunctionSignature(&ctx, &file.header, writer, decl); + try renderFunctionSignature(&ctx, writer, decl); try writer.writeAll(" {"); @@ -211,9 +220,11 @@ pub fn generate(file: *C, decl: *Decl) !void { if (instructions.len > 0) { try writer.writeAll("\n"); for (instructions) |inst| { - const indent_size = 4; - const indent_level = 1; - try writer.writeByteNTimes(' ', indent_size * indent_level); + if (inst.tag != .dbg_stmt) { + const indent_size = 4; + const indent_level = 1; + try writer.writeByteNTimes(' ', indent_size * indent_level); + } if (switch (inst.tag) { .assembly => try genAsm(&ctx, file, inst.castTag(.assembly).?), .call => try genCall(&ctx, file, inst.castTag(.call).?), @@ -226,7 +237,7 @@ pub fn generate(file: *C, decl: *Decl) !void { .breakpoint => try genBreak(&ctx, inst.castTag(.breakpoint).?), .unreach => try genUnreach(file, inst.castTag(.unreach).?), .intcast => try genIntCast(&ctx, file, inst.castTag(.intcast).?), - else => |e| return ctx.fail(decl.src(), "TODO implement C codegen for {}", .{e}), + else => |e| return ctx.fail(decl.src(), "TODO: C backend: implement codegen for {}", .{e}), }) |name| { try ctx.inst_map.putNoClobber(inst, name); } @@ -243,7 +254,7 @@ pub fn generate(file: *C, decl: *Decl) !void { // TODO ask the Decl if it is const // https://github.com/ziglang/zig/issues/7582 - try renderType(&ctx, &file.header, writer, tv.ty); + try renderType(&ctx, writer, tv.ty); try writer.print(" {s} = ", .{decl.name}); try renderValue(&ctx, writer, tv.ty, tv.val); try writer.writeAll(";\n"); @@ -269,9 +280,10 @@ pub fn generateHeader( .arena = &arena, .inst_map = &inst_map, .target = comp.getTarget(), + .header = header, }; const writer = header.buf.writer(); - renderFunctionSignature(&ctx, header, writer, decl) catch |err| { + renderFunctionSignature(&ctx, writer, decl) catch |err| { if (err == error.AnalysisFail) { try module.failed_decls.put(module.gpa, decl, ctx.error_msg); } @@ -291,6 +303,7 @@ const Context = struct { unnamed_index: usize = 0, error_msg: *Compilation.ErrorMsg = undefined, target: std.Target, + header: *C.Header, fn resolveInst(self: *Context, inst: *Inst) ![]u8 { if (inst.cast(Inst.Constant)) |const_inst| { @@ -355,9 +368,9 @@ fn genIntCast(ctx: *Context, file: *C, inst: *Inst.UnOp) !?[]u8 { const name = try ctx.name(); const from = try ctx.resolveInst(inst.operand); try writer.writeAll("const "); - try renderType(ctx, &file.header, writer, inst.base.ty); + try renderType(ctx, writer, inst.base.ty); try writer.print(" {} = (", .{name}); - try renderType(ctx, &file.header, writer, inst.base.ty); + try renderType(ctx, writer, inst.base.ty); try writer.print("){};\n", .{from}); return name; } @@ -370,7 +383,7 @@ fn genBinOp(ctx: *Context, file: *C, inst: *Inst.BinOp, comptime operator: []con const writer = file.main.writer(); const name = try ctx.name(); try writer.writeAll("const "); - try renderType(ctx, &file.header, writer, inst.base.ty); + try renderType(ctx, writer, inst.base.ty); try writer.print(" {} = {} " ++ operator ++ " {};\n", .{ name, lhs, rhs }); return name; } @@ -394,7 +407,7 @@ fn genCall(ctx: *Context, file: *C, inst: *Inst.Call) !?[]u8 { const fn_name = mem.spanZ(fn_decl.name); if (file.called.get(fn_name) == null) { try file.called.put(fn_name, void{}); - try renderFunctionSignature(ctx, &file.header, header, fn_decl); + try renderFunctionSignature(ctx, header, fn_decl); try header.writeAll(";\n"); } try writer.print("{s}(", .{fn_name}); @@ -440,7 +453,7 @@ fn genAsm(ctx: *Context, file: *C, as: *Inst.Assembly) !?[]u8 { const reg = i[1 .. i.len - 1]; const arg = as.args[index]; try writer.writeAll("register "); - try renderType(ctx, &file.header, writer, arg.ty); + try renderType(ctx, writer, arg.ty); try writer.print(" {}_constant __asm__(\"{}\") = ", .{ reg, reg }); // TODO merge constant handling into inst_map as well if (arg.castTag(.constant)) |c| { -- cgit v1.2.3 From 37f04d66be014291303b7d8ba49ff4232dbdb696 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 28 Dec 2020 18:24:55 -0700 Subject: stage2: C backend: properly render type of array decls --- src/codegen/c.zig | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/c.zig b/src/codegen/c.zig index a568e4df45..3311a9a25e 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -86,9 +86,7 @@ fn renderType( }, .Array => { try renderType(ctx, writer, t.elemType()); - const sentinel_bit = @boolToInt(t.sentinel() != null); - const c_len = t.arrayLen() + sentinel_bit; - try writer.print("[{d}]", .{c_len}); + try writer.writeAll(" *"); }, else => |e| return ctx.fail(ctx.decl.src(), "TODO: C backend: implement type {s}", .{ @tagName(e), @@ -254,8 +252,21 @@ pub fn generate(file: *C, decl: *Decl) !void { // TODO ask the Decl if it is const // https://github.com/ziglang/zig/issues/7582 - try renderType(&ctx, writer, tv.ty); - try writer.print(" {s} = ", .{decl.name}); + var suffix = std.ArrayList(u8).init(file.base.allocator); + defer suffix.deinit(); + + var render_ty = tv.ty; + while (render_ty.zigTypeTag() == .Array) { + const sentinel_bit = @boolToInt(render_ty.sentinel() != null); + const c_len = render_ty.arrayLen() + sentinel_bit; + try suffix.writer().print("[{d}]", .{c_len}); + render_ty = render_ty.elemType(); + } + + try renderType(&ctx, writer, render_ty); + try writer.print(" {s}{s}", .{ decl.name, suffix.items }); + + try writer.writeAll(" = "); try renderValue(&ctx, writer, tv.ty, tv.val); try writer.writeAll(";\n"); } -- cgit v1.2.3 From a54ccd85374407a5015c5d8e0173089e75da9be4 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 28 Dec 2020 18:43:01 -0700 Subject: stage2: C backend: implement `@breakpoint` and clean up test harness --- src/codegen/c.zig | 6 +++--- src/link/cbe.h | 19 +++++++++++++++++-- src/test.zig | 36 ++++-------------------------------- test/stage2/cbe.zig | 51 ++++++++++++++++++++++++--------------------------- 4 files changed, 48 insertions(+), 64 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 3311a9a25e..d949591a49 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -232,7 +232,7 @@ pub fn generate(file: *C, decl: *Decl) !void { .retvoid => try genRetVoid(file), .arg => try genArg(&ctx), .dbg_stmt => try genDbgStmt(&ctx, inst.castTag(.dbg_stmt).?), - .breakpoint => try genBreak(&ctx, inst.castTag(.breakpoint).?), + .breakpoint => try genBreakpoint(file, inst.castTag(.breakpoint).?), .unreach => try genUnreach(file, inst.castTag(.unreach).?), .intcast => try genIntCast(&ctx, file, inst.castTag(.intcast).?), else => |e| return ctx.fail(decl.src(), "TODO: C backend: implement codegen for {}", .{e}), @@ -447,8 +447,8 @@ fn genDbgStmt(ctx: *Context, inst: *Inst.NoOp) !?[]u8 { return null; } -fn genBreak(ctx: *Context, inst: *Inst.NoOp) !?[]u8 { - // TODO ?? +fn genBreakpoint(file: *C, inst: *Inst.NoOp) !?[]u8 { + try file.main.writer().writeAll("zig_breakpoint();\n"); return null; } diff --git a/src/link/cbe.h b/src/link/cbe.h index cd37ba2f2e..e62e6766ef 100644 --- a/src/link/cbe.h +++ b/src/link/cbe.h @@ -1,5 +1,4 @@ #if __STDC_VERSION__ >= 199901L -// C99 or newer #include #else #define bool unsigned char @@ -17,12 +16,28 @@ #define zig_noreturn #endif -#if __GNUC__ +#if defined(__GNUC__) #define zig_unreachable() __builtin_unreachable() #else #define zig_unreachable() #endif +#if defined(_MSC_VER) +#define zig_breakpoint __debugbreak() +#else +#if defined(__MINGW32__) || defined(__MINGW64__) +#define zig_breakpoint __debugbreak() +#elif defined(__clang__) +#define zig_breakpoint __builtin_debugtrap() +#elif defined(__GNUC__) +#define zig_breakpoint __builtin_trap() +#elif defined(__i386__) || defined(__x86_64__) +#define zig_breakpoint __asm__ volatile("int $0x03"); +#else +#define zig_breakpoint raise(SIGTRAP) +#endif +#endif + #include #define int128_t __int128 #define uint128_t unsigned __int128 diff --git a/src/test.zig b/src/test.zig index 6deee347af..f4374ea0bd 100644 --- a/src/test.zig +++ b/src/test.zig @@ -646,35 +646,16 @@ pub const TestContext = struct { defer file.close(); var out = file.reader().readAllAlloc(arena, 1024 * 1024) catch @panic("Unable to read headeroutput!"); - if (expected_output.len != out.len) { - std.debug.print("\nTransformed header length differs:\n================\nExpected:\n================\n{}\n================\nFound:\n================\n{}\n================\nTest failed.\n", .{ expected_output, out }); - std.process.exit(1); - } - for (expected_output) |e, i| { - if (out[i] != e) { - std.debug.print("\nTransformed header differs:\n================\nExpected:\n================\n{}\n================\nFound:\n================\n{}\n================\nTest failed.\n", .{ expected_output, out }); - std.process.exit(1); - } - } + std.testing.expectEqualStrings(expected_output, out); }, .Transformation => |expected_output| { if (case.cbe) { // The C file is always closed after an update, because we don't support - // incremental updates + // incremental updates. var file = try tmp.dir.openFile(bin_name, .{ .read = true }); defer file.close(); var out = file.reader().readAllAlloc(arena, 1024 * 1024) catch @panic("Unable to read C output!"); - - if (expected_output.len != out.len) { - std.debug.print("\nTransformed C length differs:\n================\nExpected:\n================\n{}\n================\nFound:\n================\n{}\n================\nTest failed.\n", .{ expected_output, out }); - std.process.exit(1); - } - for (expected_output) |e, i| { - if (out[i] != e) { - std.debug.print("\nTransformed C differs:\n================\nExpected:\n================\n{}\n================\nFound:\n================\n{}\n================\nTest failed.\n", .{ expected_output, out }); - std.process.exit(1); - } - } + std.testing.expectEqualStrings(expected_output, out); } else { update_node.setEstimatedTotalItems(5); var emit_node = update_node.start("emit", 0); @@ -694,16 +675,7 @@ pub const TestContext = struct { test_node.activate(); defer test_node.end(); - if (expected_output.len != out_zir.items.len) { - std.debug.print("{}\nTransformed ZIR length differs:\n================\nExpected:\n================\n{}\n================\nFound:\n================\n{}\n================\nTest failed.\n", .{ case.name, expected_output, out_zir.items }); - std.process.exit(1); - } - for (expected_output) |e, i| { - if (out_zir.items[i] != e) { - std.debug.print("{}\nTransformed ZIR differs:\n================\nExpected:\n================\n{}\n================\nFound:\n================\n{}\n================\nTest failed.\n", .{ case.name, expected_output, out_zir.items }); - std.process.exit(1); - } - } + std.testing.expectEqualStrings(expected_output, out_zir.items); } }, .Error => |e| { diff --git a/test/stage2/cbe.zig b/test/stage2/cbe.zig index 96db7b835e..3a9bff897a 100644 --- a/test/stage2/cbe.zig +++ b/test/stage2/cbe.zig @@ -15,6 +15,7 @@ pub fn addCases(ctx: *TestContext) !void { \\} , \\zig_noreturn void _start(void) { + \\ zig_breakpoint(); \\ zig_unreachable(); \\} \\ @@ -41,6 +42,7 @@ pub fn addCases(ctx: *TestContext) !void { \\} \\ \\zig_noreturn void main(void) { + \\ zig_breakpoint(); \\ zig_unreachable(); \\} \\ @@ -61,8 +63,6 @@ pub fn addCases(ctx: *TestContext) !void { \\ exitGood(); \\} , - \\#include - \\ \\zig_noreturn void exitGood(void); \\ \\const char *const exitGood__anon_0 = "{rax}"; @@ -74,9 +74,10 @@ pub fn addCases(ctx: *TestContext) !void { \\} \\ \\zig_noreturn void exitGood(void) { - \\ register size_t rax_constant __asm__("rax") = 231; - \\ register size_t rdi_constant __asm__("rdi") = 0; + \\ register uintptr_t rax_constant __asm__("rax") = 231; + \\ register uintptr_t rdi_constant __asm__("rdi") = 0; \\ __asm volatile ("syscall" :: ""(rax_constant), ""(rdi_constant)); + \\ zig_breakpoint(); \\ zig_unreachable(); \\} \\ @@ -96,9 +97,7 @@ pub fn addCases(ctx: *TestContext) !void { \\} \\ , - \\#include - \\ - \\zig_noreturn void exit(size_t arg0); + \\zig_noreturn void exit(uintptr_t arg0); \\ \\const char *const exit__anon_0 = "{rax}"; \\const char *const exit__anon_1 = "{rdi}"; @@ -108,10 +107,11 @@ pub fn addCases(ctx: *TestContext) !void { \\ exit(0); \\} \\ - \\zig_noreturn void exit(size_t arg0) { - \\ register size_t rax_constant __asm__("rax") = 231; - \\ register size_t rdi_constant __asm__("rdi") = arg0; + \\zig_noreturn void exit(uintptr_t arg0) { + \\ register uintptr_t rax_constant __asm__("rax") = 231; + \\ register uintptr_t rdi_constant __asm__("rdi") = arg0; \\ __asm volatile ("syscall" :: ""(rax_constant), ""(rdi_constant)); + \\ zig_breakpoint(); \\ zig_unreachable(); \\} \\ @@ -131,7 +131,6 @@ pub fn addCases(ctx: *TestContext) !void { \\} \\ , - \\#include \\#include \\ \\zig_noreturn void exit(uint8_t arg0); @@ -145,10 +144,11 @@ pub fn addCases(ctx: *TestContext) !void { \\} \\ \\zig_noreturn void exit(uint8_t arg0) { - \\ const size_t __temp_0 = (size_t)arg0; - \\ register size_t rax_constant __asm__("rax") = 231; - \\ register size_t rdi_constant __asm__("rdi") = __temp_0; + \\ const uintptr_t __temp_0 = (uintptr_t)arg0; + \\ register uintptr_t rax_constant __asm__("rax") = 231; + \\ register uintptr_t rdi_constant __asm__("rdi") = __temp_0; \\ __asm volatile ("syscall" :: ""(rax_constant), ""(rdi_constant)); + \\ zig_breakpoint(); \\ zig_unreachable(); \\} \\ @@ -172,7 +172,6 @@ pub fn addCases(ctx: *TestContext) !void { \\} \\ , - \\#include \\#include \\ \\zig_noreturn void exitMath(uint8_t arg0); @@ -193,10 +192,11 @@ pub fn addCases(ctx: *TestContext) !void { \\} \\ \\zig_noreturn void exit(uint8_t arg0) { - \\ const size_t __temp_0 = (size_t)arg0; - \\ register size_t rax_constant __asm__("rax") = 231; - \\ register size_t rdi_constant __asm__("rdi") = __temp_0; + \\ const uintptr_t __temp_0 = (uintptr_t)arg0; + \\ register uintptr_t rax_constant __asm__("rax") = 231; + \\ register uintptr_t rdi_constant __asm__("rdi") = __temp_0; \\ __asm volatile ("syscall" :: ""(rax_constant), ""(rdi_constant)); + \\ zig_breakpoint(); \\ zig_unreachable(); \\} \\ @@ -220,7 +220,6 @@ pub fn addCases(ctx: *TestContext) !void { \\} \\ , - \\#include \\#include \\ \\zig_noreturn void exitMath(uint8_t arg0); @@ -241,10 +240,11 @@ pub fn addCases(ctx: *TestContext) !void { \\} \\ \\zig_noreturn void exit(uint8_t arg0) { - \\ const size_t __temp_0 = (size_t)arg0; - \\ register size_t rax_constant __asm__("rax") = 231; - \\ register size_t rdi_constant __asm__("rdi") = __temp_0; + \\ const uintptr_t __temp_0 = (uintptr_t)arg0; + \\ register uintptr_t rax_constant __asm__("rax") = 231; + \\ register uintptr_t rdi_constant __asm__("rdi") = __temp_0; \\ __asm volatile ("syscall" :: ""(rax_constant), ""(rdi_constant)); + \\ zig_breakpoint(); \\ zig_unreachable(); \\} \\ @@ -276,9 +276,7 @@ pub fn addCases(ctx: *TestContext) !void { ctx.h("header with usize param function", linux_x64, \\export fn start(a: usize) void{} , - \\#include - \\ - \\void start(size_t arg0); + \\void start(uintptr_t arg0); \\ ); ctx.h("header with bool param function", linux_x64, @@ -308,10 +306,9 @@ pub fn addCases(ctx: *TestContext) !void { ctx.h("header with multiple includes", linux_x64, \\export fn start(a: u32, b: usize) void{} , - \\#include \\#include \\ - \\void start(uint32_t arg0, size_t arg1); + \\void start(uint32_t arg0, uintptr_t arg1); \\ ); } -- cgit v1.2.3 From bbe66572e1c1e2abff0a433c36291f3429482e8f Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 28 Dec 2020 20:15:00 -0700 Subject: stage2: C backend: handle string literals more gracefully --- src/codegen/c.zig | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/c.zig b/src/codegen/c.zig index d949591a49..6f00992327 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -137,22 +137,32 @@ fn renderValue( ), }, .Array => { - // TODO first try specific tag representations for more efficiency - // Fall back to inefficient generic implementation. - try writer.writeAll("{"); - var index: usize = 0; - const len = t.arrayLen(); - const elem_ty = t.elemType(); - while (index < len) : (index += 1) { - if (index != 0) try writer.writeAll(","); - const elem_val = try val.elemValue(&ctx.arena.allocator, index); - try renderValue(ctx, writer, elem_ty, elem_val); - } - if (t.sentinel()) |sentinel_val| { - if (index != 0) try writer.writeAll(","); - try renderValue(ctx, writer, elem_ty, sentinel_val); + // First try specific tag representations for more efficiency. + switch (val.tag()) { + .undef, .empty_struct_value, .empty_array => try writer.writeAll("{}"), + .bytes => { + const bytes = val.cast(Value.Payload.Bytes).?.data; + // TODO: make our own C string escape instead of using {Z} + try writer.print("\"{Z}\"", .{bytes}); + }, + else => { + // Fall back to generic implementation. + try writer.writeAll("{"); + var index: usize = 0; + const len = t.arrayLen(); + const elem_ty = t.elemType(); + while (index < len) : (index += 1) { + if (index != 0) try writer.writeAll(","); + const elem_val = try val.elemValue(&ctx.arena.allocator, index); + try renderValue(ctx, writer, elem_ty, elem_val); + } + if (t.sentinel()) |sentinel_val| { + if (index != 0) try writer.writeAll(","); + try renderValue(ctx, writer, elem_ty, sentinel_val); + } + try writer.writeAll("}"); + }, } - try writer.writeAll("}"); }, else => |e| return ctx.fail(ctx.decl.src(), "TODO: C backend: implement value {s}", .{ @tagName(e), -- cgit v1.2.3 From 813d3308ccd13bdc96a40b583ffd8722651b7b83 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 28 Dec 2020 20:27:58 -0700 Subject: stage2: update C backend test cases for new output --- src/codegen/c.zig | 20 +++++++++++++++----- test/stage2/cbe.zig | 45 ++++++++++++++++----------------------------- 2 files changed, 31 insertions(+), 34 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 6f00992327..364aa4d7ef 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -198,6 +198,13 @@ fn renderFunctionSignature( try writer.writeByte(')'); } +fn indent(file: *C) !void { + const indent_size = 4; + const indent_level = 1; + const indent_amt = indent_size * indent_level; + try file.main.writer().writeByteNTimes(' ', indent_amt); +} + pub fn generate(file: *C, decl: *Decl) !void { const tv = decl.typed_value.most_recent.typed_value; @@ -228,11 +235,6 @@ pub fn generate(file: *C, decl: *Decl) !void { if (instructions.len > 0) { try writer.writeAll("\n"); for (instructions) |inst| { - if (inst.tag != .dbg_stmt) { - const indent_size = 4; - const indent_level = 1; - try writer.writeByteNTimes(' ', indent_size * indent_level); - } if (switch (inst.tag) { .assembly => try genAsm(&ctx, file, inst.castTag(.assembly).?), .call => try genCall(&ctx, file, inst.castTag(.call).?), @@ -361,11 +363,13 @@ fn genArg(ctx: *Context) !?[]u8 { } fn genRetVoid(file: *C) !?[]u8 { + try indent(file); try file.main.writer().print("return;\n", .{}); return null; } fn genRet(ctx: *Context, file: *C, inst: *Inst.UnOp) !?[]u8 { + try indent(file); const writer = file.main.writer(); try writer.writeAll("return "); try genValue(ctx, writer, inst.operand); @@ -384,6 +388,7 @@ fn genValue(ctx: *Context, writer: Writer, inst: *Inst) !void { fn genIntCast(ctx: *Context, file: *C, inst: *Inst.UnOp) !?[]u8 { if (inst.base.isUnused()) return null; + try indent(file); const op = inst.operand; const writer = file.main.writer(); const name = try ctx.name(); @@ -399,6 +404,7 @@ fn genIntCast(ctx: *Context, file: *C, inst: *Inst.UnOp) !?[]u8 { fn genBinOp(ctx: *Context, file: *C, inst: *Inst.BinOp, comptime operator: []const u8) !?[]u8 { if (inst.base.isUnused()) return null; + try indent(file); const lhs = ctx.resolveInst(inst.lhs); const rhs = ctx.resolveInst(inst.rhs); const writer = file.main.writer(); @@ -410,6 +416,7 @@ fn genBinOp(ctx: *Context, file: *C, inst: *Inst.BinOp, comptime operator: []con } fn genCall(ctx: *Context, file: *C, inst: *Inst.Call) !?[]u8 { + try indent(file); const writer = file.main.writer(); const header = file.header.buf.writer(); if (inst.func.castTag(.constant)) |func_inst| { @@ -458,16 +465,19 @@ fn genDbgStmt(ctx: *Context, inst: *Inst.NoOp) !?[]u8 { } fn genBreakpoint(file: *C, inst: *Inst.NoOp) !?[]u8 { + try indent(file); try file.main.writer().writeAll("zig_breakpoint();\n"); return null; } fn genUnreach(file: *C, inst: *Inst.NoOp) !?[]u8 { + try indent(file); try file.main.writer().writeAll("zig_unreachable();\n"); return null; } fn genAsm(ctx: *Context, file: *C, as: *Inst.Assembly) !?[]u8 { + try indent(file); const writer = file.main.writer(); for (as.inputs) |i, index| { if (i[0] == '{' and i[i.len - 1] == '}') { diff --git a/test/stage2/cbe.zig b/test/stage2/cbe.zig index 2212d48298..cd26b8aa58 100644 --- a/test/stage2/cbe.zig +++ b/test/stage2/cbe.zig @@ -23,6 +23,7 @@ pub fn addCases(ctx: *TestContext) !void { // Now change the message only // TODO fix C backend not supporting updates + // https://github.com/ziglang/zig/issues/7589 //case.addCompareOutput( // \\extern fn puts(s: [*:0]const u8) c_int; // \\export fn main() c_int { @@ -88,9 +89,9 @@ pub fn addCases(ctx: *TestContext) !void { , \\zig_noreturn void exitGood(void); \\ - \\const char *const exitGood__anon_0 = "{rax}"; - \\const char *const exitGood__anon_1 = "{rdi}"; - \\const char *const exitGood__anon_2 = "syscall"; + \\static uint8_t exitGood__anon_0[6] = "{rax}"; + \\static uint8_t exitGood__anon_1[6] = "{rdi}"; + \\static uint8_t exitGood__anon_2[8] = "syscall"; \\ \\zig_noreturn void _start(void) { \\ exitGood(); @@ -122,9 +123,9 @@ pub fn addCases(ctx: *TestContext) !void { , \\zig_noreturn void exit(uintptr_t arg0); \\ - \\const char *const exit__anon_0 = "{rax}"; - \\const char *const exit__anon_1 = "{rdi}"; - \\const char *const exit__anon_2 = "syscall"; + \\static uint8_t exit__anon_0[6] = "{rax}"; + \\static uint8_t exit__anon_1[6] = "{rdi}"; + \\static uint8_t exit__anon_2[8] = "syscall"; \\ \\zig_noreturn void _start(void) { \\ exit(0); @@ -154,13 +155,11 @@ pub fn addCases(ctx: *TestContext) !void { \\} \\ , - \\#include - \\ \\zig_noreturn void exit(uint8_t arg0); \\ - \\const char *const exit__anon_0 = "{rax}"; - \\const char *const exit__anon_1 = "{rdi}"; - \\const char *const exit__anon_2 = "syscall"; + \\static uint8_t exit__anon_0[6] = "{rax}"; + \\static uint8_t exit__anon_1[6] = "{rdi}"; + \\static uint8_t exit__anon_2[8] = "syscall"; \\ \\zig_noreturn void _start(void) { \\ exit(0); @@ -195,14 +194,12 @@ pub fn addCases(ctx: *TestContext) !void { \\} \\ , - \\#include - \\ \\zig_noreturn void exitMath(uint8_t arg0); \\zig_noreturn void exit(uint8_t arg0); \\ - \\const char *const exit__anon_0 = "{rax}"; - \\const char *const exit__anon_1 = "{rdi}"; - \\const char *const exit__anon_2 = "syscall"; + \\static uint8_t exit__anon_0[6] = "{rax}"; + \\static uint8_t exit__anon_1[6] = "{rdi}"; + \\static uint8_t exit__anon_2[8] = "syscall"; \\ \\zig_noreturn void _start(void) { \\ exitMath(1); @@ -243,14 +240,12 @@ pub fn addCases(ctx: *TestContext) !void { \\} \\ , - \\#include - \\ \\zig_noreturn void exitMath(uint8_t arg0); \\zig_noreturn void exit(uint8_t arg0); \\ - \\const char *const exit__anon_0 = "{rax}"; - \\const char *const exit__anon_1 = "{rdi}"; - \\const char *const exit__anon_2 = "syscall"; + \\static uint8_t exit__anon_0[6] = "{rax}"; + \\static uint8_t exit__anon_1[6] = "{rdi}"; + \\static uint8_t exit__anon_2[8] = "syscall"; \\ \\zig_noreturn void _start(void) { \\ exitMath(1); @@ -275,24 +270,18 @@ pub fn addCases(ctx: *TestContext) !void { ctx.h("header with single param function", linux_x64, \\export fn start(a: u8) void{} , - \\#include - \\ \\void start(uint8_t arg0); \\ ); ctx.h("header with multiple param function", linux_x64, \\export fn start(a: u8, b: u8, c: u8) void{} , - \\#include - \\ \\void start(uint8_t arg0, uint8_t arg1, uint8_t arg2); \\ ); ctx.h("header with u32 param function", linux_x64, \\export fn start(a: u32) void{} , - \\#include - \\ \\void start(uint32_t arg0); \\ ); @@ -329,8 +318,6 @@ pub fn addCases(ctx: *TestContext) !void { ctx.h("header with multiple includes", linux_x64, \\export fn start(a: u32, b: usize) void{} , - \\#include - \\ \\void start(uint32_t arg0, uintptr_t arg1); \\ ); -- cgit v1.2.3