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

antocuni at codespeak.net antocuni at codespeak.net
Sat Feb 9 12:00:26 CET 2008


Author: antocuni
Date: Sat Feb  9 12:00:23 2008
New Revision: 51354

Modified:
   pypy/dist/pypy/translator/cli/dotnet.py
   pypy/dist/pypy/translator/cli/ilgenerator.py
   pypy/dist/pypy/translator/cli/metavm.py
   pypy/dist/pypy/translator/cli/opcodes.py
   pypy/dist/pypy/translator/cli/src/pypylib.cs
   pypy/dist/pypy/translator/cli/test/test_dotnet.py
Log:
add support to set static fields of cli classes



Modified: pypy/dist/pypy/translator/cli/dotnet.py
==============================================================================
--- pypy/dist/pypy/translator/cli/dotnet.py	(original)
+++ pypy/dist/pypy/translator/cli/dotnet.py	Sat Feb  9 12:00:23 2008
@@ -32,6 +32,15 @@
         else:
             return s_ImpossibleValue
 
+    def setattr(self, s_attr, s_value):
+        assert self.is_constant()
+        assert s_attr.is_constant
+        cliclass = self.const
+        attrname = s_attr.const
+        if attrname not in cliclass._static_fields:
+            return s_ImpossibleValue
+        # XXX: check types?
+
     def simple_call(self, *s_args):
         assert self.is_constant()
         return SomeOOInstance(self.const._INSTANCE)
@@ -102,9 +111,17 @@
             assert attrname in self.cli_class._static_fields
             TYPE = self.cli_class._static_fields[attrname]
             c_class = hop.inputarg(hop.args_r[0], arg=0)
-            c_name = hop.inputconst(ootype.Void, hop.args_v[1].value)
+            c_name = hop.inputconst(ootype.Void, attrname)
             return hop.genop("cli_getstaticfield", [c_class, c_name], resulttype=hop.r_result.lowleveltype)
 
+    def rtype_setattr(self, hop):
+        attrname = hop.args_v[1].value
+        assert attrname in self.cli_class._static_fields
+        c_class = hop.inputarg(hop.args_r[0], arg=0)
+        c_name = hop.inputconst(ootype.Void, attrname)
+        v_value = hop.inputarg(hop.args_r[2], arg=2)
+        return hop.genop("cli_setstaticfield", [c_class, c_name, v_value], resulttype=hop.r_result.lowleveltype)
+
     def rtype_simple_call(self, hop):
         # TODO: resolve constructor overloading
         INSTANCE = hop.args_r[0].cli_class._INSTANCE

Modified: pypy/dist/pypy/translator/cli/ilgenerator.py
==============================================================================
--- pypy/dist/pypy/translator/cli/ilgenerator.py	(original)
+++ pypy/dist/pypy/translator/cli/ilgenerator.py	Sat Feb  9 12:00:23 2008
@@ -244,6 +244,9 @@
     def load_static_field(self, cts_type, name):
         self.opcode('ldsfld', '%s %s' % (cts_type, name))
 
+    def store_static_field(self, cts_type, name):
+        self.opcode('stsfld', '%s %s' % (cts_type, name))
+
     def emit(self, opcode, *args):
         self.opcode(opcode,*args)
 

Modified: pypy/dist/pypy/translator/cli/metavm.py
==============================================================================
--- pypy/dist/pypy/translator/cli/metavm.py	(original)
+++ pypy/dist/pypy/translator/cli/metavm.py	Sat Feb  9 12:00:23 2008
@@ -224,6 +224,15 @@
         desc = '%s::%s' % (cli_class._name, fldname)
         generator.ilasm.load_static_field(cts_type, desc)
 
