diff options
author | John Hodge <tpg@mutabah.net> | 2018-03-04 20:04:05 +0800 |
---|---|---|
committer | John Hodge <tpg@mutabah.net> | 2018-03-17 18:52:16 +0800 |
commit | dcf1204a8bae3f15e875ffe9c861c38789f524fe (patch) | |
tree | 4405146ea56a7d409d59d0a9cf60fd0700bc28a2 /src/trans/monomorphise.cpp | |
parent | 30659582e602d3b95fdf3ab390c49c67292ea48f (diff) | |
download | mrust-dcf1204a8bae3f15e875ffe9c861c38789f524fe.tar.gz |
Trans - Move monomorphisation from codegen pass to its own pass, and do a second inlining pass after monomorph.
Diffstat (limited to 'src/trans/monomorphise.cpp')
-rw-r--r-- | src/trans/monomorphise.cpp | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/trans/monomorphise.cpp b/src/trans/monomorphise.cpp index fd05c85b..892cb730 100644 --- a/src/trans/monomorphise.cpp +++ b/src/trans/monomorphise.cpp @@ -6,8 +6,10 @@ * - MIR monomorphisation */ #include "monomorphise.hpp" +#include "hir_typeck/static.hpp" #include <mir/mir.hpp> #include <hir/hir.hpp> +#include <mir/operations.hpp> // Needed for post-monomorph checks and optimisations namespace { ::MIR::LValue monomorph_LValue(const ::StaticTraitResolve& resolve, const Trans_Params& params, const ::MIR::LValue& tpl) @@ -333,3 +335,41 @@ namespace { return ::MIR::FunctionPointer( box$(output).release() ); } + +/// Monomorphise all functions in a TransList +void Trans_Monomorphise_List(const ::HIR::Crate& crate, TransList& list) +{ + ::StaticTraitResolve resolve { crate }; + for(auto& fcn_ent : list.m_functions) + { + const auto& fcn = *fcn_ent.second->ptr; + // Trait methods (which are the only case where `Self` can exist in the argument list at this stage) always need to be monomorphised. + bool is_method = ( fcn.m_args.size() > 0 && visit_ty_with(fcn.m_args[0].second, [&](const auto& x){return x == ::HIR::TypeRef("Self",0xFFFF);}) ); + if(fcn_ent.second->pp.has_types() || is_method) + { + const auto& path = fcn_ent.first; + const auto& pp = fcn_ent.second->pp; + TRACE_FUNCTION_FR(path, path); + + auto mir = Trans_Monomorphise(resolve, fcn_ent.second->pp, fcn.m_code.m_mir); + + // TODO: Should these be moved to their own pass? Potentially not, the extra pass should just be an inlining optimise pass + auto ret_type = pp.monomorph(resolve, fcn.m_return); + ::HIR::Function::args_t args; + for(const auto& a : fcn.m_args) + args.push_back(::std::make_pair( ::HIR::Pattern{}, pp.monomorph(resolve, a.second) )); + + ::std::string s = FMT(path); + ::HIR::ItemPath ip(s); + MIR_Validate(resolve, ip, *mir, args, ret_type); + MIR_Cleanup(resolve, ip, *mir, args, ret_type); + MIR_Optimise(resolve, ip, *mir, args, ret_type); + MIR_Validate(resolve, ip, *mir, args, ret_type); + + fcn_ent.second->monomorphised.ret_ty = ::std::move(ret_type); + fcn_ent.second->monomorphised.arg_tys = ::std::move(args); + fcn_ent.second->monomorphised.code = ::std::move(mir); + } + } +} + |