@@ -165,7 +165,43 @@ extension Constant where Repr == Struct {
165165 }
166166}
167167
168+ // MARK: Truncation
168169
170+ extension Constant where Repr == Signed {
171+ /// Creates a constant truncated to a given integral type.
172+ ///
173+ /// - parameter type: The type to truncate towards.
174+ ///
175+ /// - returns: A const value representing this value truncated to the given
176+ /// integral type's bitwidth.
177+ public func truncate( to type: IntType ) -> Constant < Signed > {
178+ return Constant < Signed > ( llvm: LLVMConstTrunc ( llvm, type. asLLVM ( ) ) )
179+ }
180+ }
181+
182+ extension Constant where Repr == Unsigned {
183+ /// Creates a constant truncated to a given integral type.
184+ ///
185+ /// - parameter type: The type to truncate towards.
186+ ///
187+ /// - returns: A const value representing this value truncated to the given
188+ /// integral type's bitwidth.
189+ public func truncate( to type: IntType ) -> Constant < Unsigned > {
190+ return Constant < Unsigned > ( llvm: LLVMConstTrunc ( llvm, type. asLLVM ( ) ) )
191+ }
192+ }
193+
194+ extension Constant where Repr == Floating {
195+ /// Creates a constant truncated to a given floating type.
196+ ///
197+ /// - parameter type: The type to truncate towards.
198+ ///
199+ /// - returns: A const value representing this value truncated to the given
200+ /// floating type's bitwidth.
201+ public func truncate( to type: FloatType ) -> Constant < Floating > {
202+ return Constant < Floating > ( llvm: LLVMConstFPTrunc ( llvm, type. asLLVM ( ) ) )
203+ }
204+ }
169205
170206// MARK: Arithmetic Operations
171207
@@ -936,6 +972,23 @@ extension Constant {
936972 }
937973}
938974
975+ // MARK: Constant Pointer To Integer
976+
977+ extension Constant where Repr: IntegralConstantRepresentation {
978+ /// Creates a constant pointer-to-integer operation to convert the given constant
979+ /// global pointer value to the given integer type.
980+ ///
981+ /// - parameter val: The pointer value.
982+ /// - parameter intType: The destination integer type.
983+ ///
984+ /// - returns: An constant value representing the constant value of the given
985+ /// pointer converted to the given integer type.
986+ public static func pointerToInt( _ val: IRGlobal , _ intType: IntType ) -> Constant {
987+ precondition ( val. isConstant, " May only convert global constant pointers to integers " )
988+ return Constant < Repr > ( llvm: LLVMConstPtrToInt ( val. asLLVM ( ) , intType. asLLVM ( ) ) )
989+ }
990+ }
991+
939992// MARK: Struct Operations
940993
941994extension Constant where Repr == Struct {
0 commit comments