⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mal_instruction.mx

📁 一个内存数据库的源代码这是服务器端还有客户端
💻 MX
📖 第 1 页 / 共 5 页
字号:
mal_export void freeVariable(MalBlkPtr mb, int varid);mal_export void clearVariable(MalBlkPtr mb, int varid);mal_export int cpyConstant(MalBlkPtr mb, VarPtr vr);mal_export int defConstant(MalBlkPtr mb, int type, ValPtr cst);mal_export int fndConstant(MalBlkPtr mb, ValPtr cst);mal_export int convertConstant(malType type, ValPtr vr);mal_export void pushInstruction(MalBlkPtr mb, InstrPtr p);mal_export InstrPtr pushArgument(MalBlkPtr mb, InstrPtr p, int varid);mal_export InstrPtr setArgument(MalBlkPtr mb, InstrPtr p, int idx, int varid);mal_export InstrPtr pushReturn(MalBlkPtr mb, InstrPtr p, int varid);mal_export InstrPtr pushArgumentId(MalBlkPtr mb, InstrPtr p, str name);mal_export void delArgument(InstrPtr p, int varid);mal_export void setVarType(MalBlkPtr mb, int i, int tpe);mal_export void clrAllTypes(MalBlkPtr mb);mal_export void setArgType(MalBlkPtr mb, InstrPtr p, int i, int tpe);mal_export void setReturnArgument(InstrPtr p, int varid);mal_export malType destinationType(MalBlkPtr mb, InstrPtr p);mal_export void setPolymorphic(InstrPtr p, int tpe, int force);mal_export void setVarProperty(MalBlkPtr mb, int i, str name, str op, ValPtr cst);mal_export str fcnClass(InstrPtr p);mal_export str fcnDefinition(MalBlkPtr mb, InstrPtr p, str s, int flg);mal_export void printInstruction(stream *fd, MalBlkPtr mb, InstrPtr p, int flg);mal_export str recognizedCall(MalBlkPtr mb, InstrPtr p, str s);mal_export void promptInstruction(stream *fd, MalBlkPtr mb, InstrPtr p, int flg);mal_export str instruction2str(MalBlkPtr mb, InstrPtr p, int hidden);mal_export str function2str(MalBlkPtr mb, int flg);mal_export void pushEndInstruction(MalBlkPtr mb);	/* used in src/mal/mal_parser.c *//* Utility macros to inspect an instruction */#define functionStart(X) ((X)->token == FUNCTIONsymbol || \              (X)->token == COMMANDsymbol || \              (X)->token == FACTORYsymbol )#define functionExit(X)  ((X)->token == ENDsymbol)#define blockStart(X)   ((X)->barrier && (((X)->barrier == BARRIERsymbol || \             (X)->barrier == CATCHsymbol )))#define blockExit(X) (X)->barrier == EXITsymbol#define blockCntrl(X) ( (X)->barrier== LEAVEsymbol ||  \             (X)->barrier== REDOsymbol || (X)->barrier== RETURNsymbol )mal_export void strBeforeCall(ValPtr v, ValPtr bak);mal_export void strAfterCall(ValPtr v, ValPtr bak);mal_export void batBeforeCall(ValPtr v, ValPtr bak);mal_export void batAfterCall(ValPtr v, ValPtr bak);#endif /*  _MAL_INSTR_H */@c#include "mal_config.h"#include "mal_instruction.h"#include "mal_function.h"	/* for getPC() */#include "mal_utils.h"#include "mal_exception.h"SymbolnewSymbol(str nme, int kind){	Symbol cur;	if (nme == NULL) {		GDKfatal("@1:unexpected name (=null)\n");	}	cur = (Symbol) GDKzalloc(sizeof(SymRecord));	if (cur == NULL) {		GDKfatal("@1: cannot initialize scope\n");	}	cur->name = nme;	cur->kind = kind;	cur->peer = NULL;	cur->def = newMalBlk(MAXVARS, STMT_INCREMENT);	return cur;}voidfreeSymbol(Symbol s){	if (s == NULL)		return;	if (s->def) {		freeMalBlk(s->def);		s->def = NULL;	}	GDKfree(s);}voidfreeSymbolList(Symbol s){	Symbol t = s;	while (s) {		t = s->peer;		s->peer = NULL;		freeSymbol(s);		s = t;	}}voidnewMalBlkStmt(MalBlkPtr mb, int maxstmts){	InstrPtr *p;	p = (InstrPtr *) GDKzalloc(sizeof(InstrPtr) * maxstmts);	if (p == NULL)		GDKfatal("newMalBlk:could not get instruction storage\n");	mb->stmt = p;	mb->stop = 0;	mb->ssize = maxstmts;}MalBlkPtrnewMalBlk(int maxvars, int maxstmts){	MalBlkPtr mb;	VarPtr *v;	v = (VarPtr *) GDKzalloc(sizeof(VarPtr) * maxvars);	if (v == NULL)		GDKfatal("newMalBlk:could not get variable storage\n");	mb = (MalBlkPtr) GDKmalloc(sizeof(MalBlkRecord));	if (mb == NULL)		GDKfatal("newMalBlk:could not get storage\n");	newMalBlkStmt(mb, maxstmts);	mb->var = v;	mb->vtop = 0;	mb->vsize = maxvars;	mb->help = mb->binding = NULL;	mb->errors = 0;	mb->props = NULL;	mb->alternative = NULL;	mb->history = NULL;	mb->typefixed = 0;	mb->flowfixed = 0;	mb->profiler = NULL;	return mb;}@-The resetMalBlk code removes instructions, but without freeingthe space. This way the structure is prepared for re-use@cvoidresetMalBlk(MalBlkPtr mb, int stop){	mb->stop = stop;}@-The freeMalBlk code is quite defensive. It is used to localize anillegal re-use of a MAL blk.@cvoidfreeMalBlk(MalBlkPtr mb){	int i;	for (i = 0; i < mb->stop; i++) {		freeInstruction(getInstrPtr(mb, i));		mb->stmt[i] = NULL;	}	mb->stop = 0;	for (i = 0; i < mb->vtop; i++)		freeVariable(mb, i);	mb->vtop = 0;	GDKfree(mb->stmt);	mb->stmt = 0;	GDKfree(mb->var);	mb->var = 0;	if( mb->history)		freeMalBlk(mb->history);	if (mb->binding)		GDKfree(mb->binding);	mb->binding = 0;	if (mb->help)		GDKfree(mb->help);	mb->help = 0;	if (mb->props)		GDKfree(mb->props);	mb->props = NULL;	if (mb->profiler)		GDKfree(mb->profiler);	mb->profiler = NULL;	GDKfree(mb);}@-The routine below should assure that all referenced structuresare private. The copying is memory conservative.@cMalBlkPtrcopyMalBlk(MalBlkPtr old){	MalBlkPtr mb;	int i;	mb = (MalBlkPtr) GDKzalloc(sizeof(MalBlkRecord));	if (mb == NULL)		GDKfatal("newMalBlk:could not get storage\n");	mb->props = old->props;	mb->alternative = old->alternative;	mb->history = NULL;	mb->var = (VarPtr *) GDKmalloc(sizeof(VarPtr) * old->vsize);	if (mb->var == NULL)		GDKfatal("newMalBlk:could not get variable storage\n");	mb->vsize = old->vsize;	memcpy((char *) mb->var, old->var, sizeof(VarPtr) * old->vsize);	mb->vtop = 0;	for (i = 0; i < old->vtop; i++) {		copyVariable(mb, old, getVar(old, i));		mb->vtop++;	}	mb->stmt = (InstrPtr *) GDKzalloc(sizeof(InstrPtr) * old->ssize);	if (mb->stmt == NULL)		GDKfatal("newMalBlk:could not get instruction storage\n");	mb->stop = old->stop;	mb->ssize = old->ssize;	assert(old->stop < old->ssize);	for (i = 0; i < old->stop; i++)		mb->stmt[i] = copyInstruction(old->stmt[i]);	mb->help = old->help ? GDKstrdup(old->help) : NULL;	mb->binding = old->binding ? GDKstrdup(old->binding) : NULL;	mb->errors = old->errors;	mb->typefixed = old->typefixed;	mb->flowfixed = old->flowfixed;	/* copy the properties as well */	if (mb->props)		mb->props = cpyPropertySet(old->props);	mb->profiler = NULL;	return mb;}voidaddtoMalBlkHistory(MalBlkPtr mb){	MalBlkPtr cpy;	cpy= copyMalBlk(mb);	cpy->alternative= NULL;	cpy->history= mb->history;	mb->history= cpy;}MalBlkPtrgetMalBlkHistory(MalBlkPtr mb, int idx){	while( mb && idx-- > 0)		mb= mb->history;	return mb;}@-The MalBlk structures potentially consume a lot a of space, because itis not possible to precisely estimate the default sizes of the var and stmtcomponents. The routines below provide a mechanism to handle the issue.The expandMalBlk routine takes the number of new-lines as a parameterand guesses the size of variable and statement table.Experience shows that trimming leads to memory fragmentation (140K lostafter server init) and is therefore turned off.@cvoidtrimexpand(MalBlkPtr mb, int varsize, int stmtsize){	VarRecord **v;	InstrPtr *stmt;	int len;	assert(varsize > 0 && stmtsize > 0);	len = sizeof(ValPtr) * (mb->vtop + varsize);	v = (VarRecord **) GDKzalloc(len);	if (v == NULL)		GDKfatal("newMalBlk:could not get variable storage\n");	memcpy((str) v, (str) mb->var, sizeof(ValPtr) * mb->vtop);	if (mb->var)		GDKfree(mb->var);	mb->var = v;	mb->vsize = mb->vtop + varsize;	len = sizeof(InstrPtr) * (mb->ssize + stmtsize);	stmt = (InstrPtr *) GDKzalloc(len);	if (stmt == NULL)		GDKfatal("newMalBlk:could not get instruction storage\n");	memcpy((str) stmt, (str) mb->stmt, sizeof(InstrPtr) * mb->stop);	GDKfree(mb->stmt);	mb->stmt = stmt;	mb->ssize = mb->ssize + stmtsize;	if (mb->profiler) {		GDKfree(mb->profiler);		mb->profiler = 0;	}}voidexpandMalBlk(MalBlkPtr mb, int lines){	int newlines = (int) (lines * 1.1);	if (newlines > mb->ssize || newlines > mb->vsize)		trimexpand(mb, newlines, newlines);}voidtrimMalBlk(MalBlkPtr mb){	(void) mb;		/* fool the compiler */	/* printf("safe %d %d\n",mb->vtop, (mb->vsize-mb->vtop)*sizeof(VarPtr));	   trimexpand(mb,0); */}@-Before compiling a large string, it makes sense to allocate approximatelyenough space to keep the intermediate code. Otherwise, we end up with arepeated extend on the MAL block, which really consumes a lot of memcpyresources.The average MAL string length could been derived from thetest cases. An error in the estimate is more expensive thanjust counting the lines.The MAL blocks act as instruction pools.Using a resetMALblock makes the instructions available.@cvoidprepareMalBlk(MalBlkPtr mb, str s){	int cnt = 0;	while(s){		s= strchr(s,'\n');		if(s) { s++; cnt++;}	}	expandMalBlk(mb, cnt);}InstrPtrnewInstruction(MalBlkPtr mb, int kind){	InstrPtr p = NULL;	int space;	if (mb && mb->stop <mb->ssize) {		p = mb->stmt[mb->stop];		if (p && p->maxarg < MAXARG)			p = NULL;		mb->stmt[mb->stop] = NULL;	}	if (p == NULL) {		space = (MAXARG - 1) * sizeof(int) + sizeof(InstrRecord);		p = GDKmalloc(space);		p->maxarg = MAXARG;	}	p->typechk = TYPE_UNKNOWN;	setModuleId(p, NULL);	setFunctionId(p, NULL);	p->fcn = NULL;	p->blk = NULL;	p->polymorphic = 0;	p->varargs = 0;	p->argc = 1;	p->retc = 1;	p->argv[0] = -1;	/* watch out for direct use in variable table */@-Flow of control instructions are always marked as an assignmentwith modifier@c	switch (kind) {	case BARRIERsymbol:	case REDOsymbol:	case LEAVEsymbol:	case EXITsymbol:	case RETURNsymbol:	case YIELDsymbol:	case CATCHsymbol:	case RAISEsymbol:		p->token = ASSIGNsymbol;		p->barrier = kind;		break;	default:		p->token = kind;		p->barrier = 0;	}	p->gc = 0;	p->jump = 0;	return p;}@- Copying an instruction is space conservative.@cInstrPtrcopyInstruction(InstrPtr p){	InstrPtr new;	new = (InstrPtr) GDKmalloc(sizeof(InstrRecord) + sizeof(int) * p->maxarg - 1);	oldmoveInstruction(new, p);	return new;}voidclrFunction(InstrPtr p){	p->fcn = 0;	setModuleId(p, NULL);	setFunctionId(p, NULL);}voidclrInstruction(InstrPtr p){	clrFunction(p);	memset((char *) p, 0, sizeof(InstrRecord) + (p->argc - 1) * sizeof(int));}voidfreeInstruction(InstrPtr p){	assert(p != 0);	GDKfree(p);}@-Moving instructions around calls for care, because all dependentinformation should also be updated.@cvoidoldmoveInstruction(InstrPtr new, InstrPtr p){	int space;	space = sizeof(InstrRecord) + sizeof(int) * p->argc;	memcpy((char *) new, (char *) p, space);	setFunctionId(new, getFunctionId(p));	setModuleId(new, getModuleId(p));}@-Query optimizers walk their way through a MAL program block.They require some primitives to move instructions around andto remove superflous instructions.The removal is based on the assumption that indeed the instructionbelonged to the block.@cvoidremoveInstruction(MalBlkPtr mb, InstrPtr p){	int i;	for (i = 0; i < mb->stop - 1; i++)		if (mb->stmt[i] == p)			break;	if (i == mb->stop)		return;	for (; i < mb->stop - 1; i++)		mb->stmt[i] = mb->stmt[i + 1];	mb->stop--;	mb->stmt[i] = p;	/* freeInstruction(p); */}voidremoveInstructionBlock(MalBlkPtr mb, int pc, int cnt){	int i;	InstrPtr p;	for (i = pc; i < pc + cnt; i++) {		p = getInstrPtr(mb, i);		freeInstruction(p);	}	for (i = pc; i < mb->stop - cnt; i++)		mb->stmt[i] = mb->stmt[i + cnt];	mb->stop -= cnt;	for (; i < mb->stop; i++)		mb->stmt[i] = 0;}voidmoveInstruction(MalBlkPtr mb, int pc, int target){	InstrPtr p;	int i;	p = getInstrPtr(mb, pc);	if (pc > target) {		for (i = pc; i > target; i--)			mb->stmt[i] = mb->stmt[i - 1];		mb->stmt[i] = p;	} else {		for (i = target; i > pc; i--)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -