1//===-- llvm-mc.cpp - Machine Code Hacking Driver ---------------*- 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// This utility is a simple driver that allows command line hacking on machine
10// code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Disassembler.h"
15#include "llvm/ADT/ScopeExit.h"
16#include "llvm/DWARFCFIChecker/DWARFCFIFunctionFrameAnalyzer.h"
17#include "llvm/DWARFCFIChecker/DWARFCFIFunctionFrameStreamer.h"
18#include "llvm/MC/MCAsmBackend.h"
19#include "llvm/MC/MCAsmInfo.h"
20#include "llvm/MC/MCCodeEmitter.h"
21#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCInstPrinter.h"
23#include "llvm/MC/MCInstrInfo.h"
24#include "llvm/MC/MCLFI.h"
25#include "llvm/MC/MCObjectFileInfo.h"
26#include "llvm/MC/MCObjectWriter.h"
27#include "llvm/MC/MCParser/AsmLexer.h"
28#include "llvm/MC/MCParser/MCTargetAsmParser.h"
29#include "llvm/MC/MCRegisterInfo.h"
30#include "llvm/MC/MCStreamer.h"
31#include "llvm/MC/MCSubtargetInfo.h"
32#include "llvm/MC/MCTargetOptionsCommandFlags.h"
33#include "llvm/MC/TargetRegistry.h"
34#include "llvm/Support/CommandLine.h"
35#include "llvm/Support/Compression.h"
36#include "llvm/Support/FileUtilities.h"
37#include "llvm/Support/FormattedStream.h"
38#include "llvm/Support/InitLLVM.h"
39#include "llvm/Support/MemoryBuffer.h"
40#include "llvm/Support/SourceMgr.h"
41#include "llvm/Support/TargetSelect.h"
42#include "llvm/Support/TimeProfiler.h"
43#include "llvm/Support/ToolOutputFile.h"
44#include "llvm/Support/VirtualFileSystem.h"
45#include "llvm/Support/WithColor.h"
46#include "llvm/TargetParser/Host.h"
47#include <memory>
48
49using namespace llvm;
50
51static mc::RegisterMCTargetOptionsFlags MOF;
52
53static cl::OptionCategory MCCategory("MC Options");
54
55static cl::opt<std::string> InputFilename(cl::Positional,
56 cl::desc("<input file>"),
57 cl::init(Val: "-"), cl::cat(MCCategory));
58
59static cl::list<std::string> InstPrinterOptions("M",
60 cl::desc("InstPrinter options"),
61 cl::cat(MCCategory));
62
63static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
64 cl::value_desc("filename"),
65 cl::init(Val: "-"), cl::cat(MCCategory));
66
67static cl::opt<std::string> SplitDwarfFile("split-dwarf-file",
68 cl::desc("DWO output filename"),
69 cl::value_desc("filename"),
70 cl::cat(MCCategory));
71
72static cl::opt<bool> ShowEncoding("show-encoding",
73 cl::desc("Show instruction encodings"),
74 cl::cat(MCCategory));
75
76static cl::opt<DebugCompressionType> CompressDebugSections(
77 "compress-debug-sections", cl::ValueOptional,
78 cl::init(Val: DebugCompressionType::None),
79 cl::desc("Choose DWARF debug sections compression:"),
80 cl::values(clEnumValN(DebugCompressionType::None, "none", "No compression"),
81 clEnumValN(DebugCompressionType::Zlib, "zlib", "Use zlib"),
82 clEnumValN(DebugCompressionType::Zstd, "zstd", "Use zstd")),
83 cl::cat(MCCategory));
84
85static cl::opt<bool>
86 ShowInst("show-inst", cl::desc("Show internal instruction representation"),
87 cl::cat(MCCategory));
88
89static cl::opt<bool>
90 ShowInstOperands("show-inst-operands",
91 cl::desc("Show instructions operands as parsed"),
92 cl::cat(MCCategory));
93
94static cl::opt<unsigned>
95 OutputAsmVariant("output-asm-variant",
96 cl::desc("Syntax variant to use for output printing"),
97 cl::cat(MCCategory));
98
99static cl::opt<bool>
100 PrintImmHex("print-imm-hex", cl::init(Val: false),
101 cl::desc("Prefer hex format for immediate values"),
102 cl::cat(MCCategory));
103
104static cl::opt<bool>
105 HexBytes("hex",
106 cl::desc("Take raw hexadecimal bytes as input for disassembly. "
107 "Whitespace is ignored"),
108 cl::cat(MCCategory));
109
110static cl::list<std::string>
111 DefineSymbol("defsym",
112 cl::desc("Defines a symbol to be an integer constant"),
113 cl::cat(MCCategory));
114
115static cl::opt<bool>
116 PreserveComments("preserve-comments",
117 cl::desc("Preserve Comments in outputted assembly"),
118 cl::cat(MCCategory));
119
120static cl::opt<unsigned> CommentColumn("comment-column",
121 cl::desc("Asm comments indentation"),
122 cl::init(Val: 40));
123
124enum OutputFileType {
125 OFT_Null,
126 OFT_AssemblyFile,
127 OFT_ObjectFile
128};
129static cl::opt<OutputFileType>
130 FileType("filetype", cl::init(Val: OFT_AssemblyFile),
131 cl::desc("Choose an output file type:"),
132 cl::values(clEnumValN(OFT_AssemblyFile, "asm",
133 "Emit an assembly ('.s') file"),
134 clEnumValN(OFT_Null, "null",
135 "Don't emit anything (for timing purposes)"),
136 clEnumValN(OFT_ObjectFile, "obj",
137 "Emit a native object ('.o') file")),
138 cl::cat(MCCategory));
139
140static cl::list<std::string> IncludeDirs("I",
141 cl::desc("Directory of include files"),
142 cl::value_desc("directory"),
143 cl::Prefix, cl::cat(MCCategory));
144
145static cl::opt<std::string>
146 ArchName("arch",
147 cl::desc("Target arch to assemble for, "
148 "see -version for available targets"),
149 cl::cat(MCCategory));
150
151static cl::opt<std::string>
152 TripleName("triple",
153 cl::desc("Target triple to assemble for, "
154 "see -version for available targets"),
155 cl::cat(MCCategory));
156
157static cl::opt<std::string>
158 MCPU("mcpu",
159 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
160 cl::value_desc("cpu-name"), cl::init(Val: ""), cl::cat(MCCategory));
161
162static cl::list<std::string>
163 MAttrs("mattr", cl::CommaSeparated,
164 cl::desc("Target specific attributes (-mattr=help for details)"),
165 cl::value_desc("a1,+a2,-a3,..."), cl::cat(MCCategory));
166
167static cl::opt<bool> PIC("position-independent",
168 cl::desc("Position independent"), cl::init(Val: false),
169 cl::cat(MCCategory));
170
171static cl::opt<bool>
172 LargeCodeModel("large-code-model",
173 cl::desc("Create cfi directives that assume the code might "
174 "be more than 2gb away"),
175 cl::cat(MCCategory));
176
177static cl::opt<bool>
178 NoInitialTextSection("n",
179 cl::desc("Don't assume assembly file starts "
180 "in the text section"),
181 cl::cat(MCCategory));
182
183static cl::opt<bool>
184 GenDwarfForAssembly("g",
185 cl::desc("Generate dwarf debugging info for assembly "
186 "source files"),
187 cl::cat(MCCategory));
188
189static cl::opt<std::string>
190 DebugCompilationDir("fdebug-compilation-dir",
191 cl::desc("Specifies the debug info's compilation dir"),
192 cl::cat(MCCategory));
193
194static cl::list<std::string> DebugPrefixMap(
195 "fdebug-prefix-map", cl::desc("Map file source paths in debug info"),
196 cl::value_desc("= separated key-value pairs"), cl::cat(MCCategory));
197
198static cl::opt<std::string> MainFileName(
199 "main-file-name",
200 cl::desc("Specifies the name we should consider the input file"),
201 cl::cat(MCCategory));
202
203static cl::opt<bool> LexMasmIntegers(
204 "masm-integers",
205 cl::desc("Enable binary and hex masm integers (0b110 and 0ABCh)"),
206 cl::cat(MCCategory));
207
208static cl::opt<bool> LexMasmHexFloats(
209 "masm-hexfloats",
210 cl::desc("Enable MASM-style hex float initializers (3F800000r)"),
211 cl::cat(MCCategory));
212
213static cl::opt<bool> LexMotorolaIntegers(
214 "motorola-integers",
215 cl::desc("Enable binary and hex Motorola integers (%110 and $ABC)"),
216 cl::cat(MCCategory));
217
218static cl::opt<bool> NoExecStack("no-exec-stack",
219 cl::desc("File doesn't need an exec stack"),
220 cl::cat(MCCategory));
221
222static cl::opt<bool> ValidateCFI("validate-cfi",
223 cl::desc("Validate the CFI directives"),
224 cl::cat(MCCategory));
225
226enum ActionType {
227 AC_AsLex,
228 AC_Assemble,
229 AC_Disassemble,
230 AC_MDisassemble,
231 AC_CDisassemble,
232};
233
234static cl::opt<ActionType> Action(
235 cl::desc("Action to perform:"), cl::init(Val: AC_Assemble),
236 cl::values(clEnumValN(AC_AsLex, "as-lex", "Lex tokens from a .s file"),
237 clEnumValN(AC_Assemble, "assemble",
238 "Assemble a .s file (default)"),
239 clEnumValN(AC_Disassemble, "disassemble",
240 "Disassemble strings of hex bytes"),
241 clEnumValN(AC_MDisassemble, "mdis",
242 "Marked up disassembly of strings of hex bytes"),
243 clEnumValN(AC_CDisassemble, "cdis",
244 "Colored disassembly of strings of hex bytes")),
245 cl::cat(MCCategory));
246
247static cl::opt<unsigned>
248 NumBenchmarkRuns("runs", cl::desc("Number of runs for benchmarking"),
249 cl::cat(MCCategory));
250
251static cl::opt<bool> TimeTrace("time-trace", cl::desc("Record time trace"));
252
253static cl::opt<unsigned> TimeTraceGranularity(
254 "time-trace-granularity",
255 cl::desc(
256 "Minimum time granularity (in microseconds) traced by time profiler"),
257 cl::init(Val: 500), cl::Hidden);
258
259static cl::opt<std::string>
260 TimeTraceFile("time-trace-file",
261 cl::desc("Specify time trace file destination"),
262 cl::value_desc("filename"));
263
264static const Target *GetTarget(const char *ProgName) {
265 // Figure out the target triple.
266 if (TripleName.empty())
267 TripleName = sys::getDefaultTargetTriple();
268 Triple TheTriple(Triple::normalize(Str: TripleName));
269
270 // Get the target specific parser.
271 std::string Error;
272 const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple,
273 Error);
274 if (!TheTarget) {
275 WithColor::error(OS&: errs(), Prefix: ProgName) << Error;
276 return nullptr;
277 }
278
279 // Update the triple name and return the found target.
280 TripleName = TheTriple.getTriple();
281 return TheTarget;
282}
283
284static std::unique_ptr<ToolOutputFile> GetOutputStream(StringRef Path,
285 sys::fs::OpenFlags Flags) {
286 std::error_code EC;
287 auto Out = std::make_unique<ToolOutputFile>(args&: Path, args&: EC, args&: Flags);
288 if (EC) {
289 WithColor::error() << EC.message() << '\n';
290 return nullptr;
291 }
292
293 return Out;
294}
295
296static std::string DwarfDebugFlags;
297static void setDwarfDebugFlags(int argc, char **argv) {
298 if (!getenv(name: "RC_DEBUG_OPTIONS"))
299 return;
300 for (int i = 0; i < argc; i++) {
301 DwarfDebugFlags += argv[i];
302 if (i + 1 < argc)
303 DwarfDebugFlags += " ";
304 }
305}
306
307static std::string DwarfDebugProducer;
308static void setDwarfDebugProducer() {
309 if(!getenv(name: "DEBUG_PRODUCER"))
310 return;
311 DwarfDebugProducer += getenv(name: "DEBUG_PRODUCER");
312}
313
314static int AsLexInput(SourceMgr &SrcMgr, MCAsmInfo &MAI,
315 raw_ostream &OS) {
316
317 AsmLexer Lexer(MAI);
318 Lexer.setBuffer(Buf: SrcMgr.getMemoryBuffer(i: SrcMgr.getMainFileID())->getBuffer());
319
320 bool Error = false;
321 while (Lexer.Lex().isNot(K: AsmToken::Eof)) {
322 Lexer.getTok().dump(OS);
323 OS << "\n";
324 if (Lexer.getTok().getKind() == AsmToken::Error)
325 Error = true;
326 }
327
328 return Error;
329}
330
331static int fillCommandLineSymbols(MCAsmParser &Parser) {
332 for (auto &I: DefineSymbol) {
333 auto Pair = StringRef(I).split(Separator: '=');
334 auto Sym = Pair.first;
335 auto Val = Pair.second;
336
337 if (Sym.empty() || Val.empty()) {
338 WithColor::error() << "defsym must be of the form: sym=value: " << I
339 << "\n";
340 return 1;
341 }
342 int64_t Value;
343 if (Val.getAsInteger(Radix: 0, Result&: Value)) {
344 WithColor::error() << "value is not an integer: " << Val << "\n";
345 return 1;
346 }
347 Parser.getContext().setSymbolValue(Streamer&: Parser.getStreamer(), Sym, Val: Value);
348 }
349 return 0;
350}
351
352static int AssembleInput(const char *ProgName, const Target *TheTarget,
353 SourceMgr &SrcMgr, MCContext &Ctx, MCStreamer &Str,
354 MCAsmInfo &MAI, MCSubtargetInfo &STI,
355 MCInstrInfo &MCII, MCTargetOptions const &MCOptions) {
356 std::unique_ptr<MCAsmParser> Parser(
357 createMCAsmParser(SrcMgr, Ctx, Str, MAI));
358 std::unique_ptr<MCTargetAsmParser> TAP(
359 TheTarget->createMCAsmParser(STI, Parser&: *Parser, MII: MCII, Options: MCOptions));
360
361 if (!TAP) {
362 WithColor::error(OS&: errs(), Prefix: ProgName)
363 << "this target does not support assembly parsing.\n";
364 return 1;
365 }
366
367 int SymbolResult = fillCommandLineSymbols(Parser&: *Parser);
368 if(SymbolResult)
369 return SymbolResult;
370 Parser->setShowParsedOperands(ShowInstOperands);
371 Parser->setTargetParser(*TAP);
372 Parser->getLexer().setLexMasmIntegers(LexMasmIntegers);
373 Parser->getLexer().setLexMasmHexFloats(LexMasmHexFloats);
374 Parser->getLexer().setLexMotorolaIntegers(LexMotorolaIntegers);
375
376 int Res = Parser->Run(NoInitialTextSection);
377
378 return Res;
379}
380
381int main(int argc, char **argv) {
382 InitLLVM X(argc, argv);
383
384 // Initialize targets and assembly printers/parsers.
385 llvm::InitializeAllTargetInfos();
386 llvm::InitializeAllTargetMCs();
387 llvm::InitializeAllAsmParsers();
388 llvm::InitializeAllDisassemblers();
389
390 // Register the target printer for --version.
391 cl::AddExtraVersionPrinter(func: TargetRegistry::printRegisteredTargetsForVersion);
392
393 cl::HideUnrelatedOptions(Categories: {&MCCategory, &getColorCategory()});
394 cl::ParseCommandLineOptions(argc, argv, Overview: "llvm machine code playground\n");
395
396 if (TimeTrace)
397 timeTraceProfilerInitialize(TimeTraceGranularity, ProcName: argv[0]);
398
399 llvm::scope_exit TimeTraceScopeExit([]() {
400 if (!TimeTrace)
401 return;
402 if (auto E = timeTraceProfilerWrite(PreferredFileName: TimeTraceFile, FallbackFileName: OutputFilename)) {
403 logAllUnhandledErrors(E: std::move(E), OS&: errs());
404 return;
405 }
406 timeTraceProfilerCleanup();
407 });
408
409 MCTargetOptions MCOptions = mc::InitMCTargetOptionsFromFlags();
410 MCOptions.CompressDebugSections = CompressDebugSections.getValue();
411 MCOptions.ShowMCInst = ShowInst;
412 MCOptions.AsmVerbose = true;
413 MCOptions.MCNoExecStack = NoExecStack;
414 MCOptions.MCUseDwarfDirectory = MCTargetOptions::EnableDwarfDirectory;
415 MCOptions.InstPrinterOptions = InstPrinterOptions;
416
417 setDwarfDebugFlags(argc, argv);
418 setDwarfDebugProducer();
419
420 const char *ProgName = argv[0];
421 const Target *TheTarget = GetTarget(ProgName);
422 if (!TheTarget)
423 return 1;
424 // Now that GetTarget() has (potentially) replaced TripleName, it's safe to
425 // construct the Triple object.
426 Triple TheTriple(TripleName);
427
428 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferPtr =
429 MemoryBuffer::getFileOrSTDIN(Filename: InputFilename, /*IsText=*/true);
430 if (std::error_code EC = BufferPtr.getError()) {
431 WithColor::error(OS&: errs(), Prefix: ProgName)
432 << InputFilename << ": " << EC.message() << '\n';
433 return 1;
434 }
435 MemoryBuffer *Buffer = BufferPtr->get();
436
437 SourceMgr SrcMgr;
438
439 // Tell SrcMgr about this buffer, which is what the parser will pick up.
440 SrcMgr.AddNewSourceBuffer(F: std::move(*BufferPtr), IncludeLoc: SMLoc());
441
442 // Record the location of the include directories so that the lexer can find
443 // it later.
444 SrcMgr.setIncludeDirs(IncludeDirs);
445 SrcMgr.setVirtualFileSystem(vfs::getRealFileSystem());
446
447 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TT: TheTriple));
448 assert(MRI && "Unable to create target register info!");
449
450 std::unique_ptr<MCAsmInfo> MAI(
451 TheTarget->createMCAsmInfo(MRI: *MRI, TheTriple, Options: MCOptions));
452 assert(MAI && "Unable to create target asm info!");
453
454 if (CompressDebugSections != DebugCompressionType::None) {
455 if (const char *Reason = compression::getReasonIfUnsupported(
456 F: compression::formatFor(Type: CompressDebugSections))) {
457 WithColor::error(OS&: errs(), Prefix: ProgName)
458 << "--compress-debug-sections: " << Reason;
459 return 1;
460 }
461 }
462 MAI->setPreserveAsmComments(PreserveComments);
463 MAI->setCommentColumn(CommentColumn);
464
465 // Package up features to be passed to target/subtarget
466 SubtargetFeatures Features;
467 std::string FeaturesStr;
468
469 // Replace -mcpu=native with Host CPU and features.
470 if (MCPU == "native") {
471 MCPU = std::string(llvm::sys::getHostCPUName());
472
473 llvm::StringMap<bool> TargetFeatures = llvm::sys::getHostCPUFeatures();
474 for (auto const &[FeatureName, IsSupported] : TargetFeatures)
475 Features.AddFeature(String: FeatureName, Enable: IsSupported);
476 }
477
478 // Handle features passed to target/subtarget.
479 for (unsigned i = 0; i != MAttrs.size(); ++i)
480 Features.AddFeature(String: MAttrs[i]);
481 FeaturesStr = Features.getString();
482
483 std::unique_ptr<MCSubtargetInfo> STI(
484 TheTarget->createMCSubtargetInfo(TheTriple, CPU: MCPU, Features: FeaturesStr));
485 if (!STI) {
486 WithColor::error(OS&: errs(), Prefix: ProgName) << "unable to create subtarget info\n";
487 return 1;
488 }
489
490 // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
491 // MCObjectFileInfo needs a MCContext reference in order to initialize itself.
492 MCContext Ctx(TheTriple, MAI.get(), MRI.get(), STI.get(), &SrcMgr,
493 &MCOptions);
494 std::unique_ptr<MCObjectFileInfo> MOFI(
495 TheTarget->createMCObjectFileInfo(Ctx, PIC, LargeCodeModel));
496 Ctx.setObjectFileInfo(MOFI.get());
497
498 Ctx.setGenDwarfForAssembly(GenDwarfForAssembly);
499 // Default to 4 for dwarf version.
500 unsigned DwarfVersion = MCOptions.DwarfVersion ? MCOptions.DwarfVersion : 4;
501 if (DwarfVersion < 2 || DwarfVersion > 6) {
502 errs() << ProgName << ": Dwarf version " << DwarfVersion
503 << " is not supported." << '\n';
504 return 1;
505 }
506 Ctx.setDwarfVersion(DwarfVersion);
507 if (MCOptions.Dwarf64) {
508 // The 64-bit DWARF format was introduced in DWARFv3.
509 if (DwarfVersion < 3) {
510 errs() << ProgName
511 << ": the 64-bit DWARF format is not supported for DWARF versions "
512 "prior to 3\n";
513 return 1;
514 }
515 // 32-bit targets don't support DWARF64, which requires 64-bit relocations.
516 if (MAI->getCodePointerSize() < 8) {
517 errs() << ProgName
518 << ": the 64-bit DWARF format is only supported for 64-bit "
519 "targets\n";
520 return 1;
521 }
522 // If needsDwarfSectionOffsetDirective is true, we would eventually call
523 // MCStreamer::emitSymbolValue() with IsSectionRelative = true, but that
524 // is supported only for 4-byte long references.
525 if (MAI->needsDwarfSectionOffsetDirective()) {
526 errs() << ProgName << ": the 64-bit DWARF format is not supported for "
527 << TheTriple.normalize() << "\n";
528 return 1;
529 }
530 Ctx.setDwarfFormat(dwarf::DWARF64);
531 }
532 if (!DwarfDebugFlags.empty())
533 Ctx.setDwarfDebugFlags(StringRef(DwarfDebugFlags));
534 if (!DwarfDebugProducer.empty())
535 Ctx.setDwarfDebugProducer(StringRef(DwarfDebugProducer));
536 if (!DebugCompilationDir.empty())
537 Ctx.setCompilationDir(DebugCompilationDir);
538 else {
539 // If no compilation dir is set, try to use the current directory.
540 SmallString<128> CWD;
541 if (!sys::fs::current_path(result&: CWD))
542 Ctx.setCompilationDir(CWD);
543 }
544 for (const auto &Arg : DebugPrefixMap) {
545 const auto &KV = StringRef(Arg).split(Separator: '=');
546 Ctx.addDebugPrefixMapEntry(From: std::string(KV.first), To: std::string(KV.second));
547 }
548 if (!MainFileName.empty())
549 Ctx.setMainFileName(MainFileName);
550 if (GenDwarfForAssembly)
551 Ctx.setGenDwarfRootFile(FileName: InputFilename, Buffer: Buffer->getBuffer());
552
553 sys::fs::OpenFlags Flags = (FileType == OFT_AssemblyFile)
554 ? sys::fs::OF_TextWithCRLF
555 : sys::fs::OF_None;
556 std::unique_ptr<ToolOutputFile> Out = GetOutputStream(Path: OutputFilename, Flags);
557 if (!Out)
558 return 1;
559
560 std::unique_ptr<ToolOutputFile> DwoOut;
561 if (!SplitDwarfFile.empty()) {
562 if (FileType != OFT_ObjectFile) {
563 WithColor::error() << "dwo output only supported with object files\n";
564 return 1;
565 }
566 DwoOut = GetOutputStream(Path: SplitDwarfFile, Flags: sys::fs::OF_None);
567 if (!DwoOut)
568 return 1;
569 }
570
571 std::unique_ptr<buffer_ostream> BOS;
572 raw_pwrite_stream *OS = &Out->os();
573 std::unique_ptr<MCStreamer> Str;
574
575 std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
576 assert(MCII && "Unable to create instruction info!");
577
578 std::unique_ptr<MCInstPrinter> IP;
579 if (ValidateCFI) {
580 // TODO: The DWARF CFI checker support for emitting anything other than
581 // errors and warnings has not been implemented yet. Because of this, it is
582 // assert-checked that the filetype output is null.
583 assert(FileType == OFT_Null);
584 auto FFA = std::make_unique<CFIFunctionFrameAnalyzer>(args&: Ctx, args&: *MCII);
585 auto FFS = std::make_unique<CFIFunctionFrameStreamer>(args&: Ctx, args: std::move(FFA));
586 TheTarget->createNullTargetStreamer(S&: *FFS);
587 Str = std::move(FFS);
588 } else if (FileType == OFT_AssemblyFile) {
589 unsigned AsmVariant = OutputAsmVariant.getNumOccurrences()
590 ? OutputAsmVariant
591 : MAI->getAssemblerDialect();
592 IP.reset(p: TheTarget->createMCInstPrinter(T: Triple(TripleName), SyntaxVariant: AsmVariant,
593 MAI: *MAI, MII: *MCII, MRI: *MRI));
594
595 if (!IP) {
596 WithColor::error()
597 << "unable to create instruction printer for target triple '"
598 << TheTriple.normalize() << "' with assembly variant " << AsmVariant
599 << "\n";
600 return 1;
601 }
602
603 for (StringRef Opt : InstPrinterOptions)
604 if (!IP->applyTargetSpecificCLOption(Opt)) {
605 WithColor::error() << "invalid InstPrinter option '" << Opt << "'\n";
606 return 1;
607 }
608
609 // Set the display preference for hex vs. decimal immediates.
610 IP->setPrintImmHex(PrintImmHex);
611
612 switch (Action) {
613 case AC_MDisassemble:
614 IP->setUseMarkup(true);
615 break;
616 case AC_CDisassemble:
617 IP->setUseColor(true);
618 break;
619 default:
620 break;
621 }
622
623 // Set up the AsmStreamer.
624 std::unique_ptr<MCCodeEmitter> CE;
625 if (ShowEncoding)
626 CE.reset(p: TheTarget->createMCCodeEmitter(II: *MCII, Ctx));
627
628 std::unique_ptr<MCAsmBackend> MAB(
629 TheTarget->createMCAsmBackend(STI: *STI, MRI: *MRI, Options: MCOptions));
630 auto FOut = std::make_unique<formatted_raw_ostream>(args&: *OS);
631 Str.reset(p: TheTarget->createAsmStreamer(Ctx, OS: std::move(FOut), IP: std::move(IP),
632 CE: std::move(CE), TAB: std::move(MAB)));
633
634 Triple T(TripleName);
635 if (T.isLFI())
636 initializeLFIMCStreamer(Streamer&: *Str.get(), Ctx, TheTriple: T);
637 } else if (FileType == OFT_Null) {
638 Str.reset(p: TheTarget->createNullStreamer(Ctx));
639 } else {
640 assert(FileType == OFT_ObjectFile && "Invalid file type!");
641
642 if (!Out->os().supportsSeeking()) {
643 BOS = std::make_unique<buffer_ostream>(args&: Out->os());
644 OS = BOS.get();
645 }
646
647 MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(II: *MCII, Ctx);
648 MCAsmBackend *MAB = TheTarget->createMCAsmBackend(STI: *STI, MRI: *MRI, Options: MCOptions);
649 Str.reset(p: TheTarget->createMCObjectStreamer(
650 T: TheTriple, Ctx, TAB: std::unique_ptr<MCAsmBackend>(MAB),
651 OW: DwoOut ? MAB->createDwoObjectWriter(OS&: *OS, DwoOS&: DwoOut->os())
652 : MAB->createObjectWriter(OS&: *OS),
653 Emitter: std::unique_ptr<MCCodeEmitter>(CE), STI: *STI));
654 Str->emitVersionForTarget(Target: TheTriple, SDKVersion: VersionTuple(), DarwinTargetVariantTriple: nullptr,
655 DarwinTargetVariantSDKVersion: VersionTuple());
656 }
657
658 int Res = 1;
659 bool disassemble = false;
660 switch (Action) {
661 case AC_AsLex:
662 Res = AsLexInput(SrcMgr, MAI&: *MAI, OS&: Out->os());
663 break;
664 case AC_Assemble:
665 Res = AssembleInput(ProgName, TheTarget, SrcMgr, Ctx, Str&: *Str, MAI&: *MAI, STI&: *STI,
666 MCII&: *MCII, MCOptions);
667 break;
668 case AC_MDisassemble:
669 case AC_CDisassemble:
670 case AC_Disassemble:
671 disassemble = true;
672 break;
673 }
674 if (disassemble)
675 Res = Disassembler::disassemble(T: *TheTarget, STI&: *STI, Streamer&: *Str, Buffer&: *Buffer, SM&: SrcMgr,
676 Ctx, MCOptions, HexBytes, NumBenchmarkRuns);
677
678 // Keep output if no errors.
679 if (Res == 0) {
680 Out->keep();
681 if (DwoOut)
682 DwoOut->keep();
683 }
684
685 return Res;
686}
687