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
);
41 InstSize
= LLVMDisasmInstruction(DCR
, BytesP
, NumBytes
, PC
, OutString
,
43 EXPECT_EQ(InstSize
, 1U);
44 EXPECT_EQ(StringRef(OutString
), "\tnop");
49 InstSize
= LLVMDisasmInstruction(DCR
, BytesP
, NumBytes
, PC
, OutString
,
51 EXPECT_EQ(InstSize
, 1U);
52 EXPECT_EQ(StringRef(OutString
), "\tnop");
57 InstSize
= LLVMDisasmInstruction(DCR
, BytesP
, NumBytes
, PC
, OutString
,
59 EXPECT_EQ(InstSize
, 2U);
60 EXPECT_EQ(StringRef(OutString
), "\tjmp\t0x1");
62 LLVMDisasmDispose(DCR
);
65 TEST(Disassembler
, WebAssemblyTest
) {
66 llvm::InitializeAllTargetInfos();
67 llvm::InitializeAllTargetMCs();
68 llvm::InitializeAllDisassemblers();
70 uint8_t Bytes
[] = {0x6a, 0x42, 0x7F, 0x35, 0x01, 0x10};
71 uint8_t *BytesP
= Bytes
;
72 const char OutStringSize
= 100;
73 char OutString
[OutStringSize
];
74 LLVMDisasmContextRef DCR
= LLVMCreateDisasm("wasm32-unknown-unknown", nullptr,
75 0, nullptr, symbolLookupCallback
);
80 unsigned NumBytes
= sizeof(Bytes
);
83 InstSize
= LLVMDisasmInstruction(DCR
, BytesP
, NumBytes
, PC
, OutString
,
85 EXPECT_EQ(InstSize
, 1U);
86 EXPECT_EQ(StringRef(OutString
), "\ti32.add ");
91 InstSize
= LLVMDisasmInstruction(DCR
, BytesP
, NumBytes
, PC
, OutString
,
93 EXPECT_EQ(InstSize
, 2U);
94 EXPECT_EQ(StringRef(OutString
), "\ti64.const\t-1");
100 InstSize
= LLVMDisasmInstruction(DCR
, BytesP
, NumBytes
, PC
, OutString
,
102 EXPECT_EQ(InstSize
, 3U);
103 EXPECT_EQ(StringRef(OutString
), "\ti64.load32_u\t16:p2align=1");
105 LLVMDisasmDispose(DCR
);