/
listacs.py
executable file
·303 lines (239 loc) · 8.93 KB
/
listacs.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/python
# listacs.py: ZDoom ACS disassembler/decompiler
#
# Copyright (c) 2009 Jared Stafford (jspenguin@gmail.com)
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import sys
import optparse
import acsutil
import doomwad
options = optparse.OptionParser(usage="usage: %prog [-d] [-s] [-v] [-c] [-o file] [-w wad] <file or lump>")
options.add_option("-o", "--output", action="store",
dest="output", metavar="FILE",
default=None, help="write output to FILE")
options.add_option("-d", "--decompile", action="store_true",
dest="decompile", metavar="BOOL",
default=False, help="try to decompile to ACS source")
options.add_option("-g", "--goto", action="store_true",
dest="goto", metavar="BOOL",
default=False, help="use 'goto' statement instead of switch-loop hack")
options.add_option("-s", "--strings", action="store_true",
dest="strings", metavar="BOOL",
default=False, help="print string table")
options.add_option("-v", "--vars", action="store_true",
dest="vars", metavar="BOOL",
default=False, help="print variable declarations when disassembling")
options.add_option("-c", "--comment", action="store_true",
dest="comment", metavar="BOOL",
default=False, help="comment out anything not executable")
options.add_option("-w", "--wad", action="store",
dest="wad", metavar="FILE",
default=None, help="read from a WAD file")
def indexes(lst):
return sorted(b.index for b in lst)
def getstack(v, env):
while v:
yield v.tocode(env)
v = v._next
class GotoParser(acsutil.Parser):
spaces = ' '
def __init__(self, scr, *args):
acsutil.Parser.__init__(self, *args)
self.script = scr
self.stgt = self.read_instructions(scr.ptr)
self.create_block(self.stgt)
self.run()
def goto_code(self, tgt):
return 'goto block%d' % tgt.id
def label_code(self, tgt):
return ' block%s:' % tgt.id
def prefix_code(self):
yield '%s' % (self.script.getlabel())
def suffix_code(self):
yield ''
def gen_code(self):
addrs = self.targets.keys()
sorted(addrs)
self.blocks = blocks = []
for i, a in enumerate(addrs):
tgt = self.targets[a]
if tgt.entries > 1:
tgt.id = len(blocks) + 1
blocks.append(tgt)
yield self.script.getheader()
yield '{'
vars = ['local%d' % v for v in self.locals.vars if v >= self.script.argc]
if vars:
vars.sort()
yield ' int %s;' % ', '.join(vars)
for l in self.prefix_code():
yield l
self.stgt.id = 0
for tgt in blocks:
lbl = self.label_code(tgt)
if lbl:
yield lbl
for l in tgt.block.genlines(self):
yield self.spaces + l
yield ''
for l in self.suffix_code():
yield l
yield '}'
class SwitchParserScript(GotoParser):
spaces = ' '
def goto_code(self, tgt):
return 'goto_block = %d; restart' % tgt.id
def label_code(self, tgt):
if len(self.blocks) != 1:
return ' case %d:' % tgt.id
def restart_code(self):
if len(self.blocks) == 1:
return 'restart'
else:
return 'goto_block = 0; restart'
def prefix_code(self):
if len(self.blocks) == 1:
self.spaces = ' '
else:
yield ' int goto_block;'
yield ' switch (goto_block) {'
def suffix_code(self):
if len(self.blocks) != 1:
yield ' }'
class SwitchParserFunction(GotoParser):
spaces = ' '
def goto_code(self, tgt):
return 'goto_block = %d; continue' % tgt.id
def label_code(self, tgt):
if len(self.blocks) != 1:
return ' case %d:' % tgt.id
def prefix_code(self):
if len(self.blocks) == 1:
self.spaces = ' '
else:
yield ' int goto_block;'
yield ' while (1) {'
yield ' switch (goto_block) {'
def suffix_code(self):
if len(self.blocks) != 1:
yield ' }'
yield ' }'
if not self.script.isvoid:
yield ' return 0;'
def declare_mapvars(acsf, seq):
for var in seq:
val = var.initval
i = var.id
isarray = not isinstance(val, int)
isstring = var.is_string()
if isarray:
if isstring:
yield 'int map%d[%d] = {%s};' % \
(i, len(val), ', '.join(acsf.getstring(v) for v in val))
else:
yield 'int map%d[%d] = {%s};' % \
(i, len(val), ', '.join(str(v) for v in val))
else:
if isstring:
yield 'int map%d = %s; ' % (i, acsf.getstring(val))
else:
yield 'int map%d = %d;' % (i, val)
if isarray:
if not var.isarray:
yield '// Warning: array map%d[] not used as array' % i
else:
if var.isarray:
yield '// Warning: variable map%d used as array' % i
def declare_vars(seq, type):
for var in seq:
arrdec = ''
if var.isarray:
arrdec = '[]'
yield '%s int %d:%s%d%s;' % (type, var.id, type, var.id, arrdec)
def getvars(acsf, type):
vars = acsf.vars[type].vars
varids = vars.keys()
for id in sorted(varids):
yield vars[id]
def main():
opts, args = options.parse_args()
if not args:
options.print_help()
return
if opts.wad:
f = open(opts.wad, 'rb')
wad = doomwad.WadFile(f)
f.close()
dat = wad[args[0]].data
else:
dat = open(args[0], 'rb').read()
acsf = acsutil.Behavior(dat)
comment = ''
if opts.comment:
comment = '// '
if opts.output:
output = open(opts.output, 'w')
else:
output = sys.stdout
if opts.strings:
print('%sStrings:' % comment, file=output)
for i, s in enumerate(acsf.strings):
print('%s %3d: %r' % (comment, i, s), file=output)
print('', file=output)
if opts.decompile:
parsers = []
for m in acsf.markers:
if m.executable:
if opts.goto:
p = GotoParser(m, acsf)
elif isinstance(m, acsutil.Script):
p = SwitchParserScript(m, acsf)
else:
p = SwitchParserFunction(m, acsf)
parsers.append(p)
print('#include "zcommon.acs"', file=output)
print('', file=output)
for l in declare_vars(getvars(acsf, 'global'), 'global'):
print(l, file=output)
for l in declare_vars(getvars(acsf, 'world'), 'world'):
print(l, file=output)
for l in declare_mapvars(acsf, getvars(acsf, 'map')):
print(l, file=output)
print('', file=output)
for p in parsers:
for l in p.gen_code():
print(l, file=output)
else:
if opts.vars:
for l in declare_mapvars(acsf, getvars(acsf, 'map')):
print(l, file=output)
print('', file=output)
for m in acsf.markers:
for lin in m.disassemble():
print('%s%s' % (comment, lin), file=output)
if sys.hexversion < 0x3070000:
print('This script requires Python 3.7 or newer')
exit(1)
main()