Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create local string using LLVM

Tags:

llvm

llvm-ir

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"

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

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

Thanks

like image 362
renno Avatar asked May 08 '16 16:05

renno


1 Answers

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

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.

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

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

like image 167
renno Avatar answered Sep 22 '22 16:09

renno