Skip to content

[AVR] standard library support for AVR #75127

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
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
1 change: 1 addition & 0 deletions cmake/modules/SwiftHandleGybSources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ function(handle_gyb_sources dependency_out_var_name sources_var_name)
if (GYB_ARCH)
set_if_arch_bitness(ptr_size
ARCH "${GYB_ARCH}"
CASE_16_BIT "2"
CASE_32_BIT "4"
CASE_64_BIT "8")
set(extra_gyb_flags "-DCMAKE_SIZEOF_VOID_P=${ptr_size}")
Expand Down
6 changes: 4 additions & 2 deletions cmake/modules/SwiftSetIfArchBitness.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ function(set_if_arch_bitness var_name)
cmake_parse_arguments(
SIA # prefix
"" # options
"ARCH;CASE_32_BIT;CASE_64_BIT" # single-value args
"ARCH;CASE_16_BIT;CASE_32_BIT;CASE_64_BIT" # single-value args
"" # multi-value args
${ARGN})

if("${SIA_ARCH}" STREQUAL "i386" OR
if("${SIA_ARCH}" STREQUAL "avr")
set("${var_name}" "${SIA_CASE_16_BIT}" PARENT_SCOPE)
elseif("${SIA_ARCH}" STREQUAL "i386" OR
"${SIA_ARCH}" STREQUAL "i686" OR
"${SIA_ARCH}" STREQUAL "x86" OR
"${SIA_ARCH}" STREQUAL "armv4t" OR
Expand Down
5 changes: 4 additions & 1 deletion lib/Basic/LangOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ static const SupportedConditionalValue SupportedConditionalCompilationEndianness
};

static const SupportedConditionalValue SupportedConditionalCompilationPointerBitWidths[] = {
"_16",
"_32",
"_64"
};
Expand Down Expand Up @@ -568,7 +569,9 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
}

