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...@@ -9226,6 +9226,8 @@ static TypeTableEntry *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionP
9226 return ira->codegen->builtin_types.entry_invalid;9226 return ira->codegen->builtin_types.entry_invalid;
9227 }9227 }
92289228
9229 bool all_stack_ptrs = (resolved_type->id == TypeTableEntryIdPointer);
9230
9229 // cast all values to the resolved type. however we can't put cast instructions in front of the phi instruction.9231 // cast all values to the resolved type. however we can't put cast instructions in front of the phi instruction.
9230 // so we go back and insert the casts as the last instruction in the corresponding predecessor blocks, and9232 // so we go back and insert the casts as the last instruction in the corresponding predecessor blocks, and
9231 // then make sure the branch instruction is preserved.9233 // then make sure the branch instruction is preserved.
...@@ -9238,12 +9240,23 @@ static TypeTableEntry *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionP...@@ -9238,12 +9240,23 @@ static TypeTableEntry *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionP
9238 IrInstruction *casted_value = ir_implicit_cast(ira, new_value, resolved_type);9240 IrInstruction *casted_value = ir_implicit_cast(ira, new_value, resolved_type);
9239 new_incoming_values.items[i] = casted_value;9241 new_incoming_values.items[i] = casted_value;
9240 predecessor->instruction_list.append(branch_instruction);9242 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 }
9241 }9249 }
9242 ir_set_cursor_at_end(&ira->new_irb, cur_bb);9250 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,
9245 new_incoming_blocks.items, new_incoming_values.items);9253 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
9247 return resolved_type;9260 return resolved_type;
9248}9261}
92499262
test/compile_errors.zig+11-1
...@@ -1611,11 +1611,21 @@ pub fn addCases(cases: &tests.CompileErrorContext) {...@@ -1611,11 +1611,21 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
1611 ".tmp_source.zig:3:5: error: cannot set section of external function 'foo'",1611 ".tmp_source.zig:3:5: error: cannot set section of external function 'foo'",
1612 ".tmp_source.zig:1:8: note: declared here");1612 ".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",
1615 \\export fn foo() -> &i32 {1615 \\export fn foo() -> &i32 {
1616 \\ var a: i32 = undefined;1616 \\ var a: i32 = undefined;
1617 \\ return &a;1617 \\ return &a;
1618 \\}1618 \\}
1619 ,1619 ,
1620 ".tmp_source.zig:3:13: error: function returns address of local variable");1620 ".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
1621}1631}