diff options
author | John Hodge <tpg@mutabah.net> | 2016-08-24 11:31:57 +0800 |
---|---|---|
committer | John Hodge <tpg@mutabah.net> | 2016-08-24 11:31:57 +0800 |
commit | 280af9e9346bfd78a0c6f2fdd817f808a74363f2 (patch) | |
tree | 0d02073e540731e977fdd17df0a6d8ea4a9e0400 /src/mir | |
parent | 62a2bde2e87b4ac0789848b5b9ec54e91de15238 (diff) | |
download | mrust-280af9e9346bfd78a0c6f2fdd817f808a74363f2.tar.gz |
MIR Gen - Move variable states to function state (instead of scope)
Diffstat (limited to 'src/mir')
-rw-r--r-- | src/mir/from_hir.cpp | 7 | ||||
-rw-r--r-- | src/mir/from_hir.hpp | 6 | ||||
-rw-r--r-- | src/mir/mir_builder.cpp | 38 |
3 files changed, 23 insertions, 28 deletions
diff --git a/src/mir/from_hir.cpp b/src/mir/from_hir.cpp index 481c1294..33104822 100644 --- a/src/mir/from_hir.cpp +++ b/src/mir/from_hir.cpp @@ -235,8 +235,7 @@ namespace { if( node.m_nodes.size() > 0 ) { bool res_valid; - //::MIR::RValue res; - auto res = m_builder.new_temporary( node.m_res_type ); + ::MIR::RValue res; auto scope = m_builder.new_scope_var(node.span()); @@ -267,8 +266,8 @@ namespace { auto stmt_scope = m_builder.new_scope_temp(sp); this->visit_node_ptr(subnode); if( m_builder.has_result() || m_builder.block_active() ) { - //res = m_builder.get_result(sp); - m_builder.push_stmt_assign( sp, res.clone(), m_builder.get_result(sp) ); + // PROBLEM: + res = m_builder.get_result(sp); m_builder.terminate_scope(sp, mv$(stmt_scope)); res_valid = true; } diff --git a/src/mir/from_hir.hpp b/src/mir/from_hir.hpp index 93100b44..6153ac9f 100644 --- a/src/mir/from_hir.hpp +++ b/src/mir/from_hir.hpp @@ -57,11 +57,9 @@ struct SplitArm { TAGGED_UNION(ScopeType, Variables, (Variables, struct { ::std::vector<unsigned int> vars; // List of owned variables - ::std::vector<VarState> var_states; // Indexed by position in above list }), (Temporaries, struct { ::std::vector<unsigned int> temporaries; // Controlled temporaries - ::std::vector<VarState> states; // Indexed by position in above list }), (Split, struct { ::std::vector<SplitArm> arms; @@ -85,8 +83,8 @@ class MirBuilder ::MIR::RValue m_result; bool m_result_valid; - ::std::vector<bool> variables_valid; - ::std::vector<bool> temporaries_valid; + ::std::vector<VarState> m_variable_states; + ::std::vector<VarState> m_temporary_states; struct ScopeDef { diff --git a/src/mir/mir_builder.cpp b/src/mir/mir_builder.cpp index b5155097..b847b31d 100644 --- a/src/mir/mir_builder.cpp +++ b/src/mir/mir_builder.cpp @@ -25,6 +25,8 @@ MirBuilder::MirBuilder(const Span& sp, const StaticTraitResolve& resolve, ::MIR: m_scopes.push_back( ScopeDef { sp, ScopeType::make_Temporaries({}) } ); m_scope_stack.push_back( 1 ); + + m_variable_states.resize( output.named_variables.size(), VarState::Uninit ); } MirBuilder::~MirBuilder() { @@ -55,7 +57,6 @@ void MirBuilder::define_variable(unsigned int idx) auto it = ::std::find(e.vars.begin(), e.vars.end(), idx); assert(it == e.vars.end()); e.vars.push_back( idx ); - e.var_states.push_back( VarState::Uninit ); return ; ), (Split, @@ -71,7 +72,8 @@ void MirBuilder::define_variable(unsigned int idx) DEBUG("DEFINE tmp" << rv << ": " << ty); m_output.temporaries.push_back( ty.clone() ); - temporaries_valid.push_back(false); + m_temporary_states.push_back( VarState::Uninit ); + assert(m_output.temporaries.size() == m_temporary_states.size()); ScopeDef* top_scope = nullptr; for(unsigned int i = m_scope_stack.size(); i --; ) @@ -85,7 +87,6 @@ void MirBuilder::define_variable(unsigned int idx) assert( top_scope ); auto& tmp_scope = top_scope->data.as_Temporaries(); tmp_scope.temporaries.push_back( rv ); - tmp_scope.states.push_back( VarState::Uninit ); return ::MIR::LValue::make_Temporary({rv}); } ::MIR::LValue MirBuilder::lvalue_or_temp(const Span& sp, const ::HIR::TypeRef& ty, ::MIR::RValue val) @@ -736,9 +737,8 @@ VarState MirBuilder::get_variable_state(const Span& sp, unsigned int idx) const (Variables, auto it = ::std::find(e.vars.begin(), e.vars.end(), idx); if( it != e.vars.end() ) { - unsigned int sub_idx = it - e.vars.begin(); - assert(sub_idx < e.var_states.size()); - return e.var_states[sub_idx]; + // If controlled by this block, exit early (won't find it elsewhere) + break ; } ), (Split, @@ -752,7 +752,8 @@ VarState MirBuilder::get_variable_state(const Span& sp, unsigned int idx) const ) } - BUG(sp, "Variable " << idx << " not found in stack"); + ASSERT_BUG(sp, idx < m_variable_states.size(), "Variable " << idx << " out of range for state table"); + return m_variable_states[idx]; } void MirBuilder::set_variable_state(const Span& sp, unsigned int idx, VarState state) { @@ -765,10 +766,7 @@ void MirBuilder::set_variable_state(const Span& sp, unsigned int idx, VarState s (Variables, auto it = ::std::find(e.vars.begin(), e.vars.end(), idx); if( it != e.vars.end() ) { - unsigned int sub_idx = it - e.vars.begin(); - ASSERT_BUG(sp, sub_idx < e.var_states.size(), "Variable list size invalid - " << sub_idx << " >= " << e.var_states.size()); - e.var_states[sub_idx] = state; - return ; + break ; } ), (Split, @@ -785,7 +783,8 @@ void MirBuilder::set_variable_state(const Span& sp, unsigned int idx, VarState s ) } - BUG(sp, "Variable " << idx << " not found in stack"); + ASSERT_BUG(sp, idx < m_variable_states.size(), "Variable " << idx << " out of range for state table"); + m_variable_states[idx] = state; } VarState MirBuilder::get_temp_state(const Span& sp, unsigned int idx) const { @@ -798,9 +797,7 @@ VarState MirBuilder::get_temp_state(const Span& sp, unsigned int idx) const (Temporaries, auto it = ::std::find(e.temporaries.begin(), e.temporaries.end(), idx); if( it != e.temporaries.end() ) { - unsigned int sub_idx = it - e.temporaries.begin(); - ASSERT_BUG(sp, sub_idx < e.states.size(), "Temporary list sizes invalid - " << sub_idx << " >= " << e.states.size()); - return e.states[sub_idx]; + break ; } ), (Split, @@ -809,7 +806,8 @@ VarState MirBuilder::get_temp_state(const Span& sp, unsigned int idx) const ) } - BUG(sp, "Temporary " << idx << " not found in stack"); + ASSERT_BUG(sp, idx < m_temporary_states.size(), "Temporary " << idx << " out of range for state table"); + return m_temporary_states[idx]; } void MirBuilder::set_temp_state(const Span& sp, unsigned int idx, VarState state) { @@ -822,10 +820,7 @@ void MirBuilder::set_temp_state(const Span& sp, unsigned int idx, VarState state (Temporaries, auto it = ::std::find(e.temporaries.begin(), e.temporaries.end(), idx); if( it != e.temporaries.end() ) { - unsigned int sub_idx = it - e.temporaries.begin(); - assert(sub_idx < e.states.size()); - e.states[sub_idx] = state; - return ; + break; } ), (Split, @@ -833,6 +828,9 @@ void MirBuilder::set_temp_state(const Span& sp, unsigned int idx, VarState state ) ) } + + ASSERT_BUG(sp, idx < m_temporary_states.size(), "Temporary " << idx << " out of range for state table"); + m_temporary_states[idx] = state; } void MirBuilder::drop_scope_values(const ScopeDef& sd) |