简体   繁体   中英

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? (all other conditions for example os and architecture and... are the same). Why? How they are different?

  2. Does the binary generated from compiling A language differs from the binary generated from compiling B language? (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?

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. It's doesn't matter which way you go, as long as you get to your destination.

Similarly, if you tell the compiler to add 3 and 5, there are multiple ways to solve this problem. It doesn't matter what the compiler does, as long as the result is the same.

Compiler A: Start with 3, then add 5.

Compiler B: Start with 5, then add 3.

Compiler C: Compute 3+5=8 at compile-time, then just load 8 into a register a run-time.

Compiler D: Start with 0, bitwise invert, left-bitshift 3, bitwise invert, add 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?

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. This allows the compiler to optimize around this fact. (ie load things from RAM once and then save them in a cache). C does not have this assumption, so frequently data is reloaded from RAM if the compiler cannot determine that a pointer is unique. C99 introduced the restrict keyword to allow programmers to inform the compiler to treat a C pointer like a Fortran pointer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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