Skip to content

Add constant GEP #154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 13, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Sources/LLVM/Constant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,44 @@ extension Constant where Repr == Struct {
return LLVMConstExtractValue(asLLVM(), buf.baseAddress, UInt32(buf.count))
}
}

/// Build a constant `GEP` (Get Element Pointer) instruction with a resultant
/// value that is undefined if the address is outside the actual underlying
/// allocated object and not the address one-past-the-end.
///
/// The `GEP` instruction is often the source of confusion. LLVM [provides a
/// document](http://llvm.org/docs/GetElementPtr.html) to answer questions
/// around its semantics and correct usage.
///
/// - parameter indices: A list of indices that indicate which of the elements
/// of the aggregate object are indexed.
///
/// - returns: A value representing the address of a subelement of the given
/// aggregate data structure value.
public func getElementPointer(indices: [IRValue]) -> IRValue {
var indices = indices.map({ $0.asLLVM() as LLVMValueRef? })
return indices.withUnsafeMutableBufferPointer { buf in
return LLVMConstGEP(asLLVM(), buf.baseAddress, UInt32(buf.count))
}
}

/// Build a GEP (Get Element Pointer) instruction.
///
/// The `GEP` instruction is often the source of confusion. LLVM [provides a
/// document](http://llvm.org/docs/GetElementPtr.html) to answer questions
/// around its semantics and correct usage.
///
/// - parameter indices: A list of indices that indicate which of the elements
/// of the aggregate object are indexed.
///
/// - returns: A value representing the address of a subelement of the given
/// aggregate data structure value.
public func inBoundsGetElementPointer(indices: [IRValue]) -> IRValue {
var indices = indices.map({ $0.asLLVM() as LLVMValueRef? })
return indices.withUnsafeMutableBufferPointer { buf in
return LLVMConstInBoundsGEP(asLLVM(), buf.baseAddress, UInt32(buf.count))
}
}
}

// MARK: Vector Operations
Expand Down