nforce.device: Use AROS_UFIx() macros
[AROS.git] / tools / genmodule / writeincdefines.c
blobe3ad1988ee7d0c4df5e8b86333043a1ababf2b7e
1 /*
2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
3 $Id$
5 Function to write defines/modulename.h. Part of genmodule.
6 */
7 #include "genmodule.h"
9 static void writedefineregister(FILE *, struct functionhead *, struct config *);
10 static void writedefinevararg(FILE *, struct functionhead *, struct config *);
11 static void writealiases(FILE *, struct functionhead *, struct config *);
13 void writeincdefines(struct config *cfg)
15 FILE *out;
16 char line[256], *banner;
17 struct functionhead *funclistit;
19 snprintf(line, 255, "%s/defines/%s.h", cfg->gendir, cfg->modulename);
20 out = fopen(line, "w");
22 if (out == NULL)
24 perror(line);
25 exit(20);
28 banner = getBanner(cfg);
29 fprintf(out,
30 "#ifndef DEFINES_%s_H\n"
31 "#define DEFINES_%s_H\n"
32 "\n"
33 "%s"
34 "\n"
35 "/*\n"
36 " Desc: Defines for %s\n"
37 "*/\n"
38 "\n"
39 "#include <aros/libcall.h>\n"
40 "#include <exec/types.h>\n"
41 "#include <aros/symbolsets.h>\n"
42 "#include <aros/preprocessor/variadic/cast2iptr.hpp>\n"
43 "\n"
44 "__BEGIN_DECLS\n"
45 "\n",
46 cfg->modulenameupper, cfg->modulenameupper, banner, cfg->modulename
48 freeBanner(banner);
50 for (funclistit = cfg->funclist; funclistit!=NULL; funclistit = funclistit->next)
52 if (!funclistit->priv && (funclistit->lvo >= cfg->firstlvo))
54 fprintf(out,
55 "\n"
56 "#if !defined(__%s_LIBAPI__) || (%d <= __%s_LIBAPI__)"
57 "\n",
58 cfg->modulenameupper,
59 funclistit->version,
60 cfg->modulenameupper
63 if (funclistit->libcall != STACK)
65 writedefineregister(out, funclistit, cfg);
66 if (!funclistit->novararg)
67 writedefinevararg(out, funclistit, cfg);
69 else /* libcall == STACK */
71 writedefinestack(out, funclistit, cfg);
74 writealiases(out, funclistit, cfg);
76 fprintf(out,
77 "\n"
78 "#endif /* !defined(__%s_LIBAPI__) || (%d <= __%s_LIBAPI__) */"
79 "\n",
80 cfg->modulenameupper,
81 funclistit->version,
82 cfg->modulenameupper
87 fprintf(out,
88 "\n"
89 "__END_DECLS\n"
90 "\n"
91 "#endif /* DEFINES_%s_H*/\n",
92 cfg->modulenameupper
94 fclose(out);
98 void
99 writedefineregister(FILE *out, struct functionhead *funclistit, struct config *cfg)
101 struct functionarg *arglistit;
102 int count, isvoid;
103 char *type;
105 isvoid = strcmp(funclistit->type, "void") == 0
106 || strcmp(funclistit->type, "VOID") == 0;
108 fprintf(out,
109 "\n"
110 "#define __%s_WB(__%s",
111 funclistit->name, cfg->libbase
113 for (arglistit = funclistit->arguments, count = 1;
114 arglistit!=NULL;
115 arglistit = arglistit->next, count++
118 fprintf(out, ", __arg%d", count);
120 fprintf(out,
121 ") ({\\\n"
123 if (cfg->options & OPTION_AUTOINIT) {
124 fprintf(out,
125 " AROS_LIBREQ(%s,%d)\\\n",
126 cfg->libbase, funclistit->version
129 if (funclistit->arguments == NULL
130 || strchr(funclistit->arguments->reg, '/') == NULL
133 fprintf(out,
134 " AROS_LC%d%s(%s, %s, \\\n",
135 funclistit->argcount, (isvoid) ? "NR" : "",
136 funclistit->type, funclistit->name
139 for (arglistit = funclistit->arguments, count = 1;
140 arglistit!=NULL;
141 arglistit = arglistit->next, count++
144 type = getargtype(arglistit);
145 assert(type != NULL);
146 fprintf(out,
147 " AROS_LCA(%s,(__arg%d),%s), \\\n",
148 type, count, arglistit->reg
150 free(type);
153 else
155 fprintf(out,
156 " AROS_LCQUAD%d%s(%s, %s, \\\n",
157 funclistit->argcount, (isvoid) ? "NR" : "",
158 funclistit->type, funclistit->name
161 for (arglistit = funclistit->arguments, count = 1;
162 arglistit != NULL;
163 arglistit = arglistit->next, count++
166 if (strlen(arglistit->reg) != 5)
168 fprintf(stderr, "Internal error: ../.. register format expected\n");
169 exit(20);
171 arglistit->reg[2] = 0;
173 type = getargtype(arglistit);
174 assert(type != NULL);
176 fprintf(out,
177 " AROS_LCAQUAD(%s, (__arg%d), %s, %s), \\\n",
178 type, count, arglistit->reg, arglistit->reg+3
180 arglistit->reg[2] = '/';
181 free(type);
184 fprintf(out,
185 " %s, (__%s), %u, %s);\\\n})\n\n",
186 cfg->libbasetypeptrextern, cfg->libbase, funclistit->lvo, cfg->basename
189 fprintf(out, "#define %s(", funclistit->name);
190 for (arglistit = funclistit->arguments, count = 1;
191 arglistit != NULL;
192 arglistit = arglistit->next, count++
195 if (arglistit != funclistit->arguments)
196 fprintf(out, ", ");
197 fprintf(out, "arg%d", count);
199 fprintf(out, ") \\\n __%s_WB(%s", funclistit->name, cfg->libbase);
200 for (arglistit = funclistit->arguments, count = 1;
201 arglistit != NULL;
202 arglistit = arglistit->next, count++
204 fprintf(out, ", (arg%d)", count);
205 fprintf(out, ")\n");
208 void
209 writedefinevararg(FILE *out, struct functionhead *funclistit, struct config *cfg)
211 struct functionarg *arglistit = funclistit->arguments;
212 char isvararg = 0, *varargname, *lastname;
214 /* Go to last argument */
215 if (arglistit == NULL)
216 return;
218 while (arglistit->next != NULL) arglistit = arglistit->next;
220 lastname = getargname(arglistit);
221 assert(lastname != NULL);
223 if (*(funclistit->name + strlen(funclistit->name) - 1) == 'A')
225 isvararg = 1;
226 varargname = strdup(funclistit->name);
227 varargname[strlen(funclistit->name)-1] = '\0';
229 else if (strcmp(funclistit->name + strlen(funclistit->name) - 7, "TagList") == 0)
231 isvararg = 1;
232 /* TagList has to be changed in Tags at the end of the functionname */
233 varargname = strdup(funclistit->name);
234 varargname[strlen(funclistit->name)-4] = 's';
235 varargname[strlen(funclistit->name)-3] = '\0';
237 else if (strcmp(funclistit->name + strlen(funclistit->name) - 4, "Args") == 0
238 && (strcasecmp(lastname, "args") == 0 || strcasecmp(lastname, "arglist") == 0)
241 isvararg = 1;
242 varargname = strdup(funclistit->name);
243 varargname[strlen(funclistit->name)-4] = '\0';
245 else if ((funclistit->name[0] == 'V') && (strncmp(arglistit->arg, "va_list", 7) == 0))
247 isvararg = 2;
248 varargname = malloc(strlen(funclistit->name));
249 strcpy(varargname, &funclistit->name[1]);
251 else
253 char *p;
255 if (strncmp(arglistit->arg, "const", 5) == 0) {
256 p = arglistit->arg + 5;
257 while (isspace(*p)) p++;
258 } else
259 p = arglistit->arg;
260 if (strncmp(p, "struct", 6)==0)
262 p += 6;
263 while (isspace(*p)) p++;
264 if (strncmp(p, "TagItem", 7) == 0)
266 p += 7;
267 while (isspace(*p)) p++;
269 if (*p == '*')
271 isvararg = 1;
272 varargname = malloc(strlen(funclistit->name) + 5);
273 strcpy(varargname, funclistit->name);
274 strcat(varargname, "Tags");
280 if (isvararg == 1)
282 int count;
283 char *type;
285 fprintf(out,
286 "\n#if !defined(NO_INLINE_STDARG) && !defined(%s_NO_INLINE_STDARG)\n"
287 "#define %s(",
288 cfg->modulenameupper, varargname
290 for (arglistit = funclistit->arguments, count = 1;
291 arglistit != NULL && arglistit->next != NULL;
292 arglistit = arglistit->next, count++
295 fprintf(out, "arg%d, ", count);
297 fprintf(out,
298 "...) \\\n"
299 "({ \\\n"
300 " %s(",
301 funclistit->name
303 for (arglistit = funclistit->arguments, count = 1;
304 arglistit != NULL;
305 arglistit = arglistit->next, count++
308 if (arglistit != funclistit->arguments)
309 fprintf(out, ", ");
311 if (arglistit->next == NULL)
313 type = getargtype(arglistit);
314 assert(type != NULL);
315 fprintf(out, "(%s)(IPTR []){ AROS_PP_VARIADIC_CAST2IPTR(__VA_ARGS__) }", type);
316 free(type);
318 else
319 fprintf(out, "(arg%d)", count);
321 fprintf(out,
322 "); \\\n"
323 "})\n"
324 "#endif /* !NO_INLINE_STDARG */\n"
327 free(varargname);
329 else if (isvararg == 2)
331 int count;
332 struct functionarg *lastarg;
334 fprintf(out,
335 "\n#if !defined(NO_INLINE_STDARG) && !defined(%s_NO_INLINE_STDARG)\n"
336 "static inline %s __%s_WB(%s __%s",
337 cfg->modulenameupper,
338 funclistit->type, varargname, cfg->libbasetypeptrextern, cfg->libbase
340 for (arglistit = funclistit->arguments;
341 arglistit != NULL && arglistit->next != NULL;
342 arglistit = arglistit->next
345 fprintf(out, ", %s", arglistit->arg);
346 lastarg = arglistit;
348 fprintf(out, ", ...)\n");
350 fprintf(out,
351 "{\n"
352 " %s retval;\n"
353 " va_list args;\n"
354 "\n"
355 " va_start(args, %s);\n"
356 " retval = __%s_WB(__%s, ",
357 funclistit->type,
358 getargname(lastarg),
359 funclistit->name, cfg->libbase
361 for (arglistit = funclistit->arguments;
362 arglistit != NULL && arglistit->next != NULL;
363 arglistit = arglistit->next
366 fprintf(out, "%s, ", getargname(arglistit));
368 fprintf(out,
369 "args);\n"
370 " va_end(args);\n"
371 " return retval;\n"
372 "}\n"
373 "#define %s(",
374 varargname
376 for (arglistit = funclistit->arguments, count = 1;
377 arglistit != NULL && arglistit->next != NULL;
378 arglistit = arglistit->next, count++
381 fprintf(out, "arg%d, ", count);
383 fprintf(out,
384 "...) __%s_WB(%s, ",
385 varargname, cfg->libbase
387 for (arglistit = funclistit->arguments, count = 1;
388 arglistit != NULL && arglistit->next != NULL;
389 arglistit = arglistit->next, count++
392 fprintf(out, "(arg%d), ", count);
394 fprintf(out,
395 "__VA_ARGS__)\n"
396 "#endif /* !NO_INLINE_STDARG */\n"
399 free(varargname);
403 void
404 writedefinestack(FILE *out, struct functionhead *funclistit, struct config *cfg)
406 struct functionarg *arglistit;
408 /* Only if no peropener or pertask base */
409 if (cfg->options & OPTION_DUPBASE)
410 return;
412 fprintf(out, "#define %s ((%s (*)(", funclistit->name, funclistit->type);
413 for (arglistit = funclistit->arguments;
414 arglistit != NULL;
415 arglistit = arglistit->next
418 fprintf(out, "%s", arglistit->arg);
419 if (arglistit->next != NULL)
420 fprintf(out, ", ");
422 fprintf(out, "))__AROS_GETVECADDR(%s,%d))\n", cfg->libbase, funclistit->lvo);
425 void
426 writealiases(FILE *out, struct functionhead *funclistit, struct config *cfg)
428 struct stringlist *aliasesit;
430 for (aliasesit = funclistit->aliases;
431 aliasesit != NULL;
432 aliasesit = aliasesit->next
435 fprintf(out, "#define %s %s\n", aliasesit->s, funclistit->name);