authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-04-27 23:40:43-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-04-27 23:40:43-04:00
log458afb0ef9634b6a0480bcb6e07a92cd9a330d84
tree60788ee3dd56b66c81da0b1ef77ca09f46b5045a
parent2e512a0e6e81532b62ddb7fdafdcd28926690eaa

phi instruction retains stack ptr hint


2 files changed, 25 insertions(+), 2 deletions(-)

src/ir.cpp+14-1
......@@ -9226,6 +9226,8 @@ static TypeTableEntry *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionP
92269226 return ira->codegen->builtin_types.entry_invalid;
92279227 }
92289228
9229 bool all_stack_ptrs = (resolved_type->id == TypeTableEntryIdPointer);
9230
92299231 // cast all values to the resolved type. however we can't put cast instructions in front of the phi instruction.
92309232 // so we go back and insert the casts as the last instruction in the corresponding predecessor blocks, and
92319233 // then make sure the branch instruction is preserved.
......@@ -9238,12 +9240,23 @@ static TypeTableEntry *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionP
92389240 IrInstruction *casted_value = ir_implicit_cast(ira, new_value, resolved_type);
92399241 new_incoming_values.items[i] = casted_value;
92409242 predecessor->instruction_list.append(branch_instruction);
9243
9244 if (all_stack_ptrs && (casted_value->value.special != ConstValSpecialRuntime ||
9245 casted_value->value.data.rh_ptr != RuntimeHintPtrStack))
9246 {
9247 all_stack_ptrs = false;
9248 }
92419249 }
92429250 ir_set_cursor_at_end(&ira->new_irb, cur_bb);
92439251
9244 ir_build_phi_from(&ira->new_irb, &phi_instruction->base, new_incoming_blocks.length,
9252 IrInstruction *result = ir_build_phi_from(&ira->new_irb, &phi_instruction->base, new_incoming_blocks.length,
92459253 new_incoming_blocks.items, new_incoming_values.items);
92469254
9255 if (all_stack_ptrs) {
9256 assert(result->value.special == ConstValSpecialRuntime);
9257 result->value.data.rh_ptr = RuntimeHintPtrStack;
9258 }
9259
92479260 return resolved_type;
92489261}
92499262
test/compile_errors.zig+11-1
......@@ -1611,11 +1611,21 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
16111611 ".tmp_source.zig:3:5: error: cannot set section of external function 'foo'",
16121612 ".tmp_source.zig:1:8: note: declared here");
16131613
1614 cases.add("returning address of local variable",
1614 cases.add("returning address of local variable - simple",
16151615 \\export fn foo() -> &i32 {
16161616 \\ var a: i32 = undefined;
16171617 \\ return &a;
16181618 \\}
16191619 ,
16201620 ".tmp_source.zig:3:13: error: function returns address of local variable");
1621
1622 cases.add("returning address of local variable - phi",
1623 \\export fn foo(c: bool) -> &i32 {
1624 \\ var a: i32 = undefined;
1625 \\ var b: i32 = undefined;
1626 \\ return if (c) &a else &b;
1627 \\}
1628 ,
1629 ".tmp_source.zig:4:12: error: function returns address of local variable");
1630
16211631}