nitg: use libgc only if platform supports it
[nit.git] / src / abstract_compiler.nit
index 831311d..c1078af 100644 (file)
@@ -61,6 +61,8 @@ redef class ToolContext
        var opt_stacktrace: OptionString = new OptionString("Control the generation of stack traces", "--stacktrace")
        # --no-gcc-directives
        var opt_no_gcc_directive = new OptionArray("Disable a advanced gcc directives for optimization", "--no-gcc-directive")
+       # --release
+       var opt_release = new OptionBool("Compile in release mode and finalize application", "--release")
 
        redef init
        do
@@ -70,6 +72,7 @@ redef class ToolContext
                self.option_context.add_option(self.opt_typing_test_metrics, self.opt_invocation_metrics, self.opt_isset_checks_metrics)
                self.option_context.add_option(self.opt_stacktrace)
                self.option_context.add_option(self.opt_no_gcc_directive)
+               self.option_context.add_option(self.opt_release)
        end
 
        redef fun process_options(args)
@@ -199,8 +202,12 @@ class MakefileToolchain
        do
                if self.toolcontext.opt_stacktrace.value == "nitstack" then compiler.build_c_to_nit_bindings
 
+               var platform = compiler.mainmodule.target_platform
+               var cc_opt_with_libgc = "-DWITH_LIBGC"
+               if platform != null and not platform.supports_libgc then cc_opt_with_libgc = ""
+
                # Add gc_choser.h to aditionnal bodies
-               var gc_chooser = new ExternCFile("gc_chooser.c", "-DWITH_LIBGC")
+               var gc_chooser = new ExternCFile("gc_chooser.c", cc_opt_with_libgc)
                compiler.extern_bodies.add(gc_chooser)
                compiler.files_to_copy.add "{cc_paths.first}/gc_chooser.c"
                compiler.files_to_copy.add "{cc_paths.first}/gc_chooser.h"
@@ -493,6 +500,12 @@ abstract class AbstractCompiler
                self.header.add_decl("#include <stdio.h>")
                self.header.add_decl("#include <string.h>")
                self.header.add_decl("#include \"gc_chooser.h\"")
+               self.header.add_decl("#ifdef ANDROID")
+               self.header.add_decl("  #include <android/log.h>")
+               self.header.add_decl("  #define PRINT_ERROR(...) (void)__android_log_print(ANDROID_LOG_WARN, \"Nit\", __VA_ARGS__)")
+               self.header.add_decl("#else")
+               self.header.add_decl("  #define PRINT_ERROR(...) fprintf(stderr, __VA_ARGS__)")
+               self.header.add_decl("#endif")
 
                compile_header_structs
                compile_nitni_structs
@@ -592,7 +605,7 @@ abstract class AbstractCompiler
                end
 
                v.add_decl("void sig_handler(int signo)\{")
-               v.add_decl("printf(\"Caught signal : %s\\n\", strsignal(signo));")
+               v.add_decl("PRINT_ERROR(\"Caught signal : %s\\n\", strsignal(signo));")
                v.add_decl("show_backtrace(signo);")
                v.add_decl("\}")
 
@@ -606,23 +619,23 @@ abstract class AbstractCompiler
                        v.add_decl("char* procname = malloc(sizeof(char) * 100);")
                        v.add_decl("unw_getcontext(&uc);")
                        v.add_decl("unw_init_local(&cursor, &uc);")
-                       v.add_decl("printf(\"-------------------------------------------------\\n\");")
-                       v.add_decl("printf(\"--   Stack Trace   ------------------------------\\n\");")
-                       v.add_decl("printf(\"-------------------------------------------------\\n\");")
+                       v.add_decl("PRINT_ERROR(\"-------------------------------------------------\\n\");")
+                       v.add_decl("PRINT_ERROR(\"--   Stack Trace   ------------------------------\\n\");")
+                       v.add_decl("PRINT_ERROR(\"-------------------------------------------------\\n\");")
                        v.add_decl("while (unw_step(&cursor) > 0) \{")
                        v.add_decl("    unw_get_proc_name(&cursor, procname, 100, &ip);")
                        if ost == "nitstack" then
                        v.add_decl("    const char* recv = get_nit_name(procname, strlen(procname));")
                        v.add_decl("    if (recv != NULL)\{")
-                       v.add_decl("            printf(\"` %s\\n\", recv);")
+                       v.add_decl("            PRINT_ERROR(\"` %s\\n\", recv);")
                        v.add_decl("    \}else\{")
