使用LLVM创建本地字符串 [英] Create local string using LLVM

查看:270
本文介绍了使用LLVM创建本地字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create a local variable using LLVM to store strings, but my code is currently throwing a syntax error.

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

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

My IR code that allocates and store the string:

@.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" ...

Using llc I could see that the way llvm implements is allocating and assigning to a global variable, but I want it to be local (inside a basic block). The code below works, but I don't want to create this 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

谢谢

推荐答案

在弄乱了我以前的代码后,我自己弄清楚了.

I figured out by myself after messing more with my previous code.

下面是代码,因此与我遇到相同问题的人可以检查

Below is the code, so people who had the same problem as I had can check

@.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中显示的方法c" ... " .org/docs/LangRef.html#id669 .在IR的语言规范中,这似乎是一种特殊情况,它们必须在全球范围内.

Basically, I had to store each of the characters individually in the array and then bitcast to i8* so I could use the printf function. I couldn't use the c" ... " method which is the one shown in LLVM webpage http://llvm.org/docs/LangRef.html#id669 . It seems it is a special case in the language specification of the IR and they required to be in the global scope.

更新:我再次使用同一代码,发现最好的方法是存储一个常量而不是每个i8符号.因此,第6行将替换为:

UPDATE: I was working on the same code again and I found out that the best way was to store a constant instead of each of the i8 symbols. So the line 6, will be replaced by:

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

使用llvm生成代码更容易,并且更具可读性!

It is easier to generate code using llvm and it's more readable!

这篇关于使用LLVM创建本地字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