| ... | @@ -17,40 +17,58 @@ fn map(allocator: *std.mem.Allocator, name: []const u8) ![]const u8 { | ... | @@ -17,40 +17,58 @@ fn map(allocator: *std.mem.Allocator, name: []const u8) ![]const u8 { |
| 17 | return allocator.dupe(u8, name); | 17 | return allocator.dupe(u8, name); |
| 18 | } | 18 | } |
| 19 | | 19 | |
| 20 | fn renderType(file: *C, writer: std.ArrayList(u8).Writer, T: Type, src: usize) !void { | 20 | fn renderType(ctx: *Context, writer: std.ArrayList(u8).Writer, T: Type) !void { |
| 21 | if (T.tag() == .usize) { | 21 | switch (T.zigTypeTag()) { |
| 22 | file.need_stddef = true; | 22 | .NoReturn => { |
| 23 | try writer.writeAll("size_t"); | 23 | try writer.writeAll("zig_noreturn void"); |
| 24 | } else { | 24 | }, |
| 25 | switch (T.zigTypeTag()) { | 25 | .Void => try writer.writeAll("void"), |
| 26 | .NoReturn => { | 26 | .Int => { |
| 27 | file.need_noreturn = true; | 27 | if (T.tag() == .u8) { |
| 28 | try writer.writeAll("noreturn void"); | 28 | ctx.file.need_stdint = true; |
| 29 | }, | 29 | try writer.writeAll("uint8_t"); |
| 30 | .Void => try writer.writeAll("void"), | 30 | } else if (T.tag() == .usize) { |
| 31 | .Int => { | 31 | ctx.file.need_stddef = true; |
| 32 | if (T.tag() == .u8) { | 32 | try writer.writeAll("size_t"); |
| 33 | file.need_stdint = true; | 33 | } else { |
| 34 | try writer.writeAll("uint8_t"); | 34 | return ctx.file.fail(ctx.decl.src(), "TODO implement int types", .{}); |
| 35 | } else { | 35 | } |
| 36 | return file.fail(src, "TODO implement int types", .{}); | 36 | }, |
| 37 | } | 37 | else => |e| return ctx.file.fail(ctx.decl.src(), "TODO implement type {}", .{e}), |
| 38 | }, | | |
| 39 | else => |e| return file.fail(src, "TODO implement type {}", .{e}), | | |
| 40 | } | | |
| 41 | } | 38 | } |
| 42 | } | 39 | } |
| 43 | | 40 | |
| 44 | fn renderFunctionSignature(file: *C, writer: std.ArrayList(u8).Writer, decl: *Decl) !void { | 41 | fn renderValue(ctx: *Context, writer: std.ArrayList(u8).Writer, T: Type, val: Value) !void { |
| | 42 | switch (T.zigTypeTag()) { |
| | 43 | .Int => { |
| | 44 | if (T.isSignedInt()) |
| | 45 | return writer.print("{}", .{val.toSignedInt()}); |
| | 46 | return writer.print("{}", .{val.toUnsignedInt()}); |
| | 47 | }, |
| | 48 | else => |e| return ctx.file.fail(ctx.decl.src(), "TODO implement value {}", .{e}), |
| | 49 | } |
| | 50 | } |
| | 51 | |
| | 52 | fn renderFunctionSignature(ctx: *Context, writer: std.ArrayList(u8).Writer, decl: *Decl) !void { |
| 45 | const tv = decl.typed_value.most_recent.typed_value; | 53 | const tv = decl.typed_value.most_recent.typed_value; |
| 46 | try renderType(file, writer, tv.ty.fnReturnType(), decl.src()); | 54 | try renderType(ctx, writer, tv.ty.fnReturnType()); |
| 47 | const name = try map(file.base.allocator, mem.spanZ(decl.name)); | 55 | const name = try map(ctx.file.base.allocator, mem.spanZ(decl.name)); |
| 48 | defer file.base.allocator.free(name); | 56 | defer ctx.file.base.allocator.free(name); |
| 49 | try writer.print(" {}(", .{name}); | 57 | try writer.print(" {}(", .{name}); |
| 50 | if (tv.ty.fnParamLen() == 0) | 58 | var param_len = tv.ty.fnParamLen(); |
| 51 | try writer.writeAll("void)") | 59 | if (param_len == 0) |
| 52 | else | 60 | try writer.writeAll("void") |
| 53 | return file.fail(decl.src(), "TODO implement parameters", .{}); | 61 | else { |
| | 62 | var index: usize = 0; |
| | 63 | while (index < param_len) : (index += 1) { |
| | 64 | if (index > 0) { |
| | 65 | try writer.writeAll(", "); |
| | 66 | } |
| | 67 | try renderType(ctx, writer, tv.ty.fnParamType(index)); |
| | 68 | try writer.print(" arg{}", .{index}); |
| | 69 | } |
| | 70 | } |
| | 71 | try writer.writeByte(')'); |
| 54 | } | 72 | } |
| 55 | | 73 | |
| 56 | pub fn generate(file: *C, decl: *Decl) !void { | 74 | pub fn generate(file: *C, decl: *Decl) !void { |
| ... | @@ -78,11 +96,40 @@ fn genArray(file: *C, decl: *Decl) !void { | ... | @@ -78,11 +96,40 @@ fn genArray(file: *C, decl: *Decl) !void { |
| 78 | return file.fail(decl.src(), "TODO non-byte arrays", .{}); | 96 | return file.fail(decl.src(), "TODO non-byte arrays", .{}); |
| 79 | } | 97 | } |
| 80 | | 98 | |
| | 99 | const Context = struct { |
| | 100 | file: *C, |
| | 101 | decl: *Decl, |
| | 102 | inst_map: std.AutoHashMap(*Inst, []u8), |
| | 103 | argdex: usize = 0, |
| | 104 | unnamed_index: usize = 0, |
| | 105 | |
| | 106 | fn name(self: *Context) ![]u8 { |
| | 107 | const val = try std.fmt.allocPrint(self.file.base.allocator, "__temp_{}", .{self.unnamed_index}); |
| | 108 | self.unnamed_index += 1; |
| | 109 | return val; |
| | 110 | } |
| | 111 | |
| | 112 | fn deinit(self: *Context) void { |
| | 113 | for (self.inst_map.items()) |kv| { |
| | 114 | self.file.base.allocator.free(kv.value); |
| | 115 | } |
| | 116 | self.inst_map.deinit(); |
| | 117 | self.* = undefined; |
| | 118 | } |
| | 119 | }; |
| | 120 | |
| 81 | fn genFn(file: *C, decl: *Decl) !void { | 121 | fn genFn(file: *C, decl: *Decl) !void { |
| 82 | const writer = file.main.writer(); | 122 | const writer = file.main.writer(); |
| 83 | const tv = decl.typed_value.most_recent.typed_value; | 123 | const tv = decl.typed_value.most_recent.typed_value; |
| 84 | | 124 | |
| 85 | try renderFunctionSignature(file, writer, decl); | 125 | var ctx = Context{ |
| | 126 | .file = file, |
| | 127 | .decl = decl, |
| | 128 | .inst_map = std.AutoHashMap(*Inst, []u8).init(file.base.allocator), |
| | 129 | }; |
| | 130 | defer ctx.deinit(); |
| | 131 | |
| | 132 | try renderFunctionSignature(&ctx, writer, decl); |
| 86 | | 133 | |
| 87 | try writer.writeAll(" {"); | 134 | try writer.writeAll(" {"); |
| 88 | | 135 | |
| ... | @@ -91,13 +138,19 @@ fn genFn(file: *C, decl: *Decl) !void { | ... | @@ -91,13 +138,19 @@ fn genFn(file: *C, decl: *Decl) !void { |
| 91 | if (instructions.len > 0) { | 138 | if (instructions.len > 0) { |
| 92 | try writer.writeAll("\n"); | 139 | try writer.writeAll("\n"); |
| 93 | for (instructions) |inst| { | 140 | for (instructions) |inst| { |
| 94 | switch (inst.tag) { | 141 | if (switch (inst.tag) { |
| 95 | .assembly => try genAsm(file, inst.castTag(.assembly).?, decl), | 142 | .assembly => try genAsm(&ctx, inst.castTag(.assembly).?), |
| 96 | .call => try genCall(file, inst.castTag(.call).?, decl), | 143 | .call => try genCall(&ctx, inst.castTag(.call).?), |
| 97 | .ret => try genRet(file, inst.castTag(.ret).?, decl, tv.ty.fnReturnType()), | 144 | .ret => try genRet(&ctx, inst.castTag(.ret).?), |
| 98 | .retvoid => try file.main.writer().print(" return;\n", .{}), | 145 | .retvoid => try genRetVoid(&ctx), |
| 99 | .dbg_stmt => try genDbgStmt(file, inst.castTag(.dbg_stmt).?, decl), | 146 | .arg => try genArg(&ctx), |
| | 147 | .dbg_stmt => try genDbgStmt(&ctx, inst.castTag(.dbg_stmt).?), |
| | 148 | .breakpoint => try genBreak(&ctx, inst.castTag(.breakpoint).?), |
| | 149 | .unreach => try genUnreach(&ctx, inst.castTag(.unreach).?), |
| | 150 | .intcast => try genIntCast(&ctx, inst.castTag(.intcast).?), |
| 100 | else => |e| return file.fail(decl.src(), "TODO implement C codegen for {}", .{e}), | 151 | else => |e| return file.fail(decl.src(), "TODO implement C codegen for {}", .{e}), |
| | 152 | }) |name| { |
| | 153 | try ctx.inst_map.putNoClobber(inst, name); |
| 101 | } | 154 | } |
| 102 | } | 155 | } |
| 103 | } | 156 | } |
| ... | @@ -105,13 +158,40 @@ fn genFn(file: *C, decl: *Decl) !void { | ... | @@ -105,13 +158,40 @@ fn genFn(file: *C, decl: *Decl) !void { |
| 105 | try writer.writeAll("}\n\n"); | 158 | try writer.writeAll("}\n\n"); |
| 106 | } | 159 | } |
| 107 | | 160 | |
| 108 | fn genRet(file: *C, inst: *Inst.UnOp, decl: *Decl, expected_return_type: Type) !void { | 161 | fn genArg(ctx: *Context) !?[]u8 { |
| 109 | return file.fail(decl.src(), "TODO return {}", .{expected_return_type}); | 162 | const name = try std.fmt.allocPrint(ctx.file.base.allocator, "arg{}", .{ctx.argdex}); |
| | 163 | ctx.argdex += 1; |
| | 164 | return name; |
| 110 | } | 165 | } |
| 111 | | 166 | |
| 112 | fn genCall(file: *C, inst: *Inst.Call, decl: *Decl) !void { | 167 | fn genRetVoid(ctx: *Context) !?[]u8 { |
| 113 | const writer = file.main.writer(); | 168 | try ctx.file.main.writer().print(" return;\n", .{}); |
| 114 | const header = file.header.writer(); | 169 | return null; |
| | 170 | } |
| | 171 | |
| | 172 | fn genRet(ctx: *Context, inst: *Inst.UnOp) !?[]u8 { |
| | 173 | return ctx.file.fail(ctx.decl.src(), "TODO return", .{}); |
| | 174 | } |
| | 175 | |
| | 176 | fn genIntCast(ctx: *Context, inst: *Inst.UnOp) !?[]u8 { |
| | 177 | if (inst.base.isUnused()) |
| | 178 | return null; |
| | 179 | const op = inst.operand; |
| | 180 | const writer = ctx.file.main.writer(); |
| | 181 | const name = try ctx.name(); |
| | 182 | const from = ctx.inst_map.get(op) orelse |
| | 183 | return ctx.file.fail(ctx.decl.src(), "Internal error in C backend: intCast argument not found in inst_map", .{}); |
| | 184 | try writer.writeAll(" const "); |
| | 185 | try renderType(ctx, writer, inst.base.ty); |
| | 186 | try writer.print(" {} = (", .{name}); |
| | 187 | try renderType(ctx, writer, inst.base.ty); |
| | 188 | try writer.print("){};\n", .{from}); |
| | 189 | return name; |
| | 190 | } |
| | 191 | |
| | 192 | fn genCall(ctx: *Context, inst: *Inst.Call) !?[]u8 { |
| | 193 | const writer = ctx.file.main.writer(); |
| | 194 | const header = ctx.file.header.writer(); |
| 115 | try writer.writeAll(" "); | 195 | try writer.writeAll(" "); |
| 116 | if (inst.func.castTag(.constant)) |func_inst| { | 196 | if (inst.func.castTag(.constant)) |func_inst| { |
| 117 | if (func_inst.val.cast(Value.Payload.Function)) |func_val| { | 197 | if (func_inst.val.cast(Value.Payload.Function)) |func_val| { |
| ... | @@ -122,52 +202,77 @@ fn genCall(file: *C, inst: *Inst.Call, decl: *Decl) !void { | ... | @@ -122,52 +202,77 @@ fn genCall(file: *C, inst: *Inst.Call, decl: *Decl) !void { |
| 122 | try writer.print("(void)", .{}); | 202 | try writer.print("(void)", .{}); |
| 123 | } | 203 | } |
| 124 | const tname = mem.spanZ(target.name); | 204 | const tname = mem.spanZ(target.name); |
| 125 | if (file.called.get(tname) == null) { | 205 | if (ctx.file.called.get(tname) == null) { |
| 126 | try file.called.put(tname, void{}); | 206 | try ctx.file.called.put(tname, void{}); |
| 127 | try renderFunctionSignature(file, header, target); | 207 | try renderFunctionSignature(ctx, header, target); |
| 128 | try header.writeAll(";\n"); | 208 | try header.writeAll(";\n"); |
| 129 | } | 209 | } |
| 130 | try writer.print("{}();\n", .{tname}); | 210 | try writer.print("{}(", .{tname}); |
| | 211 | if (inst.args.len != 0) { |
| | 212 | for (inst.args) |arg, i| { |
| | 213 | if (i > 0) { |
| | 214 | try writer.writeAll(", "); |
| | 215 | } |
| | 216 | if (arg.cast(Inst.Constant)) |con| { |
| | 217 | try renderValue(ctx, writer, arg.ty, con.val); |
| | 218 | } else { |
| | 219 | return ctx.file.fail(ctx.decl.src(), "TODO call pass arg {}", .{arg}); |
| | 220 | } |
| | 221 | } |
| | 222 | } |
| | 223 | try writer.writeAll(");\n"); |
| 131 | } else { | 224 | } else { |
| 132 | return file.fail(decl.src(), "TODO non-function call target?", .{}); | 225 | return ctx.file.fail(ctx.decl.src(), "TODO non-function call target?", .{}); |
| 133 | } | | |
| 134 | if (inst.args.len != 0) { | | |
| 135 | return file.fail(decl.src(), "TODO function arguments", .{}); | | |
| 136 | } | 226 | } |
| 137 | } else { | 227 | } else { |
| 138 | return file.fail(decl.src(), "TODO non-constant call inst?", .{}); | 228 | return ctx.file.fail(ctx.decl.src(), "TODO non-constant call inst?", .{}); |
| 139 | } | 229 | } |
| | 230 | return null; |
| 140 | } | 231 | } |
| 141 | | 232 | |
| 142 | fn genDbgStmt(file: *C, inst: *Inst.NoOp, decl: *Decl) !void { | 233 | fn genDbgStmt(ctx: *Context, inst: *Inst.NoOp) !?[]u8 { |
| 143 | // TODO emit #line directive here with line number and filename | 234 | // TODO emit #line directive here with line number and filename |
| | 235 | return null; |
| 144 | } | 236 | } |
| 145 | | 237 | |
| 146 | fn genAsm(file: *C, as: *Inst.Assembly, decl: *Decl) !void { | 238 | fn genBreak(ctx: *Context, inst: *Inst.NoOp) !?[]u8 { |
| 147 | const writer = file.main.writer(); | 239 | // TODO ?? |
| | 240 | return null; |
| | 241 | } |
| | 242 | |
| | 243 | fn genUnreach(ctx: *Context, inst: *Inst.NoOp) !?[]u8 { |
| | 244 | try ctx.file.main.writer().writeAll(" zig_unreachable();\n"); |
| | 245 | return null; |
| | 246 | } |
| | 247 | |
| | 248 | fn genAsm(ctx: *Context, as: *Inst.Assembly) !?[]u8 { |
| | 249 | const writer = ctx.file.main.writer(); |
| 148 | try writer.writeAll(" "); | 250 | try writer.writeAll(" "); |
| 149 | for (as.inputs) |i, index| { | 251 | for (as.inputs) |i, index| { |
| 150 | if (i[0] == '{' and i[i.len - 1] == '}') { | 252 | if (i[0] == '{' and i[i.len - 1] == '}') { |
| 151 | const reg = i[1 .. i.len - 1]; | 253 | const reg = i[1 .. i.len - 1]; |
| 152 | const arg = as.args[index]; | 254 | const arg = as.args[index]; |
| | 255 | try writer.writeAll("register "); |
| | 256 | try renderType(ctx, writer, arg.ty); |
| | 257 | try writer.print(" {}_constant __asm__(\"{}\") = ", .{ reg, reg }); |
| | 258 | // TODO merge constant handling into inst_map as well |
| 153 | if (arg.castTag(.constant)) |c| { | 259 | if (arg.castTag(.constant)) |c| { |
| 154 | if (c.val.tag() == .int_u64) { | 260 | try renderValue(ctx, writer, arg.ty, c.val); |
| 155 | try writer.writeAll("register "); | 261 | try writer.writeAll(";\n "); |
| 156 | try renderType(file, writer, arg.ty, decl.src()); | | |
| 157 | try writer.print(" {}_constant __asm__(\"{}\") = {};\n ", .{ reg, reg, c.val.toUnsignedInt() }); | | |
| 158 | } else { | | |
| 159 | return file.fail(decl.src(), "TODO inline asm {} args", .{c.val.tag()}); | | |
| 160 | } | | |
| 161 | } else { | 262 | } else { |
| 162 | return file.fail(decl.src(), "TODO non-constant inline asm args", .{}); | 263 | const gop = try ctx.inst_map.getOrPut(arg); |
| | 264 | if (!gop.found_existing) { |
| | 265 | return ctx.file.fail(ctx.decl.src(), "Internal error in C backend: asm argument not found in inst_map", .{}); |
| | 266 | } |
| | 267 | try writer.print("{};\n ", .{gop.entry.value}); |
| 163 | } | 268 | } |
| 164 | } else { | 269 | } else { |
| 165 | return file.fail(decl.src(), "TODO non-explicit inline asm regs", .{}); | 270 | return ctx.file.fail(ctx.decl.src(), "TODO non-explicit inline asm regs", .{}); |
| 166 | } | 271 | } |
| 167 | } | 272 | } |
| 168 | try writer.print("__asm {} (\"{}\"", .{ if (as.is_volatile) @as([]const u8, "volatile") else "", as.asm_source }); | 273 | try writer.print("__asm {} (\"{}\"", .{ if (as.is_volatile) @as([]const u8, "volatile") else "", as.asm_source }); |
| 169 | if (as.output) |o| { | 274 | if (as.output) |o| { |
| 170 | return file.fail(decl.src(), "TODO inline asm output", .{}); | 275 | return ctx.file.fail(ctx.decl.src(), "TODO inline asm output", .{}); |
| 171 | } | 276 | } |
| 172 | if (as.inputs.len > 0) { | 277 | if (as.inputs.len > 0) { |
| 173 | if (as.output == null) { | 278 | if (as.output == null) { |
| ... | @@ -181,12 +286,7 @@ fn genAsm(file: *C, as: *Inst.Assembly, decl: *Decl) !void { | ... | @@ -181,12 +286,7 @@ fn genAsm(file: *C, as: *Inst.Assembly, decl: *Decl) !void { |
| 181 | if (index > 0) { | 286 | if (index > 0) { |
| 182 | try writer.writeAll(", "); | 287 | try writer.writeAll(", "); |
| 183 | } | 288 | } |
| 184 | if (arg.castTag(.constant)) |c| { | 289 | try writer.print("\"\"({}_constant)", .{reg}); |
| 185 | try writer.print("\"\"({}_constant)", .{reg}); | | |
| 186 | } else { | | |
| 187 | // This is blocked by the earlier test | | |
| 188 | unreachable; | | |
| 189 | } | | |
| 190 | } else { | 290 | } else { |
| 191 | // This is blocked by the earlier test | 291 | // This is blocked by the earlier test |
| 192 | unreachable; | 292 | unreachable; |
| ... | @@ -194,4 +294,5 @@ fn genAsm(file: *C, as: *Inst.Assembly, decl: *Decl) !void { | ... | @@ -194,4 +294,5 @@ fn genAsm(file: *C, as: *Inst.Assembly, decl: *Decl) !void { |
| 194 | } | 294 | } |
| 195 | } | 295 | } |
| 196 | try writer.writeAll(");\n"); | 296 | try writer.writeAll(");\n"); |
| | 297 | return null; |
| 197 | } | 298 | } |