blob: 4259f2985fbe8a7ec312a94cd53cc94d884b4f8c [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"
Rafael Espindola4dedcd72015-04-09 21:06:11 +000022#include "llvm/ADT/STLExtras.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"
Evgeniy Stepanoveeb820f2014-04-23 11:15:49 +000037#include "llvm/MC/MCTargetOptions.h"
Reid Kleckner898229a2013-06-14 17:17:23 +000038#include "llvm/Option/Arg.h"
39#include "llvm/Option/ArgList.h"
40#include "llvm/Option/OptTable.h"
Daniel Dunbar0454f652010-05-20 18:15:20 +000041#include "llvm/Support/CommandLine.h"
Daniel Dunbar2fcaa542010-05-20 17:49:16 +000042#include "llvm/Support/ErrorHandling.h"
Rafael Espindolaccc6ea62013-06-26 12:48:34 +000043#include "llvm/Support/FileSystem.h"
Chandler Carruthcc0694c2012-12-04 09:25:21 +000044#include "llvm/Support/FormattedStream.h"
45#include "llvm/Support/Host.h"
Daniel Dunbar2fcaa542010-05-20 17:49:16 +000046#include "llvm/Support/ManagedStatic.h"
47#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000048#include "llvm/Support/Path.h"
Chandler Carruthcc0694c2012-12-04 09:25:21 +000049#include "llvm/Support/PrettyStackTrace.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000050#include "llvm/Support/Signals.h"
Chandler Carruthcc0694c2012-12-04 09:25:21 +000051#include "llvm/Support/SourceMgr.h"
Evan Cheng494eb062011-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"
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000056#include <memory>
Rafael Espindola8a8e5542014-06-12 17:19:42 +000057#include <system_error>
Daniel Dunbar2fcaa542010-05-20 17:49:16 +000058using namespace clang;
59using namespace clang::driver;
Alp Toker61dad752014-07-09 14:05:11 +000060using namespace clang::driver::options;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +000061using namespace llvm;
Reid Kleckner898229a2013-06-14 17:17:23 +000062using namespace llvm::opt;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +000063
64namespace {
65
66/// \brief Helper class for representing a single invocation of the assembler.
67struct AssemblerInvocation {
68 /// @name Target Options
69 /// @{
70
Jim Grosbach576452b2012-02-10 20:37:10 +000071 /// The name of the target triple to assemble for.
Daniel Dunbar2fcaa542010-05-20 17:49:16 +000072 std::string Triple;
73
Jim Grosbach576452b2012-02-10 20:37:10 +000074 /// If given, the name of the target CPU to determine which instructions
75 /// are legal.
76 std::string CPU;
77
78 /// The list of target specific features to enable or disable -- this should
79 /// be a list of strings starting with '+' or '-'.
80 std::vector<std::string> Features;
81
Daniel Dunbar2fcaa542010-05-20 17:49:16 +000082 /// @}
83 /// @name Language Options
84 /// @{
85
86 std::vector<std::string> IncludePaths;
87 unsigned NoInitialTextSection : 1;
Daniel Dunbar9cf7bc72011-03-28 22:49:24 +000088 unsigned SaveTemporaryLabels : 1;
Kevin Enderby292dc082011-12-22 19:31:58 +000089 unsigned GenDwarfForAssembly : 1;
David Blaikie7e2fd942014-03-27 20:47:30 +000090 unsigned CompressDebugSections : 1;
Oliver Stannard9b2a7d42014-05-19 13:39:13 +000091 unsigned DwarfVersion;
Kevin Enderby292dc082011-12-22 19:31:58 +000092 std::string DwarfDebugFlags;
Kevin Enderbyae2ec472013-01-17 21:38:06 +000093 std::string DwarfDebugProducer;
Chandler Carruth4d5e1a92012-12-17 21:40:04 +000094 std::string DebugCompilationDir;
Eric Christopher45f2e712012-12-18 00:31:10 +000095 std::string MainFileName;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +000096
97 /// @}
98 /// @name Frontend Options
99 /// @{
100
101 std::string InputFile;
Daniel Dunbar0454f652010-05-20 18:15:20 +0000102 std::vector<std::string> LLVMArgs;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000103 std::string OutputPath;
104 enum FileType {
105 FT_Asm, ///< Assembly (.s) output, transliterate mode.
106 FT_Null, ///< No output, for timing purposes.
107 FT_Obj ///< Object file output.
108 };
109 FileType OutputType;
Daniel Dunbar0454f652010-05-20 18:15:20 +0000110 unsigned ShowHelp : 1;
111 unsigned ShowVersion : 1;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000112
113 /// @}
114 /// @name Transliterate Options
115 /// @{
116
117 unsigned OutputAsmVariant;
118 unsigned ShowEncoding : 1;
119 unsigned ShowInst : 1;
120
121 /// @}
122 /// @name Assembler Options
123 /// @{
124
125 unsigned RelaxAll : 1;
Rafael Espindola148141c2011-01-23 17:58:26 +0000126 unsigned NoExecStack : 1;
Joerg Sonnenbergera43604a2014-08-26 18:40:25 +0000127 unsigned FatalWarnings : 1;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000128
Joerg Sonnenberger924f6ad2015-09-18 11:13:43 +0000129 /// The name of the relocation model to use.
130 std::string RelocationModel;
131
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000132 /// @}
133
134public:
135 AssemblerInvocation() {
136 Triple = "";
137 NoInitialTextSection = 0;
138 InputFile = "-";
Daniel Dunbar0454f652010-05-20 18:15:20 +0000139 OutputPath = "-";
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000140 OutputType = FT_Asm;
141 OutputAsmVariant = 0;
142 ShowInst = 0;
143 ShowEncoding = 0;
144 RelaxAll = 0;
Rafael Espindola148141c2011-01-23 17:58:26 +0000145 NoExecStack = 0;
Joerg Sonnenbergera43604a2014-08-26 18:40:25 +0000146 FatalWarnings = 0;
Oliver Stannard9b2a7d42014-05-19 13:39:13 +0000147 DwarfVersion = 3;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000148 }
149
Sean Silva070cd2d2014-08-15 21:38:36 +0000150 static bool CreateFromArgs(AssemblerInvocation &Res,
151 ArrayRef<const char *> Argv,
152 DiagnosticsEngine &Diags);
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000153};
154
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000155}
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000156
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000157bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,
Sean Silva070cd2d2014-08-15 21:38:36 +0000158 ArrayRef<const char *> Argv,
David Blaikie9c902b52011-09-25 23:23:43 +0000159 DiagnosticsEngine &Diags) {
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000160 bool Success = true;
161
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000162 // Parse the arguments.
Alp Toker61dad752014-07-09 14:05:11 +0000163 std::unique_ptr<OptTable> OptTbl(createDriverOptTable());
164
165 const unsigned IncludedFlagsBitmask = options::CC1AsOption;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000166 unsigned MissingArgIndex, MissingArgCount;
David Blaikie69a1d8c2015-06-22 22:07:27 +0000167 InputArgList Args = OptTbl->ParseArgs(Argv, MissingArgIndex, MissingArgCount,
168 IncludedFlagsBitmask);
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000169
170 // Check for missing argument error.
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000171 if (MissingArgCount) {
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000172 Diags.Report(diag::err_drv_missing_argument)
David Blaikie69a1d8c2015-06-22 22:07:27 +0000173 << Args.getArgString(MissingArgIndex) << MissingArgCount;
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000174 Success = false;
175 }
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000176
177 // Issue errors on unknown arguments.
David Blaikie69a1d8c2015-06-22 22:07:27 +0000178 for (const Arg *A : Args.filtered(OPT_UNKNOWN)) {
179 Diags.Report(diag::err_drv_unknown_argument) << A->getAsString(Args);
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000180 Success = false;
181 }
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000182
183 // Construct the invocation.
184
185 // Target Options
David Blaikie69a1d8c2015-06-22 22:07:27 +0000186 Opts.Triple = llvm::Triple::normalize(Args.getLastArgValue(OPT_triple));
187 Opts.CPU = Args.getLastArgValue(OPT_target_cpu);
188 Opts.Features = Args.getAllArgValues(OPT_target_feature);
Jim Grosbach576452b2012-02-10 20:37:10 +0000189
190 // Use the default target triple if unspecified.
191 if (Opts.Triple.empty())
192 Opts.Triple = llvm::sys::getDefaultTargetTriple();
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000193
194 // Language Options
David Blaikie69a1d8c2015-06-22 22:07:27 +0000195 Opts.IncludePaths = Args.getAllArgValues(OPT_I);
196 Opts.NoInitialTextSection = Args.hasArg(OPT_n);
197 Opts.SaveTemporaryLabels = Args.hasArg(OPT_msave_temp_labels);
198 Opts.GenDwarfForAssembly = Args.hasArg(OPT_g_Flag);
199 Opts.CompressDebugSections = Args.hasArg(OPT_compress_debug_sections);
200 if (Args.hasArg(OPT_gdwarf_2))
Oliver Stannard9b2a7d42014-05-19 13:39:13 +0000201 Opts.DwarfVersion = 2;
David Blaikie69a1d8c2015-06-22 22:07:27 +0000202 if (Args.hasArg(OPT_gdwarf_3))
Oliver Stannard9b2a7d42014-05-19 13:39:13 +0000203 Opts.DwarfVersion = 3;
David Blaikie69a1d8c2015-06-22 22:07:27 +0000204 if (Args.hasArg(OPT_gdwarf_4))
Oliver Stannard9b2a7d42014-05-19 13:39:13 +0000205 Opts.DwarfVersion = 4;
David Blaikie69a1d8c2015-06-22 22:07:27 +0000206 Opts.DwarfDebugFlags = Args.getLastArgValue(OPT_dwarf_debug_flags);
207 Opts.DwarfDebugProducer = Args.getLastArgValue(OPT_dwarf_debug_producer);
208 Opts.DebugCompilationDir = Args.getLastArgValue(OPT_fdebug_compilation_dir);
209 Opts.MainFileName = Args.getLastArgValue(OPT_main_file_name);
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000210
211 // Frontend Options
David Blaikie69a1d8c2015-06-22 22:07:27 +0000212 if (Args.hasArg(OPT_INPUT)) {
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000213 bool First = true;
David Blaikie69a1d8c2015-06-22 22:07:27 +0000214 for (arg_iterator it = Args.filtered_begin(OPT_INPUT),
215 ie = Args.filtered_end();
216 it != ie; ++it, First = false) {
Daniel Dunbara442fd52010-06-11 22:00:13 +0000217 const Arg *A = it;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000218 if (First)
Richard Smithbd55daf2012-11-01 04:30:05 +0000219 Opts.InputFile = A->getValue();
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000220 else {
David Blaikie69a1d8c2015-06-22 22:07:27 +0000221 Diags.Report(diag::err_drv_unknown_argument) << A->getAsString(Args);
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000222 Success = false;
223 }
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000224 }
225 }
David Blaikie69a1d8c2015-06-22 22:07:27 +0000226 Opts.LLVMArgs = Args.getAllArgValues(OPT_mllvm);
227 Opts.OutputPath = Args.getLastArgValue(OPT_o);
228 if (Arg *A = Args.getLastArg(OPT_filetype)) {
Richard Smithbd55daf2012-11-01 04:30:05 +0000229 StringRef Name = A->getValue();
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000230 unsigned OutputType = StringSwitch<unsigned>(Name)
231 .Case("asm", FT_Asm)
232 .Case("null", FT_Null)
233 .Case("obj", FT_Obj)
234 .Default(~0U);
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000235 if (OutputType == ~0U) {
David Blaikie69a1d8c2015-06-22 22:07:27 +0000236 Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Name;
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000237 Success = false;
238 } else
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000239 Opts.OutputType = FileType(OutputType);
240 }
David Blaikie69a1d8c2015-06-22 22:07:27 +0000241 Opts.ShowHelp = Args.hasArg(OPT_help);
242 Opts.ShowVersion = Args.hasArg(OPT_version);
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000243
244 // Transliterate Options
Reid Kleckner898229a2013-06-14 17:17:23 +0000245 Opts.OutputAsmVariant =
David Blaikie69a1d8c2015-06-22 22:07:27 +0000246 getLastArgIntValue(Args, OPT_output_asm_variant, 0, Diags);
247 Opts.ShowEncoding = Args.hasArg(OPT_show_encoding);
248 Opts.ShowInst = Args.hasArg(OPT_show_inst);
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000249
250 // Assemble Options
David Blaikie69a1d8c2015-06-22 22:07:27 +0000251 Opts.RelaxAll = Args.hasArg(OPT_mrelax_all);
252 Opts.NoExecStack = Args.hasArg(OPT_mno_exec_stack);
253 Opts.FatalWarnings = Args.hasArg(OPT_massembler_fatal_warnings);
Joerg Sonnenberger924f6ad2015-09-18 11:13:43 +0000254 Opts.RelocationModel = Args.getLastArgValue(OPT_mrelocation_model, "pic");
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000255
Dylan Noblesmith68207c02011-12-26 19:29:47 +0000256 return Success;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000257}
258
Rafael Espindola7e556ad2015-04-09 21:50:11 +0000259static std::unique_ptr<raw_fd_ostream>
260getOutputStream(AssemblerInvocation &Opts, DiagnosticsEngine &Diags,
261 bool Binary) {
Daniel Dunbar0454f652010-05-20 18:15:20 +0000262 if (Opts.OutputPath.empty())
263 Opts.OutputPath = "-";
264
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000265 // Make sure that the Out file gets unlinked from the disk if we get a
266 // SIGINT.
267 if (Opts.OutputPath != "-")
Rafael Espindola18556de2013-06-13 21:02:40 +0000268 sys::RemoveFileOnSignal(Opts.OutputPath);
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000269
Rafael Espindoladae941a2014-08-25 18:17:04 +0000270 std::error_code EC;
Rafael Espindola7e556ad2015-04-09 21:50:11 +0000271 auto Out = llvm::make_unique<raw_fd_ostream>(
Rafael Espindoladae941a2014-08-25 18:17:04 +0000272 Opts.OutputPath, EC, (Binary ? sys::fs::F_None : sys::fs::F_Text));
273 if (EC) {
274 Diags.Report(diag::err_fe_unable_to_open_output) << Opts.OutputPath
275 << EC.message();
Craig Topper69186e72014-06-08 08:38:04 +0000276 return nullptr;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000277 }
278
Rafael Espindola4dedcd72015-04-09 21:06:11 +0000279 return Out;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000280}
281
David Blaikie9c902b52011-09-25 23:23:43 +0000282static bool ExecuteAssembler(AssemblerInvocation &Opts,
283 DiagnosticsEngine &Diags) {
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000284 // Get the target specific parser.
285 std::string Error;
Alp Tokere98ea7c2014-05-31 00:02:21 +0000286 const Target *TheTarget = TargetRegistry::lookupTarget(Opts.Triple, Error);
Alp Toker44293142014-05-31 00:02:27 +0000287 if (!TheTarget)
288 return Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000289
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000290 ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
291 MemoryBuffer::getFileOrSTDIN(Opts.InputFile);
292
293 if (std::error_code EC = Buffer.getError()) {
294 Error = EC.message();
Alp Toker44293142014-05-31 00:02:27 +0000295 return Diags.Report(diag::err_fe_error_reading) << Opts.InputFile;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000296 }
297
298 SourceMgr SrcMgr;
299
300 // Tell SrcMgr about this buffer, which is what the parser will pick up.
David Blaikie9e095d92014-08-21 21:01:00 +0000301 SrcMgr.AddNewSourceBuffer(std::move(*Buffer), SMLoc());
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000302
303 // Record the location of the include directories so that the lexer can find
304 // it later.
305 SrcMgr.setIncludeDirs(Opts.IncludePaths);
306
Ahmed Charlesb8984322014-03-07 20:03:18 +0000307 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(Opts.Triple));
Evan Chengb505ace2011-07-18 20:57:51 +0000308 assert(MRI && "Unable to create target register info!");
309
Ahmed Charlesb8984322014-03-07 20:03:18 +0000310 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, Opts.Triple));
Rafael Espindola77056232013-05-13 01:24:18 +0000311 assert(MAI && "Unable to create target asm info!");
312
David Blaikieb31e1d12014-03-31 23:47:13 +0000313 // Ensure MCAsmInfo initialization occurs before any use, otherwise sections
314 // may be created with a combination of default and explicit settings.
315 if (Opts.CompressDebugSections)
316 MAI->setCompressDebugSections(true);
317
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000318 bool IsBinary = Opts.OutputType == AssemblerInvocation::FT_Obj;
Rafael Espindola2f16bc12015-04-14 15:15:49 +0000319 std::unique_ptr<raw_fd_ostream> FDOS = getOutputStream(Opts, Diags, IsBinary);
320 if (!FDOS)
Alp Toker44293142014-05-31 00:02:27 +0000321 return true;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000322
Evan Cheng347033f2011-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.
Ahmed Charlesb8984322014-03-07 20:03:18 +0000325 std::unique_ptr<MCObjectFileInfo> MOFI(new MCObjectFileInfo());
David Blaikie07b8d4e2014-03-31 23:13:30 +0000326
Bill Wendlingda1e3e72013-06-18 07:22:05 +0000327 MCContext Ctx(MAI.get(), MRI.get(), MOFI.get(), &SrcMgr);
Joerg Sonnenberger924f6ad2015-09-18 11:13:43 +0000328
329 llvm::Reloc::Model RM = llvm::Reloc::Default;
330 if (Opts.RelocationModel == "static") {
331 RM = llvm::Reloc::Static;
332 } else if (Opts.RelocationModel == "pic") {
333 RM = llvm::Reloc::PIC_;
334 } else {
335 assert(Opts.RelocationModel == "dynamic-no-pic" &&
336 "Invalid PIC model!");
337 RM = llvm::Reloc::DynamicNoPIC;
338 }
339
340 MOFI->InitMCObjectFileInfo(Triple(Opts.Triple), RM,
Daniel Sanders8d8b13d2015-06-16 12:18:07 +0000341 CodeModel::Default, Ctx);
Daniel Dunbar9cf7bc72011-03-28 22:49:24 +0000342 if (Opts.SaveTemporaryLabels)
343 Ctx.setAllowTemporaryLabels(false);
Kevin Enderby292dc082011-12-22 19:31:58 +0000344 if (Opts.GenDwarfForAssembly)
345 Ctx.setGenDwarfForAssembly(true);
346 if (!Opts.DwarfDebugFlags.empty())
347 Ctx.setDwarfDebugFlags(StringRef(Opts.DwarfDebugFlags));
Kevin Enderbyae2ec472013-01-17 21:38:06 +0000348 if (!Opts.DwarfDebugProducer.empty())
349 Ctx.setDwarfDebugProducer(StringRef(Opts.DwarfDebugProducer));
Chandler Carruth4d5e1a92012-12-17 21:40:04 +0000350 if (!Opts.DebugCompilationDir.empty())
351 Ctx.setCompilationDir(Opts.DebugCompilationDir);
Eric Christopher45f2e712012-12-18 00:31:10 +0000352 if (!Opts.MainFileName.empty())
353 Ctx.setMainFileName(StringRef(Opts.MainFileName));
Oliver Stannard9b2a7d42014-05-19 13:39:13 +0000354 Ctx.setDwarfVersion(Opts.DwarfVersion);
Rafael Espindola86f2c572010-12-10 07:40:14 +0000355
Jim Grosbach576452b2012-02-10 20:37:10 +0000356 // Build up the feature string from the target feature list.
357 std::string FS;
358 if (!Opts.Features.empty()) {
359 FS = Opts.Features[0];
360 for (unsigned i = 1, e = Opts.Features.size(); i != e; ++i)
361 FS += "," + Opts.Features[i];
362 }
363
Ahmed Charlesb8984322014-03-07 20:03:18 +0000364 std::unique_ptr<MCStreamer> Str;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000365
Ahmed Charlesb8984322014-03-07 20:03:18 +0000366 std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
367 std::unique_ptr<MCSubtargetInfo> STI(
368 TheTarget->createMCSubtargetInfo(Opts.Triple, Opts.CPU, FS));
Evan Cheng279f2902011-07-11 04:24:19 +0000369
Rafael Espindola2f16bc12015-04-14 15:15:49 +0000370 raw_pwrite_stream *Out = FDOS.get();
371 std::unique_ptr<buffer_ostream> BOS;
372
Rafael Espindola148141c2011-01-23 17:58:26 +0000373 // FIXME: There is a bit of code duplication with addPassesToEmitFile.
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000374 if (Opts.OutputType == AssemblerInvocation::FT_Asm) {
Eric Christopher7e0fadf2015-03-31 00:10:23 +0000375 MCInstPrinter *IP = TheTarget->createMCInstPrinter(
Daniel Sanders50f17232015-09-15 16:17:27 +0000376 llvm::Triple(Opts.Triple), Opts.OutputAsmVariant, *MAI, *MCII, *MRI);
Craig Topper69186e72014-06-08 08:38:04 +0000377 MCCodeEmitter *CE = nullptr;
378 MCAsmBackend *MAB = nullptr;
Daniel Dunbar8f514a82010-12-16 03:06:05 +0000379 if (Opts.ShowEncoding) {
Eric Christophere0646e62015-03-10 22:03:27 +0000380 CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx);
Bill Wendling52575382013-09-09 02:37:56 +0000381 MAB = TheTarget->createMCAsmBackend(*MRI, Opts.Triple, Opts.CPU);
Daniel Dunbar8f514a82010-12-16 03:06:05 +0000382 }
Rafael Espindola4dedcd72015-04-09 21:06:11 +0000383 auto FOut = llvm::make_unique<formatted_raw_ostream>(*Out);
384 Str.reset(TheTarget->createAsmStreamer(
385 Ctx, std::move(FOut), /*asmverbose*/ true,
386 /*useDwarfDirectory*/ true, IP, CE, MAB, Opts.ShowInst));
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000387 } else if (Opts.OutputType == AssemblerInvocation::FT_Null) {
388 Str.reset(createNullStreamer(Ctx));
389 } else {
390 assert(Opts.OutputType == AssemblerInvocation::FT_Obj &&
391 "Invalid file type!");
Rafael Espindola2f16bc12015-04-14 15:15:49 +0000392 if (!FDOS->supportsSeeking()) {
393 BOS = make_unique<buffer_ostream>(*FDOS);
394 Out = BOS.get();
395 }
396
Eric Christophere0646e62015-03-10 22:03:27 +0000397 MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx);
Bill Wendling52575382013-09-09 02:37:56 +0000398 MCAsmBackend *MAB = TheTarget->createMCAsmBackend(*MRI, Opts.Triple,
399 Opts.CPU);
Daniel Sanders50f17232015-09-15 16:17:27 +0000400 Triple T(Opts.Triple);
401 Str.reset(TheTarget->createMCObjectStreamer(T, Ctx, *MAB, *Out, CE, *STI,
402 Opts.RelaxAll,
403 /*DWARFMustBeAtTheEnd*/ true));
Rafael Espindolad465be82014-10-15 16:12:57 +0000404 Str.get()->InitSections(Opts.NoExecStack);
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000405 }
406
Alp Toker44293142014-05-31 00:02:27 +0000407 bool Failed = false;
Alp Tokere98ea7c2014-05-31 00:02:21 +0000408
Ahmed Charlesb8984322014-03-07 20:03:18 +0000409 std::unique_ptr<MCAsmParser> Parser(
410 createMCAsmParser(SrcMgr, Ctx, *Str.get(), *MAI));
Evgeniy Stepanoveeb820f2014-04-23 11:15:49 +0000411
412 // FIXME: init MCTargetOptions from sanitizer flags here.
413 MCTargetOptions Options;
Ahmed Charlesb8984322014-03-07 20:03:18 +0000414 std::unique_ptr<MCTargetAsmParser> TAP(
Evgeniy Stepanoveeb820f2014-04-23 11:15:49 +0000415 TheTarget->createMCAsmParser(*STI, *Parser, *MCII, Options));
Alp Toker44293142014-05-31 00:02:27 +0000416 if (!TAP)
417 Failed = Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000418
Alp Toker44293142014-05-31 00:02:27 +0000419 if (!Failed) {
Alp Tokere98ea7c2014-05-31 00:02:21 +0000420 Parser->setTargetParser(*TAP.get());
Alp Toker44293142014-05-31 00:02:27 +0000421 Failed = Parser->Run(Opts.NoInitialTextSection);
Alp Tokere98ea7c2014-05-31 00:02:21 +0000422 }
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000423
Steven Wuae480ec2015-07-24 02:12:43 +0000424 // Close Streamer first.
425 // It might have a reference to the output stream.
426 Str.reset();
Alp Tokere98ea7c2014-05-31 00:02:21 +0000427 // Close the output stream early.
Rafael Espindola2f16bc12015-04-14 15:15:49 +0000428 BOS.reset();
429 FDOS.reset();
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000430
Alp Tokere98ea7c2014-05-31 00:02:21 +0000431 // Delete output file if there were errors.
Alp Toker44293142014-05-31 00:02:27 +0000432 if (Failed && Opts.OutputPath != "-")
Rafael Espindolaccc6ea62013-06-26 12:48:34 +0000433 sys::fs::remove(Opts.OutputPath);
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000434
Alp Toker44293142014-05-31 00:02:27 +0000435 return Failed;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000436}
437
Chad Rosier05c71aa2013-03-27 18:28:23 +0000438static void LLVMErrorHandler(void *UserData, const std::string &Message,
439 bool GenCrashDiag) {
David Blaikie9c902b52011-09-25 23:23:43 +0000440 DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000441
442 Diags.Report(diag::err_fe_error_backend) << Message;
443
444 // We cannot recover from llvm errors.
445 exit(1);
446}
447
Sean Silva070cd2d2014-08-15 21:38:36 +0000448int cc1as_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000449 // Print a stack trace if we signal out.
450 sys::PrintStackTraceOnErrorSignal();
Sean Silva070cd2d2014-08-15 21:38:36 +0000451 PrettyStackTraceProgram X(Argv.size(), Argv.data());
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000452 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
453
454 // Initialize targets and assembly printers/parsers.
455 InitializeAllTargetInfos();
Evan Chengc391a582011-07-22 21:59:11 +0000456 InitializeAllTargetMCs();
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000457 InitializeAllAsmParsers();
458
459 // Construct our diagnostic client.
Douglas Gregor811db4e2012-10-23 22:26:28 +0000460 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
Douglas Gregor2dd19f12010-08-18 22:29:43 +0000461 TextDiagnosticPrinter *DiagClient
Douglas Gregor811db4e2012-10-23 22:26:28 +0000462 = new TextDiagnosticPrinter(errs(), &*DiagOpts);
Douglas Gregor2dd19f12010-08-18 22:29:43 +0000463 DiagClient->setPrefix("clang -cc1as");
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000464 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
Douglas Gregor811db4e2012-10-23 22:26:28 +0000465 DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagClient);
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000466
467 // Set an error handler, so that any LLVM backend diagnostics go through our
468 // error handler.
Dan Gohmanb37af7d2010-08-18 21:23:17 +0000469 ScopedFatalErrorHandler FatalErrorHandler
470 (LLVMErrorHandler, static_cast<void*>(&Diags));
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000471
472 // Parse the arguments.
473 AssemblerInvocation Asm;
Sean Silva070cd2d2014-08-15 21:38:36 +0000474 if (!AssemblerInvocation::CreateFromArgs(Asm, Argv, Diags))
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000475 return 1;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000476
Daniel Dunbar0454f652010-05-20 18:15:20 +0000477 if (Asm.ShowHelp) {
Alp Toker61dad752014-07-09 14:05:11 +0000478 std::unique_ptr<OptTable> Opts(driver::createDriverOptTable());
Alp Toker532e5b92014-07-09 14:09:52 +0000479 Opts->PrintHelp(llvm::outs(), "clang -cc1as", "Clang Integrated Assembler",
480 /*Include=*/driver::options::CC1AsOption, /*Exclude=*/0);
Daniel Dunbar0454f652010-05-20 18:15:20 +0000481 return 0;
482 }
483
484 // Honor -version.
485 //
486 // FIXME: Use a better -version message?
487 if (Asm.ShowVersion) {
488 llvm::cl::PrintVersionMessage();
489 return 0;
490 }
491
492 // Honor -mllvm.
493 //
494 // FIXME: Remove this, one day.
495 if (!Asm.LLVMArgs.empty()) {
496 unsigned NumArgs = Asm.LLVMArgs.size();
497 const char **Args = new const char*[NumArgs + 2];
498 Args[0] = "clang (LLVM option parsing)";
499 for (unsigned i = 0; i != NumArgs; ++i)
500 Args[i + 1] = Asm.LLVMArgs[i].c_str();
Craig Topper69186e72014-06-08 08:38:04 +0000501 Args[NumArgs + 1] = nullptr;
David Blaikie09d20ee2012-02-07 19:36:38 +0000502 llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args);
Daniel Dunbar0454f652010-05-20 18:15:20 +0000503 }
504
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000505 // Execute the invocation, unless there were parsing errors.
Alp Toker44293142014-05-31 00:02:27 +0000506 bool Failed = Diags.hasErrorOccurred() || ExecuteAssembler(Asm, Diags);
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000507
508 // If any timers were active but haven't been destroyed yet, print their
509 // results now.
510 TimerGroup::printAll(errs());
511
Alp Toker44293142014-05-31 00:02:27 +0000512 return !!Failed;
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000513}
Alp Toker61dad752014-07-09 14:05:11 +0000514