From 1a1a281c01a3cefb6eebf90c639fccb7462172ec Mon Sep 17 00:00:00 2001 From: Matt Knight Date: Wed, 2 Mar 2022 23:59:26 -0800 Subject: [PATCH] description word wrapping and fixed unhandled interrupt function --- tools/regz/src/Database.zig | 80 +++++++++++++++++++++++++------------ tools/regz/src/mmio.zig | 10 +++-- tools/regz/src/xml.zig | 17 ++------ 3 files changed, 65 insertions(+), 42 deletions(-) diff --git a/tools/regz/src/Database.zig b/tools/regz/src/Database.zig index fa999d1..a93b105 100644 --- a/tools/regz/src/Database.zig +++ b/tools/regz/src/Database.zig @@ -395,6 +395,46 @@ pub fn deinit(self: *Self) void { self.arena.deinit(); } +fn writeDescription( + allocator: Allocator, + writer: anytype, + description: []const u8, + indent: usize, +) !void { + const max_line_width = 80; + + // keep explicit lines from svd + var line_it = std.mem.tokenize(u8, description, "\n\r"); + while (line_it.next()) |input_line| { + var it = std.mem.tokenize(u8, input_line, " \t"); + var line = std.ArrayList(u8).init(allocator); + defer line.deinit(); + + while (it.next()) |token| { + if (line.items.len + token.len > max_line_width) { + if (line.items.len > 0) { + try writer.writeByteNTimes(' ', indent * 4); + try writer.print("///{s}\n", .{line.items}); + line.clearRetainingCapacity(); + try line.append(' '); + try line.appendSlice(token); + } else { + try writer.writeByteNTimes(' ', indent * 4); + try writer.print("/// {s}\n", .{token}); + } + } else { + try line.append(' '); + try line.appendSlice(token); + } + } + + if (line.items.len > 0) { + try writer.writeByteNTimes(' ', indent * 4); + try writer.print("///{s}\n", .{line.items}); + } + } +} + pub fn toZig(self: *Self, writer: anytype) !void { try writer.writeAll("// this file is generated by regz\n//\n"); if (self.device.vendor) |vendor_name| @@ -463,7 +503,7 @@ pub fn toZig(self: *Self, writer: anytype) !void { } if (interrupt.description) |description| if (!isUselessDescription(description)) - try writer.print("\n /// {s}\n", .{description}); + try writeDescription(self.arena.child_allocator, writer, description, 1); try writer.print(" {s}: InterruptVector = unhandled,\n", .{std.zig.fmtId(interrupt.name)}); expected += 1; @@ -495,7 +535,7 @@ pub fn toZig(self: *Self, writer: anytype) !void { const registers = self.registers.items[reg_range.begin..reg_range.end]; if (registers.len != 0 or has_clusters) { if (peripheral.description) |description| if (!isUselessDescription(description)) - try writer.print("\n /// {s}\n", .{description}); + try writeDescription(self.arena.child_allocator, writer, description, 1); try writer.print( \\ pub const {s} = struct {{ \\ pub const base_address = 0x{x}; @@ -554,12 +594,9 @@ fn genZigCluster( .namespaced => if (db.registers_in_clusters.get(cluster_idx)) |range| { const registers = db.registers.items[range.begin..range.end]; try writer.writeByte('\n'); - if (cluster.description) |description| { - if (!isUselessDescription(description)) { - try writer.writeByteNTimes(' ', indent * 4); - try writer.print("/// {s}\n", .{description}); - } - } + if (cluster.description) |description| + if (!isUselessDescription(description)) + try writeDescription(db.arena.child_allocator, writer, description, indent); if (dimension_opt) |dimension| { const name = try std.mem.replaceOwned(u8, db.arena.allocator(), cluster.name, "[%s]", ""); @@ -762,12 +799,9 @@ fn genZigFields( } // TODO: default values? - if (field.description) |description| { - if (!isUselessDescription(description)) { - try writer.writeByteNTimes(' ', indent * 4); - try writer.print("/// {s}\n", .{description}); - } - } + if (field.description) |description| + if (!isUselessDescription(description)) + try writeDescription(self.arena.child_allocator, writer, description, indent); try writer.writeByteNTimes(' ', indent * 4); try writer.print("{s}: u{},\n", .{ std.zig.fmtId(field.name), field.width }); @@ -828,10 +862,8 @@ fn genZigRegister( try writer.print("/// address: 0x{x}\n", .{base_addr + addr_offset}); } - if (register.description) |description| { - try writer.writeByteNTimes(' ', indent * 4); - try writer.print("/// {s}\n", .{description}); - } + if (register.description) |description| + try writeDescription(self.arena.child_allocator, writer, description, indent); try self.genZigSingleRegister( writer, @@ -865,10 +897,8 @@ fn genZigRegister( try writer.print("/// address: 0x{x}\n", .{base_addr + addr_offset}); } - if (register.description) |description| { - try writer.writeByteNTimes(' ', indent * 4); - try writer.print("/// {s}\n", .{description}); - } + if (register.description) |description| + try writeDescription(self.arena.child_allocator, writer, description, indent); try self.genZigSingleRegister( writer, @@ -923,10 +953,8 @@ fn genZigRegister( try writer.print("/// address: 0x{x}\n", .{base_addr + register.addr_offset}); } - if (register.description) |description| { - try writer.writeByteNTimes(' ', indent * 4); - try writer.print("/// {s}\n", .{description}); - } + if (register.description) |description| + try writeDescription(self.arena.child_allocator, writer, description, indent); try self.genZigSingleRegister( writer, diff --git a/tools/regz/src/mmio.zig b/tools/regz/src/mmio.zig index c759566..32c0254 100644 --- a/tools/regz/src/mmio.zig +++ b/tools/regz/src/mmio.zig @@ -82,6 +82,10 @@ const InterruptVector = extern union { // Interrupt is not supported on arm }; -fn unhandled() callconv(.C) noreturn { - @panic("unhandled interrupt"); -} +const unhandled = InterruptVector{ + .C = struct { + fn tmp() callconv(.C) noreturn { + @panic("unhandled interrupt"); + } + }.tmp, +}; diff --git a/tools/regz/src/xml.zig b/tools/regz/src/xml.zig index 03324ed..40dfd30 100644 --- a/tools/regz/src/xml.zig +++ b/tools/regz/src/xml.zig @@ -56,19 +56,10 @@ pub fn findValueForKey(node: ?*Node, key: []const u8) ?[]const u8 { } pub fn parseDescription(allocator: Allocator, node: ?*Node, key: []const u8) !?[]const u8 { - return if (findValueForKey(node, key)) |value| blk: { - var str = std.ArrayList(u8).init(allocator); - errdefer str.deinit(); - - var it = std.mem.tokenize(u8, value, " \n\t\r"); - try str.appendSlice(it.next() orelse return null); - while (it.next()) |token| { - try str.append(' '); - try str.appendSlice(token); - } - - break :blk str.toOwnedSlice(); - } else null; + return if (findValueForKey(node, key)) |value| + try allocator.dupe(u8, value) + else + null; } pub fn parseIntForKey(comptime T: type, allocator: std.mem.Allocator, node: ?*Node, key: []const u8) !?T {