hlorenzi / customasm

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

"Unknown variable" when forwarding primitive arguments #161

Closed SamiKalliomaki closed 1 year ago

SamiKalliomaki commented 1 year ago

I was trying to do argument forwarding where some of the middle layers are generic.

#subruledef register {
  rx0 => 0xFF
}

#subruledef reg_or_imm {
  {reg: register} => reg
  {imm: i8} => imm
}

#subruledef imm {
  {imm: i8} => imm
}

#ruledef {
  foo {reg: register} => 0x01 @ reg
  foo {val: i8} => 0x02 @ val

  bar {arg: reg_or_imm} => asm { foo {arg} }

  test_1 {asd: reg_or_imm} => asm { bar {asd} }
  test_2 {asd: imm} => asm { bar {asd} }
  test_3 {asd: i8} => asm { bar asd }
}

test_1 rx0 ; works = 0x01 0xFF
test_1 0x03 ; works = 0x02 0x03

test_2 0x03 ; works = 0x02 0x03

test_3 0x03 ; does not work, expected 0x02 0x03

I also tried test_3 {asd: i8} => asm { bar {asd} } but that is the same result. I would expect test_2 and test_3 to behave identically.

I get error:

error: failed to resolve instruction
 --> test.asm:30:1:
 28 | test_2 0x03 ; works = 0x02 0x03 
 29 |  
 30 | test_3 0x03 ; does not work, expected 0x02 0x03
    | ^^^^^^^^^^^                                     
 === error: unknown variable
 --> test.asm:18:38:
 16 |   foo {val: i8} => 0x02 @ val 
 17 |  
 18 |   bar {arg: reg_or_imm} => asm { foo {arg} } 
    |                                      ^^^^^    
 19 |  
 20 |   test_1 {asd: reg_or_imm} => asm { bar {asd} } 

It seems somehow related to the variable names - if I use the same variable name throughout, everything works.

SamiKalliomaki commented 1 year ago

This works (replacing argument name asd with arg):

test_3 {arg: i8} => asm { bar arg }
hlorenzi commented 1 year ago

Hmm, I was going to say you should just use test_3 {asd: i8} => asm { bar {asd} } (with braces) as a workaround for now, but that doesn't work either (because it's an i8 argument?). I'll have to investigate further.

hlorenzi commented 1 year ago

This should be working on v0.13.0 using braces {} as I've described in the previous comment!