revert between 56095 -> 55830 in arch
[AROS.git] / tools / genmodule / writeincdefines.c
blob3368934e1afeabbce5cbdb73eb29646195712780
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"
352 for (arglistit = funclistit->arguments, count = 1;
353 arglistit != NULL;
354 arglistit = arglistit->next, count++
357 if (arglistit->next == NULL)
359 fprintf(out, " const IPTR %s_args[] = { AROS_PP_VARIADIC_CAST2IPTR(__VA_ARGS__) };\\\n", funclistit->name);
362 fprintf(out,
363 " %s(",
364 funclistit->name
366 for (arglistit = funclistit->arguments, count = 1;
367 arglistit != NULL;
368 arglistit = arglistit->next, count++
371 if (arglistit != funclistit->arguments)
372 fprintf(out, ", ");
374 if (arglistit->next == NULL)
376 type = getargtype(arglistit);
377 assert(type != NULL);
378 fprintf(out, "(%s)(%s_args)", type, funclistit->name);
379 free(type);
381 else
382 fprintf(out, "(arg%d)", count);
384 fprintf(out,
385 "); \\\n"
386 "})\n"
387 "#endif /* !NO_INLINE_STDARG */\n"
390 else if (isvararg == 2)
392 int count;
393 struct functionarg *lastarg;
395 fprintf(out,
396 "\n#if !defined(NO_INLINE_STDARG) && !defined(%s_NO_INLINE_STDARG)\n"
397 "static inline %s __%s_WB(%s __%s",
398 cfg->includenameupper,
399 funclistit->type, varargname, cfg->libbasetypeptrextern, cfg->libbase
401 for (arglistit = funclistit->arguments;
402 arglistit != NULL && arglistit->next != NULL;
403 arglistit = arglistit->next
406 fprintf(out, ", %s", arglistit->arg);
407 lastarg = arglistit;
409 fprintf(out, ", ...)\n");
411 fprintf(out,
412 "{\n"
413 " %s retval;\n"
414 " va_list args;\n"
415 "\n"
416 " va_start(args, %s);\n"
417 " retval = __%s_WB(__%s, ",
418 funclistit->type,
419 getargname(lastarg),
420 funclistit->name, cfg->libbase
422 for (arglistit = funclistit->arguments;
423 arglistit != NULL && arglistit->next != NULL;
424 arglistit = arglistit->next
427 fprintf(out, "%s, ", getargname(arglistit));
429 fprintf(out,
430 "args);\n"
431 " va_end(args);\n"
432 " return retval;\n"
433 "}\n"
434 "#define %s(",
435 varargname
437 for (arglistit = funclistit->arguments, count = 1;
438 arglistit != NULL && arglistit->next != NULL && arglistit->next->next != NULL;
439 arglistit = arglistit->next, count++
442 fprintf(out, "arg%d, ", count);
444 fprintf(out,
445 "...) __%s_WB(",
446 varargname
448 fprintf(out, "(%s)__%s_LIBBASE, ",
449 cfg->libbasetypeptrextern,
450 cfg->includenameupper);
451 for (arglistit = funclistit->arguments, count = 1;
452 arglistit != NULL && arglistit->next != NULL && arglistit->next->next != NULL;
453 arglistit = arglistit->next, count++
456 fprintf(out, "(arg%d), ", count);
458 fprintf(out,
459 "__VA_ARGS__)\n"
460 "#endif /* !NO_INLINE_STDARG */\n"
463 else if (isvararg == 3)
465 int count;
467 fprintf(out,
468 "\n#if !defined(NO_INLINE_STDARG) && !defined(%s_NO_INLINE_STDARG)\n"
469 "static inline %s __inline_%s_%s(%s __%s",
470 cfg->includenameupper,
471 funclistit->type, cfg->basename, varargname, cfg->libbasetypeptrextern, cfg->libbase
473 for (arglistit = funclistit->arguments, count = 0;
474 arglistit != NULL && arglistit->next != NULL;
475 arglistit = arglistit->next
478 char *type = getargtype(arglistit);
480 fprintf(out, ", %s __arg%d", type, ++count);
482 fprintf(out, ", ...)\n");
484 fprintf(out,"{\n");
485 if (!isvoid)
486 fprintf(out, " %s retval;\n\n", funclistit->type);
488 fprintf(out,
489 " AROS_SLOWSTACKFORMAT_PRE(__arg%d);\n"
490 " %s__%s_WB(__%s",
491 count,
492 isvoid ? "" : "retval = ",
493 funclistit->name,
494 cfg->libbase
496 for (arglistit = funclistit->arguments, count = 1;
497 arglistit != NULL && arglistit->next != NULL;
498 arglistit = arglistit->next, count++
501 fprintf(out, ", __arg%d", count);
503 count--;
504 fprintf(out,
505 ", AROS_SLOWSTACKFORMAT_ARG(__arg%d));\n"
506 " AROS_SLOWSTACKFORMAT_POST(__arg%d);\n"
507 " return%s;\n"
508 "}\n"
509 "\n"
510 "#define %s(",
511 count,
512 count,
513 isvoid ? "" : " retval",
514 varargname
516 for (arglistit = funclistit->arguments, count = 1;
517 arglistit != NULL && arglistit->next != NULL && arglistit->next->next != NULL;
518 arglistit = arglistit->next, count++
521 fprintf(out, "arg%d, ", count);
523 fprintf(out,
524 "...) \\\n"
525 " __inline_%s_%s(",
526 cfg->basename, varargname
528 fprintf(out, "(%s)__%s_LIBBASE, ",
529 cfg->libbasetypeptrextern,
530 cfg->includenameupper);
531 for (arglistit = funclistit->arguments, count = 1;
532 arglistit != NULL && arglistit->next != NULL && arglistit->next->next != NULL;
533 arglistit = arglistit->next, count++
536 fprintf(out, "(arg%d), ", count);
538 fprintf(out,
539 "__VA_ARGS__)\n"
540 "#endif /* !NO_INLINE_STDARG */\n"
545 void
546 writealiases(FILE *out, struct functionhead *funclistit, struct config *cfg)
548 struct stringlist *aliasesit;
550 for (aliasesit = funclistit->aliases;
551 aliasesit != NULL;
552 aliasesit = aliasesit->next
555 fprintf(out, "#define %s %s\n", aliasesit->s, funclistit->name);