本文解析lcc中最复杂的模块之一decl.c,作为语法分析的一部分,它处理各个声明。
#include "c.h"
static char rcsid[] = "$Id: decl.c,v 1.1 2002/08/28 23:12:42 drh Exp $";
#define add(x,n) (x > inttype->u.sym->u.limits.max.i-(n) ? (overflow=1,x) : x+(n))
#define chkoverflow(x,n) ((void)add(x,n))
#define bits2bytes(n) (((n) + 7)/8)
static int regcount;
static List autos, registers;
Symbol cfunc; /* current function */
Symbol retv; /* return value location for structs */
static void checkref(Symbol, void *);
static Symbol dclglobal(int, char *, Type, Coordinate *);
static Symbol dcllocal(int, char *, Type, Coordinate *);
static Symbol dclparam(int, char *, Type, Coordinate *);
static Type dclr(Type, char **, Symbol **, int);
static Type dclr1(char **, Symbol **, int);
static void decl(Symbol (*)(int, char *, Type, Coordinate *));
extern void doconst(Symbol, void *);
static void doglobal(Symbol, void *);
static void doextern(Symbol, void *);
static void exitparams(Symbol []);
static void fields(Type);
static void funcdefn(int, char *, Type, Symbol [], Coordinate);
static void initglobal(Symbol, int);
static void oldparam(Symbol, void *);
static Symbol *parameters(Type);
static Type specifier(int *);
static Type structdcl(int);
static Type tnode(int, Type);
/*
*循环地分析所有源程序,把所有声明和语义都分析出来,并且在遇到函数的定义时,就会调用后端生成汇编代码
*/
void program(void) {
int n;
//作用域为全局
level = GLOBAL;
//分析源程序到文件尾
for (n = 0; t != EOI; n++)//它的终止条件是分析源程序文件到结束
//判断开始的词素是否合法
if (kind[t] == CHAR || kind[t] == STATIC
|| t == ID || t == '*' || t == '(') {
//声明开始
decl(dclglobal);
//删除分配内存空间
deallocate(STMT);
if (!(glevel >= 3 || xref))
deallocate(FUNC);
//取得记号t为分号,那这种情况是出错的,发出警告给软件开发人员,说明写了一行空行代码,接着获取下一个记号
} else if (t == ';') {
warning("empty declaration\n");
t = gettok();
//处理错误的声明,因为不能处理这种记号开始的声明
} else {
error("unrecognized declaration\n");
t = gettok();
}
//当整个文件没有写一行代码的情况给出警告
if (n == 0)
warning("empty input file\n");
}
//输入了获取声明的存储类型参数sclass
static Type specifier(int *sclass) {
//一个声明变量有可能出现的说明符就有以下几种:
//存储类型、常量说明、符号说明、类型大小、类型、是否优化
int cls, cons, sign, size, type, vol;
//定义返回类型保存变量
Type ty = NULL;
//把所有类型说明初始化为0值,表示没有出现这种说明
cls = vol = cons = sign = size = type = 0;
//把存储类型设置为自动类型。由于在C语言里,所有变量声明如果没有明确地指明存储类型时,缺省就是自动类型AUTO
if (sclass == NULL)
cls = AUTO;
for (;;) {
int *p, tt = t;
switch (t) {
case AUTO:
//当全局变量声明而又声明为寄存器类型,就出会提示出错
case REGISTER: if (level <= GLOBAL && cls == 0)
error("invalid use of `%k'\n", t);
//保存当前识别出来的类型说明并获取下一个词素
p = &cls; t = gettok(); break;
case STATIC: case EXTERN:
case TYPEDEF: p = &cls; t = gettok(); break;
case CONST: p = &cons; t = gettok(); break;
//识别优化限制说明,当指定这个属性时,表示这个变量不能优化掉的
case VOLATILE: p = &vol; t = gettok(); break;
case SIGNED:
case UNSIGNED: p = &sign; t = gettok(); break;
case LONG: if (size == LONG) {
size = 0;
tt = LONG+LONG;
}
p = &size; t = gettok(); break;
case SHORT: p = &size; t = gettok(); break;
case VOID: case CHAR: case INT: case FLOAT:
case DOUBLE: p = &type; ty = tsym->type;
t = gettok(); break;
case ENUM: p = &type; ty = enumdcl(); break;//枚举的声明处理
case STRUCT:
case UNION: p = &type; ty = structdcl(t); break;//共用体和结构体放在一起处理
//当把所有的说明符分析完成后,最后肯定是剩下一个ID,如果不是就有可能出错
//处理ID是自己定义的类型处理,比如用typedef定义的ID类型,就需要在那里识别出类型的属性
case ID:
if (istypename(t, tsym) && type == 0
&& sign == 0 && size == 0) {
use(tsym, src);
ty = tsym->type;
if (isqual(ty)
&& ty->size != ty->type->size) {
ty = unqual(ty);
if (isconst(tsym->type))
ty = qual(CONST, ty);
if (isvolatile(tsym->type))
ty = qual(VOLATILE, ty);
tsym->type = ty;
}
p = &type;
t = gettok();
} else//如果这个ID是变量
p = NULL;
break;
default: p = NULL;
}
if (p == NULL)
break;
if (*p)
error("invalid use of `%k'\n", tt);
//意味着整个声明已经分析完成,接着就是把所有分析出来的说明符组成属性结构,保存了到相应的符号表里
*p = tt;
}
//保存存储类型返回给调用函数
if (sclass)
*sclass = cls;
//设置类型为缺省值
if (type == 0) {
type = INT;
ty = inttype;
}
//判断声明组合是否合法,如果不合法的组合,就需要提示出错
if (size == SHORT && type != INT
|| size == LONG+LONG && type != INT
|| size == LONG && type != INT && type != DOUBLE
|| sign && type != INT && type != CHAR)
error("invalid type specification\n");
//根据符号和类型来判断声明的类型,由于类型的大小不同,符号位不同,而组成不同的类型。这些类型都是C编译器预先定义好的
if (type == CHAR && sign)
ty = sign == UNSIGNED ? unsignedchar : signedchar;
else if (size == SHORT)
ty = sign == UNSIGNED ? unsignedshort : shorttype;
else if (size == LONG && type == DOUBLE)
ty = longdouble;
else if (size == LONG+LONG) {
ty = sign == UNSIGNED ? unsignedlonglong : longlong;
if (Aflag >= 1)
warning("`%t' is a non-ANSI type\n", ty);
} else if (size == LONG)
ty = sign == UNSIGNED ? unsignedlong : longtype;
else if (sign == UNSIGNED && type == INT)
ty = unsignedtype;
//添加常量属性
if (cons == CONST)
ty = qual(CONST, ty);//构造属性限定类型
//添加不可优化属性
if (vol == VOLATILE)
ty = qual(VOLATILE, ty);//构造属性限定类型
//把声明的类型返回给调用函数
return ty;
}
static void decl(Symbol (*dcl)(int, char *, Type, Coordinate *)) {
//定义保存存储类型的局部变量
int sclass;
Type ty, ty1;
//定义语法出错时停止的条件
static char stop[] = { CHAR, STATIC, ID, 0 };
//调用函数specifier进行语法的声明处理
ty = specifier(&sclass);
//在声明分析之后,也就是识别了说明符之后,记号开始一定是ID、'*'、'('、'['等几种类型了。如果不是这样的记号,说明语法出错
if (t == ID || t == '*' || t == '(' || t == '[') {
char *id;
Coordinate pos; //file info, pan , tilt
id = NULL;
//保存源程序分析的位置
pos = src;
//根据当前作用域来分别处理,局部声明的作用域是没有参数处理的
if (level == GLOBAL) {
Symbol *params = NULL;
//调声明处理函数dclr来分析全局变量和函数的声明
ty1 = dclr(ty, &id, ¶ms, 0);
//判断是否函数的声明,如果是函数的声明,就再进一步判断是否函数定义的复合语句
if (params && id && isfunc(ty1)
&& (t == '{' || istypename(t, tsym)
|| (kind[t] == STATIC && t != TYPEDEF))) {
if (sclass == TYPEDEF) {
error("invalid use of `typedef'\n");
sclass = EXTERN;//错误处理
}
if (ty1->u.f.oldstyle)//处理旧时风格的函数参数方式
exitscope();
/*处理函数定义,并生成汇编代码
*sclass 是函数返回存储类型
*id 是函数声明的名称
*ty1是返回类型
*params是函数的参数列表
*pos是函数定义的位置
*/
funcdefn(sclass, id, ty1, params, pos);
return;
//处理参数列表,退出参数的作用域
}
else if (params)
exitparams(params);
}
else//局部函数和变量
ty1 = dclr(ty, &id, NULL, 0);
//用来处理声明的变量是逗号表达式时并列处理
for (;;) {
if (Aflag >= 1 && !hasproto(ty1))
warning("missing prototype\n");
//处理声明没有ID的出错情况
if (id == NULL)
error("missing identifier\n");
//处理typedef定义的类型声明
else if (sclass == TYPEDEF)
{
//从符号表identifiers里查找这个ID是否已经声明了,
//如果有声明过并且作用域一样,就表示重复声明了同一个ID
Symbol p = lookup(id, identifiers);
if (p && p->scope == level)
error("redeclaration of `%s'\n", id);
//保存这个ID到符号表identifiers
p = install(id, &identifiers, level,
level < LOCAL ? PERM : FUNC);
p->type = ty1;//保存声明的类型
p->sclass = TYPEDEF;//保存存储的类型
p->src = pos;//保存源程序位置
}
else
//调用全局函数dclglobal,或者局部函数dcllocal,或者参数函数dclparam来处理变量ID
(void)(*dcl)(sclass, id, ty1, &pos);
if (t != ',')
break;
t = gettok();
id = NULL;
pos = src;
ty1 = dclr(ty, &id, NULL, 0);
}
}
//处理出错的情况
else if (ty == NULL
|| !(isenum(ty) ||
isstruct(ty) && (*unqual(ty)->u.sym->name < '1' || *unqual(ty)->u.sym->name > '9')))
error("empty declaration\n");
test(';', stop);//测试当前的记号是否语句结束符,如果不是,就会跳过错误直到下一句语句再进行分析处理
}
/*
------------------>主要用来分析全局函数
int sclass -->是这个函数名称存储类型。
char *id -->是函数的名称。
Type ty -->是函数的类型,包括返回类型。
Coordinate *pos -->是函数定义的源程序中位置
*/
static Symbol dclglobal(int sclass, char *id, Type ty, Coordinate *pos) {
Symbol p;
if (sclass == 0)
sclass = AUTO;//默认存储类型
else if (sclass != EXTERN && sclass != STATIC) {
error("invalid storage class `%k' for `%t %s'\n",
sclass, ty, id);
sclass = AUTO;
}
//在id符号表中查找这个全局函数或者变量
p = lookup(id, identifiers);
if (p && p->scope == GLOBAL) {
if (p->sclass != TYPEDEF && eqtype(ty, p->type, 1))
ty = compose(ty, p->type);
else
error("redeclaration of `%s' previously declared at %w\n", p->name, &p->src);
if (!isfunc(ty) && p->defined && t == '=')
error("redefinition of `%s' previously defined at %w\n", p->name, &p->src);
if (p->sclass == EXTERN && sclass == STATIC
|| p->sclass == STATIC && sclass == AUTO
|| p->sclass == AUTO && sclass == STATIC)
warning("inconsistent linkage for `%s' previously declared at %w\n", p->name, &p->src);
}
if (p == NULL || p->scope != GLOBAL) {
Symbol q = lookup(id, externals);
if (q) {
if (sclass == STATIC || !eqtype(ty, q->type, 1))
warning("declaration of `%s' does not match previous declaration at %w\n", id, &q->src);
p = relocate(id, externals, globals);
p->sclass = sclass;
} else {
p = install(id, &globals, GLOBAL, PERM);
p->sclass = sclass;
(*IR->defsymbol)(p);
}
//统计全局函数定义的个数
if (p->sclass != STATIC) {
static int nglobals;
nglobals++;
if (Aflag >= 2 && nglobals == 512)
warning("more than 511 external identifiers\n");
}
}
else if (p->sclass == EXTERN)
p->sclass = sclass;
//处理函数初始化
p->type = ty;
p->src = *pos;
if (t == '=' && isfunc(p->type)) {
error("illegal initialization for `%s'\n", p->name);
t = gettok();
initializer(p->type, 0);
} else if (t == '=') {
initglobal(p, 0);
if (glevel > 0 && IR->stabsym) {
(*IR->stabsym)(p); swtoseg(p->u.seg); }
} else if (p->sclass == STATIC && !isfunc(p->type)
&& p->type->size == 0)
error("undefined size for `%t %s'\n", p->type, p->name);
return p;//返回这个函数属性符号
}
static void initglobal(Symbol p, int flag) {
Type ty;
if (t == '=' || flag) {
if (p->sclass == STATIC) {
for (ty = p->type; isarray(ty); ty = ty->type)
;
defglobal(p, isconst(ty) ? LIT : DATA);
} else
defglobal(p, DATA);
if (t == '=')
t = gettok();
ty = initializer(p->type, 0);
if (isarray(p->type) && p->type->size == 0)
p->type = ty;
if (p->sclass == EXTERN)
p->sclass = AUTO;
}
}
void defglobal(Symbol p, int seg) {
p->u.seg = seg;
swtoseg(p->u.seg);
if (p->sclass != STATIC)
(*IR->export)(p);
(*IR->global)(p);
p->defined = 1;
}
/*
*/
static Type dclr(Type basety, char **id, Symbol **params, int abstract) {
Type ty = dclr1(id, params, abstract);
//用for循环把所有类型添加到一个链表basety里
for ( ; ty; ty = ty->type)
switch (ty->op) {
case POINTER:
basety = ptr(basety);//调用ptr来添加类型到链表
break;
case FUNCTION:
basety = func(basety, ty->u.f.proto,
ty->u.f.oldstyle);
break;
case ARRAY:
basety = array(basety, ty->size, 0);
break;
case CONST: case VOLATILE:
basety = qual(ty->op, basety);
break;
default: assert(0);
}
if (Aflag >= 2 && basety->size > 32767)
warning("more than 32767 bytes in `%t'\n", basety);
return basety;//返回这个指针链表的类型,这样就可以给后面判断语法和语义了
}
//构造一个类型
static Type tnode(int op, Type type) {
Type ty;
NEW0(ty, STMT);
ty->op = op;
ty->type = type;
return ty;
}
/*
*/
static Type dclr1(char **id, Symbol **params, int abstract) {
Type ty = NULL;
switch (t) {
case ID: if (id)
*id = token; //global variable in lex.c, current token
else
error("extraneous identifier `%s'\n", token);
t = gettok();
break;
case '*': t = gettok(); //递归解析表达式,知道得出结论
if (t == CONST || t == VOLATILE) {
Type ty1;
ty1 = ty = tnode(t, NULL);//构造一个指针类型
while ((t = gettok()) == CONST || t == VOLATILE)//处理前置限制
ty1 = tnode(t, ty1);
ty->type = dclr1(id, params, abstract);
ty = ty1;
}
else
ty = dclr1(id, params, abstract);
ty = tnode(POINTER, ty); break;
case '(': t = gettok(); if (abstract
&& (t == REGISTER || istypename(t, tsym) || t == ')')) {
Symbol *args;
ty = tnode(FUNCTION, ty);
enterscope();
if (level > PARAM)
enterscope();
args = parameters(ty);//处理函数参数
exitparams(args);
} else {
ty = dclr1(id, params, abstract);
expect(')');
if (abstract && ty == NULL
&& (id == NULL || *id == NULL))
return tnode(FUNCTION, NULL);
} break;
case '[': break;
default: return ty;
}
while (t == '(' || t == '[')
switch (t) {
//func declaration
case '(':
//specifier函数里就已经可以把类型和ID识别出来,并判断语法的合法性,这里处理括号和参数列表
t = gettok();
{
Symbol *args;
ty = tnode(FUNCTION, ty);
enterscope();
if (level > PARAM)/*判断是否在函数声明里再声明一个函数,如是就需要再增加它的作用域*/
enterscope();
//分析参数列表
args = parameters(ty);
if (params && *params == NULL)/*保存分析后的参数列表返回,以便后面构造函数声明的类型*/
*params = args;
else
exitparams(args);
}
break;
case '[': t = gettok(); { int n = 0;
if (kind[t] == ID) {
n = intexpr(']', 1);
if (n <= 0) {
error("`%d' is an illegal array size\n", n);
n = 1;
}
} else
expect(']');
ty = tnode(ARRAY, ty);
ty->size = n; } break;
default: assert(0);
}
return ty;
}
//分析函数参数列表
static Symbol *parameters(Type fty) {
List list = NULL;
Symbol *params;
//判断参数类型是否合法,主要是存储类型或者类型声明
if (kind[t] == STATIC || istypename(t, tsym)) {
int n = 0;
Type ty1 = NULL;
//一个一个地分析逗号分隔的参数
for (;;) {
Type ty;
int sclass = 0;
char *id = NULL;
if (ty1 && t == ELLIPSIS) //处理参数里可变参数(…)
{
static struct symbol sentinel;
if (sentinel.type == NULL) {
sentinel.type = voidtype;
sentinel.defined = 1;
}
if (ty1 == voidtype)
error("illegal formal parameter types\n");
list = append(&sentinel, list);
t = gettok();
break;
/*C编译器为了处理可变参数需要从右向左地压参数入栈,以便计算有多少个参数。
ty1的判断是表示在可变参数之前,一定需要有一个参数,如果只有一个可变参数是非法的。
处理可变参数很简单,就是把它类型sentinel添加到参数列表list*/
}
//判断参数是否有声明类型,如果没有就出错
if (!istypename(t, tsym) && t != REGISTER)
error("missing parameter type\n");
n++;//增加参数的计算,也可以说参数的名称
//递归调用类型声明函数dclr来分析这个参数定义是否合法,而在函数dclr调用之前,就需要先递归调用说明符处理函数specifier来处理
ty = dclr(specifier(&sclass), &id, NULL, 1);
if ( ty == voidtype && (ty1 || id)
|| ty1 == voidtype)//处理声明为void类型语法错误处理
error("illegal formal parameter types\n");
if (id == NULL)//处理声明参数变量是空时处理,采用自定义的数据作为名称
id = stringd(n);
/*判断声明的类型不为空类型的话,就说明已经定义了一个参数变量,
需要检查这个参数变量是否已经在前面的局部变量或者全局变量里声明了吗?
要解决定这个疑问就需要调用参数变量声明函数dclparam来处理*/
if (ty != voidtype)
list = append(dclparam(sclass, id, ty, &src), list);
if (Aflag >= 1 && !hasproto(ty))
warning("missing prototype\n");
if (ty1 == NULL)
ty1 = ty;
//判断是否还有参数变量声明,如果没有就跳出for循环,处理参数完毕
if (t != ',')
break;
t = gettok();
}
fty->u.f.proto = newarray(length(list) + 1,
sizeof (Type *), PERM);//创建参数类型原型列表
params = ltov(&list, FUNC);
for (n = 0; params[n]; n++)//保存所有参数变量的类型
fty->u.f.proto[n] = params[n]->type;
fty->u.f.proto[n] = NULL;
fty->u.f.oldstyle = 0;//指定这个函数声明是新类型的函数声明
}
else {
if (t == ID)
for (;;) {
Symbol p;
if (t != ID) {
error("expecting an identifier\n");
break;
}
p = dclparam(0, token, inttype, &src);
p->defined = 0;
list = append(p, list);
t = gettok();
if (t != ',')
break;
t = gettok();
}
params = ltov(&list, FUNC);
fty->u.f.proto = NULL;
fty->u.f.oldstyle = 1;
}
if (t != ')') {//判断是否函数声明结束
static char stop[] = { CHAR, STATIC, IF, ')', 0 };
expect(')');
skipto('{', stop);
}
if (t == ')')
t = gettok();
return params;
}
static void exitparams(Symbol params[]) {
assert(params);
if (params[0] && !params[0]->defined)
error("extraneous old-style parameter list\n");
if (level > PARAM)
exitscope();
exitscope();
}
static Symbol dclparam(int sclass, char *id, Type ty, Coordinate *pos) {
Symbol p;
if (isfunc(ty))//判断这个参数变量是否声明为函数类型,如果是就需要创建新类型
ty = ptr(ty);
else if (isarray(ty))//判断这个参数变量是否声明为数据类型
ty = atop(ty);
//***************************************************************//
//都是判断存储类型。如果没有声明存储类型,缺省为AUTO类型。
//其它几个识别为寄存器类型REGISTER、不可删除属性、结构类型
if (sclass == 0)
sclass = AUTO;
else if (sclass != REGISTER) {
error("invalid storage class `%k' for `%t%s\n",
sclass, ty, stringf(id ? " %s'" : "' parameter", id));
sclass = AUTO;
} else if (isvolatile(ty) || isstruct(ty)) {
warning("register declaration ignored for `%t%s\n",
ty, stringf(id ? " %s'" : "' parameter", id));
sclass = AUTO;
}
//***************************************************************//
//查找这个参数变量是否已经在其它地方声明过,如果声明过就从lookup返回给p,
//如果作用域一样的声明肯定就是重复声明,这是出错的
p = lookup(id, identifiers);
if (p && p->scope == level)
error("duplicate declaration for `%s' previously declared at %w\n", id, &p->src);
else//如果没有这个参数变量是合法的,就把它保存到声明表格identifiers里
p = install(id, &identifiers, level, FUNC);
p->sclass = sclass;
p->src = *pos;
p->type = ty;
p->defined = 1;
if (t == '=') {//标准C里是不允许参数变量初始化
error("illegal initialization for parameter `%s'\n", id);
t = gettok();
(void)expr1(0);
}
return p;
}
static Type structdcl(int op) {
char *tag;
Type ty;
Symbol p;
Coordinate pos;
t = gettok();
pos = src;
if (t == ID) {
tag = token;
t = gettok();
} else
tag = "";//没有tag,struct后的结构名
if (t == '{') {
static char stop[] = { IF, ',', 0 };
ty = newstruct(op, tag);//创建结构的类型ty
ty->u.sym->src = pos;
ty->u.sym->defined = 1;
t = gettok();
if (istypename(t, tsym))//判断语句是否类型开始的记号,如果不是就是出错的定义
fields(ty);//如果是类型,就需要调用函数fields(ty)来处理所有的字段定义
else
error("invalid %k field declarations\n", op);
test('}', stop);
}
else if (*tag && (p = lookup(tag, types)) != NULL
&& p->type->op == op) {
ty = p->type;
if (t == ';' && p->scope < level)
ty = newstruct(op, tag);
}
else {
if (*tag == 0)
error("missing %k tag\n", op);
ty = newstruct(op, tag);
}
if (*tag && xref)
use(ty->u.sym, pos);
return ty;
}
static void fields(Type ty) {
{ int n = 0;
while (istypename(t, tsym)) //判断是否类型定义,如果是的话就不断地进行字段列表处理
{
static char stop[] = { IF, CHAR, '}', 0 };
Type ty1 = specifier(NULL);//这里也是递归调用的。主要用来分析一行代码的声明处理
for (;;) {
Field p;
char *id = NULL;
Type fty = dclr(ty1, &id, NULL, 0);//进行一个声明的处理,也是调用函数dclr来递归处理
p = newfield(id, ty, fty);
if (Aflag >= 1 && !hasproto(p->type))
warning("missing prototype\n");
if (t == ':') {
if (unqual(p->type) != inttype
&& unqual(p->type) != unsignedtype) {
error("`%t' is an illegal bit-field type\n",
p->type);
p->type = inttype;
}
t = gettok();
p->bitsize = intexpr(0, 0);
if (p->bitsize > 8*inttype->size || p->bitsize < 0) {
error("`%d' is an illegal bit-field size\n",
p->bitsize);
p->bitsize = 8*inttype->size;
} else if (p->bitsize == 0 && id) {
warning("extraneous 0-width bit field `%t %s' ignored\n", p->type, id);
p->name = stringd(genlabel(1));
}
p->lsb = 1;
}
else {
if (id == NULL)
error("field name missing\n");
else if (isfunc(p->type))
error("`%t' is an illegal field type\n", p->type);
else if (p->type->size == 0)
error("undefined size for field `%t %s'\n",
p->type, id);
}
if (isconst(p->type))
ty->u.sym->u.s.cfields = 1;
if (isvolatile(p->type))
ty->u.sym->u.s.vfields = 1;
n++;
if (Aflag >= 2 && n == 128)
warning("more than 127 fields in `%t'\n", ty);
if (t != ',')
break;
t = gettok();
}
test(';', stop);
} }
{ int bits = 0, off = 0, overflow = 0;
Field p, *q = &ty->u.sym->u.s.flist;
ty->align = IR->structmetric.align;
for (p = *q; p; p = p->link) {
int a = p->type->align ? p->type->align : 1;
if (p->lsb)
a = unsignedtype->align;
if (ty->op == UNION)
off = bits = 0;
else if (p->bitsize == 0 || bits == 0
|| bits - 1 + p->bitsize > 8*unsignedtype->size) {
off = add(off, bits2bytes(bits-1));
bits = 0;
chkoverflow(off, a - 1);
off = roundup(off, a);
}
if (a > ty->align)
ty->align = a;
p->offset = off;
if (p->lsb) {
if (bits == 0)
bits = 1;
if (IR->little_endian)
p->lsb = bits;
else
p->lsb = 8*unsignedtype->size - bits + 1
- p->bitsize + 1;
bits += p->bitsize;
} else
off = add(off, p->type->size);
if (off + bits2bytes(bits-1) > ty->size)
ty->size = off + bits2bytes(bits-1);
if (p->name == NULL
|| !('1' <= *p->name && *p->name <= '9')) {
*q = p;
q = &p->link;
}
}
*q = NULL;
chkoverflow(ty->size, ty->align - 1);
ty->size = roundup(ty->size, ty->align);
if (overflow) {
error("size of `%t' exceeds %d bytes\n", ty, inttype->u.sym->u.limits.max.i);
ty->size = inttype->u.sym->u.limits.max.i&(~(ty->align - 1));
} }
}
/*-------------------> 处理函数定义,并生成汇编代码
*int sclass --> 是函数返回存储类型
*char *id --> 是函数声明的名称
*Type ty --> 是返回类型
*Symbol params[] --> 是函数的参数列表
*Coordinate pt --> 是函数定义的位置
*/
static void funcdefn(int sclass, char *id, Type ty, Symbol params[], Coordinate pt) {
int i, n;
Symbol *callee, *caller, p;
Type rty = freturn(ty);//处理函数返回的类型
if (isstruct(rty) && rty->size == 0)//什么情况下会出现返回值类型为结构,但是size为0 ???
error("illegal use of incomplete type `%t'\n", rty);
for (n = 0; params[n]; n++)
;
//然后设置参数列表结束位置
if (n > 0 && params[n-1]->name == NULL)
params[--n] = NULL;
//参数过多告警
if (Aflag >= 2 && n > 31)
warning("more than 31 parameters in function `%s'\n", id);
//下面代码是生成旧风格和新风格的参数callee和caller,
//第一个是传入函数的参数列表,第二个是返回给调用函数的参数列表
if (ty->u.f.oldstyle) {
if (Aflag >= 1)
warning("old-style function definition for `%s'\n", id);
caller = params;
callee = newarray(n + 1, sizeof *callee, FUNC);
memcpy(callee, caller, (n+1)*sizeof *callee);
enterscope();
assert(level == PARAM);
while (kind[t] == STATIC || istypename(t, tsym))
decl(dclparam);
foreach(identifiers, PARAM, oldparam, callee);
for (i = 0; (p = callee[i]) != NULL; i++) {
if (!p->defined)
callee[i] = dclparam(0, p->name, inttype, &p->src);
*caller[i] = *p;
caller[i]->sclass = AUTO;
caller[i]->type = promote(p->type);
}
p = lookup(id, identifiers);
if (p && p->scope == GLOBAL && isfunc(p->type)
&& p->type->u.f.proto) {
Type *proto = p->type->u.f.proto;
for (i = 0; caller[i] && proto[i]; i++) {
Type ty = unqual(proto[i]);
if (eqtype(isenum(ty) ? ty->type : ty,
unqual(caller[i]->type), 1) == 0)
break;
else if (isenum(ty) && !isenum(unqual(caller[i]->type)))
warning("compatibility of `%t' and `%t' is compiler dependent\n",
proto[i], caller[i]->type);
}
if (proto[i] || caller[i])
error("conflicting argument declarations for function `%s'\n", id);
}
else {
Type *proto = newarray(n + 1, sizeof *proto, PERM);
if (Aflag >= 1)
warning("missing prototype for `%s'\n", id);
for (i = 0; i < n; i++)
proto[i] = caller[i]->type;
proto[i] = NULL;
ty = func(rty, proto, 1);
}
}//endof if (ty->u.f.oldstyle)
else {
callee = params;
caller = newarray(n + 1, sizeof *caller, FUNC);
for (i = 0; (p = callee[i]) != NULL && p->name; i++) {
NEW(caller[i], FUNC);
*caller[i] = *p;
if (isint(p->type))
caller[i]->type = promote(p->type);
caller[i]->sclass = AUTO;
if ('1' <= *p->name && *p->name <= '9')
error("missing name for parameter %d to function `%s'\n", i + 1, id);
}
caller[i] = NULL;
}
//判断参数传送的类型是否出错
for (i = 0; (p = callee[i]) != NULL; i++)
if (p->type->size == 0) {
error("undefined size for parameter `%t %s'\n",
p->type, p->name);
caller[i]->type = p->type = inttype;
}
//处理main函数定义出错的处理
if (Aflag >= 2 && sclass != STATIC && strcmp(id, "main") == 0) {
if (ty->u.f.oldstyle)
warning("`%t %s()' is a non-ANSI definition\n", rty, id);
else if (!(rty == inttype
&& (n == 0 && callee[0] == NULL
|| n == 2 && callee[0]->type == inttype
&& isptr(callee[1]->type) && callee[1]->type->type == charptype
&& !variadic(ty))))
warning("`%s' is a non-ANSI definition\n", typestring(ty, id));
}
//上面的代码是判断函数是否重复声明
p = lookup(id, identifiers);
if (p && isfunc(p->type) && p->defined)
error("redefinition of `%s' previously defined at %w\n",
p->name, &p->src);
//处理函数全局定义
cfunc = dclglobal(sclass, id, ty, &pt);
//保存函数的属性
cfunc->u.f.label = genlabel(1);
cfunc->u.f.callee = callee;
cfunc->u.f.pt = src;
cfunc->defined = 1;
if (xref)
use(cfunc, cfunc->src);
if (Pflag)
printproto(cfunc, cfunc->u.f.callee);
if (ncalled >= 0)
ncalled = findfunc(cfunc->name, pt.file);
//准备生成代码
labels = table(NULL, LABELS);
stmtlabs = table(NULL, LABELS);
refinc = 1.0;
regcount = 0;
codelist = &codehead;
codelist->next = NULL;
if (!IR->wants_callb && isstruct(rty))
retv = genident(AUTO, ptr(unqual(rty)), PARAM);
compound(0, NULL, 0);//分析函数定义里的复合语句
definelab(cfunc->u.f.label);
if (events.exit)
apply(events.exit, cfunc, NULL);
walk(NULL, 0, 0);
exitscope();
assert(level == PARAM);
foreach(identifiers, level, checkref, NULL);
if (!IR->wants_callb && isstruct(rty)) {
Symbol *a;
a = newarray(n + 2, sizeof *a, FUNC);
a[0] = retv;
memcpy(&a[1], callee, (n+1)*sizeof *callee);
callee = a;
a = newarray(n + 2, sizeof *a, FUNC);
NEW(a[0], FUNC);
*a[0] = *retv;
memcpy(&a[1], caller, (n+1)*sizeof *callee);
caller = a;
}
if (!IR->wants_argb)
for (i = 0; caller[i]; i++)
if (isstruct(caller[i]->type)) {
caller[i]->type = ptr(caller[i]->type);
callee[i]->type = ptr(callee[i]->type);
caller[i]->structarg = callee[i]->structarg = 1;
}
if (glevel > 1) for (i = 0; callee[i]; i++) callee[i]->sclass = AUTO;
if (cfunc->sclass != STATIC)
(*IR->export)(cfunc);
if (glevel && IR->stabsym) {
swtoseg(CODE); (*IR->stabsym)(cfunc); }
swtoseg(CODE);
(*IR->function)(cfunc, caller, callee, cfunc->u.f.ncalls);
if (glevel && IR->stabfend)
(*IR->stabfend)(cfunc, lineno);
foreach(stmtlabs, LABELS, checklab, NULL);
exitscope();
expect('}');
labels = stmtlabs = NULL;
retv = NULL;
cfunc = NULL;
}
static void oldparam(Symbol p, void *cl) {
int i;
Symbol *callee = cl;
for (i = 0; callee[i]; i++)
if (p->name == callee[i]->name) {
callee[i] = p;
return;
}
error("declared parameter `%s' is missing\n", p->name);
}
void compound(int loop, struct swtch *swp, int lev) {
Code cp;
int nregs;
//复位一些前面使用过的全局变量,以便后面可以正确地分析
walk(NULL, 0, 0);//这步实际上什么都没干啊 ?????
//创建代码开始块cp,并保存这块代码块到代码列表codelist里
cp = code(Blockbeg);
enterscope();
assert(level >= LOCAL);
if (level == LOCAL && events.entry)
apply(events.entry, cfunc, NULL);
definept(NULL);
//测试是否复合语句的开始,如果是,获取下一个词素
expect('{');
autos = registers = NULL;
if (level == LOCAL && IR->wants_callb
&& isstruct(freturn(cfunc->type))) {
retv = genident(AUTO, ptr(unqual(freturn(cfunc->type))), level);
retv->defined = 1;
retv->ref = 1;
registers = append(retv, registers);
}
//局部变量声明处理
while (kind[t] == CHAR || kind[t] == STATIC
|| istypename(t, tsym) && getchr() != ':')
decl(dcllocal);//递归地调用decl来处理声明
{//处理自动类型和寄存器类型的局部变量
int i;
Symbol *a = ltov(&autos, STMT);
nregs = length(registers);
for (i = 0; a[i]; i++)
registers = append(a[i], registers);
cp->u.block.locals = ltov(®isters, FUNC);
}
if (events.blockentry)//定义了事件响应处理
apply(events.blockentry, cp->u.block.locals, NULL);
while (kind[t] == IF || kind[t] == ID)
statement(loop, swp, lev);//递归调用语句分析函数statement
walk(NULL, 0, 0);//复位所有使用全部变量
foreach(identifiers, level, checkref, NULL);//统计变量的引用计数
{
int i = nregs, j;
Symbol p;
for ( ; (p = cp->u.block.locals[i]) != NULL; i++) {
for (j = i; j > nregs
&& cp->u.block.locals[j-1]->ref < p->ref; j--)
cp->u.block.locals[j] = cp->u.block.locals[j-1];
cp->u.block.locals[j] = p;
}
}
if (level == LOCAL) {//处理局部函数返回值
Code cp;
for (cp = codelist; cp->kind < Label; cp = cp->prev)
;
if (cp->kind != Jump) {//如果代码的类型不是跳转,就需生成返回代码
if (freturn(cfunc->type) != voidtype) {
warning("missing return value\n");
retcode(cnsttree(inttype, 0L));
} else
retcode(NULL);
}
}
//保存复合语句的代码属性,就处理完成了复合语句
if (events.blockexit)
apply(events.blockexit, cp->u.block.locals, NULL);
cp->u.block.level = level;
cp->u.block.identifiers = identifiers;
cp->u.block.types = types;
code(Blockend)->u.begin = cp;
if (reachable(Gen))
definept(NULL);
if (level > LOCAL) {
exitscope();
expect('}');
}
}
static void checkref(Symbol p, void *cl) {
if (p->scope >= PARAM
&& (isvolatile(p->type) || isfunc(p->type)))
p->addressed = 1;
if (Aflag >= 2 && p->defined && p->ref == 0) {
if (p->sclass == STATIC)
warning("static `%t %s' is not referenced\n",
p->type, p->name);
else if (p->scope == PARAM)
warning("parameter `%t %s' is not referenced\n",
p->type, p->name);
else if (p->scope >= LOCAL && p->sclass != EXTERN)
warning("local `%t %s' is not referenced\n",
p->type, p->name);
}
if (p->sclass == AUTO
&& (p->scope == PARAM && regcount == 0
|| p->scope >= LOCAL)
&& !p->addressed && isscalar(p->type) && p->ref >= 3.0)
p->sclass = REGISTER;
if (level == GLOBAL && p->sclass == STATIC && !p->defined
&& isfunc(p->type) && p->ref)
error("undefined static `%t %s'\n", p->type, p->name);
assert(!(level == GLOBAL && p->sclass == STATIC && !p->defined && !isfunc(p->type)));
}
/*
局部函数和变量的声明处理
*/
static Symbol dcllocal(int sclass, char *id, Type ty, Coordinate *pos) {
Symbol p, q;
if (sclass == 0)
sclass = isfunc(ty) ? EXTERN : AUTO;
else if (isfunc(ty) && sclass != EXTERN) {
error("invalid storage class `%k' for `%t %s'\n",
sclass, ty, id);
sclass = EXTERN;
} else if (sclass == REGISTER
&& (isvolatile(ty) || isstruct(ty) || isarray(ty))) {
warning("register declaration ignored for `%t %s'\n",
ty, id);
sclass = AUTO;
}
q = lookup(id, identifiers);
if (q && q->scope >= level
|| q && q->scope == PARAM && level == LOCAL)
if (sclass == EXTERN && q->sclass == EXTERN
&& eqtype(q->type, ty, 1))
ty = compose(ty, q->type);
else
error("redeclaration of `%s' previously declared at %w\n", q->name, &q->src);
assert(level >= LOCAL);
p = install(id, &identifiers, level, sclass == STATIC || sclass == EXTERN ? PERM : FUNC);
p->type = ty;
p->sclass = sclass;
p->src = *pos;
switch (sclass) {
case EXTERN: q = lookup(id, globals);
if (q == NULL || q->sclass == TYPEDEF || q->sclass == ENUM) {
q = lookup(id, externals);
if (q == NULL) {
q = install(p->name, &externals, GLOBAL, PERM);
q->type = p->type;
q->sclass = EXTERN;
q->src = src;
(*IR->defsymbol)(q);
}
}
if (!eqtype(p->type, q->type, 1))
warning("declaration of `%s' does not match previous declaration at %w\n", q->name, &q->src);
p->u.alias = q; break;
case STATIC: (*IR->defsymbol)(p);
initglobal(p, 0);
if (!p->defined)
if (p->type->size > 0) {
defglobal(p, BSS);
(*IR->space)(p->type->size);
} else
error("undefined size for `%t %s'\n",
p->type, p->name);
p->defined = 1; break;
case REGISTER: registers = append(p, registers);
regcount++;
p->defined = 1;
break;
case AUTO: autos = append(p, autos);
p->defined = 1;
if (isarray(ty))
p->addressed = 1; break;
default: assert(0);
}
if (t == '=') {
Tree e;
if (sclass == EXTERN)
error("illegal initialization of `extern %s'\n", id);
t = gettok();
definept(NULL);
if (isscalar(p->type)
|| isstruct(p->type) && t != '{') {
if (t == '{') {
t = gettok();
e = expr1(0);
expect('}');
} else
e = expr1(0);
} else {
Symbol t1;
Type ty = p->type, ty1 = ty;
while (isarray(ty1))
ty1 = ty1->type;
if (!isconst(ty) && (!isarray(ty) || !isconst(ty1)))
ty = qual(CONST, ty);
t1 = genident(STATIC, ty, GLOBAL);
initglobal(t1, 1);
if (isarray(p->type) && p->type->size == 0
&& t1->type->size > 0)
p->type = array(p->type->type,
t1->type->size/t1->type->type->size, 0);
e = idtree(t1);
}
walk(root(asgn(p, e)), 0, 0);
p->ref = 1;
}
if (!isfunc(p->type) && p->defined && p->type->size <= 0)
error("undefined size for `%t %s'\n", p->type, id);
return p;
}
void finalize(void) {
foreach(externals, GLOBAL, doextern, NULL);
foreach(identifiers, GLOBAL, doglobal, NULL);
foreach(identifiers, GLOBAL, checkref, NULL);
foreach(constants, CONSTANTS, doconst, NULL);
}
static void doextern(Symbol p, void *cl) {
(*IR->import)(p);
}
static void doglobal(Symbol p, void *cl) {
if (!p->defined && (p->sclass == EXTERN
|| isfunc(p->type) && p->sclass == AUTO))
(*IR->import)(p);
else if (!p->defined && !isfunc(p->type)
&& (p->sclass == AUTO || p->sclass == STATIC)) {
if (isarray(p->type)
&& p->type->size == 0 && p->type->type->size > 0)
p->type = array(p->type->type, 1, 0);
if (p->type->size > 0) {
defglobal(p, BSS);
(*IR->space)(p->type->size);
if (glevel > 0 && IR->stabsym)
(*IR->stabsym)(p);
} else
error("undefined size for `%t %s'\n",
p->type, p->name);
p->defined = 1;
}
if (Pflag
&& !isfunc(p->type)
&& !p->generated && p->sclass != EXTERN)
printdecl(p, p->type);
}
void doconst(Symbol p, void *cl) {
if (p->u.c.loc) {
assert(p->u.c.loc->u.seg == 0);
defglobal(p->u.c.loc, LIT);
if (isarray(p->type) && p->type->type == widechar) {
unsigned int *s = p->u.c.v.p;
int n = p->type->size/widechar->size;
while (n-- > 0) {
Value v;
v.u = *s++;
(*IR->defconst)(widechar->op, widechar->size, v);
}
} else if (isarray(p->type))
(*IR->defstring)(p->type->size, p->u.c.v.p);
else
(*IR->defconst)(p->type->op, p->type->size, p->u.c.v);
p->u.c.loc = NULL;
}
}
void checklab(Symbol p, void *cl) {
if (!p->defined)
error("undefined label `%s'\n", p->name);
p->defined = 1;
}
Type enumdcl(void) {
char *tag;
Type ty;
Symbol p;
Coordinate pos;
t = gettok();
pos = src;
if (t == ID) {
tag = token;
t = gettok();
} else
tag = "";
if (t == '{') {
static char follow[] = { IF, 0 };
int n = 0;
long k = -1;
List idlist = 0;
ty = newstruct(ENUM, tag);
t = gettok();
if (t != ID)
error("expecting an enumerator identifier\n");
while (t == ID) {//循环处理枚举中的每一个字段
char *id = token;
Coordinate s;
if (tsym && tsym->scope == level)
error("redeclaration of `%s' previously declared at %w\n",
token, &tsym->src);
s = src;
t = gettok();
if (t == '=') {//处理赋值枚举
t = gettok();
k = intexpr(0, 0);
} else {
if (k == inttype->u.sym->u.limits.max.i)
error("overflow in value for enumeration constant `%s'\n", id);
k++;
}
//装入id符号表
p = install(id, &identifiers, level, level < LOCAL ? PERM : FUNC);
p->src = s;
p->type = ty;
p->sclass = ENUM;
p->u.value = k;
idlist = append(p, idlist);//将p加入链表
n++;
if (Aflag >= 2 && n == 128)//枚举字段超过限定值
warning("more than 127 enumeration constants in `%t'\n", ty);
if (t != ',')
break;
t = gettok();//获取下一个词素,用于下一轮循环
if (Aflag >= 2 && t == '}')
warning("non-ANSI trailing comma in enumerator list\n");
}
test('}', follow);//声明结束
ty->type = inttype;
ty->size = ty->type->size;
ty->align = ty->type->align;
ty->u.sym->u.idlist = ltov(&idlist, PERM);
ty->u.sym->defined = 1;
}
else if ((p = lookup(tag, types)) != NULL && p->type->op == ENUM) {
ty = p->type;
if (t == ';')
error("empty declaration\n");
} else {
error("unknown enumeration `%s'\n", tag);
ty = newstruct(ENUM, tag);
ty->type = inttype;
}
if (*tag && xref)
use(p, pos);
return ty;
}
Type typename(void) {
Type ty = specifier(NULL);
if (t == '*' || t == '(' || t == '[') {
ty = dclr(ty, NULL, NULL, 1);
if (Aflag >= 1 && !hasproto(ty))
warning("missing prototype\n");
}
return ty;
}
本文深入解析LCC编译器中负责处理声明的复杂模块Decl.c。该模块作为语法分析的一部分,详细介绍了如何处理各种类型的声明,包括全局变量、函数参数等,并涉及类型解析、函数定义及代码生成等内容。
2613

被折叠的 条评论
为什么被折叠?



