[pypy-svn] r74037 - in pypy/branch/blackhole-improvement/pypy/jit/codewriter: . test

arigo at codespeak.net arigo at codespeak.net
Sat Apr 24 18:13:34 CEST 2010


Author: arigo
Date: Sat Apr 24 18:13:32 2010
New Revision: 74037

Modified:
   pypy/branch/blackhole-improvement/pypy/jit/codewriter/assembler.py
   pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_assembler.py
Log:
Correctly cast incoming constants to either Signed, GCREF or Float.
Non-GC pointers end up being cast via Address to 'AddressAsInt',
a Symbolic Signed.


Modified: pypy/branch/blackhole-improvement/pypy/jit/codewriter/assembler.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/jit/codewriter/assembler.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/jit/codewriter/assembler.py	Sat Apr 24 18:13:32 2010
@@ -2,6 +2,7 @@
 from pypy.jit.codewriter.flatten import Register, Label, TLabel, KINDS
 from pypy.jit.codewriter.flatten import ListOfKind
 from pypy.objspace.flow.model import Constant
+from pypy.rpython.lltypesystem import lltype, llmemory
 
 
 class JitCode(AbstractValue):
@@ -55,19 +56,38 @@
             self.highest_regs[reg.kind] = reg.index
         self.code.append(chr(reg.index))
 
-    def emit_const(self, const, kind):
+    def emit_const(self, const, kind, allow_short=False):
         if const not in self.constants_dict:
+            value = const.value
+            TYPE = lltype.typeOf(value)
             if kind == 'int':
+                if isinstance(TYPE, lltype.Ptr):
+                    assert TYPE.TO._gckind == 'raw'
+                    value = llmemory.cast_ptr_to_adr(value)
+                    TYPE = llmemory.Address
+                if TYPE == llmemory.Address:
+                    value = llmemory.cast_adr_to_int(value)
+                else:
+                    value = lltype.cast_primitive(lltype.Signed, value)
+                    if allow_short and -128 <= value <= 127:  # xxx symbolic
+                        # emit the constant as a small integer
+                        self.code.append(chr(value & 0xFF))
+                        return True
                 constants = self.constants_i
             elif kind == 'ref':
+                value = lltype.cast_opaque_ptr(llmemory.GCREF, value)
                 constants = self.constants_r
             elif kind == 'float':
+                assert TYPE == lltype.Float
                 constants = self.constants_f
             else:
                 raise NotImplementedError(const)
-            constants.append(const.value)
+            constants.append(value)
             self.constants_dict[const] = 256 - len(constants)
+        # emit the constant normally, as one byte that is an index in the
+        # list of constants
         self.code.append(chr(self.constants_dict[const]))
+        return False
 
     def write_insn(self, insn):
         if isinstance(insn[0], Label):
@@ -83,11 +103,10 @@
                 argcodes.append(x.kind[0])
             elif isinstance(x, Constant):
                 kind = getkind(x.concretetype)
-                if kind == 'int' and -128 <= x.value <= 127:
-                    self.code.append(chr(x.value & 0xFF))
+                is_short = self.emit_const(x, kind, allow_short=True)
+                if is_short:
                     argcodes.append('c')
                 else:
-                    self.emit_const(x, kind)
                     argcodes.append(kind[0])
             elif isinstance(x, TLabel):
                 self.tlabel_positions.append((x.name, len(self.code)))

Modified: pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_assembler.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_assembler.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_assembler.py	Sat Apr 24 18:13:32 2010
@@ -4,7 +4,7 @@
 from pypy.jit.codewriter.flatten import ListOfKind
 from pypy.jit.metainterp.history import AbstractDescr
 from pypy.objspace.flow.model import Constant
-from pypy.rpython.lltypesystem import lltype
+from pypy.rpython.lltypesystem import lltype, llmemory
 
 
 def test_assemble_simple():
@@ -58,6 +58,32 @@
     assert assembler.insns == {'float_return/f': 0}
     assert jitcode.constants_f == [18.0, -4.0, 128.1]
 
+def test_assemble_cast_consts():
+    ssarepr = SSARepr("test")
+    S = lltype.GcStruct('S')
+    s = lltype.malloc(S)
+    F = lltype.FuncType([], lltype.Signed)
+    f = lltype.functionptr(F, 'f')
+    ssarepr.insns = [
+        ('int_return', Constant('X', lltype.Char)),
+        ('int_return', Constant(unichr(0x1234), lltype.UniChar)),
+        ('int_return', Constant(f, lltype.Ptr(F))),
+        ('ref_return', Constant(s, lltype.Ptr(S))),
+        ]
+    assembler = Assembler()
+    jitcode = assembler.assemble(ssarepr)
+    assert jitcode.code == ("\x00\x58"
+                            "\x01\xFF"
+                            "\x01\xFE"
+                            "\x02\xFF")
+    assert assembler.insns == {'int_return/c': 0,
+                               'int_return/i': 1,
+                               'ref_return/r': 2}
+    f_int = llmemory.cast_adr_to_int(llmemory.cast_ptr_to_adr(f))
+    assert jitcode.constants_i == [0x1234, f_int]
+    s_gcref = lltype.cast_opaque_ptr(llmemory.GCREF, s)
+    assert jitcode.constants_r == [s_gcref]
+
 def test_assemble_loop():
     ssarepr = SSARepr("test")
     i0, i1 = Register('int', 0x16), Register('int', 0x17)



More information about the Pypy-commit mailing list