Skip to content

Commit 75f0e3c

Browse files
committed
[embedded] Add a stop-gap print() for embedded Swift
1 parent fe0ccf4 commit 75f0e3c

27 files changed

+93
-70
lines changed

stdlib/public/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ endif()
263263
list(APPEND SWIFTLIB_EMBEDDED_SOURCES
264264
EmbeddedRuntime.swift
265265
EmbeddedStubs.swift
266+
EmbeddedPrint.swift
266267
)
267268

268269
set(GROUP_INFO_JSON_FILE ${CMAKE_CURRENT_SOURCE_DIR}/GroupInfo.json)

test/embedded/Inputs/print.swift renamed to stdlib/public/core/EmbeddedPrint.swift

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SwiftShims
14+
15+
// TODO: This is all a stop-gap so that at least some types are printable in
16+
// embedded Swift, in an embedded-programming friendly way (we mainly need
17+
// printing to not need to heap allocate).
18+
119
@_silgen_name("putchar")
220
func putchar(_: UInt8)
321

4-
func print(_ s: StaticString, terminator: StaticString = "\n") {
5-
var p = s.utf8Start
22+
public func print(_ string: StaticString, terminator: StaticString = "\n") {
23+
var p = string.utf8Start
624
while p.pointee != 0 {
725
putchar(p.pointee)
826
p += 1
@@ -43,7 +61,8 @@ extension BinaryInteger {
4361
withUnsafeTemporaryAllocation(byteCount: 64, alignment: 1) { buffer in
4462
var index = buffer.count - 1
4563
while value != 0 {
46-
let (quotient, remainder) = value.quotientAndRemainder(dividingBy: Magnitude(radix))
64+
let (quotient, remainder) =
65+
value.quotientAndRemainder(dividingBy: Magnitude(radix))
4766
buffer[index] = _ascii(UInt8(truncatingIfNeeded: remainder))
4867
index -= 1
4968
value = quotient
@@ -55,18 +74,20 @@ extension BinaryInteger {
5574
let start = index + 1
5675
let end = buffer.count - 1
5776
let count = end - start + 1
58-
printCharacters(UnsafeBufferPointer(start: buffer.baseAddress?.advanced(by: start).assumingMemoryBound(to: UInt8.self), count: count))
77+
78+
let pointerToPrint = buffer.baseAddress?.advanced(by: start).assumingMemoryBound(to: UInt8.self)
79+
printCharacters(UnsafeBufferPointer(start: pointerToPrint, count: count))
5980
}
6081
}
6182
}
6283

63-
func print(_ n: some BinaryInteger, terminator: StaticString = "\n") {
64-
n.writeToStdout()
84+
public func print(_ integer: some BinaryInteger, terminator: StaticString = "\n") {
85+
integer.writeToStdout()
6586
print("", terminator: terminator)
6687
}
6788

68-
func print(_ b: Bool, terminator: StaticString = "\n") {
69-
if b {
89+
public func print(_ boolean: Bool, terminator: StaticString = "\n") {
90+
if boolean {
7091
print("true", terminator: terminator)
7192
} else {
7293
print("false", terminator: terminator)

stdlib/public/core/GroupInfo.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@
248248
"DurationProtocol.swift",
249249
"Instant.swift",
250250
"EmbeddedRuntime.swift",
251-
"EmbeddedStubs.swift"
251+
"EmbeddedStubs.swift",
252+
"EmbeddedPrint.swift"
252253
],
253254
"Result": [
254255
"Result.swift"

test/embedded/FixedArray.swift

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11

2-
// RUN: %target-run-simple-swift(%S/Inputs/ExperimentalFixedArray.swift %S/Inputs/print.swift -enable-experimental-feature Embedded -enable-experimental-feature FixedArrays -parse-as-library -runtime-compatibility-version none -wmo -Xfrontend -disable-objc-interop) | %FileCheck %s
3-
// RUN: %target-run-simple-swift(-O %S/Inputs/ExperimentalFixedArray.swift %S/Inputs/print.swift -enable-experimental-feature Embedded -enable-experimental-feature FixedArrays -parse-as-library -runtime-compatibility-version none -wmo -Xfrontend -disable-objc-interop) | %FileCheck %s
2+
// RUN: %target-run-simple-swift(%S/Inputs/ExperimentalFixedArray.swift -enable-experimental-feature Embedded -enable-experimental-feature FixedArrays -parse-as-library -runtime-compatibility-version none -wmo -Xfrontend -disable-objc-interop) | %FileCheck %s
3+
// RUN: %target-run-simple-swift(-O %S/Inputs/ExperimentalFixedArray.swift -enable-experimental-feature Embedded -enable-experimental-feature FixedArrays -parse-as-library -runtime-compatibility-version none -wmo -Xfrontend -disable-objc-interop) | %FileCheck %s
44

55
// Also test in non-embedded mode
66

7-
// RUN: %target-run-simple-swift(%S/Inputs/ExperimentalFixedArray.swift %S/Inputs/print.swift -enable-experimental-feature FixedArrays -parse-as-library -wmo) | %FileCheck %s
8-
// RUN: %target-run-simple-swift(-O %S/Inputs/ExperimentalFixedArray.swift %S/Inputs/print.swift -enable-experimental-feature FixedArrays -parse-as-library -wmo) | %FileCheck %s
7+
// RUN: %target-run-simple-swift(%S/Inputs/ExperimentalFixedArray.swift -enable-experimental-feature FixedArrays -parse-as-library -wmo) | %FileCheck %s
8+
// RUN: %target-run-simple-swift(-O %S/Inputs/ExperimentalFixedArray.swift -enable-experimental-feature FixedArrays -parse-as-library -wmo) | %FileCheck %s
99

1010
// REQUIRES: executable_test
1111
// REQUIRES: optimized_stdlib
@@ -37,18 +37,18 @@ struct IntByte: Printable {
3737
let b: Int8
3838

3939
func print() {
40-
main.print(i, terminator: "")
41-
main.print(",", terminator: "")
42-
main.print(b, terminator: "")
40+
Swift.print(i, terminator: "")
41+
Swift.print(",", terminator: "")
42+
Swift.print(b, terminator: "")
4343
}
4444
}
4545

4646
extension Optional: Printable where Wrapped == Int64 {
4747
func print() {
4848
if let x = self {
49-
main.print(x, terminator: "")
49+
Swift.print(x, terminator: "")
5050
} else {
51-
main.print("nil", terminator: "")
51+
Swift.print("nil", terminator: "")
5252
}
5353
}
5454
}
@@ -151,13 +151,13 @@ struct Matrix<T: Printable>: ~Copyable {
151151

152152
func print() {
153153
for r in 0..<rows {
154-
main.print("(", terminator: "")
154+
Swift.print("(", terminator: "")
155155
for c in 0..<columns {
156156
self[r, c].print()
157157
if c < columns - 1 {
158-
main.print(",", terminator: "")
158+
Swift.print(",", terminator: "")
159159
} else {
160-
main.print(")")
160+
Swift.print(")")
161161
}
162162
}
163163
}
@@ -271,7 +271,7 @@ protocol Printable {
271271

272272
extension Int: Printable {
273273
func print() {
274-
main.print(self, terminator: "")
274+
Swift.print(self, terminator: "")
275275
}
276276
}
277277

@@ -290,13 +290,13 @@ final class CheckLifetime: Printable {
290290
}
291291

292292
deinit {
293-
main.print("deinit ", terminator: "")
294-
main.print(x)
293+
Swift.print("deinit ", terminator: "")
294+
Swift.print(x)
295295
}
296296

297297
func print() {
298-
main.print("CheckLifetime(", terminator: "")
299-
main.print(x, terminator: ")")
298+
Swift.print("CheckLifetime(", terminator: "")
299+
Swift.print(x, terminator: ")")
300300
}
301301
}
302302

test/embedded/array-builtins-exec.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-swift-frontend %s %S/Inputs/print.swift -enable-experimental-feature Embedded -enable-builtin-module -c -o %t/main.o
2+
// RUN: %target-swift-frontend %s -parse-as-library -enable-experimental-feature Embedded -enable-builtin-module -c -o %t/main.o
33
// RUN: %target-clang %t/main.o -o %t/a.out -dead_strip
44
// RUN: %target-run %t/a.out | %FileCheck %s
55

test/embedded/class-func.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-run-simple-swift(%S/Inputs/print.swift -enable-experimental-feature Embedded -parse-as-library -runtime-compatibility-version none -wmo -Xfrontend -disable-objc-interop) | %FileCheck %s
1+
// RUN: %target-run-simple-swift(-enable-experimental-feature Embedded -parse-as-library -runtime-compatibility-version none -wmo -Xfrontend -disable-objc-interop) | %FileCheck %s
22

33
// REQUIRES: swift_in_compiler
44
// REQUIRES: executable_test

test/embedded/classes-arrays.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-swift-frontend %s %S/Inputs/print.swift -enable-experimental-feature Embedded -c -o %t/main.o
2+
// RUN: %target-swift-frontend %s -parse-as-library -enable-experimental-feature Embedded -c -o %t/main.o
33
// RUN: %target-clang %t/main.o -o %t/a.out -dead_strip
44
// RUN: %target-run %t/a.out | %FileCheck %s
55

test/embedded/classes-stack-promotion.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-swift-frontend -O -Xllvm -sil-disable-pass=function-signature-opts %s %S/Inputs/print.swift -enable-experimental-feature Embedded -module-name main -emit-irgen | %FileCheck %s --check-prefix CHECK-IR
3-
// RUN: %target-swift-frontend -O -Xllvm -sil-disable-pass=function-signature-opts %s %S/Inputs/print.swift -enable-experimental-feature Embedded -c -o %t/main.o
2+
// RUN: %target-swift-frontend -O -Xllvm -sil-disable-pass=function-signature-opts %s -parse-as-library -enable-experimental-feature Embedded -module-name main -emit-irgen | %FileCheck %s --check-prefix CHECK-IR
3+
// RUN: %target-swift-frontend -O -Xllvm -sil-disable-pass=function-signature-opts %s -parse-as-library -enable-experimental-feature Embedded -c -o %t/main.o
44
// RUN: %target-clang %t/main.o -o %t/a.out -dead_strip
55
// RUN: %target-run %t/a.out | %FileCheck %s
66

@@ -72,7 +72,7 @@ struct Main {
7272

7373
// CHECK-IR: define {{.*}}@"$s4main8MyStructVyAcA0B10FinalClassCcfC"
7474
// CHECK-IR-NEXT: entry:
75-
// CHECK-IR-NEXT: call {{.*}}@"$s4main5print_10terminatorys12StaticStringV_AEtF"
75+
// CHECK-IR-NEXT: call {{.*}}@"$ss5print_10terminatorys12StaticStringV_ADtF"
7676
// CHECK-IR-NEXT: call {{.*}}@swift_release
7777
// CHECK-IR-NEXT: ret void
7878
// CHECK-IR-NEXT: }
@@ -82,17 +82,17 @@ struct Main {
8282
// CHECK-IR-NEXT: alloca %T4main10MySubClassC
8383
// CHECK-IR-NEXT: alloca %T4main12MyFinalClassC
8484
// CHECK-IR-NEXT: call {{.*}}@swift_initStackObject
85-
// CHECK-IR-NEXT: call {{.*}}@"$s4main5print_10terminatorys12StaticStringV_AEtF"
86-
// CHECK-IR-NEXT: call {{.*}}@"$s4main5print_10terminatorys12StaticStringV_AEtF"
87-
// CHECK-IR-NEXT: call {{.*}}@"$s4main5print_10terminatorys12StaticStringV_AEtF"
85+
// CHECK-IR-NEXT: call {{.*}}@"$ss5print_10terminatorys12StaticStringV_ADtF"
86+
// CHECK-IR-NEXT: call {{.*}}@"$ss5print_10terminatorys12StaticStringV_ADtF"
87+
// CHECK-IR-NEXT: call {{.*}}@"$ss5print_10terminatorys12StaticStringV_ADtF"
8888
// CHECK-IR-NEXT: call {{.*}}@"$s4main3bar1oyAA7MyClassC_tF"
8989
// CHECK-IR-NEXT: call {{.*}}@swift_setDeallocating
90-
// CHECK-IR-NEXT: call {{.*}}@"$s4main5print_10terminatorys12StaticStringV_AEtF"
91-
// CHECK-IR-NEXT: call {{.*}}@"$s4main5print_10terminatorys12StaticStringV_AEtF"
90+
// CHECK-IR-NEXT: call {{.*}}@"$ss5print_10terminatorys12StaticStringV_ADtF"
91+
// CHECK-IR-NEXT: call {{.*}}@"$ss5print_10terminatorys12StaticStringV_ADtF"
9292
// CHECK-IR-NEXT: call {{.*}}@llvm.lifetime.end.p0
93-
// CHECK-IR-NEXT: call {{.*}}@"$s4main5print_10terminatorys12StaticStringV_AEtF"
93+
// CHECK-IR-NEXT: call {{.*}}@"$ss5print_10terminatorys12StaticStringV_ADtF"
9494
// CHECK-IR-NEXT: call {{.*}}@swift_initStackObject
95-
// CHECK-IR-NEXT: call {{.*}}@"$s4main5print_10terminatorys12StaticStringV_AEtF"
95+
// CHECK-IR-NEXT: call {{.*}}@"$ss5print_10terminatorys12StaticStringV_ADtF"
9696
// CHECK-IR-NEXT: call {{.*}}@"$s4main8MyStructVyAcA0B10FinalClassCcfC"
9797
// CHECK-IR-NEXT: call {{.*}}@llvm.lifetime.end.p0
9898
// CHECK-IR-NEXT: ret {{.*}}0

test/embedded/classes.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-swift-frontend %s %S/Inputs/print.swift -enable-experimental-feature Embedded -c -o %t/main.o
2+
// RUN: %target-swift-frontend %s -parse-as-library -enable-experimental-feature Embedded -c -o %t/main.o
33
// RUN: %target-clang %t/main.o -o %t/a.out -dead_strip
44
// RUN: %target-run %t/a.out | %FileCheck %s
55

test/embedded/closures-heap.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-swift-frontend %s %S/Inputs/print.swift -enable-experimental-feature Embedded -c -o %t/main.o
2+
// RUN: %target-swift-frontend %s -parse-as-library -enable-experimental-feature Embedded -c -o %t/main.o
33
// RUN: %target-clang %t/main.o -o %t/a.out -dead_strip
44
// RUN: %target-run %t/a.out | %FileCheck %s
55

0 commit comments

Comments
 (0)