ashmind / SharpLab

.NET language playground
https://sharplab.io
BSD 2-Clause "Simplified" License
2.72k stars 199 forks source link

Improve label handling in ASM #849

Open am11 opened 3 years ago

am11 commented 3 years ago

A few suggestions for labels in ASM, which I think may improve end user experience:

  1. Emit used labels only: label for each instruction is noisy. We are using look-ahead buffer in emitter. It would require us to change that to make it possible to insert or manipulate nodes in memory (preferably by avoiding copying of nodes).
    • current SharpLab's output is not useful for assemblers is the wild, as they complain about duplicate labels (among other things like method signatures, that are not commented).
  2. Emit unique labels: the labels are generated relatively with this addressing formula similar to {PC/IP - current method's address} and jumps in resolver are based on {current method's address - given token's address}. We can use a global counter to increment the labels nicely.
  3. Emit constant string or data sections in global labels: resolve the string from address, create a global label and offset flat:L123.
    • not sure if this is the limitation imposed by clrmd or Iced.Intel or something we can manage in our resolver by our own?

Mainly inspired by godbolt's presentation of Intel syntax: https://godbolt.org/z/srsnvbPW3 vs. https://sharplab.io/#gist:bdd34fbf887666b8ad8c2af70044ef7c (both 32-bit for easy comparison, since symbol resolver for RyuJIT x64 is disabled atm due to another issue).

Aside: I was looking at ways to implement label navigation in ASM viewer. These suggestions, if implemented, will also make it easier to provide that feature, i.e. click on jump label to go-to its declaration as godbolt provides, but better: if it is outside of visible window scroll-to the declaration region.

ashmind commented 3 years ago

Thanks for reporting! I think for both this and your PR I'll likely do some kind of "asm improvement"-focused milestone to take a look at the problem as a whole and all improvements. Can't promise a specific timeline yet though.