Is each language ultimately compiled into a low-level computer language?

Are not all languages ​​compiled to a low-level computer?

If so, do not all languages ​​have the same performance?

Just wondering...

+7
performance compiler-construction interpreter
source share
6 answers

As pointed out by others, not every language is translated into machine language; some are translated in some form (bytecode, reverse polish, AST), which is interpreted.

But even among languages ​​translated into machine code,

  • Some translators are better than others
  • Some language features are easier to translate to high-performance code than others.

An example of a translator that is better than others is the GCC C compiler. For example, it has many years of work in creating good code and its translations are superior, for example, to the simpler tcc and tcc .

An example of a function that is difficult to translate to high-performance code is the ability of C to perform pointer arithmetic and dereference pointers: when a program is stored through a pointer, it is very difficult for the compiler to find out which memory locations are affected. Similarly, when an unknown function is called, the compiler must make very pessimistic assumptions about what might happen to the contents of objects allocated on the heap. In a language such as Java, the compiler can perform translation better because the type system provides greater separation between pointers of different types. In a language such as ML or Haskell, the compiler can do better, because in these languages ​​most of the data allocated in memory cannot be changed by calling a function. But of course, object-oriented languages ​​and functional languages ​​present their own translation problems.

Finally, translating the Turing language itself is a difficult problem: in general, finding the best translation for a program is an NP-difficult problem , which means that the only solutions known to potentially take time exponentially in program size. This would be unacceptable in the compiler (cannot wait forever to compile several thousand lines), so compilers use heuristics. There is always room for improvement in these heuristics.

+15
source share

It’s easier and more efficient to map some languages ​​to machine language than others. There is no simple analogy that I can think of for this. Closest I can translate Italian to Spanish vs. translation of Khoisan into Hawaiian.

Another analogy is: “Well, the laws of physics are what govern how each animal moves, so why do some animals move as fast as others? Don't they all move at the same speed?”

+14
source share

No, some languages ​​are simply interpreted. They never turn into machine code. Thus, these languages ​​usually work more slowly than low-level languages ​​such as C.

Even for languages ​​that are compiled into machine code, sometimes what comes out of the compiler is not the most efficient way to write this program. Therefore, it is often possible to write programs, for example, in assembly language, that run faster than their C equivalents, and C programs that run faster than their JIT-compiled Java equivalents, etc. (Modern compilers are pretty good, although not so much of a problem these days)

+4
source share

Yes, all programs end up being converted to machine code. BUT:

  • Some programs are translated at compile time , while others are translated on the fly by an interpreter (such as Perl) or a virtual machine (such as original Java)

    Obviously, the latter is much slower as you spend time translating while you work.

  • Different languages ​​can be translated into DIFFERENT machine code. Even when the same programming task is performed. So machine code can be faster or slower depending on the language.

+3
source share

You must understand the difference between compilation (which is translation) and interpretation (which imitates). You must also understand the concept of a universal framework for computing.

A language or set of instructions is universal if it can be used to write an interpreter (or simulator) for any other language or set of commands. Most computers are electronic, but they can be made in many other ways, such as vibes or mechanical parts, or even people by following directions. A good exercise is to write a small program in BASIC, and then the class of students “executes” the program, following its steps. Since BASIC is universal (in a first approximation), you can use it to write a program that simulates a set of commands for any other computer.

So, you can take the program in your favorite language, compile (translate) it into the machine language for your favorite machine, have a translator for this machine written in BASIC, and then (in principle) have a class full of students to “execute” this. Thus, it first comes down to a set of commands for a "fast" machine, and then runs a very very slow "computer". He will still get the same answer, only about a trillion times slower.

Point, the concept of universality makes all computers equivalent to each other, although some of them are very fast and others are very slow.

+2
source share

No, some languages ​​are controlled by the "software interpreter" as byte code .

In addition, it depends on what the language is doing in the background, so 2 identically functioning programs in different languages ​​can have different mechanisms behind the scenes and, therefore, actually run different instructions, which leads to different performance.

+1
source share

All Articles