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 程序模块
8#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
9pub struct GaiaModule {
10    pub name: String,
11    pub functions: Vec<GaiaFunction>,
12    pub structs: Vec<GaiaStruct>,
13    pub classes: Vec<GaiaClass>,
14    pub constants: Vec<(String, GaiaConstant)>,
15    pub globals: Vec<GaiaGlobal>,
16    pub imports: Vec<GaiaImport>,
17}
18
19/// 外部导入项
20#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
21pub struct GaiaImport {
22    pub library: String,
23    pub symbol: String,
24}
25
26/// Gaia 类 (用于托管运行时)
27#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
28pub struct GaiaClass {
29    pub name: String,
30    pub parent: Option<String>,
31    pub interfaces: Vec<String>,
32    pub fields: Vec<GaiaField>,
33    pub methods: Vec<GaiaFunction>,
34    pub attributes: Vec<String>,
35}
36
37/// Gaia 字段
38#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
39pub struct GaiaField {
40    pub name: String,
41    pub ty: GaiaType,
42    pub is_static: bool,
43    pub visibility: Visibility,
44}
45
46#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
47pub enum Visibility {
48    Public,
49    Private,
50    Protected,
51    Internal,
52}
53
54/// Gaia 函数
55#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
56pub struct GaiaFunction {
57    pub name: String,
58    pub signature: GaiaSignature,
59    pub blocks: Vec<GaiaBlock>,
60    pub is_external: bool,
61}
62
63/// Gaia 基本块
64#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
65pub struct GaiaBlock {
66    pub label: String,
67    pub instructions: Vec<GaiaInstruction>,
68    pub terminator: GaiaTerminator,
69}
70
71/// 终结指令 (控制流)
72#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
73pub enum GaiaTerminator {
74    /// 跳转
75    Jump(String),
76    /// 条件跳转 (真标签, 假标签)
77    Branch { true_label: String, false_label: String },
78    /// 函数返回
79    Return,
80    /// 调用并跳转 (函数名, 参数, 返回后跳转的标签)
81    Call { callee: String, args_count: usize, next_block: String },
82    /// 停止/退出
83    Halt,
84}
85
86/// Gaia 结构体
87#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
88pub struct GaiaStruct {
89    pub name: String,
90    pub fields: Vec<(String, GaiaType)>,
91}
92
93/// Gaia 全局变量
94#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
95pub struct GaiaGlobal {
96    pub name: String,
97    pub ty: GaiaType,
98    pub initial_value: Option<GaiaConstant>,
99}
100
101/// Gaia 常量
102#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
103pub enum GaiaConstant {
104    Bool(bool),
105    I8(i8),
106    U8(u8),
107    I16(i16),
108    U16(u16),
109    I32(i32),
110    U32(u32),
111    I64(i64),
112    U64(u64),
113    F32(f32),
114    F64(f64),
115    String(String),
116    /// 原始二进制数据 (用于权重、常量数组等)
117    Blob(Vec<u8>),
118    /// 常量数组
119    Array(Vec<GaiaConstant>),
120    /// 空值/空引用
121    Null,
122}