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 layout: SpecializedLayout,
37    pub stack_offset: i32,
38    /// Original register it came from (for debugging)
39    pub source_reg: Option<u8>,
40}
41
42pub struct JitCompiler {
43    pub(super) ops: Assembler,
44    pub(super) leaked_constants: Vec<*const Value>,
45    fail_stack: Vec<dynasmrt::DynamicLabel>,
46    exit_stack: Vec<dynasmrt::DynamicLabel>,
47    inline_depth: usize,
48    /// Registry for type specializations
49    pub(super) specialization_registry: SpecializationRegistry,
50    /// Track active specialized values in trace
51    pub(super) specialized_values: HashMap<usize, SpecializedValue>,
52    /// Next ID for specialized values
53    pub(super) next_specialized_id: usize,
54}
55
56impl Default for JitCompiler {
57    fn default() -> Self {
58        Self::new()
59    }
60}