1use gaia_types::{GaiaError, QualifiedName};
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
6pub struct NyarHandler {
7 pub start: u32,
8 pub end: u32,
9 pub target: u32,
10 pub error_type: u16,
11}
12
13#[derive(Debug, Serialize, Deserialize)]
14pub struct NyarChunk {
15 pub code: Vec<u8>,
16 pub locals: u16,
17 pub upvalues: u16,
18 pub max_stack: u16,
19 pub handlers: Vec<NyarHandler>,
20 pub lines: Vec<(u32, u32)>,
21}
22
23impl Clone for NyarChunk {
24 fn clone(&self) -> Self {
25 Self {
26 code: self.code.clone(),
27 locals: self.locals,
28 upvalues: self.upvalues,
29 max_stack: self.max_stack,
30 handlers: self.handlers.clone(),
31 lines: self.lines.clone(),
32 }
33 }
34}
35
36impl Default for NyarChunk {
37 fn default() -> Self {
38 Self {
39 code: Vec::new(),
40 locals: 0,
41 upvalues: 0,
42 max_stack: 0,
43 handlers: Vec::new(),
44 lines: Vec::new(),
45 }
46 }
47}
48
49#[derive(Debug, Clone, Serialize, Deserialize)]
50pub struct NyarModule {
51 pub name: QualifiedName,
52 pub version: u16,
53 pub flags: u32,
54 pub timestamp: u64,
55 pub constants: Vec<NyarConstant>,
56 pub effects: Vec<QualifiedName>,
57 pub chunks: Vec<NyarChunk>,
58 pub classes: Vec<NyarClassInfo>,
59 pub traits: Vec<NyarTraitInfo>,
60 pub impls: Vec<NyarImplInfo>,
61 pub imports: Vec<NyarImportInfo>,
62 pub exports: Vec<NyarExportInfo>,
63}
64
65impl Default for NyarModule {
66 fn default() -> Self {
67 Self {
68 name: QualifiedName::new(vec![], "main".to_string()),
69 version: 1,
70 flags: 0,
71 timestamp: 0,
72 constants: Vec::new(),
73 effects: Vec::new(),
74 chunks: Vec::new(),
75 classes: Vec::new(),
76 traits: Vec::new(),
77 impls: Vec::new(),
78 imports: Vec::new(),
79 exports: Vec::new(),
80 }
81 }
82}
83
84#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
86#[serde(tag = "kind", content = "value")]
87pub enum NyarConstant {
88 None,
90 Int(i64),
92 Float(u64), String(String),
96 QualifiedName(QualifiedName),
98}
99
100impl NyarConstant {
101 pub fn float(f: f64) -> Self {
102 Self::Float(f.to_bits())
103 }
104 pub fn get_float(&self) -> Option<f64> {
105 if let Self::Float(v) = self {
106 Some(f64::from_bits(*v))
107 } else {
108 None
109 }
110 }
111}
112
113#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
115pub struct NyarClassInfo {
116 pub name: QualifiedName,
117 pub fields: Vec<String>,
118}
119
120#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
122pub struct NyarTraitInfo {
123 pub name: QualifiedName,
124 pub methods: Vec<String>,
125}
126
127#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
129pub struct NyarImplInfo {
130 pub class_idx: u16,
131 pub trait_idx: u16,
132 pub methods: Vec<u16>,
133}
134
135#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
137pub struct NyarImportInfo {
138 pub provider: String,
139 pub symbol: QualifiedName,
140}
141
142#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
144pub struct NyarExportInfo {
145 pub symbol: QualifiedName,
146 pub chunk_idx: u16,
147}
148
149#[repr(C)]
151#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
152pub struct NyarUpvalueRef {
153 pub is_local: bool,
155 pub index: u8,
157}
158
159#[repr(u8)]
161#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
162pub enum NyarOpcode {
163 Nop = 0x00,
165 Push = 0x01,
167 Pop = 0x02,
169 Dup = 0x03,
171 Swap = 0x04,
173 LoadLocal = 0x05,
175 StoreLocal = 0x06,
177 LoadGlobal = 0x07,
179 StoreGlobal = 0x08,
181 LoadUpvalue = 0x09,
183 StoreUpvalue = 0x0A,
185 CloseUpvalues = 0x0B,
187 Jump = 0x0C,
189 JumpIfFalse = 0x0D,
191 JumpIfTrue = 0x0E,
193 JumpIfNull = 0x0F,
195 PushNone = 0x10,
197 Return = 0x13,
199 MakeClosure = 0x15,
201
202 Call = 0x20,
204 CallVirtual = 0x21,
206 CallDynamic = 0x22,
208 CallClosure = 0x23,
210 InvokeMethod = 0x24,
212 CallSymbol = 0x25,
214 FFICall = 0xF0,
216 TailCall = 0x14,
218 TailCallClosure = 0x16,
220
221 GetField = 0x30,
223 SetField = 0x31,
225 NewObject = 0x32,
227 NewArray = 0x33,
229 GetElement = 0x34,
231 SetElement = 0x35,
233 MakeTuple = 0x36,
235 HasKey = 0x37,
237 MatchVariant = 0x38,
239 SizeOf = 0x39,
241 NewDynObject = 0x3A,
243 RemoveKey = 0x3B,
245 NewList = 0x3C,
247 PushElementLeft = 0x3D,
249 PopElementLeft = 0x3E,
251 PushElementRight = 0x3F,
253 PopElementRight = 0x40,
255 TypeOf = 0x41,
257 InstanceOf = 0x42,
259 CheckCast = 0x43,
261 Cast = 0x44,
263 Initiate = 0x45,
265 Finalize = 0x46,
267 Perform = 0x50,
269 WithHandler = 0x51,
271 ResumeWith = 0x52,
273 CaptureCont = 0x53,
275 Await = 0x54,
277 BlockOn = 0x55,
279 MatchEffect = 0x56,
281 GetWitnessTable = 0x60,
283 WitnessMethod = 0x61,
285 OpenExistential = 0x62,
287 CloseExistential = 0x63,
289 Quote = 0x70,
291 Splice = 0x71,
293 Eval = 0x72,
295 ExpandMacro = 0x73,
297
298 NewChannel = 0x57,
300 SendChannel = 0x58,
302 RecvChannel = 0x59,
304 SelectChannel = 0x5A,
306 NewQuery = 0x5B,
308 QueryExec = 0x5C,
310 NewClock = 0x5D,
312 ClockWait = 0x5E,
314
315 I32Ext = 0xC1,
317 I64Ext = 0xC2,
319 F32Ext = 0xC3,
321 F64Ext = 0xC4,
323 BigIntExt = 0xC5,
325 StringExt = 0xC6,
327
328 Halt = 0xFF,
330}
331
332#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
334pub enum NyarInstruction {
335 Nop,
337 Push(u16),
339 PushNone,
341 Pop,
343 Dup(u8),
345 Swap(u8),
347 LoadLocal(u8),
349 StoreLocal(u8),
351 LoadGlobal(u16),
353 StoreGlobal(u16),
355 LoadUpvalue(u8),
357 StoreUpvalue(u8),
359 CloseUpvalues,
361 Jump(i16),
363 JumpIfFalse(i16),
365 JumpIfTrue(i16),
367 JumpIfNull(i16),
369 Return,
371 MakeClosure(u16, Vec<NyarUpvalueRef>),
373 TailCall(u16, u8),
375 TailCallClosure(u8),
377 Call(u16, u8),
379 CallVirtual(u16, u8),
381 CallDynamic(u16, u8),
383 CallClosure(u8),
385 InvokeMethod(u16, u8),
387 CallSymbol(u16, u8),
389 GetField(u16),
391 SetField(u16),
393 NewObject(u16),
395 NewArray(u16),
397 GetElement,
399 SetElement,
401 NewDynObject,
403 RemoveKey,
405 Initiate(u8),
407 Finalize,
409 NewList(u16),
411 PushElementLeft,
413 PopElementLeft,
415 PushElementRight,
417 PopElementRight,
419 MakeTuple(u8),
421 HasKey,
423 MatchVariant(u16),
425 SizeOf,
427 TypeOf,
429 InstanceOf(u16),
431 CheckCast(u16),
433 Cast(u16),
435 Perform(u16, u8),
437 WithHandler(u16),
439 ResumeWith,
441 CaptureCont,
443 Await,
445 BlockOn,
447 MatchEffect(u16),
449 GetWitnessTable(u16, u16),
451 WitnessMethod(u16),
453 OpenExistential,
455 CloseExistential,
457 Quote(u32),
459 Splice,
461 Eval(u8),
463 ExpandMacro(u16, u8),
465 FFICall(u16, u8),
467 Halt,
469
470 I32Const(i32),
474 I32Add,
476 I32Sub,
478 I32Mul,
480 I32DivS,
482 I32DivU,
484 I32RemS,
486 I32RemU,
488 I32And,
490 I32Or,
492 I32Xor,
494 I32Shl,
496 I32ShrS,
498 I32ShrU,
500 I32Not,
502 I32Neg,
504 I32Eq,
506 I32Ne,
508 I32LtS,
510 I32LtU,
512 I32LeS,
514 I32LeU,
516 I32GtS,
518 I32GtU,
520 I32GeS,
522 I32GeU,
524 I32ToF32S,
526 I32ToF32U,
528 I32ToF64S,
530 I32ToF64U,
532 I32AddSatS,
534 I32AddSatU,
536 I32SubSatS,
538 I32SubSatU,
540 I32Extend64S,
542 I32Extend64U,
544 I32Trunc64SLow,
546 I32Trunc64S,
548 I32Trunc64U,
550
551 I64Const(i64),
553 I64Add,
555 I64Sub,
557 I64Mul,
559 I64DivS,
561 I64DivU,
563 I64RemS,
565 I64RemU,
567 I64And,
569 I64Or,
571 I64Xor,
573 I64Shl,
575 I64ShrS,
577 I64ShrU,
579 I64Not,
581 I64Neg,
583 I64Eq,
585 I64Ne,
587 I64LtS,
589 I64LtU,
591 I64LeS,
593 I64LeU,
595 I64GtS,
597 I64GtU,
599 I64GeS,
601 I64GeU,
603 I64ToF32S,
605 I64ToF32U,
607 I64ToF64S,
609 I64ToF64U,
611 I64AddSatS,
613 I64AddSatU,
615
616 F32Const(f32),
618 F32Add,
620 F32Sub,
622 F32Mul,
624 F32Div,
626 F32Neg,
628 F32Eq,
630 F32Ne,
632 F32Lt,
634 F32Le,
636 F32Gt,
638 F32Ge,
640 F32ToI32S,
642 F32ToI32U,
644 F32ToI64S,
646 F32ToI64U,
648 F32ToF64,
650
651 F64Const(f64),
653 F64Add,
655 F64Sub,
657 F64Mul,
659 F64Div,
661 F64Neg,
663 F64Eq,
665 F64Ne,
667 F64Lt,
669 F64Le,
671 F64Gt,
673 F64Ge,
675 F64ToI32S,
677 F64ToI32U,
679 F64ToI64S,
681 F64ToI64U,
683 F64ToF32,
685
686 BigIntConst {
688 sign: u8,
690 bytes: Vec<u8>,
692 },
693 BigIntAdd,
695 BigIntSub,
697 BigIntMul,
699 BigIntDiv,
701 BigIntMod,
703 BigIntNeg,
705 BigIntEq,
707 BigIntNe,
709 BigIntLt,
711 BigIntLe,
713 BigIntGt,
715 BigIntGe,
717 BigIntToI64,
719 BigIntFromI64,
721 BigIntToString,
723
724 StringConst(String),
726 StringConcat,
728 StringLenBytes,
730 StringLenChars,
732 StringEq,
734 StringNe,
736 StringLt,
738 StringLe,
740 StringGt,
742 StringGe,
744 StringSubstr,
746
747 NewChannel(u16),
749 SendChannel,
751 RecvChannel,
753 SelectChannel(u8),
755 NewQuery(u16),
757 QueryExec,
759 NewClock,
761 ClockWait,
763}