| ... | ... | @@ -12,7 +12,7 @@ const C = link.File.C; |
| 12 | 12 | const Decl = Module.Decl; |
| 13 | 13 | const mem = std.mem; |
| 14 | 14 | |
| 15 | | const indentation = " "; |
| 15 | const Writer = std.ArrayList(u8).Writer; |
| 16 | 16 | |
| 17 | 17 | /// Maps a name from Zig source to C. Currently, this will always give the same |
| 18 | 18 | /// output for any given input, sometimes resulting in broken identifiers. |
| ... | ... | @@ -20,43 +20,145 @@ fn map(allocator: *std.mem.Allocator, name: []const u8) ![]const u8 { |
| 20 | 20 | return allocator.dupe(u8, name); |
| 21 | 21 | } |
| 22 | 22 | |
| 23 | | fn renderType(ctx: *Context, header: *C.Header, writer: std.ArrayList(u8).Writer, T: Type) !void { |
| 24 | | switch (T.zigTypeTag()) { |
| 23 | fn renderType( |
| 24 | ctx: *Context, |
| 25 | header: *C.Header, |
| 26 | writer: Writer, |
| 27 | t: Type, |
| 28 | ) error{ OutOfMemory, AnalysisFail }!void { |
| 29 | switch (t.zigTypeTag()) { |
| 25 | 30 | .NoReturn => { |
| 26 | 31 | try writer.writeAll("zig_noreturn void"); |
| 27 | 32 | }, |
| 28 | 33 | .Void => try writer.writeAll("void"), |
| 29 | 34 | .Bool => try writer.writeAll("bool"), |
| 30 | 35 | .Int => { |
| 31 | | if (T.tag() == .u8) { |
| 32 | | header.need_stdint = true; |
| 33 | | try writer.writeAll("uint8_t"); |
| 34 | | } else if (T.tag() == .u32) { |
| 35 | | header.need_stdint = true; |
| 36 | | try writer.writeAll("uint32_t"); |
| 37 | | } else if (T.tag() == .usize) { |
| 38 | | header.need_stddef = true; |
| 39 | | try writer.writeAll("size_t"); |
| 36 | switch (t.tag()) { |
| 37 | .u8 => try writer.writeAll("uint8_t"), |
| 38 | .i8 => try writer.writeAll("int8_t"), |
| 39 | .u16 => try writer.writeAll("uint16_t"), |
| 40 | .i16 => try writer.writeAll("int16_t"), |
| 41 | .u32 => try writer.writeAll("uint32_t"), |
| 42 | .i32 => try writer.writeAll("int32_t"), |
| 43 | .u64 => try writer.writeAll("uint64_t"), |
| 44 | .i64 => try writer.writeAll("int64_t"), |
| 45 | .usize => try writer.writeAll("uintptr_t"), |
| 46 | .isize => try writer.writeAll("intptr_t"), |
| 47 | .c_short => try writer.writeAll("short"), |
| 48 | .c_ushort => try writer.writeAll("unsigned short"), |
| 49 | .c_int => try writer.writeAll("int"), |
| 50 | .c_uint => try writer.writeAll("unsigned int"), |
| 51 | .c_long => try writer.writeAll("long"), |
| 52 | .c_ulong => try writer.writeAll("unsigned long"), |
| 53 | .c_longlong => try writer.writeAll("long long"), |
| 54 | .c_ulonglong => try writer.writeAll("unsigned long long"), |
| 55 | .int_signed, .int_unsigned => { |
| 56 | const info = t.intInfo(ctx.target); |
| 57 | const sign_prefix = switch (info.signedness) { |
| 58 | .signed => "i", |
| 59 | .unsigned => "", |
| 60 | }; |
| 61 | inline for (.{ 8, 16, 32, 64, 128 }) |nbits| { |
| 62 | if (info.bits <= nbits) { |
| 63 | try writer.print("{s}int{d}_t", .{ sign_prefix, nbits }); |
| 64 | break; |
| 65 | } |
| 66 | } else { |
| 67 | return ctx.fail(ctx.decl.src(), "TODO: C backend: implement integer types larger than 128 bits", .{}); |
| 68 | } |
| 69 | }, |
| 70 | else => unreachable, |
| 71 | } |
| 72 | }, |
| 73 | .Pointer => { |
| 74 | if (t.isSlice()) { |
| 75 | return ctx.fail(ctx.decl.src(), "TODO: C backend: implement slices", .{}); |
| 40 | 76 | } else { |
| 41 | | return ctx.fail(ctx.decl.src(), "TODO implement int type {}", .{T}); |
| 77 | if (t.isConstPtr()) { |
| 78 | try writer.writeAll("const "); |
| 79 | } |
| 80 | if (t.isVolatilePtr()) { |
| 81 | try writer.writeAll("volatile "); |
| 82 | } |
| 83 | try renderType(ctx, header, writer, t.elemType()); |
| 84 | try writer.writeAll(" *"); |
| 42 | 85 | } |
| 43 | 86 | }, |
| 44 | | else => |e| return ctx.fail(ctx.decl.src(), "TODO implement type {}", .{e}), |
| 87 | .Array => { |
| 88 | try renderType(ctx, header, writer, t.elemType()); |
| 89 | const sentinel_bit = @boolToInt(t.sentinel() != null); |
| 90 | const c_len = t.arrayLen() + sentinel_bit; |
| 91 | try writer.print("[{d}]", .{c_len}); |
| 92 | }, |
| 93 | else => |e| return ctx.fail(ctx.decl.src(), "TODO: C backend: implement type {s}", .{ |
| 94 | @tagName(e), |
| 95 | }), |
| 45 | 96 | } |
| 46 | 97 | } |
| 47 | 98 | |
| 48 | | fn renderValue(ctx: *Context, writer: std.ArrayList(u8).Writer, T: Type, val: Value) !void { |
| 49 | | switch (T.zigTypeTag()) { |
| 99 | fn renderValue( |
| 100 | ctx: *Context, |
| 101 | writer: Writer, |
| 102 | t: Type, |
| 103 | val: Value, |
| 104 | ) error{ OutOfMemory, AnalysisFail }!void { |
| 105 | switch (t.zigTypeTag()) { |
| 50 | 106 | .Int => { |
| 51 | | if (T.isSignedInt()) |
| 52 | | return writer.print("{}", .{val.toSignedInt()}); |
| 53 | | return writer.print("{}", .{val.toUnsignedInt()}); |
| 107 | if (t.isSignedInt()) |
| 108 | return writer.print("{d}", .{val.toSignedInt()}); |
| 109 | return writer.print("{d}", .{val.toUnsignedInt()}); |
| 110 | }, |
| 111 | .Pointer => switch (val.tag()) { |
| 112 | .undef, .zero => try writer.writeAll("0"), |
| 113 | .one => try writer.writeAll("1"), |
| 114 | .decl_ref => { |
| 115 | const decl_ref_payload = val.cast(Value.Payload.DeclRef).?; |
| 116 | try writer.print("&{s}", .{decl_ref_payload.decl.name}); |
| 117 | }, |
| 118 | .function => { |
| 119 | const payload = val.cast(Value.Payload.Function).?; |
| 120 | try writer.print("{s}", .{payload.func.owner_decl.name}); |
| 121 | }, |
| 122 | .extern_fn => { |
| 123 | const payload = val.cast(Value.Payload.ExternFn).?; |
| 124 | try writer.print("{s}", .{payload.decl.name}); |
| 125 | }, |
| 126 | else => |e| return ctx.fail( |
| 127 | ctx.decl.src(), |
| 128 | "TODO: C backend: implement Pointer value {s}", |
| 129 | .{@tagName(e)}, |
| 130 | ), |
| 131 | }, |
| 132 | .Array => { |
| 133 | // TODO first try specific tag representations for more efficiency |
| 134 | // Fall back to inefficient generic implementation. |
| 135 | try writer.writeAll("{"); |
| 136 | var index: usize = 0; |
| 137 | const len = t.arrayLen(); |
| 138 | const elem_ty = t.elemType(); |
| 139 | while (index < len) : (index += 1) { |
| 140 | if (index != 0) try writer.writeAll(","); |
| 141 | const elem_val = try val.elemValue(&ctx.arena.allocator, index); |
| 142 | try renderValue(ctx, writer, elem_ty, elem_val); |
| 143 | } |
| 144 | if (t.sentinel()) |sentinel_val| { |
| 145 | if (index != 0) try writer.writeAll(","); |
| 146 | try renderValue(ctx, writer, elem_ty, sentinel_val); |
| 147 | } |
| 148 | try writer.writeAll("}"); |
| 54 | 149 | }, |
| 55 | | else => |e| return ctx.fail(ctx.decl.src(), "TODO implement value {}", .{e}), |
| 150 | else => |e| return ctx.fail(ctx.decl.src(), "TODO: C backend: implement value {s}", .{ |
| 151 | @tagName(e), |
| 152 | }), |
| 56 | 153 | } |
| 57 | 154 | } |
| 58 | 155 | |
| 59 | | fn renderFunctionSignature(ctx: *Context, header: *C.Header, writer: std.ArrayList(u8).Writer, decl: *Decl) !void { |
| 156 | fn renderFunctionSignature( |
| 157 | ctx: *Context, |
| 158 | header: *C.Header, |
| 159 | writer: Writer, |
| 160 | decl: *Decl, |
| 161 | ) !void { |
| 60 | 162 | const tv = decl.typed_value.most_recent.typed_value; |
| 61 | 163 | try renderType(ctx, header, writer, tv.ty.fnReturnType()); |
| 62 | 164 | // Use the child allocator directly, as we know the name can be freed before |
| ... | ... | @@ -81,27 +183,92 @@ fn renderFunctionSignature(ctx: *Context, header: *C.Header, writer: std.ArrayLi |
| 81 | 183 | } |
| 82 | 184 | |
| 83 | 185 | pub fn generate(file: *C, decl: *Decl) !void { |
| 84 | | switch (decl.typed_value.most_recent.typed_value.ty.zigTypeTag()) { |
| 85 | | .Fn => try genFn(file, decl), |
| 86 | | .Array => try genArray(file, decl), |
| 87 | | else => |e| return file.fail(decl.src(), "TODO {}", .{e}), |
| 186 | const tv = decl.typed_value.most_recent.typed_value; |
| 187 | |
| 188 | var arena = std.heap.ArenaAllocator.init(file.base.allocator); |
| 189 | defer arena.deinit(); |
| 190 | var inst_map = std.AutoHashMap(*Inst, []u8).init(&arena.allocator); |
| 191 | defer inst_map.deinit(); |
| 192 | var ctx = Context{ |
| 193 | .decl = decl, |
| 194 | .arena = &arena, |
| 195 | .inst_map = &inst_map, |
| 196 | .target = file.base.options.target, |
| 197 | }; |
| 198 | defer { |
| 199 | file.error_msg = ctx.error_msg; |
| 200 | ctx.deinit(); |
| 201 | } |
| 202 | |
| 203 | if (tv.val.cast(Value.Payload.Function)) |func_payload| { |
| 204 | const writer = file.main.writer(); |
| 205 | try renderFunctionSignature(&ctx, &file.header, writer, decl); |
| 206 | |
| 207 | try writer.writeAll(" {"); |
| 208 | |
| 209 | const func: *Module.Fn = func_payload.func; |
| 210 | const instructions = func.analysis.success.instructions; |
| 211 | if (instructions.len > 0) { |
| 212 | try writer.writeAll("\n"); |
| 213 | for (instructions) |inst| { |
| 214 | const indent_size = 4; |
| 215 | const indent_level = 1; |
| 216 | try writer.writeByteNTimes(' ', indent_size * indent_level); |
| 217 | if (switch (inst.tag) { |
| 218 | .assembly => try genAsm(&ctx, file, inst.castTag(.assembly).?), |
| 219 | .call => try genCall(&ctx, file, inst.castTag(.call).?), |
| 220 | .add => try genBinOp(&ctx, file, inst.cast(Inst.BinOp).?, "+"), |
| 221 | .sub => try genBinOp(&ctx, file, inst.cast(Inst.BinOp).?, "-"), |
| 222 | .ret => try genRet(&ctx, file, inst.castTag(.ret).?), |
| 223 | .retvoid => try genRetVoid(file), |
| 224 | .arg => try genArg(&ctx), |
| 225 | .dbg_stmt => try genDbgStmt(&ctx, inst.castTag(.dbg_stmt).?), |
| 226 | .breakpoint => try genBreak(&ctx, inst.castTag(.breakpoint).?), |
| 227 | .unreach => try genUnreach(file, inst.castTag(.unreach).?), |
| 228 | .intcast => try genIntCast(&ctx, file, inst.castTag(.intcast).?), |
| 229 | else => |e| return ctx.fail(decl.src(), "TODO implement C codegen for {}", .{e}), |
| 230 | }) |name| { |
| 231 | try ctx.inst_map.putNoClobber(inst, name); |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | try writer.writeAll("}\n\n"); |
| 237 | } else if (tv.val.tag() == .extern_fn) { |
| 238 | return; // handled when referenced |
| 239 | } else { |
| 240 | const writer = file.constants.writer(); |
| 241 | try writer.writeAll("static "); |
| 242 | |
| 243 | // TODO ask the Decl if it is const |
| 244 | // https://github.com/ziglang/zig/issues/7582 |
| 245 | |
| 246 | try renderType(&ctx, &file.header, writer, tv.ty); |
| 247 | try writer.print(" {s} = ", .{decl.name}); |
| 248 | try renderValue(&ctx, writer, tv.ty, tv.val); |
| 249 | try writer.writeAll(";\n"); |
| 88 | 250 | } |
| 89 | 251 | } |
| 90 | 252 | |
| 91 | 253 | pub fn generateHeader( |
| 92 | | arena: *std.heap.ArenaAllocator, |
| 254 | comp: *Compilation, |
| 93 | 255 | module: *Module, |
| 94 | 256 | header: *C.Header, |
| 95 | 257 | decl: *Decl, |
| 96 | 258 | ) error{ AnalysisFail, OutOfMemory }!void { |
| 97 | 259 | switch (decl.typed_value.most_recent.typed_value.ty.zigTypeTag()) { |
| 98 | 260 | .Fn => { |
| 99 | | var inst_map = std.AutoHashMap(*Inst, []u8).init(&arena.allocator); |
| 261 | var inst_map = std.AutoHashMap(*Inst, []u8).init(comp.gpa); |
| 100 | 262 | defer inst_map.deinit(); |
| 263 | |
| 264 | var arena = std.heap.ArenaAllocator.init(comp.gpa); |
| 265 | defer arena.deinit(); |
| 266 | |
| 101 | 267 | var ctx = Context{ |
| 102 | 268 | .decl = decl, |
| 103 | | .arena = arena, |
| 269 | .arena = &arena, |
| 104 | 270 | .inst_map = &inst_map, |
| 271 | .target = comp.getTarget(), |
| 105 | 272 | }; |
| 106 | 273 | const writer = header.buf.writer(); |
| 107 | 274 | renderFunctionSignature(&ctx, header, writer, decl) catch |err| { |
| ... | ... | @@ -116,24 +283,6 @@ pub fn generateHeader( |
| 116 | 283 | } |
| 117 | 284 | } |
| 118 | 285 | |
| 119 | | fn genArray(file: *C, decl: *Decl) !void { |
| 120 | | const tv = decl.typed_value.most_recent.typed_value; |
| 121 | | // TODO: prevent inline asm constants from being emitted |
| 122 | | const name = try map(file.base.allocator, mem.span(decl.name)); |
| 123 | | defer file.base.allocator.free(name); |
| 124 | | if (tv.val.cast(Value.Payload.Bytes)) |payload| |
| 125 | | if (tv.ty.sentinel()) |sentinel| |
| 126 | | if (sentinel.toUnsignedInt() == 0) |
| 127 | | // TODO: static by default |
| 128 | | try file.constants.writer().print("const char *const {} = \"{}\";\n", .{ name, payload.data }) |
| 129 | | else |
| 130 | | return file.fail(decl.src(), "TODO byte arrays with non-zero sentinels", .{}) |
| 131 | | else |
| 132 | | return file.fail(decl.src(), "TODO byte arrays without sentinels", .{}) |
| 133 | | else |
| 134 | | return file.fail(decl.src(), "TODO non-byte arrays", .{}); |
| 135 | | } |
| 136 | | |
| 137 | 286 | const Context = struct { |
| 138 | 287 | decl: *Decl, |
| 139 | 288 | inst_map: *std.AutoHashMap(*Inst, []u8), |
| ... | ... | @@ -141,6 +290,7 @@ const Context = struct { |
| 141 | 290 | argdex: usize = 0, |
| 142 | 291 | unnamed_index: usize = 0, |
| 143 | 292 | error_msg: *Compilation.ErrorMsg = undefined, |
| 293 | target: std.Target, |
| 144 | 294 | |
| 145 | 295 | fn resolveInst(self: *Context, inst: *Inst) ![]u8 { |
| 146 | 296 | if (inst.cast(Inst.Constant)) |const_inst| { |
| ... | ... | @@ -170,55 +320,6 @@ const Context = struct { |
| 170 | 320 | } |
| 171 | 321 | }; |
| 172 | 322 | |
| 173 | | fn genFn(file: *C, decl: *Decl) !void { |
| 174 | | const writer = file.main.writer(); |
| 175 | | const tv = decl.typed_value.most_recent.typed_value; |
| 176 | | |
| 177 | | var arena = std.heap.ArenaAllocator.init(file.base.allocator); |
| 178 | | defer arena.deinit(); |
| 179 | | var inst_map = std.AutoHashMap(*Inst, []u8).init(&arena.allocator); |
| 180 | | defer inst_map.deinit(); |
| 181 | | var ctx = Context{ |
| 182 | | .decl = decl, |
| 183 | | .arena = &arena, |
| 184 | | .inst_map = &inst_map, |
| 185 | | }; |
| 186 | | defer { |
| 187 | | file.error_msg = ctx.error_msg; |
| 188 | | ctx.deinit(); |
| 189 | | } |
| 190 | | |
| 191 | | try renderFunctionSignature(&ctx, &file.header, writer, decl); |
| 192 | | |
| 193 | | try writer.writeAll(" {"); |
| 194 | | |
| 195 | | const func: *Module.Fn = tv.val.cast(Value.Payload.Function).?.func; |
| 196 | | const instructions = func.analysis.success.instructions; |
| 197 | | if (instructions.len > 0) { |
| 198 | | try writer.writeAll("\n"); |
| 199 | | for (instructions) |inst| { |
| 200 | | if (switch (inst.tag) { |
| 201 | | .assembly => try genAsm(&ctx, file, inst.castTag(.assembly).?), |
| 202 | | .call => try genCall(&ctx, file, inst.castTag(.call).?), |
| 203 | | .add => try genBinOp(&ctx, file, inst.cast(Inst.BinOp).?, "+"), |
| 204 | | .sub => try genBinOp(&ctx, file, inst.cast(Inst.BinOp).?, "-"), |
| 205 | | .ret => try genRet(&ctx, inst.castTag(.ret).?), |
| 206 | | .retvoid => try genRetVoid(file), |
| 207 | | .arg => try genArg(&ctx), |
| 208 | | .dbg_stmt => try genDbgStmt(&ctx, inst.castTag(.dbg_stmt).?), |
| 209 | | .breakpoint => try genBreak(&ctx, inst.castTag(.breakpoint).?), |
| 210 | | .unreach => try genUnreach(file, inst.castTag(.unreach).?), |
| 211 | | .intcast => try genIntCast(&ctx, file, inst.castTag(.intcast).?), |
| 212 | | else => |e| return ctx.fail(decl.src(), "TODO implement C codegen for {}", .{e}), |
| 213 | | }) |name| { |
| 214 | | try ctx.inst_map.putNoClobber(inst, name); |
| 215 | | } |
| 216 | | } |
| 217 | | } |
| 218 | | |
| 219 | | try writer.writeAll("}\n\n"); |
| 220 | | } |
| 221 | | |
| 222 | 323 | fn genArg(ctx: *Context) !?[]u8 { |
| 223 | 324 | const name = try std.fmt.allocPrint(&ctx.arena.allocator, "arg{}", .{ctx.argdex}); |
| 224 | 325 | ctx.argdex += 1; |
| ... | ... | @@ -226,12 +327,24 @@ fn genArg(ctx: *Context) !?[]u8 { |
| 226 | 327 | } |
| 227 | 328 | |
| 228 | 329 | fn genRetVoid(file: *C) !?[]u8 { |
| 229 | | try file.main.writer().print(indentation ++ "return;\n", .{}); |
| 330 | try file.main.writer().print("return;\n", .{}); |
| 230 | 331 | return null; |
| 231 | 332 | } |
| 232 | 333 | |
| 233 | | fn genRet(ctx: *Context, inst: *Inst.UnOp) !?[]u8 { |
| 234 | | return ctx.fail(ctx.decl.src(), "TODO return", .{}); |
| 334 | fn genRet(ctx: *Context, file: *C, inst: *Inst.UnOp) !?[]u8 { |
| 335 | const writer = file.main.writer(); |
| 336 | try writer.writeAll("return "); |
| 337 | try genValue(ctx, writer, inst.operand); |
| 338 | try writer.writeAll(";\n"); |
| 339 | return null; |
| 340 | } |
| 341 | |
| 342 | fn genValue(ctx: *Context, writer: Writer, inst: *Inst) !void { |
| 343 | if (inst.value()) |val| { |
| 344 | try renderValue(ctx, writer, inst.ty, val); |
| 345 | return; |
| 346 | } |
| 347 | return ctx.fail(ctx.decl.src(), "TODO: C backend: genValue for non-constant value", .{}); |
| 235 | 348 | } |
| 236 | 349 | |
| 237 | 350 | fn genIntCast(ctx: *Context, file: *C, inst: *Inst.UnOp) !?[]u8 { |
| ... | ... | @@ -241,7 +354,7 @@ fn genIntCast(ctx: *Context, file: *C, inst: *Inst.UnOp) !?[]u8 { |
| 241 | 354 | const writer = file.main.writer(); |
| 242 | 355 | const name = try ctx.name(); |
| 243 | 356 | const from = try ctx.resolveInst(inst.operand); |
| 244 | | try writer.writeAll(indentation ++ "const "); |
| 357 | try writer.writeAll("const "); |
| 245 | 358 | try renderType(ctx, &file.header, writer, inst.base.ty); |
| 246 | 359 | try writer.print(" {} = (", .{name}); |
| 247 | 360 | try renderType(ctx, &file.header, writer, inst.base.ty); |
| ... | ... | @@ -256,7 +369,7 @@ fn genBinOp(ctx: *Context, file: *C, inst: *Inst.BinOp, comptime operator: []con |
| 256 | 369 | const rhs = ctx.resolveInst(inst.rhs); |
| 257 | 370 | const writer = file.main.writer(); |
| 258 | 371 | const name = try ctx.name(); |
| 259 | | try writer.writeAll(indentation ++ "const "); |
| 372 | try writer.writeAll("const "); |
| 260 | 373 | try renderType(ctx, &file.header, writer, inst.base.ty); |
| 261 | 374 | try writer.print(" {} = {} " ++ operator ++ " {};\n", .{ name, lhs, rhs }); |
| 262 | 375 | return name; |
| ... | ... | @@ -265,41 +378,42 @@ fn genBinOp(ctx: *Context, file: *C, inst: *Inst.BinOp, comptime operator: []con |
| 265 | 378 | fn genCall(ctx: *Context, file: *C, inst: *Inst.Call) !?[]u8 { |
| 266 | 379 | const writer = file.main.writer(); |
| 267 | 380 | const header = file.header.buf.writer(); |
| 268 | | try writer.writeAll(indentation); |
| 269 | 381 | if (inst.func.castTag(.constant)) |func_inst| { |
| 270 | | if (func_inst.val.cast(Value.Payload.Function)) |func_val| { |
| 271 | | const target = func_val.func.owner_decl; |
| 272 | | const target_ty = target.typed_value.most_recent.typed_value.ty; |
| 273 | | const ret_ty = target_ty.fnReturnType().tag(); |
| 274 | | if (target_ty.fnReturnType().hasCodeGenBits() and inst.base.isUnused()) { |
| 275 | | try writer.print("(void)", .{}); |
| 276 | | } |
| 277 | | const tname = mem.spanZ(target.name); |
| 278 | | if (file.called.get(tname) == null) { |
| 279 | | try file.called.put(tname, void{}); |
| 280 | | try renderFunctionSignature(ctx, &file.header, header, target); |
| 281 | | try header.writeAll(";\n"); |
| 282 | | } |
| 283 | | try writer.print("{}(", .{tname}); |
| 284 | | if (inst.args.len != 0) { |
| 285 | | for (inst.args) |arg, i| { |
| 286 | | if (i > 0) { |
| 287 | | try writer.writeAll(", "); |
| 288 | | } |
| 289 | | if (arg.cast(Inst.Constant)) |con| { |
| 290 | | try renderValue(ctx, writer, arg.ty, con.val); |
| 291 | | } else { |
| 292 | | const val = try ctx.resolveInst(arg); |
| 293 | | try writer.print("{}", .{val}); |
| 294 | | } |
| 382 | const fn_decl = if (func_inst.val.cast(Value.Payload.ExternFn)) |extern_fn| |
| 383 | extern_fn.decl |
| 384 | else if (func_inst.val.cast(Value.Payload.Function)) |func_val| |
| 385 | func_val.func.owner_decl |
| 386 | else |
| 387 | unreachable; |
| 388 | |
| 389 | const fn_ty = fn_decl.typed_value.most_recent.typed_value.ty; |
| 390 | const ret_ty = fn_ty.fnReturnType().tag(); |
| 391 | if (fn_ty.fnReturnType().hasCodeGenBits() and inst.base.isUnused()) { |
| 392 | try writer.print("(void)", .{}); |
| 393 | } |
| 394 | const fn_name = mem.spanZ(fn_decl.name); |
| 395 | if (file.called.get(fn_name) == null) { |
| 396 | try file.called.put(fn_name, void{}); |
| 397 | try renderFunctionSignature(ctx, &file.header, header, fn_decl); |
| 398 | try header.writeAll(";\n"); |
| 399 | } |
| 400 | try writer.print("{s}(", .{fn_name}); |
| 401 | if (inst.args.len != 0) { |
| 402 | for (inst.args) |arg, i| { |
| 403 | if (i > 0) { |
| 404 | try writer.writeAll(", "); |
| 405 | } |
| 406 | if (arg.cast(Inst.Constant)) |con| { |
| 407 | try renderValue(ctx, writer, arg.ty, con.val); |
| 408 | } else { |
| 409 | const val = try ctx.resolveInst(arg); |
| 410 | try writer.print("{}", .{val}); |
| 295 | 411 | } |
| 296 | 412 | } |
| 297 | | try writer.writeAll(");\n"); |
| 298 | | } else { |
| 299 | | return ctx.fail(ctx.decl.src(), "TODO non-function call target?", .{}); |
| 300 | 413 | } |
| 414 | try writer.writeAll(");\n"); |
| 301 | 415 | } else { |
| 302 | | return ctx.fail(ctx.decl.src(), "TODO non-constant call inst?", .{}); |
| 416 | return ctx.fail(ctx.decl.src(), "TODO: C backend: implement function pointers", .{}); |
| 303 | 417 | } |
| 304 | 418 | return null; |
| 305 | 419 | } |
| ... | ... | @@ -315,13 +429,12 @@ fn genBreak(ctx: *Context, inst: *Inst.NoOp) !?[]u8 { |
| 315 | 429 | } |
| 316 | 430 | |
| 317 | 431 | fn genUnreach(file: *C, inst: *Inst.NoOp) !?[]u8 { |
| 318 | | try file.main.writer().writeAll(indentation ++ "zig_unreachable();\n"); |
| 432 | try file.main.writer().writeAll("zig_unreachable();\n"); |
| 319 | 433 | return null; |
| 320 | 434 | } |
| 321 | 435 | |
| 322 | 436 | fn genAsm(ctx: *Context, file: *C, as: *Inst.Assembly) !?[]u8 { |
| 323 | 437 | const writer = file.main.writer(); |
| 324 | | try writer.writeAll(indentation); |
| 325 | 438 | for (as.inputs) |i, index| { |
| 326 | 439 | if (i[0] == '{' and i[i.len - 1] == '}') { |
| 327 | 440 | const reg = i[1 .. i.len - 1]; |