+class _SetStaticField(MicroInstruction):
+    def render(self, generator, op):
+        cli_class = op.args[0].value
+        fldname = op.args[1].value
+        TYPE = op.result.concretetype
+        cts_type = generator.cts.lltype_to_cts(TYPE)
+        desc = '%s::%s' % (cli_class._name, fldname)
+        generator.load(op.args[2])
+        generator.ilasm.store_static_field(cts_type, desc)
 
 OOTYPE_TO_MNEMONIC = {
     ootype.Signed: 'i4',
@@ -252,4 +261,5 @@
 TypeOf = _TypeOf()
 EventHandler = _EventHandler()
 GetStaticField = _GetStaticField()
+SetStaticField = _SetStaticField()
 CastPrimitive = _CastPrimitive()

Modified: pypy/dist/pypy/translator/cli/opcodes.py
==============================================================================
--- pypy/dist/pypy/translator/cli/opcodes.py	(original)
+++ pypy/dist/pypy/translator/cli/opcodes.py	Sat Feb  9 12:00:23 2008
@@ -1,7 +1,7 @@
 from pypy.translator.cli.metavm import  Call, CallMethod, \
      IndirectCall, GetField, SetField, DownCast, NewCustomDict,\
      MapException, Box, Unbox, NewArray, GetArrayElem, SetArrayElem,\
-     TypeOf, CastPrimitive, EventHandler, GetStaticField
+     TypeOf, CastPrimitive, EventHandler, GetStaticField, SetStaticField
 from pypy.translator.oosupport.metavm import PushArg, PushAllArgs, StoreResult, InstructionList,\
     New, RuntimeNew, CastTo, PushPrimitive, OOString, OOUnicode
 from pypy.translator.cli.cts import WEAKREF
@@ -44,6 +44,7 @@
     'cli_arraylength':          'ldlen',
     'cli_eventhandler':         [EventHandler],
     'cli_getstaticfield':       [GetStaticField],
+    'cli_setstaticfield':       [SetStaticField],
     'oois':                     'ceq',
     'oononnull':                [PushAllArgs, 'ldnull', 'ceq']+Not,
     'instanceof':               [CastTo, 'ldnull', 'cgt.un'],
@@ -106,7 +107,7 @@
     # trick.
     'cast_bool_to_int':         [PushAllArgs, 'ldc.i4.0', 'ceq']+Not,
     'cast_bool_to_uint':        [PushAllArgs, 'ldc.i4.0', 'ceq']+Not,
-    'cast_bool_to_float':       [PushAllArgs, 'ldc.i4 0', 'ceq']+Not+['conv.r8'],
+    'cast_bool_to_float':       [PushAllArgs, 'ldc.i4.0', 'ceq']+Not+['conv.r8'],
     'cast_char_to_int':         DoNothing,
     'cast_unichar_to_int':      DoNothing,
     'cast_int_to_char':         DoNothing,

Modified: pypy/dist/pypy/translator/cli/src/pypylib.cs
==============================================================================
--- pypy/dist/pypy/translator/cli/src/pypylib.cs	(original)
+++ pypy/dist/pypy/translator/cli/src/pypylib.cs	Sat Feb  9 12:00:23 2008
@@ -57,6 +57,12 @@
     public delegate int DelegateType_int__int_int(int a, int b);
     public delegate int DelegateType_int__int_100(int a0, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9, int a10, int a11, int a12, int a13, int a14, int a15, int a16, int a17, int a18, int a19, int a20, int a21, int a22, int a23, int a24, int a25, int a26, int a27, int a28, int a29, int a30, int a31, int a32, int a33, int a34, int a35, int a36, int a37, int a38, int a39, int a40, int a41, int a42, int a43, int a44, int a45, int a46, int a47, int a48, int a49, int a50, int a51, int a52, int a53, int a54, int a55, int a56, int a57, int a58, int a59, int a60, int a61, int a62, int a63, int a64, int a65, int a66, int a67, int a68, int a69, int a70, int a71, int a72, int a73, int a74, int a75, int a76, int a77, int a78, int a79, int a80, int a81, int a82, int a83, int a84, int a85, int a86, int a87, int a88, int a89, int a90, int a91, int a92, int a93, int a94, int a95, int a96, int a97, int a98, int a99);
 
+    public class Constants
+    {
+        public static object const1;
+        public static object const2;
+    }
+
     public class Utils
     {
         public static DynamicMethod CreateDynamicMethod(string name, Type res, Type[] args)

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	Sat Feb  9 12:00:23 2008
@@ -469,12 +469,13 @@
         assert res == 42
 
     def test_static_fields(self):
-        py.test.skip("does not work, and no need to implement")
+        Constants = CLR.pypy.runtime.Constants
         def fn():
-            op = OpCodes.Add
-            return op.get_Name()
+            obj = System.Object()
+            Constants.const1 = obj
+            return Constants.const1 is obj
         res = self.interpret(fn, [])
-        assert self.ll_to_string(res) == 'add'
+        assert res
 
     def test_pypylib(self):
         def fn():



More information about the Pypy-commit mailing list