[pypy-svn] r35744 - in pypy/dist/pypy/translator: cli oosupport

antocuni at codespeak.net antocuni at codespeak.net
Thu Dec 14 16:16:39 CET 2006


Author: antocuni
Date: Thu Dec 14 16:16:38 2006
New Revision: 35744

Modified:
   pypy/dist/pypy/translator/cli/function.py
   pypy/dist/pypy/translator/cli/metavm.py
   pypy/dist/pypy/translator/oosupport/function.py
Log:
Added a generic way to get unique labels inside a function, a some
refactoring to use it.



Modified: pypy/dist/pypy/translator/cli/function.py
==============================================================================
--- pypy/dist/pypy/translator/cli/function.py	(original)
+++ pypy/dist/pypy/translator/cli/function.py	Thu Dec 14 16:16:38 2006
@@ -58,16 +58,8 @@
             self._setup_link(link)
 
 class LastExceptionHandler(object):
-    catch_label_count = 0
     in_try = False
 
-    def next_catch_label(self):
-        self.catch_label_count += 1
-        return self.catch_label()
-
-    def catch_label(self):
-        return '__catch_%d' % self.catch_label_count
-
     def begin_try(self):
         self.in_try = True
         self.ilasm.opcode('// begin_try')
@@ -79,16 +71,16 @@
         self.in_try = False
 
     def begin_catch(self, llexitcase):
-        self.ilasm.label(self.catch_label())
+        self.ilasm.label(self.current_label('catch'))
         ll_meta_exc = llexitcase
         ll_exc = ll_meta_exc._inst.class_._INSTANCE
         cts_exc = self.cts.lltype_to_cts(ll_exc, False)
         self.ilasm.opcode('ldsfld', 'object last_exception')
         self.isinstance(cts_exc)
         self.ilasm.opcode('dup')
-        self.ilasm.opcode('brtrue.s', 6) # ??
+        self.ilasm.opcode('brtrue.s', 6)
         self.ilasm.opcode('pop')
-        self.ilasm.opcode('br', self.next_catch_label())
+        self.ilasm.opcode('br', self.next_label('catch'))
         # here is the target of the above brtrue.s
         self.ilasm.opcode('ldnull')
         self.ilasm.opcode('stsfld', 'object last_exception')
@@ -113,6 +105,10 @@
                 self.store(link.last_exc_value)
             self._setup_link(link)
 
+    def before_last_blocks(self):
+        self.ilasm.label(self.current_label('catch'))
+        self.ilasm.opcode('nop')
+
     def render_raise_block(self, block):
         exc = block.inputargs[1]
         self.load(exc)
@@ -169,13 +165,6 @@
 
 class Function(ExceptionHandler, OOFunction, Node, CLIBaseGenerator):
 
-    def next_catch_label(self):
-        self.catch_label_count += 1
-        return self.catch_label()
-
-    def catch_label(self):
-        return '__catch_%d' % self.catch_label_count
-
     def __init__(self, *args, **kwargs):
         OOFunction.__init__(self, *args, **kwargs)
         self._set_args()
@@ -202,13 +191,6 @@
         self.ilasm.begin_function(self.name, args, returntype, self.is_entrypoint, meth_type)        
         self.ilasm.locals(self.locals)
 
-    def before_last_blocks(self):
-        # This is only executed when using LastExceptionHandler.
-        # Need to be deleted when we will use a saner approach.
-        if hasattr(self, 'catch_label'):
-            self.ilasm.label(self.catch_label())
-            self.ilasm.opcode('nop')
-
     def end_render(self):
         self.ilasm.end_function()
 

Modified: pypy/dist/pypy/translator/cli/metavm.py
==============================================================================
--- pypy/dist/pypy/translator/cli/metavm.py	(original)
+++ pypy/dist/pypy/translator/cli/metavm.py	Thu Dec 14 16:16:38 2006
@@ -154,7 +154,8 @@
         self.mapping = mapping
 
     def render(self, generator, op):
-        if hasattr(self, 'catch_label'):
+        from pypy.translator.cli.function import LastExceptionHandler
+        if isinstance(generator, LastExceptionHandler):
             self.render_last(generator, op)
         else:
             self.render_native(generator, op)

Modified: pypy/dist/pypy/translator/oosupport/function.py
==============================================================================
--- pypy/dist/pypy/translator/oosupport/function.py	(original)
+++ pypy/dist/pypy/translator/oosupport/function.py	Thu Dec 14 16:16:38 2006
@@ -3,6 +3,7 @@
 from pypy.translator.oosupport.metavm import InstructionList
 
 class Function(object):
+
     def __init__(self, db, graph, name = None, is_method = False, is_entrypoint = False):
         self.db = db
         self.cts = db.genoo.TypeSystem(db)
@@ -11,12 +12,22 @@
         self.is_method = is_method
         self.is_entrypoint = is_entrypoint
         self.generator = None # set in render()
+        self.label_counters = {}
         
         # If you want to enumerate args/locals before processing, then
         # add these functions into your __init__() [they are defined below]
         #   self._set_args()
         #   self._set_locals()
 
+    def current_label(self, prefix='label'):
+        current = self.label_counters.get(prefix, 0)
+        return '__%s_%d' % (prefix, current)
+
+    def next_label(self, prefix='label'):
+        current = self.label_counters.get(prefix, 0)
+        self.label_counters[prefix] = current+1
+        return self.current_label(prefix)
+
     def get_name(self):
         return self.name
 



More information about the Pypy-commit mailing list