tests: remove some very-old useless tests
[nit.git] / src / global_compiler.nit
index ecd7265..8c6ac6c 100644 (file)
@@ -25,7 +25,29 @@ module global_compiler
 
 import abstract_compiler
 import rapid_type_analysis
-import compiler_ffi
+
+redef class ToolContext
+       # option --global
+       var opt_global = new OptionBool("Use global compilation", "--global")
+
+       var global_compiler_phase = new GlobalCompilerPhase(self, null)
+
+       redef init do
+               super
+               option_context.add_option(opt_global)
+       end
+end
+
+class GlobalCompilerPhase
+       super Phase
+       redef fun process_mainmodule(mainmodule, given_mmodules) do
+               if not toolcontext.opt_global.value then return
+
+               var modelbuilder = toolcontext.modelbuilder
+               var analysis = modelbuilder.do_rapid_type_analysis(mainmodule)
+               modelbuilder.run_global_compiler(mainmodule, analysis)
+       end
+end
 
 redef class ModelBuilder
        # Entry point to performs a global compilation on the AST of a complete program.
@@ -166,6 +188,7 @@ class GlobalCompiler
 
                if mtype.mclass.name == "NativeArray" then
                        # NativeArrays are just a instance header followed by an array of values
+                       v.add_decl("int length;")
                        v.add_decl("{mtype.arguments.first.ctype} values[1];")
                end
 
@@ -213,6 +236,7 @@ class GlobalCompiler
                if is_native_array then
                        var mtype_elt = mtype.arguments.first
                        v.add("{res} = nit_alloc(sizeof(struct {mtype.c_name}) + length*sizeof({mtype_elt.ctype}));")
+                       v.add("((struct {mtype.c_name}*){res})->length = length;")
                else
                        v.add("{res} = nit_alloc(sizeof(struct {mtype.c_name}));")
                end
@@ -247,8 +271,6 @@ class GlobalCompiler
        do
                self.header.add_decl("struct nitni_instance \{ val *value; \};")
        end
-
-       redef fun supports_ffi do return true
 end
 
 # A visitor on the AST of property definition that generate the C code.
@@ -271,18 +293,18 @@ class GlobalCompilerVisitor
                        var res = self.new_var(mtype)
                        if not compiler.runtime_type_analysis.live_types.has(valtype) then
                                self.add("/*no autobox from {value.mtype} to {mtype}: {value.mtype} is not live! */")
-                               self.add("printf(\"Dead code executed!\\n\"); show_backtrace(1);")
+                               self.add("PRINT_ERROR(\"Dead code executed!\\n\"); show_backtrace(1);")
                                return res
                        end
                        self.add("{res} = BOX_{valtype.c_name}({value}); /* autobox from {value.mtype} to {mtype} */")
                        return res
-               else if value.mtype.cname_blind == "void*" and mtype.cname_blind == "void*" then
+               else if value.mtype.ctype == "void*" and mtype.ctype == "void*" then
                        return value
                else
                        # Bad things will appen!
                        var res = self.new_var(mtype)
                        self.add("/* {res} left unintialized (cannot convert {value.mtype} to {mtype}) */")
-                       self.add("printf(\"Cast error: Cannot cast %s to %s.\\n\", \"{value.mtype}\", \"{mtype}\"); show_backtrace(1);")
+                       self.add("PRINT_ERROR(\"Cast error: Cannot cast %s to %s.\\n\", \"{value.mtype}\", \"{mtype}\"); show_backtrace(1);")
                        return res
                end
        end
@@ -321,6 +343,9 @@ class GlobalCompilerVisitor
                else if pname == "[]=" then
                        self.add("{recv}[{arguments[1]}]={arguments[2]};")
                        return
+               else if pname == "length" then
+                       self.ret(self.new_expr("((struct {arguments[0].mcasttype.c_name}*){arguments[0]})->length", ret_type.as(not null)))
+                       return
                else if pname == "copy_to" then
                        var recv1 = "((struct {arguments[1].mcasttype.c_name}*){arguments[1]})->values"
                        self.add("memcpy({recv1},{recv},{arguments[2]}*sizeof({elttype.ctype}));")
@@ -328,6 +353,12 @@ class GlobalCompilerVisitor
                end
        end
 
+       redef fun native_array_instance(elttype: MType, length: RuntimeVariable): RuntimeVariable
+       do
+               var ret_type = self.get_class("NativeArray").get_mtype([elttype])
+               return self.new_expr("NEW_{ret_type.c_name}({length})", ret_type)
+       end
+
        redef fun calloc_array(ret_type, arguments)
        do
                self.ret(self.new_expr("NEW_{ret_type.c_name}({arguments[1]})", ret_type))
@@ -384,7 +415,7 @@ class GlobalCompilerVisitor
                                        self.add("{res} = 1; /* {args[1].inspect} cannot be null */")
                                end
                        else
-                               self.add_abort("Reciever is null")
+                               self.add_abort("Receiver is null")
                        end
                        self.add "\} else"
                end
@@ -456,11 +487,7 @@ class GlobalCompilerVisitor
        # Finalizes a call to a method ´m´ on type ´recvtype´ with arguments ´args´
        private fun finalize_call(m: MMethodDef, recvtype: MClassType, args: Array[RuntimeVariable]): nullable RuntimeVariable
        do
-               if args.length != m.msignature.arity + 1 then # because of self
-                       add("printf(\"NOT YET IMPLEMENTED: Invalid arity for {m}. {args.length} arguments given.\\n\"); show_backtrace(1);")
-                       debug("NOT YET IMPLEMENTED: Invalid arity for {m}. {args.length} arguments given.")
-                       return null
-               end
+               assert args.length == m.msignature.arity + 1 else debug("Invalid arity for {m}. {args.length} arguments given.")
 
                var rm = new CustomizedRuntimeFunction(m, recvtype)
                return rm.call(self, args)
@@ -565,7 +592,7 @@ class GlobalCompilerVisitor
        fun bugtype(recv: RuntimeVariable)
        do
                if recv.mtype.ctype != "val*" then return
-               self.add("fprintf(stderr, \"BTD BUG: Dynamic type is %s, static type is %s\\n\", class_names[{recv}->classid], \"{recv.mcasttype}\");")
+               self.add("PRINT_ERROR(\"BTD BUG: Dynamic type is %s, static type is %s\\n\", class_names[{recv}->classid], \"{recv.mcasttype}\");")
                self.add("show_backtrace(1);")
        end
 
@@ -908,8 +935,8 @@ private class CustomizedRuntimeFunction
                var frame = new Frame(v, mmethoddef, recv, arguments)
                v.frame = frame
 
-               var sig = new Buffer
-               var comment = new Buffer
+               var sig = new FlatBuffer
+               var comment = new FlatBuffer
                var ret = mmethoddef.msignature.return_mtype
                if ret != null then
                        ret = v.resolve_for(ret, selfvar)