Skip to content

Commit a565d1a

Browse files
committed
[embedded] Make embedded print() not depend on untyped throwing and allocations
1 parent ca9113c commit a565d1a

File tree

3 files changed

+40
-19
lines changed

3 files changed

+40
-19
lines changed

include/swift/Runtime/RuntimeFunctions.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ FUNCTION(UnexpectedError, swift_unexpectedError, SwiftCC, AlwaysAvailable,
193193
RETURNS(VoidTy),
194194
ARGS(ErrorPtrTy),
195195
ATTRS(NoUnwind, NoReturn),
196-
EFFECT(NoEffect),
196+
EFFECT(NoEffect, Deallocating),
197197
UNKNOWN_MEMEFFECTS)
198198

199199
// void *swift_copyPOD(void *dest, void *src, Metadata *self);

stdlib/public/core/EmbeddedPrint.swift

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,26 +59,35 @@ extension BinaryInteger {
5959
}
6060
let isNegative = Self.isSigned && self < (0 as Self)
6161
var value = magnitude
62-
withUnsafeTemporaryAllocation(byteCount: 64, alignment: 1) { buffer in
63-
var index = buffer.count - 1
64-
while value != 0 {
65-
let (quotient, remainder) =
66-
value.quotientAndRemainder(dividingBy: Magnitude(radix))
67-
buffer[index] = _ascii(UInt8(truncatingIfNeeded: remainder))
68-
index -= 1
69-
value = quotient
70-
}
71-
if isNegative {
72-
buffer[index] = UInt8(("-" as Unicode.Scalar).value)
73-
index -= 1
74-
}
75-
let start = index + 1
76-
let end = buffer.count - 1
77-
let count = end - start + 1
7862
79-
let pointerToPrint = buffer.baseAddress?.advanced(by: start).assumingMemoryBound(to: UInt8.self)
80-
printCharacters(UnsafeBufferPointer(start: pointerToPrint, count: count))
63+
// Avoid withUnsafeTemporaryAllocation which is not typed-throws ready yet
64+
let byteCount = 64
65+
let stackBuffer = Builtin.stackAlloc(byteCount._builtinWordValue,
66+
1._builtinWordValue, 1._builtinWordValue)
67+
let buffer = UnsafeMutableRawBufferPointer(start: .init(stackBuffer),
68+
count: byteCount)
69+
70+
var index = buffer.count - 1
71+
while value != 0 {
72+
let (quotient, remainder) =
73+
value.quotientAndRemainder(dividingBy: Magnitude(radix))
74+
buffer[index] = _ascii(UInt8(truncatingIfNeeded: remainder))
75+
index -= 1
76+
value = quotient
8177
}
78+
if isNegative {
79+
buffer[index] = UInt8(("-" as Unicode.Scalar).value)
80+
index -= 1
81+
}
82+
let start = index + 1
83+
let end = buffer.count - 1
84+
let count = end - start + 1
85+
86+
let pointerToPrint = buffer.baseAddress?.advanced(by: start)
87+
.assumingMemoryBound(to: UInt8.self)
88+
printCharacters(UnsafeBufferPointer(start: pointerToPrint, count: count))
89+
90+
Builtin.stackDealloc(stackBuffer)
8291
}
8392
}
8493

stdlib/public/core/LifetimeManager.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ public func _withUnprotectedUnsafeMutablePointer<T, Result>(
9999
#endif
100100
}
101101

102+
#if !$Embedded
102103
/// Invokes the given closure with a pointer to the given argument.
103104
///
104105
/// The `withUnsafePointer(to:_:)` function is useful for calling Objective-C
@@ -127,6 +128,17 @@ public func withUnsafePointer<T, Result>(
127128
{
128129
return try body(UnsafePointer<T>(Builtin.addressOfBorrow(value)))
129130
}
131+
#else
132+
// TODO: This should be unified with non-embedded Swift.
133+
@inlinable
134+
public func withUnsafePointer<T, E, Result>(
135+
to value: T,
136+
_ body: (UnsafePointer<T>) throws(E) -> Result
137+
) throws(E) -> Result
138+
{
139+
return try body(UnsafePointer<T>(Builtin.addressOfBorrow(value)))
140+
}
141+
#endif
130142

131143
/// Invokes the given closure with a pointer to the given argument.
132144
///

0 commit comments

Comments
 (0)