Commit 3bad1c16cc
Changed files (4)
src-self-hosted
test
stage2
src-self-hosted/cgen.zig
@@ -26,6 +26,14 @@ fn renderType(file: *C, writer: std.ArrayList(u8).Writer, T: Type, src: usize) !
try writer.writeAll("noreturn void");
},
.Void => try writer.writeAll("void"),
+ .Int => {
+ if (T.tag() == .u8) {
+ file.need_stdint = true;
+ try writer.writeAll("uint8_t");
+ } else {
+ return file.fail(src, "TODO implement int types", .{});
+ }
+ },
else => |e| return file.fail(src, "TODO implement type {}", .{e}),
}
}
@@ -116,6 +124,12 @@ pub fn generate(file: *C, decl: *Decl) !void {
if (call.func.cast(ir.Inst.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 (ret_ty != .void and ret_ty != .noreturn) {
+ // TODO: don't do this if we're actually using the value
+ try writer.print("(void)", .{});
+ }
const tname = mem.spanZ(target.name);
if (file.called.get(tname) == null) {
try file.called.put(tname, void{});
@@ -133,6 +147,28 @@ pub fn generate(file: *C, decl: *Decl) !void {
return file.fail(decl.src(), "TODO non-constant call inst?", .{});
}
},
+ .ret => {
+ const ret_value: *ir.Inst = inst.cast(ir.Inst.Ret).?.args.operand;
+ const expected_return_type = tv.ty.fnReturnType();
+ const value = ret_value.value().?;
+ if (expected_return_type.eql(ret_value.ty)) {
+ return file.fail(decl.src(), "TODO return {}", .{expected_return_type});
+ } else {
+ if (expected_return_type.isInt() and ret_value.ty.tag() == .comptime_int) {
+ if (value.intFitsInType(expected_return_type, file.options.target)) {
+ if (expected_return_type.intInfo(file.options.target).bits <= 64) {
+ try writer.print("return {};", .{value.toUnsignedInt()});
+ } else {
+ return file.fail(decl.src(), "TODO return ints > 64 bits", .{});
+ }
+ } else {
+ return file.fail(decl.src(), "comptime int {} does not fit in {}", .{ value.toUnsignedInt(), expected_return_type });
+ }
+ } else {
+ return file.fail(decl.src(), "return type mismatch: expected {}, found {}", .{ expected_return_type, ret_value.ty });
+ }
+ }
+ },
else => |e| {
return file.fail(decl.src(), "TODO {}", .{e});
},
src-self-hosted/Module.zig
@@ -989,14 +989,22 @@ pub fn performAllTheWork(self: *Module) error{OutOfMemory}!void {
error.AnalysisFail => {
decl.analysis = .dependency_failure;
},
+ error.CGenFailure => {
+ // Error is handled by CBE, don't try adding it again
+ },
else => {
try self.failed_decls.ensureCapacity(self.gpa, self.failed_decls.items().len + 1);
- self.failed_decls.putAssumeCapacityNoClobber(decl, try ErrorMsg.create(
- self.gpa,
- decl.src(),
- "unable to codegen: {}",
- .{@errorName(err)},
- ));
+ const result = self.failed_decls.getOrPutAssumeCapacity(decl);
+ if (result.found_existing) {
+ std.debug.panic("Internal error: attempted to override error '{}' with 'unable to codegen: {}'", .{ result.entry.value.msg, @errorName(err) });
+ } else {
+ result.entry.value = try ErrorMsg.create(
+ self.gpa,
+ decl.src(),
+ "unable to codegen: {}",
+ .{@errorName(err)},
+ );
+ }
decl.analysis = .codegen_failure_retryable;
},
};
@@ -1300,6 +1308,20 @@ fn astGenExpr(self: *Module, scope: *Scope, ast_node: *ast.Node) InnerError!*zir
fn astGenInfixOp(self: *Module, scope: *Scope, infix_node: *ast.Node.InfixOp) InnerError!*zir.Inst {
switch (infix_node.op) {
+ .Assign => {
+ if (infix_node.lhs.id == .Identifier) {
+ const ident = @fieldParentPtr(ast.Node.Identifier, "base", infix_node.lhs);
+ const tree = scope.tree();
+ const ident_name = tree.tokenSlice(ident.token);
+ if (std.mem.eql(u8, ident_name, "_")) {
+ return self.astGenExpr(scope, infix_node.rhs);
+ } else {
+ return self.failNode(scope, &infix_node.base, "TODO implement infix operator assign", .{});
+ }
+ } else {
+ return self.failNode(scope, &infix_node.base, "TODO implement infix operator assign", .{});
+ }
+ },
.Add => {
const lhs = try self.astGenExpr(scope, infix_node.lhs);
const rhs = try self.astGenExpr(scope, infix_node.rhs);
src-self-hosted/type.zig
@@ -921,6 +921,11 @@ pub const Type = extern union {
};
}
+ /// Returns true if and only if the type is a fixed-width integer.
+ pub fn isInt(self: Type) bool {
+ return self.isSignedInt() or self.isUnsignedInt();
+ }
+
/// Returns true if and only if the type is a fixed-width, signed integer.
pub fn isSignedInt(self: Type) bool {
return switch (self.tag()) {
@@ -975,6 +980,60 @@ pub const Type = extern union {
};
}
+ /// Returns true if and only if the type is a fixed-width, unsigned integer.
+ pub fn isUnsignedInt(self: Type) bool {
+ return switch (self.tag()) {
+ .f16,
+ .f32,
+ .f64,
+ .f128,
+ .c_longdouble,
+ .c_void,
+ .bool,
+ .void,
+ .type,
+ .anyerror,
+ .comptime_int,
+ .comptime_float,
+ .noreturn,
+ .@"null",
+ .@"undefined",
+ .fn_noreturn_no_args,
+ .fn_void_no_args,
+ .fn_naked_noreturn_no_args,
+ .fn_ccc_void_no_args,
+ .function,
+ .array,
+ .single_const_pointer,
+ .single_const_pointer_to_comptime_int,
+ .array_u8_sentinel_0,
+ .const_slice_u8,
+ .int_signed,
+ .i8,
+ .isize,
+ .c_short,
+ .c_int,
+ .c_long,
+ .c_longlong,
+ .i16,
+ .i32,
+ .i64,
+ => false,
+
+ .int_unsigned,
+ .u8,
+ .usize,
+ .c_ushort,
+ .c_uint,
+ .c_ulong,
+ .c_ulonglong,
+ .u16,
+ .u32,
+ .u64,
+ => true,
+ };
+ }
+
/// Asserts the type is an integer.
pub fn intInfo(self: Type, target: Target) struct { signed: bool, bits: u16 } {
return switch (self.tag()) {
test/stage2/cbe.zig
@@ -65,26 +65,26 @@ pub fn addCases(ctx: *TestContext) !void {
\\}
\\
);
- //ctx.c("basic return", linux_x64,
- // \\fn main() u8 {
- // \\ return 103;
- // \\}
- // \\
- // \\export fn _start() noreturn {
- // \\ _ = main();
- // \\}
- //,
- // \\#include <stdint.h>
- // \\
- // \\uint8_t main(void);
- // \\
- // \\noreturn void _start(void) {
- // \\ (void)main();
- // \\}
- // \\
- // \\uint8_t main(void) {
- // \\ return 103;
- // \\}
- // \\
- //);
+ ctx.c("basic return", linux_x64,
+ \\fn main() u8 {
+ \\ return 103;
+ \\}
+ \\
+ \\export fn _start() noreturn {
+ \\ _ = main();
+ \\}
+ ,
+ \\#include <stdint.h>
+ \\
+ \\uint8_t main(void);
+ \\
+ \\noreturn void _start(void) {
+ \\ (void)main();
+ \\}
+ \\
+ \\uint8_t main(void) {
+ \\ return 103;
+ \\}
+ \\
+ );
}