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 I/O P1OUT C! ;

This would require the following code;

PUSHT \ Make room

MOV # 03 , TOS \ literal 03

PUSHT \ Make room

MOV # P1OUT , TOS \ literal P1OUT

CALL C! \ CALL C! routine

RET finished

However, the optimizer produces:

MOV # 03 , & P1OUT \ literal 03

RET \ finished

This has saved 9 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 MOVs TOS into P1OUT and then cleans the stack. Still a significant saving