blob: ca9581af83a01ee6a8c7fbcc9efaa74bd04126a5 [file] [log] [blame]
Rafael Espindola961f1bb2014-11-13 16:52:07 +00001//===- 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
14using namespace llvm;
15
16static 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 Clegg04cf0d72018-05-10 22:16:44 +000024TEST(Disassembler, X86Test) {
Rafael Espindola961f1bb2014-11-13 16:52:07 +000025 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 Kramerf04ce0e2014-11-15 10:53:12 +000062
63 LLVMDisasmDispose(DCR);
Rafael Espindola961f1bb2014-11-13 16:52:07 +000064}
Sam Clegg04cf0d72018-05-10 22:16:44 +000065
66TEST(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 Clegg2a147a12018-07-16 23:09:29 +000075 LLVMDisasmContextRef DCR = LLVMCreateDisasm("wasm32-unknown-unknown", nullptr,
76 0, nullptr, symbolLookupCallback);
Sam Clegg04cf0d72018-05-10 22:16:44 +000077 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 Schufff1354202018-06-19 00:02:34 +000087 EXPECT_EQ(StringRef(OutString), "\ti32.add ");
Sam Clegg04cf0d72018-05-10 22:16:44 +000088 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 Schufff1354202018-06-19 00:02:34 +000095 EXPECT_EQ(StringRef(OutString), "\ti64.const\t-1");
Sam Clegg04cf0d72018-05-10 22:16:44 +000096
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 Oortmerssena26aa422018-08-27 15:45:51 +0000104 EXPECT_EQ(StringRef(OutString), "\ti64.load32_u\t16:p2align=1");
Sam Clegg04cf0d72018-05-10 22:16:44 +0000105
106 LLVMDisasmDispose(DCR);
107}