void AstPrinterVisitor::visitBlockNode(BlockNode* node) { if (!isMainScope(node->scope())) { _output << "{" << endl; } Scope::VarIterator varIt(node->scope()); while(varIt.hasNext()) { AstVar* var = varIt.next(); printVarType(var->type()); _output << " " << var->name(); printSemicolon(); } Scope::FunctionIterator funcIt(node->scope()); while(funcIt.hasNext()) { FunctionNode* func = funcIt.next()->node(); func->visit(this); } for (uint32_t i = 0; i < node->nodes(); ++i) { node->nodeAt(i)->visit(this); if (!(node->nodeAt(i)->isIfNode() || node->nodeAt(i)->isWhileNode() || node->nodeAt(i)->isForNode() || node->nodeAt(i)->isReturnNode() || node->nodeAt(i)->isBlockNode())) { printSemicolon(); } } if (!isMainScope(node->scope())) { _output << "}" << endl; } }
void printVarTypeAndName(FILE *f, const VarType *vt) { printVarType(f, vt); if (vt->name[0]) { fprintf(f, " %s", vt->name); } }
void printApiCpp(FILE *f) { int ct; int ct2; fprintf(f, "#include \"rsDevice.h\"\n"); fprintf(f, "#include \"rsContext.h\"\n"); fprintf(f, "#include \"rsThreadIO.h\"\n"); //fprintf(f, "#include \"rsgApiStructs.h\"\n"); fprintf(f, "#include \"rsgApiFuncDecl.h\"\n"); fprintf(f, "\n"); fprintf(f, "using namespace android;\n"); fprintf(f, "using namespace android::renderscript;\n"); fprintf(f, "#include \"rsHandcode.h\"\n"); fprintf(f, "\n"); for(ct=0; ct < apiCount; ct++) { int needFlush = 0; const ApiEntry * api = &apis[ct]; printFuncDecl(f, api, "rs", 0); fprintf(f, "\n{\n"); if (api->handcodeApi) { fprintf(f, " rsHCAPI_%s(rsc", api->name); for(ct2=0; ct2 < api->paramCount; ct2++) { const VarType *vt = &api->params[ct2]; fprintf(f, ", %s", vt->name); } fprintf(f, ");\n"); } else { fprintf(f, " ThreadIO *io = &((Context *)rsc)->mIO;\n"); //fprintf(f, " LOGE(\"add command %s\\n\");\n", api->name); 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); fprintf(f, " uint32_t size = sizeof(RS_CMD_%s);\n", api->name); for(ct2=0; ct2 < api->paramCount; ct2++) { const VarType *vt = &api->params[ct2]; needFlush += vt->ptrLevel; fprintf(f, " cmd->%s = %s;\n", vt->name, vt->name); } if (api->ret.typeName[0]) { needFlush = 1; } fprintf(f, " io->mToCore.commit"); if (needFlush) { fprintf(f, "Sync"); } fprintf(f, "(RS_CMD_ID_%s, size);\n", api->name); if (api->ret.typeName[0]) { fprintf(f, " return reinterpret_cast<"); printVarType(f, &api->ret); fprintf(f, ">(io->mToCoreRet);\n"); } } fprintf(f, "};\n\n"); } }
void printArgList(FILE *f, const ApiEntry * api, int assumePrevious) { int ct; for (ct=0; ct < api->paramCount; ct++) { if (ct || assumePrevious) { fprintf(f, ", "); } printVarType(f, &api->params[ct]); } }
void printFuncDecl(FILE *f, const ApiEntry *api, const char *prefix, int addContext) { printVarType(f, &api->ret); fprintf(f, " %s%s (", prefix, api->name); if (addContext) { fprintf(f, "Context *"); } else { fprintf(f, "RsContext rsc"); } printArgList(f, api, 1); fprintf(f, ")"); }
void AstPrinterVisitor::visitFunctionNode(FunctionNode* node) { if (node->name() != "<top>") { _output << "function "; printVarType(node->returnType()); _output << " " << node->name() << "("; for (uint32_t i = 0; i < node->parametersNumber(); ++i) { printVarType(node->parameterType(i)); _output << " " << node->parameterName(i); if (i != node->parametersNumber() - 1) { _output << ", "; } } _output << ")"; } if (node->body()->nodes() > 0 && node->body()->nodeAt(0)->isNativeCallNode()) { node->body()->nodeAt(0)->visit(this); printSemicolon(); } else { _output << endl; node->body()->visit(this); } }
void printStructures(FILE *f) { int ct; int ct2; for (ct=0; ct < apiCount; ct++) { fprintf(f, "typedef struct RS_CMD_%s_rec RS_CMD_%s;\n", apis[ct].name, apis[ct].name); } fprintf(f, "\n"); for (ct=0; ct < apiCount; ct++) { const ApiEntry * api = &apis[ct]; fprintf(f, "#define RS_CMD_ID_%s %i\n", api->name, ct+1); fprintf(f, "struct RS_CMD_%s_rec {\n", api->name); //fprintf(f, " RsCommandHeader _hdr;\n"); for (ct2=0; ct2 < api->paramCount; ct2++) { fprintf(f, " "); printVarType(f, &api->params[ct2]); fprintf(f, ";\n"); } fprintf(f, "};\n\n"); } }
void printPlaybackCpp(FILE *f) { int ct; int ct2; fprintf(f, "#include \"rsDevice.h\"\n"); fprintf(f, "#include \"rsContext.h\"\n"); fprintf(f, "#include \"rsThreadIO.h\"\n"); fprintf(f, "#include \"rsgApiStructs.h\"\n"); fprintf(f, "#include \"rsgApiFuncDecl.h\"\n"); fprintf(f, "\n"); fprintf(f, "namespace android {\n"); fprintf(f, "namespace renderscript {\n"); fprintf(f, "\n"); for (ct=0; ct < apiCount; ct++) { const ApiEntry * api = &apis[ct]; int needFlush = 0; if (api->direct) { continue; } fprintf(f, "void rsp_%s(Context *con, const void *vp, size_t cmdSizeBytes) {\n", api->name); fprintf(f, " const RS_CMD_%s *cmd = static_cast<const RS_CMD_%s *>(vp);\n", api->name, api->name); if (hasInlineDataPointers(api)) { fprintf(f, " const uint8_t *baseData = 0;\n"); fprintf(f, " if (cmdSizeBytes != sizeof(RS_CMD_%s)) {\n", api->name); fprintf(f, " baseData = &((const uint8_t *)vp)[sizeof(*cmd)];\n"); fprintf(f, " }\n"); } fprintf(f, " "); if (api->ret.typeName[0]) { fprintf(f, "\n "); printVarType(f, &api->ret); fprintf(f, " ret = "); } fprintf(f, "rsi_%s(con", api->name); for (ct2=0; ct2 < api->paramCount; ct2++) { const VarType *vt = &api->params[ct2]; needFlush += vt->ptrLevel; if (hasInlineDataPointers(api) && vt->ptrLevel) { fprintf(f, ",\n (const %s *)&baseData[(intptr_t)cmd->%s]", vt->typeName, vt->name); } else { fprintf(f, ",\n cmd->%s", vt->name); } } fprintf(f, ");\n"); if (hasInlineDataPointers(api)) { fprintf(f, " size_t totalSize = 0;\n"); for (ct2=0; ct2 < api->paramCount; ct2++) { if (api->params[ct2].ptrLevel) { fprintf(f, " totalSize += cmd->%s_length;\n", api->params[ct2].name); } } fprintf(f, " if ((totalSize != 0) && (cmdSizeBytes == sizeof(RS_CMD_%s))) {\n", api->name); fprintf(f, " con->mIO.coreSetReturn(NULL, 0);\n"); fprintf(f, " }\n"); } else if (api->ret.typeName[0]) { fprintf(f, " con->mIO.coreSetReturn(&ret, sizeof(ret));\n"); } else if (api->sync || needFlush) { fprintf(f, " con->mIO.coreSetReturn(NULL, 0);\n"); } fprintf(f, "};\n\n"); } for (ct=0; ct < apiCount; ct++) { const ApiEntry * api = &apis[ct]; int needFlush = 0; fprintf(f, "void rspr_%s(Context *con, ThreadIO *io) {\n", api->name); fprintf(f, " RS_CMD_%s cmd;\n", api->name); for (ct2=0; ct2 < api->paramCount; ct2++) { const VarType *vt = &api->params[ct2]; if (vt->ptrLevel == 0) { fprintf(f, " io->coreRead(&cmd.%s, sizeof(cmd.%s));\n", vt->name, vt->name); } } fprintf(f, "\n"); for (ct2=0; ct2 < api->paramCount; ct2++) { const VarType *vt = &api->params[ct2]; if (vt->ptrLevel == 1) { fprintf(f, " cmd.%s = (", vt->name); printVarType(f, vt); fprintf(f, ")malloc(cmd.%s_length);\n", vt->name); if (vt->isConst) { fprintf(f, " if (cmd.%s_length) io->coreRead((void *)cmd.%s, cmd.%s_length);\n", vt->name, vt->name, vt->name); } } } fprintf(f, "\n"); for (ct2=0; ct2 < api->paramCount; ct2++) { const VarType *vt = &api->params[ct2]; if (vt->ptrLevel == 2) { fprintf(f, " for (size_t ct = 0; ct < (cmd.%s_length_length / sizeof(cmd.%s_length)); ct++) {\n", vt->name, vt->name); fprintf(f, " cmd.%s = (", vt->name); printVarType(f, vt); fprintf(f, ")malloc(cmd.%s_length[ct]);\n", vt->name); fprintf(f, " io->coreRead(& cmd.%s, cmd.%s_length[ct]);\n", vt->name, vt->name); fprintf(f, " }\n"); } } fprintf(f, "\n"); if (api->ret.typeName[0]) { fprintf(f, " "); printVarType(f, &api->ret); fprintf(f, " ret =\n"); } fprintf(f, " rsi_%s(", api->name); if (!api->nocontext) { fprintf(f, "con"); } for (ct2=0; ct2 < api->paramCount; ct2++) { const VarType *vt = &api->params[ct2]; if (ct2 > 0 || !api->nocontext) { fprintf(f, ",\n"); } fprintf(f, " cmd.%s", vt->name); } fprintf(f, ");\n"); for (ct2=0; ct2 < api->paramCount; ct2++) { const VarType *vt = &api->params[ct2]; if ((vt->ptrLevel == 1) && (!vt->isConst)) { fprintf(f, " io->coreSetReturn((void *)cmd.%s, cmd.%s_length);\n", vt->name, vt->name); } } for (ct2=0; ct2 < api->paramCount; ct2++) { const VarType *vt = &api->params[ct2]; if ((vt->ptrLevel == 2) && (!vt->isConst)) { fprintf(f, " for (size_t ct = 0; ct < (cmd.%s_length_length / sizeof(cmd.%s_length)); ct++) {\n", vt->name, vt->name); fprintf(f, " io->coreSetReturn((void *)cmd.%s[ct], cmd.%s_length[ct]);\n", vt->name, vt->name); fprintf(f, " }\n"); } } fprintf(f, "\n"); if (api->ret.typeName[0]) { fprintf(f, " io->coreSetReturn(&ret, sizeof(ret));\n"); } else /*if (needFlush)*/ { fprintf(f, " io->coreSetReturn(NULL, 0);\n"); } for (ct2=0; ct2 < api->paramCount; ct2++) { const VarType *vt = &api->params[ct2]; if (vt->ptrLevel == 1) { fprintf(f, " free((void *)cmd.%s);\n", vt->name); } } for (ct2=0; ct2 < api->paramCount; ct2++) { const VarType *vt = &api->params[ct2]; if (vt->ptrLevel == 2) { fprintf(f, " for (size_t ct = 0; ct < (cmd.%s_length_length / sizeof(cmd.%s_length)); ct++) {\n", vt->name, vt->name); fprintf(f, " free((void *)cmd.%s);\n", vt->name); fprintf(f, " }\n"); } } fprintf(f, "};\n\n"); } fprintf(f, "RsPlaybackLocalFunc gPlaybackFuncs[%i] = {\n", apiCount + 1); fprintf(f, " NULL,\n"); for (ct=0; ct < apiCount; ct++) { if (apis[ct].direct) { fprintf(f, " NULL,\n"); } else { fprintf(f, " %s%s,\n", "rsp_", apis[ct].name); } } fprintf(f, "};\n"); fprintf(f, "RsPlaybackRemoteFunc gPlaybackRemoteFuncs[%i] = {\n", apiCount + 1); fprintf(f, " NULL,\n"); for (ct=0; ct < apiCount; ct++) { fprintf(f, " %s%s,\n", "rspr_", apis[ct].name); } fprintf(f, "};\n"); fprintf(f, "};\n"); fprintf(f, "};\n"); }
void printApiCpp(FILE *f) { int ct; int ct2; fprintf(f, "#include \"rsDevice.h\"\n"); fprintf(f, "#include \"rsContext.h\"\n"); fprintf(f, "#include \"rsThreadIO.h\"\n"); fprintf(f, "#include \"rsgApiStructs.h\"\n"); fprintf(f, "#include \"rsgApiFuncDecl.h\"\n"); fprintf(f, "#include \"rsFifo.h\"\n"); fprintf(f, "\n"); fprintf(f, "using namespace android;\n"); fprintf(f, "using namespace android::renderscript;\n"); fprintf(f, "\n"); printFuncPointers(f, 0); // Generate RS funcs for local fifo for (ct=0; ct < apiCount; ct++) { int needFlush = 0; const ApiEntry * api = &apis[ct]; fprintf(f, "static "); printFuncDecl(f, api, "LF_", 0, 0); fprintf(f, "\n{\n"); if (api->direct) { fprintf(f, " "); if (api->ret.typeName[0]) { fprintf(f, "return "); } fprintf(f, "rsi_%s(", api->name); if (!api->nocontext) { fprintf(f, "(Context *)rsc"); } for (ct2=0; ct2 < api->paramCount; ct2++) { const VarType *vt = &api->params[ct2]; if (ct2 > 0 || !api->nocontext) { fprintf(f, ", "); } fprintf(f, "%s", vt->name); } fprintf(f, ");\n"); } else { fprintf(f, " ThreadIO *io = &((Context *)rsc)->mIO;\n"); fprintf(f, " const uint32_t size = sizeof(RS_CMD_%s);\n", api->name); if (hasInlineDataPointers(api)) { fprintf(f, " uint32_t dataSize = 0;\n"); for (ct2=0; ct2 < api->paramCount; ct2++) { const VarType *vt = &api->params[ct2]; if (vt->isConst && vt->ptrLevel) { fprintf(f, " dataSize += %s_length;\n", vt->name); } } } //fprintf(f, " ALOGE(\"add command %s\\n\");\n", api->name); if (hasInlineDataPointers(api)) { fprintf(f, " RS_CMD_%s *cmd = NULL;\n", api->name); fprintf(f, " if (dataSize < io->getMaxInlineSize()) {;\n"); fprintf(f, " cmd = static_cast<RS_CMD_%s *>(io->coreHeader(RS_CMD_ID_%s, dataSize + size));\n", api->name, api->name); fprintf(f, " } else {\n"); fprintf(f, " cmd = static_cast<RS_CMD_%s *>(io->coreHeader(RS_CMD_ID_%s, size));\n", api->name, api->name); fprintf(f, " }\n"); fprintf(f, " uint8_t *payload = (uint8_t *)&cmd[1];\n"); } else { fprintf(f, " RS_CMD_%s *cmd = static_cast<RS_CMD_%s *>(io->coreHeader(RS_CMD_ID_%s, size));\n", api->name, api->name, api->name); } for (ct2=0; ct2 < api->paramCount; ct2++) { const VarType *vt = &api->params[ct2]; needFlush += vt->ptrLevel; if (vt->ptrLevel && hasInlineDataPointers(api)) { fprintf(f, " if (dataSize < io->getMaxInlineSize()) {\n"); fprintf(f, " memcpy(payload, %s, %s_length);\n", vt->name, vt->name); fprintf(f, " cmd->%s = (", vt->name); printVarType(f, vt); fprintf(f, ")(payload - ((uint8_t *)&cmd[1]));\n"); fprintf(f, " payload += %s_length;\n", vt->name); fprintf(f, " } else {\n"); fprintf(f, " cmd->%s = %s;\n", vt->name, vt->name); fprintf(f, " }\n"); } else { fprintf(f, " cmd->%s = %s;\n", vt->name, vt->name); } } if (api->ret.typeName[0] || api->sync) { needFlush = 1; } fprintf(f, " io->coreCommit();\n"); if (hasInlineDataPointers(api)) { fprintf(f, " if (dataSize >= io->getMaxInlineSize()) {\n"); fprintf(f, " io->coreGetReturn(NULL, 0);\n"); fprintf(f, " }\n"); } else if (api->ret.typeName[0]) { fprintf(f, "\n "); printVarType(f, &api->ret); fprintf(f, " ret;\n"); fprintf(f, " io->coreGetReturn(&ret, sizeof(ret));\n"); fprintf(f, " return ret;\n"); } else if (needFlush) { fprintf(f, " io->coreGetReturn(NULL, 0);\n"); } } fprintf(f, "};\n\n"); // Generate a remote sender function const char * str = "core"; if (api->direct) { str = "async"; } fprintf(f, "static "); printFuncDecl(f, api, "RF_", 0, 0); fprintf(f, "\n{\n"); fprintf(f, " ThreadIO *io = &((Context *)rsc)->mIO;\n"); fprintf(f, " const uint32_t cmdID = RS_CMD_ID_%s;\n", api->name); fprintf(f, " io->%sWrite(&cmdID, sizeof(cmdID));\n\n", str); for (ct2=0; ct2 < api->paramCount; ct2++) { const VarType *vt = &api->params[ct2]; if (vt->ptrLevel == 0) { fprintf(f, " io->%sWrite(& %s, sizeof(%s));\n", str, vt->name, vt->name); } } fprintf(f, "\n"); for (ct2=0; ct2 < api->paramCount; ct2++) { const VarType *vt = &api->params[ct2]; if ((vt->ptrLevel == 1) && (vt->isConst)) { fprintf(f, " io->%sWrite(%s, %s_length);\n", str, vt->name, vt->name); } } fprintf(f, "\n"); for (ct2=0; ct2 < api->paramCount; ct2++) { const VarType *vt = &api->params[ct2]; if ((vt->ptrLevel == 2) && (vt->isConst)) { fprintf(f, " for (size_t ct = 0; ct < (%s_length_length / sizeof(%s_length)); ct++) {\n", vt->name, vt->name); fprintf(f, " io->%sWrite(%s[ct], %s_length[ct]);\n", str, vt->name, vt->name); fprintf(f, " }\n"); } } fprintf(f, "\n"); for (ct2=0; ct2 < api->paramCount; ct2++) { const VarType *vt = &api->params[ct2]; if ((vt->ptrLevel == 1) && (!vt->isConst)) { fprintf(f, " io->%sGetReturn(%s, %s_length);\n", str, vt->name, vt->name); } } fprintf(f, "\n"); for (ct2=0; ct2 < api->paramCount; ct2++) { const VarType *vt = &api->params[ct2]; if ((vt->ptrLevel == 2) && (!vt->isConst)) { fprintf(f, " for (size_t ct = 0; ct < (%s_length_length / sizeof(%s_length)); ct++) {\n", vt->name, vt->name); fprintf(f, " io->%sGetReturn(%s[ct], %s_length[ct]);\n", str, vt->name, vt->name); fprintf(f, " }\n"); } } fprintf(f, "\n"); if (api->ret.typeName[0]) { fprintf(f, " "); printVarType(f, &api->ret); fprintf(f, " retValue;\n"); fprintf(f, " io->%sGetReturn(&retValue, sizeof(retValue));\n", str); fprintf(f, " return retValue;\n"); } else /*if (api->sync)*/ { fprintf(f, " io->%sGetReturn(NULL, 0);\n", str); } fprintf(f, "}\n\n"); } fprintf(f, "\n"); fprintf(f, "static RsApiEntrypoints_t s_LocalTable = {\n"); for (ct=0; ct < apiCount; ct++) { fprintf(f, " LF_%s,\n", apis[ct].name); } fprintf(f, "};\n"); fprintf(f, "\n"); fprintf(f, "static RsApiEntrypoints_t s_RemoteTable = {\n"); for (ct=0; ct < apiCount; ct++) { fprintf(f, " RF_%s,\n", apis[ct].name); } fprintf(f, "};\n"); fprintf(f, "static RsApiEntrypoints_t *s_CurrentTable = &s_LocalTable;\n\n"); for (ct=0; ct < apiCount; ct++) { int needFlush = 0; const ApiEntry * api = &apis[ct]; printFuncDecl(f, api, "rs", 0, 0); fprintf(f, "\n{\n"); fprintf(f, " "); if (api->ret.typeName[0]) { fprintf(f, "return "); } fprintf(f, "s_CurrentTable->%s(", api->name); if (!api->nocontext) { fprintf(f, "(Context *)rsc"); } for (ct2=0; ct2 < api->paramCount; ct2++) { const VarType *vt = &api->params[ct2]; if (ct2 > 0 || !api->nocontext) { fprintf(f, ", "); } fprintf(f, "%s", vt->name); } fprintf(f, ");\n"); fprintf(f, "}\n\n"); } }