From 0aa994298d516263361c0bc3dab7a9db2d5d3016 Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Sun, 6 Nov 2022 22:00:55 -0800 Subject: [PATCH] [stdlib] Added MIPS support --- cmake/modules/AddSwift.cmake | 4 ++-- cmake/modules/AddSwiftUnittests.cmake | 2 +- cmake/modules/SwiftConfigureSDK.cmake | 2 +- cmake/modules/SwiftSetIfArchBitness.cmake | 4 ++++ lib/Basic/LangOptions.cpp | 16 ++++++++++++++++ stdlib/public/core/AtomicInt.swift.gyb | 16 ++++++++-------- stdlib/public/core/Builtin.swift | 3 +-- stdlib/public/stubs/MathStubs.cpp | 3 ++- .../basicParseErrors.swift | 3 +-- 9 files changed, 36 insertions(+), 17 deletions(-) diff --git a/cmake/modules/AddSwift.cmake b/cmake/modules/AddSwift.cmake index 6d93e52f1d5bd..1d75bb7975b12 100644 --- a/cmake/modules/AddSwift.cmake +++ b/cmake/modules/AddSwift.cmake @@ -330,7 +330,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 "mips|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 _FILE_OFFSET_BITS=64>) @@ -344,7 +344,7 @@ function(_add_host_variant_link_flags target) target_link_libraries(${target} PRIVATE pthread dl) - if("${SWIFT_HOST_VARIANT_ARCH}" MATCHES "armv5|armv6|armv7|i686") + if("${SWIFT_HOST_VARIANT_ARCH}" MATCHES "mips|mipsel|armv5|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 4b9dcf11197b3..d9133095d8610 100644 --- a/cmake/modules/AddSwiftUnittests.cmake +++ b/cmake/modules/AddSwiftUnittests.cmake @@ -37,7 +37,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 "armv5|armv6|armv7|i686") + elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "mips|mipsel|armv5|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 967326b0bd387..e8637f4e3042f 100644 --- a/cmake/modules/SwiftConfigureSDK.cmake +++ b/cmake/modules/SwiftConfigureSDK.cmake @@ -323,7 +323,7 @@ macro(configure_sdk_unix name architectures) set(SWIFT_SDK_LINUX_ARCH_${arch}_TRIPLE "${arch}-unknown-linux-gnueabi") elseif(arch MATCHES "(armv6|armv7)") set(SWIFT_SDK_LINUX_ARCH_${arch}_TRIPLE "${arch}-unknown-linux-gnueabihf") - elseif(arch MATCHES "(aarch64|i686|powerpc|powerpc64|powerpc64le|s390x|x86_64|riscv64)") + elseif(arch MATCHES "(aarch64|i686|powerpc|powerpc64|powerpc64le|s390x|x86_64|riscv64|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 9038302669e85..8844312a2bfdc 100644 --- a/cmake/modules/SwiftSetIfArchBitness.cmake +++ b/cmake/modules/SwiftSetIfArchBitness.cmake @@ -10,6 +10,8 @@ function(set_if_arch_bitness var_name) "${SIA_ARCH}" STREQUAL "i686" OR "${SIA_ARCH}" STREQUAL "x86" OR "${SIA_ARCH}" STREQUAL "armv5" 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 @@ -25,6 +27,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" OR "${SIA_ARCH}" STREQUAL "riscv64") set("${var_name}" "${SIA_CASE_64_BIT}" PARENT_SCOPE) diff --git a/lib/Basic/LangOptions.cpp b/lib/Basic/LangOptions.cpp index 9727180dff470..3dcf9b6249a49 100644 --- a/lib/Basic/LangOptions.cpp +++ b/lib/Basic/LangOptions.cpp @@ -65,6 +65,10 @@ static const SupportedConditionalValue SupportedConditionalCompilationArches[] = "powerpc", "powerpc64", "powerpc64le", + "mips", + "mipsel", + "mips64", + "mips64el", "s390x", "wasm32", "riscv64", @@ -323,6 +327,18 @@ std::pair 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; diff --git a/stdlib/public/core/AtomicInt.swift.gyb b/stdlib/public/core/AtomicInt.swift.gyb index e367a62cc523d..77cf268be1ed2 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, expected: UnsafeMutablePointer, desired: Int) -> Bool { -#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32) || arch(powerpc) +#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32) || arch(mips) || arch(mipsel) || arch(powerpc) 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) || arch(riscv64) +#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(riscv64) || 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 { -#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32) || arch(powerpc) +#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32) || arch(mips) || arch(mipsel) || arch(powerpc) let value = Builtin.atomicload_seqcst_Int32(target._rawValue) return Int(value) -#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(riscv64) +#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(riscv64) || 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, desired: Int) { -#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32) || arch(powerpc) +#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32) || arch(mips) || arch(mipsel) || arch(powerpc) Builtin.atomicstore_seqcst_Int32(target._rawValue, desired._value) -#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(riscv64) +#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(riscv64) || 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, operand: Int) -> Int { let rawTarget = UnsafeMutableRawPointer(target) -#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32) || arch(powerpc) +#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32) || arch(mips) || arch(mipsel) || arch(powerpc) 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) || arch(riscv64) +#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(riscv64) || 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 ab89aea45dc32..95568148303cf 100644 --- a/stdlib/public/core/Builtin.swift +++ b/stdlib/public/core/Builtin.swift @@ -394,8 +394,7 @@ internal var _objectPointerLowSpareBitShift: UInt { } } -#if arch(i386) || arch(arm) || arch(wasm32) || arch(powerpc) || arch(powerpc64) || arch( - powerpc64le) || arch(s390x) || arch(arm64_32) +#if arch(i386) || arch(arm) || arch(wasm32) || arch(powerpc) || 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 211100c70e182..dc734e9b8f410 100644 --- a/stdlib/public/stubs/MathStubs.cpp +++ b/stdlib/public/stubs/MathStubs.cpp @@ -113,7 +113,8 @@ __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(__powerpc__))) || defined(_WIN32) + +#if (defined(__linux__) && (defined(__arm__) || defined(__i386__) || defined(__powerpc__) || (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 diff --git a/test/Parse/ConditionalCompilation/basicParseErrors.swift b/test/Parse/ConditionalCompilation/basicParseErrors.swift index 43c2685cddab1..79d05219b8591 100644 --- a/test/Parse/ConditionalCompilation/basicParseErrors.swift +++ b/test/Parse/ConditionalCompilation/basicParseErrors.swift @@ -134,8 +134,7 @@ fn_k() // expected-note@-2 {{did you mean 'macOS'?}} {{8-16=macOS}} // expected-note@-3 {{did you mean 'iOS'?}} {{8-16=iOS}} // expected-warning@-4 {{unknown architecture for build configuration 'arch'}} -// expected-note@-5 {{did you mean 'arm'?}} {{26-29=arm}} -// expected-note@-6 {{did you mean 'i386'?}} {{26-29=i386}} +// expected-note@-5 {{did you mean 'mips'?}} {{26-29=mips}} func undefinedFunc() // ignored. #endif undefinedFunc() // expected-error {{cannot find 'undefinedFunc' in scope}}