Listtree.mcc: rename variables in Listtree_GetEntry to match the template of other...
[AROS.git] / tools / genmodule / writeincinline.c
blob1aa6de0221eef2b1cd148ee4aaceff8e79483b1d
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"
322 for (arglistit = funclistit->arguments, count = 1;
323 arglistit != NULL;
324 arglistit = arglistit->next, count++
327 if (arglistit->next == NULL)
329 fprintf(out, " const IPTR %s_args[] = { AROS_PP_VARIADIC_CAST2IPTR(__VA_ARGS__) };\\\n", funclistit->name);
332 fprintf(out,
333 " %s(",
334 funclistit->name
336 for (arglistit = funclistit->arguments, count = 1;
337 arglistit != NULL;
338 arglistit = arglistit->next, count++
341 if (arglistit != funclistit->arguments)
342 fprintf(out, ", ");
344 if (arglistit->next == NULL)
346 type = getargtype(arglistit);
347 assert(type != NULL);
348 fprintf(out, "(const %s)(%s_args)", type, funclistit->name);
349 free(type);
351 else
352 fprintf(out, "(arg%d)", count);
354 fprintf(out,
355 "); \\\n"
356 "})\n"
357 "#endif /* !NO_INLINE_STDARG */\n"
360 else if (isvararg == 2)
362 int count;
364 fprintf(out,
365 "\n#if !defined(NO_INLINE_STDARG) && !defined(%s_NO_INLINE_STDARG)\n"
366 "static inline %s __inline_%s_%s(%s __%s",
367 cfg->includenameupper,
368 funclistit->type, cfg->basename, varargname, cfg->libbasetypeptrextern, cfg->libbase
370 for (arglistit = funclistit->arguments, count = 0;
371 arglistit != NULL && arglistit->next != NULL;
372 arglistit = arglistit->next
375 char *type = getargtype(arglistit);
377 fprintf(out, ", %s __arg%d", type, ++count);
379 fprintf(out, ", ...)\n");
381 fprintf(out,
382 "{\n"
383 " %s retval;\n"
384 " va_list __args;\n"
385 "\n"
386 " va_start(__args, __arg%d);\n"
387 " retval = __inline_%s_%s(",
388 funclistit->type,
389 count,
390 cfg->basename, funclistit->name
392 for (arglistit = funclistit->arguments, count = 1;
393 arglistit != NULL && arglistit->next != NULL;
394 arglistit = arglistit->next, count++
397 fprintf(out, "__arg%d, ", count);
399 fprintf(out,
400 "__args, __%s);\n"
401 " va_end(__args);\n"
402 " return retval;\n"
403 "}\n"
404 "\n"
405 "#define %s(",
406 cfg->libbase,
407 varargname
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 "...) \\\n"
418 " __inline_%s_%s(%s, ",
419 cfg->basename, varargname, cfg->libbase
421 for (arglistit = funclistit->arguments, count = 1;
422 arglistit != NULL && arglistit->next != NULL && arglistit->next->next != NULL;
423 arglistit = arglistit->next, count++
426 fprintf(out, "(arg%d), ", count);
428 fprintf(out,
429 "__VA_ARGS__)\n"
430 "#endif /* !NO_INLINE_STDARG */\n"
435 void
436 writealiases(FILE *out, struct functionhead *funclistit, struct config *cfg)
438 struct stringlist *aliasesit;
440 for (aliasesit = funclistit->aliases;
441 aliasesit != NULL;
442 aliasesit = aliasesit->next
445 fprintf(out, "#define %s %s\n", aliasesit->s, funclistit->name);