-
Notifications
You must be signed in to change notification settings - Fork 14
/
agsoptimize
executable file
·608 lines (534 loc) · 20.3 KB
/
agsoptimize
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
#!/usr/bin/env python2
# the AGS script compiler basically doesn't optimize at all,
# and the code it emits is super-naive and redundant.
# the register bx is most often used only as a temporary
# storage and discarded immediately after doing one basic op.
# therefore, doing these transformations should be safe.
# running these transformations on .s files reduces the
# amount of code by about 15-25%, but it also removes debugging
# information (sourceline directives).
# this speeds up game execution and makes the game smaller.
# currently, the optimizer takes input only from stdin and
# apply a single transformation at a time.
# a future optimization could be to cache code-chunks between
# jump labels and apply multiple transformation on the in-memory
# code.
import sys, re
class MultiLineMatcher():
def __init__(self, regexes, matchfn, nomatchfn, fout=sys.stdout):
self.matchfn = matchfn
self.nomatchfn = nomatchfn
self.regexes = regexes
self.line_matches = 0
self.saved_lines = None
self.matches = None
self.fout = fout
def feed(self, line):
line = line.rstrip('\n')
m = self.regexes[self.line_matches].match(line)
if m:
if self.matches is None: self.matches = []
if self.saved_lines is None: self.saved_lines = []
self.matches.append(m)
self.saved_lines.append(line)
self.line_matches += 1
if self.line_matches == len(self.regexes):
self.matchfn(self, self.matches, self.saved_lines)
self.matches = None
self.line_matches = 0
self.saved_lines = None
else:
self.line_matches = 0
self.matches = None
if self.saved_lines:
for ln in self.saved_lines:
self.nomatchfn(self, ln)
self.saved_lines = None
self.nomatchfn(self, line)
removed = 0
lineno = 0
def push_pop_matchfn(matcher, matches, lines):
ws, reg1 = matches[0].groups(0)
ws2, reg2 = matches[1].groups(0)
global removed
if reg1 == reg2: removed += 1
else: matcher.fout.write("%smr %s, %s\n"%(ws, reg2, reg1))
removed += 1
def output_fn(matcher, line):
matcher.fout.write( "%s\n" % line)
def sourceline_matchfn(matcher, matches, lines):
global removed
removed += 1
def thisaddr_sourceline_matchfn(matcher, matches, lines):
global removed
ws, addr = matches[0].groups(0)
matcher.fout.write( "%s\n" % lines[0])
matcher.fout.write( "%ssourceline %d\n" %(ws, int(addr)))
removed += 1
def cmp_mr_matchfn(matcher, matches, lines):
global removed
ws, op = matches[0].groups(0)
matcher.fout.write( "%s%s ax, bx\n"%(ws, op))
removed += 1
def cmp2_matchfn(matcher, matches, lines):
def reverse_cmp(op):
if op == 'gt': return 'lte'
elif op == 'gte': return 'lt'
elif op == 'lt': return 'gte'
elif op == 'lte': return 'gt'
global removed
ws, val = matches[1].groups(0)
ws2, op = matches[2].groups(0)
matcher.fout.write("%sli bx, %s\n"%(ws, val))
matcher.fout.write("%s%s ax, bx\n"%(ws, op)) # since we already switched registers, we don't need to switch the op too
removed += 2
def load_negative_literal_matchfn(matcher, matches, lines):
global removed
ws, val = matches[1].groups(0)
matcher.fout.write( "%smr bx, ax\n"%(ws))
matcher.fout.write( "%sli ax, -%s\n"%(ws, val))
removed += 4
def load_negative_literal2_matchfn(matcher, matches, lines):
global removed
ws, val = matches[0].groups(0)
matcher.fout.write( "%sli ax, -%s\n"%(ws, val))
matcher.fout.write( "%sli bx, -%s\n"%(ws, val))
removed += 2
def load_literal_matchfn(matcher, matches, lines):
global removed
ws, val = matches[1].groups(0)
matcher.fout.write( "%smr bx, ax\n"%(ws))
matcher.fout.write( "%sli ax, %s\n"%(ws, val))
removed += 1
def axmar_matchfn(matcher, matches, lines):
global removed
ws, val = matches[0].groups(0)
matcher.fout.write( "%sli bx, %s\n"%(ws, val))
matcher.fout.write( "%s\n" % lines[2])
matcher.fout.write( "%s\n" % lines[3])
removed += 1
def mr_swap_matchfn(matcher, matches, lines):
global removed
matcher.fout.write( "%s\n" % lines[0])
removed += 1
def memread4_swap_matchfn(matcher, matches, lines):
global removed
# # memread4 ax; mr bx, ax; li ax, 1
ws, reg1 = matches[0].groups(0)
ws, reg2a, reg2b = matches[1].groups(0)
ws, reg3, val = matches[2].groups(0)
if reg1 == reg2b and reg1 == reg3:
matcher.fout.write("%smemread4 %s\n"%(ws, reg2a))
matcher.fout.write("%sli %s, %s\n"%(ws, reg1, val))
removed += 1
else:
for line in lines: matcher.fout.write("%s\n"%line)
def ptrstack2x_matchfn(matcher, matches, lines):
global removed
ws, val1 = matches[0].groups(0)
ws, val2 = matches[2].groups(0)
if val1 == val2:
matcher.fout.write( "%s\n" % lines[0])
matcher.fout.write( "%s\n" % lines[1])
removed += 1
else:
for line in lines: matcher.fout.write("%s\n"%line)
def regload_arith_matchfn(matcher, matches, lines):
# li ax, 1; add bx, ax, ax; mr ax, bx
global removed
ws, reg1, val = matches[0].groups(0)
ws, op, reg2a, reg2b = matches[1].groups(0)
ws, reg3a, reg3b = matches[2].groups(0)
if reg1 == reg2b and reg3a == reg1 and reg2a == reg3b:
matcher.fout.write( "%s%si %s, %s\n" % (ws, op, reg2a, val))
matcher.fout.write( "%s\n" % lines[2])
removed += 1
else:
for line in lines: matcher.fout.write("%s\n"%line)
def load0_matchfn(matcher, matches, lines):
global removed
ws, reg1 = matches[0].groups(0)
matcher.fout.write( "%sxor %s, %s\n"%(ws, reg1, reg1))
# actually we don't remove here, but we wanna see the count of replacements
removed += 1
# ========= macro replacement ==========
def objcall0_matchfn(matcher, matches, lines):
ws, obj, tail = matches[0].groups(0)
ws, func, tail = matches[5].groups(0)
global removed
matcher.fout.write("%sOBJCALL0(%s, %s)%s\n"%(ws, func, obj, tail))
removed += 1
def objcall1_matchfn(matcher, matches, lines):
ws, arg, tail = matches[0].groups(0)
ws, obj, tail = matches[1].groups(0)
ws, func, tail = matches[7].groups(0)
global removed
matcher.fout.write("%sOBJCALL1(%s, %s, %s)%s\n"%(ws, func, obj, arg, tail))
removed += 10
def objcall1dynstr_matchfn(matcher, matches, lines):
ws, arg, tail = matches[0].groups(0)
ws, obj, tail = matches[3].groups(0)
ws, func, tail = matches[9].groups(0)
global removed
matcher.fout.write("%sOBJCALL1_DYNSTR(%s, %s, %s)%s\n"%(ws, func, obj, arg, tail))
removed += 12
def farcall0_matchfn(matcher, matches, lines):
ws, fun, tail = matches[1].groups(0)
global removed
removed += 1
matcher.fout.write("%sFARCALL0(%s)%s\n"%(ws, fun, tail))
def farcall1_matchfn(matcher, matches, lines):
ws, arg, tail = matches[0].groups(0)
ws, fun, tail = matches[3].groups(0)
global removed
removed += 1
matcher.fout.write("%sFARCALL1(%s, %s)%s\n"%(ws, fun, arg, tail))
def varislit_matchfn(matcher, matches, lines):
ws, var, tail = matches[0].groups(0)
ws, val, tail = matches[3].groups(0)
ws, op, tail_ = matches[4].groups(0)
global removed
removed += 2
m = "VAR_EQ" if op == 'cmpeq' else "VAR_NE"
matcher.fout.write("%s%s(%s, %s)%s\n"%(ws, m, var, val, tail))
def varislit2_matchfn(matcher, matches, lines):
ws, var, tail = matches[0].groups(0)
ws, val, tail = matches[2].groups(0)
ws, op, tail_ = matches[3].groups(0)
global removed
removed += 1
m = "VAR_EQ" if op == 'cmpeq' else "VAR_NE"
matcher.fout.write("%s%s(%s, %s)%s\n"%(ws, m, var, val, tail))
def incvar_matchfn(matcher, matches, lines):
ws, val, tail = matches[0].groups(0)
ws, var1, tail = matches[2].groups(0)
ws, op, tail = matches[5].groups(0)
ws, var2, tail = matches[6].groups(0)
global removed
if var1 != var2:
for line in lines: matcher.fout.write("%s\n"%line)
else:
removed += 1
vali = int(val)
if op == "sub":
vali *= -1
matcher.fout.write("%sINC_VAR(%s, %d)%s\n"%(ws, var1, vali, tail))
def incvar2_matchfn(matcher, matches, lines):
ws, var, tail = matches[0].groups(0)
ws, op, val, tail = matches[2].groups(0)
global removed
removed += 1
vali = int(val)
if op == "subi":
vali *= -1
matcher.fout.write("%sINC_VAR(%s, %d)%s\n"%(ws, var, vali, tail))
def setvar_matchfn(matcher, matches, lines):
ws, val, tail = matches[0].groups(0)
ws, var, tail = matches[1].groups(0)
global removed
removed += 1
matcher.fout.write("%sSET_VAR(%s, %s)%s\n"%(ws, var, val, tail))
def eprint(text): sys.stderr.write("%s\n"%text)
def usage():
eprint("usage: %s [options] infile outfile"%sys.argv[0])
eprint("")
eprint("options: -cmp -pushpop -sourceline -lnl -ll -cmp2 -axmar -mrswap -m4s ...")
eprint("at least one option required.\n")
for i in commandline_args_matcher_map.keys():
eprint("%s: %s"%(i, help_text[i]))
return 1
def get_matcher_name(matcher):
return matcher_names[matcher]
removed_per_matcher = {}
def optimize(matcher, fin):
global lineno
global removed
removed = 0
while True:
lineno = lineno + 1
s = fin.readline()
if s == '': break
matcher.feed(s)
mn = get_matcher_name(matcher)
global removed_per_matcher
removed_per_matcher[mn] = removed
def seek_text(fin, fout):
global lineno
while True:
lineno = lineno + 1
s = fin.readline()
if s == '': break
fout.write(s)
if s.startswith('.text'): break
all_matchers = {}
matcher_names = {}
re_tail = r"(| \\)$"
re_digits = r"([0-9]+)"
re_number__ = r"-{0,1}[0-9]+"
re_number = r"(" + re_number__ + ")"
re_string__ = r'\"[^"]*\"'
re_string = r'(' + re_string__ + ')'
# variable prefixed with @ means: variable exported by curr script
re_var__ = r"@{0,1}[_9A-Za-z]+[0-9A-Za-z_]*"
re_var = r"(" + re_var__ + ")"
re_var_str_or_num = "(" + "(?:" + re_var__ + ")|(?:" + re_string__ + ")|(?:" + re_number__ +")" + ")"
re_iden_or_num = r"([0-9A-Za-z_]+)"
re_leading_ws = r"(\s*)"
re_gpr = r"([a-d]x)"
re_extfunc = r"([_9A-Za-z]+[0-9A-Za-z_:]*)"
def prep_matchers():
global all_matchers
global matcher_names
all_matchers['pushpop_matcher'] = MultiLineMatcher([
re.compile('(\s+)push ([a-z]+)'),
re.compile('(\s+)pop ([a-z]+)'),
], push_pop_matchfn, output_fn)
all_matchers['sourceline_matcher'] = MultiLineMatcher([
re.compile('(\s+)sourceline ([0-9]+)'),
], sourceline_matchfn, output_fn)
# this matcher is only for debug purposes.
# it adds a sourceline statement after every thisaddr,
# which is right at the beginning of each func.
# so one can set e.g. a breakpoint in the ags interpreter.
all_matchers['thisaddr_sourceline_matcher'] = MultiLineMatcher([
re.compile('(\s+)thisaddr ([0-9]+)$'),
], thisaddr_sourceline_matchfn, output_fn)
all_matchers['cmp_mr_matcher'] = MultiLineMatcher([
re.compile('(\s+)(cmpeq|cmpne|lor|land) bx, ax'),
re.compile('(\s+)mr ax, bx'),
], cmp_mr_matchfn, output_fn)
all_matchers['load_negative_literal_matcher'] = MultiLineMatcher([
re.compile('(\s+)push ax$'),
re.compile('(\s+)li ax, ([0-9]+)$'),
re.compile('(\s+)li bx, 0$'),
re.compile('(\s+)sub bx, ax$'),
re.compile('(\s+)mr ax, bx$'),
re.compile('(\s+)pop bx$'),
], load_negative_literal_matchfn, output_fn)
all_matchers['load_negative_literal2_matcher'] = MultiLineMatcher([
re.compile('(\s+)li ax, ([0-9]+)$'),
re.compile('(\s+)li bx, 0$'),
re.compile('(\s+)sub bx, ax$'),
re.compile('(\s+)mr ax, bx$'),
], load_negative_literal2_matchfn, output_fn)
all_matchers['load_literal_matcher'] = MultiLineMatcher([
re.compile('(\s+)push ax$'),
re.compile('(\s+)li ax, ([0-9]+)$'),
re.compile('(\s+)pop bx$'),
], load_literal_matchfn, output_fn)
all_matchers['cmp2_matcher'] = MultiLineMatcher([
re.compile('(\s+)mr bx, ax'),
re.compile('(\s+)li ax, ([0-9]+)'),
re.compile('(\s+)(gt|gte|lt|lte) bx, ax'),
re.compile('(\s+)mr ax, bx'),
], cmp2_matchfn, output_fn)
all_matchers['axmar_matcher'] = MultiLineMatcher([
re.compile('(\s+)li ax, ([0-9]+)$'),
re.compile('(\s+)mr bx, ax$'),
re.compile('(\s+)li mar, (.+)$'),
re.compile('(\s+)mr ax, mar$'),
], axmar_matchfn, output_fn)
all_matchers['mr_swap_matcher'] = MultiLineMatcher([
re.compile('(\s+)mr ax, bx'),
re.compile('(\s+)mr bx, ax'),
], mr_swap_matchfn, output_fn)
all_matchers['memread4_swap_matcher'] = MultiLineMatcher([
# memread4 ax; mr bx, ax; li ax, 1
re.compile('(\s+)memread4 ([a-d]x)'),
re.compile('(\s+)mr ([a-d]x), ([a-d]x)'),
re.compile('(\s+)li ([a-d]x), ([0-9]+)$'),
], memread4_swap_matchfn, output_fn)
all_matchers['ptrstack2x_matcher'] = MultiLineMatcher([
re.compile('(\s+)ptrstack ([0-9]+)$'),
re.compile('(\s+)mem.*$'),
re.compile('(\s+)ptrstack ([0-9]+)$'),
], ptrstack2x_matchfn, output_fn)
all_matchers['regload_arith_matcher'] = MultiLineMatcher([
# li ax, 1; add bx, ax, ax; mr ax, bx
re.compile('(\s+)li ([a-d]x), ([0-9]+)$'),
re.compile('(\s+)(add|sub) ([a-d]x), ([a-d]x)$'),
re.compile('(\s+)mr ([a-d]x), ([a-d]x)$'),
], regload_arith_matchfn, output_fn)
all_matchers['assertlte_matcher'] = MultiLineMatcher([
re.compile('(\s+)assertlte\s.*'),
], sourceline_matchfn, output_fn)
all_matchers['load0_matcher'] = MultiLineMatcher([
re.compile('(\s+)li ([a-d]x), 0$'),
], load0_matchfn, output_fn)
# macros. we replace specific snippets with macros, to make the code
# more readable.
all_matchers['farcall0_matcher'] = MultiLineMatcher([
re.compile(re_leading_ws + r'setfuncargs 0' + re_tail),
re.compile(re_leading_ws + r'li ax, ' + re_extfunc + re_tail),
re.compile(re_leading_ws + r'farcall ax' + re_tail),
], farcall0_matchfn, output_fn)
all_matchers['farcall1_matcher'] = MultiLineMatcher([
re.compile(re_leading_ws + r'li ax, ' + re_var_str_or_num + re_tail),
re.compile(re_leading_ws + r'farpush ax' + re_tail),
re.compile(re_leading_ws + r'setfuncargs 1' + re_tail),
re.compile(re_leading_ws + r'li ax, ' + re_extfunc + re_tail),
re.compile(re_leading_ws + r'farcall ax' + re_tail),
re.compile(re_leading_ws + r'farsubsp 1' + re_tail),
], farcall1_matchfn, output_fn)
all_matchers['objcall0_matcher'] = MultiLineMatcher([
re.compile(re_leading_ws + r'li mar, ' + re_var + re_tail),
re.compile(re_leading_ws + r'mr ax, mar' + re_tail),
re.compile(re_leading_ws + r'push op' + re_tail),
re.compile(re_leading_ws + r'callobj ax' + re_tail),
re.compile(re_leading_ws + r'setfuncargs 0' + re_tail),
re.compile(re_leading_ws + r'li ax, ' + re_extfunc + re_tail),
re.compile(re_leading_ws + r'farcall ax' + re_tail),
re.compile(re_leading_ws + r'pop op' + re_tail),
], objcall0_matchfn, output_fn)
all_matchers['objcall1_matcher'] = MultiLineMatcher([
re.compile(re_leading_ws + r'li bx, ' + re_iden_or_num + re_tail),
re.compile(re_leading_ws + r'li mar, ' + re_iden_or_num + re_tail),
re.compile(re_leading_ws + r'mr ax, mar' + re_tail),
re.compile(re_leading_ws + r'push op' + re_tail),
re.compile(re_leading_ws + r'callobj ax' + re_tail),
re.compile(re_leading_ws + r'farpush bx' + re_tail),
re.compile(re_leading_ws + r'setfuncargs 1' + re_tail),
re.compile(re_leading_ws + r'li ax, ' + re_extfunc + re_tail),
re.compile(re_leading_ws + r'farcall ax' + re_tail),
re.compile(re_leading_ws + r'farsubsp 1' + re_tail),
re.compile(re_leading_ws + r'pop op' + re_tail),
], objcall1_matchfn, output_fn)
all_matchers['objcall1dynstr_matcher'] = MultiLineMatcher([
re.compile(re_leading_ws + r'li ax, ' + re_string + re_tail),
re.compile(re_leading_ws + r'newstr ax' + re_tail),
re.compile(re_leading_ws + r'mr bx, ax' + re_tail),
re.compile(re_leading_ws + r'li mar, ' + re_var + re_tail),
re.compile(re_leading_ws + r'mr ax, mar' + re_tail),
re.compile(re_leading_ws + r'push op' + re_tail),
re.compile(re_leading_ws + r'callobj ax' + re_tail),
re.compile(re_leading_ws + r'farpush bx' + re_tail),
re.compile(re_leading_ws + r'setfuncargs 1' + re_tail),
re.compile(re_leading_ws + r'li ax, ' + re_extfunc + re_tail),
re.compile(re_leading_ws + r'farcall ax' + re_tail),
re.compile(re_leading_ws + r'farsubsp 1' + re_tail),
re.compile(re_leading_ws + r'pop op' + re_tail),
], objcall1dynstr_matchfn, output_fn)
all_matchers['varislit_matcher'] = MultiLineMatcher([
re.compile(re_leading_ws + r'li mar, ' + re_var + re_tail),
re.compile(re_leading_ws + r'memread4 ax' + re_tail),
re.compile(re_leading_ws + r'mr bx, ax' + re_tail),
re.compile(re_leading_ws + r'li ax, ' + re_number + re_tail),
re.compile(re_leading_ws + r'(cmpeq|cmpne) ax, bx' + re_tail),
], varislit_matchfn, output_fn)
all_matchers['varislit2_matcher'] = MultiLineMatcher([
re.compile(re_leading_ws + r'li mar, ' + re_var + re_tail),
re.compile(re_leading_ws + r'memread4 bx' + re_tail),
re.compile(re_leading_ws + r'li ax, ' + re_number + re_tail),
re.compile(re_leading_ws + r'(cmpeq|cmpne) ax, bx' + re_tail),
], varislit2_matchfn, output_fn)
all_matchers['incvar_matcher'] = MultiLineMatcher([
re.compile(re_leading_ws + r'li ax, ' + re_number + re_tail),
re.compile(re_leading_ws + r'push ax' + re_tail),
re.compile(re_leading_ws + r'li mar, ' + re_var + re_tail),
re.compile(re_leading_ws + r'memread4 ax' + re_tail),
re.compile(re_leading_ws + r'pop bx' + re_tail),
re.compile(re_leading_ws + r'(sub|add) ax, bx' + re_tail),
re.compile(re_leading_ws + r'li mar, ' + re_var + re_tail),
re.compile(re_leading_ws + r'memwrite4 ax' + re_tail),
], incvar_matchfn, output_fn)
all_matchers['incvar2_matcher'] = MultiLineMatcher([
re.compile(re_leading_ws + r'li mar, ' + re_var + re_tail),
re.compile(re_leading_ws + r'memread4 ax' + re_tail),
re.compile(re_leading_ws + r'(subi|addi) ax, ' + re_number + re_tail),
re.compile(re_leading_ws + r'memwrite4 ax' + re_tail),
], incvar2_matchfn, output_fn)
all_matchers['setvar_matcher'] = MultiLineMatcher([
re.compile(re_leading_ws + r'li ax, ' + re_number + re_tail),
re.compile(re_leading_ws + r'li mar, ' + re_var + re_tail),
re.compile(re_leading_ws + r'memwrite4 ax' + re_tail),
], setvar_matchfn, output_fn)
for i in all_matchers.keys():
matcher_names[all_matchers[i]] = i
commandline_args_matcher_map = {
"-cmp" : 'cmp_mr_matcher',
"-pushpop": 'pushpop_matcher',
"-sourceline": 'sourceline_matcher',
"-lnl": 'load_negative_literal_matcher',
"-lnl2": 'load_negative_literal2_matcher',
"-ll": 'load_literal_matcher',
"-cmp2": 'cmp2_matcher',
"-axmar": 'axmar_matcher',
"-mrswap": 'mr_swap_matcher',
"-m4s": 'memread4_swap_matcher',
"-ptrstack2x": 'ptrstack2x_matcher',
"-rlarith": 'regload_arith_matcher',
"-assertlte": "assertlte_matcher",
"-load0": "load0_matcher",
"-fcdebug": "thisaddr_sourceline_matcher",
"-objcall0": "objcall0_matcher",
"-objcall1": "objcall1_matcher",
"-objcall1dynstr": "objcall1dynstr_matcher",
"-farcall0": "farcall0_matcher",
"-farcall1": "farcall1_matcher",
"-varislit": "varislit_matcher",
"-varislit2": "varislit2_matcher",
"-incvar": "incvar_matcher",
"-incvar2": "incvar2_matcher",
"-setvar": "setvar_matcher",
}
help_text = {
'-cmp': "optimize cmp/mr",
'-cmp2': "optimize gt/gte/lt/lte (requires prev -ll pass)",
"-pushpop": "optimize push/pop",
"-sourceline": "remove sourceline statements",
"-lnl": "optimize negative literal loads",
"-lnl2": "optimize negative literal loads variant2",
"-ll" : "optimize literal loads",
"-axmar": "...",
"-mrswap": "remove gratuitous reverse register copy",
"-m4s": "optimize register swap after memread",
"-ptrstack2x": "optimize duplicate ptrstack statements",
"-rlarith": "remove temporary register loads for add/sub",
"-assertlte": "remove assertlte statements",
"-load0": "replace load of 0 with xor reg, reg",
"-fcdebug": "insert sourceline directives on the start of each func (4 debugging)",
"-objcall0": "replace objcall0 with macro",
"-objcall1": "replace objcall1 with macro",
"-objcall1dynstr": "replace objcall1 with macro",
"-farcall0": "replace farcall0 with macro",
"-farcall1": "replace farcall1 with macro",
"-varislit": "varislit macro (neg)",
"-varislit2": "varislit macro",
"-incvar": "incvar macro",
"-incvar2": "incvar macro",
"-setvar": "setvar macro",
}
def main():
matcher_names = []
if len(sys.argv) < 4: return usage()
# -cmp -pushpop -sourceline -lnl -ll -cmp2 -axmar -mrswap
for i in xrange(1, len(sys.argv)-2):
if not sys.argv[i] in commandline_args_matcher_map: return usage()
else: matcher_names.append(commandline_args_matcher_map[sys.argv[i]])
fn = sys.argv[len(sys.argv)-2]
fin = open(fn, "r")
fout = open(sys.argv[len(sys.argv)-1], "w")
prep_matchers()
import tempfile
for mn in matcher_names:
tmp = tempfile.TemporaryFile()
m = all_matchers[mn]
m.fout = tmp
seek_text(fin, m.fout)
optimize(m, fin)
tmp.seek(0)
fin = tmp
while 1:
chunk = fin.read(4096)
if chunk == '': break
fout.write(chunk)
s = ''
total_removed = 0
for i in removed_per_matcher.keys():
if removed_per_matcher[i] == 0: continue
total_removed += removed_per_matcher[i]
s += "[%s:%d] " %(i[:len(i)-len("_matcher")], removed_per_matcher[i])
if total_removed:
sys.stdout.write( "%s: removed %d lines %s\n"%(fn, total_removed, s))
return 0
if __name__ == "__main__": sys.exit(main())