hlorenzi / customasm

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

Sometimes subruledef's aren't evaluated in asm{...} blocks #170

Open chrisgbk opened 1 year ago

chrisgbk commented 1 year ago

In 12.0, following is valid:

#subruledef reg
{
    x{val: u4} => val`4
}
#ruledef test
{
    test {imm: u8} => imm
    testasm {R1: reg}, {R2: reg} => asm{test (R1 @ R2)}
}

    testasm x0, x1 ;succeeds
 outp | addr | data

  0:0 |    0 | 01 ; testasm x0, x1

which obviously fails in 13.x with the requirement of enclosing parameters in {}; however enclosing in {} also fails:

#subruledef reg
{
    x{val: u4} => val`4
}
#ruledef test
{
    test {imm: u8} => imm
    testasm {R1: reg}, {R2: reg} => asm{test ({R1} @ {R2})}
}

    testasm x0, x1 ;fails
error: failed to resolve instruction
  --> test.asm:11:2:
 9 | }
10 |
11 |   testasm x0, x1
   |   ^^^^^^^^^^^^^^
   + note: within `test`, rule 1
   --> test.asm:8:2:
  8 |   testasm {R1: reg}, {R2: reg} => asm{test ({R1} @ {R2})}
    |   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    + error: failed to resolve instruction
     --> test.asm:8:38:
    6 | {
    7 |   test {imm: u8} => imm
    8 |   testasm {R1: reg}, {R2: reg} => asm{test ({R1} @ {R2})}
      |                                       ^^^^^^^^^^^^^^^^^^
    9 | }
   10 |
      + note: within `test`, rule 0
      --> test.asm:7:2:
     7 |   test {imm: u8} => imm
       |   ^^^^^^^^^^^^^^
       + error: unknown symbol `x0`
        --> test.asm:8:45:
       6 | {
       7 |   test {imm: u8} => imm
       8 |   testasm {R1: reg}, {R2: reg} => asm{test ({R1} @ {R2})}
         |                                              ^^
       9 | }
      10 |

it seems the parameters can end up out of scope.

However, as a workaround, the following does work:

    testasm2 {R1: reg}, {R2: reg} => 
    {
        temp = (R1 @ R2)
        asm{test {temp}}
    }

So I'm not sure if this is best handled as a known/documented incompatibility between 12.0 and 13,x and the workaround is best practice for this situation, or if there is actually an issue to address. A note that conversions/calculations required in asm{...} blocks should be performed with temporary values in the wiki could suffice.