Skip to content

Commit f20adb8

Browse files
committed
Scalar constants
1 parent 169d1de commit f20adb8

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import CLLVM
2+
3+
/// Functions in this group model ValueRef instances that correspond to constants referring to scalar types.
4+
public enum ScalarConstants {
5+
/// Obtain a constant value for an integer type.
6+
public static func constInt(intTy: TypeRef, n: UInt64, signExtend: Bool) -> Value? {
7+
guard let valueRef = LLVMConstInt(intTy.typeRef, n, signExtend.llvm) else { return nil }
8+
return Value(llvm: valueRef)
9+
}
10+
11+
/// Obtain a constant value for an integer of arbitrary precision.
12+
public static func constIntOfArbitraryPrecision(intType: TypeRef, words: [UInt64]) -> Value? {
13+
let numWords = UInt32(words.count)
14+
let val = words.withUnsafeBufferPointer { bufferPointer in
15+
LLVMConstIntOfArbitraryPrecision(intType.typeRef, numWords, bufferPointer.baseAddress)
16+
}
17+
guard let valueRef = val else { return nil }
18+
return Value(llvm: valueRef)
19+
}
20+
21+
/// Obtain a constant value for an integer parsed from a string.
22+
/// A similar API, `constIntOfStringAndSize` is also available. If the
23+
/// string's length is available, it is preferred to call that function instead.
24+
public static func constIntOfString(intType: TypeRef, text: String, radix: UInt8) -> Value? {
25+
let val = text.withCString { cString in
26+
LLVMConstIntOfString(intType.typeRef, cString, radix)
27+
}
28+
guard let valueRef = val else { return nil }
29+
return Value(llvm: valueRef)
30+
}
31+
32+
/// Obtain a constant value for an integer parsed from a string with specified length.
33+
public static func constIntOfStringAndSize(intType: TypeRef, text: String, radix: UInt8) -> Value? {
34+
let val = text.withCString { cString in
35+
let length = UInt32(text.utf8.count)
36+
return LLVMConstIntOfStringAndSize(intType.typeRef, cString, length, radix)
37+
}
38+
guard let valueRef = val else { return nil }
39+
return Value(llvm: valueRef)
40+
}
41+
42+
/// Obtain a constant value referring to a double floating point value.
43+
public static func constReal(realType: TypeRef, n: Double) -> Value? {
44+
guard let valueRef = LLVMConstReal(realType.typeRef, n) else { return nil }
45+
return Value(llvm: valueRef)
46+
}
47+
48+
/// Obtain a constant for a floating point value parsed from a string.
49+
/// A similar API, LLVMConstRealOfStringAndSize is also available. It
50+
/// should be used if the input string's length is known.
51+
public static func constRealOfString(realType: TypeRef, text: String) -> Value? {
52+
let val = text.withCString { cString in
53+
LLVMConstRealOfString(realType.typeRef, cString)
54+
}
55+
guard let valueRef = val else { return nil }
56+
return Value(llvm: valueRef)
57+
}
58+
59+
/// Obtain a constant for a floating point value parsed from a string.
60+
public static func constRealOfStringAndSize(realType: TypeRef, text: String) -> Value? {
61+
let val = text.withCString { cString in
62+
let length = UInt32(text.utf8.count)
63+
return LLVMConstRealOfStringAndSize(realType.typeRef, cString, length)
64+
}
65+
guard let valueRef = val else { return nil }
66+
return Value(llvm: valueRef)
67+
}
68+
69+
/// Obtain the zero extended value for an integer constant value.
70+
// unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal);
71+
// public static func getLLVMConstIntZExtValue(constantVal: LLVMValueRef) -> UInt64 {
72+
// return LLVMConstIntGetZExtValue(constantVal)
73+
// }
74+
75+
76+
/// Obtain the sign extended value for an integer constant value.
77+
// long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal);
78+
// public static func constIntSExtValue(constantVal: LLVMValueRef) -> Int64 {
79+
// return LLVMConstIntGetSExtValue(constantVal)
80+
// }
81+
82+
/// Obtain the double value for an floating point constant value.
83+
/// losesInfo indicates if some precision was lost in the conversion.
84+
// double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *losesInfo);
85+
// public static func constRealGetDouble(constantVal: LLVMValueRef) -> (Double, Bool) {
86+
// var losesInfo: LLVMBool = 0
87+
// let result = LLVMConstRealGetDouble(constantVal, &losesInfo)
88+
// return (result, losesInfo != 0)
89+
// }
90+
}

0 commit comments

Comments
 (0)