[pypy-svn] r12756 - in pypy/dist/pypy/translator/c: . test
arigo at codespeak.net
arigo at codespeak.net
Tue May 24 01:29:39 CEST 2005
Author: arigo
Date: Tue May 24 01:29:39 2005
New Revision: 12756
Added:
pypy/dist/pypy/translator/c/g_include.h
- copied, changed from r12755, pypy/dist/pypy/translator/genc/g_include.h
pypy/dist/pypy/translator/c/g_module.h
- copied unchanged from r12755, pypy/dist/pypy/translator/genc/g_module.h
pypy/dist/pypy/translator/c/g_support.h
- copied unchanged from r12755, pypy/dist/pypy/translator/genc/g_support.h
pypy/dist/pypy/translator/c/g_trace.h
- copied unchanged from r12755, pypy/dist/pypy/translator/genc/g_trace.h
pypy/dist/pypy/translator/c/pyobj_include.h
- copied unchanged from r12755, pypy/dist/pypy/translator/genc/pyobj_include.h
Modified:
pypy/dist/pypy/translator/c/database.py
pypy/dist/pypy/translator/c/funcgen.py
pypy/dist/pypy/translator/c/node.py
pypy/dist/pypy/translator/c/pyobj.py
pypy/dist/pypy/translator/c/test/test_database.py
Log:
- Reasonable(?) support for PyObjects is integrated again.
- Generating complete modules again, too.
Modified: pypy/dist/pypy/translator/c/database.py
==============================================================================
--- pypy/dist/pypy/translator/c/database.py (original)
+++ pypy/dist/pypy/translator/c/database.py Tue May 24 01:29:39 2005
@@ -8,7 +8,7 @@
from pypy.translator.c.node import StructDefNode, ArrayDefNode
from pypy.translator.c.node import ContainerNodeClass
from pypy.translator.c.support import cdecl, CNameManager, ErrorValue
-#from pypy.translator.c.pyobj import PyObjMaker
+from pypy.translator.c.pyobj import PyObjMaker
# ____________________________________________________________
@@ -20,7 +20,7 @@
self.containernodes = {}
self.containerlist = []
self.namespace = CNameManager()
- #self.pyobjmaker = PyObjMaker(self.namespace)
+ self.pyobjmaker = PyObjMaker(self.namespace, self.get)
def gettypedefnode(self, T, varlength=1):
if varlength <= 1:
@@ -104,39 +104,20 @@
raise Exception("don't know about %r" % (obj,))
def complete(self):
- for node in self.containerlist:
+ i = 0
+ while True:
+ self.pyobjmaker.collect_initcode()
+ if i == len(self.containerlist):
+ break
+ node = self.containerlist[i]
for value in node.enum_dependencies():
if isinstance(typeOf(value), ContainerType):
self.getcontainernode(value)
else:
self.get(value)
+ i += 1
def globalcontainers(self):
for node in self.containerlist:
if node.globalcontainer:
yield node
-
- def write_all_declarations(self, f):
- print >> f
- print >> f, '/********************************************************/'
- print >> f, '/*** Structure definitions ***/'
- print >> f
- for node in self.structdeflist:
- for line in node.definition():
- print >> f, line
- print >> f
- print >> f, '/********************************************************/'
- print >> f, '/*** Forward declarations ***/'
- print >> f
- for node in self.globalcontainers():
- for line in node.forward_declaration():
- print >> f, line
-
- def write_all_implementations(self, f):
- print >> f
- print >> f, '/********************************************************/'
- print >> f, '/*** Implementations ***/'
- for node in self.globalcontainers():
- print >> f
- for line in node.implementation():
- print >> f, line
Modified: pypy/dist/pypy/translator/c/funcgen.py
==============================================================================
--- pypy/dist/pypy/translator/c/funcgen.py (original)
+++ pypy/dist/pypy/translator/c/funcgen.py Tue May 24 01:29:39 2005
@@ -69,7 +69,7 @@
if isinstance(v, Variable):
return v.name
elif isinstance(v, Constant):
- return self.getvalue(v.value)
+ return self.getvalue(llvalue_from_constant(v))
else:
raise TypeError, "expr(%r)" % (v,)
Copied: pypy/dist/pypy/translator/c/g_include.h (from r12755, pypy/dist/pypy/translator/genc/g_include.h)
==============================================================================
--- pypy/dist/pypy/translator/genc/g_include.h (original)
+++ pypy/dist/pypy/translator/c/g_include.h Tue May 24 01:29:39 2005
@@ -14,10 +14,4 @@
#include "g_support.h"
#include "g_module.h"
-#include "heapobject_include.h"
-#include "int_include.h"
-#include "list_include.h"
-#include "none_include.h"
#include "pyobj_include.h"
-#include "tuple_include.h"
-#include "ll_include.h"
Modified: pypy/dist/pypy/translator/c/node.py
==============================================================================
--- pypy/dist/pypy/translator/c/node.py (original)
+++ pypy/dist/pypy/translator/c/node.py Tue May 24 01:29:39 2005
@@ -256,9 +256,15 @@
self.db = db
self.T = T
self.obj = obj
- self.name = db.namespace.uniquename('g_' + WORKING_ON_IT)
+ self.name = db.pyobjmaker.computenameof(obj.value)
self.ptrname = self.name
+ def enum_dependencies(self):
+ return []
+
+ def implementation(self):
+ return []
+
ContainerNodeClass = {
Struct: StructNode,
Modified: pypy/dist/pypy/translator/c/pyobj.py
==============================================================================
--- pypy/dist/pypy/translator/c/pyobj.py (original)
+++ pypy/dist/pypy/translator/c/pyobj.py Tue May 24 01:29:39 2005
@@ -6,6 +6,7 @@
from pypy.translator.gensupp import builtin_base, NameManager
from pypy.rpython.rarithmetic import r_int, r_uint
+from pypy.rpython.lltypes import pyobject
# XXX maybe this can be done more elegantly:
# needed to convince should_translate_attr
@@ -21,60 +22,48 @@
reconstruct them.
"""
- def __init__(self, namespace):
+ def __init__(self, namespace, getvalue):
self.namespace = namespace
- self.cnames = {Constant(None).key: 'Py_None',
- Constant(False).key: 'Py_False',
- Constant(True).key: 'Py_True',
- }
+ self.getvalue = getvalue
self.initcode = [ # list of lines for the module's initxxx()
'import new, types, sys',
- 'Py_None = None',
- 'Py_False = False',
- 'Py_True = True',
]
- self.globaldecl = []
self.latercode = [] # list of generators generating extra lines
# for later in initxxx() -- for recursive
# objects
- self.globalobjects = []
self.debugstack = () # linked list of nested nameof()
def nameof(self, obj, debug=None):
- key = Constant(obj).key
+ if debug:
+ stackentry = debug, obj
+ else:
+ stackentry = obj
+ self.debugstack = (self.debugstack, stackentry)
try:
- return self.cnames[key]
- except KeyError:
- if debug:
- stackentry = debug, obj
- else:
- stackentry = obj
- self.debugstack = (self.debugstack, stackentry)
- obj_builtin_base = builtin_base(obj)
- if obj_builtin_base in (object, int, long) and type(obj) is not obj_builtin_base:
- # assume it's a user defined thingy
- name = self.nameof_instance(obj)
- else:
- for cls in type(obj).__mro__:
- meth = getattr(self,
- 'nameof_' + cls.__name__.replace(' ', ''),
- None)
- if meth:
- break
- else:
- raise Exception, "nameof(%r)" % (obj,)
- name = meth(obj)
+ return self.getvalue(pyobject(obj))
+ finally:
self.debugstack, x = self.debugstack
assert x is stackentry
- self.cnames[key] = name
- return name
+
+ def computenameof(self, obj):
+ obj_builtin_base = builtin_base(obj)
+ if obj_builtin_base in (object, int, long) and type(obj) is not obj_builtin_base:
+ # assume it's a user defined thingy
+ return self.nameof_instance(obj)
+ else:
+ for cls in type(obj).__mro__:
+ meth = getattr(self,
+ 'nameof_' + cls.__name__.replace(' ', ''),
+ None)
+ if meth:
+ break
+ else:
+ raise Exception, "nameof(%r)" % (obj,)
+ return meth(obj)
def uniquename(self, basename):
- name = self.namespace.uniquename(basename)
- self.globalobjects.append(name)
- self.globaldecl.append('static PyObject *%s;' % (name,))
- return name
+ return self.namespace.uniquename(basename)
def initcode_python(self, name, pyexpr):
# generate init code that will evaluate the given Python expression
@@ -420,36 +409,26 @@
def later(self, gen):
self.latercode.append((gen, self.debugstack))
- def collect_globals(self, genc):
+ def collect_initcode(self):
while self.latercode:
gen, self.debugstack = self.latercode.pop()
#self.initcode.extend(gen) -- eats TypeError! bad CPython!
for line in gen:
self.initcode.append(line)
self.debugstack = ()
- if genc.f2 is not None:
- for line in self.initcode:
- print >> genc.f2, line
- del self.initcode[:]
- result = self.globaldecl
- self.globaldecl = []
- return result
-
- def getfrozenbytecode(self, genc):
- if genc.f2 is not None:
- genc.f2.seek(0)
- self.initcode.insert(0, genc.f2.read())
+
+ def getfrozenbytecode(self):
self.initcode.append('')
source = '\n'.join(self.initcode)
del self.initcode[:]
- co = compile(source, genc.modname, 'exec')
- del source
+ co = compile(source, '<initcode>', 'exec')
+ originalsource = source
small = zlib.compress(marshal.dumps(co))
source = """if 1:
import zlib, marshal
exec marshal.loads(zlib.decompress(%r))""" % small
# Python 2.2 SyntaxError without newline: Bug #501622
source += '\n'
- co = compile(source, genc.modname, 'exec')
+ co = compile(source, '<initcode>', 'exec')
del source
- return marshal.dumps(co)
+ return marshal.dumps(co), originalsource
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 Tue May 24 01:29:39 2005
@@ -7,6 +7,20 @@
from pypy.rpython.lltypes import Struct, Array, malloc
+def dump_on_stdout(database):
+ print '/*********************************/'
+ for node in database.structdeflist:
+ for line in node.definition():
+ print line
+ print
+ for node in database.globalcontainers():
+ for line in node.forward_declaration():
+ print line
+ for node in database.globalcontainers():
+ print
+ for line in node.implementation():
+ print line
+
def test_primitive():
db = LowLevelDatabase()
@@ -62,8 +76,7 @@
s.p = cast_flags(NonGcPtr(U), s.u)
db.get(s)
db.complete()
- db.write_all_declarations(sys.stdout)
- db.write_all_implementations(sys.stdout)
+ dump_on_stdout(db)
def test_codegen_2():
db = LowLevelDatabase()
@@ -77,8 +90,7 @@
s.aptr = a
db.get(s)
db.complete()
- db.write_all_declarations(sys.stdout)
- db.write_all_implementations(sys.stdout)
+ dump_on_stdout(db)
def test_codegen_3():
db = LowLevelDatabase()
@@ -97,8 +109,7 @@
s.anarray = cast_flags(NonGcPtr(A.y), a.y)
db.get(s)
db.complete()
- db.write_all_declarations(sys.stdout)
- db.write_all_implementations(sys.stdout)
+ dump_on_stdout(db)
def test_func_simple():
# -------------------- flowgraph building --------------------
@@ -123,8 +134,7 @@
db = LowLevelDatabase()
db.get(f)
db.complete()
- db.write_all_declarations(sys.stdout)
- db.write_all_implementations(sys.stdout)
+ dump_on_stdout(db)
S = GcStruct('testing', ('fptr', NonGcPtr(F)))
s = malloc(S)
@@ -132,10 +142,9 @@
db = LowLevelDatabase()
db.get(s)
db.complete()
- db.write_all_declarations(sys.stdout)
- db.write_all_implementations(sys.stdout)
+ dump_on_stdout(db)
-def WORKING_ON_test_untyped_func():
+def test_untyped_func():
def f(x):
return x+1
t = Translator(f)
@@ -146,8 +155,7 @@
db = LowLevelDatabase()
db.get(f)
db.complete()
- db.write_all_declarations(sys.stdout)
- db.write_all_implementations(sys.stdout)
+ dump_on_stdout(db)
S = GcStruct('testing', ('fptr', NonGcPtr(F)))
s = malloc(S)
@@ -155,5 +163,4 @@
db = LowLevelDatabase()
db.get(s)
db.complete()
- db.write_all_declarations(sys.stdout)
- db.write_all_implementations(sys.stdout)
+ dump_on_stdout(db)
More information about the Pypy-commit
mailing list