rainwoodman / tinypy

tpy, a pet python forked from tinypy
Other
21 stars 5 forks source link

compiler reports incorrect number of REGS per frame. #24

Closed rainwoodman closed 3 years ago

rainwoodman commented 3 years ago

The compiler generates a REGS byte code at the entry point of a frame to ask the VM to allocate regs for the frame.

We are reporting a number less than the code actually uses. As a result, the garbage collector misses objects currently in the regs and causes a crash.

We can fix this by reading the encoder more carefully. In the longer term, however, I do not see why we need to reserve REGS at the beginning of the frame. I thought we can keep asking for scratch space from the stack and simply reset the stack top upon exiting the frame. This way the compiler only need to locally minimize the use of stack space but there is no need to count the maximum stack usage in the frame, which may not even be possible, e.g. alloc() in a loop) at compile time.

rainwoodman commented 3 years ago

The problem appears to be in do_class. We create a new 'frame' with D.begin(), but the vm is not aware of this. Therefore, the REGS bytecode overwrites the cregs of the current frame.