Skip to content

Commit 50d58b2

Browse files
committed
Add a lot of calling-convention annotations to the standard library / runtime.
The general rule here is that something needs to be SWIFT_CC(swift) if it's just declared in Swift code using _silgen_name, as opposed to importing something via a header. Of course, SWIFT_CC(swift) expands to nothing by default for now, and I haven't made an effort yet to add the indirect-result / context parameter ABI attributes. This is just a best-effort first pass. I also took the opportunity to shift a few files to just implement their shims header and to demote a few things to be private stdlib interfaces.
1 parent 7ae91ea commit 50d58b2

28 files changed

+373
-244
lines changed

include/swift/Runtime/Config.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030
#endif
3131
#endif
3232

33+
/// Does the current Swift platform use LLVM's intrinsic "swiftcall"
34+
/// calling convention for Swift functions?
35+
#ifndef SWIFT_USE_SWIFTCALL
36+
#define SWIFT_USE_SWIFTCALL 0
37+
#endif
38+
3339
/// Does the current Swift platform allow information other than the
3440
/// class pointer to be stored in the isa field? If so, when deriving
3541
/// the class pointer of an object, we must apply a
@@ -90,6 +96,18 @@
9096
#define SWIFT_CC_preserve_all __attribute__((preserve_all))
9197
#define SWIFT_CC_c
9298

99+
#if SWIFT_USE_SWIFTCALL
100+
#define SWIFT_CC_swift __attribute__((swiftcall))
101+
#define SWIFT_CONTEXT __attribute__((swift_context))
102+
#define SWIFT_ERROR_RESULT __attribute__((swift_error_result))
103+
#define SWIFT_INDIRECT_RESULT __attribute__((swift_indirect_result))
104+
#else
105+
#define SWIFT_CC_swift
106+
#define SWIFT_CONTEXT
107+
#define SWIFT_ERROR_RESULT
108+
#define SWIFT_INDIRECT_RESULT
109+
#endif
110+
93111
// Map a logical calling convention (e.g. RegisterPreservingCC) to LLVM calling
94112
// convention.
95113
#define SWIFT_LLVM_CC(CC) SWIFT_LLVM_CC_##CC

stdlib/private/StdlibUnittest/GetOSVersion.mm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
#include <Foundation/Foundation.h>
1414

