@@ -192,11 +192,12 @@ pub fn toBigIntAdvanced(
192
192
zcu : * Zcu ,
193
193
tid : strat .Tid (),
194
194
) Zcu .CompileError ! BigIntConst {
195
+ const ip = & zcu .intern_pool ;
195
196
return switch (val .toIntern ()) {
196
197
.bool_false = > BigIntMutable .init (& space .limbs , 0 ).toConst (),
197
198
.bool_true = > BigIntMutable .init (& space .limbs , 1 ).toConst (),
198
199
.null_value = > BigIntMutable .init (& space .limbs , 0 ).toConst (),
199
- else = > switch (zcu . intern_pool .indexToKey (val .toIntern ())) {
200
+ else = > switch (ip .indexToKey (val .toIntern ())) {
200
201
.int = > | int | switch (int .storage ) {
201
202
.u64 , .i64 , .big_int = > int .storage .toBigInt (space ),
202
203
.lazy_align , .lazy_size = > | ty | {
@@ -214,6 +215,7 @@ pub fn toBigIntAdvanced(
214
215
& space .limbs ,
215
216
(try val .getUnsignedIntInner (strat , zcu , tid )).? ,
216
217
).toConst (),
218
+ .err = > | err | BigIntMutable .init (& space .limbs , ip .getErrorValueIfExists (err .name ).? ).toConst (),
217
219
else = > unreachable ,
218
220
},
219
221
};
@@ -326,15 +328,11 @@ pub fn toBool(val: Value) bool {
326
328
};
327
329
}
328
330
329
- fn ptrHasIntAddr (val : Value , zcu : * Zcu ) bool {
330
- return zcu .intern_pool .getBackingAddrTag (val .toIntern ()).? == .int ;
331
- }
332
-
333
331
/// Write a Value's contents to `buffer`.
334
332
///
335
333
/// Asserts that buffer.len >= ty.abiSize(). The buffer is allowed to extend past
336
334
/// the end of the value in memory.
337
- pub fn writeToMemory (val : Value , ty : Type , pt : Zcu.PerThread , buffer : []u8 ) error {
335
+ pub fn writeToMemory (val : Value , pt : Zcu.PerThread , buffer : []u8 ) error {
338
336
ReinterpretDeclRef ,
339
337
IllDefinedMemoryLayout ,
340
338
Unimplemented ,
@@ -343,19 +341,25 @@ pub fn writeToMemory(val: Value, ty: Type, pt: Zcu.PerThread, buffer: []u8) erro
343
341
const zcu = pt .zcu ;
344
342
const target = zcu .getTarget ();
345
343
const endian = target .cpu .arch .endian ();
344
+ const ip = & zcu .intern_pool ;
345
+ const ty = val .typeOf (zcu );
346
346
if (val .isUndef (zcu )) {
347
347
const size : usize = @intCast (ty .abiSize (zcu ));
348
348
@memset (buffer [0.. size ], 0xaa );
349
349
return ;
350
350
}
351
- const ip = & zcu .intern_pool ;
352
351
switch (ty .zigTypeTag (zcu )) {
353
352
.Void = > {},
354
353
.Bool = > {
355
354
buffer [0 ] = @intFromBool (val .toBool ());
356
355
},
357
- .Int , .Enum = > {
358
- const int_info = ty .intInfo (zcu );
356
+ .Int , .Enum , .ErrorSet , .Pointer = > | tag | {
357
+ const int_ty = if (tag == .Pointer ) int_ty : {
358
+ if (ty .isSlice (zcu )) return error .IllDefinedMemoryLayout ;
359
+ if (ip .getBackingAddrTag (val .toIntern ()).? != .int ) return error .ReinterpretDeclRef ;
360
+ break :int_ty Type .usize ;
361
+ } else ty ;
362
+ const int_info = int_ty .intInfo (zcu );
359
363
const bits = int_info .bits ;
360
364
const byte_count : u16 = @intCast ((@as (u17 , bits ) + 7 ) / 8 );
361
365
@@ -379,7 +383,7 @@ pub fn writeToMemory(val: Value, ty: Type, pt: Zcu.PerThread, buffer: []u8) erro
379
383
var buf_off : usize = 0 ;
380
384
while (elem_i < len ) : (elem_i += 1 ) {
381
385
const elem_val = try val .elemValue (pt , elem_i );
382
- try elem_val .writeToMemory (elem_ty , pt , buffer [buf_off .. ]);
386
+ try elem_val .writeToMemory (pt , buffer [buf_off .. ]);
383
387
buf_off += elem_size ;
384
388
}
385
389
},
@@ -403,31 +407,14 @@ pub fn writeToMemory(val: Value, ty: Type, pt: Zcu.PerThread, buffer: []u8) erro
403
407
.elems = > | elems | elems [field_index ],
404
408
.repeated_elem = > | elem | elem ,
405
409
});
406
- const field_ty = Type .fromInterned (struct_type .field_types .get (ip )[field_index ]);
407
- try writeToMemory (field_val , field_ty , pt , buffer [off .. ]);
410
+ try writeToMemory (field_val , pt , buffer [off .. ]);
408
411
},
409
412
.@"packed" = > {
410
413
const byte_count = (@as (usize , @intCast (ty .bitSize (zcu ))) + 7 ) / 8 ;
411
414
return writeToPackedMemory (val , ty , pt , buffer [0.. byte_count ], 0 );
412
415
},
413
416
}
414
417
},
415
- .ErrorSet = > {
416
- const bits = zcu .errorSetBits ();
417
- const byte_count : u16 = @intCast ((@as (u17 , bits ) + 7 ) / 8 );
418
-
419
- const name = switch (ip .indexToKey (val .toIntern ())) {
420
- .err = > | err | err .name ,
421
- .error_union = > | error_union | error_union .val .err_name ,
422
- else = > unreachable ,
423
- };
424
- var bigint_buffer : BigIntSpace = undefined ;
425
- const bigint = BigIntMutable .init (
426
- & bigint_buffer .limbs ,
427
- ip .getErrorValueIfExists (name ).? ,
428
- ).toConst ();
429
- bigint .writeTwosComplement (buffer [0.. byte_count ], endian );
430
- },
431
418
.Union = > switch (ty .containerLayout (zcu )) {
432
419
.auto = > return error .IllDefinedMemoryLayout , // Sema is supposed to have emitted a compile error already
433
420
.@"extern" = > {
@@ -437,11 +424,11 @@ pub fn writeToMemory(val: Value, ty: Type, pt: Zcu.PerThread, buffer: []u8) erro
437
424
const field_type = Type .fromInterned (union_obj .field_types .get (ip )[field_index ]);
438
425
const field_val = try val .fieldValue (pt , field_index );
439
426
const byte_count : usize = @intCast (field_type .abiSize (zcu ));
440
- return writeToMemory (field_val , field_type , pt , buffer [0.. byte_count ]);
427
+ return writeToMemory (field_val , pt , buffer [0.. byte_count ]);
441
428
} else {
442
429
const backing_ty = try ty .unionBackingType (pt );
443
430
const byte_count : usize = @intCast (backing_ty .abiSize (zcu ));
444
- return writeToMemory (val .unionValue (zcu ), backing_ty , pt , buffer [0.. byte_count ]);
431
+ return writeToMemory (val .unionValue (zcu ), pt , buffer [0.. byte_count ]);
445
432
}
446
433
},
447
434
.@"packed" = > {
@@ -450,19 +437,13 @@ pub fn writeToMemory(val: Value, ty: Type, pt: Zcu.PerThread, buffer: []u8) erro
450
437
return writeToPackedMemory (val , ty , pt , buffer [0.. byte_count ], 0 );
451
438
},
452
439
},
453
- .Pointer = > {
454
- if (ty .isSlice (zcu )) return error .IllDefinedMemoryLayout ;
455
- if (! val .ptrHasIntAddr (zcu )) return error .ReinterpretDeclRef ;
456
- return val .writeToMemory (Type .usize , pt , buffer );
457
- },
458
440
.Optional = > {
459
441
if (! ty .isPtrLikeOptional (zcu )) return error .IllDefinedMemoryLayout ;
460
- const child = ty .optionalChild (zcu );
461
442
const opt_val = val .optionalValue (zcu );
462
443
if (opt_val ) | some | {
463
- return some .writeToMemory (child , pt , buffer );
444
+ return some .writeToMemory (pt , buffer );
464
445
} else {
465
- return writeToMemory (try pt .intValue (Type .usize , 0 ), Type . usize , pt , buffer );
446
+ return writeToMemory (try pt .intValue (Type .usize , 0 ), pt , buffer );
466
447
}
467
448
},
468
449
else = > return error .Unimplemented ,
@@ -582,7 +563,7 @@ pub fn writeToPackedMemory(
582
563
},
583
564
.Pointer = > {
584
565
assert (! ty .isSlice (zcu )); // No well defined layout.
585
- if (! val .ptrHasIntAddr ( zcu ) ) return error .ReinterpretDeclRef ;
566
+ if (ip . getBackingAddrTag ( val .toIntern ()) .? != .int ) return error .ReinterpretDeclRef ;
586
567
return val .writeToPackedMemory (Type .usize , pt , buffer , bit_offset );
587
568
},
588
569
.Optional = > {
@@ -3658,14 +3639,15 @@ pub fn mulAddScalar(
3658
3639
3659
3640
/// If the value is represented in-memory as a series of bytes that all
3660
3641
/// have the same value, return that byte value, otherwise null.
3661
- pub fn hasRepeatedByteRepr (val : Value , ty : Type , pt : Zcu.PerThread ) ! ? u8 {
3642
+ pub fn hasRepeatedByteRepr (val : Value , pt : Zcu.PerThread ) ! ? u8 {
3662
3643
const zcu = pt .zcu ;
3644
+ const ty = val .typeOf (zcu );
3663
3645
const abi_size = std .math .cast (usize , ty .abiSize (zcu )) orelse return null ;
3664
3646
assert (abi_size >= 1 );
3665
3647
const byte_buffer = try zcu .gpa .alloc (u8 , abi_size );
3666
3648
defer zcu .gpa .free (byte_buffer );
3667
3649
3668
- writeToMemory (val , ty , pt , byte_buffer ) catch | err | switch (err ) {
3650
+ writeToMemory (val , pt , byte_buffer ) catch | err | switch (err ) {
3669
3651
error .OutOfMemory = > return error .OutOfMemory ,
3670
3652
error .ReinterpretDeclRef = > return null ,
3671
3653
// TODO: The writeToMemory function was originally created for the purpose
0 commit comments