icode: add IStaticCall
[nit.git] / src / compiling / compiling_global.nit
index b2a9838..20e74c3 100644 (file)
@@ -177,6 +177,7 @@ redef class MMModule
                var itab = new Array[TableElt]
 
                ctab.add(new TableEltClassSelfId)
+               ctab.add(new TableEltClassObjectSize)
                itab.add(new TableEltVftPointer)
                itab.add(new TableEltObjectId)
 
@@ -462,6 +463,7 @@ redef class MMModule
                        else
                                var mainm = sys.select_method(name)
                                v.add_instr("G_sys = NEW_Sys();")
+                               v.add_instr("register_static_object(&G_sys);")
                                v.add_instr("{mainm.cname}(G_sys);")
                        end
                end
@@ -770,6 +772,28 @@ special TableElt
        end
 end
 
+
+# The element that represent the Object Size
+class TableEltClassObjectSize
+special TableElt
+       redef fun is_related_to(c) do return true
+       redef fun compile_to_c(v, c)
+       do
+        var nb = 0
+        var ga = v.global_analysis
+               if c.name == "NativeArray".to_symbol then
+                       nb = -1
+               else
+                       var cc = ga.compiled_classes[c.global]
+                       var itab = cc.instance_table
+                       for e in itab do
+                               nb += 1
+                       end
+               end
+               return "{nb} /* {ga.color(self)}: Object size (-1 if a NativeArray)*/"
+       end
+end
+
 # The element that represent the object id
 class TableEltObjectId
 special TableElt
@@ -831,7 +855,9 @@ redef class MMLocalClass
                v.add_decl("")
                var pi = primitive_info
                v.add_decl("extern const classtable_elt_t VFT_{name}[];")
-               if pi == null then
+               if name == "NativeArray".to_symbol then
+                       v.add_decl("val_t NEW_NativeArray(size_t length, size_t size);")
+               else if pi == null then
                        # v.add_decl("val_t NEW_{name}(void);")
                else if not pi.tagged then
                        var t = pi.cname
@@ -876,7 +902,19 @@ redef class MMLocalClass
                end
 
                var pi = primitive_info
-               if pi == null then
+               if name == "NativeArray".to_symbol then
+                       v.add_instr("val_t NEW_NativeArray(size_t length, size_t size) \{")
+                       v.indent
+                       v.add_instr("Nit_NativeArray array;")
+                       v.add_instr("array = (Nit_NativeArray)alloc(sizeof(struct Nit_NativeArray) + ((length - 1) * size));")
+                       v.add_instr("array->vft = (classtable_elt_t*)VFT_{name};")
+                       v.add_instr("array->object_id = object_id_counter;")
+                       v.add_instr("object_id_counter = object_id_counter + 1;")
+                       v.add_instr("array->size = length;")
+                       v.add_instr("return OBJ2VAL(array);")
+                       v.unindent
+                       v.add_instr("}")
+               else if pi == null then
                        do
                                var iself = new IRegister(get_type)
                                var iselfa = [iself]
@@ -960,9 +998,9 @@ redef class MMLocalClass
                                icb.stmt(inew)
                                var iargs = [iself]
                                iargs.add_all(iparams)
-                               icb.stmt(new INative("{p.cname}(@@@{", @@@"*iparams.length}, init_table)", iargs))
-                               icb.stmt(new INative("CHECKNEW_{name}(@@@)", [iself]))
 
+                               icb.stmt(new IStaticCall(p, iargs))
+                               icb.stmt(new INative("CHECKNEW_{name}(@@@)", [iself]))
                                var cname = "NEW_{self}_{p.global.intro.cname}"
                                var new_args = iroutine.compile_signature_to_c(v, cname, "new {self} {p.full_name}", null, null)
                                var ctx_old = v.ctx