Segmentation fault encountered at ret void in llvm-ir instructions (original) (raw)

November 16, 2024, 5:27pm 1

I’m currently making a compiler that outputs bare LLVM-IR instructions and implementing variadic function calls. I have defined a println function that accepts a (format) string and variable amount of arguments for the printf call. I have included printf calls to see where my program faults and it is as the return of the function, which would make me think that there is something wrong with cleaning up the @llvm.va_end calls, since it does what i wanted it to do before the fault.

Here is the llvm instrucitons:

declare void @llvm.va_start(i8*)
declare void @llvm.va_end(i8*)
declare void @vprintf(i8*, i8*)
@.str_3 = private unnamed_addr constant [2 x i8] c"\0A\00"
declare void @printf(i8*, ...)
@.str_5 = private unnamed_addr constant [4 x i8] c"%i\0A\00"
@.str_6 = private unnamed_addr constant [16 x i8] c"number is %i %i\00"

define void @println(i8* %a, ...) {
entry:
    call void @printf(i8* @.str_5, i32 1) ; debug, added prior
    %.va_list = alloca i8*
    call void @printf(i8* @.str_5, i32 2) ; debug, added prior
    call void @llvm.va_start(i8* %.va_list)
    call void @printf(i8* @.str_5, i32 3) ; debug, added prior
    call void @vprintf(i8* %a, i8* %.va_list)
    call void @printf(i8* @.str_3)
    call void @printf(i8* @.str_5, i32 4) ; debug, added prior
    call void @llvm.va_end(i8* %.va_list)
    call void @printf(i8* @.str_5, i32 5) ; debug, added prior
    ret void
}

define void @main() {
entry:
    call void @printf(i8* @.str_5, i32 0) ; debug, added prior
    call void @println(i8* @.str_6, i32 5, i32 2)
    call void @printf(i8* @.str_5, i32 6) ; debug, added prior
    ret void
}

Output of running the built program:

0
1
2
3
number is 5 2
4
5
Segmentation fault (core dumped)

As you can see here i get the segmentation fault between printf(5) and printf(6) which would entail that there is something going on at the return/deallocating of memory or something in the println function.

johron November 16, 2024, 7:47pm 2

I fixed this