-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopcodes.py
More file actions
executable file
·120 lines (94 loc) · 1.98 KB
/
opcodes.py
File metadata and controls
executable file
·120 lines (94 loc) · 1.98 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
"""
OpCodes for Eva VM
"""
# Stop the program
OP_HALT = 0x00
# Push a const onto the stack
OP_CONST = 0x01
# Math instructions
OP_ADD = 0x02
OP_SUB = 0x03
OP_MUL = 0x04
OP_DIV = 0x05
# Comparison
OP_COMPARE = 0x06
# Control flow: jump if the value on the stack is false
OP_JMP_IF_FALSE = 0x07
# Unconditional jump
OP_JMP = 0x08
# Get global variable
OP_GET_GLOBAL = 0x09
# Set a global variable
OP_SET_GLOBAL = 0x0A
# Pop a value from the stack
OP_POP = 0x0B
# Get local variable
OP_GET_LOCAL = 0x0C
# Set local variable
OP_SET_LOCAL = 0x0D
# Exit scope
OP_SCOPE_EXIT = 0x0E
# Call a function
OP_CALL = 0x0F
# Return from a function
OP_RETURN = 0x10
# Get cell
OP_GET_CELL = 0x11
# Set cell
OP_SET_CELL = 0x12
# Load cell onto the stack
OP_LOAD_CELL = 0x13
# Make a function
OP_MAKE_FUNCTION = 0x14
# Create instance
OP_NEW = 0x15
# Property access
OP_GET_PROP = 0x16
# Property set
OP_SET_PROP = 0x17
# Opcode names mapping
OPCODE_NAMES = {
OP_HALT: 'HALT',
OP_CONST: 'CONST',
OP_ADD: 'ADD',
OP_SUB: 'SUB',
OP_MUL: 'MUL',
OP_DIV: 'DIV',
OP_COMPARE: 'COMPARE',
OP_JMP_IF_FALSE: 'JMP_IF_FALSE',
OP_JMP: 'JMP',
OP_GET_GLOBAL: 'GET_GLOBAL',
OP_SET_GLOBAL: 'SET_GLOBAL',
OP_POP: 'POP',
OP_GET_LOCAL: 'GET_LOCAL',
OP_SET_LOCAL: 'SET_LOCAL',
OP_SCOPE_EXIT: 'SCOPE_EXIT',
OP_CALL: 'CALL',
OP_RETURN: 'RETURN',
OP_GET_CELL: 'GET_CELL',
OP_SET_CELL: 'SET_CELL',
OP_LOAD_CELL: 'LOAD_CELL',
OP_MAKE_FUNCTION: 'MAKE_FUNCTION',
OP_NEW: 'NEW',
OP_GET_PROP: 'GET_PROP',
OP_SET_PROP: 'SET_PROP',
}
# Compare operations
CMP_LT = 0 # <
CMP_GT = 1 # >
CMP_EQ = 2 # ==
CMP_GE = 3 # >=
CMP_LE = 4 # <=
CMP_NE = 5 # !=
COMPARE_OPS = {
'<': CMP_LT,
'>': CMP_GT,
'==': CMP_EQ,
'>=': CMP_GE,
'<=': CMP_LE,
'!=': CMP_NE
}
COMPARE_NAMES = ['<', '>', '==', '>=', '<=', '!=']
def opcode_to_string(opcode):
"""Convert opcode to string name"""
return OPCODE_NAMES.get(opcode, f'UNKNOWN_{hex(opcode)}')