[pypy-svn] r48495 - in pypy/branch/unicode-objspace/pypy/translator: cli jvm jvm/src/pypy jvm/test oosupport

antocuni at codespeak.net antocuni at codespeak.net
Sat Nov 10 11:47:08 CET 2007


Author: antocuni
Date: Sat Nov 10 11:47:06 2007
New Revision: 48495

Added:
   pypy/branch/unicode-objspace/pypy/translator/jvm/test/test_unicode.py   (contents, props changed)
Modified:
   pypy/branch/unicode-objspace/pypy/translator/cli/function.py
   pypy/branch/unicode-objspace/pypy/translator/cli/metavm.py
   pypy/branch/unicode-objspace/pypy/translator/cli/opcodes.py
   pypy/branch/unicode-objspace/pypy/translator/jvm/builtin.py
   pypy/branch/unicode-objspace/pypy/translator/jvm/conftest.py
   pypy/branch/unicode-objspace/pypy/translator/jvm/database.py
   pypy/branch/unicode-objspace/pypy/translator/jvm/generator.py
   pypy/branch/unicode-objspace/pypy/translator/jvm/opcodes.py
   pypy/branch/unicode-objspace/pypy/translator/jvm/prebuiltnodes.py
   pypy/branch/unicode-objspace/pypy/translator/jvm/src/pypy/Interlink.java
   pypy/branch/unicode-objspace/pypy/translator/jvm/src/pypy/PyPy.java
   pypy/branch/unicode-objspace/pypy/translator/jvm/test/runtest.py
   pypy/branch/unicode-objspace/pypy/translator/oosupport/metavm.py
Log:
add unicode support to genjvm



Modified: pypy/branch/unicode-objspace/pypy/translator/cli/function.py
==============================================================================
--- pypy/branch/unicode-objspace/pypy/translator/cli/function.py	(original)
+++ pypy/branch/unicode-objspace/pypy/translator/cli/function.py	Sat Nov 10 11:47:06 2007
@@ -162,6 +162,17 @@
         for link, lbl in cases.itervalues():
             self.render_switch_case(link, lbl)
 
+    def call_oostring(self, ARGTYPE):
+        if isinstance(ARGTYPE, ootype.Instance):
+            argtype = self.cts.types.object
+        else:
+            argtype = self.cts.lltype_to_cts(ARGTYPE)
+        self.call_signature('string [pypylib]pypy.runtime.Utils::OOString(%s, int32)' % argtype)
+
+    def call_oounicode(self, ARGTYPE):
+        argtype = self.cts.lltype_to_cts(ARGTYPE)
+        self.call_signature('string [pypylib]pypy.runtime.Utils::OOUnicode(%s)' % argtype)
+
     # Those parts of the generator interface that are function
     # specific
 

Modified: pypy/branch/unicode-objspace/pypy/translator/cli/metavm.py
==============================================================================
--- pypy/branch/unicode-objspace/pypy/translator/cli/metavm.py	(original)
+++ pypy/branch/unicode-objspace/pypy/translator/cli/metavm.py	Sat Nov 10 11:47:06 2007
@@ -95,28 +95,6 @@
         generator.call_signature('object [pypylib]pypy.runtime.Utils::RuntimeNew(class [mscorlib]System.Type)')
         generator.cast_to(op.result.concretetype)
 
-class _OOString(MicroInstruction):
-    def render(self, generator, op):
-        ARGTYPE = op.args[0].concretetype
-        if isinstance(ARGTYPE, ootype.Instance):
-            argtype = 'object'
-        else:
-            argtype = generator.cts.lltype_to_cts(ARGTYPE)
-        generator.load(op.args[0])
-        generator.load(op.args[1])
-        generator.call_signature('string [pypylib]pypy.runtime.Utils::OOString(%s, int32)' % argtype)
-
-class _OOUnicode(MicroInstruction):
-    def render(self, generator, op):
-        from pypy.objspace.flow.model import Constant
-        ARGTYPE = op.args[0].concretetype
-        argtype = generator.cts.lltype_to_cts(ARGTYPE)
-        v_base = op.args[1]
-        assert v_base.value == -1, "The second argument of oounicode must be -1"
-        
-        generator.load(op.args[0])
-        generator.call_signature('string [pypylib]pypy.runtime.Utils::OOUnicode(%s)' % argtype)
-
 class _NewCustomDict(MicroInstruction):
     def render(self, generator, op):
         DICT = op.args[0].value
