xref: /llvm-project/llvm/unittests/tools/llvm-exegesis/Common/AssemblerUtils.h (revision 3bbdea4a44ed1ae39d51fc60d50f522e38f8bcdd)
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 //===----------------------------------------------------------------------===//
9 
10 #include "Assembler.h"
11 #include "llvm/ADT/ArrayRef.h"
12 #include "llvm/CodeGen/MachineInstrBuilder.h"
13 #include "llvm/CodeGen/TargetInstrInfo.h"
14 #include "llvm/CodeGen/TargetSubtargetInfo.h"
15 #include "llvm/MC/MCInstBuilder.h"
16 #include "llvm/Support/Host.h"
17 #include "llvm/Support/TargetRegistry.h"
18 #include "llvm/Support/TargetSelect.h"
19 #include "gmock/gmock.h"
20 #include "gtest/gtest.h"
21 
22 namespace exegesis {
23 
24 class MachineFunctionGeneratorBaseTest : public ::testing::Test {
25 protected:
26   MachineFunctionGeneratorBaseTest(const std::string &TT,
27                                    const std::string &CpuName)
28       : TT(TT), CpuName(CpuName),
29         CanExecute(llvm::Triple(TT).getArch() ==
30                    llvm::Triple(llvm::sys::getProcessTriple()).getArch()) {
31     if (!CanExecute) {
32       llvm::outs() << "Skipping execution, host:"
33                    << llvm::sys::getProcessTriple() << ", target:" << TT
34                    << "\n";
35     }
36   }
37 
38   template <class... Bs> inline void Check(llvm::MCInst MCInst, Bs... Bytes) {
39     ExecutableFunction Function = (MCInst.getOpcode() == 0)
40                                       ? assembleToFunction({})
41                                       : assembleToFunction({MCInst});
42     ASSERT_THAT(Function.getFunctionBytes().str(),
43                 testing::ElementsAre(Bytes...));
44     if (CanExecute)
45       Function();
46   }
47 
48 private:
49   std::unique_ptr<llvm::LLVMTargetMachine> createTargetMachine() {
50     std::string Error;
51     const llvm::Target *TheTarget =
52         llvm::TargetRegistry::lookupTarget(TT, Error);
53     EXPECT_TRUE(TheTarget) << Error << " " << TT;
54     const llvm::TargetOptions Options;
55     llvm::TargetMachine *TM = TheTarget->createTargetMachine(
56         TT, CpuName, "", Options, llvm::Reloc::Model::Static);
57     EXPECT_TRUE(TM) << TT << " " << CpuName;
58     return std::unique_ptr<llvm::LLVMTargetMachine>(
59         static_cast<llvm::LLVMTargetMachine *>(TM));
60   }
61 
62   ExecutableFunction
63   assembleToFunction(llvm::ArrayRef<llvm::MCInst> Instructions) {
64     llvm::SmallString<256> Buffer;
65     llvm::raw_svector_ostream AsmStream(Buffer);
66     assembleToStream(createTargetMachine(), Instructions, AsmStream);
67     return ExecutableFunction(createTargetMachine(),
68                               getObjectFromBuffer(AsmStream.str()));
69   }
70 
71   const std::string TT;
72   const std::string CpuName;
73   const bool CanExecute;
74 };
75 
76 } // namespace exegesis
77