1
$\begingroup$
  • When it is said that machine code is specific to a machine, is it specific to an architecture, or is it specific to a machine? Do two identical processors require different machine code? My guess is that people are referencing instruction sets/architecture when they say machine code is specific to a machine?
  • If they are referencing instruction sets; how does a compiler know which instruction set to create machine code for? -Ultimately -- if I compile a C program on my computer, generating machine code, to what extent is that machine code usable on other machines?

Like, if one of the points of compiled languages is that you don't have to share the source code, how does that even work? Don't you have to, at a minimum, compile for each available architecture?

edit: Sorry if these questions seem easily googleable, believe me I have tried. It's at the point where I need to just start asking people things, which is why I've made this account.

$\endgroup$

2 Answers 2

4
$\begingroup$

In principle, identical processors share the same instruction set. Anyway, on modern machines some instructions can be enabled or disabled through the BIOS (UEFI) by the user.

The successive processor generations in a given architecture have an evolving instruction set, usually keeping downward compatibility, but not always.

So in principle code compiled for an old processor should still run on a newer.

For compilers to cope, there are two strategies:

  • compile-time compatibility lets the developers specify the processor family that should be supported, leading to different code generation; a substantial historical example is that of the transition from 32 to 64 bit systems.

  • run-time compatibility relies on querying the processor itself during program execution to know which instructions are actually supported, and switch to the relevant code to optimize the utilization of the hardware capabilities; this means that the generated code will include several instances of some functions.

$\endgroup$
0
$\begingroup$

For example for gcc and clang, you pass an argument -arch=x86, -arch=ppc, -arch=arm, or with x86_64, ppc64 or aarch64. The compiler does what it can.

(The names might be slightly wrong).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.