authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-20 11:04:31-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-20 11:04:31-04:00
logf8fe517d126d582e37ab4537fe9fd42f0531f44b
tree040baa17d21eb30b1dc7d2a4ef470fe7aea08e85
parent492821781dc34659f694577aacbc853c8839406f
signaturelock-open Commit is signed but in an unrecognized format.

better string literal caching implementation

We were caching the ConstExprValue of string literals, which works if you can never modify ConstExprValues. This premise is broken with `comptime var ...`. So I implemented an optimization in ConstExprValue arrays, where it stores a `Buf *` directly rather than an array of ConstExprValues for the elements, and then similar to array of undefined, it is expanded into the canonical form when necessary. However many operations can happen directly on the `Buf *`, which is faster. Furthermore, before a ConstExprValue array is expanded into canonical form, it removes itself from the string literal cache. This fixes the issue, because before an array element is modified it would have to be expanded. closes #1076

8 files changed, 278 insertions(+), 210 deletions(-)

src/all_types.hpp+12-5
......@@ -130,14 +130,18 @@ struct ConstUnionValue {
130130enum ConstArraySpecial {
131131 ConstArraySpecialNone,
132132 ConstArraySpecialUndef,
133 ConstArraySpecialBuf,
133134};
134135
135136struct ConstArrayValue {
136137 ConstArraySpecial special;
137 struct {
138 ConstExprValue *elements;
139 ConstParent parent;
140 } s_none;
138 union {
139 struct {
140 ConstExprValue *elements;
141 ConstParent parent;
142 } s_none;
143 Buf *s_buf;
144 } data;
141145};
142146
143147enum ConstPtrSpecial {
......@@ -983,6 +987,7 @@ struct FnTypeParamInfo {
983987};
984988
985989struct GenericFnTypeId {
990 CodeGen *codegen;
986991 ZigFn *fn_entry;
987992 ConstExprValue *params;
988993 size_t param_count;
......@@ -1291,6 +1296,7 @@ struct FnExport {
12911296};
12921297
12931298struct ZigFn {
1299 CodeGen *codegen;
12941300 LLVMValueRef llvm_value;
12951301 const char *llvm_name;
12961302 AstNode *proto_node;
......@@ -1848,13 +1854,14 @@ enum ScopeId {
18481854};
18491855
18501856struct Scope {
1851 ScopeId id;
1857 CodeGen *codegen;
18521858 AstNode *source_node;
18531859
18541860 // if the scope has a parent, this is it
18551861 Scope *parent;
18561862
18571863 ZigLLVMDIScope *di_scope;
1864 ScopeId id;
18581865};
18591866
18601867// This scope comes from global declarations or from
src/analyze.cpp+135-104
......@@ -92,62 +92,63 @@ ScopeDecls *get_container_scope(ZigType *type_entry) {
9292 return *get_container_scope_ptr(type_entry);
9393}
9494
95void init_scope(Scope *dest, ScopeId id, AstNode *source_node, Scope *parent) {
95void init_scope(CodeGen *g, Scope *dest, ScopeId id, AstNode *source_node, Scope *parent) {
96 dest->codegen = g;
9697 dest->id = id;
9798 dest->source_node = source_node;
9899 dest->parent = parent;
99100}
100101
101ScopeDecls *create_decls_scope(AstNode *node, Scope *parent, ZigType *container_type, ImportTableEntry *import) {
102ScopeDecls *create_decls_scope(CodeGen *g, AstNode *node, Scope *parent, ZigType *container_type, ImportTableEntry *import) {
102103 assert(node == nullptr || node->type == NodeTypeRoot || node->type == NodeTypeContainerDecl || node->type == NodeTypeFnCallExpr);
103104 ScopeDecls *scope = allocate<ScopeDecls>(1);
104 init_scope(&scope->base, ScopeIdDecls, node, parent);
105 init_scope(g, &scope->base, ScopeIdDecls, node, parent);
105106 scope->decl_table.init(4);
106107 scope->container_type = container_type;
107108 scope->import = import;
108109 return scope;
109110}
110111
111ScopeBlock *create_block_scope(AstNode *node, Scope *parent) {
112ScopeBlock *create_block_scope(CodeGen *g, AstNode *node, Scope *parent) {
112113 assert(node->type == NodeTypeBlock);
113114 ScopeBlock *scope = allocate<ScopeBlock>(1);
114 init_scope(&scope->base, ScopeIdBlock, node, parent);
115 init_scope(g, &scope->base, ScopeIdBlock, node, parent);
115116 scope->name = node->data.block.name;
116117 return scope;
117118}
118119
119ScopeDefer *create_defer_scope(AstNode *node, Scope *parent) {
120ScopeDefer *create_defer_scope(CodeGen *g, AstNode *node, Scope *parent) {
120121 assert(node->type == NodeTypeDefer);
121122 ScopeDefer *scope = allocate<ScopeDefer>(1);
122 init_scope(&scope->base, ScopeIdDefer, node, parent);
123 init_scope(g, &scope->base, ScopeIdDefer, node, parent);
123124 return scope;
124125}
125126
126ScopeDeferExpr *create_defer_expr_scope(AstNode *node, Scope *parent) {
127ScopeDeferExpr *create_defer_expr_scope(CodeGen *g, AstNode *node, Scope *parent) {
127128 assert(node->type == NodeTypeDefer);
128129 ScopeDeferExpr *scope = allocate<ScopeDeferExpr>(1);
129 init_scope(&scope->base, ScopeIdDeferExpr, node, parent);
130 init_scope(g, &scope->base, ScopeIdDeferExpr, node, parent);
130131 return scope;
131132}
132133
133Scope *create_var_scope(AstNode *node, Scope *parent, ZigVar *var) {
134Scope *create_var_scope(CodeGen *g, AstNode *node, Scope *parent, ZigVar *var) {
134135 ScopeVarDecl *scope = allocate<ScopeVarDecl>(1);
135 init_scope(&scope->base, ScopeIdVarDecl, node, parent);
136 init_scope(g, &scope->base, ScopeIdVarDecl, node, parent);
136137 scope->var = var;
137138 return &scope->base;
138139}
139140
140ScopeCImport *create_cimport_scope(AstNode *node, Scope *parent) {
141ScopeCImport *create_cimport_scope(CodeGen *g, AstNode *node, Scope *parent) {
141142 assert(node->type == NodeTypeFnCallExpr);
142143 ScopeCImport *scope = allocate<ScopeCImport>(1);
143 init_scope(&scope->base, ScopeIdCImport, node, parent);
144 init_scope(g, &scope->base, ScopeIdCImport, node, parent);
144145 buf_resize(&scope->buf, 0);
145146 return scope;
146147}
147148
148ScopeLoop *create_loop_scope(AstNode *node, Scope *parent) {
149ScopeLoop *create_loop_scope(CodeGen *g, AstNode *node, Scope *parent) {
149150 ScopeLoop *scope = allocate<ScopeLoop>(1);
150 init_scope(&scope->base, ScopeIdLoop, node, parent);
151 init_scope(g, &scope->base, ScopeIdLoop, node, parent);
151152 if (node->type == NodeTypeWhileExpr) {
152153 scope->name = node->data.while_expr.name;
153154 } else if (node->type == NodeTypeForExpr) {
......@@ -158,37 +159,37 @@ ScopeLoop *create_loop_scope(AstNode *node, Scope *parent) {
158159 return scope;
159160}
160161
161Scope *create_runtime_scope(AstNode *node, Scope *parent, IrInstruction *is_comptime) {
162Scope *create_runtime_scope(CodeGen *g, AstNode *node, Scope *parent, IrInstruction *is_comptime) {
162163 ScopeRuntime *scope = allocate<ScopeRuntime>(1);
163164 scope->is_comptime = is_comptime;
164 init_scope(&scope->base, ScopeIdRuntime, node, parent);
165 init_scope(g, &scope->base, ScopeIdRuntime, node, parent);
165166 return &scope->base;
166167}
167168
168ScopeSuspend *create_suspend_scope(AstNode *node, Scope *parent) {
169ScopeSuspend *create_suspend_scope(CodeGen *g, AstNode *node, Scope *parent) {
169170 assert(node->type == NodeTypeSuspend);
170171 ScopeSuspend *scope = allocate<ScopeSuspend>(1);
171 init_scope(&scope->base, ScopeIdSuspend, node, parent);
172 init_scope(g, &scope->base, ScopeIdSuspend, node, parent);
172173 return scope;
173174}
174175
175ScopeFnDef *create_fndef_scope(AstNode *node, Scope *parent, ZigFn *fn_entry) {
176ScopeFnDef *create_fndef_scope(CodeGen *g, AstNode *node, Scope *parent, ZigFn *fn_entry) {
176177 ScopeFnDef *scope = allocate<ScopeFnDef>(1);
177 init_scope(&scope->base, ScopeIdFnDef, node, parent);
178 init_scope(g, &scope->base, ScopeIdFnDef, node, parent);
178179 scope->fn_entry = fn_entry;
179180 return scope;
180181}
181182
182Scope *create_comptime_scope(AstNode *node, Scope *parent) {
183Scope *create_comptime_scope(CodeGen *g, AstNode *node, Scope *parent) {
183184 assert(node->type == NodeTypeCompTime || node->type == NodeTypeSwitchExpr);
184185 ScopeCompTime *scope = allocate<ScopeCompTime>(1);
185 init_scope(&scope->base, ScopeIdCompTime, node, parent);
186 init_scope(g, &scope->base, ScopeIdCompTime, node, parent);
186187 return &scope->base;
187188}
188189
189Scope *create_coro_prelude_scope(AstNode *node, Scope *parent) {
190Scope *create_coro_prelude_scope(CodeGen *g, AstNode *node, Scope *parent) {
190191 ScopeCoroPrelude *scope = allocate<ScopeCoroPrelude>(1);
191 init_scope(&scope->base, ScopeIdCoroPrelude, node, parent);
192 init_scope(g, &scope->base, ScopeIdCoroPrelude, node, parent);
192193 return &scope->base;
193194}
194195
......@@ -204,9 +205,9 @@ ImportTableEntry *get_scope_import(Scope *scope) {
204205 zig_unreachable();
205206}
206207
207static ZigType *new_container_type_entry(ZigTypeId id, AstNode *source_node, Scope *parent_scope) {
208static ZigType *new_container_type_entry(CodeGen *g, ZigTypeId id, AstNode *source_node, Scope *parent_scope) {
208209 ZigType *entry = new_type_table_entry(id);
209 *get_container_scope_ptr(entry) = create_decls_scope(source_node, parent_scope, entry, get_scope_import(parent_scope));
210 *get_container_scope_ptr(entry) = create_decls_scope(g, source_node, parent_scope, entry, get_scope_import(parent_scope));
210211 return entry;
211212}
212213
......@@ -1245,7 +1246,7 @@ ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind
12451246 AstNode *decl_node, const char *name, ContainerLayout layout)
12461247{
12471248 ZigTypeId type_id = container_to_type(kind);
1248 ZigType *entry = new_container_type_entry(type_id, decl_node, scope);
1249 ZigType *entry = new_container_type_entry(g, type_id, decl_node, scope);
12491250
12501251 switch (kind) {
12511252 case ContainerKindStruct:
......@@ -1372,13 +1373,17 @@ static bool analyze_const_string(CodeGen *g, Scope *scope, AstNode *node, Buf **
13721373
13731374 assert(ptr_field->data.x_ptr.special == ConstPtrSpecialBaseArray);
13741375 ConstExprValue *array_val = ptr_field->data.x_ptr.data.base_array.array_val;
1376 if (array_val->data.x_array.special == ConstArraySpecialBuf) {
1377 *out_buffer = array_val->data.x_array.data.s_buf;
1378 return true;
1379 }
13751380 expand_undef_array(g, array_val);
13761381 size_t len = bigint_as_unsigned(&len_field->data.x_bigint);
13771382 Buf *result = buf_alloc();
13781383 buf_resize(result, len);
13791384 for (size_t i = 0; i < len; i += 1) {
13801385 size_t new_index = ptr_field->data.x_ptr.data.base_array.elem_index + i;
1381 ConstExprValue *char_val = &array_val->data.x_array.s_none.elements[new_index];
1386 ConstExprValue *char_val = &array_val->data.x_array.data.s_none.elements[new_index];
13821387 if (char_val->special == ConstValSpecialUndef) {
13831388 add_node_error(g, node, buf_sprintf("use of undefined value"));
13841389 return false;
......@@ -3093,9 +3098,10 @@ static void get_fully_qualified_decl_name(Buf *buf, Tld *tld, uint8_t sep) {
30933098 buf_append_buf(buf, tld->name);
30943099}
30953100
3096ZigFn *create_fn_raw(FnInline inline_value) {
3101ZigFn *create_fn_raw(CodeGen *g, FnInline inline_value) {
30973102 ZigFn *fn_entry = allocate<ZigFn>(1);
30983103
3104 fn_entry->codegen = g;
30993105 fn_entry->analyzed_executable.backward_branch_count = &fn_entry->prealloc_bbc;
31003106 fn_entry->analyzed_executable.backward_branch_quota = default_backward_branch_quota;
31013107 fn_entry->analyzed_executable.fn_entry = fn_entry;
......@@ -3105,12 +3111,12 @@ ZigFn *create_fn_raw(FnInline inline_value) {
31053111 return fn_entry;
31063112}
31073113
3108ZigFn *create_fn(AstNode *proto_node) {
3114ZigFn *create_fn(CodeGen *g, AstNode *proto_node) {
31093115 assert(proto_node->type == NodeTypeFnProto);
31103116 AstNodeFnProto *fn_proto = &proto_node->data.fn_proto;
31113117
31123118 FnInline inline_value = fn_proto->is_inline ? FnInlineAlways : FnInlineAuto;
3113 ZigFn *fn_entry = create_fn_raw(inline_value);
3119 ZigFn *fn_entry = create_fn_raw(g, inline_value);
31143120
31153121 fn_entry->proto_node = proto_node;
31163122 fn_entry->body_node = (proto_node->data.fn_proto.fn_def_node == nullptr) ? nullptr :
......@@ -3209,7 +3215,7 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {
32093215
32103216 AstNode *fn_def_node = fn_proto->fn_def_node;
32113217
3212 ZigFn *fn_table_entry = create_fn(source_node);
3218 ZigFn *fn_table_entry = create_fn(g, source_node);
32133219 get_fully_qualified_decl_name(&fn_table_entry->symbol_name, &tld_fn->base, '_');
32143220
32153221 if (fn_proto->is_export) {
......@@ -3220,7 +3226,7 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {
32203226 tld_fn->fn_entry = fn_table_entry;
32213227
32223228 if (fn_table_entry->body_node) {
3223 fn_table_entry->fndef_scope = create_fndef_scope(
3229 fn_table_entry->fndef_scope = create_fndef_scope(g,
32243230 fn_table_entry->body_node, tld_fn->base.parent_scope, fn_table_entry);
32253231
32263232 for (size_t i = 0; i < fn_proto->params.length; i += 1) {
......@@ -3270,14 +3276,14 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {
32703276 }
32713277 }
32723278 } else if (source_node->type == NodeTypeTestDecl) {
3273 ZigFn *fn_table_entry = create_fn_raw(FnInlineAuto);
3279 ZigFn *fn_table_entry = create_fn_raw(g, FnInlineAuto);
32743280
32753281 get_fully_qualified_decl_name(&fn_table_entry->symbol_name, &tld_fn->base, '_');
32763282
32773283 tld_fn->fn_entry = fn_table_entry;
32783284
32793285 fn_table_entry->proto_node = source_node;
3280 fn_table_entry->fndef_scope = create_fndef_scope(source_node, tld_fn->base.parent_scope, fn_table_entry);
3286 fn_table_entry->fndef_scope = create_fndef_scope(g, source_node, tld_fn->base.parent_scope, fn_table_entry);
32813287 fn_table_entry->type_entry = get_test_fn_type(g);
32823288 fn_table_entry->body_node = source_node->data.test_decl.body;
32833289 fn_table_entry->is_test = true;
......@@ -3606,7 +3612,7 @@ ZigVar *add_variable(CodeGen *g, AstNode *source_node, Scope *parent_scope, Buf
36063612
36073613 Scope *child_scope;
36083614 if (source_node && source_node->type == NodeTypeParamDecl) {
3609 child_scope = create_var_scope(source_node, parent_scope, variable_entry);
3615 child_scope = create_var_scope(g, source_node, parent_scope, variable_entry);
36103616 } else {
36113617 // it's already in the decls table
36123618 child_scope = parent_scope;
......@@ -4329,7 +4335,7 @@ ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package, Buf *r
43294335 g->import_table.put(resolved_path, import_entry);
43304336 g->import_queue.append(import_entry);
43314337
4332 import_entry->decls_scope = create_decls_scope(import_entry->root, nullptr, nullptr, import_entry);
4338 import_entry->decls_scope = create_decls_scope(g, import_entry->root, nullptr, nullptr, import_entry);
43334339
43344340
43354341 assert(import_entry->root->type == NodeTypeRoot);
......@@ -4880,7 +4886,7 @@ bool generic_fn_type_id_eql(GenericFnTypeId *a, GenericFnTypeId *b) {
48804886 if (a_val->special != ConstValSpecialRuntime && b_val->special != ConstValSpecialRuntime) {
48814887 assert(a_val->special == ConstValSpecialStatic);
48824888 assert(b_val->special == ConstValSpecialStatic);
4883 if (!const_values_equal(a_val, b_val)) {
4889 if (!const_values_equal(a->fn_entry->codegen, a_val, b_val)) {
48844890 return false;
48854891 }
48864892 } else {
......@@ -4920,14 +4926,18 @@ static bool can_mutate_comptime_var_state(ConstExprValue *value) {
49204926 case ZigTypeIdArray:
49214927 if (value->type->data.array.len == 0)
49224928 return false;
4923 if (value->data.x_array.special == ConstArraySpecialUndef)
4924 return false;
4925 for (uint32_t i = 0; i < value->type->data.array.len; i += 1) {
4926 if (can_mutate_comptime_var_state(&value->data.x_array.s_none.elements[i]))
4927 return true;
4929 switch (value->data.x_array.special) {
4930 case ConstArraySpecialUndef:
4931 case ConstArraySpecialBuf:
4932 return false;
4933 case ConstArraySpecialNone:
4934 for (uint32_t i = 0; i < value->type->data.array.len; i += 1) {
4935 if (can_mutate_comptime_var_state(&value->data.x_array.data.s_none.elements[i]))
4936 return true;
4937 }
4938 return false;
49284939 }
4929 return false;
4930
4940 zig_unreachable();
49314941 case ZigTypeIdStruct:
49324942 for (uint32_t i = 0; i < value->type->data.structure.src_field_count; i += 1) {
49334943 if (can_mutate_comptime_var_state(&value->data.x_struct.fields[i]))
......@@ -5039,6 +5049,8 @@ uint32_t fn_eval_hash(Scope* scope) {
50395049}
50405050
50415051bool fn_eval_eql(Scope *a, Scope *b) {
5052 assert(a->codegen != nullptr);
5053 assert(b->codegen != nullptr);
50425054 while (a && b) {
50435055 if (a->id != b->id)
50445056 return false;
......@@ -5048,7 +5060,7 @@ bool fn_eval_eql(Scope *a, Scope *b) {
50485060 ScopeVarDecl *b_var_scope = (ScopeVarDecl *)b;
50495061 if (a_var_scope->var->value->type != b_var_scope->var->value->type)
50505062 return false;
5051 if (!const_values_equal(a_var_scope->var->value, b_var_scope->var->value))
5063 if (!const_values_equal(a->codegen, a_var_scope->var->value, b_var_scope->var->value))
50525064 return false;
50535065 } else if (a->id == ScopeIdFnDef) {
50545066 ScopeFnDef *a_fn_scope = (ScopeFnDef *)a;
......@@ -5130,14 +5142,8 @@ void init_const_str_lit(CodeGen *g, ConstExprValue *const_val, Buf *str) {
51305142
51315143 const_val->special = ConstValSpecialStatic;
51325144 const_val->type = get_array_type(g, g->builtin_types.entry_u8, buf_len(str));
5133 const_val->data.x_array.s_none.elements = create_const_vals(buf_len(str));
5134
5135 for (size_t i = 0; i < buf_len(str); i += 1) {
5136 ConstExprValue *this_char = &const_val->data.x_array.s_none.elements[i];
5137 this_char->special = ConstValSpecialStatic;
5138 this_char->type = g->builtin_types.entry_u8;
5139 bigint_init_unsigned(&this_char->data.x_bigint, (uint8_t)buf_ptr(str)[i]);
5140 }
5145 const_val->data.x_array.special = ConstArraySpecialBuf;
5146 const_val->data.x_array.data.s_buf = str;
51415147
51425148 g->string_literals_table.put(str, const_val);
51435149}
......@@ -5154,14 +5160,15 @@ void init_const_c_str_lit(CodeGen *g, ConstExprValue *const_val, Buf *str) {
51545160 ConstExprValue *array_val = create_const_vals(1);
51555161 array_val->special = ConstValSpecialStatic;
51565162 array_val->type = get_array_type(g, g->builtin_types.entry_u8, len_with_null);
5157 array_val->data.x_array.s_none.elements = create_const_vals(len_with_null);
5163 // TODO buf optimization
5164 array_val->data.x_array.data.s_none.elements = create_const_vals(len_with_null);
51585165 for (size_t i = 0; i < buf_len(str); i += 1) {
5159 ConstExprValue *this_char = &array_val->data.x_array.s_none.elements[i];
5166 ConstExprValue *this_char = &array_val->data.x_array.data.s_none.elements[i];
51605167 this_char->special = ConstValSpecialStatic;
51615168 this_char->type = g->builtin_types.entry_u8;
51625169 bigint_init_unsigned(&this_char->data.x_bigint, (uint8_t)buf_ptr(str)[i]);
51635170 }
5164 ConstExprValue *null_char = &array_val->data.x_array.s_none.elements[len_with_null - 1];
5171 ConstExprValue *null_char = &array_val->data.x_array.data.s_none.elements[len_with_null - 1];
51655172 null_char->special = ConstValSpecialStatic;
51665173 null_char->type = g->builtin_types.entry_u8;
51675174 bigint_init_unsigned(&null_char->data.x_bigint, 0);
......@@ -5535,7 +5542,7 @@ bool const_values_equal_ptr(ConstExprValue *a, ConstExprValue *b) {
55355542 zig_unreachable();
55365543}
55375544
5538bool const_values_equal(ConstExprValue *a, ConstExprValue *b) {
5545bool const_values_equal(CodeGen *g, ConstExprValue *a, ConstExprValue *b) {
55395546 assert(a->type->id == b->type->id);
55405547 assert(a->special == ConstValSpecialStatic);
55415548 assert(b->special == ConstValSpecialStatic);
......@@ -5593,13 +5600,20 @@ bool const_values_equal(ConstExprValue *a, ConstExprValue *b) {
55935600 assert(a->type->data.array.len == b->type->data.array.len);
55945601 assert(a->data.x_array.special != ConstArraySpecialUndef);
55955602 assert(b->data.x_array.special != ConstArraySpecialUndef);
5603 if (a->data.x_array.special == ConstArraySpecialBuf &&
5604 b->data.x_array.special == ConstArraySpecialBuf)
5605 {
5606 return buf_eql_buf(a->data.x_array.data.s_buf, b->data.x_array.data.s_buf);
5607 }
5608 expand_undef_array(g, a);
5609 expand_undef_array(g, b);
55965610
55975611 size_t len = a->type->data.array.len;
5598 ConstExprValue *a_elems = a->data.x_array.s_none.elements;
5599 ConstExprValue *b_elems = b->data.x_array.s_none.elements;
5612 ConstExprValue *a_elems = a->data.x_array.data.s_none.elements;
5613 ConstExprValue *b_elems = b->data.x_array.data.s_none.elements;
56005614
56015615 for (size_t i = 0; i < len; ++i) {
5602 if (!const_values_equal(&a_elems[i], &b_elems[i]))
5616 if (!const_values_equal(g, &a_elems[i], &b_elems[i]))
56035617 return false;
56045618 }
56055619
......@@ -5609,7 +5623,7 @@ bool const_values_equal(ConstExprValue *a, ConstExprValue *b) {
56095623 for (size_t i = 0; i < a->type->data.structure.src_field_count; i += 1) {
56105624 ConstExprValue *field_a = &a->data.x_struct.fields[i];
56115625 ConstExprValue *field_b = &b->data.x_struct.fields[i];
5612 if (!const_values_equal(field_a, field_b))
5626 if (!const_values_equal(g, field_a, field_b))
56135627 return false;
56145628 }
56155629 return true;
......@@ -5623,7 +5637,7 @@ bool const_values_equal(ConstExprValue *a, ConstExprValue *b) {
56235637 if (a->data.x_optional == nullptr || b->data.x_optional == nullptr) {
56245638 return (a->data.x_optional == nullptr && b->data.x_optional == nullptr);
56255639 } else {
5626 return const_values_equal(a->data.x_optional, b->data.x_optional);
5640 return const_values_equal(g, a->data.x_optional, b->data.x_optional);
56275641 }
56285642 case ZigTypeIdErrorUnion:
56295643 zig_panic("TODO");
......@@ -5808,26 +5822,15 @@ void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) {
58085822 case ZigTypeIdPointer:
58095823 return render_const_val_ptr(g, buf, const_val, type_entry);
58105824 case ZigTypeIdArray:
5811 {
5812 ZigType *child_type = type_entry->data.array.child_type;
5813 uint64_t len = type_entry->data.array.len;
5814
5815 if (const_val->data.x_array.special == ConstArraySpecialUndef) {
5825 switch (const_val->data.x_array.special) {
5826 case ConstArraySpecialUndef:
58165827 buf_append_str(buf, "undefined");
58175828 return;
5818 }
5819
5820 // if it's []u8, assume UTF-8 and output a string
5821 if (child_type->id == ZigTypeIdInt &&
5822 child_type->data.integral.bit_count == 8 &&
5823 !child_type->data.integral.is_signed)
5824 {
5829 case ConstArraySpecialBuf: {
5830 Buf *array_buf = const_val->data.x_array.data.s_buf;
58255831 buf_append_char(buf, '"');
5826 for (uint64_t i = 0; i < len; i += 1) {
5827 ConstExprValue *child_value = &const_val->data.x_array.s_none.elements[i];
5828 uint64_t big_c = bigint_as_unsigned(&child_value->data.x_bigint);
5829 assert(big_c <= UINT8_MAX);
5830 uint8_t c = (uint8_t)big_c;
5832 for (size_t i = 0; i < buf_len(array_buf); i += 1) {
5833 uint8_t c = buf_ptr(array_buf)[i];
58315834 if (c == '"') {
58325835 buf_append_str(buf, "\\\"");
58335836 } else {
......@@ -5837,17 +5840,20 @@ void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) {
58375840 buf_append_char(buf, '"');
58385841 return;
58395842 }
5840
5841 buf_appendf(buf, "%s{", buf_ptr(&type_entry->name));
5842 for (uint64_t i = 0; i < len; i += 1) {
5843 if (i != 0)
5844 buf_appendf(buf, ",");
5845 ConstExprValue *child_value = &const_val->data.x_array.s_none.elements[i];
5846 render_const_value(g, buf, child_value);
5843 case ConstArraySpecialNone: {
5844 buf_appendf(buf, "%s{", buf_ptr(&type_entry->name));
5845 uint64_t len = type_entry->data.array.len;
5846 for (uint64_t i = 0; i < len; i += 1) {
5847 if (i != 0)
5848 buf_appendf(buf, ",");
5849 ConstExprValue *child_value = &const_val->data.x_array.data.s_none.elements[i];
5850 render_const_value(g, buf, child_value);
5851 }
5852 buf_appendf(buf, "}");
5853 return;
58475854 }
5848 buf_appendf(buf, "}");
5849 return;
58505855 }
5856 zig_unreachable();
58515857 case ZigTypeIdNull:
58525858 {
58535859 buf_appendf(buf, "null");
......@@ -6102,24 +6108,49 @@ bool zig_llvm_fn_key_eql(ZigLLVMFnKey a, ZigLLVMFnKey b) {
61026108 zig_unreachable();
61036109}
61046110
6111// Canonicalize the array value as ConstArraySpecialNone
61056112void expand_undef_array(CodeGen *g, ConstExprValue *const_val) {
61066113 assert(const_val->type->id == ZigTypeIdArray);
6107 if (const_val->data.x_array.special == ConstArraySpecialUndef) {
6108 const_val->data.x_array.special = ConstArraySpecialNone;
6109 size_t elem_count = const_val->type->data.array.len;
6110 const_val->data.x_array.s_none.elements = create_const_vals(elem_count);
6111 for (size_t i = 0; i < elem_count; i += 1) {
6112 ConstExprValue *element_val = &const_val->data.x_array.s_none.elements[i];
6113 element_val->type = const_val->type->data.array.child_type;
6114 init_const_undefined(g, element_val);
6115 ConstParent *parent = get_const_val_parent(g, element_val);
6116 if (parent != nullptr) {
6117 parent->id = ConstParentIdArray;
6118 parent->data.p_array.array_val = const_val;
6119 parent->data.p_array.elem_index = i;
6114 switch (const_val->data.x_array.special) {
6115 case ConstArraySpecialNone:
6116 return;
6117 case ConstArraySpecialUndef: {
6118 const_val->data.x_array.special = ConstArraySpecialNone;
6119 size_t elem_count = const_val->type->data.array.len;
6120 const_val->data.x_array.data.s_none.elements = create_const_vals(elem_count);
6121 for (size_t i = 0; i < elem_count; i += 1) {
6122 ConstExprValue *element_val = &const_val->data.x_array.data.s_none.elements[i];
6123 element_val->type = const_val->type->data.array.child_type;
6124 init_const_undefined(g, element_val);
6125 ConstParent *parent = get_const_val_parent(g, element_val);
6126 if (parent != nullptr) {
6127 parent->id = ConstParentIdArray;
6128 parent->data.p_array.array_val = const_val;
6129 parent->data.p_array.elem_index = i;
6130 }
61206131 }
6132 return;
6133 }
6134 case ConstArraySpecialBuf: {
6135 Buf *buf = const_val->data.x_array.data.s_buf;
6136 // If we're doing this it means that we are potentially modifying the data,
6137 // so we can't have it be in the string literals table
6138 g->string_literals_table.maybe_remove(buf);
6139
6140 const_val->data.x_array.special = ConstArraySpecialNone;
6141 size_t elem_count = const_val->type->data.array.len;
6142 assert(elem_count == buf_len(buf));
6143 const_val->data.x_array.data.s_none.elements = create_const_vals(elem_count);
6144 for (size_t i = 0; i < elem_count; i += 1) {
6145 ConstExprValue *this_char = &const_val->data.x_array.data.s_none.elements[i];
6146 this_char->special = ConstValSpecialStatic;
6147 this_char->type = g->builtin_types.entry_u8;
6148 bigint_init_unsigned(&this_char->data.x_bigint, (uint8_t)buf_ptr(buf)[i]);
6149 }
6150 return;
61216151 }
61226152 }
6153 zig_unreachable();
61236154}
61246155
61256156ConstParent *get_const_val_parent(CodeGen *g, ConstExprValue *value) {
......@@ -6127,7 +6158,7 @@ ConstParent *get_const_val_parent(CodeGen *g, ConstExprValue *value) {
61276158 ZigType *type_entry = value->type;
61286159 if (type_entry->id == ZigTypeIdArray) {
61296160 expand_undef_array(g, value);
6130 return &value->data.x_array.s_none.parent;
6161 return &value->data.x_array.data.s_none.parent;
61316162 } else if (type_entry->id == ZigTypeIdStruct) {
61326163 return &value->data.x_struct.parent;
61336164 } else if (type_entry->id == ZigTypeIdUnion) {
src/analyze.hpp+15-15
......@@ -84,8 +84,8 @@ void init_tld(Tld *tld, TldId id, Buf *name, VisibMod visib_mod, AstNode *source
8484ZigVar *add_variable(CodeGen *g, AstNode *source_node, Scope *parent_scope, Buf *name,
8585 bool is_const, ConstExprValue *init_value, Tld *src_tld);
8686ZigType *analyze_type_expr(CodeGen *g, Scope *scope, AstNode *node);
87ZigFn *create_fn(AstNode *proto_node);
88ZigFn *create_fn_raw(FnInline inline_value, GlobalLinkageId linkage);
87ZigFn *create_fn(CodeGen *g, AstNode *proto_node);
88ZigFn *create_fn_raw(CodeGen *g, FnInline inline_value);
8989void init_fn_type_id(FnTypeId *fn_type_id, AstNode *proto_node, size_t param_count_alloc);
9090AstNode *get_param_decl_node(ZigFn *fn_entry, size_t index);
9191bool type_requires_comptime(ZigType *type_entry);
......@@ -93,25 +93,25 @@ Error ATTRIBUTE_MUST_USE ensure_complete_type(CodeGen *g, ZigType *type_entry);
9393Error ATTRIBUTE_MUST_USE type_resolve(CodeGen *g, ZigType *type_entry, ResolveStatus status);
9494void complete_enum(CodeGen *g, ZigType *enum_type);
9595bool ir_get_var_is_comptime(ZigVar *var);
96bool const_values_equal(ConstExprValue *a, ConstExprValue *b);
96bool const_values_equal(CodeGen *g, ConstExprValue *a, ConstExprValue *b);
9797void eval_min_max_value(CodeGen *g, ZigType *type_entry, ConstExprValue *const_val, bool is_max);
9898void eval_min_max_value_int(CodeGen *g, ZigType *int_type, BigInt *bigint, bool is_max);
9999
100100void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val);
101101void analyze_fn_ir(CodeGen *g, ZigFn *fn_table_entry, AstNode *return_type_node);
102102
103ScopeBlock *create_block_scope(AstNode *node, Scope *parent);
104ScopeDefer *create_defer_scope(AstNode *node, Scope *parent);
105ScopeDeferExpr *create_defer_expr_scope(AstNode *node, Scope *parent);
106Scope *create_var_scope(AstNode *node, Scope *parent, ZigVar *var);
107ScopeCImport *create_cimport_scope(AstNode *node, Scope *parent);
108ScopeLoop *create_loop_scope(AstNode *node, Scope *parent);
109ScopeSuspend *create_suspend_scope(AstNode *node, Scope *parent);
110ScopeFnDef *create_fndef_scope(AstNode *node, Scope *parent, ZigFn *fn_entry);
111ScopeDecls *create_decls_scope(AstNode *node, Scope *parent, ZigType *container_type, ImportTableEntry *import);
112Scope *create_comptime_scope(AstNode *node, Scope *parent);
113Scope *create_coro_prelude_scope(AstNode *node, Scope *parent);
114Scope *create_runtime_scope(AstNode *node, Scope *parent, IrInstruction *is_comptime);
103ScopeBlock *create_block_scope(CodeGen *g, AstNode *node, Scope *parent);
104ScopeDefer *create_defer_scope(CodeGen *g, AstNode *node, Scope *parent);
105ScopeDeferExpr *create_defer_expr_scope(CodeGen *g, AstNode *node, Scope *parent);
106Scope *create_var_scope(CodeGen *g, AstNode *node, Scope *parent, ZigVar *var);
107ScopeCImport *create_cimport_scope(CodeGen *g, AstNode *node, Scope *parent);
108ScopeLoop *create_loop_scope(CodeGen *g, AstNode *node, Scope *parent);
109ScopeSuspend *create_suspend_scope(CodeGen *g, AstNode *node, Scope *parent);
110ScopeFnDef *create_fndef_scope(CodeGen *g, AstNode *node, Scope *parent, ZigFn *fn_entry);
111ScopeDecls *create_decls_scope(CodeGen *g, AstNode *node, Scope *parent, ZigType *container_type, ImportTableEntry *import);
112Scope *create_comptime_scope(CodeGen *g, AstNode *node, Scope *parent);
113Scope *create_coro_prelude_scope(CodeGen *g, AstNode *node, Scope *parent);
114Scope *create_runtime_scope(CodeGen *g, AstNode *node, Scope *parent, IrInstruction *is_comptime);
115115
116116void init_const_str_lit(CodeGen *g, ConstExprValue *const_val, Buf *str);
117117ConstExprValue *create_const_str_lit(CodeGen *g, Buf *str);
src/codegen.cpp+27-21
......@@ -5336,7 +5336,7 @@ static LLVMValueRef gen_parent_ptr(CodeGen *g, ConstExprValue *val, ConstParent
53365336
53375337static LLVMValueRef gen_const_ptr_array_recursive(CodeGen *g, ConstExprValue *array_const_val, size_t index) {
53385338 expand_undef_array(g, array_const_val);
5339 ConstParent *parent = &array_const_val->data.x_array.s_none.parent;
5339 ConstParent *parent = &array_const_val->data.x_array.data.s_none.parent;
53405340 LLVMValueRef base_ptr = gen_parent_ptr(g, array_const_val, parent);
53415341
53425342 LLVMTypeKind el_type = LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(base_ptr)));
......@@ -5716,23 +5716,29 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
57165716 case ZigTypeIdArray:
57175717 {
57185718 uint64_t len = type_entry->data.array.len;
5719 if (const_val->data.x_array.special == ConstArraySpecialUndef) {
5720 return LLVMGetUndef(type_entry->type_ref);
5721 }
5722
5723 LLVMValueRef *values = allocate<LLVMValueRef>(len);
5724 LLVMTypeRef element_type_ref = type_entry->data.array.child_type->type_ref;
5725 bool make_unnamed_struct = false;
5726 for (uint64_t i = 0; i < len; i += 1) {
5727 ConstExprValue *elem_value = &const_val->data.x_array.s_none.elements[i];
5728 LLVMValueRef val = gen_const_val(g, elem_value, "");
5729 values[i] = val;
5730 make_unnamed_struct = make_unnamed_struct || is_llvm_value_unnamed_type(elem_value->type, val);
5731 }
5732 if (make_unnamed_struct) {
5733 return LLVMConstStruct(values, len, true);
5734 } else {
5735 return LLVMConstArray(element_type_ref, values, (unsigned)len);
5719 switch (const_val->data.x_array.special) {
5720 case ConstArraySpecialUndef:
5721 return LLVMGetUndef(type_entry->type_ref);
5722 case ConstArraySpecialNone: {
5723 LLVMValueRef *values = allocate<LLVMValueRef>(len);
5724 LLVMTypeRef element_type_ref = type_entry->data.array.child_type->type_ref;
5725 bool make_unnamed_struct = false;
5726 for (uint64_t i = 0; i < len; i += 1) {
5727 ConstExprValue *elem_value = &const_val->data.x_array.data.s_none.elements[i];
5728 LLVMValueRef val = gen_const_val(g, elem_value, "");
5729 values[i] = val;
5730 make_unnamed_struct = make_unnamed_struct || is_llvm_value_unnamed_type(elem_value->type, val);
5731 }
5732 if (make_unnamed_struct) {
5733 return LLVMConstStruct(values, len, true);
5734 } else {
5735 return LLVMConstArray(element_type_ref, values, (unsigned)len);
5736 }
5737 }
5738 case ConstArraySpecialBuf: {
5739 Buf *buf = const_val->data.x_array.data.s_buf;
5740 return LLVMConstString(buf_ptr(buf), (unsigned)buf_len(buf), true);
5741 }
57365742 }
57375743 }
57385744 case ZigTypeIdUnion:
......@@ -7278,7 +7284,7 @@ void codegen_translate_c(CodeGen *g, Buf *full_path) {
72787284 import->source_code = nullptr;
72797285 import->path = full_path;
72807286 g->root_import = import;
7281 import->decls_scope = create_decls_scope(nullptr, nullptr, nullptr, import);
7287 import->decls_scope = create_decls_scope(g, nullptr, nullptr, nullptr, import);
72827288
72837289 init(g);
72847290
......@@ -7352,12 +7358,12 @@ static void create_test_compile_var_and_add_test_runner(CodeGen *g) {
73527358 ConstExprValue *test_fn_array = create_const_vals(1);
73537359 test_fn_array->type = get_array_type(g, struct_type, g->test_fns.length);
73547360 test_fn_array->special = ConstValSpecialStatic;
7355 test_fn_array->data.x_array.s_none.elements = create_const_vals(g->test_fns.length);
7361 test_fn_array->data.x_array.data.s_none.elements = create_const_vals(g->test_fns.length);
73567362
73577363 for (size_t i = 0; i < g->test_fns.length; i += 1) {
73587364 ZigFn *test_fn_entry = g->test_fns.at(i);
73597365
7360 ConstExprValue *this_val = &test_fn_array->data.x_array.s_none.elements[i];
7366 ConstExprValue *this_val = &test_fn_array->data.x_array.data.s_none.elements[i];
73617367 this_val->special = ConstValSpecialStatic;
73627368 this_val->type = struct_type;
73637369 this_val->data.x_struct.parent.id = ConstParentIdArray;
src/ir.cpp+71-64
......@@ -166,7 +166,7 @@ static ConstExprValue *const_ptr_pointee_unchecked(CodeGen *g, ConstExprValue *c
166166 break;
167167 case ConstPtrSpecialBaseArray:
168168 expand_undef_array(g, const_val->data.x_ptr.data.base_array.array_val);
169 result = &const_val->data.x_ptr.data.base_array.array_val->data.x_array.s_none.elements[
169 result = &const_val->data.x_ptr.data.base_array.array_val->data.x_array.data.s_none.elements[
170170 const_val->data.x_ptr.data.base_array.elem_index];
171171 break;
172172 case ConstPtrSpecialBaseStruct:
......@@ -3360,7 +3360,7 @@ static ZigVar *create_local_var(CodeGen *codegen, AstNode *node, Scope *parent_s
33603360 variable_entry->src_is_const = src_is_const;
33613361 variable_entry->gen_is_const = gen_is_const;
33623362 variable_entry->decl_node = node;
3363 variable_entry->child_scope = create_var_scope(node, parent_scope, variable_entry);
3363 variable_entry->child_scope = create_var_scope(codegen, node, parent_scope, variable_entry);
33643364
33653365 return variable_entry;
33663366}
......@@ -3388,7 +3388,7 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode
33883388 ZigList<IrInstruction *> incoming_values = {0};
33893389 ZigList<IrBasicBlock *> incoming_blocks = {0};
33903390
3391 ScopeBlock *scope_block = create_block_scope(block_node, parent_scope);
3391 ScopeBlock *scope_block = create_block_scope(irb->codegen, block_node, parent_scope);
33923392
33933393 Scope *outer_block_scope = &scope_block->base;
33943394 Scope *child_scope = outer_block_scope;
......@@ -5026,7 +5026,7 @@ static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, Scope *scope, AstNode
50265026
50275027 ir_set_cursor_at_end_and_append_block(irb, then_block);
50285028
5029 Scope *subexpr_scope = create_runtime_scope(node, scope, is_comptime);
5029 Scope *subexpr_scope = create_runtime_scope(irb->codegen, node, scope, is_comptime);
50305030 IrInstruction *then_expr_result = ir_gen_node(irb, then_node, subexpr_scope);
50315031 if (then_expr_result == irb->codegen->invalid_instruction)
50325032 return then_expr_result;
......@@ -5318,7 +5318,7 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
53185318 ir_should_inline(irb->exec, scope) || node->data.while_expr.is_inline);
53195319 ir_build_br(irb, scope, node, cond_block, is_comptime);
53205320
5321 Scope *subexpr_scope = create_runtime_scope(node, scope, is_comptime);
5321 Scope *subexpr_scope = create_runtime_scope(irb->codegen, node, scope, is_comptime);
53225322 Buf *var_symbol = node->data.while_expr.var_symbol;
53235323 Buf *err_symbol = node->data.while_expr.err_symbol;
53245324 if (err_symbol != nullptr) {
......@@ -5359,7 +5359,7 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
53595359 ZigList<IrInstruction *> incoming_values = {0};
53605360 ZigList<IrBasicBlock *> incoming_blocks = {0};
53615361
5362 ScopeLoop *loop_scope = create_loop_scope(node, payload_scope);
5362 ScopeLoop *loop_scope = create_loop_scope(irb->codegen, node, payload_scope);
53635363 loop_scope->break_block = end_block;
53645364 loop_scope->continue_block = continue_block;
53655365 loop_scope->is_comptime = is_comptime;
......@@ -5415,7 +5415,7 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
54155415 return ir_build_phi(irb, scope, node, incoming_blocks.length, incoming_blocks.items, incoming_values.items);
54165416 } else if (var_symbol != nullptr) {
54175417 ir_set_cursor_at_end_and_append_block(irb, cond_block);
5418 Scope *subexpr_scope = create_runtime_scope(node, scope, is_comptime);
5418 Scope *subexpr_scope = create_runtime_scope(irb->codegen, node, scope, is_comptime);
54195419 // TODO make it an error to write to payload variable
54205420 AstNode *symbol_node = node; // TODO make more accurate
54215421
......@@ -5443,7 +5443,7 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
54435443 ZigList<IrInstruction *> incoming_values = {0};
54445444 ZigList<IrBasicBlock *> incoming_blocks = {0};
54455445
5446 ScopeLoop *loop_scope = create_loop_scope(node, child_scope);
5446 ScopeLoop *loop_scope = create_loop_scope(irb->codegen, node, child_scope);
54475447 loop_scope->break_block = end_block;
54485448 loop_scope->continue_block = continue_block;
54495449 loop_scope->is_comptime = is_comptime;
......@@ -5506,9 +5506,9 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
55065506 ZigList<IrInstruction *> incoming_values = {0};
55075507 ZigList<IrBasicBlock *> incoming_blocks = {0};
55085508
5509 Scope *subexpr_scope = create_runtime_scope(node, scope, is_comptime);
5509 Scope *subexpr_scope = create_runtime_scope(irb->codegen, node, scope, is_comptime);
55105510
5511 ScopeLoop *loop_scope = create_loop_scope(node, subexpr_scope);
5511 ScopeLoop *loop_scope = create_loop_scope(irb->codegen, node, subexpr_scope);
55125512 loop_scope->break_block = end_block;
55135513 loop_scope->continue_block = continue_block;
55145514 loop_scope->is_comptime = is_comptime;
......@@ -5645,7 +5645,7 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
56455645
56465646 ZigList<IrInstruction *> incoming_values = {0};
56475647 ZigList<IrBasicBlock *> incoming_blocks = {0};
5648 ScopeLoop *loop_scope = create_loop_scope(node, child_scope);
5648 ScopeLoop *loop_scope = create_loop_scope(irb->codegen, node, child_scope);
56495649 loop_scope->break_block = end_block;
56505650 loop_scope->continue_block = continue_block;
56515651 loop_scope->is_comptime = is_comptime;
......@@ -5855,7 +5855,7 @@ static IrInstruction *ir_gen_test_expr(IrBuilder *irb, Scope *scope, AstNode *no
58555855
58565856 ir_set_cursor_at_end_and_append_block(irb, then_block);
58575857
5858 Scope *subexpr_scope = create_runtime_scope(node, scope, is_comptime);
5858 Scope *subexpr_scope = create_runtime_scope(irb->codegen, node, scope, is_comptime);
58595859 Scope *var_scope;
58605860 if (var_symbol) {
58615861 IrInstruction *var_type = nullptr;
......@@ -5930,7 +5930,7 @@ static IrInstruction *ir_gen_if_err_expr(IrBuilder *irb, Scope *scope, AstNode *
59305930
59315931 ir_set_cursor_at_end_and_append_block(irb, ok_block);
59325932
5933 Scope *subexpr_scope = create_runtime_scope(node, scope, is_comptime);
5933 Scope *subexpr_scope = create_runtime_scope(irb->codegen, node, scope, is_comptime);
59345934 Scope *var_scope;
59355935 if (var_symbol) {
59365936 IrInstruction *var_type = nullptr;
......@@ -6066,8 +6066,8 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
60666066 ZigList<IrInstructionCheckSwitchProngsRange> check_ranges = {0};
60676067
60686068 // First do the else and the ranges
6069 Scope *subexpr_scope = create_runtime_scope(node, scope, is_comptime);
6070 Scope *comptime_scope = create_comptime_scope(node, scope);
6069 Scope *subexpr_scope = create_runtime_scope(irb->codegen, node, scope, is_comptime);
6070 Scope *comptime_scope = create_comptime_scope(irb->codegen, node, scope);
60716071 AstNode *else_prong = nullptr;
60726072 for (size_t prong_i = 0; prong_i < prong_count; prong_i += 1) {
60736073 AstNode *prong_node = node->data.switch_expr.prongs.at(prong_i);
......@@ -6231,7 +6231,7 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
62316231static IrInstruction *ir_gen_comptime(IrBuilder *irb, Scope *parent_scope, AstNode *node, LVal lval) {
62326232 assert(node->type == NodeTypeCompTime);
62336233
6234 Scope *child_scope = create_comptime_scope(node, parent_scope);
6234 Scope *child_scope = create_comptime_scope(irb->codegen, node, parent_scope);
62356235 return ir_gen_node_extra(irb, node->data.comptime_expr.expr, child_scope, lval);
62366236}
62376237
......@@ -6394,10 +6394,10 @@ static IrInstruction *ir_gen_error_type(IrBuilder *irb, Scope *scope, AstNode *n
63946394static IrInstruction *ir_gen_defer(IrBuilder *irb, Scope *parent_scope, AstNode *node) {
63956395 assert(node->type == NodeTypeDefer);
63966396
6397 ScopeDefer *defer_child_scope = create_defer_scope(node, parent_scope);
6397 ScopeDefer *defer_child_scope = create_defer_scope(irb->codegen, node, parent_scope);
63986398 node->data.defer.child_scope = &defer_child_scope->base;
63996399
6400 ScopeDeferExpr *defer_expr_scope = create_defer_expr_scope(node, parent_scope);
6400 ScopeDeferExpr *defer_expr_scope = create_defer_expr_scope(irb->codegen, node, parent_scope);
64016401 node->data.defer.expr_scope = &defer_expr_scope->base;
64026402
64036403 return ir_build_const_void(irb, parent_scope, node);
......@@ -7154,7 +7154,7 @@ static IrInstruction *ir_gen_suspend(IrBuilder *irb, Scope *parent_scope, AstNod
71547154 suspend_code = ir_build_coro_suspend(irb, parent_scope, node, nullptr, const_bool_false);
71557155 } else {
71567156 Scope *child_scope;
7157 ScopeSuspend *suspend_scope = create_suspend_scope(node, parent_scope);
7157 ScopeSuspend *suspend_scope = create_suspend_scope(irb->codegen, node, parent_scope);
71587158 suspend_scope->resume_block = resume_block;
71597159 child_scope = &suspend_scope->base;
71607160 IrInstruction *save_token = ir_build_coro_save(irb, child_scope, node, irb->exec->coro_handle);
......@@ -7370,7 +7370,7 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
73707370 ZigVar *coro_size_var;
73717371 if (is_async) {
73727372 // create the coro promise
7373 Scope *coro_scope = create_coro_prelude_scope(node, scope);
7373 Scope *coro_scope = create_coro_prelude_scope(irb->codegen, node, scope);
73747374 const_bool_false = ir_build_const_bool(irb, coro_scope, node, false);
73757375 ZigVar *promise_var = ir_create_var(irb, node, coro_scope, nullptr, false, false, true, const_bool_false);
73767376
......@@ -10569,9 +10569,9 @@ static IrInstruction *ir_analyze_ptr_to_array(IrAnalyze *ira, IrInstruction *sou
1056910569 array_val->special = ConstValSpecialStatic;
1057010570 array_val->type = array_type;
1057110571 array_val->data.x_array.special = ConstArraySpecialNone;
10572 array_val->data.x_array.s_none.elements = pointee;
10573 array_val->data.x_array.s_none.parent.id = ConstParentIdScalar;
10574 array_val->data.x_array.s_none.parent.data.p_scalar.scalar_val = pointee;
10572 array_val->data.x_array.data.s_none.elements = pointee;
10573 array_val->data.x_array.data.s_none.parent.id = ConstParentIdScalar;
10574 array_val->data.x_array.data.s_none.parent.data.p_scalar.scalar_val = pointee;
1057510575
1057610576 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(&ira->new_irb,
1057710577 source_instr->scope, source_instr->source_node);
......@@ -11391,13 +11391,16 @@ static Buf *ir_resolve_str(IrAnalyze *ira, IrInstruction *value) {
1139111391
1139211392 assert(ptr_field->data.x_ptr.special == ConstPtrSpecialBaseArray);
1139311393 ConstExprValue *array_val = ptr_field->data.x_ptr.data.base_array.array_val;
11394 if (array_val->data.x_array.special == ConstArraySpecialBuf) {
11395 return array_val->data.x_array.data.s_buf;
11396 }
1139411397 expand_undef_array(ira->codegen, array_val);
1139511398 size_t len = bigint_as_unsigned(&len_field->data.x_bigint);
1139611399 Buf *result = buf_alloc();
1139711400 buf_resize(result, len);
1139811401 for (size_t i = 0; i < len; i += 1) {
1139911402 size_t new_index = ptr_field->data.x_ptr.data.base_array.elem_index + i;
11400 ConstExprValue *char_val = &array_val->data.x_array.s_none.elements[new_index];
11403 ConstExprValue *char_val = &array_val->data.x_array.data.s_none.elements[new_index];
1140111404 if (char_val->special == ConstValSpecialUndef) {
1140211405 ir_add_error(ira, casted_value, buf_sprintf("use of undefined value"));
1140311406 return nullptr;
......@@ -11750,7 +11753,7 @@ static ZigType *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp *bin_op
1175011753 Cmp cmp_result = bigint_cmp(&op1_val->data.x_bigint, &op2_val->data.x_bigint);
1175111754 answer = resolve_cmp_op_id(op_id, cmp_result);
1175211755 } else {
11753 bool are_equal = one_possible_value || const_values_equal(op1_val, op2_val);
11756 bool are_equal = one_possible_value || const_values_equal(ira->codegen, op1_val, op2_val);
1175411757 if (op_id == IrBinOpCmpEq) {
1175511758 answer = are_equal;
1175611759 } else if (op_id == IrBinOpCmpNotEq) {
......@@ -12463,19 +12466,20 @@ static ZigType *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *instruc
1246312466 return result_type;
1246412467 }
1246512468
12466 out_array_val->data.x_array.s_none.elements = create_const_vals(new_len);
12469 out_array_val->data.x_array.data.s_none.elements = create_const_vals(new_len);
12470 // TODO handle the buf case here for an optimization
1246712471 expand_undef_array(ira->codegen, op1_array_val);
1246812472 expand_undef_array(ira->codegen, op2_array_val);
1246912473
1247012474 size_t next_index = 0;
1247112475 for (size_t i = op1_array_index; i < op1_array_end; i += 1, next_index += 1) {
12472 out_array_val->data.x_array.s_none.elements[next_index] = op1_array_val->data.x_array.s_none.elements[i];
12476 out_array_val->data.x_array.data.s_none.elements[next_index] = op1_array_val->data.x_array.data.s_none.elements[i];
1247312477 }
1247412478 for (size_t i = op2_array_index; i < op2_array_end; i += 1, next_index += 1) {
12475 out_array_val->data.x_array.s_none.elements[next_index] = op2_array_val->data.x_array.s_none.elements[i];
12479 out_array_val->data.x_array.data.s_none.elements[next_index] = op2_array_val->data.x_array.data.s_none.elements[i];
1247612480 }
1247712481 if (next_index < new_len) {
12478 ConstExprValue *null_byte = &out_array_val->data.x_array.s_none.elements[next_index];
12482 ConstExprValue *null_byte = &out_array_val->data.x_array.data.s_none.elements[next_index];
1247912483 init_const_unsigned_negative(null_byte, child_type, 0, false);
1248012484 next_index += 1;
1248112485 }
......@@ -12524,12 +12528,14 @@ static ZigType *ir_analyze_array_mult(IrAnalyze *ira, IrInstructionBinOp *instru
1252412528 return get_array_type(ira->codegen, child_type, new_array_len);
1252512529 }
1252612530
12527 out_val->data.x_array.s_none.elements = create_const_vals(new_array_len);
12531 // TODO optimize the buf case
12532 expand_undef_array(ira->codegen, array_val);
12533 out_val->data.x_array.data.s_none.elements = create_const_vals(new_array_len);
1252812534
1252912535 uint64_t i = 0;
1253012536 for (uint64_t x = 0; x < mult_amt; x += 1) {
1253112537 for (uint64_t y = 0; y < old_array_len; y += 1) {
12532 out_val->data.x_array.s_none.elements[i] = array_val->data.x_array.s_none.elements[y];
12538 out_val->data.x_array.data.s_none.elements[i] = array_val->data.x_array.data.s_none.elements[y];
1253312539 i += 1;
1253412540 }
1253512541 }
......@@ -13502,10 +13508,10 @@ static ZigType *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call_instr
1350213508
1350313509 // Fork a scope of the function with known values for the parameters.
1350413510 Scope *parent_scope = fn_entry->fndef_scope->base.parent;
13505 ZigFn *impl_fn = create_fn(fn_proto_node);
13511 ZigFn *impl_fn = create_fn(ira->codegen, fn_proto_node);
1350613512 impl_fn->param_source_nodes = allocate<AstNode *>(new_fn_arg_count);
1350713513 buf_init_from_buf(&impl_fn->symbol_name, &fn_entry->symbol_name);
13508 impl_fn->fndef_scope = create_fndef_scope(impl_fn->body_node, parent_scope, impl_fn);
13514 impl_fn->fndef_scope = create_fndef_scope(ira->codegen, impl_fn->body_node, parent_scope, impl_fn);
1350913515 impl_fn->child_scope = &impl_fn->fndef_scope->base;
1351013516 FnTypeId inst_fn_type_id = {0};
1351113517 init_fn_type_id(&inst_fn_type_id, fn_proto_node, new_fn_arg_count);
......@@ -16073,7 +16079,7 @@ static ZigType *ir_analyze_instruction_switch_br(IrAnalyze *ira,
1607316079 if (!case_val)
1607416080 return ir_unreach_error(ira);
1607516081
16076 if (const_values_equal(target_val, case_val)) {
16082 if (const_values_equal(ira->codegen, target_val, case_val)) {
1607716083 old_dest_block = old_case->block;
1607816084 break;
1607916085 }
......@@ -16652,7 +16658,7 @@ static ZigType *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
1665216658 ConstExprValue const_val = {};
1665316659 const_val.special = ConstValSpecialStatic;
1665416660 const_val.type = fixed_size_array_type;
16655 const_val.data.x_array.s_none.elements = create_const_vals(elem_count);
16661 const_val.data.x_array.data.s_none.elements = create_const_vals(elem_count);
1665616662
1665716663 bool is_comptime = ir_should_inline(ira->new_irb.exec, instruction->base.scope);
1665816664
......@@ -16677,7 +16683,7 @@ static ZigType *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
1667716683 if (!elem_val)
1667816684 return ira->codegen->builtin_types.entry_invalid;
1667916685
16680 copy_const_val(&const_val.data.x_array.s_none.elements[i], elem_val, true);
16686 copy_const_val(&const_val.data.x_array.data.s_none.elements[i], elem_val, true);
1668116687 } else {
1668216688 first_non_const_instruction = casted_arg;
1668316689 const_val.special = ConstValSpecialRuntime;
......@@ -16689,7 +16695,7 @@ static ZigType *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
1668916695 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
1669016696 *out_val = const_val;
1669116697 for (size_t i = 0; i < elem_count; i += 1) {
16692 ConstExprValue *elem_val = &out_val->data.x_array.s_none.elements[i];
16698 ConstExprValue *elem_val = &out_val->data.x_array.data.s_none.elements[i];
1669316699 ConstParent *parent = get_const_val_parent(ira->codegen, elem_val);
1669416700 if (parent != nullptr) {
1669516701 parent->id = ConstParentIdArray;
......@@ -17146,8 +17152,8 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, Sco
1714617152 definition_array->special = ConstValSpecialStatic;
1714717153 definition_array->type = get_array_type(ira->codegen, type_info_definition_type, definition_count);
1714817154 definition_array->data.x_array.special = ConstArraySpecialNone;
17149 definition_array->data.x_array.s_none.parent.id = ConstParentIdNone;
17150 definition_array->data.x_array.s_none.elements = create_const_vals(definition_count);
17155 definition_array->data.x_array.data.s_none.parent.id = ConstParentIdNone;
17156 definition_array->data.x_array.data.s_none.elements = create_const_vals(definition_count);
1715117157 init_const_slice(ira->codegen, out_val, definition_array, 0, definition_count, false);
1715217158
1715317159 // Loop through the definitions and generate info.
......@@ -17164,7 +17170,7 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, Sco
1716417170 continue;
1716517171 }
1716617172
17167 ConstExprValue *definition_val = &definition_array->data.x_array.s_none.elements[definition_index];
17173 ConstExprValue *definition_val = &definition_array->data.x_array.data.s_none.elements[definition_index];
1716817174
1716917175 definition_val->special = ConstValSpecialStatic;
1717017176 definition_val->type = type_info_definition_type;
......@@ -17293,15 +17299,15 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, Sco
1729317299 fn_arg_name_array->type = get_array_type(ira->codegen,
1729417300 get_slice_type(ira->codegen, u8_ptr), fn_arg_count);
1729517301 fn_arg_name_array->data.x_array.special = ConstArraySpecialNone;
17296 fn_arg_name_array->data.x_array.s_none.parent.id = ConstParentIdNone;
17297 fn_arg_name_array->data.x_array.s_none.elements = create_const_vals(fn_arg_count);
17302 fn_arg_name_array->data.x_array.data.s_none.parent.id = ConstParentIdNone;
17303 fn_arg_name_array->data.x_array.data.s_none.elements = create_const_vals(fn_arg_count);
1729817304
1729917305 init_const_slice(ira->codegen, &fn_def_fields[8], fn_arg_name_array, 0, fn_arg_count, false);
1730017306
1730117307 for (size_t fn_arg_index = 0; fn_arg_index < fn_arg_count; fn_arg_index++)
1730217308 {
1730317309 ZigVar *arg_var = fn_entry->variable_list.at(fn_arg_index);
17304 ConstExprValue *fn_arg_name_val = &fn_arg_name_array->data.x_array.s_none.elements[fn_arg_index];
17310 ConstExprValue *fn_arg_name_val = &fn_arg_name_array->data.x_array.data.s_none.elements[fn_arg_index];
1730517311 ConstExprValue *arg_name = create_const_str_lit(ira->codegen, &arg_var->name);
1730617312 init_const_slice(ira->codegen, fn_arg_name_val, arg_name, 0, buf_len(&arg_var->name), true);
1730717313 fn_arg_name_val->data.x_struct.parent.id = ConstParentIdArray;
......@@ -17593,15 +17599,15 @@ static Error ir_make_type_info_value(IrAnalyze *ira, ZigType *type_entry, ConstE
1759317599 enum_field_array->special = ConstValSpecialStatic;
1759417600 enum_field_array->type = get_array_type(ira->codegen, type_info_enum_field_type, enum_field_count);
1759517601 enum_field_array->data.x_array.special = ConstArraySpecialNone;
17596 enum_field_array->data.x_array.s_none.parent.id = ConstParentIdNone;
17597 enum_field_array->data.x_array.s_none.elements = create_const_vals(enum_field_count);
17602 enum_field_array->data.x_array.data.s_none.parent.id = ConstParentIdNone;
17603 enum_field_array->data.x_array.data.s_none.elements = create_const_vals(enum_field_count);
1759817604
1759917605 init_const_slice(ira->codegen, &fields[2], enum_field_array, 0, enum_field_count, false);
1760017606
1760117607 for (uint32_t enum_field_index = 0; enum_field_index < enum_field_count; enum_field_index++)
1760217608 {
1760317609 TypeEnumField *enum_field = &type_entry->data.enumeration.fields[enum_field_index];
17604 ConstExprValue *enum_field_val = &enum_field_array->data.x_array.s_none.elements[enum_field_index];
17610 ConstExprValue *enum_field_val = &enum_field_array->data.x_array.data.s_none.elements[enum_field_index];
1760517611 make_enum_field_val(ira, enum_field_val, enum_field, type_info_enum_field_type);
1760617612 enum_field_val->data.x_struct.parent.id = ConstParentIdArray;
1760717613 enum_field_val->data.x_struct.parent.data.p_array.array_val = enum_field_array;
......@@ -17632,13 +17638,13 @@ static Error ir_make_type_info_value(IrAnalyze *ira, ZigType *type_entry, ConstE
1763217638 error_array->special = ConstValSpecialStatic;
1763317639 error_array->type = get_array_type(ira->codegen, type_info_error_type, error_count);
1763417640 error_array->data.x_array.special = ConstArraySpecialNone;
17635 error_array->data.x_array.s_none.parent.id = ConstParentIdNone;
17636 error_array->data.x_array.s_none.elements = create_const_vals(error_count);
17641 error_array->data.x_array.data.s_none.parent.id = ConstParentIdNone;
17642 error_array->data.x_array.data.s_none.elements = create_const_vals(error_count);
1763717643
1763817644 init_const_slice(ira->codegen, &fields[0], error_array, 0, error_count, false);
1763917645 for (uint32_t error_index = 0; error_index < error_count; error_index++) {
1764017646 ErrorTableEntry *error = type_entry->data.error_set.errors[error_index];
17641 ConstExprValue *error_val = &error_array->data.x_array.s_none.elements[error_index];
17647 ConstExprValue *error_val = &error_array->data.x_array.data.s_none.elements[error_index];
1764217648
1764317649 error_val->special = ConstValSpecialStatic;
1764417650 error_val->type = type_info_error_type;
......@@ -17727,8 +17733,8 @@ static Error ir_make_type_info_value(IrAnalyze *ira, ZigType *type_entry, ConstE
1772717733 union_field_array->special = ConstValSpecialStatic;
1772817734 union_field_array->type = get_array_type(ira->codegen, type_info_union_field_type, union_field_count);
1772917735 union_field_array->data.x_array.special = ConstArraySpecialNone;
17730 union_field_array->data.x_array.s_none.parent.id = ConstParentIdNone;
17731 union_field_array->data.x_array.s_none.elements = create_const_vals(union_field_count);
17736 union_field_array->data.x_array.data.s_none.parent.id = ConstParentIdNone;
17737 union_field_array->data.x_array.data.s_none.elements = create_const_vals(union_field_count);
1773217738
1773317739 init_const_slice(ira->codegen, &fields[2], union_field_array, 0, union_field_count, false);
1773417740
......@@ -17736,7 +17742,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, ZigType *type_entry, ConstE
1773617742
1773717743 for (uint32_t union_field_index = 0; union_field_index < union_field_count; union_field_index++) {
1773817744 TypeUnionField *union_field = &type_entry->data.unionation.fields[union_field_index];
17739 ConstExprValue *union_field_val = &union_field_array->data.x_array.s_none.elements[union_field_index];
17745 ConstExprValue *union_field_val = &union_field_array->data.x_array.data.s_none.elements[union_field_index];
1774017746
1774117747 union_field_val->special = ConstValSpecialStatic;
1774217748 union_field_val->type = type_info_union_field_type;
......@@ -17800,14 +17806,14 @@ static Error ir_make_type_info_value(IrAnalyze *ira, ZigType *type_entry, ConstE
1780017806 struct_field_array->special = ConstValSpecialStatic;
1780117807 struct_field_array->type = get_array_type(ira->codegen, type_info_struct_field_type, struct_field_count);
1780217808 struct_field_array->data.x_array.special = ConstArraySpecialNone;
17803 struct_field_array->data.x_array.s_none.parent.id = ConstParentIdNone;
17804 struct_field_array->data.x_array.s_none.elements = create_const_vals(struct_field_count);
17809 struct_field_array->data.x_array.data.s_none.parent.id = ConstParentIdNone;
17810 struct_field_array->data.x_array.data.s_none.elements = create_const_vals(struct_field_count);
1780517811
1780617812 init_const_slice(ira->codegen, &fields[1], struct_field_array, 0, struct_field_count, false);
1780717813
1780817814 for (uint32_t struct_field_index = 0; struct_field_index < struct_field_count; struct_field_index++) {
1780917815 TypeStructField *struct_field = &type_entry->data.structure.fields[struct_field_index];
17810 ConstExprValue *struct_field_val = &struct_field_array->data.x_array.s_none.elements[struct_field_index];
17816 ConstExprValue *struct_field_val = &struct_field_array->data.x_array.data.s_none.elements[struct_field_index];
1781117817
1781217818 struct_field_val->special = ConstValSpecialStatic;
1781317819 struct_field_val->type = type_info_struct_field_type;
......@@ -17906,15 +17912,15 @@ static Error ir_make_type_info_value(IrAnalyze *ira, ZigType *type_entry, ConstE
1790617912 fn_arg_array->special = ConstValSpecialStatic;
1790717913 fn_arg_array->type = get_array_type(ira->codegen, type_info_fn_arg_type, fn_arg_count);
1790817914 fn_arg_array->data.x_array.special = ConstArraySpecialNone;
17909 fn_arg_array->data.x_array.s_none.parent.id = ConstParentIdNone;
17910 fn_arg_array->data.x_array.s_none.elements = create_const_vals(fn_arg_count);
17915 fn_arg_array->data.x_array.data.s_none.parent.id = ConstParentIdNone;
17916 fn_arg_array->data.x_array.data.s_none.elements = create_const_vals(fn_arg_count);
1791117917
1791217918 init_const_slice(ira->codegen, &fields[5], fn_arg_array, 0, fn_arg_count, false);
1791317919
1791417920 for (size_t fn_arg_index = 0; fn_arg_index < fn_arg_count; fn_arg_index++)
1791517921 {
1791617922 FnTypeParamInfo *fn_param_info = &type_entry->data.fn.fn_type_id.param_info[fn_arg_index];
17917 ConstExprValue *fn_arg_val = &fn_arg_array->data.x_array.s_none.elements[fn_arg_index];
17923 ConstExprValue *fn_arg_val = &fn_arg_array->data.x_array.data.s_none.elements[fn_arg_index];
1791817924
1791917925 fn_arg_val->special = ConstValSpecialStatic;
1792017926 fn_arg_val->type = type_info_fn_arg_type;
......@@ -18059,7 +18065,7 @@ static ZigType *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstructionCIm
1805918065 assert(node->type == NodeTypeFnCallExpr);
1806018066 AstNode *block_node = node->data.fn_call_expr.params.at(0);
1806118067
18062 ScopeCImport *cimport_scope = create_cimport_scope(node, instruction->base.scope);
18068 ScopeCImport *cimport_scope = create_cimport_scope(ira->codegen, node, instruction->base.scope);
1806318069
1806418070 // Execute the C import block like an inline function
1806518071 ZigType *void_type = ira->codegen->builtin_types.entry_void;
......@@ -18072,7 +18078,7 @@ static ZigType *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstructionCIm
1807218078 find_libc_include_path(ira->codegen);
1807318079
1807418080 ImportTableEntry *child_import = allocate<ImportTableEntry>(1);
18075 child_import->decls_scope = create_decls_scope(node, nullptr, nullptr, child_import);
18081 child_import->decls_scope = create_decls_scope(ira->codegen, node, nullptr, nullptr, child_import);
1807618082 child_import->c_import_node = node;
1807718083 child_import->package = new_anonymous_package();
1807818084 child_import->package->package_table.put(buf_create_from_str("builtin"), ira->codegen->compile_var_package);
......@@ -18826,7 +18832,7 @@ static ZigType *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructionMemse
1882618832 {
1882718833 ConstExprValue *array_val = dest_ptr_val->data.x_ptr.data.base_array.array_val;
1882818834 expand_undef_array(ira->codegen, array_val);
18829 dest_elements = array_val->data.x_array.s_none.elements;
18835 dest_elements = array_val->data.x_array.data.s_none.elements;
1883018836 start = dest_ptr_val->data.x_ptr.data.base_array.elem_index;
1883118837 bound_end = array_val->type->data.array.len;
1883218838 break;
......@@ -18940,7 +18946,7 @@ static ZigType *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructionMemcp
1894018946 {
1894118947 ConstExprValue *array_val = dest_ptr_val->data.x_ptr.data.base_array.array_val;
1894218948 expand_undef_array(ira->codegen, array_val);
18943 dest_elements = array_val->data.x_array.s_none.elements;
18949 dest_elements = array_val->data.x_array.data.s_none.elements;
1894418950 dest_start = dest_ptr_val->data.x_ptr.data.base_array.elem_index;
1894518951 dest_end = array_val->type->data.array.len;
1894618952 break;
......@@ -18976,7 +18982,7 @@ static ZigType *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructionMemcp
1897618982 {
1897718983 ConstExprValue *array_val = src_ptr_val->data.x_ptr.data.base_array.array_val;
1897818984 expand_undef_array(ira->codegen, array_val);
18979 src_elements = array_val->data.x_array.s_none.elements;
18985 src_elements = array_val->data.x_array.data.s_none.elements;
1898018986 src_start = src_ptr_val->data.x_ptr.data.base_array.elem_index;
1898118987 src_end = array_val->type->data.array.len;
1898218988 break;
......@@ -20282,9 +20288,10 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue
2028220288 case ZigTypeIdArray:
2028320289 {
2028420290 size_t buf_i = 0;
20291 // TODO optimize the buf case
2028520292 expand_undef_array(codegen, val);
2028620293 for (size_t elem_i = 0; elem_i < val->type->data.array.len; elem_i += 1) {
20287 ConstExprValue *elem = &val->data.x_array.s_none.elements[elem_i];
20294 ConstExprValue *elem = &val->data.x_array.data.s_none.elements[elem_i];
2028820295 buf_write_value_bytes(codegen, &buf[buf_i], elem);
2028920296 buf_i += type_size(codegen, elem->type);
2029020297 }
test/behavior.zig+1
......@@ -8,6 +8,7 @@ comptime {
88 _ = @import("cases/atomics.zig");
99 _ = @import("cases/bitcast.zig");
1010 _ = @import("cases/bool.zig");
11 _ = @import("cases/bugs/1076.zig");
1112 _ = @import("cases/bugs/1111.zig");
1213 _ = @import("cases/bugs/1277.zig");
1314 _ = @import("cases/bugs/1322.zig");
test/cases/bugs/1076.zig created+16
......@@ -0,0 +1,16 @@
1const std = @import("std");
2const mem = std.mem;
3const assert = std.debug.assert;
4
5test "comptime code should not modify constant data" {
6 testCastPtrOfArrayToSliceAndPtr();
7 comptime testCastPtrOfArrayToSliceAndPtr();
8}
9
10fn testCastPtrOfArrayToSliceAndPtr() void {
11 var array = "aoeu";
12 const x: [*]u8 = &array;
13 x[0] += 1;
14 assert(mem.eql(u8, array[0..], "boeu"));
15}
16
test/cases/cast.zig+1-1
......@@ -400,7 +400,7 @@ test "single-item pointer of array to slice and to unknown length pointer" {
400400}
401401
402402fn testCastPtrOfArrayToSliceAndPtr() void {
403 var array = "ao" ++ "eu"; // TODO https://github.com/ziglang/zig/issues/1076
403 var array = "aoeu";
404404 const x: [*]u8 = &array;
405405 x[0] += 1;
406406 assert(mem.eql(u8, array[0..], "boeu"));