Skip to content

Commit 85edf55

Browse files
authored
Merge pull request #2135 from ziglang/decouple-llvm-types
decouple llvm types from zig types
2 parents 2f96c55 + 025a147 commit 85edf55

File tree

9 files changed

+2017
-1689
lines changed

9 files changed

+2017
-1689
lines changed

doc/langref.html.in

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,8 +2112,8 @@ test "linked list" {
21122112
<li>A {#link|packed enum#} field uses exactly the bit width of its integer tag type.</li>
21132113
<li>A {#link|packed union#} field uses exactly the bit width of the union field with
21142114
the largest bit width.</li>
2115-
<li>Non-byte-aligned fields are packed into the smallest possible
2116-
byte-aligned integers in accordance with the target endianness.
2115+
<li>Non-ABI-aligned fields are packed into the smallest possible
2116+
ABI-aligned integers in accordance with the target endianness.
21172117
</li>
21182118
</ul>
21192119
<p>
@@ -2213,10 +2213,10 @@ fn bar(x: *const u3) u3 {
22132213
{#code_end#}
22142214
<p>
22152215
In this case, the function {#syntax#}bar{#endsyntax#} cannot be called becuse the pointer
2216-
to the non-byte-aligned field mentions the bit offset, but the function expects a byte-aligned pointer.
2216+
to the non-ABI-aligned field mentions the bit offset, but the function expects an ABI-aligned pointer.
22172217
</p>
22182218
<p>
2219-
Pointers to non-byte-aligned fields share the same address as the other fields within their host integer:
2219+
Pointers to non-ABI-aligned fields share the same address as the other fields within their host integer:
22202220
</p>
22212221
{#code_begin|test#}
22222222
const std = @import("std");

src/all_types.hpp

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,8 +1067,10 @@ struct TypeStructField {
10671067
ZigType *type_entry;
10681068
size_t src_index;
10691069
size_t gen_index;
1070-
uint32_t bit_offset_in_host; // offset from the memory at gen_index
1070+
size_t offset; // byte offset from beginning of struct
10711071
AstNode *decl_node;
1072+
uint32_t bit_offset_in_host; // offset from the memory at gen_index
1073+
uint32_t host_int_bytes; // size of host integer
10721074
};
10731075

10741076
enum ResolveStatus {
@@ -1077,6 +1079,8 @@ enum ResolveStatus {
10771079
ResolveStatusZeroBitsKnown,
10781080
ResolveStatusAlignmentKnown,
10791081
ResolveStatusSizeKnown,
1082+
ResolveStatusLLVMFwdDecl,
1083+
ResolveStatusLLVMFull,
10801084
};
10811085

10821086
struct ZigPackage {
@@ -1101,14 +1105,13 @@ struct ZigTypeStruct {
11011105
AstNode *decl_node;
11021106
TypeStructField *fields;
11031107
ScopeDecls *decls_scope;
1104-
uint64_t size_bytes;
11051108
HashMap<Buf *, TypeStructField *, buf_hash, buf_eql_buf> fields_by_name;
11061109
RootStruct *root_struct;
1110+
uint32_t *host_int_bytes; // available for packed structs, indexed by gen_index
11071111

11081112
uint32_t src_field_count;
11091113
uint32_t gen_field_count;
11101114

1111-
uint32_t abi_alignment; // known after ResolveStatusAlignmentKnown
11121115
ContainerLayout layout;
11131116
ResolveStatus resolve_status;
11141117

@@ -1164,39 +1167,28 @@ bool type_ptr_eql(const ZigType *a, const ZigType *b);
11641167

11651168
struct ZigTypeUnion {
11661169
AstNode *decl_node;
1167-
ContainerLayout layout;
1168-
uint32_t src_field_count;
1169-
uint32_t gen_field_count;
11701170
TypeUnionField *fields;
1171-
bool is_invalid; // true if any fields are invalid
1171+
ScopeDecls *decls_scope;
1172+
HashMap<Buf *, TypeUnionField *, buf_hash, buf_eql_buf> fields_by_name;
11721173
ZigType *tag_type; // always an enum or null
1173-
LLVMTypeRef union_type_ref;
1174+
LLVMTypeRef union_llvm_type;
1175+
ZigType *most_aligned_union_member;
1176+
size_t gen_union_index;
1177+
size_t gen_tag_index;
1178+
size_t union_abi_size;
11741179

1175-
ScopeDecls *decls_scope;
1180+
uint32_t src_field_count;
1181+
uint32_t gen_field_count;
11761182

1177-
// set this flag temporarily to detect infinite loops
1178-
bool embedded_in_current;
1179-
bool reported_infinite_err;
1180-
// whether we've finished resolving it
1181-
bool complete;
1183+
ContainerLayout layout;
1184+
ResolveStatus resolve_status;
11821185

1186+
bool have_explicit_tag_type;
1187+
bool resolve_loop_flag; // set this flag temporarily to detect infinite loops
1188+
bool reported_infinite_err;
11831189
// whether any of the fields require comptime
11841190
// the value is not valid until zero_bits_known == true
11851191
bool requires_comptime;
1186-
1187-
bool zero_bits_loop_flag;
1188-
bool zero_bits_known;
1189-
uint32_t abi_alignment; // also figured out with zero_bits pass
1190-
1191-
size_t gen_union_index;
1192-
size_t gen_tag_index;
1193-
1194-
bool have_explicit_tag_type;
1195-
1196-
uint32_t union_size_bytes;
1197-
ZigType *most_aligned_union_member;
1198-
1199-
HashMap<Buf *, TypeUnionField *, buf_hash, buf_eql_buf> fields_by_name;
12001192
};
12011193

12021194
struct FnGenParamInfo {
@@ -1277,8 +1269,11 @@ struct ZigType {
12771269
ZigTypeId id;
12781270
Buf name;
12791271

1280-
LLVMTypeRef type_ref;
1281-
ZigLLVMDIType *di_type;
1272+
// These are not supposed to be accessed directly. They're
1273+
// null during semantic analysis, memoized with get_llvm_type
1274+
// and get_llvm_di_type
1275+
LLVMTypeRef llvm_type;
1276+
ZigLLVMDIType *llvm_di_type;
12821277

12831278
union {
12841279
ZigTypePointer pointer;
@@ -1308,8 +1303,14 @@ struct ZigType {
13081303
ConstExprValue *cached_const_name_val;
13091304

13101305
OnePossibleValue one_possible_value;
1306+
// Known after ResolveStatusAlignmentKnown.
1307+
uint32_t abi_align;
1308+
// The offset in bytes between consecutive array elements of this type. Known
1309+
// after ResolveStatusSizeKnown.
1310+
size_t abi_size;
1311+
// Number of bits of information in this type. Known after ResolveStatusSizeKnown.
1312+
size_t size_in_bits;
13111313

1312-
bool zero_bits; // this is denormalized data
13131314
bool gen_h_loop_flag;
13141315
};
13151316

@@ -1700,11 +1701,9 @@ struct CodeGen {
17001701
ZigList<AstNode *> use_queue;
17011702
size_t use_queue_index;
17021703
ZigList<TimeEvent> timing_events;
1703-
ZigList<ZigLLVMDIType **> error_di_types;
17041704
ZigList<AstNode *> tld_ref_source_node_stack;
17051705
ZigList<ZigFn *> inline_fns;
17061706
ZigList<ZigFn *> test_fns;
1707-
ZigList<ZigLLVMDIEnumerator *> err_enumerators;
17081707
ZigList<ErrorTableEntry *> errors_by_index;
17091708
ZigList<CacheHash *> caches_to_release;
17101709
size_t largest_err_name_len;
@@ -1896,7 +1895,6 @@ struct ZigVar {
18961895
AstNode *decl_node;
18971896
ZigLLVMDILocalVariable *di_loc_var;
18981897
size_t src_arg_index;
1899-
size_t gen_arg_index;
19001898
Scope *parent_scope;
19011899
Scope *child_scope;
19021900
LLVMValueRef param_value_ref;

0 commit comments

Comments
 (0)