[pypy-svn] r28890 - in pypy/dist/pypy/translator: . goal test

pedronis at codespeak.net pedronis at codespeak.net
Fri Jun 16 17:27:58 CEST 2006


Author: pedronis
Date: Fri Jun 16 17:27:56 2006
New Revision: 28890

Added:
   pypy/dist/pypy/translator/test/test_driver.py   (contents, props changed)
Modified:
   pypy/dist/pypy/translator/driver.py
   pypy/dist/pypy/translator/goal/translate.py
   pypy/dist/pypy/translator/interactive.py
   pypy/dist/pypy/translator/test/test_interactive.py
Log:
remove .ootype() from driver and interactive.Translation. now choosing a backend through the backend options
or using the 'type_system' option decides the kind of rtyping needed.



Modified: pypy/dist/pypy/translator/driver.py
==============================================================================
--- pypy/dist/pypy/translator/driver.py	(original)
+++ pypy/dist/pypy/translator/driver.py	Fri Jun 16 17:27:56 2006
@@ -14,7 +14,7 @@
 py.log.setconsumer("translation", ansi_log)
 
 
-DEFAULT_OPTIONS = optparse.Values(defaults={
+DEFAULT_OPTIONS = {
   'gc': 'ref',
 
   'thread': False, # influences GC policy
@@ -23,11 +23,14 @@
   'debug': True,
   'insist': False,
   'backend': 'c',
+  'type_system': None,
   'lowmem': False,
   'fork_before': None,
   'raisingop2direct_call' : False,
   'merge_if_blocks': True
-})
+}
+
+_default_options = optparse.Values(defaults=DEFAULT_OPTIONS)
 
 def taskdef(taskfunc, deps, title, new_state=None, expected_states=[], idemp=False):
     taskfunc.task_deps = deps
@@ -40,6 +43,14 @@
 # TODO:
 # sanity-checks using states
 
