blob: 55c9fe602fa35bb2e2bcac4dc1f19eabcf189bf0 [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"
Chandler Carruthf59edb92012-12-04 09:25:21 +000017#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar41b5b172010-05-20 17:49:16 +000018#include "clang/Driver/Options.h"
Daniel Dunbar41b5b172010-05-20 17:49:16 +000019#include "clang/Frontend/FrontendDiagnostic.h"
20#include "clang/Frontend/TextDiagnosticPrinter.h"
Reid Klecknerb1e25a12013-06-14 17:17:23 +000021#include "clang/Frontend/Utils.h"
Daniel Dunbar41b5b172010-05-20 17:49:16 +000022#include "llvm/ADT/StringSwitch.h"
Duncan Sands2dc14532010-08-30 09:42:39 +000023#include "llvm/ADT/Triple.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000024#include "llvm/IR/DataLayout.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000025#include "llvm/MC/MCAsmBackend.h"
Daniel Dunbar7374f1b2010-07-17 02:26:21 +000026#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbar41b5b172010-05-20 17:49:16 +000027#include "llvm/MC/MCCodeEmitter.h"
28#include "llvm/MC/MCContext.h"
Evan Cheng74e13322011-07-11 04:24:19 +000029#include "llvm/MC/MCInstrInfo.h"
Evan Cheng36fc3aa2011-07-20 06:22:27 +000030#include "llvm/MC/MCObjectFileInfo.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000031#include "llvm/MC/MCParser/MCAsmParser.h"
Evan Cheng884744b2011-07-18 20:57:51 +000032#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbar41b5b172010-05-20 17:49:16 +000033#include "llvm/MC/MCStreamer.h"
Evan Chengbb36ed92011-07-09 06:04:17 +000034#include "llvm/MC/MCSubtargetInfo.h"
Evan Cheng37712352011-07-26 00:24:45 +000035#include "llvm/MC/MCTargetAsmParser.h"
Stephen Hines6bcf27b2014-05-29 04:14:42 -070036#include "llvm/MC/MCTargetOptions.h"
Reid Klecknerb1e25a12013-06-14 17:17:23 +000037#include "llvm/Option/Arg.h"
38#include "llvm/Option/ArgList.h"
39#include "llvm/Option/OptTable.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"
Rafael Espindola2669e962013-06-26 12:48:34 +000042#include "llvm/Support/FileSystem.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"
Chandler Carruthf59edb92012-12-04 09:25:21 +000048#include "llvm/Support/PrettyStackTrace.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000049#include "llvm/Support/Signals.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000050#include "llvm/Support/SourceMgr.h"
Evan Chenga6b40452011-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"
Stephen Hines651f13c2014-04-23 16:59:28 -070055#include <memory>
Stephen Hinesc568f1e2014-07-21 00:47:37 -070056#include <system_error>
Daniel Dunbar41b5b172010-05-20 17:49:16 +000057using namespace clang;
58using namespace clang::driver;
Stephen Hinesc568f1e2014-07-21 00:47:37 -070059using namespace clang::driver::options;
Daniel Dunbar41b5b172010-05-20 17:49:16 +000060using 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;
Stephen Hines176edba2014-12-01 14:53:08 -0800126 unsigned FatalWarnings : 1;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000127
128 /// @}
129
130public:
131 AssemblerInvocation() {
132 Triple = "";
133 NoInitialTextSection = 0;
134 InputFile = "-";
Daniel Dunbarc673af72010-05-20 18:15:20 +0000135 OutputPath = "-";
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000136 OutputType = FT_Asm;
137 OutputAsmVariant = 0;
138 ShowInst = 0;
139 ShowEncoding = 0;
140 RelaxAll = 0;
Rafael Espindola3176cca2011-01-23 17:58:26 +0000141 NoExecStack = 0;
Stephen Hines176edba2014-12-01 14:53:08 -0800142 FatalWarnings = 0;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700143 DwarfVersion = 3;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000144 }
145
Stephen Hines176edba2014-12-01 14:53:08 -0800146 static bool CreateFromArgs(AssemblerInvocation &Res,
147 ArrayRef<const char *> Argv,
148 DiagnosticsEngine &Diags);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000149};
150
151}
152
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000153bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,
Stephen Hines176edba2014-12-01 14:53:08 -0800154 ArrayRef<const char *> Argv,
David Blaikied6471f72011-09-25 23:23:43 +0000155 DiagnosticsEngine &Diags) {
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000156 bool Success = true;
157
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000158 // Parse the arguments.
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700159 std::unique_ptr<OptTable> OptTbl(createDriverOptTable());
160
161 const unsigned IncludedFlagsBitmask = options::CC1AsOption;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000162 unsigned MissingArgIndex, MissingArgCount;
Stephen Hines651f13c2014-04-23 16:59:28 -0700163 std::unique_ptr<InputArgList> Args(
Stephen Hines176edba2014-12-01 14:53:08 -0800164 OptTbl->ParseArgs(Argv.begin(), Argv.end(), MissingArgIndex, MissingArgCount,
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700165 IncludedFlagsBitmask));
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000166
167 // Check for missing argument error.
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000168 if (MissingArgCount) {
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000169 Diags.Report(diag::err_drv_missing_argument)
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700170 << Args->getArgString(MissingArgIndex) << MissingArgCount;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000171 Success = false;
172 }
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000173
174 // Issue errors on unknown arguments.
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700175 for (arg_iterator it = Args->filtered_begin(OPT_UNKNOWN),
176 ie = Args->filtered_end();
177 it != ie; ++it) {
178 Diags.Report(diag::err_drv_unknown_argument) << (*it)->getAsString(*Args);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000179 Success = false;
180 }
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000181
182 // Construct the invocation.
183
184 // Target Options
Jim Grosbachfc308292012-02-10 20:37:10 +0000185 Opts.Triple = llvm::Triple::normalize(Args->getLastArgValue(OPT_triple));
186 Opts.CPU = Args->getLastArgValue(OPT_target_cpu);
187 Opts.Features = Args->getAllArgValues(OPT_target_feature);
188
189 // Use the default target triple if unspecified.
190 if (Opts.Triple.empty())
191 Opts.Triple = llvm::sys::getDefaultTargetTriple();
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000192
193 // Language Options
194 Opts.IncludePaths = Args->getAllArgValues(OPT_I);
195 Opts.NoInitialTextSection = Args->hasArg(OPT_n);
Bob Wilson084be2d2013-09-26 21:00:51 +0000196 Opts.SaveTemporaryLabels = Args->hasArg(OPT_msave_temp_labels);
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700197 Opts.GenDwarfForAssembly = Args->hasArg(OPT_g_Flag);
Stephen Hines651f13c2014-04-23 16:59:28 -0700198 Opts.CompressDebugSections = Args->hasArg(OPT_compress_debug_sections);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700199 if (Args->hasArg(OPT_gdwarf_2))
200 Opts.DwarfVersion = 2;
201 if (Args->hasArg(OPT_gdwarf_3))
202 Opts.DwarfVersion = 3;
203 if (Args->hasArg(OPT_gdwarf_4))
204 Opts.DwarfVersion = 4;
Kevin Enderby567003e2011-12-22 19:31:58 +0000205 Opts.DwarfDebugFlags = Args->getLastArgValue(OPT_dwarf_debug_flags);
Kevin Enderby02341792013-01-17 21:38:06 +0000206 Opts.DwarfDebugProducer = Args->getLastArgValue(OPT_dwarf_debug_producer);
Chandler Carruthd566df62012-12-17 21:40:04 +0000207 Opts.DebugCompilationDir = Args->getLastArgValue(OPT_fdebug_compilation_dir);
Eric Christopher27e2b982012-12-18 00:31:10 +0000208 Opts.MainFileName = Args->getLastArgValue(OPT_main_file_name);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000209
210 // Frontend Options
211 if (Args->hasArg(OPT_INPUT)) {
212 bool First = true;
213 for (arg_iterator it = Args->filtered_begin(OPT_INPUT),
214 ie = Args->filtered_end(); it != ie; ++it, First=false) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000215 const Arg *A = it;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000216 if (First)
Richard Smith1d489cf2012-11-01 04:30:05 +0000217 Opts.InputFile = A->getValue();
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000218 else {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000219 Diags.Report(diag::err_drv_unknown_argument) << A->getAsString(*Args);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000220 Success = false;
221 }
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000222 }
223 }
Daniel Dunbarc673af72010-05-20 18:15:20 +0000224 Opts.LLVMArgs = Args->getAllArgValues(OPT_mllvm);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000225 Opts.OutputPath = Args->getLastArgValue(OPT_o);
226 if (Arg *A = Args->getLastArg(OPT_filetype)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000227 StringRef Name = A->getValue();
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000228 unsigned OutputType = StringSwitch<unsigned>(Name)
229 .Case("asm", FT_Asm)
230 .Case("null", FT_Null)
231 .Case("obj", FT_Obj)
232 .Default(~0U);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000233 if (OutputType == ~0U) {
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000234 Diags.Report(diag::err_drv_invalid_value)
235 << A->getAsString(*Args) << Name;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000236 Success = false;
237 } else
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000238 Opts.OutputType = FileType(OutputType);
239 }
Daniel Dunbarc673af72010-05-20 18:15:20 +0000240 Opts.ShowHelp = Args->hasArg(OPT_help);
241 Opts.ShowVersion = Args->hasArg(OPT_version);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000242
243 // Transliterate Options
Reid Klecknerb1e25a12013-06-14 17:17:23 +0000244 Opts.OutputAsmVariant =
245 getLastArgIntValue(*Args.get(), OPT_output_asm_variant, 0, Diags);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000246 Opts.ShowEncoding = Args->hasArg(OPT_show_encoding);
247 Opts.ShowInst = Args->hasArg(OPT_show_inst);
248
249 // Assemble Options
David Blaikie73168db2013-07-25 21:19:01 +0000250 Opts.RelaxAll = Args->hasArg(OPT_mrelax_all);
Stephen Hines176edba2014-12-01 14:53:08 -0800251 Opts.NoExecStack = Args->hasArg(OPT_mno_exec_stack);
252 Opts.FatalWarnings = Args->hasArg(OPT_massembler_fatal_warnings);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000253
Dylan Noblesmith89ea4162011-12-26 19:29:47 +0000254 return Success;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000255}
256
257static formatted_raw_ostream *GetOutputStream(AssemblerInvocation &Opts,
David Blaikied6471f72011-09-25 23:23:43 +0000258 DiagnosticsEngine &Diags,
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000259 bool Binary) {
Daniel Dunbarc673af72010-05-20 18:15:20 +0000260 if (Opts.OutputPath.empty())
261 Opts.OutputPath = "-";
262
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000263 // Make sure that the Out file gets unlinked from the disk if we get a
264 // SIGINT.
265 if (Opts.OutputPath != "-")
Rafael Espindola6f2e23b2013-06-13 21:02:40 +0000266 sys::RemoveFileOnSignal(Opts.OutputPath);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000267
Stephen Hines176edba2014-12-01 14:53:08 -0800268 std::error_code EC;
269 raw_fd_ostream *Out = new raw_fd_ostream(
270 Opts.OutputPath, EC, (Binary ? sys::fs::F_None : sys::fs::F_Text));
271 if (EC) {
272 Diags.Report(diag::err_fe_unable_to_open_output) << Opts.OutputPath
273 << EC.message();
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700274 delete Out;
275 return nullptr;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000276 }
277
278 return new formatted_raw_ostream(*Out, formatted_raw_ostream::DELETE_STREAM);
279}
280
David Blaikied6471f72011-09-25 23:23:43 +0000281static bool ExecuteAssembler(AssemblerInvocation &Opts,
282 DiagnosticsEngine &Diags) {
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000283 // Get the target specific parser.
284 std::string Error;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700285 const Target *TheTarget = TargetRegistry::lookupTarget(Opts.Triple, Error);
286 if (!TheTarget)
287 return Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000288
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700289 ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
290 MemoryBuffer::getFileOrSTDIN(Opts.InputFile);
291
292 if (std::error_code EC = Buffer.getError()) {
293 Error = EC.message();
294 return Diags.Report(diag::err_fe_error_reading) << Opts.InputFile;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000295 }
296
297 SourceMgr SrcMgr;
298
299 // Tell SrcMgr about this buffer, which is what the parser will pick up.
Stephen Hines176edba2014-12-01 14:53:08 -0800300 SrcMgr.AddNewSourceBuffer(std::move(*Buffer), SMLoc());
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000301
302 // Record the location of the include directories so that the lexer can find
303 // it later.
304 SrcMgr.setIncludeDirs(Opts.IncludePaths);
305
Stephen Hines651f13c2014-04-23 16:59:28 -0700306 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(Opts.Triple));
Evan Cheng884744b2011-07-18 20:57:51 +0000307 assert(MRI && "Unable to create target register info!");
308
Stephen Hines651f13c2014-04-23 16:59:28 -0700309 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, Opts.Triple));
Rafael Espindola1fcf31e2013-05-13 01:24:18 +0000310 assert(MAI && "Unable to create target asm info!");
311
Stephen Hines651f13c2014-04-23 16:59:28 -0700312 // Ensure MCAsmInfo initialization occurs before any use, otherwise sections
313 // may be created with a combination of default and explicit settings.
314 if (Opts.CompressDebugSections)
315 MAI->setCompressDebugSections(true);
316
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000317 bool IsBinary = Opts.OutputType == AssemblerInvocation::FT_Obj;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700318 std::unique_ptr<formatted_raw_ostream> Out(
319 GetOutputStream(Opts, Diags, IsBinary));
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000320 if (!Out)
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700321 return true;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000322
Evan Cheng36fc3aa2011-07-20 06:22:27 +0000323 // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
324 // MCObjectFileInfo needs a MCContext reference in order to initialize itself.
Stephen Hines651f13c2014-04-23 16:59:28 -0700325 std::unique_ptr<MCObjectFileInfo> MOFI(new MCObjectFileInfo());
326
Bill Wendling4b7bae32013-06-18 07:22:05 +0000327 MCContext Ctx(MAI.get(), MRI.get(), MOFI.get(), &SrcMgr);
Evan Cheng36fc3aa2011-07-20 06:22:27 +0000328 // FIXME: Assembler behavior can change with -static.
Evan Cheng66488ed2011-07-20 19:53:19 +0000329 MOFI->InitMCObjectFileInfo(Opts.Triple,
330 Reloc::Default, CodeModel::Default, Ctx);
Daniel Dunbar402adc32011-03-28 22:49:24 +0000331 if (Opts.SaveTemporaryLabels)
332 Ctx.setAllowTemporaryLabels(false);
Kevin Enderby567003e2011-12-22 19:31:58 +0000333 if (Opts.GenDwarfForAssembly)
334 Ctx.setGenDwarfForAssembly(true);
335 if (!Opts.DwarfDebugFlags.empty())
336 Ctx.setDwarfDebugFlags(StringRef(Opts.DwarfDebugFlags));
Kevin Enderby02341792013-01-17 21:38:06 +0000337 if (!Opts.DwarfDebugProducer.empty())
338 Ctx.setDwarfDebugProducer(StringRef(Opts.DwarfDebugProducer));
Chandler Carruthd566df62012-12-17 21:40:04 +0000339 if (!Opts.DebugCompilationDir.empty())
340 Ctx.setCompilationDir(Opts.DebugCompilationDir);
Eric Christopher27e2b982012-12-18 00:31:10 +0000341 if (!Opts.MainFileName.empty())
342 Ctx.setMainFileName(StringRef(Opts.MainFileName));
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700343 Ctx.setDwarfVersion(Opts.DwarfVersion);
Rafael Espindola7872d482010-12-10 07:40:14 +0000344
Jim Grosbachfc308292012-02-10 20:37:10 +0000345 // Build up the feature string from the target feature list.
346 std::string FS;
347 if (!Opts.Features.empty()) {
348 FS = Opts.Features[0];
349 for (unsigned i = 1, e = Opts.Features.size(); i != e; ++i)
350 FS += "," + Opts.Features[i];
351 }
352
Stephen Hines651f13c2014-04-23 16:59:28 -0700353 std::unique_ptr<MCStreamer> Str;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000354
Stephen Hines651f13c2014-04-23 16:59:28 -0700355 std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
356 std::unique_ptr<MCSubtargetInfo> STI(
357 TheTarget->createMCSubtargetInfo(Opts.Triple, Opts.CPU, FS));
Evan Cheng74e13322011-07-11 04:24:19 +0000358
Rafael Espindola3176cca2011-01-23 17:58:26 +0000359 // FIXME: There is a bit of code duplication with addPassesToEmitFile.
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000360 if (Opts.OutputType == AssemblerInvocation::FT_Asm) {
361 MCInstPrinter *IP =
Bill Wendling99d43b42012-04-02 06:17:37 +0000362 TheTarget->createMCInstPrinter(Opts.OutputAsmVariant, *MAI, *MCII, *MRI,
363 *STI);
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700364 MCCodeEmitter *CE = nullptr;
365 MCAsmBackend *MAB = nullptr;
Daniel Dunbar66cdf262010-12-16 03:06:05 +0000366 if (Opts.ShowEncoding) {
Jim Grosbach92ecfe92012-05-15 17:36:07 +0000367 CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, *STI, Ctx);
Bill Wendlinge904acf2013-09-09 02:37:56 +0000368 MAB = TheTarget->createMCAsmBackend(*MRI, Opts.Triple, Opts.CPU);
Daniel Dunbar66cdf262010-12-16 03:06:05 +0000369 }
Rafael Espindola7872d482010-12-10 07:40:14 +0000370 Str.reset(TheTarget->createAsmStreamer(Ctx, *Out, /*asmverbose*/true,
Nick Lewyckyea523d72011-10-17 23:05:52 +0000371 /*useDwarfDirectory*/ true,
372 IP, CE, MAB,
Rafael Espindola7872d482010-12-10 07:40:14 +0000373 Opts.ShowInst));
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000374 } else if (Opts.OutputType == AssemblerInvocation::FT_Null) {
375 Str.reset(createNullStreamer(Ctx));
376 } else {
377 assert(Opts.OutputType == AssemblerInvocation::FT_Obj &&
378 "Invalid file type!");
Jim Grosbach92ecfe92012-05-15 17:36:07 +0000379 MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, *STI, Ctx);
Bill Wendlinge904acf2013-09-09 02:37:56 +0000380 MCAsmBackend *MAB = TheTarget->createMCAsmBackend(*MRI, Opts.Triple,
381 Opts.CPU);
Evan Cheng7b6def72011-07-26 00:42:40 +0000382 Str.reset(TheTarget->createMCObjectStreamer(Opts.Triple, Ctx, *MAB, *Out,
Stephen Hines176edba2014-12-01 14:53:08 -0800383 CE, *STI, Opts.RelaxAll));
384 Str.get()->InitSections(Opts.NoExecStack);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000385 }
386
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700387 bool Failed = false;
388
Stephen Hines651f13c2014-04-23 16:59:28 -0700389 std::unique_ptr<MCAsmParser> Parser(
390 createMCAsmParser(SrcMgr, Ctx, *Str.get(), *MAI));
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700391
392 // FIXME: init MCTargetOptions from sanitizer flags here.
393 MCTargetOptions Options;
Stephen Hines651f13c2014-04-23 16:59:28 -0700394 std::unique_ptr<MCTargetAsmParser> TAP(
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700395 TheTarget->createMCAsmParser(*STI, *Parser, *MCII, Options));
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700396 if (!TAP)
397 Failed = Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
398
399 if (!Failed) {
400 Parser->setTargetParser(*TAP.get());
401 Failed = Parser->Run(Opts.NoInitialTextSection);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000402 }
403
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700404 // Close the output stream early.
405 Out.reset();
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000406
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700407 // Delete output file if there were errors.
408 if (Failed && Opts.OutputPath != "-")
Rafael Espindola2669e962013-06-26 12:48:34 +0000409 sys::fs::remove(Opts.OutputPath);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000410
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700411 return Failed;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000412}
413
Chad Rosier90836282013-03-27 18:28:23 +0000414static void LLVMErrorHandler(void *UserData, const std::string &Message,
415 bool GenCrashDiag) {
David Blaikied6471f72011-09-25 23:23:43 +0000416 DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000417
418 Diags.Report(diag::err_fe_error_backend) << Message;
419
420 // We cannot recover from llvm errors.
421 exit(1);
422}
423
Stephen Hines176edba2014-12-01 14:53:08 -0800424int cc1as_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000425 // Print a stack trace if we signal out.
426 sys::PrintStackTraceOnErrorSignal();
Stephen Hines176edba2014-12-01 14:53:08 -0800427 PrettyStackTraceProgram X(Argv.size(), Argv.data());
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000428 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
429
430 // Initialize targets and assembly printers/parsers.
431 InitializeAllTargetInfos();
Evan Chengd99d3e12011-07-22 21:59:11 +0000432 InitializeAllTargetMCs();
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000433 InitializeAllAsmParsers();
434
435 // Construct our diagnostic client.
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000436 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
Douglas Gregorbdbb0042010-08-18 22:29:43 +0000437 TextDiagnosticPrinter *DiagClient
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000438 = new TextDiagnosticPrinter(errs(), &*DiagOpts);
Douglas Gregorbdbb0042010-08-18 22:29:43 +0000439 DiagClient->setPrefix("clang -cc1as");
Dylan Noblesmithc93dc782012-02-20 14:00:23 +0000440 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000441 DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagClient);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000442
443 // Set an error handler, so that any LLVM backend diagnostics go through our
444 // error handler.
Dan Gohman726578c2010-08-18 21:23:17 +0000445 ScopedFatalErrorHandler FatalErrorHandler
446 (LLVMErrorHandler, static_cast<void*>(&Diags));
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000447
448 // Parse the arguments.
449 AssemblerInvocation Asm;
Stephen Hines176edba2014-12-01 14:53:08 -0800450 if (!AssemblerInvocation::CreateFromArgs(Asm, Argv, Diags))
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000451 return 1;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000452
Daniel Dunbarc673af72010-05-20 18:15:20 +0000453 if (Asm.ShowHelp) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700454 std::unique_ptr<OptTable> Opts(driver::createDriverOptTable());
455 Opts->PrintHelp(llvm::outs(), "clang -cc1as", "Clang Integrated Assembler",
456 /*Include=*/driver::options::CC1AsOption, /*Exclude=*/0);
Daniel Dunbarc673af72010-05-20 18:15:20 +0000457 return 0;
458 }
459
460 // Honor -version.
461 //
462 // FIXME: Use a better -version message?
463 if (Asm.ShowVersion) {
464 llvm::cl::PrintVersionMessage();
465 return 0;
466 }
467
468 // Honor -mllvm.
469 //
470 // FIXME: Remove this, one day.
471 if (!Asm.LLVMArgs.empty()) {
472 unsigned NumArgs = Asm.LLVMArgs.size();
473 const char **Args = new const char*[NumArgs + 2];
474 Args[0] = "clang (LLVM option parsing)";
475 for (unsigned i = 0; i != NumArgs; ++i)
476 Args[i + 1] = Asm.LLVMArgs[i].c_str();
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700477 Args[NumArgs + 1] = nullptr;
David Blaikie6bd17d22012-02-07 19:36:38 +0000478 llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args);
Daniel Dunbarc673af72010-05-20 18:15:20 +0000479 }
480
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000481 // Execute the invocation, unless there were parsing errors.
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700482 bool Failed = Diags.hasErrorOccurred() || ExecuteAssembler(Asm, Diags);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000483
484 // If any timers were active but haven't been destroyed yet, print their
485 // results now.
486 TimerGroup::printAll(errs());
487
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700488 return !!Failed;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000489}
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700490