Skip to main content

lust/jit/codegen/
mod.rs

1pub(super) use super::specialization::{SpecializationRegistry, SpecializedLayout};
2pub(super) use super::trace::InlineTrace;
3pub(super) use super::trace::ValueType;
4pub(super) use super::{CompiledTrace, Guard, GuardKind, Trace, TraceId, TraceOp};
5pub(super) use crate::bytecode::{Function, Value, ValueTag};
6pub(super) use crate::jit;
7pub(super) use crate::Result;
8pub(super) use alloc::vec::Vec;
9pub(super) use core::mem;
10pub(super) use dynasmrt::{dynasm, x64::Assembler, DynasmApi, DynasmLabelApi};
11use hashbrown::HashMap;
12
13/// Minimum stack allocation size for traces. Individual traces can request more
14/// space depending on how many specialized values they materialize.
15/// Must stay (8 mod 16) to preserve SysV stack alignment guarantees.
16pub(super) const MIN_JIT_STACK_SIZE: i32 = 504;
17
18/// Base offset for specialized value allocations (must avoid saved registers at rbp-40)
19pub(super) const SPECIALIZED_BASE_OFFSET: i32 = -64;
20/// Size (in bytes) reserved per specialized value (ptr + len + cap + padding)
21pub(super) const SPECIALIZED_SLOT_SIZE: i32 = 32;
22/// Extra stack space required before the first specialized slot to avoid the
23/// saved callee-saved registers (rbp-8 through rbp-40).
24pub(super) const SPECIALIZED_STACK_BASE: i32 = 64;
25mod arithmetic;
26mod builder;
27mod comparisons;
28mod guards;
29mod logic;
30mod memory;
31mod registers;
32mod specialization;
33/// Tracks a specialized value in the JIT trace
34#[derive(Debug, Clone)]
35pub(super) struct SpecializedValue {
36    pub stack_offset: i32,
37}
38
39pub struct JitCompiler {
40    pub(super) ops: Assembler,
41    pub(super) leaked_constants: Vec<*const Value>,
42    fail_stack: Vec<dynasmrt::DynamicLabel>,
43    exit_stack: Vec<dynasmrt::DynamicLabel>,
44    inline_depth: usize,
45    /// Registry for type specializations
46    pub(super) specialization_registry: SpecializationRegistry,
47    /// Track active specialized values in trace
48    pub(super) specialized_values: HashMap<usize, SpecializedValue>,
49    /// Next ID for specialized values
50    pub(super) next_specialized_id: usize,
51}
52
53impl Default for JitCompiler {
54    fn default() -> Self {
55        Self::new()
56    }
57}