Fix create_patch macro.
[AROS.git] / tools / genmodule / writeincdefines.c
blob8e16cd2c0f625f1d58da50b362ec774f91d2dee3
1 /*
2 Copyright © 1995-2012, 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 *);
10 static void writedefinevararg(FILE *, struct functionhead *, struct config *);
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->modulename);
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->modulenameupper, cfg->modulenameupper, banner, cfg->modulename
48 freeBanner(banner);
50 for (funclistit = cfg->funclist; funclistit!=NULL; funclistit = funclistit->next)
52 if (!funclistit->priv && (funclistit->lvo >= cfg->firstlvo))
54 fprintf(out,
55 "\n"
56 "#if !defined(__%s_LIBAPI__) || (%d <= __%s_LIBAPI__)"
57 "\n",
58 cfg->modulenameupper,
59 funclistit->version,
60 cfg->modulenameupper
63 if (funclistit->libcall != STACK)
65 writedefineregister(out, funclistit, cfg);
66 if (!funclistit->novararg)
67 writedefinevararg(out, funclistit, cfg);
69 else /* libcall == STACK */
71 writedefinestack(out, funclistit, cfg);
74 writealiases(out, funclistit, cfg);
76 fprintf(out,
77 "\n"
78 "#endif /* !defined(__%s_LIBAPI__) || (%d <= __%s_LIBAPI__) */"
79 "\n",
80 cfg->modulenameupper,
81 funclistit->version,
82 cfg->modulenameupper
87 fprintf(out,
88 "\n"
89 "__END_DECLS\n"
90 "\n"
91 "#endif /* DEFINES_%s_H*/\n",
92 cfg->modulenameupper
94 fclose(out);
98 void
99 writedefineregister(FILE *out, struct functionhead *funclistit, struct config *cfg)
101 struct functionarg *arglistit;
102 int count, isvoid, nquad = 0, narg = 0;
103 char *type;
105 isvoid = strcmp(funclistit->type, "void") == 0
106 || strcmp(funclistit->type, "VOID") == 0;
108 fprintf(out,
109 "\n"
110 "#define __%s_WB(__%s",
111 funclistit->name, cfg->libbase
113 for (arglistit = funclistit->arguments, count = 1;
114 arglistit!=NULL;
115 arglistit = arglistit->next, count++
118 fprintf(out, ", __arg%d", count);
119 if (strchr(arglistit->reg, '/') != NULL) {
120 nquad++;
121 } else {
122 narg++;
125 fprintf(out,
126 ") ({\\\n"
128 fprintf(out,
129 " AROS_LIBREQ(%s,%d)\\\n",
130 cfg->libbase, funclistit->version
132 if (nquad == 0)
134 fprintf(out,
135 " AROS_LC%d%s%s(%s, %s, \\\n",
136 funclistit->argcount, funclistit->unusedlibbase ? "I" : "",
137 (isvoid) ? "NR" : "",
138 funclistit->type, funclistit->name
141 for (arglistit = funclistit->arguments, count = 1;
142 arglistit!=NULL;
143 arglistit = arglistit->next, count++
146 type = getargtype(arglistit);
147 assert(type != NULL);
148 fprintf(out,
149 " AROS_LCA(%s,(__arg%d),%s), \\\n",
150 type, count, arglistit->reg
152 free(type);
155 else
157 if (narg) {
158 fprintf(out,
159 " AROS_LC%dQUAD%d%s(%s, %s,\\\n",
160 narg, nquad, (isvoid) ? "NR" : "",
161 funclistit->type, funclistit->name
163 } else {
164 fprintf(out,
165 " AROS_LCQUAD%d%s(%s, %s,\\\n",
166 nquad, (isvoid) ? "NR" : "",
167 funclistit->type, funclistit->name
171 for (arglistit = funclistit->arguments, count = 1;
172 arglistit != NULL;
173 arglistit = arglistit->next, count++
176 char *quad2 = strchr(arglistit->reg, '/');
178 arglistit->reg[2] = 0;
179 type = getargtype(arglistit);
180 assert(type != NULL);
182 if (quad2 != NULL) {
183 *quad2 = 0;
184 fprintf(out,
185 " AROS_LCAQUAD(%s, (__arg%d), %s, %s), \\\n",
186 type, count, arglistit->reg, quad2+1
188 *quad2 = '/';
189 } else {
190 fprintf(out,
191 " AROS_LCA(%s, (__arg%d), %s), \\\n",
192 type, count, arglistit->reg
195 free(type);
198 fprintf(out,
199 " %s, (__%s), %u, %s);\\\n})\n\n",
200 cfg->libbasetypeptrextern, cfg->libbase, funclistit->lvo, cfg->basename
203 fprintf(out, "#define %s(", funclistit->name);
204 for (arglistit = funclistit->arguments, count = 1;
205 arglistit != NULL;
206 arglistit = arglistit->next, count++
209 if (arglistit != funclistit->arguments)
210 fprintf(out, ", ");
211 fprintf(out, "arg%d", count);
213 fprintf(out, ") \\\n __%s_WB(__aros_getbase_%s()",
214 funclistit->name, cfg->libbase
216 for (arglistit = funclistit->arguments, count = 1;
217 arglistit != NULL;
218 arglistit = arglistit->next, count++
220 fprintf(out, ", (arg%d)", count);
221 fprintf(out, ")\n");
224 void
225 writedefinevararg(FILE *out, struct functionhead *funclistit, struct config *cfg)
227 struct functionarg *arglistit = funclistit->arguments;
228 char isvararg = 0, *varargname, *lastname;
230 /* Go to last argument */
231 if (arglistit == NULL)
232 return;
234 while (arglistit->next != NULL) arglistit = arglistit->next;
236 lastname = getargname(arglistit);
237 assert(lastname != NULL);
239 if (*(funclistit->name + strlen(funclistit->name) - 1) == 'A')
241 isvararg = 1;
242 varargname = strdup(funclistit->name);
243 varargname[strlen(funclistit->name)-1] = '\0';
245 else if (strcmp(funclistit->name + strlen(funclistit->name) - 7, "TagList") == 0)
247 isvararg = 1;
248 /* TagList has to be changed in Tags at the end of the functionname */
249 varargname = strdup(funclistit->name);
250 varargname[strlen(funclistit->name)-4] = 's';
251 varargname[strlen(funclistit->name)-3] = '\0';
253 else if (strcmp(funclistit->name + strlen(funclistit->name) - 4, "Args") == 0
254 && (strcasecmp(lastname, "args") == 0 || strcasecmp(lastname, "arglist") == 0)
257 isvararg = 1;
258 varargname = strdup(funclistit->name);
259 varargname[strlen(funclistit->name)-4] = '\0';
261 else if ((funclistit->name[0] == 'V') && (strncmp(arglistit->arg, "va_list", 7) == 0))
263 isvararg = 2;
264 varargname = malloc(strlen(funclistit->name));
265 strcpy(varargname, &funclistit->name[1]);
267 else
269 char *p;
271 if (strncmp(arglistit->arg, "const", 5) == 0) {
272 p = arglistit->arg + 5;
273 while (isspace(*p)) p++;
274 } else
275 p = arglistit->arg;
276 if (strncmp(p, "struct", 6)==0)
278 p += 6;
279 while (isspace(*p)) p++;
280 if (strncmp(p, "TagItem", 7) == 0)
282 p += 7;
283 while (isspace(*p)) p++;
285 if (*p == '*')
287 isvararg = 1;
288 varargname = malloc(strlen(funclistit->name) + 5);
289 strcpy(varargname, funclistit->name);
290 strcat(varargname, "Tags");
296 if (isvararg == 1)
298 int count;
299 char *type;
301 fprintf(out,
302 "\n#if !defined(NO_INLINE_STDARG) && !defined(%s_NO_INLINE_STDARG)\n"
303 "#define %s(",
304 cfg->modulenameupper, varargname
306 for (arglistit = funclistit->arguments, count = 1;
307 arglistit != NULL && arglistit->next != NULL;
308 arglistit = arglistit->next, count++
311 fprintf(out, "arg%d, ", count);
313 fprintf(out,
314 "...) \\\n"
315 "({ \\\n"
316 " %s(",
317 funclistit->name
319 for (arglistit = funclistit->arguments, count = 1;
320 arglistit != NULL;
321 arglistit = arglistit->next, count++
324 if (arglistit != funclistit->arguments)
325 fprintf(out, ", ");
327 if (arglistit->next == NULL)
329 type = getargtype(arglistit);
330 assert(type != NULL);
331 fprintf(out, "(%s)(IPTR []){ AROS_PP_VARIADIC_CAST2IPTR(__VA_ARGS__) }", type);
332 free(type);
334 else
335 fprintf(out, "(arg%d)", count);
337 fprintf(out,
338 "); \\\n"
339 "})\n"
340 "#endif /* !NO_INLINE_STDARG */\n"
343 free(varargname);
345 else if (isvararg == 2)
347 int count;
348 struct functionarg *lastarg;
350 fprintf(out,
351 "\n#if !defined(NO_INLINE_STDARG) && !defined(%s_NO_INLINE_STDARG)\n"
352 "static inline %s __%s_WB(%s __%s",
353 cfg->modulenameupper,
354 funclistit->type, varargname, cfg->libbasetypeptrextern, cfg->libbase
356 for (arglistit = funclistit->arguments;
357 arglistit != NULL && arglistit->next != NULL;
358 arglistit = arglistit->next
361 fprintf(out, ", %s", arglistit->arg);
362 lastarg = arglistit;
364 fprintf(out, ", ...)\n");
366 fprintf(out,
367 "{\n"
368 " %s retval;\n"
369 " va_list args;\n"
370 "\n"
371 " va_start(args, %s);\n"
372 " retval = __%s_WB(__%s, ",
373 funclistit->type,
374 getargname(lastarg),
375 funclistit->name, cfg->libbase
377 for (arglistit = funclistit->arguments;
378 arglistit != NULL && arglistit->next != NULL;
379 arglistit = arglistit->next
382 fprintf(out, "%s, ", getargname(arglistit));
384 fprintf(out,
385 "args);\n"
386 " va_end(args);\n"
387 " return retval;\n"
388 "}\n"
389 "#define %s(",
390 varargname
392 for (arglistit = funclistit->arguments, count = 1;
393 arglistit != NULL && arglistit->next != NULL && arglistit->next->next != NULL;
394 arglistit = arglistit->next, count++
397 fprintf(out, "arg%d, ", count);
399 fprintf(out,
400 "...) __%s_WB(%s, ",
401 varargname, cfg->libbase
403 for (arglistit = funclistit->arguments, count = 1;
404 arglistit != NULL && arglistit->next != NULL && arglistit->next->next != NULL;
405 arglistit = arglistit->next, count++
408 fprintf(out, "(arg%d), ", count);
410 fprintf(out,
411 "__VA_ARGS__)\n"
412 "#endif /* !NO_INLINE_STDARG */\n"
415 free(varargname);
419 void
420 writedefinestack(FILE *out, struct functionhead *funclistit, struct config *cfg)
422 struct functionarg *arglistit;
424 /* Only if no peropener or pertask base */
425 if (cfg->options & OPTION_DUPBASE)
426 return;
428 fprintf(out, "#define %s ((%s (*)(", funclistit->name, funclistit->type);
429 for (arglistit = funclistit->arguments;
430 arglistit != NULL;
431 arglistit = arglistit->next
434 fprintf(out, "%s", arglistit->arg);
435 if (arglistit->next != NULL)
436 fprintf(out, ", ");
438 fprintf(out, "))__AROS_GETVECADDR(__aros_getbase_%s(),%d))\n",
439 cfg->libbase, funclistit->lvo
443 void
444 writealiases(FILE *out, struct functionhead *funclistit, struct config *cfg)
446 struct stringlist *aliasesit;
448 for (aliasesit = funclistit->aliases;
449 aliasesit != NULL;
450 aliasesit = aliasesit->next
453 fprintf(out, "#define %s %s\n", aliasesit->s, funclistit->name);