summaryrefslogtreecommitdiffstats
path: root/gnu/llvm/tools/clang/examples/clang-interpreter
diff options
context:
space:
mode:
Diffstat (limited to 'gnu/llvm/tools/clang/examples/clang-interpreter')
-rw-r--r--gnu/llvm/tools/clang/examples/clang-interpreter/CMakeLists.txt23
-rw-r--r--gnu/llvm/tools/clang/examples/clang-interpreter/Makefile28
-rw-r--r--gnu/llvm/tools/clang/examples/clang-interpreter/README.txt17
-rw-r--r--gnu/llvm/tools/clang/examples/clang-interpreter/main.cpp175
4 files changed, 243 insertions, 0 deletions
diff --git a/gnu/llvm/tools/clang/examples/clang-interpreter/CMakeLists.txt b/gnu/llvm/tools/clang/examples/clang-interpreter/CMakeLists.txt
new file mode 100644
index 00000000000..4c6db12a4e4
--- /dev/null
+++ b/gnu/llvm/tools/clang/examples/clang-interpreter/CMakeLists.txt
@@ -0,0 +1,23 @@
+set(LLVM_LINK_COMPONENTS
+ Core
+ ExecutionEngine
+ MC
+ MCJIT
+ Support
+ native
+ )
+
+add_clang_executable(clang-interpreter
+ main.cpp
+ )
+
+add_dependencies(clang-interpreter
+ clang-headers
+ )
+
+target_link_libraries(clang-interpreter
+ clangBasic
+ clangCodeGen
+ clangDriver
+ clangFrontend
+ )
diff --git a/gnu/llvm/tools/clang/examples/clang-interpreter/Makefile b/gnu/llvm/tools/clang/examples/clang-interpreter/Makefile
new file mode 100644
index 00000000000..2eff90b32b0
--- /dev/null
+++ b/gnu/llvm/tools/clang/examples/clang-interpreter/Makefile
@@ -0,0 +1,28 @@
+##===- examples/clang-interpreter/Makefile -----------------*- Makefile -*-===##
+#
+# The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+
+CLANG_LEVEL := ../..
+
+TOOLNAME = clang-interpreter
+NO_INSTALL = 1
+
+# No plugins, optimize startup time.
+TOOL_NO_EXPORTS = 1
+
+LINK_COMPONENTS := mcjit interpreter nativecodegen bitreader bitwriter irreader \
+ ipo linker selectiondag asmparser instrumentation objcarcopts option
+USEDLIBS = clangFrontend.a clangSerialization.a clangDriver.a clangCodeGen.a \
+ clangParse.a clangSema.a clangStaticAnalyzerFrontend.a \
+ clangStaticAnalyzerCheckers.a clangStaticAnalyzerCore.a \
+ clangAnalysis.a clangRewrite.a clangRewriteFrontend.a \
+ clangEdit.a clangAST.a clangLex.a clangBasic.a LLVMCore.a \
+ LLVMExecutionEngine.a LLVMMC.a LLVMMCJIT.a LLVMRuntimeDyld.a \
+ LLVMObject.a LLVMSupport.a LLVMProfileData.a
+
+include $(CLANG_LEVEL)/Makefile
diff --git a/gnu/llvm/tools/clang/examples/clang-interpreter/README.txt b/gnu/llvm/tools/clang/examples/clang-interpreter/README.txt
new file mode 100644
index 00000000000..7dd45fad504
--- /dev/null
+++ b/gnu/llvm/tools/clang/examples/clang-interpreter/README.txt
@@ -0,0 +1,17 @@
+This is an example of Clang based interpreter, for executing standalone C
+programs.
+
+It demonstrates the following features:
+ 1. Parsing standard compiler command line arguments using the Driver library.
+
+ 2. Constructing a Clang compiler instance, using the appropriate arguments
+ derived in step #1.
+
+ 3. Invoking the Clang compiler to lex, parse, syntax check, and then generate
+ LLVM code.
+
+ 4. Use the LLVM JIT functionality to execute the final module.
+
+The implementation has many limitations and is not designed to be a full fledged
+C interpreter. It is designed to demonstrate a simple but functional use of the
+Clang compiler libraries.
diff --git a/gnu/llvm/tools/clang/examples/clang-interpreter/main.cpp b/gnu/llvm/tools/clang/examples/clang-interpreter/main.cpp
new file mode 100644
index 00000000000..9b4a257bcba
--- /dev/null
+++ b/gnu/llvm/tools/clang/examples/clang-interpreter/main.cpp
@@ -0,0 +1,175 @@
+//===-- examples/clang-interpreter/main.cpp - Clang C Interpreter Example -===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/CodeGen/CodeGenAction.h"
+#include "clang/Basic/DiagnosticOptions.h"
+#include "clang/Driver/Compilation.h"
+#include "clang/Driver/Driver.h"
+#include "clang/Driver/Tool.h"
+#include "clang/Frontend/CompilerInstance.h"
+#include "clang/Frontend/CompilerInvocation.h"
+#include "clang/Frontend/FrontendDiagnostic.h"
+#include "clang/Frontend/TextDiagnosticPrinter.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ExecutionEngine/ExecutionEngine.h"
+#include "llvm/ExecutionEngine/MCJIT.h"
+#include "llvm/IR/Module.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Host.h"
+#include "llvm/Support/ManagedStatic.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/TargetSelect.h"
+#include "llvm/Support/raw_ostream.h"
+#include <memory>
+using namespace clang;
+using namespace clang::driver;
+
+// This function isn't referenced outside its translation unit, but it
+// can't use the "static" keyword because its address is used for
+// GetMainExecutable (since some platforms don't support taking the
+// address of main, and some platforms can't implement GetMainExecutable
+// without being given the address of a function in the main executable).
+std::string GetExecutablePath(const char *Argv0) {
+ // This just needs to be some symbol in the binary; C++ doesn't
+ // allow taking the address of ::main however.
+ void *MainAddr = (void*) (intptr_t) GetExecutablePath;
+ return llvm::sys::fs::getMainExecutable(Argv0, MainAddr);
+}
+
+static llvm::ExecutionEngine *
+createExecutionEngine(std::unique_ptr<llvm::Module> M, std::string *ErrorStr) {
+ return llvm::EngineBuilder(std::move(M))
+ .setEngineKind(llvm::EngineKind::Either)
+ .setErrorStr(ErrorStr)
+ .create();
+}
+
+static int Execute(std::unique_ptr<llvm::Module> Mod, char *const *envp) {
+ llvm::InitializeNativeTarget();
+ llvm::InitializeNativeTargetAsmPrinter();
+
+ llvm::Module &M = *Mod;
+ std::string Error;
+ std::unique_ptr<llvm::ExecutionEngine> EE(
+ createExecutionEngine(std::move(Mod), &Error));
+ if (!EE) {
+ llvm::errs() << "unable to make execution engine: " << Error << "\n";
+ return 255;
+ }
+
+ llvm::Function *EntryFn = M.getFunction("main");
+ if (!EntryFn) {
+ llvm::errs() << "'main' function not found in module.\n";
+ return 255;
+ }
+
+ // FIXME: Support passing arguments.
+ std::vector<std::string> Args;
+ Args.push_back(M.getModuleIdentifier());
+
+ EE->finalizeObject();
+ return EE->runFunctionAsMain(EntryFn, Args, envp);
+}
+
+int main(int argc, const char **argv, char * const *envp) {
+ void *MainAddr = (void*) (intptr_t) GetExecutablePath;
+ std::string Path = GetExecutablePath(argv[0]);
+ IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
+ TextDiagnosticPrinter *DiagClient =
+ new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
+
+ IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
+ DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagClient);
+
+ // Use ELF on windows for now.
+ std::string TripleStr = llvm::sys::getProcessTriple();
+ llvm::Triple T(TripleStr);
+ if (T.isOSBinFormatCOFF())
+ T.setObjectFormat(llvm::Triple::ELF);
+
+ Driver TheDriver(Path, T.str(), Diags);
+ TheDriver.setTitle("clang interpreter");
+ TheDriver.setCheckInputsExist(false);
+
+ // FIXME: This is a hack to try to force the driver to do something we can
+ // recognize. We need to extend the driver library to support this use model
+ // (basically, exactly one input, and the operation mode is hard wired).
+ SmallVector<const char *, 16> Args(argv, argv + argc);
+ Args.push_back("-fsyntax-only");
+ std::unique_ptr<Compilation> C(TheDriver.BuildCompilation(Args));
+ if (!C)
+ return 0;
+
+ // FIXME: This is copied from ASTUnit.cpp; simplify and eliminate.
+
+ // We expect to get back exactly one command job, if we didn't something
+ // failed. Extract that job from the compilation.
+ const driver::JobList &Jobs = C->getJobs();
+ if (Jobs.size() != 1 || !isa<driver::Command>(*Jobs.begin())) {
+ SmallString<256> Msg;
+ llvm::raw_svector_ostream OS(Msg);
+ Jobs.Print(OS, "; ", true);
+ Diags.Report(diag::err_fe_expected_compiler_job) << OS.str();
+ return 1;
+ }
+
+ const driver::Command &Cmd = cast<driver::Command>(*Jobs.begin());
+ if (llvm::StringRef(Cmd.getCreator().getName()) != "clang") {
+ Diags.Report(diag::err_fe_expected_clang_command);
+ return 1;
+ }
+
+ // Initialize a compiler invocation object from the clang (-cc1) arguments.
+ const driver::ArgStringList &CCArgs = Cmd.getArguments();
+ std::unique_ptr<CompilerInvocation> CI(new CompilerInvocation);
+ CompilerInvocation::CreateFromArgs(*CI,
+ const_cast<const char **>(CCArgs.data()),
+ const_cast<const char **>(CCArgs.data()) +
+ CCArgs.size(),
+ Diags);
+
+ // Show the invocation, with -v.
+ if (CI->getHeaderSearchOpts().Verbose) {
+ llvm::errs() << "clang invocation:\n";
+ Jobs.Print(llvm::errs(), "\n", true);
+ llvm::errs() << "\n";
+ }
+
+ // FIXME: This is copied from cc1_main.cpp; simplify and eliminate.
+
+ // Create a compiler instance to handle the actual work.
+ CompilerInstance Clang;
+ Clang.setInvocation(CI.release());
+
+ // Create the compilers actual diagnostics engine.
+ Clang.createDiagnostics();
+ if (!Clang.hasDiagnostics())
+ return 1;
+
+ // Infer the builtin include path if unspecified.
+ if (Clang.getHeaderSearchOpts().UseBuiltinIncludes &&
+ Clang.getHeaderSearchOpts().ResourceDir.empty())
+ Clang.getHeaderSearchOpts().ResourceDir =
+ CompilerInvocation::GetResourcesPath(argv[0], MainAddr);
+
+ // Create and execute the frontend to generate an LLVM bitcode module.
+ std::unique_ptr<CodeGenAction> Act(new EmitLLVMOnlyAction());
+ if (!Clang.ExecuteAction(*Act))
+ return 1;
+
+ int Res = 255;
+ if (std::unique_ptr<llvm::Module> Module = Act->takeModule())
+ Res = Execute(std::move(Module), envp);
+
+ // Shutdown.
+
+ llvm::llvm_shutdown();
+
+ return Res;
+}