aboutsummaryrefslogtreecommitdiff
path: root/src/analyze.cpp
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2017-02-21 14:22:23 -0500
committerAndrew Kelley <superjoe30@gmail.com>2017-02-21 14:22:23 -0500
commitcf5108f222107ed242d2dc2c37d76758157da542 (patch)
tree9fbf62f7c54929d18031ad42256fcc003ca428d9 /src/analyze.cpp
parent4709fe1176ce88a35e877622dc977948846a3a43 (diff)
downloadzig-cf5108f222107ed242d2dc2c37d76758157da542.tar.gz
zig-cf5108f222107ed242d2dc2c37d76758157da542.zip
correct size of types for packed structs
with byte aligned but non-power-of-2 fields such as 24
Diffstat (limited to 'src/analyze.cpp')
-rw-r--r--src/analyze.cpp22
1 files changed, 18 insertions, 4 deletions
diff --git a/src/analyze.cpp b/src/analyze.cpp
index 398eb7ca58..f9b63f5d3f 100644
--- a/src/analyze.cpp
+++ b/src/analyze.cpp
@@ -254,15 +254,21 @@ bool type_has_zero_bits_known(TypeTableEntry *type_entry) {
uint64_t type_size(CodeGen *g, TypeTableEntry *type_entry) {
assert(type_is_complete(type_entry));
- if (type_has_bits(type_entry)) {
- return LLVMStoreSizeOfType(g->target_data_ref, type_entry->type_ref);
- } else {
+ TypeTableEntry *canon_type = get_underlying_type(type_entry);
+
+ if (!type_has_bits(type_entry))
return 0;
+
+ if (canon_type->id == TypeTableEntryIdStruct && canon_type->data.structure.layout == ContainerLayoutPacked) {
+ uint64_t size_in_bits = type_size_bits(g, type_entry);
+ return (size_in_bits + 7) / 8;
}
+
+ return LLVMStoreSizeOfType(g->target_data_ref, type_entry->type_ref);
}
-// This has to do with packed structs
uint64_t type_size_bits(CodeGen *g, TypeTableEntry *type_entry) {
+ assert(type_is_complete(type_entry));
TypeTableEntry *canon_type = get_underlying_type(type_entry);
if (!type_has_bits(type_entry))
@@ -531,6 +537,14 @@ TypeTableEntry *get_array_type(CodeGen *g, TypeTableEntry *child_type, uint64_t
ensure_complete_type(g, child_type);
+ TypeTableEntry *canon_child_type = get_underlying_type(child_type);
+ if (canon_child_type->id == TypeTableEntryIdStruct &&
+ canon_child_type->data.structure.layout == ContainerLayoutPacked &&
+ type_size_bits(g, canon_child_type) != 8 * type_size(g, canon_child_type))
+ {
+ zig_panic("TODO array of packed struct with unaligned size");
+ }
+
TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdArray);
entry->zero_bits = (array_size == 0) || child_type->zero_bits;