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 /// The base type for the pointer arithmetic.
29 base_type: GaiaType,
30 /// The indices to navigate through the type hierarchy.
31 indices: Vec<usize>,
32 },
33
34 // --- Arithmetic Operations (operands popped from stack) ---
35 /// Add two values of the specified type.
36 Add(GaiaType),
37 /// Subtract two values of the specified type.
38 Sub(GaiaType),
39 /// Multiply two values of the specified type.
40 Mul(GaiaType),
41 /// Divide two values of the specified type.
42 Div(GaiaType),
43 /// Remainder of two values of the specified type.
44 Rem(GaiaType),
45 /// Bitwise AND of two values of the specified type.
46 And(GaiaType),
47 /// Bitwise OR of two values of the specified type.
48 Or(GaiaType),
49 /// Bitwise XOR of two values of the specified type.
50 Xor(GaiaType),
51 /// Left shift a value of the specified type.
52 Shl(GaiaType),
53 /// Right shift a value of the specified type.
54 Shr(GaiaType),
55 /// Negate a value of the specified type.
56 Neg(GaiaType),
57 /// Bitwise NOT of a value of the specified type.
58 Not(GaiaType),
59
60 // --- Comparison Operations ---
61 /// Compare two values using the specified condition.
62 Cmp(CmpCondition, GaiaType),
63
64 // --- Type Conversions ---
65 /// Cast a value from one type to another with the specified conversion kind.
66 Cast {
67 /// The source type.
68 from: GaiaType,
69 /// The target type.
70 to: GaiaType,
71 /// The kind of cast operation to perform.
72 kind: CastKind,
73 },
74
75 // --- Stack Management ---
76 /// Push a constant value onto the stack.
77 PushConstant(crate::program::GaiaConstant),
78 /// Pop a value from the stack.
79 Pop,
80 /// Duplicate the top value on the stack.
81 Dup,
82
83 // --- Local Variables and Parameters (Tier 0 version) ---
84 /// Load local variable
85 LoadLocal(u32, GaiaType),
86 /// Store local variable
87 StoreLocal(u32, GaiaType),
88 /// Load parameter
89 LoadArg(u32, GaiaType),
90 /// Store parameter
91 StoreArg(u32, GaiaType),
92
93 // --- Control Flow ---
94 /// Return
95 Ret,
96 /// Unconditional branch
97 Br(String),
98 /// Branch if true
99 BrTrue(String),
100 /// Branch if false
101 BrFalse(String),
102 /// Label
103 Label(String),
104 /// Call function (function name, parameter count)
105 Call(String, usize),
106 /// Indirect call (parameter count). Stack: [..., func_ptr, arg1, arg2, ...]
107 CallIndirect(usize),
108
109 // --- Object and Array Operations ---
110 /// Create new object (type name)
111 New(String),
112 /// Create new array (element type, whether length is on stack)
113 NewArray(GaiaType, bool),
114 /// Load field (object type, field name)
115 LoadField(String, String),
116 /// Store field (object type, field name)
117 StoreField(String, String),
118 /// Load array element
119 LoadElement(GaiaType),
120 /// Store array element
121 StoreElement(GaiaType),
122 /// Get array length
123 ArrayLength,
124 /// Push element to array (array, value)
125 ArrayPush,
126
127 // --- Exception Handling ---
128 /// Throw exception
129 Throw,
130
131 // --- WASM GC Extension Instructions ---
132 /// Create new GC struct (type name)
133 StructNew(String),
134 /// Get GC struct field (type name, field index)
135 StructGet {
136 /// The name of the struct type.
137 struct_name: String,
138 /// The index of the field to get.
139 field_index: u32,
140 /// Whether the field value is signed.
141 is_signed: bool,
142 },
143 /// Set GC struct field (type name, field index)
144 StructSet {
145 /// The name of the struct type.
146 struct_name: String,
147 /// The index of the field to set.
148 field_index: u32,
149 },
150 /// Create new GC array (type name)
151 ArrayNew(String),
152 /// Get GC array element (type name)
153 ArrayGet {
154 /// The name of the array type.
155 array_name: String,
156 /// Whether the element value is signed.
157 is_signed: bool,
158 },
159 /// Set GC array element (type name)
160 ArraySet(String),
161}
162
163/// Comparison condition for compare instructions.
164#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
165pub enum CmpCondition {
166 /// Equal comparison.
167 Eq,
168 /// Not equal comparison.
169 Ne,
170 /// Less than comparison.
171 Lt,
172 /// Less than or equal comparison.
173 Le,
174 /// Greater than comparison.
175 Gt,
176 /// Greater than or equal comparison.
177 Ge,
178}
179
180/// Cast kind for type conversion instructions.
181#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
182pub enum CastKind {
183 /// Bitwise cast between types of the same size.
184 Bitcast,
185 /// Truncate a value to a smaller type.
186 Trunc,
187 /// Zero-extend a value to a larger type.
188 Zext,
189 /// Sign-extend a value to a larger type.
190 Sext,
191 /// Convert floating point to unsigned integer.
192 FpToUi,
193 /// Convert floating point to signed integer.
194 FpToSi,
195 /// Convert unsigned integer to floating point.
196 UiToFp,
197 /// Convert signed integer to floating point.
198 SiToFp,
199}
200
201/// Tier 1: Managed runtime instructions (JVM/CLR/Lua-like)
202#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
203pub enum ManagedInstruction {
204 /// Call an instance method on an object.
205 CallMethod {
206 /// The target type containing the method.
207 target: String,
208 /// The name of the method to call.
209 method: String,
210 /// The signature of the method.
211 signature: crate::types::GaiaSignature,
212 /// Whether this is a virtual method call.
213 is_virtual: bool,
214 /// The call site ID for inline caching.
215 call_site_id: Option<u32>,
216 },
217 /// Call a static method.
218 CallStatic {
219 /// The target type containing the static method.
220 target: String,
221 /// The name of the static method to call.
222 method: String,
223 /// The signature of the method.
224 signature: crate::types::GaiaSignature,
225 },
226 /// Box a value type into a reference type.
227 Box(GaiaType),
228 /// Unbox a reference type back to a value type.
229 Unbox(GaiaType),
230 /// Runtime type checking (is instance of).
231 InstanceOf(GaiaType),
232 /// Type casting with runtime check.
233 CheckCast(GaiaType),
234 /// Initialize an object with the given parameter count.
235 Initiate(usize),
236 /// Finalize an object (call destructor).
237 Finalize,
238}
239
240/// Tier 2: Domain-specific instructions (Neural network/Tensor/Parallel computing)
241#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
242pub enum DomainInstruction {
243 /// Neural network operator node.
244 Neural(NeuralNode),
245
246 // --- Basic Tensor Operations ---
247 /// Matrix multiplication operation.
248 MatMul {
249 /// The shape of matrix A.
250 a_shape: Vec<usize>,
251 /// The shape of matrix B.
252 b_shape: Vec<usize>,
253 /// Whether to transpose matrix A before multiplication.
254 transpose_a: bool,
255 /// Whether to transpose matrix B before multiplication.
256 transpose_b: bool,
257 },
258 /// 2D Convolution operation.
259 Conv2D {
260 /// The stride values [height, width].
261 stride: [usize; 2],
262 /// The padding values [height, width].
263 padding: [usize; 2],
264 /// The dilation values [height, width].
265 dilation: [usize; 2],
266 /// The number of groups for grouped convolution.
267 groups: usize,
268 },
269 /// Element-wise operation on tensors.
270 ElementWise(GaiaType, String),
271
272 // --- Parallel Computing ---
273 /// Get the thread ID in the specified dimension.
274 GetThreadId(usize),
275 /// Get the group size in the specified dimension.
276 GetGroupSize(usize),
277 /// Barrier synchronization for all threads in a group.
278 Barrier,
279}