2018-04-04 19:37:06 +08:00
|
|
|
//===-- InMemoryAssemblerTest.cpp -------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "InMemoryAssembler.h"
|
|
|
|
#include "X86InstrInfo.h"
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/CodeGen/TargetInstrInfo.h"
|
|
|
|
#include "llvm/CodeGen/TargetSubtargetInfo.h"
|
|
|
|
#include "llvm/MC/MCInstBuilder.h"
|
|
|
|
#include "llvm/Support/Host.h"
|
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
#include "llvm/Support/TargetSelect.h"
|
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace exegesis {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
using llvm::MCInstBuilder;
|
|
|
|
using llvm::X86::EAX;
|
|
|
|
using llvm::X86::MOV32ri;
|
|
|
|
using llvm::X86::MOV64ri32;
|
|
|
|
using llvm::X86::RAX;
|
|
|
|
using llvm::X86::XOR32rr;
|
|
|
|
using testing::ElementsAre;
|
|
|
|
|
|
|
|
class MachineFunctionGeneratorTest : public ::testing::Test {
|
|
|
|
protected:
|
|
|
|
MachineFunctionGeneratorTest()
|
|
|
|
: TT(llvm::sys::getProcessTriple()),
|
|
|
|
CpuName(llvm::sys::getHostCPUName().str()) {}
|
|
|
|
|
|
|
|
static void SetUpTestCase() {
|
2018-04-13 20:20:30 +08:00
|
|
|
LLVMInitializeX86TargetInfo();
|
|
|
|
LLVMInitializeX86TargetMC();
|
|
|
|
LLVMInitializeX86Target();
|
|
|
|
LLVMInitializeX86AsmPrinter();
|
2018-04-04 19:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<llvm::LLVMTargetMachine> createTargetMachine() {
|
|
|
|
std::string Error;
|
|
|
|
const llvm::Target *TheTarget =
|
|
|
|
llvm::TargetRegistry::lookupTarget(TT, Error);
|
2018-04-13 22:29:52 +08:00
|
|
|
EXPECT_TRUE(TheTarget) << Error << " " << TT;
|
2018-04-04 19:37:06 +08:00
|
|
|
const llvm::TargetOptions Options;
|
2018-04-13 22:29:52 +08:00
|
|
|
llvm::TargetMachine* TM = TheTarget->createTargetMachine(
|
|
|
|
TT, CpuName, "", Options, llvm::Reloc::Model::Static);
|
|
|
|
EXPECT_TRUE(TM) << TT << " " << CpuName;
|
2018-04-04 19:37:06 +08:00
|
|
|
return std::unique_ptr<llvm::LLVMTargetMachine>(
|
2018-04-13 22:29:52 +08:00
|
|
|
static_cast<llvm::LLVMTargetMachine *>(TM));
|
2018-04-04 19:37:06 +08:00
|
|
|
}
|
|
|
|
|
2018-04-13 20:20:30 +08:00
|
|
|
bool IsSupportedTarget() const {
|
|
|
|
return llvm::StringRef(TT).startswith_lower("x86_64");
|
|
|
|
}
|
|
|
|
|
2018-04-04 19:37:06 +08:00
|
|
|
private:
|
|
|
|
const std::string TT;
|
|
|
|
const std::string CpuName;
|
|
|
|
};
|
|
|
|
|
2018-04-13 20:20:30 +08:00
|
|
|
// Used to skip tests on unsupported architectures and operating systems.
|
|
|
|
// To skip a test, add this macro at the top of a test-case.
|
|
|
|
#define SKIP_UNSUPPORTED_PLATFORM \
|
|
|
|
do \
|
|
|
|
if (!IsSupportedTarget()) \
|
|
|
|
return; \
|
|
|
|
while(0)
|
|
|
|
|
|
|
|
|
2018-04-13 23:19:16 +08:00
|
|
|
TEST_F(MachineFunctionGeneratorTest, DISABLED_JitFunction) {
|
2018-04-13 20:20:30 +08:00
|
|
|
SKIP_UNSUPPORTED_PLATFORM;
|
2018-04-04 19:37:06 +08:00
|
|
|
JitFunctionContext Context(createTargetMachine());
|
|
|
|
JitFunction Function(std::move(Context), {});
|
|
|
|
ASSERT_THAT(Function.getFunctionBytes().str(), ElementsAre(0xc3));
|
2018-04-04 21:33:21 +08:00
|
|
|
// FIXME: Check that the function runs without errors. Right now this is
|
|
|
|
// disabled because it fails on some bots.
|
|
|
|
// Function();
|
2018-04-04 19:37:06 +08:00
|
|
|
}
|
|
|
|
|
2018-04-13 23:19:16 +08:00
|
|
|
TEST_F(MachineFunctionGeneratorTest, DISABLED_JitFunctionXOR32rr) {
|
2018-04-13 20:20:30 +08:00
|
|
|
SKIP_UNSUPPORTED_PLATFORM;
|
2018-04-04 19:37:06 +08:00
|
|
|
JitFunctionContext Context(createTargetMachine());
|
|
|
|
JitFunction Function(
|
|
|
|
std::move(Context),
|
|
|
|
{MCInstBuilder(XOR32rr).addReg(EAX).addReg(EAX).addReg(EAX)});
|
|
|
|
ASSERT_THAT(Function.getFunctionBytes().str(), ElementsAre(0x31, 0xc0, 0xc3));
|
2018-04-04 21:33:21 +08:00
|
|
|
// Function();
|
2018-04-04 19:37:06 +08:00
|
|
|
}
|
|
|
|
|
2018-04-13 23:19:16 +08:00
|
|
|
TEST_F(MachineFunctionGeneratorTest, DISABLED_JitFunctionMOV64ri) {
|
2018-04-13 20:20:30 +08:00
|
|
|
SKIP_UNSUPPORTED_PLATFORM;
|
2018-04-04 19:37:06 +08:00
|
|
|
JitFunctionContext Context(createTargetMachine());
|
|
|
|
JitFunction Function(std::move(Context),
|
|
|
|
{MCInstBuilder(MOV64ri32).addReg(RAX).addImm(42)});
|
|
|
|
ASSERT_THAT(Function.getFunctionBytes().str(),
|
|
|
|
ElementsAre(0x48, 0xc7, 0xc0, 0x2a, 0x00, 0x00, 0x00, 0xc3));
|
2018-04-04 21:33:21 +08:00
|
|
|
// Function();
|
2018-04-04 19:37:06 +08:00
|
|
|
}
|
|
|
|
|
2018-04-13 23:19:16 +08:00
|
|
|
TEST_F(MachineFunctionGeneratorTest, DISABLED_JitFunctionMOV32ri) {
|
2018-04-13 20:20:30 +08:00
|
|
|
SKIP_UNSUPPORTED_PLATFORM;
|
2018-04-04 19:37:06 +08:00
|
|
|
JitFunctionContext Context(createTargetMachine());
|
|
|
|
JitFunction Function(std::move(Context),
|
|
|
|
{MCInstBuilder(MOV32ri).addReg(EAX).addImm(42)});
|
|
|
|
ASSERT_THAT(Function.getFunctionBytes().str(),
|
|
|
|
ElementsAre(0xb8, 0x2a, 0x00, 0x00, 0x00, 0xc3));
|
2018-04-04 21:33:21 +08:00
|
|
|
// Function();
|
2018-04-04 19:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
} // namespace exegesis
|