Skip to content

Commit 589201b

Browse files
committed
fix variables which are pointers to packed struct fields
closes #1121
1 parent dcfd15a commit 589201b

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

src/analyze.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -489,20 +489,27 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons
489489
entry->zero_bits = !type_has_bits(child_type);
490490

491491
if (!entry->zero_bits) {
492-
if (is_const || is_volatile || host_int_bytes != 0 || byte_alignment != 0 ||
493-
ptr_len != PtrLenSingle)
494-
{
495-
ZigType *peer_type = get_pointer_to_type(g, child_type, false);
492+
if (is_const || is_volatile || byte_alignment != 0 || ptr_len != PtrLenSingle || bit_offset_in_host != 0) {
493+
ZigType *peer_type = get_pointer_to_type_extra(g, child_type, false, false,
494+
PtrLenSingle, 0, 0, host_int_bytes);
496495
entry->type_ref = peer_type->type_ref;
497496
entry->di_type = peer_type->di_type;
498497
} else {
499-
entry->type_ref = LLVMPointerType(child_type->type_ref, 0);
500-
501-
uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, entry->type_ref);
502-
uint64_t debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, entry->type_ref);
503-
assert(child_type->di_type);
504-
entry->di_type = ZigLLVMCreateDebugPointerType(g->dbuilder, child_type->di_type,
505-
debug_size_in_bits, debug_align_in_bits, buf_ptr(&entry->name));
498+
if (host_int_bytes == 0) {
499+
entry->type_ref = LLVMPointerType(child_type->type_ref, 0);
500+
uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, entry->type_ref);
501+
uint64_t debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, entry->type_ref);
502+
assert(child_type->di_type);
503+
entry->di_type = ZigLLVMCreateDebugPointerType(g->dbuilder, child_type->di_type,
504+
debug_size_in_bits, debug_align_in_bits, buf_ptr(&entry->name));
505+
} else {
506+
ZigType *host_int_type = get_int_type(g, false, host_int_bytes * 8);
507+
entry->type_ref = LLVMPointerType(host_int_type->type_ref, 0);
508+
uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, host_int_type->type_ref);
509+
uint64_t debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, host_int_type->type_ref);
510+
entry->di_type = ZigLLVMCreateDebugPointerType(g->dbuilder, host_int_type->di_type,
511+
debug_size_in_bits, debug_align_in_bits, buf_ptr(&entry->name));
512+
}
506513
}
507514
} else {
508515
assert(byte_alignment == 0);

test/cases/struct.zig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,3 +453,16 @@ test "implicit cast packed struct field to const ptr" {
453453
const res = LevelUpMove.toInt(lup.level);
454454
assert(res == 12);
455455
}
456+
457+
test "pointer to packed struct member in a stack variable" {
458+
const S = packed struct {
459+
a: u2,
460+
b: u2,
461+
};
462+
463+
var s = S{ .a = 2, .b = 0 };
464+
var b_ptr = &s.b;
465+
assert(s.b == 0);
466+
b_ptr.* = 2;
467+
assert(s.b == 2);
468+
}

0 commit comments

Comments
 (0)