fn BlockWriter(comptime Block: type) type start: usizebitcode: *BcWriter165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391 fn BlockWriter(comptime Block: type) type {
return struct {
const Self = @This();
// The minimum abbrev id length based on the number of abbrevs present in the block
pub const abbrev_len = std.math.log2_int_ceil(
u6,
4 + (if (@hasDecl(Block, "abbrevs")) Block.abbrevs.len else 0),
);
start: usize,
bitcode: *BcWriter,
pub fn init(bitcode: *BcWriter, comptime parent_abbrev_len: u6, comptime define_abbrevs: bool) Error!Self {
try bitcode.writeBits(1, parent_abbrev_len);
try bitcode.writeVbr(Block.id, 8);
try bitcode.writeVbr(abbrev_len, 4);
try bitcode.alignTo32();
// We store the index of the block size and store a dummy value as the number of words in the block
const start = bitcode.length();
try bitcode.writeBits(0, 32);
var self = Self{
.start = start,
.bitcode = bitcode,
};
// Predefine all block abbrevs
if (define_abbrevs) {
inline for (Block.abbrevs) |Abbrev| {
try self.defineAbbrev(&Abbrev.ops);
}
}
return self;
}
pub fn enterSubBlock(self: Self, comptime SubBlock: type, comptime define_abbrevs: bool) Error!BlockWriter(SubBlock) {
return BlockWriter(SubBlock).init(self.bitcode, abbrev_len, define_abbrevs);
}
pub fn end(self: *Self) Error!void {
try self.bitcode.writeBits(0, abbrev_len);
try self.bitcode.alignTo32();
// Set the number of words in the block at the start of the block
self.bitcode.buffer.items[self.start] = std.mem.nativeToLittle(u32, @truncate(self.bitcode.length() - self.start - 1));
}
pub fn writeUnabbrev(self: *Self, code: u32, values: []const u64) Error!void {
try self.bitcode.writeBits(3, abbrev_len);
try self.bitcode.writeVbr(code, 6);
try self.bitcode.writeVbr(values.len, 6);
for (values) |val| {
try self.bitcode.writeVbr(val, 6);
}
}
pub fn writeAbbrev(self: *Self, params: anytype) Error!void {
return self.writeAbbrevAdapted(params, struct {
pub fn get(_: @This(), param: anytype) @TypeOf(param) {
return param;
}
}{});
}
pub fn abbrevId(comptime Abbrev: type) u32 {
inline for (Block.abbrevs, 0..) |abbrev, i| {
if (Abbrev == abbrev) return i + 4;
}
@compileError("Unknown abbrev: " ++ @typeName(Abbrev));
}
pub fn writeAbbrevAdapted(
self: *Self,
params: anytype,
adapter: anytype,
) Error!void {
const Abbrev = @TypeOf(params);
try self.bitcode.writeBits(comptime abbrevId(Abbrev), abbrev_len);
const field_names = @typeInfo(Abbrev).@"struct".field_names;
// This abbreviation might only contain literals
if (field_names.len == 0) return;
comptime var field_index: usize = 0;
inline for (Abbrev.ops) |ty| {
const param = @field(params, field_names[field_index]);
switch (ty) {
.literal => continue,
.fixed => |len| try self.bitcode.writeBits(adapter.get(param), len),
.fixed_runtime => |width_ty| try self.bitcode.writeBits(
adapter.get(param),
self.bitcode.getTypeWidth(width_ty),
),
.vbr => |len| try self.bitcode.writeVbr(adapter.get(param), len),
.char6 => try self.bitcode.write6BitChar(adapter.get(param)),
.blob => {
try self.bitcode.writeVbr(param.len, 6);
try self.bitcode.writeBlob(param);
},
.array_fixed => |len| {
try self.bitcode.writeVbr(param.len, 6);
for (param) |x| {
try self.bitcode.writeBits(adapter.get(x), len);
}
},
.array_fixed_runtime => |width_ty| {
try self.bitcode.writeVbr(param.len, 6);
for (param) |x| {
try self.bitcode.writeBits(
adapter.get(x),
self.bitcode.getTypeWidth(width_ty),
);
}
},
.array_vbr => |len| {
try self.bitcode.writeVbr(param.len, 6);
for (param) |x| {
try self.bitcode.writeVbr(adapter.get(x), len);
}
},
.array_char6 => {
try self.bitcode.writeVbr(param.len, 6);
for (param) |x| {
try self.bitcode.write6BitChar(adapter.get(x));
}
},
}
field_index += 1;
if (field_index == field_names.len) break;
}
}
pub fn defineAbbrev(self: *Self, comptime ops: []const AbbrevOp) Error!void {
const bitcode = self.bitcode;
try bitcode.writeBits(2, abbrev_len);
// ops.len is not accurate because arrays are actually two ops
try bitcode.writeVbr(blk: {
var count: usize = 0;
inline for (ops) |op| {
count += switch (op) {
.literal, .fixed, .fixed_runtime, .vbr, .char6, .blob => 1,
.array_fixed, .array_fixed_runtime, .array_vbr, .array_char6 => 2,
};
}
break :blk count;
}, 5);
inline for (ops) |op| {
switch (op) {
.literal => |value| {
try bitcode.writeBits(1, 1);
try bitcode.writeVbr(value, 8);
},
.fixed => |width| {
try bitcode.writeBits(0, 1);
try bitcode.writeBits(1, 3);
try bitcode.writeVbr(width, 5);
},
.fixed_runtime => |width_ty| {
try bitcode.writeBits(0, 1);
try bitcode.writeBits(1, 3);
try bitcode.writeVbr(bitcode.getTypeWidth(width_ty), 5);
},
.vbr => |width| {
try bitcode.writeBits(0, 1);
try bitcode.writeBits(2, 3);
try bitcode.writeVbr(width, 5);
},
.char6 => {
try bitcode.writeBits(0, 1);
try bitcode.writeBits(4, 3);
},
.blob => {
try bitcode.writeBits(0, 1);
try bitcode.writeBits(5, 3);
},
.array_fixed => |width| {
// Array op
try bitcode.writeBits(0, 1);
try bitcode.writeBits(3, 3);
// Fixed or VBR op
try bitcode.writeBits(0, 1);
try bitcode.writeBits(1, 3);
try bitcode.writeVbr(width, 5);
},
.array_fixed_runtime => |width_ty| {
// Array op
try bitcode.writeBits(0, 1);
try bitcode.writeBits(3, 3);
// Fixed or VBR op
try bitcode.writeBits(0, 1);
try bitcode.writeBits(1, 3);
try bitcode.writeVbr(bitcode.getTypeWidth(width_ty), 5);
},
.array_vbr => |width| {
// Array op
try bitcode.writeBits(0, 1);
try bitcode.writeBits(3, 3);
// Fixed or VBR op
try bitcode.writeBits(0, 1);
try bitcode.writeBits(2, 3);
try bitcode.writeVbr(width, 5);
},
.array_char6 => {
// Array op
try bitcode.writeBits(0, 1);
try bitcode.writeBits(3, 3);
// Char6 op
try bitcode.writeBits(0, 1);
try bitcode.writeBits(4, 3);
},
}
}
}
};
}