Optimisation

As the compiler generates machine code it is possible for it to scan the code as it is compiled and to optimise the result. This process operates in a single pass, so the optimisation is limited, but never-the-less useful.

The Forth requires parameters on the stack before executing the function. If these parameters are literals it is often possible to reduce the code e.g..

 : TST $03 PORTA C! ;

This would require the following code;

PUSHT \ Make room

LDI TOSL,03 \ literal 03

LDI TOSH,0

PUSHT \ Make room

LDI TOSL,PORTA \ literal PORTA

LDI TOSH,0

CALL C! \ CALL C! routine

RET finished

However, the optimizer produces:

LDI SECL,03 \ literal 03

STS PORTA,SECL \ Store to portA

RET \ finished

This has saved 6 words of storage and several micro seconds of execution time. It would be unusual to make this simple code a definition and so this would normally become in-line code within a larger definition. Also in this example, if the literal 03 was computed at run-time the result is allready in TOS so the compiler just saves TOSL into PORTA and then cleans the stack. Still a significant saving