[pypy-svn] r69097 - in pypy/trunk/pypy/translator/cli: . src test

antocuni at codespeak.net antocuni at codespeak.net
Mon Nov 9 16:15:22 CET 2009


Author: antocuni
Date: Mon Nov  9 16:15:21 2009
New Revision: 69097

Added:
   pypy/trunk/pypy/translator/cli/src/debug.cs
   pypy/trunk/pypy/translator/cli/test/test_standalone.py   (contents, props changed)
Modified:
   pypy/trunk/pypy/translator/cli/entrypoint.py
   pypy/trunk/pypy/translator/cli/metavm.py
   pypy/trunk/pypy/translator/cli/opcodes.py
   pypy/trunk/pypy/translator/cli/rte.py
   pypy/trunk/pypy/translator/cli/src/pypylib.cs
Log:
(antocuni, arigo around) implement debug_{print,start,stop} & co. for CLI


Modified: pypy/trunk/pypy/translator/cli/entrypoint.py
==============================================================================
--- pypy/trunk/pypy/translator/cli/entrypoint.py	(original)
+++ pypy/trunk/pypy/translator/cli/entrypoint.py	Mon Nov  9 16:15:21 2009
@@ -65,6 +65,8 @@
 
         ilasm.call(self.cts.graph_to_signature(self.graph))
         ilasm.opcode('pop') # XXX: return this value, if it's an int32
+
+        ilasm.call('void [pypylib]pypy.runtime.DebugPrint::close_file()')
         ilasm.opcode('ret')
         ilasm.end_function()
         self.db.pending_function(self.graph)

Modified: pypy/trunk/pypy/translator/cli/metavm.py
==============================================================================
--- pypy/trunk/pypy/translator/cli/metavm.py	(original)
+++ pypy/trunk/pypy/translator/cli/metavm.py	Mon Nov  9 16:15:21 2009
@@ -253,7 +253,7 @@
 
 class _DebugPrint(MicroInstruction):
     def render(self, generator, op):
-        MAXARGS = 4
+        MAXARGS = 8
         if len(op.args) > MAXARGS:
             generator.db.genoo.log.WARNING('debug_print supported only up to '
                                            '%d arguments (got %d)' % (MAXARGS, len(op.args)))
@@ -268,11 +268,7 @@
                 boxtype = generator.cts.lltype_to_cts(TYPE)
                 generator.ilasm.opcode('box', boxtype)
 
