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