Skip to content

Commit a399453

Browse files
authored
Merge pull request #82 from CodaFi/the-schwartz
Model Terminator instructions
2 parents 4b12d9a + 0928bc1 commit a399453

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

Sources/LLVM/BasicBlock.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ public struct BasicBlock: IRValue {
4040
return Instruction(llvm: val)
4141
}
4242

43+
/// Returns the terminator instruction if this basic block is well formed or
44+
/// `nil` if it is not well formed.
45+
public var terminator: TerminatorInstruction? {
46+
guard let term = LLVMGetBasicBlockTerminator(llvm) else { return nil }
47+
return TerminatorInstruction(llvm: term)
48+
}
49+
4350
/// Returns the parent function of this basic block, if it exists.
4451
public var parent: Function? {
4552
guard let functionRef = LLVMGetBasicBlockParent(llvm) else { return nil }

Sources/LLVM/Instruction.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,30 @@ public struct Instruction: IRValue {
5151
}
5252
}
5353
}
54+
55+
/// A `TerminatorInstruction` represents an instruction that terminates a
56+
/// basic block.
57+
public struct TerminatorInstruction {
58+
internal let llvm: LLVMValueRef
59+
60+
/// Creates a `TerminatorInstruction` from an `LLVMValueRef` object.
61+
public init(llvm: LLVMValueRef) {
62+
self.llvm = llvm
63+
}
64+
65+
/// Retrieves the number of successors of this terminator instruction.
66+
public var successorCount: Int {
67+
return Int(LLVMGetNumSuccessors(llvm))
68+
}
69+
70+
/// Returns the successor block at the specified index, if it exists.
71+
public func getSuccessor(at idx: Int) -> BasicBlock? {
72+
guard let succ = LLVMGetSuccessor(llvm, UInt32(idx)) else { return nil }
73+
return BasicBlock(llvm: succ)
74+
}
75+
76+
/// Updates the successor block at the specified index.
77+
public func setSuccessor(at idx: Int, to bb: BasicBlock) {
78+
LLVMSetSuccessor(llvm, UInt32(idx), bb.asLLVM())
79+
}
80+
}

0 commit comments

Comments
 (0)