Skip to content

Commit 59139c6

Browse files
jyn514abrown
authored andcommitted
Add more documentation for entities, fixes bytecodealliance#1038 (bytecodealliance#1041)
Also fixes broken links to point to docs.rs; see bytecodealliance/cranelift#1041 (comment), there doesn't currently seem to be a way to link to a type in a dependent crate.
1 parent e252e0f commit 59139c6

File tree

1 file changed

+114
-9
lines changed

1 file changed

+114
-9
lines changed

cranelift/codegen/src/ir/entities.rs

+114-9
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ use core::u32;
2525
#[cfg(feature = "enable-serde")]
2626
use serde::{Deserialize, Serialize};
2727

28-
/// An opaque reference to an extended basic block in a function.
28+
/// An opaque reference to an [extended basic
29+
/// block](https://en.wikipedia.org/wiki/Extended_basic_block) in a
30+
/// [`Function`](super::function::Function).
31+
///
32+
/// You can get an `Ebb` using
33+
/// [`FunctionBuilder::create_ebb`](https://docs.rs/cranelift-frontend/*/cranelift_frontend/struct.FunctionBuilder.html#method.create_ebb)
2934
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
3035
pub struct Ebb(u32);
3136
entity_impl!(Ebb, "ebb");
@@ -44,6 +49,18 @@ impl Ebb {
4449
}
4550

4651
/// An opaque reference to an SSA value.
52+
///
53+
/// You can get a constant `Value` from the following
54+
/// [`InstBuilder`](super::InstBuilder) instructions:
55+
///
56+
/// - [`iconst`](super::InstBuilder::iconst) for integer constants
57+
/// - [`f32const`](super::InstBuilder::f32const) for 32-bit float constants
58+
/// - [`f64const`](super::InstBuilder::f64const) for 64-bit float constants
59+
/// - [`bconst`](super::InstBuilder::bconst) for boolean constants
60+
/// - [`vconst`](super::InstBuilder::vconst) for vector constants
61+
/// - [`null`](super::InstBuilder::null) for null reference constants
62+
///
63+
/// Any `InstBuilder` instruction that has an output will also return a `Value`.
4764
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
4865
pub struct Value(u32);
4966
entity_impl!(Value, "v");
@@ -62,12 +79,34 @@ impl Value {
6279
}
6380
}
6481

65-
/// An opaque reference to an instruction in a function.
82+
/// An opaque reference to an instruction in a [`Function`](super::Function).
83+
///
84+
/// Most usage of `Inst` is internal. `Inst`ructions are returned by
85+
/// [`InstBuilder`](super::InstBuilder) instructions that do not return a
86+
/// [`Value`], such as control flow and trap instructions.
87+
///
88+
/// If you look around the API, you can find many inventive uses for `Inst`,
89+
/// such as [annotating specific instructions with a comment][inst_comment]
90+
/// or [performing reflection at compile time](super::DataFlowGraph::analyze_branch)
91+
/// on the type of instruction.
92+
///
93+
/// [inst_comment]: https://github.com/bjorn3/rustc_codegen_cranelift/blob/0f8814fd6da3d436a90549d4bb19b94034f2b19c/src/pretty_clif.rs
6694
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
6795
pub struct Inst(u32);
6896
entity_impl!(Inst, "inst");
6997

7098
/// An opaque reference to a stack slot.
99+
///
100+
/// Stack slots represent an address on the
101+
/// [call stack](https://en.wikipedia.org/wiki/Call_stack).
102+
///
103+
/// `StackSlot`s can be created with
104+
/// [`FunctionBuilder::create_stackslot`](https://docs.rs/cranelift-frontend/*/cranelift_frontend/struct.FunctionBuilder.html#method.create_stack_slot).
105+
///
106+
/// `StackSlot`s are most often used with
107+
/// [`stack_addr`](super::InstBuilder::stack_addr),
108+
/// [`stack_load`](super::InstBuilder::stack_load), and
109+
/// [`stack_store`](super::InstBuilder::stack_store).
71110
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
72111
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
73112
pub struct StackSlot(u32);
@@ -87,6 +126,22 @@ impl StackSlot {
87126
}
88127

