blob: 0d06371dc16d2a55f42eeba5187a013376d3d8ff [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"
Daniel Dunbar2fcaa542010-05-20 17:49:16 +000017#include "clang/Driver/CC1AsOptions.h"
Chandler Carruthcc0694c2012-12-04 09:25:21 +000018#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar2fcaa542010-05-20 17:49:16 +000019#include "clang/Driver/Options.h"
Daniel Dunbar2fcaa542010-05-20 17:49:16 +000020#include "clang/Frontend/FrontendDiagnostic.h"
21#include "clang/Frontend/TextDiagnosticPrinter.h"
Reid Kleckner898229a2013-06-14 17:17:23 +000022#include "clang/Frontend/Utils.h"
Daniel Dunbar2fcaa542010-05-20 17:49:16 +000023#include "llvm/ADT/StringSwitch.h"
Duncan Sandsf610b5b2010-08-30 09:42:39 +000024#include "llvm/ADT/Triple.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000025#include "llvm/IR/DataLayout.h"
Chandler Carruthcc0694c2012-12-04 09:25:21 +000026#include "llvm/MC/MCAsmBackend.h"
Daniel Dunbar05474282010-07-17 02:26:21 +000027#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbar2fcaa542010-05-20 17:49:16 +000028#include "llvm/MC/MCCodeEmitter.h"
29#include "llvm/MC/MCContext.h"
Evan Cheng279f2902011-07-11 04:24:19 +000030#include "llvm/MC/MCInstrInfo.h"
Evan Cheng347033f2011-07-20 06:22:27 +000031#include "llvm/MC/MCObjectFileInfo.h"
Chandler Carruthcc0694c2012-12-04 09:25:21 +000032#include "llvm/MC/MCParser/MCAsmParser.h"
Evan Chengb505ace2011-07-18 20:57:51 +000033#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbar2fcaa542010-05-20 17:49:16 +000034#include "llvm/MC/MCStreamer.h"
Evan Chengdec31872011-07-09 06:04:17 +000035#include "llvm/MC/MCSubtargetInfo.h"
Evan Cheng939f8092011-07-26 00:24:45 +000036#include "llvm/MC/MCTargetAsmParser.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"
Michael J. Spencerf25faaa2010-12-09 17:36:38 +000055#include "llvm/Support/system_error.h"
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000056#include <memory>
Daniel Dunbar2fcaa542010-05-20 17:49:16 +000057using namespace clang;
58using namespace clang::driver;
59using namespace llvm;
Reid Kleckner898229a2013-06-14 17:17:23 +000060using namespace llvm::opt;
Daniel Dunbar2fcaa542010-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 Grosbach576452b2012-02-10 20:37:10 +000069 /// The name of the target triple to assemble for.
Daniel Dunbar2fcaa542010-05-20 17:49:16 +000070 std::string Triple;
71
Jim Grosbach576452b2012-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 Dunbar2fcaa542010-05-20 17:49:16 +000080 /// @}
81 /// @name Language Options
82 /// @{
83
84 std::vector<std::string> IncludePaths;
85 unsigned NoInitialTextSection : 1;
Daniel Dunbar9cf7bc72011-03-28 22:49:24 +000086 unsigned SaveTemporaryLabels : 1;
Kevin Enderby292dc082011-12-22 19:31:58 +000087 unsigned GenDwarfForAssembly : 1;
David Blaikie7e2fd942014-03-27 20:47:30 +000088 unsigned CompressDebugSections : 1;
Kevin Enderby292dc082011-12-22 19:31:58 +000089 std::string DwarfDebugFlags;
Kevin Enderbyae2ec472013-01-17 21:38:06 +000090 std::string DwarfDebugProducer;
Chandler Carruth4d5e1a92012-12-17 21:40:04 +000091 std::string DebugCompilationDir;
Eric Christopher45f2e712012-12-18 00:31:10 +000092 std::string MainFileName;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +000093
94 /// @}
95 /// @name Frontend Options
96 /// @{
97
98 std::string InputFile;
Daniel Dunbar0454f652010-05-20 18:15:20 +000099 std::vector<std::string> LLVMArgs;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000100 std::string OutputPath;
101 enum FileType {
102 FT_Asm, ///< Assembly (.s) output, transliterate mode.
103 FT_Null, ///< No output, for timing purposes.
104 FT_Obj ///< Object file output.
105 };
106 FileType OutputType;
Daniel Dunbar0454f652010-05-20 18:15:20 +0000107 unsigned ShowHelp : 1;
108 unsigned ShowVersion : 1;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000109
110 /// @}
111 /// @name Transliterate Options
112 /// @{
113
114 unsigned OutputAsmVariant;
115 unsigned ShowEncoding : 1;
116 unsigned ShowInst : 1;
117
118 /// @}
119 /// @name Assembler Options
120 /// @{
121
122 unsigned RelaxAll : 1;
Rafael Espindola148141c2011-01-23 17:58:26 +0000123 unsigned NoExecStack : 1;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000124
125 /// @}
126
127public:
128 AssemblerInvocation() {
129 Triple = "";
130 NoInitialTextSection = 0;
131 InputFile = "-";
Daniel Dunbar0454f652010-05-20 18:15:20 +0000132 OutputPath = "-";
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000133 OutputType = FT_Asm;
134 OutputAsmVariant = 0;
135 ShowInst = 0;
136 ShowEncoding = 0;
137 RelaxAll = 0;
Rafael Espindola148141c2011-01-23 17:58:26 +0000138 NoExecStack = 0;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000139 }
140
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000141 static bool CreateFromArgs(AssemblerInvocation &Res, const char **ArgBegin,
David Blaikie9c902b52011-09-25 23:23:43 +0000142 const char **ArgEnd, DiagnosticsEngine &Diags);
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000143};
144
145}
146
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000147bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000148 const char **ArgBegin,
149 const char **ArgEnd,
David Blaikie9c902b52011-09-25 23:23:43 +0000150 DiagnosticsEngine &Diags) {
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000151 using namespace clang::driver::cc1asoptions;
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000152 bool Success = true;
153
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000154 // Parse the arguments.
Ahmed Charlesb8984322014-03-07 20:03:18 +0000155 std::unique_ptr<OptTable> OptTbl(createCC1AsOptTable());
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000156 unsigned MissingArgIndex, MissingArgCount;
Ahmed Charlesb8984322014-03-07 20:03:18 +0000157 std::unique_ptr<InputArgList> Args(
158 OptTbl->ParseArgs(ArgBegin, ArgEnd, MissingArgIndex, MissingArgCount));
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000159
160 // Check for missing argument error.
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000161 if (MissingArgCount) {
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000162 Diags.Report(diag::err_drv_missing_argument)
163 << Args->getArgString(MissingArgIndex) << MissingArgCount;
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000164 Success = false;
165 }
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000166
167 // Issue errors on unknown arguments.
168 for (arg_iterator it = Args->filtered_begin(cc1asoptions::OPT_UNKNOWN),
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000169 ie = Args->filtered_end(); it != ie; ++it) {
Daniel Dunbara442fd52010-06-11 22:00:13 +0000170 Diags.Report(diag::err_drv_unknown_argument) << (*it) ->getAsString(*Args);
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000171 Success = false;
172 }
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000173
174 // Construct the invocation.
175
176 // Target Options
Jim Grosbach576452b2012-02-10 20:37:10 +0000177 Opts.Triple = llvm::Triple::normalize(Args->getLastArgValue(OPT_triple));
178 Opts.CPU = Args->getLastArgValue(OPT_target_cpu);
179 Opts.Features = Args->getAllArgValues(OPT_target_feature);
180
181 // Use the default target triple if unspecified.
182 if (Opts.Triple.empty())
183 Opts.Triple = llvm::sys::getDefaultTargetTriple();
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000184
185 // Language Options
186 Opts.IncludePaths = Args->getAllArgValues(OPT_I);
187 Opts.NoInitialTextSection = Args->hasArg(OPT_n);
Bob Wilsona1b42062013-09-26 21:00:51 +0000188 Opts.SaveTemporaryLabels = Args->hasArg(OPT_msave_temp_labels);
Kevin Enderby292dc082011-12-22 19:31:58 +0000189 Opts.GenDwarfForAssembly = Args->hasArg(OPT_g);
David Blaikie7e2fd942014-03-27 20:47:30 +0000190 Opts.CompressDebugSections = Args->hasArg(OPT_compress_debug_sections);
Kevin Enderby292dc082011-12-22 19:31:58 +0000191 Opts.DwarfDebugFlags = Args->getLastArgValue(OPT_dwarf_debug_flags);
Kevin Enderbyae2ec472013-01-17 21:38:06 +0000192 Opts.DwarfDebugProducer = Args->getLastArgValue(OPT_dwarf_debug_producer);
Chandler Carruth4d5e1a92012-12-17 21:40:04 +0000193 Opts.DebugCompilationDir = Args->getLastArgValue(OPT_fdebug_compilation_dir);
Eric Christopher45f2e712012-12-18 00:31:10 +0000194 Opts.MainFileName = Args->getLastArgValue(OPT_main_file_name);
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000195
196 // Frontend Options
197 if (Args->hasArg(OPT_INPUT)) {
198 bool First = true;
199 for (arg_iterator it = Args->filtered_begin(OPT_INPUT),
200 ie = Args->filtered_end(); it != ie; ++it, First=false) {
Daniel Dunbara442fd52010-06-11 22:00:13 +0000201 const Arg *A = it;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000202 if (First)
Richard Smithbd55daf2012-11-01 04:30:05 +0000203 Opts.InputFile = A->getValue();
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000204 else {
Daniel Dunbara442fd52010-06-11 22:00:13 +0000205 Diags.Report(diag::err_drv_unknown_argument) << A->getAsString(*Args);
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000206 Success = false;
207 }
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000208 }
209 }
Daniel Dunbar0454f652010-05-20 18:15:20 +0000210 Opts.LLVMArgs = Args->getAllArgValues(OPT_mllvm);
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000211 Opts.OutputPath = Args->getLastArgValue(OPT_o);
212 if (Arg *A = Args->getLastArg(OPT_filetype)) {
Richard Smithbd55daf2012-11-01 04:30:05 +0000213 StringRef Name = A->getValue();
Daniel Dunbar2fcaa542010-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 Noblesmithe99b27f2011-12-23 03:05:38 +0000219 if (OutputType == ~0U) {
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000220 Diags.Report(diag::err_drv_invalid_value)
221 << A->getAsString(*Args) << Name;
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000222 Success = false;
223 } else
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000224 Opts.OutputType = FileType(OutputType);
225 }
Daniel Dunbar0454f652010-05-20 18:15:20 +0000226 Opts.ShowHelp = Args->hasArg(OPT_help);
227 Opts.ShowVersion = Args->hasArg(OPT_version);
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000228
229 // Transliterate Options
Reid Kleckner898229a2013-06-14 17:17:23 +0000230 Opts.OutputAsmVariant =
231 getLastArgIntValue(*Args.get(), OPT_output_asm_variant, 0, Diags);
Daniel Dunbar2fcaa542010-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
David Blaikie9260ed62013-07-25 21:19:01 +0000236 Opts.RelaxAll = Args->hasArg(OPT_mrelax_all);
237 Opts.NoExecStack = Args->hasArg(OPT_mno_exec_stack);
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000238
Dylan Noblesmith68207c02011-12-26 19:29:47 +0000239 return Success;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000240}
241
242static formatted_raw_ostream *GetOutputStream(AssemblerInvocation &Opts,
David Blaikie9c902b52011-09-25 23:23:43 +0000243 DiagnosticsEngine &Diags,
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000244 bool Binary) {
Daniel Dunbar0454f652010-05-20 18:15:20 +0000245 if (Opts.OutputPath.empty())
246 Opts.OutputPath = "-";
247
Daniel Dunbar2fcaa542010-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 Espindola18556de2013-06-13 21:02:40 +0000251 sys::RemoveFileOnSignal(Opts.OutputPath);
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000252
253 std::string Error;
254 raw_fd_ostream *Out =
Rafael Espindola16125fb2013-07-16 19:44:23 +0000255 new raw_fd_ostream(Opts.OutputPath.c_str(), Error,
Rafael Espindola4fbd3732014-02-24 18:20:21 +0000256 (Binary ? sys::fs::F_None : sys::fs::F_Text));
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000257 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 Blaikie9c902b52011-09-25 23:23:43 +0000266static bool ExecuteAssembler(AssemblerInvocation &Opts,
267 DiagnosticsEngine &Diags) {
Daniel Dunbar2fcaa542010-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
Ahmed Charlesb8984322014-03-07 20:03:18 +0000276 std::unique_ptr<MemoryBuffer> BufferPtr;
Michael J. Spencerd9da7a12010-12-16 03:28:14 +0000277 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Opts.InputFile, BufferPtr)) {
Michael J. Spencerf25faaa2010-12-09 17:36:38 +0000278 Error = ec.message();
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000279 Diags.Report(diag::err_fe_error_reading) << Opts.InputFile;
280 return false;
281 }
Ahmed Charles9a16beb2014-03-07 19:33:25 +0000282 MemoryBuffer *Buffer = BufferPtr.release();
Daniel Dunbar2fcaa542010-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
Ahmed Charlesb8984322014-03-07 20:03:18 +0000293 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(Opts.Triple));
Evan Chengb505ace2011-07-18 20:57:51 +0000294 assert(MRI && "Unable to create target register info!");
295
Ahmed Charlesb8984322014-03-07 20:03:18 +0000296 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, Opts.Triple));
Rafael Espindola77056232013-05-13 01:24:18 +0000297 assert(MAI && "Unable to create target asm info!");
298
Daniel Dunbar2fcaa542010-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 Cheng347033f2011-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.
Ahmed Charlesb8984322014-03-07 20:03:18 +0000306 std::unique_ptr<MCObjectFileInfo> MOFI(new MCObjectFileInfo());
David Blaikie07b8d4e2014-03-31 23:13:30 +0000307
308 if (Opts.CompressDebugSections)
309 MAI->setCompressDebugSections(true);
310
Bill Wendlingda1e3e72013-06-18 07:22:05 +0000311 MCContext Ctx(MAI.get(), MRI.get(), MOFI.get(), &SrcMgr);
Evan Cheng347033f2011-07-20 06:22:27 +0000312 // FIXME: Assembler behavior can change with -static.
Evan Chengb5517a42011-07-20 19:53:19 +0000313 MOFI->InitMCObjectFileInfo(Opts.Triple,
314 Reloc::Default, CodeModel::Default, Ctx);
Daniel Dunbar9cf7bc72011-03-28 22:49:24 +0000315 if (Opts.SaveTemporaryLabels)
316 Ctx.setAllowTemporaryLabels(false);
Kevin Enderby292dc082011-12-22 19:31:58 +0000317 if (Opts.GenDwarfForAssembly)
318 Ctx.setGenDwarfForAssembly(true);
319 if (!Opts.DwarfDebugFlags.empty())
320 Ctx.setDwarfDebugFlags(StringRef(Opts.DwarfDebugFlags));
Kevin Enderbyae2ec472013-01-17 21:38:06 +0000321 if (!Opts.DwarfDebugProducer.empty())
322 Ctx.setDwarfDebugProducer(StringRef(Opts.DwarfDebugProducer));
Chandler Carruth4d5e1a92012-12-17 21:40:04 +0000323 if (!Opts.DebugCompilationDir.empty())
324 Ctx.setCompilationDir(Opts.DebugCompilationDir);
Eric Christopher45f2e712012-12-18 00:31:10 +0000325 if (!Opts.MainFileName.empty())
326 Ctx.setMainFileName(StringRef(Opts.MainFileName));
Rafael Espindola86f2c572010-12-10 07:40:14 +0000327
Jim Grosbach576452b2012-02-10 20:37:10 +0000328 // Build up the feature string from the target feature list.
329 std::string FS;
330 if (!Opts.Features.empty()) {
331 FS = Opts.Features[0];
332 for (unsigned i = 1, e = Opts.Features.size(); i != e; ++i)
333 FS += "," + Opts.Features[i];
334 }
335
Ahmed Charlesb8984322014-03-07 20:03:18 +0000336 std::unique_ptr<MCStreamer> Str;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000337
Ahmed Charlesb8984322014-03-07 20:03:18 +0000338 std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
339 std::unique_ptr<MCSubtargetInfo> STI(
340 TheTarget->createMCSubtargetInfo(Opts.Triple, Opts.CPU, FS));
Evan Cheng279f2902011-07-11 04:24:19 +0000341
Rafael Espindola148141c2011-01-23 17:58:26 +0000342 // FIXME: There is a bit of code duplication with addPassesToEmitFile.
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000343 if (Opts.OutputType == AssemblerInvocation::FT_Asm) {
344 MCInstPrinter *IP =
Bill Wendlingf4ec97d2012-04-02 06:17:37 +0000345 TheTarget->createMCInstPrinter(Opts.OutputAsmVariant, *MAI, *MCII, *MRI,
346 *STI);
Benjamin Kramer4b5de0d2010-07-29 17:48:03 +0000347 MCCodeEmitter *CE = 0;
Evan Cheng4b899832011-07-25 23:25:09 +0000348 MCAsmBackend *MAB = 0;
Daniel Dunbar8f514a82010-12-16 03:06:05 +0000349 if (Opts.ShowEncoding) {
Jim Grosbach490950a2012-05-15 17:36:07 +0000350 CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, *STI, Ctx);
Bill Wendling52575382013-09-09 02:37:56 +0000351 MAB = TheTarget->createMCAsmBackend(*MRI, Opts.Triple, Opts.CPU);
Daniel Dunbar8f514a82010-12-16 03:06:05 +0000352 }
Rafael Espindola86f2c572010-12-10 07:40:14 +0000353 Str.reset(TheTarget->createAsmStreamer(Ctx, *Out, /*asmverbose*/true,
Nick Lewycky1d617ac2011-10-17 23:05:52 +0000354 /*useCFI*/ true,
355 /*useDwarfDirectory*/ true,
356 IP, CE, MAB,
Rafael Espindola86f2c572010-12-10 07:40:14 +0000357 Opts.ShowInst));
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000358 } else if (Opts.OutputType == AssemblerInvocation::FT_Null) {
359 Str.reset(createNullStreamer(Ctx));
360 } else {
361 assert(Opts.OutputType == AssemblerInvocation::FT_Obj &&
362 "Invalid file type!");
Jim Grosbach490950a2012-05-15 17:36:07 +0000363 MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, *STI, Ctx);
Bill Wendling52575382013-09-09 02:37:56 +0000364 MCAsmBackend *MAB = TheTarget->createMCAsmBackend(*MRI, Opts.Triple,
365 Opts.CPU);
Evan Cheng9568f272011-07-26 00:42:40 +0000366 Str.reset(TheTarget->createMCObjectStreamer(Opts.Triple, Ctx, *MAB, *Out,
Rafael Espindolad51906e2014-01-26 06:39:14 +0000367 CE, *STI, Opts.RelaxAll,
Evan Cheng9568f272011-07-26 00:42:40 +0000368 Opts.NoExecStack));
Rafael Espindola85111902010-10-13 14:53:57 +0000369 Str.get()->InitSections();
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000370 }
371
Ahmed Charlesb8984322014-03-07 20:03:18 +0000372 std::unique_ptr<MCAsmParser> Parser(
373 createMCAsmParser(SrcMgr, Ctx, *Str.get(), *MAI));
374 std::unique_ptr<MCTargetAsmParser> TAP(
375 TheTarget->createMCAsmParser(*STI, *Parser, *MCII));
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000376 if (!TAP) {
377 Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
378 return false;
379 }
380
Daniel Dunbar05474282010-07-17 02:26:21 +0000381 Parser->setTargetParser(*TAP.get());
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000382
Daniel Dunbar05474282010-07-17 02:26:21 +0000383 bool Success = !Parser->Run(Opts.NoInitialTextSection);
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000384
385 // Close the output.
386 delete Out;
387
388 // Delete output on errors.
389 if (!Success && Opts.OutputPath != "-")
Rafael Espindolaccc6ea62013-06-26 12:48:34 +0000390 sys::fs::remove(Opts.OutputPath);
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000391
392 return Success;
393}
394
Chad Rosier05c71aa2013-03-27 18:28:23 +0000395static void LLVMErrorHandler(void *UserData, const std::string &Message,
396 bool GenCrashDiag) {
David Blaikie9c902b52011-09-25 23:23:43 +0000397 DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000398
399 Diags.Report(diag::err_fe_error_backend) << Message;
400
401 // We cannot recover from llvm errors.
402 exit(1);
403}
404
405int cc1as_main(const char **ArgBegin, const char **ArgEnd,
406 const char *Argv0, void *MainAddr) {
407 // Print a stack trace if we signal out.
408 sys::PrintStackTraceOnErrorSignal();
409 PrettyStackTraceProgram X(ArgEnd - ArgBegin, ArgBegin);
410 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
411
412 // Initialize targets and assembly printers/parsers.
413 InitializeAllTargetInfos();
Evan Chengc391a582011-07-22 21:59:11 +0000414 InitializeAllTargetMCs();
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000415 InitializeAllAsmParsers();
416
417 // Construct our diagnostic client.
Douglas Gregor811db4e2012-10-23 22:26:28 +0000418 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
Douglas Gregor2dd19f12010-08-18 22:29:43 +0000419 TextDiagnosticPrinter *DiagClient
Douglas Gregor811db4e2012-10-23 22:26:28 +0000420 = new TextDiagnosticPrinter(errs(), &*DiagOpts);
Douglas Gregor2dd19f12010-08-18 22:29:43 +0000421 DiagClient->setPrefix("clang -cc1as");
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000422 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
Douglas Gregor811db4e2012-10-23 22:26:28 +0000423 DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagClient);
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000424
425 // Set an error handler, so that any LLVM backend diagnostics go through our
426 // error handler.
Dan Gohmanb37af7d2010-08-18 21:23:17 +0000427 ScopedFatalErrorHandler FatalErrorHandler
428 (LLVMErrorHandler, static_cast<void*>(&Diags));
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000429
430 // Parse the arguments.
431 AssemblerInvocation Asm;
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000432 if (!AssemblerInvocation::CreateFromArgs(Asm, ArgBegin, ArgEnd, Diags))
433 return 1;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000434
Daniel Dunbar0454f652010-05-20 18:15:20 +0000435 // Honor -help.
436 if (Asm.ShowHelp) {
Ahmed Charlesb8984322014-03-07 20:03:18 +0000437 std::unique_ptr<OptTable> Opts(driver::createCC1AsOptTable());
Daniel Dunbar0454f652010-05-20 18:15:20 +0000438 Opts->PrintHelp(llvm::outs(), "clang -cc1as", "Clang Integrated Assembler");
439 return 0;
440 }
441
442 // Honor -version.
443 //
444 // FIXME: Use a better -version message?
445 if (Asm.ShowVersion) {
446 llvm::cl::PrintVersionMessage();
447 return 0;
448 }
449
450 // Honor -mllvm.
451 //
452 // FIXME: Remove this, one day.
453 if (!Asm.LLVMArgs.empty()) {
454 unsigned NumArgs = Asm.LLVMArgs.size();
455 const char **Args = new const char*[NumArgs + 2];
456 Args[0] = "clang (LLVM option parsing)";
457 for (unsigned i = 0; i != NumArgs; ++i)
458 Args[i + 1] = Asm.LLVMArgs[i].c_str();
459 Args[NumArgs + 1] = 0;
David Blaikie09d20ee2012-02-07 19:36:38 +0000460 llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args);
Daniel Dunbar0454f652010-05-20 18:15:20 +0000461 }
462
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000463 // Execute the invocation, unless there were parsing errors.
464 bool Success = false;
Argyrios Kyrtzidis5c26cda22010-11-19 00:19:18 +0000465 if (!Diags.hasErrorOccurred())
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000466 Success = ExecuteAssembler(Asm, Diags);
467
468 // If any timers were active but haven't been destroyed yet, print their
469 // results now.
470 TimerGroup::printAll(errs());
471
472 return !Success;
473}