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