test_optimize.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import unittest
  2. from src.optimize import optimize_global
  3. from src.statement import Statement as S, Block as B
  4. class TestOptimize(unittest.TestCase):
  5. def setUp(self):
  6. pass
  7. def test_optimize_global_movaa(self):
  8. foo = S('command', 'foo')
  9. bar = S('command', 'bar')
  10. block = B([foo,
  11. S('command', 'move', '$regA', '$regA'),
  12. bar])
  13. optimize_global(block)
  14. self.assertEquals(block.statements, [foo, bar])
  15. def test_optimize_global_movab(self):
  16. foo = S('command', 'foo')
  17. move = S('command', 'move', '$regA', '$regB')
  18. bar = S('command', 'bar')
  19. block = B([foo,
  20. move,
  21. bar])
  22. optimize_global(block)
  23. self.assertEquals(block.statements, [foo, move, bar])
  24. def test_optimize_global_movinst_true(self):
  25. foo = S('command', 'foo')
  26. bar = S('command', 'bar')
  27. block = B([foo,
  28. S('command', 'move', '$regA', '$regB'),
  29. S('command', 'addu', '$regA', '$regA', 2),
  30. bar])
  31. optimize_global(block)
  32. self.assertEquals(block.statements, [foo,
  33. S('command', 'addu', '$regA', '$regB', 2),
  34. bar])
  35. def test_optimize_global_movinst_false(self):
  36. foo = S('command', 'foo')
  37. bar = S('command', 'bar')
  38. statements = [foo, \
  39. S('command', 'move', '$regA', '$regB'), \
  40. S('command', 'addu', '$regA', '$regC', 2), \
  41. bar]
  42. block = B(statements)
  43. optimize_global(block)
  44. self.assertEquals(block.statements, statements)
  45. def test_optimize_global_instr_mov_jal_true(self):
  46. foo = S('command', 'foo')
  47. bar = S('command', 'bar')
  48. block = B([foo,
  49. S('command', 'addu', '$regA', '$regC', 2),
  50. S('command', 'move', '$4', '$regA'),
  51. S('command', 'jal', 'L1'),
  52. bar])
  53. optimize_global(block)
  54. self.assertEquals(block.statements, [foo,
  55. S('command', 'addu', '$4', '$regC', 2),
  56. S('command', 'jal', 'L1'),
  57. bar])
  58. def test_optimize_global_instr_mov_jal_false(self):
  59. foo = S('command', 'foo')
  60. bar = S('command', 'bar')
  61. arguments = [foo, \
  62. S('command', 'addu', '$regA', '$regC', 2), \
  63. S('command', 'move', '$3', '$regA'), \
  64. S('command', 'jal', 'L1'), \
  65. bar]
  66. block = B(arguments)
  67. optimize_global(block)
  68. self.assertEquals(block.statements, arguments)
  69. def test_optimize_global_sw_ld_true(self):
  70. foo = S('command', 'foo')
  71. bar = S('command', 'bar')
  72. block = B([foo,
  73. S('command', 'sw', '$regA', '$regB'),
  74. S('command', 'ld', '$regA', '$regC'),
  75. bar])
  76. optimize_global(block)
  77. self.assertEquals(block.statements, [foo,
  78. S('command', 'sw', '$regA', '$regB'),
  79. bar])
  80. def test_optimize_global_sw_ld_false(self):
  81. foo = S('command', 'foo')
  82. bar = S('command', 'bar')
  83. arguments = [foo, \
  84. S('command', 'sw', '$regA', '$regB'), \
  85. S('command', 'ld', '$regD', '$regC'), \
  86. bar]
  87. block = B(arguments)
  88. optimize_global(block)
  89. self.assertEquals(block.statements, arguments)
  90. def test_optimize_global_shift_true(self):
  91. foo = S('command', 'foo')
  92. bar = S('command', 'bar')
  93. block = B([foo,
  94. S('command', 'sll', '$regA', '$regA', 0),
  95. bar])
  96. optimize_global(block)
  97. self.assertEquals(block.statements, [foo,
  98. bar])
  99. def test_optimize_global_shift_false(self):
  100. foo = S('command', 'foo')
  101. bar = S('command', 'bar')
  102. arguments = [foo, \
  103. S('command', 'sll', '$regA', '$regB', 0), \
  104. bar]
  105. block = B(arguments)
  106. optimize_global(block)
  107. self.assertEquals(block.statements, arguments)
  108. arguments2 = [foo, \
  109. S('command', 'sll', '$regA', '$regA', 1), \
  110. bar]
  111. block2 = B(arguments2)
  112. optimize_global(block2)
  113. self.assertEquals(block2.statements, arguments2)
  114. def test_optimize_global_add_lw_true(self):
  115. foo = S('command', 'foo')
  116. bar = S('command', 'bar')
  117. block = B([foo,
  118. S('command', 'add', '$regA', '$regA', 10),
  119. S('command', 'lw', '$regB', '0($regA)'),
  120. bar])
  121. optimize_global(block)
  122. self.assertEquals(block.statements, [foo,
  123. S('command', 'lw', '$regB', '10($regA)'),
  124. bar])
  125. def test_optimize_global_add_lw_false(self):
  126. foo = S('command', 'foo')
  127. bar = S('command', 'bar')
  128. arguments = [foo, \
  129. S('command', 'add', '$regA', '$regA', 10), \
  130. S('command', 'lw', '$regB', '0($regC)'), \
  131. bar]
  132. block = B(arguments)
  133. optimize_global(block)
  134. arguments2 = [foo, \
  135. S('command', 'add', '$regA', '$regB', 10), \
  136. S('command', 'lw', '$regB', '0($regA)'), \
  137. bar]
  138. block2 = B(arguments2)
  139. arguments3 = [foo, \
  140. S('command', 'add', '$regA', '$regA', 10), \
  141. S('command', 'lw', '$regB', '1($regA)'), \
  142. bar]
  143. block3 = B(arguments3)
  144. optimize_global(block3)
  145. self.assertEquals(block.statements, arguments)
  146. self.assertEquals(block2.statements, arguments2)
  147. self.assertEquals(block3.statements, arguments3)
  148. # beq ..., $Lx -> bne ..., $Ly
  149. # j $Ly $Lx:
  150. # $Lx:
  151. def test_optimize_global_beq_j_true(self):
  152. foo = S('command', 'foo')
  153. bar = S('command', 'bar')
  154. block = B([foo,
  155. S('command', 'beq', '$regA', '$regB', '$Lx'),
  156. S('command', 'j', '$Ly'),
  157. S('label', '$Lx'),
  158. bar])
  159. optimize_global(block)
  160. self.assertEquals(block.statements, [foo,
  161. S('command', 'bne', '$regA', '$regB', '$Ly'),
  162. S('label', '$Lx'),
  163. bar])
  164. def test_optimize_global_beq_j_false(self):
  165. foo = S('command', 'foo')
  166. bar = S('command', 'bar')
  167. arguments = [foo, \
  168. S('command', 'beq', '$regA', '$regB', '$Lz'), \
  169. S('command', 'j', '$Ly'), \
  170. S('label', '$Lx'), \
  171. bar]
  172. block = B(arguments)
  173. optimize_global(block)
  174. self.assertEquals(block.statements, arguments)