Skip to content

Commit

Permalink
[MIPS] Add support for direct-to-nacl in Clang
Browse files Browse the repository at this point in the history
For Mips direct-to-nacl, the goal is to be close to le32 front-end and
use Mips32EL backend. This patch defines new NaClMips32ELTargetInfo and
modifies it slightly to be close to le32. It also adds necessary parts,
inline with ARM and X86.

Differential Revision: http://reviews.llvm.org/D10739

llvm-svn: 241678
  • Loading branch information
petar-jovanovic committed Jul 8, 2015
1 parent fb1662f commit 26a4a40
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 2 deletions.
15 changes: 14 additions & 1 deletion clang/lib/Basic/Targets.cpp
Expand Up @@ -6672,6 +6672,19 @@ void PNaClTargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
NumAliases = 0;
}

// We attempt to use PNaCl (le32) frontend and Mips32EL backend.
class NaClMips32ELTargetInfo : public Mips32ELTargetInfo {
public:
NaClMips32ELTargetInfo(const llvm::Triple &Triple) :
Mips32ELTargetInfo(Triple) {
MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
}

BuiltinVaListKind getBuiltinVaListKind() const override {
return TargetInfo::PNaClABIBuiltinVaList;
}
};

class Le64TargetInfo : public TargetInfo {
static const Builtin::Info BuiltinInfo[];

Expand Down Expand Up @@ -7042,7 +7055,7 @@ static TargetInfo *AllocateTarget(const llvm::Triple &Triple) {
case llvm::Triple::NetBSD:
return new NetBSDTargetInfo<Mips32ELTargetInfo>(Triple);
case llvm::Triple::NaCl:
return new NaClTargetInfo<Mips32ELTargetInfo>(Triple);
return new NaClTargetInfo<NaClMips32ELTargetInfo>(Triple);
default:
return new Mips32ELTargetInfo(Triple);
}
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/CodeGen/TargetInfo.cpp
Expand Up @@ -7099,6 +7099,8 @@ const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types));
case llvm::Triple::mips:
case llvm::Triple::mipsel:
if (Triple.getOS() == llvm::Triple::NaCl)
return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types));
return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));

case llvm::Triple::mips64:
Expand Down
14 changes: 14 additions & 0 deletions clang/lib/Driver/ToolChains.cpp
Expand Up @@ -2337,6 +2337,13 @@ NaCl_TC::NaCl_TC(const Driver &D, const llvm::Triple &Triple,
file_paths.push_back(ToolPath + "arm-nacl");
break;
}
case llvm::Triple::mipsel: {
file_paths.push_back(FilePath + "mipsel-nacl/lib");
file_paths.push_back(FilePath + "mipsel-nacl/usr/lib");
prog_paths.push_back(ProgPath + "bin");
file_paths.push_back(ToolPath + "mipsel-nacl");
break;
}
default:
break;
}
Expand Down Expand Up @@ -2372,6 +2379,9 @@ void NaCl_TC::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
case llvm::Triple::x86_64:
llvm::sys::path::append(P, "x86_64-nacl/usr/include");
break;
case llvm::Triple::mipsel:
llvm::sys::path::append(P, "mipsel-nacl/usr/include");
break;
default:
return;
}
Expand Down Expand Up @@ -2416,6 +2426,10 @@ void NaCl_TC::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
llvm::sys::path::append(P, "x86_64-nacl/include/c++/v1");
addSystemInclude(DriverArgs, CC1Args, P.str());
break;
case llvm::Triple::mipsel:
llvm::sys::path::append(P, "mipsel-nacl/include/c++/v1");
addSystemInclude(DriverArgs, CC1Args, P.str());
break;
default:
break;
}
Expand Down
4 changes: 3 additions & 1 deletion clang/lib/Driver/ToolChains.h
Expand Up @@ -747,7 +747,9 @@ class LLVM_LIBRARY_VISIBILITY NaCl_TC : public Generic_ELF {
void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &CmdArgs) const override;

bool IsIntegratedAssemblerDefault() const override { return false; }
bool IsIntegratedAssemblerDefault() const override {
return getTriple().getArch() == llvm::Triple::mipsel;
}

// Get the path to the file containing NaCl's ARM macros. It lives in NaCl_TC
// because the AssembleARM tool needs a const char * that it can pass around
Expand Down
16 changes: 16 additions & 0 deletions clang/lib/Driver/Tools.cpp
Expand Up @@ -8200,6 +8200,8 @@ void nacltools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
CmdArgs.push_back("armelf_nacl");
else if (Arch == llvm::Triple::x86_64)
CmdArgs.push_back("elf_x86_64_nacl");
else if (Arch == llvm::Triple::mipsel)
CmdArgs.push_back("mipselelf_nacl");
else
D.Diag(diag::err_target_unsupported_arch) << ToolChain.getArchName()
<< "Native Client";
Expand Down Expand Up @@ -8261,6 +8263,13 @@ void nacltools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
// in the group for C++.
if (Args.hasArg(options::OPT_pthread) ||
Args.hasArg(options::OPT_pthreads) || D.CCCIsCXX()) {
// Gold, used by Mips, handles nested groups differently than ld, and
// without '-lnacl' it prefers symbols from libpthread.a over libnacl.a,
// which is not a desired behaviour here.
// See https://sourceware.org/ml/binutils/2015-03/msg00034.html
if (getToolChain().getArch() == llvm::Triple::mipsel)
CmdArgs.push_back("-lnacl");

CmdArgs.push_back("-lpthread");
}