89128
/// An opaque reference to a global value.
129+
///
130+
/// A `GlobalValue` is a [`Value`](Value) that will be live across the entire
131+
/// function lifetime. It can be preloaded from other global values.
132+
///
133+
/// You can create a `GlobalValue` in the following ways:
134+
///
135+
/// - When compiling to WASM, you can use it to load values from a
136+
/// [`VmContext`](super::GlobalValueData::VMContext) using
137+
/// [`FuncEnvironment::make_global`](https://docs.rs/cranelift-wasm/*/cranelift_wasm/trait.FuncEnvironment.html#tymethod.make_global).
138+
/// - When compiling to native code, you can use it for objects in static memory with
139+
/// [`Module::declare_data_in_func`](https://docs.rs/cranelift-module/*/cranelift_module/struct.Module.html#method.declare_data_in_func).
140+
/// - For any compilation target, it can be registered with
141+
/// [`FunctionBuilder::create_global_value`](https://docs.rs/cranelift-frontend/*/cranelift_frontend/struct.FunctionBuilder.html#method.create_global_value).
142+
///
143+
/// `GlobalValue`s can be retrieved with
144+
/// [`InstBuilder:global_value`](super::InstBuilder::global_value).
90145
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
91146
pub struct GlobalValue(u32);
92147
entity_impl!(GlobalValue, "gv");
@@ -104,7 +159,11 @@ impl GlobalValue {
104159
}
105160
}
106161

107-
/// An opaque reference to a constant
162+
/// An opaque reference to a constant.
163+
///
164+
/// You can store [`ConstantData`](super::ConstantData) in a
165+
/// [`ConstantPool`](super::ConstantPool) for efficient storage and retrieval.
166+
/// See [`ConstantPool::insert`](super::ConstantPool::insert).
108167
#[derive(Copy, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
109168
pub struct Constant(u32);
110169
entity_impl!(Constant, "const");
@@ -122,7 +181,16 @@ impl Constant {
122181
}
123182
}
124183

125-
/// An opaque reference to a jump table.
184+
/// An opaque reference to a [jump table](https://en.wikipedia.org/wiki/Branch_table).
185+
///
186+
/// `JumpTable`s are used for indirect branching and are specialized for dense,
187+
/// 0-based jump offsets. If you want a jump table which doesn't start at 0,
188+
/// or is not contiguous, consider using a [`Switch`](https://docs.rs/cranelift-frontend/*/cranelift_frontend/struct.Switch.html) instead.
189+
///
190+
/// `JumpTable` are used with [`br_table`](super::InstBuilder::br_table).
191+
///
192+
/// `JumpTable`s can be created with
193+
/// [`create_jump_table`](https://docs.rs/cranelift-frontend/*/cranelift_frontend/struct.FunctionBuilder.html#method.create_jump_table).
126194
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
127195
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
128196
pub struct JumpTable(u32);
@@ -141,7 +209,22 @@ impl JumpTable {
141209
}
142210
}
143211

