Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C compiler compiling

Name: Anonymous 2007-10-08 20:02

One of the students bought this question up in a tutorial and this anon thought it would be interesting if it was discussed with his other aliases on 4ch.

The question this anon was approached with was:
If one day a keen programmer (with too much free time) decided to write a new C compiler in C, and succeeded.

Assuming that the following holds:
-It is actually possible to write a C compiler in C for the architecture & build environment
-The series of operations that we are about to conduct are all isolated on the original build environment and CPU architecture
-All of the C language aspects employed by the source code of the new compiler are implemented properly in the source code.
-The code is magically debugged and will fully function to it's designed functionality contracts.
-The new compiler has optimization algorithms that will fully preserve functionality of the inputed code.
-No inappropriate usage of the C language was employed in the source code (such as language hacks)

Now the programmer compiles the written source code in an industry standard C compiler, producing a derived C compiler. If the source code is then repetitively fed into the derived compilers, what would happen?
1) The machine code size will converge to a resultant product that will be in full equilibrium, and will be much more optimized than the predecessor compilers (excluding the original product that is not manufactured by this source code)
2) The machine code size will diverge however maintains functionality, efficiency however may vary.
3) The machine code size will diverge, and functionality will not be maintained thus eventually leading to an error and failing to create additional derived compilers.
4) Something else? Explain.

My answer was 2. What do you think?

Name: Anonymous 2007-10-08 20:17

The output of a compiler is not source code.

/thread

Name: Anonymous 2007-10-08 20:31

my good sir
your machine will asplode

Name: Anonymous 2007-10-08 20:51

My answer is 4: it will produce the same machine code every time after the first. The procedure of translating source code to machine code is completely defined by the source. It should always produce the same output when given the same input. If a compiler produced different source code on different runs, that would be a bug.

Good question, by the way. Watch that student.

Name: Anonymous 2007-10-08 21:30

>>4
I'm not so sure. On FreeBSD (and probably other *nix variants), when you compile GCC from source, it uses a pre-built binary (the older version of gcc you already had) to produce another binary. This new binary is used to recompile itself from source, then this new binary is used to recompile an optimized version which is the final product.

This suggests that there might potentially be some differences, but I suspect it's just a compiler optimization thing.

Name: Anonymous 2007-10-08 23:18

>>4 is right. The custom compiler will produce the same output for the same input, no matter what compiler it itself was compiled on. Optimisations performed in the resultant compiler wouldn't affect its output, just how fast it compiles its own programs.

Name: Anonymous 2007-10-09 0:22

having written a compiler for a class, i know the answer to your question

but i cbf answering it

Name: Anonymous 2007-10-09 3:08

I've also written a compiler before, albeit a simple one.

>>4 basically got it

>>5
given the same input, a program will ALWAYS produce the same output

the reason that in your case the output is different is because the two binary are different, hence the compilation result is different

it is like taking a compiler, then the same one but optimised, and compile the same source code, if they are both very efficient, then they should produce the same machine code, but that rarely happens unless the writer designed the architecture or are extremely familiar with it him/herself.

Name: Anonymous 2007-10-09 3:47

>>8
given the same input, a program will ALWAYS produce the same output
Yeah, that makes sense, that the only difference would be the implementation by which the program produces that output. Neato :)

Name: Anonymous 2007-10-09 9:14

Reminds me of the explanation for the halting problem

Name: Anonymous 2011-02-03 0:59

Name: Anonymous 2011-02-03 5:42

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List