10b57cec5SDimitry Andric //===-- llvm-mc.cpp - Machine Code Hacking Driver ---------------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This utility is a simple driver that allows command line hacking on machine 100b57cec5SDimitry Andric // code. 110b57cec5SDimitry Andric // 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric #include "Disassembler.h" 150b57cec5SDimitry Andric #include "llvm/MC/MCAsmBackend.h" 160b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h" 170b57cec5SDimitry Andric #include "llvm/MC/MCCodeEmitter.h" 180b57cec5SDimitry Andric #include "llvm/MC/MCContext.h" 190b57cec5SDimitry Andric #include "llvm/MC/MCInstPrinter.h" 200b57cec5SDimitry Andric #include "llvm/MC/MCInstrInfo.h" 210b57cec5SDimitry Andric #include "llvm/MC/MCObjectFileInfo.h" 220b57cec5SDimitry Andric #include "llvm/MC/MCObjectWriter.h" 230b57cec5SDimitry Andric #include "llvm/MC/MCParser/AsmLexer.h" 240b57cec5SDimitry Andric #include "llvm/MC/MCParser/MCTargetAsmParser.h" 250b57cec5SDimitry Andric #include "llvm/MC/MCRegisterInfo.h" 260b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h" 270b57cec5SDimitry Andric #include "llvm/MC/MCSubtargetInfo.h" 285ffd83dbSDimitry Andric #include "llvm/MC/MCTargetOptionsCommandFlags.h" 29349cc55cSDimitry Andric #include "llvm/MC/TargetRegistry.h" 300b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h" 310b57cec5SDimitry Andric #include "llvm/Support/Compression.h" 320b57cec5SDimitry Andric #include "llvm/Support/FileUtilities.h" 330b57cec5SDimitry Andric #include "llvm/Support/FormattedStream.h" 340b57cec5SDimitry Andric #include "llvm/Support/InitLLVM.h" 350b57cec5SDimitry Andric #include "llvm/Support/MemoryBuffer.h" 360b57cec5SDimitry Andric #include "llvm/Support/SourceMgr.h" 370b57cec5SDimitry Andric #include "llvm/Support/TargetSelect.h" 380b57cec5SDimitry Andric #include "llvm/Support/ToolOutputFile.h" 390b57cec5SDimitry Andric #include "llvm/Support/WithColor.h" 4006c3fb27SDimitry Andric #include "llvm/TargetParser/Host.h" 410b57cec5SDimitry Andric 420b57cec5SDimitry Andric using namespace llvm; 430b57cec5SDimitry Andric 445ffd83dbSDimitry Andric static mc::RegisterMCTargetOptionsFlags MOF; 455ffd83dbSDimitry Andric 46fe6060f1SDimitry Andric static cl::OptionCategory MCCategory("MC Options"); 47fe6060f1SDimitry Andric 48fe6060f1SDimitry Andric static cl::opt<std::string> InputFilename(cl::Positional, 49fe6060f1SDimitry Andric cl::desc("<input file>"), 50fe6060f1SDimitry Andric cl::init("-"), cl::cat(MCCategory)); 51fe6060f1SDimitry Andric 52fe6060f1SDimitry Andric static cl::list<std::string> 53fe6060f1SDimitry Andric DisassemblerOptions("M", cl::desc("Disassembler options"), 54fe6060f1SDimitry Andric cl::cat(MCCategory)); 550b57cec5SDimitry Andric 560b57cec5SDimitry Andric static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"), 570b57cec5SDimitry Andric cl::value_desc("filename"), 58fe6060f1SDimitry Andric cl::init("-"), cl::cat(MCCategory)); 590b57cec5SDimitry Andric 600b57cec5SDimitry Andric static cl::opt<std::string> SplitDwarfFile("split-dwarf-file", 610b57cec5SDimitry Andric cl::desc("DWO output filename"), 62fe6060f1SDimitry Andric cl::value_desc("filename"), 63fe6060f1SDimitry Andric cl::cat(MCCategory)); 640b57cec5SDimitry Andric 65fe6060f1SDimitry Andric static cl::opt<bool> ShowEncoding("show-encoding", 66fe6060f1SDimitry Andric cl::desc("Show instruction encodings"), 67fe6060f1SDimitry Andric cl::cat(MCCategory)); 680b57cec5SDimitry Andric 690b57cec5SDimitry Andric static cl::opt<DebugCompressionType> CompressDebugSections( 700b57cec5SDimitry Andric "compress-debug-sections", cl::ValueOptional, 710b57cec5SDimitry Andric cl::init(DebugCompressionType::None), 720b57cec5SDimitry Andric cl::desc("Choose DWARF debug sections compression:"), 730b57cec5SDimitry Andric cl::values(clEnumValN(DebugCompressionType::None, "none", "No compression"), 74bdd1243dSDimitry Andric clEnumValN(DebugCompressionType::Zlib, "zlib", "Use zlib"), 75bdd1243dSDimitry Andric clEnumValN(DebugCompressionType::Zstd, "zstd", "Use zstd")), 76fe6060f1SDimitry Andric cl::cat(MCCategory)); 770b57cec5SDimitry Andric 780b57cec5SDimitry Andric static cl::opt<bool> 79fe6060f1SDimitry Andric ShowInst("show-inst", cl::desc("Show internal instruction representation"), 80fe6060f1SDimitry Andric cl::cat(MCCategory)); 810b57cec5SDimitry Andric 820b57cec5SDimitry Andric static cl::opt<bool> 830b57cec5SDimitry Andric ShowInstOperands("show-inst-operands", 84fe6060f1SDimitry Andric cl::desc("Show instructions operands as parsed"), 85fe6060f1SDimitry Andric cl::cat(MCCategory)); 860b57cec5SDimitry Andric 870b57cec5SDimitry Andric static cl::opt<unsigned> 880b57cec5SDimitry Andric OutputAsmVariant("output-asm-variant", 89fe6060f1SDimitry Andric cl::desc("Syntax variant to use for output printing"), 90fe6060f1SDimitry Andric cl::cat(MCCategory)); 910b57cec5SDimitry Andric 920b57cec5SDimitry Andric static cl::opt<bool> 930b57cec5SDimitry Andric PrintImmHex("print-imm-hex", cl::init(false), 94fe6060f1SDimitry Andric cl::desc("Prefer hex format for immediate values"), 95fe6060f1SDimitry Andric cl::cat(MCCategory)); 960b57cec5SDimitry Andric 970b57cec5SDimitry Andric static cl::list<std::string> 98fe6060f1SDimitry Andric DefineSymbol("defsym", 99fe6060f1SDimitry Andric cl::desc("Defines a symbol to be an integer constant"), 100fe6060f1SDimitry Andric cl::cat(MCCategory)); 1010b57cec5SDimitry Andric 1020b57cec5SDimitry Andric static cl::opt<bool> 1030b57cec5SDimitry Andric PreserveComments("preserve-comments", 104fe6060f1SDimitry Andric cl::desc("Preserve Comments in outputted assembly"), 105fe6060f1SDimitry Andric cl::cat(MCCategory)); 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric enum OutputFileType { 1080b57cec5SDimitry Andric OFT_Null, 1090b57cec5SDimitry Andric OFT_AssemblyFile, 1100b57cec5SDimitry Andric OFT_ObjectFile 1110b57cec5SDimitry Andric }; 1120b57cec5SDimitry Andric static cl::opt<OutputFileType> 1130b57cec5SDimitry Andric FileType("filetype", cl::init(OFT_AssemblyFile), 1140b57cec5SDimitry Andric cl::desc("Choose an output file type:"), 115fe6060f1SDimitry Andric cl::values(clEnumValN(OFT_AssemblyFile, "asm", 1160b57cec5SDimitry Andric "Emit an assembly ('.s') file"), 1170b57cec5SDimitry Andric clEnumValN(OFT_Null, "null", 1180b57cec5SDimitry Andric "Don't emit anything (for timing purposes)"), 1190b57cec5SDimitry Andric clEnumValN(OFT_ObjectFile, "obj", 120fe6060f1SDimitry Andric "Emit a native object ('.o') file")), 121fe6060f1SDimitry Andric cl::cat(MCCategory)); 1220b57cec5SDimitry Andric 123fe6060f1SDimitry Andric static cl::list<std::string> IncludeDirs("I", 124fe6060f1SDimitry Andric cl::desc("Directory of include files"), 125fe6060f1SDimitry Andric cl::value_desc("directory"), 126fe6060f1SDimitry Andric cl::Prefix, cl::cat(MCCategory)); 1270b57cec5SDimitry Andric 1280b57cec5SDimitry Andric static cl::opt<std::string> 129fe6060f1SDimitry Andric ArchName("arch", 130fe6060f1SDimitry Andric cl::desc("Target arch to assemble for, " 131fe6060f1SDimitry Andric "see -version for available targets"), 132fe6060f1SDimitry Andric cl::cat(MCCategory)); 1330b57cec5SDimitry Andric 1340b57cec5SDimitry Andric static cl::opt<std::string> 135fe6060f1SDimitry Andric TripleName("triple", 136fe6060f1SDimitry Andric cl::desc("Target triple to assemble for, " 137fe6060f1SDimitry Andric "see -version for available targets"), 138fe6060f1SDimitry Andric cl::cat(MCCategory)); 1390b57cec5SDimitry Andric 1400b57cec5SDimitry Andric static cl::opt<std::string> 1410b57cec5SDimitry Andric MCPU("mcpu", 1420b57cec5SDimitry Andric cl::desc("Target a specific cpu type (-mcpu=help for details)"), 143fe6060f1SDimitry Andric cl::value_desc("cpu-name"), cl::init(""), cl::cat(MCCategory)); 1440b57cec5SDimitry Andric 1450b57cec5SDimitry Andric static cl::list<std::string> 146fe6060f1SDimitry Andric MAttrs("mattr", cl::CommaSeparated, 1470b57cec5SDimitry Andric cl::desc("Target specific attributes (-mattr=help for details)"), 148fe6060f1SDimitry Andric cl::value_desc("a1,+a2,-a3,..."), cl::cat(MCCategory)); 1490b57cec5SDimitry Andric 1500b57cec5SDimitry Andric static cl::opt<bool> PIC("position-independent", 151fe6060f1SDimitry Andric cl::desc("Position independent"), cl::init(false), 152fe6060f1SDimitry Andric cl::cat(MCCategory)); 1530b57cec5SDimitry Andric 1540b57cec5SDimitry Andric static cl::opt<bool> 1550b57cec5SDimitry Andric LargeCodeModel("large-code-model", 1560b57cec5SDimitry Andric cl::desc("Create cfi directives that assume the code might " 157fe6060f1SDimitry Andric "be more than 2gb away"), 158fe6060f1SDimitry Andric cl::cat(MCCategory)); 1590b57cec5SDimitry Andric 1600b57cec5SDimitry Andric static cl::opt<bool> 161fe6060f1SDimitry Andric NoInitialTextSection("n", 162fe6060f1SDimitry Andric cl::desc("Don't assume assembly file starts " 163fe6060f1SDimitry Andric "in the text section"), 164fe6060f1SDimitry Andric cl::cat(MCCategory)); 1650b57cec5SDimitry Andric 1660b57cec5SDimitry Andric static cl::opt<bool> 167fe6060f1SDimitry Andric GenDwarfForAssembly("g", 168fe6060f1SDimitry Andric cl::desc("Generate dwarf debugging info for assembly " 169fe6060f1SDimitry Andric "source files"), 170fe6060f1SDimitry Andric cl::cat(MCCategory)); 1710b57cec5SDimitry Andric 1720b57cec5SDimitry Andric static cl::opt<std::string> 1730b57cec5SDimitry Andric DebugCompilationDir("fdebug-compilation-dir", 174fe6060f1SDimitry Andric cl::desc("Specifies the debug info's compilation dir"), 175fe6060f1SDimitry Andric cl::cat(MCCategory)); 1760b57cec5SDimitry Andric 177fe6060f1SDimitry Andric static cl::list<std::string> DebugPrefixMap( 178fe6060f1SDimitry Andric "fdebug-prefix-map", cl::desc("Map file source paths in debug info"), 179fe6060f1SDimitry Andric cl::value_desc("= separated key-value pairs"), cl::cat(MCCategory)); 1800b57cec5SDimitry Andric 181fe6060f1SDimitry Andric static cl::opt<std::string> MainFileName( 182fe6060f1SDimitry Andric "main-file-name", 183fe6060f1SDimitry Andric cl::desc("Specifies the name we should consider the input file"), 184fe6060f1SDimitry Andric cl::cat(MCCategory)); 1850b57cec5SDimitry Andric 1860b57cec5SDimitry Andric static cl::opt<bool> LexMasmIntegers( 1870b57cec5SDimitry Andric "masm-integers", 188fe6060f1SDimitry Andric cl::desc("Enable binary and hex masm integers (0b110 and 0ABCh)"), 189fe6060f1SDimitry Andric cl::cat(MCCategory)); 1900b57cec5SDimitry Andric 191e8d8bef9SDimitry Andric static cl::opt<bool> LexMasmHexFloats( 192e8d8bef9SDimitry Andric "masm-hexfloats", 193fe6060f1SDimitry Andric cl::desc("Enable MASM-style hex float initializers (3F800000r)"), 194fe6060f1SDimitry Andric cl::cat(MCCategory)); 195fe6060f1SDimitry Andric 196fe6060f1SDimitry Andric static cl::opt<bool> LexMotorolaIntegers( 197fe6060f1SDimitry Andric "motorola-integers", 198fe6060f1SDimitry Andric cl::desc("Enable binary and hex Motorola integers (%110 and $ABC)"), 199fe6060f1SDimitry Andric cl::cat(MCCategory)); 200e8d8bef9SDimitry Andric 2010b57cec5SDimitry Andric static cl::opt<bool> NoExecStack("no-exec-stack", 202fe6060f1SDimitry Andric cl::desc("File doesn't need an exec stack"), 203fe6060f1SDimitry Andric cl::cat(MCCategory)); 2040b57cec5SDimitry Andric 2050b57cec5SDimitry Andric enum ActionType { 2060b57cec5SDimitry Andric AC_AsLex, 2070b57cec5SDimitry Andric AC_Assemble, 2080b57cec5SDimitry Andric AC_Disassemble, 2090b57cec5SDimitry Andric AC_MDisassemble, 2105f757f3fSDimitry Andric AC_CDisassemble, 2110b57cec5SDimitry Andric }; 2120b57cec5SDimitry Andric 213fe6060f1SDimitry Andric static cl::opt<ActionType> Action( 214fe6060f1SDimitry Andric cl::desc("Action to perform:"), cl::init(AC_Assemble), 215fe6060f1SDimitry Andric cl::values(clEnumValN(AC_AsLex, "as-lex", "Lex tokens from a .s file"), 2160b57cec5SDimitry Andric clEnumValN(AC_Assemble, "assemble", 2170b57cec5SDimitry Andric "Assemble a .s file (default)"), 2180b57cec5SDimitry Andric clEnumValN(AC_Disassemble, "disassemble", 2190b57cec5SDimitry Andric "Disassemble strings of hex bytes"), 2200b57cec5SDimitry Andric clEnumValN(AC_MDisassemble, "mdis", 2215f757f3fSDimitry Andric "Marked up disassembly of strings of hex bytes"), 2225f757f3fSDimitry Andric clEnumValN(AC_CDisassemble, "cdis", 2235f757f3fSDimitry Andric "Colored disassembly of strings of hex bytes")), 224fe6060f1SDimitry Andric cl::cat(MCCategory)); 2250b57cec5SDimitry Andric 2260b57cec5SDimitry Andric static const Target *GetTarget(const char *ProgName) { 2270b57cec5SDimitry Andric // Figure out the target triple. 2280b57cec5SDimitry Andric if (TripleName.empty()) 2290b57cec5SDimitry Andric TripleName = sys::getDefaultTargetTriple(); 2300b57cec5SDimitry Andric Triple TheTriple(Triple::normalize(TripleName)); 2310b57cec5SDimitry Andric 2320b57cec5SDimitry Andric // Get the target specific parser. 2330b57cec5SDimitry Andric std::string Error; 2340b57cec5SDimitry Andric const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple, 2350b57cec5SDimitry Andric Error); 2360b57cec5SDimitry Andric if (!TheTarget) { 2370b57cec5SDimitry Andric WithColor::error(errs(), ProgName) << Error; 2380b57cec5SDimitry Andric return nullptr; 2390b57cec5SDimitry Andric } 2400b57cec5SDimitry Andric 2410b57cec5SDimitry Andric // Update the triple name and return the found target. 2420b57cec5SDimitry Andric TripleName = TheTriple.getTriple(); 2430b57cec5SDimitry Andric return TheTarget; 2440b57cec5SDimitry Andric } 2450b57cec5SDimitry Andric 2468bcb0991SDimitry Andric static std::unique_ptr<ToolOutputFile> GetOutputStream(StringRef Path, 2478bcb0991SDimitry Andric sys::fs::OpenFlags Flags) { 2480b57cec5SDimitry Andric std::error_code EC; 2498bcb0991SDimitry Andric auto Out = std::make_unique<ToolOutputFile>(Path, EC, Flags); 2500b57cec5SDimitry Andric if (EC) { 2510b57cec5SDimitry Andric WithColor::error() << EC.message() << '\n'; 2520b57cec5SDimitry Andric return nullptr; 2530b57cec5SDimitry Andric } 2540b57cec5SDimitry Andric 2550b57cec5SDimitry Andric return Out; 2560b57cec5SDimitry Andric } 2570b57cec5SDimitry Andric 2580b57cec5SDimitry Andric static std::string DwarfDebugFlags; 2590b57cec5SDimitry Andric static void setDwarfDebugFlags(int argc, char **argv) { 2600b57cec5SDimitry Andric if (!getenv("RC_DEBUG_OPTIONS")) 2610b57cec5SDimitry Andric return; 2620b57cec5SDimitry Andric for (int i = 0; i < argc; i++) { 2630b57cec5SDimitry Andric DwarfDebugFlags += argv[i]; 2640b57cec5SDimitry Andric if (i + 1 < argc) 2650b57cec5SDimitry Andric DwarfDebugFlags += " "; 2660b57cec5SDimitry Andric } 2670b57cec5SDimitry Andric } 2680b57cec5SDimitry Andric 2690b57cec5SDimitry Andric static std::string DwarfDebugProducer; 2700b57cec5SDimitry Andric static void setDwarfDebugProducer() { 2710b57cec5SDimitry Andric if(!getenv("DEBUG_PRODUCER")) 2720b57cec5SDimitry Andric return; 2730b57cec5SDimitry Andric DwarfDebugProducer += getenv("DEBUG_PRODUCER"); 2740b57cec5SDimitry Andric } 2750b57cec5SDimitry Andric 2760b57cec5SDimitry Andric static int AsLexInput(SourceMgr &SrcMgr, MCAsmInfo &MAI, 2770b57cec5SDimitry Andric raw_ostream &OS) { 2780b57cec5SDimitry Andric 2790b57cec5SDimitry Andric AsmLexer Lexer(MAI); 2800b57cec5SDimitry Andric Lexer.setBuffer(SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer()); 2810b57cec5SDimitry Andric 2820b57cec5SDimitry Andric bool Error = false; 2830b57cec5SDimitry Andric while (Lexer.Lex().isNot(AsmToken::Eof)) { 2840b57cec5SDimitry Andric Lexer.getTok().dump(OS); 2850b57cec5SDimitry Andric OS << "\n"; 2860b57cec5SDimitry Andric if (Lexer.getTok().getKind() == AsmToken::Error) 2870b57cec5SDimitry Andric Error = true; 2880b57cec5SDimitry Andric } 2890b57cec5SDimitry Andric 2900b57cec5SDimitry Andric return Error; 2910b57cec5SDimitry Andric } 2920b57cec5SDimitry Andric 2930b57cec5SDimitry Andric static int fillCommandLineSymbols(MCAsmParser &Parser) { 2940b57cec5SDimitry Andric for (auto &I: DefineSymbol) { 2950b57cec5SDimitry Andric auto Pair = StringRef(I).split('='); 2960b57cec5SDimitry Andric auto Sym = Pair.first; 2970b57cec5SDimitry Andric auto Val = Pair.second; 2980b57cec5SDimitry Andric 2990b57cec5SDimitry Andric if (Sym.empty() || Val.empty()) { 3000b57cec5SDimitry Andric WithColor::error() << "defsym must be of the form: sym=value: " << I 3010b57cec5SDimitry Andric << "\n"; 3020b57cec5SDimitry Andric return 1; 3030b57cec5SDimitry Andric } 3040b57cec5SDimitry Andric int64_t Value; 3050b57cec5SDimitry Andric if (Val.getAsInteger(0, Value)) { 3060b57cec5SDimitry Andric WithColor::error() << "value is not an integer: " << Val << "\n"; 3070b57cec5SDimitry Andric return 1; 3080b57cec5SDimitry Andric } 3090b57cec5SDimitry Andric Parser.getContext().setSymbolValue(Parser.getStreamer(), Sym, Value); 3100b57cec5SDimitry Andric } 3110b57cec5SDimitry Andric return 0; 3120b57cec5SDimitry Andric } 3130b57cec5SDimitry Andric 3140b57cec5SDimitry Andric static int AssembleInput(const char *ProgName, const Target *TheTarget, 3150b57cec5SDimitry Andric SourceMgr &SrcMgr, MCContext &Ctx, MCStreamer &Str, 3160b57cec5SDimitry Andric MCAsmInfo &MAI, MCSubtargetInfo &STI, 3178bcb0991SDimitry Andric MCInstrInfo &MCII, MCTargetOptions const &MCOptions) { 3180b57cec5SDimitry Andric std::unique_ptr<MCAsmParser> Parser( 3190b57cec5SDimitry Andric createMCAsmParser(SrcMgr, Ctx, Str, MAI)); 3200b57cec5SDimitry Andric std::unique_ptr<MCTargetAsmParser> TAP( 3210b57cec5SDimitry Andric TheTarget->createMCAsmParser(STI, *Parser, MCII, MCOptions)); 3220b57cec5SDimitry Andric 3230b57cec5SDimitry Andric if (!TAP) { 3240b57cec5SDimitry Andric WithColor::error(errs(), ProgName) 3250b57cec5SDimitry Andric << "this target does not support assembly parsing.\n"; 3260b57cec5SDimitry Andric return 1; 3270b57cec5SDimitry Andric } 3280b57cec5SDimitry Andric 3290b57cec5SDimitry Andric int SymbolResult = fillCommandLineSymbols(*Parser); 3300b57cec5SDimitry Andric if(SymbolResult) 3310b57cec5SDimitry Andric return SymbolResult; 3320b57cec5SDimitry Andric Parser->setShowParsedOperands(ShowInstOperands); 3330b57cec5SDimitry Andric Parser->setTargetParser(*TAP); 3340b57cec5SDimitry Andric Parser->getLexer().setLexMasmIntegers(LexMasmIntegers); 335e8d8bef9SDimitry Andric Parser->getLexer().setLexMasmHexFloats(LexMasmHexFloats); 336fe6060f1SDimitry Andric Parser->getLexer().setLexMotorolaIntegers(LexMotorolaIntegers); 3370b57cec5SDimitry Andric 3380b57cec5SDimitry Andric int Res = Parser->Run(NoInitialTextSection); 3390b57cec5SDimitry Andric 3400b57cec5SDimitry Andric return Res; 3410b57cec5SDimitry Andric } 3420b57cec5SDimitry Andric 3430b57cec5SDimitry Andric int main(int argc, char **argv) { 3440b57cec5SDimitry Andric InitLLVM X(argc, argv); 3450b57cec5SDimitry Andric 3460b57cec5SDimitry Andric // Initialize targets and assembly printers/parsers. 3470b57cec5SDimitry Andric llvm::InitializeAllTargetInfos(); 3480b57cec5SDimitry Andric llvm::InitializeAllTargetMCs(); 3490b57cec5SDimitry Andric llvm::InitializeAllAsmParsers(); 3500b57cec5SDimitry Andric llvm::InitializeAllDisassemblers(); 3510b57cec5SDimitry Andric 3520b57cec5SDimitry Andric // Register the target printer for --version. 3530b57cec5SDimitry Andric cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion); 3540b57cec5SDimitry Andric 355fe6060f1SDimitry Andric cl::HideUnrelatedOptions({&MCCategory, &getColorCategory()}); 3560b57cec5SDimitry Andric cl::ParseCommandLineOptions(argc, argv, "llvm machine code playground\n"); 357*0fca6ea1SDimitry Andric MCTargetOptions MCOptions = mc::InitMCTargetOptionsFromFlags(); 358*0fca6ea1SDimitry Andric MCOptions.CompressDebugSections = CompressDebugSections.getValue(); 359*0fca6ea1SDimitry Andric MCOptions.ShowMCInst = ShowInst; 360*0fca6ea1SDimitry Andric MCOptions.AsmVerbose = true; 361*0fca6ea1SDimitry Andric MCOptions.MCUseDwarfDirectory = MCTargetOptions::EnableDwarfDirectory; 3620b57cec5SDimitry Andric 363*0fca6ea1SDimitry Andric setDwarfDebugFlags(argc, argv); 3640b57cec5SDimitry Andric setDwarfDebugProducer(); 3650b57cec5SDimitry Andric 3660b57cec5SDimitry Andric const char *ProgName = argv[0]; 3670b57cec5SDimitry Andric const Target *TheTarget = GetTarget(ProgName); 3680b57cec5SDimitry Andric if (!TheTarget) 3690b57cec5SDimitry Andric return 1; 3700b57cec5SDimitry Andric // Now that GetTarget() has (potentially) replaced TripleName, it's safe to 3710b57cec5SDimitry Andric // construct the Triple object. 3720b57cec5SDimitry Andric Triple TheTriple(TripleName); 3730b57cec5SDimitry Andric 3740b57cec5SDimitry Andric ErrorOr<std::unique_ptr<MemoryBuffer>> BufferPtr = 375fe6060f1SDimitry Andric MemoryBuffer::getFileOrSTDIN(InputFilename, /*IsText=*/true); 3760b57cec5SDimitry Andric if (std::error_code EC = BufferPtr.getError()) { 3770b57cec5SDimitry Andric WithColor::error(errs(), ProgName) 3780b57cec5SDimitry Andric << InputFilename << ": " << EC.message() << '\n'; 3790b57cec5SDimitry Andric return 1; 3800b57cec5SDimitry Andric } 3810b57cec5SDimitry Andric MemoryBuffer *Buffer = BufferPtr->get(); 3820b57cec5SDimitry Andric 3830b57cec5SDimitry Andric SourceMgr SrcMgr; 3840b57cec5SDimitry Andric 3850b57cec5SDimitry Andric // Tell SrcMgr about this buffer, which is what the parser will pick up. 3860b57cec5SDimitry Andric SrcMgr.AddNewSourceBuffer(std::move(*BufferPtr), SMLoc()); 3870b57cec5SDimitry Andric 3880b57cec5SDimitry Andric // Record the location of the include directories so that the lexer can find 3890b57cec5SDimitry Andric // it later. 3900b57cec5SDimitry Andric SrcMgr.setIncludeDirs(IncludeDirs); 3910b57cec5SDimitry Andric 3920b57cec5SDimitry Andric std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName)); 3930b57cec5SDimitry Andric assert(MRI && "Unable to create target register info!"); 3940b57cec5SDimitry Andric 395480093f4SDimitry Andric std::unique_ptr<MCAsmInfo> MAI( 396480093f4SDimitry Andric TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions)); 3970b57cec5SDimitry Andric assert(MAI && "Unable to create target asm info!"); 3980b57cec5SDimitry Andric 3990b57cec5SDimitry Andric if (CompressDebugSections != DebugCompressionType::None) { 400bdd1243dSDimitry Andric if (const char *Reason = compression::getReasonIfUnsupported( 401bdd1243dSDimitry Andric compression::formatFor(CompressDebugSections))) { 4020b57cec5SDimitry Andric WithColor::error(errs(), ProgName) 403bdd1243dSDimitry Andric << "--compress-debug-sections: " << Reason; 4040b57cec5SDimitry Andric return 1; 4050b57cec5SDimitry Andric } 4060b57cec5SDimitry Andric } 4070b57cec5SDimitry Andric MAI->setPreserveAsmComments(PreserveComments); 4080b57cec5SDimitry Andric 409fe6060f1SDimitry Andric // Package up features to be passed to target/subtarget 410fe6060f1SDimitry Andric std::string FeaturesStr; 411fe6060f1SDimitry Andric if (MAttrs.size()) { 412fe6060f1SDimitry Andric SubtargetFeatures Features; 413fe6060f1SDimitry Andric for (unsigned i = 0; i != MAttrs.size(); ++i) 414fe6060f1SDimitry Andric Features.AddFeature(MAttrs[i]); 415fe6060f1SDimitry Andric FeaturesStr = Features.getString(); 416fe6060f1SDimitry Andric } 417fe6060f1SDimitry Andric 418fe6060f1SDimitry Andric std::unique_ptr<MCSubtargetInfo> STI( 419fe6060f1SDimitry Andric TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr)); 420fe6060f1SDimitry Andric assert(STI && "Unable to create subtarget info!"); 421fe6060f1SDimitry Andric 4220b57cec5SDimitry Andric // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and 4230b57cec5SDimitry Andric // MCObjectFileInfo needs a MCContext reference in order to initialize itself. 424fe6060f1SDimitry Andric MCContext Ctx(TheTriple, MAI.get(), MRI.get(), STI.get(), &SrcMgr, 425fe6060f1SDimitry Andric &MCOptions); 426fe6060f1SDimitry Andric std::unique_ptr<MCObjectFileInfo> MOFI( 427fe6060f1SDimitry Andric TheTarget->createMCObjectFileInfo(Ctx, PIC, LargeCodeModel)); 428fe6060f1SDimitry Andric Ctx.setObjectFileInfo(MOFI.get()); 4290b57cec5SDimitry Andric 4300b57cec5SDimitry Andric Ctx.setGenDwarfForAssembly(GenDwarfForAssembly); 4310b57cec5SDimitry Andric // Default to 4 for dwarf version. 4320b57cec5SDimitry Andric unsigned DwarfVersion = MCOptions.DwarfVersion ? MCOptions.DwarfVersion : 4; 4330b57cec5SDimitry Andric if (DwarfVersion < 2 || DwarfVersion > 5) { 4340b57cec5SDimitry Andric errs() << ProgName << ": Dwarf version " << DwarfVersion 4350b57cec5SDimitry Andric << " is not supported." << '\n'; 4360b57cec5SDimitry Andric return 1; 4370b57cec5SDimitry Andric } 4380b57cec5SDimitry Andric Ctx.setDwarfVersion(DwarfVersion); 4395ffd83dbSDimitry Andric if (MCOptions.Dwarf64) { 4405ffd83dbSDimitry Andric // The 64-bit DWARF format was introduced in DWARFv3. 4415ffd83dbSDimitry Andric if (DwarfVersion < 3) { 4425ffd83dbSDimitry Andric errs() << ProgName 4435ffd83dbSDimitry Andric << ": the 64-bit DWARF format is not supported for DWARF versions " 4445ffd83dbSDimitry Andric "prior to 3\n"; 4455ffd83dbSDimitry Andric return 1; 4465ffd83dbSDimitry Andric } 4475ffd83dbSDimitry Andric // 32-bit targets don't support DWARF64, which requires 64-bit relocations. 4485ffd83dbSDimitry Andric if (MAI->getCodePointerSize() < 8) { 4495ffd83dbSDimitry Andric errs() << ProgName 4505ffd83dbSDimitry Andric << ": the 64-bit DWARF format is only supported for 64-bit " 4515ffd83dbSDimitry Andric "targets\n"; 4525ffd83dbSDimitry Andric return 1; 4535ffd83dbSDimitry Andric } 4545ffd83dbSDimitry Andric // If needsDwarfSectionOffsetDirective is true, we would eventually call 4555ffd83dbSDimitry Andric // MCStreamer::emitSymbolValue() with IsSectionRelative = true, but that 4565ffd83dbSDimitry Andric // is supported only for 4-byte long references. 4575ffd83dbSDimitry Andric if (MAI->needsDwarfSectionOffsetDirective()) { 4585ffd83dbSDimitry Andric errs() << ProgName << ": the 64-bit DWARF format is not supported for " 4595ffd83dbSDimitry Andric << TheTriple.normalize() << "\n"; 4605ffd83dbSDimitry Andric return 1; 4615ffd83dbSDimitry Andric } 4625ffd83dbSDimitry Andric Ctx.setDwarfFormat(dwarf::DWARF64); 4635ffd83dbSDimitry Andric } 4640b57cec5SDimitry Andric if (!DwarfDebugFlags.empty()) 4650b57cec5SDimitry Andric Ctx.setDwarfDebugFlags(StringRef(DwarfDebugFlags)); 4660b57cec5SDimitry Andric if (!DwarfDebugProducer.empty()) 4670b57cec5SDimitry Andric Ctx.setDwarfDebugProducer(StringRef(DwarfDebugProducer)); 4680b57cec5SDimitry Andric if (!DebugCompilationDir.empty()) 4690b57cec5SDimitry Andric Ctx.setCompilationDir(DebugCompilationDir); 4700b57cec5SDimitry Andric else { 4710b57cec5SDimitry Andric // If no compilation dir is set, try to use the current directory. 4720b57cec5SDimitry Andric SmallString<128> CWD; 4730b57cec5SDimitry Andric if (!sys::fs::current_path(CWD)) 4740b57cec5SDimitry Andric Ctx.setCompilationDir(CWD); 4750b57cec5SDimitry Andric } 4760b57cec5SDimitry Andric for (const auto &Arg : DebugPrefixMap) { 4770b57cec5SDimitry Andric const auto &KV = StringRef(Arg).split('='); 4785ffd83dbSDimitry Andric Ctx.addDebugPrefixMapEntry(std::string(KV.first), std::string(KV.second)); 4790b57cec5SDimitry Andric } 4800b57cec5SDimitry Andric if (!MainFileName.empty()) 4810b57cec5SDimitry Andric Ctx.setMainFileName(MainFileName); 4820b57cec5SDimitry Andric if (GenDwarfForAssembly) 4830b57cec5SDimitry Andric Ctx.setGenDwarfRootFile(InputFilename, Buffer->getBuffer()); 4840b57cec5SDimitry Andric 485fe6060f1SDimitry Andric sys::fs::OpenFlags Flags = (FileType == OFT_AssemblyFile) 486fe6060f1SDimitry Andric ? sys::fs::OF_TextWithCRLF 4878bcb0991SDimitry Andric : sys::fs::OF_None; 4888bcb0991SDimitry Andric std::unique_ptr<ToolOutputFile> Out = GetOutputStream(OutputFilename, Flags); 4890b57cec5SDimitry Andric if (!Out) 4900b57cec5SDimitry Andric return 1; 4910b57cec5SDimitry Andric 4920b57cec5SDimitry Andric std::unique_ptr<ToolOutputFile> DwoOut; 4930b57cec5SDimitry Andric if (!SplitDwarfFile.empty()) { 4940b57cec5SDimitry Andric if (FileType != OFT_ObjectFile) { 4950b57cec5SDimitry Andric WithColor::error() << "dwo output only supported with object files\n"; 4960b57cec5SDimitry Andric return 1; 4970b57cec5SDimitry Andric } 4988bcb0991SDimitry Andric DwoOut = GetOutputStream(SplitDwarfFile, sys::fs::OF_None); 4990b57cec5SDimitry Andric if (!DwoOut) 5000b57cec5SDimitry Andric return 1; 5010b57cec5SDimitry Andric } 5020b57cec5SDimitry Andric 5030b57cec5SDimitry Andric std::unique_ptr<buffer_ostream> BOS; 5040b57cec5SDimitry Andric raw_pwrite_stream *OS = &Out->os(); 5050b57cec5SDimitry Andric std::unique_ptr<MCStreamer> Str; 5060b57cec5SDimitry Andric 5070b57cec5SDimitry Andric std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo()); 508e8d8bef9SDimitry Andric assert(MCII && "Unable to create instruction info!"); 509e8d8bef9SDimitry Andric 5100b57cec5SDimitry Andric MCInstPrinter *IP = nullptr; 5110b57cec5SDimitry Andric if (FileType == OFT_AssemblyFile) { 5120b57cec5SDimitry Andric IP = TheTarget->createMCInstPrinter(Triple(TripleName), OutputAsmVariant, 5130b57cec5SDimitry Andric *MAI, *MCII, *MRI); 5140b57cec5SDimitry Andric 5150b57cec5SDimitry Andric if (!IP) { 5160b57cec5SDimitry Andric WithColor::error() 5170b57cec5SDimitry Andric << "unable to create instruction printer for target triple '" 5180b57cec5SDimitry Andric << TheTriple.normalize() << "' with assembly variant " 5190b57cec5SDimitry Andric << OutputAsmVariant << ".\n"; 5200b57cec5SDimitry Andric return 1; 5210b57cec5SDimitry Andric } 5220b57cec5SDimitry Andric 523fe6060f1SDimitry Andric for (StringRef Opt : DisassemblerOptions) 524fe6060f1SDimitry Andric if (!IP->applyTargetSpecificCLOption(Opt)) { 525fe6060f1SDimitry Andric WithColor::error() << "invalid disassembler option '" << Opt << "'\n"; 526fe6060f1SDimitry Andric return 1; 527fe6060f1SDimitry Andric } 528fe6060f1SDimitry Andric 5290b57cec5SDimitry Andric // Set the display preference for hex vs. decimal immediates. 5300b57cec5SDimitry Andric IP->setPrintImmHex(PrintImmHex); 5310b57cec5SDimitry Andric 5320b57cec5SDimitry Andric // Set up the AsmStreamer. 5330b57cec5SDimitry Andric std::unique_ptr<MCCodeEmitter> CE; 5340b57cec5SDimitry Andric if (ShowEncoding) 53581ad6265SDimitry Andric CE.reset(TheTarget->createMCCodeEmitter(*MCII, Ctx)); 5360b57cec5SDimitry Andric 5370b57cec5SDimitry Andric std::unique_ptr<MCAsmBackend> MAB( 5380b57cec5SDimitry Andric TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions)); 5398bcb0991SDimitry Andric auto FOut = std::make_unique<formatted_raw_ostream>(*OS); 540*0fca6ea1SDimitry Andric Str.reset(TheTarget->createAsmStreamer(Ctx, std::move(FOut), IP, 541*0fca6ea1SDimitry Andric std::move(CE), std::move(MAB))); 5420b57cec5SDimitry Andric 5430b57cec5SDimitry Andric } else if (FileType == OFT_Null) { 5440b57cec5SDimitry Andric Str.reset(TheTarget->createNullStreamer(Ctx)); 5450b57cec5SDimitry Andric } else { 5460b57cec5SDimitry Andric assert(FileType == OFT_ObjectFile && "Invalid file type!"); 5470b57cec5SDimitry Andric 5480b57cec5SDimitry Andric if (!Out->os().supportsSeeking()) { 5498bcb0991SDimitry Andric BOS = std::make_unique<buffer_ostream>(Out->os()); 5500b57cec5SDimitry Andric OS = BOS.get(); 5510b57cec5SDimitry Andric } 5520b57cec5SDimitry Andric 55381ad6265SDimitry Andric MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, Ctx); 5540b57cec5SDimitry Andric MCAsmBackend *MAB = TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions); 5550b57cec5SDimitry Andric Str.reset(TheTarget->createMCObjectStreamer( 5560b57cec5SDimitry Andric TheTriple, Ctx, std::unique_ptr<MCAsmBackend>(MAB), 5570b57cec5SDimitry Andric DwoOut ? MAB->createDwoObjectWriter(*OS, DwoOut->os()) 5580b57cec5SDimitry Andric : MAB->createObjectWriter(*OS), 559*0fca6ea1SDimitry Andric std::unique_ptr<MCCodeEmitter>(CE), *STI)); 5600b57cec5SDimitry Andric if (NoExecStack) 561349cc55cSDimitry Andric Str->initSections(true, *STI); 5620b57cec5SDimitry Andric } 5630b57cec5SDimitry Andric 5640b57cec5SDimitry Andric int Res = 1; 5650b57cec5SDimitry Andric bool disassemble = false; 5660b57cec5SDimitry Andric switch (Action) { 5670b57cec5SDimitry Andric case AC_AsLex: 5680b57cec5SDimitry Andric Res = AsLexInput(SrcMgr, *MAI, Out->os()); 5690b57cec5SDimitry Andric break; 5700b57cec5SDimitry Andric case AC_Assemble: 5710b57cec5SDimitry Andric Res = AssembleInput(ProgName, TheTarget, SrcMgr, Ctx, *Str, *MAI, *STI, 5720b57cec5SDimitry Andric *MCII, MCOptions); 5730b57cec5SDimitry Andric break; 5740b57cec5SDimitry Andric case AC_MDisassemble: 5758bcb0991SDimitry Andric IP->setUseMarkup(true); 5760b57cec5SDimitry Andric disassemble = true; 5770b57cec5SDimitry Andric break; 5785f757f3fSDimitry Andric case AC_CDisassemble: 5795f757f3fSDimitry Andric IP->setUseColor(true); 5805f757f3fSDimitry Andric disassemble = true; 5815f757f3fSDimitry Andric break; 5820b57cec5SDimitry Andric case AC_Disassemble: 5830b57cec5SDimitry Andric disassemble = true; 5840b57cec5SDimitry Andric break; 5850b57cec5SDimitry Andric } 5860b57cec5SDimitry Andric if (disassemble) 5878bcb0991SDimitry Andric Res = Disassembler::disassemble(*TheTarget, TripleName, *STI, *Str, *Buffer, 5885f757f3fSDimitry Andric SrcMgr, Ctx, MCOptions); 5890b57cec5SDimitry Andric 5900b57cec5SDimitry Andric // Keep output if no errors. 5910b57cec5SDimitry Andric if (Res == 0) { 5920b57cec5SDimitry Andric Out->keep(); 5930b57cec5SDimitry Andric if (DwoOut) 5940b57cec5SDimitry Andric DwoOut->keep(); 5950b57cec5SDimitry Andric } 5960b57cec5SDimitry Andric return Res; 5970b57cec5SDimitry Andric } 598