summaryrefslogtreecommitdiff
path: root/unittests/MC
diff options
context:
space:
mode:
Diffstat (limited to 'unittests/MC')
-rw-r--r--unittests/MC/CMakeLists.txt5
-rw-r--r--unittests/MC/Disassembler.cpp64
-rw-r--r--unittests/MC/MCAtomTest.cpp31
-rw-r--r--unittests/MC/Makefile2
-rw-r--r--unittests/MC/StringTableBuilderTest.cpp35
5 files changed, 101 insertions, 36 deletions
diff --git a/unittests/MC/CMakeLists.txt b/unittests/MC/CMakeLists.txt
index 37543f48bcef..f83eaf4779f9 100644
--- a/unittests/MC/CMakeLists.txt
+++ b/unittests/MC/CMakeLists.txt
@@ -1,11 +1,12 @@
set(LLVM_LINK_COMPONENTS
+ ${LLVM_TARGETS_TO_BUILD}
MC
- MCAnalysis
+ MCDisassembler
Support
)
add_llvm_unittest(MCTests
- MCAtomTest.cpp
+ Disassembler.cpp
StringTableBuilderTest.cpp
YAMLTest.cpp
)
diff --git a/unittests/MC/Disassembler.cpp b/unittests/MC/Disassembler.cpp
new file mode 100644
index 000000000000..dd0f1ef9ace7
--- /dev/null
+++ b/unittests/MC/Disassembler.cpp
@@ -0,0 +1,64 @@
+//===- llvm/unittest/Object/Disassembler.cpp ------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm-c/Disassembler.h"
+#include "llvm/Support/TargetSelect.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+
+static const char *symbolLookupCallback(void *DisInfo, uint64_t ReferenceValue,
+ uint64_t *ReferenceType,
+ uint64_t ReferencePC,
+ const char **ReferenceName) {
+ *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
+ return nullptr;
+}
+
+TEST(Disassembler, Test1) {
+ llvm::InitializeAllTargetInfos();
+ llvm::InitializeAllTargetMCs();
+ llvm::InitializeAllDisassemblers();
+
+ uint8_t Bytes[] = {0x90, 0x90, 0xeb, 0xfd};
+ uint8_t *BytesP = Bytes;
+ const char OutStringSize = 100;
+ char OutString[OutStringSize];
+ LLVMDisasmContextRef DCR = LLVMCreateDisasm("x86_64-pc-linux", nullptr, 0,
+ nullptr, symbolLookupCallback);
+ if (!DCR)
+ return;
+
+ size_t InstSize;
+ unsigned NumBytes = sizeof(Bytes);
+ unsigned PC = 0;
+
+ InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString,
+ OutStringSize);
+ EXPECT_EQ(InstSize, 1U);
+ EXPECT_EQ(StringRef(OutString), "\tnop");
+ PC += InstSize;
+ BytesP += InstSize;
+ NumBytes -= InstSize;
+
+ InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString,
+ OutStringSize);
+ EXPECT_EQ(InstSize, 1U);
+ EXPECT_EQ(StringRef(OutString), "\tnop");
+ PC += InstSize;
+ BytesP += InstSize;
+ NumBytes -= InstSize;
+
+ InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString,
+ OutStringSize);
+ EXPECT_EQ(InstSize, 2U);
+ EXPECT_EQ(StringRef(OutString), "\tjmp\t0x1");
+
+ LLVMDisasmDispose(DCR);
+}
diff --git a/unittests/MC/MCAtomTest.cpp b/unittests/MC/MCAtomTest.cpp
deleted file mode 100644
index 16228b521f42..000000000000
--- a/unittests/MC/MCAtomTest.cpp
+++ /dev/null
@@ -1,31 +0,0 @@
-//===- llvm/unittest/MC/MCAtomTest.cpp - Instructions unit tests ----------===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-
-#include "llvm/MC/MCAnalysis/MCAtom.h"
-#include "llvm/MC/MCAnalysis/MCModule.h"
-#include "gtest/gtest.h"
-
-namespace llvm {
-namespace {
-
-TEST(MCAtomTest, MCDataSize) {
- MCModule M;
- MCDataAtom *Atom = M.createDataAtom(0, 0);
- EXPECT_EQ(uint64_t(0), Atom->getEndAddr());
- Atom->addData(0);
- EXPECT_EQ(uint64_t(0), Atom->getEndAddr());
- Atom->addData(1);
- EXPECT_EQ(uint64_t(1), Atom->getEndAddr());
- Atom->addData(2);
- EXPECT_EQ(uint64_t(2), Atom->getEndAddr());
- EXPECT_EQ(size_t(3), Atom->getData().size());
-}
-
-} // end anonymous namespace
-} // end namespace llvm
diff --git a/unittests/MC/Makefile b/unittests/MC/Makefile
index 07a608e65a34..3f8d1ef9555c 100644
--- a/unittests/MC/Makefile
+++ b/unittests/MC/Makefile
@@ -9,7 +9,7 @@
LEVEL = ../..
TESTNAME = MC
-LINK_COMPONENTS := MCAnalysis
+LINK_COMPONENTS := all-targets MCDisassembler Object
include $(LEVEL)/Makefile.config
include $(LLVM_SRC_ROOT)/unittests/Makefile.unittest
diff --git a/unittests/MC/StringTableBuilderTest.cpp b/unittests/MC/StringTableBuilderTest.cpp
index d30dc6222d23..716b9c7ae879 100644
--- a/unittests/MC/StringTableBuilderTest.cpp
+++ b/unittests/MC/StringTableBuilderTest.cpp
@@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//
#include "llvm/MC/StringTableBuilder.h"
+#include "llvm/Support/Endian.h"
#include "gtest/gtest.h"
#include <string>
@@ -15,14 +16,14 @@ using namespace llvm;
namespace {
-TEST(StringTableBuilderTest, Basic) {
+TEST(StringTableBuilderTest, BasicELF) {
StringTableBuilder B;
B.add("foo");
B.add("bar");
B.add("foobar");
- B.finalize();
+ B.finalize(StringTableBuilder::ELF);
std::string Expected;
Expected += '\x00';
@@ -37,4 +38,34 @@ TEST(StringTableBuilderTest, Basic) {
EXPECT_EQ(8U, B.getOffset("foo"));
}
+TEST(StringTableBuilderTest, BasicWinCOFF) {
+ StringTableBuilder B;
+
+ // Strings must be 9 chars or longer to go in the table.
+ B.add("hippopotamus");
+ B.add("pygmy hippopotamus");
+ B.add("river horse");
+
+ B.finalize(StringTableBuilder::WinCOFF);
+
+ // size_field + "pygmy hippopotamus\0" + "river horse\0"
+ uint32_t ExpectedSize = 4 + 19 + 12;
+ EXPECT_EQ(ExpectedSize, B.data().size());
+
+ std::string Expected;
+
+ ExpectedSize =
+ support::endian::byte_swap<uint32_t, support::little>(ExpectedSize);
+ Expected.append((const char*)&ExpectedSize, 4);
+ Expected += "pygmy hippopotamus";
+ Expected += '\x00';
+ Expected += "river horse";
+ Expected += '\x00';
+
+ EXPECT_EQ(Expected, B.data());
+ EXPECT_EQ(4U, B.getOffset("pygmy hippopotamus"));
+ EXPECT_EQ(10U, B.getOffset("hippopotamus"));
+ EXPECT_EQ(23U, B.getOffset("river horse"));
+}
+
}