-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathgen_vmi.py
161 lines (122 loc) · 3.67 KB
/
gen_vmi.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
import re, os
import sys
from config import *
# super(self).init__() ?
top_class_body = """
class %s(VM_Inst_With_Suff):
def __init__(self):
self.mnem = "%s"
VM_Inst_With_Suff.__init__(self)
"""
small_class_body = """
class VM_%s(%s):
def __init__(self):
self.fn = "%s.txt"
self.size = %d
%s.__init__(self)
"""
disasm = """
def disasm(self, param):
d = VM_Inst_With_Suff.disasm(self, param)
%s
return d
"""
def gen_bwd(mnem, top_class_name, fn_list, custom_disasm):
top_class = top_class_body%(top_class_name, mnem)
if custom_disasm:
dis_proc = disasm%custom_disasm
top_class += "\n" + dis_proc
if len(fn_list)==3:
sizes = [8,16,32]
elif len(fn_list)==2:
sizes = [16,32]
else:
assert(False)
fn_list = zip(fn_list, sizes)
out = top_class
for fn,size in fn_list:
small_class = small_class_body%(fn, top_class_name, fn, size, top_class_name)
out += small_class
return out
def add_generated(new, d):
for h in new:
d[h] = 1
return d
def extract_small_names(s):
l = re.findall(r"class (VM_.*?)\(", s)
return l
if __name__=="__main__":
# not;VM_Not;001,002,003;d+=foo;
if len(sys.argv)<2:
print sys.argv[0], "<info file> [vm_instructions.py]"
sys.exit(1)
src_fn = None
if len(sys.argv)==3:
src_fn = sys.argv[2]
f = open(sys.argv[1])
l = f.readlines()
f.close()
l = map(lambda x: x.strip(), l)
all_handlers = os.listdir("clean_handlers")
generated_handlers = dict()
pre = "# AUTOGENERATED BY gen_vmi.py\n# DO NOT EDIT!\n"
pre += "from vmi_top_common import *"
print pre
prefix = "VM_"
classes = []
for line in l:
line = re.sub(r"#.*", "", line)
if not line:
continue
mnem, class_name, fn_list, custom_disasm = line.split(";")
clean = re.sub(r"\s+", "", custom_disasm)
if clean == "":
custom_disasm = None
fn_list = fn_list.split(",")
fn_count = len(fn_list)
if fn_count==1:
n = int(fn_list[0], 16)
fn_list = [n, n+1, n+2]
fn_list = map(lambda n: hex(n).replace("0x",""), fn_list)
fn_list = map(lambda s: "0"*(3-len(s))+s, fn_list)
generated_handlers = add_generated(fn_list, generated_handlers)
if not class_name:
class_name = prefix + mnem.title()
else:
class_name = prefix + class_name.title().replace(" ", "_")
o = gen_bwd(mnem, class_name, fn_list, custom_disasm)
print o
small = extract_small_names(o)
classes += small
#print "# not handled:"
not_handled = []
for h_fn in all_handlers:
h_id = h_fn.replace(".txt", "")
try:
generated_handlers[h_id]
continue
except:
pass
#print "# ", h_fn
not_handled.append(h_id)
if src_fn != None:
classes.sort()
f = open(src_fn, "r")
src = f.read()
f.close()
#ocaml..
def seek(cn, callback):
cn = "class "+cn
res = src.find(cn)>=0
callback(res, cn, src_fn)
def remove_f(found, cn, src_fn):
if found:
print "# remove %s from %s!"%(cn, src_fn)
def add_f(found, cn, src_fn):
if not found:
print "# add %s to %s!"%(cn, src_fn)
for cn in classes:
seek(cn, remove_f)
not_handled = map(lambda x: prefix+x, not_handled)
for cn in not_handled:
seek(cn, add_f)