-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstruction.py
More file actions
108 lines (99 loc) · 3.13 KB
/
Copy pathInstruction.py
File metadata and controls
108 lines (99 loc) · 3.13 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
"""
"""
class Instruction():
"""represent an instruction"""
def __init__(self, index, opcode, instruction_type, op_one = None, op_two = None, op_three = None, next_op = None):
self.index = index
self.type = instruction_type
self.opcode = opcode
self.op_one = op_one
self.op_two = op_two
self.op_three = op_three
def get_str(self, reg_type = "physical"):
""""""
def get_reg_val(op_name, reg_type):
if not op_name:
raise Exception
if isinstance(op_name, dict):
return op_name[reg_type]
if isinstance(op_name, basestring) or isinstance(op_name, int):
return op_name
raise Exception
if self.type == InstructionType.three_op:
return "%(opcode)s %(op_one)s, %(op_two)s => %(op_three)s" % {
"opcode" : self.opcode,
"op_one" : get_reg_val(self.op_one, reg_type),
"op_two" : get_reg_val(self.op_two, reg_type),
"op_three" : get_reg_val(self.op_three, reg_type)}
if self.type == InstructionType.two_op:
return "%(opcode)s %(op_one)s => %(op_three)s" % {
"opcode" : self.opcode,
"op_one" : get_reg_val(self.op_one, reg_type),
"op_three" : get_reg_val(self.op_three, reg_type)}
if self.type == InstructionType.store:
return "%(opcode)s %(op_one)s => %(op_two)s" % {
"opcode" : self.opcode,
"op_one" : get_reg_val(self.op_one, reg_type),
"op_two" : get_reg_val(self.op_two, reg_type)}
if self.type == InstructionType.one_op:
return "%(opcode)s %(op_one)s" % {
"opcode" : self.opcode,
"op_one" : get_reg_val(self.op_one, reg_type)}
if self.type == InstructionType.none_op:
return "%(opcode)s" % {"opcode" : self.opcode}
def get_index(self):
return self.index
def set_op_value(self, op_field, value, reg_field = None):
if not reg_field:
if not isinstance(value, dict):
raise Exception
if op_field == "op_one":
self.op_one = value
return
if op_field == "op_two":
self.op_two =value
return
if op_field == "op_three":
self.op_three = value
return
raise Exception
else:
if not isinstance(value, basestring):
raise Exception
if op_field == "op_one":
self.op_one[reg_field] = value
return
if op_field == "op_two":
self.op_two[reg_field] =value
return
if op_field == "op_three":
self.op_three[reg_field] = value
return
raise Exception
def get_op(self, op_field, reg_field = None):
if not reg_field:
if op_field == "op_one":
return self.op_one
if op_field == "op_two":
return self.op_two
if op_field == "op_three":
return self.op_three
raise Exception
else:
if op_field == "op_one":
return self.op_one[reg_field]
if op_field == "op_two":
return self.op_two[reg_field]
if op_field == "op_three":
return self.op_three[reg_field]
raise Exception
#helper function
#Source: http://stackoverflow.com/questions/36932/how-can-i-represent-an-enum-in-python
class Enum(set):
def __getattr__(self, name):
if name in self:
return name
raise AttributeError
def __setattr__(self, name, value):
raise AttributeError
InstructionType = Enum(["none_op", "one_op", "two_op", "three_op", "store"])