+_BACKEND_TO_TYPESYSTEM = {
+    'c': 'lltype',
+    'llvm': 'lltype'
+}
+
+def backend_to_typesystem(backend):
+    return _BACKEND_TO_TYPESYSTEM.get(backend, 'ootype')
+
 class TranslationDriver(SimpleTaskEngine):
 
     def __init__(self, options=None, default_goal=None, disable=[],
@@ -49,7 +60,7 @@
         self.log = log
 
         if options is None:
-            options = DEFAULT_OPTIONS
+            options = _default_options
         self.options = options
         self.exe_name = exe_name
         self.extmod_name = extmod_name
@@ -65,30 +76,72 @@
         
         self.default_goal = default_goal
 
+        self.exposed = []
+
         # expose tasks
-        def expose_task(task):
-            backend_goal, = self.backend_select_goals([task])
+        def expose_task(task, backend_goal=None):
+            if backend_goal is None:
+                backend_goal = task
             def proc():
                 return self.proceed(backend_goal)
+            self.exposed.append(task)
             setattr(self, task, proc)
 
-        if self.options.backend:
-            for task in ('annotate', 'rtype', 'backendopt', 'source', 'compile', 'run', 'llinterpret'):
-                expose_task(task)
-        else:
-            for task in self.tasks:
-                expose_task(task)
+        backend, ts = self.get_backend_and_type_system()
+        for task in self.tasks:
+            explicit_task = task
+            parts = task.split('_')
+            if len(parts) == 1:
+                if task in ('annotate'):
+                    expose_task(task)
+            else:
+                task, postfix = parts
+                if task in ('rtype', 'backendopt', 'llinterpret'):
+                    if ts:
+                        if ts == postfix:
+                            expose_task(task, explicit_task)                        
+                    else:
+                        expose_task(explicit_task)
+                elif task in ('source', 'compile', 'run'):
+                    if backend:
+                        if backend == postfix:
+                            expose_task(task, explicit_task)
+                    elif ts:
+                        if ts == backend_to_typesystem(postfix):
+                            expose_task(explicit_task)
+                    else:
+                        expose_task(explicit_task)
+
+    def get_backend_and_type_system(self):
+        type_system = None
+        backend = None
+        opts = self.options
+        if opts.type_system:
+            type_system = opts.type_system
+        if opts.backend:
+            backend = opts.backend
+            ts = backend_to_typesystem(backend)
+            if type_system:
+                if ts != type_system:
+                    raise ValueError, ("incosistent type-system and backend:"
+                                       " %s and %s" % (type_system, backend))
+            else:
+                type_system = ts
+        return backend, type_system
 
     def backend_select_goals(self, goals):
-        backend = self.options.backend
+        backend, ts = self.get_backend_and_type_system()
+        postfixes = [''] + ['_'+p for p in (backend, ts) if p]
         l = []
         for goal in goals:
-            if goal in self.tasks:
-                l.append(goal)
-            elif backend:
-                goal = "%s_%s" % (goal, backend)
-                assert goal in self.tasks
-                l.append(goal)
+            for postfix in postfixes:
+                cand = "%s%s" % (goal, postfix)
+                if cand in self.tasks:
+                    new_goal = cand
+                    break
+            else:
+                raise Exception, "cannot infer complete goal from: %r" % goal 
+            l.append(new_goal)
         return l
 
     def disable(self, to_disable):
@@ -185,41 +238,45 @@
 
 
 
-    def task_rtype(self):
+    def task_rtype_lltype(self):
         opt = self.options
-        rtyper = self.translator.buildrtyper()
+        rtyper = self.translator.buildrtyper(type_system='lltype')
         rtyper.specialize(dont_simplify_again=True,
                           crash_on_first_typeerror=not opt.insist)
     #
-    task_rtype = taskdef(task_rtype, ['annotate'], "RTyping")
+    task_rtype_lltype = taskdef(task_rtype_lltype, ['annotate'], "RTyping")
+    RTYPE = 'rtype_lltype'
 
-    def task_ootype(self):
+    def task_rtype_ootype(self):
         # Maybe type_system should simply be an option used in task_rtype
         opt = self.options
         rtyper = self.translator.buildrtyper(type_system="ootype")
         rtyper.specialize(dont_simplify_again=True,
                           crash_on_first_typeerror=not opt.insist)
     #
-    task_ootype = taskdef(task_ootype, ['annotate'], "ootyping")
+    task_rtype_ootype = taskdef(task_rtype_ootype, ['annotate'], "ootyping")
+    OOTYPE = 'rtype_ootype'
 
-    def task_backendopt(self):
+    def task_backendopt_lltype(self):
         from pypy.translator.backendopt.all import backend_optimizations
         opt = self.options
         backend_optimizations(self.translator,
                               raisingop2direct_call_all=opt.raisingop2direct_call,
                               merge_if_blocks_to_switch=opt.merge_if_blocks)
     #
-    task_backendopt = taskdef(task_backendopt, 
-                                        ['rtype'], "Back-end optimisations") 
+    task_backendopt_lltype = taskdef(task_backendopt_lltype, 
+                                        [RTYPE], "Back-end optimisations")
+    BACKENDOPT = 'backendopt_lltype'
 
-    def task_stackcheckinsertion(self):
+    def task_stackcheckinsertion_lltype(self):
         from pypy.translator.transform import insert_ll_stackcheck
         insert_ll_stackcheck(self.translator)
         
-    task_stackcheckinsertion = taskdef(
-        task_stackcheckinsertion, 
-        ['?backendopt', 'rtype', 'annotate'], 
+    task_stackcheckinsertion_lltype = taskdef(
+        task_stackcheckinsertion_lltype, 
+        ['?'+BACKENDOPT, RTYPE, 'annotate'], 
         "inserting stack checks")
+    STACKCHECKINSERTION = 'stackcheckinsertion_lltype'
 
     def task_database_c(self):
         translator = self.translator
@@ -245,7 +302,7 @@
         self.database = database
     #
     task_database_c = taskdef(task_database_c, 
-                            ['stackcheckinsertion', '?backendopt', '?rtype', '?annotate'], 
+                            [STACKCHECKINSERTION, '?'+BACKENDOPT, RTYPE, '?annotate'], 
                             "Creating database for generating c source")
     
     def task_source_c(self):  # xxx messy
@@ -298,7 +355,7 @@
                          "Running compiled c source",
                          idemp=True)
 
-    def task_llinterpret(self):
+    def task_llinterpret_lltype(self):
         from pypy.rpython.llinterp import LLInterpreter
         py.log.setconsumer("llinterp operation", None)
         
@@ -312,9 +369,9 @@
 
         log.llinterpret.event("result -> %s" % v)
     #
-    task_llinterpret = taskdef(task_llinterpret, 
-                               ['stackcheckinsertion', '?backendopt', 'rtype'], 
-                               "LLInterpreting")
+    task_llinterpret_lltype = taskdef(task_llinterpret_lltype, 
+                                      [STACKCHECKINSERTION, '?'+BACKENDOPT, RTYPE], 
+                                      "LLInterpreting")
 
     def task_source_llvm(self):
         translator = self.translator
@@ -332,7 +389,7 @@
         self.log.info("written: %s" % (llvm_filename,))
     #
     task_source_llvm = taskdef(task_source_llvm, 
-                               ['stackcheckinsertion', 'backendopt', 'rtype'], 
+                               [STACKCHECKINSERTION, BACKENDOPT, RTYPE], 
                                "Generating llvm source")
 
     def task_compile_llvm(self):
@@ -360,7 +417,7 @@
         self.gen = GenCL(self.translator, self.entry_point)
         filename = self.gen.emitfile()
         self.log.info("Wrote %s" % (filename,))
-    task_source_cl = taskdef(task_source_cl, ['ootype'],
+    task_source_cl = taskdef(task_source_cl, [OOTYPE],
                              'Generating Common Lisp source')
 
     def task_compile_cl(self):
@@ -378,7 +435,7 @@
         self.gen = GenSqueak(dir, self.translator)
         filename = self.gen.gen()
         self.log.info("Wrote %s" % (filename,))
-    task_source_squeak = taskdef(task_source_squeak, ['ootype'],
+    task_source_squeak = taskdef(task_source_squeak, [OOTYPE],
                              'Generating Squeak source')
 
     def task_compile_squeak(self):
@@ -398,7 +455,7 @@
         filename = self.gen.write_source()
         self.log.info("Wrote %s" % (filename,))
     task_source_js = taskdef(task_source_js, 
-                        ['stackcheckinsertion', 'backendopt', 'rtype'],
+                        [OOTYPE],
                         'Generating Javascript source')
 
     def task_compile_js(self):
@@ -419,7 +476,7 @@
         self.gen = GenCli(udir, self.translator, TestEntryPoint(entry_point_graph, False))
         filename = self.gen.generate_source()
         self.log.info("Wrote %s" % (filename,))
-    task_source_cli = taskdef(task_source_cli, ['ootype'],
+    task_source_cli = taskdef(task_source_cli, [OOTYPE],
                              'Generating CLI source')
 
     def task_compile_cli(self):
@@ -454,7 +511,7 @@
         if args is None:
             args = []
         if options is None:
-            options = DEFAULT_OPTIONS
+            options = _default_options
 
         driver = TranslationDriver(options, default_goal, disable)
         target = targetspec_dic['target']
@@ -478,6 +535,8 @@
     def prereq_checkpt_rtype(self):
         assert 'pypy.rpython.rmodel' not in sys.modules, (
             "cannot fork because the rtyper has already been imported")
+    prereq_checkpt_rtype_lltype = prereq_checkpt_rtype
+    prereq_checkpt_rtype_ootype = prereq_checkpt_rtype    
 
     # checkpointing support
     def _event(self, kind, goal, func):

Modified: pypy/dist/pypy/translator/goal/translate.py
==============================================================================
--- pypy/dist/pypy/translator/goal/translate.py	(original)
+++ pypy/dist/pypy/translator/goal/translate.py	Fri Jun 16 17:27:56 2006
@@ -103,6 +103,7 @@
 
     'gc': 'boehm',
     'backend': 'c',
+    'type_system': None,
     'stackless': False,
     'raisingop2direct_call' : False,
     'merge_if_blocks': True,

Modified: pypy/dist/pypy/translator/interactive.py
==============================================================================
--- pypy/dist/pypy/translator/interactive.py	(original)
+++ pypy/dist/pypy/translator/interactive.py	Fri Jun 16 17:27:56 2006
@@ -4,23 +4,10 @@
 from pypy.translator.translator import TranslationContext
 from pypy.translator import driver
 
-DEFAULT_OPTIONS = {
-  'gc': 'ref',
-
-  'thread': False, # influences GC policy
-
-  'stackless': False,
-  'debug': True,
-  'insist': False,
-   
-   'backend': None,
-   'lowmem': False,
-
-   'fork_before': None,
-
-   'raisingop2direct_call' : False,
-   'merge_if_blocks': True
-}
+DEFAULT_OPTIONS = driver.DEFAULT_OPTIONS.copy()
+DEFAULT_OPTIONS.update({
+  'backend': None,
+})
 
 class Translation(object):
 
@@ -48,10 +35,10 @@
 
     GOAL_USES_OPTS = {
         'annotate': ['debug'],
-        'rtype': ['insist'],
-        'ootype': [],
-        'backendopt': ['raisingop2direct_call', 'merge_if_blocks'],
-        'stackcheckinsertion': [],
+        'rtype_lltype': ['insist'],
+        'rtype_ootype': ['insist'],
+        'backendopt_lltype': ['raisingop2direct_call', 'merge_if_blocks'],
+        'stackcheckinsertion_lltype': [],
         'database_c': ['gc', 'stackless'],
         'source_llvm': [],
         'source_js': [],
@@ -114,12 +101,26 @@
                 setattr(self.driver.options, optname, value)
                 self.frozen_options[optname] = True
 
+    def ensure_opt(self, name, value=None, fallback=None):
+        if value is not None:
+            self.update_options(None, {name: value})
+        elif fallback is not None and name not in self.frozen_options:
+            self.update_options(None, {name: fallback})
+        val =  getattr(self.driver.options, name)
+        if val is None:
+            raise Exception("the %r option should have been specified at this point" % name)
+        return val
+
+    def ensure_type_system(self, type_system=None):
+        if type_system is None:
+            backend = self.driver.options.backend
+            if backend is not None:
+                type_system = driver.backend_to_typesystem(backend)
+        return self.ensure_opt('type_system', type_system, 'lltype')
+        
     def ensure_backend(self, backend=None):
-        if backend is not None:
-            self.update_options(None, {'backend': backend})
-        if self.driver.options.backend is None:
-            raise Exception("a backend should have been specified at this point")
-        backend = self.driver.options.backend
+        backend = self.ensure_opt('backend', backend)
+        self.ensure_type_system()
         if backend == 'llvm':
             self.update_options(None, {'gc': 'boehm'})
         return backend
@@ -134,26 +135,20 @@
         self.update_options(argtypes, kwds)
         return self.driver.annotate()
 
-    def rtype(self, argtypes=None, **kwds):
-        self.update_options(argtypes, kwds)
-        return self.driver.rtype()
+    # type system dependent
 
-    def ootype(self, argtypes=None, **kwds):
+    def rtype(self, argtypes=None, **kwds):
         self.update_options(argtypes, kwds)
-        return self.driver.ootype()
-
-    # backend depedent
+        ts = self.ensure_type_system()
+        return getattr(self.driver, 'rtype_'+ts)()        
 
     def backendopt(self, argtypes=None, **kwds):
         self.update_options(argtypes, kwds)
-        #self.ensure_backend()
-        self.driver.backendopt()
-
-    def backendopt_c(self, argtypes=None, **kwds):
-        self.update_options(argtypes, kwds)
-        self.ensure_backend('c')
-        self.driver.backendopt()
+        ts = self.ensure_type_system('lltype')
+        return getattr(self.driver, 'backendopt_'+ts)()                
             
+    # backend depedent
+
     def source(self, argtypes=None, **kwds):
         self.update_options(argtypes, kwds)
         backend = self.ensure_backend()

Added: pypy/dist/pypy/translator/test/test_driver.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/test/test_driver.py	Fri Jun 16 17:27:56 2006
@@ -0,0 +1,60 @@
+import py
+
+from pypy.translator.driver import TranslationDriver, DEFAULT_OPTIONS
+import optparse
+
+def cmpl(l1, l2):
+    l1 = list(l1)
+    l2 = list(l2)
+    l1.sort()
+    l2.sort()
+    return l1 == l2
+
+def test_ctr():
+    td = TranslationDriver()
+
+    assert cmpl(td.exposed,
+                ['annotate', 'backendopt', 'llinterpret', 'rtype', 'source', 'compile', 'run'])
+
+    assert td.backend_select_goals(['compile_c']) == ['compile_c']
+    assert td.backend_select_goals(['compile']) == ['compile_c']
+    assert td.backend_select_goals(['rtype']) == ['rtype_lltype']
+    assert td.backend_select_goals(['rtype_lltype']) == ['rtype_lltype']
+    assert td.backend_select_goals(['backendopt']) == ['backendopt_lltype']
+    assert td.backend_select_goals(['backendopt_lltype']) == ['backendopt_lltype']          
+
+    d = DEFAULT_OPTIONS.copy()
+    d['backend'] = None
+    td = TranslationDriver(options=optparse.Values(defaults=d))
+
+    assert td.backend_select_goals(['compile_c']) == ['compile_c']
+    py.test.raises(Exception, td.backend_select_goals, ['compile'])
+    py.test.raises(Exception, td.backend_select_goals, ['rtype'])
+    assert td.backend_select_goals(['rtype_lltype']) == ['rtype_lltype']          
+    py.test.raises(Exception, td.backend_select_goals, ['backendopt'])
+    assert td.backend_select_goals(['backendopt_lltype']) == ['backendopt_lltype']          
+
+
+    assert cmpl(td.exposed,
+                ['annotate', 'backendopt_lltype', 'llinterpret_lltype',
+                 'rtype_ootype', 'rtype_lltype',
+                 'source_cl', 'source_js', 'source_squeak', 'source_cli', 'source_c',
+                 'source_llvm', 'compile_cl', 'compile_cli', 'compile_c', 'compile_squeak',
+                 'compile_llvm', 'compile_js', 'run_cl', 'run_squeak', 'run_llvm',
+                 'run_c', 'run_js', 'run_cli'])
+
+    d = DEFAULT_OPTIONS.copy()
+    d['backend'] = None
+    d['type_system'] = 'lltype'
+    td = TranslationDriver(options=optparse.Values(defaults=d))
+
+    assert td.backend_select_goals(['compile_c']) == ['compile_c']
+    py.test.raises(Exception, td.backend_select_goals, ['compile'])
+    assert td.backend_select_goals(['rtype_lltype']) == ['rtype_lltype']    
+    assert td.backend_select_goals(['rtype']) == ['rtype_lltype']    
+    assert td.backend_select_goals(['backendopt']) == ['backendopt_lltype']
+    assert td.backend_select_goals(['backendopt_lltype']) == ['backendopt_lltype']          
+
+    assert cmpl(td.exposed,
+                ['annotate', 'backendopt', 'llinterpret', 'rtype', 'source_c', 'source_llvm',
+                 'compile_c', 'compile_llvm', 'run_llvm', 'run_c'])

Modified: pypy/dist/pypy/translator/test/test_interactive.py
==============================================================================
--- pypy/dist/pypy/translator/test/test_interactive.py	(original)
+++ pypy/dist/pypy/translator/test/test_interactive.py	Fri Jun 16 17:27:56 2006
@@ -33,10 +33,14 @@
     s = t.annotate()
     t.rtype()
 
+    assert 'rtype_lltype' in t.driver.done    
+
     t = Translation(f)
     s = t.annotate([int, int])
     t.rtype()
 
+    assert 'rtype_lltype' in t.driver.done        
+
     t = Translation(f, [int, int])
     t.annotate()
     py.test.raises(Exception, "t.rtype([int, int],debug=False)")
@@ -47,13 +51,14 @@
 
     t = Translation(f, [int, int], backend='c')
     t.backendopt()
-
-    t = Translation(f, [int, int])
-    t.backendopt_c()
+    
+    assert 'backendopt_lltype' in t.driver.done
 
     t = Translation(f, [int, int])
     t.backendopt()
 
+    assert 'backendopt_lltype' in t.driver.done
+
 def test_simple_source():
     def f(x, y):
         return x,y
@@ -112,3 +117,44 @@
 
     res = t_f(2,3)
     assert res == 5
+
+def test_simple_rtype_with_type_system():
+
+    def f(x,y):
+        return x+y
+
+    t = Translation(f, [int, int])
+    s = t.annotate()
+    t.rtype(type_system='lltype')
+
+    assert 'rtype_lltype' in t.driver.done    
+
+    t = Translation(f, [int, int])
+    s = t.annotate()
+    t.rtype(type_system='ootype')
+    assert 'rtype_ootype' in t.driver.done        
+
+    t = Translation(f, type_system='ootype')
+    s = t.annotate([int, int])
+    t.rtype()
+    assert 'rtype_ootype' in t.driver.done    
+
+    t = Translation(f)
+    s = t.annotate([int, int])
+    t.rtype(backend='squeak')
+    assert 'rtype_ootype' in t.driver.done
+
+
+    t = Translation(f, backend='squeak', type_system='ootype')
+    s = t.annotate([int, int])
+    t.rtype()
+    assert 'rtype_ootype' in t.driver.done        
+
+    t = Translation(f, type_system='lltype')
+    s = t.annotate([int, int])
+    py.test.raises(Exception, "t.rtype(backend='squeak')")
+
+    t = Translation(f, backend='squeak')
+    s = t.annotate([int, int])
+    py.test.raises(Exception, "t.rtype(type_system='lltype')")
+



More information about the Pypy-commit mailing list