1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
|
//! This file contains logic for bit-casting arbitrary values at comptime, including splicing
//! bits together for comptime stores of bit-pointers. The strategy is to "flatten" values to
//! a sequence of values in *packed* memory, and then unflatten through a combination of special
//! cases (particularly for pointers and `undefined` values) and in-memory buffer reinterprets.
//!
//! This is a little awkward on big-endian targets, as non-packed datastructures (e.g. `extern struct`)
//! have their fields reversed when represented as packed memory on such targets.
/// If `host_bits` is `0`, attempts to convert the memory at offset
/// `byte_offset` into `val` to a non-packed value of type `dest_ty`,
/// ignoring `bit_offset`.
///
/// Otherwise, `byte_offset` is an offset in bytes into `val` to a
/// non-packed value consisting of `host_bits` bits. A value of type
/// `dest_ty` will be interpreted at a packed offset of `bit_offset`
/// into this value.
///
/// Returns `null` if the operation must be performed at runtime.
pub fn bitCast(
sema: *Sema,
val: Value,
dest_ty: Type,
byte_offset: u64,
host_bits: u64,
bit_offset: u64,
) CompileError!?Value {
return bitCastInner(sema, val, dest_ty, byte_offset, host_bits, bit_offset) catch |err| switch (err) {
error.ReinterpretDeclRef => return null,
error.IllDefinedMemoryLayout => unreachable,
error.Unimplemented => @panic("unimplemented bitcast"),
else => |e| return e,
};
}
/// Uses bitcasting to splice the value `splice_val` into `val`,
/// replacing overlapping bits and returning the modified value.
///
/// If `host_bits` is `0`, splices `splice_val` at an offset
/// `byte_offset` bytes into the virtual memory of `val`, ignoring
/// `bit_offset`.
///
/// Otherwise, `byte_offset` is an offset into bytes into `val` to
/// a non-packed value consisting of `host_bits` bits. The value
/// `splice_val` will be placed at a packed offset of `bit_offset`
/// into this value.
pub fn bitCastSplice(
sema: *Sema,
val: Value,
splice_val: Value,
byte_offset: u64,
host_bits: u64,
bit_offset: u64,
) CompileError!?Value {
return bitCastSpliceInner(sema, val, splice_val, byte_offset, host_bits, bit_offset) catch |err| switch (err) {
error.ReinterpretDeclRef => return null,
error.IllDefinedMemoryLayout => unreachable,
error.Unimplemented => @panic("unimplemented bitcast"),
else => |e| return e,
};
}
const BitCastError = CompileError || error{ ReinterpretDeclRef, IllDefinedMemoryLayout, Unimplemented };
fn bitCastInner(
sema: *Sema,
val: Value,
dest_ty: Type,
byte_offset: u64,
host_bits: u64,
bit_offset: u64,
) BitCastError!Value {
const pt = sema.pt;
const zcu = pt.zcu;
const endian = zcu.getTarget().cpu.arch.endian();
if (dest_ty.toIntern() == val.typeOf(zcu).toIntern() and bit_offset == 0) {
return val;
}
const val_ty = val.typeOf(zcu);
try val_ty.resolveLayout(pt);
try dest_ty.resolveLayout(pt);
assert(val_ty.hasWellDefinedLayout(zcu));
const abi_pad_bits, const host_pad_bits = if (host_bits > 0)
.{ val_ty.abiSize(zcu) * 8 - host_bits, host_bits - val_ty.bitSize(zcu) }
else
.{ val_ty.abiSize(zcu) * 8 - val_ty.bitSize(zcu), 0 };
const skip_bits = switch (endian) {
.little => bit_offset + byte_offset * 8,
.big => if (host_bits > 0)
val_ty.abiSize(zcu) * 8 - byte_offset * 8 - host_bits + bit_offset
else
val_ty.abiSize(zcu) * 8 - byte_offset * 8 - dest_ty.bitSize(zcu),
};
var unpack: UnpackValueBits = .{
.pt = sema.pt,
.arena = sema.arena,
.skip_bits = skip_bits,
.remaining_bits = dest_ty.bitSize(zcu),
.unpacked = std.array_list.Managed(InternPool.Index).init(sema.arena),
};
switch (endian) {
.little => {
try unpack.add(val);
try unpack.padding(abi_pad_bits);
},
.big => {
try unpack.padding(abi_pad_bits);
try unpack.add(val);
},
}
try unpack.padding(host_pad_bits);
var pack: PackValueBits = .{
.pt = sema.pt,
.arena = sema.arena,
.unpacked = unpack.unpacked.items,
};
return pack.get(dest_ty);
}
fn bitCastSpliceInner(
sema: *Sema,
val: Value,
splice_val: Value,
byte_offset: u64,
host_bits: u64,
bit_offset: u64,
) BitCastError!Value {
const pt = sema.pt;
const zcu = pt.zcu;
const endian = zcu.getTarget().cpu.arch.endian();
const val_ty = val.typeOf(zcu);
const splice_val_ty = splice_val.typeOf(zcu);
try val_ty.resolveLayout(pt);
try splice_val_ty.resolveLayout(pt);
const splice_bits = splice_val_ty.bitSize(zcu);
const splice_offset = switch (endian) {
.little => bit_offset + byte_offset * 8,
.big => if (host_bits > 0)
val_ty.abiSize(zcu) * 8 - byte_offset * 8 - host_bits + bit_offset
else
val_ty.abiSize(zcu) * 8 - byte_offset * 8 - splice_bits,
};
assert(splice_offset + splice_bits <= val_ty.abiSize(zcu) * 8);
const abi_pad_bits, const host_pad_bits = if (host_bits > 0)
.{ val_ty.abiSize(zcu) * 8 - host_bits, host_bits - val_ty.bitSize(zcu) }
else
.{ val_ty.abiSize(zcu) * 8 - val_ty.bitSize(zcu), 0 };
var unpack: UnpackValueBits = .{
.pt = pt,
.arena = sema.arena,
.skip_bits = 0,
.remaining_bits = splice_offset,
.unpacked = std.array_list.Managed(InternPool.Index).init(sema.arena),
};
switch (endian) {
.little => {
try unpack.add(val);
try unpack.padding(abi_pad_bits);
},
.big => {
try unpack.padding(abi_pad_bits);
try unpack.add(val);
},
}
try unpack.padding(host_pad_bits);
unpack.remaining_bits = splice_bits;
try unpack.add(splice_val);
unpack.skip_bits = splice_offset + splice_bits;
unpack.remaining_bits = val_ty.abiSize(zcu) * 8 - splice_offset - splice_bits;
switch (endian) {
.little => {
try unpack.add(val);
try unpack.padding(abi_pad_bits);
},
.big => {
try unpack.padding(abi_pad_bits);
try unpack.add(val);
},
}
try unpack.padding(host_pad_bits);
var pack: PackValueBits = .{
.pt = pt,
.arena = sema.arena,
.unpacked = unpack.unpacked.items,
};
switch (endian) {
.little => {},
.big => try pack.padding(abi_pad_bits),
}
return pack.get(val_ty);
}
/// Recurses through struct fields, array elements, etc, to get a sequence of "primitive" values
/// which are bit-packed in memory to represent a single value. `unpacked` represents a series
/// of values in *packed* memory - therefore, on big-endian targets, the first element of this
/// list contains bits from the *final* byte of the value.
const UnpackValueBits = struct {
pt: Zcu.PerThread,
arena: Allocator,
skip_bits: u64,
remaining_bits: u64,
extra_bits: u64 = undefined,
unpacked: std.array_list.Managed(InternPool.Index),
fn add(unpack: *UnpackValueBits, val: Value) BitCastError!void {
const pt = unpack.pt;
const zcu = pt.zcu;
const endian = zcu.getTarget().cpu.arch.endian();
const ip = &zcu.intern_pool;
if (unpack.remaining_bits == 0) {
return;
}
const ty = val.typeOf(zcu);
const bit_size = ty.bitSize(zcu);
if (unpack.skip_bits >= bit_size) {
unpack.skip_bits -= bit_size;
return;
}
switch (ip.indexToKey(val.toIntern())) {
.int_type,
.ptr_type,
.array_type,
.vector_type,
.opt_type,
.anyframe_type,
.error_union_type,
.simple_type,
.struct_type,
.tuple_type,
.union_type,
.opaque_type,
.enum_type,
.func_type,
.error_set_type,
.inferred_error_set_type,
.variable,
.@"extern",
.func,
.err,
.error_union,
.enum_literal,
.slice,
.memoized_call,
=> unreachable, // ill-defined layout or not real values
.undef,
.int,
.enum_tag,
.simple_value,
.empty_enum_value,
.float,
.ptr,
.opt,
=> try unpack.primitive(val),
.aggregate => switch (ty.zigTypeTag(zcu)) {
.vector => {
const len: usize = @intCast(ty.arrayLen(zcu));
for (0..len) |i| {
// We reverse vector elements in packed memory on BE targets.
const real_idx = switch (endian) {
.little => i,
.big => len - i - 1,
};
const elem_val = try val.elemValue(pt, real_idx);
try unpack.add(elem_val);
}
},
.array => {
// Each element is padded up to its ABI size. Padding bits are undefined.
// The final element does not have trailing padding.
// Elements are reversed in packed memory on BE targets.
const elem_ty = ty.childType(zcu);
const pad_bits = elem_ty.abiSize(zcu) * 8 - elem_ty.bitSize(zcu);
const len = ty.arrayLen(zcu);
const maybe_sent = ty.sentinel(zcu);
if (endian == .big) if (maybe_sent) |s| {
try unpack.add(s);
if (len != 0) try unpack.padding(pad_bits);
};
for (0..@intCast(len)) |i| {
// We reverse array elements in packed memory on BE targets.
const real_idx = switch (endian) {
.little => i,
.big => len - i - 1,
};
const elem_val = try val.elemValue(pt, @intCast(real_idx));
try unpack.add(elem_val);
if (i != len - 1) try unpack.padding(pad_bits);
}
if (endian == .little) if (maybe_sent) |s| {
if (len != 0) try unpack.padding(pad_bits);
try unpack.add(s);
};
},
.@"struct" => switch (ty.containerLayout(zcu)) {
.auto => unreachable, // ill-defined layout
.@"extern" => switch (endian) {
.little => {
var cur_bit_off: u64 = 0;
var it = zcu.typeToStruct(ty).?.iterateRuntimeOrder(ip);
while (it.next()) |field_idx| {
const want_bit_off = ty.structFieldOffset(field_idx, zcu) * 8;
const pad_bits = want_bit_off - cur_bit_off;
const field_val = try val.fieldValue(pt, field_idx);
try unpack.padding(pad_bits);
try unpack.add(field_val);
cur_bit_off = want_bit_off + field_val.typeOf(zcu).bitSize(zcu);
}
// Add trailing padding bits.
try unpack.padding(bit_size - cur_bit_off);
},
.big => {
var cur_bit_off: u64 = bit_size;
var it = zcu.typeToStruct(ty).?.iterateRuntimeOrderReverse(ip);
while (it.next()) |field_idx| {
const field_val = try val.fieldValue(pt, field_idx);
const field_ty = field_val.typeOf(zcu);
const want_bit_off = ty.structFieldOffset(field_idx, zcu) * 8 + field_ty.bitSize(zcu);
const pad_bits = cur_bit_off - want_bit_off;
try unpack.padding(pad_bits);
try unpack.add(field_val);
cur_bit_off = want_bit_off - field_ty.bitSize(zcu);
}
assert(cur_bit_off == 0);
},
},
.@"packed" => {
// Just add all fields in order. There are no padding bits.
// This is identical between LE and BE targets.
for (0..ty.structFieldCount(zcu)) |i| {
const field_val = try val.fieldValue(pt, i);
try unpack.add(field_val);
}
},
},
else => unreachable,
},
.un => |un| {
// We actually don't care about the tag here!
// Instead, we just need to write the payload value, plus any necessary padding.
// This correctly handles the case where `tag == .none`, since the payload is then
// either an integer or a byte array, both of which we can unpack.
const payload_val = Value.fromInterned(un.val);
const pad_bits = bit_size - payload_val.typeOf(zcu).bitSize(zcu);
if (endian == .little or ty.containerLayout(zcu) == .@"packed") {
try unpack.add(payload_val);
try unpack.padding(pad_bits);
} else {
try unpack.padding(pad_bits);
try unpack.add(payload_val);
}
},
}
}
fn padding(unpack: *UnpackValueBits, pad_bits: u64) BitCastError!void {
if (pad_bits == 0) return;
const pt = unpack.pt;
// Figure out how many full bytes and leftover bits there are.
const bytes = pad_bits / 8;
const bits = pad_bits % 8;
// Add undef u8 values for the bytes...
const undef_u8 = try pt.undefValue(Type.u8);
for (0..@intCast(bytes)) |_| {
try unpack.primitive(undef_u8);
}
// ...and an undef int for the leftover bits.
if (bits == 0) return;
const bits_ty = try pt.intType(.unsigned, @intCast(bits));
const bits_val = try pt.undefValue(bits_ty);
try unpack.primitive(bits_val);
}
fn primitive(unpack: *UnpackValueBits, val: Value) BitCastError!void {
const pt = unpack.pt;
const zcu = pt.zcu;
if (unpack.remaining_bits == 0) {
return;
}
const ty = val.typeOf(pt.zcu);
const bit_size = ty.bitSize(zcu);
// Note that this skips all zero-bit types.
if (unpack.skip_bits >= bit_size) {
unpack.skip_bits -= bit_size;
return;
}
if (unpack.skip_bits > 0) {
const skip = unpack.skip_bits;
unpack.skip_bits = 0;
return unpack.splitPrimitive(val, skip, bit_size - skip);
}
if (unpack.remaining_bits < bit_size) {
return unpack.splitPrimitive(val, 0, unpack.remaining_bits);
}
unpack.remaining_bits -|= bit_size;
try unpack.unpacked.append(val.toIntern());
}
fn splitPrimitive(unpack: *UnpackValueBits, val: Value, bit_offset: u64, bit_count: u64) BitCastError!void {
const pt = unpack.pt;
const zcu = pt.zcu;
const ty = val.typeOf(pt.zcu);
const val_bits = ty.bitSize(zcu);
assert(bit_offset + bit_count <= val_bits);
switch (pt.zcu.intern_pool.indexToKey(val.toIntern())) {
// In the `ptr` case, this will return `error.ReinterpretDeclRef`
// if we're trying to split a non-integer pointer value.
.int, .float, .enum_tag, .ptr, .opt => {
// This @intCast is okay because no primitive can exceed the size of a u16.
const int_ty = try unpack.pt.intType(.unsigned, @intCast(bit_count));
const buf = try unpack.arena.alloc(u8, @intCast((val_bits + 7) / 8));
try val.writeToPackedMemory(ty, unpack.pt, buf, 0);
const sub_val = try Value.readFromPackedMemory(int_ty, unpack.pt, buf, @intCast(bit_offset), unpack.arena);
try unpack.primitive(sub_val);
},
.undef => try unpack.padding(bit_count),
// The only values here with runtime bits are `true` and `false.
// These are both 1 bit, so will never need truncating.
.simple_value => unreachable,
.empty_enum_value => unreachable, // zero-bit
else => unreachable, // zero-bit or not primitives
}
}
};
/// Given a sequence of bit-packed values in packed memory (see `UnpackValueBits`),
/// reconstructs a value of an arbitrary type, with correct handling of `undefined`
/// values and of pointers which align in virtual memory.
const PackValueBits = struct {
pt: Zcu.PerThread,
arena: Allocator,
bit_offset: u64 = 0,
unpacked: []const InternPool.Index,
fn get(pack: *PackValueBits, ty: Type) BitCastError!Value {
const pt = pack.pt;
const zcu = pt.zcu;
const endian = zcu.getTarget().cpu.arch.endian();
const ip = &zcu.intern_pool;
const arena = pack.arena;
switch (ty.zigTypeTag(zcu)) {
.vector => {
// Elements are bit-packed.
const len = ty.arrayLen(zcu);
const elem_ty = ty.childType(zcu);
const elems = try arena.alloc(InternPool.Index, @intCast(len));
// We reverse vector elements in packed memory on BE targets.
switch (endian) {
.little => for (elems) |*elem| {
elem.* = (try pack.get(elem_ty)).toIntern();
},
.big => {
var i = elems.len;
while (i > 0) {
i -= 1;
elems[i] = (try pack.get(elem_ty)).toIntern();
}
},
}
return pt.aggregateValue(ty, elems);
},
.array => {
// Each element is padded up to its ABI size. The final element does not have trailing padding.
const len = ty.arrayLen(zcu);
const elem_ty = ty.childType(zcu);
const maybe_sent = ty.sentinel(zcu);
const pad_bits = elem_ty.abiSize(zcu) * 8 - elem_ty.bitSize(zcu);
const elems = try arena.alloc(InternPool.Index, @intCast(len));
if (endian == .big and maybe_sent != null) {
// TODO: validate sentinel was preserved!
try pack.padding(elem_ty.bitSize(zcu));
if (len != 0) try pack.padding(pad_bits);
}
for (0..elems.len) |i| {
const real_idx = switch (endian) {
.little => i,
.big => len - i - 1,
};
elems[@intCast(real_idx)] = (try pack.get(elem_ty)).toIntern();
if (i != len - 1) try pack.padding(pad_bits);
}
if (endian == .little and maybe_sent != null) {
// TODO: validate sentinel was preserved!
if (len != 0) try pack.padding(pad_bits);
try pack.padding(elem_ty.bitSize(zcu));
}
return pt.aggregateValue(ty, elems);
},
.@"struct" => switch (ty.containerLayout(zcu)) {
.auto => unreachable, // ill-defined layout
.@"extern" => {
const elems = try arena.alloc(InternPool.Index, ty.structFieldCount(zcu));
@memset(elems, .none);
switch (endian) {
.little => {
var cur_bit_off: u64 = 0;
var it = zcu.typeToStruct(ty).?.iterateRuntimeOrder(ip);
while (it.next()) |field_idx| {
const want_bit_off = ty.structFieldOffset(field_idx, zcu) * 8;
try pack.padding(want_bit_off - cur_bit_off);
const field_ty = ty.fieldType(field_idx, zcu);
elems[field_idx] = (try pack.get(field_ty)).toIntern();
cur_bit_off = want_bit_off + field_ty.bitSize(zcu);
}
try pack.padding(ty.bitSize(zcu) - cur_bit_off);
},
.big => {
var cur_bit_off: u64 = ty.bitSize(zcu);
var it = zcu.typeToStruct(ty).?.iterateRuntimeOrderReverse(ip);
while (it.next()) |field_idx| {
const field_ty = ty.fieldType(field_idx, zcu);
const want_bit_off = ty.structFieldOffset(field_idx, zcu) * 8 + field_ty.bitSize(zcu);
try pack.padding(cur_bit_off - want_bit_off);
elems[field_idx] = (try pack.get(field_ty)).toIntern();
cur_bit_off = want_bit_off - field_ty.bitSize(zcu);
}
assert(cur_bit_off == 0);
},
}
// Any fields which do not have runtime bits should be OPV or comptime fields.
// Fill those values now.
for (elems, 0..) |*elem, field_idx| {
if (elem.* != .none) continue;
const val = (try ty.structFieldValueComptime(pt, field_idx)).?;
elem.* = val.toIntern();
}
return pt.aggregateValue(ty, elems);
},
.@"packed" => {
// All fields are in order with no padding.
// This is identical between LE and BE targets.
const elems = try arena.alloc(InternPool.Index, ty.structFieldCount(zcu));
for (elems, 0..) |*elem, i| {
const field_ty = ty.fieldType(i, zcu);
elem.* = (try pack.get(field_ty)).toIntern();
}
return pt.aggregateValue(ty, elems);
},
},
.@"union" => {
// We will attempt to read as the backing representation. If this emits
// `error.ReinterpretDeclRef`, we will try each union field, preferring larger ones.
// We will also attempt smaller fields when we get `undefined`, as if some bits are
// defined we want to include them.
// TODO: this is very very bad. We need a more sophisticated union representation.
const prev_unpacked = pack.unpacked;
const prev_bit_offset = pack.bit_offset;
const backing_ty = try ty.unionBackingType(pt);
backing: {
const backing_val = pack.get(backing_ty) catch |err| switch (err) {
error.ReinterpretDeclRef => {
pack.unpacked = prev_unpacked;
pack.bit_offset = prev_bit_offset;
break :backing;
},
else => |e| return e,
};
if (backing_val.isUndef(zcu)) {
pack.unpacked = prev_unpacked;
pack.bit_offset = prev_bit_offset;
break :backing;
}
return Value.fromInterned(try pt.internUnion(.{
.ty = ty.toIntern(),
.tag = .none,
.val = backing_val.toIntern(),
}));
}
const field_order = try pack.arena.alloc(u32, ty.unionTagTypeHypothetical(zcu).enumFieldCount(zcu));
for (field_order, 0..) |*f, i| f.* = @intCast(i);
// Sort `field_order` to put the fields with the largest bit sizes first.
const SizeSortCtx = struct {
zcu: *Zcu,
field_types: []const InternPool.Index,
fn lessThan(ctx: @This(), a_idx: u32, b_idx: u32) bool {
const a_ty = Type.fromInterned(ctx.field_types[a_idx]);
const b_ty = Type.fromInterned(ctx.field_types[b_idx]);
return a_ty.bitSize(ctx.zcu) > b_ty.bitSize(ctx.zcu);
}
};
std.mem.sortUnstable(u32, field_order, SizeSortCtx{
.zcu = zcu,
.field_types = zcu.typeToUnion(ty).?.field_types.get(ip),
}, SizeSortCtx.lessThan);
const padding_after = endian == .little or ty.containerLayout(zcu) == .@"packed";
for (field_order) |field_idx| {
const field_ty = Type.fromInterned(zcu.typeToUnion(ty).?.field_types.get(ip)[field_idx]);
const pad_bits = ty.bitSize(zcu) - field_ty.bitSize(zcu);
if (!padding_after) try pack.padding(pad_bits);
const field_val = pack.get(field_ty) catch |err| switch (err) {
error.ReinterpretDeclRef => {
pack.unpacked = prev_unpacked;
pack.bit_offset = prev_bit_offset;
continue;
},
else => |e| return e,
};
if (padding_after) try pack.padding(pad_bits);
if (field_val.isUndef(zcu)) {
pack.unpacked = prev_unpacked;
pack.bit_offset = prev_bit_offset;
continue;
}
const tag_val = try pt.enumValueFieldIndex(ty.unionTagTypeHypothetical(zcu), field_idx);
return Value.fromInterned(try pt.internUnion(.{
.ty = ty.toIntern(),
.tag = tag_val.toIntern(),
.val = field_val.toIntern(),
}));
}
// No field could represent the value. Just do whatever happens when we try to read
// the backing type - either `undefined` or `error.ReinterpretDeclRef`.
const backing_val = try pack.get(backing_ty);
return Value.fromInterned(try pt.internUnion(.{
.ty = ty.toIntern(),
.tag = .none,
.val = backing_val.toIntern(),
}));
},
else => return pack.primitive(ty),
}
}
fn padding(pack: *PackValueBits, pad_bits: u64) BitCastError!void {
_ = pack.prepareBits(pad_bits);
}
fn primitive(pack: *PackValueBits, want_ty: Type) BitCastError!Value {
const pt = pack.pt;
const zcu = pt.zcu;
const vals, const bit_offset = pack.prepareBits(want_ty.bitSize(zcu));
for (vals) |val| {
if (!Value.fromInterned(val).isUndef(zcu)) break;
} else {
// All bits of the value are `undefined`.
return pt.undefValue(want_ty);
}
// TODO: we need to decide how to handle partially-undef values here.
// Currently, a value with some undefined bits becomes `0xAA` so that we
// preserve the well-defined bits, because we can't currently represent
// a partially-undefined primitive (e.g. an int with some undef bits).
// In future, we probably want to take one of these two routes:
// * Define that if any bits are `undefined`, the entire value is `undefined`.
// This is a major breaking change, and probably a footgun.
// * Introduce tracking for partially-undef values at comptime.
// This would complicate a lot of operations in Sema, such as basic
// arithmetic.
// This design complexity is tracked by #19634.
ptr_cast: {
if (vals.len != 1) break :ptr_cast;
const val = Value.fromInterned(vals[0]);
if (!val.typeOf(zcu).isPtrAtRuntime(zcu)) break :ptr_cast;
if (!want_ty.isPtrAtRuntime(zcu)) break :ptr_cast;
return pt.getCoerced(val, want_ty);
}
// Reinterpret via an in-memory buffer.
var buf_bits: u64 = 0;
for (vals) |ip_val| {
const val = Value.fromInterned(ip_val);
const ty = val.typeOf(pt.zcu);
buf_bits += ty.bitSize(zcu);
}
const buf = try pack.arena.alloc(u8, @intCast((buf_bits + 7) / 8));
// We will skip writing undefined values, so mark the buffer as `0xAA` so we get "undefined" bits.
@memset(buf, 0xAA);
var cur_bit_off: usize = 0;
for (vals) |ip_val| {
const val = Value.fromInterned(ip_val);
const ty = val.typeOf(zcu);
if (!val.isUndef(zcu)) {
try val.writeToPackedMemory(ty, pt, buf, cur_bit_off);
}
cur_bit_off += @intCast(ty.bitSize(zcu));
}
return Value.readFromPackedMemory(want_ty, pt, buf, @intCast(bit_offset), pack.arena);
}
fn prepareBits(pack: *PackValueBits, need_bits: u64) struct { []const InternPool.Index, u64 } {
if (need_bits == 0) return .{ &.{}, 0 };
const pt = pack.pt;
const zcu = pt.zcu;
var bits: u64 = 0;
var len: usize = 0;
while (bits < pack.bit_offset + need_bits) {
bits += Value.fromInterned(pack.unpacked[len]).typeOf(pt.zcu).bitSize(zcu);
len += 1;
}
const result_vals = pack.unpacked[0..len];
const result_offset = pack.bit_offset;
const extra_bits = bits - pack.bit_offset - need_bits;
if (extra_bits == 0) {
pack.unpacked = pack.unpacked[len..];
pack.bit_offset = 0;
} else {
pack.unpacked = pack.unpacked[len - 1 ..];
pack.bit_offset = Value.fromInterned(pack.unpacked[0]).typeOf(pt.zcu).bitSize(zcu) - extra_bits;
}
return .{ result_vals, result_offset };
}
};
const std = @import("std");
const Allocator = std.mem.Allocator;
const assert = std.debug.assert;
const Sema = @import("../Sema.zig");
const Zcu = @import("../Zcu.zig");
const InternPool = @import("../InternPool.zig");
const Type = @import("../Type.zig");
const Value = @import("../Value.zig");
const CompileError = Zcu.CompileError;
|