1 //===-- LlvmState.cpp -------------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
11 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/MC/MCCodeEmitter.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCFixup.h"
15 #include "llvm/MC/MCObjectFileInfo.h"
16 #include "llvm/Support/TargetRegistry.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include "llvm/Target/TargetMachine.h"
19 #include "llvm/Target/TargetOptions.h"
24 LLVMState::LLVMState(const std::string
&Triple
, const std::string
&CpuName
,
25 const std::string
&Features
) {
27 const Target
*const TheTarget
= TargetRegistry::lookupTarget(Triple
, Error
);
28 assert(TheTarget
&& "unknown target for host");
29 const TargetOptions Options
;
30 TheTargetMachine
.reset(
31 static_cast<LLVMTargetMachine
*>(TheTarget
->createTargetMachine(
32 Triple
, CpuName
, Features
, Options
, Reloc::Model::Static
)));
33 TheExegesisTarget
= ExegesisTarget::lookup(TheTargetMachine
->getTargetTriple());
34 if (!TheExegesisTarget
) {
35 errs() << "no exegesis target for " << Triple
<< ", using default\n";
36 TheExegesisTarget
= &ExegesisTarget::getDefault();
38 PfmCounters
= &TheExegesisTarget
->getPfmCounters(CpuName
);
40 BitVector ReservedRegs
= getFunctionReservedRegs(getTargetMachine());
41 for (const unsigned Reg
: TheExegesisTarget
->getUnavailableRegisters())
42 ReservedRegs
.set(Reg
);
44 new RegisterAliasingTrackerCache(getRegInfo(), std::move(ReservedRegs
)));
45 IC
.reset(new InstructionsCache(getInstrInfo(), getRATC()));
48 LLVMState::LLVMState(const std::string
&CpuName
)
49 : LLVMState(sys::getProcessTriple(),
50 CpuName
.empty() ? sys::getHostCPUName().str() : CpuName
, "") {}
52 std::unique_ptr
<LLVMTargetMachine
> LLVMState::createTargetMachine() const {
53 return std::unique_ptr
<LLVMTargetMachine
>(static_cast<LLVMTargetMachine
*>(
54 TheTargetMachine
->getTarget().createTargetMachine(
55 TheTargetMachine
->getTargetTriple().normalize(),
56 TheTargetMachine
->getTargetCPU(),
57 TheTargetMachine
->getTargetFeatureString(), TheTargetMachine
->Options
,
58 Reloc::Model::Static
)));
61 bool LLVMState::canAssemble(const MCInst
&Inst
) const {
62 MCObjectFileInfo ObjectFileInfo
;
63 MCContext
Context(TheTargetMachine
->getMCAsmInfo(),
64 TheTargetMachine
->getMCRegisterInfo(), &ObjectFileInfo
);
65 std::unique_ptr
<const MCCodeEmitter
> CodeEmitter(
66 TheTargetMachine
->getTarget().createMCCodeEmitter(
67 *TheTargetMachine
->getMCInstrInfo(), *TheTargetMachine
->getMCRegisterInfo(),
69 SmallVector
<char, 16> Tmp
;
70 raw_svector_ostream
OS(Tmp
);
71 SmallVector
<MCFixup
, 4> Fixups
;
72 CodeEmitter
->encodeInstruction(Inst
, OS
, Fixups
,
73 *TheTargetMachine
->getMCSubtargetInfo());
74 return Tmp
.size() > 0;
77 } // namespace exegesis