[pypy-svn] r27486 - pypy/dist/pypy/translator/cli

fijal at codespeak.net fijal at codespeak.net
Fri May 19 22:45:00 CEST 2006


Author: fijal
Date: Fri May 19 22:44:24 2006
New Revision: 27486

Modified:
   pypy/dist/pypy/translator/cli/class_.py
   pypy/dist/pypy/translator/cli/database.py
   pypy/dist/pypy/translator/cli/gencli.py
   pypy/dist/pypy/translator/cli/ilgenerator.py
   pypy/dist/pypy/translator/cli/metavm.py
Log:
Generalisation of gencli to support more ootypesystem based backends (like genjs or squeak)


Modified: pypy/dist/pypy/translator/cli/class_.py
==============================================================================
--- pypy/dist/pypy/translator/cli/class_.py	(original)
+++ pypy/dist/pypy/translator/cli/class_.py	Fri May 19 22:44:24 2006
@@ -4,7 +4,7 @@
 class Class(Node):
     def __init__(self, db, classdef):
         self.db = db
-        self.cts = CTS(db)
+        self.cts = db.type_system_class(db)
         self.classdef = classdef
         self.namespace, self.name = self.cts.split_class_name(classdef._name)
 
@@ -52,9 +52,9 @@
         self._ctor()
 
         # lazy import to avoid circular dependencies
-        import pypy.translator.cli.function as function
+        #import pypy.translator.cli.function as function
         for m_name, m_meth in self.classdef._methods.iteritems():
-            f = function.Function(self.db, m_meth.graph, m_name, is_method = True)
+            f = self.db.function_class(self.db, m_meth.graph, m_name, is_method = True)
             f.render(ilasm)
 
         ilasm.end_class()

Modified: pypy/dist/pypy/translator/cli/database.py
==============================================================================
--- pypy/dist/pypy/translator/cli/database.py	(original)
+++ pypy/dist/pypy/translator/cli/database.py	Fri May 19 22:44:24 2006
@@ -3,6 +3,7 @@
 from pypy.translator.cli.class_ import Class
 from pypy.translator.cli.record import Record
 from pypy.rpython.ootypesystem import ootype
+from pypy.translator.cli.opcodes import opcodes
 
 try:
     set
@@ -13,9 +14,12 @@
 CONST_CLASS = 'Constants'
 
 class LowLevelDatabase(object):
-    def __init__(self):
+    def __init__(self, type_system_class = CTS, opcode_dict = opcodes, function_class = Function):
         self._pending_nodes = set()
+        self.opcode_dict = opcode_dict
         self._rendered_nodes = set()
+        self.function_class = function_class
+        self.type_system_class = type_system_class
         self.classes = {} # classdef --> class_name
         self.functions = {} # graph --> function_name
         self.methods = {} # graph --> method_name
@@ -23,7 +27,7 @@
         self.const_names = set()
 
     def pending_function(self, graph):
-        self.pending_node(Function(self, graph))
+        self.pending_node(self.function_class(self, graph))
 
     def pending_class(self, classdef):
         self.pending_node(Class(self, classdef))
@@ -64,6 +68,8 @@
         return '%s.%s::%s' % (CONST_NAMESPACE, CONST_CLASS, name)
 
     def gen_constants(self, ilasm):
+        if not ilasm . show_const ():
+            return
         ilasm.begin_namespace(CONST_NAMESPACE)
         ilasm.begin_class(CONST_CLASS)
 
@@ -77,9 +83,9 @@
         for const, name in self.consts.iteritems():
             const.init(ilasm)
             type_ = const.get_type()
-            ilasm.opcode('stsfld %s %s.%s::%s' % (type_, CONST_NAMESPACE, CONST_CLASS, name))
+            ilasm.set_static_field ( type_, CONST_NAMESPACE, CONST_CLASS, name )
 
-        ilasm.opcode('ret')
+        ilasm.ret()
         ilasm.end_function()
 
         ilasm.end_class()

Modified: pypy/dist/pypy/translator/cli/gencli.py
==============================================================================
--- pypy/dist/pypy/translator/cli/gencli.py	(original)
+++ pypy/dist/pypy/translator/cli/gencli.py	Fri May 19 22:44:24 2006
@@ -7,7 +7,9 @@
 from pypy.translator.cli.class_ import Class
 from pypy.translator.cli.option import getoption
 from pypy.translator.cli.database import LowLevelDatabase
-
+from pypy.translator.cli.cts import CTS
+from pypy.translator.cli.opcodes import opcodes
+from pypy.translator.squeak.node import LoopFinder
 
 class Tee(object):
     def __init__(self, *args):
@@ -23,11 +25,11 @@
                 outfile.close()
 
 class GenCli(object):
