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