Search code examples
gccclangllvm

Why does gcc not compile emitted assembly by llvm?


I have a hello world program in C, then i compiled it to llvm-ir using clang, then compiled that llvm-ir to assembly, however, GCC doesn't compile the assembly code to an exe file, i have been trying to fix this problem for a week, but i don't know why it doesn't work, here is all the code: hello.c:

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

hello.ll:

; ModuleID = 'hello.c'
source_filename = "hello.c"
target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-windows-cygnus"

@.str = private unnamed_addr constant [15 x i8] c"Hello, World!\0A\00", align 1

; Function Attrs: noinline nounwind optnone uwtable
define i32 @main() #0 {
  %1 = alloca i32, align 4
  store i32 0, i32* %1, align 4
  %2 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i32 0, i32 0))
  ret i32 0
}

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

attributes #0 = { noinline nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }

!llvm.module.flags = !{!0, !1}
!llvm.ident = !{!2}

!0 = !{i32 1, !"wchar_size", i32 2}
!1 = !{i32 7, !"PIC Level", i32 2}
!2 = !{!"clang version 5.0.1 (tags/RELEASE_501/final)"}

hello.s:

    .text
    .def     @feat.00;
    .scl    3;
    .type   0;
    .endef
    .globl  @feat.00
.set @feat.00, 0
    .file   "hello.c"
    .def     main;
    .scl    2;
    .type   32;
    .endef
    .globl  main                    # -- Begin function main
    .p2align    4, 0x90
main:                                   # @main
.seh_proc main
# %bb.0:
    pushq   %rbp
    .seh_pushreg 5
    subq    $48, %rsp
    .seh_stackalloc 48
    leaq    48(%rsp), %rbp
    .seh_setframe 5, 48
    .seh_endprologue
    callq   __main
    movl    $0, -4(%rbp)
    movabsq $.L.str, %rcx
    callq   printf
    xorl    %eax, %eax
    addq    $48, %rsp
    popq    %rbp
    retq
    .seh_handlerdata
    .text
    .seh_endproc
                                        # -- End function
    .section    .rdata,"dr"
.L.str:                                 # @.str
    .asciz  "Hello, World!\n"

My terminal:

$ clang -S -emit-llvm hello.c
$ llc hello.ll
$ gcc hello.s
hello.s: Assembler messages:
hello.s:19: Error: invalid register for .seh_pushreg
hello.s:19: Error: junk at end of line, first unrecognized character is `5'
hello.s:23: Error: invalid register for .seh_setframe
hello.s:23: Error: missing separator

I installed LLVM using Cygwin, so that might help, and i am using gcc version: 11.2.0


Solution

  • Never mind, i fixed it by installing clang/llvm 10, using WSL, sure, it's not the latest version, but it's better then nothing.