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

antocuni at codespeak.net antocuni at codespeak.net
Sat Jul 15 11:57:36 CEST 2006


Author: antocuni
Date: Sat Jul 15 11:57:28 2006
New Revision: 30061

Added:
   pypy/dist/pypy/translator/cli/entrypoint.py   (contents, props changed)
Modified:
   pypy/dist/pypy/translator/cli/test/runtest.py
   pypy/dist/pypy/translator/driver.py
Log:
Have two separate classes for StandaloneEntryPoint and
TestEntryPoint. StandaloneEntryPoint is choosen when our entrypoint
takes a list of strings, which is interpreted as 'argv'.



Added: pypy/dist/pypy/translator/cli/entrypoint.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/cli/entrypoint.py	Sat Jul 15 11:57:28 2006
@@ -0,0 +1,58 @@
+from pypy.translator.cli.cts import CTS
+from pypy.translator.cli.database import LowLevelDatabase
+from pypy.translator.cli.node import Node
+from pypy.translator.cli.test.runtest import TestEntryPoint
+from pypy.rpython.ootypesystem import ootype
+
+cts = CTS(LowLevelDatabase()) # this is a hack!
+
+def get_entrypoint(graph):
+    try:
+        ARG0 = graph.getargs()[0].concretetype
+    except IndexError:
+        ARG0 = None
+    if isinstance(ARG0, ootype.List) and ARG0._ITEMTYPE is ootype.String:
+        return StandaloneEntryPoint(graph)
+    else:
+        return TestEntryPoint(graph)
+
+class StandaloneEntryPoint(Node):
+    """
+    This class produces a 'main' method that converts the argv in a
+    List of Strings and pass it to the real entry point.
+    """
+    
+    def __init__(self, graph_to_call):
+        self.graph = graph_to_call
+        self.db = None
+
+    def get_name(self):
+        return 'main'
+
+    def render(self, ilasm):
+        try:
+            ARG0 = self.graph.getargs()[0].concretetype
+        except IndexError:
+            ARG0 = None
+        assert isinstance(ARG0, ootype.List) and ARG0._ITEMTYPE is ootype.String,\
+               'Wrong entry point signature: List(String) expected'
+
+        ilasm.begin_function('main', [('string[]', 'argv')], 'void', True, 'static')
+        ilasm.new('instance void class [pypylib]pypy.runtime.List`1<string>::.ctor()')
+
+        # fake argv[0]        
+        ilasm.opcode('dup')
+        ilasm.opcode('ldstr ""')
+        ilasm.call_method('void class [mscorlib]System.Collections.Generic.List`1<string>::Add(!0)', True)
+
+        # add real argv
+        ilasm.opcode('dup')
+        ilasm.opcode('ldarg.0')
+        ilasm.call_method('void class [mscorlib]System.Collections.Generic.List`1<string>::'
+                          'AddRange(class [mscorlib]System.Collections.Generic.IEnumerable`1<!0>)', True)
+
+        ilasm.call(cts.graph_to_signature(self.graph))
+        ilasm.opcode('pop') # XXX: return this value, if it's an int32
+        ilasm.opcode('ret')
+        ilasm.end_function()
+        self.db.pending_function(self.graph)

Modified: pypy/dist/pypy/translator/cli/test/runtest.py
==============================================================================
--- pypy/dist/pypy/translator/cli/test/runtest.py	(original)
+++ pypy/dist/pypy/translator/cli/test/runtest.py	Sat Jul 15 11:57:28 2006
@@ -48,7 +48,7 @@
     to int32, pass them to another method and prints out the result.
     """
     
-    def __init__(self, graph_to_call, wrap_exceptions):
+    def __init__(self, graph_to_call, wrap_exceptions=False):
         self.graph = graph_to_call
         self.db = None
         self.wrap_exceptions = wrap_exceptions
@@ -57,44 +57,26 @@
         return 'main'
 
     def render(self, ilasm):
-        try:
-            ARG0 = self.graph.getargs()[0].concretetype
-        except IndexError:
-            ARG0 = None
-        # special case: List(String) == argv
-        if isinstance(ARG0, ootype.List) and ARG0._ITEMTYPE is ootype.String:
-            main_argv = True
-        else:
-            main_argv = False
-        
         ilasm.begin_function('main', [('string[]', 'argv')], 'void', True, 'static')
 
         if self.wrap_exceptions:
             ilasm.begin_try()
 
-        if main_argv:
+        # convert string arguments to their true type
+        for i, arg in enumerate(self.graph.getargs()):
             ilasm.opcode('ldarg.0')
-            ilasm.new('instance void class [pypylib]pypy.runtime.List`1<string>::.ctor(!0[])')
-        else:
-            # convert string arguments to their true type
-            for i, arg in enumerate(self.graph.getargs()):
-                ilasm.opcode('ldarg.0')
-                ilasm.opcode('ldc.i4.%d' % i)
-                ilasm.opcode('ldelem.ref')
-                arg_type, arg_var = cts.llvar_to_cts(arg)
-                ilasm.call('%s class [mscorlib]System.Convert::%s(string)' %
-                           (arg_type, self.__convert_method(arg_type)))
+            ilasm.opcode('ldc.i4.%d' % i)
+            ilasm.opcode('ldelem.ref')
+            arg_type, arg_var = cts.llvar_to_cts(arg)
+            ilasm.call('%s class [mscorlib]System.Convert::%s(string)' %
+                       (arg_type, self.__convert_method(arg_type)))
 
         # call the function and convert the result to a string containing a valid python expression
         ilasm.call(cts.graph_to_signature(self.graph))
-
-        if main_argv:
-            ilasm.opcode('pop') # XXX: return this value
-        else:
-            TYPE = self.graph.getreturnvar().concretetype
-            format_object(TYPE, ilasm)
-            ilasm.call('void class [mscorlib]System.Console::WriteLine(string)')
-            ilasm.leave('return')
+        TYPE = self.graph.getreturnvar().concretetype
+        format_object(TYPE, ilasm)
+        ilasm.call('void class [mscorlib]System.Console::WriteLine(string)')
+        ilasm.leave('return')
 
         if self.wrap_exceptions:
             ilasm.end_try()

Modified: pypy/dist/pypy/translator/driver.py
==============================================================================
--- pypy/dist/pypy/translator/driver.py	(original)
+++ pypy/dist/pypy/translator/driver.py	Sat Jul 15 11:57:28 2006
@@ -471,10 +471,11 @@
 
     def task_source_cli(self):
         from pypy.translator.cli.gencli import GenCli
-        from pypy.translator.cli.test.runtest import TestEntryPoint
+        from pypy.translator.cli.entrypoint import get_entrypoint
         from pypy.tool.udir import udir
+
         entry_point_graph = self.translator.graphs[0]
-        self.gen = GenCli(udir, self.translator, TestEntryPoint(entry_point_graph, False))
+        self.gen = GenCli(udir, self.translator, get_entrypoint(entry_point_graph))
         filename = self.gen.generate_source()
         self.log.info("Wrote %s" % (filename,))
     task_source_cli = taskdef(task_source_cli, [OOTYPE],



More information about the Pypy-commit mailing list