Skip to content

Commit ac0f574

Browse files
authored
Merge pull request #75127 from carlos4242/embedded-avr-cross-compile-stdlib
[AVR] standard library support for AVR
2 parents dd221a8 + 39adecb commit ac0f574

20 files changed

+104
-43
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/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

stdlib/public/core/Hasher.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ extension Hasher {
164164
combine(UInt64(truncatingIfNeeded: value))
165165
#elseif _pointerBitWidth(_32)
166166
combine(UInt32(truncatingIfNeeded: value))
167+
#elseif _pointerBitWidth(_16)
168+
combine(UInt16(truncatingIfNeeded: value))
167169
#else
168170
#error("Unknown platform")
169171
#endif
@@ -439,7 +441,7 @@ public struct Hasher {
439441
_internalInvariant(UInt.bitWidth == UInt64.bitWidth)
440442
state.compress(UInt64(truncatingIfNeeded: value))
441443
let tbc = _TailBuffer(tail: 0, byteCount: 8)
442-
#elseif _pointerBitWidth(_32)
444+
#elseif _pointerBitWidth(_32) || _pointerBitWidth(_16)
443445
_internalInvariant(UInt.bitWidth < UInt64.bitWidth)
444446
let tbc = _TailBuffer(
445447
tail: UInt64(truncatingIfNeeded: value),

stdlib/public/core/Int128.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,8 @@ extension Int128: BinaryInteger {
456456
UInt(Builtin.trunc_Int128_Int64(_value))
457457
#elseif _pointerBitWidth(_32)
458458
UInt(Builtin.trunc_Int128_Int32(_value))
459+
#elseif _pointerBitWidth(_16)
460+
UInt(Builtin.trunc_Int128_Int16(_value))
459461
#else
460462
#error("Unsupported platform")
461463
#endif

stdlib/public/core/IntegerTypes.swift.gyb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,7 +1659,9 @@ ${assignmentOperatorComment(x.operator, True)}
16591659
@_transparent
16601660
public // @testable
16611661
init(_ _v: Builtin.Word) {
1662-
% if BuiltinName == 'Int32':
1662+
% if BuiltinName == 'Int16':
1663+
self._value = Builtin.truncOrBitCast_Word_Int16(_v)
1664+
% elif BuiltinName == 'Int32':
16631665
self._value = Builtin.truncOrBitCast_Word_Int32(_v)
16641666
% elif BuiltinName == 'Int64':
16651667
self._value = Builtin.${z}extOrBitCast_Word_Int64(_v)
@@ -1669,7 +1671,9 @@ ${assignmentOperatorComment(x.operator, True)}
16691671
@_transparent
16701672
public // @testable
16711673
var _builtinWordValue: Builtin.Word {
1672-
% if BuiltinName == 'Int32':
1674+
% if BuiltinName == 'Int16':
1675+
return Builtin.${z}extOrBitCast_Int16_Word(_value)
1676+
% elif BuiltinName == 'Int32':
16731677
return Builtin.${z}extOrBitCast_Int32_Word(_value)
16741678
% elif BuiltinName == 'Int64':
16751679
return Builtin.truncOrBitCast_Int64_Word(_value)

stdlib/public/core/SmallString.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ internal struct _SmallString {
7979
extension _SmallString {
8080
@inlinable @inline(__always)
8181
internal static var capacity: Int {
82-
#if _pointerBitWidth(_32)
82+
#if _pointerBitWidth(_32) || _pointerBitWidth(_16)
8383
return 10
8484
#elseif os(Android) && arch(arm64)
8585
return 14

stdlib/public/core/StringGuts.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ extension _StringGuts {
201201
the runtime is depending on this, update Reflection.mm and \
202202
this if you change it
203203
""")
204-
#elseif _pointerBitWidth(_32)
204+
#elseif _pointerBitWidth(_32) || _pointerBitWidth(_16)
205205
_internalInvariant(MemoryLayout<String>.size == 12, """
206206
the runtime is depending on this, update Reflection.mm and \
207207
this if you change it

0 commit comments

Comments
 (0)