2016春季学期系统方向综合实验组成部分
将类 C 的高级语言编译生成 x86 二进制可执行文件,由 NEMU 加载运行。
- 增加预处理机制
- 增加对全局变量的支持
- 增加对函数声明的支持
The code is derived from Wonicon's compiler lab code in school. This NCC generates x86 assembly codes in AT&T format instead of MIP32 ones.
Some significant limitations are shown below:
- No declaration, all functions should be defined before called;
- No global variables (parsed but cannot generate code);
- No preprocessor;
- Basic type only consists of int (float is parsed but cannot generate code);
- All local variables should be defined at the beginning of a function;
- Struct is passed by reference, and returning a struct is dangerous.
The top level of a program should look like this:
// one-line comment
/**
* multi-line comment
* can be /* nested */
* ignore everything after //
* like // */
* like // /*
* like // *//*
* like // /*_*/
*/ // This is the end of this multi-line commet!
// int read()
// write(int x)
// are provided functions to support input and output
int echo(int e)
{
write(e);
return read();
}
struct A {
int a;
int b;
};
int main()
{
/* Local definitions, like follows */
int foo;
int bar = 3;
int vec[2];
char hello[5];
struct A a;
/* character array */
hello[0] = 'h';
hello[1] = 'e';
hello[2] = 'l';
hello[3] = 'k' + 1; //char and calculate with int
hello[4] = 'o';
/* Statements, like follows */
if (/* Expression */) {
/* Statements */
}
else if (/* Expression */) {
/* Statements */
}
else {
/* Statements */
}
while (/* Expression */) {
/* Statements */
}
// the three parts of for-loop cannot be omitted currently
for (foo = 0; foo < 5; foo = foo + 1) {
writec(hello[foo]);
}
/* Expressions */
foo = bar; // Assignment
foo + 1; // Binary operation: +, -, *, /
foo < bar; // Comparing operation: <, <=, >, >=, ==, !=
!foo; // Unary operation: -, !
a.a // field access
(foo);
foo = echo(vec[0]); // Function call, array access
return 0;
}