[pypy-svn] r72226 - in pypy/branch/jit-newx86/pypy/jit/backend/newx86: . test
arigo at codespeak.net
arigo at codespeak.net
Sun Mar 14 18:40:07 CET 2010
Author: arigo
Date: Sun Mar 14 18:40:05 2010
New Revision: 72226
Modified:
pypy/branch/jit-newx86/pypy/jit/backend/newx86/codebuf.py
pypy/branch/jit-newx86/pypy/jit/backend/newx86/test/test_codebuf.py
Log:
Slowly learning the proper way of unit tests: make codebuf
independent of rx86, including tests.
Modified: pypy/branch/jit-newx86/pypy/jit/backend/newx86/codebuf.py
==============================================================================
--- pypy/branch/jit-newx86/pypy/jit/backend/newx86/codebuf.py (original)
+++ pypy/branch/jit-newx86/pypy/jit/backend/newx86/codebuf.py Sun Mar 14 18:40:05 2010
@@ -1,15 +1,14 @@
from pypy.rlib.objectmodel import we_are_translated
from pypy.rlib.rmmap import PTR, alloc, free
-from pypy.rpython.lltypesystem import rffi
-from pypy.jit.backend.newx86.rx86 import X86_32_CodeBuilder, X86_64_CodeBuilder
+from pypy.rpython.lltypesystem import lltype, rffi
class CodeBufAllocator(object):
alloc_count = 0
- def __init__(self, word):
+ def __init__(self, cb_class):
self.all_data_parts = [] # only if we are not translated
- self.cb_class = code_builder_cls[word]
+ self.cb_class = cb_class
def __del__(self):
if not we_are_translated():
@@ -22,22 +21,28 @@
if not we_are_translated():
CodeBufAllocator.alloc_count += 1
self.all_data_parts.append((data, map_size))
- return self.cb_class(data, map_size)
+ return self.cb_class(data, map_size, True)
+
+ def empty_code_buffer(self):
+ return self.cb_class(lltype.nullptr(PTR.TO), 0, True)
class CodeBufOverflow(Exception):
"Raised when a code buffer is full."
-class CodeBuilder(object):
+class AbstractCodeBuilder(object):
_mixin_ = True
- raise_on_overflow = False
- def __init__(self, data, map_size):
+ def __init__(self, data, map_size, raise_on_overflow):
self.data = data
self.write_ofs = map_size
+ self.raise_on_overflow = raise_on_overflow
def writechar(self, char):
+ """Writes a character at the *end* of buffer, and decrement the
+ current position.
+ """
ofs = self.write_ofs - 1
if ofs < 0:
self._overflow_detected()
@@ -50,25 +55,16 @@
_overflow_detected._dont_inline_ = True
def get_current_position(self):
+ """Return the current position. Note that this starts at the end."""
return rffi.ptradd(self.data, self.write_ofs)
- def extract_subbuffer(self, subsize):
+ def extract_subbuffer(self, subsize, raise_on_overflow):
subbuf = self.data
if self.write_ofs < subsize:
self._overflow_detected()
self.write_ofs -= subsize
self.data = rffi.ptradd(self.data, subsize)
- return self.__class__(subbuf, subsize)
+ return self.__class__(subbuf, subsize, raise_on_overflow)
def is_full(self):
return self.write_ofs == 0
-
-
-class CodeBuilder32(CodeBuilder, X86_32_CodeBuilder):
- pass
-
-class CodeBuilder64(CodeBuilder, X86_64_CodeBuilder):
- pass
-
-code_builder_cls = {4: CodeBuilder32,
- 8: CodeBuilder64}
Modified: pypy/branch/jit-newx86/pypy/jit/backend/newx86/test/test_codebuf.py
==============================================================================
--- pypy/branch/jit-newx86/pypy/jit/backend/newx86/test/test_codebuf.py (original)
+++ pypy/branch/jit-newx86/pypy/jit/backend/newx86/test/test_codebuf.py Sun Mar 14 18:40:05 2010
@@ -3,7 +3,7 @@
from pypy.rpython.lltypesystem import rffi
from pypy.jit.backend.newx86.codebuf import CodeBufOverflow
from pypy.jit.backend.newx86.codebuf import CodeBufAllocator
-from pypy.jit.backend.newx86.rx86 import R
+from pypy.jit.backend.newx86.codebuf import AbstractCodeBuilder
class TestCodeBuilder:
@@ -11,7 +11,7 @@
def setup_method(self, meth):
CodeBufAllocator.alloc_count = 0
- self.cballoc = CodeBufAllocator(self.WORD)
+ self.cballoc = CodeBufAllocator(AbstractCodeBuilder)
def teardown_method(self, meth):
del self.cballoc
@@ -29,59 +29,61 @@
def test_writing_from_end(self):
c = self.cballoc.new_code_buffer(4096)
- c.RET()
- c.NOP()
+ c.writechar('A')
+ c.writechar('B')
p1 = c.get_current_position()
- assert p1[0] == chr(0x90) # NOP
- assert p1[1] == chr(0xC3) # RET
- c.PUSH_r(R.ecx)
+ assert p1[0] == 'B'
+ assert p1[1] == 'A'
+ c.writechar('C')
p2 = c.get_current_position()
- assert p2[0] == chr(0x51) # PUSH ecx
- assert p2[1] == chr(0x90) # NOP
- assert p2[2] == chr(0xC3) # RET
+ assert p2[0] == 'C'
+ assert p2[1] == 'B'
+ assert p2[2] == 'A'
def test_overflowing(self):
c = self.cballoc.new_code_buffer(4096)
- c.raise_on_overflow = True
for i in range(4096):
- c.NOP()
- py.test.raises(CodeBufOverflow, c.NOP)
+ c.writechar('x')
+ py.test.raises(CodeBufOverflow, c.writechar, 'x')
def test_extract_subbuffer(self):
c1 = self.cballoc.new_code_buffer(4096)
- c1.NOP()
- c2 = c1.extract_subbuffer(3)
+ c1.writechar('x')
+ c2 = c1.extract_subbuffer(3, False)
p1 = c1.get_current_position()
- assert p1[0] == chr(0x90) # NOP
+ assert p1[0] == 'x'
p2 = c2.get_current_position()
- assert p2[4092] == chr(0x90) # NOP
+ assert p2[4092] == 'x'
assert not c2.is_full()
- c2.RET()
- c2.RET()
- c2.RET()
+ c2.writechar('1')
+ c2.writechar('2')
+ c2.writechar('3')
p2 = c2.get_current_position()
- assert p2[0] == chr(0xC3) # RET
- assert p2[1] == chr(0xC3) # RET
- assert p2[2] == chr(0xC3) # RET
- assert p2[4095] == chr(0x90) # NOP
+ assert p2[0] == '3'
+ assert p2[1] == '2'
+ assert p2[2] == '1'
+ assert p2[4095] == 'x'
assert c2.is_full()
def test_extract_subbuffer_overflow(self):
c = self.cballoc.new_code_buffer(4096)
- c.raise_on_overflow = True
for i in range(4092):
- c.NOP()
- c1 = c.extract_subbuffer(3)
- c1.RET()
- c1.RET()
- c1.RET()
- c2 = c.extract_subbuffer(1)
- c2.PUSH_r(R.edx)
+ c.writechar('x')
+ c1 = c.extract_subbuffer(3, False)
+ c1.writechar('1')
+ c1.writechar('2')
+ c1.writechar('3')
+ c2 = c.extract_subbuffer(1, False)
+ c2.writechar('4')
p1 = c1.get_current_position()
- assert p1[0] == chr(0xC3) # RET
- assert p1[1] == chr(0xC3) # RET
- assert p1[2] == chr(0xC3) # RET
- assert p1[3] == chr(0x52) # PUSH edx
- assert p1[4] == chr(0x90) # NOP
- assert p1[4095] == chr(0x90) # NOP
- py.test.raises(CodeBufOverflow, c.extract_subbuffer, 1)
+ assert p1[0] == '3'
+ assert p1[1] == '2'
+ assert p1[2] == '1'
+ assert p1[3] == '4'
+ assert p1[4] == 'x'
+ assert p1[4095] == 'x'
+ py.test.raises(CodeBufOverflow, c.extract_subbuffer, 1, False)
+
+ def test_empty_code_buffer(self):
+ c = self.cballoc.empty_code_buffer()
+ py.test.raises(CodeBufOverflow, c.writechar, 'x')
More information about the Pypy-commit
mailing list