Search code examples
ccompiler-constructionclangllvmllvm-ir

LLVM pass va_list to another function


I've been using LLVM for quite some time now and I solved problems myself by searching and using clang to output the generated code, but I could never solve this problem. I'm currently developing a compiler with my custom frontend and LLVM as backend. Since I wanted a custom println function that works exactly like the printf function (varadic argument formatting), I tried to implement it using the instrisic @llvm.va_start and @llvm.va_end.

The problem now is that everything compiles well, but when I run the program, it shows me strange numbers instead of the real arguments that are used, for example:

Input:

println("Hello World %i, %i?", 1, 2)

Output:

Hello World 1447122753, 1280590165?

It is also noticable that the numbers don't change even if the program is run again.

Some system informations and used libraries:

  • LLVM-10
  • Ubuntu 18.04.4 LTS
  • Intel(R) Core(TM) i5-8400 CPU

The linked output of my program in LLVM-IR by my compiler:

; ModuleID = 'merged.bc'
source_filename = "ld-temp.o"
target triple = "x86_64-unknown-linux-gnu"

%0 = type { i32, i32, i8*, i8* }

@0 = private unnamed_addr constant [20 x i8] c"Hello World %i, %i?\00", align 1
@1 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1

define i32 @main() {
  %1 = alloca i32, align 4
  %2 = call i32 (i8*, ...) @println(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @0, i32 0, i32 0), i32 1, i32 2)
  store i32 0, i32* %1, align 4
  br label %3

3:                                                ; preds = %0
  %4 = load i32, i32* %1, align 4
  ret i32 %4
}

define internal i32 @println(i8* %0, ...) {
  %2 = alloca i32, align 4
  %3 = call %0* @va_start()
  %4 = alloca %0*
  store %0* %3, %0** %4
  %5 = alloca i8*, align 1
  store i8* %0, i8** %5, align 1
  %6 = load i8*, i8** %5, align 1
  %7 = load %0*, %0** %4
  %8 = call i32 @vprintf(i8* %6, %0* %7)
  %9 = alloca i32, align 4
  store i32 %8, i32* %9, align 4
  %10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @1, i32 0, i32 0))
  %11 = load %0*, %0** %4
  call void @va_end(%0* %11)
  %12 = load i32, i32* %9, align 4
  store i32 %12, i32* %2, align 4
  br label %13

13:                                               ; preds = %1
  %14 = load i32, i32* %2, align 4
  ret i32 %14
}

declare i32 @vprintf(i8*, %0*)

declare i32 @printf(i8*, ...)

define internal %0* @va_start() {
  %1 = alloca %0, align 8
  %2 = bitcast %0* %1 to i8*
  call void @llvm.va_start(i8* %2)
  ret %0* %1
}

; Function Attrs: nounwind
declare void @llvm.va_start(i8*) #0

define internal void @va_end(%0* %0) {
  %2 = bitcast %0* %0 to i8*
  call void @llvm.va_end(i8* %2)
  ret void
}

; Function Attrs: nounwind
declare void @llvm.va_end(i8*) #0

attributes #0 = { nounwind }

!llvm.module.flags = !{!0}

!0 = !{i32 1, !"LTOPostLink", i32 1}

Changes:

  • Instead of using external functions, now generating the IR directly when it's called (this caused problems, because you will get the arguments of the va_start() function, which doesn't have any arguments). The working IR is now:
; ModuleID = 'merged.bc'
source_filename = "ld-temp.o"
target triple = "x86_64-unknown-linux-gnu"

%0 = type { i32, i32, i8*, i8* }

@0 = private unnamed_addr constant [20 x i8] c"Hello World %i, %i?\00", align 1
@1 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1

define i32 @main() {
  %1 = alloca i32, align 4
  %2 = call i32 (i8*, ...) @println(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @0, i32 0, i32 0), i32 1, i32 2)
  store i32 0, i32* %1, align 4
  br label %3

3:                                                ; preds = %0
  %4 = load i32, i32* %1, align 4
  ret i32 %4
}

define internal i32 @println(i8* %0, ...) {
  %2 = alloca i32, align 4
  %3 = alloca %0, align 8
  %4 = bitcast %0* %3 to i8*
  call void @llvm.va_start(i8* %4)
  %5 = load %0, %0* %3, align 8
  %6 = alloca %0
  store %0 %5, %0* %6
  %7 = alloca i8*, align 1
  store i8* %0, i8** %7, align 1
  %8 = load i8*, i8** %7, align 1
  %9 = getelementptr %0, %0* %6
  %10 = call i32 @vprintf(i8* %8, %0* %9)
  %11 = alloca i32, align 4
  store i32 %10, i32* %11, align 4
  %12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @1, i32 0, i32 0))
  %13 = getelementptr %0, %0* %6
  %14 = getelementptr %0, %0* %13
  %15 = bitcast %0* %14 to i8*
  call void @llvm.va_end(i8* %15)
  store i32 0, i32* %2, align 4
  br label %16

16:                                               ; preds = %1
  %17 = load i32, i32* %2, align 4
  ret i32 %17
}

; Function Attrs: nounwind
declare void @llvm.va_start(i8*) #0

declare i32 @vprintf(i8*, %0*)

declare i32 @printf(i8*, ...)

; Function Attrs: nounwind
declare void @llvm.va_end(i8*) #0

attributes #0 = { nounwind }

!llvm.module.flags = !{!0}

!0 = !{i32 1, !"LTOPostLink", i32 1}

Solution

  • Your main problem looks to be that you're returning a pointer to alloca-allocated memory (i.e. local memory) from @va_start. You should either make it take a pointer as an argument like @llvm.va_start does or get rid of the function altogether and call @llvm.va_start directly from @println.

    PS: I don't understand what the point of your %0 type is. If it's supposed to represent an argument list, why not use i8* (which is what the LLVM vararg functions expect) directly instead of bitcasting it.

    PPS: The format string and number of arguments in your source code doesn't match those in your generated LLVM. I'm guessing the LLVM was actually generated from a different source code (specifically println("Hello World %i?", 1)).