rems-project / sail

Sail architecture definition language
Other
622 stars 116 forks source link

Why mapping doesn't take function #758

Open rez5427 opened 3 weeks ago

rez5427 commented 3 weeks ago

I want to implement cm.push, but I've encountered a challenge: using mapping to assemble this instruction would take hundreds of lines. Any suggestions?

mapping clause assembly = CM_PUSH(rlist, spimm) if (sizeof(xlen) == 32) <->
  "cm.push" ^ spc() ^ zcmp_assembly_mapping_32(rlist, spimm) if (sizeof(xlen) == 32)
mapping zcmp_assembly_mapping_32 : (bits(4), bits(2)) <-> string = {
  (0x4, i) <-> "{ra}, " ^ "-" ^ BitStr(to_bits(xlen, i << 5)),
  (0x4, 0b01) <-> "{ra}, -32",
  (0x4, 0b10) <-> "{ra}, -48",
  (0x4, 0b11) <-> "{ra}, -64",
  (0x5, 0b00) <-> "{ra, s0}, -16",
  (0x5, 0b01) <-> "{ra, s0}, -32",
  (0x5, 0b10) <-> "{ra, s0}, -48",
  (0x5, 0b11) <-> "{ra, s0}, -64",
  (0x6, 0b00) <-> "{ra, s0-s1}, -16",
  (0x6, 0b01) <-> "{ra, s0-s1}, -32",
  (0x6, 0b10) <-> "{ra, s0-s1}, -48",
  (0x6, 0b11) <-> "{ra, s0-s1}, -64",
  (0x7, 0b00) <-> "{ra, s0-s2}, -16",
  (0x7, 0b01) <-> "{ra, s0-s2}, -32",
  (0x7, 0b10) <-> "{ra, s0-s2}, -48",
  (0x7, 0b11) <-> "{ra, s0-s2}, -64",
  (0x8, 0b00) <-> "{ra, s0-s3}, -32",
  (0x8, 0b01) <-> "{ra, s0-s3}, -48",
  (0x8, 0b10) <-> "{ra, s0-s3}, -64",
  (0x8, 0b11) <-> "{ra, s0-s3}, -80",
  (0x9, 0b00) <-> "{ra, s0-s4}, -32",
  (0x9, 0b01) <-> "{ra, s0-s4}, -48",
  (0x9, 0b10) <-> "{ra, s0-s4}, -64",
  (0x9, 0b11) <-> "{ra, s0-s4}, -80",
  (0xa, 0b00) <-> "{ra, s0-s5}, -32",
  (0xa, 0b01) <-> "{ra, s0-s5}, -48",
  (0xa, 0b10) <-> "{ra, s0-s5}, -64",
  (0xa, 0b11) <-> "{ra, s0-s5}, -80",
  (0xb, 0b00) <-> "{ra, s0-s6}, -32",
  (0xb, 0b01) <-> "{ra, s0-s6}, -48",
  (0xb, 0b10) <-> "{ra, s0-s6}, -64",
  (0xb, 0b11) <-> "{ra, s0-s6}, -80",
  (0xc, 0b00) <-> "{ra, s0-s7}, -48",
  (0xc, 0b01) <-> "{ra, s0-s7}, -64",
  (0xc, 0b10) <-> "{ra, s0-s7}, -80",
  (0xc, 0b11) <-> "{ra, s0-s7}, -96",
  (0xd, 0b00) <-> "{ra, s0-s8}, -48",
  (0xd, 0b01) <-> "{ra, s0-s8}, -64",
  (0xd, 0b10) <-> "{ra, s0-s8}, -80",
  (0xd, 0b11) <-> "{ra, s0-s8}, -96",
  (0xe, 0b00) <-> "{ra, s0-s9}, -48",
  (0xe, 0b01) <-> "{ra, s0-s9}, -64",
  (0xe, 0b10) <-> "{ra, s0-s9}, -80",
  (0xe, 0b11) <-> "{ra, s0-s9}, -96",
  (0xf, 0b00) <-> "{ra, s0-11}, -64",
  (0xf, 0b01) <-> "{ra, s0-11}, -80",
  (0xf, 0b10) <-> "{ra, s0-11}, -96",
  (0xf, 0b11) <-> "{ra, s0-11}, -112",
}
rmn30 commented 3 weeks ago

Mappings can only call other mappings because they have to be reversible and functions don't necessarily have an inverse. Most of the other assembly mappings use, for example, hex_bits_signed_5 for immediate integers. Could you try that? I'm not sure if there are decimal equivalents.

rmn30 commented 3 weeks ago

In this case I think you could have a mapping:

mapping zcmp_spimm_assembly_4_7_32 : bits(2) <-> string = {
  0b00 <-> "-16",
  0b01 <-> "-32",
  0b10 <-> "-48",
  0b11 <-> "-64",
}

and call this from the assembly mapping for rlist values 5-7 (not sure about 4?). You could define others for the other spimm mappings for 8-b, c-e and f? This would help a bit but it's still a bit verbose. You might be able to get more clever with some arithmetic.