authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-19 22:29:39-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-19 22:29:39-04:00
log78eeb6e9aea9c280513faaa83f9df959b7ac6f59
tree5ffb6fa07d22ff74ae5be52553d15bf843c5dc53
parent04c25efe112e374facaf1bc8b58bbdb6999a39e3
signaturelock-open Commit is signed but in an unrecognized format.

fix double getelementptr of runtime global


3 files changed, 76 insertions(+), 125 deletions(-)

src/codegen.cpp+3
...@@ -3832,6 +3832,9 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr...@@ -3832,6 +3832,9 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr
3832static LLVMValueRef ir_render_struct_field_ptr(CodeGen *g, IrExecutable *executable,3832static LLVMValueRef ir_render_struct_field_ptr(CodeGen *g, IrExecutable *executable,
3833 IrInstructionStructFieldPtr *instruction)3833 IrInstructionStructFieldPtr *instruction)
3834{3834{
3835 if (instruction->base.value.special != ConstValSpecialRuntime)
3836 return nullptr;
3837
3835 LLVMValueRef struct_ptr = ir_llvm_value(g, instruction->struct_ptr);3838 LLVMValueRef struct_ptr = ir_llvm_value(g, instruction->struct_ptr);
3836 // not necessarily a pointer. could be ZigTypeIdStruct3839 // not necessarily a pointer. could be ZigTypeIdStruct
3837 ZigType *struct_ptr_type = instruction->struct_ptr->value.type;3840 ZigType *struct_ptr_type = instruction->struct_ptr->value.type;
src/ir.cpp+67-119
...@@ -187,9 +187,9 @@ static IrInstruction *ir_analyze_int_to_ptr(IrAnalyze *ira, IrInstruction *sourc...@@ -187,9 +187,9 @@ static IrInstruction *ir_analyze_int_to_ptr(IrAnalyze *ira, IrInstruction *sourc
187static IrInstruction *ir_analyze_bit_cast(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *value,187static IrInstruction *ir_analyze_bit_cast(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *value,
188 ZigType *dest_type);188 ZigType *dest_type);
189static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspend_source_instr,189static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspend_source_instr,
190 ResultLoc *result_loc, ZigType *value_type, IrInstruction *value);190 ResultLoc *result_loc, ZigType *value_type, IrInstruction *value, bool non_null_comptime);
191static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_source_instr,191static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_source_instr,
192 ResultLoc *result_loc, ZigType *value_type, IrInstruction *value, bool force_runtime);192 ResultLoc *result_loc, ZigType *value_type, IrInstruction *value, bool force_runtime, bool non_null_comptime);
193static IrInstruction *ir_analyze_unwrap_optional_payload(IrAnalyze *ira, IrInstruction *source_instr,193static IrInstruction *ir_analyze_unwrap_optional_payload(IrAnalyze *ira, IrInstruction *source_instr,
194 IrInstruction *base_ptr, bool safety_check_on, bool initializing);194 IrInstruction *base_ptr, bool safety_check_on, bool initializing);
195static IrInstruction *ir_analyze_unwrap_error_payload(IrAnalyze *ira, IrInstruction *source_instr,195static IrInstruction *ir_analyze_unwrap_error_payload(IrAnalyze *ira, IrInstruction *source_instr,
...@@ -10844,7 +10844,7 @@ static IrInstruction *ir_resolve_ptr_of_array_to_slice(IrAnalyze *ira, IrInstruc...@@ -10844,7 +10844,7 @@ static IrInstruction *ir_resolve_ptr_of_array_to_slice(IrAnalyze *ira, IrInstruc
10844 }10844 }
1084510845
10846 if (result_loc == nullptr) result_loc = no_result_loc();10846 if (result_loc == nullptr) result_loc = no_result_loc();
10847 IrInstruction *result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr, true);10847 IrInstruction *result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr, true, false);
10848 if (type_is_invalid(result_loc_inst->value.type) || instr_is_unreachable(result_loc_inst)) {10848 if (type_is_invalid(result_loc_inst->value.type) || instr_is_unreachable(result_loc_inst)) {
10849 return result_loc_inst;10849 return result_loc_inst;
10850 }10850 }
...@@ -11282,7 +11282,7 @@ static IrInstruction *ir_analyze_optional_wrap(IrAnalyze *ira, IrInstruction *so...@@ -11282,7 +11282,7 @@ static IrInstruction *ir_analyze_optional_wrap(IrAnalyze *ira, IrInstruction *so
11282 }11282 }
11283 IrInstruction *result_loc_inst = nullptr;11283 IrInstruction *result_loc_inst = nullptr;
11284 if (result_loc != nullptr) {11284 if (result_loc != nullptr) {
11285 result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr, true);11285 result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr, true, false);
11286 if (type_is_invalid(result_loc_inst->value.type) || instr_is_unreachable(result_loc_inst)) {11286 if (type_is_invalid(result_loc_inst->value.type) || instr_is_unreachable(result_loc_inst)) {
11287 return result_loc_inst;11287 return result_loc_inst;
11288 }11288 }
...@@ -11325,7 +11325,7 @@ static IrInstruction *ir_analyze_err_wrap_payload(IrAnalyze *ira, IrInstruction...@@ -11325,7 +11325,7 @@ static IrInstruction *ir_analyze_err_wrap_payload(IrAnalyze *ira, IrInstruction
11325 IrInstruction *result_loc_inst;11325 IrInstruction *result_loc_inst;
11326 if (handle_is_ptr(wanted_type)) {11326 if (handle_is_ptr(wanted_type)) {
11327 if (result_loc == nullptr) result_loc = no_result_loc();11327 if (result_loc == nullptr) result_loc = no_result_loc();
11328 result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr, true);11328 result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr, true, false);
11329 if (type_is_invalid(result_loc_inst->value.type) || instr_is_unreachable(result_loc_inst)) {11329 if (type_is_invalid(result_loc_inst->value.type) || instr_is_unreachable(result_loc_inst)) {
11330 return result_loc_inst;11330 return result_loc_inst;
11331 }11331 }
...@@ -11410,7 +11410,7 @@ static IrInstruction *ir_analyze_err_wrap_code(IrAnalyze *ira, IrInstruction *so...@@ -11410,7 +11410,7 @@ static IrInstruction *ir_analyze_err_wrap_code(IrAnalyze *ira, IrInstruction *so
11410 IrInstruction *result_loc_inst;11410 IrInstruction *result_loc_inst;
11411 if (handle_is_ptr(wanted_type)) {11411 if (handle_is_ptr(wanted_type)) {
11412 if (result_loc == nullptr) result_loc = no_result_loc();11412 if (result_loc == nullptr) result_loc = no_result_loc();
11413 result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr, true);11413 result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr, true, false);
11414 if (type_is_invalid(result_loc_inst->value.type) || instr_is_unreachable(result_loc_inst)) {11414 if (type_is_invalid(result_loc_inst->value.type) || instr_is_unreachable(result_loc_inst)) {
11415 return result_loc_inst;11415 return result_loc_inst;
11416 }11416 }
...@@ -11483,7 +11483,7 @@ static IrInstruction *ir_get_ref(IrAnalyze *ira, IrInstruction *source_instructi...@@ -11483,7 +11483,7 @@ static IrInstruction *ir_get_ref(IrAnalyze *ira, IrInstruction *source_instructi
1148311483
11484 IrInstruction *result_loc;11484 IrInstruction *result_loc;
11485 if (type_has_bits(ptr_type) && !handle_is_ptr(value->value.type)) {11485 if (type_has_bits(ptr_type) && !handle_is_ptr(value->value.type)) {
11486 result_loc = ir_resolve_result(ira, source_instruction, no_result_loc(), value->value.type, nullptr, true);11486 result_loc = ir_resolve_result(ira, source_instruction, no_result_loc(), value->value.type, nullptr, true, false);
11487 } else {11487 } else {
11488 result_loc = nullptr;11488 result_loc = nullptr;
11489 }11489 }
...@@ -11527,7 +11527,7 @@ static IrInstruction *ir_analyze_array_to_slice(IrAnalyze *ira, IrInstruction *s...@@ -11527,7 +11527,7 @@ static IrInstruction *ir_analyze_array_to_slice(IrAnalyze *ira, IrInstruction *s
11527 if (!array_ptr) array_ptr = ir_get_ref(ira, source_instr, array, true, false);11527 if (!array_ptr) array_ptr = ir_get_ref(ira, source_instr, array, true, false);
1152811528
11529 if (result_loc == nullptr) result_loc = no_result_loc();11529 if (result_loc == nullptr) result_loc = no_result_loc();
11530 IrInstruction *result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr, true);11530 IrInstruction *result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr, true, false);
11531 if (type_is_invalid(result_loc_inst->value.type) || instr_is_unreachable(result_loc_inst)) {11531 if (type_is_invalid(result_loc_inst->value.type) || instr_is_unreachable(result_loc_inst)) {
11532 return result_loc_inst;11532 return result_loc_inst;
11533 }11533 }
...@@ -12180,7 +12180,7 @@ static IrInstruction *ir_analyze_vector_to_array(IrAnalyze *ira, IrInstruction *...@@ -12180,7 +12180,7 @@ static IrInstruction *ir_analyze_vector_to_array(IrAnalyze *ira, IrInstruction *
12180 result->value.type = array_type;12180 result->value.type = array_type;
12181 return result;12181 return result;
12182 }12182 }
12183 IrInstruction *result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, array_type, nullptr, true);12183 IrInstruction *result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, array_type, nullptr, true, false);
12184 if (type_is_invalid(result_loc_inst->value.type) || instr_is_unreachable(result_loc_inst)) {12184 if (type_is_invalid(result_loc_inst->value.type) || instr_is_unreachable(result_loc_inst)) {
12185 return result_loc_inst;12185 return result_loc_inst;
12186 }12186 }
...@@ -12761,7 +12761,7 @@ static IrInstruction *ir_get_deref(IrAnalyze *ira, IrInstruction *source_instruc...@@ -12761,7 +12761,7 @@ static IrInstruction *ir_get_deref(IrAnalyze *ira, IrInstruction *source_instruc
12761 IrInstruction *result_loc_inst;12761 IrInstruction *result_loc_inst;
12762 if (type_entry->data.pointer.host_int_bytes != 0 && handle_is_ptr(child_type)) {12762 if (type_entry->data.pointer.host_int_bytes != 0 && handle_is_ptr(child_type)) {
12763 if (result_loc == nullptr) result_loc = no_result_loc();12763 if (result_loc == nullptr) result_loc = no_result_loc();
12764 result_loc_inst = ir_resolve_result(ira, source_instruction, result_loc, child_type, nullptr, true);12764 result_loc_inst = ir_resolve_result(ira, source_instruction, result_loc, child_type, nullptr, true, false);
12765 if (type_is_invalid(result_loc_inst->value.type) || instr_is_unreachable(result_loc_inst)) {12765 if (type_is_invalid(result_loc_inst->value.type) || instr_is_unreachable(result_loc_inst)) {
12766 return result_loc_inst;12766 return result_loc_inst;
12767 }12767 }
...@@ -14910,7 +14910,7 @@ static void set_up_result_loc_for_inferred_comptime(IrInstruction *ptr) {...@@ -14910,7 +14910,7 @@ static void set_up_result_loc_for_inferred_comptime(IrInstruction *ptr) {
1491014910
14911// when calling this function, at the callsite must check for result type noreturn and propagate it up14911// when calling this function, at the callsite must check for result type noreturn and propagate it up
14912static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspend_source_instr,14912static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspend_source_instr,
14913 ResultLoc *result_loc, ZigType *value_type, IrInstruction *value)14913 ResultLoc *result_loc, ZigType *value_type, IrInstruction *value, bool non_null_comptime)
14914{14914{
14915 Error err;14915 Error err;
14916 if (result_loc->resolved_loc != nullptr) {14916 if (result_loc->resolved_loc != nullptr) {
...@@ -14984,9 +14984,11 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe...@@ -14984,9 +14984,11 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
14984 return result_loc->resolved_loc;14984 return result_loc->resolved_loc;
14985 }14985 }
14986 case ResultLocIdReturn: {14986 case ResultLocIdReturn: {
14987 bool is_comptime = value != nullptr && value->value.special != ConstValSpecialRuntime;14987 if (!non_null_comptime) {
14988 if (is_comptime)14988 bool is_comptime = value != nullptr && value->value.special != ConstValSpecialRuntime;
14989 return nullptr;14989 if (is_comptime)
14990 return nullptr;
14991 }
14990 if (!type_has_bits(ira->explicit_return_type) || !handle_is_ptr(ira->explicit_return_type))14992 if (!type_has_bits(ira->explicit_return_type) || !handle_is_ptr(ira->explicit_return_type))
14991 return nullptr;14993 return nullptr;
1499214994
...@@ -15005,6 +15007,10 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe...@@ -15005,6 +15007,10 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
15005 return ira->codegen->invalid_instruction;15007 return ira->codegen->invalid_instruction;
15006 peer_parent->skipped = is_comptime;15008 peer_parent->skipped = is_comptime;
15007 if (peer_parent->skipped) {15009 if (peer_parent->skipped) {
15010 if (non_null_comptime) {
15011 return ir_resolve_result(ira, suspend_source_instr, peer_parent->parent,
15012 value_type, value, false, non_null_comptime);
15013 }
15008 return nullptr;15014 return nullptr;
15009 }15015 }
1501015016
...@@ -15016,7 +15022,7 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe...@@ -15016,7 +15022,7 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
15016 }15022 }
1501715023
15018 IrInstruction *parent_result_loc = ir_resolve_result(ira, suspend_source_instr, peer_parent->parent,15024 IrInstruction *parent_result_loc = ir_resolve_result(ira, suspend_source_instr, peer_parent->parent,
15019 peer_parent->resolved_type, nullptr, false);15025 peer_parent->resolved_type, nullptr, false, non_null_comptime);
15020 if (parent_result_loc == nullptr || type_is_invalid(parent_result_loc->value.type) ||15026 if (parent_result_loc == nullptr || type_is_invalid(parent_result_loc->value.type) ||
15021 parent_result_loc->value.type->id == ZigTypeIdUnreachable)15027 parent_result_loc->value.type->id == ZigTypeIdUnreachable)
15022 {15028 {
...@@ -15066,7 +15072,7 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe...@@ -15066,7 +15072,7 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
15066 }15072 }
1506715073
15068 IrInstruction *parent_result_loc = ir_resolve_result(ira, suspend_source_instr, result_bit_cast->parent,15074 IrInstruction *parent_result_loc = ir_resolve_result(ira, suspend_source_instr, result_bit_cast->parent,
15069 dest_type, bitcasted_value, false);15075 dest_type, bitcasted_value, false, non_null_comptime);
15070 if (parent_result_loc == nullptr || type_is_invalid(parent_result_loc->value.type) ||15076 if (parent_result_loc == nullptr || type_is_invalid(parent_result_loc->value.type) ||
15071 parent_result_loc->value.type->id == ZigTypeIdUnreachable)15077 parent_result_loc->value.type->id == ZigTypeIdUnreachable)
15072 {15078 {
...@@ -15094,10 +15100,11 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe...@@ -15094,10 +15100,11 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
15094}15100}
1509515101
15096static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_source_instr,15102static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_source_instr,
15097 ResultLoc *result_loc_pass1, ZigType *value_type, IrInstruction *value, bool force_runtime)15103 ResultLoc *result_loc_pass1, ZigType *value_type, IrInstruction *value, bool force_runtime,
15104 bool non_null_comptime)
15098{15105{
15099 IrInstruction *result_loc = ir_resolve_result_raw(ira, suspend_source_instr, result_loc_pass1, value_type,15106 IrInstruction *result_loc = ir_resolve_result_raw(ira, suspend_source_instr, result_loc_pass1, value_type,
15100 value);15107 value, non_null_comptime);
15101 if (result_loc == nullptr || (instr_is_unreachable(result_loc) || type_is_invalid(result_loc->value.type)))15108 if (result_loc == nullptr || (instr_is_unreachable(result_loc) || type_is_invalid(result_loc->value.type)))
15102 return result_loc;15109 return result_loc;
1510315110
...@@ -15144,23 +15151,16 @@ static IrInstruction *ir_analyze_instruction_resolve_result(IrAnalyze *ira, IrIn...@@ -15144,23 +15151,16 @@ static IrInstruction *ir_analyze_instruction_resolve_result(IrAnalyze *ira, IrIn
15144 ZigType *implicit_elem_type = ir_resolve_type(ira, instruction->ty->child);15151 ZigType *implicit_elem_type = ir_resolve_type(ira, instruction->ty->child);
15145 if (type_is_invalid(implicit_elem_type))15152 if (type_is_invalid(implicit_elem_type))
15146 return ira->codegen->invalid_instruction;15153 return ira->codegen->invalid_instruction;
15147 ResultLoc *old_result_loc = instruction->result_loc;15154 IrInstruction *result_loc = ir_resolve_result(ira, &instruction->base, instruction->result_loc,
15148 for (;;) {15155 implicit_elem_type, nullptr, false, true);
15149 IrInstruction *result_loc = ir_resolve_result(ira, &instruction->base, old_result_loc,15156 if (result_loc != nullptr)
15150 implicit_elem_type, nullptr, false);15157 return result_loc;
15151 if (result_loc != nullptr)
15152 return result_loc;
1515315158
15154 if (instruction->result_loc->id == ResultLocIdPeer) {15159 IrInstruction *result = ir_const(ira, &instruction->base, implicit_elem_type);
15155 old_result_loc = reinterpret_cast<ResultLocPeer *>(instruction->result_loc)->parent->parent;15160 result->value.special = ConstValSpecialUndef;
15156 continue;15161 IrInstruction *ptr = ir_get_ref(ira, &instruction->base, result, false, false);
15157 }15162 ptr->value.data.x_ptr.mut = ConstPtrMutComptimeVar;
15158 IrInstruction *result = ir_const(ira, &instruction->base, implicit_elem_type);15163 return ptr;
15159 result->value.special = ConstValSpecialUndef;
15160 IrInstruction *ptr = ir_get_ref(ira, &instruction->base, result, false, false);
15161 ptr->value.data.x_ptr.mut = ConstPtrMutComptimeVar;
15162 return ptr;
15163 }
15164}15164}
1516515165
15166static void ir_reset_result(ResultLoc *result_loc) {15166static void ir_reset_result(ResultLoc *result_loc) {
...@@ -15500,7 +15500,7 @@ static IrInstruction *ir_analyze_store_ptr(IrAnalyze *ira, IrInstruction *source...@@ -15500,7 +15500,7 @@ static IrInstruction *ir_analyze_store_ptr(IrAnalyze *ira, IrInstruction *source
15500 // * "string literal used as comptime slice is memoized"15500 // * "string literal used as comptime slice is memoized"
15501 // * "comptime modification of const struct field" - except modified to avoid15501 // * "comptime modification of const struct field" - except modified to avoid
15502 // ConstPtrMutComptimeVar, thus defeating the logic below.15502 // ConstPtrMutComptimeVar, thus defeating the logic below.
15503 bool same_global_refs = ptr->value.data.x_ptr.mut == ConstPtrMutComptimeConst;15503 bool same_global_refs = ptr->value.data.x_ptr.mut != ConstPtrMutComptimeVar;
15504 copy_const_val(dest_val, &value->value, same_global_refs);15504 copy_const_val(dest_val, &value->value, same_global_refs);
15505 if (!ira->new_irb.current_basic_block->must_be_comptime_source_instr) {15505 if (!ira->new_irb.current_basic_block->must_be_comptime_source_instr) {
15506 ira->new_irb.current_basic_block->must_be_comptime_source_instr = source_instr;15506 ira->new_irb.current_basic_block->must_be_comptime_source_instr = source_instr;
...@@ -15986,7 +15986,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c...@@ -15986,7 +15986,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c
15986 IrInstruction *result_loc;15986 IrInstruction *result_loc;
15987 if (handle_is_ptr(impl_fn_type_id->return_type)) {15987 if (handle_is_ptr(impl_fn_type_id->return_type)) {
15988 result_loc = ir_resolve_result(ira, &call_instruction->base, call_instruction->result_loc,15988 result_loc = ir_resolve_result(ira, &call_instruction->base, call_instruction->result_loc,
15989 impl_fn_type_id->return_type, nullptr, true);15989 impl_fn_type_id->return_type, nullptr, true, false);
15990 if (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc)) {15990 if (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc)) {
15991 return result_loc;15991 return result_loc;
15992 }15992 }
...@@ -16106,7 +16106,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c...@@ -16106,7 +16106,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c
16106 IrInstruction *result_loc;16106 IrInstruction *result_loc;
16107 if (handle_is_ptr(return_type)) {16107 if (handle_is_ptr(return_type)) {
16108 result_loc = ir_resolve_result(ira, &call_instruction->base, call_instruction->result_loc,16108 result_loc = ir_resolve_result(ira, &call_instruction->base, call_instruction->result_loc,
16109 return_type, nullptr, true);16109 return_type, nullptr, true, false);
16110 if (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc)) {16110 if (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc)) {
16111 return result_loc;16111 return result_loc;
16112 }16112 }
...@@ -16198,7 +16198,7 @@ static Error ir_read_const_ptr(IrAnalyze *ira, CodeGen *codegen, AstNode *source...@@ -16198,7 +16198,7 @@ static Error ir_read_const_ptr(IrAnalyze *ira, CodeGen *codegen, AstNode *source
1619816198
16199 if (dst_size <= src_size) {16199 if (dst_size <= src_size) {
16200 if (src_size == dst_size && types_have_same_zig_comptime_repr(pointee->type, out_val->type)) {16200 if (src_size == dst_size && types_have_same_zig_comptime_repr(pointee->type, out_val->type)) {
16201 copy_const_val(out_val, pointee, ptr_val->data.x_ptr.mut == ConstPtrMutComptimeConst);16201 copy_const_val(out_val, pointee, ptr_val->data.x_ptr.mut != ConstPtrMutComptimeVar);
16202 return ErrorNone;16202 return ErrorNone;
16203 }16203 }
16204 Buf buf = BUF_INIT;16204 Buf buf = BUF_INIT;
...@@ -16600,7 +16600,7 @@ static IrInstruction *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionPh...@@ -16600,7 +16600,7 @@ static IrInstruction *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionPh
1660016600
16601 // In case resolving the parent activates a suspend, do it now16601 // In case resolving the parent activates a suspend, do it now
16602 IrInstruction *parent_result_loc = ir_resolve_result(ira, &phi_instruction->base, peer_parent->parent,16602 IrInstruction *parent_result_loc = ir_resolve_result(ira, &phi_instruction->base, peer_parent->parent,
16603 peer_parent->resolved_type, nullptr, false);16603 peer_parent->resolved_type, nullptr, false, false);
16604 if (parent_result_loc != nullptr &&16604 if (parent_result_loc != nullptr &&
16605 (type_is_invalid(parent_result_loc->value.type) || instr_is_unreachable(parent_result_loc)))16605 (type_is_invalid(parent_result_loc->value.type) || instr_is_unreachable(parent_result_loc)))
16606 {16606 {
...@@ -17246,11 +17246,10 @@ static IrInstruction *ir_analyze_struct_field_ptr(IrAnalyze *ira, IrInstruction...@@ -17246,11 +17246,10 @@ static IrInstruction *ir_analyze_struct_field_ptr(IrAnalyze *ira, IrInstruction
17246 field_val->special = ConstValSpecialUndef;17246 field_val->special = ConstValSpecialUndef;
17247 field_val->type = struct_type->data.structure.fields[i].type_entry;17247 field_val->type = struct_type->data.structure.fields[i].type_entry;
17248 ConstParent *parent = get_const_val_parent(ira->codegen, field_val);17248 ConstParent *parent = get_const_val_parent(ira->codegen, field_val);
17249 if (parent != nullptr) {17249 assert(parent != nullptr);
17250 parent->id = ConstParentIdStruct;17250 parent->id = ConstParentIdStruct;
17251 parent->data.p_struct.struct_val = struct_val;17251 parent->data.p_struct.struct_val = struct_val;
17252 parent->data.p_struct.field_index = i;17252 parent->data.p_struct.field_index = i;
17253 }
17254 }17253 }
17255 }17254 }
17256 IrInstruction *result;17255 IrInstruction *result;
...@@ -17264,7 +17263,7 @@ static IrInstruction *ir_analyze_struct_field_ptr(IrAnalyze *ira, IrInstruction...@@ -17264,7 +17263,7 @@ static IrInstruction *ir_analyze_struct_field_ptr(IrAnalyze *ira, IrInstruction
17264 }17263 }
17265 ConstExprValue *const_val = &result->value;17264 ConstExprValue *const_val = &result->value;
17266 const_val->data.x_ptr.special = ConstPtrSpecialBaseStruct;17265 const_val->data.x_ptr.special = ConstPtrSpecialBaseStruct;
17267 const_val->data.x_ptr.mut = struct_ptr->value.data.x_ptr.mut;17266 const_val->data.x_ptr.mut = ptr_val->data.x_ptr.mut;
17268 const_val->data.x_ptr.data.base_struct.struct_val = struct_val;17267 const_val->data.x_ptr.data.base_struct.struct_val = struct_val;
17269 const_val->data.x_ptr.data.base_struct.field_index = field->src_index;17268 const_val->data.x_ptr.data.base_struct.field_index = field->src_index;
17270 return result;17269 return result;
...@@ -17414,6 +17413,11 @@ static void add_link_lib_symbol(IrAnalyze *ira, Buf *lib_name, Buf *symbol_name,...@@ -17414,6 +17413,11 @@ static void add_link_lib_symbol(IrAnalyze *ira, Buf *lib_name, Buf *symbol_name,
17414 link_lib->symbols.append(symbol_name);17413 link_lib->symbols.append(symbol_name);
17415}17414}
1741617415
17416static IrInstruction *ir_error_dependency_loop(IrAnalyze *ira, IrInstruction *source_instr) {
17417 ErrorMsg *msg = ir_add_error(ira, source_instr, buf_sprintf("dependency loop detected"));
17418 emit_error_notes_for_ref_stack(ira->codegen, msg);
17419 return ira->codegen->invalid_instruction;
17420}
1741717421
17418static IrInstruction *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source_instruction, Tld *tld) {17422static IrInstruction *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source_instruction, Tld *tld) {
17419 resolve_top_level_decl(ira->codegen, tld, source_instruction->source_node);17423 resolve_top_level_decl(ira->codegen, tld, source_instruction->source_node);
...@@ -17428,6 +17432,9 @@ static IrInstruction *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source_...@@ -17428,6 +17432,9 @@ static IrInstruction *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source_
17428 {17432 {
17429 TldVar *tld_var = (TldVar *)tld;17433 TldVar *tld_var = (TldVar *)tld;
17430 ZigVar *var = tld_var->var;17434 ZigVar *var = tld_var->var;
17435 if (var == nullptr) {
17436 return ir_error_dependency_loop(ira, source_instruction);
17437 }
17431 if (tld_var->extern_lib_name != nullptr) {17438 if (tld_var->extern_lib_name != nullptr) {
17432 add_link_lib_symbol(ira, tld_var->extern_lib_name, &var->name, source_instruction->source_node);17439 add_link_lib_symbol(ira, tld_var->extern_lib_name, &var->name, source_instruction->source_node);
17433 }17440 }
...@@ -17443,23 +17450,13 @@ static IrInstruction *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source_...@@ -17443,23 +17450,13 @@ static IrInstruction *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source_
17443 if (type_is_invalid(fn_entry->type_entry))17450 if (type_is_invalid(fn_entry->type_entry))
17444 return ira->codegen->invalid_instruction;17451 return ira->codegen->invalid_instruction;
1744517452
17446 // TODO instead of allocating this every time, put it in the tld value and we can reference
17447 // the same one every time
17448 ConstExprValue *const_val = create_const_vals(1);
17449 const_val->special = ConstValSpecialStatic;
17450 const_val->type = fn_entry->type_entry;
17451 const_val->data.x_ptr.data.fn.fn_entry = fn_entry;
17452 const_val->data.x_ptr.special = ConstPtrSpecialFunction;
17453 const_val->data.x_ptr.mut = ConstPtrMutComptimeConst;
17454
17455 if (tld_fn->extern_lib_name != nullptr) {17453 if (tld_fn->extern_lib_name != nullptr) {
17456 add_link_lib_symbol(ira, tld_fn->extern_lib_name, &fn_entry->symbol_name, source_instruction->source_node);17454 add_link_lib_symbol(ira, tld_fn->extern_lib_name, &fn_entry->symbol_name, source_instruction->source_node);
17457 }17455 }
1745817456
17459 bool ptr_is_const = true;17457 IrInstruction *fn_inst = ir_create_const_fn(&ira->new_irb, source_instruction->scope,
17460 bool ptr_is_volatile = false;17458 source_instruction->source_node, fn_entry);
17461 return ir_get_const_ptr(ira, source_instruction, const_val, fn_entry->type_entry,17459 return ir_get_ref(ira, source_instruction, fn_inst, true, false);
17462 ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile, 0);
17463 }17460 }
17464 }17461 }
17465 zig_unreachable();17462 zig_unreachable();
...@@ -19673,12 +19670,6 @@ static IrInstruction *ir_analyze_instruction_bit_offset_of(IrAnalyze *ira,...@@ -19673,12 +19670,6 @@ static IrInstruction *ir_analyze_instruction_bit_offset_of(IrAnalyze *ira,
19673 return ir_const_unsigned(ira, &instruction->base, bit_offset);19670 return ir_const_unsigned(ira, &instruction->base, bit_offset);
19674}19671}
1967519672
19676static IrInstruction *ir_error_dependency_loop(IrAnalyze *ira, IrInstruction *source_instr) {
19677 ErrorMsg *msg = ir_add_error(ira, source_instr, buf_sprintf("dependency loop detected"));
19678 emit_error_notes_for_ref_stack(ira->codegen, msg);
19679 return ira->codegen->invalid_instruction;
19680}
19681
19682static void ensure_field_index(ZigType *type, const char *field_name, size_t index) {19673static void ensure_field_index(ZigType *type, const char *field_name, size_t index) {
19683 Buf *field_name_buf;19674 Buf *field_name_buf;
1968419675
...@@ -21088,7 +21079,7 @@ static IrInstruction *ir_analyze_instruction_cmpxchg(IrAnalyze *ira, IrInstructi...@@ -21088,7 +21079,7 @@ static IrInstruction *ir_analyze_instruction_cmpxchg(IrAnalyze *ira, IrInstructi
21088 IrInstruction *result_loc;21079 IrInstruction *result_loc;
21089 if (handle_is_ptr(result_type)) {21080 if (handle_is_ptr(result_type)) {
21090 result_loc = ir_resolve_result(ira, &instruction->base, instruction->result_loc,21081 result_loc = ir_resolve_result(ira, &instruction->base, instruction->result_loc,
21091 result_type, nullptr, true);21082 result_type, nullptr, true, false);
21092 if (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc)) {21083 if (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc)) {
21093 return result_loc;21084 return result_loc;
21094 }21085 }
...@@ -21345,7 +21336,7 @@ static IrInstruction *ir_analyze_instruction_from_bytes(IrAnalyze *ira, IrInstru...@@ -21345,7 +21336,7 @@ static IrInstruction *ir_analyze_instruction_from_bytes(IrAnalyze *ira, IrInstru
21345 }21336 }
2134621337
21347 IrInstruction *result_loc = ir_resolve_result(ira, &instruction->base, instruction->result_loc,21338 IrInstruction *result_loc = ir_resolve_result(ira, &instruction->base, instruction->result_loc,
21348 dest_slice_type, nullptr, true);21339 dest_slice_type, nullptr, true, false);
21349 if (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc)) {21340 if (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc)) {
21350 return result_loc;21341 return result_loc;
21351 }21342 }
...@@ -21422,7 +21413,7 @@ static IrInstruction *ir_analyze_instruction_to_bytes(IrAnalyze *ira, IrInstruct...@@ -21422,7 +21413,7 @@ static IrInstruction *ir_analyze_instruction_to_bytes(IrAnalyze *ira, IrInstruct
21422 }21413 }
2142321414
21424 IrInstruction *result_loc = ir_resolve_result(ira, &instruction->base, instruction->result_loc,21415 IrInstruction *result_loc = ir_resolve_result(ira, &instruction->base, instruction->result_loc,
21425 dest_slice_type, nullptr, true);21416 dest_slice_type, nullptr, true, false);
21426 if (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc)) {21417 if (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc)) {
21427 return result_loc;21418 return result_loc;
21428 }21419 }
...@@ -22164,7 +22155,7 @@ static IrInstruction *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstruction...@@ -22164,7 +22155,7 @@ static IrInstruction *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstruction
22164 }22155 }
2216522156
22166 IrInstruction *result_loc = ir_resolve_result(ira, &instruction->base, instruction->result_loc,22157 IrInstruction *result_loc = ir_resolve_result(ira, &instruction->base, instruction->result_loc,
22167 return_type, nullptr, true);22158 return_type, nullptr, true, false);
22168 if (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc)) {22159 if (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc)) {
22169 return result_loc;22160 return result_loc;
22170 }22161 }
...@@ -23729,58 +23720,15 @@ static IrInstruction *ir_analyze_instruction_int_to_ptr(IrAnalyze *ira, IrInstru...@@ -23729,58 +23720,15 @@ static IrInstruction *ir_analyze_instruction_int_to_ptr(IrAnalyze *ira, IrInstru
23729static IrInstruction *ir_analyze_instruction_decl_ref(IrAnalyze *ira,23720static IrInstruction *ir_analyze_instruction_decl_ref(IrAnalyze *ira,
23730 IrInstructionDeclRef *instruction)23721 IrInstructionDeclRef *instruction)
23731{23722{
23732 Tld *tld = instruction->tld;23723 IrInstruction *ref_instruction = ir_analyze_decl_ref(ira, &instruction->base, instruction->tld);
23733 LVal lval = instruction->lval;23724 if (type_is_invalid(ref_instruction->value.type))
23734
23735 resolve_top_level_decl(ira->codegen, tld, instruction->base.source_node);
23736 if (tld->resolution == TldResolutionInvalid)
23737 return ira->codegen->invalid_instruction;23725 return ira->codegen->invalid_instruction;
2373823726
23739 switch (tld->id) {23727 if (instruction->lval == LValPtr) {
23740 case TldIdContainer:23728 return ref_instruction;
23741 case TldIdCompTime:23729 } else {
23742 zig_unreachable();23730 return ir_get_deref(ira, &instruction->base, ref_instruction, nullptr);
23743 case TldIdVar: {
23744 TldVar *tld_var = (TldVar *)tld;
23745 ZigVar *var = tld_var->var;
23746
23747 if (var == nullptr) {
23748 return ir_error_dependency_loop(ira, &instruction->base);
23749 }
23750
23751 IrInstruction *var_ptr = ir_get_var_ptr(ira, &instruction->base, var);
23752 if (type_is_invalid(var_ptr->value.type))
23753 return ira->codegen->invalid_instruction;
23754
23755 if (tld_var->extern_lib_name != nullptr) {
23756 add_link_lib_symbol(ira, tld_var->extern_lib_name, &var->name, instruction->base.source_node);
23757 }
23758
23759 if (lval == LValPtr) {
23760 return var_ptr;
23761 } else {
23762 return ir_get_deref(ira, &instruction->base, var_ptr, nullptr);
23763 }
23764 }
23765 case TldIdFn: {
23766 TldFn *tld_fn = (TldFn *)tld;
23767 ZigFn *fn_entry = tld_fn->fn_entry;
23768 ir_assert(fn_entry->type_entry, &instruction->base);
23769
23770 if (tld_fn->extern_lib_name != nullptr) {
23771 add_link_lib_symbol(ira, tld_fn->extern_lib_name, &fn_entry->symbol_name, instruction->base.source_node);
23772 }
23773
23774 IrInstruction *ref_instruction = ir_create_const_fn(&ira->new_irb, instruction->base.scope,
23775 instruction->base.source_node, fn_entry);
23776 if (lval == LValPtr) {
23777 return ir_get_ref(ira, &instruction->base, ref_instruction, true, false);
23778 } else {
23779 return ref_instruction;
23780 }
23781 }
23782 }23731 }
23783 zig_unreachable();
23784}23732}
2378523733
23786static IrInstruction *ir_analyze_instruction_ptr_to_int(IrAnalyze *ira, IrInstructionPtrToInt *instruction) {23734static IrInstruction *ir_analyze_instruction_ptr_to_int(IrAnalyze *ira, IrInstructionPtrToInt *instruction) {
...@@ -24655,7 +24603,7 @@ static IrInstruction *ir_analyze_instruction_end_expr(IrAnalyze *ira, IrInstruct...@@ -24655,7 +24603,7 @@ static IrInstruction *ir_analyze_instruction_end_expr(IrAnalyze *ira, IrInstruct
2465524603
24656 bool was_written = instruction->result_loc->written;24604 bool was_written = instruction->result_loc->written;
24657 IrInstruction *result_loc = ir_resolve_result(ira, &instruction->base, instruction->result_loc,24605 IrInstruction *result_loc = ir_resolve_result(ira, &instruction->base, instruction->result_loc,
24658 value->value.type, value, false);24606 value->value.type, value, false, false);
24659 if (result_loc != nullptr) {24607 if (result_loc != nullptr) {
24660 if (type_is_invalid(result_loc->value.type))24608 if (type_is_invalid(result_loc->value.type))
24661 return ira->codegen->invalid_instruction;24609 return ira->codegen->invalid_instruction;
...@@ -24684,7 +24632,7 @@ static IrInstruction *ir_analyze_instruction_bit_cast_src(IrAnalyze *ira, IrInst...@@ -24684,7 +24632,7 @@ static IrInstruction *ir_analyze_instruction_bit_cast_src(IrAnalyze *ira, IrInst
24684 return operand;24632 return operand;
2468524633
24686 IrInstruction *result_loc = ir_resolve_result(ira, &instruction->base,24634 IrInstruction *result_loc = ir_resolve_result(ira, &instruction->base,
24687 &instruction->result_loc_bit_cast->base, operand->value.type, operand, false);24635 &instruction->result_loc_bit_cast->base, operand->value.type, operand, false, false);
24688 if (result_loc != nullptr && (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc)))24636 if (result_loc != nullptr && (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc)))
24689 return result_loc;24637 return result_loc;
2469024638
test/stage1/behavior/struct.zig+6-6
...@@ -522,12 +522,12 @@ const S0 = struct {...@@ -522,12 +522,12 @@ const S0 = struct {
522 }522 }
523};523};
524524
525//var g_foo: S0 = S0.init();525var g_foo: S0 = S0.init();
526//526
527//test "access to global struct fields" {527test "access to global struct fields" {
528// g_foo.bar.value = 42;528 g_foo.bar.value = 42;
529// expect(g_foo.bar.value == 42);529 expect(g_foo.bar.value == 42);
530//}530}
531531
532//test "packed struct with fp fields" {532//test "packed struct with fp fields" {
533// const S = packed struct {533// const S = packed struct {