refactored some code. compiles now without suppresing any warning with gcc-6.3.0.
[AROS.git] / tools / genmodule / writeincdefines.c
blobf59b683fa2efbb346400e40e978f9a3f09b1e345
1 /*
2 Copyright © 1995-2017, 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 *, char);
10 static void writedefinevararg(FILE *, struct functionhead *, struct config *, char, char *);
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->includename);
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 cfg->includenameupper, cfg->includenameupper, banner, cfg->modulename
46 if ((cfg->options & OPTION_RELLINKLIB) || (cfg->options & OPTION_DUPBASE))
48 fprintf(out,
49 "#if !defined(__%s_LIBBASE)\n"
50 "# if !defined(__NOLIBBASE__) && !defined(__%s_NOLIBBASE__)\n"
51 "# define __%s_LIBBASE __aros_getbase_%s()\n"
52 "# else\n"
53 "# define __%s_LIBBASE %s\n"
54 "# endif\n"
55 "#endif\n"
56 "\n",
57 cfg->includenameupper, cfg->includenameupper,
58 cfg->includenameupper, cfg->libbase,
59 cfg->includenameupper, cfg->libbase
62 else
63 fprintf(out,
64 "#if !defined(__%s_LIBBASE)\n"
65 "# define __%s_LIBBASE %s\n"
66 "#endif\n"
67 "\n",
68 cfg->includenameupper,
69 cfg->includenameupper, cfg->libbase
71 fprintf(out,
72 "__BEGIN_DECLS\n"
73 "\n"
75 freeBanner(banner);
77 for (funclistit = cfg->funclist; funclistit!=NULL; funclistit = funclistit->next)
79 if (!funclistit->priv && (funclistit->lvo >= cfg->firstlvo) && funclistit->libcall != STACK)
81 char isvararg = 0, *varargname = NULL, *lastname;
83 fprintf(out,
84 "\n"
85 "#if !defined(__%s_LIBAPI__) || (%d <= __%s_LIBAPI__)"
86 "\n",
87 cfg->includenameupper,
88 funclistit->version,
89 cfg->includenameupper
92 if ((!funclistit->novararg) && (funclistit->arguments))
94 struct functionarg *arglistit = funclistit->arguments;
96 while (arglistit->next != NULL) arglistit = arglistit->next;
98 lastname = getargname(arglistit);
99 assert(lastname != NULL);
101 if (*(funclistit->name + strlen(funclistit->name) - 1) == 'A')
103 isvararg = 1;
104 varargname = strdup(funclistit->name);
105 varargname[strlen(funclistit->name)-1] = '\0';
106 if (arglistit && strncmp(arglistit->arg, "RAWARG",6) == 0)
107 isvararg = 3;
109 else if (strcmp(funclistit->name + strlen(funclistit->name) - 7, "TagList") == 0)
111 isvararg = 1;
112 /* TagList has to be changed to Tags at the end of the functionname */
113 varargname = strdup(funclistit->name);
114 varargname[strlen(funclistit->name)-4] = 's';
115 varargname[strlen(funclistit->name)-3] = '\0';
117 else if (strcmp(funclistit->name + strlen(funclistit->name) - 4, "Args") == 0
118 && (strcasecmp(lastname, "args") == 0 || strcasecmp(lastname, "arglist") == 0)
121 isvararg = 1;
122 varargname = strdup(funclistit->name);
123 varargname[strlen(funclistit->name)-4] = '\0';
125 else if ((funclistit->name[0] == 'V') && (strncmp(arglistit->arg, "va_list", 7) == 0))
127 isvararg = 2;
128 varargname = malloc(strlen(funclistit->name));
129 strcpy(varargname, &funclistit->name[1]);
131 else if ((funclistit->name[0] == 'V') && (strncmp(arglistit->arg, "RAWARG", 6) == 0))
133 isvararg = 3;
134 varargname = malloc(strlen(funclistit->name));
135 strcpy(varargname, &funclistit->name[1]);
137 else
139 char *p;
141 if (strncmp(arglistit->arg, "const", 5) == 0) {
142 p = arglistit->arg + 5;
143 while (isspace(*p)) p++;
144 } else
145 p = arglistit->arg;
146 if (strncmp(p, "struct", 6)==0)
148 p += 6;
149 while (isspace(*p)) p++;
150 if (strncmp(p, "TagItem", 7) == 0)
152 p += 7;
153 while (isspace(*p)) p++;
155 if (*p == '*')
157 isvararg = 1;
158 varargname = malloc(strlen(funclistit->name) + 5);
159 strcpy(varargname, funclistit->name);
160 strcat(varargname, "Tags");
167 writedefineregister(out, funclistit, cfg, isvararg);
168 if (!funclistit->novararg && isvararg)
170 writedefinevararg(out, funclistit, cfg, isvararg, varargname);
171 free(varargname);
173 writealiases(out, funclistit, cfg);
175 fprintf(out,
176 "\n"
177 "#endif /* !defined(__%s_LIBAPI__) || (%d <= __%s_LIBAPI__) */"
178 "\n",
179 cfg->includenameupper,
180 funclistit->version,
181 cfg->includenameupper
186 fprintf(out,
187 "\n"
188 "__END_DECLS\n"
189 "\n"
190 "#endif /* DEFINES_%s_H*/\n",
191 cfg->includenameupper
193 fclose(out);
196 void
197 writedefineregister(FILE *out, struct functionhead *funclistit, struct config *cfg, char isvararg)
199 struct functionarg *arglistit;
200 int count, isvoid, nquad = 0, narg = 0;
201 char *type;
203 isvoid = strcmp(funclistit->type, "void") == 0
204 || strcmp(funclistit->type, "VOID") == 0;
206 fprintf(out,
207 "\n"
208 "#define __%s_WB(__%s",
209 funclistit->name, cfg->libbase
211 for (arglistit = funclistit->arguments, count = 1;
212 arglistit!=NULL;
213 arglistit = arglistit->next, count++
216 fprintf(out, ", __arg%d", count);
217 if (strchr(arglistit->reg, '/') != NULL) {
218 nquad++;
219 } else {
220 narg++;
223 fprintf(out,
224 ") ({\\\n"
226 fprintf(out,
227 " AROS_LIBREQ(%s,%d)\\\n",
228 cfg->libbase, funclistit->version
230 if (nquad == 0)
232 fprintf(out,
233 " AROS_LC%d%s%s(%s, %s, \\\n",
234 funclistit->argcount, funclistit->unusedlibbase ? "I" : "",
235 (isvoid) ? "NR" : "",
236 funclistit->type, funclistit->name
239 for (arglistit = funclistit->arguments, count = 1;
240 arglistit!=NULL;
241 arglistit = arglistit->next, count++
244 type = getargtype(arglistit);
245 assert(type != NULL);
246 fprintf(out,
247 " AROS_LCA(%s,(__arg%d),%s), \\\n",
248 type, count, arglistit->reg
250 free(type);
253 else
255 if (narg) {
256 fprintf(out,
257 " AROS_LC%dQUAD%d%s(%s, %s,\\\n",
258 narg, nquad, (isvoid) ? "NR" : "",
259 funclistit->type, funclistit->name
261 } else {
262 fprintf(out,
263 " AROS_LCQUAD%d%s(%s, %s,\\\n",
264 nquad, (isvoid) ? "NR" : "",
265 funclistit->type, funclistit->name
269 for (arglistit = funclistit->arguments, count = 1;
270 arglistit != NULL;
271 arglistit = arglistit->next, count++
274 char *quad2 = strchr(arglistit->reg, '/');
276 arglistit->reg[2] = 0;
277 type = getargtype(arglistit);
278 assert(type != NULL);
280 if (quad2 != NULL) {
281 *quad2 = 0;
282 fprintf(out,
283 " AROS_LCAQUAD(%s, (__arg%d), %s, %s), \\\n",
284 type, count, arglistit->reg, quad2+1
286 *quad2 = '/';
287 } else {
288 fprintf(out,
289 " AROS_LCA(%s, (__arg%d), %s), \\\n",
290 type, count, arglistit->reg
293 free(type);
296 fprintf(out,
297 " %s, (__%s), %u, %s);\\\n})\n\n",
298 cfg->libbasetypeptrextern, cfg->libbase, funclistit->lvo, cfg->basename
301 fprintf(out, "#define %s(", funclistit->name);
302 for (arglistit = funclistit->arguments, count = 1;
303 arglistit != NULL;
304 arglistit = arglistit->next, count++
307 if (arglistit != funclistit->arguments)
308 fprintf(out, ", ");
309 fprintf(out, "arg%d", count);
311 fprintf(out, ") \\\n __%s_WB(__%s_LIBBASE",
312 funclistit->name, cfg->includenameupper
314 for (arglistit = funclistit->arguments, count = 1;
315 arglistit != NULL;
316 arglistit = arglistit->next, count++
318 fprintf(out, ", (arg%d)", count);
319 fprintf(out, ")\n");
322 void
323 writedefinevararg(FILE *out, struct functionhead *funclistit, struct config *cfg, char isvararg, char *varargname)
325 struct functionarg *arglistit = funclistit->arguments;
326 int isvoid;
328 isvoid = strcmp(funclistit->type, "void") == 0
329 || strcmp(funclistit->type, "VOID") == 0;
331 if (isvararg == 1)
333 int count;
334 char *type;
336 fprintf(out,
337 "\n#if !defined(NO_INLINE_STDARG) && !defined(%s_NO_INLINE_STDARG)\n"
338 "#define %s(",
339 cfg->includenameupper, varargname
341 for (arglistit = funclistit->arguments, count = 1;
342 arglistit != NULL && arglistit->next != NULL;
343 arglistit = arglistit->next, count++
346 fprintf(out, "arg%d, ", count);
348 fprintf(out,
349 "...) \\\n"
350 "({ \\\n"
351 " %s(",
352 funclistit->name
354 for (arglistit = funclistit->arguments, count = 1;
355 arglistit != NULL;
356 arglistit = arglistit->next, count++
359 if (arglistit != funclistit->arguments)
360 fprintf(out, ", ");
362 if (arglistit->next == NULL)
364 type = getargtype(arglistit);
365 assert(type != NULL);
366 fprintf(out, "(%s)(const IPTR []){ AROS_PP_VARIADIC_CAST2IPTR(__VA_ARGS__) }", type);
367 free(type);
369 else
370 fprintf(out, "(arg%d)", count);
372 fprintf(out,
373 "); \\\n"
374 "})\n"
375 "#endif /* !NO_INLINE_STDARG */\n"
378 else if (isvararg == 2)
380 int count;
381 struct functionarg *lastarg;
383 fprintf(out,
384 "\n#if !defined(NO_INLINE_STDARG) && !defined(%s_NO_INLINE_STDARG)\n"
385 "static inline %s __%s_WB(%s __%s",
386 cfg->includenameupper,
387 funclistit->type, varargname, cfg->libbasetypeptrextern, cfg->libbase
389 for (arglistit = funclistit->arguments;
390 arglistit != NULL && arglistit->next != NULL;
391 arglistit = arglistit->next
394 fprintf(out, ", %s", arglistit->arg);
395 lastarg = arglistit;
397 fprintf(out, ", ...)\n");
399 fprintf(out,
400 "{\n"
401 " %s retval;\n"
402 " va_list args;\n"
403 "\n"
404 " va_start(args, %s);\n"
405 " retval = __%s_WB(__%s, ",
406 funclistit->type,
407 getargname(lastarg),
408 funclistit->name, cfg->libbase
410 for (arglistit = funclistit->arguments;
411 arglistit != NULL && arglistit->next != NULL;
412 arglistit = arglistit->next
415 fprintf(out, "%s, ", getargname(arglistit));
417 fprintf(out,
418 "args);\n"
419 " va_end(args);\n"
420 " return retval;\n"
421 "}\n"
422 "#define %s(",
423 varargname
425 for (arglistit = funclistit->arguments, count = 1;
426 arglistit != NULL && arglistit->next != NULL && arglistit->next->next != NULL;
427 arglistit = arglistit->next, count++
430 fprintf(out, "arg%d, ", count);
432 fprintf(out,
433 "...) __%s_WB(",
434 varargname
436 fprintf(out, "(%s)__%s_LIBBASE, ",
437 cfg->libbasetypeptrextern,
438 cfg->includenameupper);
439 for (arglistit = funclistit->arguments, count = 1;
440 arglistit != NULL && arglistit->next != NULL && arglistit->next->next != NULL;
441 arglistit = arglistit->next, count++
444 fprintf(out, "(arg%d), ", count);
446 fprintf(out,
447 "__VA_ARGS__)\n"
448 "#endif /* !NO_INLINE_STDARG */\n"
451 else if (isvararg == 3)
453 int count;
455 fprintf(out,
456 "\n#if !defined(NO_INLINE_STDARG) && !defined(%s_NO_INLINE_STDARG)\n"
457 "static inline %s __inline_%s_%s(%s __%s",
458 cfg->includenameupper,
459 funclistit->type, cfg->basename, varargname, cfg->libbasetypeptrextern, cfg->libbase
461 for (arglistit = funclistit->arguments, count = 0;
462 arglistit != NULL && arglistit->next != NULL;
463 arglistit = arglistit->next
466 char *type = getargtype(arglistit);
468 fprintf(out, ", %s __arg%d", type, ++count);
470 fprintf(out, ", ...)\n");
472 fprintf(out,"{\n");
473 if (!isvoid)
474 fprintf(out, " %s retval;\n\n", funclistit->type);
476 fprintf(out,
477 " AROS_SLOWSTACKFORMAT_PRE(__arg%d);\n"
478 " %s__%s_WB(__%s",
479 count,
480 isvoid ? "" : "retval = ",
481 funclistit->name,
482 cfg->libbase
484 for (arglistit = funclistit->arguments, count = 1;
485 arglistit != NULL && arglistit->next != NULL;
486 arglistit = arglistit->next, count++
489 fprintf(out, ", __arg%d", count);
491 count--;
492 fprintf(out,
493 ", AROS_SLOWSTACKFORMAT_ARG(__arg%d));\n"
494 " AROS_SLOWSTACKFORMAT_POST(__arg%d);\n"
495 " return%s;\n"
496 "}\n"
497 "\n"
498 "#define %s(",
499 count,
500 count,
501 isvoid ? "" : " retval",
502 varargname
504 for (arglistit = funclistit->arguments, count = 1;
505 arglistit != NULL && arglistit->next != NULL && arglistit->next->next != NULL;
506 arglistit = arglistit->next, count++
509 fprintf(out, "arg%d, ", count);
511 fprintf(out,
512 "...) \\\n"
513 " __inline_%s_%s(",
514 cfg->basename, varargname
516 fprintf(out, "(%s)__%s_LIBBASE, ",
517 cfg->libbasetypeptrextern,
518 cfg->includenameupper);
519 for (arglistit = funclistit->arguments, count = 1;
520 arglistit != NULL && arglistit->next != NULL && arglistit->next->next != NULL;
521 arglistit = arglistit->next, count++
524 fprintf(out, "(arg%d), ", count);
526 fprintf(out,
527 "__VA_ARGS__)\n"
528 "#endif /* !NO_INLINE_STDARG */\n"
533 void
534 writealiases(FILE *out, struct functionhead *funclistit, struct config *cfg)
536 struct stringlist *aliasesit;
538 for (aliasesit = funclistit->aliases;
539 aliasesit != NULL;
540 aliasesit = aliasesit->next
543 fprintf(out, "#define %s %s\n", aliasesit->s, funclistit->name);