Automatically enable Bohem's GC if available.
[nit.git] / lib / nit_common.h
1 #ifndef NIT_COMMON_H
2 #define NIT_COMMON_H
3
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7
8 #ifdef WITH_LIBGC
9 #include <gc/gc.h>
10 #define malloc(x) GC_MALLOC((x))
11 #define calloc(x,y) GC_MALLOC((x)*(y))
12 #endif
13
14 /* *** Types *** */
15 typedef int (*fun_t)(int); /* generic function pointer */
16 typedef unsigned int cid_t; /* class identifier */
17 typedef unsigned long int val_t; /* value (everything is a val_t) */
18 typedef union obj_tu {union classtable_elt_tu * vft; val_t attr;} *obj_t; /* standard object */
19 typedef union classtable_elt_tu { int i; fun_t f; cid_t cid;} classtable_elt_t; /* classtable element */
20
21 typedef classtable_elt_t * classtable_t; /* classtable */
22
23 /*****************************************************************************
24 * Types macros (primitive and less primitives) ******************************
25 *****************************************************************************
26 *
27 * ** types are: **
28 *
29 * OBJ (obj_t) : standard object representation (including boxes + NIL)
30 * Int (int) : integers
31 * Char (car) : characters
32 * Bool (int) : booleans (true or false)
33 *
34 * X (x_t) : generic representatio of the previous four types
35 *
36 * ** macros are: **
37 *
38 * int ISX(val_t v) : true if v is a X
39 * x_t VAL2X(val_t v) : convert a val_t to a primitive type (you should check ISX before)
40 * val_t X2VAL(x_t x) : convert a type to a val_t
41 * int XTAG : numeric identifier of a type
42 * int TAG(val_t v) : return the XTAG (ie TAG(Char2VAL('e')) == CharTag)
43 *
44 * classtable_t VAL2VFT(val_t v): the virtual function table of a value
45 *
46 * val_t NIT_NULL : the instance of the None class
47 *****************************************************************************/
48
49 #define TAG(x) ((int)(x) & 3)
50 #ifndef IntTAG
51 # define IntTAG 1
52 #endif
53 #define TAG_Int(x) ((val_t)(((x)<<2)|IntTAG))
54 #define UNTAG_Int(x) ((int)(x)>>2)
55 #ifndef CharTAG
56 # define CharTAG 2
57 #endif
58 #define TAG_Char(x) ((val_t)((((int)(x))<<2)|CharTAG))
59 #define UNTAG_Char(x) ((char)((int)(x)>>2))
60 #ifndef BoolTAG
61 # define BoolTAG 3
62 #endif
63 #define TAG_Bool(x) ((val_t)(((x)<<2)|BoolTAG))
64 #define UNTAG_Bool(x) ((int)(x)>>2)
65 #ifndef OBJTAG
66 # define OBJTAG 0
67 #endif
68
69 #define ISOBJ(x) (TAG((x)) == OBJTAG)
70 #define VAL2OBJ(x) ((obj_t)(x))
71 #define OBJ2VAL(o) ((val_t)(o))
72 #define VAL2VFT(x) (ISOBJ(x) ? VAL2OBJ(x)->vft : TAG2VFT[TAG(x)])
73 /*#define VAL2CID(x) (ISOBJ(x) ? (VAL2OBJ(x)->vft->cid) : (-TAG(x)))*/
74 #define VAL2CID(x) (VAL2OBJ(x)->vft->cid)
75
76 #define NIT_NULL ((val_t)0)
77 #define ISNULL(x) ((x)==NIT_NULL)
78
79 /* Equal comparaison */
80 /* O = Non nil object ; N = Object or nil ; P = Primitive */
81 #define OBJ_IS_BOX(x) ((VAL2OBJ(x)->vft->i) < 0)
82 #define IS_BOX(x) (ISOBJ(x) && OBJ_IS_BOX(x))
83 #define IS_EQUAL_BOX(x, y) (ISOBJ(y) && (VAL2OBJ(x)[1].vft==VAL2OBJ(y)[1].vft) && (VAL2OBJ(x)->vft==VAL2OBJ(y)->vft))
84 #define IS_EQUAL_OO(x, y) ((x)==(y) || (IS_BOX(x) && IS_EQUAL_BOX((x), (y))))
85 #define IS_EQUAL_ON(x, y) ((x)==(y) || (IS_BOX(x) && !ISNULL(y) && IS_EQUAL_BOX((x), (y))))
86 #define IS_EQUAL_NN(x, y) ((x)==(y) || (!ISNULL(x) && IS_BOX(x) && !ISNULL(y) && IS_EQUAL_BOX((x), (y))))
87
88 /* Subclass comparaison */
89 /* check is e is a subtype of c2 */
90 /* Warning, only subclasse comparaison is performed, not subtype */
91 /* e is a val (not nil) */
92 /* c a class name (eg: Int) */
93 #define VALISA(e, c) (VAL2VFT(e)[COLOR_ ## c].cid == (cid_t) ID_ ## c)
94 #define OBJISA(e, c) (VAL2OBJ(e)->vft[COLOR_ ## c].cid == (cid_t) ID_ ## c)
95
96 #define VAL_ISA(e, c, i) (VAL2VFT((e))[(c)].cid == (cid_t)(i))
97
98 void * alloc(size_t);
99 extern val_t G_args;
100 extern val_t G_stdin;
101 extern val_t G_stdout;
102 extern val_t G_stderr;
103 extern val_t G_sys;
104
105 extern int glob_argc;
106 extern char ** glob_argv;
107
108 struct trace_t {
109 struct trace_t *prev; /* previous stack frame */
110 const char *file; /* source filename */
111 int line; /* line number */
112 const char *meth; /* method name */
113 };
114 extern struct trace_t *tracehead;
115 typedef enum {true = (1==1),false = (0==1)} bool;
116
117 void nit_exit(int);
118
119 #define CALL(r,c) ((VAL2VFT(r)[c].f))
120 #define ATTR(r,c) (*(val_t*)(VAL2OBJ(r)+c))
121 #define ATTRS(r,c,o) ((VAL2OBJ(r)+VAL2VFT(r)[c].i)[o].attr)
122
123 void prepare_signals(void);
124 extern classtable_t TAG2VFT[4];
125 #endif