Skip to content

Commit 0b71280

Browse files
committed
[embedded] Move dummy test runtime into a separate .c file
1 parent 89df042 commit 0b71280

File tree

2 files changed

+69
-47
lines changed

2 files changed

+69
-47
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <strings.h>
2+
#include <stdint.h>
3+
#include <stdlib.h>
4+
#include <stdbool.h>
5+
6+
size_t _swiftEmptyArrayStorage[] = { /*isa*/0, /*refcount*/0, /*count*/0, /*flags*/1 };
7+
8+
typedef struct HeapObject {
9+
void *metadata;
10+
size_t refcount;
11+
} HeapObject;
12+
13+
void *swift_allocObject(void *metadata, size_t requiredSize, size_t requiredAlignmentMask) {
14+
void *r = NULL;
15+
posix_memalign(&r, requiredAlignmentMask + 1, requiredSize);
16+
bzero(r, requiredSize);
17+
HeapObject *object = r;
18+
object->metadata = metadata;
19+
object->refcount = 1;
20+
return object;
21+
}
22+
23+
void swift_deallocClassInstance(HeapObject *object, size_t allocatedSize, size_t allocatedAlignMask) {
24+
// don't deallocate ¯\_(ツ)_/¯
25+
}
26+
27+
HeapObject *swift_initStackObject(void *metadata, HeapObject *object) {
28+
object->metadata = metadata;
29+
object->refcount = 1;
30+
return object;
31+
}
32+
33+
bool swift_isUniquelyReferenced_nonNull_native(HeapObject *object) {
34+
return object->refcount == 1;
35+
}
36+
37+
void swift_release(HeapObject *object) {
38+
// don't release ¯\_(ツ)_/¯
39+
}
40+
41+
HeapObject *swift_retain(HeapObject *object) {
42+
object->refcount += 1;
43+
return object;
44+
}
45+
46+
void swift_setDeallocating(HeapObject *object) {
47+
// don't deallocate ¯\_(ツ)_/¯
48+
}
49+
50+
void swift_beginAccess(void *pointer, void *buffer, uintptr_t flags, void *pc) { }
51+
void swift_endAccess(void *buffer) { }

test/embedded/arrays.swift

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %{python} %utils/split_file.py -o %t %s
3-
4-
// RUN: %target-swift-frontend %t/main.swift -enable-experimental-feature Embedded -c -o %t/main.o
5-
// RUN: %target-clang -g -x c %t/runtime.c -c -o %t/runtime.o
6-
// RUN: %target-clang %t/main.o %t/runtime.o -o %t/a.out -dead_strip
2+
// RUN: %target-swift-frontend %s -enable-experimental-feature Embedded -c -o %t/main.o
3+
// RUN: %target-clang -x c -c %S/Inputs/tiny-runtime-dummy-refcounting.c -o %t/runtime.o
4+
// RUN: %target-clang -x c -c %S/Inputs/print.c -o %t/print.o
5+
// RUN: %target-clang %t/main.o %t/runtime.o %t/print.o -o %t/a.out -dead_strip
76
// RUN: %target-run %t/a.out | %FileCheck %s
87

98
// REQUIRES: executable_test
109

11-
// BEGIN main.swift
12-
1310
@_silgen_name("putchar")
1411
func putchar(_: UInt8)
1512

@@ -26,23 +23,21 @@ public func print(_ s: StaticString, terminator: StaticString = "\n") {
2623
}
2724
}
2825

29-
@_silgen_name("vprintf")
30-
func vprintf(_: UnsafePointer<UInt8>, _: UnsafeRawPointer)
26+
@_silgen_name("print_long")
27+
func print_long(_: Int)
3128

32-
var global: Int = 0
33-
public func print(_ n: Int) {
34-
let f: StaticString = "%d"
35-
global = n
36-
vprintf(f.utf8Start, &global)
29+
public func print(_ n: Int, terminator: StaticString = "\n") {
30+
print_long(n)
31+
print("", terminator: terminator)
3732
}
3833

39-
public func print(_ array: [Int]) {
40-
print("[", terminator: "")
41-
for e in array {
42-
print(e)
43-
print(", ", terminator: "")
44-
}
45-
print("]")
34+
func print(_ array: [Int]) {
35+
print("[", terminator: "")
36+
for i in 0 ..< array.count {
37+
print_long(array[i])
38+
if i != array.count - 1 { print(", ", terminator: "") }
39+
}
40+
print("]")
4641
}
4742

4843
func test() {
@@ -51,32 +46,8 @@ func test() {
5146
a.append(contentsOf: [5, 4])
5247
let b = a.sorted()
5348
var c = b
54-
c = c.reversed()
55-
print(c) // CHECK: [8, 5, 4, 3, 2, 1, ]
49+
c = c.reversed().filter { $0 % 2 == 0 }
50+
print(c) // CHECK: [8, 4, 2]
5651
}
5752

5853
test()
59-
60-
// BEGIN runtime.c
61-
62-
#include <stdint.h>
63-
#include <stdlib.h>
64-
#include <stdbool.h>
65-
66-
size_t _swiftEmptyArrayStorage[] = { /*isa*/0, /*refcount*/0, /*count*/0, /*flags*/1 };
67-
68-
void *swift_allocObject(void *metadata, size_t requiredSize, size_t requiredAlignmentMask) {
69-
void *r = NULL;
70-
posix_memalign(&r, requiredAlignmentMask + 1, requiredSize);
71-
return r;
72-
}
73-
74-
void swift_deallocClassInstance() { }
75-
void swift_initStackObject() { }
76-
bool swift_isUniquelyReferenced_nonNull_native(void *) { return true; }
77-
void swift_release() { }
78-
void swift_retain() { }
79-
void swift_setDeallocating() { }
80-
81-
void swift_beginAccess() { }
82-
void swift_endAccess() { }

0 commit comments

Comments
 (0)