Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 1 | //===-- llvm-mc.cpp - Machine Code Hacking Driver ---------------*- C++ -*-===// |
Chris Lattner | 8dd8a52 | 2009-06-18 23:04:45 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Chris Lattner | 8dd8a52 | 2009-06-18 23:04:45 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Chris Lattner | c7ab953 | 2009-06-18 23:05:21 +0000 | [diff] [blame] | 9 | // This utility is a simple driver that allows command line hacking on machine |
| 10 | // code. |
Chris Lattner | 8dd8a52 | 2009-06-18 23:04:45 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 14 | #include "Disassembler.h" |
Evan Cheng | 5928e69 | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCAsmBackend.h" |
Craig Topper | a6e377f | 2012-04-15 22:00:22 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCAsmInfo.h" |
Lang Hames | 2241ffa | 2017-10-11 23:34:47 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCCodeEmitter.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCContext.h" |
Chris Lattner | 11b2fc9 | 2009-09-14 03:02:37 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCInstPrinter.h" |
Evan Cheng | c5e6d2f | 2011-07-11 03:57:24 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCInstrInfo.h" |
Evan Cheng | 7679299 | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCObjectFileInfo.h" |
Peter Collingbourne | f7b81db | 2018-05-18 18:26:45 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCObjectWriter.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCParser/AsmLexer.h" |
Benjamin Kramer | b3e8a6d | 2016-01-27 10:01:28 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCParser/MCTargetAsmParser.h" |
Evan Cheng | 7679299 | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCRegisterInfo.h" |
Chris Lattner | 92ffdd1 | 2009-06-24 00:52:40 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCStreamer.h" |
Evan Cheng | 91111d2 | 2011-07-09 05:47:46 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCSubtargetInfo.h" |
David Blaikie | 4333f97 | 2018-04-11 18:49:37 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCTargetOptionsCommandFlags.inc" |
Chris Lattner | 8dd8a52 | 2009-06-18 23:04:45 +0000 | [diff] [blame] | 29 | #include "llvm/Support/CommandLine.h" |
David Blaikie | 9c3857c | 2014-03-28 21:00:25 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Compression.h" |
Dan Gohman | 268b0f4 | 2010-08-20 01:07:01 +0000 | [diff] [blame] | 31 | #include "llvm/Support/FileUtilities.h" |
Daniel Dunbar | 80d484e | 2009-08-14 03:48:55 +0000 | [diff] [blame] | 32 | #include "llvm/Support/FormattedStream.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Host.h" |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 34 | #include "llvm/Support/InitLLVM.h" |
Chris Lattner | 8dd8a52 | 2009-06-18 23:04:45 +0000 | [diff] [blame] | 35 | #include "llvm/Support/MemoryBuffer.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 36 | #include "llvm/Support/SourceMgr.h" |
Evan Cheng | 2bb4035 | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 37 | #include "llvm/Support/TargetRegistry.h" |
| 38 | #include "llvm/Support/TargetSelect.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 39 | #include "llvm/Support/ToolOutputFile.h" |
Jonas Devlieghere | c976aa7 | 2018-04-22 08:01:35 +0000 | [diff] [blame] | 40 | #include "llvm/Support/WithColor.h" |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 8dd8a52 | 2009-06-18 23:04:45 +0000 | [diff] [blame] | 42 | using namespace llvm; |
| 43 | |
| 44 | static cl::opt<std::string> |
| 45 | InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-")); |
| 46 | |
Peter Collingbourne | 63062d9 | 2018-05-21 19:44:54 +0000 | [diff] [blame] | 47 | static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"), |
| 48 | cl::value_desc("filename"), |
| 49 | cl::init("-")); |
| 50 | |
| 51 | static cl::opt<std::string> SplitDwarfFile("split-dwarf-file", |
| 52 | cl::desc("DWO output filename"), |
| 53 | cl::value_desc("filename")); |
Chris Lattner | 8dd8a52 | 2009-06-18 23:04:45 +0000 | [diff] [blame] | 54 | |
Daniel Dunbar | d18dd1d | 2009-08-27 07:56:39 +0000 | [diff] [blame] | 55 | static cl::opt<bool> |
| 56 | ShowEncoding("show-encoding", cl::desc("Show instruction encodings")); |
| 57 | |
Rafael Espindola | e021e85 | 2016-06-17 17:04:56 +0000 | [diff] [blame] | 58 | static cl::opt<bool> RelaxELFRel( |
| 59 | "relax-relocations", cl::init(true), |
| 60 | cl::desc("Emit R_X86_64_GOTPCRELX instead of R_X86_64_GOTPCREL")); |
Rafael Espindola | 9768d73 | 2016-05-29 01:11:00 +0000 | [diff] [blame] | 61 | |
Saleem Abdulrasool | 1f62f57 | 2017-06-09 00:40:19 +0000 | [diff] [blame] | 62 | static cl::opt<DebugCompressionType> CompressDebugSections( |
| 63 | "compress-debug-sections", cl::ValueOptional, |
| 64 | cl::init(DebugCompressionType::None), |
| 65 | cl::desc("Choose DWARF debug sections compression:"), |
| 66 | cl::values(clEnumValN(DebugCompressionType::None, "none", "No compression"), |
| 67 | clEnumValN(DebugCompressionType::Z, "zlib", |
| 68 | "Use zlib compression"), |
| 69 | clEnumValN(DebugCompressionType::GNU, "zlib-gnu", |
| 70 | "Use zlib-gnu compression (deprecated)"))); |
David Blaikie | 7400a97 | 2014-03-27 20:45:58 +0000 | [diff] [blame] | 71 | |
| 72 | static cl::opt<bool> |
Daniel Dunbar | e3ee332 | 2010-02-03 18:18:30 +0000 | [diff] [blame] | 73 | ShowInst("show-inst", cl::desc("Show internal instruction representation")); |
| 74 | |
Daniel Dunbar | 2eca025 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 75 | static cl::opt<bool> |
| 76 | ShowInstOperands("show-inst-operands", |
| 77 | cl::desc("Show instructions operands as parsed")); |
| 78 | |
Chris Lattner | 4479034 | 2009-09-20 07:17:49 +0000 | [diff] [blame] | 79 | static cl::opt<unsigned> |
| 80 | OutputAsmVariant("output-asm-variant", |
| 81 | cl::desc("Syntax variant to use for output printing")); |
| 82 | |
Jim Grosbach | 3fdf7cf | 2014-06-11 20:26:40 +0000 | [diff] [blame] | 83 | static cl::opt<bool> |
| 84 | PrintImmHex("print-imm-hex", cl::init(false), |
| 85 | cl::desc("Prefer hex format for immediate values")); |
| 86 | |
Colin LeMahieu | 229a1e6 | 2015-06-07 01:46:24 +0000 | [diff] [blame] | 87 | static cl::list<std::string> |
| 88 | DefineSymbol("defsym", cl::desc("Defines a symbol to be an integer constant")); |
| 89 | |
Nirav Dave | 53a72f4 | 2016-07-11 12:42:14 +0000 | [diff] [blame] | 90 | static cl::opt<bool> |
| 91 | PreserveComments("preserve-comments", |
| 92 | cl::desc("Preserve Comments in outputted assembly")); |
| 93 | |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 94 | enum OutputFileType { |
Daniel Dunbar | b09b890 | 2010-03-23 23:47:12 +0000 | [diff] [blame] | 95 | OFT_Null, |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 96 | OFT_AssemblyFile, |
| 97 | OFT_ObjectFile |
| 98 | }; |
| 99 | static cl::opt<OutputFileType> |
| 100 | FileType("filetype", cl::init(OFT_AssemblyFile), |
| 101 | cl::desc("Choose an output file type:"), |
| 102 | cl::values( |
| 103 | clEnumValN(OFT_AssemblyFile, "asm", |
| 104 | "Emit an assembly ('.s') file"), |
Daniel Dunbar | b09b890 | 2010-03-23 23:47:12 +0000 | [diff] [blame] | 105 | clEnumValN(OFT_Null, "null", |
| 106 | "Don't emit anything (for timing purposes)"), |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 107 | clEnumValN(OFT_ObjectFile, "obj", |
Mehdi Amini | 732afdd | 2016-10-08 19:41:06 +0000 | [diff] [blame] | 108 | "Emit a native object ('.o') file"))); |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 109 | |
Chris Lattner | d70e15b4 | 2009-06-21 05:22:37 +0000 | [diff] [blame] | 110 | static cl::list<std::string> |
| 111 | IncludeDirs("I", cl::desc("Directory of include files"), |
| 112 | cl::value_desc("directory"), cl::Prefix); |
Chris Lattner | 8dd8a52 | 2009-06-18 23:04:45 +0000 | [diff] [blame] | 113 | |
Daniel Dunbar | 8c6bad2 | 2009-07-17 22:38:58 +0000 | [diff] [blame] | 114 | static cl::opt<std::string> |
Daniel Dunbar | 4689211 | 2010-03-13 02:20:38 +0000 | [diff] [blame] | 115 | ArchName("arch", cl::desc("Target arch to assemble for, " |
Bill Wendling | e303114 | 2011-06-17 20:35:21 +0000 | [diff] [blame] | 116 | "see -version for available targets")); |
Daniel Dunbar | 4689211 | 2010-03-13 02:20:38 +0000 | [diff] [blame] | 117 | |
| 118 | static cl::opt<std::string> |
Nick Lewycky | b244909 | 2009-11-01 22:07:54 +0000 | [diff] [blame] | 119 | TripleName("triple", cl::desc("Target triple to assemble for, " |
Daniel Dunbar | 4689211 | 2010-03-13 02:20:38 +0000 | [diff] [blame] | 120 | "see -version for available targets")); |
Daniel Dunbar | 8c6bad2 | 2009-07-17 22:38:58 +0000 | [diff] [blame] | 121 | |
Jim Grosbach | 685b773 | 2010-10-30 15:57:50 +0000 | [diff] [blame] | 122 | static cl::opt<std::string> |
| 123 | MCPU("mcpu", |
| 124 | cl::desc("Target a specific cpu type (-mcpu=help for details)"), |
| 125 | cl::value_desc("cpu-name"), |
| 126 | cl::init("")); |
| 127 | |
James Molloy | 4c493e8 | 2011-09-07 17:24:38 +0000 | [diff] [blame] | 128 | static cl::list<std::string> |
| 129 | MAttrs("mattr", |
| 130 | cl::CommaSeparated, |
| 131 | cl::desc("Target specific attributes (-mattr=help for details)"), |
| 132 | cl::value_desc("a1,+a2,-a3,...")); |
| 133 | |
Rafael Espindola | 699281c | 2016-05-18 11:58:50 +0000 | [diff] [blame] | 134 | static cl::opt<bool> PIC("position-independent", |
| 135 | cl::desc("Position independent"), cl::init(false)); |
Evan Cheng | 2129f59 | 2011-07-19 06:37:02 +0000 | [diff] [blame] | 136 | |
Rafael Espindola | 9f92995 | 2017-08-02 20:32:26 +0000 | [diff] [blame] | 137 | static cl::opt<bool> |
| 138 | LargeCodeModel("large-code-model", |
| 139 | cl::desc("Create cfi directives that assume the code might " |
| 140 | "be more than 2gb away")); |
Evan Cheng | efd9b42 | 2011-07-20 07:51:56 +0000 | [diff] [blame] | 141 | |
Daniel Dunbar | 322fec6 | 2010-03-13 02:20:57 +0000 | [diff] [blame] | 142 | static cl::opt<bool> |
Bill Wendling | e303114 | 2011-06-17 20:35:21 +0000 | [diff] [blame] | 143 | NoInitialTextSection("n", cl::desc("Don't assume assembly file starts " |
| 144 | "in the text section")); |
Daniel Dunbar | 322fec6 | 2010-03-13 02:20:57 +0000 | [diff] [blame] | 145 | |
Daniel Dunbar | 4ee0d03 | 2011-03-28 22:49:15 +0000 | [diff] [blame] | 146 | static cl::opt<bool> |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 147 | GenDwarfForAssembly("g", cl::desc("Generate dwarf debugging info for assembly " |
| 148 | "source files")); |
| 149 | |
Chandler Carruth | 10700aad | 2012-12-17 21:32:42 +0000 | [diff] [blame] | 150 | static cl::opt<std::string> |
| 151 | DebugCompilationDir("fdebug-compilation-dir", |
| 152 | cl::desc("Specifies the debug info's compilation dir")); |
| 153 | |
Paul Robinson | c17c8bf | 2018-07-10 14:41:54 +0000 | [diff] [blame] | 154 | static cl::list<std::string> |
| 155 | DebugPrefixMap("fdebug-prefix-map", |
| 156 | cl::desc("Map file source paths in debug info"), |
| 157 | cl::value_desc("= separated key-value pairs")); |
| 158 | |
Eric Christopher | 906da23 | 2012-12-18 00:31:01 +0000 | [diff] [blame] | 159 | static cl::opt<std::string> |
| 160 | MainFileName("main-file-name", |
| 161 | cl::desc("Specifies the name we should consider the input file")); |
| 162 | |
Eric Christopher | dc3e9c7 | 2014-05-21 00:20:01 +0000 | [diff] [blame] | 163 | static cl::opt<bool> SaveTempLabels("save-temp-labels", |
| 164 | cl::desc("Don't discard temporary labels")); |
| 165 | |
Reid Kleckner | 953bdce | 2018-10-24 20:23:57 +0000 | [diff] [blame] | 166 | static cl::opt<bool> LexMasmIntegers( |
| 167 | "masm-integers", |
| 168 | cl::desc("Enable binary and hex masm integers (0b110 and 0ABCh)")); |
| 169 | |
Eric Christopher | 472cee3 | 2014-05-21 21:05:09 +0000 | [diff] [blame] | 170 | static cl::opt<bool> NoExecStack("no-exec-stack", |
| 171 | cl::desc("File doesn't need an exec stack")); |
| 172 | |
Chris Lattner | d70e15b4 | 2009-06-21 05:22:37 +0000 | [diff] [blame] | 173 | enum ActionType { |
Chris Lattner | b013345 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 174 | AC_AsLex, |
Sean Callanan | 7e64550 | 2009-12-17 01:49:59 +0000 | [diff] [blame] | 175 | AC_Assemble, |
Sean Callanan | 2d03d3a | 2010-04-12 19:43:00 +0000 | [diff] [blame] | 176 | AC_Disassemble, |
Kevin Enderby | 168ffb3 | 2012-12-05 18:13:19 +0000 | [diff] [blame] | 177 | AC_MDisassemble, |
Chris Lattner | d70e15b4 | 2009-06-21 05:22:37 +0000 | [diff] [blame] | 178 | }; |
Chris Lattner | 8dd8a52 | 2009-06-18 23:04:45 +0000 | [diff] [blame] | 179 | |
Chris Lattner | d70e15b4 | 2009-06-21 05:22:37 +0000 | [diff] [blame] | 180 | static cl::opt<ActionType> |
| 181 | Action(cl::desc("Action to perform:"), |
Chris Lattner | b013345 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 182 | cl::init(AC_Assemble), |
| 183 | cl::values(clEnumValN(AC_AsLex, "as-lex", |
| 184 | "Lex tokens from a .s file"), |
| 185 | clEnumValN(AC_Assemble, "assemble", |
Chris Lattner | d70e15b4 | 2009-06-21 05:22:37 +0000 | [diff] [blame] | 186 | "Assemble a .s file (default)"), |
Sean Callanan | 7e64550 | 2009-12-17 01:49:59 +0000 | [diff] [blame] | 187 | clEnumValN(AC_Disassemble, "disassemble", |
| 188 | "Disassemble strings of hex bytes"), |
Kevin Enderby | 62183c4 | 2012-10-22 22:31:46 +0000 | [diff] [blame] | 189 | clEnumValN(AC_MDisassemble, "mdis", |
Mehdi Amini | 732afdd | 2016-10-08 19:41:06 +0000 | [diff] [blame] | 190 | "Marked up disassembly of strings of hex bytes"))); |
Chris Lattner | d70e15b4 | 2009-06-21 05:22:37 +0000 | [diff] [blame] | 191 | |
Kevin Enderby | f92f990 | 2009-09-04 21:45:34 +0000 | [diff] [blame] | 192 | static const Target *GetTarget(const char *ProgName) { |
Daniel Dunbar | 4689211 | 2010-03-13 02:20:38 +0000 | [diff] [blame] | 193 | // Figure out the target triple. |
| 194 | if (TripleName.empty()) |
Sebastian Pop | 94441fb | 2011-11-01 21:32:20 +0000 | [diff] [blame] | 195 | TripleName = sys::getDefaultTargetTriple(); |
Evan Cheng | e64f0e5 | 2011-07-26 19:02:16 +0000 | [diff] [blame] | 196 | Triple TheTriple(Triple::normalize(TripleName)); |
| 197 | |
Kevin Enderby | fe3d005 | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 198 | // Get the target specific parser. |
| 199 | std::string Error; |
| 200 | const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple, |
| 201 | Error); |
| 202 | if (!TheTarget) { |
Jonas Devlieghere | c976aa7 | 2018-04-22 08:01:35 +0000 | [diff] [blame] | 203 | WithColor::error(errs(), ProgName) << Error; |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 204 | return nullptr; |
Daniel Dunbar | 4689211 | 2010-03-13 02:20:38 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Kevin Enderby | fe3d005 | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 207 | // Update the triple name and return the found target. |
Evan Cheng | e64f0e5 | 2011-07-26 19:02:16 +0000 | [diff] [blame] | 208 | TripleName = TheTriple.getTriple(); |
| 209 | return TheTarget; |
Kevin Enderby | f92f990 | 2009-09-04 21:45:34 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Kai Nacke | c9ddda8 | 2019-10-08 08:21:20 +0000 | [diff] [blame] | 212 | static std::unique_ptr<ToolOutputFile> GetOutputStream(StringRef Path, |
| 213 | sys::fs::OpenFlags Flags) { |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 214 | std::error_code EC; |
Kai Nacke | c9ddda8 | 2019-10-08 08:21:20 +0000 | [diff] [blame] | 215 | auto Out = std::make_unique<ToolOutputFile>(Path, EC, Flags); |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 216 | if (EC) { |
Jonas Devlieghere | c976aa7 | 2018-04-22 08:01:35 +0000 | [diff] [blame] | 217 | WithColor::error() << EC.message() << '\n'; |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 218 | return nullptr; |
Dan Gohman | 268b0f4 | 2010-08-20 01:07:01 +0000 | [diff] [blame] | 219 | } |
Dan Gohman | a2233f2 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 220 | |
| 221 | return Out; |
Dan Gohman | 268b0f4 | 2010-08-20 01:07:01 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Kevin Enderby | e7739d4 | 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 | e82ada6 | 2013-01-16 17:46:23 +0000 | [diff] [blame] | 235 | static std::string DwarfDebugProducer; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 236 | static void setDwarfDebugProducer() { |
Kevin Enderby | e82ada6 | 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 | 23c6d1f | 2014-06-19 06:22:01 +0000 | [diff] [blame] | 242 | static int AsLexInput(SourceMgr &SrcMgr, MCAsmInfo &MAI, |
Craig Topper | 4e05fc3 | 2014-12-12 07:52:19 +0000 | [diff] [blame] | 243 | raw_ostream &OS) { |
Chris Lattner | d70e15b4 | 2009-06-21 05:22:37 +0000 | [diff] [blame] | 244 | |
Richard Barton | def81b9 | 2012-04-16 11:32:10 +0000 | [diff] [blame] | 245 | AsmLexer Lexer(MAI); |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 246 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer()); |
Daniel Dunbar | 7a85f9c | 2010-07-18 18:31:28 +0000 | [diff] [blame] | 247 | |
Chris Lattner | b013345 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 248 | bool Error = false; |
Daniel Dunbar | bc79816 | 2009-07-28 16:56:42 +0000 | [diff] [blame] | 249 | while (Lexer.Lex().isNot(AsmToken::Eof)) { |
Oliver Stannard | 5c032ce | 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 | b013345 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 253 | Error = true; |
Chris Lattner | c8dfbcb | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 254 | } |
Dan Gohman | 268b0f4 | 2010-08-20 01:07:01 +0000 | [diff] [blame] | 255 | |
Chris Lattner | b013345 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 256 | return Error; |
Chris Lattner | d70e15b4 | 2009-06-21 05:22:37 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Mandeep Singh Grang | 3236007 | 2016-12-01 18:42:04 +0000 | [diff] [blame] | 259 | static int fillCommandLineSymbols(MCAsmParser &Parser) { |
Mandeep Singh Grang | 9a561aa | 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 | c976aa7 | 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 | 229a1e6 | 2015-06-07 01:46:24 +0000 | [diff] [blame] | 268 | return 1; |
Mandeep Singh Grang | 9a561aa | 2016-12-06 02:49:17 +0000 | [diff] [blame] | 269 | } |
| 270 | int64_t Value; |
| 271 | if (Val.getAsInteger(0, Value)) { |
Jonas Devlieghere | c976aa7 | 2018-04-22 08:01:35 +0000 | [diff] [blame] | 272 | WithColor::error() << "value is not an integer: " << Val << "\n"; |
Mandeep Singh Grang | 9a561aa | 2016-12-06 02:49:17 +0000 | [diff] [blame] | 273 | return 1; |
| 274 | } |
| 275 | Parser.getContext().setSymbolValue(Parser.getStreamer(), Sym, Value); |
| 276 | } |
Colin LeMahieu | 229a1e6 | 2015-06-07 01:46:24 +0000 | [diff] [blame] | 277 | return 0; |
| 278 | } |
| 279 | |
NAKAMURA Takumi | 89b8c17 | 2014-01-22 03:12:43 +0000 | [diff] [blame] | 280 | static int AssembleInput(const char *ProgName, const Target *TheTarget, |
Richard Barton | def81b9 | 2012-04-16 11:32:10 +0000 | [diff] [blame] | 281 | SourceMgr &SrcMgr, MCContext &Ctx, MCStreamer &Str, |
Eric Christopher | 23c6d1f | 2014-06-19 06:22:01 +0000 | [diff] [blame] | 282 | MCAsmInfo &MAI, MCSubtargetInfo &STI, |
Brian Cain | 6dbbd0f | 2019-08-08 19:13:23 +0000 | [diff] [blame] | 283 | MCInstrInfo &MCII, MCTargetOptions const &MCOptions) { |
Evgeniy Stepanov | 0a951b7 | 2014-04-23 11:16:03 +0000 | [diff] [blame] | 284 | std::unique_ptr<MCAsmParser> Parser( |
| 285 | createMCAsmParser(SrcMgr, Ctx, Str, MAI)); |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 286 | std::unique_ptr<MCTargetAsmParser> TAP( |
Eric Christopher | 4c5bff3 | 2014-06-19 06:22:08 +0000 | [diff] [blame] | 287 | TheTarget->createMCAsmParser(STI, *Parser, MCII, MCOptions)); |
| 288 | |
Richard Barton | def81b9 | 2012-04-16 11:32:10 +0000 | [diff] [blame] | 289 | if (!TAP) { |
Jonas Devlieghere | c976aa7 | 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 | def81b9 | 2012-04-16 11:32:10 +0000 | [diff] [blame] | 292 | return 1; |
| 293 | } |
| 294 | |
Colin LeMahieu | 229a1e6 | 2015-06-07 01:46:24 +0000 | [diff] [blame] | 295 | int SymbolResult = fillCommandLineSymbols(*Parser); |
| 296 | if(SymbolResult) |
| 297 | return SymbolResult; |
Richard Barton | def81b9 | 2012-04-16 11:32:10 +0000 | [diff] [blame] | 298 | Parser->setShowParsedOperands(ShowInstOperands); |
Craig Topper | 4e05fc3 | 2014-12-12 07:52:19 +0000 | [diff] [blame] | 299 | Parser->setTargetParser(*TAP); |
Reid Kleckner | 953bdce | 2018-10-24 20:23:57 +0000 | [diff] [blame] | 300 | Parser->getLexer().setLexMasmIntegers(LexMasmIntegers); |
Richard Barton | def81b9 | 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 | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 308 | InitLLVM X(argc, argv); |
Richard Barton | def81b9 | 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"); |
Brian Cain | 6dbbd0f | 2019-08-08 19:13:23 +0000 | [diff] [blame] | 320 | const MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags(); |
Richard Barton | def81b9 | 2012-04-16 11:32:10 +0000 | [diff] [blame] | 321 | setDwarfDebugFlags(argc, argv); |
| 322 | |
Kevin Enderby | e82ada6 | 2013-01-16 17:46:23 +0000 | [diff] [blame] | 323 | setDwarfDebugProducer(); |
| 324 | |
Richard Barton | def81b9 | 2012-04-16 11:32:10 +0000 | [diff] [blame] | 325 | const char *ProgName = argv[0]; |
Daniel Dunbar | 80d484e | 2009-08-14 03:48:55 +0000 | [diff] [blame] | 326 | const Target *TheTarget = GetTarget(ProgName); |
| 327 | if (!TheTarget) |
| 328 | return 1; |
Daniel Sanders | c535d93 | 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 | 80d484e | 2009-08-14 03:48:55 +0000 | [diff] [blame] | 332 | |
Rafael Espindola | adf21f2 | 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 | c976aa7 | 2018-04-22 08:01:35 +0000 | [diff] [blame] | 336 | WithColor::error(errs(), ProgName) |
| 337 | << InputFilename << ": " << EC.message() << '\n'; |
Chris Lattner | b013345 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 338 | return 1; |
| 339 | } |
David Blaikie | 1961f14 | 2014-08-21 20:44:56 +0000 | [diff] [blame] | 340 | MemoryBuffer *Buffer = BufferPtr->get(); |
Jim Grosbach | 112a2de | 2011-05-09 20:05:25 +0000 | [diff] [blame] | 341 | |
Chris Lattner | b013345 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 342 | SourceMgr SrcMgr; |
Jim Grosbach | 112a2de | 2011-05-09 20:05:25 +0000 | [diff] [blame] | 343 | |
Daniel Dunbar | 5c947db | 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 | 1961f14 | 2014-08-21 20:44:56 +0000 | [diff] [blame] | 345 | SrcMgr.AddNewSourceBuffer(std::move(*BufferPtr), SMLoc()); |
Jim Grosbach | 112a2de | 2011-05-09 20:05:25 +0000 | [diff] [blame] | 346 | |
Chris Lattner | b013345 | 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 | 112a2de | 2011-05-09 20:05:25 +0000 | [diff] [blame] | 350 | |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 351 | std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName)); |
Evan Cheng | d60fa58b | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 352 | assert(MRI && "Unable to create target register info!"); |
| 353 | |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 354 | std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName)); |
Rafael Espindola | 227144c | 2013-05-13 01:16:13 +0000 | [diff] [blame] | 355 | assert(MAI && "Unable to create target asm info!"); |
| 356 | |
Rafael Espindola | 9768d73 | 2016-05-29 01:11:00 +0000 | [diff] [blame] | 357 | MAI->setRelaxELFRelocations(RelaxELFRel); |
| 358 | |
Saleem Abdulrasool | 1f62f57 | 2017-06-09 00:40:19 +0000 | [diff] [blame] | 359 | if (CompressDebugSections != DebugCompressionType::None) { |
David Blaikie | 9b620b4 | 2014-03-28 20:45:24 +0000 | [diff] [blame] | 360 | if (!zlib::isAvailable()) { |
Jonas Devlieghere | c976aa7 | 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 | 9b620b4 | 2014-03-28 20:45:24 +0000 | [diff] [blame] | 363 | return 1; |
| 364 | } |
George Rimar | c91e38c | 2016-05-27 12:27:32 +0000 | [diff] [blame] | 365 | MAI->setCompressDebugSections(CompressDebugSections); |
David Blaikie | 9b620b4 | 2014-03-28 20:45:24 +0000 | [diff] [blame] | 366 | } |
Nirav Dave | 53a72f4 | 2016-07-11 12:42:14 +0000 | [diff] [blame] | 367 | MAI->setPreserveAsmComments(PreserveComments); |
David Blaikie | 7400a97 | 2014-03-27 20:45:58 +0000 | [diff] [blame] | 368 | |
Evan Cheng | 7679299 | 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 | 75556bc | 2014-06-28 17:46:19 +0000 | [diff] [blame] | 371 | MCObjectFileInfo MOFI; |
Brian Cain | 6dbbd0f | 2019-08-08 19:13:23 +0000 | [diff] [blame] | 372 | MCContext Ctx(MAI.get(), MRI.get(), &MOFI, &SrcMgr, &MCOptions); |
Rafael Espindola | 9f92995 | 2017-08-02 20:32:26 +0000 | [diff] [blame] | 373 | MOFI.InitMCObjectFileInfo(TheTriple, PIC, Ctx, LargeCodeModel); |
Evan Cheng | 7679299 | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 374 | |
Daniel Dunbar | 4ee0d03 | 2011-03-28 22:49:15 +0000 | [diff] [blame] | 375 | if (SaveTempLabels) |
| 376 | Ctx.setAllowTemporaryLabels(false); |
Rafael Espindola | 0a017a6 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 377 | |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 378 | Ctx.setGenDwarfForAssembly(GenDwarfForAssembly); |
Eric Christopher | 4c5bff3 | 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 | dccb4fe | 2017-02-28 23:40:46 +0000 | [diff] [blame] | 381 | if (DwarfVersion < 2 || DwarfVersion > 5) { |
Oliver Stannard | 7eacbd5 | 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 | 10700aad | 2012-12-17 21:32:42 +0000 | [diff] [blame] | 387 | if (!DwarfDebugFlags.empty()) |
Kevin Enderby | e7739d4 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 388 | Ctx.setDwarfDebugFlags(StringRef(DwarfDebugFlags)); |
Kevin Enderby | e82ada6 | 2013-01-16 17:46:23 +0000 | [diff] [blame] | 389 | if (!DwarfDebugProducer.empty()) |
| 390 | Ctx.setDwarfDebugProducer(StringRef(DwarfDebugProducer)); |
Chandler Carruth | 10700aad | 2012-12-17 21:32:42 +0000 | [diff] [blame] | 391 | if (!DebugCompilationDir.empty()) |
| 392 | Ctx.setCompilationDir(DebugCompilationDir); |
Justin Bogner | 8809c40 | 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 | c17c8bf | 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 | 906da23 | 2012-12-18 00:31:01 +0000 | [diff] [blame] | 403 | if (!MainFileName.empty()) |
| 404 | Ctx.setMainFileName(MainFileName); |
Paul Robinson | 1ca2576 | 2019-03-01 20:58:04 +0000 | [diff] [blame] | 405 | if (GenDwarfForAssembly) |
| 406 | Ctx.setGenDwarfRootFile(InputFilename, Buffer->getBuffer()); |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 407 | |
James Molloy | 4c493e8 | 2011-09-07 17:24:38 +0000 | [diff] [blame] | 408 | // Package up features to be passed to target/subtarget |
| 409 | std::string FeaturesStr; |
| 410 | if (MAttrs.size()) { |
| 411 | SubtargetFeatures Features; |
| 412 | for (unsigned i = 0; i != MAttrs.size(); ++i) |
| 413 | Features.AddFeature(MAttrs[i]); |
| 414 | FeaturesStr = Features.getString(); |
| 415 | } |
| 416 | |
Kai Nacke | c9ddda8 | 2019-10-08 08:21:20 +0000 | [diff] [blame] | 417 | sys::fs::OpenFlags Flags = (FileType == OFT_AssemblyFile) ? sys::fs::OF_Text |
| 418 | : sys::fs::OF_None; |
| 419 | std::unique_ptr<ToolOutputFile> Out = GetOutputStream(OutputFilename, Flags); |
Dan Gohman | 268b0f4 | 2010-08-20 01:07:01 +0000 | [diff] [blame] | 420 | if (!Out) |
| 421 | return 1; |
| 422 | |
Peter Collingbourne | 63062d9 | 2018-05-21 19:44:54 +0000 | [diff] [blame] | 423 | std::unique_ptr<ToolOutputFile> DwoOut; |
| 424 | if (!SplitDwarfFile.empty()) { |
| 425 | if (FileType != OFT_ObjectFile) { |
| 426 | WithColor::error() << "dwo output only supported with object files\n"; |
| 427 | return 1; |
| 428 | } |
Kai Nacke | c9ddda8 | 2019-10-08 08:21:20 +0000 | [diff] [blame] | 429 | DwoOut = GetOutputStream(SplitDwarfFile, sys::fs::OF_None); |
Peter Collingbourne | 63062d9 | 2018-05-21 19:44:54 +0000 | [diff] [blame] | 430 | if (!DwoOut) |
| 431 | return 1; |
| 432 | } |
| 433 | |
Rafael Espindola | 5560a4c | 2015-04-14 22:14:34 +0000 | [diff] [blame] | 434 | std::unique_ptr<buffer_ostream> BOS; |
| 435 | raw_pwrite_stream *OS = &Out->os(); |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 436 | std::unique_ptr<MCStreamer> Str; |
Chris Lattner | a61e93d | 2009-08-17 04:23:44 +0000 | [diff] [blame] | 437 | |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 438 | std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo()); |
| 439 | std::unique_ptr<MCSubtargetInfo> STI( |
| 440 | TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr)); |
Evan Cheng | 91111d2 | 2011-07-09 05:47:46 +0000 | [diff] [blame] | 441 | |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 442 | MCInstPrinter *IP = nullptr; |
Kevin Enderby | f92f990 | 2009-09-04 21:45:34 +0000 | [diff] [blame] | 443 | if (FileType == OFT_AssemblyFile) { |
Daniel Sanders | 50f1723 | 2015-09-15 16:17:27 +0000 | [diff] [blame] | 444 | IP = TheTarget->createMCInstPrinter(Triple(TripleName), OutputAsmVariant, |
| 445 | *MAI, *MCII, *MRI); |
Jim Grosbach | 3fdf7cf | 2014-06-11 20:26:40 +0000 | [diff] [blame] | 446 | |
Nirav Dave | a9395af | 2016-10-31 18:36:31 +0000 | [diff] [blame] | 447 | if (!IP) { |
Jonas Devlieghere | c976aa7 | 2018-04-22 08:01:35 +0000 | [diff] [blame] | 448 | WithColor::error() |
| 449 | << "unable to create instruction printer for target triple '" |
Nirav Dave | a9395af | 2016-10-31 18:36:31 +0000 | [diff] [blame] | 450 | << TheTriple.normalize() << "' with assembly variant " |
| 451 | << OutputAsmVariant << ".\n"; |
| 452 | return 1; |
| 453 | } |
| 454 | |
Jim Grosbach | 3fdf7cf | 2014-06-11 20:26:40 +0000 | [diff] [blame] | 455 | // Set the display preference for hex vs. decimal immediates. |
| 456 | IP->setPrintImmHex(PrintImmHex); |
| 457 | |
| 458 | // Set up the AsmStreamer. |
Nirav Dave | 1b5533c | 2018-04-27 15:45:54 +0000 | [diff] [blame] | 459 | std::unique_ptr<MCCodeEmitter> CE; |
| 460 | if (ShowEncoding) |
| 461 | CE.reset(TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx)); |
| 462 | |
| 463 | std::unique_ptr<MCAsmBackend> MAB( |
| 464 | TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions)); |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 465 | auto FOut = std::make_unique<formatted_raw_ostream>(*OS); |
Nirav Dave | 1b5533c | 2018-04-27 15:45:54 +0000 | [diff] [blame] | 466 | Str.reset( |
| 467 | TheTarget->createAsmStreamer(Ctx, std::move(FOut), /*asmverbose*/ true, |
| 468 | /*useDwarfDirectory*/ true, IP, |
| 469 | std::move(CE), std::move(MAB), ShowInst)); |
Jim Grosbach | 78309c4 | 2011-12-05 23:20:14 +0000 | [diff] [blame] | 470 | |
Daniel Dunbar | b09b890 | 2010-03-23 23:47:12 +0000 | [diff] [blame] | 471 | } else if (FileType == OFT_Null) { |
Peter Collingbourne | f4498a4 | 2015-02-19 00:45:04 +0000 | [diff] [blame] | 472 | Str.reset(TheTarget->createNullStreamer(Ctx)); |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 473 | } else { |
| 474 | assert(FileType == OFT_ObjectFile && "Invalid file type!"); |
Rafael Espindola | 5560a4c | 2015-04-14 22:14:34 +0000 | [diff] [blame] | 475 | |
Rafael Espindola | f27fa2b | 2015-06-17 16:26:47 +0000 | [diff] [blame] | 476 | // Don't waste memory on names of temp labels. |
| 477 | Ctx.setUseNamesOnTempLabels(false); |
| 478 | |
Rafael Espindola | 5560a4c | 2015-04-14 22:14:34 +0000 | [diff] [blame] | 479 | if (!Out->os().supportsSeeking()) { |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 480 | BOS = std::make_unique<buffer_ostream>(Out->os()); |
Rafael Espindola | 5560a4c | 2015-04-14 22:14:34 +0000 | [diff] [blame] | 481 | OS = BOS.get(); |
| 482 | } |
| 483 | |
Eric Christopher | 0169e42 | 2015-03-10 22:03:14 +0000 | [diff] [blame] | 484 | MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx); |
Alex Bradbury | b22f751 | 2018-01-03 08:53:05 +0000 | [diff] [blame] | 485 | MCAsmBackend *MAB = TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions); |
David Majnemer | 03e2cc3 | 2015-12-21 22:09:27 +0000 | [diff] [blame] | 486 | Str.reset(TheTarget->createMCObjectStreamer( |
Peter Collingbourne | f7b81db | 2018-05-18 18:26:45 +0000 | [diff] [blame] | 487 | TheTriple, Ctx, std::unique_ptr<MCAsmBackend>(MAB), |
Peter Collingbourne | 63062d9 | 2018-05-21 19:44:54 +0000 | [diff] [blame] | 488 | DwoOut ? MAB->createDwoObjectWriter(*OS, DwoOut->os()) |
| 489 | : MAB->createObjectWriter(*OS), |
| 490 | std::unique_ptr<MCCodeEmitter>(CE), *STI, MCOptions.MCRelaxAll, |
| 491 | MCOptions.MCIncrementalLinkerCompatible, |
David Majnemer | 03e2cc3 | 2015-12-21 22:09:27 +0000 | [diff] [blame] | 492 | /*DWARFMustBeAtTheEnd*/ false)); |
Rafael Espindola | 7b61ddf | 2014-10-15 16:12:52 +0000 | [diff] [blame] | 493 | if (NoExecStack) |
| 494 | Str->InitSections(true); |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 495 | } |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 496 | |
Nirav Dave | 6c0665e | 2018-04-30 19:22:40 +0000 | [diff] [blame] | 497 | // Use Assembler information for parsing. |
| 498 | Str->setUseAssemblerInfoForParsing(true); |
| 499 | |
Richard Barton | def81b9 | 2012-04-16 11:32:10 +0000 | [diff] [blame] | 500 | int Res = 1; |
Kevin Enderby | 168ffb3 | 2012-12-05 18:13:19 +0000 | [diff] [blame] | 501 | bool disassemble = false; |
Chris Lattner | d70e15b4 | 2009-06-21 05:22:37 +0000 | [diff] [blame] | 502 | switch (Action) { |
Chris Lattner | b013345 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 503 | case AC_AsLex: |
Craig Topper | 4e05fc3 | 2014-12-12 07:52:19 +0000 | [diff] [blame] | 504 | Res = AsLexInput(SrcMgr, *MAI, Out->os()); |
Richard Barton | def81b9 | 2012-04-16 11:32:10 +0000 | [diff] [blame] | 505 | break; |
Chris Lattner | d70e15b4 | 2009-06-21 05:22:37 +0000 | [diff] [blame] | 506 | case AC_Assemble: |
Eric Christopher | 23c6d1f | 2014-06-19 06:22:01 +0000 | [diff] [blame] | 507 | Res = AssembleInput(ProgName, TheTarget, SrcMgr, Ctx, *Str, *MAI, *STI, |
Eric Christopher | 4c5bff3 | 2014-06-19 06:22:08 +0000 | [diff] [blame] | 508 | *MCII, MCOptions); |
Richard Barton | def81b9 | 2012-04-16 11:32:10 +0000 | [diff] [blame] | 509 | break; |
Kevin Enderby | 62183c4 | 2012-10-22 22:31:46 +0000 | [diff] [blame] | 510 | case AC_MDisassemble: |
Eli Bendersky | 6f5d70e | 2013-02-26 23:04:17 +0000 | [diff] [blame] | 511 | assert(IP && "Expected assembly output"); |
Fangrui Song | 0588588 | 2019-07-25 09:54:12 +0000 | [diff] [blame] | 512 | IP->setUseMarkup(true); |
Kevin Enderby | 168ffb3 | 2012-12-05 18:13:19 +0000 | [diff] [blame] | 513 | disassemble = true; |
| 514 | break; |
Sean Callanan | 7e64550 | 2009-12-17 01:49:59 +0000 | [diff] [blame] | 515 | case AC_Disassemble: |
Kevin Enderby | 168ffb3 | 2012-12-05 18:13:19 +0000 | [diff] [blame] | 516 | disassemble = true; |
Richard Barton | def81b9 | 2012-04-16 11:32:10 +0000 | [diff] [blame] | 517 | break; |
Chris Lattner | d70e15b4 | 2009-06-21 05:22:37 +0000 | [diff] [blame] | 518 | } |
Kevin Enderby | 168ffb3 | 2012-12-05 18:13:19 +0000 | [diff] [blame] | 519 | if (disassemble) |
Thomas Lively | 2cb2707 | 2019-10-15 18:28:22 +0000 | [diff] [blame^] | 520 | Res = Disassembler::disassemble(*TheTarget, TripleName, *STI, *Str, *Buffer, |
| 521 | SrcMgr, Ctx, Out->os()); |
Richard Barton | def81b9 | 2012-04-16 11:32:10 +0000 | [diff] [blame] | 522 | |
| 523 | // Keep output if no errors. |
Peter Collingbourne | 63062d9 | 2018-05-21 19:44:54 +0000 | [diff] [blame] | 524 | if (Res == 0) { |
| 525 | Out->keep(); |
| 526 | if (DwoOut) |
| 527 | DwoOut->keep(); |
| 528 | } |
Richard Barton | def81b9 | 2012-04-16 11:32:10 +0000 | [diff] [blame] | 529 | return Res; |
Chris Lattner | 8dd8a52 | 2009-06-18 23:04:45 +0000 | [diff] [blame] | 530 | } |