Skip to main content

gaia_assembler/program/
mod.rs

1use crate::{
2    instruction::GaiaInstruction,
3    types::{GaiaSignature, GaiaType},
4};
5use serde::{Deserialize, Serialize};
6
7/// Gaia Program Module
8#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
9pub struct GaiaModule {
10    /// The name of the module.
11    pub name: String,
12    /// Functions defined in this module.
13    pub functions: Vec<GaiaFunction>,
14    /// Structs defined in this module.
15    pub structs: Vec<GaiaStruct>,
16    /// Classes defined in this module (for managed runtimes).
17    pub classes: Vec<GaiaClass>,
18    /// Constants defined in this module as name-value pairs.
19    pub constants: Vec<(String, GaiaConstant)>,
20    /// Global variables defined in this module.
21    pub globals: Vec<GaiaGlobal>,
22    /// External symbols imported by this module.
23    pub imports: Vec<GaiaImport>,
24}
25
26/// External import entry representing a symbol from an external library.
27#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
28pub struct GaiaImport {
29    /// The name of the external library.
30    pub library: String,
31    /// The symbol name to import from the library.
32    pub symbol: String,
33}
34
35/// Gaia Class definition for object-oriented managed runtimes.
36#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
37pub struct GaiaClass {
38    /// The name of the class.
39    pub name: String,
40    /// The parent class this class inherits from, if any.
41    pub parent: Option<String>,
42    /// Interfaces implemented by this class.
43    pub interfaces: Vec<String>,
44    /// Fields defined in this class.
45    pub fields: Vec<GaiaField>,
46    /// Methods defined in this class.
47    pub methods: Vec<GaiaFunction>,
48    /// Custom attributes applied to this class.
49    pub attributes: Vec<String>,
50}
51
52/// Gaia Field definition representing a data member of a class.
53#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
54pub struct GaiaField {
55    /// The name of the field.
56    pub name: String,
57    /// The type of the field.
58    pub ty: GaiaType,
59    /// Whether this field is static (class-level) or instance-level.
60    pub is_static: bool,
61    /// The visibility level of this field.
62    pub visibility: Visibility,
63}
64
65/// Visibility level for type members.
66#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
67pub enum Visibility {
68    /// Accessible from any code that can see the declaring type.
69    Public,
70    /// Accessible only within the declaring type.
71    Private,
72    /// Accessible within the declaring type and derived types.
73    Protected,
74    /// Accessible within the same assembly or module.
75    Internal,
76}
77
78/// Gaia Function definition representing a callable unit of code.
79#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
80pub struct GaiaFunction {
81    /// The name of the function.
82    pub name: String,
83    /// The function signature including parameter types and return type.
84    pub signature: GaiaSignature,
85    /// Basic blocks that make up the function body.
86    pub blocks: Vec<GaiaBlock>,
87    /// Whether this is an external function (defined in another module/library).
88    pub is_external: bool,
89}
90
91/// Gaia Basic Block representing a sequence of instructions with a single entry and exit point.
92#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
93pub struct GaiaBlock {
94    /// The label identifying this block.
95    pub label: String,
96    /// The sequence of instructions in this block.
97    pub instructions: Vec<GaiaInstruction>,
98    /// The terminator instruction that ends this block.
99    pub terminator: GaiaTerminator,
100}
101
102/// Terminator instructions (control flow).
103#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
104pub enum GaiaTerminator {
105    /// Unconditional jump to the specified label.
106    Jump(String),
107    /// Conditional branch based on a boolean value.
108    Branch {
109        /// The label to jump to if the condition is true.
110        true_label: String,
111        /// The label to jump to if the condition is false.
112        false_label: String,
113    },
114    /// Function return.
115    Return,
116    /// Call a function and then jump to another block.
117    Call {
118        /// The name of the function to call.
119        callee: String,
120        /// The number of arguments passed to the function.
121        args_count: usize,
122        /// The label of the block to continue after the call.
123        next_block: String,
124    },
125    /// Halt or exit the program.
126    Halt,
127}
128
129/// Gaia Struct definition representing a value type with named fields.
130#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
131pub struct GaiaStruct {
132    /// The name of the struct type.
133    pub name: String,
134    /// The fields of the struct as (name, type) pairs.
135    pub fields: Vec<(String, GaiaType)>,
136}
137
138/// Gaia Global Variable representing module-level data storage.
139#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
140pub struct GaiaGlobal {
141    /// The name of the global variable.
142    pub name: String,
143    /// The type of the global variable.
144    pub ty: GaiaType,
145    /// The initial value of the global variable, if any.
146    pub initial_value: Option<GaiaConstant>,
147}
148
149/// Gaia Constant representing a compile-time constant value.
150#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
151pub enum GaiaConstant {
152    /// Boolean constant value.
153    Bool(bool),
154    /// 8-bit signed integer constant.
155    I8(i8),
156    /// 8-bit unsigned integer constant.
157    U8(u8),
158    /// 16-bit signed integer constant.
159    I16(i16),
160    /// 16-bit unsigned integer constant.
161    U16(u16),
162    /// 32-bit signed integer constant.
163    I32(i32),
164    /// 32-bit unsigned integer constant.
165    U32(u32),
166    /// 64-bit signed integer constant.
167    I64(i64),
168    /// 64-bit unsigned integer constant.
169    U64(u64),
170    /// 32-bit floating point constant.
171    F32(f32),
172    /// 64-bit floating point constant.
173    F64(f64),
174    /// String constant value.
175    String(String),
176    /// Raw binary data used for weights, constant arrays, etc.
177    Blob(Vec<u8>),
178    /// Constant array of values.
179    Array(Vec<GaiaConstant>),
180    /// Null value or null reference.
181    Null,
182}