[Python-Dev] Python Specializing Compiler

Jeff Epler jepler@mail.inetnebr.com
Fri, 22 Jun 2001 07:18:46 -0500


On Fri, Jun 22, 2001 at 01:00:34PM +0200, Armin Rigo wrote:
> Hello everybody,
> 
> I implemented a proof-of-concept version of a "Python compiler". It is not
> really a compiler. I know perfectly well that you cannot compile Python
> into something more efficient than a bunch of calls to PyObject_xxx. 
> Still, this very preliminary version runs the following function twice as
> fast as the python interpreter:

I've implemented something similar, but didn't get such favorable results
yet.  I was concentrating more on implementing a type system and code to
infer type information, and had spent less time on the code generation.
(For instance, my system could determine the result type of subscript-type
operations, and infer the types of lists over a loop, as in:
	l1 = [1,3.14159, "tubers"]
	l2 = [0]*3
	for j in range(3):
		l2[j] = l1[j-3]
	# Type of l2 is HeterogeneousListType([IntType, FloatType,
	# StringType])
You could make it run forever on a pathological case like
	l = []
	while 1:
		l = [l]
with the fix being to "give up" after some number of iterations, and
declare the unstable object (l) as having type "ObjectType", which is
always correct but overbroad.

My code is still available, but my motivation has faded somewhat and I
haven't had the time to work on it recently in any case.  It uses "GNU
Lightning" for JIT code generation, rather than using an external
compiler.  (If I were to approach the problem again, I might discard the
JIT code generator in favor of starting over again with the python2c
compiler and adding type information)

It can make judgements about sequences of calls, such as
	def f():
		return g()
when g is given the "solid" attribute, and the compilation process begins
by hoisting the former global load of g into a constant load, something
like
	def make_f():
		local_g = g
		def f():
			return local_g()
		return f
	f = make_f()

What are you using to generate code?  How would you compare the
sophistication of your type inference system to the one I've outlined
above?

Jeff