Skip to content

Commit 3689427

Browse files
Carl PetoCarl Peto
authored andcommitted
[AVR] standard library support for AVR
- when compiling embedded cross compile target standard libraries, include AVR - add 16-bit pointer as a conditional compilation condition and get the void pointer size right for gyb sources - attempt to fix clang importer not importing __swift_intptr_t correctly on 16 bit platforms - changed the unit test target to avr-none-none-elf to match the cmake build [AVR] got the standard library compiling in a somewhat restricted form: General - updated the Embedded Runtime - tweaked CTypes.swift to fix clang import on 16 bit platforms Strings - as discussed in https://forums.swift.org/t/stringguts-stringobject-internals-how-to-layout-on-16-bit-platforms/73130, I went for just using the same basic layout in 16 bit as 32 bit but with 16 bit pointers/ints... the conversation is ongoing, I think something more efficient is possible but at least this compiles and will probably work (inefficiently) Unicode - the huge arrays of unicode stuff in UnicodeStubs would not compile, so I skipped it for AVR for now. Synchronization - disabled building the Synchronization library on AVR for now. It's arguable if it adds value on this platform anyway.
1 parent a861fc1 commit 3689427

21 files changed

+117
-44
lines changed

cmake/modules/SwiftHandleGybSources.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ function(handle_gyb_sources dependency_out_var_name sources_var_name)
109109
if (GYB_ARCH)
110110
set_if_arch_bitness(ptr_size
111111
ARCH "${GYB_ARCH}"
112+
CASE_16_BIT "2"
112113
CASE_32_BIT "4"
113114
CASE_64_BIT "8")
114115
set(extra_gyb_flags "-DCMAKE_SIZEOF_VOID_P=${ptr_size}")

cmake/modules/SwiftSetIfArchBitness.cmake

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ function(set_if_arch_bitness var_name)
22
cmake_parse_arguments(
33
SIA # prefix
44
"" # options
5-
"ARCH;CASE_32_BIT;CASE_64_BIT" # single-value args
5+
"ARCH;CASE_16_BIT;CASE_32_BIT;CASE_64_BIT" # single-value args
66
"" # multi-value args
77
${ARGN})
88

9-
if("${SIA_ARCH}" STREQUAL "i386" OR
9+
if("${SIA_ARCH}" STREQUAL "avr")
10+
set("${var_name}" "${SIA_CASE_16_BIT}" PARENT_SCOPE)
11+
elseif("${SIA_ARCH}" STREQUAL "i386" OR
1012
"${SIA_ARCH}" STREQUAL "i686" OR
1113
"${SIA_ARCH}" STREQUAL "x86" OR
1214
"${SIA_ARCH}" STREQUAL "armv4t" OR

lib/Basic/LangOptions.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ static const SupportedConditionalValue SupportedConditionalCompilationEndianness
110110
};
111111

112112
static const SupportedConditionalValue SupportedConditionalCompilationPointerBitWidths[] = {
113+
"_16",
113114
"_32",
114115
"_64"
115116
};
@@ -568,7 +569,9 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
568569
}
569570

