gpt4 book ai didi

c - GCC 避免将分支编译为链接寄存器 (blr) 语句

转载 作者:太空宇宙 更新时间:2023-11-04 08:02:01 29 4
gpt4 key购买 nike

我正在编写 C 代码并为 PowerPC 架构编译它。我想告诉编译器将所有类似返回的指令替换为函数代码末尾的分支。这是由于一些奇怪的要求,我不能在这段代码中使用任何 return 来确保之后执行更多的汇编行。

例如,我将C代码编译成如下汇编代码:

lis r9,4096
lis r8,255
lwz r10,0(r9)
ori r8,r8,65535
addi r9,r10,192
cmplw cr7,r9,r8
blelr- cr7 # Return if the cr7 register is "less than or equal"
lis r8,512
cmplw cr7,r9,r8
bgtlr- cr7 # Return if the cr7 register is "greater than or equal"
lwz r10,192(r10)
lis r8,303
ori r8,r8,65535
addi r9,r10,320
cmplw cr7,r9,r8
blelr- cr7 # Return if the cr7 register is "less than or equal"
lis r8,528
cmplw cr7,r9,r8
bgtlr- cr7 # Return if the cr7 register is "greater than or equal"
lwz r10,320(r10)
lis r8,287
ori r8,r8,65535
subi r9,r10,448
cmplw cr7,r9,r8
blelr- cr7
lis r8,544
cmplw cr7,r9,r8
bgtlr- cr7 # Return if the cr7 register is "greater than or equal"
lis r9,4919
ori r9,r9,4919
stw r9,-448(r10)
blr # Return

我想要的是将所有类似返回的语句替换为函数代码末尾的始终分支,如下所示:

lis r9,4096
lis r8,255
lwz r10,0(r9)
ori r8,r8,65535
addi r9,r10,192
cmplw cr7,r9,r8
ble _END # Branch to the _END label if "less than"
lis r8,512
cmplw cr7,r9,r8
bgt _END # Branch to the _END label if "greater than"
lwz r10,192(r10)
lis r8,303
ori r8,r8,65535
addi r9,r10,320
cmplw cr7,r9,r8
ble cr7 # Branch to the _END label if "less than"
lis r8,528
cmplw cr7,r9,r8
bgt _END # Branch to the _END label if "greater than"
lwz r10,320(r10)
lis r8,287
ori r8,r8,65535
subi r9,r10,448
cmplw cr7,r9,r8
blelr- cr7
lis r8,544
cmplw cr7,r9,r8
bgt _END # Branch to the _END label if "greater than"
lis r9,4919
ori r9,r9,4919
stw r9,-448(r10)
_END:
blr # I guess it should stay otherwise the function call will continue executing "random" garbage such as the next function in the .text section. Via post-processing this would be easy to strip though!

有什么方法可以自动执行此编译首选项?

最佳答案

@Jester 和@fuz 的评论给出了答案,我就写下来

你需要:

  1. 避免在 C 代码中的函数中间返回。因此,将 return 替换为 goto
  2. 防止编译器发现 goto end;return; 是同一回事。内联汇编是告诉编译器“不要搞砸这个”的好方法。

一些宏:

#define RETURN goto _end
#define END _end: asm volatile("")

void foo(int x) {
如果(x==1)返回;
如果(x==2)返回;
printf("你好,世界\n");
结尾;
}

asm volatile("") 指示编译器插入一些汇编代码。编译器不知道这段汇编代码的作用,因此它无法进行任何会跳过它的优化(即使它是 0 条指令)。

关于c - GCC 避免将分支编译为链接寄存器 (blr) 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45764088/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com