diff --git a/Sources/LLVM/FloatType.swift b/Sources/LLVM/FloatType.swift index 7c7b232d..c043ee8b 100644 --- a/Sources/LLVM/FloatType.swift +++ b/Sources/LLVM/FloatType.swift @@ -51,10 +51,27 @@ public struct FloatType: IRType { public static let ppcFP128 = FloatType(kind: .ppcFP128) /// Creates a constant floating value of this type from a Swift `Double` value. + /// + /// - parameter value: A Swift double value. + /// + /// - returns: A value representing a floating point constant initialized + /// with the given Swift double value. public func constant(_ value: Double) -> Constant { return Constant(llvm: LLVMConstReal(asLLVM(), value)) } + /// Creates a constant floating value of this type parsed from a string. + /// + /// - parameter value: A string value containing a float. + /// + /// - returns: A value representing a constant initialized with the result of + /// parsing the string as a floating point number. + public func constant(_ value: String) -> Constant { + return value.withCString { cString in + return Constant(llvm: LLVMConstRealOfStringAndSize(asLLVM(), cString, UInt32(value.count))) + } + } + /// Retrieves the underlying LLVM type object. public func asLLVM() -> LLVMTypeRef { switch kind { diff --git a/Sources/LLVM/IntType.swift b/Sources/LLVM/IntType.swift index 188a3929..8ada8cb6 100644 --- a/Sources/LLVM/IntType.swift +++ b/Sources/LLVM/IntType.swift @@ -50,6 +50,9 @@ public struct IntType: IRType { /// - parameter value: A Swift integer value. /// - parameter signExtend: Whether to sign-extend this value to fit this /// type's bit width. Defaults to `false`. + /// + /// - returns: A value representing an unsigned integer constant initialized + /// with the given Swift integer value. public func constant(_ value: IntTy, signExtend: Bool = false) -> Constant { return Constant(llvm: LLVMConstInt(asLLVM(), UInt64(value), @@ -61,12 +64,28 @@ public struct IntType: IRType { /// - parameter value: A Swift integer value. /// - parameter signExtend: Whether to sign-extend this value to fit this /// type's bit width. Defaults to `false`. + /// + /// - returns: A value representing a signed integer constant initialized with + /// the given Swift integer value. public func constant(_ value: IntTy, signExtend: Bool = false) -> Constant { return Constant(llvm: LLVMConstInt(asLLVM(), UInt64(bitPattern: Int64(value)), signExtend.llvm)) } + /// Creates a constant integer value of this type parsed from a string. + /// + /// - parameter value: A string value containing an integer. + /// - parameter radix: The radix, or base, to use for converting text to an + /// integer value. Defaults to 10. + /// + /// - returns: A value representing a constant initialized with the result of + /// parsing the string as a signed integer. + public func constant(_ value: String, radix: Int = 10) -> Constant { + return value.withCString { cString in + return Constant(llvm: LLVMConstIntOfStringAndSize(asLLVM(), cString, UInt32(value.count), UInt8(radix))) + } + } /// Retrieves an integer value of this type's bit width consisting of all /// one-bits. diff --git a/Sources/LLVM/StructType.swift b/Sources/LLVM/StructType.swift index aa3be8ec..587b7a08 100644 --- a/Sources/LLVM/StructType.swift +++ b/Sources/LLVM/StructType.swift @@ -64,7 +64,8 @@ public struct StructType: IRType { /// /// - returns: A value representing a constant value of this structure type. public func constant(values: [IRValue]) -> Constant { - assert(numericCast(values.count) == LLVMCountStructElementTypes(llvm), "The number of values must match the number of elements in the aggregate") + assert(numericCast(values.count) == LLVMCountStructElementTypes(llvm), + "The number of values must match the number of elements in the aggregate") var vals = values.map { $0.asLLVM() as Optional } return vals.withUnsafeMutableBufferPointer { buf in return Constant(llvm: LLVMConstNamedStruct(asLLVM(),