r/AskComputerScience • • Apr 25 '21

Does all programming turn into machine code?

I don’t know if this question makes sense but when you write code in whatever language, python, C, Java etc does it all eventually get converted into assembly and then machine code?

35 Upvotes

32 comments sorted by

View all comments

22

u/[deleted] Apr 25 '21

I don't think assembly is a requirement but yes every language gets turned into machine code. It's just a matter of WHEN. Compiled languages and turned into machine code before you run the code. So a compiled language turned into an exacutable file is fully compiled. Meanwhile, interpreted languages are always compiled as you run the code. So a interpreted exacutable would likely be a bunch of text files that get compiled while you're using the program. Wither way, every language needs to convert the code to machine code.

26

u/teraflop Apr 25 '21

Meanwhile, interpreted languages are always compiled as you run the code.

This is not really accurate.

In an interpreted language, the code is translated into some kind of data structure that represents the instructions (which might be as simple as a list of strings, or something more complex like an abstract syntax tree).

In order to execute the code, an interpreter has to look at the data structure and follow the instructions. But the instructions are never translated into machine code. The only machine code that's running is the interpreter's code. So, for instance, if you want to take an interpreter and port it to a new CPU architecture, you might need to recompile the interpreter itself, but you don't need to modify the interpreter's code to take into account a different kind of machine code.

As an analogy, think about typing text into a word processing program. There's going to be some kind of data structure that keeps track of where each typed letter appears on the page. And you can think of this data structure as being "instructions" for rendering a page (either on a screen or a printer). But the text is never represented in the form of machine code; it's just data that the word processor's code is operating on.

If a system translates source code into machine code "on the fly", it would be more correct to call it a just-in-time compiler, not an interpreter.

0

u/[deleted] Apr 25 '21

[deleted]

12

u/teraflop Apr 25 '21

Are you saying that in python an instruction doesn't get turned into machine code and instead there's a python processor that computes the instruction?

Exactly. If you're curious, here's the core of the bytecode evaluator in the latest released version of CPython. It's basically just a giant switch statement that checks each opcode and branches to a block of code that implements it.

Note that this is the reference implementation of the Python language, but it's not the only possible implementation. PyPy is an alternate Python runtime that uses JIT compilation to get somewhat better performance.

That analogy is kinda what I thought a VM was, like the JVM.

In fact, the earliest versions of the JVM were implemented as bytecode intepreters, just like Python. The HotSpot JIT compiler was introduced a couple of years later, with Java 1.2.

Also, note that even when using HotSpot, not all of the Java bytecode is JIT-compiled. Often, programs have a small amount of performance-critical code that is executed very frequently, and a lot of other code that is rarely used. Since generating efficient machine code is expensive, it's better to start by interpreting everything, and then later switch to generating machine code for the functions that turn out to be "hot spots" (hence the name).

5

u/ggchappell Apr 25 '21

Yes, basically. But I would add just one more little tweak.

What is an interpreter? It is a program that takes source code and executes it. Any such program is an interpreter, regardless of how it happens to accomplish its task.

it would be more correct to call it a just-in-time compiler, not an interpreter.

And an interpreter might involve a JIT compiler. That's an implementation detail -- albeit an often important one. It doesn't stop the program from being an interpreter.

2

u/dgpking Apr 25 '21

This I think is what I don’t understand, how can the interpreter run the instructions in the data structure without it eventually becoming machine code?

8

u/teraflop Apr 25 '21 edited Apr 25 '21

Here's a concrete example. Imagine a very, very simple language called Car for controlling a motorized toy car. A program in this language might look like this:

drive forward
turn left
drive forward
turn right
drive forward

And an interpreter for the Car language might look something like this:

for each (line in program) {
    if (line == "turn left") {
        turn_on_left_motor();
        turn_off_right_motor();
    } else if (line == "turn right") {
        turn_off_left_motor();
        turn_on_right_motor();
    } else if (line == "drive forward") {
        turn_on_left_motor();
        turn_on_right_motor();
    } else {
        error_beep();
        crash();
    }
    wait_one_second();
}
turn_off_motors();

The interpreter is just treating the input program as data (a chunk of text that happens to obey the syntax of the Car language) and behaving according to its instructions.

Of course, what the CPU is actually executing is machine code. In particular, when the interpreter calls a function such as turn_on_left_motor, that function has to contain the machine code that executes the appropriate behavior. But that machine code is part of the interpreter; it wasn't generated from the input Car program. So it's not really correct to say that the program was "translated" into machine code.

1

u/dgpking Apr 25 '21

Ah! That makes sense, thank you very much, much appreciated 😊

1

u/romanhaller Nov 23 '21

Great explanation, thank you. Was not aware of this.