nitg-s: moved type tables building from coloring to separate compiler
authorAlexandre Terrasa <alexandre@moz-code.org>
Thu, 7 Feb 2013 16:34:41 +0000 (11:34 -0500)
committerAlexandre Terrasa <alexandre@moz-code.org>
Mon, 4 Mar 2013 18:20:00 +0000 (13:20 -0500)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

src/coloring.nit
src/separate_compiler.nit

index bf591cb..adcc420 100644 (file)
@@ -157,29 +157,6 @@ class TypeColoring
 
        init(mainmodule: MModule) do self.mmodule = mainmodule
 
-       # Build type tables
-       fun build_type_tables(mtypes: Set[T], colors: Map[T, Int]): Map[T, Array[nullable T]] do
-               var tables = new HashMap[T, Array[nullable T]]
-
-               for mtype in mtypes do
-                       var table = new Array[nullable T]
-                       var supers = new HashSet[T]
-                       supers.add_all(self.super_elements(mtype, mtypes))
-                       supers.add(mtype)
-                       for sup in supers do
-                               var color = colors[sup]
-                               if table.length <= color then
-                                       for i in [table.length .. color[ do
-                                               table[i] = null
-                                       end
-                               end
-                               table[color] = sup
-                       end
-                       tables[mtype] = table
-               end
-               return tables
-       end
-
        redef fun super_elements(element, elements) do return self.mmodule.super_mtypes(element, elements)
        redef fun is_element_mi(element, elements) do return self.super_elements(element, elements).length > 1
        redef fun sub_elements(element, elements) do do return self.mmodule.sub_mtypes(element, elements)
@@ -217,30 +194,6 @@ abstract class TypePerfectHashing
                return self.coloration_result
        end
 
-       # Build type tables
-       fun hash_type_tables(mtypes: Set[T], ids: Map[T, Int], masks: Map[T, Int]): Map[T, Array[nullable T]] do
-               var tables = new HashMap[T, Array[nullable T]]
-
-               for mtype in mtypes do
-                       var table = new Array[nullable T]
-                       var supers = new HashSet[T]
-                       supers.add_all(self.super_elements(mtype, mtypes))
-                       supers.add(mtype)
-
-                       for sup in supers do
-                               var color = phash(ids[sup], masks[mtype])
-                               if table.length <= color then
-                                       for i in [table.length .. color[ do
-                                               table[i] = null
-                                       end
-                               end
-                               table[color] = sup
-                       end
-                       tables[mtype] = table
-               end
-               return tables
-       end
-
        private fun compute_mask(mtypes: Set[T], ids: Map[T, Int]): Int do
                var mask = 0
                loop
index 2efaa3e..17aef78 100644 (file)
@@ -303,27 +303,74 @@ class SeparateCompiler
                if modelbuilder.toolcontext.opt_bm_typing.value then
                        var type_coloring = new NaiveTypeColoring(self.mainmodule)
                        self.type_colors = type_coloring.colorize(mtypes)
-                       self.type_tables = type_coloring.build_type_tables(mtypes, type_colors)
+                       self.type_tables = self.build_type_tables(mtypes, type_colors, type_coloring)
                else if modelbuilder.toolcontext.opt_phmod_typing.value then
                        var type_coloring = new TypeModPerfectHashing(self.mainmodule)
                        self.type_colors = type_coloring.compute_masks(mtypes, typeids)
-                       self.type_tables = type_coloring.hash_type_tables(mtypes, typeids, type_colors)
+                       self.type_tables = self.hash_type_tables(mtypes, typeids, type_colors, type_coloring)
                        self.header.add_decl("#define HASH(mask, id) ((mask)%(id))")
                else if modelbuilder.toolcontext.opt_phand_typing.value then
                        var type_coloring = new TypeAndPerfectHashing(self.mainmodule)
                        self.type_colors = type_coloring.compute_masks(mtypes, typeids)
-                       self.type_tables = type_coloring.hash_type_tables(mtypes, typeids, type_colors)
+                       self.type_tables = self.hash_type_tables(mtypes, typeids, type_colors, type_coloring)
                        self.header.add_decl("#define HASH(mask, id) ((mask)&(id))")
                else
                        var type_coloring = new TypeColoring(self.mainmodule)
                        self.type_colors = type_coloring.colorize(mtypes)
-                       self.type_tables = type_coloring.build_type_tables(mtypes, type_colors)
+                       self.type_tables = self.build_type_tables(mtypes, type_colors, type_coloring)
                end
 
 
                return mtypes
        end
 
+       # Build type tables
+       fun build_type_tables(mtypes: Set[MType], colors: Map[MType, Int], colorer: TypeColoring): Map[MType, Array[nullable MType]] do
+               var tables = new HashMap[MType, Array[nullable MType]]
+
+               for mtype in mtypes do
+                       var table = new Array[nullable MType]
+                       var supers = new HashSet[MType]
+                       supers.add_all(colorer.super_elements(mtype, mtypes))
+                       supers.add(mtype)
+                       for sup in supers do
+                               var color = colors[sup]
+                               if table.length <= color then
+                                       for i in [table.length .. color[ do
+                                               table[i] = null
+                                       end
+                               end
+                               table[color] = sup
+                       end
+                       tables[mtype] = table
+               end
+               return tables
+       end
+
+       # Build type tables
+       fun hash_type_tables(mtypes: Set[MType], ids: Map[MType, Int], masks: Map[MType, Int], colorer: TypePerfectHashing): Map[MType, Array[nullable MType]] do
+               var tables = new HashMap[MType, Array[nullable MType]]
+
+               for mtype in mtypes do
+                       var table = new Array[nullable MType]
+                       var supers = new HashSet[MType]
+                       supers.add_all(colorer.super_elements(mtype, mtypes))
+                       supers.add(mtype)
+
+                       for sup in supers do
+                               var color = colorer.phash(ids[sup], masks[mtype])
+                               if table.length <= color then
+                                       for i in [table.length .. color[ do
+                                               table[i] = null
+                                       end
+                               end
+                               table[color] = sup
+                       end
+                       tables[mtype] = table
+               end
+               return tables
+       end
+
        protected fun compile_unanchored_tables(mtypes: Set[MType]) do
                # Unanchored_tables is used to perform a type resolution at runtime in O(1)