| ... | ... | @@ -94,6 +94,28 @@ fn genArray(file: *C, decl: *Decl) !void { |
| 94 | 94 | return file.fail(decl.src(), "TODO non-byte arrays", .{}); |
| 95 | 95 | } |
| 96 | 96 | |
| 97 | const Context = struct { |
| 98 | file: *C, |
| 99 | decl: *Decl, |
| 100 | inst_map: std.AutoHashMap(*Inst, []u8), |
| 101 | argdex: usize = 0, |
| 102 | unnamed_index: usize = 0, |
| 103 | |
| 104 | fn name(self: *Context) ![]u8 { |
| 105 | const val = try std.fmt.allocPrint(self.file.base.allocator, "__temp_{}", .{self.unnamed_index}); |
| 106 | self.unnamed_index += 1; |
| 107 | return val; |
| 108 | } |
| 109 | |
| 110 | fn deinit(self: *Context) void { |
| 111 | for (self.inst_map.items()) |kv| { |
| 112 | self.file.base.allocator.free(kv.value); |
| 113 | } |
| 114 | self.inst_map.deinit(); |
| 115 | self.* = undefined; |
| 116 | } |
| 117 | }; |
| 118 | |
| 97 | 119 | fn genFn(file: *C, decl: *Decl) !void { |
| 98 | 120 | const writer = file.main.writer(); |
| 99 | 121 | const tv = decl.typed_value.most_recent.typed_value; |
| ... | ... | @@ -102,24 +124,31 @@ fn genFn(file: *C, decl: *Decl) !void { |
| 102 | 124 | |
| 103 | 125 | try writer.writeAll(" {"); |
| 104 | 126 | |
| 127 | var ctx = Context{ |
| 128 | .file = file, |
| 129 | .decl = decl, |
| 130 | .inst_map = std.AutoHashMap(*Inst, []u8).init(file.base.allocator), |
| 131 | }; |
| 132 | defer ctx.deinit(); |
| 133 | |
| 105 | 134 | const func: *Module.Fn = tv.val.cast(Value.Payload.Function).?.func; |
| 106 | 135 | const instructions = func.analysis.success.instructions; |
| 107 | | var argdex: usize = 0; |
| 108 | 136 | if (instructions.len > 0) { |
| 109 | 137 | try writer.writeAll("\n"); |
| 110 | 138 | for (instructions) |inst| { |
| 111 | | switch (inst.tag) { |
| 112 | | .assembly => try genAsm(file, inst.castTag(.assembly).?, decl, &argdex), |
| 113 | | .call => try genCall(file, inst.castTag(.call).?, decl), |
| 114 | | .ret => try genRet(file, inst.castTag(.ret).?, decl, tv.ty.fnReturnType()), |
| 115 | | .retvoid => try file.main.writer().print(" return;\n", .{}), |
| 116 | | .arg => {}, |
| 117 | | .dbg_stmt => try genDbgStmt(file, inst.castTag(.dbg_stmt).?, decl), |
| 118 | | .breakpoint => try genBreak(file, inst.castTag(.breakpoint).?, decl), |
| 119 | | .unreach => try genUnreach(file, inst.castTag(.unreach).?, decl), |
| 120 | | // This will be handled correctly later? |
| 121 | | .intcast => {}, |
| 139 | if (switch (inst.tag) { |
| 140 | .assembly => try genAsm(&ctx, inst.castTag(.assembly).?), |
| 141 | .call => try genCall(&ctx, inst.castTag(.call).?), |
| 142 | .ret => try genRet(&ctx, inst.castTag(.ret).?), |
| 143 | .retvoid => try genRetVoid(&ctx), |
| 144 | .arg => try genArg(&ctx), |
| 145 | .dbg_stmt => try genDbgStmt(&ctx, inst.castTag(.dbg_stmt).?), |
| 146 | .breakpoint => try genBreak(&ctx, inst.castTag(.breakpoint).?), |
| 147 | .unreach => try genUnreach(&ctx, inst.castTag(.unreach).?), |
| 148 | .intcast => try genIntCast(&ctx, inst.castTag(.intcast).?), |
| 122 | 149 | else => |e| return file.fail(decl.src(), "TODO implement C codegen for {}", .{e}), |
| 150 | }) |name| { |
| 151 | try ctx.inst_map.putNoClobber(inst, name); |
| 123 | 152 | } |
| 124 | 153 | } |
| 125 | 154 | } |
| ... | ... | @@ -127,27 +156,40 @@ fn genFn(file: *C, decl: *Decl) !void { |
| 127 | 156 | try writer.writeAll("}\n\n"); |
| 128 | 157 | } |
| 129 | 158 | |
| 130 | | fn genRet(file: *C, inst: *Inst.UnOp, decl: *Decl, expected_return_type: Type) !void { |
| 131 | | return file.fail(decl.src(), "TODO return {}", .{expected_return_type}); |
| 159 | fn genArg(ctx: *Context) !?[]u8 { |
| 160 | const name = try std.fmt.allocPrint(ctx.file.base.allocator, "arg{}", .{ctx.argdex}); |
| 161 | ctx.argdex += 1; |
| 162 | return name; |
| 163 | } |
| 164 | |
| 165 | fn genRetVoid(ctx: *Context) !?[]u8 { |
| 166 | try ctx.file.main.writer().print(" return;\n", .{}); |
| 167 | return null; |
| 132 | 168 | } |
| 133 | 169 | |
| 134 | | fn genIntCast(file: *C, inst: *Inst.UnOp, decl: *Decl, argdex: *usize) !void { |
| 170 | fn genRet(ctx: *Context, inst: *Inst.UnOp) !?[]u8 { |
| 171 | return ctx.file.fail(ctx.decl.src(), "TODO return", .{}); |
| 172 | } |
| 173 | |
| 174 | fn genIntCast(ctx: *Context, inst: *Inst.UnOp) !?[]u8 { |
| 175 | if (inst.base.isUnused()) |
| 176 | return null; |
| 135 | 177 | const op = inst.operand; |
| 136 | | const writer = file.main.writer(); |
| 137 | | try writer.writeByte('('); |
| 138 | | try renderType(file, writer, inst.base.ty, decl.src()); |
| 139 | | try writer.writeByte(')'); |
| 140 | | if (op.castTag(.arg)) |_| { |
| 141 | | try writer.print("arg{}", .{argdex.*}); |
| 142 | | argdex.* += 1; |
| 143 | | } else { |
| 144 | | return file.fail(decl.src(), "TODO intcast {} to {}", .{ op, inst.base.ty }); |
| 145 | | } |
| 178 | const writer = ctx.file.main.writer(); |
| 179 | const name = try ctx.name(); |
| 180 | const from = ctx.inst_map.get(op) orelse |
| 181 | return ctx.file.fail(ctx.decl.src(), "Internal error in C backend: intCast argument not found in inst_map", .{}); |
| 182 | try writer.writeAll(" const "); |
| 183 | try renderType(ctx.file, writer, inst.base.ty, ctx.decl.src()); |
| 184 | try writer.print(" {} = (", .{name}); |
| 185 | try renderType(ctx.file, writer, inst.base.ty, ctx.decl.src()); |
| 186 | try writer.print("){};\n", .{from}); |
| 187 | return name; |
| 146 | 188 | } |
| 147 | 189 | |
| 148 | | fn genCall(file: *C, inst: *Inst.Call, decl: *Decl) !void { |
| 149 | | const writer = file.main.writer(); |
| 150 | | const header = file.header.writer(); |
| 190 | fn genCall(ctx: *Context, inst: *Inst.Call) !?[]u8 { |
| 191 | const writer = ctx.file.main.writer(); |
| 192 | const header = ctx.file.header.writer(); |
| 151 | 193 | try writer.writeAll(" "); |
| 152 | 194 | if (inst.func.castTag(.constant)) |func_inst| { |
| 153 | 195 | if (func_inst.val.cast(Value.Payload.Function)) |func_val| { |
| ... | ... | @@ -158,9 +200,9 @@ fn genCall(file: *C, inst: *Inst.Call, decl: *Decl) !void { |
| 158 | 200 | try writer.print("(void)", .{}); |
| 159 | 201 | } |
| 160 | 202 | const tname = mem.spanZ(target.name); |
| 161 | | if (file.called.get(tname) == null) { |
| 162 | | try file.called.put(tname, void{}); |
| 163 | | try renderFunctionSignature(file, header, target); |
| 203 | if (ctx.file.called.get(tname) == null) { |
| 204 | try ctx.file.called.put(tname, void{}); |
| 205 | try renderFunctionSignature(ctx.file, header, target); |
| 164 | 206 | try header.writeAll(";\n"); |
| 165 | 207 | } |
| 166 | 208 | try writer.print("{}(", .{tname}); |
| ... | ... | @@ -170,61 +212,65 @@ fn genCall(file: *C, inst: *Inst.Call, decl: *Decl) !void { |
| 170 | 212 | try writer.writeAll(", "); |
| 171 | 213 | } |
| 172 | 214 | if (arg.cast(Inst.Constant)) |con| { |
| 173 | | try renderValue(file, writer, con.val, decl.src()); |
| 215 | try renderValue(ctx.file, writer, con.val, ctx.decl.src()); |
| 174 | 216 | } else { |
| 175 | | return file.fail(decl.src(), "TODO call pass arg {}", .{arg}); |
| 217 | return ctx.file.fail(ctx.decl.src(), "TODO call pass arg {}", .{arg}); |
| 176 | 218 | } |
| 177 | 219 | } |
| 178 | 220 | } |
| 179 | 221 | try writer.writeAll(");\n"); |
| 180 | 222 | } else { |
| 181 | | return file.fail(decl.src(), "TODO non-function call target?", .{}); |
| 223 | return ctx.file.fail(ctx.decl.src(), "TODO non-function call target?", .{}); |
| 182 | 224 | } |
| 183 | 225 | } else { |
| 184 | | return file.fail(decl.src(), "TODO non-constant call inst?", .{}); |
| 226 | return ctx.file.fail(ctx.decl.src(), "TODO non-constant call inst?", .{}); |
| 185 | 227 | } |
| 228 | return null; |
| 186 | 229 | } |
| 187 | 230 | |
| 188 | | fn genDbgStmt(file: *C, inst: *Inst.NoOp, decl: *Decl) !void { |
| 231 | fn genDbgStmt(ctx: *Context, inst: *Inst.NoOp) !?[]u8 { |
| 189 | 232 | // TODO emit #line directive here with line number and filename |
| 233 | return null; |
| 190 | 234 | } |
| 191 | 235 | |
| 192 | | fn genBreak(file: *C, inst: *Inst.NoOp, decl: *Decl) !void { |
| 236 | fn genBreak(ctx: *Context, inst: *Inst.NoOp) !?[]u8 { |
| 193 | 237 | // TODO ?? |
| 238 | return null; |
| 194 | 239 | } |
| 195 | 240 | |
| 196 | | fn genUnreach(file: *C, inst: *Inst.NoOp, decl: *Decl) !void { |
| 197 | | try file.main.writer().writeAll(" zig_unreachable();\n"); |
| 241 | fn genUnreach(ctx: *Context, inst: *Inst.NoOp) !?[]u8 { |
| 242 | try ctx.file.main.writer().writeAll(" zig_unreachable();\n"); |
| 243 | return null; |
| 198 | 244 | } |
| 199 | 245 | |
| 200 | | fn genAsm(file: *C, as: *Inst.Assembly, decl: *Decl, argdex: *usize) !void { |
| 201 | | const writer = file.main.writer(); |
| 246 | fn genAsm(ctx: *Context, as: *Inst.Assembly) !?[]u8 { |
| 247 | const writer = ctx.file.main.writer(); |
| 202 | 248 | try writer.writeAll(" "); |
| 203 | 249 | for (as.inputs) |i, index| { |
| 204 | 250 | if (i[0] == '{' and i[i.len - 1] == '}') { |
| 205 | 251 | const reg = i[1 .. i.len - 1]; |
| 206 | 252 | const arg = as.args[index]; |
| 207 | 253 | try writer.writeAll("register "); |
| 208 | | try renderType(file, writer, arg.ty, decl.src()); |
| 254 | try renderType(ctx.file, writer, arg.ty, ctx.decl.src()); |
| 209 | 255 | try writer.print(" {}_constant __asm__(\"{}\") = ", .{ reg, reg }); |
| 256 | // TODO merge constant handling into inst_map as well |
| 210 | 257 | if (arg.castTag(.constant)) |c| { |
| 211 | | try renderValue(file, writer, c.val, decl.src()); |
| 212 | | } else if (arg.castTag(.arg)) |inst| { |
| 213 | | try writer.print("arg{}", .{argdex.*}); |
| 214 | | argdex.* += 1; |
| 215 | | } else if (arg.castTag(.intcast)) |inst| { |
| 216 | | try genIntCast(file, inst, decl, argdex); |
| 258 | try renderValue(ctx.file, writer, c.val, ctx.decl.src()); |
| 259 | try writer.writeAll(";\n "); |
| 217 | 260 | } else { |
| 218 | | return file.fail(decl.src(), "TODO non-constant inline asm args ({})", .{arg.tag}); |
| 261 | const gop = try ctx.inst_map.getOrPut(arg); |
| 262 | if (!gop.found_existing) { |
| 263 | return ctx.file.fail(ctx.decl.src(), "Internal error in C backend: asm argument not found in inst_map", .{}); |
| 264 | } |
| 265 | try writer.print("{};\n ", .{gop.entry.value}); |
| 219 | 266 | } |
| 220 | | try writer.writeAll(";\n "); |
| 221 | 267 | } else { |
| 222 | | return file.fail(decl.src(), "TODO non-explicit inline asm regs", .{}); |
| 268 | return ctx.file.fail(ctx.decl.src(), "TODO non-explicit inline asm regs", .{}); |
| 223 | 269 | } |
| 224 | 270 | } |
| 225 | 271 | try writer.print("__asm {} (\"{}\"", .{ if (as.is_volatile) @as([]const u8, "volatile") else "", as.asm_source }); |
| 226 | 272 | if (as.output) |o| { |
| 227 | | return file.fail(decl.src(), "TODO inline asm output", .{}); |
| 273 | return ctx.file.fail(ctx.decl.src(), "TODO inline asm output", .{}); |
| 228 | 274 | } |
| 229 | 275 | if (as.inputs.len > 0) { |
| 230 | 276 | if (as.output == null) { |
| ... | ... | @@ -246,4 +292,5 @@ fn genAsm(file: *C, as: *Inst.Assembly, decl: *Decl, argdex: *usize) !void { |
| 246 | 292 | } |
| 247 | 293 | } |
| 248 | 294 | try writer.writeAll(");\n"); |
| 295 | return null; |
| 249 | 296 | } |