【问题标题】:Create local string using LLVM使用 LLVM 创建本地字符串
【发布时间】:2016-05-08 16:11:33
【问题描述】:

我正在尝试使用 LLVM 创建一个局部变量来存储字符串,但我的代码目前正在引发语法错误。

lli: test2.ll:8:23: error: constant expression type mismatch
  %1 = load [6 x i8]* c"hello\00"

我分配和存储字符串的 IR 代码:

@.string = private constant [4 x i8] c"%s\0A\00"

define void @main() {
entry:
  %a = alloca [255 x i8]
  %0 = bitcast [255 x i8]* %a to i8*
  %1 = load [6 x i8]* c"hello\00"
  %2 = bitcast [6 x i8]* %1 to i8*
  %3 = tail call i8* @strncpy(i8* %0, i8* %2, i64 255) nounwind
  %4 = getelementptr inbounds [6 x i8]* %a, i32 0, i32 0
  %5 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([4 x     i8]* @.string, i32 0, i32 0), i8* %4)
  ret void
}

declare i32 @printf(i8*, ...)
declare i8* @strncpy(i8*, i8* nocapture, i64) nounwind

使用 llc 我可以看到 llvm 实现的方式是分配和分配给全局变量,但我希望它是本地的(在基本块内)。下面的代码有效,但我不想创建这个 var "@.str"...

@str = global [1024 x i8] zeroinitializer, align 16
@.str = private unnamed_addr constant [6 x i8] c"hello\00", align 1
@.string = private constant [4 x i8] c"%s\0A\00"

define i32 @main() nounwind uwtable {
  %1 = tail call i8* @strncpy(i8* getelementptr inbounds ([1024 x i8]*     @str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8]* @.str,     i64 0, i64 0), i64 1024) nounwind
  %2 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([4 x i8]* @.string, i32 0, i32 0), i8* %1)
  ret i32 0
}

declare i8* @strncpy(i8*, i8* nocapture, i64) nounwind
declare i32 @printf(i8*, ...) #2

谢谢

【问题讨论】:

    标签: llvm llvm-ir


    【解决方案1】:

    在我之前的代码搞砸了之后,我自己想通了。

    下面是代码,有和我一样问题的朋友可以看看

    @.string = private constant [4 x i8] c"%s\0A\00"
    
    define void @main() {
    entry:
      %a = alloca [6 x i8]
      store [6 x i8] [i8 104,i8 101,i8 108,i8 108, i8 111, i8 0], [6 x i8]* %a
      %0 = bitcast [6 x i8]* %a to i8*
      %1 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([4 x i8]* @.string, i32 0, i32 0), i8* %0)
      ret void
    }
    
    declare i32 @printf(i8*, ...)
    

    基本上,我必须将每个字符单独存储在数组中,然后比特转换为 i8*,这样我才能使用 printf 函数。我无法使用 LLVM 网页 http://llvm.org/docs/LangRef.html#id669 中显示的 c" ... " 方法。这似乎是 IR 语言规范中的一个特例,它们需要在全局范围内。

    更新:我再次处理相同的代码,我发现最好的方法是存储一个常量而不是每个 i8 符号。所以第 6 行,将被替换为:

      store [6 x i8] c"hello\00", [6 x i8]* %0
    

    使用 llvm 生成代码更容易,可读性更强!

    【讨论】:

    • 如果字符串不是最小大小,您需要注意堆栈上字符串的大小。
    • 确实如此。不过,就我的目的而言,这就足够了。在我的程序中,我初始化为 255,然后 bitcast 为 6...
    • 你能分享你创建常量字符串的代码吗?如果我想将创建的字符串分配给变量应该怎么做?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-10
    • 1970-01-01
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-10
    相关资源
    最近更新 更多