Kryptostack
|
B __compile
activates compiling features of bind
. A procedure X is internally modified in an arbitrary manner by setting true __compile X bind
. However, it is guaranteed that the modified X has the same effect as X when executed. Furthermore the procedure X is set to read-only to mark the applied changes.
An initial pass iteratively creates new optimization opportunities, thus:
2 3 5 7 11 mul mul mul mul ==> 2310
Idea: Use the syntax of Forth for compiling or optimizing:
: 2dup 2 copy ;
which corresponds to:
/2dup { 2 copy } bind def
First, examples of poor performance are needed.
In the first phase, only very simple linear optimizations of performance will be carried out.
Substitution of elementary sequences to simplify using unregistered operators.
1 --sub-- ==> --__isI-- --__---- 1 --add-- ==> --__isI-- --__++-- 0 --add-- ==> 0 --eq-- ==> --__eq0--
This is related to the PostScript idea of "Idiom Recognition". Examples in Forth would be:
2 --copy-- ==> --__2dup-- --exec-- --pop-- ==> --__nip-- 3 2 --roll-- ==> --__rot-- 3 1 --roll-- ==> --__-rot--
The equivalent of constexpr in C++.
The following example evaluations should be converted at the time of __compile
:
1 1 --add-- ==> 2 1.2 2.0 --sub-- ==> -0.8 1.0 2.0 --mul-- ==> 2.0
true false or ==> true bool {x x} {x x} --ifelse-- ==> x x bool {x x} --if-- ==> x x | {}
literal1 literal2 --exch-- ==> literal2 literal1
{x y z} --exec-- ==> x y z
[ x ] --cvx-- ==> { x }
(A) (A) ?
As described in the Red Book.
If __compile
in a procedure knows which operands will be produced, it can actually drop some of the stack checks.
Replacement by the single operator with the extension of if, ifelse, repeat, etc. to support this as well.
Like make with a timestamp.
Mode in which fewer range checks and case defaults are included.
e.g., the main interpreter loop or especially type checks of many operators
Possibly make relaxed mode switchable on and off, leaving it up to the script author?
In the course of considering UTF, also introduce 32-bit integers for loops, indices, characters, etc.