Skip to content

Commit e1db602

Browse files
samding01jrose-apple
authored andcommitted
Fix VarArgs.swift test case for s390x (#12792)
1 parent 33de34b commit e1db602

File tree

1 file changed

+46
-19
lines changed

1 file changed

+46
-19
lines changed

stdlib/public/core/VarArgs.swift

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ protocol _CVarArgAligned : CVarArg {
6060

6161
#if arch(x86_64)
6262
@_versioned
63-
internal let _x86_64CountGPRegisters = 6
63+
internal let _countGPRegisters = 6
6464
// Note to future visitors concerning the following SSE register count.
6565
//
6666
// AMD64-ABI section 3.5.7 says -- as recently as v0.99.7, Nov 2014 -- to make
@@ -75,11 +75,24 @@ internal let _x86_64CountGPRegisters = 6
7575
// from 8 to 16 based on reading the spec, probably the bug you're looking for
7676
// is elsewhere.
7777
@_versioned
78-
internal let _x86_64CountSSERegisters = 8
78+
internal let _countSSERegisters = 8
7979
@_versioned
80-
internal let _x86_64SSERegisterWords = 2
80+
internal let _sseRegisterWords = 2
8181
@_versioned
82-
internal let _x86_64RegisterSaveWords = _x86_64CountGPRegisters + _x86_64CountSSERegisters * _x86_64SSERegisterWords
82+
internal let _registerSaveWords = _countGPRegisters + _countSSERegisters * _sseRegisterWords
83+
#elseif arch(s390x)
84+
@_versioned
85+
internal let _countGPRegisters = 16
86+
@_versioned
87+
internal let _registerSaveWords = _countGPRegisters
88+
#endif
89+
90+
#if arch(s390x)
91+
internal typealias _VAUInt = CUnsignedLongLong
92+
internal typealias _VAInt = Int64
93+
#else
94+
internal typealias _VAUInt = CUnsignedInt
95+
internal typealias _VAInt = Int32
8396
#endif
8497

8598
/// Invokes the given closure with a C `va_list` argument derived from the
@@ -178,7 +191,7 @@ extension Int : CVarArg {
178191

179192
extension Bool : CVarArg {
180193
public var _cVarArgEncoding: [Int] {
181-
return _encodeBitsAsWords(Int32(self ? 1:0))
194+
return _encodeBitsAsWords(_VAInt(self ? 1:0))
182195
}
183196
}
184197

@@ -204,7 +217,7 @@ extension Int32 : CVarArg {
204217
/// appropriately interpreted by C varargs.
205218
@_inlineable // FIXME(sil-serialize-all)
206219
public var _cVarArgEncoding: [Int] {
207-
return _encodeBitsAsWords(self)
220+
return _encodeBitsAsWords(_VAInt(self))
208221
}
209222
}
210223

@@ -213,7 +226,7 @@ extension Int16 : CVarArg {
213226
/// appropriately interpreted by C varargs.
214227
@_inlineable // FIXME(sil-serialize-all)
215228
public var _cVarArgEncoding: [Int] {
216-
return _encodeBitsAsWords(Int32(self))
229+
return _encodeBitsAsWords(_VAInt(self))
217230
}
218231
}
219232

@@ -222,7 +235,7 @@ extension Int8 : CVarArg {
222235
/// appropriately interpreted by C varargs.
223236
@_inlineable // FIXME(sil-serialize-all)
224237
public var _cVarArgEncoding: [Int] {
225-
return _encodeBitsAsWords(Int32(self))
238+
return _encodeBitsAsWords(_VAInt(self))
226239
}
227240
}
228241

@@ -258,7 +271,7 @@ extension UInt32 : CVarArg {
258271
/// appropriately interpreted by C varargs.
259272
@_inlineable // FIXME(sil-serialize-all)
260273
public var _cVarArgEncoding: [Int] {
261-
return _encodeBitsAsWords(self)
274+
return _encodeBitsAsWords(_VAUInt(self))
262275
}
263276
}
264277

@@ -267,7 +280,7 @@ extension UInt16 : CVarArg {
267280
/// appropriately interpreted by C varargs.
268281
@_inlineable // FIXME(sil-serialize-all)
269282
public var _cVarArgEncoding: [Int] {
270-
return _encodeBitsAsWords(CUnsignedInt(self))
283+
return _encodeBitsAsWords(_VAUInt(self))
271284
}
272285
}
273286

@@ -276,7 +289,7 @@ extension UInt8 : CVarArg {
276289
/// appropriately interpreted by C varargs.
277290
@_inlineable // FIXME(sil-serialize-all)
278291
public var _cVarArgEncoding: [Int] {
279-
return _encodeBitsAsWords(CUnsignedInt(self))
292+
return _encodeBitsAsWords(_VAUInt(self))
280293
}
281294
}
282295

@@ -352,7 +365,7 @@ extension Double : _CVarArgPassedAsDouble, _CVarArgAligned {
352365
}
353366
}
354367

355-
#if arch(x86_64)
368+
#if arch(x86_64) || arch(s390x)
356369

357370
/// An object that can manage the lifetime of storage backing a
358371
/// `CVaListPointer`.
@@ -371,7 +384,7 @@ final internal class _VaListBuilder {
371384
internal var gp_offset = CUnsignedInt(0)
372385
@_versioned // FIXME(sil-serialize-all)
373386
internal var fp_offset =
374-
CUnsignedInt(_x86_64CountGPRegisters * MemoryLayout<Int>.stride)
387+
CUnsignedInt(_countGPRegisters * MemoryLayout<Int>.stride)
375388
@_versioned // FIXME(sil-serialize-all)
376389
internal var overflow_arg_area: UnsafeMutablePointer<Int>?
377390
@_versioned // FIXME(sil-serialize-all)
@@ -382,7 +395,7 @@ final internal class _VaListBuilder {
382395
@_versioned // FIXME(sil-serialize-all)
383396
internal init() {
384397
// prepare the register save area
385-
storage = ContiguousArray(repeating: 0, count: _x86_64RegisterSaveWords)
398+
storage = ContiguousArray(repeating: 0, count: _registerSaveWords)
386399
}
387400

388401
@_inlineable // FIXME(sil-serialize-all)
@@ -394,10 +407,11 @@ final internal class _VaListBuilder {
394407
internal func append(_ arg: CVarArg) {
395408
var encoded = arg._cVarArgEncoding
396409

410+
#if arch(x86_64)
397411
if arg is _CVarArgPassedAsDouble
398-
&& sseRegistersUsed < _x86_64CountSSERegisters {
399-
var startIndex = _x86_64CountGPRegisters
400-
+ (sseRegistersUsed * _x86_64SSERegisterWords)
412+
&& sseRegistersUsed < _countSSERegisters {
413+
var startIndex = _countGPRegisters
414+
+ (sseRegistersUsed * _sseRegisterWords)
401415
for w in encoded {
402416
storage[startIndex] = w
403417
startIndex += 1
@@ -406,7 +420,7 @@ final internal class _VaListBuilder {
406420
}
407421
else if encoded.count == 1
408422
&& !(arg is _CVarArgPassedAsDouble)
409-
&& gpRegistersUsed < _x86_64CountGPRegisters {
423+
&& gpRegistersUsed < _countGPRegisters {
410424
storage[gpRegistersUsed] = encoded[0]
411425
gpRegistersUsed += 1
412426
}
@@ -415,14 +429,27 @@ final internal class _VaListBuilder {
415429
storage.append(w)
416430
}
417431
}
432+
#elseif arch(s390x)
433+
if gpRegistersUsed < _countGPRegisters {
434+
for w in encoded {
435+
storage[gpRegistersUsed] = w
436+
gpRegistersUsed += 1
437+
}
438+
} else {
439+
for w in encoded {
440+
storage.append(w)
441+
}
442+
}
443+
#endif
444+
418445
}
419446

420447
@_inlineable // FIXME(sil-serialize-all)
421448
@_versioned // FIXME(sil-serialize-all)
422449
internal func va_list() -> CVaListPointer {
423450
header.reg_save_area = storage._baseAddress
424451
header.overflow_arg_area
425-
= storage._baseAddress + _x86_64RegisterSaveWords
452+
= storage._baseAddress + _registerSaveWords
426453
return CVaListPointer(
427454
_fromUnsafeMutablePointer: UnsafeMutableRawPointer(
428455
Builtin.addressof(&self.header)))

0 commit comments

Comments
 (0)