LLVM中编译器和编译驱动程序的区别是什么?

8

有人可以解释一下LLVM中编译器和编译器驱动程序的区别吗?

感谢任何帮助。

3个回答

14

简单来说

编译器(compiler)是整个编译过程的一个组成部分;编译驱动程序(compiler driver)是整个编译过程的串联和管理者。

但我们通常会混淆这两个概念,称它们为编译器。

实际上

编译器由多个部分组成,在将程序源代码编译为可执行文件时,经过了多个不同的步骤。
源代码 -> 预处理 -> 编译 -> 汇编 -> 链接
不同的编译器组件负责不同的阶段。

例如,编译负责 编译器(Compiler)(c 语言用 cc1,C++ 语言用 cc1plus,Objective-C 语言用 cc1obj等)(其中 cc1cc1pluscc1obj 等实际上就是 编译器(Compiler),它使用源代码作为输入并输出汇编代码)
as 负责将汇编代码转换为二进制代码,ld 负责链接。

为了方便起见,我们通常只使用
clang hello.c -o hello
来完成将 c 语言源代码构建为最终可执行文件的过程。
但实际上,在这个过程中,clang 是一个 编译驱动程序(compiler driver),以自动化方式连接整个编译过程。它做了很多工作,调用编译器中的每个工具并传递适当的参数和顺序,最终生成一个可执行文件。

具体而言

clang hello.c -o hello 命令的详细参数可以通过命令 clang hello.c -o hello -v 查看。

在我的终端上,它显示:

clang version 8.0.0 (tags/RELEASE_800/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/huangshaomang/research/LLVM/ollvmsimon/build/bin

 "/home/huangshaomang/research/LLVM/build/bin/clang-8" "-cc1" "-triple" "x86_64-unknown-linux-gnu" "-emit-obj" "-mrelax-all" "-disable-free" "-main-file-name" "hello.c" "-mrelocation-model" "static" "-mthread-model" "posix" "-mdisable-fp-elim" "-fmath-errno" "-masm-verbose" "-mconstructor-aliases" "-munwind-tables" "-fuse-init-array" "-target-cpu" "x86-64" "-dwarf-column-info" "-debugger-tuning=gdb" "-resource-dir" "/home/huangshaomang/research/LLVM/build/lib/clang/8.0.0" "-internal-isystem" "/usr/local/include" "-internal-isystem" "/home/huangshaomang/research/LLVM/build/lib/clang/8.0.0/include" "-internal-externc-isystem" "/usr/include/x86_64-linux-gnu" "-internal-externc-isystem" "/include" "-internal-externc-isystem" "/usr/include" "-fdebug-compilation-dir" "/home/huangshaomang/research/linker_loader/test_constructor" "-ferror-limit" "19" "-fmessage-length" "233" "-fobjc-runtime=gcc" "-fdiagnostics-show-option" "-fcolor-diagnostics" "-o" "/tmp/hello-5168fe.o" "-x" "c" "hello.c" "-faddrsig"

 "/usr/bin/ld" "-z" "relro" "--hash-style=gnu" "--eh-frame-hdr" "-m" "elf_x86_64" "-dynamic-linker" "/lib64/ld-linux-x86-64.so.2" "-o" "hello" "/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu/crt1.o" "/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu/crti.o" "/usr/lib/gcc/x86_64-linux-gnu/5.4.0/crtbegin.o" "-L/usr/lib/gcc/x86_64-linux-gnu/5.4.0" "-L/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu" "-L/lib/x86_64-linux-gnu" "-L/lib/../lib64" "-L/usr/lib/x86_64-linux-gnu" "-L/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../.." "-L/home/huangshaomang/research/LLVM/build/bin/../lib" "-L/lib" "-L/usr/lib" "/tmp/hello-5168fe.o" "-lgcc" "--as-needed" "-lgcc_s" "--no-as-needed" "-lc" "-lgcc" "--as-needed" "-lgcc_s" "--no-as-needed" "/usr/lib/gcc/x86_64-linux-gnu/5.4.0/crtend.o" "/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu/crtn.o"

很明显,clang(编译器驱动程序)按顺序调用了两个工具(cc1(编译器)和ld(链接器)),并传递了非常复杂的参数。

还有,在以下链接中,llvm-project使用了术语“编译器驱动程序”,这可能有助于您更好地理解它。

https://llvm.org/docs/HowToCrossCompileLLVM.html
https://llvm.org/docs/LinkTimeOptimization.html


3
在 LLVM 中,我们始终使用 Clang。我将以 Clang 作为例子来回答您的问题。
当您在命令行中输入 "clang" 时,它是编译器驱动程序。编译器驱动程序有许多选项,这些选项将决定调用哪个编译器组件。例如:clang -cc1 是前端,clang 是驱动程序。驱动程序使用适用于您系统的选项调用前端。
所以,我认为编译器驱动程序驱动编译器组件协同工作。

3

我猜,编译器指的是整个编译器,而编译驱动程序对应于驱动编译管道的逻辑。驱动程序的任务是为各种文件调用正确的工具(例如,clang为C/C++源文件调用cc1,为目标文件调用ld等),并为它们设置适当的标志。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接