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

📄 types.c

📁 window下的c编译器。
💻 C
📖 第 1 页 / 共 2 页
字号:
#include "c.h"

static Field check ARGS((Type, Type, Field, int));
static Field isfield ARGS((char *, Field));
static Type type ARGS((int, Type, int, int, void *));

static struct entry {
	struct type type;
	struct entry *link;
} *typetable[128];
static int maxlevel;

static Symbol pointersym;

Type chartype;			/* char */
Type doubletype;		/* double */
Type floattype;			/* float */
Type inttype;			/* signed int */
Type longdouble;		/* long double */
Type longtype;			/* long */
Type longlongtype;		/* long long (64 bits ) */
Type ulonglongtype;		/* unsigned long long (64 bits) */
Type shorttype;			/* signed short int */
Type signedchar;		/* signed char */
Type unsignedchar;		/* unsigned char */
Type unsignedlong;		/* unsigned long int */
Type unsignedshort;		/* unsigned short int */
Type unsignedtype;		/* unsigned int */
Type voidptype;			/* void* */
Type voidtype;			/* basic types: void */
int NrOfTypes;
static Type type(int op,Type ty,int size,int align,void *sym)
{
	unsigned h = (op^((unsigned)ty>>3))&(NELEMS(typetable)-1);
	struct entry *tn;

	if (op != FUNCTION && (op != ARRAY || size > 0))
		for (tn = typetable[h]; tn; tn = tn->link)
			if (tn->type.op    == op   && tn->type.type  == ty
			&&  tn->type.size  == size && tn->type.align == align
			&&  tn->type.u.sym == sym)
				return &tn->type;
	NEW0(tn, PERM);
//	memset(tn,0,sizeof(struct entry));
	tn->type.op = op;
	tn->type.type = ty;
	tn->type.size = size;
	tn->type.align = align;
	tn->type.u.sym = sym;
//	memset(&tn->type.x, 0, sizeof tn->type.x);
	tn->link = typetable[h];
	typetable[h] = tn;
	return &tn->type;
}
void typeInit() {
#define xx(v,name,op,metrics) { \
		Symbol p = install(string(name), &types, GLOBAL, PERM);\
		v = type(op, 0, IR->metrics.size, IR->metrics.align, p);\
		assert(v->align == 0 || v->size%v->align == 0); \
		p->type = v; p->addressed = IR->metrics.outofline; }
	xx(chartype,     "char",          CHAR,    charmetric);
	xx(doubletype,   "double",        DOUBLE,  doublemetric);
	xx(floattype,    "float",         FLOAT,   floatmetric);
	xx(inttype,      "int",           INT,     intmetric);
	xx(longdouble,   "long double",   DOUBLE,  doublemetric);
	xx(longtype,     "long int",      INT,     intmetric);
	xx(shorttype,    "short",         SHORT,   shortmetric);
	xx(signedchar,   "signed char",   CHAR,    charmetric);
	xx(unsignedchar, "unsigned char", CHAR,    charmetric);
	xx(unsignedlong, "unsigned long", UNSIGNED,intmetric);
	xx(unsignedshort,"unsigned short",SHORT,   shortmetric);
	xx(unsignedtype, "unsigned int",  UNSIGNED,intmetric);
	xx(longlongtype, "long long",     LONGLONG,doublemetric);
	xx (ulonglongtype,"unsigned long long",LONGLONG,doublemetric);
#undef xx
	{
		Symbol p;
		p = install(string("void"), &types, GLOBAL, PERM);
		voidtype = type(VOID, NULL, 0, 0, p);
		p->type = voidtype;
	}
	pointersym = install(string("T*"), &types, GLOBAL, PERM);
	pointersym->addressed = IR->ptrmetric.outofline;
	voidptype = ptr(voidtype);
	assert(voidptype->align > 0 && voidptype->size%voidptype->align == 0);
	assert(unsignedtype->size >= voidptype->size);
	assert(inttype->size >= voidptype->size);
}
void rmtypes(int lev)
{
	if (maxlevel >= lev) {
		int i;
		maxlevel = 0;
		for (i = 0; i < NELEMS(typetable); i++) {
			struct entry *tn, **tq = &typetable[i];
			while ((tn = *tq) != NULL)
				if (tn->type.op == FUNCTION)
					tq = &tn->link;
				else if (tn->type.u.sym && tn->type.u.sym->scope >= lev)
					*tq = tn->link;
				else {
					if (tn->type.u.sym && tn->type.u.sym->scope > maxlevel)
						maxlevel = tn->type.u.sym->scope;
					tq = &tn->link;
				}

		}
	}
}
Type ptr(Type ty)
{
	return type(POINTER, ty, IR->ptrmetric.size,
		IR->ptrmetric.align, pointersym);
}
Type deref(Type ty)
{
	if (isptr(ty))
		ty = ty->type;
	else
		error(StrTab[12], "pointer expected");// <type error: %s\n>
	return isenum(ty) ? unqual(ty)->type : ty;
}

