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 {...@@ -109,6 +109,18 @@ const Context = struct {
109 argdex: usize = 0,109 argdex: usize = 0,
110 unnamed_index: usize = 0,110 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
112 fn name(self: *Context) ![]u8 {124 fn name(self: *Context) ![]u8 {
113 const val = try std.fmt.allocPrint(&self.arena.allocator, "__temp_{}", .{self.unnamed_index});125 const val = try std.fmt.allocPrint(&self.arena.allocator, "__temp_{}", .{self.unnamed_index});
114 self.unnamed_index += 1;126 self.unnamed_index += 1;
...@@ -186,8 +198,7 @@ fn genIntCast(ctx: *Context, inst: *Inst.UnOp) !?[]u8 {...@@ -186,8 +198,7 @@ fn genIntCast(ctx: *Context, inst: *Inst.UnOp) !?[]u8 {
186 const op = inst.operand;198 const op = inst.operand;
187 const writer = ctx.file.main.writer();199 const writer = ctx.file.main.writer();
188 const name = try ctx.name();200 const name = try ctx.name();
189 const from = ctx.inst_map.get(op) orelse201 const from = try ctx.resolveInst(inst.operand);
190 return ctx.file.fail(ctx.decl.src(), "Internal error in C backend: intCast argument not found in inst_map", .{});
191 try writer.writeAll(indentation ++ "const ");202 try writer.writeAll(indentation ++ "const ");
192 try renderType(ctx, writer, inst.base.ty);203 try renderType(ctx, writer, inst.base.ty);
193 try writer.print(" {} = (", .{name});204 try writer.print(" {} = (", .{name});
...@@ -223,7 +234,8 @@ fn genCall(ctx: *Context, inst: *Inst.Call) !?[]u8 {...@@ -223,7 +234,8 @@ fn genCall(ctx: *Context, inst: *Inst.Call) !?[]u8 {
223 if (arg.cast(Inst.Constant)) |con| {234 if (arg.cast(Inst.Constant)) |con| {
224 try renderValue(ctx, writer, arg.ty, con.val);235 try renderValue(ctx, writer, arg.ty, con.val);
225 } else {236 } 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});
227 }239 }
228 }240 }
229 }241 }