2010-02-25 16:49:05 +08:00
|
|
|
//===-- examples/clang-interpreter/main.cpp - Clang C Interpreter Example -===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-02-25 16:49:05 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-04 17:37:22 +08:00
|
|
|
#include "clang/Basic/DiagnosticOptions.h"
|
2018-05-26 04:23:42 +08:00
|
|
|
#include "clang/CodeGen/CodeGenAction.h"
|
2010-02-25 16:49:05 +08:00
|
|
|
#include "clang/Driver/Compilation.h"
|
|
|
|
#include "clang/Driver/Driver.h"
|
|
|
|
#include "clang/Driver/Tool.h"
|
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
2012-12-04 17:37:22 +08:00
|
|
|
#include "clang/Frontend/CompilerInvocation.h"
|
2010-02-25 16:49:05 +08:00
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
|
|
|
#include "clang/Frontend/TextDiagnosticPrinter.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ExecutionEngine/ExecutionEngine.h"
|
2018-05-26 04:23:42 +08:00
|
|
|
#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
|
2019-07-19 06:47:18 +08:00
|
|
|
#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
|
2018-05-26 04:23:42 +08:00
|
|
|
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
|
|
|
|
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
|
|
|
|
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/Mangler.h"
|
2013-01-02 19:45:17 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2013-06-26 13:03:40 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2010-11-30 02:12:39 +08:00
|
|
|
#include "llvm/Support/Host.h"
|
2012-12-04 17:37:22 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2010-11-30 02:12:39 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2011-08-25 02:09:14 +08:00
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2012-12-04 17:37:22 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2018-05-26 04:23:42 +08:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2018-03-15 00:04:45 +08:00
|
|
|
|
2010-02-25 16:49:05 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace clang::driver;
|
|
|
|
|
2010-11-18 01:23:53 +08:00
|
|
|
// 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).
|
2018-03-15 00:04:45 +08:00
|
|
|
std::string GetExecutablePath(const char *Argv0, void *MainAddr) {
|
2013-06-26 13:03:40 +08:00
|
|
|
return llvm::sys::fs::getMainExecutable(Argv0, MainAddr);
|
2010-02-25 16:49:05 +08:00
|
|
|
}
|
|
|
|
|
2018-05-26 04:23:42 +08:00
|
|
|
namespace llvm {
|
|
|
|
namespace orc {
|
|
|
|
|
|
|
|
class SimpleJIT {
|
|
|
|
private:
|
|
|
|
ExecutionSession ES;
|
|
|
|
std::unique_ptr<TargetMachine> TM;
|
|
|
|
const DataLayout DL;
|
2019-07-19 06:47:18 +08:00
|
|
|
MangleAndInterner Mangle{ES, DL};
|
|
|
|
RTDyldObjectLinkingLayer ObjectLayer{ES, createMemMgr};
|
|
|
|
IRCompileLayer CompileLayer{ES, ObjectLayer, SimpleCompiler(*TM)};
|
2018-05-26 04:23:42 +08:00
|
|
|
|
2019-07-19 06:47:18 +08:00
|
|
|
static std::unique_ptr<SectionMemoryManager> createMemMgr() {
|
2019-08-15 07:04:18 +08:00
|
|
|
return std::make_unique<SectionMemoryManager>();
|
2019-07-19 06:47:18 +08:00
|
|
|
}
|
|
|
|
|
2019-08-14 09:03:35 +08:00
|
|
|
SimpleJIT(
|
|
|
|
std::unique_ptr<TargetMachine> TM, DataLayout DL,
|
|
|
|
std::unique_ptr<DynamicLibrarySearchGenerator> ProcessSymbolsGenerator)
|
2019-07-19 06:47:18 +08:00
|
|
|
: TM(std::move(TM)), DL(std::move(DL)) {
|
2018-05-26 04:23:42 +08:00
|
|
|
llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
|
2019-08-14 09:03:35 +08:00
|
|
|
ES.getMainJITDylib().addGenerator(std::move(ProcessSymbolsGenerator));
|
2018-05-26 04:23:42 +08:00
|
|
|
}
|
|
|
|
|
2019-07-19 06:47:18 +08:00
|
|
|
public:
|
|
|
|
static Expected<std::unique_ptr<SimpleJIT>> Create() {
|
|
|
|
auto JTMB = JITTargetMachineBuilder::detectHost();
|
|
|
|
if (!JTMB)
|
|
|
|
return JTMB.takeError();
|
|
|
|
|
|
|
|
auto TM = JTMB->createTargetMachine();
|
|
|
|
if (!TM)
|
|
|
|
return TM.takeError();
|
|
|
|
|
|
|
|
auto DL = (*TM)->createDataLayout();
|
2010-02-25 16:49:05 +08:00
|
|
|
|
2019-07-19 06:47:18 +08:00
|
|
|
auto ProcessSymbolsGenerator =
|
|
|
|
DynamicLibrarySearchGenerator::GetForCurrentProcess(
|
|
|
|
DL.getGlobalPrefix());
|
|
|
|
|
|
|
|
if (!ProcessSymbolsGenerator)
|
|
|
|
return ProcessSymbolsGenerator.takeError();
|
|
|
|
|
|
|
|
return std::unique_ptr<SimpleJIT>(new SimpleJIT(
|
|
|
|
std::move(*TM), std::move(DL), std::move(*ProcessSymbolsGenerator)));
|
2018-05-26 04:23:42 +08:00
|
|
|
}
|
|
|
|
|
2019-07-19 06:47:18 +08:00
|
|
|
const TargetMachine &getTargetMachine() const { return *TM; }
|
|
|
|
|
|
|
|
Error addModule(ThreadSafeModule M) {
|
|
|
|
return CompileLayer.add(ES.getMainJITDylib(), std::move(M));
|
2018-05-26 04:23:42 +08:00
|
|
|
}
|
|
|
|
|
2019-07-19 06:47:18 +08:00
|
|
|
Expected<JITEvaluatedSymbol> findSymbol(const StringRef &Name) {
|
|
|
|
return ES.lookup({&ES.getMainJITDylib()}, Mangle(Name));
|
2018-05-26 04:23:42 +08:00
|
|
|
}
|
|
|
|
|
2019-07-19 06:47:18 +08:00
|
|
|
Expected<JITTargetAddress> getSymbolAddress(const StringRef &Name) {
|
|
|
|
auto Sym = findSymbol(Name);
|
|
|
|
if (!Sym)
|
|
|
|
return Sym.takeError();
|
|
|
|
return Sym->getAddress();
|
2018-05-26 04:23:42 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace orc
|
|
|
|
} // end namespace llvm
|
|
|
|
|
2019-07-19 06:47:18 +08:00
|
|
|
llvm::ExitOnError ExitOnErr;
|
|
|
|
|
2018-05-26 04:23:42 +08:00
|
|
|
int main(int argc, const char **argv) {
|
2018-03-15 00:04:45 +08:00
|
|
|
// This just needs to be some symbol in the binary; C++ doesn't
|
|
|
|
// allow taking the address of ::main however.
|
2018-05-26 04:23:42 +08:00
|
|
|
void *MainAddr = (void*) (intptr_t) GetExecutablePath;
|
|
|
|
std::string Path = GetExecutablePath(argv[0], MainAddr);
|
2012-10-24 06:36:49 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
|
2010-08-26 21:48:56 +08:00
|
|
|
TextDiagnosticPrinter *DiagClient =
|
2012-10-24 06:36:49 +08:00
|
|
|
new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
|
2010-02-25 16:49:05 +08:00
|
|
|
|
2012-02-20 22:00:23 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
|
2012-10-24 06:36:49 +08:00
|
|
|
DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagClient);
|
2014-07-25 04:47:42 +08:00
|
|
|
|
2018-03-15 00:04:45 +08:00
|
|
|
const std::string TripleStr = llvm::sys::getProcessTriple();
|
2014-07-25 04:47:42 +08:00
|
|
|
llvm::Triple T(TripleStr);
|
2018-03-15 00:04:45 +08:00
|
|
|
|
|
|
|
// Use ELF on Windows-32 and MingW for now.
|
|
|
|
#ifndef CLANG_INTERPRETER_COFF_FORMAT
|
2014-07-25 04:47:42 +08:00
|
|
|
if (T.isOSBinFormatCOFF())
|
|
|
|
T.setObjectFormat(llvm::Triple::ELF);
|
2018-03-15 00:04:45 +08:00
|
|
|
#endif
|
2018-05-26 04:23:42 +08:00
|
|
|
|
2019-07-19 06:47:18 +08:00
|
|
|
ExitOnErr.setBanner("clang interpreter");
|
|
|
|
|
2014-07-25 04:47:42 +08:00
|
|
|
Driver TheDriver(Path, T.str(), Diags);
|
2010-02-25 16:49:05 +08:00
|
|
|
TheDriver.setTitle("clang interpreter");
|
2014-07-09 09:37:36 +08:00
|
|
|
TheDriver.setCheckInputsExist(false);
|
2010-02-25 16:49:05 +08:00
|
|
|
|
|
|
|
// 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).
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallVector<const char *, 16> Args(argv, argv + argc);
|
2010-02-25 16:49:05 +08:00
|
|
|
Args.push_back("-fsyntax-only");
|
2014-03-09 19:34:25 +08:00
|
|
|
std::unique_ptr<Compilation> C(TheDriver.BuildCompilation(Args));
|
2010-02-25 16:49:05 +08:00
|
|
|
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();
|
2014-10-03 09:08:27 +08:00
|
|
|
if (Jobs.size() != 1 || !isa<driver::Command>(*Jobs.begin())) {
|
2012-02-13 20:32:26 +08:00
|
|
|
SmallString<256> Msg;
|
2010-02-25 16:49:05 +08:00
|
|
|
llvm::raw_svector_ostream OS(Msg);
|
2013-09-13 02:23:34 +08:00
|
|
|
Jobs.Print(OS, "; ", true);
|
2010-02-25 16:49:05 +08:00
|
|
|
Diags.Report(diag::err_fe_expected_compiler_job) << OS.str();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-10-03 09:08:27 +08:00
|
|
|
const driver::Command &Cmd = cast<driver::Command>(*Jobs.begin());
|
2014-09-05 00:04:28 +08:00
|
|
|
if (llvm::StringRef(Cmd.getCreator().getName()) != "clang") {
|
2010-02-25 16:49:05 +08:00
|
|
|
Diags.Report(diag::err_fe_expected_clang_command);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize a compiler invocation object from the clang (-cc1) arguments.
|
2018-10-13 03:14:43 +08:00
|
|
|
const llvm::opt::ArgStringList &CCArgs = Cmd.getArguments();
|
2014-03-09 19:34:25 +08:00
|
|
|
std::unique_ptr<CompilerInvocation> CI(new CompilerInvocation);
|
2019-08-28 10:13:24 +08:00
|
|
|
CompilerInvocation::CreateFromArgs(*CI, CCArgs, Diags);
|
2010-02-25 16:49:05 +08:00
|
|
|
|
|
|
|
// Show the invocation, with -v.
|
|
|
|
if (CI->getHeaderSearchOpts().Verbose) {
|
|
|
|
llvm::errs() << "clang invocation:\n";
|
2013-09-13 02:23:34 +08:00
|
|
|
Jobs.Print(llvm::errs(), "\n", true);
|
2010-02-25 16:49:05 +08:00
|
|
|
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;
|
2017-01-07 03:49:01 +08:00
|
|
|
Clang.setInvocation(std::move(CI));
|
2010-02-25 16:49:05 +08:00
|
|
|
|
|
|
|
// Create the compilers actual diagnostics engine.
|
2013-01-20 09:58:28 +08:00
|
|
|
Clang.createDiagnostics();
|
2010-02-25 16:49:05 +08:00
|
|
|
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.
|
2014-03-09 19:34:25 +08:00
|
|
|
std::unique_ptr<CodeGenAction> Act(new EmitLLVMOnlyAction());
|
2010-02-25 16:49:05 +08:00
|
|
|
if (!Clang.ExecuteAction(*Act))
|
|
|
|
return 1;
|
|
|
|
|
2018-03-15 00:04:45 +08:00
|
|
|
llvm::InitializeNativeTarget();
|
|
|
|
llvm::InitializeNativeTargetAsmPrinter();
|
|
|
|
|
2010-02-25 16:49:05 +08:00
|
|
|
int Res = 255;
|
2019-07-19 06:47:18 +08:00
|
|
|
std::unique_ptr<llvm::LLVMContext> Ctx(Act->takeLLVMContext());
|
2018-05-26 04:23:42 +08:00
|
|
|
std::unique_ptr<llvm::Module> Module = Act->takeModule();
|
|
|
|
|
|
|
|
if (Module) {
|
2019-07-19 06:47:18 +08:00
|
|
|
auto J = ExitOnErr(llvm::orc::SimpleJIT::Create());
|
|
|
|
|
|
|
|
ExitOnErr(J->addModule(
|
|
|
|
llvm::orc::ThreadSafeModule(std::move(Module), std::move(Ctx))));
|
|
|
|
auto Main = (int (*)(...))ExitOnErr(J->getSymbolAddress("main"));
|
2018-05-26 04:23:42 +08:00
|
|
|
Res = Main();
|
2018-03-15 00:04:45 +08:00
|
|
|
}
|
2010-02-25 16:49:05 +08:00
|
|
|
|
|
|
|
// Shutdown.
|
|
|
|
llvm::llvm_shutdown();
|
|
|
|
|
|
|
|
return Res;
|
|
|
|
}
|