-                       v.add_decl("            printf(\"` %s\\n\", procname);")
+                       v.add_decl("            PRINT_ERROR(\"` %s\\n\", procname);")
                        v.add_decl("    \}")
                        else
-                       v.add_decl("    printf(\"` %s \\n\",procname);")
+                       v.add_decl("    PRINT_ERROR(\"` %s \\n\",procname);")
                        end
                        v.add_decl("\}")
-                       v.add_decl("printf(\"-------------------------------------------------\\n\");")
+                       v.add_decl("PRINT_ERROR(\"-------------------------------------------------\\n\");")
                        v.add_decl("free(procname);")
                        v.add_decl("\}")
                end
@@ -1217,16 +1230,16 @@ abstract class AbstractCompilerVisitor
        # used by aborts, asserts, casts, etc.
        fun add_abort(message: String)
        do
-               self.add("fprintf(stderr, \"Runtime error: %s\", \"{message.escape_to_c}\");")
+               self.add("PRINT_ERROR(\"Runtime error: %s\", \"{message.escape_to_c}\");")
                add_raw_abort
        end
 
        fun add_raw_abort
        do
                if self.current_node != null and self.current_node.location.file != null then
-                       self.add("fprintf(stderr, \" (%s:%d)\\n\", \"{self.current_node.location.file.filename.escape_to_c}\", {current_node.location.line_start});")
+                       self.add("PRINT_ERROR(\" (%s:%d)\\n\", \"{self.current_node.location.file.filename.escape_to_c}\", {current_node.location.line_start});")
                else
-                       self.add("fprintf(stderr, \"\\n\");")
+                       self.add("PRINT_ERROR(\"\\n\");")
                end
                self.add("show_backtrace(1);")
        end
@@ -1237,7 +1250,7 @@ abstract class AbstractCompilerVisitor
                var res = self.type_test(value, mtype, tag)
                self.add("if (unlikely(!{res})) \{")
                var cn = self.class_name_string(value)
-               self.add("fprintf(stderr, \"Runtime error: Cast failed. Expected `%s`, got `%s`\", \"{mtype.to_s.escape_to_c}\", {cn});")
+               self.add("PRINT_ERROR(\"Runtime error: Cast failed. Expected `%s`, got `%s`\", \"{mtype.to_s.escape_to_c}\", {cn});")
                self.add_raw_abort
                self.add("\}")
        end
@@ -1558,6 +1571,7 @@ redef class MMethodDef
        # Can the body be inlined?
        fun can_inline(v: VISITOR): Bool
        do
+               if is_abstract then return true
                var modelbuilder = v.compiler.modelbuilder
                if modelbuilder.mpropdef2npropdef.has_key(self) then
                        var npropdef = modelbuilder.mpropdef2npropdef[self]
@@ -1623,20 +1637,23 @@ end
 redef class APropdef
        fun compile_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable])
        do
-               v.add("printf(\"NOT YET IMPLEMENTED {class_name} {mpropdef} at {location.to_s}\\n\");")
+               v.add("PRINT_ERROR(\"NOT YET IMPLEMENTED {class_name} {mpropdef} at {location.to_s}\\n\");")
                debug("Not yet implemented")
        end
 
        fun can_inline: Bool do return true
 end
 
-redef class AConcreteMethPropdef
+redef class AMethPropdef
        redef fun compile_to_c(v, mpropdef, arguments)
        do
