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"
24 LLVMState::LLVMState(const std::string
&Triple
, const std::string
&CpuName
) {
26 const llvm::Target
*const TheTarget
=
27 llvm::TargetRegistry::lookupTarget(Triple
, Error
);
28 assert(TheTarget
&& "unknown target for host");
29 const llvm::TargetOptions Options
;
30 TargetMachine
.reset(static_cast<llvm::LLVMTargetMachine
*>(
31 TheTarget
->createTargetMachine(Triple
, CpuName
, /*Features*/ "", Options
,
32 llvm::Reloc::Model::Static
)));
33 TheExegesisTarget
= ExegesisTarget::lookup(TargetMachine
->getTargetTriple());
34 if (!TheExegesisTarget
) {
35 llvm::errs() << "no exegesis target for " << Triple
<< ", using default\n";
36 TheExegesisTarget
= &ExegesisTarget::getDefault();
38 RATC
.reset(new RegisterAliasingTrackerCache(
39 getRegInfo(), getFunctionReservedRegs(getTargetMachine())));
42 LLVMState::LLVMState()
43 : LLVMState(llvm::sys::getProcessTriple(),
44 llvm::sys::getHostCPUName().str()) {}
46 std::unique_ptr
<llvm::LLVMTargetMachine
>
47 LLVMState::createTargetMachine() const {
48 return std::unique_ptr
<llvm::LLVMTargetMachine
>(
49 static_cast<llvm::LLVMTargetMachine
*>(
50 TargetMachine
->getTarget().createTargetMachine(
51 TargetMachine
->getTargetTriple().normalize(),
52 TargetMachine
->getTargetCPU(),
53 TargetMachine
->getTargetFeatureString(), TargetMachine
->Options
,
54 llvm::Reloc::Model::Static
)));
57 bool LLVMState::canAssemble(const llvm::MCInst
&Inst
) const {
58 llvm::MCObjectFileInfo ObjectFileInfo
;
59 llvm::MCContext
Context(TargetMachine
->getMCAsmInfo(),
60 TargetMachine
->getMCRegisterInfo(), &ObjectFileInfo
);
61 std::unique_ptr
<const llvm::MCCodeEmitter
> CodeEmitter(
62 TargetMachine
->getTarget().createMCCodeEmitter(
63 *TargetMachine
->getMCInstrInfo(), *TargetMachine
->getMCRegisterInfo(),
65 llvm::SmallVector
<char, 16> Tmp
;
66 llvm::raw_svector_ostream
OS(Tmp
);
67 llvm::SmallVector
<llvm::MCFixup
, 4> Fixups
;
68 CodeEmitter
->encodeInstruction(Inst
, OS
, Fixups
,
69 *TargetMachine
->getMCSubtargetInfo());
70 return Tmp
.size() > 0;
73 } // namespace exegesis