NOTICE: Fix years
[nit.git] / c_src / nit.common.h
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <sys/types.h>
5
6 #include <unistd.h>
7
8 #include <stdint.h>
9
10 #ifdef __linux__
11 #include <endian.h>
12 #endif
13 #include <inttypes.h>
14
15 #include "gc_chooser.h"
16 #ifdef __APPLE__
17 #include <libkern/OSByteOrder.h>
18 #define be32toh(x) OSSwapBigToHostInt32(x)
19 #endif
20 #ifdef __pnacl__
21 #define be16toh(val) (((val) >> 8) | ((val) << 8))
22 #define be32toh(val) ((be16toh((val) << 16) | (be16toh((val) >> 16))))
23 #endif
24 #ifdef ANDROID
25 #define be32toh(val) betoh32(val)
26 #include <android/log.h>
27 #define PRINT_ERROR(...) (void)__android_log_print(ANDROID_LOG_WARN, "Nit", __VA_ARGS__)
28 #else
29 #define PRINT_ERROR(...) fprintf(stderr, __VA_ARGS__)
30 #endif
31 typedef void(*nitmethod_t)(void); /* general C type representing a Nit method. */
32 typedef union {
33 void* val;
34 long l;
35 short int s;
36 unsigned char b;
37 uint32_t c;
38 double d;
39 void* str;
40 int8_t i8;
41 int16_t i16;
42 uint16_t u16;
43 int32_t i32;
44 uint32_t u32;
45 } nitattribute_t; /* general C type representing a Nit attribute. */
46 struct class { int box_kind; nitmethod_t vft[]; }; /* general C type representing a Nit class. */
47 struct type { int id; const char *name; int color; short int is_nullable; const struct types *resolution_table; int table_size; int type_table[]; }; /* general C type representing a Nit type. */
48 struct instance { const struct type *type; const struct class *class; nitattribute_t attrs[]; }; /* general C type representing a Nit instance. */
49 struct types { int dummy; const struct type *types[]; }; /* a list types (used for vts, fts and unresolved lists). */
50 typedef struct instance val; /* general C type representing a Nit instance. */
51 extern const struct class *class_info[];
52 extern const struct type *type_info[];
53 struct nitni_instance {
54 struct nitni_instance *next,
55 *prev; /* adjacent global references in global list */
56 int count; /* number of time this global reference has been marked */
57 struct instance *value;
58 };
59
60 /* Native reference to Nit objects */
61 /* This structure is used to represent every Nit type in extern methods and custom C code. */
62 struct nitni_ref {
63 struct nitni_ref *next,
64 *prev; /* adjacent global references in global list */
65 int count; /* number of time this global reference has been marked */
66 };
67
68 /* List of global references from C code to Nit objects */
69 /* Instanciated empty at init of Nit system and filled explicitly by user in C code */
70 struct nitni_global_ref_list_t {
71 struct nitni_ref *head, *tail;
72 };
73 extern struct nitni_global_ref_list_t *nitni_global_ref_list;
74
75 /* Initializer of global reference list */
76 extern void nitni_global_ref_list_init();
77
78 /* Intern function to add a global reference to the list */
79 extern void nitni_global_ref_add( struct nitni_ref *ref );
80
81 /* Intern function to remove a global reference from the list */
82 extern void nitni_global_ref_remove( struct nitni_ref *ref );
83
84 /* Increase count on an existing global reference */
85 extern void nitni_global_ref_incr( struct nitni_ref *ref );
86
87 /* Decrease count on an existing global reference */
88 extern void nitni_global_ref_decr( struct nitni_ref *ref );
89
90 void fatal_exit(int) __attribute__ ((noreturn));
91 #define likely(x) __builtin_expect((x),1)
92 #define unlikely(x) __builtin_expect((x),0)
93 extern int glob_argc;
94 extern char **glob_argv;
95 extern val *glob_sys;
96 struct instance_core__NativeString {
97 const struct type *type;
98 const struct class *class;
99 void* value;
100 };
101 struct instance_core__Float {
102 const struct type *type;
103 const struct class *class;
104 double value;
105 };
106 struct instance_core__Byte {
107 const struct type *type;
108 const struct class *class;
109 unsigned char value;
110 };
111 struct instance_core__Pointer {
112 const struct type *type;
113 const struct class *class;
114 void* value;
115 };
116 struct instance_core__NativeArray {
117 const struct type *type;
118 const struct class *class;
119 int length;
120 val* values[0];
121 };
122 struct instance_core__Int8 {
123 const struct type *type;
124 const struct class *class;
125 int8_t value;
126 };
127 struct instance_core__Int16 {
128 const struct type *type;
129 const struct class *class;
130 int16_t value;
131 };
132 struct instance_core__UInt16 {
133 const struct type *type;
134 const struct class *class;
135 uint16_t value;
136 };
137 struct instance_core__Int32 {
138 const struct type *type;
139 const struct class *class;
140 int32_t value;
141 };
142 struct instance_core__UInt32 {
143 const struct type *type;
144 const struct class *class;
145 uint32_t value;
146 };