Skip to content

Support Linux on z as a Swift platform #2541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
May 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,8 @@ else()
set(SWIFT_HOST_VARIANT_ARCH_default "powerpc64")
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64le")
set(SWIFT_HOST_VARIANT_ARCH_default "powerpc64le")
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "s390x")
set(SWIFT_HOST_VARIANT_ARCH_default "s390x")
# FIXME: Only matches v6l/v7l - by far the most common variants
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv6l")
set(SWIFT_HOST_VARIANT_ARCH_default "armv6")
Expand Down Expand Up @@ -507,6 +509,8 @@ if("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "LINUX")
set(SWIFT_HOST_TRIPLE "aarch64-unknown-linux-gnu")
elseif("${SWIFT_HOST_VARIANT_ARCH}" MATCHES "(powerpc64|powerpc64le)")
set(SWIFT_HOST_TRIPLE "${SWIFT_HOST_VARIANT_ARCH}-unknown-linux-gnu")
elseif("${SWIFT_HOST_VARIANT_ARCH}" MATCHES "s390x")
set(SWIFT_HOST_TRIPLE "s390x-unknown-linux-gnu")
elseif("${SWIFT_HOST_VARIANT_ARCH}" MATCHES "(armv6|armv7)")
set(SWIFT_HOST_TRIPLE "${SWIFT_HOST_VARIANT_ARCH}-unknown-linux-gnueabihf")
else()
Expand Down
3 changes: 2 additions & 1 deletion cmake/modules/SwiftSetIfArchBitness.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ function(set_if_arch_bitness var_name)
"${SIA_ARCH}" STREQUAL "arm64" OR
"${SIA_ARCH}" STREQUAL "aarch64" OR
"${SIA_ARCH}" STREQUAL "powerpc64" OR
"${SIA_ARCH}" STREQUAL "powerpc64le")
"${SIA_ARCH}" STREQUAL "powerpc64le" OR
"${SIA_ARCH}" STREQUAL "s390x")
set("${var_name}" "${SIA_CASE_64_BIT}" PARENT_SCOPE)
else()
message(FATAL_ERROR "Unknown architecture: ${SIA_ARCH}")
Expand Down
5 changes: 5 additions & 0 deletions include/swift/ABI/System.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,9 @@
// Heap objects are pointer-aligned, so the low three bits are unused.
#define SWIFT_ABI_POWERPC64_SWIFT_SPARE_BITS_MASK 0x0000000000000007ULL

/*********************************** s390x ************************************/

// Top byte of pointers is unused, and heap objects are eight-byte aligned.
#define SWIFT_ABI_S390X_SWIFT_SPARE_BITS_MASK 0x0000000000000007ULL

