1

Anyone knows the complete chain of operations from .c source code to finally an executable .exe?

I've downloaded the source of gcc,and found its c-parser.y is also written in c:

extdef:
    fndef
    | datadef
    | ASM_KEYWORD '(' expr ')' ';'
        { STRIP_NOPS ($3);
          if ((TREE_CODE ($3) == ADDR_EXPR
               && TREE_CODE (TREE_OPERAND ($3, 0)) == STRING_CST)
              || TREE_CODE ($3) == STRING_CST)
            assemble_asm ($3);
          else
            error ("argument of `asm' is not a constant string"); }
    | extension extdef
        { pedantic = $<itype>1; }
    ;

So anyone knows the complete story of c's self-hosting?

UPDATE

I know how some compilers for scripts are written,most of them depends on c compilers.

So I'm now asking how c compiler works.

compiler
  • 4,143
  • 9
  • 36
  • 40
  • 2
    I think this question might be a little bit too... expansive? It kind of sounds like you're asking how does a C compiler work, specifically in generating Microsoft Windows executable binaries. Is this what you're asking? Are you looking for a general overview of the steps taken in building an executable from source, from a compiler's perspective? – Sion Sheevok Mar 05 '11 at 03:02
  • 1
    'Compiler' asking how a C compiler works! :D – Julio Gorgé Mar 05 '11 at 03:04
  • 1
    Not precisely a duplicate, but pointers to good info at: [How was the first compiler written?](http://stackoverflow.com/questions/1653649/how-was-the-first-compiler-written) – Greg Hewgill Mar 05 '11 at 03:10

2 Answers2

6

GCC has a multi-stage process, but starts with a working C compiler (which might or might not be another version of GCC).

  1. Use the other C compiler to create a first version of GCC - the GNU C Compiler.
    • This gives you a working copy of the new version of GCC (assuming the old compiler was serviceable).
    • It is not, however, identical the compiler that (the new version of) GCC would produce, except by implausible accident.
  2. Use the first version of GCC to create a second version of GCC.
    • The second version of GCC is the (obviously) the code that GCC would generate. This is the benchmark version.
    • However, it has not yet been shown that this is stable.
  3. Use the second version of GCC to create a third version of GCC.
    • The second version of GCC should produce the same output for the third version of GCC.
  4. Ensure that the second and third versions of GCC are the same.
    • On some platforms, the object files contain compilation times; clearly, the compilation time has to discount differences in compilation time.
    • Assuming that the second and third versions are 'the same', then we know that GCC can produce itself from its own source code.
    • It is now safe to use the new version of GCC to build the various runtime libraries, and the other compilers in the family.
  5. Build the other parts of the GNU Compiler Collection with the new C compiler.
    • Most people do not restrict the GNU Compiler Collection to just the C compiler.
    • Typically, they also produce the C++ and Java (and usually Objective C) compilers too.
    • And you need the Standard C++ Library support too.
    • Usually, the C compiler uses the C library provide by the platform.
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 2
    The first step can even be on a different platform, you could for example compile gcc for ARM on an x86 - called cross compilation – Martin Beckett Mar 05 '11 at 03:22
  • @compiler: the numbering is largely coincidental, and just indicates the sequence of operations, though it is difficult to compile a third version of the compiler before you've compiled a second version. What else are you after? – Jonathan Leffler Mar 05 '11 at 04:36
  • It seems you don't know what self-hosting means...For example,the self hosting language of PHP compiler is C.What's the self-hosting programming language of C? – compiler Mar 05 '11 at 05:16
  • @compiler: the self-hosting language is C. And yes, that does lead to a 'chicken and egg' problem when porting to a new CPU type for the first time. – Jonathan Leffler Mar 05 '11 at 05:20
  • No,you still don't know what self-hosting means for a compiler...I know the majority of c compiler's code is in c,but that doesn't mean its self-hosting language is c – compiler Mar 05 '11 at 07:09
  • 1
    @compiler, if you continue to be rude you're not likely to get much help. The fact is that the C compiler is written in C and C-generating tools like yacc. There's no other "self-hosting language" involved. – Jim Balter Mar 05 '11 at 08:26
  • 2
    @compiler "It seems you don't know what self-hosting means...For example,the self hosting language of PHP compiler is C", Uh, no, it seems that you don't know what **self** hosting means. – Jim Balter Mar 05 '11 at 08:38
1

You are surprised that the C compiler is written in C? What other language would a C expert use when writing his compiler?

The apparent chicken-and-egg problem only occurred once, a long time ago, when there was no compilers available at all. Then someone had to write one in assembly (after someone else wrote the first assembler in machine code :-)

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • 1
    And it's quite possible that we have [bug #2](http://cm.bell-labs.com/who/ken/trust.html) in there somewhere. – aaz Mar 05 '11 at 14:07