Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 1 | //===-- cc1as_main.cpp - Clang Assembler ---------------------------------===// |
| 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 | // |
| 10 | // This is the entry point to the clang -cc1as functionality, which implements |
| 11 | // the direct interface to the LLVM MC based assembler. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Basic/Diagnostic.h" |
Chandler Carruth | cc0694c | 2012-12-04 09:25:21 +0000 | [diff] [blame] | 16 | #include "clang/Basic/DiagnosticOptions.h" |
Chandler Carruth | cc0694c | 2012-12-04 09:25:21 +0000 | [diff] [blame] | 17 | #include "clang/Driver/DriverDiagnostic.h" |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 18 | #include "clang/Driver/Options.h" |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 19 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 20 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 21 | #include "clang/Frontend/Utils.h" |
Rafael Espindola | 4dedcd7 | 2015-04-09 21:06:11 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/STLExtras.h" |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringSwitch.h" |
Duncan Sands | f610b5b | 2010-08-30 09:42:39 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/Triple.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 25 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | cc0694c | 2012-12-04 09:25:21 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCAsmBackend.h" |
Daniel Dunbar | 0547428 | 2010-07-17 02:26:21 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCAsmInfo.h" |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCCodeEmitter.h" |
| 29 | #include "llvm/MC/MCContext.h" |
Evan Cheng | 279f290 | 2011-07-11 04:24:19 +0000 | [diff] [blame] | 30 | #include "llvm/MC/MCInstrInfo.h" |
Evan Cheng | 347033f | 2011-07-20 06:22:27 +0000 | [diff] [blame] | 31 | #include "llvm/MC/MCObjectFileInfo.h" |
Peter Collingbourne | f7b81db | 2018-05-18 18:26:45 +0000 | [diff] [blame] | 32 | #include "llvm/MC/MCObjectWriter.h" |
Chandler Carruth | cc0694c | 2012-12-04 09:25:21 +0000 | [diff] [blame] | 33 | #include "llvm/MC/MCParser/MCAsmParser.h" |
Benjamin Kramer | 5e45630 | 2016-01-27 10:01:30 +0000 | [diff] [blame] | 34 | #include "llvm/MC/MCParser/MCTargetAsmParser.h" |
Evan Cheng | b505ace | 2011-07-18 20:57:51 +0000 | [diff] [blame] | 35 | #include "llvm/MC/MCRegisterInfo.h" |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 36 | #include "llvm/MC/MCStreamer.h" |
Evan Cheng | dec3187 | 2011-07-09 06:04:17 +0000 | [diff] [blame] | 37 | #include "llvm/MC/MCSubtargetInfo.h" |
Evgeniy Stepanov | eeb820f | 2014-04-23 11:15:49 +0000 | [diff] [blame] | 38 | #include "llvm/MC/MCTargetOptions.h" |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 39 | #include "llvm/Option/Arg.h" |
| 40 | #include "llvm/Option/ArgList.h" |
| 41 | #include "llvm/Option/OptTable.h" |
Daniel Dunbar | 0454f65 | 2010-05-20 18:15:20 +0000 | [diff] [blame] | 42 | #include "llvm/Support/CommandLine.h" |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 43 | #include "llvm/Support/ErrorHandling.h" |
Rafael Espindola | ccc6ea6 | 2013-06-26 12:48:34 +0000 | [diff] [blame] | 44 | #include "llvm/Support/FileSystem.h" |
Chandler Carruth | cc0694c | 2012-12-04 09:25:21 +0000 | [diff] [blame] | 45 | #include "llvm/Support/FormattedStream.h" |
| 46 | #include "llvm/Support/Host.h" |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 47 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | 8aaf499 | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 48 | #include "llvm/Support/Path.h" |
| 49 | #include "llvm/Support/Signals.h" |
Chandler Carruth | cc0694c | 2012-12-04 09:25:21 +0000 | [diff] [blame] | 50 | #include "llvm/Support/SourceMgr.h" |
Evan Cheng | 494eb06 | 2011-08-24 18:09:14 +0000 | [diff] [blame] | 51 | #include "llvm/Support/TargetRegistry.h" |
| 52 | #include "llvm/Support/TargetSelect.h" |
| 53 | #include "llvm/Support/Timer.h" |
| 54 | #include "llvm/Support/raw_ostream.h" |
Ahmed Charles | dfca6f9 | 2014-03-09 11:36:40 +0000 | [diff] [blame] | 55 | #include <memory> |
Rafael Espindola | 8a8e554 | 2014-06-12 17:19:42 +0000 | [diff] [blame] | 56 | #include <system_error> |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 57 | using namespace clang; |
| 58 | using namespace clang::driver; |
Alp Toker | 61dad75 | 2014-07-09 14:05:11 +0000 | [diff] [blame] | 59 | using namespace clang::driver::options; |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 60 | using namespace llvm; |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 61 | using namespace llvm::opt; |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 62 | |
| 63 | namespace { |
| 64 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 65 | /// Helper class for representing a single invocation of the assembler. |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 66 | struct AssemblerInvocation { |
| 67 | /// @name Target Options |
| 68 | /// @{ |
| 69 | |
Jim Grosbach | 576452b | 2012-02-10 20:37:10 +0000 | [diff] [blame] | 70 | /// The name of the target triple to assemble for. |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 71 | std::string Triple; |
| 72 | |
Jim Grosbach | 576452b | 2012-02-10 20:37:10 +0000 | [diff] [blame] | 73 | /// If given, the name of the target CPU to determine which instructions |
| 74 | /// are legal. |
| 75 | std::string CPU; |
| 76 | |
| 77 | /// The list of target specific features to enable or disable -- this should |
| 78 | /// be a list of strings starting with '+' or '-'. |
| 79 | std::vector<std::string> Features; |
| 80 | |
Mandeep Singh Grang | 358faec | 2016-12-01 18:42:16 +0000 | [diff] [blame] | 81 | /// The list of symbol definitions. |
| 82 | std::vector<std::string> SymbolDefs; |
| 83 | |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 84 | /// @} |
| 85 | /// @name Language Options |
| 86 | /// @{ |
| 87 | |
| 88 | std::vector<std::string> IncludePaths; |
| 89 | unsigned NoInitialTextSection : 1; |
Daniel Dunbar | 9cf7bc7 | 2011-03-28 22:49:24 +0000 | [diff] [blame] | 90 | unsigned SaveTemporaryLabels : 1; |
Kevin Enderby | 292dc08 | 2011-12-22 19:31:58 +0000 | [diff] [blame] | 91 | unsigned GenDwarfForAssembly : 1; |
Rafael Espindola | f8f01c3 | 2016-05-29 02:01:14 +0000 | [diff] [blame] | 92 | unsigned RelaxELFRelocations : 1; |
Oliver Stannard | 9b2a7d4 | 2014-05-19 13:39:13 +0000 | [diff] [blame] | 93 | unsigned DwarfVersion; |
Kevin Enderby | 292dc08 | 2011-12-22 19:31:58 +0000 | [diff] [blame] | 94 | std::string DwarfDebugFlags; |
Kevin Enderby | ae2ec47 | 2013-01-17 21:38:06 +0000 | [diff] [blame] | 95 | std::string DwarfDebugProducer; |
Chandler Carruth | 4d5e1a9 | 2012-12-17 21:40:04 +0000 | [diff] [blame] | 96 | std::string DebugCompilationDir; |
Paul Robinson | 9b292b4 | 2018-07-10 15:15:24 +0000 | [diff] [blame] | 97 | std::map<const std::string, const std::string> DebugPrefixMap; |
Saleem Abdulrasool | 5444890 | 2017-06-09 00:40:30 +0000 | [diff] [blame] | 98 | llvm::DebugCompressionType CompressDebugSections = |
| 99 | llvm::DebugCompressionType::None; |
Eric Christopher | 45f2e71 | 2012-12-18 00:31:10 +0000 | [diff] [blame] | 100 | std::string MainFileName; |
Peter Collingbourne | 91d0284 | 2018-05-22 18:52:37 +0000 | [diff] [blame] | 101 | std::string SplitDwarfFile; |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 102 | |
| 103 | /// @} |
| 104 | /// @name Frontend Options |
| 105 | /// @{ |
| 106 | |
| 107 | std::string InputFile; |
Daniel Dunbar | 0454f65 | 2010-05-20 18:15:20 +0000 | [diff] [blame] | 108 | std::vector<std::string> LLVMArgs; |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 109 | std::string OutputPath; |
| 110 | enum FileType { |
| 111 | FT_Asm, ///< Assembly (.s) output, transliterate mode. |
| 112 | FT_Null, ///< No output, for timing purposes. |
| 113 | FT_Obj ///< Object file output. |
| 114 | }; |
| 115 | FileType OutputType; |
Daniel Dunbar | 0454f65 | 2010-05-20 18:15:20 +0000 | [diff] [blame] | 116 | unsigned ShowHelp : 1; |
| 117 | unsigned ShowVersion : 1; |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 118 | |
| 119 | /// @} |
| 120 | /// @name Transliterate Options |
| 121 | /// @{ |
| 122 | |
| 123 | unsigned OutputAsmVariant; |
| 124 | unsigned ShowEncoding : 1; |
| 125 | unsigned ShowInst : 1; |
| 126 | |
| 127 | /// @} |
| 128 | /// @name Assembler Options |
| 129 | /// @{ |
| 130 | |
| 131 | unsigned RelaxAll : 1; |
Rafael Espindola | 148141c | 2011-01-23 17:58:26 +0000 | [diff] [blame] | 132 | unsigned NoExecStack : 1; |
Joerg Sonnenberger | a43604a | 2014-08-26 18:40:25 +0000 | [diff] [blame] | 133 | unsigned FatalWarnings : 1; |
David Majnemer | 2b9349d | 2015-12-21 22:09:34 +0000 | [diff] [blame] | 134 | unsigned IncrementalLinkerCompatible : 1; |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 135 | |
Joerg Sonnenberger | 924f6ad | 2015-09-18 11:13:43 +0000 | [diff] [blame] | 136 | /// The name of the relocation model to use. |
| 137 | std::string RelocationModel; |
| 138 | |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 139 | /// @} |
| 140 | |
| 141 | public: |
| 142 | AssemblerInvocation() { |
| 143 | Triple = ""; |
| 144 | NoInitialTextSection = 0; |
| 145 | InputFile = "-"; |
Daniel Dunbar | 0454f65 | 2010-05-20 18:15:20 +0000 | [diff] [blame] | 146 | OutputPath = "-"; |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 147 | OutputType = FT_Asm; |
| 148 | OutputAsmVariant = 0; |
| 149 | ShowInst = 0; |
| 150 | ShowEncoding = 0; |
| 151 | RelaxAll = 0; |
Rafael Espindola | 148141c | 2011-01-23 17:58:26 +0000 | [diff] [blame] | 152 | NoExecStack = 0; |
Joerg Sonnenberger | a43604a | 2014-08-26 18:40:25 +0000 | [diff] [blame] | 153 | FatalWarnings = 0; |
David Majnemer | 2b9349d | 2015-12-21 22:09:34 +0000 | [diff] [blame] | 154 | IncrementalLinkerCompatible = 0; |
Douglas Katzman | 722bcb0 | 2015-10-13 16:22:51 +0000 | [diff] [blame] | 155 | DwarfVersion = 0; |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Sean Silva | 070cd2d | 2014-08-15 21:38:36 +0000 | [diff] [blame] | 158 | static bool CreateFromArgs(AssemblerInvocation &Res, |
| 159 | ArrayRef<const char *> Argv, |
| 160 | DiagnosticsEngine &Diags); |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 161 | }; |
| 162 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 163 | } |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 164 | |
Dylan Noblesmith | e99b27f | 2011-12-23 03:05:38 +0000 | [diff] [blame] | 165 | bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts, |
Sean Silva | 070cd2d | 2014-08-15 21:38:36 +0000 | [diff] [blame] | 166 | ArrayRef<const char *> Argv, |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 167 | DiagnosticsEngine &Diags) { |
Dylan Noblesmith | e99b27f | 2011-12-23 03:05:38 +0000 | [diff] [blame] | 168 | bool Success = true; |
| 169 | |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 170 | // Parse the arguments. |
Alp Toker | 61dad75 | 2014-07-09 14:05:11 +0000 | [diff] [blame] | 171 | std::unique_ptr<OptTable> OptTbl(createDriverOptTable()); |
| 172 | |
| 173 | const unsigned IncludedFlagsBitmask = options::CC1AsOption; |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 174 | unsigned MissingArgIndex, MissingArgCount; |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 175 | InputArgList Args = OptTbl->ParseArgs(Argv, MissingArgIndex, MissingArgCount, |
| 176 | IncludedFlagsBitmask); |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 177 | |
| 178 | // Check for missing argument error. |
Dylan Noblesmith | e99b27f | 2011-12-23 03:05:38 +0000 | [diff] [blame] | 179 | if (MissingArgCount) { |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 180 | Diags.Report(diag::err_drv_missing_argument) |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 181 | << Args.getArgString(MissingArgIndex) << MissingArgCount; |
Dylan Noblesmith | e99b27f | 2011-12-23 03:05:38 +0000 | [diff] [blame] | 182 | Success = false; |
| 183 | } |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 184 | |
| 185 | // Issue errors on unknown arguments. |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 186 | for (const Arg *A : Args.filtered(OPT_UNKNOWN)) { |
Brian Gesiak | ceb987f | 2018-01-13 17:09:11 +0000 | [diff] [blame] | 187 | auto ArgString = A->getAsString(Args); |
| 188 | std::string Nearest; |
| 189 | if (OptTbl->findNearest(ArgString, Nearest, IncludedFlagsBitmask) > 1) |
| 190 | Diags.Report(diag::err_drv_unknown_argument) << ArgString; |
| 191 | else |
| 192 | Diags.Report(diag::err_drv_unknown_argument_with_suggestion) |
| 193 | << ArgString << Nearest; |
Dylan Noblesmith | e99b27f | 2011-12-23 03:05:38 +0000 | [diff] [blame] | 194 | Success = false; |
| 195 | } |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 196 | |
| 197 | // Construct the invocation. |
| 198 | |
| 199 | // Target Options |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 200 | Opts.Triple = llvm::Triple::normalize(Args.getLastArgValue(OPT_triple)); |
| 201 | Opts.CPU = Args.getLastArgValue(OPT_target_cpu); |
| 202 | Opts.Features = Args.getAllArgValues(OPT_target_feature); |
Jim Grosbach | 576452b | 2012-02-10 20:37:10 +0000 | [diff] [blame] | 203 | |
| 204 | // Use the default target triple if unspecified. |
| 205 | if (Opts.Triple.empty()) |
| 206 | Opts.Triple = llvm::sys::getDefaultTargetTriple(); |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 207 | |
| 208 | // Language Options |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 209 | Opts.IncludePaths = Args.getAllArgValues(OPT_I); |
| 210 | Opts.NoInitialTextSection = Args.hasArg(OPT_n); |
| 211 | Opts.SaveTemporaryLabels = Args.hasArg(OPT_msave_temp_labels); |
Douglas Katzman | 3459ce2 | 2015-10-08 04:24:12 +0000 | [diff] [blame] | 212 | // Any DebugInfoKind implies GenDwarfForAssembly. |
| 213 | Opts.GenDwarfForAssembly = Args.hasArg(OPT_debug_info_kind_EQ); |
Saleem Abdulrasool | d064e91 | 2017-06-23 15:34:16 +0000 | [diff] [blame] | 214 | |
| 215 | if (const Arg *A = Args.getLastArg(OPT_compress_debug_sections, |
| 216 | OPT_compress_debug_sections_EQ)) { |
| 217 | if (A->getOption().getID() == OPT_compress_debug_sections) { |
| 218 | // TODO: be more clever about the compression type auto-detection |
| 219 | Opts.CompressDebugSections = llvm::DebugCompressionType::GNU; |
| 220 | } else { |
| 221 | Opts.CompressDebugSections = |
| 222 | llvm::StringSwitch<llvm::DebugCompressionType>(A->getValue()) |
| 223 | .Case("none", llvm::DebugCompressionType::None) |
| 224 | .Case("zlib", llvm::DebugCompressionType::Z) |
| 225 | .Case("zlib-gnu", llvm::DebugCompressionType::GNU) |
| 226 | .Default(llvm::DebugCompressionType::None); |
| 227 | } |
| 228 | } |
| 229 | |
Rafael Espindola | f8f01c3 | 2016-05-29 02:01:14 +0000 | [diff] [blame] | 230 | Opts.RelaxELFRelocations = Args.hasArg(OPT_mrelax_relocations); |
Adrian Prantl | ae39ccc | 2016-04-19 20:31:19 +0000 | [diff] [blame] | 231 | Opts.DwarfVersion = getLastArgIntValue(Args, OPT_dwarf_version_EQ, 2, Diags); |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 232 | Opts.DwarfDebugFlags = Args.getLastArgValue(OPT_dwarf_debug_flags); |
| 233 | Opts.DwarfDebugProducer = Args.getLastArgValue(OPT_dwarf_debug_producer); |
| 234 | Opts.DebugCompilationDir = Args.getLastArgValue(OPT_fdebug_compilation_dir); |
| 235 | Opts.MainFileName = Args.getLastArgValue(OPT_main_file_name); |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 236 | |
Paul Robinson | 9b292b4 | 2018-07-10 15:15:24 +0000 | [diff] [blame] | 237 | for (const auto &Arg : Args.getAllArgValues(OPT_fdebug_prefix_map_EQ)) |
| 238 | Opts.DebugPrefixMap.insert(StringRef(Arg).split('=')); |
| 239 | |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 240 | // Frontend Options |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 241 | if (Args.hasArg(OPT_INPUT)) { |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 242 | bool First = true; |
Richard Smith | 0aef305 | 2017-02-18 01:14:43 +0000 | [diff] [blame] | 243 | for (const Arg *A : Args.filtered(OPT_INPUT)) { |
| 244 | if (First) { |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 245 | Opts.InputFile = A->getValue(); |
Richard Smith | 0aef305 | 2017-02-18 01:14:43 +0000 | [diff] [blame] | 246 | First = false; |
| 247 | } else { |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 248 | Diags.Report(diag::err_drv_unknown_argument) << A->getAsString(Args); |
Dylan Noblesmith | e99b27f | 2011-12-23 03:05:38 +0000 | [diff] [blame] | 249 | Success = false; |
| 250 | } |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 251 | } |
| 252 | } |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 253 | Opts.LLVMArgs = Args.getAllArgValues(OPT_mllvm); |
| 254 | Opts.OutputPath = Args.getLastArgValue(OPT_o); |
Peter Collingbourne | 91d0284 | 2018-05-22 18:52:37 +0000 | [diff] [blame] | 255 | Opts.SplitDwarfFile = Args.getLastArgValue(OPT_split_dwarf_file); |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 256 | if (Arg *A = Args.getLastArg(OPT_filetype)) { |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 257 | StringRef Name = A->getValue(); |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 258 | unsigned OutputType = StringSwitch<unsigned>(Name) |
| 259 | .Case("asm", FT_Asm) |
| 260 | .Case("null", FT_Null) |
| 261 | .Case("obj", FT_Obj) |
| 262 | .Default(~0U); |
Dylan Noblesmith | e99b27f | 2011-12-23 03:05:38 +0000 | [diff] [blame] | 263 | if (OutputType == ~0U) { |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 264 | Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Name; |
Dylan Noblesmith | e99b27f | 2011-12-23 03:05:38 +0000 | [diff] [blame] | 265 | Success = false; |
| 266 | } else |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 267 | Opts.OutputType = FileType(OutputType); |
| 268 | } |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 269 | Opts.ShowHelp = Args.hasArg(OPT_help); |
| 270 | Opts.ShowVersion = Args.hasArg(OPT_version); |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 271 | |
| 272 | // Transliterate Options |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 273 | Opts.OutputAsmVariant = |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 274 | getLastArgIntValue(Args, OPT_output_asm_variant, 0, Diags); |
| 275 | Opts.ShowEncoding = Args.hasArg(OPT_show_encoding); |
| 276 | Opts.ShowInst = Args.hasArg(OPT_show_inst); |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 277 | |
| 278 | // Assemble Options |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 279 | Opts.RelaxAll = Args.hasArg(OPT_mrelax_all); |
| 280 | Opts.NoExecStack = Args.hasArg(OPT_mno_exec_stack); |
| 281 | Opts.FatalWarnings = Args.hasArg(OPT_massembler_fatal_warnings); |
Joerg Sonnenberger | 924f6ad | 2015-09-18 11:13:43 +0000 | [diff] [blame] | 282 | Opts.RelocationModel = Args.getLastArgValue(OPT_mrelocation_model, "pic"); |
David Majnemer | 2b9349d | 2015-12-21 22:09:34 +0000 | [diff] [blame] | 283 | Opts.IncrementalLinkerCompatible = |
| 284 | Args.hasArg(OPT_mincremental_linker_compatible); |
Mandeep Singh Grang | 358faec | 2016-12-01 18:42:16 +0000 | [diff] [blame] | 285 | Opts.SymbolDefs = Args.getAllArgValues(OPT_defsym); |
Dylan Noblesmith | e99b27f | 2011-12-23 03:05:38 +0000 | [diff] [blame] | 286 | |
Dylan Noblesmith | 68207c0 | 2011-12-26 19:29:47 +0000 | [diff] [blame] | 287 | return Success; |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Rafael Espindola | 7e556ad | 2015-04-09 21:50:11 +0000 | [diff] [blame] | 290 | static std::unique_ptr<raw_fd_ostream> |
Peter Collingbourne | 91d0284 | 2018-05-22 18:52:37 +0000 | [diff] [blame] | 291 | getOutputStream(StringRef Path, DiagnosticsEngine &Diags, bool Binary) { |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 292 | // Make sure that the Out file gets unlinked from the disk if we get a |
| 293 | // SIGINT. |
Peter Collingbourne | 91d0284 | 2018-05-22 18:52:37 +0000 | [diff] [blame] | 294 | if (Path != "-") |
| 295 | sys::RemoveFileOnSignal(Path); |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 296 | |
Rafael Espindola | dae941a | 2014-08-25 18:17:04 +0000 | [diff] [blame] | 297 | std::error_code EC; |
Rafael Espindola | 7e556ad | 2015-04-09 21:50:11 +0000 | [diff] [blame] | 298 | auto Out = llvm::make_unique<raw_fd_ostream>( |
Peter Collingbourne | 91d0284 | 2018-05-22 18:52:37 +0000 | [diff] [blame] | 299 | Path, EC, (Binary ? sys::fs::F_None : sys::fs::F_Text)); |
Rafael Espindola | dae941a | 2014-08-25 18:17:04 +0000 | [diff] [blame] | 300 | if (EC) { |
Peter Collingbourne | 91d0284 | 2018-05-22 18:52:37 +0000 | [diff] [blame] | 301 | Diags.Report(diag::err_fe_unable_to_open_output) << Path << EC.message(); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 302 | return nullptr; |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Rafael Espindola | 4dedcd7 | 2015-04-09 21:06:11 +0000 | [diff] [blame] | 305 | return Out; |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 306 | } |
| 307 | |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 308 | static bool ExecuteAssembler(AssemblerInvocation &Opts, |
| 309 | DiagnosticsEngine &Diags) { |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 310 | // Get the target specific parser. |
| 311 | std::string Error; |
Alp Toker | e98ea7c | 2014-05-31 00:02:21 +0000 | [diff] [blame] | 312 | const Target *TheTarget = TargetRegistry::lookupTarget(Opts.Triple, Error); |
Alp Toker | 4429314 | 2014-05-31 00:02:27 +0000 | [diff] [blame] | 313 | if (!TheTarget) |
| 314 | return Diags.Report(diag::err_target_unknown_triple) << Opts.Triple; |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 315 | |
Rafael Espindola | 2d2b420 | 2014-07-06 17:43:24 +0000 | [diff] [blame] | 316 | ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = |
| 317 | MemoryBuffer::getFileOrSTDIN(Opts.InputFile); |
| 318 | |
| 319 | if (std::error_code EC = Buffer.getError()) { |
| 320 | Error = EC.message(); |
Alp Toker | 4429314 | 2014-05-31 00:02:27 +0000 | [diff] [blame] | 321 | return Diags.Report(diag::err_fe_error_reading) << Opts.InputFile; |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | SourceMgr SrcMgr; |
| 325 | |
| 326 | // Tell SrcMgr about this buffer, which is what the parser will pick up. |
David Blaikie | 9e095d9 | 2014-08-21 21:01:00 +0000 | [diff] [blame] | 327 | SrcMgr.AddNewSourceBuffer(std::move(*Buffer), SMLoc()); |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 328 | |
| 329 | // Record the location of the include directories so that the lexer can find |
| 330 | // it later. |
| 331 | SrcMgr.setIncludeDirs(Opts.IncludePaths); |
| 332 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 333 | std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(Opts.Triple)); |
Evan Cheng | b505ace | 2011-07-18 20:57:51 +0000 | [diff] [blame] | 334 | assert(MRI && "Unable to create target register info!"); |
| 335 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 336 | std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, Opts.Triple)); |
Rafael Espindola | 7705623 | 2013-05-13 01:24:18 +0000 | [diff] [blame] | 337 | assert(MAI && "Unable to create target asm info!"); |
| 338 | |
David Blaikie | b31e1d1 | 2014-03-31 23:47:13 +0000 | [diff] [blame] | 339 | // Ensure MCAsmInfo initialization occurs before any use, otherwise sections |
| 340 | // may be created with a combination of default and explicit settings. |
Saleem Abdulrasool | 5444890 | 2017-06-09 00:40:30 +0000 | [diff] [blame] | 341 | MAI->setCompressDebugSections(Opts.CompressDebugSections); |
David Blaikie | b31e1d1 | 2014-03-31 23:47:13 +0000 | [diff] [blame] | 342 | |
Rafael Espindola | f8f01c3 | 2016-05-29 02:01:14 +0000 | [diff] [blame] | 343 | MAI->setRelaxELFRelocations(Opts.RelaxELFRelocations); |
| 344 | |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 345 | bool IsBinary = Opts.OutputType == AssemblerInvocation::FT_Obj; |
Peter Collingbourne | 91d0284 | 2018-05-22 18:52:37 +0000 | [diff] [blame] | 346 | if (Opts.OutputPath.empty()) |
| 347 | Opts.OutputPath = "-"; |
| 348 | std::unique_ptr<raw_fd_ostream> FDOS = |
| 349 | getOutputStream(Opts.OutputPath, Diags, IsBinary); |
Rafael Espindola | 2f16bc1 | 2015-04-14 15:15:49 +0000 | [diff] [blame] | 350 | if (!FDOS) |
Alp Toker | 4429314 | 2014-05-31 00:02:27 +0000 | [diff] [blame] | 351 | return true; |
Peter Collingbourne | 91d0284 | 2018-05-22 18:52:37 +0000 | [diff] [blame] | 352 | std::unique_ptr<raw_fd_ostream> DwoOS; |
| 353 | if (!Opts.SplitDwarfFile.empty()) |
| 354 | DwoOS = getOutputStream(Opts.SplitDwarfFile, Diags, IsBinary); |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 355 | |
Evan Cheng | 347033f | 2011-07-20 06:22:27 +0000 | [diff] [blame] | 356 | // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and |
| 357 | // MCObjectFileInfo needs a MCContext reference in order to initialize itself. |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 358 | std::unique_ptr<MCObjectFileInfo> MOFI(new MCObjectFileInfo()); |
David Blaikie | 07b8d4e | 2014-03-31 23:13:30 +0000 | [diff] [blame] | 359 | |
Bill Wendling | da1e3e7 | 2013-06-18 07:22:05 +0000 | [diff] [blame] | 360 | MCContext Ctx(MAI.get(), MRI.get(), MOFI.get(), &SrcMgr); |
Joerg Sonnenberger | 924f6ad | 2015-09-18 11:13:43 +0000 | [diff] [blame] | 361 | |
Rafael Espindola | e64e230 | 2016-05-18 11:58:56 +0000 | [diff] [blame] | 362 | bool PIC = false; |
Joerg Sonnenberger | 924f6ad | 2015-09-18 11:13:43 +0000 | [diff] [blame] | 363 | if (Opts.RelocationModel == "static") { |
Rafael Espindola | e64e230 | 2016-05-18 11:58:56 +0000 | [diff] [blame] | 364 | PIC = false; |
Joerg Sonnenberger | 924f6ad | 2015-09-18 11:13:43 +0000 | [diff] [blame] | 365 | } else if (Opts.RelocationModel == "pic") { |
Rafael Espindola | e64e230 | 2016-05-18 11:58:56 +0000 | [diff] [blame] | 366 | PIC = true; |
Joerg Sonnenberger | 924f6ad | 2015-09-18 11:13:43 +0000 | [diff] [blame] | 367 | } else { |
| 368 | assert(Opts.RelocationModel == "dynamic-no-pic" && |
| 369 | "Invalid PIC model!"); |
Rafael Espindola | e64e230 | 2016-05-18 11:58:56 +0000 | [diff] [blame] | 370 | PIC = false; |
Joerg Sonnenberger | 924f6ad | 2015-09-18 11:13:43 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Rafael Espindola | 2e8a7d3 | 2017-08-02 20:32:35 +0000 | [diff] [blame] | 373 | MOFI->InitMCObjectFileInfo(Triple(Opts.Triple), PIC, Ctx); |
Daniel Dunbar | 9cf7bc7 | 2011-03-28 22:49:24 +0000 | [diff] [blame] | 374 | if (Opts.SaveTemporaryLabels) |
| 375 | Ctx.setAllowTemporaryLabels(false); |
Kevin Enderby | 292dc08 | 2011-12-22 19:31:58 +0000 | [diff] [blame] | 376 | if (Opts.GenDwarfForAssembly) |
| 377 | Ctx.setGenDwarfForAssembly(true); |
| 378 | if (!Opts.DwarfDebugFlags.empty()) |
| 379 | Ctx.setDwarfDebugFlags(StringRef(Opts.DwarfDebugFlags)); |
Kevin Enderby | ae2ec47 | 2013-01-17 21:38:06 +0000 | [diff] [blame] | 380 | if (!Opts.DwarfDebugProducer.empty()) |
| 381 | Ctx.setDwarfDebugProducer(StringRef(Opts.DwarfDebugProducer)); |
Chandler Carruth | 4d5e1a9 | 2012-12-17 21:40:04 +0000 | [diff] [blame] | 382 | if (!Opts.DebugCompilationDir.empty()) |
| 383 | Ctx.setCompilationDir(Opts.DebugCompilationDir); |
Paul Robinson | 9b292b4 | 2018-07-10 15:15:24 +0000 | [diff] [blame] | 384 | if (!Opts.DebugPrefixMap.empty()) |
| 385 | for (const auto &KV : Opts.DebugPrefixMap) |
| 386 | Ctx.addDebugPrefixMapEntry(KV.first, KV.second); |
Eric Christopher | 45f2e71 | 2012-12-18 00:31:10 +0000 | [diff] [blame] | 387 | if (!Opts.MainFileName.empty()) |
| 388 | Ctx.setMainFileName(StringRef(Opts.MainFileName)); |
Oliver Stannard | 9b2a7d4 | 2014-05-19 13:39:13 +0000 | [diff] [blame] | 389 | Ctx.setDwarfVersion(Opts.DwarfVersion); |
Rafael Espindola | 86f2c57 | 2010-12-10 07:40:14 +0000 | [diff] [blame] | 390 | |
Jim Grosbach | 576452b | 2012-02-10 20:37:10 +0000 | [diff] [blame] | 391 | // Build up the feature string from the target feature list. |
| 392 | std::string FS; |
| 393 | if (!Opts.Features.empty()) { |
| 394 | FS = Opts.Features[0]; |
| 395 | for (unsigned i = 1, e = Opts.Features.size(); i != e; ++i) |
| 396 | FS += "," + Opts.Features[i]; |
| 397 | } |
| 398 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 399 | std::unique_ptr<MCStreamer> Str; |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 400 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 401 | std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo()); |
| 402 | std::unique_ptr<MCSubtargetInfo> STI( |
| 403 | TheTarget->createMCSubtargetInfo(Opts.Triple, Opts.CPU, FS)); |
Evan Cheng | 279f290 | 2011-07-11 04:24:19 +0000 | [diff] [blame] | 404 | |
Rafael Espindola | 2f16bc1 | 2015-04-14 15:15:49 +0000 | [diff] [blame] | 405 | raw_pwrite_stream *Out = FDOS.get(); |
| 406 | std::unique_ptr<buffer_ostream> BOS; |
| 407 | |
Rafael Espindola | 148141c | 2011-01-23 17:58:26 +0000 | [diff] [blame] | 408 | // FIXME: There is a bit of code duplication with addPassesToEmitFile. |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 409 | if (Opts.OutputType == AssemblerInvocation::FT_Asm) { |
Eric Christopher | 7e0fadf | 2015-03-31 00:10:23 +0000 | [diff] [blame] | 410 | MCInstPrinter *IP = TheTarget->createMCInstPrinter( |
Daniel Sanders | 50f1723 | 2015-09-15 16:17:27 +0000 | [diff] [blame] | 411 | llvm::Triple(Opts.Triple), Opts.OutputAsmVariant, *MAI, *MCII, *MRI); |
Nirav Dave | 1b5533c | 2018-04-27 15:45:54 +0000 | [diff] [blame] | 412 | |
| 413 | std::unique_ptr<MCCodeEmitter> CE; |
| 414 | if (Opts.ShowEncoding) |
| 415 | CE.reset(TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx)); |
| 416 | MCTargetOptions MCOptions; |
| 417 | std::unique_ptr<MCAsmBackend> MAB( |
| 418 | TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions)); |
| 419 | |
Rafael Espindola | 4dedcd7 | 2015-04-09 21:06:11 +0000 | [diff] [blame] | 420 | auto FOut = llvm::make_unique<formatted_raw_ostream>(*Out); |
| 421 | Str.reset(TheTarget->createAsmStreamer( |
| 422 | Ctx, std::move(FOut), /*asmverbose*/ true, |
Nirav Dave | 1b5533c | 2018-04-27 15:45:54 +0000 | [diff] [blame] | 423 | /*useDwarfDirectory*/ true, IP, std::move(CE), std::move(MAB), |
| 424 | Opts.ShowInst)); |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 425 | } else if (Opts.OutputType == AssemblerInvocation::FT_Null) { |
| 426 | Str.reset(createNullStreamer(Ctx)); |
| 427 | } else { |
| 428 | assert(Opts.OutputType == AssemblerInvocation::FT_Obj && |
| 429 | "Invalid file type!"); |
Rafael Espindola | 2f16bc1 | 2015-04-14 15:15:49 +0000 | [diff] [blame] | 430 | if (!FDOS->supportsSeeking()) { |
| 431 | BOS = make_unique<buffer_ostream>(*FDOS); |
| 432 | Out = BOS.get(); |
| 433 | } |
| 434 | |
Nirav Dave | 1b5533c | 2018-04-27 15:45:54 +0000 | [diff] [blame] | 435 | std::unique_ptr<MCCodeEmitter> CE( |
| 436 | TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx)); |
| 437 | MCTargetOptions MCOptions; |
| 438 | std::unique_ptr<MCAsmBackend> MAB( |
| 439 | TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions)); |
Peter Collingbourne | 91d0284 | 2018-05-22 18:52:37 +0000 | [diff] [blame] | 440 | std::unique_ptr<MCObjectWriter> OW = |
| 441 | DwoOS ? MAB->createDwoObjectWriter(*Out, *DwoOS) |
| 442 | : MAB->createObjectWriter(*Out); |
Nirav Dave | 1b5533c | 2018-04-27 15:45:54 +0000 | [diff] [blame] | 443 | |
Daniel Sanders | 50f1723 | 2015-09-15 16:17:27 +0000 | [diff] [blame] | 444 | Triple T(Opts.Triple); |
David Majnemer | 2b9349d | 2015-12-21 22:09:34 +0000 | [diff] [blame] | 445 | Str.reset(TheTarget->createMCObjectStreamer( |
Peter Collingbourne | f7b81db | 2018-05-18 18:26:45 +0000 | [diff] [blame] | 446 | T, Ctx, std::move(MAB), std::move(OW), std::move(CE), *STI, |
| 447 | Opts.RelaxAll, Opts.IncrementalLinkerCompatible, |
David Majnemer | 2b9349d | 2015-12-21 22:09:34 +0000 | [diff] [blame] | 448 | /*DWARFMustBeAtTheEnd*/ true)); |
Rafael Espindola | d465be8 | 2014-10-15 16:12:57 +0000 | [diff] [blame] | 449 | Str.get()->InitSections(Opts.NoExecStack); |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Nirav Dave | 6c0665e | 2018-04-30 19:22:40 +0000 | [diff] [blame] | 452 | // Assembly to object compilation should leverage assembly info. |
| 453 | Str->setUseAssemblerInfoForParsing(true); |
| 454 | |
Alp Toker | 4429314 | 2014-05-31 00:02:27 +0000 | [diff] [blame] | 455 | bool Failed = false; |
Alp Toker | e98ea7c | 2014-05-31 00:02:21 +0000 | [diff] [blame] | 456 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 457 | std::unique_ptr<MCAsmParser> Parser( |
| 458 | createMCAsmParser(SrcMgr, Ctx, *Str.get(), *MAI)); |
Evgeniy Stepanov | eeb820f | 2014-04-23 11:15:49 +0000 | [diff] [blame] | 459 | |
| 460 | // FIXME: init MCTargetOptions from sanitizer flags here. |
| 461 | MCTargetOptions Options; |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 462 | std::unique_ptr<MCTargetAsmParser> TAP( |
Evgeniy Stepanov | eeb820f | 2014-04-23 11:15:49 +0000 | [diff] [blame] | 463 | TheTarget->createMCAsmParser(*STI, *Parser, *MCII, Options)); |
Alp Toker | 4429314 | 2014-05-31 00:02:27 +0000 | [diff] [blame] | 464 | if (!TAP) |
| 465 | Failed = Diags.Report(diag::err_target_unknown_triple) << Opts.Triple; |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 466 | |
Mandeep Singh Grang | 358faec | 2016-12-01 18:42:16 +0000 | [diff] [blame] | 467 | // Set values for symbols, if any. |
| 468 | for (auto &S : Opts.SymbolDefs) { |
Mandeep Singh Grang | 6d1d36c | 2016-12-06 02:49:16 +0000 | [diff] [blame] | 469 | auto Pair = StringRef(S).split('='); |
| 470 | auto Sym = Pair.first; |
| 471 | auto Val = Pair.second; |
| 472 | int64_t Value; |
| 473 | // We have already error checked this in the driver. |
| 474 | Val.getAsInteger(0, Value); |
| 475 | Ctx.setSymbolValue(Parser->getStreamer(), Sym, Value); |
Mandeep Singh Grang | 358faec | 2016-12-01 18:42:16 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Alp Toker | 4429314 | 2014-05-31 00:02:27 +0000 | [diff] [blame] | 478 | if (!Failed) { |
Alp Toker | e98ea7c | 2014-05-31 00:02:21 +0000 | [diff] [blame] | 479 | Parser->setTargetParser(*TAP.get()); |
Alp Toker | 4429314 | 2014-05-31 00:02:27 +0000 | [diff] [blame] | 480 | Failed = Parser->Run(Opts.NoInitialTextSection); |
Alp Toker | e98ea7c | 2014-05-31 00:02:21 +0000 | [diff] [blame] | 481 | } |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 482 | |
Steven Wu | ae480ec | 2015-07-24 02:12:43 +0000 | [diff] [blame] | 483 | // Close Streamer first. |
| 484 | // It might have a reference to the output stream. |
| 485 | Str.reset(); |
Alp Toker | e98ea7c | 2014-05-31 00:02:21 +0000 | [diff] [blame] | 486 | // Close the output stream early. |
Rafael Espindola | 2f16bc1 | 2015-04-14 15:15:49 +0000 | [diff] [blame] | 487 | BOS.reset(); |
| 488 | FDOS.reset(); |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 489 | |
Alp Toker | e98ea7c | 2014-05-31 00:02:21 +0000 | [diff] [blame] | 490 | // Delete output file if there were errors. |
Peter Collingbourne | 91d0284 | 2018-05-22 18:52:37 +0000 | [diff] [blame] | 491 | if (Failed) { |
| 492 | if (Opts.OutputPath != "-") |
| 493 | sys::fs::remove(Opts.OutputPath); |
| 494 | if (!Opts.SplitDwarfFile.empty() && Opts.SplitDwarfFile != "-") |
| 495 | sys::fs::remove(Opts.SplitDwarfFile); |
| 496 | } |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 497 | |
Alp Toker | 4429314 | 2014-05-31 00:02:27 +0000 | [diff] [blame] | 498 | return Failed; |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Chad Rosier | 05c71aa | 2013-03-27 18:28:23 +0000 | [diff] [blame] | 501 | static void LLVMErrorHandler(void *UserData, const std::string &Message, |
| 502 | bool GenCrashDiag) { |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 503 | DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData); |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 504 | |
| 505 | Diags.Report(diag::err_fe_error_backend) << Message; |
| 506 | |
| 507 | // We cannot recover from llvm errors. |
| 508 | exit(1); |
| 509 | } |
| 510 | |
Sean Silva | 070cd2d | 2014-08-15 21:38:36 +0000 | [diff] [blame] | 511 | int cc1as_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) { |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 512 | // Initialize targets and assembly printers/parsers. |
| 513 | InitializeAllTargetInfos(); |
Evan Cheng | c391a58 | 2011-07-22 21:59:11 +0000 | [diff] [blame] | 514 | InitializeAllTargetMCs(); |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 515 | InitializeAllAsmParsers(); |
| 516 | |
| 517 | // Construct our diagnostic client. |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 518 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); |
Douglas Gregor | 2dd19f1 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 519 | TextDiagnosticPrinter *DiagClient |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 520 | = new TextDiagnosticPrinter(errs(), &*DiagOpts); |
Douglas Gregor | 2dd19f1 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 521 | DiagClient->setPrefix("clang -cc1as"); |
Dylan Noblesmith | c95d819 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 522 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 523 | DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagClient); |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 524 | |
| 525 | // Set an error handler, so that any LLVM backend diagnostics go through our |
| 526 | // error handler. |
Dan Gohman | b37af7d | 2010-08-18 21:23:17 +0000 | [diff] [blame] | 527 | ScopedFatalErrorHandler FatalErrorHandler |
| 528 | (LLVMErrorHandler, static_cast<void*>(&Diags)); |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 529 | |
| 530 | // Parse the arguments. |
| 531 | AssemblerInvocation Asm; |
Sean Silva | 070cd2d | 2014-08-15 21:38:36 +0000 | [diff] [blame] | 532 | if (!AssemblerInvocation::CreateFromArgs(Asm, Argv, Diags)) |
Dylan Noblesmith | e99b27f | 2011-12-23 03:05:38 +0000 | [diff] [blame] | 533 | return 1; |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 534 | |
Daniel Dunbar | 0454f65 | 2010-05-20 18:15:20 +0000 | [diff] [blame] | 535 | if (Asm.ShowHelp) { |
Alp Toker | 61dad75 | 2014-07-09 14:05:11 +0000 | [diff] [blame] | 536 | std::unique_ptr<OptTable> Opts(driver::createDriverOptTable()); |
Fangrui Song | d002823 | 2018-10-10 00:15:33 +0000 | [diff] [blame] | 537 | Opts->PrintHelp(llvm::outs(), "clang -cc1as [options] file...", |
| 538 | "Clang Integrated Assembler", |
George Rimar | 5dbfa4e | 2017-07-26 09:10:17 +0000 | [diff] [blame] | 539 | /*Include=*/driver::options::CC1AsOption, /*Exclude=*/0, |
| 540 | /*ShowAllAliases=*/false); |
Daniel Dunbar | 0454f65 | 2010-05-20 18:15:20 +0000 | [diff] [blame] | 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | // Honor -version. |
| 545 | // |
| 546 | // FIXME: Use a better -version message? |
| 547 | if (Asm.ShowVersion) { |
| 548 | llvm::cl::PrintVersionMessage(); |
| 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | // Honor -mllvm. |
| 553 | // |
| 554 | // FIXME: Remove this, one day. |
| 555 | if (!Asm.LLVMArgs.empty()) { |
| 556 | unsigned NumArgs = Asm.LLVMArgs.size(); |
Kostya Serebryany | 5af4515 | 2017-04-19 20:57:13 +0000 | [diff] [blame] | 557 | auto Args = llvm::make_unique<const char*[]>(NumArgs + 2); |
Daniel Dunbar | 0454f65 | 2010-05-20 18:15:20 +0000 | [diff] [blame] | 558 | Args[0] = "clang (LLVM option parsing)"; |
| 559 | for (unsigned i = 0; i != NumArgs; ++i) |
| 560 | Args[i + 1] = Asm.LLVMArgs[i].c_str(); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 561 | Args[NumArgs + 1] = nullptr; |
Kostya Serebryany | 5af4515 | 2017-04-19 20:57:13 +0000 | [diff] [blame] | 562 | llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get()); |
Daniel Dunbar | 0454f65 | 2010-05-20 18:15:20 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 565 | // Execute the invocation, unless there were parsing errors. |
Alp Toker | 4429314 | 2014-05-31 00:02:27 +0000 | [diff] [blame] | 566 | bool Failed = Diags.hasErrorOccurred() || ExecuteAssembler(Asm, Diags); |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 567 | |
| 568 | // If any timers were active but haven't been destroyed yet, print their |
| 569 | // results now. |
| 570 | TimerGroup::printAll(errs()); |
| 571 | |
Alp Toker | 4429314 | 2014-05-31 00:02:27 +0000 | [diff] [blame] | 572 | return !!Failed; |
Daniel Dunbar | 2fcaa54 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 573 | } |