Skip to content

Instantly share code, notes, and snippets.

@pieter
Created March 19, 2009 22:27
Show Gist options
  • Save pieter/82101 to your computer and use it in GitHub Desktop.
Save pieter/82101 to your computer and use it in GitHub Desktop.
From df91737994f3afba1a150016be17c161174f54d5 Mon Sep 17 00:00:00 2001
From: Pieter de Bie <pdebie@ai.rug.nl>
Date: Thu, 19 Mar 2009 22:26:02 +0000
Subject: [PATCH] Driver: Add a Darwin/Binutils assembly tool
---
lib/Driver/HostInfo.cpp | 7 +----
lib/Driver/ToolChains.h | 39 ++++++++++++++++++++++++++++++
lib/Driver/Tools.cpp | 60 +++++++++++++++++++++++++++++++++++++++++++++++
lib/Driver/Tools.h | 18 ++++++++++++++
4 files changed, 118 insertions(+), 6 deletions(-)
diff --git a/lib/Driver/HostInfo.cpp b/lib/Driver/HostInfo.cpp
index a1dc149..a8fee60 100644
--- a/lib/Driver/HostInfo.cpp
+++ b/lib/Driver/HostInfo.cpp
@@ -105,12 +105,7 @@ ToolChain *DarwinHostInfo::getToolChain(const ArgList &Args,
ToolChain *&TC = ToolChains[ArchName];
if (!TC) {
- if (strcmp(ArchName, "i386") == 0 || strcmp(ArchName, "x86_64") == 0)
- TC = new toolchains::Generic_GCC(*this, ArchName,
- getPlatformName().c_str(),
- getOSName().c_str());
- else
- TC = new toolchains::Generic_GCC(*this, ArchName,
+ TC = new toolchains::Darwin_BinUtils(*this, ArchName,
getPlatformName().c_str(),
getOSName().c_str());
}
diff --git a/lib/Driver/ToolChains.h b/lib/Driver/ToolChains.h
index b879c18..e7da90c 100644
--- a/lib/Driver/ToolChains.h
+++ b/lib/Driver/ToolChains.h
@@ -26,6 +26,7 @@ namespace toolchains {
/// all subcommands; this relies on gcc translating the majority of
/// command line options.
class VISIBILITY_HIDDEN Generic_GCC : public ToolChain {
+protected:
mutable llvm::DenseMap<unsigned, Tool*> Tools;
public:
@@ -87,6 +88,44 @@ public:
}
};
+class VISIBILITY_HIDDEN Darwin_BinUtils : public Generic_GCC
+{
+public:
+ Darwin_BinUtils(const HostInfo &Host, const char *Arch, const char *Platform,
+ const char *OS) : Generic_GCC(Host, Arch, Platform, OS) {}
+
+ virtual Tool &SelectTool(const Compilation &C, const JobAction &JA) const {
+ Action::ActionClass Key;
+ if (ShouldUseClangCompiler(C, JA))
+ Key = Action::AnalyzeJobClass;
+ else
+ Key = JA.getKind();
+
+ Tool *&T = Tools[Key];
+ if (!T) {
+ switch (Key) {
+ default:
+ assert(0 && "Invalid tool kind.");
+ case Action::PreprocessJobClass:
+ T = new tools::gcc::Preprocess(*this); break;
+ case Action::PrecompileJobClass:
+ T = new tools::gcc::Precompile(*this); break;
+ case Action::AnalyzeJobClass:
+ T = new tools::Clang(*this); break;
+ case Action::CompileJobClass:
+ T = new tools::gcc::Compile(*this); break;
+ case Action::AssembleJobClass:
+ T = new tools::binutils::Assemble(*this); break;
+ case Action::LinkJobClass:
+ T = new tools::gcc::Link(*this); break;
+ }
+ }
+
+ return *T;
+ }
+
+
+};
} // end namespace toolchains
} // end namespace driver
} // end namespace clang
diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp
index 3453811..f6a76d9 100644
--- a/lib/Driver/Tools.cpp
+++ b/lib/Driver/Tools.cpp
@@ -437,3 +437,63 @@ void gcc::Link::RenderExtraToolArgs(ArgStringList &CmdArgs) const {
// The types are (hopefully) good enough.
}
+void binutils::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
+ Job &Dest,
+ const InputInfo &Output,
+ const InputInfoList &Inputs,
+ const ArgList &Args,
+ const char *LinkingOutput) const {
+ ArgStringList CmdArgs;
+
+ assert(Inputs.Length() == 1);
+ InputInfo Input = Inputs[0];
+
+ // If using a driver driver, force the arch.
+ if (getToolChain().getHost().useDriverDriver()) {
+ CmdArgs.push_back("-arch");
+ CmdArgs.push_back(getToolChain().getArchName().c_str());
+ }
+
+ CmdArgs.push_back("-force_cpusubtype_ALL");
+
+ if (Args.getLastArg(options::OPT_mkernel) ||
+ Args.getLastArg(options::OPT_static) ||
+ Args.getLastArg(options::OPT_fapple_kext))
+ {
+ if (!Args.getLastArg(options::OPT_dynamic))
+ CmdArgs.push_back("-static");
+ }
+
+ Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA);
+ Args.AddAllArgValues(CmdArgs, options::OPT_Xassembler);
+
+ if (Output.isPipe()) {
+ assert(0 && "We can't accept pipes!");
+ } else if (Output.isFilename()) {
+ CmdArgs.push_back("-o");
+ CmdArgs.push_back(Output.getFilename());
+ } else {
+ assert(Output.isNothing() && "Unexpected output");
+ CmdArgs.push_back("-fsyntax-only");
+ }
+
+ for (ArgList::const_iterator
+ it = Args.begin(), ie = Args.end(); it != ie; ++it) {
+ Arg *A = *it;
+ if (!A->isClaimed()) {
+ A->claim();
+ A->render(Args, CmdArgs);
+ }
+ }
+
+ if (Input.isPipe())
+ CmdArgs.push_back("--");
+ else if (Input.isFilename())
+ CmdArgs.push_back(Input.getFilename());
+ else
+ Input.getInputArg().renderAsInput(Args, CmdArgs);
+
+ const char *Exec =
+ Args.MakeArgString(getToolChain().GetProgramPath(C, "as").c_str());
+ Dest.addCommand(new Command(Exec, CmdArgs));
+}
diff --git a/lib/Driver/Tools.h b/lib/Driver/Tools.h
index a87a7ba..a3a48d8 100644
--- a/lib/Driver/Tools.h
+++ b/lib/Driver/Tools.h
@@ -110,6 +110,24 @@ namespace gcc {
};
} // end namespace gcc
+namespace binutils {
+ class VISIBILITY_HIDDEN Assemble : public Tool {
+ public:
+ Assemble(const ToolChain &TC) : Tool("binutils::Assemble", TC) {}
+
+ virtual bool acceptsPipedInput() const { return true; }
+ virtual bool canPipeOutput() const { return false; }
+ virtual bool hasIntegratedCPP() const { return false; }
+
+ virtual void ConstructJob(Compilation &C, const JobAction &JA,
+ Job &Dest,
+ const InputInfo &Output,
+ const InputInfoList &Inputs,
+ const ArgList &TCArgs,
+ const char *LinkingOutput) const;
+
+ };
+} // end namespace binutils
} // end namespace toolchains
} // end namespace driver
} // end namespace clang
--
1.6.2.1.337.g6270ba
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment