- Set a default PCM volume so that something can be heard when driver is
[AROS.git] / tools / genmodule / writeincinline.c
blob432991599a357034d0decc2891823d9507060ba9
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->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 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/preprocessor/variadic/cast2iptr.hpp>\n"
42 "\n",
43 cfg->modulenameupper, cfg->modulenameupper, banner, cfg->modulename
45 freeBanner(banner);
47 for (funclistit = cfg->funclist; funclistit!=NULL; funclistit = funclistit->next)
49 if (!funclistit->priv && (funclistit->lvo >= cfg->firstlvo))
51 if (funclistit->libcall != STACK)
53 writeinlineregister(out, funclistit, cfg);
54 if (!funclistit->novararg)
55 writeinlinevararg(out, funclistit, cfg);
57 else /* libcall == STACK */
59 /* NOP: nothing to be written for stack argument passing.
60 The stubs in sthe link library will be used */
63 writealiases(out, funclistit, cfg);
67 fprintf(out,
68 "\n"
69 "#endif /* INLINE_%s_H*/\n",
70 cfg->modulenameupper
72 fclose(out);
76 void
77 writeinlineregister(FILE *out, struct functionhead *funclistit, struct config *cfg)
79 struct functionarg *arglistit;
80 int count, isvoid;
81 int narg=0,nquad=0;
82 char *type;
84 isvoid = strcmp(funclistit->type, "void") == 0
85 || strcmp(funclistit->type, "VOID") == 0;
87 fprintf(out,
88 "\n"
89 "static inline %s __inline_%s_%s(",
90 funclistit->type, cfg->basename, funclistit->name
92 for (arglistit = funclistit->arguments, count = 1;
93 arglistit!=NULL;
94 arglistit = arglistit->next, count++
97 type = getargtype(arglistit);
98 fprintf(out, "%s __arg%d, ", type, count);
99 if (strchr(arglistit->reg, '/') != NULL) {
100 nquad++;
101 } else {
102 narg++;
105 if (nquad==0)
107 fprintf(out,
108 "APTR __%s)\n"
109 "{\n"
110 " %sAROS_LC%d%s(%s, %s,\n",
111 cfg->libbase,
112 (isvoid) ? "" : "return ",
113 funclistit->argcount, (isvoid) ? "NR" : "",
114 funclistit->type, funclistit->name
117 for (arglistit = funclistit->arguments, count = 1;
118 arglistit!=NULL;
119 arglistit = arglistit->next, count++
122 type = getargtype(arglistit);
123 assert(type != NULL);
124 fprintf(out,
125 " AROS_LCA(%s,(__arg%d),%s),\n",
126 type, count, arglistit->reg
128 free(type);
131 else /* nquad != 0 */
133 if (narg) {
134 fprintf(out,
135 "APTR __%s)\n"
136 "{\n"
137 " %sAROS_LC%dQUAD%d%s(%s, %s,\n",
138 cfg->libbase,
139 (isvoid) ? "" : "return ", narg,
140 nquad, (isvoid) ? "NR" : "",
141 funclistit->type, funclistit->name
143 } else {
144 fprintf(out,
145 "APTR __%s)\n"
146 "{\n"
147 " %sAROS_LCQUAD%d%s(%s, %s,\n",
148 cfg->libbase,
149 (isvoid) ? "" : "return ",
150 nquad, (isvoid) ? "NR" : "",
151 funclistit->type, funclistit->name
155 for (arglistit = funclistit->arguments, count = 1;
156 arglistit != NULL;
157 arglistit = arglistit->next, count++
160 char *quad2 = strchr(arglistit->reg, '/');
162 arglistit->reg[2] = 0;
163 type = getargtype(arglistit);
164 assert(type != NULL);
166 if (quad2 != NULL) {
167 *quad2 = 0;
168 fprintf(out,
169 " AROS_LCAQUAD(%s, (__arg%d), %s, %s), \\\n",
170 type, count, arglistit->reg, quad2+1
172 *quad2 = '/';
173 } else {
174 fprintf(out,
175 " AROS_LCA(%s, (__arg%d), %s), \\\n",
176 type, count, arglistit->reg
179 free(type);
182 fprintf(out,
183 " %s, (__%s), %u, %s"
184 " );\n"
185 "}\n\n",
186 cfg->libbasetypeptrextern, cfg->libbase, funclistit->lvo, cfg->basename
189 fprintf(out, "#define %s(", funclistit->name);
190 for (arglistit = funclistit->arguments, count = 1;
191 arglistit != NULL;
192 arglistit = arglistit->next, count++
195 if (arglistit != funclistit->arguments)
196 fprintf(out, ", ");
197 fprintf(out, "arg%d", count);
199 fprintf(out, ") \\\n __inline_%s_%s(", cfg->basename, funclistit->name);
200 for (arglistit = funclistit->arguments, count = 1;
201 arglistit != NULL;
202 arglistit = arglistit->next, count++
204 fprintf(out, "(arg%d), ", count);
205 fprintf(out, "(APTR)%s)\n", cfg->libbase);
208 void
209 writeinlinevararg(FILE *out, struct functionhead *funclistit, struct config *cfg)
211 struct functionarg *arglistit = funclistit->arguments;
212 char isvararg = 0, *varargname, *lastname;
214 /* Go to last argument */
215 if (arglistit == NULL)
216 return;
218 while (arglistit->next != NULL) arglistit = arglistit->next;
220 lastname = getargname(arglistit);
221 assert(lastname != NULL);
223 if (*(funclistit->name + strlen(funclistit->name) - 1) == 'A')
225 isvararg = 1;
226 varargname = strdup(funclistit->name);
227 varargname[strlen(funclistit->name)-1] = '\0';
229 else if (strcmp(funclistit->name + strlen(funclistit->name) - 7, "TagList") == 0)
231 isvararg = 1;
232 /* TagList has to be changed in Tags at the end of the functionname */
233 varargname = strdup(funclistit->name);
234 varargname[strlen(funclistit->name)-4] = 's';
235 varargname[strlen(funclistit->name)-3] = '\0';
237 else if (strcmp(funclistit->name + strlen(funclistit->name) - 4, "Args") == 0
238 && (strcasecmp(lastname, "args") == 0 || strcasecmp(lastname, "arglist") == 0)
241 isvararg = 1;
242 varargname = strdup(funclistit->name);
243 varargname[strlen(funclistit->name)-4] = '\0';
245 else
247 char *p;
250 if (strncmp(arglistit->arg, "const", 5) == 0) {
251 p = arglistit->arg + 5;
252 while (isspace(*p)) p++;
253 } else
254 p = arglistit->arg;
255 if (strncmp(p, "struct", 6)==0)
257 p += 6;
258 while (isspace(*p)) p++;
259 if (strncmp(p, "TagItem", 7) == 0)
261 p += 7;
262 while (isspace(*p)) p++;
264 if (*p == '*')
266 isvararg = 1;
267 varargname = malloc(strlen(funclistit->name) + 5);
268 strcpy(varargname, funclistit->name);
269 strcat(varargname, "Tags");
274 if (isvararg)
276 int count;
277 char *type;
279 fprintf(out,
280 "\n#if !defined(NO_INLINE_STDARG) && !defined(%s_NO_INLINE_STDARG)\n"
281 "#define %s(",
282 cfg->modulenameupper, varargname
284 for (arglistit = funclistit->arguments, count = 1;
285 arglistit != NULL && arglistit->next != NULL;
286 arglistit = arglistit->next, count++
289 fprintf(out, "arg%d, ", count);
291 fprintf(out,
292 "...) \\\n"
293 "({ \\\n"
294 " IPTR __args[] = { AROS_PP_VARIADIC_CAST2IPTR(__VA_ARGS__) }; \\\n"
295 " %s(",
296 funclistit->name
298 for (arglistit = funclistit->arguments, count = 1;
299 arglistit != NULL;
300 arglistit = arglistit->next, count++
303 if (arglistit != funclistit->arguments)
304 fprintf(out, ", ");
306 if (arglistit->next == NULL)
308 type = getargtype(arglistit);
309 assert(type != NULL);
310 fprintf(out, "(%s)__args", type);
311 free(type);
313 else
314 fprintf(out, "(arg%d)", count);
316 fprintf(out,
317 "); \\\n"
318 "})\n"
319 "#endif /* !NO_INLINE_STDARG */\n"
322 free(varargname);
326 void
327 writealiases(FILE *out, struct functionhead *funclistit, struct config *cfg)
329 struct stringlist *aliasesit;
331 for (aliasesit = funclistit->aliases;
332 aliasesit != NULL;
333 aliasesit = aliasesit->next
336 fprintf(out, "#define %s %s\n", aliasesit->s, funclistit->name);