genmodule: revert r52790
[AROS.git] / tools / genmodule / writeincdefines.c
blob2555df13bb34699d8f5c92a431174ba22f4359a8
1 /*
2 Copyright © 1995-2016, 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 "__BEGIN_DECLS\n"
45 "\n",
46 cfg->includenameupper, cfg->includenameupper, banner, cfg->modulename
48 freeBanner(banner);
50 for (funclistit = cfg->funclist; funclistit!=NULL; funclistit = funclistit->next)
52 if (!funclistit->priv && (funclistit->lvo >= cfg->firstlvo) && funclistit->libcall != STACK)
54 char isvararg = 0, *varargname = NULL, *lastname;
56 fprintf(out,
57 "\n"
58 "#if !defined(__%s_LIBAPI__) || (%d <= __%s_LIBAPI__)"
59 "\n",
60 cfg->includenameupper,
61 funclistit->version,
62 cfg->includenameupper
65 if ((!funclistit->novararg) && (funclistit->arguments))
67 struct functionarg *arglistit = funclistit->arguments;
69 while (arglistit->next != NULL) arglistit = arglistit->next;
71 lastname = getargname(arglistit);
72 assert(lastname != NULL);
74 if (*(funclistit->name + strlen(funclistit->name) - 1) == 'A')
76 isvararg = 1;
77 varargname = strdup(funclistit->name);
78 varargname[strlen(funclistit->name)-1] = '\0';
79 if (arglistit && strncmp(arglistit->arg, "RAWARG",6) == 0)
80 isvararg = 3;
82 else if (strcmp(funclistit->name + strlen(funclistit->name) - 7, "TagList") == 0)
84 isvararg = 1;
85 /* TagList has to be changed to Tags at the end of the functionname */
86 varargname = strdup(funclistit->name);
87 varargname[strlen(funclistit->name)-4] = 's';
88 varargname[strlen(funclistit->name)-3] = '\0';
90 else if (strcmp(funclistit->name + strlen(funclistit->name) - 4, "Args") == 0
91 && (strcasecmp(lastname, "args") == 0 || strcasecmp(lastname, "arglist") == 0)
94 isvararg = 1;
95 varargname = strdup(funclistit->name);
96 varargname[strlen(funclistit->name)-4] = '\0';
98 else if ((funclistit->name[0] == 'V') && (strncmp(arglistit->arg, "va_list", 7) == 0))
100 isvararg = 2;
101 varargname = malloc(strlen(funclistit->name));
102 strcpy(varargname, &funclistit->name[1]);
104 else if ((funclistit->name[0] == 'V') && (strncmp(arglistit->arg, "RAWARG", 6) == 0))
106 isvararg = 3;
107 varargname = malloc(strlen(funclistit->name));
108 strcpy(varargname, &funclistit->name[1]);
110 else
112 char *p;
114 if (strncmp(arglistit->arg, "const", 5) == 0) {
115 p = arglistit->arg + 5;
116 while (isspace(*p)) p++;
117 } else
118 p = arglistit->arg;
119 if (strncmp(p, "struct", 6)==0)
121 p += 6;
122 while (isspace(*p)) p++;
123 if (strncmp(p, "TagItem", 7) == 0)
125 p += 7;
126 while (isspace(*p)) p++;
128 if (*p == '*')
130 isvararg = 1;
131 varargname = malloc(strlen(funclistit->name) + 5);
132 strcpy(varargname, funclistit->name);
133 strcat(varargname, "Tags");
140 writedefineregister(out, funclistit, cfg, isvararg);
141 if (!funclistit->novararg && isvararg)
143 writedefinevararg(out, funclistit, cfg, isvararg, varargname);
144 free(varargname);
146 writealiases(out, funclistit, cfg);
148 fprintf(out,
149 "\n"
150 "#endif /* !defined(__%s_LIBAPI__) || (%d <= __%s_LIBAPI__) */"
151 "\n",
152 cfg->includenameupper,
153 funclistit->version,
154 cfg->includenameupper
159 fprintf(out,
160 "\n"
161 "__END_DECLS\n"
162 "\n"
163 "#endif /* DEFINES_%s_H*/\n",
164 cfg->includenameupper
166 fclose(out);
169 void
170 writedefineregister(FILE *out, struct functionhead *funclistit, struct config *cfg, char isvararg)
172 struct functionarg *arglistit;
173 int count, isvoid, nquad = 0, narg = 0;
174 char *type;
176 isvoid = strcmp(funclistit->type, "void") == 0
177 || strcmp(funclistit->type, "VOID") == 0;
179 fprintf(out,
180 "\n"
181 "#define __%s_WB(__%s",
182 funclistit->name, cfg->libbase
184 for (arglistit = funclistit->arguments, count = 1;
185 arglistit!=NULL;
186 arglistit = arglistit->next, count++
189 fprintf(out, ", __arg%d", count);
190 if (strchr(arglistit->reg, '/') != NULL) {
191 nquad++;
192 } else {
193 narg++;
196 fprintf(out,
197 ") ({\\\n"
199 fprintf(out,
200 " AROS_LIBREQ(%s,%d)\\\n",
201 cfg->libbase, funclistit->version
203 if (nquad == 0)
205 fprintf(out,
206 " AROS_LC%d%s%s(%s, %s, \\\n",
207 funclistit->argcount, funclistit->unusedlibbase ? "I" : "",
208 (isvoid) ? "NR" : "",
209 funclistit->type, funclistit->name
212 for (arglistit = funclistit->arguments, count = 1;
213 arglistit!=NULL;
214 arglistit = arglistit->next, count++
217 type = getargtype(arglistit);
218 assert(type != NULL);
219 fprintf(out,
220 " AROS_LCA(%s%s,(__arg%d),%s), \\\n",
221 ((isvararg) && (!arglistit->next)) ? "const " : "",
222 type, count, arglistit->reg
224 free(type);
227 else
229 if (narg) {
230 fprintf(out,
231 " AROS_LC%dQUAD%d%s(%s, %s,\\\n",
232 narg, nquad, (isvoid) ? "NR" : "",
233 funclistit->type, funclistit->name
235 } else {
236 fprintf(out,
237 " AROS_LCQUAD%d%s(%s, %s,\\\n",
238 nquad, (isvoid) ? "NR" : "",
239 funclistit->type, funclistit->name
243 for (arglistit = funclistit->arguments, count = 1;
244 arglistit != NULL;
245 arglistit = arglistit->next, count++
248 char *quad2 = strchr(arglistit->reg, '/');
250 arglistit->reg[2] = 0;
251 type = getargtype(arglistit);
252 assert(type != NULL);
254 if (quad2 != NULL) {
255 *quad2 = 0;
256 fprintf(out,
257 " AROS_LCAQUAD(%s%s, (__arg%d), %s, %s), \\\n",
258 ((isvararg) && (!arglistit->next)) ? "const " : "",
259 type, count, arglistit->reg, quad2+1
261 *quad2 = '/';
262 } else {
263 fprintf(out,
264 " AROS_LCA(%s%s, (__arg%d), %s), \\\n",
265 ((isvararg) && (!arglistit->next)) ? "const " : "",
266 type, count, arglistit->reg
269 free(type);
272 fprintf(out,
273 " %s, (__%s), %u, %s);\\\n})\n\n",
274 cfg->libbasetypeptrextern, cfg->libbase, funclistit->lvo, cfg->basename
277 fprintf(out, "#define %s(", funclistit->name);
278 for (arglistit = funclistit->arguments, count = 1;
279 arglistit != NULL;
280 arglistit = arglistit->next, count++
283 if (arglistit != funclistit->arguments)
284 fprintf(out, ", ");
285 fprintf(out, "arg%d", count);
287 fprintf(out, ") \\\n __%s_WB(__aros_getbase_%s()",
288 funclistit->name, cfg->libbase
290 for (arglistit = funclistit->arguments, count = 1;
291 arglistit != NULL;
292 arglistit = arglistit->next, count++
294 fprintf(out, ", (arg%d)", count);
295 fprintf(out, ")\n");
298 void
299 writedefinevararg(FILE *out, struct functionhead *funclistit, struct config *cfg, char isvararg, char *varargname)
301 struct functionarg *arglistit = funclistit->arguments;
302 int isvoid;
304 isvoid = strcmp(funclistit->type, "void") == 0
305 || strcmp(funclistit->type, "VOID") == 0;
307 if (isvararg == 1)
309 int count;
310 char *type;
312 fprintf(out,
313 "\n#if !defined(NO_INLINE_STDARG) && !defined(%s_NO_INLINE_STDARG)\n"
314 "#define %s(",
315 cfg->includenameupper, varargname
317 for (arglistit = funclistit->arguments, count = 1;
318 arglistit != NULL && arglistit->next != NULL;
319 arglistit = arglistit->next, count++
322 fprintf(out, "arg%d, ", count);
324 fprintf(out,
325 "...) \\\n"
326 "({ \\\n"
327 " %s(",
328 funclistit->name
330 for (arglistit = funclistit->arguments, count = 1;
331 arglistit != NULL;
332 arglistit = arglistit->next, count++
335 if (arglistit != funclistit->arguments)
336 fprintf(out, ", ");
338 if (arglistit->next == NULL)
340 type = getargtype(arglistit);
341 assert(type != NULL);
342 fprintf(out, "(const %s)(const IPTR []){ AROS_PP_VARIADIC_CAST2IPTR(__VA_ARGS__) }", type);
343 free(type);
345 else
346 fprintf(out, "(arg%d)", count);
348 fprintf(out,
349 "); \\\n"
350 "})\n"
351 "#endif /* !NO_INLINE_STDARG */\n"
354 else if (isvararg == 2)
356 int count;
357 struct functionarg *lastarg;
359 fprintf(out,
360 "\n#if !defined(NO_INLINE_STDARG) && !defined(%s_NO_INLINE_STDARG)\n"
361 "static inline %s __%s_WB(%s __%s",
362 cfg->includenameupper,
363 funclistit->type, varargname, cfg->libbasetypeptrextern, cfg->libbase
365 for (arglistit = funclistit->arguments;
366 arglistit != NULL && arglistit->next != NULL;
367 arglistit = arglistit->next
370 fprintf(out, ", %s", arglistit->arg);
371 lastarg = arglistit;
373 fprintf(out, ", ...)\n");
375 fprintf(out,
376 "{\n"
377 " %s retval;\n"
378 " va_list args;\n"
379 "\n"
380 " va_start(args, %s);\n"
381 " retval = __%s_WB(__%s, ",
382 funclistit->type,
383 getargname(lastarg),
384 funclistit->name, cfg->libbase
386 for (arglistit = funclistit->arguments;
387 arglistit != NULL && arglistit->next != NULL;
388 arglistit = arglistit->next
391 fprintf(out, "%s, ", getargname(arglistit));
393 fprintf(out,
394 "args);\n"
395 " va_end(args);\n"
396 " return retval;\n"
397 "}\n"
398 "#define %s(",
399 varargname
401 for (arglistit = funclistit->arguments, count = 1;
402 arglistit != NULL && arglistit->next != NULL && arglistit->next->next != NULL;
403 arglistit = arglistit->next, count++
406 fprintf(out, "arg%d, ", count);
408 fprintf(out,
409 "...) __%s_WB(%s, ",
410 varargname, cfg->libbase
412 for (arglistit = funclistit->arguments, count = 1;
413 arglistit != NULL && arglistit->next != NULL && arglistit->next->next != NULL;
414 arglistit = arglistit->next, count++
417 fprintf(out, "(arg%d), ", count);
419 fprintf(out,
420 "__VA_ARGS__)\n"
421 "#endif /* !NO_INLINE_STDARG */\n"
424 else if (isvararg == 3)
426 int count;
428 fprintf(out,
429 "\n#if !defined(NO_INLINE_STDARG) && !defined(%s_NO_INLINE_STDARG)\n"
430 "static inline %s __inline_%s_%s(%s __%s",
431 cfg->includenameupper,
432 funclistit->type, cfg->basename, varargname, cfg->libbasetypeptrextern, cfg->libbase
434 for (arglistit = funclistit->arguments, count = 0;
435 arglistit != NULL && arglistit->next != NULL;
436 arglistit = arglistit->next
439 char *type = getargtype(arglistit);
441 fprintf(out, ", %s __arg%d", type, ++count);
443 fprintf(out, ", ...)\n");
445 fprintf(out,"{\n");
446 if (!isvoid)
447 fprintf(out, " %s retval;\n\n", funclistit->type);
449 fprintf(out,
450 " AROS_SLOWSTACKFORMAT_PRE(__arg%d);\n"
451 " %s__%s_WB(__%s",
452 count,
453 isvoid ? "" : "retval = ",
454 funclistit->name,
455 cfg->libbase
457 for (arglistit = funclistit->arguments, count = 1;
458 arglistit != NULL && arglistit->next != NULL;
459 arglistit = arglistit->next, count++
462 fprintf(out, ", __arg%d", count);
464 count--;
465 fprintf(out,
466 ", AROS_SLOWSTACKFORMAT_ARG(__arg%d));\n"
467 " AROS_SLOWSTACKFORMAT_POST(__arg%d);\n"
468 " return%s;\n"
469 "}\n"
470 "\n"
471 "#define %s(",
472 count,
473 count,
474 isvoid ? "" : " retval",
475 varargname
477 for (arglistit = funclistit->arguments, count = 1;
478 arglistit != NULL && arglistit->next != NULL && arglistit->next->next != NULL;
479 arglistit = arglistit->next, count++
482 fprintf(out, "arg%d, ", count);
484 fprintf(out,
485 "...) \\\n"
486 " __inline_%s_%s((%s)(%s), ",
487 cfg->basename, varargname,
488 cfg->libbasetypeptrextern,
489 cfg->libbase
491 for (arglistit = funclistit->arguments, count = 1;
492 arglistit != NULL && arglistit->next != NULL && arglistit->next->next != NULL;
493 arglistit = arglistit->next, count++
496 fprintf(out, "(arg%d), ", count);
498 fprintf(out,
499 "__VA_ARGS__)\n"
500 "#endif /* !NO_INLINE_STDARG */\n"
505 void
506 writealiases(FILE *out, struct functionhead *funclistit, struct config *cfg)
508 struct stringlist *aliasesit;
510 for (aliasesit = funclistit->aliases;
511 aliasesit != NULL;
512 aliasesit = aliasesit->next
515 fprintf(out, "#define %s %s\n", aliasesit->s, funclistit->name);