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