test_optimize.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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)