Skip to content

Commit 1f04fd4

Browse files
authored
Merge pull request #110 from CodaFi/string-cheese
Add constant initializers that parse strings
2 parents 3b13110 + 01e8d50 commit 1f04fd4

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

Sources/LLVM/FloatType.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,27 @@ public struct FloatType: IRType {
5151
public static let ppcFP128 = FloatType(kind: .ppcFP128)
5252

5353
/// Creates a constant floating value of this type from a Swift `Double` value.
54+
///
55+
/// - parameter value: A Swift double value.
56+
///
57+
/// - returns: A value representing a floating point constant initialized
58+
/// with the given Swift double value.
5459
public func constant(_ value: Double) -> Constant<Floating> {
5560
return Constant(llvm: LLVMConstReal(asLLVM(), value))
5661
}
5762

63+
/// Creates a constant floating value of this type parsed from a string.
64+
///
65+
/// - parameter value: A string value containing a float.
66+
///
67+
/// - returns: A value representing a constant initialized with the result of
68+
/// parsing the string as a floating point number.
69+
public func constant(_ value: String) -> Constant<Floating> {
70+
return value.withCString { cString in
71+
return Constant(llvm: LLVMConstRealOfStringAndSize(asLLVM(), cString, UInt32(value.count)))
72+
}
73+
}
74+
5875
/// Retrieves the underlying LLVM type object.
5976
public func asLLVM() -> LLVMTypeRef {
6077
switch kind {

Sources/LLVM/IntType.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ public struct IntType: IRType {
5050
/// - parameter value: A Swift integer value.
5151
/// - parameter signExtend: Whether to sign-extend this value to fit this
5252
/// type's bit width. Defaults to `false`.
53+
///
54+
/// - returns: A value representing an unsigned integer constant initialized
55+
/// with the given Swift integer value.
5356
public func constant<IntTy: UnsignedInteger>(_ value: IntTy, signExtend: Bool = false) -> Constant<Unsigned> {
5457
return Constant(llvm: LLVMConstInt(asLLVM(),
5558
UInt64(value),
@@ -61,12 +64,28 @@ public struct IntType: IRType {
6164
/// - parameter value: A Swift integer value.
6265
/// - parameter signExtend: Whether to sign-extend this value to fit this
6366
/// type's bit width. Defaults to `false`.
67+
///
68+
/// - returns: A value representing a signed integer constant initialized with
69+
/// the given Swift integer value.
6470
public func constant<IntTy: SignedInteger>(_ value: IntTy, signExtend: Bool = false) -> Constant<Signed> {
6571
return Constant(llvm: LLVMConstInt(asLLVM(),
6672
UInt64(bitPattern: Int64(value)),
6773
signExtend.llvm))
6874
}
6975

76+
/// Creates a constant integer value of this type parsed from a string.
77+
///
78+
/// - parameter value: A string value containing an integer.
79+
/// - parameter radix: The radix, or base, to use for converting text to an
80+
/// integer value. Defaults to 10.
81+
///
82+
/// - returns: A value representing a constant initialized with the result of
83+
/// parsing the string as a signed integer.
84+
public func constant(_ value: String, radix: Int = 10) -> Constant<Signed> {
85+
return value.withCString { cString in
86+
return Constant(llvm: LLVMConstIntOfStringAndSize(asLLVM(), cString, UInt32(value.count), UInt8(radix)))
87+
}
88+
}
7089

7190
/// Retrieves an integer value of this type's bit width consisting of all
7291
/// one-bits.

Sources/LLVM/StructType.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ public struct StructType: IRType {
6464
///
6565
/// - returns: A value representing a constant value of this structure type.
6666
public func constant(values: [IRValue]) -> Constant<Struct> {
67-
assert(numericCast(values.count) == LLVMCountStructElementTypes(llvm), "The number of values must match the number of elements in the aggregate")
67+
assert(numericCast(values.count) == LLVMCountStructElementTypes(llvm),
68+
"The number of values must match the number of elements in the aggregate")
6869
var vals = values.map { $0.asLLVM() as Optional }
6970
return vals.withUnsafeMutableBufferPointer { buf in
7071
return Constant(llvm: LLVMConstNamedStruct(asLLVM(),

0 commit comments

Comments
 (0)