Fixed a few warnings.
[tangerine.git] / tools / genmodule / writeincdefines.c
blob7c44cb4ce27a3602320dd9ec790cba5d915a8431
1 /*
2 Copyright © 1995-2009, 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 writedefinestack(FILE *, struct functionhead *, struct config *);
12 static void writealiases(FILE *, struct functionhead *, struct config *);
14 void writeincdefines(struct config *cfg)
16 FILE *out;
17 char line[256], *banner;
18 struct functionhead *funclistit;
20 snprintf(line, 255, "%s/defines/%s.h", cfg->gendir, cfg->modulename);
21 out = fopen(line, "w");
23 if (out == NULL)
25 perror(line);
26 exit(20);
29 banner = getBanner(cfg);
30 fprintf(out,
31 "#ifndef DEFINES_%s_H\n"
32 "#define DEFINES_%s_H\n"
33 "\n"
34 "%s"
35 "\n"
36 "/*\n"
37 " Desc: Defines for %s\n"
38 "*/\n"
39 "\n"
40 "#include <aros/libcall.h>\n"
41 "#include <exec/types.h>\n"
42 "#include <aros/preprocessor/variadic/cast2iptr.hpp>\n"
43 "\n",
44 cfg->modulenameupper, cfg->modulenameupper, banner, cfg->modulename
46 freeBanner(banner);
48 if (cfg->command!=DUMMY)
50 for (funclistit = cfg->funclist; funclistit!=NULL; funclistit = funclistit->next)
52 if (!funclistit->priv && (funclistit->lvo >= cfg->firstlvo))
54 if (funclistit->libcall != STACK)
56 writedefineregister(out, funclistit, cfg);
57 if (!funclistit->novararg)
58 writedefinevararg(out, funclistit, cfg);
60 else /* libcall == STACK */
62 writedefinestack(out, funclistit, cfg);
65 writealiases(out, funclistit, cfg);
69 fprintf(out,
70 "\n"
71 "#endif /* DEFINES_%s_H*/\n",
72 cfg->modulenameupper
74 fclose(out);
78 void
79 writedefineregister(FILE *out, struct functionhead *funclistit, struct config *cfg)
81 struct functionarg *arglistit;
82 int count, isvoid;
83 char *type;
85 isvoid = strcmp(funclistit->type, "void") == 0
86 || strcmp(funclistit->type, "VOID") == 0;
88 fprintf(out,
89 "\n"
90 "#define __%s_WB(__%s",
91 funclistit->name, cfg->libbase
93 for (arglistit = funclistit->arguments, count = 1;
94 arglistit!=NULL;
95 arglistit = arglistit->next, count++
98 fprintf(out, ", __arg%d", count);
100 fprintf(out,
101 ") \\\n"
102 " AROS_LC%d%s(%s, %s, \\\n",
103 funclistit->argcount, (isvoid) ? "NR" : "",
104 funclistit->type, funclistit->name
107 for (arglistit = funclistit->arguments, count = 1;
108 arglistit!=NULL;
109 arglistit = arglistit->next, count++
112 type = getargtype(arglistit);
113 assert(type != NULL);
114 fprintf(out,
115 " AROS_LCA(%s,(__arg%d),%s), \\\n",
116 type, count, arglistit->reg
118 free(type);
120 fprintf(out,
121 " %s, (__%s), %u, %s)\n\n",
122 cfg->libbasetypeptrextern, cfg->libbase, funclistit->lvo, cfg->basename
125 fprintf(out, "#define %s(", funclistit->name);
126 for (arglistit = funclistit->arguments, count = 1;
127 arglistit != NULL;
128 arglistit = arglistit->next, count++
131 if (arglistit != funclistit->arguments)
132 fprintf(out, ", ");
133 fprintf(out, "arg%d", count);
135 fprintf(out, ") \\\n __%s_WB(%s", funclistit->name, cfg->libbase);
136 for (arglistit = funclistit->arguments, count = 1;
137 arglistit != NULL;
138 arglistit = arglistit->next, count++
140 fprintf(out, ", (arg%d)", count);
141 fprintf(out, ")\n");
144 void
145 writedefinevararg(FILE *out, struct functionhead *funclistit, struct config *cfg)
147 struct functionarg *arglistit = funclistit->arguments;
148 char isvararg = 0, *varargname, *lastname;
150 /* Go to last argument */
151 if (arglistit == NULL)
152 return;
154 while (arglistit->next != NULL) arglistit = arglistit->next;
156 lastname = getargname(arglistit);
157 assert(lastname != NULL);
159 if (*(funclistit->name + strlen(funclistit->name) - 1) == 'A')
161 isvararg = 1;
162 varargname = strdup(funclistit->name);
163 varargname[strlen(funclistit->name)-1] = '\0';
165 else if (strcmp(funclistit->name + strlen(funclistit->name) - 7, "TagList") == 0)
167 isvararg = 1;
168 /* TagList has to be changed in Tags at the end of the functionname */
169 varargname = strdup(funclistit->name);
170 varargname[strlen(funclistit->name)-4] = 's';
171 varargname[strlen(funclistit->name)-3] = '\0';
173 else if (strcmp(funclistit->name + strlen(funclistit->name) - 4, "Args") == 0
174 && (strcasecmp(lastname, "args") == 0 || strcasecmp(lastname, "arglist") == 0)
177 isvararg = 1;
178 varargname = strdup(funclistit->name);
179 varargname[strlen(funclistit->name)-4] = '\0';
181 else
183 char *p;
185 if (strncmp(arglistit->arg, "const", 5) == 0) {
186 p = arglistit->arg + 5;
187 while (isspace(*p)) p++;
188 } else
189 p = arglistit->arg;
190 if (strncmp(p, "struct", 6)==0)
192 p += 6;
193 while (isspace(*p)) p++;
194 if (strncmp(p, "TagItem", 7) == 0)
196 p += 7;
197 while (isspace(*p)) p++;
199 if (*p == '*')
201 isvararg = 1;
202 varargname = malloc(strlen(funclistit->name) + 5);
203 strcpy(varargname, funclistit->name);
204 strcat(varargname, "Tags");
209 if (isvararg)
211 int count;
212 char *type;
214 fprintf(out,
215 "\n#if !defined(NO_INLINE_STDARG) && !defined(%s_NO_INLINE_STDARG)\n"
216 "#define %s(",
217 cfg->modulenameupper, varargname
219 for (arglistit = funclistit->arguments, count = 1;
220 arglistit != NULL && arglistit->next != NULL;
221 arglistit = arglistit->next, count++
224 fprintf(out, "arg%d, ", count);
226 fprintf(out,
227 "...) \\\n"
228 "({ \\\n"
229 " IPTR __args[] = { AROS_PP_VARIADIC_CAST2IPTR(__VA_ARGS__) }; \\\n"
230 " %s(",
231 funclistit->name
233 for (arglistit = funclistit->arguments, count = 1;
234 arglistit != NULL;
235 arglistit = arglistit->next, count++
238 if (arglistit != funclistit->arguments)
239 fprintf(out, ", ");
241 if (arglistit->next == NULL)
243 type = getargtype(arglistit);
244 assert(type != NULL);
245 fprintf(out, "(%s)__args", type);
246 free(type);
248 else
249 fprintf(out, "(arg%d)", count);
251 fprintf(out,
252 "); \\\n"
253 "})\n"
254 "#endif /* !NO_INLINE_STDARG */\n"
257 free(varargname);
261 void
262 writedefinestack(FILE *out, struct functionhead *funclistit, struct config *cfg)
264 struct functionarg *arglistit;
266 fprintf(out, "#define %s ((%s (*)(", funclistit->name, funclistit->type);
267 for (arglistit = funclistit->arguments;
268 arglistit != NULL;
269 arglistit = arglistit->next
272 fprintf(out, "%s", arglistit->arg);
273 if (arglistit->next != NULL)
274 fprintf(out, ", ");
276 fprintf(out, "))__AROS_GETVECADDR(%s,%d))\n", cfg->libbase, funclistit->lvo);
279 void
280 writealiases(FILE *out, struct functionhead *funclistit, struct config *cfg)
282 struct stringlist *aliasesit;
284 for (aliasesit = funclistit->aliases;
285 aliasesit != NULL;
286 aliasesit = aliasesit->next
289 fprintf(out, "#define %s %s\n", aliasesit->s, funclistit->name);