blob: c4456e2f1df70f3f8d4a74471d2add59109e827e [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/StringSwitch.h"
Duncan Sands2dc14532010-08-30 09:42:39 +000024#include "llvm/ADT/Triple.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000025#include "llvm/IR/DataLayout.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000026#include "llvm/MC/MCAsmBackend.h"
Daniel Dunbar7374f1b2010-07-17 02:26:21 +000027#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbar41b5b172010-05-20 17:49:16 +000028#include "llvm/MC/MCCodeEmitter.h"
29#include "llvm/MC/MCContext.h"
Evan Cheng74e13322011-07-11 04:24:19 +000030#include "llvm/MC/MCInstrInfo.h"
Evan Cheng36fc3aa2011-07-20 06:22:27 +000031#include "llvm/MC/MCObjectFileInfo.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000032#include "llvm/MC/MCParser/MCAsmParser.h"
Evan Cheng884744b2011-07-18 20:57:51 +000033#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbar41b5b172010-05-20 17:49:16 +000034#include "llvm/MC/MCStreamer.h"
Evan Chengbb36ed92011-07-09 06:04:17 +000035#include "llvm/MC/MCSubtargetInfo.h"
Evan Cheng37712352011-07-26 00:24:45 +000036#include "llvm/MC/MCTargetAsmParser.h"
Stephen Hines6bcf27b2014-05-29 04:14:42 -070037#include "llvm/MC/MCTargetOptions.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"
Rafael Espindola2669e962013-06-26 12:48:34 +000043#include "llvm/Support/FileSystem.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000044#include "llvm/Support/FormattedStream.h"
45#include "llvm/Support/Host.h"
Daniel Dunbar41b5b172010-05-20 17:49:16 +000046#include "llvm/Support/ManagedStatic.h"
47#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000048#include "llvm/Support/Path.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"
Stephen Hines651f13c2014-04-23 16:59:28 -070057#include <memory>
Daniel Dunbar41b5b172010-05-20 17:49:16 +000058using namespace clang;
59using namespace clang::driver;
60using namespace llvm;
Reid Klecknerb1e25a12013-06-14 17:17:23 +000061using namespace llvm::opt;
Daniel Dunbar41b5b172010-05-20 17:49:16 +000062
63namespace {
64
65/// \brief Helper class for representing a single invocation of the assembler.
66struct AssemblerInvocation {
67 /// @name Target Options
68 /// @{
69
Jim Grosbachfc308292012-02-10 20:37:10 +000070 /// The name of the target triple to assemble for.
Daniel Dunbar41b5b172010-05-20 17:49:16 +000071 std::string Triple;
72
Jim Grosbachfc308292012-02-10 20:37:10 +000073 /// If given, the name of the target CPU to determine which instructions
74 /// are legal.
75 std::string CPU;
76
77 /// The list of target specific features to enable or disable -- this should
78 /// be a list of strings starting with '+' or '-'.
79 std::vector<std::string> Features;
80
Daniel Dunbar41b5b172010-05-20 17:49:16 +000081 /// @}
82 /// @name Language Options
83 /// @{
84
85 std::vector<std::string> IncludePaths;
86 unsigned NoInitialTextSection : 1;
Daniel Dunbar402adc32011-03-28 22:49:24 +000087 unsigned SaveTemporaryLabels : 1;
Kevin Enderby567003e2011-12-22 19:31:58 +000088 unsigned GenDwarfForAssembly : 1;
Stephen Hines651f13c2014-04-23 16:59:28 -070089 unsigned CompressDebugSections : 1;
Stephen Hines6bcf27b2014-05-29 04:14:42 -070090 unsigned DwarfVersion;
Kevin Enderby567003e2011-12-22 19:31:58 +000091 std::string DwarfDebugFlags;
Kevin Enderby02341792013-01-17 21:38:06 +000092 std::string DwarfDebugProducer;
Chandler Carruthd566df62012-12-17 21:40:04 +000093 std::string DebugCompilationDir;
Eric Christopher27e2b982012-12-18 00:31:10 +000094 std::string MainFileName;
Daniel Dunbar41b5b172010-05-20 17:49:16 +000095
96 /// @}
97 /// @name Frontend Options
98 /// @{
99
100 std::string InputFile;
Daniel Dunbarc673af72010-05-20 18:15:20 +0000101 std::vector<std::string> LLVMArgs;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000102 std::string OutputPath;
103 enum FileType {
104 FT_Asm, ///< Assembly (.s) output, transliterate mode.
105 FT_Null, ///< No output, for timing purposes.
106 FT_Obj ///< Object file output.
107 };
108 FileType OutputType;
Daniel Dunbarc673af72010-05-20 18:15:20 +0000109 unsigned ShowHelp : 1;
110 unsigned ShowVersion : 1;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000111
112 /// @}
113 /// @name Transliterate Options
114 /// @{
115
116 unsigned OutputAsmVariant;
117 unsigned ShowEncoding : 1;
118 unsigned ShowInst : 1;
119
120 /// @}
121 /// @name Assembler Options
122 /// @{
123
124 unsigned RelaxAll : 1;
Rafael Espindola3176cca2011-01-23 17:58:26 +0000125 unsigned NoExecStack : 1;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000126
127 /// @}
128
129public:
130 AssemblerInvocation() {
131 Triple = "";
132 NoInitialTextSection = 0;
133 InputFile = "-";
Daniel Dunbarc673af72010-05-20 18:15:20 +0000134 OutputPath = "-";
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000135 OutputType = FT_Asm;
136 OutputAsmVariant = 0;
137 ShowInst = 0;
138 ShowEncoding = 0;
139 RelaxAll = 0;
Rafael Espindola3176cca2011-01-23 17:58:26 +0000140 NoExecStack = 0;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700141 DwarfVersion = 3;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000142 }
143
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000144 static bool CreateFromArgs(AssemblerInvocation &Res, const char **ArgBegin,
David Blaikied6471f72011-09-25 23:23:43 +0000145 const char **ArgEnd, DiagnosticsEngine &Diags);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000146};
147
148}
149
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000150bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000151 const char **ArgBegin,
152 const char **ArgEnd,
David Blaikied6471f72011-09-25 23:23:43 +0000153 DiagnosticsEngine &Diags) {
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000154 using namespace clang::driver::cc1asoptions;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000155 bool Success = true;
156
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000157 // Parse the arguments.
Stephen Hines651f13c2014-04-23 16:59:28 -0700158 std::unique_ptr<OptTable> OptTbl(createCC1AsOptTable());
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000159 unsigned MissingArgIndex, MissingArgCount;
Stephen Hines651f13c2014-04-23 16:59:28 -0700160 std::unique_ptr<InputArgList> Args(
161 OptTbl->ParseArgs(ArgBegin, ArgEnd, MissingArgIndex, MissingArgCount));
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000162
163 // Check for missing argument error.
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000164 if (MissingArgCount) {
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000165 Diags.Report(diag::err_drv_missing_argument)
166 << Args->getArgString(MissingArgIndex) << MissingArgCount;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000167 Success = false;
168 }
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000169
170 // Issue errors on unknown arguments.
171 for (arg_iterator it = Args->filtered_begin(cc1asoptions::OPT_UNKNOWN),
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000172 ie = Args->filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000173 Diags.Report(diag::err_drv_unknown_argument) << (*it) ->getAsString(*Args);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000174 Success = false;
175 }
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000176
177 // Construct the invocation.
178
179 // Target Options
Jim Grosbachfc308292012-02-10 20:37:10 +0000180 Opts.Triple = llvm::Triple::normalize(Args->getLastArgValue(OPT_triple));
181 Opts.CPU = Args->getLastArgValue(OPT_target_cpu);
182 Opts.Features = Args->getAllArgValues(OPT_target_feature);
183
184 // Use the default target triple if unspecified.
185 if (Opts.Triple.empty())
186 Opts.Triple = llvm::sys::getDefaultTargetTriple();
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000187
188 // Language Options
189 Opts.IncludePaths = Args->getAllArgValues(OPT_I);
190 Opts.NoInitialTextSection = Args->hasArg(OPT_n);
Bob Wilson084be2d2013-09-26 21:00:51 +0000191 Opts.SaveTemporaryLabels = Args->hasArg(OPT_msave_temp_labels);
Kevin Enderby567003e2011-12-22 19:31:58 +0000192 Opts.GenDwarfForAssembly = Args->hasArg(OPT_g);
Stephen Hines651f13c2014-04-23 16:59:28 -0700193 Opts.CompressDebugSections = Args->hasArg(OPT_compress_debug_sections);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700194 if (Args->hasArg(OPT_gdwarf_2))
195 Opts.DwarfVersion = 2;
196 if (Args->hasArg(OPT_gdwarf_3))
197 Opts.DwarfVersion = 3;
198 if (Args->hasArg(OPT_gdwarf_4))
199 Opts.DwarfVersion = 4;
Kevin Enderby567003e2011-12-22 19:31:58 +0000200 Opts.DwarfDebugFlags = Args->getLastArgValue(OPT_dwarf_debug_flags);
Kevin Enderby02341792013-01-17 21:38:06 +0000201 Opts.DwarfDebugProducer = Args->getLastArgValue(OPT_dwarf_debug_producer);
Chandler Carruthd566df62012-12-17 21:40:04 +0000202 Opts.DebugCompilationDir = Args->getLastArgValue(OPT_fdebug_compilation_dir);
Eric Christopher27e2b982012-12-18 00:31:10 +0000203 Opts.MainFileName = Args->getLastArgValue(OPT_main_file_name);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000204
205 // Frontend Options
206 if (Args->hasArg(OPT_INPUT)) {
207 bool First = true;
208 for (arg_iterator it = Args->filtered_begin(OPT_INPUT),
209 ie = Args->filtered_end(); it != ie; ++it, First=false) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000210 const Arg *A = it;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000211 if (First)
Richard Smith1d489cf2012-11-01 04:30:05 +0000212 Opts.InputFile = A->getValue();
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000213 else {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000214 Diags.Report(diag::err_drv_unknown_argument) << A->getAsString(*Args);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000215 Success = false;
216 }
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000217 }
218 }
Daniel Dunbarc673af72010-05-20 18:15:20 +0000219 Opts.LLVMArgs = Args->getAllArgValues(OPT_mllvm);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000220 Opts.OutputPath = Args->getLastArgValue(OPT_o);
221 if (Arg *A = Args->getLastArg(OPT_filetype)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000222 StringRef Name = A->getValue();
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000223 unsigned OutputType = StringSwitch<unsigned>(Name)
224 .Case("asm", FT_Asm)
225 .Case("null", FT_Null)
226 .Case("obj", FT_Obj)
227 .Default(~0U);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000228 if (OutputType == ~0U) {
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000229 Diags.Report(diag::err_drv_invalid_value)
230 << A->getAsString(*Args) << Name;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000231 Success = false;
232 } else
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000233 Opts.OutputType = FileType(OutputType);
234 }
Daniel Dunbarc673af72010-05-20 18:15:20 +0000235 Opts.ShowHelp = Args->hasArg(OPT_help);
236 Opts.ShowVersion = Args->hasArg(OPT_version);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000237
238 // Transliterate Options
Reid Klecknerb1e25a12013-06-14 17:17:23 +0000239 Opts.OutputAsmVariant =
240 getLastArgIntValue(*Args.get(), OPT_output_asm_variant, 0, Diags);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000241 Opts.ShowEncoding = Args->hasArg(OPT_show_encoding);
242 Opts.ShowInst = Args->hasArg(OPT_show_inst);
243
244 // Assemble Options
David Blaikie73168db2013-07-25 21:19:01 +0000245 Opts.RelaxAll = Args->hasArg(OPT_mrelax_all);
246 Opts.NoExecStack = Args->hasArg(OPT_mno_exec_stack);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000247
Dylan Noblesmith89ea4162011-12-26 19:29:47 +0000248 return Success;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000249}
250
251static formatted_raw_ostream *GetOutputStream(AssemblerInvocation &Opts,
David Blaikied6471f72011-09-25 23:23:43 +0000252 DiagnosticsEngine &Diags,
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000253 bool Binary) {
Daniel Dunbarc673af72010-05-20 18:15:20 +0000254 if (Opts.OutputPath.empty())
255 Opts.OutputPath = "-";
256
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000257 // Make sure that the Out file gets unlinked from the disk if we get a
258 // SIGINT.
259 if (Opts.OutputPath != "-")
Rafael Espindola6f2e23b2013-06-13 21:02:40 +0000260 sys::RemoveFileOnSignal(Opts.OutputPath);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000261
262 std::string Error;
263 raw_fd_ostream *Out =
Rafael Espindolad965f952013-07-16 19:44:23 +0000264 new raw_fd_ostream(Opts.OutputPath.c_str(), Error,
Stephen Hines651f13c2014-04-23 16:59:28 -0700265 (Binary ? sys::fs::F_None : sys::fs::F_Text));
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000266 if (!Error.empty()) {
267 Diags.Report(diag::err_fe_unable_to_open_output)
268 << Opts.OutputPath << Error;
269 return 0;
270 }
271
272 return new formatted_raw_ostream(*Out, formatted_raw_ostream::DELETE_STREAM);
273}
274
David Blaikied6471f72011-09-25 23:23:43 +0000275static bool ExecuteAssembler(AssemblerInvocation &Opts,
276 DiagnosticsEngine &Diags) {
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000277 // Get the target specific parser.
278 std::string Error;
279 const Target *TheTarget(TargetRegistry::lookupTarget(Opts.Triple, Error));
280 if (!TheTarget) {
281 Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
282 return false;
283 }
284
Stephen Hines651f13c2014-04-23 16:59:28 -0700285 std::unique_ptr<MemoryBuffer> BufferPtr;
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000286 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Opts.InputFile, BufferPtr)) {
Michael J. Spencer3a321e22010-12-09 17:36:38 +0000287 Error = ec.message();
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000288 Diags.Report(diag::err_fe_error_reading) << Opts.InputFile;
289 return false;
290 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700291 MemoryBuffer *Buffer = BufferPtr.release();
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000292
293 SourceMgr SrcMgr;
294
295 // Tell SrcMgr about this buffer, which is what the parser will pick up.
296 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
297
298 // Record the location of the include directories so that the lexer can find
299 // it later.
300 SrcMgr.setIncludeDirs(Opts.IncludePaths);
301
Stephen Hines651f13c2014-04-23 16:59:28 -0700302 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(Opts.Triple));
Evan Cheng884744b2011-07-18 20:57:51 +0000303 assert(MRI && "Unable to create target register info!");
304
Stephen Hines651f13c2014-04-23 16:59:28 -0700305 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, Opts.Triple));
Rafael Espindola1fcf31e2013-05-13 01:24:18 +0000306 assert(MAI && "Unable to create target asm info!");
307
Stephen Hines651f13c2014-04-23 16:59:28 -0700308 // Ensure MCAsmInfo initialization occurs before any use, otherwise sections
309 // may be created with a combination of default and explicit settings.
310 if (Opts.CompressDebugSections)
311 MAI->setCompressDebugSections(true);
312
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000313 bool IsBinary = Opts.OutputType == AssemblerInvocation::FT_Obj;
314 formatted_raw_ostream *Out = GetOutputStream(Opts, Diags, IsBinary);
315 if (!Out)
316 return false;
317
Evan Cheng36fc3aa2011-07-20 06:22:27 +0000318 // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
319 // MCObjectFileInfo needs a MCContext reference in order to initialize itself.
Stephen Hines651f13c2014-04-23 16:59:28 -0700320 std::unique_ptr<MCObjectFileInfo> MOFI(new MCObjectFileInfo());
321
Bill Wendling4b7bae32013-06-18 07:22:05 +0000322 MCContext Ctx(MAI.get(), MRI.get(), MOFI.get(), &SrcMgr);
Evan Cheng36fc3aa2011-07-20 06:22:27 +0000323 // FIXME: Assembler behavior can change with -static.
Evan Cheng66488ed2011-07-20 19:53:19 +0000324 MOFI->InitMCObjectFileInfo(Opts.Triple,
325 Reloc::Default, CodeModel::Default, Ctx);
Daniel Dunbar402adc32011-03-28 22:49:24 +0000326 if (Opts.SaveTemporaryLabels)
327 Ctx.setAllowTemporaryLabels(false);
Kevin Enderby567003e2011-12-22 19:31:58 +0000328 if (Opts.GenDwarfForAssembly)
329 Ctx.setGenDwarfForAssembly(true);
330 if (!Opts.DwarfDebugFlags.empty())
331 Ctx.setDwarfDebugFlags(StringRef(Opts.DwarfDebugFlags));
Kevin Enderby02341792013-01-17 21:38:06 +0000332 if (!Opts.DwarfDebugProducer.empty())
333 Ctx.setDwarfDebugProducer(StringRef(Opts.DwarfDebugProducer));
Chandler Carruthd566df62012-12-17 21:40:04 +0000334 if (!Opts.DebugCompilationDir.empty())
335 Ctx.setCompilationDir(Opts.DebugCompilationDir);
Eric Christopher27e2b982012-12-18 00:31:10 +0000336 if (!Opts.MainFileName.empty())
337 Ctx.setMainFileName(StringRef(Opts.MainFileName));
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700338 Ctx.setDwarfVersion(Opts.DwarfVersion);
Rafael Espindola7872d482010-12-10 07:40:14 +0000339
Jim Grosbachfc308292012-02-10 20:37:10 +0000340 // Build up the feature string from the target feature list.
341 std::string FS;
342 if (!Opts.Features.empty()) {
343 FS = Opts.Features[0];
344 for (unsigned i = 1, e = Opts.Features.size(); i != e; ++i)
345 FS += "," + Opts.Features[i];
346 }
347
Stephen Hines651f13c2014-04-23 16:59:28 -0700348 std::unique_ptr<MCStreamer> Str;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000349
Stephen Hines651f13c2014-04-23 16:59:28 -0700350 std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
351 std::unique_ptr<MCSubtargetInfo> STI(
352 TheTarget->createMCSubtargetInfo(Opts.Triple, Opts.CPU, FS));
Evan Cheng74e13322011-07-11 04:24:19 +0000353
Rafael Espindola3176cca2011-01-23 17:58:26 +0000354 // FIXME: There is a bit of code duplication with addPassesToEmitFile.
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000355 if (Opts.OutputType == AssemblerInvocation::FT_Asm) {
356 MCInstPrinter *IP =
Bill Wendling99d43b42012-04-02 06:17:37 +0000357 TheTarget->createMCInstPrinter(Opts.OutputAsmVariant, *MAI, *MCII, *MRI,
358 *STI);
Benjamin Kramer7ac3d5a2010-07-29 17:48:03 +0000359 MCCodeEmitter *CE = 0;
Evan Cheng21118dc2011-07-25 23:25:09 +0000360 MCAsmBackend *MAB = 0;
Daniel Dunbar66cdf262010-12-16 03:06:05 +0000361 if (Opts.ShowEncoding) {
Jim Grosbach92ecfe92012-05-15 17:36:07 +0000362 CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, *STI, Ctx);
Bill Wendlinge904acf2013-09-09 02:37:56 +0000363 MAB = TheTarget->createMCAsmBackend(*MRI, Opts.Triple, Opts.CPU);
Daniel Dunbar66cdf262010-12-16 03:06:05 +0000364 }
Rafael Espindola7872d482010-12-10 07:40:14 +0000365 Str.reset(TheTarget->createAsmStreamer(Ctx, *Out, /*asmverbose*/true,
Nick Lewyckyea523d72011-10-17 23:05:52 +0000366 /*useDwarfDirectory*/ true,
367 IP, CE, MAB,
Rafael Espindola7872d482010-12-10 07:40:14 +0000368 Opts.ShowInst));
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000369 } else if (Opts.OutputType == AssemblerInvocation::FT_Null) {
370 Str.reset(createNullStreamer(Ctx));
371 } else {
372 assert(Opts.OutputType == AssemblerInvocation::FT_Obj &&
373 "Invalid file type!");
Jim Grosbach92ecfe92012-05-15 17:36:07 +0000374 MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, *STI, Ctx);
Bill Wendlinge904acf2013-09-09 02:37:56 +0000375 MCAsmBackend *MAB = TheTarget->createMCAsmBackend(*MRI, Opts.Triple,
376 Opts.CPU);
Evan Cheng7b6def72011-07-26 00:42:40 +0000377 Str.reset(TheTarget->createMCObjectStreamer(Opts.Triple, Ctx, *MAB, *Out,
Stephen Hines651f13c2014-04-23 16:59:28 -0700378 CE, *STI, Opts.RelaxAll,
Evan Cheng7b6def72011-07-26 00:42:40 +0000379 Opts.NoExecStack));
Rafael Espindola4f036fa2010-10-13 14:53:57 +0000380 Str.get()->InitSections();
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000381 }
382
Stephen Hines651f13c2014-04-23 16:59:28 -0700383 std::unique_ptr<MCAsmParser> Parser(
384 createMCAsmParser(SrcMgr, Ctx, *Str.get(), *MAI));
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700385
386 // FIXME: init MCTargetOptions from sanitizer flags here.
387 MCTargetOptions Options;
Stephen Hines651f13c2014-04-23 16:59:28 -0700388 std::unique_ptr<MCTargetAsmParser> TAP(
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700389 TheTarget->createMCAsmParser(*STI, *Parser, *MCII, Options));
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000390 if (!TAP) {
391 Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
392 return false;
393 }
394
Daniel Dunbar7374f1b2010-07-17 02:26:21 +0000395 Parser->setTargetParser(*TAP.get());
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000396
Daniel Dunbar7374f1b2010-07-17 02:26:21 +0000397 bool Success = !Parser->Run(Opts.NoInitialTextSection);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000398
399 // Close the output.
400 delete Out;
401
402 // Delete output on errors.
403 if (!Success && Opts.OutputPath != "-")
Rafael Espindola2669e962013-06-26 12:48:34 +0000404 sys::fs::remove(Opts.OutputPath);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000405
406 return Success;
407}
408
Chad Rosier90836282013-03-27 18:28:23 +0000409static void LLVMErrorHandler(void *UserData, const std::string &Message,
410 bool GenCrashDiag) {
David Blaikied6471f72011-09-25 23:23:43 +0000411 DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000412
413 Diags.Report(diag::err_fe_error_backend) << Message;
414
415 // We cannot recover from llvm errors.
416 exit(1);
417}
418
419int cc1as_main(const char **ArgBegin, const char **ArgEnd,
420 const char *Argv0, void *MainAddr) {
421 // Print a stack trace if we signal out.
422 sys::PrintStackTraceOnErrorSignal();
423 PrettyStackTraceProgram X(ArgEnd - ArgBegin, ArgBegin);
424 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
425
426 // Initialize targets and assembly printers/parsers.
427 InitializeAllTargetInfos();
Evan Chengd99d3e12011-07-22 21:59:11 +0000428 InitializeAllTargetMCs();
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000429 InitializeAllAsmParsers();
430
431 // Construct our diagnostic client.
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000432 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
Douglas Gregorbdbb0042010-08-18 22:29:43 +0000433 TextDiagnosticPrinter *DiagClient
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000434 = new TextDiagnosticPrinter(errs(), &*DiagOpts);
Douglas Gregorbdbb0042010-08-18 22:29:43 +0000435 DiagClient->setPrefix("clang -cc1as");
Dylan Noblesmithc93dc782012-02-20 14:00:23 +0000436 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000437 DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagClient);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000438
439 // Set an error handler, so that any LLVM backend diagnostics go through our
440 // error handler.
Dan Gohman726578c2010-08-18 21:23:17 +0000441 ScopedFatalErrorHandler FatalErrorHandler
442 (LLVMErrorHandler, static_cast<void*>(&Diags));
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000443
444 // Parse the arguments.
445 AssemblerInvocation Asm;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000446 if (!AssemblerInvocation::CreateFromArgs(Asm, ArgBegin, ArgEnd, Diags))
447 return 1;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000448
Daniel Dunbarc673af72010-05-20 18:15:20 +0000449 // Honor -help.
450 if (Asm.ShowHelp) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700451 std::unique_ptr<OptTable> Opts(driver::createCC1AsOptTable());
Daniel Dunbarc673af72010-05-20 18:15:20 +0000452 Opts->PrintHelp(llvm::outs(), "clang -cc1as", "Clang Integrated Assembler");
453 return 0;
454 }
455
456 // Honor -version.
457 //
458 // FIXME: Use a better -version message?
459 if (Asm.ShowVersion) {
460 llvm::cl::PrintVersionMessage();
461 return 0;
462 }
463
464 // Honor -mllvm.
465 //
466 // FIXME: Remove this, one day.
467 if (!Asm.LLVMArgs.empty()) {
468 unsigned NumArgs = Asm.LLVMArgs.size();
469 const char **Args = new const char*[NumArgs + 2];
470 Args[0] = "clang (LLVM option parsing)";
471 for (unsigned i = 0; i != NumArgs; ++i)
472 Args[i + 1] = Asm.LLVMArgs[i].c_str();
473 Args[NumArgs + 1] = 0;
David Blaikie6bd17d22012-02-07 19:36:38 +0000474 llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args);
Daniel Dunbarc673af72010-05-20 18:15:20 +0000475 }
476
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000477 // Execute the invocation, unless there were parsing errors.
478 bool Success = false;
Argyrios Kyrtzidise8f0ba72010-11-19 00:19:18 +0000479 if (!Diags.hasErrorOccurred())
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000480 Success = ExecuteAssembler(Asm, Diags);
481
482 // If any timers were active but haven't been destroyed yet, print their
483 // results now.
484 TimerGroup::printAll(errs());
485
486 return !Success;
487}