openurl.library: 64-bit pointer casting cleanups
[AROS.git] / tools / genmodule / writeincinline.c
blobaf97d92ac478999f58c53e007fd47d6abade1962
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 *);
10 static void writeinlinevararg(FILE *, struct functionhead *, struct config *);
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 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 fprintf(out,
53 "\n"
54 "#if !defined(__%s_LIBAPI__) || (%d <= __%s_LIBAPI__)"
55 "\n",
56 cfg->includenameupper,
57 funclistit->version,
58 cfg->includenameupper
61 writeinlineregister(out, funclistit, cfg);
62 if (!funclistit->novararg)
63 writeinlinevararg(out, funclistit, cfg);
65 writealiases(out, funclistit, cfg);
67 fprintf(out,
68 "\n"
69 "#endif /* !defined(__%s_LIBAPI__) || (%d <= __%s_LIBAPI__) */"
70 "\n",
71 cfg->includenameupper,
72 funclistit->version,
73 cfg->includenameupper
78 fprintf(out,
79 "\n"
80 "#endif /* INLINE_%s_H*/\n",
81 cfg->includenameupper
83 fclose(out);
87 void
88 writeinlineregister(FILE *out, struct functionhead *funclistit, struct config *cfg)
90 struct functionarg *arglistit;
91 int count, isvoid;
92 int narg=0,nquad=0;
93 char *type;
95 isvoid = strcmp(funclistit->type, "void") == 0
96 || strcmp(funclistit->type, "VOID") == 0;
98 fprintf(out,
99 "\n"
100 "static inline %s __inline_%s_%s(",
101 funclistit->type, cfg->basename, funclistit->name
103 for (arglistit = funclistit->arguments, count = 1;
104 arglistit!=NULL;
105 arglistit = arglistit->next, count++
108 type = getargtype(arglistit);
109 fprintf(out, "%s __arg%d, ", type, count);
110 if (strchr(arglistit->reg, '/') != NULL) {
111 nquad++;
112 } else {
113 narg++;
116 fprintf(out,
117 "APTR __%s)\n"
118 "{\n",
119 cfg->libbase
121 fprintf(out,
122 " AROS_LIBREQ(%s, %d)\n",
123 cfg->libbase, funclistit->version
125 if (nquad==0)
127 fprintf(out,
128 " %sAROS_LC%d%s(%s, %s,\n",
129 (isvoid) ? "" : "return ",
130 funclistit->argcount, (isvoid) ? "NR" : "",
131 funclistit->type, funclistit->name
134 for (arglistit = funclistit->arguments, count = 1;
135 arglistit!=NULL;
136 arglistit = arglistit->next, count++
139 type = getargtype(arglistit);
140 assert(type != NULL);
141 fprintf(out,
142 " AROS_LCA(%s,(__arg%d),%s),\n",
143 type, count, arglistit->reg
145 free(type);
148 else /* nquad != 0 */
150 if (narg) {
151 fprintf(out,
152 " %sAROS_LC%dQUAD%d%s(%s, %s,\n",
153 (isvoid) ? "" : "return ", narg,
154 nquad, (isvoid) ? "NR" : "",
155 funclistit->type, funclistit->name
157 } else {
158 fprintf(out,
159 " %sAROS_LCQUAD%d%s(%s, %s,\n",
160 (isvoid) ? "" : "return ",
161 nquad, (isvoid) ? "NR" : "",
162 funclistit->type, funclistit->name
166 for (arglistit = funclistit->arguments, count = 1;
167 arglistit != NULL;
168 arglistit = arglistit->next, count++
171 char *quad2 = strchr(arglistit->reg, '/');
173 arglistit->reg[2] = 0;
174 type = getargtype(arglistit);
175 assert(type != NULL);
177 if (quad2 != NULL) {
178 *quad2 = 0;
179 fprintf(out,
180 " AROS_LCAQUAD(%s, (__arg%d), %s, %s), \\\n",
181 type, count, arglistit->reg, quad2+1
183 *quad2 = '/';
184 } else {
185 fprintf(out,
186 " AROS_LCA(%s, (__arg%d), %s), \\\n",
187 type, count, arglistit->reg
190 free(type);
193 fprintf(out,
194 " %s, (__%s), %u, %s"
195 " );\n"
196 "}\n\n",
197 cfg->libbasetypeptrextern, cfg->libbase, funclistit->lvo, cfg->basename
200 fprintf(out, "#define %s(", funclistit->name);
201 for (arglistit = funclistit->arguments, count = 1;
202 arglistit != NULL;
203 arglistit = arglistit->next, count++
206 if (arglistit != funclistit->arguments)
207 fprintf(out, ", ");
208 fprintf(out, "arg%d", count);
210 fprintf(out, ") \\\n __inline_%s_%s(", cfg->basename, funclistit->name);
211 for (arglistit = funclistit->arguments, count = 1;
212 arglistit != NULL;
213 arglistit = arglistit->next, count++
215 fprintf(out, "(arg%d), ", count);
216 fprintf(out, "__aros_getbase_%s())\n", cfg->libbase);
219 void
220 writeinlinevararg(FILE *out, struct functionhead *funclistit, struct config *cfg)
222 struct functionarg *arglistit = funclistit->arguments;
223 char isvararg = 0, *varargname, *lastname;
225 /* Go to last argument */
226 if (arglistit == NULL)
227 return;
229 while (arglistit->next != NULL) arglistit = arglistit->next;
231 lastname = getargname(arglistit);
232 assert(lastname != NULL);
234 if (*(funclistit->name + strlen(funclistit->name) - 1) == 'A')
236 isvararg = 1;
237 varargname = strdup(funclistit->name);
238 varargname[strlen(funclistit->name)-1] = '\0';
240 else if (strcmp(funclistit->name + strlen(funclistit->name) - 7, "TagList") == 0)
242 isvararg = 1;
243 /* TagList has to be changed in Tags at the end of the functionname */
244 varargname = strdup(funclistit->name);
245 varargname[strlen(funclistit->name)-4] = 's';
246 varargname[strlen(funclistit->name)-3] = '\0';
248 else if (strcmp(funclistit->name + strlen(funclistit->name) - 4, "Args") == 0
249 && (strcasecmp(lastname, "args") == 0 || strcasecmp(lastname, "arglist") == 0)
252 isvararg = 1;
253 varargname = strdup(funclistit->name);
254 varargname[strlen(funclistit->name)-4] = '\0';
256 else if ((funclistit->name[0] == 'V') && (strncmp(arglistit->arg, "va_list", 7) == 0))
258 isvararg = 2;
259 varargname = malloc(strlen(funclistit->name));
260 strcpy(varargname, &funclistit->name[1]);
262 else
264 char *p;
267 if (strncmp(arglistit->arg, "const", 5) == 0) {
268 p = arglistit->arg + 5;
269 while (isspace(*p)) p++;
270 } else
271 p = arglistit->arg;
272 if (strncmp(p, "struct", 6)==0)
274 p += 6;
275 while (isspace(*p)) p++;
276 if (strncmp(p, "TagItem", 7) == 0)
278 p += 7;
279 while (isspace(*p)) p++;
281 if (*p == '*')
283 isvararg = 1;
284 varargname = malloc(strlen(funclistit->name) + 5);
285 strcpy(varargname, funclistit->name);
286 strcat(varargname, "Tags");
291 if (isvararg == 1)
293 int count;
294 char *type;
296 fprintf(out,
297 "\n#if !defined(NO_INLINE_STDARG) && !defined(%s_NO_INLINE_STDARG)\n"
298 "#define %s(",
299 cfg->includenameupper, varargname
301 for (arglistit = funclistit->arguments, count = 1;
302 arglistit != NULL && arglistit->next != NULL;
303 arglistit = arglistit->next, count++
306 fprintf(out, "arg%d, ", count);
308 fprintf(out,
309 "...) \\\n"
310 "({ \\\n"
311 " %s(",
312 funclistit->name
314 for (arglistit = funclistit->arguments, count = 1;
315 arglistit != NULL;
316 arglistit = arglistit->next, count++
319 if (arglistit != funclistit->arguments)
320 fprintf(out, ", ");
322 if (arglistit->next == NULL)
324 type = getargtype(arglistit);
325 assert(type != NULL);
326 fprintf(out, "(%s)(IPTR []){ AROS_PP_VARIADIC_CAST2IPTR(__VA_ARGS__) }", type);
327 free(type);
329 else
330 fprintf(out, "(arg%d)", count);
332 fprintf(out,
333 "); \\\n"
334 "})\n"
335 "#endif /* !NO_INLINE_STDARG */\n"
338 free(varargname);
340 else if (isvararg == 2)
342 int count;
344 fprintf(out,
345 "\n#if !defined(NO_INLINE_STDARG) && !defined(%s_NO_INLINE_STDARG)\n"
346 "static inline %s __inline_%s_%s(%s __%s",
347 cfg->includenameupper,
348 funclistit->type, cfg->basename, varargname, cfg->libbasetypeptrextern, cfg->libbase
350 for (arglistit = funclistit->arguments, count = 0;
351 arglistit != NULL && arglistit->next != NULL;
352 arglistit = arglistit->next
355 char *type = getargtype(arglistit);
357 fprintf(out, ", %s __arg%d", type, ++count);
359 fprintf(out, ", ...)\n");
361 fprintf(out,
362 "{\n"
363 " %s retval;\n"
364 " va_list __args;\n"
365 "\n"
366 " va_start(__args, __arg%d);\n"
367 " retval = __inline_%s_%s(",
368 funclistit->type,
369 count,
370 cfg->basename, funclistit->name
372 for (arglistit = funclistit->arguments, count = 1;
373 arglistit != NULL && arglistit->next != NULL;
374 arglistit = arglistit->next, count++
377 fprintf(out, "__arg%d, ", count);
379 fprintf(out,
380 "__args, __%s);\n"
381 " va_end(__args);\n"
382 " return retval;\n"
383 "}\n"
384 "\n"
385 "#define %s(",
386 cfg->libbase,
387 varargname
389 for (arglistit = funclistit->arguments, count = 1;
390 arglistit != NULL && arglistit->next != NULL && arglistit->next->next != NULL;
391 arglistit = arglistit->next, count++
394 fprintf(out, "arg%d, ", count);
396 fprintf(out,
397 "...) \\\n"
398 " __inline_%s_%s(%s, ",
399 cfg->basename, varargname, cfg->libbase
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 "__VA_ARGS__)\n"
410 "#endif /* !NO_INLINE_STDARG */\n"
413 free(varargname);
417 void
418 writealiases(FILE *out, struct functionhead *funclistit, struct config *cfg)
420 struct stringlist *aliasesit;
422 for (aliasesit = funclistit->aliases;
423 aliasesit != NULL;
424 aliasesit = aliasesit->next
427 fprintf(out, "#define %s %s\n", aliasesit->s, funclistit->name);