CBE: Sorta working intcasts?

This commit is contained in:
Noam Preil 2020-08-09 19:44:16 -04:00 committed by Andrew Kelley
parent 78fe86dcd2
commit dbd1e42ef2
2 changed files with 55 additions and 8 deletions

View File

@ -117,6 +117,8 @@ fn genFn(file: *C, decl: *Decl) !void {
.dbg_stmt => try genDbgStmt(file, inst.castTag(.dbg_stmt).?, decl),
.breakpoint => try genBreak(file, inst.castTag(.breakpoint).?, decl),
.unreach => try genUnreach(file, inst.castTag(.unreach).?, decl),
// This will be handled correctly later?
.intcast => {},
else => |e| return file.fail(decl.src(), "TODO implement C codegen for {}", .{e}),
}
}
@ -129,6 +131,20 @@ fn genRet(file: *C, inst: *Inst.UnOp, decl: *Decl, expected_return_type: Type) !
return file.fail(decl.src(), "TODO return {}", .{expected_return_type});
}
fn genIntCast(file: *C, inst: *Inst.UnOp, decl: *Decl, argdex: *usize) !void {
const op = inst.operand;
const writer = file.main.writer();
try writer.writeByte('(');
try renderType(file, writer, inst.base.ty, decl.src());
try writer.writeByte(')');
if (op.castTag(.arg)) |_| {
try writer.print("arg{}", .{argdex.*});
argdex.* += 1;
} else {
return file.fail(decl.src(), "TODO intcast {} to {}", .{ op, inst.base.ty });
}
}
fn genCall(file: *C, inst: *Inst.Call, decl: *Decl) !void {
const writer = file.main.writer();
const header = file.header.writer();
@ -196,6 +212,8 @@ fn genAsm(file: *C, as: *Inst.Assembly, decl: *Decl, argdex: *usize) !void {
} else if (arg.castTag(.arg)) |inst| {
try writer.print("arg{}", .{argdex.*});
argdex.* += 1;
} else if (arg.castTag(.intcast)) |inst| {
try genIntCast(file, inst, decl, argdex);
} else {
return file.fail(decl.src(), "TODO non-constant inline asm args ({})", .{arg.tag});
}
@ -220,14 +238,7 @@ fn genAsm(file: *C, as: *Inst.Assembly, decl: *Decl, argdex: *usize) !void {
if (index > 0) {
try writer.writeAll(", ");
}
try writer.writeAll("\"\"(");
if (arg.tag == .constant or arg.tag == .arg) {
try writer.print("{}_constant", .{reg});
} else {
// This is blocked by the earlier test
unreachable;
}
try writer.writeByte(')');
try writer.print("\"\"({}_constant)", .{reg});
} else {
// This is blocked by the earlier test
unreachable;

View File

@ -100,4 +100,40 @@ pub fn addCases(ctx: *TestContext) !void {
\\}
\\
);
ctx.c("exit with u8 parameter", linux_x64,
\\export fn _start() noreturn {
\\ exit(0);
\\}
\\
\\fn exit(code: u8) noreturn {
\\ asm volatile ("syscall"
\\ :
\\ : [number] "{rax}" (231),
\\ [arg1] "{rdi}" (code)
\\ );
\\ unreachable;
\\}
\\
,
\\#include <stddef.h>
\\#include <stdint.h>
\\
\\zig_noreturn void exit(uint8_t arg0);
\\
\\const char *const exit__anon_0 = "{rax}";
\\const char *const exit__anon_1 = "{rdi}";
\\const char *const exit__anon_2 = "syscall";
\\
\\zig_noreturn void _start(void) {
\\ exit(0);
\\}
\\
\\zig_noreturn void exit(uint8_t arg0) {
\\ register size_t rax_constant __asm__("rax") = 231;
\\ register size_t rdi_constant __asm__("rdi") = (size_t)arg0;
\\ __asm volatile ("syscall" :: ""(rax_constant), ""(rdi_constant));
\\ zig_unreachable();
\\}
\\
);
}