@@ -236,8 +214,6 @@
 CallMethod = _CallMethod()
 IndirectCall = _IndirectCall()
 RuntimeNew = _RuntimeNew()
-OOString = _OOString()
-OOUnicode = _OOUnicode()
 NewCustomDict = _NewCustomDict()
 #CastWeakAdrToPtr = _CastWeakAdrToPtr()
 Box = _Box()

Modified: pypy/branch/unicode-objspace/pypy/translator/cli/opcodes.py
==============================================================================
--- pypy/branch/unicode-objspace/pypy/translator/cli/opcodes.py	(original)
+++ pypy/branch/unicode-objspace/pypy/translator/cli/opcodes.py	Sat Nov 10 11:47:06 2007
@@ -1,9 +1,9 @@
 from pypy.translator.cli.metavm import  Call, CallMethod, \
-     IndirectCall, GetField, SetField, OOString, DownCast, NewCustomDict,\
+     IndirectCall, GetField, SetField, DownCast, NewCustomDict,\
      MapException, Box, Unbox, NewArray, GetArrayElem, SetArrayElem,\
-     TypeOf, CastPrimitive, OOUnicode
+     TypeOf, CastPrimitive
 from pypy.translator.oosupport.metavm import PushArg, PushAllArgs, StoreResult, InstructionList,\
-    New, RuntimeNew, CastTo, PushPrimitive
+    New, RuntimeNew, CastTo, PushPrimitive, OOString, OOUnicode
 from pypy.translator.cli.cts import WEAKREF
 from pypy.rpython.ootypesystem import ootype
 

Modified: pypy/branch/unicode-objspace/pypy/translator/jvm/builtin.py
==============================================================================
--- pypy/branch/unicode-objspace/pypy/translator/jvm/builtin.py	(original)
+++ pypy/branch/unicode-objspace/pypy/translator/jvm/builtin.py	Sat Nov 10 11:47:06 2007
@@ -149,3 +149,18 @@
     jvmgen.Method.v(jArrayList, "get", (jInt,), jObject),
 
     }
+
+# ootype.String[Builder] and ootype.Unicode[Builder] are mapped to the
+# same JVM type, so we reuse the same builtin methods also for them
+def add_unicode_methods():
+    mapping = {
+        ootype.String.__class__: ootype.Unicode.__class__,
+        ootype.StringBuilder.__class__: ootype.UnicodeBuilder.__class__
+        }
+    
+    for (TYPE, name), value in built_in_methods.items():
+        if TYPE in mapping:
+            TYPE = mapping[TYPE]
+            built_in_methods[TYPE, name] = value
+add_unicode_methods()
+del add_unicode_methods

Modified: pypy/branch/unicode-objspace/pypy/translator/jvm/conftest.py
==============================================================================
--- pypy/branch/unicode-objspace/pypy/translator/jvm/conftest.py	(original)
+++ pypy/branch/unicode-objspace/pypy/translator/jvm/conftest.py	Sat Nov 10 11:47:06 2007
@@ -15,8 +15,8 @@
            help="don't assemble jasmin files"),
     Option('--package', action='store', dest='package', default='pypy',
            help='Package to output generated classes into'),
-    Option('--trace', action='store_true', dest='trace', default=False,
-           help='Trace execution of generated code'),
+##    Option('--trace', action='store_true', dest='trace', default=False,
+##           help='Trace execution of generated code'),
     Option('--byte-arrays', action='store_true', dest='byte-arrays',
            default=False, help='Use byte arrays rather than native strings'),
     )

