从三个地址代码到JVM字节码的代码生成 [英] Code generation from three address code to JVM bytecode

查看:130
本文介绍了从三个地址代码到JVM字节码的代码生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Renjin(JVM的R)开发字节码编译器,并且正在尝试将我们的中间三地址代码(TAC)表示形式转换为字节码。我参考过的所有有关编译器的教科书都讨论了代码生成过程中的寄存器分配,但是我还没有找到任何资源来在基于堆栈的虚拟机(如JVM)上进行代码生成。

I'm working on the byte code compiler for Renjin (R for the JVM) and am experimenting with translating our intermediate three address code (TAC) representation to byte code. All the textbooks on compilers that I've consulted discuss register allocation during code generation, but I haven't been able to find any resources for code generation on stack-based virtual machines like the JVM.

简单的TAC指令可以很容易地转换为字节码,但是当涉及到临时指令时,我会有些迷失。

Simple TAC instructions are trivial to translate into bytecode, but I get a bit lost when temporaries are involved. Does any one have any pointers to resources that describe this?

以下是一个完整的示例:

Here is a complete example:

原始R代码如下所示:

x + sqrt(x * y)

TAC IR:

 0:  _t2 := primitive<*>(x, y)
 1:  _t3 := primitive<sqrt>(_t2)
 2:  return primitive<+>(x, _t3)

(忽略一下我们有时无法始终在编译时解析对基元的函数调用的事实)

(ignore for a second the fact taht we can't always resolve function calls to primitives at compile time)

生成的JVM字节代码看起来(大致)如下:

The resulting JVM byte code would look (roughly) something like this:

aload_x 
dup
aload_y
invokestatic r/primitives/Ops.multiply(Lr/lang/Vector;Lr/lang/Vector;)
invokestatic r/primitives/Ops.sqrt(Lr/lang/Vector;)
invokestatic r/primitives/Ops.plus(Lr/lang/Vector;Lr/lang/Vector;)
areturn

基本上,在程序顶部,我已经需要思考ng到我到达TAC指令2时,我将需要在堆栈的开始处使用局部变量x。我可以通过手动方式进行思考,但无法通过算法来正确地进行操作。

Basically, at the top of the program, I already need to be thinking that I'm going to need local variable x at the beginning of the stack by the time that i get to TAC instruction 2. I can think this through manually but I'm having trouble thinking through an algorithm to do this correctly. Any pointers?

推荐答案

将3地址表示转换为堆栈要比将1地址表示转换为3地址容易。

Transforming a 3-address representation into stack is easier than a stack one into 3-address.

您的顺序应为:


  1. 形成基本块

  2. 执行SSA转换

  3. 在基本块内构建表达式树

  4. 执行寄存器调度(同时进行phi删除)为上一步未消除的寄存器分配局部变量

  5. 发出JVM代码-寄存器进入变量,表达式树被简单地扩展为堆栈操作

  1. Form basic blocks
  2. Perform an SSA-transform
  3. Build expression trees within the basic blocks
  4. Perform a register schedulling (and phi- removal simultaneously) to allocate local variables for the registers not eliminated by the previous step
  5. Emit a JVM code - registers goes into variables, expression trees are trivially expanded into stack operations

这篇关于从三个地址代码到JVM字节码的代码生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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