144-
/// A reference to an external function.
212+
/// An opaque reference to another [`Function`](super::Function).
213+
///
214+
/// `FuncRef`s are used for [direct](super::InstBuilder::call) function calls
215+
/// and by [`func_addr`](super::InstBuilder::func_addr) for use in
216+
/// [indirect](super::InstBuilder::call_indirect) function calls.
217+
///
218+
/// `FuncRef`s can be created with
219+
///
220+
/// - [`FunctionBuilder::import_function`](https://docs.rs/cranelift-frontend/*/cranelift_frontend/struct.FunctionBuilder.html#method.import_function)
221+
/// for external functions
222+
/// - [`Module::declare_func_in_func`](https://docs.rs/cranelift-module/*/cranelift_module/struct.Module.html#method.declare_func_in_func)
223+
/// for functions declared elsewhere in the same native
224+
/// [`Module`](https://docs.rs/cranelift-module/*/cranelift_module/struct.Module.html)
225+
/// - [`FuncEnvironment::make_direct_func`](https://docs.rs/cranelift-wasm/*/cranelift_wasm/trait.FuncEnvironment.html#tymethod.make_direct_func)
226+
/// for functions declared in the same WebAssembly
227+
/// [`FuncEnvironment`](https://docs.rs/cranelift-wasm/*/cranelift_wasm/trait.FuncEnvironment.html#tymethod.make_direct_func)
145228
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
146229
pub struct FuncRef(u32);
147230
entity_impl!(FuncRef, "fn");
@@ -159,7 +242,18 @@ impl FuncRef {
159242
}
160243
}
161244

162-
/// A reference to a function signature.
245+
/// An opaque reference to a function [`Signature`](super::Signature).
246+
///
247+
/// `SigRef`s are used to declare a function with
248+
/// [`FunctionBuiler::import_function`](https://docs.rs/cranelift-frontend/*/cranelift_frontend/struct.FunctionBuilder.html#method.import_function)
249+
/// as well as to make an [indirect function call](super::InstBuilder::call_indirect).
250+
///
251+
/// `SigRef`s can be created with
252+
/// [`FunctionBuilder::import_signature`](https://docs.rs/cranelift-frontend/*/cranelift_frontend/struct.FunctionBuilder.html#method.import_signature).
253+
///
254+
/// You can retrieve the [`Signature`](super::Signature) that was used to create a `SigRef` with
255+
/// [`FunctionBuilder::signature`](https://docs.rs/cranelift-frontend/*/cranelift_frontend/struct.FunctionBuilder.html#method.signature) or
256+
/// [`func.dfg.signatures`](super::dfg::DataFlowGraph::signatures).
163257
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
164258
pub struct SigRef(u32);
165259
entity_impl!(SigRef, "sig");
@@ -177,7 +271,12 @@ impl SigRef {
177271
}
178272
}
179273

180-
/// A reference to a heap.
274+
/// An opaque reference to a [heap](https://en.wikipedia.org/wiki/Memory_management#DYNAMIC).
275+
///
276+
/// Heaps are used to access dynamically allocated memory through
277+
/// [`heap_addr`](super::InstBuilder::heap_addr).
278+
///
279+
/// To create a heap, use [`FunctionBuilder::create_heap`](https://docs.rs/cranelift-frontend/*/cranelift_frontend/struct.FunctionBuilder.html#method.create_heap).
181280
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
182281
pub struct Heap(u32);
183282
entity_impl!(Heap, "heap");
@@ -195,7 +294,13 @@ impl Heap {
195294
}
196295
}
197296

198-
/// A reference to a table.
297+
/// An opaque reference to a [WebAssembly
298+
/// table](https://developer.mozilla.org/en-US/docs/WebAssembly/Understanding_the_text_format#WebAssembly_tables).
299+
///
300+
/// `Table`s are used to store a list of function references.
301+
/// They can be created with [`FuncEnvironment::make_table`](https://docs.rs/cranelift-wasm/*/cranelift_wasm/trait.FuncEnvironment.html#tymethod.make_table).
302+
/// They can be used with
303+
/// [`FuncEnvironment::translate_call_indirect`](https://docs.rs/cranelift-wasm/*/cranelift_wasm/trait.FuncEnvironment.html#tymethod.translate_call_indirect).
199304
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
200305
pub struct Table(u32);
201306
entity_impl!(Table, "table");
@@ -213,7 +318,7 @@ impl Table {
213318
}
214319
}
215320

216-
/// A reference to any of the entities defined in this module that can appear in CLIF IR.
321+
/// An opaque reference to any of the entities defined in this module that can appear in CLIF IR.
217322
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
218323
pub enum AnyEntity {
219324
/// The whole function.

0 commit comments

Comments
 (0)