【问题标题】:What is the code generator for computed goto?计算 goto 的代码生成器是什么?
【发布时间】:2021-02-15 12:37:29
【问题描述】:
计算 goto 示例:
...
GO TO ( 10, 20, 30, 40 ), N
...
10 CONTINUE
...
20 CONTINUE
...
40 CONTINUE
如果 N 等于 1,则转到 10。
如果 N 等于 2,则转到 20。
如果 N 等于 3,则转到 30。
如果 N 等于 4,则转到 40。
goto最后编译状态的代码生成器是什么?
【问题讨论】:
标签:
compilation
compiler-construction
【解决方案1】:
编译计算goto最常用的方法是静态跳转表和间接分支指令。例如(没有 -fPIC):
int test(int num) {
const void * const labels[] = {&&a, &&b, &&cl};
goto *labels[num];
a: return 1;
b: return 2;
cl: return 3;
}
将被编译为:
test(int): # @test(int)
movsxd rax, edi
jmp qword ptr [8*rax + .L__const.test(int).labels]
.Ltmp0: # Block address taken
mov eax, 1
ret
.Ltmp1: # Block address taken
mov eax, 3
ret
.Ltmp2: # Block address taken
mov eax, 2
ret
.L__const.test(int).labels:
.quad .Ltmp0
.quad .Ltmp2
.quad .Ltmp1