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

6

u/Knaapje Apr 25 '21

Nope, some languages are compiled (like C), and some are interpreted (like Python). There are also some intermediate stages where source code is compiled to a bytecode which is interpreted (for example Java).

3

u/a4555in Apr 25 '21

I think yes, all code does eventually get converted to machine code as that's the only thing that a CPU/GPU understands.

Wouldn't all code eventually get translated to CPU machine instructions regardless of where it came from? I would assume the JVM translates bytecode to machine code in order to run it on the CPU.

3

u/Objective_Mine MSCS, CS Pro (10+) Apr 25 '21

The JVM just-in-time compiles some of the Java bytecode into machine code in order to speed up the execution of the parts of the code that are important for performance. It doesn't compile everything to native machine code, though, but only the parts that are executed more than a given number of times (or whatever heuristic it uses for determining what code is performance-critical enough to justify the cost of just-in-time compilation). The rest of the bytecode it interprets. Or at least this is how it was last time I read about it.

I'm not knowledgeable enough about interpreter implementation to know what exactly they do in order to "interpret" the code, but I'm sceptical of the comments that say the interpreter code eventually gets "converted" to machine code, then directly executed on the CPU. If I'm wrong in any of the following, I'd like to hear that, but only if that's based on an actual understanding of interpreters.

All code that gets run on the CPU will of course need to be native machine code, as you say. However, that doesn't necessarily mean the code written in the interpreted language ever gets turned into machine code in the simple sense.

AFAIK a simple interpreter wouldn't literally turn the code written in the interpreted language (say, Python) into native machine code and then have the CPU execute it. Rather the interpreter would read the program code that it's expected to interpret as if it were data. The interpreter then presumably contains code to parse the interpreted language, to maintain the state of the interpreted program, and to manipulate that state based on the program code that it's interpreting on the go. The CPU then executes the interpreter code that's maintaining all of the interpreted program's state and code as its data, or as its own state.

Let's say an interpreted program contains an addition of two integers. I suppose at some point an actual integer addition instruction might take place on the CPU. It might, at least in theory, also be that the interpreter implements the addition operating by itself, on the bit level, using bit level manipulations or something. Those bit level manipulations that are part of the interpreter's native machine code would then be what gets executed on the CPU. I guess it's a bit of a matter of definition whether that means the interpreted code gets "turned" into machine code or not.

Of course it would probably be more efficient to use the native machine code instruction for implementing the addition rather than doing a large number of sequential bit-level operations in software. But even if a simple addition were handled by the interpreter by eventually executing a corresponding integer addition instruction on the CPU, I'm pretty sure not all of the code gets treated with a simple mapping to machine instructions.

If the interpreter always literally translated the interpreted code into machine code and then had the CPU run it as-is, that would by definition be just-in-time compilation, not interpretation.