📄 mal_compiler.c
字号:
#line 128 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/compiler/mal_compiler.mx"#include "mal_config.h"#include "mal_compiler.h"#include "mal_interpreter.h"#include "mal_function.h"static char *mccPrelude[] = { "/* MAL to C compiler\n", " Copyright (c) 2001-2007, CWI.\n", " All rights reserved.\n", " \n", " The current version does not yet\n", " support THREAD blocks\n", "*/\n", "#include \"mal.h\"\n", "#include \"mal_function.h\"\n", "#define BAT int\n", /* the interface works on BAT ids */ 0};voidmccVar(stream *f, MalBlkPtr mb, int i){ if (isTmpVar(mb, i)) stream_printf(f, "T%d", mb->var[i]->tmpindex); else stream_printf(f, "%s", mb->var[i]->name);}voidmccArg(stream *f, MalBlkPtr mb, int i){ stream_printf(f, "&"); mccVar(f, mb, i);}voidmccValue(stream *f, MalBlkPtr mb, int i){ int (*tostr) (str *, int *, ptr); str buf = 0, c; int sz = 0; ValPtr val; val = &getVarConstant(mb, i); if (val->vtype == TYPE_str) { stream_printf(f, "\"%s\"", val->val.sval); } else { tostr = BATatoms[val->vtype].atomToStr; (*tostr) (&buf, &sz, VALptr(val)); c = strchr(buf, '@'); if (c && *(c + 1) == '0') *c = 0; stream_printf(f, "%s", buf); GDKfree(buf); }}voidmccType(stream *f, MalBlkPtr mb, int i){ str tpe; tpe = getTypeName(getVarType(mb, i)); if (strcmp(tpe, "void") == 0) { stream_printf(f, "void *"); } else if (isaBatType(getVarType(mb, i))) { stream_printf(f, "BAT *"); } else { stream_printf(f, "%s *", tpe); } GDKfree(tpe);}voidmccInit(stream *f, MalBlkPtr mb){ int i, j; InstrPtr p; for (i = 0; mccPrelude[i]; i++) stream_printf(f, "%s", mccPrelude[i]); p = getInstrPtr(mb, 0); stream_printf(f, "str MCC%s_%s(", getModuleId(p), getFunctionId(p)); if (p->argc > 0) { mccType(f, mb, i); mccVar(f, mb, getArg(p, 0)); for (j = 1; j < p->argc; j++) { stream_printf(f, ","); mccType(f, mb, i); mccVar(f, mb, getArg(p, j)); } } stream_printf(f, ")\n{\n");}voidmccVariables(stream *f, MalBlkPtr mb){ int i; str tpe, v; for (i = 1; i < mb->vtop; i++) { tpe = getTypeName(getVarType(mb, i)); v = getVarName(mb, i); if (isTmpVar(mb, i)) *v = 'V'; if (getVarType(mb, i) == TYPE_void) { stream_printf(f, "\tvoid *"); mccVar(f, mb, i); stream_printf(f, "= 0; /* %s */\n", tpe); } else if (isaBatType(getVarType(mb, i))) { stream_printf(f, "\tBAT *"); mccVar(f, mb, i); stream_printf(f, "= 0; /* %s */\n", tpe); } else { stream_printf(f, "\t%s ", tpe); mccVar(f, mb, i); stream_printf(f, " = "); mccValue(f, mb, i); stream_printf(f, ";\n"); } GDKfree(tpe); } /* if( isTmpVar(mb,i) ) GDKfree(v); */ stream_printf(f, "\tstr Xmsg = MAL_SUCCEED;\n");}voidmccEntry(stream *f, MalBlkPtr mb, int i){ switch (getVarType(mb, i)) { case TYPE_void: stream_printf(f, "1"); break; case TYPE_bit: mccVar(f, mb, i); stream_printf(f, " == 0 ||"); mccVar(f, mb, i); stream_printf(f, "== bit_nil"); break; case TYPE_chr: mccVar(f, mb, i); stream_printf(f, "== chr_nil"); break; case TYPE_sht: mccVar(f, mb, i); stream_printf(f, " < 0 ||"); mccVar(f, mb, i); stream_printf(f, "== sht_nil"); break; case TYPE_int: mccVar(f, mb, i); stream_printf(f, " < 0 ||"); mccVar(f, mb, i); stream_printf(f, "== int_nil"); break; case TYPE_lng: mccVar(f, mb, i); stream_printf(f, " < 0 ||"); mccVar(f, mb, i); stream_printf(f, "== lng_nil"); break; case TYPE_flt: case TYPE_dbl: mccVar(f, mb, i); stream_printf(f, " < 0 ||"); mccVar(f, mb, i); stream_printf(f, "== dbl_nil"); break; case TYPE_oid: mccVar(f, mb, i); stream_printf(f, "== oid_nil"); break; case TYPE_str: stream_printf(f, " strlen("); mccVar(f, mb, i); stream_printf(f, ") == 0 ||"); mccVar(f, mb, i); stream_printf(f, "== str_nil"); break; default: stream_printf(f, "/* Unknown barrier :%d */", getVarType(mb, i)); }}#line 318 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/compiler/mal_compiler.mx"intmccInstruction(stream *f, MalBlkPtr mb, InstrPtr p, int i, int *catch, int *ctop){ int errors = 0; int j; if (p->barrier == EXITsymbol) stream_printf(f, "EXIT_%d: ;\n", i); if (p->barrier == CATCHsymbol) { stream_printf(f, "CATCH_%d:\n", i); stream_printf(f, "if( "); mccVar(f, mb, getArg(p, 0)); stream_printf(f, " == MAL_SUCCEED) goto EXIT_%d;\n", p->jump); return errors; } if (p->blk && p->blk->binding) { stream_printf(f, "\tif( Xmsg = "); stream_printf(f, "%s(", p->blk->binding); mccArg(f, mb, getArg(p, 0)); for (j = 1; j < p->argc; j++) { stream_printf(f, ","); mccArg(f, mb, getArg(p, j)); } stream_printf(f, ") )"); if (*ctop > 0) stream_printf(f, " goto CATCH_%d;\n", catch[--*ctop]); else stream_printf(f, " return Xmsg;\n"); } if (p->barrier) switch (p->barrier) { case BARRIERsymbol: stream_printf(f, "\tif( "); mccEntry(f, mb, getArg(p, 0)); stream_printf(f, " ) goto EXIT_%d;\n", p->jump); stream_printf(f, "BARRIER_%d:\n", i + 1); break; case RETURNsymbol: for (j = 0; j < p->retc; j++) { stream_printf(f, "\tVALcopy(&"); mccVar(f, mb, getArg(getInstrPtr(mb, 0), j)); stream_printf(f, ", &"); mccVar(f, mb, getArg(p, j)); stream_printf(f, ");\n"); } stream_printf(f, "\treturn Xmsg;\n"); case CATCHsymbol: case EXITsymbol: break; case LEAVEsymbol: case REDOsymbol: stream_printf(f, "\tif( !( "); mccEntry(f, mb, getArg(p, 0)); stream_printf(f, ") ) goto BARRIER_%d;\n", p->jump); break; default: stream_printf(f, "/* case not yet covered: %d */\n", p->barrier); } return errors;}#line 384 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/compiler/mal_compiler.mx"voidmccMultiplex(stream *f, MalBlkPtr mb, InstrPtr p){ int pc = getPC(mb, p); InstrPtr pn; pn = copyInstruction(p); if (getFunctionId(pn)) GDKfree(pn->fcnname); if (getModuleId(pn)) setModuleId(p,NULL); setModuleId(pn, NULL); setFunctionId(pn, GDKstrdup(getVarConstant(mb, pn->argv[pn->retc]).val.sval)); delArgument(pn, pn->retc); typeChecker(MCgetClient()->nspace, mb, pn, TRUE); stream_printf(f, "{\tlng mloop;\n\tptr h,t;\n"); stream_printf(f, "init todo\n"); stream_printf(f, "\tif(mloop>0)\n\tdo{\n"); stream_printf(f, "TODO\n"); mccInstruction(f, mb, pn, pc, 0, 0); stream_printf(f, "\t} while(mloop > 0 );\n"); stream_printf(f, "}\n"); freeInstruction(pn);}voidmccBody(stream *f, MalBlkPtr mb){ int i; InstrPtr p; int *catch, ctop = 0, errors = 0; catch = (int *) alloca(mb->stop); for (i = 1; i < mb->stop; i++) { p = getInstrPtr(mb, i); if (p->barrier == CATCHsymbol) catch[ctop++] = i; } for (i = 1; i < mb->stop; i++) { p = getInstrPtr(mb, i);#line 436 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/compiler/mal_compiler.mx" if (p->token != CMDcall && getFunctionId(p)) { if (getModuleId(p) && getFunctionId(p) && idcmp(getModuleId(p), "multiplex") == 0 && idcmp(getFunctionId(p), "script") == 0) { mccMultiplex(f, mb, p); continue; } GDKwarning("call to %s.%s can not be handled correctly\n", getModuleId(p), getFunctionId(p)); errors++; } errors += mccInstruction(f, mb, p, i, catch, &ctop); } if (errors) showErrors();}voidmccExit(stream *f){ stream_printf(f, "}\n"); (void) stream_close(f);}#line 464 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/compiler/mal_compiler.mx"strmccGenerate(MalBlkPtr mb){ char buf[1024]; stream *f; snprintf(buf, 1024, "%s/MCC%s_%s.c", monet_cwd, getModuleId(getInstrPtr(mb, 0)), getFunctionId(getInstrPtr(mb, 0))); f = open_wastream(buf); if (f == NULL) throw(IO, "optimizer.MCcompiler", "Could not access file"); mccInit(f, mb); mccVariables(f, mb); mccBody(f, mb); mccExit(f); return MAL_SUCCEED;}#line 488 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/compiler/mal_compiler.mx"strMCdynamicCompiler(MalBlkPtr mb, MalStkPtr stk, InstrPtr p){ Module s; Symbol t; int j; str nme, fcn, msg = MAL_SUCCEED; (void) mb; printf("Calling the dynamic compiler\n"); nme = *(str *) getArgReference(stk, p, 1); fcn = *(str *) getArgReference(stk, p, 2);#ifdef DEBUG_MAL_COMPILER printf("MCdynamicCompiler: %s.%s\n", nme, fcn);#endif s = findModule(mal_scope, nme); if (s == 0) s = mal_scope; for (; s; s = s->outer) if (strcmp(s->name, nme) == 0 && s->subscope) { j = getSubScope(fcn); for (t = s->subscope[j]; t != NULL; t = t->peer) if (t->def->errors == 0) { if (getSignature(t)->token == FUNCTIONsymbol && idcmp(fcn, getFunctionId(getSignature(t))) == 0) /* call macro expansion */ mccGenerate(t->def); if (msg) return msg; } } return msg;}#line 525 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/compiler/mal_compiler.mx"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -