gpt4 book ai didi

assembly - LLVM 程序集 : assign integer constant to register

转载 作者:行者123 更新时间:2023-12-03 23:46:42 24 4
gpt4 key购买 nike

我在写 a compiler使用 LLVM 作为后端,我的编译器生成以下 LLVM 汇编代码

@0 = private constant [25 x i8] c"Hello World to %dntegers\00"

declare void @printf (i8*, i32)

define void @main () {
%1 = getelementptr [25 x i8]* @0, i32 0, i32 0
%2 = 1
tail call void @printf(i8* %1, i32 %2)
ret void
}

但我收到以下错误:
c.ll:8:8: error: expected instruction opcode
%2 = 1
^

文档显示 examples like that尽管。

你知道如何让这个工作吗?目前,我正在使用:
  %2 = add i32 0, 1 ; FIXME

最佳答案

参见 LLVMdev 讨论 "How to assign a constant to a register?"正如 Duncan Sands 指出的那样:

let me say that in general doing this is pointless. Due to SSA form, if %x is set to 5 you can't set it to something else later. Thus everywhere that you use %x you might as well just directly use 5 there instead. A common situation is that you have a register %x, and due to performing optimizations you discover that in fact %x will always have the value 5. You can use RAUW (aka the replaceAllUsesWith method) to replace %x with 5 everywhere.



该线程确实生成了几个直接使用常量的替代方法:
  • 使用 alloca获取一个指针,存储到它,然后根据需要加载该值。
  • 创建一个指向常量的全局变量,然后根据需要加载该值。

  • 有人指出 alloca方法最终将在优化后使用寄存器。

    无论如何,按照@SK-logic 的建议直接使用常量似乎是最干净的解决方案。

    关于assembly - LLVM 程序集 : assign integer constant to register,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6374355/

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