[llvm-exegesis] Improve Register Setup.
[llvm-core.git] / unittests / tools / llvm-exegesis / Common / AssemblerUtils.h
blobcc00cee58e3ddb8422d633ab399c14d7a0ed6c67
1 //===-- AssemblerUtils.h ----------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_UNITTESTS_TOOLS_LLVMEXEGESIS_ASSEMBLERUTILS_H
11 #define LLVM_UNITTESTS_TOOLS_LLVMEXEGESIS_ASSEMBLERUTILS_H
13 #include "Assembler.h"
14 #include "BenchmarkRunner.h"
15 #include "Target.h"
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/CodeGen/MachineInstrBuilder.h"
18 #include "llvm/CodeGen/TargetInstrInfo.h"
19 #include "llvm/CodeGen/TargetSubtargetInfo.h"
20 #include "llvm/MC/MCInstBuilder.h"
21 #include "llvm/Support/Host.h"
22 #include "llvm/Support/TargetRegistry.h"
23 #include "llvm/Support/TargetSelect.h"
24 #include "gmock/gmock.h"
25 #include "gtest/gtest.h"
27 namespace exegesis {
29 class MachineFunctionGeneratorBaseTest : public ::testing::Test {
30 protected:
31 MachineFunctionGeneratorBaseTest(const std::string &TT,
32 const std::string &CpuName)
33 : TT(TT), CpuName(CpuName),
34 CanExecute(llvm::Triple(TT).getArch() ==
35 llvm::Triple(llvm::sys::getProcessTriple()).getArch()),
36 ET(ExegesisTarget::lookup(llvm::Triple(TT))) {
37 assert(ET);
38 if (!CanExecute) {
39 llvm::outs() << "Skipping execution, host:"
40 << llvm::sys::getProcessTriple() << ", target:" << TT
41 << "\n";
45 template <class... Bs>
46 inline void Check(llvm::ArrayRef<RegisterValue> RegisterInitialValues,
47 llvm::MCInst MCInst, Bs... Bytes) {
48 ExecutableFunction Function =
49 (MCInst.getOpcode() == 0)
50 ? assembleToFunction(RegisterInitialValues, {})
51 : assembleToFunction(RegisterInitialValues, {MCInst});
52 ASSERT_THAT(Function.getFunctionBytes().str(),
53 testing::ElementsAre(Bytes...));
54 if (CanExecute) {
55 BenchmarkRunner::ScratchSpace Scratch;
56 Function(Scratch.ptr());
60 private:
61 std::unique_ptr<llvm::LLVMTargetMachine> createTargetMachine() {
62 std::string Error;
63 const llvm::Target *TheTarget =
64 llvm::TargetRegistry::lookupTarget(TT, Error);
65 EXPECT_TRUE(TheTarget) << Error << " " << TT;
66 const llvm::TargetOptions Options;
67 llvm::TargetMachine *TM = TheTarget->createTargetMachine(
68 TT, CpuName, "", Options, llvm::Reloc::Model::Static);
69 EXPECT_TRUE(TM) << TT << " " << CpuName;
70 return std::unique_ptr<llvm::LLVMTargetMachine>(
71 static_cast<llvm::LLVMTargetMachine *>(TM));
74 ExecutableFunction
75 assembleToFunction(llvm::ArrayRef<RegisterValue> RegisterInitialValues,
76 llvm::ArrayRef<llvm::MCInst> Instructions) {
77 llvm::SmallString<256> Buffer;
78 llvm::raw_svector_ostream AsmStream(Buffer);
79 assembleToStream(*ET, createTargetMachine(), /*LiveIns=*/{},
80 RegisterInitialValues, Instructions, AsmStream);
81 return ExecutableFunction(createTargetMachine(),
82 getObjectFromBuffer(AsmStream.str()));
85 const std::string TT;
86 const std::string CpuName;
87 const bool CanExecute;
88 const ExegesisTarget *const ET;
91 } // namespace exegesis
93 #endif