Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1 | |
Joe Onorato | daed524 | 2009-08-10 15:01:51 -0700 | [diff] [blame] | 2 | #include "spec.h" |
| 3 | #include <stdio.h> |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 4 | |
| 5 | void printFileHeader(FILE *f) |
| 6 | { |
| 7 | fprintf(f, "/*\n"); |
Jason Sams | 7eecbf2 | 2010-02-23 15:27:51 -0800 | [diff] [blame] | 8 | fprintf(f, " * Copyright (C) 2010 The Android Open Source Project\n"); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 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 | daed524 | 2009-08-10 15:01:51 -0700 | [diff] [blame] | 48 | fprintf(f, "%s", vt->typeName); |
Jason Sams | d19f10d | 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 *"); |
Jason Sams | bc948de | 2009-08-17 18:35:48 -0700 | [diff] [blame] | 106 | } else { |
| 107 | fprintf(f, "RsContext rsc"); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 108 | } |
Jason Sams | bc948de | 2009-08-17 18:35:48 -0700 | [diff] [blame] | 109 | printArgList(f, api, 1); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 110 | fprintf(f, ")"); |
| 111 | } |
| 112 | |
| 113 | void 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 | |
| 123 | void 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 | |
| 131 | void 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"); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 144 | fprintf(f, "#include \"rsHandcode.h\"\n"); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 145 | fprintf(f, "\n"); |
| 146 | |
| 147 | for(ct=0; ct < apiCount; ct++) { |
| 148 | int needFlush = 0; |
| 149 | const ApiEntry * api = &apis[ct]; |
| 150 | |
| 151 | printFuncDecl(f, api, "rs", 0); |
| 152 | fprintf(f, "\n{\n"); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 153 | if (api->handcodeApi) { |
| 154 | fprintf(f, " rsHCAPI_%s(rsc", api->name); |
| 155 | for(ct2=0; ct2 < api->paramCount; ct2++) { |
| 156 | const VarType *vt = &api->params[ct2]; |
| 157 | fprintf(f, ", %s", vt->name); |
| 158 | } |
| 159 | fprintf(f, ");\n"); |
| 160 | } else { |
| 161 | fprintf(f, " ThreadIO *io = &((Context *)rsc)->mIO;\n"); |
| 162 | //fprintf(f, " LOGE(\"add command %s\\n\");\n", api->name); |
| 163 | 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); |
| 164 | fprintf(f, " uint32_t size = sizeof(RS_CMD_%s);\n", api->name); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 165 | |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 166 | for(ct2=0; ct2 < api->paramCount; ct2++) { |
| 167 | const VarType *vt = &api->params[ct2]; |
| 168 | needFlush += vt->ptrLevel; |
| 169 | fprintf(f, " cmd->%s = %s;\n", vt->name, vt->name); |
| 170 | } |
| 171 | if (api->ret.typeName[0]) { |
| 172 | needFlush = 1; |
| 173 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 174 | |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 175 | fprintf(f, " io->mToCore.commit"); |
| 176 | if (needFlush) { |
| 177 | fprintf(f, "Sync"); |
| 178 | } |
| 179 | fprintf(f, "(RS_CMD_ID_%s, size);\n", api->name); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 180 | |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 181 | if (api->ret.typeName[0]) { |
| 182 | fprintf(f, " return reinterpret_cast<"); |
| 183 | printVarType(f, &api->ret); |
| 184 | fprintf(f, ">(io->mToCoreRet);\n"); |
| 185 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 186 | } |
| 187 | fprintf(f, "};\n\n"); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | void printPlaybackCpp(FILE *f) |
| 192 | { |
| 193 | int ct; |
| 194 | int ct2; |
| 195 | |
| 196 | fprintf(f, "#include \"rsDevice.h\"\n"); |
| 197 | fprintf(f, "#include \"rsContext.h\"\n"); |
| 198 | fprintf(f, "#include \"rsThreadIO.h\"\n"); |
| 199 | //fprintf(f, "#include \"rsgApiStructs.h\"\n"); |
| 200 | fprintf(f, "#include \"rsgApiFuncDecl.h\"\n"); |
| 201 | fprintf(f, "\n"); |
| 202 | fprintf(f, "namespace android {\n"); |
| 203 | fprintf(f, "namespace renderscript {\n"); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 204 | fprintf(f, "#include \"rsHandcode.h\"\n"); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 205 | fprintf(f, "\n"); |
| 206 | |
| 207 | for(ct=0; ct < apiCount; ct++) { |
| 208 | const ApiEntry * api = &apis[ct]; |
| 209 | |
| 210 | fprintf(f, "void rsp_%s(Context *con, const void *vp)\n", api->name); |
| 211 | fprintf(f, "{\n"); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 212 | if (api->handcodePlay) { |
| 213 | fprintf(f, " rsHCPLAY_%s(con, vp);\n", api->name); |
| 214 | } else { |
| 215 | //fprintf(f, " LOGE(\"play command %s\\n\");\n", api->name); |
| 216 | fprintf(f, " const RS_CMD_%s *cmd = static_cast<const RS_CMD_%s *>(vp);\n", api->name, api->name); |
| 217 | fprintf(f, " "); |
| 218 | if (api->ret.typeName[0]) { |
| 219 | fprintf(f, "con->mIO.mToCoreRet = (intptr_t)"); |
| 220 | } |
| 221 | fprintf(f, "rsi_%s(con", api->name); |
| 222 | for(ct2=0; ct2 < api->paramCount; ct2++) { |
| 223 | const VarType *vt = &api->params[ct2]; |
| 224 | fprintf(f, ",\n cmd->%s", vt->name); |
| 225 | } |
| 226 | fprintf(f, ");\n"); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 227 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 228 | fprintf(f, "};\n\n"); |
| 229 | } |
| 230 | |
| 231 | fprintf(f, "RsPlaybackFunc gPlaybackFuncs[] = {\n"); |
| 232 | fprintf(f, " NULL,\n"); |
| 233 | for(ct=0; ct < apiCount; ct++) { |
| 234 | fprintf(f, " %s%s,\n", "rsp_", apis[ct].name); |
| 235 | } |
| 236 | fprintf(f, "};\n"); |
| 237 | |
| 238 | fprintf(f, "};\n"); |
| 239 | fprintf(f, "};\n"); |
| 240 | } |
| 241 | |
| 242 | int main(int argc, char **argv) |
| 243 | { |
| 244 | if (argc != 3) { |
| 245 | fprintf(stderr, "usage: %s commandFile outFile\n", argv[0]); |
| 246 | return 1; |
| 247 | } |
| 248 | const char* rsgFile = argv[1]; |
| 249 | const char* outFile = argv[2]; |
| 250 | FILE* input = fopen(rsgFile, "r"); |
| 251 | |
| 252 | char choice = fgetc(input); |
| 253 | fclose(input); |
| 254 | |
| 255 | if (choice < '0' || choice > '3') { |
| 256 | fprintf(stderr, "Uknown command: \'%c\'\n", choice); |
| 257 | return -2; |
| 258 | } |
| 259 | |
| 260 | yylex(); |
| 261 | // printf("# of lines = %d\n", num_lines); |
| 262 | |
| 263 | FILE *f = fopen(outFile, "w"); |
| 264 | |
| 265 | printFileHeader(f); |
| 266 | switch(choice) { |
| 267 | case '0': // rsgApiStructs.h |
| 268 | { |
| 269 | fprintf(f, "\n"); |
| 270 | fprintf(f, "#include \"rsContext.h\"\n"); |
| 271 | fprintf(f, "\n"); |
| 272 | fprintf(f, "namespace android {\n"); |
| 273 | fprintf(f, "namespace renderscript {\n"); |
| 274 | printStructures(f); |
| 275 | printFuncDecls(f, "rsi_", 1); |
| 276 | printPlaybackFuncs(f, "rsp_"); |
| 277 | fprintf(f, "\n\ntypedef void (*RsPlaybackFunc)(Context *, const void *);\n"); |
| 278 | fprintf(f, "extern RsPlaybackFunc gPlaybackFuncs[];\n"); |
| 279 | |
| 280 | fprintf(f, "}\n"); |
| 281 | fprintf(f, "}\n"); |
| 282 | } |
| 283 | break; |
| 284 | |
| 285 | case '1': // rsgApiFuncDecl.h |
| 286 | { |
| 287 | printFuncDecls(f, "rs", 0); |
| 288 | } |
| 289 | break; |
| 290 | |
| 291 | case '2': // rsgApi.cpp |
| 292 | { |
| 293 | printApiCpp(f); |
| 294 | } |
| 295 | break; |
| 296 | |
| 297 | case '3': // rsgApiReplay.cpp |
| 298 | { |
| 299 | printFileHeader(f); |
| 300 | printPlaybackCpp(f); |
| 301 | } |
| 302 | break; |
| 303 | } |
| 304 | fclose(f); |
| 305 | return 0; |
| 306 | } |