Rafael Espindola | 6c93397 | 2014-11-13 16:52:07 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/Object/Disassembler.cpp ------------------------------===// |
| 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 "llvm-c/Disassembler.h" |
| 11 | #include "llvm/Support/TargetSelect.h" |
| 12 | #include "gtest/gtest.h" |
| 13 | |
| 14 | using namespace llvm; |
| 15 | |
| 16 | static const char *symbolLookupCallback(void *DisInfo, uint64_t ReferenceValue, |
| 17 | uint64_t *ReferenceType, |
| 18 | uint64_t ReferencePC, |
| 19 | const char **ReferenceName) { |
| 20 | *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; |
| 21 | return nullptr; |
| 22 | } |
| 23 | |
| 24 | TEST(Disassembler, Test1) { |
| 25 | llvm::InitializeAllTargetInfos(); |
| 26 | llvm::InitializeAllTargetMCs(); |
| 27 | llvm::InitializeAllDisassemblers(); |
| 28 | |
| 29 | uint8_t Bytes[] = {0x90, 0x90, 0xeb, 0xfd}; |
| 30 | uint8_t *BytesP = Bytes; |
| 31 | const char OutStringSize = 100; |
| 32 | char OutString[OutStringSize]; |
| 33 | LLVMDisasmContextRef DCR = LLVMCreateDisasm("x86_64-pc-linux", nullptr, 0, |
| 34 | nullptr, symbolLookupCallback); |
| 35 | if (!DCR) |
| 36 | return; |
| 37 | |
| 38 | size_t InstSize; |
| 39 | unsigned NumBytes = sizeof(Bytes); |
| 40 | unsigned PC = 0; |
| 41 | |
| 42 | InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString, |
| 43 | OutStringSize); |
| 44 | EXPECT_EQ(InstSize, 1U); |
| 45 | EXPECT_EQ(StringRef(OutString), "\tnop"); |
| 46 | PC += InstSize; |
| 47 | BytesP += InstSize; |
| 48 | NumBytes -= InstSize; |
| 49 | |
| 50 | InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString, |
| 51 | OutStringSize); |
| 52 | EXPECT_EQ(InstSize, 1U); |
| 53 | EXPECT_EQ(StringRef(OutString), "\tnop"); |
| 54 | PC += InstSize; |
| 55 | BytesP += InstSize; |
| 56 | NumBytes -= InstSize; |
| 57 | |
| 58 | InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString, |
| 59 | OutStringSize); |
| 60 | EXPECT_EQ(InstSize, 2U); |
| 61 | EXPECT_EQ(StringRef(OutString), "\tjmp\t0x1"); |
Benjamin Kramer | 5d363ea | 2014-11-15 10:53:12 +0000 | [diff] [blame] | 62 | |
| 63 | LLVMDisasmDispose(DCR); |
Rafael Espindola | 6c93397 | 2014-11-13 16:52:07 +0000 | [diff] [blame] | 64 | } |