Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a C++ compiler allowed to emit different machine code compiling the same program?

Consider a situation. We have some specific C++ compiler, a specific set of compiler settings and a specific C++ program.

We compile that specific programs with that compiler and those settings two times, doing a "clean compile" each time.

Should the machine code emitted be the same (I don't mean timestamps and other bells and whistles, I mean only real code that will be executed) or is it allowed to vary from one compilation to another?

like image 906
sharptooth Avatar asked Jun 16 '10 13:06

sharptooth


People also ask

Does a compiler produce machine code?

Compilers produce assembly code, which is a human-readable version of machine code (eg, instead of 1's and 0's you have actual commands). However, the correct assembly/machine code needed to make your program run correctly is different depending on the operating system.

Does C compile to machine code?

C is a compiled language. Its source code is written using any editor of a programmer's choice in the form of a text file, then it has to be compiled into machine code.

Can I use a different compiler in Visual Studio?

1 Answer. If you want to install multiple compiler versions, you can install the required version through Visual Studio Installer: open Visual Studio Installer > Modify > Individual components > Compilers, build tools, and runtimes. If the answer is the right solution, please click "Accept Answer" and kindly upvote it.

Do compilers compile themselves?

The compiler can't compile itself. The output file is not the same instance as the compiler that produces the output file.


2 Answers

The C++ standard certainly doesn't say anything to prevent this from happening. In reality, however, a compiler is normally deterministic, so given identical inputs it will produce identical output.

The real question is mostly what parts of the environment it considers as its inputs -- there are a few that seem to assume characteristics of the build machine reflect characteristics of the target, and vary their output based on "inputs" that are implicit in the build environment instead of explicitly stated, such as via compiler flags. That said, even that is relatively unusual. The norm is for the output to depend on explicit inputs (input files, command line flags, etc.)

Offhand, I can only think of one fairly obvious thing that changes "spontaneously": some compilers and/or linkers embed a timestamp into their output file, so a few bytes of the output file will change from one build to the next--but this will only be in the metadata embedded in the file, not a change to the actual code that's generated.

like image 149
Jerry Coffin Avatar answered Oct 02 '22 16:10

Jerry Coffin


According to the as-if rule in the standard, as long as a conforming program (e.g., no undefined behavior) cannot tell the difference, the compiler is allowed to do whatever it wants. In other words, as long as the program produces the same output, there is no restriction in the standard prohibiting this.

From a practical point of view, I wouldn't use a compiler that does this to build production software. I want to be able to recompile a release made two years ago (with the same compiler, etc) and produce the same machine code. I don't want to worry that the reason I can't reproduce a bug is that the compiler decided to do something slightly different today.

like image 37
KeithB Avatar answered Oct 02 '22 16:10

KeithB