#endif /* SWIFT_ABI_SYSTEM_H */
4 changes: 4 additions & 0 deletions include/swift/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ namespace swift {
/// a supported target architecture.
static bool isPlatformConditionArchSupported(StringRef ArchName);

/// Returns true if the 'endian' platform condition argument represents
/// a supported target endianness.
static bool isPlatformConditionEndiannessSupported(StringRef endianness);

private:
llvm::SmallVector<std::pair<std::string, std::string>, 3>
PlatformConditionValues;
Expand Down
4 changes: 4 additions & 0 deletions include/swift/Runtime/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@
/// Does the current Swift platform use LLVM's intrinsic "swiftcall"
/// calling convention for Swift functions?
#ifndef SWIFT_USE_SWIFTCALL
#ifdef __s390x__
#define SWIFT_USE_SWIFTCALL 1
#else
#define SWIFT_USE_SWIFTCALL 0
#endif
#endif

/// Does the current Swift platform allow information other than the
/// class pointer to be stored in the isa field? If so, when deriving
Expand Down
6 changes: 4 additions & 2 deletions include/swift/Runtime/HeapObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,12 @@ using BoxPair = TwoWordPair<HeapObject *, OpaqueValue *>;
/// The heap object has an initial retain count of 1, and its metadata is set
/// such that destroying the heap object destroys the contained value.
SWIFT_RUNTIME_EXPORT
extern "C" BoxPair::Return swift_allocBox(Metadata const *type);
extern "C" BoxPair::Return swift_allocBox(Metadata const *type)
SWIFT_CC(swift);

SWIFT_RUNTIME_EXPORT
extern "C" BoxPair::Return (*_swift_allocBox)(Metadata const *type);
extern "C" BoxPair::Return (*_swift_allocBox)(Metadata const *type)
SWIFT_CC(swift);


// Allocate plain old memory. This is the generalized entry point
Expand Down
11 changes: 11 additions & 0 deletions include/swift/Runtime/Metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,17 @@ static const uintptr_t ObjCReservedBitsMask =
static const unsigned ObjCReservedLowBits =
SWIFT_ABI_DEFAULT_OBJC_NUM_RESERVED_LOW_BITS;

#elif defined(__s390x__)

static const uintptr_t LeastValidPointerValue =
SWIFT_ABI_DEFAULT_LEAST_VALID_POINTER;
static const uintptr_t SwiftSpareBitsMask =
SWIFT_ABI_S390X_SWIFT_SPARE_BITS_MASK;
static const uintptr_t ObjCReservedBitsMask =
SWIFT_ABI_DEFAULT_OBJC_RESERVED_BITS_MASK;
static const unsigned ObjCReservedLowBits =
SWIFT_ABI_DEFAULT_OBJC_NUM_RESERVED_LOW_BITS;

#else

static const uintptr_t LeastValidPointerValue =
Expand Down
47 changes: 46 additions & 1 deletion lib/Basic/LangOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ static const StringRef SupportedConditionalCompilationArches[] = {
"i386",
"x86_64",
"powerpc64",
"powerpc64le"
"powerpc64le",
"s390x"
};

static const StringRef SupportedConditionalCompilationEndianness[] = {
"little",
"big"
};

bool LangOptions::isPlatformConditionOSSupported(StringRef OSName) {
Expand All @@ -58,6 +64,14 @@ LangOptions::isPlatformConditionArchSupported(StringRef ArchName) {
return foundIt != std::end(SupportedConditionalCompilationArches);
}

bool
LangOptions::isPlatformConditionEndiannessSupported(StringRef Endianness) {
auto foundIt = std::find(std::begin(SupportedConditionalCompilationEndianness),
std::end(SupportedConditionalCompilationEndianness),
Endianness);
return foundIt != std::end(SupportedConditionalCompilationEndianness);
}

StringRef
LangOptions::getPlatformConditionValue(StringRef Name) const {
// Last one wins.
Expand Down Expand Up @@ -141,13 +155,44 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
case llvm::Triple::ArchType::x86_64:
addPlatformConditionValue("arch", "x86_64");
break;
case llvm::Triple::ArchType::systemz:
addPlatformConditionValue("arch", "s390x");
break;
default:
UnsupportedArch = true;
}

if (UnsupportedOS || UnsupportedArch)
return { UnsupportedOS, UnsupportedArch };

// Set the "_endian" platform condition.
switch (Target.getArch()) {
case llvm::Triple::ArchType::arm:
case llvm::Triple::ArchType::thumb:
addPlatformConditionValue("_endian", "little");
break;
case llvm::Triple::ArchType::aarch64:
addPlatformConditionValue("_endian", "little");
break;
case llvm::Triple::ArchType::ppc64:
addPlatformConditionValue("_endian", "big");
break;
case llvm::Triple::ArchType::ppc64le:
addPlatformConditionValue("_endian", "little");
break;
case llvm::Triple::ArchType::x86:
addPlatformConditionValue("_endian", "little");
break;
case llvm::Triple::ArchType::x86_64:
addPlatformConditionValue("_endian", "little");
break;
case llvm::Triple::ArchType::systemz:
addPlatformConditionValue("_endian", "big");
break;
default:
llvm_unreachable("undefined architecture endianness");
}

// Set the "runtime" platform condition.
if (EnableObjCInterop)
addPlatformConditionValue("_runtime", "_ObjC");
Expand Down
24 changes: 14 additions & 10 deletions lib/IRGen/GenMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4338,16 +4338,20 @@ irgen::emitClassResilientInstanceSizeAndAlignMask(IRGenFunction &IGF,
&& "didn't find size or alignment in metadata?!");
Address metadataAsBytes(IGF.Builder.CreateBitCast(metadata, IGF.IGM.Int8PtrTy),
IGF.IGM.getPointerAlignment());
auto loadZExtInt32AtOffset = [&](Size offset) {
Address slot = IGF.Builder.CreateConstByteArrayGEP(metadataAsBytes, offset);
slot = IGF.Builder.CreateBitCast(slot, IGF.IGM.Int32Ty->getPointerTo());
llvm::Value *result = IGF.Builder.CreateLoad(slot);
if (IGF.IGM.SizeTy != IGF.IGM.Int32Ty)
result = IGF.Builder.CreateZExt(result, IGF.IGM.SizeTy);
return result;
};
llvm::Value *size = loadZExtInt32AtOffset(scanner.InstanceSize);
llvm::Value *alignMask = loadZExtInt32AtOffset(scanner.InstanceAlignMask);

Address slot = IGF.Builder.CreateConstByteArrayGEP(metadataAsBytes,
scanner.InstanceSize);
slot = IGF.Builder.CreateBitCast(slot, IGF.IGM.Int32Ty->getPointerTo());
llvm::Value *size = IGF.Builder.CreateLoad(slot);
if (IGF.IGM.SizeTy != IGF.IGM.Int32Ty)
size = IGF.Builder.CreateZExt(size, IGF.IGM.SizeTy);

slot = IGF.Builder.CreateConstByteArrayGEP(metadataAsBytes,
scanner.InstanceAlignMask);
slot = IGF.Builder.CreateBitCast(slot, IGF.IGM.Int16Ty->getPointerTo());
llvm::Value *alignMask = IGF.Builder.CreateLoad(slot);
alignMask = IGF.Builder.CreateZExt(alignMask, IGF.IGM.SizeTy);

return {size, alignMask};
}

Expand Down
11 changes: 11 additions & 0 deletions lib/IRGen/SwiftTargetInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ static void configurePowerPC64(IRGenModule &IGM, const llvm::Triple &triple,
SWIFT_ABI_POWERPC64_SWIFT_SPARE_BITS_MASK);
}

