15f6dfa80SEric Astor //===- Disassembler.cpp - Disassembler for hex strings --------------------===//
25f6dfa80SEric Astor //
35f6dfa80SEric Astor // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f6dfa80SEric Astor // See https://llvm.org/LICENSE.txt for license information.
55f6dfa80SEric Astor // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65f6dfa80SEric Astor //
75f6dfa80SEric Astor //===----------------------------------------------------------------------===//
85f6dfa80SEric Astor //
95f6dfa80SEric Astor // This class implements the disassembler of strings of bytes written in
105f6dfa80SEric Astor // hexadecimal, from standard input or from a file.
115f6dfa80SEric Astor //
125f6dfa80SEric Astor //===----------------------------------------------------------------------===//
135f6dfa80SEric Astor
145f6dfa80SEric Astor #include "Disassembler.h"
155f6dfa80SEric Astor #include "llvm/MC/MCAsmInfo.h"
165f6dfa80SEric Astor #include "llvm/MC/MCContext.h"
175f6dfa80SEric Astor #include "llvm/MC/MCDisassembler/MCDisassembler.h"
185f6dfa80SEric Astor #include "llvm/MC/MCInst.h"
195f6dfa80SEric Astor #include "llvm/MC/MCRegisterInfo.h"
205f6dfa80SEric Astor #include "llvm/MC/MCStreamer.h"
215f6dfa80SEric Astor #include "llvm/MC/MCSubtargetInfo.h"
2289b57061SReid Kleckner #include "llvm/MC/TargetRegistry.h"
235f6dfa80SEric Astor #include "llvm/Support/MemoryBuffer.h"
245f6dfa80SEric Astor #include "llvm/Support/SourceMgr.h"
255f6dfa80SEric Astor #include "llvm/Support/raw_ostream.h"
26*62c7f035SArchibald Elliott #include "llvm/TargetParser/Triple.h"
275f6dfa80SEric Astor
285f6dfa80SEric Astor using namespace llvm;
295f6dfa80SEric Astor
305f6dfa80SEric Astor typedef std::pair<std::vector<unsigned char>, std::vector<const char *>>
315f6dfa80SEric Astor ByteArrayTy;
325f6dfa80SEric Astor
PrintInsts(const MCDisassembler & DisAsm,const ByteArrayTy & Bytes,SourceMgr & SM,raw_ostream & Out,MCStreamer & Streamer,bool InAtomicBlock,const MCSubtargetInfo & STI)335f6dfa80SEric Astor static bool PrintInsts(const MCDisassembler &DisAsm, const ByteArrayTy &Bytes,
345f6dfa80SEric Astor SourceMgr &SM, raw_ostream &Out, MCStreamer &Streamer,
355f6dfa80SEric Astor bool InAtomicBlock, const MCSubtargetInfo &STI) {
365f6dfa80SEric Astor ArrayRef<uint8_t> Data(Bytes.first.data(), Bytes.first.size());
375f6dfa80SEric Astor
385f6dfa80SEric Astor // Disassemble it to strings.
395f6dfa80SEric Astor uint64_t Size;
405f6dfa80SEric Astor uint64_t Index;
415f6dfa80SEric Astor
425f6dfa80SEric Astor for (Index = 0; Index < Bytes.first.size(); Index += Size) {
435f6dfa80SEric Astor MCInst Inst;
445f6dfa80SEric Astor
455f6dfa80SEric Astor MCDisassembler::DecodeStatus S;
465f6dfa80SEric Astor S = DisAsm.getInstruction(Inst, Size, Data.slice(Index), Index, nulls());
475f6dfa80SEric Astor switch (S) {
485f6dfa80SEric Astor case MCDisassembler::Fail:
495f6dfa80SEric Astor SM.PrintMessage(SMLoc::getFromPointer(Bytes.second[Index]),
505f6dfa80SEric Astor SourceMgr::DK_Warning, "invalid instruction encoding");
515f6dfa80SEric Astor // Don't try to resynchronise the stream in a block
525f6dfa80SEric Astor if (InAtomicBlock)
535f6dfa80SEric Astor return true;
545f6dfa80SEric Astor
555f6dfa80SEric Astor if (Size == 0)
565f6dfa80SEric Astor Size = 1; // skip illegible bytes
575f6dfa80SEric Astor
585f6dfa80SEric Astor break;
595f6dfa80SEric Astor
605f6dfa80SEric Astor case MCDisassembler::SoftFail:
615f6dfa80SEric Astor SM.PrintMessage(SMLoc::getFromPointer(Bytes.second[Index]),
625f6dfa80SEric Astor SourceMgr::DK_Warning,
635f6dfa80SEric Astor "potentially undefined instruction encoding");
64de9d80c1SFangrui Song [[fallthrough]];
655f6dfa80SEric Astor
665f6dfa80SEric Astor case MCDisassembler::Success:
67bcd24b2dSFangrui Song Streamer.emitInstruction(Inst, STI);
685f6dfa80SEric Astor break;
695f6dfa80SEric Astor }
705f6dfa80SEric Astor }
715f6dfa80SEric Astor
725f6dfa80SEric Astor return false;
735f6dfa80SEric Astor }
745f6dfa80SEric Astor
SkipToToken(StringRef & Str)755f6dfa80SEric Astor static bool SkipToToken(StringRef &Str) {
765f6dfa80SEric Astor for (;;) {
775f6dfa80SEric Astor if (Str.empty())
785f6dfa80SEric Astor return false;
795f6dfa80SEric Astor
805f6dfa80SEric Astor // Strip horizontal whitespace and commas.
815f6dfa80SEric Astor if (size_t Pos = Str.find_first_not_of(" \t\r\n,")) {
825f6dfa80SEric Astor Str = Str.substr(Pos);
835f6dfa80SEric Astor continue;
845f6dfa80SEric Astor }
855f6dfa80SEric Astor
865f6dfa80SEric Astor // If this is the start of a comment, remove the rest of the line.
875f6dfa80SEric Astor if (Str[0] == '#') {
885f6dfa80SEric Astor Str = Str.substr(Str.find_first_of('\n'));
895f6dfa80SEric Astor continue;
905f6dfa80SEric Astor }
915f6dfa80SEric Astor return true;
925f6dfa80SEric Astor }
935f6dfa80SEric Astor }
945f6dfa80SEric Astor
ByteArrayFromString(ByteArrayTy & ByteArray,StringRef & Str,SourceMgr & SM)955f6dfa80SEric Astor static bool ByteArrayFromString(ByteArrayTy &ByteArray, StringRef &Str,
965f6dfa80SEric Astor SourceMgr &SM) {
975f6dfa80SEric Astor while (SkipToToken(Str)) {
985f6dfa80SEric Astor // Handled by higher level
995f6dfa80SEric Astor if (Str[0] == '[' || Str[0] == ']')
1005f6dfa80SEric Astor return false;
1015f6dfa80SEric Astor
1025f6dfa80SEric Astor // Get the current token.
1035f6dfa80SEric Astor size_t Next = Str.find_first_of(" \t\n\r,#[]");
1045f6dfa80SEric Astor StringRef Value = Str.substr(0, Next);
1055f6dfa80SEric Astor
1065f6dfa80SEric Astor // Convert to a byte and add to the byte vector.
1075f6dfa80SEric Astor unsigned ByteVal;
1085f6dfa80SEric Astor if (Value.getAsInteger(0, ByteVal) || ByteVal > 255) {
1095f6dfa80SEric Astor // If we have an error, print it and skip to the end of line.
1105f6dfa80SEric Astor SM.PrintMessage(SMLoc::getFromPointer(Value.data()), SourceMgr::DK_Error,
1115f6dfa80SEric Astor "invalid input token");
1125f6dfa80SEric Astor Str = Str.substr(Str.find('\n'));
1135f6dfa80SEric Astor ByteArray.first.clear();
1145f6dfa80SEric Astor ByteArray.second.clear();
1155f6dfa80SEric Astor continue;
1165f6dfa80SEric Astor }
1175f6dfa80SEric Astor
1185f6dfa80SEric Astor ByteArray.first.push_back(ByteVal);
1195f6dfa80SEric Astor ByteArray.second.push_back(Value.data());
1205f6dfa80SEric Astor Str = Str.substr(Next);
1215f6dfa80SEric Astor }
1225f6dfa80SEric Astor
1235f6dfa80SEric Astor return false;
1245f6dfa80SEric Astor }
1255f6dfa80SEric Astor
disassemble(const Target & T,const std::string & TripleName,MCSubtargetInfo & STI,MCStreamer & Streamer,MemoryBuffer & Buffer,SourceMgr & SM,raw_ostream & Out)126632ebc4aSPhilipp Krones int Disassembler::disassemble(const Target &T, const std::string &TripleName,
1275f6dfa80SEric Astor MCSubtargetInfo &STI, MCStreamer &Streamer,
1285f6dfa80SEric Astor MemoryBuffer &Buffer, SourceMgr &SM,
1295f6dfa80SEric Astor raw_ostream &Out) {
130632ebc4aSPhilipp Krones std::unique_ptr<const MCRegisterInfo> MRI(T.createMCRegInfo(TripleName));
1315f6dfa80SEric Astor if (!MRI) {
132632ebc4aSPhilipp Krones errs() << "error: no register info for target " << TripleName << "\n";
1335f6dfa80SEric Astor return -1;
1345f6dfa80SEric Astor }
1355f6dfa80SEric Astor
1365f6dfa80SEric Astor MCTargetOptions MCOptions;
1375f6dfa80SEric Astor std::unique_ptr<const MCAsmInfo> MAI(
138632ebc4aSPhilipp Krones T.createMCAsmInfo(*MRI, TripleName, MCOptions));
1395f6dfa80SEric Astor if (!MAI) {
140632ebc4aSPhilipp Krones errs() << "error: no assembly info for target " << TripleName << "\n";
1415f6dfa80SEric Astor return -1;
1425f6dfa80SEric Astor }
1435f6dfa80SEric Astor
1445f6dfa80SEric Astor // Set up the MCContext for creating symbols and MCExpr's.
145c2f819afSPhilipp Krones MCContext Ctx(Triple(TripleName), MAI.get(), MRI.get(), &STI);
1465f6dfa80SEric Astor
1475f6dfa80SEric Astor std::unique_ptr<const MCDisassembler> DisAsm(
1485f6dfa80SEric Astor T.createMCDisassembler(STI, Ctx));
1495f6dfa80SEric Astor if (!DisAsm) {
150632ebc4aSPhilipp Krones errs() << "error: no disassembler for target " << TripleName << "\n";
1515f6dfa80SEric Astor return -1;
1525f6dfa80SEric Astor }
1535f6dfa80SEric Astor
1545f6dfa80SEric Astor // Set up initial section manually here
1555e71839fSPeter Smith Streamer.initSections(false, STI);
1565f6dfa80SEric Astor
1575f6dfa80SEric Astor bool ErrorOccurred = false;
1585f6dfa80SEric Astor
1595f6dfa80SEric Astor // Convert the input to a vector for disassembly.
1605f6dfa80SEric Astor ByteArrayTy ByteArray;
1615f6dfa80SEric Astor StringRef Str = Buffer.getBuffer();
1625f6dfa80SEric Astor bool InAtomicBlock = false;
1635f6dfa80SEric Astor
1645f6dfa80SEric Astor while (SkipToToken(Str)) {
1655f6dfa80SEric Astor ByteArray.first.clear();
1665f6dfa80SEric Astor ByteArray.second.clear();
1675f6dfa80SEric Astor
1685f6dfa80SEric Astor if (Str[0] == '[') {
1695f6dfa80SEric Astor if (InAtomicBlock) {
1705f6dfa80SEric Astor SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
1715f6dfa80SEric Astor "nested atomic blocks make no sense");
1725f6dfa80SEric Astor ErrorOccurred = true;
1735f6dfa80SEric Astor }
1745f6dfa80SEric Astor InAtomicBlock = true;
1755f6dfa80SEric Astor Str = Str.drop_front();
1765f6dfa80SEric Astor continue;
1775f6dfa80SEric Astor } else if (Str[0] == ']') {
1785f6dfa80SEric Astor if (!InAtomicBlock) {
1795f6dfa80SEric Astor SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
1805f6dfa80SEric Astor "attempt to close atomic block without opening");
1815f6dfa80SEric Astor ErrorOccurred = true;
1825f6dfa80SEric Astor }
1835f6dfa80SEric Astor InAtomicBlock = false;
1845f6dfa80SEric Astor Str = Str.drop_front();
1855f6dfa80SEric Astor continue;
1865f6dfa80SEric Astor }
1875f6dfa80SEric Astor
1885f6dfa80SEric Astor // It's a real token, get the bytes and emit them
1895f6dfa80SEric Astor ErrorOccurred |= ByteArrayFromString(ByteArray, Str, SM);
1905f6dfa80SEric Astor
1915f6dfa80SEric Astor if (!ByteArray.first.empty())
1925f6dfa80SEric Astor ErrorOccurred |=
1935f6dfa80SEric Astor PrintInsts(*DisAsm, ByteArray, SM, Out, Streamer, InAtomicBlock, STI);
1945f6dfa80SEric Astor }
1955f6dfa80SEric Astor
1965f6dfa80SEric Astor if (InAtomicBlock) {
1975f6dfa80SEric Astor SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
1985f6dfa80SEric Astor "unclosed atomic block");
1995f6dfa80SEric Astor ErrorOccurred = true;
2005f6dfa80SEric Astor }
2015f6dfa80SEric Astor
2025f6dfa80SEric Astor return ErrorOccurred;
2035f6dfa80SEric Astor }
204