refactored some code. compiles now without suppresing any warning with gcc-6.3.0.
[AROS.git] / tools / genmodule / writeincinline.c
blobb3c4bdcb4f2a6f826601802e629b545a762320cd
1 /*
2 Copyright © 1995-2017, The AROS Development Team. All rights reserved.
3 $Id$
5 Function to write inline/modulename.h. Part of genmodule.
6 */
7 #include "genmodule.h"
9 static void writeinlineregister(FILE *, struct functionhead *, struct config *, char);
10 static void writeinlinevararg(FILE *, struct functionhead *, struct config *, char, char *);
11 static void writealiases(FILE *, struct functionhead *, struct config *);
13 void writeincinline(struct config *cfg)
15 FILE *out;
16 char line[256], *banner;
17 struct functionhead *funclistit;
19 snprintf(line, 255, "%s/inline/%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 INLINE_%s_H\n"
31 "#define INLINE_%s_H\n"
32 "\n"
33 "%s"
34 "\n"
35 "/*\n"
36 " Desc: Inline function(s) 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
70 );
71 freeBanner(banner);
73 for (funclistit = cfg->funclist; funclistit!=NULL; funclistit = funclistit->next)
75 if (!funclistit->priv && (funclistit->lvo >= cfg->firstlvo) && funclistit->libcall != STACK)
77 char isvararg = 0, *varargname = NULL, *lastname;
79 fprintf(out,
80 "\n"
81 "#if !defined(__%s_LIBAPI__) || (%d <= __%s_LIBAPI__)"
82 "\n",
83 cfg->includenameupper,
84 funclistit->version,
85 cfg->includenameupper
88 if ((!funclistit->novararg) && (funclistit->arguments))
90 struct functionarg *arglistit = funclistit->arguments;
92 while (arglistit->next != NULL) arglistit = arglistit->next;
94 lastname = getargname(arglistit);
95 assert(lastname != NULL);
97 if (*(funclistit->name + strlen(funclistit->name) - 1) == 'A')
99 isvararg = 1;
100 varargname = strdup(funclistit->name);
101 varargname[strlen(funclistit->name)-1] = '\0';
102 if (arglistit && strncmp(arglistit->arg, "RAWARG", 6) == 0)
103 isvararg = 3;
105 else if (strcmp(funclistit->name + strlen(funclistit->name) - 7, "TagList") == 0)
107 isvararg = 1;
108 /* TagList has to be changed to Tags at the end of the functionname */
109 varargname = strdup(funclistit->name);
110 varargname[strlen(funclistit->name)-4] = 's';
111 varargname[strlen(funclistit->name)-3] = '\0';
113 else if (strcmp(funclistit->name + strlen(funclistit->name) - 4, "Args") == 0
114 && (strcasecmp(lastname, "args") == 0 || strcasecmp(lastname, "arglist") == 0)
117 isvararg = 1;
118 varargname = strdup(funclistit->name);
119 varargname[strlen(funclistit->name)-4] = '\0';
121 else if ((funclistit->name[0] == 'V') && (strncmp(arglistit->arg, "va_list", 7) == 0))
123 isvararg = 2;
124 varargname = malloc(strlen(funclistit->name));
125 strcpy(varargname, &funclistit->name[1]);
127 else if ((funclistit->name[0] == 'V') && (strncmp(arglistit->arg, "RAWARG", 6) == 0))
129 isvararg = 3;
130 varargname = malloc(strlen(funclistit->name));
131 strcpy(varargname, &funclistit->name[1]);
133 else
135 char *p;
138 if (strncmp(arglistit->arg, "const", 5) == 0) {
139 p = arglistit->arg + 5;
140 while (isspace(*p)) p++;
141 } else
142 p = arglistit->arg;
143 if (strncmp(p, "struct", 6)==0)
145 p += 6;
146 while (isspace(*p)) p++;
147 if (strncmp(p, "TagItem", 7) == 0)
149 p += 7;
150 while (isspace(*p)) p++;
152 if (*p == '*')
154 isvararg = 1;
155 varargname = malloc(strlen(funclistit->name) + 5);
156 strcpy(varargname, funclistit->name);
157 strcat(varargname, "Tags");
164 writeinlineregister(out, funclistit, cfg, isvararg);
165 if (!funclistit->novararg && isvararg)
167 writeinlinevararg(out, funclistit, cfg, isvararg, varargname);
168 free(varargname);
171 writealiases(out, funclistit, cfg);
173 fprintf(out,
174 "\n"
175 "#endif /* !defined(__%s_LIBAPI__) || (%d <= __%s_LIBAPI__) */"
176 "\n",
177 cfg->includenameupper,
178 funclistit->version,
179 cfg->includenameupper
184 fprintf(out,
185 "\n"
186 "#endif /* INLINE_%s_H*/\n",
187 cfg->includenameupper
189 fclose(out);
192 void
193 writeinlineregister(FILE *out, struct functionhead *funclistit, struct config *cfg, char isvararg)
195 struct functionarg *arglistit;
196 int count, isvoid;
197 int narg=0,nquad=0;
198 char *type;
200 isvoid = strcmp(funclistit->type, "void") == 0
201 || strcmp(funclistit->type, "VOID") == 0;
203 fprintf(out,
204 "\n"
205 "static inline %s __inline_%s_%s(",
206 funclistit->type, cfg->basename, funclistit->name
208 for (arglistit = funclistit->arguments, count = 1;
209 arglistit != NULL;
210 arglistit = arglistit->next, count++
213 type = getargtype(arglistit);
214 fprintf(out, "%s __arg%d, ",
215 type, count);
216 if (strchr(arglistit->reg, '/') != NULL) {
217 nquad++;
218 } else {
219 narg++;
222 fprintf(out,
223 "APTR __%s)\n"
224 "{\n",
225 cfg->libbase
227 fprintf(out,
228 " AROS_LIBREQ(%s, %d)\n",
229 cfg->libbase, funclistit->version
231 if (nquad==0)
233 fprintf(out,
234 " %sAROS_LC%d%s(%s, %s,\n",
235 (isvoid) ? "" : "return ",
236 funclistit->argcount, (isvoid) ? "NR" : "",
237 funclistit->type, funclistit->name
240 for (arglistit = funclistit->arguments, count = 1;
241 arglistit!=NULL;
242 arglistit = arglistit->next, count++
245 type = getargtype(arglistit);
246 assert(type != NULL);
247 fprintf(out,
248 " AROS_LCA(%s,(__arg%d),%s),\n",
249 type, count, arglistit->reg
251 free(type);
254 else /* nquad != 0 */
256 if (narg) {
257 fprintf(out,
258 " %sAROS_LC%dQUAD%d%s(%s, %s,\n",
259 (isvoid) ? "" : "return ", narg,
260 nquad, (isvoid) ? "NR" : "",
261 funclistit->type, funclistit->name
263 } else {
264 fprintf(out,
265 " %sAROS_LCQUAD%d%s(%s, %s,\n",
266 (isvoid) ? "" : "return ",
267 nquad, (isvoid) ? "NR" : "",
268 funclistit->type, funclistit->name
272 for (arglistit = funclistit->arguments, count = 1;
273 arglistit != NULL;
274 arglistit = arglistit->next, count++
277 char *quad2 = strchr(arglistit->reg, '/');
279 arglistit->reg[2] = 0;
280 type = getargtype(arglistit);
281 assert(type != NULL);
283 if (quad2 != NULL) {
284 *quad2 = 0;
285 fprintf(out,
286 " AROS_LCAQUAD(%s, (__arg%d), %s, %s), \n",
287 type, count, arglistit->reg, quad2+1
289 *quad2 = '/';
290 } else {
291 fprintf(out,
292 " AROS_LCA(%s, (__arg%d), %s), \n",
293 type, count, arglistit->reg
296 free(type);
299 fprintf(out,
300 " %s, (__%s), %u, %s"
301 " );\n"
302 "}\n\n",
303 cfg->libbasetypeptrextern, cfg->libbase, funclistit->lvo, cfg->basename
306 fprintf(out, "#define %s(", funclistit->name);
307 for (arglistit = funclistit->arguments, count = 1;
308 arglistit != NULL;
309 arglistit = arglistit->next, count++
312 if (arglistit != funclistit->arguments)
313 fprintf(out, ", ");
314 fprintf(out, "arg%d", count);
316 fprintf(out, ") \\\n __inline_%s_%s(", cfg->basename, funclistit->name);
317 for (arglistit = funclistit->arguments, count = 1;
318 arglistit != NULL;
319 arglistit = arglistit->next, count++
321 fprintf(out, "(arg%d), ", count);
322 fprintf(out, "__%s_LIBBASE)\n", cfg->includenameupper);
325 void
326 writeinlinevararg(FILE *out, struct functionhead *funclistit, struct config *cfg, char isvararg, char *varargname)
328 struct functionarg *arglistit = funclistit->arguments;
329 int isvoid;
331 isvoid = strcmp(funclistit->type, "void") == 0
332 || strcmp(funclistit->type, "VOID") == 0;
334 if (isvararg == 1)
336 int count;
337 char *type;
339 fprintf(out,
340 "\n#if !defined(NO_INLINE_STDARG) && !defined(%s_NO_INLINE_STDARG)\n"
341 "#define %s(",
342 cfg->includenameupper, varargname
344 for (arglistit = funclistit->arguments, count = 1;
345 arglistit != NULL && arglistit->next != NULL;
346 arglistit = arglistit->next, count++
349 fprintf(out, "arg%d, ", count);
351 fprintf(out,
352 "...) \\\n"
353 "({ \\\n"
355 for (arglistit = funclistit->arguments, count = 1;
356 arglistit != NULL;
357 arglistit = arglistit->next, count++
360 if (arglistit->next == NULL)
362 fprintf(out, " const IPTR %s_args[] = { AROS_PP_VARIADIC_CAST2IPTR(__VA_ARGS__) };\\\n", funclistit->name);
365 fprintf(out,
366 " %s(",
367 funclistit->name
369 for (arglistit = funclistit->arguments, count = 1;
370 arglistit != NULL;
371 arglistit = arglistit->next, count++
374 if (arglistit != funclistit->arguments)
375 fprintf(out, ", ");
377 if (arglistit->next == NULL)
379 type = getargtype(arglistit);
380 assert(type != NULL);
381 fprintf(out, "(%s)(%s_args)", type, funclistit->name);
382 free(type);
384 else
385 fprintf(out, "(arg%d)", count);
387 fprintf(out,
388 "); \\\n"
389 "})\n"
390 "#endif /* !NO_INLINE_STDARG */\n"
393 else if (isvararg == 2)
395 int count;
397 fprintf(out,
398 "\n#if !defined(NO_INLINE_STDARG) && !defined(%s_NO_INLINE_STDARG)\n"
399 "static inline %s __inline_%s_%s(%s __%s",
400 cfg->includenameupper,
401 funclistit->type, cfg->basename, varargname, cfg->libbasetypeptrextern, cfg->libbase
403 for (arglistit = funclistit->arguments, count = 0;
404 arglistit != NULL && arglistit->next != NULL;
405 arglistit = arglistit->next
408 char *type = getargtype(arglistit);
410 fprintf(out, ", %s __arg%d", type, ++count);
412 fprintf(out, ", ...)\n");
414 fprintf(out,
415 "{\n"
416 " %s retval;\n"
417 " va_list __args;\n"
418 "\n"
419 " va_start(__args, __arg%d);\n"
420 " retval = __inline_%s_%s(",
421 funclistit->type,
422 count,
423 cfg->basename, funclistit->name
425 for (arglistit = funclistit->arguments, count = 1;
426 arglistit != NULL && arglistit->next != NULL;
427 arglistit = arglistit->next, count++
430 fprintf(out, "__arg%d, ", count);
432 fprintf(out,
433 "__args, __%s);\n"
434 " va_end(__args);\n"
435 " return retval;\n"
436 "}\n"
437 "\n"
438 "#define %s(",
439 cfg->libbase,
440 varargname
442 for (arglistit = funclistit->arguments, count = 1;
443 arglistit != NULL && arglistit->next != NULL && arglistit->next->next != NULL;
444 arglistit = arglistit->next, count++
447 fprintf(out, "arg%d, ", count);
449 fprintf(out,
450 "...) \\\n"
451 " __inline_%s_%s(",
452 cfg->basename, varargname
454 fprintf(out, "(%s)__%s_LIBBASE, ",
455 cfg->libbasetypeptrextern,
456 cfg->includenameupper);
457 for (arglistit = funclistit->arguments, count = 1;
458 arglistit != NULL && arglistit->next != NULL && arglistit->next->next != NULL;
459 arglistit = arglistit->next, count++
462 fprintf(out, "(arg%d), ", count);
464 fprintf(out,
465 "__VA_ARGS__)\n"
466 "#endif /* !NO_INLINE_STDARG */\n"
469 else if (isvararg == 3)
471 int count;
473 fprintf(out,
474 "\n#if !defined(NO_INLINE_STDARG) && !defined(%s_NO_INLINE_STDARG)\n"
475 "static inline %s __inline_%s_%s(%s __%s",
476 cfg->includenameupper,
477 funclistit->type, cfg->basename, varargname, cfg->libbasetypeptrextern, cfg->libbase
479 for (arglistit = funclistit->arguments, count = 0;
480 arglistit != NULL && arglistit->next != NULL;
481 arglistit = arglistit->next
484 char *type = getargtype(arglistit);
486 fprintf(out, ", %s __arg%d", type, ++count);
488 fprintf(out, ", ...)\n");
490 fprintf(out,"{\n");
491 if (!isvoid)
492 fprintf(out, " %s retval;\n", funclistit->type);
494 fprintf(out,
495 "\n"
496 " AROS_SLOWSTACKFORMAT_PRE(__arg%d);\n"
497 " %s__inline_%s_%s(",
498 count,
499 isvoid ? "" : "retval = ",
500 cfg->basename, funclistit->name
502 for (arglistit = funclistit->arguments, count = 1;
503 arglistit != NULL && arglistit->next != NULL;
504 arglistit = arglistit->next, count++
507 fprintf(out, "__arg%d, ", count);
509 count--;
510 fprintf(out,
511 "AROS_SLOWSTACKFORMAT_ARG(__arg%d), __%s);\n"
512 " AROS_SLOWSTACKFORMAT_POST(__arg%d);\n"
513 " return%s;\n"
514 "}\n"
515 "\n"
516 "#define %s(",
517 count,
518 cfg->libbase,
519 count,
520 isvoid ? "" : " retval",
521 varargname
523 for (arglistit = funclistit->arguments, count = 1;
524 arglistit != NULL && arglistit->next != NULL && arglistit->next->next != NULL;
525 arglistit = arglistit->next, count++
528 fprintf(out, "arg%d, ", count);
530 fprintf(out,
531 "...) \\\n"
532 " __inline_%s_%s(",
533 cfg->basename, varargname
535 fprintf(out, "(%s)__%s_LIBBASE, ",
536 cfg->libbasetypeptrextern,
537 cfg->includenameupper);
538 for (arglistit = funclistit->arguments, count = 1;
539 arglistit != NULL && arglistit->next != NULL && arglistit->next->next != NULL;
540 arglistit = arglistit->next, count++
543 fprintf(out, "(arg%d), ", count);
545 fprintf(out,
546 "__VA_ARGS__)\n"
547 "#endif /* !NO_INLINE_STDARG */\n"
552 void
553 writealiases(FILE *out, struct functionhead *funclistit, struct config *cfg)
555 struct stringlist *aliasesit;
557 for (aliasesit = funclistit->aliases;
558 aliasesit != NULL;
559 aliasesit = aliasesit->next
562 fprintf(out, "#define %s %s\n", aliasesit->s, funclistit->name);