Modified: pypy/branch/unicode-objspace/pypy/translator/jvm/database.py
==============================================================================
--- pypy/branch/unicode-objspace/pypy/translator/jvm/database.py	(original)
+++ pypy/branch/unicode-objspace/pypy/translator/jvm/database.py	Sat Nov 10 11:47:06 2007
@@ -412,7 +412,9 @@
         ootype.Bool:jvmgen.PYPYSERIALIZEBOOLEAN,
         ootype.Void:jvmgen.PYPYSERIALIZEVOID,
         ootype.Char:jvmgen.PYPYESCAPEDCHAR,
+        ootype.UniChar:jvmgen.PYPYESCAPEDUNICHAR,
         ootype.String:jvmgen.PYPYESCAPEDSTRING,
+        ootype.Unicode:jvmgen.PYPYESCAPEDUNICODE,
         }
 
     def toString_method_for_ootype(self, OOTYPE):
@@ -466,7 +468,9 @@
     # will return a JvmBuiltInType based on the value
     ootype_to_builtin = {
         ootype.String:           jvmtype.jString,
+        ootype.Unicode:          jvmtype.jString,
         ootype.StringBuilder:    jvmtype.jStringBuilder,
+        ootype.UnicodeBuilder:   jvmtype.jStringBuilder,
         ootype.List:             jvmtype.jArrayList,
         ootype.Dict:             jvmtype.jHashMap,
         ootype.DictItemsIterator:jvmtype.jPyPyDictItemsIterator,

Modified: pypy/branch/unicode-objspace/pypy/translator/jvm/generator.py
==============================================================================
--- pypy/branch/unicode-objspace/pypy/translator/jvm/generator.py	(original)
+++ pypy/branch/unicode-objspace/pypy/translator/jvm/generator.py	Sat Nov 10 11:47:06 2007
@@ -389,7 +389,9 @@
 PYPYSERIALIZEULONG =    Method.s(jPyPy, 'serialize_ulonglong', (jLong,), jString)
 PYPYSERIALIZEVOID =     Method.s(jPyPy, 'serialize_void', (), jString)
 PYPYESCAPEDCHAR =       Method.s(jPyPy, 'escaped_char', (jChar,), jString)
+PYPYESCAPEDUNICHAR =    Method.s(jPyPy, 'escaped_unichar', (jChar,), jString)
 PYPYESCAPEDSTRING =     Method.s(jPyPy, 'escaped_string', (jString,), jString)
+PYPYESCAPEDUNICODE =    Method.s(jPyPy, 'escaped_unicode', (jString,), jString)
 PYPYSERIALIZEOBJECT =   Method.s(jPyPy, 'serializeObject', (jObject,), jString)
 PYPYRUNTIMENEW =        Method.s(jPyPy, 'RuntimeNew', (jClass,), jObject)
 PYPYSTRING2BYTES =      Method.s(jPyPy, 'string2bytes', (jString,), jByteArray)
@@ -1018,6 +1020,13 @@
         self.emit(mthd)
         if self.db.using_byte_array:
             self.emit(PYPYSTRING2BYTES)
+
+    def call_oounicode(self, OOTYPE):
+        cts_type = self.db.lltype_to_cts(OOTYPE)
+        mthd = Method.s(jPyPy, 'oounicode', [cts_type], jString)
+        self.emit(mthd)
+        if self.db.using_byte_array:
+            self.emit(PYPYSTRING2BYTES)
         
     def new(self, TYPE):
         jtype = self.db.lltype_to_cts(TYPE)
@@ -1087,11 +1096,13 @@
             self._push_long_constant(value)
         elif TYPE is ootype.Float:
             self._push_double_constant(float(value))
-        elif TYPE is ootype.String:
-            if value == ootype.null(ootype.String):
+        elif TYPE in (ootype.String, ootype.Unicode):
+            if value == ootype.null(TYPE):
                 self.emit(ACONST_NULL)
             else:
                 self.load_string(str(value._str))
+        else:
+            assert False, 'Unknown constant type: %s' % TYPE
 
     def _push_long_constant(self, value):
         if value == 0:
@@ -1336,7 +1347,7 @@
             return str(arg)
         strargs = [jasmin_syntax(arg) for arg in args]
         instr_text = '%s %s' % (jvmstr, " ".join(strargs))
-        #self.curclass.out('    .line %d\n' % self.curfunc.instr_counter)
+        self.curclass.out('    .line %d\n' % self.curfunc.instr_counter)
         self.curclass.out('    %-60s\n' % (instr_text,))
         self.curfunc.instr_counter+=1
 

Modified: pypy/branch/unicode-objspace/pypy/translator/jvm/opcodes.py
==============================================================================
--- pypy/branch/unicode-objspace/pypy/translator/jvm/opcodes.py	(original)
+++ pypy/branch/unicode-objspace/pypy/translator/jvm/opcodes.py	Sat Nov 10 11:47:06 2007
@@ -7,7 +7,8 @@
 
 from pypy.translator.oosupport.metavm import \
      PushArg, PushAllArgs, StoreResult, InstructionList, New, DoNothing, Call,\
-     SetField, GetField, DownCast, RuntimeNew, OOString, CastTo, PushPrimitive
+     SetField, GetField, DownCast, RuntimeNew, OOString, OOUnicode, \
+     CastTo, PushPrimitive
 from pypy.translator.jvm.metavm import \
      IndirectCall, JvmCallMethod, TranslateException, NewCustomDict, \
      CastPrimitive
@@ -56,6 +57,7 @@
     'ooidentityhash':           [PushAllArgs, jvmgen.OBJHASHCODE, StoreResult], 
     'oohash':                   [PushAllArgs, jvmgen.OBJHASHCODE, StoreResult], 
     'oostring':                 [OOString, StoreResult],
+    'oounicode':                [OOUnicode, StoreResult],
     #'ooparse_int':              [PushAllArgs, 'call int32 [pypylib]pypy.runtime.Utils::OOParseInt(string, int32)'],
     'ooparse_float':            jvmgen.PYPYOOPARSEFLOAT,
     'oonewcustomdict':          [NewCustomDict, StoreResult],

Modified: pypy/branch/unicode-objspace/pypy/translator/jvm/prebuiltnodes.py
==============================================================================
--- pypy/branch/unicode-objspace/pypy/translator/jvm/prebuiltnodes.py	(original)
+++ pypy/branch/unicode-objspace/pypy/translator/jvm/prebuiltnodes.py	Sat Nov 10 11:47:06 2007
@@ -14,6 +14,9 @@
 def throwValueError():
     raise ValueError
 
+def throwUnicodeDecodeError():
+    raise UnicodeDecodeError
+
 # ___________________________________________________________________________
 
 def create_interlink_node(db):

Modified: pypy/branch/unicode-objspace/pypy/translator/jvm/src/pypy/Interlink.java
==============================================================================
--- pypy/branch/unicode-objspace/pypy/translator/jvm/src/pypy/Interlink.java	(original)
+++ pypy/branch/unicode-objspace/pypy/translator/jvm/src/pypy/Interlink.java	Sat Nov 10 11:47:06 2007
@@ -14,5 +14,6 @@
     public void throwIndexError();
     public void throwOverflowError();
     public void throwValueError();
+    public void throwUnicodeDecodeError();
     public void throwOSError(int errCode);
 }

