Merge branch 'hello-c-backend' into master

This branch introduces a new kind of test into the stage2 test harness:
Zig code that compiles into C code with the C backend, and then the
resulting C code gets run and output compared against the expected
result.

This branch also implements extern functions in the frontend so that we
can have a "hello world" C backend test that passes.
This commit is contained in:
Andrew Kelley 2020-12-28 20:32:53 -07:00
commit 3b5dd48f99
12 changed files with 718 additions and 354 deletions

View File

@ -11,7 +11,15 @@ pub const io_mode: io.Mode = builtin.test_io_mode;
var log_err_count: usize = 0;
var args_buffer: [std.fs.MAX_PATH_BYTES + std.mem.page_size]u8 = undefined;
var args_allocator = std.heap.FixedBufferAllocator.init(&args_buffer);
pub fn main() anyerror!void {
const args = std.process.argsAlloc(&args_allocator.allocator) catch {
@panic("Too many bytes passed over the CLI to the test runner");
};
std.testing.zig_exe_path = args[1];
const test_fn_list = builtin.test_functions;
var ok_count: usize = 0;
var skip_count: usize = 0;

View File

@ -21,6 +21,10 @@ pub var base_allocator_instance = std.heap.FixedBufferAllocator.init("");
/// TODO https://github.com/ziglang/zig/issues/5738
pub var log_level = std.log.Level.warn;
/// This is available to any test that wants to execute Zig in a child process.
/// It will be the same executable that is running `zig test`.
pub var zig_exe_path: []const u8 = undefined;
/// This function is intended to be used only in tests. It prints diagnostics to stderr
/// and then aborts when actual_error_union is not expected_error.
pub fn expectError(expected_error: anyerror, actual_error_union: anytype) void {

View File

@ -1431,9 +1431,6 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor
var c_comp_progress_node = main_progress_node.start("Compile C Objects", self.c_source_files.len);
defer c_comp_progress_node.end();
var arena = std.heap.ArenaAllocator.init(self.gpa);
defer arena.deinit();
self.work_queue_wait_group.reset();
defer self.work_queue_wait_group.wait();
@ -1502,7 +1499,7 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor
};
if (self.c_header) |*header| {
c_codegen.generateHeader(&arena, module, &header.*, decl) catch |err| switch (err) {
c_codegen.generateHeader(self, module, header, decl) catch |err| switch (err) {
error.OutOfMemory => return error.OutOfMemory,
error.AnalysisFail => {
decl.analysis = .dependency_failure;

View File

@ -277,6 +277,8 @@ pub const Decl = struct {
};
/// Fn struct memory is owned by the Decl's TypedValue.Managed arena allocator.
/// Extern functions do not have this data structure; they are represented by
/// the `Decl` only, with a `Value` tag of `extern_fn`.
pub const Fn = struct {
/// This memory owned by the Decl's TypedValue.Managed arena allocator.
analysis: union(enum) {
@ -1010,8 +1012,6 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
defer fn_type_scope.instructions.deinit(self.gpa);
decl.is_pub = fn_proto.getVisibToken() != null;
const body_node = fn_proto.getBodyNode() orelse
return self.failTok(&fn_type_scope.base, fn_proto.fn_token, "TODO implement extern functions", .{});
const param_decls = fn_proto.params();
const param_types = try fn_type_scope.arena.alloc(*zir.Inst, param_decls.len);
@ -1083,6 +1083,36 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
const fn_type = try zir_sema.analyzeBodyValueAsType(self, &block_scope, fn_type_inst, .{
.instructions = fn_type_scope.instructions.items,
});
const body_node = fn_proto.getBodyNode() orelse {
// Extern function.
var type_changed = true;
if (decl.typedValueManaged()) |tvm| {
type_changed = !tvm.typed_value.ty.eql(fn_type);
tvm.deinit(self.gpa);
}
const value_payload = try decl_arena.allocator.create(Value.Payload.ExternFn);
value_payload.* = .{ .decl = decl };
decl_arena_state.* = decl_arena.state;
decl.typed_value = .{
.most_recent = .{
.typed_value = .{
.ty = fn_type,
.val = Value.initPayload(&value_payload.base),
},
.arena = decl_arena_state,
},
};
decl.analysis = .complete;
decl.generation = self.generation;
try self.comp.bin_file.allocateDeclIndexes(decl);
try self.comp.work_queue.writeItem(.{ .codegen_decl = decl });
return type_changed;
};
const new_func = try decl_arena.allocator.create(Fn);
const fn_payload = try decl_arena.allocator.create(Value.Payload.Function);
@ -1899,7 +1929,13 @@ pub fn resolveDefinedValue(self: *Module, scope: *Scope, base: *Inst) !?Value {
return null;
}
pub fn analyzeExport(self: *Module, scope: *Scope, src: usize, borrowed_symbol_name: []const u8, exported_decl: *Decl) !void {
pub fn analyzeExport(
self: *Module,
scope: *Scope,
src: usize,
borrowed_symbol_name: []const u8,
exported_decl: *Decl,
) !void {
try self.ensureDeclAnalyzed(exported_decl);
const typed_value = exported_decl.typed_value.most_recent.typed_value;
switch (typed_value.ty.zigTypeTag()) {
@ -2801,16 +2837,47 @@ pub fn coerce(self: *Module, scope: *Scope, dest_type: Type, inst: *Inst) !*Inst
}
}
// *[N]T to []T
if (inst.ty.isSinglePointer() and dest_type.isSlice() and
(!inst.ty.isConstPtr() or dest_type.isConstPtr()))
{
// Coercions where the source is a single pointer to an array.
src_array_ptr: {
if (!inst.ty.isSinglePointer()) break :src_array_ptr;
const array_type = inst.ty.elemType();
if (array_type.zigTypeTag() != .Array) break :src_array_ptr;
const array_elem_type = array_type.elemType();
if (inst.ty.isConstPtr() and !dest_type.isConstPtr()) break :src_array_ptr;
if (inst.ty.isVolatilePtr() and !dest_type.isVolatilePtr()) break :src_array_ptr;
const dst_elem_type = dest_type.elemType();
if (array_type.zigTypeTag() == .Array and
coerceInMemoryAllowed(dst_elem_type, array_type.elemType()) == .ok)
{
return self.coerceArrayPtrToSlice(scope, dest_type, inst);
switch (coerceInMemoryAllowed(dst_elem_type, array_elem_type)) {
.ok => {},
.no_match => break :src_array_ptr,
}
switch (dest_type.ptrSize()) {
.Slice => {
// *[N]T to []T
return self.coerceArrayPtrToSlice(scope, dest_type, inst);
},
.C => {
// *[N]T to [*c]T
return self.coerceArrayPtrToMany(scope, dest_type, inst);
},
.Many => {
// *[N]T to [*]T
// *[N:s]T to [*:s]T
const src_sentinel = array_type.sentinel();
const dst_sentinel = dest_type.sentinel();
if (src_sentinel == null and dst_sentinel == null)
return self.coerceArrayPtrToMany(scope, dest_type, inst);
if (src_sentinel) |src_s| {
if (dst_sentinel) |dst_s| {
if (src_s.eql(dst_s)) {
return self.coerceArrayPtrToMany(scope, dest_type, inst);
}
}
}
},
.One => {},
}
}
@ -2918,6 +2985,14 @@ fn coerceArrayPtrToSlice(self: *Module, scope: *Scope, dest_type: Type, inst: *I
return self.fail(scope, inst.src, "TODO implement coerceArrayPtrToSlice runtime instruction", .{});
}
fn coerceArrayPtrToMany(self: *Module, scope: *Scope, dest_type: Type, inst: *Inst) !*Inst {
if (inst.value()) |val| {
// The comptime Value representation is compatible with both types.
return self.constInst(scope, inst.src, .{ .ty = dest_type, .val = val });
}
return self.fail(scope, inst.src, "TODO implement coerceArrayPtrToMany runtime instruction", .{});
}
pub fn fail(self: *Module, scope: *Scope, src: usize, comptime format: []const u8, args: anytype) InnerError {
@setCold(true);
const err_msg = try Compilation.ErrorMsg.create(self.gpa, src, format, args);

View File

@ -11,8 +11,9 @@ 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 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,45 +21,162 @@ 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,
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");
} else {
return ctx.fail(ctx.decl.src(), "TODO implement int type {}", .{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,
}
},
else => |e| return ctx.fail(ctx.decl.src(), "TODO implement type {}", .{e}),
}
}
fn renderValue(ctx: *Context, writer: std.ArrayList(u8).Writer, T: Type, val: Value) !void {
switch (T.zigTypeTag()) {
.Int => {
if (T.isSignedInt())
return writer.print("{}", .{val.toSignedInt()});
return writer.print("{}", .{val.toUnsignedInt()});
.Pointer => {
if (t.isSlice()) {
return ctx.fail(ctx.decl.src(), "TODO: C backend: implement slices", .{});
} else {
if (t.isConstPtr()) {
try writer.writeAll("const ");
}
if (t.isVolatilePtr()) {
try writer.writeAll("volatile ");
}
try renderType(ctx, writer, t.elemType());
try writer.writeAll(" *");
}
},
else => |e| return ctx.fail(ctx.decl.src(), "TODO implement value {}", .{e}),
.Array => {
try renderType(ctx, writer, t.elemType());
try writer.writeAll(" *");
},
else => |e| return ctx.fail(ctx.decl.src(), "TODO: C backend: implement type {s}", .{
@tagName(e),
}),
}
}
fn renderFunctionSignature(ctx: *Context, header: *C.Header, writer: std.ArrayList(u8).Writer, decl: *Decl) !void {
fn renderValue(
ctx: *Context,
writer: Writer,
t: Type,
val: Value,
) error{ OutOfMemory, AnalysisFail }!void {
switch (t.zigTypeTag()) {
.Int => {
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).?;
// 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).?;
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 => {
// 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("}");
},
}
},
else => |e| return ctx.fail(ctx.decl.src(), "TODO: C backend: implement value {s}", .{
@tagName(e),
}),
}
}
fn renderFunctionSignature(
ctx: *Context,
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));
@ -73,38 +191,122 @@ fn renderFunctionSignature(ctx: *Context, header: *C.Header, writer: std.ArrayLi
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});
}
}
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 {
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,
.header = &file.header,
};
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, 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| {
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 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}),
}) |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
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");
}
}
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(),
.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);
}
@ -116,24 +318,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 +325,8 @@ const Context = struct {
argdex: usize = 0,
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| {
@ -170,55 +356,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,25 +363,40 @@ fn genArg(ctx: *Context) !?[]u8 {
}
fn genRetVoid(file: *C) !?[]u8 {
try file.main.writer().print(indentation ++ "return;\n", .{});
try indent(file);
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 {
try indent(file);
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 {
if (inst.base.isUnused())
return null;
try indent(file);
const op = inst.operand;
const writer = file.main.writer();
const name = try ctx.name();
const from = try ctx.resolveInst(inst.operand);
try writer.writeAll(indentation ++ "const ");
try renderType(ctx, &file.header, writer, inst.base.ty);
try writer.writeAll("const ");
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;
}
@ -252,54 +404,57 @@ 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();
const name = try ctx.name();
try writer.writeAll(indentation ++ "const ");
try renderType(ctx, &file.header, writer, inst.base.ty);
try writer.writeAll("const ");
try renderType(ctx, writer, inst.base.ty);
try writer.print(" {} = {} " ++ operator ++ " {};\n", .{ name, lhs, rhs });
return name;
}
fn genCall(ctx: *Context, file: *C, inst: *Inst.Call) !?[]u8 {
try indent(file);
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, 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;
}
@ -309,25 +464,27 @@ 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 indent(file);
try file.main.writer().writeAll("zig_breakpoint();\n");
return null;
}
fn genUnreach(file: *C, inst: *Inst.NoOp) !?[]u8 {
try file.main.writer().writeAll(indentation ++ "zig_unreachable();\n");
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();
try writer.writeAll(indentation);
for (as.inputs) |i, index| {
if (i[0] == '{' and i[i.len - 1] == '}') {
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| {

View File

@ -15,8 +15,6 @@ pub const base_tag: File.Tag = .c;
pub const Header = struct {
buf: std.ArrayList(u8),
need_stddef: bool = false,
need_stdint: bool = false,
emit_loc: ?Compilation.EmitLoc,
pub fn init(allocator: *Allocator, emit_loc: ?Compilation.EmitLoc) Header {
@ -31,20 +29,8 @@ pub const Header = struct {
defer tracy.end();
try writer.writeAll(@embedFile("cbe.h"));
var includes = false;
if (self.need_stddef) {
try writer.writeAll("#include <stddef.h>\n");
includes = true;
}
if (self.need_stdint) {
try writer.writeAll("#include <stdint.h>\n");
includes = true;
}
if (includes) {
try writer.writeByte('\n');
}
if (self.buf.items.len > 0) {
try writer.print("{}", .{self.buf.items});
try writer.print("{s}", .{self.buf.items});
}
}

View File

@ -1,5 +1,4 @@
#if __STDC_VERSION__ >= 199901L
// C99 or newer
#include <stdbool.h>
#else
#define bool unsigned char
@ -17,9 +16,29 @@
#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 <stdint.h>
#define int128_t __int128
#define uint128_t unsigned __int128

View File

@ -1828,7 +1828,9 @@ fn buildOutputType(
else => unreachable,
}
}
try argv.append(exe_path);
try argv.appendSlice(&[_][]const u8{
exe_path, self_exe_path,
});
} else {
for (test_exec_args.items) |arg| {
try argv.append(arg orelse exe_path);

View File

@ -11,8 +11,9 @@ const enable_wine: bool = build_options.enable_wine;
const enable_wasmtime: bool = build_options.enable_wasmtime;
const glibc_multi_install_dir: ?[]const u8 = build_options.glibc_multi_install_dir;
const ThreadPool = @import("ThreadPool.zig");
const CrossTarget = std.zig.CrossTarget;
const cheader = @embedFile("link/cbe.h");
const c_header = @embedFile("link/cbe.h");
test "self-hosted" {
var ctx = TestContext.init();
@ -88,6 +89,9 @@ pub const TestContext = struct {
/// A transformation update transforms the input and tests against
/// the expected output ZIR.
Transformation: [:0]const u8,
/// Check the main binary output file against an expected set of bytes.
/// This is most useful with, for example, `-ofmt=c`.
CompareObjectFile: []const u8,
/// An error update attempts to compile bad code, and ensures that it
/// fails to compile, and for the expected reasons.
/// A slice containing the expected errors *in sequential order*.
@ -109,12 +113,12 @@ pub const TestContext = struct {
path: []const u8,
};
pub const TestType = enum {
pub const Extension = enum {
Zig,
ZIR,
};
/// A Case consists of a set of *updates*. The same Compilation is used for each
/// A `Case` consists of a list of `Update`. The same `Compilation` is used for each
/// update, so each update's source is treated as a single file being
/// updated by the test harness and incrementally compiled.
pub const Case = struct {
@ -123,13 +127,14 @@ pub const TestContext = struct {
name: []const u8,
/// The platform the test targets. For non-native platforms, an emulator
/// such as QEMU is required for tests to complete.
target: std.zig.CrossTarget,
target: CrossTarget,
/// In order to be able to run e.g. Execution updates, this must be set
/// to Executable.
output_mode: std.builtin.OutputMode,
updates: std.ArrayList(Update),
extension: TestType,
cbe: bool = false,
extension: Extension,
object_format: ?std.builtin.ObjectFormat = null,
emit_h: bool = false,
files: std.ArrayList(File),
@ -145,6 +150,7 @@ pub const TestContext = struct {
/// Adds a subcase in which the module is updated with `src`, and a C
/// header is generated.
pub fn addHeader(self: *Case, src: [:0]const u8, result: [:0]const u8) void {
self.emit_h = true;
self.updates.append(.{
.src = src,
.case = .{ .Header = result },
@ -160,6 +166,15 @@ pub const TestContext = struct {
}) catch unreachable;
}
/// Adds a subcase in which the module is updated with `src`, compiled,
/// and the object file data is compared against `result`.
pub fn addCompareObjectFile(self: *Case, src: [:0]const u8, result: []const u8) void {
self.updates.append(.{
.src = src,
.case = .{ .CompareObjectFile = result },
}) catch unreachable;
}
/// Adds a subcase in which the module is updated with `src`, which
/// should contain invalid input, and ensures that compilation fails
/// for the expected reasons, given in sequential order in `errors` in
@ -214,86 +229,100 @@ pub const TestContext = struct {
pub fn addExe(
ctx: *TestContext,
name: []const u8,
target: std.zig.CrossTarget,
T: TestType,
target: CrossTarget,
extension: Extension,
) *Case {
ctx.cases.append(Case{
.name = name,
.target = target,
.updates = std.ArrayList(Update).init(ctx.cases.allocator),
.output_mode = .Exe,
.extension = T,
.extension = extension,
.files = std.ArrayList(File).init(ctx.cases.allocator),
}) catch unreachable;
return &ctx.cases.items[ctx.cases.items.len - 1];
}
/// Adds a test case for Zig input, producing an executable
pub fn exe(ctx: *TestContext, name: []const u8, target: std.zig.CrossTarget) *Case {
pub fn exe(ctx: *TestContext, name: []const u8, target: CrossTarget) *Case {
return ctx.addExe(name, target, .Zig);
}
/// Adds a test case for ZIR input, producing an executable
pub fn exeZIR(ctx: *TestContext, name: []const u8, target: std.zig.CrossTarget) *Case {
pub fn exeZIR(ctx: *TestContext, name: []const u8, target: CrossTarget) *Case {
return ctx.addExe(name, target, .ZIR);
}
pub fn exeFromCompiledC(ctx: *TestContext, name: []const u8, target: CrossTarget) *Case {
ctx.cases.append(Case{
.name = name,
.target = target,
.updates = std.ArrayList(Update).init(ctx.cases.allocator),
.output_mode = .Exe,
.extension = .Zig,
.object_format = .c,
.files = std.ArrayList(File).init(ctx.cases.allocator),
}) catch unreachable;
return &ctx.cases.items[ctx.cases.items.len - 1];
}
pub fn addObj(
ctx: *TestContext,
name: []const u8,
target: std.zig.CrossTarget,
T: TestType,
target: CrossTarget,
extension: Extension,
) *Case {
ctx.cases.append(Case{
.name = name,
.target = target,
.updates = std.ArrayList(Update).init(ctx.cases.allocator),
.output_mode = .Obj,
.extension = T,
.extension = extension,
.files = std.ArrayList(File).init(ctx.cases.allocator),
}) catch unreachable;
return &ctx.cases.items[ctx.cases.items.len - 1];
}
/// Adds a test case for Zig input, producing an object file
pub fn obj(ctx: *TestContext, name: []const u8, target: std.zig.CrossTarget) *Case {
/// Adds a test case for Zig input, producing an object file.
pub fn obj(ctx: *TestContext, name: []const u8, target: CrossTarget) *Case {
return ctx.addObj(name, target, .Zig);
}
/// Adds a test case for ZIR input, producing an object file
pub fn objZIR(ctx: *TestContext, name: []const u8, target: std.zig.CrossTarget) *Case {
/// Adds a test case for ZIR input, producing an object file.
pub fn objZIR(ctx: *TestContext, name: []const u8, target: CrossTarget) *Case {
return ctx.addObj(name, target, .ZIR);
}
pub fn addC(ctx: *TestContext, name: []const u8, target: std.zig.CrossTarget, T: TestType) *Case {
/// Adds a test case for Zig or ZIR input, producing C code.
pub fn addC(ctx: *TestContext, name: []const u8, target: CrossTarget, ext: Extension) *Case {
ctx.cases.append(Case{
.name = name,
.target = target,
.updates = std.ArrayList(Update).init(ctx.cases.allocator),
.output_mode = .Obj,
.extension = T,
.cbe = true,
.extension = ext,
.object_format = .c,
.files = std.ArrayList(File).init(ctx.cases.allocator),
}) catch unreachable;
return &ctx.cases.items[ctx.cases.items.len - 1];
}
pub fn c(ctx: *TestContext, name: []const u8, target: std.zig.CrossTarget, src: [:0]const u8, comptime out: [:0]const u8) void {
ctx.addC(name, target, .Zig).addTransform(src, cheader ++ out);
pub fn c(ctx: *TestContext, name: []const u8, target: CrossTarget, src: [:0]const u8, comptime out: [:0]const u8) void {
ctx.addC(name, target, .Zig).addCompareObjectFile(src, c_header ++ out);
}
pub fn h(ctx: *TestContext, name: []const u8, target: std.zig.CrossTarget, src: [:0]const u8, comptime out: [:0]const u8) void {
ctx.addC(name, target, .Zig).addHeader(src, cheader ++ out);
pub fn h(ctx: *TestContext, name: []const u8, target: CrossTarget, src: [:0]const u8, comptime out: [:0]const u8) void {
ctx.addC(name, target, .Zig).addHeader(src, c_header ++ out);
}
pub fn addCompareOutput(
ctx: *TestContext,
name: []const u8,
T: TestType,
extension: Extension,
src: [:0]const u8,
expected_stdout: []const u8,
) void {
ctx.addExe(name, .{}, T).addCompareOutput(src, expected_stdout);
ctx.addExe(name, .{}, extension).addCompareOutput(src, expected_stdout);
}
/// Adds a test case that compiles the Zig source given in `src`, executes
@ -321,12 +350,12 @@ pub const TestContext = struct {
pub fn addTransform(
ctx: *TestContext,
name: []const u8,
target: std.zig.CrossTarget,
T: TestType,
target: CrossTarget,
extension: Extension,
src: [:0]const u8,
result: [:0]const u8,
) void {
ctx.addObj(name, target, T).addTransform(src, result);
ctx.addObj(name, target, extension).addTransform(src, result);
}
/// Adds a test case that compiles the Zig given in `src` to ZIR and tests
@ -334,7 +363,7 @@ pub const TestContext = struct {
pub fn transform(
ctx: *TestContext,
name: []const u8,
target: std.zig.CrossTarget,
target: CrossTarget,
src: [:0]const u8,
result: [:0]const u8,
) void {
@ -346,7 +375,7 @@ pub const TestContext = struct {
pub fn transformZIR(
ctx: *TestContext,
name: []const u8,
target: std.zig.CrossTarget,
target: CrossTarget,
src: [:0]const u8,
result: [:0]const u8,
) void {
@ -356,12 +385,12 @@ pub const TestContext = struct {
pub fn addError(
ctx: *TestContext,
name: []const u8,
target: std.zig.CrossTarget,
T: TestType,
target: CrossTarget,
extension: Extension,
src: [:0]const u8,
expected_errors: []const []const u8,
) void {
ctx.addObj(name, target, T).addError(src, expected_errors);
ctx.addObj(name, target, extension).addError(src, expected_errors);
}
/// Adds a test case that ensures that the Zig given in `src` fails to
@ -370,7 +399,7 @@ pub const TestContext = struct {
pub fn compileError(
ctx: *TestContext,
name: []const u8,
target: std.zig.CrossTarget,
target: CrossTarget,
src: [:0]const u8,
expected_errors: []const []const u8,
) void {
@ -383,7 +412,7 @@ pub const TestContext = struct {
pub fn compileErrorZIR(
ctx: *TestContext,
name: []const u8,
target: std.zig.CrossTarget,
target: CrossTarget,
src: [:0]const u8,
expected_errors: []const []const u8,
) void {
@ -393,11 +422,11 @@ pub const TestContext = struct {
pub fn addCompiles(
ctx: *TestContext,
name: []const u8,
target: std.zig.CrossTarget,
T: TestType,
target: CrossTarget,
extension: Extension,
src: [:0]const u8,
) void {
ctx.addObj(name, target, T).compiles(src);
ctx.addObj(name, target, extension).compiles(src);
}
/// Adds a test case that asserts that the Zig given in `src` compiles
@ -405,7 +434,7 @@ pub const TestContext = struct {
pub fn compiles(
ctx: *TestContext,
name: []const u8,
target: std.zig.CrossTarget,
target: CrossTarget,
src: [:0]const u8,
) void {
ctx.addCompiles(name, target, .Zig, src);
@ -416,7 +445,7 @@ pub const TestContext = struct {
pub fn compilesZIR(
ctx: *TestContext,
name: []const u8,
target: std.zig.CrossTarget,
target: CrossTarget,
src: [:0]const u8,
) void {
ctx.addCompiles(name, target, .ZIR, src);
@ -430,7 +459,7 @@ pub const TestContext = struct {
pub fn incrementalFailure(
ctx: *TestContext,
name: []const u8,
target: std.zig.CrossTarget,
target: CrossTarget,
src: [:0]const u8,
expected_errors: []const []const u8,
fixed_src: [:0]const u8,
@ -448,7 +477,7 @@ pub const TestContext = struct {
pub fn incrementalFailureZIR(
ctx: *TestContext,
name: []const u8,
target: std.zig.CrossTarget,
target: CrossTarget,
src: [:0]const u8,
expected_errors: []const []const u8,
fixed_src: [:0]const u8,
@ -548,12 +577,11 @@ pub const TestContext = struct {
.root_src_path = tmp_src_path,
};
const ofmt: ?std.builtin.ObjectFormat = if (case.cbe) .c else null;
const bin_name = try std.zig.binNameAlloc(arena, .{
.root_name = "test_case",
.target = target,
.output_mode = case.output_mode,
.object_format = ofmt,
.object_format = case.object_format,
});
const emit_directory: Compilation.Directory = .{
@ -564,7 +592,7 @@ pub const TestContext = struct {
.directory = emit_directory,
.basename = bin_name,
};
const emit_h: ?Compilation.EmitLoc = if (case.cbe)
const emit_h: ?Compilation.EmitLoc = if (case.emit_h)
.{
.directory = emit_directory,
.basename = "test_case.h",
@ -588,7 +616,7 @@ pub const TestContext = struct {
.emit_h = emit_h,
.root_pkg = &root_pkg,
.keep_source_files_loaded = true,
.object_format = ofmt,
.object_format = case.object_format,
.is_native_os = case.target.isNativeOs(),
.is_native_abi = case.target.isNativeAbi(),
});
@ -631,9 +659,10 @@ pub const TestContext = struct {
},
}
}
if (case.cbe) {
const C = comp.bin_file.cast(link.File.C).?;
std.debug.print("Generated C: \n===============\n{}\n\n===========\n\n", .{C.main.items});
if (comp.bin_file.cast(link.File.C)) |c_file| {
std.debug.print("Generated C: \n===============\n{}\n\n===========\n\n", .{
c_file.main.items,
});
}
std.debug.print("Test failed.\n", .{});
std.process.exit(1);
@ -644,67 +673,37 @@ pub const TestContext = struct {
.Header => |expected_output| {
var file = try tmp.dir.openFile("test_case.h", .{ .read = true });
defer file.close();
var out = file.reader().readAllAlloc(arena, 1024 * 1024) catch @panic("Unable to read headeroutput!");
const out = try file.reader().readAllAlloc(arena, 5 * 1024 * 1024);
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);
},
.CompareObjectFile => |expected_output| {
var file = try tmp.dir.openFile(bin_name, .{ .read = true });
defer file.close();
const out = try file.reader().readAllAlloc(arena, 5 * 1024 * 1024);
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
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!");
update_node.setEstimatedTotalItems(5);
var emit_node = update_node.start("emit", 0);
emit_node.activate();
var new_zir_module = try zir.emit(allocator, comp.bin_file.options.module.?);
defer new_zir_module.deinit(allocator);
emit_node.end();
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);
}
}
} else {
update_node.setEstimatedTotalItems(5);
var emit_node = update_node.start("emit", 0);
emit_node.activate();
var new_zir_module = try zir.emit(allocator, comp.bin_file.options.module.?);
defer new_zir_module.deinit(allocator);
emit_node.end();
var write_node = update_node.start("write", 0);
write_node.activate();
var out_zir = std.ArrayList(u8).init(allocator);
defer out_zir.deinit();
try new_zir_module.writeToStream(allocator, out_zir.outStream());
write_node.end();
var write_node = update_node.start("write", 0);
write_node.activate();
var out_zir = std.ArrayList(u8).init(allocator);
defer out_zir.deinit();
try new_zir_module.writeToStream(allocator, out_zir.outStream());
write_node.end();
var test_node = update_node.start("assert", 0);
test_node.activate();
defer test_node.end();
var test_node = update_node.start("assert", 0);
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| {
var test_node = update_node.start("assert", 0);
@ -762,8 +761,6 @@ pub const TestContext = struct {
}
},
.Execution => |expected_stdout| {
std.debug.assert(!case.cbe);
update_node.setEstimatedTotalItems(4);
var exec_result = x: {
var exec_node = update_node.start("execute", 0);
@ -773,9 +770,12 @@ pub const TestContext = struct {
var argv = std.ArrayList([]const u8).init(allocator);
defer argv.deinit();
const exe_path = try std.fmt.allocPrint(arena, "." ++ std.fs.path.sep_str ++ "{}", .{bin_name});
switch (case.target.getExternalExecutor()) {
const exe_path = try std.fmt.allocPrint(arena, "." ++ std.fs.path.sep_str ++ "{s}", .{bin_name});
if (case.object_format != null and case.object_format.? == .c) {
try argv.appendSlice(&[_][]const u8{
std.testing.zig_exe_path, "run", exe_path, "-lc",
});
} else switch (case.target.getExternalExecutor()) {
.native => try argv.append(exe_path),
.unavailable => {
try self.runInterpreterIfAvailable(allocator, &exec_node, case, tmp.dir, bin_name);
@ -837,18 +837,13 @@ pub const TestContext = struct {
switch (exec_result.term) {
.Exited => |code| {
if (code != 0) {
std.debug.print("elf file exited with code {}\n", .{code});
std.debug.print("execution exited with code {}\n", .{code});
return error.BinaryBadExitCode;
}
},
else => return error.BinaryCrashed,
}
if (!std.mem.eql(u8, expected_stdout, exec_result.stdout)) {
std.debug.panic(
"update index {}, mismatched stdout\n====Expected (len={}):====\n{}\n====Actual (len={}):====\n{}\n========\n",
.{ update_index, expected_stdout.len, expected_stdout, exec_result.stdout.len, exec_result.stdout },
);
}
std.testing.expectEqualStrings(expected_stdout, exec_result.stdout);
},
}
}

View File

@ -172,7 +172,15 @@ pub const Type = extern union {
const is_slice_b = isSlice(b);
if (is_slice_a != is_slice_b)
return false;
@panic("TODO implement more pointer Type equality comparison");
const ptr_size_a = ptrSize(a);
const ptr_size_b = ptrSize(b);
if (ptr_size_a != ptr_size_b)
return false;
std.debug.panic("TODO implement more pointer Type equality comparison: {} and {}", .{
a, b,
});
},
.Int => {
// Detect that e.g. u64 != usize, even if the bits match on a particular target.
@ -1128,6 +1136,88 @@ pub const Type = extern union {
};
}
/// Asserts the `Type` is a pointer.
pub fn ptrSize(self: Type) std.builtin.TypeInfo.Pointer.Size {
return switch (self.tag()) {
.u8,
.i8,
.u16,
.i16,
.u32,
.i32,
.u64,
.i64,
.usize,
.isize,
.c_short,
.c_ushort,
.c_int,
.c_uint,
.c_long,
.c_ulong,
.c_longlong,
.c_ulonglong,
.c_longdouble,
.f16,
.f32,
.f64,
.f128,
.c_void,
.bool,
.void,
.type,
.anyerror,
.comptime_int,
.comptime_float,
.noreturn,
.@"null",
.@"undefined",
.array,
.array_sentinel,
.array_u8,
.array_u8_sentinel_0,
.fn_noreturn_no_args,
.fn_void_no_args,
.fn_naked_noreturn_no_args,
.fn_ccc_void_no_args,
.function,
.int_unsigned,
.int_signed,
.optional,
.optional_single_mut_pointer,
.optional_single_const_pointer,
.enum_literal,
.error_union,
.@"anyframe",
.anyframe_T,
.anyerror_void_error_union,
.error_set,
.error_set_single,
.empty_struct,
=> unreachable,
.const_slice,
.mut_slice,
.const_slice_u8,
=> .Slice,
.many_const_pointer,
.many_mut_pointer,
=> .Many,
.c_const_pointer,
.c_mut_pointer,
=> .C,
.single_const_pointer,
.single_mut_pointer,
.single_const_pointer_to_comptime_int,
=> .One,
.pointer => self.cast(Payload.Pointer).?.size,
};
}
pub fn isSlice(self: Type) bool {
return switch (self.tag()) {
.u8,

View File

@ -82,6 +82,7 @@ pub const Value = extern union {
int_big_positive,
int_big_negative,
function,
extern_fn,
variable,
ref_val,
decl_ref,
@ -205,6 +206,7 @@ pub const Value = extern union {
@panic("TODO implement copying of big ints");
},
.function => return self.copyPayloadShallow(allocator, Payload.Function),
.extern_fn => return self.copyPayloadShallow(allocator, Payload.ExternFn),
.variable => return self.copyPayloadShallow(allocator, Payload.Variable),
.ref_val => {
const payload = @fieldParentPtr(Payload.RefVal, "base", self.ptr_otherwise);
@ -337,6 +339,7 @@ pub const Value = extern union {
.int_big_positive => return out_stream.print("{}", .{val.cast(Payload.IntBigPositive).?.asBigInt()}),
.int_big_negative => return out_stream.print("{}", .{val.cast(Payload.IntBigNegative).?.asBigInt()}),
.function => return out_stream.writeAll("(function)"),
.extern_fn => return out_stream.writeAll("(extern function)"),
.variable => return out_stream.writeAll("(variable)"),
.ref_val => {
const ref_val = val.cast(Payload.RefVal).?;
@ -468,6 +471,7 @@ pub const Value = extern union {
.int_big_positive,
.int_big_negative,
.function,
.extern_fn,
.variable,
.ref_val,
.decl_ref,
@ -533,6 +537,7 @@ pub const Value = extern union {
.anyframe_type,
.null_value,
.function,
.extern_fn,
.variable,
.ref_val,
.decl_ref,
@ -617,6 +622,7 @@ pub const Value = extern union {
.anyframe_type,
.null_value,
.function,
.extern_fn,
.variable,
.ref_val,
.decl_ref,
@ -701,6 +707,7 @@ pub const Value = extern union {
.anyframe_type,
.null_value,
.function,
.extern_fn,
.variable,
.ref_val,
.decl_ref,
@ -812,6 +819,7 @@ pub const Value = extern union {
.anyframe_type,
.null_value,
.function,
.extern_fn,
.variable,
.ref_val,
.decl_ref,
@ -901,6 +909,7 @@ pub const Value = extern union {
.anyframe_type,
.null_value,
.function,
.extern_fn,
.variable,
.ref_val,
.decl_ref,
@ -1071,6 +1080,7 @@ pub const Value = extern union {
.bool_false,
.null_value,
.function,
.extern_fn,
.variable,
.ref_val,
.decl_ref,
@ -1150,6 +1160,7 @@ pub const Value = extern union {
.anyframe_type,
.null_value,
.function,
.extern_fn,
.variable,
.ref_val,
.decl_ref,
@ -1383,6 +1394,10 @@ pub const Value = extern union {
const payload = @fieldParentPtr(Payload.Function, "base", self.ptr_otherwise);
std.hash.autoHash(&hasher, payload.func);
},
.extern_fn => {
const payload = @fieldParentPtr(Payload.ExternFn, "base", self.ptr_otherwise);
std.hash.autoHash(&hasher, payload.decl);
},
.variable => {
const payload = @fieldParentPtr(Payload.Variable, "base", self.ptr_otherwise);
std.hash.autoHash(&hasher, payload.variable);
@ -1449,6 +1464,7 @@ pub const Value = extern union {
.bool_false,
.null_value,
.function,
.extern_fn,
.variable,
.int_u64,
.int_i64,
@ -1533,6 +1549,7 @@ pub const Value = extern union {
.bool_false,
.null_value,
.function,
.extern_fn,
.variable,
.int_u64,
.int_i64,
@ -1634,6 +1651,7 @@ pub const Value = extern union {
.bool_true,
.bool_false,
.function,
.extern_fn,
.variable,
.int_u64,
.int_i64,
@ -1730,6 +1748,7 @@ pub const Value = extern union {
.bool_true,
.bool_false,
.function,
.extern_fn,
.variable,
.int_u64,
.int_i64,
@ -1793,6 +1812,11 @@ pub const Value = extern union {
func: *Module.Fn,
};
pub const ExternFn = struct {
base: Payload = Payload{ .tag = .extern_fn },
decl: *Module.Decl,
};
pub const Variable = struct {
base: Payload = Payload{ .tag = .variable },
variable: *Module.Var,

View File

@ -9,12 +9,37 @@ const linux_x64 = std.zig.CrossTarget{
};
pub fn addCases(ctx: *TestContext) !void {
{
var case = ctx.exeFromCompiledC("hello world with updates", .{});
// Regular old hello world
case.addCompareOutput(
\\extern fn puts(s: [*:0]const u8) c_int;
\\export fn main() c_int {
\\ _ = puts("hello world!");
\\ return 0;
\\}
, "hello world!" ++ std.cstr.line_sep);
// 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 {
// \\ _ = puts("yo");
// \\ return 0;
// \\}
//, "yo" ++ std.cstr.line_sep);
}
ctx.c("empty start function", linux_x64,
\\export fn _start() noreturn {
\\ unreachable;
\\}
,
\\zig_noreturn void _start(void) {
\\ zig_breakpoint();
\\ zig_unreachable();
\\}
\\
@ -41,6 +66,7 @@ pub fn addCases(ctx: *TestContext) !void {
\\}
\\
\\zig_noreturn void main(void) {
\\ zig_breakpoint();
\\ zig_unreachable();
\\}
\\
@ -61,22 +87,21 @@ pub fn addCases(ctx: *TestContext) !void {
\\ exitGood();
\\}
,
\\#include <stddef.h>
\\
\\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();
\\}
\\
\\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,22 +121,21 @@ pub fn addCases(ctx: *TestContext) !void {
\\}
\\
,
\\#include <stddef.h>
\\zig_noreturn void exit(uintptr_t arg0);
\\
\\zig_noreturn void exit(size_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);
\\}
\\
\\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,24 +155,22 @@ pub fn addCases(ctx: *TestContext) !void {
\\}
\\
,
\\#include <stddef.h>
\\#include <stdint.h>
\\
\\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);
\\}
\\
\\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,15 +194,12 @@ pub fn addCases(ctx: *TestContext) !void {
\\}
\\
,
\\#include <stddef.h>
\\#include <stdint.h>
\\
\\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);
@ -193,10 +212,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,15 +240,12 @@ pub fn addCases(ctx: *TestContext) !void {
\\}
\\
,
\\#include <stddef.h>
\\#include <stdint.h>
\\
\\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);
@ -241,10 +258,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();
\\}
\\
@ -252,33 +270,25 @@ pub fn addCases(ctx: *TestContext) !void {
ctx.h("header with single param function", linux_x64,
\\export fn start(a: u8) void{}
,
\\#include <stdint.h>
\\
\\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 <stdint.h>
\\
\\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 <stdint.h>
\\
\\void start(uint32_t arg0);
\\
);
ctx.h("header with usize param function", linux_x64,
\\export fn start(a: usize) void{}
,
\\#include <stddef.h>
\\
\\void start(size_t arg0);
\\void start(uintptr_t arg0);
\\
);
ctx.h("header with bool param function", linux_x64,
@ -308,10 +318,7 @@ pub fn addCases(ctx: *TestContext) !void {
ctx.h("header with multiple includes", linux_x64,
\\export fn start(a: u32, b: usize) void{}
,
\\#include <stddef.h>
\\#include <stdint.h>
\\
\\void start(uint32_t arg0, size_t arg1);
\\void start(uint32_t arg0, uintptr_t arg1);
\\
);
}