motiejus/zig

fork of https://codeberg.org/ziglang/zig
git clone https://git.jakstys.lt/motiejus/zig.git
Log | Tree | Refs | README | LICENSE

commit edb6486b3bf7a1c333d7cc3348f88ab121b72830 (tree)
parent 7e9f321f53b53dcefaf3b771720c9c25529c39ef
Author: Jacob Young <jacobly0@users.noreply.github.com>
Date:   Sat, 24 Feb 2024 16:59:00 +0100

BitcodeWriter: cleanup type widths

Diffstat:
Msrc/codegen/llvm/Builder.zig | 2+-
Msrc/codegen/llvm/bitcode_writer.zig | 20++++++++------------
2 files changed, 9 insertions(+), 13 deletions(-)

diff --git a/src/codegen/llvm/Builder.zig b/src/codegen/llvm/Builder.zig @@ -12944,7 +12944,7 @@ fn debugConstantAssumeCapacity(self: *Builder, constant: Constant) Metadata { pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]const u32 { const BitcodeWriter = bitcode_writer.BitcodeWriter(&.{ Type, FunctionAttributes }); - var bitcode = BitcodeWriter.init(allocator, &.{ + var bitcode = BitcodeWriter.init(allocator, .{ std.math.log2_int_ceil(usize, self.type_items.items.len), std.math.log2_int_ceil(usize, 1 + self.function_attributes_set.count()), }); diff --git a/src/codegen/llvm/bitcode_writer.zig b/src/codegen/llvm/bitcode_writer.zig @@ -23,17 +23,13 @@ pub fn BitcodeWriter(comptime types: []const type) type { bit_buffer: u32 = 0, bit_count: u5 = 0, - widths: []const u16, + widths: [types.len]u16, - pub fn getTypeIndex(comptime ty: type) usize { - inline for (types, 0..) |t, i| { - if (t == ty) return i; - } - unreachable; + pub fn getTypeWidth(self: BcWriter, comptime Type: type) u16 { + return self.widths[comptime std.mem.indexOfScalar(type, types, Type).?]; } - pub fn init(allocator: std.mem.Allocator, widths: []const u16) BcWriter { - std.debug.assert(widths.len == types.len); + pub fn init(allocator: std.mem.Allocator, widths: [types.len]u16) BcWriter { return .{ .buffer = std.ArrayList(u32).init(allocator), .widths = widths, @@ -250,7 +246,7 @@ pub fn BitcodeWriter(comptime types: []const type) type { .fixed => |len| try self.bitcode.writeBits(adapter.get(param, field_name), len), .fixed_runtime => |width_ty| try self.bitcode.writeBits( adapter.get(param, field_name), - self.bitcode.widths[getTypeIndex(width_ty)], + self.bitcode.getTypeWidth(width_ty), ), .vbr => |len| try self.bitcode.writeVBR(adapter.get(param, field_name), len), .char6 => try self.bitcode.write6BitChar(adapter.get(param, field_name)), @@ -273,7 +269,7 @@ pub fn BitcodeWriter(comptime types: []const type) type { for (param) |x| { try self.bitcode.writeBits( adapter.get(x, field_name), - self.bitcode.widths[getTypeIndex(width_ty)], + self.bitcode.getTypeWidth(width_ty), ); } }, @@ -324,7 +320,7 @@ pub fn BitcodeWriter(comptime types: []const type) type { .fixed_runtime => |width_ty| { try bitcode.writeBits(0, 1); try bitcode.writeBits(1, 3); - try bitcode.writeVBR(bitcode.widths[getTypeIndex(width_ty)], 5); + try bitcode.writeVBR(bitcode.getTypeWidth(width_ty), 5); }, .vbr => |width| { try bitcode.writeBits(0, 1); @@ -357,7 +353,7 @@ pub fn BitcodeWriter(comptime types: []const type) type { // Fixed or VBR op try bitcode.writeBits(0, 1); try bitcode.writeBits(1, 3); - try bitcode.writeVBR(bitcode.widths[getTypeIndex(width_ty)], 5); + try bitcode.writeVBR(bitcode.getTypeWidth(width_ty), 5); }, .array_vbr => |width| { // Array op