Modified: pypy/branch/unicode-objspace/pypy/translator/jvm/src/pypy/PyPy.java
==============================================================================
--- pypy/branch/unicode-objspace/pypy/translator/jvm/src/pypy/PyPy.java	(original)
+++ pypy/branch/unicode-objspace/pypy/translator/jvm/src/pypy/PyPy.java	Sat Nov 10 11:47:06 2007
@@ -319,6 +319,10 @@
         return sb.toString();
     }
 
+    public static String escaped_unichar(char c) {
+        return "u" + escaped_char(c);
+    }
+
     public static String escaped_string(String b) {
         if (b == null)
             return "None";
@@ -332,6 +336,10 @@
         return sb.toString();
     }
 
+    public static String escaped_unicode(String b) {
+        return "u" + escaped_string(b);
+    }
+
     // used in running unit tests
     // not really part of the dump_XXX set of objects, hence the lack
     // of an indent parameter
@@ -806,6 +814,24 @@
     }
 
     // ----------------------------------------------------------------------
+    // OOUnicode support
+
+    public static String oounicode(char ch)
+    {
+        return new Character(ch).toString();
+    }
+
+    public static String oounicode(String s)
+    {
+        for(int i=0; i<s.length(); i++) {
+            char ch = s.charAt(i);
+            if ((int)ch > 127)
+                throwUnicodeDecodeError();
+        }
+        return s;
+    }
+
+    // ----------------------------------------------------------------------
     // Primitive built-in functions
 
     public static double ll_time_clock() {
@@ -957,6 +983,10 @@
     public static void throwValueError() {
         interlink.throwValueError();
     }
+
+    public static void throwUnicodeDecodeError() {
+        interlink.throwUnicodeDecodeError();
+    }
     
     // ----------------------------------------------------------------------
     // Self Test

Modified: pypy/branch/unicode-objspace/pypy/translator/jvm/test/runtest.py
==============================================================================
--- pypy/branch/unicode-objspace/pypy/translator/jvm/test/runtest.py	(original)
+++ pypy/branch/unicode-objspace/pypy/translator/jvm/test/runtest.py	Sat Nov 10 11:47:06 2007
@@ -140,6 +140,9 @@
     def ll_to_string(self, s):
         return s
 
+    def ll_to_unicode(self, s):
+        return s
+
     def ll_to_list(self, l):
         return l
 

Added: pypy/branch/unicode-objspace/pypy/translator/jvm/test/test_unicode.py
==============================================================================
--- (empty file)
+++ pypy/branch/unicode-objspace/pypy/translator/jvm/test/test_unicode.py	Sat Nov 10 11:47:06 2007
@@ -0,0 +1,20 @@
+import py
+from pypy.translator.jvm.test.runtest import JvmTest
+from pypy.rpython.test.test_runicode import BaseTestRUnicode
+
+# ====> ../../../rpython/test/test_runicode.py
+
+class TestJvmUnicode(JvmTest, BaseTestRUnicode):
+
+    EMPTY_STRING_HASH = 0
+
+    def test_unichar_const(self):
+        py.test.skip("JVM doesn't support unicode for command line arguments")
+    test_unichar_eq = test_unichar_const
+    test_unichar_ord = test_unichar_const
+    test_unichar_hash = test_unichar_const
+    test_char_unichar_eq = test_unichar_const
+    test_char_unichar_eq_2 = test_unichar_const
+
+    def test_getitem_exc(self):
+        py.test.skip('fixme!')

Modified: pypy/branch/unicode-objspace/pypy/translator/oosupport/metavm.py
==============================================================================
--- pypy/branch/unicode-objspace/pypy/translator/oosupport/metavm.py	(original)
+++ pypy/branch/unicode-objspace/pypy/translator/oosupport/metavm.py	Sat Nov 10 11:47:06 2007
@@ -452,6 +452,15 @@
         generator.load(op.args[1])
         generator.call_oostring(ARGTYPE)
 
+class _OOUnicode(MicroInstruction):
+    def render(self, generator, op):
+        v_base = op.args[1]
+        assert v_base.value == -1, "The second argument of oounicode must be -1"
+
+        ARGTYPE = op.args[0].concretetype
+        generator.load(op.args[0])
+        generator.call_oounicode(ARGTYPE)
+
 class _CastTo(MicroInstruction):
     def render(self, generator, op):
         generator.load(op.args[0])
@@ -471,5 +480,6 @@
 CallMethod = _CallMethod()
 RuntimeNew = _RuntimeNew()
 OOString = _OOString()
+OOUnicode = _OOUnicode()
 CastTo = _CastTo()
 



More information about the Pypy-commit mailing list