hlorenzi / customasm

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

Asserts in subruledefs are ignored when calling asm{...} #169

Closed chrisgbk closed 1 year ago

chrisgbk commented 1 year ago
#subruledef subtest
{
    {x: s32} =>
    {
        assert(x > 0)
        assert(x < 10)
        x
    }
}
#ruledef test
{
    test {s: s32} => s
    testasm {a: subtest} => asm{test {a}}
}

testasm 121

This will happily assemble. In my view, the 121 should hit the assert and fail, before the attempt to call asm{...}

In my specific use case, I have instruction macros that have different encodings but the same length depending on the range of a value, but this behavior results in an inability of the assembler to catch which one is the wrong one, as they are all accepted as being correct encodings without any assert failures.

As a workaround, I can move every assert out of the subruledef, but this results in a lot of duplication.

chrisgbk commented 1 year ago
#subruledef cpu6502_reladdr
{
    {addr: u16} =>
    {
        reladdr = addr - $ - 2
        assert(reladdr <=  0x7f)
        assert(reladdr >= !0x7f)
        reladdr`8
    }
}

#ruledef cpu6502
{
    bcc {addr: cpu6502_reladdr} => 0x90 @ addr
}

#ruledef cpu6502_macro
{
    bccmacro {addr: cpu6502_reladdr} => asm{bcc {addr}}
}

bcc 120
bccmacro 16384
bcc 120
 outp | addr | data (base 16)

  0:0 |    0 | 90 76 ; bcc 120
  2:0 |    2 |       ; bccmacro 16384
  2:0 |    2 | 90 74 ; bcc 120

An extra test; asm{...} seems very broken currently.

hlorenzi commented 1 year ago

I've managed to fix both errors, and I'll release a new version soon! Thank you so much for the reports!