java - 面向对象的三地址代码生成

标签 java code-generation compiler-optimization compiler-construction

我正在从事与编译器设计相关的项目。我需要为基于 Java 的语言生成三个地址代码,这意味着使用对象和范围。我希望您能帮助我为以下示例生成 TAC(或让我引用教程):

class A {
    int x;
    String y;

    public A(int x, String y) {
       this.x = x;
       this.y = y;
    }
}

import A;

class B {
    int f;
    A foo;

    public B() {
       this.foo = null;
       this.f = -1;
    }

    public boolean createFoo() {
       this.foo = new A(0, "TAC Example");
       return true;
    } 

    public static void main() {
       B bar = new B();
       A baz = new A(666, "TAC generation");
       bar.createFoo();
       bar.foo.y = "Hello World";
       if(bar.foo.x == 666)
           return;
       bar.foo.x = baz.x;           
    }        
}

最佳答案

首先,您需要了解“对象布局”,或者换句话说,对象在运行时在内存 (RAM) 中的样子。这没有标准,但大多数编译器以类似的方式创建对象。我们假设执行将发生在 x86(32 位)机器上,因此指针为 4B 或 32 位,而对于 64 位(x64)机器,指针为 8B。所以对象“A”看起来像这样:“A”对象的前 4 个字节将是指向虚拟指针表的指针。接下来的 4 个字节或偏移量 4 到 8 将存储“int x”。偏移量 8 到 12 将存储指向“字符串 y”的指针。 A 类的虚拟指针表可以为空或 A 对象中偏移量 0 上的指针可以为 NULL - 这取决于编译器。至于“B”类情况类似。偏移量 0 将存储 VPT(虚拟指针表)的地址,偏移量 4“int f”和偏移量 8 指向“A foo”的指针。在 B 的 VPT 中,“createFoo”地址将存储在偏移量 0 处,因为它是 B 类中的唯一方法。现在让我们来实现:

_B.createFoo:
    BeginFunc 12    // stack frame size = 3 registers * sizeof( each_register )
    _t0 = 8         // size of A object
    PushParam _t0   // this is the memory ammount that we are asking
    _t1 = LCall _Alloc  // allocate memory
    PopParams 4     //  clear stack
    _t2 = A
    *(_t1) = _t2    // load VPT
    *(_t1 + 4) = 0  // initialize _A.x
    *(_t1 + 8) = "TAC Example"  // initialize _A.foo
    *(this + 8) = _t1
    Return 1
    EndFunc

现在让我们实现main:

_B.main:
    BeginFunc 68    // 15 * 4 + 2 * 4 
    _t0 = 8         // size of B object
    PushParam _t0   // memory amount that we need
    _t1 = LCall _Alloc  // allocate memory
    PopParams 4     //  clear stack
    _t2 = B
    *(_t1) = _t2    // load VPT
    *(_t1 + 4) = 0  // initialize _B.foo
    *(_t1 + 8) = -1 // initialize _B.f
    bar = _t1
    _t3 = 8         // size of A object
    PushParam _t3   // this is the memory ammount that we are asking
    _t4 = LCall _Alloc  // allocate memory
    PopParams 4     //  clear stack
    _t5 = A
    *(_t4) = _t5    // load VPT
    *(_t4 + 4) = 666    // initialize _A.x
    *(_t4 + 8) = "TAC generation"   // initialize _A.foo
    baz = _t4
    _t6 = *(bar)    // address of _B.VPT
    _t7 = *(_t6)    // address of _B.createFoo
    PushParam bar   // this for createFoo
    ACall _t7       // call _B.createFoo
    PopParams 4     // clear stack
    _t8 = *(bar + 8)    // get _B.foo
    _t9 = *(_t8 + 8)    // get _B.foo.y
    *(_t9) = "Hello world"  // set _B.foo.y value
    _t10 = *(bar + 8)   // get _B.foo
    _t11 = *(_t10 + 4)  // get _B.foo.x
    _t12 = _t11 == 666  // test _B.foo.x equal to 666
    IfZ _t12 GoTo _L0   // if not equal continue to _L0
    Return
    _L0:
    _t13 = *(bar + 8)   // get _B.foo
    _t14 = _t13 + 4     // get address of _B.foo.x
    _t15 = *(baz + 4)   // get _A.x
    *(_t14) = _t15      // set _B.foo.x
    EndFunc

如您所见,这并不难,但还有一些工作要做。希望这会有所帮助。

关于java - 面向对象的三地址代码生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16351461/

相关文章:

c# - 多重继承

java - Java 中的 ThreadLocal Vector 清理

c# - 确定性不变的 Actions 和 Funcs 是否由 JIT 内联?

编译器 -march 标志基准测试?

java - 在 Spring Boot 中处理嵌入式 Tomcat 异常

java - 在 MySQL 中搜索以 ","分隔的文本列中的多个字符串

c# - 格式化 C# 代码片段的文字参数

serialization - 戈朗 : print struct as it would appear in source code

code-generation - 是否可以添加使用 CodeFluent 实体片段?

f# - fsharp 编译器是否将冗余函数组合减少为空操作?