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"
|
2010-05-21 01:49:16 +08:00
|
|
|
#include "clang/Driver/CC1AsOptions.h"
|
2012-12-04 17:25:21 +08:00
|
|
|
#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/OwningPtr.h"
|
|
|
|
#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"
|
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"
|
2010-12-10 01:36:38 +08:00
|
|
|
#include "llvm/Support/system_error.h"
|
2010-05-21 01:49:16 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace clang::driver;
|
|
|
|
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;
|
|
|
|
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;
|
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;
|
2010-05-21 01:49:16 +08:00
|
|
|
}
|
|
|
|
|
2011-12-23 11:05:38 +08:00
|
|
|
static bool CreateFromArgs(AssemblerInvocation &Res, const char **ArgBegin,
|
2011-09-26 07:23:43 +08:00
|
|
|
const char **ArgEnd, DiagnosticsEngine &Diags);
|
2010-05-21 01:49:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-12-23 11:05:38 +08:00
|
|
|
bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,
|
2010-05-21 01:49:16 +08:00
|
|
|
const char **ArgBegin,
|
|
|
|
const char **ArgEnd,
|
2011-09-26 07:23:43 +08:00
|
|
|
DiagnosticsEngine &Diags) {
|
2010-05-21 01:49:16 +08:00
|
|
|
using namespace clang::driver::cc1asoptions;
|
2011-12-23 11:05:38 +08:00
|
|
|
bool Success = true;
|
|
|
|
|
2010-05-21 01:49:16 +08:00
|
|
|
// Parse the arguments.
|
|
|
|
OwningPtr<OptTable> OptTbl(createCC1AsOptTable());
|
|
|
|
unsigned MissingArgIndex, MissingArgCount;
|
|
|
|
OwningPtr<InputArgList> Args(
|
|
|
|
OptTbl->ParseArgs(ArgBegin, ArgEnd,MissingArgIndex, MissingArgCount));
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
<< 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.
|
|
|
|
for (arg_iterator it = Args->filtered_begin(cc1asoptions::OPT_UNKNOWN),
|
2011-12-23 11:05:38 +08:00
|
|
|
ie = Args->filtered_end(); it != ie; ++it) {
|
2010-06-12 06:00:13 +08:00
|
|
|
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);
|
2011-03-29 06:49:24 +08:00
|
|
|
Opts.SaveTemporaryLabels = Args->hasArg(OPT_L);
|
2011-12-23 03:31:58 +08:00
|
|
|
Opts.GenDwarfForAssembly = Args->hasArg(OPT_g);
|
|
|
|
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);
|
2011-05-20 02:42:29 +08:00
|
|
|
if (Args->hasArg(OPT_fatal_warnings))
|
|
|
|
Opts.LLVMArgs.push_back("-fatal-assembler-warnings");
|
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
|
|
|
|
Opts.RelaxAll = Args->hasArg(OPT_relax_all);
|
2011-01-24 01:58:26 +08:00
|
|
|
Opts.NoExecStack = Args->hasArg(OPT_no_exec_stack);
|
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
|
|
|
|
|
|
|
std::string Error;
|
|
|
|
raw_fd_ostream *Out =
|
2013-07-17 03:44:23 +08:00
|
|
|
new raw_fd_ostream(Opts.OutputPath.c_str(), Error,
|
|
|
|
(Binary ? sys::fs::F_Binary : sys::fs::F_None));
|
2010-05-21 01:49:16 +08:00
|
|
|
if (!Error.empty()) {
|
|
|
|
Diags.Report(diag::err_fe_unable_to_open_output)
|
|
|
|
<< Opts.OutputPath << Error;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
const Target *TheTarget(TargetRegistry::lookupTarget(Opts.Triple, Error));
|
|
|
|
if (!TheTarget) {
|
|
|
|
Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-12-16 11:28:14 +08:00
|
|
|
OwningPtr<MemoryBuffer> BufferPtr;
|
|
|
|
if (error_code ec = MemoryBuffer::getFileOrSTDIN(Opts.InputFile, BufferPtr)) {
|
2010-12-10 01:36:38 +08:00
|
|
|
Error = ec.message();
|
2010-05-21 01:49:16 +08:00
|
|
|
Diags.Report(diag::err_fe_error_reading) << Opts.InputFile;
|
|
|
|
return false;
|
|
|
|
}
|
2010-12-16 11:28:14 +08:00
|
|
|
MemoryBuffer *Buffer = BufferPtr.take();
|
2010-05-21 01:49:16 +08:00
|
|
|
|
|
|
|
SourceMgr SrcMgr;
|
|
|
|
|
|
|
|
// Tell SrcMgr about this buffer, which is what the parser will pick up.
|
|
|
|
SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
|
|
|
|
|
|
|
|
// Record the location of the include directories so that the lexer can find
|
|
|
|
// it later.
|
|
|
|
SrcMgr.setIncludeDirs(Opts.IncludePaths);
|
|
|
|
|
2011-07-19 04:57:51 +08:00
|
|
|
OwningPtr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(Opts.Triple));
|
|
|
|
assert(MRI && "Unable to create target register info!");
|
|
|
|
|
2013-05-13 09:24:18 +08:00
|
|
|
OwningPtr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, Opts.Triple));
|
|
|
|
assert(MAI && "Unable to create target asm info!");
|
|
|
|
|
2010-05-21 01:49:16 +08:00
|
|
|
bool IsBinary = Opts.OutputType == AssemblerInvocation::FT_Obj;
|
|
|
|
formatted_raw_ostream *Out = GetOutputStream(Opts, Diags, IsBinary);
|
|
|
|
if (!Out)
|
|
|
|
return false;
|
|
|
|
|
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.
|
|
|
|
OwningPtr<MCObjectFileInfo> MOFI(new MCObjectFileInfo());
|
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));
|
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];
|
|
|
|
}
|
|
|
|
|
2010-05-21 01:49:16 +08:00
|
|
|
OwningPtr<MCStreamer> Str;
|
|
|
|
|
2011-07-26 09:49:26 +08:00
|
|
|
OwningPtr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
|
|
|
|
OwningPtr<MCSubtargetInfo>
|
2012-02-11 04:37:10 +08:00
|
|
|
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);
|
2010-07-30 01:48:03 +08:00
|
|
|
MCCodeEmitter *CE = 0;
|
2011-07-26 07:25:09 +08:00
|
|
|
MCAsmBackend *MAB = 0;
|
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);
|
2012-09-19 00:09:16 +08:00
|
|
|
MAB = TheTarget->createMCAsmBackend(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-04-30 11:46:18 +08:00
|
|
|
/*useLoc*/ true,
|
2011-10-18 07:05:52 +08:00
|
|
|
/*useCFI*/ true,
|
|
|
|
/*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);
|
2012-09-19 00:09:16 +08:00
|
|
|
MCAsmBackend *MAB = TheTarget->createMCAsmBackend(Opts.Triple, Opts.CPU);
|
2011-07-26 08:42:40 +08:00
|
|
|
Str.reset(TheTarget->createMCObjectStreamer(Opts.Triple, Ctx, *MAB, *Out,
|
|
|
|
CE, Opts.RelaxAll,
|
|
|
|
Opts.NoExecStack));
|
2010-10-13 22:53:57 +08:00
|
|
|
Str.get()->InitSections();
|
2010-05-21 01:49:16 +08:00
|
|
|
}
|
|
|
|
|
2011-08-17 02:33:55 +08:00
|
|
|
OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr, Ctx,
|
2010-07-17 10:26:21 +08:00
|
|
|
*Str.get(), *MAI));
|
2011-07-26 09:49:26 +08:00
|
|
|
OwningPtr<MCTargetAsmParser> TAP(TheTarget->createMCAsmParser(*STI, *Parser));
|
2010-05-21 01:49:16 +08:00
|
|
|
if (!TAP) {
|
|
|
|
Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-07-17 10:26:21 +08:00
|
|
|
Parser->setTargetParser(*TAP.get());
|
2010-05-21 01:49:16 +08:00
|
|
|
|
2010-07-17 10:26:21 +08:00
|
|
|
bool Success = !Parser->Run(Opts.NoInitialTextSection);
|
2010-05-21 01:49:16 +08:00
|
|
|
|
|
|
|
// Close the output.
|
|
|
|
delete Out;
|
|
|
|
|
|
|
|
// Delete output on errors.
|
|
|
|
if (!Success && Opts.OutputPath != "-")
|
2013-06-26 20:48:34 +08:00
|
|
|
sys::fs::remove(Opts.OutputPath);
|
2010-05-21 01:49:16 +08:00
|
|
|
|
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
int cc1as_main(const char **ArgBegin, const char **ArgEnd,
|
|
|
|
const char *Argv0, void *MainAddr) {
|
|
|
|
// Print a stack trace if we signal out.
|
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
|
|
|
PrettyStackTraceProgram X(ArgEnd - ArgBegin, ArgBegin);
|
|
|
|
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;
|
2011-12-23 11:05:38 +08:00
|
|
|
if (!AssemblerInvocation::CreateFromArgs(Asm, ArgBegin, ArgEnd, Diags))
|
|
|
|
return 1;
|
2010-05-21 01:49:16 +08:00
|
|
|
|
2010-05-21 02:15:20 +08:00
|
|
|
// Honor -help.
|
|
|
|
if (Asm.ShowHelp) {
|
2013-06-15 01:17:23 +08:00
|
|
|
OwningPtr<OptTable> Opts(driver::createCC1AsOptTable());
|
2010-05-21 02:15:20 +08:00
|
|
|
Opts->PrintHelp(llvm::outs(), "clang -cc1as", "Clang Integrated Assembler");
|
|
|
|
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();
|
|
|
|
Args[NumArgs + 1] = 0;
|
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.
|
|
|
|
bool Success = false;
|
2010-11-19 08:19:18 +08:00
|
|
|
if (!Diags.hasErrorOccurred())
|
2010-05-21 01:49:16 +08:00
|
|
|
Success = ExecuteAssembler(Asm, Diags);
|
|
|
|
|
|
|
|
// If any timers were active but haven't been destroyed yet, print their
|
|
|
|
// results now.
|
|
|
|
TimerGroup::printAll(errs());
|
|
|
|
|
|
|
|
return !Success;
|
|
|
|
}
|