Expand All @@ -8271,6 +8280,13 @@ void nacltools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
else
CmdArgs.push_back("-lgcc_s");
CmdArgs.push_back("--no-as-needed");

// Mips needs to create and use pnacl_legacy library that contains
// definitions from bitcode/pnaclmm.c and definitions for
// __nacl_tp_tls_offset() and __nacl_tp_tdb_offset().
if (getToolChain().getArch() == llvm::Triple::mipsel)
CmdArgs.push_back("-lpnacl_legacy");

CmdArgs.push_back("--end-group");
}

Expand Down
35 changes: 35 additions & 0 deletions clang/test/Driver/nacl-direct.c
Expand Up @@ -63,6 +63,29 @@
// CHECK-ARM: "-L{{.*}}{{/|\\\\}}..{{/|\\\\}}arm-nacl{{/|\\\\}}usr{{/|\\\\}}lib"
// CHECK-ARM: "-Lfoo{{/|\\\\}}lib{{/|\\\\}}arm-nacl"
// CHECK-ARM-NOT: -lpthread
//
// RUN: %clang -no-canonical-prefixes -### -o %t.o %s \
// RUN: -target mipsel-unknown-nacl -resource-dir foo 2>&1 \
// RUN: | FileCheck --check-prefix=CHECK-MIPS %s
// CHECK-MIPS: {{.*}}clang{{.*}}" "-cc1"
// CHECK-MIPS: "-fuse-init-array"
// CHECK-MIPS: "-target-cpu" "mips32r2"
// CHECK-MIPS: "-target-abi" "o32"
// CHECK-MIPS: "-mfloat-abi" "hard"
// CHECK-MIPS: "-resource-dir" "foo"
// CHECK-MIPS: "-internal-isystem" "foo{{/|\\\\}}include"
// CHECK-MIPS: "-internal-isystem" "{{.*}}{{/|\\\\}}..{{/|\\\\}}mipsel-nacl{{/|\\\\}}usr{{/|\\\\}}include"
// CHECK-MIPS: "-internal-isystem" "{{.*}}{{/|\\\\}}..{{/|\\\\}}mipsel-nacl{{/|\\\\}}include"
// CHECK-MIPS-NOT: as{{(.exe)?}}"
// CHECK-MIPS: ld{{(.exe)?}}"
// CHECK-MIPS: "--build-id"
// CHECK-MIPS: "-m" "mipselelf_nacl"
// CHECK-MIPS: "-static"
// CHECK-MIPS: "-L{{.*}}{{/|\\\\}}..{{/|\\\\}}mipsel-nacl{{/|\\\\}}lib"
// CHECK-MIPS: "-L{{.*}}{{/|\\\\}}..{{/|\\\\}}mipsel-nacl{{/|\\\\}}usr{{/|\\\\}}lib"
// CHECK-MIPS: "-Lfoo{{/|\\\\}}lib{{/|\\\\}}mipsel-nacl"
// CHECK-MIPS: "-lpnacl_legacy"
// CHECK-MIPS-NOT: "-lpthread"

// Check that even when the target arch is just "arm" (as will be the case when
// it is inferred from the binary name) that we get the right ABI flags
Expand Down Expand Up @@ -109,3 +132,15 @@
// CHECK-x86_64-CXX: "-internal-isystem" "{{.*}}{{/|\\\\}}..{{/|\\\\}}x86_64-nacl{{/|\\\\}}usr{{/|\\\\}}include"
// CHECK-x86_64-CXX: "-internal-isystem" "{{.*}}{{/|\\\\}}..{{/|\\\\}}x86_64-nacl{{/|\\\\}}include"
// CHECK-x86_64-CXX: "-lpthread"

// RUN: %clangxx -no-canonical-prefixes -### -o %t.o %s \
// RUN: -target mipsel-unknown-nacl -resource-dir foo 2>&1 \
// RUN: | FileCheck --check-prefix=CHECK-MIPS-CXX %s
// CHECK-MIPS-CXX: {{.*}}clang{{.*}}" "-cc1"
// CHECK-MIPS-CXX: "-resource-dir" "foo"
// CHECK-MIPS-CXX: "-internal-isystem" "{{.*}}{{/|\\\\}}..{{/|\\\\}}mipsel-nacl{{/|\\\\}}include{{/|\\\\}}c++{{/|\\\\}}v1"
// CHECK-MIPS-CXX: "-internal-isystem" "foo{{/|\\\\}}include"
// CHECK-MIPS-CXX: "-internal-isystem" "{{.*}}{{/|\\\\}}..{{/|\\\\}}mipsel-nacl{{/|\\\\}}usr{{/|\\\\}}include"
// CHECK-MIPS-CXX: "-internal-isystem" "{{.*}}{{/|\\\\}}..{{/|\\\\}}mipsel-nacl{{/|\\\\}}include"
// CHECK-MIPS-CXX: "-lnacl"
// CHECK-MIPS-CXX: "-lpthread"

0 comments on commit 26a4a40

Please sign in to comment.