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"
|
|
|
|
#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::shared_ptr<SymbolResolver> Resolver;
|
|
|
|
std::unique_ptr<TargetMachine> TM;
|
|
|
|
const DataLayout DL;
|
2018-10-16 17:21:58 +08:00
|
|
|
LegacyRTDyldObjectLinkingLayer ObjectLayer;
|
|
|
|
LegacyIRCompileLayer<decltype(ObjectLayer), SimpleCompiler> CompileLayer;
|
2018-05-26 04:23:42 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
SimpleJIT()
|
|
|
|
: Resolver(createLegacyLookupResolver(
|
|
|
|
ES,
|
|
|
|
[this](const std::string &Name) -> JITSymbol {
|
|
|
|
if (auto Sym = CompileLayer.findSymbol(Name, false))
|
|
|
|
return Sym;
|
|
|
|
else if (auto Err = Sym.takeError())
|
|
|
|
return std::move(Err);
|
|
|
|
if (auto SymAddr =
|
|
|
|
RTDyldMemoryManager::getSymbolAddressInProcess(Name))
|
|
|
|
return JITSymbol(SymAddr, JITSymbolFlags::Exported);
|
|
|
|
return nullptr;
|
|
|
|
},
|
|
|
|
[](Error Err) { cantFail(std::move(Err), "lookupFlags failed"); })),
|
|
|
|
TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
|
|
|
|
ObjectLayer(ES,
|
|
|
|
[this](VModuleKey) {
|
2018-10-16 17:21:58 +08:00
|
|
|
return LegacyRTDyldObjectLinkingLayer::Resources{
|
2018-05-26 04:23:42 +08:00
|
|
|
std::make_shared<SectionMemoryManager>(), Resolver};
|
|
|
|
}),
|
|
|
|
CompileLayer(ObjectLayer, SimpleCompiler(*TM)) {
|
|
|
|
llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
const TargetMachine &getTargetMachine() const { return *TM; }
|
2010-02-25 16:49:05 +08:00
|
|
|
|
2018-05-26 04:23:42 +08:00
|
|
|
VModuleKey addModule(std::unique_ptr<Module> M) {
|
|
|
|
// Add the module to the JIT with a new VModuleKey.
|
|
|
|
auto K = ES.allocateVModule();
|
|
|
|
cantFail(CompileLayer.addModule(K, std::move(M)));
|
|
|
|
return K;
|
|
|
|
}
|
|
|
|
|
|
|
|
JITSymbol findSymbol(const StringRef &Name) {
|
|
|
|
std::string MangledName;
|
|
|
|
raw_string_ostream MangledNameStream(MangledName);
|
|
|
|
Mangler::getNameWithPrefix(MangledNameStream, Name, DL);
|
|
|
|
return CompileLayer.findSymbol(MangledNameStream.str(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
JITTargetAddress getSymbolAddress(const StringRef &Name) {
|
|
|
|
return cantFail(findSymbol(Name).getAddress());
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeModule(VModuleKey K) {
|
|
|
|
cantFail(CompileLayer.removeModule(K));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace orc
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
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
|
|
|
|
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);
|
2010-04-20 19:50:39 +08:00
|
|
|
CompilerInvocation::CreateFromArgs(*CI,
|
|
|
|
const_cast<const char **>(CCArgs.data()),
|
|
|
|
const_cast<const char **>(CCArgs.data()) +
|
|
|
|
CCArgs.size(),
|
2010-04-20 19:55:38 +08:00
|
|
|
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;
|
2018-05-26 04:23:42 +08:00
|
|
|
std::unique_ptr<llvm::Module> Module = Act->takeModule();
|
|
|
|
|
|
|
|
if (Module) {
|
|
|
|
llvm::orc::SimpleJIT J;
|
|
|
|
auto H = J.addModule(std::move(Module));
|
|
|
|
auto Main = (int(*)(...))J.getSymbolAddress("main");
|
|
|
|
Res = Main();
|
|
|
|
J.removeModule(H);
|
2018-03-15 00:04:45 +08:00
|
|
|
}
|
2010-02-25 16:49:05 +08:00
|
|
|
|
|
|
|
// Shutdown.
|
|
|
|
llvm::llvm_shutdown();
|
|
|
|
|
|
|
|
return Res;
|
|
|
|
}
|