gaia_assembler/instruction/mod.rs
1use crate::types::GaiaType;
2use gaia_types::neural::NeuralNode;
3use serde::{Deserialize, Serialize};
4
5/// Gaia Instruction System (Layered Architecture)
6#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
7pub enum GaiaInstruction {
8 /// Core low-level instructions (Tier 0)
9 Core(CoreInstruction),
10 /// Managed runtime instructions (Tier 1)
11 Managed(ManagedInstruction),
12 /// Domain-specific instructions (Tier 2)
13 Domain(DomainInstruction),
14}
15
16/// Tier 0: Core low-level instructions (LLVM/Assembly-like)
17#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
18pub enum CoreInstruction {
19 // --- Memory Operations ---
20 /// Allocate space on the stack (type, count)
21 Alloca(GaiaType, usize),
22 /// Load from memory (target register type, pointer)
23 Load(GaiaType),
24 /// Store to memory (value type)
25 Store(GaiaType),
26 /// Get element pointer (calculate offset)
27 Gep {
28 base_type: GaiaType,
29 indices: Vec<usize>,
30 },
31
32 // --- Arithmetic Operations (operands popped from stack) ---
33 Add(GaiaType),
34 Sub(GaiaType),
35 Mul(GaiaType),
36 Div(GaiaType),
37 Rem(GaiaType),
38 And(GaiaType),
39 Or(GaiaType),
40 Xor(GaiaType),
41 Shl(GaiaType),
42 Shr(GaiaType),
43 Neg(GaiaType),
44 Not(GaiaType),
45
46 // --- Comparison Operations ---
47 Cmp(CmpCondition, GaiaType),
48
49 // --- Type Conversions ---
50 Cast {
51 from: GaiaType,
52 to: GaiaType,
53 kind: CastKind,
54 },
55
56 // --- Stack Management ---
57 PushConstant(crate::program::GaiaConstant),
58 Pop,
59 Dup,
60
61 // --- Local Variables and Parameters (Tier 0 version) ---
62 /// Load local variable
63 LoadLocal(u32, GaiaType),
64 /// Store local variable
65 StoreLocal(u32, GaiaType),
66 /// Load parameter
67 LoadArg(u32, GaiaType),
68 /// Store parameter
69 StoreArg(u32, GaiaType),
70
71 // --- Control Flow ---
72 /// Return
73 Ret,
74 /// Unconditional branch
75 Br(String),
76 /// Branch if true
77 BrTrue(String),
78 /// Branch if false
79 BrFalse(String),
80 /// Label
81 Label(String),
82 /// Call function (function name, parameter count)
83 Call(String, usize),
84 /// Indirect call (parameter count). Stack: [..., func_ptr, arg1, arg2, ...]
85 CallIndirect(usize),
86
87 // --- Object and Array Operations ---
88 /// Create new object (type name)
89 New(String),
90 /// Create new array (element type, whether length is on stack)
91 NewArray(GaiaType, bool),
92 /// Load field (object type, field name)
93 LoadField(String, String),
94 /// Store field (object type, field name)
95 StoreField(String, String),
96 /// Load array element
97 LoadElement(GaiaType),
98 /// Store array element
99 StoreElement(GaiaType),
100 /// Get array length
101 ArrayLength,
102 /// Push element to array (array, value)
103 ArrayPush,
104
105 // --- WASM GC Extension Instructions ---
106 /// Create new GC struct (type name)
107 StructNew(String),
108 /// Get GC struct field (type name, field index)
109 StructGet {
110 struct_name: String,
111 field_index: u32,
112 is_signed: bool,
113 },
114 /// Set GC struct field (type name, field index)
115 StructSet {
116 struct_name: String,
117 field_index: u32,
118 },
119 /// Create new GC array (type name)
120 ArrayNew(String),
121 /// Get GC array element (type name)
122 ArrayGet {
123 array_name: String,
124 is_signed: bool,
125 },
126 /// Set GC array element (type name)
127 ArraySet(String),
128}
129
130/// Comparison condition
131#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
132pub enum CmpCondition {
133 Eq,
134 Ne,
135 Lt,
136 Le,
137 Gt,
138 Ge,
139}
140
141/// Cast kind
142#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
143pub enum CastKind {
144 Bitcast,
145 Trunc,
146 Zext,
147 Sext,
148 FpToUi,
149 FpToSi,
150 UiToFp,
151 SiToFp,
152}
153
154/// Tier 1: Managed runtime instructions (JVM/CLR/Lua-like)
155#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
156pub enum ManagedInstruction {
157 /// Call method (object type, method name, signature, whether virtual call, IC call site ID)
158 CallMethod {
159 target: String,
160 method: String,
161 signature: crate::types::GaiaSignature,
162 is_virtual: bool,
163 call_site_id: Option<u32>,
164 },
165 /// Call static method
166 CallStatic { target: String, method: String, signature: crate::types::GaiaSignature },
167 /// Boxing
168 Box(GaiaType),
169 /// Unboxing
170 Unbox(GaiaType),
171 /// Runtime type checking
172 InstanceOf(GaiaType),
173 /// Type casting
174 CheckCast(GaiaType),
175 /// Initialize object (parameter count)
176 Initiate(usize),
177 /// Finalize object
178 Finalize,
179}
180
181/// Tier 2: Domain-specific instructions (Neural network/Tensor/Parallel computing)
182#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
183pub enum DomainInstruction {
184 /// Neural network operator
185 Neural(NeuralNode),
186
187 // --- Basic Tensor Operations ---
188 /// Matrix multiplication (A, B, C)
189 MatMul { a_shape: Vec<usize>, b_shape: Vec<usize>, transpose_a: bool, transpose_b: bool },
190 /// 2D Convolution
191 Conv2D { stride: [usize; 2], padding: [usize; 2], dilation: [usize; 2], groups: usize },
192 /// Element-wise operation (type, operator name)
193 ElementWise(GaiaType, String),
194
195 // --- Parallel Computing ---
196 /// Get thread ID (dimension)
197 GetThreadId(usize),
198 /// Get group size (dimension)
199 GetGroupSize(usize),
200 /// Barrier synchronization
201 Barrier,
202}