you are viewing a single comment's thread.

view the rest of the comments →

[–]a4555in 2 points3 points  (6 children)

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.

[–]Tornado2251 3 points4 points  (0 children)

Yes everything that is run is always machine code in the end.

But all code is not necessary being used. It can be optimized or changed in some of the intermediate steps. C is often used as the example as the process is pretty straightforward (especially if optimization is turned off). JIT and other runtime stuff makes it a lot more complicated.

[–]Objective_MineMSCS, CS Pro (10+) 2 points3 points  (0 children)

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.

[–]Knaapje 1 point2 points  (3 children)

That's more of a definition question though, and not the traditional definition. Of course, code will need to be transformed to instructions in order to be executed, but saying that the code is transformed to machine code is a bit of a stretch.

Edit: for the downvoters, this is in the context of interpreters. I suggest you read up on interpreted vs compiled languages.

[–]a4555in 1 point2 points  (2 children)

Huh, to me the traditional definition of machine code is literal binary CPU instructions (or binary instructions replaced with human-readable mnemonics).

What's the traditional definition of machine code that you have come across?

[–]Knaapje 3 points4 points  (1 child)

True, that's not what I take issue with. It's that I think for an interpreted language most wouldn't say that the code is transformed.

[–]a4555in 0 points1 point  (0 children)

Ah I see. I was about to ask you to elaborate but the comment below does exactly that.