4 /* This file is part of NIT ( http://www.nitlanguage.org ).
6 * Copyright 2006-2009 Jean Privat <jean@pryen.org>
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
22 typedef signed long int bigint
; /* standard int value, must be larger that any poiner */
23 typedef bigint (*fun_t
) (bigint
); /* generic function pointer */
24 typedef bigint cid_t
; /* class identifier */
25 typedef bigint val_t
; /* value (everything is a val_t) */
26 typedef union obj_tu
{union classtable_elt_tu
* vft
; bigint object_id
; val_t objectSize
;} *obj_t
; /* standard object */
27 typedef union classtable_elt_tu
{ bigint i
; fun_t f
; cid_t cid
;} classtable_elt_t
; /* classtable element */
28 typedef struct Nit_NativeArray
{const classtable_elt_t
* vft
; bigint object_id
; bigint size
; val_t val
[1];} * Nit_NativeArray
;
30 typedef classtable_elt_t
* classtable_t
; /* classtable */
32 extern bigint object_id_counter
;
34 /*****************************************************************************
35 * Types macros (primitive and less primitives) ******************************
36 *****************************************************************************
40 * OBJ (obj_t) : standard object representation (including boxes + NIL)
41 * Int (int) : integers
42 * Char (car) : characters
43 * Bool (int) : booleans (true or false)
45 * X (x_t) : generic representatio of the previous four types
49 * int ISX(val_t v) : true if v is a X
50 * x_t VAL2X(val_t v) : convert a val_t to a primitive type (you should check ISX before)
51 * val_t X2VAL(x_t x) : convert a type to a val_t
52 * int XTAG : numeric identifier of a type
53 * int TAG(val_t v) : return the XTAG (ie TAG(Char2VAL('e')) == CharTag)
55 * classtable_t VAL2VFT(val_t v): the virtual function table of a value
57 * val_t NIT_NULL : the instance of the None class
58 *****************************************************************************/
60 #define TAG(x) ((int)(x) & 3)
64 #define TAG_Int(x) ((val_t)(((x)<<2)|IntTAG))
65 #define UNTAG_Int(x) ((bigint)(x)>>2)
69 #define TAG_Char(x) ((val_t)((((int)(x))<<2)|CharTAG))
70 #define UNTAG_Char(x) ((char)((int)(x)>>2))
74 #define TAG_Bool(x) ((val_t)(((x)<<2)|BoolTAG))
75 #define UNTAG_Bool(x) ((int)(x)>>2)
80 #define ISOBJ(x) (TAG((x)) == OBJTAG)
81 #define VAL2OBJ(x) ((obj_t)(x))
82 #define VAL2ARRAY(x) ((Nit_NativeArray)(x))
83 #define OBJ2VAL(o) ((val_t)(o))
84 #define VAL2VFT(x) (ISOBJ(x) ? VAL2OBJ(x)->vft : TAG2VFT[TAG(x)])
85 /*#define VAL2CID(x) (ISOBJ(x) ? (VAL2OBJ(x)->vft->cid) : (-TAG(x)))*/
86 #define VAL2CID(x) (VAL2OBJ(x)->vft->cid)
88 #define NIT_NULL ((val_t)0)
89 #define ISNULL(x) ((x)==NIT_NULL)
91 /* Equal comparaison */
92 /* O = Non nil object ; N = Object or nil ; P = Primitive */
93 #define OBJ_IS_BOX(x) ((VAL2OBJ(x)->vft->i) < 0)
94 #define OBJ_IS_ARRAY(x) ((VAL2ARRAY(x)->vft[1].i) == -1)
95 #define IS_BOX(x) (ISOBJ(x) && OBJ_IS_BOX(x))
96 #define IS_EQUAL_BOX(x, y) (ISOBJ(y) && (VAL2OBJ(x)[2].vft==VAL2OBJ(y)[2].vft) && (VAL2OBJ(x)->vft==VAL2OBJ(y)->vft))
97 #define IS_EQUAL_OO(x, y) ((x)==(y) || (IS_BOX(x) && IS_EQUAL_BOX((x), (y))))
98 #define IS_EQUAL_ON(x, y) ((x)==(y) || (IS_BOX(x) && !ISNULL(y) && IS_EQUAL_BOX((x), (y))))
99 #define IS_EQUAL_NN(x, y) ((x)==(y) || (!ISNULL(x) && IS_BOX(x) && !ISNULL(y) && IS_EQUAL_BOX((x), (y))))
101 /* Subclass comparaison */
102 /* check is e is a subtype of c2 */
103 /* Warning, only subclasse comparaison is performed, not subtype */
104 /* e is a val (not nil) */
105 /* c a class name (eg: Int) */
106 #define VALISA(e, c) (VAL2VFT(e)[COLOR_ ## c].cid == (cid_t) ID_ ## c)
107 #define OBJISA(e, c) (VAL2OBJ(e)->vft[COLOR_ ## c].cid == (cid_t) ID_ ## c)
109 #define VAL_ISA(e, c, i) (VAL2VFT((e))[(c)].cid == (cid_t)(i))
111 /* GC and memory management */
112 void *alloc(size_t); /* allocate memory to store an object with an object header */
113 void *raw_alloc(size_t); /* allocate raw memory to store a raw stram of byte */
114 void register_static_object(val_t
*); /* mark that something is a global or once object */
117 extern val_t G_stdin
;
118 extern val_t G_stdout
;
119 extern val_t G_stderr
;
122 extern int glob_argc
;
123 extern char ** glob_argv
;
127 * - store local variables (REGS) of functions
128 * - store context for closure
129 * - provide information for stack dump
131 struct stack_frame_t
{
132 struct stack_frame_t
*prev
; /* previous stack frame */
133 const char *file
; /* source filename (.nit) */
134 int line
; /* line number (in the source) */
135 const char *meth
; /* human function name (usually the method name) */
136 struct stack_frame_t
*closure_ctx
; /* closure context (for functions that have closure parameters) */
137 fun_t
*closure_funs
; /* closure functions (for functions that have closure parameters) */
138 int has_broke
; /* has an escape occured? 0 if false, label_idx (>0) if true */
139 int REG_size
; /* number of local variables */
140 val_t REG
[1]; /* local variables (flexible array) */
142 extern struct stack_frame_t
*stack_frame_head
;
145 typedef enum {true = (1==1),false = (0==1)} bool;
149 #define CALL(r,c) ((VAL2VFT(r)[c].f))
150 #define ATTR(r,c) (*(val_t*)(VAL2OBJ(r)+c))
151 #define ATTRS(r,c,o) ((VAL2OBJ(r)+VAL2VFT(r)[c].i)[o].attr)
153 void prepare_signals(void);
154 extern classtable_t TAG2VFT
[4];