authorgravatar for pfg@pfg.pwpfg <pfg@pfg.pw> 2020-08-04 01:17:08-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-04 14:38:33-07:00
log1fd99ed32427be59567130c0304c743a22e6eb60
tree2ab34aa94e2470fbe9eee5f89dc07373e9a1b98f
parent52ae2b10aaba858185c1f3a224a4042f6b632a29

stage2: riscv hello world


2 files changed, 89 insertions(+), 34 deletions(-)

src-self-hosted/codegen.zig+65-22
......@@ -1028,8 +1028,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
10281028 .mode = @enumToInt(Instructions.CallBreak.Mode.ebreak),
10291029 });
10301030
1031 try self.code.resize(self.code.items.len + 4);
1032 mem.writeIntLittle(u32, self.code.items[self.code.items.len - 4 ..][0..4], full);
1031 mem.writeIntLittle(u32, try self.code.addManyAsArray(4), full);
10331032 },
10341033 else => return self.fail(src, "TODO implement @breakpoint() for {}", .{self.target.cpu.arch}),
10351034 }
......@@ -1351,8 +1350,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
13511350 .mode = @enumToInt(Instructions.CallBreak.Mode.ecall),
13521351 });
13531352
1354 try self.code.resize(self.code.items.len + 4);
1355 mem.writeIntLittle(u32, self.code.items[self.code.items.len - 4 ..][0..4], full);
1353 mem.writeIntLittle(u32, try self.code.addManyAsArray(4), full);
13561354 } else {
13571355 return self.fail(inst.base.src, "TODO implement support for more riscv64 assembly instructions", .{});
13581356 }
......@@ -1546,29 +1544,74 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
15461544 fn genSetReg(self: *Self, src: usize, reg: Register, mcv: MCValue) InnerError!void {
15471545 switch (arch) {
15481546 .riscv64 => switch (mcv) {
1549 .immediate => |x| {
1550 if (x > math.maxInt(u11)) {
1551 return self.fail(src, "TODO genSetReg 12+ bit immediates for riscv64", .{});
1547 .dead => unreachable,
1548 .ptr_stack_offset => unreachable,
1549 .ptr_embedded_in_code => unreachable,
1550 .unreach, .none => return, // Nothing to do.
1551 .undef => {
1552 if (!self.wantSafety())
1553 return; // The already existing value will do just fine.
1554 // Write the debug undefined value.
1555 switch (reg.size()) {
1556 64 => return self.genSetReg(src, reg, .{ .immediate = 0xaaaaaaaaaaaaaaaa }),
1557 else => unreachable,
15521558 }
1553 const Instruction = packed struct {
1554 opcode: u7,
1555 rd: u5,
1556 mode: u3,
1557 rsi1: u5,
1558 imm: u11,
1559 signextend: u1 = 0,
1560 };
1561 const full = @bitCast(u32, Instructions.Addi{
1562 .imm = @intCast(u11, x),
1563 .rsi1 = Register.zero.id(),
1564 .mode = @enumToInt(Instructions.Addi.Mode.addi),
1559 },
1560 .immediate => |unsigned_x| {
1561 const x = @bitCast(i64, unsigned_x);
1562 if (math.minInt(i12) <= x and x <= math.maxInt(i12)) {
1563 const instruction = @bitCast(u32, Instructions.Addi{
1564 .mode = @enumToInt(Instructions.Addi.Mode.addi),
1565 .imm = @truncate(i12, x),
1566 .rs1 = Register.zero.id(),
1567 .rd = reg.id(),
1568 });
1569
1570 mem.writeIntLittle(u32, try self.code.addManyAsArray(4), instruction);
1571 return;
1572 }
1573 if (math.minInt(i32) <= x and x <= math.maxInt(i32)) {
1574 const split = @bitCast(packed struct {
1575 low12: i12,
1576 up20: i20,
1577 }, @truncate(i32, x));
1578 if (split.low12 < 0) return self.fail(src, "TODO support riscv64 genSetReg i32 immediates with 12th bit set to 1", .{});
1579
1580 const lui = @bitCast(u32, Instructions.Lui{
1581 .imm = split.up20,
1582 .rd = reg.id(),
1583 });
1584 mem.writeIntLittle(u32, try self.code.addManyAsArray(4), lui);
1585
1586 const addi = @bitCast(u32, Instructions.Addi{
1587 .mode = @enumToInt(Instructions.Addi.Mode.addi),
1588 .imm = @truncate(i12, split.low12),
1589 .rs1 = reg.id(),
1590 .rd = reg.id(),
1591 });
1592 mem.writeIntLittle(u32, try self.code.addManyAsArray(4), addi);
1593 return;
1594 }
1595 return self.fail(src, "TODO genSetReg 33-64 bit immediates for riscv64", .{}); // glhf
1596 },
1597 .memory => |addr| {
1598 // The value is in memory at a hard-coded address.
1599 // If the type is a pointer, it means the pointer address is at this memory location.
1600 try self.genSetReg(src, reg, .{ .immediate = addr });
1601
1602 const ld = @bitCast(u32, Instructions.Load{
1603 .mode = @enumToInt(Instructions.Load.Mode.ld),
1604 .rs1 = reg.id(),
15651605 .rd = reg.id(),
1606 .offset = 0,
15661607 });
15671608
1568 try self.code.resize(self.code.items.len + 4);
1569 mem.writeIntLittle(u32, self.code.items[self.code.items.len - 4 ..][0..4], full);
1609 mem.writeIntLittle(u32, try self.code.addManyAsArray(4), ld);
1610 // LOAD imm=[i12 offset = 0], rs1 =
1611
1612 // return self.fail("TODO implement genSetReg memory for riscv64");
15701613 },
1571 else => return self.fail(src, "TODO implement getSetReg for riscv64 MCValue {}", .{mcv}),
1614 else => return self.fail(src, "TODO implement getSetReg for riscv64 {}", .{mcv}),
15721615 },
15731616 .x86_64 => switch (mcv) {
15741617 .dead => unreachable,
src-self-hosted/codegen/riscv64.zig+24-12
......@@ -5,29 +5,41 @@ pub const Instructions = struct {
55 unused1: u5 = 0,
66 unused2: u3 = 0,
77 unused3: u5 = 0,
8 mode: u12,
8 mode: u12, //: Mode
99 };
1010 pub const Addi = packed struct {
1111 pub const Mode = packed enum(u3) { addi = 0b000, slti = 0b010, sltiu = 0b011, xori = 0b100, ori = 0b110, andi = 0b111 };
1212 opcode: u7 = 0b0010011,
1313 rd: u5,
14 mode: u3,
15 rsi1: u5,
16 imm: u11,
17 signextend: u1 = 0,
14 mode: u3, //: Mode
15 rs1: u5,
16 imm: i12,
17 };
18 pub const Lui = packed struct {
19 opcode: u7 = 0b0110111,
20 rd: u5,
21 imm: i20,
22 };
23 pub const Load = packed struct {
24 pub const Mode = packed enum(u3) { ld = 0b011, lwu = 0b110 };
25 opcode: u7 = 0b0000011,
26 rd: u5,
27 mode: u3, //: Mode
28 rs1: u5,
29 offset: i12,
1830 };
1931};
2032
2133// zig fmt: off
2234pub const Register = enum(u8) {
2335 // 64 bit registers
24 zero = 0, // zero
25 ra = 1, // return address. caller saved
26 sp = 2, // stack pointer. callee saved.
27 gp = 3, // global pointer
28 tp = 4, // thread pointer
29 t0 = 5, t1 = 6, t2 = 7, // temporaries. caller saved.
30 s0 = 8, // s0/fp, callee saved.
36 zero, // zero
37 ra, // return address. caller saved
38 sp, // stack pointer. callee saved.
39 gp, // global pointer
40 tp, // thread pointer
41 t0, t1, t2, // temporaries. caller saved.
42 s0, // s0/fp, callee saved.
3143 s1, // callee saved.
3244 a0, a1, // fn args/return values. caller saved.
3345 a2, a3, a4, a5, a6, a7, // fn args. caller saved.