blob: 0a2fc45c2a45225557f5f10ad9e547eab3a0b2e6 [file] [log] [blame]
Daniel Dunbar2fcaa542010-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 Carruthcc0694c2012-12-04 09:25:21 +000016#include "clang/Basic/DiagnosticOptions.h"
Chandler Carruthcc0694c2012-12-04 09:25:21 +000017#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar2fcaa542010-05-20 17:49:16 +000018#include "clang/Driver/Options.h"
Daniel Dunbar2fcaa542010-05-20 17:49:16 +000019#include "clang/Frontend/FrontendDiagnostic.h"
20#include "clang/Frontend/TextDiagnosticPrinter.h"
Reid Kleckner898229a2013-06-14 17:17:23 +000021#include "clang/Frontend/Utils.h"
Daniel Dunbar2fcaa542010-05-20 17:49:16 +000022#include "llvm/ADT/StringSwitch.h"
Duncan Sandsf610b5b2010-08-30 09:42:39 +000023#include "llvm/ADT/Triple.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000024#include "llvm/IR/DataLayout.h"
Chandler Carruthcc0694c2012-12-04 09:25:21 +000025#include "llvm/MC/MCAsmBackend.h"
Daniel Dunbar05474282010-07-17 02:26:21 +000026#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbar2fcaa542010-05-20 17:49:16 +000027#include "llvm/MC/MCCodeEmitter.h"
28#include "llvm/MC/MCContext.h"
Evan Cheng279f2902011-07-11 04:24:19 +000029#include "llvm/MC/MCInstrInfo.h"
Evan Cheng347033f2011-07-20 06:22:27 +000030#include "llvm/MC/MCObjectFileInfo.h"
Chandler Carruthcc0694c2012-12-04 09:25:21 +000031#include "llvm/MC/MCParser/MCAsmParser.h"
Evan Chengb505ace2011-07-18 20:57:51 +000032#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbar2fcaa542010-05-20 17:49:16 +000033#include "llvm/MC/MCStreamer.h"
Evan Chengdec31872011-07-09 06:04:17 +000034#include "llvm/MC/MCSubtargetInfo.h"
Evan Cheng939f8092011-07-26 00:24:45 +000035#include "llvm/MC/MCTargetAsmParser.h"
Evgeniy Stepanoveeb820f2014-04-23 11:15:49 +000036#include "llvm/MC/MCTargetOptions.h"
Reid Kleckner898229a2013-06-14 17:17:23 +000037#include "llvm/Option/Arg.h"
38#include "llvm/Option/ArgList.h"
39#include "llvm/Option/OptTable.h"
Daniel Dunbar0454f652010-05-20 18:15:20 +000040#include "llvm/Support/CommandLine.h"
Daniel Dunbar2fcaa542010-05-20 17:49:16 +000041#include "llvm/Support/ErrorHandling.h"
Rafael Espindolaccc6ea62013-06-26 12:48:34 +000042#include "llvm/Support/FileSystem.h"
Chandler Carruthcc0694c2012-12-04 09:25:21 +000043#include "llvm/Support/FormattedStream.h"
44#include "llvm/Support/Host.h"
Daniel Dunbar2fcaa542010-05-20 17:49:16 +000045#include "llvm/Support/ManagedStatic.h"
46#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000047#include "llvm/Support/Path.h"
Chandler Carruthcc0694c2012-12-04 09:25:21 +000048#include "llvm/Support/PrettyStackTrace.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000049#include "llvm/Support/Signals.h"
Chandler Carruthcc0694c2012-12-04 09:25:21 +000050#include "llvm/Support/SourceMgr.h"
Evan Cheng494eb062011-08-24 18:09:14 +000051#include "llvm/Support/TargetRegistry.h"
52#include "llvm/Support/TargetSelect.h"
53#include "llvm/Support/Timer.h"
54#include "llvm/Support/raw_ostream.h"
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000055#include <memory>
Rafael Espindola8a8e5542014-06-12 17:19:42 +000056#include <system_error>
Daniel Dunbar2fcaa542010-05-20 17:49:16 +000057using namespace clang;
58using namespace clang::driver;
Alp Toker61dad752014-07-09 14:05:11 +000059using namespace clang::driver::options;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +000060using namespace llvm;
Reid Kleckner898229a2013-06-14 17:17:23 +000061using namespace llvm::opt;
Daniel Dunbar2fcaa542010-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 Grosbach576452b2012-02-10 20:37:10 +000070 /// The name of the target triple to assemble for.
Daniel Dunbar2fcaa542010-05-20 17:49:16 +000071 std::string Triple;
72
Jim Grosbach576452b2012-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 Dunbar2fcaa542010-05-20 17:49:16 +000081 /// @}
82 /// @name Language Options
83 /// @{
84
85 std::vector<std::string> IncludePaths;
86 unsigned NoInitialTextSection : 1;
Daniel Dunbar9cf7bc72011-03-28 22:49:24 +000087 unsigned SaveTemporaryLabels : 1;
Kevin Enderby292dc082011-12-22 19:31:58 +000088 unsigned GenDwarfForAssembly : 1;
David Blaikie7e2fd942014-03-27 20:47:30 +000089 unsigned CompressDebugSections : 1;
Oliver Stannard9b2a7d42014-05-19 13:39:13 +000090 unsigned DwarfVersion;
Kevin Enderby292dc082011-12-22 19:31:58 +000091 std::string DwarfDebugFlags;
Kevin Enderbyae2ec472013-01-17 21:38:06 +000092 std::string DwarfDebugProducer;
Chandler Carruth4d5e1a92012-12-17 21:40:04 +000093 std::string DebugCompilationDir;
Eric Christopher45f2e712012-12-18 00:31:10 +000094 std::string MainFileName;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +000095
96 /// @}
97 /// @name Frontend Options
98 /// @{
99
100 std::string InputFile;
Daniel Dunbar0454f652010-05-20 18:15:20 +0000101 std::vector<std::string> LLVMArgs;
Daniel Dunbar2fcaa542010-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 Dunbar0454f652010-05-20 18:15:20 +0000109 unsigned ShowHelp : 1;
110 unsigned ShowVersion : 1;
Daniel Dunbar2fcaa542010-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 Espindola148141c2011-01-23 17:58:26 +0000125 unsigned NoExecStack : 1;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000126
127 /// @}
128
129public:
130 AssemblerInvocation() {
131 Triple = "";
132 NoInitialTextSection = 0;
133 InputFile = "-";
Daniel Dunbar0454f652010-05-20 18:15:20 +0000134 OutputPath = "-";
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000135 OutputType = FT_Asm;
136 OutputAsmVariant = 0;
137 ShowInst = 0;
138 ShowEncoding = 0;
139 RelaxAll = 0;
Rafael Espindola148141c2011-01-23 17:58:26 +0000140 NoExecStack = 0;
Oliver Stannard9b2a7d42014-05-19 13:39:13 +0000141 DwarfVersion = 3;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000142 }
143
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000144 static bool CreateFromArgs(AssemblerInvocation &Res, const char **ArgBegin,
David Blaikie9c902b52011-09-25 23:23:43 +0000145 const char **ArgEnd, DiagnosticsEngine &Diags);
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000146};
147
148}
149
Alp Toker61dad752014-07-09 14:05:11 +0000150#if 0
151bool CompilerInvocation::CreateFromArgs(CompilerInvocation &Res,
152 const char *const *ArgBegin,
153 const char *const *ArgEnd,
154 DiagnosticsEngine &Diags) {
155 bool Success = true;
156
157 // Parse the arguments.
158 std::unique_ptr<OptTable> Opts(createDriverOptTable());
159 const unsigned IncludedFlagsBitmask = options::CC1Option;
160 unsigned MissingArgIndex, MissingArgCount;
161 std::unique_ptr<InputArgList> Args(
162 Opts->ParseArgs(ArgBegin, ArgEnd, MissingArgIndex, MissingArgCount,
163 IncludedFlagsBitmask));
164
165 // Check for missing argument error.
166 if (MissingArgCount) {
167 Diags.Report(diag::err_drv_missing_argument)
168 << Args->getArgString(MissingArgIndex) << MissingArgCount;
169 Success = false;
170 }
171
172 // Issue errors on unknown arguments.
173 for (arg_iterator it = Args->filtered_begin(OPT_UNKNOWN),
174 ie = Args->filtered_end(); it != ie; ++it) {
175 Diags.Report(diag::err_drv_unknown_argument) << (*it)->getAsString(*Args);
176 Success = false;
177 }
178}
179#endif
180
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000181bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000182 const char **ArgBegin,
183 const char **ArgEnd,
David Blaikie9c902b52011-09-25 23:23:43 +0000184 DiagnosticsEngine &Diags) {
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000185 bool Success = true;
186
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000187 // Parse the arguments.
Alp Toker61dad752014-07-09 14:05:11 +0000188 std::unique_ptr<OptTable> OptTbl(createDriverOptTable());
189
190 const unsigned IncludedFlagsBitmask = options::CC1AsOption;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000191 unsigned MissingArgIndex, MissingArgCount;
Ahmed Charlesb8984322014-03-07 20:03:18 +0000192 std::unique_ptr<InputArgList> Args(
Alp Toker61dad752014-07-09 14:05:11 +0000193 OptTbl->ParseArgs(ArgBegin, ArgEnd, MissingArgIndex, MissingArgCount,
194 IncludedFlagsBitmask));
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000195
196 // Check for missing argument error.
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000197 if (MissingArgCount) {
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000198 Diags.Report(diag::err_drv_missing_argument)
Alp Toker61dad752014-07-09 14:05:11 +0000199 << Args->getArgString(MissingArgIndex) << MissingArgCount;
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000200 Success = false;
201 }
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000202
203 // Issue errors on unknown arguments.
Alp Toker61dad752014-07-09 14:05:11 +0000204 for (arg_iterator it = Args->filtered_begin(OPT_UNKNOWN),
205 ie = Args->filtered_end();
206 it != ie; ++it) {
207 Diags.Report(diag::err_drv_unknown_argument) << (*it)->getAsString(*Args);
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000208 Success = false;
209 }
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000210
211 // Construct the invocation.
212
213 // Target Options
Jim Grosbach576452b2012-02-10 20:37:10 +0000214 Opts.Triple = llvm::Triple::normalize(Args->getLastArgValue(OPT_triple));
215 Opts.CPU = Args->getLastArgValue(OPT_target_cpu);
216 Opts.Features = Args->getAllArgValues(OPT_target_feature);
217
218 // Use the default target triple if unspecified.
219 if (Opts.Triple.empty())
220 Opts.Triple = llvm::sys::getDefaultTargetTriple();
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000221
222 // Language Options
223 Opts.IncludePaths = Args->getAllArgValues(OPT_I);
224 Opts.NoInitialTextSection = Args->hasArg(OPT_n);
Bob Wilsona1b42062013-09-26 21:00:51 +0000225 Opts.SaveTemporaryLabels = Args->hasArg(OPT_msave_temp_labels);
Alp Toker61dad752014-07-09 14:05:11 +0000226 Opts.GenDwarfForAssembly = Args->hasArg(OPT_g_Flag);
David Blaikie7e2fd942014-03-27 20:47:30 +0000227 Opts.CompressDebugSections = Args->hasArg(OPT_compress_debug_sections);
Oliver Stannard9b2a7d42014-05-19 13:39:13 +0000228 if (Args->hasArg(OPT_gdwarf_2))
229 Opts.DwarfVersion = 2;
230 if (Args->hasArg(OPT_gdwarf_3))
231 Opts.DwarfVersion = 3;
232 if (Args->hasArg(OPT_gdwarf_4))
233 Opts.DwarfVersion = 4;
Kevin Enderby292dc082011-12-22 19:31:58 +0000234 Opts.DwarfDebugFlags = Args->getLastArgValue(OPT_dwarf_debug_flags);
Kevin Enderbyae2ec472013-01-17 21:38:06 +0000235 Opts.DwarfDebugProducer = Args->getLastArgValue(OPT_dwarf_debug_producer);
Chandler Carruth4d5e1a92012-12-17 21:40:04 +0000236 Opts.DebugCompilationDir = Args->getLastArgValue(OPT_fdebug_compilation_dir);
Eric Christopher45f2e712012-12-18 00:31:10 +0000237 Opts.MainFileName = Args->getLastArgValue(OPT_main_file_name);
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000238
239 // Frontend Options
240 if (Args->hasArg(OPT_INPUT)) {
241 bool First = true;
242 for (arg_iterator it = Args->filtered_begin(OPT_INPUT),
243 ie = Args->filtered_end(); it != ie; ++it, First=false) {
Daniel Dunbara442fd52010-06-11 22:00:13 +0000244 const Arg *A = it;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000245 if (First)
Richard Smithbd55daf2012-11-01 04:30:05 +0000246 Opts.InputFile = A->getValue();
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000247 else {
Daniel Dunbara442fd52010-06-11 22:00:13 +0000248 Diags.Report(diag::err_drv_unknown_argument) << A->getAsString(*Args);
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000249 Success = false;
250 }
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000251 }
252 }
Daniel Dunbar0454f652010-05-20 18:15:20 +0000253 Opts.LLVMArgs = Args->getAllArgValues(OPT_mllvm);
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000254 Opts.OutputPath = Args->getLastArgValue(OPT_o);
255 if (Arg *A = Args->getLastArg(OPT_filetype)) {
Richard Smithbd55daf2012-11-01 04:30:05 +0000256 StringRef Name = A->getValue();
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000257 unsigned OutputType = StringSwitch<unsigned>(Name)
258 .Case("asm", FT_Asm)
259 .Case("null", FT_Null)
260 .Case("obj", FT_Obj)
261 .Default(~0U);
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000262 if (OutputType == ~0U) {
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000263 Diags.Report(diag::err_drv_invalid_value)
264 << A->getAsString(*Args) << Name;
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000265 Success = false;
266 } else
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000267 Opts.OutputType = FileType(OutputType);
268 }
Daniel Dunbar0454f652010-05-20 18:15:20 +0000269 Opts.ShowHelp = Args->hasArg(OPT_help);
270 Opts.ShowVersion = Args->hasArg(OPT_version);
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000271
272 // Transliterate Options
Reid Kleckner898229a2013-06-14 17:17:23 +0000273 Opts.OutputAsmVariant =
274 getLastArgIntValue(*Args.get(), OPT_output_asm_variant, 0, Diags);
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000275 Opts.ShowEncoding = Args->hasArg(OPT_show_encoding);
276 Opts.ShowInst = Args->hasArg(OPT_show_inst);
277
278 // Assemble Options
David Blaikie9260ed62013-07-25 21:19:01 +0000279 Opts.RelaxAll = Args->hasArg(OPT_mrelax_all);
280 Opts.NoExecStack = Args->hasArg(OPT_mno_exec_stack);
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000281
Dylan Noblesmith68207c02011-12-26 19:29:47 +0000282 return Success;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000283}
284
285static formatted_raw_ostream *GetOutputStream(AssemblerInvocation &Opts,
David Blaikie9c902b52011-09-25 23:23:43 +0000286 DiagnosticsEngine &Diags,
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000287 bool Binary) {
Daniel Dunbar0454f652010-05-20 18:15:20 +0000288 if (Opts.OutputPath.empty())
289 Opts.OutputPath = "-";
290
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000291 // Make sure that the Out file gets unlinked from the disk if we get a
292 // SIGINT.
293 if (Opts.OutputPath != "-")
Rafael Espindola18556de2013-06-13 21:02:40 +0000294 sys::RemoveFileOnSignal(Opts.OutputPath);
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000295
296 std::string Error;
297 raw_fd_ostream *Out =
Rafael Espindola16125fb2013-07-16 19:44:23 +0000298 new raw_fd_ostream(Opts.OutputPath.c_str(), Error,
Rafael Espindola4fbd3732014-02-24 18:20:21 +0000299 (Binary ? sys::fs::F_None : sys::fs::F_Text));
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000300 if (!Error.empty()) {
301 Diags.Report(diag::err_fe_unable_to_open_output)
302 << Opts.OutputPath << Error;
Alp Tokere98ea7c2014-05-31 00:02:21 +0000303 delete Out;
Craig Topper69186e72014-06-08 08:38:04 +0000304 return nullptr;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000305 }
306
307 return new formatted_raw_ostream(*Out, formatted_raw_ostream::DELETE_STREAM);
308}
309
David Blaikie9c902b52011-09-25 23:23:43 +0000310static bool ExecuteAssembler(AssemblerInvocation &Opts,
311 DiagnosticsEngine &Diags) {
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000312 // Get the target specific parser.
313 std::string Error;
Alp Tokere98ea7c2014-05-31 00:02:21 +0000314 const Target *TheTarget = TargetRegistry::lookupTarget(Opts.Triple, Error);
Alp Toker44293142014-05-31 00:02:27 +0000315 if (!TheTarget)
316 return Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000317
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000318 ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
319 MemoryBuffer::getFileOrSTDIN(Opts.InputFile);
320
321 if (std::error_code EC = Buffer.getError()) {
322 Error = EC.message();
Alp Toker44293142014-05-31 00:02:27 +0000323 return Diags.Report(diag::err_fe_error_reading) << Opts.InputFile;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000324 }
325
326 SourceMgr SrcMgr;
327
328 // Tell SrcMgr about this buffer, which is what the parser will pick up.
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000329 SrcMgr.AddNewSourceBuffer(Buffer->release(), SMLoc());
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000330
331 // Record the location of the include directories so that the lexer can find
332 // it later.
333 SrcMgr.setIncludeDirs(Opts.IncludePaths);
334
Ahmed Charlesb8984322014-03-07 20:03:18 +0000335 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(Opts.Triple));
Evan Chengb505ace2011-07-18 20:57:51 +0000336 assert(MRI && "Unable to create target register info!");
337
Ahmed Charlesb8984322014-03-07 20:03:18 +0000338 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, Opts.Triple));
Rafael Espindola77056232013-05-13 01:24:18 +0000339 assert(MAI && "Unable to create target asm info!");
340
David Blaikieb31e1d12014-03-31 23:47:13 +0000341 // Ensure MCAsmInfo initialization occurs before any use, otherwise sections
342 // may be created with a combination of default and explicit settings.
343 if (Opts.CompressDebugSections)
344 MAI->setCompressDebugSections(true);
345
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000346 bool IsBinary = Opts.OutputType == AssemblerInvocation::FT_Obj;
Alp Tokere98ea7c2014-05-31 00:02:21 +0000347 std::unique_ptr<formatted_raw_ostream> Out(
348 GetOutputStream(Opts, Diags, IsBinary));
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000349 if (!Out)
Alp Toker44293142014-05-31 00:02:27 +0000350 return true;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000351
Evan Cheng347033f2011-07-20 06:22:27 +0000352 // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
353 // MCObjectFileInfo needs a MCContext reference in order to initialize itself.
Ahmed Charlesb8984322014-03-07 20:03:18 +0000354 std::unique_ptr<MCObjectFileInfo> MOFI(new MCObjectFileInfo());
David Blaikie07b8d4e2014-03-31 23:13:30 +0000355
Bill Wendlingda1e3e72013-06-18 07:22:05 +0000356 MCContext Ctx(MAI.get(), MRI.get(), MOFI.get(), &SrcMgr);
Evan Cheng347033f2011-07-20 06:22:27 +0000357 // FIXME: Assembler behavior can change with -static.
Evan Chengb5517a42011-07-20 19:53:19 +0000358 MOFI->InitMCObjectFileInfo(Opts.Triple,
359 Reloc::Default, CodeModel::Default, Ctx);
Daniel Dunbar9cf7bc72011-03-28 22:49:24 +0000360 if (Opts.SaveTemporaryLabels)
361 Ctx.setAllowTemporaryLabels(false);
Kevin Enderby292dc082011-12-22 19:31:58 +0000362 if (Opts.GenDwarfForAssembly)
363 Ctx.setGenDwarfForAssembly(true);
364 if (!Opts.DwarfDebugFlags.empty())
365 Ctx.setDwarfDebugFlags(StringRef(Opts.DwarfDebugFlags));
Kevin Enderbyae2ec472013-01-17 21:38:06 +0000366 if (!Opts.DwarfDebugProducer.empty())
367 Ctx.setDwarfDebugProducer(StringRef(Opts.DwarfDebugProducer));
Chandler Carruth4d5e1a92012-12-17 21:40:04 +0000368 if (!Opts.DebugCompilationDir.empty())
369 Ctx.setCompilationDir(Opts.DebugCompilationDir);
Eric Christopher45f2e712012-12-18 00:31:10 +0000370 if (!Opts.MainFileName.empty())
371 Ctx.setMainFileName(StringRef(Opts.MainFileName));
Oliver Stannard9b2a7d42014-05-19 13:39:13 +0000372 Ctx.setDwarfVersion(Opts.DwarfVersion);
Rafael Espindola86f2c572010-12-10 07:40:14 +0000373
Jim Grosbach576452b2012-02-10 20:37:10 +0000374 // Build up the feature string from the target feature list.
375 std::string FS;
376 if (!Opts.Features.empty()) {
377 FS = Opts.Features[0];
378 for (unsigned i = 1, e = Opts.Features.size(); i != e; ++i)
379 FS += "," + Opts.Features[i];
380 }
381
Ahmed Charlesb8984322014-03-07 20:03:18 +0000382 std::unique_ptr<MCStreamer> Str;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000383
Ahmed Charlesb8984322014-03-07 20:03:18 +0000384 std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
385 std::unique_ptr<MCSubtargetInfo> STI(
386 TheTarget->createMCSubtargetInfo(Opts.Triple, Opts.CPU, FS));
Evan Cheng279f2902011-07-11 04:24:19 +0000387
Rafael Espindola148141c2011-01-23 17:58:26 +0000388 // FIXME: There is a bit of code duplication with addPassesToEmitFile.
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000389 if (Opts.OutputType == AssemblerInvocation::FT_Asm) {
390 MCInstPrinter *IP =
Bill Wendlingf4ec97d2012-04-02 06:17:37 +0000391 TheTarget->createMCInstPrinter(Opts.OutputAsmVariant, *MAI, *MCII, *MRI,
392 *STI);
Craig Topper69186e72014-06-08 08:38:04 +0000393 MCCodeEmitter *CE = nullptr;
394 MCAsmBackend *MAB = nullptr;
Daniel Dunbar8f514a82010-12-16 03:06:05 +0000395 if (Opts.ShowEncoding) {
Jim Grosbach490950a2012-05-15 17:36:07 +0000396 CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, *STI, Ctx);
Bill Wendling52575382013-09-09 02:37:56 +0000397 MAB = TheTarget->createMCAsmBackend(*MRI, Opts.Triple, Opts.CPU);
Daniel Dunbar8f514a82010-12-16 03:06:05 +0000398 }
Rafael Espindola86f2c572010-12-10 07:40:14 +0000399 Str.reset(TheTarget->createAsmStreamer(Ctx, *Out, /*asmverbose*/true,
Nick Lewycky1d617ac2011-10-17 23:05:52 +0000400 /*useDwarfDirectory*/ true,
401 IP, CE, MAB,
Rafael Espindola86f2c572010-12-10 07:40:14 +0000402 Opts.ShowInst));
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000403 } else if (Opts.OutputType == AssemblerInvocation::FT_Null) {
404 Str.reset(createNullStreamer(Ctx));
405 } else {
406 assert(Opts.OutputType == AssemblerInvocation::FT_Obj &&
407 "Invalid file type!");
Jim Grosbach490950a2012-05-15 17:36:07 +0000408 MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, *STI, Ctx);
Bill Wendling52575382013-09-09 02:37:56 +0000409 MCAsmBackend *MAB = TheTarget->createMCAsmBackend(*MRI, Opts.Triple,
410 Opts.CPU);
Evan Cheng9568f272011-07-26 00:42:40 +0000411 Str.reset(TheTarget->createMCObjectStreamer(Opts.Triple, Ctx, *MAB, *Out,
Rafael Espindolad51906e2014-01-26 06:39:14 +0000412 CE, *STI, Opts.RelaxAll,
Evan Cheng9568f272011-07-26 00:42:40 +0000413 Opts.NoExecStack));
Rafael Espindola85111902010-10-13 14:53:57 +0000414 Str.get()->InitSections();
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000415 }
416
Alp Toker44293142014-05-31 00:02:27 +0000417 bool Failed = false;
Alp Tokere98ea7c2014-05-31 00:02:21 +0000418
Ahmed Charlesb8984322014-03-07 20:03:18 +0000419 std::unique_ptr<MCAsmParser> Parser(
420 createMCAsmParser(SrcMgr, Ctx, *Str.get(), *MAI));
Evgeniy Stepanoveeb820f2014-04-23 11:15:49 +0000421
422 // FIXME: init MCTargetOptions from sanitizer flags here.
423 MCTargetOptions Options;
Ahmed Charlesb8984322014-03-07 20:03:18 +0000424 std::unique_ptr<MCTargetAsmParser> TAP(
Evgeniy Stepanoveeb820f2014-04-23 11:15:49 +0000425 TheTarget->createMCAsmParser(*STI, *Parser, *MCII, Options));
Alp Toker44293142014-05-31 00:02:27 +0000426 if (!TAP)
427 Failed = Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000428
Alp Toker44293142014-05-31 00:02:27 +0000429 if (!Failed) {
Alp Tokere98ea7c2014-05-31 00:02:21 +0000430 Parser->setTargetParser(*TAP.get());
Alp Toker44293142014-05-31 00:02:27 +0000431 Failed = Parser->Run(Opts.NoInitialTextSection);
Alp Tokere98ea7c2014-05-31 00:02:21 +0000432 }
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000433
Alp Tokere98ea7c2014-05-31 00:02:21 +0000434 // Close the output stream early.
435 Out.reset();
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000436
Alp Tokere98ea7c2014-05-31 00:02:21 +0000437 // Delete output file if there were errors.
Alp Toker44293142014-05-31 00:02:27 +0000438 if (Failed && Opts.OutputPath != "-")
Rafael Espindolaccc6ea62013-06-26 12:48:34 +0000439 sys::fs::remove(Opts.OutputPath);
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000440
Alp Toker44293142014-05-31 00:02:27 +0000441 return Failed;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000442}
443
Chad Rosier05c71aa2013-03-27 18:28:23 +0000444static void LLVMErrorHandler(void *UserData, const std::string &Message,
445 bool GenCrashDiag) {
David Blaikie9c902b52011-09-25 23:23:43 +0000446 DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000447
448 Diags.Report(diag::err_fe_error_backend) << Message;
449
450 // We cannot recover from llvm errors.
451 exit(1);
452}
453
454int cc1as_main(const char **ArgBegin, const char **ArgEnd,
455 const char *Argv0, void *MainAddr) {
456 // Print a stack trace if we signal out.
457 sys::PrintStackTraceOnErrorSignal();
458 PrettyStackTraceProgram X(ArgEnd - ArgBegin, ArgBegin);
459 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
460
461 // Initialize targets and assembly printers/parsers.
462 InitializeAllTargetInfos();
Evan Chengc391a582011-07-22 21:59:11 +0000463 InitializeAllTargetMCs();
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000464 InitializeAllAsmParsers();
465
466 // Construct our diagnostic client.
Douglas Gregor811db4e2012-10-23 22:26:28 +0000467 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
Douglas Gregor2dd19f12010-08-18 22:29:43 +0000468 TextDiagnosticPrinter *DiagClient
Douglas Gregor811db4e2012-10-23 22:26:28 +0000469 = new TextDiagnosticPrinter(errs(), &*DiagOpts);
Douglas Gregor2dd19f12010-08-18 22:29:43 +0000470 DiagClient->setPrefix("clang -cc1as");
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000471 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
Douglas Gregor811db4e2012-10-23 22:26:28 +0000472 DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagClient);
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000473
474 // Set an error handler, so that any LLVM backend diagnostics go through our
475 // error handler.
Dan Gohmanb37af7d2010-08-18 21:23:17 +0000476 ScopedFatalErrorHandler FatalErrorHandler
477 (LLVMErrorHandler, static_cast<void*>(&Diags));
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000478
479 // Parse the arguments.
480 AssemblerInvocation Asm;
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000481 if (!AssemblerInvocation::CreateFromArgs(Asm, ArgBegin, ArgEnd, Diags))
482 return 1;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000483
Daniel Dunbar0454f652010-05-20 18:15:20 +0000484 if (Asm.ShowHelp) {
Alp Toker61dad752014-07-09 14:05:11 +0000485 std::unique_ptr<OptTable> Opts(driver::createDriverOptTable());
486 Opts->PrintHelp(llvm::outs(), "clang -cc1as",
487 "Clang Integrated Assembler",
488 /*Include=*/ driver::options::CC1AsOption, /*Exclude=*/ 0);
Daniel Dunbar0454f652010-05-20 18:15:20 +0000489 return 0;
490 }
491
492 // Honor -version.
493 //
494 // FIXME: Use a better -version message?
495 if (Asm.ShowVersion) {
496 llvm::cl::PrintVersionMessage();
497 return 0;
498 }
499
500 // Honor -mllvm.
501 //
502 // FIXME: Remove this, one day.
503 if (!Asm.LLVMArgs.empty()) {
504 unsigned NumArgs = Asm.LLVMArgs.size();
505 const char **Args = new const char*[NumArgs + 2];
506 Args[0] = "clang (LLVM option parsing)";
507 for (unsigned i = 0; i != NumArgs; ++i)
508 Args[i + 1] = Asm.LLVMArgs[i].c_str();
Craig Topper69186e72014-06-08 08:38:04 +0000509 Args[NumArgs + 1] = nullptr;
David Blaikie09d20ee2012-02-07 19:36:38 +0000510 llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args);
Daniel Dunbar0454f652010-05-20 18:15:20 +0000511 }
512
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000513 // Execute the invocation, unless there were parsing errors.
Alp Toker44293142014-05-31 00:02:27 +0000514 bool Failed = Diags.hasErrorOccurred() || ExecuteAssembler(Asm, Diags);
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000515
516 // If any timers were active but haven't been destroyed yet, print their
517 // results now.
518 TimerGroup::printAll(errs());
519
Alp Toker44293142014-05-31 00:02:27 +0000520 return !!Failed;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000521}
Alp Toker61dad752014-07-09 14:05:11 +0000522