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 所示。

编译器开发系列--Ocelot语言7.中间代码

编译器开发系列--Ocelot语言7.中间代码

所有语句的节点都继承自Stmt 类,表达式的节点继承自Expr 类。

相关文章:

  • 2022-12-23
  • 2021-08-08
  • 2021-11-24
  • 2022-01-06
  • 2021-07-18
  • 2021-05-26
  • 2022-12-23
猜你喜欢
  • 2021-11-10
  • 2021-10-04
  • 2022-03-04
  • 2021-06-14
  • 2021-09-16
  • 2021-09-23
  • 2022-12-23
相关资源
相似解决方案