[pypy-svn] r25573 - in pypy/dist/pypy/translator/cli: . test

antocuni at codespeak.net antocuni at codespeak.net
Sat Apr 8 19:07:56 CEST 2006


Author: antocuni
Date: Sat Apr  8 19:07:41 2006
New Revision: 25573

Modified:
   pypy/dist/pypy/translator/cli/class_.py
   pypy/dist/pypy/translator/cli/cts.py
   pypy/dist/pypy/translator/cli/database.py
   pypy/dist/pypy/translator/cli/function.py
   pypy/dist/pypy/translator/cli/gencli.py
   pypy/dist/pypy/translator/cli/ilgenerator.py
   pypy/dist/pypy/translator/cli/test/compile.py
Log:
Added support for the 'meta' field in the Object class.

The constants used for setting the 'meta' field just after the
creation of the object are instantiated at startup time and stored as
static fields in the 'pypy.runtime.Constant' class.



Modified: pypy/dist/pypy/translator/cli/class_.py
==============================================================================
--- pypy/dist/pypy/translator/cli/class_.py	(original)
+++ pypy/dist/pypy/translator/cli/class_.py	Sat Apr  8 19:07:41 2006
@@ -8,19 +8,25 @@
         self.cts = CTS(db)
         self.classdef = classdef
         self.namespace, self.name = self.cts.split_class_name(classdef._name)
-        #0/0
+
+    def is_root(classdef):
+        return classdef._superclass is None
+    is_root = staticmethod(is_root)
 
     def get_name(self):
         return self.name
 
     def get_base_class(self):
         base_class = self.classdef._superclass
-        if base_class is None:
+        if self.is_root(base_class):
             return '[mscorlib]System.Object'
         else:
             return base_class._name
 
     def render(self, ilasm):
+        if self.is_root(self.classdef):
+            return
+        
         self.ilasm = ilasm
         if self.namespace:
             ilasm.begin_namespace(self.namespace)

Modified: pypy/dist/pypy/translator/cli/cts.py
==============================================================================
--- pypy/dist/pypy/translator/cli/cts.py	(original)
+++ pypy/dist/pypy/translator/cli/cts.py	Sat Apr  8 19:07:41 2006
@@ -23,7 +23,7 @@
     UnsignedLongLong: 'unsigned int64',
     Bool: 'bool',
     Float: 'float64',
