-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_debug.py
139 lines (106 loc) · 3.79 KB
/
gen_debug.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
import io,sys,re
class extractor:
def __init__(self, pattern, func):
self._pattern = pattern
self._func = func
def try_extract(self, line):
matcher = re.search(self._pattern, line)
if matcher:
return self._func(matcher)
else:
return []
class comp_extractor:
def __init__(self, *args):
self._extractors = args
def extract(self, line):
for e in self._extractors:
res = e.try_extract(line)
if res != []:
return res
return []
def get_funcs(path_in):
res = []
with open(path_in) as f:
in_text = False
for line in f:
if line == ".text\n":
in_text = True
if line == ".data\n":
in_text = False
if in_text:
matcher = re.match(r"(\w+):", line)
if matcher:
res.append(matcher.group(1))
return res
def ev_ext(matcher):
return [matcher.group(1)]
def make_ext(matcher):
tpl = list(map(str.strip, matcher.group(1).split(",")))
return ["ev_" + tpl[0]]
def get_events(path_in):
names = ["msg", "yesno_event", "special_event", "1op_std_event", "std_event", "non-std_event", "pseudo"]
ext = comp_extractor(extractor(r"(ev_\w+):", ev_ext), extractor(r"make_(?:" + "|".join(names) + ") ([^\n]+)", make_ext))
res = []
with open(path_in) as file:
in_data = False
for line in file:
res.extend(ext.extract(line))
return res
def make_op_ext(matcher):
tpl = list(map(str.strip, matcher.group(1).split(",")))
return ["ev_" + tpl[0] + "_op_" + tpl[1]]
def yesno_ext(matcher):
tpl = list(map(str.strip, matcher.group(1).split(",")))
return ["ev_" + tpl[0] + "_op_yes", "ev_" + tpl[0] + "_op_no"]
def op_ext(matcher):
tpl = list(map(str.strip, matcher.group(1).split(",")))
return ["ev_" + tpl[0] + "_op_1"]
def get_ops(path_in):
ext = comp_extractor(
extractor(r"make_op ([^\n]+)", make_op_ext),
extractor(r"make_yesno_event ([^\n]+)", yesno_ext),
extractor(r"make_1op_std_event ([^\n]+)", op_ext)
)
res = []
with open(path_in) as file:
in_data = False
for line in file:
res.extend(ext.extract(line))
return res
def name_lbl(func_name):
return "n_" + func_name
def write_table_entry(file, name):
file.writelines([name_lbl(name) + ": .string \"" + name + "\"\n"])
def table_line(func_name, length_name=0):
res = "\t.long "
name = name_lbl(func_name)
name += (" " * (max(length_name - len(name), 0) + 1))
res += name
res += "," + func_name
res += "\n"
return res
def longest(mapper, strings):
max_l = 0
for s in strings:
tmp = mapper(s)
max_l = max(max_l, len(tmp))
return max_l
def write_table(file, name, elements):
table_name = name + "_table"
file.writelines([table_name, ":\n"])
m_name_l = longest(name_lbl, elements)
for e in elements:
file.writelines([table_line(e, m_name_l)])
file.writelines([table_name, "_size: .long (((. - ", table_name, ") / 4) / 2)\n\n"])
for e in elements:
write_table_entry(file, e)
file.writelines(["\n"])
if __name__ == "__main__":
path_in = "ADTF_Revive.s"
path_ev_op = "ADTF_Ev_Op.s"
path_out = "ADTF_Debug_gen.s"
with open(path_out, mode='w') as file:
file.writelines([".file \"ADTF_Debug_gen.s\"", "\n", ".data", "\n"])
write_table(file, "function", get_funcs(path_in))
write_table(file, "event", get_events(path_ev_op))
write_table(file, "option", get_ops(path_ev_op))