简体   繁体   English

使用编译器生成的二进制文件取决于哪些因素?

[英]The binary generated using compiler depends on which factors?

I'm very new to compile and compilers and i have some questions:我对编译和编译器很陌生,我有一些问题:

  1. Does the binary generated by compiler A is different from compiler B?编译器 A 生成的二进制文件是否与编译器 B 不同? (all other conditions for example os and architecture and... are the same). (所有其他条件,例如 os 和 architecture 和...都是相同的)。 Why?为什么? How they are different?它们有何不同?

  2. Does the binary generated from compiling A language differs from the binary generated from compiling B language?编译 A 语言生成的二进制文件与编译 B 语言生成的二进制文件有区别吗? (all other conditions for example compiler, os and architecture and... are the same). (所有其他条件,例如编译器、操作系统和体系结构......都是相同的)。 in other words is there any relation or dependency between the source language from which the binary generated?换句话说,生成二进制文件的源语言之间是否存在任何关系或依赖关系? Why?为什么? If yes, how they are related?如果是,它们是如何相关的?

Yes to all of your questions.是的,你所有的问题。

You can even run a compiler twice on the same computer with the same source code, and the compiler can generate different binary outputs.您甚至可以使用相同的源代码在同一台计算机上运行编译器两次,并且编译器可以生成不同的二进制输出。

does the binary generated by compiler A is different from compiler B?编译器 A 生成的二进制文件与编译器 B 不同吗?

There are different ways to solve the same problem.有不同的方法来解决同样的问题。 In the human world, if you need to travel one city block north and one city block east, there are two possible paths.在人类世界中,如果你需要向北走一街区,向东走一街区,有两种可能的路径。 You can go north then east, or you can go east then north.您可以 go 向北然后向东,或者您可以 go 向东然后向北。 It's doesn't matter which way you go, as long as you get to your destination.只要您到达目的地,go 的哪条路都没有关系。

Similarly, if you tell the compiler to add 3 and 5, there are multiple ways to solve this problem.同样,如果你告诉编译器将 3 和 5 相加,有多种方法可以解决这个问题。 It doesn't matter what the compiler does, as long as the result is the same.编译器做什么并不重要,只要结果是一样的。

Compiler A: Start with 3, then add 5.编译器 A:从 3 开始,然后添加 5。

Compiler B: Start with 5, then add 3.编译器 B:从 5 开始,然后添加 3。

Compiler C: Compute 3+5=8 at compile-time, then just load 8 into a register a run-time.编译器 C:在编译时计算 3+5=8,然后在运行时将 8 加载到寄存器中。

Compiler D: Start with 0, bitwise invert, left-bitshift 3, bitwise invert, add 1.编译器 D:从 0 开始,按位反转,左移 3,按位反转,加 1。

All of these produce the same result, and depending on the computer architecture and the compiler's settings, one option will be chosen over another.所有这些都会产生相同的结果,并且根据计算机体系结构和编译器的设置,将选择一个选项而不是另一个选项。 This can result in different settings making different binaries.这可能会导致不同的设置生成不同的二进制文件。 And different compilers will likely use different default settings.并且不同的编译器可能会使用不同的默认设置。

does the binary generated from compiling A language differs from the binary generated from compiling B language?编译 A 语言生成的二进制文件与编译 B 语言生成的二进制文件有区别吗?

For the same reasons as above, unless the compiler settings are exactly the same, and the problem we ask the compiler to solve is exactly the same, we will likely have different binary outputs.出于与上述相同的原因,除非编译器设置完全相同,并且我们要求编译器解决的问题完全相同,否则我们可能会有不同的二进制输出。

Edit:编辑:

In some cases, using a different language results in different assumptions around what you can and cannot do.在某些情况下,使用不同的语言会导致围绕你能做什么和不能做什么的不同假设。 For example, in Fortran, it is assumed that each pointer in a function is unique.例如,在 Fortran 中,假设 function 中的每个指针都是唯一的。 This allows the compiler to optimize around this fact.这允许编译器围绕这个事实进行优化。 (ie load things from RAM once and then save them in a cache). (即从 RAM 中加载一次,然后将它们保存在缓存中)。 C does not have this assumption, so frequently data is reloaded from RAM if the compiler cannot determine that a pointer is unique. C 没有此假设,因此如果编译器无法确定指针是唯一的,则经常从 RAM 重新加载数据。 C99 introduced the restrict keyword to allow programmers to inform the compiler to treat a C pointer like a Fortran pointer. C99 引入了restrict关键字以允许程序员通知编译器将 C 指针视为 Fortran 指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM