nforce.device: Use AROS_UFIx() macros
[AROS.git] / tools / genmodule / writeincinline.c
blob4985907a79a58ffdc48f27d42214512cf1f5dda5
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/symbolsets.h>\n"
42 "#include <aros/preprocessor/variadic/cast2iptr.hpp>\n"
43 "\n",
44 cfg->modulenameupper, cfg->modulenameupper, banner, cfg->modulename
46 freeBanner(banner);
48 for (funclistit = cfg->funclist; funclistit!=NULL; funclistit = funclistit->next)
50 if (!funclistit->priv && (funclistit->lvo >= cfg->firstlvo))
52 fprintf(out,
53 "\n"
54 "#if !defined(__%s_LIBAPI__) || (%d <= __%s_LIBAPI__)"
55 "\n",
56 cfg->modulenameupper,
57 funclistit->version,
58 cfg->modulenameupper
61 if (funclistit->libcall != STACK)
63 writeinlineregister(out, funclistit, cfg);
64 if (!funclistit->novararg)
65 writeinlinevararg(out, funclistit, cfg);
67 else /* libcall == STACK */
69 /* This is the same as in defines */
70 writedefinestack(out, funclistit, cfg);
73 writealiases(out, funclistit, cfg);
75 fprintf(out,
76 "\n"
77 "#endif /* !defined(__%s_LIBAPI__) || (%d <= __%s_LIBAPI__) */"
78 "\n",
79 cfg->modulenameupper,
80 funclistit->version,
81 cfg->modulenameupper
86 fprintf(out,
87 "\n"
88 "#endif /* INLINE_%s_H*/\n",
89 cfg->modulenameupper
91 fclose(out);
95 void
96 writeinlineregister(FILE *out, struct functionhead *funclistit, struct config *cfg)
98 struct functionarg *arglistit;
99 int count, isvoid;
100 int narg=0,nquad=0;
101 char *type;
103 isvoid = strcmp(funclistit->type, "void") == 0
104 || strcmp(funclistit->type, "VOID") == 0;
106 fprintf(out,
107 "\n"
108 "static inline %s __inline_%s_%s(",
109 funclistit->type, cfg->basename, funclistit->name
111 for (arglistit = funclistit->arguments, count = 1;
112 arglistit!=NULL;
113 arglistit = arglistit->next, count++
116 type = getargtype(arglistit);
117 fprintf(out, "%s __arg%d, ", type, count);
118 if (strchr(arglistit->reg, '/') != NULL) {
119 nquad++;
120 } else {
121 narg++;
124 fprintf(out,
125 "APTR __%s)\n"
126 "{\n",
127 cfg->libbase
129 if (cfg->options & OPTION_AUTOINIT) {
130 fprintf(out,
131 " AROS_LIBREQ(%s, %d)\n",
132 cfg->libbase, funclistit->version
135 if (nquad==0)
137 fprintf(out,
138 " %sAROS_LC%d%s(%s, %s,\n",
139 (isvoid) ? "" : "return ",
140 funclistit->argcount, (isvoid) ? "NR" : "",
141 funclistit->type, funclistit->name
144 for (arglistit = funclistit->arguments, count = 1;
145 arglistit!=NULL;
146 arglistit = arglistit->next, count++
149 type = getargtype(arglistit);
150 assert(type != NULL);
151 fprintf(out,
152 " AROS_LCA(%s,(__arg%d),%s),\n",
153 type, count, arglistit->reg
155 free(type);
158 else /* nquad != 0 */
160 if (narg) {
161 fprintf(out,
162 " %sAROS_LC%dQUAD%d%s(%s, %s,\n",
163 (isvoid) ? "" : "return ", narg,
164 nquad, (isvoid) ? "NR" : "",
165 funclistit->type, funclistit->name
167 } else {
168 fprintf(out,
169 " %sAROS_LCQUAD%d%s(%s, %s,\n",
170 (isvoid) ? "" : "return ",
171 nquad, (isvoid) ? "NR" : "",
172 funclistit->type, funclistit->name
176 for (arglistit = funclistit->arguments, count = 1;
177 arglistit != NULL;
178 arglistit = arglistit->next, count++
181 char *quad2 = strchr(arglistit->reg, '/');
183 arglistit->reg[2] = 0;
184 type = getargtype(arglistit);
185 assert(type != NULL);
187 if (quad2 != NULL) {
188 *quad2 = 0;
189 fprintf(out,
190 " AROS_LCAQUAD(%s, (__arg%d), %s, %s), \\\n",
191 type, count, arglistit->reg, quad2+1
193 *quad2 = '/';
194 } else {
195 fprintf(out,
196 " AROS_LCA(%s, (__arg%d), %s), \\\n",
197 type, count, arglistit->reg
200 free(type);
203 fprintf(out,
204 " %s, (__%s), %u, %s"
205 " );\n"
206 "}\n\n",
207 cfg->libbasetypeptrextern, cfg->libbase, funclistit->lvo, cfg->basename
210 fprintf(out, "#define %s(", funclistit->name);
211 for (arglistit = funclistit->arguments, count = 1;
212 arglistit != NULL;
213 arglistit = arglistit->next, count++
216 if (arglistit != funclistit->arguments)
217 fprintf(out, ", ");
218 fprintf(out, "arg%d", count);
220 fprintf(out, ") \\\n __inline_%s_%s(", cfg->basename, funclistit->name);
221 for (arglistit = funclistit->arguments, count = 1;
222 arglistit != NULL;
223 arglistit = arglistit->next, count++
225 fprintf(out, "(arg%d), ", count);
226 fprintf(out, "(APTR)%s)\n", cfg->libbase);
229 void
230 writeinlinevararg(FILE *out, struct functionhead *funclistit, struct config *cfg)
232 struct functionarg *arglistit = funclistit->arguments;
233 char isvararg = 0, *varargname, *lastname;
235 /* Go to last argument */
236 if (arglistit == NULL)
237 return;
239 while (arglistit->next != NULL) arglistit = arglistit->next;
241 lastname = getargname(arglistit);
242 assert(lastname != NULL);
244 if (*(funclistit->name + strlen(funclistit->name) - 1) == 'A')
246 isvararg = 1;
247 varargname = strdup(funclistit->name);
248 varargname[strlen(funclistit->name)-1] = '\0';
250 else if (strcmp(funclistit->name + strlen(funclistit->name) - 7, "TagList") == 0)
252 isvararg = 1;
253 /* TagList has to be changed in Tags at the end of the functionname */
254 varargname = strdup(funclistit->name);
255 varargname[strlen(funclistit->name)-4] = 's';
256 varargname[strlen(funclistit->name)-3] = '\0';
258 else if (strcmp(funclistit->name + strlen(funclistit->name) - 4, "Args") == 0
259 && (strcasecmp(lastname, "args") == 0 || strcasecmp(lastname, "arglist") == 0)
262 isvararg = 1;
263 varargname = strdup(funclistit->name);
264 varargname[strlen(funclistit->name)-4] = '\0';
266 else if ((funclistit->name[0] == 'V') && (strncmp(arglistit->arg, "va_list", 7) == 0))
268 isvararg = 2;
269 varargname = malloc(strlen(funclistit->name));
270 strcpy(varargname, &funclistit->name[1]);
272 else
274 char *p;
277 if (strncmp(arglistit->arg, "const", 5) == 0) {
278 p = arglistit->arg + 5;
279 while (isspace(*p)) p++;
280 } else
281 p = arglistit->arg;
282 if (strncmp(p, "struct", 6)==0)
284 p += 6;
285 while (isspace(*p)) p++;
286 if (strncmp(p, "TagItem", 7) == 0)
288 p += 7;
289 while (isspace(*p)) p++;
291 if (*p == '*')
293 isvararg = 1;
294 varargname = malloc(strlen(funclistit->name) + 5);
295 strcpy(varargname, funclistit->name);
296 strcat(varargname, "Tags");
301 if (isvararg == 1)
303 int count;
304 char *type;
306 fprintf(out,
307 "\n#if !defined(NO_INLINE_STDARG) && !defined(%s_NO_INLINE_STDARG)\n"
308 "#define %s(",
309 cfg->modulenameupper, varargname
311 for (arglistit = funclistit->arguments, count = 1;
312 arglistit != NULL && arglistit->next != NULL;
313 arglistit = arglistit->next, count++
316 fprintf(out, "arg%d, ", count);
318 fprintf(out,
319 "...) \\\n"
320 "({ \\\n"
321 " %s(",
322 funclistit->name
324 for (arglistit = funclistit->arguments, count = 1;
325 arglistit != NULL;
326 arglistit = arglistit->next, count++
329 if (arglistit != funclistit->arguments)
330 fprintf(out, ", ");
332 if (arglistit->next == NULL)
334 type = getargtype(arglistit);
335 assert(type != NULL);
336 fprintf(out, "(%s)(IPTR []){ AROS_PP_VARIADIC_CAST2IPTR(__VA_ARGS__) }", type);
337 free(type);
339 else
340 fprintf(out, "(arg%d)", count);
342 fprintf(out,
343 "); \\\n"
344 "})\n"
345 "#endif /* !NO_INLINE_STDARG */\n"
348 free(varargname);
350 else if (isvararg == 2)
352 int count;
354 fprintf(out,
355 "\n#if !defined(NO_INLINE_STDARG) && !defined(%s_NO_INLINE_STDARG)\n"
356 "static inline %s __inline_%s_%s(%s __%s",
357 cfg->modulenameupper,
358 funclistit->type, cfg->basename, varargname, cfg->libbasetypeptrextern, cfg->libbase
360 for (arglistit = funclistit->arguments, count = 0;
361 arglistit != NULL && arglistit->next != NULL;
362 arglistit = arglistit->next
365 char *type = getargtype(arglistit);
367 fprintf(out, ", %s __arg%d", type, ++count);
369 fprintf(out, ", ...)\n");
371 fprintf(out,
372 "{\n"
373 " %s retval;\n"
374 " va_list __args;\n"
375 "\n"
376 " va_start(__args, __arg%d);\n"
377 " retval = __inline_%s_%s(",
378 funclistit->type,
379 count,
380 cfg->basename, funclistit->name
382 for (arglistit = funclistit->arguments, count = 1;
383 arglistit != NULL && arglistit->next != NULL;
384 arglistit = arglistit->next, count++
387 fprintf(out, "__arg%d, ", count);
389 fprintf(out,
390 "__args, __%s);\n"
391 " va_end(__args);\n"
392 " return retval;\n"
393 "}\n"
394 "\n"
395 "#define %s(",
396 cfg->libbase,
397 varargname
399 for (arglistit = funclistit->arguments, count = 1;
400 arglistit != NULL && arglistit->next != NULL && arglistit->next->next != NULL;
401 arglistit = arglistit->next, count++
404 fprintf(out, "arg%d, ", count);
406 fprintf(out,
407 "...) \\\n"
408 " __inline_%s_%s(%s, ",
409 cfg->basename, varargname, cfg->libbase
411 for (arglistit = funclistit->arguments, count = 1;
412 arglistit != NULL && arglistit->next != NULL && arglistit->next->next != NULL;
413 arglistit = arglistit->next, count++
416 fprintf(out, "(arg%d), ", count);
418 fprintf(out,
419 "__VA_ARGS__)\n"
420 "#endif /* !NO_INLINE_STDARG */\n"
423 free(varargname);
427 void
428 writealiases(FILE *out, struct functionhead *funclistit, struct config *cfg)
430 struct stringlist *aliasesit;
432 for (aliasesit = funclistit->aliases;
433 aliasesit != NULL;
434 aliasesit = aliasesit->next
437 fprintf(out, "#define %s %s\n", aliasesit->s, funclistit->name);