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