-    Class: '[mscorlib]System.Type', # TODO: check this
+    Class: 'class [mscorlib]System.Type', # TODO: check this
     }
 
 _pyexception_to_cts = {

Modified: pypy/dist/pypy/translator/cli/database.py
==============================================================================
--- pypy/dist/pypy/translator/cli/database.py	(original)
+++ pypy/dist/pypy/translator/cli/database.py	Sat Apr  8 19:07:41 2006
@@ -1,8 +1,12 @@
+from pypy.rpython.ootypesystem import ootype
+
 try:
     set
 except NameError:
     from sets import Set as set
 
+CONST_NAMESPACE = 'pypy.runtime'
+CONST_CLASS = 'Constants'
 
 class LowLevelDatabase(object):
     def __init__(self):
@@ -18,7 +22,66 @@
     def function_name(self, graph):
         return self.functions.get(graph, None)
 
-    def record_const(self, const):
-        name = '__XXX__CONST__NAME__XXX__' # TODO
+    def record_const(self, value):
+        const = AbstractConst.make(value)
+        name = const.get_name(len(self.consts))
         self.consts[const] = name
-        return name
+        return '%s.%s::%s' % (CONST_NAMESPACE, CONST_CLASS, name)
+
+    def gen_constants(self, ilasm):
+        ilasm.begin_namespace(CONST_NAMESPACE)
+        ilasm.begin_class(CONST_CLASS)
+
+        # render field definitions
+        for const, name in self.consts.iteritems():
+            ilasm.field(name, const.get_type(), static=True)
+
+        # initialize fields
+        ilasm.begin_function('.cctor', [], 'void', False, 'static',
+                             'specialname', 'rtspecialname', 'default')
+        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.opcode('ret')
+        ilasm.end_function()
+
+        ilasm.end_class()
+        ilasm.end_namespace()
+
+
+class AbstractConst(object):
+    def make(const):
+        if isinstance(const, ootype._view):
+            const = const._inst
+        if isinstance(const, ootype._instance):
+            return InstanceConst(const)
+        else:
+            assert False, 'Unknown constant: %s' % const
+    make = staticmethod(make)
+    
+    def get_name(self, n):
+        pass
+
+    def get_type(self):
+        pass
+
+    def init(self, ilasm):
+        pass
+
+class InstanceConst(AbstractConst):
+    def __init__(self, obj):
+        self.obj = obj
+
+    def get_name(self, n):
+        name = self.obj._TYPE._name.replace('.', '_')
+        return '%s_%d' % (name, n)
+
+    def get_type(self):
+        return 'class %s' % self.obj._TYPE._name
+
+    def init(self, ilasm):
+        classdef = self.obj._TYPE        
+        ilasm.new('instance void class %s::.ctor()' % classdef._name)
+        # TODO: initialize fields

Modified: pypy/dist/pypy/translator/cli/function.py
==============================================================================
--- pypy/dist/pypy/translator/cli/function.py	(original)
+++ pypy/dist/pypy/translator/cli/function.py	Sat Apr  8 19:07:41 2006
@@ -209,8 +209,6 @@
             if isinstance(lltype, ootype.Instance):
                 self.db.classes.add(lltype)
 
-
-
     def _render_op(self, op):
         instr_list = opcodes.get(op.opname, None)
         if instr_list is not None:
@@ -224,8 +222,11 @@
                 assert False, 'Unknown opcode: %s ' % op
 
     def field_name(self, obj, field):
-        class_name = self.class_name(obj)
-        field_type = self.cts.lltype_to_cts(obj._field_type(field))
+        class_, type_ = obj._lookup_field(field)
+        assert type_ is not None, 'Cannot find the field %s in the object %s' % (field, obj)
+        
+        class_name = self.class_name(class_)
+        field_type = self.cts.lltype_to_cts(type_)
         return '%s %s::%s' % (field_type, class_name, field)
 
     def ctor_name(self, ooinstance):
@@ -282,7 +283,7 @@
         else:
             assert False
 
-    def _load_const(self, const):
+    def _load_const(self, const):        
         type_ = const.concretetype
         if type_ is Void:
             pass
@@ -295,9 +296,9 @@
         elif type_ in (SignedLongLong, UnsignedLongLong):
             self.ilasm.opcode('ldc.i8', str(const.value))
         else:
-            name = self.db.record_const(const)
+            name = self.db.record_const(const.value)
             cts_type = self.cts.lltype_to_cts(type_)
-            self.ilasm.opcode('ldsfld %s Pypy.Constants::%s' % (cts_type, name))
+            self.ilasm.opcode('ldsfld %s %s' % (cts_type, name))
             #assert False, 'Unknown constant %s' % const
 
 

Modified: pypy/dist/pypy/translator/cli/gencli.py
==============================================================================
--- pypy/dist/pypy/translator/cli/gencli.py	(original)
+++ pypy/dist/pypy/translator/cli/gencli.py	Sat Apr  8 19:07:41 2006
@@ -49,7 +49,8 @@
         self.gen_entrypoint()
         self.find_superclasses()
         self.gen_classes()
-        self.gen_functions()        
+        self.gen_functions()
+        self.db.gen_constants(self.ilasm)
         out.close()
         return self.tmpfile.strpath
 

Modified: pypy/dist/pypy/translator/cli/ilgenerator.py
==============================================================================
--- pypy/dist/pypy/translator/cli/ilgenerator.py	(original)
+++ pypy/dist/pypy/translator/cli/ilgenerator.py	Sat Apr  8 19:07:41 2006
@@ -59,8 +59,13 @@
     def end_class(self):
         self.code.closeblock()
 
-    def field(self, name, type_):
-        self.code.writeline('.field public %s %s' %(type_, name))
+    def field(self, name, type_, static = False):
+        if static:
+            s = 'static'
+        else:
+            s = ''
+
+        self.code.writeline('.field public %s %s %s' % (s, type_, name))
 
     def begin_function(self, name, arglist, returntype, is_entrypoint = False, *args):
         # TODO: .maxstack

Modified: pypy/dist/pypy/translator/cli/test/compile.py
==============================================================================
--- pypy/dist/pypy/translator/cli/test/compile.py	(original)
+++ pypy/dist/pypy/translator/cli/test/compile.py	Sat Apr  8 19:07:41 2006
@@ -1,4 +1,5 @@
 #!/bin/env python
+import autopath
 import sys
 import py
 from pypy.rpython.rarithmetic import r_int, r_uint, r_ulonglong, r_longlong, ovfcheck
@@ -27,15 +28,12 @@
         self.x = x
 
     def compute(self):
-        return self.x
+        return self.x + 1
 
 class Derived(Base):
     def __init__(self, x):
         Base.__init__(self, x)
 
-    def compute(self):
-        return self.x+1
-
 def foo(cls, arg):
     obj = cls(arg)
     return obj.compute()
@@ -45,12 +43,10 @@
     return obj.compute()
 #    return foo(Base, x) + foo(Derived, y)
 
-
-
 f = compile_function(bar, [int, int])
 
-try:
-    pass
-except py.test.Item.Skipped:
-    print 'Test skipped'
+##try:
+##    pass
+##except py.test.Item.Skipped:
+##    print 'Test skipped'
 



More information about the Pypy-commit mailing list