From b8e4c3af5401863b4339e41a54e7bfe9d8fe181f Mon Sep 17 00:00:00 2001 From: Julien Chevalier Date: Fri, 14 Aug 2009 12:02:09 -0400 Subject: [PATCH] compile: give NativeArray its own C structure Signed-off-by: Julien Chevalier Signed-off-by: Jean Privat --- clib/nit_common.h | 3 +++ src/compiling/compiling_global.nit | 18 ++++++++++++++++-- src/primitive_info.nit | 6 +++--- src/syntax/icode_generation.nit | 10 +++++----- 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/clib/nit_common.h b/clib/nit_common.h index c3b056f..f489205 100644 --- a/clib/nit_common.h +++ b/clib/nit_common.h @@ -33,6 +33,7 @@ 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; 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 struct Nit_NativeArray {const classtable_elt_t * vft; bigint object_id; bigint size; val_t val[1];} * Nit_NativeArray; typedef classtable_elt_t * classtable_t; /* classtable */ @@ -86,6 +87,7 @@ extern bigint object_id_counter; #define ISOBJ(x) (TAG((x)) == OBJTAG) #define VAL2OBJ(x) ((obj_t)(x)) +#define VAL2ARRAY(x) ((Nit_NativeArray)(x)) #define OBJ2VAL(o) ((val_t)(o)) #define VAL2VFT(x) (ISOBJ(x) ? VAL2OBJ(x)->vft : TAG2VFT[TAG(x)]) /*#define VAL2CID(x) (ISOBJ(x) ? (VAL2OBJ(x)->vft->cid) : (-TAG(x)))*/ @@ -97,6 +99,7 @@ extern bigint object_id_counter; /* Equal comparaison */ /* O = Non nil object ; N = Object or nil ; P = Primitive */ #define OBJ_IS_BOX(x) ((VAL2OBJ(x)->vft->i) < 0) +#define OBJ_IS_ARRAY(x) ((VAL2ARRAY(x)->vft[1].i) == -1) #define IS_BOX(x) (ISOBJ(x) && OBJ_IS_BOX(x)) #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)))) diff --git a/src/compiling/compiling_global.nit b/src/compiling/compiling_global.nit index b2a9838..23f5791 100644 --- a/src/compiling/compiling_global.nit +++ b/src/compiling/compiling_global.nit @@ -831,7 +831,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 +878,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] diff --git a/src/primitive_info.nit b/src/primitive_info.nit index db9d38b..41e0cab 100644 --- a/src/primitive_info.nit +++ b/src/primitive_info.nit @@ -62,9 +62,9 @@ redef class MMLocalClass private fun primitive_ctypes: HashMap[Symbol, PrimitiveInfo] do var res = new HashMap[Symbol, PrimitiveInfo] - var pnames = ["Int", "Char", "Bool", "Float", "NativeString", "NativeArray", "Pointer"] - var tagged = [true, true, true, false, false, false, false] - var cnames = ["bigint", "char", "int", "float", "char *", "val_t *", "void *"] + var pnames = ["Int", "Char", "Bool", "Float", "NativeString", "Pointer"] + var tagged = [true, true, true, false, false, false] + var cnames = ["bigint", "char", "int", "float", "char *", "void *"] for i in [0..pnames.length[ do var n = pnames[i].to_symbol var pi = new PrimitiveInfo(n, tagged[i], cnames[i]) diff --git a/src/syntax/icode_generation.nit b/src/syntax/icode_generation.nit index 20e2a90..e63a2f9 100644 --- a/src/syntax/icode_generation.nit +++ b/src/syntax/icode_generation.nit @@ -544,16 +544,16 @@ redef class AInternMethPropdef end else if c == once "NativeArray".to_symbol then if n == once "object_id".to_symbol then - s = "TAG_Int(UNBOX_NativeArray(@@@))" + s = "TAG_Int(((Nit_NativeArray)@@@)->object_id)" else if n == once "[]".to_symbol then - s = "UNBOX_NativeArray(@@@)[UNTAG_Int(@@@)]" + s = "((Nit_NativeArray)@@@)->val[UNTAG_Int(@@@)]" else if n == once "[]=".to_symbol then - s = "UNBOX_NativeArray(@@@)[UNTAG_Int(@@@)]=@@@;" + s = "((Nit_NativeArray)@@@)->val[UNTAG_Int(@@@)]=@@@" else if n == once "copy_to".to_symbol then var t = p[0] p[0] = p[1] p[1] = t - s = "(void)memcpy(UNBOX_NativeArray(@@@), UNBOX_NativeArray(@@@), UNTAG_Int(@@@)*sizeof(val_t));" + s = "(void)memcpy(((Nit_NativeArray )@@@)->val, ((Nit_NativeArray)@@@)->val, UNTAG_Int(@@@)*sizeof(val_t))" end else if c == once "NativeString".to_symbol then if n == once "object_id".to_symbol then @@ -583,7 +583,7 @@ redef class AInternMethPropdef s = "exit(UNTAG_Int(@@@));" else if n == once "calloc_array".to_symbol then p[0] = p[1] - s = "BOX_NativeArray((val_t*)malloc((UNTAG_Int(@@@) * sizeof(val_t))))" + s = "NEW_NativeArray(UNTAG_Int(@@@), sizeof(val_t))" else if n == once "calloc_string".to_symbol then p[0] = p[1] s = "BOX_NativeString((char*)malloc((UNTAG_Int(@@@) * sizeof(char))))" -- 1.7.9.5