I'm making some progress getting the vbcc compiler to generate code for the #vtech #vsmile. However, the CPU has only 4 general purpose registers (R1-R4) with the other ones (SP, PC, SR/flags and BP/framepointer) already in useby the cpu itself or the code generator.

The register allocation in vbcc endsup allocating all 4 registers at the start of a function and freeing them at the end, despite not actually using them that much. So all code that needs a temporary register has to push/pop one

This works ok, until th.point where a PUSH operation (to push a function call parameter on the stack) tries to use a temporary register, and emits an extra push/pop that results in the wrong parameter value being passed.

Of course I could reserve one extra register permanently for use by the compiler, but that would make the code generation worse everywhere else (since there are already few available registers).

Maybe I can write an optimization pass that tries to propagate the register allocations/frees closer to where they are acually used? But I'm not sure how easy that is, since the code has various branches.
Maybe I can find some clever use of the instruction set, or allocate myself some scratch area in ram at a fixed address to backup my temporary register in this case?