1 //===- llvm/unittest/Object/Disassembler.cpp ------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm-c/Disassembler.h"
10 #include "llvm/Support/TargetSelect.h"
11 #include "gtest/gtest.h"
15 static const char *symbolLookupCallback(void *DisInfo
, uint64_t ReferenceValue
,
16 uint64_t *ReferenceType
,
18 const char **ReferenceName
) {
19 *ReferenceType
= LLVMDisassembler_ReferenceType_InOut_None
;
23 TEST(Disassembler
, X86Test
) {
24 llvm::InitializeAllTargetInfos();
25 llvm::InitializeAllTargetMCs();
26 llvm::InitializeAllDisassemblers();
28 uint8_t Bytes
[] = {0x90, 0x90, 0xeb, 0xfd};
29 uint8_t *BytesP
= Bytes
;
30 const char OutStringSize
= 100;
31 char OutString
[OutStringSize
];
32 LLVMDisasmContextRef DCR
= LLVMCreateDisasm("x86_64-pc-linux", nullptr, 0,
33 nullptr, symbolLookupCallback
);
38 unsigned NumBytes
= sizeof(Bytes
);
42 LLVMDisasmInstruction(DCR
, BytesP
, 0, PC
, OutString
, OutStringSize
);
43 EXPECT_EQ(InstSize
, 0U);
45 InstSize
= LLVMDisasmInstruction(DCR
, BytesP
, NumBytes
, PC
, OutString
,
47 EXPECT_EQ(InstSize
, 1U);
48 EXPECT_EQ(StringRef(OutString
), "\tnop");
53 InstSize
= LLVMDisasmInstruction(DCR
, BytesP
, NumBytes
, PC
, OutString
,
55 EXPECT_EQ(InstSize
, 1U);
56 EXPECT_EQ(StringRef(OutString
), "\tnop");
61 InstSize
= LLVMDisasmInstruction(DCR
, BytesP
, NumBytes
, PC
, OutString
,
63 EXPECT_EQ(InstSize
, 2U);
64 EXPECT_EQ(StringRef(OutString
), "\tjmp\t0x1");
66 LLVMDisasmDispose(DCR
);
69 TEST(Disassembler
, WebAssemblyTest
) {
70 llvm::InitializeAllTargetInfos();
71 llvm::InitializeAllTargetMCs();
72 llvm::InitializeAllDisassemblers();
74 uint8_t Bytes
[] = {0x6a, 0x42, 0x7F, 0x35, 0x01, 0x10};
75 uint8_t *BytesP
= Bytes
;
76 const char OutStringSize
= 100;
77 char OutString
[OutStringSize
];
78 LLVMDisasmContextRef DCR
= LLVMCreateDisasm("wasm32-unknown-unknown", nullptr,
79 0, nullptr, symbolLookupCallback
);
84 unsigned NumBytes
= sizeof(Bytes
);
87 InstSize
= LLVMDisasmInstruction(DCR
, BytesP
, NumBytes
, PC
, OutString
,
89 EXPECT_EQ(InstSize
, 1U);
90 EXPECT_EQ(StringRef(OutString
), "\ti32.add ");
95 InstSize
= LLVMDisasmInstruction(DCR
, BytesP
, NumBytes
, PC
, OutString
,
97 EXPECT_EQ(InstSize
, 2U);
98 EXPECT_EQ(StringRef(OutString
), "\ti64.const\t-1");
102 NumBytes
-= InstSize
;
104 InstSize
= LLVMDisasmInstruction(DCR
, BytesP
, NumBytes
, PC
, OutString
,
106 EXPECT_EQ(InstSize
, 3U);
107 EXPECT_EQ(StringRef(OutString
), "\ti64.load32_u\t16:p2align=1");
109 LLVMDisasmDispose(DCR
);