gaia-assembler 0.1.1

Universal assembler framework for Gaia project
Documentation
//! Gaia Assembler Core Type Definitions

use serde::{Deserialize, Serialize};

/// Gaia Address Spaces
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum AddressSpace {
    /// Generic or default address space.
    Generic,
    /// Stack or local memory.
    Local,
    /// Global memory.
    Global,
    /// Shared memory (e.g., GPU LDS).
    Shared,
    /// Constant memory.
    Constant,
    /// Managed heap (e.g., JVM/CLR).
    Managed,
}

/// Gaia Type System
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum GaiaType {
    // --- Scalar Types ---
    /// 1-bit boolean type.
    Bool,
    /// 8-bit signed integer.
    I8,
    /// 8-bit unsigned integer.
    U8,
    /// 16-bit signed integer.
    I16,
    /// 16-bit unsigned integer.
    U16,
    /// 32-bit signed integer.
    I32,
    /// 32-bit unsigned integer.
    U32,
    /// 64-bit signed integer.
    I64,
    /// 64-bit unsigned integer.
    U64,
    /// 16-bit half-precision floating point.
    F16,
    /// 32-bit single-precision floating point.
    F32,
    /// 64-bit double-precision floating point.
    F64,

    // --- Composite Types ---
    /// Pointer type (pointee type, address space).
    Pointer(Box<GaiaType>, AddressSpace),
    /// Array type (element type, length).
    Array(Box<GaiaType>, usize),
    /// Vector type (element type, count).
    Vector(Box<GaiaType>, usize),
    /// Struct type (name/ID).
    Struct(String),
    /// String type (managed or raw).
    String,

    // --- Managed Types ---
    /// Dynamic object type (e.g., Python/JS).
    Object,
    /// Managed class (e.g., JVM/CLR).
    Class(String),
    /// Interface or Trait.
    Interface(String),
    /// Dynamic or arbitrary type (Variant).
    Any,

    // --- Domain-Specific Types ---
    /// Tensor type (element type, shape).
    /// Shape uses -1 for dynamic dimensions.
    Tensor(Box<GaiaType>, Vec<isize>),

    // --- Special Types ---
    /// Void type.
    Void,
    /// Opaque type for external references.
    Opaque(String),
    /// Function pointer.
    FunctionPtr(Box<GaiaSignature>),
}

impl GaiaType {
    /// Check if this is an integer type.
    pub fn is_integer(&self) -> bool {
        match self {
            Self::I8 | Self::U8 | Self::I16 | Self::U16 | Self::I32 | Self::U32 | Self::I64 | Self::U64 => true,
            _ => false,
        }
    }

    /// Check if this is a floating point type.
    pub fn is_float(&self) -> bool {
        match self {
            Self::F16 | Self::F32 | Self::F64 => true,
            _ => false,
        }
    }
}

impl std::fmt::Display for GaiaType {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Self::Bool => write!(f, "bool"),
            Self::I8 => write!(f, "i8"),
            Self::U8 => write!(f, "u8"),
            Self::I16 => write!(f, "i16"),
            Self::U16 => write!(f, "u16"),
            Self::I32 => write!(f, "i32"),
            Self::U32 => write!(f, "u32"),
            Self::I64 => write!(f, "i64"),
            Self::U64 => write!(f, "u64"),
            Self::F16 => write!(f, "f16"),
            Self::F32 => write!(f, "f32"),
            Self::F64 => write!(f, "f64"),
            Self::Pointer(ty, _) => write!(f, "*{}", ty),
            Self::Array(ty, len) => write!(f, "[{}; {}]", ty, len),
            Self::Vector(ty, count) => write!(f, "vec{}<{}>", count, ty),
            Self::Struct(name) => write!(f, "struct {}", name),
            Self::String => write!(f, "string"),
            Self::Object => write!(f, "object"),
            Self::Class(name) => write!(f, "class {}", name),
            Self::Interface(name) => write!(f, "interface {}", name),
            Self::Any => write!(f, "any"),
            Self::Tensor(ty, shape) => write!(f, "tensor<{}; {:?}>", ty, shape),
            Self::Void => write!(f, "void"),
            Self::Opaque(name) => write!(f, "opaque {}", name),
            Self::FunctionPtr(sig) => write!(f, "fn({}) -> {}", sig.params.iter().map(|p| p.to_string()).collect::<Vec<_>>().join(", "), sig.return_type),
        }
    }
}



/// Function signature representing the type of a function.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct GaiaSignature {
    /// The parameter types of the function.
    pub params: Vec<GaiaType>,
    /// The return type of the function.
    pub return_type: GaiaType,
}

/// Type mapping utilities between UIR and Gaia IR
pub mod mapping;