A buffer variable wasn't dereferenced in two similar comparisons in ResList
[AROS.git] / tools / genmodule / writeincinline.c
blobade3fd435ccff6021d33417d87b5c1ef7dcaf437
1 /*
2 Copyright © 1995-2011, 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 freeBanner(banner);
48 for (funclistit = cfg->funclist; funclistit!=NULL; funclistit = funclistit->next)
50 if (!funclistit->priv && (funclistit->lvo >= cfg->firstlvo) && funclistit->libcall != STACK)
52 char isvararg = 0, *varargname = NULL, *lastname;
54 fprintf(out,
55 "\n"
56 "#if !defined(__%s_LIBAPI__) || (%d <= __%s_LIBAPI__)"
57 "\n",
58 cfg->includenameupper,
59 funclistit->version,
60 cfg->includenameupper
63 if ((!funclistit->novararg) && (funclistit->arguments))
65 struct functionarg *arglistit = funclistit->arguments;
67 while (arglistit->next != NULL) arglistit = arglistit->next;
69 lastname = getargname(arglistit);
70 assert(lastname != NULL);
72 if (*(funclistit->name + strlen(funclistit->name) - 1) == 'A')
74 isvararg = 1;
75 varargname = strdup(funclistit->name);
76 varargname[strlen(funclistit->name)-1] = '\0';
78 else if (strcmp(funclistit->name + strlen(funclistit->name) - 7, "TagList") == 0)
80 isvararg = 1;
81 /* TagList has to be changed in Tags at the end of the functionname */
82 varargname = strdup(funclistit->name);
83 varargname[strlen(funclistit->name)-4] = 's';
84 varargname[strlen(funclistit->name)-3] = '\0';
86 else if (strcmp(funclistit->name + strlen(funclistit->name) - 4, "Args") == 0
87 && (strcasecmp(lastname, "args") == 0 || strcasecmp(lastname, "arglist") == 0)
90 isvararg = 1;
91 varargname = strdup(funclistit->name);
92 varargname[strlen(funclistit->name)-4] = '\0';
94 else if ((funclistit->name[0] == 'V') && (strncmp(arglistit->arg, "va_list", 7) == 0))
96 isvararg = 2;
97 varargname = malloc(strlen(funclistit->name));
98 strcpy(varargname, &funclistit->name[1]);
100 else
102 char *p;
105 if (strncmp(arglistit->arg, "const", 5) == 0) {
106 p = arglistit->arg + 5;
107 while (isspace(*p)) p++;
108 } else
109 p = arglistit->arg;
110 if (strncmp(p, "struct", 6)==0)
112 p += 6;
113 while (isspace(*p)) p++;
114 if (strncmp(p, "TagItem", 7) == 0)
116 p += 7;
117 while (isspace(*p)) p++;
119 if (*p == '*')
121 isvararg = 1;
122 varargname = malloc(strlen(funclistit->name) + 5);
123 strcpy(varargname, funclistit->name);
124 strcat(varargname, "Tags");
131 writeinlineregister(out, funclistit, cfg, isvararg);
132 if (!funclistit->novararg && isvararg)
134 writeinlinevararg(out, funclistit, cfg, isvararg, varargname);
135 free(varargname);
138 writealiases(out, funclistit, cfg);
140 fprintf(out,
141 "\n"
142 "#endif /* !defined(__%s_LIBAPI__) || (%d <= __%s_LIBAPI__) */"
143 "\n",
144 cfg->includenameupper,
145 funclistit->version,
146 cfg->includenameupper
151 fprintf(out,
152 "\n"
153 "#endif /* INLINE_%s_H*/\n",
154 cfg->includenameupper
156 fclose(out);
159 void
160 writeinlineregister(FILE *out, struct functionhead *funclistit, struct config *cfg, char isvararg)
162 struct functionarg *arglistit;
163 int count, isvoid;
164 int narg=0,nquad=0;
165 char *type;
167 isvoid = strcmp(funclistit->type, "void") == 0
168 || strcmp(funclistit->type, "VOID") == 0;
170 fprintf(out,
171 "\n"
172 "static inline %s __inline_%s_%s(",
173 funclistit->type, cfg->basename, funclistit->name
175 for (arglistit = funclistit->arguments, count = 1;
176 arglistit!=NULL;
177 arglistit = arglistit->next, count++
180 type = getargtype(arglistit);
181 fprintf(out, "%s%s __arg%d, ",
182 ((isvararg) && (!arglistit->next)) ? "const " : "",
183 type, count);
184 if (strchr(arglistit->reg, '/') != NULL) {
185 nquad++;
186 } else {
187 narg++;
190 fprintf(out,
191 "APTR __%s)\n"
192 "{\n",
193 cfg->libbase
195 fprintf(out,
196 " AROS_LIBREQ(%s, %d)\n",
197 cfg->libbase, funclistit->version
199 if (nquad==0)
201 fprintf(out,
202 " %sAROS_LC%d%s(%s, %s,\n",
203 (isvoid) ? "" : "return ",
204 funclistit->argcount, (isvoid) ? "NR" : "",
205 funclistit->type, funclistit->name
208 for (arglistit = funclistit->arguments, count = 1;
209 arglistit!=NULL;
210 arglistit = arglistit->next, count++
213 type = getargtype(arglistit);
214 assert(type != NULL);
215 fprintf(out,
216 " AROS_LCA(%s%s,(__arg%d),%s),\n",
217 ((isvararg) && (!arglistit->next)) ? "const " : "",
218 type, count, arglistit->reg
220 free(type);
223 else /* nquad != 0 */
225 if (narg) {
226 fprintf(out,
227 " %sAROS_LC%dQUAD%d%s(%s, %s,\n",
228 (isvoid) ? "" : "return ", narg,
229 nquad, (isvoid) ? "NR" : "",
230 funclistit->type, funclistit->name
232 } else {
233 fprintf(out,
234 " %sAROS_LCQUAD%d%s(%s, %s,\n",
235 (isvoid) ? "" : "return ",
236 nquad, (isvoid) ? "NR" : "",
237 funclistit->type, funclistit->name
241 for (arglistit = funclistit->arguments, count = 1;
242 arglistit != NULL;
243 arglistit = arglistit->next, count++
246 char *quad2 = strchr(arglistit->reg, '/');
248 arglistit->reg[2] = 0;
249 type = getargtype(arglistit);
250 assert(type != NULL);
252 if (quad2 != NULL) {
253 *quad2 = 0;
254 fprintf(out,
255 " AROS_LCAQUAD(%s%s, (__arg%d), %s, %s), \\\n",
256 ((isvararg) && (!arglistit->next)) ? "const " : "",
257 type, count, arglistit->reg, quad2+1
259 *quad2 = '/';
260 } else {
261 fprintf(out,
262 " AROS_LCA(%s%s, (__arg%d), %s), \\\n",
263 ((isvararg) && (!arglistit->next)) ? "const " : "",
264 type, count, arglistit->reg
267 free(type);
270 fprintf(out,
271 " %s, (__%s), %u, %s"
272 " );\n"
273 "}\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 __inline_%s_%s(", cfg->basename, funclistit->name);
288 for (arglistit = funclistit->arguments, count = 1;
289 arglistit != NULL;
290 arglistit = arglistit->next, count++
292 fprintf(out, "(arg%d), ", count);
293 fprintf(out, "__aros_getbase_%s())\n", cfg->libbase);
296 void
297 writeinlinevararg(FILE *out, struct functionhead *funclistit, struct config *cfg, char isvararg, char *varargname)
299 struct functionarg *arglistit = funclistit->arguments;
301 if (isvararg == 1)
303 int count;
304 char *type;
306 fprintf(out,
307 "\n#if !defined(NO_INLINE_STDARG) && !defined(%s_NO_INLINE_STDARG)\n"
308 "#define %s(",
309 cfg->includenameupper, varargname
311 for (arglistit = funclistit->arguments, count = 1;
312 arglistit != NULL && arglistit->next != NULL;
313 arglistit = arglistit->next, count++
316 fprintf(out, "arg%d, ", count);
318 fprintf(out,
319 "...) \\\n"
320 "({ \\\n"
321 " %s(",
322 funclistit->name
324 for (arglistit = funclistit->arguments, count = 1;
325 arglistit != NULL;
326 arglistit = arglistit->next, count++
329 if (arglistit != funclistit->arguments)
330 fprintf(out, ", ");
332 if (arglistit->next == NULL)
334 type = getargtype(arglistit);
335 assert(type != NULL);
336 fprintf(out, "(const %s)(const IPTR []){ AROS_PP_VARIADIC_CAST2IPTR(__VA_ARGS__) }", type);
337 free(type);
339 else
340 fprintf(out, "(arg%d)", count);
342 fprintf(out,
343 "); \\\n"
344 "})\n"
345 "#endif /* !NO_INLINE_STDARG */\n"
348 else if (isvararg == 2)
350 int count;
352 fprintf(out,
353 "\n#if !defined(NO_INLINE_STDARG) && !defined(%s_NO_INLINE_STDARG)\n"
354 "static inline %s __inline_%s_%s(%s __%s",
355 cfg->includenameupper,
356 funclistit->type, cfg->basename, varargname, cfg->libbasetypeptrextern, cfg->libbase
358 for (arglistit = funclistit->arguments, count = 0;
359 arglistit != NULL && arglistit->next != NULL;
360 arglistit = arglistit->next
363 char *type = getargtype(arglistit);
365 fprintf(out, ", %s __arg%d", type, ++count);
367 fprintf(out, ", ...)\n");
369 fprintf(out,
370 "{\n"
371 " %s retval;\n"
372 " va_list __args;\n"
373 "\n"
374 " va_start(__args, __arg%d);\n"
375 " retval = __inline_%s_%s(",
376 funclistit->type,
377 count,
378 cfg->basename, funclistit->name
380 for (arglistit = funclistit->arguments, count = 1;
381 arglistit != NULL && arglistit->next != NULL;
382 arglistit = arglistit->next, count++
385 fprintf(out, "__arg%d, ", count);
387 fprintf(out,
388 "__args, __%s);\n"
389 " va_end(__args);\n"
390 " return retval;\n"
391 "}\n"
392 "\n"
393 "#define %s(",
394 cfg->libbase,
395 varargname
397 for (arglistit = funclistit->arguments, count = 1;
398 arglistit != NULL && arglistit->next != NULL && arglistit->next->next != NULL;
399 arglistit = arglistit->next, count++
402 fprintf(out, "arg%d, ", count);
404 fprintf(out,
405 "...) \\\n"
406 " __inline_%s_%s(%s, ",
407 cfg->basename, varargname, cfg->libbase
409 for (arglistit = funclistit->arguments, count = 1;
410 arglistit != NULL && arglistit->next != NULL && arglistit->next->next != NULL;
411 arglistit = arglistit->next, count++
414 fprintf(out, "(arg%d), ", count);
416 fprintf(out,
417 "__VA_ARGS__)\n"
418 "#endif /* !NO_INLINE_STDARG */\n"
423 void
424 writealiases(FILE *out, struct functionhead *funclistit, struct config *cfg)
426 struct stringlist *aliasesit;
428 for (aliasesit = funclistit->aliases;
429 aliasesit != NULL;
430 aliasesit = aliasesit->next
433 fprintf(out, "#define %s %s\n", aliasesit->s, funclistit->name);