Type array(Type ty,int n,int a)
{
	assert(ty);
	if (isfunc(ty)) {
		error(StrTab[14], ty);// <illegal type `array of %t'\n>
		return array(inttype, n, 0);
	}
	if (level > GLOBAL && isarray(ty) && ty->size == 0)
		error(StrTab[15]);// <missing array size\n>
	if (ty->size == 0) {
		if (unqual(ty) == voidtype)
			error(StrTab[16], ty);// <illegal type `array of %t'\n>
		else if (Aflag >= 2)
			warning(StrTab[17], ty);// <declaring type array of %t' is undefined\n>

	} else if (n > INT_MAX/ty->size) {
		error(StrTab[18],// <size of `array of %t' exceeds %d bytes\n>
			ty, INT_MAX);
		n = 1;
	}
	return type(ARRAY, ty, n*ty->size,
		a ? a : ty->align, NULL);
}

Type atop(Type ty)
{
	if (isarray(ty))
		return ptr(ty->type);
	error(StrTab[19], "array expected");// <type error: %s\n>
	return ptr(ty);
}

Type qual(int op,Type ty)
{
	if (isarray(ty))
		ty = type(ARRAY, qual(op, ty->type), ty->size,
			ty->align, NULL);
	else if (isfunc(ty)) {
		warning(StrTab[22]);// <qualified function type ignored\n>
	}
	else if (isconst(ty)    && op == CONST
	||       isvolatile(ty) && op == VOLATILE)
		error(StrTab[23], op, ty);// <illegal type `%k %t'\n>
	else {
		if (isqual(ty)) {
			op += ty->op;
			ty = ty->type;
		}
		ty = type(op, ty, ty->size, ty->align, NULL);
	}
	return ty;
}
Type func(Type ty,Type *proto,int style)
{
	if (ty && (isarray(ty) || isfunc(ty)))
		error(StrTab[24], ty);// <illegal return type `%t'\n>
	ty = type(FUNCTION, ty, 0, 0, NULL);
	ty->u.f.proto = proto;
	ty->u.f.oldstyle = style;
	return ty;
}
Type freturn(Type ty)
{
	if (isfunc(ty))
		return ty->type;
	error(StrTab[25], "function expected");// <type error: %s\n>
	return inttype;
}
int variadic(Type ty)
{
	if (isfunc(ty) && ty->u.f.proto) {
		int i;
		for (i = 0; ty->u.f.proto[i]; i++)
			;
		return i > 1 && ty->u.f.proto[i-1] == voidtype;
	}
	return 0;
}
Type newstruct(int op,char *tag)
{
	Symbol p;

	assert(tag);
	if (*tag == 0)
		tag = stringd(genlabel(1));
	else
		if ((p = lookup(tag, types)) != NULL && (p->scope == level
		|| p->scope == PARAM && level == PARAM+1)) {
			if (p->type->op == op && !p->defined)
				return p->type;
			error(StrTab[27],// <redefinition of `%s' previously defined at %w\n>
				p->name, &p->src);
		}
	p = install(tag, &types, level, PERM);
	p->type = type(op, NULL, 0, 0, p);
	if (p->scope > maxlevel)
		maxlevel = p->scope;
	p->src = src;
	return p->type;
}
Field newfield(char *name,Type ty,Type fty)
{
	Field p, *q = &ty->u.sym->u.s.flist;

	if (name == NULL)
		name = stringd(genlabel(1));
	for (p = *q; p; q = &p->link, p = *q)
		if (p->name == name)
			error(StrTab[28],// <duplicate field name `%s' in `%t'\n>
				name, ty);
	NEW0(p, PERM);
	*q = p;
	p->name = name;
	p->type = fty;
	if (xref) {							/* omit */
		if (ty->u.sym->u.s.ftab == NULL)			/* omit */
			ty->u.sym->u.s.ftab = table(NULL, level);	/* omit */
		install(name, &ty->u.sym->u.s.ftab, 0, PERM)->src = src;/* omit */
	}								/* omit */
	return p;
}
int eqtype(Type ty1,Type ty2,int ret)
{
	if (ty1 == ty2)
		return 1;
	if (ty1 == unsignedchar && ty2 == chartype)
		return 1;
	if (ty2 == unsignedchar && ty1 == chartype)
		return 1;
	if ((ty1->op == INT && ty2->op == ENUM) ||
		(ty1->op == ENUM && ty2->op == INT))
		return 1;
	if (ty1->op != ty2->op)
		return 0;
	switch (ty1->op) {
	case CHAR: case SHORT: case UNSIGNED: case INT: case LONGLONG:
	case ENUM: case UNION: case STRUCT:   case DOUBLE:
		return 0;
	case POINTER:  return eqtype(ty1->type, ty2->type, 1);
	case VOLATILE: case CONST+VOLATILE:
	case CONST:    return eqtype(ty1->type, ty2->type, 1);
	case ARRAY:    if (eqtype(ty1->type, ty2->type, 1)) {
				if (ty1->size == ty2->size)
					return 1;
				if (ty1->size == 0 || ty2->size == 0)
					return ret;
			}
			return 0;
	case FUNCTION: if (eqtype(ty1->type, ty2->type, 1)) {
				Type *p1 = ty1->u.f.proto, *p2 = ty2->u.f.proto;
				if (p1 == p2)
					return 1;
				if (p1 && p2) {
					for ( ; *p1 && *p2; p1++, p2++)
					if (eqtype(unqual(*p1), unqual(*p2), 1) == 0)
						return 0;
				if (*p1 == NULL && *p2 == NULL)
					return 1;
				} else {
					if (variadic(p1 ? ty1 : ty2))
					return 0;
				if (p1 == NULL)
					p1 = p2;
				for ( ; *p1; p1++) {
					Type ty = unqual(*p1);
					if (promote(ty) != (isenum(ty) ? ty->type : ty) || ty == floattype)
						return 0;
				}
				return 1;
				}
			}
			return 0;
	}
	assert(0); return 0;
}
Type promote(Type ty)
{
	ty = unqual(ty);
	if (isunsigned(ty) || ty == longtype)
		return ty;
	else if (ty == longlongtype)
		return ty;
	else if (isint(ty) || isenum(ty))
		return inttype;
	return ty;
}
Type compose(Type ty1,Type ty2)
{
	if (ty1 == ty2)
		return ty1;
	assert(ty1->op == ty2->op);
	switch (ty1->op) {
	case POINTER:
		return ptr(compose(ty1->type, ty2->type));
	case CONST+VOLATILE:
		return qual(CONST, qual(VOLATILE,
			compose(ty1->type, ty2->type)));
	case CONST: case VOLATILE:
		return qual(ty1->op, compose(ty1->type, ty2->type));
	case ARRAY:    { Type ty = compose(ty1->type, ty2->type);
			if (ty1->size && (ty1->type->size && ty2->size == 0 || ty1->size == ty2->size))
				return array(ty, ty1->size/ty1->type->size, ty1->align);
			if (ty2->size && ty2->type->size && ty1->size == 0)
				return array(ty, ty2->size/ty2->type->size, ty2->align);
			return array(ty, 0, 0);    }
	case FUNCTION: { Type *p1  = ty1->u.f.proto, *p2 = ty2->u.f.proto;
			Type ty   = compose(ty1->type, ty2->type);
			List tlist = NULL;
			if (p1 == NULL && p2 == NULL)
				return func(ty, NULL, 1);
			if (p1 && p2 == NULL)
				return func(ty, p1, ty1->u.f.oldstyle);
			if (p2 && p1 == NULL)
				return func(ty, p2, ty2->u.f.oldstyle);
			for ( ; *p1 && *p2; p1++, p2++) {
				Type ty = compose(unqual(*p1), unqual(*p2));
				if (isconst(*p1)    || isconst(*p2))
					ty = qual(CONST, ty);
				if (isvolatile(*p1) || isvolatile(*p2))
					ty = qual(VOLATILE, ty);
				tlist = append(ty, tlist);
			}
			assert(*p1 == NULL && *p2 == NULL);
			return func(ty, ltov(&tlist, PERM), 0); }
	}
	assert(0); return NULL;
}
int ttob(Type ty)
{
	switch (ty->op) {
	case CONST: case VOLATILE: case CONST+VOLATILE: case STDCALL:
		return ttob(ty->type);
	case CHAR: case INT:   case SHORT: case UNSIGNED: case LONGLONG:
	case VOID: case FLOAT: case DOUBLE:  return ty->op;
	case POINTER: case FUNCTION:         return POINTER;
	case ARRAY: case STRUCT: case UNION: return STRUCT;
	case ENUM:                           return INT;
	}
	assert(0); return INT;
}
Type btot(int op)
{
	switch (optype(op)) {
	case F: return floattype;
	case D: return doubletype;

⌨️ 快捷键说明

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