diff options
author | John Hodge <tpg@mutabah.net> | 2016-08-27 20:21:12 +0800 |
---|---|---|
committer | John Hodge <tpg@mutabah.net> | 2016-08-27 20:21:12 +0800 |
commit | 4d3b00c1e652b0c62be20cd4a6fbf6a015298a2b (patch) | |
tree | d6a280d7c17991c37ac69f599f99242aaef2bfe3 | |
parent | e91522341ea4c6922d25aaca3bce3138da95648d (diff) | |
download | mrust-4d3b00c1e652b0c62be20cd4a6fbf6a015298a2b.tar.gz |
HIR (De)serialise - Working deserialise
-rw-r--r-- | src/hir/deserialise.cpp | 92 | ||||
-rw-r--r-- | src/hir/serialise.cpp | 31 | ||||
-rw-r--r-- | src/hir/type.cpp | 10 | ||||
-rw-r--r-- | src/hir/type.hpp | 1 | ||||
-rw-r--r-- | src/main.cpp | 4 | ||||
-rw-r--r-- | src/mir/mir.cpp | 43 | ||||
-rw-r--r-- | src/mir/mir.hpp | 1 |
7 files changed, 159 insertions, 23 deletions
diff --git a/src/hir/deserialise.cpp b/src/hir/deserialise.cpp index 58e23bfa..b3d2a9f6 100644 --- a/src/hir/deserialise.cpp +++ b/src/hir/deserialise.cpp @@ -128,7 +128,7 @@ namespace { len = (len & 0x7F) << 16; len |= read_u16(); } - ::std::string rv( ' ', len ); + ::std::string rv(len, '\0'); m_is.read(const_cast<char*>(rv.data()), len); return rv; } @@ -158,6 +158,7 @@ namespace { for(size_t i = 0; i < n; i ++) { auto s = read_string(); + DEBUG("- " << s); rv.insert( ::std::make_pair( mv$(s), D<V>::des(*this) ) ); } return rv; @@ -212,6 +213,7 @@ namespace { ::HIR::TypeImpl deserialise_typeimpl() { ::HIR::TypeImpl rv; + TRACE_FUNCTION_FR("", "impl" << rv.m_params.fmt_args() << " " << rv.m_type); rv.m_params = deserialise_genericparams(); rv.m_type = deserialise_type(); @@ -225,12 +227,12 @@ namespace { } ) ); } // m_src_module doesn't matter after typeck - TRACE_FUNCTION_F("impl" << rv.m_params.fmt_args() << " " << rv.m_type); return rv; } ::HIR::TraitImpl deserialise_traitimpl() { ::HIR::TraitImpl rv; + TRACE_FUNCTION_FR("", "impl" << rv.m_params.fmt_args() << " ?" << rv.m_trait_args << " for " << rv.m_type); rv.m_params = deserialise_genericparams(); rv.m_trait_args = deserialise_pathparams(); @@ -263,7 +265,6 @@ namespace { } // m_src_module doesn't matter after typeck - TRACE_FUNCTION_F("impl" << rv.m_params.fmt_args() << " ?" << rv.m_trait_args << " for " << rv.m_type); return rv; } ::HIR::MarkerImpl deserialise_markerimpl() @@ -292,7 +293,11 @@ namespace { return rv; } ::MacroPatEnt deserialise_macropatent() { - ::MacroPatEnt rv { read_string(), static_cast<unsigned int>(read_count()), static_cast< ::MacroPatEnt::Type>(read_tag()) }; + ::MacroPatEnt rv { + read_string(), + static_cast<unsigned int>(read_count()), + static_cast< ::MacroPatEnt::Type>(read_tag()) + }; switch(rv.type) { case ::MacroPatEnt::PAT_TOKEN: @@ -375,7 +380,13 @@ namespace { ::MIR::Statement deserialise_mir_statement(); ::MIR::Terminator deserialise_mir_terminator(); - ::MIR::LValue deserialise_mir_lvalue() + ::MIR::LValue deserialise_mir_lvalue() { + ::MIR::LValue rv; + TRACE_FUNCTION_FR("", rv); + rv = deserialise_mir_lvalue_(); + return rv; + } + ::MIR::LValue deserialise_mir_lvalue_() { switch( read_tag() ) { @@ -405,6 +416,8 @@ namespace { } ::MIR::RValue deserialise_mir_rvalue() { + TRACE_FUNCTION; + switch( read_tag() ) { #define _(x, ...) case ::MIR::RValue::TAG_##x: return ::MIR::RValue::make_##x( __VA_ARGS__ ); @@ -456,9 +469,11 @@ namespace { } ::MIR::Constant deserialise_mir_constant() { + TRACE_FUNCTION; + switch( read_tag() ) { - #define _(x, ...) case ::MIR::Constant::TAG_##x: return ::MIR::Constant::make_##x( __VA_ARGS__ ); + #define _(x, ...) case ::MIR::Constant::TAG_##x: DEBUG("- " #x); return ::MIR::Constant::make_##x( __VA_ARGS__ ); _(Int, read_i64c()) _(Uint, read_u64c()) _(Float, read_double()) @@ -522,7 +537,9 @@ namespace { // - Value items ::HIR::Function deserialise_function() { - return ::HIR::Function { + TRACE_FUNCTION; + + ::HIR::Function rv { static_cast< ::HIR::Function::Receiver>( read_tag() ), read_string(), read_bool(), @@ -532,6 +549,7 @@ namespace { deserialise_type(), deserialise_exprptr() }; + return rv; } ::std::vector< ::std::pair< ::HIR::Pattern, ::HIR::TypeRef> > deserialise_fcnargs() { @@ -540,10 +558,13 @@ namespace { rv.reserve(n); for(size_t i = 0; i < n; i ++) rv.push_back( ::std::make_pair( ::HIR::Pattern{}, deserialise_type() ) ); + DEBUG("rv = " << rv); return rv; } ::HIR::Constant deserialise_constant() { + TRACE_FUNCTION; + return ::HIR::Constant { deserialise_genericparams(), deserialise_type(), @@ -554,6 +575,8 @@ namespace { } ::HIR::Static deserialise_static() { + TRACE_FUNCTION; + return ::HIR::Static { read_bool(), deserialise_type(), @@ -579,12 +602,13 @@ namespace { { switch( read_tag() ) { - #define _(x, ...) case ::HIR::TraitValueItem::TAG_##x: return ::HIR::TraitValueItem::make_##x( __VA_ARGS__ ); + #define _(x, ...) case ::HIR::TraitValueItem::TAG_##x: DEBUG("- " #x); return ::HIR::TraitValueItem::make_##x( __VA_ARGS__ ); _(Constant, deserialise_constant() ) _(Static, deserialise_static() ) _(Function, deserialise_function() ) #undef _ default: + DEBUG("Invalid TraitValueItem tag"); throw ""; } } @@ -612,7 +636,8 @@ namespace { template<typename T, typename U> struct D< ::std::pair<T,U> > { static ::std::pair<T,U> des(HirDeserialiser& d) { - return ::std::make_pair( D<T>::des(d), D<U>::des(d) ); + auto a = D<T>::des(d); + return ::std::make_pair( mv$(a), D<U>::des(d) ); }}; template<typename T> @@ -638,15 +663,17 @@ namespace { template<> DEF_D( ::MIR::LValue, return d.deserialise_mir_lvalue(); ) template<> DEF_D( ::MIR::Statement, return d.deserialise_mir_statement(); ) + template<> DEF_D( ::MIR::BasicBlock, return d.deserialise_mir_basicblock(); ) template<> DEF_D( ::HIR::TypeImpl, return d.deserialise_typeimpl(); ) template<> DEF_D( ::MacroRules, return d.deserialise_macrorules(); ) ::HIR::TypeRef HirDeserialiser::deserialise_type() { + TRACE_FUNCTION; switch( read_tag() ) { - #define _(x, ...) case ::HIR::TypeRef::Data::TAG_##x: return ::HIR::TypeRef( ::HIR::TypeRef::Data::make_##x( __VA_ARGS__ ) ); + #define _(x, ...) case ::HIR::TypeRef::Data::TAG_##x: DEBUG("- "#x); return ::HIR::TypeRef( ::HIR::TypeRef::Data::make_##x( __VA_ARGS__ ) ); _(Infer, {}) _(Diverge, {}) _(Primitive, @@ -698,6 +725,7 @@ namespace { ::HIR::SimplePath HirDeserialiser::deserialise_simplepath() { + TRACE_FUNCTION; return ::HIR::SimplePath { read_string(), deserialise_vec< ::std::string>() @@ -706,11 +734,13 @@ namespace { ::HIR::PathParams HirDeserialiser::deserialise_pathparams() { ::HIR::PathParams rv; + TRACE_FUNCTION_FR("", rv); rv.m_types = deserialise_vec< ::HIR::TypeRef>(); return rv; } ::HIR::GenericPath HirDeserialiser::deserialise_genericpath() { + TRACE_FUNCTION; return ::HIR::GenericPath { deserialise_simplepath(), deserialise_pathparams() @@ -727,23 +757,27 @@ namespace { } ::HIR::Path HirDeserialiser::deserialise_path() { + TRACE_FUNCTION; switch( read_tag() ) { case 0: + DEBUG("Generic"); return ::HIR::Path( deserialise_genericpath() ); case 1: - return ::HIR::Path( + DEBUG("Inherent"); + return ::HIR::Path { deserialise_type(), read_string(), deserialise_pathparams() - ); + }; case 2: - return ::HIR::Path( + DEBUG("Known"); + return ::HIR::Path { deserialise_type(), deserialise_genericpath(), read_string(), deserialise_pathparams() - ); + }; default: throw ""; } @@ -770,6 +804,9 @@ namespace { { switch( read_tag() ) { + case 0: + case 1: + return ::HIR::GenericBound::make_Lifetime({}); case 2: return ::HIR::GenericBound::make_TraitBound({ deserialise_type(), @@ -781,12 +818,14 @@ namespace { deserialise_type() }); default: + DEBUG("Bad GenericBound tag"); throw ""; } } ::HIR::Enum HirDeserialiser::deserialise_enum() { + TRACE_FUNCTION; return ::HIR::Enum { deserialise_genericparams(), static_cast< ::HIR::Enum::Repr>(read_tag()), @@ -814,22 +853,27 @@ namespace { } ::HIR::Struct HirDeserialiser::deserialise_struct() { + TRACE_FUNCTION; auto params = deserialise_genericparams(); auto repr = static_cast< ::HIR::Struct::Repr>( read_tag() ); + DEBUG("params = " << params.fmt_args() << params.fmt_bounds()); switch( read_tag() ) { case ::HIR::Struct::Data::TAG_Unit: + DEBUG("Unit"); return ::HIR::Struct { mv$(params), repr, ::HIR::Struct::Data::make_Unit( {} ) }; case ::HIR::Struct::Data::TAG_Tuple: + DEBUG("Tuple"); return ::HIR::Struct { mv$(params), repr, ::HIR::Struct::Data( deserialise_vec< ::HIR::VisEnt< ::HIR::TypeRef> >() ) }; case ::HIR::Struct::Data::TAG_Named: + DEBUG("Named"); return ::HIR::Struct { mv$(params), repr, ::HIR::Struct::Data( deserialise_vec< ::std::pair< ::std::string, ::HIR::VisEnt< ::HIR::TypeRef> > >() ) @@ -840,6 +884,8 @@ namespace { } ::HIR::Trait HirDeserialiser::deserialise_trait() { + TRACE_FUNCTION; + ::HIR::Trait rv { deserialise_genericparams(), "", // TODO: Better type for lifetime @@ -869,14 +915,22 @@ namespace { ::MIR::FunctionPointer HirDeserialiser::deserialise_mir() { + TRACE_FUNCTION; + ::MIR::Function rv; + rv.named_variables = deserialise_vec< ::HIR::TypeRef>( ); + DEBUG("named_variables = " << rv.named_variables); rv.temporaries = deserialise_vec< ::HIR::TypeRef>( ); - //rv.blocks = deserialise_vec< ::MIR::BasicBlock>( ); + DEBUG("temporaries = " << rv.temporaries); + rv.blocks = deserialise_vec< ::MIR::BasicBlock>( ); + return ::MIR::FunctionPointer( new ::MIR::Function(mv$(rv)) ); } ::MIR::BasicBlock HirDeserialiser::deserialise_mir_basicblock() { + TRACE_FUNCTION; + return ::MIR::BasicBlock { deserialise_vec< ::MIR::Statement>(), deserialise_mir_terminator() @@ -884,6 +938,8 @@ namespace { } ::MIR::Statement HirDeserialiser::deserialise_mir_statement() { + TRACE_FUNCTION; + switch( read_tag() ) { case 0: @@ -902,6 +958,8 @@ namespace { } ::MIR::Terminator HirDeserialiser::deserialise_mir_terminator() { + TRACE_FUNCTION; + switch( read_tag() ) { #define _(x, ...) case ::MIR::Terminator::TAG_##x: return ::MIR::Terminator::make_##x( __VA_ARGS__ ); @@ -987,11 +1045,15 @@ namespace { return ::HIR::CratePtr( mv$(rv) ); } + catch(int) + { ::std::abort(); } + #if 0 catch(const char*) { ::std::cerr << "Unable to load crate from " << filename << ": Deserialisation failure" << ::std::endl; ::std::abort(); //return ::HIR::CratePtr(); } + #endif } diff --git a/src/hir/serialise.cpp b/src/hir/serialise.cpp index 9ef9d19f..7e1c5176 100644 --- a/src/hir/serialise.cpp +++ b/src/hir/serialise.cpp @@ -122,6 +122,7 @@ namespace { { write_count(map.size()); for(const auto& v : map) { + DEBUG("- " << v.first); write_string(v.first); serialise(v.second); } @@ -230,14 +231,13 @@ namespace { } void serialise_traitpath(const ::HIR::TraitPath& path) { - //TRACE_FUNCTION_F("path="<<path); serialise_genericpath(path.m_path); // TODO: Lifetimes? (m_hrls) serialise_strmap(path.m_type_bounds); } void serialise_path(const ::HIR::Path& path) { - //TRACE_FUNCTION_F("path="<<path); + TRACE_FUNCTION_F("path="<<path); TU_MATCHA( (path.m_data), (e), (Generic, write_tag(0); @@ -278,10 +278,10 @@ namespace { void serialise(const ::HIR::GenericBound& b) { TU_MATCHA( (b), (e), (Lifetime, - //write_tag(0); + write_tag(0); ), (TypeLifetime, - //write_tag(1); + write_tag(1); ), (TraitBound, write_tag(2); @@ -408,9 +408,13 @@ namespace { void serialise(const ::MacroPatEnt& pe) { write_string(pe.name); write_count(pe.name_index); - serialise(pe.tok); - serialise_vec(pe.subpats); write_tag( static_cast<int>(pe.type) ); + if( pe.type == ::MacroPatEnt::PAT_TOKEN ) { + serialise(pe.tok); + } + else if( pe.type == ::MacroPatEnt::PAT_LOOP ) { + serialise_vec(pe.subpats); + } } void serialise(const ::MacroRulesArm& arm) { serialise_vec(arm.m_param_names); @@ -548,6 +552,7 @@ namespace { } void serialise(const ::MIR::LValue& lv) { + TRACE_FUNCTION_F("LValue = "<<lv); write_tag( static_cast<int>(lv.tag()) ); TU_MATCHA( (lv), (e), (Variable, @@ -583,6 +588,7 @@ namespace { } void serialise(const ::MIR::RValue& val) { + TRACE_FUNCTION_F("RValue = "<<val); write_tag( val.tag() ); TU_MATCHA( (val), (e), (Use, @@ -726,6 +732,8 @@ namespace { // - Value items void serialise(const ::HIR::Function& fcn) { + TRACE_FUNCTION_F("_function:"); + write_tag( static_cast<int>(fcn.m_receiver) ); write_string(fcn.m_abi); write_bool(fcn.m_unsafe); @@ -736,11 +744,14 @@ namespace { for(const auto& a : fcn.m_args) serialise(a.second); serialise(fcn.m_return); + DEBUG("m_args = " << fcn.m_args); serialise(fcn.m_code); } void serialise(const ::HIR::Constant& item) { + TRACE_FUNCTION_F("_constant:"); + serialise_generics(item.m_params); serialise(item.m_type); serialise(item.m_value); @@ -748,6 +759,8 @@ namespace { } void serialise(const ::HIR::Static& item) { + TRACE_FUNCTION_F("_static:"); + write_bool(item.m_is_mut); serialise(item.m_type); // NOTE: Omit the rest, not generic and emitted as part of the image. @@ -786,6 +799,8 @@ namespace { void serialise(const ::HIR::Struct& item) { + TRACE_FUNCTION; + serialise_generics(item.m_params); write_tag( static_cast<int>(item.m_repr) ); @@ -803,6 +818,7 @@ namespace { } void serialise(const ::HIR::Trait& item) { + TRACE_FUNCTION_F("_trait:"); serialise_generics(item.m_params); //write_string(item.m_lifetime); // TODO: Better type for lifetime serialise_vec( item.m_parent_traits ); @@ -815,12 +831,15 @@ namespace { write_tag( tvi.tag() ); TU_MATCHA( (tvi), (e), (Constant, + DEBUG("Constant"); serialise(e); ), (Static, + DEBUG("Static"); serialise(e); ), (Function, + DEBUG("Function"); serialise(e); ) ) diff --git a/src/hir/type.cpp b/src/hir/type.cpp index 57e57c16..7a833a32 100644 --- a/src/hir/type.cpp +++ b/src/hir/type.cpp @@ -36,6 +36,16 @@ namespace HIR { assert(!"Bad CoreType value"); return os; } + ::std::ostream& operator<<(::std::ostream& os, const BorrowType& bt) + { + switch(bt) + { + case BorrowType::Owned: return os << "Owned"; + case BorrowType::Unique: return os << "Unique"; + case BorrowType::Shared: return os << "Shared"; + } + return os; + } } void ::HIR::TypeRef::fmt(::std::ostream& os) const diff --git a/src/hir/type.hpp b/src/hir/type.hpp index 549bf837..8065bc1e 100644 --- a/src/hir/type.hpp +++ b/src/hir/type.hpp @@ -70,6 +70,7 @@ enum class BorrowType Unique, Owned, }; +extern ::std::ostream& operator<<(::std::ostream& os, const BorrowType& bt); struct LifetimeRef { diff --git a/src/main.cpp b/src/main.cpp index 71da37c8..e6e94132 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -32,7 +32,7 @@ int g_debug_indent_level = 0; void init_debug_list()
{
g_debug_disable_map.insert( "Parse" );
- g_debug_disable_map.insert( "Expand" );
+ //g_debug_disable_map.insert( "Expand" );
g_debug_disable_map.insert( "Resolve" );
@@ -57,7 +57,7 @@ void init_debug_list() g_debug_disable_map.insert( "MIR Validate" );
g_debug_disable_map.insert( "Dump MIR" );
- g_debug_disable_map.insert( "HIR Serialise" );
+ //g_debug_disable_map.insert( "HIR Serialise" );
}
bool debug_enabled()
{
diff --git a/src/mir/mir.cpp b/src/mir/mir.cpp index fb69bf3e..dc49efb2 100644 --- a/src/mir/mir.cpp +++ b/src/mir/mir.cpp @@ -72,6 +72,49 @@ namespace MIR { ) return os; } + + ::std::ostream& operator<<(::std::ostream& os, const RValue& x) + { + TU_MATCHA( (x), (e), + (Use, + os << "Use(" << e << ")"; + ), + (Constant, + os << "Constant(" << e << ")"; + ), + (SizedArray, + os << "SizedArray(" << e.val << "; " << e.count << ")"; + ), + (Borrow, + os << "Borrow(" << e.region << ", " << e.type << ", " << e.val << ")"; + ), + (Cast, + os << "Cast(" << e.val << " as " << e.type << ")"; + ), + (BinOp, + os << "BinOp(" << e.val_l << " " << static_cast<int>(e.op) << " " << e.val_r << ")"; + ), + (UniOp, + os << "UniOp(" << e.val << " " << static_cast<int>(e.op) << ")"; + ), + (DstMeta, + os << "DstMeta(" << e.val << ")"; + ), + (MakeDst, + os << "MakeDst(" << e.ptr_val << ", " << e.meta_val << ")"; + ), + (Tuple, + os << "Tuple(" << e.vals << ")"; + ), + (Array, + os << "Array(" << e.vals << ")"; + ), + (Struct, + os << "Struct(" << e.path << ", {" << e.vals << "})"; + ) + ) + return os; + } ::std::ostream& operator<<(::std::ostream& os, const Terminator& x) { diff --git a/src/mir/mir.hpp b/src/mir/mir.hpp index 3c52ab10..1e175ccd 100644 --- a/src/mir/mir.hpp +++ b/src/mir/mir.hpp @@ -136,6 +136,7 @@ TAGGED_UNION(RValue, Use, ::std::vector<LValue> vals; }) ); +extern ::std::ostream& operator<<(::std::ostream& os, const RValue& x); TAGGED_UNION(Terminator, Incomplete, (Incomplete, struct {}), // Block isn't complete (ERROR in output) |