1 //===-- InMemoryAssemblerTest.cpp -------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
27 using llvm::MCInstBuilder;
29 using llvm::X86::MOV32ri;
30 using llvm::X86::MOV64ri32;
32 using llvm::X86::XOR32rr;
33 using testing::ElementsAre;
35 class MachineFunctionGeneratorTest : public ::testing::Test {
37 MachineFunctionGeneratorTest()
38 : TT(llvm::sys::getProcessTriple()),
39 CpuName(llvm::sys::getHostCPUName().str()) {}
41 static void SetUpTestCase() {
42 llvm::InitializeNativeTarget();
43 llvm::InitializeNativeTargetAsmPrinter();
46 std::unique_ptr<llvm::LLVMTargetMachine> createTargetMachine() {
48 const llvm::Target *TheTarget =
49 llvm::TargetRegistry::lookupTarget(TT, Error);
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)));
59 const std::string CpuName;
62 TEST_F(MachineFunctionGeneratorTest, JitFunction) {
63 JitFunctionContext Context(createTargetMachine());
64 JitFunction Function(std::move(Context), {});
65 ASSERT_THAT(Function.getFunctionBytes().str(), ElementsAre(0xc3));
69 TEST_F(MachineFunctionGeneratorTest, JitFunctionXOR32rr) {
70 JitFunctionContext Context(createTargetMachine());
73 {MCInstBuilder(XOR32rr).addReg(EAX).addReg(EAX).addReg(EAX)});
74 ASSERT_THAT(Function.getFunctionBytes().str(), ElementsAre(0x31, 0xc0, 0xc3));
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));
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));
97 } // namespace exegesis