[clang] [llvm] Add plumbing for Xbox 360 target (PR #235915)
Lander Brandt via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 30 02:27:50 PDT 2026
https://github.com/landaire updated https://github.com/llvm/llvm-project/pull/235915
>From dd94e27a8755b6e6aafab6a55feca060e3380088 Mon Sep 17 00:00:00 2001
From: Lander Brandt <hello at landaire.net>
Date: Mon, 26 Oct 2026 02:10:14 -0700
Subject: [PATCH] [TargetParser] Recognize the Xbox 360 target
Clang treated xbox360 as an unknown OS and selected the normal powerpc64 type model. Black-box compiler probes require 32-bit pointers and long, 64-bit IEEE long double, 16-bit unsigned wchar_t, and four predefined target macros.
---
clang/lib/Basic/Targets.cpp | 2 +
clang/lib/Basic/Targets/OSTargets.h | 49 ++++++++++++++++---
clang/lib/Driver/ToolChains/Clang.cpp | 2 +-
clang/test/CodeGen/target-data.c | 4 ++
clang/test/Driver/xbox360.c | 5 ++
clang/test/Preprocessor/init-xbox360.c | 18 +++++++
llvm/include/llvm/TargetParser/Triple.h | 7 +++
llvm/include/llvm/TargetParser/TripleName.def | 1 +
llvm/lib/TargetParser/TargetDataLayout.cpp | 10 ++--
9 files changed, 84 insertions(+), 14 deletions(-)
create mode 100644 clang/test/Driver/xbox360.c
create mode 100644 clang/test/Preprocessor/init-xbox360.c
diff --git a/clang/lib/Basic/Targets.cpp b/clang/lib/Basic/Targets.cpp
index 6314cdf12728ac..287d6c1af36047 100644
--- a/clang/lib/Basic/Targets.cpp
+++ b/clang/lib/Basic/Targets.cpp
@@ -403,6 +403,8 @@ std::unique_ptr<TargetInfo> AllocateTarget(const llvm::Triple &Triple,
return std::make_unique<LinuxTargetInfo<PPC64TargetInfo>>(Triple, Opts);
case llvm::Triple::Lv2:
return std::make_unique<PS3PPUTargetInfo<PPC64TargetInfo>>(Triple, Opts);
+ case llvm::Triple::Xbox360:
+ return std::make_unique<Xbox360TargetInfo<PPC64TargetInfo>>(Triple, Opts);
case llvm::Triple::FreeBSD:
return std::make_unique<FreeBSDTargetInfo<PPC64TargetInfo>>(Triple, Opts);
case llvm::Triple::NetBSD:
diff --git a/clang/lib/Basic/Targets/OSTargets.h b/clang/lib/Basic/Targets/OSTargets.h
index eeb88370c9b584..80f8a955c86d17 100644
--- a/clang/lib/Basic/Targets/OSTargets.h
+++ b/clang/lib/Basic/Targets/OSTargets.h
@@ -556,9 +556,25 @@ class LLVM_LIBRARY_VISIBILITY OpenBSDTargetInfo : public OSTargetInfo<Target> {
}
};
+template <typename Target>
+class LLVM_LIBRARY_VISIBILITY PPC64ILP32TargetInfo
+ : public OSTargetInfo<Target> {
+public:
+ PPC64ILP32TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
+ : OSTargetInfo<Target>(Triple, Opts) {
+ this->LongWidth = this->LongAlign = 32;
+ this->PointerWidth = this->PointerAlign = 32;
+ this->IntMaxType = TargetInfo::SignedLongLong;
+ this->Int64Type = TargetInfo::SignedLongLong;
+ this->SizeType = TargetInfo::UnsignedInt;
+ this->resetDataLayout();
+ }
+};
+
// PS3 PPU Target
template <typename Target>
-class LLVM_LIBRARY_VISIBILITY PS3PPUTargetInfo : public OSTargetInfo<Target> {
+class LLVM_LIBRARY_VISIBILITY PS3PPUTargetInfo
+ : public PPC64ILP32TargetInfo<Target> {
protected:
void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
MacroBuilder &Builder) const override {
@@ -572,14 +588,31 @@ class LLVM_LIBRARY_VISIBILITY PS3PPUTargetInfo : public OSTargetInfo<Target> {
public:
PS3PPUTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
- : OSTargetInfo<Target>(Triple, Opts) {
- this->LongWidth = this->LongAlign = 32;
- this->PointerWidth = this->PointerAlign = 32;
- this->IntMaxType = TargetInfo::SignedLongLong;
- this->Int64Type = TargetInfo::SignedLongLong;
- this->SizeType = TargetInfo::UnsignedInt;
- this->resetDataLayout();
+ : PPC64ILP32TargetInfo<Target>(Triple, Opts) {}
+};
+
+template <typename Target>
+class LLVM_LIBRARY_VISIBILITY Xbox360TargetInfo
+ : public PPC64ILP32TargetInfo<Target> {
+protected:
+ void getOSDefines(const LangOptions &, const llvm::Triple &,
+ MacroBuilder &Builder) const override {
+ Builder.defineMacro("_XBOX_VER", "200");
+ Builder.defineMacro("_M_PPC", "5401");
+ Builder.defineMacro("_M_PPCBE");
+ Builder.defineMacro("_WIN32");
}
+
+public:
+ Xbox360TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
+ : PPC64ILP32TargetInfo<Target>(Triple, Opts) {
+ this->LongDoubleWidth = this->LongDoubleAlign = 64;
+ this->LongDoubleFormat = &llvm::APFloat::IEEEdouble();
+ this->WCharType = TargetInfo::UnsignedShort;
+ this->ABI.clear();
+ }
+
+ bool setABI(const std::string &) override { return false; }
};
// Common base class for PS4/PS5 targets.
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 5a1c8531812bd3..89a59afca2f4c3 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -2010,7 +2010,7 @@ void Clang::AddPPCTargetArgs(const ArgList &Args,
// Select the ABI to use.
const char *ABIName = nullptr;
- if (T.isOSBinFormatELF()) {
+ if (T.isOSBinFormatELF() && T.getOS() != llvm::Triple::Xbox360) {
switch (getToolChain().getArch()) {
case llvm::Triple::ppc64: {
if (T.isPPC64ELFv2ABI())
diff --git a/clang/test/CodeGen/target-data.c b/clang/test/CodeGen/target-data.c
index d738d5faf18617..fafc4ae7f99c63 100644
--- a/clang/test/CodeGen/target-data.c
+++ b/clang/test/CodeGen/target-data.c
@@ -90,6 +90,10 @@
// RUN: FileCheck %s -check-prefix=PS3
// PS3: target datalayout = "E-m:e-p:32:32-Fi64-i64:64-i128:128-n32:64"
+// RUN: %clang_cc1 -triple powerpc64-unknown-xbox360 -o - -emit-llvm %s | \
+// RUN: FileCheck %s -check-prefix=XBOX360
+// XBOX360: target datalayout = "E-m:e-p:32:32-Fn32-i64:64-i128:128-n32:64"
+
// RUN: %clang_cc1 -triple wasm32-unknown-unknown -o - -emit-llvm %s | \
// RUN: FileCheck %s -check-prefix=WEBASSEMBLY32
// WEBASSEMBLY32: target datalayout = "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-i128:128-n32:64-S128-ni:1:10:20"
diff --git a/clang/test/Driver/xbox360.c b/clang/test/Driver/xbox360.c
new file mode 100644
index 00000000000000..01d49c2c5d5a2e
--- /dev/null
+++ b/clang/test/Driver/xbox360.c
@@ -0,0 +1,5 @@
+// RUN: %clang -### -target powerpc64-unknown-xbox360 -c %s 2>&1 | FileCheck %s
+
+// CHECK: "-cc1"
+// CHECK: "-triple" "powerpc64-unknown-xbox360"
+// CHECK-NOT: "-target-abi"
diff --git a/clang/test/Preprocessor/init-xbox360.c b/clang/test/Preprocessor/init-xbox360.c
new file mode 100644
index 00000000000000..c4ab69d036e3ea
--- /dev/null
+++ b/clang/test/Preprocessor/init-xbox360.c
@@ -0,0 +1,18 @@
+// RUN: %clang_cc1 -E -dM -ffreestanding -triple=powerpc64-unknown-xbox360 < /dev/null | FileCheck -match-full-lines %s
+// RUN: %clang_cc1 -fsyntax-only -ffreestanding -triple=powerpc64-unknown-xbox360 %s
+
+// CHECK-DAG: #define _M_PPC 5401
+// CHECK-DAG: #define _M_PPCBE 1
+// CHECK-DAG: #define _WIN32 1
+// CHECK-DAG: #define _XBOX_VER 200
+
+// CHECK-DAG: #define __LDBL_MANT_DIG__ 53
+// CHECK-DAG: #define __SIZEOF_LONG_DOUBLE__ 8
+// CHECK-DAG: #define __SIZEOF_WCHAR_T__ 2
+// CHECK-DAG: #define __WCHAR_TYPE__ unsigned short
+// CHECK-DAG: #define __WCHAR_UNSIGNED__ 1
+
+_Static_assert(sizeof(long double) == 8, "");
+_Static_assert(_Alignof(long double) == 8, "");
+_Static_assert(sizeof(__WCHAR_TYPE__) == 2, "");
+_Static_assert((__WCHAR_TYPE__)-1 > 0, "");
diff --git a/llvm/include/llvm/TargetParser/Triple.h b/llvm/include/llvm/TargetParser/Triple.h
index ad1b654dc4076e..cbfaa5047c1364 100644
--- a/llvm/include/llvm/TargetParser/Triple.h
+++ b/llvm/include/llvm/TargetParser/Triple.h
@@ -310,6 +310,7 @@ class Triple {
KFreeBSD,
Linux,
Lv2, // PS3
+ Xbox360,
MacOSX,
Managarm,
NetBSD,
@@ -1153,6 +1154,12 @@ class Triple {
return getArch() == Triple::ppc64 || getArch() == Triple::ppc64le;
}
+ /// Tests whether the target uses the PPC64 ISA with 32-bit pointers.
+ bool isPPC64ILP32ABI() const {
+ return getArch() == Triple::ppc64 &&
+ (getOS() == Triple::Lv2 || getOS() == Triple::Xbox360);
+ }
+
/// Tests whether the target 64-bit PowerPC big endian ABI is ELFv2.
bool isPPC64ELFv2ABI() const {
return (
diff --git a/llvm/include/llvm/TargetParser/TripleName.def b/llvm/include/llvm/TargetParser/TripleName.def
index 95026bd39db786..ef063901bc307a 100644
--- a/llvm/include/llvm/TargetParser/TripleName.def
+++ b/llvm/include/llvm/TargetParser/TripleName.def
@@ -43,6 +43,7 @@ TRIPLE_OS(IOS, "ios", "iOS")
TRIPLE_OS(KFreeBSD, "kfreebsd", "FreeBSD")
TRIPLE_OS(Linux, "linux", "Linux")
TRIPLE_OS(Lv2, "lv2", "Generic")
+TRIPLE_OS(Xbox360, "xbox360", "Generic")
TRIPLE_OS(MacOSX, "macosx", "Darwin")
TRIPLE_OS_ALIAS(MacOSX, "macos")
TRIPLE_OS(Managarm, "managarm", "Generic")
diff --git a/llvm/lib/TargetParser/TargetDataLayout.cpp b/llvm/lib/TargetParser/TargetDataLayout.cpp
index 763cfb0fc65abc..e0554452ee61aa 100644
--- a/llvm/lib/TargetParser/TargetDataLayout.cpp
+++ b/llvm/lib/TargetParser/TargetDataLayout.cpp
@@ -200,16 +200,16 @@ static std::string computePowerDataLayout(const Triple &T, StringRef ABIName) {
Ret += T.isLittleEndian() ? 'e' : 'E';
Ret += getManglingComponent(T);
- // PPC32 has 32 bit pointers. The PS3 (OS Lv2) is a PPC64 machine with 32 bit
- // pointers.
- if (!is64Bit || T.getOS() == Triple::Lv2)
+ // PPC32 has 32-bit pointers. PS3 and Xbox 360 use the PPC64 ISA with
+ // 32-bit pointers.
+ if (!is64Bit || T.isPPC64ILP32ABI())
Ret += "-p:32:32";
// If the target ABI uses function descriptors, then the alignment of function
// pointers depends on the alignment used to emit the descriptor. Otherwise,
// function pointers are aligned to 32 bits because the instructions must be.
- if ((T.getArch() == Triple::ppc64 &&
- (!T.isPPC64ELFv2ABI() && ABIName != "elfv2"))) {
+ if (T.getArch() == Triple::ppc64 && T.getOS() != Triple::Xbox360 &&
+ (!T.isPPC64ELFv2ABI() && ABIName != "elfv2")) {
Ret += "-Fi64";
} else if (T.isOSAIX()) {
Ret += is64Bit ? "-Fi64" : "-Fi32";
More information about the cfe-commits
mailing list