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" |
| 16 | #include "clang/Driver/Arg.h" |
| 17 | #include "clang/Driver/ArgList.h" |
| 18 | #include "clang/Driver/DriverDiagnostic.h" |
| 19 | #include "clang/Driver/CC1AsOptions.h" |
| 20 | #include "clang/Driver/OptTable.h" |
| 21 | #include "clang/Driver/Options.h" |
| 22 | #include "clang/Frontend/DiagnosticOptions.h" |
| 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" |
Daniel Dunbar | 7374f1b | 2010-07-17 02:26:21 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCParser/MCAsmParser.h" |
| 29 | #include "llvm/MC/MCAsmInfo.h" |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 30 | #include "llvm/MC/MCCodeEmitter.h" |
| 31 | #include "llvm/MC/MCContext.h" |
| 32 | #include "llvm/MC/MCStreamer.h" |
Daniel Dunbar | c673af7 | 2010-05-20 18:15:20 +0000 | [diff] [blame] | 33 | #include "llvm/Support/CommandLine.h" |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 34 | #include "llvm/Support/FormattedStream.h" |
| 35 | #include "llvm/Support/ErrorHandling.h" |
| 36 | #include "llvm/Support/ManagedStatic.h" |
| 37 | #include "llvm/Support/MemoryBuffer.h" |
| 38 | #include "llvm/Support/PrettyStackTrace.h" |
| 39 | #include "llvm/Support/SourceMgr.h" |
| 40 | #include "llvm/Support/Timer.h" |
| 41 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 42 | #include "llvm/Support/Host.h" |
| 43 | #include "llvm/Support/Path.h" |
| 44 | #include "llvm/Support/Signals.h" |
Michael J. Spencer | 3a321e2 | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 45 | #include "llvm/Support/system_error.h" |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 46 | #include "llvm/Target/TargetAsmBackend.h" |
Rafael Espindola | 7872d48 | 2010-12-10 07:40:14 +0000 | [diff] [blame^] | 47 | #include "llvm/Target/TargetAsmInfo.h" |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 48 | #include "llvm/Target/TargetAsmParser.h" |
| 49 | #include "llvm/Target/TargetData.h" |
Rafael Espindola | 7872d48 | 2010-12-10 07:40:14 +0000 | [diff] [blame^] | 50 | #include "llvm/Target/TargetLowering.h" |
| 51 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 52 | #include "llvm/Target/TargetMachine.h" |
| 53 | #include "llvm/Target/TargetRegistry.h" |
| 54 | #include "llvm/Target/TargetSelect.h" |
| 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 | |
| 66 | std::string Triple; |
| 67 | |
| 68 | /// @} |
| 69 | /// @name Language Options |
| 70 | /// @{ |
| 71 | |
| 72 | std::vector<std::string> IncludePaths; |
| 73 | unsigned NoInitialTextSection : 1; |
| 74 | |
| 75 | /// @} |
| 76 | /// @name Frontend Options |
| 77 | /// @{ |
| 78 | |
| 79 | std::string InputFile; |
Daniel Dunbar | c673af7 | 2010-05-20 18:15:20 +0000 | [diff] [blame] | 80 | std::vector<std::string> LLVMArgs; |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 81 | std::string OutputPath; |
| 82 | enum FileType { |
| 83 | FT_Asm, ///< Assembly (.s) output, transliterate mode. |
| 84 | FT_Null, ///< No output, for timing purposes. |
| 85 | FT_Obj ///< Object file output. |
| 86 | }; |
| 87 | FileType OutputType; |
Daniel Dunbar | c673af7 | 2010-05-20 18:15:20 +0000 | [diff] [blame] | 88 | unsigned ShowHelp : 1; |
| 89 | unsigned ShowVersion : 1; |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 90 | |
| 91 | /// @} |
| 92 | /// @name Transliterate Options |
| 93 | /// @{ |
| 94 | |
| 95 | unsigned OutputAsmVariant; |
| 96 | unsigned ShowEncoding : 1; |
| 97 | unsigned ShowInst : 1; |
| 98 | |
| 99 | /// @} |
| 100 | /// @name Assembler Options |
| 101 | /// @{ |
| 102 | |
| 103 | unsigned RelaxAll : 1; |
| 104 | |
| 105 | /// @} |
| 106 | |
| 107 | public: |
| 108 | AssemblerInvocation() { |
| 109 | Triple = ""; |
| 110 | NoInitialTextSection = 0; |
| 111 | InputFile = "-"; |
Daniel Dunbar | c673af7 | 2010-05-20 18:15:20 +0000 | [diff] [blame] | 112 | OutputPath = "-"; |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 113 | OutputType = FT_Asm; |
| 114 | OutputAsmVariant = 0; |
| 115 | ShowInst = 0; |
| 116 | ShowEncoding = 0; |
| 117 | RelaxAll = 0; |
| 118 | } |
| 119 | |
| 120 | static void CreateFromArgs(AssemblerInvocation &Res, const char **ArgBegin, |
| 121 | const char **ArgEnd, Diagnostic &Diags); |
| 122 | }; |
| 123 | |
| 124 | } |
| 125 | |
| 126 | void AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts, |
| 127 | const char **ArgBegin, |
| 128 | const char **ArgEnd, |
| 129 | Diagnostic &Diags) { |
| 130 | using namespace clang::driver::cc1asoptions; |
| 131 | // Parse the arguments. |
| 132 | OwningPtr<OptTable> OptTbl(createCC1AsOptTable()); |
| 133 | unsigned MissingArgIndex, MissingArgCount; |
| 134 | OwningPtr<InputArgList> Args( |
| 135 | OptTbl->ParseArgs(ArgBegin, ArgEnd,MissingArgIndex, MissingArgCount)); |
| 136 | |
| 137 | // Check for missing argument error. |
| 138 | if (MissingArgCount) |
| 139 | Diags.Report(diag::err_drv_missing_argument) |
| 140 | << Args->getArgString(MissingArgIndex) << MissingArgCount; |
| 141 | |
| 142 | // Issue errors on unknown arguments. |
| 143 | for (arg_iterator it = Args->filtered_begin(cc1asoptions::OPT_UNKNOWN), |
| 144 | ie = Args->filtered_end(); it != ie; ++it) |
Daniel Dunbar | 7e4953e | 2010-06-11 22:00:13 +0000 | [diff] [blame] | 145 | Diags.Report(diag::err_drv_unknown_argument) << (*it) ->getAsString(*Args); |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 146 | |
| 147 | // Construct the invocation. |
| 148 | |
| 149 | // Target Options |
Duncan Sands | 2dc1453 | 2010-08-30 09:42:39 +0000 | [diff] [blame] | 150 | Opts.Triple = Triple::normalize(Args->getLastArgValue(OPT_triple)); |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 151 | if (Opts.Triple.empty()) // Use the host triple if unspecified. |
| 152 | Opts.Triple = sys::getHostTriple(); |
| 153 | |
| 154 | // Language Options |
| 155 | Opts.IncludePaths = Args->getAllArgValues(OPT_I); |
| 156 | Opts.NoInitialTextSection = Args->hasArg(OPT_n); |
| 157 | |
| 158 | // Frontend Options |
| 159 | if (Args->hasArg(OPT_INPUT)) { |
| 160 | bool First = true; |
| 161 | for (arg_iterator it = Args->filtered_begin(OPT_INPUT), |
| 162 | ie = Args->filtered_end(); it != ie; ++it, First=false) { |
Daniel Dunbar | 7e4953e | 2010-06-11 22:00:13 +0000 | [diff] [blame] | 163 | const Arg *A = it; |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 164 | if (First) |
Daniel Dunbar | 7e4953e | 2010-06-11 22:00:13 +0000 | [diff] [blame] | 165 | Opts.InputFile = A->getValue(*Args); |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 166 | else |
Daniel Dunbar | 7e4953e | 2010-06-11 22:00:13 +0000 | [diff] [blame] | 167 | Diags.Report(diag::err_drv_unknown_argument) << A->getAsString(*Args); |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
Daniel Dunbar | c673af7 | 2010-05-20 18:15:20 +0000 | [diff] [blame] | 170 | Opts.LLVMArgs = Args->getAllArgValues(OPT_mllvm); |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 171 | Opts.OutputPath = Args->getLastArgValue(OPT_o); |
| 172 | if (Arg *A = Args->getLastArg(OPT_filetype)) { |
| 173 | StringRef Name = A->getValue(*Args); |
| 174 | unsigned OutputType = StringSwitch<unsigned>(Name) |
| 175 | .Case("asm", FT_Asm) |
| 176 | .Case("null", FT_Null) |
| 177 | .Case("obj", FT_Obj) |
| 178 | .Default(~0U); |
| 179 | if (OutputType == ~0U) |
| 180 | Diags.Report(diag::err_drv_invalid_value) |
| 181 | << A->getAsString(*Args) << Name; |
| 182 | else |
| 183 | Opts.OutputType = FileType(OutputType); |
| 184 | } |
Daniel Dunbar | c673af7 | 2010-05-20 18:15:20 +0000 | [diff] [blame] | 185 | Opts.ShowHelp = Args->hasArg(OPT_help); |
| 186 | Opts.ShowVersion = Args->hasArg(OPT_version); |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 187 | |
| 188 | // Transliterate Options |
| 189 | Opts.OutputAsmVariant = Args->getLastArgIntValue(OPT_output_asm_variant, |
| 190 | 0, Diags); |
| 191 | Opts.ShowEncoding = Args->hasArg(OPT_show_encoding); |
| 192 | Opts.ShowInst = Args->hasArg(OPT_show_inst); |
| 193 | |
| 194 | // Assemble Options |
| 195 | Opts.RelaxAll = Args->hasArg(OPT_relax_all); |
| 196 | } |
| 197 | |
| 198 | static formatted_raw_ostream *GetOutputStream(AssemblerInvocation &Opts, |
| 199 | Diagnostic &Diags, |
| 200 | bool Binary) { |
Daniel Dunbar | c673af7 | 2010-05-20 18:15:20 +0000 | [diff] [blame] | 201 | if (Opts.OutputPath.empty()) |
| 202 | Opts.OutputPath = "-"; |
| 203 | |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 204 | // Make sure that the Out file gets unlinked from the disk if we get a |
| 205 | // SIGINT. |
| 206 | if (Opts.OutputPath != "-") |
| 207 | sys::RemoveFileOnSignal(sys::Path(Opts.OutputPath)); |
| 208 | |
| 209 | std::string Error; |
| 210 | raw_fd_ostream *Out = |
| 211 | new raw_fd_ostream(Opts.OutputPath.c_str(), Error, |
| 212 | (Binary ? raw_fd_ostream::F_Binary : 0)); |
| 213 | if (!Error.empty()) { |
| 214 | Diags.Report(diag::err_fe_unable_to_open_output) |
| 215 | << Opts.OutputPath << Error; |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | return new formatted_raw_ostream(*Out, formatted_raw_ostream::DELETE_STREAM); |
| 220 | } |
| 221 | |
| 222 | static bool ExecuteAssembler(AssemblerInvocation &Opts, Diagnostic &Diags) { |
| 223 | // Get the target specific parser. |
| 224 | std::string Error; |
| 225 | const Target *TheTarget(TargetRegistry::lookupTarget(Opts.Triple, Error)); |
| 226 | if (!TheTarget) { |
| 227 | Diags.Report(diag::err_target_unknown_triple) << Opts.Triple; |
| 228 | return false; |
| 229 | } |
| 230 | |
Michael J. Spencer | 3a321e2 | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 231 | error_code ec; |
| 232 | MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(Opts.InputFile, ec); |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 233 | if (Buffer == 0) { |
Michael J. Spencer | 3a321e2 | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 234 | Error = ec.message(); |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 235 | Diags.Report(diag::err_fe_error_reading) << Opts.InputFile; |
| 236 | return false; |
| 237 | } |
| 238 | |
| 239 | SourceMgr SrcMgr; |
| 240 | |
| 241 | // Tell SrcMgr about this buffer, which is what the parser will pick up. |
| 242 | SrcMgr.AddNewSourceBuffer(Buffer, SMLoc()); |
| 243 | |
| 244 | // Record the location of the include directories so that the lexer can find |
| 245 | // it later. |
| 246 | SrcMgr.setIncludeDirs(Opts.IncludePaths); |
| 247 | |
| 248 | OwningPtr<MCAsmInfo> MAI(TheTarget->createAsmInfo(Opts.Triple)); |
| 249 | assert(MAI && "Unable to create target asm info!"); |
| 250 | |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 251 | bool IsBinary = Opts.OutputType == AssemblerInvocation::FT_Obj; |
| 252 | formatted_raw_ostream *Out = GetOutputStream(Opts, Diags, IsBinary); |
| 253 | if (!Out) |
| 254 | return false; |
| 255 | |
| 256 | // FIXME: We shouldn't need to do this (and link in codegen). |
| 257 | OwningPtr<TargetMachine> TM(TheTarget->createTargetMachine(Opts.Triple, "")); |
| 258 | if (!TM) { |
| 259 | Diags.Report(diag::err_target_unknown_triple) << Opts.Triple; |
| 260 | return false; |
| 261 | } |
| 262 | |
Rafael Espindola | 7872d48 | 2010-12-10 07:40:14 +0000 | [diff] [blame^] | 263 | const TargetAsmInfo *tai = new TargetAsmInfo(*TM); |
| 264 | MCContext Ctx(*MAI, tai); |
| 265 | |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 266 | OwningPtr<MCStreamer> Str; |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 267 | |
Rafael Espindola | 7872d48 | 2010-12-10 07:40:14 +0000 | [diff] [blame^] | 268 | const TargetLoweringObjectFile &TLOF = |
| 269 | TM->getTargetLowering()->getObjFileLowering(); |
| 270 | const_cast<TargetLoweringObjectFile&>(TLOF).Initialize(Ctx, *TM); |
| 271 | |
| 272 | |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 273 | if (Opts.OutputType == AssemblerInvocation::FT_Asm) { |
| 274 | MCInstPrinter *IP = |
| 275 | TheTarget->createMCInstPrinter(Opts.OutputAsmVariant, *MAI); |
Benjamin Kramer | 7ac3d5a | 2010-07-29 17:48:03 +0000 | [diff] [blame] | 276 | MCCodeEmitter *CE = 0; |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 277 | if (Opts.ShowEncoding) |
Benjamin Kramer | 7ac3d5a | 2010-07-29 17:48:03 +0000 | [diff] [blame] | 278 | CE = TheTarget->createCodeEmitter(*TM, Ctx); |
Rafael Espindola | 7872d48 | 2010-12-10 07:40:14 +0000 | [diff] [blame^] | 279 | Str.reset(TheTarget->createAsmStreamer(Ctx, *Out, /*asmverbose*/true, |
| 280 | /*useLoc*/ true, IP, CE, |
| 281 | Opts.ShowInst)); |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 282 | } else if (Opts.OutputType == AssemblerInvocation::FT_Null) { |
| 283 | Str.reset(createNullStreamer(Ctx)); |
| 284 | } else { |
| 285 | assert(Opts.OutputType == AssemblerInvocation::FT_Obj && |
| 286 | "Invalid file type!"); |
Benjamin Kramer | 7ac3d5a | 2010-07-29 17:48:03 +0000 | [diff] [blame] | 287 | MCCodeEmitter *CE = TheTarget->createCodeEmitter(*TM, Ctx); |
| 288 | TargetAsmBackend *TAB = TheTarget->createAsmBackend(Opts.Triple); |
| 289 | Str.reset(TheTarget->createObjectStreamer(Opts.Triple, Ctx, *TAB, *Out, |
| 290 | CE, Opts.RelaxAll)); |
Rafael Espindola | 4f036fa | 2010-10-13 14:53:57 +0000 | [diff] [blame] | 291 | Str.get()->InitSections(); |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Daniel Dunbar | 7374f1b | 2010-07-17 02:26:21 +0000 | [diff] [blame] | 294 | OwningPtr<MCAsmParser> Parser(createMCAsmParser(*TheTarget, SrcMgr, Ctx, |
| 295 | *Str.get(), *MAI)); |
Daniel Dunbar | e9122a3 | 2010-07-19 00:33:53 +0000 | [diff] [blame] | 296 | OwningPtr<TargetAsmParser> TAP(TheTarget->createAsmParser(*Parser, *TM)); |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 297 | if (!TAP) { |
| 298 | Diags.Report(diag::err_target_unknown_triple) << Opts.Triple; |
| 299 | return false; |
| 300 | } |
| 301 | |
Daniel Dunbar | 7374f1b | 2010-07-17 02:26:21 +0000 | [diff] [blame] | 302 | Parser->setTargetParser(*TAP.get()); |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 303 | |
Daniel Dunbar | 7374f1b | 2010-07-17 02:26:21 +0000 | [diff] [blame] | 304 | bool Success = !Parser->Run(Opts.NoInitialTextSection); |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 305 | |
| 306 | // Close the output. |
| 307 | delete Out; |
| 308 | |
| 309 | // Delete output on errors. |
| 310 | if (!Success && Opts.OutputPath != "-") |
| 311 | sys::Path(Opts.OutputPath).eraseFromDisk(); |
| 312 | |
| 313 | return Success; |
| 314 | } |
| 315 | |
| 316 | static void LLVMErrorHandler(void *UserData, const std::string &Message) { |
| 317 | Diagnostic &Diags = *static_cast<Diagnostic*>(UserData); |
| 318 | |
| 319 | Diags.Report(diag::err_fe_error_backend) << Message; |
| 320 | |
| 321 | // We cannot recover from llvm errors. |
| 322 | exit(1); |
| 323 | } |
| 324 | |
| 325 | int cc1as_main(const char **ArgBegin, const char **ArgEnd, |
| 326 | const char *Argv0, void *MainAddr) { |
| 327 | // Print a stack trace if we signal out. |
| 328 | sys::PrintStackTraceOnErrorSignal(); |
| 329 | PrettyStackTraceProgram X(ArgEnd - ArgBegin, ArgBegin); |
| 330 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 331 | |
| 332 | // Initialize targets and assembly printers/parsers. |
| 333 | InitializeAllTargetInfos(); |
| 334 | // FIXME: We shouldn't need to initialize the Target(Machine)s. |
| 335 | InitializeAllTargets(); |
| 336 | InitializeAllAsmPrinters(); |
| 337 | InitializeAllAsmParsers(); |
| 338 | |
| 339 | // Construct our diagnostic client. |
Douglas Gregor | bdbb004 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 340 | TextDiagnosticPrinter *DiagClient |
| 341 | = new TextDiagnosticPrinter(errs(), DiagnosticOptions()); |
| 342 | DiagClient->setPrefix("clang -cc1as"); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 343 | llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 344 | Diagnostic Diags(DiagID, DiagClient); |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 345 | |
| 346 | // Set an error handler, so that any LLVM backend diagnostics go through our |
| 347 | // error handler. |
Dan Gohman | 726578c | 2010-08-18 21:23:17 +0000 | [diff] [blame] | 348 | ScopedFatalErrorHandler FatalErrorHandler |
| 349 | (LLVMErrorHandler, static_cast<void*>(&Diags)); |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 350 | |
| 351 | // Parse the arguments. |
| 352 | AssemblerInvocation Asm; |
| 353 | AssemblerInvocation::CreateFromArgs(Asm, ArgBegin, ArgEnd, Diags); |
| 354 | |
Daniel Dunbar | c673af7 | 2010-05-20 18:15:20 +0000 | [diff] [blame] | 355 | // Honor -help. |
| 356 | if (Asm.ShowHelp) { |
| 357 | llvm::OwningPtr<driver::OptTable> Opts(driver::createCC1AsOptTable()); |
| 358 | Opts->PrintHelp(llvm::outs(), "clang -cc1as", "Clang Integrated Assembler"); |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | // Honor -version. |
| 363 | // |
| 364 | // FIXME: Use a better -version message? |
| 365 | if (Asm.ShowVersion) { |
| 366 | llvm::cl::PrintVersionMessage(); |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | // Honor -mllvm. |
| 371 | // |
| 372 | // FIXME: Remove this, one day. |
| 373 | if (!Asm.LLVMArgs.empty()) { |
| 374 | unsigned NumArgs = Asm.LLVMArgs.size(); |
| 375 | const char **Args = new const char*[NumArgs + 2]; |
| 376 | Args[0] = "clang (LLVM option parsing)"; |
| 377 | for (unsigned i = 0; i != NumArgs; ++i) |
| 378 | Args[i + 1] = Asm.LLVMArgs[i].c_str(); |
| 379 | Args[NumArgs + 1] = 0; |
| 380 | llvm::cl::ParseCommandLineOptions(NumArgs + 1, const_cast<char **>(Args)); |
| 381 | } |
| 382 | |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 383 | // Execute the invocation, unless there were parsing errors. |
| 384 | bool Success = false; |
Argyrios Kyrtzidis | e8f0ba7 | 2010-11-19 00:19:18 +0000 | [diff] [blame] | 385 | if (!Diags.hasErrorOccurred()) |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 386 | Success = ExecuteAssembler(Asm, Diags); |
| 387 | |
| 388 | // If any timers were active but haven't been destroyed yet, print their |
| 389 | // results now. |
| 390 | TimerGroup::printAll(errs()); |
| 391 | |
| 392 | return !Success; |
| 393 | } |