2010-05-20 21:49:16 +04: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"
|
|
|
|
#include "clang/Driver/Arg.h"
|
|
|
|
#include "clang/Driver/ArgList.h"
|
|
|
|
#include "clang/Driver/DriverDiagnostic.h"
|
|
|
|
#include "clang/Driver/CC1AsOptions.h"
|
|
|
|
#include "clang/Driver/OptTable.h"
|
|
|
|
#include "clang/Driver/Options.h"
|
|
|
|
#include "clang/Frontend/DiagnosticOptions.h"
|
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
|
|
|
#include "clang/Frontend/TextDiagnosticPrinter.h"
|
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2010-08-30 13:42:39 +04:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2010-07-17 06:26:21 +04:00
|
|
|
#include "llvm/MC/MCParser/MCAsmParser.h"
|
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2010-05-20 21:49:16 +04:00
|
|
|
#include "llvm/MC/MCCodeEmitter.h"
|
|
|
|
#include "llvm/MC/MCContext.h"
|
2011-07-11 08:24:19 +04:00
|
|
|
#include "llvm/MC/MCInstrInfo.h"
|
2011-07-20 10:22:27 +04:00
|
|
|
#include "llvm/MC/MCObjectFileInfo.h"
|
2011-07-19 00:57:51 +04:00
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
2010-05-20 21:49:16 +04:00
|
|
|
#include "llvm/MC/MCStreamer.h"
|
2011-07-09 10:04:17 +04:00
|
|
|
#include "llvm/MC/MCSubtargetInfo.h"
|
2011-07-26 03:25:09 +04:00
|
|
|
#include "llvm/MC/MCAsmBackend.h"
|
2011-07-26 04:24:45 +04:00
|
|
|
#include "llvm/MC/MCTargetAsmParser.h"
|
2010-05-20 22:15:20 +04:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2010-05-20 21:49:16 +04:00
|
|
|
#include "llvm/Support/FormattedStream.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2010-11-29 21:12:39 +03:00
|
|
|
#include "llvm/Support/Host.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/Signals.h"
|
2011-08-24 22:09:14 +04:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
#include "llvm/Support/TargetSelect.h"
|
|
|
|
#include "llvm/Support/Timer.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-12-09 20:36:38 +03:00
|
|
|
#include "llvm/Support/system_error.h"
|
2010-05-20 21:49:16 +04:00
|
|
|
#include "llvm/Target/TargetData.h"
|
|
|
|
using namespace clang;
|
|
|
|
using namespace clang::driver;
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// \brief Helper class for representing a single invocation of the assembler.
|
|
|
|
struct AssemblerInvocation {
|
|
|
|
/// @name Target Options
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
std::string Triple;
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
/// @name Language Options
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
std::vector<std::string> IncludePaths;
|
|
|
|
unsigned NoInitialTextSection : 1;
|
2011-03-29 02:49:24 +04:00
|
|
|
unsigned SaveTemporaryLabels : 1;
|
2011-12-22 23:31:58 +04:00
|
|
|
unsigned GenDwarfForAssembly : 1;
|
|
|
|
std::string DwarfDebugFlags;
|
2010-05-20 21:49:16 +04:00
|
|
|
|
|
|
|
/// @}
|
|
|
|
/// @name Frontend Options
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
std::string InputFile;
|
2010-05-20 22:15:20 +04:00
|
|
|
std::vector<std::string> LLVMArgs;
|
2010-05-20 21:49:16 +04: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-20 22:15:20 +04:00
|
|
|
unsigned ShowHelp : 1;
|
|
|
|
unsigned ShowVersion : 1;
|
2010-05-20 21:49:16 +04:00
|
|
|
|
|
|
|
/// @}
|
|
|
|
/// @name Transliterate Options
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
unsigned OutputAsmVariant;
|
|
|
|
unsigned ShowEncoding : 1;
|
|
|
|
unsigned ShowInst : 1;
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
/// @name Assembler Options
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
unsigned RelaxAll : 1;
|
2011-01-23 20:58:26 +03:00
|
|
|
unsigned NoExecStack : 1;
|
2010-05-20 21:49:16 +04:00
|
|
|
|
|
|
|
/// @}
|
|
|
|
|
|
|
|
public:
|
|
|
|
AssemblerInvocation() {
|
|
|
|
Triple = "";
|
|
|
|
NoInitialTextSection = 0;
|
|
|
|
InputFile = "-";
|
2010-05-20 22:15:20 +04:00
|
|
|
OutputPath = "-";
|
2010-05-20 21:49:16 +04:00
|
|
|
OutputType = FT_Asm;
|
|
|
|
OutputAsmVariant = 0;
|
|
|
|
ShowInst = 0;
|
|
|
|
ShowEncoding = 0;
|
|
|
|
RelaxAll = 0;
|
2011-01-23 20:58:26 +03:00
|
|
|
NoExecStack = 0;
|
2010-05-20 21:49:16 +04:00
|
|
|
}
|
|
|
|
|
2011-12-23 07:05:38 +04:00
|
|
|
static bool CreateFromArgs(AssemblerInvocation &Res, const char **ArgBegin,
|
2011-09-26 03:23:43 +04:00
|
|
|
const char **ArgEnd, DiagnosticsEngine &Diags);
|
2010-05-20 21:49:16 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-12-23 07:05:38 +04:00
|
|
|
bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,
|
2010-05-20 21:49:16 +04:00
|
|
|
const char **ArgBegin,
|
|
|
|
const char **ArgEnd,
|
2011-09-26 03:23:43 +04:00
|
|
|
DiagnosticsEngine &Diags) {
|
2010-05-20 21:49:16 +04:00
|
|
|
using namespace clang::driver::cc1asoptions;
|
2011-12-23 07:05:38 +04:00
|
|
|
bool Success = true;
|
|
|
|
|
2010-05-20 21:49:16 +04: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 07:05:38 +04:00
|
|
|
if (MissingArgCount) {
|
2010-05-20 21:49:16 +04:00
|
|
|
Diags.Report(diag::err_drv_missing_argument)
|
|
|
|
<< Args->getArgString(MissingArgIndex) << MissingArgCount;
|
2011-12-23 07:05:38 +04:00
|
|
|
Success = false;
|
|
|
|
}
|
2010-05-20 21:49:16 +04:00
|
|
|
|
|
|
|
// Issue errors on unknown arguments.
|
|
|
|
for (arg_iterator it = Args->filtered_begin(cc1asoptions::OPT_UNKNOWN),
|
2011-12-23 07:05:38 +04:00
|
|
|
ie = Args->filtered_end(); it != ie; ++it) {
|
2010-06-12 02:00:13 +04:00
|
|
|
Diags.Report(diag::err_drv_unknown_argument) << (*it) ->getAsString(*Args);
|
2011-12-23 07:05:38 +04:00
|
|
|
Success = false;
|
|
|
|
}
|
2010-05-20 21:49:16 +04:00
|
|
|
|
|
|
|
// Construct the invocation.
|
|
|
|
|
|
|
|
// Target Options
|
2010-08-30 13:42:39 +04:00
|
|
|
Opts.Triple = Triple::normalize(Args->getLastArgValue(OPT_triple));
|
2011-11-02 01:33:06 +04:00
|
|
|
if (Opts.Triple.empty()) // Use the default target triple if unspecified.
|
|
|
|
Opts.Triple = sys::getDefaultTargetTriple();
|
2010-05-20 21:49:16 +04:00
|
|
|
|
|
|
|
// Language Options
|
|
|
|
Opts.IncludePaths = Args->getAllArgValues(OPT_I);
|
|
|
|
Opts.NoInitialTextSection = Args->hasArg(OPT_n);
|
2011-03-29 02:49:24 +04:00
|
|
|
Opts.SaveTemporaryLabels = Args->hasArg(OPT_L);
|
2011-12-22 23:31:58 +04:00
|
|
|
Opts.GenDwarfForAssembly = Args->hasArg(OPT_g);
|
|
|
|
Opts.DwarfDebugFlags = Args->getLastArgValue(OPT_dwarf_debug_flags);
|
2010-05-20 21:49:16 +04: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 02:00:13 +04:00
|
|
|
const Arg *A = it;
|
2010-05-20 21:49:16 +04:00
|
|
|
if (First)
|
2010-06-12 02:00:13 +04:00
|
|
|
Opts.InputFile = A->getValue(*Args);
|
2011-12-23 07:05:38 +04:00
|
|
|
else {
|
2010-06-12 02:00:13 +04:00
|
|
|
Diags.Report(diag::err_drv_unknown_argument) << A->getAsString(*Args);
|
2011-12-23 07:05:38 +04:00
|
|
|
Success = false;
|
|
|
|
}
|
2010-05-20 21:49:16 +04:00
|
|
|
}
|
|
|
|
}
|
2010-05-20 22:15:20 +04:00
|
|
|
Opts.LLVMArgs = Args->getAllArgValues(OPT_mllvm);
|
2011-05-19 22:42:29 +04:00
|
|
|
if (Args->hasArg(OPT_fatal_warnings))
|
|
|
|
Opts.LLVMArgs.push_back("-fatal-assembler-warnings");
|
2010-05-20 21:49:16 +04:00
|
|
|
Opts.OutputPath = Args->getLastArgValue(OPT_o);
|
|
|
|
if (Arg *A = Args->getLastArg(OPT_filetype)) {
|
|
|
|
StringRef Name = A->getValue(*Args);
|
|
|
|
unsigned OutputType = StringSwitch<unsigned>(Name)
|
|
|
|
.Case("asm", FT_Asm)
|
|
|
|
.Case("null", FT_Null)
|
|
|
|
.Case("obj", FT_Obj)
|
|
|
|
.Default(~0U);
|
2011-12-23 07:05:38 +04:00
|
|
|
if (OutputType == ~0U) {
|
2010-05-20 21:49:16 +04:00
|
|
|
Diags.Report(diag::err_drv_invalid_value)
|
|
|
|
<< A->getAsString(*Args) << Name;
|
2011-12-23 07:05:38 +04:00
|
|
|
Success = false;
|
|
|
|
} else
|
2010-05-20 21:49:16 +04:00
|
|
|
Opts.OutputType = FileType(OutputType);
|
|
|
|
}
|
2010-05-20 22:15:20 +04:00
|
|
|
Opts.ShowHelp = Args->hasArg(OPT_help);
|
|
|
|
Opts.ShowVersion = Args->hasArg(OPT_version);
|
2010-05-20 21:49:16 +04:00
|
|
|
|
|
|
|
// Transliterate Options
|
|
|
|
Opts.OutputAsmVariant = Args->getLastArgIntValue(OPT_output_asm_variant,
|
|
|
|
0, Diags);
|
|
|
|
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-23 20:58:26 +03:00
|
|
|
Opts.NoExecStack = Args->hasArg(OPT_no_exec_stack);
|
2011-12-23 07:05:38 +04:00
|
|
|
|
2011-12-26 23:29:47 +04:00
|
|
|
return Success;
|
2010-05-20 21:49:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static formatted_raw_ostream *GetOutputStream(AssemblerInvocation &Opts,
|
2011-09-26 03:23:43 +04:00
|
|
|
DiagnosticsEngine &Diags,
|
2010-05-20 21:49:16 +04:00
|
|
|
bool Binary) {
|
2010-05-20 22:15:20 +04:00
|
|
|
if (Opts.OutputPath.empty())
|
|
|
|
Opts.OutputPath = "-";
|
|
|
|
|
2010-05-20 21:49:16 +04:00
|
|
|
// Make sure that the Out file gets unlinked from the disk if we get a
|
|
|
|
// SIGINT.
|
|
|
|
if (Opts.OutputPath != "-")
|
|
|
|
sys::RemoveFileOnSignal(sys::Path(Opts.OutputPath));
|
|
|
|
|
|
|
|
std::string Error;
|
|
|
|
raw_fd_ostream *Out =
|
|
|
|
new raw_fd_ostream(Opts.OutputPath.c_str(), Error,
|
|
|
|
(Binary ? raw_fd_ostream::F_Binary : 0));
|
|
|
|
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 03:23:43 +04:00
|
|
|
static bool ExecuteAssembler(AssemblerInvocation &Opts,
|
|
|
|
DiagnosticsEngine &Diags) {
|
2010-05-20 21:49:16 +04: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 06:28:14 +03:00
|
|
|
OwningPtr<MemoryBuffer> BufferPtr;
|
|
|
|
if (error_code ec = MemoryBuffer::getFileOrSTDIN(Opts.InputFile, BufferPtr)) {
|
2010-12-09 20:36:38 +03:00
|
|
|
Error = ec.message();
|
2010-05-20 21:49:16 +04:00
|
|
|
Diags.Report(diag::err_fe_error_reading) << Opts.InputFile;
|
|
|
|
return false;
|
|
|
|
}
|
2010-12-16 06:28:14 +03:00
|
|
|
MemoryBuffer *Buffer = BufferPtr.take();
|
2010-05-20 21:49:16 +04: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-15 03:50:56 +04:00
|
|
|
OwningPtr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(Opts.Triple));
|
2010-05-20 21:49:16 +04:00
|
|
|
assert(MAI && "Unable to create target asm info!");
|
|
|
|
|
2011-07-19 00:57:51 +04:00
|
|
|
OwningPtr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(Opts.Triple));
|
|
|
|
assert(MRI && "Unable to create target register info!");
|
|
|
|
|
2010-05-20 21:49:16 +04:00
|
|
|
bool IsBinary = Opts.OutputType == AssemblerInvocation::FT_Obj;
|
|
|
|
formatted_raw_ostream *Out = GetOutputStream(Opts, Diags, IsBinary);
|
|
|
|
if (!Out)
|
|
|
|
return false;
|
|
|
|
|
2011-07-20 10:22:27 +04: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());
|
2012-02-02 21:54:07 +04:00
|
|
|
MCContext Ctx(*MAI, *MRI, MOFI.get(), &SrcMgr);
|
2011-07-20 10:22:27 +04:00
|
|
|
// FIXME: Assembler behavior can change with -static.
|
2011-07-20 23:53:19 +04:00
|
|
|
MOFI->InitMCObjectFileInfo(Opts.Triple,
|
|
|
|
Reloc::Default, CodeModel::Default, Ctx);
|
2011-03-29 02:49:24 +04:00
|
|
|
if (Opts.SaveTemporaryLabels)
|
|
|
|
Ctx.setAllowTemporaryLabels(false);
|
2011-12-22 23:31:58 +04:00
|
|
|
if (Opts.GenDwarfForAssembly)
|
|
|
|
Ctx.setGenDwarfForAssembly(true);
|
|
|
|
if (!Opts.DwarfDebugFlags.empty())
|
|
|
|
Ctx.setDwarfDebugFlags(StringRef(Opts.DwarfDebugFlags));
|
2010-12-10 10:40:14 +03:00
|
|
|
|
2010-05-20 21:49:16 +04:00
|
|
|
OwningPtr<MCStreamer> Str;
|
|
|
|
|
2011-07-26 05:49:26 +04:00
|
|
|
OwningPtr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
|
|
|
|
OwningPtr<MCSubtargetInfo>
|
|
|
|
STI(TheTarget->createMCSubtargetInfo(Opts.Triple, "", ""));
|
2011-07-11 08:24:19 +04:00
|
|
|
|
2011-01-23 20:58:26 +03:00
|
|
|
// FIXME: There is a bit of code duplication with addPassesToEmitFile.
|
2010-05-20 21:49:16 +04:00
|
|
|
if (Opts.OutputType == AssemblerInvocation::FT_Asm) {
|
|
|
|
MCInstPrinter *IP =
|
2011-09-07 21:25:30 +04:00
|
|
|
TheTarget->createMCInstPrinter(Opts.OutputAsmVariant, *MAI, *STI);
|
2010-07-29 21:48:03 +04:00
|
|
|
MCCodeEmitter *CE = 0;
|
2011-07-26 03:25:09 +04:00
|
|
|
MCAsmBackend *MAB = 0;
|
2010-12-16 06:06:05 +03:00
|
|
|
if (Opts.ShowEncoding) {
|
2011-07-26 05:49:26 +04:00
|
|
|
CE = TheTarget->createMCCodeEmitter(*MCII, *STI, Ctx);
|
2011-07-26 03:25:09 +04:00
|
|
|
MAB = TheTarget->createMCAsmBackend(Opts.Triple);
|
2010-12-16 06:06:05 +03:00
|
|
|
}
|
2010-12-10 10:40:14 +03:00
|
|
|
Str.reset(TheTarget->createAsmStreamer(Ctx, *Out, /*asmverbose*/true,
|
2011-04-30 07:46:18 +04:00
|
|
|
/*useLoc*/ true,
|
2011-10-18 03:05:52 +04:00
|
|
|
/*useCFI*/ true,
|
|
|
|
/*useDwarfDirectory*/ true,
|
|
|
|
IP, CE, MAB,
|
2010-12-10 10:40:14 +03:00
|
|
|
Opts.ShowInst));
|
2010-05-20 21:49:16 +04:00
|
|
|
} else if (Opts.OutputType == AssemblerInvocation::FT_Null) {
|
|
|
|
Str.reset(createNullStreamer(Ctx));
|
|
|
|
} else {
|
|
|
|
assert(Opts.OutputType == AssemblerInvocation::FT_Obj &&
|
|
|
|
"Invalid file type!");
|
2011-07-26 05:49:26 +04:00
|
|
|
MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, *STI, Ctx);
|
2011-07-26 03:25:09 +04:00
|
|
|
MCAsmBackend *MAB = TheTarget->createMCAsmBackend(Opts.Triple);
|
2011-07-26 04:42:40 +04:00
|
|
|
Str.reset(TheTarget->createMCObjectStreamer(Opts.Triple, Ctx, *MAB, *Out,
|
|
|
|
CE, Opts.RelaxAll,
|
|
|
|
Opts.NoExecStack));
|
2010-10-13 18:53:57 +04:00
|
|
|
Str.get()->InitSections();
|
2010-05-20 21:49:16 +04:00
|
|
|
}
|
|
|
|
|
2011-08-16 22:33:55 +04:00
|
|
|
OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr, Ctx,
|
2010-07-17 06:26:21 +04:00
|
|
|
*Str.get(), *MAI));
|
2011-07-26 05:49:26 +04:00
|
|
|
OwningPtr<MCTargetAsmParser> TAP(TheTarget->createMCAsmParser(*STI, *Parser));
|
2010-05-20 21:49:16 +04:00
|
|
|
if (!TAP) {
|
|
|
|
Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-07-17 06:26:21 +04:00
|
|
|
Parser->setTargetParser(*TAP.get());
|
2010-05-20 21:49:16 +04:00
|
|
|
|
2010-07-17 06:26:21 +04:00
|
|
|
bool Success = !Parser->Run(Opts.NoInitialTextSection);
|
2010-05-20 21:49:16 +04:00
|
|
|
|
|
|
|
// Close the output.
|
|
|
|
delete Out;
|
|
|
|
|
|
|
|
// Delete output on errors.
|
|
|
|
if (!Success && Opts.OutputPath != "-")
|
|
|
|
sys::Path(Opts.OutputPath).eraseFromDisk();
|
|
|
|
|
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void LLVMErrorHandler(void *UserData, const std::string &Message) {
|
2011-09-26 03:23:43 +04:00
|
|
|
DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
|
2010-05-20 21:49:16 +04: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 01:59:11 +04:00
|
|
|
InitializeAllTargetMCs();
|
2010-05-20 21:49:16 +04:00
|
|
|
InitializeAllAsmParsers();
|
|
|
|
|
|
|
|
// Construct our diagnostic client.
|
2010-08-19 02:29:43 +04:00
|
|
|
TextDiagnosticPrinter *DiagClient
|
|
|
|
= new TextDiagnosticPrinter(errs(), DiagnosticOptions());
|
|
|
|
DiagClient->setPrefix("clang -cc1as");
|
2010-11-18 23:06:41 +03:00
|
|
|
llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
|
2011-09-26 03:23:43 +04:00
|
|
|
DiagnosticsEngine Diags(DiagID, DiagClient);
|
2010-05-20 21:49:16 +04:00
|
|
|
|
|
|
|
// Set an error handler, so that any LLVM backend diagnostics go through our
|
|
|
|
// error handler.
|
2010-08-19 01:23:17 +04:00
|
|
|
ScopedFatalErrorHandler FatalErrorHandler
|
|
|
|
(LLVMErrorHandler, static_cast<void*>(&Diags));
|
2010-05-20 21:49:16 +04:00
|
|
|
|
|
|
|
// Parse the arguments.
|
|
|
|
AssemblerInvocation Asm;
|
2011-12-23 07:05:38 +04:00
|
|
|
if (!AssemblerInvocation::CreateFromArgs(Asm, ArgBegin, ArgEnd, Diags))
|
|
|
|
return 1;
|
2010-05-20 21:49:16 +04:00
|
|
|
|
2010-05-20 22:15:20 +04:00
|
|
|
// Honor -help.
|
|
|
|
if (Asm.ShowHelp) {
|
|
|
|
llvm::OwningPtr<driver::OptTable> Opts(driver::createCC1AsOptTable());
|
|
|
|
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;
|
|
|
|
llvm::cl::ParseCommandLineOptions(NumArgs + 1, const_cast<char **>(Args));
|
|
|
|
}
|
|
|
|
|
2010-05-20 21:49:16 +04:00
|
|
|
// Execute the invocation, unless there were parsing errors.
|
|
|
|
bool Success = false;
|
2010-11-19 03:19:18 +03:00
|
|
|
if (!Diags.hasErrorOccurred())
|
2010-05-20 21:49:16 +04: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;
|
|
|
|
}
|