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 */
#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)))*/
/* 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))))
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
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]
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])
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
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))))"