Ocelot的中间代码是仿照国外编译器相关图书Modern Compiler Implementation 中所使用的名为Tree 的中间代码设计的。顾名思义,Tree 是一种树形结构,其特征是简单,而且方便转换为机器语言。
例如以下代码:
int main(int argc, char** argv)
{
return ++argc;
}
会被转换成如下的中间代码:
<<IR>> (G:\编译原理\自制编译器\源码\test\hello_ir.cb:1)
variables:
functions:
<<DefinedFunction>> (G:\编译原理\自制编译器\源码\test\hello_ir.cb:1)
name: main
isPrivate: false
type: int(int, char**)
body:
<<Assign>> (G:\编译原理\自制编译器\源码\test\hello_ir.cb:3)
lhs:
<<Addr>>
type: INT32
entity: argc
rhs:
<<Bin>>
type: INT32
op: ADD
left:
<<Var>>
type: INT32
entity: argc
right:
<<Int>>
type: INT32
value: 1
<<Return>> (G:\编译原理\自制编译器\源码\test\hello_ir.cb:3)
expr:
<<Var>>
type: INT32
entity: argc
组成中间代码的类如表11.1 所示。
所有语句的节点都继承自Stmt 类,表达式的节点继承自Expr 类。