Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Timmmm opened this issue May 20, 2024 · 2 comments
Open

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

Timmmm opened this issue May 20, 2024 · 2 comments

Comments

@Timmmm
Copy link
Contributor

Timmmm commented May 20, 2024

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?

@Timmmm Timmmm mentioned this issue May 20, 2024
@Alasdair
Copy link
Collaborator

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
Copy link
Contributor Author

Timmmm commented May 23, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants