Skip to content

Commit 8ffc7b4

Browse files
CodaFiharlanhaskins
authored andcommitted
Add ptrdiff
1 parent 8f83387 commit 8ffc7b4

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

Sources/LLVM/BasicBlock.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public struct BasicBlock: IRValue {
8888
}
8989

9090
extension BasicBlock {
91-
/// A `Address` represents a function-relative address of a basic block for
91+
/// An `Address` represents a function-relative address of a basic block for
9292
/// use with the `indirectbr` instruction.
9393
public struct Address: IRValue {
9494
internal let llvm: LLVMValueRef

Sources/LLVM/Function.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,14 @@ public class Function: IRGlobal {
9090
}
9191
}
9292

93-
/// Computes the address of the specified basic block in this function, and
94-
/// always has an `i8*` type.
93+
/// Computes the address of the specified basic block in this function.
9594
///
9695
/// Taking the address of the entry block is illegal.
9796
///
9897
/// This value only has defined behavior when used as an operand to the
9998
/// `indirectbr` instruction, or for comparisons against null. Pointer
100-
/// equality tests between labels addresses results in undefined behavior
101-
/// — though, again, comparison against null is ok, and no label is equal to
99+
/// equality tests between labels addresses results in undefined behavior.
100+
/// Though, again, comparison against null is ok, and no label is equal to
102101
/// the null pointer. This may be passed around as an opaque pointer sized
103102
/// value as long as the bits are not inspected. This allows `ptrtoint` and
104103
/// arithmetic to be performed on these values so long as the original value

Sources/LLVM/IRBuilder.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ public class IRBuilder {
911911
}
912912

913913
/// Build a return from the current function back to the calling function with
914-
/// the given array of values in aggregate.
914+
/// the given array of values as members of an aggregate.
915915
///
916916
/// - parameter values: The values to insert as members of the returned aggregate.
917917
///
@@ -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)