blob: 84aeced4115d9dc11fdd38a7d11e7af16457204f [file] [log] [blame]
Daniel Dunbar41b5b172010-05-20 17:49:16 +00001//===-- 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 Sands2dc14532010-08-30 09:42:39 +000027#include "llvm/ADT/Triple.h"
Daniel Dunbar7374f1b2010-07-17 02:26:21 +000028#include "llvm/MC/MCParser/MCAsmParser.h"
29#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbar41b5b172010-05-20 17:49:16 +000030#include "llvm/MC/MCCodeEmitter.h"
31#include "llvm/MC/MCContext.h"
Evan Cheng74e13322011-07-11 04:24:19 +000032#include "llvm/MC/MCInstrInfo.h"
Evan Cheng36fc3aa2011-07-20 06:22:27 +000033#include "llvm/MC/MCObjectFileInfo.h"
Evan Cheng884744b2011-07-18 20:57:51 +000034#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbar41b5b172010-05-20 17:49:16 +000035#include "llvm/MC/MCStreamer.h"
Evan Chengbb36ed92011-07-09 06:04:17 +000036#include "llvm/MC/MCSubtargetInfo.h"
Evan Cheng21118dc2011-07-25 23:25:09 +000037#include "llvm/MC/MCAsmBackend.h"
Evan Cheng37712352011-07-26 00:24:45 +000038#include "llvm/MC/MCTargetAsmParser.h"
Daniel Dunbarc673af72010-05-20 18:15:20 +000039#include "llvm/Support/CommandLine.h"
Daniel Dunbar41b5b172010-05-20 17:49:16 +000040#include "llvm/Support/FormattedStream.h"
41#include "llvm/Support/ErrorHandling.h"
42#include "llvm/Support/ManagedStatic.h"
43#include "llvm/Support/MemoryBuffer.h"
44#include "llvm/Support/PrettyStackTrace.h"
45#include "llvm/Support/SourceMgr.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000046#include "llvm/Support/Host.h"
47#include "llvm/Support/Path.h"
48#include "llvm/Support/Signals.h"
Evan Chenga6b40452011-08-24 18:09:14 +000049#include "llvm/Support/TargetRegistry.h"
50#include "llvm/Support/TargetSelect.h"
51#include "llvm/Support/Timer.h"
52#include "llvm/Support/raw_ostream.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000053#include "llvm/Support/system_error.h"
Daniel Dunbar41b5b172010-05-20 17:49:16 +000054#include "llvm/Target/TargetData.h"
Daniel Dunbar41b5b172010-05-20 17:49:16 +000055using namespace clang;
56using namespace clang::driver;
57using namespace llvm;
58
59namespace {
60
61/// \brief Helper class for representing a single invocation of the assembler.
62struct 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;
Daniel Dunbar402adc32011-03-28 22:49:24 +000074 unsigned SaveTemporaryLabels : 1;
Kevin Enderby567003e2011-12-22 19:31:58 +000075 unsigned GenDwarfForAssembly : 1;
76 std::string DwarfDebugFlags;
Daniel Dunbar41b5b172010-05-20 17:49:16 +000077
78 /// @}
79 /// @name Frontend Options
80 /// @{
81
82 std::string InputFile;
Daniel Dunbarc673af72010-05-20 18:15:20 +000083 std::vector<std::string> LLVMArgs;
Daniel Dunbar41b5b172010-05-20 17:49:16 +000084 std::string OutputPath;
85 enum FileType {
86 FT_Asm, ///< Assembly (.s) output, transliterate mode.
87 FT_Null, ///< No output, for timing purposes.
88 FT_Obj ///< Object file output.
89 };
90 FileType OutputType;
Daniel Dunbarc673af72010-05-20 18:15:20 +000091 unsigned ShowHelp : 1;
92 unsigned ShowVersion : 1;
Daniel Dunbar41b5b172010-05-20 17:49:16 +000093
94 /// @}
95 /// @name Transliterate Options
96 /// @{
97
98 unsigned OutputAsmVariant;
99 unsigned ShowEncoding : 1;
100 unsigned ShowInst : 1;
101
102 /// @}
103 /// @name Assembler Options
104 /// @{
105
106 unsigned RelaxAll : 1;
Rafael Espindola3176cca2011-01-23 17:58:26 +0000107 unsigned NoExecStack : 1;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000108
109 /// @}
110
111public:
112 AssemblerInvocation() {
113 Triple = "";
114 NoInitialTextSection = 0;
115 InputFile = "-";
Daniel Dunbarc673af72010-05-20 18:15:20 +0000116 OutputPath = "-";
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000117 OutputType = FT_Asm;
118 OutputAsmVariant = 0;
119 ShowInst = 0;
120 ShowEncoding = 0;
121 RelaxAll = 0;
Rafael Espindola3176cca2011-01-23 17:58:26 +0000122 NoExecStack = 0;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000123 }
124
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000125 static bool CreateFromArgs(AssemblerInvocation &Res, const char **ArgBegin,
David Blaikied6471f72011-09-25 23:23:43 +0000126 const char **ArgEnd, DiagnosticsEngine &Diags);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000127};
128
129}
130
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000131bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000132 const char **ArgBegin,
133 const char **ArgEnd,
David Blaikied6471f72011-09-25 23:23:43 +0000134 DiagnosticsEngine &Diags) {
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000135 using namespace clang::driver::cc1asoptions;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000136 bool Success = true;
137
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000138 // Parse the arguments.
139 OwningPtr<OptTable> OptTbl(createCC1AsOptTable());
140 unsigned MissingArgIndex, MissingArgCount;
141 OwningPtr<InputArgList> Args(
142 OptTbl->ParseArgs(ArgBegin, ArgEnd,MissingArgIndex, MissingArgCount));
143
144 // Check for missing argument error.
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000145 if (MissingArgCount) {
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000146 Diags.Report(diag::err_drv_missing_argument)
147 << Args->getArgString(MissingArgIndex) << MissingArgCount;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000148 Success = false;
149 }
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000150
151 // Issue errors on unknown arguments.
152 for (arg_iterator it = Args->filtered_begin(cc1asoptions::OPT_UNKNOWN),
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000153 ie = Args->filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000154 Diags.Report(diag::err_drv_unknown_argument) << (*it) ->getAsString(*Args);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000155 Success = false;
156 }
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000157
158 // Construct the invocation.
159
160 // Target Options
Duncan Sands2dc14532010-08-30 09:42:39 +0000161 Opts.Triple = Triple::normalize(Args->getLastArgValue(OPT_triple));
Sebastian Pop5d8b9542011-11-01 21:33:06 +0000162 if (Opts.Triple.empty()) // Use the default target triple if unspecified.
163 Opts.Triple = sys::getDefaultTargetTriple();
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000164
165 // Language Options
166 Opts.IncludePaths = Args->getAllArgValues(OPT_I);
167 Opts.NoInitialTextSection = Args->hasArg(OPT_n);
Daniel Dunbar402adc32011-03-28 22:49:24 +0000168 Opts.SaveTemporaryLabels = Args->hasArg(OPT_L);
Kevin Enderby567003e2011-12-22 19:31:58 +0000169 Opts.GenDwarfForAssembly = Args->hasArg(OPT_g);
170 Opts.DwarfDebugFlags = Args->getLastArgValue(OPT_dwarf_debug_flags);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000171
172 // Frontend Options
173 if (Args->hasArg(OPT_INPUT)) {
174 bool First = true;
175 for (arg_iterator it = Args->filtered_begin(OPT_INPUT),
176 ie = Args->filtered_end(); it != ie; ++it, First=false) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000177 const Arg *A = it;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000178 if (First)
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000179 Opts.InputFile = A->getValue(*Args);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000180 else {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000181 Diags.Report(diag::err_drv_unknown_argument) << A->getAsString(*Args);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000182 Success = false;
183 }
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000184 }
185 }
Daniel Dunbarc673af72010-05-20 18:15:20 +0000186 Opts.LLVMArgs = Args->getAllArgValues(OPT_mllvm);
Joerg Sonnenbergerd7933502011-05-19 18:42:29 +0000187 if (Args->hasArg(OPT_fatal_warnings))
188 Opts.LLVMArgs.push_back("-fatal-assembler-warnings");
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000189 Opts.OutputPath = Args->getLastArgValue(OPT_o);
190 if (Arg *A = Args->getLastArg(OPT_filetype)) {
191 StringRef Name = A->getValue(*Args);
192 unsigned OutputType = StringSwitch<unsigned>(Name)
193 .Case("asm", FT_Asm)
194 .Case("null", FT_Null)
195 .Case("obj", FT_Obj)
196 .Default(~0U);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000197 if (OutputType == ~0U) {
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000198 Diags.Report(diag::err_drv_invalid_value)
199 << A->getAsString(*Args) << Name;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000200 Success = false;
201 } else
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000202 Opts.OutputType = FileType(OutputType);
203 }
Daniel Dunbarc673af72010-05-20 18:15:20 +0000204 Opts.ShowHelp = Args->hasArg(OPT_help);
205 Opts.ShowVersion = Args->hasArg(OPT_version);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000206
207 // Transliterate Options
208 Opts.OutputAsmVariant = Args->getLastArgIntValue(OPT_output_asm_variant,
209 0, Diags);
210 Opts.ShowEncoding = Args->hasArg(OPT_show_encoding);
211 Opts.ShowInst = Args->hasArg(OPT_show_inst);
212
213 // Assemble Options
214 Opts.RelaxAll = Args->hasArg(OPT_relax_all);
Rafael Espindola3176cca2011-01-23 17:58:26 +0000215 Opts.NoExecStack = Args->hasArg(OPT_no_exec_stack);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000216
Dylan Noblesmith89ea4162011-12-26 19:29:47 +0000217 return Success;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000218}
219
220static formatted_raw_ostream *GetOutputStream(AssemblerInvocation &Opts,
David Blaikied6471f72011-09-25 23:23:43 +0000221 DiagnosticsEngine &Diags,
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000222 bool Binary) {
Daniel Dunbarc673af72010-05-20 18:15:20 +0000223 if (Opts.OutputPath.empty())
224 Opts.OutputPath = "-";
225
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000226 // Make sure that the Out file gets unlinked from the disk if we get a
227 // SIGINT.
228 if (Opts.OutputPath != "-")
229 sys::RemoveFileOnSignal(sys::Path(Opts.OutputPath));
230
231 std::string Error;
232 raw_fd_ostream *Out =
233 new raw_fd_ostream(Opts.OutputPath.c_str(), Error,
234 (Binary ? raw_fd_ostream::F_Binary : 0));
235 if (!Error.empty()) {
236 Diags.Report(diag::err_fe_unable_to_open_output)
237 << Opts.OutputPath << Error;
238 return 0;
239 }
240
241 return new formatted_raw_ostream(*Out, formatted_raw_ostream::DELETE_STREAM);
242}
243
David Blaikied6471f72011-09-25 23:23:43 +0000244static bool ExecuteAssembler(AssemblerInvocation &Opts,
245 DiagnosticsEngine &Diags) {
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000246 // Get the target specific parser.
247 std::string Error;
248 const Target *TheTarget(TargetRegistry::lookupTarget(Opts.Triple, Error));
249 if (!TheTarget) {
250 Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
251 return false;
252 }
253
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000254 OwningPtr<MemoryBuffer> BufferPtr;
255 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Opts.InputFile, BufferPtr)) {
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000256 Error = ec.message();
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000257 Diags.Report(diag::err_fe_error_reading) << Opts.InputFile;
258 return false;
259 }
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000260 MemoryBuffer *Buffer = BufferPtr.take();
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000261
262 SourceMgr SrcMgr;
263
264 // Tell SrcMgr about this buffer, which is what the parser will pick up.
265 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
266
267 // Record the location of the include directories so that the lexer can find
268 // it later.
269 SrcMgr.setIncludeDirs(Opts.IncludePaths);
270
Evan Chenge27eb592011-07-14 23:50:56 +0000271 OwningPtr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(Opts.Triple));
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000272 assert(MAI && "Unable to create target asm info!");
273
Evan Cheng884744b2011-07-18 20:57:51 +0000274 OwningPtr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(Opts.Triple));
275 assert(MRI && "Unable to create target register info!");
276
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000277 bool IsBinary = Opts.OutputType == AssemblerInvocation::FT_Obj;
278 formatted_raw_ostream *Out = GetOutputStream(Opts, Diags, IsBinary);
279 if (!Out)
280 return false;
281
Evan Cheng36fc3aa2011-07-20 06:22:27 +0000282 // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
283 // MCObjectFileInfo needs a MCContext reference in order to initialize itself.
284 OwningPtr<MCObjectFileInfo> MOFI(new MCObjectFileInfo());
Jim Grosbach6a9a6d22012-02-02 17:54:07 +0000285 MCContext Ctx(*MAI, *MRI, MOFI.get(), &SrcMgr);
Evan Cheng36fc3aa2011-07-20 06:22:27 +0000286 // FIXME: Assembler behavior can change with -static.
Evan Cheng66488ed2011-07-20 19:53:19 +0000287 MOFI->InitMCObjectFileInfo(Opts.Triple,
288 Reloc::Default, CodeModel::Default, Ctx);
Daniel Dunbar402adc32011-03-28 22:49:24 +0000289 if (Opts.SaveTemporaryLabels)
290 Ctx.setAllowTemporaryLabels(false);
Kevin Enderby567003e2011-12-22 19:31:58 +0000291 if (Opts.GenDwarfForAssembly)
292 Ctx.setGenDwarfForAssembly(true);
293 if (!Opts.DwarfDebugFlags.empty())
294 Ctx.setDwarfDebugFlags(StringRef(Opts.DwarfDebugFlags));
Rafael Espindola7872d482010-12-10 07:40:14 +0000295
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000296 OwningPtr<MCStreamer> Str;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000297
Evan Chengccb21e42011-07-26 01:49:26 +0000298 OwningPtr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
299 OwningPtr<MCSubtargetInfo>
300 STI(TheTarget->createMCSubtargetInfo(Opts.Triple, "", ""));
Evan Cheng74e13322011-07-11 04:24:19 +0000301
Rafael Espindola3176cca2011-01-23 17:58:26 +0000302 // FIXME: There is a bit of code duplication with addPassesToEmitFile.
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000303 if (Opts.OutputType == AssemblerInvocation::FT_Asm) {
304 MCInstPrinter *IP =
James Molloy066d5022011-09-07 17:25:30 +0000305 TheTarget->createMCInstPrinter(Opts.OutputAsmVariant, *MAI, *STI);
Benjamin Kramer7ac3d5a2010-07-29 17:48:03 +0000306 MCCodeEmitter *CE = 0;
Evan Cheng21118dc2011-07-25 23:25:09 +0000307 MCAsmBackend *MAB = 0;
Daniel Dunbar66cdf262010-12-16 03:06:05 +0000308 if (Opts.ShowEncoding) {
Evan Chengccb21e42011-07-26 01:49:26 +0000309 CE = TheTarget->createMCCodeEmitter(*MCII, *STI, Ctx);
Evan Cheng21118dc2011-07-25 23:25:09 +0000310 MAB = TheTarget->createMCAsmBackend(Opts.Triple);
Daniel Dunbar66cdf262010-12-16 03:06:05 +0000311 }
Rafael Espindola7872d482010-12-10 07:40:14 +0000312 Str.reset(TheTarget->createAsmStreamer(Ctx, *Out, /*asmverbose*/true,
Rafael Espindola1bf39022011-04-30 03:46:18 +0000313 /*useLoc*/ true,
Nick Lewyckyea523d72011-10-17 23:05:52 +0000314 /*useCFI*/ true,
315 /*useDwarfDirectory*/ true,
316 IP, CE, MAB,
Rafael Espindola7872d482010-12-10 07:40:14 +0000317 Opts.ShowInst));
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000318 } else if (Opts.OutputType == AssemblerInvocation::FT_Null) {
319 Str.reset(createNullStreamer(Ctx));
320 } else {
321 assert(Opts.OutputType == AssemblerInvocation::FT_Obj &&
322 "Invalid file type!");
Evan Chengccb21e42011-07-26 01:49:26 +0000323 MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, *STI, Ctx);
Evan Cheng21118dc2011-07-25 23:25:09 +0000324 MCAsmBackend *MAB = TheTarget->createMCAsmBackend(Opts.Triple);
Evan Cheng7b6def72011-07-26 00:42:40 +0000325 Str.reset(TheTarget->createMCObjectStreamer(Opts.Triple, Ctx, *MAB, *Out,
326 CE, Opts.RelaxAll,
327 Opts.NoExecStack));
Rafael Espindola4f036fa2010-10-13 14:53:57 +0000328 Str.get()->InitSections();
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000329 }
330
Jim Grosbach09190be2011-08-16 18:33:55 +0000331 OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr, Ctx,
Daniel Dunbar7374f1b2010-07-17 02:26:21 +0000332 *Str.get(), *MAI));
Evan Chengccb21e42011-07-26 01:49:26 +0000333 OwningPtr<MCTargetAsmParser> TAP(TheTarget->createMCAsmParser(*STI, *Parser));
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000334 if (!TAP) {
335 Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
336 return false;
337 }
338
Daniel Dunbar7374f1b2010-07-17 02:26:21 +0000339 Parser->setTargetParser(*TAP.get());
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000340
Daniel Dunbar7374f1b2010-07-17 02:26:21 +0000341 bool Success = !Parser->Run(Opts.NoInitialTextSection);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000342
343 // Close the output.
344 delete Out;
345
346 // Delete output on errors.
347 if (!Success && Opts.OutputPath != "-")
348 sys::Path(Opts.OutputPath).eraseFromDisk();
349
350 return Success;
351}
352
353static void LLVMErrorHandler(void *UserData, const std::string &Message) {
David Blaikied6471f72011-09-25 23:23:43 +0000354 DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000355
356 Diags.Report(diag::err_fe_error_backend) << Message;
357
358 // We cannot recover from llvm errors.
359 exit(1);
360}
361
362int cc1as_main(const char **ArgBegin, const char **ArgEnd,
363 const char *Argv0, void *MainAddr) {
364 // Print a stack trace if we signal out.
365 sys::PrintStackTraceOnErrorSignal();
366 PrettyStackTraceProgram X(ArgEnd - ArgBegin, ArgBegin);
367 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
368
369 // Initialize targets and assembly printers/parsers.
370 InitializeAllTargetInfos();
Evan Chengd99d3e12011-07-22 21:59:11 +0000371 InitializeAllTargetMCs();
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000372 InitializeAllAsmParsers();
373
374 // Construct our diagnostic client.
Douglas Gregorbdbb0042010-08-18 22:29:43 +0000375 TextDiagnosticPrinter *DiagClient
376 = new TextDiagnosticPrinter(errs(), DiagnosticOptions());
377 DiagClient->setPrefix("clang -cc1as");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000378 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
David Blaikied6471f72011-09-25 23:23:43 +0000379 DiagnosticsEngine Diags(DiagID, DiagClient);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000380
381 // Set an error handler, so that any LLVM backend diagnostics go through our
382 // error handler.
Dan Gohman726578c2010-08-18 21:23:17 +0000383 ScopedFatalErrorHandler FatalErrorHandler
384 (LLVMErrorHandler, static_cast<void*>(&Diags));
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000385
386 // Parse the arguments.
387 AssemblerInvocation Asm;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000388 if (!AssemblerInvocation::CreateFromArgs(Asm, ArgBegin, ArgEnd, Diags))
389 return 1;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000390
Daniel Dunbarc673af72010-05-20 18:15:20 +0000391 // Honor -help.
392 if (Asm.ShowHelp) {
393 llvm::OwningPtr<driver::OptTable> Opts(driver::createCC1AsOptTable());
394 Opts->PrintHelp(llvm::outs(), "clang -cc1as", "Clang Integrated Assembler");
395 return 0;
396 }
397
398 // Honor -version.
399 //
400 // FIXME: Use a better -version message?
401 if (Asm.ShowVersion) {
402 llvm::cl::PrintVersionMessage();
403 return 0;
404 }
405
406 // Honor -mllvm.
407 //
408 // FIXME: Remove this, one day.
409 if (!Asm.LLVMArgs.empty()) {
410 unsigned NumArgs = Asm.LLVMArgs.size();
411 const char **Args = new const char*[NumArgs + 2];
412 Args[0] = "clang (LLVM option parsing)";
413 for (unsigned i = 0; i != NumArgs; ++i)
414 Args[i + 1] = Asm.LLVMArgs[i].c_str();
415 Args[NumArgs + 1] = 0;
David Blaikie6bd17d22012-02-07 19:36:38 +0000416 llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args);
Daniel Dunbarc673af72010-05-20 18:15:20 +0000417 }
418
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000419 // Execute the invocation, unless there were parsing errors.
420 bool Success = false;
Argyrios Kyrtzidise8f0ba72010-11-19 00:19:18 +0000421 if (!Diags.hasErrorOccurred())
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000422 Success = ExecuteAssembler(Asm, Diags);
423
424 // If any timers were active but haven't been destroyed yet, print their
425 // results now.
426 TimerGroup::printAll(errs());
427
428 return !Success;
429}