blob: e3f816f30d3544e66ea97c7ae38b4b458f41a5e2 [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001
Joe Onorato84614dd2009-08-10 15:01:51 -07002#include "spec.h"
3#include <stdio.h>
Jason Sams326e0dd2009-05-22 14:03:28 -07004
5void printFileHeader(FILE *f)
6{
7 fprintf(f, "/*\n");
8 fprintf(f, " * Copyright (C) 2009 The Android Open Source Project\n");
9 fprintf(f, " *\n");
10 fprintf(f, " * Licensed under the Apache License, Version 2.0 (the \"License\");\n");
11 fprintf(f, " * you may not use this file except in compliance with the License.\n");
12 fprintf(f, " * You may obtain a copy of the License at\n");
13 fprintf(f, " *\n");
14 fprintf(f, " * http://www.apache.org/licenses/LICENSE-2.0\n");
15 fprintf(f, " *\n");
16 fprintf(f, " * Unless required by applicable law or agreed to in writing, software\n");
17 fprintf(f, " * distributed under the License is distributed on an \"AS IS\" BASIS,\n");
18 fprintf(f, " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n");
19 fprintf(f, " * See the License for the specific language governing permissions and\n");
20 fprintf(f, " * limitations under the License.\n");
21 fprintf(f, " */\n\n");
22}
23
24void printVarType(FILE *f, const VarType *vt)
25{
26 int ct;
27 if (vt->isConst) {
28 fprintf(f, "const ");
29 }
30
31 switch(vt->type) {
32 case 0:
33 fprintf(f, "void");
34 break;
35 case 1:
36 fprintf(f, "int%i_t", vt->bits);
37 break;
38 case 2:
39 fprintf(f, "uint%i_t", vt->bits);
40 break;
41 case 3:
42 if (vt->bits == 32)
43 fprintf(f, "float");
44 else
45 fprintf(f, "double");
46 break;
47 case 4:
Joe Onorato84614dd2009-08-10 15:01:51 -070048 fprintf(f, "%s", vt->typeName);
Jason Sams326e0dd2009-05-22 14:03:28 -070049 break;
50 }
51
52 if(vt->ptrLevel) {
53 fprintf(f, " ");
54 for(ct=0; ct < vt->ptrLevel; ct++) {
55 fprintf(f, "*");
56 }
57 }
58
59 if(vt->name[0]) {
60 fprintf(f, " %s", vt->name);
61 }
62}
63
64void printArgList(FILE *f, const ApiEntry * api, int assumePrevious)
65{
66 int ct;
67 for(ct=0; ct < api->paramCount; ct++) {
68 if (ct || assumePrevious) {
69 fprintf(f, ", ");
70 }
71 printVarType(f, &api->params[ct]);
72 }
73}
74
75void printStructures(FILE *f)
76{
77 int ct;
78 int ct2;
79
80 for(ct=0; ct < apiCount; ct++) {
81 fprintf(f, "typedef struct RS_CMD_%s_rec RS_CMD_%s;\n", apis[ct].name, apis[ct].name);
82 }
83 fprintf(f, "\n");
84
85 for(ct=0; ct < apiCount; ct++) {
86 const ApiEntry * api = &apis[ct];
87 fprintf(f, "#define RS_CMD_ID_%s %i\n", api->name, ct+1);
88 fprintf(f, "struct RS_CMD_%s_rec {\n", api->name);
89 //fprintf(f, " RsCommandHeader _hdr;\n");
90
91 for(ct2=0; ct2 < api->paramCount; ct2++) {
92 fprintf(f, " ");
93 printVarType(f, &api->params[ct2]);
94 fprintf(f, ";\n");
95 }
96 fprintf(f, "};\n\n");
97 }
98}
99
100void printFuncDecl(FILE *f, const ApiEntry *api, const char *prefix, int addContext)
101{
102 printVarType(f, &api->ret);
103 fprintf(f, " %s%s (", prefix, api->name);
104 if (addContext) {
105 fprintf(f, "Context *");
Jason Samsfcd31922009-08-17 18:35:48 -0700106 } else {
107 fprintf(f, "RsContext rsc");
Jason Sams326e0dd2009-05-22 14:03:28 -0700108 }
Jason Samsfcd31922009-08-17 18:35:48 -0700109 printArgList(f, api, 1);
Jason Sams326e0dd2009-05-22 14:03:28 -0700110 fprintf(f, ")");
111}
112
113void printFuncDecls(FILE *f, const char *prefix, int addContext)
114{
115 int ct;
116 for(ct=0; ct < apiCount; ct++) {
117 printFuncDecl(f, &apis[ct], prefix, addContext);
118 fprintf(f, ";\n");
119 }
120 fprintf(f, "\n\n");
121}
122
123void printPlaybackFuncs(FILE *f, const char *prefix)
124{
125 int ct;
126 for(ct=0; ct < apiCount; ct++) {
127 fprintf(f, "void %s%s (Context *, const void *);\n", prefix, apis[ct].name);
128 }
129}
130
131void printApiCpp(FILE *f)
132{
133 int ct;
134 int ct2;
135
136 fprintf(f, "#include \"rsDevice.h\"\n");
137 fprintf(f, "#include \"rsContext.h\"\n");
138 fprintf(f, "#include \"rsThreadIO.h\"\n");
139 //fprintf(f, "#include \"rsgApiStructs.h\"\n");
140 fprintf(f, "#include \"rsgApiFuncDecl.h\"\n");
141 fprintf(f, "\n");
142 fprintf(f, "using namespace android;\n");
143 fprintf(f, "using namespace android::renderscript;\n");
144 fprintf(f, "\n");
145
146 for(ct=0; ct < apiCount; ct++) {
147 int needFlush = 0;
148 const ApiEntry * api = &apis[ct];
149
150 printFuncDecl(f, api, "rs", 0);
151 fprintf(f, "\n{\n");
Jason Samsfcd31922009-08-17 18:35:48 -0700152 fprintf(f, " ThreadIO *io = &((Context *)rsc)->mIO;\n");
Jason Sams326e0dd2009-05-22 14:03:28 -0700153 //fprintf(f, " LOGE(\"add command %s\\n\");\n", api->name);
154 fprintf(f, " RS_CMD_%s *cmd = static_cast<RS_CMD_%s *>(io->mToCore.reserve(sizeof(RS_CMD_%s)));\n", api->name, api->name, api->name);
155 fprintf(f, " uint32_t size = sizeof(RS_CMD_%s);\n", api->name);
156
157 for(ct2=0; ct2 < api->paramCount; ct2++) {
158 const VarType *vt = &api->params[ct2];
159 needFlush += vt->ptrLevel;
160 fprintf(f, " cmd->%s = %s;\n", vt->name, vt->name);
161 }
Joe Onorato84614dd2009-08-10 15:01:51 -0700162 if (api->ret.typeName[0]) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700163 needFlush = 1;
164 }
165
166 fprintf(f, " io->mToCore.commit");
167 if (needFlush) {
168 fprintf(f, "Sync");
169 }
170 fprintf(f, "(RS_CMD_ID_%s, size);\n", api->name);
171
Joe Onorato84614dd2009-08-10 15:01:51 -0700172 if (api->ret.typeName[0]) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700173 fprintf(f, " return reinterpret_cast<");
174 printVarType(f, &api->ret);
175 fprintf(f, ">(io->mToCoreRet);\n");
176 }
177 fprintf(f, "};\n\n");
178 }
179}
180
181void printPlaybackCpp(FILE *f)
182{
183 int ct;
184 int ct2;
185
186 fprintf(f, "#include \"rsDevice.h\"\n");
187 fprintf(f, "#include \"rsContext.h\"\n");
188 fprintf(f, "#include \"rsThreadIO.h\"\n");
189 //fprintf(f, "#include \"rsgApiStructs.h\"\n");
190 fprintf(f, "#include \"rsgApiFuncDecl.h\"\n");
191 fprintf(f, "\n");
192 fprintf(f, "namespace android {\n");
193 fprintf(f, "namespace renderscript {\n");
194 fprintf(f, "\n");
195
196 for(ct=0; ct < apiCount; ct++) {
197 const ApiEntry * api = &apis[ct];
198
199 fprintf(f, "void rsp_%s(Context *con, const void *vp)\n", api->name);
200 fprintf(f, "{\n");
201 //fprintf(f, " LOGE(\"play command %s\\n\");\n", api->name);
202 fprintf(f, " const RS_CMD_%s *cmd = static_cast<const RS_CMD_%s *>(vp);\n", api->name, api->name);
203 fprintf(f, " ");
Joe Onorato84614dd2009-08-10 15:01:51 -0700204 if (api->ret.typeName[0]) {
Jason Samsfcd31922009-08-17 18:35:48 -0700205 fprintf(f, "con->mIO.mToCoreRet = (intptr_t)");
Jason Sams326e0dd2009-05-22 14:03:28 -0700206 }
207 fprintf(f, "rsi_%s(con", api->name);
208 for(ct2=0; ct2 < api->paramCount; ct2++) {
209 const VarType *vt = &api->params[ct2];
210 fprintf(f, ",");
211 fprintf(f, "\n cmd->%s", vt->name);
212 }
213 fprintf(f, ");\n");
214
215 fprintf(f, "};\n\n");
216 }
217
218 fprintf(f, "RsPlaybackFunc gPlaybackFuncs[] = {\n");
219 fprintf(f, " NULL,\n");
220 for(ct=0; ct < apiCount; ct++) {
221 fprintf(f, " %s%s,\n", "rsp_", apis[ct].name);
222 }
223 fprintf(f, "};\n");
224
225 fprintf(f, "};\n");
226 fprintf(f, "};\n");
227}
228
229int main(int argc, char **argv)
230{
231 if (argc != 3) {
232 fprintf(stderr, "usage: %s commandFile outFile\n", argv[0]);
233 return 1;
234 }
235 const char* rsgFile = argv[1];
236 const char* outFile = argv[2];
237 FILE* input = fopen(rsgFile, "r");
238
239 char choice = fgetc(input);
240 fclose(input);
241
242 if (choice < '0' || choice > '3') {
243 fprintf(stderr, "Uknown command: \'%c\'\n", choice);
244 return -2;
245 }
246
247 yylex();
248 // printf("# of lines = %d\n", num_lines);
249
250 FILE *f = fopen(outFile, "w");
251
252 printFileHeader(f);
253 switch(choice) {
254 case '0': // rsgApiStructs.h
255 {
256 fprintf(f, "\n");
257 fprintf(f, "#include \"rsContext.h\"\n");
258 fprintf(f, "\n");
259 fprintf(f, "namespace android {\n");
260 fprintf(f, "namespace renderscript {\n");
261 printStructures(f);
262 printFuncDecls(f, "rsi_", 1);
263 printPlaybackFuncs(f, "rsp_");
264 fprintf(f, "\n\ntypedef void (*RsPlaybackFunc)(Context *, const void *);\n");
265 fprintf(f, "extern RsPlaybackFunc gPlaybackFuncs[];\n");
266
267 fprintf(f, "}\n");
268 fprintf(f, "}\n");
269 }
270 break;
271
272 case '1': // rsgApiFuncDecl.h
273 {
274 printFuncDecls(f, "rs", 0);
275 }
276 break;
277
278 case '2': // rsgApi.cpp
279 {
280 printApiCpp(f);
281 }
282 break;
283
284 case '3': // rsgApiReplay.cpp
285 {
286 printFileHeader(f);
287 printPlaybackCpp(f);
288 }
289 break;
290 }
291 fclose(f);
292 return 0;
293}