1

A quick search on the role of compiler brings up the following:

A compiler is a special program that processes statements written in a particular programming language and turns them into machine language or "code" that a computer's processor uses.

However, I also read that every language needs to have a target runtime system and programs written in that language execute in the context of the runtime system.

Adding up the two, it appears that a compiler converts programs into bits understandable by the runtime system and the runtime system acts as the agent interpreting the bits, translating them to the processor intructions. Is this correct? If not, how else are compiler and runtime system related?

And if the above is correct, it would imply the following:

  1. two executables compiled from programs with identical logic written in different languages would not be identical
  2. an average machine would have dozens of runtimes which doesnt seem to be the case.

What am I missing here?

6

two executables compiled from programs with identical logic written in different languages would not be identical

True!

an average machine would have dozens of runtimes which doesnt seem to be the case

Actually it is quite common to have several runtimes installed on a system. It just depends on how many languages you've installed. Sometimes several languages can share a common runtime (Java, JRuby, Jython, Scala, ..). Or you can have multiple versions of a single language that all use different run times. There are several different implementations of the Java runtime for example.

the runtime system acts as the agent interpreting the bits, translating them to the processor intructions. Is this correct? If not, how else are compiler and runtime system related?

I think you are conflating runtimes with virtual machines. Virtual machines are a specific flavor of runtime.

Some language implementations compile source directly to the native machine code for the physical hardware, and the runtime is just a library of functions that takes care of housekeeping details like memory organization, process initialization, and low level IO. C, C++, Go, and Rust are typically implemented like this.

Other language implementations compile source to instructions for a "virtual machine". In that case the runtime is an executable that implements the virtual machine using the instructions for the native hardware. This seems to be the type of runtime you are thinking of. Java, Python, and C# are usually implemented using virtual machines.

|improve this answer|||||
  • Actually, both Scala and JRuby have pretty sizeable runtimes of their own, and I assume the same is true for Jython (with which I am not familiar enough to say definitively). – Jörg W Mittag Nov 9 '17 at 0:36
1

The statement that every language needs a runtime is false. Many early computers and many current small embedded processors have hardware instruction decoders and let programs compiled to binary machine code write directly to the IO registers, no run time of any sort needed after the program is loaded (unless you want to consider, for instance, the microcode in an MC68000, or the RTL for an FPGA RISC soft processor to be a runtime).

The problem is this kind of code is highly non-portable.

|improve this answer|||||

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.