forked from tkoptimizer/Assembly-code-optimizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperations_new.py
679 lines (511 loc) · 14.9 KB
/
operations_new.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
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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
"""
File: operations_new.py
Authors: Tim van Deurzen, Koos van Strien
Date: 26-02-2010
"""
class operation:
"""
Defines a single operation and allows easy access to all the datafields of
the operation. Using this class also simplifies certain optimizations.
"""
# Control operations
control = [
'j',
'jr',
'jal',
'jalr',
'beq',
'bne',
'blez',
'bgtz',
'bltz',
'bgez',
'bc1f',
'bc1t',
'bct',
'bcf'
]
# Load operations
load = [
'la',
'lb',
'lbu',
'lhu',
'lh',
'lwl',
'lwr',
'lw',
'dlw',
'l.s',
'l.d',
'li'
]
# Store operations
store = [
'sb',
'sh',
'dsz',
'swl',
'swr',
'sbu',
'shu',
'sw',
'dsw',
's.s',
's.d'
]
# Integer arithmetic operations
integerArithmetic = [
'add',
'addi',
'addu',
'addiu',
'sub',
'subu',
'mult',
'multu',
'div',
'divu',
'mfhi',
'mthi',
'mflo',
'mtlo',
'move',
'and',
'andi',
'or',
'ori',
'xor',
'xori',
'nor',
'sll',
'sllv',
'srl',
'srlv',
'sra',
'srav',
'slt',
'slti',
'sltu',
'sltiu'
]
# Float arithmetic operations
floatArithmetic = [
'add.s',
'add.d',
'sub.s',
'sub.d',
'mul.s',
'mul.d',
'div.s',
'div.d',
'abs.s',
'abs.d',
'mov.s',
'mov.d',
'neg.s',
'neg.d',
'cvt',
'c.s',
'c.d',
'cvt.s.d',
'cvt.s.w',
'cvt.d.s',
'cvt.d.w',
'cvt.w.s',
'cvt.w.d',
'c.eq.s',
'c.eq.d',
'c.lt.s',
'c.l.t.d',
'c.le.s',
'c.le.d',
'sqrt.s',
'sqrt.d'
]
# System operations
system = [
'nop',
'syscall',
'break',
'lui',
'mfc1',
'mtc1'
]
# The operation types, these values can be used for operation
# identification by other classes.
CONTROL= 0
LOAD = 1
STORE = 2
INT_ARITHMETIC = 3
FLOAT_ARITHMETIC = 4
SYSTEM = 5
LABEL = 6
NONE = 7
# Load and store sizes
BYTE = 0
WORD = 1
UNKNOWN = 2
@staticmethod
def getInstance(line, lineNumber):
parts = line.split()
if len(parts) == 1:
return LabelOp(line, lineNumber)
elif len(parts) < 1:
return NoOp(line, lineNumber)
op = parts[0]
if op in operation.control:
return ControlOp(line, lineNumber)
elif op in operation.load:
return LoadOp(line, lineNumber)
elif op in operation.store:
return StoreOp(line, lineNumber)
elif op in operation.integerArithmetic:
return IntArithmeticOp(line, lineNumber)
elif op in operation.floatArithmetic:
return FloatArithmeticOp(line, lineNumber)
elif op in operation.system:
return SystemOp(line, lineNumber)
else:
return NoOp(line, lineNumber)
def __init__(self, line, lineNumber):
if len(line) == 0:
raise Exception, "Can't find an operation on an empty line."
# Get the operation from the line.
parts = line.split()
self.code = line[0:-1]
# Backup the code so we can always return to a certain status quo.
self.original_code = self.code
self.included = True
self.lineNumber = lineNumber
def __str__(self):
"""
Python's version of toString :-)
"""
code = self.code
code = code.replace('\t', ' ')
if self.included:
return str(self.lineNumber) + " [ " + \
self.verboseType(self.type) + " ]: " + code
else:
return str(self.lineNumber) + " [ " + \
self.verboseType(self.type) + " ]: " + code
def hasArguments(self):
"""
Method necessary to counter certain exceptions which are thrown if
arguments are sought, but are not there.
"""
return self.getArguments() is not None
def getArguments(self):
"""
Get a string list of all the arguments for an operation.
"""
parts = self.code.split()
if len(parts) > 1:
return parts[1].split(",")
return None
def setArguments(self, arguments):
"""
Change the arguments.
"""
parts = self.code.split()
if len(parts) > 1:
self.code = "\t" + parts[0] + "\t"
for argument in arguments:
self.code += argument + ","
# remove last ','
self.code = self.code[:-1]
def setOperation(self, operation):
"""
Alter the operation.
"""
parts = self.code.split()
if len(parts) > 1:
self.code = "\t" + operation + "\t" + parts[1]
def verboseType(self, type):
"""
Gives a string representation of the type of the operator.
"""
typeList = ({
0: 'CONTROL',
1: 'LOAD',
2: 'STORE',
3: 'INTEGER ARITHMETIC',
4: 'FLOAT ARITHMETIC',
5: 'SYSTEM',
6: 'LABEL',
7: 'NONE'
})
return typeList[type]
def exclude(self):
"""
Makes sure the status of this operation is 'not included'. Operations
that are exclude will be commented out in the eventual code (or left
out).
"""
self.included = False
def include(self):
"""
Re-include the operation in the eventual code.
"""
self.included = True
def getType(self):
"""
Python lacks an actual 'switch' statement which makes this code less
than pretty.
"""
if self.operation in operation.control:
return operation.CONTROL
elif self.operation in operation.load:
return operation.LOAD
elif self.operation in operation.store:
return operation.STORE
elif self.operation in operation.integerArithmetic:
return operation.INT_ARITHMETIC
elif self.operation in operation.floatArithmetic:
return operation.FLOAT_ARITHMETIC
else:
return operation.SYSTEM
def getTarget(self):
"""
Gets either the jump target for a control operation or the target
register for an arithmetic, load or store operation.
"""
parts = self.code.split()
arguments = parts[-1].split(",")
return arguments[0]
def setTarget(self, target):
"""
Change the target register for either a control operation or an
arithmetic or load register.
"""
if self.type == operation.STORE or self.type == operation.SYSTEM:
raise Exception, "Store or system operations don't have a target!"
elif self.type == operation.CONTROL:
# Replace the last argument, the target, with a new target.
parts = self.code.split(",")
if len(parts) == 1:
parts = parts[0]
parts = parts.split()
oldTarget = parts[-1]
self.code = self.code[:len(self.code) - len(oldTarget)] + target
else:
# Replace the first argument, the target, with a new target.
parts = self.code.split()
first = parts[0]
parts = parts[1].split(",")
self.code = first + target + parts[1:]
def resetOperation(self):
"""
Put back a backup of the original code.
"""
self.include()
self.code = self.original_code
def isMove(self):
return False
class ControlOp(operation):
def __init__(self, line, lineNumber):
"""
Stores the line.
"""
operation.__init__(self, line, lineNumber)
parts = line.split()
self.operation = parts[0]
self.type = operation.CONTROL
def getTarget(self):
"""
For branch operations the last argument is the target (or offset).
"""
parts = self.code.split(",")
if len(parts) == 1:
parts = parts[0]
parts = parts.split()
return parts[-1]
class LoadStore(operation):
def __init__(self, line, lineNumber):
operation.__init__(self, line, lineNumber)
parts = line.split()
self.operation = parts[0]
self.size = self.determineSize()
def getAddress(self):
"""
Get the address of a load or store operation using python's 'split'
statement.
"""
parts = self.code.split()
parts = parts[1].split(",")
try:
return parts[1]
except:
" Apparently we're trying to return empty data, skipping. "
def setAddress(self, address):
"""
Change the address of a load or store operation using python's 'split'
operation.
"""
parts = self.code.split()
first = parts[0]
parts = parts[1].split(",")
self.code = first + address + parts[:1]
def usesOffset(self):
"""
Determine wether a load or a store uses an offset.
"""
try:
address = self.getAddress()
if address[-1] == ")":
return True
except:
return False
def getOffsetRegister(self):
"""
Get the register that is used as an offset.
"""
address = self.getAddress()
if address[-1] == ")":
parts = address.split("(")
register = parts[1][:-1]
return register
def getOffset(self):
address = self.getAddress()
if address[-1] == ")":
parts = address.split("(")
return parts[0]
def determineSize(self):
"""
Find out if we're loading a block, a byte or a word.
"""
sizeToken = self.operation[1]
if sizeToken == ".":
sizeToken = self.operation[2]
elif sizeToken == "i":
sizeToken = "b"
if sizeToken == "w":
return operation.WORD
elif sizeToken == "b":
return operation.BYTE
else:
return operation.UNKNOWN
class LoadOp(LoadStore):
def __init__(self, line, lineNumber):
"""
Stores the line.
"""
LoadStore.__init__(self, line, lineNumber)
self.type = operation.LOAD
class StoreOp(LoadStore):
def __init__(self, line, lineNumber):
"""
Stores the line.
"""
LoadStore.__init__(self, line, lineNumber)
self.type = operation.STORE
def setSource(self, source):
"""
This method exists mostly for semantic reasons and does the same as
setTarget. This method only works with STORE operations.
"""
parts = self.code.split()
first = parts[0]
parts = parts[1].split(",")
self.code = "\t" + first + "\t" + source + "," + parts[1]
class IntArithmeticOp(operation):
def __init__(self, line, lineNumber):
"""
Stores the line.
"""
operation.__init__(self, line, lineNumber)
parts = line.split()
self.operation = parts[0]
self.type = operation.INT_ARITHMETIC
def isMove(self):
"""
Determine if current operation is a 'move' operation
"""
return (self.operation == "move")
def getMoveSource(self):
"""
Gets the source address of a move operation
"""
if self.isMove():
parts = self.code.split()
parts = parts[1].split(",")
try:
return parts[1]
except:
raise Exception, "Fatal: move operation " + self.code + \
" has no source address."
def setMoveSource(self, address):
"""
Sets the source address of a move operation
"""
if self.isMove():
parts = self.code.split()
parts = parts[1].split(",")
self.code = self.code.replace(parts[1], address)
def getMoveDestination(self):
"""
Gets the destination address of a move operation
"""
if self.isMove():
parts = self.code.split()
parts = parts[1].split(",")
try:
return parts[0]
except:
raise Exception, "Fatal: move operation " + self.code + \
" has no destination address..."
def setMoveDestination(self, address):
"""
Sets the destination address of a move operation
"""
if self.isMove():
parts = self.code.split()
parts = parts[1].split(",")
self.code = self.code.replace(parts[0], address)
class FloatArithmeticOp(operation):
def __init__(self, line, lineNumber):
"""
Stores the line.
"""
operation.__init__(self, line, lineNumber)
parts = line.split()
self.operation = parts[0]
self.type = operation.FLOAT_ARITHMETIC
class SystemOp(operation):
def __init__(self, line, lineNumber):
"""
Stores the line.
"""
operation.__init__(self, line, lineNumber)
parts = line.split()
self.operation = parts[0]
self.type = operation.SYSTEM
def getTarget(self):
raise Exception, "System operations don't have a target!" + \
str(self.operation)
class LabelOp(operation):
def __init__(self, line, lineNumber):
"""
Stores the line.
"""
operation.__init__(self, line, lineNumber)
self.labelName = line
self.type = operation.LABEL
def getLabelName(self):
"""
Only return the label name if there is no operation, just a label.
"""
if self.type == operation.LABEL:
return self.labelName
class NoOp(operation):
def __init__(self, line, lineNumber):
"""
Stores the line.
"""
operation.__init__(self, line, lineNumber)
self.labelName = line
self.type = operation.NONE
parts = line.split()
if len(parts) > 0:
self.operation = parts[0]