-
Hello, I'm working with the Capstone project to generate a RISC-V disassembler module from the Sail description of RISC-V maintained here. Previously Capstone relied on LLVM descriptions and metadata for automatically generating C assemblers and disassemblers, but those were found to be outdated and containing subtle errors. Capstone thus decided to start relying on Sail descriptions of architectures, at least with RISC-V. I wanted to ask a couple of questions that I can't find easy answers to in the documentation. Am I reinventing the wheel ?Sail already has a C backend. The Sail manual linked above seems to imply that all Sail is translatable to C, however, almost all discussions that I see about Sail here and elsewhere reference the C backend only in the context of generating an "execution model", namely the execute function clauses of an arch model. For my part, I did a small experiment where I wrote a very trivial and toy arch model, a single decode rule that uses the What I am asking for is: A clarification of the exact scope of the C backend. What is the subset of Sail that the C backend definitely 100% supports? Is there any known deviations or bugs in the backend within this subset? Is the generated code readable (unlikely given that it's generated from the low-level instructions in Sail IR form, but asking here just to make sure), and/or suitable in any way to be called from human-written code? Essentially, what I am asking is: is it worth it to write yet another generator, specialized for generating disassemblers in particular? Or is the C backend adequate enough and need only minor bug fixes and/or documentation and tutorials? This question may be somewhat "too late" since I have already written a first prototype of the specialized generator tool, it's nearly 1/2 of the C backend at about 1200 lines of code. I appreciate any feedback if this is necessary, or if what I want to achieve* could be achieved by clever usage of the C backend. If the answer to the last question is "yes", it could still be worth it to compare the 2 generators, and possibly rely on the C backend if possible when the RISC-V model uses features that isn't supported by my custom generator. * : "What I want to achieve" is generating a bunch of C files that faithfully emulates the logic of RISC-V decoding, defined as a bunch of mapping rules in the RISCV arch models. Hopefully the generated C is readable, but that's not strictly necessary. Am I reinventing half of a wheelIn the case of the C backend not suitable for my purposes, I have a feeling I'm still reinventing parts of Sail's implementation. For example, the type checker. One of the things that my generator has to do is computing the bitvector lengths of all arguments in an AST union case. This is in order that a bitvector destructuring expression like What I'm asking is: Can I use the output of Sail's type checker to do this? I'm using the function Is Sail designed to use as a libraryAs a final question: Is our way of usage supported by Sail or does it work by sheer luck? By "Our way of use" I mean including Sail as an OCaml library and building a tool on top of it. Is Sail's implementation written with this in mind or will we encounter friction because Sail was never meant to be used like this? Sorry for the wall of questions! Have a nice day. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The only thing that is not currently translated is the string parsing part of the mappings. If we wanted to translate those we might need to generate something more like a yacc grammar. In particular the string append pattern
The code within functions is not particularly readable. When we have to monomorphise function calls the generated names can become pretty gnarly, but in general the name-mangling scheme we use is predictable and doesn't mangle most names that much. I didn't intend for the generated C code to be called as a library originally (only that it would build an emulator). In practice people have done this a bunch, and it makes it tricky to improve the output as a library because there are now backwards-compatibility concerns. I think we could have an option that avoids any name-mangling where possible, and tries to produce a nicer library but would need a little bit of work.
I think parts of the C backend might be what you want, but it might require a bit of refactoring to make what you are trying to do easier.
Yes, I think the function you are looking for is
I think the answer to this is 'yes', but it's a rather qualified yes. The splitting into a main library and plugins for each backend is mostly for our own code organisation purposes - different backends can have varying levels of quality and maturity, and particularly experimental ones can in theory live out-of-tree until they are mature enough. Other people using it is not a problem, but we don't guarantee any kind of API compatibility between Sail versions. If people build something that is generally useful and wouldn't be a maintenance burden going forwards, then the ideal situation would be to upstream it. |
Beta Was this translation helpful? Give feedback.
The only thing that is not currently translated is the string parsing part of the mappings. If we wanted to translate those we might need to generate something more like a yacc grammar. In particular the string append pattern
x ^ y
doesn't have a good translation, as you can't decide how to split the input string without a somewhat sophisticated parsing approach. Right now be…