KarolS / millfork

Millfork: a middle-level programming language targeting 6502- and Z80-based microcomputers and home consoles
https://karols.github.io/millfork/
GNU General Public License v3.0
257 stars 21 forks source link

Support identity table usage #129

Open joshop opened 2 years ago

joshop commented 2 years ago

There's an interesting 6502 assembly optimization where you put a 256-byte page aligned table in memory with each byte having the value of its index. Then you can make some pseudo-operations:

KarolS commented 2 years ago

This sounds like something worth pursuing. For example, currently there are a lot of places that have the following code pattern: PHA ... TSX ORA $101,X INX TXS which usually could be simplified to TAX ... ORA identity,X. Variable-to-register optimization could optimize more opcodes (and since these already use cost-to-benefit calculations, they won't have speed regressions). There are also several places that could use TXY/TYX right now (even if they don't use it even on 65816)

I think it would almost always lead to larger code though, so I agree that it should be a flag.

KarolS commented 2 years ago

So I checked and it turned out that the identity table is actually already used in few places at the -Ob level of optimization. I'll add more uses for it, I'll add some optimizations that target it, so that things like LDA identity,X or ORA identity+n are optimized, and I'll move it to a separate flag, so that it can be used without -Ob (which is an optimization level that is a bit ridiculous, with things like loop unrolling etc., and blows up the code size too much).