Added includes generation by genmodule that will use static inline function to call...
[AROS.git] / tools / genmodule / writeincinline.c
blob14df0a2ede55624c16fc67d6dd9062596fd24a9a
1 /*
2 Copyright © 1995-2008, 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];
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 fprintf(out,
29 "#ifndef INLINE_%s_H\n"
30 "#define INLINE_%s_H\n"
31 "\n"
32 "%s"
33 "\n"
34 "/*\n"
35 " Desc: Inline function for %s\n"
36 "*/\n"
37 "\n"
38 "#include <aros/libcall.h>\n"
39 "#include <exec/types.h>\n"
40 "#include <aros/preprocessor/variadic/cast2iptr.hpp>\n"
41 "\n",
42 cfg->modulenameupper, cfg->modulenameupper, getBanner(cfg), cfg->modulename
44 if (cfg->command!=DUMMY)
46 for (funclistit = cfg->funclist; funclistit!=NULL; funclistit = funclistit->next)
48 if (!funclistit->priv && (funclistit->lvo >= cfg->firstlvo))
50 if (funclistit->libcall != STACK)
52 writeinlineregister(out, funclistit, cfg);
53 if (!funclistit->novararg)
54 writeinlinevararg(out, funclistit, cfg);
56 else /* libcall == STACK */
58 /* NOP: nothing to be written for stack argument passing.
59 The stubs in sthe link library will be used */
62 writealiases(out, funclistit, cfg);
66 fprintf(out,
67 "\n"
68 "#endif /* INLINE_%s_H*/\n",
69 cfg->modulenameupper
71 fclose(out);
75 void
76 writeinlineregister(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 "static inline %s __%s_inline(",
88 funclistit->type, funclistit->name
90 for (arglistit = funclistit->arguments, count = 1;
91 arglistit!=NULL;
92 arglistit = arglistit->next, count++
95 type = getargtype(arglistit);
96 fprintf(out, "%s __arg%d, ", type, count);
98 fprintf(out,
99 "%s __%s)\n"
100 "{\n"
101 " %sAROS_LC%d%s(%s, %s,\n",
102 cfg->libbasetypeptrextern, cfg->libbase,
103 (isvoid) ? "" : "return ",
104 funclistit->argcount, (isvoid) ? "NR" : "",
105 funclistit->type, funclistit->name
108 for (arglistit = funclistit->arguments, count = 1;
109 arglistit!=NULL;
110 arglistit = arglistit->next, count++
113 type = getargtype(arglistit);
114 assert(type != NULL);
115 fprintf(out,
116 " AROS_LCA(%s,(__arg%d),%s),\n",
117 type, count, arglistit->reg
119 free(type);
121 fprintf(out,
122 " %s, (__%s), %u, %s"
123 " );\n"
124 "}\n\n",
125 cfg->libbasetypeptrextern, cfg->libbase, funclistit->lvo, cfg->basename
128 fprintf(out, "#define %s(", funclistit->name);
129 for (arglistit = funclistit->arguments, count = 1;
130 arglistit != NULL;
131 arglistit = arglistit->next, count++
134 if (arglistit != funclistit->arguments)
135 fprintf(out, ", ");
136 fprintf(out, "arg%d", count);
138 fprintf(out, ") \\\n __%s_inline(", funclistit->name);
139 for (arglistit = funclistit->arguments, count = 1;
140 arglistit != NULL;
141 arglistit = arglistit->next, count++
143 fprintf(out, "(arg%d), ", count);
144 fprintf(out, "%s)\n", cfg->libbase);
147 void
148 writeinlinevararg(FILE *out, struct functionhead *funclistit, struct config *cfg)
150 struct functionarg *arglistit = funclistit->arguments;
151 char isvararg = 0, *varargname, *lastname;
153 /* Go to last argument */
154 if (arglistit == NULL)
155 return;
157 while (arglistit->next != NULL) arglistit = arglistit->next;
159 lastname = getargname(arglistit);
160 assert(lastname != NULL);
162 if (*(funclistit->name + strlen(funclistit->name) - 1) == 'A')
164 isvararg = 1;
165 varargname = strdup(funclistit->name);
166 varargname[strlen(funclistit->name)-1] = '\0';
168 else if (strcmp(funclistit->name + strlen(funclistit->name) - 7, "TagList") == 0)
170 isvararg = 1;
171 /* TagList has to be changed in Tags at the end of the functionname */
172 varargname = strdup(funclistit->name);
173 varargname[strlen(funclistit->name)-4] = 's';
174 varargname[strlen(funclistit->name)-3] = '\0';
176 else if (strcmp(funclistit->name + strlen(funclistit->name) - 4, "Args") == 0
177 && (strcasecmp(lastname, "args") == 0 || strcasecmp(lastname, "arglist") == 0)
180 isvararg = 1;
181 varargname = strdup(funclistit->name);
182 varargname[strlen(funclistit->name)-4] = '\0';
184 else
186 char *p;
189 if (strncmp(arglistit->arg, "const", 5) == 0) {
190 p = arglistit->arg + 5;
191 while (isspace(*p)) p++;
192 } else
193 p = arglistit->arg;
194 if (strncmp(p, "struct", 6)==0)
196 p += 6;
197 while (isspace(*p)) p++;
198 if (strncmp(p, "TagItem", 7) == 0)
200 p += 7;
201 while (isspace(*p)) p++;
203 if (*p == '*')
205 isvararg = 1;
206 varargname = malloc(strlen(funclistit->name) + 5);
207 strcpy(varargname, funclistit->name);
208 strcat(varargname, "Tags");
213 if (isvararg)
215 int count;
216 char *type;
218 fprintf(out,
219 "\n#if !defined(NO_INLINE_STDARG) && !defined(%s_NO_INLINE_STDARG)\n"
220 "#define %s(",
221 cfg->modulenameupper, varargname
223 for (arglistit = funclistit->arguments, count = 1;
224 arglistit != NULL && arglistit->next != NULL;
225 arglistit = arglistit->next, count++
228 fprintf(out, "arg%d, ", count);
230 fprintf(out,
231 "...) \\\n"
232 "({ \\\n"
233 " IPTR __args[] = { AROS_PP_VARIADIC_CAST2IPTR(__VA_ARGS__) }; \\\n"
234 " %s(",
235 funclistit->name
237 for (arglistit = funclistit->arguments, count = 1;
238 arglistit != NULL;
239 arglistit = arglistit->next, count++
242 if (arglistit != funclistit->arguments)
243 fprintf(out, ", ");
245 if (arglistit->next == NULL)
247 type = getargtype(arglistit);
248 assert(type != NULL);
249 fprintf(out, "(%s)__args", type);
250 free(type);
252 else
253 fprintf(out, "(arg%d)", count);
255 fprintf(out,
256 "); \\\n"
257 "})\n"
258 "#endif /* !NO_INLINE_STDARG */\n"
261 free(varargname);
265 void
266 writealiases(FILE *out, struct functionhead *funclistit, struct config *cfg)
268 struct stringlist *aliasesit;
270 for (aliasesit = funclistit->aliases;
271 aliasesit != NULL;
272 aliasesit = aliasesit->next
275 fprintf(out, "#define %s %s\n", aliasesit->s, funclistit->name);