Daniel Dunbar | e224628 | 2010-02-25 08:49:05 +0000 | [diff] [blame] | 1 | //===-- examples/clang-interpreter/main.cpp - Clang C Interpreter Example -===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Daniel Dunbar | 9b414d3 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 10 | #include "clang/CodeGen/CodeGenAction.h" |
Daniel Dunbar | e224628 | 2010-02-25 08:49:05 +0000 | [diff] [blame] | 11 | #include "clang/Driver/Compilation.h" |
| 12 | #include "clang/Driver/Driver.h" |
| 13 | #include "clang/Driver/Tool.h" |
Daniel Dunbar | e224628 | 2010-02-25 08:49:05 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/CompilerInvocation.h" |
| 15 | #include "clang/Frontend/CompilerInstance.h" |
| 16 | #include "clang/Frontend/DiagnosticOptions.h" |
| 17 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 18 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
| 19 | |
Daniel Dunbar | e224628 | 2010-02-25 08:49:05 +0000 | [diff] [blame] | 20 | #include "llvm/Module.h" |
| 21 | #include "llvm/Config/config.h" |
| 22 | #include "llvm/ADT/OwningPtr.h" |
| 23 | #include "llvm/ADT/SmallString.h" |
| 24 | #include "llvm/Config/config.h" |
Eli Friedman | 17e2794 | 2011-10-06 22:24:13 +0000 | [diff] [blame] | 25 | #include "llvm/ExecutionEngine/JIT.h" |
Daniel Dunbar | e224628 | 2010-02-25 08:49:05 +0000 | [diff] [blame] | 26 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
| 27 | #include "llvm/Support/ManagedStatic.h" |
| 28 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Host.h" |
| 30 | #include "llvm/Support/Path.h" |
Evan Cheng | a6b4045 | 2011-08-24 18:09:14 +0000 | [diff] [blame] | 31 | #include "llvm/Support/TargetSelect.h" |
Daniel Dunbar | e224628 | 2010-02-25 08:49:05 +0000 | [diff] [blame] | 32 | using namespace clang; |
| 33 | using namespace clang::driver; |
| 34 | |
Dan Gohman | ea1924e | 2010-11-17 17:23:53 +0000 | [diff] [blame] | 35 | // This function isn't referenced outside its translation unit, but it |
| 36 | // can't use the "static" keyword because its address is used for |
| 37 | // GetMainExecutable (since some platforms don't support taking the |
| 38 | // address of main, and some platforms can't implement GetMainExecutable |
| 39 | // without being given the address of a function in the main executable). |
Benjamin Kramer | aeed3da | 2010-10-30 17:32:40 +0000 | [diff] [blame] | 40 | llvm::sys::Path GetExecutablePath(const char *Argv0) { |
Daniel Dunbar | e224628 | 2010-02-25 08:49:05 +0000 | [diff] [blame] | 41 | // This just needs to be some symbol in the binary; C++ doesn't |
| 42 | // allow taking the address of ::main however. |
| 43 | void *MainAddr = (void*) (intptr_t) GetExecutablePath; |
| 44 | return llvm::sys::Path::GetMainExecutable(Argv0, MainAddr); |
| 45 | } |
| 46 | |
Dan Gohman | f129562 | 2010-10-29 22:41:35 +0000 | [diff] [blame] | 47 | static int Execute(llvm::Module *Mod, char * const *envp) { |
Daniel Dunbar | e224628 | 2010-02-25 08:49:05 +0000 | [diff] [blame] | 48 | llvm::InitializeNativeTarget(); |
| 49 | |
| 50 | std::string Error; |
| 51 | llvm::OwningPtr<llvm::ExecutionEngine> EE( |
| 52 | llvm::ExecutionEngine::createJIT(Mod, &Error)); |
| 53 | if (!EE) { |
| 54 | llvm::errs() << "unable to make execution engine: " << Error << "\n"; |
| 55 | return 255; |
| 56 | } |
| 57 | |
| 58 | llvm::Function *EntryFn = Mod->getFunction("main"); |
| 59 | if (!EntryFn) { |
| 60 | llvm::errs() << "'main' function not found in module.\n"; |
| 61 | return 255; |
| 62 | } |
| 63 | |
| 64 | // FIXME: Support passing arguments. |
| 65 | std::vector<std::string> Args; |
| 66 | Args.push_back(Mod->getModuleIdentifier()); |
| 67 | |
| 68 | return EE->runFunctionAsMain(EntryFn, Args, envp); |
| 69 | } |
| 70 | |
| 71 | int main(int argc, const char **argv, char * const *envp) { |
| 72 | void *MainAddr = (void*) (intptr_t) GetExecutablePath; |
| 73 | llvm::sys::Path Path = GetExecutablePath(argv[0]); |
Benjamin Kramer | 06b901b | 2010-08-26 13:48:56 +0000 | [diff] [blame] | 74 | TextDiagnosticPrinter *DiagClient = |
| 75 | new TextDiagnosticPrinter(llvm::errs(), DiagnosticOptions()); |
Daniel Dunbar | e224628 | 2010-02-25 08:49:05 +0000 | [diff] [blame] | 76 | |
Eli Friedman | 97ab3ac | 2010-11-24 00:32:51 +0000 | [diff] [blame] | 77 | llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
Eli Friedman | d3c1661 | 2011-09-27 18:33:47 +0000 | [diff] [blame] | 78 | DiagnosticsEngine Diags(DiagID, DiagClient); |
Sebastian Pop | 5d8b954 | 2011-11-01 21:33:06 +0000 | [diff] [blame] | 79 | Driver TheDriver(Path.str(), llvm::sys::getDefaultTargetTriple(), |
Bob Wilson | 10a82cd | 2011-10-04 05:34:14 +0000 | [diff] [blame] | 80 | "a.out", /*IsProduction=*/false, Diags); |
Daniel Dunbar | e224628 | 2010-02-25 08:49:05 +0000 | [diff] [blame] | 81 | TheDriver.setTitle("clang interpreter"); |
| 82 | |
| 83 | // FIXME: This is a hack to try to force the driver to do something we can |
| 84 | // recognize. We need to extend the driver library to support this use model |
| 85 | // (basically, exactly one input, and the operation mode is hard wired). |
| 86 | llvm::SmallVector<const char *, 16> Args(argv, argv + argc); |
| 87 | Args.push_back("-fsyntax-only"); |
John Thompson | 903dec2 | 2011-03-29 18:31:21 +0000 | [diff] [blame] | 88 | llvm::OwningPtr<Compilation> C(TheDriver.BuildCompilation(Args)); |
Daniel Dunbar | e224628 | 2010-02-25 08:49:05 +0000 | [diff] [blame] | 89 | if (!C) |
| 90 | return 0; |
| 91 | |
| 92 | // FIXME: This is copied from ASTUnit.cpp; simplify and eliminate. |
| 93 | |
| 94 | // We expect to get back exactly one command job, if we didn't something |
| 95 | // failed. Extract that job from the compilation. |
| 96 | const driver::JobList &Jobs = C->getJobs(); |
Manuel Klimek | c8b3e63 | 2011-05-23 18:25:41 +0000 | [diff] [blame] | 97 | if (Jobs.size() != 1 || !isa<driver::Command>(*Jobs.begin())) { |
Daniel Dunbar | e224628 | 2010-02-25 08:49:05 +0000 | [diff] [blame] | 98 | llvm::SmallString<256> Msg; |
| 99 | llvm::raw_svector_ostream OS(Msg); |
| 100 | C->PrintJob(OS, C->getJobs(), "; ", true); |
| 101 | Diags.Report(diag::err_fe_expected_compiler_job) << OS.str(); |
| 102 | return 1; |
| 103 | } |
| 104 | |
| 105 | const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin()); |
| 106 | if (llvm::StringRef(Cmd->getCreator().getName()) != "clang") { |
| 107 | Diags.Report(diag::err_fe_expected_clang_command); |
| 108 | return 1; |
| 109 | } |
| 110 | |
| 111 | // Initialize a compiler invocation object from the clang (-cc1) arguments. |
| 112 | const driver::ArgStringList &CCArgs = Cmd->getArguments(); |
| 113 | llvm::OwningPtr<CompilerInvocation> CI(new CompilerInvocation); |
Benjamin Kramer | 9e8635a | 2010-04-20 11:50:39 +0000 | [diff] [blame] | 114 | CompilerInvocation::CreateFromArgs(*CI, |
| 115 | const_cast<const char **>(CCArgs.data()), |
| 116 | const_cast<const char **>(CCArgs.data()) + |
| 117 | CCArgs.size(), |
Benjamin Kramer | a4f0a80 | 2010-04-20 11:55:38 +0000 | [diff] [blame] | 118 | Diags); |
Daniel Dunbar | e224628 | 2010-02-25 08:49:05 +0000 | [diff] [blame] | 119 | |
| 120 | // Show the invocation, with -v. |
| 121 | if (CI->getHeaderSearchOpts().Verbose) { |
| 122 | llvm::errs() << "clang invocation:\n"; |
| 123 | C->PrintJob(llvm::errs(), C->getJobs(), "\n", true); |
| 124 | llvm::errs() << "\n"; |
| 125 | } |
| 126 | |
| 127 | // FIXME: This is copied from cc1_main.cpp; simplify and eliminate. |
| 128 | |
| 129 | // Create a compiler instance to handle the actual work. |
| 130 | CompilerInstance Clang; |
Daniel Dunbar | e224628 | 2010-02-25 08:49:05 +0000 | [diff] [blame] | 131 | Clang.setInvocation(CI.take()); |
| 132 | |
| 133 | // Create the compilers actual diagnostics engine. |
Benjamin Kramer | a4f0a80 | 2010-04-20 11:55:38 +0000 | [diff] [blame] | 134 | Clang.createDiagnostics(int(CCArgs.size()),const_cast<char**>(CCArgs.data())); |
Daniel Dunbar | e224628 | 2010-02-25 08:49:05 +0000 | [diff] [blame] | 135 | if (!Clang.hasDiagnostics()) |
| 136 | return 1; |
| 137 | |
| 138 | // Infer the builtin include path if unspecified. |
| 139 | if (Clang.getHeaderSearchOpts().UseBuiltinIncludes && |
| 140 | Clang.getHeaderSearchOpts().ResourceDir.empty()) |
| 141 | Clang.getHeaderSearchOpts().ResourceDir = |
| 142 | CompilerInvocation::GetResourcesPath(argv[0], MainAddr); |
| 143 | |
| 144 | // Create and execute the frontend to generate an LLVM bitcode module. |
| 145 | llvm::OwningPtr<CodeGenAction> Act(new EmitLLVMOnlyAction()); |
| 146 | if (!Clang.ExecuteAction(*Act)) |
| 147 | return 1; |
| 148 | |
| 149 | int Res = 255; |
| 150 | if (llvm::Module *Module = Act->takeModule()) |
| 151 | Res = Execute(Module, envp); |
| 152 | |
| 153 | // Shutdown. |
| 154 | |
| 155 | llvm::llvm_shutdown(); |
| 156 | |
| 157 | return Res; |
| 158 | } |