authorgravatar for noam@pixelhero.devNoam Preil <noam@pixelhero.dev> 2020-09-08 14:08:40-04:00
committergravatar for noam@pixelhero.devNoam Preil <noam@pixelhero.dev> 2020-10-06 15:09:56-04:00
logce29fc09470f0db9e7bdc075ebb84fe42146a222
treead2cb732651e31740729768af687bf0ba08e8cab
parent58502b8bfec51f04ad12ec65b0d52452e179256a
signaturelock-open Commit is signed but in an unrecognized format.

Make indentation adjustable (hardcode 4 spaces for now)


1 files changed, 8 insertions(+), 5 deletions(-)

src/codegen/c.zig+8-5
......@@ -11,6 +11,8 @@ const C = link.File.C;
1111const Decl = Module.Decl;
1212const mem = std.mem;
1313
14const indentation = " ";
15
1416/// Maps a name from Zig source to C. Currently, this will always give the same
1517/// output for any given input, sometimes resulting in broken identifiers.
1618fn map(allocator: *std.mem.Allocator, name: []const u8) ![]const u8 {
......@@ -87,6 +89,7 @@ fn genArray(file: *C, decl: *Decl) !void {
8789 if (tv.val.cast(Value.Payload.Bytes)) |payload|
8890 if (tv.ty.sentinel()) |sentinel|
8991 if (sentinel.toUnsignedInt() == 0)
92 // TODO: static by default
9093 try file.constants.writer().print("const char *const {} = \"{}\";\n", .{ name, payload.data })
9194 else
9295 return file.fail(decl.src(), "TODO byte arrays with non-zero sentinels", .{})
......@@ -166,7 +169,7 @@ fn genArg(ctx: *Context) !?[]u8 {
166169}
167170
168171fn genRetVoid(ctx: *Context) !?[]u8 {
169 try ctx.file.main.writer().print(" return;\n", .{});
172 try ctx.file.main.writer().print(indentation ++ "return;\n", .{});
170173 return null;
171174}
172175
......@@ -182,7 +185,7 @@ fn genIntCast(ctx: *Context, inst: *Inst.UnOp) !?[]u8 {
182185 const name = try ctx.name();
183186 const from = ctx.inst_map.get(op) orelse
184187 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 ");
186189 try renderType(ctx, writer, inst.base.ty);
187190 try writer.print(" {} = (", .{name});
188191 try renderType(ctx, writer, inst.base.ty);
......@@ -193,7 +196,7 @@ fn genIntCast(ctx: *Context, inst: *Inst.UnOp) !?[]u8 {
193196fn genCall(ctx: *Context, inst: *Inst.Call) !?[]u8 {
194197 const writer = ctx.file.main.writer();
195198 const header = ctx.file.header.writer();
196 try writer.writeAll(" ");
199 try writer.writeAll(indentation);
197200 if (inst.func.castTag(.constant)) |func_inst| {
198201 if (func_inst.val.cast(Value.Payload.Function)) |func_val| {
199202 const target = func_val.func.owner_decl;
......@@ -242,13 +245,13 @@ fn genBreak(ctx: *Context, inst: *Inst.NoOp) !?[]u8 {
242245}
243246
244247fn 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");
246249 return null;
247250}
248251
249252fn genAsm(ctx: *Context, as: *Inst.Assembly) !?[]u8 {
250253 const writer = ctx.file.main.writer();
251 try writer.writeAll(" ");
254 try writer.writeAll(indentation);
252255 for (as.inputs) |i, index| {
253256 if (i[0] == '{' and i[i.len - 1] == '}') {
254257 const reg = i[1 .. i.len - 1];