[pypy-svn] r18343 - in pypy/dist/pypy/translator/c: . src test

cfbolz at codespeak.net cfbolz at codespeak.net
Mon Oct 10 18:03:09 CEST 2005


Author: cfbolz
Date: Mon Oct 10 18:03:07 2005
New Revision: 18343

Modified:
   pypy/dist/pypy/translator/c/funcgen.py
   pypy/dist/pypy/translator/c/src/address.h
   pypy/dist/pypy/translator/c/test/test_lladdresses.py
Log:
(hpk, cfbolz):
finished address operations, tests


Modified: pypy/dist/pypy/translator/c/funcgen.py
==============================================================================
--- pypy/dist/pypy/translator/c/funcgen.py	(original)
+++ pypy/dist/pypy/translator/c/funcgen.py	Mon Oct 10 18:03:07 2005
@@ -541,6 +541,23 @@
     def OP_KEEPALIVE(self, op, err): # xxx what should be the sematics consequences of this
         return "/* kept alive: %s */ ;" % self.expr(op.args[0], special_case_void=False)
 
+    #address operations
+    def OP_RAW_STORE(self, op, err):
+       addr = self.expr(op.args[0])
+       TYPE = op.args[1].value
+       offset = self.expr(op.args[2])
+       value = self.expr(op.args[3])
+       typename = self.db.gettype(TYPE).replace("@", "*") #XXX help! is this the way to do it?
+       return "*(((%(typename)s) %(addr)s ) + %(offset)s) = %(value)s;" % locals()
+
+    def OP_RAW_LOAD(self, op, err):
+        addr = self.expr(op.args[0])
+        TYPE = op.args[1].value
+        offset = self.expr(op.args[2])
+        result = self.expr(op.result)
+        typename = self.db.gettype(TYPE).replace("@", "*") #XXX see above
+        return "%(result)s = *(((%(typename)s) %(addr)s ) + %(offset)s);" % locals()
+
     def pyobj_incref(self, v):
         T = self.lltypemap(v)
         return self.pyobj_incref_expr(LOCALVAR % v.name, T)

Modified: pypy/dist/pypy/translator/c/src/address.h
==============================================================================
--- pypy/dist/pypy/translator/c/src/address.h	(original)
+++ pypy/dist/pypy/translator/c/src/address.h	Mon Oct 10 18:03:07 2005
@@ -16,3 +16,9 @@
 #define OP_ADR_LT(x,y,r,err)	  r = ((x) <  (y))
 #define OP_ADR_GE(x,y,r,err)	  r = ((x) >= (y))
 
+#define OP_RAW_MALLOC(size,r,err)                                           \
+    r = (void*) malloc(size);                                              \
+    if (r == NULL) FAIL_EXCEPTION(err, PyExc_MemoryError, "out of memory");\
+ 
+#define OP_RAW_FREE(x,r,err)        free(x);
+#define OP_RAW_MEMCOPY(x,y,size,r,err) memcpy(y,x,size);

Modified: pypy/dist/pypy/translator/c/test/test_lladdresses.py
==============================================================================
--- pypy/dist/pypy/translator/c/test/test_lladdresses.py	(original)
+++ pypy/dist/pypy/translator/c/test/test_lladdresses.py	Mon Oct 10 18:03:07 2005
@@ -1,7 +1,74 @@
-from pypy.rpython.memory import lladdress
+from pypy.rpython.memory.lladdress import *
+from pypy.annotation.model import SomeAddress, SomeChar
 from pypy.translator.c.test.test_genc import compile
 
 def test_null():
     def f():
-        return lladdress.NULL - lladdress.NULL
+        return NULL - NULL
     fc = compile(f, [])
+
+def test_convert_to_bool():
+    def f(x):
+        if x:
+            return bool(NULL)
+        else:
+            return bool(NULL + 1)
+    fc = compile(f, [int])
+    res = fc(1)
+    assert isinstance(res, bool) and not res
+    res = fc(0)
+    assert isinstance(res, bool) and res
+
+def test_memory_access():
+    def f(value):
+        addr = raw_malloc(16)
+        addr.signed[0] = value
+        return addr.signed[0]
+    fc = compile(f, [int])
+    res = fc(42)
+    assert res == 42
+    res = fc(1)
+    assert res == 1
+    
+def test_pointer_arithmetic():
+    def f(offset, char):
+        addr = raw_malloc(10000)
+        same_offset = (addr + 2 * offset - offset) - addr 
+        addr.char[offset] = char
+        result = (addr + same_offset).char[0]
+        raw_free(addr)
+        return result
+    fc = compile(f, [int, SomeChar()])
+    res = fc(10, "c")
+    assert res == "c"
+    res = fc(12, "x")
+    assert res == "x"
+
+def test_pointer_arithmetic_inplace():
+    def f(offset, char):
+        addr = raw_malloc(10000)
+        addr += offset
+        addr.char[-offset] = char
+        addr -= offset
+        return addr.char[0]
+    fc = compile(f, [int, SomeChar()])
+    res = fc(10, "c")
+    assert res == "c"
+
+def test_raw_memcopy():
+    def f():
+        addr = raw_malloc(100)
+        addr.signed[0] = 12
+        (addr + 10).signed[0] = 42
+        (addr + 20).char[0] = "a"
+        addr1 = raw_malloc(100)
+        raw_memcopy(addr, addr1, 100)
+        result = addr1.signed[0] == 12
+        result = result and (addr1 + 10).signed[0] == 42
+        result = result and (addr1 + 20).char[0] == "a"
+        return result
+    fc = compile(f, [])
+    res = fc()
+    assert res
+
+



More information about the Pypy-commit mailing list