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