4 ms·
The PVM – Python Virtual Machine
- tom_mellior 6y agoIf you want to follow along in the Python source code, the relevant file is https://github.com/python/cpython/blob/master/Python/ceval.c https://github.com/python/cpython/blob/master/Python/ceval.c. The interpreter is a loop over a big switch statement on the byte codes. Most operations are pretty simple. For example, here is the implementation of the BINARY_MULTIPLY instruction: case 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(); } This corresponds to the article's description, but it also adds reference counting (the Py_DECREF operations) and error handling. The real work is inside the PyNumber_Multiply function, which must check the operand types and execute the appropriate operation.