authorgravatar for noam@pixelhero.devNoam Preil <noam@pixelhero.dev> 2020-09-08 14:41:51-04:00
committergravatar for noam@pixelhero.devNoam Preil <noam@pixelhero.dev> 2020-10-06 15:09:57-04:00
loge06ba9e86e53797d90d74e87724b897858fe2d77
treeb793af9a3e3da291e883db125e37ec17ecc5da85
parent9ef6c0a035820b586cc2d3a051eef4ae21ec244b
signaturelock-open Commit is signed but in an unrecognized format.

CBE: properly resolve Insts


1 files changed, 15 insertions(+), 3 deletions(-)

src/codegen/c.zig+15-3
......@@ -109,6 +109,18 @@ const Context = struct {
109109 argdex: usize = 0,
110110 unnamed_index: usize = 0,
111111
112 fn resolveInst(self: *Context, inst: *Inst) ![]u8 {
113 if (inst.cast(Inst.Constant)) |const_inst| {
114 var out = std.ArrayList(u8).init(&self.arena.allocator);
115 try renderValue(self, out.writer(), inst.ty, const_inst.val);
116 return out.toOwnedSlice();
117 }
118 if (self.inst_map.get(inst)) |val| {
119 return val;
120 }
121 return self.file.fail(inst.src, "Internal error: failed to resolve inst!", .{});
122 }
123
112124 fn name(self: *Context) ![]u8 {
113125 const val = try std.fmt.allocPrint(&self.arena.allocator, "__temp_{}", .{self.unnamed_index});
114126 self.unnamed_index += 1;
......@@ -186,8 +198,7 @@ fn genIntCast(ctx: *Context, inst: *Inst.UnOp) !?[]u8 {
186198 const op = inst.operand;
187199 const writer = ctx.file.main.writer();
188200 const name = try ctx.name();
189 const from = ctx.inst_map.get(op) orelse
190 return ctx.file.fail(ctx.decl.src(), "Internal error in C backend: intCast argument not found in inst_map", .{});
201 const from = try ctx.resolveInst(inst.operand);
191202 try writer.writeAll(indentation ++ "const ");
192203 try renderType(ctx, writer, inst.base.ty);
193204 try writer.print(" {} = (", .{name});
......@@ -223,7 +234,8 @@ fn genCall(ctx: *Context, inst: *Inst.Call) !?[]u8 {
223234 if (arg.cast(Inst.Constant)) |con| {
224235 try renderValue(ctx, writer, arg.ty, con.val);
225236 } else {
226 return ctx.file.fail(ctx.decl.src(), "TODO call pass arg {}", .{arg});
237 const val = try ctx.resolveInst(arg);
238 try writer.print("{}", .{val});
227239 }
228240 }
229241 }