Close

Release 0.11.0

A project log for zeptoforth

A full-featured Forth-based RTOS for ARM Cortex-M

travis-bemannTravis Bemann 09/12/2020 at 04:016 Comments

This release includes a optimizes constant arguments to inlined words, including, when possible, constant arguments to inlined instructions in such words, such as +, -, AND, OR, XOR, LSHIFT, RSHIFT, ARSHIFT, B!, H!, and !.

Discussions

Thomas wrote 09/14/2020 at 10:54 point

That's impressive! How does the compiler "know" that FOO is immutable?

  Are you sure? yes | no

Travis Bemann wrote 09/14/2020 at 17:30 point

It analyzes the instructions in FOO and only if these instructions are identical to that of a CONSTANT with a given value does it treat the word as being able to be optimized away. At the present the word also needs to be marked as inlinable, even though I could potentially in the future add some degree of automatic inlining.

  Are you sure? yes | no

Travis Bemann wrote 09/14/2020 at 18:58 point

The particular instructions it looks for are:

PUSH {LR}

SUBS R7, #$4

STR R6, [R7]

Either:

MOVS R6, #<lower 8 bits>

or:

MOVW R6, #<lower 16 bits>

or:

MOVW R6, #<lower 16 bits>

MOVT R6, #<upper 16 bits>

and then:

POP {PC}

  Are you sure? yes | no

Thomas wrote 09/12/2020 at 09:54 point

Nice :-)

How do you collapse phrases onto single instructions?

Are there limitations (e.g. "readADC 2 *" works but "2 readADC * " doesn't)?

  Are you sure? yes | no

Travis Bemann wrote 09/13/2020 at 17:24 point

The main limitations are that it only collapses instruction sequences where the last argument is a constant, and that it never statically evaluates expressions. However such constants include ones defined with CONSTANT.

  Are you sure? yes | no

Travis Bemann wrote 09/13/2020 at 17:54 point

Also note that any inlined word instruction-for-instruction equivalent to a word defined with CONSTANT is treated similarly by the optimizer. Take the following for instance:

: FOO 4 [INLINED] ;  ok
SEE FOO
2000045C B500:      FOO:                  PUSH {LR}
2000045E 3F04:                            SUBS R7, #$4
20000460 603E:                            STR R6, [R7]
20000462 2604:                            MOVS R6, #$4
20000464 BD00:                            POP {PC}
 ok
4 CONSTANT BAR  ok
SEE BAR
20000474 B500:      BAR:                  PUSH {LR}
20000476 3F04:                            SUBS R7, #$4
20000478 603E:                            STR R6, [R7]
2000047A 2604:                            MOVS R6, #$4
2000047C BD00:                            POP {PC}
 ok
: +FOO FOO + ;  ok
SEE +FOO
2000048E B500:      +FOO:                 PUSH {LR}
20000490 3604:                            ADDS R6, #$4
20000492 BD00:                            POP {PC}
 ok
: +BAR BAR + ;  ok
SEE +BAR
200004A6 B500:      +BAR:                 PUSH {LR}
200004A8 3604:                            ADDS R6, #$4
200004AA BD00:                            POP {PC}
 ok

  Are you sure? yes | no