blob: 6f1c8afaa2fe23e4ec51c74a4bbe677c925f2f55 [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"
Chandler Carruthf59edb92012-12-04 09:25:21 +000016#include "clang/Basic/DiagnosticOptions.h"
Daniel Dunbar41b5b172010-05-20 17:49:16 +000017#include "clang/Driver/Arg.h"
18#include "clang/Driver/ArgList.h"
Daniel Dunbar41b5b172010-05-20 17:49:16 +000019#include "clang/Driver/CC1AsOptions.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000020#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar41b5b172010-05-20 17:49:16 +000021#include "clang/Driver/OptTable.h"
22#include "clang/Driver/Options.h"
Daniel Dunbar41b5b172010-05-20 17:49:16 +000023#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"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000028#include "llvm/IR/DataLayout.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000029#include "llvm/MC/MCAsmBackend.h"
Daniel Dunbar7374f1b2010-07-17 02:26:21 +000030#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbar41b5b172010-05-20 17:49:16 +000031#include "llvm/MC/MCCodeEmitter.h"
32#include "llvm/MC/MCContext.h"
Evan Cheng74e13322011-07-11 04:24:19 +000033#include "llvm/MC/MCInstrInfo.h"
Evan Cheng36fc3aa2011-07-20 06:22:27 +000034#include "llvm/MC/MCObjectFileInfo.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000035#include "llvm/MC/MCParser/MCAsmParser.h"
Evan Cheng884744b2011-07-18 20:57:51 +000036#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbar41b5b172010-05-20 17:49:16 +000037#include "llvm/MC/MCStreamer.h"
Evan Chengbb36ed92011-07-09 06:04:17 +000038#include "llvm/MC/MCSubtargetInfo.h"
Evan Cheng37712352011-07-26 00:24:45 +000039#include "llvm/MC/MCTargetAsmParser.h"
Daniel Dunbarc673af72010-05-20 18:15:20 +000040#include "llvm/Support/CommandLine.h"
Daniel Dunbar41b5b172010-05-20 17:49:16 +000041#include "llvm/Support/ErrorHandling.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000042#include "llvm/Support/FormattedStream.h"
43#include "llvm/Support/Host.h"
Daniel Dunbar41b5b172010-05-20 17:49:16 +000044#include "llvm/Support/ManagedStatic.h"
45#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000046#include "llvm/Support/Path.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000047#include "llvm/Support/PrettyStackTrace.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000048#include "llvm/Support/Signals.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000049#include "llvm/Support/SourceMgr.h"
Evan Chenga6b40452011-08-24 18:09:14 +000050#include "llvm/Support/TargetRegistry.h"
51#include "llvm/Support/TargetSelect.h"
52#include "llvm/Support/Timer.h"
53#include "llvm/Support/raw_ostream.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000054#include "llvm/Support/system_error.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;
Chandler Carruthd566df62012-12-17 21:40:04 +000086 std::string DebugCompilationDir;
Eric Christopher27e2b982012-12-18 00:31:10 +000087 std::string MainFileName;
Daniel Dunbar41b5b172010-05-20 17:49:16 +000088
89 /// @}
90 /// @name Frontend Options
91 /// @{
92
93 std::string InputFile;
Daniel Dunbarc673af72010-05-20 18:15:20 +000094 std::vector<std::string> LLVMArgs;
Daniel Dunbar41b5b172010-05-20 17:49:16 +000095 std::string OutputPath;
96 enum FileType {
97 FT_Asm, ///< Assembly (.s) output, transliterate mode.
98 FT_Null, ///< No output, for timing purposes.
99 FT_Obj ///< Object file output.
100 };
101 FileType OutputType;
Daniel Dunbarc673af72010-05-20 18:15:20 +0000102 unsigned ShowHelp : 1;
103 unsigned ShowVersion : 1;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000104
105 /// @}
106 /// @name Transliterate Options
107 /// @{
108
109 unsigned OutputAsmVariant;
110 unsigned ShowEncoding : 1;
111 unsigned ShowInst : 1;
112
113 /// @}
114 /// @name Assembler Options
115 /// @{
116
117 unsigned RelaxAll : 1;
Rafael Espindola3176cca2011-01-23 17:58:26 +0000118 unsigned NoExecStack : 1;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000119
120 /// @}
121
122public:
123 AssemblerInvocation() {
124 Triple = "";
125 NoInitialTextSection = 0;
126 InputFile = "-";
Daniel Dunbarc673af72010-05-20 18:15:20 +0000127 OutputPath = "-";
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000128 OutputType = FT_Asm;
129 OutputAsmVariant = 0;
130 ShowInst = 0;
131 ShowEncoding = 0;
132 RelaxAll = 0;
Rafael Espindola3176cca2011-01-23 17:58:26 +0000133 NoExecStack = 0;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000134 }
135
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000136 static bool CreateFromArgs(AssemblerInvocation &Res, const char **ArgBegin,
David Blaikied6471f72011-09-25 23:23:43 +0000137 const char **ArgEnd, DiagnosticsEngine &Diags);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000138};
139
140}
141
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000142bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000143 const char **ArgBegin,
144 const char **ArgEnd,
David Blaikied6471f72011-09-25 23:23:43 +0000145 DiagnosticsEngine &Diags) {
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000146 using namespace clang::driver::cc1asoptions;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000147 bool Success = true;
148
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000149 // Parse the arguments.
150 OwningPtr<OptTable> OptTbl(createCC1AsOptTable());
151 unsigned MissingArgIndex, MissingArgCount;
152 OwningPtr<InputArgList> Args(
153 OptTbl->ParseArgs(ArgBegin, ArgEnd,MissingArgIndex, MissingArgCount));
154
155 // Check for missing argument error.
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000156 if (MissingArgCount) {
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000157 Diags.Report(diag::err_drv_missing_argument)
158 << Args->getArgString(MissingArgIndex) << MissingArgCount;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000159 Success = false;
160 }
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000161
162 // Issue errors on unknown arguments.
163 for (arg_iterator it = Args->filtered_begin(cc1asoptions::OPT_UNKNOWN),
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000164 ie = Args->filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000165 Diags.Report(diag::err_drv_unknown_argument) << (*it) ->getAsString(*Args);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000166 Success = false;
167 }
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000168
169 // Construct the invocation.
170
171 // Target Options
Jim Grosbachfc308292012-02-10 20:37:10 +0000172 Opts.Triple = llvm::Triple::normalize(Args->getLastArgValue(OPT_triple));
173 Opts.CPU = Args->getLastArgValue(OPT_target_cpu);
174 Opts.Features = Args->getAllArgValues(OPT_target_feature);
175
176 // Use the default target triple if unspecified.
177 if (Opts.Triple.empty())
178 Opts.Triple = llvm::sys::getDefaultTargetTriple();
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000179
180 // Language Options
181 Opts.IncludePaths = Args->getAllArgValues(OPT_I);
182 Opts.NoInitialTextSection = Args->hasArg(OPT_n);
Daniel Dunbar402adc32011-03-28 22:49:24 +0000183 Opts.SaveTemporaryLabels = Args->hasArg(OPT_L);
Kevin Enderby567003e2011-12-22 19:31:58 +0000184 Opts.GenDwarfForAssembly = Args->hasArg(OPT_g);
185 Opts.DwarfDebugFlags = Args->getLastArgValue(OPT_dwarf_debug_flags);
Chandler Carruthd566df62012-12-17 21:40:04 +0000186 Opts.DebugCompilationDir = Args->getLastArgValue(OPT_fdebug_compilation_dir);
Eric Christopher27e2b982012-12-18 00:31:10 +0000187 Opts.MainFileName = Args->getLastArgValue(OPT_main_file_name);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000188
189 // Frontend Options
190 if (Args->hasArg(OPT_INPUT)) {
191 bool First = true;
192 for (arg_iterator it = Args->filtered_begin(OPT_INPUT),
193 ie = Args->filtered_end(); it != ie; ++it, First=false) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000194 const Arg *A = it;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000195 if (First)
Richard Smith1d489cf2012-11-01 04:30:05 +0000196 Opts.InputFile = A->getValue();
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000197 else {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000198 Diags.Report(diag::err_drv_unknown_argument) << A->getAsString(*Args);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000199 Success = false;
200 }
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000201 }
202 }
Daniel Dunbarc673af72010-05-20 18:15:20 +0000203 Opts.LLVMArgs = Args->getAllArgValues(OPT_mllvm);
Joerg Sonnenbergerd7933502011-05-19 18:42:29 +0000204 if (Args->hasArg(OPT_fatal_warnings))
205 Opts.LLVMArgs.push_back("-fatal-assembler-warnings");
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000206 Opts.OutputPath = Args->getLastArgValue(OPT_o);
207 if (Arg *A = Args->getLastArg(OPT_filetype)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000208 StringRef Name = A->getValue();
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000209 unsigned OutputType = StringSwitch<unsigned>(Name)
210 .Case("asm", FT_Asm)
211 .Case("null", FT_Null)
212 .Case("obj", FT_Obj)
213 .Default(~0U);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000214 if (OutputType == ~0U) {
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000215 Diags.Report(diag::err_drv_invalid_value)
216 << A->getAsString(*Args) << Name;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000217 Success = false;
218 } else
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000219 Opts.OutputType = FileType(OutputType);
220 }
Daniel Dunbarc673af72010-05-20 18:15:20 +0000221 Opts.ShowHelp = Args->hasArg(OPT_help);
222 Opts.ShowVersion = Args->hasArg(OPT_version);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000223
224 // Transliterate Options
225 Opts.OutputAsmVariant = Args->getLastArgIntValue(OPT_output_asm_variant,
226 0, Diags);
227 Opts.ShowEncoding = Args->hasArg(OPT_show_encoding);
228 Opts.ShowInst = Args->hasArg(OPT_show_inst);
229
230 // Assemble Options
231 Opts.RelaxAll = Args->hasArg(OPT_relax_all);
Rafael Espindola3176cca2011-01-23 17:58:26 +0000232 Opts.NoExecStack = Args->hasArg(OPT_no_exec_stack);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000233
Dylan Noblesmith89ea4162011-12-26 19:29:47 +0000234 return Success;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000235}
236
237static formatted_raw_ostream *GetOutputStream(AssemblerInvocation &Opts,
David Blaikied6471f72011-09-25 23:23:43 +0000238 DiagnosticsEngine &Diags,
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000239 bool Binary) {
Daniel Dunbarc673af72010-05-20 18:15:20 +0000240 if (Opts.OutputPath.empty())
241 Opts.OutputPath = "-";
242
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000243 // Make sure that the Out file gets unlinked from the disk if we get a
244 // SIGINT.
245 if (Opts.OutputPath != "-")
246 sys::RemoveFileOnSignal(sys::Path(Opts.OutputPath));
247
248 std::string Error;
249 raw_fd_ostream *Out =
250 new raw_fd_ostream(Opts.OutputPath.c_str(), Error,
251 (Binary ? raw_fd_ostream::F_Binary : 0));
252 if (!Error.empty()) {
253 Diags.Report(diag::err_fe_unable_to_open_output)
254 << Opts.OutputPath << Error;
255 return 0;
256 }
257
258 return new formatted_raw_ostream(*Out, formatted_raw_ostream::DELETE_STREAM);
259}
260
David Blaikied6471f72011-09-25 23:23:43 +0000261static bool ExecuteAssembler(AssemblerInvocation &Opts,
262 DiagnosticsEngine &Diags) {
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000263 // Get the target specific parser.
264 std::string Error;
265 const Target *TheTarget(TargetRegistry::lookupTarget(Opts.Triple, Error));
266 if (!TheTarget) {
267 Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
268 return false;
269 }
270
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000271 OwningPtr<MemoryBuffer> BufferPtr;
272 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Opts.InputFile, BufferPtr)) {
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000273 Error = ec.message();
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000274 Diags.Report(diag::err_fe_error_reading) << Opts.InputFile;
275 return false;
276 }
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000277 MemoryBuffer *Buffer = BufferPtr.take();
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000278
279 SourceMgr SrcMgr;
280
281 // Tell SrcMgr about this buffer, which is what the parser will pick up.
282 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
283
284 // Record the location of the include directories so that the lexer can find
285 // it later.
286 SrcMgr.setIncludeDirs(Opts.IncludePaths);
287
Evan Chenge27eb592011-07-14 23:50:56 +0000288 OwningPtr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(Opts.Triple));
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000289 assert(MAI && "Unable to create target asm info!");
290
Evan Cheng884744b2011-07-18 20:57:51 +0000291 OwningPtr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(Opts.Triple));
292 assert(MRI && "Unable to create target register info!");
293
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000294 bool IsBinary = Opts.OutputType == AssemblerInvocation::FT_Obj;
295 formatted_raw_ostream *Out = GetOutputStream(Opts, Diags, IsBinary);
296 if (!Out)
297 return false;
298
Evan Cheng36fc3aa2011-07-20 06:22:27 +0000299 // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
300 // MCObjectFileInfo needs a MCContext reference in order to initialize itself.
301 OwningPtr<MCObjectFileInfo> MOFI(new MCObjectFileInfo());
Jim Grosbach6a9a6d22012-02-02 17:54:07 +0000302 MCContext Ctx(*MAI, *MRI, MOFI.get(), &SrcMgr);
Evan Cheng36fc3aa2011-07-20 06:22:27 +0000303 // FIXME: Assembler behavior can change with -static.
Evan Cheng66488ed2011-07-20 19:53:19 +0000304 MOFI->InitMCObjectFileInfo(Opts.Triple,
305 Reloc::Default, CodeModel::Default, Ctx);
Daniel Dunbar402adc32011-03-28 22:49:24 +0000306 if (Opts.SaveTemporaryLabels)
307 Ctx.setAllowTemporaryLabels(false);
Kevin Enderby567003e2011-12-22 19:31:58 +0000308 if (Opts.GenDwarfForAssembly)
309 Ctx.setGenDwarfForAssembly(true);
310 if (!Opts.DwarfDebugFlags.empty())
311 Ctx.setDwarfDebugFlags(StringRef(Opts.DwarfDebugFlags));
Chandler Carruthd566df62012-12-17 21:40:04 +0000312 if (!Opts.DebugCompilationDir.empty())
313 Ctx.setCompilationDir(Opts.DebugCompilationDir);
Eric Christopher27e2b982012-12-18 00:31:10 +0000314 if (!Opts.MainFileName.empty())
315 Ctx.setMainFileName(StringRef(Opts.MainFileName));
Rafael Espindola7872d482010-12-10 07:40:14 +0000316
Jim Grosbachfc308292012-02-10 20:37:10 +0000317 // Build up the feature string from the target feature list.
318 std::string FS;
319 if (!Opts.Features.empty()) {
320 FS = Opts.Features[0];
321 for (unsigned i = 1, e = Opts.Features.size(); i != e; ++i)
322 FS += "," + Opts.Features[i];
323 }
324
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000325 OwningPtr<MCStreamer> Str;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000326
Evan Chengccb21e42011-07-26 01:49:26 +0000327 OwningPtr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
328 OwningPtr<MCSubtargetInfo>
Jim Grosbachfc308292012-02-10 20:37:10 +0000329 STI(TheTarget->createMCSubtargetInfo(Opts.Triple, Opts.CPU, FS));
Evan Cheng74e13322011-07-11 04:24:19 +0000330
Rafael Espindola3176cca2011-01-23 17:58:26 +0000331 // FIXME: There is a bit of code duplication with addPassesToEmitFile.
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000332 if (Opts.OutputType == AssemblerInvocation::FT_Asm) {
333 MCInstPrinter *IP =
Bill Wendling99d43b42012-04-02 06:17:37 +0000334 TheTarget->createMCInstPrinter(Opts.OutputAsmVariant, *MAI, *MCII, *MRI,
335 *STI);
Benjamin Kramer7ac3d5a2010-07-29 17:48:03 +0000336 MCCodeEmitter *CE = 0;
Evan Cheng21118dc2011-07-25 23:25:09 +0000337 MCAsmBackend *MAB = 0;
Daniel Dunbar66cdf262010-12-16 03:06:05 +0000338 if (Opts.ShowEncoding) {
Jim Grosbach92ecfe92012-05-15 17:36:07 +0000339 CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, *STI, Ctx);
Roman Divackyf9361b52012-09-18 16:09:16 +0000340 MAB = TheTarget->createMCAsmBackend(Opts.Triple, Opts.CPU);
Daniel Dunbar66cdf262010-12-16 03:06:05 +0000341 }
Rafael Espindola7872d482010-12-10 07:40:14 +0000342 Str.reset(TheTarget->createAsmStreamer(Ctx, *Out, /*asmverbose*/true,
Rafael Espindola1bf39022011-04-30 03:46:18 +0000343 /*useLoc*/ true,
Nick Lewyckyea523d72011-10-17 23:05:52 +0000344 /*useCFI*/ true,
345 /*useDwarfDirectory*/ true,
346 IP, CE, MAB,
Rafael Espindola7872d482010-12-10 07:40:14 +0000347 Opts.ShowInst));
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000348 } else if (Opts.OutputType == AssemblerInvocation::FT_Null) {
349 Str.reset(createNullStreamer(Ctx));
350 } else {
351 assert(Opts.OutputType == AssemblerInvocation::FT_Obj &&
352 "Invalid file type!");
Jim Grosbach92ecfe92012-05-15 17:36:07 +0000353 MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, *STI, Ctx);
Roman Divackyf9361b52012-09-18 16:09:16 +0000354 MCAsmBackend *MAB = TheTarget->createMCAsmBackend(Opts.Triple, Opts.CPU);
Evan Cheng7b6def72011-07-26 00:42:40 +0000355 Str.reset(TheTarget->createMCObjectStreamer(Opts.Triple, Ctx, *MAB, *Out,
356 CE, Opts.RelaxAll,
357 Opts.NoExecStack));
Rafael Espindola4f036fa2010-10-13 14:53:57 +0000358 Str.get()->InitSections();
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000359 }
360
Jim Grosbach09190be2011-08-16 18:33:55 +0000361 OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr, Ctx,
Daniel Dunbar7374f1b2010-07-17 02:26:21 +0000362 *Str.get(), *MAI));
Evan Chengccb21e42011-07-26 01:49:26 +0000363 OwningPtr<MCTargetAsmParser> TAP(TheTarget->createMCAsmParser(*STI, *Parser));
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000364 if (!TAP) {
365 Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
366 return false;
367 }
368
Daniel Dunbar7374f1b2010-07-17 02:26:21 +0000369 Parser->setTargetParser(*TAP.get());
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000370
Daniel Dunbar7374f1b2010-07-17 02:26:21 +0000371 bool Success = !Parser->Run(Opts.NoInitialTextSection);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000372
373 // Close the output.
374 delete Out;
375
376 // Delete output on errors.
377 if (!Success && Opts.OutputPath != "-")
378 sys::Path(Opts.OutputPath).eraseFromDisk();
379
380 return Success;
381}
382
383static void LLVMErrorHandler(void *UserData, const std::string &Message) {
David Blaikied6471f72011-09-25 23:23:43 +0000384 DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000385
386 Diags.Report(diag::err_fe_error_backend) << Message;
387
388 // We cannot recover from llvm errors.
389 exit(1);
390}
391
392int cc1as_main(const char **ArgBegin, const char **ArgEnd,
393 const char *Argv0, void *MainAddr) {
394 // Print a stack trace if we signal out.
395 sys::PrintStackTraceOnErrorSignal();
396 PrettyStackTraceProgram X(ArgEnd - ArgBegin, ArgBegin);
397 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
398
399 // Initialize targets and assembly printers/parsers.
400 InitializeAllTargetInfos();
Evan Chengd99d3e12011-07-22 21:59:11 +0000401 InitializeAllTargetMCs();
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000402 InitializeAllAsmParsers();
403
404 // Construct our diagnostic client.
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000405 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
Douglas Gregorbdbb0042010-08-18 22:29:43 +0000406 TextDiagnosticPrinter *DiagClient
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000407 = new TextDiagnosticPrinter(errs(), &*DiagOpts);
Douglas Gregorbdbb0042010-08-18 22:29:43 +0000408 DiagClient->setPrefix("clang -cc1as");
Dylan Noblesmithc93dc782012-02-20 14:00:23 +0000409 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000410 DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagClient);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000411
412 // Set an error handler, so that any LLVM backend diagnostics go through our
413 // error handler.
Dan Gohman726578c2010-08-18 21:23:17 +0000414 ScopedFatalErrorHandler FatalErrorHandler
415 (LLVMErrorHandler, static_cast<void*>(&Diags));
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000416
417 // Parse the arguments.
418 AssemblerInvocation Asm;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000419 if (!AssemblerInvocation::CreateFromArgs(Asm, ArgBegin, ArgEnd, Diags))
420 return 1;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000421
Daniel Dunbarc673af72010-05-20 18:15:20 +0000422 // Honor -help.
423 if (Asm.ShowHelp) {
Dylan Noblesmith1e4c01b2012-02-13 12:32:21 +0000424 OwningPtr<driver::OptTable> Opts(driver::createCC1AsOptTable());
Daniel Dunbarc673af72010-05-20 18:15:20 +0000425 Opts->PrintHelp(llvm::outs(), "clang -cc1as", "Clang Integrated Assembler");
426 return 0;
427 }
428
429 // Honor -version.
430 //
431 // FIXME: Use a better -version message?
432 if (Asm.ShowVersion) {
433 llvm::cl::PrintVersionMessage();
434 return 0;
435 }
436
437 // Honor -mllvm.
438 //
439 // FIXME: Remove this, one day.
440 if (!Asm.LLVMArgs.empty()) {
441 unsigned NumArgs = Asm.LLVMArgs.size();
442 const char **Args = new const char*[NumArgs + 2];
443 Args[0] = "clang (LLVM option parsing)";
444 for (unsigned i = 0; i != NumArgs; ++i)
445 Args[i + 1] = Asm.LLVMArgs[i].c_str();
446 Args[NumArgs + 1] = 0;
David Blaikie6bd17d22012-02-07 19:36:38 +0000447 llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args);
Daniel Dunbarc673af72010-05-20 18:15:20 +0000448 }
449
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000450 // Execute the invocation, unless there were parsing errors.
451 bool Success = false;
Argyrios Kyrtzidise8f0ba72010-11-19 00:19:18 +0000452 if (!Diags.hasErrorOccurred())
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000453 Success = ExecuteAssembler(Asm, Diags);
454
455 // If any timers were active but haven't been destroyed yet, print their
456 // results now.
457 TimerGroup::printAll(errs());
458
459 return !Success;
460}