xref: /openbsd-src/gnu/llvm/llvm/tools/llvm-ml/Disassembler.cpp (revision d415bd752c734aee168c4ee86ff32e8cc249eb16)
1097a140dSpatrick //===- Disassembler.cpp - Disassembler for hex strings --------------------===//
2097a140dSpatrick //
3097a140dSpatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4097a140dSpatrick // See https://llvm.org/LICENSE.txt for license information.
5097a140dSpatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6097a140dSpatrick //
7097a140dSpatrick //===----------------------------------------------------------------------===//
8097a140dSpatrick //
9097a140dSpatrick // This class implements the disassembler of strings of bytes written in
10097a140dSpatrick // hexadecimal, from standard input or from a file.
11097a140dSpatrick //
12097a140dSpatrick //===----------------------------------------------------------------------===//
13097a140dSpatrick 
14097a140dSpatrick #include "Disassembler.h"
15097a140dSpatrick #include "llvm/ADT/Triple.h"
16097a140dSpatrick #include "llvm/MC/MCAsmInfo.h"
17097a140dSpatrick #include "llvm/MC/MCContext.h"
18097a140dSpatrick #include "llvm/MC/MCDisassembler/MCDisassembler.h"
19097a140dSpatrick #include "llvm/MC/MCInst.h"
20097a140dSpatrick #include "llvm/MC/MCRegisterInfo.h"
21097a140dSpatrick #include "llvm/MC/MCStreamer.h"
22097a140dSpatrick #include "llvm/MC/MCSubtargetInfo.h"
23*d415bd75Srobert #include "llvm/MC/TargetRegistry.h"
24097a140dSpatrick #include "llvm/Support/MemoryBuffer.h"
25097a140dSpatrick #include "llvm/Support/SourceMgr.h"
26097a140dSpatrick #include "llvm/Support/raw_ostream.h"
27097a140dSpatrick 
28097a140dSpatrick using namespace llvm;
29097a140dSpatrick 
30097a140dSpatrick typedef std::pair<std::vector<unsigned char>, std::vector<const char *>>
31097a140dSpatrick     ByteArrayTy;
32097a140dSpatrick 
PrintInsts(const MCDisassembler & DisAsm,const ByteArrayTy & Bytes,SourceMgr & SM,raw_ostream & Out,MCStreamer & Streamer,bool InAtomicBlock,const MCSubtargetInfo & STI)33097a140dSpatrick static bool PrintInsts(const MCDisassembler &DisAsm, const ByteArrayTy &Bytes,
34097a140dSpatrick                        SourceMgr &SM, raw_ostream &Out, MCStreamer &Streamer,
35097a140dSpatrick                        bool InAtomicBlock, const MCSubtargetInfo &STI) {
36097a140dSpatrick   ArrayRef<uint8_t> Data(Bytes.first.data(), Bytes.first.size());
37097a140dSpatrick 
38097a140dSpatrick   // Disassemble it to strings.
39097a140dSpatrick   uint64_t Size;
40097a140dSpatrick   uint64_t Index;
41097a140dSpatrick 
42097a140dSpatrick   for (Index = 0; Index < Bytes.first.size(); Index += Size) {
43097a140dSpatrick     MCInst Inst;
44097a140dSpatrick 
45097a140dSpatrick     MCDisassembler::DecodeStatus S;
46097a140dSpatrick     S = DisAsm.getInstruction(Inst, Size, Data.slice(Index), Index, nulls());
47097a140dSpatrick     switch (S) {
48097a140dSpatrick     case MCDisassembler::Fail:
49097a140dSpatrick       SM.PrintMessage(SMLoc::getFromPointer(Bytes.second[Index]),
50097a140dSpatrick                       SourceMgr::DK_Warning, "invalid instruction encoding");
51097a140dSpatrick       // Don't try to resynchronise the stream in a block
52097a140dSpatrick       if (InAtomicBlock)
53097a140dSpatrick         return true;
54097a140dSpatrick 
55097a140dSpatrick       if (Size == 0)
56097a140dSpatrick         Size = 1;  // skip illegible bytes
57097a140dSpatrick 
58097a140dSpatrick       break;
59097a140dSpatrick 
60097a140dSpatrick     case MCDisassembler::SoftFail:
61097a140dSpatrick       SM.PrintMessage(SMLoc::getFromPointer(Bytes.second[Index]),
62097a140dSpatrick                       SourceMgr::DK_Warning,
63097a140dSpatrick                       "potentially undefined instruction encoding");
64*d415bd75Srobert       [[fallthrough]];
65097a140dSpatrick 
66097a140dSpatrick     case MCDisassembler::Success:
67097a140dSpatrick       Streamer.emitInstruction(Inst, STI);
68097a140dSpatrick       break;
69097a140dSpatrick     }
70097a140dSpatrick   }
71097a140dSpatrick 
72097a140dSpatrick   return false;
73097a140dSpatrick }
74097a140dSpatrick 
SkipToToken(StringRef & Str)75097a140dSpatrick static bool SkipToToken(StringRef &Str) {
76097a140dSpatrick   for (;;) {
77097a140dSpatrick     if (Str.empty())
78097a140dSpatrick       return false;
79097a140dSpatrick 
80097a140dSpatrick     // Strip horizontal whitespace and commas.
81097a140dSpatrick     if (size_t Pos = Str.find_first_not_of(" \t\r\n,")) {
82097a140dSpatrick       Str = Str.substr(Pos);
83097a140dSpatrick       continue;
84097a140dSpatrick     }
85097a140dSpatrick 
86097a140dSpatrick     // If this is the start of a comment, remove the rest of the line.
87097a140dSpatrick     if (Str[0] == '#') {
88097a140dSpatrick       Str = Str.substr(Str.find_first_of('\n'));
89097a140dSpatrick       continue;
90097a140dSpatrick     }
91097a140dSpatrick     return true;
92097a140dSpatrick   }
93097a140dSpatrick }
94097a140dSpatrick 
ByteArrayFromString(ByteArrayTy & ByteArray,StringRef & Str,SourceMgr & SM)95097a140dSpatrick static bool ByteArrayFromString(ByteArrayTy &ByteArray, StringRef &Str,
96097a140dSpatrick                                 SourceMgr &SM) {
97097a140dSpatrick   while (SkipToToken(Str)) {
98097a140dSpatrick     // Handled by higher level
99097a140dSpatrick     if (Str[0] == '[' || Str[0] == ']')
100097a140dSpatrick       return false;
101097a140dSpatrick 
102097a140dSpatrick     // Get the current token.
103097a140dSpatrick     size_t Next = Str.find_first_of(" \t\n\r,#[]");
104097a140dSpatrick     StringRef Value = Str.substr(0, Next);
105097a140dSpatrick 
106097a140dSpatrick     // Convert to a byte and add to the byte vector.
107097a140dSpatrick     unsigned ByteVal;
108097a140dSpatrick     if (Value.getAsInteger(0, ByteVal) || ByteVal > 255) {
109097a140dSpatrick       // If we have an error, print it and skip to the end of line.
110097a140dSpatrick       SM.PrintMessage(SMLoc::getFromPointer(Value.data()), SourceMgr::DK_Error,
111097a140dSpatrick                       "invalid input token");
112097a140dSpatrick       Str = Str.substr(Str.find('\n'));
113097a140dSpatrick       ByteArray.first.clear();
114097a140dSpatrick       ByteArray.second.clear();
115097a140dSpatrick       continue;
116097a140dSpatrick     }
117097a140dSpatrick 
118097a140dSpatrick     ByteArray.first.push_back(ByteVal);
119097a140dSpatrick     ByteArray.second.push_back(Value.data());
120097a140dSpatrick     Str = Str.substr(Next);
121097a140dSpatrick   }
122097a140dSpatrick 
123097a140dSpatrick   return false;
124097a140dSpatrick }
125097a140dSpatrick 
disassemble(const Target & T,const std::string & TripleName,MCSubtargetInfo & STI,MCStreamer & Streamer,MemoryBuffer & Buffer,SourceMgr & SM,raw_ostream & Out)12673471bf0Spatrick int Disassembler::disassemble(const Target &T, const std::string &TripleName,
127097a140dSpatrick                               MCSubtargetInfo &STI, MCStreamer &Streamer,
128097a140dSpatrick                               MemoryBuffer &Buffer, SourceMgr &SM,
129097a140dSpatrick                               raw_ostream &Out) {
13073471bf0Spatrick   std::unique_ptr<const MCRegisterInfo> MRI(T.createMCRegInfo(TripleName));
131097a140dSpatrick   if (!MRI) {
13273471bf0Spatrick     errs() << "error: no register info for target " << TripleName << "\n";
133097a140dSpatrick     return -1;
134097a140dSpatrick   }
135097a140dSpatrick 
136097a140dSpatrick   MCTargetOptions MCOptions;
137097a140dSpatrick   std::unique_ptr<const MCAsmInfo> MAI(
13873471bf0Spatrick       T.createMCAsmInfo(*MRI, TripleName, MCOptions));
139097a140dSpatrick   if (!MAI) {
14073471bf0Spatrick     errs() << "error: no assembly info for target " << TripleName << "\n";
141097a140dSpatrick     return -1;
142097a140dSpatrick   }
143097a140dSpatrick 
144097a140dSpatrick   // Set up the MCContext for creating symbols and MCExpr's.
14573471bf0Spatrick   MCContext Ctx(Triple(TripleName), MAI.get(), MRI.get(), &STI);
146097a140dSpatrick 
147097a140dSpatrick   std::unique_ptr<const MCDisassembler> DisAsm(
148097a140dSpatrick       T.createMCDisassembler(STI, Ctx));
149097a140dSpatrick   if (!DisAsm) {
15073471bf0Spatrick     errs() << "error: no disassembler for target " << TripleName << "\n";
151097a140dSpatrick     return -1;
152097a140dSpatrick   }
153097a140dSpatrick 
154097a140dSpatrick   // Set up initial section manually here
155*d415bd75Srobert   Streamer.initSections(false, STI);
156097a140dSpatrick 
157097a140dSpatrick   bool ErrorOccurred = false;
158097a140dSpatrick 
159097a140dSpatrick   // Convert the input to a vector for disassembly.
160097a140dSpatrick   ByteArrayTy ByteArray;
161097a140dSpatrick   StringRef Str = Buffer.getBuffer();
162097a140dSpatrick   bool InAtomicBlock = false;
163097a140dSpatrick 
164097a140dSpatrick   while (SkipToToken(Str)) {
165097a140dSpatrick     ByteArray.first.clear();
166097a140dSpatrick     ByteArray.second.clear();
167097a140dSpatrick 
168097a140dSpatrick     if (Str[0] == '[') {
169097a140dSpatrick       if (InAtomicBlock) {
170097a140dSpatrick         SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
171097a140dSpatrick                         "nested atomic blocks make no sense");
172097a140dSpatrick         ErrorOccurred = true;
173097a140dSpatrick       }
174097a140dSpatrick       InAtomicBlock = true;
175097a140dSpatrick       Str = Str.drop_front();
176097a140dSpatrick       continue;
177097a140dSpatrick     } else if (Str[0] == ']') {
178097a140dSpatrick       if (!InAtomicBlock) {
179097a140dSpatrick         SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
180097a140dSpatrick                         "attempt to close atomic block without opening");
181097a140dSpatrick         ErrorOccurred = true;
182097a140dSpatrick       }
183097a140dSpatrick       InAtomicBlock = false;
184097a140dSpatrick       Str = Str.drop_front();
185097a140dSpatrick       continue;
186097a140dSpatrick     }
187097a140dSpatrick 
188097a140dSpatrick     // It's a real token, get the bytes and emit them
189097a140dSpatrick     ErrorOccurred |= ByteArrayFromString(ByteArray, Str, SM);
190097a140dSpatrick 
191097a140dSpatrick     if (!ByteArray.first.empty())
192097a140dSpatrick       ErrorOccurred |=
193097a140dSpatrick           PrintInsts(*DisAsm, ByteArray, SM, Out, Streamer, InAtomicBlock, STI);
194097a140dSpatrick   }
195097a140dSpatrick 
196097a140dSpatrick   if (InAtomicBlock) {
197097a140dSpatrick     SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
198097a140dSpatrick                     "unclosed atomic block");
199097a140dSpatrick     ErrorOccurred = true;
200097a140dSpatrick   }
201097a140dSpatrick 
202097a140dSpatrick   return ErrorOccurred;
203097a140dSpatrick }
204