Choosing the Right Compiler for Your Microprocessor: A Newbie's Guide

  • Thread starter RedX
  • Start date
In summary: It's a sequence of machine code instructions. You can't really interpret it or do anything with it other than run it on a machine.
  • #1
RedX
970
3
A compiler converts a high-level language to machine code. So when you buy or download a compiler, don't you have to make sure the compiler is compatible with your microprocessor? Yet when I look at the various compilers I can download, the requirements are listed as what operating system you are running, and not your chip architecture.

I have a Pentium, but that means I could have any operating system written for a Pentium. The operating system does not determine the chip architecture (indeed it is written for the chip architecture), so how can the requirements for a compiler be the operating system and not the chip architecture?
 
Technology news on Phys.org
  • #2
Compilers are usually made for a specific chip architecture running under a specific operating system, where those two may be conflated (like Windows) or the details relaxed vis the actual versions or such. However it doesn't have to be the case. The compiler translates high-level instructions to something lower. That lower-level might be some kind of portable object code which would then be translated into instructions for a specific chip by an "assembler". The OS dependence is more in the area of support libraries -- how one accesses resources through the operating system. As long as one has a good API specification, e.g. Posix -- I'm not calling it good...just sufficient -- you can "link" your portable object code with the correct libraries and not have to know exactly what is under the sheets. This is the essence of "cross-compiling".

Of course you have to run all of these programs somewhere, so they themselves have to be built for some specific hardware/OS version(s).
 
  • #3
RedX said:
A compiler converts a high-level language to machine code. So when you buy or download a compiler, don't you have to make sure the compiler is compatible with your microprocessor?

Nope. Most everyone and their grandma have a pc with an x86 or compatible instruction set architecture processor. With various revisions and extensions over the years it's still pretty backwards compatible.

For other stuff there's the hardware abstraction layer.

You really needn't concern yourself with things the compiler developers didn't want you to. The OS matters. Because the OS API is about as low as you need to be privy to nowadays for all but the most advanced software.

With Java, not even the OS matters as much. It only matters in as much as whether there's a version of the JRE written for it.

RedX said:
Yet when I look at the various compilers I can download, the requirements are listed as what operating system you are running, and not your chip architecture.

That would defeat the purpose of an operating system and of open ISAs, to need a compiler and to recompile for every processor out there. Plus the tendency is ever towards more abstraction. Interpreted languages, just in time compilation, common intermediate language, bla bla.

It's all good.

RedX said:
I have a Pentium, but that means I could have any operating system written for a Pentium.

No. You have an OS written for an instruction set.

RedX said:
The operating system does not determine the chip architecture (indeed it is written for the chip architecture), so how can the requirements for a compiler be the operating system and not the chip architecture?

Not so. You can't readily run MacOSX on x86 processors. You need virtualization.
And you can't readily run Windows on PowerPC processors. You need virtualization.
 
  • #4
schip666! said:
Compilers are usually made for a specific chip architecture running under a specific operating system, where those two may be conflated (like Windows) or the details relaxed vis the actual versions or such.

I sometimes see compilers advertised as written for 32-bit Windows. Are they assuming the x86-32 architecture? I was thinking that it could be the case that 32-bit Windows was only written for the x86-32 architecture, so when you specify 32-bit Windows you automatically specify x86-32, but I don't know if that's true or not. I know for 64-bit Windows there is the x86-64 architecture, and also an Itanium architecture, so a 64-bit Windows compiler would have to specify whether it is x86-64 or Itanium.

However it doesn't have to be the case. The compiler translates high-level instructions to something lower. That lower-level might be some kind of portable object code which would then be translated into instructions for a specific chip by an "assembler".

Can all the different type of chips read Assembly, i.e., convert Assembly code to their machine code? If so, then all compilers could just translate into Assembly, which could then be converted to your particular machine code for your particular processor.
 
  • #5
RedX said:
Can all the different type of chips read Assembly, i.e., convert Assembly code to their machine code? If so, then all compilers could just translate into Assembly, which could then be converted to your particular machine code for your particular processor.
I'm not aware that any chips can "read" assembly. Assembly code is human-readable code that corresponds to machine code for a particular processor. Each processor architecture (e.g. x86-32, x64, Itanium, and so on) has its own instruction set and hence its own kind of assembly code.

Chips are generally not capable of converting (or translating) assembly code into machine code. For that you need an assembler, a software tool that translates assembly code into machine code.

Some compilers, which are called cross-compilers, are capable of generating machine code for a number of different processor architectures. So for example, you could develop a program on a Windows machine, and have the compiler produce machine code that would run on, for example, a Motorola 68000-family processor.
 
  • #6
RedX said:
I sometimes see compilers advertised as written for 32-bit Windows. Are they assuming the x86-32 architecture? I was thinking that it could be the case that 32-bit Windows was only written for the x86-32 architecture, so when you specify 32-bit Windows you automatically specify x86-32, but I don't know if that's true or not. I know for 64-bit Windows there is the x86-64 architecture, and also an Itanium architecture, so a 64-bit Windows compiler would have to specify whether it is x86-64 or Itanium.

The x86 architecture is designed towards backwards compatibility. I have an x86-64 processor but run windows XP HE on it. That's why I can't run applications compiled for windows on the x86-64 ISA.

But I could if I had XP 64bit ed. The CPU would just switch between 32 and 64 bit modes or use 64 bit registers to hold 32 bits of data. Etc.

RedX said:
Can all the different type of chips read Assembly, i.e., convert Assembly code to their machine code? If so, then all compilers could just translate into Assembly, which could then be converted to your particular machine code for your particular processor.

There is an assembly language for any processor architecture. For instance, http://en.wikipedia.org/wiki/ARB_(GPU_assembly_language)" . This language is just a shade above the level of actual machine instructions. Or executable code.

What you're thinking of there is http://en.wikipedia.org/wiki/Interpreted_language" (CIL).

All part of the ubiquitous strive towards higher and higher level of hardware abstraction.
 
Last edited by a moderator:

1. What is a compiler?

A compiler is a software program that translates source code written in a high-level programming language into a lower-level language, such as machine code, that can be executed by a computer.

2. Why do we need compilers?

Compilers are necessary because computers can only understand and execute instructions written in machine code. High-level programming languages, such as C++ or Java, are easier for humans to read and write, so a compiler is needed to translate the code into a format that the computer can understand.

3. What is the difference between a compiler and an interpreter?

While both compilers and interpreters are used to translate high-level code into machine code, they do so in different ways. A compiler translates the entire source code into machine code before execution, while an interpreter translates and executes one line of code at a time.

4. How does a compiler work?

A compiler works by breaking down the source code into tokens, which are the basic building blocks of the language. It then analyzes and translates these tokens into an intermediate code, which is then converted into machine code. The machine code is then saved as an executable file that can be executed by a computer.

5. Can I write my own compiler?

Yes, it is possible to write your own compiler, but it is a complex and time-consuming process. It requires a deep understanding of computer architecture, programming languages, and data structures. There are also several tools and frameworks available that can assist with the process of building a compiler.

Similar threads

  • Programming and Computer Science
Replies
29
Views
2K
Replies
6
Views
1K
  • Programming and Computer Science
2
Replies
59
Views
5K
  • Programming and Computer Science
Replies
6
Views
1K
  • General Discussion
2
Replies
40
Views
2K
  • Programming and Computer Science
Replies
15
Views
16K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
21
Views
3K
Back
Top