[pypy-svn] r12720 - in pypy/dist/pypy/translator/c: . test
arigo at codespeak.net
arigo at codespeak.net
Sat May 21 23:40:32 CEST 2005
Author: arigo
Date: Sat May 21 23:40:32 2005
New Revision: 12720
Modified:
pypy/dist/pypy/translator/c/database.py
pypy/dist/pypy/translator/c/node.py
pypy/dist/pypy/translator/c/test/test_database.py
Log:
Can produce static variable-sized structures too.
Modified: pypy/dist/pypy/translator/c/database.py
==============================================================================
--- pypy/dist/pypy/translator/c/database.py (original)
+++ pypy/dist/pypy/translator/c/database.py Sat May 21 23:40:32 2005
@@ -42,7 +42,7 @@
node = self.structdefnodes[key]
except KeyError:
if isinstance(T, Struct):
- node = StructDefNode(self, T)
+ node = StructDefNode(self, T, varlength)
elif isinstance(T, Array):
node = ArrayDefNode(self, T, varlength)
else:
Modified: pypy/dist/pypy/translator/c/node.py
==============================================================================
--- pypy/dist/pypy/translator/c/node.py (original)
+++ pypy/dist/pypy/translator/c/node.py Sat May 21 23:40:32 2005
@@ -26,15 +26,23 @@
class StructDefNode:
- def __init__(self, db, STRUCT):
+ def __init__(self, db, STRUCT, varlength=1):
self.STRUCT = STRUCT
- self.name = db.namespace.uniquename(STRUCT._name)
+ if varlength == 1:
+ basename = STRUCT._name
+ else:
+ basename = db.gettypedefnode(STRUCT).name
+ basename = '%s_len%d' % (basename, varlength)
+ self.name = db.namespace.uniquename(basename)
self.dependencies = {}
self.fields = []
self.prefix = somelettersfrom(STRUCT._name) + '_'
for name in STRUCT._names:
T = STRUCT._flds[name]
- typename = db.gettype(T, who_asks=self)
+ if name == STRUCT._arrayfld:
+ typename = db.gettype(T, varlength=varlength, who_asks=self)
+ else:
+ typename = db.gettype(T, who_asks=self)
self.fields.append((self.c_struct_field_name(name), typename))
def c_struct_field_name(self, name):
@@ -55,7 +63,7 @@
class ArrayDefNode:
- def __init__(self, db, ARRAY, varlength):
+ def __init__(self, db, ARRAY, varlength=1):
self.ARRAY = ARRAY
if varlength == 1:
basename = 'array'
@@ -128,6 +136,13 @@
for name in self.T._names:
yield getattr(self.obj, name)
+ def getlength(self):
+ if self.T._arrayfld is None:
+ return 1
+ else:
+ array = getattr(self.obj, self.T._arrayfld)
+ return len(array.items)
+
def initializationexpr(self, prefix=''):
yield '{'
if needs_refcount(self.T):
@@ -172,6 +187,7 @@
yield '\t%s' % expr
yield '}'
+
class FuncNode(ContainerNode):
def basename(self):
return self.obj._name
Modified: pypy/dist/pypy/translator/c/test/test_database.py
==============================================================================
--- pypy/dist/pypy/translator/c/test/test_database.py (original)
+++ pypy/dist/pypy/translator/c/test/test_database.py Sat May 21 23:40:32 2005
@@ -85,3 +85,27 @@
print '\n'.join(node.forward_declaration())
for node in db.globalcontainers():
print '\n'.join(node.implementation())
+
+def test_codegen_3():
+ db = LowLevelDatabase()
+ A = GcStruct('varsizedstuff', ('x', Signed), ('y', Array(('i', Signed))))
+ S = GcStruct('test', ('aptr', GcPtr(A)),
+ ('anitem', NonGcPtr(A.y.OF)),
+ ('anarray', NonGcPtr(A.y)))
+ a = malloc(A, 3)
+ a.x = 99
+ a.y[0].i = 100
+ a.y[1].i = 101
+ a.y[2].i = 102
+ s = malloc(S)
+ s.aptr = a
+ s.anitem = cast_flags(NonGcPtr(A.y.OF), a.y[1])
+ s.anarray = cast_flags(NonGcPtr(A.y), a.y)
+ db.get(s)
+ db.complete()
+ for node in db.structdeflist:
+ print '\n'.join(node.definition())
+ for node in db.globalcontainers():
+ print '\n'.join(node.forward_declaration())
+ for node in db.globalcontainers():
+ print '\n'.join(node.implementation())
More information about the Pypy-commit
mailing list