Skip to content

Commit 49aa000

Browse files
CodaFiharlanhaskins
authored andcommitted
Document BasicBlock and Instruction (#2)
1 parent 6a8d271 commit 49aa000

File tree

1 file changed

+100
-70
lines changed

1 file changed

+100
-70
lines changed

Sources/LLVMSwift/BasicBlock.swift

Lines changed: 100 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,109 @@
11
import cllvm
22

3+
/// A `BasicBlock` represents a basic block in an LLVM IR program. A basic
4+
/// block contains a sequence of instructions, a pointer to its parent block and
5+
/// its follower block, and an optional label that gives the basic block an
6+
/// entry in the symbol table.
7+
///
8+
/// A basic block can be thought of as a sequence of instructions, and indeed
9+
/// its member instructions may be iterated over with a `for-in` loop.
10+
///
11+
/// The first basic block in a function is special in two ways: it is
12+
/// immediately executed on entrance to the function, and it is not allowed to
13+
/// have predecessor basic blocks (i.e. there can not be any branches to the
14+
/// entry block of a function). Because the block can have no predecessors, it
15+
/// also cannot have any PHI nodes.
316
public struct BasicBlock: IRValue, Sequence {
4-
internal let llvm: LLVMBasicBlockRef
5-
public init(llvm: LLVMBasicBlockRef) {
6-
self.llvm = llvm
7-
}
8-
9-
public var firstInstruction: Instruction? {
10-
guard let val = LLVMGetFirstInstruction(llvm) else { return nil }
11-
return Instruction(llvm: val)
12-
}
13-
14-
public var lastInstruction: Instruction? {
15-
guard let val = LLVMGetLastInstruction(llvm) else { return nil }
16-
return Instruction(llvm: val)
17-
}
18-
19-
public func parent() -> BasicBlock? {
20-
guard let blockRef = LLVMGetBasicBlockParent(llvm) else { return nil }
21-
return BasicBlock(llvm: blockRef)
22-
}
23-
24-
public func asLLVM() -> LLVMValueRef {
25-
return llvm
26-
}
27-
28-
public func next() -> BasicBlock? {
29-
guard let blockRef = LLVMGetNextBasicBlock(llvm) else { return nil }
30-
return BasicBlock(llvm: blockRef)
31-
}
32-
33-
public func delete() {
34-
LLVMDeleteBasicBlock(llvm)
35-
}
36-
37-
public func removeFromParent() {
38-
LLVMRemoveBasicBlockFromParent(llvm)
39-
}
40-
41-
public func moveBefore(_ block: BasicBlock) {
42-
LLVMMoveBasicBlockBefore(llvm, block.llvm)
43-
}
44-
45-
public func moveAfter(_ block: BasicBlock) {
46-
LLVMMoveBasicBlockAfter(llvm, block.llvm)
47-
}
48-
49-
public func makeIterator() -> AnyIterator<Instruction> {
50-
var current = firstInstruction
51-
return AnyIterator {
52-
defer { current = current?.next() }
53-
return current
54-
}
17+
internal let llvm: LLVMBasicBlockRef
18+
19+
/// Creates a `BasicBlock` from an `LLVMBasicBlockRef` object.
20+
public init(llvm: LLVMBasicBlockRef) {
21+
self.llvm = llvm
22+
}
23+
24+
/// Retrieves the underlying LLVM value object.
25+
public func asLLVM() -> LLVMValueRef {
26+
return llvm
27+
}
28+
29+
/// Returns the first instruction in the basic block, if it exists.
30+
public var firstInstruction: Instruction? {
31+
guard let val = LLVMGetFirstInstruction(llvm) else { return nil }
32+
return Instruction(llvm: val)
33+
}
34+
35+
/// Returns the first instruction in the basic block, if it exists.
36+
public var lastInstruction: Instruction? {
37+
guard let val = LLVMGetLastInstruction(llvm) else { return nil }
38+
return Instruction(llvm: val)
39+
}
40+
41+
/// Returns the parent of this basic block, if it exists.
42+
public func parent() -> BasicBlock? {
43+
guard let blockRef = LLVMGetBasicBlockParent(llvm) else { return nil }
44+
return BasicBlock(llvm: blockRef)
45+
}
46+
47+
/// Returns the basic block following this basic block, if it exists.
48+
public func next() -> BasicBlock? {
49+
guard let blockRef = LLVMGetNextBasicBlock(llvm) else { return nil }
50+
return BasicBlock(llvm: blockRef)
51+
}
52+
53+
/// Removes this basic block from a function and deletes it.
54+
public func delete() {
55+
LLVMDeleteBasicBlock(llvm)
56+
}
57+
58+
/// Removes this basic block from a function but keeps it alive.
59+
public func removeFromParent() {
60+
LLVMRemoveBasicBlockFromParent(llvm)
61+
}
62+
63+
/// Moves this basic block before the given basic block.
64+
public func moveBefore(_ block: BasicBlock) {
65+
LLVMMoveBasicBlockBefore(llvm, block.llvm)
66+
}
67+
68+
/// Moves this basic block after the given basic block.
69+
public func moveAfter(_ block: BasicBlock) {
70+
LLVMMoveBasicBlockAfter(llvm, block.llvm)
71+
}
72+
73+
/// Returns an iterator over the `Instruction`s that make up this basic block.
74+
public func makeIterator() -> AnyIterator<Instruction> {
75+
var current = firstInstruction
76+
return AnyIterator {
77+
defer { current = current?.next() }
78+
return current
5579
}
80+
}
5681
}
5782

83+
/// An `Instruction` represents an instruction residing in a basic block.
5884
public struct Instruction: IRValue {
59-
internal let llvm: LLVMValueRef
60-
61-
public init(llvm: LLVMValueRef) {
62-
self.llvm = llvm
63-
}
64-
65-
public func asLLVM() -> LLVMValueRef {
66-
return llvm
67-
}
68-
69-
public func previous() -> Instruction? {
70-
guard let val = LLVMGetPreviousInstruction(llvm) else { return nil }
71-
return Instruction(llvm: val)
72-
}
73-
74-
public func next() -> Instruction? {
75-
guard let val = LLVMGetNextInstruction(llvm) else { return nil }
76-
return Instruction(llvm: val)
77-
}
85+
internal let llvm: LLVMValueRef
86+
87+
/// Creates an `Intruction` from an `LLVMValueRef` object.
88+
public init(llvm: LLVMValueRef) {
89+
self.llvm = llvm
90+
}
91+
92+
/// Retrieves the underlying LLVM value object.
93+
public func asLLVM() -> LLVMValueRef {
94+
return llvm
95+
}
96+
97+
/// Obtain the instruction that occurs before this one, if it exists.
98+
public func previous() -> Instruction? {
99+
guard let val = LLVMGetPreviousInstruction(llvm) else { return nil }
100+
return Instruction(llvm: val)
101+
}
102+
103+
/// Obtain the instruction that occurs after this one, if it exists.
104+
public func next() -> Instruction? {
105+
guard let val = LLVMGetNextInstruction(llvm) else { return nil }
106+
return Instruction(llvm: val)
107+
}
78108
}
79109

0 commit comments

Comments
 (0)