aboutsummaryrefslogtreecommitdiff
path: root/src-self-hosted/codegen/c.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src-self-hosted/codegen/c.zig')
-rw-r--r--src-self-hosted/codegen/c.zig239
1 files changed, 170 insertions, 69 deletions
diff --git a/src-self-hosted/codegen/c.zig b/src-self-hosted/codegen/c.zig
index db9d9a1030..f0d3d8367a 100644
--- a/src-self-hosted/codegen/c.zig
+++ b/src-self-hosted/codegen/c.zig
@@ -17,40 +17,58 @@ fn map(allocator: *std.mem.Allocator, name: []const u8) ![]const u8 {
return allocator.dupe(u8, name);
}
-fn renderType(file: *C, writer: std.ArrayList(u8).Writer, T: Type, src: usize) !void {
- if (T.tag() == .usize) {
- file.need_stddef = true;
- try writer.writeAll("size_t");
- } else {
- switch (T.zigTypeTag()) {
- .NoReturn => {
- file.need_noreturn = true;
- 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}),
- }
+fn renderType(ctx: *Context, writer: std.ArrayList(u8).Writer, T: Type) !void {
+ switch (T.zigTypeTag()) {
+ .NoReturn => {
+ try writer.writeAll("zig_noreturn void");
+ },
+ .Void => try writer.writeAll("void"),
+ .Int => {
+ if (T.tag() == .u8) {
+ ctx.file.need_stdint = true;
+ try writer.writeAll("uint8_t");
+ } else if (T.tag() == .usize) {
+ ctx.file.need_stddef = true;
+ try writer.writeAll("size_t");
+ } else {
+ return ctx.file.fail(ctx.decl.src(), "TODO implement int types", .{});
+ }
+ },
+ else => |e| return ctx.file.fail(ctx.decl.src(), "TODO implement type {}", .{e}),
}
}
-fn renderFunctionSignature(file: *C, writer: std.ArrayList(u8).Writer, decl: *Decl) !void {
+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()});
+ },
+ else => |e| return ctx.file.fail(ctx.decl.src(), "TODO implement value {}", .{e}),
+ }
+}
+
+fn renderFunctionSignature(ctx: *Context, writer: std.ArrayList(u8).Writer, decl: *Decl) !void {
const tv = decl.typed_value.most_recent.typed_value;
- try renderType(file, writer, tv.ty.fnReturnType(), decl.src());
- const name = try map(file.base.allocator, mem.spanZ(decl.name));
- defer file.base.allocator.free(name);
+ try renderType(ctx, writer, tv.ty.fnReturnType());
+ const name = try map(ctx.file.base.allocator, mem.spanZ(decl.name));
+ defer ctx.file.base.allocator.free(name);
try writer.print(" {}(", .{name});
- if (tv.ty.fnParamLen() == 0)
- try writer.writeAll("void)")
- else
- return file.fail(decl.src(), "TODO implement parameters", .{});
+ var param_len = tv.ty.fnParamLen();
+ if (param_len == 0)
+ try writer.writeAll("void")
+ else {
+ var index: usize = 0;
+ while (index < param_len) : (index += 1) {
+ if (index > 0) {
+ try writer.writeAll(", ");
+ }
+ try renderType(ctx, writer, tv.ty.fnParamType(index));
+ try writer.print(" arg{}", .{index});
+ }
+ }
+ try writer.writeByte(')');
}
pub fn generate(file: *C, decl: *Decl) !void {
@@ -78,11 +96,40 @@ fn genArray(file: *C, decl: *Decl) !void {
return file.fail(decl.src(), "TODO non-byte arrays", .{});
}
+const Context = struct {
+ file: *C,
+ decl: *Decl,
+ inst_map: std.AutoHashMap(*Inst, []u8),
+ argdex: usize = 0,
+ unnamed_index: usize = 0,
+
+ fn name(self: *Context) ![]u8 {
+ const val = try std.fmt.allocPrint(self.file.base.allocator, "__temp_{}", .{self.unnamed_index});
+ self.unnamed_index += 1;
+ return val;
+ }
+
+ fn deinit(self: *Context) void {
+ for (self.inst_map.items()) |kv| {
+ self.file.base.allocator.free(kv.value);
+ }
+ self.inst_map.deinit();
+ self.* = undefined;
+ }
+};
+
fn genFn(file: *C, decl: *Decl) !void {
const writer = file.main.writer();
const tv = decl.typed_value.most_recent.typed_value;
- try renderFunctionSignature(file, writer, decl);
+ var ctx = Context{
+ .file = file,
+ .decl = decl,
+ .inst_map = std.AutoHashMap(*Inst, []u8).init(file.base.allocator),
+ };
+ defer ctx.deinit();
+
+ try renderFunctionSignature(&ctx, writer, decl);
try writer.writeAll(" {");
@@ -91,13 +138,19 @@ fn genFn(file: *C, decl: *Decl) !void {
if (instructions.len > 0) {
try writer.writeAll("\n");
for (instructions) |inst| {
- switch (inst.tag) {
- .assembly => try genAsm(file, inst.castTag(.assembly).?, decl),
- .call => try genCall(file, inst.castTag(.call).?, decl),
- .ret => try genRet(file, inst.castTag(.ret).?, decl, tv.ty.fnReturnType()),
- .retvoid => try file.main.writer().print(" return;\n", .{}),
- .dbg_stmt => try genDbgStmt(file, inst.castTag(.dbg_stmt).?, decl),
+ if (switch (inst.tag) {
+ .assembly => try genAsm(&ctx, inst.castTag(.assembly).?),
+ .call => try genCall(&ctx, inst.castTag(.call).?),
+ .ret => try genRet(&ctx, inst.castTag(.ret).?),
+ .retvoid => try genRetVoid(&ctx),
+ .arg => try genArg(&ctx),
+ .dbg_stmt => try genDbgStmt(&ctx, inst.castTag(.dbg_stmt).?),
+ .breakpoint => try genBreak(&ctx, inst.castTag(.breakpoint).?),
+ .unreach => try genUnreach(&ctx, inst.castTag(.unreach).?),
+ .intcast => try genIntCast(&ctx, inst.castTag(.intcast).?),
else => |e| return file.fail(decl.src(), "TODO implement C codegen for {}", .{e}),
+ }) |name| {
+ try ctx.inst_map.putNoClobber(inst, name);
}
}
}
@@ -105,13 +158,40 @@ fn genFn(file: *C, decl: *Decl) !void {
try writer.writeAll("}\n\n");
}
-fn genRet(file: *C, inst: *Inst.UnOp, decl: *Decl, expected_return_type: Type) !void {
- return file.fail(decl.src(), "TODO return {}", .{expected_return_type});
+fn genArg(ctx: *Context) !?[]u8 {
+ const name = try std.fmt.allocPrint(ctx.file.base.allocator, "arg{}", .{ctx.argdex});
+ ctx.argdex += 1;
+ return name;
}
-fn genCall(file: *C, inst: *Inst.Call, decl: *Decl) !void {
- const writer = file.main.writer();
- const header = file.header.writer();
+fn genRetVoid(ctx: *Context) !?[]u8 {
+ try ctx.file.main.writer().print(" return;\n", .{});
+ return null;
+}
+
+fn genRet(ctx: *Context, inst: *Inst.UnOp) !?[]u8 {
+ return ctx.file.fail(ctx.decl.src(), "TODO return", .{});
+}
+
+fn genIntCast(ctx: *Context, inst: *Inst.UnOp) !?[]u8 {
+ if (inst.base.isUnused())
+ return null;
+ const op = inst.operand;
+ const writer = ctx.file.main.writer();
+ const name = try ctx.name();
+ const from = ctx.inst_map.get(op) orelse
+ return ctx.file.fail(ctx.decl.src(), "Internal error in C backend: intCast argument not found in inst_map", .{});
+ try writer.writeAll(" const ");
+ try renderType(ctx, writer, inst.base.ty);
+ try writer.print(" {} = (", .{name});
+ try renderType(ctx, writer, inst.base.ty);
+ try writer.print("){};\n", .{from});
+ return name;
+}
+
+fn genCall(ctx: *Context, inst: *Inst.Call) !?[]u8 {
+ const writer = ctx.file.main.writer();
+ const header = ctx.file.header.writer();
try writer.writeAll(" ");
if (inst.func.castTag(.constant)) |func_inst| {
if (func_inst.val.cast(Value.Payload.Function)) |func_val| {
@@ -122,52 +202,77 @@ fn genCall(file: *C, inst: *Inst.Call, decl: *Decl) !void {
try writer.print("(void)", .{});
}
const tname = mem.spanZ(target.name);
- if (file.called.get(tname) == null) {
- try file.called.put(tname, void{});
- try renderFunctionSignature(file, header, target);
+ if (ctx.file.called.get(tname) == null) {
+ try ctx.file.called.put(tname, void{});
+ try renderFunctionSignature(ctx, header, target);
try header.writeAll(";\n");
}
- try writer.print("{}();\n", .{tname});
+ 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 {
+ return ctx.file.fail(ctx.decl.src(), "TODO call pass arg {}", .{arg});
+ }
+ }
+ }
+ try writer.writeAll(");\n");
} else {
- return file.fail(decl.src(), "TODO non-function call target?", .{});
- }
- if (inst.args.len != 0) {
- return file.fail(decl.src(), "TODO function arguments", .{});
+ return ctx.file.fail(ctx.decl.src(), "TODO non-function call target?", .{});
}
} else {
- return file.fail(decl.src(), "TODO non-constant call inst?", .{});
+ return ctx.file.fail(ctx.decl.src(), "TODO non-constant call inst?", .{});
}
+ return null;
}
-fn genDbgStmt(file: *C, inst: *Inst.NoOp, decl: *Decl) !void {
+fn genDbgStmt(ctx: *Context, inst: *Inst.NoOp) !?[]u8 {
// TODO emit #line directive here with line number and filename
+ return null;
}
-fn genAsm(file: *C, as: *Inst.Assembly, decl: *Decl) !void {
- const writer = file.main.writer();
+fn genBreak(ctx: *Context, inst: *Inst.NoOp) !?[]u8 {
+ // TODO ??
+ return null;
+}
+
+fn genUnreach(ctx: *Context, inst: *Inst.NoOp) !?[]u8 {
+ try ctx.file.main.writer().writeAll(" zig_unreachable();\n");
+ return null;
+}
+
+fn genAsm(ctx: *Context, as: *Inst.Assembly) !?[]u8 {
+ const writer = ctx.file.main.writer();
try writer.writeAll(" ");
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, writer, arg.ty);
+ try writer.print(" {}_constant __asm__(\"{}\") = ", .{ reg, reg });
+ // TODO merge constant handling into inst_map as well
if (arg.castTag(.constant)) |c| {
- if (c.val.tag() == .int_u64) {
- try writer.writeAll("register ");
- try renderType(file, writer, arg.ty, decl.src());
- try writer.print(" {}_constant __asm__(\"{}\") = {};\n ", .{ reg, reg, c.val.toUnsignedInt() });
- } else {
- return file.fail(decl.src(), "TODO inline asm {} args", .{c.val.tag()});
- }
+ try renderValue(ctx, writer, arg.ty, c.val);
+ try writer.writeAll(";\n ");
} else {
- return file.fail(decl.src(), "TODO non-constant inline asm args", .{});
+ const gop = try ctx.inst_map.getOrPut(arg);
+ if (!gop.found_existing) {
+ return ctx.file.fail(ctx.decl.src(), "Internal error in C backend: asm argument not found in inst_map", .{});
+ }
+ try writer.print("{};\n ", .{gop.entry.value});
}
} else {
- return file.fail(decl.src(), "TODO non-explicit inline asm regs", .{});
+ return ctx.file.fail(ctx.decl.src(), "TODO non-explicit inline asm regs", .{});
}
}
try writer.print("__asm {} (\"{}\"", .{ if (as.is_volatile) @as([]const u8, "volatile") else "", as.asm_source });
if (as.output) |o| {
- return file.fail(decl.src(), "TODO inline asm output", .{});
+ return ctx.file.fail(ctx.decl.src(), "TODO inline asm output", .{});
}
if (as.inputs.len > 0) {
if (as.output == null) {
@@ -181,12 +286,7 @@ fn genAsm(file: *C, as: *Inst.Assembly, decl: *Decl) !void {
if (index > 0) {
try writer.writeAll(", ");
}
- if (arg.castTag(.constant)) |c| {
- try writer.print("\"\"({}_constant)", .{reg});
- } else {
- // This is blocked by the earlier test
- unreachable;
- }
+ try writer.print("\"\"({}_constant)", .{reg});
} else {
// This is blocked by the earlier test
unreachable;
@@ -194,4 +294,5 @@ fn genAsm(file: *C, as: *Inst.Assembly, decl: *Decl) !void {
}
}
try writer.writeAll(");\n");
+ return null;
}