gaia-assembler 0.1.1

Universal assembler framework for Gaia project
Documentation
use crate::{
    instruction::GaiaInstruction,
    types::{GaiaSignature, GaiaType},
};
use serde::{Deserialize, Serialize};

/// Gaia Program Module
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct GaiaModule {
    /// The name of the module.
    pub name: String,
    /// Functions defined in this module.
    pub functions: Vec<GaiaFunction>,
    /// Structs defined in this module.
    pub structs: Vec<GaiaStruct>,
    /// Classes defined in this module (for managed runtimes).
    pub classes: Vec<GaiaClass>,
    /// Constants defined in this module as name-value pairs.
    pub constants: Vec<(String, GaiaConstant)>,
    /// Global variables defined in this module.
    pub globals: Vec<GaiaGlobal>,
    /// External symbols imported by this module.
    pub imports: Vec<GaiaImport>,
}

/// External import entry representing a symbol from an external library.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct GaiaImport {
    /// The name of the external library.
    pub library: String,
    /// The symbol name to import from the library.
    pub symbol: String,
}

/// Gaia Class definition for object-oriented managed runtimes.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct GaiaClass {
    /// The name of the class.
    pub name: String,
    /// The parent class this class inherits from, if any.
    pub parent: Option<String>,
    /// Interfaces implemented by this class.
    pub interfaces: Vec<String>,
    /// Fields defined in this class.
    pub fields: Vec<GaiaField>,
    /// Methods defined in this class.
    pub methods: Vec<GaiaFunction>,
    /// Custom attributes applied to this class.
    pub attributes: Vec<String>,
}

/// Gaia Field definition representing a data member of a class.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct GaiaField {
    /// The name of the field.
    pub name: String,
    /// The type of the field.
    pub ty: GaiaType,
    /// Whether this field is static (class-level) or instance-level.
    pub is_static: bool,
    /// The visibility level of this field.
    pub visibility: Visibility,
}

/// Visibility level for type members.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Visibility {
    /// Accessible from any code that can see the declaring type.
    Public,
    /// Accessible only within the declaring type.
    Private,
    /// Accessible within the declaring type and derived types.
    Protected,
    /// Accessible within the same assembly or module.
    Internal,
}

/// Gaia Function definition representing a callable unit of code.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct GaiaFunction {
    /// The name of the function.
    pub name: String,
    /// The function signature including parameter types and return type.
    pub signature: GaiaSignature,
    /// Basic blocks that make up the function body.
    pub blocks: Vec<GaiaBlock>,
    /// Whether this is an external function (defined in another module/library).
    pub is_external: bool,
}

/// Gaia Basic Block representing a sequence of instructions with a single entry and exit point.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct GaiaBlock {
    /// The label identifying this block.
    pub label: String,
    /// The sequence of instructions in this block.
    pub instructions: Vec<GaiaInstruction>,
    /// The terminator instruction that ends this block.
    pub terminator: GaiaTerminator,
}

/// Terminator instructions (control flow).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum GaiaTerminator {
    /// Unconditional jump to the specified label.
    Jump(String),
    /// Conditional branch based on a boolean value.
    Branch {
        /// The label to jump to if the condition is true.
        true_label: String,
        /// The label to jump to if the condition is false.
        false_label: String,
    },
    /// Function return.
    Return,
    /// Call a function and then jump to another block.
    Call {
        /// The name of the function to call.
        callee: String,
        /// The number of arguments passed to the function.
        args_count: usize,
        /// The label of the block to continue after the call.
        next_block: String,
    },
    /// Halt or exit the program.
    Halt,
}

/// Gaia Struct definition representing a value type with named fields.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct GaiaStruct {
    /// The name of the struct type.
    pub name: String,
    /// The fields of the struct as (name, type) pairs.
    pub fields: Vec<(String, GaiaType)>,
}

/// Gaia Global Variable representing module-level data storage.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct GaiaGlobal {
    /// The name of the global variable.
    pub name: String,
    /// The type of the global variable.
    pub ty: GaiaType,
    /// The initial value of the global variable, if any.
    pub initial_value: Option<GaiaConstant>,
}

/// Gaia Constant representing a compile-time constant value.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum GaiaConstant {
    /// Boolean constant value.
    Bool(bool),
    /// 8-bit signed integer constant.
    I8(i8),
    /// 8-bit unsigned integer constant.
    U8(u8),
    /// 16-bit signed integer constant.
    I16(i16),
    /// 16-bit unsigned integer constant.
    U16(u16),
    /// 32-bit signed integer constant.
    I32(i32),
    /// 32-bit unsigned integer constant.
    U32(u32),
    /// 64-bit signed integer constant.
    I64(i64),
    /// 64-bit unsigned integer constant.
    U64(u64),
    /// 32-bit floating point constant.
    F32(f32),
    /// 64-bit floating point constant.
    F64(f64),
    /// String constant value.
    String(String),
    /// Raw binary data used for weights, constant arrays, etc.
    Blob(Vec<u8>),
    /// Constant array of values.
    Array(Vec<GaiaConstant>),
    /// Null value or null reference.
    Null,
}