Skip to content

Commit 83e4453

Browse files
committed
Introduce _va_list_kind conditional. Use it for VarArgs implementation.
In order to simplify the logic for CVaListPtr and __VaListBuilder, introduce a new kind of conditional _va_list_kind which will be populated from the equivalent enumeration in clang::TargetInfo::BuiltinVaListKind.
1 parent 5154886 commit 83e4453

File tree

5 files changed

+58
-9
lines changed

5 files changed

+58
-9
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ namespace swift {
5050
CanImport,
5151
/// Target Environment (currently just 'simulator' or absent)
5252
TargetEnvironment,
53+
/// Variable argument list standard
54+
VariableArgumentList,
5355
};
5456

5557
/// Describes which Swift 3 Objective-C inference warnings should be
@@ -388,7 +390,7 @@ namespace swift {
388390
}
389391

390392
private:
391-
llvm::SmallVector<std::pair<PlatformConditionKind, std::string>, 5>
393+
llvm::SmallVector<std::pair<PlatformConditionKind, std::string>, 6>
392394
PlatformConditionValues;
393395
llvm::SmallVector<std::string, 2> CustomConditionalCompilationFlags;
394396
};

lib/Basic/LangOptions.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "swift/Basic/Platform.h"
2020
#include "swift/Basic/Range.h"
2121
#include "swift/Config.h"
22+
#include "clang/Basic/TargetInfo.h"
2223
#include "llvm/ADT/Hashing.h"
2324
#include "llvm/ADT/SmallString.h"
2425
#include "llvm/Support/raw_ostream.h"
@@ -65,6 +66,17 @@ static const StringRef SupportedConditionalCompilationTargetEnvironments[] = {
6566
"simulator",
6667
};
6768