-    def __init__(self, tmpdir, translator, entrypoint = None):
+    def __init__(self, tmpdir, translator, entrypoint = None , type_system_class = CTS , opcode_dict = opcodes ):
         self.tmpdir = tmpdir
         self.translator = translator
         self.entrypoint = entrypoint
-        self.db = LowLevelDatabase()
+        self.db = LowLevelDatabase( type_system_class = type_system_class , opcode_dict = opcode_dict )
 
         if entrypoint is None:
             self.assembly_name = self.translator.graphs[0].name
@@ -36,12 +38,12 @@
 
         self.tmpfile = tmpdir.join(self.assembly_name + '.il')
 
-    def generate_source(self):
+    def generate_source(self , asm_class = IlasmGenerator ):
         out = self.tmpfile.open('w')
         if getoption('stdout'):
             out = Tee(sys.stdout, out)
 
-        self.ilasm = IlasmGenerator(out, self.assembly_name)
+        self.ilasm = asm_class(out, self.assembly_name )
         
         # TODO: instance methods that are also called as unbound
         # methods are rendered twice, once within the class and once

Modified: pypy/dist/pypy/translator/cli/ilgenerator.py
==============================================================================
--- pypy/dist/pypy/translator/cli/ilgenerator.py	(original)
+++ pypy/dist/pypy/translator/cli/ilgenerator.py	Fri May 19 22:44:24 2006
@@ -1,3 +1,6 @@
+from pypy.rpython.lltypesystem.lltype import Signed, Unsigned, Void, Bool, Float
+from pypy.rpython.lltypesystem.lltype import SignedLongLong, UnsignedLongLong
+
 class CodeGenerator(object):
     def __init__(self, out, indentstep = 4, startblock = '{', endblock = '}'):
         self._out = out
@@ -40,6 +43,9 @@
         self.code.writeline('.assembly extern pypylib {}')
         self.code.writeline('.assembly %s {}' % name)
 
+    def show_const(self):
+        return True
+
     def close(self):
         self.out.close()
 
@@ -134,6 +140,60 @@
     def new(self, class_):
         self.code.writeline('newobj ' + class_)
 
+    def set_field(self, field_data ):
+        self.opcode('stfld %s %s::%s' % field_data )
+
+    def get_field(self,field_data):
+        self.opcode('ldfld %s %s::%s' % field_data )
+
+    def load_set_field(self, cts_type, name):
+        self.opcode('ldsfld %s %s' % (cts_type, name))
+    
+    def throw(self):
+        self.opcode('throw')
+    
+    def pop(self):
+        self.opcode('pop')
+    
+    def ret(self):
+        self.opcode('ret')
+
+    def castclass(self,cts_type):
+        self.opcode('castclass', cts_type)
+    
+    def load_self(self):
+        self.opcode('ldarg.0')
+    
+    def load_arg(self,v):
+        self.opcode('ldarg', repr(v.name))
+    
+    def load_local(self,v):
+        self.opcode('ldloc', repr(v.name))
+
+    def load_const(self,type_,v):
+        if type_ is Void:
+            pass
+        elif type_ is Bool:
+            self.opcode('ldc.i4', str(int(v)))
+        elif type_ is Float:
+            self.opcode('ldc.r8', repr(v))
+        elif type_ in (Signed, Unsigned):
+            self.opcode('ldc.i4', str(v))
+        elif type_ in (SignedLongLong, UnsignedLongLong):
+            self.opcode('ldc.i8', str(v))
+
+    def store_local ( self , v ):
+        self.opcode('stloc', repr(v.name))
+
+    def set_static_field ( self, type_, CONST_NAMESPACE, CONST_CLASS, name ):
+        self.opcode('stsfld %s %s.%s::%s' % (type_, CONST_NAMESPACE, CONST_CLASS, name))
+
+    def emit ( self , opcode , *args ):
+        self.opcode ( opcode , *args )
+
+    def begin_link ( self ):
+        pass
+
     def opcode(self, opcode, *args):
         self.code.write(opcode + ' ')
         self.code.writeline(' '.join(map(str, args)))

Modified: pypy/dist/pypy/translator/cli/metavm.py
==============================================================================
--- pypy/dist/pypy/translator/cli/metavm.py	(original)
+++ pypy/dist/pypy/translator/cli/metavm.py	Fri May 19 22:44:24 2006
@@ -106,7 +106,6 @@
         generator.call_signature('object [pypylib]pypy.runtime.Utils::RuntimeNew(class [mscorlib]System.Type)')
         generator.cast_to(op.result.concretetype)
 
-
 PushAllArgs = _PushAllArgs()
 StoreResult = _StoreResult()
 Call = _Call()



More information about the Pypy-commit mailing list