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