summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn Hodge <tpg@mutabah.net>2018-05-27 11:34:36 +0800
committerJohn Hodge <tpg@mutabah.net>2018-05-27 11:34:36 +0800
commit30783d1e61a83bb680f1231af32004f4c994fea3 (patch)
tree2618d25f73c4f6653f3ddfe54e19fb947792b2d0 /src
parentaf1ec8a893d33aa974186d06498d0fc81d22ae7b (diff)
downloadmrust-30783d1e61a83bb680f1231af32004f4c994fea3.tar.gz
Trans - Plannin for dynamic library support
Diffstat (limited to 'src')
-rw-r--r--src/main.cpp107
-rw-r--r--src/trans/codegen_c.cpp1
-rw-r--r--src/trans/main_bindings.hpp7
3 files changed, 67 insertions, 48 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 0f325230..f3b5a2cc 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -670,72 +670,83 @@ int main(int argc, char *argv[])
// If the test harness is enabled, override crate type to "Executable"
crate_type = ::AST::Crate::Type::Executable;
}
- switch( crate_type )
+
+ // Enumerate items to be passed to codegen
+ TransList items = CompilePhase<TransList>("Trans Enumerate", [&]() {
+ switch( crate_type )
+ {
+ case ::AST::Crate::Type::Unknown:
+ ::std::cerr << "BUG? Unknown crate type" << ::std::endl;
+ exit(1);
+ break;
+ case ::AST::Crate::Type::RustLib:
+ case ::AST::Crate::Type::RustDylib:
+ case ::AST::Crate::Type::CDylib:
+ return Trans_Enumerate_Public(*hir_crate);
+ case ::AST::Crate::Type::ProcMacro:
+ // TODO: proc macros enumerate twice, once as a library (why?) and again as an executable
+ return Trans_Enumerate_Public(*hir_crate);
+ case ::AST::Crate::Type::Executable:
+ return Trans_Enumerate_Main(*hir_crate);
+ }
+ throw ::std::runtime_error("Invalid crate_type value");
+ });
+ // - Generate monomorphised versions of all functions
+ CompilePhaseV("Trans Monomorph", [&]() { Trans_Monomorphise_List(*hir_crate, items); });
+ // - Do post-monomorph inlining
+ CompilePhaseV("MIR Optimise Inline", [&]() { MIR_OptimiseCrate_Inlining(*hir_crate, items); });
+ // - Clean up no-unused functions
+ //CompilePhaseV("Trans Enumerate Cleanup", [&]() { Trans_Enumerate_Cleanup(*hir_crate, items); });
+
+ switch(crate_type)
{
case ::AST::Crate::Type::Unknown:
- // ERROR?
- break;
- case ::AST::Crate::Type::RustLib: {
- #if 1
- // Generate a .o
- TransList items = CompilePhase<TransList>("Trans Enumerate", [&]() { return Trans_Enumerate_Public(*hir_crate); });
- CompilePhaseV("Trans Monomorph", [&]() { Trans_Monomorphise_List(*hir_crate, items); });
- CompilePhaseV("MIR Optimise Inline", [&]() { MIR_OptimiseCrate_Inlining(*hir_crate, items); });
- //CompilePhaseV("Trans Enumerate Cleanup", [&]() { Trans_Enumerate_Cleanup(*hir_crate, items); });
- CompilePhaseV("Trans Codegen", [&]() { Trans_Codegen(params.outfile + ".o", trans_opt, *hir_crate, items, false); });
- #endif
-
+ throw "";
+ case ::AST::Crate::Type::RustLib:
+ // Generate a loadable .o
+ CompilePhaseV("Trans Codegen", [&]() { Trans_Codegen(params.outfile + ".o", trans_opt, *hir_crate, items, /*is_executable=*/false); });
// Save a loadable HIR dump
- CompilePhaseV("HIR Serialise", [&]() {
- //HIR_Serialise(params.outfile + ".meta", *hir_crate);
- HIR_Serialise(params.outfile, *hir_crate);
- });
-
- // Link metatdata and object into a .rlib
- break; }
- case ::AST::Crate::Type::RustDylib: {
- #if 1
- // Generate a .o
- TransList items = CompilePhase<TransList>("Trans Enumerate", [&]() { return Trans_Enumerate_Public(*hir_crate); });
- CompilePhaseV("Trans Monomorph", [&]() { Trans_Monomorphise_List(*hir_crate, items); });
- CompilePhaseV("MIR Optimise Inline", [&]() { MIR_OptimiseCrate_Inlining(*hir_crate, items); });
- CompilePhaseV("Trans Codegen", [&]() { Trans_Codegen(params.outfile + ".o", trans_opt, *hir_crate, items, false); });
- #endif
+ CompilePhaseV("HIR Serialise", [&]() { HIR_Serialise(params.outfile, *hir_crate); });
+ // TODO: Link metatdata and object into a .rlib
+ //Trans_Link(params.outfile, params.outfile + ".hir", params.outfile + ".o", CodegenOutput::StaticLibrary);
+ break;
+ case ::AST::Crate::Type::RustDylib:
+ // Generate a .so
+ //CompilePhaseV("Trans Codegen", [&]() { Trans_Codegen(params.outfile + ".so", trans_opt, *hir_crate, items, CodegenOutput::DynamicLibrary); });
+ CompilePhaseV("Trans Codegen", [&]() { Trans_Codegen(params.outfile + ".o", trans_opt, *hir_crate, items, /*is_executable=*/false); });
// Save a loadable HIR dump
CompilePhaseV("HIR Serialise", [&]() { HIR_Serialise(params.outfile, *hir_crate); });
-
- // Generate a .so/.dll
- // TODO: Codegen and include the metadata in a non-loadable segment
- break; }
+ // TODO: Add the metadata to the .so as a non-loadable segment
+ //Trans_Link(params.outfile, params.outfile + ".hir", params.outfile + ".o", CodegenOutput::DynamicLibrary);
+ break;
case ::AST::Crate::Type::CDylib:
// Generate a .so/.dll
+ CompilePhaseV("Trans Codegen", [&]() { Trans_Codegen(params.outfile, trans_opt, *hir_crate, items, /*is_executable=*/false); });
+ // - No metadata file
+ //Trans_Link(params.outfile, "", params.outfile + ".o", CodegenOutput::DynamicLibrary);
break;
case ::AST::Crate::Type::ProcMacro: {
// Needs: An executable (the actual macro handler), metadata (for `extern crate foo;`)
- // Can just emit the metadata and do miri?
- // - Requires MIR for EVERYTHING, not feasable.
- TransList items = CompilePhase<TransList>("Trans Enumerate", [&]() { return Trans_Enumerate_Public(*hir_crate); });
- CompilePhaseV("Trans Monomorph", [&]() { Trans_Monomorphise_List(*hir_crate, items); });
- CompilePhaseV("MIR Optimise Inline", [&]() { MIR_OptimiseCrate_Inlining(*hir_crate, items); });
- CompilePhaseV("Trans Codegen", [&]() { Trans_Codegen(params.outfile + ".o", trans_opt, *hir_crate, items, false); });
+ // 1. Generate code for the .o file
+ // TODO: Is the .o actually needed for proc macros?
+ CompilePhaseV("Trans Codegen", [&]() { Trans_Codegen(params.outfile + ".o", trans_opt, *hir_crate, items, /*is_executable=*/false); });
+
+ // 2. Generate code for the plugin itself
TransList items2 = CompilePhase<TransList>("Trans Enumerate", [&]() { return Trans_Enumerate_Main(*hir_crate); });
CompilePhaseV("Trans Monomorph", [&]() { Trans_Monomorphise_List(*hir_crate, items2); });
CompilePhaseV("MIR Optimise Inline", [&]() { MIR_OptimiseCrate_Inlining(*hir_crate, items2); });
- CompilePhaseV("Trans Codegen", [&]() { Trans_Codegen(params.outfile + "-plugin", trans_opt, *hir_crate, items2, true); });
+ CompilePhaseV("Trans Codegen", [&]() { Trans_Codegen(params.outfile + "-plugin", trans_opt, *hir_crate, items2, /*is_executable=*/true); });
- hir_crate->m_lang_items.clear(); // Make sure that we're not exporting any lang items
+ // - Save a very basic HIR dump, making sure that there's no lang items in it (e.g. `mrustc-main`)
+ hir_crate->m_lang_items.clear();
CompilePhaseV("HIR Serialise", [&]() { HIR_Serialise(params.outfile, *hir_crate); });
+ //Trans_Link(params.outfile, params.outfile + ".hir", params.outfile + ".o", CodegenOutput::StaticLibrary);
+ //Trans_Link(params.outfile+"-plugin", "", params.outfile + "-plugin.o", CodegenOutput::StaticLibrary);
break; }
case ::AST::Crate::Type::Executable:
- // Generate a binary
- // - Enumerate items for translation
- TransList items = CompilePhase<TransList>("Trans Enumerate", [&]() { return Trans_Enumerate_Main(*hir_crate); });
- // - Monomorphise
- CompilePhaseV("Trans Monomorph", [&]() { Trans_Monomorphise_List(*hir_crate, items); });
- CompilePhaseV("MIR Optimise Inline", [&]() { MIR_OptimiseCrate_Inlining(*hir_crate, items); });
- // - Perform codegen
- CompilePhaseV("Trans Codegen", [&]() { Trans_Codegen(params.outfile, trans_opt, *hir_crate, items, true); });
+ CompilePhaseV("Trans Codegen", [&]() { Trans_Codegen(params.outfile, trans_opt, *hir_crate, items, /*is_executable=*/true); });
+ //Trans_Link(params.outfile, "", params.outfile + ".o", CodegenOutput::Executable);
break;
}
}
diff --git a/src/trans/codegen_c.cpp b/src/trans/codegen_c.cpp
index 187485dc..f891d267 100644
--- a/src/trans/codegen_c.cpp
+++ b/src/trans/codegen_c.cpp
@@ -510,6 +510,7 @@ namespace {
emit_box_drop_glue( mv$(e.first), *e.second );
}
+ // TODO: Define this function in MIR.
if( is_executable )
{
m_of << "int main(int argc, const char* argv[]) {\n";
diff --git a/src/trans/main_bindings.hpp b/src/trans/main_bindings.hpp
index 46d2cdb1..b938e8bf 100644
--- a/src/trans/main_bindings.hpp
+++ b/src/trans/main_bindings.hpp
@@ -24,6 +24,13 @@ struct TransOptions
::std::vector< ::std::string> libraries;
};
+enum class CodegenOutput {
+ Object, // .o
+ StaticLibrary, // .a
+ DynamicLibrary, // .so
+ Executable, // no suffix, includes main stub (TODO: Can't that just be added earlier?)
+};
+
extern TransList Trans_Enumerate_Main(const ::HIR::Crate& crate);
// NOTE: This also sets the saveout flags
extern TransList Trans_Enumerate_Public(::HIR::Crate& crate);