|
1 | 1 | import CLLVM |
2 | 2 |
|
3 | 3 | /// Functions in this group model ValueRef instances that correspond to constants referring to scalar types. |
4 | | -public enum ScalarConstants { |
| 4 | +public enum ScalarConstant { |
5 | 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 } |
| 6 | + public static func constInt(intTy: TypeRef, n: UInt64, signExtend: Bool) -> Value { |
| 7 | + let valueRef = LLVMConstInt(intTy.typeRef, n, signExtend.llvm)! |
8 | 8 | return Value(llvm: valueRef) |
9 | 9 | } |
10 | 10 |
|
11 | 11 | /// Obtain a constant value for an integer of arbitrary precision. |
12 | 12 | public static func constIntOfArbitraryPrecision(intType: TypeRef, words: [UInt64]) -> Value? { |
13 | 13 | let numWords = UInt32(words.count) |
14 | | - let val = words.withUnsafeBufferPointer { bufferPointer in |
15 | | - LLVMConstIntOfArbitraryPrecision(intType.typeRef, numWords, bufferPointer.baseAddress) |
| 14 | + let valueRef = words.withUnsafeBufferPointer { bufferPointer in |
| 15 | + LLVMConstIntOfArbitraryPrecision(intType.typeRef, numWords, bufferPointer.baseAddress)! |
16 | 16 | } |
17 | | - guard let valueRef = val else { return nil } |
18 | 17 | return Value(llvm: valueRef) |
19 | 18 | } |
20 | 19 |
|
21 | 20 | /// Obtain a constant value for an integer parsed from a string. |
22 | 21 | /// A similar API, `constIntOfStringAndSize` is also available. If the |
23 | 22 | /// 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) |
| 23 | + public static func constIntOfString(intType: TypeRef, text: String, radix: UInt8) -> Value { |
| 24 | + let valueRef = text.withCString { cString in |
| 25 | + LLVMConstIntOfString(intType.typeRef, cString, radix)! |
27 | 26 | } |
28 | | - guard let valueRef = val else { return nil } |
29 | 27 | return Value(llvm: valueRef) |
30 | 28 | } |
31 | 29 |
|
32 | 30 | /// 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 |
| 31 | + public static func constIntOfStringAndSize(intType: TypeRef, text: String, radix: UInt8) -> Value { |
| 32 | + let valueRef = text.withCString { cString in |
35 | 33 | let length = UInt32(text.utf8.count) |
36 | | - return LLVMConstIntOfStringAndSize(intType.typeRef, cString, length, radix) |
| 34 | + return LLVMConstIntOfStringAndSize(intType.typeRef, cString, length, radix)! |
37 | 35 | } |
38 | | - guard let valueRef = val else { return nil } |
39 | 36 | return Value(llvm: valueRef) |
40 | 37 | } |
41 | 38 |
|
42 | 39 | /// 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 } |
| 40 | + public static func constReal(realType: TypeRef, n: Double) -> Value { |
| 41 | + let valueRef = LLVMConstReal(realType.typeRef, n)! |
45 | 42 | return Value(llvm: valueRef) |
46 | 43 | } |
47 | 44 |
|
48 | 45 | /// Obtain a constant for a floating point value parsed from a string. |
49 | 46 | /// A similar API, LLVMConstRealOfStringAndSize is also available. It |
50 | 47 | /// 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) |
| 48 | + public static func constRealOfString(realType: TypeRef, text: String) -> Value { |
| 49 | + let valueRef = text.withCString { cString in |
| 50 | + LLVMConstRealOfString(realType.typeRef, cString)! |
54 | 51 | } |
55 | | - guard let valueRef = val else { return nil } |
56 | 52 | return Value(llvm: valueRef) |
57 | 53 | } |
58 | 54 |
|
59 | 55 | /// 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 |
| 56 | + public static func constRealOfStringAndSize(realType: TypeRef, text: String) -> Value { |
| 57 | + let valueRef = text.withCString { cString in |
62 | 58 | let length = UInt32(text.utf8.count) |
63 | | - return LLVMConstRealOfStringAndSize(realType.typeRef, cString, length) |
| 59 | + return LLVMConstRealOfStringAndSize(realType.typeRef, cString, length)! |
64 | 60 | } |
65 | | - guard let valueRef = val else { return nil } |
66 | 61 | return Value(llvm: valueRef) |
67 | 62 | } |
68 | 63 |
|
69 | 64 | /// 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 | | - |
| 65 | + public static func constIntZExtValue(constantVal: ValueRef) -> UInt64 { |
| 66 | + LLVMConstIntGetZExtValue(constantVal.valueRef) |
| 67 | + } |
75 | 68 |
|
76 | 69 | /// 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 | | -// } |
| 70 | + public static func constIntSExtValue(constantVal: ValueRef) -> Int64 { |
| 71 | + LLVMConstIntGetSExtValue(constantVal.valueRef) |
| 72 | + } |
81 | 73 |
|
82 | 74 | /// Obtain the double value for an floating point constant value. |
83 | 75 | /// 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 | | -// } |
| 76 | + public static func constRealGetDouble(constantVal: ValueRef) -> (Double, Bool) { |
| 77 | + var losesInfo: LLVMBool = 0 |
| 78 | + let val = LLVMConstRealGetDouble(constantVal.valueRef, &losesInfo) |
| 79 | + return (val, losesInfo != 0) |
| 80 | + } |
90 | 81 | } |
0 commit comments