Skip to content

Commit da0ac16

Browse files
authored
Merge pull request #154 from CodaFi/gep
Add constant GEP
2 parents 75f3bb5 + 1dbab1f commit da0ac16

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

Sources/LLVM/Constant.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,44 @@ extension Constant where Repr == Struct {
10151015
return LLVMConstExtractValue(asLLVM(), buf.baseAddress, UInt32(buf.count))
10161016
}
10171017
}
1018+
1019+
/// Build a constant `GEP` (Get Element Pointer) instruction with a resultant
1020+
/// value that is undefined if the address is outside the actual underlying
1021+
/// allocated object and not the address one-past-the-end.
1022+
///
1023+
/// The `GEP` instruction is often the source of confusion. LLVM [provides a
1024+
/// document](http://llvm.org/docs/GetElementPtr.html) to answer questions
1025+
/// around its semantics and correct usage.
1026+
///
1027+
/// - parameter indices: A list of indices that indicate which of the elements
1028+
/// of the aggregate object are indexed.
1029+
///
1030+
/// - returns: A value representing the address of a subelement of the given
1031+
/// aggregate data structure value.
1032+
public func getElementPointer(indices: [IRValue]) -> IRValue {
1033+
var indices = indices.map({ $0.asLLVM() as LLVMValueRef? })
1034+
return indices.withUnsafeMutableBufferPointer { buf in
1035+
return LLVMConstGEP(asLLVM(), buf.baseAddress, UInt32(buf.count))
1036+
}
1037+
}
1038+
1039+
/// Build a GEP (Get Element Pointer) instruction.
1040+
///
1041+
/// The `GEP` instruction is often the source of confusion. LLVM [provides a
1042+
/// document](http://llvm.org/docs/GetElementPtr.html) to answer questions
1043+
/// around its semantics and correct usage.
1044+
///
1045+
/// - parameter indices: A list of indices that indicate which of the elements
1046+
/// of the aggregate object are indexed.
1047+
///
1048+
/// - returns: A value representing the address of a subelement of the given
1049+
/// aggregate data structure value.
1050+
public func inBoundsGetElementPointer(indices: [IRValue]) -> IRValue {
1051+
var indices = indices.map({ $0.asLLVM() as LLVMValueRef? })
1052+
return indices.withUnsafeMutableBufferPointer { buf in
1053+
return LLVMConstInBoundsGEP(asLLVM(), buf.baseAddress, UInt32(buf.count))
1054+
}
1055+
}
10181056
}
10191057

10201058
// MARK: Vector Operations

0 commit comments

Comments
 (0)