为什么 gcc 不通过 llvm 编译发出的程序集?
Why does gcc not compile emitted assembly by llvm?
我有一个 C 语言的 hello world 程序,然后我使用 clang 将它编译为 llvm-ir,然后将该 llvm-ir 编译为汇编,但是,GCC 不会将汇编代码编译为 exe 文件,
我已经尝试解决这个问题一个星期了,但我不知道为什么它不起作用,
这是所有代码:
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![=11=]A[=11=]", 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 , %rsp
.seh_stackalloc 48
leaq 48(%rsp), %rbp
.seh_setframe 5, 48
.seh_endprologue
callq __main
movl [=12=], -4(%rbp)
movabsq $.L.str, %rcx
callq printf
xorl %eax, %eax
addq , %rsp
popq %rbp
retq
.seh_handlerdata
.text
.seh_endproc
# -- End function
.section .rdata,"dr"
.L.str: # @.str
.asciz "Hello, World!\n"
我的终端:
$ 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
我使用 Cygwin 安装了 LLVM,这可能会有所帮助,
我正在使用 gcc 版本:11.2.0
没关系,我通过安装 clang/llvm 10 修复了它,使用 WSL,当然,它不是最新版本,但总比什么都没有好。
我有一个 C 语言的 hello world 程序,然后我使用 clang 将它编译为 llvm-ir,然后将该 llvm-ir 编译为汇编,但是,GCC 不会将汇编代码编译为 exe 文件, 我已经尝试解决这个问题一个星期了,但我不知道为什么它不起作用, 这是所有代码: 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![=11=]A[=11=]", 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 , %rsp
.seh_stackalloc 48
leaq 48(%rsp), %rbp
.seh_setframe 5, 48
.seh_endprologue
callq __main
movl [=12=], -4(%rbp)
movabsq $.L.str, %rcx
callq printf
xorl %eax, %eax
addq , %rsp
popq %rbp
retq
.seh_handlerdata
.text
.seh_endproc
# -- End function
.section .rdata,"dr"
.L.str: # @.str
.asciz "Hello, World!\n"
我的终端:
$ 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
我使用 Cygwin 安装了 LLVM,这可能会有所帮助, 我正在使用 gcc 版本:11.2.0
没关系,我通过安装 clang/llvm 10 修复了它,使用 WSL,当然,它不是最新版本,但总比什么都没有好。