diff options
author | John Hodge <tpg@mutabah.net> | 2018-02-18 12:14:06 +0800 |
---|---|---|
committer | John Hodge <tpg@mutabah.net> | 2018-02-18 12:14:06 +0800 |
commit | 08ab5d99ed8b7622440a8a33fed4a2475e5e714d (patch) | |
tree | 3be7fe35b4c1a803764e0e0fb85fb16c576f8aaf | |
parent | 954314365e4683317d2049a8cd76d5d75e9f4033 (diff) | |
download | mrust-08ab5d99ed8b7622440a8a33fed4a2475e5e714d.tar.gz |
Standalone MIRI (and mmir codegen) - Support external functions
-rw-r--r-- | src/trans/codegen_mmir.cpp | 24 | ||||
-rw-r--r-- | tools/standalone_miri/module_tree.cpp | 20 | ||||
-rw-r--r-- | tools/standalone_miri/module_tree.hpp | 6 |
3 files changed, 47 insertions, 3 deletions
diff --git a/src/trans/codegen_mmir.cpp b/src/trans/codegen_mmir.cpp index 345a5703..703ef44d 100644 --- a/src/trans/codegen_mmir.cpp +++ b/src/trans/codegen_mmir.cpp @@ -796,6 +796,30 @@ namespace m_mir_res = nullptr; } + void emit_function_ext(const ::HIR::Path& p, const ::HIR::Function& item, const Trans_Params& params) override + { + ::MIR::Function empty_fcn; + ::MIR::TypeResolve top_mir_res { sp, m_resolve, FMT_CB(ss, ss << "extern fn " << p;), ::HIR::TypeRef(), {}, empty_fcn }; + m_mir_res = &top_mir_res; + TRACE_FUNCTION_F(p); + + // If the function is a C external, emit as such + if( item.m_linkage.name != "" ) + { + ::HIR::TypeRef ret_type_tmp; + const auto& ret_type = monomorphise_fcn_return(ret_type_tmp, item, params); + + m_of << "fn " << p << "("; + for(unsigned int i = 0; i < item.m_args.size(); i ++) + { + if( i != 0 ) m_of << ", "; + m_of << params.monomorph(m_resolve, item.m_args[i].second); + } + m_of << "): " << ret_type << " = \"" << item.m_linkage.name << "\":\"" << item.m_abi << "\";\n"; + } + + m_mir_res = nullptr; + } void emit_function_code(const ::HIR::Path& p, const ::HIR::Function& item, const Trans_Params& params, bool is_extern_def, const ::MIR::FunctionPointer& code) override { TRACE_FUNCTION_F(p); diff --git a/tools/standalone_miri/module_tree.cpp b/tools/standalone_miri/module_tree.cpp index 321eaad9..a457db8e 100644 --- a/tools/standalone_miri/module_tree.cpp +++ b/tools/standalone_miri/module_tree.cpp @@ -91,10 +91,24 @@ bool Parser::parse_one() { rv_ty = parse_type(); } - auto body = parse_body(); + + if( lex.consume_if('=') ) + { + auto link_name = ::std::move(lex.check_consume(TokenClass::String).strval); + lex.check_consume(':'); + auto abi = ::std::move(lex.check_consume(TokenClass::String).strval); + lex.check_consume(';'); - auto p2 = p; - tree.functions.insert( ::std::make_pair(::std::move(p), Function { ::std::move(p2), ::std::move(arg_tys), rv_ty, ::std::move(body) }) ); + auto p2 = p; + tree.functions.insert( ::std::make_pair(::std::move(p), Function { ::std::move(p2), ::std::move(arg_tys), rv_ty, {link_name, abi}, {} }) ); + } + else + { + auto body = parse_body(); + + auto p2 = p; + tree.functions.insert( ::std::make_pair(::std::move(p), Function { ::std::move(p2), ::std::move(arg_tys), rv_ty, {}, ::std::move(body) }) ); + } } else if( lex.consume_if("static") ) { diff --git a/tools/standalone_miri/module_tree.hpp b/tools/standalone_miri/module_tree.hpp index dd302367..3489e77e 100644 --- a/tools/standalone_miri/module_tree.hpp +++ b/tools/standalone_miri/module_tree.hpp @@ -17,6 +17,12 @@ struct Function ::HIR::Path my_path; ::std::vector<::HIR::TypeRef> args; ::HIR::TypeRef ret_ty; + + // If `link_name` is non-empty, then the function is an external + struct { + ::std::string link_name; + ::std::string link_abi; + } external; ::MIR::Function m_mir; }; |