unc_vm_compiler/jump_table.rs
1//! A jump table is a method of transferring program control (branching)
2//! to another part of a program (or a different program that may have
3//! been dynamically loaded) using a table of branch or jump instructions.
4//!
5//! [Learn more](https://en.wikipedia.org/wiki/Branch_table).
6
7use super::CodeOffset;
8use unc_vm_types::entity::{entity_impl, SecondaryMap};
9
10/// An opaque reference to a [jump table](https://en.wikipedia.org/wiki/Branch_table).
11///
12/// `JumpTable`s are used for indirect branching and are specialized for dense,
13/// 0-based jump offsets.
14#[derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive)]
15#[archive_attr(derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord))]
16#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
17pub struct JumpTable(u32);
18
19entity_impl!(JumpTable, "jt");
20entity_impl!(ArchivedJumpTable, "jt");
21
22impl JumpTable {
23 /// Create a new jump table reference from its number.
24 ///
25 /// This method is for use by the parser.
26 pub fn with_number(n: u32) -> Option<Self> {
27 if n < u32::max_value() {
28 Some(Self(n))
29 } else {
30 None
31 }
32 }
33}
34
35/// Code offsets for Jump Tables.
36pub type JumpTableOffsets = SecondaryMap<JumpTable, CodeOffset>;