Skip to content

Instantly share code, notes, and snippets.

@colemancda
Last active March 19, 2022 07:42
Show Gist options
  • Save colemancda/562f954d32869b2bdeb49be88e63c59a to your computer and use it in GitHub Desktop.
Save colemancda/562f954d32869b2bdeb49be88e63c59a to your computer and use it in GitHub Desktop.
Swift 5.5.3 patched for Mips
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 7a3cc3af07a..d2f6f88c6c7 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -285,7 +285,7 @@ set(SWIFT_ANDROID_DEPLOY_DEVICE_PATH "" CACHE STRING
#
foreach(sdk ANDROID;FREEBSD;LINUX;WINDOWS;HAIKU;WASI)
- foreach(arch aarch64;armv6;armv7;i686;powerpc64;powerpc64le;s390x;wasm32;x86_64)
+ foreach(arch aarch64;armv6;armv7;i686;powerpc64;powerpc64le;s390x;wasm32;x86_64;mips;mipsel;mips64;mips64el)
set(SWIFT_${sdk}_${arch}_ICU_UC "" CACHE STRING
"Path to a directory containing the icuuc library for ${sdk}")
set(SWIFT_${sdk}_${arch}_ICU_UC_INCLUDE "" CACHE STRING
@@ -636,6 +636,14 @@ else()
set(SWIFT_HOST_VARIANT_ARCH_default "powerpc64le")
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "s390x")
set(SWIFT_HOST_VARIANT_ARCH_default "s390x")
+ elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "mips")
+ set(SWIFT_HOST_VARIANT_ARCH_default "mips")
+ elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "mipsel")
+ set(SWIFT_HOST_VARIANT_ARCH_default "mipsel")
+ elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "mips64")
+ set(SWIFT_HOST_VARIANT_ARCH_default "mips64")
+ elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "mips64el")
+ set(SWIFT_HOST_VARIANT_ARCH_default "mips64el")
# FIXME: Only matches v6l/v7l - by far the most common variants
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv6l")
set(SWIFT_HOST_VARIANT_ARCH_default "armv6")
diff --git a/cmake/modules/AddSwift.cmake b/cmake/modules/AddSwift.cmake
index f244b6b46f6..e8cd7a0ca30 100644
--- a/cmake/modules/AddSwift.cmake
+++ b/cmake/modules/AddSwift.cmake
@@ -286,7 +286,7 @@ function(_add_host_variant_c_compile_flags target)
# The LLVM backend is built with these defines on most 32-bit platforms,
# llvm/llvm-project@66395c9, which can cause incompatibilities with the Swift
# frontend if not built the same way.
- if("${SWIFT_HOST_VARIANT_ARCH}" MATCHES "armv6|armv7|i686" AND
+ if("${SWIFT_HOST_VARIANT_ARCH}" MATCHES "mipsel|armv6|armv7|i686" AND
NOT (SWIFT_HOST_VARIANT_SDK STREQUAL ANDROID AND SWIFT_ANDROID_API_LEVEL LESS 24))
target_compile_definitions(${target} PRIVATE
_LARGEFILE_SOURCE
@@ -301,7 +301,7 @@ function(_add_host_variant_link_flags target)
target_link_libraries(${target} PRIVATE
pthread
dl)
- if("${SWIFT_HOST_VARIANT_ARCH}" MATCHES "armv6|armv7|i686")
+ if("${SWIFT_HOST_VARIANT_ARCH}" MATCHES "mipsel|armv6|armv7|i686")
target_link_libraries(${target} PRIVATE atomic)
endif()
elseif(SWIFT_HOST_VARIANT_SDK STREQUAL FREEBSD)
diff --git a/cmake/modules/AddSwiftUnittests.cmake b/cmake/modules/AddSwiftUnittests.cmake
index 0690973abd3..fc61a4920c4 100644
--- a/cmake/modules/AddSwiftUnittests.cmake
+++ b/cmake/modules/AddSwiftUnittests.cmake
@@ -38,7 +38,7 @@ function(add_swift_unittest test_dirname)
if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64")
target_compile_options(${test_dirname} PRIVATE
-march=core2)
- elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "armv6|armv7|i686")
+ elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "mips|mipsel|armv6|armv7|i686")
set_property(TARGET "${test_dirname}" APPEND PROPERTY LINK_LIBRARIES
"atomic")
endif()
diff --git a/cmake/modules/SwiftConfigureSDK.cmake b/cmake/modules/SwiftConfigureSDK.cmake
index 8a32c12abb1..f685cef922b 100644
--- a/cmake/modules/SwiftConfigureSDK.cmake
+++ b/cmake/modules/SwiftConfigureSDK.cmake
@@ -337,7 +337,7 @@ macro(configure_sdk_unix name architectures)
if("${prefix}" STREQUAL "LINUX")
if(arch MATCHES "(armv6|armv7)")
set(SWIFT_SDK_LINUX_ARCH_${arch}_TRIPLE "${arch}-unknown-linux-gnueabihf")
- elseif(arch MATCHES "(aarch64|i686|powerpc64|powerpc64le|s390x|x86_64)")
+ elseif(arch MATCHES "(aarch64|i686|powerpc64|powerpc64le|s390x|x86_64|mips|mipsel|mips64|mips64el)")
set(SWIFT_SDK_LINUX_ARCH_${arch}_TRIPLE "${arch}-unknown-linux-gnu")
else()
message(FATAL_ERROR "unknown arch for ${prefix}: ${arch}")
diff --git a/cmake/modules/SwiftSetIfArchBitness.cmake b/cmake/modules/SwiftSetIfArchBitness.cmake
index fa29d77fe26..a2107380ac8 100644
--- a/cmake/modules/SwiftSetIfArchBitness.cmake
+++ b/cmake/modules/SwiftSetIfArchBitness.cmake
@@ -9,6 +9,8 @@ function(set_if_arch_bitness var_name)
if("${SIA_ARCH}" STREQUAL "i386" OR
"${SIA_ARCH}" STREQUAL "i686" OR
"${SIA_ARCH}" STREQUAL "x86" OR
+ "${SIA_ARCH}" STREQUAL "mips" OR
+ "${SIA_ARCH}" STREQUAL "mipsel" OR
"${SIA_ARCH}" STREQUAL "armv6" OR
"${SIA_ARCH}" STREQUAL "armv7" OR
"${SIA_ARCH}" STREQUAL "armv7k" OR
@@ -23,6 +25,8 @@ function(set_if_arch_bitness var_name)
"${SIA_ARCH}" STREQUAL "aarch64" OR
"${SIA_ARCH}" STREQUAL "powerpc64" OR
"${SIA_ARCH}" STREQUAL "powerpc64le" OR
+ "${SIA_ARCH}" STREQUAL "mips64" OR
+ "${SIA_ARCH}" STREQUAL "mips64el" OR
"${SIA_ARCH}" STREQUAL "s390x")
set("${var_name}" "${SIA_CASE_64_BIT}" PARENT_SCOPE)
else()
diff --git a/lib/Basic/LangOptions.cpp b/lib/Basic/LangOptions.cpp
index 041d6a637b1..a00706b8367 100644
--- a/lib/Basic/LangOptions.cpp
+++ b/lib/Basic/LangOptions.cpp
@@ -63,6 +63,10 @@ static const SupportedConditionalValue SupportedConditionalCompilationArches[] =
"x86_64",
"powerpc64",
"powerpc64le",
+ "mips",
+ "mipsel",
+ "mips64",
+ "mips64el",
"s390x",
"wasm32",
};
@@ -317,6 +321,18 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
case llvm::Triple::ArchType::ppc64le:
addPlatformConditionValue(PlatformConditionKind::Arch, "powerpc64le");
break;
+ case llvm::Triple::ArchType::mips:
+ addPlatformConditionValue(PlatformConditionKind::Arch, "mips");
+ break;
+ case llvm::Triple::ArchType::mipsel:
+ addPlatformConditionValue(PlatformConditionKind::Arch, "mipsel");
+ break;
+ case llvm::Triple::ArchType::mips64:
+ addPlatformConditionValue(PlatformConditionKind::Arch, "mips64");
+ break;
+ case llvm::Triple::ArchType::mips64el:
+ addPlatformConditionValue(PlatformConditionKind::Arch, "mips64el");
+ break;
case llvm::Triple::ArchType::x86:
addPlatformConditionValue(PlatformConditionKind::Arch, "i386");
break;
@@ -347,8 +363,12 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
case llvm::Triple::ArchType::wasm32:
case llvm::Triple::ArchType::x86:
case llvm::Triple::ArchType::x86_64:
+ case llvm::Triple::ArchType::mipsel:
+ case llvm::Triple::ArchType::mips64el:
addPlatformConditionValue(PlatformConditionKind::Endianness, "little");
break;
+ case llvm::Triple::ArchType::mips:
+ case llvm::Triple::ArchType::mips64:
case llvm::Triple::ArchType::ppc64:
case llvm::Triple::ArchType::systemz:
addPlatformConditionValue(PlatformConditionKind::Endianness, "big");
diff --git a/stdlib/public/core/AtomicInt.swift.gyb b/stdlib/public/core/AtomicInt.swift.gyb
index a66212f4b35..297d2879805 100644
--- a/stdlib/public/core/AtomicInt.swift.gyb
+++ b/stdlib/public/core/AtomicInt.swift.gyb
@@ -65,10 +65,10 @@ internal func _swift_stdlib_atomicCompareExchangeStrongInt(
object target: UnsafeMutablePointer<Int>,
expected: UnsafeMutablePointer<Int>,
desired: Int) -> Bool {
-#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32)
+#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32) || arch(mips) || arch(mipsel)
let (oldValue, won) = Builtin.cmpxchg_seqcst_seqcst_Int32(
target._rawValue, expected.pointee._value, desired._value)
-#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
+#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(mips64) || arch(mips64el)
let (oldValue, won) = Builtin.cmpxchg_seqcst_seqcst_Int64(
target._rawValue, expected.pointee._value, desired._value)
#endif
@@ -82,10 +82,10 @@ internal func _swift_stdlib_atomicCompareExchangeStrongInt(
public // Existing uses outside stdlib
func _swift_stdlib_atomicLoadInt(
object target: UnsafeMutablePointer<Int>) -> Int {
-#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32)
+#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32) || arch(mips) || arch(mipsel)
let value = Builtin.atomicload_seqcst_Int32(target._rawValue)
return Int(value)
-#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
+#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(mips64) || arch(mips64el)
let value = Builtin.atomicload_seqcst_Int64(target._rawValue)
return Int(value)
#endif
@@ -95,9 +95,9 @@ func _swift_stdlib_atomicLoadInt(
internal func _swift_stdlib_atomicStoreInt(
object target: UnsafeMutablePointer<Int>,
desired: Int) {
-#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32)
+#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32) || arch(mips) || arch(mipsel)
Builtin.atomicstore_seqcst_Int32(target._rawValue, desired._value)
-#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
+#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(mips64) || arch(mips64el)
Builtin.atomicstore_seqcst_Int64(target._rawValue, desired._value)
#endif
}
@@ -111,11 +111,11 @@ func _swift_stdlib_atomicFetch${operation}Int(
object target: UnsafeMutablePointer<Int>,
operand: Int) -> Int {
let rawTarget = UnsafeMutableRawPointer(target)
-#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32)
+#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32) || arch(mips) || arch(mipsel)
let value = _swift_stdlib_atomicFetch${operation}Int32(
object: rawTarget.assumingMemoryBound(to: Int32.self),
operand: Int32(operand))
-#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
+#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(mips64) || arch(mips64el)
let value = _swift_stdlib_atomicFetch${operation}Int64(
object: rawTarget.assumingMemoryBound(to: Int64.self),
operand: Int64(operand))
diff --git a/stdlib/public/core/Builtin.swift b/stdlib/public/core/Builtin.swift
index 29c17834204..0a640255de3 100644
--- a/stdlib/public/core/Builtin.swift
+++ b/stdlib/public/core/Builtin.swift
@@ -390,8 +390,7 @@ internal var _objectPointerLowSpareBitShift: UInt {
}
}
-#if arch(i386) || arch(arm) || arch(wasm32) || arch(powerpc64) || arch(
- powerpc64le) || arch(s390x) || arch(arm64_32)
+#if arch(i386) || arch(arm) || arch(wasm32) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(arm64_32) || arch(mips) || arch(mipsel) || arch(mips64) || arch(mips64el)
@inlinable
internal var _objectPointerIsObjCBit: UInt {
@inline(__always) get { return 0x0000_0002 }
diff --git a/stdlib/public/stubs/MathStubs.cpp b/stdlib/public/stubs/MathStubs.cpp
index 8d0617089ca..6ecf9641c81 100644
--- a/stdlib/public/stubs/MathStubs.cpp
+++ b/stdlib/public/stubs/MathStubs.cpp
@@ -112,7 +112,7 @@ __muloti4(ti_int a, ti_int b, int* overflow)
// lowered to instructions as though MSVC had generated. There does not seem to
// be a MSVC provided multiply with overflow detection that I can see, but this
// avoids an unnecessary dependency on compiler-rt for a single function.
-#if (defined(__linux__) && (defined(__arm__) || defined(__i386__))) || defined(_WIN32)
+#if (defined(__linux__) && (defined(__arm__) || defined(__i386__) || (defined(__mips__) && !defined(__LP64__)))) || defined(_WIN32)
// Similar to above, but with mulodi4. Perhaps this is
// something that shouldn't be done, and is a bandaid over
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment