added concrete implementations of putc(), getc(), getchar() and gets()
[tangerine.git] / tools / genmodule / writeincdefines.c
blob808814b995f23cd691ce0ca18b5e2cbd9703388c
1 /*
2 Copyright © 1995-2007, 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];
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 fprintf(out,
30 "#ifndef DEFINES_%s_PROTOS_H\n"
31 "#define DEFINES_%s_PROTOS_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/preprocessor/variadic/cast2iptr.hpp>\n"
42 "\n",
43 cfg->modulenameupper, cfg->modulenameupper, getBanner(cfg), cfg->modulename
45 if (cfg->command!=DUMMY)
47 for (funclistit = cfg->funclist; funclistit!=NULL; funclistit = funclistit->next)
49 if (!funclistit->priv && (funclistit->lvo >= cfg->firstlvo))
51 if (funclistit->libcall != STACK)
53 writedefineregister(out, funclistit, cfg);
54 if (!funclistit->novararg)
55 writedefinevararg(out, funclistit, cfg);
57 else /* libcall == STACK */
59 writedefinestack(out, funclistit, cfg);
62 writealiases(out, funclistit, cfg);
66 fprintf(out,
67 "\n"
68 "#endif /* DEFINES_%s_PROTOS_H*/\n",
69 cfg->modulenameupper
71 fclose(out);
75 void
76 writedefineregister(FILE *out, struct functionhead *funclistit, struct config *cfg)
78 struct functionarg *arglistit;
79 int count, isvoid;
80 char *type;
82 isvoid = strcmp(funclistit->type, "void") == 0
83 || strcmp(funclistit->type, "VOID") == 0;
85 fprintf(out,
86 "\n"
87 "#define __%s_WB(__%s",
88 funclistit->name, cfg->libbase
90 for (arglistit = funclistit->arguments, count = 1;
91 arglistit!=NULL;
92 arglistit = arglistit->next, count++
95 fprintf(out, ", __arg%d", count);
97 fprintf(out,
98 ") \\\n"
99 " AROS_LC%d%s(%s, %s, \\\n",
100 funclistit->argcount, (isvoid) ? "NR" : "",
101 funclistit->type, funclistit->name
104 for (arglistit = funclistit->arguments, count = 1;
105 arglistit!=NULL;
106 arglistit = arglistit->next, count++
109 type = getargtype(arglistit);
110 assert(type != NULL);
111 fprintf(out,
112 " AROS_LCA(%s,(__arg%d),%s), \\\n",
113 type, count, arglistit->reg
115 free(type);
117 fprintf(out,
118 " %s, (__%s), %u, %s)\n\n",
119 cfg->libbasetypeptrextern, cfg->libbase, funclistit->lvo, cfg->basename
122 fprintf(out, "#define %s(", funclistit->name);
123 for (arglistit = funclistit->arguments, count = 1;
124 arglistit != NULL;
125 arglistit = arglistit->next, count++
128 if (arglistit != funclistit->arguments)
129 fprintf(out, ", ");
130 fprintf(out, "arg%d", count);
132 fprintf(out, ") \\\n __%s_WB(%s", funclistit->name, cfg->libbase);
133 for (arglistit = funclistit->arguments, count = 1;
134 arglistit != NULL;
135 arglistit = arglistit->next, count++
137 fprintf(out, ", (arg%d)", count);
138 fprintf(out, ")\n");
141 void
142 writedefinevararg(FILE *out, struct functionhead *funclistit, struct config *cfg)
144 struct functionarg *arglistit = funclistit->arguments;
145 char isvararg = 0, *varargname, *lastname;
147 /* Go to last argument */
148 if (arglistit == NULL)
149 return;
151 while (arglistit->next != NULL) arglistit = arglistit->next;
153 lastname = getargname(arglistit);
154 assert(lastname != NULL);
156 if (*(funclistit->name + strlen(funclistit->name) - 1) == 'A')
158 isvararg = 1;
159 varargname = strdup(funclistit->name);
160 varargname[strlen(funclistit->name)-1] = '\0';
162 else if (strcmp(funclistit->name + strlen(funclistit->name) - 7, "TagList") == 0)
164 isvararg = 1;
165 /* TagList has to be changed in Tags at the end of the functionname */
166 varargname = strdup(funclistit->name);
167 varargname[strlen(funclistit->name)-4] = 's';
168 varargname[strlen(funclistit->name)-3] = '\0';
170 else if (strcmp(funclistit->name + strlen(funclistit->name) - 4, "Args") == 0
171 && (strcasecmp(lastname, "args") == 0 || strcasecmp(lastname, "arglist") == 0)
174 isvararg = 1;
175 varargname = strdup(funclistit->name);
176 varargname[strlen(funclistit->name)-4] = '\0';
178 else
180 char *p;
182 if (strncmp(arglistit->arg, "struct", 6)==0)
184 p = arglistit->arg + 6;
185 while (isspace(*p)) p++;
186 if (strncmp(p, "TagItem", 7) == 0)
188 p += 7;
189 while (isspace(*p)) p++;
191 if (*p == '*')
193 isvararg = 1;
194 varargname = malloc(strlen(funclistit->name) + 5);
195 strcpy(varargname, funclistit->name);
196 strcat(varargname, "Tags");
201 if (isvararg)
203 int count;
204 char *type;
206 fprintf(out,
207 "\n#if !defined(NO_INLINE_STDARG) && !defined(%s_NO_INLINE_STDARG)\n"
208 "#define %s(",
209 cfg->modulenameupper, varargname
211 for (arglistit = funclistit->arguments, count = 1;
212 arglistit != NULL && arglistit->next != NULL;
213 arglistit = arglistit->next, count++
216 fprintf(out, "arg%d, ", count);
218 fprintf(out,
219 "...) \\\n"
220 "({ \\\n"
221 " IPTR __args[] = { AROS_PP_VARIADIC_CAST2IPTR(__VA_ARGS__) }; \\\n"
222 " %s(",
223 funclistit->name
225 for (arglistit = funclistit->arguments, count = 1;
226 arglistit != NULL;
227 arglistit = arglistit->next, count++
230 if (arglistit != funclistit->arguments)
231 fprintf(out, ", ");
233 if (arglistit->next == NULL)
235 type = getargtype(arglistit);
236 assert(type != NULL);
237 fprintf(out, "(%s)__args", type);
238 free(type);
240 else
241 fprintf(out, "(arg%d)", count);
243 fprintf(out,
244 "); \\\n"
245 "})\n"
246 "#endif /* !NO_INLINE_STDARG */\n"
249 free(varargname);
253 void
254 writedefinestack(FILE *out, struct functionhead *funclistit, struct config *cfg)
256 struct functionarg *arglistit;
258 fprintf(out, "#define %s ((%s (*)(", funclistit->name, funclistit->type);
259 for (arglistit = funclistit->arguments;
260 arglistit != NULL;
261 arglistit = arglistit->next
264 fprintf(out, "%s", arglistit->arg);
265 if (arglistit->next != NULL)
266 fprintf(out, ", ");
268 fprintf(out, "))__AROS_GETVECADDR(%s,%d))\n", cfg->libbase, funclistit->lvo);
271 void
272 writealiases(FILE *out, struct functionhead *funclistit, struct config *cfg)
274 struct stringlist *aliasesit;
276 for (aliasesit = funclistit->aliases;
277 aliasesit != NULL;
278 aliasesit = aliasesit->next
281 fprintf(out, "#define %s %s\n", aliasesit->s, funclistit->name);