osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Tips or strategies to understanding how CPython works under the hood


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