Eugene Zelenko | 4e036ff | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 1 | //===-- llvm-mc.cpp - Machine Code Hacking Driver ---------------*- C++ -*-===// |
Chris Lattner | f9f065e | 2009-06-18 23:04:45 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | bb4688a | 2009-06-18 23:05:21 +0000 | [diff] [blame] | 10 | // This utility is a simple driver that allows command line hacking on machine |
| 11 | // code. |
Chris Lattner | f9f065e | 2009-06-18 23:04:45 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 15 | #include "Disassembler.h" |
Evan Cheng | 78c10ee | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCAsmBackend.h" |
Craig Topper | fb22ede | 2012-04-15 22:00:22 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCAsmInfo.h" |
Lang Hames | 806f68b | 2017-10-11 23:34:47 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCCodeEmitter.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCContext.h" |
Chris Lattner | 90edac0 | 2009-09-14 03:02:37 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCInstPrinter.h" |
Evan Cheng | 59ee62d | 2011-07-11 03:57:24 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCInstrInfo.h" |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCObjectFileInfo.h" |
Peter Collingbourne | 17a9814 | 2018-05-18 18:26:45 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCObjectWriter.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCParser/AsmLexer.h" |
Benjamin Kramer | 030c9dc | 2016-01-27 10:01:28 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCParser/MCTargetAsmParser.h" |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCRegisterInfo.h" |
Chris Lattner | cbc23f7 | 2009-06-24 00:52:40 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCStreamer.h" |
Evan Cheng | ffc0e73 | 2011-07-09 05:47:46 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCSubtargetInfo.h" |
David Blaikie | 461bf52 | 2018-04-11 18:49:37 +0000 | [diff] [blame] | 29 | #include "llvm/MC/MCTargetOptionsCommandFlags.inc" |
Chris Lattner | f9f065e | 2009-06-18 23:04:45 +0000 | [diff] [blame] | 30 | #include "llvm/Support/CommandLine.h" |
David Blaikie | 8a21cb2 | 2014-03-28 21:00:25 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Compression.h" |
Dan Gohman | d5826a3 | 2010-08-20 01:07:01 +0000 | [diff] [blame] | 32 | #include "llvm/Support/FileUtilities.h" |
Daniel Dunbar | c22e0b2 | 2009-08-14 03:48:55 +0000 | [diff] [blame] | 33 | #include "llvm/Support/FormattedStream.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Host.h" |
Rui Ueyama | 0b9d56a | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 35 | #include "llvm/Support/InitLLVM.h" |
Chris Lattner | f9f065e | 2009-06-18 23:04:45 +0000 | [diff] [blame] | 36 | #include "llvm/Support/MemoryBuffer.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 37 | #include "llvm/Support/SourceMgr.h" |
Evan Cheng | 3e74d6f | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 38 | #include "llvm/Support/TargetRegistry.h" |
| 39 | #include "llvm/Support/TargetSelect.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 40 | #include "llvm/Support/ToolOutputFile.h" |
Jonas Devlieghere | aa9bcfc | 2018-04-22 08:01:35 +0000 | [diff] [blame] | 41 | #include "llvm/Support/WithColor.h" |
Eugene Zelenko | 4e036ff | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 42 | |
Chris Lattner | f9f065e | 2009-06-18 23:04:45 +0000 | [diff] [blame] | 43 | using namespace llvm; |
| 44 | |
| 45 | static cl::opt<std::string> |
| 46 | InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-")); |
| 47 | |
Peter Collingbourne | 60fd99d | 2018-05-21 19:44:54 +0000 | [diff] [blame] | 48 | static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"), |
| 49 | cl::value_desc("filename"), |
| 50 | cl::init("-")); |
| 51 | |
| 52 | static cl::opt<std::string> SplitDwarfFile("split-dwarf-file", |
| 53 | cl::desc("DWO output filename"), |
| 54 | cl::value_desc("filename")); |
Chris Lattner | f9f065e | 2009-06-18 23:04:45 +0000 | [diff] [blame] | 55 | |
Daniel Dunbar | f2f6b0c | 2009-08-27 07:56:39 +0000 | [diff] [blame] | 56 | static cl::opt<bool> |
| 57 | ShowEncoding("show-encoding", cl::desc("Show instruction encodings")); |
| 58 | |
Rafael Espindola | 3be11e6 | 2016-06-17 17:04:56 +0000 | [diff] [blame] | 59 | static cl::opt<bool> RelaxELFRel( |
| 60 | "relax-relocations", cl::init(true), |
| 61 | cl::desc("Emit R_X86_64_GOTPCRELX instead of R_X86_64_GOTPCREL")); |
Rafael Espindola | c615a82 | 2016-05-29 01:11:00 +0000 | [diff] [blame] | 62 | |
Saleem Abdulrasool | e6be41f | 2017-06-09 00:40:19 +0000 | [diff] [blame] | 63 | static cl::opt<DebugCompressionType> CompressDebugSections( |
| 64 | "compress-debug-sections", cl::ValueOptional, |
| 65 | cl::init(DebugCompressionType::None), |
| 66 | cl::desc("Choose DWARF debug sections compression:"), |
| 67 | cl::values(clEnumValN(DebugCompressionType::None, "none", "No compression"), |
| 68 | clEnumValN(DebugCompressionType::Z, "zlib", |
| 69 | "Use zlib compression"), |
| 70 | clEnumValN(DebugCompressionType::GNU, "zlib-gnu", |
| 71 | "Use zlib-gnu compression (deprecated)"))); |
David Blaikie | 0a0c4cc | 2014-03-27 20:45:58 +0000 | [diff] [blame] | 72 | |
| 73 | static cl::opt<bool> |
Daniel Dunbar | 9dee8e3 | 2010-02-03 18:18:30 +0000 | [diff] [blame] | 74 | ShowInst("show-inst", cl::desc("Show internal instruction representation")); |
| 75 | |
Daniel Dunbar | 3c14ca4 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 76 | static cl::opt<bool> |
| 77 | ShowInstOperands("show-inst-operands", |
| 78 | cl::desc("Show instructions operands as parsed")); |
| 79 | |
Chris Lattner | e895c61 | 2009-09-20 07:17:49 +0000 | [diff] [blame] | 80 | static cl::opt<unsigned> |
| 81 | OutputAsmVariant("output-asm-variant", |
| 82 | cl::desc("Syntax variant to use for output printing")); |
| 83 | |
Jim Grosbach | 7ca0567 | 2014-06-11 20:26:40 +0000 | [diff] [blame] | 84 | static cl::opt<bool> |
| 85 | PrintImmHex("print-imm-hex", cl::init(false), |
| 86 | cl::desc("Prefer hex format for immediate values")); |
| 87 | |
Colin LeMahieu | 4514db2 | 2015-06-07 01:46:24 +0000 | [diff] [blame] | 88 | static cl::list<std::string> |
| 89 | DefineSymbol("defsym", cl::desc("Defines a symbol to be an integer constant")); |
| 90 | |
Nirav Dave | aee0f14 | 2016-07-11 12:42:14 +0000 | [diff] [blame] | 91 | static cl::opt<bool> |
| 92 | PreserveComments("preserve-comments", |
| 93 | cl::desc("Preserve Comments in outputted assembly")); |
| 94 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 95 | enum OutputFileType { |
Daniel Dunbar | 2d9f5d1 | 2010-03-23 23:47:12 +0000 | [diff] [blame] | 96 | OFT_Null, |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 97 | OFT_AssemblyFile, |
| 98 | OFT_ObjectFile |
| 99 | }; |
| 100 | static cl::opt<OutputFileType> |
| 101 | FileType("filetype", cl::init(OFT_AssemblyFile), |
| 102 | cl::desc("Choose an output file type:"), |
| 103 | cl::values( |
| 104 | clEnumValN(OFT_AssemblyFile, "asm", |
| 105 | "Emit an assembly ('.s') file"), |
Daniel Dunbar | 2d9f5d1 | 2010-03-23 23:47:12 +0000 | [diff] [blame] | 106 | clEnumValN(OFT_Null, "null", |
| 107 | "Don't emit anything (for timing purposes)"), |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 108 | clEnumValN(OFT_ObjectFile, "obj", |
Mehdi Amini | 3ffe113 | 2016-10-08 19:41:06 +0000 | [diff] [blame] | 109 | "Emit a native object ('.o') file"))); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 110 | |
Chris Lattner | b23677e | 2009-06-21 05:22:37 +0000 | [diff] [blame] | 111 | static cl::list<std::string> |
| 112 | IncludeDirs("I", cl::desc("Directory of include files"), |
| 113 | cl::value_desc("directory"), cl::Prefix); |
Chris Lattner | f9f065e | 2009-06-18 23:04:45 +0000 | [diff] [blame] | 114 | |
Daniel Dunbar | b4b53e5 | 2009-07-17 22:38:58 +0000 | [diff] [blame] | 115 | static cl::opt<std::string> |
Daniel Dunbar | 181ab6a | 2010-03-13 02:20:38 +0000 | [diff] [blame] | 116 | ArchName("arch", cl::desc("Target arch to assemble for, " |
Bill Wendling | 916a94b | 2011-06-17 20:35:21 +0000 | [diff] [blame] | 117 | "see -version for available targets")); |
Daniel Dunbar | 181ab6a | 2010-03-13 02:20:38 +0000 | [diff] [blame] | 118 | |
| 119 | static cl::opt<std::string> |
Nick Lewycky | ed1f168 | 2009-11-01 22:07:54 +0000 | [diff] [blame] | 120 | TripleName("triple", cl::desc("Target triple to assemble for, " |
Daniel Dunbar | 181ab6a | 2010-03-13 02:20:38 +0000 | [diff] [blame] | 121 | "see -version for available targets")); |
Daniel Dunbar | b4b53e5 | 2009-07-17 22:38:58 +0000 | [diff] [blame] | 122 | |
Jim Grosbach | d2f1425 | 2010-10-30 15:57:50 +0000 | [diff] [blame] | 123 | static cl::opt<std::string> |
| 124 | MCPU("mcpu", |
| 125 | cl::desc("Target a specific cpu type (-mcpu=help for details)"), |
| 126 | cl::value_desc("cpu-name"), |
| 127 | cl::init("")); |
| 128 | |
James Molloy | b950585 | 2011-09-07 17:24:38 +0000 | [diff] [blame] | 129 | static cl::list<std::string> |
| 130 | MAttrs("mattr", |
| 131 | cl::CommaSeparated, |
| 132 | cl::desc("Target specific attributes (-mattr=help for details)"), |
| 133 | cl::value_desc("a1,+a2,-a3,...")); |
| 134 | |
Rafael Espindola | 2dc6371 | 2016-05-18 11:58:50 +0000 | [diff] [blame] | 135 | static cl::opt<bool> PIC("position-independent", |
| 136 | cl::desc("Position independent"), cl::init(false)); |
Evan Cheng | 4396613 | 2011-07-19 06:37:02 +0000 | [diff] [blame] | 137 | |
Rafael Espindola | 2600a67 | 2017-08-02 20:32:26 +0000 | [diff] [blame] | 138 | static cl::opt<bool> |
| 139 | LargeCodeModel("large-code-model", |
| 140 | cl::desc("Create cfi directives that assume the code might " |
| 141 | "be more than 2gb away")); |
Evan Cheng | 34ad6db | 2011-07-20 07:51:56 +0000 | [diff] [blame] | 142 | |
Daniel Dunbar | 5e6a7a2 | 2010-03-13 02:20:57 +0000 | [diff] [blame] | 143 | static cl::opt<bool> |
Bill Wendling | 916a94b | 2011-06-17 20:35:21 +0000 | [diff] [blame] | 144 | NoInitialTextSection("n", cl::desc("Don't assume assembly file starts " |
| 145 | "in the text section")); |
Daniel Dunbar | 5e6a7a2 | 2010-03-13 02:20:57 +0000 | [diff] [blame] | 146 | |
Daniel Dunbar | c6cf43d | 2011-03-28 22:49:15 +0000 | [diff] [blame] | 147 | static cl::opt<bool> |
Kevin Enderby | 613b757 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 148 | GenDwarfForAssembly("g", cl::desc("Generate dwarf debugging info for assembly " |
| 149 | "source files")); |
| 150 | |
Chandler Carruth | 6c31d31 | 2012-12-17 21:32:42 +0000 | [diff] [blame] | 151 | static cl::opt<std::string> |
| 152 | DebugCompilationDir("fdebug-compilation-dir", |
| 153 | cl::desc("Specifies the debug info's compilation dir")); |
| 154 | |
Paul Robinson | 4575d34 | 2018-07-10 14:41:54 +0000 | [diff] [blame] | 155 | static cl::list<std::string> |
| 156 | DebugPrefixMap("fdebug-prefix-map", |
| 157 | cl::desc("Map file source paths in debug info"), |
| 158 | cl::value_desc("= separated key-value pairs")); |
| 159 | |
Eric Christopher | 6c58314 | 2012-12-18 00:31:01 +0000 | [diff] [blame] | 160 | static cl::opt<std::string> |
| 161 | MainFileName("main-file-name", |
| 162 | cl::desc("Specifies the name we should consider the input file")); |
| 163 | |
Eric Christopher | e648d75 | 2014-05-21 00:20:01 +0000 | [diff] [blame] | 164 | static cl::opt<bool> SaveTempLabels("save-temp-labels", |
| 165 | cl::desc("Don't discard temporary labels")); |
| 166 | |
Reid Kleckner | 3435941 | 2018-10-24 20:23:57 +0000 | [diff] [blame] | 167 | static cl::opt<bool> LexMasmIntegers( |
| 168 | "masm-integers", |
| 169 | cl::desc("Enable binary and hex masm integers (0b110 and 0ABCh)")); |
| 170 | |
Eric Christopher | d1b5bda | 2014-05-21 21:05:09 +0000 | [diff] [blame] | 171 | static cl::opt<bool> NoExecStack("no-exec-stack", |
| 172 | cl::desc("File doesn't need an exec stack")); |
| 173 | |
Chris Lattner | b23677e | 2009-06-21 05:22:37 +0000 | [diff] [blame] | 174 | enum ActionType { |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 175 | AC_AsLex, |
Sean Callanan | ba847da | 2009-12-17 01:49:59 +0000 | [diff] [blame] | 176 | AC_Assemble, |
Sean Callanan | 668b154 | 2010-04-12 19:43:00 +0000 | [diff] [blame] | 177 | AC_Disassemble, |
Kevin Enderby | 14ccc90 | 2012-12-05 18:13:19 +0000 | [diff] [blame] | 178 | AC_MDisassemble, |
Chris Lattner | b23677e | 2009-06-21 05:22:37 +0000 | [diff] [blame] | 179 | }; |
Chris Lattner | f9f065e | 2009-06-18 23:04:45 +0000 | [diff] [blame] | 180 | |
Chris Lattner | b23677e | 2009-06-21 05:22:37 +0000 | [diff] [blame] | 181 | static cl::opt<ActionType> |
| 182 | Action(cl::desc("Action to perform:"), |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 183 | cl::init(AC_Assemble), |
| 184 | cl::values(clEnumValN(AC_AsLex, "as-lex", |
| 185 | "Lex tokens from a .s file"), |
| 186 | clEnumValN(AC_Assemble, "assemble", |
Chris Lattner | b23677e | 2009-06-21 05:22:37 +0000 | [diff] [blame] | 187 | "Assemble a .s file (default)"), |
Sean Callanan | ba847da | 2009-12-17 01:49:59 +0000 | [diff] [blame] | 188 | clEnumValN(AC_Disassemble, "disassemble", |
| 189 | "Disassemble strings of hex bytes"), |
Kevin Enderby | 3ed0316 | 2012-10-22 22:31:46 +0000 | [diff] [blame] | 190 | clEnumValN(AC_MDisassemble, "mdis", |
Mehdi Amini | 3ffe113 | 2016-10-08 19:41:06 +0000 | [diff] [blame] | 191 | "Marked up disassembly of strings of hex bytes"))); |
Chris Lattner | b23677e | 2009-06-21 05:22:37 +0000 | [diff] [blame] | 192 | |
Kevin Enderby | 9823ca9 | 2009-09-04 21:45:34 +0000 | [diff] [blame] | 193 | static const Target *GetTarget(const char *ProgName) { |
Daniel Dunbar | 181ab6a | 2010-03-13 02:20:38 +0000 | [diff] [blame] | 194 | // Figure out the target triple. |
| 195 | if (TripleName.empty()) |
Sebastian Pop | 0173864 | 2011-11-01 21:32:20 +0000 | [diff] [blame] | 196 | TripleName = sys::getDefaultTargetTriple(); |
Evan Cheng | d6dcf39 | 2011-07-26 19:02:16 +0000 | [diff] [blame] | 197 | Triple TheTriple(Triple::normalize(TripleName)); |
| 198 | |
Kevin Enderby | 9ed9e5d | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 199 | // Get the target specific parser. |
| 200 | std::string Error; |
| 201 | const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple, |
| 202 | Error); |
| 203 | if (!TheTarget) { |
Jonas Devlieghere | aa9bcfc | 2018-04-22 08:01:35 +0000 | [diff] [blame] | 204 | WithColor::error(errs(), ProgName) << Error; |
Craig Topper | 573faec | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 205 | return nullptr; |
Daniel Dunbar | 181ab6a | 2010-03-13 02:20:38 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Kevin Enderby | 9ed9e5d | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 208 | // Update the triple name and return the found target. |
Evan Cheng | d6dcf39 | 2011-07-26 19:02:16 +0000 | [diff] [blame] | 209 | TripleName = TheTriple.getTriple(); |
| 210 | return TheTarget; |
Kevin Enderby | 9823ca9 | 2009-09-04 21:45:34 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Peter Collingbourne | 60fd99d | 2018-05-21 19:44:54 +0000 | [diff] [blame] | 213 | static std::unique_ptr<ToolOutputFile> GetOutputStream(StringRef Path) { |
Rafael Espindola | 8c96862 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 214 | std::error_code EC; |
Peter Collingbourne | 60fd99d | 2018-05-21 19:44:54 +0000 | [diff] [blame] | 215 | auto Out = llvm::make_unique<ToolOutputFile>(Path, EC, sys::fs::F_None); |
Rafael Espindola | 8c96862 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 216 | if (EC) { |
Jonas Devlieghere | aa9bcfc | 2018-04-22 08:01:35 +0000 | [diff] [blame] | 217 | WithColor::error() << EC.message() << '\n'; |
Craig Topper | 573faec | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 218 | return nullptr; |
Dan Gohman | d5826a3 | 2010-08-20 01:07:01 +0000 | [diff] [blame] | 219 | } |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 220 | |
| 221 | return Out; |
Dan Gohman | d5826a3 | 2010-08-20 01:07:01 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 224 | static std::string DwarfDebugFlags; |
| 225 | static void setDwarfDebugFlags(int argc, char **argv) { |
| 226 | if (!getenv("RC_DEBUG_OPTIONS")) |
| 227 | return; |
| 228 | for (int i = 0; i < argc; i++) { |
| 229 | DwarfDebugFlags += argv[i]; |
| 230 | if (i + 1 < argc) |
| 231 | DwarfDebugFlags += " "; |
| 232 | } |
| 233 | } |
| 234 | |
Kevin Enderby | 75c9b93 | 2013-01-16 17:46:23 +0000 | [diff] [blame] | 235 | static std::string DwarfDebugProducer; |
Eugene Zelenko | 4e036ff | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 236 | static void setDwarfDebugProducer() { |
Kevin Enderby | 75c9b93 | 2013-01-16 17:46:23 +0000 | [diff] [blame] | 237 | if(!getenv("DEBUG_PRODUCER")) |
| 238 | return; |
| 239 | DwarfDebugProducer += getenv("DEBUG_PRODUCER"); |
| 240 | } |
| 241 | |
Eric Christopher | e793899 | 2014-06-19 06:22:01 +0000 | [diff] [blame] | 242 | static int AsLexInput(SourceMgr &SrcMgr, MCAsmInfo &MAI, |
Craig Topper | 2c86ed8 | 2014-12-12 07:52:19 +0000 | [diff] [blame] | 243 | raw_ostream &OS) { |
Chris Lattner | b23677e | 2009-06-21 05:22:37 +0000 | [diff] [blame] | 244 | |
Richard Barton | d0c478d | 2012-04-16 11:32:10 +0000 | [diff] [blame] | 245 | AsmLexer Lexer(MAI); |
Rafael Espindola | 26a84a6 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 246 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer()); |
Daniel Dunbar | 346cc61 | 2010-07-18 18:31:28 +0000 | [diff] [blame] | 247 | |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 248 | bool Error = false; |
Daniel Dunbar | a3c924f | 2009-07-28 16:56:42 +0000 | [diff] [blame] | 249 | while (Lexer.Lex().isNot(AsmToken::Eof)) { |
Oliver Stannard | 3c24f8a | 2018-03-06 14:02:14 +0000 | [diff] [blame] | 250 | Lexer.getTok().dump(OS); |
| 251 | OS << "\n"; |
| 252 | if (Lexer.getTok().getKind() == AsmToken::Error) |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 253 | Error = true; |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 254 | } |
Dan Gohman | d5826a3 | 2010-08-20 01:07:01 +0000 | [diff] [blame] | 255 | |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 256 | return Error; |
Chris Lattner | b23677e | 2009-06-21 05:22:37 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Mandeep Singh Grang | 017b7af | 2016-12-01 18:42:04 +0000 | [diff] [blame] | 259 | static int fillCommandLineSymbols(MCAsmParser &Parser) { |
Mandeep Singh Grang | 9196899 | 2016-12-06 02:49:17 +0000 | [diff] [blame] | 260 | for (auto &I: DefineSymbol) { |
| 261 | auto Pair = StringRef(I).split('='); |
| 262 | auto Sym = Pair.first; |
| 263 | auto Val = Pair.second; |
| 264 | |
| 265 | if (Sym.empty() || Val.empty()) { |
Jonas Devlieghere | aa9bcfc | 2018-04-22 08:01:35 +0000 | [diff] [blame] | 266 | WithColor::error() << "defsym must be of the form: sym=value: " << I |
| 267 | << "\n"; |
Colin LeMahieu | 4514db2 | 2015-06-07 01:46:24 +0000 | [diff] [blame] | 268 | return 1; |
Mandeep Singh Grang | 9196899 | 2016-12-06 02:49:17 +0000 | [diff] [blame] | 269 | } |
| 270 | int64_t Value; |
| 271 | if (Val.getAsInteger(0, Value)) { |
Jonas Devlieghere | aa9bcfc | 2018-04-22 08:01:35 +0000 | [diff] [blame] | 272 | WithColor::error() << "value is not an integer: " << Val << "\n"; |
Mandeep Singh Grang | 9196899 | 2016-12-06 02:49:17 +0000 | [diff] [blame] | 273 | return 1; |
| 274 | } |
| 275 | Parser.getContext().setSymbolValue(Parser.getStreamer(), Sym, Value); |
| 276 | } |
Colin LeMahieu | 4514db2 | 2015-06-07 01:46:24 +0000 | [diff] [blame] | 277 | return 0; |
| 278 | } |
| 279 | |
NAKAMURA Takumi | ad8d69b | 2014-01-22 03:12:43 +0000 | [diff] [blame] | 280 | static int AssembleInput(const char *ProgName, const Target *TheTarget, |
Richard Barton | d0c478d | 2012-04-16 11:32:10 +0000 | [diff] [blame] | 281 | SourceMgr &SrcMgr, MCContext &Ctx, MCStreamer &Str, |
Eric Christopher | e793899 | 2014-06-19 06:22:01 +0000 | [diff] [blame] | 282 | MCAsmInfo &MAI, MCSubtargetInfo &STI, |
Eric Christopher | 179bb4e | 2014-06-19 06:22:08 +0000 | [diff] [blame] | 283 | MCInstrInfo &MCII, MCTargetOptions &MCOptions) { |
Evgeniy Stepanov | d6af41b | 2014-04-23 11:16:03 +0000 | [diff] [blame] | 284 | std::unique_ptr<MCAsmParser> Parser( |
| 285 | createMCAsmParser(SrcMgr, Ctx, Str, MAI)); |
Ahmed Charles | f4ccd11 | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 286 | std::unique_ptr<MCTargetAsmParser> TAP( |
Eric Christopher | 179bb4e | 2014-06-19 06:22:08 +0000 | [diff] [blame] | 287 | TheTarget->createMCAsmParser(STI, *Parser, MCII, MCOptions)); |
| 288 | |
Richard Barton | d0c478d | 2012-04-16 11:32:10 +0000 | [diff] [blame] | 289 | if (!TAP) { |
Jonas Devlieghere | aa9bcfc | 2018-04-22 08:01:35 +0000 | [diff] [blame] | 290 | WithColor::error(errs(), ProgName) |
| 291 | << "this target does not support assembly parsing.\n"; |
Richard Barton | d0c478d | 2012-04-16 11:32:10 +0000 | [diff] [blame] | 292 | return 1; |
| 293 | } |
| 294 | |
Colin LeMahieu | 4514db2 | 2015-06-07 01:46:24 +0000 | [diff] [blame] | 295 | int SymbolResult = fillCommandLineSymbols(*Parser); |
| 296 | if(SymbolResult) |
| 297 | return SymbolResult; |
Richard Barton | d0c478d | 2012-04-16 11:32:10 +0000 | [diff] [blame] | 298 | Parser->setShowParsedOperands(ShowInstOperands); |
Craig Topper | 2c86ed8 | 2014-12-12 07:52:19 +0000 | [diff] [blame] | 299 | Parser->setTargetParser(*TAP); |
Reid Kleckner | 3435941 | 2018-10-24 20:23:57 +0000 | [diff] [blame] | 300 | Parser->getLexer().setLexMasmIntegers(LexMasmIntegers); |
Richard Barton | d0c478d | 2012-04-16 11:32:10 +0000 | [diff] [blame] | 301 | |
| 302 | int Res = Parser->Run(NoInitialTextSection); |
| 303 | |
| 304 | return Res; |
| 305 | } |
| 306 | |
| 307 | int main(int argc, char **argv) { |
Rui Ueyama | 0b9d56a | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 308 | InitLLVM X(argc, argv); |
Richard Barton | d0c478d | 2012-04-16 11:32:10 +0000 | [diff] [blame] | 309 | |
| 310 | // Initialize targets and assembly printers/parsers. |
| 311 | llvm::InitializeAllTargetInfos(); |
| 312 | llvm::InitializeAllTargetMCs(); |
| 313 | llvm::InitializeAllAsmParsers(); |
| 314 | llvm::InitializeAllDisassemblers(); |
| 315 | |
| 316 | // Register the target printer for --version. |
| 317 | cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion); |
| 318 | |
| 319 | cl::ParseCommandLineOptions(argc, argv, "llvm machine code playground\n"); |
Eric Christopher | 179bb4e | 2014-06-19 06:22:08 +0000 | [diff] [blame] | 320 | MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags(); |
Richard Barton | d0c478d | 2012-04-16 11:32:10 +0000 | [diff] [blame] | 321 | setDwarfDebugFlags(argc, argv); |
| 322 | |
Kevin Enderby | 75c9b93 | 2013-01-16 17:46:23 +0000 | [diff] [blame] | 323 | setDwarfDebugProducer(); |
| 324 | |
Richard Barton | d0c478d | 2012-04-16 11:32:10 +0000 | [diff] [blame] | 325 | const char *ProgName = argv[0]; |
Daniel Dunbar | c22e0b2 | 2009-08-14 03:48:55 +0000 | [diff] [blame] | 326 | const Target *TheTarget = GetTarget(ProgName); |
| 327 | if (!TheTarget) |
| 328 | return 1; |
Daniel Sanders | a8cc0535 | 2015-06-16 09:57:38 +0000 | [diff] [blame] | 329 | // Now that GetTarget() has (potentially) replaced TripleName, it's safe to |
| 330 | // construct the Triple object. |
| 331 | Triple TheTriple(TripleName); |
Daniel Dunbar | c22e0b2 | 2009-08-14 03:48:55 +0000 | [diff] [blame] | 332 | |
Rafael Espindola | 7cba2a9 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 333 | ErrorOr<std::unique_ptr<MemoryBuffer>> BufferPtr = |
| 334 | MemoryBuffer::getFileOrSTDIN(InputFilename); |
| 335 | if (std::error_code EC = BufferPtr.getError()) { |
Jonas Devlieghere | aa9bcfc | 2018-04-22 08:01:35 +0000 | [diff] [blame] | 336 | WithColor::error(errs(), ProgName) |
| 337 | << InputFilename << ": " << EC.message() << '\n'; |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 338 | return 1; |
| 339 | } |
David Blaikie | 95ca0fb | 2014-08-21 20:44:56 +0000 | [diff] [blame] | 340 | MemoryBuffer *Buffer = BufferPtr->get(); |
Jim Grosbach | f5bf3cf | 2011-05-09 20:05:25 +0000 | [diff] [blame] | 341 | |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 342 | SourceMgr SrcMgr; |
Jim Grosbach | f5bf3cf | 2011-05-09 20:05:25 +0000 | [diff] [blame] | 343 | |
Daniel Dunbar | 12a8a44 | 2009-08-19 16:25:53 +0000 | [diff] [blame] | 344 | // Tell SrcMgr about this buffer, which is what the parser will pick up. |
David Blaikie | 95ca0fb | 2014-08-21 20:44:56 +0000 | [diff] [blame] | 345 | SrcMgr.AddNewSourceBuffer(std::move(*BufferPtr), SMLoc()); |
Jim Grosbach | f5bf3cf | 2011-05-09 20:05:25 +0000 | [diff] [blame] | 346 | |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 347 | // Record the location of the include directories so that the lexer can find |
| 348 | // it later. |
| 349 | SrcMgr.setIncludeDirs(IncludeDirs); |
Jim Grosbach | f5bf3cf | 2011-05-09 20:05:25 +0000 | [diff] [blame] | 350 | |
Ahmed Charles | f4ccd11 | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 351 | std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName)); |
Evan Cheng | 0e6a052 | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 352 | assert(MRI && "Unable to create target register info!"); |
| 353 | |
Ahmed Charles | f4ccd11 | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 354 | std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName)); |
Rafael Espindola | 4a97170 | 2013-05-13 01:16:13 +0000 | [diff] [blame] | 355 | assert(MAI && "Unable to create target asm info!"); |
| 356 | |
Rafael Espindola | c615a82 | 2016-05-29 01:11:00 +0000 | [diff] [blame] | 357 | MAI->setRelaxELFRelocations(RelaxELFRel); |
| 358 | |
Saleem Abdulrasool | e6be41f | 2017-06-09 00:40:19 +0000 | [diff] [blame] | 359 | if (CompressDebugSections != DebugCompressionType::None) { |
David Blaikie | 4a9c258 | 2014-03-28 20:45:24 +0000 | [diff] [blame] | 360 | if (!zlib::isAvailable()) { |
Jonas Devlieghere | aa9bcfc | 2018-04-22 08:01:35 +0000 | [diff] [blame] | 361 | WithColor::error(errs(), ProgName) |
| 362 | << "build tools with zlib to enable -compress-debug-sections"; |
David Blaikie | 4a9c258 | 2014-03-28 20:45:24 +0000 | [diff] [blame] | 363 | return 1; |
| 364 | } |
George Rimar | ec285af | 2016-05-27 12:27:32 +0000 | [diff] [blame] | 365 | MAI->setCompressDebugSections(CompressDebugSections); |
David Blaikie | 4a9c258 | 2014-03-28 20:45:24 +0000 | [diff] [blame] | 366 | } |
Nirav Dave | aee0f14 | 2016-07-11 12:42:14 +0000 | [diff] [blame] | 367 | MAI->setPreserveAsmComments(PreserveComments); |
David Blaikie | 0a0c4cc | 2014-03-27 20:45:58 +0000 | [diff] [blame] | 368 | |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 369 | // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and |
| 370 | // MCObjectFileInfo needs a MCContext reference in order to initialize itself. |
Rafael Espindola | 8d5114e | 2014-06-28 17:46:19 +0000 | [diff] [blame] | 371 | MCObjectFileInfo MOFI; |
| 372 | MCContext Ctx(MAI.get(), MRI.get(), &MOFI, &SrcMgr); |
Rafael Espindola | 2600a67 | 2017-08-02 20:32:26 +0000 | [diff] [blame] | 373 | MOFI.InitMCObjectFileInfo(TheTriple, PIC, Ctx, LargeCodeModel); |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 374 | |
Daniel Dunbar | c6cf43d | 2011-03-28 22:49:15 +0000 | [diff] [blame] | 375 | if (SaveTempLabels) |
| 376 | Ctx.setAllowTemporaryLabels(false); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 377 | |
Kevin Enderby | 613b757 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 378 | Ctx.setGenDwarfForAssembly(GenDwarfForAssembly); |
Eric Christopher | 179bb4e | 2014-06-19 06:22:08 +0000 | [diff] [blame] | 379 | // Default to 4 for dwarf version. |
| 380 | unsigned DwarfVersion = MCOptions.DwarfVersion ? MCOptions.DwarfVersion : 4; |
Paul Robinson | 28ca8c3 | 2017-02-28 23:40:46 +0000 | [diff] [blame] | 381 | if (DwarfVersion < 2 || DwarfVersion > 5) { |
Oliver Stannard | 5604ab9 | 2014-05-01 08:46:02 +0000 | [diff] [blame] | 382 | errs() << ProgName << ": Dwarf version " << DwarfVersion |
| 383 | << " is not supported." << '\n'; |
| 384 | return 1; |
| 385 | } |
| 386 | Ctx.setDwarfVersion(DwarfVersion); |
Chandler Carruth | 6c31d31 | 2012-12-17 21:32:42 +0000 | [diff] [blame] | 387 | if (!DwarfDebugFlags.empty()) |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 388 | Ctx.setDwarfDebugFlags(StringRef(DwarfDebugFlags)); |
Kevin Enderby | 75c9b93 | 2013-01-16 17:46:23 +0000 | [diff] [blame] | 389 | if (!DwarfDebugProducer.empty()) |
| 390 | Ctx.setDwarfDebugProducer(StringRef(DwarfDebugProducer)); |
Chandler Carruth | 6c31d31 | 2012-12-17 21:32:42 +0000 | [diff] [blame] | 391 | if (!DebugCompilationDir.empty()) |
| 392 | Ctx.setCompilationDir(DebugCompilationDir); |
Justin Bogner | 6d476e6 | 2016-03-22 22:24:29 +0000 | [diff] [blame] | 393 | else { |
| 394 | // If no compilation dir is set, try to use the current directory. |
| 395 | SmallString<128> CWD; |
| 396 | if (!sys::fs::current_path(CWD)) |
| 397 | Ctx.setCompilationDir(CWD); |
| 398 | } |
Paul Robinson | 4575d34 | 2018-07-10 14:41:54 +0000 | [diff] [blame] | 399 | for (const auto &Arg : DebugPrefixMap) { |
| 400 | const auto &KV = StringRef(Arg).split('='); |
| 401 | Ctx.addDebugPrefixMapEntry(KV.first, KV.second); |
| 402 | } |
Eric Christopher | 6c58314 | 2012-12-18 00:31:01 +0000 | [diff] [blame] | 403 | if (!MainFileName.empty()) |
| 404 | Ctx.setMainFileName(MainFileName); |
Paul Robinson | e369b9d | 2018-06-12 16:09:03 +0000 | [diff] [blame] | 405 | if (GenDwarfForAssembly && DwarfVersion >= 5) { |
Paul Robinson | 2740096 | 2018-03-29 17:16:41 +0000 | [diff] [blame] | 406 | // DWARF v5 needs the root file as well as the compilation directory. |
| 407 | // If we find a '.file 0' directive that will supersede these values. |
| 408 | MD5 Hash; |
| 409 | MD5::MD5Result *Cksum = |
| 410 | (MD5::MD5Result *)Ctx.allocate(sizeof(MD5::MD5Result), 1); |
| 411 | Hash.update(Buffer->getBuffer()); |
| 412 | Hash.final(*Cksum); |
| 413 | Ctx.setMCLineTableRootFile( |
| 414 | /*CUID=*/0, Ctx.getCompilationDir(), |
| 415 | !MainFileName.empty() ? MainFileName : InputFilename, Cksum, None); |
| 416 | } |
Kevin Enderby | 613b757 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 417 | |
James Molloy | b950585 | 2011-09-07 17:24:38 +0000 | [diff] [blame] | 418 | // Package up features to be passed to target/subtarget |
| 419 | std::string FeaturesStr; |
| 420 | if (MAttrs.size()) { |
| 421 | SubtargetFeatures Features; |
| 422 | for (unsigned i = 0; i != MAttrs.size(); ++i) |
| 423 | Features.AddFeature(MAttrs[i]); |
| 424 | FeaturesStr = Features.getString(); |
| 425 | } |
| 426 | |
Peter Collingbourne | 60fd99d | 2018-05-21 19:44:54 +0000 | [diff] [blame] | 427 | std::unique_ptr<ToolOutputFile> Out = GetOutputStream(OutputFilename); |
Dan Gohman | d5826a3 | 2010-08-20 01:07:01 +0000 | [diff] [blame] | 428 | if (!Out) |
| 429 | return 1; |
| 430 | |
Peter Collingbourne | 60fd99d | 2018-05-21 19:44:54 +0000 | [diff] [blame] | 431 | std::unique_ptr<ToolOutputFile> DwoOut; |
| 432 | if (!SplitDwarfFile.empty()) { |
| 433 | if (FileType != OFT_ObjectFile) { |
| 434 | WithColor::error() << "dwo output only supported with object files\n"; |
| 435 | return 1; |
| 436 | } |
| 437 | DwoOut = GetOutputStream(SplitDwarfFile); |
| 438 | if (!DwoOut) |
| 439 | return 1; |
| 440 | } |
| 441 | |
Rafael Espindola | c98092e | 2015-04-14 22:14:34 +0000 | [diff] [blame] | 442 | std::unique_ptr<buffer_ostream> BOS; |
| 443 | raw_pwrite_stream *OS = &Out->os(); |
Ahmed Charles | f4ccd11 | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 444 | std::unique_ptr<MCStreamer> Str; |
Chris Lattner | f3ce009 | 2009-08-17 04:23:44 +0000 | [diff] [blame] | 445 | |
Ahmed Charles | f4ccd11 | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 446 | std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo()); |
| 447 | std::unique_ptr<MCSubtargetInfo> STI( |
| 448 | TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr)); |
Evan Cheng | ffc0e73 | 2011-07-09 05:47:46 +0000 | [diff] [blame] | 449 | |
Craig Topper | 573faec | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 450 | MCInstPrinter *IP = nullptr; |
Kevin Enderby | 9823ca9 | 2009-09-04 21:45:34 +0000 | [diff] [blame] | 451 | if (FileType == OFT_AssemblyFile) { |
Daniel Sanders | 47b167d | 2015-09-15 16:17:27 +0000 | [diff] [blame] | 452 | IP = TheTarget->createMCInstPrinter(Triple(TripleName), OutputAsmVariant, |
| 453 | *MAI, *MCII, *MRI); |
Jim Grosbach | 7ca0567 | 2014-06-11 20:26:40 +0000 | [diff] [blame] | 454 | |
Nirav Dave | 30e34b0 | 2016-10-31 18:36:31 +0000 | [diff] [blame] | 455 | if (!IP) { |
Jonas Devlieghere | aa9bcfc | 2018-04-22 08:01:35 +0000 | [diff] [blame] | 456 | WithColor::error() |
| 457 | << "unable to create instruction printer for target triple '" |
Nirav Dave | 30e34b0 | 2016-10-31 18:36:31 +0000 | [diff] [blame] | 458 | << TheTriple.normalize() << "' with assembly variant " |
| 459 | << OutputAsmVariant << ".\n"; |
| 460 | return 1; |
| 461 | } |
| 462 | |
Jim Grosbach | 7ca0567 | 2014-06-11 20:26:40 +0000 | [diff] [blame] | 463 | // Set the display preference for hex vs. decimal immediates. |
| 464 | IP->setPrintImmHex(PrintImmHex); |
| 465 | |
| 466 | // Set up the AsmStreamer. |
Nirav Dave | 80e97ed | 2018-04-27 15:45:54 +0000 | [diff] [blame] | 467 | std::unique_ptr<MCCodeEmitter> CE; |
| 468 | if (ShowEncoding) |
| 469 | CE.reset(TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx)); |
| 470 | |
| 471 | std::unique_ptr<MCAsmBackend> MAB( |
| 472 | TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions)); |
Rafael Espindola | c98092e | 2015-04-14 22:14:34 +0000 | [diff] [blame] | 473 | auto FOut = llvm::make_unique<formatted_raw_ostream>(*OS); |
Nirav Dave | 80e97ed | 2018-04-27 15:45:54 +0000 | [diff] [blame] | 474 | Str.reset( |
| 475 | TheTarget->createAsmStreamer(Ctx, std::move(FOut), /*asmverbose*/ true, |
| 476 | /*useDwarfDirectory*/ true, IP, |
| 477 | std::move(CE), std::move(MAB), ShowInst)); |
Jim Grosbach | 5792051 | 2011-12-05 23:20:14 +0000 | [diff] [blame] | 478 | |
Daniel Dunbar | 2d9f5d1 | 2010-03-23 23:47:12 +0000 | [diff] [blame] | 479 | } else if (FileType == OFT_Null) { |
Peter Collingbourne | 7d3b145 | 2015-02-19 00:45:04 +0000 | [diff] [blame] | 480 | Str.reset(TheTarget->createNullStreamer(Ctx)); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 481 | } else { |
| 482 | assert(FileType == OFT_ObjectFile && "Invalid file type!"); |
Rafael Espindola | c98092e | 2015-04-14 22:14:34 +0000 | [diff] [blame] | 483 | |
Rafael Espindola | b306ae3 | 2015-06-17 16:26:47 +0000 | [diff] [blame] | 484 | // Don't waste memory on names of temp labels. |
| 485 | Ctx.setUseNamesOnTempLabels(false); |
| 486 | |
Rafael Espindola | c98092e | 2015-04-14 22:14:34 +0000 | [diff] [blame] | 487 | if (!Out->os().supportsSeeking()) { |
| 488 | BOS = make_unique<buffer_ostream>(Out->os()); |
| 489 | OS = BOS.get(); |
| 490 | } |
| 491 | |
Eric Christopher | 57849e3 | 2015-03-10 22:03:14 +0000 | [diff] [blame] | 492 | MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx); |
Alex Bradbury | d32868d | 2018-01-03 08:53:05 +0000 | [diff] [blame] | 493 | MCAsmBackend *MAB = TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions); |
David Majnemer | 56afa6e | 2015-12-21 22:09:27 +0000 | [diff] [blame] | 494 | Str.reset(TheTarget->createMCObjectStreamer( |
Peter Collingbourne | 17a9814 | 2018-05-18 18:26:45 +0000 | [diff] [blame] | 495 | TheTriple, Ctx, std::unique_ptr<MCAsmBackend>(MAB), |
Peter Collingbourne | 60fd99d | 2018-05-21 19:44:54 +0000 | [diff] [blame] | 496 | DwoOut ? MAB->createDwoObjectWriter(*OS, DwoOut->os()) |
| 497 | : MAB->createObjectWriter(*OS), |
| 498 | std::unique_ptr<MCCodeEmitter>(CE), *STI, MCOptions.MCRelaxAll, |
| 499 | MCOptions.MCIncrementalLinkerCompatible, |
David Majnemer | 56afa6e | 2015-12-21 22:09:27 +0000 | [diff] [blame] | 500 | /*DWARFMustBeAtTheEnd*/ false)); |
Rafael Espindola | 90ce9f7 | 2014-10-15 16:12:52 +0000 | [diff] [blame] | 501 | if (NoExecStack) |
| 502 | Str->InitSections(true); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 503 | } |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 504 | |
Nirav Dave | 4d34462 | 2018-04-30 19:22:40 +0000 | [diff] [blame] | 505 | // Use Assembler information for parsing. |
| 506 | Str->setUseAssemblerInfoForParsing(true); |
| 507 | |
Richard Barton | d0c478d | 2012-04-16 11:32:10 +0000 | [diff] [blame] | 508 | int Res = 1; |
Kevin Enderby | 14ccc90 | 2012-12-05 18:13:19 +0000 | [diff] [blame] | 509 | bool disassemble = false; |
Chris Lattner | b23677e | 2009-06-21 05:22:37 +0000 | [diff] [blame] | 510 | switch (Action) { |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 511 | case AC_AsLex: |
Craig Topper | 2c86ed8 | 2014-12-12 07:52:19 +0000 | [diff] [blame] | 512 | Res = AsLexInput(SrcMgr, *MAI, Out->os()); |
Richard Barton | d0c478d | 2012-04-16 11:32:10 +0000 | [diff] [blame] | 513 | break; |
Chris Lattner | b23677e | 2009-06-21 05:22:37 +0000 | [diff] [blame] | 514 | case AC_Assemble: |
Eric Christopher | e793899 | 2014-06-19 06:22:01 +0000 | [diff] [blame] | 515 | Res = AssembleInput(ProgName, TheTarget, SrcMgr, Ctx, *Str, *MAI, *STI, |
Eric Christopher | 179bb4e | 2014-06-19 06:22:08 +0000 | [diff] [blame] | 516 | *MCII, MCOptions); |
Richard Barton | d0c478d | 2012-04-16 11:32:10 +0000 | [diff] [blame] | 517 | break; |
Kevin Enderby | 3ed0316 | 2012-10-22 22:31:46 +0000 | [diff] [blame] | 518 | case AC_MDisassemble: |
Eli Bendersky | 5330482 | 2013-02-26 23:04:17 +0000 | [diff] [blame] | 519 | assert(IP && "Expected assembly output"); |
Kevin Enderby | 3ed0316 | 2012-10-22 22:31:46 +0000 | [diff] [blame] | 520 | IP->setUseMarkup(1); |
Kevin Enderby | 14ccc90 | 2012-12-05 18:13:19 +0000 | [diff] [blame] | 521 | disassemble = true; |
| 522 | break; |
Sean Callanan | ba847da | 2009-12-17 01:49:59 +0000 | [diff] [blame] | 523 | case AC_Disassemble: |
Kevin Enderby | 14ccc90 | 2012-12-05 18:13:19 +0000 | [diff] [blame] | 524 | disassemble = true; |
Richard Barton | d0c478d | 2012-04-16 11:32:10 +0000 | [diff] [blame] | 525 | break; |
Chris Lattner | b23677e | 2009-06-21 05:22:37 +0000 | [diff] [blame] | 526 | } |
Kevin Enderby | 14ccc90 | 2012-12-05 18:13:19 +0000 | [diff] [blame] | 527 | if (disassemble) |
| 528 | Res = Disassembler::disassemble(*TheTarget, TripleName, *STI, *Str, |
| 529 | *Buffer, SrcMgr, Out->os()); |
Richard Barton | d0c478d | 2012-04-16 11:32:10 +0000 | [diff] [blame] | 530 | |
| 531 | // Keep output if no errors. |
Peter Collingbourne | 60fd99d | 2018-05-21 19:44:54 +0000 | [diff] [blame] | 532 | if (Res == 0) { |
| 533 | Out->keep(); |
| 534 | if (DwoOut) |
| 535 | DwoOut->keep(); |
| 536 | } |
Richard Barton | d0c478d | 2012-04-16 11:32:10 +0000 | [diff] [blame] | 537 | return Res; |
Chris Lattner | f9f065e | 2009-06-18 23:04:45 +0000 | [diff] [blame] | 538 | } |