69+
static const StringRef SupportedConditionalCompilationVariableArgumentLists[] = {
70+
"_charPtr",
71+
"_voidPtr",
72+
"_aarch64",
73+
"_pnacl",
74+
"_power",
75+
"_x86_64",
76+
"_aapcs",
77+
"_systemZ",
78+
};
79+
6880
template <size_t N>
6981
bool contains(const StringRef (&Array)[N], const StringRef &V,
7082
std::vector<StringRef> &suggestions) {
@@ -107,6 +119,9 @@ checkPlatformConditionSupported(PlatformConditionKind Kind, StringRef Value,
107119
case PlatformConditionKind::TargetEnvironment:
108120
return contains(SupportedConditionalCompilationTargetEnvironments, Value,
109121
suggestions);
122+
case PlatformConditionKind::VariableArgumentList:
123+
return contains(SupportedConditionalCompilationVariableArgumentLists, Value,
124+
suggestions);
110125
case PlatformConditionKind::CanImport:
111126
// All importable names are valid.
112127
// FIXME: Perform some kind of validation of the string?
@@ -269,6 +284,35 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
269284
addPlatformConditionValue(PlatformConditionKind::TargetEnvironment,
270285
"simulator");
271286

287+
// Set the "_va_list_kind" platform condition.
288+
clang::TargetInfo *TargetInfo = NULL; // TODO: how?
289+
switch (TargetInfo->getBuiltinVaListKind()) {
290+
case clang::TargetInfo::CharPtrBuiltinVaList:
291+
addPlatformConditionValue(PlatformConditionKind::VariableArgumentList, "_charPtr");
292+
break;
293+
case clang::TargetInfo::VoidPtrBuiltinVaList:
294+
addPlatformConditionValue(PlatformConditionKind::VariableArgumentList, "_voidPtr");
295+
break;
296+
case clang::TargetInfo::AArch64ABIBuiltinVaList:
297+
addPlatformConditionValue(PlatformConditionKind::VariableArgumentList, "_aarch64");
298+
break;
299+
case clang::TargetInfo::PNaClABIBuiltinVaList:
300+
addPlatformConditionValue(PlatformConditionKind::VariableArgumentList, "_pnacl");
301+
break;
302+
case clang::TargetInfo::PowerABIBuiltinVaList:
303+
addPlatformConditionValue(PlatformConditionKind::VariableArgumentList, "_power");
304+
break;
305+
case clang::TargetInfo::X86_64ABIBuiltinVaList:
306+
addPlatformConditionValue(PlatformConditionKind::VariableArgumentList, "_x86_64");
307+
break;
308+
case clang::TargetInfo::AAPCSABIBuiltinVaList:
309+
addPlatformConditionValue(PlatformConditionKind::VariableArgumentList, "_aapcs");
310+
break;
311+
case clang::TargetInfo::SystemZBuiltinVaList:
312+
addPlatformConditionValue(PlatformConditionKind::VariableArgumentList, "_systemZ");
313+
break;
314+
}
315+
272316
// If you add anything to this list, change the default size of
273317
// PlatformConditionValues to not require an extra allocation
274318
// in the common case.

lib/Parse/ParseIfConfig.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Optional<PlatformConditionKind> getPlatformConditionKind(StringRef Name) {
4343
.Case("_runtime", PlatformConditionKind::Runtime)
4444
.Case("canImport", PlatformConditionKind::CanImport)
4545
.Case("targetEnvironment", PlatformConditionKind::TargetEnvironment)
46+
.Case("_va_list_kind", PlatformConditionKind::VariableArgumentList)
4647
.Default(None);
4748
}
4849

@@ -287,6 +288,8 @@ class ValidateIfConfigCondition :
287288
DiagName = "import conditional"; break;
288289
case PlatformConditionKind::TargetEnvironment:
289290
DiagName = "target environment"; break;
291+
case PlatformConditionKind::VariableArgumentList:
292+
DiagName = "variable argument list kind"; break;
290293
case PlatformConditionKind::Runtime:
291294
llvm_unreachable("handled above");
292295
}

stdlib/public/core/CTypes.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ extension UInt {
219219
}
220220

221221
/// A wrapper around a C `va_list` pointer.
222-
#if arch(arm64) && os(Linux)
222+
#if _va_list_kind(_aarch64)
223223
@_fixed_layout
224224
public struct CVaListPointer {
225225
@usableFromInline // unsafe-performance

stdlib/public/core/VarArgs.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ protocol _CVarArgAligned : CVarArg {
6363
var _cVarArgAlignment: Int { get }
6464
}
6565

66-
#if arch(x86_64)
66+
#if _va_list_kind(_x86_64)
6767
@usableFromInline
6868
internal let _countGPRegisters = 6
6969
// Note to future visitors concerning the following SSE register count.
@@ -91,7 +91,7 @@ internal let _countGPRegisters = 16
9191
@usableFromInline
9292
internal let _registerSaveWords = _countGPRegisters
9393

94-
#elseif arch(arm64) && os(Linux)
94+
#elseif _va_list_kind(_aarch64)
9595
// ARM Procedure Call Standard for aarch64. (IHI0055B)
9696
// The va_list type may refer to any parameter in a parameter list may be in one
9797
// of three memory locations depending on its type and position in the argument
@@ -109,7 +109,7 @@ internal let _fpRegisterWords = 16 / MemoryLayout<Int>.size
109109
internal let _registerSaveWords = _countGPRegisters + (_countFPRegisters * _fpRegisterWords)
110110
#endif
111111

112-
#if arch(s390x)
112+
#if _va_list_kind(_systemZ)
113113
@usableFromInline
114114
internal typealias _VAUInt = CUnsignedLongLong
115115
@usableFromInline
@@ -419,7 +419,7 @@ extension Float80 : CVarArg, _CVarArgAligned {
419419
}
420420
#endif
421421

422-
#if arch(x86_64) || arch(s390x)
422+
#if _va_list_kind(_x86_64) || _va_list_kind(_systemZ)
423423

424424
/// An object that can manage the lifetime of storage backing a
425425
/// `CVaListPointer`.
@@ -470,7 +470,7 @@ final internal class __VaListBuilder {
470470
internal func append(_ arg: CVarArg) {
471471
var encoded = arg._cVarArgEncoding
472472

473-
#if arch(x86_64)
473+
#if _va_list_kind(_x86_64)
474474
let isDouble = arg is _CVarArgPassedAsDouble
475475

476476
if isDouble && fpRegistersUsed < _countFPRegisters {
@@ -493,7 +493,7 @@ final internal class __VaListBuilder {
493493
storage.append(w)
494494
}
495495
}
496-
#elseif arch(s390x)
496+
#elseif _va_list_kind(_systemZ)
497497
if gpRegistersUsed < _countGPRegisters {
498498
for w in encoded {
499499
storage[gpRegistersUsed] = w
@@ -518,7 +518,7 @@ final internal class __VaListBuilder {
518518
Builtin.addressof(&self.header)))
519519
}
520520
}
521-
#elseif arch(arm64) && os(Linux)
521+
#elseif _va_list_kind(_aarch64)
522522

523523
// NOTE: older runtimes called this _VaListBuilder. The two must
524524
// coexist, so it was renamed. The old name must not be used in the new

0 commit comments

Comments
 (0)