near_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 near_vm_types::entity::{SecondaryMap, entity_impl};
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#[rkyv(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() { Some(Self(n)) } else { None }
28    }
29}
30
31/// Code offsets for Jump Tables.
32pub type JumpTableOffsets = SecondaryMap<JumpTable, CodeOffset>;