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

📄 mal_instruction.c

📁 一个内存数据库的源代码这是服务器端还有客户端
💻 C
📖 第 1 页 / 共 3 页
字号:
intnewVariable(MalBlkPtr mb, str name, malType type){	int n;	#line 1250 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_instruction.mx"	if( mb->vtop >= (1<<15)-2 ){		mb->vtop = (1<<15)-2 ;		GDKerror("newVariable: too many variables\n");	} else	if( mb->vtop >= mb->vsize){		VarPtr *new;        int s= mb->vtop + MAXVARS;		if (mb->vtop >mb->vsize)			GDKfatal("newVariable:variable administration\n");        new= (VarPtr *) GDKzalloc(s * sizeof(VarPtr));        /*printf("generate new variable block %d\n",s);*/        if( new== NULL ){            mb->errors++;            showScriptException(mb,0,MAL,"newMalBlk:no storage left\n");            return mb->vtop;        }        memcpy((char*)new, (char*)mb->var, sizeof(VarPtr) * mb->vtop);        GDKfree((str)mb->var);        mb->vsize= s;        mb->var= new;        mb->var[mb->vtop-1]->gdktype= TYPE_void;    }#line 1280 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_instruction.mx"	if (isTmpName(name)) {		int i = atol(name + 1);		/* test validity */		if (i > mb->vtop) {			showScriptException(mb,0,MAL,				"newVariable:variable %s mis-aligned\n", name);			mb->errors++;		} else if (i < mb->vtop ) {			showScriptException(mb,0,MAL,				"'%s' overwrites %s\n", name, getVarName(mb, i));			mb->errors++;		}	}	n = mb->vtop;	getVar(mb, n) = (VarPtr) GDKzalloc(sizeof(VarRecord));	mb->var[n]->name = name;	mb->var[n]->props = NULL;	setVarType(mb, n, type);	isConstant(mb, n) = 0;	setVarCleanup(mb, n) = FALSE;	getVar(mb, n)->isudftype = 0;	getVar(mb, n)->isused = 0;	mb->vtop++;	return n;}voidrenameVariable(MalBlkPtr mb, int id, str name){	VarPtr v;	assert(id >=0 && id <mb->vtop);	v = getVar(mb, id);	if (v->name)		GDKfree(v->name);	v->name = name;	v->tmpindex = 0;}#line 1335 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_instruction.mx"intnewTmpVariable(MalBlkPtr mb, malType type){	int n;	#line 1250 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_instruction.mx"	if( mb->vtop >= (1<<15)-2 ){		mb->vtop = (1<<15)-2 ;		GDKerror("newVariable: too many variables\n");	} else	if( mb->vtop >= mb->vsize){		VarPtr *new;        int s= mb->vtop + MAXVARS;		if (mb->vtop >mb->vsize)			GDKfatal("newVariable:variable administration\n");        new= (VarPtr *) GDKzalloc(s * sizeof(VarPtr));        /*printf("generate new variable block %d\n",s);*/        if( new== NULL ){            mb->errors++;            showScriptException(mb,0,MAL,"newMalBlk:no storage left\n");            return mb->vtop;        }        memcpy((char*)new, (char*)mb->var, sizeof(VarPtr) * mb->vtop);        GDKfree((str)mb->var);        mb->vsize= s;        mb->var= new;        mb->var[mb->vtop-1]->gdktype= TYPE_void;    }#line 1340 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_instruction.mx"	n = mb->vtop;	if (getVar(mb, n) == NULL)		getVar(mb, n) = (VarPtr) GDKzalloc(sizeof(VarRecord));/* already captured by memset    mb->var[n]->name = 0;    mb->var[n]->props = NULL;    isConstant(mb,n) = 0;    setVarCleanup(mb,n) = FALSE;	getVar(mb,n)->isudftype= 0;	getVar(mb,n)->isused= 0;*/	getVarTmp(mb, n) = n;	setVarType(mb, n, type);	mb->vtop++;	return n;}intnewTmpSink(MalBlkPtr mb, malType type){	int n;	n = type == TYPE_any ? -1 : findTmpVariable(mb, type);	if (n >= 0)		return n;	return newTmpVariable(mb, type);}intnewTypeVariable(MalBlkPtr mb, malType type){	int n;	n = type == TYPE_any ? -1 : findTmpVariable(mb, type);	if (n > 0 && isTypeVar(mb, n))		return n;	n = newTmpVariable(mb, type);	isTypeVar(mb, n) = TRUE;	return n;}voiddelVariable(MalBlkPtr mb, int varid){	if (varid == mb->vtop - 1) {		GDKfree(getVar(mb, varid));		getVar(mb, varid) = 0;		mb->vtop--;	}}voidcopyVariable(MalBlkPtr dst, MalBlkPtr src, VarPtr v){	VarPtr w;	(void) src;		/* fool the compiler */	w = (VarPtr) GDKzalloc(sizeof(VarRecord));	w->name = v->name ? GDKstrdup(v->name) : 0;	w->type = v->type;	w->gdktype = v->gdktype;	w->cleanup = v->cleanup;	w->fixtype = v->fixtype;	w->isudftype = v->isudftype;	w->isused = v->isused;	w->isaconstant = v->isaconstant;	w->isatypevar = v->isatypevar;	w->tmpindex = v->tmpindex;	w->depth = v->depth;	w->scope = v->scope;	VALcopy(&w->value, &v->value);	dst->var[dst->vtop] = w;	if( v->props )		w->props = cpyPropertySet(v->props);}#line 1423 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_instruction.mx"voidremoveVariable(MalBlkPtr mb, int varid){	int i, j;	InstrPtr p;	for (i = 0; i < mb->stop; i++) {		p = getInstrPtr(mb, i);		for (j = 0; j < p->argc; j++)			if (p->argv[j] > varid)				p->argv[j]--;	}	/* remove the variable from the symbol table */	freeVariable(mb, varid);	for (i = varid; i < mb->vtop - 1; i++)		mb->var[i] = mb->var[i + 1];	mb->vtop--;}voidclearVariable(MalBlkPtr mb, int varid){	VarPtr v;	v = getVar(mb, varid);	if (v == 0)		return;	if (v->name)		GDKfree(v->name);	if (v->isaconstant)		VALclear(&v->value);	if (v->props)		freePropertySet(v->props);	v->props= NULL;	v->name = 0;	v->type = 0;	v->gdktype = 0;	v->cleanup = 0;	v->fixtype = 0;	v->isudftype = 0;	v->isused = 0;	v->isaconstant = 0;	v->isatypevar = 0;	v->tmpindex = 0;	v->depth = 0;	v->scope = 0;	/* the above version is faster memset((char*)v, 0, sizeof(VarRecord)); */}voidfreeVariable(MalBlkPtr mb, int varid){	VarPtr v;	v = getVar(mb, varid);	clearVariable(mb, varid);	GDKfree(v);	getVar(mb, varid) = NULL;}#line 1519 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_instruction.mx"/** * Converts the constant in vr to the MAL type type.  Conversion is done * in the vr struct. * Returns 0 on success, non-zero otherwise. */intconvertConstant(malType type, ValPtr vr){	if( vr->vtype == type)		return 0;	if( vr->vtype == TYPE_str){#ifdef STRUCT_ALIGNED		int ll = 0;		ptr d = NULL;		ATOMfromstr(type, &d, &ll, vr->val.sval);		if (d == NULL) {			showException(SYNTAX, "convertConstant", "parse error in '%s'", vr->val.sval);			VALinit(vr, type, ATOMnilptr(type));			return 1;		}		VALset(vr, type, d);		if (ATOMextern(type) == 0)			GDKfree(d);#else		showException(SYNTAX, "convertConstant", "missing implementation");#endif		return vr->vtype != type;	}		switch (type) {	case TYPE_any:		assert(0);	case TYPE_bit:	case TYPE_chr:	case TYPE_sht:	case TYPE_int:	case TYPE_void:	case TYPE_oid:	case TYPE_flt:	case TYPE_dbl:	case TYPE_lng:		VALconvert(type,vr);		return vr->vtype != type;	case TYPE_str:		{ str w=0;		  if( vr->vtype == TYPE_void){			vr->vtype = type;			vr->val.sval = GDKstrdup(str_nil);			return 0;		  }		  if( vr->vtype == TYPE_chr){			w= GDKstrdup("a");			*w = vr->val.cval[0];		  } else			ATOMformat(vr->vtype, VALptr(vr), &w);			assert(w != NULL);			vr->vtype= TYPE_str;			vr->len = strlen(w);			vr->val.sval= w;		  /* VALset(vr, type, w); does not use TYPE-str */		}		return vr->vtype != type;	case TYPE_bat:		/* BAT variables can only be set to nil */		vr->vtype = type;		vr->val.bval = 0;		return 0;	case TYPE_ptr:		/* all coercions should be avoided to protect against memory probing */		/*		if (vr->vtype == TYPE_void) {			vr->vtype = type;			vr->val.pval = 0;			return 0;		}		if (ATOMcmp(vr->vtype, ATOMnilptr(vr->vtype), VALptr(vr)) == 0) {			vr->vtype = type;			vr->val.pval = 0;			return 0;		}		if (vr->vtype == TYPE_int) {			char buf[BUFSIZ];			int ll = 0;			ptr d = NULL;			snprintf(buf, BUFSIZ, "%d", vr->val.ival);			(*BATatoms[type].atomFromStr) (buf, &ll, &d);			if( d==0 ){				VALinit(vr, type, BATatoms[type].atomNull);				return 1;			}			VALset(vr, type, d);			if (ATOMextern(type) == 0 )				GDKfree(d);		}		*/		return vr->vtype != type;#line 1625 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_instruction.mx"	default:{		int ll = 0;		ptr d = NULL;		str s;		if( isaBatType(type)){			VALinit(vr, TYPE_bat, ATOMnilptr(TYPE_bat));			break;		}		/* see if an atomFromStr() function is available */		if (BATatoms[type].atomFromStr == 0) {			s = getTypeName(type);			showException(SYNTAX, "convertConstant", "no string coercion for '%s'", s);			GDKfree(s);			break;		}		/* if the value we're converting from is nil, the to		 * convert to value will also be nil */		if (ATOMcmp(vr->vtype, ATOMnilptr(vr->vtype), VALptr(vr)) == 0) {			VALinit(vr, type, ATOMnilptr(type));			break;		}		/* if what we're converting from is not a string */		if (vr->vtype != TYPE_str) {			/* an extern type */			str w = 0;			/* dump the non-string atom as string in w */			ATOMformat(vr->vtype, VALptr(vr), &w);			/* and try to parse it from string as the desired type */			ATOMfromstr(type, &d, &ll, w);			if (d == 0) {				showException(SYNTAX, "convertConstant", "parse error in '%s'", w);				VALinit(vr, type, ATOMnilptr(type));				GDKfree(w);				return 1;			}			VALset(vr, type, d);			if (ATOMextern(type) == 0)				GDKfree(d);			GDKfree(w);		} else { /* what we're converting from is a string */#ifdef STRUCT_ALIGNED			ATOMfromstr(type, &d, &ll, vr->val.sval);			if (d == NULL) {				showException(SYNTAX, "convertConstant", "parse error in '%s'", vr->val.sval);				VALinit(vr, type, ATOMnilptr(type));				return 1;			}			VALset(vr, type, d);			if (ATOMextern(type) == 0)				GDKfree(d);#else			showException(SYNTAX, "convertConstant", "missing implementation");#endif		}	}	}	return vr->vtype != type;}#define MAL_VAR_WINDOW  4*MAXARGintfndConstant(MalBlkPtr mb, ValPtr cst){	int i, k;	ptr p = VALget(cst);	k = mb->vtop - MAL_VAR_WINDOW;	if (k < 0)		k = 0;	for (i = mb->vtop - 1; i >= k; i--) {		VarPtr v = getVar(mb, i);		if (v && v->isaconstant &&			v->type == cst->vtype &&			ATOMcmp(cst->vtype, VALget(&v->value), p) == 0)			return i;	}	return -1;}intcpyConstant(MalBlkPtr mb, VarPtr vr){	int i;	ValRecord cst;	VALcopy(&cst, &vr->value);	i = defConstant(mb, vr->type, &cst);	return i;}intdefConstant(MalBlkPtr mb, int type, ValPtr cst){	int i, k;	ValPtr vr;	if (cst->vtype != type){		int otype= cst->vtype;		i= convertConstant(type, cst);		if( i ) {			str ft,tt;			ft= getTypeName(otype);			tt= getTypeName(type);			showException(SYNTAX, "defConstant", "constant coercion error from %s to %s", ft,tt);			GDKfree(ft);			GDKfree(tt);			mb->errors++;		} else			assert(cst->vtype== type);	}	k= fndConstant(mb,cst);	if( k >= 0 )		return k;	k = newTmpVariable(mb, type);	isConstant(mb, k) = 1;	setFixed(mb, k);	vr = &getVarConstant(mb, k);	*vr = *cst;	/*memcpy((char *) vr, cst, sizeof(ValRecord));*/	return k;}#line 1759 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_instruction.mx"InstrPtrpushArgument(MalBlkPtr mb, InstrPtr p, int varid){	p->argv[p->argc++] = varid;	if (p->argc == p->maxarg) {		InstrPtr pn;		int pc = 0;		int space = (p->maxarg - 1) * sizeof(int) + sizeof(InstrRecord);		pn = GDKmalloc(space + MAXARG * sizeof(int));		memcpy((char *) pn, (char *) p, space);		pn->maxarg = p->maxarg + MAXARG;		pc = getPC(mb, p);		if (pc >= 0)			mb->stmt[pc] = pn;		GDKfree(p);		p = pn;	}	return p;}InstrPtrsetArgument(MalBlkPtr mb, InstrPtr p, int idx, int varid){	int i;	p = pushArgument(mb, p, varid);	/* make space */	for (i = p->argc - 1; i > idx; i--)		getArg(p, i) = getArg(p, i - 1);	getArg(p, i) = varid;	return p;}InstrPtrpushReturn(MalBlkPtr mb, InstrPtr p, int varid){	if (p->argv[p->retc - 1] == -1) {		p->argv[p->retc - 1] = varid;		return p;	}	p = pushArgument(mb, p, varid);	p->retc++;	return p;}#line 1810 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_instruction.mx"InstrPtrpushArgumentId(MalBlkPtr mb, InstrPtr p, str name){	int v;	v = findVariable(mb, name);	if (v < 0)		v = newVariable(mb, name, getTypeIndex(name, -1, TYPE_any));	else		GDKfree(name);	return pushArgument(mb, p, v);}#line 1827 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_instruction.mx"voiddelArgument(InstrPtr p, int idx){	int i;	for (i = idx; i < p->argc - 1; i++)		p->argv[i] = p->argv[i + 1];	p->argc--;	if (idx < p->retc)		p->retc--;}voidsetVarType(MalBlkPtr mb, int i, int tpe){	VarPtr v;	v = mb->var[i];	v->type = tpe;	v->gdktype = tpe <= TYPE_str ? tpe : (tpe == TYPE_any ? TYPE_void : findGDKtype(tpe));}#line 1853 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_instruction.mx"voidclrAllTypes(MalBlkPtr mb){	int i;	InstrPtr p;	p= getInstrPtr(mb,0);	for(i=p->argc; i<mb->vtop; i++)	if( !mb->var[i]->isudftype &&		mb->var[i]->isused &&		!mb->var[i]->isaconstant){		setVarType(mb,i,TYPE_any);		setVarCleanup(mb,i) = FALSE;		mb->var[i]->gdktype= TYPE_void;		mb->var[i]->fixtype= 0;	}	for(i=1; i<mb->stop-1; i++){		p= getInstrPtr(mb,i);		switch(p->token){		case RAISEsymbol:		case CATCHsymbol:		case RETURNsymbol:		case LEAVEsymbol:		case YIELDsymbol:		case EXITsymbol:		case NOOPsymbol:			break;		case ENDsymbol:			return;		default:			p->token= ASSIGNsymbol;			p->typechk= TYPE_UNKNOWN;			p->fcn= 0;			p->blk= NULL;		}	}}voidsetArgType(MalBlkPtr mb, InstrPtr p, int i, int tpe){	if (p->argv[i] >= mb->vsize) {		GDKwarning("setArgType:array bound error\n");		return;	}	mb->var[getArg(p, i)]->type = tpe;}voidsetReturnArgument(InstrPtr p, int i){	setDestVar(p, i);}malTypedestinationType(MalBlkPtr mb, InstrPtr p){	if (p->argc > 0)		return getVarType(mb, getDestVar(p));	return TYPE_any;}#line 1924 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_instruction.mx"INLINE voidsetPolymorphic(InstrPtr p, int tpe, int force){	int c1 = 0, c2 = 0;	if (force == FALSE && tpe == TYPE_any)		return;	if (isaBatType(tpe)) {		if (getHeadIndex(tpe) > 0)			c1 = getHeadIndex(tpe);		else if (getHeadType(tpe) == TYPE_any)			c1 = 1;	}	if (getTailIndex(tpe) > 0)		c2 = getTailIndex(tpe);	else if (getTailType(tpe) == TYPE_any)		c2 = 1;

⌨️ 快捷键说明

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