分析开源编译器GCC的内部结构及其可移植机制,提出GCC前后端分离的结构以适合移植到不同硬件平台.以Nios嵌入式处理器为例,研究了移植GCC的步骤以及必须的数据文件,机器描述文件、宏定义文件等.给出了算术逻辑运算指令、数据转移指令、控制指令这3种典型指令所对应的RTL表达式编写方法和模板.通过使用一个C程序对移植后的GCC编译器进行测试,结果表明,移植后的GCC能够产生正确的汇编代码。
This paper analyzed the internal architectures and retargeting mechanisms of GCC, and pointed out that GCC was very convenient to be retargeted because of its separate and relatively independent front end and back end. By using Nios, a processor for embedded system, as an example, this paper studied the retargeting process and the data files which must be provided to retarget GCC, including the Machine Description file and macro definition files. The method to compose RTL expressions, and the template examples for some typical instructions, such as arithmetic instructions, data transfer instructions and program flow control instructions, were presented. The retargeted GCC was tested by a C program. The result shows that it can generate correct assemble codes.