/// Configures target-specific information for SystemZ platforms.
static void configureSystemZ(IRGenModule &IGM, const llvm::Triple &triple,
SwiftTargetInfo &target) {
setToMask(target.PointerSpareBits, 64,
SWIFT_ABI_S390X_SWIFT_SPARE_BITS_MASK);
}

/// Configure a default target.
SwiftTargetInfo::SwiftTargetInfo(
llvm::Triple::ObjectFormatType outputObjectFormat,
Expand Down Expand Up @@ -154,6 +161,10 @@ SwiftTargetInfo SwiftTargetInfo::get(IRGenModule &IGM) {
configurePowerPC64(IGM, triple, target);
break;

case llvm::Triple::systemz:
configureSystemZ(IGM, triple, target);
break;

default:
// FIXME: Complain here? Default target info is unlikely to be correct.
break;
Expand Down
8 changes: 7 additions & 1 deletion lib/Parse/ParseStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1608,7 +1608,7 @@ ParserResult<Stmt> Parser::parseStmtGuard() {
// "_compiler_version"), and whose argument is a named decl ref expression
ConditionalCompilationExprState
Parser::evaluateConditionalCompilationExpr(Expr *condition) {
// Evaluate a ParenExpr.
// Evaluate a ParenExpr.
if (auto *PE = dyn_cast<ParenExpr>(condition))
return evaluateConditionalCompilationExpr(PE->getSubExpr());

Expand Down Expand Up @@ -1709,6 +1709,7 @@ Parser::evaluateConditionalCompilationExpr(Expr *condition) {
}

if (!fnName.equals("arch") && !fnName.equals("os") &&
!fnName.equals("_endian") &&
!fnName.equals("_runtime") &&
!fnName.equals("swift") &&
!fnName.equals("_compiler_version")) {
Expand Down Expand Up @@ -1799,6 +1800,11 @@ Parser::evaluateConditionalCompilationExpr(Expr *condition) {
"architecture", fnName);
return ConditionalCompilationExprState::error();
}
} else if (fnName == "_endian") {
if (!LangOptions::isPlatformConditionEndiannessSupported(argument)) {
diagnose(UDRE->getLoc(), diag::unknown_platform_condition_argument,
"endianness", fnName);
}
}
auto target = Context.LangOpts.getPlatformConditionValue(fnName);
return {target == argument, ConditionalCompilationExprKind::DeclRef};
Expand Down
2 changes: 1 addition & 1 deletion stdlib/private/SwiftPrivate/PRNG.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public func rand64() -> UInt64 {
public func randInt() -> Int {
#if arch(i386) || arch(arm)
return Int(Int32(bitPattern: rand32()))
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le)
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
return Int(Int64(bitPattern: rand64()))
#else
fatalError("unimplemented")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ public var _stdlib_FD_SETSIZE: CInt {
}

public struct _stdlib_fd_set {
var _data: [UInt32]
var _data: [UInt]
static var _wordBits: Int {
return sizeof(UInt32) * 8
return sizeof(UInt) * 8
}

public init() {
_data = [UInt32](
_data = [UInt](
repeating: 0,
count: Int(_stdlib_FD_SETSIZE) / _stdlib_fd_set._wordBits)
}
Expand All @@ -53,20 +53,20 @@ public struct _stdlib_fd_set {
let fdInt = Int(fd)
return (
_data[fdInt / _stdlib_fd_set._wordBits] &
UInt32(1 << (fdInt % _stdlib_fd_set._wordBits))
UInt(1 << (fdInt % _stdlib_fd_set._wordBits))
) != 0
}

public mutating func set(_ fd: CInt) {
let fdInt = Int(fd)
_data[fdInt / _stdlib_fd_set._wordBits] |=
UInt32(1 << (fdInt % _stdlib_fd_set._wordBits))
UInt(1 << (fdInt % _stdlib_fd_set._wordBits))
}

public mutating func clear(_ fd: CInt) {
let fdInt = Int(fd)
_data[fdInt / _stdlib_fd_set._wordBits] &=
~UInt32(1 << (fdInt % _stdlib_fd_set._wordBits))
~UInt(1 << (fdInt % _stdlib_fd_set._wordBits))
}

public mutating func zero() {
Expand Down
13 changes: 13 additions & 0 deletions stdlib/public/core/Builtin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,19 @@ internal var _objectPointerLowSpareBitShift: UInt {
internal var _objCTaggedPointerBits: UInt {
@inline(__always) get { return 0 }
}
#elseif arch(s390x)
internal var _objectPointerSpareBits: UInt {
@inline(__always) get { return 0x0000_0000_0000_0007 }
}
internal var _objectPointerIsObjCBit: UInt {
@inline(__always) get { return 0x0000_0000_0000_0002 }
}
internal var _objectPointerLowSpareBitShift: UInt {
@inline(__always) get { return 0 }
}
internal var _objCTaggedPointerBits: UInt {
@inline(__always) get { return 0 }
}
#endif

/// Extract the raw bits of `x`.
Expand Down
24 changes: 8 additions & 16 deletions stdlib/public/core/FixedPoint.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -265,25 +265,21 @@ public struct ${Self}
/// byte order if necessary.
@_transparent public
init(bigEndian value: ${Self}) {
#if arch(i386) || arch(x86_64) || arch(arm) || arch(arm64) || arch(powerpc64le)
self = ${Self}(Builtin.int_bswap_${BuiltinName}(value._value) )
#elseif arch(powerpc64)
#if _endian(big)
self = value
#else
_UnsupportedArchitectureError()
self = ${Self}(Builtin.int_bswap_${BuiltinName}(value._value) )
#endif
}

/// Creates an integer from its little-endian representation, changing the
/// byte order if necessary.
@_transparent public
init(littleEndian value: ${Self}) {
#if arch(i386) || arch(x86_64) || arch(arm) || arch(arm64) || arch(powerpc64le)
#if _endian(little)
self = value
#elseif arch(powerpc64)
self = ${Self}(Builtin.int_bswap_${BuiltinName}(value._value) )
#else
_UnsupportedArchitectureError()
self = ${Self}(Builtin.int_bswap_${BuiltinName}(value._value) )
#endif
}
% end
Expand All @@ -303,23 +299,19 @@ public struct ${Self}
/// Returns the big-endian representation of the integer, changing the
/// byte order if necessary.
public var bigEndian: ${Self} {
#if arch(i386) || arch(x86_64) || arch(arm) || arch(arm64) || arch(powerpc64le)
return ${Self}(Builtin.int_bswap_${BuiltinName}(_value))
#elseif arch(powerpc64)
#if _endian(big)
return self
#else
_UnsupportedArchitectureError()
return ${Self}(Builtin.int_bswap_${BuiltinName}(_value))
#endif
}
/// Returns the little-endian representation of the integer, changing the
/// byte order if necessary.
public var littleEndian: ${Self} {
#if arch(i386) || arch(x86_64) || arch(arm) || arch(arm64) || arch(powerpc64le)
#if _endian(little)
return self
#elseif arch(powerpc64)
return ${Self}(Builtin.int_bswap_${BuiltinName}(_value))
#else
_UnsupportedArchitectureError()
return ${Self}(Builtin.int_bswap_${BuiltinName}(_value))
#endif
}
% end
Expand Down
Loading