compile: object_id is no more the object pointer
authorJulien Chevalier <chevjulien@gmail.com>
Mon, 17 Aug 2009 19:13:06 +0000 (15:13 -0400)
committerJean Privat <jean@pryen.org>
Mon, 17 Aug 2009 20:15:29 +0000 (16:15 -0400)
Warning: now, two objects of the same class can have the same object_id.
It is unlikely, but possible.

Trivially-hacked-by: Jean Privat <jean@pryen.org>

Signed-off-by: Julien Chevalier <chevjulien@gmail.com>
Signed-off-by: Jean Privat <jean@pryen.org>

clib/nit_common.h
clib/nit_main.c
src/compiling/compiling_global.nit
src/syntax/icode_generation.nit

index 09561dd..c3b056f 100644 (file)
@@ -31,11 +31,13 @@ typedef signed long int bigint;     /* standard int value, must be larger that any p
 typedef bigint (*fun_t) (bigint);                                      /* generic function pointer */
 typedef bigint cid_t;                                  /* class identifier */
 typedef bigint val_t;  /* value (everything is a val_t) */
-typedef union obj_tu {union classtable_elt_tu * vft; val_t attr;} *obj_t; /* standard object */
+typedef union obj_tu {union classtable_elt_tu * vft; bigint object_id; val_t objectSize;} *obj_t; /* standard object */
 typedef union classtable_elt_tu { bigint i; fun_t f; cid_t cid;} classtable_elt_t;     /* classtable element */
 
 typedef classtable_elt_t * classtable_t;                       /* classtable */
 
+extern bigint object_id_counter;
+
 /*****************************************************************************
  * Types macros (primitive and less primitives) ******************************
  *****************************************************************************
@@ -96,7 +98,7 @@ typedef classtable_elt_t * classtable_t;                      /* classtable */
 /* O = Non nil object ; N = Object or nil ; P = Primitive */
 #define OBJ_IS_BOX(x) ((VAL2OBJ(x)->vft->i) < 0)
 #define IS_BOX(x) (ISOBJ(x) && OBJ_IS_BOX(x))
-#define IS_EQUAL_BOX(x, y) (ISOBJ(y) && (VAL2OBJ(x)[1].vft==VAL2OBJ(y)[1].vft) && (VAL2OBJ(x)->vft==VAL2OBJ(y)->vft))
+#define IS_EQUAL_BOX(x, y) (ISOBJ(y) && (VAL2OBJ(x)[2].vft==VAL2OBJ(y)[2].vft) && (VAL2OBJ(x)->vft==VAL2OBJ(y)->vft))
 #define IS_EQUAL_OO(x, y) ((x)==(y) || (IS_BOX(x) && IS_EQUAL_BOX((x), (y))))
 #define IS_EQUAL_ON(x, y) ((x)==(y) || (IS_BOX(x) && !ISNULL(y) && IS_EQUAL_BOX((x), (y))))
 #define IS_EQUAL_NN(x, y) ((x)==(y) || (!ISNULL(x) && IS_BOX(x) && !ISNULL(y) && IS_EQUAL_BOX((x), (y))))
index 5ad9df4..3661cf8 100644 (file)
@@ -14,6 +14,7 @@
 #include "nit_common.h"
 #include <signal.h>
 #include <stdarg.h>
+bigint object_id_counter = 1000000;
 
 #ifdef WITH_LIBGC
 #define GC_DEBUG
index 49b390f..b2a9838 100644 (file)
@@ -178,6 +178,7 @@ redef class MMModule
 
                ctab.add(new TableEltClassSelfId)
                itab.add(new TableEltVftPointer)
+               itab.add(new TableEltObjectId)
 
                var pclassid = -1
                var classid = 3
@@ -769,6 +770,17 @@ special TableElt
        end
 end
 
+# The element that represent the object id
+class TableEltObjectId
+special TableElt
+       redef fun is_related_to(c) do return true
+       redef fun compile_to_c(v, c)
+       do
+               var ga = v.global_analysis
+               return "/* {ga.color(self)}: Object_id */"
+       end
+end
+
 # The element that
 class TableEltVftPointer
 special TableElt
@@ -824,7 +836,7 @@ redef class MMLocalClass
                else if not pi.tagged then
                        var t = pi.cname
                        var tbox = "struct TBOX_{name}"
-                       v.add_decl("{tbox} \{ const classtable_elt_t * vft; {t} val;};")
+                       v.add_decl("{tbox} \{ const classtable_elt_t * vft; bigint object_id; {t} val;};")
                        v.add_decl("val_t BOX_{name}({t} val);")
                        v.add_decl("#define UNBOX_{name}(x) ((({tbox} *)(VAL2OBJ(x)))->val)")
                end
@@ -893,6 +905,8 @@ redef class MMLocalClass
                                v.add_decl("obj_t obj;")
                                v.add_instr("obj = alloc(sizeof(val_t) * {itab.length});")
                                v.add_instr("obj->vft = (classtable_elt_t*)VFT_{name};")
+                               v.add_instr("obj[1].object_id = object_id_counter;")
+                               v.add_instr("object_id_counter = object_id_counter + 1;")
                                var r = iroutine.compile_to_c(v, cname, args).as(not null)
                                v.add_instr("return {r};")
                                ctx_old.append(v.ctx)
@@ -969,6 +983,8 @@ redef class MMLocalClass
                        v.add_instr("{tbox} *box = ({tbox}*)alloc(sizeof({tbox}));")
                        v.add_instr("box->vft = VFT_{name};")
                        v.add_instr("box->val = val;")
+                       v.add_instr("box->object_id = object_id_counter;")
+                       v.add_instr("object_id_counter = object_id_counter + 1;")
                        v.add_instr("return OBJ2VAL(box);")
                        v.unindent
                        v.add_instr("}")
index 64fd0f2..20e2a90 100644 (file)
@@ -573,7 +573,7 @@ redef class AInternMethPropdef
                                s = "(void)memcpy(UNBOX_NativeString(@@@)+UNTAG_Int(@@@), UNBOX_NativeString(@@@)+UNTAG_Int(@@@), UNTAG_Int(@@@));"
                        end
                else if n == once "object_id".to_symbol then
-                       s = "TAG_Int((bigint)@@@)"
+                       s = "TAG_Int((bigint)((obj_t)@@@)[1].object_id)"
                else if n == once "sys".to_symbol then
                        s = "(G_sys)"
                else if n == once "is_same_type".to_symbol then