8

I'm a little confused as to how the PVM gets the cpu to carry out the bytecode instructions. I had read somewhere on StackOverflow that it doesn't convert byte-code to machine code, (alas, I can't find the thread now).

Does it already have tons of pre-compiled machine instructions hard-coded that it runs/chooses one of those depending on the byte code?

Thank you.

2 Answers 2

7

It's a lot higher-level than machine language. There's a giant switch statement that looks at each opcode and decides what to do based on the opcode. Here are some snippets:

switch (opcode) {
...
TARGET(LOAD_CONST) {
    PyObject *value = GETITEM(consts, oparg);
    Py_INCREF(value);
    PUSH(value);
    FAST_DISPATCH();
}
...
TARGET(UNARY_NEGATIVE) {
    PyObject *value = TOP();
    PyObject *res = PyNumber_Negative(value);
    Py_DECREF(value);
    SET_TOP(res);
    if (res == NULL)
        goto error;
    DISPATCH();
}
...
TARGET(BINARY_MULTIPLY) {
    PyObject *right = POP();
    PyObject *left = TOP();
    PyObject *res = PyNumber_Multiply(left, right);
    Py_DECREF(left);
    Py_DECREF(right);
    SET_TOP(res);
    if (res == NULL)
        goto error;
    DISPATCH();
}

The TARGET and DISPATCH stuff is part of an optimization that doesn't quite go through the regular switch mechanics. Functions like PyNumber_Negative and PyNumber_Multiply are part of the Python C API, and they dispatch operations like negation and multiplication.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you. So the bytecodes get translated (maybe interpreted is the better word) into different opcodes by the VM, the VM writes these opcodes to some memory, which then the CPU fetches and carries out (these opcodes)? I looked up opcodes and it seems opcodes are machine instructions. So I'm assuming the opcodes are some sort of assembly language used to communicate the the instructions to the CPU?
@Moondra: No. CPython does not translate bytecode to machine language in any way. It does not write instructions for the CPU to execute. This giant switch statement looks at a bytecode instruction and decides what to do. Bytecode instructions are instructions to this giant switch statement, not to the CPU. This giant switch statement is compiled to machine language, as are the functions it calls and the rest of the interpreter machinery, but bytecode is not.
I think I got it. I just want to clarify some details. It essentially sounds like a pseudo-dictionary with the bytecode looking up its associated switch instruction/value. Each line(?) of bytecode (which would be the key) find its associated switch value/instruction (value) -- that switch value get compiled into machine language instruction which the CPU will eventually execute. So there are enough switch values to match all the different types of bytecode instructions. The term opcodes is referring to the multitude of switch values and instructions?
@Moondra: CPython doesn't compile anything to machine language at runtime. You should probably go learn about C and how C programs are compiled before trying to understand how the interpreter fits on top of that.
@so besides that point, is everything else I"m understanding correct? Thank you.
3

If you mean Standard Python (CPython) by Python, then no! The byte-code (.pyc or .pyo files) are just a binary version of your code line by line, and is interpreted at run-time. But if you use pypy, yes! It has a JIT Compiler and it runs your byte-codeas like Java dn .NET (CLR).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.