-
Notifications
You must be signed in to change notification settings - Fork 29
/
Instruction.py
138 lines (99 loc) · 4.27 KB
/
Instruction.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
"""
This file is part of SEA.
reserbot is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
reserbot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SEA. If not, see <http://www.gnu.org/licenses/>.
Copyright 2013 by neuromancer
"""
from Operand import *
class Instruction:
"""An abstract instruction class"""
def __init__(self, pins, mem_access, mem_regs = True):
self.address = pins.address
self.instruction = pins.instruction
#print pins.instruction, mem_access
self.operands = []
# for memory instructions
self.mem_reg = None
# for call instructions
self.called_function = None
aopers = pins.augmented_operands
for (i,x) in enumerate(aopers):
if x == ",":
self.operands.append(Operand(aopers[i-1], aopers[i-2]))
self.operands.append(Operand(aopers[-1], aopers[-2]))
self.read_operands = []
self.write_operands = []
# ldm: op_2 = [op_0]
if (pins.instruction == "ldm"):
self.write_operands = [self.operands[2]]
self.mem_reg = self.operands[0]
if (mem_access <> None):
#if (mem_regs):
# self.read_operands = [self.operands[0]]
mem_source = mem_access["source"]
mem_offset = mem_access["offset"]
for i in range(self.operands[2].size):
name = mem_source+"@"+str(mem_offset+i)
self.read_operands.append(Operand(name, "BYTE", mem_source, mem_offset+i))
#self.read_operands.append(self.operands[0])
#else:
# print "#WARNING: No memory access information of ldm in", self.address
# stm: [op_2] = op_0
elif (pins.instruction == "stm"):
self.read_operands.append(self.operands[0])
self.mem_reg = self.operands[2]
if (mem_access <> None):
# if (mem_regs):
# self.write_operands = [self.operands[2]]
mem_source = mem_access["source"]
mem_offset = mem_access["offset"]
for i in range(self.operands[0].size):
name = mem_source+"@"+str(mem_offset+i)
self.write_operands.append(Operand(name, "BYTE", mem_source, mem_offset+i))
#else:
# print "#WARNING: No memory access information of stm in", self.address
elif (pins.instruction == "jcc"):
self.read_operands = filter(lambda o: not o.isEmpty(), self.operands[0:2])
self.write_operands = []
elif (pins.instruction == "call"):
#print self.operands[0].name
if (self.operands[0].name <> "EMPTY"):
self.called_function = self.operands[0].name
else:
self.read_operands = filter(lambda o: not o.isEmpty(), self.operands[0:2])
self.write_operands = filter(lambda o: not o.isEmpty(), self.operands[2:3])
def getOperands(self):
# if (pins.instruction == "ldm"):
# return list(self.read_operands[1:] + self.write_operands)
# if (pins.instruction == "stm"):
# return list(self.read_operands[1:] + self.write_operands)
# else:
return list(self.read_operands + self.write_operands)
def getReadOperands(self):
return list(self.read_operands)
def getWriteOperands(self):
return list(self.write_operands)
def getReadRegOperands(self):
return filter(lambda o: o.isReg(), self.read_operands)
def getWriteRegOperands(self):
return filter(lambda o: o.isReg(), self.write_operands)
def getReadVarOperands(self):
return filter(lambda o: o.isVar(), self.read_operands)
def getWriteVarOperands(self):
return filter(lambda o: o.isVar(), self.write_operands)
def getReadMemOperands(self):
return filter(lambda o: o.isMem(), self.read_operands)
def getWriteMemOperands(self):
return filter(lambda o: o.isMem(), self.write_operands)
def getMemReg(self):
return self.mem_reg
#def getEq(self):
# return None