Skip to content

Commit 259a2b1

Browse files
committed
Distinguish signed and unsigned constants
1 parent d8572a3 commit 259a2b1

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

Sources/LLVM/IntType.swift

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,29 @@ public struct IntType: IRType {
3535
return null()
3636
}
3737

38-
/// Creates an integer constant value with the given Swift integer value.
38+
/// Creates an unsigned integer constant value with the given Swift integer value.
3939
///
4040
/// - parameter value: A Swift integer value.
4141
/// - parameter signExtend: Whether to sign-extend this value to fit this
4242
/// type's bit width. Defaults to `false`.
43-
public func constant<IntTy: Integer>(_ value: IntTy, signExtend: Bool = false) -> IRValue {
44-
return LLVMConstInt(asLLVM(),
45-
UInt64(bitPattern: value.toIntMax()),
46-
signExtend.llvm)
43+
public func constant<IntTy: UnsignedInteger>(_ value: IntTy, signExtend: Bool = false) -> Constant {
44+
return Constant(llvm: LLVMConstInt(asLLVM(),
45+
unsafeBitCast(value.toIntMax(), to: UInt64.self),
46+
signExtend.llvm), representation: .unsigned)
4747
}
4848

49+
/// Creates a signed integer constant value with the given Swift integer value.
50+
///
51+
/// - parameter value: A Swift integer value.
52+
/// - parameter signExtend: Whether to sign-extend this value to fit this
53+
/// type's bit width. Defaults to `false`.
54+
public func constant<IntTy: SignedInteger>(_ value: IntTy, signExtend: Bool = false) -> Constant {
55+
return Constant(llvm: LLVMConstInt(asLLVM(),
56+
unsafeBitCast(value.toIntMax(), to: UInt64.self),
57+
signExtend.llvm), representation: .signed)
58+
}
59+
60+
4961
/// Retrieves an integer value of this type's bit width consisting of all
5062
/// one-bits.
5163
///

0 commit comments

Comments
 (0)