Skip to content

Model Terminator instructions #82

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
Mar 30, 2017
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions Sources/LLVM/BasicBlock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ public struct BasicBlock: IRValue {
return Instruction(llvm: val)
}

/// Returns the terminator instruction if this basic block is well formed or
/// `nil` if it is not well formed.
public var terminator: TerminatorInstruction? {
guard let term = LLVMGetBasicBlockTerminator(llvm) else { return nil }
return TerminatorInstruction(llvm: term)
}

/// Returns the parent function of this basic block, if it exists.
public var parent: Function? {
guard let functionRef = LLVMGetBasicBlockParent(llvm) else { return nil }
Expand Down
27 changes: 27 additions & 0 deletions Sources/LLVM/Instruction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,30 @@ public struct Instruction: IRValue {
}
}
}

/// A `TerminatorInstruction` represents an instruction that terminates a
/// basic block.
public struct TerminatorInstruction {
internal let llvm: LLVMValueRef

/// Creates a `TerminatorInstruction` from an `LLVMValueRef` object.
public init(llvm: LLVMValueRef) {
self.llvm = llvm
}

/// Retrieves the number of successors of this terminator instruction.
public var successorCount: Int {
return Int(LLVMGetNumSuccessors(llvm))
}

/// Returns the successor block at the specified index, if it exists.
public func getSuccessor(at idx: Int) -> BasicBlock? {
guard let succ = LLVMGetSuccessor(llvm, UInt32(idx)) else { return nil }
return BasicBlock(llvm: succ)
}

/// Updates the successor block at the specified index.
public func setSuccessor(at idx: Int, to bb: BasicBlock) {
LLVMSetSuccessor(llvm, UInt32(idx), bb.asLLVM())
}
}