使用LLVM创建本地字符串

5

我正在尝试使用LLVM创建一个本地变量来存储字符串,但我的代码目前会抛出语法错误。

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

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

@.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实现的方式是分配并分配给一个全局变量,但我希望它是本地的(在基本块内)。下面的代码有效,但我不想创建此变量“@.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

谢谢

1个回答

2
我在进一步研究之前的代码后自己解决了问题。
以下是代码,因此遇到与我相同问题的人可以检查。
@.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中显示的方法。它似乎是IR语言规范中的一个特例,需要在全局范围内使用。
更新:我再次处理相同的代码时发现,最好的方法是存储常量而不是每个i8符号。因此,第6行将被替换为:
  store [6 x i8] c"hello\00", [6 x i8]* %0

使用 LLVM 生成代码更加容易,并且代码更易读!

1
如果字符串不是一些最小尺寸,那么在堆栈上的字符串大小就需要小心处理。 - Frank C.
没错。不过对于我的目的来说,那已经足够了。在我的程序中,我将其初始化为255,然后进行位转换为6... - renno
你能分享一下创建常量字符串的代码吗?如果我想将创建的字符串赋值给一个变量,应该怎么做? - Mohannad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接