<div dir="ltr">I had a quick look at some reference material this time :-) I also did some work on a DEC Rainbow and a Kaypro CP/M luggable a few years later. The compilers of the time were just awful!<div><br></div><div>It should be possible to get llvm to produce very good code for the Z80 -- in larger functions probably better than any human would put the effort in to achieve. In particular, what the human regards as the "same variable" (but a different SSA value) might live in different registers at different times.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Jul 22, 2018 at 12:26 AM, Michael Stellmann <span dir="ltr"><<a href="mailto:Michael.Stellmann@gmx.net" target="_blank">Michael.Stellmann@gmx.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Thanks Bruce,<br>
<br>
and elaborately as ever. Again, I'm surprised about your very thorough Z80 knowledge when you said you only did little on the ZX81 in the eighties :D<br>
<br>
OK, understood. I was first thinking about doing something like this for small frames:<br>
<br>
1.  push bc # 1 byte; 11 cycles - part of call frame-cleanup: save scratch register<br>
<br>
    +-----begin call-related<br>
2.  ld <rr>,stack-param<br>
3.  push <rr><br>
     ... more code to load non-stack params into registers<br>
4.  call ...<br>
5.  pop bc # 1 byte ; 10 cycles - call frame cleanup: restore stack-pointer (value in BC is not used)<br>
    +-----end call-related<br>
<br>
6.  pop bc # part of call frame-cleanup: restore scratch reg's value<br>
<br>
The stack cleanup would insert line 5, and have to insert lines 1 and 6 - summing up to 3 bytes of instructions - and maybe the outer two could be eliminated in a late optimization pass, when register usage is known.<br>
<br>
But then again - looking at your math and the *total* mem and cycles, incl. setup and tear-down - convinced me of dropping my complex idea with saving "BC" and use it for cleanup or sacrificing the calling convention. The complexity just doesn't justify the gains. Instead, going for easy-to-implement solutions:<br>
For small call frames with only 1 or 2 params on the stack, two "inc sp" (1 byte, 6 cycles per inst) per parameter can be used, and your "big stack frame" suggestion for larger ones.<br>
<br>
This also allows keeping a "beneficial" param and return value calling convention:<br>
I want to assign the first 3 (HL + DE + BC - or at least 2) function params to registers, so the stack cleanup is only required for functions with more than 3 parameters at all - or vararg funcs.<br>
And only functions with more than 5 params will need the "big stack frame" cleanup. Those cases are rare (or at least can be avoided easily by a developer), or, knowing the mechanics, shouldn't be used for time critical inner loops anyway.<br>
Being able to keep HL for the return value allows very efficient nested function calls in the form "Func1(Func2(nnn));", as register shuffling can be avoided - the result of Func2 can be passed directly Func1.<br>
<br>
Thanks for pointing me again to the right direction!<br>
<br>
Michael<br>
<br>
Oh, and BTW, I'm planning to do the backend primarily for the MSX - my first computer in 1984. Just for the fun of it, I started now writing a small game for it after 25+ years of absence, and was wondering what 30+ years compiler technology would be able to achieve on such a simple (but challenging, as in "not-alway-straightforward") CPU ;-)<br>
<br>
</blockquote></div><br></div>