#Mikroforth has lots going on, see image how much code I can save.
Mikroforth for Cortex-M has a lot going on under the hood, with the main goal being to strip the Forth-OS completely out of the application layer.
To pull this off, I run quite a few optimizations under the hood:
Dead-code elimination (Trimming): I strip out all uncalled words to ensure we are only dealing with addressable memory within the code.
Constant folding: I calculate values ahead of time where possible and assign them directly to memory.
Immediate literals: Literals become immediate—basically interpreting the Forth at compile time and storing the resulting values.
Branch optimization: A comparison flag followed by if, while, or until gets fused into a single compare and conditional branch. This also handles do ... if constructs and not inversions (among other patterns).
Stack optimization: A literal followed by an A-word that starts by popping its operand is collapsed into a direct register load.
Inlining A-words: Primitives written in assembly get copied inline whenever they end with a single bx lr and contain no calls, no push/pop instructions, no inline data, no named labels, and no references to sp, lr, or pc.
Literal inlining: Number literals get inlined directly as well, and so on.
You can see the resulting size difference in the image below.
In benchmarks featuring heavy literal use versus raw stack manipulation, it hits roughly 95% the performance of C implementations for SHA-1 and Huffman encodings.
#Forth #cortexM #RP2040 #PICO #STM32