gaia-assembler 0.1.1

Universal assembler framework for Gaia project
Documentation
use crate::types::GaiaType;
use gaia_types::neural::NeuralNode;
use serde::{Deserialize, Serialize};

/// Gaia Instruction System (Layered Architecture)
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum GaiaInstruction {
    /// Core low-level instructions (Tier 0)
    Core(CoreInstruction),
    /// Managed runtime instructions (Tier 1)
    Managed(ManagedInstruction),
    /// Domain-specific instructions (Tier 2)
    Domain(DomainInstruction),
}

/// Tier 0: Core low-level instructions (LLVM/Assembly-like)
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum CoreInstruction {
    // --- Memory Operations ---
    /// Allocate space on the stack (type, count)
    Alloca(GaiaType, usize),
    /// Load from memory (target register type, pointer)
    Load(GaiaType),
    /// Store to memory (value type)
    Store(GaiaType),
    /// Get element pointer (calculate offset)
    Gep {
        /// The base type for the pointer arithmetic.
        base_type: GaiaType,
        /// The indices to navigate through the type hierarchy.
        indices: Vec<usize>,
    },

    // --- Arithmetic Operations (operands popped from stack) ---
    /// Add two values of the specified type.
    Add(GaiaType),
    /// Subtract two values of the specified type.
    Sub(GaiaType),
    /// Multiply two values of the specified type.
    Mul(GaiaType),
    /// Divide two values of the specified type.
    Div(GaiaType),
    /// Remainder of two values of the specified type.
    Rem(GaiaType),
    /// Bitwise AND of two values of the specified type.
    And(GaiaType),
    /// Bitwise OR of two values of the specified type.
    Or(GaiaType),
    /// Bitwise XOR of two values of the specified type.
    Xor(GaiaType),
    /// Left shift a value of the specified type.
    Shl(GaiaType),
    /// Right shift a value of the specified type.
    Shr(GaiaType),
    /// Negate a value of the specified type.
    Neg(GaiaType),
    /// Bitwise NOT of a value of the specified type.
    Not(GaiaType),

    // --- Comparison Operations ---
    /// Compare two values using the specified condition.
    Cmp(CmpCondition, GaiaType),

    // --- Type Conversions ---
    /// Cast a value from one type to another with the specified conversion kind.
    Cast {
        /// The source type.
        from: GaiaType,
        /// The target type.
        to: GaiaType,
        /// The kind of cast operation to perform.
        kind: CastKind,
    },

    // --- Stack Management ---
    /// Push a constant value onto the stack.
    PushConstant(crate::program::GaiaConstant),
    /// Pop a value from the stack.
    Pop,
    /// Duplicate the top value on the stack.
    Dup,

    // --- Local Variables and Parameters (Tier 0 version) ---
    /// Load local variable
    LoadLocal(u32, GaiaType),
    /// Store local variable
    StoreLocal(u32, GaiaType),
    /// Load parameter
    LoadArg(u32, GaiaType),
    /// Store parameter
    StoreArg(u32, GaiaType),

    // --- Control Flow ---
    /// Return
    Ret,
    /// Unconditional branch
    Br(String),
    /// Branch if true
    BrTrue(String),
    /// Branch if false
    BrFalse(String),
    /// Label
    Label(String),
    /// Call function (function name, parameter count)
    Call(String, usize),
    /// Indirect call (parameter count). Stack: [..., func_ptr, arg1, arg2, ...]
    CallIndirect(usize),

    // --- Object and Array Operations ---
    /// Create new object (type name)
    New(String),
    /// Create new array (element type, whether length is on stack)
    NewArray(GaiaType, bool),
    /// Load field (object type, field name)
    LoadField(String, String),
    /// Store field (object type, field name)
    StoreField(String, String),
    /// Load array element
    LoadElement(GaiaType),
    /// Store array element
    StoreElement(GaiaType),
    /// Get array length
    ArrayLength,
    /// Push element to array (array, value)
    ArrayPush,
    
    // --- Exception Handling ---
    /// Throw exception
    Throw,

    // --- WASM GC Extension Instructions ---
    /// Create new GC struct (type name)
    StructNew(String),
    /// Get GC struct field (type name, field index)
    StructGet {
        /// The name of the struct type.
        struct_name: String,
        /// The index of the field to get.
        field_index: u32,
        /// Whether the field value is signed.
        is_signed: bool,
    },
    /// Set GC struct field (type name, field index)
    StructSet {
        /// The name of the struct type.
        struct_name: String,
        /// The index of the field to set.
        field_index: u32,
    },
    /// Create new GC array (type name)
    ArrayNew(String),
    /// Get GC array element (type name)
    ArrayGet {
        /// The name of the array type.
        array_name: String,
        /// Whether the element value is signed.
        is_signed: bool,
    },
    /// Set GC array element (type name)
    ArraySet(String),
}

