Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1 | |
Joe Onorato | 84614dd | 2009-08-10 15:01:51 -0700 | [diff] [blame] | 2 | #include "spec.h" |
| 3 | #include <stdio.h> |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 4 | |
| 5 | void 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 | |
| 24 | void 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 Onorato | 84614dd | 2009-08-10 15:01:51 -0700 | [diff] [blame] | 48 | fprintf(f, "%s", vt->typeName); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 49 | 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 | |
| 64 | void 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 | |
| 75 | void 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 | |
| 100 | void 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 *"); |
| 106 | } |
| 107 | printArgList(f, api, addContext); |
| 108 | fprintf(f, ")"); |
| 109 | } |
| 110 | |
| 111 | void printFuncDecls(FILE *f, const char *prefix, int addContext) |
| 112 | { |
| 113 | int ct; |
| 114 | for(ct=0; ct < apiCount; ct++) { |
| 115 | printFuncDecl(f, &apis[ct], prefix, addContext); |
| 116 | fprintf(f, ";\n"); |
| 117 | } |
| 118 | fprintf(f, "\n\n"); |
| 119 | } |
| 120 | |
| 121 | void printPlaybackFuncs(FILE *f, const char *prefix) |
| 122 | { |
| 123 | int ct; |
| 124 | for(ct=0; ct < apiCount; ct++) { |
| 125 | fprintf(f, "void %s%s (Context *, const void *);\n", prefix, apis[ct].name); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void printApiCpp(FILE *f) |
| 130 | { |
| 131 | int ct; |
| 132 | int ct2; |
| 133 | |
| 134 | fprintf(f, "#include \"rsDevice.h\"\n"); |
| 135 | fprintf(f, "#include \"rsContext.h\"\n"); |
| 136 | fprintf(f, "#include \"rsThreadIO.h\"\n"); |
| 137 | //fprintf(f, "#include \"rsgApiStructs.h\"\n"); |
| 138 | fprintf(f, "#include \"rsgApiFuncDecl.h\"\n"); |
| 139 | fprintf(f, "\n"); |
| 140 | fprintf(f, "using namespace android;\n"); |
| 141 | fprintf(f, "using namespace android::renderscript;\n"); |
| 142 | fprintf(f, "\n"); |
| 143 | |
| 144 | for(ct=0; ct < apiCount; ct++) { |
| 145 | int needFlush = 0; |
| 146 | const ApiEntry * api = &apis[ct]; |
| 147 | |
| 148 | printFuncDecl(f, api, "rs", 0); |
| 149 | fprintf(f, "\n{\n"); |
| 150 | fprintf(f, " ThreadIO *io = gIO;\n"); |
| 151 | //fprintf(f, " LOGE(\"add command %s\\n\");\n", api->name); |
| 152 | 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); |
| 153 | fprintf(f, " uint32_t size = sizeof(RS_CMD_%s);\n", api->name); |
| 154 | |
| 155 | for(ct2=0; ct2 < api->paramCount; ct2++) { |
| 156 | const VarType *vt = &api->params[ct2]; |
| 157 | needFlush += vt->ptrLevel; |
| 158 | fprintf(f, " cmd->%s = %s;\n", vt->name, vt->name); |
| 159 | } |
Joe Onorato | 84614dd | 2009-08-10 15:01:51 -0700 | [diff] [blame] | 160 | if (api->ret.typeName[0]) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 161 | needFlush = 1; |
| 162 | } |
| 163 | |
| 164 | fprintf(f, " io->mToCore.commit"); |
| 165 | if (needFlush) { |
| 166 | fprintf(f, "Sync"); |
| 167 | } |
| 168 | fprintf(f, "(RS_CMD_ID_%s, size);\n", api->name); |
| 169 | |
Joe Onorato | 84614dd | 2009-08-10 15:01:51 -0700 | [diff] [blame] | 170 | if (api->ret.typeName[0]) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 171 | fprintf(f, " return reinterpret_cast<"); |
| 172 | printVarType(f, &api->ret); |
| 173 | fprintf(f, ">(io->mToCoreRet);\n"); |
| 174 | } |
| 175 | fprintf(f, "};\n\n"); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | void printPlaybackCpp(FILE *f) |
| 180 | { |
| 181 | int ct; |
| 182 | int ct2; |
| 183 | |
| 184 | fprintf(f, "#include \"rsDevice.h\"\n"); |
| 185 | fprintf(f, "#include \"rsContext.h\"\n"); |
| 186 | fprintf(f, "#include \"rsThreadIO.h\"\n"); |
| 187 | //fprintf(f, "#include \"rsgApiStructs.h\"\n"); |
| 188 | fprintf(f, "#include \"rsgApiFuncDecl.h\"\n"); |
| 189 | fprintf(f, "\n"); |
| 190 | fprintf(f, "namespace android {\n"); |
| 191 | fprintf(f, "namespace renderscript {\n"); |
| 192 | fprintf(f, "\n"); |
| 193 | |
| 194 | for(ct=0; ct < apiCount; ct++) { |
| 195 | const ApiEntry * api = &apis[ct]; |
| 196 | |
| 197 | fprintf(f, "void rsp_%s(Context *con, const void *vp)\n", api->name); |
| 198 | fprintf(f, "{\n"); |
| 199 | //fprintf(f, " LOGE(\"play command %s\\n\");\n", api->name); |
| 200 | fprintf(f, " const RS_CMD_%s *cmd = static_cast<const RS_CMD_%s *>(vp);\n", api->name, api->name); |
| 201 | fprintf(f, " "); |
Joe Onorato | 84614dd | 2009-08-10 15:01:51 -0700 | [diff] [blame] | 202 | if (api->ret.typeName[0]) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 203 | fprintf(f, "gIO->mToCoreRet = (intptr_t)"); |
| 204 | } |
| 205 | fprintf(f, "rsi_%s(con", api->name); |
| 206 | for(ct2=0; ct2 < api->paramCount; ct2++) { |
| 207 | const VarType *vt = &api->params[ct2]; |
| 208 | fprintf(f, ","); |
| 209 | fprintf(f, "\n cmd->%s", vt->name); |
| 210 | } |
| 211 | fprintf(f, ");\n"); |
| 212 | |
| 213 | fprintf(f, "};\n\n"); |
| 214 | } |
| 215 | |
| 216 | fprintf(f, "RsPlaybackFunc gPlaybackFuncs[] = {\n"); |
| 217 | fprintf(f, " NULL,\n"); |
| 218 | for(ct=0; ct < apiCount; ct++) { |
| 219 | fprintf(f, " %s%s,\n", "rsp_", apis[ct].name); |
| 220 | } |
| 221 | fprintf(f, "};\n"); |
| 222 | |
| 223 | fprintf(f, "};\n"); |
| 224 | fprintf(f, "};\n"); |
| 225 | } |
| 226 | |
| 227 | int main(int argc, char **argv) |
| 228 | { |
| 229 | if (argc != 3) { |
| 230 | fprintf(stderr, "usage: %s commandFile outFile\n", argv[0]); |
| 231 | return 1; |
| 232 | } |
| 233 | const char* rsgFile = argv[1]; |
| 234 | const char* outFile = argv[2]; |
| 235 | FILE* input = fopen(rsgFile, "r"); |
| 236 | |
| 237 | char choice = fgetc(input); |
| 238 | fclose(input); |
| 239 | |
| 240 | if (choice < '0' || choice > '3') { |
| 241 | fprintf(stderr, "Uknown command: \'%c\'\n", choice); |
| 242 | return -2; |
| 243 | } |
| 244 | |
| 245 | yylex(); |
| 246 | // printf("# of lines = %d\n", num_lines); |
| 247 | |
| 248 | FILE *f = fopen(outFile, "w"); |
| 249 | |
| 250 | printFileHeader(f); |
| 251 | switch(choice) { |
| 252 | case '0': // rsgApiStructs.h |
| 253 | { |
| 254 | fprintf(f, "\n"); |
| 255 | fprintf(f, "#include \"rsContext.h\"\n"); |
| 256 | fprintf(f, "\n"); |
| 257 | fprintf(f, "namespace android {\n"); |
| 258 | fprintf(f, "namespace renderscript {\n"); |
| 259 | printStructures(f); |
| 260 | printFuncDecls(f, "rsi_", 1); |
| 261 | printPlaybackFuncs(f, "rsp_"); |
| 262 | fprintf(f, "\n\ntypedef void (*RsPlaybackFunc)(Context *, const void *);\n"); |
| 263 | fprintf(f, "extern RsPlaybackFunc gPlaybackFuncs[];\n"); |
| 264 | |
| 265 | fprintf(f, "}\n"); |
| 266 | fprintf(f, "}\n"); |
| 267 | } |
| 268 | break; |
| 269 | |
| 270 | case '1': // rsgApiFuncDecl.h |
| 271 | { |
| 272 | printFuncDecls(f, "rs", 0); |
| 273 | } |
| 274 | break; |
| 275 | |
| 276 | case '2': // rsgApi.cpp |
| 277 | { |
| 278 | printApiCpp(f); |
| 279 | } |
| 280 | break; |
| 281 | |
| 282 | case '3': // rsgApiReplay.cpp |
| 283 | { |
| 284 | printFileHeader(f); |
| 285 | printPlaybackCpp(f); |
| 286 | } |
| 287 | break; |
| 288 | } |
| 289 | fclose(f); |
| 290 | return 0; |
| 291 | } |