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