mirror of
https://github.com/ziglang/zig.git
synced 2026-02-14 05:20:34 +00:00
CBE: Code cleanup
This commit is contained in:
parent
4a46248198
commit
8d6cadee16
@ -1,9 +1,11 @@
|
||||
const link = @import("link.zig");
|
||||
const Module = @import("Module.zig");
|
||||
const ir = @import("ir.zig");
|
||||
|
||||
const std = @import("std");
|
||||
|
||||
const Inst = @import("ir.zig").Inst;
|
||||
const Value = @import("value.zig").Value;
|
||||
const Type = @import("type.zig").Type;
|
||||
const std = @import("std");
|
||||
|
||||
const C = link.File.C;
|
||||
const Decl = Module.Decl;
|
||||
@ -45,159 +47,159 @@ fn renderFunctionSignature(file: *C, writer: std.ArrayList(u8).Writer, decl: *De
|
||||
const name = try map(file.allocator, mem.spanZ(decl.name));
|
||||
defer file.allocator.free(name);
|
||||
try writer.print(" {}(", .{name});
|
||||
if (tv.ty.fnParamLen() == 0) {
|
||||
try writer.writeAll("void)");
|
||||
} else {
|
||||
if (tv.ty.fnParamLen() == 0)
|
||||
try writer.writeAll("void)")
|
||||
else
|
||||
return file.fail(decl.src(), "TODO implement parameters", .{});
|
||||
}
|
||||
}
|
||||
|
||||
pub fn generate(file: *C, decl: *Decl) !void {
|
||||
const writer = file.main.writer();
|
||||
const header = file.header.writer();
|
||||
const tv = decl.typed_value.most_recent.typed_value;
|
||||
switch (tv.ty.zigTypeTag()) {
|
||||
.Fn => {
|
||||
try renderFunctionSignature(file, writer, decl);
|
||||
|
||||
try writer.writeAll(" {");
|
||||
|
||||
const func: *Module.Fn = tv.val.cast(Value.Payload.Function).?.func;
|
||||
const instructions = func.analysis.success.instructions;
|
||||
if (instructions.len > 0) {
|
||||
for (instructions) |inst| {
|
||||
try writer.writeAll("\n\t");
|
||||
switch (inst.tag) {
|
||||
.assembly => {
|
||||
const as = inst.cast(ir.Inst.Assembly).?.args;
|
||||
for (as.inputs) |i, index| {
|
||||
if (i[0] == '{' and i[i.len - 1] == '}') {
|
||||
const reg = i[1 .. i.len - 1];
|
||||
const arg = as.args[index];
|
||||
if (arg.cast(ir.Inst.Constant)) |c| {
|
||||
if (c.val.tag() == .int_u64) {
|
||||
try writer.writeAll("register ");
|
||||
try renderType(file, writer, arg.ty, decl.src());
|
||||
try writer.print(" {}_constant __asm__(\"{}\") = {};\n\t", .{ reg, reg, c.val.toUnsignedInt() });
|
||||
} else {
|
||||
return file.fail(decl.src(), "TODO inline asm {} args", .{c.val.tag()});
|
||||
}
|
||||
} else {
|
||||
return file.fail(decl.src(), "TODO non-constant inline asm args", .{});
|
||||
}
|
||||
} else {
|
||||
return file.fail(decl.src(), "TODO non-explicit inline asm regs", .{});
|
||||
}
|
||||
}
|
||||
try writer.print("__asm {} (\"{}\"", .{ if (as.is_volatile) @as([]const u8, "volatile") else "", as.asm_source });
|
||||
if (as.output) |o| {
|
||||
return file.fail(decl.src(), "TODO inline asm output", .{});
|
||||
}
|
||||
if (as.inputs.len > 0) {
|
||||
if (as.output == null) {
|
||||
try writer.writeAll(" :");
|
||||
}
|
||||
try writer.writeAll(": ");
|
||||
for (as.inputs) |i, index| {
|
||||
if (i[0] == '{' and i[i.len - 1] == '}') {
|
||||
const reg = i[1 .. i.len - 1];
|
||||
const arg = as.args[index];
|
||||
if (index > 0) {
|
||||
try writer.writeAll(", ");
|
||||
}
|
||||
if (arg.cast(ir.Inst.Constant)) |c| {
|
||||
try writer.print("\"\"({}_constant)", .{reg});
|
||||
} else {
|
||||
// This is blocked by the earlier test
|
||||
unreachable;
|
||||
}
|
||||
} else {
|
||||
// This is blocked by the earlier test
|
||||
unreachable;
|
||||
}
|
||||
}
|
||||
}
|
||||
try writer.writeAll(");");
|
||||
},
|
||||
.call => {
|
||||
const call = inst.cast(ir.Inst.Call).?;
|
||||
if (call.args.func.cast(ir.Inst.Constant)) |func_inst| {
|
||||
if (func_inst.val.cast(Value.Payload.Function)) |func_val| {
|
||||
const target = func_val.func.owner_decl;
|
||||
const target_ty = target.typed_value.most_recent.typed_value.ty;
|
||||
const ret_ty = target_ty.fnReturnType().tag();
|
||||
if (target_ty.fnReturnType().hasCodeGenBits() and call.base.isUnused()) {
|
||||
try writer.print("(void)", .{});
|
||||
}
|
||||
const tname = mem.spanZ(target.name);
|
||||
if (file.called.get(tname) == null) {
|
||||
try file.called.put(tname, void{});
|
||||
try renderFunctionSignature(file, header, target);
|
||||
try header.writeAll(";\n");
|
||||
}
|
||||
try writer.print("{}();", .{tname});
|
||||
} else {
|
||||
return file.fail(decl.src(), "TODO non-function call target?", .{});
|
||||
}
|
||||
if (call.args.args.len != 0) {
|
||||
return file.fail(decl.src(), "TODO function arguments", .{});
|
||||
}
|
||||
} else {
|
||||
return file.fail(decl.src(), "TODO non-constant call inst?", .{});
|
||||
}
|
||||
},
|
||||
.ret => {
|
||||
const ret_value: *ir.Inst = inst.cast(ir.Inst.Ret).?.args.operand;
|
||||
const expected_return_type = tv.ty.fnReturnType();
|
||||
const value = ret_value.value().?;
|
||||
if (expected_return_type.eql(ret_value.ty)) {
|
||||
return file.fail(decl.src(), "TODO return {}", .{expected_return_type});
|
||||
} else {
|
||||
if (expected_return_type.isInt() and ret_value.ty.tag() == .comptime_int) {
|
||||
if (value.intFitsInType(expected_return_type, file.options.target)) {
|
||||
if (expected_return_type.intInfo(file.options.target).bits <= 64) {
|
||||
try writer.print("return {};", .{value.toUnsignedInt()});
|
||||
} else {
|
||||
return file.fail(decl.src(), "TODO return ints > 64 bits", .{});
|
||||
}
|
||||
} else {
|
||||
return file.fail(decl.src(), "comptime int {} does not fit in {}", .{ value.toUnsignedInt(), expected_return_type });
|
||||
}
|
||||
} else {
|
||||
return file.fail(decl.src(), "return type mismatch: expected {}, found {}", .{ expected_return_type, ret_value.ty });
|
||||
}
|
||||
}
|
||||
},
|
||||
else => |e| {
|
||||
return file.fail(decl.src(), "TODO {}", .{e});
|
||||
},
|
||||
}
|
||||
}
|
||||
try writer.writeAll("\n");
|
||||
}
|
||||
|
||||
try writer.writeAll("}\n\n");
|
||||
},
|
||||
.Array => {
|
||||
// TODO: prevent inline asm constants from being emitted
|
||||
const name = try map(file.allocator, mem.span(decl.name));
|
||||
defer file.allocator.free(name);
|
||||
if (tv.val.cast(Value.Payload.Bytes)) |payload| {
|
||||
if (tv.ty.arraySentinel()) |sentinel| {
|
||||
if (sentinel.toUnsignedInt() == 0) {
|
||||
try file.constants.writer().print("const char *const {} = \"{}\";\n", .{ name, payload.data });
|
||||
} else {
|
||||
return file.fail(decl.src(), "TODO byte arrays with non-zero sentinels", .{});
|
||||
}
|
||||
} else {
|
||||
return file.fail(decl.src(), "TODO byte arrays without sentinels", .{});
|
||||
}
|
||||
} else {
|
||||
return file.fail(decl.src(), "TODO non-byte arrays", .{});
|
||||
}
|
||||
},
|
||||
else => |e| {
|
||||
return file.fail(decl.src(), "TODO {}", .{e});
|
||||
},
|
||||
switch (decl.typed_value.most_recent.typed_value.ty.zigTypeTag()) {
|
||||
.Fn => try genFn(file, decl),
|
||||
.Array => try genArray(file, decl),
|
||||
else => |e| return file.fail(decl.src(), "TODO {}", .{e}),
|
||||
}
|
||||
}
|
||||
|
||||
fn genArray(file: *C, decl: *Decl) !void {
|
||||
const tv = decl.typed_value.most_recent.typed_value;
|
||||
// TODO: prevent inline asm constants from being emitted
|
||||
const name = try map(file.allocator, mem.span(decl.name));
|
||||
defer file.allocator.free(name);
|
||||
if (tv.val.cast(Value.Payload.Bytes)) |payload|
|
||||
if (tv.ty.arraySentinel()) |sentinel|
|
||||
if (sentinel.toUnsignedInt() == 0)
|
||||
try file.constants.writer().print("const char *const {} = \"{}\";\n", .{ name, payload.data })
|
||||
else
|
||||
return file.fail(decl.src(), "TODO byte arrays with non-zero sentinels", .{})
|
||||
else
|
||||
return file.fail(decl.src(), "TODO byte arrays without sentinels", .{})
|
||||
else
|
||||
return file.fail(decl.src(), "TODO non-byte arrays", .{});
|
||||
}
|
||||
|
||||
fn genFn(file: *C, decl: *Decl) !void {
|
||||
const writer = file.main.writer();
|
||||
const tv = decl.typed_value.most_recent.typed_value;
|
||||
|
||||
try renderFunctionSignature(file, writer, decl);
|
||||
|
||||
try writer.writeAll(" {");
|
||||
|
||||
const func: *Module.Fn = tv.val.cast(Value.Payload.Function).?.func;
|
||||
const instructions = func.analysis.success.instructions;
|
||||
if (instructions.len > 0) {
|
||||
for (instructions) |inst| {
|
||||
try writer.writeAll("\n\t");
|
||||
switch (inst.tag) {
|
||||
.assembly => try genAsm(file, inst.cast(Inst.Assembly).?, decl),
|
||||
.call => try genCall(file, inst.cast(Inst.Call).?, decl),
|
||||
.ret => try genRet(file, inst.cast(Inst.Ret).?, decl, tv.ty.fnReturnType()),
|
||||
else => |e| return file.fail(decl.src(), "TODO {}", .{e}),
|
||||
}
|
||||
}
|
||||
try writer.writeAll("\n");
|
||||
}
|
||||
|
||||
try writer.writeAll("}\n\n");
|
||||
}
|
||||
|
||||
fn genRet(file: *C, inst: *Inst.Ret, decl: *Decl, expected_return_type: Type) !void {
|
||||
const writer = file.main.writer();
|
||||
const ret_value = inst.args.operand;
|
||||
const value = ret_value.value().?;
|
||||
if (expected_return_type.eql(ret_value.ty))
|
||||
return file.fail(decl.src(), "TODO return {}", .{expected_return_type})
|
||||
else if (expected_return_type.isInt() and ret_value.ty.tag() == .comptime_int)
|
||||
if (value.intFitsInType(expected_return_type, file.options.target))
|
||||
if (expected_return_type.intInfo(file.options.target).bits <= 64)
|
||||
try writer.print("return {};", .{value.toUnsignedInt()})
|
||||
else
|
||||
return file.fail(decl.src(), "TODO return ints > 64 bits", .{})
|
||||
else
|
||||
return file.fail(decl.src(), "comptime int {} does not fit in {}", .{ value.toUnsignedInt(), expected_return_type })
|
||||
else
|
||||
return file.fail(decl.src(), "return type mismatch: expected {}, found {}", .{ expected_return_type, ret_value.ty });
|
||||
}
|
||||
|
||||
fn genCall(file: *C, inst: *Inst.Call, decl: *Decl) !void {
|
||||
const writer = file.main.writer();
|
||||
const header = file.header.writer();
|
||||
if (inst.args.func.cast(Inst.Constant)) |func_inst| {
|
||||
if (func_inst.val.cast(Value.Payload.Function)) |func_val| {
|
||||
const target = func_val.func.owner_decl;
|
||||
const target_ty = target.typed_value.most_recent.typed_value.ty;
|
||||
const ret_ty = target_ty.fnReturnType().tag();
|
||||
if (target_ty.fnReturnType().hasCodeGenBits() and inst.base.isUnused()) {
|
||||
try writer.print("(void)", .{});
|
||||
}
|
||||
const tname = mem.spanZ(target.name);
|
||||
if (file.called.get(tname) == null) {
|
||||
try file.called.put(tname, void{});
|
||||
try renderFunctionSignature(file, header, target);
|
||||
try header.writeAll(";\n");
|
||||
}
|
||||
try writer.print("{}();", .{tname});
|
||||
} else {
|
||||
return file.fail(decl.src(), "TODO non-function call target?", .{});
|
||||
}
|
||||
if (inst.args.args.len != 0) {
|
||||
return file.fail(decl.src(), "TODO function arguments", .{});
|
||||
}
|
||||
} else {
|
||||
return file.fail(decl.src(), "TODO non-constant call inst?", .{});
|
||||
}
|
||||
}
|
||||
|
||||
fn genAsm(file: *C, inst: *Inst.Assembly, decl: *Decl) !void {
|
||||
const as = inst.args;
|
||||
const writer = file.main.writer();
|
||||
for (as.inputs) |i, index| {
|
||||
if (i[0] == '{' and i[i.len - 1] == '}') {
|
||||
const reg = i[1 .. i.len - 1];
|
||||
const arg = as.args[index];
|
||||
if (arg.cast(Inst.Constant)) |c| {
|
||||
if (c.val.tag() == .int_u64) {
|
||||
try writer.writeAll("register ");
|
||||
try renderType(file, writer, arg.ty, decl.src());
|
||||
try writer.print(" {}_constant __asm__(\"{}\") = {};\n\t", .{ reg, reg, c.val.toUnsignedInt() });
|
||||
} else {
|
||||
return file.fail(decl.src(), "TODO inline asm {} args", .{c.val.tag()});
|
||||
}
|
||||
} else {
|
||||
return file.fail(decl.src(), "TODO non-constant inline asm args", .{});
|
||||
}
|
||||
} else {
|
||||
return file.fail(decl.src(), "TODO non-explicit inline asm regs", .{});
|
||||
}
|
||||
}
|
||||
try writer.print("__asm {} (\"{}\"", .{ if (as.is_volatile) @as([]const u8, "volatile") else "", as.asm_source });
|
||||
if (as.output) |o| {
|
||||
return file.fail(decl.src(), "TODO inline asm output", .{});
|
||||
}
|
||||
if (as.inputs.len > 0) {
|
||||
if (as.output == null) {
|
||||
try writer.writeAll(" :");
|
||||
}
|
||||
try writer.writeAll(": ");
|
||||
for (as.inputs) |i, index| {
|
||||
if (i[0] == '{' and i[i.len - 1] == '}') {
|
||||
const reg = i[1 .. i.len - 1];
|
||||
const arg = as.args[index];
|
||||
if (index > 0) {
|
||||
try writer.writeAll(", ");
|
||||
}
|
||||
if (arg.cast(Inst.Constant)) |c| {
|
||||
try writer.print("\"\"({}_constant)", .{reg});
|
||||
} else {
|
||||
// This is blocked by the earlier test
|
||||
unreachable;
|
||||
}
|
||||
} else {
|
||||
// This is blocked by the earlier test
|
||||
unreachable;
|
||||
}
|
||||
}
|
||||
}
|
||||
try writer.writeAll(");");
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user