faster higher order call
Peter Schachte
pets at cs.mu.oz.au
Tue Jun 24 15:14:25 AEST 1997
On the theory that higher order call needs to be made as efficient as
possible (so that, unlike Prolog programmers, Mercury programmers really
do a lot of higher-order programming without having to worry to much about
the cost), I think it's worth taking a bold step.
Here's the suggestion: change order of arguments for all calls, so that
the last input argument goes in r1, the second last in r2, etc. Then
call/N could be implemented by N-1 ordinary loads of registers (loading
the first registers with the "extra" arguments) followed by inline code to
unload the curried arguments into registers starting with N, followed by
an indirect call to the code for the closure.
Ok, one problem is that you don't know at compile-time how many arguments
are curried, and some of the arguments have to go into machine registers,
and others have to go into memory. But we do know at compile-time how
many registers are machine registers, so we can generate code like (say N
= 3, ie we have 2 extra args, and the first 6 argument registers are real
registers):
n = <number of curried args>;
switch (n) {
default:
from = <ptr to fifth last arg in closure>;
to = <start of memory registers>;
end = to + n;
while (to <= end) {
*to++ = *from++;
}
case 4:
r6 = fourth last arg from closure;
case 3:
r5 = third last arg from closure;
case 2:
r4 = second last arg from closure;
case 1:
r3 = last arg from closure;
case 0:
}
set cp and branch to code referred to by closure;
Certainly this code is ugly, but it could be put in a C macro which would
be written to work for any number of real registers up to some fixed
limit, say 16 or even 32, using lots of #ifs.
I think this approach should be about as efficient as is possible for HO
call, given that you have to copy arguments out of a structure and into
registers.
Comments?
-Peter Schachte URL: http://www.cs.mu.oz.au/~pets/
pets at cs.mu.OZ.AU PGP: finger pets at 128.250.37.150 for key
[A computer is] like an Old Testament god, with a lot of rules
and no mercy. -- Joseph Campbell
More information about the developers
mailing list