authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-25 04:15:23-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-25 04:15:23-05:00
logf47dea2a2ee5e2351736fa416d82c1ed38bfe0f1
treedb58b40ab7bc848c6fdbf19007c8d303fb0d7185
parent6f91fb4c37a676002ccb68b78bea8044ba44b7e2

IR: support compile time global pointer reinterpret

this required moving the place we store types to ConstExprValue

9 files changed, 1090 insertions(+), 938 deletions(-)

src/all_types.hpp+12-12
...@@ -131,6 +131,7 @@ enum RuntimeHintMaybe {...@@ -131,6 +131,7 @@ enum RuntimeHintMaybe {
131};131};
132132
133struct ConstExprValue {133struct ConstExprValue {
134 TypeTableEntry *type;
134 ConstValSpecial special;135 ConstValSpecial special;
135 bool depends_on_compile_var;136 bool depends_on_compile_var;
136 LLVMValueRef llvm_value;137 LLVMValueRef llvm_value;
...@@ -429,8 +430,6 @@ enum CastOp {...@@ -429,8 +430,6 @@ enum CastOp {
429 CastOpPtrToInt,430 CastOpPtrToInt,
430 CastOpIntToPtr,431 CastOpIntToPtr,
431 CastOpWidenOrShorten,432 CastOpWidenOrShorten,
432 CastOpToUnknownSizeArray,
433 CastOpPointerReinterpret,
434 CastOpErrToInt,433 CastOpErrToInt,
435 CastOpIntToFloat,434 CastOpIntToFloat,
436 CastOpFloatToInt,435 CastOpFloatToInt,
...@@ -749,14 +748,9 @@ struct FnTypeParamInfo {...@@ -749,14 +748,9 @@ struct FnTypeParamInfo {
749 TypeTableEntry *type;748 TypeTableEntry *type;
750};749};
751750
752struct GenericParamValue {
753 TypeTableEntry *type;
754 ConstExprValue *value;
755};
756
757struct GenericFnTypeId {751struct GenericFnTypeId {
758 FnTableEntry *fn_entry;752 FnTableEntry *fn_entry;
759 GenericParamValue *params;753 ConstExprValue *params;
760 size_t param_count;754 size_t param_count;
761};755};
762756
...@@ -1238,7 +1232,7 @@ struct CodeGen {...@@ -1238,7 +1232,7 @@ struct CodeGen {
1238// TODO after merging IR branch, we can probably delete some of these fields1232// TODO after merging IR branch, we can probably delete some of these fields
1239struct VariableTableEntry {1233struct VariableTableEntry {
1240 Buf name;1234 Buf name;
1241 TypeTableEntry *type;1235 ConstExprValue value;
1242 LLVMValueRef value_ref;1236 LLVMValueRef value_ref;
1243 bool src_is_const;1237 bool src_is_const;
1244 bool gen_is_const;1238 bool gen_is_const;
...@@ -1254,7 +1248,6 @@ struct VariableTableEntry {...@@ -1254,7 +1248,6 @@ struct VariableTableEntry {
1254 bool shadowable;1248 bool shadowable;
1255 size_t mem_slot_index;1249 size_t mem_slot_index;
1256 size_t ref_count;1250 size_t ref_count;
1257 ConstExprValue *value;
1258 bool is_extern;1251 bool is_extern;
1259};1252};
12601253
...@@ -1460,14 +1453,14 @@ enum IrInstructionId {...@@ -1460,14 +1453,14 @@ enum IrInstructionId {
1460 IrInstructionIdFnProto,1453 IrInstructionIdFnProto,
1461 IrInstructionIdTestComptime,1454 IrInstructionIdTestComptime,
1462 IrInstructionIdInitEnum,1455 IrInstructionIdInitEnum,
1456 IrInstructionIdPointerReinterpret,
1463};1457};
14641458
1465struct IrInstruction {1459struct IrInstruction {
1466 IrInstructionId id;1460 IrInstructionId id;
1467 Scope *scope;1461 Scope *scope;
1468 AstNode *source_node;1462 AstNode *source_node;
1469 ConstExprValue static_value;1463 ConstExprValue value;
1470 TypeTableEntry *type_entry;
1471 size_t debug_id;1464 size_t debug_id;
1472 LLVMValueRef llvm_value;1465 LLVMValueRef llvm_value;
1473 // if ref_count is zero, instruction can be omitted in codegen1466 // if ref_count is zero, instruction can be omitted in codegen
...@@ -1986,6 +1979,7 @@ struct IrInstructionSlice {...@@ -1986,6 +1979,7 @@ struct IrInstructionSlice {
1986 IrInstruction *start;1979 IrInstruction *start;
1987 IrInstruction *end;1980 IrInstruction *end;
1988 bool is_const;1981 bool is_const;
1982 bool safety_check_on;
1989 LLVMValueRef tmp_ptr;1983 LLVMValueRef tmp_ptr;
1990};1984};
19911985
...@@ -2096,6 +2090,12 @@ struct IrInstructionInitEnum {...@@ -2096,6 +2090,12 @@ struct IrInstructionInitEnum {
2096 LLVMValueRef tmp_ptr;2090 LLVMValueRef tmp_ptr;
2097};2091};
20982092
2093struct IrInstructionPointerReinterpret {
2094 IrInstruction base;
2095
2096 IrInstruction *ptr;
2097};
2098
2099enum LValPurpose {2099enum LValPurpose {
2100 LValPurposeNone,2100 LValPurposeNone,
2101 LValPurposeAssign,2101 LValPurposeAssign,
src/analyze.cpp+176-62
...@@ -876,11 +876,11 @@ static IrInstruction *analyze_const_value(CodeGen *g, Scope *scope, AstNode *nod...@@ -876,11 +876,11 @@ static IrInstruction *analyze_const_value(CodeGen *g, Scope *scope, AstNode *nod
876876
877TypeTableEntry *analyze_type_expr(CodeGen *g, Scope *scope, AstNode *node) {877TypeTableEntry *analyze_type_expr(CodeGen *g, Scope *scope, AstNode *node) {
878 IrInstruction *result = analyze_const_value(g, scope, node, g->builtin_types.entry_type, nullptr);878 IrInstruction *result = analyze_const_value(g, scope, node, g->builtin_types.entry_type, nullptr);
879 if (result->type_entry->id == TypeTableEntryIdInvalid)879 if (result->value.type->id == TypeTableEntryIdInvalid)
880 return g->builtin_types.entry_invalid;880 return g->builtin_types.entry_invalid;
881881
882 assert(result->static_value.special != ConstValSpecialRuntime);882 assert(result->value.special != ConstValSpecialRuntime);
883 return result->static_value.data.x_type;883 return result->value.data.x_type;
884}884}
885885
886static TypeTableEntry *get_generic_fn_type(CodeGen *g, FnTypeId *fn_type_id) {886static TypeTableEntry *get_generic_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
...@@ -1708,41 +1708,42 @@ TypeTableEntry *validate_var_type(CodeGen *g, AstNode *source_node, TypeTableEnt...@@ -1708,41 +1708,42 @@ TypeTableEntry *validate_var_type(CodeGen *g, AstNode *source_node, TypeTableEnt
1708// Set name to nullptr to make the variable anonymous (not visible to programmer).1708// Set name to nullptr to make the variable anonymous (not visible to programmer).
1709// TODO merge with definition of add_local_var in ir.cpp1709// TODO merge with definition of add_local_var in ir.cpp
1710VariableTableEntry *add_variable(CodeGen *g, AstNode *source_node, Scope *parent_scope, Buf *name,1710VariableTableEntry *add_variable(CodeGen *g, AstNode *source_node, Scope *parent_scope, Buf *name,
1711 TypeTableEntry *type_entry, bool is_const, ConstExprValue *init_value)1711 bool is_const, ConstExprValue *value)
1712{1712{
1713 assert(value);
1714
1713 VariableTableEntry *variable_entry = allocate<VariableTableEntry>(1);1715 VariableTableEntry *variable_entry = allocate<VariableTableEntry>(1);
1714 variable_entry->type = type_entry;1716 variable_entry->value = *value;
1715 variable_entry->parent_scope = parent_scope;1717 variable_entry->parent_scope = parent_scope;
1716 variable_entry->shadowable = false;1718 variable_entry->shadowable = false;
1717 variable_entry->mem_slot_index = SIZE_MAX;1719 variable_entry->mem_slot_index = SIZE_MAX;
1718 variable_entry->value = init_value;
1719 variable_entry->src_arg_index = SIZE_MAX;1720 variable_entry->src_arg_index = SIZE_MAX;
17201721
1721 assert(name);1722 assert(name);
17221723
1723 buf_init_from_buf(&variable_entry->name, name);1724 buf_init_from_buf(&variable_entry->name, name);
17241725
1725 if (type_entry->id != TypeTableEntryIdInvalid) {1726 if (value->type->id != TypeTableEntryIdInvalid) {
1726 VariableTableEntry *existing_var = find_variable(g, parent_scope, name);1727 VariableTableEntry *existing_var = find_variable(g, parent_scope, name);
1727 if (existing_var && !existing_var->shadowable) {1728 if (existing_var && !existing_var->shadowable) {
1728 ErrorMsg *msg = add_node_error(g, source_node,1729 ErrorMsg *msg = add_node_error(g, source_node,
1729 buf_sprintf("redeclaration of variable '%s'", buf_ptr(name)));1730 buf_sprintf("redeclaration of variable '%s'", buf_ptr(name)));
1730 add_error_note(g, msg, existing_var->decl_node, buf_sprintf("previous declaration is here"));1731 add_error_note(g, msg, existing_var->decl_node, buf_sprintf("previous declaration is here"));
1731 variable_entry->type = g->builtin_types.entry_invalid;1732 variable_entry->value.type = g->builtin_types.entry_invalid;
1732 } else {1733 } else {
1733 auto primitive_table_entry = g->primitive_type_table.maybe_get(name);1734 auto primitive_table_entry = g->primitive_type_table.maybe_get(name);
1734 if (primitive_table_entry) {1735 if (primitive_table_entry) {
1735 TypeTableEntry *type = primitive_table_entry->value;1736 TypeTableEntry *type = primitive_table_entry->value;
1736 add_node_error(g, source_node,1737 add_node_error(g, source_node,
1737 buf_sprintf("variable shadows type '%s'", buf_ptr(&type->name)));1738 buf_sprintf("variable shadows type '%s'", buf_ptr(&type->name)));
1738 variable_entry->type = g->builtin_types.entry_invalid;1739 variable_entry->value.type = g->builtin_types.entry_invalid;
1739 } else {1740 } else {
1740 Tld *tld = find_decl(parent_scope, name);1741 Tld *tld = find_decl(parent_scope, name);
1741 if (tld && tld->id != TldIdVar) {1742 if (tld && tld->id != TldIdVar) {
1742 ErrorMsg *msg = add_node_error(g, source_node,1743 ErrorMsg *msg = add_node_error(g, source_node,
1743 buf_sprintf("redefinition of '%s'", buf_ptr(name)));1744 buf_sprintf("redefinition of '%s'", buf_ptr(name)));
1744 add_error_note(g, msg, tld->source_node, buf_sprintf("previous definition is here"));1745 add_error_note(g, msg, tld->source_node, buf_sprintf("previous definition is here"));
1745 variable_entry->type = g->builtin_types.entry_invalid;1746 variable_entry->value.type = g->builtin_types.entry_invalid;
1746 }1747 }
1747 }1748 }
1748 }1749 }
...@@ -1789,7 +1790,7 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {...@@ -1789,7 +1790,7 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {
1789 } else if (var_decl->expr) {1790 } else if (var_decl->expr) {
1790 init_value = analyze_const_value(g, tld_var->base.parent_scope, var_decl->expr, explicit_type, var_decl->symbol);1791 init_value = analyze_const_value(g, tld_var->base.parent_scope, var_decl->expr, explicit_type, var_decl->symbol);
1791 assert(init_value);1792 assert(init_value);
1792 implicit_type = init_value->type_entry;1793 implicit_type = init_value->value.type;
17931794
1794 if (implicit_type->id == TypeTableEntryIdUnreachable) {1795 if (implicit_type->id == TypeTableEntryIdUnreachable) {
1795 add_node_error(g, source_node, buf_sprintf("variable initialization is unreachable"));1796 add_node_error(g, source_node, buf_sprintf("variable initialization is unreachable"));
...@@ -1807,7 +1808,7 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {...@@ -1807,7 +1808,7 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {
1807 add_node_error(g, source_node, buf_sprintf("variable of type 'type' must be constant"));1808 add_node_error(g, source_node, buf_sprintf("variable of type 'type' must be constant"));
1808 implicit_type = g->builtin_types.entry_invalid;1809 implicit_type = g->builtin_types.entry_invalid;
1809 }1810 }
1810 assert(implicit_type->id == TypeTableEntryIdInvalid || init_value->static_value.special != ConstValSpecialRuntime);1811 assert(implicit_type->id == TypeTableEntryIdInvalid || init_value->value.special != ConstValSpecialRuntime);
1811 } else if (!is_extern) {1812 } else if (!is_extern) {
1812 add_node_error(g, source_node, buf_sprintf("variables must be initialized"));1813 add_node_error(g, source_node, buf_sprintf("variables must be initialized"));
1813 implicit_type = g->builtin_types.entry_invalid;1814 implicit_type = g->builtin_types.entry_invalid;
...@@ -1816,8 +1817,9 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {...@@ -1816,8 +1817,9 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {
1816 TypeTableEntry *type = explicit_type ? explicit_type : implicit_type;1817 TypeTableEntry *type = explicit_type ? explicit_type : implicit_type;
1817 assert(type != nullptr); // should have been caught by the parser1818 assert(type != nullptr); // should have been caught by the parser
18181819
1819 tld_var->var = add_variable(g, source_node, tld_var->base.parent_scope,1820 ConstExprValue *init_val = init_value ? &init_value->value : create_const_runtime(type);
1820 var_decl->symbol, type, is_const, &init_value->static_value);1821
1822 tld_var->var = add_variable(g, source_node, tld_var->base.parent_scope, var_decl->symbol, is_const, init_val);
1821 tld_var->var->is_extern = is_extern;1823 tld_var->var->is_extern = is_extern;
18221824
1823 g->global_vars.append(tld_var->var);1825 g->global_vars.append(tld_var->var);
...@@ -2231,7 +2233,8 @@ static void analyze_fn_body(CodeGen *g, FnTableEntry *fn_table_entry) {...@@ -2231,7 +2233,8 @@ static void analyze_fn_body(CodeGen *g, FnTableEntry *fn_table_entry) {
2231 buf_sprintf("byvalue types not yet supported on extern function parameters"));2233 buf_sprintf("byvalue types not yet supported on extern function parameters"));
2232 }2234 }
22332235
2234 VariableTableEntry *var = add_variable(g, param_decl_node, fn_table_entry->child_scope, param_decl->name, param_type, true, nullptr);2236 VariableTableEntry *var = add_variable(g, param_decl_node, fn_table_entry->child_scope,
2237 param_decl->name, true, create_const_runtime(param_type));
2235 var->src_arg_index = i;2238 var->src_arg_index = i;
2236 fn_table_entry->child_scope = var->child_scope;2239 fn_table_entry->child_scope = var->child_scope;
22372240
...@@ -2287,14 +2290,14 @@ static void analyze_fn_body(CodeGen *g, FnTableEntry *fn_table_entry) {...@@ -2287,14 +2290,14 @@ static void analyze_fn_body(CodeGen *g, FnTableEntry *fn_table_entry) {
22872290
2288static void add_symbols_from_import(CodeGen *g, AstNode *dst_use_node) {2291static void add_symbols_from_import(CodeGen *g, AstNode *dst_use_node) {
2289 IrInstruction *use_target_value = dst_use_node->data.use.value;2292 IrInstruction *use_target_value = dst_use_node->data.use.value;
2290 if (use_target_value->type_entry->id == TypeTableEntryIdInvalid) {2293 if (use_target_value->value.type->id == TypeTableEntryIdInvalid) {
2291 dst_use_node->owner->any_imports_failed = true;2294 dst_use_node->owner->any_imports_failed = true;
2292 return;2295 return;
2293 }2296 }
22942297
2295 dst_use_node->data.use.resolution = TldResolutionOk;2298 dst_use_node->data.use.resolution = TldResolutionOk;
22962299
2297 ConstExprValue *const_val = &use_target_value->static_value;2300 ConstExprValue *const_val = &use_target_value->value;
2298 assert(const_val->special != ConstValSpecialRuntime);2301 assert(const_val->special != ConstValSpecialRuntime);
22992302
2300 ImportTableEntry *target_import = const_val->data.x_import;2303 ImportTableEntry *target_import = const_val->data.x_import;
...@@ -2351,7 +2354,7 @@ void preview_use_decl(CodeGen *g, AstNode *node) {...@@ -2351,7 +2354,7 @@ void preview_use_decl(CodeGen *g, AstNode *node) {
2351 IrInstruction *result = analyze_const_value(g, &node->owner->decls_scope->base,2354 IrInstruction *result = analyze_const_value(g, &node->owner->decls_scope->base,
2352 node->data.use.expr, g->builtin_types.entry_namespace, nullptr);2355 node->data.use.expr, g->builtin_types.entry_namespace, nullptr);
23532356
2354 if (result->type_entry->id == TypeTableEntryIdInvalid)2357 if (result->value.type->id == TypeTableEntryIdInvalid)
2355 node->owner->any_imports_failed = true;2358 node->owner->any_imports_failed = true;
23562359
2357 node->data.use.value = result;2360 node->data.use.value = result;
...@@ -2620,8 +2623,9 @@ bool fn_type_id_eql(FnTypeId *a, FnTypeId *b) {...@@ -2620,8 +2623,9 @@ bool fn_type_id_eql(FnTypeId *a, FnTypeId *b) {
2620 return true;2623 return true;
2621}2624}
26222625
2623static uint32_t hash_const_val(TypeTableEntry *type, ConstExprValue *const_val) {2626static uint32_t hash_const_val(ConstExprValue *const_val) {
2624 switch (type->id) {2627 assert(const_val->special == ConstValSpecialStatic);
2628 switch (const_val->type->id) {
2625 case TypeTableEntryIdBool:2629 case TypeTableEntryIdBool:
2626 return const_val->data.x_bool ? 127863866 : 215080464;2630 return const_val->data.x_bool ? 127863866 : 215080464;
2627 case TypeTableEntryIdMetaType:2631 case TypeTableEntryIdMetaType:
...@@ -2630,6 +2634,7 @@ static uint32_t hash_const_val(TypeTableEntry *type, ConstExprValue *const_val)...@@ -2630,6 +2634,7 @@ static uint32_t hash_const_val(TypeTableEntry *type, ConstExprValue *const_val)
2630 return 4149439618;2634 return 4149439618;
2631 case TypeTableEntryIdInt:2635 case TypeTableEntryIdInt:
2632 case TypeTableEntryIdNumLitInt:2636 case TypeTableEntryIdNumLitInt:
2637 case TypeTableEntryIdEnumTag:
2633 return ((uint32_t)(bignum_to_twos_complement(&const_val->data.x_bignum) % UINT32_MAX)) * 1331471175;2638 return ((uint32_t)(bignum_to_twos_complement(&const_val->data.x_bignum) % UINT32_MAX)) * 1331471175;
2634 case TypeTableEntryIdFloat:2639 case TypeTableEntryIdFloat:
2635 case TypeTableEntryIdNumLitFloat:2640 case TypeTableEntryIdNumLitFloat:
...@@ -2651,8 +2656,7 @@ static uint32_t hash_const_val(TypeTableEntry *type, ConstExprValue *const_val)...@@ -2651,8 +2656,7 @@ static uint32_t hash_const_val(TypeTableEntry *type, ConstExprValue *const_val)
2651 return 2709806591;2656 return 2709806591;
2652 case TypeTableEntryIdMaybe:2657 case TypeTableEntryIdMaybe:
2653 if (const_val->data.x_maybe) {2658 if (const_val->data.x_maybe) {
2654 TypeTableEntry *child_type = type->data.maybe.child_type;2659 return hash_const_val(const_val->data.x_maybe) * 1992916303;
2655 return hash_const_val(child_type, const_val->data.x_maybe) * 1992916303;
2656 } else {2660 } else {
2657 return 4016830364;2661 return 4016830364;
2658 }2662 }
...@@ -2673,8 +2677,6 @@ static uint32_t hash_const_val(TypeTableEntry *type, ConstExprValue *const_val)...@@ -2673,8 +2677,6 @@ static uint32_t hash_const_val(TypeTableEntry *type, ConstExprValue *const_val)
2673 return hash_ptr(const_val->data.x_import);2677 return hash_ptr(const_val->data.x_import);
2674 case TypeTableEntryIdBlock:2678 case TypeTableEntryIdBlock:
2675 return hash_ptr(const_val->data.x_block);2679 return hash_ptr(const_val->data.x_block);
2676 case TypeTableEntryIdEnumTag:
2677 return 983996406 + hash_const_val(type->data.enum_tag.int_type, const_val);
2678 case TypeTableEntryIdBoundFn:2680 case TypeTableEntryIdBoundFn:
2679 case TypeTableEntryIdInvalid:2681 case TypeTableEntryIdInvalid:
2680 case TypeTableEntryIdUnreachable:2682 case TypeTableEntryIdUnreachable:
...@@ -2688,9 +2690,9 @@ uint32_t generic_fn_type_id_hash(GenericFnTypeId *id) {...@@ -2688,9 +2690,9 @@ uint32_t generic_fn_type_id_hash(GenericFnTypeId *id) {
2688 uint32_t result = 0;2690 uint32_t result = 0;
2689 result += hash_ptr(id->fn_entry);2691 result += hash_ptr(id->fn_entry);
2690 for (size_t i = 0; i < id->param_count; i += 1) {2692 for (size_t i = 0; i < id->param_count; i += 1) {
2691 GenericParamValue *generic_param = &id->params[i];2693 ConstExprValue *generic_param = &id->params[i];
2692 if (generic_param->value) {2694 if (generic_param->special != ConstValSpecialRuntime) {
2693 result += hash_const_val(generic_param->type, generic_param->value);2695 result += hash_const_val(generic_param);
2694 result += hash_ptr(generic_param->type);2696 result += hash_ptr(generic_param->type);
2695 }2697 }
2696 }2698 }
...@@ -2702,17 +2704,17 @@ bool generic_fn_type_id_eql(GenericFnTypeId *a, GenericFnTypeId *b) {...@@ -2702,17 +2704,17 @@ bool generic_fn_type_id_eql(GenericFnTypeId *a, GenericFnTypeId *b) {
2702 if (a->fn_entry != b->fn_entry) return false;2704 if (a->fn_entry != b->fn_entry) return false;
2703 assert(a->param_count == b->param_count);2705 assert(a->param_count == b->param_count);
2704 for (size_t i = 0; i < a->param_count; i += 1) {2706 for (size_t i = 0; i < a->param_count; i += 1) {
2705 GenericParamValue *a_val = &a->params[i];2707 ConstExprValue *a_val = &a->params[i];
2706 GenericParamValue *b_val = &b->params[i];2708 ConstExprValue *b_val = &b->params[i];
2707 if (a_val->type != b_val->type) return false;2709 if (a_val->type != b_val->type) return false;
2708 if (a_val->value && b_val->value) {2710 if (a_val->special != ConstValSpecialRuntime && b_val->special != ConstValSpecialRuntime) {
2709 assert(a_val->value->special == ConstValSpecialStatic);2711 assert(a_val->special == ConstValSpecialStatic);
2710 assert(b_val->value->special == ConstValSpecialStatic);2712 assert(b_val->special == ConstValSpecialStatic);
2711 if (!const_values_equal(a_val->value, b_val->value, a_val->type)) {2713 if (!const_values_equal(a_val, b_val)) {
2712 return false;2714 return false;
2713 }2715 }
2714 } else {2716 } else {
2715 assert(!a_val->value && !b_val->value);2717 assert(a_val->special == ConstValSpecialRuntime && b_val->special == ConstValSpecialRuntime);
2716 }2718 }
2717 }2719 }
2718 return true;2720 return true;
...@@ -2723,7 +2725,7 @@ uint32_t fn_eval_hash(Scope* scope) {...@@ -2723,7 +2725,7 @@ uint32_t fn_eval_hash(Scope* scope) {
2723 while (scope) {2725 while (scope) {
2724 if (scope->id == ScopeIdVarDecl) {2726 if (scope->id == ScopeIdVarDecl) {
2725 ScopeVarDecl *var_scope = (ScopeVarDecl *)scope;2727 ScopeVarDecl *var_scope = (ScopeVarDecl *)scope;
2726 result += hash_const_val(var_scope->var->type, var_scope->var->value);2728 result += hash_const_val(&var_scope->var->value);
2727 } else if (scope->id == ScopeIdFnDef) {2729 } else if (scope->id == ScopeIdFnDef) {
2728 ScopeFnDef *fn_scope = (ScopeFnDef *)scope;2730 ScopeFnDef *fn_scope = (ScopeFnDef *)scope;
2729 result += hash_ptr(fn_scope->fn_entry);2731 result += hash_ptr(fn_scope->fn_entry);
...@@ -2745,9 +2747,9 @@ bool fn_eval_eql(Scope *a, Scope *b) {...@@ -2745,9 +2747,9 @@ bool fn_eval_eql(Scope *a, Scope *b) {
2745 if (a->id == ScopeIdVarDecl) {2747 if (a->id == ScopeIdVarDecl) {
2746 ScopeVarDecl *a_var_scope = (ScopeVarDecl *)a;2748 ScopeVarDecl *a_var_scope = (ScopeVarDecl *)a;
2747 ScopeVarDecl *b_var_scope = (ScopeVarDecl *)b;2749 ScopeVarDecl *b_var_scope = (ScopeVarDecl *)b;
2748 if (a_var_scope->var->type != b_var_scope->var->type)2750 if (a_var_scope->var->value.type != b_var_scope->var->value.type)
2749 return false;2751 return false;
2750 if (!const_values_equal(a_var_scope->var->value, b_var_scope->var->value, a_var_scope->var->type))2752 if (!const_values_equal(&a_var_scope->var->value, &b_var_scope->var->value))
2751 return false;2753 return false;
2752 } else if (a->id == ScopeIdFnDef) {2754 } else if (a->id == ScopeIdFnDef) {
2753 ScopeFnDef *a_fn_scope = (ScopeFnDef *)a;2755 ScopeFnDef *a_fn_scope = (ScopeFnDef *)a;
...@@ -2865,77 +2867,187 @@ uint64_t get_memcpy_align(CodeGen *g, TypeTableEntry *type_entry) {...@@ -2865,77 +2867,187 @@ uint64_t get_memcpy_align(CodeGen *g, TypeTableEntry *type_entry) {
2865 return LLVMABISizeOfType(g->target_data_ref, first_type_in_mem->type_ref);2867 return LLVMABISizeOfType(g->target_data_ref, first_type_in_mem->type_ref);
2866}2868}
28672869
2868void init_const_str_lit(ConstExprValue *const_val, Buf *str) {2870void init_const_str_lit(CodeGen *g, ConstExprValue *const_val, Buf *str) {
2869 const_val->special = ConstValSpecialStatic;2871 const_val->special = ConstValSpecialStatic;
2872 const_val->type = get_array_type(g, g->builtin_types.entry_u8, buf_len(str));
2870 const_val->data.x_array.elements = allocate<ConstExprValue>(buf_len(str));2873 const_val->data.x_array.elements = allocate<ConstExprValue>(buf_len(str));
2871 const_val->data.x_array.size = buf_len(str);2874 const_val->data.x_array.size = buf_len(str);
28722875
2873 for (size_t i = 0; i < buf_len(str); i += 1) {2876 for (size_t i = 0; i < buf_len(str); i += 1) {
2874 ConstExprValue *this_char = &const_val->data.x_array.elements[i];2877 ConstExprValue *this_char = &const_val->data.x_array.elements[i];
2875 this_char->special = ConstValSpecialStatic;2878 this_char->special = ConstValSpecialStatic;
2879 this_char->type = g->builtin_types.entry_u8;
2876 bignum_init_unsigned(&this_char->data.x_bignum, buf_ptr(str)[i]);2880 bignum_init_unsigned(&this_char->data.x_bignum, buf_ptr(str)[i]);
2877 }2881 }
2878}2882}
28792883
2880ConstExprValue *create_const_str_lit(Buf *str) {2884ConstExprValue *create_const_str_lit(CodeGen *g, Buf *str) {
2885 ConstExprValue *const_val = allocate<ConstExprValue>(1);
2886 init_const_str_lit(g, const_val, str);
2887 return const_val;
2888}
2889
2890void init_const_c_str_lit(CodeGen *g, ConstExprValue *const_val, Buf *str) {
2891 // first we build the underlying array
2892 size_t len_with_null = buf_len(str) + 1;
2893 ConstExprValue *array_val = allocate<ConstExprValue>(1);
2894 array_val->special = ConstValSpecialStatic;
2895 array_val->type = get_array_type(g, g->builtin_types.entry_u8, len_with_null);
2896 array_val->data.x_array.elements = allocate<ConstExprValue>(len_with_null);
2897 array_val->data.x_array.size = len_with_null;
2898 for (size_t i = 0; i < buf_len(str); i += 1) {
2899 ConstExprValue *this_char = &array_val->data.x_array.elements[i];
2900 this_char->special = ConstValSpecialStatic;
2901 this_char->type = g->builtin_types.entry_u8;
2902 bignum_init_unsigned(&this_char->data.x_bignum, buf_ptr(str)[i]);
2903 }
2904 ConstExprValue *null_char = &array_val->data.x_array.elements[len_with_null - 1];
2905 null_char->special = ConstValSpecialStatic;
2906 null_char->type = g->builtin_types.entry_u8;
2907 bignum_init_unsigned(&null_char->data.x_bignum, 0);
2908
2909 // then make the pointer point to it
2910 const_val->special = ConstValSpecialStatic;
2911 const_val->type = get_pointer_to_type(g, g->builtin_types.entry_u8, true);
2912 const_val->data.x_ptr.base_ptr = array_val;
2913 const_val->data.x_ptr.index = 0;
2914 const_val->data.x_ptr.special = ConstPtrSpecialCStr;
2915}
2916ConstExprValue *create_const_c_str_lit(CodeGen *g, Buf *str) {
2881 ConstExprValue *const_val = allocate<ConstExprValue>(1);2917 ConstExprValue *const_val = allocate<ConstExprValue>(1);
2882 init_const_str_lit(const_val, str);2918 init_const_c_str_lit(g, const_val, str);
2883 return const_val;2919 return const_val;
2884}2920}
28852921
2886void init_const_unsigned_negative(ConstExprValue *const_val, uint64_t x, bool negative) {2922void init_const_unsigned_negative(ConstExprValue *const_val, TypeTableEntry *type, uint64_t x, bool negative) {
2887 const_val->special = ConstValSpecialStatic;2923 const_val->special = ConstValSpecialStatic;
2924 const_val->type = type;
2888 bignum_init_unsigned(&const_val->data.x_bignum, x);2925 bignum_init_unsigned(&const_val->data.x_bignum, x);
2889 const_val->data.x_bignum.is_negative = negative;2926 const_val->data.x_bignum.is_negative = negative;
2890}2927}
28912928
2892ConstExprValue *create_const_unsigned_negative(uint64_t x, bool negative) {2929ConstExprValue *create_const_unsigned_negative(TypeTableEntry *type, uint64_t x, bool negative) {
2893 ConstExprValue *const_val = allocate<ConstExprValue>(1);2930 ConstExprValue *const_val = allocate<ConstExprValue>(1);
2894 init_const_unsigned_negative(const_val, x, negative);2931 init_const_unsigned_negative(const_val, type, x, negative);
2895 return const_val;2932 return const_val;
2896}2933}
28972934
2898void init_const_signed(ConstExprValue *const_val, int64_t x) {2935void init_const_usize(CodeGen *g, ConstExprValue *const_val, uint64_t x) {
2936 return init_const_unsigned_negative(const_val, g->builtin_types.entry_usize, x, false);
2937}
2938
2939ConstExprValue *create_const_usize(CodeGen *g, uint64_t x) {
2940 return create_const_unsigned_negative(g->builtin_types.entry_usize, x, false);
2941}
2942
2943void init_const_signed(ConstExprValue *const_val, TypeTableEntry *type, int64_t x) {
2899 const_val->special = ConstValSpecialStatic;2944 const_val->special = ConstValSpecialStatic;
2945 const_val->type = type;
2900 bignum_init_signed(&const_val->data.x_bignum, x);2946 bignum_init_signed(&const_val->data.x_bignum, x);
2901}2947}
29022948
2903ConstExprValue *create_const_signed(int64_t x) {2949ConstExprValue *create_const_signed(TypeTableEntry *type, int64_t x) {
2904 ConstExprValue *const_val = allocate<ConstExprValue>(1);2950 ConstExprValue *const_val = allocate<ConstExprValue>(1);
2905 init_const_signed(const_val, x);2951 init_const_signed(const_val, type, x);
2906 return const_val;2952 return const_val;
2907}2953}
29082954
2909void init_const_float(ConstExprValue *const_val, double value) {2955void init_const_float(ConstExprValue *const_val, TypeTableEntry *type, double value) {
2910 const_val->special = ConstValSpecialStatic;2956 const_val->special = ConstValSpecialStatic;
2957 const_val->type = type;
2911 bignum_init_float(&const_val->data.x_bignum, value);2958 bignum_init_float(&const_val->data.x_bignum, value);
2912}2959}
29132960
2914ConstExprValue *create_const_float(double value) {2961ConstExprValue *create_const_float(TypeTableEntry *type, double value) {
2915 ConstExprValue *const_val = allocate<ConstExprValue>(1);2962 ConstExprValue *const_val = allocate<ConstExprValue>(1);
2916 init_const_float(const_val, value);2963 init_const_float(const_val, type, value);
2917 return const_val;2964 return const_val;
2918}2965}
29192966
2920void init_const_enum_tag(ConstExprValue *const_val, uint64_t tag) {2967void init_const_enum_tag(ConstExprValue *const_val, TypeTableEntry *type, uint64_t tag) {
2921 const_val->special = ConstValSpecialStatic;2968 const_val->special = ConstValSpecialStatic;
2969 const_val->type = type;
2922 const_val->data.x_enum.tag = tag;2970 const_val->data.x_enum.tag = tag;
2923}2971}
29242972
2925ConstExprValue *create_const_enum_tag(uint64_t tag) {2973ConstExprValue *create_const_enum_tag(TypeTableEntry *type, uint64_t tag) {
2926 ConstExprValue *const_val = allocate<ConstExprValue>(1);2974 ConstExprValue *const_val = allocate<ConstExprValue>(1);
2927 init_const_enum_tag(const_val, tag);2975 init_const_enum_tag(const_val, type, tag);
2928 return const_val;2976 return const_val;
2929}2977}
29302978
2931void init_const_bool(ConstExprValue *const_val, bool value) {2979void init_const_bool(CodeGen *g, ConstExprValue *const_val, bool value) {
2932 const_val->special = ConstValSpecialStatic;2980 const_val->special = ConstValSpecialStatic;
2981 const_val->type = g->builtin_types.entry_bool;
2933 const_val->data.x_bool = value;2982 const_val->data.x_bool = value;
2934}2983}
29352984
2936ConstExprValue *create_const_bool(bool value) {2985ConstExprValue *create_const_bool(CodeGen *g, bool value) {
2986 ConstExprValue *const_val = allocate<ConstExprValue>(1);
2987 init_const_bool(g, const_val, value);
2988 return const_val;
2989}
2990
2991void init_const_runtime(ConstExprValue *const_val, TypeTableEntry *type) {
2992 const_val->special = ConstValSpecialRuntime;
2993 const_val->type = type;
2994}
2995
2996ConstExprValue *create_const_runtime(TypeTableEntry *type) {
2937 ConstExprValue *const_val = allocate<ConstExprValue>(1);2997 ConstExprValue *const_val = allocate<ConstExprValue>(1);
2938 init_const_bool(const_val, value);2998 init_const_runtime(const_val, type);
2999 return const_val;
3000}
3001
3002void init_const_type(CodeGen *g, ConstExprValue *const_val, TypeTableEntry *type_value) {
3003 const_val->special = ConstValSpecialStatic;
3004 const_val->type = g->builtin_types.entry_type;
3005 const_val->data.x_type = type_value;
3006}
3007
3008ConstExprValue *create_const_type(CodeGen *g, TypeTableEntry *type_value) {
3009 ConstExprValue *const_val = allocate<ConstExprValue>(1);
3010 init_const_type(g, const_val, type_value);
3011 return const_val;
3012}
3013
3014void init_const_slice(CodeGen *g, ConstExprValue *const_val, ConstExprValue *array_val,
3015 size_t start, size_t len, bool is_const)
3016{
3017 assert(array_val->type->id == TypeTableEntryIdArray);
3018
3019 const_val->special = ConstValSpecialStatic;
3020 const_val->type = get_slice_type(g, array_val->type->data.array.child_type, is_const);
3021 const_val->data.x_struct.fields = allocate<ConstExprValue>(2);
3022
3023 init_const_ptr(g, &const_val->data.x_struct.fields[slice_ptr_index], array_val, start, is_const);
3024 init_const_usize(g, &const_val->data.x_struct.fields[slice_len_index], len);
3025}
3026
3027ConstExprValue *create_const_slice(CodeGen *g, ConstExprValue *array_val, size_t start, size_t len, bool is_const) {
3028 ConstExprValue *const_val = allocate<ConstExprValue>(1);
3029 init_const_slice(g, const_val, array_val, start, len, is_const);
3030 return const_val;
3031}
3032
3033void init_const_ptr(CodeGen *g, ConstExprValue *const_val, ConstExprValue *base_ptr, size_t index, bool is_const) {
3034 TypeTableEntry *child_type;
3035 if (index == SIZE_MAX) {
3036 child_type = base_ptr->type;
3037 } else {
3038 assert(base_ptr->type->id == TypeTableEntryIdArray);
3039 child_type = base_ptr->type->data.array.child_type;
3040 }
3041
3042 const_val->special = ConstValSpecialStatic;
3043 const_val->type = get_pointer_to_type(g, child_type, is_const);
3044 const_val->data.x_ptr.base_ptr = base_ptr;
3045 const_val->data.x_ptr.index = index;
3046}
3047
3048ConstExprValue *create_const_ptr(CodeGen *g, ConstExprValue *base_ptr, size_t index, bool is_const) {
3049 ConstExprValue *const_val = allocate<ConstExprValue>(1);
3050 init_const_ptr(g, const_val, base_ptr, index, is_const);
2939 return const_val;3051 return const_val;
2940}3052}
29413053
...@@ -2956,18 +3068,21 @@ bool ir_get_var_is_comptime(VariableTableEntry *var) {...@@ -2956,18 +3068,21 @@ bool ir_get_var_is_comptime(VariableTableEntry *var) {
2956 if (!var->is_comptime)3068 if (!var->is_comptime)
2957 return false;3069 return false;
2958 if (var->is_comptime->other)3070 if (var->is_comptime->other)
2959 return var->is_comptime->other->static_value.data.x_bool;3071 return var->is_comptime->other->value.data.x_bool;
2960 return var->is_comptime->static_value.data.x_bool;3072 return var->is_comptime->value.data.x_bool;
2961}3073}
29623074
2963bool const_values_equal(ConstExprValue *a, ConstExprValue *b, TypeTableEntry *type_entry) {3075bool const_values_equal(ConstExprValue *a, ConstExprValue *b) {
2964 switch (type_entry->id) {3076 assert(a->type == b->type);
3077 assert(a->special == ConstValSpecialStatic);
3078 assert(b->special == ConstValSpecialStatic);
3079 switch (a->type->id) {
2965 case TypeTableEntryIdEnum:3080 case TypeTableEntryIdEnum:
2966 {3081 {
2967 ConstEnumValue *enum1 = &a->data.x_enum;3082 ConstEnumValue *enum1 = &a->data.x_enum;
2968 ConstEnumValue *enum2 = &b->data.x_enum;3083 ConstEnumValue *enum2 = &b->data.x_enum;
2969 if (enum1->tag == enum2->tag) {3084 if (enum1->tag == enum2->tag) {
2970 TypeEnumField *enum_field = &type_entry->data.enumeration.fields[enum1->tag];3085 TypeEnumField *enum_field = &a->type->data.enumeration.fields[enum1->tag];
2971 if (type_has_bits(enum_field->type_entry)) {3086 if (type_has_bits(enum_field->type_entry)) {
2972 zig_panic("TODO const expr analyze enum special value for equality");3087 zig_panic("TODO const expr analyze enum special value for equality");
2973 } else {3088 } else {
...@@ -2990,9 +3105,8 @@ bool const_values_equal(ConstExprValue *a, ConstExprValue *b, TypeTableEntry *ty...@@ -2990,9 +3105,8 @@ bool const_values_equal(ConstExprValue *a, ConstExprValue *b, TypeTableEntry *ty
2990 case TypeTableEntryIdFloat:3105 case TypeTableEntryIdFloat:
2991 case TypeTableEntryIdNumLitFloat:3106 case TypeTableEntryIdNumLitFloat:
2992 case TypeTableEntryIdNumLitInt:3107 case TypeTableEntryIdNumLitInt:
2993 return bignum_cmp_eq(&a->data.x_bignum, &b->data.x_bignum);
2994 case TypeTableEntryIdEnumTag:3108 case TypeTableEntryIdEnumTag:
2995 return const_values_equal(a, b, type_entry->data.enum_tag.int_type);3109 return bignum_cmp_eq(&a->data.x_bignum, &b->data.x_bignum);
2996 case TypeTableEntryIdPointer:3110 case TypeTableEntryIdPointer:
2997 zig_panic("TODO");3111 zig_panic("TODO");
2998 case TypeTableEntryIdArray:3112 case TypeTableEntryIdArray:
src/analyze.hpp+34-14
...@@ -66,7 +66,7 @@ FnTableEntry *scope_fn_entry(Scope *scope);...@@ -66,7 +66,7 @@ FnTableEntry *scope_fn_entry(Scope *scope);
66ImportTableEntry *get_scope_import(Scope *scope);66ImportTableEntry *get_scope_import(Scope *scope);
67void init_tld(Tld *tld, TldId id, Buf *name, VisibMod visib_mod, AstNode *source_node, Scope *parent_scope);67void init_tld(Tld *tld, TldId id, Buf *name, VisibMod visib_mod, AstNode *source_node, Scope *parent_scope);
68VariableTableEntry *add_variable(CodeGen *g, AstNode *source_node, Scope *parent_scope, Buf *name,68VariableTableEntry *add_variable(CodeGen *g, AstNode *source_node, Scope *parent_scope, Buf *name,
69 TypeTableEntry *type_entry, bool is_const, ConstExprValue *init_value);69 bool is_const, ConstExprValue *init_value);
70TypeTableEntry *analyze_type_expr(CodeGen *g, Scope *scope, AstNode *node);70TypeTableEntry *analyze_type_expr(CodeGen *g, Scope *scope, AstNode *node);
71FnTableEntry *create_fn(AstNode *proto_node);71FnTableEntry *create_fn(AstNode *proto_node);
72FnTableEntry *create_fn_raw(FnInline inline_value, bool internal_linkage);72FnTableEntry *create_fn_raw(FnInline inline_value, bool internal_linkage);
...@@ -77,7 +77,7 @@ bool type_requires_comptime(TypeTableEntry *type_entry);...@@ -77,7 +77,7 @@ bool type_requires_comptime(TypeTableEntry *type_entry);
77void ensure_complete_type(CodeGen *g, TypeTableEntry *type_entry);77void ensure_complete_type(CodeGen *g, TypeTableEntry *type_entry);
78void complete_enum(CodeGen *g, TypeTableEntry *enum_type);78void complete_enum(CodeGen *g, TypeTableEntry *enum_type);
79bool ir_get_var_is_comptime(VariableTableEntry *var);79bool ir_get_var_is_comptime(VariableTableEntry *var);
80bool const_values_equal(ConstExprValue *a, ConstExprValue *b, TypeTableEntry *type_entry);80bool const_values_equal(ConstExprValue *a, ConstExprValue *b);
81void eval_min_max_value(CodeGen *g, TypeTableEntry *type_entry, ConstExprValue *const_val, bool is_max);81void eval_min_max_value(CodeGen *g, TypeTableEntry *type_entry, ConstExprValue *const_val, bool is_max);
8282
83ScopeBlock *create_block_scope(AstNode *node, Scope *parent);83ScopeBlock *create_block_scope(AstNode *node, Scope *parent);
...@@ -88,22 +88,42 @@ Scope *create_loop_scope(AstNode *node, Scope *parent);...@@ -88,22 +88,42 @@ Scope *create_loop_scope(AstNode *node, Scope *parent);
88ScopeFnDef *create_fndef_scope(AstNode *node, Scope *parent, FnTableEntry *fn_entry);88ScopeFnDef *create_fndef_scope(AstNode *node, Scope *parent, FnTableEntry *fn_entry);
89ScopeDecls *create_decls_scope(AstNode *node, Scope *parent, TypeTableEntry *container_type, ImportTableEntry *import);89ScopeDecls *create_decls_scope(AstNode *node, Scope *parent, TypeTableEntry *container_type, ImportTableEntry *import);
9090
91void init_const_str_lit(ConstExprValue *const_val, Buf *str);91void init_const_str_lit(CodeGen *g, ConstExprValue *const_val, Buf *str);
92ConstExprValue *create_const_str_lit(Buf *str);92ConstExprValue *create_const_str_lit(CodeGen *g, Buf *str);
9393
94void init_const_unsigned_negative(ConstExprValue *const_val, uint64_t x, bool negative);94void init_const_c_str_lit(CodeGen *g, ConstExprValue *const_val, Buf *c_str);
95ConstExprValue *create_const_unsigned_negative(uint64_t x, bool negative);95ConstExprValue *create_const_c_str_lit(CodeGen *g, Buf *c_str);
9696
97void init_const_signed(ConstExprValue *const_val, int64_t x);97void init_const_unsigned_negative(ConstExprValue *const_val, TypeTableEntry *type, uint64_t x, bool negative);
98ConstExprValue *create_const_signed(int64_t x);98ConstExprValue *create_const_unsigned_negative(TypeTableEntry *type, uint64_t x, bool negative);
9999
100void init_const_float(ConstExprValue *const_val, double value);100void init_const_signed(ConstExprValue *const_val, TypeTableEntry *type, int64_t x);
101ConstExprValue *create_const_float(double value);101ConstExprValue *create_const_signed(TypeTableEntry *type, int64_t x);
102102
103void init_const_enum_tag(ConstExprValue *const_val, uint64_t tag);103void init_const_usize(CodeGen *g, ConstExprValue *const_val, uint64_t x);
104ConstExprValue *create_const_enum_tag(uint64_t tag);104ConstExprValue *create_const_usize(CodeGen *g, uint64_t x);
105105
106void init_const_bool(ConstExprValue *const_val, bool value);106void init_const_float(ConstExprValue *const_val, TypeTableEntry *type, double value);
107ConstExprValue *create_const_bool(bool value);107ConstExprValue *create_const_float(TypeTableEntry *type, double value);
108
109void init_const_enum_tag(ConstExprValue *const_val, TypeTableEntry *type, uint64_t tag);
110ConstExprValue *create_const_enum_tag(TypeTableEntry *type, uint64_t tag);
111
112void init_const_bool(CodeGen *g, ConstExprValue *const_val, bool value);
113ConstExprValue *create_const_bool(CodeGen *g, bool value);
114
115void init_const_type(CodeGen *g, ConstExprValue *const_val, TypeTableEntry *type_value);
116ConstExprValue *create_const_type(CodeGen *g, TypeTableEntry *type_value);
117
118void init_const_runtime(ConstExprValue *const_val, TypeTableEntry *type);
119ConstExprValue *create_const_runtime(TypeTableEntry *type);
120
121void init_const_ptr(CodeGen *g, ConstExprValue *const_val, ConstExprValue *base_ptr, size_t index, bool is_const);
122ConstExprValue *create_const_ptr(CodeGen *g, ConstExprValue *const_val, ConstExprValue *base_ptr,
123 size_t index, bool is_const);
124
125void init_const_slice(CodeGen *g, ConstExprValue *const_val, ConstExprValue *array_val,
126 size_t start, size_t len, bool is_const);
127ConstExprValue *create_const_slice(CodeGen *g, ConstExprValue *array_val, size_t start, size_t len, bool is_const);
108128
109#endif129#endif
src/codegen.cpp+179-193
...@@ -225,8 +225,8 @@ void codegen_set_rdynamic(CodeGen *g, bool rdynamic) {...@@ -225,8 +225,8 @@ void codegen_set_rdynamic(CodeGen *g, bool rdynamic) {
225 g->linker_rdynamic = rdynamic;225 g->linker_rdynamic = rdynamic;
226}226}
227227
228static void render_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstExprValue *const_val);228static void render_const_val(CodeGen *g, ConstExprValue *const_val);
229static void render_const_val_global(CodeGen *g, TypeTableEntry *type_entry, ConstExprValue *const_val);229static void render_const_val_global(CodeGen *g, ConstExprValue *const_val);
230230
231static LLVMValueRef fn_llvm_value(CodeGen *g, FnTableEntry *fn_table_entry) {231static LLVMValueRef fn_llvm_value(CodeGen *g, FnTableEntry *fn_table_entry) {
232 if (fn_table_entry->llvm_value)232 if (fn_table_entry->llvm_value)
...@@ -645,18 +645,14 @@ static LLVMValueRef gen_struct_memcpy(CodeGen *g, LLVMValueRef src, LLVMValueRef...@@ -645,18 +645,14 @@ static LLVMValueRef gen_struct_memcpy(CodeGen *g, LLVMValueRef src, LLVMValueRef
645 return LLVMBuildCall(g->builder, g->memcpy_fn_val, params, 5, "");645 return LLVMBuildCall(g->builder, g->memcpy_fn_val, params, 5, "");
646}646}
647647
648static LLVMValueRef gen_assign_raw(CodeGen *g,648static LLVMValueRef gen_assign_raw(CodeGen *g, LLVMValueRef target_ref, LLVMValueRef value,
649 LLVMValueRef target_ref, LLVMValueRef value,649 TypeTableEntry *child_type)
650 TypeTableEntry *op1_type, TypeTableEntry *op2_type)
651{650{
652 if (!type_has_bits(op1_type)) {651 if (!type_has_bits(child_type))
653 return nullptr;652 return nullptr;
654 }
655 if (handle_is_ptr(op1_type)) {
656 assert(op1_type == op2_type);
657653
658 return gen_struct_memcpy(g, value, target_ref, op1_type);654 if (handle_is_ptr(child_type))
659 }655 return gen_struct_memcpy(g, value, target_ref, child_type);
660656
661 LLVMBuildStore(g->builder, value, target_ref);657 LLVMBuildStore(g->builder, value, target_ref);
662 return nullptr;658 return nullptr;
...@@ -671,17 +667,17 @@ static void gen_var_debug_decl(CodeGen *g, VariableTableEntry *var) {...@@ -671,17 +667,17 @@ static void gen_var_debug_decl(CodeGen *g, VariableTableEntry *var) {
671}667}
672668
673static LLVMValueRef ir_llvm_value(CodeGen *g, IrInstruction *instruction) {669static LLVMValueRef ir_llvm_value(CodeGen *g, IrInstruction *instruction) {
674 if (!type_has_bits(instruction->type_entry))670 if (!type_has_bits(instruction->value.type))
675 return nullptr;671 return nullptr;
676 if (!instruction->llvm_value) {672 if (!instruction->llvm_value) {
677 assert(instruction->static_value.special != ConstValSpecialRuntime);673 assert(instruction->value.special != ConstValSpecialRuntime);
678 assert(instruction->type_entry);674 assert(instruction->value.type);
679 render_const_val(g, instruction->type_entry, &instruction->static_value);675 render_const_val(g, &instruction->value);
680 if (handle_is_ptr(instruction->type_entry)) {676 if (handle_is_ptr(instruction->value.type)) {
681 render_const_val_global(g, instruction->type_entry, &instruction->static_value);677 render_const_val_global(g, &instruction->value);
682 instruction->llvm_value = instruction->static_value.llvm_global;678 instruction->llvm_value = instruction->value.llvm_global;
683 } else {679 } else {
684 instruction->llvm_value = instruction->static_value.llvm_value;680 instruction->llvm_value = instruction->value.llvm_value;
685 }681 }
686 assert(instruction->llvm_value);682 assert(instruction->llvm_value);
687 }683 }
...@@ -690,7 +686,7 @@ static LLVMValueRef ir_llvm_value(CodeGen *g, IrInstruction *instruction) {...@@ -690,7 +686,7 @@ static LLVMValueRef ir_llvm_value(CodeGen *g, IrInstruction *instruction) {
690686
691static LLVMValueRef ir_render_return(CodeGen *g, IrExecutable *executable, IrInstructionReturn *return_instruction) {687static LLVMValueRef ir_render_return(CodeGen *g, IrExecutable *executable, IrInstructionReturn *return_instruction) {
692 LLVMValueRef value = ir_llvm_value(g, return_instruction->value);688 LLVMValueRef value = ir_llvm_value(g, return_instruction->value);
693 TypeTableEntry *return_type = g->cur_fn->type_entry->data.fn.fn_type_id.return_type;689 TypeTableEntry *return_type = return_instruction->value->value.type;
694 bool is_extern = g->cur_fn->type_entry->data.fn.fn_type_id.is_extern;690 bool is_extern = g->cur_fn->type_entry->data.fn.fn_type_id.is_extern;
695 if (handle_is_ptr(return_type)) {691 if (handle_is_ptr(return_type)) {
696 if (is_extern) {692 if (is_extern) {
...@@ -698,7 +694,7 @@ static LLVMValueRef ir_render_return(CodeGen *g, IrExecutable *executable, IrIns...@@ -698,7 +694,7 @@ static LLVMValueRef ir_render_return(CodeGen *g, IrExecutable *executable, IrIns
698 LLVMBuildRet(g->builder, by_val_value);694 LLVMBuildRet(g->builder, by_val_value);
699 } else {695 } else {
700 assert(g->cur_ret_ptr);696 assert(g->cur_ret_ptr);
701 gen_assign_raw(g, g->cur_ret_ptr, value, return_type, return_instruction->value->type_entry);697 gen_assign_raw(g, g->cur_ret_ptr, value, return_type);
702 LLVMBuildRetVoid(g->builder);698 LLVMBuildRetVoid(g->builder);
703 }699 }
704 } else {700 } else {
...@@ -809,7 +805,7 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,...@@ -809,7 +805,7 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,
809 IrInstruction *op1 = bin_op_instruction->op1;805 IrInstruction *op1 = bin_op_instruction->op1;
810 IrInstruction *op2 = bin_op_instruction->op2;806 IrInstruction *op2 = bin_op_instruction->op2;
811807
812 assert(op1->type_entry == op2->type_entry);808 assert(op1->value.type == op2->value.type);
813809
814 bool want_debug_safety = bin_op_instruction->safety_check_on &&810 bool want_debug_safety = bin_op_instruction->safety_check_on &&
815 ir_want_debug_safety(g, &bin_op_instruction->base);811 ir_want_debug_safety(g, &bin_op_instruction->base);
...@@ -831,22 +827,22 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,...@@ -831,22 +827,22 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,
831 case IrBinOpCmpGreaterThan:827 case IrBinOpCmpGreaterThan:
832 case IrBinOpCmpLessOrEq:828 case IrBinOpCmpLessOrEq:
833 case IrBinOpCmpGreaterOrEq:829 case IrBinOpCmpGreaterOrEq:
834 if (op1->type_entry->id == TypeTableEntryIdFloat) {830 if (op1->value.type->id == TypeTableEntryIdFloat) {
835 LLVMRealPredicate pred = cmp_op_to_real_predicate(op_id);831 LLVMRealPredicate pred = cmp_op_to_real_predicate(op_id);
836 return LLVMBuildFCmp(g->builder, pred, op1_value, op2_value, "");832 return LLVMBuildFCmp(g->builder, pred, op1_value, op2_value, "");
837 } else if (op1->type_entry->id == TypeTableEntryIdInt) {833 } else if (op1->value.type->id == TypeTableEntryIdInt) {
838 LLVMIntPredicate pred = cmp_op_to_int_predicate(op_id, op1->type_entry->data.integral.is_signed);834 LLVMIntPredicate pred = cmp_op_to_int_predicate(op_id, op1->value.type->data.integral.is_signed);
839 return LLVMBuildICmp(g->builder, pred, op1_value, op2_value, "");835 return LLVMBuildICmp(g->builder, pred, op1_value, op2_value, "");
840 } else if (op1->type_entry->id == TypeTableEntryIdEnum) {836 } else if (op1->value.type->id == TypeTableEntryIdEnum) {
841 if (op1->type_entry->data.enumeration.gen_field_count == 0) {837 if (op1->value.type->data.enumeration.gen_field_count == 0) {
842 LLVMIntPredicate pred = cmp_op_to_int_predicate(op_id, false);838 LLVMIntPredicate pred = cmp_op_to_int_predicate(op_id, false);
843 return LLVMBuildICmp(g->builder, pred, op1_value, op2_value, "");839 return LLVMBuildICmp(g->builder, pred, op1_value, op2_value, "");
844 } else {840 } else {
845 zig_unreachable();841 zig_unreachable();
846 }842 }
847 } else if (op1->type_entry->id == TypeTableEntryIdPureError ||843 } else if (op1->value.type->id == TypeTableEntryIdPureError ||
848 op1->type_entry->id == TypeTableEntryIdPointer ||844 op1->value.type->id == TypeTableEntryIdPointer ||
849 op1->type_entry->id == TypeTableEntryIdBool)845 op1->value.type->id == TypeTableEntryIdBool)
850 {846 {
851 LLVMIntPredicate pred = cmp_op_to_int_predicate(op_id, false);847 LLVMIntPredicate pred = cmp_op_to_int_predicate(op_id, false);
852 return LLVMBuildICmp(g->builder, pred, op1_value, op2_value, "");848 return LLVMBuildICmp(g->builder, pred, op1_value, op2_value, "");
...@@ -855,15 +851,15 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,...@@ -855,15 +851,15 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,
855 }851 }
856 case IrBinOpAdd:852 case IrBinOpAdd:
857 case IrBinOpAddWrap:853 case IrBinOpAddWrap:
858 if (op1->type_entry->id == TypeTableEntryIdFloat) {854 if (op1->value.type->id == TypeTableEntryIdFloat) {
859 return LLVMBuildFAdd(g->builder, op1_value, op2_value, "");855 return LLVMBuildFAdd(g->builder, op1_value, op2_value, "");
860 } else if (op1->type_entry->id == TypeTableEntryIdInt) {856 } else if (op1->value.type->id == TypeTableEntryIdInt) {
861 bool is_wrapping = (op_id == IrBinOpAddWrap);857 bool is_wrapping = (op_id == IrBinOpAddWrap);
862 if (is_wrapping) {858 if (is_wrapping) {
863 return LLVMBuildAdd(g->builder, op1_value, op2_value, "");859 return LLVMBuildAdd(g->builder, op1_value, op2_value, "");
864 } else if (want_debug_safety) {860 } else if (want_debug_safety) {
865 return gen_overflow_op(g, op1->type_entry, AddSubMulAdd, op1_value, op2_value);861 return gen_overflow_op(g, op1->value.type, AddSubMulAdd, op1_value, op2_value);
866 } else if (op1->type_entry->data.integral.is_signed) {862 } else if (op1->value.type->data.integral.is_signed) {
867 return LLVMBuildNSWAdd(g->builder, op1_value, op2_value, "");863 return LLVMBuildNSWAdd(g->builder, op1_value, op2_value, "");
868 } else {864 } else {
869 return LLVMBuildNUWAdd(g->builder, op1_value, op2_value, "");865 return LLVMBuildNUWAdd(g->builder, op1_value, op2_value, "");
...@@ -880,36 +876,36 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,...@@ -880,36 +876,36 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,
880 case IrBinOpBitShiftLeft:876 case IrBinOpBitShiftLeft:
881 case IrBinOpBitShiftLeftWrap:877 case IrBinOpBitShiftLeftWrap:
882 {878 {
883 assert(op1->type_entry->id == TypeTableEntryIdInt);879 assert(op1->value.type->id == TypeTableEntryIdInt);
884 bool is_wrapping = (op_id == IrBinOpBitShiftLeftWrap);880 bool is_wrapping = (op_id == IrBinOpBitShiftLeftWrap);
885 if (is_wrapping) {881 if (is_wrapping) {
886 return LLVMBuildShl(g->builder, op1_value, op2_value, "");882 return LLVMBuildShl(g->builder, op1_value, op2_value, "");
887 } else if (want_debug_safety) {883 } else if (want_debug_safety) {
888 return gen_overflow_shl_op(g, op1->type_entry, op1_value, op2_value);884 return gen_overflow_shl_op(g, op1->value.type, op1_value, op2_value);
889 } else if (op1->type_entry->data.integral.is_signed) {885 } else if (op1->value.type->data.integral.is_signed) {
890 return ZigLLVMBuildNSWShl(g->builder, op1_value, op2_value, "");886 return ZigLLVMBuildNSWShl(g->builder, op1_value, op2_value, "");
891 } else {887 } else {
892 return ZigLLVMBuildNUWShl(g->builder, op1_value, op2_value, "");888 return ZigLLVMBuildNUWShl(g->builder, op1_value, op2_value, "");
893 }889 }
894 }890 }
895 case IrBinOpBitShiftRight:891 case IrBinOpBitShiftRight:
896 assert(op1->type_entry->id == TypeTableEntryIdInt);892 assert(op1->value.type->id == TypeTableEntryIdInt);
897 if (op1->type_entry->data.integral.is_signed) {893 if (op1->value.type->data.integral.is_signed) {
898 return LLVMBuildAShr(g->builder, op1_value, op2_value, "");894 return LLVMBuildAShr(g->builder, op1_value, op2_value, "");
899 } else {895 } else {
900 return LLVMBuildLShr(g->builder, op1_value, op2_value, "");896 return LLVMBuildLShr(g->builder, op1_value, op2_value, "");
901 }897 }
902 case IrBinOpSub:898 case IrBinOpSub:
903 case IrBinOpSubWrap:899 case IrBinOpSubWrap:
904 if (op1->type_entry->id == TypeTableEntryIdFloat) {900 if (op1->value.type->id == TypeTableEntryIdFloat) {
905 return LLVMBuildFSub(g->builder, op1_value, op2_value, "");901 return LLVMBuildFSub(g->builder, op1_value, op2_value, "");
906 } else if (op1->type_entry->id == TypeTableEntryIdInt) {902 } else if (op1->value.type->id == TypeTableEntryIdInt) {
907 bool is_wrapping = (op_id == IrBinOpSubWrap);903 bool is_wrapping = (op_id == IrBinOpSubWrap);
908 if (is_wrapping) {904 if (is_wrapping) {
909 return LLVMBuildSub(g->builder, op1_value, op2_value, "");905 return LLVMBuildSub(g->builder, op1_value, op2_value, "");
910 } else if (want_debug_safety) {906 } else if (want_debug_safety) {
911 return gen_overflow_op(g, op1->type_entry, AddSubMulSub, op1_value, op2_value);907 return gen_overflow_op(g, op1->value.type, AddSubMulSub, op1_value, op2_value);
912 } else if (op1->type_entry->data.integral.is_signed) {908 } else if (op1->value.type->data.integral.is_signed) {
913 return LLVMBuildNSWSub(g->builder, op1_value, op2_value, "");909 return LLVMBuildNSWSub(g->builder, op1_value, op2_value, "");
914 } else {910 } else {
915 return LLVMBuildNUWSub(g->builder, op1_value, op2_value, "");911 return LLVMBuildNUWSub(g->builder, op1_value, op2_value, "");
...@@ -919,15 +915,15 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,...@@ -919,15 +915,15 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,
919 }915 }
920 case IrBinOpMult:916 case IrBinOpMult:
921 case IrBinOpMultWrap:917 case IrBinOpMultWrap:
922 if (op1->type_entry->id == TypeTableEntryIdFloat) {918 if (op1->value.type->id == TypeTableEntryIdFloat) {
923 return LLVMBuildFMul(g->builder, op1_value, op2_value, "");919 return LLVMBuildFMul(g->builder, op1_value, op2_value, "");
924 } else if (op1->type_entry->id == TypeTableEntryIdInt) {920 } else if (op1->value.type->id == TypeTableEntryIdInt) {
925 bool is_wrapping = (op_id == IrBinOpMultWrap);921 bool is_wrapping = (op_id == IrBinOpMultWrap);
926 if (is_wrapping) {922 if (is_wrapping) {
927 return LLVMBuildMul(g->builder, op1_value, op2_value, "");923 return LLVMBuildMul(g->builder, op1_value, op2_value, "");
928 } else if (want_debug_safety) {924 } else if (want_debug_safety) {
929 return gen_overflow_op(g, op1->type_entry, AddSubMulMul, op1_value, op2_value);925 return gen_overflow_op(g, op1->value.type, AddSubMulMul, op1_value, op2_value);
930 } else if (op1->type_entry->data.integral.is_signed) {926 } else if (op1->value.type->data.integral.is_signed) {
931 return LLVMBuildNSWMul(g->builder, op1_value, op2_value, "");927 return LLVMBuildNSWMul(g->builder, op1_value, op2_value, "");
932 } else {928 } else {
933 return LLVMBuildNUWMul(g->builder, op1_value, op2_value, "");929 return LLVMBuildNUWMul(g->builder, op1_value, op2_value, "");
...@@ -936,13 +932,13 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,...@@ -936,13 +932,13 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,
936 zig_unreachable();932 zig_unreachable();
937 }933 }
938 case IrBinOpDiv:934 case IrBinOpDiv:
939 return gen_div(g, want_debug_safety, op1_value, op2_value, op1->type_entry, false);935 return gen_div(g, want_debug_safety, op1_value, op2_value, op1->value.type, false);
940 case IrBinOpMod:936 case IrBinOpMod:
941 if (op1->type_entry->id == TypeTableEntryIdFloat) {937 if (op1->value.type->id == TypeTableEntryIdFloat) {
942 return LLVMBuildFRem(g->builder, op1_value, op2_value, "");938 return LLVMBuildFRem(g->builder, op1_value, op2_value, "");
943 } else {939 } else {
944 assert(op1->type_entry->id == TypeTableEntryIdInt);940 assert(op1->value.type->id == TypeTableEntryIdInt);
945 if (op1->type_entry->data.integral.is_signed) {941 if (op1->value.type->data.integral.is_signed) {
946 return LLVMBuildSRem(g->builder, op1_value, op2_value, "");942 return LLVMBuildSRem(g->builder, op1_value, op2_value, "");
947 } else {943 } else {
948 return LLVMBuildURem(g->builder, op1_value, op2_value, "");944 return LLVMBuildURem(g->builder, op1_value, op2_value, "");
...@@ -955,8 +951,8 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,...@@ -955,8 +951,8 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,
955static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,951static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,
956 IrInstructionCast *cast_instruction)952 IrInstructionCast *cast_instruction)
957{953{
958 TypeTableEntry *actual_type = cast_instruction->value->type_entry;954 TypeTableEntry *actual_type = cast_instruction->value->value.type;
959 TypeTableEntry *wanted_type = cast_instruction->base.type_entry;955 TypeTableEntry *wanted_type = cast_instruction->base.value.type;
960 LLVMValueRef expr_val = ir_llvm_value(g, cast_instruction->value);956 LLVMValueRef expr_val = ir_llvm_value(g, cast_instruction->value);
961 assert(expr_val);957 assert(expr_val);
962958
...@@ -977,35 +973,9 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,...@@ -977,35 +973,9 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,
977 return LLVMBuildPtrToInt(g->builder, expr_val, wanted_type->type_ref, "");973 return LLVMBuildPtrToInt(g->builder, expr_val, wanted_type->type_ref, "");
978 case CastOpIntToPtr:974 case CastOpIntToPtr:
979 return LLVMBuildIntToPtr(g->builder, expr_val, wanted_type->type_ref, "");975 return LLVMBuildIntToPtr(g->builder, expr_val, wanted_type->type_ref, "");
980 case CastOpPointerReinterpret:
981 return LLVMBuildBitCast(g->builder, expr_val, wanted_type->type_ref, "");
982 case CastOpWidenOrShorten:976 case CastOpWidenOrShorten:
983 return gen_widen_or_shorten(g, ir_want_debug_safety(g, &cast_instruction->base),977 return gen_widen_or_shorten(g, ir_want_debug_safety(g, &cast_instruction->base),
984 actual_type, wanted_type, expr_val);978 actual_type, wanted_type, expr_val);
985 case CastOpToUnknownSizeArray:
986 {
987 assert(cast_instruction->tmp_ptr);
988 assert(wanted_type->id == TypeTableEntryIdStruct);
989 assert(wanted_type->data.structure.is_slice);
990
991 TypeTableEntry *pointer_type = wanted_type->data.structure.fields[0].type_entry;
992
993
994 size_t ptr_index = wanted_type->data.structure.fields[0].gen_index;
995 if (ptr_index != SIZE_MAX) {
996 LLVMValueRef ptr_ptr = LLVMBuildStructGEP(g->builder, cast_instruction->tmp_ptr, ptr_index, "");
997 LLVMValueRef expr_bitcast = LLVMBuildBitCast(g->builder, expr_val, pointer_type->type_ref, "");
998 LLVMBuildStore(g->builder, expr_bitcast, ptr_ptr);
999 }
1000
1001 size_t len_index = wanted_type->data.structure.fields[1].gen_index;
1002 LLVMValueRef len_ptr = LLVMBuildStructGEP(g->builder, cast_instruction->tmp_ptr, len_index, "");
1003 LLVMValueRef len_val = LLVMConstInt(g->builtin_types.entry_usize->type_ref,
1004 actual_type->data.array.len, false);
1005 LLVMBuildStore(g->builder, len_val, len_ptr);
1006
1007 return cast_instruction->tmp_ptr;
1008 }
1009 case CastOpResizeSlice:979 case CastOpResizeSlice:
1010 {980 {
1011 assert(cast_instruction->tmp_ptr);981 assert(cast_instruction->tmp_ptr);
...@@ -1123,6 +1093,15 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,...@@ -1123,6 +1093,15 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,
1123 zig_unreachable();1093 zig_unreachable();
1124}1094}
11251095
1096static LLVMValueRef ir_render_pointer_reinterpret(CodeGen *g, IrExecutable *executable,
1097 IrInstructionPointerReinterpret *instruction)
1098{
1099 TypeTableEntry *wanted_type = instruction->base.value.type;
1100 LLVMValueRef ptr = ir_llvm_value(g, instruction->ptr);
1101 return LLVMBuildBitCast(g->builder, ptr, wanted_type->type_ref, "");
1102}
1103
1104
1126static LLVMValueRef ir_render_unreachable(CodeGen *g, IrExecutable *executable,1105static LLVMValueRef ir_render_unreachable(CodeGen *g, IrExecutable *executable,
1127 IrInstructionUnreachable *unreachable_instruction)1106 IrInstructionUnreachable *unreachable_instruction)
1128{1107{
...@@ -1152,7 +1131,7 @@ static LLVMValueRef ir_render_br(CodeGen *g, IrExecutable *executable, IrInstruc...@@ -1152,7 +1131,7 @@ static LLVMValueRef ir_render_br(CodeGen *g, IrExecutable *executable, IrInstruc
1152static LLVMValueRef ir_render_un_op(CodeGen *g, IrExecutable *executable, IrInstructionUnOp *un_op_instruction) {1131static LLVMValueRef ir_render_un_op(CodeGen *g, IrExecutable *executable, IrInstructionUnOp *un_op_instruction) {
1153 IrUnOp op_id = un_op_instruction->op_id;1132 IrUnOp op_id = un_op_instruction->op_id;
1154 LLVMValueRef expr = ir_llvm_value(g, un_op_instruction->value);1133 LLVMValueRef expr = ir_llvm_value(g, un_op_instruction->value);
1155 TypeTableEntry *expr_type = un_op_instruction->value->type_entry;1134 TypeTableEntry *expr_type = un_op_instruction->value->value.type;
11561135
1157 switch (op_id) {1136 switch (op_id) {
1158 case IrUnOpInvalid:1137 case IrUnOpInvalid:
...@@ -1213,7 +1192,7 @@ static LLVMValueRef ir_render_decl_var(CodeGen *g, IrExecutable *executable,...@@ -1213,7 +1192,7 @@ static LLVMValueRef ir_render_decl_var(CodeGen *g, IrExecutable *executable,
1213{1192{
1214 VariableTableEntry *var = decl_var_instruction->var;1193 VariableTableEntry *var = decl_var_instruction->var;
12151194
1216 if (!type_has_bits(var->type))1195 if (!type_has_bits(var->value.type))
1217 return nullptr;1196 return nullptr;
12181197
1219 if (var->ref_count == 0)1198 if (var->ref_count == 0)
...@@ -1224,22 +1203,23 @@ static LLVMValueRef ir_render_decl_var(CodeGen *g, IrExecutable *executable,...@@ -1224,22 +1203,23 @@ static LLVMValueRef ir_render_decl_var(CodeGen *g, IrExecutable *executable,
1224 bool have_init_expr = false;1203 bool have_init_expr = false;
1225 bool want_zeroes = false;1204 bool want_zeroes = false;
12261205
1227 ConstExprValue *const_val = &init_value->static_value;1206 ConstExprValue *const_val = &init_value->value;
1228 if (const_val->special == ConstValSpecialRuntime || const_val->special == ConstValSpecialStatic)1207 if (const_val->special == ConstValSpecialRuntime || const_val->special == ConstValSpecialStatic)
1229 have_init_expr = true;1208 have_init_expr = true;
1230 if (const_val->special == ConstValSpecialZeroes)1209 if (const_val->special == ConstValSpecialZeroes)
1231 want_zeroes = true;1210 want_zeroes = true;
12321211
1233 if (have_init_expr) {1212 if (have_init_expr) {
1234 gen_assign_raw(g, var->value_ref, ir_llvm_value(g, init_value), var->type, init_value->type_entry);1213 assert(var->value.type == init_value->value.type);
1214 gen_assign_raw(g, var->value_ref, ir_llvm_value(g, init_value), var->value.type);
1235 } else {1215 } else {
1236 bool ignore_uninit = false;1216 bool ignore_uninit = false;
1237 // handle runtime stack allocation1217 // handle runtime stack allocation
1238 bool want_safe = ir_want_debug_safety(g, &decl_var_instruction->base);1218 bool want_safe = ir_want_debug_safety(g, &decl_var_instruction->base);
1239 if (!ignore_uninit && (want_safe || want_zeroes)) {1219 if (!ignore_uninit && (want_safe || want_zeroes)) {
1240 TypeTableEntry *usize = g->builtin_types.entry_usize;1220 TypeTableEntry *usize = g->builtin_types.entry_usize;
1241 uint64_t size_bytes = LLVMStoreSizeOfType(g->target_data_ref, var->type->type_ref);1221 uint64_t size_bytes = LLVMStoreSizeOfType(g->target_data_ref, var->value.type->type_ref);
1242 uint64_t align_bytes = get_memcpy_align(g, var->type);1222 uint64_t align_bytes = get_memcpy_align(g, var->value.type);
12431223
1244 // memset uninitialized memory to 0xa1224 // memset uninitialized memory to 0xa
1245 LLVMTypeRef ptr_u8 = LLVMPointerType(LLVMInt8Type(), 0);1225 LLVMTypeRef ptr_u8 = LLVMPointerType(LLVMInt8Type(), 0);
...@@ -1265,16 +1245,16 @@ static LLVMValueRef ir_render_decl_var(CodeGen *g, IrExecutable *executable,...@@ -1265,16 +1245,16 @@ static LLVMValueRef ir_render_decl_var(CodeGen *g, IrExecutable *executable,
12651245
1266static LLVMValueRef ir_render_load_ptr(CodeGen *g, IrExecutable *executable, IrInstructionLoadPtr *instruction) {1246static LLVMValueRef ir_render_load_ptr(CodeGen *g, IrExecutable *executable, IrInstructionLoadPtr *instruction) {
1267 LLVMValueRef ptr = ir_llvm_value(g, instruction->ptr);1247 LLVMValueRef ptr = ir_llvm_value(g, instruction->ptr);
1268 return get_handle_value(g, ptr, instruction->base.type_entry);1248 return get_handle_value(g, ptr, instruction->base.value.type);
1269}1249}
12701250
1271static LLVMValueRef ir_render_store_ptr(CodeGen *g, IrExecutable *executable, IrInstructionStorePtr *instruction) {1251static LLVMValueRef ir_render_store_ptr(CodeGen *g, IrExecutable *executable, IrInstructionStorePtr *instruction) {
1272 LLVMValueRef ptr = ir_llvm_value(g, instruction->ptr);1252 LLVMValueRef ptr = ir_llvm_value(g, instruction->ptr);
1273 LLVMValueRef value = ir_llvm_value(g, instruction->value);1253 LLVMValueRef value = ir_llvm_value(g, instruction->value);
12741254
1275 assert(instruction->ptr->type_entry->id == TypeTableEntryIdPointer);1255 assert(instruction->ptr->value.type->id == TypeTableEntryIdPointer);
1276 TypeTableEntry *op1_type = instruction->ptr->type_entry->data.pointer.child_type;1256 TypeTableEntry *op1_type = instruction->ptr->value.type->data.pointer.child_type;
1277 TypeTableEntry *op2_type = instruction->value->type_entry;1257 TypeTableEntry *op2_type = instruction->value->value.type;
12781258
1279 if (!type_has_bits(op1_type)) {1259 if (!type_has_bits(op1_type)) {
1280 return nullptr;1260 return nullptr;
...@@ -1290,7 +1270,7 @@ static LLVMValueRef ir_render_store_ptr(CodeGen *g, IrExecutable *executable, Ir...@@ -1290,7 +1270,7 @@ static LLVMValueRef ir_render_store_ptr(CodeGen *g, IrExecutable *executable, Ir
12901270
1291static LLVMValueRef ir_render_var_ptr(CodeGen *g, IrExecutable *executable, IrInstructionVarPtr *instruction) {1271static LLVMValueRef ir_render_var_ptr(CodeGen *g, IrExecutable *executable, IrInstructionVarPtr *instruction) {
1292 VariableTableEntry *var = instruction->var;1272 VariableTableEntry *var = instruction->var;
1293 if (type_has_bits(var->type)) {1273 if (type_has_bits(var->value.type)) {
1294 assert(var->value_ref);1274 assert(var->value_ref);
1295 return var->value_ref;1275 return var->value_ref;
1296 } else {1276 } else {
...@@ -1300,7 +1280,7 @@ static LLVMValueRef ir_render_var_ptr(CodeGen *g, IrExecutable *executable, IrIn...@@ -1300,7 +1280,7 @@ static LLVMValueRef ir_render_var_ptr(CodeGen *g, IrExecutable *executable, IrIn
13001280
1301static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrInstructionElemPtr *instruction) {1281static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrInstructionElemPtr *instruction) {
1302 LLVMValueRef array_ptr_ptr = ir_llvm_value(g, instruction->array_ptr);1282 LLVMValueRef array_ptr_ptr = ir_llvm_value(g, instruction->array_ptr);
1303 TypeTableEntry *array_ptr_type = instruction->array_ptr->type_entry;1283 TypeTableEntry *array_ptr_type = instruction->array_ptr->value.type;
1304 assert(array_ptr_type->id == TypeTableEntryIdPointer);1284 assert(array_ptr_type->id == TypeTableEntryIdPointer);
1305 TypeTableEntry *array_type = array_ptr_type->data.pointer.child_type;1285 TypeTableEntry *array_type = array_ptr_type->data.pointer.child_type;
1306 LLVMValueRef array_ptr = get_handle_value(g, array_ptr_ptr, array_type);1286 LLVMValueRef array_ptr = get_handle_value(g, array_ptr_ptr, array_type);
...@@ -1361,7 +1341,7 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr...@@ -1361,7 +1341,7 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr
1361 } else {1341 } else {
1362 assert(instruction->fn_ref);1342 assert(instruction->fn_ref);
1363 fn_val = ir_llvm_value(g, instruction->fn_ref);1343 fn_val = ir_llvm_value(g, instruction->fn_ref);
1364 fn_type = instruction->fn_ref->type_entry;1344 fn_type = instruction->fn_ref->value.type;
1365 }1345 }
13661346
1367 TypeTableEntry *src_return_type = fn_type->data.fn.fn_type_id.return_type;1347 TypeTableEntry *src_return_type = fn_type->data.fn.fn_type_id.return_type;
...@@ -1377,7 +1357,7 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr...@@ -1377,7 +1357,7 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr
1377 }1357 }
1378 for (size_t call_i = 0; call_i < instruction->arg_count; call_i += 1) {1358 for (size_t call_i = 0; call_i < instruction->arg_count; call_i += 1) {
1379 IrInstruction *param_instruction = instruction->args[call_i];1359 IrInstruction *param_instruction = instruction->args[call_i];
1380 TypeTableEntry *param_type = param_instruction->type_entry;1360 TypeTableEntry *param_type = param_instruction->value.type;
1381 if (is_var_args || type_has_bits(param_type)) {1361 if (is_var_args || type_has_bits(param_type)) {
1382 LLVMValueRef param_value = ir_llvm_value(g, param_instruction);1362 LLVMValueRef param_value = ir_llvm_value(g, param_instruction);
1383 assert(param_value);1363 assert(param_value);
...@@ -1526,7 +1506,7 @@ static LLVMValueRef ir_render_asm(CodeGen *g, IrExecutable *executable, IrInstru...@@ -1526,7 +1506,7 @@ static LLVMValueRef ir_render_asm(CodeGen *g, IrExecutable *executable, IrInstru
1526 buf_append_char(&constraint_buf, ',');1506 buf_append_char(&constraint_buf, ',');
1527 }1507 }
15281508
1529 param_types[param_index] = ir_input->type_entry->type_ref;1509 param_types[param_index] = ir_input->value.type->type_ref;
1530 param_values[param_index] = ir_llvm_value(g, ir_input);1510 param_values[param_index] = ir_llvm_value(g, ir_input);
1531 }1511 }
1532 for (size_t i = 0; i < asm_expr->clobber_list.length; i += 1, total_index += 1) {1512 for (size_t i = 0; i < asm_expr->clobber_list.length; i += 1, total_index += 1) {
...@@ -1541,7 +1521,7 @@ static LLVMValueRef ir_render_asm(CodeGen *g, IrExecutable *executable, IrInstru...@@ -1541,7 +1521,7 @@ static LLVMValueRef ir_render_asm(CodeGen *g, IrExecutable *executable, IrInstru
1541 if (instruction->return_count == 0) {1521 if (instruction->return_count == 0) {
1542 ret_type = LLVMVoidType();1522 ret_type = LLVMVoidType();
1543 } else {1523 } else {
1544 ret_type = instruction->base.type_entry->type_ref;1524 ret_type = instruction->base.value.type->type_ref;
1545 }1525 }
1546 LLVMTypeRef function_type = LLVMFunctionType(ret_type, param_types, input_and_output_count, false);1526 LLVMTypeRef function_type = LLVMFunctionType(ret_type, param_types, input_and_output_count, false);
15471527
...@@ -1565,13 +1545,13 @@ static LLVMValueRef gen_non_null_bit(CodeGen *g, TypeTableEntry *maybe_type, LLV...@@ -1565,13 +1545,13 @@ static LLVMValueRef gen_non_null_bit(CodeGen *g, TypeTableEntry *maybe_type, LLV
1565static LLVMValueRef ir_render_test_non_null(CodeGen *g, IrExecutable *executable,1545static LLVMValueRef ir_render_test_non_null(CodeGen *g, IrExecutable *executable,
1566 IrInstructionTestNonNull *instruction)1546 IrInstructionTestNonNull *instruction)
1567{1547{
1568 return gen_non_null_bit(g, instruction->value->type_entry, ir_llvm_value(g, instruction->value));1548 return gen_non_null_bit(g, instruction->value->value.type, ir_llvm_value(g, instruction->value));
1569}1549}
15701550
1571static LLVMValueRef ir_render_unwrap_maybe(CodeGen *g, IrExecutable *executable,1551static LLVMValueRef ir_render_unwrap_maybe(CodeGen *g, IrExecutable *executable,
1572 IrInstructionUnwrapMaybe *instruction)1552 IrInstructionUnwrapMaybe *instruction)
1573{1553{
1574 TypeTableEntry *ptr_type = instruction->value->type_entry;1554 TypeTableEntry *ptr_type = instruction->value->value.type;
1575 assert(ptr_type->id == TypeTableEntryIdPointer);1555 assert(ptr_type->id == TypeTableEntryIdPointer);
1576 TypeTableEntry *maybe_type = ptr_type->data.pointer.child_type;1556 TypeTableEntry *maybe_type = ptr_type->data.pointer.child_type;
1577 assert(maybe_type->id == TypeTableEntryIdMaybe);1557 assert(maybe_type->id == TypeTableEntryIdMaybe);
...@@ -1617,7 +1597,7 @@ static LLVMValueRef get_int_builtin_fn(CodeGen *g, TypeTableEntry *int_type, Bui...@@ -1617,7 +1597,7 @@ static LLVMValueRef get_int_builtin_fn(CodeGen *g, TypeTableEntry *int_type, Bui
1617}1597}
16181598
1619static LLVMValueRef ir_render_clz(CodeGen *g, IrExecutable *executable, IrInstructionClz *instruction) {1599static LLVMValueRef ir_render_clz(CodeGen *g, IrExecutable *executable, IrInstructionClz *instruction) {
1620 TypeTableEntry *int_type = instruction->base.type_entry;1600 TypeTableEntry *int_type = instruction->base.value.type;
1621 LLVMValueRef fn_val = get_int_builtin_fn(g, int_type, BuiltinFnIdClz);1601 LLVMValueRef fn_val = get_int_builtin_fn(g, int_type, BuiltinFnIdClz);
1622 LLVMValueRef operand = ir_llvm_value(g, instruction->value);1602 LLVMValueRef operand = ir_llvm_value(g, instruction->value);
1623 LLVMValueRef params[] {1603 LLVMValueRef params[] {
...@@ -1628,7 +1608,7 @@ static LLVMValueRef ir_render_clz(CodeGen *g, IrExecutable *executable, IrInstru...@@ -1628,7 +1608,7 @@ static LLVMValueRef ir_render_clz(CodeGen *g, IrExecutable *executable, IrInstru
1628}1608}
16291609
1630static LLVMValueRef ir_render_ctz(CodeGen *g, IrExecutable *executable, IrInstructionCtz *instruction) {1610static LLVMValueRef ir_render_ctz(CodeGen *g, IrExecutable *executable, IrInstructionCtz *instruction) {
1631 TypeTableEntry *int_type = instruction->base.type_entry;1611 TypeTableEntry *int_type = instruction->base.value.type;
1632 LLVMValueRef fn_val = get_int_builtin_fn(g, int_type, BuiltinFnIdCtz);1612 LLVMValueRef fn_val = get_int_builtin_fn(g, int_type, BuiltinFnIdCtz);
1633 LLVMValueRef operand = ir_llvm_value(g, instruction->value);1613 LLVMValueRef operand = ir_llvm_value(g, instruction->value);
1634 LLVMValueRef params[] {1614 LLVMValueRef params[] {
...@@ -1650,14 +1630,14 @@ static LLVMValueRef ir_render_switch_br(CodeGen *g, IrExecutable *executable, Ir...@@ -1650,14 +1630,14 @@ static LLVMValueRef ir_render_switch_br(CodeGen *g, IrExecutable *executable, Ir
1650}1630}
16511631
1652static LLVMValueRef ir_render_phi(CodeGen *g, IrExecutable *executable, IrInstructionPhi *instruction) {1632static LLVMValueRef ir_render_phi(CodeGen *g, IrExecutable *executable, IrInstructionPhi *instruction) {
1653 if (!type_has_bits(instruction->base.type_entry))1633 if (!type_has_bits(instruction->base.value.type))
1654 return nullptr;1634 return nullptr;
16551635
1656 LLVMTypeRef phi_type;1636 LLVMTypeRef phi_type;
1657 if (handle_is_ptr(instruction->base.type_entry)) {1637 if (handle_is_ptr(instruction->base.value.type)) {
1658 phi_type = LLVMPointerType(instruction->base.type_entry->type_ref, 0);1638 phi_type = LLVMPointerType(instruction->base.value.type->type_ref, 0);
1659 } else {1639 } else {
1660 phi_type = instruction->base.type_entry->type_ref;1640 phi_type = instruction->base.value.type->type_ref;
1661 }1641 }
16621642
1663 LLVMValueRef phi = LLVMBuildPhi(g->builder, phi_type, "");1643 LLVMValueRef phi = LLVMBuildPhi(g->builder, phi_type, "");
...@@ -1673,7 +1653,7 @@ static LLVMValueRef ir_render_phi(CodeGen *g, IrExecutable *executable, IrInstru...@@ -1673,7 +1653,7 @@ static LLVMValueRef ir_render_phi(CodeGen *g, IrExecutable *executable, IrInstru
16731653
1674static LLVMValueRef ir_render_ref(CodeGen *g, IrExecutable *executable, IrInstructionRef *instruction) {1654static LLVMValueRef ir_render_ref(CodeGen *g, IrExecutable *executable, IrInstructionRef *instruction) {
1675 LLVMValueRef value = ir_llvm_value(g, instruction->value);1655 LLVMValueRef value = ir_llvm_value(g, instruction->value);
1676 if (handle_is_ptr(instruction->value->type_entry)) {1656 if (handle_is_ptr(instruction->value->value.type)) {
1677 return value;1657 return value;
1678 } else {1658 } else {
1679 assert(instruction->tmp_ptr);1659 assert(instruction->tmp_ptr);
...@@ -1741,17 +1721,17 @@ static LLVMValueRef ir_render_div_exact(CodeGen *g, IrExecutable *executable, Ir...@@ -1741,17 +1721,17 @@ static LLVMValueRef ir_render_div_exact(CodeGen *g, IrExecutable *executable, Ir
1741 LLVMValueRef op2_val = ir_llvm_value(g, instruction->op2);1721 LLVMValueRef op2_val = ir_llvm_value(g, instruction->op2);
17421722
1743 bool want_debug_safety = ir_want_debug_safety(g, &instruction->base);1723 bool want_debug_safety = ir_want_debug_safety(g, &instruction->base);
1744 return gen_div(g, want_debug_safety, op1_val, op2_val, instruction->base.type_entry, true);1724 return gen_div(g, want_debug_safety, op1_val, op2_val, instruction->base.value.type, true);
1745}1725}
17461726
1747static LLVMValueRef ir_render_truncate(CodeGen *g, IrExecutable *executable, IrInstructionTruncate *instruction) {1727static LLVMValueRef ir_render_truncate(CodeGen *g, IrExecutable *executable, IrInstructionTruncate *instruction) {
1748 TypeTableEntry *dest_type = get_underlying_type(instruction->base.type_entry);1728 TypeTableEntry *dest_type = get_underlying_type(instruction->base.value.type);
1749 LLVMValueRef target_val = ir_llvm_value(g, instruction->target);1729 LLVMValueRef target_val = ir_llvm_value(g, instruction->target);
1750 return LLVMBuildTrunc(g->builder, target_val, dest_type->type_ref, "");1730 return LLVMBuildTrunc(g->builder, target_val, dest_type->type_ref, "");
1751}1731}
17521732
1753static LLVMValueRef ir_render_alloca(CodeGen *g, IrExecutable *executable, IrInstructionAlloca *instruction) {1733static LLVMValueRef ir_render_alloca(CodeGen *g, IrExecutable *executable, IrInstructionAlloca *instruction) {
1754 TypeTableEntry *slice_type = get_underlying_type(instruction->base.type_entry);1734 TypeTableEntry *slice_type = get_underlying_type(instruction->base.value.type);
1755 TypeTableEntry *ptr_type = slice_type->data.structure.fields[slice_ptr_index].type_entry;1735 TypeTableEntry *ptr_type = slice_type->data.structure.fields[slice_ptr_index].type_entry;
1756 TypeTableEntry *child_type = ptr_type->data.pointer.child_type;1736 TypeTableEntry *child_type = ptr_type->data.pointer.child_type;
1757 LLVMValueRef size_val = ir_llvm_value(g, instruction->count);1737 LLVMValueRef size_val = ir_llvm_value(g, instruction->count);
...@@ -1814,12 +1794,14 @@ static LLVMValueRef ir_render_memcpy(CodeGen *g, IrExecutable *executable, IrIns...@@ -1814,12 +1794,14 @@ static LLVMValueRef ir_render_memcpy(CodeGen *g, IrExecutable *executable, IrIns
1814}1794}
18151795
1816static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutable *executable, IrInstructionSlice *instruction) {1796static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutable *executable, IrInstructionSlice *instruction) {
1817 TypeTableEntry *array_type = get_underlying_type(instruction->ptr->type_entry);1797 assert(instruction->tmp_ptr);
1798
1799 TypeTableEntry *array_type = get_underlying_type(instruction->ptr->value.type);
18181800
1819 LLVMValueRef tmp_struct_ptr = instruction->tmp_ptr;1801 LLVMValueRef tmp_struct_ptr = instruction->tmp_ptr;
1820 LLVMValueRef array_ptr = ir_llvm_value(g, instruction->ptr);1802 LLVMValueRef array_ptr = ir_llvm_value(g, instruction->ptr);
18211803
1822 bool want_debug_safety = ir_want_debug_safety(g, &instruction->base);1804 bool want_debug_safety = instruction->safety_check_on && ir_want_debug_safety(g, &instruction->base);
18231805
1824 if (array_type->id == TypeTableEntryIdArray) {1806 if (array_type->id == TypeTableEntryIdArray) {
1825 LLVMValueRef start_val = ir_llvm_value(g, instruction->start);1807 LLVMValueRef start_val = ir_llvm_value(g, instruction->start);
...@@ -1997,7 +1979,7 @@ static LLVMValueRef ir_render_overflow_op(CodeGen *g, IrExecutable *executable,...@@ -1997,7 +1979,7 @@ static LLVMValueRef ir_render_overflow_op(CodeGen *g, IrExecutable *executable,
1997}1979}
19981980
1999static LLVMValueRef ir_render_test_err(CodeGen *g, IrExecutable *executable, IrInstructionTestErr *instruction) {1981static LLVMValueRef ir_render_test_err(CodeGen *g, IrExecutable *executable, IrInstructionTestErr *instruction) {
2000 TypeTableEntry *err_union_type = get_underlying_type(instruction->value->type_entry);1982 TypeTableEntry *err_union_type = get_underlying_type(instruction->value->value.type);
2001 TypeTableEntry *child_type = get_underlying_type(err_union_type->data.error.child_type);1983 TypeTableEntry *child_type = get_underlying_type(err_union_type->data.error.child_type);
2002 LLVMValueRef err_union_handle = ir_llvm_value(g, instruction->value);1984 LLVMValueRef err_union_handle = ir_llvm_value(g, instruction->value);
20031985
...@@ -2014,7 +1996,7 @@ static LLVMValueRef ir_render_test_err(CodeGen *g, IrExecutable *executable, IrI...@@ -2014,7 +1996,7 @@ static LLVMValueRef ir_render_test_err(CodeGen *g, IrExecutable *executable, IrI
2014}1996}
20151997
2016static LLVMValueRef ir_render_unwrap_err_code(CodeGen *g, IrExecutable *executable, IrInstructionUnwrapErrCode *instruction) {1998static LLVMValueRef ir_render_unwrap_err_code(CodeGen *g, IrExecutable *executable, IrInstructionUnwrapErrCode *instruction) {
2017 TypeTableEntry *ptr_type = get_underlying_type(instruction->value->type_entry);1999 TypeTableEntry *ptr_type = get_underlying_type(instruction->value->value.type);
2018 TypeTableEntry *err_union_type = get_underlying_type(ptr_type->data.pointer.child_type);2000 TypeTableEntry *err_union_type = get_underlying_type(ptr_type->data.pointer.child_type);
2019 TypeTableEntry *child_type = get_underlying_type(err_union_type->data.error.child_type);2001 TypeTableEntry *child_type = get_underlying_type(err_union_type->data.error.child_type);
2020 LLVMValueRef err_union_ptr = ir_llvm_value(g, instruction->value);2002 LLVMValueRef err_union_ptr = ir_llvm_value(g, instruction->value);
...@@ -2029,7 +2011,7 @@ static LLVMValueRef ir_render_unwrap_err_code(CodeGen *g, IrExecutable *executab...@@ -2029,7 +2011,7 @@ static LLVMValueRef ir_render_unwrap_err_code(CodeGen *g, IrExecutable *executab
2029}2011}
20302012
2031static LLVMValueRef ir_render_unwrap_err_payload(CodeGen *g, IrExecutable *executable, IrInstructionUnwrapErrPayload *instruction) {2013static LLVMValueRef ir_render_unwrap_err_payload(CodeGen *g, IrExecutable *executable, IrInstructionUnwrapErrPayload *instruction) {
2032 TypeTableEntry *ptr_type = get_underlying_type(instruction->value->type_entry);2014 TypeTableEntry *ptr_type = get_underlying_type(instruction->value->value.type);
2033 TypeTableEntry *err_union_type = get_underlying_type(ptr_type->data.pointer.child_type);2015 TypeTableEntry *err_union_type = get_underlying_type(ptr_type->data.pointer.child_type);
2034 TypeTableEntry *child_type = get_underlying_type(err_union_type->data.error.child_type);2016 TypeTableEntry *child_type = get_underlying_type(err_union_type->data.error.child_type);
2035 LLVMValueRef err_union_ptr = ir_llvm_value(g, instruction->value);2017 LLVMValueRef err_union_ptr = ir_llvm_value(g, instruction->value);
...@@ -2063,7 +2045,7 @@ static LLVMValueRef ir_render_unwrap_err_payload(CodeGen *g, IrExecutable *execu...@@ -2063,7 +2045,7 @@ static LLVMValueRef ir_render_unwrap_err_payload(CodeGen *g, IrExecutable *execu
2063}2045}
20642046
2065static LLVMValueRef ir_render_maybe_wrap(CodeGen *g, IrExecutable *executable, IrInstructionMaybeWrap *instruction) {2047static LLVMValueRef ir_render_maybe_wrap(CodeGen *g, IrExecutable *executable, IrInstructionMaybeWrap *instruction) {
2066 TypeTableEntry *wanted_type = instruction->base.type_entry;2048 TypeTableEntry *wanted_type = instruction->base.value.type;
20672049
2068 assert(wanted_type->id == TypeTableEntryIdMaybe);2050 assert(wanted_type->id == TypeTableEntryIdMaybe);
20692051
...@@ -2079,7 +2061,8 @@ static LLVMValueRef ir_render_maybe_wrap(CodeGen *g, IrExecutable *executable, I...@@ -2079,7 +2061,8 @@ static LLVMValueRef ir_render_maybe_wrap(CodeGen *g, IrExecutable *executable, I
2079 assert(instruction->tmp_ptr);2061 assert(instruction->tmp_ptr);
20802062
2081 LLVMValueRef val_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, maybe_child_index, "");2063 LLVMValueRef val_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, maybe_child_index, "");
2082 gen_assign_raw(g, val_ptr, payload_val, child_type, instruction->value->type_entry);2064 assert(child_type == instruction->value->value.type);
2065 gen_assign_raw(g, val_ptr, payload_val, child_type);
2083 LLVMValueRef maybe_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, maybe_null_index, "");2066 LLVMValueRef maybe_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, maybe_null_index, "");
2084 LLVMBuildStore(g->builder, LLVMConstAllOnes(LLVMInt1Type()), maybe_ptr);2067 LLVMBuildStore(g->builder, LLVMConstAllOnes(LLVMInt1Type()), maybe_ptr);
20852068
...@@ -2087,7 +2070,7 @@ static LLVMValueRef ir_render_maybe_wrap(CodeGen *g, IrExecutable *executable, I...@@ -2087,7 +2070,7 @@ static LLVMValueRef ir_render_maybe_wrap(CodeGen *g, IrExecutable *executable, I
2087}2070}
20882071
2089static LLVMValueRef ir_render_err_wrap_code(CodeGen *g, IrExecutable *executable, IrInstructionErrWrapCode *instruction) {2072static LLVMValueRef ir_render_err_wrap_code(CodeGen *g, IrExecutable *executable, IrInstructionErrWrapCode *instruction) {
2090 TypeTableEntry *wanted_type = instruction->base.type_entry;2073 TypeTableEntry *wanted_type = instruction->base.value.type;
20912074
2092 assert(wanted_type->id == TypeTableEntryIdErrorUnion);2075 assert(wanted_type->id == TypeTableEntryIdErrorUnion);
20932076
...@@ -2106,7 +2089,7 @@ static LLVMValueRef ir_render_err_wrap_code(CodeGen *g, IrExecutable *executable...@@ -2106,7 +2089,7 @@ static LLVMValueRef ir_render_err_wrap_code(CodeGen *g, IrExecutable *executable
2106}2089}
21072090
2108static LLVMValueRef ir_render_err_wrap_payload(CodeGen *g, IrExecutable *executable, IrInstructionErrWrapPayload *instruction) {2091static LLVMValueRef ir_render_err_wrap_payload(CodeGen *g, IrExecutable *executable, IrInstructionErrWrapPayload *instruction) {
2109 TypeTableEntry *wanted_type = instruction->base.type_entry;2092 TypeTableEntry *wanted_type = instruction->base.value.type;
21102093
2111 assert(wanted_type->id == TypeTableEntryIdErrorUnion);2094 assert(wanted_type->id == TypeTableEntryIdErrorUnion);
21122095
...@@ -2125,13 +2108,14 @@ static LLVMValueRef ir_render_err_wrap_payload(CodeGen *g, IrExecutable *executa...@@ -2125,13 +2108,14 @@ static LLVMValueRef ir_render_err_wrap_payload(CodeGen *g, IrExecutable *executa
2125 LLVMBuildStore(g->builder, ok_err_val, err_tag_ptr);2108 LLVMBuildStore(g->builder, ok_err_val, err_tag_ptr);
21262109
2127 LLVMValueRef payload_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, err_union_payload_index, "");2110 LLVMValueRef payload_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, err_union_payload_index, "");
2128 gen_assign_raw(g, payload_ptr, payload_val, child_type, instruction->value->type_entry);2111 assert(child_type == instruction->value->value.type);
2112 gen_assign_raw(g, payload_ptr, payload_val, child_type);
21292113
2130 return instruction->tmp_ptr;2114 return instruction->tmp_ptr;
2131}2115}
21322116
2133static LLVMValueRef ir_render_enum_tag(CodeGen *g, IrExecutable *executable, IrInstructionEnumTag *instruction) {2117static LLVMValueRef ir_render_enum_tag(CodeGen *g, IrExecutable *executable, IrInstructionEnumTag *instruction) {
2134 TypeTableEntry *enum_type = instruction->value->type_entry;2118 TypeTableEntry *enum_type = instruction->value->value.type;
2135 TypeTableEntry *tag_type = enum_type->data.enumeration.tag_type;2119 TypeTableEntry *tag_type = enum_type->data.enumeration.tag_type;
2136 if (!type_has_bits(tag_type))2120 if (!type_has_bits(tag_type))
2137 return nullptr;2121 return nullptr;
...@@ -2165,7 +2149,7 @@ static LLVMValueRef ir_render_init_enum(CodeGen *g, IrExecutable *executable, Ir...@@ -2165,7 +2149,7 @@ static LLVMValueRef ir_render_init_enum(CodeGen *g, IrExecutable *executable, Ir
2165 LLVMValueRef bitcasted_union_field_ptr = LLVMBuildBitCast(g->builder, union_field_ptr,2149 LLVMValueRef bitcasted_union_field_ptr = LLVMBuildBitCast(g->builder, union_field_ptr,
2166 LLVMPointerType(union_val_type->type_ref, 0), "");2150 LLVMPointerType(union_val_type->type_ref, 0), "");
21672151
2168 gen_assign_raw(g, bitcasted_union_field_ptr, new_union_val, union_val_type, union_val_type);2152 gen_assign_raw(g, bitcasted_union_field_ptr, new_union_val, union_val_type);
2169 }2153 }
21702154
2171 return tmp_struct_ptr;2155 return tmp_struct_ptr;
...@@ -2180,7 +2164,7 @@ static LLVMValueRef ir_render_struct_init(CodeGen *g, IrExecutable *executable,...@@ -2180,7 +2164,7 @@ static LLVMValueRef ir_render_struct_init(CodeGen *g, IrExecutable *executable,
21802164
2181 LLVMValueRef field_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, type_struct_field->gen_index, "");2165 LLVMValueRef field_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, type_struct_field->gen_index, "");
2182 LLVMValueRef value = ir_llvm_value(g, field->value);2166 LLVMValueRef value = ir_llvm_value(g, field->value);
2183 gen_assign_raw(g, field_ptr, value, type_struct_field->type_entry, type_struct_field->type_entry);2167 gen_assign_raw(g, field_ptr, value, type_struct_field->type_entry);
2184 }2168 }
2185 return instruction->tmp_ptr;2169 return instruction->tmp_ptr;
2186}2170}
...@@ -2323,6 +2307,8 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,...@@ -2323,6 +2307,8 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
2323 return ir_render_init_enum(g, executable, (IrInstructionInitEnum *)instruction);2307 return ir_render_init_enum(g, executable, (IrInstructionInitEnum *)instruction);
2324 case IrInstructionIdStructInit:2308 case IrInstructionIdStructInit:
2325 return ir_render_struct_init(g, executable, (IrInstructionStructInit *)instruction);2309 return ir_render_struct_init(g, executable, (IrInstructionStructInit *)instruction);
2310 case IrInstructionIdPointerReinterpret:
2311 return ir_render_pointer_reinterpret(g, executable, (IrInstructionPointerReinterpret *)instruction);
2326 case IrInstructionIdSwitchVar:2312 case IrInstructionIdSwitchVar:
2327 zig_panic("TODO render switch var instruction to LLVM");2313 zig_panic("TODO render switch var instruction to LLVM");
2328 case IrInstructionIdContainerInitList:2314 case IrInstructionIdContainerInitList:
...@@ -2350,37 +2336,40 @@ static void ir_render(CodeGen *g, FnTableEntry *fn_entry) {...@@ -2350,37 +2336,40 @@ static void ir_render(CodeGen *g, FnTableEntry *fn_entry) {
2350 }2336 }
2351}2337}
23522338
2353static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstExprValue *const_val) {2339static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
2340 TypeTableEntry *canon_type = get_underlying_type(const_val->type);
2341
2354 switch (const_val->special) {2342 switch (const_val->special) {
2355 case ConstValSpecialRuntime:2343 case ConstValSpecialRuntime:
2356 zig_unreachable();2344 zig_unreachable();
2357 case ConstValSpecialUndef:2345 case ConstValSpecialUndef:
2358 return LLVMGetUndef(type_entry->type_ref);2346 return LLVMGetUndef(canon_type->type_ref);
2359 case ConstValSpecialZeroes:2347 case ConstValSpecialZeroes:
2360 return LLVMConstNull(type_entry->type_ref);2348 return LLVMConstNull(canon_type->type_ref);
2361 case ConstValSpecialStatic:2349 case ConstValSpecialStatic:
2362 break;2350 break;
23632351
2364 }2352 }
23652353
2366 switch (type_entry->id) {2354 switch (canon_type->id) {
2367 case TypeTableEntryIdTypeDecl:2355 case TypeTableEntryIdTypeDecl:
2368 return gen_const_val(g, type_entry->data.type_decl.canonical_type, const_val);2356 zig_unreachable();
2369 case TypeTableEntryIdInt:2357 case TypeTableEntryIdInt:
2370 return LLVMConstInt(type_entry->type_ref, bignum_to_twos_complement(&const_val->data.x_bignum), false);2358 case TypeTableEntryIdEnumTag:
2359 return LLVMConstInt(canon_type->type_ref, bignum_to_twos_complement(&const_val->data.x_bignum), false);
2371 case TypeTableEntryIdPureError:2360 case TypeTableEntryIdPureError:
2372 assert(const_val->data.x_pure_err);2361 assert(const_val->data.x_pure_err);
2373 return LLVMConstInt(g->builtin_types.entry_pure_error->type_ref,2362 return LLVMConstInt(g->builtin_types.entry_pure_error->type_ref,
2374 const_val->data.x_pure_err->value, false);2363 const_val->data.x_pure_err->value, false);
2375 case TypeTableEntryIdFloat:2364 case TypeTableEntryIdFloat:
2376 if (const_val->data.x_bignum.kind == BigNumKindFloat) {2365 if (const_val->data.x_bignum.kind == BigNumKindFloat) {
2377 return LLVMConstReal(type_entry->type_ref, const_val->data.x_bignum.data.x_float);2366 return LLVMConstReal(canon_type->type_ref, const_val->data.x_bignum.data.x_float);
2378 } else {2367 } else {
2379 int64_t x = const_val->data.x_bignum.data.x_uint;2368 int64_t x = const_val->data.x_bignum.data.x_uint;
2380 if (const_val->data.x_bignum.is_negative) {2369 if (const_val->data.x_bignum.is_negative) {
2381 x = -x;2370 x = -x;
2382 }2371 }
2383 return LLVMConstReal(type_entry->type_ref, x);2372 return LLVMConstReal(canon_type->type_ref, x);
2384 }2373 }
2385 case TypeTableEntryIdBool:2374 case TypeTableEntryIdBool:
2386 if (const_val->data.x_bool) {2375 if (const_val->data.x_bool) {
...@@ -2390,12 +2379,12 @@ static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstE...@@ -2390,12 +2379,12 @@ static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstE
2390 }2379 }
2391 case TypeTableEntryIdMaybe:2380 case TypeTableEntryIdMaybe:
2392 {2381 {
2393 TypeTableEntry *child_type = type_entry->data.maybe.child_type;2382 TypeTableEntry *child_type = canon_type->data.maybe.child_type;
2394 if (child_type->id == TypeTableEntryIdPointer ||2383 if (child_type->id == TypeTableEntryIdPointer ||
2395 child_type->id == TypeTableEntryIdFn)2384 child_type->id == TypeTableEntryIdFn)
2396 {2385 {
2397 if (const_val->data.x_maybe) {2386 if (const_val->data.x_maybe) {
2398 return gen_const_val(g, child_type, const_val->data.x_maybe);2387 return gen_const_val(g, const_val->data.x_maybe);
2399 } else {2388 } else {
2400 return LLVMConstNull(child_type->type_ref);2389 return LLVMConstNull(child_type->type_ref);
2401 }2390 }
...@@ -2403,7 +2392,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstE...@@ -2403,7 +2392,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstE
2403 LLVMValueRef child_val;2392 LLVMValueRef child_val;
2404 LLVMValueRef maybe_val;2393 LLVMValueRef maybe_val;
2405 if (const_val->data.x_maybe) {2394 if (const_val->data.x_maybe) {
2406 child_val = gen_const_val(g, child_type, const_val->data.x_maybe);2395 child_val = gen_const_val(g, const_val->data.x_maybe);
2407 maybe_val = LLVMConstAllOnes(LLVMInt1Type());2396 maybe_val = LLVMConstAllOnes(LLVMInt1Type());
2408 } else {2397 } else {
2409 child_val = LLVMConstNull(child_type->type_ref);2398 child_val = LLVMConstNull(child_type->type_ref);
...@@ -2418,17 +2407,16 @@ static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstE...@@ -2418,17 +2407,16 @@ static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstE
2418 }2407 }
2419 case TypeTableEntryIdStruct:2408 case TypeTableEntryIdStruct:
2420 {2409 {
2421 LLVMValueRef *fields = allocate<LLVMValueRef>(type_entry->data.structure.gen_field_count);2410 LLVMValueRef *fields = allocate<LLVMValueRef>(canon_type->data.structure.gen_field_count);
2422 for (uint32_t i = 0; i < type_entry->data.structure.src_field_count; i += 1) {2411 for (uint32_t i = 0; i < canon_type->data.structure.src_field_count; i += 1) {
2423 TypeStructField *type_struct_field = &type_entry->data.structure.fields[i];2412 TypeStructField *type_struct_field = &canon_type->data.structure.fields[i];
2424 if (type_struct_field->gen_index == SIZE_MAX) {2413 if (type_struct_field->gen_index == SIZE_MAX) {
2425 continue;2414 continue;
2426 }2415 }
2427 fields[type_struct_field->gen_index] = gen_const_val(g, type_struct_field->type_entry,2416 fields[type_struct_field->gen_index] = gen_const_val(g, &const_val->data.x_struct.fields[i]);
2428 &const_val->data.x_struct.fields[i]);
2429 }2417 }
2430 return LLVMConstNamedStruct(type_entry->type_ref, fields,2418 return LLVMConstNamedStruct(canon_type->type_ref, fields,
2431 type_entry->data.structure.gen_field_count);2419 canon_type->data.structure.gen_field_count);
2432 }2420 }
2433 case TypeTableEntryIdUnion:2421 case TypeTableEntryIdUnion:
2434 {2422 {
...@@ -2436,24 +2424,24 @@ static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstE...@@ -2436,24 +2424,24 @@ static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstE
2436 }2424 }
2437 case TypeTableEntryIdArray:2425 case TypeTableEntryIdArray:
2438 {2426 {
2439 TypeTableEntry *child_type = type_entry->data.array.child_type;2427 TypeTableEntry *child_type = canon_type->data.array.child_type;
2440 uint64_t len = type_entry->data.array.len;2428 uint64_t len = canon_type->data.array.len;
2441 LLVMValueRef *values = allocate<LLVMValueRef>(len);2429 LLVMValueRef *values = allocate<LLVMValueRef>(len);
2442 for (uint64_t i = 0; i < len; i += 1) {2430 for (uint64_t i = 0; i < len; i += 1) {
2443 ConstExprValue *elem_value = &const_val->data.x_array.elements[i];2431 ConstExprValue *elem_value = &const_val->data.x_array.elements[i];
2444 values[i] = gen_const_val(g, child_type, elem_value);2432 values[i] = gen_const_val(g, elem_value);
2445 }2433 }
2446 return LLVMConstArray(child_type->type_ref, values, len);2434 return LLVMConstArray(child_type->type_ref, values, len);
2447 }2435 }
2448 case TypeTableEntryIdEnum:2436 case TypeTableEntryIdEnum:
2449 {2437 {
2450 LLVMTypeRef tag_type_ref = type_entry->data.enumeration.tag_type->type_ref;2438 LLVMTypeRef tag_type_ref = canon_type->data.enumeration.tag_type->type_ref;
2451 LLVMValueRef tag_value = LLVMConstInt(tag_type_ref, const_val->data.x_enum.tag, false);2439 LLVMValueRef tag_value = LLVMConstInt(tag_type_ref, const_val->data.x_enum.tag, false);
2452 if (type_entry->data.enumeration.gen_field_count == 0) {2440 if (canon_type->data.enumeration.gen_field_count == 0) {
2453 return tag_value;2441 return tag_value;
2454 } else {2442 } else {
2455 TypeTableEntry *union_type = type_entry->data.enumeration.union_type;2443 TypeTableEntry *union_type = canon_type->data.enumeration.union_type;
2456 TypeEnumField *enum_field = &type_entry->data.enumeration.fields[const_val->data.x_enum.tag];2444 TypeEnumField *enum_field = &canon_type->data.enumeration.fields[const_val->data.x_enum.tag];
2457 assert(enum_field->value == const_val->data.x_enum.tag);2445 assert(enum_field->value == const_val->data.x_enum.tag);
2458 LLVMValueRef union_value;2446 LLVMValueRef union_value;
2459 if (type_has_bits(enum_field->type_entry)) {2447 if (type_has_bits(enum_field->type_entry)) {
...@@ -2463,8 +2451,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstE...@@ -2463,8 +2451,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstE
2463 enum_field->type_entry->type_ref);2451 enum_field->type_entry->type_ref);
2464 uint64_t pad_bytes = union_type_bytes - field_type_bytes;2452 uint64_t pad_bytes = union_type_bytes - field_type_bytes;
24652453
2466 LLVMValueRef correctly_typed_value = gen_const_val(g, enum_field->type_entry,2454 LLVMValueRef correctly_typed_value = gen_const_val(g, const_val->data.x_enum.payload);
2467 const_val->data.x_enum.payload);
2468 if (pad_bytes == 0) {2455 if (pad_bytes == 0) {
2469 union_value = correctly_typed_value;2456 union_value = correctly_typed_value;
2470 } else {2457 } else {
...@@ -2481,29 +2468,31 @@ static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstE...@@ -2481,29 +2468,31 @@ static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstE
2481 tag_value,2468 tag_value,
2482 union_value,2469 union_value,
2483 };2470 };
2484 return LLVMConstNamedStruct(type_entry->type_ref, fields, 2);2471 return LLVMConstNamedStruct(canon_type->type_ref, fields, 2);
2485 }2472 }
2486 }2473 }
2487 case TypeTableEntryIdFn:2474 case TypeTableEntryIdFn:
2488 return fn_llvm_value(g, const_val->data.x_fn);2475 return fn_llvm_value(g, const_val->data.x_fn);
2489 case TypeTableEntryIdPointer:2476 case TypeTableEntryIdPointer:
2490 {2477 {
2491 TypeTableEntry *child_type = type_entry->data.pointer.child_type;2478 render_const_val_global(g, const_val);
2492
2493 render_const_val_global(g, type_entry, const_val);
2494 size_t index = const_val->data.x_ptr.index;2479 size_t index = const_val->data.x_ptr.index;
2495 if (index == SIZE_MAX) {2480 if (index == SIZE_MAX) {
2496 render_const_val(g, child_type, const_val->data.x_ptr.base_ptr);2481 render_const_val(g, const_val->data.x_ptr.base_ptr);
2497 render_const_val_global(g, child_type, const_val->data.x_ptr.base_ptr);2482 render_const_val_global(g, const_val->data.x_ptr.base_ptr);
2498 const_val->llvm_value = const_val->data.x_ptr.base_ptr->llvm_global;2483 ConstExprValue *other_val = const_val->data.x_ptr.base_ptr;
2499 render_const_val_global(g, type_entry, const_val);2484 if (other_val->type == const_val->type->data.pointer.child_type) {
2485 const_val->llvm_value = other_val->llvm_global;
2486 } else {
2487 const_val->llvm_value = LLVMConstBitCast(other_val->llvm_global, const_val->type->type_ref);
2488 }
2489 render_const_val_global(g, const_val);
2500 return const_val->llvm_value;2490 return const_val->llvm_value;
2501 } else {2491 } else {
2502 ConstExprValue *array_const_val = const_val->data.x_ptr.base_ptr;2492 ConstExprValue *array_const_val = const_val->data.x_ptr.base_ptr;
2503 TypeTableEntry *array_type = get_array_type(g, child_type,2493 assert(array_const_val->type->id == TypeTableEntryIdArray);
2504 array_const_val->data.x_array.size);2494 render_const_val(g, array_const_val);
2505 render_const_val(g, array_type, array_const_val);2495 render_const_val_global(g, array_const_val);
2506 render_const_val_global(g, array_type, array_const_val);
2507 TypeTableEntry *usize = g->builtin_types.entry_usize;2496 TypeTableEntry *usize = g->builtin_types.entry_usize;
2508 LLVMValueRef indices[] = {2497 LLVMValueRef indices[] = {
2509 LLVMConstNull(usize->type_ref),2498 LLVMConstNull(usize->type_ref),
...@@ -2511,13 +2500,13 @@ static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstE...@@ -2511,13 +2500,13 @@ static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstE
2511 };2500 };
2512 LLVMValueRef ptr_val = LLVMConstInBoundsGEP(array_const_val->llvm_global, indices, 2);2501 LLVMValueRef ptr_val = LLVMConstInBoundsGEP(array_const_val->llvm_global, indices, 2);
2513 const_val->llvm_value = ptr_val;2502 const_val->llvm_value = ptr_val;
2514 render_const_val_global(g, type_entry, const_val);2503 render_const_val_global(g, const_val);
2515 return ptr_val;2504 return ptr_val;
2516 }2505 }
2517 }2506 }
2518 case TypeTableEntryIdErrorUnion:2507 case TypeTableEntryIdErrorUnion:
2519 {2508 {
2520 TypeTableEntry *child_type = type_entry->data.error.child_type;2509 TypeTableEntry *child_type = canon_type->data.error.child_type;
2521 if (!type_has_bits(child_type)) {2510 if (!type_has_bits(child_type)) {
2522 uint64_t value = const_val->data.x_err_union.err ? const_val->data.x_err_union.err->value : 0;2511 uint64_t value = const_val->data.x_err_union.err ? const_val->data.x_err_union.err->value : 0;
2523 return LLVMConstInt(g->err_tag_type->type_ref, value, false);2512 return LLVMConstInt(g->err_tag_type->type_ref, value, false);
...@@ -2529,7 +2518,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstE...@@ -2529,7 +2518,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstE
2529 err_payload_value = LLVMConstNull(child_type->type_ref);2518 err_payload_value = LLVMConstNull(child_type->type_ref);
2530 } else {2519 } else {
2531 err_tag_value = LLVMConstNull(g->err_tag_type->type_ref);2520 err_tag_value = LLVMConstNull(g->err_tag_type->type_ref);
2532 err_payload_value = gen_const_val(g, child_type, const_val->data.x_err_union.payload);2521 err_payload_value = gen_const_val(g, const_val->data.x_err_union.payload);
2533 }2522 }
2534 LLVMValueRef fields[] = {2523 LLVMValueRef fields[] = {
2535 err_tag_value,2524 err_tag_value,
...@@ -2540,8 +2529,6 @@ static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstE...@@ -2540,8 +2529,6 @@ static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstE
2540 }2529 }
2541 case TypeTableEntryIdVoid:2530 case TypeTableEntryIdVoid:
2542 return nullptr;2531 return nullptr;
2543 case TypeTableEntryIdEnumTag:
2544 return gen_const_val(g, type_entry->data.enum_tag.int_type, const_val);
2545 case TypeTableEntryIdInvalid:2532 case TypeTableEntryIdInvalid:
2546 case TypeTableEntryIdMetaType:2533 case TypeTableEntryIdMetaType:
2547 case TypeTableEntryIdUnreachable:2534 case TypeTableEntryIdUnreachable:
...@@ -2559,17 +2546,17 @@ static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstE...@@ -2559,17 +2546,17 @@ static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstE
2559 zig_unreachable();2546 zig_unreachable();
2560}2547}
25612548
2562static void render_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstExprValue *const_val) {2549static void render_const_val(CodeGen *g, ConstExprValue *const_val) {
2563 if (!const_val->llvm_value)2550 if (!const_val->llvm_value)
2564 const_val->llvm_value = gen_const_val(g, type_entry, const_val);2551 const_val->llvm_value = gen_const_val(g, const_val);
25652552
2566 if (const_val->llvm_global)2553 if (const_val->llvm_global)
2567 LLVMSetInitializer(const_val->llvm_global, const_val->llvm_value);2554 LLVMSetInitializer(const_val->llvm_global, const_val->llvm_value);
2568}2555}
25692556
2570static void render_const_val_global(CodeGen *g, TypeTableEntry *type_entry, ConstExprValue *const_val) {2557static void render_const_val_global(CodeGen *g, ConstExprValue *const_val) {
2571 if (!const_val->llvm_global) {2558 if (!const_val->llvm_global) {
2572 LLVMValueRef global_value = LLVMAddGlobal(g->module, type_entry->type_ref, "");2559 LLVMValueRef global_value = LLVMAddGlobal(g->module, const_val->type->type_ref, "");
2573 LLVMSetLinkage(global_value, LLVMInternalLinkage);2560 LLVMSetLinkage(global_value, LLVMInternalLinkage);
2574 LLVMSetGlobalConstant(global_value, true);2561 LLVMSetGlobalConstant(global_value, true);
2575 LLVMSetUnnamedAddr(global_value, true);2562 LLVMSetUnnamedAddr(global_value, true);
...@@ -2717,9 +2704,9 @@ static void do_code_gen(CodeGen *g) {...@@ -2717,9 +2704,9 @@ static void do_code_gen(CodeGen *g) {
2717 for (size_t i = 0; i < g->global_vars.length; i += 1) {2704 for (size_t i = 0; i < g->global_vars.length; i += 1) {
2718 VariableTableEntry *var = g->global_vars.at(i);2705 VariableTableEntry *var = g->global_vars.at(i);
27192706
2720 if (var->type->id == TypeTableEntryIdNumLitFloat) {2707 if (var->value.type->id == TypeTableEntryIdNumLitFloat) {
2721 // Generate debug info for it but that's it.2708 // Generate debug info for it but that's it.
2722 ConstExprValue *const_val = var->value;2709 ConstExprValue *const_val = &var->value;
2723 assert(const_val->special != ConstValSpecialRuntime);2710 assert(const_val->special != ConstValSpecialRuntime);
2724 TypeTableEntry *var_type = g->builtin_types.entry_f64;2711 TypeTableEntry *var_type = g->builtin_types.entry_f64;
2725 LLVMValueRef init_val = LLVMConstReal(var_type->type_ref, const_val->data.x_bignum.data.x_float);2712 LLVMValueRef init_val = LLVMConstReal(var_type->type_ref, const_val->data.x_bignum.data.x_float);
...@@ -2727,9 +2714,9 @@ static void do_code_gen(CodeGen *g) {...@@ -2727,9 +2714,9 @@ static void do_code_gen(CodeGen *g) {
2727 continue;2714 continue;
2728 }2715 }
27292716
2730 if (var->type->id == TypeTableEntryIdNumLitInt) {2717 if (var->value.type->id == TypeTableEntryIdNumLitInt) {
2731 // Generate debug info for it but that's it.2718 // Generate debug info for it but that's it.
2732 ConstExprValue *const_val = var->value;2719 ConstExprValue *const_val = &var->value;
2733 assert(const_val->special != ConstValSpecialRuntime);2720 assert(const_val->special != ConstValSpecialRuntime);
2734 TypeTableEntry *var_type = const_val->data.x_bignum.is_negative ?2721 TypeTableEntry *var_type = const_val->data.x_bignum.is_negative ?
2735 g->builtin_types.entry_isize : g->builtin_types.entry_usize;2722 g->builtin_types.entry_isize : g->builtin_types.entry_usize;
...@@ -2739,27 +2726,26 @@ static void do_code_gen(CodeGen *g) {...@@ -2739,27 +2726,26 @@ static void do_code_gen(CodeGen *g) {
2739 continue;2726 continue;
2740 }2727 }
27412728
2742 if (!type_has_bits(var->type)) {2729 if (!type_has_bits(var->value.type))
2743 continue;2730 continue;
2744 }
27452731
2746 assert(var->decl_node);2732 assert(var->decl_node);
2747 assert(var->decl_node->type == NodeTypeVariableDeclaration);2733 assert(var->decl_node->type == NodeTypeVariableDeclaration);
27482734
2749 LLVMValueRef global_value;2735 LLVMValueRef global_value;
2750 if (var->is_extern) {2736 if (var->is_extern) {
2751 global_value = LLVMAddGlobal(g->module, var->type->type_ref, buf_ptr(&var->name));2737 global_value = LLVMAddGlobal(g->module, var->value.type->type_ref, buf_ptr(&var->name));
27522738
2753 // TODO debug info for the extern variable2739 // TODO debug info for the extern variable
27542740
2755 LLVMSetLinkage(global_value, LLVMExternalLinkage);2741 LLVMSetLinkage(global_value, LLVMExternalLinkage);
2756 } else {2742 } else {
2757 render_const_val(g, var->type, var->value);2743 render_const_val(g, &var->value);
2758 render_const_val_global(g, var->type, var->value);2744 render_const_val_global(g, &var->value);
2759 global_value = var->value->llvm_global;2745 global_value = var->value.llvm_global;
2760 // TODO debug info for function pointers2746 // TODO debug info for function pointers
2761 if (var->gen_is_const && var->type->id != TypeTableEntryIdFn) {2747 if (var->gen_is_const && var->value.type->id != TypeTableEntryIdFn) {
2762 gen_global_var(g, var, var->value->llvm_value, var->type);2748 gen_global_var(g, var, var->value.llvm_value, var->value.type);
2763 }2749 }
2764 }2750 }
27652751
...@@ -2890,15 +2876,15 @@ static void do_code_gen(CodeGen *g) {...@@ -2890,15 +2876,15 @@ static void do_code_gen(CodeGen *g) {
2890 for (size_t alloca_i = 0; alloca_i < fn_table_entry->alloca_list.length; alloca_i += 1) {2876 for (size_t alloca_i = 0; alloca_i < fn_table_entry->alloca_list.length; alloca_i += 1) {
2891 IrInstruction *instruction = fn_table_entry->alloca_list.at(alloca_i);2877 IrInstruction *instruction = fn_table_entry->alloca_list.at(alloca_i);
2892 LLVMValueRef *slot;2878 LLVMValueRef *slot;
2893 TypeTableEntry *slot_type = instruction->type_entry;2879 TypeTableEntry *slot_type = instruction->value.type;
2894 if (instruction->id == IrInstructionIdCast) {2880 if (instruction->id == IrInstructionIdCast) {
2895 IrInstructionCast *cast_instruction = (IrInstructionCast *)instruction;2881 IrInstructionCast *cast_instruction = (IrInstructionCast *)instruction;
2896 slot = &cast_instruction->tmp_ptr;2882 slot = &cast_instruction->tmp_ptr;
2897 } else if (instruction->id == IrInstructionIdRef) {2883 } else if (instruction->id == IrInstructionIdRef) {
2898 IrInstructionRef *ref_instruction = (IrInstructionRef *)instruction;2884 IrInstructionRef *ref_instruction = (IrInstructionRef *)instruction;
2899 slot = &ref_instruction->tmp_ptr;2885 slot = &ref_instruction->tmp_ptr;
2900 assert(instruction->type_entry->id == TypeTableEntryIdPointer);2886 assert(instruction->value.type->id == TypeTableEntryIdPointer);
2901 slot_type = instruction->type_entry->data.pointer.child_type;2887 slot_type = instruction->value.type->data.pointer.child_type;
2902 } else if (instruction->id == IrInstructionIdContainerInitList) {2888 } else if (instruction->id == IrInstructionIdContainerInitList) {
2903 IrInstructionContainerInitList *container_init_list_instruction = (IrInstructionContainerInitList *)instruction;2889 IrInstructionContainerInitList *container_init_list_instruction = (IrInstructionContainerInitList *)instruction;
2904 slot = &container_init_list_instruction->tmp_ptr;2890 slot = &container_init_list_instruction->tmp_ptr;
...@@ -2938,33 +2924,33 @@ static void do_code_gen(CodeGen *g) {...@@ -2938,33 +2924,33 @@ static void do_code_gen(CodeGen *g) {
2938 for (size_t var_i = 0; var_i < fn_table_entry->variable_list.length; var_i += 1) {2924 for (size_t var_i = 0; var_i < fn_table_entry->variable_list.length; var_i += 1) {
2939 VariableTableEntry *var = fn_table_entry->variable_list.at(var_i);2925 VariableTableEntry *var = fn_table_entry->variable_list.at(var_i);
29402926
2941 if (!type_has_bits(var->type)) {2927 if (!type_has_bits(var->value.type)) {
2942 continue;2928 continue;
2943 }2929 }
2944 if (ir_get_var_is_comptime(var))2930 if (ir_get_var_is_comptime(var))
2945 continue;2931 continue;
29462932
2947 if (var->src_arg_index == SIZE_MAX) {2933 if (var->src_arg_index == SIZE_MAX) {
2948 var->value_ref = LLVMBuildAlloca(g->builder, var->type->type_ref, buf_ptr(&var->name));2934 var->value_ref = LLVMBuildAlloca(g->builder, var->value.type->type_ref, buf_ptr(&var->name));
29492935
29502936
2951 unsigned align_bytes = ZigLLVMGetPrefTypeAlignment(g->target_data_ref, var->type->type_ref);2937 unsigned align_bytes = ZigLLVMGetPrefTypeAlignment(g->target_data_ref, var->value.type->type_ref);
2952 LLVMSetAlignment(var->value_ref, align_bytes);2938 LLVMSetAlignment(var->value_ref, align_bytes);
29532939
2954 var->di_loc_var = ZigLLVMCreateAutoVariable(g->dbuilder, get_di_scope(g, var->parent_scope),2940 var->di_loc_var = ZigLLVMCreateAutoVariable(g->dbuilder, get_di_scope(g, var->parent_scope),
2955 buf_ptr(&var->name), import->di_file, var->decl_node->line + 1,2941 buf_ptr(&var->name), import->di_file, var->decl_node->line + 1,
2956 var->type->di_type, !g->strip_debug_symbols, 0);2942 var->value.type->di_type, !g->strip_debug_symbols, 0);
29572943
2958 } else {2944 } else {
2959 assert(var->gen_arg_index != SIZE_MAX);2945 assert(var->gen_arg_index != SIZE_MAX);
2960 TypeTableEntry *gen_type;2946 TypeTableEntry *gen_type;
2961 if (handle_is_ptr(var->type)) {2947 if (handle_is_ptr(var->value.type)) {
2962 gen_type = fn_table_entry->type_entry->data.fn.gen_param_info[var->src_arg_index].type;2948 gen_type = fn_table_entry->type_entry->data.fn.gen_param_info[var->src_arg_index].type;
2963 var->value_ref = LLVMGetParam(fn, var->gen_arg_index);2949 var->value_ref = LLVMGetParam(fn, var->gen_arg_index);
2964 } else {2950 } else {
2965 gen_type = var->type;2951 gen_type = var->value.type;
2966 var->value_ref = LLVMBuildAlloca(g->builder, var->type->type_ref, buf_ptr(&var->name));2952 var->value_ref = LLVMBuildAlloca(g->builder, var->value.type->type_ref, buf_ptr(&var->name));
2967 unsigned align_bytes = ZigLLVMGetPrefTypeAlignment(g->target_data_ref, var->type->type_ref);2953 unsigned align_bytes = ZigLLVMGetPrefTypeAlignment(g->target_data_ref, var->value.type->type_ref);
2968 LLVMSetAlignment(var->value_ref, align_bytes);2954 LLVMSetAlignment(var->value_ref, align_bytes);
2969 }2955 }
2970 var->di_loc_var = ZigLLVMCreateParameterVariable(g->dbuilder, get_di_scope(g, var->parent_scope),2956 var->di_loc_var = ZigLLVMCreateParameterVariable(g->dbuilder, get_di_scope(g, var->parent_scope),
...@@ -2991,7 +2977,7 @@ static void do_code_gen(CodeGen *g) {...@@ -2991,7 +2977,7 @@ static void do_code_gen(CodeGen *g) {
2991 assert(variable);2977 assert(variable);
2992 assert(variable->value_ref);2978 assert(variable->value_ref);
29932979
2994 if (!handle_is_ptr(variable->type)) {2980 if (!handle_is_ptr(variable->value.type)) {
2995 clear_debug_source_node(g);2981 clear_debug_source_node(g);
2996 LLVMBuildStore(g->builder, LLVMGetParam(fn, variable->gen_arg_index), variable->value_ref);2982 LLVMBuildStore(g->builder, LLVMGetParam(fn, variable->gen_arg_index), variable->value_ref);
2997 }2983 }
...@@ -3627,7 +3613,7 @@ static void init(CodeGen *g, Buf *source_path) {...@@ -3627,7 +3613,7 @@ static void init(CodeGen *g, Buf *source_path) {
3627 define_builtin_fns(g);3613 define_builtin_fns(g);
36283614
3629 g->invalid_instruction = allocate<IrInstruction>(1);3615 g->invalid_instruction = allocate<IrInstruction>(1);
3630 g->invalid_instruction->type_entry = g->builtin_types.entry_invalid;3616 g->invalid_instruction->value.type = g->builtin_types.entry_invalid;
3631}3617}
36323618
3633void codegen_parseh(CodeGen *g, Buf *src_dirname, Buf *src_basename, Buf *source_code) {3619void codegen_parseh(CodeGen *g, Buf *src_dirname, Buf *src_basename, Buf *source_code) {
src/ir.cpp+620-588
...@@ -48,6 +48,7 @@ static IrInstruction *ir_gen_node(IrBuilder *irb, AstNode *node, Scope *scope);...@@ -48,6 +48,7 @@ static IrInstruction *ir_gen_node(IrBuilder *irb, AstNode *node, Scope *scope);
48static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, Scope *scope,48static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, Scope *scope,
49 LValPurpose lval);49 LValPurpose lval);
50static TypeTableEntry *ir_analyze_instruction(IrAnalyze *ira, IrInstruction *instruction);50static TypeTableEntry *ir_analyze_instruction(IrAnalyze *ira, IrInstruction *instruction);
51static IrInstruction *ir_implicit_cast(IrAnalyze *ira, IrInstruction *value, TypeTableEntry *expected_type);
5152
52ConstExprValue *const_ptr_pointee(ConstExprValue *const_val) {53ConstExprValue *const_ptr_pointee(ConstExprValue *const_val) {
53 assert(const_val->special == ConstValSpecialStatic);54 assert(const_val->special == ConstValSpecialStatic);
...@@ -93,7 +94,7 @@ static Buf *exec_c_import_buf(IrExecutable *exec) {...@@ -93,7 +94,7 @@ static Buf *exec_c_import_buf(IrExecutable *exec) {
93}94}
9495
95static bool instr_is_comptime(IrInstruction *instruction) {96static bool instr_is_comptime(IrInstruction *instruction) {
96 return instruction->static_value.special != ConstValSpecialRuntime;97 return instruction->value.special != ConstValSpecialRuntime;
97}98}
9899
99static void ir_link_new_instruction(IrInstruction *new_instruction, IrInstruction *old_instruction) {100static void ir_link_new_instruction(IrInstruction *new_instruction, IrInstruction *old_instruction) {
...@@ -450,6 +451,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionInitEnum *) {...@@ -450,6 +451,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionInitEnum *) {
450 return IrInstructionIdInitEnum;451 return IrInstructionIdInitEnum;
451}452}
452453
454static constexpr IrInstructionId ir_instruction_id(IrInstructionPointerReinterpret *) {
455 return IrInstructionIdPointerReinterpret;
456}
457
453template<typename T>458template<typename T>
454static T *ir_create_instruction(IrExecutable *exec, Scope *scope, AstNode *source_node) {459static T *ir_create_instruction(IrExecutable *exec, Scope *scope, AstNode *source_node) {
455 T *special_instruction = allocate<T>(1);460 T *special_instruction = allocate<T>(1);
...@@ -485,8 +490,8 @@ static IrInstruction *ir_build_cond_br(IrBuilder *irb, Scope *scope, AstNode *so...@@ -485,8 +490,8 @@ static IrInstruction *ir_build_cond_br(IrBuilder *irb, Scope *scope, AstNode *so
485 IrBasicBlock *then_block, IrBasicBlock *else_block, IrInstruction *is_comptime)490 IrBasicBlock *then_block, IrBasicBlock *else_block, IrInstruction *is_comptime)
486{491{
487 IrInstructionCondBr *cond_br_instruction = ir_build_instruction<IrInstructionCondBr>(irb, scope, source_node);492 IrInstructionCondBr *cond_br_instruction = ir_build_instruction<IrInstructionCondBr>(irb, scope, source_node);
488 cond_br_instruction->base.type_entry = irb->codegen->builtin_types.entry_unreachable;493 cond_br_instruction->base.value.type = irb->codegen->builtin_types.entry_unreachable;
489 cond_br_instruction->base.static_value.special = ConstValSpecialStatic;494 cond_br_instruction->base.value.special = ConstValSpecialStatic;
490 cond_br_instruction->condition = condition;495 cond_br_instruction->condition = condition;
491 cond_br_instruction->then_block = then_block;496 cond_br_instruction->then_block = then_block;
492 cond_br_instruction->else_block = else_block;497 cond_br_instruction->else_block = else_block;
...@@ -511,8 +516,8 @@ static IrInstruction *ir_build_cond_br_from(IrBuilder *irb, IrInstruction *old_i...@@ -511,8 +516,8 @@ static IrInstruction *ir_build_cond_br_from(IrBuilder *irb, IrInstruction *old_i
511516
512static IrInstruction *ir_build_return(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *return_value) {517static IrInstruction *ir_build_return(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *return_value) {
513 IrInstructionReturn *return_instruction = ir_build_instruction<IrInstructionReturn>(irb, scope, source_node);518 IrInstructionReturn *return_instruction = ir_build_instruction<IrInstructionReturn>(irb, scope, source_node);
514 return_instruction->base.type_entry = irb->codegen->builtin_types.entry_unreachable;519 return_instruction->base.value.type = irb->codegen->builtin_types.entry_unreachable;
515 return_instruction->base.static_value.special = ConstValSpecialStatic;520 return_instruction->base.value.special = ConstValSpecialStatic;
516 return_instruction->value = return_value;521 return_instruction->value = return_value;
517522
518 ir_ref_instruction(return_value);523 ir_ref_instruction(return_value);
...@@ -533,55 +538,55 @@ static IrInstruction *ir_create_const(IrBuilder *irb, Scope *scope, AstNode *sou...@@ -533,55 +538,55 @@ static IrInstruction *ir_create_const(IrBuilder *irb, Scope *scope, AstNode *sou
533{538{
534 assert(type_entry);539 assert(type_entry);
535 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(irb->exec, scope, source_node);540 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(irb->exec, scope, source_node);
536 const_instruction->base.type_entry = type_entry;541 const_instruction->base.value.type = type_entry;
537 const_instruction->base.static_value.special = ConstValSpecialStatic;542 const_instruction->base.value.special = ConstValSpecialStatic;
538 const_instruction->base.static_value.depends_on_compile_var = depends_on_compile_var;543 const_instruction->base.value.depends_on_compile_var = depends_on_compile_var;
539 return &const_instruction->base;544 return &const_instruction->base;
540}545}
541546
542static IrInstruction *ir_build_const_void(IrBuilder *irb, Scope *scope, AstNode *source_node) {547static IrInstruction *ir_build_const_void(IrBuilder *irb, Scope *scope, AstNode *source_node) {
543 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);548 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);
544 const_instruction->base.type_entry = irb->codegen->builtin_types.entry_void;549 const_instruction->base.value.type = irb->codegen->builtin_types.entry_void;
545 const_instruction->base.static_value.special = ConstValSpecialStatic;550 const_instruction->base.value.special = ConstValSpecialStatic;
546 return &const_instruction->base;551 return &const_instruction->base;
547}552}
548553
549static IrInstruction *ir_build_const_undefined(IrBuilder *irb, Scope *scope, AstNode *source_node) {554static IrInstruction *ir_build_const_undefined(IrBuilder *irb, Scope *scope, AstNode *source_node) {
550 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);555 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);
551 const_instruction->base.static_value.special = ConstValSpecialUndef;556 const_instruction->base.value.special = ConstValSpecialUndef;
552 const_instruction->base.type_entry = irb->codegen->builtin_types.entry_undef;557 const_instruction->base.value.type = irb->codegen->builtin_types.entry_undef;
553 return &const_instruction->base;558 return &const_instruction->base;
554}559}
555560
556static IrInstruction *ir_build_const_uint(IrBuilder *irb, Scope *scope, AstNode *source_node, uint64_t value) {561static IrInstruction *ir_build_const_uint(IrBuilder *irb, Scope *scope, AstNode *source_node, uint64_t value) {
557 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);562 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);
558 const_instruction->base.type_entry = irb->codegen->builtin_types.entry_num_lit_int;563 const_instruction->base.value.type = irb->codegen->builtin_types.entry_num_lit_int;
559 const_instruction->base.static_value.special = ConstValSpecialStatic;564 const_instruction->base.value.special = ConstValSpecialStatic;
560 bignum_init_unsigned(&const_instruction->base.static_value.data.x_bignum, value);565 bignum_init_unsigned(&const_instruction->base.value.data.x_bignum, value);
561 return &const_instruction->base;566 return &const_instruction->base;
562}567}
563568
564static IrInstruction *ir_build_const_bignum(IrBuilder *irb, Scope *scope, AstNode *source_node, BigNum *bignum) {569static IrInstruction *ir_build_const_bignum(IrBuilder *irb, Scope *scope, AstNode *source_node, BigNum *bignum) {
565 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);570 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);
566 const_instruction->base.type_entry = (bignum->kind == BigNumKindInt) ?571 const_instruction->base.value.type = (bignum->kind == BigNumKindInt) ?
567 irb->codegen->builtin_types.entry_num_lit_int : irb->codegen->builtin_types.entry_num_lit_float;572 irb->codegen->builtin_types.entry_num_lit_int : irb->codegen->builtin_types.entry_num_lit_float;
568 const_instruction->base.static_value.special = ConstValSpecialStatic;573 const_instruction->base.value.special = ConstValSpecialStatic;
569 const_instruction->base.static_value.data.x_bignum = *bignum;574 const_instruction->base.value.data.x_bignum = *bignum;
570 return &const_instruction->base;575 return &const_instruction->base;
571}576}
572577
573static IrInstruction *ir_build_const_null(IrBuilder *irb, Scope *scope, AstNode *source_node) {578static IrInstruction *ir_build_const_null(IrBuilder *irb, Scope *scope, AstNode *source_node) {
574 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);579 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);
575 const_instruction->base.type_entry = irb->codegen->builtin_types.entry_null;580 const_instruction->base.value.type = irb->codegen->builtin_types.entry_null;
576 const_instruction->base.static_value.special = ConstValSpecialStatic;581 const_instruction->base.value.special = ConstValSpecialStatic;
577 return &const_instruction->base;582 return &const_instruction->base;
578}583}
579584
580static IrInstruction *ir_build_const_usize(IrBuilder *irb, Scope *scope, AstNode *source_node, uint64_t value) {585static IrInstruction *ir_build_const_usize(IrBuilder *irb, Scope *scope, AstNode *source_node, uint64_t value) {
581 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);586 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);
582 const_instruction->base.type_entry = irb->codegen->builtin_types.entry_usize;587 const_instruction->base.value.type = irb->codegen->builtin_types.entry_usize;
583 const_instruction->base.static_value.special = ConstValSpecialStatic;588 const_instruction->base.value.special = ConstValSpecialStatic;
584 bignum_init_unsigned(&const_instruction->base.static_value.data.x_bignum, value);589 bignum_init_unsigned(&const_instruction->base.value.data.x_bignum, value);
585 return &const_instruction->base;590 return &const_instruction->base;
586}591}
587592
...@@ -589,9 +594,9 @@ static IrInstruction *ir_create_const_type(IrBuilder *irb, Scope *scope, AstNode...@@ -589,9 +594,9 @@ static IrInstruction *ir_create_const_type(IrBuilder *irb, Scope *scope, AstNode
589 TypeTableEntry *type_entry)594 TypeTableEntry *type_entry)
590{595{
591 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(irb->exec, scope, source_node);596 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(irb->exec, scope, source_node);
592 const_instruction->base.type_entry = irb->codegen->builtin_types.entry_type;597 const_instruction->base.value.type = irb->codegen->builtin_types.entry_type;
593 const_instruction->base.static_value.special = ConstValSpecialStatic;598 const_instruction->base.value.special = ConstValSpecialStatic;
594 const_instruction->base.static_value.data.x_type = type_entry;599 const_instruction->base.value.data.x_type = type_entry;
595 return &const_instruction->base;600 return &const_instruction->base;
596}601}
597602
...@@ -605,17 +610,17 @@ static IrInstruction *ir_build_const_type(IrBuilder *irb, Scope *scope, AstNode...@@ -605,17 +610,17 @@ static IrInstruction *ir_build_const_type(IrBuilder *irb, Scope *scope, AstNode
605610
606static IrInstruction *ir_build_const_fn(IrBuilder *irb, Scope *scope, AstNode *source_node, FnTableEntry *fn_entry) {611static IrInstruction *ir_build_const_fn(IrBuilder *irb, Scope *scope, AstNode *source_node, FnTableEntry *fn_entry) {
607 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);612 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);
608 const_instruction->base.type_entry = fn_entry->type_entry;613 const_instruction->base.value.type = fn_entry->type_entry;
609 const_instruction->base.static_value.special = ConstValSpecialStatic;614 const_instruction->base.value.special = ConstValSpecialStatic;
610 const_instruction->base.static_value.data.x_fn = fn_entry;615 const_instruction->base.value.data.x_fn = fn_entry;
611 return &const_instruction->base;616 return &const_instruction->base;
612}617}
613618
614static IrInstruction *ir_build_const_import(IrBuilder *irb, Scope *scope, AstNode *source_node, ImportTableEntry *import) {619static IrInstruction *ir_build_const_import(IrBuilder *irb, Scope *scope, AstNode *source_node, ImportTableEntry *import) {
615 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);620 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);
616 const_instruction->base.type_entry = irb->codegen->builtin_types.entry_namespace;621 const_instruction->base.value.type = irb->codegen->builtin_types.entry_namespace;
617 const_instruction->base.static_value.special = ConstValSpecialStatic;622 const_instruction->base.value.special = ConstValSpecialStatic;
618 const_instruction->base.static_value.data.x_import = import;623 const_instruction->base.value.data.x_import = import;
619 return &const_instruction->base;624 return &const_instruction->base;
620}625}
621626
...@@ -623,17 +628,17 @@ static IrInstruction *ir_build_const_scope(IrBuilder *irb, Scope *parent_scope,...@@ -623,17 +628,17 @@ static IrInstruction *ir_build_const_scope(IrBuilder *irb, Scope *parent_scope,
623 Scope *target_scope)628 Scope *target_scope)
624{629{
625 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, parent_scope, source_node);630 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, parent_scope, source_node);
626 const_instruction->base.type_entry = irb->codegen->builtin_types.entry_block;631 const_instruction->base.value.type = irb->codegen->builtin_types.entry_block;
627 const_instruction->base.static_value.special = ConstValSpecialStatic;632 const_instruction->base.value.special = ConstValSpecialStatic;
628 const_instruction->base.static_value.data.x_block = target_scope;633 const_instruction->base.value.data.x_block = target_scope;
629 return &const_instruction->base;634 return &const_instruction->base;
630}635}
631636
632static IrInstruction *ir_build_const_bool(IrBuilder *irb, Scope *scope, AstNode *source_node, bool value) {637static IrInstruction *ir_build_const_bool(IrBuilder *irb, Scope *scope, AstNode *source_node, bool value) {
633 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);638 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);
634 const_instruction->base.type_entry = irb->codegen->builtin_types.entry_bool;639 const_instruction->base.value.type = irb->codegen->builtin_types.entry_bool;
635 const_instruction->base.static_value.special = ConstValSpecialStatic;640 const_instruction->base.value.special = ConstValSpecialStatic;
636 const_instruction->base.static_value.data.x_bool = value;641 const_instruction->base.value.data.x_bool = value;
637 return &const_instruction->base;642 return &const_instruction->base;
638}643}
639644
...@@ -641,21 +646,17 @@ static IrInstruction *ir_build_const_bound_fn(IrBuilder *irb, Scope *scope, AstN...@@ -641,21 +646,17 @@ static IrInstruction *ir_build_const_bound_fn(IrBuilder *irb, Scope *scope, AstN
641 FnTableEntry *fn_entry, IrInstruction *first_arg, bool depends_on_compile_var)646 FnTableEntry *fn_entry, IrInstruction *first_arg, bool depends_on_compile_var)
642{647{
643 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);648 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);
644 const_instruction->base.type_entry = get_bound_fn_type(irb->codegen, fn_entry);649 const_instruction->base.value.type = get_bound_fn_type(irb->codegen, fn_entry);
645 const_instruction->base.static_value.special = ConstValSpecialStatic;650 const_instruction->base.value.special = ConstValSpecialStatic;
646 const_instruction->base.static_value.depends_on_compile_var = depends_on_compile_var;651 const_instruction->base.value.depends_on_compile_var = depends_on_compile_var;
647 const_instruction->base.static_value.data.x_bound_fn.fn = fn_entry;652 const_instruction->base.value.data.x_bound_fn.fn = fn_entry;
648 const_instruction->base.static_value.data.x_bound_fn.first_arg = first_arg;653 const_instruction->base.value.data.x_bound_fn.first_arg = first_arg;
649 return &const_instruction->base;654 return &const_instruction->base;
650}655}
651656
652static IrInstruction *ir_create_const_str_lit(IrBuilder *irb, Scope *scope, AstNode *source_node, Buf *str) {657static IrInstruction *ir_create_const_str_lit(IrBuilder *irb, Scope *scope, AstNode *source_node, Buf *str) {
653 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(irb->exec, scope, source_node);658 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(irb->exec, scope, source_node);
654 TypeTableEntry *u8_type = irb->codegen->builtin_types.entry_u8;659 init_const_str_lit(irb->codegen, &const_instruction->base.value, str);
655 TypeTableEntry *type_entry = get_array_type(irb->codegen, u8_type, buf_len(str));
656 const_instruction->base.type_entry = type_entry;
657 ConstExprValue *const_val = &const_instruction->base.static_value;
658 init_const_str_lit(const_val, str);
659660
660 return &const_instruction->base;661 return &const_instruction->base;
661}662}
...@@ -666,32 +667,8 @@ static IrInstruction *ir_build_const_str_lit(IrBuilder *irb, Scope *scope, AstNo...@@ -666,32 +667,8 @@ static IrInstruction *ir_build_const_str_lit(IrBuilder *irb, Scope *scope, AstNo
666}667}
667668
668static IrInstruction *ir_build_const_c_str_lit(IrBuilder *irb, Scope *scope, AstNode *source_node, Buf *str) {669static IrInstruction *ir_build_const_c_str_lit(IrBuilder *irb, Scope *scope, AstNode *source_node, Buf *str) {
669 // first we build the underlying array
670 size_t len_with_null = buf_len(str) + 1;
671 ConstExprValue *array_val = allocate<ConstExprValue>(1);
672 array_val->special = ConstValSpecialStatic;
673 array_val->data.x_array.elements = allocate<ConstExprValue>(len_with_null);
674 array_val->data.x_array.size = len_with_null;
675 for (size_t i = 0; i < buf_len(str); i += 1) {
676 ConstExprValue *this_char = &array_val->data.x_array.elements[i];
677 this_char->special = ConstValSpecialStatic;
678 bignum_init_unsigned(&this_char->data.x_bignum, buf_ptr(str)[i]);
679 }
680 ConstExprValue *null_char = &array_val->data.x_array.elements[len_with_null - 1];
681 null_char->special = ConstValSpecialStatic;
682 bignum_init_unsigned(&null_char->data.x_bignum, 0);
683
684 // then make the pointer point to it
685 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);670 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);
686 TypeTableEntry *u8_type = irb->codegen->builtin_types.entry_u8;671 init_const_c_str_lit(irb->codegen, &const_instruction->base.value, str);
687 TypeTableEntry *type_entry = get_pointer_to_type(irb->codegen, u8_type, true);
688 const_instruction->base.type_entry = type_entry;
689 ConstExprValue *ptr_val = &const_instruction->base.static_value;
690 ptr_val->special = ConstValSpecialStatic;
691 ptr_val->data.x_ptr.base_ptr = array_val;
692 ptr_val->data.x_ptr.index = 0;
693 ptr_val->data.x_ptr.special = ConstPtrSpecialCStr;
694
695 return &const_instruction->base;672 return &const_instruction->base;
696}673}
697674
...@@ -870,8 +847,8 @@ static IrInstruction *ir_create_br(IrBuilder *irb, Scope *scope, AstNode *source...@@ -870,8 +847,8 @@ static IrInstruction *ir_create_br(IrBuilder *irb, Scope *scope, AstNode *source
870 IrBasicBlock *dest_block, IrInstruction *is_comptime)847 IrBasicBlock *dest_block, IrInstruction *is_comptime)
871{848{
872 IrInstructionBr *br_instruction = ir_create_instruction<IrInstructionBr>(irb->exec, scope, source_node);849 IrInstructionBr *br_instruction = ir_create_instruction<IrInstructionBr>(irb->exec, scope, source_node);
873 br_instruction->base.type_entry = irb->codegen->builtin_types.entry_unreachable;850 br_instruction->base.value.type = irb->codegen->builtin_types.entry_unreachable;
874 br_instruction->base.static_value.special = ConstValSpecialStatic;851 br_instruction->base.value.special = ConstValSpecialStatic;
875 br_instruction->dest_block = dest_block;852 br_instruction->dest_block = dest_block;
876 br_instruction->is_comptime = is_comptime;853 br_instruction->is_comptime = is_comptime;
877854
...@@ -983,8 +960,8 @@ static IrInstruction *ir_build_struct_init_from(IrBuilder *irb, IrInstruction *o...@@ -983,8 +960,8 @@ static IrInstruction *ir_build_struct_init_from(IrBuilder *irb, IrInstruction *o
983static IrInstruction *ir_build_unreachable(IrBuilder *irb, Scope *scope, AstNode *source_node) {960static IrInstruction *ir_build_unreachable(IrBuilder *irb, Scope *scope, AstNode *source_node) {
984 IrInstructionUnreachable *unreachable_instruction =961 IrInstructionUnreachable *unreachable_instruction =
985 ir_build_instruction<IrInstructionUnreachable>(irb, scope, source_node);962 ir_build_instruction<IrInstructionUnreachable>(irb, scope, source_node);
986 unreachable_instruction->base.static_value.special = ConstValSpecialStatic;963 unreachable_instruction->base.value.special = ConstValSpecialStatic;
987 unreachable_instruction->base.type_entry = irb->codegen->builtin_types.entry_unreachable;964 unreachable_instruction->base.value.type = irb->codegen->builtin_types.entry_unreachable;
988 return &unreachable_instruction->base;965 return &unreachable_instruction->base;
989}966}
990967
...@@ -998,8 +975,8 @@ static IrInstruction *ir_build_store_ptr(IrBuilder *irb, Scope *scope, AstNode *...@@ -998,8 +975,8 @@ static IrInstruction *ir_build_store_ptr(IrBuilder *irb, Scope *scope, AstNode *
998 IrInstruction *ptr, IrInstruction *value)975 IrInstruction *ptr, IrInstruction *value)
999{976{
1000 IrInstructionStorePtr *instruction = ir_build_instruction<IrInstructionStorePtr>(irb, scope, source_node);977 IrInstructionStorePtr *instruction = ir_build_instruction<IrInstructionStorePtr>(irb, scope, source_node);
1001 instruction->base.static_value.special = ConstValSpecialStatic;978 instruction->base.value.special = ConstValSpecialStatic;
1002 instruction->base.type_entry = irb->codegen->builtin_types.entry_void;979 instruction->base.value.type = irb->codegen->builtin_types.entry_void;
1003 instruction->ptr = ptr;980 instruction->ptr = ptr;
1004 instruction->value = value;981 instruction->value = value;
1005982
...@@ -1022,8 +999,8 @@ static IrInstruction *ir_build_var_decl(IrBuilder *irb, Scope *scope, AstNode *s...@@ -1022,8 +999,8 @@ static IrInstruction *ir_build_var_decl(IrBuilder *irb, Scope *scope, AstNode *s
1022 VariableTableEntry *var, IrInstruction *var_type, IrInstruction *init_value)999 VariableTableEntry *var, IrInstruction *var_type, IrInstruction *init_value)
1023{1000{
1024 IrInstructionDeclVar *decl_var_instruction = ir_build_instruction<IrInstructionDeclVar>(irb, scope, source_node);1001 IrInstructionDeclVar *decl_var_instruction = ir_build_instruction<IrInstructionDeclVar>(irb, scope, source_node);
1025 decl_var_instruction->base.static_value.special = ConstValSpecialStatic;1002 decl_var_instruction->base.value.special = ConstValSpecialStatic;
1026 decl_var_instruction->base.type_entry = irb->codegen->builtin_types.entry_void;1003 decl_var_instruction->base.value.type = irb->codegen->builtin_types.entry_void;
1027 decl_var_instruction->var = var;1004 decl_var_instruction->var = var;
1028 decl_var_instruction->var_type = var_type;1005 decl_var_instruction->var_type = var_type;
1029 decl_var_instruction->init_value = init_value;1006 decl_var_instruction->init_value = init_value;
...@@ -1302,8 +1279,8 @@ static IrInstruction *ir_build_switch_br(IrBuilder *irb, Scope *scope, AstNode *...@@ -1302,8 +1279,8 @@ static IrInstruction *ir_build_switch_br(IrBuilder *irb, Scope *scope, AstNode *
1302 IrBasicBlock *else_block, size_t case_count, IrInstructionSwitchBrCase *cases, IrInstruction *is_comptime)1279 IrBasicBlock *else_block, size_t case_count, IrInstructionSwitchBrCase *cases, IrInstruction *is_comptime)
1303{1280{
1304 IrInstructionSwitchBr *instruction = ir_build_instruction<IrInstructionSwitchBr>(irb, scope, source_node);1281 IrInstructionSwitchBr *instruction = ir_build_instruction<IrInstructionSwitchBr>(irb, scope, source_node);
1305 instruction->base.type_entry = irb->codegen->builtin_types.entry_unreachable;1282 instruction->base.value.type = irb->codegen->builtin_types.entry_unreachable;
1306 instruction->base.static_value.special = ConstValSpecialStatic;1283 instruction->base.value.special = ConstValSpecialStatic;
1307 instruction->target_value = target_value;1284 instruction->target_value = target_value;
1308 instruction->else_block = else_block;1285 instruction->else_block = else_block;
1309 instruction->case_count = case_count;1286 instruction->case_count = case_count;
...@@ -1682,13 +1659,14 @@ static IrInstruction *ir_build_memcpy_from(IrBuilder *irb, IrInstruction *old_in...@@ -1682,13 +1659,14 @@ static IrInstruction *ir_build_memcpy_from(IrBuilder *irb, IrInstruction *old_in
1682}1659}
16831660
1684static IrInstruction *ir_build_slice(IrBuilder *irb, Scope *scope, AstNode *source_node,1661static IrInstruction *ir_build_slice(IrBuilder *irb, Scope *scope, AstNode *source_node,
1685 IrInstruction *ptr, IrInstruction *start, IrInstruction *end, bool is_const)1662 IrInstruction *ptr, IrInstruction *start, IrInstruction *end, bool is_const, bool safety_check_on)
1686{1663{
1687 IrInstructionSlice *instruction = ir_build_instruction<IrInstructionSlice>(irb, scope, source_node);1664 IrInstructionSlice *instruction = ir_build_instruction<IrInstructionSlice>(irb, scope, source_node);
1688 instruction->ptr = ptr;1665 instruction->ptr = ptr;
1689 instruction->start = start;1666 instruction->start = start;
1690 instruction->end = end;1667 instruction->end = end;
1691 instruction->is_const = is_const;1668 instruction->is_const = is_const;
1669 instruction->safety_check_on = safety_check_on;
16921670
1693 ir_ref_instruction(ptr);1671 ir_ref_instruction(ptr);
1694 ir_ref_instruction(start);1672 ir_ref_instruction(start);
...@@ -1698,9 +1676,10 @@ static IrInstruction *ir_build_slice(IrBuilder *irb, Scope *scope, AstNode *sour...@@ -1698,9 +1676,10 @@ static IrInstruction *ir_build_slice(IrBuilder *irb, Scope *scope, AstNode *sour
1698}1676}
16991677
1700static IrInstruction *ir_build_slice_from(IrBuilder *irb, IrInstruction *old_instruction,1678static IrInstruction *ir_build_slice_from(IrBuilder *irb, IrInstruction *old_instruction,
1701 IrInstruction *ptr, IrInstruction *start, IrInstruction *end, bool is_const)1679 IrInstruction *ptr, IrInstruction *start, IrInstruction *end, bool is_const, bool safety_check_on)
1702{1680{
1703 IrInstruction *new_instruction = ir_build_slice(irb, old_instruction->scope, old_instruction->source_node, ptr, start, end, is_const);1681 IrInstruction *new_instruction = ir_build_slice(irb, old_instruction->scope,
1682 old_instruction->source_node, ptr, start, end, is_const, safety_check_on);
1704 ir_link_new_instruction(new_instruction, old_instruction);1683 ir_link_new_instruction(new_instruction, old_instruction);
1705 return new_instruction;1684 return new_instruction;
1706}1685}
...@@ -1892,6 +1871,18 @@ static IrInstruction *ir_build_init_enum_from(IrBuilder *irb, IrInstruction *old...@@ -1892,6 +1871,18 @@ static IrInstruction *ir_build_init_enum_from(IrBuilder *irb, IrInstruction *old
1892 return new_instruction;1871 return new_instruction;
1893}1872}
18941873
1874static IrInstruction *ir_build_pointer_reinterpret(IrBuilder *irb, Scope *scope, AstNode *source_node,
1875 IrInstruction *ptr)
1876{
1877 IrInstructionPointerReinterpret *instruction = ir_build_instruction<IrInstructionPointerReinterpret>(
1878 irb, scope, source_node);
1879 instruction->ptr = ptr;
1880
1881 ir_ref_instruction(ptr);
1882
1883 return &instruction->base;
1884}
1885
1895static void ir_count_defers(IrBuilder *irb, Scope *inner_scope, Scope *outer_scope, size_t *results) {1886static void ir_count_defers(IrBuilder *irb, Scope *inner_scope, Scope *outer_scope, size_t *results) {
1896 results[ReturnKindUnconditional] = 0;1887 results[ReturnKindUnconditional] = 0;
1897 results[ReturnKindError] = 0;1888 results[ReturnKindError] = 0;
...@@ -2088,21 +2079,21 @@ static VariableTableEntry *create_local_var(CodeGen *codegen, AstNode *node, Sco...@@ -2088,21 +2079,21 @@ static VariableTableEntry *create_local_var(CodeGen *codegen, AstNode *node, Sco
2088 ErrorMsg *msg = add_node_error(codegen, node,2079 ErrorMsg *msg = add_node_error(codegen, node,
2089 buf_sprintf("redeclaration of variable '%s'", buf_ptr(name)));2080 buf_sprintf("redeclaration of variable '%s'", buf_ptr(name)));
2090 add_error_note(codegen, msg, existing_var->decl_node, buf_sprintf("previous declaration is here"));2081 add_error_note(codegen, msg, existing_var->decl_node, buf_sprintf("previous declaration is here"));
2091 variable_entry->type = codegen->builtin_types.entry_invalid;2082 variable_entry->value.type = codegen->builtin_types.entry_invalid;
2092 } else {2083 } else {
2093 auto primitive_table_entry = codegen->primitive_type_table.maybe_get(name);2084 auto primitive_table_entry = codegen->primitive_type_table.maybe_get(name);
2094 if (primitive_table_entry) {2085 if (primitive_table_entry) {
2095 TypeTableEntry *type = primitive_table_entry->value;2086 TypeTableEntry *type = primitive_table_entry->value;
2096 add_node_error(codegen, node,2087 add_node_error(codegen, node,
2097 buf_sprintf("variable shadows type '%s'", buf_ptr(&type->name)));2088 buf_sprintf("variable shadows type '%s'", buf_ptr(&type->name)));
2098 variable_entry->type = codegen->builtin_types.entry_invalid;2089 variable_entry->value.type = codegen->builtin_types.entry_invalid;
2099 } else {2090 } else {
2100 Tld *tld = find_decl(parent_scope, name);2091 Tld *tld = find_decl(parent_scope, name);
2101 if (tld && tld->id != TldIdVar) {2092 if (tld && tld->id != TldIdVar) {
2102 ErrorMsg *msg = add_node_error(codegen, node,2093 ErrorMsg *msg = add_node_error(codegen, node,
2103 buf_sprintf("redefinition of '%s'", buf_ptr(name)));2094 buf_sprintf("redefinition of '%s'", buf_ptr(name)));
2104 add_error_note(codegen, msg, tld->source_node, buf_sprintf("previous definition is here"));2095 add_error_note(codegen, msg, tld->source_node, buf_sprintf("previous definition is here"));
2105 variable_entry->type = codegen->builtin_types.entry_invalid;2096 variable_entry->value.type = codegen->builtin_types.entry_invalid;
2106 }2097 }
2107 }2098 }
2108 }2099 }
...@@ -3275,7 +3266,7 @@ static IrInstruction *ir_gen_var_decl(IrBuilder *irb, Scope *scope, AstNode *nod...@@ -3275,7 +3266,7 @@ static IrInstruction *ir_gen_var_decl(IrBuilder *irb, Scope *scope, AstNode *nod
3275 // is inside var->child_scope3266 // is inside var->child_scope
32763267
3277 if (!is_extern && !variable_declaration->expr) {3268 if (!is_extern && !variable_declaration->expr) {
3278 var->type = irb->codegen->builtin_types.entry_invalid;3269 var->value.type = irb->codegen->builtin_types.entry_invalid;
3279 add_node_error(irb->codegen, node, buf_sprintf("variables must be initialized"));3270 add_node_error(irb->codegen, node, buf_sprintf("variables must be initialized"));
3280 return irb->codegen->invalid_instruction;3271 return irb->codegen->invalid_instruction;
3281 }3272 }
...@@ -3988,7 +3979,7 @@ static IrInstruction *ir_gen_slice(IrBuilder *irb, Scope *scope, AstNode *node)...@@ -3988,7 +3979,7 @@ static IrInstruction *ir_gen_slice(IrBuilder *irb, Scope *scope, AstNode *node)
3988 end_value = nullptr;3979 end_value = nullptr;
3989 }3980 }
39903981
3991 return ir_build_slice(irb, scope, node, ptr_value, start_value, end_value, slice_expr->is_const);3982 return ir_build_slice(irb, scope, node, ptr_value, start_value, end_value, slice_expr->is_const, true);
3992}3983}
39933984
3994static IrInstruction *ir_gen_err_ok_or(IrBuilder *irb, Scope *parent_scope, AstNode *node) {3985static IrInstruction *ir_gen_err_ok_or(IrBuilder *irb, Scope *parent_scope, AstNode *node) {
...@@ -4348,7 +4339,7 @@ static IrInstruction *ir_exec_const_result(IrExecutable *exec) {...@@ -4348,7 +4339,7 @@ static IrInstruction *ir_exec_const_result(IrExecutable *exec) {
4348 if (instruction->id == IrInstructionIdReturn) {4339 if (instruction->id == IrInstructionIdReturn) {
4349 IrInstructionReturn *ret_inst = (IrInstructionReturn *)instruction;4340 IrInstructionReturn *ret_inst = (IrInstructionReturn *)instruction;
4350 IrInstruction *value = ret_inst->value;4341 IrInstruction *value = ret_inst->value;
4351 assert(value->static_value.special != ConstValSpecialRuntime);4342 assert(value->value.special != ConstValSpecialRuntime);
4352 return value;4343 return value;
4353 } else if (ir_has_side_effects(instruction)) {4344 } else if (ir_has_side_effects(instruction)) {
4354 return nullptr;4345 return nullptr;
...@@ -4372,7 +4363,7 @@ static bool ir_num_lit_fits_in_other_type(IrAnalyze *ira, IrInstruction *instruc...@@ -4372,7 +4363,7 @@ static bool ir_num_lit_fits_in_other_type(IrAnalyze *ira, IrInstruction *instruc
4372 return false;4363 return false;
4373 }4364 }
43744365
4375 ConstExprValue *const_val = &instruction->static_value;4366 ConstExprValue *const_val = &instruction->value;
4376 assert(const_val->special != ConstValSpecialRuntime);4367 assert(const_val->special != ConstValSpecialRuntime);
4377 if (other_type_underlying->id == TypeTableEntryIdFloat) {4368 if (other_type_underlying->id == TypeTableEntryIdFloat) {
4378 return true;4369 return true;
...@@ -4514,15 +4505,15 @@ static ImplicitCastMatchResult ir_types_match_with_implicit_cast(IrAnalyze *ira,...@@ -4514,15 +4505,15 @@ static ImplicitCastMatchResult ir_types_match_with_implicit_cast(IrAnalyze *ira,
4514static TypeTableEntry *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, IrInstruction **instructions, size_t instruction_count) {4505static TypeTableEntry *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, IrInstruction **instructions, size_t instruction_count) {
4515 assert(instruction_count >= 1);4506 assert(instruction_count >= 1);
4516 IrInstruction *prev_inst = instructions[0];4507 IrInstruction *prev_inst = instructions[0];
4517 if (prev_inst->type_entry->id == TypeTableEntryIdInvalid) {4508 if (prev_inst->value.type->id == TypeTableEntryIdInvalid) {
4518 return ira->codegen->builtin_types.entry_invalid;4509 return ira->codegen->builtin_types.entry_invalid;
4519 }4510 }
4520 bool any_are_pure_error = (prev_inst->type_entry->id == TypeTableEntryIdPureError);4511 bool any_are_pure_error = (prev_inst->value.type->id == TypeTableEntryIdPureError);
4521 bool any_are_null = (prev_inst->type_entry->id == TypeTableEntryIdNullLit);4512 bool any_are_null = (prev_inst->value.type->id == TypeTableEntryIdNullLit);
4522 for (size_t i = 1; i < instruction_count; i += 1) {4513 for (size_t i = 1; i < instruction_count; i += 1) {
4523 IrInstruction *cur_inst = instructions[i];4514 IrInstruction *cur_inst = instructions[i];
4524 TypeTableEntry *cur_type = cur_inst->type_entry;4515 TypeTableEntry *cur_type = cur_inst->value.type;
4525 TypeTableEntry *prev_type = prev_inst->type_entry;4516 TypeTableEntry *prev_type = prev_inst->value.type;
4526 if (cur_type->id == TypeTableEntryIdInvalid) {4517 if (cur_type->id == TypeTableEntryIdInvalid) {
4527 return cur_type;4518 return cur_type;
4528 } else if (prev_type->id == TypeTableEntryIdUnreachable) {4519 } else if (prev_type->id == TypeTableEntryIdUnreachable) {
...@@ -4598,32 +4589,32 @@ static TypeTableEntry *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_nod...@@ -4598,32 +4589,32 @@ static TypeTableEntry *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_nod
4598 return ira->codegen->builtin_types.entry_invalid;4589 return ira->codegen->builtin_types.entry_invalid;
4599 }4590 }
4600 }4591 }
4601 if (any_are_pure_error && prev_inst->type_entry->id != TypeTableEntryIdPureError) {4592 if (any_are_pure_error && prev_inst->value.type->id != TypeTableEntryIdPureError) {
4602 if (prev_inst->type_entry->id == TypeTableEntryIdNumLitInt ||4593 if (prev_inst->value.type->id == TypeTableEntryIdNumLitInt ||
4603 prev_inst->type_entry->id == TypeTableEntryIdNumLitFloat)4594 prev_inst->value.type->id == TypeTableEntryIdNumLitFloat)
4604 {4595 {
4605 ir_add_error_node(ira, source_node,4596 ir_add_error_node(ira, source_node,
4606 buf_sprintf("unable to make error union out of number literal"));4597 buf_sprintf("unable to make error union out of number literal"));
4607 return ira->codegen->builtin_types.entry_invalid;4598 return ira->codegen->builtin_types.entry_invalid;
4608 } else if (prev_inst->type_entry->id == TypeTableEntryIdNullLit) {4599 } else if (prev_inst->value.type->id == TypeTableEntryIdNullLit) {
4609 ir_add_error_node(ira, source_node,4600 ir_add_error_node(ira, source_node,
4610 buf_sprintf("unable to make error union out of null literal"));4601 buf_sprintf("unable to make error union out of null literal"));
4611 return ira->codegen->builtin_types.entry_invalid;4602 return ira->codegen->builtin_types.entry_invalid;
4612 } else {4603 } else {
4613 return get_error_type(ira->codegen, prev_inst->type_entry);4604 return get_error_type(ira->codegen, prev_inst->value.type);
4614 }4605 }
4615 } else if (any_are_null && prev_inst->type_entry->id != TypeTableEntryIdNullLit) {4606 } else if (any_are_null && prev_inst->value.type->id != TypeTableEntryIdNullLit) {
4616 if (prev_inst->type_entry->id == TypeTableEntryIdNumLitInt ||4607 if (prev_inst->value.type->id == TypeTableEntryIdNumLitInt ||
4617 prev_inst->type_entry->id == TypeTableEntryIdNumLitFloat)4608 prev_inst->value.type->id == TypeTableEntryIdNumLitFloat)
4618 {4609 {
4619 ir_add_error_node(ira, source_node,4610 ir_add_error_node(ira, source_node,
4620 buf_sprintf("unable to make maybe out of number literal"));4611 buf_sprintf("unable to make maybe out of number literal"));
4621 return ira->codegen->builtin_types.entry_invalid;4612 return ira->codegen->builtin_types.entry_invalid;
4622 } else {4613 } else {
4623 return get_maybe_type(ira->codegen, prev_inst->type_entry);4614 return get_maybe_type(ira->codegen, prev_inst->value.type);
4624 }4615 }
4625 } else {4616 } else {
4626 return prev_inst->type_entry;4617 return prev_inst->value.type;
4627 }4618 }
4628}4619}
46294620
...@@ -4649,9 +4640,7 @@ static void eval_const_expr_implicit_cast(CastOp cast_op,...@@ -4649,9 +4640,7 @@ static void eval_const_expr_implicit_cast(CastOp cast_op,
4649 case CastOpNoop:4640 case CastOpNoop:
4650 case CastOpWidenOrShorten:4641 case CastOpWidenOrShorten:
4651 *const_val = *other_val;4642 *const_val = *other_val;
4652 break;4643 const_val->type = new_type;
4653 case CastOpPointerReinterpret:
4654 zig_panic("TODO compile time pointer reinterpret");
4655 break;4644 break;
4656 case CastOpPtrToInt:4645 case CastOpPtrToInt:
4657 case CastOpIntToPtr:4646 case CastOpIntToPtr:
...@@ -4659,24 +4648,6 @@ static void eval_const_expr_implicit_cast(CastOp cast_op,...@@ -4659,24 +4648,6 @@ static void eval_const_expr_implicit_cast(CastOp cast_op,
4659 case CastOpBytesToSlice:4648 case CastOpBytesToSlice:
4660 // can't do it4649 // can't do it
4661 break;4650 break;
4662 case CastOpToUnknownSizeArray:
4663 {
4664 assert(other_type->id == TypeTableEntryIdArray);
4665 assert(other_val->data.x_array.size == other_type->data.array.len);
4666
4667 const_val->data.x_struct.fields = allocate<ConstExprValue>(2);
4668 ConstExprValue *ptr_field = &const_val->data.x_struct.fields[slice_ptr_index];
4669 ConstExprValue *len_field = &const_val->data.x_struct.fields[slice_len_index];
4670
4671 ptr_field->special = ConstValSpecialStatic;
4672 ptr_field->data.x_ptr.base_ptr = other_val;
4673
4674 len_field->special = ConstValSpecialStatic;
4675 bignum_init_unsigned(&len_field->data.x_bignum, other_type->data.array.len);
4676
4677 const_val->special = ConstValSpecialStatic;
4678 break;
4679 }
4680 case CastOpErrToInt:4651 case CastOpErrToInt:
4681 {4652 {
4682 uint64_t value;4653 uint64_t value;
...@@ -4722,14 +4693,15 @@ static void eval_const_expr_implicit_cast(CastOp cast_op,...@@ -4722,14 +4693,15 @@ static void eval_const_expr_implicit_cast(CastOp cast_op,
4722static IrInstruction *ir_resolve_cast(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *value,4693static IrInstruction *ir_resolve_cast(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *value,
4723 TypeTableEntry *wanted_type, CastOp cast_op, bool need_alloca)4694 TypeTableEntry *wanted_type, CastOp cast_op, bool need_alloca)
4724{4695{
4725 if (value->static_value.special != ConstValSpecialRuntime) {4696 if (value->value.special != ConstValSpecialRuntime) {
4726 IrInstruction *result = ir_create_const(&ira->new_irb, source_instr->scope, source_instr->source_node, wanted_type, false);4697 IrInstruction *result = ir_create_const(&ira->new_irb, source_instr->scope,
4727 eval_const_expr_implicit_cast(cast_op, &value->static_value, value->type_entry,4698 source_instr->source_node, wanted_type, false);
4728 &result->static_value, wanted_type);4699 eval_const_expr_implicit_cast(cast_op, &value->value, value->value.type,
4700 &result->value, wanted_type);
4729 return result;4701 return result;
4730 } else {4702 } else {
4731 IrInstruction *result = ir_build_cast(&ira->new_irb, source_instr->scope, source_instr->source_node, wanted_type, value, cast_op);4703 IrInstruction *result = ir_build_cast(&ira->new_irb, source_instr->scope, source_instr->source_node, wanted_type, value, cast_op);
4732 result->type_entry = wanted_type;4704 result->value.type = wanted_type;
4733 if (need_alloca) {4705 if (need_alloca) {
4734 FnTableEntry *fn_entry = exec_fn_entry(ira->new_irb.exec);4706 FnTableEntry *fn_entry = exec_fn_entry(ira->new_irb.exec);
4735 if (fn_entry)4707 if (fn_entry)
...@@ -4861,7 +4833,7 @@ static ConstExprValue *ir_build_const_from(IrAnalyze *ira, IrInstruction *old_in...@@ -4861,7 +4833,7 @@ static ConstExprValue *ir_build_const_from(IrAnalyze *ira, IrInstruction *old_in
4861 new_instruction = &const_instruction->base;4833 new_instruction = &const_instruction->base;
4862 }4834 }
4863 ir_link_new_instruction(new_instruction, old_instruction);4835 ir_link_new_instruction(new_instruction, old_instruction);
4864 ConstExprValue *const_val = &new_instruction->static_value;4836 ConstExprValue *const_val = &new_instruction->value;
4865 const_val->special = ConstValSpecialStatic;4837 const_val->special = ConstValSpecialStatic;
4866 const_val->depends_on_compile_var = depends_on_compile_var;4838 const_val->depends_on_compile_var = depends_on_compile_var;
4867 return const_val;4839 return const_val;
...@@ -4906,15 +4878,15 @@ enum UndefAllowed {...@@ -4906,15 +4878,15 @@ enum UndefAllowed {
4906};4878};
49074879
4908static ConstExprValue *ir_resolve_const(IrAnalyze *ira, IrInstruction *value, UndefAllowed undef_allowed) {4880static ConstExprValue *ir_resolve_const(IrAnalyze *ira, IrInstruction *value, UndefAllowed undef_allowed) {
4909 switch (value->static_value.special) {4881 switch (value->value.special) {
4910 case ConstValSpecialStatic:4882 case ConstValSpecialStatic:
4911 return &value->static_value;4883 return &value->value;
4912 case ConstValSpecialRuntime:4884 case ConstValSpecialRuntime:
4913 ir_add_error(ira, value, buf_sprintf("unable to evaluate constant expression"));4885 ir_add_error(ira, value, buf_sprintf("unable to evaluate constant expression"));
4914 return nullptr;4886 return nullptr;
4915 case ConstValSpecialUndef:4887 case ConstValSpecialUndef:
4916 if (undef_allowed == UndefOk) {4888 if (undef_allowed == UndefOk) {
4917 return &value->static_value;4889 return &value->value;
4918 } else {4890 } else {
4919 ir_add_error(ira, value, buf_sprintf("use of undefined value"));4891 ir_add_error(ira, value, buf_sprintf("use of undefined value"));
4920 return nullptr;4892 return nullptr;
...@@ -4979,12 +4951,12 @@ IrInstruction *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node...@@ -4979,12 +4951,12 @@ IrInstruction *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node
4979}4951}
49804952
4981static TypeTableEntry *ir_resolve_type(IrAnalyze *ira, IrInstruction *type_value) {4953static TypeTableEntry *ir_resolve_type(IrAnalyze *ira, IrInstruction *type_value) {
4982 if (type_value->type_entry->id == TypeTableEntryIdInvalid)4954 if (type_value->value.type->id == TypeTableEntryIdInvalid)
4983 return ira->codegen->builtin_types.entry_invalid;4955 return ira->codegen->builtin_types.entry_invalid;
49844956
4985 if (type_value->type_entry->id != TypeTableEntryIdMetaType) {4957 if (type_value->value.type->id != TypeTableEntryIdMetaType) {
4986 ir_add_error(ira, type_value,4958 ir_add_error(ira, type_value,
4987 buf_sprintf("expected type 'type', found '%s'", buf_ptr(&type_value->type_entry->name)));4959 buf_sprintf("expected type 'type', found '%s'", buf_ptr(&type_value->value.type->name)));
4988 return ira->codegen->builtin_types.entry_invalid;4960 return ira->codegen->builtin_types.entry_invalid;
4989 }4961 }
49904962
...@@ -4999,12 +4971,12 @@ static FnTableEntry *ir_resolve_fn(IrAnalyze *ira, IrInstruction *fn_value) {...@@ -4999,12 +4971,12 @@ static FnTableEntry *ir_resolve_fn(IrAnalyze *ira, IrInstruction *fn_value) {
4999 if (fn_value == ira->codegen->invalid_instruction)4971 if (fn_value == ira->codegen->invalid_instruction)
5000 return nullptr;4972 return nullptr;
50014973
5002 if (fn_value->type_entry->id == TypeTableEntryIdInvalid)4974 if (fn_value->value.type->id == TypeTableEntryIdInvalid)
5003 return nullptr;4975 return nullptr;
50044976
5005 if (fn_value->type_entry->id != TypeTableEntryIdFn) {4977 if (fn_value->value.type->id != TypeTableEntryIdFn) {
5006 ir_add_error_node(ira, fn_value->source_node,4978 ir_add_error_node(ira, fn_value->source_node,
5007 buf_sprintf("expected function type, found '%s'", buf_ptr(&fn_value->type_entry->name)));4979 buf_sprintf("expected function type, found '%s'", buf_ptr(&fn_value->value.type->name)));
5008 return nullptr;4980 return nullptr;
5009 }4981 }
50104982
...@@ -5019,47 +4991,88 @@ static IrInstruction *ir_analyze_maybe_wrap(IrAnalyze *ira, IrInstruction *sourc...@@ -5019,47 +4991,88 @@ static IrInstruction *ir_analyze_maybe_wrap(IrAnalyze *ira, IrInstruction *sourc
5019 assert(wanted_type->id == TypeTableEntryIdMaybe);4991 assert(wanted_type->id == TypeTableEntryIdMaybe);
50204992
5021 if (instr_is_comptime(value)) {4993 if (instr_is_comptime(value)) {
5022 ConstExprValue *val = ir_resolve_const(ira, value, UndefBad);4994 TypeTableEntry *payload_type = wanted_type->data.maybe.child_type;
4995 IrInstruction *casted_payload = ir_implicit_cast(ira, value, payload_type);
4996 if (casted_payload->value.type->id == TypeTableEntryIdInvalid)
4997 return ira->codegen->invalid_instruction;
4998
4999 ConstExprValue *val = ir_resolve_const(ira, casted_payload, UndefBad);
5023 if (!val)5000 if (!val)
5024 return ira->codegen->invalid_instruction;5001 return ira->codegen->invalid_instruction;
50255002
5026 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(ira->new_irb.exec,5003 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(ira->new_irb.exec,
5027 source_instr->scope, source_instr->source_node);5004 source_instr->scope, source_instr->source_node);
5028 const_instruction->base.type_entry = wanted_type;5005 const_instruction->base.value.type = wanted_type;
5029 const_instruction->base.static_value.special = ConstValSpecialStatic;5006 const_instruction->base.value.special = ConstValSpecialStatic;
5030 const_instruction->base.static_value.depends_on_compile_var = val->depends_on_compile_var;5007 const_instruction->base.value.depends_on_compile_var = val->depends_on_compile_var;
5031 const_instruction->base.static_value.data.x_maybe = &value->static_value;5008 const_instruction->base.value.data.x_maybe = val;
5032 return &const_instruction->base;5009 return &const_instruction->base;
5033 }5010 }
50345011
5035 IrInstruction *result = ir_build_maybe_wrap(&ira->new_irb, source_instr->scope, source_instr->source_node, value);5012 IrInstruction *result = ir_build_maybe_wrap(&ira->new_irb, source_instr->scope, source_instr->source_node, value);
5036 result->type_entry = wanted_type;5013 result->value.type = wanted_type;
5037 result->static_value.data.rh_maybe = RuntimeHintMaybeNonNull;5014 result->value.data.rh_maybe = RuntimeHintMaybeNonNull;
5038 ir_add_alloca(ira, result, wanted_type);5015 ir_add_alloca(ira, result, wanted_type);
5039 return result;5016 return result;
5040}5017}
50415018
5042static IrInstruction *ir_analyze_err_wrap_payload(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *value, TypeTableEntry *wanted_type) {5019static IrInstruction *ir_analyze_pointer_reinterpret(IrAnalyze *ira, IrInstruction *source_instr,
5020 IrInstruction *ptr, TypeTableEntry *wanted_type)
5021{
5022 assert(wanted_type->id == TypeTableEntryIdPointer);
5023
5024 if (ptr->value.type->id != TypeTableEntryIdPointer) {
5025 ir_add_error(ira, ptr,
5026 buf_sprintf("expected pointer, found '%s'", buf_ptr(&ptr->value.type->name)));
5027 return ira->codegen->invalid_instruction;
5028 }
5029
5030 if (instr_is_comptime(ptr)) {
5031 ConstExprValue *val = ir_resolve_const(ira, ptr, UndefOk);
5032 if (!val)
5033 return ira->codegen->invalid_instruction;
5034
5035 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(ira->new_irb.exec,
5036 source_instr->scope, source_instr->source_node);
5037 const_instruction->base.value = *val;
5038 const_instruction->base.value.type = wanted_type;
5039 return &const_instruction->base;
5040 }
5041
5042 IrInstruction *result = ir_build_pointer_reinterpret(&ira->new_irb, source_instr->scope,
5043 source_instr->source_node, ptr);
5044 result->value.type = wanted_type;
5045 return result;
5046}
5047
5048static IrInstruction *ir_analyze_err_wrap_payload(IrAnalyze *ira, IrInstruction *source_instr,
5049 IrInstruction *value, TypeTableEntry *wanted_type)
5050{
5043 assert(wanted_type->id == TypeTableEntryIdErrorUnion);5051 assert(wanted_type->id == TypeTableEntryIdErrorUnion);
50445052
5045 if (instr_is_comptime(value)) {5053 if (instr_is_comptime(value)) {
5046 ConstExprValue *val = ir_resolve_const(ira, value, UndefBad);5054 TypeTableEntry *payload_type = wanted_type->data.error.child_type;
5055 IrInstruction *casted_payload = ir_implicit_cast(ira, value, payload_type);
5056 if (casted_payload->value.type->id == TypeTableEntryIdInvalid)
5057 return ira->codegen->invalid_instruction;
5058
5059 ConstExprValue *val = ir_resolve_const(ira, casted_payload, UndefBad);
5047 if (!val)5060 if (!val)
5048 return ira->codegen->invalid_instruction;5061 return ira->codegen->invalid_instruction;
50495062
5050 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(ira->new_irb.exec,5063 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(ira->new_irb.exec,
5051 source_instr->scope, source_instr->source_node);5064 source_instr->scope, source_instr->source_node);
5052 const_instruction->base.type_entry = wanted_type;5065 const_instruction->base.value.type = wanted_type;
5053 const_instruction->base.static_value.special = ConstValSpecialStatic;5066 const_instruction->base.value.special = ConstValSpecialStatic;
5054 const_instruction->base.static_value.depends_on_compile_var = val->depends_on_compile_var;5067 const_instruction->base.value.depends_on_compile_var = val->depends_on_compile_var;
5055 const_instruction->base.static_value.data.x_err_union.err = nullptr;5068 const_instruction->base.value.data.x_err_union.err = nullptr;
5056 const_instruction->base.static_value.data.x_err_union.payload = val;5069 const_instruction->base.value.data.x_err_union.payload = val;
5057 return &const_instruction->base;5070 return &const_instruction->base;
5058 }5071 }
50595072
5060 IrInstruction *result = ir_build_err_wrap_payload(&ira->new_irb, source_instr->scope, source_instr->source_node, value);5073 IrInstruction *result = ir_build_err_wrap_payload(&ira->new_irb, source_instr->scope, source_instr->source_node, value);
5061 result->type_entry = wanted_type;5074 result->value.type = wanted_type;
5062 result->static_value.data.rh_error_union = RuntimeHintErrorUnionNonError;5075 result->value.data.rh_error_union = RuntimeHintErrorUnionNonError;
5063 ir_add_alloca(ira, result, wanted_type);5076 ir_add_alloca(ira, result, wanted_type);
5064 return result;5077 return result;
5065}5078}
...@@ -5074,17 +5087,17 @@ static IrInstruction *ir_analyze_err_wrap_code(IrAnalyze *ira, IrInstruction *so...@@ -5074,17 +5087,17 @@ static IrInstruction *ir_analyze_err_wrap_code(IrAnalyze *ira, IrInstruction *so
50745087
5075 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(ira->new_irb.exec,5088 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(ira->new_irb.exec,
5076 source_instr->scope, source_instr->source_node);5089 source_instr->scope, source_instr->source_node);
5077 const_instruction->base.type_entry = wanted_type;5090 const_instruction->base.value.type = wanted_type;
5078 const_instruction->base.static_value.special = ConstValSpecialStatic;5091 const_instruction->base.value.special = ConstValSpecialStatic;
5079 const_instruction->base.static_value.depends_on_compile_var = val->depends_on_compile_var;5092 const_instruction->base.value.depends_on_compile_var = val->depends_on_compile_var;
5080 const_instruction->base.static_value.data.x_err_union.err = val->data.x_pure_err;5093 const_instruction->base.value.data.x_err_union.err = val->data.x_pure_err;
5081 const_instruction->base.static_value.data.x_err_union.payload = nullptr;5094 const_instruction->base.value.data.x_err_union.payload = nullptr;
5082 return &const_instruction->base;5095 return &const_instruction->base;
5083 }5096 }
50845097
5085 IrInstruction *result = ir_build_err_wrap_code(&ira->new_irb, source_instr->scope, source_instr->source_node, value);5098 IrInstruction *result = ir_build_err_wrap_code(&ira->new_irb, source_instr->scope, source_instr->source_node, value);
5086 result->type_entry = wanted_type;5099 result->value.type = wanted_type;
5087 result->static_value.data.rh_error_union = RuntimeHintErrorUnionError;5100 result->value.data.rh_error_union = RuntimeHintErrorUnionError;
5088 ir_add_alloca(ira, result, wanted_type);5101 ir_add_alloca(ira, result, wanted_type);
5089 return result;5102 return result;
5090}5103}
...@@ -5097,11 +5110,11 @@ static IrInstruction *ir_analyze_cast_ref(IrAnalyze *ira, IrInstruction *source_...@@ -5097,11 +5110,11 @@ static IrInstruction *ir_analyze_cast_ref(IrAnalyze *ira, IrInstruction *source_
50975110
5098 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(ira->new_irb.exec,5111 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(ira->new_irb.exec,
5099 source_instr->scope, source_instr->source_node);5112 source_instr->scope, source_instr->source_node);
5100 const_instruction->base.type_entry = wanted_type;5113 const_instruction->base.value.type = wanted_type;
5101 const_instruction->base.static_value.special = ConstValSpecialStatic;5114 const_instruction->base.value.special = ConstValSpecialStatic;
5102 const_instruction->base.static_value.depends_on_compile_var = val->depends_on_compile_var;5115 const_instruction->base.value.depends_on_compile_var = val->depends_on_compile_var;
5103 const_instruction->base.static_value.data.x_ptr.base_ptr = val;5116 const_instruction->base.value.data.x_ptr.base_ptr = val;
5104 const_instruction->base.static_value.data.x_ptr.index = SIZE_MAX;5117 const_instruction->base.value.data.x_ptr.index = SIZE_MAX;
5105 return &const_instruction->base;5118 return &const_instruction->base;
5106 }5119 }
51075120
...@@ -5130,17 +5143,48 @@ static IrInstruction *ir_analyze_null_to_maybe(IrAnalyze *ira, IrInstruction *so...@@ -5130,17 +5143,48 @@ static IrInstruction *ir_analyze_null_to_maybe(IrAnalyze *ira, IrInstruction *so
5130 assert(val);5143 assert(val);
51315144
5132 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(ira->new_irb.exec, source_instr->scope, source_instr->source_node);5145 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(ira->new_irb.exec, source_instr->scope, source_instr->source_node);
5133 const_instruction->base.type_entry = wanted_type;5146 const_instruction->base.value.type = wanted_type;
5134 const_instruction->base.static_value.special = ConstValSpecialStatic;5147 const_instruction->base.value.special = ConstValSpecialStatic;
5135 const_instruction->base.static_value.depends_on_compile_var = val->depends_on_compile_var;5148 const_instruction->base.value.depends_on_compile_var = val->depends_on_compile_var;
5136 const_instruction->base.static_value.data.x_maybe = nullptr;5149 const_instruction->base.value.data.x_maybe = nullptr;
5137 return &const_instruction->base;5150 return &const_instruction->base;
5138}5151}
51395152
5153static IrInstruction *ir_analyze_array_to_slice(IrAnalyze *ira, IrInstruction *source_instr,
5154 IrInstruction *array, TypeTableEntry *wanted_type)
5155{
5156 assert(is_slice(wanted_type));
5157
5158 TypeTableEntry *array_type = array->value.type;
5159 assert(array_type->id == TypeTableEntryIdArray);
5160
5161 if (instr_is_comptime(array)) {
5162 IrInstruction *result = ir_create_const(&ira->new_irb, source_instr->scope,
5163 source_instr->source_node, wanted_type, false);
5164 init_const_slice(ira->codegen, &result->value, &array->value, 0, array_type->data.array.len, true);
5165 return result;
5166 }
5167
5168 IrInstruction *start = ir_create_const(&ira->new_irb, source_instr->scope,
5169 source_instr->source_node, ira->codegen->builtin_types.entry_usize, false);
5170 init_const_usize(ira->codegen, &start->value, 0);
5171
5172 IrInstruction *end = ir_create_const(&ira->new_irb, source_instr->scope,
5173 source_instr->source_node, ira->codegen->builtin_types.entry_usize, false);
5174 init_const_usize(ira->codegen, &end->value, array_type->data.array.len);
5175
5176 IrInstruction *result = ir_build_slice(&ira->new_irb, source_instr->scope,
5177 source_instr->source_node, array, start, end, true, false);
5178 TypeTableEntry *child_type = array_type->data.array.child_type;
5179 result->value.type = get_slice_type(ira->codegen, child_type, true);
5180 ir_add_alloca(ira, result, result->value.type);
5181 return result;
5182}
5183
5140static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_instr,5184static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_instr,
5141 TypeTableEntry *wanted_type, IrInstruction *value)5185 TypeTableEntry *wanted_type, IrInstruction *value)
5142{5186{
5143 TypeTableEntry *actual_type = value->type_entry;5187 TypeTableEntry *actual_type = value->value.type;
5144 TypeTableEntry *wanted_type_canon = get_underlying_type(wanted_type);5188 TypeTableEntry *wanted_type_canon = get_underlying_type(wanted_type);
5145 TypeTableEntry *actual_type_canon = get_underlying_type(actual_type);5189 TypeTableEntry *actual_type_canon = get_underlying_type(actual_type);
51465190
...@@ -5213,7 +5257,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst...@@ -5213,7 +5257,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
5213 wanted_type->data.structure.fields[0].type_entry->data.pointer.child_type,5257 wanted_type->data.structure.fields[0].type_entry->data.pointer.child_type,
5214 actual_type->data.array.child_type))5258 actual_type->data.array.child_type))
5215 {5259 {
5216 return ir_resolve_cast(ira, source_instr, value, wanted_type, CastOpToUnknownSizeArray, true);5260 return ir_analyze_array_to_slice(ira, source_instr, value, wanted_type);
5217 }5261 }
52185262
5219 // explicit cast from []T to []u8 or []u8 to []T5263 // explicit cast from []T to []u8 or []u8 to []T
...@@ -5251,7 +5295,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst...@@ -5251,7 +5295,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
5251 if ((actual_type->id == TypeTableEntryIdPointer || actual_type->id == TypeTableEntryIdFn) &&5295 if ((actual_type->id == TypeTableEntryIdPointer || actual_type->id == TypeTableEntryIdFn) &&
5252 (wanted_type->id == TypeTableEntryIdPointer || wanted_type->id == TypeTableEntryIdFn))5296 (wanted_type->id == TypeTableEntryIdPointer || wanted_type->id == TypeTableEntryIdFn))
5253 {5297 {
5254 return ir_resolve_cast(ira, source_instr, value, wanted_type, CastOpPointerReinterpret, false);5298 return ir_analyze_pointer_reinterpret(ira, source_instr, value, wanted_type);
5255 }5299 }
52565300
5257 // explicit cast from maybe pointer to another maybe pointer5301 // explicit cast from maybe pointer to another maybe pointer
...@@ -5262,7 +5306,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst...@@ -5262,7 +5306,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
5262 (wanted_type->data.maybe.child_type->id == TypeTableEntryIdPointer ||5306 (wanted_type->data.maybe.child_type->id == TypeTableEntryIdPointer ||
5263 wanted_type->data.maybe.child_type->id == TypeTableEntryIdFn))5307 wanted_type->data.maybe.child_type->id == TypeTableEntryIdFn))
5264 {5308 {
5265 return ir_resolve_cast(ira, source_instr, value, wanted_type, CastOpPointerReinterpret, false);5309 return ir_analyze_pointer_reinterpret(ira, source_instr, value, wanted_type);
5266 }5310 }
52675311
5268 // explicit cast from child type of maybe type to maybe type5312 // explicit cast from child type of maybe type to maybe type
...@@ -5394,22 +5438,22 @@ static IrInstruction *ir_implicit_cast(IrAnalyze *ira, IrInstruction *value, Typ...@@ -5394,22 +5438,22 @@ static IrInstruction *ir_implicit_cast(IrAnalyze *ira, IrInstruction *value, Typ
5394 assert(value);5438 assert(value);
5395 assert(value != ira->codegen->invalid_instruction);5439 assert(value != ira->codegen->invalid_instruction);
5396 assert(!expected_type || expected_type->id != TypeTableEntryIdInvalid);5440 assert(!expected_type || expected_type->id != TypeTableEntryIdInvalid);
5397 assert(value->type_entry);5441 assert(value->value.type);
5398 assert(value->type_entry->id != TypeTableEntryIdInvalid);5442 assert(value->value.type->id != TypeTableEntryIdInvalid);
5399 if (expected_type == nullptr)5443 if (expected_type == nullptr)
5400 return value; // anything will do5444 return value; // anything will do
5401 if (expected_type == value->type_entry)5445 if (expected_type == value->value.type)
5402 return value; // match5446 return value; // match
5403 if (value->type_entry->id == TypeTableEntryIdUnreachable)5447 if (value->value.type->id == TypeTableEntryIdUnreachable)
5404 return value;5448 return value;
54055449
5406 ImplicitCastMatchResult result = ir_types_match_with_implicit_cast(ira, expected_type, value->type_entry, value);5450 ImplicitCastMatchResult result = ir_types_match_with_implicit_cast(ira, expected_type, value->value.type, value);
5407 switch (result) {5451 switch (result) {
5408 case ImplicitCastMatchResultNo:5452 case ImplicitCastMatchResultNo:
5409 ir_add_error(ira, value,5453 ir_add_error(ira, value,
5410 buf_sprintf("expected type '%s', found '%s'",5454 buf_sprintf("expected type '%s', found '%s'",
5411 buf_ptr(&expected_type->name),5455 buf_ptr(&expected_type->name),
5412 buf_ptr(&value->type_entry->name)));5456 buf_ptr(&value->value.type->name)));
5413 return ira->codegen->invalid_instruction;5457 return ira->codegen->invalid_instruction;
54145458
5415 case ImplicitCastMatchResultYes:5459 case ImplicitCastMatchResultYes:
...@@ -5422,23 +5466,23 @@ static IrInstruction *ir_implicit_cast(IrAnalyze *ira, IrInstruction *value, Typ...@@ -5422,23 +5466,23 @@ static IrInstruction *ir_implicit_cast(IrAnalyze *ira, IrInstruction *value, Typ
5422}5466}
54235467
5424static IrInstruction *ir_get_deref(IrAnalyze *ira, IrInstruction *source_instruction, IrInstruction *ptr) {5468static IrInstruction *ir_get_deref(IrAnalyze *ira, IrInstruction *source_instruction, IrInstruction *ptr) {
5425 TypeTableEntry *type_entry = ptr->type_entry;5469 TypeTableEntry *type_entry = ptr->value.type;
5426 if (type_entry->id == TypeTableEntryIdInvalid) {5470 if (type_entry->id == TypeTableEntryIdInvalid) {
5427 return ira->codegen->invalid_instruction;5471 return ira->codegen->invalid_instruction;
5428 } else if (type_entry->id == TypeTableEntryIdPointer) {5472 } else if (type_entry->id == TypeTableEntryIdPointer) {
5429 TypeTableEntry *child_type = type_entry->data.pointer.child_type;5473 TypeTableEntry *child_type = type_entry->data.pointer.child_type;
5430 if (ptr->static_value.special != ConstValSpecialRuntime) {5474 if (ptr->value.special != ConstValSpecialRuntime) {
5431 ConstExprValue *pointee = const_ptr_pointee(&ptr->static_value);5475 ConstExprValue *pointee = const_ptr_pointee(&ptr->value);
5432 if (pointee->special != ConstValSpecialRuntime) {5476 if (pointee->special != ConstValSpecialRuntime) {
5433 IrInstruction *result = ir_create_const(&ira->new_irb, source_instruction->scope,5477 IrInstruction *result = ir_create_const(&ira->new_irb, source_instruction->scope,
5434 source_instruction->source_node, child_type, pointee->depends_on_compile_var);5478 source_instruction->source_node, child_type, pointee->depends_on_compile_var);
5435 result->static_value = *pointee;5479 result->value = *pointee;
5436 return result;5480 return result;
5437 }5481 }
5438 }5482 }
5439 // TODO if the instruction is a get pointer instruction we can skip it5483 // TODO if the instruction is a get pointer instruction we can skip it
5440 IrInstruction *load_ptr_instruction = ir_build_load_ptr(&ira->new_irb, source_instruction->scope, source_instruction->source_node, ptr);5484 IrInstruction *load_ptr_instruction = ir_build_load_ptr(&ira->new_irb, source_instruction->scope, source_instruction->source_node, ptr);
5441 load_ptr_instruction->type_entry = child_type;5485 load_ptr_instruction->value.type = child_type;
5442 return load_ptr_instruction;5486 return load_ptr_instruction;
5443 } else if (type_entry->id == TypeTableEntryIdMetaType) {5487 } else if (type_entry->id == TypeTableEntryIdMetaType) {
5444 ConstExprValue *ptr_val = ir_resolve_const(ira, ptr, UndefBad);5488 ConstExprValue *ptr_val = ir_resolve_const(ira, ptr, UndefBad);
...@@ -5465,18 +5509,18 @@ static IrInstruction *ir_get_deref(IrAnalyze *ira, IrInstruction *source_instruc...@@ -5465,18 +5509,18 @@ static IrInstruction *ir_get_deref(IrAnalyze *ira, IrInstruction *source_instruc
5465static TypeTableEntry *ir_analyze_ref(IrAnalyze *ira, IrInstruction *source_instruction, IrInstruction *value,5509static TypeTableEntry *ir_analyze_ref(IrAnalyze *ira, IrInstruction *source_instruction, IrInstruction *value,
5466 bool is_const)5510 bool is_const)
5467{5511{
5468 if (value->type_entry->id == TypeTableEntryIdInvalid)5512 if (value->value.type->id == TypeTableEntryIdInvalid)
5469 return ira->codegen->builtin_types.entry_invalid;5513 return ira->codegen->builtin_types.entry_invalid;
54705514
5471 if (instr_is_comptime(value)) {5515 if (instr_is_comptime(value)) {
5472 ConstExprValue *val = ir_resolve_const(ira, value, UndefBad);5516 ConstExprValue *val = ir_resolve_const(ira, value, UndefBad);
5473 if (!val)5517 if (!val)
5474 return ira->codegen->builtin_types.entry_invalid;5518 return ira->codegen->builtin_types.entry_invalid;
5475 return ir_analyze_const_ptr(ira, source_instruction, val, value->type_entry,5519 return ir_analyze_const_ptr(ira, source_instruction, val, value->value.type,
5476 false, ConstPtrSpecialNone, is_const);5520 false, ConstPtrSpecialNone, is_const);
5477 }5521 }
54785522
5479 TypeTableEntry *ptr_type = get_pointer_to_type(ira->codegen, value->type_entry, true);5523 TypeTableEntry *ptr_type = get_pointer_to_type(ira->codegen, value->value.type, true);
5480 FnTableEntry *fn_entry = exec_fn_entry(ira->new_irb.exec);5524 FnTableEntry *fn_entry = exec_fn_entry(ira->new_irb.exec);
5481 assert(fn_entry);5525 assert(fn_entry);
5482 IrInstruction *new_instruction = ir_build_ref_from(&ira->new_irb, source_instruction, value, is_const);5526 IrInstruction *new_instruction = ir_build_ref_from(&ira->new_irb, source_instruction, value, is_const);
...@@ -5485,11 +5529,11 @@ static TypeTableEntry *ir_analyze_ref(IrAnalyze *ira, IrInstruction *source_inst...@@ -5485,11 +5529,11 @@ static TypeTableEntry *ir_analyze_ref(IrAnalyze *ira, IrInstruction *source_inst
5485}5529}
54865530
5487static bool ir_resolve_usize(IrAnalyze *ira, IrInstruction *value, uint64_t *out) {5531static bool ir_resolve_usize(IrAnalyze *ira, IrInstruction *value, uint64_t *out) {
5488 if (value->type_entry->id == TypeTableEntryIdInvalid)5532 if (value->value.type->id == TypeTableEntryIdInvalid)
5489 return false;5533 return false;
54905534
5491 IrInstruction *casted_value = ir_implicit_cast(ira, value, ira->codegen->builtin_types.entry_usize);5535 IrInstruction *casted_value = ir_implicit_cast(ira, value, ira->codegen->builtin_types.entry_usize);
5492 if (casted_value->type_entry->id == TypeTableEntryIdInvalid)5536 if (casted_value->value.type->id == TypeTableEntryIdInvalid)
5493 return false;5537 return false;
54945538
5495 ConstExprValue *const_val = ir_resolve_const(ira, casted_value, UndefBad);5539 ConstExprValue *const_val = ir_resolve_const(ira, casted_value, UndefBad);
...@@ -5501,11 +5545,11 @@ static bool ir_resolve_usize(IrAnalyze *ira, IrInstruction *value, uint64_t *out...@@ -5501,11 +5545,11 @@ static bool ir_resolve_usize(IrAnalyze *ira, IrInstruction *value, uint64_t *out
5501}5545}
55025546
5503static bool ir_resolve_bool(IrAnalyze *ira, IrInstruction *value, bool *out) {5547static bool ir_resolve_bool(IrAnalyze *ira, IrInstruction *value, bool *out) {
5504 if (value->type_entry->id == TypeTableEntryIdInvalid)5548 if (value->value.type->id == TypeTableEntryIdInvalid)
5505 return false;5549 return false;
55065550
5507 IrInstruction *casted_value = ir_implicit_cast(ira, value, ira->codegen->builtin_types.entry_bool);5551 IrInstruction *casted_value = ir_implicit_cast(ira, value, ira->codegen->builtin_types.entry_bool);
5508 if (casted_value->type_entry->id == TypeTableEntryIdInvalid)5552 if (casted_value->value.type->id == TypeTableEntryIdInvalid)
5509 return false;5553 return false;
55105554
5511 ConstExprValue *const_val = ir_resolve_const(ira, casted_value, UndefBad);5555 ConstExprValue *const_val = ir_resolve_const(ira, casted_value, UndefBad);
...@@ -5517,11 +5561,11 @@ static bool ir_resolve_bool(IrAnalyze *ira, IrInstruction *value, bool *out) {...@@ -5517,11 +5561,11 @@ static bool ir_resolve_bool(IrAnalyze *ira, IrInstruction *value, bool *out) {
5517}5561}
55185562
5519static bool ir_resolve_atomic_order(IrAnalyze *ira, IrInstruction *value, AtomicOrder *out) {5563static bool ir_resolve_atomic_order(IrAnalyze *ira, IrInstruction *value, AtomicOrder *out) {
5520 if (value->type_entry->id == TypeTableEntryIdInvalid)5564 if (value->value.type->id == TypeTableEntryIdInvalid)
5521 return false;5565 return false;
55225566
5523 IrInstruction *casted_value = ir_implicit_cast(ira, value, ira->codegen->builtin_types.entry_atomic_order_enum);5567 IrInstruction *casted_value = ir_implicit_cast(ira, value, ira->codegen->builtin_types.entry_atomic_order_enum);
5524 if (casted_value->type_entry->id == TypeTableEntryIdInvalid)5568 if (casted_value->value.type->id == TypeTableEntryIdInvalid)
5525 return false;5569 return false;
55265570
5527 ConstExprValue *const_val = ir_resolve_const(ira, casted_value, UndefBad);5571 ConstExprValue *const_val = ir_resolve_const(ira, casted_value, UndefBad);
...@@ -5533,12 +5577,12 @@ static bool ir_resolve_atomic_order(IrAnalyze *ira, IrInstruction *value, Atomic...@@ -5533,12 +5577,12 @@ static bool ir_resolve_atomic_order(IrAnalyze *ira, IrInstruction *value, Atomic
5533}5577}
55345578
5535static Buf *ir_resolve_str(IrAnalyze *ira, IrInstruction *value) {5579static Buf *ir_resolve_str(IrAnalyze *ira, IrInstruction *value) {
5536 if (value->type_entry->id == TypeTableEntryIdInvalid)5580 if (value->value.type->id == TypeTableEntryIdInvalid)
5537 return nullptr;5581 return nullptr;
55385582
5539 TypeTableEntry *str_type = get_slice_type(ira->codegen, ira->codegen->builtin_types.entry_u8, true);5583 TypeTableEntry *str_type = get_slice_type(ira->codegen, ira->codegen->builtin_types.entry_u8, true);
5540 IrInstruction *casted_value = ir_implicit_cast(ira, value, str_type);5584 IrInstruction *casted_value = ir_implicit_cast(ira, value, str_type);
5541 if (casted_value->type_entry->id == TypeTableEntryIdInvalid)5585 if (casted_value->value.type->id == TypeTableEntryIdInvalid)
5542 return nullptr;5586 return nullptr;
55435587
5544 ConstExprValue *const_val = ir_resolve_const(ira, casted_value, UndefBad);5588 ConstExprValue *const_val = ir_resolve_const(ira, casted_value, UndefBad);
...@@ -5567,7 +5611,7 @@ static TypeTableEntry *ir_analyze_instruction_return(IrAnalyze *ira,...@@ -5567,7 +5611,7 @@ static TypeTableEntry *ir_analyze_instruction_return(IrAnalyze *ira,
5567 IrInstructionReturn *return_instruction)5611 IrInstructionReturn *return_instruction)
5568{5612{
5569 IrInstruction *value = return_instruction->value->other;5613 IrInstruction *value = return_instruction->value->other;
5570 if (value->type_entry->id == TypeTableEntryIdInvalid)5614 if (value->value.type->id == TypeTableEntryIdInvalid)
5571 return ir_unreach_error(ira);5615 return ir_unreach_error(ira);
5572 ira->implicit_return_type_list.append(value);5616 ira->implicit_return_type_list.append(value);
55735617
...@@ -5580,19 +5624,19 @@ static TypeTableEntry *ir_analyze_instruction_return(IrAnalyze *ira,...@@ -5580,19 +5624,19 @@ static TypeTableEntry *ir_analyze_instruction_return(IrAnalyze *ira,
5580}5624}
55815625
5582static TypeTableEntry *ir_analyze_instruction_const(IrAnalyze *ira, IrInstructionConst *const_instruction) {5626static TypeTableEntry *ir_analyze_instruction_const(IrAnalyze *ira, IrInstructionConst *const_instruction) {
5583 bool depends_on_compile_var = const_instruction->base.static_value.depends_on_compile_var;5627 bool depends_on_compile_var = const_instruction->base.value.depends_on_compile_var;
5584 ConstExprValue *out_val = ir_build_const_from(ira, &const_instruction->base, depends_on_compile_var);5628 ConstExprValue *out_val = ir_build_const_from(ira, &const_instruction->base, depends_on_compile_var);
5585 *out_val = const_instruction->base.static_value;5629 *out_val = const_instruction->base.value;
5586 return const_instruction->base.type_entry;5630 return const_instruction->base.value.type;
5587}5631}
55885632
5589static TypeTableEntry *ir_analyze_bin_op_bool(IrAnalyze *ira, IrInstructionBinOp *bin_op_instruction) {5633static TypeTableEntry *ir_analyze_bin_op_bool(IrAnalyze *ira, IrInstructionBinOp *bin_op_instruction) {
5590 IrInstruction *op1 = bin_op_instruction->op1->other;5634 IrInstruction *op1 = bin_op_instruction->op1->other;
5591 if (op1->type_entry->id == TypeTableEntryIdInvalid)5635 if (op1->value.type->id == TypeTableEntryIdInvalid)
5592 return ira->codegen->builtin_types.entry_invalid;5636 return ira->codegen->builtin_types.entry_invalid;
55935637
5594 IrInstruction *op2 = bin_op_instruction->op2->other;5638 IrInstruction *op2 = bin_op_instruction->op2->other;
5595 if (op2->type_entry->id == TypeTableEntryIdInvalid)5639 if (op2->value.type->id == TypeTableEntryIdInvalid)
5596 return ira->codegen->builtin_types.entry_invalid;5640 return ira->codegen->builtin_types.entry_invalid;
55975641
5598 TypeTableEntry *bool_type = ira->codegen->builtin_types.entry_bool;5642 TypeTableEntry *bool_type = ira->codegen->builtin_types.entry_bool;
...@@ -5605,14 +5649,14 @@ static TypeTableEntry *ir_analyze_bin_op_bool(IrAnalyze *ira, IrInstructionBinOp...@@ -5605,14 +5649,14 @@ static TypeTableEntry *ir_analyze_bin_op_bool(IrAnalyze *ira, IrInstructionBinOp
5605 if (casted_op2 == ira->codegen->invalid_instruction)5649 if (casted_op2 == ira->codegen->invalid_instruction)
5606 return ira->codegen->builtin_types.entry_invalid;5650 return ira->codegen->builtin_types.entry_invalid;
56075651
5608 ConstExprValue *op1_val = &casted_op1->static_value;5652 ConstExprValue *op1_val = &casted_op1->value;
5609 ConstExprValue *op2_val = &casted_op2->static_value;5653 ConstExprValue *op2_val = &casted_op2->value;
5610 if (op1_val->special != ConstValSpecialRuntime && op2_val->special != ConstValSpecialRuntime) {5654 if (op1_val->special != ConstValSpecialRuntime && op2_val->special != ConstValSpecialRuntime) {
5611 bool depends_on_compile_var = op1_val->depends_on_compile_var || op2_val->depends_on_compile_var;5655 bool depends_on_compile_var = op1_val->depends_on_compile_var || op2_val->depends_on_compile_var;
5612 ConstExprValue *out_val = ir_build_const_from(ira, &bin_op_instruction->base, depends_on_compile_var);5656 ConstExprValue *out_val = ir_build_const_from(ira, &bin_op_instruction->base, depends_on_compile_var);
56135657
5614 assert(casted_op1->type_entry->id == TypeTableEntryIdBool);5658 assert(casted_op1->value.type->id == TypeTableEntryIdBool);
5615 assert(casted_op2->type_entry->id == TypeTableEntryIdBool);5659 assert(casted_op2->value.type->id == TypeTableEntryIdBool);
5616 if (bin_op_instruction->op_id == IrBinOpBoolOr) {5660 if (bin_op_instruction->op_id == IrBinOpBoolOr) {
5617 out_val->data.x_bool = op1_val->data.x_bool || op2_val->data.x_bool;5661 out_val->data.x_bool = op1_val->data.x_bool || op2_val->data.x_bool;
5618 } else if (bin_op_instruction->op_id == IrBinOpBoolAnd) {5662 } else if (bin_op_instruction->op_id == IrBinOpBoolAnd) {
...@@ -5701,8 +5745,8 @@ static TypeTableEntry *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp...@@ -5701,8 +5745,8 @@ static TypeTableEntry *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp
5701 if (casted_op2 == ira->codegen->invalid_instruction)5745 if (casted_op2 == ira->codegen->invalid_instruction)
5702 return ira->codegen->builtin_types.entry_invalid;5746 return ira->codegen->builtin_types.entry_invalid;
57035747
5704 ConstExprValue *op1_val = &casted_op1->static_value;5748 ConstExprValue *op1_val = &casted_op1->value;
5705 ConstExprValue *op2_val = &casted_op2->static_value;5749 ConstExprValue *op2_val = &casted_op2->value;
5706 if (op1_val->special != ConstValSpecialRuntime && op2_val->special != ConstValSpecialRuntime) {5750 if (op1_val->special != ConstValSpecialRuntime && op2_val->special != ConstValSpecialRuntime) {
5707 bool type_can_gt_lt_cmp = (resolved_type->id == TypeTableEntryIdNumLitFloat ||5751 bool type_can_gt_lt_cmp = (resolved_type->id == TypeTableEntryIdNumLitFloat ||
5708 resolved_type->id == TypeTableEntryIdNumLitInt ||5752 resolved_type->id == TypeTableEntryIdNumLitInt ||
...@@ -5729,7 +5773,7 @@ static TypeTableEntry *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp...@@ -5729,7 +5773,7 @@ static TypeTableEntry *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp
57295773
5730 answer = bignum_cmp(&op1_val->data.x_bignum, &op2_val->data.x_bignum);5774 answer = bignum_cmp(&op1_val->data.x_bignum, &op2_val->data.x_bignum);
5731 } else {5775 } else {
5732 bool are_equal = const_values_equal(op1_val, op2_val, resolved_type);5776 bool are_equal = const_values_equal(op1_val, op2_val);
5733 if (op_id == IrBinOpCmpEq) {5777 if (op_id == IrBinOpCmpEq) {
5734 answer = are_equal;5778 answer = are_equal;
5735 } else if (op_id == IrBinOpCmpNotEq) {5779 } else if (op_id == IrBinOpCmpNotEq) {
...@@ -5872,8 +5916,8 @@ static TypeTableEntry *ir_analyze_bin_op_math(IrAnalyze *ira, IrInstructionBinOp...@@ -5872,8 +5916,8 @@ static TypeTableEntry *ir_analyze_bin_op_math(IrAnalyze *ira, IrInstructionBinOp
5872 AstNode *source_node = bin_op_instruction->base.source_node;5916 AstNode *source_node = bin_op_instruction->base.source_node;
5873 ir_add_error_node(ira, source_node,5917 ir_add_error_node(ira, source_node,
5874 buf_sprintf("invalid operands to binary expression: '%s' and '%s'",5918 buf_sprintf("invalid operands to binary expression: '%s' and '%s'",
5875 buf_ptr(&op1->type_entry->name),5919 buf_ptr(&op1->value.type->name),
5876 buf_ptr(&op2->type_entry->name)));5920 buf_ptr(&op2->value.type->name)));
5877 return ira->codegen->builtin_types.entry_invalid;5921 return ira->codegen->builtin_types.entry_invalid;
5878 }5922 }
58795923
...@@ -5886,10 +5930,10 @@ static TypeTableEntry *ir_analyze_bin_op_math(IrAnalyze *ira, IrInstructionBinOp...@@ -5886,10 +5930,10 @@ static TypeTableEntry *ir_analyze_bin_op_math(IrAnalyze *ira, IrInstructionBinOp
5886 return ira->codegen->builtin_types.entry_invalid;5930 return ira->codegen->builtin_types.entry_invalid;
58875931
58885932
5889 if (casted_op1->static_value.special != ConstValSpecialRuntime && casted_op2->static_value.special != ConstValSpecialRuntime) {5933 if (casted_op1->value.special != ConstValSpecialRuntime && casted_op2->value.special != ConstValSpecialRuntime) {
5890 ConstExprValue *op1_val = &casted_op1->static_value;5934 ConstExprValue *op1_val = &casted_op1->value;
5891 ConstExprValue *op2_val = &casted_op2->static_value;5935 ConstExprValue *op2_val = &casted_op2->value;
5892 ConstExprValue *out_val = &bin_op_instruction->base.static_value;5936 ConstExprValue *out_val = &bin_op_instruction->base.value;
58935937
5894 bin_op_instruction->base.other = &bin_op_instruction->base;5938 bin_op_instruction->base.other = &bin_op_instruction->base;
58955939
...@@ -5919,12 +5963,12 @@ static TypeTableEntry *ir_analyze_bin_op_math(IrAnalyze *ira, IrInstructionBinOp...@@ -5919,12 +5963,12 @@ static TypeTableEntry *ir_analyze_bin_op_math(IrAnalyze *ira, IrInstructionBinOp
59195963
5920static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *instruction) {5964static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *instruction) {
5921 IrInstruction *op1 = instruction->op1->other;5965 IrInstruction *op1 = instruction->op1->other;
5922 TypeTableEntry *op1_canon_type = get_underlying_type(op1->type_entry);5966 TypeTableEntry *op1_canon_type = get_underlying_type(op1->value.type);
5923 if (op1_canon_type->id == TypeTableEntryIdInvalid)5967 if (op1_canon_type->id == TypeTableEntryIdInvalid)
5924 return ira->codegen->builtin_types.entry_invalid;5968 return ira->codegen->builtin_types.entry_invalid;
59255969
5926 IrInstruction *op2 = instruction->op2->other;5970 IrInstruction *op2 = instruction->op2->other;
5927 TypeTableEntry *op2_canon_type = get_underlying_type(op2->type_entry);5971 TypeTableEntry *op2_canon_type = get_underlying_type(op2->value.type);
5928 if (op2_canon_type->id == TypeTableEntryIdInvalid)5972 if (op2_canon_type->id == TypeTableEntryIdInvalid)
5929 return ira->codegen->builtin_types.entry_invalid;5973 return ira->codegen->builtin_types.entry_invalid;
59305974
...@@ -5955,7 +5999,7 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *...@@ -5955,7 +5999,7 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *
5955 op1_array_end = op1_array_val->data.x_array.size - 1;5999 op1_array_end = op1_array_val->data.x_array.size - 1;
5956 } else {6000 } else {
5957 ir_add_error(ira, op1,6001 ir_add_error(ira, op1,
5958 buf_sprintf("expected array or C string literal, found '%s'", buf_ptr(&op1->type_entry->name)));6002 buf_sprintf("expected array or C string literal, found '%s'", buf_ptr(&op1->value.type->name)));
5959 // TODO if meta_type is type decl, add note pointing to type decl declaration6003 // TODO if meta_type is type decl, add note pointing to type decl declaration
5960 return ira->codegen->builtin_types.entry_invalid;6004 return ira->codegen->builtin_types.entry_invalid;
5961 }6005 }
...@@ -5967,7 +6011,7 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *...@@ -5967,7 +6011,7 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *
5967 if (op2_canon_type->data.array.child_type != child_type) {6011 if (op2_canon_type->data.array.child_type != child_type) {
5968 ir_add_error(ira, op2, buf_sprintf("expected array of type '%s', found '%s'",6012 ir_add_error(ira, op2, buf_sprintf("expected array of type '%s', found '%s'",
5969 buf_ptr(&child_type->name),6013 buf_ptr(&child_type->name),
5970 buf_ptr(&op2->type_entry->name)));6014 buf_ptr(&op2->value.type->name)));
5971 return ira->codegen->builtin_types.entry_invalid;6015 return ira->codegen->builtin_types.entry_invalid;
5972 }6016 }
5973 op2_array_val = op2_val;6017 op2_array_val = op2_val;
...@@ -5980,7 +6024,7 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *...@@ -5980,7 +6024,7 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *
5980 if (child_type != ira->codegen->builtin_types.entry_u8) {6024 if (child_type != ira->codegen->builtin_types.entry_u8) {
5981 ir_add_error(ira, op2, buf_sprintf("expected array of type '%s', found '%s'",6025 ir_add_error(ira, op2, buf_sprintf("expected array of type '%s', found '%s'",
5982 buf_ptr(&child_type->name),6026 buf_ptr(&child_type->name),
5983 buf_ptr(&op2->type_entry->name)));6027 buf_ptr(&op2->value.type->name)));
5984 return ira->codegen->builtin_types.entry_invalid;6028 return ira->codegen->builtin_types.entry_invalid;
5985 }6029 }
5986 op2_array_val = op2_val->data.x_ptr.base_ptr;6030 op2_array_val = op2_val->data.x_ptr.base_ptr;
...@@ -5988,12 +6032,12 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *...@@ -5988,12 +6032,12 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *
5988 op2_array_end = op2_array_val->data.x_array.size - 1;6032 op2_array_end = op2_array_val->data.x_array.size - 1;
5989 } else {6033 } else {
5990 ir_add_error(ira, op2,6034 ir_add_error(ira, op2,
5991 buf_sprintf("expected array or C string literal, found '%s'", buf_ptr(&op1->type_entry->name)));6035 buf_sprintf("expected array or C string literal, found '%s'", buf_ptr(&op1->value.type->name)));
5992 // TODO if meta_type is type decl, add note pointing to type decl declaration6036 // TODO if meta_type is type decl, add note pointing to type decl declaration
5993 return ira->codegen->builtin_types.entry_invalid;6037 return ira->codegen->builtin_types.entry_invalid;
5994 }6038 }
59956039
5996 bool depends_on_compile_var = op1->static_value.depends_on_compile_var || op2->static_value.depends_on_compile_var;6040 bool depends_on_compile_var = op1->value.depends_on_compile_var || op2->value.depends_on_compile_var;
5997 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base, depends_on_compile_var);6041 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base, depends_on_compile_var);
59986042
5999 TypeTableEntry *result_type;6043 TypeTableEntry *result_type;
...@@ -6008,6 +6052,7 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *...@@ -6008,6 +6052,7 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *
60086052
6009 out_array_val = allocate<ConstExprValue>(1);6053 out_array_val = allocate<ConstExprValue>(1);
6010 out_array_val->special = ConstValSpecialStatic;6054 out_array_val->special = ConstValSpecialStatic;
6055 out_array_val->type = result_type;
6011 out_val->data.x_ptr.base_ptr = out_array_val;6056 out_val->data.x_ptr.base_ptr = out_array_val;
6012 out_val->data.x_ptr.index = 0;6057 out_val->data.x_ptr.index = 0;
6013 out_val->data.x_ptr.special = ConstPtrSpecialCStr;6058 out_val->data.x_ptr.special = ConstPtrSpecialCStr;
...@@ -6026,8 +6071,7 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *...@@ -6026,8 +6071,7 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *
6026 }6071 }
6027 if (next_index < new_len) {6072 if (next_index < new_len) {
6028 ConstExprValue *null_byte = &out_array_val->data.x_array.elements[next_index];6073 ConstExprValue *null_byte = &out_array_val->data.x_array.elements[next_index];
6029 null_byte->special = ConstValSpecialStatic;6074 init_const_unsigned_negative(null_byte, child_type, 0, false);
6030 bignum_init_unsigned(&null_byte->data.x_bignum, 0);
6031 next_index += 1;6075 next_index += 1;
6032 }6076 }
6033 assert(next_index == new_len);6077 assert(next_index == new_len);
...@@ -6037,11 +6081,11 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *...@@ -6037,11 +6081,11 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *
60376081
6038static TypeTableEntry *ir_analyze_array_mult(IrAnalyze *ira, IrInstructionBinOp *instruction) {6082static TypeTableEntry *ir_analyze_array_mult(IrAnalyze *ira, IrInstructionBinOp *instruction) {
6039 IrInstruction *op1 = instruction->op1->other;6083 IrInstruction *op1 = instruction->op1->other;
6040 if (op1->type_entry->id == TypeTableEntryIdInvalid)6084 if (op1->value.type->id == TypeTableEntryIdInvalid)
6041 return ira->codegen->builtin_types.entry_invalid;6085 return ira->codegen->builtin_types.entry_invalid;
60426086
6043 IrInstruction *op2 = instruction->op2->other;6087 IrInstruction *op2 = instruction->op2->other;
6044 if (op2->type_entry->id == TypeTableEntryIdInvalid)6088 if (op2->value.type->id == TypeTableEntryIdInvalid)
6045 return ira->codegen->builtin_types.entry_invalid;6089 return ira->codegen->builtin_types.entry_invalid;
60466090
6047 ConstExprValue *array_val = ir_resolve_const(ira, op1, UndefBad);6091 ConstExprValue *array_val = ir_resolve_const(ira, op1, UndefBad);
...@@ -6052,9 +6096,9 @@ static TypeTableEntry *ir_analyze_array_mult(IrAnalyze *ira, IrInstructionBinOp...@@ -6052,9 +6096,9 @@ static TypeTableEntry *ir_analyze_array_mult(IrAnalyze *ira, IrInstructionBinOp
6052 if (!ir_resolve_usize(ira, op2, &mult_amt))6096 if (!ir_resolve_usize(ira, op2, &mult_amt))
6053 return ira->codegen->builtin_types.entry_invalid;6097 return ira->codegen->builtin_types.entry_invalid;
60546098
6055 TypeTableEntry *array_canon_type = get_underlying_type(op1->type_entry);6099 TypeTableEntry *array_canon_type = get_underlying_type(op1->value.type);
6056 if (array_canon_type->id != TypeTableEntryIdArray) {6100 if (array_canon_type->id != TypeTableEntryIdArray) {
6057 ir_add_error(ira, op1, buf_sprintf("expected array type, found '%s'", buf_ptr(&op1->type_entry->name)));6101 ir_add_error(ira, op1, buf_sprintf("expected array type, found '%s'", buf_ptr(&op1->value.type->name)));
6058 // TODO if meta_type is type decl, add note pointing to type decl declaration6102 // TODO if meta_type is type decl, add note pointing to type decl declaration
6059 return ira->codegen->builtin_types.entry_invalid;6103 return ira->codegen->builtin_types.entry_invalid;
6060 }6104 }
...@@ -6068,7 +6112,7 @@ static TypeTableEntry *ir_analyze_array_mult(IrAnalyze *ira, IrInstructionBinOp...@@ -6068,7 +6112,7 @@ static TypeTableEntry *ir_analyze_array_mult(IrAnalyze *ira, IrInstructionBinOp
6068 return ira->codegen->builtin_types.entry_invalid;6112 return ira->codegen->builtin_types.entry_invalid;
6069 }6113 }
60706114
6071 bool depends_on_compile_var = op1->static_value.depends_on_compile_var || op2->static_value.depends_on_compile_var;6115 bool depends_on_compile_var = op1->value.depends_on_compile_var || op2->value.depends_on_compile_var;
6072 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base, depends_on_compile_var);6116 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base, depends_on_compile_var);
60736117
6074 uint64_t new_array_len = array_len.data.x_uint;6118 uint64_t new_array_len = array_len.data.x_uint;
...@@ -6130,9 +6174,9 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc...@@ -6130,9 +6174,9 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc
6130 VariableTableEntry *var = decl_var_instruction->var;6174 VariableTableEntry *var = decl_var_instruction->var;
61316175
6132 IrInstruction *init_value = decl_var_instruction->init_value->other;6176 IrInstruction *init_value = decl_var_instruction->init_value->other;
6133 if (init_value->type_entry->id == TypeTableEntryIdInvalid) {6177 if (init_value->value.type->id == TypeTableEntryIdInvalid) {
6134 var->type = ira->codegen->builtin_types.entry_invalid;6178 var->value.type = ira->codegen->builtin_types.entry_invalid;
6135 return var->type;6179 return var->value.type;
6136 }6180 }
61376181
6138 AstNodeVariableDeclaration *variable_declaration = &var->decl_node->data.variable_declaration;6182 AstNodeVariableDeclaration *variable_declaration = &var->decl_node->data.variable_declaration;
...@@ -6148,15 +6192,15 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc...@@ -6148,15 +6192,15 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc
6148 TypeTableEntry *proposed_type = ir_resolve_type(ira, var_type);6192 TypeTableEntry *proposed_type = ir_resolve_type(ira, var_type);
6149 explicit_type = validate_var_type(ira->codegen, var_type->source_node, proposed_type);6193 explicit_type = validate_var_type(ira->codegen, var_type->source_node, proposed_type);
6150 if (explicit_type->id == TypeTableEntryIdInvalid) {6194 if (explicit_type->id == TypeTableEntryIdInvalid) {
6151 var->type = ira->codegen->builtin_types.entry_invalid;6195 var->value.type = ira->codegen->builtin_types.entry_invalid;
6152 return var->type;6196 return var->value.type;
6153 }6197 }
6154 }6198 }
61556199
6156 AstNode *source_node = decl_var_instruction->base.source_node;6200 AstNode *source_node = decl_var_instruction->base.source_node;
61576201
6158 IrInstruction *casted_init_value = ir_implicit_cast(ira, init_value, explicit_type);6202 IrInstruction *casted_init_value = ir_implicit_cast(ira, init_value, explicit_type);
6159 TypeTableEntry *result_type = get_underlying_type(casted_init_value->type_entry);6203 TypeTableEntry *result_type = get_underlying_type(casted_init_value->value.type);
6160 switch (result_type->id) {6204 switch (result_type->id) {
6161 case TypeTableEntryIdTypeDecl:6205 case TypeTableEntryIdTypeDecl:
6162 zig_unreachable();6206 zig_unreachable();
...@@ -6165,7 +6209,7 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc...@@ -6165,7 +6209,7 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc
6165 break;6209 break;
6166 case TypeTableEntryIdNumLitFloat:6210 case TypeTableEntryIdNumLitFloat:
6167 case TypeTableEntryIdNumLitInt:6211 case TypeTableEntryIdNumLitInt:
6168 if (is_export || is_extern || casted_init_value->static_value.special == ConstValSpecialRuntime) {6212 if (is_export || is_extern || casted_init_value->value.special == ConstValSpecialRuntime) {
6169 ir_add_error_node(ira, source_node, buf_sprintf("unable to infer variable type"));6213 ir_add_error_node(ira, source_node, buf_sprintf("unable to infer variable type"));
6170 result_type = ira->codegen->builtin_types.entry_invalid;6214 result_type = ira->codegen->builtin_types.entry_invalid;
6171 }6215 }
...@@ -6180,7 +6224,7 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc...@@ -6180,7 +6224,7 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc
6180 break;6224 break;
6181 case TypeTableEntryIdMetaType:6225 case TypeTableEntryIdMetaType:
6182 case TypeTableEntryIdNamespace:6226 case TypeTableEntryIdNamespace:
6183 if (casted_init_value->static_value.special == ConstValSpecialRuntime) {6227 if (casted_init_value->value.special == ConstValSpecialRuntime) {
6184 ir_add_error_node(ira, source_node,6228 ir_add_error_node(ira, source_node,
6185 buf_sprintf("variable of type '%s' must be constant", buf_ptr(&result_type->name)));6229 buf_sprintf("variable of type '%s' must be constant", buf_ptr(&result_type->name)));
6186 result_type = ira->codegen->builtin_types.entry_invalid;6230 result_type = ira->codegen->builtin_types.entry_invalid;
...@@ -6206,16 +6250,16 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc...@@ -6206,16 +6250,16 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc
6206 break;6250 break;
6207 }6251 }
62086252
6209 var->type = result_type;6253 var->value.type = result_type;
6210 assert(var->type);6254 assert(var->value.type);
62116255
6212 bool is_comptime = ir_get_var_is_comptime(var);6256 bool is_comptime = ir_get_var_is_comptime(var);
62136257
6214 if (casted_init_value->static_value.special != ConstValSpecialRuntime) {6258 if (casted_init_value->value.special != ConstValSpecialRuntime) {
6215 if (var->mem_slot_index != SIZE_MAX) {6259 if (var->mem_slot_index != SIZE_MAX) {
6216 assert(var->mem_slot_index < ira->exec_context.mem_slot_count);6260 assert(var->mem_slot_index < ira->exec_context.mem_slot_count);
6217 ConstExprValue *mem_slot = &ira->exec_context.mem_slot_list[var->mem_slot_index];6261 ConstExprValue *mem_slot = &ira->exec_context.mem_slot_list[var->mem_slot_index];
6218 *mem_slot = casted_init_value->static_value;6262 *mem_slot = casted_init_value->value;
62196263
6220 if (is_comptime) {6264 if (is_comptime) {
6221 ir_build_const_from(ira, &decl_var_instruction->base, false);6265 ir_build_const_from(ira, &decl_var_instruction->base, false);
...@@ -6225,7 +6269,7 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc...@@ -6225,7 +6269,7 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc
6225 } else if (is_comptime) {6269 } else if (is_comptime) {
6226 ir_add_error(ira, &decl_var_instruction->base,6270 ir_add_error(ira, &decl_var_instruction->base,
6227 buf_sprintf("cannot store runtime value in compile time variable"));6271 buf_sprintf("cannot store runtime value in compile time variable"));
6228 var->type = ira->codegen->builtin_types.entry_invalid;6272 var->value.type = ira->codegen->builtin_types.entry_invalid;
6229 return ira->codegen->builtin_types.entry_invalid;6273 return ira->codegen->builtin_types.entry_invalid;
6230 }6274 }
62316275
...@@ -6249,7 +6293,7 @@ static bool ir_analyze_fn_call_inline_arg(IrAnalyze *ira, AstNode *fn_proto_node...@@ -6249,7 +6293,7 @@ static bool ir_analyze_fn_call_inline_arg(IrAnalyze *ira, AstNode *fn_proto_node
6249 return false;6293 return false;
62506294
6251 IrInstruction *casted_arg = ir_implicit_cast(ira, arg, param_type);6295 IrInstruction *casted_arg = ir_implicit_cast(ira, arg, param_type);
6252 if (casted_arg->type_entry->id == TypeTableEntryIdInvalid)6296 if (casted_arg->value.type->id == TypeTableEntryIdInvalid)
6253 return false;6297 return false;
62546298
6255 ConstExprValue *first_arg_val = ir_resolve_const(ira, casted_arg, UndefBad);6299 ConstExprValue *first_arg_val = ir_resolve_const(ira, casted_arg, UndefBad);
...@@ -6258,7 +6302,7 @@ static bool ir_analyze_fn_call_inline_arg(IrAnalyze *ira, AstNode *fn_proto_node...@@ -6258,7 +6302,7 @@ static bool ir_analyze_fn_call_inline_arg(IrAnalyze *ira, AstNode *fn_proto_node
62586302
6259 Buf *param_name = param_decl_node->data.param_decl.name;6303 Buf *param_name = param_decl_node->data.param_decl.name;
6260 VariableTableEntry *var = add_variable(ira->codegen, param_decl_node,6304 VariableTableEntry *var = add_variable(ira->codegen, param_decl_node,
6261 *exec_scope, param_name, casted_arg->type_entry, true, first_arg_val);6305 *exec_scope, param_name, true, first_arg_val);
6262 *exec_scope = var->child_scope;6306 *exec_scope = var->child_scope;
6263 *next_proto_i += 1;6307 *next_proto_i += 1;
62646308
...@@ -6278,7 +6322,7 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod...@@ -6278,7 +6322,7 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod
6278 return false;6322 return false;
62796323
6280 IrInstruction *casted_arg = ir_implicit_cast(ira, arg, param_type);6324 IrInstruction *casted_arg = ir_implicit_cast(ira, arg, param_type);
6281 if (casted_arg->type_entry->id == TypeTableEntryIdInvalid)6325 if (casted_arg->value.type->id == TypeTableEntryIdInvalid)
6282 return false;6326 return false;
62836327
6284 bool inline_arg = param_decl_node->data.param_decl.is_inline;6328 bool inline_arg = param_decl_node->data.param_decl.is_inline;
...@@ -6294,24 +6338,22 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod...@@ -6294,24 +6338,22 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod
6294 // needs to know that it depends on compile time variable data.6338 // needs to know that it depends on compile time variable data.
6295 arg_val->depends_on_compile_var = true;6339 arg_val->depends_on_compile_var = true;
6296 } else {6340 } else {
6297 arg_val = nullptr;6341 arg_val = create_const_runtime(casted_arg->value.type);
6298 }6342 }
62996343
6300 Buf *param_name = param_decl_node->data.param_decl.name;6344 Buf *param_name = param_decl_node->data.param_decl.name;
6301 VariableTableEntry *var = add_variable(ira->codegen, param_decl_node,6345 VariableTableEntry *var = add_variable(ira->codegen, param_decl_node,
6302 *child_scope, param_name, casted_arg->type_entry, true, arg_val);6346 *child_scope, param_name, true, arg_val);
6303 *child_scope = var->child_scope;6347 *child_scope = var->child_scope;
63046348
6305 if (inline_arg || is_var_type) {6349 if (inline_arg || is_var_type) {
6306 GenericParamValue *generic_param = &generic_id->params[generic_id->param_count];6350 generic_id->params[generic_id->param_count] = *arg_val;
6307 generic_param->type = casted_arg->type_entry;
6308 generic_param->value = arg_val;
6309 generic_id->param_count += 1;6351 generic_id->param_count += 1;
6310 }6352 }
6311 if (!inline_arg) {6353 if (!inline_arg) {
6312 if (type_requires_comptime(var->type)) {6354 if (type_requires_comptime(var->value.type)) {
6313 ir_add_error(ira, arg,6355 ir_add_error(ira, arg,
6314 buf_sprintf("parameter of type '%s' not allowed", buf_ptr(&var->type->name)));6356 buf_sprintf("parameter of type '%s' not allowed", buf_ptr(&var->value.type->name)));
6315 return false;6357 return false;
6316 }6358 }
63176359
...@@ -6319,7 +6361,7 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod...@@ -6319,7 +6361,7 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod
63196361
6320 casted_args[fn_type_id->param_count] = casted_arg;6362 casted_args[fn_type_id->param_count] = casted_arg;
6321 FnTypeParamInfo *param_info = &fn_type_id->param_info[fn_type_id->param_count];6363 FnTypeParamInfo *param_info = &fn_type_id->param_info[fn_type_id->param_count];
6322 param_info->type = casted_arg->type_entry;6364 param_info->type = casted_arg->value.type;
6323 param_info->is_noalias = param_decl_node->data.param_decl.is_noalias;6365 param_info->is_noalias = param_decl_node->data.param_decl.is_noalias;
6324 impl_fn->param_source_nodes[fn_type_id->param_count] = param_decl_node;6366 impl_fn->param_source_nodes[fn_type_id->param_count] = param_decl_node;
6325 fn_type_id->param_count += 1;6367 fn_type_id->param_count += 1;
...@@ -6376,7 +6418,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal...@@ -6376,7 +6418,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
6376 size_t next_proto_i = 0;6418 size_t next_proto_i = 0;
6377 if (first_arg_ptr) {6419 if (first_arg_ptr) {
6378 IrInstruction *first_arg = ir_get_deref(ira, first_arg_ptr, first_arg_ptr);6420 IrInstruction *first_arg = ir_get_deref(ira, first_arg_ptr, first_arg_ptr);
6379 if (first_arg->type_entry->id == TypeTableEntryIdInvalid)6421 if (first_arg->value.type->id == TypeTableEntryIdInvalid)
6380 return ira->codegen->builtin_types.entry_invalid;6422 return ira->codegen->builtin_types.entry_invalid;
63816423
6382 if (!ir_analyze_fn_call_inline_arg(ira, fn_proto_node, first_arg, &exec_scope, &next_proto_i))6424 if (!ir_analyze_fn_call_inline_arg(ira, fn_proto_node, first_arg, &exec_scope, &next_proto_i))
...@@ -6385,7 +6427,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal...@@ -6385,7 +6427,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
63856427
6386 for (size_t call_i = 0; call_i < call_instruction->arg_count; call_i += 1) {6428 for (size_t call_i = 0; call_i < call_instruction->arg_count; call_i += 1) {
6387 IrInstruction *old_arg = call_instruction->args[call_i]->other;6429 IrInstruction *old_arg = call_instruction->args[call_i]->other;
6388 if (old_arg->type_entry->id == TypeTableEntryIdInvalid)6430 if (old_arg->value.type->id == TypeTableEntryIdInvalid)
6389 return ira->codegen->builtin_types.entry_invalid;6431 return ira->codegen->builtin_types.entry_invalid;
63906432
6391 if (!ir_analyze_fn_call_inline_arg(ira, fn_proto_node, old_arg, &exec_scope, &next_proto_i))6433 if (!ir_analyze_fn_call_inline_arg(ira, fn_proto_node, old_arg, &exec_scope, &next_proto_i))
...@@ -6408,15 +6450,15 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal...@@ -6408,15 +6450,15 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
6408 result = ir_eval_const_value(ira->codegen, exec_scope, body_node, return_type,6450 result = ir_eval_const_value(ira->codegen, exec_scope, body_node, return_type,
6409 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota, fn_entry,6451 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota, fn_entry,
6410 nullptr, call_instruction->base.source_node, nullptr, ira->new_irb.exec);6452 nullptr, call_instruction->base.source_node, nullptr, ira->new_irb.exec);
6411 if (result->type_entry->id == TypeTableEntryIdInvalid)6453 if (result->value.type->id == TypeTableEntryIdInvalid)
6412 return ira->codegen->builtin_types.entry_invalid;6454 return ira->codegen->builtin_types.entry_invalid;
64136455
6414 ira->codegen->memoized_fn_eval_table.put(exec_scope, result);6456 ira->codegen->memoized_fn_eval_table.put(exec_scope, result);
6415 }6457 }
64166458
6417 ConstExprValue *out_val = ir_build_const_from(ira, &call_instruction->base,6459 ConstExprValue *out_val = ir_build_const_from(ira, &call_instruction->base,
6418 result->static_value.depends_on_compile_var);6460 result->value.depends_on_compile_var);
6419 *out_val = result->static_value;6461 *out_val = result->value;
6420 return ir_finish_anal(ira, return_type);6462 return ir_finish_anal(ira, return_type);
6421 }6463 }
64226464
...@@ -6441,12 +6483,12 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal...@@ -6441,12 +6483,12 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
6441 GenericFnTypeId *generic_id = allocate<GenericFnTypeId>(1);6483 GenericFnTypeId *generic_id = allocate<GenericFnTypeId>(1);
6442 generic_id->fn_entry = fn_entry;6484 generic_id->fn_entry = fn_entry;
6443 generic_id->param_count = 0;6485 generic_id->param_count = 0;
6444 generic_id->params = allocate<GenericParamValue>(src_param_count);6486 generic_id->params = allocate<ConstExprValue>(src_param_count);
6445 size_t next_proto_i = 0;6487 size_t next_proto_i = 0;
64466488
6447 if (first_arg_ptr) {6489 if (first_arg_ptr) {
6448 IrInstruction *first_arg = ir_get_deref(ira, first_arg_ptr, first_arg_ptr);6490 IrInstruction *first_arg = ir_get_deref(ira, first_arg_ptr, first_arg_ptr);
6449 if (first_arg->type_entry->id == TypeTableEntryIdInvalid)6491 if (first_arg->value.type->id == TypeTableEntryIdInvalid)
6450 return ira->codegen->builtin_types.entry_invalid;6492 return ira->codegen->builtin_types.entry_invalid;
64516493
6452 if (!ir_analyze_fn_call_generic_arg(ira, fn_proto_node, first_arg, &impl_fn->child_scope,6494 if (!ir_analyze_fn_call_generic_arg(ira, fn_proto_node, first_arg, &impl_fn->child_scope,
...@@ -6457,7 +6499,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal...@@ -6457,7 +6499,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
6457 }6499 }
6458 for (size_t call_i = 0; call_i < call_instruction->arg_count; call_i += 1) {6500 for (size_t call_i = 0; call_i < call_instruction->arg_count; call_i += 1) {
6459 IrInstruction *arg = call_instruction->args[call_i]->other;6501 IrInstruction *arg = call_instruction->args[call_i]->other;
6460 if (arg->type_entry->id == TypeTableEntryIdInvalid)6502 if (arg->value.type->id == TypeTableEntryIdInvalid)
6461 return ira->codegen->builtin_types.entry_invalid;6503 return ira->codegen->builtin_types.entry_invalid;
64626504
6463 if (!ir_analyze_fn_call_generic_arg(ira, fn_proto_node, arg, &impl_fn->child_scope,6505 if (!ir_analyze_fn_call_generic_arg(ira, fn_proto_node, arg, &impl_fn->child_scope,
...@@ -6513,7 +6555,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal...@@ -6513,7 +6555,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
6513 size_t next_arg_index = 0;6555 size_t next_arg_index = 0;
6514 if (first_arg_ptr) {6556 if (first_arg_ptr) {
6515 IrInstruction *first_arg = ir_get_deref(ira, first_arg_ptr, first_arg_ptr);6557 IrInstruction *first_arg = ir_get_deref(ira, first_arg_ptr, first_arg_ptr);
6516 if (first_arg->type_entry->id == TypeTableEntryIdInvalid)6558 if (first_arg->value.type->id == TypeTableEntryIdInvalid)
6517 return ira->codegen->builtin_types.entry_invalid;6559 return ira->codegen->builtin_types.entry_invalid;
65186560
6519 TypeTableEntry *param_type = fn_type_id->param_info[next_arg_index].type;6561 TypeTableEntry *param_type = fn_type_id->param_info[next_arg_index].type;
...@@ -6521,7 +6563,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal...@@ -6521,7 +6563,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
6521 return ira->codegen->builtin_types.entry_invalid;6563 return ira->codegen->builtin_types.entry_invalid;
65226564
6523 IrInstruction *casted_arg = ir_implicit_cast(ira, first_arg, param_type);6565 IrInstruction *casted_arg = ir_implicit_cast(ira, first_arg, param_type);
6524 if (casted_arg->type_entry->id == TypeTableEntryIdInvalid)6566 if (casted_arg->value.type->id == TypeTableEntryIdInvalid)
6525 return ira->codegen->builtin_types.entry_invalid;6567 return ira->codegen->builtin_types.entry_invalid;
65266568
6527 casted_args[next_arg_index] = casted_arg;6569 casted_args[next_arg_index] = casted_arg;
...@@ -6529,7 +6571,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal...@@ -6529,7 +6571,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
6529 }6571 }
6530 for (size_t call_i = 0; call_i < call_instruction->arg_count; call_i += 1) {6572 for (size_t call_i = 0; call_i < call_instruction->arg_count; call_i += 1) {
6531 IrInstruction *old_arg = call_instruction->args[call_i]->other;6573 IrInstruction *old_arg = call_instruction->args[call_i]->other;
6532 if (old_arg->type_entry->id == TypeTableEntryIdInvalid)6574 if (old_arg->value.type->id == TypeTableEntryIdInvalid)
6533 return ira->codegen->builtin_types.entry_invalid;6575 return ira->codegen->builtin_types.entry_invalid;
6534 IrInstruction *casted_arg;6576 IrInstruction *casted_arg;
6535 if (next_arg_index < src_param_count) {6577 if (next_arg_index < src_param_count) {
...@@ -6537,7 +6579,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal...@@ -6537,7 +6579,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
6537 if (param_type->id == TypeTableEntryIdInvalid)6579 if (param_type->id == TypeTableEntryIdInvalid)
6538 return ira->codegen->builtin_types.entry_invalid;6580 return ira->codegen->builtin_types.entry_invalid;
6539 casted_arg = ir_implicit_cast(ira, old_arg, param_type);6581 casted_arg = ir_implicit_cast(ira, old_arg, param_type);
6540 if (casted_arg->type_entry->id == TypeTableEntryIdInvalid)6582 if (casted_arg->value.type->id == TypeTableEntryIdInvalid)
6541 return ira->codegen->builtin_types.entry_invalid;6583 return ira->codegen->builtin_types.entry_invalid;
6542 } else {6584 } else {
6543 casted_arg = old_arg;6585 casted_arg = old_arg;
...@@ -6562,13 +6604,13 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal...@@ -6562,13 +6604,13 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
65626604
6563static TypeTableEntry *ir_analyze_instruction_call(IrAnalyze *ira, IrInstructionCall *call_instruction) {6605static TypeTableEntry *ir_analyze_instruction_call(IrAnalyze *ira, IrInstructionCall *call_instruction) {
6564 IrInstruction *fn_ref = call_instruction->fn_ref->other;6606 IrInstruction *fn_ref = call_instruction->fn_ref->other;
6565 if (fn_ref->type_entry->id == TypeTableEntryIdInvalid)6607 if (fn_ref->value.type->id == TypeTableEntryIdInvalid)
6566 return ira->codegen->builtin_types.entry_invalid;6608 return ira->codegen->builtin_types.entry_invalid;
65676609
6568 bool is_inline = call_instruction->is_inline || ir_should_inline(&ira->new_irb);6610 bool is_inline = call_instruction->is_inline || ir_should_inline(&ira->new_irb);
65696611
6570 if (is_inline || fn_ref->static_value.special != ConstValSpecialRuntime) {6612 if (is_inline || fn_ref->value.special != ConstValSpecialRuntime) {
6571 if (fn_ref->type_entry->id == TypeTableEntryIdMetaType) {6613 if (fn_ref->value.type->id == TypeTableEntryIdMetaType) {
6572 TypeTableEntry *dest_type = ir_resolve_type(ira, fn_ref);6614 TypeTableEntry *dest_type = ir_resolve_type(ira, fn_ref);
6573 if (dest_type->id == TypeTableEntryIdInvalid)6615 if (dest_type->id == TypeTableEntryIdInvalid)
6574 return ira->codegen->builtin_types.entry_invalid;6616 return ira->codegen->builtin_types.entry_invalid;
...@@ -6584,34 +6626,34 @@ static TypeTableEntry *ir_analyze_instruction_call(IrAnalyze *ira, IrInstruction...@@ -6584,34 +6626,34 @@ static TypeTableEntry *ir_analyze_instruction_call(IrAnalyze *ira, IrInstruction
6584 IrInstruction *arg = call_instruction->args[0]->other;6626 IrInstruction *arg = call_instruction->args[0]->other;
65856627
6586 IrInstruction *cast_instruction = ir_analyze_cast(ira, &call_instruction->base, dest_type, arg);6628 IrInstruction *cast_instruction = ir_analyze_cast(ira, &call_instruction->base, dest_type, arg);
6587 if (cast_instruction->type_entry->id == TypeTableEntryIdInvalid)6629 if (cast_instruction->value.type->id == TypeTableEntryIdInvalid)
6588 return ira->codegen->builtin_types.entry_invalid;6630 return ira->codegen->builtin_types.entry_invalid;
65896631
6590 ir_link_new_instruction(cast_instruction, &call_instruction->base);6632 ir_link_new_instruction(cast_instruction, &call_instruction->base);
6591 return ir_finish_anal(ira, cast_instruction->type_entry);6633 return ir_finish_anal(ira, cast_instruction->value.type);
6592 } else if (fn_ref->type_entry->id == TypeTableEntryIdFn) {6634 } else if (fn_ref->value.type->id == TypeTableEntryIdFn) {
6593 FnTableEntry *fn_table_entry = ir_resolve_fn(ira, fn_ref);6635 FnTableEntry *fn_table_entry = ir_resolve_fn(ira, fn_ref);
6594 return ir_analyze_fn_call(ira, call_instruction, fn_table_entry, fn_table_entry->type_entry,6636 return ir_analyze_fn_call(ira, call_instruction, fn_table_entry, fn_table_entry->type_entry,
6595 fn_ref, nullptr, is_inline);6637 fn_ref, nullptr, is_inline);
6596 } else if (fn_ref->type_entry->id == TypeTableEntryIdBoundFn) {6638 } else if (fn_ref->value.type->id == TypeTableEntryIdBoundFn) {
6597 assert(fn_ref->static_value.special == ConstValSpecialStatic);6639 assert(fn_ref->value.special == ConstValSpecialStatic);
6598 FnTableEntry *fn_table_entry = fn_ref->static_value.data.x_bound_fn.fn;6640 FnTableEntry *fn_table_entry = fn_ref->value.data.x_bound_fn.fn;
6599 IrInstruction *first_arg_ptr = fn_ref->static_value.data.x_bound_fn.first_arg;6641 IrInstruction *first_arg_ptr = fn_ref->value.data.x_bound_fn.first_arg;
6600 return ir_analyze_fn_call(ira, call_instruction, fn_table_entry, fn_table_entry->type_entry,6642 return ir_analyze_fn_call(ira, call_instruction, fn_table_entry, fn_table_entry->type_entry,
6601 nullptr, first_arg_ptr, is_inline);6643 nullptr, first_arg_ptr, is_inline);
6602 } else {6644 } else {
6603 ir_add_error_node(ira, fn_ref->source_node,6645 ir_add_error_node(ira, fn_ref->source_node,
6604 buf_sprintf("type '%s' not a function", buf_ptr(&fn_ref->type_entry->name)));6646 buf_sprintf("type '%s' not a function", buf_ptr(&fn_ref->value.type->name)));
6605 return ira->codegen->builtin_types.entry_invalid;6647 return ira->codegen->builtin_types.entry_invalid;
6606 }6648 }
6607 }6649 }
66086650
6609 if (fn_ref->type_entry->id == TypeTableEntryIdFn) {6651 if (fn_ref->value.type->id == TypeTableEntryIdFn) {
6610 return ir_analyze_fn_call(ira, call_instruction, nullptr, fn_ref->type_entry,6652 return ir_analyze_fn_call(ira, call_instruction, nullptr, fn_ref->value.type,
6611 fn_ref, nullptr, false);6653 fn_ref, nullptr, false);
6612 } else {6654 } else {
6613 ir_add_error_node(ira, fn_ref->source_node,6655 ir_add_error_node(ira, fn_ref->source_node,
6614 buf_sprintf("type '%s' not a function", buf_ptr(&fn_ref->type_entry->name)));6656 buf_sprintf("type '%s' not a function", buf_ptr(&fn_ref->value.type->name)));
6615 return ira->codegen->builtin_types.entry_invalid;6657 return ira->codegen->builtin_types.entry_invalid;
6616 }6658 }
6617}6659}
...@@ -6620,10 +6662,6 @@ static TypeTableEntry *ir_analyze_unary_prefix_op_err(IrAnalyze *ira, IrInstruct...@@ -6620,10 +6662,6 @@ static TypeTableEntry *ir_analyze_unary_prefix_op_err(IrAnalyze *ira, IrInstruct
6620 assert(un_op_instruction->op_id == IrUnOpError);6662 assert(un_op_instruction->op_id == IrUnOpError);
6621 IrInstruction *value = un_op_instruction->value->other;6663 IrInstruction *value = un_op_instruction->value->other;
66226664
6623 TypeTableEntry *type_entry = value->type_entry;
6624 if (type_entry->id == TypeTableEntryIdInvalid)
6625 return ira->codegen->builtin_types.entry_invalid;
6626
6627 TypeTableEntry *meta_type = ir_resolve_type(ira, value);6665 TypeTableEntry *meta_type = ir_resolve_type(ira, value);
6628 TypeTableEntry *underlying_meta_type = get_underlying_type(meta_type);6666 TypeTableEntry *underlying_meta_type = get_underlying_type(meta_type);
6629 switch (underlying_meta_type->id) {6667 switch (underlying_meta_type->id) {
...@@ -6648,7 +6686,7 @@ static TypeTableEntry *ir_analyze_unary_prefix_op_err(IrAnalyze *ira, IrInstruct...@@ -6648,7 +6686,7 @@ static TypeTableEntry *ir_analyze_unary_prefix_op_err(IrAnalyze *ira, IrInstruct
6648 case TypeTableEntryIdEnumTag:6686 case TypeTableEntryIdEnumTag:
6649 {6687 {
6650 ConstExprValue *out_val = ir_build_const_from(ira, &un_op_instruction->base,6688 ConstExprValue *out_val = ir_build_const_from(ira, &un_op_instruction->base,
6651 value->static_value.depends_on_compile_var);6689 value->value.depends_on_compile_var);
6652 TypeTableEntry *result_type = get_error_type(ira->codegen, meta_type);6690 TypeTableEntry *result_type = get_error_type(ira->codegen, meta_type);
6653 out_val->data.x_type = result_type;6691 out_val->data.x_type = result_type;
6654 return ira->codegen->builtin_types.entry_type;6692 return ira->codegen->builtin_types.entry_type;
...@@ -6674,7 +6712,7 @@ static TypeTableEntry *ir_analyze_unary_prefix_op_err(IrAnalyze *ira, IrInstruct...@@ -6674,7 +6712,7 @@ static TypeTableEntry *ir_analyze_unary_prefix_op_err(IrAnalyze *ira, IrInstruct
6674static TypeTableEntry *ir_analyze_dereference(IrAnalyze *ira, IrInstructionUnOp *un_op_instruction) {6712static TypeTableEntry *ir_analyze_dereference(IrAnalyze *ira, IrInstructionUnOp *un_op_instruction) {
6675 IrInstruction *value = un_op_instruction->value->other;6713 IrInstruction *value = un_op_instruction->value->other;
66766714
6677 TypeTableEntry *ptr_type = value->type_entry;6715 TypeTableEntry *ptr_type = value->value.type;
6678 TypeTableEntry *child_type;6716 TypeTableEntry *child_type;
6679 if (ptr_type->id == TypeTableEntryIdInvalid) {6717 if (ptr_type->id == TypeTableEntryIdInvalid) {
6680 return ira->codegen->builtin_types.entry_invalid;6718 return ira->codegen->builtin_types.entry_invalid;
...@@ -6690,9 +6728,9 @@ static TypeTableEntry *ir_analyze_dereference(IrAnalyze *ira, IrInstructionUnOp...@@ -6690,9 +6728,9 @@ static TypeTableEntry *ir_analyze_dereference(IrAnalyze *ira, IrInstructionUnOp
6690 // this dereference is always an rvalue because in the IR gen we identify lvalue and emit6728 // this dereference is always an rvalue because in the IR gen we identify lvalue and emit
6691 // one of the ptr instructions6729 // one of the ptr instructions
66926730
6693 if (value->static_value.special != ConstValSpecialRuntime) {6731 if (value->value.special != ConstValSpecialRuntime) {
6694 ConstExprValue *out_val = ir_build_const_from(ira, &un_op_instruction->base, false);6732 ConstExprValue *out_val = ir_build_const_from(ira, &un_op_instruction->base, false);
6695 ConstExprValue *pointee = const_ptr_pointee(&value->static_value);6733 ConstExprValue *pointee = const_ptr_pointee(&value->value);
6696 *out_val = *pointee;6734 *out_val = *pointee;
6697 return child_type;6735 return child_type;
6698 }6736 }
...@@ -6735,7 +6773,7 @@ static TypeTableEntry *ir_analyze_maybe(IrAnalyze *ira, IrInstructionUnOp *un_op...@@ -6735,7 +6773,7 @@ static TypeTableEntry *ir_analyze_maybe(IrAnalyze *ira, IrInstructionUnOp *un_op
6735 case TypeTableEntryIdEnumTag:6773 case TypeTableEntryIdEnumTag:
6736 {6774 {
6737 ConstExprValue *out_val = ir_build_const_from(ira, &un_op_instruction->base,6775 ConstExprValue *out_val = ir_build_const_from(ira, &un_op_instruction->base,
6738 value->static_value.depends_on_compile_var);6776 value->value.depends_on_compile_var);
6739 out_val->data.x_type = get_maybe_type(ira->codegen, type_entry);6777 out_val->data.x_type = get_maybe_type(ira->codegen, type_entry);
6740 return ira->codegen->builtin_types.entry_type;6778 return ira->codegen->builtin_types.entry_type;
6741 }6779 }
...@@ -6750,7 +6788,7 @@ static TypeTableEntry *ir_analyze_maybe(IrAnalyze *ira, IrInstructionUnOp *un_op...@@ -6750,7 +6788,7 @@ static TypeTableEntry *ir_analyze_maybe(IrAnalyze *ira, IrInstructionUnOp *un_op
67506788
6751static TypeTableEntry *ir_analyze_negation(IrAnalyze *ira, IrInstructionUnOp *un_op_instruction) {6789static TypeTableEntry *ir_analyze_negation(IrAnalyze *ira, IrInstructionUnOp *un_op_instruction) {
6752 IrInstruction *value = un_op_instruction->value->other;6790 IrInstruction *value = un_op_instruction->value->other;
6753 TypeTableEntry *expr_type = value->type_entry;6791 TypeTableEntry *expr_type = value->value.type;
6754 if (expr_type->id == TypeTableEntryIdInvalid)6792 if (expr_type->id == TypeTableEntryIdInvalid)
6755 return ira->codegen->builtin_types.entry_invalid;6793 return ira->codegen->builtin_types.entry_invalid;
67566794
...@@ -6765,7 +6803,7 @@ static TypeTableEntry *ir_analyze_negation(IrAnalyze *ira, IrInstructionUnOp *un...@@ -6765,7 +6803,7 @@ static TypeTableEntry *ir_analyze_negation(IrAnalyze *ira, IrInstructionUnOp *un
6765 if (!target_const_val)6803 if (!target_const_val)
6766 return ira->codegen->builtin_types.entry_invalid;6804 return ira->codegen->builtin_types.entry_invalid;
67676805
6768 bool depends_on_compile_var = value->static_value.depends_on_compile_var;6806 bool depends_on_compile_var = value->value.depends_on_compile_var;
6769 ConstExprValue *out_val = ir_build_const_from(ira, &un_op_instruction->base, depends_on_compile_var);6807 ConstExprValue *out_val = ir_build_const_from(ira, &un_op_instruction->base, depends_on_compile_var);
6770 bignum_negate(&out_val->data.x_bignum, &target_const_val->data.x_bignum);6808 bignum_negate(&out_val->data.x_bignum, &target_const_val->data.x_bignum);
6771 if (expr_type->id == TypeTableEntryIdFloat ||6809 if (expr_type->id == TypeTableEntryIdFloat ||
...@@ -6846,7 +6884,7 @@ static TypeTableEntry *ir_analyze_instruction_br(IrAnalyze *ira, IrInstructionBr...@@ -6846,7 +6884,7 @@ static TypeTableEntry *ir_analyze_instruction_br(IrAnalyze *ira, IrInstructionBr
68466884
6847static TypeTableEntry *ir_analyze_instruction_cond_br(IrAnalyze *ira, IrInstructionCondBr *cond_br_instruction) {6885static TypeTableEntry *ir_analyze_instruction_cond_br(IrAnalyze *ira, IrInstructionCondBr *cond_br_instruction) {
6848 IrInstruction *condition = cond_br_instruction->condition->other;6886 IrInstruction *condition = cond_br_instruction->condition->other;
6849 if (condition->type_entry->id == TypeTableEntryIdInvalid)6887 if (condition->value.type->id == TypeTableEntryIdInvalid)
6850 return ir_unreach_error(ira);6888 return ir_unreach_error(ira);
68516889
6852 bool is_comptime;6890 bool is_comptime;
...@@ -6891,18 +6929,18 @@ static TypeTableEntry *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionP...@@ -6891,18 +6929,18 @@ static TypeTableEntry *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionP
6891 if (predecessor != ira->const_predecessor_bb)6929 if (predecessor != ira->const_predecessor_bb)
6892 continue;6930 continue;
6893 IrInstruction *value = phi_instruction->incoming_values[i]->other;6931 IrInstruction *value = phi_instruction->incoming_values[i]->other;
6894 assert(value->type_entry);6932 assert(value->value.type);
6895 if (value->type_entry->id == TypeTableEntryIdInvalid)6933 if (value->value.type->id == TypeTableEntryIdInvalid)
6896 return ira->codegen->builtin_types.entry_invalid;6934 return ira->codegen->builtin_types.entry_invalid;
68976935
6898 if (value->static_value.special != ConstValSpecialRuntime) {6936 if (value->value.special != ConstValSpecialRuntime) {
6899 ConstExprValue *out_val = ir_build_const_from(ira, &phi_instruction->base,6937 ConstExprValue *out_val = ir_build_const_from(ira, &phi_instruction->base,
6900 value->static_value.depends_on_compile_var);6938 value->value.depends_on_compile_var);
6901 *out_val = value->static_value;6939 *out_val = value->value;
6902 } else {6940 } else {
6903 phi_instruction->base.other = value;6941 phi_instruction->base.other = value;
6904 }6942 }
6905 return value->type_entry;6943 return value->value.type;
6906 }6944 }
6907 zig_unreachable();6945 zig_unreachable();
6908 }6946 }
...@@ -6919,10 +6957,10 @@ static TypeTableEntry *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionP...@@ -6919,10 +6957,10 @@ static TypeTableEntry *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionP
6919 IrInstruction *old_value = phi_instruction->incoming_values[i];6957 IrInstruction *old_value = phi_instruction->incoming_values[i];
6920 assert(old_value);6958 assert(old_value);
6921 IrInstruction *new_value = old_value->other;6959 IrInstruction *new_value = old_value->other;
6922 if (!new_value || new_value->type_entry->id == TypeTableEntryIdUnreachable)6960 if (!new_value || new_value->value.type->id == TypeTableEntryIdUnreachable)
6923 continue;6961 continue;
69246962
6925 if (new_value->type_entry->id == TypeTableEntryIdInvalid)6963 if (new_value->value.type->id == TypeTableEntryIdInvalid)
6926 return ira->codegen->builtin_types.entry_invalid;6964 return ira->codegen->builtin_types.entry_invalid;
69276965
69286966
...@@ -6935,7 +6973,7 @@ static TypeTableEntry *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionP...@@ -6935,7 +6973,7 @@ static TypeTableEntry *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionP
6935 if (new_incoming_blocks.length == 1) {6973 if (new_incoming_blocks.length == 1) {
6936 IrInstruction *first_value = new_incoming_values.at(0);6974 IrInstruction *first_value = new_incoming_values.at(0);
6937 phi_instruction->base.other = first_value;6975 phi_instruction->base.other = first_value;
6938 return first_value->type_entry;6976 return first_value->value.type;
6939 }6977 }
69406978
6941 TypeTableEntry *resolved_type = ir_resolve_peer_types(ira, phi_instruction->base.source_node,6979 TypeTableEntry *resolved_type = ir_resolve_peer_types(ira, phi_instruction->base.source_node,
...@@ -6973,16 +7011,16 @@ static TypeTableEntry *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionP...@@ -6973,16 +7011,16 @@ static TypeTableEntry *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionP
6973}7011}
69747012
6975static TypeTableEntry *ir_analyze_var_ptr(IrAnalyze *ira, IrInstruction *instruction, VariableTableEntry *var) {7013static TypeTableEntry *ir_analyze_var_ptr(IrAnalyze *ira, IrInstruction *instruction, VariableTableEntry *var) {
6976 assert(var->type);7014 assert(var->value.type);
6977 if (var->type->id == TypeTableEntryIdInvalid)7015 if (var->value.type->id == TypeTableEntryIdInvalid)
6978 return var->type;7016 return var->value.type;
69797017
6980 bool is_comptime = ir_get_var_is_comptime(var);7018 bool is_comptime = ir_get_var_is_comptime(var);
69817019
6982 ConstExprValue *mem_slot = nullptr;7020 ConstExprValue *mem_slot = nullptr;
6983 FnTableEntry *fn_entry = scope_fn_entry(var->parent_scope);7021 FnTableEntry *fn_entry = scope_fn_entry(var->parent_scope);
6984 if (var->src_is_const && var->value) {7022 if (var->src_is_const && var->value.special == ConstValSpecialStatic) {
6985 mem_slot = var->value;7023 mem_slot = &var->value;
6986 assert(mem_slot->special != ConstValSpecialRuntime);7024 assert(mem_slot->special != ConstValSpecialRuntime);
6987 } else if (fn_entry) {7025 } else if (fn_entry) {
6988 // TODO once the analyze code is fully ported over to IR we won't need this SIZE_MAX thing.7026 // TODO once the analyze code is fully ported over to IR we won't need this SIZE_MAX thing.
...@@ -6992,10 +7030,10 @@ static TypeTableEntry *ir_analyze_var_ptr(IrAnalyze *ira, IrInstruction *instruc...@@ -6992,10 +7030,10 @@ static TypeTableEntry *ir_analyze_var_ptr(IrAnalyze *ira, IrInstruction *instruc
69927030
6993 if (mem_slot && mem_slot->special != ConstValSpecialRuntime) {7031 if (mem_slot && mem_slot->special != ConstValSpecialRuntime) {
6994 ConstPtrSpecial ptr_special = is_comptime ? ConstPtrSpecialInline : ConstPtrSpecialNone;7032 ConstPtrSpecial ptr_special = is_comptime ? ConstPtrSpecialInline : ConstPtrSpecialNone;
6995 return ir_analyze_const_ptr(ira, instruction, mem_slot, var->type, false, ptr_special, var->src_is_const);7033 return ir_analyze_const_ptr(ira, instruction, mem_slot, var->value.type, false, ptr_special, var->src_is_const);
6996 } else {7034 } else {
6997 ir_build_var_ptr_from(&ira->new_irb, instruction, var);7035 ir_build_var_ptr_from(&ira->new_irb, instruction, var);
6998 return get_pointer_to_type(ira->codegen, var->type, var->src_is_const);7036 return get_pointer_to_type(ira->codegen, var->value.type, var->src_is_const);
6999 }7037 }
7000}7038}
70017039
...@@ -7006,15 +7044,15 @@ static TypeTableEntry *ir_analyze_instruction_var_ptr(IrAnalyze *ira, IrInstruct...@@ -7006,15 +7044,15 @@ static TypeTableEntry *ir_analyze_instruction_var_ptr(IrAnalyze *ira, IrInstruct
70067044
7007static TypeTableEntry *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstructionElemPtr *elem_ptr_instruction) {7045static TypeTableEntry *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstructionElemPtr *elem_ptr_instruction) {
7008 IrInstruction *array_ptr = elem_ptr_instruction->array_ptr->other;7046 IrInstruction *array_ptr = elem_ptr_instruction->array_ptr->other;
7009 if (array_ptr->type_entry->id == TypeTableEntryIdInvalid)7047 if (array_ptr->value.type->id == TypeTableEntryIdInvalid)
7010 return ira->codegen->builtin_types.entry_invalid;7048 return ira->codegen->builtin_types.entry_invalid;
70117049
7012 IrInstruction *elem_index = elem_ptr_instruction->elem_index->other;7050 IrInstruction *elem_index = elem_ptr_instruction->elem_index->other;
7013 if (elem_index->type_entry->id == TypeTableEntryIdInvalid)7051 if (elem_index->value.type->id == TypeTableEntryIdInvalid)
7014 return ira->codegen->builtin_types.entry_invalid;7052 return ira->codegen->builtin_types.entry_invalid;
70157053
7016 // This will be a pointer type because elem ptr IR instruction operates on a pointer to a thing.7054 // This will be a pointer type because elem ptr IR instruction operates on a pointer to a thing.
7017 TypeTableEntry *ptr_type = array_ptr->type_entry;7055 TypeTableEntry *ptr_type = array_ptr->value.type;
7018 assert(ptr_type->id == TypeTableEntryIdPointer);7056 assert(ptr_type->id == TypeTableEntryIdPointer);
70197057
7020 TypeTableEntry *array_type = ptr_type->data.pointer.child_type;7058 TypeTableEntry *array_type = ptr_type->data.pointer.child_type;
...@@ -7045,8 +7083,8 @@ static TypeTableEntry *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruc...@@ -7045,8 +7083,8 @@ static TypeTableEntry *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruc
7045 return ira->codegen->builtin_types.entry_invalid;7083 return ira->codegen->builtin_types.entry_invalid;
70467084
7047 bool safety_check_on = elem_ptr_instruction->safety_check_on;7085 bool safety_check_on = elem_ptr_instruction->safety_check_on;
7048 if (casted_elem_index->static_value.special != ConstValSpecialRuntime) {7086 if (casted_elem_index->value.special != ConstValSpecialRuntime) {
7049 uint64_t index = casted_elem_index->static_value.data.x_bignum.data.x_uint;7087 uint64_t index = casted_elem_index->value.data.x_bignum.data.x_uint;
7050 if (array_type->id == TypeTableEntryIdArray) {7088 if (array_type->id == TypeTableEntryIdArray) {
7051 uint64_t array_len = array_type->data.array.len;7089 uint64_t array_len = array_type->data.array.len;
7052 if (index >= array_len) {7090 if (index >= array_len) {
...@@ -7059,12 +7097,12 @@ static TypeTableEntry *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruc...@@ -7059,12 +7097,12 @@ static TypeTableEntry *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruc
7059 }7097 }
70607098
7061 ConstExprValue *array_ptr_val;7099 ConstExprValue *array_ptr_val;
7062 if (array_ptr->static_value.special != ConstValSpecialRuntime &&7100 if (array_ptr->value.special != ConstValSpecialRuntime &&
7063 (array_ptr_val = const_ptr_pointee(&array_ptr->static_value)) &&7101 (array_ptr_val = const_ptr_pointee(&array_ptr->value)) &&
7064 array_ptr_val->special != ConstValSpecialRuntime)7102 array_ptr_val->special != ConstValSpecialRuntime)
7065 {7103 {
7066 bool depends_on_compile_var = array_ptr_val->depends_on_compile_var ||7104 bool depends_on_compile_var = array_ptr_val->depends_on_compile_var ||
7067 casted_elem_index->static_value.depends_on_compile_var;7105 casted_elem_index->value.depends_on_compile_var;
7068 ConstExprValue *out_val = ir_build_const_from(ira, &elem_ptr_instruction->base, depends_on_compile_var);7106 ConstExprValue *out_val = ir_build_const_from(ira, &elem_ptr_instruction->base, depends_on_compile_var);
7069 if (array_type->id == TypeTableEntryIdPointer) {7107 if (array_type->id == TypeTableEntryIdPointer) {
7070 size_t offset = array_ptr_val->data.x_ptr.index;7108 size_t offset = array_ptr_val->data.x_ptr.index;
...@@ -7136,7 +7174,7 @@ static TypeTableEntry *ir_analyze_container_member_access_inner(IrAnalyze *ira,...@@ -7136,7 +7174,7 @@ static TypeTableEntry *ir_analyze_container_member_access_inner(IrAnalyze *ira,
7136 return ira->codegen->builtin_types.entry_invalid;7174 return ira->codegen->builtin_types.entry_invalid;
7137 TldFn *tld_fn = (TldFn *)tld;7175 TldFn *tld_fn = (TldFn *)tld;
7138 FnTableEntry *fn_entry = tld_fn->fn_entry;7176 FnTableEntry *fn_entry = tld_fn->fn_entry;
7139 bool depends_on_compile_var = container_ptr->static_value.depends_on_compile_var;7177 bool depends_on_compile_var = container_ptr->value.depends_on_compile_var;
7140 IrInstruction *bound_fn_value = ir_build_const_bound_fn(&ira->new_irb, field_ptr_instruction->base.scope,7178 IrInstruction *bound_fn_value = ir_build_const_bound_fn(&ira->new_irb, field_ptr_instruction->base.scope,
7141 field_ptr_instruction->base.source_node, fn_entry, container_ptr, depends_on_compile_var);7179 field_ptr_instruction->base.source_node, fn_entry, container_ptr, depends_on_compile_var);
7142 return ir_analyze_ref(ira, &field_ptr_instruction->base, bound_fn_value, true);7180 return ir_analyze_ref(ira, &field_ptr_instruction->base, bound_fn_value, true);
...@@ -7206,6 +7244,7 @@ static TypeTableEntry *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source...@@ -7206,6 +7244,7 @@ static TypeTableEntry *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source
7206 // the same one every time7244 // the same one every time
7207 ConstExprValue *const_val = allocate<ConstExprValue>(1);7245 ConstExprValue *const_val = allocate<ConstExprValue>(1);
7208 const_val->special = ConstValSpecialStatic;7246 const_val->special = ConstValSpecialStatic;
7247 const_val->type = fn_entry->type_entry;
7209 const_val->data.x_fn = fn_entry;7248 const_val->data.x_fn = fn_entry;
72107249
7211 bool ptr_is_const = true;7250 bool ptr_is_const = true;
...@@ -7221,6 +7260,7 @@ static TypeTableEntry *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source...@@ -7221,6 +7260,7 @@ static TypeTableEntry *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source
7221 // the same one every time7260 // the same one every time
7222 ConstExprValue *const_val = allocate<ConstExprValue>(1);7261 ConstExprValue *const_val = allocate<ConstExprValue>(1);
7223 const_val->special = ConstValSpecialStatic;7262 const_val->special = ConstValSpecialStatic;
7263 const_val->type = ira->codegen->builtin_types.entry_type;
7224 const_val->data.x_type = tld_typedef->type_entry;7264 const_val->data.x_type = tld_typedef->type_entry;
72257265
7226 bool ptr_is_const = true;7266 bool ptr_is_const = true;
...@@ -7233,26 +7273,26 @@ static TypeTableEntry *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source...@@ -7233,26 +7273,26 @@ static TypeTableEntry *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source
72337273
7234static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstructionFieldPtr *field_ptr_instruction) {7274static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstructionFieldPtr *field_ptr_instruction) {
7235 IrInstruction *container_ptr = field_ptr_instruction->container_ptr->other;7275 IrInstruction *container_ptr = field_ptr_instruction->container_ptr->other;
7236 if (container_ptr->type_entry->id == TypeTableEntryIdInvalid)7276 if (container_ptr->value.type->id == TypeTableEntryIdInvalid)
7237 return ira->codegen->builtin_types.entry_invalid;7277 return ira->codegen->builtin_types.entry_invalid;
72387278
7239 TypeTableEntry *container_type;7279 TypeTableEntry *container_type;
7240 if (container_ptr->type_entry->id == TypeTableEntryIdPointer) {7280 if (container_ptr->value.type->id == TypeTableEntryIdPointer) {
7241 container_type = container_ptr->type_entry->data.pointer.child_type;7281 container_type = container_ptr->value.type->data.pointer.child_type;
7242 } else if (container_ptr->type_entry->id == TypeTableEntryIdMetaType) {7282 } else if (container_ptr->value.type->id == TypeTableEntryIdMetaType) {
7243 container_type = container_ptr->type_entry;7283 container_type = container_ptr->value.type;
7244 } else {7284 } else {
7245 zig_unreachable();7285 zig_unreachable();
7246 }7286 }
72477287
7248 bool depends_on_compile_var = container_ptr->static_value.depends_on_compile_var;7288 bool depends_on_compile_var = container_ptr->value.depends_on_compile_var;
7249 Buf *field_name = field_ptr_instruction->field_name;7289 Buf *field_name = field_ptr_instruction->field_name;
7250 AstNode *source_node = field_ptr_instruction->base.source_node;7290 AstNode *source_node = field_ptr_instruction->base.source_node;
72517291
7252 if (container_type->id == TypeTableEntryIdInvalid) {7292 if (container_type->id == TypeTableEntryIdInvalid) {
7253 return container_type;7293 return container_type;
7254 } else if (is_container_ref(container_type)) {7294 } else if (is_container_ref(container_type)) {
7255 assert(container_ptr->type_entry->id == TypeTableEntryIdPointer);7295 assert(container_ptr->value.type->id == TypeTableEntryIdPointer);
7256 if (container_type->id == TypeTableEntryIdPointer) {7296 if (container_type->id == TypeTableEntryIdPointer) {
7257 TypeTableEntry *bare_type = container_ref_type(container_type);7297 TypeTableEntry *bare_type = container_ref_type(container_type);
7258 IrInstruction *container_child = ir_get_deref(ira, &field_ptr_instruction->base, container_ptr);7298 IrInstruction *container_child = ir_get_deref(ira, &field_ptr_instruction->base, container_ptr);
...@@ -7263,8 +7303,7 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru...@@ -7263,8 +7303,7 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru
7263 } else if (container_type->id == TypeTableEntryIdArray) {7303 } else if (container_type->id == TypeTableEntryIdArray) {
7264 if (buf_eql_str(field_name, "len")) {7304 if (buf_eql_str(field_name, "len")) {
7265 ConstExprValue *len_val = allocate<ConstExprValue>(1);7305 ConstExprValue *len_val = allocate<ConstExprValue>(1);
7266 len_val->special = ConstValSpecialStatic;7306 init_const_usize(ira->codegen, len_val, container_type->data.array.len);
7267 bignum_init_unsigned(&len_val->data.x_bignum, container_type->data.array.len);
72687307
7269 TypeTableEntry *usize = ira->codegen->builtin_types.entry_usize;7308 TypeTableEntry *usize = ira->codegen->builtin_types.entry_usize;
7270 bool ptr_is_const = true;7309 bool ptr_is_const = true;
...@@ -7282,11 +7321,11 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru...@@ -7282,11 +7321,11 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru
7282 return ira->codegen->builtin_types.entry_invalid;7321 return ira->codegen->builtin_types.entry_invalid;
72837322
7284 TypeTableEntry *child_type;7323 TypeTableEntry *child_type;
7285 if (container_ptr->type_entry->id == TypeTableEntryIdMetaType) {7324 if (container_ptr->value.type->id == TypeTableEntryIdMetaType) {
7286 TypeTableEntry *ptr_type = container_ptr_val->data.x_type;7325 TypeTableEntry *ptr_type = container_ptr_val->data.x_type;
7287 assert(ptr_type->id == TypeTableEntryIdPointer);7326 assert(ptr_type->id == TypeTableEntryIdPointer);
7288 child_type = ptr_type->data.pointer.child_type;7327 child_type = ptr_type->data.pointer.child_type;
7289 } else if (container_ptr->type_entry->id == TypeTableEntryIdPointer) {7328 } else if (container_ptr->value.type->id == TypeTableEntryIdPointer) {
7290 ConstExprValue *child_val = const_ptr_pointee(container_ptr_val);7329 ConstExprValue *child_val = const_ptr_pointee(container_ptr_val);
7291 child_type = child_val->data.x_type;7330 child_type = child_val->data.x_type;
7292 } else {7331 } else {
...@@ -7303,14 +7342,14 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru...@@ -7303,14 +7342,14 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru
7303 if (field->type_entry->id == TypeTableEntryIdVoid) {7342 if (field->type_entry->id == TypeTableEntryIdVoid) {
7304 bool ptr_is_const = true;7343 bool ptr_is_const = true;
7305 return ir_analyze_const_ptr(ira, &field_ptr_instruction->base,7344 return ir_analyze_const_ptr(ira, &field_ptr_instruction->base,
7306 create_const_enum_tag(field->value), child_type, depends_on_compile_var,7345 create_const_enum_tag(child_type, field->value), child_type, depends_on_compile_var,
7307 ConstPtrSpecialNone, ptr_is_const);7346 ConstPtrSpecialNone, ptr_is_const);
7308 } else {7347 } else {
7309 bool ptr_is_const = true;7348 bool ptr_is_const = true;
7310 return ir_analyze_const_ptr(ira, &field_ptr_instruction->base,7349 return ir_analyze_const_ptr(ira, &field_ptr_instruction->base,
7311 create_const_unsigned_negative(field->value, false),7350 create_const_unsigned_negative(child_type->data.enumeration.tag_type, field->value, false),
7312 child_type->data.enumeration.tag_type, depends_on_compile_var,7351 child_type->data.enumeration.tag_type, depends_on_compile_var,
7313 ConstPtrSpecialNone, ptr_is_const);7352 ConstPtrSpecialNone, ptr_is_const);
7314 }7353 }
7315 }7354 }
7316 }7355 }
...@@ -7329,6 +7368,7 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru...@@ -7329,6 +7368,7 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru
7329 if (err_table_entry) {7368 if (err_table_entry) {
7330 ConstExprValue *const_val = allocate<ConstExprValue>(1);7369 ConstExprValue *const_val = allocate<ConstExprValue>(1);
7331 const_val->special = ConstValSpecialStatic;7370 const_val->special = ConstValSpecialStatic;
7371 const_val->type = child_type;
7332 const_val->data.x_pure_err = err_table_entry->value;7372 const_val->data.x_pure_err = err_table_entry->value;
73337373
7334 bool ptr_is_const = true;7374 bool ptr_is_const = true;
...@@ -7343,13 +7383,14 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru...@@ -7343,13 +7383,14 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru
7343 if (buf_eql_str(field_name, "bit_count")) {7383 if (buf_eql_str(field_name, "bit_count")) {
7344 bool ptr_is_const = true;7384 bool ptr_is_const = true;
7345 return ir_analyze_const_ptr(ira, &field_ptr_instruction->base,7385 return ir_analyze_const_ptr(ira, &field_ptr_instruction->base,
7346 create_const_unsigned_negative(child_type->data.integral.bit_count, false),7386 create_const_unsigned_negative(ira->codegen->builtin_types.entry_num_lit_int,
7387 child_type->data.integral.bit_count, false),
7347 ira->codegen->builtin_types.entry_num_lit_int, depends_on_compile_var,7388 ira->codegen->builtin_types.entry_num_lit_int, depends_on_compile_var,
7348 ConstPtrSpecialNone, ptr_is_const);7389 ConstPtrSpecialNone, ptr_is_const);
7349 } else if (buf_eql_str(field_name, "is_signed")) {7390 } else if (buf_eql_str(field_name, "is_signed")) {
7350 bool ptr_is_const = true;7391 bool ptr_is_const = true;
7351 return ir_analyze_const_ptr(ira, &field_ptr_instruction->base,7392 return ir_analyze_const_ptr(ira, &field_ptr_instruction->base,
7352 create_const_bool(child_type->data.integral.is_signed),7393 create_const_bool(ira->codegen, child_type->data.integral.is_signed),
7353 ira->codegen->builtin_types.entry_bool, depends_on_compile_var,7394 ira->codegen->builtin_types.entry_bool, depends_on_compile_var,
7354 ConstPtrSpecialNone, ptr_is_const);7395 ConstPtrSpecialNone, ptr_is_const);
7355 } else {7396 } else {
...@@ -7364,7 +7405,7 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru...@@ -7364,7 +7405,7 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru
7364 return ira->codegen->builtin_types.entry_invalid;7405 return ira->codegen->builtin_types.entry_invalid;
7365 }7406 }
7366 } else if (container_type->id == TypeTableEntryIdNamespace) {7407 } else if (container_type->id == TypeTableEntryIdNamespace) {
7367 assert(container_ptr->type_entry->id == TypeTableEntryIdPointer);7408 assert(container_ptr->value.type->id == TypeTableEntryIdPointer);
7368 ConstExprValue *container_ptr_val = ir_resolve_const(ira, container_ptr, UndefBad);7409 ConstExprValue *container_ptr_val = ir_resolve_const(ira, container_ptr, UndefBad);
7369 if (!container_ptr_val)7410 if (!container_ptr_val)
7370 return ira->codegen->builtin_types.entry_invalid;7411 return ira->codegen->builtin_types.entry_invalid;
...@@ -7414,30 +7455,30 @@ static TypeTableEntry *ir_analyze_instruction_load_ptr(IrAnalyze *ira, IrInstruc...@@ -7414,30 +7455,30 @@ static TypeTableEntry *ir_analyze_instruction_load_ptr(IrAnalyze *ira, IrInstruc
7414 IrInstruction *ptr = load_ptr_instruction->ptr->other;7455 IrInstruction *ptr = load_ptr_instruction->ptr->other;
7415 IrInstruction *result = ir_get_deref(ira, &load_ptr_instruction->base, ptr);7456 IrInstruction *result = ir_get_deref(ira, &load_ptr_instruction->base, ptr);
7416 ir_link_new_instruction(result, &load_ptr_instruction->base);7457 ir_link_new_instruction(result, &load_ptr_instruction->base);
7417 assert(result->type_entry);7458 assert(result->value.type);
7418 return result->type_entry;7459 return result->value.type;
7419}7460}
74207461
7421static TypeTableEntry *ir_analyze_instruction_store_ptr(IrAnalyze *ira, IrInstructionStorePtr *store_ptr_instruction) {7462static TypeTableEntry *ir_analyze_instruction_store_ptr(IrAnalyze *ira, IrInstructionStorePtr *store_ptr_instruction) {
7422 IrInstruction *ptr = store_ptr_instruction->ptr->other;7463 IrInstruction *ptr = store_ptr_instruction->ptr->other;
7423 if (ptr->type_entry->id == TypeTableEntryIdInvalid)7464 if (ptr->value.type->id == TypeTableEntryIdInvalid)
7424 return ptr->type_entry;7465 return ptr->value.type;
74257466
7426 IrInstruction *value = store_ptr_instruction->value->other;7467 IrInstruction *value = store_ptr_instruction->value->other;
7427 if (value->type_entry->id == TypeTableEntryIdInvalid)7468 if (value->value.type->id == TypeTableEntryIdInvalid)
7428 return value->type_entry;7469 return value->value.type;
74297470
7430 TypeTableEntry *child_type = ptr->type_entry->data.pointer.child_type;7471 TypeTableEntry *child_type = ptr->value.type->data.pointer.child_type;
7431 IrInstruction *casted_value = ir_implicit_cast(ira, value, child_type);7472 IrInstruction *casted_value = ir_implicit_cast(ira, value, child_type);
7432 if (casted_value == ira->codegen->invalid_instruction)7473 if (casted_value == ira->codegen->invalid_instruction)
7433 return ira->codegen->builtin_types.entry_invalid;7474 return ira->codegen->builtin_types.entry_invalid;
74347475
7435 if (ptr->static_value.special != ConstValSpecialRuntime) {7476 if (ptr->value.special != ConstValSpecialRuntime) {
7436 bool is_inline = (ptr->static_value.data.x_ptr.special == ConstPtrSpecialInline);7477 bool is_inline = (ptr->value.data.x_ptr.special == ConstPtrSpecialInline);
7437 if (casted_value->static_value.special != ConstValSpecialRuntime) {7478 if (casted_value->value.special != ConstValSpecialRuntime) {
7438 ConstExprValue *dest_val = const_ptr_pointee(&ptr->static_value);7479 ConstExprValue *dest_val = const_ptr_pointee(&ptr->value);
7439 if (dest_val->special != ConstValSpecialRuntime) {7480 if (dest_val->special != ConstValSpecialRuntime) {
7440 *dest_val = casted_value->static_value;7481 *dest_val = casted_value->value;
7441 return ir_analyze_void(ira, &store_ptr_instruction->base);7482 return ir_analyze_void(ira, &store_ptr_instruction->base);
7442 }7483 }
7443 }7484 }
...@@ -7448,11 +7489,11 @@ static TypeTableEntry *ir_analyze_instruction_store_ptr(IrAnalyze *ira, IrInstru...@@ -7448,11 +7489,11 @@ static TypeTableEntry *ir_analyze_instruction_store_ptr(IrAnalyze *ira, IrInstru
7448 }7489 }
7449 }7490 }
74507491
7451 if (ptr->static_value.special != ConstValSpecialRuntime) {7492 if (ptr->value.special != ConstValSpecialRuntime) {
7452 // This memory location is transforming from known at compile time to known at runtime.7493 // This memory location is transforming from known at compile time to known at runtime.
7453 // We must emit our own var ptr instruction.7494 // We must emit our own var ptr instruction.
7454 // TODO can we delete this code now that we have inline var?7495 // TODO can we delete this code now that we have inline var?
7455 ptr->static_value.special = ConstValSpecialRuntime;7496 ptr->value.special = ConstValSpecialRuntime;
7456 IrInstruction *new_ptr_inst;7497 IrInstruction *new_ptr_inst;
7457 if (ptr->id == IrInstructionIdVarPtr) {7498 if (ptr->id == IrInstructionIdVarPtr) {
7458 IrInstructionVarPtr *var_ptr_inst = (IrInstructionVarPtr *)ptr;7499 IrInstructionVarPtr *var_ptr_inst = (IrInstructionVarPtr *)ptr;
...@@ -7469,7 +7510,7 @@ static TypeTableEntry *ir_analyze_instruction_store_ptr(IrAnalyze *ira, IrInstru...@@ -7469,7 +7510,7 @@ static TypeTableEntry *ir_analyze_instruction_store_ptr(IrAnalyze *ira, IrInstru
7469 } else {7510 } else {
7470 zig_unreachable();7511 zig_unreachable();
7471 }7512 }
7472 new_ptr_inst->type_entry = ptr->type_entry;7513 new_ptr_inst->value.type = ptr->value.type;
7473 ir_build_store_ptr(&ira->new_irb, store_ptr_instruction->base.scope,7514 ir_build_store_ptr(&ira->new_irb, store_ptr_instruction->base.scope,
7474 store_ptr_instruction->base.source_node, new_ptr_inst, casted_value);7515 store_ptr_instruction->base.source_node, new_ptr_inst, casted_value);
7475 return ir_analyze_void(ira, &store_ptr_instruction->base);7516 return ir_analyze_void(ira, &store_ptr_instruction->base);
...@@ -7481,7 +7522,7 @@ static TypeTableEntry *ir_analyze_instruction_store_ptr(IrAnalyze *ira, IrInstru...@@ -7481,7 +7522,7 @@ static TypeTableEntry *ir_analyze_instruction_store_ptr(IrAnalyze *ira, IrInstru
74817522
7482static TypeTableEntry *ir_analyze_instruction_typeof(IrAnalyze *ira, IrInstructionTypeOf *typeof_instruction) {7523static TypeTableEntry *ir_analyze_instruction_typeof(IrAnalyze *ira, IrInstructionTypeOf *typeof_instruction) {
7483 IrInstruction *expr_value = typeof_instruction->value->other;7524 IrInstruction *expr_value = typeof_instruction->value->other;
7484 TypeTableEntry *type_entry = expr_value->type_entry;7525 TypeTableEntry *type_entry = expr_value->value.type;
7485 switch (type_entry->id) {7526 switch (type_entry->id) {
7486 case TypeTableEntryIdInvalid:7527 case TypeTableEntryIdInvalid:
7487 return type_entry;7528 return type_entry;
...@@ -7547,7 +7588,7 @@ static TypeTableEntry *ir_analyze_instruction_to_ptr_type(IrAnalyze *ira,...@@ -7547,7 +7588,7 @@ static TypeTableEntry *ir_analyze_instruction_to_ptr_type(IrAnalyze *ira,
7547 }7588 }
75487589
7549 ConstExprValue *out_val = ir_build_const_from(ira, &to_ptr_type_instruction->base,7590 ConstExprValue *out_val = ir_build_const_from(ira, &to_ptr_type_instruction->base,
7550 type_value->static_value.depends_on_compile_var);7591 type_value->value.depends_on_compile_var);
7551 out_val->data.x_type = ptr_type;7592 out_val->data.x_type = ptr_type;
7552 return ira->codegen->builtin_types.entry_type;7593 return ira->codegen->builtin_types.entry_type;
7553}7594}
...@@ -7568,7 +7609,7 @@ static TypeTableEntry *ir_analyze_instruction_ptr_type_child(IrAnalyze *ira,...@@ -7568,7 +7609,7 @@ static TypeTableEntry *ir_analyze_instruction_ptr_type_child(IrAnalyze *ira,
7568 }7609 }
75697610
7570 ConstExprValue *out_val = ir_build_const_from(ira, &ptr_type_child_instruction->base,7611 ConstExprValue *out_val = ir_build_const_from(ira, &ptr_type_child_instruction->base,
7571 type_value->static_value.depends_on_compile_var);7612 type_value->value.depends_on_compile_var);
7572 out_val->data.x_type = type_entry->data.pointer.child_type;7613 out_val->data.x_type = type_entry->data.pointer.child_type;
7573 return ira->codegen->builtin_types.entry_type;7614 return ira->codegen->builtin_types.entry_type;
7574}7615}
...@@ -7631,7 +7672,7 @@ static TypeTableEntry *ir_analyze_instruction_set_debug_safety(IrAnalyze *ira,...@@ -7631,7 +7672,7 @@ static TypeTableEntry *ir_analyze_instruction_set_debug_safety(IrAnalyze *ira,
7631 IrInstructionSetDebugSafety *set_debug_safety_instruction)7672 IrInstructionSetDebugSafety *set_debug_safety_instruction)
7632{7673{
7633 IrInstruction *target_instruction = set_debug_safety_instruction->scope_value->other;7674 IrInstruction *target_instruction = set_debug_safety_instruction->scope_value->other;
7634 TypeTableEntry *target_type = target_instruction->type_entry;7675 TypeTableEntry *target_type = target_instruction->value.type;
7635 if (target_type->id == TypeTableEntryIdInvalid)7676 if (target_type->id == TypeTableEntryIdInvalid)
7636 return ira->codegen->builtin_types.entry_invalid;7677 return ira->codegen->builtin_types.entry_invalid;
7637 ConstExprValue *target_val = ir_resolve_const(ira, target_instruction, UndefBad);7678 ConstExprValue *target_val = ir_resolve_const(ira, target_instruction, UndefBad);
...@@ -7694,7 +7735,7 @@ static TypeTableEntry *ir_analyze_instruction_slice_type(IrAnalyze *ira,...@@ -7694,7 +7735,7 @@ static TypeTableEntry *ir_analyze_instruction_slice_type(IrAnalyze *ira,
7694 IrInstructionSliceType *slice_type_instruction)7735 IrInstructionSliceType *slice_type_instruction)
7695{7736{
7696 IrInstruction *child_type = slice_type_instruction->child_type->other;7737 IrInstruction *child_type = slice_type_instruction->child_type->other;
7697 if (child_type->type_entry->id == TypeTableEntryIdInvalid)7738 if (child_type->value.type->id == TypeTableEntryIdInvalid)
7698 return ira->codegen->builtin_types.entry_invalid;7739 return ira->codegen->builtin_types.entry_invalid;
7699 bool is_const = slice_type_instruction->is_const;7740 bool is_const = slice_type_instruction->is_const;
77007741
...@@ -7736,7 +7777,7 @@ static TypeTableEntry *ir_analyze_instruction_slice_type(IrAnalyze *ira,...@@ -7736,7 +7777,7 @@ static TypeTableEntry *ir_analyze_instruction_slice_type(IrAnalyze *ira,
7736 {7777 {
7737 TypeTableEntry *result_type = get_slice_type(ira->codegen, resolved_child_type, is_const);7778 TypeTableEntry *result_type = get_slice_type(ira->codegen, resolved_child_type, is_const);
7738 ConstExprValue *out_val = ir_build_const_from(ira, &slice_type_instruction->base,7779 ConstExprValue *out_val = ir_build_const_from(ira, &slice_type_instruction->base,
7739 child_type->static_value.depends_on_compile_var);7780 child_type->value.depends_on_compile_var);
7740 out_val->data.x_type = result_type;7781 out_val->data.x_type = result_type;
7741 return ira->codegen->builtin_types.entry_type;7782 return ira->codegen->builtin_types.entry_type;
7742 }7783 }
...@@ -7770,7 +7811,7 @@ static TypeTableEntry *ir_analyze_instruction_asm(IrAnalyze *ira, IrInstructionA...@@ -7770,7 +7811,7 @@ static TypeTableEntry *ir_analyze_instruction_asm(IrAnalyze *ira, IrInstructionA
77707811
7771 for (size_t i = 0; i < asm_expr->input_list.length; i += 1) {7812 for (size_t i = 0; i < asm_expr->input_list.length; i += 1) {
7772 input_list[i] = asm_instruction->input_list[i]->other;7813 input_list[i] = asm_instruction->input_list[i]->other;
7773 if (input_list[i]->type_entry->id == TypeTableEntryIdInvalid)7814 if (input_list[i]->value.type->id == TypeTableEntryIdInvalid)
7774 return ira->codegen->builtin_types.entry_invalid;7815 return ira->codegen->builtin_types.entry_invalid;
7775 }7816 }
77767817
...@@ -7825,8 +7866,8 @@ static TypeTableEntry *ir_analyze_instruction_array_type(IrAnalyze *ira,...@@ -7825,8 +7866,8 @@ static TypeTableEntry *ir_analyze_instruction_array_type(IrAnalyze *ira,
7825 case TypeTableEntryIdEnumTag:7866 case TypeTableEntryIdEnumTag:
7826 {7867 {
7827 TypeTableEntry *result_type = get_array_type(ira->codegen, child_type, size);7868 TypeTableEntry *result_type = get_array_type(ira->codegen, child_type, size);
7828 bool depends_on_compile_var = child_type_value->static_value.depends_on_compile_var ||7869 bool depends_on_compile_var = child_type_value->value.depends_on_compile_var ||
7829 size_value->static_value.depends_on_compile_var;7870 size_value->value.depends_on_compile_var;
7830 ConstExprValue *out_val = ir_build_const_from(ira, &array_type_instruction->base,7871 ConstExprValue *out_val = ir_build_const_from(ira, &array_type_instruction->base,
7831 depends_on_compile_var);7872 depends_on_compile_var);
7832 out_val->data.x_type = result_type;7873 out_val->data.x_type = result_type;
...@@ -7927,10 +7968,10 @@ static TypeTableEntry *ir_analyze_instruction_size_of(IrAnalyze *ira,...@@ -7927,10 +7968,10 @@ static TypeTableEntry *ir_analyze_instruction_size_of(IrAnalyze *ira,
79277968
7928static TypeTableEntry *ir_analyze_instruction_test_non_null(IrAnalyze *ira, IrInstructionTestNonNull *instruction) {7969static TypeTableEntry *ir_analyze_instruction_test_non_null(IrAnalyze *ira, IrInstructionTestNonNull *instruction) {
7929 IrInstruction *value = instruction->value->other;7970 IrInstruction *value = instruction->value->other;
7930 if (value->type_entry->id == TypeTableEntryIdInvalid)7971 if (value->value.type->id == TypeTableEntryIdInvalid)
7931 return ira->codegen->builtin_types.entry_invalid;7972 return ira->codegen->builtin_types.entry_invalid;
79327973
7933 TypeTableEntry *type_entry = value->type_entry;7974 TypeTableEntry *type_entry = value->value.type;
79347975
7935 if (type_entry->id == TypeTableEntryIdMaybe) {7976 if (type_entry->id == TypeTableEntryIdMaybe) {
7936 if (instr_is_comptime(value)) {7977 if (instr_is_comptime(value)) {
...@@ -7961,11 +8002,11 @@ static TypeTableEntry *ir_analyze_instruction_unwrap_maybe(IrAnalyze *ira,...@@ -7961,11 +8002,11 @@ static TypeTableEntry *ir_analyze_instruction_unwrap_maybe(IrAnalyze *ira,
7961 IrInstructionUnwrapMaybe *unwrap_maybe_instruction)8002 IrInstructionUnwrapMaybe *unwrap_maybe_instruction)
7962{8003{
7963 IrInstruction *value = unwrap_maybe_instruction->value->other;8004 IrInstruction *value = unwrap_maybe_instruction->value->other;
7964 if (value->type_entry->id == TypeTableEntryIdInvalid)8005 if (value->value.type->id == TypeTableEntryIdInvalid)
7965 return ira->codegen->builtin_types.entry_invalid;8006 return ira->codegen->builtin_types.entry_invalid;
79668007
7967 // This will be a pointer type because test null IR instruction operates on a pointer to a thing.8008 // This will be a pointer type because test null IR instruction operates on a pointer to a thing.
7968 TypeTableEntry *ptr_type = value->type_entry;8009 TypeTableEntry *ptr_type = value->value.type;
7969 assert(ptr_type->id == TypeTableEntryIdPointer);8010 assert(ptr_type->id == TypeTableEntryIdPointer);
79708011
7971 TypeTableEntry *type_entry = ptr_type->data.pointer.child_type;8012 TypeTableEntry *type_entry = ptr_type->data.pointer.child_type;
...@@ -8008,59 +8049,59 @@ static TypeTableEntry *ir_analyze_instruction_unwrap_maybe(IrAnalyze *ira,...@@ -8008,59 +8049,59 @@ static TypeTableEntry *ir_analyze_instruction_unwrap_maybe(IrAnalyze *ira,
80088049
8009static TypeTableEntry *ir_analyze_instruction_ctz(IrAnalyze *ira, IrInstructionCtz *ctz_instruction) {8050static TypeTableEntry *ir_analyze_instruction_ctz(IrAnalyze *ira, IrInstructionCtz *ctz_instruction) {
8010 IrInstruction *value = ctz_instruction->value->other;8051 IrInstruction *value = ctz_instruction->value->other;
8011 if (value->type_entry->id == TypeTableEntryIdInvalid) {8052 if (value->value.type->id == TypeTableEntryIdInvalid) {
8012 return ira->codegen->builtin_types.entry_invalid;8053 return ira->codegen->builtin_types.entry_invalid;
8013 } else if (value->type_entry->id == TypeTableEntryIdInt) {8054 } else if (value->value.type->id == TypeTableEntryIdInt) {
8014 if (value->static_value.special != ConstValSpecialRuntime) {8055 if (value->value.special != ConstValSpecialRuntime) {
8015 uint32_t result = bignum_ctz(&value->static_value.data.x_bignum,8056 uint32_t result = bignum_ctz(&value->value.data.x_bignum,
8016 value->type_entry->data.integral.bit_count);8057 value->value.type->data.integral.bit_count);
8017 bool depends_on_compile_var = value->static_value.depends_on_compile_var;8058 bool depends_on_compile_var = value->value.depends_on_compile_var;
8018 ConstExprValue *out_val = ir_build_const_from(ira, &ctz_instruction->base,8059 ConstExprValue *out_val = ir_build_const_from(ira, &ctz_instruction->base,
8019 depends_on_compile_var);8060 depends_on_compile_var);
8020 bignum_init_unsigned(&out_val->data.x_bignum, result);8061 bignum_init_unsigned(&out_val->data.x_bignum, result);
8021 return value->type_entry;8062 return value->value.type;
8022 }8063 }
80238064
8024 ir_build_ctz_from(&ira->new_irb, &ctz_instruction->base, value);8065 ir_build_ctz_from(&ira->new_irb, &ctz_instruction->base, value);
8025 return value->type_entry;8066 return value->value.type;
8026 } else {8067 } else {
8027 ir_add_error_node(ira, ctz_instruction->base.source_node,8068 ir_add_error_node(ira, ctz_instruction->base.source_node,
8028 buf_sprintf("expected integer type, found '%s'", buf_ptr(&value->type_entry->name)));8069 buf_sprintf("expected integer type, found '%s'", buf_ptr(&value->value.type->name)));
8029 return ira->codegen->builtin_types.entry_invalid;8070 return ira->codegen->builtin_types.entry_invalid;
8030 }8071 }
8031}8072}
80328073
8033static TypeTableEntry *ir_analyze_instruction_clz(IrAnalyze *ira, IrInstructionClz *clz_instruction) {8074static TypeTableEntry *ir_analyze_instruction_clz(IrAnalyze *ira, IrInstructionClz *clz_instruction) {
8034 IrInstruction *value = clz_instruction->value->other;8075 IrInstruction *value = clz_instruction->value->other;
8035 if (value->type_entry->id == TypeTableEntryIdInvalid) {8076 if (value->value.type->id == TypeTableEntryIdInvalid) {
8036 return ira->codegen->builtin_types.entry_invalid;8077 return ira->codegen->builtin_types.entry_invalid;
8037 } else if (value->type_entry->id == TypeTableEntryIdInt) {8078 } else if (value->value.type->id == TypeTableEntryIdInt) {
8038 if (value->static_value.special != ConstValSpecialRuntime) {8079 if (value->value.special != ConstValSpecialRuntime) {
8039 uint32_t result = bignum_clz(&value->static_value.data.x_bignum,8080 uint32_t result = bignum_clz(&value->value.data.x_bignum,
8040 value->type_entry->data.integral.bit_count);8081 value->value.type->data.integral.bit_count);
8041 bool depends_on_compile_var = value->static_value.depends_on_compile_var;8082 bool depends_on_compile_var = value->value.depends_on_compile_var;
8042 ConstExprValue *out_val = ir_build_const_from(ira, &clz_instruction->base,8083 ConstExprValue *out_val = ir_build_const_from(ira, &clz_instruction->base,
8043 depends_on_compile_var);8084 depends_on_compile_var);
8044 bignum_init_unsigned(&out_val->data.x_bignum, result);8085 bignum_init_unsigned(&out_val->data.x_bignum, result);
8045 return value->type_entry;8086 return value->value.type;
8046 }8087 }
80478088
8048 ir_build_clz_from(&ira->new_irb, &clz_instruction->base, value);8089 ir_build_clz_from(&ira->new_irb, &clz_instruction->base, value);
8049 return value->type_entry;8090 return value->value.type;
8050 } else {8091 } else {
8051 ir_add_error_node(ira, clz_instruction->base.source_node,8092 ir_add_error_node(ira, clz_instruction->base.source_node,
8052 buf_sprintf("expected integer type, found '%s'", buf_ptr(&value->type_entry->name)));8093 buf_sprintf("expected integer type, found '%s'", buf_ptr(&value->value.type->name)));
8053 return ira->codegen->builtin_types.entry_invalid;8094 return ira->codegen->builtin_types.entry_invalid;
8054 }8095 }
8055}8096}
80568097
8057static IrInstruction *ir_analyze_enum_tag(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *value) {8098static IrInstruction *ir_analyze_enum_tag(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *value) {
8058 if (value->type_entry->id == TypeTableEntryIdInvalid)8099 if (value->value.type->id == TypeTableEntryIdInvalid)
8059 return ira->codegen->invalid_instruction;8100 return ira->codegen->invalid_instruction;
80608101
8061 if (value->type_entry->id != TypeTableEntryIdEnum) {8102 if (value->value.type->id != TypeTableEntryIdEnum) {
8062 ir_add_error(ira, source_instr,8103 ir_add_error(ira, source_instr,
8063 buf_sprintf("expected enum type, found '%s'", buf_ptr(&value->type_entry->name)));8104 buf_sprintf("expected enum type, found '%s'", buf_ptr(&value->value.type->name)));
8064 return ira->codegen->invalid_instruction;8105 return ira->codegen->invalid_instruction;
8065 }8106 }
80668107
...@@ -8071,10 +8112,10 @@ static IrInstruction *ir_analyze_enum_tag(IrAnalyze *ira, IrInstruction *source_...@@ -8071,10 +8112,10 @@ static IrInstruction *ir_analyze_enum_tag(IrAnalyze *ira, IrInstruction *source_
80718112
8072 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(ira->new_irb.exec,8113 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(ira->new_irb.exec,
8073 source_instr->scope, source_instr->source_node);8114 source_instr->scope, source_instr->source_node);
8074 const_instruction->base.type_entry = value->type_entry->data.enumeration.tag_type;8115 const_instruction->base.value.type = value->value.type->data.enumeration.tag_type;
8075 const_instruction->base.static_value.special = ConstValSpecialStatic;8116 const_instruction->base.value.special = ConstValSpecialStatic;
8076 const_instruction->base.static_value.depends_on_compile_var = val->depends_on_compile_var;8117 const_instruction->base.value.depends_on_compile_var = val->depends_on_compile_var;
8077 bignum_init_unsigned(&const_instruction->base.static_value.data.x_bignum, val->data.x_enum.tag);8118 bignum_init_unsigned(&const_instruction->base.value.data.x_bignum, val->data.x_enum.tag);
8078 return &const_instruction->base;8119 return &const_instruction->base;
8079 }8120 }
80808121
...@@ -8085,7 +8126,7 @@ static TypeTableEntry *ir_analyze_instruction_switch_br(IrAnalyze *ira,...@@ -8085,7 +8126,7 @@ static TypeTableEntry *ir_analyze_instruction_switch_br(IrAnalyze *ira,
8085 IrInstructionSwitchBr *switch_br_instruction)8126 IrInstructionSwitchBr *switch_br_instruction)
8086{8127{
8087 IrInstruction *target_value = switch_br_instruction->target_value->other;8128 IrInstruction *target_value = switch_br_instruction->target_value->other;
8088 if (target_value->type_entry->id == TypeTableEntryIdInvalid)8129 if (target_value->value.type->id == TypeTableEntryIdInvalid)
8089 return ir_unreach_error(ira);8130 return ir_unreach_error(ira);
80908131
8091 size_t case_count = switch_br_instruction->case_count;8132 size_t case_count = switch_br_instruction->case_count;
...@@ -8103,24 +8144,24 @@ static TypeTableEntry *ir_analyze_instruction_switch_br(IrAnalyze *ira,...@@ -8103,24 +8144,24 @@ static TypeTableEntry *ir_analyze_instruction_switch_br(IrAnalyze *ira,
8103 for (size_t i = 0; i < case_count; i += 1) {8144 for (size_t i = 0; i < case_count; i += 1) {
8104 IrInstructionSwitchBrCase *old_case = &switch_br_instruction->cases[i];8145 IrInstructionSwitchBrCase *old_case = &switch_br_instruction->cases[i];
8105 IrInstruction *case_value = old_case->value->other;8146 IrInstruction *case_value = old_case->value->other;
8106 if (case_value->type_entry->id == TypeTableEntryIdInvalid)8147 if (case_value->value.type->id == TypeTableEntryIdInvalid)
8107 return ir_unreach_error(ira);8148 return ir_unreach_error(ira);
81088149
8109 if (case_value->type_entry->id == TypeTableEntryIdEnum) {8150 if (case_value->value.type->id == TypeTableEntryIdEnum) {
8110 case_value = ir_analyze_enum_tag(ira, &switch_br_instruction->base, case_value);8151 case_value = ir_analyze_enum_tag(ira, &switch_br_instruction->base, case_value);
8111 if (case_value->type_entry->id == TypeTableEntryIdInvalid)8152 if (case_value->value.type->id == TypeTableEntryIdInvalid)
8112 return ir_unreach_error(ira);8153 return ir_unreach_error(ira);
8113 }8154 }
81148155
8115 IrInstruction *casted_case_value = ir_implicit_cast(ira, case_value, target_value->type_entry);8156 IrInstruction *casted_case_value = ir_implicit_cast(ira, case_value, target_value->value.type);
8116 if (casted_case_value->type_entry->id == TypeTableEntryIdInvalid)8157 if (casted_case_value->value.type->id == TypeTableEntryIdInvalid)
8117 return ir_unreach_error(ira);8158 return ir_unreach_error(ira);
81188159
8119 ConstExprValue *case_val = ir_resolve_const(ira, casted_case_value, UndefBad);8160 ConstExprValue *case_val = ir_resolve_const(ira, casted_case_value, UndefBad);
8120 if (!case_val)8161 if (!case_val)
8121 return ir_unreach_error(ira);8162 return ir_unreach_error(ira);
81228163
8123 if (const_values_equal(target_val, case_val, target_value->type_entry)) {8164 if (const_values_equal(target_val, case_val)) {
8124 old_dest_block = old_case->block;8165 old_dest_block = old_case->block;
8125 break;8166 break;
8126 }8167 }
...@@ -8144,17 +8185,17 @@ static TypeTableEntry *ir_analyze_instruction_switch_br(IrAnalyze *ira,...@@ -8144,17 +8185,17 @@ static TypeTableEntry *ir_analyze_instruction_switch_br(IrAnalyze *ira,
81448185
8145 IrInstruction *old_value = old_case->value;8186 IrInstruction *old_value = old_case->value;
8146 IrInstruction *new_value = old_value->other;8187 IrInstruction *new_value = old_value->other;
8147 if (new_value->type_entry->id == TypeTableEntryIdInvalid)8188 if (new_value->value.type->id == TypeTableEntryIdInvalid)
8148 continue;8189 continue;
81498190
8150 if (new_value->type_entry->id == TypeTableEntryIdEnum) {8191 if (new_value->value.type->id == TypeTableEntryIdEnum) {
8151 new_value = ir_analyze_enum_tag(ira, &switch_br_instruction->base, new_value);8192 new_value = ir_analyze_enum_tag(ira, &switch_br_instruction->base, new_value);
8152 if (new_value->type_entry->id == TypeTableEntryIdInvalid)8193 if (new_value->value.type->id == TypeTableEntryIdInvalid)
8153 continue;8194 continue;
8154 }8195 }
81558196
8156 IrInstruction *casted_new_value = ir_implicit_cast(ira, new_value, target_value->type_entry);8197 IrInstruction *casted_new_value = ir_implicit_cast(ira, new_value, target_value->value.type);
8157 if (casted_new_value->type_entry->id == TypeTableEntryIdInvalid)8198 if (casted_new_value->value.type->id == TypeTableEntryIdInvalid)
8158 continue;8199 continue;
81598200
8160 if (!ir_resolve_const(ira, casted_new_value, UndefBad))8201 if (!ir_resolve_const(ira, casted_new_value, UndefBad))
...@@ -8173,15 +8214,15 @@ static TypeTableEntry *ir_analyze_instruction_switch_target(IrAnalyze *ira,...@@ -8173,15 +8214,15 @@ static TypeTableEntry *ir_analyze_instruction_switch_target(IrAnalyze *ira,
8173 IrInstructionSwitchTarget *switch_target_instruction)8214 IrInstructionSwitchTarget *switch_target_instruction)
8174{8215{
8175 IrInstruction *target_value_ptr = switch_target_instruction->target_value_ptr->other;8216 IrInstruction *target_value_ptr = switch_target_instruction->target_value_ptr->other;
8176 if (target_value_ptr->type_entry->id == TypeTableEntryIdInvalid)8217 if (target_value_ptr->value.type->id == TypeTableEntryIdInvalid)
8177 return ira->codegen->builtin_types.entry_invalid;8218 return ira->codegen->builtin_types.entry_invalid;
81788219
8179 assert(target_value_ptr->type_entry->id == TypeTableEntryIdPointer);8220 assert(target_value_ptr->value.type->id == TypeTableEntryIdPointer);
8180 TypeTableEntry *target_type = target_value_ptr->type_entry->data.pointer.child_type;8221 TypeTableEntry *target_type = target_value_ptr->value.type->data.pointer.child_type;
8181 bool depends_on_compile_var = target_value_ptr->static_value.depends_on_compile_var;8222 bool depends_on_compile_var = target_value_ptr->value.depends_on_compile_var;
8182 ConstExprValue *pointee_val = nullptr;8223 ConstExprValue *pointee_val = nullptr;
8183 if (target_value_ptr->static_value.special != ConstValSpecialRuntime) {8224 if (target_value_ptr->value.special != ConstValSpecialRuntime) {
8184 pointee_val = const_ptr_pointee(&target_value_ptr->static_value);8225 pointee_val = const_ptr_pointee(&target_value_ptr->value);
8185 if (pointee_val->special == ConstValSpecialRuntime)8226 if (pointee_val->special == ConstValSpecialRuntime)
8186 pointee_val = nullptr;8227 pointee_val = nullptr;
8187 }8228 }
...@@ -8206,6 +8247,7 @@ static TypeTableEntry *ir_analyze_instruction_switch_target(IrAnalyze *ira,...@@ -8206,6 +8247,7 @@ static TypeTableEntry *ir_analyze_instruction_switch_target(IrAnalyze *ira,
8206 ConstExprValue *out_val = ir_build_const_from(ira, &switch_target_instruction->base,8247 ConstExprValue *out_val = ir_build_const_from(ira, &switch_target_instruction->base,
8207 depends_on_compile_var);8248 depends_on_compile_var);
8208 *out_val = *pointee_val;8249 *out_val = *pointee_val;
8250 out_val->type = target_type;
8209 return target_type;8251 return target_type;
8210 }8252 }
82118253
...@@ -8223,7 +8265,7 @@ static TypeTableEntry *ir_analyze_instruction_switch_target(IrAnalyze *ira,...@@ -8223,7 +8265,7 @@ static TypeTableEntry *ir_analyze_instruction_switch_target(IrAnalyze *ira,
82238265
8224 IrInstruction *enum_value = ir_build_load_ptr(&ira->new_irb, switch_target_instruction->base.scope,8266 IrInstruction *enum_value = ir_build_load_ptr(&ira->new_irb, switch_target_instruction->base.scope,
8225 switch_target_instruction->base.source_node, target_value_ptr);8267 switch_target_instruction->base.source_node, target_value_ptr);
8226 enum_value->type_entry = target_type;8268 enum_value->value.type = target_type;
8227 ir_build_enum_tag_from(&ira->new_irb, &switch_target_instruction->base, enum_value);8269 ir_build_enum_tag_from(&ira->new_irb, &switch_target_instruction->base, enum_value);
8228 return tag_type;8270 return tag_type;
8229 }8271 }
...@@ -8251,24 +8293,24 @@ static TypeTableEntry *ir_analyze_instruction_switch_target(IrAnalyze *ira,...@@ -8251,24 +8293,24 @@ static TypeTableEntry *ir_analyze_instruction_switch_target(IrAnalyze *ira,
82518293
8252static TypeTableEntry *ir_analyze_instruction_switch_var(IrAnalyze *ira, IrInstructionSwitchVar *instruction) {8294static TypeTableEntry *ir_analyze_instruction_switch_var(IrAnalyze *ira, IrInstructionSwitchVar *instruction) {
8253 IrInstruction *target_value_ptr = instruction->target_value_ptr->other;8295 IrInstruction *target_value_ptr = instruction->target_value_ptr->other;
8254 if (target_value_ptr->type_entry->id == TypeTableEntryIdInvalid)8296 if (target_value_ptr->value.type->id == TypeTableEntryIdInvalid)
8255 return ira->codegen->builtin_types.entry_invalid;8297 return ira->codegen->builtin_types.entry_invalid;
82568298
8257 IrInstruction *prong_value = instruction->prong_value->other;8299 IrInstruction *prong_value = instruction->prong_value->other;
8258 if (prong_value->type_entry->id == TypeTableEntryIdInvalid)8300 if (prong_value->value.type->id == TypeTableEntryIdInvalid)
8259 return ira->codegen->builtin_types.entry_invalid;8301 return ira->codegen->builtin_types.entry_invalid;
82608302
8261 assert(target_value_ptr->type_entry->id == TypeTableEntryIdPointer);8303 assert(target_value_ptr->value.type->id == TypeTableEntryIdPointer);
8262 TypeTableEntry *target_type = target_value_ptr->type_entry->data.pointer.child_type;8304 TypeTableEntry *target_type = target_value_ptr->value.type->data.pointer.child_type;
8263 if (target_type->id == TypeTableEntryIdEnum) {8305 if (target_type->id == TypeTableEntryIdEnum) {
8264 ConstExprValue *prong_val = ir_resolve_const(ira, prong_value, UndefBad);8306 ConstExprValue *prong_val = ir_resolve_const(ira, prong_value, UndefBad);
8265 if (!prong_val)8307 if (!prong_val)
8266 return ira->codegen->builtin_types.entry_invalid;8308 return ira->codegen->builtin_types.entry_invalid;
82678309
8268 TypeEnumField *field = &target_type->data.enumeration.fields[prong_val->data.x_bignum.data.x_uint];8310 TypeEnumField *field = &target_type->data.enumeration.fields[prong_val->data.x_bignum.data.x_uint];
8269 if (prong_value->type_entry->id == TypeTableEntryIdEnumTag) {8311 if (prong_value->value.type->id == TypeTableEntryIdEnumTag) {
8270 field = &target_type->data.enumeration.fields[prong_val->data.x_bignum.data.x_uint];8312 field = &target_type->data.enumeration.fields[prong_val->data.x_bignum.data.x_uint];
8271 } else if (prong_value->type_entry->id == TypeTableEntryIdEnum) {8313 } else if (prong_value->value.type->id == TypeTableEntryIdEnum) {
8272 field = &target_type->data.enumeration.fields[prong_val->data.x_enum.tag];8314 field = &target_type->data.enumeration.fields[prong_val->data.x_enum.tag];
8273 } else {8315 } else {
8274 zig_unreachable();8316 zig_unreachable();
...@@ -8280,7 +8322,7 @@ static TypeTableEntry *ir_analyze_instruction_switch_var(IrAnalyze *ira, IrInstr...@@ -8280,7 +8322,7 @@ static TypeTableEntry *ir_analyze_instruction_switch_var(IrAnalyze *ira, IrInstr
82808322
8281 ir_build_enum_field_ptr_from(&ira->new_irb, &instruction->base, target_value_ptr, field);8323 ir_build_enum_field_ptr_from(&ira->new_irb, &instruction->base, target_value_ptr, field);
8282 return get_pointer_to_type(ira->codegen, field->type_entry,8324 return get_pointer_to_type(ira->codegen, field->type_entry,
8283 target_value_ptr->type_entry->data.pointer.is_const);8325 target_value_ptr->value.type->data.pointer.is_const);
8284 } else {8326 } else {
8285 ErrorMsg *msg = ir_add_error(ira, &instruction->base,8327 ErrorMsg *msg = ir_add_error(ira, &instruction->base,
8286 buf_sprintf("switch on type '%s' provides no expression parameter", buf_ptr(&target_type->name)));8328 buf_sprintf("switch on type '%s' provides no expression parameter", buf_ptr(&target_type->name)));
...@@ -8293,14 +8335,14 @@ static TypeTableEntry *ir_analyze_instruction_enum_tag(IrAnalyze *ira, IrInstruc...@@ -8293,14 +8335,14 @@ static TypeTableEntry *ir_analyze_instruction_enum_tag(IrAnalyze *ira, IrInstruc
8293 IrInstruction *value = enum_tag_instruction->value->other;8335 IrInstruction *value = enum_tag_instruction->value->other;
8294 IrInstruction *new_instruction = ir_analyze_enum_tag(ira, &enum_tag_instruction->base, value);8336 IrInstruction *new_instruction = ir_analyze_enum_tag(ira, &enum_tag_instruction->base, value);
8295 ir_link_new_instruction(new_instruction, &enum_tag_instruction->base);8337 ir_link_new_instruction(new_instruction, &enum_tag_instruction->base);
8296 return new_instruction->type_entry;8338 return new_instruction->value.type;
8297}8339}
82988340
8299static TypeTableEntry *ir_analyze_instruction_static_eval(IrAnalyze *ira,8341static TypeTableEntry *ir_analyze_instruction_static_eval(IrAnalyze *ira,
8300 IrInstructionStaticEval *static_eval_instruction)8342 IrInstructionStaticEval *static_eval_instruction)
8301{8343{
8302 IrInstruction *value = static_eval_instruction->value->other;8344 IrInstruction *value = static_eval_instruction->value->other;
8303 if (value->type_entry->id == TypeTableEntryIdInvalid)8345 if (value->value.type->id == TypeTableEntryIdInvalid)
8304 return ira->codegen->builtin_types.entry_invalid;8346 return ira->codegen->builtin_types.entry_invalid;
83058347
8306 ConstExprValue *val = ir_resolve_const(ira, value, UndefBad);8348 ConstExprValue *val = ir_resolve_const(ira, value, UndefBad);
...@@ -8309,7 +8351,7 @@ static TypeTableEntry *ir_analyze_instruction_static_eval(IrAnalyze *ira,...@@ -8309,7 +8351,7 @@ static TypeTableEntry *ir_analyze_instruction_static_eval(IrAnalyze *ira,
83098351
8310 ConstExprValue *out_val = ir_build_const_from(ira, &static_eval_instruction->base, val->depends_on_compile_var);8352 ConstExprValue *out_val = ir_build_const_from(ira, &static_eval_instruction->base, val->depends_on_compile_var);
8311 *out_val = *val;8353 *out_val = *val;
8312 return value->type_entry;8354 return value->value.type;
8313}8355}
83148356
8315static TypeTableEntry *ir_analyze_instruction_import(IrAnalyze *ira, IrInstructionImport *import_instruction) {8357static TypeTableEntry *ir_analyze_instruction_import(IrAnalyze *ira, IrInstructionImport *import_instruction) {
...@@ -8317,7 +8359,7 @@ static TypeTableEntry *ir_analyze_instruction_import(IrAnalyze *ira, IrInstructi...@@ -8317,7 +8359,7 @@ static TypeTableEntry *ir_analyze_instruction_import(IrAnalyze *ira, IrInstructi
8317 Buf *import_target_str = ir_resolve_str(ira, name_value);8359 Buf *import_target_str = ir_resolve_str(ira, name_value);
8318 if (!import_target_str)8360 if (!import_target_str)
8319 return ira->codegen->builtin_types.entry_invalid;8361 return ira->codegen->builtin_types.entry_invalid;
8320 bool depends_on_compile_var = name_value->static_value.depends_on_compile_var;8362 bool depends_on_compile_var = name_value->value.depends_on_compile_var;
83218363
8322 AstNode *source_node = import_instruction->base.source_node;8364 AstNode *source_node = import_instruction->base.source_node;
8323 ImportTableEntry *import = source_node->owner;8365 ImportTableEntry *import = source_node->owner;
...@@ -8390,16 +8432,16 @@ static TypeTableEntry *ir_analyze_instruction_array_len(IrAnalyze *ira,...@@ -8390,16 +8432,16 @@ static TypeTableEntry *ir_analyze_instruction_array_len(IrAnalyze *ira,
8390 IrInstructionArrayLen *array_len_instruction)8432 IrInstructionArrayLen *array_len_instruction)
8391{8433{
8392 IrInstruction *array_value = array_len_instruction->array_value->other;8434 IrInstruction *array_value = array_len_instruction->array_value->other;
8393 TypeTableEntry *canon_type = get_underlying_type(array_value->type_entry);8435 TypeTableEntry *canon_type = get_underlying_type(array_value->value.type);
8394 if (canon_type->id == TypeTableEntryIdInvalid) {8436 if (canon_type->id == TypeTableEntryIdInvalid) {
8395 return ira->codegen->builtin_types.entry_invalid;8437 return ira->codegen->builtin_types.entry_invalid;
8396 } else if (canon_type->id == TypeTableEntryIdArray) {8438 } else if (canon_type->id == TypeTableEntryIdArray) {
8397 bool depends_on_compile_var = array_value->static_value.depends_on_compile_var;8439 bool depends_on_compile_var = array_value->value.depends_on_compile_var;
8398 return ir_analyze_const_usize(ira, &array_len_instruction->base,8440 return ir_analyze_const_usize(ira, &array_len_instruction->base,
8399 canon_type->data.array.len, depends_on_compile_var);8441 canon_type->data.array.len, depends_on_compile_var);
8400 } else if (is_slice(canon_type)) {8442 } else if (is_slice(canon_type)) {
8401 if (array_value->static_value.special != ConstValSpecialRuntime) {8443 if (array_value->value.special != ConstValSpecialRuntime) {
8402 ConstExprValue *len_val = &array_value->static_value.data.x_struct.fields[slice_len_index];8444 ConstExprValue *len_val = &array_value->value.data.x_struct.fields[slice_len_index];
8403 if (len_val->special != ConstValSpecialRuntime) {8445 if (len_val->special != ConstValSpecialRuntime) {
8404 bool depends_on_compile_var = len_val->depends_on_compile_var;8446 bool depends_on_compile_var = len_val->depends_on_compile_var;
8405 return ir_analyze_const_usize(ira, &array_len_instruction->base,8447 return ir_analyze_const_usize(ira, &array_len_instruction->base,
...@@ -8409,12 +8451,12 @@ static TypeTableEntry *ir_analyze_instruction_array_len(IrAnalyze *ira,...@@ -8409,12 +8451,12 @@ static TypeTableEntry *ir_analyze_instruction_array_len(IrAnalyze *ira,
8409 TypeStructField *field = &canon_type->data.structure.fields[slice_len_index];8451 TypeStructField *field = &canon_type->data.structure.fields[slice_len_index];
8410 IrInstruction *len_ptr = ir_build_struct_field_ptr(&ira->new_irb, array_len_instruction->base.scope,8452 IrInstruction *len_ptr = ir_build_struct_field_ptr(&ira->new_irb, array_len_instruction->base.scope,
8411 array_len_instruction->base.source_node, array_value, field);8453 array_len_instruction->base.source_node, array_value, field);
8412 len_ptr->type_entry = get_pointer_to_type(ira->codegen, ira->codegen->builtin_types.entry_usize, true);8454 len_ptr->value.type = get_pointer_to_type(ira->codegen, ira->codegen->builtin_types.entry_usize, true);
8413 ir_build_load_ptr_from(&ira->new_irb, &array_len_instruction->base, len_ptr);8455 ir_build_load_ptr_from(&ira->new_irb, &array_len_instruction->base, len_ptr);
8414 return ira->codegen->builtin_types.entry_usize;8456 return ira->codegen->builtin_types.entry_usize;
8415 } else {8457 } else {
8416 ir_add_error_node(ira, array_len_instruction->base.source_node,8458 ir_add_error_node(ira, array_len_instruction->base.source_node,
8417 buf_sprintf("type '%s' has no field 'len'", buf_ptr(&array_value->type_entry->name)));8459 buf_sprintf("type '%s' has no field 'len'", buf_ptr(&array_value->value.type->name)));
8418 // TODO if this is a typedecl, add error note showing the declaration of the type decl8460 // TODO if this is a typedecl, add error note showing the declaration of the type decl
8419 return ira->codegen->builtin_types.entry_invalid;8461 return ira->codegen->builtin_types.entry_invalid;
8420 }8462 }
...@@ -8444,13 +8486,14 @@ static TypeTableEntry *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstru...@@ -8444,13 +8486,14 @@ static TypeTableEntry *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstru
84448486
8445 ConstExprValue const_val = {};8487 ConstExprValue const_val = {};
8446 const_val.special = ConstValSpecialStatic;8488 const_val.special = ConstValSpecialStatic;
8489 const_val.type = container_type;
8447 const_val.depends_on_compile_var = depends_on_compile_var;8490 const_val.depends_on_compile_var = depends_on_compile_var;
8448 const_val.data.x_struct.fields = allocate<ConstExprValue>(actual_field_count);8491 const_val.data.x_struct.fields = allocate<ConstExprValue>(actual_field_count);
8449 for (size_t i = 0; i < instr_field_count; i += 1) {8492 for (size_t i = 0; i < instr_field_count; i += 1) {
8450 IrInstructionContainerInitFieldsField *field = &fields[i];8493 IrInstructionContainerInitFieldsField *field = &fields[i];
84518494
8452 IrInstruction *field_value = field->value->other;8495 IrInstruction *field_value = field->value->other;
8453 if (field_value->type_entry->id == TypeTableEntryIdInvalid)8496 if (field_value->value.type->id == TypeTableEntryIdInvalid)
8454 return ira->codegen->builtin_types.entry_invalid;8497 return ira->codegen->builtin_types.entry_invalid;
84558498
8456 TypeStructField *type_field = find_struct_type_field(container_type, field->name);8499 TypeStructField *type_field = find_struct_type_field(container_type, field->name);
...@@ -8481,7 +8524,7 @@ static TypeTableEntry *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstru...@@ -8481,7 +8524,7 @@ static TypeTableEntry *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstru
8481 new_fields[field_index].type_struct_field = type_field;8524 new_fields[field_index].type_struct_field = type_field;
84828525
8483 if (const_val.special == ConstValSpecialStatic) {8526 if (const_val.special == ConstValSpecialStatic) {
8484 if (is_comptime || casted_field_value->static_value.special != ConstValSpecialRuntime) {8527 if (is_comptime || casted_field_value->value.special != ConstValSpecialRuntime) {
8485 ConstExprValue *field_val = ir_resolve_const(ira, casted_field_value, UndefOk);8528 ConstExprValue *field_val = ir_resolve_const(ira, casted_field_value, UndefOk);
8486 if (!field_val)8529 if (!field_val)
8487 return ira->codegen->builtin_types.entry_invalid;8530 return ira->codegen->builtin_types.entry_invalid;
...@@ -8529,16 +8572,16 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira...@@ -8529,16 +8572,16 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira
8529 IrInstructionContainerInitList *instruction)8572 IrInstructionContainerInitList *instruction)
8530{8573{
8531 IrInstruction *container_type_value = instruction->container_type->other;8574 IrInstruction *container_type_value = instruction->container_type->other;
8532 if (container_type_value->type_entry->id == TypeTableEntryIdInvalid)8575 if (container_type_value->value.type->id == TypeTableEntryIdInvalid)
8533 return ira->codegen->builtin_types.entry_invalid;8576 return ira->codegen->builtin_types.entry_invalid;
85348577
8535 size_t elem_count = instruction->item_count;8578 size_t elem_count = instruction->item_count;
8536 if (container_type_value->type_entry->id == TypeTableEntryIdMetaType) {8579 if (container_type_value->value.type->id == TypeTableEntryIdMetaType) {
8537 TypeTableEntry *container_type = ir_resolve_type(ira, container_type_value);8580 TypeTableEntry *container_type = ir_resolve_type(ira, container_type_value);
8538 if (container_type->id == TypeTableEntryIdInvalid)8581 if (container_type->id == TypeTableEntryIdInvalid)
8539 return ira->codegen->builtin_types.entry_invalid;8582 return ira->codegen->builtin_types.entry_invalid;
85408583
8541 bool depends_on_compile_var = container_type_value->static_value.depends_on_compile_var;8584 bool depends_on_compile_var = container_type_value->value.depends_on_compile_var;
85428585
8543 if (container_type->id == TypeTableEntryIdStruct && !is_slice(container_type) && elem_count == 0) {8586 if (container_type->id == TypeTableEntryIdStruct && !is_slice(container_type) && elem_count == 0) {
8544 return ir_analyze_container_init_fields(ira, &instruction->base, container_type,8587 return ir_analyze_container_init_fields(ira, &instruction->base, container_type,
...@@ -8547,9 +8590,11 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira...@@ -8547,9 +8590,11 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira
8547 TypeTableEntry *pointer_type = container_type->data.structure.fields[slice_ptr_index].type_entry;8590 TypeTableEntry *pointer_type = container_type->data.structure.fields[slice_ptr_index].type_entry;
8548 assert(pointer_type->id == TypeTableEntryIdPointer);8591 assert(pointer_type->id == TypeTableEntryIdPointer);
8549 TypeTableEntry *child_type = pointer_type->data.pointer.child_type;8592 TypeTableEntry *child_type = pointer_type->data.pointer.child_type;
8593 TypeTableEntry *fixed_size_array_type = get_array_type(ira->codegen, child_type, elem_count);
85508594
8551 ConstExprValue const_val = {};8595 ConstExprValue const_val = {};
8552 const_val.special = ConstValSpecialStatic;8596 const_val.special = ConstValSpecialStatic;
8597 const_val.type = fixed_size_array_type;
8553 const_val.depends_on_compile_var = depends_on_compile_var;8598 const_val.depends_on_compile_var = depends_on_compile_var;
8554 const_val.data.x_array.elements = allocate<ConstExprValue>(elem_count);8599 const_val.data.x_array.elements = allocate<ConstExprValue>(elem_count);
8555 const_val.data.x_array.size = elem_count;8600 const_val.data.x_array.size = elem_count;
...@@ -8562,7 +8607,7 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira...@@ -8562,7 +8607,7 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira
85628607
8563 for (size_t i = 0; i < elem_count; i += 1) {8608 for (size_t i = 0; i < elem_count; i += 1) {
8564 IrInstruction *arg_value = instruction->items[i]->other;8609 IrInstruction *arg_value = instruction->items[i]->other;
8565 if (arg_value->type_entry->id == TypeTableEntryIdInvalid)8610 if (arg_value->value.type->id == TypeTableEntryIdInvalid)
8566 return ira->codegen->builtin_types.entry_invalid;8611 return ira->codegen->builtin_types.entry_invalid;
85678612
8568 IrInstruction *casted_arg = ir_implicit_cast(ira, arg_value, child_type);8613 IrInstruction *casted_arg = ir_implicit_cast(ira, arg_value, child_type);
...@@ -8572,7 +8617,7 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira...@@ -8572,7 +8617,7 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira
8572 new_items[i] = casted_arg;8617 new_items[i] = casted_arg;
85738618
8574 if (const_val.special == ConstValSpecialStatic) {8619 if (const_val.special == ConstValSpecialStatic) {
8575 if (is_comptime || casted_arg->static_value.special != ConstValSpecialRuntime) {8620 if (is_comptime || casted_arg->value.special != ConstValSpecialRuntime) {
8576 ConstExprValue *elem_val = ir_resolve_const(ira, casted_arg, UndefBad);8621 ConstExprValue *elem_val = ir_resolve_const(ira, casted_arg, UndefBad);
8577 if (!elem_val)8622 if (!elem_val)
8578 return ira->codegen->builtin_types.entry_invalid;8623 return ira->codegen->builtin_types.entry_invalid;
...@@ -8586,7 +8631,6 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira...@@ -8586,7 +8631,6 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira
8586 }8631 }
8587 }8632 }
85888633
8589 TypeTableEntry *fixed_size_array_type = get_array_type(ira->codegen, child_type, elem_count);
8590 if (const_val.special == ConstValSpecialStatic) {8634 if (const_val.special == ConstValSpecialStatic) {
8591 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base, const_val.depends_on_compile_var);8635 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base, const_val.depends_on_compile_var);
8592 *out_val = const_val;8636 *out_val = const_val;
...@@ -8619,7 +8663,7 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira...@@ -8619,7 +8663,7 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira
8619 buf_ptr(&container_type->name)));8663 buf_ptr(&container_type->name)));
8620 return ira->codegen->builtin_types.entry_invalid;8664 return ira->codegen->builtin_types.entry_invalid;
8621 }8665 }
8622 } else if (container_type_value->type_entry->id == TypeTableEntryIdEnumTag) {8666 } else if (container_type_value->value.type->id == TypeTableEntryIdEnumTag) {
8623 if (elem_count != 1) {8667 if (elem_count != 1) {
8624 ir_add_error(ira, &instruction->base, buf_sprintf("enum initialization requires exactly one element"));8668 ir_add_error(ira, &instruction->base, buf_sprintf("enum initialization requires exactly one element"));
8625 return ira->codegen->builtin_types.entry_invalid;8669 return ira->codegen->builtin_types.entry_invalid;
...@@ -8628,7 +8672,7 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira...@@ -8628,7 +8672,7 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira
8628 if (!tag_value)8672 if (!tag_value)
8629 return ira->codegen->builtin_types.entry_invalid;8673 return ira->codegen->builtin_types.entry_invalid;
86308674
8631 TypeTableEntry *enum_type = container_type_value->type_entry->data.enum_tag.enum_type;8675 TypeTableEntry *enum_type = container_type_value->value.type->data.enum_tag.enum_type;
86328676
8633 uint64_t tag_uint = tag_value->data.x_bignum.data.x_uint;8677 uint64_t tag_uint = tag_value->data.x_bignum.data.x_uint;
8634 TypeEnumField *field = &enum_type->data.enumeration.fields[tag_uint];8678 TypeEnumField *field = &enum_type->data.enumeration.fields[tag_uint];
...@@ -8645,7 +8689,7 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira...@@ -8645,7 +8689,7 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira
8645 if (!init_val)8689 if (!init_val)
8646 return ira->codegen->builtin_types.entry_invalid;8690 return ira->codegen->builtin_types.entry_invalid;
8647 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base,8691 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base,
8648 casted_init_value->static_value.depends_on_compile_var);8692 casted_init_value->value.depends_on_compile_var);
8649 out_val->data.x_enum.tag = tag_uint;8693 out_val->data.x_enum.tag = tag_uint;
8650 out_val->data.x_enum.payload = init_val;8694 out_val->data.x_enum.payload = init_val;
8651 return enum_type;8695 return enum_type;
...@@ -8657,7 +8701,7 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira...@@ -8657,7 +8701,7 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira
8657 return enum_type;8701 return enum_type;
8658 } else {8702 } else {
8659 ir_add_error(ira, container_type_value,8703 ir_add_error(ira, container_type_value,
8660 buf_sprintf("expected type, found '%s'", buf_ptr(&container_type_value->type_entry->name)));8704 buf_sprintf("expected type, found '%s'", buf_ptr(&container_type_value->value.type->name)));
8661 return ira->codegen->builtin_types.entry_invalid;8705 return ira->codegen->builtin_types.entry_invalid;
8662 }8706 }
8663}8707}
...@@ -8668,7 +8712,7 @@ static TypeTableEntry *ir_analyze_instruction_container_init_fields(IrAnalyze *i...@@ -8668,7 +8712,7 @@ static TypeTableEntry *ir_analyze_instruction_container_init_fields(IrAnalyze *i
8668 if (container_type->id == TypeTableEntryIdInvalid)8712 if (container_type->id == TypeTableEntryIdInvalid)
8669 return ira->codegen->builtin_types.entry_invalid;8713 return ira->codegen->builtin_types.entry_invalid;
86708714
8671 bool depends_on_compile_var = container_type_value->static_value.depends_on_compile_var;8715 bool depends_on_compile_var = container_type_value->value.depends_on_compile_var;
86728716
8673 return ir_analyze_container_init_fields(ira, &instruction->base, container_type,8717 return ir_analyze_container_init_fields(ira, &instruction->base, container_type,
8674 instruction->field_count, instruction->fields, depends_on_compile_var);8718 instruction->field_count, instruction->fields, depends_on_compile_var);
...@@ -8678,7 +8722,7 @@ static TypeTableEntry *ir_analyze_min_max(IrAnalyze *ira, IrInstruction *source_...@@ -8678,7 +8722,7 @@ static TypeTableEntry *ir_analyze_min_max(IrAnalyze *ira, IrInstruction *source_
8678 IrInstruction *target_type_value, bool is_max)8722 IrInstruction *target_type_value, bool is_max)
8679{8723{
8680 TypeTableEntry *target_type = ir_resolve_type(ira, target_type_value);8724 TypeTableEntry *target_type = ir_resolve_type(ira, target_type_value);
8681 bool depends_on_compile_var = target_type_value->static_value.depends_on_compile_var;8725 bool depends_on_compile_var = target_type_value->value.depends_on_compile_var;
8682 TypeTableEntry *canon_type = get_underlying_type(target_type);8726 TypeTableEntry *canon_type = get_underlying_type(target_type);
8683 switch (canon_type->id) {8727 switch (canon_type->id) {
8684 case TypeTableEntryIdInvalid:8728 case TypeTableEntryIdInvalid:
...@@ -8763,35 +8807,22 @@ static TypeTableEntry *ir_analyze_instruction_compile_err(IrAnalyze *ira,...@@ -8763,35 +8807,22 @@ static TypeTableEntry *ir_analyze_instruction_compile_err(IrAnalyze *ira,
87638807
8764static TypeTableEntry *ir_analyze_instruction_err_name(IrAnalyze *ira, IrInstructionErrName *instruction) {8808static TypeTableEntry *ir_analyze_instruction_err_name(IrAnalyze *ira, IrInstructionErrName *instruction) {
8765 IrInstruction *value = instruction->value->other;8809 IrInstruction *value = instruction->value->other;
8766 if (value->type_entry->id == TypeTableEntryIdInvalid)8810 if (value->value.type->id == TypeTableEntryIdInvalid)
8767 return ira->codegen->builtin_types.entry_invalid;8811 return ira->codegen->builtin_types.entry_invalid;
87688812
8769 IrInstruction *casted_value = ir_implicit_cast(ira, value, value->type_entry);8813 IrInstruction *casted_value = ir_implicit_cast(ira, value, value->value.type);
8770 if (casted_value->type_entry->id == TypeTableEntryIdInvalid)8814 if (casted_value->value.type->id == TypeTableEntryIdInvalid)
8771 return ira->codegen->builtin_types.entry_invalid;8815 return ira->codegen->builtin_types.entry_invalid;
87728816
8773 TypeTableEntry *str_type = get_slice_type(ira->codegen, ira->codegen->builtin_types.entry_u8, true);8817 TypeTableEntry *str_type = get_slice_type(ira->codegen, ira->codegen->builtin_types.entry_u8, true);
8774 if (casted_value->static_value.special == ConstValSpecialStatic) {8818 if (casted_value->value.special == ConstValSpecialStatic) {
8775 ErrorTableEntry *err = casted_value->static_value.data.x_pure_err;8819 ErrorTableEntry *err = casted_value->value.data.x_pure_err;
8776 if (!err->cached_error_name_val) {8820 if (!err->cached_error_name_val) {
8777 err->cached_error_name_val = allocate<ConstExprValue>(1);8821 ConstExprValue *array_val = create_const_str_lit(ira->codegen, &err->name);
8778 err->cached_error_name_val->special = ConstValSpecialStatic;8822 err->cached_error_name_val = create_const_slice(ira->codegen, array_val, 0, buf_len(&err->name), true);
8779 err->cached_error_name_val->data.x_struct.fields = allocate<ConstExprValue>(2);
8780
8781 ConstExprValue *array_val = allocate<ConstExprValue>(1);
8782 init_const_str_lit(array_val, &err->name);
8783
8784 ConstExprValue *ptr_val = &err->cached_error_name_val->data.x_struct.fields[slice_ptr_index];
8785 ptr_val->special = ConstValSpecialStatic;
8786 ptr_val->data.x_ptr.base_ptr = array_val;
8787 ptr_val->data.x_ptr.index = 0;
8788
8789 ConstExprValue *len_val = &err->cached_error_name_val->data.x_struct.fields[slice_len_index];
8790 len_val->special = ConstValSpecialStatic;
8791 bignum_init_unsigned(&len_val->data.x_bignum, buf_len(&err->name));
8792 }8823 }
8793 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base,8824 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base,
8794 casted_value->static_value.depends_on_compile_var);8825 casted_value->value.depends_on_compile_var);
8795 *out_val = *err->cached_error_name_val;8826 *out_val = *err->cached_error_name_val;
8796 return str_type;8827 return str_type;
8797 }8828 }
...@@ -8813,7 +8844,7 @@ static TypeTableEntry *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstruc...@@ -8813,7 +8844,7 @@ static TypeTableEntry *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstruc
8813 IrInstruction *result = ir_eval_const_value(ira->codegen, &cimport_scope->base, block_node, void_type,8844 IrInstruction *result = ir_eval_const_value(ira->codegen, &cimport_scope->base, block_node, void_type,
8814 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota, nullptr,8845 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota, nullptr,
8815 &cimport_scope->buf, block_node, nullptr, nullptr);8846 &cimport_scope->buf, block_node, nullptr, nullptr);
8816 if (result->type_entry->id == TypeTableEntryIdInvalid)8847 if (result->value.type->id == TypeTableEntryIdInvalid)
8817 return ira->codegen->builtin_types.entry_invalid;8848 return ira->codegen->builtin_types.entry_invalid;
88188849
8819 find_libc_include_path(ira->codegen);8850 find_libc_include_path(ira->codegen);
...@@ -8855,7 +8886,7 @@ static TypeTableEntry *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstruc...@@ -8855,7 +8886,7 @@ static TypeTableEntry *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstruc
88558886
8856static TypeTableEntry *ir_analyze_instruction_c_include(IrAnalyze *ira, IrInstructionCInclude *instruction) {8887static TypeTableEntry *ir_analyze_instruction_c_include(IrAnalyze *ira, IrInstructionCInclude *instruction) {
8857 IrInstruction *name_value = instruction->name->other;8888 IrInstruction *name_value = instruction->name->other;
8858 if (name_value->type_entry->id == TypeTableEntryIdInvalid)8889 if (name_value->value.type->id == TypeTableEntryIdInvalid)
8859 return ira->codegen->builtin_types.entry_invalid;8890 return ira->codegen->builtin_types.entry_invalid;
88608891
8861 Buf *include_name = ir_resolve_str(ira, name_value);8892 Buf *include_name = ir_resolve_str(ira, name_value);
...@@ -8874,7 +8905,7 @@ static TypeTableEntry *ir_analyze_instruction_c_include(IrAnalyze *ira, IrInstru...@@ -8874,7 +8905,7 @@ static TypeTableEntry *ir_analyze_instruction_c_include(IrAnalyze *ira, IrInstru
88748905
8875static TypeTableEntry *ir_analyze_instruction_c_define(IrAnalyze *ira, IrInstructionCDefine *instruction) {8906static TypeTableEntry *ir_analyze_instruction_c_define(IrAnalyze *ira, IrInstructionCDefine *instruction) {
8876 IrInstruction *name = instruction->name->other;8907 IrInstruction *name = instruction->name->other;
8877 if (name->type_entry->id == TypeTableEntryIdInvalid)8908 if (name->value.type->id == TypeTableEntryIdInvalid)
8878 return ira->codegen->builtin_types.entry_invalid;8909 return ira->codegen->builtin_types.entry_invalid;
88798910
8880 Buf *define_name = ir_resolve_str(ira, name);8911 Buf *define_name = ir_resolve_str(ira, name);
...@@ -8882,7 +8913,7 @@ static TypeTableEntry *ir_analyze_instruction_c_define(IrAnalyze *ira, IrInstruc...@@ -8882,7 +8913,7 @@ static TypeTableEntry *ir_analyze_instruction_c_define(IrAnalyze *ira, IrInstruc
8882 return ira->codegen->builtin_types.entry_invalid;8913 return ira->codegen->builtin_types.entry_invalid;
88838914
8884 IrInstruction *value = instruction->value->other;8915 IrInstruction *value = instruction->value->other;
8885 if (value->type_entry->id == TypeTableEntryIdInvalid)8916 if (value->value.type->id == TypeTableEntryIdInvalid)
8886 return ira->codegen->builtin_types.entry_invalid;8917 return ira->codegen->builtin_types.entry_invalid;
88878918
8888 Buf *define_value = ir_resolve_str(ira, value);8919 Buf *define_value = ir_resolve_str(ira, value);
...@@ -8901,7 +8932,7 @@ static TypeTableEntry *ir_analyze_instruction_c_define(IrAnalyze *ira, IrInstruc...@@ -8901,7 +8932,7 @@ static TypeTableEntry *ir_analyze_instruction_c_define(IrAnalyze *ira, IrInstruc
89018932
8902static TypeTableEntry *ir_analyze_instruction_c_undef(IrAnalyze *ira, IrInstructionCUndef *instruction) {8933static TypeTableEntry *ir_analyze_instruction_c_undef(IrAnalyze *ira, IrInstructionCUndef *instruction) {
8903 IrInstruction *name = instruction->name->other;8934 IrInstruction *name = instruction->name->other;
8904 if (name->type_entry->id == TypeTableEntryIdInvalid)8935 if (name->value.type->id == TypeTableEntryIdInvalid)
8905 return ira->codegen->builtin_types.entry_invalid;8936 return ira->codegen->builtin_types.entry_invalid;
89068937
8907 Buf *undef_name = ir_resolve_str(ira, name);8938 Buf *undef_name = ir_resolve_str(ira, name);
...@@ -8920,7 +8951,7 @@ static TypeTableEntry *ir_analyze_instruction_c_undef(IrAnalyze *ira, IrInstruct...@@ -8920,7 +8951,7 @@ static TypeTableEntry *ir_analyze_instruction_c_undef(IrAnalyze *ira, IrInstruct
89208951
8921static TypeTableEntry *ir_analyze_instruction_embed_file(IrAnalyze *ira, IrInstructionEmbedFile *instruction) {8952static TypeTableEntry *ir_analyze_instruction_embed_file(IrAnalyze *ira, IrInstructionEmbedFile *instruction) {
8922 IrInstruction *name = instruction->name->other;8953 IrInstruction *name = instruction->name->other;
8923 if (name->type_entry->id == TypeTableEntryIdInvalid)8954 if (name->value.type->id == TypeTableEntryIdInvalid)
8924 return ira->codegen->builtin_types.entry_invalid;8955 return ira->codegen->builtin_types.entry_invalid;
89258956
8926 Buf *rel_file_path = ir_resolve_str(ira, name);8957 Buf *rel_file_path = ir_resolve_str(ira, name);
...@@ -8953,26 +8984,26 @@ static TypeTableEntry *ir_analyze_instruction_embed_file(IrAnalyze *ira, IrInstr...@@ -8953,26 +8984,26 @@ static TypeTableEntry *ir_analyze_instruction_embed_file(IrAnalyze *ira, IrInstr
89538984
8954 bool depends_on_compile_var = true;8985 bool depends_on_compile_var = true;
8955 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base, depends_on_compile_var);8986 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base, depends_on_compile_var);
8956 init_const_str_lit(out_val, &file_contents);8987 init_const_str_lit(ira->codegen, out_val, &file_contents);
89578988
8958 return get_array_type(ira->codegen, ira->codegen->builtin_types.entry_u8, buf_len(&file_contents));8989 return get_array_type(ira->codegen, ira->codegen->builtin_types.entry_u8, buf_len(&file_contents));
8959}8990}
89608991
8961static TypeTableEntry *ir_analyze_instruction_cmpxchg(IrAnalyze *ira, IrInstructionCmpxchg *instruction) {8992static TypeTableEntry *ir_analyze_instruction_cmpxchg(IrAnalyze *ira, IrInstructionCmpxchg *instruction) {
8962 IrInstruction *ptr = instruction->ptr->other;8993 IrInstruction *ptr = instruction->ptr->other;
8963 if (ptr->type_entry->id == TypeTableEntryIdInvalid)8994 if (ptr->value.type->id == TypeTableEntryIdInvalid)
8964 return ira->codegen->builtin_types.entry_invalid;8995 return ira->codegen->builtin_types.entry_invalid;
89658996
8966 IrInstruction *cmp_value = instruction->cmp_value->other;8997 IrInstruction *cmp_value = instruction->cmp_value->other;
8967 if (cmp_value->type_entry->id == TypeTableEntryIdInvalid)8998 if (cmp_value->value.type->id == TypeTableEntryIdInvalid)
8968 return ira->codegen->builtin_types.entry_invalid;8999 return ira->codegen->builtin_types.entry_invalid;
89699000
8970 IrInstruction *new_value = instruction->new_value->other;9001 IrInstruction *new_value = instruction->new_value->other;
8971 if (new_value->type_entry->id == TypeTableEntryIdInvalid)9002 if (new_value->value.type->id == TypeTableEntryIdInvalid)
8972 return ira->codegen->builtin_types.entry_invalid;9003 return ira->codegen->builtin_types.entry_invalid;
89739004
8974 IrInstruction *success_order_value = instruction->success_order_value->other;9005 IrInstruction *success_order_value = instruction->success_order_value->other;
8975 if (success_order_value->type_entry->id == TypeTableEntryIdInvalid)9006 if (success_order_value->value.type->id == TypeTableEntryIdInvalid)
8976 return ira->codegen->builtin_types.entry_invalid;9007 return ira->codegen->builtin_types.entry_invalid;
89779008
8978 AtomicOrder success_order;9009 AtomicOrder success_order;
...@@ -8980,27 +9011,27 @@ static TypeTableEntry *ir_analyze_instruction_cmpxchg(IrAnalyze *ira, IrInstruct...@@ -8980,27 +9011,27 @@ static TypeTableEntry *ir_analyze_instruction_cmpxchg(IrAnalyze *ira, IrInstruct
8980 return ira->codegen->builtin_types.entry_invalid;9011 return ira->codegen->builtin_types.entry_invalid;
89819012
8982 IrInstruction *failure_order_value = instruction->failure_order_value->other;9013 IrInstruction *failure_order_value = instruction->failure_order_value->other;
8983 if (failure_order_value->type_entry->id == TypeTableEntryIdInvalid)9014 if (failure_order_value->value.type->id == TypeTableEntryIdInvalid)
8984 return ira->codegen->builtin_types.entry_invalid;9015 return ira->codegen->builtin_types.entry_invalid;
89859016
8986 AtomicOrder failure_order;9017 AtomicOrder failure_order;
8987 if (!ir_resolve_atomic_order(ira, failure_order_value, &failure_order))9018 if (!ir_resolve_atomic_order(ira, failure_order_value, &failure_order))
8988 return ira->codegen->builtin_types.entry_invalid;9019 return ira->codegen->builtin_types.entry_invalid;
89899020
8990 if (ptr->type_entry->id != TypeTableEntryIdPointer) {9021 if (ptr->value.type->id != TypeTableEntryIdPointer) {
8991 ir_add_error(ira, instruction->ptr,9022 ir_add_error(ira, instruction->ptr,
8992 buf_sprintf("expected pointer argument, found '%s'", buf_ptr(&ptr->type_entry->name)));9023 buf_sprintf("expected pointer argument, found '%s'", buf_ptr(&ptr->value.type->name)));
8993 return ira->codegen->builtin_types.entry_invalid;9024 return ira->codegen->builtin_types.entry_invalid;
8994 }9025 }
89959026
8996 TypeTableEntry *child_type = ptr->type_entry->data.pointer.child_type;9027 TypeTableEntry *child_type = ptr->value.type->data.pointer.child_type;
89979028
8998 IrInstruction *casted_cmp_value = ir_implicit_cast(ira, cmp_value, child_type);9029 IrInstruction *casted_cmp_value = ir_implicit_cast(ira, cmp_value, child_type);
8999 if (casted_cmp_value->type_entry->id == TypeTableEntryIdInvalid)9030 if (casted_cmp_value->value.type->id == TypeTableEntryIdInvalid)
9000 return ira->codegen->builtin_types.entry_invalid;9031 return ira->codegen->builtin_types.entry_invalid;
90019032
9002 IrInstruction *casted_new_value = ir_implicit_cast(ira, new_value, child_type);9033 IrInstruction *casted_new_value = ir_implicit_cast(ira, new_value, child_type);
9003 if (casted_new_value->type_entry->id == TypeTableEntryIdInvalid)9034 if (casted_new_value->value.type->id == TypeTableEntryIdInvalid)
9004 return ira->codegen->builtin_types.entry_invalid;9035 return ira->codegen->builtin_types.entry_invalid;
90059036
9006 if (success_order < AtomicOrderMonotonic) {9037 if (success_order < AtomicOrderMonotonic) {
...@@ -9031,7 +9062,7 @@ static TypeTableEntry *ir_analyze_instruction_cmpxchg(IrAnalyze *ira, IrInstruct...@@ -9031,7 +9062,7 @@ static TypeTableEntry *ir_analyze_instruction_cmpxchg(IrAnalyze *ira, IrInstruct
90319062
9032static TypeTableEntry *ir_analyze_instruction_fence(IrAnalyze *ira, IrInstructionFence *instruction) {9063static TypeTableEntry *ir_analyze_instruction_fence(IrAnalyze *ira, IrInstructionFence *instruction) {
9033 IrInstruction *order_value = instruction->order_value->other;9064 IrInstruction *order_value = instruction->order_value->other;
9034 if (order_value->type_entry->id == TypeTableEntryIdInvalid)9065 if (order_value->value.type->id == TypeTableEntryIdInvalid)
9035 return ira->codegen->builtin_types.entry_invalid;9066 return ira->codegen->builtin_types.entry_invalid;
90369067
9037 AtomicOrder order;9068 AtomicOrder order;
...@@ -9044,11 +9075,11 @@ static TypeTableEntry *ir_analyze_instruction_fence(IrAnalyze *ira, IrInstructio...@@ -9044,11 +9075,11 @@ static TypeTableEntry *ir_analyze_instruction_fence(IrAnalyze *ira, IrInstructio
90449075
9045static TypeTableEntry *ir_analyze_instruction_div_exact(IrAnalyze *ira, IrInstructionDivExact *instruction) {9076static TypeTableEntry *ir_analyze_instruction_div_exact(IrAnalyze *ira, IrInstructionDivExact *instruction) {
9046 IrInstruction *op1 = instruction->op1->other;9077 IrInstruction *op1 = instruction->op1->other;
9047 if (op1->type_entry->id == TypeTableEntryIdInvalid)9078 if (op1->value.type->id == TypeTableEntryIdInvalid)
9048 return ira->codegen->builtin_types.entry_invalid;9079 return ira->codegen->builtin_types.entry_invalid;
90499080
9050 IrInstruction *op2 = instruction->op2->other;9081 IrInstruction *op2 = instruction->op2->other;
9051 if (op2->type_entry->id == TypeTableEntryIdInvalid)9082 if (op2->value.type->id == TypeTableEntryIdInvalid)
9052 return ira->codegen->builtin_types.entry_invalid;9083 return ira->codegen->builtin_types.entry_invalid;
90539084
90549085
...@@ -9070,15 +9101,15 @@ static TypeTableEntry *ir_analyze_instruction_div_exact(IrAnalyze *ira, IrInstru...@@ -9070,15 +9101,15 @@ static TypeTableEntry *ir_analyze_instruction_div_exact(IrAnalyze *ira, IrInstru
9070 }9101 }
90719102
9072 IrInstruction *casted_op1 = ir_implicit_cast(ira, op1, result_type);9103 IrInstruction *casted_op1 = ir_implicit_cast(ira, op1, result_type);
9073 if (casted_op1->type_entry->id == TypeTableEntryIdInvalid)9104 if (casted_op1->value.type->id == TypeTableEntryIdInvalid)
9074 return ira->codegen->builtin_types.entry_invalid;9105 return ira->codegen->builtin_types.entry_invalid;
90759106
9076 IrInstruction *casted_op2 = ir_implicit_cast(ira, op2, result_type);9107 IrInstruction *casted_op2 = ir_implicit_cast(ira, op2, result_type);
9077 if (casted_op2->type_entry->id == TypeTableEntryIdInvalid)9108 if (casted_op2->value.type->id == TypeTableEntryIdInvalid)
9078 return ira->codegen->builtin_types.entry_invalid;9109 return ira->codegen->builtin_types.entry_invalid;
90799110
9080 if (casted_op1->static_value.special == ConstValSpecialStatic &&9111 if (casted_op1->value.special == ConstValSpecialStatic &&
9081 casted_op2->static_value.special == ConstValSpecialStatic)9112 casted_op2->value.special == ConstValSpecialStatic)
9082 {9113 {
9083 ConstExprValue *op1_val = ir_resolve_const(ira, casted_op1, UndefBad);9114 ConstExprValue *op1_val = ir_resolve_const(ira, casted_op1, UndefBad);
9084 ConstExprValue *op2_val = ir_resolve_const(ira, casted_op2, UndefBad);9115 ConstExprValue *op2_val = ir_resolve_const(ira, casted_op2, UndefBad);
...@@ -9101,8 +9132,8 @@ static TypeTableEntry *ir_analyze_instruction_div_exact(IrAnalyze *ira, IrInstru...@@ -9101,8 +9132,8 @@ static TypeTableEntry *ir_analyze_instruction_div_exact(IrAnalyze *ira, IrInstru
9101 return ira->codegen->builtin_types.entry_invalid;9132 return ira->codegen->builtin_types.entry_invalid;
9102 }9133 }
91039134
9104 bool depends_on_compile_var = casted_op1->static_value.depends_on_compile_var ||9135 bool depends_on_compile_var = casted_op1->value.depends_on_compile_var ||
9105 casted_op2->static_value.depends_on_compile_var;9136 casted_op2->value.depends_on_compile_var;
9106 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base, depends_on_compile_var);9137 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base, depends_on_compile_var);
9107 bignum_div(&out_val->data.x_bignum, &op1_val->data.x_bignum, &op2_val->data.x_bignum);9138 bignum_div(&out_val->data.x_bignum, &op1_val->data.x_bignum, &op2_val->data.x_bignum);
9108 return result_type;9139 return result_type;
...@@ -9129,7 +9160,7 @@ static TypeTableEntry *ir_analyze_instruction_truncate(IrAnalyze *ira, IrInstruc...@@ -9129,7 +9160,7 @@ static TypeTableEntry *ir_analyze_instruction_truncate(IrAnalyze *ira, IrInstruc
9129 }9160 }
91309161
9131 IrInstruction *target = instruction->target->other;9162 IrInstruction *target = instruction->target->other;
9132 TypeTableEntry *src_type = target->type_entry;9163 TypeTableEntry *src_type = target->value.type;
9133 TypeTableEntry *canon_src_type = get_underlying_type(src_type);9164 TypeTableEntry *canon_src_type = get_underlying_type(src_type);
9134 if (canon_src_type->id == TypeTableEntryIdInvalid)9165 if (canon_src_type->id == TypeTableEntryIdInvalid)
9135 return ira->codegen->builtin_types.entry_invalid;9166 return ira->codegen->builtin_types.entry_invalid;
...@@ -9154,10 +9185,10 @@ static TypeTableEntry *ir_analyze_instruction_truncate(IrAnalyze *ira, IrInstruc...@@ -9154,10 +9185,10 @@ static TypeTableEntry *ir_analyze_instruction_truncate(IrAnalyze *ira, IrInstruc
9154 return ira->codegen->builtin_types.entry_invalid;9185 return ira->codegen->builtin_types.entry_invalid;
9155 }9186 }
91569187
9157 if (target->static_value.special == ConstValSpecialStatic) {9188 if (target->value.special == ConstValSpecialStatic) {
9158 bool depends_on_compile_var = dest_type_value->static_value.depends_on_compile_var || target->static_value.depends_on_compile_var;9189 bool depends_on_compile_var = dest_type_value->value.depends_on_compile_var || target->value.depends_on_compile_var;
9159 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base, depends_on_compile_var);9190 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base, depends_on_compile_var);
9160 bignum_init_bignum(&out_val->data.x_bignum, &target->static_value.data.x_bignum);9191 bignum_init_bignum(&out_val->data.x_bignum, &target->value.data.x_bignum);
9161 bignum_truncate(&out_val->data.x_bignum, canon_dest_type->data.integral.bit_count);9192 bignum_truncate(&out_val->data.x_bignum, canon_dest_type->data.integral.bit_count);
9162 return dest_type;9193 return dest_type;
9163 }9194 }
...@@ -9177,8 +9208,8 @@ static TypeTableEntry *ir_analyze_instruction_int_type(IrAnalyze *ira, IrInstruc...@@ -9177,8 +9208,8 @@ static TypeTableEntry *ir_analyze_instruction_int_type(IrAnalyze *ira, IrInstruc
9177 if (!ir_resolve_usize(ira, bit_count_value, &bit_count))9208 if (!ir_resolve_usize(ira, bit_count_value, &bit_count))
9178 return ira->codegen->builtin_types.entry_invalid;9209 return ira->codegen->builtin_types.entry_invalid;
91799210
9180 bool depends_on_compile_var = is_signed_value->static_value.depends_on_compile_var ||9211 bool depends_on_compile_var = is_signed_value->value.depends_on_compile_var ||
9181 bit_count_value->static_value.depends_on_compile_var;9212 bit_count_value->value.depends_on_compile_var;
91829213
9183 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base, depends_on_compile_var);9214 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base, depends_on_compile_var);
9184 out_val->data.x_type = get_int_type(ira->codegen, is_signed, bit_count);9215 out_val->data.x_type = get_int_type(ira->codegen, is_signed, bit_count);
...@@ -9187,19 +9218,19 @@ static TypeTableEntry *ir_analyze_instruction_int_type(IrAnalyze *ira, IrInstruc...@@ -9187,19 +9218,19 @@ static TypeTableEntry *ir_analyze_instruction_int_type(IrAnalyze *ira, IrInstruc
91879218
9188static TypeTableEntry *ir_analyze_instruction_bool_not(IrAnalyze *ira, IrInstructionBoolNot *instruction) {9219static TypeTableEntry *ir_analyze_instruction_bool_not(IrAnalyze *ira, IrInstructionBoolNot *instruction) {
9189 IrInstruction *value = instruction->value->other;9220 IrInstruction *value = instruction->value->other;
9190 if (value->type_entry->id == TypeTableEntryIdInvalid)9221 if (value->value.type->id == TypeTableEntryIdInvalid)
9191 return ira->codegen->builtin_types.entry_invalid;9222 return ira->codegen->builtin_types.entry_invalid;
91929223
9193 TypeTableEntry *bool_type = ira->codegen->builtin_types.entry_bool;9224 TypeTableEntry *bool_type = ira->codegen->builtin_types.entry_bool;
91949225
9195 IrInstruction *casted_value = ir_implicit_cast(ira, value, bool_type);9226 IrInstruction *casted_value = ir_implicit_cast(ira, value, bool_type);
9196 if (casted_value->type_entry->id == TypeTableEntryIdInvalid)9227 if (casted_value->value.type->id == TypeTableEntryIdInvalid)
9197 return ira->codegen->builtin_types.entry_invalid;9228 return ira->codegen->builtin_types.entry_invalid;
91989229
9199 if (casted_value->static_value.special != ConstValSpecialRuntime) {9230 if (casted_value->value.special != ConstValSpecialRuntime) {
9200 bool depends_on_compile_var = casted_value->static_value.depends_on_compile_var;9231 bool depends_on_compile_var = casted_value->value.depends_on_compile_var;
9201 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base, depends_on_compile_var);9232 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base, depends_on_compile_var);
9202 out_val->data.x_bool = !casted_value->static_value.data.x_bool;9233 out_val->data.x_bool = !casted_value->value.data.x_bool;
9203 return bool_type;9234 return bool_type;
9204 }9235 }
92059236
...@@ -9209,11 +9240,11 @@ static TypeTableEntry *ir_analyze_instruction_bool_not(IrAnalyze *ira, IrInstruc...@@ -9209,11 +9240,11 @@ static TypeTableEntry *ir_analyze_instruction_bool_not(IrAnalyze *ira, IrInstruc
92099240
9210static TypeTableEntry *ir_analyze_instruction_alloca(IrAnalyze *ira, IrInstructionAlloca *instruction) {9241static TypeTableEntry *ir_analyze_instruction_alloca(IrAnalyze *ira, IrInstructionAlloca *instruction) {
9211 IrInstruction *type_value = instruction->type_value->other;9242 IrInstruction *type_value = instruction->type_value->other;
9212 if (type_value->type_entry->id == TypeTableEntryIdInvalid)9243 if (type_value->value.type->id == TypeTableEntryIdInvalid)
9213 return ira->codegen->builtin_types.entry_invalid;9244 return ira->codegen->builtin_types.entry_invalid;
92149245
9215 IrInstruction *count_value = instruction->count->other;9246 IrInstruction *count_value = instruction->count->other;
9216 if (count_value->type_entry->id == TypeTableEntryIdInvalid)9247 if (count_value->value.type->id == TypeTableEntryIdInvalid)
9217 return ira->codegen->builtin_types.entry_invalid;9248 return ira->codegen->builtin_types.entry_invalid;
92189249
9219 TypeTableEntry *child_type = ir_resolve_type(ira, type_value);9250 TypeTableEntry *child_type = ir_resolve_type(ira, type_value);
...@@ -9234,15 +9265,15 @@ static TypeTableEntry *ir_analyze_instruction_alloca(IrAnalyze *ira, IrInstructi...@@ -9234,15 +9265,15 @@ static TypeTableEntry *ir_analyze_instruction_alloca(IrAnalyze *ira, IrInstructi
92349265
9235static TypeTableEntry *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructionMemset *instruction) {9266static TypeTableEntry *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructionMemset *instruction) {
9236 IrInstruction *dest_ptr = instruction->dest_ptr->other;9267 IrInstruction *dest_ptr = instruction->dest_ptr->other;
9237 if (dest_ptr->type_entry->id == TypeTableEntryIdInvalid)9268 if (dest_ptr->value.type->id == TypeTableEntryIdInvalid)
9238 return ira->codegen->builtin_types.entry_invalid;9269 return ira->codegen->builtin_types.entry_invalid;
92399270
9240 IrInstruction *byte_value = instruction->byte->other;9271 IrInstruction *byte_value = instruction->byte->other;
9241 if (byte_value->type_entry->id == TypeTableEntryIdInvalid)9272 if (byte_value->value.type->id == TypeTableEntryIdInvalid)
9242 return ira->codegen->builtin_types.entry_invalid;9273 return ira->codegen->builtin_types.entry_invalid;
92439274
9244 IrInstruction *count_value = instruction->count->other;9275 IrInstruction *count_value = instruction->count->other;
9245 if (count_value->type_entry->id == TypeTableEntryIdInvalid)9276 if (count_value->value.type->id == TypeTableEntryIdInvalid)
9246 return ira->codegen->builtin_types.entry_invalid;9277 return ira->codegen->builtin_types.entry_invalid;
92479278
9248 TypeTableEntry *usize = ira->codegen->builtin_types.entry_usize;9279 TypeTableEntry *usize = ira->codegen->builtin_types.entry_usize;
...@@ -9250,22 +9281,22 @@ static TypeTableEntry *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructi...@@ -9250,22 +9281,22 @@ static TypeTableEntry *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructi
9250 TypeTableEntry *u8_ptr = get_pointer_to_type(ira->codegen, u8, false);9281 TypeTableEntry *u8_ptr = get_pointer_to_type(ira->codegen, u8, false);
92519282
9252 IrInstruction *casted_dest_ptr = ir_implicit_cast(ira, dest_ptr, u8_ptr);9283 IrInstruction *casted_dest_ptr = ir_implicit_cast(ira, dest_ptr, u8_ptr);
9253 if (casted_dest_ptr->type_entry->id == TypeTableEntryIdInvalid)9284 if (casted_dest_ptr->value.type->id == TypeTableEntryIdInvalid)
9254 return ira->codegen->builtin_types.entry_invalid;9285 return ira->codegen->builtin_types.entry_invalid;
92559286
9256 IrInstruction *casted_byte = ir_implicit_cast(ira, byte_value, u8);9287 IrInstruction *casted_byte = ir_implicit_cast(ira, byte_value, u8);
9257 if (casted_byte->type_entry->id == TypeTableEntryIdInvalid)9288 if (casted_byte->value.type->id == TypeTableEntryIdInvalid)
9258 return ira->codegen->builtin_types.entry_invalid;9289 return ira->codegen->builtin_types.entry_invalid;
92599290
9260 IrInstruction *casted_count = ir_implicit_cast(ira, count_value, usize);9291 IrInstruction *casted_count = ir_implicit_cast(ira, count_value, usize);
9261 if (casted_count->type_entry->id == TypeTableEntryIdInvalid)9292 if (casted_count->value.type->id == TypeTableEntryIdInvalid)
9262 return ira->codegen->builtin_types.entry_invalid;9293 return ira->codegen->builtin_types.entry_invalid;
92639294
9264 if (casted_dest_ptr->static_value.special == ConstValSpecialStatic &&9295 if (casted_dest_ptr->value.special == ConstValSpecialStatic &&
9265 casted_byte->static_value.special == ConstValSpecialStatic &&9296 casted_byte->value.special == ConstValSpecialStatic &&
9266 casted_count->static_value.special == ConstValSpecialStatic)9297 casted_count->value.special == ConstValSpecialStatic)
9267 {9298 {
9268 ConstExprValue *dest_ptr_val = &casted_dest_ptr->static_value;9299 ConstExprValue *dest_ptr_val = &casted_dest_ptr->value;
92699300
9270 ConstExprValue *dest_elements;9301 ConstExprValue *dest_elements;
9271 size_t start;9302 size_t start;
...@@ -9281,14 +9312,14 @@ static TypeTableEntry *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructi...@@ -9281,14 +9312,14 @@ static TypeTableEntry *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructi
9281 bound_end = array_val->data.x_array.size;9312 bound_end = array_val->data.x_array.size;
9282 }9313 }
92839314
9284 size_t count = casted_count->static_value.data.x_bignum.data.x_uint;9315 size_t count = casted_count->value.data.x_bignum.data.x_uint;
9285 size_t end = start + count;9316 size_t end = start + count;
9286 if (end > bound_end) {9317 if (end > bound_end) {
9287 ir_add_error(ira, count_value, buf_sprintf("out of bounds pointer access"));9318 ir_add_error(ira, count_value, buf_sprintf("out of bounds pointer access"));
9288 return ira->codegen->builtin_types.entry_invalid;9319 return ira->codegen->builtin_types.entry_invalid;
9289 }9320 }
92909321
9291 ConstExprValue *byte_val = &casted_byte->static_value;9322 ConstExprValue *byte_val = &casted_byte->value;
9292 for (size_t i = start; i < end; i += 1) {9323 for (size_t i = start; i < end; i += 1) {
9293 dest_elements[i] = *byte_val;9324 dest_elements[i] = *byte_val;
9294 }9325 }
...@@ -9303,15 +9334,15 @@ static TypeTableEntry *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructi...@@ -9303,15 +9334,15 @@ static TypeTableEntry *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructi
93039334
9304static TypeTableEntry *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructionMemcpy *instruction) {9335static TypeTableEntry *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructionMemcpy *instruction) {
9305 IrInstruction *dest_ptr = instruction->dest_ptr->other;9336 IrInstruction *dest_ptr = instruction->dest_ptr->other;
9306 if (dest_ptr->type_entry->id == TypeTableEntryIdInvalid)9337 if (dest_ptr->value.type->id == TypeTableEntryIdInvalid)
9307 return ira->codegen->builtin_types.entry_invalid;9338 return ira->codegen->builtin_types.entry_invalid;
93089339
9309 IrInstruction *src_ptr = instruction->src_ptr->other;9340 IrInstruction *src_ptr = instruction->src_ptr->other;
9310 if (src_ptr->type_entry->id == TypeTableEntryIdInvalid)9341 if (src_ptr->value.type->id == TypeTableEntryIdInvalid)
9311 return ira->codegen->builtin_types.entry_invalid;9342 return ira->codegen->builtin_types.entry_invalid;
93129343
9313 IrInstruction *count_value = instruction->count->other;9344 IrInstruction *count_value = instruction->count->other;
9314 if (count_value->type_entry->id == TypeTableEntryIdInvalid)9345 if (count_value->value.type->id == TypeTableEntryIdInvalid)
9315 return ira->codegen->builtin_types.entry_invalid;9346 return ira->codegen->builtin_types.entry_invalid;
93169347
9317 TypeTableEntry *usize = ira->codegen->builtin_types.entry_usize;9348 TypeTableEntry *usize = ira->codegen->builtin_types.entry_usize;
...@@ -9320,24 +9351,24 @@ static TypeTableEntry *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructi...@@ -9320,24 +9351,24 @@ static TypeTableEntry *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructi
9320 TypeTableEntry *u8_ptr_const = get_pointer_to_type(ira->codegen, u8, true);9351 TypeTableEntry *u8_ptr_const = get_pointer_to_type(ira->codegen, u8, true);
93219352
9322 IrInstruction *casted_dest_ptr = ir_implicit_cast(ira, dest_ptr, u8_ptr_mut);9353 IrInstruction *casted_dest_ptr = ir_implicit_cast(ira, dest_ptr, u8_ptr_mut);
9323 if (casted_dest_ptr->type_entry->id == TypeTableEntryIdInvalid)9354 if (casted_dest_ptr->value.type->id == TypeTableEntryIdInvalid)
9324 return ira->codegen->builtin_types.entry_invalid;9355 return ira->codegen->builtin_types.entry_invalid;
93259356
9326 IrInstruction *casted_src_ptr = ir_implicit_cast(ira, src_ptr, u8_ptr_const);9357 IrInstruction *casted_src_ptr = ir_implicit_cast(ira, src_ptr, u8_ptr_const);
9327 if (casted_src_ptr->type_entry->id == TypeTableEntryIdInvalid)9358 if (casted_src_ptr->value.type->id == TypeTableEntryIdInvalid)
9328 return ira->codegen->builtin_types.entry_invalid;9359 return ira->codegen->builtin_types.entry_invalid;
93299360
9330 IrInstruction *casted_count = ir_implicit_cast(ira, count_value, usize);9361 IrInstruction *casted_count = ir_implicit_cast(ira, count_value, usize);
9331 if (casted_count->type_entry->id == TypeTableEntryIdInvalid)9362 if (casted_count->value.type->id == TypeTableEntryIdInvalid)
9332 return ira->codegen->builtin_types.entry_invalid;9363 return ira->codegen->builtin_types.entry_invalid;
93339364
9334 if (casted_dest_ptr->static_value.special == ConstValSpecialStatic &&9365 if (casted_dest_ptr->value.special == ConstValSpecialStatic &&
9335 casted_src_ptr->static_value.special == ConstValSpecialStatic &&9366 casted_src_ptr->value.special == ConstValSpecialStatic &&
9336 casted_count->static_value.special == ConstValSpecialStatic)9367 casted_count->value.special == ConstValSpecialStatic)
9337 {9368 {
9338 size_t count = casted_count->static_value.data.x_bignum.data.x_uint;9369 size_t count = casted_count->value.data.x_bignum.data.x_uint;
93399370
9340 ConstExprValue *dest_ptr_val = &casted_dest_ptr->static_value;9371 ConstExprValue *dest_ptr_val = &casted_dest_ptr->value;
9341 ConstExprValue *dest_elements;9372 ConstExprValue *dest_elements;
9342 size_t dest_start;9373 size_t dest_start;
9343 size_t dest_end;9374 size_t dest_end;
...@@ -9357,7 +9388,7 @@ static TypeTableEntry *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructi...@@ -9357,7 +9388,7 @@ static TypeTableEntry *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructi
9357 return ira->codegen->builtin_types.entry_invalid;9388 return ira->codegen->builtin_types.entry_invalid;
9358 }9389 }
93599390
9360 ConstExprValue *src_ptr_val = &casted_src_ptr->static_value;9391 ConstExprValue *src_ptr_val = &casted_src_ptr->value;
9361 ConstExprValue *src_elements;9392 ConstExprValue *src_elements;
9362 size_t src_start;9393 size_t src_start;
9363 size_t src_end;9394 size_t src_end;
...@@ -9393,31 +9424,31 @@ static TypeTableEntry *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructi...@@ -9393,31 +9424,31 @@ static TypeTableEntry *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructi
93939424
9394static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructionSlice *instruction) {9425static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructionSlice *instruction) {
9395 IrInstruction *ptr = instruction->ptr->other;9426 IrInstruction *ptr = instruction->ptr->other;
9396 if (ptr->type_entry->id == TypeTableEntryIdInvalid)9427 if (ptr->value.type->id == TypeTableEntryIdInvalid)
9397 return ira->codegen->builtin_types.entry_invalid;9428 return ira->codegen->builtin_types.entry_invalid;
93989429
9399 IrInstruction *start = instruction->start->other;9430 IrInstruction *start = instruction->start->other;
9400 if (start->type_entry->id == TypeTableEntryIdInvalid)9431 if (start->value.type->id == TypeTableEntryIdInvalid)
9401 return ira->codegen->builtin_types.entry_invalid;9432 return ira->codegen->builtin_types.entry_invalid;
94029433
9403 TypeTableEntry *usize = ira->codegen->builtin_types.entry_usize;9434 TypeTableEntry *usize = ira->codegen->builtin_types.entry_usize;
9404 IrInstruction *casted_start = ir_implicit_cast(ira, start, usize);9435 IrInstruction *casted_start = ir_implicit_cast(ira, start, usize);
9405 if (casted_start->type_entry->id == TypeTableEntryIdInvalid)9436 if (casted_start->value.type->id == TypeTableEntryIdInvalid)
9406 return ira->codegen->builtin_types.entry_invalid;9437 return ira->codegen->builtin_types.entry_invalid;
94079438
9408 IrInstruction *end;9439 IrInstruction *end;
9409 if (instruction->end) {9440 if (instruction->end) {
9410 end = instruction->end->other;9441 end = instruction->end->other;
9411 if (end->type_entry->id == TypeTableEntryIdInvalid)9442 if (end->value.type->id == TypeTableEntryIdInvalid)
9412 return ira->codegen->builtin_types.entry_invalid;9443 return ira->codegen->builtin_types.entry_invalid;
9413 end = ir_implicit_cast(ira, end, usize);9444 end = ir_implicit_cast(ira, end, usize);
9414 if (end->type_entry->id == TypeTableEntryIdInvalid)9445 if (end->value.type->id == TypeTableEntryIdInvalid)
9415 return ira->codegen->builtin_types.entry_invalid;9446 return ira->codegen->builtin_types.entry_invalid;
9416 } else {9447 } else {
9417 end = nullptr;9448 end = nullptr;
9418 }9449 }
94199450
9420 TypeTableEntry *array_type = get_underlying_type(ptr->type_entry);9451 TypeTableEntry *array_type = get_underlying_type(ptr->value.type);
94219452
9422 TypeTableEntry *return_type;9453 TypeTableEntry *return_type;
94239454
...@@ -9435,38 +9466,38 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio...@@ -9435,38 +9466,38 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio
9435 instruction->is_const);9466 instruction->is_const);
9436 } else {9467 } else {
9437 ir_add_error(ira, &instruction->base,9468 ir_add_error(ira, &instruction->base,
9438 buf_sprintf("slice of non-array type '%s'", buf_ptr(&ptr->type_entry->name)));9469 buf_sprintf("slice of non-array type '%s'", buf_ptr(&ptr->value.type->name)));
9439 // TODO if this is a typedecl, add error note showing the declaration of the type decl9470 // TODO if this is a typedecl, add error note showing the declaration of the type decl
9440 return ira->codegen->builtin_types.entry_invalid;9471 return ira->codegen->builtin_types.entry_invalid;
9441 }9472 }
94429473
9443 if (ptr->static_value.special == ConstValSpecialStatic &&9474 if (ptr->value.special == ConstValSpecialStatic &&
9444 casted_start->static_value.special == ConstValSpecialStatic &&9475 casted_start->value.special == ConstValSpecialStatic &&
9445 (!end || end->static_value.special == ConstValSpecialStatic))9476 (!end || end->value.special == ConstValSpecialStatic))
9446 {9477 {
9447 bool depends_on_compile_var =9478 bool depends_on_compile_var =
9448 ptr->static_value.depends_on_compile_var ||9479 ptr->value.depends_on_compile_var ||
9449 casted_start->static_value.depends_on_compile_var ||9480 casted_start->value.depends_on_compile_var ||
9450 (end ? end->static_value.depends_on_compile_var : false);9481 (end ? end->value.depends_on_compile_var : false);
94519482
9452 ConstExprValue *base_ptr;9483 ConstExprValue *base_ptr;
9453 size_t abs_offset;9484 size_t abs_offset;
9454 size_t rel_end;9485 size_t rel_end;
9455 if (array_type->id == TypeTableEntryIdArray) {9486 if (array_type->id == TypeTableEntryIdArray) {
9456 base_ptr = &ptr->static_value;9487 base_ptr = &ptr->value;
9457 abs_offset = 0;9488 abs_offset = 0;
9458 rel_end = array_type->data.array.len;9489 rel_end = array_type->data.array.len;
9459 } else if (array_type->id == TypeTableEntryIdPointer) {9490 } else if (array_type->id == TypeTableEntryIdPointer) {
9460 base_ptr = ptr->static_value.data.x_ptr.base_ptr;9491 base_ptr = ptr->value.data.x_ptr.base_ptr;
9461 abs_offset = ptr->static_value.data.x_ptr.index;9492 abs_offset = ptr->value.data.x_ptr.index;
9462 if (abs_offset == SIZE_MAX) {9493 if (abs_offset == SIZE_MAX) {
9463 rel_end = 1;9494 rel_end = 1;
9464 } else {9495 } else {
9465 rel_end = base_ptr->data.x_array.size - abs_offset;9496 rel_end = base_ptr->data.x_array.size - abs_offset;
9466 }9497 }
9467 } else if (is_slice(array_type)) {9498 } else if (is_slice(array_type)) {
9468 ConstExprValue *ptr_val = &ptr->static_value.data.x_struct.fields[slice_ptr_index];9499 ConstExprValue *ptr_val = &ptr->value.data.x_struct.fields[slice_ptr_index];
9469 ConstExprValue *len_val = &ptr->static_value.data.x_struct.fields[slice_len_index];9500 ConstExprValue *len_val = &ptr->value.data.x_struct.fields[slice_len_index];
9470 base_ptr = ptr_val->data.x_ptr.base_ptr;9501 base_ptr = ptr_val->data.x_ptr.base_ptr;
9471 abs_offset = ptr_val->data.x_ptr.index;9502 abs_offset = ptr_val->data.x_ptr.index;
94729503
...@@ -9479,7 +9510,7 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio...@@ -9479,7 +9510,7 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio
9479 zig_unreachable();9510 zig_unreachable();
9480 }9511 }
94819512
9482 uint64_t start_scalar = casted_start->static_value.data.x_bignum.data.x_uint;9513 uint64_t start_scalar = casted_start->value.data.x_bignum.data.x_uint;
9483 if (start_scalar > rel_end) {9514 if (start_scalar > rel_end) {
9484 ir_add_error(ira, &instruction->base, buf_sprintf("out of bounds slice"));9515 ir_add_error(ira, &instruction->base, buf_sprintf("out of bounds slice"));
9485 return ira->codegen->builtin_types.entry_invalid;9516 return ira->codegen->builtin_types.entry_invalid;
...@@ -9487,7 +9518,7 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio...@@ -9487,7 +9518,7 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio
94879518
9488 uint64_t end_scalar;9519 uint64_t end_scalar;
9489 if (end) {9520 if (end) {
9490 end_scalar = end->static_value.data.x_bignum.data.x_uint;9521 end_scalar = end->value.data.x_bignum.data.x_uint;
9491 } else {9522 } else {
9492 end_scalar = rel_end;9523 end_scalar = rel_end;
9493 }9524 }
...@@ -9504,18 +9535,17 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio...@@ -9504,18 +9535,17 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio
9504 out_val->data.x_struct.fields = allocate<ConstExprValue>(2);9535 out_val->data.x_struct.fields = allocate<ConstExprValue>(2);
95059536
9506 ConstExprValue *ptr_val = &out_val->data.x_struct.fields[slice_ptr_index];9537 ConstExprValue *ptr_val = &out_val->data.x_struct.fields[slice_ptr_index];
9507 ptr_val->special = ConstValSpecialStatic;9538 size_t index = (abs_offset != SIZE_MAX) ? (abs_offset + start_scalar) : SIZE_MAX;
9508 ptr_val->data.x_ptr.base_ptr = base_ptr;9539 init_const_ptr(ira->codegen, ptr_val, base_ptr, index, instruction->is_const);
9509 ptr_val->data.x_ptr.index = (abs_offset != SIZE_MAX) ? (abs_offset + start_scalar) : SIZE_MAX;
95109540
9511 ConstExprValue *len_val = &out_val->data.x_struct.fields[slice_len_index];9541 ConstExprValue *len_val = &out_val->data.x_struct.fields[slice_len_index];
9512 len_val->special = ConstValSpecialStatic;9542 init_const_usize(ira->codegen, len_val, rel_end);
9513 bignum_init_unsigned(&len_val->data.x_bignum, rel_end);
95149543
9515 return return_type;9544 return return_type;
9516 }9545 }
95179546
9518 IrInstruction *new_instruction = ir_build_slice_from(&ira->new_irb, &instruction->base, ptr, casted_start, end, instruction->is_const);9547 IrInstruction *new_instruction = ir_build_slice_from(&ira->new_irb, &instruction->base, ptr,
9548 casted_start, end, instruction->is_const, instruction->safety_check_on);
9519 ir_add_alloca(ira, new_instruction, return_type);9549 ir_add_alloca(ira, new_instruction, return_type);
95209550
9521 return return_type;9551 return return_type;
...@@ -9523,7 +9553,7 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio...@@ -9523,7 +9553,7 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio
95239553
9524static TypeTableEntry *ir_analyze_instruction_member_count(IrAnalyze *ira, IrInstructionMemberCount *instruction) {9554static TypeTableEntry *ir_analyze_instruction_member_count(IrAnalyze *ira, IrInstructionMemberCount *instruction) {
9525 IrInstruction *container = instruction->container->other;9555 IrInstruction *container = instruction->container->other;
9526 if (container->type_entry->id == TypeTableEntryIdInvalid)9556 if (container->value.type->id == TypeTableEntryIdInvalid)
9527 return ira->codegen->builtin_types.entry_invalid;9557 return ira->codegen->builtin_types.entry_invalid;
9528 TypeTableEntry *container_type = ir_resolve_type(ira, container);9558 TypeTableEntry *container_type = ir_resolve_type(ira, container);
9529 TypeTableEntry *canon_type = get_underlying_type(container_type);9559 TypeTableEntry *canon_type = get_underlying_type(container_type);
...@@ -9542,7 +9572,7 @@ static TypeTableEntry *ir_analyze_instruction_member_count(IrAnalyze *ira, IrIns...@@ -9542,7 +9572,7 @@ static TypeTableEntry *ir_analyze_instruction_member_count(IrAnalyze *ira, IrIns
9542 return ira->codegen->builtin_types.entry_invalid;9572 return ira->codegen->builtin_types.entry_invalid;
9543 }9573 }
95449574
9545 bool depends_on_compile_var = container->static_value.depends_on_compile_var;9575 bool depends_on_compile_var = container->value.depends_on_compile_var;
9546 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base, depends_on_compile_var);9576 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base, depends_on_compile_var);
9547 bignum_init_unsigned(&out_val->data.x_bignum, result);9577 bignum_init_unsigned(&out_val->data.x_bignum, result);
9548 return ira->codegen->builtin_types.entry_num_lit_int;9578 return ira->codegen->builtin_types.entry_num_lit_int;
...@@ -9571,7 +9601,7 @@ static TypeTableEntry *ir_analyze_instruction_frame_address(IrAnalyze *ira, IrIn...@@ -9571,7 +9601,7 @@ static TypeTableEntry *ir_analyze_instruction_frame_address(IrAnalyze *ira, IrIn
95719601
9572static TypeTableEntry *ir_analyze_instruction_alignof(IrAnalyze *ira, IrInstructionAlignOf *instruction) {9602static TypeTableEntry *ir_analyze_instruction_alignof(IrAnalyze *ira, IrInstructionAlignOf *instruction) {
9573 IrInstruction *type_value = instruction->type_value->other;9603 IrInstruction *type_value = instruction->type_value->other;
9574 if (type_value->type_entry->id == TypeTableEntryIdInvalid)9604 if (type_value->value.type->id == TypeTableEntryIdInvalid)
9575 return ira->codegen->builtin_types.entry_invalid;9605 return ira->codegen->builtin_types.entry_invalid;
9576 TypeTableEntry *type_entry = ir_resolve_type(ira, type_value);9606 TypeTableEntry *type_entry = ir_resolve_type(ira, type_value);
95779607
...@@ -9583,7 +9613,7 @@ static TypeTableEntry *ir_analyze_instruction_alignof(IrAnalyze *ira, IrInstruct...@@ -9583,7 +9613,7 @@ static TypeTableEntry *ir_analyze_instruction_alignof(IrAnalyze *ira, IrInstruct
9583 return ira->codegen->builtin_types.entry_invalid;9613 return ira->codegen->builtin_types.entry_invalid;
9584 } else {9614 } else {
9585 uint64_t align_in_bytes = LLVMABISizeOfType(ira->codegen->target_data_ref, type_entry->type_ref);9615 uint64_t align_in_bytes = LLVMABISizeOfType(ira->codegen->target_data_ref, type_entry->type_ref);
9586 bool depends_on_compile_var = type_value->static_value.depends_on_compile_var;9616 bool depends_on_compile_var = type_value->value.depends_on_compile_var;
9587 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base, depends_on_compile_var);9617 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base, depends_on_compile_var);
9588 bignum_init_unsigned(&out_val->data.x_bignum, align_in_bytes);9618 bignum_init_unsigned(&out_val->data.x_bignum, align_in_bytes);
9589 return ira->codegen->builtin_types.entry_num_lit_int;9619 return ira->codegen->builtin_types.entry_num_lit_int;
...@@ -9592,7 +9622,7 @@ static TypeTableEntry *ir_analyze_instruction_alignof(IrAnalyze *ira, IrInstruct...@@ -9592,7 +9622,7 @@ static TypeTableEntry *ir_analyze_instruction_alignof(IrAnalyze *ira, IrInstruct
95929622
9593static TypeTableEntry *ir_analyze_instruction_overflow_op(IrAnalyze *ira, IrInstructionOverflowOp *instruction) {9623static TypeTableEntry *ir_analyze_instruction_overflow_op(IrAnalyze *ira, IrInstructionOverflowOp *instruction) {
9594 IrInstruction *type_value = instruction->type_value->other;9624 IrInstruction *type_value = instruction->type_value->other;
9595 if (type_value->type_entry->id == TypeTableEntryIdInvalid)9625 if (type_value->value.type->id == TypeTableEntryIdInvalid)
9596 return ira->codegen->builtin_types.entry_invalid;9626 return ira->codegen->builtin_types.entry_invalid;
9597 TypeTableEntry *dest_type = ir_resolve_type(ira, type_value);9627 TypeTableEntry *dest_type = ir_resolve_type(ira, type_value);
9598 TypeTableEntry *canon_type = get_underlying_type(dest_type);9628 TypeTableEntry *canon_type = get_underlying_type(dest_type);
...@@ -9607,41 +9637,41 @@ static TypeTableEntry *ir_analyze_instruction_overflow_op(IrAnalyze *ira, IrInst...@@ -9607,41 +9637,41 @@ static TypeTableEntry *ir_analyze_instruction_overflow_op(IrAnalyze *ira, IrInst
9607 }9637 }
96089638
9609 IrInstruction *op1 = instruction->op1->other;9639 IrInstruction *op1 = instruction->op1->other;
9610 if (op1->type_entry->id == TypeTableEntryIdInvalid)9640 if (op1->value.type->id == TypeTableEntryIdInvalid)
9611 return ira->codegen->builtin_types.entry_invalid;9641 return ira->codegen->builtin_types.entry_invalid;
96129642
9613 IrInstruction *casted_op1 = ir_implicit_cast(ira, op1, dest_type);9643 IrInstruction *casted_op1 = ir_implicit_cast(ira, op1, dest_type);
9614 if (casted_op1->type_entry->id == TypeTableEntryIdInvalid)9644 if (casted_op1->value.type->id == TypeTableEntryIdInvalid)
9615 return ira->codegen->builtin_types.entry_invalid;9645 return ira->codegen->builtin_types.entry_invalid;
96169646
9617 IrInstruction *op2 = instruction->op2->other;9647 IrInstruction *op2 = instruction->op2->other;
9618 if (op2->type_entry->id == TypeTableEntryIdInvalid)9648 if (op2->value.type->id == TypeTableEntryIdInvalid)
9619 return ira->codegen->builtin_types.entry_invalid;9649 return ira->codegen->builtin_types.entry_invalid;
96209650
9621 IrInstruction *casted_op2 = ir_implicit_cast(ira, op2, dest_type);9651 IrInstruction *casted_op2 = ir_implicit_cast(ira, op2, dest_type);
9622 if (casted_op2->type_entry->id == TypeTableEntryIdInvalid)9652 if (casted_op2->value.type->id == TypeTableEntryIdInvalid)
9623 return ira->codegen->builtin_types.entry_invalid;9653 return ira->codegen->builtin_types.entry_invalid;
96249654
9625 IrInstruction *result_ptr = instruction->result_ptr->other;9655 IrInstruction *result_ptr = instruction->result_ptr->other;
9626 if (result_ptr->type_entry->id == TypeTableEntryIdInvalid)9656 if (result_ptr->value.type->id == TypeTableEntryIdInvalid)
9627 return ira->codegen->builtin_types.entry_invalid;9657 return ira->codegen->builtin_types.entry_invalid;
96289658
9629 TypeTableEntry *expected_ptr_type = get_pointer_to_type(ira->codegen, dest_type, false);9659 TypeTableEntry *expected_ptr_type = get_pointer_to_type(ira->codegen, dest_type, false);
9630 IrInstruction *casted_result_ptr = ir_implicit_cast(ira, result_ptr, expected_ptr_type);9660 IrInstruction *casted_result_ptr = ir_implicit_cast(ira, result_ptr, expected_ptr_type);
9631 if (casted_result_ptr->type_entry->id == TypeTableEntryIdInvalid)9661 if (casted_result_ptr->value.type->id == TypeTableEntryIdInvalid)
9632 return ira->codegen->builtin_types.entry_invalid;9662 return ira->codegen->builtin_types.entry_invalid;
96339663
9634 if (casted_op1->static_value.special == ConstValSpecialStatic &&9664 if (casted_op1->value.special == ConstValSpecialStatic &&
9635 casted_op2->static_value.special == ConstValSpecialStatic &&9665 casted_op2->value.special == ConstValSpecialStatic &&
9636 casted_result_ptr->static_value.special == ConstValSpecialStatic)9666 casted_result_ptr->value.special == ConstValSpecialStatic)
9637 {9667 {
9638 bool depends_on_compile_var = type_value->static_value.depends_on_compile_var ||9668 bool depends_on_compile_var = type_value->value.depends_on_compile_var ||
9639 casted_op1->static_value.depends_on_compile_var || casted_op2->static_value.depends_on_compile_var ||9669 casted_op1->value.depends_on_compile_var || casted_op2->value.depends_on_compile_var ||
9640 casted_result_ptr->static_value.depends_on_compile_var;9670 casted_result_ptr->value.depends_on_compile_var;
9641 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base, depends_on_compile_var);9671 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base, depends_on_compile_var);
9642 BigNum *op1_bignum = &casted_op1->static_value.data.x_bignum;9672 BigNum *op1_bignum = &casted_op1->value.data.x_bignum;
9643 BigNum *op2_bignum = &casted_op2->static_value.data.x_bignum;9673 BigNum *op2_bignum = &casted_op2->value.data.x_bignum;
9644 ConstExprValue *pointee_val = const_ptr_pointee(&casted_result_ptr->static_value);9674 ConstExprValue *pointee_val = const_ptr_pointee(&casted_result_ptr->value);
9645 BigNum *dest_bignum = &pointee_val->data.x_bignum;9675 BigNum *dest_bignum = &pointee_val->data.x_bignum;
9646 switch (instruction->op) {9676 switch (instruction->op) {
9647 case IrOverflowOpAdd:9677 case IrOverflowOpAdd:
...@@ -9674,10 +9704,10 @@ static TypeTableEntry *ir_analyze_instruction_overflow_op(IrAnalyze *ira, IrInst...@@ -9674,10 +9704,10 @@ static TypeTableEntry *ir_analyze_instruction_overflow_op(IrAnalyze *ira, IrInst
96749704
9675static TypeTableEntry *ir_analyze_instruction_test_err(IrAnalyze *ira, IrInstructionTestErr *instruction) {9705static TypeTableEntry *ir_analyze_instruction_test_err(IrAnalyze *ira, IrInstructionTestErr *instruction) {
9676 IrInstruction *value = instruction->value->other;9706 IrInstruction *value = instruction->value->other;
9677 if (value->type_entry->id == TypeTableEntryIdInvalid)9707 if (value->value.type->id == TypeTableEntryIdInvalid)
9678 return ira->codegen->builtin_types.entry_invalid;9708 return ira->codegen->builtin_types.entry_invalid;
96799709
9680 TypeTableEntry *non_canon_type = value->type_entry;9710 TypeTableEntry *non_canon_type = value->value.type;
96819711
9682 TypeTableEntry *canon_type = get_underlying_type(non_canon_type);9712 TypeTableEntry *canon_type = get_underlying_type(non_canon_type);
9683 if (canon_type->id == TypeTableEntryIdInvalid) {9713 if (canon_type->id == TypeTableEntryIdInvalid) {
...@@ -9713,9 +9743,9 @@ static TypeTableEntry *ir_analyze_instruction_unwrap_err_code(IrAnalyze *ira,...@@ -9713,9 +9743,9 @@ static TypeTableEntry *ir_analyze_instruction_unwrap_err_code(IrAnalyze *ira,
9713 IrInstructionUnwrapErrCode *instruction)9743 IrInstructionUnwrapErrCode *instruction)
9714{9744{
9715 IrInstruction *value = instruction->value->other;9745 IrInstruction *value = instruction->value->other;
9716 if (value->type_entry->id == TypeTableEntryIdInvalid)9746 if (value->value.type->id == TypeTableEntryIdInvalid)
9717 return ira->codegen->builtin_types.entry_invalid;9747 return ira->codegen->builtin_types.entry_invalid;
9718 TypeTableEntry *ptr_type = value->type_entry;9748 TypeTableEntry *ptr_type = value->value.type;
97199749
9720 // This will be a pointer type because unwrap err payload IR instruction operates on a pointer to a thing.9750 // This will be a pointer type because unwrap err payload IR instruction operates on a pointer to a thing.
9721 assert(ptr_type->id == TypeTableEntryIdPointer);9751 assert(ptr_type->id == TypeTableEntryIdPointer);
...@@ -9756,9 +9786,9 @@ static TypeTableEntry *ir_analyze_instruction_unwrap_err_payload(IrAnalyze *ira,...@@ -9756,9 +9786,9 @@ static TypeTableEntry *ir_analyze_instruction_unwrap_err_payload(IrAnalyze *ira,
9756 IrInstructionUnwrapErrPayload *instruction)9786 IrInstructionUnwrapErrPayload *instruction)
9757{9787{
9758 IrInstruction *value = instruction->value->other;9788 IrInstruction *value = instruction->value->other;
9759 if (value->type_entry->id == TypeTableEntryIdInvalid)9789 if (value->value.type->id == TypeTableEntryIdInvalid)
9760 return ira->codegen->builtin_types.entry_invalid;9790 return ira->codegen->builtin_types.entry_invalid;
9761 TypeTableEntry *ptr_type = value->type_entry;9791 TypeTableEntry *ptr_type = value->value.type;
97629792
9763 // This will be a pointer type because unwrap err payload IR instruction operates on a pointer to a thing.9793 // This will be a pointer type because unwrap err payload IR instruction operates on a pointer to a thing.
9764 assert(ptr_type->id == TypeTableEntryIdPointer);9794 assert(ptr_type->id == TypeTableEntryIdPointer);
...@@ -9824,14 +9854,14 @@ static TypeTableEntry *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstruc...@@ -9824,14 +9854,14 @@ static TypeTableEntry *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstruc
9824 if (param_info->type->id == TypeTableEntryIdInvalid)9854 if (param_info->type->id == TypeTableEntryIdInvalid)
9825 return ira->codegen->builtin_types.entry_invalid;9855 return ira->codegen->builtin_types.entry_invalid;
98269856
9827 depends_on_compile_var = depends_on_compile_var || param_type_value->static_value.depends_on_compile_var;9857 depends_on_compile_var = depends_on_compile_var || param_type_value->value.depends_on_compile_var;
9828 }9858 }
98299859
9830 IrInstruction *return_type_value = instruction->return_type->other;9860 IrInstruction *return_type_value = instruction->return_type->other;
9831 fn_type_id.return_type = ir_resolve_type(ira, return_type_value);9861 fn_type_id.return_type = ir_resolve_type(ira, return_type_value);
9832 if (fn_type_id.return_type->id == TypeTableEntryIdInvalid)9862 if (fn_type_id.return_type->id == TypeTableEntryIdInvalid)
9833 return ira->codegen->builtin_types.entry_invalid;9863 return ira->codegen->builtin_types.entry_invalid;
9834 depends_on_compile_var = depends_on_compile_var || return_type_value->static_value.depends_on_compile_var;9864 depends_on_compile_var = depends_on_compile_var || return_type_value->value.depends_on_compile_var;
98359865
9836 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base, depends_on_compile_var);9866 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base, depends_on_compile_var);
9837 out_val->data.x_type = get_fn_type(ira->codegen, &fn_type_id);9867 out_val->data.x_type = get_fn_type(ira->codegen, &fn_type_id);
...@@ -9840,7 +9870,7 @@ static TypeTableEntry *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstruc...@@ -9840,7 +9870,7 @@ static TypeTableEntry *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstruc
98409870
9841static TypeTableEntry *ir_analyze_instruction_test_comptime(IrAnalyze *ira, IrInstructionTestComptime *instruction) {9871static TypeTableEntry *ir_analyze_instruction_test_comptime(IrAnalyze *ira, IrInstructionTestComptime *instruction) {
9842 IrInstruction *value = instruction->value->other;9872 IrInstruction *value = instruction->value->other;
9843 if (value->type_entry->id == TypeTableEntryIdInvalid)9873 if (value->value.type->id == TypeTableEntryIdInvalid)
9844 return ira->codegen->builtin_types.entry_invalid;9874 return ira->codegen->builtin_types.entry_invalid;
98459875
9846 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base, false);9876 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base, false);
...@@ -9851,6 +9881,11 @@ static TypeTableEntry *ir_analyze_instruction_test_comptime(IrAnalyze *ira, IrIn...@@ -9851,6 +9881,11 @@ static TypeTableEntry *ir_analyze_instruction_test_comptime(IrAnalyze *ira, IrIn
9851static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstruction *instruction) {9881static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstruction *instruction) {
9852 switch (instruction->id) {9882 switch (instruction->id) {
9853 case IrInstructionIdInvalid:9883 case IrInstructionIdInvalid:
9884 case IrInstructionIdPointerReinterpret:
9885 case IrInstructionIdStructInit:
9886 case IrInstructionIdStructFieldPtr:
9887 case IrInstructionIdEnumFieldPtr:
9888 case IrInstructionIdInitEnum:
9854 zig_unreachable();9889 zig_unreachable();
9855 case IrInstructionIdReturn:9890 case IrInstructionIdReturn:
9856 return ir_analyze_instruction_return(ira, (IrInstructionReturn *)instruction);9891 return ir_analyze_instruction_return(ira, (IrInstructionReturn *)instruction);
...@@ -9996,10 +10031,6 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi...@@ -9996,10 +10031,6 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi
9996 case IrInstructionIdErrWrapCode:10031 case IrInstructionIdErrWrapCode:
9997 case IrInstructionIdErrWrapPayload:10032 case IrInstructionIdErrWrapPayload:
9998 case IrInstructionIdCast:10033 case IrInstructionIdCast:
9999 case IrInstructionIdStructFieldPtr:
10000 case IrInstructionIdEnumFieldPtr:
10001 case IrInstructionIdStructInit:
10002 case IrInstructionIdInitEnum:
10003 zig_panic("TODO analyze more instructions");10034 zig_panic("TODO analyze more instructions");
10004 }10035 }
10005 zig_unreachable();10036 zig_unreachable();
...@@ -10007,9 +10038,9 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi...@@ -10007,9 +10038,9 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi
1000710038
10008static TypeTableEntry *ir_analyze_instruction(IrAnalyze *ira, IrInstruction *instruction) {10039static TypeTableEntry *ir_analyze_instruction(IrAnalyze *ira, IrInstruction *instruction) {
10009 TypeTableEntry *instruction_type = ir_analyze_instruction_nocast(ira, instruction);10040 TypeTableEntry *instruction_type = ir_analyze_instruction_nocast(ira, instruction);
10010 instruction->type_entry = instruction_type;10041 instruction->value.type = instruction_type;
10011 if (instruction->other) {10042 if (instruction->other) {
10012 instruction->other->type_entry = instruction_type;10043 instruction->other->value.type = instruction_type;
10013 } else {10044 } else {
10014 assert(instruction_type->id == TypeTableEntryIdInvalid ||10045 assert(instruction_type->id == TypeTableEntryIdInvalid ||
10015 instruction_type->id == TypeTableEntryIdUnreachable);10046 instruction_type->id == TypeTableEntryIdUnreachable);
...@@ -10156,6 +10187,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {...@@ -10156,6 +10187,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {
10156 case IrInstructionIdFnProto:10187 case IrInstructionIdFnProto:
10157 case IrInstructionIdTestComptime:10188 case IrInstructionIdTestComptime:
10158 case IrInstructionIdInitEnum:10189 case IrInstructionIdInitEnum:
10190 case IrInstructionIdPointerReinterpret:
10159 return false;10191 return false;
10160 case IrInstructionIdAsm:10192 case IrInstructionIdAsm:
10161 {10193 {
src/ir_print.cpp+35-29
...@@ -5,6 +5,7 @@...@@ -5,6 +5,7 @@
5 * See http://opensource.org/licenses/MIT5 * See http://opensource.org/licenses/MIT
6 */6 */
77
8#include "analyze.hpp"
8#include "ir.hpp"9#include "ir.hpp"
9#include "ir_print.hpp"10#include "ir_print.hpp"
1011
...@@ -24,13 +25,13 @@ static void ir_print_indent(IrPrint *irp) {...@@ -24,13 +25,13 @@ static void ir_print_indent(IrPrint *irp) {
2425
25static void ir_print_prefix(IrPrint *irp, IrInstruction *instruction) {26static void ir_print_prefix(IrPrint *irp, IrInstruction *instruction) {
26 ir_print_indent(irp);27 ir_print_indent(irp);
27 const char *type_name = instruction->type_entry ? buf_ptr(&instruction->type_entry->name) : "(unknown)";28 const char *type_name = instruction->value.type ? buf_ptr(&instruction->value.type->name) : "(unknown)";
28 const char *ref_count = ir_has_side_effects(instruction) ?29 const char *ref_count = ir_has_side_effects(instruction) ?
29 "-" : buf_ptr(buf_sprintf("%zu", instruction->ref_count));30 "-" : buf_ptr(buf_sprintf("%zu", instruction->ref_count));
30 fprintf(irp->f, "#%-3zu| %-12s| %-2s| ", instruction->debug_id, type_name, ref_count);31 fprintf(irp->f, "#%-3zu| %-12s| %-2s| ", instruction->debug_id, type_name, ref_count);
31}32}
3233
33static void ir_print_const_value(IrPrint *irp, TypeTableEntry *type_entry, ConstExprValue *const_val) {34static void ir_print_const_value(IrPrint *irp, ConstExprValue *const_val) {
34 switch (const_val->special) {35 switch (const_val->special) {
35 case ConstValSpecialRuntime:36 case ConstValSpecialRuntime:
36 zig_unreachable();37 zig_unreachable();
...@@ -43,9 +44,12 @@ static void ir_print_const_value(IrPrint *irp, TypeTableEntry *type_entry, Const...@@ -43,9 +44,12 @@ static void ir_print_const_value(IrPrint *irp, TypeTableEntry *type_entry, Const
43 case ConstValSpecialStatic:44 case ConstValSpecialStatic:
44 break;45 break;
45 }46 }
46 switch (type_entry->id) {47 assert(const_val->type);
48
49 TypeTableEntry *canon_type = get_underlying_type(const_val->type);
50 switch (canon_type->id) {
47 case TypeTableEntryIdTypeDecl:51 case TypeTableEntryIdTypeDecl:
48 return ir_print_const_value(irp, type_entry->data.type_decl.canonical_type, const_val);52 zig_unreachable();
49 case TypeTableEntryIdInvalid:53 case TypeTableEntryIdInvalid:
50 fprintf(irp->f, "(invalid)");54 fprintf(irp->f, "(invalid)");
51 return;55 return;
...@@ -94,7 +98,7 @@ static void ir_print_const_value(IrPrint *irp, TypeTableEntry *type_entry, Const...@@ -94,7 +98,7 @@ static void ir_print_const_value(IrPrint *irp, TypeTableEntry *type_entry, Const
94 }98 }
95 case TypeTableEntryIdPointer:99 case TypeTableEntryIdPointer:
96 fprintf(irp->f, "&");100 fprintf(irp->f, "&");
97 ir_print_const_value(irp, type_entry->data.pointer.child_type, const_ptr_pointee(const_val));101 ir_print_const_value(irp, const_ptr_pointee(const_val));
98 return;102 return;
99 case TypeTableEntryIdFn:103 case TypeTableEntryIdFn:
100 {104 {
...@@ -110,14 +114,13 @@ static void ir_print_const_value(IrPrint *irp, TypeTableEntry *type_entry, Const...@@ -110,14 +114,13 @@ static void ir_print_const_value(IrPrint *irp, TypeTableEntry *type_entry, Const
110 }114 }
111 case TypeTableEntryIdArray:115 case TypeTableEntryIdArray:
112 {116 {
113 uint64_t len = type_entry->data.array.len;117 uint64_t len = canon_type->data.array.len;
114 fprintf(irp->f, "%s{", buf_ptr(&type_entry->name));118 fprintf(irp->f, "%s{", buf_ptr(&canon_type->name));
115 for (uint64_t i = 0; i < len; i += 1) {119 for (uint64_t i = 0; i < len; i += 1) {
116 if (i != 0)120 if (i != 0)
117 fprintf(irp->f, ",");121 fprintf(irp->f, ",");
118 ConstExprValue *child_value = &const_val->data.x_array.elements[i];122 ConstExprValue *child_value = &const_val->data.x_array.elements[i];
119 TypeTableEntry *child_type = type_entry->data.array.child_type;123 ir_print_const_value(irp, child_value);
120 ir_print_const_value(irp, child_type, child_value);
121 }124 }
122 fprintf(irp->f, "}");125 fprintf(irp->f, "}");
123 return;126 return;
...@@ -135,7 +138,7 @@ static void ir_print_const_value(IrPrint *irp, TypeTableEntry *type_entry, Const...@@ -135,7 +138,7 @@ static void ir_print_const_value(IrPrint *irp, TypeTableEntry *type_entry, Const
135 case TypeTableEntryIdMaybe:138 case TypeTableEntryIdMaybe:
136 {139 {
137 if (const_val->data.x_maybe) {140 if (const_val->data.x_maybe) {
138 ir_print_const_value(irp, type_entry->data.maybe.child_type, const_val->data.x_maybe);141 ir_print_const_value(irp, const_val->data.x_maybe);
139 } else {142 } else {
140 fprintf(irp->f, "null");143 fprintf(irp->f, "null");
141 }144 }
...@@ -160,22 +163,22 @@ static void ir_print_const_value(IrPrint *irp, TypeTableEntry *type_entry, Const...@@ -160,22 +163,22 @@ static void ir_print_const_value(IrPrint *irp, TypeTableEntry *type_entry, Const
160 }163 }
161 case TypeTableEntryIdStruct:164 case TypeTableEntryIdStruct:
162 {165 {
163 fprintf(irp->f, "(struct %s constant)", buf_ptr(&type_entry->name));166 fprintf(irp->f, "(struct %s constant)", buf_ptr(&canon_type->name));
164 return;167 return;
165 }168 }
166 case TypeTableEntryIdEnum:169 case TypeTableEntryIdEnum:
167 {170 {
168 fprintf(irp->f, "(enum %s constant)", buf_ptr(&type_entry->name));171 fprintf(irp->f, "(enum %s constant)", buf_ptr(&canon_type->name));
169 return;172 return;
170 }173 }
171 case TypeTableEntryIdErrorUnion:174 case TypeTableEntryIdErrorUnion:
172 {175 {
173 fprintf(irp->f, "(error union %s constant)", buf_ptr(&type_entry->name));176 fprintf(irp->f, "(error union %s constant)", buf_ptr(&canon_type->name));
174 return;177 return;
175 }178 }
176 case TypeTableEntryIdUnion:179 case TypeTableEntryIdUnion:
177 {180 {
178 fprintf(irp->f, "(union %s constant)", buf_ptr(&type_entry->name));181 fprintf(irp->f, "(union %s constant)", buf_ptr(&canon_type->name));
179 return;182 return;
180 }183 }
181 case TypeTableEntryIdPureError:184 case TypeTableEntryIdPureError:
...@@ -185,7 +188,7 @@ static void ir_print_const_value(IrPrint *irp, TypeTableEntry *type_entry, Const...@@ -185,7 +188,7 @@ static void ir_print_const_value(IrPrint *irp, TypeTableEntry *type_entry, Const
185 }188 }
186 case TypeTableEntryIdEnumTag:189 case TypeTableEntryIdEnumTag:
187 {190 {
188 TypeTableEntry *enum_type = type_entry->data.enum_tag.enum_type;191 TypeTableEntry *enum_type = canon_type->data.enum_tag.enum_type;
189 TypeEnumField *field = &enum_type->data.enumeration.fields[const_val->data.x_bignum.data.x_uint];192 TypeEnumField *field = &enum_type->data.enumeration.fields[const_val->data.x_bignum.data.x_uint];
190 fprintf(irp->f, "%s.%s", buf_ptr(&enum_type->name), buf_ptr(field->name));193 fprintf(irp->f, "%s.%s", buf_ptr(&enum_type->name), buf_ptr(field->name));
191 return;194 return;
...@@ -194,19 +197,13 @@ static void ir_print_const_value(IrPrint *irp, TypeTableEntry *type_entry, Const...@@ -194,19 +197,13 @@ static void ir_print_const_value(IrPrint *irp, TypeTableEntry *type_entry, Const
194 zig_unreachable();197 zig_unreachable();
195}198}
196199
197static void ir_print_const_instruction(IrPrint *irp, IrInstruction *instruction) {
198 TypeTableEntry *type_entry = instruction->type_entry;
199 ConstExprValue *const_val = &instruction->static_value;
200 ir_print_const_value(irp, type_entry, const_val);
201}
202
203static void ir_print_var_instruction(IrPrint *irp, IrInstruction *instruction) {200static void ir_print_var_instruction(IrPrint *irp, IrInstruction *instruction) {
204 fprintf(irp->f, "#%zu", instruction->debug_id);201 fprintf(irp->f, "#%zu", instruction->debug_id);
205}202}
206203
207static void ir_print_other_instruction(IrPrint *irp, IrInstruction *instruction) {204static void ir_print_other_instruction(IrPrint *irp, IrInstruction *instruction) {
208 if (instruction->static_value.special != ConstValSpecialRuntime) {205 if (instruction->value.special != ConstValSpecialRuntime) {
209 ir_print_const_instruction(irp, instruction);206 ir_print_const_value(irp, &instruction->value);
210 } else {207 } else {
211 ir_print_var_instruction(irp, instruction);208 ir_print_var_instruction(irp, instruction);
212 }209 }
...@@ -223,7 +220,7 @@ static void ir_print_return(IrPrint *irp, IrInstructionReturn *return_instructio...@@ -223,7 +220,7 @@ static void ir_print_return(IrPrint *irp, IrInstructionReturn *return_instructio
223}220}
224221
225static void ir_print_const(IrPrint *irp, IrInstructionConst *const_instruction) {222static void ir_print_const(IrPrint *irp, IrInstructionConst *const_instruction) {
226 ir_print_const_instruction(irp, &const_instruction->base);223 ir_print_const_value(irp, &const_instruction->base.value);
227}224}
228225
229static const char *ir_bin_op_id_str(IrBinOp op_id) {226static const char *ir_bin_op_id_str(IrBinOp op_id) {
...@@ -925,6 +922,12 @@ static void ir_print_init_enum(IrPrint *irp, IrInstructionInitEnum *instruction)...@@ -925,6 +922,12 @@ static void ir_print_init_enum(IrPrint *irp, IrInstructionInitEnum *instruction)
925 fprintf(irp->f, "}");922 fprintf(irp->f, "}");
926}923}
927924
925static void ir_print_pointer_reinterpret(IrPrint *irp, IrInstructionPointerReinterpret *instruction) {
926 fprintf(irp->f, "(%s)(", buf_ptr(&instruction->base.value.type->name));
927 ir_print_other_instruction(irp, instruction->ptr);
928 fprintf(irp->f, ")");
929}
930
928static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {931static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
929 ir_print_prefix(irp, instruction);932 ir_print_prefix(irp, instruction);
930 switch (instruction->id) {933 switch (instruction->id) {
...@@ -1164,6 +1167,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {...@@ -1164,6 +1167,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
1164 case IrInstructionIdInitEnum:1167 case IrInstructionIdInitEnum:
1165 ir_print_init_enum(irp, (IrInstructionInitEnum *)instruction);1168 ir_print_init_enum(irp, (IrInstructionInitEnum *)instruction);
1166 break;1169 break;
1170 case IrInstructionIdPointerReinterpret:
1171 ir_print_pointer_reinterpret(irp, (IrInstructionPointerReinterpret *)instruction);
1172 break;
1167 }1173 }
1168 fprintf(irp->f, "\n");1174 fprintf(irp->f, "\n");
1169}1175}
...@@ -1188,14 +1194,14 @@ void ir_print(FILE *f, IrExecutable *executable, int indent_size) {...@@ -1188,14 +1194,14 @@ void ir_print(FILE *f, IrExecutable *executable, int indent_size) {
1188static void print_tld_var(IrPrint *irp, TldVar *tld_var) {1194static void print_tld_var(IrPrint *irp, TldVar *tld_var) {
1189 const char *const_or_var = tld_var->var->src_is_const ? "const" : "var";1195 const char *const_or_var = tld_var->var->src_is_const ? "const" : "var";
1190 fprintf(irp->f, "%s %s", const_or_var, buf_ptr(tld_var->base.name));1196 fprintf(irp->f, "%s %s", const_or_var, buf_ptr(tld_var->base.name));
1191 bool omit_type = (tld_var->var->type->id == TypeTableEntryIdNumLitFloat ||1197 bool omit_type = (tld_var->var->value.type->id == TypeTableEntryIdNumLitFloat ||
1192 tld_var->var->type->id == TypeTableEntryIdNumLitInt);1198 tld_var->var->value.type->id == TypeTableEntryIdNumLitInt);
1193 if (!omit_type) {1199 if (!omit_type) {
1194 fprintf(irp->f, ": %s", buf_ptr(&tld_var->var->type->name));1200 fprintf(irp->f, ": %s", buf_ptr(&tld_var->var->value.type->name));
1195 }1201 }
1196 if (tld_var->var->value) {1202 if (tld_var->var->value.special != ConstValSpecialRuntime) {
1197 fprintf(irp->f, " = ");1203 fprintf(irp->f, " = ");
1198 ir_print_const_value(irp, tld_var->var->type, tld_var->var->value);1204 ir_print_const_value(irp, &tld_var->var->value);
1199 }1205 }
1200 fprintf(irp->f, ";\n");1206 fprintf(irp->f, ";\n");
1201}1207}
src/parseh.cpp+23-23
...@@ -124,51 +124,52 @@ static void parseh_init_tld(Context *c, Tld *tld, TldId id, Buf *name) {...@@ -124,51 +124,52 @@ static void parseh_init_tld(Context *c, Tld *tld, TldId id, Buf *name) {
124 tld->resolution = TldResolutionOk;124 tld->resolution = TldResolutionOk;
125}125}
126126
127static TldVar *create_global_var(Context *c, Buf *name, TypeTableEntry *var_type, ConstExprValue *var_value, bool is_const) {127static TldVar *create_global_var(Context *c, Buf *name, ConstExprValue *var_value, bool is_const) {
128 TldVar *tld_var = allocate<TldVar>(1);128 TldVar *tld_var = allocate<TldVar>(1);
129 parseh_init_tld(c, &tld_var->base, TldIdVar, name);129 parseh_init_tld(c, &tld_var->base, TldIdVar, name);
130 tld_var->var = add_variable(c->codegen, c->source_node, &c->import->decls_scope->base, name, var_type, is_const, var_value);130 tld_var->var = add_variable(c->codegen, c->source_node, &c->import->decls_scope->base, name, is_const, var_value);
131 return tld_var;131 return tld_var;
132}132}
133133
134static Tld *create_global_char_lit_var(Context *c, Buf *name, uint8_t value) {134static Tld *create_global_char_lit_var(Context *c, Buf *name, uint8_t value) {
135 TldVar *tld_var = create_global_var(c, name, c->codegen->builtin_types.entry_u8,135 ConstExprValue *var_val = create_const_unsigned_negative(c->codegen->builtin_types.entry_u8, value, false);
136 create_const_unsigned_negative(value, false), true);136 TldVar *tld_var = create_global_var(c, name, var_val, true);
137 return &tld_var->base;137 return &tld_var->base;
138}138}
139139
140static Tld *create_global_str_lit_var(Context *c, Buf *name, Buf *value) {140static Tld *create_global_str_lit_var(Context *c, Buf *name, Buf *value) {
141 TypeTableEntry *str_type = get_array_type(c->codegen, c->codegen->builtin_types.entry_u8, buf_len(value));141 TldVar *tld_var = create_global_var(c, name, create_const_str_lit(c->codegen, value), true);
142 TldVar *tld_var = create_global_var(c, name, str_type, create_const_str_lit(value), true);
143 return &tld_var->base;142 return &tld_var->base;
144}143}
145144
146static Tld *create_global_num_lit_unsigned_negative(Context *c, Buf *name, uint64_t x, bool negative) {145static Tld *create_global_num_lit_unsigned_negative(Context *c, Buf *name, uint64_t x, bool negative) {
147 TldVar *tld_var = create_global_var(c, name, c->codegen->builtin_types.entry_num_lit_int,146 ConstExprValue *var_val = create_const_unsigned_negative(c->codegen->builtin_types.entry_num_lit_int, x, negative);
148 create_const_unsigned_negative(x, negative), true);147 TldVar *tld_var = create_global_var(c, name, var_val, true);
149 return &tld_var->base;148 return &tld_var->base;
150}149}
151150
152static Tld *create_global_num_lit_float(Context *c, Buf *name, double value) {151static Tld *create_global_num_lit_float(Context *c, Buf *name, double value) {
153 TldVar *tld_var = create_global_var(c, name, c->codegen->builtin_types.entry_num_lit_float,152 ConstExprValue *var_val = create_const_float(c->codegen->builtin_types.entry_num_lit_float, value);
154 create_const_float(value), true);153 TldVar *tld_var = create_global_var(c, name, var_val, true);
155 return &tld_var->base;154 return &tld_var->base;
156}155}
157156
158static ConstExprValue *create_const_num_lit_ap(Context *c, const Decl *source_decl, const llvm::APSInt &aps_int) {157static ConstExprValue *create_const_int_ap(Context *c, TypeTableEntry *type, const Decl *source_decl,
158 const llvm::APSInt &aps_int)
159{
159 if (aps_int.isSigned()) {160 if (aps_int.isSigned()) {
160 if (aps_int > INT64_MAX || aps_int < INT64_MIN) {161 if (aps_int > INT64_MAX || aps_int < INT64_MIN) {
161 emit_warning(c, source_decl, "integer overflow\n");162 emit_warning(c, source_decl, "integer overflow\n");
162 return nullptr;163 return nullptr;
163 } else {164 } else {
164 return create_const_signed(aps_int.getExtValue());165 return create_const_signed(type, aps_int.getExtValue());
165 }166 }
166 } else {167 } else {
167 if (aps_int > INT64_MAX) {168 if (aps_int > INT64_MAX) {
168 emit_warning(c, source_decl, "integer overflow\n");169 emit_warning(c, source_decl, "integer overflow\n");
169 return nullptr;170 return nullptr;
170 } else {171 } else {
171 return create_const_unsigned_negative(aps_int.getExtValue(), false);172 return create_const_unsigned_negative(type, aps_int.getExtValue(), false);
172 }173 }
173 }174 }
174}175}
...@@ -176,19 +177,18 @@ static ConstExprValue *create_const_num_lit_ap(Context *c, const Decl *source_de...@@ -176,19 +177,18 @@ static ConstExprValue *create_const_num_lit_ap(Context *c, const Decl *source_de
176static Tld *create_global_num_lit_ap(Context *c, const Decl *source_decl, Buf *name,177static Tld *create_global_num_lit_ap(Context *c, const Decl *source_decl, Buf *name,
177 const llvm::APSInt &aps_int)178 const llvm::APSInt &aps_int)
178{179{
179 ConstExprValue *const_value = create_const_num_lit_ap(c, source_decl, aps_int);180 ConstExprValue *const_value = create_const_int_ap(c, c->codegen->builtin_types.entry_num_lit_int,
181 source_decl, aps_int);
180 if (!const_value)182 if (!const_value)
181 return nullptr;183 return nullptr;
182 TldVar *tld_var = create_global_var(c, name, c->codegen->builtin_types.entry_num_lit_int, const_value, true);184 TldVar *tld_var = create_global_var(c, name, const_value, true);
183 return &tld_var->base;185 return &tld_var->base;
184}186}
185187
186188
187static Tld *add_const_type(Context *c, Buf *name, TypeTableEntry *type_entry) {189static Tld *add_const_type(Context *c, Buf *name, TypeTableEntry *type_entry) {
188 ConstExprValue *var_value = allocate<ConstExprValue>(1);190 ConstExprValue *var_value = create_const_type(c->codegen, type_entry);
189 var_value->special = ConstValSpecialStatic;191 TldVar *tld_var = create_global_var(c, name, var_value, true);
190 var_value->data.x_type = type_entry;
191 TldVar *tld_var = create_global_var(c, name, c->codegen->builtin_types.entry_type, var_value, true);
192 add_global(c, &tld_var->base);192 add_global(c, &tld_var->base);
193193
194 c->global_type_table.put(name, type_entry);194 c->global_type_table.put(name, type_entry);
...@@ -1025,7 +1025,7 @@ static void visit_var_decl(Context *c, const VarDecl *var_decl) {...@@ -1025,7 +1025,7 @@ static void visit_var_decl(Context *c, const VarDecl *var_decl) {
1025 "ignoring variable '%s' - int initializer for non int type\n", buf_ptr(name));1025 "ignoring variable '%s' - int initializer for non int type\n", buf_ptr(name));
1026 return;1026 return;
1027 }1027 }
1028 init_value = create_const_num_lit_ap(c, var_decl, ap_value->getInt());1028 init_value = create_const_int_ap(c, var_type, var_decl, ap_value->getInt());
1029 if (!init_value)1029 if (!init_value)
1030 return;1030 return;
10311031
...@@ -1047,13 +1047,13 @@ static void visit_var_decl(Context *c, const VarDecl *var_decl) {...@@ -1047,13 +1047,13 @@ static void visit_var_decl(Context *c, const VarDecl *var_decl) {
1047 return;1047 return;
1048 }1048 }
10491049
1050 TldVar *tld_var = create_global_var(c, name, var_type, init_value, true);1050 TldVar *tld_var = create_global_var(c, name, init_value, true);
1051 add_global(c, &tld_var->base);1051 add_global(c, &tld_var->base);
1052 return;1052 return;
1053 }1053 }
10541054
1055 if (is_extern) {1055 if (is_extern) {
1056 TldVar *tld_var = create_global_var(c, name, var_type, nullptr, is_const);1056 TldVar *tld_var = create_global_var(c, name, create_const_runtime(var_type), is_const);
1057 tld_var->var->is_extern = true;1057 tld_var->var->is_extern = true;
1058 add_global(c, &tld_var->base);1058 add_global(c, &tld_var->base);
1059 return;1059 return;
...@@ -1199,7 +1199,7 @@ static void process_symbol_macros(Context *c) {...@@ -1199,7 +1199,7 @@ static void process_symbol_macros(Context *c) {
1199 // variable is non-null and calls it.1199 // variable is non-null and calls it.
1200 if (existing_tld->id == TldIdVar) {1200 if (existing_tld->id == TldIdVar) {
1201 TldVar *tld_var = (TldVar *)existing_tld;1201 TldVar *tld_var = (TldVar *)existing_tld;
1202 TypeTableEntry *var_type = tld_var->var->type;1202 TypeTableEntry *var_type = tld_var->var->value.type;
1203 if (var_type->id == TypeTableEntryIdMaybe && !tld_var->var->src_is_const) {1203 if (var_type->id == TypeTableEntryIdMaybe && !tld_var->var->src_is_const) {
1204 TypeTableEntry *child_type = var_type->data.maybe.child_type;1204 TypeTableEntry *child_type = var_type->data.maybe.child_type;
1205 if (child_type->id == TypeTableEntryIdFn) {1205 if (child_type->id == TypeTableEntryIdFn) {
test/cases3/misc.zig+10-1
...@@ -261,6 +261,16 @@ fn typeEquality() {...@@ -261,6 +261,16 @@ fn typeEquality() {
261 assert(&const u8 != &u8);261 assert(&const u8 != &u8);
262}262}
263263
264
265const global_a: i32 = 1234;
266const global_b: &const i32 = &global_a;
267const global_c: &const f32 = (&const f32)(global_b);
268fn compileTimeGlobalReinterpret() {
269 @setFnTest(this);
270 const d = (&const i32)(global_c);
271 assert(*d == 1234);
272}
273
264// TODO import from std.str274// TODO import from std.str
265pub fn memeql(a: []const u8, b: []const u8) -> bool {275pub fn memeql(a: []const u8, b: []const u8) -> bool {
266 sliceEql(u8, a, b)276 sliceEql(u8, a, b)
...@@ -288,7 +298,6 @@ pub fn cstrcmp(a: &const u8, b: &const u8) -> i8 {...@@ -288,7 +298,6 @@ pub fn cstrcmp(a: &const u8, b: &const u8) -> i8 {
288 };298 };
289}299}
290300
291
292// TODO const assert = @import("std").debug.assert;301// TODO const assert = @import("std").debug.assert;
293fn assert(ok: bool) {302fn assert(ok: bool) {
294 if (!ok)303 if (!ok)
test/self_hosted.zig+1-16
...@@ -9,27 +9,12 @@ const test_enum_with_members = @import("cases/enum_with_members.zig");...@@ -9,27 +9,12 @@ const test_enum_with_members = @import("cases/enum_with_members.zig");
99
1010
11fn explicitCastMaybePointers() {11fn explicitCastMaybePointers() {
12 @setFnTest(this, true);12 @setFnTest(this);
1313
14 const a: ?&i32 = undefined;14 const a: ?&i32 = undefined;
15 const b: ?&f32 = (?&f32)(a);15 const b: ?&f32 = (?&f32)(a);
16}16}
1717
18
19fn multilineCString() {
20 @setFnTest(this);
21
22 const s1 =
23 c\\one
24 c\\two)
25 c\\three
26 ;
27 const s2 = c"one\ntwo)\nthree";
28 assert(cstr.cmp(s1, s2) == 0);
29}
30
31
32
33fn genericMallocFree() {18fn genericMallocFree() {
34 @setFnTest(this, true);19 @setFnTest(this, true);
3520