2018-04-04 19:37:06 +08:00
|
|
|
//===-- LlvmState.cpp -------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "LlvmState.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/MC/MCCodeEmitter.h"
|
|
|
|
#include "llvm/MC/MCContext.h"
|
|
|
|
#include "llvm/MC/MCFixup.h"
|
|
|
|
#include "llvm/MC/MCObjectFileInfo.h"
|
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetOptions.h"
|
|
|
|
|
|
|
|
namespace exegesis {
|
|
|
|
|
2018-06-21 22:11:09 +08:00
|
|
|
LLVMState::LLVMState(const std::string &Triple, const std::string &CpuName) {
|
2018-04-04 19:37:06 +08:00
|
|
|
std::string Error;
|
2018-06-21 22:11:09 +08:00
|
|
|
const llvm::Target *const TheTarget =
|
|
|
|
llvm::TargetRegistry::lookupTarget(Triple, Error);
|
2018-04-04 19:37:06 +08:00
|
|
|
assert(TheTarget && "unknown target for host");
|
2018-06-21 22:11:09 +08:00
|
|
|
const llvm::TargetOptions Options;
|
|
|
|
TargetMachine.reset(static_cast<llvm::LLVMTargetMachine *>(
|
|
|
|
TheTarget->createTargetMachine(Triple, CpuName, /*Features*/ "", Options,
|
|
|
|
llvm::Reloc::Model::Static)));
|
2018-04-04 19:37:06 +08:00
|
|
|
}
|
|
|
|
|
2018-06-13 22:07:36 +08:00
|
|
|
LLVMState::LLVMState()
|
|
|
|
: LLVMState(llvm::sys::getProcessTriple(),
|
|
|
|
llvm::sys::getHostCPUName().str()) {}
|
|
|
|
|
2018-04-04 19:37:06 +08:00
|
|
|
std::unique_ptr<llvm::LLVMTargetMachine>
|
|
|
|
LLVMState::createTargetMachine() const {
|
|
|
|
return std::unique_ptr<llvm::LLVMTargetMachine>(
|
2018-06-21 22:11:09 +08:00
|
|
|
static_cast<llvm::LLVMTargetMachine *>(
|
|
|
|
TargetMachine->getTarget().createTargetMachine(
|
|
|
|
TargetMachine->getTargetTriple().normalize(),
|
|
|
|
TargetMachine->getTargetCPU(),
|
|
|
|
TargetMachine->getTargetFeatureString(), TargetMachine->Options,
|
|
|
|
llvm::Reloc::Model::Static)));
|
2018-04-04 19:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LLVMState::canAssemble(const llvm::MCInst &Inst) const {
|
|
|
|
llvm::MCObjectFileInfo ObjectFileInfo;
|
2018-06-21 22:11:09 +08:00
|
|
|
llvm::MCContext Context(TargetMachine->getMCAsmInfo(),
|
|
|
|
TargetMachine->getMCRegisterInfo(), &ObjectFileInfo);
|
2018-04-04 19:37:06 +08:00
|
|
|
std::unique_ptr<const llvm::MCCodeEmitter> CodeEmitter(
|
2018-06-21 22:11:09 +08:00
|
|
|
TargetMachine->getTarget().createMCCodeEmitter(
|
|
|
|
*TargetMachine->getMCInstrInfo(), *TargetMachine->getMCRegisterInfo(),
|
|
|
|
Context));
|
2018-04-04 19:37:06 +08:00
|
|
|
llvm::SmallVector<char, 16> Tmp;
|
|
|
|
llvm::raw_svector_ostream OS(Tmp);
|
|
|
|
llvm::SmallVector<llvm::MCFixup, 4> Fixups;
|
2018-06-21 22:11:09 +08:00
|
|
|
CodeEmitter->encodeInstruction(Inst, OS, Fixups,
|
|
|
|
*TargetMachine->getMCSubtargetInfo());
|
2018-04-04 19:37:06 +08:00
|
|
|
return Tmp.size() > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace exegesis
|