authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-04-23 13:22:36-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-04-23 14:34:40-04:00
log6de33ded81981554ccffc3ecbfcd5b0f628cf502
tree520cfac3890b90d5357866a6c4b6e7e9509ee735
parent2ed47070efa5933365da724e79f6ed87d603ee27

make undefined as a constant value lazy

closes #268

11 files changed, 139 insertions(+), 93 deletions(-)

src/all_types.hpp+10-2
......@@ -98,9 +98,17 @@ struct ConstStructValue {
9898 ConstParent parent;
9999};
100100
101enum ConstArraySpecial {
102 ConstArraySpecialNone,
103 ConstArraySpecialUndef,
104};
105
101106struct ConstArrayValue {
102 ConstExprValue *elements;
103 ConstParent parent;
107 ConstArraySpecial special;
108 struct {
109 ConstExprValue *elements;
110 ConstParent parent;
111 } s_none;
104112};
105113
106114enum ConstPtrSpecial {
src/analyze.cpp+45-31
......@@ -2727,7 +2727,7 @@ void analyze_fn_ir(CodeGen *g, FnTableEntry *fn_table_entry, AstNode *return_typ
27272727
27282728 if (g->verbose) {
27292729 fprintf(stderr, "{ // (analyzed)\n");
2730 ir_print(stderr, &fn_table_entry->analyzed_executable, 4);
2730 ir_print(g, stderr, &fn_table_entry->analyzed_executable, 4);
27312731 fprintf(stderr, "}\n");
27322732 }
27332733
......@@ -2766,9 +2766,9 @@ static void analyze_fn_body(CodeGen *g, FnTableEntry *fn_table_entry) {
27662766 }
27672767 if (g->verbose) {
27682768 fprintf(stderr, "\n");
2769 ast_render(stderr, fn_table_entry->body_node, 4);
2769 ast_render(g, stderr, fn_table_entry->body_node, 4);
27702770 fprintf(stderr, "\n{ // (IR)\n");
2771 ir_print(stderr, &fn_table_entry->ir_executable, 4);
2771 ir_print(g, stderr, &fn_table_entry->ir_executable, 4);
27722772 fprintf(stderr, "}\n");
27732773 }
27742774
......@@ -3355,10 +3355,10 @@ bool type_requires_comptime(TypeTableEntry *type_entry) {
33553355void init_const_str_lit(CodeGen *g, ConstExprValue *const_val, Buf *str) {
33563356 const_val->special = ConstValSpecialStatic;
33573357 const_val->type = get_array_type(g, g->builtin_types.entry_u8, buf_len(str));
3358 const_val->data.x_array.elements = allocate<ConstExprValue>(buf_len(str));
3358 const_val->data.x_array.s_none.elements = allocate<ConstExprValue>(buf_len(str));
33593359
33603360 for (size_t i = 0; i < buf_len(str); i += 1) {
3361 ConstExprValue *this_char = &const_val->data.x_array.elements[i];
3361 ConstExprValue *this_char = &const_val->data.x_array.s_none.elements[i];
33623362 this_char->special = ConstValSpecialStatic;
33633363 this_char->type = g->builtin_types.entry_u8;
33643364 bignum_init_unsigned(&this_char->data.x_bignum, (uint8_t)buf_ptr(str)[i]);
......@@ -3377,14 +3377,14 @@ void init_const_c_str_lit(CodeGen *g, ConstExprValue *const_val, Buf *str) {
33773377 ConstExprValue *array_val = allocate<ConstExprValue>(1);
33783378 array_val->special = ConstValSpecialStatic;
33793379 array_val->type = get_array_type(g, g->builtin_types.entry_u8, len_with_null);
3380 array_val->data.x_array.elements = allocate<ConstExprValue>(len_with_null);
3380 array_val->data.x_array.s_none.elements = allocate<ConstExprValue>(len_with_null);
33813381 for (size_t i = 0; i < buf_len(str); i += 1) {
3382 ConstExprValue *this_char = &array_val->data.x_array.elements[i];
3382 ConstExprValue *this_char = &array_val->data.x_array.s_none.elements[i];
33833383 this_char->special = ConstValSpecialStatic;
33843384 this_char->type = g->builtin_types.entry_u8;
33853385 bignum_init_unsigned(&this_char->data.x_bignum, (uint8_t)buf_ptr(str)[i]);
33863386 }
3387 ConstExprValue *null_char = &array_val->data.x_array.elements[len_with_null - 1];
3387 ConstExprValue *null_char = &array_val->data.x_array.s_none.elements[len_with_null - 1];
33883388 null_char->special = ConstValSpecialStatic;
33893389 null_char->type = g->builtin_types.entry_u8;
33903390 bignum_init_unsigned(&null_char->data.x_bignum, 0);
......@@ -3564,19 +3564,7 @@ void init_const_undefined(CodeGen *g, ConstExprValue *const_val) {
35643564 TypeTableEntry *wanted_type = const_val->type;
35653565 if (wanted_type->id == TypeTableEntryIdArray) {
35663566 const_val->special = ConstValSpecialStatic;
3567 size_t elem_count = wanted_type->data.array.len;
3568 const_val->data.x_array.elements = allocate<ConstExprValue>(elem_count);
3569 for (size_t i = 0; i < elem_count; i += 1) {
3570 ConstExprValue *element_val = &const_val->data.x_array.elements[i];
3571 element_val->type = wanted_type->data.array.child_type;
3572 init_const_undefined(g, element_val);
3573 ConstParent *parent = get_const_val_parent(element_val);
3574 if (parent != nullptr) {
3575 parent->id = ConstParentIdArray;
3576 parent->data.p_array.array_val = const_val;
3577 parent->data.p_array.elem_index = i;
3578 }
3579 }
3567 const_val->data.x_array.special = ConstArraySpecialUndef;
35803568 } else if (wanted_type->id == TypeTableEntryIdStruct) {
35813569 ensure_complete_type(g, wanted_type);
35823570
......@@ -3588,7 +3576,7 @@ void init_const_undefined(CodeGen *g, ConstExprValue *const_val) {
35883576 field_val->type = wanted_type->data.structure.fields[i].type_entry;
35893577 assert(field_val->type);
35903578 init_const_undefined(g, field_val);
3591 ConstParent *parent = get_const_val_parent(field_val);
3579 ConstParent *parent = get_const_val_parent(g, field_val);
35923580 if (parent != nullptr) {
35933581 parent->id = ConstParentIdStruct;
35943582 parent->data.p_struct.struct_val = const_val;
......@@ -3802,7 +3790,7 @@ void eval_min_max_value(CodeGen *g, TypeTableEntry *type_entry, ConstExprValue *
38023790 }
38033791}
38043792
3805void render_const_value(Buf *buf, ConstExprValue *const_val) {
3793void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) {
38063794 switch (const_val->special) {
38073795 case ConstValSpecialRuntime:
38083796 buf_appendf(buf, "(runtime value)");
......@@ -3872,7 +3860,7 @@ void render_const_value(Buf *buf, ConstExprValue *const_val) {
38723860 case ConstPtrSpecialRef:
38733861 case ConstPtrSpecialBaseStruct:
38743862 buf_appendf(buf, "&");
3875 render_const_value(buf, const_ptr_pointee(const_val));
3863 render_const_value(g, buf, const_ptr_pointee(g, const_val));
38763864 return;
38773865 case ConstPtrSpecialBaseArray:
38783866 if (const_val->data.x_ptr.data.base_array.is_cstr) {
......@@ -3880,7 +3868,7 @@ void render_const_value(Buf *buf, ConstExprValue *const_val) {
38803868 return;
38813869 } else {
38823870 buf_appendf(buf, "&");
3883 render_const_value(buf, const_ptr_pointee(const_val));
3871 render_const_value(g, buf, const_ptr_pointee(g, const_val));
38843872 return;
38853873 }
38863874 case ConstPtrSpecialHardCodedAddr:
......@@ -3910,6 +3898,11 @@ void render_const_value(Buf *buf, ConstExprValue *const_val) {
39103898 TypeTableEntry *child_type = type_entry->data.array.child_type;
39113899 uint64_t len = type_entry->data.array.len;
39123900
3901 if (const_val->data.x_array.special == ConstArraySpecialUndef) {
3902 buf_append_str(buf, "undefined");
3903 return;
3904 }
3905
39133906 // if it's []u8, assume UTF-8 and output a string
39143907 if (child_type->id == TypeTableEntryIdInt &&
39153908 child_type->data.integral.bit_count == 8 &&
......@@ -3917,7 +3910,7 @@ void render_const_value(Buf *buf, ConstExprValue *const_val) {
39173910 {
39183911 buf_append_char(buf, '"');
39193912 for (uint64_t i = 0; i < len; i += 1) {
3920 ConstExprValue *child_value = &const_val->data.x_array.elements[i];
3913 ConstExprValue *child_value = &const_val->data.x_array.s_none.elements[i];
39213914 uint64_t big_c = child_value->data.x_bignum.data.x_uint;
39223915 assert(big_c <= UINT8_MAX);
39233916 uint8_t c = (uint8_t)big_c;
......@@ -3935,8 +3928,8 @@ void render_const_value(Buf *buf, ConstExprValue *const_val) {
39353928 for (uint64_t i = 0; i < len; i += 1) {
39363929 if (i != 0)
39373930 buf_appendf(buf, ",");
3938 ConstExprValue *child_value = &const_val->data.x_array.elements[i];
3939 render_const_value(buf, child_value);
3931 ConstExprValue *child_value = &const_val->data.x_array.s_none.elements[i];
3932 render_const_value(g, buf, child_value);
39403933 }
39413934 buf_appendf(buf, "}");
39423935 return;
......@@ -3954,7 +3947,7 @@ void render_const_value(Buf *buf, ConstExprValue *const_val) {
39543947 case TypeTableEntryIdMaybe:
39553948 {
39563949 if (const_val->data.x_maybe) {
3957 render_const_value(buf, const_val->data.x_maybe);
3950 render_const_value(g, buf, const_val->data.x_maybe);
39583951 } else {
39593952 buf_appendf(buf, "null");
39603953 }
......@@ -4167,11 +4160,32 @@ bool zig_llvm_fn_key_eql(ZigLLVMFnKey a, ZigLLVMFnKey b) {
41674160 zig_unreachable();
41684161}
41694162
4170ConstParent *get_const_val_parent(ConstExprValue *value) {
4163void expand_undef_array(CodeGen *g, ConstExprValue *const_val) {
4164 assert(const_val->type->id == TypeTableEntryIdArray);
4165 if (const_val->data.x_array.special == ConstArraySpecialUndef) {
4166 const_val->data.x_array.special = ConstArraySpecialNone;
4167 size_t elem_count = const_val->type->data.array.len;
4168 const_val->data.x_array.s_none.elements = allocate<ConstExprValue>(elem_count);
4169 for (size_t i = 0; i < elem_count; i += 1) {
4170 ConstExprValue *element_val = &const_val->data.x_array.s_none.elements[i];
4171 element_val->type = const_val->type->data.array.child_type;
4172 init_const_undefined(g, element_val);
4173 ConstParent *parent = get_const_val_parent(g, element_val);
4174 if (parent != nullptr) {
4175 parent->id = ConstParentIdArray;
4176 parent->data.p_array.array_val = const_val;
4177 parent->data.p_array.elem_index = i;
4178 }
4179 }
4180 }
4181}
4182
4183ConstParent *get_const_val_parent(CodeGen *g, ConstExprValue *value) {
41714184 assert(value->type);
41724185 TypeTableEntry *type_entry = value->type;
41734186 if (type_entry->id == TypeTableEntryIdArray) {
4174 return &value->data.x_array.parent;
4187 expand_undef_array(g, value);
4188 return &value->data.x_array.s_none.parent;
41754189 } else if (type_entry->id == TypeTableEntryIdStruct) {
41764190 return &value->data.x_struct.parent;
41774191 }
src/analyze.hpp+3-2
......@@ -84,7 +84,7 @@ void eval_min_max_value(CodeGen *g, TypeTableEntry *type_entry, ConstExprValue *
8484int64_t min_signed_val(TypeTableEntry *type_entry);
8585uint64_t max_unsigned_val(TypeTableEntry *type_entry);
8686
87void render_const_value(Buf *buf, ConstExprValue *const_val);
87void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val);
8888void define_local_param_variables(CodeGen *g, FnTableEntry *fn_table_entry, VariableTableEntry **arg_vars);
8989void analyze_fn_ir(CodeGen *g, FnTableEntry *fn_table_entry, AstNode *return_type_node);
9090
......@@ -145,8 +145,9 @@ ConstExprValue *create_const_arg_tuple(CodeGen *g, size_t arg_index_start, size_
145145void init_const_undefined(CodeGen *g, ConstExprValue *const_val);
146146
147147TypeTableEntry *make_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits);
148ConstParent *get_const_val_parent(ConstExprValue *value);
148ConstParent *get_const_val_parent(CodeGen *g, ConstExprValue *value);
149149FnTableEntry *get_extern_panic_fn(CodeGen *g);
150150TypeTableEntry *create_enum_tag_type(CodeGen *g, TypeTableEntry *enum_type, TypeTableEntry *int_type);
151void expand_undef_array(CodeGen *g, ConstExprValue *const_val);
151152
152153#endif
src/ast_render.cpp+6-3
......@@ -273,6 +273,7 @@ void ast_print(FILE *f, AstNode *node, int indent) {
273273
274274
275275struct AstRender {
276 CodeGen *codegen;
276277 int indent;
277278 int indent_size;
278279 FILE *f;
......@@ -924,8 +925,9 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
924925}
925926
926927
927void ast_render(FILE *f, AstNode *node, int indent_size) {
928void ast_render(CodeGen *codegen, FILE *f, AstNode *node, int indent_size) {
928929 AstRender ar = {0};
930 ar.codegen = codegen;
929931 ar.f = f;
930932 ar.indent_size = indent_size;
931933 ar.indent = 0;
......@@ -1030,15 +1032,16 @@ static void ast_render_tld_var(AstRender *ar, Buf *name, TldVar *tld_var) {
10301032 } else {
10311033 Buf buf = BUF_INIT;
10321034 buf_resize(&buf, 0);
1033 render_const_value(&buf, var->value);
1035 render_const_value(ar->codegen, &buf, var->value);
10341036 fprintf(ar->f, "%s", buf_ptr(&buf));
10351037 }
10361038
10371039 fprintf(ar->f, ";\n");
10381040}
10391041
1040void ast_render_decls(FILE *f, int indent_size, ImportTableEntry *import) {
1042void ast_render_decls(CodeGen *codegen, FILE *f, int indent_size, ImportTableEntry *import) {
10411043 AstRender ar = {0};
1044 ar.codegen = codegen;
10421045 ar.f = f;
10431046 ar.indent_size = indent_size;
10441047 ar.indent = 0;
src/ast_render.hpp+2-2
......@@ -15,11 +15,11 @@
1515
1616void ast_print(FILE *f, AstNode *node, int indent);
1717
18void ast_render(FILE *f, AstNode *node, int indent_size);
18void ast_render(CodeGen *codegen, FILE *f, AstNode *node, int indent_size);
1919
2020const char *container_string(ContainerKind kind);
2121
22void ast_render_decls(FILE *f, int indent_size, ImportTableEntry *import);
22void ast_render_decls(CodeGen *codegen, FILE *f, int indent_size, ImportTableEntry *import);
2323
2424#endif
2525
src/codegen.cpp+9-4
......@@ -3051,7 +3051,8 @@ static LLVMValueRef gen_parent_ptr(CodeGen *g, ConstExprValue *val, ConstParent
30513051}
30523052
30533053static LLVMValueRef gen_const_ptr_array_recursive(CodeGen *g, ConstExprValue *array_const_val, size_t index) {
3054 ConstParent *parent = &array_const_val->data.x_array.parent;
3054 expand_undef_array(g, array_const_val);
3055 ConstParent *parent = &array_const_val->data.x_array.s_none.parent;
30553056 LLVMValueRef base_ptr = gen_parent_ptr(g, array_const_val, parent);
30563057
30573058 TypeTableEntry *usize = g->builtin_types.entry_usize;
......@@ -3283,9 +3284,13 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
32833284 case TypeTableEntryIdArray:
32843285 {
32853286 uint64_t len = type_entry->data.array.len;
3287 if (const_val->data.x_array.special == ConstArraySpecialUndef) {
3288 return LLVMGetUndef(type_entry->type_ref);
3289 }
3290
32863291 LLVMValueRef *values = allocate<LLVMValueRef>(len);
32873292 for (uint64_t i = 0; i < len; i += 1) {
3288 ConstExprValue *elem_value = &const_val->data.x_array.elements[i];
3293 ConstExprValue *elem_value = &const_val->data.x_array.s_none.elements[i];
32893294 values[i] = gen_const_val(g, elem_value);
32903295 }
32913296 return LLVMConstArray(LLVMTypeOf(values[0]), values, (unsigned)len);
......@@ -4577,11 +4582,11 @@ static void define_builtin_compile_vars(CodeGen *g) {
45774582 ConstExprValue *const_val = allocate<ConstExprValue>(1);
45784583 const_val->special = ConstValSpecialStatic;
45794584 const_val->type = get_array_type(g, str_type, g->link_libs.length);
4580 const_val->data.x_array.elements = allocate<ConstExprValue>(g->link_libs.length);
4585 const_val->data.x_array.s_none.elements = allocate<ConstExprValue>(g->link_libs.length);
45814586 for (size_t i = 0; i < g->link_libs.length; i += 1) {
45824587 Buf *link_lib_buf = g->link_libs.at(i);
45834588 ConstExprValue *array_val = create_const_str_lit(g, link_lib_buf);
4584 init_const_slice(g, &const_val->data.x_array.elements[i], array_val, 0, buf_len(link_lib_buf), true);
4589 init_const_slice(g, &const_val->data.x_array.s_none.elements[i], array_val, 0, buf_len(link_lib_buf), true);
45854590 }
45864591
45874592 add_compile_var(g, "link_libs", const_val);
src/ir.cpp+57-44
......@@ -56,7 +56,7 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, Scope *sc
5656static TypeTableEntry *ir_analyze_instruction(IrAnalyze *ira, IrInstruction *instruction);
5757static IrInstruction *ir_implicit_cast(IrAnalyze *ira, IrInstruction *value, TypeTableEntry *expected_type);
5858
59ConstExprValue *const_ptr_pointee(ConstExprValue *const_val) {
59ConstExprValue *const_ptr_pointee(CodeGen *g, ConstExprValue *const_val) {
6060 assert(const_val->type->id == TypeTableEntryIdPointer);
6161 assert(const_val->special == ConstValSpecialStatic);
6262 switch (const_val->data.x_ptr.special) {
......@@ -65,7 +65,8 @@ ConstExprValue *const_ptr_pointee(ConstExprValue *const_val) {
6565 case ConstPtrSpecialRef:
6666 return const_val->data.x_ptr.data.ref.pointee;
6767 case ConstPtrSpecialBaseArray:
68 return &const_val->data.x_ptr.data.base_array.array_val->data.x_array.elements[
68 expand_undef_array(g, const_val->data.x_ptr.data.base_array.array_val);
69 return &const_val->data.x_ptr.data.base_array.array_val->data.x_array.s_none.elements[
6970 const_val->data.x_ptr.data.base_array.elem_index];
7071 case ConstPtrSpecialBaseStruct:
7172 return &const_val->data.x_ptr.data.base_struct.struct_val->data.x_struct.fields[
......@@ -5503,16 +5504,16 @@ static IrInstruction *ir_gen_err_ok_or(IrBuilder *irb, Scope *parent_scope, AstN
55035504 return ir_build_phi(irb, parent_scope, node, 2, incoming_blocks, incoming_values);
55045505}
55055506
5506static bool render_instance_name_recursive(Buf *name, Scope *outer_scope, Scope *inner_scope) {
5507static bool render_instance_name_recursive(CodeGen *codegen, Buf *name, Scope *outer_scope, Scope *inner_scope) {
55075508 if (inner_scope == nullptr || inner_scope == outer_scope) return false;
5508 bool need_comma = render_instance_name_recursive(name, outer_scope, inner_scope->parent);
5509 bool need_comma = render_instance_name_recursive(codegen, name, outer_scope, inner_scope->parent);
55095510 if (inner_scope->id != ScopeIdVarDecl)
55105511 return need_comma;
55115512
55125513 ScopeVarDecl *var_scope = (ScopeVarDecl *)inner_scope;
55135514 if (need_comma)
55145515 buf_append_char(name, ',');
5515 render_const_value(name, var_scope->var->value);
5516 render_const_value(codegen, name, var_scope->var->value);
55165517 return true;
55175518}
55185519
......@@ -5529,7 +5530,7 @@ static IrInstruction *ir_gen_container_decl(IrBuilder *irb, Scope *parent_scope,
55295530 name = buf_alloc();
55305531 buf_append_buf(name, &fn_entry->symbol_name);
55315532 buf_appendf(name, "(");
5532 render_instance_name_recursive(name, &fn_entry->fndef_scope->base, irb->exec->begin_scope);
5533 render_instance_name_recursive(irb->codegen, name, &fn_entry->fndef_scope->base, irb->exec->begin_scope);
55335534 buf_appendf(name, ")");
55345535 } else {
55355536 name = buf_sprintf("(anonymous %s at %s:%zu:%zu)", container_string(kind),
......@@ -6523,9 +6524,9 @@ IrInstruction *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node
65236524
65246525 if (codegen->verbose) {
65256526 fprintf(stderr, "\nSource: ");
6526 ast_render(stderr, node, 4);
6527 ast_render(codegen, stderr, node, 4);
65276528 fprintf(stderr, "\n{ // (IR)\n");
6528 ir_print(stderr, &ir_executable, 4);
6529 ir_print(codegen, stderr, &ir_executable, 4);
65296530 fprintf(stderr, "}\n");
65306531 }
65316532 IrExecutable analyzed_executable = {0};
......@@ -6544,7 +6545,7 @@ IrInstruction *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node
65446545
65456546 if (codegen->verbose) {
65466547 fprintf(stderr, "{ // (analyzed)\n");
6547 ir_print(stderr, &analyzed_executable, 4);
6548 ir_print(codegen, stderr, &analyzed_executable, 4);
65486549 fprintf(stderr, "}\n");
65496550 }
65506551
......@@ -7305,7 +7306,7 @@ static IrInstruction *ir_get_deref(IrAnalyze *ira, IrInstruction *source_instruc
73057306 if (ptr->value.data.x_ptr.mut == ConstPtrMutComptimeConst ||
73067307 ptr->value.data.x_ptr.mut == ConstPtrMutComptimeVar)
73077308 {
7308 ConstExprValue *pointee = const_ptr_pointee(&ptr->value);
7309 ConstExprValue *pointee = const_ptr_pointee(ira->codegen, &ptr->value);
73097310 if (pointee->special != ConstValSpecialRuntime) {
73107311 IrInstruction *result = ir_create_const(&ira->new_irb, source_instruction->scope,
73117312 source_instruction->source_node, child_type);
......@@ -7441,12 +7442,17 @@ static Buf *ir_resolve_str(IrAnalyze *ira, IrInstruction *value) {
74417442
74427443 assert(ptr_field->data.x_ptr.special == ConstPtrSpecialBaseArray);
74437444 ConstExprValue *array_val = ptr_field->data.x_ptr.data.base_array.array_val;
7445 expand_undef_array(ira->codegen, array_val);
74447446 size_t len = len_field->data.x_bignum.data.x_uint;
74457447 Buf *result = buf_alloc();
74467448 buf_resize(result, len);
74477449 for (size_t i = 0; i < len; i += 1) {
74487450 size_t new_index = ptr_field->data.x_ptr.data.base_array.elem_index + i;
7449 ConstExprValue *char_val = &array_val->data.x_array.elements[new_index];
7451 ConstExprValue *char_val = &array_val->data.x_array.s_none.elements[new_index];
7452 if (char_val->special == ConstValSpecialUndef) {
7453 ir_add_error(ira, casted_value, buf_sprintf("use of undefined value"));
7454 return nullptr;
7455 }
74507456 uint64_t big_c = char_val->data.x_bignum.data.x_uint;
74517457 assert(big_c <= UINT8_MAX);
74527458 uint8_t c = (uint8_t)big_c;
......@@ -8007,17 +8013,19 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *
80078013 out_val->data.x_ptr.data.base_array.array_val = out_array_val;
80088014 out_val->data.x_ptr.data.base_array.elem_index = 0;
80098015 }
8010 out_array_val->data.x_array.elements = allocate<ConstExprValue>(new_len);
8016 out_array_val->data.x_array.s_none.elements = allocate<ConstExprValue>(new_len);
8017
8018 expand_undef_array(ira->codegen, op1_array_val);
80118019
80128020 size_t next_index = 0;
80138021 for (size_t i = op1_array_index; i < op1_array_end; i += 1, next_index += 1) {
8014 out_array_val->data.x_array.elements[next_index] = op1_array_val->data.x_array.elements[i];
8022 out_array_val->data.x_array.s_none.elements[next_index] = op1_array_val->data.x_array.s_none.elements[i];
80158023 }
80168024 for (size_t i = op2_array_index; i < op2_array_end; i += 1, next_index += 1) {
8017 out_array_val->data.x_array.elements[next_index] = op2_array_val->data.x_array.elements[i];
8025 out_array_val->data.x_array.s_none.elements[next_index] = op2_array_val->data.x_array.s_none.elements[i];
80188026 }
80198027 if (next_index < new_len) {
8020 ConstExprValue *null_byte = &out_array_val->data.x_array.elements[next_index];
8028 ConstExprValue *null_byte = &out_array_val->data.x_array.s_none.elements[next_index];
80218029 init_const_unsigned_negative(null_byte, child_type, 0, false);
80228030 next_index += 1;
80238031 }
......@@ -8061,12 +8069,14 @@ static TypeTableEntry *ir_analyze_array_mult(IrAnalyze *ira, IrInstructionBinOp
80618069 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
80628070
80638071 uint64_t new_array_len = array_len.data.x_uint;
8064 out_val->data.x_array.elements = allocate<ConstExprValue>(new_array_len);
8072 out_val->data.x_array.s_none.elements = allocate<ConstExprValue>(new_array_len);
8073
8074 expand_undef_array(ira->codegen, array_val);
80658075
80668076 uint64_t i = 0;
80678077 for (uint64_t x = 0; x < mult_amt; x += 1) {
80688078 for (uint64_t y = 0; y < old_array_len; y += 1) {
8069 out_val->data.x_array.elements[i] = array_val->data.x_array.elements[y];
8079 out_val->data.x_array.s_none.elements[i] = array_val->data.x_array.s_none.elements[y];
80708080 i += 1;
80718081 }
80728082 }
......@@ -8848,7 +8858,7 @@ static TypeTableEntry *ir_analyze_dereference(IrAnalyze *ira, IrInstructionUnOp
88488858 // one of the ptr instructions
88498859
88508860 if (instr_is_comptime(value)) {
8851 ConstExprValue *pointee = const_ptr_pointee(&value->value);
8861 ConstExprValue *pointee = const_ptr_pointee(ira->codegen, &value->value);
88528862 if (pointee->type == child_type) {
88538863 ConstExprValue *out_val = ir_build_const_from(ira, &un_op_instruction->base);
88548864 *out_val = *pointee;
......@@ -9222,7 +9232,7 @@ static TypeTableEntry *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruc
92229232 ConstExprValue *ptr_val = ir_resolve_const(ira, array_ptr, UndefBad);
92239233 if (!ptr_val)
92249234 return ira->codegen->builtin_types.entry_invalid;
9225 ConstExprValue *args_val = const_ptr_pointee(ptr_val);
9235 ConstExprValue *args_val = const_ptr_pointee(ira->codegen, ptr_val);
92269236 size_t start = args_val->data.x_arg_tuple.start_index;
92279237 size_t end = args_val->data.x_arg_tuple.end_index;
92289238 ConstExprValue *elem_index_val = ir_resolve_const(ira, elem_index, UndefBad);
......@@ -9276,7 +9286,7 @@ static TypeTableEntry *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruc
92769286 ConstExprValue *array_ptr_val;
92779287 if (array_ptr->value.special != ConstValSpecialRuntime &&
92789288 array_ptr->value.data.x_ptr.mut != ConstPtrMutRuntimeVar &&
9279 (array_ptr_val = const_ptr_pointee(&array_ptr->value)) &&
9289 (array_ptr_val = const_ptr_pointee(ira->codegen, &array_ptr->value)) &&
92809290 array_ptr_val->special != ConstValSpecialRuntime &&
92819291 (array_type->id != TypeTableEntryIdPointer ||
92829292 array_ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr))
......@@ -9431,7 +9441,7 @@ static TypeTableEntry *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field
94319441 return ira->codegen->builtin_types.entry_invalid;
94329442
94339443 if (ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr) {
9434 ConstExprValue *struct_val = const_ptr_pointee(ptr_val);
9444 ConstExprValue *struct_val = const_ptr_pointee(ira->codegen, ptr_val);
94359445 ConstExprValue *field_val = &struct_val->data.x_struct.fields[field->src_index];
94369446 TypeTableEntry *ptr_type = get_pointer_to_type_extra(ira->codegen, field_val->type,
94379447 is_const, is_volatile, 0, 0);
......@@ -9565,7 +9575,7 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru
95659575 return ira->codegen->builtin_types.entry_invalid;
95669576
95679577 assert(container_ptr->value.type->id == TypeTableEntryIdPointer);
9568 ConstExprValue *child_val = const_ptr_pointee(container_ptr_val);
9578 ConstExprValue *child_val = const_ptr_pointee(ira->codegen, container_ptr_val);
95699579
95709580 if (buf_eql_str(field_name, "len")) {
95719581 ConstExprValue *len_val = allocate<ConstExprValue>(1);
......@@ -9594,7 +9604,7 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru
95949604 assert(ptr_type->id == TypeTableEntryIdPointer);
95959605 child_type = ptr_type->data.pointer.child_type;
95969606 } else if (container_ptr->value.type->id == TypeTableEntryIdPointer) {
9597 ConstExprValue *child_val = const_ptr_pointee(container_ptr_val);
9607 ConstExprValue *child_val = const_ptr_pointee(ira->codegen, container_ptr_val);
95989608 child_type = child_val->data.x_type;
95999609 } else {
96009610 zig_unreachable();
......@@ -9688,7 +9698,7 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru
96889698 if (!container_ptr_val)
96899699 return ira->codegen->builtin_types.entry_invalid;
96909700
9691 ConstExprValue *namespace_val = const_ptr_pointee(container_ptr_val);
9701 ConstExprValue *namespace_val = const_ptr_pointee(ira->codegen, container_ptr_val);
96929702 assert(namespace_val->special == ConstValSpecialStatic);
96939703
96949704 ImportTableEntry *namespace_import = namespace_val->data.x_import;
......@@ -9761,7 +9771,7 @@ static TypeTableEntry *ir_analyze_instruction_store_ptr(IrAnalyze *ira, IrInstru
97619771 }
97629772 if (ptr->value.data.x_ptr.mut == ConstPtrMutComptimeVar) {
97639773 if (instr_is_comptime(casted_value)) {
9764 ConstExprValue *dest_val = const_ptr_pointee(&ptr->value);
9774 ConstExprValue *dest_val = const_ptr_pointee(ira->codegen, &ptr->value);
97659775 if (dest_val->special != ConstValSpecialRuntime) {
97669776 *dest_val = casted_value->value;
97679777 if (!ira->new_irb.current_basic_block->must_be_comptime_source_instr) {
......@@ -10630,7 +10640,7 @@ static TypeTableEntry *ir_analyze_instruction_switch_target(IrAnalyze *ira,
1063010640 TypeTableEntry *target_type = target_value_ptr->value.type->data.pointer.child_type;
1063110641 ConstExprValue *pointee_val = nullptr;
1063210642 if (instr_is_comptime(target_value_ptr)) {
10633 pointee_val = const_ptr_pointee(&target_value_ptr->value);
10643 pointee_val = const_ptr_pointee(ira->codegen, &target_value_ptr->value);
1063410644 if (pointee_val->special == ConstValSpecialRuntime)
1063510645 pointee_val = nullptr;
1063610646 }
......@@ -10730,7 +10740,7 @@ static TypeTableEntry *ir_analyze_instruction_switch_var(IrAnalyze *ira, IrInstr
1073010740 if (!target_value_ptr)
1073110741 return ira->codegen->builtin_types.entry_invalid;
1073210742
10733 ConstExprValue *pointee_val = const_ptr_pointee(target_val_ptr);
10743 ConstExprValue *pointee_val = const_ptr_pointee(ira->codegen, target_val_ptr);
1073410744 if (pointee_val->type->id == TypeTableEntryIdEnum) {
1073510745 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
1073610746 out_val->data.x_ptr.special = ConstPtrSpecialRef;
......@@ -10982,7 +10992,7 @@ static TypeTableEntry *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstru
1098210992
1098310993 for (size_t i = 0; i < instr_field_count; i += 1) {
1098410994 ConstExprValue *field_val = &out_val->data.x_struct.fields[i];
10985 ConstParent *parent = get_const_val_parent(field_val);
10995 ConstParent *parent = get_const_val_parent(ira->codegen, field_val);
1098610996 if (parent != nullptr) {
1098710997 parent->id = ConstParentIdStruct;
1098810998 parent->data.p_struct.field_index = i;
......@@ -11031,7 +11041,7 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira
1103111041 ConstExprValue const_val = {};
1103211042 const_val.special = ConstValSpecialStatic;
1103311043 const_val.type = fixed_size_array_type;
11034 const_val.data.x_array.elements = allocate<ConstExprValue>(elem_count);
11044 const_val.data.x_array.s_none.elements = allocate<ConstExprValue>(elem_count);
1103511045
1103611046 bool is_comptime = ir_should_inline(ira->new_irb.exec, instruction->base.scope);
1103711047
......@@ -11056,7 +11066,7 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira
1105611066 if (!elem_val)
1105711067 return ira->codegen->builtin_types.entry_invalid;
1105811068
11059 const_val.data.x_array.elements[i] = *elem_val;
11069 const_val.data.x_array.s_none.elements[i] = *elem_val;
1106011070 } else {
1106111071 first_non_const_instruction = casted_arg;
1106211072 const_val.special = ConstValSpecialRuntime;
......@@ -11068,8 +11078,8 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira
1106811078 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
1106911079 *out_val = const_val;
1107011080 for (size_t i = 0; i < elem_count; i += 1) {
11071 ConstExprValue *elem_val = &out_val->data.x_array.elements[i];
11072 ConstParent *parent = get_const_val_parent(elem_val);
11081 ConstExprValue *elem_val = &out_val->data.x_array.s_none.elements[i];
11082 ConstParent *parent = get_const_val_parent(ira->codegen, elem_val);
1107311083 if (parent != nullptr) {
1107411084 parent->id = ConstParentIdArray;
1107511085 parent->data.p_array.array_val = out_val;
......@@ -11254,7 +11264,7 @@ static TypeTableEntry *ir_analyze_instruction_compile_log(IrAnalyze *ira, IrInst
1125411264 if (type_is_invalid(msg->value.type))
1125511265 return ira->codegen->builtin_types.entry_invalid;
1125611266 buf_resize(&buf, 0);
11257 render_const_value(&buf, &msg->value);
11267 render_const_value(ira->codegen, &buf, &msg->value);
1125811268 const char *comma_str = (i != 0) ? ", " : "";
1125911269 fprintf(stderr, "%s%s", comma_str, buf_ptr(&buf));
1126011270 }
......@@ -11526,7 +11536,7 @@ static TypeTableEntry *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstruc
1152611536 if (ira->codegen->verbose) {
1152711537 fprintf(stderr, "\nC imports:\n");
1152811538 fprintf(stderr, "-----------\n");
11529 ast_render_decls(stderr, 4, child_import);
11539 ast_render_decls(ira->codegen, stderr, 4, child_import);
1153011540 }
1153111541
1153211542 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
......@@ -11925,7 +11935,8 @@ static TypeTableEntry *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructi
1192511935 case ConstPtrSpecialBaseArray:
1192611936 {
1192711937 ConstExprValue *array_val = dest_ptr_val->data.x_ptr.data.base_array.array_val;
11928 dest_elements = array_val->data.x_array.elements;
11938 expand_undef_array(ira->codegen, array_val);
11939 dest_elements = array_val->data.x_array.s_none.elements;
1192911940 start = dest_ptr_val->data.x_ptr.data.base_array.elem_index;
1193011941 bound_end = array_val->type->data.array.len;
1193111942 break;
......@@ -12016,7 +12027,8 @@ static TypeTableEntry *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructi
1201612027 case ConstPtrSpecialBaseArray:
1201712028 {
1201812029 ConstExprValue *array_val = dest_ptr_val->data.x_ptr.data.base_array.array_val;
12019 dest_elements = array_val->data.x_array.elements;
12030 expand_undef_array(ira->codegen, array_val);
12031 dest_elements = array_val->data.x_array.s_none.elements;
1202012032 dest_start = dest_ptr_val->data.x_ptr.data.base_array.elem_index;
1202112033 dest_end = array_val->type->data.array.len;
1202212034 break;
......@@ -12049,7 +12061,8 @@ static TypeTableEntry *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructi
1204912061 case ConstPtrSpecialBaseArray:
1205012062 {
1205112063 ConstExprValue *array_val = src_ptr_val->data.x_ptr.data.base_array.array_val;
12052 src_elements = array_val->data.x_array.elements;
12064 expand_undef_array(ira->codegen, array_val);
12065 src_elements = array_val->data.x_array.s_none.elements;
1205312066 src_start = src_ptr_val->data.x_ptr.data.base_array.elem_index;
1205412067 src_end = array_val->type->data.array.len;
1205512068 break;
......@@ -12138,12 +12151,12 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio
1213812151 size_t abs_offset;
1213912152 size_t rel_end;
1214012153 if (array_type->id == TypeTableEntryIdArray) {
12141 array_val = const_ptr_pointee(&ptr_ptr->value);
12154 array_val = const_ptr_pointee(ira->codegen, &ptr_ptr->value);
1214212155 abs_offset = 0;
1214312156 rel_end = array_type->data.array.len;
1214412157 parent_ptr = nullptr;
1214512158 } else if (array_type->id == TypeTableEntryIdPointer) {
12146 parent_ptr = const_ptr_pointee(&ptr_ptr->value);
12159 parent_ptr = const_ptr_pointee(ira->codegen, &ptr_ptr->value);
1214712160 switch (parent_ptr->data.x_ptr.special) {
1214812161 case ConstPtrSpecialInvalid:
1214912162 case ConstPtrSpecialDiscard:
......@@ -12165,7 +12178,7 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio
1216512178 break;
1216612179 }
1216712180 } else if (is_slice(array_type)) {
12168 ConstExprValue *slice_ptr = const_ptr_pointee(&ptr_ptr->value);
12181 ConstExprValue *slice_ptr = const_ptr_pointee(ira->codegen, &ptr_ptr->value);
1216912182 parent_ptr = &slice_ptr->data.x_struct.fields[slice_ptr_index];
1217012183 ConstExprValue *len_val = &slice_ptr->data.x_struct.fields[slice_len_index];
1217112184
......@@ -12371,7 +12384,7 @@ static TypeTableEntry *ir_analyze_instruction_overflow_op(IrAnalyze *ira, IrInst
1237112384 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
1237212385 BigNum *op1_bignum = &casted_op1->value.data.x_bignum;
1237312386 BigNum *op2_bignum = &casted_op2->value.data.x_bignum;
12374 ConstExprValue *pointee_val = const_ptr_pointee(&casted_result_ptr->value);
12387 ConstExprValue *pointee_val = const_ptr_pointee(ira->codegen, &casted_result_ptr->value);
1237512388 BigNum *dest_bignum = &pointee_val->data.x_bignum;
1237612389 switch (instruction->op) {
1237712390 case IrOverflowOpAdd:
......@@ -12455,7 +12468,7 @@ static TypeTableEntry *ir_analyze_instruction_unwrap_err_code(IrAnalyze *ira,
1245512468 ConstExprValue *ptr_val = ir_resolve_const(ira, value, UndefBad);
1245612469 if (!ptr_val)
1245712470 return ira->codegen->builtin_types.entry_invalid;
12458 ConstExprValue *err_union_val = const_ptr_pointee(ptr_val);
12471 ConstExprValue *err_union_val = const_ptr_pointee(ira->codegen, ptr_val);
1245912472 if (err_union_val->special != ConstValSpecialRuntime) {
1246012473 ErrorTableEntry *err = err_union_val->data.x_err_union.err;
1246112474 assert(err);
......@@ -12498,7 +12511,7 @@ static TypeTableEntry *ir_analyze_instruction_unwrap_err_payload(IrAnalyze *ira,
1249812511 ConstExprValue *ptr_val = ir_resolve_const(ira, value, UndefBad);
1249912512 if (!ptr_val)
1250012513 return ira->codegen->builtin_types.entry_invalid;
12501 ConstExprValue *err_union_val = const_ptr_pointee(ptr_val);
12514 ConstExprValue *err_union_val = const_ptr_pointee(ira->codegen, ptr_val);
1250212515 if (err_union_val->special != ConstValSpecialRuntime) {
1250312516 ErrorTableEntry *err = err_union_val->data.x_err_union.err;
1250412517 if (err != nullptr) {
......@@ -13248,7 +13261,7 @@ FnTableEntry *ir_create_inline_fn(CodeGen *codegen, Buf *fn_name, VariableTableE
1324813261
1324913262 if (codegen->verbose) {
1325013263 fprintf(stderr, "{\n");
13251 ir_print(stderr, &fn_entry->ir_executable, 4);
13264 ir_print(codegen, stderr, &fn_entry->ir_executable, 4);
1325213265 fprintf(stderr, "}\n");
1325313266 }
1325413267
src/ir.hpp+1-1
......@@ -22,7 +22,7 @@ TypeTableEntry *ir_analyze(CodeGen *g, IrExecutable *old_executable, IrExecutabl
2222 TypeTableEntry *expected_type, AstNode *expected_type_source_node);
2323
2424bool ir_has_side_effects(IrInstruction *instruction);
25ConstExprValue *const_ptr_pointee(ConstExprValue *const_val);
25ConstExprValue *const_ptr_pointee(CodeGen *codegen, ConstExprValue *const_val);
2626
2727FnTableEntry *ir_create_inline_fn(CodeGen *codegen, Buf *fn_name, VariableTableEntry *var, Scope *parent_scope);
2828
src/ir_print.cpp+4-2
......@@ -10,6 +10,7 @@
1010#include "ir_print.hpp"
1111
1212struct IrPrint {
13 CodeGen *codegen;
1314 FILE *f;
1415 int indent;
1516 int indent_size;
......@@ -34,7 +35,7 @@ static void ir_print_prefix(IrPrint *irp, IrInstruction *instruction) {
3435static void ir_print_const_value(IrPrint *irp, ConstExprValue *const_val) {
3536 Buf buf = BUF_INIT;
3637 buf_resize(&buf, 0);
37 render_const_value(&buf, const_val);
38 render_const_value(irp->codegen, &buf, const_val);
3839 fprintf(irp->f, "%s", buf_ptr(&buf));
3940}
4041
......@@ -1188,9 +1189,10 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
11881189 fprintf(irp->f, "\n");
11891190}
11901191
1191void ir_print(FILE *f, IrExecutable *executable, int indent_size) {
1192void ir_print(CodeGen *codegen, FILE *f, IrExecutable *executable, int indent_size) {
11921193 IrPrint ir_print = {};
11931194 IrPrint *irp = &ir_print;
1195 irp->codegen = codegen;
11941196 irp->f = f;
11951197 irp->indent = indent_size;
11961198 irp->indent_size = indent_size;
src/ir_print.hpp+1-1
......@@ -12,6 +12,6 @@
1212
1313#include <stdio.h>
1414
15void ir_print(FILE *f, IrExecutable *executable, int indent_size);
15void ir_print(CodeGen *codegen, FILE *f, IrExecutable *executable, int indent_size);
1616
1717#endif
src/main.cpp+1-1
......@@ -609,7 +609,7 @@ int main(int argc, char **argv) {
609609 return EXIT_SUCCESS;
610610 } else if (cmd == CmdParseH) {
611611 codegen_parseh(g, &root_source_dir, &root_source_name, &root_source_code);
612 ast_render_decls(stdout, 4, g->root_import);
612 ast_render_decls(g, stdout, 4, g->root_import);
613613 return EXIT_SUCCESS;
614614 } else if (cmd == CmdTest) {
615615 codegen_add_root_code(g, &root_source_dir, &root_source_name, &root_source_code);