nitg-s: moved class tables building from coloring to separate erased compiler
authorAlexandre Terrasa <alexandre@moz-code.org>
Thu, 7 Feb 2013 16:41:14 +0000 (11:41 -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_erasure_compiler.nit

index adcc420..84deb3d 100644 (file)
@@ -243,29 +243,6 @@ class ClassColoring
 
        init(mainmodule: MModule) do self.mmodule = mainmodule
 
-       # Build type tables
-       fun build_type_tables(mclasses: Set[T], colors: Map[T, Int]): Map[T, Array[nullable T]] do
-               var tables = new HashMap[T, Array[nullable T]]
-
-               for mclasse in mclasses do
-                       var table = new Array[nullable T]
-                       var supers = new HashSet[T]
-                       supers.add_all(self.super_elements(mclasse, mclasses))
-                       supers.add(mclasse)
-                       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[mclasse] = table
-               end
-               return tables
-       end
-
        redef fun super_elements(element, elements) do return self.mmodule.super_mclasses(element)
        fun parent_elements(element: MClass): Set[MClass] do return self.mmodule.parent_mclasses(element)
        redef fun is_element_mi(element, elements) do return self.parent_elements(element).length > 1
@@ -309,30 +286,6 @@ abstract class ClassPerfectHashing
                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 a46bc85..89aa8db 100644 (file)
@@ -15,7 +15,7 @@
 # Separate compilation of a Nit program with generic type erasure
 module separate_erasure_compiler
 
-import separate_compiler
+intrude import separate_compiler
 
 # Add separate erased compiler specific options
 redef class ToolContext
@@ -85,7 +85,7 @@ class SeparateErasureCompiler
 
                        var class_coloring = new ClassModPerfectHashing(mainmodule)
                        self.class_colors = class_coloring.compute_masks(mclasses, class_ids)
-                       self.class_tables = class_coloring.hash_type_tables(mclasses, class_ids, class_colors)
+                       self.class_tables = self.hash_class_typing_tables(mclasses, class_ids, class_colors, class_coloring)
 
                        self.header.add_decl("#define HASH(mask, id) ((mask)%(id))")
                else if modelbuilder.toolcontext.opt_phand_typing.value then
@@ -96,7 +96,7 @@ class SeparateErasureCompiler
 
                        var class_coloring = new ClassAndPerfectHashing(mainmodule)
                        self.class_colors = class_coloring.compute_masks(mclasses, class_ids)
-                       self.class_tables = class_coloring.hash_type_tables(mclasses, class_ids, class_colors)
+                       self.class_tables = self.hash_class_typing_tables(mclasses, class_ids, class_colors, class_coloring)
 
                        self.header.add_decl("#define HASH(mask, id) ((mask)&(id))")
                else
@@ -111,10 +111,57 @@ class SeparateErasureCompiler
                                self.class_ids[mclass] = self.class_ids.length + 1
                        end
                        self.class_colors = class_coloring.colorize(mclasses)
-                       self.class_tables = class_coloring.build_type_tables(mclasses, class_colors)
+                       self.class_tables = self.build_class_typing_tables(mclasses, class_colors, class_coloring)
                end
        end
 
+       # Build type tables
+       fun build_class_typing_tables(mclasses: Set[MClass], colors: Map[MClass, Int], colorer: ClassColoring): Map[MClass, Array[nullable MClass]] do
+               var tables = new HashMap[MClass, Array[nullable MClass]]
+
+               for mclasse in mclasses do
+                       var table = new Array[nullable MClass]
+                       var supers = new HashSet[MClass]
+                       supers.add_all(colorer.super_elements(mclasse, mclasses))
+                       supers.add(mclasse)
+                       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[mclasse] = table
+               end
+               return tables
+       end
+
+       # Build type tables
+       fun hash_class_typing_tables(mtypes: Set[MClass], ids: Map[MClass, Int], masks: Map[MClass, Int], colorer: ClassPerfectHashing): Map[MClass, Array[nullable MClass]] do
+               var tables = new HashMap[MClass, Array[nullable MClass]]
+
+               for mtype in mtypes do
+                       var table = new Array[nullable MClass]
+                       var supers = new HashSet[MClass]
+                       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
+
        redef fun compile_header_structs do
                self.header.add_decl("typedef void(*nitmethod_t)(void); /* general C type representing a Nit method. */")
                self.compile_header_attribute_structs