LLVM compiling with GCC i32 is truncated to i8

42 views Asked by At

compiling this simple code should return 2000 but it returns 208 because it gets truncated to i8.

define i32 @"main"()
{
entry:
  %"x" = alloca i32
  store i32 2000, i32* %"x"
  %".3" = load i32, i32* %"x"
  ret i32 %".3"
}

this is how I compiled it:

llc -filetype=obj output.ll -o output.o
gcc output.o -o output -ggdb

the debugger shows the correct value to store 0x7d0, 2000 but the end result returns 208

->  0x401110 <+0>:  movl   $0x7d0, -0x4(%rsp)        ; imm = 0x7D0 
    0x401118 <+8>:  movl   $0x7d0, %eax              ; imm = 0x7D0 
    0x40111d <+13>: retq   
    0x40111e:       addb   %al, (%rax)
Process 4869 exited with status = 208 (0x000000d0) 

is this because I am using the terminal? or should I use stdout using llvm c printf?

Thank you in advance

compiling this simple code should return 2000 but it returns 208 because it gets truncated to i8.

1

There are 1 answers

1
TAW On BEST ANSWER

this fixed it, the terminal truncates the result to i8, I just used printf:

@str_fmt = private unnamed_addr constant [3 x i8] c"%d\00"

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

define i32 @"main"()
{
entry:
  %"x" = alloca i32
  store i32 2000, i32* %"x"
  %".3" = load i32, i32* %"x"
  call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @str_fmt, i32 0, i32 0), i32 %".3")
  ret i32 %".3"
}