@@ -192,11 +192,12 @@ pub fn toBigIntAdvanced(
192192 zcu : * Zcu ,
193193 tid : strat .Tid (),
194194) Zcu .CompileError ! BigIntConst {
195+ const ip = & zcu .intern_pool ;
195196 return switch (val .toIntern ()) {
196197 .bool_false = > BigIntMutable .init (& space .limbs , 0 ).toConst (),
197198 .bool_true = > BigIntMutable .init (& space .limbs , 1 ).toConst (),
198199 .null_value = > BigIntMutable .init (& space .limbs , 0 ).toConst (),
199- else = > switch (zcu . intern_pool .indexToKey (val .toIntern ())) {
200+ else = > switch (ip .indexToKey (val .toIntern ())) {
200201 .int = > | int | switch (int .storage ) {
201202 .u64 , .i64 , .big_int = > int .storage .toBigInt (space ),
202203 .lazy_align , .lazy_size = > | ty | {
@@ -214,6 +215,7 @@ pub fn toBigIntAdvanced(
214215 & space .limbs ,
215216 (try val .getUnsignedIntInner (strat , zcu , tid )).? ,
216217 ).toConst (),
218+ .err = > | err | BigIntMutable .init (& space .limbs , ip .getErrorValueIfExists (err .name ).? ).toConst (),
217219 else = > unreachable ,
218220 },
219221 };
@@ -326,15 +328,11 @@ pub fn toBool(val: Value) bool {
326328 };
327329}
328330
329- fn ptrHasIntAddr (val : Value , zcu : * Zcu ) bool {
330- return zcu .intern_pool .getBackingAddrTag (val .toIntern ()).? == .int ;
331- }
332-
333331/// Write a Value's contents to `buffer`.
334332///
335333/// Asserts that buffer.len >= ty.abiSize(). The buffer is allowed to extend past
336334/// 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 {
338336 ReinterpretDeclRef ,
339337 IllDefinedMemoryLayout ,
340338 Unimplemented ,
@@ -343,19 +341,25 @@ pub fn writeToMemory(val: Value, ty: Type, pt: Zcu.PerThread, buffer: []u8) erro
343341 const zcu = pt .zcu ;
344342 const target = zcu .getTarget ();
345343 const endian = target .cpu .arch .endian ();
344+ const ip = & zcu .intern_pool ;
345+ const ty = val .typeOf (zcu );
346346 if (val .isUndef (zcu )) {
347347 const size : usize = @intCast (ty .abiSize (zcu ));
348348 @memset (buffer [0.. size ], 0xaa );
349349 return ;
350350 }
351- const ip = & zcu .intern_pool ;
352351 switch (ty .zigTypeTag (zcu )) {
353352 .Void = > {},
354353 .Bool = > {
355354 buffer [0 ] = @intFromBool (val .toBool ());
356355 },
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 );
359363 const bits = int_info .bits ;
360364 const byte_count : u16 = @intCast ((@as (u17 , bits ) + 7 ) / 8 );
361365
@@ -379,7 +383,7 @@ pub fn writeToMemory(val: Value, ty: Type, pt: Zcu.PerThread, buffer: []u8) erro
379383 var buf_off : usize = 0 ;
380384 while (elem_i < len ) : (elem_i += 1 ) {
381385 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 .. ]);
383387 buf_off += elem_size ;
384388 }
385389 },
@@ -403,31 +407,14 @@ pub fn writeToMemory(val: Value, ty: Type, pt: Zcu.PerThread, buffer: []u8) erro
403407 .elems = > | elems | elems [field_index ],
404408 .repeated_elem = > | elem | elem ,
405409 });
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 .. ]);
408411 },
409412 .@"packed" = > {
410413 const byte_count = (@as (usize , @intCast (ty .bitSize (zcu ))) + 7 ) / 8 ;
411414 return writeToPackedMemory (val , ty , pt , buffer [0.. byte_count ], 0 );
412415 },
413416 }
414417 },
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- },
431418 .Union = > switch (ty .containerLayout (zcu )) {
432419 .auto = > return error .IllDefinedMemoryLayout , // Sema is supposed to have emitted a compile error already
433420 .@"extern" = > {
@@ -437,11 +424,11 @@ pub fn writeToMemory(val: Value, ty: Type, pt: Zcu.PerThread, buffer: []u8) erro
437424 const field_type = Type .fromInterned (union_obj .field_types .get (ip )[field_index ]);
438425 const field_val = try val .fieldValue (pt , field_index );
439426 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 ]);
441428 } else {
442429 const backing_ty = try ty .unionBackingType (pt );
443430 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 ]);
445432 }
446433 },
447434 .@"packed" = > {
@@ -450,19 +437,13 @@ pub fn writeToMemory(val: Value, ty: Type, pt: Zcu.PerThread, buffer: []u8) erro
450437 return writeToPackedMemory (val , ty , pt , buffer [0.. byte_count ], 0 );
451438 },
452439 },
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- },
458440 .Optional = > {
459441 if (! ty .isPtrLikeOptional (zcu )) return error .IllDefinedMemoryLayout ;
460- const child = ty .optionalChild (zcu );
461442 const opt_val = val .optionalValue (zcu );
462443 if (opt_val ) | some | {
463- return some .writeToMemory (child , pt , buffer );
444+ return some .writeToMemory (pt , buffer );
464445 } 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 );
466447 }
467448 },
468449 else = > return error .Unimplemented ,
@@ -582,7 +563,7 @@ pub fn writeToPackedMemory(
582563 },
583564 .Pointer = > {
584565 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 ;
586567 return val .writeToPackedMemory (Type .usize , pt , buffer , bit_offset );
587568 },
588569 .Optional = > {
@@ -3658,14 +3639,15 @@ pub fn mulAddScalar(
36583639
36593640/// If the value is represented in-memory as a series of bytes that all
36603641/// 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 {
36623643 const zcu = pt .zcu ;
3644+ const ty = val .typeOf (zcu );
36633645 const abi_size = std .math .cast (usize , ty .abiSize (zcu )) orelse return null ;
36643646 assert (abi_size >= 1 );
36653647 const byte_buffer = try zcu .gpa .alloc (u8 , abi_size );
36663648 defer zcu .gpa .free (byte_buffer );
36673649
3668- writeToMemory (val , ty , pt , byte_buffer ) catch | err | switch (err ) {
3650+ writeToMemory (val , pt , byte_buffer ) catch | err | switch (err ) {
36693651 error .OutOfMemory = > return error .OutOfMemory ,
36703652 error .ReinterpretDeclRef = > return null ,
36713653 // TODO: The writeToMemory function was originally created for the purpose
0 commit comments