-               for i in [0..mpropdef.msignature.arity[ do
-                       var variable = self.n_signature.n_params[i].variable.as(not null)
-                       v.assign(v.variable(variable), arguments[i+1])
+               if mpropdef.is_abstract then
+                       var cn = v.class_name_string(arguments.first)
+                       v.add("PRINT_ERROR(\"Runtime error: Abstract method `%s` called on `%s`\", \"{mpropdef.mproperty.name.escape_to_c}\", {cn});")
+                       v.add_raw_abort
+                       return
                end
+
                # Call the implicit super-init
                var auto_super_inits = self.auto_super_inits
                if auto_super_inits != null then
@@ -1649,7 +1666,23 @@ redef class AConcreteMethPropdef
                                v.compile_callsite(auto_super_init, args)
                        end
                end
-               v.stmt(self.n_block)
+
+               var n_block = n_block
+               if n_block != null then
+                       for i in [0..mpropdef.msignature.arity[ do
+                               var variable = self.n_signature.n_params[i].variable.as(not null)
+                               v.assign(v.variable(variable), arguments[i+1])
+                       end
+                       v.stmt(n_block)
+               else if mpropdef.is_intern then
+                       compile_intern_to_c(v, mpropdef, arguments)
+               else if mpropdef.is_extern then
+                       if mpropdef.mproperty.is_init then
+                               compile_externinit_to_c(v, mpropdef, arguments)
+                       else
+                               compile_externmeth_to_c(v, mpropdef, arguments)
+                       end
+               end
        end
 
        redef fun can_inline
@@ -1661,10 +1694,8 @@ redef class AConcreteMethPropdef
                if nblock isa ABlockExpr and nblock.n_expr.length == 0 then return true
                return false
        end
-end
 
-redef class AInternMethPropdef
-       redef fun compile_to_c(v, mpropdef, arguments)
+       fun compile_intern_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable])
        do
                var pname = mpropdef.mproperty.name
                var cname = mpropdef.mclassdef.mclass.name
@@ -1894,18 +1925,16 @@ redef class AInternMethPropdef
                        v.ret(v.new_expr("glob_argv[{arguments[1]}]", ret.as(not null)))
                        return
                end
-               v.add("printf(\"NOT YET IMPLEMENTED {class_name}:{mpropdef} at {location.to_s}\\n\");")
+               v.add("PRINT_ERROR(\"NOT YET IMPLEMENTED {class_name}:{mpropdef} at {location.to_s}\\n\");")
                debug("Not implemented {mpropdef}")
        end
-end
 
-redef class AExternMethPropdef
-       redef fun compile_to_c(v, mpropdef, arguments)
+       fun compile_externmeth_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable])
        do
                var externname
                var nextern = self.n_extern
                if nextern == null then
-                       v.add("fprintf(stderr, \"NOT YET IMPLEMENTED nitni for {mpropdef} at {location.to_s}\\n\");")
+                       v.add("PRINT_ERROR(\"NOT YET IMPLEMENTED nitni for {mpropdef} at {location.to_s}\\n\");")
                        v.add("show_backtrace(1);")
                        return
                end
@@ -1929,15 +1958,13 @@ redef class AExternMethPropdef
                        v.ret(res)
                end
        end
-end
 
-redef class AExternInitPropdef
-       redef fun compile_to_c(v, mpropdef, arguments)
+       fun compile_externinit_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable])
        do
                var externname
                var nextern = self.n_extern
                if nextern == null then
-                       v.add("printf(\"NOT YET IMPLEMENTED nitni for {mpropdef} at {location.to_s}\\n\");")
+                       v.add("PRINT_ERROR(\"NOT YET IMPLEMENTED nitni for {mpropdef} at {location.to_s}\\n\");")
                        v.add("show_backtrace(1);")
                        return
                end
@@ -2007,11 +2034,11 @@ redef class AClassdef
                if mpropdef == self.mfree_init then
                        var super_inits = self.super_inits
                        if super_inits != null then
-                               assert arguments.length == 1
+                               var args_of_super = arguments
+                               if arguments.length > 1 then args_of_super = [arguments.first]
                                for su in super_inits do
-                                       v.send(su, arguments)
+                                       v.send(su, args_of_super)
                                end
-                               return
                        end
                        var recv = arguments.first
                        var i = 1
@@ -2028,21 +2055,12 @@ redef class AClassdef
        end
 end
 
-redef class ADeferredMethPropdef
-       redef fun compile_to_c(v, mpropdef, arguments) do
-               var cn = v.class_name_string(arguments.first)
-               v.add("fprintf(stderr, \"Runtime error: Abstract method `%s` called on `%s`\", \"{mpropdef.mproperty.name.escape_to_c}\", {cn});")
-               v.add_raw_abort
-       end
-       redef fun can_inline do return true
-end
-
 redef class AExpr
        # Try to compile self as an expression
        # Do not call this method directly, use `v.expr` instead
        private fun expr(v: AbstractCompilerVisitor): nullable RuntimeVariable
        do
-               v.add("printf(\"NOT YET IMPLEMENTED {class_name}:{location.to_s}\\n\");")
+               v.add("PRINT_ERROR(\"NOT YET IMPLEMENTED {class_name}:{location.to_s}\\n\");")
                var mtype = self.mtype
                if mtype == null then
                        return null
@@ -2568,7 +2586,7 @@ redef class ASuperExpr
                if callsite != null then
                        # Add additionnals arguments for the super init call
                        if args.length == 1 then
-                               for i in [0..callsite.mproperty.intro.msignature.arity[ do
+                               for i in [0..callsite.msignature.arity[ do
                                        args.add(v.frame.arguments[i+1])
                                end
                        end