15+
#include "swift/Runtime/Config.h"
16+
17+
SWIFT_CC(swift)
1518
extern "C" const char *
1619
swift_stdlib_getSystemVersionPlistProperty(const char *PropertyName) {
1720
// This function is implemented in Objective-C because Swift does not support

stdlib/private/StdlibUnittest/InterceptTraps.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include <string.h>
1616
#include <unistd.h>
1717

18+
#include "swift/Runtime/Config.h"
19+
1820
static void CrashCatcher(int Sig) {
1921
const char *Msg;
2022
switch (Sig) {
@@ -31,6 +33,7 @@ static void CrashCatcher(int Sig) {
3133
_exit(0);
3234
}
3335

36+
SWIFT_CC(swift)
3437
extern "C" void swift_stdlib_installTrapInterceptor() {
3538
// Disable buffering on stdout so that everything is printed before crashing.
3639
setbuf(stdout, 0);

stdlib/private/StdlibUnittest/OpaqueIdentityFunctions.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,9 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
extern "C" void *swift_stdlib_getPointer(void *x) { return x; }
13+
#include "swift/Runtime/Config.h"
14+
15+
SWIFT_CC(swift)
16+
extern "C"
17+
void *swift_stdlib_getPointer(void *x) { return x; }
1418

stdlib/private/SwiftPrivateLibcExtras/Subprocess.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
// Spawn is not available on Android.
1414
#if !defined(__ANDROID__)
1515

16+
#include "swift/Runtime/Config.h"
17+
1618
// NOTE: preprocess away the availability information to allow use of
1719
// unsupported APIs on certain targets (i.e. tvOS)
1820
#define availability(...)
@@ -24,26 +26,31 @@
2426
extern char ***_NSGetEnviron(void);
2527
#endif // defined(__APPLE__)
2628

29+
SWIFT_CC(swift)
2730
int swift_posix_spawn_file_actions_init(
2831
posix_spawn_file_actions_t *file_actions) {
2932
return posix_spawn_file_actions_init(file_actions);
3033
}
3134

35+
SWIFT_CC(swift)
3236
int swift_posix_spawn_file_actions_destroy(
3337
posix_spawn_file_actions_t *file_actions) {
3438
return posix_spawn_file_actions_destroy(file_actions);
3539
}
3640

41+
SWIFT_CC(swift)
3742
int swift_posix_spawn_file_actions_addclose(
3843
posix_spawn_file_actions_t *file_actions, int filedes) {
3944
return posix_spawn_file_actions_addclose(file_actions, filedes);
4045
}
4146

47+
SWIFT_CC(swift)
4248
int swift_posix_spawn_file_actions_adddup2(
4349
posix_spawn_file_actions_t *file_actions, int filedes, int newfiledes) {
4450
return posix_spawn_file_actions_adddup2(file_actions, filedes, newfiledes);
4551
}
4652

53+
SWIFT_CC(swift)
4754
int swift_posix_spawn(pid_t *__restrict pid, const char * __restrict path,
4855
const posix_spawn_file_actions_t *file_actions,
4956
const posix_spawnattr_t *__restrict attrp,
@@ -53,6 +60,7 @@ int swift_posix_spawn(pid_t *__restrict pid, const char * __restrict path,
5360
}
5461

5562
#if defined(__APPLE__)
63+
SWIFT_CC(swift)
5664
char ***swift_SwiftPrivateLibcExtras_NSGetEnviron(void) {
5765
return _NSGetEnviron();
5866
}

stdlib/public/Platform/Misc.c

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,58 +15,62 @@
1515
#include <sys/types.h>
1616
#include <sys/stat.h>
1717

18-
extern int
19-
_swift_Platform_open(const char *path, int oflag, mode_t mode) {
18+
#include "swift/Runtime/Config.h"
19+
20+
SWIFT_CC(swift)
21+
extern int _swift_Platform_open(const char *path, int oflag, mode_t mode) {
2022
return open(path, oflag, mode);
2123
}
2224

23-
extern int
24-
_swift_Platform_openat(int fd, const char *path, int oflag, mode_t mode) {
25+
SWIFT_CC(swift)
26+
extern int _swift_Platform_openat(int fd, const char *path, int oflag,
27+
mode_t mode) {
2528
return openat(fd, path, oflag, mode);
2629
}
2730

31+
SWIFT_CC(swift)
2832
extern sem_t *_swift_Platform_sem_open2(const char *name, int oflag) {
2933
return sem_open(name, oflag);
3034
}
3135

36+
SWIFT_CC(swift)
3237
extern sem_t *_swift_Platform_sem_open4(const char *name, int oflag,
3338
mode_t mode, unsigned int value) {
3439
return sem_open(name, oflag, mode, value);
3540
}
3641

37-
extern int
38-
_swift_Platform_fcntl(int fd, int cmd, int value) {
42+
SWIFT_CC(swift)
43+
extern int _swift_Platform_fcntl(int fd, int cmd, int value) {
3944
return fcntl(fd, cmd, value);
4045
}
4146

42-
extern int
43-
_swift_Platform_fcntlPtr(int fd, int cmd, void* ptr) {
47+
SWIFT_CC(swift)
48+
extern int _swift_Platform_fcntlPtr(int fd, int cmd, void* ptr) {
4449
return fcntl(fd, cmd, ptr);
4550
}
4651

4752
#if defined(__APPLE__)
4853
#define _REENTRANT
4954
#include <math.h>
5055

51-
extern float
52-
_swift_Darwin_lgammaf_r(float x, int *psigngam) {
56+
SWIFT_CC(swift)
57+
extern float _swift_Darwin_lgammaf_r(float x, int *psigngam) {
5358
return lgammaf_r(x, psigngam);
5459
}
5560

56-
extern double
57-
_swift_Darwin_lgamma_r(double x, int *psigngam) {
61+
SWIFT_CC(swift)
62+
extern double _swift_Darwin_lgamma_r(double x, int *psigngam) {
5863
return lgamma_r(x, psigngam);
5964
}
6065

61-
extern long double
62-
_swift_Darwin_lgammal_r(long double x, int *psigngam) {
66+
SWIFT_CC(swift)
67+
extern long double _swift_Darwin_lgammal_r(long double x, int *psigngam) {
6368
return lgammal_r(x, psigngam);
6469
}
6570
#endif // defined(__APPLE__)
6671

6772
#if defined(__FreeBSD__)
68-
extern char **
69-
_swift_FreeBSD_getEnv() {
73+
SWIFT_CC(swift) extern char **_swift_FreeBSD_getEnv() {
7074
extern char **environ;
7175
return environ;
7276
}

stdlib/public/SDK/Dispatch/Dispatch.mm

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,25 @@
1212

1313
#include <dispatch/dispatch.h>
1414

15-
__attribute__((visibility("hidden")))
15+
#include "swift/Runtime/Config.h"
16+
17+
SWIFT_CC(swift) __attribute__((visibility("hidden")))
1618
extern "C" dispatch_queue_attr_t
1719
_swift_dispatch_queue_concurrent(void) {
1820
return DISPATCH_QUEUE_CONCURRENT;
1921
}
2022

21-
__attribute__((visibility("hidden")))
22-
extern "C" dispatch_data_t
23+
SWIFT_CC(swift) __attribute__((visibility("hidden")))
24+
extern "C" SWIFT_CC(swift) dispatch_data_t
2325
_swift_dispatch_data_empty(void) {
2426
return dispatch_data_empty;
2527
}
2628

27-
#define SOURCE(t) \
28-
__attribute__((visibility("hidden"))) \
29-
extern "C" dispatch_source_type_t \
30-
_swift_dispatch_source_type_##t(void) { \
31-
return DISPATCH_SOURCE_TYPE_##t; \
29+
#define SOURCE(t) \
30+
SWIFT_CC(swift) __attribute__((visibility("hidden"))) \
31+
extern "C" dispatch_source_type_t \
32+
_swift_dispatch_source_type_##t(void) { \
33+
return DISPATCH_SOURCE_TYPE_##t; \
3234
}
3335

3436
SOURCE(DATA_ADD)

stdlib/public/SDK/Foundation/Thunks.mm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
#import <Foundation/Foundation.h>
1414

15+
#include "swift/Runtime/Config.h"
16+
17+
SWIFT_CC(swift)
1518
extern "C" void
1619
NS_Swift_NSUndoManager_registerUndoWithTargetHandler(
1720
id NS_RELEASES_ARGUMENT __nonnull self_,
@@ -28,6 +31,7 @@
2831

2932

3033
// -- NSCoder
34+
SWIFT_CC(swift)
3135
extern "C" NS_RETURNS_RETAINED __nullable id
3236
NS_Swift_NSCoder_decodeObject(id NS_RELEASES_ARGUMENT __nonnull self_,
3337
NSError *__nullable *__nullable error) {
@@ -42,6 +46,7 @@
4246
return [result retain];
4347
}
4448

49+
SWIFT_CC(swift)
4550
extern "C" NS_RETURNS_RETAINED __nullable id
4651
NS_Swift_NSCoder_decodeObjectForKey(id NS_RELEASES_ARGUMENT __nonnull self_,
4752
id NS_RELEASES_ARGUMENT __nonnull key,
@@ -58,6 +63,7 @@
5863
return [result retain];
5964
}
6065

66+
SWIFT_CC(swift)
6167
extern "C" NS_RETURNS_RETAINED __nullable id
6268
NS_Swift_NSCoder_decodeObjectOfClassForKey(
6369
id NS_RELEASES_ARGUMENT __nonnull self_,
@@ -76,6 +82,7 @@
7682
return [result retain];
7783
}
7884

85+
SWIFT_CC(swift)
7986
extern "C" NS_RETURNS_RETAINED __nullable id
8087
NS_Swift_NSCoder_decodeObjectOfClassesForKey(
8188
id NS_RELEASES_ARGUMENT __nonnull self_,
@@ -96,6 +103,7 @@
96103
}
97104

98105
// -- NSKeyedUnarchiver
106+
SWIFT_CC(swift)
99107
extern "C" NS_RETURNS_RETAINED __nullable id
100108
NS_Swift_NSKeyedUnarchiver_unarchiveObjectWithData(
101109
Class Self_, id NS_RELEASES_ARGUMENT __nonnull data,

stdlib/public/SDK/GameplayKit/GameplayKit.mm

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212

1313
#import <GameplayKit/GameplayKit.h>
1414

15-
extern "C" NS_RETURNS_RETAINED GKComponent * __nullable
15+
#include "swift/Runtime/Config.h"
16+
17+
extern "C" SWIFT_CC(swift) NS_RETURNS_RETAINED GKComponent * __nullable
1618
GK_Swift_GKEntity_componentForClass(
1719
id NS_RELEASES_ARGUMENT __nonnull self_,
1820
Class __nonnull componentClass) {
@@ -22,7 +24,7 @@
2224
return component;
2325
}
2426

25-
extern "C" NS_RETURNS_RETAINED GKState * __nullable
27+
extern "C" SWIFT_CC(swift) NS_RETURNS_RETAINED GKState * __nullable
2628
GK_Swift_GKStateMachine_stateForClass(
2729
id NS_RELEASES_ARGUMENT __nonnull self_,
2830
Class __nonnull stateClass) {

stdlib/public/SDK/ObjectiveC/ObjectiveC.mm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,20 @@
1212

1313
#include <objc/objc-api.h>
1414

15+
#include "swift/Runtime/Config.h"
16+
1517
OBJC_EXPORT
1618
void *objc_autoreleasePoolPush(void);
1719

1820
OBJC_EXPORT
1921
void objc_autoreleasePoolPop(void *);
2022

23+
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERFACE
2124
extern "C" void *_swift_objc_autoreleasePoolPush(void) {
2225
return objc_autoreleasePoolPush();
2326
}
2427

28+
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERFACE
2529
extern "C" void _swift_objc_autoreleasePoolPop(void *context) {
2630
return objc_autoreleasePoolPop(context);
2731
}

0 commit comments

Comments
 (0)