blob: 2e8ecd95f6b95267475c11940d13aca1d1e8e13b [file] [log] [blame]
Eric Astor5f6dfa82020-01-20 09:18:25 -05001//===-- llvm-ml.cpp - masm-compatible assembler -----------------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// A simple driver around MasmParser; based on llvm-mc.
10//
11//===----------------------------------------------------------------------===//
12
13#include "Disassembler.h"
14
15#include "llvm/MC/MCAsmBackend.h"
16#include "llvm/MC/MCAsmInfo.h"
17#include "llvm/MC/MCCodeEmitter.h"
18#include "llvm/MC/MCContext.h"
19#include "llvm/MC/MCInstPrinter.h"
20#include "llvm/MC/MCInstrInfo.h"
21#include "llvm/MC/MCObjectFileInfo.h"
22#include "llvm/MC/MCObjectWriter.h"
23#include "llvm/MC/MCParser/AsmLexer.h"
24#include "llvm/MC/MCParser/MCTargetAsmParser.h"
25#include "llvm/MC/MCRegisterInfo.h"
26#include "llvm/MC/MCStreamer.h"
27#include "llvm/MC/MCSubtargetInfo.h"
28#include "llvm/MC/MCTargetOptionsCommandFlags.inc"
29#include "llvm/Support/CommandLine.h"
30#include "llvm/Support/Compression.h"
31#include "llvm/Support/FileUtilities.h"
32#include "llvm/Support/FormattedStream.h"
33#include "llvm/Support/Host.h"
34#include "llvm/Support/InitLLVM.h"
35#include "llvm/Support/MemoryBuffer.h"
36#include "llvm/Support/SourceMgr.h"
37#include "llvm/Support/TargetRegistry.h"
38#include "llvm/Support/TargetSelect.h"
39#include "llvm/Support/ToolOutputFile.h"
40#include "llvm/Support/WithColor.h"
41
42using namespace llvm;
43
44static cl::opt<std::string>
45InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
46
47static cl::opt<std::string>
48OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
49 cl::init("-"));
50
51static cl::opt<bool>
52ShowEncoding("show-encoding", cl::desc("Show instruction encodings"));
53
54static cl::opt<bool>
55ShowInst("show-inst", cl::desc("Show internal instruction representation"));
56
57static cl::opt<bool>
58ShowInstOperands("show-inst-operands",
59 cl::desc("Show instructions operands as parsed"));
60
61static cl::opt<bool>
62OutputATTAsm("output-att-asm", cl::desc("Use ATT syntax for output printing"));
63
64static cl::opt<bool>
65PrintImmHex("print-imm-hex", cl::init(false),
66 cl::desc("Prefer hex format for immediate values"));
67
68static cl::opt<bool>
69PreserveComments("preserve-comments",
70 cl::desc("Preserve Comments in outputted assembly"));
71
72enum OutputFileType {
73 OFT_Null,
74 OFT_AssemblyFile,
75 OFT_ObjectFile
76};
77static cl::opt<OutputFileType>
78FileType("filetype", cl::init(OFT_ObjectFile),
79 cl::desc("Choose an output file type:"),
80 cl::values(
81 clEnumValN(OFT_AssemblyFile, "asm",
82 "Emit an assembly ('.s') file"),
83 clEnumValN(OFT_Null, "null",
84 "Don't emit anything (for timing purposes)"),
85 clEnumValN(OFT_ObjectFile, "obj",
86 "Emit a native object ('.o') file")));
87
88static cl::list<std::string>
89IncludeDirs("I", cl::desc("Directory of include files"),
90 cl::value_desc("directory"), cl::Prefix);
91
92enum BitnessType {
93 m32,
94 m64,
95};
96cl::opt<BitnessType> Bitness(cl::desc("Choose bitness:"), cl::init(m64),
97 cl::values(clEnumVal(m32, "32-bit"),
98 clEnumVal(m64, "64-bit (default)")));
99
100static cl::opt<std::string>
101TripleName("triple", cl::desc("Target triple to assemble for, "
102 "see -version for available targets"));
103
104static cl::opt<std::string>
105DebugCompilationDir("fdebug-compilation-dir",
106 cl::desc("Specifies the debug info's compilation dir"));
107
108static cl::list<std::string>
109DebugPrefixMap("fdebug-prefix-map",
110 cl::desc("Map file source paths in debug info"),
111 cl::value_desc("= separated key-value pairs"));
112
113static cl::opt<std::string>
114MainFileName("main-file-name",
115 cl::desc("Specifies the name we should consider the input file"));
116
117static cl::opt<bool> SaveTempLabels("save-temp-labels",
118 cl::desc("Don't discard temporary labels"));
119
120enum ActionType {
121 AC_AsLex,
122 AC_Assemble,
123 AC_Disassemble,
124 AC_MDisassemble,
125};
126
127static cl::opt<ActionType>
128Action(cl::desc("Action to perform:"),
129 cl::init(AC_Assemble),
130 cl::values(clEnumValN(AC_AsLex, "as-lex",
131 "Lex tokens from a .asm file"),
132 clEnumValN(AC_Assemble, "assemble",
133 "Assemble a .asm file (default)"),
134 clEnumValN(AC_Disassemble, "disassemble",
135 "Disassemble strings of hex bytes"),
136 clEnumValN(AC_MDisassemble, "mdis",
137 "Marked up disassembly of strings of hex bytes")));
138
139static const Target *GetTarget(const char *ProgName) {
140 // Figure out the target triple.
141 if (TripleName.empty()) {
142 if (Bitness == m32)
143 TripleName = "i386-pc-windows";
144 else if (Bitness == m64)
145 TripleName = "x86_64-pc-windows";
146 }
147 Triple TheTriple(Triple::normalize(TripleName));
148
149 // Get the target specific parser.
150 std::string Error;
151 const Target *TheTarget = TargetRegistry::lookupTarget("", TheTriple, Error);
152 if (!TheTarget) {
153 WithColor::error(errs(), ProgName) << Error;
154 return nullptr;
155 }
156
157 // Update the triple name and return the found target.
158 TripleName = TheTriple.getTriple();
159 return TheTarget;
160}
161
162static std::unique_ptr<ToolOutputFile> GetOutputStream(StringRef Path) {
163 std::error_code EC;
164 auto Out = std::make_unique<ToolOutputFile>(Path, EC, sys::fs::F_None);
165 if (EC) {
166 WithColor::error() << EC.message() << '\n';
167 return nullptr;
168 }
169
170 return Out;
171}
172
173static int AsLexInput(SourceMgr &SrcMgr, MCAsmInfo &MAI, raw_ostream &OS) {
174 AsmLexer Lexer(MAI);
175 Lexer.setBuffer(SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer());
176
177 bool Error = false;
178 while (Lexer.Lex().isNot(AsmToken::Eof)) {
179 Lexer.getTok().dump(OS);
180 OS << "\n";
181 if (Lexer.getTok().getKind() == AsmToken::Error)
182 Error = true;
183 }
184
185 return Error;
186}
187
188static int AssembleInput(const char *ProgName, const Target *TheTarget,
189 SourceMgr &SrcMgr, MCContext &Ctx, MCStreamer &Str,
190 MCAsmInfo &MAI, MCSubtargetInfo &STI,
191 MCInstrInfo &MCII, MCTargetOptions &MCOptions) {
Eric Astoree2c0f72020-02-16 12:29:51 -0500192 std::unique_ptr<MCAsmParser> Parser(
193 createMCMasmParser(SrcMgr, Ctx, Str, MAI));
Eric Astor5f6dfa82020-01-20 09:18:25 -0500194 std::unique_ptr<MCTargetAsmParser> TAP(
195 TheTarget->createMCAsmParser(STI, *Parser, MCII, MCOptions));
196
197 if (!TAP) {
198 WithColor::error(errs(), ProgName)
199 << "this target does not support assembly parsing.\n";
200 return 1;
201 }
202
203 Parser->setShowParsedOperands(ShowInstOperands);
204 Parser->setTargetParser(*TAP);
205 Parser->getLexer().setLexMasmIntegers(true);
206
207 int Res = Parser->Run(/*NoInitialTextSection=*/true);
208
209 return Res;
210}
211
212int main(int argc, char **argv) {
213 InitLLVM X(argc, argv);
214
215 // Initialize targets and assembly printers/parsers.
216 llvm::InitializeAllTargetInfos();
217 llvm::InitializeAllTargetMCs();
218 llvm::InitializeAllAsmParsers();
219 llvm::InitializeAllDisassemblers();
220
221 // Register the target printer for --version.
222 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
223
224 cl::ParseCommandLineOptions(argc, argv, "llvm machine code playground\n");
225 MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
Eric Astoree2c0f72020-02-16 12:29:51 -0500226 MCOptions.AssemblyLanguage = "masm";
Eric Astor5f6dfa82020-01-20 09:18:25 -0500227
228 const char *ProgName = argv[0];
229 const Target *TheTarget = GetTarget(ProgName);
230 if (!TheTarget)
231 return 1;
232 // Now that GetTarget() has (potentially) replaced TripleName, it's safe to
233 // construct the Triple object.
234 Triple TheTriple(TripleName);
235
236 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferPtr =
237 MemoryBuffer::getFileOrSTDIN(InputFilename);
238 if (std::error_code EC = BufferPtr.getError()) {
239 WithColor::error(errs(), ProgName)
240 << InputFilename << ": " << EC.message() << '\n';
241 return 1;
242 }
243 MemoryBuffer *Buffer = BufferPtr->get();
244
245 SourceMgr SrcMgr;
246
247 // Tell SrcMgr about this buffer, which is what the parser will pick up.
248 SrcMgr.AddNewSourceBuffer(std::move(*BufferPtr), SMLoc());
249
250 // Record the location of the include directories so that the lexer can find
251 // it later.
252 SrcMgr.setIncludeDirs(IncludeDirs);
253
254 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
255 assert(MRI && "Unable to create target register info!");
256
257 std::unique_ptr<MCAsmInfo> MAI(
258 TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
259 assert(MAI && "Unable to create target asm info!");
260
261 MAI->setPreserveAsmComments(PreserveComments);
262
263 // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
264 // MCObjectFileInfo needs a MCContext reference in order to initialize itself.
265 MCObjectFileInfo MOFI;
266 MCContext Ctx(MAI.get(), MRI.get(), &MOFI, &SrcMgr);
267 MOFI.InitMCObjectFileInfo(TheTriple, /*PIC=*/false, Ctx,
268 /*LargeCodeModel=*/true);
269
270 if (SaveTempLabels)
271 Ctx.setAllowTemporaryLabels(false);
272
273 if (!DebugCompilationDir.empty()) {
274 Ctx.setCompilationDir(DebugCompilationDir);
275 } else {
276 // If no compilation dir is set, try to use the current directory.
277 SmallString<128> CWD;
278 if (!sys::fs::current_path(CWD))
279 Ctx.setCompilationDir(CWD);
280 }
281 for (const auto &Arg : DebugPrefixMap) {
282 const auto &KV = StringRef(Arg).split('=');
Benjamin Krameradcd0262020-01-28 20:23:46 +0100283 Ctx.addDebugPrefixMapEntry(std::string(KV.first), std::string(KV.second));
Eric Astor5f6dfa82020-01-20 09:18:25 -0500284 }
285 if (!MainFileName.empty())
286 Ctx.setMainFileName(MainFileName);
287
288 std::unique_ptr<ToolOutputFile> Out = GetOutputStream(OutputFilename);
289 if (!Out)
290 return 1;
291
292 std::unique_ptr<buffer_ostream> BOS;
293 raw_pwrite_stream *OS = &Out->os();
294 std::unique_ptr<MCStreamer> Str;
295
296 std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
297 std::unique_ptr<MCSubtargetInfo> STI(TheTarget->createMCSubtargetInfo(
298 TripleName, /*CPU=*/"", /*Features=*/""));
299
300 MCInstPrinter *IP = nullptr;
301 if (FileType == OFT_AssemblyFile) {
302 const unsigned OutputAsmVariant = OutputATTAsm ? 0U // ATT dialect
303 : 1U; // Intel dialect
304 IP = TheTarget->createMCInstPrinter(Triple(TripleName), OutputAsmVariant,
305 *MAI, *MCII, *MRI);
306
307 if (!IP) {
308 WithColor::error()
309 << "unable to create instruction printer for target triple '"
310 << TheTriple.normalize() << "' with "
311 << (OutputATTAsm ? "ATT" : "Intel") << " assembly variant.\n";
312 return 1;
313 }
314
315 // Set the display preference for hex vs. decimal immediates.
316 IP->setPrintImmHex(PrintImmHex);
317
318 // Set up the AsmStreamer.
319 std::unique_ptr<MCCodeEmitter> CE;
320 if (ShowEncoding)
321 CE.reset(TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx));
322
323 std::unique_ptr<MCAsmBackend> MAB(
324 TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions));
325 auto FOut = std::make_unique<formatted_raw_ostream>(*OS);
326 Str.reset(
327 TheTarget->createAsmStreamer(Ctx, std::move(FOut), /*asmverbose*/ true,
328 /*useDwarfDirectory*/ true, IP,
329 std::move(CE), std::move(MAB), ShowInst));
330
331 } else if (FileType == OFT_Null) {
332 Str.reset(TheTarget->createNullStreamer(Ctx));
333 } else {
334 assert(FileType == OFT_ObjectFile && "Invalid file type!");
335
336 // Don't waste memory on names of temp labels.
337 Ctx.setUseNamesOnTempLabels(false);
338
339 if (!Out->os().supportsSeeking()) {
340 BOS = std::make_unique<buffer_ostream>(Out->os());
341 OS = BOS.get();
342 }
343
344 MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx);
345 MCAsmBackend *MAB = TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions);
346 Str.reset(TheTarget->createMCObjectStreamer(
347 TheTriple, Ctx, std::unique_ptr<MCAsmBackend>(MAB),
348 MAB->createObjectWriter(*OS), std::unique_ptr<MCCodeEmitter>(CE), *STI,
349 MCOptions.MCRelaxAll, MCOptions.MCIncrementalLinkerCompatible,
350 /*DWARFMustBeAtTheEnd*/ false));
351 }
352
353 // Use Assembler information for parsing.
354 Str->setUseAssemblerInfoForParsing(true);
355
356 int Res = 1;
357 bool disassemble = false;
358 switch (Action) {
359 case AC_AsLex:
360 Res = AsLexInput(SrcMgr, *MAI, Out->os());
361 break;
362 case AC_Assemble:
363 Res = AssembleInput(ProgName, TheTarget, SrcMgr, Ctx, *Str, *MAI, *STI,
364 *MCII, MCOptions);
365 break;
366 case AC_MDisassemble:
367 assert(IP && "Expected assembly output");
368 IP->setUseMarkup(1);
369 disassemble = true;
370 break;
371 case AC_Disassemble:
372 disassemble = true;
373 break;
374 }
375 if (disassemble)
376 Res = Disassembler::disassemble(*TheTarget, TripleName, *STI, *Str, *Buffer,
377 SrcMgr, Out->os());
378
379 // Keep output if no errors.
380 if (Res == 0)
381 Out->keep();
382 return Res;
383}