-
Notifications
You must be signed in to change notification settings - Fork 3
/
convert_parse_tree.py
executable file
·162 lines (134 loc) · 3.41 KB
/
convert_parse_tree.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/python3
from lark import Lark, Transformer
# Change: inline the GOT label conversions
from register_map import mode_map
import copy
class TreeToDict(Transformer):
def register(self, r):
(r,) = r
return r
def hex_val(self, v):
(v,) = v
return int(v, 16)
def immediate(self, d):
(d,) = d
return {'immediate': int(d)}
def opcode(self, o):
(o,) = o
o = o.replace('.', '') # Remove dots in opcodes, e.g. b.eq
return {'opcode': str(o)}
def indirect(self, l):
d = {k: v for d in l for k, v in d.items()}
d['indirect'] = True
return d
def writeback(self, w):
return True
def operand(self, op):
wb = len(op) > 1 # Only way to have more than one child item here is writeback
op = op[0]
op['writeback'] = wb
return op
# return {'operand': op, 'writeback': wb}
def operation(self, c):
return {'operation': {
'opcode': c[0]['opcode'],
'operands': c[1:]
}}
def start(self, d):
if not d:
return d
(d,) = d
return d
def directive(self, d):
(d,) = d
return {'directive': str(d)}
def label_target(self, l):
(l,) = l
return {'label': str(l)}
def mode(self, m):
(m,) = m
return str(m)
# Inlining conversion of preprocessor load
def proc_load(self, l):
mode, label = l
label = label['label']
new_mode = mode_map[mode]
return {
'original_mode': mode,
'label': f'%{new_mode}({label})',
'is_load': True
}
def offset(self, n):
(n,) = n
if 'immediate' in n.keys():
n = n['immediate']
return {'offset': n}
mode = n.get('original_mode')
if mode:
return {
'offset': n['label'],
'original_mode': mode
}
return {'offset': n}
def label(self, l):
(l,) = l
return {'label': l}
def half_reg(self, r):
(r,) = r
return {
'register': str(r),
'type': 'gp',
'half_width': True
}
def full_reg(self, r):
(r,) = r
return {
'register': str(r),
'type': 'gp',
'half_width': False
}
def double64(self, r):
(r,) = r
return {
'register': str(r),
'width': 64
}
def float32(self, r):
(r,) = r
return {
'register': str(r),
'width': 32
}
def float16(self, r):
(r,) = r
return {
'register': str(r),
'width': 16
}
def float_reg(self, r):
(r,) = r
r['type'] = 'fp'
return r
# Can we clean this up?
def shifted_register(self, r):
reg, stype, *by = r
if by:
by = by[0]
if type(by) != dict:
by = int(by)
else:
by = None
rdict = copy.deepcopy(reg)
rdict['register'] = 'shift_temp'
rdict['shift'] = {
'shift_reg': reg,
'shift_type': stype,
'shift_by': by
}
return rdict
def shift_by(self, o):
(o,) = o
return o
def shift_type(self, t):
(t,) = t
return str(t)