summaryrefslogtreecommitdiffstats
path: root/llvm/tools/llvm-exegesis/lib/LlvmState.cpp
blob: cb534f50998d81d1193e484046f5774720e5cefa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//===-- 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 {

LLVMState::LLVMState()
    : TheTriple(llvm::sys::getProcessTriple()),
      CpuName(llvm::sys::getHostCPUName().str()) {
  std::string Error;
  TheTarget = llvm::TargetRegistry::lookupTarget(TheTriple, Error);
  assert(TheTarget && "unknown target for host");
  SubtargetInfo.reset(
      TheTarget->createMCSubtargetInfo(TheTriple, CpuName, Features));
  InstrInfo.reset(TheTarget->createMCInstrInfo());
  RegInfo.reset(TheTarget->createMCRegInfo(TheTriple));
  AsmInfo.reset(TheTarget->createMCAsmInfo(*RegInfo, TheTriple));
}

std::unique_ptr<llvm::LLVMTargetMachine>
LLVMState::createTargetMachine() const {
  const llvm::TargetOptions Options;
  return std::unique_ptr<llvm::LLVMTargetMachine>(
      static_cast<llvm::LLVMTargetMachine *>(TheTarget->createTargetMachine(
          TheTriple, CpuName, Features, Options, llvm::Reloc::Model::Static)));
}

bool LLVMState::canAssemble(const llvm::MCInst &Inst) const {
  llvm::MCObjectFileInfo ObjectFileInfo;
  llvm::MCContext Context(AsmInfo.get(), RegInfo.get(), &ObjectFileInfo);
  std::unique_ptr<const llvm::MCCodeEmitter> CodeEmitter(
      TheTarget->createMCCodeEmitter(*InstrInfo, *RegInfo, Context));
  llvm::SmallVector<char, 16> Tmp;
  llvm::raw_svector_ostream OS(Tmp);
  llvm::SmallVector<llvm::MCFixup, 4> Fixups;
  CodeEmitter->encodeInstruction(Inst, OS, Fixups, *SubtargetInfo);
  return Tmp.size() > 0;
}

} // namespace exegesis
OpenPOWER on IntegriCloud