builtin type inference fix (#106)

wch-ch32v003
Matt Knight 1 year ago
parent fec8f40ecc
commit f3c994956b

@ -3,8 +3,8 @@
.version = "0.0.0", .version = "0.0.0",
.dependencies = .{ .dependencies = .{
.clap = .{ .clap = .{
.url = "https://github.com/Hejsil/zig-clap/archive/3d78af4b000758abe6c6761cbf61f5a66abe4f10.tar.gz", .url = "https://github.com/Hejsil/zig-clap/archive/bdb5853b678d68f342ec65b04a6785af522ca6c9.tar.gz",
.hash = "1220ae43eccd3940d287e9e62bf84897040d0cc9e82c970b59c5703d7fd733d0213f", .hash = "12202af04ec78191f2018458a7be29f54e0d9118f7688e7a226857acf754d68b8473",
}, },
}, },
} }

@ -84,7 +84,7 @@ pub fn load(db: *Database, device_id: EntityId) !void {
for (interrupt_registers) |register| { for (interrupt_registers) |register| {
_ = try db.create_field(register, .{ _ = try db.create_field(register, .{
.name = interrupt_name, .name = interrupt_name,
.offset = @bitCast(u32, interrupt_index), .offset = @as(u32, @bitCast(interrupt_index)),
.size = 1, .size = 1,
}); });
} }
@ -100,7 +100,7 @@ pub fn load(db: *Database, device_id: EntityId) !void {
const reg_id = try db.get_entity_id_by_name("type.register", reg_name); const reg_id = try db.get_entity_id_by_name("type.register", reg_name);
_ = try db.create_field(reg_id, .{ _ = try db.create_field(reg_id, .{
.name = interrupt_name, .name = interrupt_name,
.offset = (8 * (@intCast(u8, interrupt_index) % 4)) + (8 - nvic_prio_bits), .offset = (8 * (@as(u8, @intCast(interrupt_index)) % 4)) + (8 - nvic_prio_bits),
.size = nvic_prio_bits, .size = nvic_prio_bits,
}); });
} }

