Tips or strategies to understanding how CPython works under the hood

Robert O'Shea robertoshea2k11 at gmail.com
Thu Jan 11 08:59:20 EST 2018


Thanks all for the links and suggestions, they are greatly appreciated. I
might be programming for a long time (relative to my age) but I haven't
touched much on compilers or interpreters. Inspired a but by Python's
interpreter I wrote a little bytecode interpreter in C (maybe should have
upgrade to C++ but not yet), I enter custom bytes into a program array,
currently can jump, add/subtract, print values on stack and halt the
interpreter. I'm happy with what I've done so far. A compiler is out of my
reach for the moment so I'm going to research that after learning a good
bit of Python's internals.

On Wed 10 Jan 2018, 12:13 bartc, <bc at freeuk.com> wrote:

> On 09/01/2018 20:12, Alain Ketterlin wrote:
> > ElChino <elchino at cnn.cn> writes:
> >
> >> Chris Angelico wrote:
> >>
> >>> CPython is a stack-based interpreter, which means it loads values onto
> >>> an (invisible) internal stack, processes values at the top of the
> >>> stack, and removes them when it's done.
> >>
> >> Is this similar to how Lua operates too?
> >
> > No. Lua uses a register-based (virtual) machine. See
> >
> > https://www.lua.org/doc/jucs05.pdf
>
> "Registers are kept in the run-time stack ... an array".
>
> So it sounds like a stack is still used, but instructions directly
> access specific slots on the stack, within a particular register window.
>
> It means there need be fewer instructions to implement some code, but
> each has more operands.
>
> Also interesting is that the comparison operators include only EQ, LT
> and LE. There is no NE, so no issues such as those discussed recently.
>
> >
> > I think Lua was the first language in widespread use to move to a
> > register-based machine.
>
> I believe stack-based byte-code, which is very easy to generate, can be
> transformed to some 'register'-based version, if performance is the
> motivation.
>
> (But I'm not convinced that register-based is necessarily faster. Lua is
> quite fast, especially LuaJIT, but the language is also smaller and
> simpler.)
>
> --
> bartc
> --
> https://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list