rems-project / sail

Sail architecture definition language
Other
617 stars 112 forks source link

Support class/struct-body output mode for C backend #547

Open Timmmm opened 5 months ago

Timmmm commented 5 months ago

As described here, if you are using C++ you can get multiple instances of a model without having to add an explicit context parameter everywhere (I believe there was an attempted "C2" backend that tried this at some point). Currently I have to do a few hacks to make it work but it would be nice to have an official mode that supported this.

This requires a flag that changes the C output slightly:

  1. Output no #includes at all.
  2. Don't output #ifndef __cplusplus. I don't think that is actually necessary at all.
  3. Don't output function declarations.
  4. Don't output any functions as static.
  5. Don't output void (*sail_rts_set_coverage_file)(const char *) = &sail_set_coverage_file;.

This allows creating independent model instances by wrapping the code in a struct or class like this:

struct Model : public ModelRuntime {
#include "model.c"
};

Where ModelRuntime is a class that provides all of the C functions that the model expects.

I think those would be fairly minimal changes to the compiler? Would it be ok if I added this feature?

Alasdair commented 5 months ago

Sorry I didn't reply earlier. Yes I think this would be fine.

I do have a local branch that wraps the C code generator in a parameterised module, which makes it much easier to make the behavior configurable. Maybe I should push that first?

Timmmm commented 5 months ago

Ah yeah that definitely makes sense. No rush though; our hacks are working for now.