Move all system definitions to iolib.asd
[iolib.git] / src / grovel / grovel-common.h
blobea3b43ea9b36a0f9e09091c589993fd0ded907c0
1 #include <cstdio>
2 #include <cstdlib>
3 #include <cstring>
5 #define CFFI_BUFSIZE 65536
7 \f
9 ///////////
11 // Printers
13 ///////////
15 char* cffi_print_value(signed int value)
17 static char buf[CFFI_BUFSIZE]; memset(buf, 0, CFFI_BUFSIZE);
18 snprintf(buf, CFFI_BUFSIZE-1, "%d.", value);
19 return buf;
22 char* cffi_print_value(unsigned int value)
24 static char buf[CFFI_BUFSIZE]; memset(buf, 0, CFFI_BUFSIZE);
25 snprintf(buf, CFFI_BUFSIZE-1, "%u.", value);
26 return buf;
29 char* cffi_print_value(signed long value)
31 static char buf[CFFI_BUFSIZE]; memset(buf, 0, CFFI_BUFSIZE);
32 snprintf(buf, CFFI_BUFSIZE-1, "%ld.", value);
33 return buf;
36 char* cffi_print_value(unsigned long value)
38 static char buf[CFFI_BUFSIZE]; memset(buf, 0, CFFI_BUFSIZE);
39 snprintf(buf, CFFI_BUFSIZE-1, "%lu.", value);
40 return buf;
43 char* cffi_print_value(signed long long value)
45 static char buf[CFFI_BUFSIZE]; memset(buf, 0, CFFI_BUFSIZE);
46 snprintf(buf, CFFI_BUFSIZE-1, "%lld.", value);
47 return buf;
50 char* cffi_print_value(unsigned long long value)
52 static char buf[CFFI_BUFSIZE]; memset(buf, 0, CFFI_BUFSIZE);
53 snprintf(buf, CFFI_BUFSIZE-1, "%llu.", value);
54 return buf;
57 char* cffi_print_value(double value)
59 static char buf[CFFI_BUFSIZE]; memset(buf, 0, CFFI_BUFSIZE);
60 snprintf(buf, CFFI_BUFSIZE-1, "#.(let ((*read-default-float-format* 'double-float)) (read-from-string \"%.20E\"))", value);
61 return buf;
64 char* cffi_print_value(void *value)
66 static char buf[CFFI_BUFSIZE]; memset(buf, 0, CFFI_BUFSIZE);
67 snprintf(buf, CFFI_BUFSIZE-1, "%lld.", (signed long long)value);
68 return buf;
71 char* cffi_print_value(void (*value)())
73 static char buf[CFFI_BUFSIZE]; memset(buf, 0, CFFI_BUFSIZE);
74 snprintf(buf, CFFI_BUFSIZE-1, "%lld.", (signed long long)value);
75 return buf;
78 char* cffi_print_value(void (*value)(int))
80 static char buf[CFFI_BUFSIZE]; memset(buf, 0, CFFI_BUFSIZE);
81 snprintf(buf, CFFI_BUFSIZE-1, "%lld.", (signed long long)value);
82 return buf;
85 char* cffi_type_name(int signed_p, int size)
87 static char buf[CFFI_BUFSIZE]; memset(buf, 0, CFFI_BUFSIZE);
88 if (signed_p) {
89 switch (size) {
90 case 1: snprintf(buf, CFFI_BUFSIZE-1, ":int8"); break;
91 case 2: snprintf(buf, CFFI_BUFSIZE-1, ":int16"); break;
92 case 4: snprintf(buf, CFFI_BUFSIZE-1, ":int32"); break;
93 case 8: snprintf(buf, CFFI_BUFSIZE-1, ":int64"); break;
94 default: goto error;
96 } else {
97 switch(size) {
98 case 1: snprintf(buf, CFFI_BUFSIZE-1, ":uint8"); break;
99 case 2: snprintf(buf, CFFI_BUFSIZE-1, ":uint16"); break;
100 case 4: snprintf(buf, CFFI_BUFSIZE-1, ":uint32"); break;
101 case 8: snprintf(buf, CFFI_BUFSIZE-1, ":uint64"); break;
102 default: goto error;
106 goto ok;
108 error:
109 snprintf(buf, CFFI_BUFSIZE-1, "(cl:error \"No type of size ~D.\" %d)\n", size);
111 return buf;
116 ////////////////
118 // Helper macros
120 ////////////////
122 #ifndef offsetof
123 # define offsetof(type, slot) ((long) ((char *) &(((type *) 0)->slot)))
124 #endif
125 #define CFFI_SIZEOFSLOT(type, slot) (sizeof(((type *) 0)->slot))
126 #define CFFI_SIZEOFSLOTMEMBER(type, slot) (sizeof(((type *) 0)->slot[0]))
128 #define CFFI_STRINGIFY(x) #x
129 #define CFFI_TYPE_SIGNED_P(type) (((type)-1)<0LL)
133 ///////////////////////
135 // Definers - functions
137 ///////////////////////
139 void cffi_defconstant(FILE *output, char *name, char *value, char *docstring)
141 static char buf[CFFI_BUFSIZE]; memset(buf, 0, CFFI_BUFSIZE);
142 if (docstring)
143 snprintf(buf, CFFI_BUFSIZE-1, "\n \"%s\"", docstring);
144 fprintf(output, "(cl:defconstant %s %s%s)\n", name, value, buf);
147 void cffi_deftypedef(FILE *output, char *lisp_name, char *aggregate_type)
149 fprintf(output, "(cffi:defctype %s (:%s %s))\n",
150 lisp_name, aggregate_type, lisp_name);
153 void cffi_deftypesize(FILE *output, char *type, int type_size)
155 static char buf[CFFI_BUFSIZE]; memset(buf, 0, CFFI_BUFSIZE);
156 snprintf(buf, CFFI_BUFSIZE-1, "size-of-%s", type);
157 cffi_defconstant(output, buf, cffi_print_value(type_size), NULL);
160 void cffi_defctype(FILE *output, char *type, char *canonical_type, int type_size)
162 fprintf(output, "(cffi:defctype %s %s)\n", type, canonical_type);
163 cffi_deftypesize(output, type, type_size);
166 void cffi_defcstruct_start(FILE *output, char *lisp_name, int type_size, char *docstring)
168 static char buf[CFFI_BUFSIZE]; memset(buf, 0, CFFI_BUFSIZE);
169 if (docstring)
170 snprintf(buf, CFFI_BUFSIZE-1, "\n \"%s\"", docstring);
171 fprintf(output, "(cffi:defcstruct (%s :size %s)%s", lisp_name,
172 cffi_print_value(type_size), buf);
175 void cffi_signal_missing_definition(FILE *output, char *lisp_name)
177 fprintf(output, "(cl:warn 'iolib-grovel:missing-definition :name '%s)\n", lisp_name);
180 void cffi_defcstruct_slot(FILE *output, char *slot_lname, char *slot_ltype, int count,
181 unsigned slot_offset)
183 fprintf(output, "\n (%s %s :count %s :offset %s)",
184 slot_lname, slot_ltype,
185 cffi_print_value(count),
186 cffi_print_value(slot_offset));
189 void cffi_defcunion_slot(FILE *output, char *slot_lname, char *slot_ltype, int count)
191 fprintf(output, "\n (%s %s :count %s)",
192 slot_lname, slot_ltype,
193 cffi_print_value(count));
196 void cffi_defcunion_start(FILE *output, char *lisp_name, int type_size, char *docstring)
198 static char buf[CFFI_BUFSIZE]; memset(buf, 0, CFFI_BUFSIZE);
199 if (docstring)
200 snprintf(buf, CFFI_BUFSIZE-1, "\n \"%s\"", docstring);
201 fprintf(output, "(cffi:defcunion (%s :size %s)%s", lisp_name,
202 cffi_print_value(type_size), buf);
205 void cffi_defcenum_start(FILE *output, char *lisp_name, char *base_type, char *docstring)
207 static char buf[CFFI_BUFSIZE]; memset(buf, 0, CFFI_BUFSIZE);
208 if (docstring)
209 snprintf(buf, CFFI_BUFSIZE-1, "\n \"%s\"", docstring);
210 fprintf(output, "(cffi:defcenum (%s %s)%s", lisp_name, base_type, buf);
213 void cffi_defcenum_member(FILE *output, char *lisp_name, char *value, char *docstring)
215 fprintf(output, "\n (%s %s)", lisp_name, value);
220 ////////////////////
222 // Definers - macros
224 ////////////////////
226 #define CFFI_DEFCONSTANT(name, value, docstring) \
227 cffi_defconstant(output, name, cffi_print_value(value), docstring)
229 #define CFFI_DEFTYPEDEF(lisp_name, aggregate_type) \
230 cffi_deftypedef(output, lisp_name, aggregate_type)
232 #define CFFI_DEFTYPESIZE(lisp_name, ctype) \
233 cffi_deftypesize(output, lisp_name, sizeof(ctype))
235 #define CFFI_DEFCTYPE(lisp_name, ctype) \
236 cffi_defctype(output, lisp_name, cffi_type_name(CFFI_TYPE_SIGNED_P(ctype), sizeof(ctype)), sizeof(ctype))
238 #define CFFI_DEFCSTRUCT_START(lisp_name, ctype, docstring) \
239 cffi_defcstruct_start(output, lisp_name, sizeof(ctype), docstring)
241 #define CFFI_DEFCSTRUCT_END \
242 fprintf(output, ")\n")
244 #define CFFI_DEFCSTRUCT_SLOT(struct_name, slot_cname, slot_lname, slot_ltype, count) \
245 cffi_defcstruct_slot(output, slot_lname, slot_ltype, count, \
246 offsetof(struct_name, slot_cname))
248 #define CFFI_DEFCSTRUCT_SLOT_AUTO(struct_name, slot_cname, slot_lname, slot_ltype) \
249 cffi_defcstruct_slot(output, slot_lname, slot_ltype, \
250 ((CFFI_SIZEOFSLOT(struct_name, slot_cname)) / \
251 (CFFI_SIZEOFSLOTMEMBER(struct_name, slot_cname))), \
252 offsetof(struct_name, slot_cname))
254 #define CFFI_DEFCUNION_START(lisp_name, ctype, docstring) \
255 cffi_defcunion_start(output, lisp_name, sizeof(ctype), docstring)
257 #define CFFI_DEFCUNION_END \
258 fprintf(output, ")\n")
260 #define CFFI_DEFCUNION_SLOT(union_name, slot_cname, slot_lname, slot_ltype, count) \
261 cffi_defcunion_slot(output, slot_lname, slot_ltype, count)
263 #define CFFI_DEFCUNION_SLOT_AUTO(union_name, slot_cname, slot_lname, slot_ltype) \
264 cffi_defcunion_slot(output, slot_lname, slot_ltype, \
265 ((CFFI_SIZEOFSLOT(union_name, slot_cname)) / \
266 (CFFI_SIZEOFSLOTMEMBER(union_name, slot_cname))))
268 #define CFFI_DEFCENUM_START(lisp_name, base_type, docstring) \
269 cffi_defcenum_start(output, lisp_name, base_type, docstring)
271 #define CFFI_DEFCENUM_END \
272 fprintf(output, ")\n")
274 #define CFFI_DEFCENUM_MEMBER(lisp_name, cname, docstring) \
275 cffi_defcenum_member(output, lisp_name, cffi_print_value(cname), docstring)