authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-07-12 23:12:41-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-07-13 00:08:21-07:00
logc306392b4404a5a05dcb0958b88b50ecc9c7b6f5
treeedef468906035db14188f8f0c07234d24156b00b
parentb75a51f94b3b2ba1dc240a27caced8e848db5e10

stage2: codegen: more branching support


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

src-self-hosted/codegen.zig+11-3
......@@ -780,14 +780,22 @@ const Function = struct {
780780 }
781781
782782 fn genBr(self: *Function, inst: *ir.Inst.Br, comptime arch: std.Target.Cpu.Arch) !MCValue {
783 if (!inst.args.operand.ty.hasCodeGenBits())
784 return self.brVoid(inst.base.src, inst.args.block, arch);
785
786 const operand = try self.resolveInst(inst.args.operand);
783787 switch (arch) {
784788 else => return self.fail(inst.base.src, "TODO implement br for {}", .{self.target.cpu.arch}),
785789 }
786790 }
787791
788792 fn genBrVoid(self: *Function, inst: *ir.Inst.BrVoid, comptime arch: std.Target.Cpu.Arch) !MCValue {
793 return self.brVoid(inst.base.src, inst.args.block, arch);
794 }
795
796 fn brVoid(self: *Function, src: usize, block: *ir.Inst.Block, comptime arch: std.Target.Cpu.Arch) !MCValue {
789797 // Emit a jump with a relocation. It will be patched up after the block ends.
790 try inst.args.block.codegen.relocs.ensureCapacity(self.gpa, inst.args.block.codegen.relocs.items.len + 1);
798 try block.codegen.relocs.ensureCapacity(self.gpa, block.codegen.relocs.items.len + 1);
791799
792800 switch (arch) {
793801 .i386, .x86_64 => {
......@@ -796,9 +804,9 @@ const Function = struct {
796804 try self.code.resize(self.code.items.len + 5);
797805 self.code.items[self.code.items.len - 5] = 0xe9; // jmp rel32
798806 // Leave the jump offset undefined
799 inst.args.block.codegen.relocs.appendAssumeCapacity(.{ .rel32 = self.code.items.len - 4 });
807 block.codegen.relocs.appendAssumeCapacity(.{ .rel32 = self.code.items.len - 4 });
800808 },
801 else => return self.fail(inst.base.src, "TODO implement brvoid for {}", .{self.target.cpu.arch}),
809 else => return self.fail(src, "TODO implement brvoid for {}", .{self.target.cpu.arch}),
802810 }
803811 return .none;
804812 }