4
我想创建一个使用LLVM来存储字符串的本地变量,但是我的代码当前正在抛出一个语法错误。使用LLVM创建本地字符串
@.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工具被分配和分配给一个全局变量,但我希望它是本地:
,其分配和存储字符串lli: test2.ll:8:23: error: constant expression type mismatch
%1 = load [6 x i8]* c"hello\00"
我的IR代码(在基本块内)。以下作品的代码,但我不希望创建这个变种“@名为.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
感谢
如果字符串不是最小的大小,您将需要小心字符串在堆栈中的大小。 –
的确如此。不过,就我而言,这足够了。在我的程序中,我初始化为255,然后将bitcast初始化为6 ... – mk2
好的调整和更好的可读性! –