Skip to content

Add constant initializers that parse strings #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Sources/LLVM/FloatType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Floating> {
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<Floating> {
return value.withCString { cString in
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think this needs withCString, but 👌

return Constant(llvm: LLVMConstRealOfStringAndSize(asLLVM(), cString, UInt32(value.count)))
}
}

/// Retrieves the underlying LLVM type object.
public func asLLVM() -> LLVMTypeRef {
switch kind {
Expand Down
19 changes: 19 additions & 0 deletions Sources/LLVM/IntType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<IntTy: UnsignedInteger>(_ value: IntTy, signExtend: Bool = false) -> Constant<Unsigned> {
return Constant(llvm: LLVMConstInt(asLLVM(),
UInt64(value),
Expand All @@ -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<IntTy: SignedInteger>(_ value: IntTy, signExtend: Bool = false) -> Constant<Signed> {
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<Signed> {
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.
Expand Down
3 changes: 2 additions & 1 deletion Sources/LLVM/StructType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Struct> {
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(),
Expand Down