2014-11-14 00:52:07 +08:00
|
|
|
//===- llvm/unittest/Object/Disassembler.cpp ------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm-c/Disassembler.h"
|
|
|
|
#include "llvm/Support/TargetSelect.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
static const char *symbolLookupCallback(void *DisInfo, uint64_t ReferenceValue,
|
|
|
|
uint64_t *ReferenceType,
|
|
|
|
uint64_t ReferencePC,
|
|
|
|
const char **ReferenceName) {
|
|
|
|
*ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-05-11 06:16:44 +08:00
|
|
|
TEST(Disassembler, X86Test) {
|
2014-11-14 00:52:07 +08:00
|
|
|
llvm::InitializeAllTargetInfos();
|
|
|
|
llvm::InitializeAllTargetMCs();
|
|
|
|
llvm::InitializeAllDisassemblers();
|
|
|
|
|
|
|
|
uint8_t Bytes[] = {0x90, 0x90, 0xeb, 0xfd};
|
|
|
|
uint8_t *BytesP = Bytes;
|
|
|
|
const char OutStringSize = 100;
|
|
|
|
char OutString[OutStringSize];
|
|
|
|
LLVMDisasmContextRef DCR = LLVMCreateDisasm("x86_64-pc-linux", nullptr, 0,
|
|
|
|
nullptr, symbolLookupCallback);
|
|
|
|
if (!DCR)
|
|
|
|
return;
|
|
|
|
|
|
|
|
size_t InstSize;
|
|
|
|
unsigned NumBytes = sizeof(Bytes);
|
|
|
|
unsigned PC = 0;
|
|
|
|
|
|
|
|
InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString,
|
|
|
|
OutStringSize);
|
|
|
|
EXPECT_EQ(InstSize, 1U);
|
|
|
|
EXPECT_EQ(StringRef(OutString), "\tnop");
|
|
|
|
PC += InstSize;
|
|
|
|
BytesP += InstSize;
|
|
|
|
NumBytes -= InstSize;
|
|
|
|
|
|
|
|
InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString,
|
|
|
|
OutStringSize);
|
|
|
|
EXPECT_EQ(InstSize, 1U);
|
|
|
|
EXPECT_EQ(StringRef(OutString), "\tnop");
|
|
|
|
PC += InstSize;
|
|
|
|
BytesP += InstSize;
|
|
|
|
NumBytes -= InstSize;
|
|
|
|
|
|
|
|
InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString,
|
|
|
|
OutStringSize);
|
|
|
|
EXPECT_EQ(InstSize, 2U);
|
|
|
|
EXPECT_EQ(StringRef(OutString), "\tjmp\t0x1");
|
2014-11-15 18:53:12 +08:00
|
|
|
|
|
|
|
LLVMDisasmDispose(DCR);
|
2014-11-14 00:52:07 +08:00
|
|
|
}
|
2018-05-11 06:16:44 +08:00
|
|
|
|
|
|
|
TEST(Disassembler, WebAssemblyTest) {
|
|
|
|
llvm::InitializeAllTargetInfos();
|
|
|
|
llvm::InitializeAllTargetMCs();
|
|
|
|
llvm::InitializeAllDisassemblers();
|
|
|
|
|
|
|
|
uint8_t Bytes[] = {0x6a, 0x42, 0x7F, 0x35, 0x01, 0x10};
|
|
|
|
uint8_t *BytesP = Bytes;
|
|
|
|
const char OutStringSize = 100;
|
|
|
|
char OutString[OutStringSize];
|
2018-07-17 07:09:29 +08:00
|
|
|
LLVMDisasmContextRef DCR = LLVMCreateDisasm("wasm32-unknown-unknown", nullptr,
|
|
|
|
0, nullptr, symbolLookupCallback);
|
2018-05-11 06:16:44 +08:00
|
|
|
if (!DCR)
|
|
|
|
return;
|
|
|
|
|
|
|
|
size_t InstSize;
|
|
|
|
unsigned NumBytes = sizeof(Bytes);
|
|
|
|
unsigned PC = 0;
|
|
|
|
|
|
|
|
InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString,
|
|
|
|
OutStringSize);
|
|
|
|
EXPECT_EQ(InstSize, 1U);
|
2018-06-19 08:02:34 +08:00
|
|
|
EXPECT_EQ(StringRef(OutString), "\ti32.add ");
|
2018-05-11 06:16:44 +08:00
|
|
|
PC += InstSize;
|
|
|
|
BytesP += InstSize;
|
|
|
|
NumBytes -= InstSize;
|
|
|
|
|
|
|
|
InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString,
|
|
|
|
OutStringSize);
|
|
|
|
EXPECT_EQ(InstSize, 2U);
|
2018-06-19 08:02:34 +08:00
|
|
|
EXPECT_EQ(StringRef(OutString), "\ti64.const\t-1");
|
2018-05-11 06:16:44 +08:00
|
|
|
|
|
|
|
PC += InstSize;
|
|
|
|
BytesP += InstSize;
|
|
|
|
NumBytes -= InstSize;
|
|
|
|
|
|
|
|
InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString,
|
|
|
|
OutStringSize);
|
|
|
|
EXPECT_EQ(InstSize, 3U);
|
2018-08-14 07:12:49 +08:00
|
|
|
EXPECT_EQ(StringRef(OutString), "\ti64.load32_u\t16, :p2align=1");
|
2018-05-11 06:16:44 +08:00
|
|
|
|
|
|
|
LLVMDisasmDispose(DCR);
|
|
|
|
}
|