5 #define CFFI_BUFSIZE 65536
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
85 char* cffi_type_name(int signed_p
, int size
)
87 static char buf
[CFFI_BUFSIZE
]; memset(buf
, 0, CFFI_BUFSIZE
);
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;
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;
109 snprintf(buf
, CFFI_BUFSIZE
-1, "(cl:error \"No type of size ~D.\" %d)\n", size
);
123 # define offsetof(type, slot) ((long) ((char *) &(((type *) 0)->slot)))
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
);
143 snprintf(buf
, CFFI_BUFSIZE
-1, "\n \"%s\"", docstring
);
144 fprintf(output
, "(cl:defconstant %s %s%s)\n", name
, value
, buf
);
147 void cffi_deftypesize(FILE *output
, char *type
, int type_size
)
149 static char buf
[CFFI_BUFSIZE
]; memset(buf
, 0, CFFI_BUFSIZE
);
150 snprintf(buf
, CFFI_BUFSIZE
-1, "size-of-%s", type
);
151 cffi_defconstant(output
, buf
, cffi_print_value(type_size
), NULL
);
154 void cffi_defctype(FILE *output
, char *type
, char *canonical_type
, int type_size
)
156 fprintf(output
, "(cffi:defctype %s %s)\n", type
, canonical_type
);
157 cffi_deftypesize(output
, type
, type_size
);
160 void cffi_defcstruct_start(FILE *output
, char *lisp_name
, int type_size
, char *docstring
)
162 static char buf
[CFFI_BUFSIZE
]; memset(buf
, 0, CFFI_BUFSIZE
);
164 snprintf(buf
, CFFI_BUFSIZE
-1, "\n \"%s\"", docstring
);
165 fprintf(output
, "(cffi:defcstruct (%s :size %s)%s", lisp_name
,
166 cffi_print_value(type_size
), buf
);
169 void cffi_signal_missing_definition(FILE *output
, char *lisp_name
)
171 fprintf(output
, "(cl:warn 'iolib-grovel:missing-definition :name '%s)\n", lisp_name
);
174 void cffi_defcstruct_slot(FILE *output
, char *slot_lname
, char *slot_ltype
, int count
,
175 unsigned slot_offset
)
177 fprintf(output
, "\n (%s %s :count %s :offset %s)",
178 slot_lname
, slot_ltype
,
179 cffi_print_value(count
),
180 cffi_print_value(slot_offset
));
183 void cffi_defcunion_slot(FILE *output
, char *slot_lname
, char *slot_ltype
, int count
)
185 fprintf(output
, "\n (%s %s :count %s)",
186 slot_lname
, slot_ltype
,
187 cffi_print_value(count
));
190 void cffi_defcunion_start(FILE *output
, char *lisp_name
, int type_size
, char *docstring
)
192 static char buf
[CFFI_BUFSIZE
]; memset(buf
, 0, CFFI_BUFSIZE
);
194 snprintf(buf
, CFFI_BUFSIZE
-1, "\n \"%s\"", docstring
);
195 fprintf(output
, "(cffi:defcunion (%s :size %s)%s", lisp_name
,
196 cffi_print_value(type_size
), buf
);
199 void cffi_defcenum_start(FILE *output
, char *lisp_name
, char *base_type
, char *docstring
)
201 static char buf
[CFFI_BUFSIZE
]; memset(buf
, 0, CFFI_BUFSIZE
);
203 snprintf(buf
, CFFI_BUFSIZE
-1, "\n \"%s\"", docstring
);
204 fprintf(output
, "(cffi:defcenum (%s %s)%s", lisp_name
, base_type
, buf
);
207 void cffi_defcenum_member(FILE *output
, char *lisp_name
, char *value
, char *docstring
)
209 fprintf(output
, "\n (%s %s)", lisp_name
, value
);
220 #define CFFI_DEFCONSTANT(name, value, docstring) \
221 cffi_defconstant(output, name, cffi_print_value(value), docstring)
223 #define CFFI_DEFTYPESIZE(lisp_name, ctype) \
224 cffi_deftypesize(output, lisp_name, sizeof(ctype))
226 #define CFFI_DEFCTYPE(lisp_name, ctype) \
227 cffi_defctype(output, lisp_name, cffi_type_name(CFFI_TYPE_SIGNED_P(ctype), sizeof(ctype)), sizeof(ctype))
229 #define CFFI_DEFCSTRUCT_START(lisp_name, ctype, docstring) \
230 cffi_defcstruct_start(output, lisp_name, sizeof(ctype), docstring)
232 #define CFFI_DEFCSTRUCT_END \
233 fprintf(output, ")\n")
235 #define CFFI_DEFCSTRUCT_SLOT(struct_name, slot_cname, slot_lname, slot_ltype, count) \
236 cffi_defcstruct_slot(output, slot_lname, slot_ltype, count, \
237 offsetof(struct_name, slot_cname))
239 #define CFFI_DEFCSTRUCT_SLOT_AUTO(struct_name, slot_cname, slot_lname, slot_ltype) \
240 cffi_defcstruct_slot(output, slot_lname, slot_ltype, \
241 ((CFFI_SIZEOFSLOT(struct_name, slot_cname)) / \
242 (CFFI_SIZEOFSLOTMEMBER(struct_name, slot_cname))), \
243 offsetof(struct_name, slot_cname))
245 #define CFFI_DEFCUNION_START(lisp_name, ctype, docstring) \
246 cffi_defcunion_start(output, lisp_name, sizeof(ctype), docstring)
248 #define CFFI_DEFCUNION_END \
249 fprintf(output, ")\n")
251 #define CFFI_DEFCUNION_SLOT(union_name, slot_cname, slot_lname, slot_ltype, count) \
252 cffi_defcunion_slot(output, slot_lname, slot_ltype, count)
254 #define CFFI_DEFCUNION_SLOT_AUTO(union_name, slot_cname, slot_lname, slot_ltype) \
255 cffi_defcunion_slot(output, slot_lname, slot_ltype, \
256 ((CFFI_SIZEOFSLOT(union_name, slot_cname)) / \
257 (CFFI_SIZEOFSLOTMEMBER(union_name, slot_cname))))
259 #define CFFI_DEFCENUM_START(lisp_name, base_type, docstring) \
260 cffi_defcenum_start(output, lisp_name, base_type, docstring)
262 #define CFFI_DEFCENUM_END \
263 fprintf(output, ")\n")
265 #define CFFI_DEFCENUM_MEMBER(lisp_name, cname, docstring) \
266 cffi_defcenum_member(output, lisp_name, cffi_print_value(cname), docstring)