| ... | @@ -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, |
| 111 | | 111 | |
| | 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) orelse | 201 | 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 | } |