@ -639,7 +639,7 @@ fn load_field(ctx: *Context, node: xml.Node, register_id: EntityId) !void {
var bit_count: u32 = 0; var bit_count: u32 = 0;
var i = offset; var i = offset;
while (i < 32) : (i += 1) { while (i < 32) : (i += 1) {
if (0 != (@as(u64, 1) << @intCast(u5, i)) & mask) { if (0 != (@as(u64, 1) << @as(u5, @intCast(i))) & mask) {
const field_name = try std.fmt.allocPrint(db.arena.allocator(), "{s}_bit{}", .{ const field_name = try std.fmt.allocPrint(db.arena.allocator(), "{s}_bit{}", .{
name, name,
bit_count, bit_count,

@ -42,7 +42,7 @@ pub fn to_zig(db: Database, out_writer: anytype) !void {
try writer.writeByte(0); try writer.writeByte(0);
// format the generated code // format the generated code
var ast = try std.zig.Ast.parse(db.gpa, @ptrCast([:0]const u8, buffer.items[0 .. buffer.items.len - 1]), .zig); var ast = try std.zig.Ast.parse(db.gpa, @as([:0]const u8, @ptrCast(buffer.items[0 .. buffer.items.len - 1])), .zig);
defer ast.deinit(db.gpa); defer ast.deinit(db.gpa);
// TODO: ast check? // TODO: ast check?
@ -262,7 +262,7 @@ fn write_peripheral_instance(db: Database, instance_id: EntityId, offset: u64, o
else else
""; "";
try writer.print("pub const {s} = @intToPtr(*volatile {s}{s}, 0x{x});\n", .{ try writer.print("pub const {s} = @as(*volatile {s}{s}, @ptrFromInt(0x{x}));\n", .{
std.zig.fmtId(name), std.zig.fmtId(name),
array_prefix, array_prefix,
type_ref, type_ref,
@ -906,7 +906,7 @@ test "gen.peripheral instantiation" {
\\pub const devices = struct { \\pub const devices = struct {
\\ pub const TEST_DEVICE = struct { \\ pub const TEST_DEVICE = struct {
\\ pub const peripherals = struct { \\ pub const peripherals = struct {
\\ pub const TEST0 = @intToPtr(*volatile types.peripherals.TEST_PERIPHERAL, 0x1000); \\ pub const TEST0 = @as(*volatile types.peripherals.TEST_PERIPHERAL, @ptrFromInt(0x1000));
\\ }; \\ };
\\ }; \\ };
\\}; \\};
@ -940,8 +940,8 @@ test "gen.peripherals with a shared type" {
\\pub const devices = struct { \\pub const devices = struct {
\\ pub const TEST_DEVICE = struct { \\ pub const TEST_DEVICE = struct {
\\ pub const peripherals = struct { \\ pub const peripherals = struct {
\\ pub const TEST0 = @intToPtr(*volatile types.peripherals.TEST_PERIPHERAL, 0x1000); \\ pub const TEST0 = @as(*volatile types.peripherals.TEST_PERIPHERAL, @ptrFromInt(0x1000));
\\ pub const TEST1 = @intToPtr(*volatile types.peripherals.TEST_PERIPHERAL, 0x2000); \\ pub const TEST1 = @as(*volatile types.peripherals.TEST_PERIPHERAL, @ptrFromInt(0x2000));
\\ }; \\ };
\\ }; \\ };
\\}; \\};
@ -1160,8 +1160,8 @@ test "gen.namespaced register groups" {
\\pub const devices = struct { \\pub const devices = struct {
\\ pub const ATmega328P = struct { \\ pub const ATmega328P = struct {
\\ pub const peripherals = struct { \\ pub const peripherals = struct {
\\ pub const PORTB = @intToPtr(*volatile types.peripherals.PORT.PORTB, 0x23); \\ pub const PORTB = @as(*volatile types.peripherals.PORT.PORTB, @ptrFromInt(0x23));
\\ pub const PORTC = @intToPtr(*volatile types.peripherals.PORT.PORTC, 0x26); \\ pub const PORTC = @as(*volatile types.peripherals.PORT.PORTC, @ptrFromInt(0x26));
\\ }; \\ };
\\ }; \\ };
\\}; \\};
@ -1202,7 +1202,7 @@ test "gen.peripheral with reserved register" {
\\pub const devices = struct { \\pub const devices = struct {
\\ pub const ATmega328P = struct { \\ pub const ATmega328P = struct {
\\ pub const peripherals = struct { \\ pub const peripherals = struct {
\\ pub const PORTB = @intToPtr(*volatile types.peripherals.PORTB, 0x23); \\ pub const PORTB = @as(*volatile types.peripherals.PORTB, @ptrFromInt(0x23));
\\ }; \\ };
\\ }; \\ };
\\}; \\};
@ -1235,7 +1235,7 @@ test "gen.peripheral with count" {
\\pub const devices = struct { \\pub const devices = struct {
\\ pub const ATmega328P = struct { \\ pub const ATmega328P = struct {
\\ pub const peripherals = struct { \\ pub const peripherals = struct {
\\ pub const PORTB = @intToPtr(*volatile [4]types.peripherals.PORTB, 0x23); \\ pub const PORTB = @as(*volatile [4]types.peripherals.PORTB, @ptrFromInt(0x23));
\\ }; \\ };
\\ }; \\ };
\\}; \\};
@ -1268,7 +1268,7 @@ test "gen.peripheral with count, padding required" {
\\pub const devices = struct { \\pub const devices = struct {
\\ pub const ATmega328P = struct { \\ pub const ATmega328P = struct {
\\ pub const peripherals = struct { \\ pub const peripherals = struct {
\\ pub const PORTB = @intToPtr(*volatile [4]types.peripherals.PORTB, 0x23); \\ pub const PORTB = @as(*volatile [4]types.peripherals.PORTB, @ptrFromInt(0x23));
\\ }; \\ };
\\ }; \\ };
\\}; \\};
@ -1302,7 +1302,7 @@ test "gen.register with count" {
\\pub const devices = struct { \\pub const devices = struct {
\\ pub const ATmega328P = struct { \\ pub const ATmega328P = struct {
\\ pub const peripherals = struct { \\ pub const peripherals = struct {
\\ pub const PORTB = @intToPtr(*volatile types.peripherals.PORTB, 0x23); \\ pub const PORTB = @as(*volatile types.peripherals.PORTB, @ptrFromInt(0x23));
\\ }; \\ };
\\ }; \\ };
\\}; \\};
@ -1335,7 +1335,7 @@ test "gen.register with count and fields" {
\\pub const devices = struct { \\pub const devices = struct {
\\ pub const ATmega328P = struct { \\ pub const ATmega328P = struct {
\\ pub const peripherals = struct { \\ pub const peripherals = struct {
\\ pub const PORTB = @intToPtr(*volatile types.peripherals.PORTB, 0x23); \\ pub const PORTB = @as(*volatile types.peripherals.PORTB, @ptrFromInt(0x23));
\\ }; \\ };
\\ }; \\ };
\\}; \\};

@ -152,9 +152,9 @@ fn read_fn(ctx: ?*anyopaque, buffer: ?[*]u8, len: c_int) callconv(.C) c_int {
return -1; return -1;
return if (ctx) |c| blk: { return if (ctx) |c| blk: {
const reader = @ptrCast(*std.fs.File.Reader, @alignCast(@alignOf(*std.fs.File.Reader), c)); const reader = @as(*std.fs.File.Reader, @ptrCast(@alignCast(c)));
const n = reader.read(buffer.?[0..@intCast(usize, len)]) catch return -1; const n = reader.read(buffer.?[0..@as(usize, @intCast(len))]) catch return -1;
break :blk @intCast(c_int, n); break :blk @as(c_int, @intCast(n));
} else -1; } else -1;
} }

@ -20,13 +20,13 @@ pub fn Mmio(comptime size: u8, comptime PackedT: type) type {
pub const underlying_type = PackedT; pub const underlying_type = PackedT;
pub inline fn read(addr: *volatile Self) PackedT { pub inline fn read(addr: *volatile Self) PackedT {
return @bitCast(PackedT, addr.raw); return @as(PackedT, @bitCast(addr.raw));
} }
pub inline fn write(addr: *volatile Self, val: PackedT) void { pub inline fn write(addr: *volatile Self, val: PackedT) void {
// This is a workaround for a compiler bug related to miscompilation // This is a workaround for a compiler bug related to miscompilation
// If the tmp var is not used, result location will fuck things up // If the tmp var is not used, result location will fuck things up
var tmp = @bitCast(IntT, val); var tmp = @as(IntT, @bitCast(val));
addr.raw = tmp; addr.raw = tmp;
} }

@ -48,7 +48,7 @@ fn get_array(val: Value) !Array {
// TODO: handle edge cases // TODO: handle edge cases
fn get_integer_from_object(obj: ObjectMap, comptime T: type, key: []const u8) !?T { fn get_integer_from_object(obj: ObjectMap, comptime T: type, key: []const u8) !?T {
return switch (obj.get(key) orelse return null) { return switch (obj.get(key) orelse return null) {
.integer => |num| @intCast(T, num), .integer => |num| @as(T, @intCast(num)),
else => return error.NotJsonInteger, else => return error.NotJsonInteger,
}; };
} }
@ -247,7 +247,7 @@ fn load_peripheral(
.description = try get_string_from_object(peripheral, "description"), .description = try get_string_from_object(peripheral, "description"),
.size = if (peripheral.get("size")) |size_val| .size = if (peripheral.get("size")) |size_val|
switch (size_val) { switch (size_val) {
.integer => |num| @intCast(u64, num), .integer => |num| @as(u64, @intCast(num)),
else => return error.SizeNotInteger, else => return error.SizeNotInteger,
} }
else else
@ -633,19 +633,19 @@ fn populate_type(
try typ.put("description", .{ .string = description }); try typ.put("description", .{ .string = description });
if (db.attrs.offset.get(id)) |offset| if (db.attrs.offset.get(id)) |offset|
try typ.put("offset", .{ .integer = @intCast(i64, offset) }); try typ.put("offset", .{ .integer = @as(i64, @intCast(offset)) });
if (db.attrs.size.get(id)) |size| if (db.attrs.size.get(id)) |size|
try typ.put("size", .{ .integer = @intCast(i64, size) }); try typ.put("size", .{ .integer = @as(i64, @intCast(size)) });
if (db.attrs.count.get(id)) |count| if (db.attrs.count.get(id)) |count|
try typ.put("count", .{ .integer = @intCast(i64, count) }); try typ.put("count", .{ .integer = @as(i64, @intCast(count)) });
if (db.attrs.reset_value.get(id)) |reset_value| if (db.attrs.reset_value.get(id)) |reset_value|
try typ.put("reset_value", .{ .integer = @intCast(i64, reset_value) }); try typ.put("reset_value", .{ .integer = @as(i64, @intCast(reset_value)) });
if (db.attrs.reset_mask.get(id)) |reset_mask| if (db.attrs.reset_mask.get(id)) |reset_mask|
try typ.put("reset_mask", .{ .integer = @intCast(i64, reset_mask) }); try typ.put("reset_mask", .{ .integer = @as(i64, @intCast(reset_mask)) });
if (db.attrs.version.get(id)) |version| if (db.attrs.version.get(id)) |version|
try typ.put("version", .{ .string = version }); try typ.put("version", .{ .string = version });
@ -798,13 +798,13 @@ fn populate_peripheral(
try peripheral.put("description", .{ .string = description }); try peripheral.put("description", .{ .string = description });
if (db.attrs.offset.get(id)) |offset| if (db.attrs.offset.get(id)) |offset|
try peripheral.put("offset", .{ .integer = @intCast(i64, offset) }); try peripheral.put("offset", .{ .integer = @as(i64, @intCast(offset)) });
if (db.attrs.version.get(id)) |version| if (db.attrs.version.get(id)) |version|
try peripheral.put("version", .{ .string = version }); try peripheral.put("version", .{ .string = version });
if (db.attrs.count.get(id)) |count| if (db.attrs.count.get(id)) |count|
try peripheral.put("count", .{ .integer = @intCast(i64, count) }); try peripheral.put("count", .{ .integer = @as(i64, @intCast(count)) });
// TODO: handle collisions -- will need to inline the type // TODO: handle collisions -- will need to inline the type
const type_ref = try id_to_ref( const type_ref = try id_to_ref(

@ -46,7 +46,7 @@ pub const Node = struct {
pub fn next(it: *AttrIterator) ?Entry { pub fn next(it: *AttrIterator) ?Entry {
return if (it.attr) |attr| ret: { return if (it.attr) |attr| ret: {
if (attr.name) |name| if (attr.name) |name|
if (@ptrCast(*c.xmlNode, attr.children).content) |content| { if (@as(*c.xmlNode, @ptrCast(attr.children)).content) |content| {
defer it.attr = attr.next; defer it.attr = attr.next;
break :ret Entry{ break :ret Entry{
.key = std.mem.span(name), .key = std.mem.span(name),
@ -59,8 +59,8 @@ pub const Node = struct {
pub fn get_attribute(node: Node, key: [:0]const u8) ?[]const u8 { pub fn get_attribute(node: Node, key: [:0]const u8) ?[]const u8 {
if (c.xmlHasProp(node.impl, key.ptr)) |prop| { if (c.xmlHasProp(node.impl, key.ptr)) |prop| {
if (@ptrCast(*c.xmlAttr, prop).children) |value_node| { if (@as(*c.xmlAttr, @ptrCast(prop)).children) |value_node| {
if (@ptrCast(*c.xmlNode, value_node).content) |content| { if (@as(*c.xmlNode, @ptrCast(value_node)).content) |content| {
return std.mem.span(content); return std.mem.span(content);
} }
} }
@ -70,7 +70,7 @@ pub const Node = struct {
} }
pub fn find_child(node: Node, key: []const u8) ?Node { pub fn find_child(node: Node, key: []const u8) ?Node {
var it = @ptrCast(?*c.xmlNode, node.impl.children); var it = @as(?*c.xmlNode, @ptrCast(node.impl.children));
return while (it != null) : (it = it.?.next) { return while (it != null) : (it = it.?.next) {
if (it.?.type != 1) if (it.?.type != 1)
continue; continue;
@ -107,7 +107,7 @@ pub const Node = struct {
pub fn get_value(node: Node, key: []const u8) ?[:0]const u8 { pub fn get_value(node: Node, key: []const u8) ?[:0]const u8 {
return if (node.find_child(key)) |child| return if (node.find_child(key)) |child|
if (child.impl.children) |value_node| if (child.impl.children) |value_node|
if (@ptrCast(*c.xmlNode, value_node).content) |content| if (@as(*c.xmlNode, @ptrCast(value_node)).content) |content|
std.mem.span(content) std.mem.span(content)
else else
null null
@ -135,7 +135,7 @@ pub const Doc = struct {
return Doc{ return Doc{
.impl = c.xmlReadMemory( .impl = c.xmlReadMemory(
text.ptr, text.ptr,
@intCast(c_int, text.len), @as(c_int, @intCast(text.len)),
null, null,
null, null,
0, 0,

Loading…
Cancel
Save