Rafael Espindola | 961f1bb | 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 | |
Sam Clegg | 04cf0d7 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 24 | TEST(Disassembler, X86Test) { |
Rafael Espindola | 961f1bb | 2014-11-13 16:52:07 +0000 | [diff] [blame] | 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 | f04ce0e | 2014-11-15 10:53:12 +0000 | [diff] [blame] | 62 | |
| 63 | LLVMDisasmDispose(DCR); |
Rafael Espindola | 961f1bb | 2014-11-13 16:52:07 +0000 | [diff] [blame] | 64 | } |
Sam Clegg | 04cf0d7 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 65 | |
| 66 | TEST(Disassembler, WebAssemblyTest) { |
| 67 | llvm::InitializeAllTargetInfos(); |
| 68 | llvm::InitializeAllTargetMCs(); |
| 69 | llvm::InitializeAllDisassemblers(); |
| 70 | |
| 71 | uint8_t Bytes[] = {0x6a, 0x42, 0x7F, 0x35, 0x01, 0x10}; |
| 72 | uint8_t *BytesP = Bytes; |
| 73 | const char OutStringSize = 100; |
| 74 | char OutString[OutStringSize]; |
Sam Clegg | 2a147a1 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 75 | LLVMDisasmContextRef DCR = LLVMCreateDisasm("wasm32-unknown-unknown", nullptr, |
| 76 | 0, nullptr, symbolLookupCallback); |
Sam Clegg | 04cf0d7 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 77 | if (!DCR) |
| 78 | return; |
| 79 | |
| 80 | size_t InstSize; |
| 81 | unsigned NumBytes = sizeof(Bytes); |
| 82 | unsigned PC = 0; |
| 83 | |
| 84 | InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString, |
| 85 | OutStringSize); |
| 86 | EXPECT_EQ(InstSize, 1U); |
Derek Schuff | f135420 | 2018-06-19 00:02:34 +0000 | [diff] [blame] | 87 | EXPECT_EQ(StringRef(OutString), "\ti32.add "); |
Sam Clegg | 04cf0d7 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 88 | PC += InstSize; |
| 89 | BytesP += InstSize; |
| 90 | NumBytes -= InstSize; |
| 91 | |
| 92 | InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString, |
| 93 | OutStringSize); |
| 94 | EXPECT_EQ(InstSize, 2U); |
Derek Schuff | f135420 | 2018-06-19 00:02:34 +0000 | [diff] [blame] | 95 | EXPECT_EQ(StringRef(OutString), "\ti64.const\t-1"); |
Sam Clegg | 04cf0d7 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 96 | |
| 97 | PC += InstSize; |
| 98 | BytesP += InstSize; |
| 99 | NumBytes -= InstSize; |
| 100 | |
| 101 | InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString, |
| 102 | OutStringSize); |
| 103 | EXPECT_EQ(InstSize, 3U); |
Wouter van Oortmerssen | a26aa42 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 104 | EXPECT_EQ(StringRef(OutString), "\ti64.load32_u\t16:p2align=1"); |
Sam Clegg | 04cf0d7 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 105 | |
| 106 | LLVMDisasmDispose(DCR); |
| 107 | } |