2016-05-08 47 views
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 

感谢

回答

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*, ...) 

基本上谁有同样的问题,我不得不给每个字符的存储单独的数组,然后bitcast到6-18 *所以我可以使用printf函数。我无法使用在LLVM网页http://llvm.org/docs/LangRef.html#id669中显示的方法c" ... "。它似乎是IR的语言规范中的一个特例,它们需要处于全球范围内。

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

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

使用llvm生成代码更容易,它更易读!

+1

如果字符串不是最小的大小,您将需要小心字符串在堆栈中的大小。 –

+0

的确如此。不过,就我而言,这足够了。在我的程序中,我初始化为255,然后将bitcast初始化为6 ... – mk2

+0

好的调整和更好的可读性! –