2010-05-21 01:49:16 +08:00
|
|
|
//===-- cc1as_main.cpp - Clang Assembler ---------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This is the entry point to the clang -cc1as functionality, which implements
|
|
|
|
// the direct interface to the LLVM MC based assembler.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2012-12-04 17:25:21 +08:00
|
|
|
#include "clang/Basic/DiagnosticOptions.h"
|
|
|
|
#include "clang/Driver/DriverDiagnostic.h"
|
2010-05-21 01:49:16 +08:00
|
|
|
#include "clang/Driver/Options.h"
|
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
|
|
|
#include "clang/Frontend/TextDiagnosticPrinter.h"
|
2013-06-15 01:17:23 +08:00
|
|
|
#include "clang/Frontend/Utils.h"
|
2010-05-21 01:49:16 +08:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2010-08-30 17:42:39 +08:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2013-01-02 19:45:17 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2012-12-04 17:25:21 +08:00
|
|
|
#include "llvm/MC/MCAsmBackend.h"
|
2010-07-17 10:26:21 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2010-05-21 01:49:16 +08:00
|
|
|
#include "llvm/MC/MCCodeEmitter.h"
|
|
|
|
#include "llvm/MC/MCContext.h"
|
2011-07-11 12:24:19 +08:00
|
|
|
#include "llvm/MC/MCInstrInfo.h"
|
2011-07-20 14:22:27 +08:00
|
|
|
#include "llvm/MC/MCObjectFileInfo.h"
|
2012-12-04 17:25:21 +08:00
|
|
|
#include "llvm/MC/MCParser/MCAsmParser.h"
|
2011-07-19 04:57:51 +08:00
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
2010-05-21 01:49:16 +08:00
|
|
|
#include "llvm/MC/MCStreamer.h"
|
2011-07-09 14:04:17 +08:00
|
|
|
#include "llvm/MC/MCSubtargetInfo.h"
|
2011-07-26 08:24:45 +08:00
|
|
|
#include "llvm/MC/MCTargetAsmParser.h"
|
2014-04-23 19:15:49 +08:00
|
|
|
#include "llvm/MC/MCTargetOptions.h"
|
2013-06-15 01:17:23 +08:00
|
|
|
#include "llvm/Option/Arg.h"
|
|
|
|
#include "llvm/Option/ArgList.h"
|
|
|
|
#include "llvm/Option/OptTable.h"
|
2010-05-21 02:15:20 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2010-05-21 01:49:16 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2013-06-26 20:48:34 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2012-12-04 17:25:21 +08:00
|
|
|
#include "llvm/Support/FormattedStream.h"
|
|
|
|
#include "llvm/Support/Host.h"
|
2010-05-21 01:49:16 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2010-11-30 02:12:39 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2012-12-04 17:25:21 +08:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
2010-11-30 02:12:39 +08:00
|
|
|
#include "llvm/Support/Signals.h"
|
2012-12-04 17:25:21 +08:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2011-08-25 02:09:14 +08:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
#include "llvm/Support/TargetSelect.h"
|
|
|
|
#include "llvm/Support/Timer.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-03-09 19:36:40 +08:00
|
|
|
#include <memory>
|
2014-06-13 01:19:42 +08:00
|
|
|
#include <system_error>
|
2010-05-21 01:49:16 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace clang::driver;
|
2014-07-09 22:05:11 +08:00
|
|
|
using namespace clang::driver::options;
|
2010-05-21 01:49:16 +08:00
|
|
|
using namespace llvm;
|
2013-06-15 01:17:23 +08:00
|
|
|
using namespace llvm::opt;
|
2010-05-21 01:49:16 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// \brief Helper class for representing a single invocation of the assembler.
|
|
|
|
struct AssemblerInvocation {
|
|
|
|
/// @name Target Options
|
|
|
|
/// @{
|
|
|
|
|
2012-02-11 04:37:10 +08:00
|
|
|
/// The name of the target triple to assemble for.
|
2010-05-21 01:49:16 +08:00
|
|
|
std::string Triple;
|
|
|
|
|
2012-02-11 04:37:10 +08:00
|
|
|
/// If given, the name of the target CPU to determine which instructions
|
|
|
|
/// are legal.
|
|
|
|
std::string CPU;
|
|
|
|
|
|
|
|
/// The list of target specific features to enable or disable -- this should
|
|
|
|
/// be a list of strings starting with '+' or '-'.
|
|
|
|
std::vector<std::string> Features;
|
|
|
|
|
2010-05-21 01:49:16 +08:00
|
|
|
/// @}
|
|
|
|
/// @name Language Options
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
std::vector<std::string> IncludePaths;
|
|
|
|
unsigned NoInitialTextSection : 1;
|
2011-03-29 06:49:24 +08:00
|
|
|
unsigned SaveTemporaryLabels : 1;
|
2011-12-23 03:31:58 +08:00
|
|
|
unsigned GenDwarfForAssembly : 1;
|
2014-03-28 04:47:30 +08:00
|
|
|
unsigned CompressDebugSections : 1;
|
2014-05-19 21:39:13 +08:00
|
|
|
unsigned DwarfVersion;
|
2011-12-23 03:31:58 +08:00
|
|
|
std::string DwarfDebugFlags;
|
2013-01-18 05:38:06 +08:00
|
|
|
std::string DwarfDebugProducer;
|
2012-12-18 05:40:04 +08:00
|
|
|
std::string DebugCompilationDir;
|
2012-12-18 08:31:10 +08:00
|
|
|
std::string MainFileName;
|
2010-05-21 01:49:16 +08:00
|
|
|
|
|
|
|
/// @}
|
|
|
|
/// @name Frontend Options
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
std::string InputFile;
|
2010-05-21 02:15:20 +08:00
|
|
|
std::vector<std::string> LLVMArgs;
|
2010-05-21 01:49:16 +08:00
|
|
|
std::string OutputPath;
|
|
|
|
enum FileType {
|
|
|
|
FT_Asm, ///< Assembly (.s) output, transliterate mode.
|
|
|
|
FT_Null, ///< No output, for timing purposes.
|
|
|
|
FT_Obj ///< Object file output.
|
|
|
|
};
|
|
|
|
FileType OutputType;
|
2010-05-21 02:15:20 +08:00
|
|
|
unsigned ShowHelp : 1;
|
|
|
|
unsigned ShowVersion : 1;
|
2010-05-21 01:49:16 +08:00
|
|
|
|
|
|
|
/// @}
|
|
|
|
/// @name Transliterate Options
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
unsigned OutputAsmVariant;
|
|
|
|
unsigned ShowEncoding : 1;
|
|
|
|
unsigned ShowInst : 1;
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
/// @name Assembler Options
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
unsigned RelaxAll : 1;
|
2011-01-24 01:58:26 +08:00
|
|
|
unsigned NoExecStack : 1;
|
2014-08-27 02:40:25 +08:00
|
|
|
unsigned FatalWarnings : 1;
|
2010-05-21 01:49:16 +08:00
|
|
|
|
|
|
|
/// @}
|
|
|
|
|
|
|
|
public:
|
|
|
|
AssemblerInvocation() {
|
|
|
|
Triple = "";
|
|
|
|
NoInitialTextSection = 0;
|
|
|
|
InputFile = "-";
|
2010-05-21 02:15:20 +08:00
|
|
|
OutputPath = "-";
|
2010-05-21 01:49:16 +08:00
|
|
|
OutputType = FT_Asm;
|
|
|
|
OutputAsmVariant = 0;
|
|
|
|
ShowInst = 0;
|
|
|
|
ShowEncoding = 0;
|
|
|
|
RelaxAll = 0;
|
2011-01-24 01:58:26 +08:00
|
|
|
NoExecStack = 0;
|
2014-08-27 02:40:25 +08:00
|
|
|
FatalWarnings = 0;
|
2014-05-19 21:39:13 +08:00
|
|
|
DwarfVersion = 3;
|
2010-05-21 01:49:16 +08:00
|
|
|
}
|
|
|
|
|
2014-08-16 05:38:36 +08:00
|
|
|
static bool CreateFromArgs(AssemblerInvocation &Res,
|
|
|
|
ArrayRef<const char *> Argv,
|
|
|
|
DiagnosticsEngine &Diags);
|
2010-05-21 01:49:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-12-23 11:05:38 +08:00
|
|
|
bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,
|
2014-08-16 05:38:36 +08:00
|
|
|
ArrayRef<const char *> Argv,
|
2011-09-26 07:23:43 +08:00
|
|
|
DiagnosticsEngine &Diags) {
|
2011-12-23 11:05:38 +08:00
|
|
|
bool Success = true;
|
|
|
|
|
2010-05-21 01:49:16 +08:00
|
|
|
// Parse the arguments.
|
2014-07-09 22:05:11 +08:00
|
|
|
std::unique_ptr<OptTable> OptTbl(createDriverOptTable());
|
|
|
|
|
|
|
|
const unsigned IncludedFlagsBitmask = options::CC1AsOption;
|
2010-05-21 01:49:16 +08:00
|
|
|
unsigned MissingArgIndex, MissingArgCount;
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<InputArgList> Args(
|
2014-08-16 05:38:36 +08:00
|
|
|
OptTbl->ParseArgs(Argv.begin(), Argv.end(), MissingArgIndex, MissingArgCount,
|
2014-07-09 22:09:52 +08:00
|
|
|
IncludedFlagsBitmask));
|
2010-05-21 01:49:16 +08:00
|
|
|
|
|
|
|
// Check for missing argument error.
|
2011-12-23 11:05:38 +08:00
|
|
|
if (MissingArgCount) {
|
2010-05-21 01:49:16 +08:00
|
|
|
Diags.Report(diag::err_drv_missing_argument)
|
2014-07-09 22:05:11 +08:00
|
|
|
<< Args->getArgString(MissingArgIndex) << MissingArgCount;
|
2011-12-23 11:05:38 +08:00
|
|
|
Success = false;
|
|
|
|
}
|
2010-05-21 01:49:16 +08:00
|
|
|
|
|
|
|
// Issue errors on unknown arguments.
|
2014-07-09 22:05:11 +08:00
|
|
|
for (arg_iterator it = Args->filtered_begin(OPT_UNKNOWN),
|
|
|
|
ie = Args->filtered_end();
|
|
|
|
it != ie; ++it) {
|
|
|
|
Diags.Report(diag::err_drv_unknown_argument) << (*it)->getAsString(*Args);
|
2011-12-23 11:05:38 +08:00
|
|
|
Success = false;
|
|
|
|
}
|
2010-05-21 01:49:16 +08:00
|
|
|
|
|
|
|
// Construct the invocation.
|
|
|
|
|
|
|
|
// Target Options
|
2012-02-11 04:37:10 +08:00
|
|
|
Opts.Triple = llvm::Triple::normalize(Args->getLastArgValue(OPT_triple));
|
|
|
|
Opts.CPU = Args->getLastArgValue(OPT_target_cpu);
|
|
|
|
Opts.Features = Args->getAllArgValues(OPT_target_feature);
|
|
|
|
|
|
|
|
// Use the default target triple if unspecified.
|
|
|
|
if (Opts.Triple.empty())
|
|
|
|
Opts.Triple = llvm::sys::getDefaultTargetTriple();
|
2010-05-21 01:49:16 +08:00
|
|
|
|
|
|
|
// Language Options
|
|
|
|
Opts.IncludePaths = Args->getAllArgValues(OPT_I);
|
|
|
|
Opts.NoInitialTextSection = Args->hasArg(OPT_n);
|
2013-09-27 05:00:51 +08:00
|
|
|
Opts.SaveTemporaryLabels = Args->hasArg(OPT_msave_temp_labels);
|
2014-07-09 22:05:11 +08:00
|
|
|
Opts.GenDwarfForAssembly = Args->hasArg(OPT_g_Flag);
|
2014-03-28 04:47:30 +08:00
|
|
|
Opts.CompressDebugSections = Args->hasArg(OPT_compress_debug_sections);
|
2014-05-19 21:39:13 +08:00
|
|
|
if (Args->hasArg(OPT_gdwarf_2))
|
|
|
|
Opts.DwarfVersion = 2;
|
|
|
|
if (Args->hasArg(OPT_gdwarf_3))
|
|
|
|
Opts.DwarfVersion = 3;
|
|
|
|
if (Args->hasArg(OPT_gdwarf_4))
|
|
|
|
Opts.DwarfVersion = 4;
|
2011-12-23 03:31:58 +08:00
|
|
|
Opts.DwarfDebugFlags = Args->getLastArgValue(OPT_dwarf_debug_flags);
|
2013-01-18 05:38:06 +08:00
|
|
|
Opts.DwarfDebugProducer = Args->getLastArgValue(OPT_dwarf_debug_producer);
|
2012-12-18 05:40:04 +08:00
|
|
|
Opts.DebugCompilationDir = Args->getLastArgValue(OPT_fdebug_compilation_dir);
|
2012-12-18 08:31:10 +08:00
|
|
|
Opts.MainFileName = Args->getLastArgValue(OPT_main_file_name);
|
2010-05-21 01:49:16 +08:00
|
|
|
|
|
|
|
// Frontend Options
|
|
|
|
if (Args->hasArg(OPT_INPUT)) {
|
|
|
|
bool First = true;
|
|
|
|
for (arg_iterator it = Args->filtered_begin(OPT_INPUT),
|
|
|
|
ie = Args->filtered_end(); it != ie; ++it, First=false) {
|
2010-06-12 06:00:13 +08:00
|
|
|
const Arg *A = it;
|
2010-05-21 01:49:16 +08:00
|
|
|
if (First)
|
2012-11-01 12:30:05 +08:00
|
|
|
Opts.InputFile = A->getValue();
|
2011-12-23 11:05:38 +08:00
|
|
|
else {
|
2010-06-12 06:00:13 +08:00
|
|
|
Diags.Report(diag::err_drv_unknown_argument) << A->getAsString(*Args);
|
2011-12-23 11:05:38 +08:00
|
|
|
Success = false;
|
|
|
|
}
|
2010-05-21 01:49:16 +08:00
|
|
|
}
|
|
|
|
}
|
2010-05-21 02:15:20 +08:00
|
|
|
Opts.LLVMArgs = Args->getAllArgValues(OPT_mllvm);
|
2010-05-21 01:49:16 +08:00
|
|
|
Opts.OutputPath = Args->getLastArgValue(OPT_o);
|
|
|
|
if (Arg *A = Args->getLastArg(OPT_filetype)) {
|
2012-11-01 12:30:05 +08:00
|
|
|
StringRef Name = A->getValue();
|
2010-05-21 01:49:16 +08:00
|
|
|
unsigned OutputType = StringSwitch<unsigned>(Name)
|
|
|
|
.Case("asm", FT_Asm)
|
|
|
|
.Case("null", FT_Null)
|
|
|
|
.Case("obj", FT_Obj)
|
|
|
|
.Default(~0U);
|
2011-12-23 11:05:38 +08:00
|
|
|
if (OutputType == ~0U) {
|
2010-05-21 01:49:16 +08:00
|
|
|
Diags.Report(diag::err_drv_invalid_value)
|
|
|
|
<< A->getAsString(*Args) << Name;
|
2011-12-23 11:05:38 +08:00
|
|
|
Success = false;
|
|
|
|
} else
|
2010-05-21 01:49:16 +08:00
|
|
|
Opts.OutputType = FileType(OutputType);
|
|
|
|
}
|
2010-05-21 02:15:20 +08:00
|
|
|
Opts.ShowHelp = Args->hasArg(OPT_help);
|
|
|
|
Opts.ShowVersion = Args->hasArg(OPT_version);
|
2010-05-21 01:49:16 +08:00
|
|
|
|
|
|
|
// Transliterate Options
|
2013-06-15 01:17:23 +08:00
|
|
|
Opts.OutputAsmVariant =
|
|
|
|
getLastArgIntValue(*Args.get(), OPT_output_asm_variant, 0, Diags);
|
2010-05-21 01:49:16 +08:00
|
|
|
Opts.ShowEncoding = Args->hasArg(OPT_show_encoding);
|
|
|
|
Opts.ShowInst = Args->hasArg(OPT_show_inst);
|
|
|
|
|
|
|
|
// Assemble Options
|
2013-07-26 05:19:01 +08:00
|
|
|
Opts.RelaxAll = Args->hasArg(OPT_mrelax_all);
|
2014-08-27 02:40:25 +08:00
|
|
|
Opts.NoExecStack = Args->hasArg(OPT_mno_exec_stack);
|
|
|
|
Opts.FatalWarnings = Args->hasArg(OPT_massembler_fatal_warnings);
|
2011-12-23 11:05:38 +08:00
|
|
|
|
2011-12-27 03:29:47 +08:00
|
|
|
return Success;
|
2010-05-21 01:49:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static formatted_raw_ostream *GetOutputStream(AssemblerInvocation &Opts,
|
2011-09-26 07:23:43 +08:00
|
|
|
DiagnosticsEngine &Diags,
|
2010-05-21 01:49:16 +08:00
|
|
|
bool Binary) {
|
2010-05-21 02:15:20 +08:00
|
|
|
if (Opts.OutputPath.empty())
|
|
|
|
Opts.OutputPath = "-";
|
|
|
|
|
2010-05-21 01:49:16 +08:00
|
|
|
// Make sure that the Out file gets unlinked from the disk if we get a
|
|
|
|
// SIGINT.
|
|
|
|
if (Opts.OutputPath != "-")
|
2013-06-14 05:02:40 +08:00
|
|
|
sys::RemoveFileOnSignal(Opts.OutputPath);
|
2010-05-21 01:49:16 +08:00
|
|
|
|
2014-08-26 02:17:04 +08:00
|
|
|
std::error_code EC;
|
|
|
|
raw_fd_ostream *Out = new raw_fd_ostream(
|
|
|
|
Opts.OutputPath, EC, (Binary ? sys::fs::F_None : sys::fs::F_Text));
|
|
|
|
if (EC) {
|
|
|
|
Diags.Report(diag::err_fe_unable_to_open_output) << Opts.OutputPath
|
|
|
|
<< EC.message();
|
2014-05-31 08:02:21 +08:00
|
|
|
delete Out;
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2010-05-21 01:49:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return new formatted_raw_ostream(*Out, formatted_raw_ostream::DELETE_STREAM);
|
|
|
|
}
|
|
|
|
|
2011-09-26 07:23:43 +08:00
|
|
|
static bool ExecuteAssembler(AssemblerInvocation &Opts,
|
|
|
|
DiagnosticsEngine &Diags) {
|
2010-05-21 01:49:16 +08:00
|
|
|
// Get the target specific parser.
|
|
|
|
std::string Error;
|
2014-05-31 08:02:21 +08:00
|
|
|
const Target *TheTarget = TargetRegistry::lookupTarget(Opts.Triple, Error);
|
2014-05-31 08:02:27 +08:00
|
|
|
if (!TheTarget)
|
|
|
|
return Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
|
2010-05-21 01:49:16 +08:00
|
|
|
|
2014-07-07 01:43:24 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
|
|
|
|
MemoryBuffer::getFileOrSTDIN(Opts.InputFile);
|
|
|
|
|
|
|
|
if (std::error_code EC = Buffer.getError()) {
|
|
|
|
Error = EC.message();
|
2014-05-31 08:02:27 +08:00
|
|
|
return Diags.Report(diag::err_fe_error_reading) << Opts.InputFile;
|
2010-05-21 01:49:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SourceMgr SrcMgr;
|
|
|
|
|
|
|
|
// Tell SrcMgr about this buffer, which is what the parser will pick up.
|
2014-08-22 05:01:00 +08:00
|
|
|
SrcMgr.AddNewSourceBuffer(std::move(*Buffer), SMLoc());
|
2010-05-21 01:49:16 +08:00
|
|
|
|
|
|
|
// Record the location of the include directories so that the lexer can find
|
|
|
|
// it later.
|
|
|
|
SrcMgr.setIncludeDirs(Opts.IncludePaths);
|
|
|
|
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(Opts.Triple));
|
2011-07-19 04:57:51 +08:00
|
|
|
assert(MRI && "Unable to create target register info!");
|
|
|
|
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, Opts.Triple));
|
2013-05-13 09:24:18 +08:00
|
|
|
assert(MAI && "Unable to create target asm info!");
|
|
|
|
|
2014-04-01 07:47:13 +08:00
|
|
|
// Ensure MCAsmInfo initialization occurs before any use, otherwise sections
|
|
|
|
// may be created with a combination of default and explicit settings.
|
|
|
|
if (Opts.CompressDebugSections)
|
|
|
|
MAI->setCompressDebugSections(true);
|
|
|
|
|
2010-05-21 01:49:16 +08:00
|
|
|
bool IsBinary = Opts.OutputType == AssemblerInvocation::FT_Obj;
|
2014-05-31 08:02:21 +08:00
|
|
|
std::unique_ptr<formatted_raw_ostream> Out(
|
|
|
|
GetOutputStream(Opts, Diags, IsBinary));
|
2010-05-21 01:49:16 +08:00
|
|
|
if (!Out)
|
2014-05-31 08:02:27 +08:00
|
|
|
return true;
|
2010-05-21 01:49:16 +08:00
|
|
|
|
2011-07-20 14:22:27 +08:00
|
|
|
// FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
|
|
|
|
// MCObjectFileInfo needs a MCContext reference in order to initialize itself.
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<MCObjectFileInfo> MOFI(new MCObjectFileInfo());
|
2014-04-01 07:13:30 +08:00
|
|
|
|
2013-06-18 15:22:05 +08:00
|
|
|
MCContext Ctx(MAI.get(), MRI.get(), MOFI.get(), &SrcMgr);
|
2011-07-20 14:22:27 +08:00
|
|
|
// FIXME: Assembler behavior can change with -static.
|
2011-07-21 03:53:19 +08:00
|
|
|
MOFI->InitMCObjectFileInfo(Opts.Triple,
|
|
|
|
Reloc::Default, CodeModel::Default, Ctx);
|
2011-03-29 06:49:24 +08:00
|
|
|
if (Opts.SaveTemporaryLabels)
|
|
|
|
Ctx.setAllowTemporaryLabels(false);
|
2011-12-23 03:31:58 +08:00
|
|
|
if (Opts.GenDwarfForAssembly)
|
|
|
|
Ctx.setGenDwarfForAssembly(true);
|
|
|
|
if (!Opts.DwarfDebugFlags.empty())
|
|
|
|
Ctx.setDwarfDebugFlags(StringRef(Opts.DwarfDebugFlags));
|
2013-01-18 05:38:06 +08:00
|
|
|
if (!Opts.DwarfDebugProducer.empty())
|
|
|
|
Ctx.setDwarfDebugProducer(StringRef(Opts.DwarfDebugProducer));
|
2012-12-18 05:40:04 +08:00
|
|
|
if (!Opts.DebugCompilationDir.empty())
|
|
|
|
Ctx.setCompilationDir(Opts.DebugCompilationDir);
|
2012-12-18 08:31:10 +08:00
|
|
|
if (!Opts.MainFileName.empty())
|
|
|
|
Ctx.setMainFileName(StringRef(Opts.MainFileName));
|
2014-05-19 21:39:13 +08:00
|
|
|
Ctx.setDwarfVersion(Opts.DwarfVersion);
|
2010-12-10 15:40:14 +08:00
|
|
|
|
2012-02-11 04:37:10 +08:00
|
|
|
// Build up the feature string from the target feature list.
|
|
|
|
std::string FS;
|
|
|
|
if (!Opts.Features.empty()) {
|
|
|
|
FS = Opts.Features[0];
|
|
|
|
for (unsigned i = 1, e = Opts.Features.size(); i != e; ++i)
|
|
|
|
FS += "," + Opts.Features[i];
|
|
|
|
}
|
|
|
|
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<MCStreamer> Str;
|
2010-05-21 01:49:16 +08:00
|
|
|
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
|
|
|
|
std::unique_ptr<MCSubtargetInfo> STI(
|
|
|
|
TheTarget->createMCSubtargetInfo(Opts.Triple, Opts.CPU, FS));
|
2011-07-11 12:24:19 +08:00
|
|
|
|
2011-01-24 01:58:26 +08:00
|
|
|
// FIXME: There is a bit of code duplication with addPassesToEmitFile.
|
2010-05-21 01:49:16 +08:00
|
|
|
if (Opts.OutputType == AssemblerInvocation::FT_Asm) {
|
|
|
|
MCInstPrinter *IP =
|
2012-04-02 14:17:37 +08:00
|
|
|
TheTarget->createMCInstPrinter(Opts.OutputAsmVariant, *MAI, *MCII, *MRI,
|
|
|
|
*STI);
|
2014-06-08 16:38:04 +08:00
|
|
|
MCCodeEmitter *CE = nullptr;
|
|
|
|
MCAsmBackend *MAB = nullptr;
|
2010-12-16 11:06:05 +08:00
|
|
|
if (Opts.ShowEncoding) {
|
2012-05-16 01:36:07 +08:00
|
|
|
CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, *STI, Ctx);
|
2013-09-09 10:37:56 +08:00
|
|
|
MAB = TheTarget->createMCAsmBackend(*MRI, Opts.Triple, Opts.CPU);
|
2010-12-16 11:06:05 +08:00
|
|
|
}
|
2010-12-10 15:40:14 +08:00
|
|
|
Str.reset(TheTarget->createAsmStreamer(Ctx, *Out, /*asmverbose*/true,
|
2011-10-18 07:05:52 +08:00
|
|
|
/*useDwarfDirectory*/ true,
|
|
|
|
IP, CE, MAB,
|
2010-12-10 15:40:14 +08:00
|
|
|
Opts.ShowInst));
|
2010-05-21 01:49:16 +08:00
|
|
|
} else if (Opts.OutputType == AssemblerInvocation::FT_Null) {
|
|
|
|
Str.reset(createNullStreamer(Ctx));
|
|
|
|
} else {
|
|
|
|
assert(Opts.OutputType == AssemblerInvocation::FT_Obj &&
|
|
|
|
"Invalid file type!");
|
2012-05-16 01:36:07 +08:00
|
|
|
MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, *STI, Ctx);
|
2013-09-09 10:37:56 +08:00
|
|
|
MCAsmBackend *MAB = TheTarget->createMCAsmBackend(*MRI, Opts.Triple,
|
|
|
|
Opts.CPU);
|
2011-07-26 08:42:40 +08:00
|
|
|
Str.reset(TheTarget->createMCObjectStreamer(Opts.Triple, Ctx, *MAB, *Out,
|
2014-10-16 00:12:57 +08:00
|
|
|
CE, *STI, Opts.RelaxAll));
|
|
|
|
Str.get()->InitSections(Opts.NoExecStack);
|
2010-05-21 01:49:16 +08:00
|
|
|
}
|
|
|
|
|
2014-05-31 08:02:27 +08:00
|
|
|
bool Failed = false;
|
2014-05-31 08:02:21 +08:00
|
|
|
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<MCAsmParser> Parser(
|
|
|
|
createMCAsmParser(SrcMgr, Ctx, *Str.get(), *MAI));
|
2014-04-23 19:15:49 +08:00
|
|
|
|
|
|
|
// FIXME: init MCTargetOptions from sanitizer flags here.
|
|
|
|
MCTargetOptions Options;
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<MCTargetAsmParser> TAP(
|
2014-04-23 19:15:49 +08:00
|
|
|
TheTarget->createMCAsmParser(*STI, *Parser, *MCII, Options));
|
2014-05-31 08:02:27 +08:00
|
|
|
if (!TAP)
|
|
|
|
Failed = Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
|
2010-05-21 01:49:16 +08:00
|
|
|
|
2014-05-31 08:02:27 +08:00
|
|
|
if (!Failed) {
|
2014-05-31 08:02:21 +08:00
|
|
|
Parser->setTargetParser(*TAP.get());
|
2014-05-31 08:02:27 +08:00
|
|
|
Failed = Parser->Run(Opts.NoInitialTextSection);
|
2014-05-31 08:02:21 +08:00
|
|
|
}
|
2010-05-21 01:49:16 +08:00
|
|
|
|
2014-05-31 08:02:21 +08:00
|
|
|
// Close the output stream early.
|
|
|
|
Out.reset();
|
2010-05-21 01:49:16 +08:00
|
|
|
|
2014-05-31 08:02:21 +08:00
|
|
|
// Delete output file if there were errors.
|
2014-05-31 08:02:27 +08:00
|
|
|
if (Failed && Opts.OutputPath != "-")
|
2013-06-26 20:48:34 +08:00
|
|
|
sys::fs::remove(Opts.OutputPath);
|
2010-05-21 01:49:16 +08:00
|
|
|
|
2014-05-31 08:02:27 +08:00
|
|
|
return Failed;
|
2010-05-21 01:49:16 +08:00
|
|
|
}
|
|
|
|
|
2013-03-28 02:28:23 +08:00
|
|
|
static void LLVMErrorHandler(void *UserData, const std::string &Message,
|
|
|
|
bool GenCrashDiag) {
|
2011-09-26 07:23:43 +08:00
|
|
|
DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
|
2010-05-21 01:49:16 +08:00
|
|
|
|
|
|
|
Diags.Report(diag::err_fe_error_backend) << Message;
|
|
|
|
|
|
|
|
// We cannot recover from llvm errors.
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2014-08-16 05:38:36 +08:00
|
|
|
int cc1as_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
|
2010-05-21 01:49:16 +08:00
|
|
|
// Print a stack trace if we signal out.
|
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
2014-08-16 05:38:36 +08:00
|
|
|
PrettyStackTraceProgram X(Argv.size(), Argv.data());
|
2010-05-21 01:49:16 +08:00
|
|
|
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
|
|
|
|
|
|
|
|
// Initialize targets and assembly printers/parsers.
|
|
|
|
InitializeAllTargetInfos();
|
2011-07-23 05:59:11 +08:00
|
|
|
InitializeAllTargetMCs();
|
2010-05-21 01:49:16 +08:00
|
|
|
InitializeAllAsmParsers();
|
|
|
|
|
|
|
|
// Construct our diagnostic client.
|
2012-10-24 06:26:28 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
|
2010-08-19 06:29:43 +08:00
|
|
|
TextDiagnosticPrinter *DiagClient
|
2012-10-24 06:26:28 +08:00
|
|
|
= new TextDiagnosticPrinter(errs(), &*DiagOpts);
|
2010-08-19 06:29:43 +08:00
|
|
|
DiagClient->setPrefix("clang -cc1as");
|
2012-02-20 22:00:23 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
|
2012-10-24 06:26:28 +08:00
|
|
|
DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagClient);
|
2010-05-21 01:49:16 +08:00
|
|
|
|
|
|
|
// Set an error handler, so that any LLVM backend diagnostics go through our
|
|
|
|
// error handler.
|
2010-08-19 05:23:17 +08:00
|
|
|
ScopedFatalErrorHandler FatalErrorHandler
|
|
|
|
(LLVMErrorHandler, static_cast<void*>(&Diags));
|
2010-05-21 01:49:16 +08:00
|
|
|
|
|
|
|
// Parse the arguments.
|
|
|
|
AssemblerInvocation Asm;
|
2014-08-16 05:38:36 +08:00
|
|
|
if (!AssemblerInvocation::CreateFromArgs(Asm, Argv, Diags))
|
2011-12-23 11:05:38 +08:00
|
|
|
return 1;
|
2010-05-21 01:49:16 +08:00
|
|
|
|
2010-05-21 02:15:20 +08:00
|
|
|
if (Asm.ShowHelp) {
|
2014-07-09 22:05:11 +08:00
|
|
|
std::unique_ptr<OptTable> Opts(driver::createDriverOptTable());
|
2014-07-09 22:09:52 +08:00
|
|
|
Opts->PrintHelp(llvm::outs(), "clang -cc1as", "Clang Integrated Assembler",
|
|
|
|
/*Include=*/driver::options::CC1AsOption, /*Exclude=*/0);
|
2010-05-21 02:15:20 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Honor -version.
|
|
|
|
//
|
|
|
|
// FIXME: Use a better -version message?
|
|
|
|
if (Asm.ShowVersion) {
|
|
|
|
llvm::cl::PrintVersionMessage();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Honor -mllvm.
|
|
|
|
//
|
|
|
|
// FIXME: Remove this, one day.
|
|
|
|
if (!Asm.LLVMArgs.empty()) {
|
|
|
|
unsigned NumArgs = Asm.LLVMArgs.size();
|
|
|
|
const char **Args = new const char*[NumArgs + 2];
|
|
|
|
Args[0] = "clang (LLVM option parsing)";
|
|
|
|
for (unsigned i = 0; i != NumArgs; ++i)
|
|
|
|
Args[i + 1] = Asm.LLVMArgs[i].c_str();
|
2014-06-08 16:38:04 +08:00
|
|
|
Args[NumArgs + 1] = nullptr;
|
2012-02-08 03:36:38 +08:00
|
|
|
llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args);
|
2010-05-21 02:15:20 +08:00
|
|
|
}
|
|
|
|
|
2010-05-21 01:49:16 +08:00
|
|
|
// Execute the invocation, unless there were parsing errors.
|
2014-05-31 08:02:27 +08:00
|
|
|
bool Failed = Diags.hasErrorOccurred() || ExecuteAssembler(Asm, Diags);
|
2010-05-21 01:49:16 +08:00
|
|
|
|
|
|
|
// If any timers were active but haven't been destroyed yet, print their
|
|
|
|
// results now.
|
|
|
|
TimerGroup::printAll(errs());
|
|
|
|
|
2014-05-31 08:02:27 +08:00
|
|
|
return !!Failed;
|
2010-05-21 01:49:16 +08:00
|
|
|
}
|
2014-07-09 22:05:11 +08:00
|
|
|
|