-        generator.ilasm.call('void [pypylib]pypy.runtime.Utils::debug_print(%s)' % signature)
-
-class _HaveDebugPrints(MicroInstruction):
-    def render(self, generator, op):
-        generator.ilasm.load_const(ootype.Bool, True)
+        generator.ilasm.call('void [pypylib]pypy.runtime.DebugPrint::DEBUG_PRINT(%s)' % signature)
 
 
 OOTYPE_TO_MNEMONIC = {
@@ -310,4 +306,4 @@
 SetStaticField = _SetStaticField()
 CastPrimitive = _CastPrimitive()
 DebugPrint = _DebugPrint()
-HaveDebugPrints = _HaveDebugPrints()
+

Modified: pypy/trunk/pypy/translator/cli/opcodes.py
==============================================================================
--- pypy/trunk/pypy/translator/cli/opcodes.py	(original)
+++ pypy/trunk/pypy/translator/cli/opcodes.py	Mon Nov  9 16:15:21 2009
@@ -2,7 +2,7 @@
      IndirectCall, GetField, SetField, DownCast, NewCustomDict,\
      MapException, Box, Unbox, NewArray, GetArrayElem, SetArrayElem,\
      TypeOf, CastPrimitive, EventHandler, GetStaticField, SetStaticField, \
-     DebugPrint, HaveDebugPrints
+     DebugPrint
 from pypy.translator.oosupport.metavm import PushArg, PushAllArgs, StoreResult, InstructionList,\
     New, RuntimeNew, CastTo, PushPrimitive, OOString, OOUnicode, OONewArray
 from pypy.translator.cli.cts import WEAKREF
@@ -78,10 +78,10 @@
     'resume_point':             Ignore,
     'debug_assert':             Ignore,
     'debug_print':              [DebugPrint],
-    'debug_start':              Ignore,
-    'debug_stop':               Ignore,
-    'have_debug_prints':        [HaveDebugPrints],
-    'debug_fatalerror':         [PushAllArgs, 'call void [pypylib]pypy.runtime.Utils::debug_fatalerror(string)'],
+    'debug_start':              [PushAllArgs, 'call void [pypylib]pypy.runtime.DebugPrint::DEBUG_START(string)'],
+    'debug_stop':               [PushAllArgs, 'call void [pypylib]pypy.runtime.DebugPrint::DEBUG_STOP(string)'],
+    'have_debug_prints':        [PushAllArgs, 'call bool [pypylib]pypy.runtime.DebugPrint::HAVE_DEBUG_PRINTS()'],
+    'debug_fatalerror':         [PushAllArgs, 'call void [pypylib]pypy.runtime.Debug::DEBUG_FATALERROR(string)'],
     'keepalive':                Ignore,
     'jit_marker':               Ignore,
     'promote_virtualizable':    Ignore,

Modified: pypy/trunk/pypy/translator/cli/rte.py
==============================================================================
--- pypy/trunk/pypy/translator/cli/rte.py	(original)
+++ pypy/trunk/pypy/translator/cli/rte.py	Mon Nov  9 16:15:21 2009
@@ -77,7 +77,8 @@
     get_COMPILER = classmethod(get_COMPILER)
     
 class PyPyLibDLL(Target):
-    SOURCES = ['pypylib.cs', 'll_os.cs', 'll_os_path.cs', 'errno.cs', 'll_math.cs']
+    SOURCES = ['pypylib.cs', 'll_os.cs', 'll_os_path.cs', 'errno.cs', 'll_math.cs',
+               'debug.cs']
     OUTPUT = 'pypylib.dll'
     FLAGS = ['/t:library', '/unsafe', '/r:main.exe']
     DEPENDENCIES = [MainStub]

Added: pypy/trunk/pypy/translator/cli/src/debug.cs
==============================================================================
--- (empty file)
+++ pypy/trunk/pypy/translator/cli/src/debug.cs	Mon Nov  9 16:15:21 2009
@@ -0,0 +1,179 @@
+using System;
+using System.IO;
+using System.Diagnostics;
+
+// this code is modeled after translator/c/src/debug.h
+namespace pypy.runtime
+{
+
+    public class Debug
+    {
+        public static void DEBUG_FATALERROR(string msg)
+        {
+            throw new Exception("debug_fatalerror: " + msg);
+        }
+    }
+
+    public class DebugPrint
+    {
+        static Stopwatch watch = null;
+        static TextWriter debug_file = null;
+        static int have_debug_prints = -1;
+        static bool debug_ready = false;
+        static bool debug_profile = false;
+        static string debug_prefix = null;
+
+        public static void close_file()
+        {
+            if (debug_file != null)
+                debug_file.Close();
+        }
+
+        public static bool HAVE_DEBUG_PRINTS()
+        {
+            if ((have_debug_prints & 1) != 0) {
+                debug_ensure_opened();
+                return true;
+            }
+            return false;
+        }
+
+        public static void DEBUG_START(string category)
+        {
+            debug_ensure_opened();
+            /* Enter a nesting level.  Nested debug_prints are disabled by
+               default because the following left shift introduces a 0 in the
+               last bit.  Note that this logic assumes that we are never going
+               to nest debug_starts more than 31 levels (63 on 64-bits). */
+            have_debug_prints <<= 1;
+            if (!debug_profile) {
+                /* non-profiling version */
+                if (debug_prefix == null || !category.StartsWith(debug_prefix)) {
+                    /* wrong section name, or no PYPYLOG at all, skip it */
+                    return;
+                }
+                /* else make this subsection active */
+                have_debug_prints |= 1;
+            }
+            display_startstop("{", "", category);
+        }
+
+        public static void DEBUG_STOP(string category)
+        {
+            if (debug_profile || (have_debug_prints & 1) != 0)
+                display_startstop("", "}", category);
+            have_debug_prints >>= 1;
+        }
+
+
+        static void setup_profiling()
+        {
+            watch = new Stopwatch();
+            watch.Start();
+        }
+
+        static void debug_open()
+        {
+            string filename = Environment.GetEnvironmentVariable("PYPYLOG");
+            if (filename != null && filename.Length > 0){
+                int colon = filename.IndexOf(':');
+                if (colon == -1) {
+                    /* PYPYLOG=filename --- profiling version */
+                    debug_profile = true;
+                }
+                else {
+                    /* PYPYLOG=prefix:filename --- conditional logging */
+                    debug_prefix = filename.Substring(0, colon);
+                    filename = filename.Substring(colon+1);
+                }
+                if (filename != "-")
+                    debug_file = File.CreateText(filename);
+            }
+            if (debug_file == null)
+                debug_file = System.Console.Error;
+            debug_ready = true;
+            setup_profiling();
+        }
+        
+        static void debug_ensure_opened() {
+            if (!debug_ready)
+                debug_open();
+        }
+
+        static long read_timestamp() {
+            return watch.ElapsedMilliseconds;
+        }
+
+        static void display_startstop(string prefix,
+                                      string postfix,
+                                      string category)
+        {
+            long timestamp = read_timestamp();
+            debug_file.WriteLine("[{0:X}] {1}{2}{3}", 
+                                 timestamp,
+                                 prefix,
+                                 category,
+                                 postfix);
+        }
+
+        // **************************************************
+        // debug_print family
+        // **************************************************
+        public static void DEBUG_PRINT(object a0)
+        {
+            if (HAVE_DEBUG_PRINTS())
+                debug_file.WriteLine("{0}", a0);
+        }
+
+        public static void DEBUG_PRINT(object a0, object a1)
+        {
+            if (HAVE_DEBUG_PRINTS())
+                debug_file.WriteLine("{0} {1}", a0, a1);
+        }
+
+        public static void DEBUG_PRINT(object a0, object a1, object a2)
+        {
+            if (HAVE_DEBUG_PRINTS())
+                debug_file.WriteLine("{0} {1} {2}", a0, a1, a2);
+        }
+
+        public static void DEBUG_PRINT(object a0, object a1, object a2, object a3)
+        {
+            if (HAVE_DEBUG_PRINTS())
+                debug_file.WriteLine("{0} {1} {2} {3}",
+                                        a0, a1, a2, a3);
+        }
+
+        public static void DEBUG_PRINT(object a0, object a1, object a2, object a3,
+                                       object a4)
+        {
+            if (HAVE_DEBUG_PRINTS())
+                debug_file.WriteLine("{0} {1} {2} {3} {4}", 
+                                        a0, a1, a2, a3, a4);
+        }
+
+        public static void DEBUG_PRINT(object a0, object a1, object a2, object a3,
+                                       object a4, object a5)
+        {
+            if (HAVE_DEBUG_PRINTS())
+                debug_file.WriteLine("{0} {1} {2} {3} {4} {5}", 
+                                        a0, a1, a2, a3, a4, a5);
+        }
+
+        public static void DEBUG_PRINT(object a0, object a1, object a2, object a3,
+                                       object a4, object a5, object a6)
+        {
+            if (HAVE_DEBUG_PRINTS())
+                debug_file.WriteLine("{0} {1} {2} {3} {4} {5} {6}", 
+                                        a0, a1, a2, a3, a4, a5, a6);
+        }
+
+        public static void DEBUG_PRINT(object a0, object a1, object a2, object a3,
+                                       object a4, object a5, object a6, object a7)
+        {
+            if (HAVE_DEBUG_PRINTS())
+                debug_file.WriteLine("{0} {1} {2} {3} {4} {5} {6} {7}", 
+                                        a0, a1, a2, a3, a4, a5, a6, a7);
+        }
+    }
+}

Modified: pypy/trunk/pypy/translator/cli/src/pypylib.cs
==============================================================================
--- pypy/trunk/pypy/translator/cli/src/pypylib.cs	(original)
+++ pypy/trunk/pypy/translator/cli/src/pypylib.cs	Mon Nov  9 16:15:21 2009
@@ -382,31 +382,6 @@
     public class Utils
     {
 
-        public static void debug_fatalerror(string msg)
-        {
-          throw new Exception("debug_fatalerror: " + msg);
-        }
-
-        public static void debug_print(object a)
-        {
-            Console.Error.WriteLine(a);
-        }
-
-        public static void debug_print(object a, object b)
-        {
-            Console.Error.WriteLine("{0} {1}", a, b);
-        }
-
-        public static void debug_print(object a, object b, object c)
-        {
-            Console.Error.WriteLine("{0} {1} {2}", a, b, c);
-        }
-
-        public static void debug_print(object a, object b, object c, object d)
-        {
-            Console.Error.WriteLine("{0} {1} {2} {3}", a, b, c, d);
-        }
-
         public static DynamicMethod CreateDynamicMethod(string name, Type res, Type[] args)
         {
             return new DynamicMethod(name, res, args, typeof(Utils).Module);

Added: pypy/trunk/pypy/translator/cli/test/test_standalone.py
==============================================================================
--- (empty file)
+++ pypy/trunk/pypy/translator/cli/test/test_standalone.py	Mon Nov  9 16:15:21 2009
@@ -0,0 +1,54 @@
+import subprocess
+from pypy.translator.c.test.test_standalone import TestStandalone as CTestStandalone
+from pypy.annotation.listdef import s_list_of_strings
+from pypy.translator.translator import TranslationContext
+from pypy.translator.cli.sdk import SDK
+
+class CliStandaloneBuilder(object):
+
+    def __init__(self, translator, entry_point, config):
+        self.translator = translator
+        self.entry_point = entry_point
+        self.config = config
+        self.exe_name = None
+
+    def compile(self):
+        from pypy.translator.cli.test.runtest import _build_gen_from_graph
+        graph = self.translator.graphs[0]
+        gen = _build_gen_from_graph(graph, self.translator, standalone=True)
+        gen.generate_source()
+        self.exe_name = gen.build_exe()
+
+    def cmdexec(self, args='', env=None, err=False):
+        assert self.exe_name
+        stdout, stderr, retval = self.run(args, env=env)
+        if retval != 0:
+            raise Exception("Returned %d" % (retval,))
+        if err:
+            return stdout, stderr
+        return stdout
+
+    def run(self, args, env):
+        arglist = SDK.runtime() + [self.exe_name] + map(str, args)
+        env = env.copy()
+        env['LANG'] = 'C'
+        mono = subprocess.Popen(arglist, stdout=subprocess.PIPE,
+                                stderr=subprocess.PIPE, env=env)
+        stdout, stderr = mono.communicate()
+        retval = mono.wait()
+        return stdout, stderr, retval
+
+
+class TestStandalone(object):
+    config = None
+
+    def compile(self, entry_point):
+        t = TranslationContext(self.config)
+        t.buildannotator().build_types(entry_point, [s_list_of_strings])
+        t.buildrtyper(type_system='ootype').specialize()
+
+        cbuilder = CliStandaloneBuilder(t, entry_point, t.config)
+        cbuilder.compile()
+        return t, cbuilder
+
+    test_debug_print_start_stop = CTestStandalone.test_debug_print_start_stop.im_func



More information about the Pypy-commit mailing list