nitg-s: inserted layout builder concept in coloring
authorAlexandre Terrasa <alexandre@moz-code.org>
Thu, 7 Feb 2013 16:55:17 +0000 (11:55 -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 84deb3d..e43e26e 100644 (file)
@@ -17,6 +17,9 @@ module coloring
 
 import typing
 
+abstract class TypeLayoutBuilder
+end
+
 abstract class AbstractColoring[E: Object]
 
        private var core: Set[E] = new HashSet[E]
@@ -150,6 +153,7 @@ end
 # MClassType coloring
 class TypeColoring
        super AbstractColoring[MType]
+       super TypeLayoutBuilder
 
        type T: MType
 
index 17aef78..c304845 100644 (file)
@@ -108,6 +108,7 @@ class SeparateCompiler
        private var partial_types: Set[MType] = new HashSet[MType]
        protected var typeids: HashMap[MType, Int] protected writable = new HashMap[MType, Int]
 
+       private var type_layout_builder: TypeLayoutBuilder
        private var type_colors: Map[MType, Int] = typeids
        private var type_tables: nullable Map[MType, Array[nullable MType]] = null
 
@@ -135,12 +136,26 @@ class SeparateCompiler
 
        init(mainmodule: MModule, mmbuilder: ModelBuilder, runtime_type_analysis: RapidTypeAnalysis) do
                super
+               self.init_layout_builders
                self.header = new_visitor
                self.runtime_type_analysis = runtime_type_analysis
                self.do_property_coloring
                self.compile_box_kinds
        end
 
+       protected fun init_layout_builders do
+               # Typing Layout
+               if modelbuilder.toolcontext.opt_bm_typing.value then
+                       self.type_layout_builder = new NaiveTypeColoring(self.mainmodule)
+               else if modelbuilder.toolcontext.opt_phmod_typing.value then
+                       self.type_layout_builder = new TypeModPerfectHashing(self.mainmodule)
+               else if modelbuilder.toolcontext.opt_phand_typing.value then
+                       self.type_layout_builder = new TypeAndPerfectHashing(self.mainmodule)
+               else
+                       self.type_layout_builder = new TypeColoring(self.mainmodule)
+               end
+       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
@@ -300,22 +315,19 @@ class SeparateCompiler
                self.compile_unanchored_tables(mtypes)
 
                # colorize types
-               if modelbuilder.toolcontext.opt_bm_typing.value then
-                       var type_coloring = new NaiveTypeColoring(self.mainmodule)
+               var type_coloring = self.type_layout_builder
+               if type_coloring isa NaiveTypeColoring then
                        self.type_colors = type_coloring.colorize(mtypes)
                        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)
+               else if type_coloring isa TypeModPerfectHashing then
                        self.type_colors = type_coloring.compute_masks(mtypes, typeids)
                        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)
+               else if type_coloring isa TypeAndPerfectHashing then
                        self.type_colors = type_coloring.compute_masks(mtypes, typeids)
                        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)
+               else if type_coloring isa TypeColoring then
                        self.type_colors = type_coloring.colorize(mtypes)
                        self.type_tables = self.build_type_tables(mtypes, type_colors, type_coloring)
                end