本项目是一个类C0语言的编译器。特性如下:
- 扩充C0文法,包含了常量、变量定义,支持条件、循环、开关等多种语句,支持函数递归调用
- 目标代码格式是x86汇编代码,可经MASM汇编器生成Win32可执行文件
- 实现了一些代码优化,如公共子表达式删除(DAG图)、数据流分析、全局寄存器分配等
- 编译器代码本身平台无关
扩充C0语言文法定义见:docs/designed_BNF.txt,详细设计文档:docs/designing_report.doc。
可以直接打开在Code::Blocks中打开hcc.cbp
项目文件,在编译器设置里勾选C++11支持,然后单击build按钮。
或者可以直接在命令行中输入:
g++ -std=c++11 -c actvar.cpp -o obj\Debug\actvar.o
g++ -std=c++11 -c genasm.cpp -o obj\Debug\genasm.o
g++ -std=c++11 -c lex.cpp -o obj\Debug\lex.o
g++ -std=c++11 -c main.cpp -o obj\Debug\main.o
g++ -std=c++11 -c opt.cpp -o obj\Debug\opt.o
g++ -std=c++11 -c opt_dag.cpp -o obj\Debug\opt_dag.o
g++ -std=c++11 -c semantics.cpp -o obj\Debug\semantics.o
g++ -std=c++11 -c syntax.cpp -o obj\Debug\syntax.o
g++ -std=c++11 -c util.cpp -o obj\Debug\util.o
g++ -o bin\Debug\hcc.exe obj\Debug\*.o
tests
目录中包含了一些测试样例。例如运行bin\Debug\hcc.exe tests\3.fibo.c
即可输出对应x86汇编代码。
hcc.exe
的用法如下
Usage: hcc [-c FILENAME|-l FILENAME|-s FILENAME|
-t FILENAME] [-o] [-D] [-R] [-r] [-d] SOURCEFILE
Read extended C0 source code from SOURCEFILE file and compile
to MASM source code. The generated code will be output into
std-out by default.
-c FILENAME
Output the generated code to FILENAME. Assign FILENAME as - to
indicate std-out.
-l FILENAME
Only do lexical analysis and output the result to FILENAME.
Assign FILENAME as - to indicate std-out.
-s FILENAME
Only do syntax analysis and output the result to FILENAME.
Assign FILENAME as - to indicate std-out.
-t FILENAME
Do semantic analysis and dump the relevant tables to FILENAME.
Assign FILENAME as - to indicate std-out.
-o Perform optimization.
-D Do NOT use DAG to simplify repeated expression.
-R Do NOT use reduce code using data flow, redundant deletion, copy-.
spread, or constant combination.
-r Use global register.
-d Output verbose debug information.
This is a compiler for a C0-like language. Features include:
- Extended C0 syntax, including constant and variable definitions, supporting multiple statements such as conditions, loops, switches, and supporting recursive function calls
- The target code is x86 assembly, and Win32 executable file can be generated by MASM assembler
- Some code optimizations have been implemented, such as common sub-expression deletion (DAG graph), data flow analysis, global register allocation, etc.
- The compiler code itself is platform independent
The language definition can be found in docs/designed_BNF.txt. A full documentation (in Chinese) can be found in docs/designing_report.doc.
You can directly open hcc.cbp
in Code::Blocks, check C++11 standard support in "Compiler settings", and click build button.
Or, you can run following commands in your shell.
g++ -std=c++11 -c actvar.cpp -o obj\Debug\actvar.o
g++ -std=c++11 -c genasm.cpp -o obj\Debug\genasm.o
g++ -std=c++11 -c lex.cpp -o obj\Debug\lex.o
g++ -std=c++11 -c main.cpp -o obj\Debug\main.o
g++ -std=c++11 -c opt.cpp -o obj\Debug\opt.o
g++ -std=c++11 -c opt_dag.cpp -o obj\Debug\opt_dag.o
g++ -std=c++11 -c semantics.cpp -o obj\Debug\semantics.o
g++ -std=c++11 -c syntax.cpp -o obj\Debug\syntax.o
g++ -std=c++11 -c util.cpp -o obj\Debug\util.o
g++ -o bin\Debug\hcc.exe obj\Debug\*.o
There is some test samples intests
directory. For example, run bin\Debug\hcc.exe tests\3.fibo.c
to get the x86 assembly code.
The usage of hcc.exe
:
Usage: hcc [-c FILENAME|-l FILENAME|-s FILENAME|
-t FILENAME] [-o] [-D] [-R] [-r] [-d] SOURCEFILE
Read extended C0 source code from SOURCEFILE file and compile
to MASM source code. The generated code will be output into
std-out by default.
-c FILENAME
Output the generated code to FILENAME. Assign FILENAME as - to
indicate std-out.
-l FILENAME
Only do lexical analysis and output the result to FILENAME.
Assign FILENAME as - to indicate std-out.
-s FILENAME
Only do syntax analysis and output the result to FILENAME.
Assign FILENAME as - to indicate std-out.
-t FILENAME
Do semantic analysis and dump the relevant tables to FILENAME.
Assign FILENAME as - to indicate std-out.
-o Perform optimization.
-D Do NOT use DAG to simplify repeated expression.
-R Do NOT use reduce code using data flow, redundant deletion, copy-.
spread, or constant combination.
-r Use global register.
-d Output verbose debug information.