forked from teamnsrg/erays
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instructions.py
138 lines (95 loc) · 3.99 KB
/
instructions.py
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
from copy import deepcopy
INTERNAL_CALL_OPCODE = "INTCALL"
INTERNAL_RETURN_OPCODE = "INTRETURN"
SWAP_REGISTER = "$t"
STACK_REGISTER = "$s"
def to_stack_registers(items):
return [STACK_REGISTER + str(i) for i in items]
class Instruction:
def __init__(self, opcode, reads, writes, address):
self.opcode = opcode
# must deep copy
self.reads = deepcopy(reads)
self.writes = deepcopy(writes)
self.address = address
def rename_read_register(self, old_name, new_name):
for index, register in enumerate(self.reads):
if register == old_name:
self.reads[index] = new_name
def rename_write_register(self, old_name, new_name):
for index, register in enumerate(self.writes):
if register == old_name:
self.writes[index] = new_name
def read_to_string(self, index):
return str(self.reads[index])
def reads_from(self, register):
return register in self.reads
def writes_to(self, register):
return register in self.writes
def reads_to_string(self):
return ", ".join([str(i) for i in self.reads])
def writes_to_string(self):
return ", ".join([str(i) for i in self.writes])
def get_read_registers(self):
return set([i for i in self.reads if isinstance(i, str)])
def get_write_registers(self):
return set(self.writes)
def set_constant(self, tar_register, constant):
for index, register in enumerate(self.reads):
if register == tar_register:
self.reads[index] = constant
def is_constant_move(self):
return self.opcode == "MOVE" and not isinstance(self.reads[0], str)
def is_register_move(self):
return self.opcode == "MOVE" and isinstance(self.reads[0], str)
def get_constants(self):
constants = list()
for register in self.reads:
if isinstance(register, str):
return None
constants.append(register)
return constants
def __str__(self):
reads = self.reads_to_string()
writes = self.writes_to_string()
if reads == "":
return self.opcode + "\t" + writes
if writes == "":
return self.opcode + "\t" + reads
return self.opcode + "\t" + writes + ", " + reads
class MoveInstruction(Instruction):
def __str__(self):
return "MOVE\t%s, %s" % (self.writes[0], self.reads_to_string())
class MonoOpInstruction(Instruction):
def __str__(self):
return "%s\t%s, %s" % (self.opcode, self.writes[0], self.reads_to_string())
class BinOpInstruction(Instruction):
def __str__(self):
return "%s\t%s, %s" % (self.opcode, self.writes[0], self.reads_to_string())
class MloadInstruction(Instruction):
def __str__(self):
return "MLOAD\t%s, [%s]" % (self.writes[0], self.reads_to_string())
class MstoreInstruction(Instruction):
def __str__(self):
return "MSTORE\t%s, [%s]" % (self.read_to_string(1), self.read_to_string(0))
class SloadInstruction(Instruction):
def __str__(self):
return "SLOAD\t%s, [%s]" % (self.writes[0], self.reads_to_string())
class SstoreInstruction(Instruction):
def __str__(self):
return "SSTORE\t%s, [%s]" % (self.read_to_string(1), self.read_to_string(0))
class CallLoadInstruction(Instruction):
def __str__(self):
return "CLOAD\t%s, [%s]" % (self.writes[0], self.reads_to_string())
class IntCallInstruction(Instruction):
def __str__(self):
return "%s %s\t%s" % (self.opcode, self.writes_to_string(), self.reads_to_string())
class NopInstruction(Instruction):
def __init__(self, address):
Instruction.__init__(self, "NOP", [], [], address)
# class SHA3Operation(Instruction):
# def __init__(self, reads, writes, address):
# Instruction.__init__(self, "SHA3R", reads, writes, address)
# class AssertOperation(Instruction):
# def __init__(self, reads, writes, address):
# Instruction.__init__(self, "ASSERT", reads, writes, address)