| ... | ... | @@ -11,6 +11,8 @@ const C = link.File.C; |
| 11 | 11 | const Decl = Module.Decl; |
| 12 | 12 | const mem = std.mem; |
| 13 | 13 | |
| 14 | const indentation = " "; |
| 15 | |
| 14 | 16 | /// Maps a name from Zig source to C. Currently, this will always give the same |
| 15 | 17 | /// output for any given input, sometimes resulting in broken identifiers. |
| 16 | 18 | fn map(allocator: *std.mem.Allocator, name: []const u8) ![]const u8 { |
| ... | ... | @@ -87,6 +89,7 @@ fn genArray(file: *C, decl: *Decl) !void { |
| 87 | 89 | if (tv.val.cast(Value.Payload.Bytes)) |payload| |
| 88 | 90 | if (tv.ty.sentinel()) |sentinel| |
| 89 | 91 | if (sentinel.toUnsignedInt() == 0) |
| 92 | // TODO: static by default |
| 90 | 93 | try file.constants.writer().print("const char *const {} = \"{}\";\n", .{ name, payload.data }) |
| 91 | 94 | else |
| 92 | 95 | return file.fail(decl.src(), "TODO byte arrays with non-zero sentinels", .{}) |
| ... | ... | @@ -166,7 +169,7 @@ fn genArg(ctx: *Context) !?[]u8 { |
| 166 | 169 | } |
| 167 | 170 | |
| 168 | 171 | fn genRetVoid(ctx: *Context) !?[]u8 { |
| 169 | | try ctx.file.main.writer().print(" return;\n", .{}); |
| 172 | try ctx.file.main.writer().print(indentation ++ "return;\n", .{}); |
| 170 | 173 | return null; |
| 171 | 174 | } |
| 172 | 175 | |
| ... | ... | @@ -182,7 +185,7 @@ fn genIntCast(ctx: *Context, inst: *Inst.UnOp) !?[]u8 { |
| 182 | 185 | const name = try ctx.name(); |
| 183 | 186 | const from = ctx.inst_map.get(op) orelse |
| 184 | 187 | return ctx.file.fail(ctx.decl.src(), "Internal error in C backend: intCast argument not found in inst_map", .{}); |
| 185 | | try writer.writeAll(" const "); |
| 188 | try writer.writeAll(indentation ++ "const "); |
| 186 | 189 | try renderType(ctx, writer, inst.base.ty); |
| 187 | 190 | try writer.print(" {} = (", .{name}); |
| 188 | 191 | try renderType(ctx, writer, inst.base.ty); |
| ... | ... | @@ -193,7 +196,7 @@ fn genIntCast(ctx: *Context, inst: *Inst.UnOp) !?[]u8 { |
| 193 | 196 | fn genCall(ctx: *Context, inst: *Inst.Call) !?[]u8 { |
| 194 | 197 | const writer = ctx.file.main.writer(); |
| 195 | 198 | const header = ctx.file.header.writer(); |
| 196 | | try writer.writeAll(" "); |
| 199 | try writer.writeAll(indentation); |
| 197 | 200 | if (inst.func.castTag(.constant)) |func_inst| { |
| 198 | 201 | if (func_inst.val.cast(Value.Payload.Function)) |func_val| { |
| 199 | 202 | const target = func_val.func.owner_decl; |
| ... | ... | @@ -242,13 +245,13 @@ fn genBreak(ctx: *Context, inst: *Inst.NoOp) !?[]u8 { |
| 242 | 245 | } |
| 243 | 246 | |
| 244 | 247 | fn genUnreach(ctx: *Context, inst: *Inst.NoOp) !?[]u8 { |
| 245 | | try ctx.file.main.writer().writeAll(" zig_unreachable();\n"); |
| 248 | try ctx.file.main.writer().writeAll(indentation ++ "zig_unreachable();\n"); |
| 246 | 249 | return null; |
| 247 | 250 | } |
| 248 | 251 | |
| 249 | 252 | fn genAsm(ctx: *Context, as: *Inst.Assembly) !?[]u8 { |
| 250 | 253 | const writer = ctx.file.main.writer(); |
| 251 | | try writer.writeAll(" "); |
| 254 | try writer.writeAll(indentation); |
| 252 | 255 | for (as.inputs) |i, index| { |
| 253 | 256 | if (i[0] == '{' and i[i.len - 1] == '}') { |
| 254 | 257 | const reg = i[1 .. i.len - 1]; |