📄 type.c
字号:
#include "cmm.h"#include <limits.h>static struct type typetab[] = {{CHAR,NULL,1,1,}, {INT, NULL,4,4,}, {VOID,NULL,0,0,}, {UNSIGNED, NULL,4,4,}};Type chartype = &typetab[0];Type inttype = &typetab[1];Type voidtype = &typetab[2];Type unsignedtype = &typetab[3];Type voidptype;static struct entry { struct type type; struct entry *link;}*typetable[64];/*type:构建新的类型*/static Type type(int op,Type ty, int size, int align){ struct entry *p; unsigned h = (op^((unsigned)ty>>3))&(NELEMS(typetable) -1); if (op != FUNCTION) for(p = typetable[h]; p ; p=p->link) if(p->type.op == op && p->type.type == ty && p->type.size == size && p->type.align == align) return &p->type; NEW0(p, PERM); p->type.op = op; p->type.type = ty; p->type.size = size; p->type.align = align; p->link = typetable[h]; typetable[h] = p; return &p->type;}/*ptr:创建指向类型为ty的指针类型*/Type ptr(Type ty){ return type(POINTER, ty, 4, 4);}Type deref(Type ty){ if(isptr(ty)) return ty->type; error("type error:%s\n","pointer expected"); return ty;}Type array(Type ty, int n, int align){ if(isfunc(ty)) { /*数组元素不能为函数*/ error("illegal type 'array of %t'\n",ty); return array(inttype,n,0); } if(n > INT_MAX/ty->size) { error("size of 'array of %t' exceeds %d bytes\n", ty, INT_MAX); n = INT_MAX/ty->size; } return type(ARRAY, ty, n*ty->size, align?align:ty->align);}Type atop(Type ty){ if(isarray(ty)) return ptr(ty->type); error("type error:%s\n","array expected"); return ptr(ty);}Type func(Type ty, Type *proto){ if(ty && (isarray(ty) || isfunc(ty))) { /*函数不能返回函数或数组*/ error("illegal type return type '%t'\n",ty); return func(inttype,proto); } ty = type(FUNCTION,ty,0,0); ty->proto = proto; return ty;}Type freturn(Type ty){ if(isfunc(ty)) return ty->type; error("type error:%s\n","function expected"); return inttype;}/*eqtype:判别类型ty1与ty2是否兼容。兼容则返回1,否则返回0*/int eqtype(Type ty1, Type ty2){ if (ty1 == ty2) return 1; if (ty1->op != ty2->op) return 0; if (!eqtype(ty1->type, ty2->type)) return 0; /*对于函数类型,需要检查其参数类型是否兼容*/ Type *p1= ty1->proto, *p2=ty2->proto; for (;p1 && p2 && *p1 && *p2 ; p1++,p2++) if(!eqtype(*p1,*p2)) return 0; if (NULL == p1 && NULL == p2) return 1; if (NULL == p1 || NULL == p2) return 0; return *p1 == *p2;}/*promote:将字符类型提升为整形*/Type promote(Type ty){ return ischar(ty) ? inttype:ty;}Type binary(Type xty, Type yty){ if(xty == unsignedtype || yty == unsignedtype) return unsignedtype; return inttype;}/*outtype输出类型ty的字面信息*/void outtype(Type ty){ if(NULL == ty) return; switch(ty->op) { case CHAR: case INT: case VOID: {int tok = t; t = ty->op; printtoken(); t = tok; }break; case ARRAY: fprint(2,"array %d of", ty->size); outtype(ty->type); break; case POINTER: fprint(2, "pointer to "); outtype(ty->type); break; case FUNCTION: outtype(ty->type); fprint(2, "function("); if(ty->proto && ty->proto[0]) { int i; fprint(2,"(%t", ty->proto[0]); for(i = 1; ty->proto[i]; i++) fprint(2,",%t",ty->proto[i]); }else if(ty->proto && ty->proto[0] == 0) fprint(2,"(void)"); break; } }int ttob(Type ty){ switch(ty->op) { case POINTER: case FUNCTION: return POINTER; default: return ty->op; }}Type btot(int op){ switch (optype(op)) { case C: return chartype; case I: return inttype; case P: return voidptype; case U: return unsignedtype; } assert(0); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -