OSDN Git Service

Re-land r329156 "Add llvm-exegesis tool."
[android-x86/external-llvm.git] / unittests / tools / llvm-exegesis / InMemoryAssemblerTest.cpp
1 //===-- InMemoryAssemblerTest.cpp -------------------------------*- 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 "InMemoryAssembler.h"
11 #include "X86InstrInfo.h"
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/CodeGen/MachineInstrBuilder.h"
14 #include "llvm/CodeGen/TargetInstrInfo.h"
15 #include "llvm/CodeGen/TargetSubtargetInfo.h"
16 #include "llvm/MC/MCInstBuilder.h"
17 #include "llvm/Support/Host.h"
18 #include "llvm/Support/TargetRegistry.h"
19 #include "llvm/Support/TargetSelect.h"
20 #include "gmock/gmock.h"
21 #include "gtest/gtest.h"
22 #include <memory>
23
24 namespace exegesis {
25 namespace {
26
27 using llvm::MCInstBuilder;
28 using llvm::X86::EAX;
29 using llvm::X86::MOV32ri;
30 using llvm::X86::MOV64ri32;
31 using llvm::X86::RAX;
32 using llvm::X86::XOR32rr;
33 using testing::ElementsAre;
34
35 class MachineFunctionGeneratorTest : public ::testing::Test {
36 protected:
37   MachineFunctionGeneratorTest()
38       : TT(llvm::sys::getProcessTriple()),
39         CpuName(llvm::sys::getHostCPUName().str()) {}
40
41   static void SetUpTestCase() {
42     llvm::InitializeNativeTarget();
43     llvm::InitializeNativeTargetAsmPrinter();
44   }
45
46   std::unique_ptr<llvm::LLVMTargetMachine> createTargetMachine() {
47     std::string Error;
48     const llvm::Target *TheTarget =
49         llvm::TargetRegistry::lookupTarget(TT, Error);
50     assert(TheTarget);
51     const llvm::TargetOptions Options;
52     return std::unique_ptr<llvm::LLVMTargetMachine>(
53         static_cast<llvm::LLVMTargetMachine *>(TheTarget->createTargetMachine(
54             TT, CpuName, "", Options, llvm::Reloc::Model::Static)));
55   }
56
57 private:
58   const std::string TT;
59   const std::string CpuName;
60 };
61
62 TEST_F(MachineFunctionGeneratorTest, JitFunction) {
63   JitFunctionContext Context(createTargetMachine());
64   JitFunction Function(std::move(Context), {});
65   ASSERT_THAT(Function.getFunctionBytes().str(), ElementsAre(0xc3));
66   Function();
67 }
68
69 TEST_F(MachineFunctionGeneratorTest, JitFunctionXOR32rr) {
70   JitFunctionContext Context(createTargetMachine());
71   JitFunction Function(
72       std::move(Context),
73       {MCInstBuilder(XOR32rr).addReg(EAX).addReg(EAX).addReg(EAX)});
74   ASSERT_THAT(Function.getFunctionBytes().str(), ElementsAre(0x31, 0xc0, 0xc3));
75   Function();
76 }
77
78 TEST_F(MachineFunctionGeneratorTest, JitFunctionMOV64ri) {
79   JitFunctionContext Context(createTargetMachine());
80   JitFunction Function(std::move(Context),
81                        {MCInstBuilder(MOV64ri32).addReg(RAX).addImm(42)});
82   ASSERT_THAT(Function.getFunctionBytes().str(),
83               ElementsAre(0x48, 0xc7, 0xc0, 0x2a, 0x00, 0x00, 0x00, 0xc3));
84   Function();
85 }
86
87 TEST_F(MachineFunctionGeneratorTest, JitFunctionMOV32ri) {
88   JitFunctionContext Context(createTargetMachine());
89   JitFunction Function(std::move(Context),
90                        {MCInstBuilder(MOV32ri).addReg(EAX).addImm(42)});
91   ASSERT_THAT(Function.getFunctionBytes().str(),
92               ElementsAre(0xb8, 0x2a, 0x00, 0x00, 0x00, 0xc3));
93   Function();
94 }
95
96 } // namespace
97 } // namespace exegesis