-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopcode.rs
More file actions
140 lines (116 loc) · 5.06 KB
/
Copy pathopcode.rs
File metadata and controls
140 lines (116 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use serde::{Deserialize, Serialize};
/// Operations supported by the TechScript 2.0 Virtual Machine.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Opcode {
// ── Stack Operations ────────────────────────────────────────────────────
Pop,
Dup,
Swap,
// ── Value Loading and Storing ───────────────────────────────────────────
LoadConst,
LoadLocal,
StoreLocal,
LoadGlobal,
StoreGlobal,
// ── Arithmetic Operations ───────────────────────────────────────────────
Add,
Sub,
Mul,
Div,
IntDiv,
Mod,
Pow,
// ── Unary Operations ────────────────────────────────────────────────────
Neg,
Not,
Await,
// ── Logical Operations ──────────────────────────────────────────────────
And,
Or,
// ── Comparison Operations ───────────────────────────────────────────────
Equal,
StrictEqual,
NotEqual,
Less,
LessEqual,
Greater,
GreaterEqual,
// ── Control Flow ────────────────────────────────────────────────────────
Jump,
JumpIfTrue,
JumpIfFalse,
Call,
Return,
// ── Collections ─────────────────────────────────────────────────────────
MakeList,
MakeMap,
IndexLoad,
IndexStore,
// ── OOP / Structures ────────────────────────────────────────────────────
MakeStruct,
MakeEnum,
MakeModel,
FieldLoad,
FieldStore,
// ── Closures ────────────────────────────────────────────────────────────
Capture,
LoadUpvalue,
StoreUpvalue,
CloseUpvalue,
// ── Exceptions ──────────────────────────────────────────────────────────
Throw,
Try,
EndTry,
// ── Miscellaneous ───────────────────────────────────────────────────────
NoOp,
}
impl Opcode {
/// Returns the net stack effect (push - pop) of executing the opcode.
pub fn stack_effect(&self) -> i32 {
match self {
Opcode::Pop => -1,
Opcode::Dup => 1,
Opcode::Swap => 0,
Opcode::LoadConst => 1,
Opcode::LoadLocal => 1,
Opcode::StoreLocal => 0, // Leaves value on stack or pops depending on compiler semantics; here we assume store consumes nothing (or pops; let's treat as popping 1 value, i.e. -1)
Opcode::LoadGlobal => 1,
Opcode::StoreGlobal => -1,
Opcode::Add
| Opcode::Sub
| Opcode::Mul
| Opcode::Div
| Opcode::IntDiv
| Opcode::Mod
| Opcode::Pow => -1, // Pops 2, pushes 1
Opcode::Neg | Opcode::Not | Opcode::Await => 0, // Pops 1, pushes 1
Opcode::And | Opcode::Or => -1,
Opcode::Equal
| Opcode::StrictEqual
| Opcode::NotEqual
| Opcode::Less
| Opcode::LessEqual
| Opcode::Greater
| Opcode::GreaterEqual => -1,
Opcode::Jump => 0,
Opcode::JumpIfTrue | Opcode::JumpIfFalse => -1,
Opcode::Call => 0, // Dynamic, resolved by call arguments count
Opcode::Return => -1,
Opcode::MakeList => 1,
Opcode::MakeMap => 1,
Opcode::IndexLoad => -1, // Pops base & index, pushes element -> -1
Opcode::IndexStore => -3, // Pops base, index & value -> -3
Opcode::MakeStruct | Opcode::MakeEnum | Opcode::MakeModel => 1,
Opcode::FieldLoad => 0, // Pops base, pushes field val -> 0
Opcode::FieldStore => -2, // Pops base & value -> -2
Opcode::Capture => 1,
Opcode::LoadUpvalue => 1,
Opcode::StoreUpvalue => -1,
Opcode::CloseUpvalue => 0,
Opcode::Throw => -1,
Opcode::Try => 0,
Opcode::EndTry => 0,
Opcode::NoOp => 0,
}
}
}