1 //===-- LlvmState.cpp -------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "LlvmState.h"
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/MC/MCCodeEmitter.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCFixup.h"
16 #include "llvm/MC/MCObjectFileInfo.h"
17 #include "llvm/Support/TargetRegistry.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include "llvm/Target/TargetMachine.h"
20 #include "llvm/Target/TargetOptions.h"
25 LLVMState::LLVMState(const std::string
&Triple
, const std::string
&CpuName
) {
27 const llvm::Target
*const TheTarget
=
28 llvm::TargetRegistry::lookupTarget(Triple
, Error
);
29 assert(TheTarget
&& "unknown target for host");
30 const llvm::TargetOptions Options
;
31 TargetMachine
.reset(static_cast<llvm::LLVMTargetMachine
*>(
32 TheTarget
->createTargetMachine(Triple
, CpuName
, /*Features*/ "", Options
,
33 llvm::Reloc::Model::Static
)));
34 TheExegesisTarget
= ExegesisTarget::lookup(TargetMachine
->getTargetTriple());
35 if (!TheExegesisTarget
) {
36 llvm::errs() << "no exegesis target for " << Triple
<< ", using default\n";
37 TheExegesisTarget
= &ExegesisTarget::getDefault();
39 RATC
.reset(new RegisterAliasingTrackerCache(
40 getRegInfo(), getFunctionReservedRegs(getTargetMachine())));
41 IC
.reset(new InstructionsCache(getInstrInfo(), getRATC()));
44 LLVMState::LLVMState()
45 : LLVMState(llvm::sys::getProcessTriple(),
46 llvm::sys::getHostCPUName().str()) {}
48 std::unique_ptr
<llvm::LLVMTargetMachine
>
49 LLVMState::createTargetMachine() const {
50 return std::unique_ptr
<llvm::LLVMTargetMachine
>(
51 static_cast<llvm::LLVMTargetMachine
*>(
52 TargetMachine
->getTarget().createTargetMachine(
53 TargetMachine
->getTargetTriple().normalize(),
54 TargetMachine
->getTargetCPU(),
55 TargetMachine
->getTargetFeatureString(), TargetMachine
->Options
,
56 llvm::Reloc::Model::Static
)));
59 bool LLVMState::canAssemble(const llvm::MCInst
&Inst
) const {
60 llvm::MCObjectFileInfo ObjectFileInfo
;
61 llvm::MCContext
Context(TargetMachine
->getMCAsmInfo(),
62 TargetMachine
->getMCRegisterInfo(), &ObjectFileInfo
);
63 std::unique_ptr
<const llvm::MCCodeEmitter
> CodeEmitter(
64 TargetMachine
->getTarget().createMCCodeEmitter(
65 *TargetMachine
->getMCInstrInfo(), *TargetMachine
->getMCRegisterInfo(),
67 llvm::SmallVector
<char, 16> Tmp
;
68 llvm::raw_svector_ostream
OS(Tmp
);
69 llvm::SmallVector
<llvm::MCFixup
, 4> Fixups
;
70 CodeEmitter
->encodeInstruction(Inst
, OS
, Fixups
,
71 *TargetMachine
->getMCSubtargetInfo());
72 return Tmp
.size() > 0;
75 } // namespace exegesis