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

Show parent comments

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.

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 😊