Skip to content

simple peephole optimizer #66

@patricksurry

Description

@patricksurry

One way we could improve space/cycle performance is to use a very simple peephole optimizer. Common pairs of words of the form ( ... -- n ) ( n -- ... ) can often be replaced by a more efficient compound word which avoid stack operations and replace two jsrs with one. Often the compound word could just be an alternate entry point to an existing word where TOS is already in tmp1 or A/Y registers, and/or a flag is set via CPU flags rather than passed as a stack value.

Examples might be things like ?dup if, 0= if, literal +, literal <, ...

I was thinking about trying a simple implementation where compile tracks the last XP it emitted with its corresponding here. When it compiles the next word, it can check if the new XP2 is part of a compound word (XP, XP2). If not, proceed as before. If so, rewind and process the compound instead. This would be a general way of handling literal folding, and would even support longer compound sequences if that was useful (e.g. the compound XP-XP2 could then get rewritten again by a third compound XP-XP2-XP3)

@SamCoVT is this something you'd be interested in? Have you looked at anything along these lines before?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions