| ... | ... | @@ -23,17 +23,13 @@ pub fn BitcodeWriter(comptime types: []const type) type { |
| 23 | 23 | bit_buffer: u32 = 0, |
| 24 | 24 | bit_count: u5 = 0, |
| 25 | 25 | |
| 26 | | widths: []const u16, |
| 26 | widths: [types.len]u16, |
| 27 | 27 | |
| 28 | | pub fn getTypeIndex(comptime ty: type) usize { |
| 29 | | inline for (types, 0..) |t, i| { |
| 30 | | if (t == ty) return i; |
| 31 | | } |
| 32 | | unreachable; |
| 28 | pub fn getTypeWidth(self: BcWriter, comptime Type: type) u16 { |
| 29 | return self.widths[comptime std.mem.indexOfScalar(type, types, Type).?]; |
| 33 | 30 | } |
| 34 | 31 | |
| 35 | | pub fn init(allocator: std.mem.Allocator, widths: []const u16) BcWriter { |
| 36 | | std.debug.assert(widths.len == types.len); |
| 32 | pub fn init(allocator: std.mem.Allocator, widths: [types.len]u16) BcWriter { |
| 37 | 33 | return .{ |
| 38 | 34 | .buffer = std.ArrayList(u32).init(allocator), |
| 39 | 35 | .widths = widths, |
| ... | ... | @@ -250,7 +246,7 @@ pub fn BitcodeWriter(comptime types: []const type) type { |
| 250 | 246 | .fixed => |len| try self.bitcode.writeBits(adapter.get(param, field_name), len), |
| 251 | 247 | .fixed_runtime => |width_ty| try self.bitcode.writeBits( |
| 252 | 248 | adapter.get(param, field_name), |
| 253 | | self.bitcode.widths[getTypeIndex(width_ty)], |
| 249 | self.bitcode.getTypeWidth(width_ty), |
| 254 | 250 | ), |
| 255 | 251 | .vbr => |len| try self.bitcode.writeVBR(adapter.get(param, field_name), len), |
| 256 | 252 | .char6 => try self.bitcode.write6BitChar(adapter.get(param, field_name)), |
| ... | ... | @@ -273,7 +269,7 @@ pub fn BitcodeWriter(comptime types: []const type) type { |
| 273 | 269 | for (param) |x| { |
| 274 | 270 | try self.bitcode.writeBits( |
| 275 | 271 | adapter.get(x, field_name), |
| 276 | | self.bitcode.widths[getTypeIndex(width_ty)], |
| 272 | self.bitcode.getTypeWidth(width_ty), |
| 277 | 273 | ); |
| 278 | 274 | } |
| 279 | 275 | }, |
| ... | ... | @@ -324,7 +320,7 @@ pub fn BitcodeWriter(comptime types: []const type) type { |
| 324 | 320 | .fixed_runtime => |width_ty| { |
| 325 | 321 | try bitcode.writeBits(0, 1); |
| 326 | 322 | try bitcode.writeBits(1, 3); |
| 327 | | try bitcode.writeVBR(bitcode.widths[getTypeIndex(width_ty)], 5); |
| 323 | try bitcode.writeVBR(bitcode.getTypeWidth(width_ty), 5); |
| 328 | 324 | }, |
| 329 | 325 | .vbr => |width| { |
| 330 | 326 | try bitcode.writeBits(0, 1); |
| ... | ... | @@ -357,7 +353,7 @@ pub fn BitcodeWriter(comptime types: []const type) type { |
| 357 | 353 | // Fixed or VBR op |
| 358 | 354 | try bitcode.writeBits(0, 1); |
| 359 | 355 | try bitcode.writeBits(1, 3); |
| 360 | | try bitcode.writeVBR(bitcode.widths[getTypeIndex(width_ty)], 5); |
| 356 | try bitcode.writeVBR(bitcode.getTypeWidth(width_ty), 5); |
| 361 | 357 | }, |
| 362 | 358 | .array_vbr => |width| { |
| 363 | 359 | // Array op |