@@ -5,16 +5,72 @@ import cllvm
55/// A `BasicBlock` represents a basic block in an LLVM IR program. A basic
66/// block contains a sequence of instructions, a pointer to its parent block and
77/// its follower block, and an optional label that gives the basic block an
8- /// entry in the symbol table.
8+ /// entry in the symbol table. Because of this label, the type of every basic
9+ /// block is `LabelType`.
910///
1011/// A basic block can be thought of as a sequence of instructions, and indeed
11- /// its member instructions may be iterated over with a `for-in` loop.
12+ /// its member instructions may be iterated over with a `for-in` loop. A well-
13+ /// formed basic block has as its last instruction a "terminator" that produces
14+ /// a transfer of control flow and possibly yields a value. All other
15+ /// instructions in the middle of the basic block may not be "terminator"
16+ /// instructions. Basic blocks are not required to be well-formed until
17+ /// code generation is complete.
1218///
13- /// The first basic block in a function is special in two ways: it is
14- /// immediately executed on entrance to the function, and it is not allowed to
15- /// have predecessor basic blocks (i.e. there can not be any branches to the
16- /// entry block of a function). Because the block can have no predecessors, it
17- /// also cannot have any PHI nodes.
19+ /// Creating a Basic Block
20+ /// ======================
21+ ///
22+ /// By default, the initializer for a basic block merely creates the block but
23+ /// does not associate it with a function.
24+ ///
25+ /// let module = Module(name: "Example")
26+ /// let fun = builder.addFunction("example",
27+ /// type: FunctionType(argTypes: [],
28+ /// returnType: VoidType()))
29+ ///
30+ /// // This basic block is "floating" outside of a function.
31+ /// let floatingBB = BasicBlock(name: "floating")
32+ /// // Until we associate it with a function by calling `Function.append(_:)`.
33+ /// fun.append(floatingBB)
34+ ///
35+ /// A basic block may be created and automatically inserted at the end of a
36+ /// function by calling `Function.appendBasicBlock(named:in:)`.
37+ ///
38+ /// let module = Module(name: "Example")
39+ /// let fun = builder.addFunction("example",
40+ /// type: FunctionType(argTypes: [],
41+ /// returnType: VoidType()))
42+ ///
43+ /// // This basic block is "attached" to the example function.
44+ /// let attachedBB = fun.appendBasicBlock(named: "attached")
45+ ///
46+ /// The Address of a Basic Block
47+ /// ============================
48+ ///
49+ /// Basic blocks (except the entry block) may have their labels appear in the
50+ /// symbol table. Naturally, these labels are associated with address values
51+ /// in the final object file. The value of that address may be accessed for the
52+ /// purpose of an indirect call or a direct comparisson by calling
53+ /// `Function.address(of:)` and providing one of the function's child blocks as
54+ /// an argument. Providing any other basic block outside of the function as an
55+ /// argument value is undefined.
56+ ///
57+ /// The Entry Block
58+ /// ===============
59+ ///
60+ /// The first basic block (the entry block) in a `Function` is special:
61+ ///
62+ /// - The entry block is immediately executed when the flo wof control enters
63+ /// its parent function.
64+ /// - The entry block is not allowed to have predecessor basic blocks
65+ /// (i.e. there cannot be any branches to the entry block of a function).
66+ /// - The address of the entry block is not a well-defined value.
67+ /// - The entry block cannot have PHI nodes. This is enforced structurally,
68+ /// as the entry block can have no predecessor blocks to serve as operands
69+ /// to the PHI node.
70+ /// - Static `alloca` instructions situated in the entry block are treated
71+ /// specially by most LLVM backends. For example, FastISel keeps track of
72+ /// static `alloca` values in the entry block to more efficiently reference
73+ /// them from the base pointer of the stack frame.
1874public struct BasicBlock : IRValue {
1975 internal let llvm : LLVMBasicBlockRef
2076
0 commit comments