blob: 08d61ac4de734944763e060164b3b39358ffc1bb [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/CC1AsOptions.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000018#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar41b5b172010-05-20 17:49:16 +000019#include "clang/Driver/Options.h"
Daniel Dunbar41b5b172010-05-20 17:49:16 +000020#include "clang/Frontend/FrontendDiagnostic.h"
21#include "clang/Frontend/TextDiagnosticPrinter.h"
Reid Klecknerb1e25a12013-06-14 17:17:23 +000022#include "clang/Frontend/Utils.h"
Daniel Dunbar41b5b172010-05-20 17:49:16 +000023#include "llvm/ADT/OwningPtr.h"
24#include "llvm/ADT/StringSwitch.h"
Duncan Sands2dc14532010-08-30 09:42:39 +000025#include "llvm/ADT/Triple.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000026#include "llvm/IR/DataLayout.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000027#include "llvm/MC/MCAsmBackend.h"
Daniel Dunbar7374f1b2010-07-17 02:26:21 +000028#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbar41b5b172010-05-20 17:49:16 +000029#include "llvm/MC/MCCodeEmitter.h"
30#include "llvm/MC/MCContext.h"
Evan Cheng74e13322011-07-11 04:24:19 +000031#include "llvm/MC/MCInstrInfo.h"
Evan Cheng36fc3aa2011-07-20 06:22:27 +000032#include "llvm/MC/MCObjectFileInfo.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000033#include "llvm/MC/MCParser/MCAsmParser.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 Cheng37712352011-07-26 00:24:45 +000037#include "llvm/MC/MCTargetAsmParser.h"
Reid Klecknerb1e25a12013-06-14 17:17:23 +000038#include "llvm/Option/Arg.h"
39#include "llvm/Option/ArgList.h"
40#include "llvm/Option/OptTable.h"
Daniel Dunbarc673af72010-05-20 18:15:20 +000041#include "llvm/Support/CommandLine.h"
Daniel Dunbar41b5b172010-05-20 17:49:16 +000042#include "llvm/Support/ErrorHandling.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000043#include "llvm/Support/FormattedStream.h"
44#include "llvm/Support/Host.h"
Daniel Dunbar41b5b172010-05-20 17:49:16 +000045#include "llvm/Support/ManagedStatic.h"
46#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000047#include "llvm/Support/Path.h"
Rafael Espindolaaa12d8f2013-06-13 21:09:29 +000048#include "llvm/Support/PathV1.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000049#include "llvm/Support/PrettyStackTrace.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000050#include "llvm/Support/Signals.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000051#include "llvm/Support/SourceMgr.h"
Evan Chenga6b40452011-08-24 18:09:14 +000052#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. Spencer3a321e22010-12-09 17:36:38 +000056#include "llvm/Support/system_error.h"
Daniel Dunbar41b5b172010-05-20 17:49:16 +000057using namespace clang;
58using namespace clang::driver;
59using namespace llvm;
Reid Klecknerb1e25a12013-06-14 17:17:23 +000060using namespace llvm::opt;
Daniel Dunbar41b5b172010-05-20 17:49:16 +000061
62namespace {
63
64/// \brief Helper class for representing a single invocation of the assembler.
65struct AssemblerInvocation {
66 /// @name Target Options
67 /// @{
68
Jim Grosbachfc308292012-02-10 20:37:10 +000069 /// The name of the target triple to assemble for.
Daniel Dunbar41b5b172010-05-20 17:49:16 +000070 std::string Triple;
71
Jim Grosbachfc308292012-02-10 20:37:10 +000072 /// If given, the name of the target CPU to determine which instructions
73 /// are legal.
74 std::string CPU;
75
76 /// The list of target specific features to enable or disable -- this should
77 /// be a list of strings starting with '+' or '-'.
78 std::vector<std::string> Features;
79
Daniel Dunbar41b5b172010-05-20 17:49:16 +000080 /// @}
81 /// @name Language Options
82 /// @{
83
84 std::vector<std::string> IncludePaths;
85 unsigned NoInitialTextSection : 1;
Daniel Dunbar402adc32011-03-28 22:49:24 +000086 unsigned SaveTemporaryLabels : 1;
Kevin Enderby567003e2011-12-22 19:31:58 +000087 unsigned GenDwarfForAssembly : 1;
88 std::string DwarfDebugFlags;
Kevin Enderby02341792013-01-17 21:38:06 +000089 std::string DwarfDebugProducer;
Chandler Carruthd566df62012-12-17 21:40:04 +000090 std::string DebugCompilationDir;
Eric Christopher27e2b982012-12-18 00:31:10 +000091 std::string MainFileName;
Daniel Dunbar41b5b172010-05-20 17:49:16 +000092
93 /// @}
94 /// @name Frontend Options
95 /// @{
96
97 std::string InputFile;
Daniel Dunbarc673af72010-05-20 18:15:20 +000098 std::vector<std::string> LLVMArgs;
Daniel Dunbar41b5b172010-05-20 17:49:16 +000099 std::string OutputPath;
100 enum FileType {
101 FT_Asm, ///< Assembly (.s) output, transliterate mode.
102 FT_Null, ///< No output, for timing purposes.
103 FT_Obj ///< Object file output.
104 };
105 FileType OutputType;
Daniel Dunbarc673af72010-05-20 18:15:20 +0000106 unsigned ShowHelp : 1;
107 unsigned ShowVersion : 1;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000108
109 /// @}
110 /// @name Transliterate Options
111 /// @{
112
113 unsigned OutputAsmVariant;
114 unsigned ShowEncoding : 1;
115 unsigned ShowInst : 1;
116
117 /// @}
118 /// @name Assembler Options
119 /// @{
120
121 unsigned RelaxAll : 1;
Rafael Espindola3176cca2011-01-23 17:58:26 +0000122 unsigned NoExecStack : 1;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000123
124 /// @}
125
126public:
127 AssemblerInvocation() {
128 Triple = "";
129 NoInitialTextSection = 0;
130 InputFile = "-";
Daniel Dunbarc673af72010-05-20 18:15:20 +0000131 OutputPath = "-";
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000132 OutputType = FT_Asm;
133 OutputAsmVariant = 0;
134 ShowInst = 0;
135 ShowEncoding = 0;
136 RelaxAll = 0;
Rafael Espindola3176cca2011-01-23 17:58:26 +0000137 NoExecStack = 0;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000138 }
139
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000140 static bool CreateFromArgs(AssemblerInvocation &Res, const char **ArgBegin,
David Blaikied6471f72011-09-25 23:23:43 +0000141 const char **ArgEnd, DiagnosticsEngine &Diags);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000142};
143
144}
145
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000146bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000147 const char **ArgBegin,
148 const char **ArgEnd,
David Blaikied6471f72011-09-25 23:23:43 +0000149 DiagnosticsEngine &Diags) {
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000150 using namespace clang::driver::cc1asoptions;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000151 bool Success = true;
152
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000153 // Parse the arguments.
154 OwningPtr<OptTable> OptTbl(createCC1AsOptTable());
155 unsigned MissingArgIndex, MissingArgCount;
156 OwningPtr<InputArgList> Args(
157 OptTbl->ParseArgs(ArgBegin, ArgEnd,MissingArgIndex, MissingArgCount));
158
159 // Check for missing argument error.
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000160 if (MissingArgCount) {
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000161 Diags.Report(diag::err_drv_missing_argument)
162 << Args->getArgString(MissingArgIndex) << MissingArgCount;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000163 Success = false;
164 }
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000165
166 // Issue errors on unknown arguments.
167 for (arg_iterator it = Args->filtered_begin(cc1asoptions::OPT_UNKNOWN),
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000168 ie = Args->filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000169 Diags.Report(diag::err_drv_unknown_argument) << (*it) ->getAsString(*Args);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000170 Success = false;
171 }
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000172
173 // Construct the invocation.
174
175 // Target Options
Jim Grosbachfc308292012-02-10 20:37:10 +0000176 Opts.Triple = llvm::Triple::normalize(Args->getLastArgValue(OPT_triple));
177 Opts.CPU = Args->getLastArgValue(OPT_target_cpu);
178 Opts.Features = Args->getAllArgValues(OPT_target_feature);
179
180 // Use the default target triple if unspecified.
181 if (Opts.Triple.empty())
182 Opts.Triple = llvm::sys::getDefaultTargetTriple();
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000183
184 // Language Options
185 Opts.IncludePaths = Args->getAllArgValues(OPT_I);
186 Opts.NoInitialTextSection = Args->hasArg(OPT_n);
Daniel Dunbar402adc32011-03-28 22:49:24 +0000187 Opts.SaveTemporaryLabels = Args->hasArg(OPT_L);
Kevin Enderby567003e2011-12-22 19:31:58 +0000188 Opts.GenDwarfForAssembly = Args->hasArg(OPT_g);
189 Opts.DwarfDebugFlags = Args->getLastArgValue(OPT_dwarf_debug_flags);
Kevin Enderby02341792013-01-17 21:38:06 +0000190 Opts.DwarfDebugProducer = Args->getLastArgValue(OPT_dwarf_debug_producer);
Chandler Carruthd566df62012-12-17 21:40:04 +0000191 Opts.DebugCompilationDir = Args->getLastArgValue(OPT_fdebug_compilation_dir);
Eric Christopher27e2b982012-12-18 00:31:10 +0000192 Opts.MainFileName = Args->getLastArgValue(OPT_main_file_name);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000193
194 // Frontend Options
195 if (Args->hasArg(OPT_INPUT)) {
196 bool First = true;
197 for (arg_iterator it = Args->filtered_begin(OPT_INPUT),
198 ie = Args->filtered_end(); it != ie; ++it, First=false) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000199 const Arg *A = it;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000200 if (First)
Richard Smith1d489cf2012-11-01 04:30:05 +0000201 Opts.InputFile = A->getValue();
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000202 else {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000203 Diags.Report(diag::err_drv_unknown_argument) << A->getAsString(*Args);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000204 Success = false;
205 }
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000206 }
207 }
Daniel Dunbarc673af72010-05-20 18:15:20 +0000208 Opts.LLVMArgs = Args->getAllArgValues(OPT_mllvm);
Joerg Sonnenbergerd7933502011-05-19 18:42:29 +0000209 if (Args->hasArg(OPT_fatal_warnings))
210 Opts.LLVMArgs.push_back("-fatal-assembler-warnings");
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000211 Opts.OutputPath = Args->getLastArgValue(OPT_o);
212 if (Arg *A = Args->getLastArg(OPT_filetype)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000213 StringRef Name = A->getValue();
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000214 unsigned OutputType = StringSwitch<unsigned>(Name)
215 .Case("asm", FT_Asm)
216 .Case("null", FT_Null)
217 .Case("obj", FT_Obj)
218 .Default(~0U);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000219 if (OutputType == ~0U) {
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000220 Diags.Report(diag::err_drv_invalid_value)
221 << A->getAsString(*Args) << Name;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000222 Success = false;
223 } else
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000224 Opts.OutputType = FileType(OutputType);
225 }
Daniel Dunbarc673af72010-05-20 18:15:20 +0000226 Opts.ShowHelp = Args->hasArg(OPT_help);
227 Opts.ShowVersion = Args->hasArg(OPT_version);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000228
229 // Transliterate Options
Reid Klecknerb1e25a12013-06-14 17:17:23 +0000230 Opts.OutputAsmVariant =
231 getLastArgIntValue(*Args.get(), OPT_output_asm_variant, 0, Diags);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000232 Opts.ShowEncoding = Args->hasArg(OPT_show_encoding);
233 Opts.ShowInst = Args->hasArg(OPT_show_inst);
234
235 // Assemble Options
236 Opts.RelaxAll = Args->hasArg(OPT_relax_all);
Rafael Espindola3176cca2011-01-23 17:58:26 +0000237 Opts.NoExecStack = Args->hasArg(OPT_no_exec_stack);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000238
Dylan Noblesmith89ea4162011-12-26 19:29:47 +0000239 return Success;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000240}
241
242static formatted_raw_ostream *GetOutputStream(AssemblerInvocation &Opts,
David Blaikied6471f72011-09-25 23:23:43 +0000243 DiagnosticsEngine &Diags,
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000244 bool Binary) {
Daniel Dunbarc673af72010-05-20 18:15:20 +0000245 if (Opts.OutputPath.empty())
246 Opts.OutputPath = "-";
247
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000248 // Make sure that the Out file gets unlinked from the disk if we get a
249 // SIGINT.
250 if (Opts.OutputPath != "-")
Rafael Espindola6f2e23b2013-06-13 21:02:40 +0000251 sys::RemoveFileOnSignal(Opts.OutputPath);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000252
253 std::string Error;
254 raw_fd_ostream *Out =
255 new raw_fd_ostream(Opts.OutputPath.c_str(), Error,
256 (Binary ? raw_fd_ostream::F_Binary : 0));
257 if (!Error.empty()) {
258 Diags.Report(diag::err_fe_unable_to_open_output)
259 << Opts.OutputPath << Error;
260 return 0;
261 }
262
263 return new formatted_raw_ostream(*Out, formatted_raw_ostream::DELETE_STREAM);
264}
265
David Blaikied6471f72011-09-25 23:23:43 +0000266static bool ExecuteAssembler(AssemblerInvocation &Opts,
267 DiagnosticsEngine &Diags) {
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000268 // Get the target specific parser.
269 std::string Error;
270 const Target *TheTarget(TargetRegistry::lookupTarget(Opts.Triple, Error));
271 if (!TheTarget) {
272 Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
273 return false;
274 }
275
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000276 OwningPtr<MemoryBuffer> BufferPtr;
277 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Opts.InputFile, BufferPtr)) {
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000278 Error = ec.message();
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000279 Diags.Report(diag::err_fe_error_reading) << Opts.InputFile;
280 return false;
281 }
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000282 MemoryBuffer *Buffer = BufferPtr.take();
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000283
284 SourceMgr SrcMgr;
285
286 // Tell SrcMgr about this buffer, which is what the parser will pick up.
287 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
288
289 // Record the location of the include directories so that the lexer can find
290 // it later.
291 SrcMgr.setIncludeDirs(Opts.IncludePaths);
292
Evan Cheng884744b2011-07-18 20:57:51 +0000293 OwningPtr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(Opts.Triple));
294 assert(MRI && "Unable to create target register info!");
295
Rafael Espindola1fcf31e2013-05-13 01:24:18 +0000296 OwningPtr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, Opts.Triple));
297 assert(MAI && "Unable to create target asm info!");
298
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000299 bool IsBinary = Opts.OutputType == AssemblerInvocation::FT_Obj;
300 formatted_raw_ostream *Out = GetOutputStream(Opts, Diags, IsBinary);
301 if (!Out)
302 return false;
303
Evan Cheng36fc3aa2011-07-20 06:22:27 +0000304 // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
305 // MCObjectFileInfo needs a MCContext reference in order to initialize itself.
306 OwningPtr<MCObjectFileInfo> MOFI(new MCObjectFileInfo());
Bill Wendling4b7bae32013-06-18 07:22:05 +0000307 MCContext Ctx(MAI.get(), MRI.get(), MOFI.get(), &SrcMgr);
Evan Cheng36fc3aa2011-07-20 06:22:27 +0000308 // FIXME: Assembler behavior can change with -static.
Evan Cheng66488ed2011-07-20 19:53:19 +0000309 MOFI->InitMCObjectFileInfo(Opts.Triple,
310 Reloc::Default, CodeModel::Default, Ctx);
Daniel Dunbar402adc32011-03-28 22:49:24 +0000311 if (Opts.SaveTemporaryLabels)
312 Ctx.setAllowTemporaryLabels(false);
Kevin Enderby567003e2011-12-22 19:31:58 +0000313 if (Opts.GenDwarfForAssembly)
314 Ctx.setGenDwarfForAssembly(true);
315 if (!Opts.DwarfDebugFlags.empty())
316 Ctx.setDwarfDebugFlags(StringRef(Opts.DwarfDebugFlags));
Kevin Enderby02341792013-01-17 21:38:06 +0000317 if (!Opts.DwarfDebugProducer.empty())
318 Ctx.setDwarfDebugProducer(StringRef(Opts.DwarfDebugProducer));
Chandler Carruthd566df62012-12-17 21:40:04 +0000319 if (!Opts.DebugCompilationDir.empty())
320 Ctx.setCompilationDir(Opts.DebugCompilationDir);
Eric Christopher27e2b982012-12-18 00:31:10 +0000321 if (!Opts.MainFileName.empty())
322 Ctx.setMainFileName(StringRef(Opts.MainFileName));
Rafael Espindola7872d482010-12-10 07:40:14 +0000323
Jim Grosbachfc308292012-02-10 20:37:10 +0000324 // Build up the feature string from the target feature list.
325 std::string FS;
326 if (!Opts.Features.empty()) {
327 FS = Opts.Features[0];
328 for (unsigned i = 1, e = Opts.Features.size(); i != e; ++i)
329 FS += "," + Opts.Features[i];
330 }
331
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000332 OwningPtr<MCStreamer> Str;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000333
Evan Chengccb21e42011-07-26 01:49:26 +0000334 OwningPtr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
335 OwningPtr<MCSubtargetInfo>
Jim Grosbachfc308292012-02-10 20:37:10 +0000336 STI(TheTarget->createMCSubtargetInfo(Opts.Triple, Opts.CPU, FS));
Evan Cheng74e13322011-07-11 04:24:19 +0000337
Rafael Espindola3176cca2011-01-23 17:58:26 +0000338 // FIXME: There is a bit of code duplication with addPassesToEmitFile.
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000339 if (Opts.OutputType == AssemblerInvocation::FT_Asm) {
340 MCInstPrinter *IP =
Bill Wendling99d43b42012-04-02 06:17:37 +0000341 TheTarget->createMCInstPrinter(Opts.OutputAsmVariant, *MAI, *MCII, *MRI,
342 *STI);
Benjamin Kramer7ac3d5a2010-07-29 17:48:03 +0000343 MCCodeEmitter *CE = 0;
Evan Cheng21118dc2011-07-25 23:25:09 +0000344 MCAsmBackend *MAB = 0;
Daniel Dunbar66cdf262010-12-16 03:06:05 +0000345 if (Opts.ShowEncoding) {
Jim Grosbach92ecfe92012-05-15 17:36:07 +0000346 CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, *STI, Ctx);
Roman Divackyf9361b52012-09-18 16:09:16 +0000347 MAB = TheTarget->createMCAsmBackend(Opts.Triple, Opts.CPU);
Daniel Dunbar66cdf262010-12-16 03:06:05 +0000348 }
Rafael Espindola7872d482010-12-10 07:40:14 +0000349 Str.reset(TheTarget->createAsmStreamer(Ctx, *Out, /*asmverbose*/true,
Rafael Espindola1bf39022011-04-30 03:46:18 +0000350 /*useLoc*/ true,
Nick Lewyckyea523d72011-10-17 23:05:52 +0000351 /*useCFI*/ true,
352 /*useDwarfDirectory*/ true,
353 IP, CE, MAB,
Rafael Espindola7872d482010-12-10 07:40:14 +0000354 Opts.ShowInst));
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000355 } else if (Opts.OutputType == AssemblerInvocation::FT_Null) {
356 Str.reset(createNullStreamer(Ctx));
357 } else {
358 assert(Opts.OutputType == AssemblerInvocation::FT_Obj &&
359 "Invalid file type!");
Jim Grosbach92ecfe92012-05-15 17:36:07 +0000360 MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, *STI, Ctx);
Roman Divackyf9361b52012-09-18 16:09:16 +0000361 MCAsmBackend *MAB = TheTarget->createMCAsmBackend(Opts.Triple, Opts.CPU);
Evan Cheng7b6def72011-07-26 00:42:40 +0000362 Str.reset(TheTarget->createMCObjectStreamer(Opts.Triple, Ctx, *MAB, *Out,
363 CE, Opts.RelaxAll,
364 Opts.NoExecStack));
Rafael Espindola4f036fa2010-10-13 14:53:57 +0000365 Str.get()->InitSections();
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000366 }
367
Jim Grosbach09190be2011-08-16 18:33:55 +0000368 OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr, Ctx,
Daniel Dunbar7374f1b2010-07-17 02:26:21 +0000369 *Str.get(), *MAI));
Evan Chengccb21e42011-07-26 01:49:26 +0000370 OwningPtr<MCTargetAsmParser> TAP(TheTarget->createMCAsmParser(*STI, *Parser));
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000371 if (!TAP) {
372 Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
373 return false;
374 }
375
Daniel Dunbar7374f1b2010-07-17 02:26:21 +0000376 Parser->setTargetParser(*TAP.get());
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000377
Daniel Dunbar7374f1b2010-07-17 02:26:21 +0000378 bool Success = !Parser->Run(Opts.NoInitialTextSection);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000379
380 // Close the output.
381 delete Out;
382
383 // Delete output on errors.
384 if (!Success && Opts.OutputPath != "-")
385 sys::Path(Opts.OutputPath).eraseFromDisk();
386
387 return Success;
388}
389
Chad Rosier90836282013-03-27 18:28:23 +0000390static void LLVMErrorHandler(void *UserData, const std::string &Message,
391 bool GenCrashDiag) {
David Blaikied6471f72011-09-25 23:23:43 +0000392 DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000393
394 Diags.Report(diag::err_fe_error_backend) << Message;
395
396 // We cannot recover from llvm errors.
397 exit(1);
398}
399
400int cc1as_main(const char **ArgBegin, const char **ArgEnd,
401 const char *Argv0, void *MainAddr) {
402 // Print a stack trace if we signal out.
403 sys::PrintStackTraceOnErrorSignal();
404 PrettyStackTraceProgram X(ArgEnd - ArgBegin, ArgBegin);
405 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
406
407 // Initialize targets and assembly printers/parsers.
408 InitializeAllTargetInfos();
Evan Chengd99d3e12011-07-22 21:59:11 +0000409 InitializeAllTargetMCs();
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000410 InitializeAllAsmParsers();
411
412 // Construct our diagnostic client.
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000413 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
Douglas Gregorbdbb0042010-08-18 22:29:43 +0000414 TextDiagnosticPrinter *DiagClient
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000415 = new TextDiagnosticPrinter(errs(), &*DiagOpts);
Douglas Gregorbdbb0042010-08-18 22:29:43 +0000416 DiagClient->setPrefix("clang -cc1as");
Dylan Noblesmithc93dc782012-02-20 14:00:23 +0000417 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000418 DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagClient);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000419
420 // Set an error handler, so that any LLVM backend diagnostics go through our
421 // error handler.
Dan Gohman726578c2010-08-18 21:23:17 +0000422 ScopedFatalErrorHandler FatalErrorHandler
423 (LLVMErrorHandler, static_cast<void*>(&Diags));
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000424
425 // Parse the arguments.
426 AssemblerInvocation Asm;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000427 if (!AssemblerInvocation::CreateFromArgs(Asm, ArgBegin, ArgEnd, Diags))
428 return 1;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000429
Daniel Dunbarc673af72010-05-20 18:15:20 +0000430 // Honor -help.
431 if (Asm.ShowHelp) {
Reid Klecknerb1e25a12013-06-14 17:17:23 +0000432 OwningPtr<OptTable> Opts(driver::createCC1AsOptTable());
Daniel Dunbarc673af72010-05-20 18:15:20 +0000433 Opts->PrintHelp(llvm::outs(), "clang -cc1as", "Clang Integrated Assembler");
434 return 0;
435 }
436
437 // Honor -version.
438 //
439 // FIXME: Use a better -version message?
440 if (Asm.ShowVersion) {
441 llvm::cl::PrintVersionMessage();
442 return 0;
443 }
444
445 // Honor -mllvm.
446 //
447 // FIXME: Remove this, one day.
448 if (!Asm.LLVMArgs.empty()) {
449 unsigned NumArgs = Asm.LLVMArgs.size();
450 const char **Args = new const char*[NumArgs + 2];
451 Args[0] = "clang (LLVM option parsing)";
452 for (unsigned i = 0; i != NumArgs; ++i)
453 Args[i + 1] = Asm.LLVMArgs[i].c_str();
454 Args[NumArgs + 1] = 0;
David Blaikie6bd17d22012-02-07 19:36:38 +0000455 llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args);
Daniel Dunbarc673af72010-05-20 18:15:20 +0000456 }
457
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000458 // Execute the invocation, unless there were parsing errors.
459 bool Success = false;
Argyrios Kyrtzidise8f0ba72010-11-19 00:19:18 +0000460 if (!Diags.hasErrorOccurred())
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000461 Success = ExecuteAssembler(Asm, Diags);
462
463 // If any timers were active but haven't been destroyed yet, print their
464 // results now.
465 TimerGroup::printAll(errs());
466
467 return !Success;
468}