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