Anit Gandhi
Anit Gandhi

Reputation: 125

ARM and GCC Compiling

Hopefully this hasn't been asked and answered already, but I just had a quick question on ARM.

Specifically, if when compiling Android (which has a lot of C and C++), you use GCC to compile, doesn't that create x86 based code? How is it that an ARM processor, which uses a reduced instruction set, can interpret this code and run like it does?

Thanks!

Upvotes: 0

Views: 1065

Answers (3)

Michael Burr
Michael Burr

Reputation: 340168

to put it simply, when you're building for ARM on your x86 computer you're using a cross-compiler - a compiler that runs on one platform but generates code for another. This is extremely common when developing for embedded or mobile platforms.

Upvotes: 1

artless-noise-bye-due2AI
artless-noise-bye-due2AI

Reputation: 22395

Building gcc goes through a configuration step, part of this is to specify a back-end. The back-end is responsible for op-code generation. The typical compiler is many phases. Briefly,

  1. Parser - convert text to a data representation.
  2. Front end - Optimize by changing code constructs, possibly language specific.
  3. Middle end - Performs computer science optimization that are common to any compiler.
  4. Back end - Performs optimization specific to the target CPU.

See stackoverflow compiler wiki for more.

So parts one to three are common for the x86 and the ARM versions of gcc (or any gcc). The Android compiler is a version of gcc which has been configured to generate ARM code. It is a different compiler than the one that normally runs on an x86. You maybe running an ARM emulator on a PC and then believe that this code is run by the x86. However, this is a virtual ARM machine running this code. An x86 processor can not run ARM code natively.

The Android gcc is an ARM configured gcc. A normal Linux distributions gcc is configured for an x86 or x86_64.


Something is missing above: Who compiles the compiler? In both cases, an x86 compiler compile the new compiler. The difference is the selected back-end. One is x86, the other ARM. Both compilers run on an x86, but they generate code for different targets. Gcc can only generate code for an ARM or an x86; never both via any sort of command line switch. A compiler build usually refers to three different CPU types.

  1. Build - Machine where the compiler is built. This is the compiler's compiler.
  2. Host - the machine the compiler runs on. Not it's output, but the compiler itself.
  3. Target - the machine the back-end targets. The one code is generated for.

I think maybe people are thinking because they both run on the same host, they must generate code for the same target. But that is not true; it is a little mind bending at first. Depending on the setup, you may need compilers for each of these machines to make a final compiler.

The first compiler for any machine is usually a cross compiler. Except for some people who made primitive compilers long ago in assembler.

See also: Cross compiler.

Upvotes: 3

Gabe Sechan
Gabe Sechan

Reputation: 93542

GCC doesn't just compile for x86. It actually compiles to any instruction set. If you wanted to you could create a new one just by adding a few files.

And ARM isn't a reduced instruction set. Its a completely different instruction set. There's some things ARM has that x86 doesn't and vice versa.

Upvotes: 3

Related Questions