[pypy-dev] MetaVM design

Antonio Cuni anto.cuni at gmail.com
Sun May 21 12:38:40 CEST 2006

Hi Maciek, I all

as I announced in my previous e-mail here are some ideas on how to use 
metavm for genjs2.

I think the best way to use metavm for your purposes is to write a bunch 
of MicroInstruction classes designed for emitting source code. For 
example I guess that some rpython low level instruction need to be 
mapped to infix operator (such as int_add & co.) while others need to be 
mapped to some support function (such as int_abs). You could write 
something like this:

class InfixOperator(MicroInstruction):
   def __init__(self, operator):
     self.operator = operator

   def render(self, generator, op):
     generator.emit('(%s %s %s)' %\
               (op.args[0], self.operator, op.args[1])

class CallHelper(MicroInstruction):
   def __init__(self, helper):
     self.helper = helper

   def render(self, generator, op):
     arglist = ... # compute arglist from op.args
     generator.emit('%s(%s)' % (self.helper, arglist)

Then, in your opcodes.py:

opcodes = {
   'int_add': InfixOperator('+'),
   'int_abs': CallHelper('abs')

The difficult thing here is to design the Generator interface in a way 
that is suitable for emitting both asm code and source code. Maybe we 
try to separate the two worlds, in this way:


class Generator:
   def function_signature(self, graph): pass
   # put here all methods shared by both AsmGenerator and SourceGenerator

class StackBasedGenerator(Generator):
   def emit(self, instr, *args): pass
   def call(self, func_name): pass
   def load(self, v): pass
   def store(self, v): pass

class SourceCodeGenerator(Generator):
   def emit(self, expression): pass
   # and so on


class Function(Node, Generator):
   # some shared code

class StackBasedFunction(Node, StackBasedGenerator):

class SourceCodeFunction(Node, SourceCodeGenerator):


class CliFunction(StackBasedFucntion):


class JsFunction(SourceCodeFunction):

Once we have this design, each MicroInstruction subclass can decide 
whether to target the generic Generator interface or one of its 

ciao Anto

More information about the Pypy-dev mailing list