sep_compiler: table_send take the original entity instead of the color
[nit.git] / src / compiler / separate_compiler.nit
index 6b7a004..b14da6a 100644 (file)
@@ -33,6 +33,8 @@ redef class ToolContext
        var opt_colors_are_symbols = new OptionBool("Store colors as symbols (faster)", "--colors-are-symbols")
        # --trampoline-call
        var opt_trampoline_call = new OptionBool("Use an indirection when calling", "--trampoline-call")
+       # --substitute-monomorph
+       var opt_substitute_monomorph = new OptionBool("Replace monomorph trampoline with direct call", "--substitute-monomorph")
 
        # --inline-coloring-numbers
        var opt_inline_coloring_numbers = new OptionBool("Inline colors and ids (semi-global)", "--inline-coloring-numbers")
@@ -55,7 +57,7 @@ redef class ToolContext
                self.option_context.add_option(self.opt_separate)
                self.option_context.add_option(self.opt_no_inline_intern)
                self.option_context.add_option(self.opt_no_union_attribute)
-               self.option_context.add_option(self.opt_no_shortcut_equate, opt_colors_are_symbols, opt_trampoline_call)
+               self.option_context.add_option(self.opt_no_shortcut_equate, opt_colors_are_symbols, opt_trampoline_call, opt_substitute_monomorph)
                self.option_context.add_option(self.opt_inline_coloring_numbers, opt_inline_some_methods, opt_direct_call_monomorph, opt_skip_dead_methods, opt_semi_global)
                self.option_context.add_option(self.opt_colo_dead_methods)
                self.option_context.add_option(self.opt_tables_metrics)
@@ -567,6 +569,24 @@ class SeparateCompiler
                                # Generate trampolines
                                if modelbuilder.toolcontext.opt_trampoline_call.value then
                                        r2.compile_trampolines(self)
+
+                                       # Replace monomorphic call to a trampoline by a direct call to the virtual implementation
+                                       if modelbuilder.toolcontext.opt_substitute_monomorph.value then do
+                                               var m = pd.mproperty
+                                               if rta == null then
+                                                       # Without RTA, monomorphic means alone (uniq name)
+                                                       if m.mpropdefs.length != 1 then break label
+                                               else
+                                                       # With RTA, monomorphic means only live methoddef
+                                                       if not rta.live_methoddefs.has(pd) then break label
+                                                       for md in m.mpropdefs do
+                                                               if md != pd and rta.live_methoddefs.has(md) then break label
+                                                       end
+                                               end
+                                               # Here the trick, GNU ld can substitute symbols with specific values.
+                                               var n2 = "CALL_" + m.const_color
+                                               linker_script.add("{n2} = {r2.c_name};")
+                                       end label
                                end
                        end
                end
@@ -1111,7 +1131,7 @@ class SeparateCompilerVisitor
                        return res
                end
 
-               return table_send(mmethod, arguments, mmethod.const_color)
+               return table_send(mmethod, arguments, mmethod)
        end
 
        # Handle common special cases before doing the effective method invocation
@@ -1177,7 +1197,7 @@ class SeparateCompilerVisitor
                return res
        end
 
-       private fun table_send(mmethod: MMethod, arguments: Array[RuntimeVariable], const_color: String): nullable RuntimeVariable
+       private fun table_send(mmethod: MMethod, arguments: Array[RuntimeVariable], mentity: MEntity): nullable RuntimeVariable
        do
                compiler.modelbuilder.nb_invok_by_tables += 1
                if compiler.modelbuilder.toolcontext.opt_invocation_metrics.value then add("count_invoke_by_tables++;")
@@ -1211,6 +1231,7 @@ class SeparateCompilerVisitor
                        ss.append(", {a}")
                end
 
+               var const_color = mentity.const_color
                var call
                if not compiler.modelbuilder.toolcontext.opt_trampoline_call.value then
                        self.require_declaration(const_color)
@@ -1295,7 +1316,7 @@ class SeparateCompilerVisitor
                        self.compiler.mainmodule = main
                        return res
                end
-               return table_send(m.mproperty, arguments, m.const_color)
+               return table_send(m.mproperty, arguments, m)
        end
 
        redef fun vararg_instance(mpropdef, recv, varargs, elttype)