【发布时间】:2013-12-01 14:22:38
【问题描述】:
从给定的抽象语法树 (AST) 生成 register based 字节码的知名策略有哪些?
考虑这个表达式 1 + 2 - 3 * 4 / 5 及其 AST 形式:
bin_exp(-)
bin_exp(+)
num_exp(1)
num_exp(2)
bin_exp(/)
bin_exp(*)
num_exp(3)
num_exp(4)
num_exp(5)
我正在努力通过程序将 AST 转换为相应的字节码。 到目前为止,我只找到了一个article,其中只是简单地谈到了它。我对它想要表达的内容的解释......
int ridx; // register index
function visit_exp(exp)
{
switch (exp)
{
case bin_exp:
visit_exp(exp.left);
visit_exp(exp.right);
printf("add %i, %i -> %i\n", ridx - 2, ridx - 1, ridx);
// save ridx, as it contains the result
break;
case num_exp:
printf("mov %i -> %i\n", ridx, exp.value);
break;
}
}
请帮帮我,谢谢。
【问题讨论】:
-
这有什么难的?如果您不关心优化,那是微不足道的。
标签: compiler-construction code-generation bytecode abstract-syntax-tree