grub2: bring back build of aros-side grub2 tools
[AROS.git] / tools / genmodule / writethunk.c
blob46222c1ae0213b92b430a904ac6a903eb0efc308
1 /*
2 Copyright © 2015, The AROS Development Team. All rights reserved.
3 $Id$
5 The code for creating a thunk file for the functions present in the module
6 */
7 #include <string.h>
8 #include <assert.h>
9 #include <stdint.h>
10 #include "functionhead.h"
11 #include "config.h"
13 static void writethunkfunc(FILE *out, struct config *cfg, struct functionhead *funclist)
15 struct functionarg *arglistit;
16 char *type, *name;
17 int is_void;
19 is_void = (strcasecmp(funclist->type, "void") == 0);
21 fprintf(out,
22 "THUNK_PROTO%d(%s, %s, %s %s",
23 funclist->argcount,
24 funclist->type, funclist->internalname,
25 cfg->libbasetypeptrextern, cfg->libbase);
26 for (arglistit = funclist->arguments;
27 arglistit!=NULL;
28 arglistit = arglistit->next
31 type = getargtype(arglistit);
32 name = getargname(arglistit);
33 assert(name != NULL && type != NULL);
34 fprintf(out, ", %s %s", type, name);
35 free(name);
36 free(type);
38 fprintf(out, ");\n\n");
40 fprintf(out,
41 "AROS_LH%d(%s, %s,\n",
42 funclist->argcount, funclist->type, funclist->internalname
45 for (arglistit = funclist->arguments;
46 arglistit!=NULL;
47 arglistit = arglistit->next
50 type = getargtype(arglistit);
51 name = getargname(arglistit);
52 assert(name != NULL && type != NULL);
54 fprintf(out,
55 " AROS_LHA(%s, %s, %s),\n",
56 type, name, arglistit->reg
58 free(type);
59 free(name);
62 fprintf(out,
63 " %s, %s, %u, %s)\n"
64 "{\n"
65 " AROS_LIBFUNC_INIT\n\n",
66 cfg->libbasetypeptrextern, cfg->libbase, funclist->lvo, cfg->basename
68 fprintf(out, " ");
69 fprintf(out, "THUNK_CALL%d(%s, %s, %s",
70 funclist->argcount,
71 is_void ? "" : "return ",
72 funclist->internalname,
73 cfg->libbase);
75 for (arglistit = funclist->arguments;
76 arglistit!=NULL;
77 arglistit = arglistit->next
80 name = getargname(arglistit);
81 assert(name != NULL);
82 fprintf(out, ", %s", name);
83 free(name);
85 fprintf(out, ");\n\n");
86 fprintf(out,
87 " AROS_LIBFUNC_EXIT\n"
88 "}\n\n"
92 void writethunk(struct config *cfg)
94 struct functionhead *funclistit;
95 FILE *out = NULL;
96 char line[256];
97 struct functionarg *arglistit;
98 char *type, *name;
99 uint64_t argmask = 0;
100 int i;
102 for(funclistit = cfg->funclist; funclistit != NULL; funclistit = funclistit->next)
104 argmask |= (1ULL << funclistit->argcount);
107 snprintf(line, 255, "%s/%s_thunk.c", cfg->gendir, cfg->modulename);
108 out = fopen(line, "w");
110 if (out == NULL)
112 perror(line);
113 exit(20);
116 for (i = 0; i < 64; i++) {
117 if (argmask & (1ULL << i)) {
118 int j;
120 fprintf(out,
121 "#define THUNK_PROTO%d(type, name, arg_base", i);
122 for (j = 0; j < i; j++) {
123 fprintf(out, ", arg_%d", j);
125 fprintf(out, ")\\\n\ttype name(");
126 for (j = 0; j < i; j++) {
127 fprintf(out, "arg_%d, ", j);
129 fprintf(out, "arg_base)\n");
131 fprintf(out,
132 "#define THUNK_CALL%d(retcode, name, arg_base", i);
133 for (j = 0; j < i; j++) {
134 fprintf(out, ", arg_%d", j);
136 fprintf(out, ")\\\n\tretcode name(");
137 for (j = 0; j < i; j++) {
138 fprintf(out, "arg_%d, ", j);
140 fprintf(out, "arg_base)\n");
144 for(funclistit = cfg->funclist; funclistit != NULL; funclistit = funclistit->next)
146 writethunkfunc(out, cfg, funclistit);
149 fclose(out);