hlorenzi / customasm

💻 An assembler for custom, user-defined instruction sets! https://hlorenzi.github.io/customasm/web/
Apache License 2.0
715 stars 56 forks source link

pattern matching error: whitespace and constants after mnemomics cause pattern matching rules to be broken #182

Closed thenorili closed 9 months ago

thenorili commented 1 year ago

If you run the code below, you'll see that the last instruction greedily ignores the whitespace and parses 'r1 5' as 'r15'. I understand that it grabs the shortest output, but this is ignoring whitespace to force a match. I mention this because I think this could be the root cause of the issue.

If you uncomment 'add r1 4 r0', you'll see "unknown symbol 'r1'" and the stacktrace will indicate it's parsing r1 as an immediate despite it not matching the pattern and producing a longer output.

This is worked around by adding a prefix for immediates, but it seems to break the parsing rules about respecting whitespace as a part of the pattern and about prioritizing the match that produces the shortest output.


#subruledef register
{
    r{n:u4} => n`8
}
#subruledef imm
{
    {n:s8} => 0b1 @ n
}
#subruledef value
{
    ; register
    {a: register} => a`8
    ; immediate
    {a: imm} => a`9
}
#subruledef ALU
{
    add => 0x00
    or => 0x01
}
#ruledef
{
    {op: ALU} {a: value} {b: value} {r: register} => 
    {
        (op | ((a >> 8) << 7) | ((b >> 8) << 6))`8 @ a`8 @ b`8 @ r`8
    }
    {a: value} => a`8
    {a: value} {b: value} => a`8 @ b`8

}

multiply3x4:
    add r1 r2 r3
    ; add r1 4 r0
    r1
    r1 r5
hlorenzi commented 11 months ago

Yeah, this seems really strange. I can see the weird behavior if I add another instruction r1 5. I'll need to do a deep dive in the code.

hlorenzi commented 9 months ago

This seems to be fixed on v0.13.5! Can you please let me know if you still experience the issue?