PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/rpython/jit/backend/ppc/test/test_regalloc.py

https://bitbucket.org/pypy/pypy/
Python | 154 lines | 120 code | 34 blank | 0 comment | 2 complexity | 0deb70304054c493bd13bff3f52896b7 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. from rpython.rtyper.lltypesystem import lltype, llmemory
  2. from rpython.rtyper.lltypesystem import rstr
  3. from rpython.rtyper import rclass
  4. from rpython.rtyper.annlowlevel import llhelper
  5. from rpython.rlib.objectmodel import instantiate
  6. from rpython.jit.backend.ppc.locations import (imm, RegisterLocation,
  7. ImmLocation, StackLocation)
  8. from rpython.jit.backend.ppc.register import *
  9. from rpython.jit.backend.ppc.codebuilder import hi, lo
  10. from rpython.jit.backend.ppc.ppc_assembler import AssemblerPPC
  11. from rpython.jit.backend.ppc.arch import WORD
  12. from rpython.jit.codewriter.effectinfo import EffectInfo
  13. from rpython.jit.codewriter import longlong
  14. from rpython.jit.metainterp.history import BasicFailDescr, \
  15. JitCellToken, TargetToken
  16. from rpython.jit.tool.oparser import parse
  17. class MockBuilder(object):
  18. def __init__(self):
  19. self.reset()
  20. def __getattr__(self, name):
  21. instr = MockInstruction(name)
  22. self.instrs.append(instr)
  23. return instr
  24. def str_instrs(self):
  25. return [str(inst) for inst in self.instrs]
  26. def reset(self):
  27. self.instrs = []
  28. class MockInstruction(object):
  29. def __init__(self, name, *args):
  30. self.name = name
  31. self.args = args
  32. def __call__(self, *args):
  33. self.args = args
  34. def __eq__(self, other):
  35. assert isinstance(other, MockInstruction)
  36. return self.name == other.name and self.args == other.args
  37. def __repr__(self):
  38. return self.__str__()
  39. def __str__(self):
  40. return "%s %r" % (self.name, self.args)
  41. MI = MockInstruction
  42. class TestMocks(object):
  43. def setup_method(self, method):
  44. self.builder = MockBuilder()
  45. def test_cmp_instruction(self):
  46. assert MI("a", 1, 2) == MI("a", 1, 2)
  47. assert not MI("a", 1, 2) == MI("b", 1, 2)
  48. assert not MI("a", 1, 2) == MI("a", 2, 2)
  49. assert not MI("a", 1) == MI("a", 1, 2)
  50. assert not MI("a", 1, 2) == MI("a")
  51. assert MI("a") == MI("a")
  52. def test_basic(self):
  53. exp_instrs = [MI("mr", 3, 5),
  54. MI("foobar"),
  55. MI("li", 3, 2),
  56. MI("stw", 3, 5, 100)]
  57. self.builder.mr(3, 5)
  58. self.builder.foobar()
  59. self.builder.li(3, 2)
  60. self.builder.stw(3, 5, 100)
  61. assert self.builder.instrs == exp_instrs
  62. self.builder.blub()
  63. assert self.builder.instr != exp_instrs
  64. class TestRegallocMov(object):
  65. def setup_method(self, method):
  66. self.builder = MockBuilder()
  67. self.asm = instantiate(AssemblerPPC)
  68. self.asm.mc = self.builder
  69. def test_immediate_to_reg(self):
  70. self.asm.regalloc_mov(imm(5), r10)
  71. big = 2 << 28
  72. self.asm.regalloc_mov(imm(big), r0)
  73. exp_instr = [MI("load_imm", r10, 5),
  74. MI("load_imm", r0, big)]
  75. assert self.asm.mc.instrs == exp_instr
  76. def test_immediate_to_mem(self):
  77. self.asm.regalloc_mov(imm(5), stack(6))
  78. big = 2 << 28
  79. self.asm.regalloc_mov(imm(big), stack(7))
  80. exp_instr = [MI("alloc_scratch_reg"),
  81. MI("load_imm", r0, 5),
  82. MI("store", r0.value, SPP.value, get_spp_offset(6)),
  83. MI("free_scratch_reg"),
  84. MI("alloc_scratch_reg"),
  85. MI("load_imm", r0, big),
  86. MI("store", r0.value, SPP.value, get_spp_offset(7)),
  87. MI("free_scratch_reg")]
  88. assert self.asm.mc.instrs == exp_instr
  89. def test_mem_to_reg(self):
  90. self.asm.regalloc_mov(stack(5), reg(10))
  91. self.asm.regalloc_mov(stack(0), reg(0))
  92. exp_instrs = [MI("load", r10.value, SPP.value, get_spp_offset(5)),
  93. MI("load", r0.value, SPP.value, get_spp_offset(0))]
  94. assert self.asm.mc.instrs == exp_instrs
  95. def test_mem_to_mem(self):
  96. self.asm.regalloc_mov(stack(5), stack(6))
  97. exp_instrs = [
  98. MI("alloc_scratch_reg"),
  99. MI("load", r0.value, SPP.value, get_spp_offset(5)),
  100. MI("store", r0.value, SPP.value, get_spp_offset(6)),
  101. MI("free_scratch_reg")]
  102. assert self.asm.mc.instrs == exp_instrs
  103. def test_reg_to_reg(self):
  104. self.asm.regalloc_mov(reg(0), reg(1))
  105. self.asm.regalloc_mov(reg(5), reg(10))
  106. exp_instrs = [MI("mr", r1.value, r0.value),
  107. MI("mr", r10.value, r5.value)]
  108. assert self.asm.mc.instrs == exp_instrs
  109. def test_reg_to_mem(self):
  110. self.asm.regalloc_mov(reg(5), stack(10))
  111. self.asm.regalloc_mov(reg(0), stack(2))
  112. exp_instrs = [MI("store", r5.value, SPP.value, get_spp_offset(10)),
  113. MI("store", r0.value, SPP.value, get_spp_offset(2))]
  114. assert self.asm.mc.instrs == exp_instrs
  115. def reg(i):
  116. return RegisterLocation(i)
  117. def stack(i):
  118. return StackLocation(i, get_spp_offset(i))
  119. def get_spp_offset(i):
  120. return i * 8 + 304