/// Comparison condition for compare instructions.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum CmpCondition {
    /// Equal comparison.
    Eq,
    /// Not equal comparison.
    Ne,
    /// Less than comparison.
    Lt,
    /// Less than or equal comparison.
    Le,
    /// Greater than comparison.
    Gt,
    /// Greater than or equal comparison.
    Ge,
}

/// Cast kind for type conversion instructions.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum CastKind {
    /// Bitwise cast between types of the same size.
    Bitcast,
    /// Truncate a value to a smaller type.
    Trunc,
    /// Zero-extend a value to a larger type.
    Zext,
    /// Sign-extend a value to a larger type.
    Sext,
    /// Convert floating point to unsigned integer.
    FpToUi,
    /// Convert floating point to signed integer.
    FpToSi,
    /// Convert unsigned integer to floating point.
    UiToFp,
    /// Convert signed integer to floating point.
    SiToFp,
}

/// Tier 1: Managed runtime instructions (JVM/CLR/Lua-like)
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ManagedInstruction {
    /// Call an instance method on an object.
    CallMethod {
        /// The target type containing the method.
        target: String,
        /// The name of the method to call.
        method: String,
        /// The signature of the method.
        signature: crate::types::GaiaSignature,
        /// Whether this is a virtual method call.
        is_virtual: bool,
        /// The call site ID for inline caching.
        call_site_id: Option<u32>,
    },
    /// Call a static method.
    CallStatic {
        /// The target type containing the static method.
        target: String,
        /// The name of the static method to call.
        method: String,
        /// The signature of the method.
        signature: crate::types::GaiaSignature,
    },
    /// Box a value type into a reference type.
    Box(GaiaType),
    /// Unbox a reference type back to a value type.
    Unbox(GaiaType),
    /// Runtime type checking (is instance of).
    InstanceOf(GaiaType),
    /// Type casting with runtime check.
    CheckCast(GaiaType),
    /// Initialize an object with the given parameter count.
    Initiate(usize),
    /// Finalize an object (call destructor).
    Finalize,
}

/// Tier 2: Domain-specific instructions (Neural network/Tensor/Parallel computing)
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum DomainInstruction {
    /// Neural network operator node.
    Neural(NeuralNode),

    // --- Basic Tensor Operations ---
    /// Matrix multiplication operation.
    MatMul {
        /// The shape of matrix A.
        a_shape: Vec<usize>,
        /// The shape of matrix B.
        b_shape: Vec<usize>,
        /// Whether to transpose matrix A before multiplication.
        transpose_a: bool,
        /// Whether to transpose matrix B before multiplication.
        transpose_b: bool,
    },
    /// 2D Convolution operation.
    Conv2D {
        /// The stride values [height, width].
        stride: [usize; 2],
        /// The padding values [height, width].
        padding: [usize; 2],
        /// The dilation values [height, width].
        dilation: [usize; 2],
        /// The number of groups for grouped convolution.
        groups: usize,
    },
    /// Element-wise operation on tensors.
    ElementWise(GaiaType, String),

    // --- Parallel Computing ---
    /// Get the thread ID in the specified dimension.
    GetThreadId(usize),
    /// Get the group size in the specified dimension.
    GetGroupSize(usize),
    /// Barrier synchronization for all threads in a group.
    Barrier,
}