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-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"
|
|
|
|
#include "llvm/MC/MCStreamer.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"
|
|
|
|
#include "llvm/Support/Timer.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/System/Host.h"
|
|
|
|
#include "llvm/System/Path.h"
|
|
|
|
#include "llvm/System/Signals.h"
|
|
|
|
#include "llvm/Target/TargetAsmBackend.h"
|
|
|
|
#include "llvm/Target/TargetAsmParser.h"
|
|
|
|
#include "llvm/Target/TargetData.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetRegistry.h"
|
|
|
|
#include "llvm/Target/TargetSelect.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;
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
/// @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;
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void CreateFromArgs(AssemblerInvocation &Res, const char **ArgBegin,
|
|
|
|
const char **ArgEnd, Diagnostic &Diags);
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,
|
|
|
|
const char **ArgBegin,
|
|
|
|
const char **ArgEnd,
|
|
|
|
Diagnostic &Diags) {
|
|
|
|
using namespace clang::driver::cc1asoptions;
|
|
|
|
// Parse the arguments.
|
|
|
|
OwningPtr<OptTable> OptTbl(createCC1AsOptTable());
|
|
|
|
unsigned MissingArgIndex, MissingArgCount;
|
|
|
|
OwningPtr<InputArgList> Args(
|
|
|
|
OptTbl->ParseArgs(ArgBegin, ArgEnd,MissingArgIndex, MissingArgCount));
|
|
|
|
|
|
|
|
// Check for missing argument error.
|
|
|
|
if (MissingArgCount)
|
|
|
|
Diags.Report(diag::err_drv_missing_argument)
|
|
|
|
<< Args->getArgString(MissingArgIndex) << MissingArgCount;
|
|
|
|
|
|
|
|
// Issue errors on unknown arguments.
|
|
|
|
for (arg_iterator it = Args->filtered_begin(cc1asoptions::OPT_UNKNOWN),
|
|
|
|
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);
|
2010-05-20 21:49:16 +04:00
|
|
|
|
|
|
|
// Construct the invocation.
|
|
|
|
|
|
|
|
// Target Options
|
|
|
|
Opts.Triple = Args->getLastArgValue(OPT_triple);
|
|
|
|
if (Opts.Triple.empty()) // Use the host triple if unspecified.
|
|
|
|
Opts.Triple = sys::getHostTriple();
|
|
|
|
|
|
|
|
// Language Options
|
|
|
|
Opts.IncludePaths = Args->getAllArgValues(OPT_I);
|
|
|
|
Opts.NoInitialTextSection = Args->hasArg(OPT_n);
|
|
|
|
|
|
|
|
// 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);
|
2010-05-20 21:49:16 +04:00
|
|
|
else
|
2010-06-12 02:00:13 +04:00
|
|
|
Diags.Report(diag::err_drv_unknown_argument) << A->getAsString(*Args);
|
2010-05-20 21:49:16 +04:00
|
|
|
}
|
|
|
|
}
|
2010-05-20 22:15:20 +04:00
|
|
|
Opts.LLVMArgs = Args->getAllArgValues(OPT_mllvm);
|
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);
|
|
|
|
if (OutputType == ~0U)
|
|
|
|
Diags.Report(diag::err_drv_invalid_value)
|
|
|
|
<< A->getAsString(*Args) << Name;
|
|
|
|
else
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static formatted_raw_ostream *GetOutputStream(AssemblerInvocation &Opts,
|
|
|
|
Diagnostic &Diags,
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ExecuteAssembler(AssemblerInvocation &Opts, Diagnostic &Diags) {
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(Opts.InputFile, &Error);
|
|
|
|
if (Buffer == 0) {
|
|
|
|
Diags.Report(diag::err_fe_error_reading) << Opts.InputFile;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
OwningPtr<MCAsmInfo> MAI(TheTarget->createAsmInfo(Opts.Triple));
|
|
|
|
assert(MAI && "Unable to create target asm info!");
|
|
|
|
|
|
|
|
MCContext Ctx(*MAI);
|
|
|
|
bool IsBinary = Opts.OutputType == AssemblerInvocation::FT_Obj;
|
|
|
|
formatted_raw_ostream *Out = GetOutputStream(Opts, Diags, IsBinary);
|
|
|
|
if (!Out)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// FIXME: We shouldn't need to do this (and link in codegen).
|
|
|
|
OwningPtr<TargetMachine> TM(TheTarget->createTargetMachine(Opts.Triple, ""));
|
|
|
|
if (!TM) {
|
|
|
|
Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
OwningPtr<MCStreamer> Str;
|
|
|
|
|
|
|
|
if (Opts.OutputType == AssemblerInvocation::FT_Asm) {
|
|
|
|
MCInstPrinter *IP =
|
|
|
|
TheTarget->createMCInstPrinter(Opts.OutputAsmVariant, *MAI);
|
2010-07-29 21:48:03 +04:00
|
|
|
MCCodeEmitter *CE = 0;
|
2010-05-20 21:49:16 +04:00
|
|
|
if (Opts.ShowEncoding)
|
2010-07-29 21:48:03 +04:00
|
|
|
CE = TheTarget->createCodeEmitter(*TM, Ctx);
|
2010-05-20 21:49:16 +04:00
|
|
|
Str.reset(createAsmStreamer(Ctx, *Out,TM->getTargetData()->isLittleEndian(),
|
2010-07-29 21:48:03 +04:00
|
|
|
/*asmverbose*/true, IP, CE, 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!");
|
2010-07-29 21:48:03 +04:00
|
|
|
MCCodeEmitter *CE = TheTarget->createCodeEmitter(*TM, Ctx);
|
|
|
|
TargetAsmBackend *TAB = TheTarget->createAsmBackend(Opts.Triple);
|
|
|
|
Str.reset(TheTarget->createObjectStreamer(Opts.Triple, Ctx, *TAB, *Out,
|
|
|
|
CE, Opts.RelaxAll));
|
2010-05-20 21:49:16 +04:00
|
|
|
}
|
|
|
|
|
2010-07-17 06:26:21 +04:00
|
|
|
OwningPtr<MCAsmParser> Parser(createMCAsmParser(*TheTarget, SrcMgr, Ctx,
|
|
|
|
*Str.get(), *MAI));
|
2010-07-19 04:33:53 +04:00
|
|
|
OwningPtr<TargetAsmParser> TAP(TheTarget->createAsmParser(*Parser, *TM));
|
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) {
|
|
|
|
Diagnostic &Diags = *static_cast<Diagnostic*>(UserData);
|
|
|
|
|
|
|
|
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();
|
|
|
|
// FIXME: We shouldn't need to initialize the Target(Machine)s.
|
|
|
|
InitializeAllTargets();
|
|
|
|
InitializeAllAsmPrinters();
|
|
|
|
InitializeAllAsmParsers();
|
|
|
|
|
|
|
|
// Construct our diagnostic client.
|
|
|
|
TextDiagnosticPrinter DiagClient(errs(), DiagnosticOptions());
|
|
|
|
DiagClient.setPrefix("clang -cc1as");
|
|
|
|
Diagnostic Diags(&DiagClient);
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
AssemblerInvocation::CreateFromArgs(Asm, ArgBegin, ArgEnd, Diags);
|
|
|
|
|
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;
|
|
|
|
if (!Diags.getNumErrors())
|
|
|
|
Success = ExecuteAssembler(Asm, Diags);
|
|
|
|
|
|
|
|
// If any timers were active but haven't been destroyed yet, print their
|
|
|
|
// results now.
|
|
|
|
TimerGroup::printAll(errs());
|
|
|
|
|
|
|
|
return !Success;
|
|
|
|
}
|