[pypy-svn] r12761 - in pypy/dist/pypy/translator/c: . test
arigo at codespeak.net
arigo at codespeak.net
Tue May 24 12:01:08 CEST 2005
Author: arigo
Date: Tue May 24 12:01:07 2005
New Revision: 12761
Added:
pypy/dist/pypy/translator/c/genc.py (contents, props changed)
pypy/dist/pypy/translator/c/test/test_genc.py (contents, props changed)
Log:
Generation of a complete CPython extension module again
(forgot to check in yesterday).
Added: pypy/dist/pypy/translator/c/genc.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/c/genc.py Tue May 24 12:01:07 2005
@@ -0,0 +1,127 @@
+import autopath
+import os
+from pypy.translator.c.node import PyObjectNode
+
+
+def gen_source(database, modulename, targetdir):
+ filename = os.path.join(targetdir, modulename + '.c')
+ f = open(filename, 'w')
+
+ #
+ # Header
+ #
+ print >> f, '#include "g_include.h"'
+
+ #
+ # All declarations
+ #
+ print >> f
+ print >> f, '/***********************************************************/'
+ print >> f, '/*** Structure definitions ***/'
+ print >> f
+ for node in database.structdeflist:
+ for line in node.definition():
+ print >> f, line
+ print >> f
+ print >> f, '/***********************************************************/'
+ print >> f, '/*** Forward declarations ***/'
+ print >> f
+ for node in database.globalcontainers():
+ for line in node.forward_declaration():
+ print >> f, line
+
+ #
+ # Implementation of functions and global structures and arrays
+ #
+ print >> f
+ print >> f, '/***********************************************************/'
+ print >> f, '/*** Implementations ***/'
+ for node in database.globalcontainers():
+ print >> f
+ for line in node.implementation():
+ print >> f, line
+
+ #
+ # PyObject support (strange) code
+ #
+ print >> f
+ print >> f, '/***********************************************************/'
+ print >> f, '/*** Table of global PyObjects ***/'
+ print >> f
+ print >> f, 'static globalobjectdef_t globalobjectdefs[] = {'
+ for node in database.globalcontainers():
+ if isinstance(node, PyObjectNode):
+ name = node.name
+ if not name.startswith('gfunc_'):
+ print >> f, '\t{&%s, "%s"},' % (name, name)
+ print >> f, '\t{ NULL }\t/* Sentinel */'
+ print >> f, '};'
+ print >> f
+ print >> f, '/***********************************************************/'
+ print >> f, '/*** Table of functions ***/'
+ print >> f
+ print >> f, 'static globalfunctiondef_t globalfunctiondefs[] = {'
+ print >> f, '\t/* XXX */'
+ print >> f, '\t{ NULL }\t/* Sentinel */'
+ print >> f, '};'
+ print >> f
+ print >> f, '/***********************************************************/'
+ print >> f, '/*** Frozen Python bytecode: the initialization code ***/'
+ print >> f
+ print >> f, 'static char *frozen_initcode[] = {"\\'
+ bytecode, originalsource = database.pyobjmaker.getfrozenbytecode()
+ g = open(os.path.join(targetdir, 'frozen.py'), 'w')
+ g.write(originalsource)
+ g.close()
+ def char_repr(c):
+ if c in '\\"': return '\\' + c
+ if ' ' <= c < '\x7F': return c
+ return '\\%03o' % ord(c)
+ for i in range(0, len(bytecode), 32):
+ print >> f, ''.join([char_repr(c) for c in bytecode[i:i+32]])+'\\'
+ if (i+32) % 1024 == 0:
+ print >> f, '", "\\'
+ print >> f, '"};'
+ print >> f, "#define FROZEN_INITCODE_SIZE %d" % len(bytecode)
+ print >> f
+
+ #
+ # Module initialization function
+ #
+ print >> f, '/***********************************************************/'
+ print >> f, '/*** Module initialization function ***/'
+ print >> f
+ print >> f, 'MODULE_INITFUNC(%s)' % modulename
+ print >> f, '{'
+ print >> f, '\tSETUP_MODULE(%s)' % modulename
+ #print >> f, '\tPyModule_AddObject(m, "%(entrypointname)s", %(entrypoint)s);'
+ print >> f, '}'
+ f.close()
+
+ #
+ # Generate a setup.py while we're at it
+ #
+ pypy_include_dir = autopath.this_dir
+ f = open(os.path.join(targetdir, 'setup.py'), 'w')
+ f.write(SETUP_PY % locals())
+ f.close()
+
+
+SETUP_PY = '''
+from distutils.core import setup
+from distutils.extension import Extension
+from distutils.ccompiler import get_default_compiler
+
+PYPY_INCLUDE_DIR = %(pypy_include_dir)r
+
+extra_compile_args = []
+if get_default_compiler() == "unix":
+ extra_compile_args.extend(["-Wno-unused-label",
+ "-Wno-unused-variable"])
+
+setup(name="%(modulename)s",
+ ext_modules = [Extension(name = "%(modulename)s",
+ sources = ["%(modulename)s.c"],
+ extra_compile_args = extra_compile_args,
+ include_dirs = [PYPY_INCLUDE_DIR])])
+'''
Added: pypy/dist/pypy/translator/c/test/test_genc.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/c/test/test_genc.py Tue May 24 12:01:07 2005
@@ -0,0 +1,35 @@
+import autopath, sys, os
+from pypy.rpython.lltype import *
+from pypy.translator.translator import Translator
+from pypy.translator.c.database import LowLevelDatabase
+from pypy.translator.c.genc import gen_source
+from pypy.objspace.flow.model import Constant, Variable, SpaceOperation
+from pypy.objspace.flow.model import Block, Link, FunctionGraph
+from pypy.tool.udir import udir
+from pypy.translator.tool.buildpyxmodule import make_module_from_c
+from pypy.translator.gensupp import uniquemodulename
+
+
+def compile_db(db):
+ modulename = uniquemodulename('testing')
+ targetdir = udir.join(modulename).ensure(dir=1)
+ gen_source(db, modulename, str(targetdir))
+ make_module_from_c(targetdir.join(modulename+'.c'),
+ include_dirs = [os.path.dirname(autopath.this_dir)])
+
+
+def test_untyped_func():
+ def f(x):
+ return x+1
+ t = Translator(f)
+ graph = t.getflowgraph()
+
+ F = FuncType([GcPtr(PyObject)], GcPtr(PyObject))
+ S = GcStruct('testing', ('fptr', NonGcPtr(F)))
+ f = functionptr(F, "f", graph=graph)
+ s = malloc(S)
+ s.fptr = f
+ db = LowLevelDatabase()
+ db.get(s)
+ db.complete()
+ compile_db(db)
More information about the Pypy-commit
mailing list