Skip to content

Commit 81681fa

Browse files
authored
Set register and bitfield defaults to reset values (ZigEmbeddedGroup#774)
1 parent 6de3020 commit 81681fa

File tree

6 files changed

+684
-186
lines changed

6 files changed

+684
-186
lines changed

tools/regz/src/Database.zig

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,9 @@ pub const Register = struct {
158158

159159
pub fn format(
160160
register: Register,
161-
comptime fmt: []const u8,
162-
options: std.fmt.FormatOptions,
163-
writer: anytype,
161+
writer: *std.Io.Writer,
164162
) !void {
165-
_ = fmt;
166-
_ = options;
167-
168-
try writer.print("Register: id={} struct_id={?} name='{s}' size_bits={} offset_bytes={} desc='{?s}'", .{
163+
try writer.print("Register: id={f} struct_id={?f} name='{s}' size_bits={} offset_bytes={} desc='{?s}'", .{
169164
register.id,
170165
register.struct_id,
171166
register.name,
@@ -703,7 +698,7 @@ pub fn get_struct_decl_by_name(db: *Database, allocator: Allocator, parent: Stru
703698
}
704699

705700
pub fn get_peripheral_by_struct_id(db: *Database, allocator: Allocator, struct_id: StructID) !?Peripheral {
706-
log.debug("get_peripheral_by_struct_id: strut_id={}", .{struct_id});
701+
log.debug("get_peripheral_by_struct_id: strut_id={f}", .{struct_id});
707702
const query = std.fmt.comptimePrint(
708703
\\SELECT {s}
709704
\\FROM peripherals
@@ -718,7 +713,7 @@ pub fn get_peripheral_by_struct_id(db: *Database, allocator: Allocator, struct_i
718713
}
719714

720715
pub fn get_struct_decl_by_struct_id(db: *Database, allocator: Allocator, struct_id: StructID) !?StructDecl {
721-
log.debug("get_struct_decl_by_struct_id: struct_id={}", .{struct_id});
716+
log.debug("get_struct_decl_by_struct_id: struct_id={f}", .{struct_id});
722717
const query = std.fmt.comptimePrint(
723718
\\SELECT {s}
724719
\\FROM struct_decls
@@ -969,7 +964,7 @@ pub fn get_nested_struct_fields_with_calculated_size(
969964
const nested_struct_fields = try db.get_nested_struct_fields(gpa, struct_id);
970965
defer gpa.free(nested_struct_fields);
971966

972-
log.debug("nested_struct_fields.len={} struct_id={}", .{ nested_struct_fields.len, struct_id });
967+
log.debug("nested_struct_fields.len={} struct_id={f}", .{ nested_struct_fields.len, struct_id });
973968

974969
var size_cache: std.AutoArrayHashMap(StructID, u64) = .init(gpa);
975970
defer size_cache.deinit();
@@ -982,7 +977,7 @@ pub fn get_nested_struct_fields_with_calculated_size(
982977

983978
var depth: u8 = 0;
984979
const size_bytes = try db.recursively_calculate_struct_size(&depth, &size_cache, gpa, nsf.struct_id);
985-
std.log.debug("Calculated struct size: struct_id={} size_bytes={}", .{ nsf.struct_id, size_bytes });
980+
std.log.debug("Calculated struct size: struct_id={f} size_bytes={}", .{ nsf.struct_id, size_bytes });
986981
nsf.size_bytes = if (size_bytes > 0) size_bytes else continue;
987982
try ret.append(gpa, nsf.*);
988983
}
Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ const Allocator = std.mem.Allocator;
1313
const Map = std.AutoArrayHashMapUnmanaged;
1414
const assert = std.debug.assert;
1515

16-
const regz = @import("regz");
17-
const Directory = regz.Database.Directory;
16+
const Directory = @import("Database.zig").Directory;
1817

1918
pub const Kind = enum {
2019
file,
@@ -68,6 +67,47 @@ pub fn dir(fs: *VirtualFilesystem) Directory {
6867
};
6968
}
7069

70+
pub fn get_file(fs: *VirtualFilesystem, path: []const u8) !?ID {
71+
var components: std.ArrayList([]const u8) = .{};
72+
defer components.deinit(fs.gpa);
73+
74+
var it = std.mem.tokenizeScalar(u8, path, '/');
75+
while (it.next()) |component|
76+
try components.append(fs.gpa, component);
77+
78+
return fs.recursive_get_file(.root, components.items);
79+
}
80+
81+
fn recursive_get_file(fs: *VirtualFilesystem, dir_id: ID, components: []const []const u8) !?ID {
82+
return switch (components.len) {
83+
0 => null,
84+
1 => blk: {
85+
const children = try fs.get_children(fs.gpa, dir_id);
86+
defer fs.gpa.free(children);
87+
88+
break :blk for (children) |child| {
89+
if (child.kind != .file)
90+
continue;
91+
92+
const name = fs.get_name(child.id);
93+
if (std.mem.eql(u8, name, components[0]))
94+
break child.id;
95+
} else null;
96+
},
97+
else => blk: {
98+
const children = try fs.get_children(fs.gpa, dir_id);
99+
defer fs.gpa.free(children);
100+
101+
break :blk for (children) |child| {
102+
if (child.kind != .directory)
103+
continue;
104+
105+
break try fs.recursive_get_file(child.id, components[1..]) orelse continue;
106+
} else null;
107+
},
108+
};
109+
}
110+
71111
pub const Entry = struct {
72112
id: ID,
73113
kind: Kind,
@@ -154,9 +194,9 @@ fn create_file_fn(ctx: *anyopaque, path: []const u8, content: []const u8) Direct
154194
var components: std.ArrayList([]const u8) = .{};
155195
defer components.deinit(fs.gpa);
156196

157-
var it = try std.fs.path.componentIterator(path);
197+
var it = std.mem.tokenizeScalar(u8, path, '/');
158198
while (it.next()) |component|
159-
try components.append(fs.gpa, component.name);
199+
try components.append(fs.gpa, component);
160200

161201
var parent: ID = .root;
162202
if (components.items.len > 1) for (components.items[0 .. components.items.len - 2]) |component| {

0 commit comments

Comments
 (0)