// Set the "_pointerBitWidth" platform condition.
if (Target.isArch32Bit()) {
if (Target.isArch16Bit()) {
addPlatformConditionValue(PlatformConditionKind::PointerBitWidth, "_16");
} else if (Target.isArch32Bit()) {
addPlatformConditionValue(PlatformConditionKind::PointerBitWidth, "_32");
} else if (Target.isArch64Bit()) {
addPlatformConditionValue(PlatformConditionKind::PointerBitWidth, "_64");
Expand Down
4 changes: 2 additions & 2 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,14 +290,14 @@ getSwiftStdlibType(const clang::TypedefNameDecl *D,
break;

case MappedCTypeKind::UnsignedWord:
if (ClangTypeSize != 64 && ClangTypeSize != 32)
if (ClangTypeSize != 64 && ClangTypeSize != 32 && ClangTypeSize != 16)
return std::make_pair(Type(), "");
if (!ClangType->isUnsignedIntegerType())
return std::make_pair(Type(), "");
break;

case MappedCTypeKind::SignedWord:
if (ClangTypeSize != 64 && ClangTypeSize != 32)
if (ClangTypeSize != 64 && ClangTypeSize != 32 && ClangTypeSize != 16)
return std::make_pair(Type(), "");
if (!ClangType->isSignedIntegerType())
return std::make_pair(Type(), "");
Expand Down
6 changes: 6 additions & 0 deletions stdlib/public/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,12 @@ if(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB_CROSS_COMPILING)
"wasm64 wasm64-unknown-none-wasm wasm64-unknown-none-wasm"
)
endif()

if("AVR" IN_LIST LLVM_TARGETS_TO_BUILD)
list(APPEND EMBEDDED_STDLIB_TARGET_TRIPLES
"avr avr-none-none-elf avr-none-none-elf"
)
endif()
endif()

if(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB)
Expand Down
5 changes: 5 additions & 0 deletions stdlib/public/Synchronization/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ if(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB)
list(GET list 0 arch)
list(GET list 1 mod)
list(GET list 2 triple)

# Disable the Synchronization library on AVR for now.
if("${arch}" MATCHES "avr")
continue()
endif()

set(SWIFT_SDK_embedded_ARCH_${arch}_MODULE "${mod}")
set(SWIFT_SDK_embedded_LIB_SUBDIR "embedded")
Expand Down
2 changes: 2 additions & 0 deletions stdlib/public/core/Builtin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,8 @@ internal var _objectPointerIsObjCBit: UInt {
return 0x4000_0000_0000_0000
#elseif _pointerBitWidth(_32)
return 0x0000_0002
#elseif _pointerBitWidth(_16)
return 0x0000
#else
#error("Unknown platform")
#endif
Expand Down
12 changes: 10 additions & 2 deletions stdlib/public/core/CTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ public typealias CUnsignedChar = UInt8
public typealias CUnsignedShort = UInt16

/// The C 'unsigned int' type.
#if _pointerBitWidth(_16)
public typealias CUnsignedInt = UInt
#else
public typealias CUnsignedInt = UInt32
#endif

/// The C 'unsigned long' type.
#if os(Windows) && (arch(x86_64) || arch(arm64))
#if (os(Windows) && (arch(x86_64) || arch(arm64))) || _pointerBitWidth(_16)
public typealias CUnsignedLong = UInt32
#else
public typealias CUnsignedLong = UInt
Expand All @@ -44,10 +48,14 @@ public typealias CSignedChar = Int8
public typealias CShort = Int16

/// The C 'int' type.
#if _pointerBitWidth(_16)
public typealias CInt = Int
#else
public typealias CInt = Int32
#endif

/// The C 'long' type.
#if os(Windows) && (arch(x86_64) || arch(arm64))
#if (os(Windows) && (arch(x86_64) || arch(arm64))) || _pointerBitWidth(_16)
public typealias CLong = Int32
#else
public typealias CLong = Int
Expand Down
9 changes: 7 additions & 2 deletions stdlib/public/core/EmbeddedRuntime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ public struct HeapObject {
#if _pointerBitWidth(_64)
static let doNotFreeBit = Int(bitPattern: 0x8000_0000_0000_0000)
static let refcountMask = Int(bitPattern: 0x7fff_ffff_ffff_ffff)
#else
#elseif _pointerBitWidth(_32)
static let doNotFreeBit = Int(bitPattern: 0x8000_0000)
static let refcountMask = Int(bitPattern: 0x7fff_ffff)
#elseif _pointerBitWidth(_16)
static let doNotFreeBit = Int(bitPattern: 0x8000)
static let refcountMask = Int(bitPattern: 0x7fff)
#endif

// Note: The immortalRefCount value of -1 is also hard-coded in IRGen in `irgen::emitConstantObject`.
Expand All @@ -55,8 +58,10 @@ public struct HeapObject {

#if _pointerBitWidth(_64)
static let bridgeObjectToPlainObjectMask = UInt(0x8fff_ffff_ffff_fff8)
#else
#elseif _pointerBitWidth(_32)
static let bridgeObjectToPlainObjectMask = UInt(0xffff_ffff)
#elseif _pointerBitWidth(_16)
static let bridgeObjectToPlainObjectMask = UInt(0xffff)
#endif
}

Expand Down
4 changes: 3 additions & 1 deletion stdlib/public/core/Hasher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ extension Hasher {
combine(UInt64(truncatingIfNeeded: value))
#elseif _pointerBitWidth(_32)
combine(UInt32(truncatingIfNeeded: value))
#elseif _pointerBitWidth(_16)
combine(UInt16(truncatingIfNeeded: value))
#else
#error("Unknown platform")
#endif
Expand Down Expand Up @@ -439,7 +441,7 @@ public struct Hasher {
_internalInvariant(UInt.bitWidth == UInt64.bitWidth)
state.compress(UInt64(truncatingIfNeeded: value))
let tbc = _TailBuffer(tail: 0, byteCount: 8)
#elseif _pointerBitWidth(_32)
#elseif _pointerBitWidth(_32) || _pointerBitWidth(_16)
_internalInvariant(UInt.bitWidth < UInt64.bitWidth)
let tbc = _TailBuffer(
tail: UInt64(truncatingIfNeeded: value),
Expand Down
2 changes: 2 additions & 0 deletions stdlib/public/core/Int128.swift
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,8 @@ extension Int128: BinaryInteger {
UInt(Builtin.trunc_Int128_Int64(_value))
#elseif _pointerBitWidth(_32)
UInt(Builtin.trunc_Int128_Int32(_value))
#elseif _pointerBitWidth(_16)
UInt(Builtin.trunc_Int128_Int16(_value))
#else
#error("Unsupported platform")
#endif
Expand Down
8 changes: 6 additions & 2 deletions stdlib/public/core/IntegerTypes.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -1659,7 +1659,9 @@ ${assignmentOperatorComment(x.operator, True)}
@_transparent
public // @testable
init(_ _v: Builtin.Word) {
% if BuiltinName == 'Int32':
% if BuiltinName == 'Int16':
self._value = Builtin.truncOrBitCast_Word_Int16(_v)
% elif BuiltinName == 'Int32':
self._value = Builtin.truncOrBitCast_Word_Int32(_v)
% elif BuiltinName == 'Int64':
self._value = Builtin.${z}extOrBitCast_Word_Int64(_v)
Expand All @@ -1669,7 +1671,9 @@ ${assignmentOperatorComment(x.operator, True)}
@_transparent
public // @testable
var _builtinWordValue: Builtin.Word {
% if BuiltinName == 'Int32':
% if BuiltinName == 'Int16':
return Builtin.${z}extOrBitCast_Int16_Word(_value)
% elif BuiltinName == 'Int32':
return Builtin.${z}extOrBitCast_Int32_Word(_value)
% elif BuiltinName == 'Int64':
return Builtin.truncOrBitCast_Int64_Word(_value)
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/SmallString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ internal struct _SmallString {
extension _SmallString {
@inlinable @inline(__always)
internal static var capacity: Int {
#if _pointerBitWidth(_32)
#if _pointerBitWidth(_32) || _pointerBitWidth(_16)
return 10
#elseif os(Android) && arch(arm64)
return 14
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/StringGuts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ extension _StringGuts {
the runtime is depending on this, update Reflection.mm and \
this if you change it
""")
#elseif _pointerBitWidth(_32)
#elseif _pointerBitWidth(_32) || _pointerBitWidth(_16)
_internalInvariant(MemoryLayout<String>.size == 12, """
the runtime is depending on this, update Reflection.mm and \
this if you change it
Expand Down
Loading