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

antocuni at codespeak.net antocuni at codespeak.net
Wed Oct 25 16:22:52 CEST 2006


Author: antocuni
Date: Wed Oct 25 16:22:50 2006
New Revision: 33723

Modified:
   pypy/dist/pypy/translator/cli/cts.py
   pypy/dist/pypy/translator/cli/dotnet.py
   pypy/dist/pypy/translator/cli/metavm.py
   pypy/dist/pypy/translator/cli/opcodes.py
   pypy/dist/pypy/translator/cli/test/test_dotnet.py
Log:
Initial support for boxing object.



Modified: pypy/dist/pypy/translator/cli/cts.py
==============================================================================
--- pypy/dist/pypy/translator/cli/cts.py	(original)
+++ pypy/dist/pypy/translator/cli/cts.py	Wed Oct 25 16:22:50 2006
@@ -57,6 +57,10 @@
     ootype.DictItemsIterator.VALUETYPE_T: '!1',
     }
 
+_lltype_to_box = {
+    ootype.Signed: '[mscorlib]System.Int32'
+    }
+
 _pyexception_to_cts = {
     exceptions.Exception: '[mscorlib]System.Exception',
     exceptions.OverflowError: '[mscorlib]System.OverflowException'
@@ -187,6 +191,9 @@
     def llconst_to_cts(self, const):
         return self.lltype_to_cts(const.concretetype), const.value
 
+    def lltype_to_box(self, TYPE):
+        return _get_from_dict(_lltype_to_box, TYPE, "Don't know how to box type %s" % TYPE)
+
     def ctor_name(self, t):
         return 'instance void %s::.ctor()' % self.lltype_to_cts(t)
 

Modified: pypy/dist/pypy/translator/cli/dotnet.py
==============================================================================
--- pypy/dist/pypy/translator/cli/dotnet.py	(original)
+++ pypy/dist/pypy/translator/cli/dotnet.py	Wed Oct 25 16:22:50 2006
@@ -1,3 +1,4 @@
+from pypy.rpython.error import TyperError
 from pypy.rpython.extregistry import ExtRegistryEntry
 from pypy.rpython.ootypesystem import ootype
 from pypy.rpython.ootypesystem.ootype import meth, overload, Meth, StaticMethod
@@ -39,7 +40,6 @@
         return self.__class__, self.cli_class, self.meth_name
 
 
-
 ## Rtyper model
 
 class CliClassRepr(Repr):
@@ -90,7 +90,7 @@
         self._name = name
 
     def _get_desc(self, ARGS):
-        assert ARGS == self._TYPE.ARGS
+        #assert ARGS == self._TYPE.ARGS
         return self
 
 
@@ -117,7 +117,6 @@
         self._classname = name
         ootype.Instance.__init__(self, fullname, superclass, fields, methods, _is_root, _hints)
 
-
 ## RPython interface definition
 
 class CliClass(object):
@@ -173,6 +172,27 @@
         return SomeCliClass()
 
 
+def box(x):
+    return x
+
+class Entry(ExtRegistryEntry):
+    _about_ = box
+
+    boxable_types = [ootype.Signed, ootype.String]
+
+    def compute_result_annotation(self, x_s):
+        return annmodel.SomeOOInstance(CLR.System.Object._INSTANCE)
+
+    def specialize_call(self, hop):
+        v_obj, = hop.inputargs(*hop.args_r)
+        if v_obj.concretetype not in self.boxable_types:
+            raise TyperError, "Can't box values of type %s" % v_obj.concretetype
+        
+        if (v_obj.concretetype is ootype.String):
+            return hop.genop('ooupcast', [v_obj], hop.r_result.lowleveltype)
+        else:
+            return hop.genop('clibox', [v_obj], hop.r_result.lowleveltype)
+
 class CliNamespace(object):
     def __init__(self, name):
         self._name = name

Modified: pypy/dist/pypy/translator/cli/metavm.py
==============================================================================
--- pypy/dist/pypy/translator/cli/metavm.py	(original)
+++ pypy/dist/pypy/translator/cli/metavm.py	Wed Oct 25 16:22:50 2006
@@ -156,6 +156,13 @@
         ilasm.label(label)
         ilasm.opcode('nop')
 
+class _Box(MicroInstruction): 
+    def render(self, generator, op):
+        generator.load(op.args[0])
+        TYPE = op.args[0].concretetype
+        boxtype = generator.cts.lltype_to_box(TYPE)
+        generator.ilasm.opcode('box', boxtype)
+
 Call = _Call()
 CallMethod = _CallMethod()
 IndirectCall = _IndirectCall()
@@ -164,3 +171,4 @@
 OOString = _OOString()
 NewCustomDict = _NewCustomDict()
 CastWeakAdrToPtr = _CastWeakAdrToPtr()
+Box = _Box()

Modified: pypy/dist/pypy/translator/cli/opcodes.py
==============================================================================
--- pypy/dist/pypy/translator/cli/opcodes.py	(original)
+++ pypy/dist/pypy/translator/cli/opcodes.py	Wed Oct 25 16:22:50 2006
@@ -1,6 +1,6 @@
 from pypy.translator.cli.metavm import  Call, CallMethod, RuntimeNew, \
      IndirectCall, GetField, SetField, CastTo, OOString, DownCast, NewCustomDict,\
-     CastWeakAdrToPtr, MapException
+     CastWeakAdrToPtr, MapException, Box
 from pypy.translator.oosupport.metavm import PushArg, PushAllArgs, StoreResult, InstructionList,\
     New
 from pypy.translator.cli.cts import WEAKREF
@@ -34,6 +34,7 @@
     'oosend':                   [CallMethod],
     'ooupcast':                 DoNothing,
     'oodowncast':               [DownCast],
+    'clibox':                   [Box],
     'oois':                     'ceq',
     'oononnull':                [PushAllArgs, 'ldnull', 'ceq']+Not,
     'instanceof':               [CastTo, 'ldnull', 'cgt.un'],

Modified: pypy/dist/pypy/translator/cli/test/test_dotnet.py
==============================================================================
--- pypy/dist/pypy/translator/cli/test/test_dotnet.py	(original)
+++ pypy/dist/pypy/translator/cli/test/test_dotnet.py	Wed Oct 25 16:22:50 2006
@@ -2,7 +2,7 @@
 from pypy.annotation import model as annmodel
 from pypy.translator.cli.test.runtest import CliTest
 from pypy.translator.cli.dotnet import SomeCliClass, SomeCliStaticMethod,\
-     NativeInstance, CLR
+     NativeInstance, CLR, box
 
 Math = CLR.System.Math
 ArrayList = CLR.System.Collections.ArrayList
@@ -81,3 +81,10 @@
         mylist.Add('foo')
         assert mylist.Count == 2
     
+    def test_box(self):
+        def fn():
+            x = ArrayList()
+            x.Add(box(42))
+            x.Add(box('Foo'))
+            return x.get_Count()
+        assert self.interpret(fn, []) == 2



More information about the Pypy-commit mailing list