570571
// Set the "_pointerBitWidth" platform condition.
571-
if (Target.isArch32Bit()) {
572+
if (Target.isArch16Bit()) {
573+
addPlatformConditionValue(PlatformConditionKind::PointerBitWidth, "_16");
574+
} else if (Target.isArch32Bit()) {
572575
addPlatformConditionValue(PlatformConditionKind::PointerBitWidth, "_32");
573576
} else if (Target.isArch64Bit()) {
574577
addPlatformConditionValue(PlatformConditionKind::PointerBitWidth, "_64");

lib/ClangImporter/ImportDecl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,14 +290,14 @@ getSwiftStdlibType(const clang::TypedefNameDecl *D,
290290
break;
291291

292292
case MappedCTypeKind::UnsignedWord:
293-
if (ClangTypeSize != 64 && ClangTypeSize != 32)
293+
if (ClangTypeSize != 64 && ClangTypeSize != 32 && ClangTypeSize != 16)
294294
return std::make_pair(Type(), "");
295295
if (!ClangType->isUnsignedIntegerType())
296296
return std::make_pair(Type(), "");
297297
break;
298298

299299
case MappedCTypeKind::SignedWord:
300-
if (ClangTypeSize != 64 && ClangTypeSize != 32)
300+
if (ClangTypeSize != 64 && ClangTypeSize != 32 && ClangTypeSize != 16)
301301
return std::make_pair(Type(), "");
302302
if (!ClangType->isSignedIntegerType())
303303
return std::make_pair(Type(), "");

stdlib/public/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,12 @@ if(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB_CROSS_COMPILING)
214214
"wasm64 wasm64-unknown-none-wasm wasm64-unknown-none-wasm"
215215
)
216216
endif()
217+
218+
if("AVR" IN_LIST LLVM_TARGETS_TO_BUILD)
219+
list(APPEND EMBEDDED_STDLIB_TARGET_TRIPLES
220+
"avr avr-none-none-elf avr-none-none-elf"
221+
)
222+
endif()
217223
endif()
218224

219225
if(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB)

stdlib/public/Synchronization/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ if(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB)
142142
list(GET list 0 arch)
143143
list(GET list 1 mod)
144144
list(GET list 2 triple)
145+
146+
# Disable the Synchronization library on AVR for now.
147+
if("${arch}" MATCHES "avr")
148+
continue()
149+
endif()
145150

146151
set(SWIFT_SDK_embedded_ARCH_${arch}_MODULE "${mod}")
147152
set(SWIFT_SDK_embedded_LIB_SUBDIR "embedded")

stdlib/public/core/AtomicInt.swift.gyb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ internal func _swift_stdlib_atomicCompareExchangeStrongInt(
7474
#elseif _pointerBitWidth(_32)
7575
let (oldValue, won) = Builtin.cmpxchg_seqcst_seqcst_Int32(
7676
target._rawValue, expected.pointee._value, desired._value)
77+
#elseif _pointerBitWidth(_16)
78+
let (oldValue, won) = Builtin.cmpxchg_seqcst_seqcst_Int16(
79+
target._rawValue, expected.pointee._value, desired._value)
7780
#else
7881
#error("Unknown platform")
7982
#endif
@@ -93,6 +96,9 @@ func _swift_stdlib_atomicLoadInt(
9396
#elseif _pointerBitWidth(_32)
9497
let value = Builtin.atomicload_seqcst_Int32(target._rawValue)
9598
return Int(value)
99+
#elseif _pointerBitWidth(_16)
100+
let value = Builtin.atomicload_seqcst_Int16(target._rawValue)
101+
return Int(value)
96102
#else
97103
#error("Unknown platform")
98104
#endif
@@ -106,6 +112,8 @@ internal func _swift_stdlib_atomicStoreInt(
106112
Builtin.atomicstore_seqcst_Int64(target._rawValue, desired._value)
107113
#elseif _pointerBitWidth(_32)
108114
Builtin.atomicstore_seqcst_Int32(target._rawValue, desired._value)
115+
#elseif _pointerBitWidth(_16)
116+
Builtin.atomicstore_seqcst_Int16(target._rawValue, desired._value)
109117
#else
110118
#error("Unknown platform")
111119
#endif
@@ -128,13 +136,17 @@ func _swift_stdlib_atomicFetch${operation}Int(
128136
let value = _swift_stdlib_atomicFetch${operation}Int32(
129137
object: rawTarget.assumingMemoryBound(to: Int32.self),
130138
operand: Int32(operand))
139+
#elseif _pointerBitWidth(_16)
140+
let value = _swift_stdlib_atomicFetch${operation}Int16(
141+
object: rawTarget.assumingMemoryBound(to: Int16.self),
142+
operand: Int16(operand))
131143
#else
132144
#error("Unknown platform")
133145
#endif
134146
return Int(value)
135147
}
136148

137-
% for bits in [ 32, 64 ]:
149+
% for bits in [ 16, 32, 64 ]:
138150

139151
// Warning: no overflow checking.
140152
@usableFromInline // used by SwiftPrivate._stdlib_AtomicInt

stdlib/public/core/Builtin.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,8 @@ internal var _objectPointerIsObjCBit: UInt {
429429
return 0x4000_0000_0000_0000
430430
#elseif _pointerBitWidth(_32)
431431
return 0x0000_0002
432+
#elseif _pointerBitWidth(_16)
433+
return 0x0000
432434
#else
433435
#error("Unknown platform")
434436
#endif

stdlib/public/core/CTypes.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@ public typealias CUnsignedChar = UInt8
2525
public typealias CUnsignedShort = UInt16
2626

2727
/// The C 'unsigned int' type.
28+
#if _pointerBitWidth(_16)
29+
public typealias CUnsignedInt = UInt
30+
#else
2831
public typealias CUnsignedInt = UInt32
32+
#endif
2933

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

4650
/// The C 'int' type.
51+
#if _pointerBitWidth(_16)
52+
public typealias CInt = Int
53+
#else
4754
public typealias CInt = Int32
55+
#endif
4856

4957
/// The C 'long' type.
50-
#if os(Windows) && (arch(x86_64) || arch(arm64))
58+
#if (os(Windows) && (arch(x86_64) || arch(arm64))) || _pointerBitWidth(_16)
5159
public typealias CLong = Int32
5260
#else
5361
public typealias CLong = Int

stdlib/public/core/EmbeddedRuntime.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@ public struct HeapObject {
4141
#if _pointerBitWidth(_64)
4242
static let doNotFreeBit = Int(bitPattern: 0x8000_0000_0000_0000)
4343
static let refcountMask = Int(bitPattern: 0x7fff_ffff_ffff_ffff)
44-
#else
44+
#elseif _pointerBitWidth(_32)
4545
static let doNotFreeBit = Int(bitPattern: 0x8000_0000)
4646
static let refcountMask = Int(bitPattern: 0x7fff_ffff)
47+
#elseif _pointerBitWidth(_16)
48+
static let doNotFreeBit = Int(bitPattern: 0x8000)
49+
static let refcountMask = Int(bitPattern: 0x7fff)
4750
#endif
4851

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

5659
#if _pointerBitWidth(_64)
5760
static let bridgeObjectToPlainObjectMask = UInt(0x8fff_ffff_ffff_fff8)
58-
#else
61+
#elseif _pointerBitWidth(_32)
5962
static let bridgeObjectToPlainObjectMask = UInt(0xffff_ffff)
63+
#elseif _pointerBitWidth(_16)
64+
static let bridgeObjectToPlainObjectMask = UInt(0xffff)
6065
#endif
6166
}
6267

0 commit comments

Comments
 (0)