lehit 0.2.0

Embeddable Lehit language frontend and Cranelift JIT runtime
Documentation
use crate::{
    parsing::Span,
    semantic::{GlobalFunctionId, SemanticType},
};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct LocalId(pub u32);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct PlaceId(pub u32);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct IrValueId(pub u32);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct BasicBlockId(pub u32);

#[derive(Debug, Clone, PartialEq)]
pub struct TypedBody {
    pub locals: Vec<TypedLocal>,
    pub places: Vec<TypedPlace>,
    pub entry: BasicBlockId,
    pub blocks: Vec<TypedBasicBlock>,
    pub return_type: SemanticType,
    pub return_cleanup: ReturnCleanupPlan,
}

impl TypedBody {
    pub fn block(&self, id: BasicBlockId) -> Option<&TypedBasicBlock> {
        self.blocks.get(id.0 as usize)
    }

    pub fn entry_block(&self) -> &TypedBasicBlock {
        self.block(self.entry)
            .expect("TypedBody entry must reference an existing block")
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct TypedBasicBlock {
    pub id: BasicBlockId,
    pub parameters: Vec<TypedValue>,
    pub instructions: Vec<TypedInstruction>,
    pub terminator: TypedTerminator,
}

#[derive(Debug, Clone, PartialEq)]
pub enum TypedTerminator {
    Return(Option<IrValueId>),
    Jump {
        target: BasicBlockId,
        arguments: Vec<IrValueId>,
    },
    Branch {
        condition: IrValueId,
        then_block: BasicBlockId,
        else_block: BasicBlockId,
    },
    Unreachable,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TypedLocal {
    pub id: LocalId,
    pub name: String,
    pub type_: SemanticType,
    pub mutable: bool,
    pub parameter: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TypedPlace {
    pub id: PlaceId,
    pub base: PlaceBase,
    pub projections: Vec<PlaceProjection>,
    pub type_: SemanticType,
    pub mutable: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PlaceBase {
    Local(LocalId),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PlaceProjection {
    Dereference,
    Field(String),
    EnumTag,
    EnumPayload(u32),
    TupleField(usize),
    ConstantIndex(usize),
    Index(IrValueId),
}

#[derive(Debug, Clone, PartialEq)]
pub struct TypedInstruction {
    pub span: Span,
    pub result: Option<TypedValue>,
    pub kind: TypedInstructionKind,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TypedValue {
    pub id: IrValueId,
    pub type_: SemanticType,
    pub storage_place: Option<PlaceId>,
    pub borrow_origins: Vec<PlaceId>,
}

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct CheckedCleanupPlan {
    pub drops: Vec<CheckedDropAction>,
}

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ReturnCleanupPlan {
    pub drops: Vec<ReturnDropAction>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReturnDropAction {
    pub path: Vec<ReturnPlaceProjection>,
    pub type_: SemanticType,
    pub function: GlobalFunctionId,
    pub conditions: Vec<ReturnEnumCondition>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReturnEnumCondition {
    pub enum_path: Vec<ReturnPlaceProjection>,
    pub enum_type: SemanticType,
    pub tag_bits: u16,
    pub matching_tags: Vec<u128>,
    pub fallback: bool,
    pub explicit_tags: Vec<u128>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ReturnPlaceProjection {
    Dereference,
    Field(String),
    EnumPayload(u32),
    TupleField(usize),
    ConstantIndex(usize),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CheckedDropAction {
    pub place: PlaceId,
    pub function: GlobalFunctionId,
    /// All conditions must select their variant before this Drop is executed.
    pub conditions: Vec<CheckedEnumCondition>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CheckedEnumCondition {
    pub enum_place: PlaceId,
    pub tag_bits: u16,
    pub matching_tags: Vec<u128>,
    /// A fallback variant matches when none of the enum's explicit tags match.
    pub fallback: bool,
    pub explicit_tags: Vec<u128>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum TypedInstructionKind {
    Constant(TypedConstant),
    Load(PlaceId),
    Store {
        place: PlaceId,
        value: IrValueId,
    },
    Borrow {
        place: PlaceId,
        mutable: bool,
    },
    BoundsCheck {
        index: IrValueId,
        length: usize,
        cleanup: CheckedCleanupPlan,
    },
    IntegerEqual {
        left: IrValueId,
        right: IrValueId,
    },
    Drop {
        place: PlaceId,
        function: GlobalFunctionId,
        conditions: Vec<CheckedEnumCondition>,
    },
    Call {
        function: GlobalFunctionId,
        arguments: Vec<IrValueId>,
        cleanup: CheckedCleanupPlan,
    },
}

#[derive(Debug, Clone, PartialEq)]
pub enum TypedConstant {
    Void,
    Bool(bool),
    Char(char),
    Signed(i128),
    Unsigned(u128),
    Float(f64),
    String(String),
}