What factors affect the required compiler?

I am learning C and I do not understand what factors determine the required compiler and why.

Let's say I have C code that is a small console application and I want to compile it for a specific platform. This platform will have a specific OS and instruction set.

  • How does instruction set affect the compiler? Does it depend on the actual instruction set or just its register size (16/32/64 bits)? If it only depends on whether it's 64-bit or 32-bit, can't the instruction set look different and machine code doesn't work? I am confused by this because 32-bit and 64-bit versions of the application are always offered, although there are several possible instruction sets.

  • Does an OS running on top of a specific instruction set provide what compiler is needed and why? Isn't the machine code the same if the CPU is the same?


UPDATE

Thanks to all who responded. My final conclusion

+3


source to share


3 answers


How does a set of instructions affect the compiler?

The compiler must generate different instructions if the processor has a different instruction set. Most compilers support multiple instruction sets, so you don't need to change the compiler just because the instruction set changes.

Does it depend on the actual instruction set or just its register size (16/32/64 bits)?

AND.



If it only depends on whether it's 64-bit or 32-bit, can't the instruction set look different and machine code doesn't work? I am confused by this because 32-bit and 64-bit versions of the application are always offered, although there are several possible instruction sets.

Although the desktop market is dominated by different x86 and x64 instruction sets, most software vendors see no need to support anything else.

Is the OS running on top of a specific set of commands, which compiler is needed and why? Isn't the machine code the same if the CPU is the same?

The command set is the same, but the way of communicating with the operating system is different. Basically you can run a Windows application on Linux if they both run on the same toolbox, however the application will say β€œwindows open this file” and linux doesn't know what to do. The wine project fills in the gap and runs Windows applications under Linux, converting "windows open the file" to "linux open the file", which gets tricky if additional Windows functionality is needed.

+3


source


  • It definitely depends on the machine your program will run on. Not only on registers 8/16/32/64, but also (and more importantly) on the instruction set itself. If you try to run a 32-bit x86 program on a 32-bit ARM processor, of course it won't work. This is simply because machine code is very architecture specific and thus determines which bit does / what means. Although some instructions may be the same length, the order of the operands may be different, or certain bits may have different meanings, or (very likely) the opcodes themselves may be completely different. Hence a MOV

    on x86 can also have the same opcode asBLOWUPTHEWORLD

    on a different architecture. This is why specific compilers exist for each architecture. Embedded systems (I think everyone from plane controller to RaspberryPis to AVRs that drive toy cars) have different compilers for almost every processor / microcontroller family.

    Differences other than 32 or 64 bits that you see in most applications are somewhat shielded by the operating system, because one of the jobs of the OS is to provide abstractions (think of system calls such as open

    with a file) whatever that means by itself (in terms of low-level instructions). 32 vs 64 bit is a difference, even the OS can't hide.

  • Yes. Think about dynamic libraries. Microsoft DLLs will definitely not work on Linux unless some guy decodes them manually. Viceversa, shared objects (.so) in Linux will not work on Windows simply because they are built in completely different ways.



+2


source


All of the factors that you speak of are important to your choice of compiler. The set of instructions, ABI, including the register size, the operating system you are trying to deploy, and what you want the compiler to run. You can run different operating systems on the same computer (like buying an iMac and you can install macOS, multiple Linux flavors, multiple Unix, or Windows), so a single processor isn't enough to decide which compiler you need. Sometimes you may be using the same compiler and need a different set of header files that allow you to access the capabilities of your operating system.

However, this does not guarantee the choice of a particular compiler. A compiler is just an application program. A converter that converts text into machine code. Thus, there are many compilers that can run on multiple platforms (for example, ABI, OS and CPU associations) and many compilers that can generate executables for multiple platforms. OTOH for some smaller processors and OSs (like embedded CPUs) sometimes there is only one compiler.

+1


source







All Articles