r/AskComputerScience • u/dgpking • 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?
4
u/claytonkb Apr 25 '21
We usually distinguish between (a) compiled languages (like C++) and (b) interpreted languages (like Python). The CPU itself, however, only ever executes machine code. What makes compiled and interpreted languages different is that the interpreted language will have some kind of table of basic operations that it can perform (often called a "bytecode machine"). Each of these basic operations exists as snippets of machine code in the computer's memory. While the interpreter is executing the interpreted language, each "instruction" in the interpreted bytecode causes the interpreter to "branch" to the specific snippet of machine-code corresponding to that bytecode instruction. If that's not clear, just web search and read the Wikipedia articles on this, they're quite good.
Bear in mind that the word "programming" is broad enough to include things that are not a CPU or a standard computer at all. So, you can "program" a CNC router, you can "program" an industrial control that (physically) controls a petroleum processing plant, you can "program" an audio-processing software package, you can "program" 3D-rendering software, you can even "program" the logic circuits for a CPU chip design. And of course, there's plain old software programming.
There is nothing about a CPU that prevents it from being designed to directly read source code. The trouble is that the circuitry would be complex (would take up a lot of space), would not be used very often (thus wasting that CPU die space), and would have poor performance relative to the standard software compilation / interpretation flows that we use today. There have been CPUs that have been designed to read intermediate layer, such as Java bytecode, or even Lisp machines which could directly read and operate on Lisp intermediate-layer code.
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).
7
u/teraflop Apr 25 '21
I'm not sure why you're being downvoted, because this is correct (or at least a lot more correct than the current top-voted comment).
6
u/Knaapje Apr 25 '21
I have no clue either. People see a downvoted comment and downvote for no reason I guess? Oh well.
3
u/BeautifulPiss Apr 25 '21
Doesn't it end up as machine code either way? I was under the impression that it doesn't change the actual code that's being generated, just how.
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.
4
u/Tornado2251 Apr 25 '21
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.
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.
2
u/Knaapje Apr 25 '21 edited Apr 25 '21
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.
2
u/a4555in Apr 25 '21
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?
3
u/Knaapje Apr 25 '21
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.
1
u/a4555in Apr 25 '21
Ah I see. I was about to ask you to elaborate but the comment below does exactly that.
1
u/newytag Apr 28 '21
Not all code gets executed on a machine. I'm sure there's lots of code snippets on GitHub that have never been run and hence never been turned into machine code.
But any programming code that wants to be executed on a machine, needs to be machine code at some point. When that happens depends on the language and environment used.
Assembly is essentially machine code in a human-readable format, so it is not necessary and usually not included as a step.
1
0
Apr 25 '21
[deleted]
0
u/dgpking Apr 25 '21
Yea, thanks, makes sense, I feel I have another question... just not sure what it is yet 🙈😄
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.