Skip to content

Commit 6f8af5b

Browse files
committed
Add ptrdiff
1 parent 4000703 commit 6f8af5b

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Sources/LLVM/IRBuilder.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,6 +1253,27 @@ public class IRBuilder {
12531253
return LLVMSizeOf(val.asLLVM())
12541254
}
12551255

1256+
/// Builds an expression that returns the difference between two pointer
1257+
/// values, dividing out the size of the pointed-to objects.
1258+
///
1259+
/// This is intended to implement C-style pointer subtraction. As such, the
1260+
/// pointers must be appropriately aligned for their element types and
1261+
/// pointing into the same object.
1262+
///
1263+
/// - parameter lhs: The first pointer (the minuend).
1264+
/// - parameter rhs: The second pointer (the subtrahend).
1265+
/// - parameter name: The name for the newly inserted instruction.
1266+
///
1267+
/// - returns: A IRValue representing a 64-bit integer value of the difference
1268+
/// of the two pointer values modulo the size of the pointed-to objects.
1269+
public func buildPointerDifference(_ lhs: IRValue, _ rhs: IRValue, name: String = "") -> IRValue {
1270+
precondition(
1271+
lhs.type is PointerType && rhs.type is PointerType,
1272+
"Cannot take pointer diff of \(lhs.type) and \(rhs.type)."
1273+
)
1274+
return LLVMBuildPtrDiff(llvm, lhs.asLLVM(), rhs.asLLVM(), name)
1275+
}
1276+
12561277
// MARK: Atomic Instructions
12571278

12581279
/// Builds a fence instruction that introduces "happens-before" edges between
@@ -1264,6 +1285,7 @@ public class IRBuilder {
12641285
/// with other atomics in the same thread. (This is useful for interacting
12651286
/// with signal handlers.) Otherwise this fence is atomic with respect to
12661287
/// all other code in the system.
1288+
/// - parameter name: The name for the newly inserted instruction.
12671289
///
12681290
/// - returns: A value representing `void`.
12691291
public func buildFence(ordering: AtomicOrdering, singleThreaded: Bool = false, name: String = "") -> IRValue {

0 commit comments

Comments
 (0)