diff options
| author | Noam Preil <noam@pixelhero.dev> | 2020-09-08 14:41:51 -0400 |
|---|---|---|
| committer | Noam Preil <noam@pixelhero.dev> | 2020-10-06 15:09:57 -0400 |
| commit | e06ba9e86e53797d90d74e87724b897858fe2d77 (patch) | |
| tree | b793af9a3e3da291e883db125e37ec17ecc5da85 /src/codegen/c.zig | |
| parent | 9ef6c0a035820b586cc2d3a051eef4ae21ec244b (diff) | |
| download | zig-e06ba9e86e53797d90d74e87724b897858fe2d77.tar.gz zig-e06ba9e86e53797d90d74e87724b897858fe2d77.zip | |
CBE: properly resolve Insts
Diffstat (limited to 'src/codegen/c.zig')
| -rw-r--r-- | src/codegen/c.zig | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 21bd0093d8..5d350f7bd5 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -109,6 +109,18 @@ const Context = struct { argdex: usize = 0, unnamed_index: usize = 0, + fn resolveInst(self: *Context, inst: *Inst) ![]u8 { + if (inst.cast(Inst.Constant)) |const_inst| { + var out = std.ArrayList(u8).init(&self.arena.allocator); + try renderValue(self, out.writer(), inst.ty, const_inst.val); + return out.toOwnedSlice(); + } + if (self.inst_map.get(inst)) |val| { + return val; + } + return self.file.fail(inst.src, "Internal error: failed to resolve inst!", .{}); + } + fn name(self: *Context) ![]u8 { const val = try std.fmt.allocPrint(&self.arena.allocator, "__temp_{}", .{self.unnamed_index}); self.unnamed_index += 1; @@ -186,8 +198,7 @@ fn genIntCast(ctx: *Context, inst: *Inst.UnOp) !?[]u8 { const op = inst.operand; const writer = ctx.file.main.writer(); const name = try ctx.name(); - const from = ctx.inst_map.get(op) orelse - return ctx.file.fail(ctx.decl.src(), "Internal error in C backend: intCast argument not found in inst_map", .{}); + const from = try ctx.resolveInst(inst.operand); try writer.writeAll(indentation ++ "const "); try renderType(ctx, writer, inst.base.ty); try writer.print(" {} = (", .{name}); @@ -223,7 +234,8 @@ fn genCall(ctx: *Context, inst: *Inst.Call) !?[]u8 { if (arg.cast(Inst.Constant)) |con| { try renderValue(ctx, writer, arg.ty, con.val); } else { - return ctx.file.fail(ctx.decl.src(), "TODO call pass arg {}", .{arg}); + const val = try ctx.resolveInst(arg); + try writer.print("{}", .{val}); } } } |
