blob: 5587e404955c15d1453d881b98bebcf5488b7afa [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"
Daniel Dunbar41b5b172010-05-20 17:49:16 +000022#include "clang/Frontend/FrontendDiagnostic.h"
23#include "clang/Frontend/TextDiagnosticPrinter.h"
Douglas Gregor02c23eb2012-10-23 22:26:28 +000024#include "clang/Basic/DiagnosticOptions.h"
Daniel Dunbar41b5b172010-05-20 17:49:16 +000025#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"
Micah Villmow25a6a842012-10-08 16:25:52 +000054#include "llvm/DataLayout.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
Jim Grosbachfc308292012-02-10 20:37:10 +000066 /// The name of the target triple to assemble for.
Daniel Dunbar41b5b172010-05-20 17:49:16 +000067 std::string Triple;
68
Jim Grosbachfc308292012-02-10 20:37:10 +000069 /// 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 Dunbar41b5b172010-05-20 17:49:16 +000077 /// @}
78 /// @name Language Options
79 /// @{
80
81 std::vector<std::string> IncludePaths;
82 unsigned NoInitialTextSection : 1;
Daniel Dunbar402adc32011-03-28 22:49:24 +000083 unsigned SaveTemporaryLabels : 1;
Kevin Enderby567003e2011-12-22 19:31:58 +000084 unsigned GenDwarfForAssembly : 1;
85 std::string DwarfDebugFlags;
Daniel Dunbar41b5b172010-05-20 17:49:16 +000086
87 /// @}
88 /// @name Frontend Options
89 /// @{
90
91 std::string InputFile;
Daniel Dunbarc673af72010-05-20 18:15:20 +000092 std::vector<std::string> LLVMArgs;
Daniel Dunbar41b5b172010-05-20 17:49:16 +000093 std::string OutputPath;
94 enum FileType {
95 FT_Asm, ///< Assembly (.s) output, transliterate mode.
96 FT_Null, ///< No output, for timing purposes.
97 FT_Obj ///< Object file output.
98 };
99 FileType OutputType;
Daniel Dunbarc673af72010-05-20 18:15:20 +0000100 unsigned ShowHelp : 1;
101 unsigned ShowVersion : 1;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000102
103 /// @}
104 /// @name Transliterate Options
105 /// @{
106
107 unsigned OutputAsmVariant;
108 unsigned ShowEncoding : 1;
109 unsigned ShowInst : 1;
110
111 /// @}
112 /// @name Assembler Options
113 /// @{
114
115 unsigned RelaxAll : 1;
Rafael Espindola3176cca2011-01-23 17:58:26 +0000116 unsigned NoExecStack : 1;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000117
118 /// @}
119
120public:
121 AssemblerInvocation() {
122 Triple = "";
123 NoInitialTextSection = 0;
124 InputFile = "-";
Daniel Dunbarc673af72010-05-20 18:15:20 +0000125 OutputPath = "-";
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000126 OutputType = FT_Asm;
127 OutputAsmVariant = 0;
128 ShowInst = 0;
129 ShowEncoding = 0;
130 RelaxAll = 0;
Rafael Espindola3176cca2011-01-23 17:58:26 +0000131 NoExecStack = 0;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000132 }
133
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000134 static bool CreateFromArgs(AssemblerInvocation &Res, const char **ArgBegin,
David Blaikied6471f72011-09-25 23:23:43 +0000135 const char **ArgEnd, DiagnosticsEngine &Diags);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000136};
137
138}
139
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000140bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000141 const char **ArgBegin,
142 const char **ArgEnd,
David Blaikied6471f72011-09-25 23:23:43 +0000143 DiagnosticsEngine &Diags) {
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000144 using namespace clang::driver::cc1asoptions;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000145 bool Success = true;
146
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000147 // Parse the arguments.
148 OwningPtr<OptTable> OptTbl(createCC1AsOptTable());
149 unsigned MissingArgIndex, MissingArgCount;
150 OwningPtr<InputArgList> Args(
151 OptTbl->ParseArgs(ArgBegin, ArgEnd,MissingArgIndex, MissingArgCount));
152
153 // Check for missing argument error.
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000154 if (MissingArgCount) {
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000155 Diags.Report(diag::err_drv_missing_argument)
156 << Args->getArgString(MissingArgIndex) << MissingArgCount;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000157 Success = false;
158 }
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000159
160 // Issue errors on unknown arguments.
161 for (arg_iterator it = Args->filtered_begin(cc1asoptions::OPT_UNKNOWN),
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000162 ie = Args->filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000163 Diags.Report(diag::err_drv_unknown_argument) << (*it) ->getAsString(*Args);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000164 Success = false;
165 }
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000166
167 // Construct the invocation.
168
169 // Target Options
Jim Grosbachfc308292012-02-10 20:37:10 +0000170 Opts.Triple = llvm::Triple::normalize(Args->getLastArgValue(OPT_triple));
171 Opts.CPU = Args->getLastArgValue(OPT_target_cpu);
172 Opts.Features = Args->getAllArgValues(OPT_target_feature);
173
174 // Use the default target triple if unspecified.
175 if (Opts.Triple.empty())
176 Opts.Triple = llvm::sys::getDefaultTargetTriple();
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000177
178 // Language Options
179 Opts.IncludePaths = Args->getAllArgValues(OPT_I);
180 Opts.NoInitialTextSection = Args->hasArg(OPT_n);
Daniel Dunbar402adc32011-03-28 22:49:24 +0000181 Opts.SaveTemporaryLabels = Args->hasArg(OPT_L);
Kevin Enderby567003e2011-12-22 19:31:58 +0000182 Opts.GenDwarfForAssembly = Args->hasArg(OPT_g);
183 Opts.DwarfDebugFlags = Args->getLastArgValue(OPT_dwarf_debug_flags);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000184
185 // Frontend Options
186 if (Args->hasArg(OPT_INPUT)) {
187 bool First = true;
188 for (arg_iterator it = Args->filtered_begin(OPT_INPUT),
189 ie = Args->filtered_end(); it != ie; ++it, First=false) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000190 const Arg *A = it;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000191 if (First)
Richard Smith1d489cf2012-11-01 04:30:05 +0000192 Opts.InputFile = A->getValue();
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000193 else {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000194 Diags.Report(diag::err_drv_unknown_argument) << A->getAsString(*Args);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000195 Success = false;
196 }
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000197 }
198 }
Daniel Dunbarc673af72010-05-20 18:15:20 +0000199 Opts.LLVMArgs = Args->getAllArgValues(OPT_mllvm);
Joerg Sonnenbergerd7933502011-05-19 18:42:29 +0000200 if (Args->hasArg(OPT_fatal_warnings))
201 Opts.LLVMArgs.push_back("-fatal-assembler-warnings");
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000202 Opts.OutputPath = Args->getLastArgValue(OPT_o);
203 if (Arg *A = Args->getLastArg(OPT_filetype)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000204 StringRef Name = A->getValue();
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000205 unsigned OutputType = StringSwitch<unsigned>(Name)
206 .Case("asm", FT_Asm)
207 .Case("null", FT_Null)
208 .Case("obj", FT_Obj)
209 .Default(~0U);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000210 if (OutputType == ~0U) {
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000211 Diags.Report(diag::err_drv_invalid_value)
212 << A->getAsString(*Args) << Name;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000213 Success = false;
214 } else
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000215 Opts.OutputType = FileType(OutputType);
216 }
Daniel Dunbarc673af72010-05-20 18:15:20 +0000217 Opts.ShowHelp = Args->hasArg(OPT_help);
218 Opts.ShowVersion = Args->hasArg(OPT_version);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000219
220 // Transliterate Options
221 Opts.OutputAsmVariant = Args->getLastArgIntValue(OPT_output_asm_variant,
222 0, Diags);
223 Opts.ShowEncoding = Args->hasArg(OPT_show_encoding);
224 Opts.ShowInst = Args->hasArg(OPT_show_inst);
225
226 // Assemble Options
227 Opts.RelaxAll = Args->hasArg(OPT_relax_all);
Rafael Espindola3176cca2011-01-23 17:58:26 +0000228 Opts.NoExecStack = Args->hasArg(OPT_no_exec_stack);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000229
Dylan Noblesmith89ea4162011-12-26 19:29:47 +0000230 return Success;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000231}
232
233static formatted_raw_ostream *GetOutputStream(AssemblerInvocation &Opts,
David Blaikied6471f72011-09-25 23:23:43 +0000234 DiagnosticsEngine &Diags,
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000235 bool Binary) {
Daniel Dunbarc673af72010-05-20 18:15:20 +0000236 if (Opts.OutputPath.empty())
237 Opts.OutputPath = "-";
238
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000239 // Make sure that the Out file gets unlinked from the disk if we get a
240 // SIGINT.
241 if (Opts.OutputPath != "-")
242 sys::RemoveFileOnSignal(sys::Path(Opts.OutputPath));
243
244 std::string Error;
245 raw_fd_ostream *Out =
246 new raw_fd_ostream(Opts.OutputPath.c_str(), Error,
247 (Binary ? raw_fd_ostream::F_Binary : 0));
248 if (!Error.empty()) {
249 Diags.Report(diag::err_fe_unable_to_open_output)
250 << Opts.OutputPath << Error;
251 return 0;
252 }
253
254 return new formatted_raw_ostream(*Out, formatted_raw_ostream::DELETE_STREAM);
255}
256
David Blaikied6471f72011-09-25 23:23:43 +0000257static bool ExecuteAssembler(AssemblerInvocation &Opts,
258 DiagnosticsEngine &Diags) {
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000259 // Get the target specific parser.
260 std::string Error;
261 const Target *TheTarget(TargetRegistry::lookupTarget(Opts.Triple, Error));
262 if (!TheTarget) {
263 Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
264 return false;
265 }
266
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000267 OwningPtr<MemoryBuffer> BufferPtr;
268 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Opts.InputFile, BufferPtr)) {
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000269 Error = ec.message();
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000270 Diags.Report(diag::err_fe_error_reading) << Opts.InputFile;
271 return false;
272 }
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000273 MemoryBuffer *Buffer = BufferPtr.take();
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000274
275 SourceMgr SrcMgr;
276
277 // Tell SrcMgr about this buffer, which is what the parser will pick up.
278 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
279
280 // Record the location of the include directories so that the lexer can find
281 // it later.
282 SrcMgr.setIncludeDirs(Opts.IncludePaths);
283
Evan Chenge27eb592011-07-14 23:50:56 +0000284 OwningPtr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(Opts.Triple));
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000285 assert(MAI && "Unable to create target asm info!");
286
Evan Cheng884744b2011-07-18 20:57:51 +0000287 OwningPtr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(Opts.Triple));
288 assert(MRI && "Unable to create target register info!");
289
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000290 bool IsBinary = Opts.OutputType == AssemblerInvocation::FT_Obj;
291 formatted_raw_ostream *Out = GetOutputStream(Opts, Diags, IsBinary);
292 if (!Out)
293 return false;
294
Evan Cheng36fc3aa2011-07-20 06:22:27 +0000295 // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
296 // MCObjectFileInfo needs a MCContext reference in order to initialize itself.
297 OwningPtr<MCObjectFileInfo> MOFI(new MCObjectFileInfo());
Jim Grosbach6a9a6d22012-02-02 17:54:07 +0000298 MCContext Ctx(*MAI, *MRI, MOFI.get(), &SrcMgr);
Evan Cheng36fc3aa2011-07-20 06:22:27 +0000299 // FIXME: Assembler behavior can change with -static.
Evan Cheng66488ed2011-07-20 19:53:19 +0000300 MOFI->InitMCObjectFileInfo(Opts.Triple,
301 Reloc::Default, CodeModel::Default, Ctx);
Daniel Dunbar402adc32011-03-28 22:49:24 +0000302 if (Opts.SaveTemporaryLabels)
303 Ctx.setAllowTemporaryLabels(false);
Kevin Enderby567003e2011-12-22 19:31:58 +0000304 if (Opts.GenDwarfForAssembly)
305 Ctx.setGenDwarfForAssembly(true);
306 if (!Opts.DwarfDebugFlags.empty())
307 Ctx.setDwarfDebugFlags(StringRef(Opts.DwarfDebugFlags));
Rafael Espindola7872d482010-12-10 07:40:14 +0000308
Jim Grosbachfc308292012-02-10 20:37:10 +0000309 // Build up the feature string from the target feature list.
310 std::string FS;
311 if (!Opts.Features.empty()) {
312 FS = Opts.Features[0];
313 for (unsigned i = 1, e = Opts.Features.size(); i != e; ++i)
314 FS += "," + Opts.Features[i];
315 }
316
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000317 OwningPtr<MCStreamer> Str;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000318
Evan Chengccb21e42011-07-26 01:49:26 +0000319 OwningPtr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
320 OwningPtr<MCSubtargetInfo>
Jim Grosbachfc308292012-02-10 20:37:10 +0000321 STI(TheTarget->createMCSubtargetInfo(Opts.Triple, Opts.CPU, FS));
Evan Cheng74e13322011-07-11 04:24:19 +0000322
Rafael Espindola3176cca2011-01-23 17:58:26 +0000323 // FIXME: There is a bit of code duplication with addPassesToEmitFile.
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000324 if (Opts.OutputType == AssemblerInvocation::FT_Asm) {
325 MCInstPrinter *IP =
Bill Wendling99d43b42012-04-02 06:17:37 +0000326 TheTarget->createMCInstPrinter(Opts.OutputAsmVariant, *MAI, *MCII, *MRI,
327 *STI);
Benjamin Kramer7ac3d5a2010-07-29 17:48:03 +0000328 MCCodeEmitter *CE = 0;
Evan Cheng21118dc2011-07-25 23:25:09 +0000329 MCAsmBackend *MAB = 0;
Daniel Dunbar66cdf262010-12-16 03:06:05 +0000330 if (Opts.ShowEncoding) {
Jim Grosbach92ecfe92012-05-15 17:36:07 +0000331 CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, *STI, Ctx);
Roman Divackyf9361b52012-09-18 16:09:16 +0000332 MAB = TheTarget->createMCAsmBackend(Opts.Triple, Opts.CPU);
Daniel Dunbar66cdf262010-12-16 03:06:05 +0000333 }
Rafael Espindola7872d482010-12-10 07:40:14 +0000334 Str.reset(TheTarget->createAsmStreamer(Ctx, *Out, /*asmverbose*/true,
Rafael Espindola1bf39022011-04-30 03:46:18 +0000335 /*useLoc*/ true,
Nick Lewyckyea523d72011-10-17 23:05:52 +0000336 /*useCFI*/ true,
337 /*useDwarfDirectory*/ true,
338 IP, CE, MAB,
Rafael Espindola7872d482010-12-10 07:40:14 +0000339 Opts.ShowInst));
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000340 } else if (Opts.OutputType == AssemblerInvocation::FT_Null) {
341 Str.reset(createNullStreamer(Ctx));
342 } else {
343 assert(Opts.OutputType == AssemblerInvocation::FT_Obj &&
344 "Invalid file type!");
Jim Grosbach92ecfe92012-05-15 17:36:07 +0000345 MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, *STI, Ctx);
Roman Divackyf9361b52012-09-18 16:09:16 +0000346 MCAsmBackend *MAB = TheTarget->createMCAsmBackend(Opts.Triple, Opts.CPU);
Evan Cheng7b6def72011-07-26 00:42:40 +0000347 Str.reset(TheTarget->createMCObjectStreamer(Opts.Triple, Ctx, *MAB, *Out,
348 CE, Opts.RelaxAll,
349 Opts.NoExecStack));
Rafael Espindola4f036fa2010-10-13 14:53:57 +0000350 Str.get()->InitSections();
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000351 }
352
Jim Grosbach09190be2011-08-16 18:33:55 +0000353 OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr, Ctx,
Daniel Dunbar7374f1b2010-07-17 02:26:21 +0000354 *Str.get(), *MAI));
Evan Chengccb21e42011-07-26 01:49:26 +0000355 OwningPtr<MCTargetAsmParser> TAP(TheTarget->createMCAsmParser(*STI, *Parser));
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000356 if (!TAP) {
357 Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
358 return false;
359 }
360
Daniel Dunbar7374f1b2010-07-17 02:26:21 +0000361 Parser->setTargetParser(*TAP.get());
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000362
Daniel Dunbar7374f1b2010-07-17 02:26:21 +0000363 bool Success = !Parser->Run(Opts.NoInitialTextSection);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000364
365 // Close the output.
366 delete Out;
367
368 // Delete output on errors.
369 if (!Success && Opts.OutputPath != "-")
370 sys::Path(Opts.OutputPath).eraseFromDisk();
371
372 return Success;
373}
374
375static void LLVMErrorHandler(void *UserData, const std::string &Message) {
David Blaikied6471f72011-09-25 23:23:43 +0000376 DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000377
378 Diags.Report(diag::err_fe_error_backend) << Message;
379
380 // We cannot recover from llvm errors.
381 exit(1);
382}
383
384int cc1as_main(const char **ArgBegin, const char **ArgEnd,
385 const char *Argv0, void *MainAddr) {
386 // Print a stack trace if we signal out.
387 sys::PrintStackTraceOnErrorSignal();
388 PrettyStackTraceProgram X(ArgEnd - ArgBegin, ArgBegin);
389 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
390
391 // Initialize targets and assembly printers/parsers.
392 InitializeAllTargetInfos();
Evan Chengd99d3e12011-07-22 21:59:11 +0000393 InitializeAllTargetMCs();
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000394 InitializeAllAsmParsers();
395
396 // Construct our diagnostic client.
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000397 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
Douglas Gregorbdbb0042010-08-18 22:29:43 +0000398 TextDiagnosticPrinter *DiagClient
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000399 = new TextDiagnosticPrinter(errs(), &*DiagOpts);
Douglas Gregorbdbb0042010-08-18 22:29:43 +0000400 DiagClient->setPrefix("clang -cc1as");
Dylan Noblesmithc93dc782012-02-20 14:00:23 +0000401 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000402 DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagClient);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000403
404 // Set an error handler, so that any LLVM backend diagnostics go through our
405 // error handler.
Dan Gohman726578c2010-08-18 21:23:17 +0000406 ScopedFatalErrorHandler FatalErrorHandler
407 (LLVMErrorHandler, static_cast<void*>(&Diags));
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000408
409 // Parse the arguments.
410 AssemblerInvocation Asm;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000411 if (!AssemblerInvocation::CreateFromArgs(Asm, ArgBegin, ArgEnd, Diags))
412 return 1;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000413
Daniel Dunbarc673af72010-05-20 18:15:20 +0000414 // Honor -help.
415 if (Asm.ShowHelp) {
Dylan Noblesmith1e4c01b2012-02-13 12:32:21 +0000416 OwningPtr<driver::OptTable> Opts(driver::createCC1AsOptTable());
Daniel Dunbarc673af72010-05-20 18:15:20 +0000417 Opts->PrintHelp(llvm::outs(), "clang -cc1as", "Clang Integrated Assembler");
418 return 0;
419 }
420
421 // Honor -version.
422 //
423 // FIXME: Use a better -version message?
424 if (Asm.ShowVersion) {
425 llvm::cl::PrintVersionMessage();
426 return 0;
427 }
428
429 // Honor -mllvm.
430 //
431 // FIXME: Remove this, one day.
432 if (!Asm.LLVMArgs.empty()) {
433 unsigned NumArgs = Asm.LLVMArgs.size();
434 const char **Args = new const char*[NumArgs + 2];
435 Args[0] = "clang (LLVM option parsing)";
436 for (unsigned i = 0; i != NumArgs; ++i)
437 Args[i + 1] = Asm.LLVMArgs[i].c_str();
438 Args[NumArgs + 1] = 0;
David Blaikie6bd17d22012-02-07 19:36:38 +0000439 llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args);
Daniel Dunbarc673af72010-05-20 18:15:20 +0000440 }
441
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000442 // Execute the invocation, unless there were parsing errors.
443 bool Success = false;
Argyrios Kyrtzidise8f0ba72010-11-19 00:19:18 +0000444 if (!Diags.hasErrorOccurred())
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000445 Success = ExecuteAssembler(Asm, Diags);
446
447 // If any timers were active but haven't been destroyed yet, print their
448 // results now.
449 TimerGroup::printAll(errs());
450
451 return !Success;
452}