compile: give NativeArray its own C structure
[nit.git] / clib / nit_common.h
1 #ifndef NIT_COMMON_H
2 #define NIT_COMMON_H
3
4 /* This file is part of NIT ( http://www.nitlanguage.org ).
5 *
6 * Copyright 2006-2009 Jean Privat <jean@pryen.org>
7 *
8 * This file is free software, which comes along with NIT. This software is
9 * distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
11 * PARTICULAR PURPOSE. You can modify it is you want, provided this header
12 * is kept unaltered, and a notification of the changes is added.
13 * You are allowed to redistribute it and sell it, alone or is a part of
14 * another product.
15 */
16
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <string.h>
20
21 #ifdef WITH_LIBGC
22 #include <gc/gc.h>
23 #define malloc(x) GC_MALLOC((x))
24 #define calloc(x,y) GC_MALLOC((x)*(y))
25 #endif
26
27
28
29 /* *** Types *** */
30 typedef signed long int bigint; /* standard int value, must be larger that any poiner */
31 typedef bigint (*fun_t) (bigint); /* generic function pointer */
32 typedef bigint cid_t; /* class identifier */
33 typedef bigint val_t; /* value (everything is a val_t) */
34 typedef union obj_tu {union classtable_elt_tu * vft; bigint object_id; val_t objectSize;} *obj_t; /* standard object */
35 typedef union classtable_elt_tu { bigint i; fun_t f; cid_t cid;} classtable_elt_t; /* classtable element */
36 typedef struct Nit_NativeArray {const classtable_elt_t * vft; bigint object_id; bigint size; val_t val[1];} * Nit_NativeArray;
37
38 typedef classtable_elt_t * classtable_t; /* classtable */
39
40 extern bigint object_id_counter;
41
42 /*****************************************************************************
43 * Types macros (primitive and less primitives) ******************************
44 *****************************************************************************
45 *
46 * ** types are: **
47 *
48 * OBJ (obj_t) : standard object representation (including boxes + NIL)
49 * Int (int) : integers
50 * Char (car) : characters
51 * Bool (int) : booleans (true or false)
52 *
53 * X (x_t) : generic representatio of the previous four types
54 *
55 * ** macros are: **
56 *
57 * int ISX(val_t v) : true if v is a X
58 * x_t VAL2X(val_t v) : convert a val_t to a primitive type (you should check ISX before)
59 * val_t X2VAL(x_t x) : convert a type to a val_t
60 * int XTAG : numeric identifier of a type
61 * int TAG(val_t v) : return the XTAG (ie TAG(Char2VAL('e')) == CharTag)
62 *
63 * classtable_t VAL2VFT(val_t v): the virtual function table of a value
64 *
65 * val_t NIT_NULL : the instance of the None class
66 *****************************************************************************/
67
68 #define TAG(x) ((int)(x) & 3)
69 #ifndef IntTAG
70 # define IntTAG 1
71 #endif
72 #define TAG_Int(x) ((val_t)(((x)<<2)|IntTAG))
73 #define UNTAG_Int(x) ((bigint)(x)>>2)
74 #ifndef CharTAG
75 # define CharTAG 2
76 #endif
77 #define TAG_Char(x) ((val_t)((((int)(x))<<2)|CharTAG))
78 #define UNTAG_Char(x) ((char)((int)(x)>>2))
79 #ifndef BoolTAG
80 # define BoolTAG 3
81 #endif
82 #define TAG_Bool(x) ((val_t)(((x)<<2)|BoolTAG))
83 #define UNTAG_Bool(x) ((int)(x)>>2)
84 #ifndef OBJTAG
85 # define OBJTAG 0
86 #endif
87
88 #define ISOBJ(x) (TAG((x)) == OBJTAG)
89 #define VAL2OBJ(x) ((obj_t)(x))
90 #define VAL2ARRAY(x) ((Nit_NativeArray)(x))
91 #define OBJ2VAL(o) ((val_t)(o))
92 #define VAL2VFT(x) (ISOBJ(x) ? VAL2OBJ(x)->vft : TAG2VFT[TAG(x)])
93 /*#define VAL2CID(x) (ISOBJ(x) ? (VAL2OBJ(x)->vft->cid) : (-TAG(x)))*/
94 #define VAL2CID(x) (VAL2OBJ(x)->vft->cid)
95
96 #define NIT_NULL ((val_t)0)
97 #define ISNULL(x) ((x)==NIT_NULL)
98
99 /* Equal comparaison */
100 /* O = Non nil object ; N = Object or nil ; P = Primitive */
101 #define OBJ_IS_BOX(x) ((VAL2OBJ(x)->vft->i) < 0)
102 #define OBJ_IS_ARRAY(x) ((VAL2ARRAY(x)->vft[1].i) == -1)
103 #define IS_BOX(x) (ISOBJ(x) && OBJ_IS_BOX(x))
104 #define IS_EQUAL_BOX(x, y) (ISOBJ(y) && (VAL2OBJ(x)[2].vft==VAL2OBJ(y)[2].vft) && (VAL2OBJ(x)->vft==VAL2OBJ(y)->vft))
105 #define IS_EQUAL_OO(x, y) ((x)==(y) || (IS_BOX(x) && IS_EQUAL_BOX((x), (y))))
106 #define IS_EQUAL_ON(x, y) ((x)==(y) || (IS_BOX(x) && !ISNULL(y) && IS_EQUAL_BOX((x), (y))))
107 #define IS_EQUAL_NN(x, y) ((x)==(y) || (!ISNULL(x) && IS_BOX(x) && !ISNULL(y) && IS_EQUAL_BOX((x), (y))))
108
109 /* Subclass comparaison */
110 /* check is e is a subtype of c2 */
111 /* Warning, only subclasse comparaison is performed, not subtype */
112 /* e is a val (not nil) */
113 /* c a class name (eg: Int) */
114 #define VALISA(e, c) (VAL2VFT(e)[COLOR_ ## c].cid == (cid_t) ID_ ## c)
115 #define OBJISA(e, c) (VAL2OBJ(e)->vft[COLOR_ ## c].cid == (cid_t) ID_ ## c)
116
117 #define VAL_ISA(e, c, i) (VAL2VFT((e))[(c)].cid == (cid_t)(i))
118
119 void * alloc(size_t);
120 extern val_t G_args;
121 extern val_t G_stdin;
122 extern val_t G_stdout;
123 extern val_t G_stderr;
124 extern val_t G_sys;
125
126 extern int glob_argc;
127 extern char ** glob_argv;
128
129 struct trace_t {
130 struct trace_t *prev; /* previous stack frame */
131 const char *file; /* source filename */
132 int line; /* line number */
133 const char *meth; /* method name */
134 };
135 extern struct trace_t *tracehead;
136 typedef enum {true = (1==1),false = (0==1)} bool;
137
138 void nit_exit(int);
139
140 #define CALL(r,c) ((VAL2VFT(r)[c].f))
141 #define ATTR(r,c) (*(val_t*)(VAL2OBJ(r)+c))
142 #define ATTRS(r,c,o) ((VAL2OBJ(r)+VAL2VFT(r)[c].i)[o].attr)
143
144 void prepare_signals(void);
145 extern classtable_t TAG2VFT[4];
146
147 /* This structure is used to store closure.
148 * Specific closure use a specific fun parameter.
149 */
150 struct WBT_ {fun_t fun; val_t *has_broke; val_t broke_value; val_t *variable; struct WBT_ **closurevariable;};
151 #endif