如何将三地址代码(TAC)转换为Java字节码? [英] How to translate Three Address Code(TAC) to Java Bytecode?

查看:126
本文介绍了如何将三地址代码(TAC)转换为Java字节码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个简单的三地址代码文件转换为Java字节码。已经有一些与此主题相关的问题,但是没有得到回答 问题超出了我的期望。

I would like to translate a plain Three Address Code file to Java Bytecode. There are some questions related to this topic already, but either they are not answered properly or the question goes way beyond what I'm looking for.

以这段代码段为例,该段代码是在《龙书》中使用编译器的前端生成的:

Take for instance this segment of code, generated with the front end of the compiler available in the "Dragon Book":

L1:L3:  i = i + 1
L5:     t1 = i * 8
        t2 = a [ t1 ]
        if t2 < v goto L3
L4:     j = j - 1

字节码看起来如何?我需要重建符号表来进行翻译吗?如果有人能够像blackcompe这样在 answer (我知道JVM是一台堆栈机,而不是一台注册机)。

How would it look like in bytecode? Do I need to reconstruct the symbol table to do the translation? It would be really helpful if someone could describe it like blackcompe did in this answer(I know JVM is a stack machine, not a register one).

推荐答案

在这里,我将用字节码编写代码。但这只是做到这一点的一种方法,而且问题还很悬而未决。我假设除a以外的所有变量均为整数。如果它们是不同的类型,则所需的代码看起来显然会有所不同。

Here's how I would write your code in bytecode. But this is just one way to do it, and the question is pretty open ended. I'm assuming that all the variables are ints except for a. If they are different types, the required code would obviously look different.

; assume i, j, a, and v are in slots 0-3 respectively
L3: 
iinc 0 1
iload_0
bipush 8
imul
; store t1 in a variable for simplicity - you could simplify the code by eliminating the temporary
istore 4
aload_2
iload 4
iaload
istore 5
iload 5
iload_3
if_lt L3
iinc 1 -1

As提到,这是一个相当开放的问题。例如,上面的代码将临时变量明确存储到本地插槽(也称为寄存器)中,以便与代码完全匹配。但是您可以通过重新布置代码来避免出现如下所示的临时事件来简化代码

As mentioned, this is a pretty open ended question though. For example, the above code explicitly stores the temporary variables into local slots aka "registers" in order to match the code exactly. But you could simplify the code by rearranging things to avoid the temporaries as shown below

; assume i, j, a, and v are in slots 0-3 respectively
L3: 
iinc 0 1
aload_2
iload_0
bipush 8
imul
iaload
iload_3
if_lt L3
iinc 1 -1

这篇关于如何将三地址代码(TAC)转换为Java字节码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